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 static 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 memset(usb_config, 0, 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].stream_id = 0; /* XXX support more stream ID's */ 257 usb_config[0].direction = UE_DIR_TX; 258 usb_config[0].interval = USB_DEFAULT_INTERVAL; 259 usb_config[0].flags.proxy_buffer = 1; 260 usb_config[0].usb_mode = USB_MODE_DUAL; /* both modes */ 261 262 switch (ed->bmAttributes & UE_XFERTYPE) { 263 case UE_INTERRUPT: 264 case UE_BULK: 265 if (f->flag_short) { 266 usb_config[0].flags.force_short_xfer = 1; 267 } 268 usb_config[0].callback = &ugen_ctrl_write_callback; 269 usb_config[0].timeout = f->timeout; 270 usb_config[0].frames = 1; 271 usb_config[0].bufsize = f->bufsize; 272 if (ugen_transfer_setup(f, usb_config, 2)) { 273 return (EIO); 274 } 275 /* first transfer does not clear stall */ 276 f->flag_stall = 0; 277 break; 278 279 case UE_ISOCHRONOUS: 280 usb_config[0].flags.short_xfer_ok = 1; 281 usb_config[0].bufsize = 0; /* use default */ 282 usb_config[0].frames = f->nframes; 283 usb_config[0].callback = &ugen_isoc_write_callback; 284 usb_config[0].timeout = 0; 285 286 /* clone configuration */ 287 usb_config[1] = usb_config[0]; 288 289 if (ugen_transfer_setup(f, usb_config, 2)) { 290 return (EIO); 291 } 292 break; 293 default: 294 return (EINVAL); 295 } 296 return (0); 297 } 298 299 static int 300 ugen_open_pipe_read(struct usb_fifo *f) 301 { 302 struct usb_config usb_config[2]; 303 struct usb_endpoint *ep = usb_fifo_softc(f); 304 struct usb_endpoint_descriptor *ed = ep->edesc; 305 306 mtx_assert(f->priv_mtx, MA_OWNED); 307 308 if (f->xfer[0] || f->xfer[1]) { 309 /* transfers are already opened */ 310 return (0); 311 } 312 memset(usb_config, 0, sizeof(usb_config)); 313 314 usb_config[1].type = UE_CONTROL; 315 usb_config[1].endpoint = 0; 316 usb_config[1].direction = UE_DIR_ANY; 317 usb_config[1].timeout = 1000; /* 1 second */ 318 usb_config[1].interval = 50;/* 50 milliseconds */ 319 usb_config[1].bufsize = sizeof(struct usb_device_request); 320 usb_config[1].callback = &ugen_read_clear_stall_callback; 321 usb_config[1].usb_mode = USB_MODE_HOST; 322 323 usb_config[0].type = ed->bmAttributes & UE_XFERTYPE; 324 usb_config[0].endpoint = ed->bEndpointAddress & UE_ADDR; 325 usb_config[0].stream_id = 0; /* XXX support more stream ID's */ 326 usb_config[0].direction = UE_DIR_RX; 327 usb_config[0].interval = USB_DEFAULT_INTERVAL; 328 usb_config[0].flags.proxy_buffer = 1; 329 usb_config[0].usb_mode = USB_MODE_DUAL; /* both modes */ 330 331 switch (ed->bmAttributes & UE_XFERTYPE) { 332 case UE_INTERRUPT: 333 case UE_BULK: 334 if (f->flag_short) { 335 usb_config[0].flags.short_xfer_ok = 1; 336 } 337 usb_config[0].timeout = f->timeout; 338 usb_config[0].frames = 1; 339 usb_config[0].callback = &ugen_ctrl_read_callback; 340 usb_config[0].bufsize = f->bufsize; 341 342 if (ugen_transfer_setup(f, usb_config, 2)) { 343 return (EIO); 344 } 345 /* first transfer does not clear stall */ 346 f->flag_stall = 0; 347 break; 348 349 case UE_ISOCHRONOUS: 350 usb_config[0].flags.short_xfer_ok = 1; 351 usb_config[0].bufsize = 0; /* use default */ 352 usb_config[0].frames = f->nframes; 353 usb_config[0].callback = &ugen_isoc_read_callback; 354 usb_config[0].timeout = 0; 355 356 /* clone configuration */ 357 usb_config[1] = usb_config[0]; 358 359 if (ugen_transfer_setup(f, usb_config, 2)) { 360 return (EIO); 361 } 362 break; 363 364 default: 365 return (EINVAL); 366 } 367 return (0); 368 } 369 370 static void 371 ugen_start_read(struct usb_fifo *f) 372 { 373 /* check that pipes are open */ 374 if (ugen_open_pipe_read(f)) { 375 /* signal error */ 376 usb_fifo_put_data_error(f); 377 } 378 /* start transfers */ 379 usbd_transfer_start(f->xfer[0]); 380 usbd_transfer_start(f->xfer[1]); 381 } 382 383 static void 384 ugen_start_write(struct usb_fifo *f) 385 { 386 /* check that pipes are open */ 387 if (ugen_open_pipe_write(f)) { 388 /* signal error */ 389 usb_fifo_get_data_error(f); 390 } 391 /* start transfers */ 392 usbd_transfer_start(f->xfer[0]); 393 usbd_transfer_start(f->xfer[1]); 394 } 395 396 static void 397 ugen_stop_io(struct usb_fifo *f) 398 { 399 /* stop transfers */ 400 usbd_transfer_stop(f->xfer[0]); 401 usbd_transfer_stop(f->xfer[1]); 402 } 403 404 static void 405 ugen_ctrl_read_callback(struct usb_xfer *xfer, usb_error_t error) 406 { 407 struct usb_fifo *f = usbd_xfer_softc(xfer); 408 struct usb_mbuf *m; 409 410 DPRINTFN(4, "actlen=%u, aframes=%u\n", xfer->actlen, xfer->aframes); 411 412 switch (USB_GET_STATE(xfer)) { 413 case USB_ST_TRANSFERRED: 414 if (xfer->actlen == 0) { 415 if (f->fifo_zlp != 4) { 416 f->fifo_zlp++; 417 } else { 418 /* 419 * Throttle a little bit we have multiple ZLPs 420 * in a row! 421 */ 422 xfer->interval = 64; /* ms */ 423 } 424 } else { 425 /* clear throttle */ 426 xfer->interval = 0; 427 f->fifo_zlp = 0; 428 } 429 usb_fifo_put_data(f, xfer->frbuffers, 0, 430 xfer->actlen, 1); 431 432 case USB_ST_SETUP: 433 if (f->flag_stall) { 434 usbd_transfer_start(f->xfer[1]); 435 break; 436 } 437 USB_IF_POLL(&f->free_q, m); 438 if (m) { 439 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 440 usbd_transfer_submit(xfer); 441 } 442 break; 443 444 default: /* Error */ 445 if (xfer->error != USB_ERR_CANCELLED) { 446 /* send a zero length packet to userland */ 447 usb_fifo_put_data(f, xfer->frbuffers, 0, 0, 1); 448 f->flag_stall = 1; 449 f->fifo_zlp = 0; 450 usbd_transfer_start(f->xfer[1]); 451 } 452 break; 453 } 454 } 455 456 static void 457 ugen_ctrl_write_callback(struct usb_xfer *xfer, usb_error_t error) 458 { 459 struct usb_fifo *f = usbd_xfer_softc(xfer); 460 usb_frlength_t actlen; 461 462 DPRINTFN(4, "actlen=%u, aframes=%u\n", xfer->actlen, xfer->aframes); 463 464 switch (USB_GET_STATE(xfer)) { 465 case USB_ST_SETUP: 466 case USB_ST_TRANSFERRED: 467 /* 468 * If writing is in stall, just jump to clear stall 469 * callback and solve the situation. 470 */ 471 if (f->flag_stall) { 472 usbd_transfer_start(f->xfer[1]); 473 break; 474 } 475 /* 476 * Write data, setup and perform hardware transfer. 477 */ 478 if (usb_fifo_get_data(f, xfer->frbuffers, 0, 479 xfer->max_data_length, &actlen, 0)) { 480 usbd_xfer_set_frame_len(xfer, 0, actlen); 481 usbd_transfer_submit(xfer); 482 } 483 break; 484 485 default: /* Error */ 486 if (xfer->error != USB_ERR_CANCELLED) { 487 f->flag_stall = 1; 488 usbd_transfer_start(f->xfer[1]); 489 } 490 break; 491 } 492 } 493 494 static void 495 ugen_read_clear_stall_callback(struct usb_xfer *xfer, usb_error_t error) 496 { 497 struct usb_fifo *f = usbd_xfer_softc(xfer); 498 struct usb_xfer *xfer_other = f->xfer[0]; 499 500 if (f->flag_stall == 0) { 501 /* nothing to do */ 502 return; 503 } 504 if (usbd_clear_stall_callback(xfer, xfer_other)) { 505 DPRINTFN(5, "f=%p: stall cleared\n", f); 506 f->flag_stall = 0; 507 usbd_transfer_start(xfer_other); 508 } 509 } 510 511 static void 512 ugen_write_clear_stall_callback(struct usb_xfer *xfer, usb_error_t error) 513 { 514 struct usb_fifo *f = usbd_xfer_softc(xfer); 515 struct usb_xfer *xfer_other = f->xfer[0]; 516 517 if (f->flag_stall == 0) { 518 /* nothing to do */ 519 return; 520 } 521 if (usbd_clear_stall_callback(xfer, xfer_other)) { 522 DPRINTFN(5, "f=%p: stall cleared\n", f); 523 f->flag_stall = 0; 524 usbd_transfer_start(xfer_other); 525 } 526 } 527 528 static void 529 ugen_isoc_read_callback(struct usb_xfer *xfer, usb_error_t error) 530 { 531 struct usb_fifo *f = usbd_xfer_softc(xfer); 532 usb_frlength_t offset; 533 usb_frcount_t n; 534 535 DPRINTFN(4, "actlen=%u, aframes=%u\n", xfer->actlen, xfer->aframes); 536 537 switch (USB_GET_STATE(xfer)) { 538 case USB_ST_TRANSFERRED: 539 540 DPRINTFN(6, "actlen=%d\n", xfer->actlen); 541 542 offset = 0; 543 544 for (n = 0; n != xfer->aframes; n++) { 545 usb_fifo_put_data(f, xfer->frbuffers, offset, 546 xfer->frlengths[n], 1); 547 offset += xfer->max_frame_size; 548 } 549 550 case USB_ST_SETUP: 551 tr_setup: 552 for (n = 0; n != xfer->nframes; n++) { 553 /* setup size for next transfer */ 554 usbd_xfer_set_frame_len(xfer, n, xfer->max_frame_size); 555 } 556 usbd_transfer_submit(xfer); 557 break; 558 559 default: /* Error */ 560 if (xfer->error == USB_ERR_CANCELLED) { 561 break; 562 } 563 goto tr_setup; 564 } 565 } 566 567 static void 568 ugen_isoc_write_callback(struct usb_xfer *xfer, usb_error_t error) 569 { 570 struct usb_fifo *f = usbd_xfer_softc(xfer); 571 usb_frlength_t actlen; 572 usb_frlength_t offset; 573 usb_frcount_t n; 574 575 DPRINTFN(4, "actlen=%u, aframes=%u\n", xfer->actlen, xfer->aframes); 576 577 switch (USB_GET_STATE(xfer)) { 578 case USB_ST_TRANSFERRED: 579 case USB_ST_SETUP: 580 tr_setup: 581 offset = 0; 582 for (n = 0; n != xfer->nframes; n++) { 583 if (usb_fifo_get_data(f, xfer->frbuffers, offset, 584 xfer->max_frame_size, &actlen, 1)) { 585 usbd_xfer_set_frame_len(xfer, n, actlen); 586 offset += actlen; 587 } else { 588 break; 589 } 590 } 591 592 for (; n != xfer->nframes; n++) { 593 /* fill in zero frames */ 594 usbd_xfer_set_frame_len(xfer, n, 0); 595 } 596 usbd_transfer_submit(xfer); 597 break; 598 599 default: /* Error */ 600 if (xfer->error == USB_ERR_CANCELLED) { 601 break; 602 } 603 goto tr_setup; 604 } 605 } 606 607 static int 608 ugen_set_config(struct usb_fifo *f, uint8_t index) 609 { 610 DPRINTFN(2, "index %u\n", index); 611 612 if (f->udev->flags.usb_mode != USB_MODE_HOST) { 613 /* not possible in device side mode */ 614 return (ENOTTY); 615 } 616 if (f->udev->curr_config_index == index) { 617 /* no change needed */ 618 return (0); 619 } 620 /* make sure all FIFO's are gone */ 621 /* else there can be a deadlock */ 622 if (ugen_fs_uninit(f)) { 623 /* ignore any errors */ 624 DPRINTFN(6, "no FIFOs\n"); 625 } 626 /* change setting - will free generic FIFOs, if any */ 627 if (usbd_set_config_index(f->udev, index)) { 628 return (EIO); 629 } 630 /* probe and attach */ 631 if (usb_probe_and_attach(f->udev, USB_IFACE_INDEX_ANY)) { 632 return (EIO); 633 } 634 return (0); 635 } 636 637 static int 638 ugen_set_interface(struct usb_fifo *f, 639 uint8_t iface_index, uint8_t alt_index) 640 { 641 DPRINTFN(2, "%u, %u\n", iface_index, alt_index); 642 643 if (f->udev->flags.usb_mode != USB_MODE_HOST) { 644 /* not possible in device side mode */ 645 return (ENOTTY); 646 } 647 /* make sure all FIFO's are gone */ 648 /* else there can be a deadlock */ 649 if (ugen_fs_uninit(f)) { 650 /* ignore any errors */ 651 DPRINTFN(6, "no FIFOs\n"); 652 } 653 /* change setting - will free generic FIFOs, if any */ 654 if (usbd_set_alt_interface_index(f->udev, iface_index, alt_index)) { 655 return (EIO); 656 } 657 /* probe and attach */ 658 if (usb_probe_and_attach(f->udev, iface_index)) { 659 return (EIO); 660 } 661 return (0); 662 } 663 664 /*------------------------------------------------------------------------* 665 * ugen_get_cdesc 666 * 667 * This function will retrieve the complete configuration descriptor 668 * at the given index. 669 *------------------------------------------------------------------------*/ 670 static int 671 ugen_get_cdesc(struct usb_fifo *f, struct usb_gen_descriptor *ugd) 672 { 673 struct usb_config_descriptor *cdesc; 674 struct usb_device *udev = f->udev; 675 int error; 676 uint16_t len; 677 uint8_t free_data; 678 679 DPRINTFN(6, "\n"); 680 681 if (ugd->ugd_data == NULL) { 682 /* userland pointer should not be zero */ 683 return (EINVAL); 684 } 685 if ((ugd->ugd_config_index == USB_UNCONFIG_INDEX) || 686 (ugd->ugd_config_index == udev->curr_config_index)) { 687 cdesc = usbd_get_config_descriptor(udev); 688 if (cdesc == NULL) { 689 return (ENXIO); 690 } 691 free_data = 0; 692 693 } else { 694 if (usbd_req_get_config_desc_full(udev, 695 NULL, &cdesc, M_USBDEV, 696 ugd->ugd_config_index)) { 697 return (ENXIO); 698 } 699 free_data = 1; 700 } 701 702 len = UGETW(cdesc->wTotalLength); 703 if (len > ugd->ugd_maxlen) { 704 len = ugd->ugd_maxlen; 705 } 706 DPRINTFN(6, "len=%u\n", len); 707 708 ugd->ugd_actlen = len; 709 ugd->ugd_offset = 0; 710 711 error = copyout(cdesc, ugd->ugd_data, len); 712 713 if (free_data) { 714 free(cdesc, M_USBDEV); 715 } 716 return (error); 717 } 718 719 static int 720 ugen_get_sdesc(struct usb_fifo *f, struct usb_gen_descriptor *ugd) 721 { 722 void *ptr = f->udev->bus->scratch[0].data; 723 uint16_t size = sizeof(f->udev->bus->scratch[0].data); 724 int error; 725 726 if (usbd_req_get_string_desc(f->udev, NULL, ptr, 727 size, ugd->ugd_lang_id, ugd->ugd_string_index)) { 728 error = EINVAL; 729 } else { 730 731 if (size > ((uint8_t *)ptr)[0]) { 732 size = ((uint8_t *)ptr)[0]; 733 } 734 if (size > ugd->ugd_maxlen) { 735 size = ugd->ugd_maxlen; 736 } 737 ugd->ugd_actlen = size; 738 ugd->ugd_offset = 0; 739 740 error = copyout(ptr, ugd->ugd_data, size); 741 } 742 return (error); 743 } 744 745 /*------------------------------------------------------------------------* 746 * ugen_get_iface_driver 747 * 748 * This function generates an USB interface description for userland. 749 * 750 * Returns: 751 * 0: Success 752 * Else: Failure 753 *------------------------------------------------------------------------*/ 754 static int 755 ugen_get_iface_driver(struct usb_fifo *f, struct usb_gen_descriptor *ugd) 756 { 757 struct usb_device *udev = f->udev; 758 struct usb_interface *iface; 759 const char *ptr; 760 const char *desc; 761 unsigned int len; 762 unsigned int maxlen; 763 char buf[128]; 764 int error; 765 766 DPRINTFN(6, "\n"); 767 768 if ((ugd->ugd_data == NULL) || (ugd->ugd_maxlen == 0)) { 769 /* userland pointer should not be zero */ 770 return (EINVAL); 771 } 772 773 iface = usbd_get_iface(udev, ugd->ugd_iface_index); 774 if ((iface == NULL) || (iface->idesc == NULL)) { 775 /* invalid interface index */ 776 return (EINVAL); 777 } 778 779 /* read out device nameunit string, if any */ 780 if ((iface->subdev != NULL) && 781 device_is_attached(iface->subdev) && 782 (ptr = device_get_nameunit(iface->subdev)) && 783 (desc = device_get_desc(iface->subdev))) { 784 785 /* print description */ 786 snprintf(buf, sizeof(buf), "%s: <%s>", ptr, desc); 787 788 /* range checks */ 789 maxlen = ugd->ugd_maxlen - 1; 790 len = strlen(buf); 791 if (len > maxlen) 792 len = maxlen; 793 794 /* update actual length, including terminating zero */ 795 ugd->ugd_actlen = len + 1; 796 797 /* copy out interface description */ 798 error = copyout(buf, ugd->ugd_data, ugd->ugd_actlen); 799 } else { 800 /* zero length string is default */ 801 error = copyout("", ugd->ugd_data, 1); 802 } 803 return (error); 804 } 805 806 /*------------------------------------------------------------------------* 807 * usb_gen_fill_deviceinfo 808 * 809 * This function dumps information about an USB device to the 810 * structure pointed to by the "di" argument. 811 * 812 * Returns: 813 * 0: Success 814 * Else: Failure 815 *------------------------------------------------------------------------*/ 816 static int 817 usb_gen_fill_deviceinfo(struct usb_fifo *f, struct usb_device_info *di) 818 { 819 struct usb_device *udev; 820 struct usb_device *hub; 821 822 udev = f->udev; 823 824 bzero(di, sizeof(di[0])); 825 826 di->udi_bus = device_get_unit(udev->bus->bdev); 827 di->udi_addr = udev->address; 828 di->udi_index = udev->device_index; 829 strlcpy(di->udi_serial, usb_get_serial(udev), sizeof(di->udi_serial)); 830 strlcpy(di->udi_vendor, usb_get_manufacturer(udev), sizeof(di->udi_vendor)); 831 strlcpy(di->udi_product, usb_get_product(udev), sizeof(di->udi_product)); 832 usb_printbcd(di->udi_release, sizeof(di->udi_release), 833 UGETW(udev->ddesc.bcdDevice)); 834 di->udi_vendorNo = UGETW(udev->ddesc.idVendor); 835 di->udi_productNo = UGETW(udev->ddesc.idProduct); 836 di->udi_releaseNo = UGETW(udev->ddesc.bcdDevice); 837 di->udi_class = udev->ddesc.bDeviceClass; 838 di->udi_subclass = udev->ddesc.bDeviceSubClass; 839 di->udi_protocol = udev->ddesc.bDeviceProtocol; 840 di->udi_config_no = udev->curr_config_no; 841 di->udi_config_index = udev->curr_config_index; 842 di->udi_power = udev->flags.self_powered ? 0 : udev->power; 843 di->udi_speed = udev->speed; 844 di->udi_mode = udev->flags.usb_mode; 845 di->udi_power_mode = udev->power_mode; 846 di->udi_suspended = udev->flags.peer_suspended; 847 848 hub = udev->parent_hub; 849 if (hub) { 850 di->udi_hubaddr = hub->address; 851 di->udi_hubindex = hub->device_index; 852 di->udi_hubport = udev->port_no; 853 } 854 return (0); 855 } 856 857 /*------------------------------------------------------------------------* 858 * ugen_check_request 859 * 860 * Return values: 861 * 0: Access allowed 862 * Else: No access 863 *------------------------------------------------------------------------*/ 864 static int 865 ugen_check_request(struct usb_device *udev, struct usb_device_request *req) 866 { 867 struct usb_endpoint *ep; 868 int error; 869 870 /* 871 * Avoid requests that would damage the bus integrity: 872 */ 873 if (((req->bmRequestType == UT_WRITE_DEVICE) && 874 (req->bRequest == UR_SET_ADDRESS)) || 875 ((req->bmRequestType == UT_WRITE_DEVICE) && 876 (req->bRequest == UR_SET_CONFIG)) || 877 ((req->bmRequestType == UT_WRITE_INTERFACE) && 878 (req->bRequest == UR_SET_INTERFACE))) { 879 /* 880 * These requests can be useful for testing USB drivers. 881 */ 882 error = priv_check(curthread, PRIV_DRIVER); 883 if (error) { 884 return (error); 885 } 886 } 887 /* 888 * Special case - handle clearing of stall 889 */ 890 if (req->bmRequestType == UT_WRITE_ENDPOINT) { 891 892 ep = usbd_get_ep_by_addr(udev, req->wIndex[0]); 893 if (ep == NULL) { 894 return (EINVAL); 895 } 896 if ((req->bRequest == UR_CLEAR_FEATURE) && 897 (UGETW(req->wValue) == UF_ENDPOINT_HALT)) { 898 usbd_clear_data_toggle(udev, ep); 899 } 900 } 901 /* TODO: add more checks to verify the interface index */ 902 903 return (0); 904 } 905 906 int 907 ugen_do_request(struct usb_fifo *f, struct usb_ctl_request *ur) 908 { 909 int error; 910 uint16_t len; 911 uint16_t actlen; 912 913 if (ugen_check_request(f->udev, &ur->ucr_request)) { 914 return (EPERM); 915 } 916 len = UGETW(ur->ucr_request.wLength); 917 918 /* check if "ucr_data" is valid */ 919 if (len != 0) { 920 if (ur->ucr_data == NULL) { 921 return (EFAULT); 922 } 923 } 924 /* do the USB request */ 925 error = usbd_do_request_flags 926 (f->udev, NULL, &ur->ucr_request, ur->ucr_data, 927 (ur->ucr_flags & USB_SHORT_XFER_OK) | 928 USB_USER_DATA_PTR, &actlen, 929 USB_DEFAULT_TIMEOUT); 930 931 ur->ucr_actlen = actlen; 932 933 if (error) { 934 error = EIO; 935 } 936 return (error); 937 } 938 939 /*------------------------------------------------------------------------ 940 * ugen_re_enumerate 941 *------------------------------------------------------------------------*/ 942 static int 943 ugen_re_enumerate(struct usb_fifo *f) 944 { 945 struct usb_device *udev = f->udev; 946 int error; 947 948 /* 949 * This request can be useful for testing USB drivers: 950 */ 951 error = priv_check(curthread, PRIV_DRIVER); 952 if (error) { 953 return (error); 954 } 955 if (udev->flags.usb_mode != USB_MODE_HOST) { 956 /* not possible in device side mode */ 957 DPRINTFN(6, "device mode\n"); 958 return (ENOTTY); 959 } 960 if (udev->parent_hub == NULL) { 961 /* the root HUB cannot be re-enumerated */ 962 DPRINTFN(6, "cannot reset root HUB\n"); 963 return (EINVAL); 964 } 965 /* make sure all FIFO's are gone */ 966 /* else there can be a deadlock */ 967 if (ugen_fs_uninit(f)) { 968 /* ignore any errors */ 969 DPRINTFN(6, "no FIFOs\n"); 970 } 971 /* start re-enumeration of device */ 972 usbd_start_re_enumerate(udev); 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_open_stream *popen_stream; 1397 struct usb_fs_close *pclose; 1398 struct usb_fs_clear_stall_sync *pstall; 1399 void *addr; 1400 } u; 1401 struct usb_endpoint *ep; 1402 struct usb_endpoint_descriptor *ed; 1403 struct usb_xfer *xfer; 1404 int error = 0; 1405 uint8_t iface_index; 1406 uint8_t isread; 1407 uint8_t ep_index; 1408 uint8_t pre_scale; 1409 1410 u.addr = addr; 1411 1412 DPRINTFN(6, "cmd=0x%08lx\n", cmd); 1413 1414 switch (cmd) { 1415 case USB_FS_COMPLETE: 1416 mtx_lock(f->priv_mtx); 1417 error = ugen_fs_get_complete(f, &ep_index); 1418 mtx_unlock(f->priv_mtx); 1419 1420 if (error) { 1421 error = EBUSY; 1422 break; 1423 } 1424 u.pcomp->ep_index = ep_index; 1425 error = ugen_fs_copy_out(f, u.pcomp->ep_index); 1426 break; 1427 1428 case USB_FS_START: 1429 error = ugen_fs_copy_in(f, u.pstart->ep_index); 1430 if (error) 1431 break; 1432 mtx_lock(f->priv_mtx); 1433 xfer = f->fs_xfer[u.pstart->ep_index]; 1434 usbd_transfer_start(xfer); 1435 mtx_unlock(f->priv_mtx); 1436 break; 1437 1438 case USB_FS_STOP: 1439 if (u.pstop->ep_index >= f->fs_ep_max) { 1440 error = EINVAL; 1441 break; 1442 } 1443 mtx_lock(f->priv_mtx); 1444 xfer = f->fs_xfer[u.pstart->ep_index]; 1445 if (usbd_transfer_pending(xfer)) { 1446 usbd_transfer_stop(xfer); 1447 /* 1448 * Check if the USB transfer was stopped 1449 * before it was even started. Else a cancel 1450 * callback will be pending. 1451 */ 1452 if (!xfer->flags_int.transferring) { 1453 ugen_fs_set_complete(xfer->priv_sc, 1454 USB_P2U(xfer->priv_fifo)); 1455 } 1456 } 1457 mtx_unlock(f->priv_mtx); 1458 break; 1459 1460 case USB_FS_OPEN: 1461 case USB_FS_OPEN_STREAM: 1462 if (u.popen->ep_index >= f->fs_ep_max) { 1463 error = EINVAL; 1464 break; 1465 } 1466 if (f->fs_xfer[u.popen->ep_index] != NULL) { 1467 error = EBUSY; 1468 break; 1469 } 1470 if (u.popen->max_bufsize > USB_FS_MAX_BUFSIZE) { 1471 u.popen->max_bufsize = USB_FS_MAX_BUFSIZE; 1472 } 1473 if (u.popen->max_frames & USB_FS_MAX_FRAMES_PRE_SCALE) { 1474 pre_scale = 1; 1475 u.popen->max_frames &= ~USB_FS_MAX_FRAMES_PRE_SCALE; 1476 } else { 1477 pre_scale = 0; 1478 } 1479 if (u.popen->max_frames > USB_FS_MAX_FRAMES) { 1480 u.popen->max_frames = USB_FS_MAX_FRAMES; 1481 break; 1482 } 1483 if (u.popen->max_frames == 0) { 1484 error = EINVAL; 1485 break; 1486 } 1487 ep = usbd_get_ep_by_addr(f->udev, u.popen->ep_no); 1488 if (ep == NULL) { 1489 error = EINVAL; 1490 break; 1491 } 1492 ed = ep->edesc; 1493 if (ed == NULL) { 1494 error = ENXIO; 1495 break; 1496 } 1497 iface_index = ep->iface_index; 1498 1499 memset(usb_config, 0, sizeof(usb_config)); 1500 1501 usb_config[0].type = ed->bmAttributes & UE_XFERTYPE; 1502 usb_config[0].endpoint = ed->bEndpointAddress & UE_ADDR; 1503 usb_config[0].direction = ed->bEndpointAddress & (UE_DIR_OUT | UE_DIR_IN); 1504 usb_config[0].interval = USB_DEFAULT_INTERVAL; 1505 usb_config[0].flags.proxy_buffer = 1; 1506 if (pre_scale != 0) 1507 usb_config[0].flags.pre_scale_frames = 1; 1508 usb_config[0].callback = &ugen_ctrl_fs_callback; 1509 usb_config[0].timeout = 0; /* no timeout */ 1510 usb_config[0].frames = u.popen->max_frames; 1511 usb_config[0].bufsize = u.popen->max_bufsize; 1512 usb_config[0].usb_mode = USB_MODE_DUAL; /* both modes */ 1513 if (cmd == USB_FS_OPEN_STREAM) 1514 usb_config[0].stream_id = u.popen_stream->stream_id; 1515 1516 if (usb_config[0].type == UE_CONTROL) { 1517 if (f->udev->flags.usb_mode != USB_MODE_HOST) { 1518 error = EINVAL; 1519 break; 1520 } 1521 } else { 1522 1523 isread = ((usb_config[0].endpoint & 1524 (UE_DIR_IN | UE_DIR_OUT)) == UE_DIR_IN); 1525 1526 if (f->udev->flags.usb_mode != USB_MODE_HOST) { 1527 isread = !isread; 1528 } 1529 /* check permissions */ 1530 if (isread) { 1531 if (!(fflags & FREAD)) { 1532 error = EPERM; 1533 break; 1534 } 1535 } else { 1536 if (!(fflags & FWRITE)) { 1537 error = EPERM; 1538 break; 1539 } 1540 } 1541 } 1542 error = usbd_transfer_setup(f->udev, &iface_index, 1543 f->fs_xfer + u.popen->ep_index, usb_config, 1, 1544 f, f->priv_mtx); 1545 if (error == 0) { 1546 /* update maximums */ 1547 u.popen->max_packet_length = 1548 f->fs_xfer[u.popen->ep_index]->max_frame_size; 1549 u.popen->max_bufsize = 1550 f->fs_xfer[u.popen->ep_index]->max_data_length; 1551 /* update number of frames */ 1552 u.popen->max_frames = 1553 f->fs_xfer[u.popen->ep_index]->nframes; 1554 /* store index of endpoint */ 1555 f->fs_xfer[u.popen->ep_index]->priv_fifo = 1556 ((uint8_t *)0) + u.popen->ep_index; 1557 } else { 1558 error = ENOMEM; 1559 } 1560 break; 1561 1562 case USB_FS_CLOSE: 1563 if (u.pclose->ep_index >= f->fs_ep_max) { 1564 error = EINVAL; 1565 break; 1566 } 1567 if (f->fs_xfer[u.pclose->ep_index] == NULL) { 1568 error = EINVAL; 1569 break; 1570 } 1571 usbd_transfer_unsetup(f->fs_xfer + u.pclose->ep_index, 1); 1572 break; 1573 1574 case USB_FS_CLEAR_STALL_SYNC: 1575 if (u.pstall->ep_index >= f->fs_ep_max) { 1576 error = EINVAL; 1577 break; 1578 } 1579 if (f->fs_xfer[u.pstall->ep_index] == NULL) { 1580 error = EINVAL; 1581 break; 1582 } 1583 if (f->udev->flags.usb_mode != USB_MODE_HOST) { 1584 error = EINVAL; 1585 break; 1586 } 1587 mtx_lock(f->priv_mtx); 1588 error = usbd_transfer_pending(f->fs_xfer[u.pstall->ep_index]); 1589 mtx_unlock(f->priv_mtx); 1590 1591 if (error) { 1592 return (EBUSY); 1593 } 1594 ep = f->fs_xfer[u.pstall->ep_index]->endpoint; 1595 1596 /* setup a clear-stall packet */ 1597 req.bmRequestType = UT_WRITE_ENDPOINT; 1598 req.bRequest = UR_CLEAR_FEATURE; 1599 USETW(req.wValue, UF_ENDPOINT_HALT); 1600 req.wIndex[0] = ep->edesc->bEndpointAddress; 1601 req.wIndex[1] = 0; 1602 USETW(req.wLength, 0); 1603 1604 error = usbd_do_request(f->udev, NULL, &req, NULL); 1605 if (error == 0) { 1606 usbd_clear_data_toggle(f->udev, ep); 1607 } else { 1608 error = ENXIO; 1609 } 1610 break; 1611 1612 default: 1613 error = ENOIOCTL; 1614 break; 1615 } 1616 1617 DPRINTFN(6, "error=%d\n", error); 1618 1619 return (error); 1620 } 1621 1622 static int 1623 ugen_set_short_xfer(struct usb_fifo *f, void *addr) 1624 { 1625 uint8_t t; 1626 1627 if (*(int *)addr) 1628 t = 1; 1629 else 1630 t = 0; 1631 1632 if (f->flag_short == t) { 1633 /* same value like before - accept */ 1634 return (0); 1635 } 1636 if (f->xfer[0] || f->xfer[1]) { 1637 /* cannot change this during transfer */ 1638 return (EBUSY); 1639 } 1640 f->flag_short = t; 1641 return (0); 1642 } 1643 1644 static int 1645 ugen_set_timeout(struct usb_fifo *f, void *addr) 1646 { 1647 f->timeout = *(int *)addr; 1648 if (f->timeout > 65535) { 1649 /* limit user input */ 1650 f->timeout = 65535; 1651 } 1652 return (0); 1653 } 1654 1655 static int 1656 ugen_get_frame_size(struct usb_fifo *f, void *addr) 1657 { 1658 if (f->xfer[0]) { 1659 *(int *)addr = f->xfer[0]->max_frame_size; 1660 } else { 1661 return (EINVAL); 1662 } 1663 return (0); 1664 } 1665 1666 static int 1667 ugen_set_buffer_size(struct usb_fifo *f, void *addr) 1668 { 1669 usb_frlength_t t; 1670 1671 if (*(int *)addr < 0) 1672 t = 0; /* use "wMaxPacketSize" */ 1673 else if (*(int *)addr < (256 * 1024)) 1674 t = *(int *)addr; 1675 else 1676 t = 256 * 1024; 1677 1678 if (f->bufsize == t) { 1679 /* same value like before - accept */ 1680 return (0); 1681 } 1682 if (f->xfer[0] || f->xfer[1]) { 1683 /* cannot change this during transfer */ 1684 return (EBUSY); 1685 } 1686 f->bufsize = t; 1687 return (0); 1688 } 1689 1690 static int 1691 ugen_get_buffer_size(struct usb_fifo *f, void *addr) 1692 { 1693 *(int *)addr = f->bufsize; 1694 return (0); 1695 } 1696 1697 static int 1698 ugen_get_iface_desc(struct usb_fifo *f, 1699 struct usb_interface_descriptor *idesc) 1700 { 1701 struct usb_interface *iface; 1702 1703 iface = usbd_get_iface(f->udev, f->iface_index); 1704 if (iface && iface->idesc) { 1705 *idesc = *(iface->idesc); 1706 } else { 1707 return (EIO); 1708 } 1709 return (0); 1710 } 1711 1712 static int 1713 ugen_get_endpoint_desc(struct usb_fifo *f, 1714 struct usb_endpoint_descriptor *ed) 1715 { 1716 struct usb_endpoint *ep; 1717 1718 ep = usb_fifo_softc(f); 1719 1720 if (ep && ep->edesc) { 1721 *ed = *ep->edesc; 1722 } else { 1723 return (EINVAL); 1724 } 1725 return (0); 1726 } 1727 1728 static int 1729 ugen_set_power_mode(struct usb_fifo *f, int mode) 1730 { 1731 struct usb_device *udev = f->udev; 1732 int err; 1733 uint8_t old_mode; 1734 1735 if ((udev == NULL) || 1736 (udev->parent_hub == NULL)) { 1737 return (EINVAL); 1738 } 1739 err = priv_check(curthread, PRIV_DRIVER); 1740 if (err) 1741 return (err); 1742 1743 /* get old power mode */ 1744 old_mode = udev->power_mode; 1745 1746 /* if no change, then just return */ 1747 if (old_mode == mode) 1748 return (0); 1749 1750 switch (mode) { 1751 case USB_POWER_MODE_OFF: 1752 /* get the device unconfigured */ 1753 err = ugen_set_config(f, USB_UNCONFIG_INDEX); 1754 if (err) { 1755 DPRINTFN(0, "Could not unconfigure " 1756 "device (ignored)\n"); 1757 } 1758 1759 /* clear port enable */ 1760 err = usbd_req_clear_port_feature(udev->parent_hub, 1761 NULL, udev->port_no, UHF_PORT_ENABLE); 1762 break; 1763 1764 case USB_POWER_MODE_ON: 1765 case USB_POWER_MODE_SAVE: 1766 break; 1767 1768 case USB_POWER_MODE_RESUME: 1769 #if USB_HAVE_POWERD 1770 /* let USB-powerd handle resume */ 1771 USB_BUS_LOCK(udev->bus); 1772 udev->pwr_save.write_refs++; 1773 udev->pwr_save.last_xfer_time = ticks; 1774 USB_BUS_UNLOCK(udev->bus); 1775 1776 /* set new power mode */ 1777 usbd_set_power_mode(udev, USB_POWER_MODE_SAVE); 1778 1779 /* wait for resume to complete */ 1780 usb_pause_mtx(NULL, hz / 4); 1781 1782 /* clear write reference */ 1783 USB_BUS_LOCK(udev->bus); 1784 udev->pwr_save.write_refs--; 1785 USB_BUS_UNLOCK(udev->bus); 1786 #endif 1787 mode = USB_POWER_MODE_SAVE; 1788 break; 1789 1790 case USB_POWER_MODE_SUSPEND: 1791 #if USB_HAVE_POWERD 1792 /* let USB-powerd handle suspend */ 1793 USB_BUS_LOCK(udev->bus); 1794 udev->pwr_save.last_xfer_time = ticks - (256 * hz); 1795 USB_BUS_UNLOCK(udev->bus); 1796 #endif 1797 mode = USB_POWER_MODE_SAVE; 1798 break; 1799 1800 default: 1801 return (EINVAL); 1802 } 1803 1804 if (err) 1805 return (ENXIO); /* I/O failure */ 1806 1807 /* if we are powered off we need to re-enumerate first */ 1808 if (old_mode == USB_POWER_MODE_OFF) { 1809 if (udev->flags.usb_mode == USB_MODE_HOST) { 1810 if (udev->re_enumerate_wait == 0) 1811 udev->re_enumerate_wait = 1; 1812 } 1813 /* set power mode will wake up the explore thread */ 1814 } 1815 1816 /* set new power mode */ 1817 usbd_set_power_mode(udev, mode); 1818 1819 return (0); /* success */ 1820 } 1821 1822 static int 1823 ugen_get_power_mode(struct usb_fifo *f) 1824 { 1825 struct usb_device *udev = f->udev; 1826 1827 if (udev == NULL) 1828 return (USB_POWER_MODE_ON); 1829 1830 return (udev->power_mode); 1831 } 1832 1833 static int 1834 ugen_do_port_feature(struct usb_fifo *f, uint8_t port_no, 1835 uint8_t set, uint16_t feature) 1836 { 1837 struct usb_device *udev = f->udev; 1838 struct usb_hub *hub; 1839 int err; 1840 1841 err = priv_check(curthread, PRIV_DRIVER); 1842 if (err) { 1843 return (err); 1844 } 1845 if (port_no == 0) { 1846 return (EINVAL); 1847 } 1848 if ((udev == NULL) || 1849 (udev->hub == NULL)) { 1850 return (EINVAL); 1851 } 1852 hub = udev->hub; 1853 1854 if (port_no > hub->nports) { 1855 return (EINVAL); 1856 } 1857 if (set) 1858 err = usbd_req_set_port_feature(udev, 1859 NULL, port_no, feature); 1860 else 1861 err = usbd_req_clear_port_feature(udev, 1862 NULL, port_no, feature); 1863 1864 if (err) 1865 return (ENXIO); /* failure */ 1866 1867 return (0); /* success */ 1868 } 1869 1870 static int 1871 ugen_iface_ioctl(struct usb_fifo *f, u_long cmd, void *addr, int fflags) 1872 { 1873 struct usb_fifo *f_rx; 1874 struct usb_fifo *f_tx; 1875 int error = 0; 1876 1877 f_rx = f->udev->fifo[(f->fifo_index & ~1) + USB_FIFO_RX]; 1878 f_tx = f->udev->fifo[(f->fifo_index & ~1) + USB_FIFO_TX]; 1879 1880 switch (cmd) { 1881 case USB_SET_RX_SHORT_XFER: 1882 if (fflags & FREAD) { 1883 error = ugen_set_short_xfer(f_rx, addr); 1884 } else { 1885 error = EINVAL; 1886 } 1887 break; 1888 1889 case USB_SET_TX_FORCE_SHORT: 1890 if (fflags & FWRITE) { 1891 error = ugen_set_short_xfer(f_tx, addr); 1892 } else { 1893 error = EINVAL; 1894 } 1895 break; 1896 1897 case USB_SET_RX_TIMEOUT: 1898 if (fflags & FREAD) { 1899 error = ugen_set_timeout(f_rx, addr); 1900 } else { 1901 error = EINVAL; 1902 } 1903 break; 1904 1905 case USB_SET_TX_TIMEOUT: 1906 if (fflags & FWRITE) { 1907 error = ugen_set_timeout(f_tx, addr); 1908 } else { 1909 error = EINVAL; 1910 } 1911 break; 1912 1913 case USB_GET_RX_FRAME_SIZE: 1914 if (fflags & FREAD) { 1915 error = ugen_get_frame_size(f_rx, addr); 1916 } else { 1917 error = EINVAL; 1918 } 1919 break; 1920 1921 case USB_GET_TX_FRAME_SIZE: 1922 if (fflags & FWRITE) { 1923 error = ugen_get_frame_size(f_tx, addr); 1924 } else { 1925 error = EINVAL; 1926 } 1927 break; 1928 1929 case USB_SET_RX_BUFFER_SIZE: 1930 if (fflags & FREAD) { 1931 error = ugen_set_buffer_size(f_rx, addr); 1932 } else { 1933 error = EINVAL; 1934 } 1935 break; 1936 1937 case USB_SET_TX_BUFFER_SIZE: 1938 if (fflags & FWRITE) { 1939 error = ugen_set_buffer_size(f_tx, addr); 1940 } else { 1941 error = EINVAL; 1942 } 1943 break; 1944 1945 case USB_GET_RX_BUFFER_SIZE: 1946 if (fflags & FREAD) { 1947 error = ugen_get_buffer_size(f_rx, addr); 1948 } else { 1949 error = EINVAL; 1950 } 1951 break; 1952 1953 case USB_GET_TX_BUFFER_SIZE: 1954 if (fflags & FWRITE) { 1955 error = ugen_get_buffer_size(f_tx, addr); 1956 } else { 1957 error = EINVAL; 1958 } 1959 break; 1960 1961 case USB_GET_RX_INTERFACE_DESC: 1962 if (fflags & FREAD) { 1963 error = ugen_get_iface_desc(f_rx, addr); 1964 } else { 1965 error = EINVAL; 1966 } 1967 break; 1968 1969 case USB_GET_TX_INTERFACE_DESC: 1970 if (fflags & FWRITE) { 1971 error = ugen_get_iface_desc(f_tx, addr); 1972 } else { 1973 error = EINVAL; 1974 } 1975 break; 1976 1977 case USB_GET_RX_ENDPOINT_DESC: 1978 if (fflags & FREAD) { 1979 error = ugen_get_endpoint_desc(f_rx, addr); 1980 } else { 1981 error = EINVAL; 1982 } 1983 break; 1984 1985 case USB_GET_TX_ENDPOINT_DESC: 1986 if (fflags & FWRITE) { 1987 error = ugen_get_endpoint_desc(f_tx, addr); 1988 } else { 1989 error = EINVAL; 1990 } 1991 break; 1992 1993 case USB_SET_RX_STALL_FLAG: 1994 if ((fflags & FREAD) && (*(int *)addr)) { 1995 f_rx->flag_stall = 1; 1996 } 1997 break; 1998 1999 case USB_SET_TX_STALL_FLAG: 2000 if ((fflags & FWRITE) && (*(int *)addr)) { 2001 f_tx->flag_stall = 1; 2002 } 2003 break; 2004 2005 default: 2006 error = ENOIOCTL; 2007 break; 2008 } 2009 return (error); 2010 } 2011 2012 static int 2013 ugen_ioctl_post(struct usb_fifo *f, u_long cmd, void *addr, int fflags) 2014 { 2015 union { 2016 struct usb_interface_descriptor *idesc; 2017 struct usb_alt_interface *ai; 2018 struct usb_device_descriptor *ddesc; 2019 struct usb_config_descriptor *cdesc; 2020 struct usb_device_stats *stat; 2021 struct usb_fs_init *pinit; 2022 struct usb_fs_uninit *puninit; 2023 uint32_t *ptime; 2024 void *addr; 2025 int *pint; 2026 } u; 2027 struct usb_device_descriptor *dtemp; 2028 struct usb_config_descriptor *ctemp; 2029 struct usb_interface *iface; 2030 int error = 0; 2031 uint8_t n; 2032 2033 u.addr = addr; 2034 2035 DPRINTFN(6, "cmd=0x%08lx\n", cmd); 2036 2037 switch (cmd) { 2038 case USB_DISCOVER: 2039 usb_needs_explore_all(); 2040 break; 2041 2042 case USB_SETDEBUG: 2043 if (!(fflags & FWRITE)) { 2044 error = EPERM; 2045 break; 2046 } 2047 usb_debug = *(int *)addr; 2048 break; 2049 2050 case USB_GET_CONFIG: 2051 *(int *)addr = f->udev->curr_config_index; 2052 break; 2053 2054 case USB_SET_CONFIG: 2055 if (!(fflags & FWRITE)) { 2056 error = EPERM; 2057 break; 2058 } 2059 error = ugen_set_config(f, *(int *)addr); 2060 break; 2061 2062 case USB_GET_ALTINTERFACE: 2063 iface = usbd_get_iface(f->udev, 2064 u.ai->uai_interface_index); 2065 if (iface && iface->idesc) { 2066 u.ai->uai_alt_index = iface->alt_index; 2067 } else { 2068 error = EINVAL; 2069 } 2070 break; 2071 2072 case USB_SET_ALTINTERFACE: 2073 if (!(fflags & FWRITE)) { 2074 error = EPERM; 2075 break; 2076 } 2077 error = ugen_set_interface(f, 2078 u.ai->uai_interface_index, u.ai->uai_alt_index); 2079 break; 2080 2081 case USB_GET_DEVICE_DESC: 2082 dtemp = usbd_get_device_descriptor(f->udev); 2083 if (!dtemp) { 2084 error = EIO; 2085 break; 2086 } 2087 *u.ddesc = *dtemp; 2088 break; 2089 2090 case USB_GET_CONFIG_DESC: 2091 ctemp = usbd_get_config_descriptor(f->udev); 2092 if (!ctemp) { 2093 error = EIO; 2094 break; 2095 } 2096 *u.cdesc = *ctemp; 2097 break; 2098 2099 case USB_GET_FULL_DESC: 2100 error = ugen_get_cdesc(f, addr); 2101 break; 2102 2103 case USB_GET_STRING_DESC: 2104 error = ugen_get_sdesc(f, addr); 2105 break; 2106 2107 case USB_GET_IFACE_DRIVER: 2108 error = ugen_get_iface_driver(f, addr); 2109 break; 2110 2111 case USB_REQUEST: 2112 case USB_DO_REQUEST: 2113 if (!(fflags & FWRITE)) { 2114 error = EPERM; 2115 break; 2116 } 2117 error = ugen_do_request(f, addr); 2118 break; 2119 2120 case USB_DEVICEINFO: 2121 case USB_GET_DEVICEINFO: 2122 error = usb_gen_fill_deviceinfo(f, addr); 2123 break; 2124 2125 case USB_DEVICESTATS: 2126 for (n = 0; n != 4; n++) { 2127 2128 u.stat->uds_requests_fail[n] = 2129 f->udev->bus->stats_err.uds_requests[n]; 2130 2131 u.stat->uds_requests_ok[n] = 2132 f->udev->bus->stats_ok.uds_requests[n]; 2133 } 2134 break; 2135 2136 case USB_DEVICEENUMERATE: 2137 error = ugen_re_enumerate(f); 2138 break; 2139 2140 case USB_GET_PLUGTIME: 2141 *u.ptime = f->udev->plugtime; 2142 break; 2143 2144 case USB_CLAIM_INTERFACE: 2145 case USB_RELEASE_INTERFACE: 2146 /* TODO */ 2147 break; 2148 2149 case USB_IFACE_DRIVER_ACTIVE: 2150 2151 n = *u.pint & 0xFF; 2152 2153 iface = usbd_get_iface(f->udev, n); 2154 2155 if (iface && iface->subdev) 2156 error = 0; 2157 else 2158 error = ENXIO; 2159 break; 2160 2161 case USB_IFACE_DRIVER_DETACH: 2162 2163 error = priv_check(curthread, PRIV_DRIVER); 2164 2165 if (error) 2166 break; 2167 2168 n = *u.pint & 0xFF; 2169 2170 if (n == USB_IFACE_INDEX_ANY) { 2171 error = EINVAL; 2172 break; 2173 } 2174 2175 /* 2176 * Detach the currently attached driver. 2177 */ 2178 usb_detach_device(f->udev, n, 0); 2179 2180 /* 2181 * Set parent to self, this should keep attach away 2182 * until the next set configuration event. 2183 */ 2184 usbd_set_parent_iface(f->udev, n, n); 2185 break; 2186 2187 case USB_SET_POWER_MODE: 2188 error = ugen_set_power_mode(f, *u.pint); 2189 break; 2190 2191 case USB_GET_POWER_MODE: 2192 *u.pint = ugen_get_power_mode(f); 2193 break; 2194 2195 case USB_SET_PORT_ENABLE: 2196 error = ugen_do_port_feature(f, 2197 *u.pint, 1, UHF_PORT_ENABLE); 2198 break; 2199 2200 case USB_SET_PORT_DISABLE: 2201 error = ugen_do_port_feature(f, 2202 *u.pint, 0, UHF_PORT_ENABLE); 2203 break; 2204 2205 case USB_FS_INIT: 2206 /* verify input parameters */ 2207 if (u.pinit->pEndpoints == NULL) { 2208 error = EINVAL; 2209 break; 2210 } 2211 if (u.pinit->ep_index_max > 127) { 2212 error = EINVAL; 2213 break; 2214 } 2215 if (u.pinit->ep_index_max == 0) { 2216 error = EINVAL; 2217 break; 2218 } 2219 if (f->fs_xfer != NULL) { 2220 error = EBUSY; 2221 break; 2222 } 2223 if (f->dev_ep_index != 0) { 2224 error = EINVAL; 2225 break; 2226 } 2227 if (ugen_fifo_in_use(f, fflags)) { 2228 error = EBUSY; 2229 break; 2230 } 2231 error = usb_fifo_alloc_buffer(f, 1, u.pinit->ep_index_max); 2232 if (error) { 2233 break; 2234 } 2235 f->fs_xfer = malloc(sizeof(f->fs_xfer[0]) * 2236 u.pinit->ep_index_max, M_USB, M_WAITOK | M_ZERO); 2237 if (f->fs_xfer == NULL) { 2238 usb_fifo_free_buffer(f); 2239 error = ENOMEM; 2240 break; 2241 } 2242 f->fs_ep_max = u.pinit->ep_index_max; 2243 f->fs_ep_ptr = u.pinit->pEndpoints; 2244 break; 2245 2246 case USB_FS_UNINIT: 2247 if (u.puninit->dummy != 0) { 2248 error = EINVAL; 2249 break; 2250 } 2251 error = ugen_fs_uninit(f); 2252 break; 2253 2254 default: 2255 mtx_lock(f->priv_mtx); 2256 error = ugen_iface_ioctl(f, cmd, addr, fflags); 2257 mtx_unlock(f->priv_mtx); 2258 break; 2259 } 2260 DPRINTFN(6, "error=%d\n", error); 2261 return (error); 2262 } 2263 2264 static void 2265 ugen_ctrl_fs_callback(struct usb_xfer *xfer, usb_error_t error) 2266 { 2267 ; /* workaround for a bug in "indent" */ 2268 2269 DPRINTF("st=%u alen=%u aframes=%u\n", 2270 USB_GET_STATE(xfer), xfer->actlen, xfer->aframes); 2271 2272 switch (USB_GET_STATE(xfer)) { 2273 case USB_ST_SETUP: 2274 usbd_transfer_submit(xfer); 2275 break; 2276 default: 2277 ugen_fs_set_complete(xfer->priv_sc, USB_P2U(xfer->priv_fifo)); 2278 break; 2279 } 2280 } 2281 #endif /* USB_HAVE_UGEN */ 2282