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