gdb - Local mods (compile)
[dragonfly.git] / lib / libusb / libusb01.c
1 /* $FreeBSD: head/lib/libusb/libusb01.c 264344 2014-04-11 14:11:55Z hselasky $ */
2 /*-
3  * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 /*
28  * This file contains the emulation layer for LibUSB v0.1 from sourceforge.
29  */
30
31 #ifdef LIBUSB_GLOBAL_INCLUDE_FILE
32 #include LIBUSB_GLOBAL_INCLUDE_FILE
33 #else
34 #include <errno.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <time.h>
39 #include <sys/queue.h>
40 #endif
41
42 #include "libusb20.h"
43 #include "libusb20_desc.h"
44 #include "libusb20_int.h"
45 #include "usb.h"
46
47 /*
48  * The two following macros were taken from the original LibUSB v0.1
49  * for sake of compatibility:
50  */
51 #define LIST_ADD(begin, ent)       \
52   do {                             \
53     if (begin) {                   \
54       ent->next = begin;           \
55       ent->next->prev = ent;       \
56     } else {                       \
57       ent->next = NULL;            \
58     }                              \
59     ent->prev = NULL;              \
60     begin = ent;                   \
61   } while(0)
62
63 #define LIST_DEL(begin, ent)             \
64   do {                                   \
65     if (ent->prev) {                     \
66       ent->prev->next = ent->next;       \
67     } else {                             \
68       begin = ent->next;                 \
69     }                                    \
70     if (ent->next) {                     \
71       ent->next->prev = ent->prev;       \
72     }                                    \
73     ent->prev = NULL;                    \
74     ent->next = NULL;                    \
75   } while (0)
76
77 struct usb_bus *usb_busses = NULL;
78
79 static struct usb_bus usb_global_bus = {
80         .dirname = {"/dev/usb"},
81         .root_dev = NULL,
82         .devices = NULL,
83 };
84
85 static struct libusb20_backend *usb_backend = NULL;
86
87 struct usb_parse_state {
88
89         struct {
90                 struct libusb20_endpoint *currep;
91                 struct libusb20_interface *currifc;
92                 struct libusb20_config *currcfg;
93                 struct libusb20_me_struct *currextra;
94         }       a;
95
96         struct {
97                 struct usb_config_descriptor *currcfg;
98                 struct usb_interface_descriptor *currifc;
99                 struct usb_endpoint_descriptor *currep;
100                 struct usb_interface *currifcw;
101                 uint8_t *currextra;
102         }       b;
103
104         uint8_t preparse;
105 };
106
107 static struct libusb20_transfer *
108 usb_get_transfer_by_ep_no(usb_dev_handle * dev, uint8_t ep_no)
109 {
110         struct libusb20_device *pdev = (void *)dev;
111         struct libusb20_transfer *xfer;
112         int err;
113         uint32_t bufsize;
114         uint8_t x;
115         uint8_t speed;
116
117         x = (ep_no & LIBUSB20_ENDPOINT_ADDRESS_MASK) * 2;
118
119         if (ep_no & LIBUSB20_ENDPOINT_DIR_MASK) {
120                 /* this is an IN endpoint */
121                 x |= 1;
122         }
123         speed = libusb20_dev_get_speed(pdev);
124
125         /* select a sensible buffer size */
126         if (speed == LIBUSB20_SPEED_LOW) {
127                 bufsize = 256;
128         } else if (speed == LIBUSB20_SPEED_FULL) {
129                 bufsize = 4096;
130         } else if (speed == LIBUSB20_SPEED_SUPER) {
131                 bufsize = 65536;
132         } else {
133                 bufsize = 16384;
134         }
135
136         xfer = libusb20_tr_get_pointer(pdev, x);
137
138         if (xfer == NULL)
139                 return (xfer);
140
141         err = libusb20_tr_open(xfer, bufsize, 1, ep_no);
142         if (err == LIBUSB20_ERROR_BUSY) {
143                 /* already opened */
144                 return (xfer);
145         } else if (err) {
146                 return (NULL);
147         }
148         /* success */
149         return (xfer);
150 }
151
152 usb_dev_handle *
153 usb_open(struct usb_device *dev)
154 {
155         int err;
156
157         err = libusb20_dev_open(dev->dev, 16 * 2);
158         if (err == LIBUSB20_ERROR_BUSY) {
159                 /*
160                  * Workaround buggy USB applications which open the USB
161                  * device multiple times:
162                  */
163                 return (dev->dev);
164         }
165         if (err)
166                 return (NULL);
167
168         /*
169          * Dequeue USB device from backend queue so that it does not get
170          * freed when the backend is re-scanned:
171          */
172         libusb20_be_dequeue_device(usb_backend, dev->dev);
173
174         return (dev->dev);
175 }
176
177 int
178 usb_close(usb_dev_handle * udev)
179 {
180         struct usb_device *dev;
181         int err;
182
183         err = libusb20_dev_close((void *)udev);
184
185         if (err)
186                 return (-1);
187
188         if (usb_backend != NULL) {
189                 /*
190                  * Enqueue USB device to backend queue so that it gets freed
191                  * when the backend is re-scanned:
192                  */
193                 libusb20_be_enqueue_device(usb_backend, (void *)udev);
194         } else {
195                 /*
196                  * The backend is gone. Free device data so that we
197                  * don't start leaking memory!
198                  */
199                 dev = usb_device(udev);
200                 libusb20_dev_free((void *)udev);
201                 LIST_DEL(usb_global_bus.devices, dev);
202                 free(dev);
203         }
204         return (0);
205 }
206
207 int
208 usb_get_string(usb_dev_handle * dev, int strindex,
209     int langid, char *buf, size_t buflen)
210 {
211         int err;
212
213         if (dev == NULL)
214                 return (-1);
215
216         if (buflen > 65535)
217                 buflen = 65535;
218
219         err = libusb20_dev_req_string_sync((void *)dev,
220             strindex, langid, buf, buflen);
221
222         if (err)
223                 return (-1);
224
225         return (0);
226 }
227
228 int
229 usb_get_string_simple(usb_dev_handle * dev, int strindex,
230     char *buf, size_t buflen)
231 {
232         int err;
233
234         if (dev == NULL)
235                 return (-1);
236
237         if (buflen > 65535)
238                 buflen = 65535;
239
240         err = libusb20_dev_req_string_simple_sync((void *)dev,
241             strindex, buf, buflen);
242
243         if (err)
244                 return (-1);
245
246         return (strlen(buf));
247 }
248
249 int
250 usb_get_descriptor_by_endpoint(usb_dev_handle * udev, int ep, uint8_t type,
251     uint8_t ep_index, void *buf, int size)
252 {
253         memset(buf, 0, size);
254
255         if (udev == NULL)
256                 return (-1);
257
258         if (size > 65535)
259                 size = 65535;
260
261         return (usb_control_msg(udev, ep | USB_ENDPOINT_IN,
262             USB_REQ_GET_DESCRIPTOR, (type << 8) + ep_index, 0,
263             buf, size, 1000));
264 }
265
266 int
267 usb_get_descriptor(usb_dev_handle * udev, uint8_t type, uint8_t desc_index,
268     void *buf, int size)
269 {
270         memset(buf, 0, size);
271
272         if (udev == NULL)
273                 return (-1);
274
275         if (size > 65535)
276                 size = 65535;
277
278         return (usb_control_msg(udev, USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR,
279             (type << 8) + desc_index, 0, buf, size, 1000));
280 }
281
282 int
283 usb_parse_descriptor(uint8_t *source, char *description, void *dest)
284 {
285         uint8_t *sp = source;
286         uint8_t *dp = dest;
287         uint16_t w;
288         uint32_t d;
289         char *cp;
290
291         for (cp = description; *cp; cp++) {
292                 switch (*cp) {
293                 case 'b':               /* 8-bit byte */
294                         *dp++ = *sp++;
295                         break;
296                         /*
297                          * 16-bit word, convert from little endian to CPU
298                          */
299                 case 'w':
300                         w = (sp[1] << 8) | sp[0];
301                         sp += 2;
302                         /* Align to word boundary */
303                         dp += ((dp - (uint8_t *)0) & 1);
304                         *((uint16_t *)dp) = w;
305                         dp += 2;
306                         break;
307                         /*
308                          * 32-bit dword, convert from little endian to CPU
309                          */
310                 case 'd':
311                         d = (sp[3] << 24) | (sp[2] << 16) |
312                             (sp[1] << 8) | sp[0];
313                         sp += 4;
314                         /* Align to word boundary */
315                         dp += ((dp - (uint8_t *)0) & 1);
316                         /* Align to double word boundary */
317                         dp += ((dp - (uint8_t *)0) & 2);
318                         *((uint32_t *)dp) = d;
319                         dp += 4;
320                         break;
321                 }
322         }
323         return (sp - source);
324 }
325
326 static void
327 usb_parse_extra(struct usb_parse_state *ps, uint8_t **pptr, int *plen)
328 {
329         void *ptr;
330         uint16_t len;
331
332         ptr = ps->a.currextra->ptr;
333         len = ps->a.currextra->len;
334
335         if (ps->preparse == 0) {
336                 memcpy(ps->b.currextra, ptr, len);
337                 *pptr = ps->b.currextra;
338                 *plen = len;
339         }
340         ps->b.currextra += len;
341         return;
342 }
343
344 static void
345 usb_parse_endpoint(struct usb_parse_state *ps)
346 {
347         struct usb_endpoint_descriptor *bep;
348         struct libusb20_endpoint *aep;
349
350         aep = ps->a.currep;
351         bep = ps->b.currep++;
352
353         if (ps->preparse == 0) {
354                 /* copy descriptor fields */
355                 bep->bLength = aep->desc.bLength;
356                 bep->bDescriptorType = aep->desc.bDescriptorType;
357                 bep->bEndpointAddress = aep->desc.bEndpointAddress;
358                 bep->bmAttributes = aep->desc.bmAttributes;
359                 bep->wMaxPacketSize = aep->desc.wMaxPacketSize;
360                 bep->bInterval = aep->desc.bInterval;
361                 bep->bRefresh = aep->desc.bRefresh;
362                 bep->bSynchAddress = aep->desc.bSynchAddress;
363         }
364         ps->a.currextra = &aep->extra;
365         usb_parse_extra(ps, &bep->extra, &bep->extralen);
366         return;
367 }
368
369 static void
370 usb_parse_iface_sub(struct usb_parse_state *ps)
371 {
372         struct libusb20_interface *aifc;
373         struct usb_interface_descriptor *bifc;
374         uint8_t x;
375
376         aifc = ps->a.currifc;
377         bifc = ps->b.currifc++;
378
379         if (ps->preparse == 0) {
380                 /* copy descriptor fields */
381                 bifc->bLength = aifc->desc.bLength;
382                 bifc->bDescriptorType = aifc->desc.bDescriptorType;
383                 bifc->bInterfaceNumber = aifc->desc.bInterfaceNumber;
384                 bifc->bAlternateSetting = aifc->desc.bAlternateSetting;
385                 bifc->bNumEndpoints = aifc->num_endpoints;
386                 bifc->bInterfaceClass = aifc->desc.bInterfaceClass;
387                 bifc->bInterfaceSubClass = aifc->desc.bInterfaceSubClass;
388                 bifc->bInterfaceProtocol = aifc->desc.bInterfaceProtocol;
389                 bifc->iInterface = aifc->desc.iInterface;
390                 bifc->endpoint = ps->b.currep;
391         }
392         for (x = 0; x != aifc->num_endpoints; x++) {
393                 ps->a.currep = aifc->endpoints + x;
394                 usb_parse_endpoint(ps);
395         }
396
397         ps->a.currextra = &aifc->extra;
398         usb_parse_extra(ps, &bifc->extra, &bifc->extralen);
399         return;
400 }
401
402 static void
403 usb_parse_iface(struct usb_parse_state *ps)
404 {
405         struct libusb20_interface *aifc;
406         struct usb_interface *bifc;
407         uint8_t x;
408
409         aifc = ps->a.currifc;
410         bifc = ps->b.currifcw++;
411
412         if (ps->preparse == 0) {
413                 /* initialise interface wrapper */
414                 bifc->altsetting = ps->b.currifc;
415                 bifc->num_altsetting = aifc->num_altsetting + 1;
416         }
417         usb_parse_iface_sub(ps);
418
419         for (x = 0; x != aifc->num_altsetting; x++) {
420                 ps->a.currifc = aifc->altsetting + x;
421                 usb_parse_iface_sub(ps);
422         }
423         return;
424 }
425
426 static void
427 usb_parse_config(struct usb_parse_state *ps)
428 {
429         struct libusb20_config *acfg;
430         struct usb_config_descriptor *bcfg;
431         uint8_t x;
432
433         acfg = ps->a.currcfg;
434         bcfg = ps->b.currcfg;
435
436         if (ps->preparse == 0) {
437                 /* initialise config wrapper */
438                 bcfg->bLength = acfg->desc.bLength;
439                 bcfg->bDescriptorType = acfg->desc.bDescriptorType;
440                 bcfg->wTotalLength = acfg->desc.wTotalLength;
441                 bcfg->bNumInterfaces = acfg->num_interface;
442                 bcfg->bConfigurationValue = acfg->desc.bConfigurationValue;
443                 bcfg->iConfiguration = acfg->desc.iConfiguration;
444                 bcfg->bmAttributes = acfg->desc.bmAttributes;
445                 bcfg->MaxPower = acfg->desc.bMaxPower;
446                 bcfg->interface = ps->b.currifcw;
447         }
448         for (x = 0; x != acfg->num_interface; x++) {
449                 ps->a.currifc = acfg->interface + x;
450                 usb_parse_iface(ps);
451         }
452
453         ps->a.currextra = &acfg->extra;
454         usb_parse_extra(ps, &bcfg->extra, &bcfg->extralen);
455         return;
456 }
457
458 int
459 usb_parse_configuration(struct usb_config_descriptor *config,
460     uint8_t *buffer)
461 {
462         struct usb_parse_state ps;
463         uint8_t *ptr;
464         uint32_t a;
465         uint32_t b;
466         uint32_t c;
467         uint32_t d;
468
469         if ((buffer == NULL) || (config == NULL)) {
470                 return (-1);
471         }
472         memset(&ps, 0, sizeof(ps));
473
474         ps.a.currcfg = libusb20_parse_config_desc(buffer);
475         ps.b.currcfg = config;
476         if (ps.a.currcfg == NULL) {
477                 /* could not parse config or out of memory */
478                 return (-1);
479         }
480         /* do the pre-parse */
481         ps.preparse = 1;
482         usb_parse_config(&ps);
483
484         a = ((uint8_t *)(ps.b.currifcw) - ((uint8_t *)0));
485         b = ((uint8_t *)(ps.b.currifc) - ((uint8_t *)0));
486         c = ((uint8_t *)(ps.b.currep) - ((uint8_t *)0));
487         d = ((uint8_t *)(ps.b.currextra) - ((uint8_t *)0));
488
489         /* allocate memory for our configuration */
490         ptr = malloc(a + b + c + d);
491         if (ptr == NULL) {
492                 /* free config structure */
493                 free(ps.a.currcfg);
494                 return (-1);
495         }
496
497         /* "currifcw" must be first, hence this pointer is freed */
498         ps.b.currifcw = (void *)(ptr);
499         ps.b.currifc = (void *)(ptr + a);
500         ps.b.currep = (void *)(ptr + a + b);
501         ps.b.currextra = (void *)(ptr + a + b + c);
502
503         /* generate a libusb v0.1 compatible structure */
504         ps.preparse = 0;
505         usb_parse_config(&ps);
506
507         /* free config structure */
508         free(ps.a.currcfg);
509
510         return (0);                     /* success */
511 }
512
513 void
514 usb_destroy_configuration(struct usb_device *dev)
515 {
516         uint8_t c;
517
518         if (dev->config == NULL) {
519                 return;
520         }
521         for (c = 0; c != dev->descriptor.bNumConfigurations; c++) {
522                 struct usb_config_descriptor *cf = &dev->config[c];
523
524                 if (cf->interface != NULL) {
525                         free(cf->interface);
526                         cf->interface = NULL;
527                 }
528         }
529
530         free(dev->config);
531         dev->config = NULL;
532         return;
533 }
534
535 void
536 usb_fetch_and_parse_descriptors(usb_dev_handle * udev)
537 {
538         struct usb_device *dev;
539         struct libusb20_device *pdev;
540         uint8_t *ptr;
541         int error;
542         uint32_t size;
543         uint16_t len;
544         uint8_t x;
545
546         if (udev == NULL) {
547                 /* be NULL safe */
548                 return;
549         }
550         dev = usb_device(udev);
551         pdev = (void *)udev;
552
553         if (dev->descriptor.bNumConfigurations == 0) {
554                 /* invalid device */
555                 return;
556         }
557         size = dev->descriptor.bNumConfigurations *
558             sizeof(struct usb_config_descriptor);
559
560         dev->config = malloc(size);
561         if (dev->config == NULL) {
562                 /* out of memory */
563                 return;
564         }
565         memset(dev->config, 0, size);
566
567         for (x = 0; x != dev->descriptor.bNumConfigurations; x++) {
568
569                 error = (pdev->methods->get_config_desc_full) (
570                     pdev, &ptr, &len, x);
571
572                 if (error) {
573                         usb_destroy_configuration(dev);
574                         return;
575                 }
576                 usb_parse_configuration(dev->config + x, ptr);
577
578                 /* free config buffer */
579                 free(ptr);
580         }
581         return;
582 }
583
584 static int
585 usb_std_io(usb_dev_handle * dev, int ep, char *bytes, int size,
586     int timeout, int is_intr)
587 {
588         struct libusb20_transfer *xfer;
589         uint32_t temp;
590         uint32_t maxsize;
591         uint32_t actlen;
592         char *oldbytes;
593
594         xfer = usb_get_transfer_by_ep_no(dev, ep);
595         if (xfer == NULL)
596                 return (-1);
597
598         if (libusb20_tr_pending(xfer)) {
599                 /* there is already a transfer ongoing */
600                 return (-1);
601         }
602         maxsize = libusb20_tr_get_max_total_length(xfer);
603         oldbytes = bytes;
604
605         /*
606          * We allow transferring zero bytes which is the same
607          * equivalent to a zero length USB packet.
608          */
609         do {
610
611                 temp = size;
612                 if (temp > maxsize) {
613                         /* find maximum possible length */
614                         temp = maxsize;
615                 }
616                 if (is_intr)
617                         libusb20_tr_setup_intr(xfer, bytes, temp, timeout);
618                 else
619                         libusb20_tr_setup_bulk(xfer, bytes, temp, timeout);
620
621                 libusb20_tr_start(xfer);
622
623                 while (1) {
624
625                         if (libusb20_dev_process((void *)dev) != 0) {
626                                 /* device detached */
627                                 return (-1);
628                         }
629                         if (libusb20_tr_pending(xfer) == 0) {
630                                 /* transfer complete */
631                                 break;
632                         }
633                         /* wait for USB event from kernel */
634                         libusb20_dev_wait_process((void *)dev, -1);
635                 }
636
637                 switch (libusb20_tr_get_status(xfer)) {
638                 case 0:
639                         /* success */
640                         break;
641                 case LIBUSB20_TRANSFER_TIMED_OUT:
642                         /* transfer timeout */
643                         return (-ETIMEDOUT);
644                 default:
645                         /* other transfer error */
646                         return (-ENXIO);
647                 }
648                 actlen = libusb20_tr_get_actual_length(xfer);
649
650                 bytes += actlen;
651                 size -= actlen;
652
653                 if (actlen != temp) {
654                         /* short transfer */
655                         break;
656                 }
657         } while (size > 0);
658
659         return (bytes - oldbytes);
660 }
661
662 int
663 usb_bulk_write(usb_dev_handle * dev, int ep, char *bytes,
664     int size, int timeout)
665 {
666         return (usb_std_io(dev, ep & ~USB_ENDPOINT_DIR_MASK,
667             bytes, size, timeout, 0));
668 }
669
670 int
671 usb_bulk_read(usb_dev_handle * dev, int ep, char *bytes,
672     int size, int timeout)
673 {
674         return (usb_std_io(dev, ep | USB_ENDPOINT_DIR_MASK,
675             bytes, size, timeout, 0));
676 }
677
678 int
679 usb_interrupt_write(usb_dev_handle * dev, int ep, char *bytes,
680     int size, int timeout)
681 {
682         return (usb_std_io(dev, ep & ~USB_ENDPOINT_DIR_MASK,
683             bytes, size, timeout, 1));
684 }
685
686 int
687 usb_interrupt_read(usb_dev_handle * dev, int ep, char *bytes,
688     int size, int timeout)
689 {
690         return (usb_std_io(dev, ep | USB_ENDPOINT_DIR_MASK,
691             bytes, size, timeout, 1));
692 }
693
694 int
695 usb_control_msg(usb_dev_handle * dev, int requesttype, int request,
696     int value, int wIndex, char *bytes, int size, int timeout)
697 {
698         struct LIBUSB20_CONTROL_SETUP_DECODED req;
699         int err;
700         uint16_t actlen;
701
702         LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &req);
703
704         req.bmRequestType = requesttype;
705         req.bRequest = request;
706         req.wValue = value;
707         req.wIndex = wIndex;
708         req.wLength = size;
709
710         err = libusb20_dev_request_sync((void *)dev, &req, bytes,
711             &actlen, timeout, 0);
712
713         if (err)
714                 return (-1);
715
716         return (actlen);
717 }
718
719 int
720 usb_set_configuration(usb_dev_handle * udev, int bConfigurationValue)
721 {
722         struct usb_device *dev;
723         int err;
724         uint8_t i;
725
726         /*
727          * Need to translate from "bConfigurationValue" to
728          * configuration index:
729          */
730
731         if (bConfigurationValue == 0) {
732                 /* unconfigure */
733                 i = 255;
734         } else {
735                 /* lookup configuration index */
736                 dev = usb_device(udev);
737
738                 /* check if the configuration array is not there */
739                 if (dev->config == NULL) {
740                         return (-1);
741                 }
742                 for (i = 0;; i++) {
743                         if (i == dev->descriptor.bNumConfigurations) {
744                                 /* "bConfigurationValue" not found */
745                                 return (-1);
746                         }
747                         if ((dev->config + i)->bConfigurationValue ==
748                             bConfigurationValue) {
749                                 break;
750                         }
751                 }
752         }
753
754         err = libusb20_dev_set_config_index((void *)udev, i);
755
756         if (err)
757                 return (-1);
758
759         return (0);
760 }
761
762 int
763 usb_claim_interface(usb_dev_handle * dev, int interface)
764 {
765         struct libusb20_device *pdev = (void *)dev;
766
767         pdev->claimed_interface = interface;
768
769         return (0);
770 }
771
772 int
773 usb_release_interface(usb_dev_handle * dev, int interface)
774 {
775         /* do nothing */
776         return (0);
777 }
778
779 int
780 usb_set_altinterface(usb_dev_handle * dev, int alternate)
781 {
782         struct libusb20_device *pdev = (void *)dev;
783         int err;
784         uint8_t iface;
785
786         iface = pdev->claimed_interface;
787
788         err = libusb20_dev_set_alt_index((void *)dev, iface, alternate);
789
790         if (err)
791                 return (-1);
792
793         return (0);
794 }
795
796 int
797 usb_resetep(usb_dev_handle * dev, unsigned int ep)
798 {
799         /* emulate an endpoint reset through clear-STALL */
800         return (usb_clear_halt(dev, ep));
801 }
802
803 int
804 usb_clear_halt(usb_dev_handle * dev, unsigned int ep)
805 {
806         struct libusb20_transfer *xfer;
807
808         xfer = usb_get_transfer_by_ep_no(dev, ep);
809         if (xfer == NULL)
810                 return (-1);
811
812         libusb20_tr_clear_stall_sync(xfer);
813
814         return (0);
815 }
816
817 int
818 usb_reset(usb_dev_handle * dev)
819 {
820         int err;
821
822         err = libusb20_dev_reset((void *)dev);
823
824         if (err)
825                 return (-1);
826
827         /*
828          * Be compatible with LibUSB from sourceforge and close the
829          * handle after reset!
830          */
831         return (usb_close(dev));
832 }
833
834 int
835 usb_check_connected(usb_dev_handle * dev)
836 {
837         int err;
838
839         err = libusb20_dev_check_connected((void *)dev);
840
841         if (err)
842                 return (-1);
843
844         return (0);
845 }
846
847 const char *
848 usb_strerror(void)
849 {
850         /* TODO */
851         return ("Unknown error");
852 }
853
854 void
855 usb_init(void)
856 {
857         /* nothing to do */
858         return;
859 }
860
861 void
862 usb_set_debug(int level)
863 {
864         /* use kernel UGEN debugging if you need to see what is going on */
865         return;
866 }
867
868 int
869 usb_find_busses(void)
870 {
871         usb_busses = &usb_global_bus;
872         return (1);
873 }
874
875 int
876 usb_find_devices(void)
877 {
878         struct libusb20_device *pdev;
879         struct usb_device *udev;
880         struct LIBUSB20_DEVICE_DESC_DECODED *ddesc;
881         int devnum;
882         int err;
883
884         /* cleanup after last device search */
885         /* close all opened devices, if any */
886
887         while ((pdev = libusb20_be_device_foreach(usb_backend, NULL))) {
888                 udev = pdev->privLuData;
889                 libusb20_be_dequeue_device(usb_backend, pdev);
890                 libusb20_dev_free(pdev);
891                 if (udev != NULL) {
892                         LIST_DEL(usb_global_bus.devices, udev);
893                         free(udev);
894                 }
895         }
896
897         /* free old USB backend, if any */
898
899         libusb20_be_free(usb_backend);
900
901         /* do a new backend device search */
902         usb_backend = libusb20_be_alloc_default();
903         if (usb_backend == NULL) {
904                 return (-1);
905         }
906         /* iterate all devices */
907
908         devnum = 1;
909         pdev = NULL;
910         while ((pdev = libusb20_be_device_foreach(usb_backend, pdev))) {
911                 udev = malloc(sizeof(*udev));
912                 if (udev == NULL)
913                         break;
914
915                 memset(udev, 0, sizeof(*udev));
916
917                 udev->bus = &usb_global_bus;
918
919                 snprintf(udev->filename, sizeof(udev->filename),
920                     "/dev/ugen%u.%u",
921                     libusb20_dev_get_bus_number(pdev),
922                     libusb20_dev_get_address(pdev));
923
924                 ddesc = libusb20_dev_get_device_desc(pdev);
925
926                 udev->descriptor.bLength = sizeof(udev->descriptor);
927                 udev->descriptor.bDescriptorType = ddesc->bDescriptorType;
928                 udev->descriptor.bcdUSB = ddesc->bcdUSB;
929                 udev->descriptor.bDeviceClass = ddesc->bDeviceClass;
930                 udev->descriptor.bDeviceSubClass = ddesc->bDeviceSubClass;
931                 udev->descriptor.bDeviceProtocol = ddesc->bDeviceProtocol;
932                 udev->descriptor.bMaxPacketSize0 = ddesc->bMaxPacketSize0;
933                 udev->descriptor.idVendor = ddesc->idVendor;
934                 udev->descriptor.idProduct = ddesc->idProduct;
935                 udev->descriptor.bcdDevice = ddesc->bcdDevice;
936                 udev->descriptor.iManufacturer = ddesc->iManufacturer;
937                 udev->descriptor.iProduct = ddesc->iProduct;
938                 udev->descriptor.iSerialNumber = ddesc->iSerialNumber;
939                 udev->descriptor.bNumConfigurations =
940                     ddesc->bNumConfigurations;
941                 if (udev->descriptor.bNumConfigurations > USB_MAXCONFIG) {
942                         /* truncate number of configurations */
943                         udev->descriptor.bNumConfigurations = USB_MAXCONFIG;
944                 }
945                 udev->devnum = devnum++;
946                 /* link together the two structures */
947                 udev->dev = pdev;
948                 pdev->privLuData = udev;
949
950                 err = libusb20_dev_open(pdev, 0);
951                 if (err == 0) {
952                         /* XXX get all config descriptors by default */
953                         usb_fetch_and_parse_descriptors((void *)pdev);
954                         libusb20_dev_close(pdev);
955                 }
956                 LIST_ADD(usb_global_bus.devices, udev);
957         }
958
959         return (devnum - 1);                    /* success */
960 }
961
962 struct usb_device *
963 usb_device(usb_dev_handle * dev)
964 {
965         struct libusb20_device *pdev;
966
967         pdev = (void *)dev;
968
969         return (pdev->privLuData);
970 }
971
972 struct usb_bus *
973 usb_get_busses(void)
974 {
975         return (usb_busses);
976 }
977
978 int
979 usb_get_driver_np(usb_dev_handle * dev, int interface, char *name, int namelen)
980 {
981         struct libusb20_device *pdev;
982         char *ptr;
983         int err;
984
985         pdev = (void *)dev;
986
987         if (pdev == NULL)
988                 return (-1);
989         if (namelen < 1)
990                 return (-1);
991         if (namelen > 255)
992                 namelen = 255;
993
994         err = libusb20_dev_get_iface_desc(pdev, interface, name, namelen);
995         if (err != 0)
996                 return (-1);
997
998         /* we only want the driver name */
999         ptr = strstr(name, ":");
1000         if (ptr != NULL)
1001                 *ptr = 0;
1002
1003         return (0);
1004 }
1005
1006 int
1007 usb_detach_kernel_driver_np(usb_dev_handle * dev, int interface)
1008 {
1009         struct libusb20_device *pdev;
1010         int err;
1011
1012         pdev = (void *)dev;
1013
1014         if (pdev == NULL)
1015                 return (-1);
1016
1017         err = libusb20_dev_detach_kernel_driver(pdev, interface);
1018         if (err != 0)
1019                 return (-1);
1020
1021         return (0);
1022 }