1 /* $FreeBSD: stable/9/lib/libusb/libusb10.c 362224 2020-06-16 12:21:55Z kevans $ */
2 /*-
3  * Copyright (c) 2009 Sylvestre Gallon. All rights reserved.
4  * Copyright (c) 2009 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 #include <sys/fcntl.h>
29 #include <sys/ioctl.h>
30 #include <sys/queue.h>
31 
32 #include <assert.h>
33 #include <errno.h>
34 #include <poll.h>
35 #include <pthread.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <unistd.h>
39 
40 #define	libusb_device_handle libusb20_device
41 
42 #include "libusb20.h"
43 #include "libusb20_desc.h"
44 #include "libusb20_int.h"
45 #include "libusb.h"
46 #include "libusb10.h"
47 
48 #define	LIBUSB_NUM_SW_ENDPOINTS	(16 * 4)
49 
50 static pthread_mutex_t default_context_lock = PTHREAD_MUTEX_INITIALIZER;
51 struct libusb_context *usbi_default_context = NULL;
52 
53 /* Prototypes */
54 
55 static struct libusb20_transfer *libusb10_get_transfer(struct libusb20_device *, uint8_t, uint8_t);
56 static int libusb10_get_buffsize(struct libusb20_device *, libusb_transfer *);
57 static int libusb10_convert_error(uint8_t status);
58 static void libusb10_complete_transfer(struct libusb20_transfer *, struct libusb_super_transfer *, int);
59 static void libusb10_isoc_proxy(struct libusb20_transfer *);
60 static void libusb10_bulk_intr_proxy(struct libusb20_transfer *);
61 static void libusb10_ctrl_proxy(struct libusb20_transfer *);
62 static void libusb10_submit_transfer_sub(struct libusb20_device *, uint8_t);
63 
64 /*  Library initialisation / deinitialisation */
65 
66 static const struct libusb_version libusb_version = {
67 	.major = 1,
68 	.minor = 0,
69 	.micro = 0,
70 	.nano = 2016,
71 	.rc = "",
72 	.describe = "http://www.freebsd.org"
73 };
74 
75 const struct libusb_version *
libusb_get_version(void)76 libusb_get_version(void)
77 {
78 
79 	return (&libusb_version);
80 }
81 
82 void
libusb_set_debug(libusb_context * ctx,int level)83 libusb_set_debug(libusb_context *ctx, int level)
84 {
85 	ctx = GET_CONTEXT(ctx);
86 	if (ctx)
87 		ctx->debug = level;
88 }
89 
90 static void
libusb_set_nonblocking(int f)91 libusb_set_nonblocking(int f)
92 {
93 	int flags;
94 
95 	/*
96 	 * We ignore any failures in this function, hence the
97 	 * non-blocking flag is not critical to the operation of
98 	 * libUSB. We use F_GETFL and F_SETFL to be compatible with
99 	 * Linux.
100 	 */
101 
102 	flags = fcntl(f, F_GETFL, NULL);
103 	if (flags == -1)
104 		return;
105 	flags |= O_NONBLOCK;
106 	fcntl(f, F_SETFL, flags);
107 }
108 
109 static void
libusb10_wakeup_event_loop(libusb_context * ctx)110 libusb10_wakeup_event_loop(libusb_context *ctx)
111 {
112 	uint8_t dummy = 0;
113 	int err;
114 
115 	err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy));
116 	if (err < (int)sizeof(dummy)) {
117 		/* ignore error, if any */
118 		DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "Waking up event loop failed!");
119 	}
120 }
121 
122 int
libusb_init(libusb_context ** context)123 libusb_init(libusb_context **context)
124 {
125 	struct libusb_context *ctx;
126 	pthread_condattr_t attr;
127 	char *debug;
128 	int ret;
129 
130 	ctx = malloc(sizeof(*ctx));
131 	if (!ctx)
132 		return (LIBUSB_ERROR_INVALID_PARAM);
133 
134 	memset(ctx, 0, sizeof(*ctx));
135 
136 	debug = getenv("LIBUSB_DEBUG");
137 	if (debug != NULL) {
138 		ctx->debug = atoi(debug);
139 		if (ctx->debug != 0)
140 			ctx->debug_fixed = 1;
141 	}
142 	TAILQ_INIT(&ctx->pollfds);
143 	TAILQ_INIT(&ctx->tr_done);
144 	TAILQ_INIT(&ctx->hotplug_cbh);
145 	TAILQ_INIT(&ctx->hotplug_devs);
146 
147 	if (pthread_mutex_init(&ctx->ctx_lock, NULL) != 0) {
148 		free(ctx);
149 		return (LIBUSB_ERROR_NO_MEM);
150 	}
151 	if (pthread_mutex_init(&ctx->hotplug_lock, NULL) != 0) {
152 		pthread_mutex_destroy(&ctx->ctx_lock);
153 		free(ctx);
154 		return (LIBUSB_ERROR_NO_MEM);
155 	}
156 	if (pthread_condattr_init(&attr) != 0) {
157 		pthread_mutex_destroy(&ctx->ctx_lock);
158 		pthread_mutex_destroy(&ctx->hotplug_lock);
159 		free(ctx);
160 		return (LIBUSB_ERROR_NO_MEM);
161 	}
162 	if (pthread_condattr_setclock(&attr, CLOCK_MONOTONIC) != 0) {
163 		pthread_mutex_destroy(&ctx->ctx_lock);
164 		pthread_mutex_destroy(&ctx->hotplug_lock);
165 		pthread_condattr_destroy(&attr);
166 		free(ctx);
167 		return (LIBUSB_ERROR_OTHER);
168 	}
169 	if (pthread_cond_init(&ctx->ctx_cond, &attr) != 0) {
170 		pthread_mutex_destroy(&ctx->ctx_lock);
171 		pthread_mutex_destroy(&ctx->hotplug_lock);
172 		pthread_condattr_destroy(&attr);
173 		free(ctx);
174 		return (LIBUSB_ERROR_NO_MEM);
175 	}
176 	pthread_condattr_destroy(&attr);
177 
178 	ctx->ctx_handler = NO_THREAD;
179 	ctx->hotplug_handler = NO_THREAD;
180 
181 	ret = pipe(ctx->ctrl_pipe);
182 	if (ret < 0) {
183 		pthread_mutex_destroy(&ctx->ctx_lock);
184 		pthread_mutex_destroy(&ctx->hotplug_lock);
185 		pthread_cond_destroy(&ctx->ctx_cond);
186 		free(ctx);
187 		return (LIBUSB_ERROR_OTHER);
188 	}
189 	/* set non-blocking mode on the control pipe to avoid deadlock */
190 	libusb_set_nonblocking(ctx->ctrl_pipe[0]);
191 	libusb_set_nonblocking(ctx->ctrl_pipe[1]);
192 
193 	libusb10_add_pollfd(ctx, &ctx->ctx_poll, NULL, ctx->ctrl_pipe[0], POLLIN);
194 
195 	pthread_mutex_lock(&default_context_lock);
196 	if (usbi_default_context == NULL) {
197 		usbi_default_context = ctx;
198 	}
199 	pthread_mutex_unlock(&default_context_lock);
200 
201 	if (context)
202 		*context = ctx;
203 
204 	DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_init complete");
205 
206 	return (0);
207 }
208 
209 void
libusb_exit(libusb_context * ctx)210 libusb_exit(libusb_context *ctx)
211 {
212 	ctx = GET_CONTEXT(ctx);
213 
214 	if (ctx == NULL)
215 		return;
216 
217 	/* stop hotplug thread, if any */
218 
219 	if (ctx->hotplug_handler != NO_THREAD) {
220 		pthread_t td;
221 		void *ptr;
222 
223 		HOTPLUG_LOCK(ctx);
224 		td = ctx->hotplug_handler;
225 		ctx->hotplug_handler = NO_THREAD;
226 		HOTPLUG_UNLOCK(ctx);
227 
228 		pthread_join(td, &ptr);
229 	}
230 
231 	/* XXX cleanup devices */
232 
233 	libusb10_remove_pollfd(ctx, &ctx->ctx_poll);
234 	close(ctx->ctrl_pipe[0]);
235 	close(ctx->ctrl_pipe[1]);
236 	pthread_mutex_destroy(&ctx->ctx_lock);
237 	pthread_mutex_destroy(&ctx->hotplug_lock);
238 	pthread_cond_destroy(&ctx->ctx_cond);
239 
240 	pthread_mutex_lock(&default_context_lock);
241 	if (ctx == usbi_default_context) {
242 		usbi_default_context = NULL;
243 	}
244 	pthread_mutex_unlock(&default_context_lock);
245 
246 	free(ctx);
247 }
248 
249 /* Device handling and initialisation. */
250 
251 ssize_t
libusb_get_device_list(libusb_context * ctx,libusb_device *** list)252 libusb_get_device_list(libusb_context *ctx, libusb_device ***list)
253 {
254 	struct libusb20_backend *usb_backend;
255 	struct libusb20_device *pdev;
256 	struct libusb_device *dev;
257 	int i;
258 
259 	ctx = GET_CONTEXT(ctx);
260 
261 	if (ctx == NULL)
262 		return (LIBUSB_ERROR_INVALID_PARAM);
263 
264 	if (list == NULL)
265 		return (LIBUSB_ERROR_INVALID_PARAM);
266 
267 	usb_backend = libusb20_be_alloc_default();
268 	if (usb_backend == NULL)
269 		return (LIBUSB_ERROR_NO_MEM);
270 
271 	/* figure out how many USB devices are present */
272 	pdev = NULL;
273 	i = 0;
274 	while ((pdev = libusb20_be_device_foreach(usb_backend, pdev)))
275 		i++;
276 
277 	/* allocate device pointer list */
278 	*list = malloc((i + 1) * sizeof(void *));
279 	if (*list == NULL) {
280 		libusb20_be_free(usb_backend);
281 		return (LIBUSB_ERROR_NO_MEM);
282 	}
283 	/* create libusb v1.0 compliant devices */
284 	i = 0;
285 	while ((pdev = libusb20_be_device_foreach(usb_backend, NULL))) {
286 
287 		dev = malloc(sizeof(*dev));
288 		if (dev == NULL) {
289 			while (i != 0) {
290 				libusb_unref_device((*list)[i - 1]);
291 				i--;
292 			}
293 			free(*list);
294 			*list = NULL;
295 			libusb20_be_free(usb_backend);
296 			return (LIBUSB_ERROR_NO_MEM);
297 		}
298 		/* get device into libUSB v1.0 list */
299 		libusb20_be_dequeue_device(usb_backend, pdev);
300 
301 		memset(dev, 0, sizeof(*dev));
302 
303 		/* init transfer queues */
304 		TAILQ_INIT(&dev->tr_head);
305 
306 		/* set context we belong to */
307 		dev->ctx = ctx;
308 
309 		/* link together the two structures */
310 		dev->os_priv = pdev;
311 		pdev->privLuData = dev;
312 
313 		(*list)[i] = libusb_ref_device(dev);
314 		i++;
315 	}
316 	(*list)[i] = NULL;
317 
318 	libusb20_be_free(usb_backend);
319 	return (i);
320 }
321 
322 void
libusb_free_device_list(libusb_device ** list,int unref_devices)323 libusb_free_device_list(libusb_device **list, int unref_devices)
324 {
325 	int i;
326 
327 	if (list == NULL)
328 		return;			/* be NULL safe */
329 
330 	if (unref_devices) {
331 		for (i = 0; list[i] != NULL; i++)
332 			libusb_unref_device(list[i]);
333 	}
334 	free(list);
335 }
336 
337 uint8_t
libusb_get_bus_number(libusb_device * dev)338 libusb_get_bus_number(libusb_device *dev)
339 {
340 	if (dev == NULL)
341 		return (0);		/* should not happen */
342 	return (libusb20_dev_get_bus_number(dev->os_priv));
343 }
344 
345 uint8_t
libusb_get_port_number(libusb_device * dev)346 libusb_get_port_number(libusb_device *dev)
347 {
348 	if (dev == NULL)
349 		return (0);		/* should not happen */
350 	return (libusb20_dev_get_parent_port(dev->os_priv));
351 }
352 
353 int
libusb_get_port_numbers(libusb_device * dev,uint8_t * buf,uint8_t bufsize)354 libusb_get_port_numbers(libusb_device *dev, uint8_t *buf, uint8_t bufsize)
355 {
356 	return (libusb20_dev_get_port_path(dev->os_priv, buf, bufsize));
357 }
358 
359 int
libusb_get_port_path(libusb_context * ctx,libusb_device * dev,uint8_t * buf,uint8_t bufsize)360 libusb_get_port_path(libusb_context *ctx, libusb_device *dev, uint8_t *buf,
361     uint8_t bufsize)
362 {
363 	return (libusb20_dev_get_port_path(dev->os_priv, buf, bufsize));
364 }
365 
366 uint8_t
libusb_get_device_address(libusb_device * dev)367 libusb_get_device_address(libusb_device *dev)
368 {
369 	if (dev == NULL)
370 		return (0);		/* should not happen */
371 	return (libusb20_dev_get_address(dev->os_priv));
372 }
373 
374 enum libusb_speed
libusb_get_device_speed(libusb_device * dev)375 libusb_get_device_speed(libusb_device *dev)
376 {
377 	if (dev == NULL)
378 		return (LIBUSB_SPEED_UNKNOWN);	/* should not happen */
379 
380 	switch (libusb20_dev_get_speed(dev->os_priv)) {
381 	case LIBUSB20_SPEED_LOW:
382 		return (LIBUSB_SPEED_LOW);
383 	case LIBUSB20_SPEED_FULL:
384 		return (LIBUSB_SPEED_FULL);
385 	case LIBUSB20_SPEED_HIGH:
386 		return (LIBUSB_SPEED_HIGH);
387 	case LIBUSB20_SPEED_SUPER:
388 		return (LIBUSB_SPEED_SUPER);
389 	default:
390 		break;
391 	}
392 	return (LIBUSB_SPEED_UNKNOWN);
393 }
394 
395 int
libusb_get_max_packet_size(libusb_device * dev,uint8_t endpoint)396 libusb_get_max_packet_size(libusb_device *dev, uint8_t endpoint)
397 {
398 	struct libusb_config_descriptor *pdconf;
399 	struct libusb_interface *pinf;
400 	struct libusb_interface_descriptor *pdinf;
401 	struct libusb_endpoint_descriptor *pdend;
402 	int i;
403 	int j;
404 	int k;
405 	int ret;
406 
407 	if (dev == NULL)
408 		return (LIBUSB_ERROR_NO_DEVICE);
409 
410 	ret = libusb_get_active_config_descriptor(dev, &pdconf);
411 	if (ret < 0)
412 		return (ret);
413 
414 	ret = LIBUSB_ERROR_NOT_FOUND;
415 	for (i = 0; i < pdconf->bNumInterfaces; i++) {
416 		pinf = &pdconf->interface[i];
417 		for (j = 0; j < pinf->num_altsetting; j++) {
418 			pdinf = &pinf->altsetting[j];
419 			for (k = 0; k < pdinf->bNumEndpoints; k++) {
420 				pdend = &pdinf->endpoint[k];
421 				if (pdend->bEndpointAddress == endpoint) {
422 					ret = pdend->wMaxPacketSize;
423 					goto out;
424 				}
425 			}
426 		}
427 	}
428 
429 out:
430 	libusb_free_config_descriptor(pdconf);
431 	return (ret);
432 }
433 
434 int
libusb_get_max_iso_packet_size(libusb_device * dev,uint8_t endpoint)435 libusb_get_max_iso_packet_size(libusb_device *dev, uint8_t endpoint)
436 {
437 	int multiplier;
438 	int ret;
439 
440 	ret = libusb_get_max_packet_size(dev, endpoint);
441 
442 	switch (libusb20_dev_get_speed(dev->os_priv)) {
443 	case LIBUSB20_SPEED_LOW:
444 	case LIBUSB20_SPEED_FULL:
445 		break;
446 	default:
447 		if (ret > -1) {
448 			multiplier = (1 + ((ret >> 11) & 3));
449 			if (multiplier > 3)
450 				multiplier = 3;
451 			ret = (ret & 0x7FF) * multiplier;
452 		}
453 		break;
454 	}
455 	return (ret);
456 }
457 
458 libusb_device *
libusb_ref_device(libusb_device * dev)459 libusb_ref_device(libusb_device *dev)
460 {
461 	if (dev == NULL)
462 		return (NULL);		/* be NULL safe */
463 
464 	CTX_LOCK(dev->ctx);
465 	dev->refcnt++;
466 	CTX_UNLOCK(dev->ctx);
467 
468 	return (dev);
469 }
470 
471 void
libusb_unref_device(libusb_device * dev)472 libusb_unref_device(libusb_device *dev)
473 {
474 	if (dev == NULL)
475 		return;			/* be NULL safe */
476 
477 	CTX_LOCK(dev->ctx);
478 	dev->refcnt--;
479 	CTX_UNLOCK(dev->ctx);
480 
481 	if (dev->refcnt == 0) {
482 		libusb20_dev_free(dev->os_priv);
483 		free(dev);
484 	}
485 }
486 
487 int
libusb_open(libusb_device * dev,libusb_device_handle ** devh)488 libusb_open(libusb_device *dev, libusb_device_handle **devh)
489 {
490 	libusb_context *ctx = dev->ctx;
491 	struct libusb20_device *pdev = dev->os_priv;
492 	int err;
493 
494 	if (devh == NULL)
495 		return (LIBUSB_ERROR_INVALID_PARAM);
496 
497 	/* set default device handle value */
498 	*devh = NULL;
499 
500 	dev = libusb_ref_device(dev);
501 	if (dev == NULL)
502 		return (LIBUSB_ERROR_INVALID_PARAM);
503 
504 	err = libusb20_dev_open(pdev, LIBUSB_NUM_SW_ENDPOINTS);
505 	if (err) {
506 		libusb_unref_device(dev);
507 		return (LIBUSB_ERROR_NO_MEM);
508 	}
509 	libusb10_add_pollfd(ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
510 	    POLLOUT | POLLRDNORM | POLLWRNORM);
511 
512 	/* make sure our event loop detects the new device */
513 	libusb10_wakeup_event_loop(ctx);
514 
515 	*devh = pdev;
516 
517 	return (0);
518 }
519 
520 libusb_device_handle *
libusb_open_device_with_vid_pid(libusb_context * ctx,uint16_t vendor_id,uint16_t product_id)521 libusb_open_device_with_vid_pid(libusb_context *ctx, uint16_t vendor_id,
522     uint16_t product_id)
523 {
524 	struct libusb_device **devs;
525 	struct libusb20_device *pdev;
526 	struct LIBUSB20_DEVICE_DESC_DECODED *pdesc;
527 	int i;
528 	int j;
529 
530 	ctx = GET_CONTEXT(ctx);
531 	if (ctx == NULL)
532 		return (NULL);		/* be NULL safe */
533 
534 	DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid enter");
535 
536 	if ((i = libusb_get_device_list(ctx, &devs)) < 0)
537 		return (NULL);
538 
539 	pdev = NULL;
540 	for (j = 0; j < i; j++) {
541 		struct libusb20_device *tdev;
542 
543 		tdev = devs[j]->os_priv;
544 		pdesc = libusb20_dev_get_device_desc(tdev);
545 		/*
546 		 * NOTE: The USB library will automatically swap the
547 		 * fields in the device descriptor to be of host
548 		 * endian type!
549 		 */
550 		if (pdesc->idVendor == vendor_id &&
551 		    pdesc->idProduct == product_id) {
552 			libusb_open(devs[j], &pdev);
553 			break;
554 		}
555 	}
556 
557 	libusb_free_device_list(devs, 1);
558 	DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid leave");
559 	return (pdev);
560 }
561 
562 void
libusb_close(struct libusb20_device * pdev)563 libusb_close(struct libusb20_device *pdev)
564 {
565 	libusb_context *ctx;
566 	struct libusb_device *dev;
567 
568 	if (pdev == NULL)
569 		return;			/* be NULL safe */
570 
571 	dev = libusb_get_device(pdev);
572 	ctx = dev->ctx;
573 
574 	libusb10_remove_pollfd(ctx, &dev->dev_poll);
575 
576 	libusb20_dev_close(pdev);
577 
578 	/* unref will free the "pdev" when the refcount reaches zero */
579 	libusb_unref_device(dev);
580 
581 	/* make sure our event loop detects the closed device */
582 	libusb10_wakeup_event_loop(ctx);
583 }
584 
585 libusb_device *
libusb_get_device(struct libusb20_device * pdev)586 libusb_get_device(struct libusb20_device *pdev)
587 {
588 	if (pdev == NULL)
589 		return (NULL);
590 	return ((libusb_device *)pdev->privLuData);
591 }
592 
593 int
libusb_get_configuration(struct libusb20_device * pdev,int * config)594 libusb_get_configuration(struct libusb20_device *pdev, int *config)
595 {
596 	struct libusb20_config *pconf;
597 
598 	if (pdev == NULL || config == NULL)
599 		return (LIBUSB_ERROR_INVALID_PARAM);
600 
601 	pconf = libusb20_dev_alloc_config(pdev, libusb20_dev_get_config_index(pdev));
602 	if (pconf == NULL)
603 		return (LIBUSB_ERROR_NO_MEM);
604 
605 	*config = pconf->desc.bConfigurationValue;
606 
607 	free(pconf);
608 
609 	return (0);
610 }
611 
612 int
libusb_set_configuration(struct libusb20_device * pdev,int configuration)613 libusb_set_configuration(struct libusb20_device *pdev, int configuration)
614 {
615 	struct libusb20_config *pconf;
616 	struct libusb_device *dev;
617 	int err;
618 	uint8_t i;
619 
620 	dev = libusb_get_device(pdev);
621 	if (dev == NULL)
622 		return (LIBUSB_ERROR_INVALID_PARAM);
623 
624 	if (configuration < 1) {
625 		/* unconfigure */
626 		i = 255;
627 	} else {
628 		for (i = 0; i != 255; i++) {
629 			uint8_t found;
630 
631 			pconf = libusb20_dev_alloc_config(pdev, i);
632 			if (pconf == NULL)
633 				return (LIBUSB_ERROR_INVALID_PARAM);
634 			found = (pconf->desc.bConfigurationValue
635 			    == configuration);
636 			free(pconf);
637 
638 			if (found)
639 				goto set_config;
640 		}
641 		return (LIBUSB_ERROR_INVALID_PARAM);
642 	}
643 
644 set_config:
645 
646 	libusb10_cancel_all_transfer(dev);
647 
648 	libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
649 
650 	err = libusb20_dev_set_config_index(pdev, i);
651 
652 	libusb10_add_pollfd(dev->ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN |
653 	    POLLOUT | POLLRDNORM | POLLWRNORM);
654 
655 	return (err ? LIBUSB_ERROR_INVALID_PARAM : 0);
656 }
657 
658 int
libusb_claim_interface(struct libusb20_device * pdev,int interface_number)659 libusb_claim_interface(struct libusb20_device *pdev, int interface_number)
660 {
661 	libusb_device *dev;
662 	int err = 0;
663 
664 	dev = libusb_get_device(pdev);
665 	if (dev == NULL)
666 		return (LIBUSB_ERROR_INVALID_PARAM);
667 
668 	if (interface_number < 0 || interface_number > 31)
669 		return (LIBUSB_ERROR_INVALID_PARAM);
670 
671 	if (pdev->auto_detach != 0) {
672 		err = libusb_detach_kernel_driver(pdev, interface_number);
673 		if (err != 0)
674 			goto done;
675 	}
676 
677 	CTX_LOCK(dev->ctx);
678 	dev->claimed_interfaces |= (1 << interface_number);
679 	CTX_UNLOCK(dev->ctx);
680 done:
681 	return (err);
682 }
683 
684 int
libusb_release_interface(struct libusb20_device * pdev,int interface_number)685 libusb_release_interface(struct libusb20_device *pdev, int interface_number)
686 {
687 	libusb_device *dev;
688 	int err = 0;
689 
690 	dev = libusb_get_device(pdev);
691 	if (dev == NULL)
692 		return (LIBUSB_ERROR_INVALID_PARAM);
693 
694 	if (interface_number < 0 || interface_number > 31)
695 		return (LIBUSB_ERROR_INVALID_PARAM);
696 
697 	if (pdev->auto_detach != 0) {
698 		err = libusb_attach_kernel_driver(pdev, interface_number);
699 		if (err != 0)
700 			goto done;
701 	}
702 
703 	CTX_LOCK(dev->ctx);
704 	if (!(dev->claimed_interfaces & (1 << interface_number)))
705 		err = LIBUSB_ERROR_NOT_FOUND;
706 	else
707 		dev->claimed_interfaces &= ~(1 << interface_number);
708 	CTX_UNLOCK(dev->ctx);
709 done:
710 	return (err);
711 }
712 
713 int
libusb_set_interface_alt_setting(struct libusb20_device * pdev,int interface_number,int alternate_setting)714 libusb_set_interface_alt_setting(struct libusb20_device *pdev,
715     int interface_number, int alternate_setting)
716 {
717 	libusb_device *dev;
718 	int err = 0;
719 
720 	dev = libusb_get_device(pdev);
721 	if (dev == NULL)
722 		return (LIBUSB_ERROR_INVALID_PARAM);
723 
724 	if (interface_number < 0 || interface_number > 31)
725 		return (LIBUSB_ERROR_INVALID_PARAM);
726 
727 	CTX_LOCK(dev->ctx);
728 	if (!(dev->claimed_interfaces & (1 << interface_number)))
729 		err = LIBUSB_ERROR_NOT_FOUND;
730 	CTX_UNLOCK(dev->ctx);
731 
732 	if (err)
733 		return (err);
734 
735 	libusb10_cancel_all_transfer(dev);
736 
737 	libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
738 
739 	err = libusb20_dev_set_alt_index(pdev,
740 	    interface_number, alternate_setting);
741 
742 	libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
743 	    pdev, libusb20_dev_get_fd(pdev),
744 	    POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
745 
746 	return (err ? LIBUSB_ERROR_OTHER : 0);
747 }
748 
749 static struct libusb20_transfer *
libusb10_get_transfer(struct libusb20_device * pdev,uint8_t endpoint,uint8_t xfer_index)750 libusb10_get_transfer(struct libusb20_device *pdev,
751     uint8_t endpoint, uint8_t xfer_index)
752 {
753 	xfer_index &= 1;	/* double buffering */
754 
755 	xfer_index |= (endpoint & LIBUSB20_ENDPOINT_ADDRESS_MASK) * 4;
756 
757 	if (endpoint & LIBUSB20_ENDPOINT_DIR_MASK) {
758 		/* this is an IN endpoint */
759 		xfer_index |= 2;
760 	}
761 	return (libusb20_tr_get_pointer(pdev, xfer_index));
762 }
763 
764 int
libusb_clear_halt(struct libusb20_device * pdev,uint8_t endpoint)765 libusb_clear_halt(struct libusb20_device *pdev, uint8_t endpoint)
766 {
767 	struct libusb20_transfer *xfer;
768 	struct libusb_device *dev;
769 	int err;
770 
771 	xfer = libusb10_get_transfer(pdev, endpoint, 0);
772 	if (xfer == NULL)
773 		return (LIBUSB_ERROR_INVALID_PARAM);
774 
775 	dev = libusb_get_device(pdev);
776 	if (dev == NULL)
777 		return (LIBUSB_ERROR_INVALID_PARAM);
778 
779 	CTX_LOCK(dev->ctx);
780 	err = libusb20_tr_open(xfer, 0, 1, endpoint);
781 	CTX_UNLOCK(dev->ctx);
782 
783 	if (err != 0 && err != LIBUSB20_ERROR_BUSY)
784 		return (LIBUSB_ERROR_OTHER);
785 
786 	libusb20_tr_clear_stall_sync(xfer);
787 
788 	/* check if we opened the transfer */
789 	if (err == 0) {
790 		CTX_LOCK(dev->ctx);
791 		libusb20_tr_close(xfer);
792 		CTX_UNLOCK(dev->ctx);
793 	}
794 	return (0);			/* success */
795 }
796 
797 int
libusb_reset_device(struct libusb20_device * pdev)798 libusb_reset_device(struct libusb20_device *pdev)
799 {
800 	libusb_device *dev;
801 	int err;
802 
803 	dev = libusb_get_device(pdev);
804 	if (dev == NULL)
805 		return (LIBUSB_ERROR_INVALID_PARAM);
806 
807 	libusb10_cancel_all_transfer(dev);
808 
809 	libusb10_remove_pollfd(dev->ctx, &dev->dev_poll);
810 
811 	err = libusb20_dev_reset(pdev);
812 
813 	libusb10_add_pollfd(dev->ctx, &dev->dev_poll,
814 	    pdev, libusb20_dev_get_fd(pdev),
815 	    POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM);
816 
817 	return (err ? LIBUSB_ERROR_OTHER : 0);
818 }
819 
820 int
libusb_check_connected(struct libusb20_device * pdev)821 libusb_check_connected(struct libusb20_device *pdev)
822 {
823 	libusb_device *dev;
824 	int err;
825 
826 	dev = libusb_get_device(pdev);
827 	if (dev == NULL)
828 		return (LIBUSB_ERROR_INVALID_PARAM);
829 
830 	err = libusb20_dev_check_connected(pdev);
831 
832 	return (err ? LIBUSB_ERROR_NO_DEVICE : 0);
833 }
834 
835 int
libusb_kernel_driver_active(struct libusb20_device * pdev,int interface)836 libusb_kernel_driver_active(struct libusb20_device *pdev, int interface)
837 {
838 	if (pdev == NULL)
839 		return (LIBUSB_ERROR_INVALID_PARAM);
840 
841 	if (libusb20_dev_kernel_driver_active(pdev, interface))
842 		return (0);		/* no kernel driver is active */
843 	else
844 		return (1);		/* kernel driver is active */
845 }
846 
847 int
libusb_get_driver_np(struct libusb20_device * pdev,int interface,char * name,int namelen)848 libusb_get_driver_np(struct libusb20_device *pdev, int interface,
849     char *name, int namelen)
850 {
851 	return (libusb_get_driver(pdev, interface, name, namelen));
852 }
853 
854 int
libusb_get_driver(struct libusb20_device * pdev,int interface,char * name,int namelen)855 libusb_get_driver(struct libusb20_device *pdev, int interface,
856     char *name, int namelen)
857 {
858 	char *ptr;
859 	int err;
860 
861 	if (pdev == NULL)
862 		return (LIBUSB_ERROR_INVALID_PARAM);
863 	if (namelen < 1)
864 		return (LIBUSB_ERROR_INVALID_PARAM);
865 	if (namelen > 255)
866 		namelen = 255;
867 
868 	err = libusb20_dev_get_iface_desc(
869 	    pdev, interface, name, namelen);
870 
871 	if (err != 0)
872 		return (LIBUSB_ERROR_OTHER);
873 
874 	/* we only want the driver name */
875 	ptr = strstr(name, ":");
876 	if (ptr != NULL)
877 		*ptr = 0;
878 
879 	return (0);
880 }
881 
882 int
libusb_detach_kernel_driver_np(struct libusb20_device * pdev,int interface)883 libusb_detach_kernel_driver_np(struct libusb20_device *pdev, int interface)
884 {
885 	return (libusb_detach_kernel_driver(pdev, interface));
886 }
887 
888 int
libusb_detach_kernel_driver(struct libusb20_device * pdev,int interface)889 libusb_detach_kernel_driver(struct libusb20_device *pdev, int interface)
890 {
891 	int err;
892 
893 	if (pdev == NULL)
894 		return (LIBUSB_ERROR_INVALID_PARAM);
895 
896 	err = libusb20_dev_detach_kernel_driver(
897 	    pdev, interface);
898 
899 	return (err ? LIBUSB_ERROR_OTHER : 0);
900 }
901 
902 int
libusb_attach_kernel_driver(struct libusb20_device * pdev,int interface)903 libusb_attach_kernel_driver(struct libusb20_device *pdev, int interface)
904 {
905 	if (pdev == NULL)
906 		return (LIBUSB_ERROR_INVALID_PARAM);
907 	/* stub - currently not supported by libusb20 */
908 	return (0);
909 }
910 
911 int
libusb_set_auto_detach_kernel_driver(libusb_device_handle * dev,int enable)912 libusb_set_auto_detach_kernel_driver(libusb_device_handle *dev, int enable)
913 {
914 	dev->auto_detach = (enable ? 1 : 0);
915 	return (0);
916 }
917 
918 /* Asynchronous device I/O */
919 
920 struct libusb_transfer *
libusb_alloc_transfer(int iso_packets)921 libusb_alloc_transfer(int iso_packets)
922 {
923 	struct libusb_transfer *uxfer;
924 	struct libusb_super_transfer *sxfer;
925 	int len;
926 
927 	len = sizeof(struct libusb_transfer) +
928 	    sizeof(struct libusb_super_transfer) +
929 	    (iso_packets * sizeof(libusb_iso_packet_descriptor));
930 
931 	sxfer = malloc(len);
932 	if (sxfer == NULL)
933 		return (NULL);
934 
935 	memset(sxfer, 0, len);
936 
937 	uxfer = (struct libusb_transfer *)(
938 	    ((uint8_t *)sxfer) + sizeof(*sxfer));
939 
940 	/* set default value */
941 	uxfer->num_iso_packets = iso_packets;
942 
943 	return (uxfer);
944 }
945 
946 void
libusb_free_transfer(struct libusb_transfer * uxfer)947 libusb_free_transfer(struct libusb_transfer *uxfer)
948 {
949 	struct libusb_super_transfer *sxfer;
950 
951 	if (uxfer == NULL)
952 		return;			/* be NULL safe */
953 
954 	/* check if we should free the transfer buffer */
955 	if (uxfer->flags & LIBUSB_TRANSFER_FREE_BUFFER)
956 		free(uxfer->buffer);
957 
958 	sxfer = (struct libusb_super_transfer *)(
959 	    (uint8_t *)uxfer - sizeof(*sxfer));
960 
961 	free(sxfer);
962 }
963 
964 static uint32_t
libusb10_get_maxframe(struct libusb20_device * pdev,libusb_transfer * xfer)965 libusb10_get_maxframe(struct libusb20_device *pdev, libusb_transfer *xfer)
966 {
967 	uint32_t ret;
968 
969 	switch (xfer->type) {
970 	case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
971 		ret = 60 | LIBUSB20_MAX_FRAME_PRE_SCALE;	/* 60ms */
972 		break;
973 	case LIBUSB_TRANSFER_TYPE_CONTROL:
974 		ret = 2;
975 		break;
976 	default:
977 		ret = 1;
978 		break;
979 	}
980 	return (ret);
981 }
982 
983 static int
libusb10_get_buffsize(struct libusb20_device * pdev,libusb_transfer * xfer)984 libusb10_get_buffsize(struct libusb20_device *pdev, libusb_transfer *xfer)
985 {
986 	int ret;
987 	int usb_speed;
988 
989 	usb_speed = libusb20_dev_get_speed(pdev);
990 
991 	switch (xfer->type) {
992 	case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
993 		ret = 0;		/* kernel will auto-select */
994 		break;
995 	case LIBUSB_TRANSFER_TYPE_CONTROL:
996 		ret = 1024;
997 		break;
998 	default:
999 		switch (usb_speed) {
1000 		case LIBUSB20_SPEED_LOW:
1001 			ret = 256;
1002 			break;
1003 		case LIBUSB20_SPEED_FULL:
1004 			ret = 4096;
1005 			break;
1006 		case LIBUSB20_SPEED_SUPER:
1007 			ret = 65536;
1008 			break;
1009 		default:
1010 			ret = 16384;
1011 			break;
1012 		}
1013 		break;
1014 	}
1015 	return (ret);
1016 }
1017 
1018 static int
libusb10_convert_error(uint8_t status)1019 libusb10_convert_error(uint8_t status)
1020 {
1021 	;				/* indent fix */
1022 
1023 	switch (status) {
1024 	case LIBUSB20_TRANSFER_START:
1025 	case LIBUSB20_TRANSFER_COMPLETED:
1026 		return (LIBUSB_TRANSFER_COMPLETED);
1027 	case LIBUSB20_TRANSFER_OVERFLOW:
1028 		return (LIBUSB_TRANSFER_OVERFLOW);
1029 	case LIBUSB20_TRANSFER_NO_DEVICE:
1030 		return (LIBUSB_TRANSFER_NO_DEVICE);
1031 	case LIBUSB20_TRANSFER_STALL:
1032 		return (LIBUSB_TRANSFER_STALL);
1033 	case LIBUSB20_TRANSFER_CANCELLED:
1034 		return (LIBUSB_TRANSFER_CANCELLED);
1035 	case LIBUSB20_TRANSFER_TIMED_OUT:
1036 		return (LIBUSB_TRANSFER_TIMED_OUT);
1037 	default:
1038 		return (LIBUSB_TRANSFER_ERROR);
1039 	}
1040 }
1041 
1042 /* This function must be called locked */
1043 
1044 static void
libusb10_complete_transfer(struct libusb20_transfer * pxfer,struct libusb_super_transfer * sxfer,int status)1045 libusb10_complete_transfer(struct libusb20_transfer *pxfer,
1046     struct libusb_super_transfer *sxfer, int status)
1047 {
1048 	struct libusb_transfer *uxfer;
1049 	struct libusb_device *dev;
1050 
1051 	uxfer = (struct libusb_transfer *)(
1052 	    ((uint8_t *)sxfer) + sizeof(*sxfer));
1053 
1054 	if (pxfer != NULL)
1055 		libusb20_tr_set_priv_sc1(pxfer, NULL);
1056 
1057 	/* set transfer status */
1058 	uxfer->status = status;
1059 
1060 	/* update super transfer state */
1061 	sxfer->state = LIBUSB_SUPER_XFER_ST_NONE;
1062 
1063 	dev = libusb_get_device(uxfer->dev_handle);
1064 
1065 	TAILQ_INSERT_TAIL(&dev->ctx->tr_done, sxfer, entry);
1066 }
1067 
1068 /* This function must be called locked */
1069 
1070 static void
libusb10_isoc_proxy(struct libusb20_transfer * pxfer)1071 libusb10_isoc_proxy(struct libusb20_transfer *pxfer)
1072 {
1073 	struct libusb_super_transfer *sxfer;
1074 	struct libusb_transfer *uxfer;
1075 	uint32_t actlen;
1076 	uint16_t iso_packets;
1077 	uint16_t i;
1078 	uint8_t status;
1079 	uint8_t flags;
1080 
1081 	status = libusb20_tr_get_status(pxfer);
1082 	sxfer = libusb20_tr_get_priv_sc1(pxfer);
1083 	actlen = libusb20_tr_get_actual_length(pxfer);
1084 	iso_packets = libusb20_tr_get_max_frames(pxfer);
1085 
1086 	if (sxfer == NULL)
1087 		return;			/* cancelled - nothing to do */
1088 
1089 	uxfer = (struct libusb_transfer *)(
1090 	    ((uint8_t *)sxfer) + sizeof(*sxfer));
1091 
1092 	if (iso_packets > uxfer->num_iso_packets)
1093 		iso_packets = uxfer->num_iso_packets;
1094 
1095 	if (iso_packets == 0)
1096 		return;			/* nothing to do */
1097 
1098 	/* make sure that the number of ISOCHRONOUS packets is valid */
1099 	uxfer->num_iso_packets = iso_packets;
1100 
1101 	flags = uxfer->flags;
1102 
1103 	switch (status) {
1104 	case LIBUSB20_TRANSFER_COMPLETED:
1105 
1106 		/* update actual length */
1107 		uxfer->actual_length = actlen;
1108 		for (i = 0; i != iso_packets; i++) {
1109 			uxfer->iso_packet_desc[i].actual_length =
1110 			    libusb20_tr_get_length(pxfer, i);
1111 		}
1112 		libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1113 		break;
1114 
1115 	case LIBUSB20_TRANSFER_START:
1116 
1117 		/* setup length(s) */
1118 		actlen = 0;
1119 		for (i = 0; i != iso_packets; i++) {
1120 			libusb20_tr_setup_isoc(pxfer,
1121 			    &uxfer->buffer[actlen],
1122 			    uxfer->iso_packet_desc[i].length, i);
1123 			actlen += uxfer->iso_packet_desc[i].length;
1124 		}
1125 
1126 		/* no remainder */
1127 		sxfer->rem_len = 0;
1128 
1129 		libusb20_tr_set_total_frames(pxfer, iso_packets);
1130 		libusb20_tr_submit(pxfer);
1131 
1132 		/* fork another USB transfer, if any */
1133 		libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1134 		break;
1135 
1136 	default:
1137 		libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1138 		break;
1139 	}
1140 }
1141 
1142 /* This function must be called locked */
1143 
1144 static void
libusb10_bulk_intr_proxy(struct libusb20_transfer * pxfer)1145 libusb10_bulk_intr_proxy(struct libusb20_transfer *pxfer)
1146 {
1147 	struct libusb_super_transfer *sxfer;
1148 	struct libusb_transfer *uxfer;
1149 	uint32_t max_bulk;
1150 	uint32_t actlen;
1151 	uint8_t status;
1152 	uint8_t flags;
1153 
1154 	status = libusb20_tr_get_status(pxfer);
1155 	sxfer = libusb20_tr_get_priv_sc1(pxfer);
1156 	max_bulk = libusb20_tr_get_max_total_length(pxfer);
1157 	actlen = libusb20_tr_get_actual_length(pxfer);
1158 
1159 	if (sxfer == NULL)
1160 		return;			/* cancelled - nothing to do */
1161 
1162 	uxfer = (struct libusb_transfer *)(
1163 	    ((uint8_t *)sxfer) + sizeof(*sxfer));
1164 
1165 	flags = uxfer->flags;
1166 
1167 	switch (status) {
1168 	case LIBUSB20_TRANSFER_COMPLETED:
1169 
1170 		uxfer->actual_length += actlen;
1171 
1172 		/* check for short packet */
1173 		if (sxfer->last_len != actlen) {
1174 			if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1175 				libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1176 			} else {
1177 				libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1178 			}
1179 			break;
1180 		}
1181 		/* check for end of data */
1182 		if (sxfer->rem_len == 0) {
1183 			libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1184 			break;
1185 		}
1186 		/* FALLTHROUGH */
1187 
1188 	case LIBUSB20_TRANSFER_START:
1189 		if (max_bulk > sxfer->rem_len) {
1190 			max_bulk = sxfer->rem_len;
1191 		}
1192 		/* setup new BULK or INTERRUPT transaction */
1193 		libusb20_tr_setup_bulk(pxfer,
1194 		    sxfer->curr_data, max_bulk, uxfer->timeout);
1195 
1196 		/* update counters */
1197 		sxfer->last_len = max_bulk;
1198 		sxfer->curr_data += max_bulk;
1199 		sxfer->rem_len -= max_bulk;
1200 
1201 		libusb20_tr_submit(pxfer);
1202 
1203 		/* check if we can fork another USB transfer */
1204 		if (sxfer->rem_len == 0)
1205 			libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1206 		break;
1207 
1208 	default:
1209 		libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1210 		break;
1211 	}
1212 }
1213 
1214 /* This function must be called locked */
1215 
1216 static void
libusb10_ctrl_proxy(struct libusb20_transfer * pxfer)1217 libusb10_ctrl_proxy(struct libusb20_transfer *pxfer)
1218 {
1219 	struct libusb_super_transfer *sxfer;
1220 	struct libusb_transfer *uxfer;
1221 	uint32_t max_bulk;
1222 	uint32_t actlen;
1223 	uint8_t status;
1224 	uint8_t flags;
1225 
1226 	status = libusb20_tr_get_status(pxfer);
1227 	sxfer = libusb20_tr_get_priv_sc1(pxfer);
1228 	max_bulk = libusb20_tr_get_max_total_length(pxfer);
1229 	actlen = libusb20_tr_get_actual_length(pxfer);
1230 
1231 	if (sxfer == NULL)
1232 		return;			/* cancelled - nothing to do */
1233 
1234 	uxfer = (struct libusb_transfer *)(
1235 	    ((uint8_t *)sxfer) + sizeof(*sxfer));
1236 
1237 	flags = uxfer->flags;
1238 
1239 	switch (status) {
1240 	case LIBUSB20_TRANSFER_COMPLETED:
1241 
1242 		uxfer->actual_length += actlen;
1243 
1244 		/* subtract length of SETUP packet, if any */
1245 		actlen -= libusb20_tr_get_length(pxfer, 0);
1246 
1247 		/* check for short packet */
1248 		if (sxfer->last_len != actlen) {
1249 			if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) {
1250 				libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR);
1251 			} else {
1252 				libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1253 			}
1254 			break;
1255 		}
1256 		/* check for end of data */
1257 		if (sxfer->rem_len == 0) {
1258 			libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED);
1259 			break;
1260 		}
1261 		/* FALLTHROUGH */
1262 
1263 	case LIBUSB20_TRANSFER_START:
1264 		if (max_bulk > sxfer->rem_len) {
1265 			max_bulk = sxfer->rem_len;
1266 		}
1267 		/* setup new CONTROL transaction */
1268 		if (status == LIBUSB20_TRANSFER_COMPLETED) {
1269 			/* next fragment - don't send SETUP packet */
1270 			libusb20_tr_set_length(pxfer, 0, 0);
1271 		} else {
1272 			/* first fragment - send SETUP packet */
1273 			libusb20_tr_set_length(pxfer, 8, 0);
1274 			libusb20_tr_set_buffer(pxfer, uxfer->buffer, 0);
1275 		}
1276 
1277 		if (max_bulk != 0) {
1278 			libusb20_tr_set_length(pxfer, max_bulk, 1);
1279 			libusb20_tr_set_buffer(pxfer, sxfer->curr_data, 1);
1280 			libusb20_tr_set_total_frames(pxfer, 2);
1281 		} else {
1282 			libusb20_tr_set_total_frames(pxfer, 1);
1283 		}
1284 
1285 		/* update counters */
1286 		sxfer->last_len = max_bulk;
1287 		sxfer->curr_data += max_bulk;
1288 		sxfer->rem_len -= max_bulk;
1289 
1290 		libusb20_tr_submit(pxfer);
1291 
1292 		/* check if we can fork another USB transfer */
1293 		if (sxfer->rem_len == 0)
1294 			libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint);
1295 		break;
1296 
1297 	default:
1298 		libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status));
1299 		break;
1300 	}
1301 }
1302 
1303 /* The following function must be called locked */
1304 
1305 static void
libusb10_submit_transfer_sub(struct libusb20_device * pdev,uint8_t endpoint)1306 libusb10_submit_transfer_sub(struct libusb20_device *pdev, uint8_t endpoint)
1307 {
1308 	struct libusb20_transfer *pxfer0;
1309 	struct libusb20_transfer *pxfer1;
1310 	struct libusb_super_transfer *sxfer;
1311 	struct libusb_transfer *uxfer;
1312 	struct libusb_device *dev;
1313 	int err;
1314 	int buffsize;
1315 	int maxframe;
1316 	int temp;
1317 
1318 	dev = libusb_get_device(pdev);
1319 
1320 	pxfer0 = libusb10_get_transfer(pdev, endpoint, 0);
1321 	pxfer1 = libusb10_get_transfer(pdev, endpoint, 1);
1322 
1323 	if (pxfer0 == NULL || pxfer1 == NULL)
1324 		return;			/* shouldn't happen */
1325 
1326 	temp = 0;
1327 	if (libusb20_tr_pending(pxfer0))
1328 		temp |= 1;
1329 	if (libusb20_tr_pending(pxfer1))
1330 		temp |= 2;
1331 
1332 	switch (temp) {
1333 	case 3:
1334 		/* wait till one of the transfers complete */
1335 		return;
1336 	case 2:
1337 		sxfer = libusb20_tr_get_priv_sc1(pxfer1);
1338 		if (sxfer == NULL)
1339 			return;		/* cancelling */
1340 		if (sxfer->rem_len)
1341 			return;		/* cannot queue another one */
1342 		/* swap transfers */
1343 		pxfer1 = pxfer0;
1344 		break;
1345 	case 1:
1346 		sxfer = libusb20_tr_get_priv_sc1(pxfer0);
1347 		if (sxfer == NULL)
1348 			return;		/* cancelling */
1349 		if (sxfer->rem_len)
1350 			return;		/* cannot queue another one */
1351 		/* swap transfers */
1352 		pxfer0 = pxfer1;
1353 		break;
1354 	default:
1355 		break;
1356 	}
1357 
1358 	/* find next transfer on same endpoint */
1359 	TAILQ_FOREACH(sxfer, &dev->tr_head, entry) {
1360 
1361 		uxfer = (struct libusb_transfer *)(
1362 		    ((uint8_t *)sxfer) + sizeof(*sxfer));
1363 
1364 		if (uxfer->endpoint == endpoint) {
1365 			TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1366 			sxfer->entry.tqe_prev = NULL;
1367 			goto found;
1368 		}
1369 	}
1370 	return;				/* success */
1371 
1372 found:
1373 
1374 	libusb20_tr_set_priv_sc0(pxfer0, pdev);
1375 	libusb20_tr_set_priv_sc1(pxfer0, sxfer);
1376 
1377 	/* reset super transfer state */
1378 	sxfer->rem_len = uxfer->length;
1379 	sxfer->curr_data = uxfer->buffer;
1380 	uxfer->actual_length = 0;
1381 
1382 	switch (uxfer->type) {
1383 	case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
1384 		libusb20_tr_set_callback(pxfer0, libusb10_isoc_proxy);
1385 		break;
1386 	case LIBUSB_TRANSFER_TYPE_BULK:
1387 	case LIBUSB_TRANSFER_TYPE_INTERRUPT:
1388 		libusb20_tr_set_callback(pxfer0, libusb10_bulk_intr_proxy);
1389 		break;
1390 	case LIBUSB_TRANSFER_TYPE_CONTROL:
1391 		libusb20_tr_set_callback(pxfer0, libusb10_ctrl_proxy);
1392 		if (sxfer->rem_len < 8)
1393 			goto failure;
1394 
1395 		/* remove SETUP packet from data */
1396 		sxfer->rem_len -= 8;
1397 		sxfer->curr_data += 8;
1398 		break;
1399 	default:
1400 		goto failure;
1401 	}
1402 
1403 	buffsize = libusb10_get_buffsize(pdev, uxfer);
1404 	maxframe = libusb10_get_maxframe(pdev, uxfer);
1405 
1406 	/* make sure the transfer is opened */
1407 	err = libusb20_tr_open(pxfer0, buffsize, maxframe, endpoint);
1408 	if (err && (err != LIBUSB20_ERROR_BUSY)) {
1409 		goto failure;
1410 	}
1411 	libusb20_tr_start(pxfer0);
1412 	return;
1413 
1414 failure:
1415 	libusb10_complete_transfer(pxfer0, sxfer, LIBUSB_TRANSFER_ERROR);
1416 	/* make sure our event loop spins the done handler */
1417 	libusb10_wakeup_event_loop(dev->ctx);
1418 }
1419 
1420 /* The following function must be called unlocked */
1421 
1422 int
libusb_submit_transfer(struct libusb_transfer * uxfer)1423 libusb_submit_transfer(struct libusb_transfer *uxfer)
1424 {
1425 	struct libusb20_transfer *pxfer0;
1426 	struct libusb20_transfer *pxfer1;
1427 	struct libusb_super_transfer *sxfer;
1428 	struct libusb_device *dev;
1429 	uint32_t endpoint;
1430 	int err;
1431 
1432 	if (uxfer == NULL)
1433 		return (LIBUSB_ERROR_INVALID_PARAM);
1434 
1435 	if (uxfer->dev_handle == NULL)
1436 		return (LIBUSB_ERROR_INVALID_PARAM);
1437 
1438 	endpoint = uxfer->endpoint;
1439 
1440 	if (endpoint > 255)
1441 		return (LIBUSB_ERROR_INVALID_PARAM);
1442 
1443 	dev = libusb_get_device(uxfer->dev_handle);
1444 
1445 	DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer enter");
1446 
1447 	sxfer = (struct libusb_super_transfer *)(
1448 	    (uint8_t *)uxfer - sizeof(*sxfer));
1449 
1450 	CTX_LOCK(dev->ctx);
1451 
1452 	pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0);
1453 	pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1);
1454 
1455 	if (pxfer0 == NULL || pxfer1 == NULL) {
1456 		err = LIBUSB_ERROR_OTHER;
1457 	} else if ((sxfer->entry.tqe_prev != NULL) ||
1458 	    (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) ||
1459 	    (libusb20_tr_get_priv_sc1(pxfer1) == sxfer)) {
1460 		err = LIBUSB_ERROR_BUSY;
1461 	} else if (dev->device_is_gone != 0) {
1462 		err = LIBUSB_ERROR_NO_DEVICE;
1463 	} else {
1464 
1465 		/* set pending state */
1466 		sxfer->state = LIBUSB_SUPER_XFER_ST_PEND;
1467 
1468 		/* insert transfer into transfer head list */
1469 		TAILQ_INSERT_TAIL(&dev->tr_head, sxfer, entry);
1470 
1471 		/* start work transfers */
1472 		libusb10_submit_transfer_sub(
1473 		    uxfer->dev_handle, endpoint);
1474 
1475 		err = 0;		/* success */
1476 	}
1477 
1478 	CTX_UNLOCK(dev->ctx);
1479 
1480 	DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer leave %d", err);
1481 
1482 	return (err);
1483 }
1484 
1485 /* Asynchronous transfer cancel */
1486 
1487 int
libusb_cancel_transfer(struct libusb_transfer * uxfer)1488 libusb_cancel_transfer(struct libusb_transfer *uxfer)
1489 {
1490 	struct libusb20_transfer *pxfer0;
1491 	struct libusb20_transfer *pxfer1;
1492 	struct libusb_super_transfer *sxfer;
1493 	struct libusb_device *dev;
1494 	struct libusb_device_handle *devh;
1495 	uint32_t endpoint;
1496 	int retval;
1497 
1498 	if (uxfer == NULL)
1499 		return (LIBUSB_ERROR_INVALID_PARAM);
1500 
1501 	/* check if not initialised */
1502 	if ((devh = uxfer->dev_handle) == NULL)
1503 		return (LIBUSB_ERROR_NOT_FOUND);
1504 
1505 	endpoint = uxfer->endpoint;
1506 
1507 	if (endpoint > 255)
1508 		return (LIBUSB_ERROR_INVALID_PARAM);
1509 
1510 	dev = libusb_get_device(devh);
1511 
1512 	DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer enter");
1513 
1514 	sxfer = (struct libusb_super_transfer *)(
1515 	    (uint8_t *)uxfer - sizeof(*sxfer));
1516 
1517 	retval = 0;
1518 
1519 	CTX_LOCK(dev->ctx);
1520 
1521 	pxfer0 = libusb10_get_transfer(devh, endpoint, 0);
1522 	pxfer1 = libusb10_get_transfer(devh, endpoint, 1);
1523 
1524 	if (sxfer->state != LIBUSB_SUPER_XFER_ST_PEND) {
1525 		/* only update the transfer status */
1526 		uxfer->status = LIBUSB_TRANSFER_CANCELLED;
1527 		retval = LIBUSB_ERROR_NOT_FOUND;
1528 	} else if (sxfer->entry.tqe_prev != NULL) {
1529 		/* we are lucky - transfer is on a queue */
1530 		TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1531 		sxfer->entry.tqe_prev = NULL;
1532 		libusb10_complete_transfer(NULL,
1533 		    sxfer, LIBUSB_TRANSFER_CANCELLED);
1534 		/* make sure our event loop spins the done handler */
1535 		libusb10_wakeup_event_loop(dev->ctx);
1536 	} else if (pxfer0 == NULL || pxfer1 == NULL) {
1537 		/* not started */
1538 		retval = LIBUSB_ERROR_NOT_FOUND;
1539 	} else if (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) {
1540 		libusb10_complete_transfer(pxfer0,
1541 		    sxfer, LIBUSB_TRANSFER_CANCELLED);
1542 		if (dev->device_is_gone != 0) {
1543 			/* clear transfer pointer */
1544 			libusb20_tr_set_priv_sc1(pxfer0, NULL);
1545 			/* make sure our event loop spins the done handler */
1546 			libusb10_wakeup_event_loop(dev->ctx);
1547 		} else {
1548 			libusb20_tr_stop(pxfer0);
1549 			/* make sure the queue doesn't stall */
1550 			libusb10_submit_transfer_sub(devh, endpoint);
1551 		}
1552 	} else if (libusb20_tr_get_priv_sc1(pxfer1) == sxfer) {
1553 		libusb10_complete_transfer(pxfer1,
1554 		    sxfer, LIBUSB_TRANSFER_CANCELLED);
1555 		/* check if handle is still active */
1556 		if (dev->device_is_gone != 0) {
1557 			/* clear transfer pointer */
1558 			libusb20_tr_set_priv_sc1(pxfer1, NULL);
1559 			/* make sure our event loop spins the done handler */
1560 			libusb10_wakeup_event_loop(dev->ctx);
1561 		} else {
1562 			libusb20_tr_stop(pxfer1);
1563 			/* make sure the queue doesn't stall */
1564 			libusb10_submit_transfer_sub(devh, endpoint);
1565 		}
1566 	} else {
1567 		/* not started */
1568 		retval = LIBUSB_ERROR_NOT_FOUND;
1569 	}
1570 
1571 	CTX_UNLOCK(dev->ctx);
1572 
1573 	DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer leave");
1574 
1575 	return (retval);
1576 }
1577 
1578 UNEXPORTED void
libusb10_cancel_all_transfer(libusb_device * dev)1579 libusb10_cancel_all_transfer(libusb_device *dev)
1580 {
1581 	struct libusb20_device *pdev = dev->os_priv;
1582 	unsigned x;
1583 
1584 	for (x = 0; x != LIBUSB_NUM_SW_ENDPOINTS; x++) {
1585 		struct libusb20_transfer *xfer;
1586 
1587 		xfer = libusb20_tr_get_pointer(pdev, x);
1588 		if (xfer == NULL)
1589 			continue;
1590 		libusb20_tr_close(xfer);
1591 	}
1592 }
1593 
1594 UNEXPORTED void
libusb10_cancel_all_transfer_locked(struct libusb20_device * pdev,struct libusb_device * dev)1595 libusb10_cancel_all_transfer_locked(struct libusb20_device *pdev, struct libusb_device *dev)
1596 {
1597 	struct libusb_super_transfer *sxfer;
1598 	unsigned x;
1599 
1600 	for (x = 0; x != LIBUSB_NUM_SW_ENDPOINTS; x++) {
1601 		struct libusb20_transfer *xfer;
1602 
1603 		xfer = libusb20_tr_get_pointer(pdev, x);
1604 		if (xfer == NULL)
1605 			continue;
1606 		if (libusb20_tr_pending(xfer) == 0)
1607 			continue;
1608 		sxfer = libusb20_tr_get_priv_sc1(xfer);
1609 		if (sxfer == NULL)
1610 			continue;
1611 		/* complete pending transfer */
1612 		libusb10_complete_transfer(xfer, sxfer, LIBUSB_TRANSFER_ERROR);
1613 	}
1614 
1615 	while ((sxfer = TAILQ_FIRST(&dev->tr_head))) {
1616 		TAILQ_REMOVE(&dev->tr_head, sxfer, entry);
1617 
1618 		/* complete pending transfer */
1619 		libusb10_complete_transfer(NULL, sxfer, LIBUSB_TRANSFER_ERROR);
1620 	}
1621 }
1622 
1623 uint16_t
libusb_cpu_to_le16(uint16_t x)1624 libusb_cpu_to_le16(uint16_t x)
1625 {
1626 	return (htole16(x));
1627 }
1628 
1629 uint16_t
libusb_le16_to_cpu(uint16_t x)1630 libusb_le16_to_cpu(uint16_t x)
1631 {
1632 	return (le16toh(x));
1633 }
1634 
1635 const char *
libusb_strerror(int code)1636 libusb_strerror(int code)
1637 {
1638 	switch (code) {
1639 	case LIBUSB_SUCCESS:
1640 		return ("Success");
1641 	case LIBUSB_ERROR_IO:
1642 		return ("I/O error");
1643 	case LIBUSB_ERROR_INVALID_PARAM:
1644 		return ("Invalid parameter");
1645 	case LIBUSB_ERROR_ACCESS:
1646 		return ("Permissions error");
1647 	case LIBUSB_ERROR_NO_DEVICE:
1648 		return ("No device");
1649 	case LIBUSB_ERROR_NOT_FOUND:
1650 		return ("Not found");
1651 	case LIBUSB_ERROR_BUSY:
1652 		return ("Device busy");
1653 	case LIBUSB_ERROR_TIMEOUT:
1654 		return ("Timeout");
1655 	case LIBUSB_ERROR_OVERFLOW:
1656 		return ("Overflow");
1657 	case LIBUSB_ERROR_PIPE:
1658 		return ("Pipe error");
1659 	case LIBUSB_ERROR_INTERRUPTED:
1660 		return ("Interrupted");
1661 	case LIBUSB_ERROR_NO_MEM:
1662 		return ("Out of memory");
1663 	case LIBUSB_ERROR_NOT_SUPPORTED:
1664 		return ("Not supported");
1665 	case LIBUSB_ERROR_OTHER:
1666 		return ("Other error");
1667 	default:
1668 		return ("Unknown error");
1669 	}
1670 }
1671 
1672 const char *
libusb_error_name(int code)1673 libusb_error_name(int code)
1674 {
1675 	switch (code) {
1676 	case LIBUSB_SUCCESS:
1677 		return ("LIBUSB_SUCCESS");
1678 	case LIBUSB_ERROR_IO:
1679 		return ("LIBUSB_ERROR_IO");
1680 	case LIBUSB_ERROR_INVALID_PARAM:
1681 		return ("LIBUSB_ERROR_INVALID_PARAM");
1682 	case LIBUSB_ERROR_ACCESS:
1683 		return ("LIBUSB_ERROR_ACCESS");
1684 	case LIBUSB_ERROR_NO_DEVICE:
1685 		return ("LIBUSB_ERROR_NO_DEVICE");
1686 	case LIBUSB_ERROR_NOT_FOUND:
1687 		return ("LIBUSB_ERROR_NOT_FOUND");
1688 	case LIBUSB_ERROR_BUSY:
1689 		return ("LIBUSB_ERROR_BUSY");
1690 	case LIBUSB_ERROR_TIMEOUT:
1691 		return ("LIBUSB_ERROR_TIMEOUT");
1692 	case LIBUSB_ERROR_OVERFLOW:
1693 		return ("LIBUSB_ERROR_OVERFLOW");
1694 	case LIBUSB_ERROR_PIPE:
1695 		return ("LIBUSB_ERROR_PIPE");
1696 	case LIBUSB_ERROR_INTERRUPTED:
1697 		return ("LIBUSB_ERROR_INTERRUPTED");
1698 	case LIBUSB_ERROR_NO_MEM:
1699 		return ("LIBUSB_ERROR_NO_MEM");
1700 	case LIBUSB_ERROR_NOT_SUPPORTED:
1701 		return ("LIBUSB_ERROR_NOT_SUPPORTED");
1702 	case LIBUSB_ERROR_OTHER:
1703 		return ("LIBUSB_ERROR_OTHER");
1704 	default:
1705 		return ("LIBUSB_ERROR_UNKNOWN");
1706 	}
1707 }
1708 
1709 int
libusb_has_capability(uint32_t capability)1710 libusb_has_capability(uint32_t capability)
1711 {
1712 
1713 	switch (capability) {
1714 	case LIBUSB_CAP_HAS_CAPABILITY:
1715 	case LIBUSB_CAP_HAS_HOTPLUG:
1716 	case LIBUSB_CAP_HAS_HID_ACCESS:
1717 	case LIBUSB_CAP_SUPPORTS_DETACH_KERNEL_DRIVER:
1718 		return (1);
1719 	default:
1720 		return (0);
1721 	}
1722 }
1723