2/*- 3 * Copyright (c) 2007 Luigi Rizzo - Universita` di Pisa. All rights reserved. 4 * Copyright (c) 2007 Hans Petter Selasky. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28#include <sys/stdint.h> 29#include <sys/stddef.h> 30#include <sys/param.h> 31#include <sys/queue.h> 32#include <sys/types.h> 33#include <sys/systm.h> 34#include <sys/kernel.h> 35#include <sys/bus.h> 36#include <sys/linker_set.h> 37#include <sys/module.h> 38#include <sys/lock.h> 39#include <sys/mutex.h> 40#include <sys/condvar.h> 41#include <sys/sysctl.h> 42#include <sys/sx.h> 43#include <sys/unistd.h> 44#include <sys/callout.h> 45#include <sys/malloc.h> 46#include <sys/priv.h> 47 48#include <dev/usb/usb.h> 49#include <dev/usb/usb_ioctl.h> 50#include <dev/usb/usbdi.h> 51#include <dev/usb/usbdi_util.h> 52 53#define USB_DEBUG_VAR usb_debug 54 55#include <dev/usb/usb_core.h> 56#include <dev/usb/usb_compat_linux.h> 57#include <dev/usb/usb_process.h> 58#include <dev/usb/usb_device.h> 59#include <dev/usb/usb_util.h> 60#include <dev/usb/usb_busdma.h> 61#include <dev/usb/usb_transfer.h> 62#include <dev/usb/usb_hub.h> 63#include <dev/usb/usb_request.h> 64#include <dev/usb/usb_debug.h> 65 66struct usb_linux_softc { 67 LIST_ENTRY(usb_linux_softc) sc_attached_list; 68 69 device_t sc_fbsd_dev; 70 struct usb_device *sc_fbsd_udev; 71 struct usb_interface *sc_ui; 72 struct usb_driver *sc_udrv; 73}; 74 75/* prototypes */ 76static device_probe_t usb_linux_probe; 77static device_attach_t usb_linux_attach; 78static device_detach_t usb_linux_detach; 79static device_suspend_t usb_linux_suspend; 80static device_resume_t usb_linux_resume; 81 82static usb_callback_t usb_linux_isoc_callback; 83static usb_callback_t usb_linux_non_isoc_callback; 84 85static usb_complete_t usb_linux_wait_complete; 86 87static uint16_t usb_max_isoc_frames(struct usb_device *); 88static int usb_start_wait_urb(struct urb *, usb_timeout_t, uint16_t *); 89static const struct usb_device_id *usb_linux_lookup_id( 90 const struct usb_device_id *, struct usb_attach_arg *); 91static struct usb_driver *usb_linux_get_usb_driver(struct usb_linux_softc *); 92static int usb_linux_create_usb_device(struct usb_device *, device_t); 93static void usb_linux_cleanup_interface(struct usb_device *, 94 struct usb_interface *); 95static void usb_linux_complete(struct usb_xfer *); 96static int usb_unlink_urb_sub(struct urb *, uint8_t); 97 98/*------------------------------------------------------------------------* 99 * FreeBSD USB interface 100 *------------------------------------------------------------------------*/ 101 102static LIST_HEAD(, usb_linux_softc) usb_linux_attached_list; 103static LIST_HEAD(, usb_driver) usb_linux_driver_list; 104 105static device_method_t usb_linux_methods[] = { 106 /* Device interface */ 107 DEVMETHOD(device_probe, usb_linux_probe), 108 DEVMETHOD(device_attach, usb_linux_attach), 109 DEVMETHOD(device_detach, usb_linux_detach), 110 DEVMETHOD(device_suspend, usb_linux_suspend), 111 DEVMETHOD(device_resume, usb_linux_resume), 112 113 {0, 0} 114}; 115 116static driver_t usb_linux_driver = { 117 .name = "usb_linux", 118 .methods = usb_linux_methods, 119 .size = sizeof(struct usb_linux_softc), 120}; 121 122static devclass_t usb_linux_devclass; 123 124DRIVER_MODULE(usb_linux, uhub, usb_linux_driver, usb_linux_devclass, NULL, 0);
| 2/*- 3 * Copyright (c) 2007 Luigi Rizzo - Universita` di Pisa. All rights reserved. 4 * Copyright (c) 2007 Hans Petter Selasky. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28#include <sys/stdint.h> 29#include <sys/stddef.h> 30#include <sys/param.h> 31#include <sys/queue.h> 32#include <sys/types.h> 33#include <sys/systm.h> 34#include <sys/kernel.h> 35#include <sys/bus.h> 36#include <sys/linker_set.h> 37#include <sys/module.h> 38#include <sys/lock.h> 39#include <sys/mutex.h> 40#include <sys/condvar.h> 41#include <sys/sysctl.h> 42#include <sys/sx.h> 43#include <sys/unistd.h> 44#include <sys/callout.h> 45#include <sys/malloc.h> 46#include <sys/priv.h> 47 48#include <dev/usb/usb.h> 49#include <dev/usb/usb_ioctl.h> 50#include <dev/usb/usbdi.h> 51#include <dev/usb/usbdi_util.h> 52 53#define USB_DEBUG_VAR usb_debug 54 55#include <dev/usb/usb_core.h> 56#include <dev/usb/usb_compat_linux.h> 57#include <dev/usb/usb_process.h> 58#include <dev/usb/usb_device.h> 59#include <dev/usb/usb_util.h> 60#include <dev/usb/usb_busdma.h> 61#include <dev/usb/usb_transfer.h> 62#include <dev/usb/usb_hub.h> 63#include <dev/usb/usb_request.h> 64#include <dev/usb/usb_debug.h> 65 66struct usb_linux_softc { 67 LIST_ENTRY(usb_linux_softc) sc_attached_list; 68 69 device_t sc_fbsd_dev; 70 struct usb_device *sc_fbsd_udev; 71 struct usb_interface *sc_ui; 72 struct usb_driver *sc_udrv; 73}; 74 75/* prototypes */ 76static device_probe_t usb_linux_probe; 77static device_attach_t usb_linux_attach; 78static device_detach_t usb_linux_detach; 79static device_suspend_t usb_linux_suspend; 80static device_resume_t usb_linux_resume; 81 82static usb_callback_t usb_linux_isoc_callback; 83static usb_callback_t usb_linux_non_isoc_callback; 84 85static usb_complete_t usb_linux_wait_complete; 86 87static uint16_t usb_max_isoc_frames(struct usb_device *); 88static int usb_start_wait_urb(struct urb *, usb_timeout_t, uint16_t *); 89static const struct usb_device_id *usb_linux_lookup_id( 90 const struct usb_device_id *, struct usb_attach_arg *); 91static struct usb_driver *usb_linux_get_usb_driver(struct usb_linux_softc *); 92static int usb_linux_create_usb_device(struct usb_device *, device_t); 93static void usb_linux_cleanup_interface(struct usb_device *, 94 struct usb_interface *); 95static void usb_linux_complete(struct usb_xfer *); 96static int usb_unlink_urb_sub(struct urb *, uint8_t); 97 98/*------------------------------------------------------------------------* 99 * FreeBSD USB interface 100 *------------------------------------------------------------------------*/ 101 102static LIST_HEAD(, usb_linux_softc) usb_linux_attached_list; 103static LIST_HEAD(, usb_driver) usb_linux_driver_list; 104 105static device_method_t usb_linux_methods[] = { 106 /* Device interface */ 107 DEVMETHOD(device_probe, usb_linux_probe), 108 DEVMETHOD(device_attach, usb_linux_attach), 109 DEVMETHOD(device_detach, usb_linux_detach), 110 DEVMETHOD(device_suspend, usb_linux_suspend), 111 DEVMETHOD(device_resume, usb_linux_resume), 112 113 {0, 0} 114}; 115 116static driver_t usb_linux_driver = { 117 .name = "usb_linux", 118 .methods = usb_linux_methods, 119 .size = sizeof(struct usb_linux_softc), 120}; 121 122static devclass_t usb_linux_devclass; 123 124DRIVER_MODULE(usb_linux, uhub, usb_linux_driver, usb_linux_devclass, NULL, 0);
|
125 126/*------------------------------------------------------------------------* 127 * usb_linux_lookup_id 128 * 129 * This functions takes an array of "struct usb_device_id" and tries 130 * to match the entries with the information in "struct usb_attach_arg". 131 * If it finds a match the matching entry will be returned. 132 * Else "NULL" will be returned. 133 *------------------------------------------------------------------------*/ 134static const struct usb_device_id * 135usb_linux_lookup_id(const struct usb_device_id *id, struct usb_attach_arg *uaa) 136{ 137 if (id == NULL) { 138 goto done; 139 } 140 /* 141 * Keep on matching array entries until we find one with 142 * "match_flags" equal to zero, which indicates the end of the 143 * array: 144 */ 145 for (; id->match_flags; id++) { 146 147 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) && 148 (id->idVendor != uaa->info.idVendor)) { 149 continue; 150 } 151 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) && 152 (id->idProduct != uaa->info.idProduct)) { 153 continue; 154 } 155 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) && 156 (id->bcdDevice_lo > uaa->info.bcdDevice)) { 157 continue; 158 } 159 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) && 160 (id->bcdDevice_hi < uaa->info.bcdDevice)) { 161 continue; 162 } 163 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) && 164 (id->bDeviceClass != uaa->info.bDeviceClass)) { 165 continue; 166 } 167 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) && 168 (id->bDeviceSubClass != uaa->info.bDeviceSubClass)) { 169 continue; 170 } 171 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) && 172 (id->bDeviceProtocol != uaa->info.bDeviceProtocol)) { 173 continue; 174 } 175 if ((uaa->info.bDeviceClass == 0xFF) && 176 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) && 177 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS | 178 USB_DEVICE_ID_MATCH_INT_SUBCLASS | 179 USB_DEVICE_ID_MATCH_INT_PROTOCOL))) { 180 continue; 181 } 182 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) && 183 (id->bInterfaceClass != uaa->info.bInterfaceClass)) { 184 continue; 185 } 186 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) && 187 (id->bInterfaceSubClass != uaa->info.bInterfaceSubClass)) { 188 continue; 189 } 190 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) && 191 (id->bInterfaceProtocol != uaa->info.bInterfaceProtocol)) { 192 continue; 193 } 194 /* we found a match! */ 195 return (id); 196 } 197 198done: 199 return (NULL); 200} 201 202/*------------------------------------------------------------------------* 203 * usb_linux_probe 204 * 205 * This function is the FreeBSD probe callback. It is called from the 206 * FreeBSD USB stack through the "device_probe_and_attach()" function. 207 *------------------------------------------------------------------------*/ 208static int 209usb_linux_probe(device_t dev) 210{ 211 struct usb_attach_arg *uaa = device_get_ivars(dev); 212 struct usb_driver *udrv; 213 int err = ENXIO; 214 215 if (uaa->usb_mode != USB_MODE_HOST) { 216 return (ENXIO); 217 } 218 mtx_lock(&Giant); 219 LIST_FOREACH(udrv, &usb_linux_driver_list, linux_driver_list) { 220 if (usb_linux_lookup_id(udrv->id_table, uaa)) { 221 err = 0; 222 break; 223 } 224 } 225 mtx_unlock(&Giant); 226 227 return (err); 228} 229 230/*------------------------------------------------------------------------* 231 * usb_linux_get_usb_driver 232 * 233 * This function returns the pointer to the "struct usb_driver" where 234 * the Linux USB device driver "struct usb_device_id" match was found. 235 * We apply a lock before reading out the pointer to avoid races. 236 *------------------------------------------------------------------------*/ 237static struct usb_driver * 238usb_linux_get_usb_driver(struct usb_linux_softc *sc) 239{ 240 struct usb_driver *udrv; 241 242 mtx_lock(&Giant); 243 udrv = sc->sc_udrv; 244 mtx_unlock(&Giant); 245 return (udrv); 246} 247 248/*------------------------------------------------------------------------* 249 * usb_linux_attach 250 * 251 * This function is the FreeBSD attach callback. It is called from the 252 * FreeBSD USB stack through the "device_probe_and_attach()" function. 253 * This function is called when "usb_linux_probe()" returns zero. 254 *------------------------------------------------------------------------*/ 255static int 256usb_linux_attach(device_t dev) 257{ 258 struct usb_attach_arg *uaa = device_get_ivars(dev); 259 struct usb_linux_softc *sc = device_get_softc(dev); 260 struct usb_driver *udrv; 261 const struct usb_device_id *id = NULL; 262 263 mtx_lock(&Giant); 264 LIST_FOREACH(udrv, &usb_linux_driver_list, linux_driver_list) { 265 id = usb_linux_lookup_id(udrv->id_table, uaa); 266 if (id) 267 break; 268 } 269 mtx_unlock(&Giant); 270 271 if (id == NULL) { 272 return (ENXIO); 273 } 274 if (usb_linux_create_usb_device(uaa->device, dev) != 0) 275 return (ENOMEM); 276 device_set_usb_desc(dev); 277 278 sc->sc_fbsd_udev = uaa->device; 279 sc->sc_fbsd_dev = dev; 280 sc->sc_udrv = udrv; 281 sc->sc_ui = usb_ifnum_to_if(uaa->device, uaa->info.bIfaceNum); 282 if (sc->sc_ui == NULL) { 283 return (EINVAL); 284 } 285 if (udrv->probe) { 286 if ((udrv->probe) (sc->sc_ui, id)) { 287 return (ENXIO); 288 } 289 } 290 mtx_lock(&Giant); 291 LIST_INSERT_HEAD(&usb_linux_attached_list, sc, sc_attached_list); 292 mtx_unlock(&Giant); 293 294 /* success */ 295 return (0); 296} 297 298/*------------------------------------------------------------------------* 299 * usb_linux_detach 300 * 301 * This function is the FreeBSD detach callback. It is called from the 302 * FreeBSD USB stack through the "device_detach()" function. 303 *------------------------------------------------------------------------*/ 304static int 305usb_linux_detach(device_t dev) 306{ 307 struct usb_linux_softc *sc = device_get_softc(dev); 308 struct usb_driver *udrv = NULL; 309 310 mtx_lock(&Giant); 311 if (sc->sc_attached_list.le_prev) { 312 LIST_REMOVE(sc, sc_attached_list); 313 sc->sc_attached_list.le_prev = NULL; 314 udrv = sc->sc_udrv; 315 sc->sc_udrv = NULL; 316 } 317 mtx_unlock(&Giant); 318 319 if (udrv && udrv->disconnect) { 320 (udrv->disconnect) (sc->sc_ui); 321 } 322 /* 323 * Make sure that we free all FreeBSD USB transfers belonging to 324 * this Linux "usb_interface", hence they will most likely not be 325 * needed any more. 326 */ 327 usb_linux_cleanup_interface(sc->sc_fbsd_udev, sc->sc_ui); 328 return (0); 329} 330 331/*------------------------------------------------------------------------* 332 * usb_linux_suspend 333 * 334 * This function is the FreeBSD suspend callback. Usually it does nothing. 335 *------------------------------------------------------------------------*/ 336static int 337usb_linux_suspend(device_t dev) 338{ 339 struct usb_linux_softc *sc = device_get_softc(dev); 340 struct usb_driver *udrv = usb_linux_get_usb_driver(sc); 341 int err; 342 343 if (udrv && udrv->suspend) { 344 err = (udrv->suspend) (sc->sc_ui, 0); 345 } 346 return (0); 347} 348 349/*------------------------------------------------------------------------* 350 * usb_linux_resume 351 * 352 * This function is the FreeBSD resume callback. Usually it does nothing. 353 *------------------------------------------------------------------------*/ 354static int 355usb_linux_resume(device_t dev) 356{ 357 struct usb_linux_softc *sc = device_get_softc(dev); 358 struct usb_driver *udrv = usb_linux_get_usb_driver(sc); 359 int err; 360 361 if (udrv && udrv->resume) { 362 err = (udrv->resume) (sc->sc_ui); 363 } 364 return (0); 365} 366 367/*------------------------------------------------------------------------* 368 * Linux emulation layer 369 *------------------------------------------------------------------------*/ 370 371/*------------------------------------------------------------------------* 372 * usb_max_isoc_frames 373 * 374 * The following function returns the maximum number of isochronous 375 * frames that we support per URB. It is not part of the Linux USB API. 376 *------------------------------------------------------------------------*/ 377static uint16_t 378usb_max_isoc_frames(struct usb_device *dev) 379{ 380 ; /* indent fix */ 381 switch (usbd_get_speed(dev)) { 382 case USB_SPEED_LOW: 383 case USB_SPEED_FULL: 384 return (USB_MAX_FULL_SPEED_ISOC_FRAMES); 385 default: 386 return (USB_MAX_HIGH_SPEED_ISOC_FRAMES); 387 } 388} 389 390/*------------------------------------------------------------------------* 391 * usb_submit_urb 392 * 393 * This function is used to queue an URB after that it has been 394 * initialized. If it returns non-zero, it means that the URB was not 395 * queued. 396 *------------------------------------------------------------------------*/ 397int 398usb_submit_urb(struct urb *urb, uint16_t mem_flags) 399{ 400 struct usb_host_endpoint *uhe; 401 uint8_t do_unlock; 402 int err; 403 404 if (urb == NULL) 405 return (-EINVAL); 406 407 do_unlock = mtx_owned(&Giant) ? 0 : 1; 408 if (do_unlock) 409 mtx_lock(&Giant); 410 411 if (urb->endpoint == NULL) { 412 err = -EINVAL; 413 goto done; 414 } 415 416 /* 417 * Check to see if the urb is in the process of being killed 418 * and stop a urb that is in the process of being killed from 419 * being re-submitted (e.g. from its completion callback 420 * function). 421 */ 422 if (urb->kill_count != 0) { 423 err = -EPERM; 424 goto done; 425 } 426 427 uhe = urb->endpoint; 428 429 /* 430 * Check that we have got a FreeBSD USB transfer that will dequeue 431 * the URB structure and do the real transfer. If there are no USB 432 * transfers, then we return an error. 433 */ 434 if (uhe->bsd_xfer[0] || 435 uhe->bsd_xfer[1]) { 436 /* we are ready! */ 437 438 TAILQ_INSERT_TAIL(&uhe->bsd_urb_list, urb, bsd_urb_list); 439 440 urb->status = -EINPROGRESS; 441 442 usbd_transfer_start(uhe->bsd_xfer[0]); 443 usbd_transfer_start(uhe->bsd_xfer[1]); 444 err = 0; 445 } else { 446 /* no pipes have been setup yet! */ 447 urb->status = -EINVAL; 448 err = -EINVAL; 449 } 450done: 451 if (do_unlock) 452 mtx_unlock(&Giant); 453 return (err); 454} 455 456/*------------------------------------------------------------------------* 457 * usb_unlink_urb 458 * 459 * This function is used to stop an URB after that it is been 460 * submitted, but before the "complete" callback has been called. On 461 *------------------------------------------------------------------------*/ 462int 463usb_unlink_urb(struct urb *urb) 464{ 465 return (usb_unlink_urb_sub(urb, 0)); 466} 467 468static void 469usb_unlink_bsd(struct usb_xfer *xfer, 470 struct urb *urb, uint8_t drain) 471{ 472 if (xfer == NULL) 473 return; 474 if (!usbd_transfer_pending(xfer)) 475 return; 476 if (xfer->priv_fifo == (void *)urb) { 477 if (drain) { 478 mtx_unlock(&Giant); 479 usbd_transfer_drain(xfer); 480 mtx_lock(&Giant); 481 } else { 482 usbd_transfer_stop(xfer); 483 } 484 usbd_transfer_start(xfer); 485 } 486} 487 488static int 489usb_unlink_urb_sub(struct urb *urb, uint8_t drain) 490{ 491 struct usb_host_endpoint *uhe; 492 uint16_t x; 493 uint8_t do_unlock; 494 int err; 495 496 if (urb == NULL) 497 return (-EINVAL); 498 499 do_unlock = mtx_owned(&Giant) ? 0 : 1; 500 if (do_unlock) 501 mtx_lock(&Giant); 502 if (drain) 503 urb->kill_count++; 504 505 if (urb->endpoint == NULL) { 506 err = -EINVAL; 507 goto done; 508 } 509 uhe = urb->endpoint; 510 511 if (urb->bsd_urb_list.tqe_prev) { 512 513 /* not started yet, just remove it from the queue */ 514 TAILQ_REMOVE(&uhe->bsd_urb_list, urb, bsd_urb_list); 515 urb->bsd_urb_list.tqe_prev = NULL; 516 urb->status = -ECONNRESET; 517 urb->actual_length = 0; 518 519 for (x = 0; x < urb->number_of_packets; x++) { 520 urb->iso_frame_desc[x].actual_length = 0; 521 } 522 523 if (urb->complete) { 524 (urb->complete) (urb); 525 } 526 } else { 527 528 /* 529 * If the URB is not on the URB list, then check if one of 530 * the FreeBSD USB transfer are processing the current URB. 531 * If so, re-start that transfer, which will lead to the 532 * termination of that URB: 533 */ 534 usb_unlink_bsd(uhe->bsd_xfer[0], urb, drain); 535 usb_unlink_bsd(uhe->bsd_xfer[1], urb, drain); 536 } 537 err = 0; 538done: 539 if (drain) 540 urb->kill_count--; 541 if (do_unlock) 542 mtx_unlock(&Giant); 543 return (err); 544} 545 546/*------------------------------------------------------------------------* 547 * usb_clear_halt 548 * 549 * This function must always be used to clear the stall. Stall is when 550 * an USB endpoint returns a stall message to the USB host controller. 551 * Until the stall is cleared, no data can be transferred. 552 *------------------------------------------------------------------------*/ 553int 554usb_clear_halt(struct usb_device *dev, struct usb_host_endpoint *uhe) 555{ 556 struct usb_config cfg[1]; 557 struct usb_endpoint *ep; 558 uint8_t type; 559 uint8_t addr; 560 561 if (uhe == NULL) 562 return (-EINVAL); 563 564 type = uhe->desc.bmAttributes & UE_XFERTYPE; 565 addr = uhe->desc.bEndpointAddress; 566 567 bzero(cfg, sizeof(cfg)); 568 569 cfg[0].type = type; 570 cfg[0].endpoint = addr & UE_ADDR; 571 cfg[0].direction = addr & (UE_DIR_OUT | UE_DIR_IN); 572 573 ep = usbd_get_endpoint(dev, uhe->bsd_iface_index, cfg); 574 if (ep == NULL) 575 return (-EINVAL); 576 577 usbd_clear_data_toggle(dev, ep); 578 579 return (usb_control_msg(dev, &dev->ep0, 580 UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT, 581 UF_ENDPOINT_HALT, addr, NULL, 0, 1000)); 582} 583 584/*------------------------------------------------------------------------* 585 * usb_start_wait_urb 586 * 587 * This is an internal function that is used to perform synchronous 588 * Linux USB transfers. 589 *------------------------------------------------------------------------*/ 590static int 591usb_start_wait_urb(struct urb *urb, usb_timeout_t timeout, uint16_t *p_actlen) 592{ 593 int err; 594 uint8_t do_unlock; 595 596 /* you must have a timeout! */ 597 if (timeout == 0) { 598 timeout = 1; 599 } 600 urb->complete = &usb_linux_wait_complete; 601 urb->timeout = timeout; 602 urb->transfer_flags |= URB_WAIT_WAKEUP; 603 urb->transfer_flags &= ~URB_IS_SLEEPING; 604 605 do_unlock = mtx_owned(&Giant) ? 0 : 1; 606 if (do_unlock) 607 mtx_lock(&Giant); 608 err = usb_submit_urb(urb, 0); 609 if (err) 610 goto done; 611 612 /* 613 * the URB might have completed before we get here, so check that by 614 * using some flags! 615 */ 616 while (urb->transfer_flags & URB_WAIT_WAKEUP) { 617 urb->transfer_flags |= URB_IS_SLEEPING; 618 cv_wait(&urb->cv_wait, &Giant); 619 urb->transfer_flags &= ~URB_IS_SLEEPING; 620 } 621 622 err = urb->status; 623 624done: 625 if (do_unlock) 626 mtx_unlock(&Giant); 627 if (p_actlen != NULL) { 628 if (err) 629 *p_actlen = 0; 630 else 631 *p_actlen = urb->actual_length; 632 } 633 return (err); 634} 635 636/*------------------------------------------------------------------------* 637 * usb_control_msg 638 * 639 * The following function performs a control transfer sequence one any 640 * control, bulk or interrupt endpoint, specified by "uhe". A control 641 * transfer means that you transfer an 8-byte header first followed by 642 * a data-phase as indicated by the 8-byte header. The "timeout" is 643 * given in milliseconds. 644 * 645 * Return values: 646 * 0: Success 647 * < 0: Failure 648 * > 0: Acutal length 649 *------------------------------------------------------------------------*/ 650int 651usb_control_msg(struct usb_device *dev, struct usb_host_endpoint *uhe, 652 uint8_t request, uint8_t requesttype, 653 uint16_t value, uint16_t index, void *data, 654 uint16_t size, usb_timeout_t timeout) 655{ 656 struct usb_device_request req; 657 struct urb *urb; 658 int err; 659 uint16_t actlen; 660 uint8_t type; 661 uint8_t addr; 662 663 req.bmRequestType = requesttype; 664 req.bRequest = request; 665 USETW(req.wValue, value); 666 USETW(req.wIndex, index); 667 USETW(req.wLength, size); 668 669 if (uhe == NULL) { 670 return (-EINVAL); 671 } 672 type = (uhe->desc.bmAttributes & UE_XFERTYPE); 673 addr = (uhe->desc.bEndpointAddress & UE_ADDR); 674 675 if (type != UE_CONTROL) { 676 return (-EINVAL); 677 } 678 if (addr == 0) { 679 /* 680 * The FreeBSD USB stack supports standard control 681 * transfers on control endpoint zero: 682 */ 683 err = usbd_do_request_flags(dev, 684 NULL, &req, data, USB_SHORT_XFER_OK, 685 &actlen, timeout); 686 if (err) { 687 err = -EPIPE; 688 } else { 689 err = actlen; 690 } 691 return (err); 692 } 693 if (dev->flags.usb_mode != USB_MODE_HOST) { 694 /* not supported */ 695 return (-EINVAL); 696 } 697 err = usb_setup_endpoint(dev, uhe, 1 /* dummy */ ); 698 699 /* 700 * NOTE: we need to allocate real memory here so that we don't 701 * transfer data to/from the stack! 702 * 703 * 0xFFFF is a FreeBSD specific magic value. 704 */ 705 urb = usb_alloc_urb(0xFFFF, size); 706 if (urb == NULL) 707 return (-ENOMEM); 708 709 urb->dev = dev; 710 urb->endpoint = uhe; 711 712 bcopy(&req, urb->setup_packet, sizeof(req)); 713 714 if (size && (!(req.bmRequestType & UT_READ))) { 715 /* move the data to a real buffer */ 716 bcopy(data, USB_ADD_BYTES(urb->setup_packet, 717 sizeof(req)), size); 718 } 719 err = usb_start_wait_urb(urb, timeout, &actlen); 720 721 if (req.bmRequestType & UT_READ) { 722 if (actlen) { 723 bcopy(USB_ADD_BYTES(urb->setup_packet, 724 sizeof(req)), data, actlen); 725 } 726 } 727 usb_free_urb(urb); 728 729 if (err == 0) { 730 err = actlen; 731 } 732 return (err); 733} 734 735/*------------------------------------------------------------------------* 736 * usb_set_interface 737 * 738 * The following function will select which alternate setting of an 739 * USB interface you plan to use. By default alternate setting with 740 * index zero is selected. Note that "iface_no" is not the interface 741 * index, but rather the value of "bInterfaceNumber". 742 *------------------------------------------------------------------------*/ 743int 744usb_set_interface(struct usb_device *dev, uint8_t iface_no, uint8_t alt_index) 745{ 746 struct usb_interface *p_ui = usb_ifnum_to_if(dev, iface_no); 747 int err; 748 749 if (p_ui == NULL) 750 return (-EINVAL); 751 if (alt_index >= p_ui->num_altsetting) 752 return (-EINVAL); 753 usb_linux_cleanup_interface(dev, p_ui); 754 err = -usbd_set_alt_interface_index(dev, 755 p_ui->bsd_iface_index, alt_index); 756 if (err == 0) { 757 p_ui->cur_altsetting = p_ui->altsetting + alt_index; 758 } 759 return (err); 760} 761 762/*------------------------------------------------------------------------* 763 * usb_setup_endpoint 764 * 765 * The following function is an extension to the Linux USB API that 766 * allows you to set a maximum buffer size for a given USB endpoint. 767 * The maximum buffer size is per URB. If you don't call this function 768 * to set a maximum buffer size, the endpoint will not be functional. 769 * Note that for isochronous endpoints the maximum buffer size must be 770 * a non-zero dummy, hence this function will base the maximum buffer 771 * size on "wMaxPacketSize". 772 *------------------------------------------------------------------------*/ 773int 774usb_setup_endpoint(struct usb_device *dev, 775 struct usb_host_endpoint *uhe, usb_size_t bufsize) 776{ 777 struct usb_config cfg[2]; 778 uint8_t type = uhe->desc.bmAttributes & UE_XFERTYPE; 779 uint8_t addr = uhe->desc.bEndpointAddress; 780 781 if (uhe->fbsd_buf_size == bufsize) { 782 /* optimize */ 783 return (0); 784 } 785 usbd_transfer_unsetup(uhe->bsd_xfer, 2); 786 787 uhe->fbsd_buf_size = bufsize; 788 789 if (bufsize == 0) { 790 return (0); 791 } 792 bzero(cfg, sizeof(cfg)); 793 794 if (type == UE_ISOCHRONOUS) { 795 796 /* 797 * Isochronous transfers are special in that they don't fit 798 * into the BULK/INTR/CONTROL transfer model. 799 */ 800 801 cfg[0].type = type; 802 cfg[0].endpoint = addr & UE_ADDR; 803 cfg[0].direction = addr & (UE_DIR_OUT | UE_DIR_IN); 804 cfg[0].callback = &usb_linux_isoc_callback; 805 cfg[0].bufsize = 0; /* use wMaxPacketSize */ 806 cfg[0].frames = usb_max_isoc_frames(dev); 807 cfg[0].flags.proxy_buffer = 1; 808#if 0 809 /* 810 * The Linux USB API allows non back-to-back 811 * isochronous frames which we do not support. If the 812 * isochronous frames are not back-to-back we need to 813 * do a copy, and then we need a buffer for 814 * that. Enable this at your own risk. 815 */ 816 cfg[0].flags.ext_buffer = 1; 817#endif 818 cfg[0].flags.short_xfer_ok = 1; 819 820 bcopy(cfg, cfg + 1, sizeof(*cfg)); 821 822 /* Allocate and setup two generic FreeBSD USB transfers */ 823 824 if (usbd_transfer_setup(dev, &uhe->bsd_iface_index, 825 uhe->bsd_xfer, cfg, 2, uhe, &Giant)) { 826 return (-EINVAL); 827 } 828 } else { 829 if (bufsize > (1 << 22)) { 830 /* limit buffer size */ 831 bufsize = (1 << 22); 832 } 833 /* Allocate and setup one generic FreeBSD USB transfer */ 834 835 cfg[0].type = type; 836 cfg[0].endpoint = addr & UE_ADDR; 837 cfg[0].direction = addr & (UE_DIR_OUT | UE_DIR_IN); 838 cfg[0].callback = &usb_linux_non_isoc_callback; 839 cfg[0].bufsize = bufsize; 840 cfg[0].flags.ext_buffer = 1; /* enable zero-copy */ 841 cfg[0].flags.proxy_buffer = 1; 842 cfg[0].flags.short_xfer_ok = 1; 843 844 if (usbd_transfer_setup(dev, &uhe->bsd_iface_index, 845 uhe->bsd_xfer, cfg, 1, uhe, &Giant)) { 846 return (-EINVAL); 847 } 848 } 849 return (0); 850} 851 852/*------------------------------------------------------------------------* 853 * usb_linux_create_usb_device 854 * 855 * The following function is used to build up a per USB device 856 * structure tree, that mimics the Linux one. The root structure 857 * is returned by this function. 858 *------------------------------------------------------------------------*/ 859static int 860usb_linux_create_usb_device(struct usb_device *udev, device_t dev) 861{ 862 struct usb_config_descriptor *cd = usbd_get_config_descriptor(udev); 863 struct usb_descriptor *desc; 864 struct usb_interface_descriptor *id; 865 struct usb_endpoint_descriptor *ed; 866 struct usb_interface *p_ui = NULL; 867 struct usb_host_interface *p_uhi = NULL; 868 struct usb_host_endpoint *p_uhe = NULL; 869 usb_size_t size; 870 uint16_t niface_total; 871 uint16_t nedesc; 872 uint16_t iface_no_curr; 873 uint16_t iface_index; 874 uint8_t pass; 875 uint8_t iface_no; 876 877 /* 878 * We do two passes. One pass for computing necessary memory size 879 * and one pass to initialize all the allocated memory structures. 880 */ 881 for (pass = 0; pass < 2; pass++) { 882 883 iface_no_curr = 0 - 1; 884 niface_total = 0; 885 iface_index = 0; 886 nedesc = 0; 887 desc = NULL; 888 889 /* 890 * Iterate over all the USB descriptors. Use the USB config 891 * descriptor pointer provided by the FreeBSD USB stack. 892 */ 893 while ((desc = usb_desc_foreach(cd, desc))) { 894 895 /* 896 * Build up a tree according to the descriptors we 897 * find: 898 */ 899 switch (desc->bDescriptorType) { 900 case UDESC_DEVICE: 901 break; 902 903 case UDESC_ENDPOINT: 904 ed = (void *)desc; 905 if ((ed->bLength < sizeof(*ed)) || 906 (iface_index == 0)) 907 break; 908 if (p_uhe) { 909 bcopy(ed, &p_uhe->desc, sizeof(p_uhe->desc)); 910 p_uhe->bsd_iface_index = iface_index - 1; 911 TAILQ_INIT(&p_uhe->bsd_urb_list); 912 p_uhe++; 913 } 914 if (p_uhi) { 915 (p_uhi - 1)->desc.bNumEndpoints++; 916 } 917 nedesc++; 918 break; 919 920 case UDESC_INTERFACE: 921 id = (void *)desc; 922 if (id->bLength < sizeof(*id)) 923 break; 924 if (p_uhi) { 925 bcopy(id, &p_uhi->desc, sizeof(p_uhi->desc)); 926 p_uhi->desc.bNumEndpoints = 0; 927 p_uhi->endpoint = p_uhe; 928 p_uhi->string = ""; 929 p_uhi->bsd_iface_index = iface_index; 930 p_uhi++; 931 } 932 iface_no = id->bInterfaceNumber; 933 niface_total++; 934 if (iface_no_curr != iface_no) { 935 if (p_ui) { 936 p_ui->altsetting = p_uhi - 1; 937 p_ui->cur_altsetting = p_uhi - 1; 938 p_ui->num_altsetting = 1; 939 p_ui->bsd_iface_index = iface_index; 940 p_ui->linux_udev = udev; 941 p_ui++; 942 } 943 iface_no_curr = iface_no; 944 iface_index++; 945 } else { 946 if (p_ui) { 947 (p_ui - 1)->num_altsetting++; 948 } 949 } 950 break; 951 952 default: 953 break; 954 } 955 } 956 957 if (pass == 0) { 958 959 size = (sizeof(*p_uhe) * nedesc) + 960 (sizeof(*p_ui) * iface_index) + 961 (sizeof(*p_uhi) * niface_total); 962 963 p_uhe = malloc(size, M_USBDEV, M_WAITOK | M_ZERO); 964 p_ui = (void *)(p_uhe + nedesc); 965 p_uhi = (void *)(p_ui + iface_index); 966 967 udev->linux_iface_start = p_ui; 968 udev->linux_iface_end = p_ui + iface_index; 969 udev->linux_endpoint_start = p_uhe; 970 udev->linux_endpoint_end = p_uhe + nedesc; 971 udev->devnum = device_get_unit(dev); 972 bcopy(&udev->ddesc, &udev->descriptor, 973 sizeof(udev->descriptor)); 974 bcopy(udev->ctrl_ep.edesc, &udev->ep0.desc, 975 sizeof(udev->ep0.desc)); 976 } 977 } 978 return (0); 979} 980 981/*------------------------------------------------------------------------* 982 * usb_alloc_urb 983 * 984 * This function should always be used when you allocate an URB for 985 * use with the USB Linux stack. In case of an isochronous transfer 986 * you must specifiy the maximum number of "iso_packets" which you 987 * plan to transfer per URB. This function is always blocking, and 988 * "mem_flags" are not regarded like on Linux. 989 *------------------------------------------------------------------------*/ 990struct urb * 991usb_alloc_urb(uint16_t iso_packets, uint16_t mem_flags) 992{ 993 struct urb *urb; 994 usb_size_t size; 995 996 if (iso_packets == 0xFFFF) { 997 /* 998 * FreeBSD specific magic value to ask for control transfer 999 * memory allocation: 1000 */ 1001 size = sizeof(*urb) + sizeof(struct usb_device_request) + mem_flags; 1002 } else { 1003 size = sizeof(*urb) + (iso_packets * sizeof(urb->iso_frame_desc[0])); 1004 } 1005 1006 urb = malloc(size, M_USBDEV, M_WAITOK | M_ZERO); 1007 if (urb) { 1008 1009 cv_init(&urb->cv_wait, "URBWAIT"); 1010 if (iso_packets == 0xFFFF) { 1011 urb->setup_packet = (void *)(urb + 1); 1012 urb->transfer_buffer = (void *)(urb->setup_packet + 1013 sizeof(struct usb_device_request)); 1014 } else { 1015 urb->number_of_packets = iso_packets; 1016 } 1017 } 1018 return (urb); 1019} 1020 1021/*------------------------------------------------------------------------* 1022 * usb_find_host_endpoint 1023 * 1024 * The following function will return the Linux USB host endpoint 1025 * structure that matches the given endpoint type and endpoint 1026 * value. If no match is found, NULL is returned. This function is not 1027 * part of the Linux USB API and is only used internally. 1028 *------------------------------------------------------------------------*/ 1029struct usb_host_endpoint * 1030usb_find_host_endpoint(struct usb_device *dev, uint8_t type, uint8_t ep) 1031{ 1032 struct usb_host_endpoint *uhe; 1033 struct usb_host_endpoint *uhe_end; 1034 struct usb_host_interface *uhi; 1035 struct usb_interface *ui; 1036 uint8_t ea; 1037 uint8_t at; 1038 uint8_t mask; 1039 1040 if (dev == NULL) { 1041 return (NULL); 1042 } 1043 if (type == UE_CONTROL) { 1044 mask = UE_ADDR; 1045 } else { 1046 mask = (UE_DIR_IN | UE_DIR_OUT | UE_ADDR); 1047 } 1048 1049 ep &= mask; 1050 1051 /* 1052 * Iterate over all the interfaces searching the selected alternate 1053 * setting only, and all belonging endpoints. 1054 */ 1055 for (ui = dev->linux_iface_start; 1056 ui != dev->linux_iface_end; 1057 ui++) { 1058 uhi = ui->cur_altsetting; 1059 if (uhi) { 1060 uhe_end = uhi->endpoint + uhi->desc.bNumEndpoints; 1061 for (uhe = uhi->endpoint; 1062 uhe != uhe_end; 1063 uhe++) { 1064 ea = uhe->desc.bEndpointAddress; 1065 at = uhe->desc.bmAttributes; 1066 1067 if (((ea & mask) == ep) && 1068 ((at & UE_XFERTYPE) == type)) { 1069 return (uhe); 1070 } 1071 } 1072 } 1073 } 1074 1075 if ((type == UE_CONTROL) && ((ep & UE_ADDR) == 0)) { 1076 return (&dev->ep0); 1077 } 1078 return (NULL); 1079} 1080 1081/*------------------------------------------------------------------------* 1082 * usb_altnum_to_altsetting 1083 * 1084 * The following function returns a pointer to an alternate setting by 1085 * index given a "usb_interface" pointer. If the alternate setting by 1086 * index does not exist, NULL is returned. And alternate setting is a 1087 * variant of an interface, but usually with slightly different 1088 * characteristics. 1089 *------------------------------------------------------------------------*/ 1090struct usb_host_interface * 1091usb_altnum_to_altsetting(const struct usb_interface *intf, uint8_t alt_index) 1092{ 1093 if (alt_index >= intf->num_altsetting) { 1094 return (NULL); 1095 } 1096 return (intf->altsetting + alt_index); 1097} 1098 1099/*------------------------------------------------------------------------* 1100 * usb_ifnum_to_if 1101 * 1102 * The following function searches up an USB interface by 1103 * "bInterfaceNumber". If no match is found, NULL is returned. 1104 *------------------------------------------------------------------------*/ 1105struct usb_interface * 1106usb_ifnum_to_if(struct usb_device *dev, uint8_t iface_no) 1107{ 1108 struct usb_interface *p_ui; 1109 1110 for (p_ui = dev->linux_iface_start; 1111 p_ui != dev->linux_iface_end; 1112 p_ui++) { 1113 if ((p_ui->num_altsetting > 0) && 1114 (p_ui->altsetting->desc.bInterfaceNumber == iface_no)) { 1115 return (p_ui); 1116 } 1117 } 1118 return (NULL); 1119} 1120 1121/*------------------------------------------------------------------------* 1122 * usb_buffer_alloc 1123 *------------------------------------------------------------------------*/ 1124void * 1125usb_buffer_alloc(struct usb_device *dev, usb_size_t size, uint16_t mem_flags, uint8_t *dma_addr) 1126{ 1127 return (malloc(size, M_USBDEV, M_WAITOK | M_ZERO)); 1128} 1129 1130/*------------------------------------------------------------------------* 1131 * usbd_get_intfdata 1132 *------------------------------------------------------------------------*/ 1133void * 1134usbd_get_intfdata(struct usb_interface *intf) 1135{ 1136 return (intf->bsd_priv_sc); 1137} 1138 1139/*------------------------------------------------------------------------* 1140 * usb_linux_register 1141 * 1142 * The following function is used by the "USB_DRIVER_EXPORT()" macro, 1143 * and is used to register a Linux USB driver, so that its 1144 * "usb_device_id" structures gets searched a probe time. This 1145 * function is not part of the Linux USB API, and is for internal use 1146 * only. 1147 *------------------------------------------------------------------------*/ 1148void 1149usb_linux_register(void *arg) 1150{ 1151 struct usb_driver *drv = arg; 1152 1153 mtx_lock(&Giant); 1154 LIST_INSERT_HEAD(&usb_linux_driver_list, drv, linux_driver_list); 1155 mtx_unlock(&Giant); 1156 1157 usb_needs_explore_all(); 1158} 1159 1160/*------------------------------------------------------------------------* 1161 * usb_linux_deregister 1162 * 1163 * The following function is used by the "USB_DRIVER_EXPORT()" macro, 1164 * and is used to deregister a Linux USB driver. This function will 1165 * ensure that all driver instances belonging to the Linux USB device 1166 * driver in question, gets detached before the driver is 1167 * unloaded. This function is not part of the Linux USB API, and is 1168 * for internal use only. 1169 *------------------------------------------------------------------------*/ 1170void 1171usb_linux_deregister(void *arg) 1172{ 1173 struct usb_driver *drv = arg; 1174 struct usb_linux_softc *sc; 1175 1176repeat: 1177 mtx_lock(&Giant); 1178 LIST_FOREACH(sc, &usb_linux_attached_list, sc_attached_list) { 1179 if (sc->sc_udrv == drv) { 1180 mtx_unlock(&Giant); 1181 device_detach(sc->sc_fbsd_dev); 1182 goto repeat; 1183 } 1184 } 1185 LIST_REMOVE(drv, linux_driver_list); 1186 mtx_unlock(&Giant); 1187} 1188 1189/*------------------------------------------------------------------------* 1190 * usb_linux_free_device 1191 * 1192 * The following function is only used by the FreeBSD USB stack, to 1193 * cleanup and free memory after that a Linux USB device was attached. 1194 *------------------------------------------------------------------------*/ 1195void 1196usb_linux_free_device(struct usb_device *dev) 1197{ 1198 struct usb_host_endpoint *uhe; 1199 struct usb_host_endpoint *uhe_end; 1200 int err; 1201 1202 uhe = dev->linux_endpoint_start; 1203 uhe_end = dev->linux_endpoint_end; 1204 while (uhe != uhe_end) { 1205 err = usb_setup_endpoint(dev, uhe, 0); 1206 uhe++; 1207 } 1208 err = usb_setup_endpoint(dev, &dev->ep0, 0); 1209 free(dev->linux_endpoint_start, M_USBDEV); 1210} 1211 1212/*------------------------------------------------------------------------* 1213 * usb_buffer_free 1214 *------------------------------------------------------------------------*/ 1215void 1216usb_buffer_free(struct usb_device *dev, usb_size_t size, 1217 void *addr, uint8_t dma_addr) 1218{ 1219 free(addr, M_USBDEV); 1220} 1221 1222/*------------------------------------------------------------------------* 1223 * usb_free_urb 1224 *------------------------------------------------------------------------*/ 1225void 1226usb_free_urb(struct urb *urb) 1227{ 1228 if (urb == NULL) { 1229 return; 1230 } 1231 /* make sure that the current URB is not active */ 1232 usb_kill_urb(urb); 1233 1234 /* destroy condition variable */ 1235 cv_destroy(&urb->cv_wait); 1236 1237 /* just free it */ 1238 free(urb, M_USBDEV); 1239} 1240 1241/*------------------------------------------------------------------------* 1242 * usb_init_urb 1243 * 1244 * The following function can be used to initialize a custom URB. It 1245 * is not recommended to use this function. Use "usb_alloc_urb()" 1246 * instead. 1247 *------------------------------------------------------------------------*/ 1248void 1249usb_init_urb(struct urb *urb) 1250{ 1251 if (urb == NULL) { 1252 return; 1253 } 1254 bzero(urb, sizeof(*urb)); 1255} 1256 1257/*------------------------------------------------------------------------* 1258 * usb_kill_urb 1259 *------------------------------------------------------------------------*/ 1260void 1261usb_kill_urb(struct urb *urb) 1262{ 1263 usb_unlink_urb_sub(urb, 1); 1264} 1265 1266/*------------------------------------------------------------------------* 1267 * usb_set_intfdata 1268 * 1269 * The following function sets the per Linux USB interface private 1270 * data pointer. It is used by most Linux USB device drivers. 1271 *------------------------------------------------------------------------*/ 1272void 1273usb_set_intfdata(struct usb_interface *intf, void *data) 1274{ 1275 intf->bsd_priv_sc = data; 1276} 1277 1278/*------------------------------------------------------------------------* 1279 * usb_linux_cleanup_interface 1280 * 1281 * The following function will release all FreeBSD USB transfers 1282 * associated with a Linux USB interface. It is for internal use only. 1283 *------------------------------------------------------------------------*/ 1284static void 1285usb_linux_cleanup_interface(struct usb_device *dev, struct usb_interface *iface) 1286{ 1287 struct usb_host_interface *uhi; 1288 struct usb_host_interface *uhi_end; 1289 struct usb_host_endpoint *uhe; 1290 struct usb_host_endpoint *uhe_end; 1291 int err; 1292 1293 uhi = iface->altsetting; 1294 uhi_end = iface->altsetting + iface->num_altsetting; 1295 while (uhi != uhi_end) { 1296 uhe = uhi->endpoint; 1297 uhe_end = uhi->endpoint + uhi->desc.bNumEndpoints; 1298 while (uhe != uhe_end) { 1299 err = usb_setup_endpoint(dev, uhe, 0); 1300 uhe++; 1301 } 1302 uhi++; 1303 } 1304} 1305 1306/*------------------------------------------------------------------------* 1307 * usb_linux_wait_complete 1308 * 1309 * The following function is used by "usb_start_wait_urb()" to wake it 1310 * up, when an USB transfer has finished. 1311 *------------------------------------------------------------------------*/ 1312static void 1313usb_linux_wait_complete(struct urb *urb) 1314{ 1315 if (urb->transfer_flags & URB_IS_SLEEPING) { 1316 cv_signal(&urb->cv_wait); 1317 } 1318 urb->transfer_flags &= ~URB_WAIT_WAKEUP; 1319} 1320 1321/*------------------------------------------------------------------------* 1322 * usb_linux_complete 1323 *------------------------------------------------------------------------*/ 1324static void 1325usb_linux_complete(struct usb_xfer *xfer) 1326{ 1327 struct urb *urb; 1328 1329 urb = usbd_xfer_get_priv(xfer); 1330 usbd_xfer_set_priv(xfer, NULL); 1331 if (urb->complete) { 1332 (urb->complete) (urb); 1333 } 1334} 1335 1336/*------------------------------------------------------------------------* 1337 * usb_linux_isoc_callback 1338 * 1339 * The following is the FreeBSD isochronous USB callback. Isochronous 1340 * frames are USB packets transferred 1000 or 8000 times per second, 1341 * depending on whether a full- or high- speed USB transfer is 1342 * used. 1343 *------------------------------------------------------------------------*/ 1344static void 1345usb_linux_isoc_callback(struct usb_xfer *xfer, usb_error_t error) 1346{ 1347 usb_frlength_t max_frame = xfer->max_frame_size; 1348 usb_frlength_t offset; 1349 usb_frcount_t x; 1350 struct urb *urb = usbd_xfer_get_priv(xfer); 1351 struct usb_host_endpoint *uhe = usbd_xfer_softc(xfer); 1352 struct usb_iso_packet_descriptor *uipd; 1353 1354 DPRINTF("\n"); 1355 1356 switch (USB_GET_STATE(xfer)) { 1357 case USB_ST_TRANSFERRED: 1358 1359 if (urb->bsd_isread) { 1360 1361 /* copy in data with regard to the URB */ 1362 1363 offset = 0; 1364 1365 for (x = 0; x < urb->number_of_packets; x++) { 1366 uipd = urb->iso_frame_desc + x; 1367 if (uipd->length > xfer->frlengths[x]) { 1368 if (urb->transfer_flags & URB_SHORT_NOT_OK) { 1369 /* XXX should be EREMOTEIO */ 1370 uipd->status = -EPIPE; 1371 } else { 1372 uipd->status = 0; 1373 } 1374 } else { 1375 uipd->status = 0; 1376 } 1377 uipd->actual_length = xfer->frlengths[x]; 1378 if (!xfer->flags.ext_buffer) { 1379 usbd_copy_out(xfer->frbuffers, offset, 1380 USB_ADD_BYTES(urb->transfer_buffer, 1381 uipd->offset), uipd->actual_length); 1382 } 1383 offset += max_frame; 1384 } 1385 } else { 1386 for (x = 0; x < urb->number_of_packets; x++) { 1387 uipd = urb->iso_frame_desc + x; 1388 uipd->actual_length = xfer->frlengths[x]; 1389 uipd->status = 0; 1390 } 1391 } 1392 1393 urb->actual_length = xfer->actlen; 1394 1395 /* check for short transfer */ 1396 if (xfer->actlen < xfer->sumlen) { 1397 /* short transfer */ 1398 if (urb->transfer_flags & URB_SHORT_NOT_OK) { 1399 /* XXX should be EREMOTEIO */ 1400 urb->status = -EPIPE; 1401 } else { 1402 urb->status = 0; 1403 } 1404 } else { 1405 /* success */ 1406 urb->status = 0; 1407 } 1408 1409 /* call callback */ 1410 usb_linux_complete(xfer); 1411 1412 case USB_ST_SETUP: 1413tr_setup: 1414 1415 if (xfer->priv_fifo == NULL) { 1416 1417 /* get next transfer */ 1418 urb = TAILQ_FIRST(&uhe->bsd_urb_list); 1419 if (urb == NULL) { 1420 /* nothing to do */ 1421 return; 1422 } 1423 TAILQ_REMOVE(&uhe->bsd_urb_list, urb, bsd_urb_list); 1424 urb->bsd_urb_list.tqe_prev = NULL; 1425 1426 x = xfer->max_frame_count; 1427 if (urb->number_of_packets > x) { 1428 /* XXX simply truncate the transfer */ 1429 urb->number_of_packets = x; 1430 } 1431 } else { 1432 DPRINTF("Already got a transfer\n"); 1433 1434 /* already got a transfer (should not happen) */ 1435 urb = usbd_xfer_get_priv(xfer); 1436 } 1437 1438 urb->bsd_isread = (uhe->desc.bEndpointAddress & UE_DIR_IN) ? 1 : 0; 1439 1440 if (xfer->flags.ext_buffer) { 1441 /* set virtual address to load */ 1442 usbd_xfer_set_frame_data(xfer, 0, urb->transfer_buffer, 0); 1443 } 1444 if (!(urb->bsd_isread)) { 1445 1446 /* copy out data with regard to the URB */ 1447 1448 offset = 0; 1449 1450 for (x = 0; x < urb->number_of_packets; x++) { 1451 uipd = urb->iso_frame_desc + x; 1452 usbd_xfer_set_frame_len(xfer, x, uipd->length); 1453 if (!xfer->flags.ext_buffer) { 1454 usbd_copy_in(xfer->frbuffers, offset, 1455 USB_ADD_BYTES(urb->transfer_buffer, 1456 uipd->offset), uipd->length); 1457 } 1458 offset += uipd->length; 1459 } 1460 } else { 1461 1462 /* 1463 * compute the transfer length into the "offset" 1464 * variable 1465 */ 1466 1467 offset = urb->number_of_packets * max_frame; 1468 1469 /* setup "frlengths" array */ 1470 1471 for (x = 0; x < urb->number_of_packets; x++) { 1472 uipd = urb->iso_frame_desc + x; 1473 usbd_xfer_set_frame_len(xfer, x, max_frame); 1474 } 1475 } 1476 usbd_xfer_set_priv(xfer, urb); 1477 xfer->flags.force_short_xfer = 0; 1478 xfer->timeout = urb->timeout; 1479 xfer->nframes = urb->number_of_packets; 1480 usbd_transfer_submit(xfer); 1481 return; 1482 1483 default: /* Error */ 1484 if (xfer->error == USB_ERR_CANCELLED) { 1485 urb->status = -ECONNRESET; 1486 } else { 1487 urb->status = -EPIPE; /* stalled */ 1488 } 1489 1490 /* Set zero for "actual_length" */ 1491 urb->actual_length = 0; 1492 1493 /* Set zero for "actual_length" */ 1494 for (x = 0; x < urb->number_of_packets; x++) { 1495 urb->iso_frame_desc[x].actual_length = 0; 1496 urb->iso_frame_desc[x].status = urb->status; 1497 } 1498 1499 /* call callback */ 1500 usb_linux_complete(xfer); 1501 1502 if (xfer->error == USB_ERR_CANCELLED) { 1503 /* we need to return in this case */ 1504 return; 1505 } 1506 goto tr_setup; 1507 1508 } 1509} 1510 1511/*------------------------------------------------------------------------* 1512 * usb_linux_non_isoc_callback 1513 * 1514 * The following is the FreeBSD BULK/INTERRUPT and CONTROL USB 1515 * callback. It dequeues Linux USB stack compatible URB's, transforms 1516 * the URB fields into a FreeBSD USB transfer, and defragments the USB 1517 * transfer as required. When the transfer is complete the "complete" 1518 * callback is called. 1519 *------------------------------------------------------------------------*/ 1520static void 1521usb_linux_non_isoc_callback(struct usb_xfer *xfer, usb_error_t error) 1522{ 1523 enum { 1524 REQ_SIZE = sizeof(struct usb_device_request) 1525 }; 1526 struct urb *urb = usbd_xfer_get_priv(xfer); 1527 struct usb_host_endpoint *uhe = usbd_xfer_softc(xfer); 1528 uint8_t *ptr; 1529 usb_frlength_t max_bulk = usbd_xfer_max_len(xfer); 1530 uint8_t data_frame = xfer->flags_int.control_xfr ? 1 : 0; 1531 1532 DPRINTF("\n"); 1533 1534 switch (USB_GET_STATE(xfer)) { 1535 case USB_ST_TRANSFERRED: 1536 1537 if (xfer->flags_int.control_xfr) { 1538 1539 /* don't transfer the setup packet again: */ 1540 1541 usbd_xfer_set_frame_len(xfer, 0, 0); 1542 } 1543 if (urb->bsd_isread && (!xfer->flags.ext_buffer)) { 1544 /* copy in data with regard to the URB */ 1545 usbd_copy_out(xfer->frbuffers + data_frame, 0, 1546 urb->bsd_data_ptr, xfer->frlengths[data_frame]); 1547 } 1548 urb->bsd_length_rem -= xfer->frlengths[data_frame]; 1549 urb->bsd_data_ptr += xfer->frlengths[data_frame]; 1550 urb->actual_length += xfer->frlengths[data_frame]; 1551 1552 /* check for short transfer */ 1553 if (xfer->actlen < xfer->sumlen) { 1554 urb->bsd_length_rem = 0; 1555 1556 /* short transfer */ 1557 if (urb->transfer_flags & URB_SHORT_NOT_OK) { 1558 urb->status = -EPIPE; 1559 } else { 1560 urb->status = 0; 1561 } 1562 } else { 1563 /* check remainder */ 1564 if (urb->bsd_length_rem > 0) { 1565 goto setup_bulk; 1566 } 1567 /* success */ 1568 urb->status = 0; 1569 } 1570 1571 /* call callback */ 1572 usb_linux_complete(xfer); 1573 1574 case USB_ST_SETUP: 1575tr_setup: 1576 /* get next transfer */ 1577 urb = TAILQ_FIRST(&uhe->bsd_urb_list); 1578 if (urb == NULL) { 1579 /* nothing to do */ 1580 return; 1581 } 1582 TAILQ_REMOVE(&uhe->bsd_urb_list, urb, bsd_urb_list); 1583 urb->bsd_urb_list.tqe_prev = NULL; 1584 1585 usbd_xfer_set_priv(xfer, urb); 1586 xfer->flags.force_short_xfer = 0; 1587 xfer->timeout = urb->timeout; 1588 1589 if (xfer->flags_int.control_xfr) { 1590 1591 /* 1592 * USB control transfers need special handling. 1593 * First copy in the header, then copy in data! 1594 */ 1595 if (!xfer->flags.ext_buffer) { 1596 usbd_copy_in(xfer->frbuffers, 0, 1597 urb->setup_packet, REQ_SIZE); 1598 usbd_xfer_set_frame_len(xfer, 0, REQ_SIZE); 1599 } else { 1600 /* set virtual address to load */ 1601 usbd_xfer_set_frame_data(xfer, 0, 1602 urb->setup_packet, REQ_SIZE); 1603 } 1604 1605 ptr = urb->setup_packet; 1606 1607 /* setup data transfer direction and length */ 1608 urb->bsd_isread = (ptr[0] & UT_READ) ? 1 : 0; 1609 urb->bsd_length_rem = ptr[6] | (ptr[7] << 8); 1610 1611 } else { 1612 1613 /* setup data transfer direction */ 1614 1615 urb->bsd_length_rem = urb->transfer_buffer_length; 1616 urb->bsd_isread = (uhe->desc.bEndpointAddress & 1617 UE_DIR_IN) ? 1 : 0; 1618 } 1619 1620 urb->bsd_data_ptr = urb->transfer_buffer; 1621 urb->actual_length = 0; 1622 1623setup_bulk: 1624 if (max_bulk > urb->bsd_length_rem) { 1625 max_bulk = urb->bsd_length_rem; 1626 } 1627 /* check if we need to force a short transfer */ 1628 1629 if ((max_bulk == urb->bsd_length_rem) && 1630 (urb->transfer_flags & URB_ZERO_PACKET) && 1631 (!xfer->flags_int.control_xfr)) { 1632 xfer->flags.force_short_xfer = 1; 1633 } 1634 /* check if we need to copy in data */ 1635 1636 if (xfer->flags.ext_buffer) { 1637 /* set virtual address to load */ 1638 usbd_xfer_set_frame_data(xfer, data_frame, 1639 urb->bsd_data_ptr, max_bulk); 1640 } else if (!urb->bsd_isread) { 1641 /* copy out data with regard to the URB */ 1642 usbd_copy_in(xfer->frbuffers + data_frame, 0, 1643 urb->bsd_data_ptr, max_bulk); 1644 usbd_xfer_set_frame_len(xfer, data_frame, max_bulk); 1645 } 1646 if (xfer->flags_int.control_xfr) { 1647 if (max_bulk > 0) { 1648 xfer->nframes = 2; 1649 } else { 1650 xfer->nframes = 1; 1651 } 1652 } else { 1653 xfer->nframes = 1; 1654 } 1655 usbd_transfer_submit(xfer); 1656 return; 1657 1658 default: 1659 if (xfer->error == USB_ERR_CANCELLED) { 1660 urb->status = -ECONNRESET; 1661 } else { 1662 urb->status = -EPIPE; 1663 } 1664 1665 /* Set zero for "actual_length" */ 1666 urb->actual_length = 0; 1667 1668 /* call callback */ 1669 usb_linux_complete(xfer); 1670 1671 if (xfer->error == USB_ERR_CANCELLED) { 1672 /* we need to return in this case */ 1673 return; 1674 } 1675 goto tr_setup; 1676 } 1677} 1678 1679/*------------------------------------------------------------------------* 1680 * usb_fill_bulk_urb 1681 *------------------------------------------------------------------------*/ 1682void 1683usb_fill_bulk_urb(struct urb *urb, struct usb_device *udev, 1684 struct usb_host_endpoint *uhe, void *buf, 1685 int length, usb_complete_t callback, void *arg) 1686{ 1687 urb->dev = udev; 1688 urb->endpoint = uhe; 1689 urb->transfer_buffer = buf; 1690 urb->transfer_buffer_length = length; 1691 urb->complete = callback; 1692 urb->context = arg; 1693} 1694 1695/*------------------------------------------------------------------------* 1696 * usb_bulk_msg 1697 * 1698 * NOTE: This function can also be used for interrupt endpoints! 1699 * 1700 * Return values: 1701 * 0: Success 1702 * Else: Failure 1703 *------------------------------------------------------------------------*/ 1704int 1705usb_bulk_msg(struct usb_device *udev, struct usb_host_endpoint *uhe, 1706 void *data, int len, uint16_t *pactlen, usb_timeout_t timeout) 1707{ 1708 struct urb *urb; 1709 int err; 1710 1711 if (uhe == NULL) 1712 return (-EINVAL); 1713 if (len < 0) 1714 return (-EINVAL); 1715 1716 err = usb_setup_endpoint(udev, uhe, 4096 /* bytes */); 1717 if (err) 1718 return (err); 1719 1720 urb = usb_alloc_urb(0, 0); 1721 if (urb == NULL) 1722 return (-ENOMEM); 1723 1724 usb_fill_bulk_urb(urb, udev, uhe, data, len, 1725 usb_linux_wait_complete, NULL); 1726 1727 err = usb_start_wait_urb(urb, timeout, pactlen); 1728 1729 usb_free_urb(urb); 1730 1731 return (err); 1732}
| 126 127/*------------------------------------------------------------------------* 128 * usb_linux_lookup_id 129 * 130 * This functions takes an array of "struct usb_device_id" and tries 131 * to match the entries with the information in "struct usb_attach_arg". 132 * If it finds a match the matching entry will be returned. 133 * Else "NULL" will be returned. 134 *------------------------------------------------------------------------*/ 135static const struct usb_device_id * 136usb_linux_lookup_id(const struct usb_device_id *id, struct usb_attach_arg *uaa) 137{ 138 if (id == NULL) { 139 goto done; 140 } 141 /* 142 * Keep on matching array entries until we find one with 143 * "match_flags" equal to zero, which indicates the end of the 144 * array: 145 */ 146 for (; id->match_flags; id++) { 147 148 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) && 149 (id->idVendor != uaa->info.idVendor)) { 150 continue; 151 } 152 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) && 153 (id->idProduct != uaa->info.idProduct)) { 154 continue; 155 } 156 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) && 157 (id->bcdDevice_lo > uaa->info.bcdDevice)) { 158 continue; 159 } 160 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) && 161 (id->bcdDevice_hi < uaa->info.bcdDevice)) { 162 continue; 163 } 164 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) && 165 (id->bDeviceClass != uaa->info.bDeviceClass)) { 166 continue; 167 } 168 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) && 169 (id->bDeviceSubClass != uaa->info.bDeviceSubClass)) { 170 continue; 171 } 172 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) && 173 (id->bDeviceProtocol != uaa->info.bDeviceProtocol)) { 174 continue; 175 } 176 if ((uaa->info.bDeviceClass == 0xFF) && 177 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) && 178 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS | 179 USB_DEVICE_ID_MATCH_INT_SUBCLASS | 180 USB_DEVICE_ID_MATCH_INT_PROTOCOL))) { 181 continue; 182 } 183 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) && 184 (id->bInterfaceClass != uaa->info.bInterfaceClass)) { 185 continue; 186 } 187 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) && 188 (id->bInterfaceSubClass != uaa->info.bInterfaceSubClass)) { 189 continue; 190 } 191 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) && 192 (id->bInterfaceProtocol != uaa->info.bInterfaceProtocol)) { 193 continue; 194 } 195 /* we found a match! */ 196 return (id); 197 } 198 199done: 200 return (NULL); 201} 202 203/*------------------------------------------------------------------------* 204 * usb_linux_probe 205 * 206 * This function is the FreeBSD probe callback. It is called from the 207 * FreeBSD USB stack through the "device_probe_and_attach()" function. 208 *------------------------------------------------------------------------*/ 209static int 210usb_linux_probe(device_t dev) 211{ 212 struct usb_attach_arg *uaa = device_get_ivars(dev); 213 struct usb_driver *udrv; 214 int err = ENXIO; 215 216 if (uaa->usb_mode != USB_MODE_HOST) { 217 return (ENXIO); 218 } 219 mtx_lock(&Giant); 220 LIST_FOREACH(udrv, &usb_linux_driver_list, linux_driver_list) { 221 if (usb_linux_lookup_id(udrv->id_table, uaa)) { 222 err = 0; 223 break; 224 } 225 } 226 mtx_unlock(&Giant); 227 228 return (err); 229} 230 231/*------------------------------------------------------------------------* 232 * usb_linux_get_usb_driver 233 * 234 * This function returns the pointer to the "struct usb_driver" where 235 * the Linux USB device driver "struct usb_device_id" match was found. 236 * We apply a lock before reading out the pointer to avoid races. 237 *------------------------------------------------------------------------*/ 238static struct usb_driver * 239usb_linux_get_usb_driver(struct usb_linux_softc *sc) 240{ 241 struct usb_driver *udrv; 242 243 mtx_lock(&Giant); 244 udrv = sc->sc_udrv; 245 mtx_unlock(&Giant); 246 return (udrv); 247} 248 249/*------------------------------------------------------------------------* 250 * usb_linux_attach 251 * 252 * This function is the FreeBSD attach callback. It is called from the 253 * FreeBSD USB stack through the "device_probe_and_attach()" function. 254 * This function is called when "usb_linux_probe()" returns zero. 255 *------------------------------------------------------------------------*/ 256static int 257usb_linux_attach(device_t dev) 258{ 259 struct usb_attach_arg *uaa = device_get_ivars(dev); 260 struct usb_linux_softc *sc = device_get_softc(dev); 261 struct usb_driver *udrv; 262 const struct usb_device_id *id = NULL; 263 264 mtx_lock(&Giant); 265 LIST_FOREACH(udrv, &usb_linux_driver_list, linux_driver_list) { 266 id = usb_linux_lookup_id(udrv->id_table, uaa); 267 if (id) 268 break; 269 } 270 mtx_unlock(&Giant); 271 272 if (id == NULL) { 273 return (ENXIO); 274 } 275 if (usb_linux_create_usb_device(uaa->device, dev) != 0) 276 return (ENOMEM); 277 device_set_usb_desc(dev); 278 279 sc->sc_fbsd_udev = uaa->device; 280 sc->sc_fbsd_dev = dev; 281 sc->sc_udrv = udrv; 282 sc->sc_ui = usb_ifnum_to_if(uaa->device, uaa->info.bIfaceNum); 283 if (sc->sc_ui == NULL) { 284 return (EINVAL); 285 } 286 if (udrv->probe) { 287 if ((udrv->probe) (sc->sc_ui, id)) { 288 return (ENXIO); 289 } 290 } 291 mtx_lock(&Giant); 292 LIST_INSERT_HEAD(&usb_linux_attached_list, sc, sc_attached_list); 293 mtx_unlock(&Giant); 294 295 /* success */ 296 return (0); 297} 298 299/*------------------------------------------------------------------------* 300 * usb_linux_detach 301 * 302 * This function is the FreeBSD detach callback. It is called from the 303 * FreeBSD USB stack through the "device_detach()" function. 304 *------------------------------------------------------------------------*/ 305static int 306usb_linux_detach(device_t dev) 307{ 308 struct usb_linux_softc *sc = device_get_softc(dev); 309 struct usb_driver *udrv = NULL; 310 311 mtx_lock(&Giant); 312 if (sc->sc_attached_list.le_prev) { 313 LIST_REMOVE(sc, sc_attached_list); 314 sc->sc_attached_list.le_prev = NULL; 315 udrv = sc->sc_udrv; 316 sc->sc_udrv = NULL; 317 } 318 mtx_unlock(&Giant); 319 320 if (udrv && udrv->disconnect) { 321 (udrv->disconnect) (sc->sc_ui); 322 } 323 /* 324 * Make sure that we free all FreeBSD USB transfers belonging to 325 * this Linux "usb_interface", hence they will most likely not be 326 * needed any more. 327 */ 328 usb_linux_cleanup_interface(sc->sc_fbsd_udev, sc->sc_ui); 329 return (0); 330} 331 332/*------------------------------------------------------------------------* 333 * usb_linux_suspend 334 * 335 * This function is the FreeBSD suspend callback. Usually it does nothing. 336 *------------------------------------------------------------------------*/ 337static int 338usb_linux_suspend(device_t dev) 339{ 340 struct usb_linux_softc *sc = device_get_softc(dev); 341 struct usb_driver *udrv = usb_linux_get_usb_driver(sc); 342 int err; 343 344 if (udrv && udrv->suspend) { 345 err = (udrv->suspend) (sc->sc_ui, 0); 346 } 347 return (0); 348} 349 350/*------------------------------------------------------------------------* 351 * usb_linux_resume 352 * 353 * This function is the FreeBSD resume callback. Usually it does nothing. 354 *------------------------------------------------------------------------*/ 355static int 356usb_linux_resume(device_t dev) 357{ 358 struct usb_linux_softc *sc = device_get_softc(dev); 359 struct usb_driver *udrv = usb_linux_get_usb_driver(sc); 360 int err; 361 362 if (udrv && udrv->resume) { 363 err = (udrv->resume) (sc->sc_ui); 364 } 365 return (0); 366} 367 368/*------------------------------------------------------------------------* 369 * Linux emulation layer 370 *------------------------------------------------------------------------*/ 371 372/*------------------------------------------------------------------------* 373 * usb_max_isoc_frames 374 * 375 * The following function returns the maximum number of isochronous 376 * frames that we support per URB. It is not part of the Linux USB API. 377 *------------------------------------------------------------------------*/ 378static uint16_t 379usb_max_isoc_frames(struct usb_device *dev) 380{ 381 ; /* indent fix */ 382 switch (usbd_get_speed(dev)) { 383 case USB_SPEED_LOW: 384 case USB_SPEED_FULL: 385 return (USB_MAX_FULL_SPEED_ISOC_FRAMES); 386 default: 387 return (USB_MAX_HIGH_SPEED_ISOC_FRAMES); 388 } 389} 390 391/*------------------------------------------------------------------------* 392 * usb_submit_urb 393 * 394 * This function is used to queue an URB after that it has been 395 * initialized. If it returns non-zero, it means that the URB was not 396 * queued. 397 *------------------------------------------------------------------------*/ 398int 399usb_submit_urb(struct urb *urb, uint16_t mem_flags) 400{ 401 struct usb_host_endpoint *uhe; 402 uint8_t do_unlock; 403 int err; 404 405 if (urb == NULL) 406 return (-EINVAL); 407 408 do_unlock = mtx_owned(&Giant) ? 0 : 1; 409 if (do_unlock) 410 mtx_lock(&Giant); 411 412 if (urb->endpoint == NULL) { 413 err = -EINVAL; 414 goto done; 415 } 416 417 /* 418 * Check to see if the urb is in the process of being killed 419 * and stop a urb that is in the process of being killed from 420 * being re-submitted (e.g. from its completion callback 421 * function). 422 */ 423 if (urb->kill_count != 0) { 424 err = -EPERM; 425 goto done; 426 } 427 428 uhe = urb->endpoint; 429 430 /* 431 * Check that we have got a FreeBSD USB transfer that will dequeue 432 * the URB structure and do the real transfer. If there are no USB 433 * transfers, then we return an error. 434 */ 435 if (uhe->bsd_xfer[0] || 436 uhe->bsd_xfer[1]) { 437 /* we are ready! */ 438 439 TAILQ_INSERT_TAIL(&uhe->bsd_urb_list, urb, bsd_urb_list); 440 441 urb->status = -EINPROGRESS; 442 443 usbd_transfer_start(uhe->bsd_xfer[0]); 444 usbd_transfer_start(uhe->bsd_xfer[1]); 445 err = 0; 446 } else { 447 /* no pipes have been setup yet! */ 448 urb->status = -EINVAL; 449 err = -EINVAL; 450 } 451done: 452 if (do_unlock) 453 mtx_unlock(&Giant); 454 return (err); 455} 456 457/*------------------------------------------------------------------------* 458 * usb_unlink_urb 459 * 460 * This function is used to stop an URB after that it is been 461 * submitted, but before the "complete" callback has been called. On 462 *------------------------------------------------------------------------*/ 463int 464usb_unlink_urb(struct urb *urb) 465{ 466 return (usb_unlink_urb_sub(urb, 0)); 467} 468 469static void 470usb_unlink_bsd(struct usb_xfer *xfer, 471 struct urb *urb, uint8_t drain) 472{ 473 if (xfer == NULL) 474 return; 475 if (!usbd_transfer_pending(xfer)) 476 return; 477 if (xfer->priv_fifo == (void *)urb) { 478 if (drain) { 479 mtx_unlock(&Giant); 480 usbd_transfer_drain(xfer); 481 mtx_lock(&Giant); 482 } else { 483 usbd_transfer_stop(xfer); 484 } 485 usbd_transfer_start(xfer); 486 } 487} 488 489static int 490usb_unlink_urb_sub(struct urb *urb, uint8_t drain) 491{ 492 struct usb_host_endpoint *uhe; 493 uint16_t x; 494 uint8_t do_unlock; 495 int err; 496 497 if (urb == NULL) 498 return (-EINVAL); 499 500 do_unlock = mtx_owned(&Giant) ? 0 : 1; 501 if (do_unlock) 502 mtx_lock(&Giant); 503 if (drain) 504 urb->kill_count++; 505 506 if (urb->endpoint == NULL) { 507 err = -EINVAL; 508 goto done; 509 } 510 uhe = urb->endpoint; 511 512 if (urb->bsd_urb_list.tqe_prev) { 513 514 /* not started yet, just remove it from the queue */ 515 TAILQ_REMOVE(&uhe->bsd_urb_list, urb, bsd_urb_list); 516 urb->bsd_urb_list.tqe_prev = NULL; 517 urb->status = -ECONNRESET; 518 urb->actual_length = 0; 519 520 for (x = 0; x < urb->number_of_packets; x++) { 521 urb->iso_frame_desc[x].actual_length = 0; 522 } 523 524 if (urb->complete) { 525 (urb->complete) (urb); 526 } 527 } else { 528 529 /* 530 * If the URB is not on the URB list, then check if one of 531 * the FreeBSD USB transfer are processing the current URB. 532 * If so, re-start that transfer, which will lead to the 533 * termination of that URB: 534 */ 535 usb_unlink_bsd(uhe->bsd_xfer[0], urb, drain); 536 usb_unlink_bsd(uhe->bsd_xfer[1], urb, drain); 537 } 538 err = 0; 539done: 540 if (drain) 541 urb->kill_count--; 542 if (do_unlock) 543 mtx_unlock(&Giant); 544 return (err); 545} 546 547/*------------------------------------------------------------------------* 548 * usb_clear_halt 549 * 550 * This function must always be used to clear the stall. Stall is when 551 * an USB endpoint returns a stall message to the USB host controller. 552 * Until the stall is cleared, no data can be transferred. 553 *------------------------------------------------------------------------*/ 554int 555usb_clear_halt(struct usb_device *dev, struct usb_host_endpoint *uhe) 556{ 557 struct usb_config cfg[1]; 558 struct usb_endpoint *ep; 559 uint8_t type; 560 uint8_t addr; 561 562 if (uhe == NULL) 563 return (-EINVAL); 564 565 type = uhe->desc.bmAttributes & UE_XFERTYPE; 566 addr = uhe->desc.bEndpointAddress; 567 568 bzero(cfg, sizeof(cfg)); 569 570 cfg[0].type = type; 571 cfg[0].endpoint = addr & UE_ADDR; 572 cfg[0].direction = addr & (UE_DIR_OUT | UE_DIR_IN); 573 574 ep = usbd_get_endpoint(dev, uhe->bsd_iface_index, cfg); 575 if (ep == NULL) 576 return (-EINVAL); 577 578 usbd_clear_data_toggle(dev, ep); 579 580 return (usb_control_msg(dev, &dev->ep0, 581 UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT, 582 UF_ENDPOINT_HALT, addr, NULL, 0, 1000)); 583} 584 585/*------------------------------------------------------------------------* 586 * usb_start_wait_urb 587 * 588 * This is an internal function that is used to perform synchronous 589 * Linux USB transfers. 590 *------------------------------------------------------------------------*/ 591static int 592usb_start_wait_urb(struct urb *urb, usb_timeout_t timeout, uint16_t *p_actlen) 593{ 594 int err; 595 uint8_t do_unlock; 596 597 /* you must have a timeout! */ 598 if (timeout == 0) { 599 timeout = 1; 600 } 601 urb->complete = &usb_linux_wait_complete; 602 urb->timeout = timeout; 603 urb->transfer_flags |= URB_WAIT_WAKEUP; 604 urb->transfer_flags &= ~URB_IS_SLEEPING; 605 606 do_unlock = mtx_owned(&Giant) ? 0 : 1; 607 if (do_unlock) 608 mtx_lock(&Giant); 609 err = usb_submit_urb(urb, 0); 610 if (err) 611 goto done; 612 613 /* 614 * the URB might have completed before we get here, so check that by 615 * using some flags! 616 */ 617 while (urb->transfer_flags & URB_WAIT_WAKEUP) { 618 urb->transfer_flags |= URB_IS_SLEEPING; 619 cv_wait(&urb->cv_wait, &Giant); 620 urb->transfer_flags &= ~URB_IS_SLEEPING; 621 } 622 623 err = urb->status; 624 625done: 626 if (do_unlock) 627 mtx_unlock(&Giant); 628 if (p_actlen != NULL) { 629 if (err) 630 *p_actlen = 0; 631 else 632 *p_actlen = urb->actual_length; 633 } 634 return (err); 635} 636 637/*------------------------------------------------------------------------* 638 * usb_control_msg 639 * 640 * The following function performs a control transfer sequence one any 641 * control, bulk or interrupt endpoint, specified by "uhe". A control 642 * transfer means that you transfer an 8-byte header first followed by 643 * a data-phase as indicated by the 8-byte header. The "timeout" is 644 * given in milliseconds. 645 * 646 * Return values: 647 * 0: Success 648 * < 0: Failure 649 * > 0: Acutal length 650 *------------------------------------------------------------------------*/ 651int 652usb_control_msg(struct usb_device *dev, struct usb_host_endpoint *uhe, 653 uint8_t request, uint8_t requesttype, 654 uint16_t value, uint16_t index, void *data, 655 uint16_t size, usb_timeout_t timeout) 656{ 657 struct usb_device_request req; 658 struct urb *urb; 659 int err; 660 uint16_t actlen; 661 uint8_t type; 662 uint8_t addr; 663 664 req.bmRequestType = requesttype; 665 req.bRequest = request; 666 USETW(req.wValue, value); 667 USETW(req.wIndex, index); 668 USETW(req.wLength, size); 669 670 if (uhe == NULL) { 671 return (-EINVAL); 672 } 673 type = (uhe->desc.bmAttributes & UE_XFERTYPE); 674 addr = (uhe->desc.bEndpointAddress & UE_ADDR); 675 676 if (type != UE_CONTROL) { 677 return (-EINVAL); 678 } 679 if (addr == 0) { 680 /* 681 * The FreeBSD USB stack supports standard control 682 * transfers on control endpoint zero: 683 */ 684 err = usbd_do_request_flags(dev, 685 NULL, &req, data, USB_SHORT_XFER_OK, 686 &actlen, timeout); 687 if (err) { 688 err = -EPIPE; 689 } else { 690 err = actlen; 691 } 692 return (err); 693 } 694 if (dev->flags.usb_mode != USB_MODE_HOST) { 695 /* not supported */ 696 return (-EINVAL); 697 } 698 err = usb_setup_endpoint(dev, uhe, 1 /* dummy */ ); 699 700 /* 701 * NOTE: we need to allocate real memory here so that we don't 702 * transfer data to/from the stack! 703 * 704 * 0xFFFF is a FreeBSD specific magic value. 705 */ 706 urb = usb_alloc_urb(0xFFFF, size); 707 if (urb == NULL) 708 return (-ENOMEM); 709 710 urb->dev = dev; 711 urb->endpoint = uhe; 712 713 bcopy(&req, urb->setup_packet, sizeof(req)); 714 715 if (size && (!(req.bmRequestType & UT_READ))) { 716 /* move the data to a real buffer */ 717 bcopy(data, USB_ADD_BYTES(urb->setup_packet, 718 sizeof(req)), size); 719 } 720 err = usb_start_wait_urb(urb, timeout, &actlen); 721 722 if (req.bmRequestType & UT_READ) { 723 if (actlen) { 724 bcopy(USB_ADD_BYTES(urb->setup_packet, 725 sizeof(req)), data, actlen); 726 } 727 } 728 usb_free_urb(urb); 729 730 if (err == 0) { 731 err = actlen; 732 } 733 return (err); 734} 735 736/*------------------------------------------------------------------------* 737 * usb_set_interface 738 * 739 * The following function will select which alternate setting of an 740 * USB interface you plan to use. By default alternate setting with 741 * index zero is selected. Note that "iface_no" is not the interface 742 * index, but rather the value of "bInterfaceNumber". 743 *------------------------------------------------------------------------*/ 744int 745usb_set_interface(struct usb_device *dev, uint8_t iface_no, uint8_t alt_index) 746{ 747 struct usb_interface *p_ui = usb_ifnum_to_if(dev, iface_no); 748 int err; 749 750 if (p_ui == NULL) 751 return (-EINVAL); 752 if (alt_index >= p_ui->num_altsetting) 753 return (-EINVAL); 754 usb_linux_cleanup_interface(dev, p_ui); 755 err = -usbd_set_alt_interface_index(dev, 756 p_ui->bsd_iface_index, alt_index); 757 if (err == 0) { 758 p_ui->cur_altsetting = p_ui->altsetting + alt_index; 759 } 760 return (err); 761} 762 763/*------------------------------------------------------------------------* 764 * usb_setup_endpoint 765 * 766 * The following function is an extension to the Linux USB API that 767 * allows you to set a maximum buffer size for a given USB endpoint. 768 * The maximum buffer size is per URB. If you don't call this function 769 * to set a maximum buffer size, the endpoint will not be functional. 770 * Note that for isochronous endpoints the maximum buffer size must be 771 * a non-zero dummy, hence this function will base the maximum buffer 772 * size on "wMaxPacketSize". 773 *------------------------------------------------------------------------*/ 774int 775usb_setup_endpoint(struct usb_device *dev, 776 struct usb_host_endpoint *uhe, usb_size_t bufsize) 777{ 778 struct usb_config cfg[2]; 779 uint8_t type = uhe->desc.bmAttributes & UE_XFERTYPE; 780 uint8_t addr = uhe->desc.bEndpointAddress; 781 782 if (uhe->fbsd_buf_size == bufsize) { 783 /* optimize */ 784 return (0); 785 } 786 usbd_transfer_unsetup(uhe->bsd_xfer, 2); 787 788 uhe->fbsd_buf_size = bufsize; 789 790 if (bufsize == 0) { 791 return (0); 792 } 793 bzero(cfg, sizeof(cfg)); 794 795 if (type == UE_ISOCHRONOUS) { 796 797 /* 798 * Isochronous transfers are special in that they don't fit 799 * into the BULK/INTR/CONTROL transfer model. 800 */ 801 802 cfg[0].type = type; 803 cfg[0].endpoint = addr & UE_ADDR; 804 cfg[0].direction = addr & (UE_DIR_OUT | UE_DIR_IN); 805 cfg[0].callback = &usb_linux_isoc_callback; 806 cfg[0].bufsize = 0; /* use wMaxPacketSize */ 807 cfg[0].frames = usb_max_isoc_frames(dev); 808 cfg[0].flags.proxy_buffer = 1; 809#if 0 810 /* 811 * The Linux USB API allows non back-to-back 812 * isochronous frames which we do not support. If the 813 * isochronous frames are not back-to-back we need to 814 * do a copy, and then we need a buffer for 815 * that. Enable this at your own risk. 816 */ 817 cfg[0].flags.ext_buffer = 1; 818#endif 819 cfg[0].flags.short_xfer_ok = 1; 820 821 bcopy(cfg, cfg + 1, sizeof(*cfg)); 822 823 /* Allocate and setup two generic FreeBSD USB transfers */ 824 825 if (usbd_transfer_setup(dev, &uhe->bsd_iface_index, 826 uhe->bsd_xfer, cfg, 2, uhe, &Giant)) { 827 return (-EINVAL); 828 } 829 } else { 830 if (bufsize > (1 << 22)) { 831 /* limit buffer size */ 832 bufsize = (1 << 22); 833 } 834 /* Allocate and setup one generic FreeBSD USB transfer */ 835 836 cfg[0].type = type; 837 cfg[0].endpoint = addr & UE_ADDR; 838 cfg[0].direction = addr & (UE_DIR_OUT | UE_DIR_IN); 839 cfg[0].callback = &usb_linux_non_isoc_callback; 840 cfg[0].bufsize = bufsize; 841 cfg[0].flags.ext_buffer = 1; /* enable zero-copy */ 842 cfg[0].flags.proxy_buffer = 1; 843 cfg[0].flags.short_xfer_ok = 1; 844 845 if (usbd_transfer_setup(dev, &uhe->bsd_iface_index, 846 uhe->bsd_xfer, cfg, 1, uhe, &Giant)) { 847 return (-EINVAL); 848 } 849 } 850 return (0); 851} 852 853/*------------------------------------------------------------------------* 854 * usb_linux_create_usb_device 855 * 856 * The following function is used to build up a per USB device 857 * structure tree, that mimics the Linux one. The root structure 858 * is returned by this function. 859 *------------------------------------------------------------------------*/ 860static int 861usb_linux_create_usb_device(struct usb_device *udev, device_t dev) 862{ 863 struct usb_config_descriptor *cd = usbd_get_config_descriptor(udev); 864 struct usb_descriptor *desc; 865 struct usb_interface_descriptor *id; 866 struct usb_endpoint_descriptor *ed; 867 struct usb_interface *p_ui = NULL; 868 struct usb_host_interface *p_uhi = NULL; 869 struct usb_host_endpoint *p_uhe = NULL; 870 usb_size_t size; 871 uint16_t niface_total; 872 uint16_t nedesc; 873 uint16_t iface_no_curr; 874 uint16_t iface_index; 875 uint8_t pass; 876 uint8_t iface_no; 877 878 /* 879 * We do two passes. One pass for computing necessary memory size 880 * and one pass to initialize all the allocated memory structures. 881 */ 882 for (pass = 0; pass < 2; pass++) { 883 884 iface_no_curr = 0 - 1; 885 niface_total = 0; 886 iface_index = 0; 887 nedesc = 0; 888 desc = NULL; 889 890 /* 891 * Iterate over all the USB descriptors. Use the USB config 892 * descriptor pointer provided by the FreeBSD USB stack. 893 */ 894 while ((desc = usb_desc_foreach(cd, desc))) { 895 896 /* 897 * Build up a tree according to the descriptors we 898 * find: 899 */ 900 switch (desc->bDescriptorType) { 901 case UDESC_DEVICE: 902 break; 903 904 case UDESC_ENDPOINT: 905 ed = (void *)desc; 906 if ((ed->bLength < sizeof(*ed)) || 907 (iface_index == 0)) 908 break; 909 if (p_uhe) { 910 bcopy(ed, &p_uhe->desc, sizeof(p_uhe->desc)); 911 p_uhe->bsd_iface_index = iface_index - 1; 912 TAILQ_INIT(&p_uhe->bsd_urb_list); 913 p_uhe++; 914 } 915 if (p_uhi) { 916 (p_uhi - 1)->desc.bNumEndpoints++; 917 } 918 nedesc++; 919 break; 920 921 case UDESC_INTERFACE: 922 id = (void *)desc; 923 if (id->bLength < sizeof(*id)) 924 break; 925 if (p_uhi) { 926 bcopy(id, &p_uhi->desc, sizeof(p_uhi->desc)); 927 p_uhi->desc.bNumEndpoints = 0; 928 p_uhi->endpoint = p_uhe; 929 p_uhi->string = ""; 930 p_uhi->bsd_iface_index = iface_index; 931 p_uhi++; 932 } 933 iface_no = id->bInterfaceNumber; 934 niface_total++; 935 if (iface_no_curr != iface_no) { 936 if (p_ui) { 937 p_ui->altsetting = p_uhi - 1; 938 p_ui->cur_altsetting = p_uhi - 1; 939 p_ui->num_altsetting = 1; 940 p_ui->bsd_iface_index = iface_index; 941 p_ui->linux_udev = udev; 942 p_ui++; 943 } 944 iface_no_curr = iface_no; 945 iface_index++; 946 } else { 947 if (p_ui) { 948 (p_ui - 1)->num_altsetting++; 949 } 950 } 951 break; 952 953 default: 954 break; 955 } 956 } 957 958 if (pass == 0) { 959 960 size = (sizeof(*p_uhe) * nedesc) + 961 (sizeof(*p_ui) * iface_index) + 962 (sizeof(*p_uhi) * niface_total); 963 964 p_uhe = malloc(size, M_USBDEV, M_WAITOK | M_ZERO); 965 p_ui = (void *)(p_uhe + nedesc); 966 p_uhi = (void *)(p_ui + iface_index); 967 968 udev->linux_iface_start = p_ui; 969 udev->linux_iface_end = p_ui + iface_index; 970 udev->linux_endpoint_start = p_uhe; 971 udev->linux_endpoint_end = p_uhe + nedesc; 972 udev->devnum = device_get_unit(dev); 973 bcopy(&udev->ddesc, &udev->descriptor, 974 sizeof(udev->descriptor)); 975 bcopy(udev->ctrl_ep.edesc, &udev->ep0.desc, 976 sizeof(udev->ep0.desc)); 977 } 978 } 979 return (0); 980} 981 982/*------------------------------------------------------------------------* 983 * usb_alloc_urb 984 * 985 * This function should always be used when you allocate an URB for 986 * use with the USB Linux stack. In case of an isochronous transfer 987 * you must specifiy the maximum number of "iso_packets" which you 988 * plan to transfer per URB. This function is always blocking, and 989 * "mem_flags" are not regarded like on Linux. 990 *------------------------------------------------------------------------*/ 991struct urb * 992usb_alloc_urb(uint16_t iso_packets, uint16_t mem_flags) 993{ 994 struct urb *urb; 995 usb_size_t size; 996 997 if (iso_packets == 0xFFFF) { 998 /* 999 * FreeBSD specific magic value to ask for control transfer 1000 * memory allocation: 1001 */ 1002 size = sizeof(*urb) + sizeof(struct usb_device_request) + mem_flags; 1003 } else { 1004 size = sizeof(*urb) + (iso_packets * sizeof(urb->iso_frame_desc[0])); 1005 } 1006 1007 urb = malloc(size, M_USBDEV, M_WAITOK | M_ZERO); 1008 if (urb) { 1009 1010 cv_init(&urb->cv_wait, "URBWAIT"); 1011 if (iso_packets == 0xFFFF) { 1012 urb->setup_packet = (void *)(urb + 1); 1013 urb->transfer_buffer = (void *)(urb->setup_packet + 1014 sizeof(struct usb_device_request)); 1015 } else { 1016 urb->number_of_packets = iso_packets; 1017 } 1018 } 1019 return (urb); 1020} 1021 1022/*------------------------------------------------------------------------* 1023 * usb_find_host_endpoint 1024 * 1025 * The following function will return the Linux USB host endpoint 1026 * structure that matches the given endpoint type and endpoint 1027 * value. If no match is found, NULL is returned. This function is not 1028 * part of the Linux USB API and is only used internally. 1029 *------------------------------------------------------------------------*/ 1030struct usb_host_endpoint * 1031usb_find_host_endpoint(struct usb_device *dev, uint8_t type, uint8_t ep) 1032{ 1033 struct usb_host_endpoint *uhe; 1034 struct usb_host_endpoint *uhe_end; 1035 struct usb_host_interface *uhi; 1036 struct usb_interface *ui; 1037 uint8_t ea; 1038 uint8_t at; 1039 uint8_t mask; 1040 1041 if (dev == NULL) { 1042 return (NULL); 1043 } 1044 if (type == UE_CONTROL) { 1045 mask = UE_ADDR; 1046 } else { 1047 mask = (UE_DIR_IN | UE_DIR_OUT | UE_ADDR); 1048 } 1049 1050 ep &= mask; 1051 1052 /* 1053 * Iterate over all the interfaces searching the selected alternate 1054 * setting only, and all belonging endpoints. 1055 */ 1056 for (ui = dev->linux_iface_start; 1057 ui != dev->linux_iface_end; 1058 ui++) { 1059 uhi = ui->cur_altsetting; 1060 if (uhi) { 1061 uhe_end = uhi->endpoint + uhi->desc.bNumEndpoints; 1062 for (uhe = uhi->endpoint; 1063 uhe != uhe_end; 1064 uhe++) { 1065 ea = uhe->desc.bEndpointAddress; 1066 at = uhe->desc.bmAttributes; 1067 1068 if (((ea & mask) == ep) && 1069 ((at & UE_XFERTYPE) == type)) { 1070 return (uhe); 1071 } 1072 } 1073 } 1074 } 1075 1076 if ((type == UE_CONTROL) && ((ep & UE_ADDR) == 0)) { 1077 return (&dev->ep0); 1078 } 1079 return (NULL); 1080} 1081 1082/*------------------------------------------------------------------------* 1083 * usb_altnum_to_altsetting 1084 * 1085 * The following function returns a pointer to an alternate setting by 1086 * index given a "usb_interface" pointer. If the alternate setting by 1087 * index does not exist, NULL is returned. And alternate setting is a 1088 * variant of an interface, but usually with slightly different 1089 * characteristics. 1090 *------------------------------------------------------------------------*/ 1091struct usb_host_interface * 1092usb_altnum_to_altsetting(const struct usb_interface *intf, uint8_t alt_index) 1093{ 1094 if (alt_index >= intf->num_altsetting) { 1095 return (NULL); 1096 } 1097 return (intf->altsetting + alt_index); 1098} 1099 1100/*------------------------------------------------------------------------* 1101 * usb_ifnum_to_if 1102 * 1103 * The following function searches up an USB interface by 1104 * "bInterfaceNumber". If no match is found, NULL is returned. 1105 *------------------------------------------------------------------------*/ 1106struct usb_interface * 1107usb_ifnum_to_if(struct usb_device *dev, uint8_t iface_no) 1108{ 1109 struct usb_interface *p_ui; 1110 1111 for (p_ui = dev->linux_iface_start; 1112 p_ui != dev->linux_iface_end; 1113 p_ui++) { 1114 if ((p_ui->num_altsetting > 0) && 1115 (p_ui->altsetting->desc.bInterfaceNumber == iface_no)) { 1116 return (p_ui); 1117 } 1118 } 1119 return (NULL); 1120} 1121 1122/*------------------------------------------------------------------------* 1123 * usb_buffer_alloc 1124 *------------------------------------------------------------------------*/ 1125void * 1126usb_buffer_alloc(struct usb_device *dev, usb_size_t size, uint16_t mem_flags, uint8_t *dma_addr) 1127{ 1128 return (malloc(size, M_USBDEV, M_WAITOK | M_ZERO)); 1129} 1130 1131/*------------------------------------------------------------------------* 1132 * usbd_get_intfdata 1133 *------------------------------------------------------------------------*/ 1134void * 1135usbd_get_intfdata(struct usb_interface *intf) 1136{ 1137 return (intf->bsd_priv_sc); 1138} 1139 1140/*------------------------------------------------------------------------* 1141 * usb_linux_register 1142 * 1143 * The following function is used by the "USB_DRIVER_EXPORT()" macro, 1144 * and is used to register a Linux USB driver, so that its 1145 * "usb_device_id" structures gets searched a probe time. This 1146 * function is not part of the Linux USB API, and is for internal use 1147 * only. 1148 *------------------------------------------------------------------------*/ 1149void 1150usb_linux_register(void *arg) 1151{ 1152 struct usb_driver *drv = arg; 1153 1154 mtx_lock(&Giant); 1155 LIST_INSERT_HEAD(&usb_linux_driver_list, drv, linux_driver_list); 1156 mtx_unlock(&Giant); 1157 1158 usb_needs_explore_all(); 1159} 1160 1161/*------------------------------------------------------------------------* 1162 * usb_linux_deregister 1163 * 1164 * The following function is used by the "USB_DRIVER_EXPORT()" macro, 1165 * and is used to deregister a Linux USB driver. This function will 1166 * ensure that all driver instances belonging to the Linux USB device 1167 * driver in question, gets detached before the driver is 1168 * unloaded. This function is not part of the Linux USB API, and is 1169 * for internal use only. 1170 *------------------------------------------------------------------------*/ 1171void 1172usb_linux_deregister(void *arg) 1173{ 1174 struct usb_driver *drv = arg; 1175 struct usb_linux_softc *sc; 1176 1177repeat: 1178 mtx_lock(&Giant); 1179 LIST_FOREACH(sc, &usb_linux_attached_list, sc_attached_list) { 1180 if (sc->sc_udrv == drv) { 1181 mtx_unlock(&Giant); 1182 device_detach(sc->sc_fbsd_dev); 1183 goto repeat; 1184 } 1185 } 1186 LIST_REMOVE(drv, linux_driver_list); 1187 mtx_unlock(&Giant); 1188} 1189 1190/*------------------------------------------------------------------------* 1191 * usb_linux_free_device 1192 * 1193 * The following function is only used by the FreeBSD USB stack, to 1194 * cleanup and free memory after that a Linux USB device was attached. 1195 *------------------------------------------------------------------------*/ 1196void 1197usb_linux_free_device(struct usb_device *dev) 1198{ 1199 struct usb_host_endpoint *uhe; 1200 struct usb_host_endpoint *uhe_end; 1201 int err; 1202 1203 uhe = dev->linux_endpoint_start; 1204 uhe_end = dev->linux_endpoint_end; 1205 while (uhe != uhe_end) { 1206 err = usb_setup_endpoint(dev, uhe, 0); 1207 uhe++; 1208 } 1209 err = usb_setup_endpoint(dev, &dev->ep0, 0); 1210 free(dev->linux_endpoint_start, M_USBDEV); 1211} 1212 1213/*------------------------------------------------------------------------* 1214 * usb_buffer_free 1215 *------------------------------------------------------------------------*/ 1216void 1217usb_buffer_free(struct usb_device *dev, usb_size_t size, 1218 void *addr, uint8_t dma_addr) 1219{ 1220 free(addr, M_USBDEV); 1221} 1222 1223/*------------------------------------------------------------------------* 1224 * usb_free_urb 1225 *------------------------------------------------------------------------*/ 1226void 1227usb_free_urb(struct urb *urb) 1228{ 1229 if (urb == NULL) { 1230 return; 1231 } 1232 /* make sure that the current URB is not active */ 1233 usb_kill_urb(urb); 1234 1235 /* destroy condition variable */ 1236 cv_destroy(&urb->cv_wait); 1237 1238 /* just free it */ 1239 free(urb, M_USBDEV); 1240} 1241 1242/*------------------------------------------------------------------------* 1243 * usb_init_urb 1244 * 1245 * The following function can be used to initialize a custom URB. It 1246 * is not recommended to use this function. Use "usb_alloc_urb()" 1247 * instead. 1248 *------------------------------------------------------------------------*/ 1249void 1250usb_init_urb(struct urb *urb) 1251{ 1252 if (urb == NULL) { 1253 return; 1254 } 1255 bzero(urb, sizeof(*urb)); 1256} 1257 1258/*------------------------------------------------------------------------* 1259 * usb_kill_urb 1260 *------------------------------------------------------------------------*/ 1261void 1262usb_kill_urb(struct urb *urb) 1263{ 1264 usb_unlink_urb_sub(urb, 1); 1265} 1266 1267/*------------------------------------------------------------------------* 1268 * usb_set_intfdata 1269 * 1270 * The following function sets the per Linux USB interface private 1271 * data pointer. It is used by most Linux USB device drivers. 1272 *------------------------------------------------------------------------*/ 1273void 1274usb_set_intfdata(struct usb_interface *intf, void *data) 1275{ 1276 intf->bsd_priv_sc = data; 1277} 1278 1279/*------------------------------------------------------------------------* 1280 * usb_linux_cleanup_interface 1281 * 1282 * The following function will release all FreeBSD USB transfers 1283 * associated with a Linux USB interface. It is for internal use only. 1284 *------------------------------------------------------------------------*/ 1285static void 1286usb_linux_cleanup_interface(struct usb_device *dev, struct usb_interface *iface) 1287{ 1288 struct usb_host_interface *uhi; 1289 struct usb_host_interface *uhi_end; 1290 struct usb_host_endpoint *uhe; 1291 struct usb_host_endpoint *uhe_end; 1292 int err; 1293 1294 uhi = iface->altsetting; 1295 uhi_end = iface->altsetting + iface->num_altsetting; 1296 while (uhi != uhi_end) { 1297 uhe = uhi->endpoint; 1298 uhe_end = uhi->endpoint + uhi->desc.bNumEndpoints; 1299 while (uhe != uhe_end) { 1300 err = usb_setup_endpoint(dev, uhe, 0); 1301 uhe++; 1302 } 1303 uhi++; 1304 } 1305} 1306 1307/*------------------------------------------------------------------------* 1308 * usb_linux_wait_complete 1309 * 1310 * The following function is used by "usb_start_wait_urb()" to wake it 1311 * up, when an USB transfer has finished. 1312 *------------------------------------------------------------------------*/ 1313static void 1314usb_linux_wait_complete(struct urb *urb) 1315{ 1316 if (urb->transfer_flags & URB_IS_SLEEPING) { 1317 cv_signal(&urb->cv_wait); 1318 } 1319 urb->transfer_flags &= ~URB_WAIT_WAKEUP; 1320} 1321 1322/*------------------------------------------------------------------------* 1323 * usb_linux_complete 1324 *------------------------------------------------------------------------*/ 1325static void 1326usb_linux_complete(struct usb_xfer *xfer) 1327{ 1328 struct urb *urb; 1329 1330 urb = usbd_xfer_get_priv(xfer); 1331 usbd_xfer_set_priv(xfer, NULL); 1332 if (urb->complete) { 1333 (urb->complete) (urb); 1334 } 1335} 1336 1337/*------------------------------------------------------------------------* 1338 * usb_linux_isoc_callback 1339 * 1340 * The following is the FreeBSD isochronous USB callback. Isochronous 1341 * frames are USB packets transferred 1000 or 8000 times per second, 1342 * depending on whether a full- or high- speed USB transfer is 1343 * used. 1344 *------------------------------------------------------------------------*/ 1345static void 1346usb_linux_isoc_callback(struct usb_xfer *xfer, usb_error_t error) 1347{ 1348 usb_frlength_t max_frame = xfer->max_frame_size; 1349 usb_frlength_t offset; 1350 usb_frcount_t x; 1351 struct urb *urb = usbd_xfer_get_priv(xfer); 1352 struct usb_host_endpoint *uhe = usbd_xfer_softc(xfer); 1353 struct usb_iso_packet_descriptor *uipd; 1354 1355 DPRINTF("\n"); 1356 1357 switch (USB_GET_STATE(xfer)) { 1358 case USB_ST_TRANSFERRED: 1359 1360 if (urb->bsd_isread) { 1361 1362 /* copy in data with regard to the URB */ 1363 1364 offset = 0; 1365 1366 for (x = 0; x < urb->number_of_packets; x++) { 1367 uipd = urb->iso_frame_desc + x; 1368 if (uipd->length > xfer->frlengths[x]) { 1369 if (urb->transfer_flags & URB_SHORT_NOT_OK) { 1370 /* XXX should be EREMOTEIO */ 1371 uipd->status = -EPIPE; 1372 } else { 1373 uipd->status = 0; 1374 } 1375 } else { 1376 uipd->status = 0; 1377 } 1378 uipd->actual_length = xfer->frlengths[x]; 1379 if (!xfer->flags.ext_buffer) { 1380 usbd_copy_out(xfer->frbuffers, offset, 1381 USB_ADD_BYTES(urb->transfer_buffer, 1382 uipd->offset), uipd->actual_length); 1383 } 1384 offset += max_frame; 1385 } 1386 } else { 1387 for (x = 0; x < urb->number_of_packets; x++) { 1388 uipd = urb->iso_frame_desc + x; 1389 uipd->actual_length = xfer->frlengths[x]; 1390 uipd->status = 0; 1391 } 1392 } 1393 1394 urb->actual_length = xfer->actlen; 1395 1396 /* check for short transfer */ 1397 if (xfer->actlen < xfer->sumlen) { 1398 /* short transfer */ 1399 if (urb->transfer_flags & URB_SHORT_NOT_OK) { 1400 /* XXX should be EREMOTEIO */ 1401 urb->status = -EPIPE; 1402 } else { 1403 urb->status = 0; 1404 } 1405 } else { 1406 /* success */ 1407 urb->status = 0; 1408 } 1409 1410 /* call callback */ 1411 usb_linux_complete(xfer); 1412 1413 case USB_ST_SETUP: 1414tr_setup: 1415 1416 if (xfer->priv_fifo == NULL) { 1417 1418 /* get next transfer */ 1419 urb = TAILQ_FIRST(&uhe->bsd_urb_list); 1420 if (urb == NULL) { 1421 /* nothing to do */ 1422 return; 1423 } 1424 TAILQ_REMOVE(&uhe->bsd_urb_list, urb, bsd_urb_list); 1425 urb->bsd_urb_list.tqe_prev = NULL; 1426 1427 x = xfer->max_frame_count; 1428 if (urb->number_of_packets > x) { 1429 /* XXX simply truncate the transfer */ 1430 urb->number_of_packets = x; 1431 } 1432 } else { 1433 DPRINTF("Already got a transfer\n"); 1434 1435 /* already got a transfer (should not happen) */ 1436 urb = usbd_xfer_get_priv(xfer); 1437 } 1438 1439 urb->bsd_isread = (uhe->desc.bEndpointAddress & UE_DIR_IN) ? 1 : 0; 1440 1441 if (xfer->flags.ext_buffer) { 1442 /* set virtual address to load */ 1443 usbd_xfer_set_frame_data(xfer, 0, urb->transfer_buffer, 0); 1444 } 1445 if (!(urb->bsd_isread)) { 1446 1447 /* copy out data with regard to the URB */ 1448 1449 offset = 0; 1450 1451 for (x = 0; x < urb->number_of_packets; x++) { 1452 uipd = urb->iso_frame_desc + x; 1453 usbd_xfer_set_frame_len(xfer, x, uipd->length); 1454 if (!xfer->flags.ext_buffer) { 1455 usbd_copy_in(xfer->frbuffers, offset, 1456 USB_ADD_BYTES(urb->transfer_buffer, 1457 uipd->offset), uipd->length); 1458 } 1459 offset += uipd->length; 1460 } 1461 } else { 1462 1463 /* 1464 * compute the transfer length into the "offset" 1465 * variable 1466 */ 1467 1468 offset = urb->number_of_packets * max_frame; 1469 1470 /* setup "frlengths" array */ 1471 1472 for (x = 0; x < urb->number_of_packets; x++) { 1473 uipd = urb->iso_frame_desc + x; 1474 usbd_xfer_set_frame_len(xfer, x, max_frame); 1475 } 1476 } 1477 usbd_xfer_set_priv(xfer, urb); 1478 xfer->flags.force_short_xfer = 0; 1479 xfer->timeout = urb->timeout; 1480 xfer->nframes = urb->number_of_packets; 1481 usbd_transfer_submit(xfer); 1482 return; 1483 1484 default: /* Error */ 1485 if (xfer->error == USB_ERR_CANCELLED) { 1486 urb->status = -ECONNRESET; 1487 } else { 1488 urb->status = -EPIPE; /* stalled */ 1489 } 1490 1491 /* Set zero for "actual_length" */ 1492 urb->actual_length = 0; 1493 1494 /* Set zero for "actual_length" */ 1495 for (x = 0; x < urb->number_of_packets; x++) { 1496 urb->iso_frame_desc[x].actual_length = 0; 1497 urb->iso_frame_desc[x].status = urb->status; 1498 } 1499 1500 /* call callback */ 1501 usb_linux_complete(xfer); 1502 1503 if (xfer->error == USB_ERR_CANCELLED) { 1504 /* we need to return in this case */ 1505 return; 1506 } 1507 goto tr_setup; 1508 1509 } 1510} 1511 1512/*------------------------------------------------------------------------* 1513 * usb_linux_non_isoc_callback 1514 * 1515 * The following is the FreeBSD BULK/INTERRUPT and CONTROL USB 1516 * callback. It dequeues Linux USB stack compatible URB's, transforms 1517 * the URB fields into a FreeBSD USB transfer, and defragments the USB 1518 * transfer as required. When the transfer is complete the "complete" 1519 * callback is called. 1520 *------------------------------------------------------------------------*/ 1521static void 1522usb_linux_non_isoc_callback(struct usb_xfer *xfer, usb_error_t error) 1523{ 1524 enum { 1525 REQ_SIZE = sizeof(struct usb_device_request) 1526 }; 1527 struct urb *urb = usbd_xfer_get_priv(xfer); 1528 struct usb_host_endpoint *uhe = usbd_xfer_softc(xfer); 1529 uint8_t *ptr; 1530 usb_frlength_t max_bulk = usbd_xfer_max_len(xfer); 1531 uint8_t data_frame = xfer->flags_int.control_xfr ? 1 : 0; 1532 1533 DPRINTF("\n"); 1534 1535 switch (USB_GET_STATE(xfer)) { 1536 case USB_ST_TRANSFERRED: 1537 1538 if (xfer->flags_int.control_xfr) { 1539 1540 /* don't transfer the setup packet again: */ 1541 1542 usbd_xfer_set_frame_len(xfer, 0, 0); 1543 } 1544 if (urb->bsd_isread && (!xfer->flags.ext_buffer)) { 1545 /* copy in data with regard to the URB */ 1546 usbd_copy_out(xfer->frbuffers + data_frame, 0, 1547 urb->bsd_data_ptr, xfer->frlengths[data_frame]); 1548 } 1549 urb->bsd_length_rem -= xfer->frlengths[data_frame]; 1550 urb->bsd_data_ptr += xfer->frlengths[data_frame]; 1551 urb->actual_length += xfer->frlengths[data_frame]; 1552 1553 /* check for short transfer */ 1554 if (xfer->actlen < xfer->sumlen) { 1555 urb->bsd_length_rem = 0; 1556 1557 /* short transfer */ 1558 if (urb->transfer_flags & URB_SHORT_NOT_OK) { 1559 urb->status = -EPIPE; 1560 } else { 1561 urb->status = 0; 1562 } 1563 } else { 1564 /* check remainder */ 1565 if (urb->bsd_length_rem > 0) { 1566 goto setup_bulk; 1567 } 1568 /* success */ 1569 urb->status = 0; 1570 } 1571 1572 /* call callback */ 1573 usb_linux_complete(xfer); 1574 1575 case USB_ST_SETUP: 1576tr_setup: 1577 /* get next transfer */ 1578 urb = TAILQ_FIRST(&uhe->bsd_urb_list); 1579 if (urb == NULL) { 1580 /* nothing to do */ 1581 return; 1582 } 1583 TAILQ_REMOVE(&uhe->bsd_urb_list, urb, bsd_urb_list); 1584 urb->bsd_urb_list.tqe_prev = NULL; 1585 1586 usbd_xfer_set_priv(xfer, urb); 1587 xfer->flags.force_short_xfer = 0; 1588 xfer->timeout = urb->timeout; 1589 1590 if (xfer->flags_int.control_xfr) { 1591 1592 /* 1593 * USB control transfers need special handling. 1594 * First copy in the header, then copy in data! 1595 */ 1596 if (!xfer->flags.ext_buffer) { 1597 usbd_copy_in(xfer->frbuffers, 0, 1598 urb->setup_packet, REQ_SIZE); 1599 usbd_xfer_set_frame_len(xfer, 0, REQ_SIZE); 1600 } else { 1601 /* set virtual address to load */ 1602 usbd_xfer_set_frame_data(xfer, 0, 1603 urb->setup_packet, REQ_SIZE); 1604 } 1605 1606 ptr = urb->setup_packet; 1607 1608 /* setup data transfer direction and length */ 1609 urb->bsd_isread = (ptr[0] & UT_READ) ? 1 : 0; 1610 urb->bsd_length_rem = ptr[6] | (ptr[7] << 8); 1611 1612 } else { 1613 1614 /* setup data transfer direction */ 1615 1616 urb->bsd_length_rem = urb->transfer_buffer_length; 1617 urb->bsd_isread = (uhe->desc.bEndpointAddress & 1618 UE_DIR_IN) ? 1 : 0; 1619 } 1620 1621 urb->bsd_data_ptr = urb->transfer_buffer; 1622 urb->actual_length = 0; 1623 1624setup_bulk: 1625 if (max_bulk > urb->bsd_length_rem) { 1626 max_bulk = urb->bsd_length_rem; 1627 } 1628 /* check if we need to force a short transfer */ 1629 1630 if ((max_bulk == urb->bsd_length_rem) && 1631 (urb->transfer_flags & URB_ZERO_PACKET) && 1632 (!xfer->flags_int.control_xfr)) { 1633 xfer->flags.force_short_xfer = 1; 1634 } 1635 /* check if we need to copy in data */ 1636 1637 if (xfer->flags.ext_buffer) { 1638 /* set virtual address to load */ 1639 usbd_xfer_set_frame_data(xfer, data_frame, 1640 urb->bsd_data_ptr, max_bulk); 1641 } else if (!urb->bsd_isread) { 1642 /* copy out data with regard to the URB */ 1643 usbd_copy_in(xfer->frbuffers + data_frame, 0, 1644 urb->bsd_data_ptr, max_bulk); 1645 usbd_xfer_set_frame_len(xfer, data_frame, max_bulk); 1646 } 1647 if (xfer->flags_int.control_xfr) { 1648 if (max_bulk > 0) { 1649 xfer->nframes = 2; 1650 } else { 1651 xfer->nframes = 1; 1652 } 1653 } else { 1654 xfer->nframes = 1; 1655 } 1656 usbd_transfer_submit(xfer); 1657 return; 1658 1659 default: 1660 if (xfer->error == USB_ERR_CANCELLED) { 1661 urb->status = -ECONNRESET; 1662 } else { 1663 urb->status = -EPIPE; 1664 } 1665 1666 /* Set zero for "actual_length" */ 1667 urb->actual_length = 0; 1668 1669 /* call callback */ 1670 usb_linux_complete(xfer); 1671 1672 if (xfer->error == USB_ERR_CANCELLED) { 1673 /* we need to return in this case */ 1674 return; 1675 } 1676 goto tr_setup; 1677 } 1678} 1679 1680/*------------------------------------------------------------------------* 1681 * usb_fill_bulk_urb 1682 *------------------------------------------------------------------------*/ 1683void 1684usb_fill_bulk_urb(struct urb *urb, struct usb_device *udev, 1685 struct usb_host_endpoint *uhe, void *buf, 1686 int length, usb_complete_t callback, void *arg) 1687{ 1688 urb->dev = udev; 1689 urb->endpoint = uhe; 1690 urb->transfer_buffer = buf; 1691 urb->transfer_buffer_length = length; 1692 urb->complete = callback; 1693 urb->context = arg; 1694} 1695 1696/*------------------------------------------------------------------------* 1697 * usb_bulk_msg 1698 * 1699 * NOTE: This function can also be used for interrupt endpoints! 1700 * 1701 * Return values: 1702 * 0: Success 1703 * Else: Failure 1704 *------------------------------------------------------------------------*/ 1705int 1706usb_bulk_msg(struct usb_device *udev, struct usb_host_endpoint *uhe, 1707 void *data, int len, uint16_t *pactlen, usb_timeout_t timeout) 1708{ 1709 struct urb *urb; 1710 int err; 1711 1712 if (uhe == NULL) 1713 return (-EINVAL); 1714 if (len < 0) 1715 return (-EINVAL); 1716 1717 err = usb_setup_endpoint(udev, uhe, 4096 /* bytes */); 1718 if (err) 1719 return (err); 1720 1721 urb = usb_alloc_urb(0, 0); 1722 if (urb == NULL) 1723 return (-ENOMEM); 1724 1725 usb_fill_bulk_urb(urb, udev, uhe, data, len, 1726 usb_linux_wait_complete, NULL); 1727 1728 err = usb_start_wait_urb(urb, timeout, pactlen); 1729 1730 usb_free_urb(urb); 1731 1732 return (err); 1733}
|