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