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