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