openbsd: Split openbsd backend into separate openbsd and netbsd backends
[platform/upstream/libusb.git] / libusb / os / netbsd_usb.c
1 /*
2  * Copyright © 2011 Martin Pieuchot <mpi@openbsd.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 #include <sys/time.h>
20 #include <sys/types.h>
21
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28
29 #include <dev/usb/usb.h>
30
31 #include "libusb.h"
32 #include "libusbi.h"
33
34 struct device_priv {
35         char devnode[16];
36         int fd;
37
38         unsigned char *cdesc;                   /* active config descriptor */
39         usb_device_descriptor_t ddesc;          /* usb device descriptor */
40 };
41
42 struct handle_priv {
43         int pipe[2];                            /* for event notification */
44         int endpoints[USB_MAX_ENDPOINTS];
45 };
46
47 /*
48  * Backend functions
49  */
50 static int netbsd_get_device_list(struct libusb_context *,
51     struct discovered_devs **);
52 static int netbsd_open(struct libusb_device_handle *);
53 static void netbsd_close(struct libusb_device_handle *);
54
55 static int netbsd_get_device_descriptor(struct libusb_device *, unsigned char *,
56     int *);
57 static int netbsd_get_active_config_descriptor(struct libusb_device *,
58     unsigned char *, size_t, int *);
59 static int netbsd_get_config_descriptor(struct libusb_device *, uint8_t,
60     unsigned char *, size_t, int *);
61
62 static int netbsd_get_configuration(struct libusb_device_handle *, int *);
63 static int netbsd_set_configuration(struct libusb_device_handle *, int);
64
65 static int netbsd_claim_interface(struct libusb_device_handle *, int);
66 static int netbsd_release_interface(struct libusb_device_handle *, int);
67
68 static int netbsd_set_interface_altsetting(struct libusb_device_handle *, int,
69     int);
70 static int netbsd_clear_halt(struct libusb_device_handle *, unsigned char);
71 static int netbsd_reset_device(struct libusb_device_handle *);
72 static void netbsd_destroy_device(struct libusb_device *);
73
74 static int netbsd_submit_transfer(struct usbi_transfer *);
75 static int netbsd_cancel_transfer(struct usbi_transfer *);
76 static void netbsd_clear_transfer_priv(struct usbi_transfer *);
77 static int netbsd_handle_events(struct libusb_context *ctx, struct pollfd *,
78     nfds_t, int);
79 static int netbsd_clock_gettime(int, struct timespec *);
80
81 /*
82  * Private functions
83  */
84 static int _errno_to_libusb(int);
85 static int _cache_active_config_descriptor(struct libusb_device *, int);
86 static int _sync_control_transfer(struct usbi_transfer *);
87 static int _sync_gen_transfer(struct usbi_transfer *);
88 static int _access_endpoint(struct libusb_transfer *);
89
90 const struct usbi_os_backend netbsd_backend = {
91         "Synchronous NetBSD backend",
92         0,
93         NULL,                           /* init() */
94         NULL,                           /* exit() */
95         netbsd_get_device_list,
96         NULL,                           /* hotplug_poll */
97         netbsd_open,
98         netbsd_close,
99
100         netbsd_get_device_descriptor,
101         netbsd_get_active_config_descriptor,
102         netbsd_get_config_descriptor,
103         NULL,                           /* get_config_descriptor_by_value() */
104
105         netbsd_get_configuration,
106         netbsd_set_configuration,
107
108         netbsd_claim_interface,
109         netbsd_release_interface,
110
111         netbsd_set_interface_altsetting,
112         netbsd_clear_halt,
113         netbsd_reset_device,
114
115         NULL,                           /* kernel_driver_active() */
116         NULL,                           /* detach_kernel_driver() */
117         NULL,                           /* attach_kernel_driver() */
118
119         netbsd_destroy_device,
120
121         netbsd_submit_transfer,
122         netbsd_cancel_transfer,
123         netbsd_clear_transfer_priv,
124
125         netbsd_handle_events,
126
127         netbsd_clock_gettime,
128         sizeof(struct device_priv),
129         sizeof(struct handle_priv),
130         0,                              /* transfer_priv_size */
131         0,                              /* add_iso_packet_size */
132 };
133
134 int
135 netbsd_get_device_list(struct libusb_context * ctx,
136         struct discovered_devs **discdevs)
137 {
138         struct libusb_device *dev;
139         struct device_priv *dpriv;
140         struct usb_device_info di;
141         unsigned long session_id;
142         char devnode[16];
143         int fd, err, i;
144
145         usbi_dbg("");
146
147         /* Only ugen(4) is supported */
148         for (i = 0; i < USB_MAX_DEVICES; i++) {
149                 /* Control endpoint is always .00 */
150                 snprintf(devnode, sizeof(devnode), "/dev/ugen%d.00", i);
151
152                 if ((fd = open(devnode, O_RDONLY)) < 0) {
153                         if (errno != ENOENT && errno != ENXIO)
154                                 usbi_err(ctx, "could not open %s", devnode);
155                         continue;
156                 }
157
158                 if (ioctl(fd, USB_GET_DEVICEINFO, &di) < 0)
159                         continue;
160
161                 session_id = (di.udi_bus << 8 | di.udi_addr);
162                 dev = usbi_get_device_by_session_id(ctx, session_id);
163
164                 if (dev) {
165                         dev = libusb_ref_device(dev);
166                 } else {
167                         dev = usbi_alloc_device(ctx, session_id);
168                         if (dev == NULL)
169                                 return (LIBUSB_ERROR_NO_MEM);
170
171                         dev->bus_number = di.udi_bus;
172                         dev->device_address = di.udi_addr;
173                         dev->speed = di.udi_speed;
174
175                         dpriv = (struct device_priv *)dev->os_priv;
176                         strlcpy(dpriv->devnode, devnode, sizeof(devnode));
177                         dpriv->fd = -1;
178
179                         if (ioctl(fd, USB_GET_DEVICE_DESC, &dpriv->ddesc) < 0) {
180                                 err = errno;
181                                 goto error;
182                         }
183
184                         dpriv->cdesc = NULL;
185                         if (_cache_active_config_descriptor(dev, fd)) {
186                                 err = errno;
187                                 goto error;
188                         }
189
190                         if ((err = usbi_sanitize_device(dev)))
191                                 goto error;
192                 }
193                 close(fd);
194
195                 if (discovered_devs_append(*discdevs, dev) == NULL)
196                         return (LIBUSB_ERROR_NO_MEM);
197
198                 libusb_unref_device(dev);
199         }
200
201         return (LIBUSB_SUCCESS);
202
203 error:
204         close(fd);
205         libusb_unref_device(dev);
206         return _errno_to_libusb(err);
207 }
208
209 int
210 netbsd_open(struct libusb_device_handle *handle)
211 {
212         struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
213         struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
214
215         dpriv->fd = open(dpriv->devnode, O_RDWR);
216         if (dpriv->fd < 0) {
217                 dpriv->fd = open(dpriv->devnode, O_RDONLY);
218                 if (dpriv->fd < 0)
219                         return _errno_to_libusb(errno);
220         }
221
222         usbi_dbg("open %s: fd %d", dpriv->devnode, dpriv->fd);
223
224         if (pipe(hpriv->pipe) < 0)
225                 return _errno_to_libusb(errno);
226
227         return usbi_add_pollfd(HANDLE_CTX(handle), hpriv->pipe[0], POLLIN);
228 }
229
230 void
231 netbsd_close(struct libusb_device_handle *handle)
232 {
233         struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
234         struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
235
236         usbi_dbg("close: fd %d", dpriv->fd);
237
238         close(dpriv->fd);
239         dpriv->fd = -1;
240
241         usbi_remove_pollfd(HANDLE_CTX(handle), hpriv->pipe[0]);
242
243         close(hpriv->pipe[0]);
244         close(hpriv->pipe[1]);
245 }
246
247 int
248 netbsd_get_device_descriptor(struct libusb_device *dev, unsigned char *buf,
249     int *host_endian)
250 {
251         struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
252
253         usbi_dbg("");
254
255         memcpy(buf, &dpriv->ddesc, DEVICE_DESC_LENGTH);
256
257         *host_endian = 0;
258
259         return (LIBUSB_SUCCESS);
260 }
261
262 int
263 netbsd_get_active_config_descriptor(struct libusb_device *dev,
264     unsigned char *buf, size_t len, int *host_endian)
265 {
266         struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
267         usb_config_descriptor_t *ucd;
268
269         ucd = (usb_config_descriptor_t *) dpriv->cdesc;
270         len = MIN(len, UGETW(ucd->wTotalLength));
271
272         usbi_dbg("len %d", len);
273
274         memcpy(buf, dpriv->cdesc, len);
275
276         *host_endian = 0;
277
278         return len;
279 }
280
281 int
282 netbsd_get_config_descriptor(struct libusb_device *dev, uint8_t idx,
283     unsigned char *buf, size_t len, int *host_endian)
284 {
285         struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
286         struct usb_full_desc ufd;
287         int fd, err;
288
289         usbi_dbg("index %d, len %d", idx, len);
290
291         /* A config descriptor may be requested before opening the device */
292         if (dpriv->fd >= 0) {
293                 fd = dpriv->fd;
294         } else {
295                 fd = open(dpriv->devnode, O_RDONLY);
296                 if (fd < 0)
297                         return _errno_to_libusb(errno);
298         }
299
300         ufd.ufd_config_index = idx;
301         ufd.ufd_size = len;
302         ufd.ufd_data = buf;
303
304         if ((ioctl(fd, USB_GET_FULL_DESC, &ufd)) < 0) {
305                 err = errno;
306                 if (dpriv->fd < 0)
307                         close(fd);
308                 return _errno_to_libusb(err);
309         }
310
311         if (dpriv->fd < 0)
312                 close(fd);
313
314         *host_endian = 0;
315
316         return len;
317 }
318
319 int
320 netbsd_get_configuration(struct libusb_device_handle *handle, int *config)
321 {
322         struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
323
324         usbi_dbg("");
325
326         if (ioctl(dpriv->fd, USB_GET_CONFIG, config) < 0)
327                 return _errno_to_libusb(errno);
328
329         usbi_dbg("configuration %d", *config);
330
331         return (LIBUSB_SUCCESS);
332 }
333
334 int
335 netbsd_set_configuration(struct libusb_device_handle *handle, int config)
336 {
337         struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
338
339         usbi_dbg("configuration %d", config);
340
341         if (ioctl(dpriv->fd, USB_SET_CONFIG, &config) < 0)
342                 return _errno_to_libusb(errno);
343
344         return _cache_active_config_descriptor(handle->dev, dpriv->fd);
345 }
346
347 int
348 netbsd_claim_interface(struct libusb_device_handle *handle, int iface)
349 {
350         struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
351         int i;
352
353         for (i = 0; i < USB_MAX_ENDPOINTS; i++)
354                 hpriv->endpoints[i] = -1;
355
356         return (LIBUSB_SUCCESS);
357 }
358
359 int
360 netbsd_release_interface(struct libusb_device_handle *handle, int iface)
361 {
362         struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
363         int i;
364
365         for (i = 0; i < USB_MAX_ENDPOINTS; i++)
366                 if (hpriv->endpoints[i] >= 0)
367                         close(hpriv->endpoints[i]);
368
369         return (LIBUSB_SUCCESS);
370 }
371
372 int
373 netbsd_set_interface_altsetting(struct libusb_device_handle *handle, int iface,
374     int altsetting)
375 {
376         struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
377         struct usb_alt_interface intf;
378
379         usbi_dbg("iface %d, setting %d", iface, altsetting);
380
381         memset(&intf, 0, sizeof(intf));
382
383         intf.uai_interface_index = iface;
384         intf.uai_alt_no = altsetting;
385
386         if (ioctl(dpriv->fd, USB_SET_ALTINTERFACE, &intf) < 0)
387                 return _errno_to_libusb(errno);
388
389         return (LIBUSB_SUCCESS);
390 }
391
392 int
393 netbsd_clear_halt(struct libusb_device_handle *handle, unsigned char endpoint)
394 {
395         struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
396         struct usb_ctl_request req;
397
398         usbi_dbg("");
399
400         req.ucr_request.bmRequestType = UT_WRITE_ENDPOINT;
401         req.ucr_request.bRequest = UR_CLEAR_FEATURE;
402         USETW(req.ucr_request.wValue, UF_ENDPOINT_HALT);
403         USETW(req.ucr_request.wIndex, endpoint);
404         USETW(req.ucr_request.wLength, 0);
405
406         if (ioctl(dpriv->fd, USB_DO_REQUEST, &req) < 0)
407                 return _errno_to_libusb(errno);
408
409         return (LIBUSB_SUCCESS);
410 }
411
412 int
413 netbsd_reset_device(struct libusb_device_handle *handle)
414 {
415         usbi_dbg("");
416
417         return (LIBUSB_ERROR_NOT_SUPPORTED);
418 }
419
420 void
421 netbsd_destroy_device(struct libusb_device *dev)
422 {
423         struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
424
425         usbi_dbg("");
426
427         free(dpriv->cdesc);
428 }
429
430 int
431 netbsd_submit_transfer(struct usbi_transfer *itransfer)
432 {
433         struct libusb_transfer *transfer;
434         struct handle_priv *hpriv;
435         int err = 0;
436
437         usbi_dbg("");
438
439         transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
440         hpriv = (struct handle_priv *)transfer->dev_handle->os_priv;
441
442         switch (transfer->type) {
443         case LIBUSB_TRANSFER_TYPE_CONTROL:
444                 err = _sync_control_transfer(itransfer);
445                 break;
446         case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
447                 if (IS_XFEROUT(transfer)) {
448                         /* Isochronous write is not supported */
449                         err = LIBUSB_ERROR_NOT_SUPPORTED;
450                         break;
451                 }
452                 err = _sync_gen_transfer(itransfer);
453                 break;
454         case LIBUSB_TRANSFER_TYPE_BULK:
455         case LIBUSB_TRANSFER_TYPE_INTERRUPT:
456                 if (IS_XFEROUT(transfer) &&
457                     transfer->flags & LIBUSB_TRANSFER_ADD_ZERO_PACKET) {
458                         err = LIBUSB_ERROR_NOT_SUPPORTED;
459                         break;
460                 }
461                 err = _sync_gen_transfer(itransfer);
462                 break;
463         }
464
465         if (err)
466                 return (err);
467
468         if (write(hpriv->pipe[1], &itransfer, sizeof(itransfer)) < 0)
469                 return _errno_to_libusb(errno);
470
471         return (LIBUSB_SUCCESS);
472 }
473
474 int
475 netbsd_cancel_transfer(struct usbi_transfer *itransfer)
476 {
477         usbi_dbg("");
478
479         return (LIBUSB_ERROR_NOT_SUPPORTED);
480 }
481
482 void
483 netbsd_clear_transfer_priv(struct usbi_transfer *itransfer)
484 {
485         usbi_dbg("");
486
487         /* Nothing to do */
488 }
489
490 int
491 netbsd_handle_events(struct libusb_context *ctx, struct pollfd *fds, nfds_t nfds,
492     int num_ready)
493 {
494         struct libusb_device_handle *handle;
495         struct handle_priv *hpriv = NULL;
496         struct usbi_transfer *itransfer;
497         struct pollfd *pollfd;
498         int i, err = 0;
499
500         usbi_dbg("");
501
502         pthread_mutex_lock(&ctx->open_devs_lock);
503         for (i = 0; i < nfds && num_ready > 0; i++) {
504                 pollfd = &fds[i];
505
506                 if (!pollfd->revents)
507                         continue;
508
509                 hpriv = NULL;
510                 num_ready--;
511                 list_for_each_entry(handle, &ctx->open_devs, list,
512                     struct libusb_device_handle) {
513                         hpriv = (struct handle_priv *)handle->os_priv;
514
515                         if (hpriv->pipe[0] == pollfd->fd)
516                                 break;
517
518                         hpriv = NULL;
519                 }
520
521                 if (NULL == hpriv) {
522                         usbi_dbg("fd %d is not an event pipe!", pollfd->fd);
523                         err = ENOENT;
524                         break;
525                 }
526
527                 if (pollfd->revents & POLLERR) {
528                         usbi_remove_pollfd(HANDLE_CTX(handle), hpriv->pipe[0]);
529                         usbi_handle_disconnect(handle);
530                         continue;
531                 }
532
533                 if (read(hpriv->pipe[0], &itransfer, sizeof(itransfer)) < 0) {
534                         err = errno;
535                         break;
536                 }
537
538                 if ((err = usbi_handle_transfer_completion(itransfer,
539                     LIBUSB_TRANSFER_COMPLETED)))
540                         break;
541         }
542         pthread_mutex_unlock(&ctx->open_devs_lock);
543
544         if (err)
545                 return _errno_to_libusb(err);
546
547         return (LIBUSB_SUCCESS);
548 }
549
550 int
551 netbsd_clock_gettime(int clkid, struct timespec *tp)
552 {
553         usbi_dbg("clock %d", clkid);
554
555         if (clkid == USBI_CLOCK_REALTIME)
556                 return clock_gettime(CLOCK_REALTIME, tp);
557
558         if (clkid == USBI_CLOCK_MONOTONIC)
559                 return clock_gettime(CLOCK_MONOTONIC, tp);
560
561         return (LIBUSB_ERROR_INVALID_PARAM);
562 }
563
564 int
565 _errno_to_libusb(int err)
566 {
567         switch (err) {
568         case EIO:
569                 return (LIBUSB_ERROR_IO);
570         case EACCES:
571                 return (LIBUSB_ERROR_ACCESS);
572         case ENOENT:
573                 return (LIBUSB_ERROR_NO_DEVICE);
574         case ENOMEM:
575                 return (LIBUSB_ERROR_NO_MEM);
576         }
577
578         usbi_dbg("error: %s", strerror(err));
579
580         return (LIBUSB_ERROR_OTHER);
581 }
582
583 int
584 _cache_active_config_descriptor(struct libusb_device *dev, int fd)
585 {
586         struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
587         struct usb_config_desc ucd;
588         struct usb_full_desc ufd;
589         unsigned char* buf;
590         int len;
591
592         usbi_dbg("fd %d", fd);
593
594         ucd.ucd_config_index = USB_CURRENT_CONFIG_INDEX;
595
596         if ((ioctl(fd, USB_GET_CONFIG_DESC, &ucd)) < 0)
597                 return _errno_to_libusb(errno);
598
599         usbi_dbg("active bLength %d", ucd.ucd_desc.bLength);
600
601         len = UGETW(ucd.ucd_desc.wTotalLength);
602         buf = malloc(len);
603         if (buf == NULL)
604                 return (LIBUSB_ERROR_NO_MEM);
605
606         ufd.ufd_config_index = ucd.ucd_config_index;
607         ufd.ufd_size = len;
608         ufd.ufd_data = buf;
609
610         usbi_dbg("index %d, len %d", ufd.ufd_config_index, len);
611
612         if ((ioctl(fd, USB_GET_FULL_DESC, &ufd)) < 0) {
613                 free(buf);
614                 return _errno_to_libusb(errno);
615         }
616
617         if (dpriv->cdesc)
618                 free(dpriv->cdesc);
619         dpriv->cdesc = buf;
620
621         return (0);
622 }
623
624 int
625 _sync_control_transfer(struct usbi_transfer *itransfer)
626 {
627         struct libusb_transfer *transfer;
628         struct libusb_control_setup *setup;
629         struct device_priv *dpriv;
630         struct usb_ctl_request req;
631
632         transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
633         dpriv = (struct device_priv *)transfer->dev_handle->dev->os_priv;
634         setup = (struct libusb_control_setup *)transfer->buffer;
635
636         usbi_dbg("type %d request %d value %d index %d length %d timeout %d",
637             setup->bmRequestType, setup->bRequest,
638             libusb_le16_to_cpu(setup->wValue),
639             libusb_le16_to_cpu(setup->wIndex),
640             libusb_le16_to_cpu(setup->wLength), transfer->timeout);
641
642         req.ucr_request.bmRequestType = setup->bmRequestType;
643         req.ucr_request.bRequest = setup->bRequest;
644         /* Don't use USETW, libusbx already deals with the endianness */
645         (*(uint16_t *)req.ucr_request.wValue) = setup->wValue;
646         (*(uint16_t *)req.ucr_request.wIndex) = setup->wIndex;
647         (*(uint16_t *)req.ucr_request.wLength) = setup->wLength;
648         req.ucr_data = transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE;
649
650         if ((transfer->flags & LIBUSB_TRANSFER_SHORT_NOT_OK) == 0)
651                 req.ucr_flags = USBD_SHORT_XFER_OK;
652
653         if ((ioctl(dpriv->fd, USB_SET_TIMEOUT, &transfer->timeout)) < 0)
654                 return _errno_to_libusb(errno);
655
656         if ((ioctl(dpriv->fd, USB_DO_REQUEST, &req)) < 0)
657                 return _errno_to_libusb(errno);
658
659         itransfer->transferred = req.ucr_actlen;
660
661         usbi_dbg("transferred %d", itransfer->transferred);
662
663         return (0);
664 }
665
666 int
667 _access_endpoint(struct libusb_transfer *transfer)
668 {
669         struct handle_priv *hpriv;
670         struct device_priv *dpriv;
671         char *s, devnode[16];
672         int fd, endpt;
673         mode_t mode;
674
675         hpriv = (struct handle_priv *)transfer->dev_handle->os_priv;
676         dpriv = (struct device_priv *)transfer->dev_handle->dev->os_priv;
677
678         endpt = UE_GET_ADDR(transfer->endpoint);
679         mode = IS_XFERIN(transfer) ? O_RDONLY : O_WRONLY;
680
681         usbi_dbg("endpoint %d mode %d", endpt, mode);
682
683         if (hpriv->endpoints[endpt] < 0) {
684                 /* Pick the right node given the control one */
685                 strlcpy(devnode, dpriv->devnode, sizeof(devnode));
686                 s = strchr(devnode, '.');
687                 snprintf(s, 4, ".%02d", endpt);
688
689                 /* We may need to read/write to the same endpoint later. */
690                 if (((fd = open(devnode, O_RDWR)) < 0) && (errno == ENXIO))
691                         if ((fd = open(devnode, mode)) < 0)
692                                 return (-1);
693
694                 hpriv->endpoints[endpt] = fd;
695         }
696
697         return (hpriv->endpoints[endpt]);
698 }
699
700 int
701 _sync_gen_transfer(struct usbi_transfer *itransfer)
702 {
703         struct libusb_transfer *transfer;
704         int fd, nr = 1;
705
706         transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
707
708         /*
709          * Bulk, Interrupt or Isochronous transfer depends on the
710          * endpoint and thus the node to open.
711          */
712         if ((fd = _access_endpoint(transfer)) < 0)
713                 return _errno_to_libusb(errno);
714
715         if ((ioctl(fd, USB_SET_TIMEOUT, &transfer->timeout)) < 0)
716                 return _errno_to_libusb(errno);
717
718         if (IS_XFERIN(transfer)) {
719                 if ((transfer->flags & LIBUSB_TRANSFER_SHORT_NOT_OK) == 0)
720                         if ((ioctl(fd, USB_SET_SHORT_XFER, &nr)) < 0)
721                                 return _errno_to_libusb(errno);
722
723                 nr = read(fd, transfer->buffer, transfer->length);
724         } else {
725                 nr = write(fd, transfer->buffer, transfer->length);
726         }
727
728         if (nr < 0)
729                 return _errno_to_libusb(errno);
730
731         itransfer->transferred = nr;
732
733         return (0);
734 }