]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - lib/libusb/libusb10.c
MFC r203774
[FreeBSD/stable/8.git] / lib / libusb / libusb10.c
1 /* $FreeBSD$ */
2 /*-
3  * Copyright (c) 2009 Sylvestre Gallon. All rights reserved.
4  * Copyright (c) 2009 Hans Petter Selasky. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27
28 #include <assert.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <stdio.h>
32 #include <poll.h>
33 #include <pthread.h>
34 #include <errno.h>
35 #include <sys/ioctl.h>
36 #include <sys/fcntl.h>
37 #include <sys/queue.h>
38
39 #define libusb_device_handle libusb20_device
40
41 #include "libusb20.h"
42 #include "libusb20_desc.h"
43 #include "libusb20_int.h"
44 #include "libusb.h"
45 #include "libusb10.h"
46
47 static pthread_mutex_t default_context_lock = PTHREAD_MUTEX_INITIALIZER;
48 struct libusb_context *usbi_default_context = NULL;
49
50 /* Prototypes */
51
52 static struct libusb20_transfer *libusb10_get_transfer(struct libusb20_device *, uint8_t, uint8_t);
53 static int libusb10_get_maxframe(struct libusb20_device *, libusb_transfer *);
54 static int libusb10_get_buffsize(struct libusb20_device *, libusb_transfer *);
55 static int libusb10_convert_error(uint8_t status);
56 static void libusb10_complete_transfer(struct libusb20_transfer *, struct libusb_super_transfer *, int);
57 static void libusb10_isoc_proxy(struct libusb20_transfer *);
58 static void libusb10_bulk_intr_proxy(struct libusb20_transfer *);
59 static void libusb10_ctrl_proxy(struct libusb20_transfer *);
60 static void libusb10_submit_transfer_sub(struct libusb20_device *, uint8_t);
61
62 /*  Library initialisation / deinitialisation */
63
64 void
65 libusb_set_debug(libusb_context *ctx, int level)
66 {
67         ctx = GET_CONTEXT(ctx);
68         if (ctx)
69                 ctx->debug = level;
70 }
71
72 int
73 libusb_init(libusb_context **context)
74 {
75         struct libusb_context *ctx;
76         char *debug;
77         int flag;
78         int ret;
79
80         ctx = malloc(sizeof(*ctx));
81         if (!ctx)
82                 return (LIBUSB_ERROR_INVALID_PARAM);
83
84         memset(ctx, 0, sizeof(*ctx));
85
86         debug = getenv("LIBUSB_DEBUG");
87         if (debug != NULL) {
88                 ctx->debug = atoi(debug);
89                 if (ctx->debug != 0)
90                         ctx->debug_fixed = 1;
91         }
92         TAILQ_INIT(&ctx->pollfds);
93         TAILQ_INIT(&ctx->tr_done);
94
95         pthread_mutex_init(&ctx->ctx_lock, NULL);
96         pthread_cond_init(&ctx->ctx_cond, NULL);
97
98         ctx->ctx_handler = NO_THREAD;
99
100         ret = pipe(ctx->ctrl_pipe);
101         if (ret < 0) {
102                 pthread_mutex_destroy(&ctx->ctx_lock);
103                 pthread_cond_destroy(&ctx->ctx_cond);
104                 free(ctx);
105                 return (LIBUSB_ERROR_OTHER);
106         }
107         /* set non-blocking mode on the control pipe to avoid deadlock */
108         flag = 1;
109         ret = fcntl(ctx->ctrl_pipe[0], O_NONBLOCK, &flag);
110         assert(ret != -1 && "Couldn't set O_NONBLOCK for ctx->ctrl_pipe[0]");
111         flag = 1;
112         ret = fcntl(ctx->ctrl_pipe[1], O_NONBLOCK, &flag);
113         assert(ret != -1 && "Couldn't set O_NONBLOCK for ctx->ctrl_pipe[1]");
114
115         libusb10_add_pollfd(ctx, &ctx->ctx_poll, NULL, ctx->ctrl_pipe[0], POLLIN);
116
117         pthread_mutex_lock(&default_context_lock);
118         if (usbi_default_context == NULL) {
119                 usbi_default_context = ctx;
120         }
121         pthread_mutex_unlock(&default_context_lock);
122
123         if (context)
124                 *context = ctx;
125
126         DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_init complete");
127
128         return (0);
129 }
130
131 void
132 libusb_exit(libusb_context *ctx)
133 {
134         ctx = GET_CONTEXT(ctx);
135
136         if (ctx == NULL)
137                 return;
138
139         /* XXX cleanup devices */
140
141         libusb10_remove_pollfd(ctx, &ctx->ctx_poll);
142         close(ctx->ctrl_pipe[0]);
143         close(ctx->ctrl_pipe[1]);
144         pthread_mutex_destroy(&ctx->ctx_lock);
145         pthread_cond_destroy(&ctx->ctx_cond);
146
147         pthread_mutex_lock(&default_context_lock);
148         if (ctx == usbi_default_context) {
149                 usbi_default_context = NULL;
150         }
151         pthread_mutex_unlock(&default_context_lock);
152
153         free(ctx);
154 }
155
156 /* Device handling and initialisation. */
157
158 ssize_t
159 libusb_get_device_list(libusb_context *ctx, libusb_device ***list)
160 {
161         struct libusb20_backend *usb_backend;
162         struct libusb20_device *pdev;
163         struct libusb_device *dev;
164         int i;
165
166         ctx = GET_CONTEXT(ctx);
167
168         if (ctx == NULL)
169                 return (LIBUSB_ERROR_INVALID_PARAM);
170
171         if (list == NULL)
172                 return (LIBUSB_ERROR_INVALID_PARAM);
173
174         usb_backend = libusb20_be_alloc_default();
175         if (usb_backend == NULL)
176                 return (LIBUSB_ERROR_NO_MEM);
177
178         /* figure out how many USB devices are present */
179         pdev = NULL;
180         i = 0;
181         while ((pdev = libusb20_be_device_foreach(usb_backend, pdev)))
182                 i++;
183
184         /* allocate device pointer list */
185         *list = malloc((i + 1) * sizeof(void *));
186         if (*list == NULL) {
187                 libusb20_be_free(usb_backend);
188                 return (LIBUSB_ERROR_NO_MEM);
189         }
190         /* create libusb v1.0 compliant devices */
191         i = 0;
192         while ((pdev = libusb20_be_device_foreach(usb_backend, NULL))) {
193
194                 dev = malloc(sizeof(*dev));
195                 if (dev == NULL) {
196                         while (i != 0) {
197                                 libusb_unref_device((*list)[i - 1]);
198                                 i--;
199                         }
200                         free(*list);
201                         *list = NULL;
202                         libusb20_be_free(usb_backend);
203                         return (LIBUSB_ERROR_NO_MEM);
204                 }
205
206                 /* get device into libUSB v1.0 list */
207                 libusb20_be_dequeue_device(usb_backend, pdev);
208
209                 memset(dev, 0, sizeof(*dev));
210
211                 /* init transfer queues */
212                 TAILQ_INIT(&dev->tr_head);
213
214                 /* set context we belong to */
215                 dev->ctx = ctx;
216
217                 /* link together the two structures */
218                 dev->os_priv = pdev;
219                 pdev->privLuData = dev;
220
221                 (*list)[i] = libusb_ref_device(dev);
222                 i++;
223         }
224         (*list)[i] = NULL;
225
226         libusb20_be_free(usb_backend);
227         return (i);
228 }
229
230 void
231 libusb_free_device_list(libusb_device **list, int unref_devices)
232 {
233         int i;
234
235         if (list == NULL)
236                 return;                 /* be NULL safe */
237
238         if (unref_devices) {
239                 for (i = 0; list[i] != NULL; i++)
240                         libusb_unref_device(list[i]);
241         }
242         free(list);
243 }
244
245 uint8_t
246 libusb_get_bus_number(libusb_device *dev)
247 {
248         if (dev == NULL)
249                 return (0);             /* should not happen */
250         return (libusb20_dev_get_bus_number(dev->os_priv));
251 }
252
253 uint8_t
254 libusb_get_device_address(libusb_device *dev)
255 {
256         if (dev == NULL)
257                 return (0);             /* should not happen */
258         return (libusb20_dev_get_address(dev->os_priv));
259 }
260
261 int
262 libusb_get_max_packet_size(libusb_device *dev, uint8_t endpoint)
263 {
264         struct libusb_config_descriptor *pdconf;
265         struct libusb_interface *pinf;
266         struct libusb_interface_descriptor *pdinf;
267         struct libusb_endpoint_descriptor *pdend;
268         int i;
269         int j;
270         int k;
271         int ret;
272
273         if (dev == NULL)
274                 return (LIBUSB_ERROR_NO_DEVICE);
275
276         ret = libusb_get_active_config_descriptor(dev, &pdconf);
277         if (ret < 0)
278                 return (ret);
279
280         ret = LIBUSB_ERROR_NOT_FOUND;
281         for (i = 0; i < pdconf->bNumInterfaces; i++) {
282                 pinf = &pdconf->interface[i];
283                 for (j = 0; j < pinf->num_altsetting; j++) {
284                         pdinf = &pinf->altsetting[j];
285                         for (k = 0; k < pdinf->bNumEndpoints; k++) {
286                                 pdend = &pdinf->endpoint[k];
287                                 if (pdend->bEndpointAddress == endpoint) {
288                                         ret = pdend->wMaxPacketSize;
289                                         goto out;
290                                 }
291                         }
292                 }
293         }
294
295 out:
296         libusb_free_config_descriptor(pdconf);
297         return (ret);
298 }
299
300 libusb_device *
301 libusb_ref_device(libusb_device *dev)
302 {
303         if (dev == NULL)
304                 return (NULL);          /* be NULL safe */
305
306         CTX_LOCK(dev->ctx);
307         dev->refcnt++;
308         CTX_UNLOCK(dev->ctx);
309
310         return (dev);
311 }
312
313 void
314 libusb_unref_device(libusb_device *dev)
315 {
316         if (dev == NULL)
317                 return;                 /* be NULL safe */
318
319         CTX_LOCK(dev->ctx);
320         dev->refcnt--;
321         CTX_UNLOCK(dev->ctx);
322
323         if (dev->refcnt == 0) {
324                 libusb20_dev_free(dev->os_priv);
325                 free(dev);
326         }
327 }
328
329 int
330 libusb_open(libusb_device *dev, libusb_device_handle **devh)
331 {
332         libusb_context *ctx = dev->ctx;
333         struct libusb20_device *pdev = dev->os_priv;
334         uint8_t dummy;
335         int err;
336
337         if (devh == NULL)
338                 return (LIBUSB_ERROR_INVALID_PARAM);
339
340         /* set default device handle value */
341         *devh = NULL;
342
343         dev = libusb_ref_device(dev);
344         if (dev == NULL)
345                 return (LIBUSB_ERROR_INVALID_PARAM);
346
347         err = libusb20_dev_open(pdev, 16 * 4 /* number of endpoints */ );
348         if (err) {
349                 libusb_unref_device(dev);
350                 return (LIBUSB_ERROR_NO_MEM);
351         }
352         libusb10_add_pollfd(ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
353             POLLOUT | POLLRDNORM | POLLWRNORM);
354
355         /* make sure our event loop detects the new device */
356         dummy = 0;
357         err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
358         if (err < sizeof(dummy)) {
359                 /* ignore error, if any */
360                 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open write failed!");
361         }
362         *devh = pdev;
363
364         return (0);
365 }
366
367 libusb_device_handle *
368 libusb_open_device_with_vid_pid(libusb_context *ctx, uint16_t vendor_id,
369     uint16_t product_id)
370 {
371         struct libusb_device **devs;
372         struct libusb20_device *pdev;
373         struct LIBUSB20_DEVICE_DESC_DECODED *pdesc;
374         int i;
375         int j;
376
377         ctx = GET_CONTEXT(ctx);
378         if (ctx == NULL)
379                 return (NULL);          /* be NULL safe */
380
381         DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid enter");
382
383         if ((i = libusb_get_device_list(ctx, &devs)) < 0)
384                 return (NULL);
385
386         for (j = 0; j < i; j++) {
387                 pdev = devs[j]->os_priv;
388                 pdesc = libusb20_dev_get_device_desc(pdev);
389                 /*
390                  * NOTE: The USB library will automatically swap the
391                  * fields in the device descriptor to be of host
392                  * endian type!
393                  */
394                 if (pdesc->idVendor == vendor_id &&
395                     pdesc->idProduct == product_id) {
396                         if (libusb_open(devs[j], &pdev) < 0)
397                                 pdev = NULL;
398                         break;
399                 }
400         }
401         if (j == i)
402                 pdev = NULL;
403
404         libusb_free_device_list(devs, 1);
405         DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid leave");
406         return (pdev);
407 }
408
409 void
410 libusb_close(struct libusb20_device *pdev)
411 {
412         libusb_context *ctx;
413         struct libusb_device *dev;
414         uint8_t dummy;
415         int err;
416
417         if (pdev == NULL)
418                 return;                 /* be NULL safe */
419
420         dev = libusb_get_device(pdev);
421         ctx = dev->ctx;
422
423         libusb10_remove_pollfd(ctx, &dev->dev_poll);
424
425         libusb20_dev_close(pdev);
426
427         /* unref will free the "pdev" when the refcount reaches zero */
428         libusb_unref_device(dev);
429
430         /* make sure our event loop detects the closed device */
431         dummy = 0;
432         err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
433         if (err < sizeof(dummy)) {
434                 /* ignore error, if any */
435                 DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_close write failed!");
436         }
437 }
438
439 libusb_device *
440 libusb_get_device(struct libusb20_device *pdev)
441 {
442         if (pdev == NULL)
443                 return (NULL);
444         return ((libusb_device *)pdev->privLuData);
445 }
446
447 int
448 libusb_get_configuration(struct libusb20_device *pdev, int *config)
449 {
450         struct libusb20_config *pconf;
451
452         if (pdev == NULL || config == NULL)
453                 return (LIBUSB_ERROR_INVALID_PARAM);
454
455         pconf = libusb20_dev_alloc_config(pdev, libusb20_dev_get_config_index(pdev));
456         if (pconf == NULL)
457                 return (LIBUSB_ERROR_NO_MEM);
458
459         *config = pconf->desc.bConfigurationValue;
460
461         free(pconf);
462
463         return (0);
464 }
465
466 int
467 libusb_set_configuration(struct libusb20_device *pdev, int configuration)
468 {
469         struct libusb20_config *pconf;
470         struct libusb_device *dev;
471         int err;
472         uint8_t i;
473
474         dev = libusb_get_device(pdev);
475
476         if (dev == NULL)
477                 return (LIBUSB_ERROR_INVALID_PARAM);
478
479         if (configuration < 1) {
480                 /* unconfigure */
481                 i = 255;
482         } else {
483                 for (i = 0; i != 255; i++) {
484                         uint8_t found;
485
486                         pconf = libusb20_dev_alloc_config(pdev, i);
487                         if (pconf == NULL)
488                                 return (LIBUSB_ERROR_INVALID_PARAM);
489                         found = (pconf->desc.bConfigurationValue
490                             == configuration);
491                         free(pconf);
492
493                         if (found)
494                                 goto set_config;
495                 }
496                 return (LIBUSB_ERROR_INVALID_PARAM);
497         }
498
499 set_config:
500
501         libusb10_cancel_all_transfer(dev);
502
503         libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
504
505         err = libusb20_dev_set_config_index(pdev, i);
506
507         libusb10_add_pollfd(dev->ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
508             POLLOUT | POLLRDNORM | POLLWRNORM);
509
510         return (err ? LIBUSB_ERROR_INVALID_PARAM : 0);
511 }
512
513 int
514 libusb_claim_interface(struct libusb20_device *pdev, int interface_number)
515 {
516         libusb_device *dev;
517         int err = 0;
518
519         dev = libusb_get_device(pdev);
520         if (dev == NULL)
521                 return (LIBUSB_ERROR_INVALID_PARAM);
522
523         if (interface_number < 0 || interface_number > 31)
524                 return (LIBUSB_ERROR_INVALID_PARAM);
525
526         CTX_LOCK(dev->ctx);
527         if (dev->claimed_interfaces & (1 << interface_number))
528                 err = LIBUSB_ERROR_BUSY;
529
530         if (!err)
531                 dev->claimed_interfaces |= (1 << interface_number);
532         CTX_UNLOCK(dev->ctx);
533         return (err);
534 }
535
536 int
537 libusb_release_interface(struct libusb20_device *pdev, int interface_number)
538 {
539         libusb_device *dev;
540         int err = 0;
541
542         dev = libusb_get_device(pdev);
543         if (dev == NULL)
544                 return (LIBUSB_ERROR_INVALID_PARAM);
545
546         if (interface_number < 0 || interface_number > 31)
547                 return (LIBUSB_ERROR_INVALID_PARAM);
548
549         CTX_LOCK(dev->ctx);
550         if (!(dev->claimed_interfaces & (1 << interface_number)))
551                 err = LIBUSB_ERROR_NOT_FOUND;
552
553         if (!err)
554                 dev->claimed_interfaces &= ~(1 << interface_number);
555         CTX_UNLOCK(dev->ctx);
556         return (err);
557 }
558
559 int
560 libusb_set_interface_alt_setting(struct libusb20_device *pdev,
561     int interface_number, int alternate_setting)
562 {
563         libusb_device *dev;
564         int err = 0;
565
566         dev = libusb_get_device(pdev);
567         if (dev == NULL)
568                 return (LIBUSB_ERROR_INVALID_PARAM);
569
570         if (interface_number < 0 || interface_number > 31)
571                 return (LIBUSB_ERROR_INVALID_PARAM);
572
573         CTX_LOCK(dev->ctx);
574         if (!(dev->claimed_interfaces & (1 << interface_number)))
575                 err = LIBUSB_ERROR_NOT_FOUND;
576         CTX_UNLOCK(dev->ctx);
577
578         if (err)
579                 return (err);
580
581         libusb10_cancel_all_transfer(dev);
582
583         libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
584
585         err = libusb20_dev_set_alt_index(pdev,
586             interface_number, alternate_setting);
587
588         libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
589             pdev, libusb20_dev_get_fd(pdev),
590             POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
591
592         return (err ? LIBUSB_ERROR_OTHER : 0);
593 }
594
595 static struct libusb20_transfer *
596 libusb10_get_transfer(struct libusb20_device *pdev,
597     uint8_t endpoint, uint8_t index)
598 {
599         index &= 1;                     /* double buffering */
600
601         index |= (endpoint & LIBUSB20_ENDPOINT_ADDRESS_MASK) * 4;
602
603         if (endpoint & LIBUSB20_ENDPOINT_DIR_MASK) {
604                 /* this is an IN endpoint */
605                 index |= 2;
606         }
607         return (libusb20_tr_get_pointer(pdev, index));
608 }
609
610 int
611 libusb_clear_halt(struct libusb20_device *pdev, uint8_t endpoint)
612 {
613         struct libusb20_transfer *xfer;
614         struct libusb_device *dev;
615         int err;
616
617         xfer = libusb10_get_transfer(pdev, endpoint, 0);
618         if (xfer == NULL)
619                 return (LIBUSB_ERROR_INVALID_PARAM);
620
621         dev = libusb_get_device(pdev);
622
623         CTX_LOCK(dev->ctx);
624         err = libusb20_tr_open(xfer, 0, 0, endpoint);
625         CTX_UNLOCK(dev->ctx);
626
627         if (err != 0 && err != LIBUSB20_ERROR_BUSY)
628                 return (LIBUSB_ERROR_OTHER);
629
630         libusb20_tr_clear_stall_sync(xfer);
631
632         /* check if we opened the transfer */
633         if (err == 0) {
634                 CTX_LOCK(dev->ctx);
635                 libusb20_tr_close(xfer);
636                 CTX_UNLOCK(dev->ctx);
637         }
638         return (0);                     /* success */
639 }
640
641 int
642 libusb_reset_device(struct libusb20_device *pdev)
643 {
644         libusb_device *dev;
645         int err;
646
647         dev = libusb_get_device(pdev);
648         if (dev == NULL)
649                 return (LIBUSB20_ERROR_INVALID_PARAM);
650
651         libusb10_cancel_all_transfer(dev);
652
653         libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
654
655         err = libusb20_dev_reset(pdev);
656
657         libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
658             pdev, libusb20_dev_get_fd(pdev),
659             POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
660
661         return (err ? LIBUSB_ERROR_OTHER : 0);
662 }
663
664 int
665 libusb_kernel_driver_active(struct libusb20_device *pdev, int interface)
666 {
667         if (pdev == NULL)
668                 return (LIBUSB_ERROR_INVALID_PARAM);
669
670         return (libusb20_dev_kernel_driver_active(
671             pdev, interface));
672 }
673
674 int
675 libusb_detach_kernel_driver(struct libusb20_device *pdev, int interface)
676 {
677         int err;
678
679         if (pdev == NULL)
680                 return (LIBUSB_ERROR_INVALID_PARAM);
681
682         err = libusb20_dev_detach_kernel_driver(
683             pdev, interface);
684
685         return (err ? LIBUSB20_ERROR_OTHER : 0);
686 }
687
688 int
689 libusb_attach_kernel_driver(struct libusb20_device *pdev, int interface)
690 {
691         if (pdev == NULL)
692                 return (LIBUSB_ERROR_INVALID_PARAM);
693         /* stub - currently not supported by libusb20 */
694         return (0);
695 }
696
697 /* Asynchronous device I/O */
698
699 struct libusb_transfer *
700 libusb_alloc_transfer(int iso_packets)
701 {
702         struct libusb_transfer *uxfer;
703         struct libusb_super_transfer *sxfer;
704         int len;
705
706         len = sizeof(struct libusb_transfer) +
707             sizeof(struct libusb_super_transfer) +
708             (iso_packets * sizeof(libusb_iso_packet_descriptor));
709
710         sxfer = malloc(len);
711         if (sxfer == NULL)
712                 return (NULL);
713
714         memset(sxfer, 0, len);
715
716         uxfer = (struct libusb_transfer *)(
717             ((uint8_t *)sxfer) + sizeof(*sxfer));
718
719         /* set default value */
720         uxfer->num_iso_packets = iso_packets;
721
722         return (uxfer);
723 }
724
725 void
726 libusb_free_transfer(struct libusb_transfer *uxfer)
727 {
728         struct libusb_super_transfer *sxfer;
729
730         if (uxfer == NULL)
731                 return;                 /* be NULL safe */
732
733         sxfer = (struct libusb_super_transfer *)(
734             (uint8_t *)uxfer - sizeof(*sxfer));
735
736         free(sxfer);
737 }
738
739 static int
740 libusb10_get_maxframe(struct libusb20_device *pdev, libusb_transfer *xfer)
741 {
742         int ret;
743         int usb_speed;
744
745         usb_speed = libusb20_dev_get_speed(pdev);
746
747         switch (xfer->type) {
748         case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
749                 switch (usb_speed) {
750                 case LIBUSB20_SPEED_LOW:
751                 case LIBUSB20_SPEED_FULL:
752                         ret = 60 * 1;
753                         break;
754                 default:
755                         ret = 60 * 8;
756                         break;
757                 }
758                 break;
759         case LIBUSB_TRANSFER_TYPE_CONTROL:
760                 ret = 2;
761                 break;
762         default:
763                 ret = 1;
764                 break;
765         }
766         return (ret);
767 }
768
769 static int
770 libusb10_get_buffsize(struct libusb20_device *pdev, libusb_transfer *xfer)
771 {
772         int ret;
773         int usb_speed;
774
775         usb_speed = libusb20_dev_get_speed(pdev);
776
777         switch (xfer->type) {
778         case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
779                 ret = 0;                /* kernel will auto-select */
780                 break;
781         case LIBUSB_TRANSFER_TYPE_CONTROL:
782                 ret = 1024;
783                 break;
784         default:
785                 switch (usb_speed) {
786                 case LIBUSB20_SPEED_LOW:
787                         ret = 256;
788                         break;
789                 case LIBUSB20_SPEED_FULL:
790                         ret = 4096;
791                         break;
792                 default:
793                         ret = 16384;
794                         break;
795                 }
796                 break;
797         }
798         return (ret);
799 }
800
801 static int
802 libusb10_convert_error(uint8_t status)
803 {
804         ;                               /* indent fix */
805
806         switch (status) {
807         case LIBUSB20_TRANSFER_START:
808         case LIBUSB20_TRANSFER_COMPLETED:
809                 return (LIBUSB_TRANSFER_COMPLETED);
810         case LIBUSB20_TRANSFER_OVERFLOW:
811                 return (LIBUSB_TRANSFER_OVERFLOW);
812         case LIBUSB20_TRANSFER_NO_DEVICE:
813                 return (LIBUSB_TRANSFER_NO_DEVICE);
814         case LIBUSB20_TRANSFER_STALL:
815                 return (LIBUSB_TRANSFER_STALL);
816         case LIBUSB20_TRANSFER_CANCELLED:
817                 return (LIBUSB_TRANSFER_CANCELLED);
818         case LIBUSB20_TRANSFER_TIMED_OUT:
819                 return (LIBUSB_TRANSFER_TIMED_OUT);
820         default:
821                 return (LIBUSB_TRANSFER_ERROR);
822         }
823 }
824
825 /* This function must be called locked */
826
827 static void
828 libusb10_complete_transfer(struct libusb20_transfer *pxfer,
829     struct libusb_super_transfer *sxfer, int status)
830 {
831         struct libusb_transfer *uxfer;
832         struct libusb_device *dev;
833
834         uxfer = (struct libusb_transfer *)(
835             ((uint8_t *)sxfer) + sizeof(*sxfer));
836
837         if (pxfer != NULL)
838                 libusb20_tr_set_priv_sc1(pxfer, NULL);
839
840         /* set transfer status */
841         uxfer->status = status;
842
843         /* update super transfer state */
844         sxfer->state = LIBUSB_SUPER_XFER_ST_NONE;
845
846         dev = libusb_get_device(uxfer->dev_handle);
847
848         TAILQ_INSERT_TAIL(&dev->ctx->tr_done, sxfer, entry);
849 }
850
851 /* This function must be called locked */
852
853 static void
854 libusb10_isoc_proxy(struct libusb20_transfer *pxfer)
855 {
856         struct libusb_super_transfer *sxfer;
857         struct libusb_transfer *uxfer;
858         uint32_t actlen;
859         uint16_t iso_packets;
860         uint16_t i;
861         uint8_t status;
862         uint8_t flags;
863
864         status = libusb20_tr_get_status(pxfer);
865         sxfer = libusb20_tr_get_priv_sc1(pxfer);
866         actlen = libusb20_tr_get_actual_length(pxfer);
867         iso_packets = libusb20_tr_get_max_frames(pxfer);
868
869         if (sxfer == NULL)
870                 return;                 /* cancelled - nothing to do */
871
872         uxfer = (struct libusb_transfer *)(
873             ((uint8_t *)sxfer) + sizeof(*sxfer));
874
875         if (iso_packets > uxfer->num_iso_packets)
876                 iso_packets = uxfer->num_iso_packets;
877
878         if (iso_packets == 0)
879                 return;                 /* nothing to do */
880
881         /* make sure that the number of ISOCHRONOUS packets is valid */
882         uxfer->num_iso_packets = iso_packets;
883
884         flags = uxfer->flags;
885
886         switch (status) {
887         case LIBUSB20_TRANSFER_COMPLETED:
888
889                 /* update actual length */
890                 uxfer->actual_length = actlen;
891                 for (i = 0; i != iso_packets; i++) {
892                         uxfer->iso_packet_desc[i].actual_length =
893                             libusb20_tr_get_length(pxfer, i);
894                 }
895                 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
896                 break;
897
898         case LIBUSB20_TRANSFER_START:
899
900                 /* setup length(s) */
901                 actlen = 0;
902                 for (i = 0; i != iso_packets; i++) {
903                         libusb20_tr_setup_isoc(pxfer,
904                             &uxfer->buffer[actlen],
905                             uxfer->iso_packet_desc[i].length, i);
906                         actlen += uxfer->iso_packet_desc[i].length;
907                 }
908
909                 /* no remainder */
910                 sxfer->rem_len = 0;
911
912                 libusb20_tr_set_total_frames(pxfer, iso_packets);
913                 libusb20_tr_submit(pxfer);
914
915                 /* fork another USB transfer, if any */
916                 libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
917                 break;
918
919         default:
920                 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
921                 break;
922         }
923 }
924
925 /* This function must be called locked */
926
927 static void
928 libusb10_bulk_intr_proxy(struct libusb20_transfer *pxfer)
929 {
930         struct libusb_super_transfer *sxfer;
931         struct libusb_transfer *uxfer;
932         uint32_t max_bulk;
933         uint32_t actlen;
934         uint8_t status;
935         uint8_t flags;
936
937         status = libusb20_tr_get_status(pxfer);
938         sxfer = libusb20_tr_get_priv_sc1(pxfer);
939         max_bulk = libusb20_tr_get_max_total_length(pxfer);
940         actlen = libusb20_tr_get_actual_length(pxfer);
941
942         if (sxfer == NULL)
943                 return;                 /* cancelled - nothing to do */
944
945         uxfer = (struct libusb_transfer *)(
946             ((uint8_t *)sxfer) + sizeof(*sxfer));
947
948         flags = uxfer->flags;
949
950         switch (status) {
951         case LIBUSB20_TRANSFER_COMPLETED:
952
953                 uxfer->actual_length += actlen;
954
955                 /* check for short packet */
956                 if (sxfer->last_len != actlen) {
957                         if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
958                                 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
959                         } else {
960                                 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
961                         }
962                         break;
963                 }
964                 /* check for end of data */
965                 if (sxfer->rem_len == 0) {
966                         libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
967                         break;
968                 }
969                 /* FALLTHROUGH */
970
971         case LIBUSB20_TRANSFER_START:
972                 if (max_bulk > sxfer->rem_len) {
973                         max_bulk = sxfer->rem_len;
974                 }
975                 /* setup new BULK or INTERRUPT transaction */
976                 libusb20_tr_setup_bulk(pxfer,
977                     sxfer->curr_data, max_bulk, uxfer->timeout);
978
979                 /* update counters */
980                 sxfer->last_len = max_bulk;
981                 sxfer->curr_data += max_bulk;
982                 sxfer->rem_len -= max_bulk;
983
984                 libusb20_tr_submit(pxfer);
985
986                 /* check if we can fork another USB transfer */
987                 if (sxfer->rem_len == 0)
988                         libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
989                 break;
990
991         default:
992                 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
993                 break;
994         }
995 }
996
997 /* This function must be called locked */
998
999 static void
1000 libusb10_ctrl_proxy(struct libusb20_transfer *pxfer)
1001 {
1002         struct libusb_super_transfer *sxfer;
1003         struct libusb_transfer *uxfer;
1004         uint32_t max_bulk;
1005         uint32_t actlen;
1006         uint8_t status;
1007         uint8_t flags;
1008
1009         status = libusb20_tr_get_status(pxfer);
1010         sxfer = libusb20_tr_get_priv_sc1(pxfer);
1011         max_bulk = libusb20_tr_get_max_total_length(pxfer);
1012         actlen = libusb20_tr_get_actual_length(pxfer);
1013
1014         if (sxfer == NULL)
1015                 return;                 /* cancelled - nothing to do */
1016
1017         uxfer = (struct libusb_transfer *)(
1018             ((uint8_t *)sxfer) + sizeof(*sxfer));
1019
1020         flags = uxfer->flags;
1021
1022         switch (status) {
1023         case LIBUSB20_TRANSFER_COMPLETED:
1024
1025                 uxfer->actual_length += actlen;
1026
1027                 /* subtract length of SETUP packet, if any */
1028                 actlen -= libusb20_tr_get_length(pxfer, 0);
1029
1030                 /* check for short packet */
1031                 if (sxfer->last_len != actlen) {
1032                         if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1033                                 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1034                         } else {
1035                                 libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1036                         }
1037                         break;
1038                 }
1039                 /* check for end of data */
1040                 if (sxfer->rem_len == 0) {
1041                         libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1042                         break;
1043                 }
1044                 /* FALLTHROUGH */
1045
1046         case LIBUSB20_TRANSFER_START:
1047                 if (max_bulk > sxfer->rem_len) {
1048                         max_bulk = sxfer->rem_len;
1049                 }
1050                 /* setup new CONTROL transaction */
1051                 if (status == LIBUSB20_TRANSFER_COMPLETED) {
1052                         /* next fragment - don't send SETUP packet */
1053                         libusb20_tr_set_length(pxfer, 0, 0);
1054                 } else {
1055                         /* first fragment - send SETUP packet */
1056                         libusb20_tr_set_length(pxfer, 8, 0);
1057                         libusb20_tr_set_buffer(pxfer, uxfer->buffer, 0);
1058                 }
1059
1060                 if (max_bulk != 0) {
1061                         libusb20_tr_set_length(pxfer, max_bulk, 1);
1062                         libusb20_tr_set_buffer(pxfer, sxfer->curr_data, 1);
1063                         libusb20_tr_set_total_frames(pxfer, 2);
1064                 } else {
1065                         libusb20_tr_set_total_frames(pxfer, 1);
1066                 }
1067
1068                 /* update counters */
1069                 sxfer->last_len = max_bulk;
1070                 sxfer->curr_data += max_bulk;
1071                 sxfer->rem_len -= max_bulk;
1072
1073                 libusb20_tr_submit(pxfer);
1074
1075                 /* check if we can fork another USB transfer */
1076                 if (sxfer->rem_len == 0)
1077                         libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1078                 break;
1079
1080         default:
1081                 libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1082                 break;
1083         }
1084 }
1085
1086 /* The following function must be called locked */
1087
1088 static void
1089 libusb10_submit_transfer_sub(struct libusb20_device *pdev, uint8_t endpoint)
1090 {
1091         struct libusb20_transfer *pxfer0;
1092         struct libusb20_transfer *pxfer1;
1093         struct libusb_super_transfer *sxfer;
1094         struct libusb_transfer *uxfer;
1095         struct libusb_device *dev;
1096         int err;
1097         int buffsize;
1098         int maxframe;
1099         int temp;
1100         uint8_t dummy;
1101
1102         dev = libusb_get_device(pdev);
1103
1104         pxfer0 = libusb10_get_transfer(pdev, endpoint, 0);
1105         pxfer1 = libusb10_get_transfer(pdev, endpoint, 1);
1106
1107         if (pxfer0 == NULL || pxfer1 == NULL)
1108                 return;                 /* shouldn't happen */
1109
1110         temp = 0;
1111         if (libusb20_tr_pending(pxfer0))
1112                 temp |= 1;
1113         if (libusb20_tr_pending(pxfer1))
1114                 temp |= 2;
1115
1116         switch (temp) {
1117         case 3:
1118                 /* wait till one of the transfers complete */
1119                 return;
1120         case 2:
1121                 sxfer = libusb20_tr_get_priv_sc1(pxfer1);
1122                 if (sxfer == NULL)
1123                         return;         /* cancelling */
1124                 if (sxfer->rem_len)
1125                         return;         /* cannot queue another one */
1126                 /* swap transfers */
1127                 pxfer1 = pxfer0;
1128                 break;
1129         case 1:
1130                 sxfer = libusb20_tr_get_priv_sc1(pxfer0);
1131                 if (sxfer == NULL)
1132                         return;         /* cancelling */
1133                 if (sxfer->rem_len)
1134                         return;         /* cannot queue another one */
1135                 /* swap transfers */
1136                 pxfer0 = pxfer1;
1137                 break;
1138         default:
1139                 break;
1140         }
1141
1142         /* find next transfer on same endpoint */
1143         TAILQ_FOREACH(sxfer, &dev->tr_head, entry) {
1144
1145                 uxfer = (struct libusb_transfer *)(
1146                     ((uint8_t *)sxfer) + sizeof(*sxfer));
1147
1148                 if (uxfer->endpoint == endpoint) {
1149                         TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1150                         sxfer->entry.tqe_prev = NULL;
1151                         goto found;
1152                 }
1153         }
1154         return;                         /* success */
1155
1156 found:
1157
1158         libusb20_tr_set_priv_sc0(pxfer0, pdev);
1159         libusb20_tr_set_priv_sc1(pxfer0, sxfer);
1160
1161         /* reset super transfer state */
1162         sxfer->rem_len = uxfer->length;
1163         sxfer->curr_data = uxfer->buffer;
1164         uxfer->actual_length = 0;
1165
1166         switch (uxfer->type) {
1167         case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
1168                 libusb20_tr_set_callback(pxfer0, libusb10_isoc_proxy);
1169                 break;
1170         case LIBUSB_TRANSFER_TYPE_BULK:
1171         case LIBUSB_TRANSFER_TYPE_INTERRUPT:
1172                 libusb20_tr_set_callback(pxfer0, libusb10_bulk_intr_proxy);
1173                 break;
1174         case LIBUSB_TRANSFER_TYPE_CONTROL:
1175                 libusb20_tr_set_callback(pxfer0, libusb10_ctrl_proxy);
1176                 if (sxfer->rem_len < 8)
1177                         goto failure;
1178
1179                 /* remove SETUP packet from data */
1180                 sxfer->rem_len -= 8;
1181                 sxfer->curr_data += 8;
1182                 break;
1183         default:
1184                 goto failure;
1185         }
1186
1187         buffsize = libusb10_get_buffsize(pdev, uxfer);
1188         maxframe = libusb10_get_maxframe(pdev, uxfer);
1189
1190         /* make sure the transfer is opened */
1191         err = libusb20_tr_open(pxfer0, buffsize, maxframe, endpoint);
1192         if (err && (err != LIBUSB20_ERROR_BUSY)) {
1193                 goto failure;
1194         }
1195         libusb20_tr_start(pxfer0);
1196         return;
1197
1198 failure:
1199         libusb10_complete_transfer(pxfer0, sxfer, LIBUSB_TRANSFER_ERROR);
1200
1201         /* make sure our event loop spins the done handler */
1202         dummy = 0;
1203         write(dev->ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
1204 }
1205
1206 /* The following function must be called unlocked */
1207
1208 int
1209 libusb_submit_transfer(struct libusb_transfer *uxfer)
1210 {
1211         struct libusb20_transfer *pxfer0;
1212         struct libusb20_transfer *pxfer1;
1213         struct libusb_super_transfer *sxfer;
1214         struct libusb_device *dev;
1215         uint32_t endpoint;
1216         int err;
1217
1218         if (uxfer == NULL)
1219                 return (LIBUSB_ERROR_INVALID_PARAM);
1220
1221         if (uxfer->dev_handle == NULL)
1222                 return (LIBUSB_ERROR_INVALID_PARAM);
1223
1224         endpoint = uxfer->endpoint;
1225
1226         if (endpoint > 255)
1227                 return (LIBUSB_ERROR_INVALID_PARAM);
1228
1229         dev = libusb_get_device(uxfer->dev_handle);
1230
1231         DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer enter");
1232
1233         sxfer = (struct libusb_super_transfer *)(
1234             (uint8_t *)uxfer - sizeof(*sxfer));
1235
1236         CTX_LOCK(dev->ctx);
1237
1238         pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1239         pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1240
1241         if (pxfer0 == NULL || pxfer1 == NULL) {
1242                 err = LIBUSB_ERROR_OTHER;
1243         } else if ((sxfer->entry.tqe_prev != NULL) ||
1244             (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) ||
1245             (libusb20_tr_get_priv_sc1(pxfer1) == sxfer)) {
1246                 err = LIBUSB_ERROR_BUSY;
1247         } else {
1248
1249                 /* set pending state */
1250                 sxfer->state = LIBUSB_SUPER_XFER_ST_PEND;
1251
1252                 /* insert transfer into transfer head list */
1253                 TAILQ_INSERT_TAIL(&dev->tr_head, sxfer, entry);
1254
1255                 /* start work transfers */
1256                 libusb10_submit_transfer_sub(
1257                     uxfer->dev_handle, endpoint);
1258
1259                 err = 0;                /* success */
1260         }
1261
1262         CTX_UNLOCK(dev->ctx);
1263
1264         DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer leave %d", err);
1265
1266         return (err);
1267 }
1268
1269 /* Asynchronous transfer cancel */
1270
1271 int
1272 libusb_cancel_transfer(struct libusb_transfer *uxfer)
1273 {
1274         struct libusb20_transfer *pxfer0;
1275         struct libusb20_transfer *pxfer1;
1276         struct libusb_super_transfer *sxfer;
1277         struct libusb_device *dev;
1278         uint32_t endpoint;
1279         int retval;
1280
1281         if (uxfer == NULL)
1282                 return (LIBUSB_ERROR_INVALID_PARAM);
1283
1284         /* check if not initialised */
1285         if (uxfer->dev_handle == NULL)
1286                 return (LIBUSB_ERROR_NOT_FOUND);
1287
1288         endpoint = uxfer->endpoint;
1289
1290         if (endpoint > 255)
1291                 return (LIBUSB_ERROR_INVALID_PARAM);
1292
1293         dev = libusb_get_device(uxfer->dev_handle);
1294
1295         DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer enter");
1296
1297         sxfer = (struct libusb_super_transfer *)(
1298             (uint8_t *)uxfer - sizeof(*sxfer));
1299
1300         retval = 0;
1301
1302         CTX_LOCK(dev->ctx);
1303
1304         pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1305         pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1306
1307         if (sxfer->state != LIBUSB_SUPER_XFER_ST_PEND) {
1308                 /* only update the transfer status */
1309                 uxfer->status = LIBUSB_TRANSFER_CANCELLED;
1310                 retval = LIBUSB_ERROR_NOT_FOUND;
1311         } else if (sxfer->entry.tqe_prev != NULL) {
1312                 /* we are lucky - transfer is on a queue */
1313                 TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1314                 sxfer->entry.tqe_prev = NULL;
1315                 libusb10_complete_transfer(NULL,
1316                     sxfer, LIBUSB_TRANSFER_CANCELLED);
1317         } else if (pxfer0 == NULL || pxfer1 == NULL) {
1318                 /* not started */
1319                 retval = LIBUSB_ERROR_NOT_FOUND;
1320         } else if (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) {
1321                 libusb10_complete_transfer(pxfer0,
1322                     sxfer, LIBUSB_TRANSFER_CANCELLED);
1323                 libusb20_tr_stop(pxfer0);
1324                 /* make sure the queue doesn't stall */
1325                 libusb10_submit_transfer_sub(
1326                     uxfer->dev_handle, endpoint);
1327         } else if (libusb20_tr_get_priv_sc1(pxfer1) == sxfer) {
1328                 libusb10_complete_transfer(pxfer1,
1329                     sxfer, LIBUSB_TRANSFER_CANCELLED);
1330                 libusb20_tr_stop(pxfer1);
1331                 /* make sure the queue doesn't stall */
1332                 libusb10_submit_transfer_sub(
1333                     uxfer->dev_handle, endpoint);
1334         } else {
1335                 /* not started */
1336                 retval = LIBUSB_ERROR_NOT_FOUND;
1337         }
1338
1339         CTX_UNLOCK(dev->ctx);
1340
1341         DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer leave");
1342
1343         return (retval);
1344 }
1345
1346 UNEXPORTED void
1347 libusb10_cancel_all_transfer(libusb_device *dev)
1348 {
1349         /* TODO */
1350 }
1351
1352 uint16_t
1353 libusb_cpu_to_le16(uint16_t x)
1354 {
1355         return (htole16(x));
1356 }
1357
1358 uint16_t
1359 libusb_le16_to_cpu(uint16_t x)
1360 {
1361         return (le16toh(x));
1362 }
1363