Make usbi_get_device_by_session_id return a ref to the found device
[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 == NULL) {
165                         dev = usbi_alloc_device(ctx, session_id);
166                         if (dev == NULL)
167                                 return (LIBUSB_ERROR_NO_MEM);
168
169                         dev->bus_number = di.udi_bus;
170                         dev->device_address = di.udi_addr;
171                         dev->speed = di.udi_speed;
172
173                         dpriv = (struct device_priv *)dev->os_priv;
174                         strlcpy(dpriv->devnode, devnode, sizeof(devnode));
175                         dpriv->fd = -1;
176
177                         if (ioctl(fd, USB_GET_DEVICE_DESC, &dpriv->ddesc) < 0) {
178                                 err = errno;
179                                 goto error;
180                         }
181
182                         dpriv->cdesc = NULL;
183                         if (_cache_active_config_descriptor(dev, fd)) {
184                                 err = errno;
185                                 goto error;
186                         }
187
188                         if ((err = usbi_sanitize_device(dev)))
189                                 goto error;
190                 }
191                 close(fd);
192
193                 if (discovered_devs_append(*discdevs, dev) == NULL)
194                         return (LIBUSB_ERROR_NO_MEM);
195
196                 libusb_unref_device(dev);
197         }
198
199         return (LIBUSB_SUCCESS);
200
201 error:
202         close(fd);
203         libusb_unref_device(dev);
204         return _errno_to_libusb(err);
205 }
206
207 int
208 netbsd_open(struct libusb_device_handle *handle)
209 {
210         struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
211         struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
212
213         dpriv->fd = open(dpriv->devnode, O_RDWR);
214         if (dpriv->fd < 0) {
215                 dpriv->fd = open(dpriv->devnode, O_RDONLY);
216                 if (dpriv->fd < 0)
217                         return _errno_to_libusb(errno);
218         }
219
220         usbi_dbg("open %s: fd %d", dpriv->devnode, dpriv->fd);
221
222         if (pipe(hpriv->pipe) < 0)
223                 return _errno_to_libusb(errno);
224
225         return usbi_add_pollfd(HANDLE_CTX(handle), hpriv->pipe[0], POLLIN);
226 }
227
228 void
229 netbsd_close(struct libusb_device_handle *handle)
230 {
231         struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
232         struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
233
234         usbi_dbg("close: fd %d", dpriv->fd);
235
236         close(dpriv->fd);
237         dpriv->fd = -1;
238
239         usbi_remove_pollfd(HANDLE_CTX(handle), hpriv->pipe[0]);
240
241         close(hpriv->pipe[0]);
242         close(hpriv->pipe[1]);
243 }
244
245 int
246 netbsd_get_device_descriptor(struct libusb_device *dev, unsigned char *buf,
247     int *host_endian)
248 {
249         struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
250
251         usbi_dbg("");
252
253         memcpy(buf, &dpriv->ddesc, DEVICE_DESC_LENGTH);
254
255         *host_endian = 0;
256
257         return (LIBUSB_SUCCESS);
258 }
259
260 int
261 netbsd_get_active_config_descriptor(struct libusb_device *dev,
262     unsigned char *buf, size_t len, int *host_endian)
263 {
264         struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
265         usb_config_descriptor_t *ucd;
266
267         ucd = (usb_config_descriptor_t *) dpriv->cdesc;
268         len = MIN(len, UGETW(ucd->wTotalLength));
269
270         usbi_dbg("len %d", len);
271
272         memcpy(buf, dpriv->cdesc, len);
273
274         *host_endian = 0;
275
276         return len;
277 }
278
279 int
280 netbsd_get_config_descriptor(struct libusb_device *dev, uint8_t idx,
281     unsigned char *buf, size_t len, int *host_endian)
282 {
283         struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
284         struct usb_full_desc ufd;
285         int fd, err;
286
287         usbi_dbg("index %d, len %d", idx, len);
288
289         /* A config descriptor may be requested before opening the device */
290         if (dpriv->fd >= 0) {
291                 fd = dpriv->fd;
292         } else {
293                 fd = open(dpriv->devnode, O_RDONLY);
294                 if (fd < 0)
295                         return _errno_to_libusb(errno);
296         }
297
298         ufd.ufd_config_index = idx;
299         ufd.ufd_size = len;
300         ufd.ufd_data = buf;
301
302         if ((ioctl(fd, USB_GET_FULL_DESC, &ufd)) < 0) {
303                 err = errno;
304                 if (dpriv->fd < 0)
305                         close(fd);
306                 return _errno_to_libusb(err);
307         }
308
309         if (dpriv->fd < 0)
310                 close(fd);
311
312         *host_endian = 0;
313
314         return len;
315 }
316
317 int
318 netbsd_get_configuration(struct libusb_device_handle *handle, int *config)
319 {
320         struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
321
322         usbi_dbg("");
323
324         if (ioctl(dpriv->fd, USB_GET_CONFIG, config) < 0)
325                 return _errno_to_libusb(errno);
326
327         usbi_dbg("configuration %d", *config);
328
329         return (LIBUSB_SUCCESS);
330 }
331
332 int
333 netbsd_set_configuration(struct libusb_device_handle *handle, int config)
334 {
335         struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
336
337         usbi_dbg("configuration %d", config);
338
339         if (ioctl(dpriv->fd, USB_SET_CONFIG, &config) < 0)
340                 return _errno_to_libusb(errno);
341
342         return _cache_active_config_descriptor(handle->dev, dpriv->fd);
343 }
344
345 int
346 netbsd_claim_interface(struct libusb_device_handle *handle, int iface)
347 {
348         struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
349         int i;
350
351         for (i = 0; i < USB_MAX_ENDPOINTS; i++)
352                 hpriv->endpoints[i] = -1;
353
354         return (LIBUSB_SUCCESS);
355 }
356
357 int
358 netbsd_release_interface(struct libusb_device_handle *handle, int iface)
359 {
360         struct handle_priv *hpriv = (struct handle_priv *)handle->os_priv;
361         int i;
362
363         for (i = 0; i < USB_MAX_ENDPOINTS; i++)
364                 if (hpriv->endpoints[i] >= 0)
365                         close(hpriv->endpoints[i]);
366
367         return (LIBUSB_SUCCESS);
368 }
369
370 int
371 netbsd_set_interface_altsetting(struct libusb_device_handle *handle, int iface,
372     int altsetting)
373 {
374         struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
375         struct usb_alt_interface intf;
376
377         usbi_dbg("iface %d, setting %d", iface, altsetting);
378
379         memset(&intf, 0, sizeof(intf));
380
381         intf.uai_interface_index = iface;
382         intf.uai_alt_no = altsetting;
383
384         if (ioctl(dpriv->fd, USB_SET_ALTINTERFACE, &intf) < 0)
385                 return _errno_to_libusb(errno);
386
387         return (LIBUSB_SUCCESS);
388 }
389
390 int
391 netbsd_clear_halt(struct libusb_device_handle *handle, unsigned char endpoint)
392 {
393         struct device_priv *dpriv = (struct device_priv *)handle->dev->os_priv;
394         struct usb_ctl_request req;
395
396         usbi_dbg("");
397
398         req.ucr_request.bmRequestType = UT_WRITE_ENDPOINT;
399         req.ucr_request.bRequest = UR_CLEAR_FEATURE;
400         USETW(req.ucr_request.wValue, UF_ENDPOINT_HALT);
401         USETW(req.ucr_request.wIndex, endpoint);
402         USETW(req.ucr_request.wLength, 0);
403
404         if (ioctl(dpriv->fd, USB_DO_REQUEST, &req) < 0)
405                 return _errno_to_libusb(errno);
406
407         return (LIBUSB_SUCCESS);
408 }
409
410 int
411 netbsd_reset_device(struct libusb_device_handle *handle)
412 {
413         usbi_dbg("");
414
415         return (LIBUSB_ERROR_NOT_SUPPORTED);
416 }
417
418 void
419 netbsd_destroy_device(struct libusb_device *dev)
420 {
421         struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
422
423         usbi_dbg("");
424
425         free(dpriv->cdesc);
426 }
427
428 int
429 netbsd_submit_transfer(struct usbi_transfer *itransfer)
430 {
431         struct libusb_transfer *transfer;
432         struct handle_priv *hpriv;
433         int err = 0;
434
435         usbi_dbg("");
436
437         transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
438         hpriv = (struct handle_priv *)transfer->dev_handle->os_priv;
439
440         switch (transfer->type) {
441         case LIBUSB_TRANSFER_TYPE_CONTROL:
442                 err = _sync_control_transfer(itransfer);
443                 break;
444         case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
445                 if (IS_XFEROUT(transfer)) {
446                         /* Isochronous write is not supported */
447                         err = LIBUSB_ERROR_NOT_SUPPORTED;
448                         break;
449                 }
450                 err = _sync_gen_transfer(itransfer);
451                 break;
452         case LIBUSB_TRANSFER_TYPE_BULK:
453         case LIBUSB_TRANSFER_TYPE_INTERRUPT:
454                 if (IS_XFEROUT(transfer) &&
455                     transfer->flags & LIBUSB_TRANSFER_ADD_ZERO_PACKET) {
456                         err = LIBUSB_ERROR_NOT_SUPPORTED;
457                         break;
458                 }
459                 err = _sync_gen_transfer(itransfer);
460                 break;
461         }
462
463         if (err)
464                 return (err);
465
466         if (write(hpriv->pipe[1], &itransfer, sizeof(itransfer)) < 0)
467                 return _errno_to_libusb(errno);
468
469         return (LIBUSB_SUCCESS);
470 }
471
472 int
473 netbsd_cancel_transfer(struct usbi_transfer *itransfer)
474 {
475         usbi_dbg("");
476
477         return (LIBUSB_ERROR_NOT_SUPPORTED);
478 }
479
480 void
481 netbsd_clear_transfer_priv(struct usbi_transfer *itransfer)
482 {
483         usbi_dbg("");
484
485         /* Nothing to do */
486 }
487
488 int
489 netbsd_handle_events(struct libusb_context *ctx, struct pollfd *fds, nfds_t nfds,
490     int num_ready)
491 {
492         struct libusb_device_handle *handle;
493         struct handle_priv *hpriv = NULL;
494         struct usbi_transfer *itransfer;
495         struct pollfd *pollfd;
496         int i, err = 0;
497
498         usbi_dbg("");
499
500         pthread_mutex_lock(&ctx->open_devs_lock);
501         for (i = 0; i < nfds && num_ready > 0; i++) {
502                 pollfd = &fds[i];
503
504                 if (!pollfd->revents)
505                         continue;
506
507                 hpriv = NULL;
508                 num_ready--;
509                 list_for_each_entry(handle, &ctx->open_devs, list,
510                     struct libusb_device_handle) {
511                         hpriv = (struct handle_priv *)handle->os_priv;
512
513                         if (hpriv->pipe[0] == pollfd->fd)
514                                 break;
515
516                         hpriv = NULL;
517                 }
518
519                 if (NULL == hpriv) {
520                         usbi_dbg("fd %d is not an event pipe!", pollfd->fd);
521                         err = ENOENT;
522                         break;
523                 }
524
525                 if (pollfd->revents & POLLERR) {
526                         usbi_remove_pollfd(HANDLE_CTX(handle), hpriv->pipe[0]);
527                         usbi_handle_disconnect(handle);
528                         continue;
529                 }
530
531                 if (read(hpriv->pipe[0], &itransfer, sizeof(itransfer)) < 0) {
532                         err = errno;
533                         break;
534                 }
535
536                 if ((err = usbi_handle_transfer_completion(itransfer,
537                     LIBUSB_TRANSFER_COMPLETED)))
538                         break;
539         }
540         pthread_mutex_unlock(&ctx->open_devs_lock);
541
542         if (err)
543                 return _errno_to_libusb(err);
544
545         return (LIBUSB_SUCCESS);
546 }
547
548 int
549 netbsd_clock_gettime(int clkid, struct timespec *tp)
550 {
551         usbi_dbg("clock %d", clkid);
552
553         if (clkid == USBI_CLOCK_REALTIME)
554                 return clock_gettime(CLOCK_REALTIME, tp);
555
556         if (clkid == USBI_CLOCK_MONOTONIC)
557                 return clock_gettime(CLOCK_MONOTONIC, tp);
558
559         return (LIBUSB_ERROR_INVALID_PARAM);
560 }
561
562 int
563 _errno_to_libusb(int err)
564 {
565         switch (err) {
566         case EIO:
567                 return (LIBUSB_ERROR_IO);
568         case EACCES:
569                 return (LIBUSB_ERROR_ACCESS);
570         case ENOENT:
571                 return (LIBUSB_ERROR_NO_DEVICE);
572         case ENOMEM:
573                 return (LIBUSB_ERROR_NO_MEM);
574         }
575
576         usbi_dbg("error: %s", strerror(err));
577
578         return (LIBUSB_ERROR_OTHER);
579 }
580
581 int
582 _cache_active_config_descriptor(struct libusb_device *dev, int fd)
583 {
584         struct device_priv *dpriv = (struct device_priv *)dev->os_priv;
585         struct usb_config_desc ucd;
586         struct usb_full_desc ufd;
587         unsigned char* buf;
588         int len;
589
590         usbi_dbg("fd %d", fd);
591
592         ucd.ucd_config_index = USB_CURRENT_CONFIG_INDEX;
593
594         if ((ioctl(fd, USB_GET_CONFIG_DESC, &ucd)) < 0)
595                 return _errno_to_libusb(errno);
596
597         usbi_dbg("active bLength %d", ucd.ucd_desc.bLength);
598
599         len = UGETW(ucd.ucd_desc.wTotalLength);
600         buf = malloc(len);
601         if (buf == NULL)
602                 return (LIBUSB_ERROR_NO_MEM);
603
604         ufd.ufd_config_index = ucd.ucd_config_index;
605         ufd.ufd_size = len;
606         ufd.ufd_data = buf;
607
608         usbi_dbg("index %d, len %d", ufd.ufd_config_index, len);
609
610         if ((ioctl(fd, USB_GET_FULL_DESC, &ufd)) < 0) {
611                 free(buf);
612                 return _errno_to_libusb(errno);
613         }
614
615         if (dpriv->cdesc)
616                 free(dpriv->cdesc);
617         dpriv->cdesc = buf;
618
619         return (0);
620 }
621
622 int
623 _sync_control_transfer(struct usbi_transfer *itransfer)
624 {
625         struct libusb_transfer *transfer;
626         struct libusb_control_setup *setup;
627         struct device_priv *dpriv;
628         struct usb_ctl_request req;
629
630         transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
631         dpriv = (struct device_priv *)transfer->dev_handle->dev->os_priv;
632         setup = (struct libusb_control_setup *)transfer->buffer;
633
634         usbi_dbg("type %d request %d value %d index %d length %d timeout %d",
635             setup->bmRequestType, setup->bRequest,
636             libusb_le16_to_cpu(setup->wValue),
637             libusb_le16_to_cpu(setup->wIndex),
638             libusb_le16_to_cpu(setup->wLength), transfer->timeout);
639
640         req.ucr_request.bmRequestType = setup->bmRequestType;
641         req.ucr_request.bRequest = setup->bRequest;
642         /* Don't use USETW, libusbx already deals with the endianness */
643         (*(uint16_t *)req.ucr_request.wValue) = setup->wValue;
644         (*(uint16_t *)req.ucr_request.wIndex) = setup->wIndex;
645         (*(uint16_t *)req.ucr_request.wLength) = setup->wLength;
646         req.ucr_data = transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE;
647
648         if ((transfer->flags & LIBUSB_TRANSFER_SHORT_NOT_OK) == 0)
649                 req.ucr_flags = USBD_SHORT_XFER_OK;
650
651         if ((ioctl(dpriv->fd, USB_SET_TIMEOUT, &transfer->timeout)) < 0)
652                 return _errno_to_libusb(errno);
653
654         if ((ioctl(dpriv->fd, USB_DO_REQUEST, &req)) < 0)
655                 return _errno_to_libusb(errno);
656
657         itransfer->transferred = req.ucr_actlen;
658
659         usbi_dbg("transferred %d", itransfer->transferred);
660
661         return (0);
662 }
663
664 int
665 _access_endpoint(struct libusb_transfer *transfer)
666 {
667         struct handle_priv *hpriv;
668         struct device_priv *dpriv;
669         char *s, devnode[16];
670         int fd, endpt;
671         mode_t mode;
672
673         hpriv = (struct handle_priv *)transfer->dev_handle->os_priv;
674         dpriv = (struct device_priv *)transfer->dev_handle->dev->os_priv;
675
676         endpt = UE_GET_ADDR(transfer->endpoint);
677         mode = IS_XFERIN(transfer) ? O_RDONLY : O_WRONLY;
678
679         usbi_dbg("endpoint %d mode %d", endpt, mode);
680
681         if (hpriv->endpoints[endpt] < 0) {
682                 /* Pick the right node given the control one */
683                 strlcpy(devnode, dpriv->devnode, sizeof(devnode));
684                 s = strchr(devnode, '.');
685                 snprintf(s, 4, ".%02d", endpt);
686
687                 /* We may need to read/write to the same endpoint later. */
688                 if (((fd = open(devnode, O_RDWR)) < 0) && (errno == ENXIO))
689                         if ((fd = open(devnode, mode)) < 0)
690                                 return (-1);
691
692                 hpriv->endpoints[endpt] = fd;
693         }
694
695         return (hpriv->endpoints[endpt]);
696 }
697
698 int
699 _sync_gen_transfer(struct usbi_transfer *itransfer)
700 {
701         struct libusb_transfer *transfer;
702         int fd, nr = 1;
703
704         transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer);
705
706         /*
707          * Bulk, Interrupt or Isochronous transfer depends on the
708          * endpoint and thus the node to open.
709          */
710         if ((fd = _access_endpoint(transfer)) < 0)
711                 return _errno_to_libusb(errno);
712
713         if ((ioctl(fd, USB_SET_TIMEOUT, &transfer->timeout)) < 0)
714                 return _errno_to_libusb(errno);
715
716         if (IS_XFERIN(transfer)) {
717                 if ((transfer->flags & LIBUSB_TRANSFER_SHORT_NOT_OK) == 0)
718                         if ((ioctl(fd, USB_SET_SHORT_XFER, &nr)) < 0)
719                                 return _errno_to_libusb(errno);
720
721                 nr = read(fd, transfer->buffer, transfer->length);
722         } else {
723                 nr = write(fd, transfer->buffer, transfer->length);
724         }
725
726         if (nr < 0)
727                 return _errno_to_libusb(errno);
728
729         itransfer->transferred = nr;
730
731         return (0);
732 }