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