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