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