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