1 /* $FreeBSD$ */ 2 /*- 3 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #ifdef USB_GLOBAL_INCLUDE_FILE 28 #include USB_GLOBAL_INCLUDE_FILE 29 #else 30 #include <sys/stdint.h> 31 #include <sys/stddef.h> 32 #include <sys/param.h> 33 #include <sys/queue.h> 34 #include <sys/types.h> 35 #include <sys/systm.h> 36 #include <sys/kernel.h> 37 #include <sys/bus.h> 38 #include <sys/module.h> 39 #include <sys/lock.h> 40 #include <sys/mutex.h> 41 #include <sys/condvar.h> 42 #include <sys/sysctl.h> 43 #include <sys/sx.h> 44 #include <sys/unistd.h> 45 #include <sys/callout.h> 46 #include <sys/malloc.h> 47 #include <sys/priv.h> 48 #include <sys/conf.h> 49 #include <sys/fcntl.h> 50 51 #include <dev/usb/usb.h> 52 #include <dev/usb/usb_ioctl.h> 53 #include <dev/usb/usbdi.h> 54 #include <dev/usb/usbdi_util.h> 55 56 #define USB_DEBUG_VAR ugen_debug 57 58 #include <dev/usb/usb_core.h> 59 #include <dev/usb/usb_dev.h> 60 #include <dev/usb/usb_mbuf.h> 61 #include <dev/usb/usb_process.h> 62 #include <dev/usb/usb_device.h> 63 #include <dev/usb/usb_debug.h> 64 #include <dev/usb/usb_request.h> 65 #include <dev/usb/usb_busdma.h> 66 #include <dev/usb/usb_util.h> 67 #include <dev/usb/usb_hub.h> 68 #include <dev/usb/usb_generic.h> 69 #include <dev/usb/usb_transfer.h> 70 71 #include <dev/usb/usb_controller.h> 72 #include <dev/usb/usb_bus.h> 73 #endif /* USB_GLOBAL_INCLUDE_FILE */ 74 75 #if USB_HAVE_UGEN 76 77 /* defines */ 78 79 #define UGEN_BULK_FS_BUFFER_SIZE (64*32) /* bytes */ 80 #define UGEN_BULK_HS_BUFFER_SIZE (1024*32) /* bytes */ 81 #define UGEN_HW_FRAMES 50 /* number of milliseconds per transfer */ 82 83 /* function prototypes */ 84 85 static usb_callback_t ugen_read_clear_stall_callback; 86 static usb_callback_t ugen_write_clear_stall_callback; 87 static usb_callback_t ugen_ctrl_read_callback; 88 static usb_callback_t ugen_ctrl_write_callback; 89 static usb_callback_t ugen_isoc_read_callback; 90 static usb_callback_t ugen_isoc_write_callback; 91 static usb_callback_t ugen_ctrl_fs_callback; 92 93 static usb_fifo_open_t ugen_open; 94 static usb_fifo_close_t ugen_close; 95 static usb_fifo_ioctl_t ugen_ioctl; 96 static usb_fifo_ioctl_t ugen_ioctl_post; 97 static usb_fifo_cmd_t ugen_start_read; 98 static usb_fifo_cmd_t ugen_start_write; 99 static usb_fifo_cmd_t ugen_stop_io; 100 101 static int ugen_transfer_setup(struct usb_fifo *, 102 const struct usb_config *, uint8_t); 103 static int ugen_open_pipe_write(struct usb_fifo *); 104 static int ugen_open_pipe_read(struct usb_fifo *); 105 static int ugen_set_config(struct usb_fifo *, uint8_t); 106 static int ugen_set_interface(struct usb_fifo *, uint8_t, uint8_t); 107 static int ugen_get_cdesc(struct usb_fifo *, struct usb_gen_descriptor *); 108 static int ugen_get_sdesc(struct usb_fifo *, struct usb_gen_descriptor *); 109 static int ugen_get_iface_driver(struct usb_fifo *f, struct usb_gen_descriptor *ugd); 110 static int usb_gen_fill_deviceinfo(struct usb_fifo *, 111 struct usb_device_info *); 112 static int ugen_re_enumerate(struct usb_fifo *); 113 static int ugen_iface_ioctl(struct usb_fifo *, u_long, void *, int); 114 static uint8_t ugen_fs_get_complete(struct usb_fifo *, uint8_t *); 115 static int ugen_fs_uninit(struct usb_fifo *f); 116 117 /* structures */ 118 119 struct usb_fifo_methods usb_ugen_methods = { 120 .f_open = &ugen_open, 121 .f_close = &ugen_close, 122 .f_ioctl = &ugen_ioctl, 123 .f_ioctl_post = &ugen_ioctl_post, 124 .f_start_read = &ugen_start_read, 125 .f_stop_read = &ugen_stop_io, 126 .f_start_write = &ugen_start_write, 127 .f_stop_write = &ugen_stop_io, 128 }; 129 130 #ifdef USB_DEBUG 131 static int ugen_debug = 0; 132 133 static SYSCTL_NODE(_hw_usb, OID_AUTO, ugen, CTLFLAG_RW, 0, "USB generic"); 134 SYSCTL_INT(_hw_usb_ugen, OID_AUTO, debug, CTLFLAG_RW | CTLFLAG_TUN, &ugen_debug, 135 0, "Debug level"); 136 TUNABLE_INT("hw.usb.ugen.debug", &ugen_debug); 137 #endif 138 139 140 /* prototypes */ 141 142 static int 143 ugen_transfer_setup(struct usb_fifo *f, 144 const struct usb_config *setup, uint8_t n_setup) 145 { 146 struct usb_endpoint *ep = usb_fifo_softc(f); 147 struct usb_device *udev = f->udev; 148 uint8_t iface_index = ep->iface_index; 149 int error; 150 151 mtx_unlock(f->priv_mtx); 152 153 /* 154 * "usbd_transfer_setup()" can sleep so one needs to make a wrapper, 155 * exiting the mutex and checking things 156 */ 157 error = usbd_transfer_setup(udev, &iface_index, f->xfer, 158 setup, n_setup, f, f->priv_mtx); 159 if (error == 0) { 160 161 if (f->xfer[0]->nframes == 1) { 162 error = usb_fifo_alloc_buffer(f, 163 f->xfer[0]->max_data_length, 2); 164 } else { 165 error = usb_fifo_alloc_buffer(f, 166 f->xfer[0]->max_frame_size, 167 2 * f->xfer[0]->nframes); 168 } 169 if (error) { 170 usbd_transfer_unsetup(f->xfer, n_setup); 171 } 172 } 173 mtx_lock(f->priv_mtx); 174 175 return (error); 176 } 177 178 static int 179 ugen_open(struct usb_fifo *f, int fflags) 180 { 181 struct usb_endpoint *ep = usb_fifo_softc(f); 182 struct usb_endpoint_descriptor *ed = ep->edesc; 183 uint8_t type; 184 185 DPRINTFN(6, "flag=0x%x\n", fflags); 186 187 mtx_lock(f->priv_mtx); 188 switch (usbd_get_speed(f->udev)) { 189 case USB_SPEED_LOW: 190 case USB_SPEED_FULL: 191 f->nframes = UGEN_HW_FRAMES; 192 f->bufsize = UGEN_BULK_FS_BUFFER_SIZE; 193 break; 194 default: 195 f->nframes = UGEN_HW_FRAMES * 8; 196 f->bufsize = UGEN_BULK_HS_BUFFER_SIZE; 197 break; 198 } 199 200 type = ed->bmAttributes & UE_XFERTYPE; 201 if (type == UE_INTERRUPT) { 202 f->bufsize = 0; /* use "wMaxPacketSize" */ 203 } 204 f->timeout = USB_NO_TIMEOUT; 205 f->flag_short = 0; 206 f->fifo_zlp = 0; 207 mtx_unlock(f->priv_mtx); 208 209 return (0); 210 } 211 212 static void 213 ugen_close(struct usb_fifo *f, int fflags) 214 { 215 DPRINTFN(6, "flag=0x%x\n", fflags); 216 217 /* cleanup */ 218 219 mtx_lock(f->priv_mtx); 220 usbd_transfer_stop(f->xfer[0]); 221 usbd_transfer_stop(f->xfer[1]); 222 mtx_unlock(f->priv_mtx); 223 224 usbd_transfer_unsetup(f->xfer, 2); 225 usb_fifo_free_buffer(f); 226 227 if (ugen_fs_uninit(f)) { 228 /* ignore any errors - we are closing */ 229 DPRINTFN(6, "no FIFOs\n"); 230 } 231 } 232 233 static int 234 ugen_open_pipe_write(struct usb_fifo *f) 235 { 236 struct usb_config usb_config[2]; 237 struct usb_endpoint *ep = usb_fifo_softc(f); 238 struct usb_endpoint_descriptor *ed = ep->edesc; 239 240 mtx_assert(f->priv_mtx, MA_OWNED); 241 242 if (f->xfer[0] || f->xfer[1]) { 243 /* transfers are already opened */ 244 return (0); 245 } 246 memset(usb_config, 0, sizeof(usb_config)); 247 248 usb_config[1].type = UE_CONTROL; 249 usb_config[1].endpoint = 0; 250 usb_config[1].direction = UE_DIR_ANY; 251 usb_config[1].timeout = 1000; /* 1 second */ 252 usb_config[1].interval = 50;/* 50 milliseconds */ 253 usb_config[1].bufsize = sizeof(struct usb_device_request); 254 usb_config[1].callback = &ugen_write_clear_stall_callback; 255 usb_config[1].usb_mode = USB_MODE_HOST; 256 257 usb_config[0].type = ed->bmAttributes & UE_XFERTYPE; 258 usb_config[0].endpoint = ed->bEndpointAddress & UE_ADDR; 259 usb_config[0].stream_id = 0; /* XXX support more stream ID's */ 260 usb_config[0].direction = UE_DIR_TX; 261 usb_config[0].interval = USB_DEFAULT_INTERVAL; 262 usb_config[0].flags.proxy_buffer = 1; 263 usb_config[0].usb_mode = USB_MODE_DUAL; /* both modes */ 264 265 switch (ed->bmAttributes & UE_XFERTYPE) { 266 case UE_INTERRUPT: 267 case UE_BULK: 268 if (f->flag_short) { 269 usb_config[0].flags.force_short_xfer = 1; 270 } 271 usb_config[0].callback = &ugen_ctrl_write_callback; 272 usb_config[0].timeout = f->timeout; 273 usb_config[0].frames = 1; 274 usb_config[0].bufsize = f->bufsize; 275 if (ugen_transfer_setup(f, usb_config, 2)) { 276 return (EIO); 277 } 278 /* first transfer does not clear stall */ 279 f->flag_stall = 0; 280 break; 281 282 case UE_ISOCHRONOUS: 283 usb_config[0].flags.short_xfer_ok = 1; 284 usb_config[0].bufsize = 0; /* use default */ 285 usb_config[0].frames = f->nframes; 286 usb_config[0].callback = &ugen_isoc_write_callback; 287 usb_config[0].timeout = 0; 288 289 /* clone configuration */ 290 usb_config[1] = usb_config[0]; 291 292 if (ugen_transfer_setup(f, usb_config, 2)) { 293 return (EIO); 294 } 295 break; 296 default: 297 return (EINVAL); 298 } 299 return (0); 300 } 301 302 static int 303 ugen_open_pipe_read(struct usb_fifo *f) 304 { 305 struct usb_config usb_config[2]; 306 struct usb_endpoint *ep = usb_fifo_softc(f); 307 struct usb_endpoint_descriptor *ed = ep->edesc; 308 309 mtx_assert(f->priv_mtx, MA_OWNED); 310 311 if (f->xfer[0] || f->xfer[1]) { 312 /* transfers are already opened */ 313 return (0); 314 } 315 memset(usb_config, 0, sizeof(usb_config)); 316 317 usb_config[1].type = UE_CONTROL; 318 usb_config[1].endpoint = 0; 319 usb_config[1].direction = UE_DIR_ANY; 320 usb_config[1].timeout = 1000; /* 1 second */ 321 usb_config[1].interval = 50;/* 50 milliseconds */ 322 usb_config[1].bufsize = sizeof(struct usb_device_request); 323 usb_config[1].callback = &ugen_read_clear_stall_callback; 324 usb_config[1].usb_mode = USB_MODE_HOST; 325 326 usb_config[0].type = ed->bmAttributes & UE_XFERTYPE; 327 usb_config[0].endpoint = ed->bEndpointAddress & UE_ADDR; 328 usb_config[0].stream_id = 0; /* XXX support more stream ID's */ 329 usb_config[0].direction = UE_DIR_RX; 330 usb_config[0].interval = USB_DEFAULT_INTERVAL; 331 usb_config[0].flags.proxy_buffer = 1; 332 usb_config[0].usb_mode = USB_MODE_DUAL; /* both modes */ 333 334 switch (ed->bmAttributes & UE_XFERTYPE) { 335 case UE_INTERRUPT: 336 case UE_BULK: 337 if (f->flag_short) { 338 usb_config[0].flags.short_xfer_ok = 1; 339 } 340 usb_config[0].timeout = f->timeout; 341 usb_config[0].frames = 1; 342 usb_config[0].callback = &ugen_ctrl_read_callback; 343 usb_config[0].bufsize = f->bufsize; 344 345 if (ugen_transfer_setup(f, usb_config, 2)) { 346 return (EIO); 347 } 348 /* first transfer does not clear stall */ 349 f->flag_stall = 0; 350 break; 351 352 case UE_ISOCHRONOUS: 353 usb_config[0].flags.short_xfer_ok = 1; 354 usb_config[0].bufsize = 0; /* use default */ 355 usb_config[0].frames = f->nframes; 356 usb_config[0].callback = &ugen_isoc_read_callback; 357 usb_config[0].timeout = 0; 358 359 /* clone configuration */ 360 usb_config[1] = usb_config[0]; 361 362 if (ugen_transfer_setup(f, usb_config, 2)) { 363 return (EIO); 364 } 365 break; 366 367 default: 368 return (EINVAL); 369 } 370 return (0); 371 } 372 373 static void 374 ugen_start_read(struct usb_fifo *f) 375 { 376 /* check that pipes are open */ 377 if (ugen_open_pipe_read(f)) { 378 /* signal error */ 379 usb_fifo_put_data_error(f); 380 } 381 /* start transfers */ 382 usbd_transfer_start(f->xfer[0]); 383 usbd_transfer_start(f->xfer[1]); 384 } 385 386 static void 387 ugen_start_write(struct usb_fifo *f) 388 { 389 /* check that pipes are open */ 390 if (ugen_open_pipe_write(f)) { 391 /* signal error */ 392 usb_fifo_get_data_error(f); 393 } 394 /* start transfers */ 395 usbd_transfer_start(f->xfer[0]); 396 usbd_transfer_start(f->xfer[1]); 397 } 398 399 static void 400 ugen_stop_io(struct usb_fifo *f) 401 { 402 /* stop transfers */ 403 usbd_transfer_stop(f->xfer[0]); 404 usbd_transfer_stop(f->xfer[1]); 405 } 406 407 static void 408 ugen_ctrl_read_callback(struct usb_xfer *xfer, usb_error_t error) 409 { 410 struct usb_fifo *f = usbd_xfer_softc(xfer); 411 struct usb_mbuf *m; 412 413 DPRINTFN(4, "actlen=%u, aframes=%u\n", xfer->actlen, xfer->aframes); 414 415 switch (USB_GET_STATE(xfer)) { 416 case USB_ST_TRANSFERRED: 417 if (xfer->actlen == 0) { 418 if (f->fifo_zlp != 4) { 419 f->fifo_zlp++; 420 } else { 421 /* 422 * Throttle a little bit we have multiple ZLPs 423 * in a row! 424 */ 425 xfer->interval = 64; /* ms */ 426 } 427 } else { 428 /* clear throttle */ 429 xfer->interval = 0; 430 f->fifo_zlp = 0; 431 } 432 usb_fifo_put_data(f, xfer->frbuffers, 0, 433 xfer->actlen, 1); 434 435 case USB_ST_SETUP: 436 if (f->flag_stall) { 437 usbd_transfer_start(f->xfer[1]); 438 break; 439 } 440 USB_IF_POLL(&f->free_q, m); 441 if (m) { 442 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 443 usbd_transfer_submit(xfer); 444 } 445 break; 446 447 default: /* Error */ 448 if (xfer->error != USB_ERR_CANCELLED) { 449 /* send a zero length packet to userland */ 450 usb_fifo_put_data(f, xfer->frbuffers, 0, 0, 1); 451 f->flag_stall = 1; 452 f->fifo_zlp = 0; 453 usbd_transfer_start(f->xfer[1]); 454 } 455 break; 456 } 457 } 458 459 static void 460 ugen_ctrl_write_callback(struct usb_xfer *xfer, usb_error_t error) 461 { 462 struct usb_fifo *f = usbd_xfer_softc(xfer); 463 usb_frlength_t actlen; 464 465 DPRINTFN(4, "actlen=%u, aframes=%u\n", xfer->actlen, xfer->aframes); 466 467 switch (USB_GET_STATE(xfer)) { 468 case USB_ST_SETUP: 469 case USB_ST_TRANSFERRED: 470 /* 471 * If writing is in stall, just jump to clear stall 472 * callback and solve the situation. 473 */ 474 if (f->flag_stall) { 475 usbd_transfer_start(f->xfer[1]); 476 break; 477 } 478 /* 479 * Write data, setup and perform hardware transfer. 480 */ 481 if (usb_fifo_get_data(f, xfer->frbuffers, 0, 482 xfer->max_data_length, &actlen, 0)) { 483 usbd_xfer_set_frame_len(xfer, 0, actlen); 484 usbd_transfer_submit(xfer); 485 } 486 break; 487 488 default: /* Error */ 489 if (xfer->error != USB_ERR_CANCELLED) { 490 f->flag_stall = 1; 491 usbd_transfer_start(f->xfer[1]); 492 } 493 break; 494 } 495 } 496 497 static void 498 ugen_read_clear_stall_callback(struct usb_xfer *xfer, usb_error_t error) 499 { 500 struct usb_fifo *f = usbd_xfer_softc(xfer); 501 struct usb_xfer *xfer_other = f->xfer[0]; 502 503 if (f->flag_stall == 0) { 504 /* nothing to do */ 505 return; 506 } 507 if (usbd_clear_stall_callback(xfer, xfer_other)) { 508 DPRINTFN(5, "f=%p: stall cleared\n", f); 509 f->flag_stall = 0; 510 usbd_transfer_start(xfer_other); 511 } 512 } 513 514 static void 515 ugen_write_clear_stall_callback(struct usb_xfer *xfer, usb_error_t error) 516 { 517 struct usb_fifo *f = usbd_xfer_softc(xfer); 518 struct usb_xfer *xfer_other = f->xfer[0]; 519 520 if (f->flag_stall == 0) { 521 /* nothing to do */ 522 return; 523 } 524 if (usbd_clear_stall_callback(xfer, xfer_other)) { 525 DPRINTFN(5, "f=%p: stall cleared\n", f); 526 f->flag_stall = 0; 527 usbd_transfer_start(xfer_other); 528 } 529 } 530 531 static void 532 ugen_isoc_read_callback(struct usb_xfer *xfer, usb_error_t error) 533 { 534 struct usb_fifo *f = usbd_xfer_softc(xfer); 535 usb_frlength_t offset; 536 usb_frcount_t n; 537 538 DPRINTFN(4, "actlen=%u, aframes=%u\n", xfer->actlen, xfer->aframes); 539 540 switch (USB_GET_STATE(xfer)) { 541 case USB_ST_TRANSFERRED: 542 543 DPRINTFN(6, "actlen=%d\n", xfer->actlen); 544 545 offset = 0; 546 547 for (n = 0; n != xfer->aframes; n++) { 548 usb_fifo_put_data(f, xfer->frbuffers, offset, 549 xfer->frlengths[n], 1); 550 offset += xfer->max_frame_size; 551 } 552 553 case USB_ST_SETUP: 554 tr_setup: 555 for (n = 0; n != xfer->nframes; n++) { 556 /* setup size for next transfer */ 557 usbd_xfer_set_frame_len(xfer, n, xfer->max_frame_size); 558 } 559 usbd_transfer_submit(xfer); 560 break; 561 562 default: /* Error */ 563 if (xfer->error == USB_ERR_CANCELLED) { 564 break; 565 } 566 goto tr_setup; 567 } 568 } 569 570 static void 571 ugen_isoc_write_callback(struct usb_xfer *xfer, usb_error_t error) 572 { 573 struct usb_fifo *f = usbd_xfer_softc(xfer); 574 usb_frlength_t actlen; 575 usb_frlength_t offset; 576 usb_frcount_t n; 577 578 DPRINTFN(4, "actlen=%u, aframes=%u\n", xfer->actlen, xfer->aframes); 579 580 switch (USB_GET_STATE(xfer)) { 581 case USB_ST_TRANSFERRED: 582 case USB_ST_SETUP: 583 tr_setup: 584 offset = 0; 585 for (n = 0; n != xfer->nframes; n++) { 586 if (usb_fifo_get_data(f, xfer->frbuffers, offset, 587 xfer->max_frame_size, &actlen, 1)) { 588 usbd_xfer_set_frame_len(xfer, n, actlen); 589 offset += actlen; 590 } else { 591 break; 592 } 593 } 594 595 for (; n != xfer->nframes; n++) { 596 /* fill in zero frames */ 597 usbd_xfer_set_frame_len(xfer, n, 0); 598 } 599 usbd_transfer_submit(xfer); 600 break; 601 602 default: /* Error */ 603 if (xfer->error == USB_ERR_CANCELLED) { 604 break; 605 } 606 goto tr_setup; 607 } 608 } 609 610 static int 611 ugen_set_config(struct usb_fifo *f, uint8_t index) 612 { 613 DPRINTFN(2, "index %u\n", index); 614 615 if (f->udev->flags.usb_mode != USB_MODE_HOST) { 616 /* not possible in device side mode */ 617 return (ENOTTY); 618 } 619 if (f->udev->curr_config_index == index) { 620 /* no change needed */ 621 return (0); 622 } 623 /* make sure all FIFO's are gone */ 624 /* else there can be a deadlock */ 625 if (ugen_fs_uninit(f)) { 626 /* ignore any errors */ 627 DPRINTFN(6, "no FIFOs\n"); 628 } 629 /* change setting - will free generic FIFOs, if any */ 630 if (usbd_set_config_index(f->udev, index)) { 631 return (EIO); 632 } 633 /* probe and attach */ 634 if (usb_probe_and_attach(f->udev, USB_IFACE_INDEX_ANY)) { 635 return (EIO); 636 } 637 return (0); 638 } 639 640 static int 641 ugen_set_interface(struct usb_fifo *f, 642 uint8_t iface_index, uint8_t alt_index) 643 { 644 DPRINTFN(2, "%u, %u\n", iface_index, alt_index); 645 646 if (f->udev->flags.usb_mode != USB_MODE_HOST) { 647 /* not possible in device side mode */ 648 return (ENOTTY); 649 } 650 /* make sure all FIFO's are gone */ 651 /* else there can be a deadlock */ 652 if (ugen_fs_uninit(f)) { 653 /* ignore any errors */ 654 DPRINTFN(6, "no FIFOs\n"); 655 } 656 /* change setting - will free generic FIFOs, if any */ 657 if (usbd_set_alt_interface_index(f->udev, iface_index, alt_index)) { 658 return (EIO); 659 } 660 /* probe and attach */ 661 if (usb_probe_and_attach(f->udev, iface_index)) { 662 return (EIO); 663 } 664 return (0); 665 } 666 667 /*------------------------------------------------------------------------* 668 * ugen_get_cdesc 669 * 670 * This function will retrieve the complete configuration descriptor 671 * at the given index. 672 *------------------------------------------------------------------------*/ 673 static int 674 ugen_get_cdesc(struct usb_fifo *f, struct usb_gen_descriptor *ugd) 675 { 676 struct usb_config_descriptor *cdesc; 677 struct usb_device *udev = f->udev; 678 int error; 679 uint16_t len; 680 uint8_t free_data; 681 682 DPRINTFN(6, "\n"); 683 684 if (ugd->ugd_data == NULL) { 685 /* userland pointer should not be zero */ 686 return (EINVAL); 687 } 688 if ((ugd->ugd_config_index == USB_UNCONFIG_INDEX) || 689 (ugd->ugd_config_index == udev->curr_config_index)) { 690 cdesc = usbd_get_config_descriptor(udev); 691 if (cdesc == NULL) 692 return (ENXIO); 693 free_data = 0; 694 695 } else { 696 #if (USB_HAVE_FIXED_CONFIG == 0) 697 if (usbd_req_get_config_desc_full(udev, 698 NULL, &cdesc, ugd->ugd_config_index)) { 699 return (ENXIO); 700 } 701 free_data = 1; 702 #else 703 /* configuration descriptor data is shared */ 704 return (EINVAL); 705 #endif 706 } 707 708 len = UGETW(cdesc->wTotalLength); 709 if (len > ugd->ugd_maxlen) { 710 len = ugd->ugd_maxlen; 711 } 712 DPRINTFN(6, "len=%u\n", len); 713 714 ugd->ugd_actlen = len; 715 ugd->ugd_offset = 0; 716 717 error = copyout(cdesc, ugd->ugd_data, len); 718 719 if (free_data) 720 usbd_free_config_desc(udev, cdesc); 721 722 return (error); 723 } 724 725 /* 726 * This function is called having the enumeration SX locked which 727 * protects the scratch area used. 728 */ 729 static int 730 ugen_get_sdesc(struct usb_fifo *f, struct usb_gen_descriptor *ugd) 731 { 732 void *ptr; 733 uint16_t size; 734 int error; 735 736 ptr = f->udev->scratch.data; 737 size = sizeof(f->udev->scratch.data); 738 739 if (usbd_req_get_string_desc(f->udev, NULL, ptr, 740 size, ugd->ugd_lang_id, ugd->ugd_string_index)) { 741 error = EINVAL; 742 } else { 743 744 if (size > ((uint8_t *)ptr)[0]) { 745 size = ((uint8_t *)ptr)[0]; 746 } 747 if (size > ugd->ugd_maxlen) { 748 size = ugd->ugd_maxlen; 749 } 750 ugd->ugd_actlen = size; 751 ugd->ugd_offset = 0; 752 753 error = copyout(ptr, ugd->ugd_data, size); 754 } 755 return (error); 756 } 757 758 /*------------------------------------------------------------------------* 759 * ugen_get_iface_driver 760 * 761 * This function generates an USB interface description for userland. 762 * 763 * Returns: 764 * 0: Success 765 * Else: Failure 766 *------------------------------------------------------------------------*/ 767 static int 768 ugen_get_iface_driver(struct usb_fifo *f, struct usb_gen_descriptor *ugd) 769 { 770 struct usb_device *udev = f->udev; 771 struct usb_interface *iface; 772 const char *ptr; 773 const char *desc; 774 unsigned int len; 775 unsigned int maxlen; 776 char buf[128]; 777 int error; 778 779 DPRINTFN(6, "\n"); 780 781 if ((ugd->ugd_data == NULL) || (ugd->ugd_maxlen == 0)) { 782 /* userland pointer should not be zero */ 783 return (EINVAL); 784 } 785 786 iface = usbd_get_iface(udev, ugd->ugd_iface_index); 787 if ((iface == NULL) || (iface->idesc == NULL)) { 788 /* invalid interface index */ 789 return (EINVAL); 790 } 791 792 /* read out device nameunit string, if any */ 793 if ((iface->subdev != NULL) && 794 device_is_attached(iface->subdev) && 795 (ptr = device_get_nameunit(iface->subdev)) && 796 (desc = device_get_desc(iface->subdev))) { 797 798 /* print description */ 799 snprintf(buf, sizeof(buf), "%s: <%s>", ptr, desc); 800 801 /* range checks */ 802 maxlen = ugd->ugd_maxlen - 1; 803 len = strlen(buf); 804 if (len > maxlen) 805 len = maxlen; 806 807 /* update actual length, including terminating zero */ 808 ugd->ugd_actlen = len + 1; 809 810 /* copy out interface description */ 811 error = copyout(buf, ugd->ugd_data, ugd->ugd_actlen); 812 } else { 813 /* zero length string is default */ 814 error = copyout("", ugd->ugd_data, 1); 815 } 816 return (error); 817 } 818 819 /*------------------------------------------------------------------------* 820 * usb_gen_fill_deviceinfo 821 * 822 * This function dumps information about an USB device to the 823 * structure pointed to by the "di" argument. 824 * 825 * Returns: 826 * 0: Success 827 * Else: Failure 828 *------------------------------------------------------------------------*/ 829 static int 830 usb_gen_fill_deviceinfo(struct usb_fifo *f, struct usb_device_info *di) 831 { 832 struct usb_device *udev; 833 struct usb_device *hub; 834 835 udev = f->udev; 836 837 bzero(di, sizeof(di[0])); 838 839 di->udi_bus = device_get_unit(udev->bus->bdev); 840 di->udi_addr = udev->address; 841 di->udi_index = udev->device_index; 842 strlcpy(di->udi_serial, usb_get_serial(udev), sizeof(di->udi_serial)); 843 strlcpy(di->udi_vendor, usb_get_manufacturer(udev), sizeof(di->udi_vendor)); 844 strlcpy(di->udi_product, usb_get_product(udev), sizeof(di->udi_product)); 845 usb_printbcd(di->udi_release, sizeof(di->udi_release), 846 UGETW(udev->ddesc.bcdDevice)); 847 di->udi_vendorNo = UGETW(udev->ddesc.idVendor); 848 di->udi_productNo = UGETW(udev->ddesc.idProduct); 849 di->udi_releaseNo = UGETW(udev->ddesc.bcdDevice); 850 di->udi_class = udev->ddesc.bDeviceClass; 851 di->udi_subclass = udev->ddesc.bDeviceSubClass; 852 di->udi_protocol = udev->ddesc.bDeviceProtocol; 853 di->udi_config_no = udev->curr_config_no; 854 di->udi_config_index = udev->curr_config_index; 855 di->udi_power = udev->flags.self_powered ? 0 : udev->power; 856 di->udi_speed = udev->speed; 857 di->udi_mode = udev->flags.usb_mode; 858 di->udi_power_mode = udev->power_mode; 859 di->udi_suspended = udev->flags.peer_suspended; 860 861 hub = udev->parent_hub; 862 if (hub) { 863 di->udi_hubaddr = hub->address; 864 di->udi_hubindex = hub->device_index; 865 di->udi_hubport = udev->port_no; 866 } 867 return (0); 868 } 869 870 /*------------------------------------------------------------------------* 871 * ugen_check_request 872 * 873 * Return values: 874 * 0: Access allowed 875 * Else: No access 876 *------------------------------------------------------------------------*/ 877 static int 878 ugen_check_request(struct usb_device *udev, struct usb_device_request *req) 879 { 880 struct usb_endpoint *ep; 881 int error; 882 883 /* 884 * Avoid requests that would damage the bus integrity: 885 */ 886 if (((req->bmRequestType == UT_WRITE_DEVICE) && 887 (req->bRequest == UR_SET_ADDRESS)) || 888 ((req->bmRequestType == UT_WRITE_DEVICE) && 889 (req->bRequest == UR_SET_CONFIG)) || 890 ((req->bmRequestType == UT_WRITE_INTERFACE) && 891 (req->bRequest == UR_SET_INTERFACE))) { 892 /* 893 * These requests can be useful for testing USB drivers. 894 */ 895 error = priv_check(curthread, PRIV_DRIVER); 896 if (error) { 897 return (error); 898 } 899 } 900 /* 901 * Special case - handle clearing of stall 902 */ 903 if (req->bmRequestType == UT_WRITE_ENDPOINT) { 904 905 ep = usbd_get_ep_by_addr(udev, req->wIndex[0]); 906 if (ep == NULL) { 907 return (EINVAL); 908 } 909 if ((req->bRequest == UR_CLEAR_FEATURE) && 910 (UGETW(req->wValue) == UF_ENDPOINT_HALT)) { 911 usbd_clear_data_toggle(udev, ep); 912 } 913 } 914 /* TODO: add more checks to verify the interface index */ 915 916 return (0); 917 } 918 919 int 920 ugen_do_request(struct usb_fifo *f, struct usb_ctl_request *ur) 921 { 922 int error; 923 uint16_t len; 924 uint16_t actlen; 925 926 if (ugen_check_request(f->udev, &ur->ucr_request)) { 927 return (EPERM); 928 } 929 len = UGETW(ur->ucr_request.wLength); 930 931 /* check if "ucr_data" is valid */ 932 if (len != 0) { 933 if (ur->ucr_data == NULL) { 934 return (EFAULT); 935 } 936 } 937 /* do the USB request */ 938 error = usbd_do_request_flags 939 (f->udev, NULL, &ur->ucr_request, ur->ucr_data, 940 (ur->ucr_flags & USB_SHORT_XFER_OK) | 941 USB_USER_DATA_PTR, &actlen, 942 USB_DEFAULT_TIMEOUT); 943 944 ur->ucr_actlen = actlen; 945 946 if (error) { 947 error = EIO; 948 } 949 return (error); 950 } 951 952 /*------------------------------------------------------------------------ 953 * ugen_re_enumerate 954 *------------------------------------------------------------------------*/ 955 static int 956 ugen_re_enumerate(struct usb_fifo *f) 957 { 958 struct usb_device *udev = f->udev; 959 int error; 960 961 /* 962 * This request can be useful for testing USB drivers: 963 */ 964 error = priv_check(curthread, PRIV_DRIVER); 965 if (error) { 966 return (error); 967 } 968 if (udev->flags.usb_mode != USB_MODE_HOST) { 969 /* not possible in device side mode */ 970 DPRINTFN(6, "device mode\n"); 971 return (ENOTTY); 972 } 973 if (udev->parent_hub == NULL) { 974 /* the root HUB cannot be re-enumerated */ 975 DPRINTFN(6, "cannot reset root HUB\n"); 976 return (EINVAL); 977 } 978 /* make sure all FIFO's are gone */ 979 /* else there can be a deadlock */ 980 if (ugen_fs_uninit(f)) { 981 /* ignore any errors */ 982 DPRINTFN(6, "no FIFOs\n"); 983 } 984 /* start re-enumeration of device */ 985 usbd_start_re_enumerate(udev); 986 return (0); 987 } 988 989 int 990 ugen_fs_uninit(struct usb_fifo *f) 991 { 992 if (f->fs_xfer == NULL) { 993 return (EINVAL); 994 } 995 usbd_transfer_unsetup(f->fs_xfer, f->fs_ep_max); 996 free(f->fs_xfer, M_USB); 997 f->fs_xfer = NULL; 998 f->fs_ep_max = 0; 999 f->fs_ep_ptr = NULL; 1000 f->flag_iscomplete = 0; 1001 usb_fifo_free_buffer(f); 1002 return (0); 1003 } 1004 1005 static uint8_t 1006 ugen_fs_get_complete(struct usb_fifo *f, uint8_t *pindex) 1007 { 1008 struct usb_mbuf *m; 1009 1010 USB_IF_DEQUEUE(&f->used_q, m); 1011 1012 if (m) { 1013 *pindex = *((uint8_t *)(m->cur_data_ptr)); 1014 1015 USB_IF_ENQUEUE(&f->free_q, m); 1016 1017 return (0); /* success */ 1018 } else { 1019 1020 *pindex = 0; /* fix compiler warning */ 1021 1022 f->flag_iscomplete = 0; 1023 } 1024 return (1); /* failure */ 1025 } 1026 1027 static void 1028 ugen_fs_set_complete(struct usb_fifo *f, uint8_t index) 1029 { 1030 struct usb_mbuf *m; 1031 1032 USB_IF_DEQUEUE(&f->free_q, m); 1033 1034 if (m == NULL) { 1035 /* can happen during close */ 1036 DPRINTF("out of buffers\n"); 1037 return; 1038 } 1039 USB_MBUF_RESET(m); 1040 1041 *((uint8_t *)(m->cur_data_ptr)) = index; 1042 1043 USB_IF_ENQUEUE(&f->used_q, m); 1044 1045 f->flag_iscomplete = 1; 1046 1047 usb_fifo_wakeup(f); 1048 } 1049 1050 static int 1051 ugen_fs_copy_in(struct usb_fifo *f, uint8_t ep_index) 1052 { 1053 struct usb_device_request *req; 1054 struct usb_xfer *xfer; 1055 struct usb_fs_endpoint fs_ep; 1056 void *uaddr; /* userland pointer */ 1057 void *kaddr; 1058 usb_frlength_t offset; 1059 usb_frlength_t rem; 1060 usb_frcount_t n; 1061 uint32_t length; 1062 int error; 1063 uint8_t isread; 1064 1065 if (ep_index >= f->fs_ep_max) { 1066 return (EINVAL); 1067 } 1068 xfer = f->fs_xfer[ep_index]; 1069 if (xfer == NULL) { 1070 return (EINVAL); 1071 } 1072 mtx_lock(f->priv_mtx); 1073 if (usbd_transfer_pending(xfer)) { 1074 mtx_unlock(f->priv_mtx); 1075 return (EBUSY); /* should not happen */ 1076 } 1077 mtx_unlock(f->priv_mtx); 1078 1079 error = copyin(f->fs_ep_ptr + 1080 ep_index, &fs_ep, sizeof(fs_ep)); 1081 if (error) { 1082 return (error); 1083 } 1084 /* security checks */ 1085 1086 if (fs_ep.nFrames > xfer->max_frame_count) { 1087 xfer->error = USB_ERR_INVAL; 1088 goto complete; 1089 } 1090 if (fs_ep.nFrames == 0) { 1091 xfer->error = USB_ERR_INVAL; 1092 goto complete; 1093 } 1094 error = copyin(fs_ep.ppBuffer, 1095 &uaddr, sizeof(uaddr)); 1096 if (error) { 1097 return (error); 1098 } 1099 /* reset first frame */ 1100 usbd_xfer_set_frame_offset(xfer, 0, 0); 1101 1102 if (xfer->flags_int.control_xfr) { 1103 1104 req = xfer->frbuffers[0].buffer; 1105 1106 error = copyin(fs_ep.pLength, 1107 &length, sizeof(length)); 1108 if (error) { 1109 return (error); 1110 } 1111 if (length != sizeof(*req)) { 1112 xfer->error = USB_ERR_INVAL; 1113 goto complete; 1114 } 1115 if (length != 0) { 1116 error = copyin(uaddr, req, length); 1117 if (error) { 1118 return (error); 1119 } 1120 } 1121 if (ugen_check_request(f->udev, req)) { 1122 xfer->error = USB_ERR_INVAL; 1123 goto complete; 1124 } 1125 usbd_xfer_set_frame_len(xfer, 0, length); 1126 1127 /* Host mode only ! */ 1128 if ((req->bmRequestType & 1129 (UT_READ | UT_WRITE)) == UT_READ) { 1130 isread = 1; 1131 } else { 1132 isread = 0; 1133 } 1134 n = 1; 1135 offset = sizeof(*req); 1136 1137 } else { 1138 /* Device and Host mode */ 1139 if (USB_GET_DATA_ISREAD(xfer)) { 1140 isread = 1; 1141 } else { 1142 isread = 0; 1143 } 1144 n = 0; 1145 offset = 0; 1146 } 1147 1148 rem = usbd_xfer_max_len(xfer); 1149 xfer->nframes = fs_ep.nFrames; 1150 xfer->timeout = fs_ep.timeout; 1151 if (xfer->timeout > 65535) { 1152 xfer->timeout = 65535; 1153 } 1154 if (fs_ep.flags & USB_FS_FLAG_SINGLE_SHORT_OK) 1155 xfer->flags.short_xfer_ok = 1; 1156 else 1157 xfer->flags.short_xfer_ok = 0; 1158 1159 if (fs_ep.flags & USB_FS_FLAG_MULTI_SHORT_OK) 1160 xfer->flags.short_frames_ok = 1; 1161 else 1162 xfer->flags.short_frames_ok = 0; 1163 1164 if (fs_ep.flags & USB_FS_FLAG_FORCE_SHORT) 1165 xfer->flags.force_short_xfer = 1; 1166 else 1167 xfer->flags.force_short_xfer = 0; 1168 1169 if (fs_ep.flags & USB_FS_FLAG_CLEAR_STALL) 1170 usbd_xfer_set_stall(xfer); 1171 else 1172 xfer->flags.stall_pipe = 0; 1173 1174 for (; n != xfer->nframes; n++) { 1175 1176 error = copyin(fs_ep.pLength + n, 1177 &length, sizeof(length)); 1178 if (error) { 1179 break; 1180 } 1181 usbd_xfer_set_frame_len(xfer, n, length); 1182 1183 if (length > rem) { 1184 xfer->error = USB_ERR_INVAL; 1185 goto complete; 1186 } 1187 rem -= length; 1188 1189 if (!isread) { 1190 1191 /* we need to know the source buffer */ 1192 error = copyin(fs_ep.ppBuffer + n, 1193 &uaddr, sizeof(uaddr)); 1194 if (error) { 1195 break; 1196 } 1197 if (xfer->flags_int.isochronous_xfr) { 1198 /* get kernel buffer address */ 1199 kaddr = xfer->frbuffers[0].buffer; 1200 kaddr = USB_ADD_BYTES(kaddr, offset); 1201 } else { 1202 /* set current frame offset */ 1203 usbd_xfer_set_frame_offset(xfer, offset, n); 1204 1205 /* get kernel buffer address */ 1206 kaddr = xfer->frbuffers[n].buffer; 1207 } 1208 1209 /* move data */ 1210 error = copyin(uaddr, kaddr, length); 1211 if (error) { 1212 break; 1213 } 1214 } 1215 offset += length; 1216 } 1217 return (error); 1218 1219 complete: 1220 mtx_lock(f->priv_mtx); 1221 ugen_fs_set_complete(f, ep_index); 1222 mtx_unlock(f->priv_mtx); 1223 return (0); 1224 } 1225 1226 static int 1227 ugen_fs_copy_out(struct usb_fifo *f, uint8_t ep_index) 1228 { 1229 struct usb_device_request *req; 1230 struct usb_xfer *xfer; 1231 struct usb_fs_endpoint fs_ep; 1232 struct usb_fs_endpoint *fs_ep_uptr; /* userland ptr */ 1233 void *uaddr; /* userland ptr */ 1234 void *kaddr; 1235 usb_frlength_t offset; 1236 usb_frlength_t rem; 1237 usb_frcount_t n; 1238 uint32_t length; 1239 uint32_t temp; 1240 int error; 1241 uint8_t isread; 1242 1243 if (ep_index >= f->fs_ep_max) 1244 return (EINVAL); 1245 1246 xfer = f->fs_xfer[ep_index]; 1247 if (xfer == NULL) 1248 return (EINVAL); 1249 1250 mtx_lock(f->priv_mtx); 1251 if (usbd_transfer_pending(xfer)) { 1252 mtx_unlock(f->priv_mtx); 1253 return (EBUSY); /* should not happen */ 1254 } 1255 mtx_unlock(f->priv_mtx); 1256 1257 fs_ep_uptr = f->fs_ep_ptr + ep_index; 1258 error = copyin(fs_ep_uptr, &fs_ep, sizeof(fs_ep)); 1259 if (error) { 1260 return (error); 1261 } 1262 fs_ep.status = xfer->error; 1263 fs_ep.aFrames = xfer->aframes; 1264 fs_ep.isoc_time_complete = xfer->isoc_time_complete; 1265 if (xfer->error) { 1266 goto complete; 1267 } 1268 if (xfer->flags_int.control_xfr) { 1269 req = xfer->frbuffers[0].buffer; 1270 1271 /* Host mode only ! */ 1272 if ((req->bmRequestType & (UT_READ | UT_WRITE)) == UT_READ) { 1273 isread = 1; 1274 } else { 1275 isread = 0; 1276 } 1277 if (xfer->nframes == 0) 1278 n = 0; /* should never happen */ 1279 else 1280 n = 1; 1281 } else { 1282 /* Device and Host mode */ 1283 if (USB_GET_DATA_ISREAD(xfer)) { 1284 isread = 1; 1285 } else { 1286 isread = 0; 1287 } 1288 n = 0; 1289 } 1290 1291 /* Update lengths and copy out data */ 1292 1293 rem = usbd_xfer_max_len(xfer); 1294 offset = 0; 1295 1296 for (; n != xfer->nframes; n++) { 1297 1298 /* get initial length into "temp" */ 1299 error = copyin(fs_ep.pLength + n, 1300 &temp, sizeof(temp)); 1301 if (error) { 1302 return (error); 1303 } 1304 if (temp > rem) { 1305 /* the userland length has been corrupted */ 1306 DPRINTF("corrupt userland length " 1307 "%u > %u\n", temp, rem); 1308 fs_ep.status = USB_ERR_INVAL; 1309 goto complete; 1310 } 1311 rem -= temp; 1312 1313 /* get actual transfer length */ 1314 length = xfer->frlengths[n]; 1315 if (length > temp) { 1316 /* data overflow */ 1317 fs_ep.status = USB_ERR_INVAL; 1318 DPRINTF("data overflow %u > %u\n", 1319 length, temp); 1320 goto complete; 1321 } 1322 if (isread) { 1323 1324 /* we need to know the destination buffer */ 1325 error = copyin(fs_ep.ppBuffer + n, 1326 &uaddr, sizeof(uaddr)); 1327 if (error) { 1328 return (error); 1329 } 1330 if (xfer->flags_int.isochronous_xfr) { 1331 /* only one frame buffer */ 1332 kaddr = USB_ADD_BYTES( 1333 xfer->frbuffers[0].buffer, offset); 1334 } else { 1335 /* multiple frame buffers */ 1336 kaddr = xfer->frbuffers[n].buffer; 1337 } 1338 1339 /* move data */ 1340 error = copyout(kaddr, uaddr, length); 1341 if (error) { 1342 return (error); 1343 } 1344 } 1345 /* 1346 * Update offset according to initial length, which is 1347 * needed by isochronous transfers! 1348 */ 1349 offset += temp; 1350 1351 /* update length */ 1352 error = copyout(&length, 1353 fs_ep.pLength + n, sizeof(length)); 1354 if (error) { 1355 return (error); 1356 } 1357 } 1358 1359 complete: 1360 /* update "aFrames" */ 1361 error = copyout(&fs_ep.aFrames, &fs_ep_uptr->aFrames, 1362 sizeof(fs_ep.aFrames)); 1363 if (error) 1364 goto done; 1365 1366 /* update "isoc_time_complete" */ 1367 error = copyout(&fs_ep.isoc_time_complete, 1368 &fs_ep_uptr->isoc_time_complete, 1369 sizeof(fs_ep.isoc_time_complete)); 1370 if (error) 1371 goto done; 1372 /* update "status" */ 1373 error = copyout(&fs_ep.status, &fs_ep_uptr->status, 1374 sizeof(fs_ep.status)); 1375 done: 1376 return (error); 1377 } 1378 1379 static uint8_t 1380 ugen_fifo_in_use(struct usb_fifo *f, int fflags) 1381 { 1382 struct usb_fifo *f_rx; 1383 struct usb_fifo *f_tx; 1384 1385 f_rx = f->udev->fifo[(f->fifo_index & ~1) + USB_FIFO_RX]; 1386 f_tx = f->udev->fifo[(f->fifo_index & ~1) + USB_FIFO_TX]; 1387 1388 if ((fflags & FREAD) && f_rx && 1389 (f_rx->xfer[0] || f_rx->xfer[1])) { 1390 return (1); /* RX FIFO in use */ 1391 } 1392 if ((fflags & FWRITE) && f_tx && 1393 (f_tx->xfer[0] || f_tx->xfer[1])) { 1394 return (1); /* TX FIFO in use */ 1395 } 1396 return (0); /* not in use */ 1397 } 1398 1399 static int 1400 ugen_ioctl(struct usb_fifo *f, u_long cmd, void *addr, int fflags) 1401 { 1402 struct usb_config usb_config[1]; 1403 struct usb_device_request req; 1404 union { 1405 struct usb_fs_complete *pcomp; 1406 struct usb_fs_start *pstart; 1407 struct usb_fs_stop *pstop; 1408 struct usb_fs_open *popen; 1409 struct usb_fs_open_stream *popen_stream; 1410 struct usb_fs_close *pclose; 1411 struct usb_fs_clear_stall_sync *pstall; 1412 void *addr; 1413 } u; 1414 struct usb_endpoint *ep; 1415 struct usb_endpoint_descriptor *ed; 1416 struct usb_xfer *xfer; 1417 int error = 0; 1418 uint8_t iface_index; 1419 uint8_t isread; 1420 uint8_t ep_index; 1421 uint8_t pre_scale; 1422 1423 u.addr = addr; 1424 1425 DPRINTFN(6, "cmd=0x%08lx\n", cmd); 1426 1427 switch (cmd) { 1428 case USB_FS_COMPLETE: 1429 mtx_lock(f->priv_mtx); 1430 error = ugen_fs_get_complete(f, &ep_index); 1431 mtx_unlock(f->priv_mtx); 1432 1433 if (error) { 1434 error = EBUSY; 1435 break; 1436 } 1437 u.pcomp->ep_index = ep_index; 1438 error = ugen_fs_copy_out(f, u.pcomp->ep_index); 1439 break; 1440 1441 case USB_FS_START: 1442 error = ugen_fs_copy_in(f, u.pstart->ep_index); 1443 if (error) 1444 break; 1445 mtx_lock(f->priv_mtx); 1446 xfer = f->fs_xfer[u.pstart->ep_index]; 1447 usbd_transfer_start(xfer); 1448 mtx_unlock(f->priv_mtx); 1449 break; 1450 1451 case USB_FS_STOP: 1452 if (u.pstop->ep_index >= f->fs_ep_max) { 1453 error = EINVAL; 1454 break; 1455 } 1456 mtx_lock(f->priv_mtx); 1457 xfer = f->fs_xfer[u.pstart->ep_index]; 1458 if (usbd_transfer_pending(xfer)) { 1459 usbd_transfer_stop(xfer); 1460 /* 1461 * Check if the USB transfer was stopped 1462 * before it was even started. Else a cancel 1463 * callback will be pending. 1464 */ 1465 if (!xfer->flags_int.transferring) { 1466 ugen_fs_set_complete(xfer->priv_sc, 1467 USB_P2U(xfer->priv_fifo)); 1468 } 1469 } 1470 mtx_unlock(f->priv_mtx); 1471 break; 1472 1473 case USB_FS_OPEN: 1474 case USB_FS_OPEN_STREAM: 1475 if (u.popen->ep_index >= f->fs_ep_max) { 1476 error = EINVAL; 1477 break; 1478 } 1479 if (f->fs_xfer[u.popen->ep_index] != NULL) { 1480 error = EBUSY; 1481 break; 1482 } 1483 if (u.popen->max_bufsize > USB_FS_MAX_BUFSIZE) { 1484 u.popen->max_bufsize = USB_FS_MAX_BUFSIZE; 1485 } 1486 if (u.popen->max_frames & USB_FS_MAX_FRAMES_PRE_SCALE) { 1487 pre_scale = 1; 1488 u.popen->max_frames &= ~USB_FS_MAX_FRAMES_PRE_SCALE; 1489 } else { 1490 pre_scale = 0; 1491 } 1492 if (u.popen->max_frames > USB_FS_MAX_FRAMES) { 1493 u.popen->max_frames = USB_FS_MAX_FRAMES; 1494 break; 1495 } 1496 if (u.popen->max_frames == 0) { 1497 error = EINVAL; 1498 break; 1499 } 1500 ep = usbd_get_ep_by_addr(f->udev, u.popen->ep_no); 1501 if (ep == NULL) { 1502 error = EINVAL; 1503 break; 1504 } 1505 ed = ep->edesc; 1506 if (ed == NULL) { 1507 error = ENXIO; 1508 break; 1509 } 1510 iface_index = ep->iface_index; 1511 1512 memset(usb_config, 0, sizeof(usb_config)); 1513 1514 usb_config[0].type = ed->bmAttributes & UE_XFERTYPE; 1515 usb_config[0].endpoint = ed->bEndpointAddress & UE_ADDR; 1516 usb_config[0].direction = ed->bEndpointAddress & (UE_DIR_OUT | UE_DIR_IN); 1517 usb_config[0].interval = USB_DEFAULT_INTERVAL; 1518 usb_config[0].flags.proxy_buffer = 1; 1519 if (pre_scale != 0) 1520 usb_config[0].flags.pre_scale_frames = 1; 1521 usb_config[0].callback = &ugen_ctrl_fs_callback; 1522 usb_config[0].timeout = 0; /* no timeout */ 1523 usb_config[0].frames = u.popen->max_frames; 1524 usb_config[0].bufsize = u.popen->max_bufsize; 1525 usb_config[0].usb_mode = USB_MODE_DUAL; /* both modes */ 1526 if (cmd == USB_FS_OPEN_STREAM) 1527 usb_config[0].stream_id = u.popen_stream->stream_id; 1528 1529 if (usb_config[0].type == UE_CONTROL) { 1530 if (f->udev->flags.usb_mode != USB_MODE_HOST) { 1531 error = EINVAL; 1532 break; 1533 } 1534 } else { 1535 1536 isread = ((usb_config[0].endpoint & 1537 (UE_DIR_IN | UE_DIR_OUT)) == UE_DIR_IN); 1538 1539 if (f->udev->flags.usb_mode != USB_MODE_HOST) { 1540 isread = !isread; 1541 } 1542 /* check permissions */ 1543 if (isread) { 1544 if (!(fflags & FREAD)) { 1545 error = EPERM; 1546 break; 1547 } 1548 } else { 1549 if (!(fflags & FWRITE)) { 1550 error = EPERM; 1551 break; 1552 } 1553 } 1554 } 1555 error = usbd_transfer_setup(f->udev, &iface_index, 1556 f->fs_xfer + u.popen->ep_index, usb_config, 1, 1557 f, f->priv_mtx); 1558 if (error == 0) { 1559 /* update maximums */ 1560 u.popen->max_packet_length = 1561 f->fs_xfer[u.popen->ep_index]->max_frame_size; 1562 u.popen->max_bufsize = 1563 f->fs_xfer[u.popen->ep_index]->max_data_length; 1564 /* update number of frames */ 1565 u.popen->max_frames = 1566 f->fs_xfer[u.popen->ep_index]->nframes; 1567 /* store index of endpoint */ 1568 f->fs_xfer[u.popen->ep_index]->priv_fifo = 1569 ((uint8_t *)0) + u.popen->ep_index; 1570 } else { 1571 error = ENOMEM; 1572 } 1573 break; 1574 1575 case USB_FS_CLOSE: 1576 if (u.pclose->ep_index >= f->fs_ep_max) { 1577 error = EINVAL; 1578 break; 1579 } 1580 if (f->fs_xfer[u.pclose->ep_index] == NULL) { 1581 error = EINVAL; 1582 break; 1583 } 1584 usbd_transfer_unsetup(f->fs_xfer + u.pclose->ep_index, 1); 1585 break; 1586 1587 case USB_FS_CLEAR_STALL_SYNC: 1588 if (u.pstall->ep_index >= f->fs_ep_max) { 1589 error = EINVAL; 1590 break; 1591 } 1592 if (f->fs_xfer[u.pstall->ep_index] == NULL) { 1593 error = EINVAL; 1594 break; 1595 } 1596 if (f->udev->flags.usb_mode != USB_MODE_HOST) { 1597 error = EINVAL; 1598 break; 1599 } 1600 mtx_lock(f->priv_mtx); 1601 error = usbd_transfer_pending(f->fs_xfer[u.pstall->ep_index]); 1602 mtx_unlock(f->priv_mtx); 1603 1604 if (error) { 1605 return (EBUSY); 1606 } 1607 ep = f->fs_xfer[u.pstall->ep_index]->endpoint; 1608 1609 /* setup a clear-stall packet */ 1610 req.bmRequestType = UT_WRITE_ENDPOINT; 1611 req.bRequest = UR_CLEAR_FEATURE; 1612 USETW(req.wValue, UF_ENDPOINT_HALT); 1613 req.wIndex[0] = ep->edesc->bEndpointAddress; 1614 req.wIndex[1] = 0; 1615 USETW(req.wLength, 0); 1616 1617 error = usbd_do_request(f->udev, NULL, &req, NULL); 1618 if (error == 0) { 1619 usbd_clear_data_toggle(f->udev, ep); 1620 } else { 1621 error = ENXIO; 1622 } 1623 break; 1624 1625 default: 1626 error = ENOIOCTL; 1627 break; 1628 } 1629 1630 DPRINTFN(6, "error=%d\n", error); 1631 1632 return (error); 1633 } 1634 1635 static int 1636 ugen_set_short_xfer(struct usb_fifo *f, void *addr) 1637 { 1638 uint8_t t; 1639 1640 if (*(int *)addr) 1641 t = 1; 1642 else 1643 t = 0; 1644 1645 if (f->flag_short == t) { 1646 /* same value like before - accept */ 1647 return (0); 1648 } 1649 if (f->xfer[0] || f->xfer[1]) { 1650 /* cannot change this during transfer */ 1651 return (EBUSY); 1652 } 1653 f->flag_short = t; 1654 return (0); 1655 } 1656 1657 static int 1658 ugen_set_timeout(struct usb_fifo *f, void *addr) 1659 { 1660 f->timeout = *(int *)addr; 1661 if (f->timeout > 65535) { 1662 /* limit user input */ 1663 f->timeout = 65535; 1664 } 1665 return (0); 1666 } 1667 1668 static int 1669 ugen_get_frame_size(struct usb_fifo *f, void *addr) 1670 { 1671 if (f->xfer[0]) { 1672 *(int *)addr = f->xfer[0]->max_frame_size; 1673 } else { 1674 return (EINVAL); 1675 } 1676 return (0); 1677 } 1678 1679 static int 1680 ugen_set_buffer_size(struct usb_fifo *f, void *addr) 1681 { 1682 usb_frlength_t t; 1683 1684 if (*(int *)addr < 0) 1685 t = 0; /* use "wMaxPacketSize" */ 1686 else if (*(int *)addr < (256 * 1024)) 1687 t = *(int *)addr; 1688 else 1689 t = 256 * 1024; 1690 1691 if (f->bufsize == t) { 1692 /* same value like before - accept */ 1693 return (0); 1694 } 1695 if (f->xfer[0] || f->xfer[1]) { 1696 /* cannot change this during transfer */ 1697 return (EBUSY); 1698 } 1699 f->bufsize = t; 1700 return (0); 1701 } 1702 1703 static int 1704 ugen_get_buffer_size(struct usb_fifo *f, void *addr) 1705 { 1706 *(int *)addr = f->bufsize; 1707 return (0); 1708 } 1709 1710 static int 1711 ugen_get_iface_desc(struct usb_fifo *f, 1712 struct usb_interface_descriptor *idesc) 1713 { 1714 struct usb_interface *iface; 1715 1716 iface = usbd_get_iface(f->udev, f->iface_index); 1717 if (iface && iface->idesc) { 1718 *idesc = *(iface->idesc); 1719 } else { 1720 return (EIO); 1721 } 1722 return (0); 1723 } 1724 1725 static int 1726 ugen_get_endpoint_desc(struct usb_fifo *f, 1727 struct usb_endpoint_descriptor *ed) 1728 { 1729 struct usb_endpoint *ep; 1730 1731 ep = usb_fifo_softc(f); 1732 1733 if (ep && ep->edesc) { 1734 *ed = *ep->edesc; 1735 } else { 1736 return (EINVAL); 1737 } 1738 return (0); 1739 } 1740 1741 static int 1742 ugen_set_power_mode(struct usb_fifo *f, int mode) 1743 { 1744 struct usb_device *udev = f->udev; 1745 int err; 1746 uint8_t old_mode; 1747 1748 if ((udev == NULL) || 1749 (udev->parent_hub == NULL)) { 1750 return (EINVAL); 1751 } 1752 err = priv_check(curthread, PRIV_DRIVER); 1753 if (err) 1754 return (err); 1755 1756 /* get old power mode */ 1757 old_mode = udev->power_mode; 1758 1759 /* if no change, then just return */ 1760 if (old_mode == mode) 1761 return (0); 1762 1763 switch (mode) { 1764 case USB_POWER_MODE_OFF: 1765 if (udev->flags.usb_mode == USB_MODE_HOST && 1766 udev->re_enumerate_wait == USB_RE_ENUM_DONE) { 1767 udev->re_enumerate_wait = USB_RE_ENUM_PWR_OFF; 1768 } 1769 /* set power mode will wake up the explore thread */ 1770 break; 1771 1772 case USB_POWER_MODE_ON: 1773 case USB_POWER_MODE_SAVE: 1774 break; 1775 1776 case USB_POWER_MODE_RESUME: 1777 #if USB_HAVE_POWERD 1778 /* let USB-powerd handle resume */ 1779 USB_BUS_LOCK(udev->bus); 1780 udev->pwr_save.write_refs++; 1781 udev->pwr_save.last_xfer_time = ticks; 1782 USB_BUS_UNLOCK(udev->bus); 1783 1784 /* set new power mode */ 1785 usbd_set_power_mode(udev, USB_POWER_MODE_SAVE); 1786 1787 /* wait for resume to complete */ 1788 usb_pause_mtx(NULL, hz / 4); 1789 1790 /* clear write reference */ 1791 USB_BUS_LOCK(udev->bus); 1792 udev->pwr_save.write_refs--; 1793 USB_BUS_UNLOCK(udev->bus); 1794 #endif 1795 mode = USB_POWER_MODE_SAVE; 1796 break; 1797 1798 case USB_POWER_MODE_SUSPEND: 1799 #if USB_HAVE_POWERD 1800 /* let USB-powerd handle suspend */ 1801 USB_BUS_LOCK(udev->bus); 1802 udev->pwr_save.last_xfer_time = ticks - (256 * hz); 1803 USB_BUS_UNLOCK(udev->bus); 1804 #endif 1805 mode = USB_POWER_MODE_SAVE; 1806 break; 1807 1808 default: 1809 return (EINVAL); 1810 } 1811 1812 if (err) 1813 return (ENXIO); /* I/O failure */ 1814 1815 /* if we are powered off we need to re-enumerate first */ 1816 if (old_mode == USB_POWER_MODE_OFF) { 1817 if (udev->flags.usb_mode == USB_MODE_HOST && 1818 udev->re_enumerate_wait == USB_RE_ENUM_DONE) { 1819 udev->re_enumerate_wait = USB_RE_ENUM_START; 1820 } 1821 /* set power mode will wake up the explore thread */ 1822 } 1823 1824 /* set new power mode */ 1825 usbd_set_power_mode(udev, mode); 1826 1827 return (0); /* success */ 1828 } 1829 1830 static int 1831 ugen_get_power_mode(struct usb_fifo *f) 1832 { 1833 struct usb_device *udev = f->udev; 1834 1835 if (udev == NULL) 1836 return (USB_POWER_MODE_ON); 1837 1838 return (udev->power_mode); 1839 } 1840 1841 static int 1842 ugen_get_port_path(struct usb_fifo *f, struct usb_device_port_path *dpp) 1843 { 1844 struct usb_device *udev = f->udev; 1845 struct usb_device *next; 1846 unsigned int nlevel = 0; 1847 1848 if (udev == NULL) 1849 goto error; 1850 1851 dpp->udp_bus = device_get_unit(udev->bus->bdev); 1852 dpp->udp_index = udev->device_index; 1853 1854 /* count port levels */ 1855 next = udev; 1856 while (next->parent_hub != NULL) { 1857 nlevel++; 1858 next = next->parent_hub; 1859 } 1860 1861 /* check if too many levels */ 1862 if (nlevel > USB_DEVICE_PORT_PATH_MAX) 1863 goto error; 1864 1865 /* store port index array */ 1866 next = udev; 1867 while (next->parent_hub != NULL) { 1868 nlevel--; 1869 1870 dpp->udp_port_no[nlevel] = next->port_no; 1871 dpp->udp_port_level = nlevel; 1872 1873 next = next->parent_hub; 1874 } 1875 return (0); /* success */ 1876 1877 error: 1878 return (EINVAL); /* failure */ 1879 } 1880 1881 static int 1882 ugen_get_power_usage(struct usb_fifo *f) 1883 { 1884 struct usb_device *udev = f->udev; 1885 1886 if (udev == NULL) 1887 return (0); 1888 1889 return (udev->power); 1890 } 1891 1892 static int 1893 ugen_do_port_feature(struct usb_fifo *f, uint8_t port_no, 1894 uint8_t set, uint16_t feature) 1895 { 1896 struct usb_device *udev = f->udev; 1897 struct usb_hub *hub; 1898 int err; 1899 1900 err = priv_check(curthread, PRIV_DRIVER); 1901 if (err) { 1902 return (err); 1903 } 1904 if (port_no == 0) { 1905 return (EINVAL); 1906 } 1907 if ((udev == NULL) || 1908 (udev->hub == NULL)) { 1909 return (EINVAL); 1910 } 1911 hub = udev->hub; 1912 1913 if (port_no > hub->nports) { 1914 return (EINVAL); 1915 } 1916 if (set) 1917 err = usbd_req_set_port_feature(udev, 1918 NULL, port_no, feature); 1919 else 1920 err = usbd_req_clear_port_feature(udev, 1921 NULL, port_no, feature); 1922 1923 if (err) 1924 return (ENXIO); /* failure */ 1925 1926 return (0); /* success */ 1927 } 1928 1929 static int 1930 ugen_iface_ioctl(struct usb_fifo *f, u_long cmd, void *addr, int fflags) 1931 { 1932 struct usb_fifo *f_rx; 1933 struct usb_fifo *f_tx; 1934 int error = 0; 1935 1936 f_rx = f->udev->fifo[(f->fifo_index & ~1) + USB_FIFO_RX]; 1937 f_tx = f->udev->fifo[(f->fifo_index & ~1) + USB_FIFO_TX]; 1938 1939 switch (cmd) { 1940 case USB_SET_RX_SHORT_XFER: 1941 if (fflags & FREAD) { 1942 error = ugen_set_short_xfer(f_rx, addr); 1943 } else { 1944 error = EINVAL; 1945 } 1946 break; 1947 1948 case USB_SET_TX_FORCE_SHORT: 1949 if (fflags & FWRITE) { 1950 error = ugen_set_short_xfer(f_tx, addr); 1951 } else { 1952 error = EINVAL; 1953 } 1954 break; 1955 1956 case USB_SET_RX_TIMEOUT: 1957 if (fflags & FREAD) { 1958 error = ugen_set_timeout(f_rx, addr); 1959 } else { 1960 error = EINVAL; 1961 } 1962 break; 1963 1964 case USB_SET_TX_TIMEOUT: 1965 if (fflags & FWRITE) { 1966 error = ugen_set_timeout(f_tx, addr); 1967 } else { 1968 error = EINVAL; 1969 } 1970 break; 1971 1972 case USB_GET_RX_FRAME_SIZE: 1973 if (fflags & FREAD) { 1974 error = ugen_get_frame_size(f_rx, addr); 1975 } else { 1976 error = EINVAL; 1977 } 1978 break; 1979 1980 case USB_GET_TX_FRAME_SIZE: 1981 if (fflags & FWRITE) { 1982 error = ugen_get_frame_size(f_tx, addr); 1983 } else { 1984 error = EINVAL; 1985 } 1986 break; 1987 1988 case USB_SET_RX_BUFFER_SIZE: 1989 if (fflags & FREAD) { 1990 error = ugen_set_buffer_size(f_rx, addr); 1991 } else { 1992 error = EINVAL; 1993 } 1994 break; 1995 1996 case USB_SET_TX_BUFFER_SIZE: 1997 if (fflags & FWRITE) { 1998 error = ugen_set_buffer_size(f_tx, addr); 1999 } else { 2000 error = EINVAL; 2001 } 2002 break; 2003 2004 case USB_GET_RX_BUFFER_SIZE: 2005 if (fflags & FREAD) { 2006 error = ugen_get_buffer_size(f_rx, addr); 2007 } else { 2008 error = EINVAL; 2009 } 2010 break; 2011 2012 case USB_GET_TX_BUFFER_SIZE: 2013 if (fflags & FWRITE) { 2014 error = ugen_get_buffer_size(f_tx, addr); 2015 } else { 2016 error = EINVAL; 2017 } 2018 break; 2019 2020 case USB_GET_RX_INTERFACE_DESC: 2021 if (fflags & FREAD) { 2022 error = ugen_get_iface_desc(f_rx, addr); 2023 } else { 2024 error = EINVAL; 2025 } 2026 break; 2027 2028 case USB_GET_TX_INTERFACE_DESC: 2029 if (fflags & FWRITE) { 2030 error = ugen_get_iface_desc(f_tx, addr); 2031 } else { 2032 error = EINVAL; 2033 } 2034 break; 2035 2036 case USB_GET_RX_ENDPOINT_DESC: 2037 if (fflags & FREAD) { 2038 error = ugen_get_endpoint_desc(f_rx, addr); 2039 } else { 2040 error = EINVAL; 2041 } 2042 break; 2043 2044 case USB_GET_TX_ENDPOINT_DESC: 2045 if (fflags & FWRITE) { 2046 error = ugen_get_endpoint_desc(f_tx, addr); 2047 } else { 2048 error = EINVAL; 2049 } 2050 break; 2051 2052 case USB_SET_RX_STALL_FLAG: 2053 if ((fflags & FREAD) && (*(int *)addr)) { 2054 f_rx->flag_stall = 1; 2055 } 2056 break; 2057 2058 case USB_SET_TX_STALL_FLAG: 2059 if ((fflags & FWRITE) && (*(int *)addr)) { 2060 f_tx->flag_stall = 1; 2061 } 2062 break; 2063 2064 default: 2065 error = ENOIOCTL; 2066 break; 2067 } 2068 return (error); 2069 } 2070 2071 static int 2072 ugen_ioctl_post(struct usb_fifo *f, u_long cmd, void *addr, int fflags) 2073 { 2074 union { 2075 struct usb_interface_descriptor *idesc; 2076 struct usb_alt_interface *ai; 2077 struct usb_device_descriptor *ddesc; 2078 struct usb_config_descriptor *cdesc; 2079 struct usb_device_stats *stat; 2080 struct usb_fs_init *pinit; 2081 struct usb_fs_uninit *puninit; 2082 struct usb_device_port_path *dpp; 2083 uint32_t *ptime; 2084 void *addr; 2085 int *pint; 2086 } u; 2087 struct usb_device_descriptor *dtemp; 2088 struct usb_config_descriptor *ctemp; 2089 struct usb_interface *iface; 2090 int error = 0; 2091 uint8_t n; 2092 2093 u.addr = addr; 2094 2095 DPRINTFN(6, "cmd=0x%08lx\n", cmd); 2096 2097 switch (cmd) { 2098 case USB_DISCOVER: 2099 usb_needs_explore_all(); 2100 break; 2101 2102 case USB_SETDEBUG: 2103 if (!(fflags & FWRITE)) { 2104 error = EPERM; 2105 break; 2106 } 2107 usb_debug = *(int *)addr; 2108 break; 2109 2110 case USB_GET_CONFIG: 2111 *(int *)addr = f->udev->curr_config_index; 2112 break; 2113 2114 case USB_SET_CONFIG: 2115 if (!(fflags & FWRITE)) { 2116 error = EPERM; 2117 break; 2118 } 2119 error = ugen_set_config(f, *(int *)addr); 2120 break; 2121 2122 case USB_GET_ALTINTERFACE: 2123 iface = usbd_get_iface(f->udev, 2124 u.ai->uai_interface_index); 2125 if (iface && iface->idesc) { 2126 u.ai->uai_alt_index = iface->alt_index; 2127 } else { 2128 error = EINVAL; 2129 } 2130 break; 2131 2132 case USB_SET_ALTINTERFACE: 2133 if (!(fflags & FWRITE)) { 2134 error = EPERM; 2135 break; 2136 } 2137 error = ugen_set_interface(f, 2138 u.ai->uai_interface_index, u.ai->uai_alt_index); 2139 break; 2140 2141 case USB_GET_DEVICE_DESC: 2142 dtemp = usbd_get_device_descriptor(f->udev); 2143 if (!dtemp) { 2144 error = EIO; 2145 break; 2146 } 2147 *u.ddesc = *dtemp; 2148 break; 2149 2150 case USB_GET_CONFIG_DESC: 2151 ctemp = usbd_get_config_descriptor(f->udev); 2152 if (!ctemp) { 2153 error = EIO; 2154 break; 2155 } 2156 *u.cdesc = *ctemp; 2157 break; 2158 2159 case USB_GET_FULL_DESC: 2160 error = ugen_get_cdesc(f, addr); 2161 break; 2162 2163 case USB_GET_STRING_DESC: 2164 error = ugen_get_sdesc(f, addr); 2165 break; 2166 2167 case USB_GET_IFACE_DRIVER: 2168 error = ugen_get_iface_driver(f, addr); 2169 break; 2170 2171 case USB_REQUEST: 2172 case USB_DO_REQUEST: 2173 if (!(fflags & FWRITE)) { 2174 error = EPERM; 2175 break; 2176 } 2177 error = ugen_do_request(f, addr); 2178 break; 2179 2180 case USB_DEVICEINFO: 2181 case USB_GET_DEVICEINFO: 2182 error = usb_gen_fill_deviceinfo(f, addr); 2183 break; 2184 2185 case USB_DEVICESTATS: 2186 for (n = 0; n != 4; n++) { 2187 2188 u.stat->uds_requests_fail[n] = 2189 f->udev->bus->stats_err.uds_requests[n]; 2190 2191 u.stat->uds_requests_ok[n] = 2192 f->udev->bus->stats_ok.uds_requests[n]; 2193 } 2194 break; 2195 2196 case USB_DEVICEENUMERATE: 2197 error = ugen_re_enumerate(f); 2198 break; 2199 2200 case USB_GET_PLUGTIME: 2201 *u.ptime = f->udev->plugtime; 2202 break; 2203 2204 case USB_CLAIM_INTERFACE: 2205 case USB_RELEASE_INTERFACE: 2206 /* TODO */ 2207 break; 2208 2209 case USB_IFACE_DRIVER_ACTIVE: 2210 2211 n = *u.pint & 0xFF; 2212 2213 iface = usbd_get_iface(f->udev, n); 2214 2215 if (iface && iface->subdev) 2216 error = 0; 2217 else 2218 error = ENXIO; 2219 break; 2220 2221 case USB_IFACE_DRIVER_DETACH: 2222 2223 error = priv_check(curthread, PRIV_DRIVER); 2224 2225 if (error) 2226 break; 2227 2228 n = *u.pint & 0xFF; 2229 2230 if (n == USB_IFACE_INDEX_ANY) { 2231 error = EINVAL; 2232 break; 2233 } 2234 2235 /* 2236 * Detach the currently attached driver. 2237 */ 2238 usb_detach_device(f->udev, n, 0); 2239 2240 /* 2241 * Set parent to self, this should keep attach away 2242 * until the next set configuration event. 2243 */ 2244 usbd_set_parent_iface(f->udev, n, n); 2245 break; 2246 2247 case USB_SET_POWER_MODE: 2248 error = ugen_set_power_mode(f, *u.pint); 2249 break; 2250 2251 case USB_GET_POWER_MODE: 2252 *u.pint = ugen_get_power_mode(f); 2253 break; 2254 2255 case USB_GET_DEV_PORT_PATH: 2256 error = ugen_get_port_path(f, u.dpp); 2257 break; 2258 2259 case USB_GET_POWER_USAGE: 2260 *u.pint = ugen_get_power_usage(f); 2261 break; 2262 2263 case USB_SET_PORT_ENABLE: 2264 error = ugen_do_port_feature(f, 2265 *u.pint, 1, UHF_PORT_ENABLE); 2266 break; 2267 2268 case USB_SET_PORT_DISABLE: 2269 error = ugen_do_port_feature(f, 2270 *u.pint, 0, UHF_PORT_ENABLE); 2271 break; 2272 2273 case USB_FS_INIT: 2274 /* verify input parameters */ 2275 if (u.pinit->pEndpoints == NULL) { 2276 error = EINVAL; 2277 break; 2278 } 2279 if (u.pinit->ep_index_max > 127) { 2280 error = EINVAL; 2281 break; 2282 } 2283 if (u.pinit->ep_index_max == 0) { 2284 error = EINVAL; 2285 break; 2286 } 2287 if (f->fs_xfer != NULL) { 2288 error = EBUSY; 2289 break; 2290 } 2291 if (f->dev_ep_index != 0) { 2292 error = EINVAL; 2293 break; 2294 } 2295 if (ugen_fifo_in_use(f, fflags)) { 2296 error = EBUSY; 2297 break; 2298 } 2299 error = usb_fifo_alloc_buffer(f, 1, u.pinit->ep_index_max); 2300 if (error) { 2301 break; 2302 } 2303 f->fs_xfer = malloc(sizeof(f->fs_xfer[0]) * 2304 u.pinit->ep_index_max, M_USB, M_WAITOK | M_ZERO); 2305 if (f->fs_xfer == NULL) { 2306 usb_fifo_free_buffer(f); 2307 error = ENOMEM; 2308 break; 2309 } 2310 f->fs_ep_max = u.pinit->ep_index_max; 2311 f->fs_ep_ptr = u.pinit->pEndpoints; 2312 break; 2313 2314 case USB_FS_UNINIT: 2315 if (u.puninit->dummy != 0) { 2316 error = EINVAL; 2317 break; 2318 } 2319 error = ugen_fs_uninit(f); 2320 break; 2321 2322 default: 2323 mtx_lock(f->priv_mtx); 2324 error = ugen_iface_ioctl(f, cmd, addr, fflags); 2325 mtx_unlock(f->priv_mtx); 2326 break; 2327 } 2328 DPRINTFN(6, "error=%d\n", error); 2329 return (error); 2330 } 2331 2332 static void 2333 ugen_ctrl_fs_callback(struct usb_xfer *xfer, usb_error_t error) 2334 { 2335 ; /* workaround for a bug in "indent" */ 2336 2337 DPRINTF("st=%u alen=%u aframes=%u\n", 2338 USB_GET_STATE(xfer), xfer->actlen, xfer->aframes); 2339 2340 switch (USB_GET_STATE(xfer)) { 2341 case USB_ST_SETUP: 2342 usbd_transfer_submit(xfer); 2343 break; 2344 default: 2345 ugen_fs_set_complete(xfer->priv_sc, USB_P2U(xfer->priv_fifo)); 2346 break; 2347 } 2348 } 2349 #endif /* USB_HAVE_UGEN */ 2350