libusb10.c revision 248236
1194676Sthompsa/* $FreeBSD: head/lib/libusb/libusb10.c 248236 2013-03-13 12:23:14Z hselasky $ */ 2194676Sthompsa/*- 3194676Sthompsa * Copyright (c) 2009 Sylvestre Gallon. All rights reserved. 4195957Salfred * Copyright (c) 2009 Hans Petter Selasky. All rights reserved. 5194676Sthompsa * 6194676Sthompsa * Redistribution and use in source and binary forms, with or without 7194676Sthompsa * modification, are permitted provided that the following conditions 8194676Sthompsa * are met: 9194676Sthompsa * 1. Redistributions of source code must retain the above copyright 10194676Sthompsa * notice, this list of conditions and the following disclaimer. 11194676Sthompsa * 2. Redistributions in binary form must reproduce the above copyright 12194676Sthompsa * notice, this list of conditions and the following disclaimer in the 13194676Sthompsa * documentation and/or other materials provided with the distribution. 14194676Sthompsa * 15194676Sthompsa * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16194676Sthompsa * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17194676Sthompsa * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18194676Sthompsa * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19194676Sthompsa * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20194676Sthompsa * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21194676Sthompsa * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22194676Sthompsa * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23194676Sthompsa * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24194676Sthompsa * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25194676Sthompsa * SUCH DAMAGE. 26194676Sthompsa */ 27194676Sthompsa 28248236Shselasky#ifdef LIBUSB_GLOBAL_INCLUDE_FILE 29248236Shselasky#include LIBUSB_GLOBAL_INCLUDE_FILE 30248236Shselasky#else 31203774Swkoszek#include <assert.h> 32203815Swkoszek#include <errno.h> 33203815Swkoszek#include <poll.h> 34203815Swkoszek#include <pthread.h> 35203815Swkoszek#include <stdio.h> 36194676Sthompsa#include <stdlib.h> 37248236Shselasky#include <string.h> 38194676Sthompsa#include <unistd.h> 39248236Shselasky#include <time.h> 40248236Shselasky#include <sys/fcntl.h> 41248236Shselasky#include <sys/ioctl.h> 42248236Shselasky#include <sys/queue.h> 43248236Shselasky#include <sys/endian.h> 44248236Shselasky#endif 45194676Sthompsa 46208020Sthompsa#define libusb_device_handle libusb20_device 47208020Sthompsa 48194676Sthompsa#include "libusb20.h" 49194676Sthompsa#include "libusb20_desc.h" 50194676Sthompsa#include "libusb20_int.h" 51194676Sthompsa#include "libusb.h" 52194676Sthompsa#include "libusb10.h" 53194676Sthompsa 54194676Sthompsastatic pthread_mutex_t default_context_lock = PTHREAD_MUTEX_INITIALIZER; 55194676Sthompsastruct libusb_context *usbi_default_context = NULL; 56194676Sthompsa 57195957Salfred/* Prototypes */ 58195957Salfred 59195957Salfredstatic struct libusb20_transfer *libusb10_get_transfer(struct libusb20_device *, uint8_t, uint8_t); 60195957Salfredstatic int libusb10_get_buffsize(struct libusb20_device *, libusb_transfer *); 61195957Salfredstatic int libusb10_convert_error(uint8_t status); 62195957Salfredstatic void libusb10_complete_transfer(struct libusb20_transfer *, struct libusb_super_transfer *, int); 63195957Salfredstatic void libusb10_isoc_proxy(struct libusb20_transfer *); 64195957Salfredstatic void libusb10_bulk_intr_proxy(struct libusb20_transfer *); 65195957Salfredstatic void libusb10_ctrl_proxy(struct libusb20_transfer *); 66195957Salfredstatic void libusb10_submit_transfer_sub(struct libusb20_device *, uint8_t); 67195957Salfred 68194676Sthompsa/* Library initialisation / deinitialisation */ 69194676Sthompsa 70194676Sthompsavoid 71195957Salfredlibusb_set_debug(libusb_context *ctx, int level) 72194676Sthompsa{ 73195957Salfred ctx = GET_CONTEXT(ctx); 74194676Sthompsa if (ctx) 75194676Sthompsa ctx->debug = level; 76194676Sthompsa} 77194676Sthompsa 78213853Shselaskystatic void 79213853Shselaskylibusb_set_nonblocking(int f) 80213853Shselasky{ 81213853Shselasky int flags; 82213853Shselasky 83213853Shselasky /* 84213853Shselasky * We ignore any failures in this function, hence the 85213853Shselasky * non-blocking flag is not critical to the operation of 86213853Shselasky * libUSB. We use F_GETFL and F_SETFL to be compatible with 87213853Shselasky * Linux. 88213853Shselasky */ 89213853Shselasky 90213853Shselasky flags = fcntl(f, F_GETFL, NULL); 91213853Shselasky if (flags == -1) 92213853Shselasky return; 93213853Shselasky flags |= O_NONBLOCK; 94213853Shselasky fcntl(f, F_SETFL, flags); 95213853Shselasky} 96213853Shselasky 97194676Sthompsaint 98195957Salfredlibusb_init(libusb_context **context) 99194676Sthompsa{ 100194676Sthompsa struct libusb_context *ctx; 101236944Shselasky pthread_condattr_t attr; 102195957Salfred char *debug; 103194676Sthompsa int ret; 104194676Sthompsa 105194676Sthompsa ctx = malloc(sizeof(*ctx)); 106194676Sthompsa if (!ctx) 107194676Sthompsa return (LIBUSB_ERROR_INVALID_PARAM); 108194676Sthompsa 109194676Sthompsa memset(ctx, 0, sizeof(*ctx)); 110194676Sthompsa 111194676Sthompsa debug = getenv("LIBUSB_DEBUG"); 112194676Sthompsa if (debug != NULL) { 113194676Sthompsa ctx->debug = atoi(debug); 114194676Sthompsa if (ctx->debug != 0) 115194676Sthompsa ctx->debug_fixed = 1; 116194676Sthompsa } 117195957Salfred TAILQ_INIT(&ctx->pollfds); 118195957Salfred TAILQ_INIT(&ctx->tr_done); 119194676Sthompsa 120236944Shselasky if (pthread_mutex_init(&ctx->ctx_lock, NULL) != 0) { 121236944Shselasky free(ctx); 122236944Shselasky return (LIBUSB_ERROR_NO_MEM); 123236944Shselasky } 124236944Shselasky if (pthread_condattr_init(&attr) != 0) { 125236944Shselasky pthread_mutex_destroy(&ctx->ctx_lock); 126236944Shselasky free(ctx); 127236944Shselasky return (LIBUSB_ERROR_NO_MEM); 128236944Shselasky } 129236944Shselasky if (pthread_condattr_setclock(&attr, CLOCK_MONOTONIC) != 0) { 130236944Shselasky pthread_mutex_destroy(&ctx->ctx_lock); 131236944Shselasky pthread_condattr_destroy(&attr); 132236944Shselasky free(ctx); 133236944Shselasky return (LIBUSB_ERROR_OTHER); 134236944Shselasky } 135236944Shselasky if (pthread_cond_init(&ctx->ctx_cond, &attr) != 0) { 136236944Shselasky pthread_mutex_destroy(&ctx->ctx_lock); 137236944Shselasky pthread_condattr_destroy(&attr); 138236944Shselasky free(ctx); 139236944Shselasky return (LIBUSB_ERROR_NO_MEM); 140236944Shselasky } 141236944Shselasky pthread_condattr_destroy(&attr); 142194676Sthompsa 143195957Salfred ctx->ctx_handler = NO_THREAD; 144194676Sthompsa 145194676Sthompsa ret = pipe(ctx->ctrl_pipe); 146194676Sthompsa if (ret < 0) { 147195957Salfred pthread_mutex_destroy(&ctx->ctx_lock); 148195957Salfred pthread_cond_destroy(&ctx->ctx_cond); 149194676Sthompsa free(ctx); 150194676Sthompsa return (LIBUSB_ERROR_OTHER); 151194676Sthompsa } 152195957Salfred /* set non-blocking mode on the control pipe to avoid deadlock */ 153213853Shselasky libusb_set_nonblocking(ctx->ctrl_pipe[0]); 154213853Shselasky libusb_set_nonblocking(ctx->ctrl_pipe[1]); 155194676Sthompsa 156195957Salfred libusb10_add_pollfd(ctx, &ctx->ctx_poll, NULL, ctx->ctrl_pipe[0], POLLIN); 157194676Sthompsa 158194676Sthompsa pthread_mutex_lock(&default_context_lock); 159194676Sthompsa if (usbi_default_context == NULL) { 160194676Sthompsa usbi_default_context = ctx; 161194676Sthompsa } 162194676Sthompsa pthread_mutex_unlock(&default_context_lock); 163194676Sthompsa 164194676Sthompsa if (context) 165194676Sthompsa *context = ctx; 166194676Sthompsa 167195957Salfred DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_init complete"); 168195957Salfred 169194676Sthompsa return (0); 170194676Sthompsa} 171194676Sthompsa 172194676Sthompsavoid 173195957Salfredlibusb_exit(libusb_context *ctx) 174194676Sthompsa{ 175195957Salfred ctx = GET_CONTEXT(ctx); 176194676Sthompsa 177195957Salfred if (ctx == NULL) 178195957Salfred return; 179195957Salfred 180195957Salfred /* XXX cleanup devices */ 181195957Salfred 182195957Salfred libusb10_remove_pollfd(ctx, &ctx->ctx_poll); 183194676Sthompsa close(ctx->ctrl_pipe[0]); 184194676Sthompsa close(ctx->ctrl_pipe[1]); 185195957Salfred pthread_mutex_destroy(&ctx->ctx_lock); 186195957Salfred pthread_cond_destroy(&ctx->ctx_cond); 187194676Sthompsa 188194676Sthompsa pthread_mutex_lock(&default_context_lock); 189194676Sthompsa if (ctx == usbi_default_context) { 190194676Sthompsa usbi_default_context = NULL; 191194676Sthompsa } 192194676Sthompsa pthread_mutex_unlock(&default_context_lock); 193194676Sthompsa 194194676Sthompsa free(ctx); 195194676Sthompsa} 196194676Sthompsa 197194676Sthompsa/* Device handling and initialisation. */ 198194676Sthompsa 199194676Sthompsassize_t 200195957Salfredlibusb_get_device_list(libusb_context *ctx, libusb_device ***list) 201194676Sthompsa{ 202195957Salfred struct libusb20_backend *usb_backend; 203194676Sthompsa struct libusb20_device *pdev; 204194676Sthompsa struct libusb_device *dev; 205194676Sthompsa int i; 206194676Sthompsa 207195957Salfred ctx = GET_CONTEXT(ctx); 208194676Sthompsa 209195957Salfred if (ctx == NULL) 210195957Salfred return (LIBUSB_ERROR_INVALID_PARAM); 211195957Salfred 212195957Salfred if (list == NULL) 213195957Salfred return (LIBUSB_ERROR_INVALID_PARAM); 214195957Salfred 215194676Sthompsa usb_backend = libusb20_be_alloc_default(); 216194676Sthompsa if (usb_backend == NULL) 217195957Salfred return (LIBUSB_ERROR_NO_MEM); 218194676Sthompsa 219195957Salfred /* figure out how many USB devices are present */ 220194676Sthompsa pdev = NULL; 221194676Sthompsa i = 0; 222194676Sthompsa while ((pdev = libusb20_be_device_foreach(usb_backend, pdev))) 223194676Sthompsa i++; 224194676Sthompsa 225195957Salfred /* allocate device pointer list */ 226194676Sthompsa *list = malloc((i + 1) * sizeof(void *)); 227194676Sthompsa if (*list == NULL) { 228194676Sthompsa libusb20_be_free(usb_backend); 229194676Sthompsa return (LIBUSB_ERROR_NO_MEM); 230194676Sthompsa } 231195957Salfred /* create libusb v1.0 compliant devices */ 232194676Sthompsa i = 0; 233194676Sthompsa while ((pdev = libusb20_be_device_foreach(usb_backend, NULL))) { 234194676Sthompsa 235194676Sthompsa dev = malloc(sizeof(*dev)); 236194676Sthompsa if (dev == NULL) { 237195560Sthompsa while (i != 0) { 238195560Sthompsa libusb_unref_device((*list)[i - 1]); 239195560Sthompsa i--; 240195560Sthompsa } 241194676Sthompsa free(*list); 242195957Salfred *list = NULL; 243194676Sthompsa libusb20_be_free(usb_backend); 244194676Sthompsa return (LIBUSB_ERROR_NO_MEM); 245194676Sthompsa } 246199055Sthompsa /* get device into libUSB v1.0 list */ 247199055Sthompsa libusb20_be_dequeue_device(usb_backend, pdev); 248199055Sthompsa 249194676Sthompsa memset(dev, 0, sizeof(*dev)); 250194676Sthompsa 251195957Salfred /* init transfer queues */ 252195957Salfred TAILQ_INIT(&dev->tr_head); 253195957Salfred 254195957Salfred /* set context we belong to */ 255194676Sthompsa dev->ctx = ctx; 256194676Sthompsa 257194676Sthompsa /* link together the two structures */ 258194676Sthompsa dev->os_priv = pdev; 259195957Salfred pdev->privLuData = dev; 260194676Sthompsa 261194676Sthompsa (*list)[i] = libusb_ref_device(dev); 262194676Sthompsa i++; 263194676Sthompsa } 264194676Sthompsa (*list)[i] = NULL; 265194676Sthompsa 266194676Sthompsa libusb20_be_free(usb_backend); 267194676Sthompsa return (i); 268194676Sthompsa} 269194676Sthompsa 270194676Sthompsavoid 271194676Sthompsalibusb_free_device_list(libusb_device **list, int unref_devices) 272194676Sthompsa{ 273194676Sthompsa int i; 274194676Sthompsa 275194676Sthompsa if (list == NULL) 276195957Salfred return; /* be NULL safe */ 277194676Sthompsa 278194676Sthompsa if (unref_devices) { 279194676Sthompsa for (i = 0; list[i] != NULL; i++) 280194676Sthompsa libusb_unref_device(list[i]); 281194676Sthompsa } 282194676Sthompsa free(list); 283194676Sthompsa} 284194676Sthompsa 285194676Sthompsauint8_t 286195957Salfredlibusb_get_bus_number(libusb_device *dev) 287194676Sthompsa{ 288194676Sthompsa if (dev == NULL) 289195957Salfred return (0); /* should not happen */ 290195957Salfred return (libusb20_dev_get_bus_number(dev->os_priv)); 291194676Sthompsa} 292194676Sthompsa 293194676Sthompsauint8_t 294195957Salfredlibusb_get_device_address(libusb_device *dev) 295194676Sthompsa{ 296194676Sthompsa if (dev == NULL) 297195957Salfred return (0); /* should not happen */ 298195957Salfred return (libusb20_dev_get_address(dev->os_priv)); 299194676Sthompsa} 300194676Sthompsa 301224903Shselaskyenum libusb_speed 302224903Shselaskylibusb_get_device_speed(libusb_device *dev) 303224903Shselasky{ 304224903Shselasky if (dev == NULL) 305225035Shselasky return (LIBUSB_SPEED_UNKNOWN); /* should not happen */ 306224903Shselasky 307224903Shselasky switch (libusb20_dev_get_speed(dev->os_priv)) { 308224903Shselasky case LIBUSB20_SPEED_LOW: 309224903Shselasky return (LIBUSB_SPEED_LOW); 310224903Shselasky case LIBUSB20_SPEED_FULL: 311224903Shselasky return (LIBUSB_SPEED_FULL); 312224903Shselasky case LIBUSB20_SPEED_HIGH: 313224903Shselasky return (LIBUSB_SPEED_HIGH); 314224903Shselasky case LIBUSB20_SPEED_SUPER: 315224903Shselasky return (LIBUSB_SPEED_SUPER); 316224903Shselasky default: 317224903Shselasky break; 318224903Shselasky } 319224903Shselasky return (LIBUSB_SPEED_UNKNOWN); 320224903Shselasky} 321224903Shselasky 322194676Sthompsaint 323195957Salfredlibusb_get_max_packet_size(libusb_device *dev, uint8_t endpoint) 324194676Sthompsa{ 325194676Sthompsa struct libusb_config_descriptor *pdconf; 326194676Sthompsa struct libusb_interface *pinf; 327194676Sthompsa struct libusb_interface_descriptor *pdinf; 328194676Sthompsa struct libusb_endpoint_descriptor *pdend; 329195957Salfred int i; 330195957Salfred int j; 331195957Salfred int k; 332195957Salfred int ret; 333194676Sthompsa 334194676Sthompsa if (dev == NULL) 335194676Sthompsa return (LIBUSB_ERROR_NO_DEVICE); 336194676Sthompsa 337195957Salfred ret = libusb_get_active_config_descriptor(dev, &pdconf); 338195957Salfred if (ret < 0) 339195957Salfred return (ret); 340195957Salfred 341194676Sthompsa ret = LIBUSB_ERROR_NOT_FOUND; 342195957Salfred for (i = 0; i < pdconf->bNumInterfaces; i++) { 343194676Sthompsa pinf = &pdconf->interface[i]; 344195957Salfred for (j = 0; j < pinf->num_altsetting; j++) { 345194676Sthompsa pdinf = &pinf->altsetting[j]; 346195957Salfred for (k = 0; k < pdinf->bNumEndpoints; k++) { 347194676Sthompsa pdend = &pdinf->endpoint[k]; 348194676Sthompsa if (pdend->bEndpointAddress == endpoint) { 349194676Sthompsa ret = pdend->wMaxPacketSize; 350194676Sthompsa goto out; 351194676Sthompsa } 352194676Sthompsa } 353194676Sthompsa } 354194676Sthompsa } 355194676Sthompsa 356194676Sthompsaout: 357194676Sthompsa libusb_free_config_descriptor(pdconf); 358194676Sthompsa return (ret); 359194676Sthompsa} 360194676Sthompsa 361234193Shselaskyint 362234193Shselaskylibusb_get_max_iso_packet_size(libusb_device *dev, uint8_t endpoint) 363234193Shselasky{ 364234193Shselasky int multiplier; 365234193Shselasky int ret; 366234193Shselasky 367234193Shselasky ret = libusb_get_max_packet_size(dev, endpoint); 368234193Shselasky 369234193Shselasky switch (libusb20_dev_get_speed(dev->os_priv)) { 370234193Shselasky case LIBUSB20_SPEED_LOW: 371234193Shselasky case LIBUSB20_SPEED_FULL: 372234193Shselasky break; 373234193Shselasky default: 374234193Shselasky if (ret > -1) { 375234193Shselasky multiplier = (1 + ((ret >> 11) & 3)); 376234193Shselasky if (multiplier > 3) 377234193Shselasky multiplier = 3; 378234193Shselasky ret = (ret & 0x7FF) * multiplier; 379234193Shselasky } 380234193Shselasky break; 381234193Shselasky } 382234193Shselasky return (ret); 383234193Shselasky} 384234193Shselasky 385194676Sthompsalibusb_device * 386195957Salfredlibusb_ref_device(libusb_device *dev) 387194676Sthompsa{ 388194676Sthompsa if (dev == NULL) 389195957Salfred return (NULL); /* be NULL safe */ 390194676Sthompsa 391195957Salfred CTX_LOCK(dev->ctx); 392194676Sthompsa dev->refcnt++; 393195957Salfred CTX_UNLOCK(dev->ctx); 394194676Sthompsa 395194676Sthompsa return (dev); 396194676Sthompsa} 397194676Sthompsa 398194676Sthompsavoid 399195957Salfredlibusb_unref_device(libusb_device *dev) 400194676Sthompsa{ 401194676Sthompsa if (dev == NULL) 402195957Salfred return; /* be NULL safe */ 403194676Sthompsa 404195957Salfred CTX_LOCK(dev->ctx); 405194676Sthompsa dev->refcnt--; 406195957Salfred CTX_UNLOCK(dev->ctx); 407194676Sthompsa 408194676Sthompsa if (dev->refcnt == 0) { 409194676Sthompsa libusb20_dev_free(dev->os_priv); 410194676Sthompsa free(dev); 411194676Sthompsa } 412194676Sthompsa} 413194676Sthompsa 414194676Sthompsaint 415195957Salfredlibusb_open(libusb_device *dev, libusb_device_handle **devh) 416194676Sthompsa{ 417194676Sthompsa libusb_context *ctx = dev->ctx; 418194676Sthompsa struct libusb20_device *pdev = dev->os_priv; 419195957Salfred uint8_t dummy; 420194676Sthompsa int err; 421194676Sthompsa 422194676Sthompsa if (devh == NULL) 423194676Sthompsa return (LIBUSB_ERROR_INVALID_PARAM); 424194676Sthompsa 425195957Salfred /* set default device handle value */ 426195957Salfred *devh = NULL; 427194676Sthompsa 428195957Salfred dev = libusb_ref_device(dev); 429195957Salfred if (dev == NULL) 430195957Salfred return (LIBUSB_ERROR_INVALID_PARAM); 431195957Salfred 432194676Sthompsa err = libusb20_dev_open(pdev, 16 * 4 /* number of endpoints */ ); 433194676Sthompsa if (err) { 434195957Salfred libusb_unref_device(dev); 435194676Sthompsa return (LIBUSB_ERROR_NO_MEM); 436194676Sthompsa } 437195957Salfred libusb10_add_pollfd(ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN | 438194676Sthompsa POLLOUT | POLLRDNORM | POLLWRNORM); 439194676Sthompsa 440195957Salfred /* make sure our event loop detects the new device */ 441195957Salfred dummy = 0; 442194676Sthompsa err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy)); 443213853Shselasky if (err < (int)sizeof(dummy)) { 444195957Salfred /* ignore error, if any */ 445195957Salfred DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open write failed!"); 446194676Sthompsa } 447195957Salfred *devh = pdev; 448194676Sthompsa 449194676Sthompsa return (0); 450194676Sthompsa} 451194676Sthompsa 452194676Sthompsalibusb_device_handle * 453195957Salfredlibusb_open_device_with_vid_pid(libusb_context *ctx, uint16_t vendor_id, 454194676Sthompsa uint16_t product_id) 455194676Sthompsa{ 456194676Sthompsa struct libusb_device **devs; 457194676Sthompsa struct libusb20_device *pdev; 458194676Sthompsa struct LIBUSB20_DEVICE_DESC_DECODED *pdesc; 459195957Salfred int i; 460195957Salfred int j; 461194676Sthompsa 462195957Salfred ctx = GET_CONTEXT(ctx); 463195957Salfred if (ctx == NULL) 464195957Salfred return (NULL); /* be NULL safe */ 465195957Salfred 466195560Sthompsa DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid enter"); 467194676Sthompsa 468194676Sthompsa if ((i = libusb_get_device_list(ctx, &devs)) < 0) 469194676Sthompsa return (NULL); 470194676Sthompsa 471228236Shselasky pdev = NULL; 472194676Sthompsa for (j = 0; j < i; j++) { 473228236Shselasky struct libusb20_device *tdev; 474228236Shselasky 475228236Shselasky tdev = devs[j]->os_priv; 476228236Shselasky pdesc = libusb20_dev_get_device_desc(tdev); 477195957Salfred /* 478195957Salfred * NOTE: The USB library will automatically swap the 479195957Salfred * fields in the device descriptor to be of host 480195957Salfred * endian type! 481195957Salfred */ 482194676Sthompsa if (pdesc->idVendor == vendor_id && 483195560Sthompsa pdesc->idProduct == product_id) { 484228235Shselasky libusb_open(devs[j], &pdev); 485195957Salfred break; 486195560Sthompsa } 487194676Sthompsa } 488194676Sthompsa 489194676Sthompsa libusb_free_device_list(devs, 1); 490195560Sthompsa DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_open_device_width_vid_pid leave"); 491195957Salfred return (pdev); 492194676Sthompsa} 493194676Sthompsa 494194676Sthompsavoid 495195957Salfredlibusb_close(struct libusb20_device *pdev) 496194676Sthompsa{ 497194676Sthompsa libusb_context *ctx; 498195957Salfred struct libusb_device *dev; 499195957Salfred uint8_t dummy; 500194676Sthompsa int err; 501194676Sthompsa 502195957Salfred if (pdev == NULL) 503195957Salfred return; /* be NULL safe */ 504194676Sthompsa 505195957Salfred dev = libusb_get_device(pdev); 506195957Salfred ctx = dev->ctx; 507194676Sthompsa 508195957Salfred libusb10_remove_pollfd(ctx, &dev->dev_poll); 509194676Sthompsa 510195957Salfred libusb20_dev_close(pdev); 511199055Sthompsa 512199055Sthompsa /* unref will free the "pdev" when the refcount reaches zero */ 513195957Salfred libusb_unref_device(dev); 514194676Sthompsa 515195957Salfred /* make sure our event loop detects the closed device */ 516195957Salfred dummy = 0; 517194676Sthompsa err = write(ctx->ctrl_pipe[1], &dummy, sizeof(dummy)); 518213853Shselasky if (err < (int)sizeof(dummy)) { 519195957Salfred /* ignore error, if any */ 520195957Salfred DPRINTF(ctx, LIBUSB_DEBUG_FUNCTION, "libusb_close write failed!"); 521194676Sthompsa } 522194676Sthompsa} 523194676Sthompsa 524194676Sthompsalibusb_device * 525195957Salfredlibusb_get_device(struct libusb20_device *pdev) 526194676Sthompsa{ 527195957Salfred if (pdev == NULL) 528194676Sthompsa return (NULL); 529195957Salfred return ((libusb_device *)pdev->privLuData); 530194676Sthompsa} 531194676Sthompsa 532194676Sthompsaint 533195957Salfredlibusb_get_configuration(struct libusb20_device *pdev, int *config) 534194676Sthompsa{ 535195957Salfred struct libusb20_config *pconf; 536194676Sthompsa 537195957Salfred if (pdev == NULL || config == NULL) 538194676Sthompsa return (LIBUSB_ERROR_INVALID_PARAM); 539194676Sthompsa 540195957Salfred pconf = libusb20_dev_alloc_config(pdev, libusb20_dev_get_config_index(pdev)); 541195957Salfred if (pconf == NULL) 542195957Salfred return (LIBUSB_ERROR_NO_MEM); 543194676Sthompsa 544195957Salfred *config = pconf->desc.bConfigurationValue; 545195957Salfred 546195957Salfred free(pconf); 547195957Salfred 548194676Sthompsa return (0); 549194676Sthompsa} 550194676Sthompsa 551194676Sthompsaint 552195957Salfredlibusb_set_configuration(struct libusb20_device *pdev, int configuration) 553194676Sthompsa{ 554195957Salfred struct libusb20_config *pconf; 555195957Salfred struct libusb_device *dev; 556195957Salfred int err; 557195957Salfred uint8_t i; 558194676Sthompsa 559195957Salfred dev = libusb_get_device(pdev); 560195957Salfred if (dev == NULL) 561194676Sthompsa return (LIBUSB_ERROR_INVALID_PARAM); 562194676Sthompsa 563195957Salfred if (configuration < 1) { 564195957Salfred /* unconfigure */ 565195957Salfred i = 255; 566195957Salfred } else { 567195957Salfred for (i = 0; i != 255; i++) { 568195957Salfred uint8_t found; 569194676Sthompsa 570195957Salfred pconf = libusb20_dev_alloc_config(pdev, i); 571195957Salfred if (pconf == NULL) 572195957Salfred return (LIBUSB_ERROR_INVALID_PARAM); 573195957Salfred found = (pconf->desc.bConfigurationValue 574195957Salfred == configuration); 575195957Salfred free(pconf); 576195957Salfred 577195957Salfred if (found) 578195957Salfred goto set_config; 579195957Salfred } 580195957Salfred return (LIBUSB_ERROR_INVALID_PARAM); 581195957Salfred } 582195957Salfred 583195957Salfredset_config: 584195957Salfred 585195957Salfred libusb10_cancel_all_transfer(dev); 586195957Salfred 587195957Salfred libusb10_remove_pollfd(dev->ctx, &dev->dev_poll); 588195957Salfred 589195957Salfred err = libusb20_dev_set_config_index(pdev, i); 590195957Salfred 591195957Salfred libusb10_add_pollfd(dev->ctx, &dev->dev_poll, pdev, libusb20_dev_get_fd(pdev), POLLIN | 592195957Salfred POLLOUT | POLLRDNORM | POLLWRNORM); 593195957Salfred 594195957Salfred return (err ? LIBUSB_ERROR_INVALID_PARAM : 0); 595194676Sthompsa} 596194676Sthompsa 597194676Sthompsaint 598195957Salfredlibusb_claim_interface(struct libusb20_device *pdev, int interface_number) 599194676Sthompsa{ 600195957Salfred libusb_device *dev; 601195957Salfred int err = 0; 602194676Sthompsa 603195957Salfred dev = libusb_get_device(pdev); 604194676Sthompsa if (dev == NULL) 605194676Sthompsa return (LIBUSB_ERROR_INVALID_PARAM); 606194676Sthompsa 607195957Salfred if (interface_number < 0 || interface_number > 31) 608194676Sthompsa return (LIBUSB_ERROR_INVALID_PARAM); 609194676Sthompsa 610195957Salfred CTX_LOCK(dev->ctx); 611194676Sthompsa if (dev->claimed_interfaces & (1 << interface_number)) 612195957Salfred err = LIBUSB_ERROR_BUSY; 613194676Sthompsa 614195957Salfred if (!err) 615194676Sthompsa dev->claimed_interfaces |= (1 << interface_number); 616195957Salfred CTX_UNLOCK(dev->ctx); 617195957Salfred return (err); 618194676Sthompsa} 619194676Sthompsa 620194676Sthompsaint 621195957Salfredlibusb_release_interface(struct libusb20_device *pdev, int interface_number) 622194676Sthompsa{ 623195957Salfred libusb_device *dev; 624195957Salfred int err = 0; 625194676Sthompsa 626195957Salfred dev = libusb_get_device(pdev); 627194676Sthompsa if (dev == NULL) 628194676Sthompsa return (LIBUSB_ERROR_INVALID_PARAM); 629194676Sthompsa 630195957Salfred if (interface_number < 0 || interface_number > 31) 631194676Sthompsa return (LIBUSB_ERROR_INVALID_PARAM); 632194676Sthompsa 633195957Salfred CTX_LOCK(dev->ctx); 634194676Sthompsa if (!(dev->claimed_interfaces & (1 << interface_number))) 635195957Salfred err = LIBUSB_ERROR_NOT_FOUND; 636194676Sthompsa 637195957Salfred if (!err) 638194676Sthompsa dev->claimed_interfaces &= ~(1 << interface_number); 639195957Salfred CTX_UNLOCK(dev->ctx); 640195957Salfred return (err); 641194676Sthompsa} 642194676Sthompsa 643194676Sthompsaint 644195957Salfredlibusb_set_interface_alt_setting(struct libusb20_device *pdev, 645194676Sthompsa int interface_number, int alternate_setting) 646194676Sthompsa{ 647195957Salfred libusb_device *dev; 648195957Salfred int err = 0; 649194676Sthompsa 650195957Salfred dev = libusb_get_device(pdev); 651194676Sthompsa if (dev == NULL) 652194676Sthompsa return (LIBUSB_ERROR_INVALID_PARAM); 653194676Sthompsa 654195957Salfred if (interface_number < 0 || interface_number > 31) 655194676Sthompsa return (LIBUSB_ERROR_INVALID_PARAM); 656194676Sthompsa 657195957Salfred CTX_LOCK(dev->ctx); 658195957Salfred if (!(dev->claimed_interfaces & (1 << interface_number))) 659195957Salfred err = LIBUSB_ERROR_NOT_FOUND; 660195957Salfred CTX_UNLOCK(dev->ctx); 661194676Sthompsa 662195957Salfred if (err) 663195957Salfred return (err); 664195957Salfred 665195957Salfred libusb10_cancel_all_transfer(dev); 666195957Salfred 667195957Salfred libusb10_remove_pollfd(dev->ctx, &dev->dev_poll); 668195957Salfred 669195957Salfred err = libusb20_dev_set_alt_index(pdev, 670195957Salfred interface_number, alternate_setting); 671195957Salfred 672195957Salfred libusb10_add_pollfd(dev->ctx, &dev->dev_poll, 673195957Salfred pdev, libusb20_dev_get_fd(pdev), 674195957Salfred POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM); 675195957Salfred 676195957Salfred return (err ? LIBUSB_ERROR_OTHER : 0); 677194676Sthompsa} 678194676Sthompsa 679195957Salfredstatic struct libusb20_transfer * 680195957Salfredlibusb10_get_transfer(struct libusb20_device *pdev, 681234491Shselasky uint8_t endpoint, uint8_t xfer_index) 682195957Salfred{ 683234491Shselasky xfer_index &= 1; /* double buffering */ 684195957Salfred 685234491Shselasky xfer_index |= (endpoint & LIBUSB20_ENDPOINT_ADDRESS_MASK) * 4; 686195957Salfred 687195957Salfred if (endpoint & LIBUSB20_ENDPOINT_DIR_MASK) { 688195957Salfred /* this is an IN endpoint */ 689234491Shselasky xfer_index |= 2; 690195957Salfred } 691234491Shselasky return (libusb20_tr_get_pointer(pdev, xfer_index)); 692195957Salfred} 693195957Salfred 694194676Sthompsaint 695195957Salfredlibusb_clear_halt(struct libusb20_device *pdev, uint8_t endpoint) 696194676Sthompsa{ 697194676Sthompsa struct libusb20_transfer *xfer; 698195957Salfred struct libusb_device *dev; 699195957Salfred int err; 700194676Sthompsa 701195957Salfred xfer = libusb10_get_transfer(pdev, endpoint, 0); 702195560Sthompsa if (xfer == NULL) 703195957Salfred return (LIBUSB_ERROR_INVALID_PARAM); 704194676Sthompsa 705195957Salfred dev = libusb_get_device(pdev); 706213853Shselasky if (dev == NULL) 707213853Shselasky return (LIBUSB_ERROR_INVALID_PARAM); 708195957Salfred 709195957Salfred CTX_LOCK(dev->ctx); 710223642Shselasky err = libusb20_tr_open(xfer, 0, 1, endpoint); 711195957Salfred CTX_UNLOCK(dev->ctx); 712195957Salfred 713195957Salfred if (err != 0 && err != LIBUSB20_ERROR_BUSY) 714194676Sthompsa return (LIBUSB_ERROR_OTHER); 715194676Sthompsa 716194676Sthompsa libusb20_tr_clear_stall_sync(xfer); 717195957Salfred 718195957Salfred /* check if we opened the transfer */ 719195957Salfred if (err == 0) { 720195957Salfred CTX_LOCK(dev->ctx); 721194676Sthompsa libusb20_tr_close(xfer); 722195957Salfred CTX_UNLOCK(dev->ctx); 723195957Salfred } 724195957Salfred return (0); /* success */ 725194676Sthompsa} 726194676Sthompsa 727194676Sthompsaint 728195957Salfredlibusb_reset_device(struct libusb20_device *pdev) 729194676Sthompsa{ 730195957Salfred libusb_device *dev; 731195957Salfred int err; 732194676Sthompsa 733195957Salfred dev = libusb_get_device(pdev); 734194676Sthompsa if (dev == NULL) 735213853Shselasky return (LIBUSB_ERROR_INVALID_PARAM); 736194676Sthompsa 737195957Salfred libusb10_cancel_all_transfer(dev); 738195957Salfred 739195957Salfred libusb10_remove_pollfd(dev->ctx, &dev->dev_poll); 740195957Salfred 741195957Salfred err = libusb20_dev_reset(pdev); 742195957Salfred 743195957Salfred libusb10_add_pollfd(dev->ctx, &dev->dev_poll, 744195957Salfred pdev, libusb20_dev_get_fd(pdev), 745195957Salfred POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM); 746195957Salfred 747195957Salfred return (err ? LIBUSB_ERROR_OTHER : 0); 748194676Sthompsa} 749194676Sthompsa 750194676Sthompsaint 751213848Shselaskylibusb_check_connected(struct libusb20_device *pdev) 752213848Shselasky{ 753213848Shselasky libusb_device *dev; 754213848Shselasky int err; 755213848Shselasky 756213848Shselasky dev = libusb_get_device(pdev); 757213848Shselasky if (dev == NULL) 758213848Shselasky return (LIBUSB_ERROR_INVALID_PARAM); 759213848Shselasky 760213848Shselasky err = libusb20_dev_check_connected(pdev); 761213848Shselasky 762213848Shselasky return (err ? LIBUSB_ERROR_NO_DEVICE : 0); 763213848Shselasky} 764213848Shselasky 765213848Shselaskyint 766195957Salfredlibusb_kernel_driver_active(struct libusb20_device *pdev, int interface) 767194676Sthompsa{ 768195957Salfred if (pdev == NULL) 769194676Sthompsa return (LIBUSB_ERROR_INVALID_PARAM); 770194676Sthompsa 771226220Shselasky if (libusb20_dev_kernel_driver_active(pdev, interface)) 772226220Shselasky return (0); /* no kernel driver is active */ 773226220Shselasky else 774226220Shselasky return (1); /* kernel driver is active */ 775194676Sthompsa} 776194676Sthompsa 777194676Sthompsaint 778213853Shselaskylibusb_get_driver_np(struct libusb20_device *pdev, int interface, 779213853Shselasky char *name, int namelen) 780213853Shselasky{ 781213853Shselasky return (libusb_get_driver(pdev, interface, name, namelen)); 782213853Shselasky} 783213853Shselasky 784213853Shselaskyint 785213853Shselaskylibusb_get_driver(struct libusb20_device *pdev, int interface, 786213853Shselasky char *name, int namelen) 787213853Shselasky{ 788213853Shselasky char *ptr; 789213853Shselasky int err; 790213853Shselasky 791213853Shselasky if (pdev == NULL) 792213853Shselasky return (LIBUSB_ERROR_INVALID_PARAM); 793213853Shselasky if (namelen < 1) 794213853Shselasky return (LIBUSB_ERROR_INVALID_PARAM); 795224085Shselasky if (namelen > 255) 796224085Shselasky namelen = 255; 797213853Shselasky 798213853Shselasky err = libusb20_dev_get_iface_desc( 799213853Shselasky pdev, interface, name, namelen); 800213853Shselasky 801213853Shselasky if (err != 0) 802213853Shselasky return (LIBUSB_ERROR_OTHER); 803213853Shselasky 804213853Shselasky /* we only want the driver name */ 805213853Shselasky ptr = strstr(name, ":"); 806213853Shselasky if (ptr != NULL) 807213853Shselasky *ptr = 0; 808213853Shselasky 809213853Shselasky return (0); 810213853Shselasky} 811213853Shselasky 812213853Shselaskyint 813213853Shselaskylibusb_detach_kernel_driver_np(struct libusb20_device *pdev, int interface) 814213853Shselasky{ 815213853Shselasky return (libusb_detach_kernel_driver(pdev, interface)); 816213853Shselasky} 817213853Shselasky 818213853Shselaskyint 819195957Salfredlibusb_detach_kernel_driver(struct libusb20_device *pdev, int interface) 820194676Sthompsa{ 821195957Salfred int err; 822194676Sthompsa 823195957Salfred if (pdev == NULL) 824194676Sthompsa return (LIBUSB_ERROR_INVALID_PARAM); 825194676Sthompsa 826195957Salfred err = libusb20_dev_detach_kernel_driver( 827195957Salfred pdev, interface); 828194676Sthompsa 829213853Shselasky return (err ? LIBUSB_ERROR_OTHER : 0); 830194676Sthompsa} 831194676Sthompsa 832194676Sthompsaint 833195957Salfredlibusb_attach_kernel_driver(struct libusb20_device *pdev, int interface) 834194676Sthompsa{ 835195957Salfred if (pdev == NULL) 836194676Sthompsa return (LIBUSB_ERROR_INVALID_PARAM); 837195957Salfred /* stub - currently not supported by libusb20 */ 838194676Sthompsa return (0); 839194676Sthompsa} 840194676Sthompsa 841194676Sthompsa/* Asynchronous device I/O */ 842194676Sthompsa 843194676Sthompsastruct libusb_transfer * 844194676Sthompsalibusb_alloc_transfer(int iso_packets) 845194676Sthompsa{ 846195957Salfred struct libusb_transfer *uxfer; 847195957Salfred struct libusb_super_transfer *sxfer; 848194676Sthompsa int len; 849194676Sthompsa 850194676Sthompsa len = sizeof(struct libusb_transfer) + 851195957Salfred sizeof(struct libusb_super_transfer) + 852194676Sthompsa (iso_packets * sizeof(libusb_iso_packet_descriptor)); 853194676Sthompsa 854195957Salfred sxfer = malloc(len); 855195957Salfred if (sxfer == NULL) 856194676Sthompsa return (NULL); 857194676Sthompsa 858195957Salfred memset(sxfer, 0, len); 859194676Sthompsa 860195957Salfred uxfer = (struct libusb_transfer *)( 861195957Salfred ((uint8_t *)sxfer) + sizeof(*sxfer)); 862194676Sthompsa 863195957Salfred /* set default value */ 864195957Salfred uxfer->num_iso_packets = iso_packets; 865195957Salfred 866195957Salfred return (uxfer); 867194676Sthompsa} 868194676Sthompsa 869194676Sthompsavoid 870195957Salfredlibusb_free_transfer(struct libusb_transfer *uxfer) 871194676Sthompsa{ 872195957Salfred struct libusb_super_transfer *sxfer; 873194676Sthompsa 874195957Salfred if (uxfer == NULL) 875195957Salfred return; /* be NULL safe */ 876194676Sthompsa 877215253Shselasky /* check if we should free the transfer buffer */ 878215253Shselasky if (uxfer->flags & LIBUSB_TRANSFER_FREE_BUFFER) 879215253Shselasky free(uxfer->buffer); 880215253Shselasky 881195957Salfred sxfer = (struct libusb_super_transfer *)( 882195957Salfred (uint8_t *)uxfer - sizeof(*sxfer)); 883194676Sthompsa 884195957Salfred free(sxfer); 885194676Sthompsa} 886194676Sthompsa 887219100Shselaskystatic uint32_t 888195957Salfredlibusb10_get_maxframe(struct libusb20_device *pdev, libusb_transfer *xfer) 889195560Sthompsa{ 890219100Shselasky uint32_t ret; 891195560Sthompsa 892195560Sthompsa switch (xfer->type) { 893195560Sthompsa case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS: 894219100Shselasky ret = 60 | LIBUSB20_MAX_FRAME_PRE_SCALE; /* 60ms */ 895195957Salfred break; 896195560Sthompsa case LIBUSB_TRANSFER_TYPE_CONTROL: 897195560Sthompsa ret = 2; 898195957Salfred break; 899195560Sthompsa default: 900195560Sthompsa ret = 1; 901195957Salfred break; 902195560Sthompsa } 903195957Salfred return (ret); 904195560Sthompsa} 905195560Sthompsa 906195560Sthompsastatic int 907195957Salfredlibusb10_get_buffsize(struct libusb20_device *pdev, libusb_transfer *xfer) 908195560Sthompsa{ 909195560Sthompsa int ret; 910195560Sthompsa int usb_speed; 911195560Sthompsa 912195560Sthompsa usb_speed = libusb20_dev_get_speed(pdev); 913195560Sthompsa 914195560Sthompsa switch (xfer->type) { 915195560Sthompsa case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS: 916195957Salfred ret = 0; /* kernel will auto-select */ 917195957Salfred break; 918195560Sthompsa case LIBUSB_TRANSFER_TYPE_CONTROL: 919195957Salfred ret = 1024; 920195957Salfred break; 921195957Salfred default: 922195560Sthompsa switch (usb_speed) { 923195957Salfred case LIBUSB20_SPEED_LOW: 924195957Salfred ret = 256; 925195957Salfred break; 926195957Salfred case LIBUSB20_SPEED_FULL: 927195957Salfred ret = 4096; 928195957Salfred break; 929195957Salfred default: 930195957Salfred ret = 16384; 931195957Salfred break; 932195560Sthompsa } 933195957Salfred break; 934195560Sthompsa } 935195957Salfred return (ret); 936195560Sthompsa} 937195560Sthompsa 938195957Salfredstatic int 939195957Salfredlibusb10_convert_error(uint8_t status) 940195957Salfred{ 941195957Salfred ; /* indent fix */ 942195957Salfred 943195957Salfred switch (status) { 944195957Salfred case LIBUSB20_TRANSFER_START: 945195957Salfred case LIBUSB20_TRANSFER_COMPLETED: 946195957Salfred return (LIBUSB_TRANSFER_COMPLETED); 947195957Salfred case LIBUSB20_TRANSFER_OVERFLOW: 948195957Salfred return (LIBUSB_TRANSFER_OVERFLOW); 949195957Salfred case LIBUSB20_TRANSFER_NO_DEVICE: 950195957Salfred return (LIBUSB_TRANSFER_NO_DEVICE); 951195957Salfred case LIBUSB20_TRANSFER_STALL: 952195957Salfred return (LIBUSB_TRANSFER_STALL); 953195957Salfred case LIBUSB20_TRANSFER_CANCELLED: 954195957Salfred return (LIBUSB_TRANSFER_CANCELLED); 955195957Salfred case LIBUSB20_TRANSFER_TIMED_OUT: 956195957Salfred return (LIBUSB_TRANSFER_TIMED_OUT); 957195957Salfred default: 958195957Salfred return (LIBUSB_TRANSFER_ERROR); 959195957Salfred } 960195957Salfred} 961195957Salfred 962195957Salfred/* This function must be called locked */ 963195957Salfred 964194676Sthompsastatic void 965195957Salfredlibusb10_complete_transfer(struct libusb20_transfer *pxfer, 966195957Salfred struct libusb_super_transfer *sxfer, int status) 967194676Sthompsa{ 968195957Salfred struct libusb_transfer *uxfer; 969195957Salfred struct libusb_device *dev; 970195957Salfred 971195957Salfred uxfer = (struct libusb_transfer *)( 972195957Salfred ((uint8_t *)sxfer) + sizeof(*sxfer)); 973195957Salfred 974195957Salfred if (pxfer != NULL) 975195957Salfred libusb20_tr_set_priv_sc1(pxfer, NULL); 976195957Salfred 977199575Sthompsa /* set transfer status */ 978195957Salfred uxfer->status = status; 979195957Salfred 980199575Sthompsa /* update super transfer state */ 981199575Sthompsa sxfer->state = LIBUSB_SUPER_XFER_ST_NONE; 982199575Sthompsa 983195957Salfred dev = libusb_get_device(uxfer->dev_handle); 984195957Salfred 985195957Salfred TAILQ_INSERT_TAIL(&dev->ctx->tr_done, sxfer, entry); 986195957Salfred} 987195957Salfred 988195957Salfred/* This function must be called locked */ 989195957Salfred 990195957Salfredstatic void 991195957Salfredlibusb10_isoc_proxy(struct libusb20_transfer *pxfer) 992195957Salfred{ 993195957Salfred struct libusb_super_transfer *sxfer; 994195957Salfred struct libusb_transfer *uxfer; 995195957Salfred uint32_t actlen; 996195957Salfred uint16_t iso_packets; 997195957Salfred uint16_t i; 998194676Sthompsa uint8_t status; 999195957Salfred uint8_t flags; 1000194676Sthompsa 1001195957Salfred status = libusb20_tr_get_status(pxfer); 1002195957Salfred sxfer = libusb20_tr_get_priv_sc1(pxfer); 1003195957Salfred actlen = libusb20_tr_get_actual_length(pxfer); 1004195957Salfred iso_packets = libusb20_tr_get_max_frames(pxfer); 1005194676Sthompsa 1006195957Salfred if (sxfer == NULL) 1007195957Salfred return; /* cancelled - nothing to do */ 1008195957Salfred 1009195957Salfred uxfer = (struct libusb_transfer *)( 1010195957Salfred ((uint8_t *)sxfer) + sizeof(*sxfer)); 1011195957Salfred 1012195957Salfred if (iso_packets > uxfer->num_iso_packets) 1013195957Salfred iso_packets = uxfer->num_iso_packets; 1014195957Salfred 1015195957Salfred if (iso_packets == 0) 1016195957Salfred return; /* nothing to do */ 1017195957Salfred 1018195957Salfred /* make sure that the number of ISOCHRONOUS packets is valid */ 1019195957Salfred uxfer->num_iso_packets = iso_packets; 1020195957Salfred 1021195957Salfred flags = uxfer->flags; 1022195957Salfred 1023194676Sthompsa switch (status) { 1024194676Sthompsa case LIBUSB20_TRANSFER_COMPLETED: 1025195560Sthompsa 1026195957Salfred /* update actual length */ 1027195957Salfred uxfer->actual_length = actlen; 1028195957Salfred for (i = 0; i != iso_packets; i++) { 1029195957Salfred uxfer->iso_packet_desc[i].actual_length = 1030195957Salfred libusb20_tr_get_length(pxfer, i); 1031195957Salfred } 1032195957Salfred libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED); 1033195957Salfred break; 1034195560Sthompsa 1035194676Sthompsa case LIBUSB20_TRANSFER_START: 1036195957Salfred 1037195957Salfred /* setup length(s) */ 1038195957Salfred actlen = 0; 1039195957Salfred for (i = 0; i != iso_packets; i++) { 1040195957Salfred libusb20_tr_setup_isoc(pxfer, 1041195957Salfred &uxfer->buffer[actlen], 1042195957Salfred uxfer->iso_packet_desc[i].length, i); 1043195957Salfred actlen += uxfer->iso_packet_desc[i].length; 1044194676Sthompsa } 1045195957Salfred 1046195957Salfred /* no remainder */ 1047195957Salfred sxfer->rem_len = 0; 1048195957Salfred 1049195957Salfred libusb20_tr_set_total_frames(pxfer, iso_packets); 1050195957Salfred libusb20_tr_submit(pxfer); 1051195957Salfred 1052195957Salfred /* fork another USB transfer, if any */ 1053195957Salfred libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint); 1054195957Salfred break; 1055195957Salfred 1056194676Sthompsa default: 1057195957Salfred libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status)); 1058195957Salfred break; 1059194676Sthompsa } 1060195957Salfred} 1061194676Sthompsa 1062195957Salfred/* This function must be called locked */ 1063195957Salfred 1064195957Salfredstatic void 1065195957Salfredlibusb10_bulk_intr_proxy(struct libusb20_transfer *pxfer) 1066195957Salfred{ 1067195957Salfred struct libusb_super_transfer *sxfer; 1068195957Salfred struct libusb_transfer *uxfer; 1069195957Salfred uint32_t max_bulk; 1070195957Salfred uint32_t actlen; 1071195957Salfred uint8_t status; 1072195957Salfred uint8_t flags; 1073195957Salfred 1074195957Salfred status = libusb20_tr_get_status(pxfer); 1075195957Salfred sxfer = libusb20_tr_get_priv_sc1(pxfer); 1076195957Salfred max_bulk = libusb20_tr_get_max_total_length(pxfer); 1077195957Salfred actlen = libusb20_tr_get_actual_length(pxfer); 1078195957Salfred 1079195957Salfred if (sxfer == NULL) 1080195957Salfred return; /* cancelled - nothing to do */ 1081195957Salfred 1082195957Salfred uxfer = (struct libusb_transfer *)( 1083195957Salfred ((uint8_t *)sxfer) + sizeof(*sxfer)); 1084195957Salfred 1085195957Salfred flags = uxfer->flags; 1086195957Salfred 1087194676Sthompsa switch (status) { 1088194676Sthompsa case LIBUSB20_TRANSFER_COMPLETED: 1089195957Salfred 1090195957Salfred uxfer->actual_length += actlen; 1091195957Salfred 1092195957Salfred /* check for short packet */ 1093195957Salfred if (sxfer->last_len != actlen) { 1094195957Salfred if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) { 1095195957Salfred libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR); 1096195957Salfred } else { 1097195957Salfred libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED); 1098195957Salfred } 1099195957Salfred break; 1100195957Salfred } 1101195957Salfred /* check for end of data */ 1102195957Salfred if (sxfer->rem_len == 0) { 1103195957Salfred libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED); 1104195957Salfred break; 1105195957Salfred } 1106195957Salfred /* FALLTHROUGH */ 1107195957Salfred 1108195957Salfred case LIBUSB20_TRANSFER_START: 1109195957Salfred if (max_bulk > sxfer->rem_len) { 1110195957Salfred max_bulk = sxfer->rem_len; 1111195957Salfred } 1112195957Salfred /* setup new BULK or INTERRUPT transaction */ 1113195957Salfred libusb20_tr_setup_bulk(pxfer, 1114195957Salfred sxfer->curr_data, max_bulk, uxfer->timeout); 1115195957Salfred 1116195957Salfred /* update counters */ 1117195957Salfred sxfer->last_len = max_bulk; 1118195957Salfred sxfer->curr_data += max_bulk; 1119195957Salfred sxfer->rem_len -= max_bulk; 1120195957Salfred 1121195957Salfred libusb20_tr_submit(pxfer); 1122195957Salfred 1123195957Salfred /* check if we can fork another USB transfer */ 1124195957Salfred if (sxfer->rem_len == 0) 1125195957Salfred libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint); 1126195957Salfred break; 1127195957Salfred 1128195957Salfred default: 1129195957Salfred libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status)); 1130195957Salfred break; 1131194676Sthompsa } 1132194676Sthompsa} 1133194676Sthompsa 1134195957Salfred/* This function must be called locked */ 1135195957Salfred 1136195957Salfredstatic void 1137195957Salfredlibusb10_ctrl_proxy(struct libusb20_transfer *pxfer) 1138194676Sthompsa{ 1139195957Salfred struct libusb_super_transfer *sxfer; 1140195957Salfred struct libusb_transfer *uxfer; 1141195957Salfred uint32_t max_bulk; 1142195957Salfred uint32_t actlen; 1143195957Salfred uint8_t status; 1144195957Salfred uint8_t flags; 1145194676Sthompsa 1146195957Salfred status = libusb20_tr_get_status(pxfer); 1147195957Salfred sxfer = libusb20_tr_get_priv_sc1(pxfer); 1148195957Salfred max_bulk = libusb20_tr_get_max_total_length(pxfer); 1149195957Salfred actlen = libusb20_tr_get_actual_length(pxfer); 1150194676Sthompsa 1151195957Salfred if (sxfer == NULL) 1152195957Salfred return; /* cancelled - nothing to do */ 1153194676Sthompsa 1154195957Salfred uxfer = (struct libusb_transfer *)( 1155195957Salfred ((uint8_t *)sxfer) + sizeof(*sxfer)); 1156194676Sthompsa 1157195957Salfred flags = uxfer->flags; 1158194676Sthompsa 1159195957Salfred switch (status) { 1160195957Salfred case LIBUSB20_TRANSFER_COMPLETED: 1161194676Sthompsa 1162195957Salfred uxfer->actual_length += actlen; 1163195957Salfred 1164195957Salfred /* subtract length of SETUP packet, if any */ 1165195957Salfred actlen -= libusb20_tr_get_length(pxfer, 0); 1166195957Salfred 1167195957Salfred /* check for short packet */ 1168195957Salfred if (sxfer->last_len != actlen) { 1169195957Salfred if (flags & LIBUSB_TRANSFER_SHORT_NOT_OK) { 1170195957Salfred libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_ERROR); 1171195957Salfred } else { 1172195957Salfred libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED); 1173195957Salfred } 1174195957Salfred break; 1175194676Sthompsa } 1176195957Salfred /* check for end of data */ 1177195957Salfred if (sxfer->rem_len == 0) { 1178195957Salfred libusb10_complete_transfer(pxfer, sxfer, LIBUSB_TRANSFER_COMPLETED); 1179195957Salfred break; 1180195957Salfred } 1181195957Salfred /* FALLTHROUGH */ 1182194676Sthompsa 1183195957Salfred case LIBUSB20_TRANSFER_START: 1184195957Salfred if (max_bulk > sxfer->rem_len) { 1185195957Salfred max_bulk = sxfer->rem_len; 1186195957Salfred } 1187195957Salfred /* setup new CONTROL transaction */ 1188195957Salfred if (status == LIBUSB20_TRANSFER_COMPLETED) { 1189195957Salfred /* next fragment - don't send SETUP packet */ 1190195957Salfred libusb20_tr_set_length(pxfer, 0, 0); 1191195957Salfred } else { 1192195957Salfred /* first fragment - send SETUP packet */ 1193195957Salfred libusb20_tr_set_length(pxfer, 8, 0); 1194195957Salfred libusb20_tr_set_buffer(pxfer, uxfer->buffer, 0); 1195195957Salfred } 1196195957Salfred 1197195957Salfred if (max_bulk != 0) { 1198195957Salfred libusb20_tr_set_length(pxfer, max_bulk, 1); 1199195957Salfred libusb20_tr_set_buffer(pxfer, sxfer->curr_data, 1); 1200195957Salfred libusb20_tr_set_total_frames(pxfer, 2); 1201195957Salfred } else { 1202195957Salfred libusb20_tr_set_total_frames(pxfer, 1); 1203195957Salfred } 1204195957Salfred 1205195957Salfred /* update counters */ 1206195957Salfred sxfer->last_len = max_bulk; 1207195957Salfred sxfer->curr_data += max_bulk; 1208195957Salfred sxfer->rem_len -= max_bulk; 1209195957Salfred 1210195957Salfred libusb20_tr_submit(pxfer); 1211195957Salfred 1212195957Salfred /* check if we can fork another USB transfer */ 1213195957Salfred if (sxfer->rem_len == 0) 1214195957Salfred libusb10_submit_transfer_sub(libusb20_tr_get_priv_sc0(pxfer), uxfer->endpoint); 1215195957Salfred break; 1216195957Salfred 1217195957Salfred default: 1218195957Salfred libusb10_complete_transfer(pxfer, sxfer, libusb10_convert_error(status)); 1219195957Salfred break; 1220194676Sthompsa } 1221195957Salfred} 1222195957Salfred 1223195957Salfred/* The following function must be called locked */ 1224195957Salfred 1225195957Salfredstatic void 1226195957Salfredlibusb10_submit_transfer_sub(struct libusb20_device *pdev, uint8_t endpoint) 1227195957Salfred{ 1228195957Salfred struct libusb20_transfer *pxfer0; 1229195957Salfred struct libusb20_transfer *pxfer1; 1230195957Salfred struct libusb_super_transfer *sxfer; 1231195957Salfred struct libusb_transfer *uxfer; 1232195957Salfred struct libusb_device *dev; 1233195957Salfred int err; 1234195957Salfred int buffsize; 1235195957Salfred int maxframe; 1236195957Salfred int temp; 1237195957Salfred uint8_t dummy; 1238195957Salfred 1239195957Salfred dev = libusb_get_device(pdev); 1240195957Salfred 1241195957Salfred pxfer0 = libusb10_get_transfer(pdev, endpoint, 0); 1242195957Salfred pxfer1 = libusb10_get_transfer(pdev, endpoint, 1); 1243195957Salfred 1244195957Salfred if (pxfer0 == NULL || pxfer1 == NULL) 1245195957Salfred return; /* shouldn't happen */ 1246195957Salfred 1247195957Salfred temp = 0; 1248195957Salfred if (libusb20_tr_pending(pxfer0)) 1249195957Salfred temp |= 1; 1250195957Salfred if (libusb20_tr_pending(pxfer1)) 1251195957Salfred temp |= 2; 1252195957Salfred 1253195957Salfred switch (temp) { 1254195957Salfred case 3: 1255195957Salfred /* wait till one of the transfers complete */ 1256195957Salfred return; 1257195957Salfred case 2: 1258195957Salfred sxfer = libusb20_tr_get_priv_sc1(pxfer1); 1259199575Sthompsa if (sxfer == NULL) 1260199575Sthompsa return; /* cancelling */ 1261195957Salfred if (sxfer->rem_len) 1262195957Salfred return; /* cannot queue another one */ 1263195957Salfred /* swap transfers */ 1264195957Salfred pxfer1 = pxfer0; 1265195957Salfred break; 1266195957Salfred case 1: 1267195957Salfred sxfer = libusb20_tr_get_priv_sc1(pxfer0); 1268199575Sthompsa if (sxfer == NULL) 1269199575Sthompsa return; /* cancelling */ 1270195957Salfred if (sxfer->rem_len) 1271195957Salfred return; /* cannot queue another one */ 1272195957Salfred /* swap transfers */ 1273195957Salfred pxfer0 = pxfer1; 1274195957Salfred break; 1275195957Salfred default: 1276195957Salfred break; 1277194676Sthompsa } 1278195957Salfred 1279195957Salfred /* find next transfer on same endpoint */ 1280195957Salfred TAILQ_FOREACH(sxfer, &dev->tr_head, entry) { 1281195957Salfred 1282195957Salfred uxfer = (struct libusb_transfer *)( 1283195957Salfred ((uint8_t *)sxfer) + sizeof(*sxfer)); 1284195957Salfred 1285195957Salfred if (uxfer->endpoint == endpoint) { 1286195957Salfred TAILQ_REMOVE(&dev->tr_head, sxfer, entry); 1287195957Salfred sxfer->entry.tqe_prev = NULL; 1288195957Salfred goto found; 1289194676Sthompsa } 1290195957Salfred } 1291195957Salfred return; /* success */ 1292194676Sthompsa 1293195957Salfredfound: 1294194676Sthompsa 1295195957Salfred libusb20_tr_set_priv_sc0(pxfer0, pdev); 1296195957Salfred libusb20_tr_set_priv_sc1(pxfer0, sxfer); 1297194676Sthompsa 1298195957Salfred /* reset super transfer state */ 1299195957Salfred sxfer->rem_len = uxfer->length; 1300195957Salfred sxfer->curr_data = uxfer->buffer; 1301195957Salfred uxfer->actual_length = 0; 1302194676Sthompsa 1303195957Salfred switch (uxfer->type) { 1304195957Salfred case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS: 1305195957Salfred libusb20_tr_set_callback(pxfer0, libusb10_isoc_proxy); 1306195957Salfred break; 1307195957Salfred case LIBUSB_TRANSFER_TYPE_BULK: 1308195957Salfred case LIBUSB_TRANSFER_TYPE_INTERRUPT: 1309195957Salfred libusb20_tr_set_callback(pxfer0, libusb10_bulk_intr_proxy); 1310195957Salfred break; 1311195957Salfred case LIBUSB_TRANSFER_TYPE_CONTROL: 1312195957Salfred libusb20_tr_set_callback(pxfer0, libusb10_ctrl_proxy); 1313195957Salfred if (sxfer->rem_len < 8) 1314195957Salfred goto failure; 1315194676Sthompsa 1316195957Salfred /* remove SETUP packet from data */ 1317195957Salfred sxfer->rem_len -= 8; 1318195957Salfred sxfer->curr_data += 8; 1319195957Salfred break; 1320195957Salfred default: 1321195957Salfred goto failure; 1322195560Sthompsa } 1323195957Salfred 1324195957Salfred buffsize = libusb10_get_buffsize(pdev, uxfer); 1325195957Salfred maxframe = libusb10_get_maxframe(pdev, uxfer); 1326195957Salfred 1327195957Salfred /* make sure the transfer is opened */ 1328195957Salfred err = libusb20_tr_open(pxfer0, buffsize, maxframe, endpoint); 1329195957Salfred if (err && (err != LIBUSB20_ERROR_BUSY)) { 1330195957Salfred goto failure; 1331194676Sthompsa } 1332195957Salfred libusb20_tr_start(pxfer0); 1333195957Salfred return; 1334194676Sthompsa 1335195957Salfredfailure: 1336195957Salfred libusb10_complete_transfer(pxfer0, sxfer, LIBUSB_TRANSFER_ERROR); 1337194676Sthompsa 1338195957Salfred /* make sure our event loop spins the done handler */ 1339195957Salfred dummy = 0; 1340248236Shselasky err = write(dev->ctx->ctrl_pipe[1], &dummy, sizeof(dummy)); 1341195957Salfred} 1342194676Sthompsa 1343195957Salfred/* The following function must be called unlocked */ 1344194676Sthompsa 1345195957Salfredint 1346195957Salfredlibusb_submit_transfer(struct libusb_transfer *uxfer) 1347195957Salfred{ 1348195957Salfred struct libusb20_transfer *pxfer0; 1349195957Salfred struct libusb20_transfer *pxfer1; 1350195957Salfred struct libusb_super_transfer *sxfer; 1351195957Salfred struct libusb_device *dev; 1352234684Shselasky uint8_t endpoint; 1353195957Salfred int err; 1354195957Salfred 1355195957Salfred if (uxfer == NULL) 1356195957Salfred return (LIBUSB_ERROR_INVALID_PARAM); 1357195957Salfred 1358195957Salfred if (uxfer->dev_handle == NULL) 1359195957Salfred return (LIBUSB_ERROR_INVALID_PARAM); 1360195957Salfred 1361195957Salfred endpoint = uxfer->endpoint; 1362195957Salfred 1363195957Salfred dev = libusb_get_device(uxfer->dev_handle); 1364195957Salfred 1365195957Salfred DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer enter"); 1366195957Salfred 1367195957Salfred sxfer = (struct libusb_super_transfer *)( 1368195957Salfred (uint8_t *)uxfer - sizeof(*sxfer)); 1369195957Salfred 1370195957Salfred CTX_LOCK(dev->ctx); 1371195957Salfred 1372195957Salfred pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0); 1373195957Salfred pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1); 1374195957Salfred 1375195957Salfred if (pxfer0 == NULL || pxfer1 == NULL) { 1376195957Salfred err = LIBUSB_ERROR_OTHER; 1377195957Salfred } else if ((sxfer->entry.tqe_prev != NULL) || 1378199575Sthompsa (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) || 1379195957Salfred (libusb20_tr_get_priv_sc1(pxfer1) == sxfer)) { 1380195957Salfred err = LIBUSB_ERROR_BUSY; 1381195957Salfred } else { 1382199575Sthompsa 1383199575Sthompsa /* set pending state */ 1384199575Sthompsa sxfer->state = LIBUSB_SUPER_XFER_ST_PEND; 1385199575Sthompsa 1386199575Sthompsa /* insert transfer into transfer head list */ 1387195957Salfred TAILQ_INSERT_TAIL(&dev->tr_head, sxfer, entry); 1388195957Salfred 1389199575Sthompsa /* start work transfers */ 1390195957Salfred libusb10_submit_transfer_sub( 1391195957Salfred uxfer->dev_handle, endpoint); 1392195957Salfred 1393195957Salfred err = 0; /* success */ 1394195957Salfred } 1395195957Salfred 1396195957Salfred CTX_UNLOCK(dev->ctx); 1397195957Salfred 1398195957Salfred DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_submit_transfer leave %d", err); 1399195957Salfred 1400195957Salfred return (err); 1401194676Sthompsa} 1402194676Sthompsa 1403195957Salfred/* Asynchronous transfer cancel */ 1404195957Salfred 1405194676Sthompsaint 1406195957Salfredlibusb_cancel_transfer(struct libusb_transfer *uxfer) 1407194676Sthompsa{ 1408195957Salfred struct libusb20_transfer *pxfer0; 1409195957Salfred struct libusb20_transfer *pxfer1; 1410195957Salfred struct libusb_super_transfer *sxfer; 1411195957Salfred struct libusb_device *dev; 1412234684Shselasky uint8_t endpoint; 1413199575Sthompsa int retval; 1414194676Sthompsa 1415195957Salfred if (uxfer == NULL) 1416195957Salfred return (LIBUSB_ERROR_INVALID_PARAM); 1417194676Sthompsa 1418199575Sthompsa /* check if not initialised */ 1419195957Salfred if (uxfer->dev_handle == NULL) 1420199575Sthompsa return (LIBUSB_ERROR_NOT_FOUND); 1421194676Sthompsa 1422195957Salfred endpoint = uxfer->endpoint; 1423194676Sthompsa 1424195957Salfred dev = libusb_get_device(uxfer->dev_handle); 1425195957Salfred 1426195957Salfred DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer enter"); 1427195957Salfred 1428195957Salfred sxfer = (struct libusb_super_transfer *)( 1429195957Salfred (uint8_t *)uxfer - sizeof(*sxfer)); 1430195957Salfred 1431199575Sthompsa retval = 0; 1432199575Sthompsa 1433195957Salfred CTX_LOCK(dev->ctx); 1434195957Salfred 1435195957Salfred pxfer0 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 0); 1436195957Salfred pxfer1 = libusb10_get_transfer(uxfer->dev_handle, endpoint, 1); 1437195957Salfred 1438199575Sthompsa if (sxfer->state != LIBUSB_SUPER_XFER_ST_PEND) { 1439199575Sthompsa /* only update the transfer status */ 1440199575Sthompsa uxfer->status = LIBUSB_TRANSFER_CANCELLED; 1441199575Sthompsa retval = LIBUSB_ERROR_NOT_FOUND; 1442199575Sthompsa } else if (sxfer->entry.tqe_prev != NULL) { 1443195957Salfred /* we are lucky - transfer is on a queue */ 1444195957Salfred TAILQ_REMOVE(&dev->tr_head, sxfer, entry); 1445195957Salfred sxfer->entry.tqe_prev = NULL; 1446199575Sthompsa libusb10_complete_transfer(NULL, 1447199575Sthompsa sxfer, LIBUSB_TRANSFER_CANCELLED); 1448195957Salfred } else if (pxfer0 == NULL || pxfer1 == NULL) { 1449195957Salfred /* not started */ 1450199575Sthompsa retval = LIBUSB_ERROR_NOT_FOUND; 1451195957Salfred } else if (libusb20_tr_get_priv_sc1(pxfer0) == sxfer) { 1452199575Sthompsa libusb10_complete_transfer(pxfer0, 1453199575Sthompsa sxfer, LIBUSB_TRANSFER_CANCELLED); 1454195957Salfred libusb20_tr_stop(pxfer0); 1455195957Salfred /* make sure the queue doesn't stall */ 1456195957Salfred libusb10_submit_transfer_sub( 1457195957Salfred uxfer->dev_handle, endpoint); 1458195957Salfred } else if (libusb20_tr_get_priv_sc1(pxfer1) == sxfer) { 1459199575Sthompsa libusb10_complete_transfer(pxfer1, 1460199575Sthompsa sxfer, LIBUSB_TRANSFER_CANCELLED); 1461195957Salfred libusb20_tr_stop(pxfer1); 1462195957Salfred /* make sure the queue doesn't stall */ 1463195957Salfred libusb10_submit_transfer_sub( 1464195957Salfred uxfer->dev_handle, endpoint); 1465195957Salfred } else { 1466195957Salfred /* not started */ 1467199575Sthompsa retval = LIBUSB_ERROR_NOT_FOUND; 1468195957Salfred } 1469195957Salfred 1470195957Salfred CTX_UNLOCK(dev->ctx); 1471195957Salfred 1472195957Salfred DPRINTF(dev->ctx, LIBUSB_DEBUG_FUNCTION, "libusb_cancel_transfer leave"); 1473195957Salfred 1474199575Sthompsa return (retval); 1475194676Sthompsa} 1476194676Sthompsa 1477195957SalfredUNEXPORTED void 1478195957Salfredlibusb10_cancel_all_transfer(libusb_device *dev) 1479195957Salfred{ 1480195957Salfred /* TODO */ 1481195957Salfred} 1482199055Sthompsa 1483199055Sthompsauint16_t 1484199055Sthompsalibusb_cpu_to_le16(uint16_t x) 1485199055Sthompsa{ 1486199055Sthompsa return (htole16(x)); 1487199055Sthompsa} 1488199055Sthompsa 1489199055Sthompsauint16_t 1490199055Sthompsalibusb_le16_to_cpu(uint16_t x) 1491199055Sthompsa{ 1492199055Sthompsa return (le16toh(x)); 1493199055Sthompsa} 1494199055Sthompsa 1495213853Shselaskyconst char * 1496213853Shselaskylibusb_strerror(int code) 1497213853Shselasky{ 1498225659Shselasky switch (code) { 1499225659Shselasky case LIBUSB_SUCCESS: 1500225659Shselasky return ("Success"); 1501225659Shselasky case LIBUSB_ERROR_IO: 1502225659Shselasky return ("I/O error"); 1503225659Shselasky case LIBUSB_ERROR_INVALID_PARAM: 1504225659Shselasky return ("Invalid parameter"); 1505225659Shselasky case LIBUSB_ERROR_ACCESS: 1506225659Shselasky return ("Permissions error"); 1507225659Shselasky case LIBUSB_ERROR_NO_DEVICE: 1508225659Shselasky return ("No device"); 1509225659Shselasky case LIBUSB_ERROR_NOT_FOUND: 1510225659Shselasky return ("Not found"); 1511225659Shselasky case LIBUSB_ERROR_BUSY: 1512225659Shselasky return ("Device busy"); 1513225659Shselasky case LIBUSB_ERROR_TIMEOUT: 1514225659Shselasky return ("Timeout"); 1515225659Shselasky case LIBUSB_ERROR_OVERFLOW: 1516225659Shselasky return ("Overflow"); 1517225659Shselasky case LIBUSB_ERROR_PIPE: 1518225659Shselasky return ("Pipe error"); 1519225659Shselasky case LIBUSB_ERROR_INTERRUPTED: 1520225659Shselasky return ("Interrupted"); 1521225659Shselasky case LIBUSB_ERROR_NO_MEM: 1522225659Shselasky return ("Out of memory"); 1523225659Shselasky case LIBUSB_ERROR_NOT_SUPPORTED: 1524225659Shselasky return ("Not supported"); 1525225659Shselasky case LIBUSB_ERROR_OTHER: 1526225659Shselasky return ("Other error"); 1527225659Shselasky default: 1528225659Shselasky return ("Unknown error"); 1529225659Shselasky } 1530213853Shselasky} 1531225659Shselasky 1532225659Shselaskyconst char * 1533225659Shselaskylibusb_error_name(int code) 1534225659Shselasky{ 1535225659Shselasky switch (code) { 1536225659Shselasky case LIBUSB_SUCCESS: 1537225659Shselasky return ("LIBUSB_SUCCESS"); 1538225659Shselasky case LIBUSB_ERROR_IO: 1539225659Shselasky return ("LIBUSB_ERROR_IO"); 1540225659Shselasky case LIBUSB_ERROR_INVALID_PARAM: 1541225659Shselasky return ("LIBUSB_ERROR_INVALID_PARAM"); 1542225659Shselasky case LIBUSB_ERROR_ACCESS: 1543225659Shselasky return ("LIBUSB_ERROR_ACCESS"); 1544225659Shselasky case LIBUSB_ERROR_NO_DEVICE: 1545225659Shselasky return ("LIBUSB_ERROR_NO_DEVICE"); 1546225659Shselasky case LIBUSB_ERROR_NOT_FOUND: 1547225659Shselasky return ("LIBUSB_ERROR_NOT_FOUND"); 1548225659Shselasky case LIBUSB_ERROR_BUSY: 1549225659Shselasky return ("LIBUSB_ERROR_BUSY"); 1550225659Shselasky case LIBUSB_ERROR_TIMEOUT: 1551225659Shselasky return ("LIBUSB_ERROR_TIMEOUT"); 1552225659Shselasky case LIBUSB_ERROR_OVERFLOW: 1553225659Shselasky return ("LIBUSB_ERROR_OVERFLOW"); 1554225659Shselasky case LIBUSB_ERROR_PIPE: 1555225659Shselasky return ("LIBUSB_ERROR_PIPE"); 1556225659Shselasky case LIBUSB_ERROR_INTERRUPTED: 1557225659Shselasky return ("LIBUSB_ERROR_INTERRUPTED"); 1558225659Shselasky case LIBUSB_ERROR_NO_MEM: 1559225659Shselasky return ("LIBUSB_ERROR_NO_MEM"); 1560225659Shselasky case LIBUSB_ERROR_NOT_SUPPORTED: 1561225659Shselasky return ("LIBUSB_ERROR_NOT_SUPPORTED"); 1562225659Shselasky case LIBUSB_ERROR_OTHER: 1563225659Shselasky return ("LIBUSB_ERROR_OTHER"); 1564225659Shselasky default: 1565225659Shselasky return ("LIBUSB_ERROR_UNKNOWN"); 1566225659Shselasky } 1567225659Shselasky} 1568