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