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