1df4b8c2aSAndrew Thompson /* $FreeBSD$ */ 2df4b8c2aSAndrew Thompson /*- 3df4b8c2aSAndrew Thompson * Copyright (c) 2008 Hans Petter Selasky. All rights reserved. 4df4b8c2aSAndrew Thompson * 5df4b8c2aSAndrew Thompson * Redistribution and use in source and binary forms, with or without 6df4b8c2aSAndrew Thompson * modification, are permitted provided that the following conditions 7df4b8c2aSAndrew Thompson * are met: 8df4b8c2aSAndrew Thompson * 1. Redistributions of source code must retain the above copyright 9df4b8c2aSAndrew Thompson * notice, this list of conditions and the following disclaimer. 10df4b8c2aSAndrew Thompson * 2. Redistributions in binary form must reproduce the above copyright 11df4b8c2aSAndrew Thompson * notice, this list of conditions and the following disclaimer in the 12df4b8c2aSAndrew Thompson * documentation and/or other materials provided with the distribution. 13df4b8c2aSAndrew Thompson * 14df4b8c2aSAndrew Thompson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15df4b8c2aSAndrew Thompson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16df4b8c2aSAndrew Thompson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17df4b8c2aSAndrew Thompson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18df4b8c2aSAndrew Thompson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19df4b8c2aSAndrew Thompson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20df4b8c2aSAndrew Thompson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21df4b8c2aSAndrew Thompson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22df4b8c2aSAndrew Thompson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23df4b8c2aSAndrew Thompson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24df4b8c2aSAndrew Thompson * SUCH DAMAGE. 25df4b8c2aSAndrew Thompson */ 26df4b8c2aSAndrew Thompson 27df4b8c2aSAndrew Thompson #include <sys/queue.h> 28df4b8c2aSAndrew Thompson #include <sys/types.h> 29df4b8c2aSAndrew Thompson 30df4b8c2aSAndrew Thompson #include <stdio.h> 31df4b8c2aSAndrew Thompson #include <stdlib.h> 32df4b8c2aSAndrew Thompson #include <unistd.h> 33df4b8c2aSAndrew Thompson #include <string.h> 34df4b8c2aSAndrew Thompson #include <poll.h> 35df4b8c2aSAndrew Thompson #include <fcntl.h> 36df4b8c2aSAndrew Thompson #include <errno.h> 37df4b8c2aSAndrew Thompson 38df4b8c2aSAndrew Thompson #include "libusb20.h" 39df4b8c2aSAndrew Thompson #include "libusb20_desc.h" 40df4b8c2aSAndrew Thompson #include "libusb20_int.h" 41df4b8c2aSAndrew Thompson 42df4b8c2aSAndrew Thompson #include <dev/usb/usb.h> 43ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h> 44df4b8c2aSAndrew Thompson #include <dev/usb/usb_ioctl.h> 45df4b8c2aSAndrew Thompson 46df4b8c2aSAndrew Thompson static libusb20_init_backend_t ugen20_init_backend; 47df4b8c2aSAndrew Thompson static libusb20_open_device_t ugen20_open_device; 48df4b8c2aSAndrew Thompson static libusb20_close_device_t ugen20_close_device; 49df4b8c2aSAndrew Thompson static libusb20_get_backend_name_t ugen20_get_backend_name; 50df4b8c2aSAndrew Thompson static libusb20_exit_backend_t ugen20_exit_backend; 51df4b8c2aSAndrew Thompson static libusb20_dev_get_iface_desc_t ugen20_dev_get_iface_desc; 52df4b8c2aSAndrew Thompson static libusb20_dev_get_info_t ugen20_dev_get_info; 53df4b8c2aSAndrew Thompson static libusb20_root_get_dev_quirk_t ugen20_root_get_dev_quirk; 54df4b8c2aSAndrew Thompson static libusb20_root_get_quirk_name_t ugen20_root_get_quirk_name; 55df4b8c2aSAndrew Thompson static libusb20_root_add_dev_quirk_t ugen20_root_add_dev_quirk; 56df4b8c2aSAndrew Thompson static libusb20_root_remove_dev_quirk_t ugen20_root_remove_dev_quirk; 57df4b8c2aSAndrew Thompson static libusb20_root_set_template_t ugen20_root_set_template; 58df4b8c2aSAndrew Thompson static libusb20_root_get_template_t ugen20_root_get_template; 59df4b8c2aSAndrew Thompson 60df4b8c2aSAndrew Thompson const struct libusb20_backend_methods libusb20_ugen20_backend = { 61df4b8c2aSAndrew Thompson LIBUSB20_BACKEND(LIBUSB20_DECLARE, ugen20) 62df4b8c2aSAndrew Thompson }; 63df4b8c2aSAndrew Thompson 64df4b8c2aSAndrew Thompson /* USB device specific */ 65df4b8c2aSAndrew Thompson static libusb20_get_config_desc_full_t ugen20_get_config_desc_full; 66df4b8c2aSAndrew Thompson static libusb20_get_config_index_t ugen20_get_config_index; 67df4b8c2aSAndrew Thompson static libusb20_set_config_index_t ugen20_set_config_index; 68df4b8c2aSAndrew Thompson static libusb20_set_alt_index_t ugen20_set_alt_index; 69df4b8c2aSAndrew Thompson static libusb20_reset_device_t ugen20_reset_device; 70df4b8c2aSAndrew Thompson static libusb20_set_power_mode_t ugen20_set_power_mode; 71df4b8c2aSAndrew Thompson static libusb20_get_power_mode_t ugen20_get_power_mode; 72df4b8c2aSAndrew Thompson static libusb20_kernel_driver_active_t ugen20_kernel_driver_active; 73df4b8c2aSAndrew Thompson static libusb20_detach_kernel_driver_t ugen20_detach_kernel_driver; 74df4b8c2aSAndrew Thompson static libusb20_do_request_sync_t ugen20_do_request_sync; 75df4b8c2aSAndrew Thompson static libusb20_process_t ugen20_process; 76df4b8c2aSAndrew Thompson 77df4b8c2aSAndrew Thompson /* USB transfer specific */ 78df4b8c2aSAndrew Thompson static libusb20_tr_open_t ugen20_tr_open; 79df4b8c2aSAndrew Thompson static libusb20_tr_close_t ugen20_tr_close; 80df4b8c2aSAndrew Thompson static libusb20_tr_clear_stall_sync_t ugen20_tr_clear_stall_sync; 81df4b8c2aSAndrew Thompson static libusb20_tr_submit_t ugen20_tr_submit; 82df4b8c2aSAndrew Thompson static libusb20_tr_cancel_async_t ugen20_tr_cancel_async; 83df4b8c2aSAndrew Thompson 84df4b8c2aSAndrew Thompson static const struct libusb20_device_methods libusb20_ugen20_device_methods = { 85df4b8c2aSAndrew Thompson LIBUSB20_DEVICE(LIBUSB20_DECLARE, ugen20) 86df4b8c2aSAndrew Thompson }; 87df4b8c2aSAndrew Thompson 88df4b8c2aSAndrew Thompson static const char * 89df4b8c2aSAndrew Thompson ugen20_get_backend_name(void) 90df4b8c2aSAndrew Thompson { 91df4b8c2aSAndrew Thompson return ("FreeBSD UGEN 2.0"); 92df4b8c2aSAndrew Thompson } 93df4b8c2aSAndrew Thompson 94df4b8c2aSAndrew Thompson static uint32_t 95df4b8c2aSAndrew Thompson ugen20_path_convert_one(const char **pp) 96df4b8c2aSAndrew Thompson { 97df4b8c2aSAndrew Thompson const char *ptr; 98df4b8c2aSAndrew Thompson uint32_t temp = 0; 99df4b8c2aSAndrew Thompson 100df4b8c2aSAndrew Thompson ptr = *pp; 101df4b8c2aSAndrew Thompson 102df4b8c2aSAndrew Thompson while ((*ptr >= '0') && (*ptr <= '9')) { 103df4b8c2aSAndrew Thompson temp *= 10; 104df4b8c2aSAndrew Thompson temp += (*ptr - '0'); 105df4b8c2aSAndrew Thompson if (temp >= 1000000) { 106df4b8c2aSAndrew Thompson /* catch overflow early */ 107df4b8c2aSAndrew Thompson return (0 - 1); 108df4b8c2aSAndrew Thompson } 109df4b8c2aSAndrew Thompson ptr++; 110df4b8c2aSAndrew Thompson } 111df4b8c2aSAndrew Thompson 112df4b8c2aSAndrew Thompson if (*ptr == '.') { 113df4b8c2aSAndrew Thompson /* skip dot */ 114df4b8c2aSAndrew Thompson ptr++; 115df4b8c2aSAndrew Thompson } 116df4b8c2aSAndrew Thompson *pp = ptr; 117df4b8c2aSAndrew Thompson 118df4b8c2aSAndrew Thompson return (temp); 119df4b8c2aSAndrew Thompson } 120df4b8c2aSAndrew Thompson 121df4b8c2aSAndrew Thompson static int 122df4b8c2aSAndrew Thompson ugen20_enumerate(struct libusb20_device *pdev, const char *id) 123df4b8c2aSAndrew Thompson { 124df4b8c2aSAndrew Thompson const char *tmp = id; 125760bc48eSAndrew Thompson struct usb_device_descriptor ddesc; 126760bc48eSAndrew Thompson struct usb_device_info devinfo; 127df4b8c2aSAndrew Thompson uint32_t plugtime; 128df4b8c2aSAndrew Thompson char buf[64]; 129df4b8c2aSAndrew Thompson int f; 130df4b8c2aSAndrew Thompson int error; 131df4b8c2aSAndrew Thompson 132df4b8c2aSAndrew Thompson pdev->bus_number = ugen20_path_convert_one(&tmp); 133df4b8c2aSAndrew Thompson pdev->device_address = ugen20_path_convert_one(&tmp); 134df4b8c2aSAndrew Thompson 135df4b8c2aSAndrew Thompson snprintf(buf, sizeof(buf), "/dev/" USB_GENERIC_NAME "%u.%u", 136df4b8c2aSAndrew Thompson pdev->bus_number, pdev->device_address); 137df4b8c2aSAndrew Thompson 138df4b8c2aSAndrew Thompson f = open(buf, O_RDWR); 139df4b8c2aSAndrew Thompson if (f < 0) { 140df4b8c2aSAndrew Thompson return (LIBUSB20_ERROR_OTHER); 141df4b8c2aSAndrew Thompson } 142df4b8c2aSAndrew Thompson if (ioctl(f, USB_GET_PLUGTIME, &plugtime)) { 143df4b8c2aSAndrew Thompson error = LIBUSB20_ERROR_OTHER; 144df4b8c2aSAndrew Thompson goto done; 145df4b8c2aSAndrew Thompson } 146df4b8c2aSAndrew Thompson /* store when the device was plugged */ 147df4b8c2aSAndrew Thompson pdev->session_data.plugtime = plugtime; 148df4b8c2aSAndrew Thompson 149df4b8c2aSAndrew Thompson if (ioctl(f, USB_GET_DEVICE_DESC, &ddesc)) { 150df4b8c2aSAndrew Thompson error = LIBUSB20_ERROR_OTHER; 151df4b8c2aSAndrew Thompson goto done; 152df4b8c2aSAndrew Thompson } 153df4b8c2aSAndrew Thompson LIBUSB20_INIT(LIBUSB20_DEVICE_DESC, &(pdev->ddesc)); 154df4b8c2aSAndrew Thompson 155df4b8c2aSAndrew Thompson libusb20_me_decode(&ddesc, sizeof(ddesc), &(pdev->ddesc)); 156df4b8c2aSAndrew Thompson 157df4b8c2aSAndrew Thompson if (pdev->ddesc.bNumConfigurations == 0) { 158df4b8c2aSAndrew Thompson error = LIBUSB20_ERROR_OTHER; 159df4b8c2aSAndrew Thompson goto done; 160df4b8c2aSAndrew Thompson } else if (pdev->ddesc.bNumConfigurations >= 8) { 161df4b8c2aSAndrew Thompson error = LIBUSB20_ERROR_OTHER; 162df4b8c2aSAndrew Thompson goto done; 163df4b8c2aSAndrew Thompson } 164df4b8c2aSAndrew Thompson if (ioctl(f, USB_GET_DEVICEINFO, &devinfo)) { 165df4b8c2aSAndrew Thompson error = LIBUSB20_ERROR_OTHER; 166df4b8c2aSAndrew Thompson goto done; 167df4b8c2aSAndrew Thompson } 168df4b8c2aSAndrew Thompson switch (devinfo.udi_mode) { 169df4b8c2aSAndrew Thompson case USB_MODE_DEVICE: 170df4b8c2aSAndrew Thompson pdev->usb_mode = LIBUSB20_MODE_DEVICE; 171df4b8c2aSAndrew Thompson break; 172df4b8c2aSAndrew Thompson default: 173df4b8c2aSAndrew Thompson pdev->usb_mode = LIBUSB20_MODE_HOST; 174df4b8c2aSAndrew Thompson break; 175df4b8c2aSAndrew Thompson } 176df4b8c2aSAndrew Thompson 177df4b8c2aSAndrew Thompson switch (devinfo.udi_speed) { 178df4b8c2aSAndrew Thompson case USB_SPEED_LOW: 179df4b8c2aSAndrew Thompson pdev->usb_speed = LIBUSB20_SPEED_LOW; 180df4b8c2aSAndrew Thompson break; 181df4b8c2aSAndrew Thompson case USB_SPEED_FULL: 182df4b8c2aSAndrew Thompson pdev->usb_speed = LIBUSB20_SPEED_FULL; 183df4b8c2aSAndrew Thompson break; 184df4b8c2aSAndrew Thompson case USB_SPEED_HIGH: 185df4b8c2aSAndrew Thompson pdev->usb_speed = LIBUSB20_SPEED_HIGH; 186df4b8c2aSAndrew Thompson break; 187df4b8c2aSAndrew Thompson case USB_SPEED_VARIABLE: 188df4b8c2aSAndrew Thompson pdev->usb_speed = LIBUSB20_SPEED_VARIABLE; 189df4b8c2aSAndrew Thompson break; 190df4b8c2aSAndrew Thompson case USB_SPEED_SUPER: 191df4b8c2aSAndrew Thompson pdev->usb_speed = LIBUSB20_SPEED_SUPER; 192df4b8c2aSAndrew Thompson break; 193df4b8c2aSAndrew Thompson default: 194df4b8c2aSAndrew Thompson pdev->usb_speed = LIBUSB20_SPEED_UNKNOWN; 195df4b8c2aSAndrew Thompson break; 196df4b8c2aSAndrew Thompson } 197df4b8c2aSAndrew Thompson 198df4b8c2aSAndrew Thompson /* generate a nice description for printout */ 199df4b8c2aSAndrew Thompson 200df4b8c2aSAndrew Thompson snprintf(pdev->usb_desc, sizeof(pdev->usb_desc), 201df4b8c2aSAndrew Thompson USB_GENERIC_NAME "%u.%u: <%s %s> at usbus%u", pdev->bus_number, 202df4b8c2aSAndrew Thompson pdev->device_address, devinfo.udi_product, 203df4b8c2aSAndrew Thompson devinfo.udi_vendor, pdev->bus_number); 204df4b8c2aSAndrew Thompson 205df4b8c2aSAndrew Thompson error = 0; 206df4b8c2aSAndrew Thompson done: 207df4b8c2aSAndrew Thompson close(f); 208df4b8c2aSAndrew Thompson return (error); 209df4b8c2aSAndrew Thompson } 210df4b8c2aSAndrew Thompson 211df4b8c2aSAndrew Thompson struct ugen20_urd_state { 212760bc48eSAndrew Thompson struct usb_read_dir urd; 213df4b8c2aSAndrew Thompson uint32_t nparsed; 214df4b8c2aSAndrew Thompson int f; 215df4b8c2aSAndrew Thompson uint8_t *ptr; 216df4b8c2aSAndrew Thompson const char *src; 217df4b8c2aSAndrew Thompson const char *dst; 218df4b8c2aSAndrew Thompson uint8_t buf[256]; 219df4b8c2aSAndrew Thompson uint8_t dummy_zero[1]; 220df4b8c2aSAndrew Thompson }; 221df4b8c2aSAndrew Thompson 222df4b8c2aSAndrew Thompson static int 223df4b8c2aSAndrew Thompson ugen20_readdir(struct ugen20_urd_state *st) 224df4b8c2aSAndrew Thompson { 225df4b8c2aSAndrew Thompson ; /* style fix */ 226df4b8c2aSAndrew Thompson repeat: 227df4b8c2aSAndrew Thompson if (st->ptr == NULL) { 228df4b8c2aSAndrew Thompson st->urd.urd_startentry += st->nparsed; 229df4b8c2aSAndrew Thompson st->urd.urd_data = st->buf; 230df4b8c2aSAndrew Thompson st->urd.urd_maxlen = sizeof(st->buf); 231df4b8c2aSAndrew Thompson st->nparsed = 0; 232df4b8c2aSAndrew Thompson 233df4b8c2aSAndrew Thompson if (ioctl(st->f, USB_READ_DIR, &st->urd)) { 234df4b8c2aSAndrew Thompson return (EINVAL); 235df4b8c2aSAndrew Thompson } 236df4b8c2aSAndrew Thompson st->ptr = st->buf; 237df4b8c2aSAndrew Thompson } 238df4b8c2aSAndrew Thompson if (st->ptr[0] == 0) { 239df4b8c2aSAndrew Thompson if (st->nparsed) { 240df4b8c2aSAndrew Thompson st->ptr = NULL; 241df4b8c2aSAndrew Thompson goto repeat; 242df4b8c2aSAndrew Thompson } else { 243df4b8c2aSAndrew Thompson return (ENXIO); 244df4b8c2aSAndrew Thompson } 245df4b8c2aSAndrew Thompson } 246df4b8c2aSAndrew Thompson st->src = (void *)(st->ptr + 1); 247df4b8c2aSAndrew Thompson st->dst = st->src + strlen(st->src) + 1; 248df4b8c2aSAndrew Thompson st->ptr = st->ptr + st->ptr[0]; 249df4b8c2aSAndrew Thompson st->nparsed++; 250df4b8c2aSAndrew Thompson 251df4b8c2aSAndrew Thompson if ((st->ptr < st->buf) || 252df4b8c2aSAndrew Thompson (st->ptr > st->dummy_zero)) { 253df4b8c2aSAndrew Thompson /* invalid entry */ 254df4b8c2aSAndrew Thompson return (EINVAL); 255df4b8c2aSAndrew Thompson } 256df4b8c2aSAndrew Thompson return (0); 257df4b8c2aSAndrew Thompson } 258df4b8c2aSAndrew Thompson 259df4b8c2aSAndrew Thompson static int 260df4b8c2aSAndrew Thompson ugen20_init_backend(struct libusb20_backend *pbe) 261df4b8c2aSAndrew Thompson { 262df4b8c2aSAndrew Thompson struct ugen20_urd_state state; 263df4b8c2aSAndrew Thompson struct libusb20_device *pdev; 264df4b8c2aSAndrew Thompson 265df4b8c2aSAndrew Thompson memset(&state, 0, sizeof(state)); 266df4b8c2aSAndrew Thompson 267df4b8c2aSAndrew Thompson state.f = open("/dev/" USB_DEVICE_NAME, O_RDONLY); 268df4b8c2aSAndrew Thompson if (state.f < 0) 269df4b8c2aSAndrew Thompson return (LIBUSB20_ERROR_OTHER); 270df4b8c2aSAndrew Thompson 271df4b8c2aSAndrew Thompson while (ugen20_readdir(&state) == 0) { 272df4b8c2aSAndrew Thompson 273df4b8c2aSAndrew Thompson if ((state.src[0] != 'u') || 274df4b8c2aSAndrew Thompson (state.src[1] != 'g') || 275df4b8c2aSAndrew Thompson (state.src[2] != 'e') || 276df4b8c2aSAndrew Thompson (state.src[3] != 'n')) { 277df4b8c2aSAndrew Thompson continue; 278df4b8c2aSAndrew Thompson } 279df4b8c2aSAndrew Thompson pdev = libusb20_dev_alloc(); 280df4b8c2aSAndrew Thompson if (pdev == NULL) { 281df4b8c2aSAndrew Thompson continue; 282df4b8c2aSAndrew Thompson } 283df4b8c2aSAndrew Thompson if (ugen20_enumerate(pdev, state.src + 4)) { 284df4b8c2aSAndrew Thompson libusb20_dev_free(pdev); 285df4b8c2aSAndrew Thompson continue; 286df4b8c2aSAndrew Thompson } 287df4b8c2aSAndrew Thompson /* put the device on the backend list */ 288df4b8c2aSAndrew Thompson libusb20_be_enqueue_device(pbe, pdev); 289df4b8c2aSAndrew Thompson } 290df4b8c2aSAndrew Thompson close(state.f); 291df4b8c2aSAndrew Thompson return (0); /* success */ 292df4b8c2aSAndrew Thompson } 293df4b8c2aSAndrew Thompson 294df4b8c2aSAndrew Thompson static void 295df4b8c2aSAndrew Thompson ugen20_tr_release(struct libusb20_device *pdev) 296df4b8c2aSAndrew Thompson { 297760bc48eSAndrew Thompson struct usb_fs_uninit fs_uninit; 298df4b8c2aSAndrew Thompson 299df4b8c2aSAndrew Thompson if (pdev->nTransfer == 0) { 300df4b8c2aSAndrew Thompson return; 301df4b8c2aSAndrew Thompson } 302df4b8c2aSAndrew Thompson /* release all pending USB transfers */ 303df4b8c2aSAndrew Thompson if (pdev->privBeData != NULL) { 304df4b8c2aSAndrew Thompson memset(&fs_uninit, 0, sizeof(fs_uninit)); 305df4b8c2aSAndrew Thompson if (ioctl(pdev->file, USB_FS_UNINIT, &fs_uninit)) { 306df4b8c2aSAndrew Thompson /* ignore any errors of this kind */ 307df4b8c2aSAndrew Thompson } 308df4b8c2aSAndrew Thompson } 309df4b8c2aSAndrew Thompson return; 310df4b8c2aSAndrew Thompson } 311df4b8c2aSAndrew Thompson 312df4b8c2aSAndrew Thompson static int 313df4b8c2aSAndrew Thompson ugen20_tr_renew(struct libusb20_device *pdev) 314df4b8c2aSAndrew Thompson { 315760bc48eSAndrew Thompson struct usb_fs_init fs_init; 316760bc48eSAndrew Thompson struct usb_fs_endpoint *pfse; 317df4b8c2aSAndrew Thompson int error; 318df4b8c2aSAndrew Thompson uint32_t size; 319df4b8c2aSAndrew Thompson uint16_t nMaxTransfer; 320df4b8c2aSAndrew Thompson 321df4b8c2aSAndrew Thompson nMaxTransfer = pdev->nTransfer; 322df4b8c2aSAndrew Thompson error = 0; 323df4b8c2aSAndrew Thompson 324df4b8c2aSAndrew Thompson if (nMaxTransfer == 0) { 325df4b8c2aSAndrew Thompson goto done; 326df4b8c2aSAndrew Thompson } 327df4b8c2aSAndrew Thompson size = nMaxTransfer * sizeof(*pfse); 328df4b8c2aSAndrew Thompson 329df4b8c2aSAndrew Thompson if (pdev->privBeData == NULL) { 330df4b8c2aSAndrew Thompson pfse = malloc(size); 331df4b8c2aSAndrew Thompson if (pfse == NULL) { 332df4b8c2aSAndrew Thompson error = LIBUSB20_ERROR_NO_MEM; 333df4b8c2aSAndrew Thompson goto done; 334df4b8c2aSAndrew Thompson } 335df4b8c2aSAndrew Thompson pdev->privBeData = pfse; 336df4b8c2aSAndrew Thompson } 337df4b8c2aSAndrew Thompson /* reset endpoint data */ 338df4b8c2aSAndrew Thompson memset(pdev->privBeData, 0, size); 339df4b8c2aSAndrew Thompson 340df4b8c2aSAndrew Thompson memset(&fs_init, 0, sizeof(fs_init)); 341df4b8c2aSAndrew Thompson 342df4b8c2aSAndrew Thompson fs_init.pEndpoints = pdev->privBeData; 343df4b8c2aSAndrew Thompson fs_init.ep_index_max = nMaxTransfer; 344df4b8c2aSAndrew Thompson 345df4b8c2aSAndrew Thompson if (ioctl(pdev->file, USB_FS_INIT, &fs_init)) { 346df4b8c2aSAndrew Thompson error = LIBUSB20_ERROR_OTHER; 347df4b8c2aSAndrew Thompson goto done; 348df4b8c2aSAndrew Thompson } 349df4b8c2aSAndrew Thompson done: 350df4b8c2aSAndrew Thompson return (error); 351df4b8c2aSAndrew Thompson } 352df4b8c2aSAndrew Thompson 353df4b8c2aSAndrew Thompson static int 354df4b8c2aSAndrew Thompson ugen20_open_device(struct libusb20_device *pdev, uint16_t nMaxTransfer) 355df4b8c2aSAndrew Thompson { 356df4b8c2aSAndrew Thompson uint32_t plugtime; 357df4b8c2aSAndrew Thompson char buf[64]; 358df4b8c2aSAndrew Thompson int f; 359df4b8c2aSAndrew Thompson int g; 360df4b8c2aSAndrew Thompson int error; 361df4b8c2aSAndrew Thompson 362df4b8c2aSAndrew Thompson snprintf(buf, sizeof(buf), "/dev/" USB_GENERIC_NAME "%u.%u", 363df4b8c2aSAndrew Thompson pdev->bus_number, pdev->device_address); 364df4b8c2aSAndrew Thompson 365df4b8c2aSAndrew Thompson /* 366df4b8c2aSAndrew Thompson * We need two file handles, one for the control endpoint and one 367df4b8c2aSAndrew Thompson * for BULK, INTERRUPT and ISOCHRONOUS transactions due to optimised 368df4b8c2aSAndrew Thompson * kernel locking. 369df4b8c2aSAndrew Thompson */ 370df4b8c2aSAndrew Thompson g = open(buf, O_RDWR); 371df4b8c2aSAndrew Thompson if (g < 0) { 372df4b8c2aSAndrew Thompson return (LIBUSB20_ERROR_NO_DEVICE); 373df4b8c2aSAndrew Thompson } 374df4b8c2aSAndrew Thompson f = open(buf, O_RDWR); 375df4b8c2aSAndrew Thompson if (f < 0) { 376df4b8c2aSAndrew Thompson close(g); 377df4b8c2aSAndrew Thompson return (LIBUSB20_ERROR_NO_DEVICE); 378df4b8c2aSAndrew Thompson } 379df4b8c2aSAndrew Thompson if (ioctl(f, USB_GET_PLUGTIME, &plugtime)) { 380df4b8c2aSAndrew Thompson error = LIBUSB20_ERROR_OTHER; 381df4b8c2aSAndrew Thompson goto done; 382df4b8c2aSAndrew Thompson } 383df4b8c2aSAndrew Thompson /* check that the correct device is still plugged */ 384df4b8c2aSAndrew Thompson if (pdev->session_data.plugtime != plugtime) { 385df4b8c2aSAndrew Thompson error = LIBUSB20_ERROR_NO_DEVICE; 386df4b8c2aSAndrew Thompson goto done; 387df4b8c2aSAndrew Thompson } 388df4b8c2aSAndrew Thompson /* need to set this before "tr_renew()" */ 389df4b8c2aSAndrew Thompson pdev->file = f; 390df4b8c2aSAndrew Thompson pdev->file_ctrl = g; 391df4b8c2aSAndrew Thompson 392df4b8c2aSAndrew Thompson /* renew all USB transfers */ 393df4b8c2aSAndrew Thompson error = ugen20_tr_renew(pdev); 394df4b8c2aSAndrew Thompson if (error) { 395df4b8c2aSAndrew Thompson goto done; 396df4b8c2aSAndrew Thompson } 397df4b8c2aSAndrew Thompson /* set methods */ 398df4b8c2aSAndrew Thompson pdev->methods = &libusb20_ugen20_device_methods; 399df4b8c2aSAndrew Thompson 400df4b8c2aSAndrew Thompson done: 401df4b8c2aSAndrew Thompson if (error) { 402df4b8c2aSAndrew Thompson if (pdev->privBeData) { 403df4b8c2aSAndrew Thompson /* cleanup after "tr_renew()" */ 404df4b8c2aSAndrew Thompson free(pdev->privBeData); 405df4b8c2aSAndrew Thompson pdev->privBeData = NULL; 406df4b8c2aSAndrew Thompson } 407df4b8c2aSAndrew Thompson pdev->file = -1; 408df4b8c2aSAndrew Thompson pdev->file_ctrl = -1; 409df4b8c2aSAndrew Thompson close(f); 410df4b8c2aSAndrew Thompson close(g); 411df4b8c2aSAndrew Thompson } 412df4b8c2aSAndrew Thompson return (error); 413df4b8c2aSAndrew Thompson } 414df4b8c2aSAndrew Thompson 415df4b8c2aSAndrew Thompson static int 416df4b8c2aSAndrew Thompson ugen20_close_device(struct libusb20_device *pdev) 417df4b8c2aSAndrew Thompson { 418760bc48eSAndrew Thompson struct usb_fs_uninit fs_uninit; 419df4b8c2aSAndrew Thompson 420df4b8c2aSAndrew Thompson if (pdev->privBeData) { 421df4b8c2aSAndrew Thompson memset(&fs_uninit, 0, sizeof(fs_uninit)); 422df4b8c2aSAndrew Thompson if (ioctl(pdev->file, USB_FS_UNINIT, &fs_uninit)) { 423df4b8c2aSAndrew Thompson /* ignore this error */ 424df4b8c2aSAndrew Thompson } 425df4b8c2aSAndrew Thompson free(pdev->privBeData); 426df4b8c2aSAndrew Thompson } 427df4b8c2aSAndrew Thompson pdev->nTransfer = 0; 428df4b8c2aSAndrew Thompson pdev->privBeData = NULL; 429df4b8c2aSAndrew Thompson close(pdev->file); 430df4b8c2aSAndrew Thompson close(pdev->file_ctrl); 431df4b8c2aSAndrew Thompson pdev->file = -1; 432df4b8c2aSAndrew Thompson pdev->file_ctrl = -1; 433df4b8c2aSAndrew Thompson return (0); /* success */ 434df4b8c2aSAndrew Thompson } 435df4b8c2aSAndrew Thompson 436df4b8c2aSAndrew Thompson static void 437df4b8c2aSAndrew Thompson ugen20_exit_backend(struct libusb20_backend *pbe) 438df4b8c2aSAndrew Thompson { 439df4b8c2aSAndrew Thompson return; /* nothing to do */ 440df4b8c2aSAndrew Thompson } 441df4b8c2aSAndrew Thompson 442df4b8c2aSAndrew Thompson static int 443df4b8c2aSAndrew Thompson ugen20_get_config_desc_full(struct libusb20_device *pdev, 444df4b8c2aSAndrew Thompson uint8_t **ppbuf, uint16_t *plen, uint8_t cfg_index) 445df4b8c2aSAndrew Thompson { 446760bc48eSAndrew Thompson struct usb_gen_descriptor gen_desc; 447760bc48eSAndrew Thompson struct usb_config_descriptor cdesc; 448df4b8c2aSAndrew Thompson uint8_t *ptr; 449df4b8c2aSAndrew Thompson uint16_t len; 450df4b8c2aSAndrew Thompson int error; 451df4b8c2aSAndrew Thompson 452df4b8c2aSAndrew Thompson memset(&gen_desc, 0, sizeof(gen_desc)); 453df4b8c2aSAndrew Thompson 454df4b8c2aSAndrew Thompson gen_desc.ugd_data = &cdesc; 455df4b8c2aSAndrew Thompson gen_desc.ugd_maxlen = sizeof(cdesc); 456df4b8c2aSAndrew Thompson gen_desc.ugd_config_index = cfg_index; 457df4b8c2aSAndrew Thompson 458df4b8c2aSAndrew Thompson error = ioctl(pdev->file_ctrl, USB_GET_FULL_DESC, &gen_desc); 459df4b8c2aSAndrew Thompson if (error) { 460df4b8c2aSAndrew Thompson return (LIBUSB20_ERROR_OTHER); 461df4b8c2aSAndrew Thompson } 462df4b8c2aSAndrew Thompson len = UGETW(cdesc.wTotalLength); 463df4b8c2aSAndrew Thompson if (len < sizeof(cdesc)) { 464df4b8c2aSAndrew Thompson /* corrupt descriptor */ 465df4b8c2aSAndrew Thompson return (LIBUSB20_ERROR_OTHER); 466df4b8c2aSAndrew Thompson } 467df4b8c2aSAndrew Thompson ptr = malloc(len); 468df4b8c2aSAndrew Thompson if (!ptr) { 469df4b8c2aSAndrew Thompson return (LIBUSB20_ERROR_NO_MEM); 470df4b8c2aSAndrew Thompson } 471df4b8c2aSAndrew Thompson gen_desc.ugd_data = ptr; 472df4b8c2aSAndrew Thompson gen_desc.ugd_maxlen = len; 473df4b8c2aSAndrew Thompson 474df4b8c2aSAndrew Thompson error = ioctl(pdev->file_ctrl, USB_GET_FULL_DESC, &gen_desc); 475df4b8c2aSAndrew Thompson if (error) { 476df4b8c2aSAndrew Thompson free(ptr); 477df4b8c2aSAndrew Thompson return (LIBUSB20_ERROR_OTHER); 478df4b8c2aSAndrew Thompson } 479df4b8c2aSAndrew Thompson /* make sure that the device doesn't fool us */ 480df4b8c2aSAndrew Thompson memcpy(ptr, &cdesc, sizeof(cdesc)); 481df4b8c2aSAndrew Thompson 482df4b8c2aSAndrew Thompson *ppbuf = ptr; 483df4b8c2aSAndrew Thompson *plen = len; 484df4b8c2aSAndrew Thompson 485df4b8c2aSAndrew Thompson return (0); /* success */ 486df4b8c2aSAndrew Thompson } 487df4b8c2aSAndrew Thompson 488df4b8c2aSAndrew Thompson static int 489df4b8c2aSAndrew Thompson ugen20_get_config_index(struct libusb20_device *pdev, uint8_t *pindex) 490df4b8c2aSAndrew Thompson { 491df4b8c2aSAndrew Thompson int temp; 492df4b8c2aSAndrew Thompson 493df4b8c2aSAndrew Thompson if (ioctl(pdev->file_ctrl, USB_GET_CONFIG, &temp)) { 494df4b8c2aSAndrew Thompson return (LIBUSB20_ERROR_OTHER); 495df4b8c2aSAndrew Thompson } 496df4b8c2aSAndrew Thompson *pindex = temp; 497df4b8c2aSAndrew Thompson 498df4b8c2aSAndrew Thompson return (0); 499df4b8c2aSAndrew Thompson } 500df4b8c2aSAndrew Thompson 501df4b8c2aSAndrew Thompson static int 502df4b8c2aSAndrew Thompson ugen20_set_config_index(struct libusb20_device *pdev, uint8_t cfg_index) 503df4b8c2aSAndrew Thompson { 504df4b8c2aSAndrew Thompson int temp = cfg_index; 505df4b8c2aSAndrew Thompson 506df4b8c2aSAndrew Thompson /* release all active USB transfers */ 507df4b8c2aSAndrew Thompson ugen20_tr_release(pdev); 508df4b8c2aSAndrew Thompson 509df4b8c2aSAndrew Thompson if (ioctl(pdev->file_ctrl, USB_SET_CONFIG, &temp)) { 510df4b8c2aSAndrew Thompson return (LIBUSB20_ERROR_OTHER); 511df4b8c2aSAndrew Thompson } 512df4b8c2aSAndrew Thompson return (ugen20_tr_renew(pdev)); 513df4b8c2aSAndrew Thompson } 514df4b8c2aSAndrew Thompson 515df4b8c2aSAndrew Thompson static int 516df4b8c2aSAndrew Thompson ugen20_set_alt_index(struct libusb20_device *pdev, 517df4b8c2aSAndrew Thompson uint8_t iface_index, uint8_t alt_index) 518df4b8c2aSAndrew Thompson { 519760bc48eSAndrew Thompson struct usb_alt_interface alt_iface; 520df4b8c2aSAndrew Thompson 521df4b8c2aSAndrew Thompson memset(&alt_iface, 0, sizeof(alt_iface)); 522df4b8c2aSAndrew Thompson 523df4b8c2aSAndrew Thompson alt_iface.uai_interface_index = iface_index; 524df4b8c2aSAndrew Thompson alt_iface.uai_alt_index = alt_index; 525df4b8c2aSAndrew Thompson 526df4b8c2aSAndrew Thompson /* release all active USB transfers */ 527df4b8c2aSAndrew Thompson ugen20_tr_release(pdev); 528df4b8c2aSAndrew Thompson 529df4b8c2aSAndrew Thompson if (ioctl(pdev->file_ctrl, USB_SET_ALTINTERFACE, &alt_iface)) { 530df4b8c2aSAndrew Thompson return (LIBUSB20_ERROR_OTHER); 531df4b8c2aSAndrew Thompson } 532df4b8c2aSAndrew Thompson return (ugen20_tr_renew(pdev)); 533df4b8c2aSAndrew Thompson } 534df4b8c2aSAndrew Thompson 535df4b8c2aSAndrew Thompson static int 536df4b8c2aSAndrew Thompson ugen20_reset_device(struct libusb20_device *pdev) 537df4b8c2aSAndrew Thompson { 538df4b8c2aSAndrew Thompson int temp = 0; 539df4b8c2aSAndrew Thompson 540df4b8c2aSAndrew Thompson /* release all active USB transfers */ 541df4b8c2aSAndrew Thompson ugen20_tr_release(pdev); 542df4b8c2aSAndrew Thompson 543df4b8c2aSAndrew Thompson if (ioctl(pdev->file_ctrl, USB_DEVICEENUMERATE, &temp)) { 544df4b8c2aSAndrew Thompson return (LIBUSB20_ERROR_OTHER); 545df4b8c2aSAndrew Thompson } 546df4b8c2aSAndrew Thompson return (ugen20_tr_renew(pdev)); 547df4b8c2aSAndrew Thompson } 548df4b8c2aSAndrew Thompson 549df4b8c2aSAndrew Thompson static int 550df4b8c2aSAndrew Thompson ugen20_set_power_mode(struct libusb20_device *pdev, uint8_t power_mode) 551df4b8c2aSAndrew Thompson { 552df4b8c2aSAndrew Thompson int temp; 553df4b8c2aSAndrew Thompson 554df4b8c2aSAndrew Thompson switch (power_mode) { 555df4b8c2aSAndrew Thompson case LIBUSB20_POWER_OFF: 556df4b8c2aSAndrew Thompson temp = USB_POWER_MODE_OFF; 557df4b8c2aSAndrew Thompson break; 558df4b8c2aSAndrew Thompson case LIBUSB20_POWER_ON: 559df4b8c2aSAndrew Thompson temp = USB_POWER_MODE_ON; 560df4b8c2aSAndrew Thompson break; 561df4b8c2aSAndrew Thompson case LIBUSB20_POWER_SAVE: 562df4b8c2aSAndrew Thompson temp = USB_POWER_MODE_SAVE; 563df4b8c2aSAndrew Thompson break; 564df4b8c2aSAndrew Thompson case LIBUSB20_POWER_SUSPEND: 565df4b8c2aSAndrew Thompson temp = USB_POWER_MODE_SUSPEND; 566df4b8c2aSAndrew Thompson break; 567df4b8c2aSAndrew Thompson case LIBUSB20_POWER_RESUME: 568df4b8c2aSAndrew Thompson temp = USB_POWER_MODE_RESUME; 569df4b8c2aSAndrew Thompson break; 570df4b8c2aSAndrew Thompson default: 571df4b8c2aSAndrew Thompson return (LIBUSB20_ERROR_INVALID_PARAM); 572df4b8c2aSAndrew Thompson } 573df4b8c2aSAndrew Thompson if (ioctl(pdev->file_ctrl, USB_SET_POWER_MODE, &temp)) { 574df4b8c2aSAndrew Thompson return (LIBUSB20_ERROR_OTHER); 575df4b8c2aSAndrew Thompson } 576df4b8c2aSAndrew Thompson return (0); 577df4b8c2aSAndrew Thompson } 578df4b8c2aSAndrew Thompson 579df4b8c2aSAndrew Thompson static int 580df4b8c2aSAndrew Thompson ugen20_get_power_mode(struct libusb20_device *pdev, uint8_t *power_mode) 581df4b8c2aSAndrew Thompson { 582df4b8c2aSAndrew Thompson int temp; 583df4b8c2aSAndrew Thompson 584df4b8c2aSAndrew Thompson if (ioctl(pdev->file_ctrl, USB_GET_POWER_MODE, &temp)) { 585df4b8c2aSAndrew Thompson return (LIBUSB20_ERROR_OTHER); 586df4b8c2aSAndrew Thompson } 587df4b8c2aSAndrew Thompson switch (temp) { 588df4b8c2aSAndrew Thompson case USB_POWER_MODE_OFF: 589df4b8c2aSAndrew Thompson temp = LIBUSB20_POWER_OFF; 590df4b8c2aSAndrew Thompson break; 591df4b8c2aSAndrew Thompson case USB_POWER_MODE_ON: 592df4b8c2aSAndrew Thompson temp = LIBUSB20_POWER_ON; 593df4b8c2aSAndrew Thompson break; 594df4b8c2aSAndrew Thompson case USB_POWER_MODE_SAVE: 595df4b8c2aSAndrew Thompson temp = LIBUSB20_POWER_SAVE; 596df4b8c2aSAndrew Thompson break; 597df4b8c2aSAndrew Thompson case USB_POWER_MODE_SUSPEND: 598df4b8c2aSAndrew Thompson temp = LIBUSB20_POWER_SUSPEND; 599df4b8c2aSAndrew Thompson break; 600df4b8c2aSAndrew Thompson case USB_POWER_MODE_RESUME: 601df4b8c2aSAndrew Thompson temp = LIBUSB20_POWER_RESUME; 602df4b8c2aSAndrew Thompson break; 603df4b8c2aSAndrew Thompson default: 604df4b8c2aSAndrew Thompson temp = LIBUSB20_POWER_ON; 605df4b8c2aSAndrew Thompson break; 606df4b8c2aSAndrew Thompson } 607df4b8c2aSAndrew Thompson *power_mode = temp; 608df4b8c2aSAndrew Thompson return (0); /* success */ 609df4b8c2aSAndrew Thompson } 610df4b8c2aSAndrew Thompson 611df4b8c2aSAndrew Thompson static int 612df4b8c2aSAndrew Thompson ugen20_kernel_driver_active(struct libusb20_device *pdev, 613df4b8c2aSAndrew Thompson uint8_t iface_index) 614df4b8c2aSAndrew Thompson { 615df4b8c2aSAndrew Thompson int temp = iface_index; 616df4b8c2aSAndrew Thompson 617df4b8c2aSAndrew Thompson if (ioctl(pdev->file_ctrl, USB_IFACE_DRIVER_ACTIVE, &temp)) { 618df4b8c2aSAndrew Thompson return (LIBUSB20_ERROR_OTHER); 619df4b8c2aSAndrew Thompson } 620df4b8c2aSAndrew Thompson return (0); /* kernel driver is active */ 621df4b8c2aSAndrew Thompson } 622df4b8c2aSAndrew Thompson 623df4b8c2aSAndrew Thompson static int 624df4b8c2aSAndrew Thompson ugen20_detach_kernel_driver(struct libusb20_device *pdev, 625df4b8c2aSAndrew Thompson uint8_t iface_index) 626df4b8c2aSAndrew Thompson { 627df4b8c2aSAndrew Thompson int temp = iface_index; 628df4b8c2aSAndrew Thompson 629df4b8c2aSAndrew Thompson if (ioctl(pdev->file_ctrl, USB_IFACE_DRIVER_DETACH, &temp)) { 630df4b8c2aSAndrew Thompson return (LIBUSB20_ERROR_OTHER); 631df4b8c2aSAndrew Thompson } 632df4b8c2aSAndrew Thompson return (0); /* kernel driver is active */ 633df4b8c2aSAndrew Thompson } 634df4b8c2aSAndrew Thompson 635df4b8c2aSAndrew Thompson static int 636df4b8c2aSAndrew Thompson ugen20_do_request_sync(struct libusb20_device *pdev, 637df4b8c2aSAndrew Thompson struct LIBUSB20_CONTROL_SETUP_DECODED *setup, 638df4b8c2aSAndrew Thompson void *data, uint16_t *pactlen, uint32_t timeout, uint8_t flags) 639df4b8c2aSAndrew Thompson { 640760bc48eSAndrew Thompson struct usb_ctl_request req; 641df4b8c2aSAndrew Thompson 642df4b8c2aSAndrew Thompson memset(&req, 0, sizeof(req)); 643df4b8c2aSAndrew Thompson 644df4b8c2aSAndrew Thompson req.ucr_data = data; 645df4b8c2aSAndrew Thompson if (!(flags & LIBUSB20_TRANSFER_SINGLE_SHORT_NOT_OK)) { 646df4b8c2aSAndrew Thompson req.ucr_flags |= USB_SHORT_XFER_OK; 647df4b8c2aSAndrew Thompson } 648df4b8c2aSAndrew Thompson if (libusb20_me_encode(&req.ucr_request, 649df4b8c2aSAndrew Thompson sizeof(req.ucr_request), setup)) { 650df4b8c2aSAndrew Thompson /* ignore */ 651df4b8c2aSAndrew Thompson } 652df4b8c2aSAndrew Thompson if (ioctl(pdev->file_ctrl, USB_DO_REQUEST, &req)) { 653df4b8c2aSAndrew Thompson return (LIBUSB20_ERROR_OTHER); 654df4b8c2aSAndrew Thompson } 655df4b8c2aSAndrew Thompson if (pactlen) { 656df4b8c2aSAndrew Thompson /* get actual length */ 657df4b8c2aSAndrew Thompson *pactlen = req.ucr_actlen; 658df4b8c2aSAndrew Thompson } 659df4b8c2aSAndrew Thompson return (0); /* kernel driver is active */ 660df4b8c2aSAndrew Thompson } 661df4b8c2aSAndrew Thompson 662df4b8c2aSAndrew Thompson static int 663df4b8c2aSAndrew Thompson ugen20_process(struct libusb20_device *pdev) 664df4b8c2aSAndrew Thompson { 665760bc48eSAndrew Thompson struct usb_fs_complete temp; 666760bc48eSAndrew Thompson struct usb_fs_endpoint *fsep; 667df4b8c2aSAndrew Thompson struct libusb20_transfer *xfer; 668df4b8c2aSAndrew Thompson 669df4b8c2aSAndrew Thompson while (1) { 670df4b8c2aSAndrew Thompson 671df4b8c2aSAndrew Thompson if (ioctl(pdev->file, USB_FS_COMPLETE, &temp)) { 672df4b8c2aSAndrew Thompson if (errno == EBUSY) { 673df4b8c2aSAndrew Thompson break; 674df4b8c2aSAndrew Thompson } else { 675df4b8c2aSAndrew Thompson /* device detached */ 676df4b8c2aSAndrew Thompson return (LIBUSB20_ERROR_OTHER); 677df4b8c2aSAndrew Thompson } 678df4b8c2aSAndrew Thompson } 679df4b8c2aSAndrew Thompson fsep = pdev->privBeData; 680df4b8c2aSAndrew Thompson xfer = pdev->pTransfer; 681df4b8c2aSAndrew Thompson fsep += temp.ep_index; 682df4b8c2aSAndrew Thompson xfer += temp.ep_index; 683df4b8c2aSAndrew Thompson 684df4b8c2aSAndrew Thompson /* update transfer status */ 685df4b8c2aSAndrew Thompson 686df4b8c2aSAndrew Thompson if (fsep->status == 0) { 687df4b8c2aSAndrew Thompson xfer->aFrames = fsep->aFrames; 688df4b8c2aSAndrew Thompson xfer->timeComplete = fsep->isoc_time_complete; 689df4b8c2aSAndrew Thompson xfer->status = LIBUSB20_TRANSFER_COMPLETED; 690df4b8c2aSAndrew Thompson } else if (fsep->status == USB_ERR_CANCELLED) { 691df4b8c2aSAndrew Thompson xfer->aFrames = 0; 692df4b8c2aSAndrew Thompson xfer->timeComplete = 0; 693df4b8c2aSAndrew Thompson xfer->status = LIBUSB20_TRANSFER_CANCELLED; 694df4b8c2aSAndrew Thompson } else if (fsep->status == USB_ERR_STALLED) { 695df4b8c2aSAndrew Thompson xfer->aFrames = 0; 696df4b8c2aSAndrew Thompson xfer->timeComplete = 0; 697df4b8c2aSAndrew Thompson xfer->status = LIBUSB20_TRANSFER_STALL; 698df4b8c2aSAndrew Thompson } else if (fsep->status == USB_ERR_TIMEOUT) { 699df4b8c2aSAndrew Thompson xfer->aFrames = 0; 700df4b8c2aSAndrew Thompson xfer->timeComplete = 0; 701df4b8c2aSAndrew Thompson xfer->status = LIBUSB20_TRANSFER_TIMED_OUT; 702df4b8c2aSAndrew Thompson } else { 703df4b8c2aSAndrew Thompson xfer->aFrames = 0; 704df4b8c2aSAndrew Thompson xfer->timeComplete = 0; 705df4b8c2aSAndrew Thompson xfer->status = LIBUSB20_TRANSFER_ERROR; 706df4b8c2aSAndrew Thompson } 707df4b8c2aSAndrew Thompson libusb20_tr_callback_wrapper(xfer); 708df4b8c2aSAndrew Thompson } 709df4b8c2aSAndrew Thompson return (0); /* done */ 710df4b8c2aSAndrew Thompson } 711df4b8c2aSAndrew Thompson 712df4b8c2aSAndrew Thompson static int 713df4b8c2aSAndrew Thompson ugen20_tr_open(struct libusb20_transfer *xfer, uint32_t MaxBufSize, 714df4b8c2aSAndrew Thompson uint32_t MaxFrameCount, uint8_t ep_no) 715df4b8c2aSAndrew Thompson { 716760bc48eSAndrew Thompson struct usb_fs_open temp; 717760bc48eSAndrew Thompson struct usb_fs_endpoint *fsep; 718df4b8c2aSAndrew Thompson 719df4b8c2aSAndrew Thompson memset(&temp, 0, sizeof(temp)); 720df4b8c2aSAndrew Thompson 721df4b8c2aSAndrew Thompson fsep = xfer->pdev->privBeData; 722df4b8c2aSAndrew Thompson fsep += xfer->trIndex; 723df4b8c2aSAndrew Thompson 724df4b8c2aSAndrew Thompson temp.max_bufsize = MaxBufSize; 725df4b8c2aSAndrew Thompson temp.max_frames = MaxFrameCount; 726df4b8c2aSAndrew Thompson temp.ep_index = xfer->trIndex; 727df4b8c2aSAndrew Thompson temp.ep_no = ep_no; 728df4b8c2aSAndrew Thompson 729df4b8c2aSAndrew Thompson if (ioctl(xfer->pdev->file, USB_FS_OPEN, &temp)) { 730df4b8c2aSAndrew Thompson return (LIBUSB20_ERROR_INVALID_PARAM); 731df4b8c2aSAndrew Thompson } 732df4b8c2aSAndrew Thompson /* maximums might have changed - update */ 733df4b8c2aSAndrew Thompson xfer->maxFrames = temp.max_frames; 734df4b8c2aSAndrew Thompson 735df4b8c2aSAndrew Thompson /* "max_bufsize" should be multiple of "max_packet_length" */ 736df4b8c2aSAndrew Thompson xfer->maxTotalLength = temp.max_bufsize; 737df4b8c2aSAndrew Thompson xfer->maxPacketLen = temp.max_packet_length; 738df4b8c2aSAndrew Thompson 739df4b8c2aSAndrew Thompson /* setup buffer and length lists */ 740df4b8c2aSAndrew Thompson fsep->ppBuffer = xfer->ppBuffer;/* zero copy */ 741df4b8c2aSAndrew Thompson fsep->pLength = xfer->pLength; /* zero copy */ 742df4b8c2aSAndrew Thompson 743df4b8c2aSAndrew Thompson return (0); /* success */ 744df4b8c2aSAndrew Thompson } 745df4b8c2aSAndrew Thompson 746df4b8c2aSAndrew Thompson static int 747df4b8c2aSAndrew Thompson ugen20_tr_close(struct libusb20_transfer *xfer) 748df4b8c2aSAndrew Thompson { 749760bc48eSAndrew Thompson struct usb_fs_close temp; 750df4b8c2aSAndrew Thompson 751df4b8c2aSAndrew Thompson memset(&temp, 0, sizeof(temp)); 752df4b8c2aSAndrew Thompson 753df4b8c2aSAndrew Thompson temp.ep_index = xfer->trIndex; 754df4b8c2aSAndrew Thompson 755df4b8c2aSAndrew Thompson if (ioctl(xfer->pdev->file, USB_FS_CLOSE, &temp)) { 756df4b8c2aSAndrew Thompson return (LIBUSB20_ERROR_INVALID_PARAM); 757df4b8c2aSAndrew Thompson } 758df4b8c2aSAndrew Thompson return (0); /* success */ 759df4b8c2aSAndrew Thompson } 760df4b8c2aSAndrew Thompson 761df4b8c2aSAndrew Thompson static int 762df4b8c2aSAndrew Thompson ugen20_tr_clear_stall_sync(struct libusb20_transfer *xfer) 763df4b8c2aSAndrew Thompson { 764760bc48eSAndrew Thompson struct usb_fs_clear_stall_sync temp; 765df4b8c2aSAndrew Thompson 766df4b8c2aSAndrew Thompson memset(&temp, 0, sizeof(temp)); 767df4b8c2aSAndrew Thompson 768df4b8c2aSAndrew Thompson /* if the transfer is active, an error will be returned */ 769df4b8c2aSAndrew Thompson 770df4b8c2aSAndrew Thompson temp.ep_index = xfer->trIndex; 771df4b8c2aSAndrew Thompson 772df4b8c2aSAndrew Thompson if (ioctl(xfer->pdev->file, USB_FS_CLEAR_STALL_SYNC, &temp)) { 773df4b8c2aSAndrew Thompson return (LIBUSB20_ERROR_INVALID_PARAM); 774df4b8c2aSAndrew Thompson } 775df4b8c2aSAndrew Thompson return (0); /* success */ 776df4b8c2aSAndrew Thompson } 777df4b8c2aSAndrew Thompson 778df4b8c2aSAndrew Thompson static void 779df4b8c2aSAndrew Thompson ugen20_tr_submit(struct libusb20_transfer *xfer) 780df4b8c2aSAndrew Thompson { 781760bc48eSAndrew Thompson struct usb_fs_start temp; 782760bc48eSAndrew Thompson struct usb_fs_endpoint *fsep; 783df4b8c2aSAndrew Thompson 784df4b8c2aSAndrew Thompson memset(&temp, 0, sizeof(temp)); 785df4b8c2aSAndrew Thompson 786df4b8c2aSAndrew Thompson fsep = xfer->pdev->privBeData; 787df4b8c2aSAndrew Thompson fsep += xfer->trIndex; 788df4b8c2aSAndrew Thompson 789df4b8c2aSAndrew Thompson fsep->nFrames = xfer->nFrames; 790df4b8c2aSAndrew Thompson fsep->flags = 0; 791df4b8c2aSAndrew Thompson if (!(xfer->flags & LIBUSB20_TRANSFER_SINGLE_SHORT_NOT_OK)) { 792df4b8c2aSAndrew Thompson fsep->flags |= USB_FS_FLAG_SINGLE_SHORT_OK; 793df4b8c2aSAndrew Thompson } 794df4b8c2aSAndrew Thompson if (!(xfer->flags & LIBUSB20_TRANSFER_MULTI_SHORT_NOT_OK)) { 795df4b8c2aSAndrew Thompson fsep->flags |= USB_FS_FLAG_MULTI_SHORT_OK; 796df4b8c2aSAndrew Thompson } 797df4b8c2aSAndrew Thompson if (xfer->flags & LIBUSB20_TRANSFER_FORCE_SHORT) { 798df4b8c2aSAndrew Thompson fsep->flags |= USB_FS_FLAG_FORCE_SHORT; 799df4b8c2aSAndrew Thompson } 800df4b8c2aSAndrew Thompson if (xfer->flags & LIBUSB20_TRANSFER_DO_CLEAR_STALL) { 801df4b8c2aSAndrew Thompson fsep->flags |= USB_FS_FLAG_CLEAR_STALL; 802df4b8c2aSAndrew Thompson } 803df4b8c2aSAndrew Thompson fsep->timeout = xfer->timeout; 804df4b8c2aSAndrew Thompson 805df4b8c2aSAndrew Thompson temp.ep_index = xfer->trIndex; 806df4b8c2aSAndrew Thompson 807df4b8c2aSAndrew Thompson if (ioctl(xfer->pdev->file, USB_FS_START, &temp)) { 808df4b8c2aSAndrew Thompson /* ignore any errors - should never happen */ 809df4b8c2aSAndrew Thompson } 810df4b8c2aSAndrew Thompson return; /* success */ 811df4b8c2aSAndrew Thompson } 812df4b8c2aSAndrew Thompson 813df4b8c2aSAndrew Thompson static void 814df4b8c2aSAndrew Thompson ugen20_tr_cancel_async(struct libusb20_transfer *xfer) 815df4b8c2aSAndrew Thompson { 816760bc48eSAndrew Thompson struct usb_fs_stop temp; 817df4b8c2aSAndrew Thompson 818df4b8c2aSAndrew Thompson memset(&temp, 0, sizeof(temp)); 819df4b8c2aSAndrew Thompson 820df4b8c2aSAndrew Thompson temp.ep_index = xfer->trIndex; 821df4b8c2aSAndrew Thompson 822df4b8c2aSAndrew Thompson if (ioctl(xfer->pdev->file, USB_FS_STOP, &temp)) { 823df4b8c2aSAndrew Thompson /* ignore any errors - should never happen */ 824df4b8c2aSAndrew Thompson } 825df4b8c2aSAndrew Thompson return; 826df4b8c2aSAndrew Thompson } 827df4b8c2aSAndrew Thompson 828df4b8c2aSAndrew Thompson static int 829df4b8c2aSAndrew Thompson ugen20_be_ioctl(uint32_t cmd, void *data) 830df4b8c2aSAndrew Thompson { 831df4b8c2aSAndrew Thompson int f; 832df4b8c2aSAndrew Thompson int error; 833df4b8c2aSAndrew Thompson 834df4b8c2aSAndrew Thompson f = open("/dev/" USB_DEVICE_NAME, O_RDONLY); 835df4b8c2aSAndrew Thompson if (f < 0) 836df4b8c2aSAndrew Thompson return (LIBUSB20_ERROR_OTHER); 837df4b8c2aSAndrew Thompson error = ioctl(f, cmd, data); 838df4b8c2aSAndrew Thompson if (error == -1) { 839df4b8c2aSAndrew Thompson if (errno == EPERM) { 840df4b8c2aSAndrew Thompson error = LIBUSB20_ERROR_ACCESS; 841df4b8c2aSAndrew Thompson } else { 842df4b8c2aSAndrew Thompson error = LIBUSB20_ERROR_OTHER; 843df4b8c2aSAndrew Thompson } 844df4b8c2aSAndrew Thompson } 845df4b8c2aSAndrew Thompson close(f); 846df4b8c2aSAndrew Thompson return (error); 847df4b8c2aSAndrew Thompson } 848df4b8c2aSAndrew Thompson 849df4b8c2aSAndrew Thompson static int 850df4b8c2aSAndrew Thompson ugen20_dev_get_iface_desc(struct libusb20_device *pdev, 851df4b8c2aSAndrew Thompson uint8_t iface_index, char *buf, uint8_t len) 852df4b8c2aSAndrew Thompson { 853760bc48eSAndrew Thompson struct usb_gen_descriptor ugd; 854df4b8c2aSAndrew Thompson 855df4b8c2aSAndrew Thompson memset(&ugd, 0, sizeof(ugd)); 856df4b8c2aSAndrew Thompson 857df4b8c2aSAndrew Thompson ugd.ugd_data = buf; 858df4b8c2aSAndrew Thompson ugd.ugd_maxlen = len; 859df4b8c2aSAndrew Thompson ugd.ugd_iface_index = iface_index; 860df4b8c2aSAndrew Thompson 861df4b8c2aSAndrew Thompson if (ioctl(pdev->file, USB_GET_IFACE_DRIVER, &ugd)) { 862df4b8c2aSAndrew Thompson return (LIBUSB20_ERROR_INVALID_PARAM); 863df4b8c2aSAndrew Thompson } 864df4b8c2aSAndrew Thompson return (0); 865df4b8c2aSAndrew Thompson } 866df4b8c2aSAndrew Thompson 867df4b8c2aSAndrew Thompson static int 868df4b8c2aSAndrew Thompson ugen20_dev_get_info(struct libusb20_device *pdev, 869760bc48eSAndrew Thompson struct usb_device_info *pinfo) 870df4b8c2aSAndrew Thompson { 871df4b8c2aSAndrew Thompson if (ioctl(pdev->file, USB_GET_DEVICEINFO, pinfo)) { 872df4b8c2aSAndrew Thompson return (LIBUSB20_ERROR_INVALID_PARAM); 873df4b8c2aSAndrew Thompson } 874df4b8c2aSAndrew Thompson return (0); 875df4b8c2aSAndrew Thompson } 876df4b8c2aSAndrew Thompson 877df4b8c2aSAndrew Thompson static int 878df4b8c2aSAndrew Thompson ugen20_root_get_dev_quirk(struct libusb20_backend *pbe, 879df4b8c2aSAndrew Thompson uint16_t quirk_index, struct libusb20_quirk *pq) 880df4b8c2aSAndrew Thompson { 881760bc48eSAndrew Thompson struct usb_gen_quirk q; 882df4b8c2aSAndrew Thompson int error; 883df4b8c2aSAndrew Thompson 884df4b8c2aSAndrew Thompson memset(&q, 0, sizeof(q)); 885df4b8c2aSAndrew Thompson 886df4b8c2aSAndrew Thompson q.index = quirk_index; 887df4b8c2aSAndrew Thompson 888df4b8c2aSAndrew Thompson error = ugen20_be_ioctl(USB_DEV_QUIRK_GET, &q); 889df4b8c2aSAndrew Thompson 890df4b8c2aSAndrew Thompson if (error) { 891df4b8c2aSAndrew Thompson if (errno == EINVAL) { 892df4b8c2aSAndrew Thompson return (LIBUSB20_ERROR_NOT_FOUND); 893df4b8c2aSAndrew Thompson } 894df4b8c2aSAndrew Thompson } else { 895df4b8c2aSAndrew Thompson pq->vid = q.vid; 896df4b8c2aSAndrew Thompson pq->pid = q.pid; 897df4b8c2aSAndrew Thompson pq->bcdDeviceLow = q.bcdDeviceLow; 898df4b8c2aSAndrew Thompson pq->bcdDeviceHigh = q.bcdDeviceHigh; 899df4b8c2aSAndrew Thompson strlcpy(pq->quirkname, q.quirkname, sizeof(pq->quirkname)); 900df4b8c2aSAndrew Thompson } 901df4b8c2aSAndrew Thompson return (error); 902df4b8c2aSAndrew Thompson } 903df4b8c2aSAndrew Thompson 904df4b8c2aSAndrew Thompson static int 905df4b8c2aSAndrew Thompson ugen20_root_get_quirk_name(struct libusb20_backend *pbe, uint16_t quirk_index, 906df4b8c2aSAndrew Thompson struct libusb20_quirk *pq) 907df4b8c2aSAndrew Thompson { 908760bc48eSAndrew Thompson struct usb_gen_quirk q; 909df4b8c2aSAndrew Thompson int error; 910df4b8c2aSAndrew Thompson 911df4b8c2aSAndrew Thompson memset(&q, 0, sizeof(q)); 912df4b8c2aSAndrew Thompson 913df4b8c2aSAndrew Thompson q.index = quirk_index; 914df4b8c2aSAndrew Thompson 915df4b8c2aSAndrew Thompson error = ugen20_be_ioctl(USB_QUIRK_NAME_GET, &q); 916df4b8c2aSAndrew Thompson 917df4b8c2aSAndrew Thompson if (error) { 918df4b8c2aSAndrew Thompson if (errno == EINVAL) { 919df4b8c2aSAndrew Thompson return (LIBUSB20_ERROR_NOT_FOUND); 920df4b8c2aSAndrew Thompson } 921df4b8c2aSAndrew Thompson } else { 922df4b8c2aSAndrew Thompson strlcpy(pq->quirkname, q.quirkname, sizeof(pq->quirkname)); 923df4b8c2aSAndrew Thompson } 924df4b8c2aSAndrew Thompson return (error); 925df4b8c2aSAndrew Thompson } 926df4b8c2aSAndrew Thompson 927df4b8c2aSAndrew Thompson static int 928df4b8c2aSAndrew Thompson ugen20_root_add_dev_quirk(struct libusb20_backend *pbe, 929df4b8c2aSAndrew Thompson struct libusb20_quirk *pq) 930df4b8c2aSAndrew Thompson { 931760bc48eSAndrew Thompson struct usb_gen_quirk q; 932df4b8c2aSAndrew Thompson int error; 933df4b8c2aSAndrew Thompson 934df4b8c2aSAndrew Thompson memset(&q, 0, sizeof(q)); 935df4b8c2aSAndrew Thompson 936df4b8c2aSAndrew Thompson q.vid = pq->vid; 937df4b8c2aSAndrew Thompson q.pid = pq->pid; 938df4b8c2aSAndrew Thompson q.bcdDeviceLow = pq->bcdDeviceLow; 939df4b8c2aSAndrew Thompson q.bcdDeviceHigh = pq->bcdDeviceHigh; 940df4b8c2aSAndrew Thompson strlcpy(q.quirkname, pq->quirkname, sizeof(q.quirkname)); 941df4b8c2aSAndrew Thompson 942df4b8c2aSAndrew Thompson error = ugen20_be_ioctl(USB_DEV_QUIRK_ADD, &q); 943df4b8c2aSAndrew Thompson if (error) { 944df4b8c2aSAndrew Thompson if (errno == ENOMEM) { 945df4b8c2aSAndrew Thompson return (LIBUSB20_ERROR_NO_MEM); 946df4b8c2aSAndrew Thompson } 947df4b8c2aSAndrew Thompson } 948df4b8c2aSAndrew Thompson return (error); 949df4b8c2aSAndrew Thompson } 950df4b8c2aSAndrew Thompson 951df4b8c2aSAndrew Thompson static int 952df4b8c2aSAndrew Thompson ugen20_root_remove_dev_quirk(struct libusb20_backend *pbe, 953df4b8c2aSAndrew Thompson struct libusb20_quirk *pq) 954df4b8c2aSAndrew Thompson { 955760bc48eSAndrew Thompson struct usb_gen_quirk q; 956df4b8c2aSAndrew Thompson int error; 957df4b8c2aSAndrew Thompson 958df4b8c2aSAndrew Thompson memset(&q, 0, sizeof(q)); 959df4b8c2aSAndrew Thompson 960df4b8c2aSAndrew Thompson q.vid = pq->vid; 961df4b8c2aSAndrew Thompson q.pid = pq->pid; 962df4b8c2aSAndrew Thompson q.bcdDeviceLow = pq->bcdDeviceLow; 963df4b8c2aSAndrew Thompson q.bcdDeviceHigh = pq->bcdDeviceHigh; 964df4b8c2aSAndrew Thompson strlcpy(q.quirkname, pq->quirkname, sizeof(q.quirkname)); 965df4b8c2aSAndrew Thompson 966df4b8c2aSAndrew Thompson error = ugen20_be_ioctl(USB_DEV_QUIRK_REMOVE, &q); 967df4b8c2aSAndrew Thompson if (error) { 968df4b8c2aSAndrew Thompson if (errno == EINVAL) { 969df4b8c2aSAndrew Thompson return (LIBUSB20_ERROR_NOT_FOUND); 970df4b8c2aSAndrew Thompson } 971df4b8c2aSAndrew Thompson } 972df4b8c2aSAndrew Thompson return (error); 973df4b8c2aSAndrew Thompson } 974df4b8c2aSAndrew Thompson 975df4b8c2aSAndrew Thompson static int 976df4b8c2aSAndrew Thompson ugen20_root_set_template(struct libusb20_backend *pbe, int temp) 977df4b8c2aSAndrew Thompson { 978df4b8c2aSAndrew Thompson return (ugen20_be_ioctl(USB_SET_TEMPLATE, &temp)); 979df4b8c2aSAndrew Thompson } 980df4b8c2aSAndrew Thompson 981df4b8c2aSAndrew Thompson static int 982df4b8c2aSAndrew Thompson ugen20_root_get_template(struct libusb20_backend *pbe, int *ptemp) 983df4b8c2aSAndrew Thompson { 984df4b8c2aSAndrew Thompson return (ugen20_be_ioctl(USB_GET_TEMPLATE, ptemp)); 985df4b8c2aSAndrew Thompson } 986