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