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