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_cancelled(struct usb_fs_endpoint *fs_ep_uptr) 1223 { 1224 struct usb_fs_endpoint fs_ep; 1225 int error; 1226 1227 error = copyin(fs_ep_uptr, &fs_ep, sizeof(fs_ep)); 1228 if (error) 1229 return (error); 1230 1231 fs_ep.status = USB_ERR_CANCELLED; 1232 fs_ep.aFrames = 0; 1233 fs_ep.isoc_time_complete = 0; 1234 1235 /* update "aFrames" */ 1236 error = copyout(&fs_ep.aFrames, &fs_ep_uptr->aFrames, 1237 sizeof(fs_ep.aFrames)); 1238 if (error) 1239 goto done; 1240 1241 /* update "isoc_time_complete" */ 1242 error = copyout(&fs_ep.isoc_time_complete, 1243 &fs_ep_uptr->isoc_time_complete, 1244 sizeof(fs_ep.isoc_time_complete)); 1245 if (error) 1246 goto done; 1247 1248 /* update "status" */ 1249 error = copyout(&fs_ep.status, &fs_ep_uptr->status, 1250 sizeof(fs_ep.status)); 1251 done: 1252 return (error); 1253 } 1254 1255 static int 1256 ugen_fs_copy_out(struct usb_fifo *f, uint8_t ep_index) 1257 { 1258 struct usb_device_request *req; 1259 struct usb_xfer *xfer; 1260 struct usb_fs_endpoint fs_ep; 1261 struct usb_fs_endpoint *fs_ep_uptr; /* userland ptr */ 1262 void *uaddr; /* userland ptr */ 1263 void *kaddr; 1264 usb_frlength_t offset; 1265 usb_frlength_t rem; 1266 usb_frcount_t n; 1267 uint32_t length; 1268 uint32_t temp; 1269 int error; 1270 uint8_t isread; 1271 1272 if (ep_index >= f->fs_ep_max) 1273 return (EINVAL); 1274 1275 xfer = f->fs_xfer[ep_index]; 1276 if (xfer == NULL) 1277 return (EINVAL); 1278 1279 mtx_lock(f->priv_mtx); 1280 if (!xfer->flags_int.transferring && 1281 !xfer->flags_int.started) { 1282 mtx_unlock(f->priv_mtx); 1283 DPRINTF("Returning fake cancel event\n"); 1284 return (ugen_fs_copy_out_cancelled(f->fs_ep_ptr + ep_index)); 1285 } else if (usbd_transfer_pending(xfer)) { 1286 mtx_unlock(f->priv_mtx); 1287 return (EBUSY); /* should not happen */ 1288 } 1289 mtx_unlock(f->priv_mtx); 1290 1291 fs_ep_uptr = f->fs_ep_ptr + ep_index; 1292 error = copyin(fs_ep_uptr, &fs_ep, sizeof(fs_ep)); 1293 if (error) { 1294 return (error); 1295 } 1296 fs_ep.status = xfer->error; 1297 fs_ep.aFrames = xfer->aframes; 1298 fs_ep.isoc_time_complete = xfer->isoc_time_complete; 1299 if (xfer->error) { 1300 goto complete; 1301 } 1302 if (xfer->flags_int.control_xfr) { 1303 req = xfer->frbuffers[0].buffer; 1304 1305 /* Host mode only ! */ 1306 if ((req->bmRequestType & (UT_READ | UT_WRITE)) == UT_READ) { 1307 isread = 1; 1308 } else { 1309 isread = 0; 1310 } 1311 if (xfer->nframes == 0) 1312 n = 0; /* should never happen */ 1313 else 1314 n = 1; 1315 } else { 1316 /* Device and Host mode */ 1317 if (USB_GET_DATA_ISREAD(xfer)) { 1318 isread = 1; 1319 } else { 1320 isread = 0; 1321 } 1322 n = 0; 1323 } 1324 1325 /* Update lengths and copy out data */ 1326 1327 rem = usbd_xfer_max_len(xfer); 1328 offset = 0; 1329 1330 for (; n != xfer->nframes; n++) { 1331 1332 /* get initial length into "temp" */ 1333 error = copyin(fs_ep.pLength + n, 1334 &temp, sizeof(temp)); 1335 if (error) { 1336 return (error); 1337 } 1338 if (temp > rem) { 1339 /* the userland length has been corrupted */ 1340 DPRINTF("corrupt userland length " 1341 "%u > %u\n", temp, rem); 1342 fs_ep.status = USB_ERR_INVAL; 1343 goto complete; 1344 } 1345 rem -= temp; 1346 1347 /* get actual transfer length */ 1348 length = xfer->frlengths[n]; 1349 if (length > temp) { 1350 /* data overflow */ 1351 fs_ep.status = USB_ERR_INVAL; 1352 DPRINTF("data overflow %u > %u\n", 1353 length, temp); 1354 goto complete; 1355 } 1356 if (isread) { 1357 1358 /* we need to know the destination buffer */ 1359 error = copyin(fs_ep.ppBuffer + n, 1360 &uaddr, sizeof(uaddr)); 1361 if (error) { 1362 return (error); 1363 } 1364 if (xfer->flags_int.isochronous_xfr) { 1365 /* only one frame buffer */ 1366 kaddr = USB_ADD_BYTES( 1367 xfer->frbuffers[0].buffer, offset); 1368 } else { 1369 /* multiple frame buffers */ 1370 kaddr = xfer->frbuffers[n].buffer; 1371 } 1372 1373 /* move data */ 1374 error = copyout(kaddr, uaddr, length); 1375 if (error) { 1376 return (error); 1377 } 1378 } 1379 /* 1380 * Update offset according to initial length, which is 1381 * needed by isochronous transfers! 1382 */ 1383 offset += temp; 1384 1385 /* update length */ 1386 error = copyout(&length, 1387 fs_ep.pLength + n, sizeof(length)); 1388 if (error) { 1389 return (error); 1390 } 1391 } 1392 1393 complete: 1394 /* update "aFrames" */ 1395 error = copyout(&fs_ep.aFrames, &fs_ep_uptr->aFrames, 1396 sizeof(fs_ep.aFrames)); 1397 if (error) 1398 goto done; 1399 1400 /* update "isoc_time_complete" */ 1401 error = copyout(&fs_ep.isoc_time_complete, 1402 &fs_ep_uptr->isoc_time_complete, 1403 sizeof(fs_ep.isoc_time_complete)); 1404 if (error) 1405 goto done; 1406 1407 /* update "status" */ 1408 error = copyout(&fs_ep.status, &fs_ep_uptr->status, 1409 sizeof(fs_ep.status)); 1410 done: 1411 return (error); 1412 } 1413 1414 static uint8_t 1415 ugen_fifo_in_use(struct usb_fifo *f, int fflags) 1416 { 1417 struct usb_fifo *f_rx; 1418 struct usb_fifo *f_tx; 1419 1420 f_rx = f->udev->fifo[(f->fifo_index & ~1) + USB_FIFO_RX]; 1421 f_tx = f->udev->fifo[(f->fifo_index & ~1) + USB_FIFO_TX]; 1422 1423 if ((fflags & FREAD) && f_rx && 1424 (f_rx->xfer[0] || f_rx->xfer[1])) { 1425 return (1); /* RX FIFO in use */ 1426 } 1427 if ((fflags & FWRITE) && f_tx && 1428 (f_tx->xfer[0] || f_tx->xfer[1])) { 1429 return (1); /* TX FIFO in use */ 1430 } 1431 return (0); /* not in use */ 1432 } 1433 1434 static int 1435 ugen_ioctl(struct usb_fifo *f, u_long cmd, void *addr, int fflags) 1436 { 1437 struct usb_config usb_config[1]; 1438 struct usb_device_request req; 1439 union { 1440 struct usb_fs_complete *pcomp; 1441 struct usb_fs_start *pstart; 1442 struct usb_fs_stop *pstop; 1443 struct usb_fs_open *popen; 1444 struct usb_fs_open_stream *popen_stream; 1445 struct usb_fs_close *pclose; 1446 struct usb_fs_clear_stall_sync *pstall; 1447 void *addr; 1448 } u; 1449 struct usb_endpoint *ep; 1450 struct usb_endpoint_descriptor *ed; 1451 struct usb_xfer *xfer; 1452 int error = 0; 1453 uint8_t iface_index; 1454 uint8_t isread; 1455 uint8_t ep_index; 1456 uint8_t pre_scale; 1457 1458 u.addr = addr; 1459 1460 DPRINTFN(6, "cmd=0x%08lx\n", cmd); 1461 1462 switch (cmd) { 1463 case USB_FS_COMPLETE: 1464 mtx_lock(f->priv_mtx); 1465 error = ugen_fs_get_complete(f, &ep_index); 1466 mtx_unlock(f->priv_mtx); 1467 1468 if (error) { 1469 error = EBUSY; 1470 break; 1471 } 1472 u.pcomp->ep_index = ep_index; 1473 error = ugen_fs_copy_out(f, u.pcomp->ep_index); 1474 break; 1475 1476 case USB_FS_START: 1477 error = ugen_fs_copy_in(f, u.pstart->ep_index); 1478 if (error) 1479 break; 1480 mtx_lock(f->priv_mtx); 1481 xfer = f->fs_xfer[u.pstart->ep_index]; 1482 usbd_transfer_start(xfer); 1483 mtx_unlock(f->priv_mtx); 1484 break; 1485 1486 case USB_FS_STOP: 1487 if (u.pstop->ep_index >= f->fs_ep_max) { 1488 error = EINVAL; 1489 break; 1490 } 1491 mtx_lock(f->priv_mtx); 1492 xfer = f->fs_xfer[u.pstart->ep_index]; 1493 if (usbd_transfer_pending(xfer)) { 1494 usbd_transfer_stop(xfer); 1495 1496 /* 1497 * Check if the USB transfer was stopped 1498 * before it was even started and fake a 1499 * cancel event. 1500 */ 1501 if (!xfer->flags_int.transferring && 1502 !xfer->flags_int.started) { 1503 DPRINTF("Issuing fake completion event\n"); 1504 ugen_fs_set_complete(xfer->priv_sc, 1505 USB_P2U(xfer->priv_fifo)); 1506 } 1507 } 1508 mtx_unlock(f->priv_mtx); 1509 break; 1510 1511 case USB_FS_OPEN: 1512 case USB_FS_OPEN_STREAM: 1513 if (u.popen->ep_index >= f->fs_ep_max) { 1514 error = EINVAL; 1515 break; 1516 } 1517 if (f->fs_xfer[u.popen->ep_index] != NULL) { 1518 error = EBUSY; 1519 break; 1520 } 1521 if (u.popen->max_bufsize > USB_FS_MAX_BUFSIZE) { 1522 u.popen->max_bufsize = USB_FS_MAX_BUFSIZE; 1523 } 1524 if (u.popen->max_frames & USB_FS_MAX_FRAMES_PRE_SCALE) { 1525 pre_scale = 1; 1526 u.popen->max_frames &= ~USB_FS_MAX_FRAMES_PRE_SCALE; 1527 } else { 1528 pre_scale = 0; 1529 } 1530 if (u.popen->max_frames > USB_FS_MAX_FRAMES) { 1531 u.popen->max_frames = USB_FS_MAX_FRAMES; 1532 break; 1533 } 1534 if (u.popen->max_frames == 0) { 1535 error = EINVAL; 1536 break; 1537 } 1538 ep = usbd_get_ep_by_addr(f->udev, u.popen->ep_no); 1539 if (ep == NULL) { 1540 error = EINVAL; 1541 break; 1542 } 1543 ed = ep->edesc; 1544 if (ed == NULL) { 1545 error = ENXIO; 1546 break; 1547 } 1548 iface_index = ep->iface_index; 1549 1550 memset(usb_config, 0, sizeof(usb_config)); 1551 1552 usb_config[0].type = ed->bmAttributes & UE_XFERTYPE; 1553 usb_config[0].endpoint = ed->bEndpointAddress & UE_ADDR; 1554 usb_config[0].direction = ed->bEndpointAddress & (UE_DIR_OUT | UE_DIR_IN); 1555 usb_config[0].interval = USB_DEFAULT_INTERVAL; 1556 usb_config[0].flags.proxy_buffer = 1; 1557 if (pre_scale != 0) 1558 usb_config[0].flags.pre_scale_frames = 1; 1559 usb_config[0].callback = &ugen_ctrl_fs_callback; 1560 usb_config[0].timeout = 0; /* no timeout */ 1561 usb_config[0].frames = u.popen->max_frames; 1562 usb_config[0].bufsize = u.popen->max_bufsize; 1563 usb_config[0].usb_mode = USB_MODE_DUAL; /* both modes */ 1564 if (cmd == USB_FS_OPEN_STREAM) 1565 usb_config[0].stream_id = u.popen_stream->stream_id; 1566 1567 if (usb_config[0].type == UE_CONTROL) { 1568 if (f->udev->flags.usb_mode != USB_MODE_HOST) { 1569 error = EINVAL; 1570 break; 1571 } 1572 } else { 1573 1574 isread = ((usb_config[0].endpoint & 1575 (UE_DIR_IN | UE_DIR_OUT)) == UE_DIR_IN); 1576 1577 if (f->udev->flags.usb_mode != USB_MODE_HOST) { 1578 isread = !isread; 1579 } 1580 /* check permissions */ 1581 if (isread) { 1582 if (!(fflags & FREAD)) { 1583 error = EPERM; 1584 break; 1585 } 1586 } else { 1587 if (!(fflags & FWRITE)) { 1588 error = EPERM; 1589 break; 1590 } 1591 } 1592 } 1593 error = usbd_transfer_setup(f->udev, &iface_index, 1594 f->fs_xfer + u.popen->ep_index, usb_config, 1, 1595 f, f->priv_mtx); 1596 if (error == 0) { 1597 /* update maximums */ 1598 u.popen->max_packet_length = 1599 f->fs_xfer[u.popen->ep_index]->max_frame_size; 1600 u.popen->max_bufsize = 1601 f->fs_xfer[u.popen->ep_index]->max_data_length; 1602 /* update number of frames */ 1603 u.popen->max_frames = 1604 f->fs_xfer[u.popen->ep_index]->nframes; 1605 /* store index of endpoint */ 1606 f->fs_xfer[u.popen->ep_index]->priv_fifo = 1607 ((uint8_t *)0) + u.popen->ep_index; 1608 } else { 1609 error = ENOMEM; 1610 } 1611 break; 1612 1613 case USB_FS_CLOSE: 1614 if (u.pclose->ep_index >= f->fs_ep_max) { 1615 error = EINVAL; 1616 break; 1617 } 1618 if (f->fs_xfer[u.pclose->ep_index] == NULL) { 1619 error = EINVAL; 1620 break; 1621 } 1622 usbd_transfer_unsetup(f->fs_xfer + u.pclose->ep_index, 1); 1623 break; 1624 1625 case USB_FS_CLEAR_STALL_SYNC: 1626 if (u.pstall->ep_index >= f->fs_ep_max) { 1627 error = EINVAL; 1628 break; 1629 } 1630 if (f->fs_xfer[u.pstall->ep_index] == NULL) { 1631 error = EINVAL; 1632 break; 1633 } 1634 if (f->udev->flags.usb_mode != USB_MODE_HOST) { 1635 error = EINVAL; 1636 break; 1637 } 1638 mtx_lock(f->priv_mtx); 1639 error = usbd_transfer_pending(f->fs_xfer[u.pstall->ep_index]); 1640 mtx_unlock(f->priv_mtx); 1641 1642 if (error) { 1643 return (EBUSY); 1644 } 1645 ep = f->fs_xfer[u.pstall->ep_index]->endpoint; 1646 1647 /* setup a clear-stall packet */ 1648 req.bmRequestType = UT_WRITE_ENDPOINT; 1649 req.bRequest = UR_CLEAR_FEATURE; 1650 USETW(req.wValue, UF_ENDPOINT_HALT); 1651 req.wIndex[0] = ep->edesc->bEndpointAddress; 1652 req.wIndex[1] = 0; 1653 USETW(req.wLength, 0); 1654 1655 error = usbd_do_request(f->udev, NULL, &req, NULL); 1656 if (error == 0) { 1657 usbd_clear_data_toggle(f->udev, ep); 1658 } else { 1659 error = ENXIO; 1660 } 1661 break; 1662 1663 default: 1664 error = ENOIOCTL; 1665 break; 1666 } 1667 1668 DPRINTFN(6, "error=%d\n", error); 1669 1670 return (error); 1671 } 1672 1673 static int 1674 ugen_set_short_xfer(struct usb_fifo *f, void *addr) 1675 { 1676 uint8_t t; 1677 1678 if (*(int *)addr) 1679 t = 1; 1680 else 1681 t = 0; 1682 1683 if (f->flag_short == t) { 1684 /* same value like before - accept */ 1685 return (0); 1686 } 1687 if (f->xfer[0] || f->xfer[1]) { 1688 /* cannot change this during transfer */ 1689 return (EBUSY); 1690 } 1691 f->flag_short = t; 1692 return (0); 1693 } 1694 1695 static int 1696 ugen_set_timeout(struct usb_fifo *f, void *addr) 1697 { 1698 f->timeout = *(int *)addr; 1699 if (f->timeout > 65535) { 1700 /* limit user input */ 1701 f->timeout = 65535; 1702 } 1703 return (0); 1704 } 1705 1706 static int 1707 ugen_get_frame_size(struct usb_fifo *f, void *addr) 1708 { 1709 if (f->xfer[0]) { 1710 *(int *)addr = f->xfer[0]->max_frame_size; 1711 } else { 1712 return (EINVAL); 1713 } 1714 return (0); 1715 } 1716 1717 static int 1718 ugen_set_buffer_size(struct usb_fifo *f, void *addr) 1719 { 1720 usb_frlength_t t; 1721 1722 if (*(int *)addr < 0) 1723 t = 0; /* use "wMaxPacketSize" */ 1724 else if (*(int *)addr < (256 * 1024)) 1725 t = *(int *)addr; 1726 else 1727 t = 256 * 1024; 1728 1729 if (f->bufsize == t) { 1730 /* same value like before - accept */ 1731 return (0); 1732 } 1733 if (f->xfer[0] || f->xfer[1]) { 1734 /* cannot change this during transfer */ 1735 return (EBUSY); 1736 } 1737 f->bufsize = t; 1738 return (0); 1739 } 1740 1741 static int 1742 ugen_get_buffer_size(struct usb_fifo *f, void *addr) 1743 { 1744 *(int *)addr = f->bufsize; 1745 return (0); 1746 } 1747 1748 static int 1749 ugen_get_iface_desc(struct usb_fifo *f, 1750 struct usb_interface_descriptor *idesc) 1751 { 1752 struct usb_interface *iface; 1753 1754 iface = usbd_get_iface(f->udev, f->iface_index); 1755 if (iface && iface->idesc) { 1756 *idesc = *(iface->idesc); 1757 } else { 1758 return (EIO); 1759 } 1760 return (0); 1761 } 1762 1763 static int 1764 ugen_get_endpoint_desc(struct usb_fifo *f, 1765 struct usb_endpoint_descriptor *ed) 1766 { 1767 struct usb_endpoint *ep; 1768 1769 ep = usb_fifo_softc(f); 1770 1771 if (ep && ep->edesc) { 1772 *ed = *ep->edesc; 1773 } else { 1774 return (EINVAL); 1775 } 1776 return (0); 1777 } 1778 1779 static int 1780 ugen_set_power_mode(struct usb_fifo *f, int mode) 1781 { 1782 struct usb_device *udev = f->udev; 1783 int err; 1784 uint8_t old_mode; 1785 1786 if ((udev == NULL) || 1787 (udev->parent_hub == NULL)) { 1788 return (EINVAL); 1789 } 1790 err = priv_check(curthread, PRIV_DRIVER); 1791 if (err) 1792 return (err); 1793 1794 /* get old power mode */ 1795 old_mode = udev->power_mode; 1796 1797 /* if no change, then just return */ 1798 if (old_mode == mode) 1799 return (0); 1800 1801 switch (mode) { 1802 case USB_POWER_MODE_OFF: 1803 if (udev->flags.usb_mode == USB_MODE_HOST && 1804 udev->re_enumerate_wait == USB_RE_ENUM_DONE) { 1805 udev->re_enumerate_wait = USB_RE_ENUM_PWR_OFF; 1806 } 1807 /* set power mode will wake up the explore thread */ 1808 break; 1809 1810 case USB_POWER_MODE_ON: 1811 case USB_POWER_MODE_SAVE: 1812 break; 1813 1814 case USB_POWER_MODE_RESUME: 1815 #if USB_HAVE_POWERD 1816 /* let USB-powerd handle resume */ 1817 USB_BUS_LOCK(udev->bus); 1818 udev->pwr_save.write_refs++; 1819 udev->pwr_save.last_xfer_time = ticks; 1820 USB_BUS_UNLOCK(udev->bus); 1821 1822 /* set new power mode */ 1823 usbd_set_power_mode(udev, USB_POWER_MODE_SAVE); 1824 1825 /* wait for resume to complete */ 1826 usb_pause_mtx(NULL, hz / 4); 1827 1828 /* clear write reference */ 1829 USB_BUS_LOCK(udev->bus); 1830 udev->pwr_save.write_refs--; 1831 USB_BUS_UNLOCK(udev->bus); 1832 #endif 1833 mode = USB_POWER_MODE_SAVE; 1834 break; 1835 1836 case USB_POWER_MODE_SUSPEND: 1837 #if USB_HAVE_POWERD 1838 /* let USB-powerd handle suspend */ 1839 USB_BUS_LOCK(udev->bus); 1840 udev->pwr_save.last_xfer_time = ticks - (256 * hz); 1841 USB_BUS_UNLOCK(udev->bus); 1842 #endif 1843 mode = USB_POWER_MODE_SAVE; 1844 break; 1845 1846 default: 1847 return (EINVAL); 1848 } 1849 1850 if (err) 1851 return (ENXIO); /* I/O failure */ 1852 1853 /* if we are powered off we need to re-enumerate first */ 1854 if (old_mode == USB_POWER_MODE_OFF) { 1855 if (udev->flags.usb_mode == USB_MODE_HOST && 1856 udev->re_enumerate_wait == USB_RE_ENUM_DONE) { 1857 udev->re_enumerate_wait = USB_RE_ENUM_START; 1858 } 1859 /* set power mode will wake up the explore thread */ 1860 } 1861 1862 /* set new power mode */ 1863 usbd_set_power_mode(udev, mode); 1864 1865 return (0); /* success */ 1866 } 1867 1868 static int 1869 ugen_get_power_mode(struct usb_fifo *f) 1870 { 1871 struct usb_device *udev = f->udev; 1872 1873 if (udev == NULL) 1874 return (USB_POWER_MODE_ON); 1875 1876 return (udev->power_mode); 1877 } 1878 1879 static int 1880 ugen_get_port_path(struct usb_fifo *f, struct usb_device_port_path *dpp) 1881 { 1882 struct usb_device *udev = f->udev; 1883 struct usb_device *next; 1884 unsigned int nlevel = 0; 1885 1886 if (udev == NULL) 1887 goto error; 1888 1889 dpp->udp_bus = device_get_unit(udev->bus->bdev); 1890 dpp->udp_index = udev->device_index; 1891 1892 /* count port levels */ 1893 next = udev; 1894 while (next->parent_hub != NULL) { 1895 nlevel++; 1896 next = next->parent_hub; 1897 } 1898 1899 /* check if too many levels */ 1900 if (nlevel > USB_DEVICE_PORT_PATH_MAX) 1901 goto error; 1902 1903 /* store total level of ports */ 1904 dpp->udp_port_level = nlevel; 1905 1906 /* store port index array */ 1907 next = udev; 1908 while (next->parent_hub != NULL) { 1909 dpp->udp_port_no[--nlevel] = next->port_no; 1910 next = next->parent_hub; 1911 } 1912 return (0); /* success */ 1913 1914 error: 1915 return (EINVAL); /* failure */ 1916 } 1917 1918 static int 1919 ugen_get_power_usage(struct usb_fifo *f) 1920 { 1921 struct usb_device *udev = f->udev; 1922 1923 if (udev == NULL) 1924 return (0); 1925 1926 return (udev->power); 1927 } 1928 1929 static int 1930 ugen_do_port_feature(struct usb_fifo *f, uint8_t port_no, 1931 uint8_t set, uint16_t feature) 1932 { 1933 struct usb_device *udev = f->udev; 1934 struct usb_hub *hub; 1935 int err; 1936 1937 err = priv_check(curthread, PRIV_DRIVER); 1938 if (err) { 1939 return (err); 1940 } 1941 if (port_no == 0) { 1942 return (EINVAL); 1943 } 1944 if ((udev == NULL) || 1945 (udev->hub == NULL)) { 1946 return (EINVAL); 1947 } 1948 hub = udev->hub; 1949 1950 if (port_no > hub->nports) { 1951 return (EINVAL); 1952 } 1953 if (set) 1954 err = usbd_req_set_port_feature(udev, 1955 NULL, port_no, feature); 1956 else 1957 err = usbd_req_clear_port_feature(udev, 1958 NULL, port_no, feature); 1959 1960 if (err) 1961 return (ENXIO); /* failure */ 1962 1963 return (0); /* success */ 1964 } 1965 1966 static int 1967 ugen_iface_ioctl(struct usb_fifo *f, u_long cmd, void *addr, int fflags) 1968 { 1969 struct usb_fifo *f_rx; 1970 struct usb_fifo *f_tx; 1971 int error = 0; 1972 1973 f_rx = f->udev->fifo[(f->fifo_index & ~1) + USB_FIFO_RX]; 1974 f_tx = f->udev->fifo[(f->fifo_index & ~1) + USB_FIFO_TX]; 1975 1976 switch (cmd) { 1977 case USB_SET_RX_SHORT_XFER: 1978 if (fflags & FREAD) { 1979 error = ugen_set_short_xfer(f_rx, addr); 1980 } else { 1981 error = EINVAL; 1982 } 1983 break; 1984 1985 case USB_SET_TX_FORCE_SHORT: 1986 if (fflags & FWRITE) { 1987 error = ugen_set_short_xfer(f_tx, addr); 1988 } else { 1989 error = EINVAL; 1990 } 1991 break; 1992 1993 case USB_SET_RX_TIMEOUT: 1994 if (fflags & FREAD) { 1995 error = ugen_set_timeout(f_rx, addr); 1996 } else { 1997 error = EINVAL; 1998 } 1999 break; 2000 2001 case USB_SET_TX_TIMEOUT: 2002 if (fflags & FWRITE) { 2003 error = ugen_set_timeout(f_tx, addr); 2004 } else { 2005 error = EINVAL; 2006 } 2007 break; 2008 2009 case USB_GET_RX_FRAME_SIZE: 2010 if (fflags & FREAD) { 2011 error = ugen_get_frame_size(f_rx, addr); 2012 } else { 2013 error = EINVAL; 2014 } 2015 break; 2016 2017 case USB_GET_TX_FRAME_SIZE: 2018 if (fflags & FWRITE) { 2019 error = ugen_get_frame_size(f_tx, addr); 2020 } else { 2021 error = EINVAL; 2022 } 2023 break; 2024 2025 case USB_SET_RX_BUFFER_SIZE: 2026 if (fflags & FREAD) { 2027 error = ugen_set_buffer_size(f_rx, addr); 2028 } else { 2029 error = EINVAL; 2030 } 2031 break; 2032 2033 case USB_SET_TX_BUFFER_SIZE: 2034 if (fflags & FWRITE) { 2035 error = ugen_set_buffer_size(f_tx, addr); 2036 } else { 2037 error = EINVAL; 2038 } 2039 break; 2040 2041 case USB_GET_RX_BUFFER_SIZE: 2042 if (fflags & FREAD) { 2043 error = ugen_get_buffer_size(f_rx, addr); 2044 } else { 2045 error = EINVAL; 2046 } 2047 break; 2048 2049 case USB_GET_TX_BUFFER_SIZE: 2050 if (fflags & FWRITE) { 2051 error = ugen_get_buffer_size(f_tx, addr); 2052 } else { 2053 error = EINVAL; 2054 } 2055 break; 2056 2057 case USB_GET_RX_INTERFACE_DESC: 2058 if (fflags & FREAD) { 2059 error = ugen_get_iface_desc(f_rx, addr); 2060 } else { 2061 error = EINVAL; 2062 } 2063 break; 2064 2065 case USB_GET_TX_INTERFACE_DESC: 2066 if (fflags & FWRITE) { 2067 error = ugen_get_iface_desc(f_tx, addr); 2068 } else { 2069 error = EINVAL; 2070 } 2071 break; 2072 2073 case USB_GET_RX_ENDPOINT_DESC: 2074 if (fflags & FREAD) { 2075 error = ugen_get_endpoint_desc(f_rx, addr); 2076 } else { 2077 error = EINVAL; 2078 } 2079 break; 2080 2081 case USB_GET_TX_ENDPOINT_DESC: 2082 if (fflags & FWRITE) { 2083 error = ugen_get_endpoint_desc(f_tx, addr); 2084 } else { 2085 error = EINVAL; 2086 } 2087 break; 2088 2089 case USB_SET_RX_STALL_FLAG: 2090 if ((fflags & FREAD) && (*(int *)addr)) { 2091 f_rx->flag_stall = 1; 2092 } 2093 break; 2094 2095 case USB_SET_TX_STALL_FLAG: 2096 if ((fflags & FWRITE) && (*(int *)addr)) { 2097 f_tx->flag_stall = 1; 2098 } 2099 break; 2100 2101 default: 2102 error = ENOIOCTL; 2103 break; 2104 } 2105 return (error); 2106 } 2107 2108 static int 2109 ugen_ioctl_post(struct usb_fifo *f, u_long cmd, void *addr, int fflags) 2110 { 2111 union { 2112 struct usb_interface_descriptor *idesc; 2113 struct usb_alt_interface *ai; 2114 struct usb_device_descriptor *ddesc; 2115 struct usb_config_descriptor *cdesc; 2116 struct usb_device_stats *stat; 2117 struct usb_fs_init *pinit; 2118 struct usb_fs_uninit *puninit; 2119 struct usb_device_port_path *dpp; 2120 uint32_t *ptime; 2121 void *addr; 2122 int *pint; 2123 } u; 2124 struct usb_device_descriptor *dtemp; 2125 struct usb_config_descriptor *ctemp; 2126 struct usb_interface *iface; 2127 int error = 0; 2128 uint8_t n; 2129 2130 u.addr = addr; 2131 2132 DPRINTFN(6, "cmd=0x%08lx\n", cmd); 2133 2134 switch (cmd) { 2135 case USB_DISCOVER: 2136 usb_needs_explore_all(); 2137 break; 2138 2139 case USB_SETDEBUG: 2140 if (!(fflags & FWRITE)) { 2141 error = EPERM; 2142 break; 2143 } 2144 usb_debug = *(int *)addr; 2145 break; 2146 2147 case USB_GET_CONFIG: 2148 *(int *)addr = f->udev->curr_config_index; 2149 break; 2150 2151 case USB_SET_CONFIG: 2152 if (!(fflags & FWRITE)) { 2153 error = EPERM; 2154 break; 2155 } 2156 error = ugen_set_config(f, *(int *)addr); 2157 break; 2158 2159 case USB_GET_ALTINTERFACE: 2160 iface = usbd_get_iface(f->udev, 2161 u.ai->uai_interface_index); 2162 if (iface && iface->idesc) { 2163 u.ai->uai_alt_index = iface->alt_index; 2164 } else { 2165 error = EINVAL; 2166 } 2167 break; 2168 2169 case USB_SET_ALTINTERFACE: 2170 if (!(fflags & FWRITE)) { 2171 error = EPERM; 2172 break; 2173 } 2174 error = ugen_set_interface(f, 2175 u.ai->uai_interface_index, u.ai->uai_alt_index); 2176 break; 2177 2178 case USB_GET_DEVICE_DESC: 2179 dtemp = usbd_get_device_descriptor(f->udev); 2180 if (!dtemp) { 2181 error = EIO; 2182 break; 2183 } 2184 *u.ddesc = *dtemp; 2185 break; 2186 2187 case USB_GET_CONFIG_DESC: 2188 ctemp = usbd_get_config_descriptor(f->udev); 2189 if (!ctemp) { 2190 error = EIO; 2191 break; 2192 } 2193 *u.cdesc = *ctemp; 2194 break; 2195 2196 case USB_GET_FULL_DESC: 2197 error = ugen_get_cdesc(f, addr); 2198 break; 2199 2200 case USB_GET_STRING_DESC: 2201 error = ugen_get_sdesc(f, addr); 2202 break; 2203 2204 case USB_GET_IFACE_DRIVER: 2205 error = ugen_get_iface_driver(f, addr); 2206 break; 2207 2208 case USB_REQUEST: 2209 case USB_DO_REQUEST: 2210 if (!(fflags & FWRITE)) { 2211 error = EPERM; 2212 break; 2213 } 2214 error = ugen_do_request(f, addr); 2215 break; 2216 2217 case USB_DEVICEINFO: 2218 case USB_GET_DEVICEINFO: 2219 error = usb_gen_fill_deviceinfo(f, addr); 2220 break; 2221 2222 case USB_DEVICESTATS: 2223 for (n = 0; n != 4; n++) { 2224 2225 u.stat->uds_requests_fail[n] = 2226 f->udev->stats_err.uds_requests[n]; 2227 u.stat->uds_requests_ok[n] = 2228 f->udev->stats_ok.uds_requests[n]; 2229 } 2230 break; 2231 2232 case USB_DEVICEENUMERATE: 2233 error = ugen_re_enumerate(f); 2234 break; 2235 2236 case USB_GET_PLUGTIME: 2237 *u.ptime = f->udev->plugtime; 2238 break; 2239 2240 case USB_CLAIM_INTERFACE: 2241 case USB_RELEASE_INTERFACE: 2242 /* TODO */ 2243 break; 2244 2245 case USB_IFACE_DRIVER_ACTIVE: 2246 2247 n = *u.pint & 0xFF; 2248 2249 iface = usbd_get_iface(f->udev, n); 2250 2251 if (iface && iface->subdev) 2252 error = 0; 2253 else 2254 error = ENXIO; 2255 break; 2256 2257 case USB_IFACE_DRIVER_DETACH: 2258 2259 error = priv_check(curthread, PRIV_DRIVER); 2260 2261 if (error) 2262 break; 2263 2264 n = *u.pint & 0xFF; 2265 2266 if (n == USB_IFACE_INDEX_ANY) { 2267 error = EINVAL; 2268 break; 2269 } 2270 2271 /* 2272 * Detach the currently attached driver. 2273 */ 2274 usb_detach_device(f->udev, n, 0); 2275 2276 /* 2277 * Set parent to self, this should keep attach away 2278 * until the next set configuration event. 2279 */ 2280 usbd_set_parent_iface(f->udev, n, n); 2281 break; 2282 2283 case USB_SET_POWER_MODE: 2284 error = ugen_set_power_mode(f, *u.pint); 2285 break; 2286 2287 case USB_GET_POWER_MODE: 2288 *u.pint = ugen_get_power_mode(f); 2289 break; 2290 2291 case USB_GET_DEV_PORT_PATH: 2292 error = ugen_get_port_path(f, u.dpp); 2293 break; 2294 2295 case USB_GET_POWER_USAGE: 2296 *u.pint = ugen_get_power_usage(f); 2297 break; 2298 2299 case USB_SET_PORT_ENABLE: 2300 error = ugen_do_port_feature(f, 2301 *u.pint, 1, UHF_PORT_ENABLE); 2302 break; 2303 2304 case USB_SET_PORT_DISABLE: 2305 error = ugen_do_port_feature(f, 2306 *u.pint, 0, UHF_PORT_ENABLE); 2307 break; 2308 2309 case USB_FS_INIT: 2310 /* verify input parameters */ 2311 if (u.pinit->pEndpoints == NULL) { 2312 error = EINVAL; 2313 break; 2314 } 2315 if (u.pinit->ep_index_max > 127) { 2316 error = EINVAL; 2317 break; 2318 } 2319 if (u.pinit->ep_index_max == 0) { 2320 error = EINVAL; 2321 break; 2322 } 2323 if (f->fs_xfer != NULL) { 2324 error = EBUSY; 2325 break; 2326 } 2327 if (f->dev_ep_index != 0) { 2328 error = EINVAL; 2329 break; 2330 } 2331 if (ugen_fifo_in_use(f, fflags)) { 2332 error = EBUSY; 2333 break; 2334 } 2335 error = usb_fifo_alloc_buffer(f, 1, u.pinit->ep_index_max); 2336 if (error) { 2337 break; 2338 } 2339 f->fs_xfer = malloc(sizeof(f->fs_xfer[0]) * 2340 u.pinit->ep_index_max, M_USB, M_WAITOK | M_ZERO); 2341 if (f->fs_xfer == NULL) { 2342 usb_fifo_free_buffer(f); 2343 error = ENOMEM; 2344 break; 2345 } 2346 f->fs_ep_max = u.pinit->ep_index_max; 2347 f->fs_ep_ptr = u.pinit->pEndpoints; 2348 break; 2349 2350 case USB_FS_UNINIT: 2351 if (u.puninit->dummy != 0) { 2352 error = EINVAL; 2353 break; 2354 } 2355 error = ugen_fs_uninit(f); 2356 break; 2357 2358 default: 2359 mtx_lock(f->priv_mtx); 2360 error = ugen_iface_ioctl(f, cmd, addr, fflags); 2361 mtx_unlock(f->priv_mtx); 2362 break; 2363 } 2364 DPRINTFN(6, "error=%d\n", error); 2365 return (error); 2366 } 2367 2368 static void 2369 ugen_ctrl_fs_callback(struct usb_xfer *xfer, usb_error_t error) 2370 { 2371 ; /* workaround for a bug in "indent" */ 2372 2373 DPRINTF("st=%u alen=%u aframes=%u\n", 2374 USB_GET_STATE(xfer), xfer->actlen, xfer->aframes); 2375 2376 switch (USB_GET_STATE(xfer)) { 2377 case USB_ST_SETUP: 2378 usbd_transfer_submit(xfer); 2379 break; 2380 default: 2381 ugen_fs_set_complete(xfer->priv_sc, USB_P2U(xfer->priv_fifo)); 2382 break; 2383 } 2384 } 2385 #endif /* USB_HAVE_UGEN */ 2386