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