1184610Salfred/* $FreeBSD: stable/11/sys/compat/linuxkpi/common/src/linux_usb.c 368827 2020-12-30 01:11:12Z hselasky $ */ 2184610Salfred/*- 3184610Salfred * Copyright (c) 2007 Luigi Rizzo - Universita` di Pisa. All rights reserved. 4184610Salfred * Copyright (c) 2007 Hans Petter Selasky. All rights reserved. 5184610Salfred * 6184610Salfred * Redistribution and use in source and binary forms, with or without 7184610Salfred * modification, are permitted provided that the following conditions 8184610Salfred * are met: 9184610Salfred * 1. Redistributions of source code must retain the above copyright 10184610Salfred * notice, this list of conditions and the following disclaimer. 11184610Salfred * 2. Redistributions in binary form must reproduce the above copyright 12184610Salfred * notice, this list of conditions and the following disclaimer in the 13184610Salfred * documentation and/or other materials provided with the distribution. 14184610Salfred * 15184610Salfred * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16184610Salfred * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17184610Salfred * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18184610Salfred * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19184610Salfred * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20184610Salfred * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21184610Salfred * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22184610Salfred * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23184610Salfred * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24184610Salfred * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25184610Salfred * SUCH DAMAGE. 26184610Salfred */ 27184610Salfred 28246122Shselasky#ifdef USB_GLOBAL_INCLUDE_FILE 29246122Shselasky#include USB_GLOBAL_INCLUDE_FILE 30246122Shselasky#else 31194677Sthompsa#include <sys/stdint.h> 32194677Sthompsa#include <sys/stddef.h> 33194677Sthompsa#include <sys/param.h> 34194677Sthompsa#include <sys/queue.h> 35194677Sthompsa#include <sys/types.h> 36194677Sthompsa#include <sys/systm.h> 37194677Sthompsa#include <sys/kernel.h> 38194677Sthompsa#include <sys/bus.h> 39194677Sthompsa#include <sys/module.h> 40194677Sthompsa#include <sys/lock.h> 41194677Sthompsa#include <sys/mutex.h> 42194677Sthompsa#include <sys/condvar.h> 43194677Sthompsa#include <sys/sysctl.h> 44194677Sthompsa#include <sys/sx.h> 45194677Sthompsa#include <sys/unistd.h> 46194677Sthompsa#include <sys/callout.h> 47194677Sthompsa#include <sys/malloc.h> 48194677Sthompsa#include <sys/priv.h> 49194677Sthompsa 50188942Sthompsa#include <dev/usb/usb.h> 51194677Sthompsa#include <dev/usb/usbdi.h> 52194677Sthompsa#include <dev/usb/usbdi_util.h> 53184610Salfred 54194228Sthompsa#define USB_DEBUG_VAR usb_debug 55184610Salfred 56188942Sthompsa#include <dev/usb/usb_core.h> 57290135Shselasky#include <linux/usb.h> 58188942Sthompsa#include <dev/usb/usb_process.h> 59188942Sthompsa#include <dev/usb/usb_device.h> 60188942Sthompsa#include <dev/usb/usb_util.h> 61188942Sthompsa#include <dev/usb/usb_busdma.h> 62188942Sthompsa#include <dev/usb/usb_transfer.h> 63188942Sthompsa#include <dev/usb/usb_hub.h> 64188942Sthompsa#include <dev/usb/usb_request.h> 65188942Sthompsa#include <dev/usb/usb_debug.h> 66290135Shselasky#include <dev/usb/usb_dynamic.h> 67246122Shselasky#endif /* USB_GLOBAL_INCLUDE_FILE */ 68184610Salfred 69184610Salfredstruct usb_linux_softc { 70184610Salfred LIST_ENTRY(usb_linux_softc) sc_attached_list; 71184610Salfred 72184610Salfred device_t sc_fbsd_dev; 73192984Sthompsa struct usb_device *sc_fbsd_udev; 74184610Salfred struct usb_interface *sc_ui; 75184610Salfred struct usb_driver *sc_udrv; 76184610Salfred}; 77184610Salfred 78184610Salfred/* prototypes */ 79184610Salfredstatic device_probe_t usb_linux_probe; 80184610Salfredstatic device_attach_t usb_linux_attach; 81184610Salfredstatic device_detach_t usb_linux_detach; 82184610Salfredstatic device_suspend_t usb_linux_suspend; 83184610Salfredstatic device_resume_t usb_linux_resume; 84184610Salfred 85193045Sthompsastatic usb_callback_t usb_linux_isoc_callback; 86193045Sthompsastatic usb_callback_t usb_linux_non_isoc_callback; 87184610Salfred 88184610Salfredstatic usb_complete_t usb_linux_wait_complete; 89184610Salfred 90185948Sthompsastatic uint16_t usb_max_isoc_frames(struct usb_device *); 91193045Sthompsastatic int usb_start_wait_urb(struct urb *, usb_timeout_t, uint16_t *); 92185948Sthompsastatic const struct usb_device_id *usb_linux_lookup_id( 93192984Sthompsa const struct usb_device_id *, struct usb_attach_arg *); 94185948Sthompsastatic struct usb_driver *usb_linux_get_usb_driver(struct usb_linux_softc *); 95192984Sthompsastatic int usb_linux_create_usb_device(struct usb_device *, device_t); 96185948Sthompsastatic void usb_linux_cleanup_interface(struct usb_device *, 97185948Sthompsa struct usb_interface *); 98192984Sthompsastatic void usb_linux_complete(struct usb_xfer *); 99185948Sthompsastatic int usb_unlink_urb_sub(struct urb *, uint8_t); 100184610Salfred 101184610Salfred/*------------------------------------------------------------------------* 102184610Salfred * FreeBSD USB interface 103184610Salfred *------------------------------------------------------------------------*/ 104184610Salfred 105184610Salfredstatic LIST_HEAD(, usb_linux_softc) usb_linux_attached_list; 106184610Salfredstatic LIST_HEAD(, usb_driver) usb_linux_driver_list; 107184610Salfred 108184610Salfredstatic device_method_t usb_linux_methods[] = { 109184610Salfred /* Device interface */ 110184610Salfred DEVMETHOD(device_probe, usb_linux_probe), 111184610Salfred DEVMETHOD(device_attach, usb_linux_attach), 112184610Salfred DEVMETHOD(device_detach, usb_linux_detach), 113184610Salfred DEVMETHOD(device_suspend, usb_linux_suspend), 114184610Salfred DEVMETHOD(device_resume, usb_linux_resume), 115184610Salfred 116246128Ssbz DEVMETHOD_END 117184610Salfred}; 118184610Salfred 119184610Salfredstatic driver_t usb_linux_driver = { 120184610Salfred .name = "usb_linux", 121184610Salfred .methods = usb_linux_methods, 122184610Salfred .size = sizeof(struct usb_linux_softc), 123184610Salfred}; 124184610Salfred 125184610Salfredstatic devclass_t usb_linux_devclass; 126184610Salfred 127189275SthompsaDRIVER_MODULE(usb_linux, uhub, usb_linux_driver, usb_linux_devclass, NULL, 0); 128212122SthompsaMODULE_VERSION(usb_linux, 1); 129184610Salfred 130184610Salfred/*------------------------------------------------------------------------* 131184610Salfred * usb_linux_lookup_id 132184610Salfred * 133184610Salfred * This functions takes an array of "struct usb_device_id" and tries 134192984Sthompsa * to match the entries with the information in "struct usb_attach_arg". 135184610Salfred * If it finds a match the matching entry will be returned. 136184610Salfred * Else "NULL" will be returned. 137184610Salfred *------------------------------------------------------------------------*/ 138184610Salfredstatic const struct usb_device_id * 139192984Sthompsausb_linux_lookup_id(const struct usb_device_id *id, struct usb_attach_arg *uaa) 140184610Salfred{ 141184610Salfred if (id == NULL) { 142184610Salfred goto done; 143184610Salfred } 144184610Salfred /* 145184610Salfred * Keep on matching array entries until we find one with 146184610Salfred * "match_flags" equal to zero, which indicates the end of the 147184610Salfred * array: 148184610Salfred */ 149184610Salfred for (; id->match_flags; id++) { 150184610Salfred 151184610Salfred if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) && 152184610Salfred (id->idVendor != uaa->info.idVendor)) { 153184610Salfred continue; 154184610Salfred } 155184610Salfred if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) && 156184610Salfred (id->idProduct != uaa->info.idProduct)) { 157184610Salfred continue; 158184610Salfred } 159184610Salfred if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) && 160184610Salfred (id->bcdDevice_lo > uaa->info.bcdDevice)) { 161184610Salfred continue; 162184610Salfred } 163184610Salfred if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) && 164184610Salfred (id->bcdDevice_hi < uaa->info.bcdDevice)) { 165184610Salfred continue; 166184610Salfred } 167184610Salfred if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) && 168184610Salfred (id->bDeviceClass != uaa->info.bDeviceClass)) { 169184610Salfred continue; 170184610Salfred } 171184610Salfred if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) && 172184610Salfred (id->bDeviceSubClass != uaa->info.bDeviceSubClass)) { 173184610Salfred continue; 174184610Salfred } 175184610Salfred if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) && 176184610Salfred (id->bDeviceProtocol != uaa->info.bDeviceProtocol)) { 177184610Salfred continue; 178184610Salfred } 179184610Salfred if ((uaa->info.bDeviceClass == 0xFF) && 180184610Salfred !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) && 181184610Salfred (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS | 182184610Salfred USB_DEVICE_ID_MATCH_INT_SUBCLASS | 183184610Salfred USB_DEVICE_ID_MATCH_INT_PROTOCOL))) { 184184610Salfred continue; 185184610Salfred } 186184610Salfred if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) && 187184610Salfred (id->bInterfaceClass != uaa->info.bInterfaceClass)) { 188184610Salfred continue; 189184610Salfred } 190184610Salfred if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) && 191184610Salfred (id->bInterfaceSubClass != uaa->info.bInterfaceSubClass)) { 192184610Salfred continue; 193184610Salfred } 194184610Salfred if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) && 195184610Salfred (id->bInterfaceProtocol != uaa->info.bInterfaceProtocol)) { 196184610Salfred continue; 197184610Salfred } 198184610Salfred /* we found a match! */ 199184610Salfred return (id); 200184610Salfred } 201184610Salfred 202184610Salfreddone: 203184610Salfred return (NULL); 204184610Salfred} 205184610Salfred 206184610Salfred/*------------------------------------------------------------------------* 207184610Salfred * usb_linux_probe 208184610Salfred * 209184610Salfred * This function is the FreeBSD probe callback. It is called from the 210184610Salfred * FreeBSD USB stack through the "device_probe_and_attach()" function. 211184610Salfred *------------------------------------------------------------------------*/ 212184610Salfredstatic int 213184610Salfredusb_linux_probe(device_t dev) 214184610Salfred{ 215192984Sthompsa struct usb_attach_arg *uaa = device_get_ivars(dev); 216184610Salfred struct usb_driver *udrv; 217184610Salfred int err = ENXIO; 218184610Salfred 219192499Sthompsa if (uaa->usb_mode != USB_MODE_HOST) { 220184610Salfred return (ENXIO); 221184610Salfred } 222196403Sjhb mtx_lock(&Giant); 223184610Salfred LIST_FOREACH(udrv, &usb_linux_driver_list, linux_driver_list) { 224184610Salfred if (usb_linux_lookup_id(udrv->id_table, uaa)) { 225184610Salfred err = 0; 226184610Salfred break; 227184610Salfred } 228184610Salfred } 229196403Sjhb mtx_unlock(&Giant); 230184610Salfred 231184610Salfred return (err); 232184610Salfred} 233184610Salfred 234184610Salfred/*------------------------------------------------------------------------* 235184610Salfred * usb_linux_get_usb_driver 236184610Salfred * 237184610Salfred * This function returns the pointer to the "struct usb_driver" where 238184610Salfred * the Linux USB device driver "struct usb_device_id" match was found. 239184610Salfred * We apply a lock before reading out the pointer to avoid races. 240184610Salfred *------------------------------------------------------------------------*/ 241184610Salfredstatic struct usb_driver * 242184610Salfredusb_linux_get_usb_driver(struct usb_linux_softc *sc) 243184610Salfred{ 244184610Salfred struct usb_driver *udrv; 245184610Salfred 246196403Sjhb mtx_lock(&Giant); 247184610Salfred udrv = sc->sc_udrv; 248196403Sjhb mtx_unlock(&Giant); 249184610Salfred return (udrv); 250184610Salfred} 251184610Salfred 252184610Salfred/*------------------------------------------------------------------------* 253184610Salfred * usb_linux_attach 254184610Salfred * 255184610Salfred * This function is the FreeBSD attach callback. It is called from the 256184610Salfred * FreeBSD USB stack through the "device_probe_and_attach()" function. 257184610Salfred * This function is called when "usb_linux_probe()" returns zero. 258184610Salfred *------------------------------------------------------------------------*/ 259184610Salfredstatic int 260184610Salfredusb_linux_attach(device_t dev) 261184610Salfred{ 262192984Sthompsa struct usb_attach_arg *uaa = device_get_ivars(dev); 263184610Salfred struct usb_linux_softc *sc = device_get_softc(dev); 264184610Salfred struct usb_driver *udrv; 265184610Salfred const struct usb_device_id *id = NULL; 266184610Salfred 267196403Sjhb mtx_lock(&Giant); 268184610Salfred LIST_FOREACH(udrv, &usb_linux_driver_list, linux_driver_list) { 269184610Salfred id = usb_linux_lookup_id(udrv->id_table, uaa); 270184610Salfred if (id) 271184610Salfred break; 272184610Salfred } 273196403Sjhb mtx_unlock(&Giant); 274184610Salfred 275184610Salfred if (id == NULL) { 276184610Salfred return (ENXIO); 277184610Salfred } 278192984Sthompsa if (usb_linux_create_usb_device(uaa->device, dev) != 0) 279192984Sthompsa return (ENOMEM); 280194228Sthompsa device_set_usb_desc(dev); 281184610Salfred 282184610Salfred sc->sc_fbsd_udev = uaa->device; 283184610Salfred sc->sc_fbsd_dev = dev; 284184610Salfred sc->sc_udrv = udrv; 285192984Sthompsa sc->sc_ui = usb_ifnum_to_if(uaa->device, uaa->info.bIfaceNum); 286184610Salfred if (sc->sc_ui == NULL) { 287184610Salfred return (EINVAL); 288184610Salfred } 289184610Salfred if (udrv->probe) { 290184610Salfred if ((udrv->probe) (sc->sc_ui, id)) { 291184610Salfred return (ENXIO); 292184610Salfred } 293184610Salfred } 294196403Sjhb mtx_lock(&Giant); 295184610Salfred LIST_INSERT_HEAD(&usb_linux_attached_list, sc, sc_attached_list); 296196403Sjhb mtx_unlock(&Giant); 297184610Salfred 298184610Salfred /* success */ 299184610Salfred return (0); 300184610Salfred} 301184610Salfred 302184610Salfred/*------------------------------------------------------------------------* 303184610Salfred * usb_linux_detach 304184610Salfred * 305184610Salfred * This function is the FreeBSD detach callback. It is called from the 306184610Salfred * FreeBSD USB stack through the "device_detach()" function. 307184610Salfred *------------------------------------------------------------------------*/ 308184610Salfredstatic int 309184610Salfredusb_linux_detach(device_t dev) 310184610Salfred{ 311184610Salfred struct usb_linux_softc *sc = device_get_softc(dev); 312184610Salfred struct usb_driver *udrv = NULL; 313184610Salfred 314196403Sjhb mtx_lock(&Giant); 315184610Salfred if (sc->sc_attached_list.le_prev) { 316184610Salfred LIST_REMOVE(sc, sc_attached_list); 317184610Salfred sc->sc_attached_list.le_prev = NULL; 318184610Salfred udrv = sc->sc_udrv; 319184610Salfred sc->sc_udrv = NULL; 320184610Salfred } 321196403Sjhb mtx_unlock(&Giant); 322184610Salfred 323184610Salfred if (udrv && udrv->disconnect) { 324184610Salfred (udrv->disconnect) (sc->sc_ui); 325184610Salfred } 326184610Salfred /* 327184610Salfred * Make sure that we free all FreeBSD USB transfers belonging to 328184610Salfred * this Linux "usb_interface", hence they will most likely not be 329184610Salfred * needed any more. 330184610Salfred */ 331192984Sthompsa usb_linux_cleanup_interface(sc->sc_fbsd_udev, sc->sc_ui); 332184610Salfred return (0); 333184610Salfred} 334184610Salfred 335184610Salfred/*------------------------------------------------------------------------* 336184610Salfred * usb_linux_suspend 337184610Salfred * 338184610Salfred * This function is the FreeBSD suspend callback. Usually it does nothing. 339184610Salfred *------------------------------------------------------------------------*/ 340184610Salfredstatic int 341184610Salfredusb_linux_suspend(device_t dev) 342184610Salfred{ 343184610Salfred struct usb_linux_softc *sc = device_get_softc(dev); 344184610Salfred struct usb_driver *udrv = usb_linux_get_usb_driver(sc); 345184610Salfred int err; 346184610Salfred 347184610Salfred if (udrv && udrv->suspend) { 348184610Salfred err = (udrv->suspend) (sc->sc_ui, 0); 349184610Salfred } 350184610Salfred return (0); 351184610Salfred} 352184610Salfred 353184610Salfred/*------------------------------------------------------------------------* 354184610Salfred * usb_linux_resume 355184610Salfred * 356184610Salfred * This function is the FreeBSD resume callback. Usually it does nothing. 357184610Salfred *------------------------------------------------------------------------*/ 358184610Salfredstatic int 359184610Salfredusb_linux_resume(device_t dev) 360184610Salfred{ 361184610Salfred struct usb_linux_softc *sc = device_get_softc(dev); 362184610Salfred struct usb_driver *udrv = usb_linux_get_usb_driver(sc); 363184610Salfred int err; 364184610Salfred 365184610Salfred if (udrv && udrv->resume) { 366184610Salfred err = (udrv->resume) (sc->sc_ui); 367184610Salfred } 368184610Salfred return (0); 369184610Salfred} 370184610Salfred 371184610Salfred/*------------------------------------------------------------------------* 372184610Salfred * Linux emulation layer 373184610Salfred *------------------------------------------------------------------------*/ 374184610Salfred 375184610Salfred/*------------------------------------------------------------------------* 376184610Salfred * usb_max_isoc_frames 377184610Salfred * 378184610Salfred * The following function returns the maximum number of isochronous 379184610Salfred * frames that we support per URB. It is not part of the Linux USB API. 380184610Salfred *------------------------------------------------------------------------*/ 381184610Salfredstatic uint16_t 382184610Salfredusb_max_isoc_frames(struct usb_device *dev) 383184610Salfred{ 384187180Sthompsa ; /* indent fix */ 385194228Sthompsa switch (usbd_get_speed(dev)) { 386187180Sthompsa case USB_SPEED_LOW: 387187180Sthompsa case USB_SPEED_FULL: 388187180Sthompsa return (USB_MAX_FULL_SPEED_ISOC_FRAMES); 389187180Sthompsa default: 390187180Sthompsa return (USB_MAX_HIGH_SPEED_ISOC_FRAMES); 391187180Sthompsa } 392184610Salfred} 393184610Salfred 394184610Salfred/*------------------------------------------------------------------------* 395184610Salfred * usb_submit_urb 396184610Salfred * 397184610Salfred * This function is used to queue an URB after that it has been 398184610Salfred * initialized. If it returns non-zero, it means that the URB was not 399184610Salfred * queued. 400184610Salfred *------------------------------------------------------------------------*/ 401184610Salfredint 402184610Salfredusb_submit_urb(struct urb *urb, uint16_t mem_flags) 403184610Salfred{ 404184610Salfred struct usb_host_endpoint *uhe; 405195966Salfred uint8_t do_unlock; 406195966Salfred int err; 407184610Salfred 408195966Salfred if (urb == NULL) 409184610Salfred return (-EINVAL); 410184610Salfred 411195966Salfred do_unlock = mtx_owned(&Giant) ? 0 : 1; 412195966Salfred if (do_unlock) 413195966Salfred mtx_lock(&Giant); 414195966Salfred 415193644Sthompsa if (urb->endpoint == NULL) { 416195966Salfred err = -EINVAL; 417195966Salfred goto done; 418184610Salfred } 419195966Salfred 420195966Salfred /* 421331756Semaste * Check to see if the urb is in the process of being killed 422331756Semaste * and stop a urb that is in the process of being killed from 423331756Semaste * being re-submitted (e.g. from its completion callback 424331756Semaste * function). 425331756Semaste */ 426195966Salfred if (urb->kill_count != 0) { 427195966Salfred err = -EPERM; 428195966Salfred goto done; 429195966Salfred } 430195966Salfred 431193644Sthompsa uhe = urb->endpoint; 432184610Salfred 433184610Salfred /* 434184610Salfred * Check that we have got a FreeBSD USB transfer that will dequeue 435184610Salfred * the URB structure and do the real transfer. If there are no USB 436184610Salfred * transfers, then we return an error. 437184610Salfred */ 438184610Salfred if (uhe->bsd_xfer[0] || 439184610Salfred uhe->bsd_xfer[1]) { 440184610Salfred /* we are ready! */ 441184610Salfred 442205803Sthompsa TAILQ_INSERT_TAIL(&uhe->bsd_urb_list, urb, bsd_urb_list); 443184610Salfred 444184610Salfred urb->status = -EINPROGRESS; 445184610Salfred 446194228Sthompsa usbd_transfer_start(uhe->bsd_xfer[0]); 447194228Sthompsa usbd_transfer_start(uhe->bsd_xfer[1]); 448195966Salfred err = 0; 449184610Salfred } else { 450184610Salfred /* no pipes have been setup yet! */ 451184610Salfred urb->status = -EINVAL; 452195966Salfred err = -EINVAL; 453184610Salfred } 454195966Salfreddone: 455195966Salfred if (do_unlock) 456195966Salfred mtx_unlock(&Giant); 457195966Salfred return (err); 458184610Salfred} 459184610Salfred 460184610Salfred/*------------------------------------------------------------------------* 461184610Salfred * usb_unlink_urb 462184610Salfred * 463184610Salfred * This function is used to stop an URB after that it is been 464184610Salfred * submitted, but before the "complete" callback has been called. On 465184610Salfred *------------------------------------------------------------------------*/ 466184610Salfredint 467184610Salfredusb_unlink_urb(struct urb *urb) 468184610Salfred{ 469184610Salfred return (usb_unlink_urb_sub(urb, 0)); 470184610Salfred} 471184610Salfred 472184610Salfredstatic void 473192984Sthompsausb_unlink_bsd(struct usb_xfer *xfer, 474184610Salfred struct urb *urb, uint8_t drain) 475184610Salfred{ 476195966Salfred if (xfer == NULL) 477195966Salfred return; 478195966Salfred if (!usbd_transfer_pending(xfer)) 479195966Salfred return; 480195966Salfred if (xfer->priv_fifo == (void *)urb) { 481196403Sjhb if (drain) { 482196403Sjhb mtx_unlock(&Giant); 483194228Sthompsa usbd_transfer_drain(xfer); 484196403Sjhb mtx_lock(&Giant); 485196403Sjhb } else { 486194228Sthompsa usbd_transfer_stop(xfer); 487196403Sjhb } 488194228Sthompsa usbd_transfer_start(xfer); 489184610Salfred } 490184610Salfred} 491184610Salfred 492184610Salfredstatic int 493184610Salfredusb_unlink_urb_sub(struct urb *urb, uint8_t drain) 494184610Salfred{ 495184610Salfred struct usb_host_endpoint *uhe; 496184610Salfred uint16_t x; 497195966Salfred uint8_t do_unlock; 498195966Salfred int err; 499184610Salfred 500195966Salfred if (urb == NULL) 501184610Salfred return (-EINVAL); 502184610Salfred 503195966Salfred do_unlock = mtx_owned(&Giant) ? 0 : 1; 504195966Salfred if (do_unlock) 505195966Salfred mtx_lock(&Giant); 506195966Salfred if (drain) 507195966Salfred urb->kill_count++; 508195966Salfred 509193644Sthompsa if (urb->endpoint == NULL) { 510195966Salfred err = -EINVAL; 511195966Salfred goto done; 512184610Salfred } 513193644Sthompsa uhe = urb->endpoint; 514184610Salfred 515184610Salfred if (urb->bsd_urb_list.tqe_prev) { 516184610Salfred 517184610Salfred /* not started yet, just remove it from the queue */ 518184610Salfred TAILQ_REMOVE(&uhe->bsd_urb_list, urb, bsd_urb_list); 519184610Salfred urb->bsd_urb_list.tqe_prev = NULL; 520184610Salfred urb->status = -ECONNRESET; 521184610Salfred urb->actual_length = 0; 522184610Salfred 523184610Salfred for (x = 0; x < urb->number_of_packets; x++) { 524184610Salfred urb->iso_frame_desc[x].actual_length = 0; 525184610Salfred } 526184610Salfred 527184610Salfred if (urb->complete) { 528184610Salfred (urb->complete) (urb); 529184610Salfred } 530184610Salfred } else { 531184610Salfred 532184610Salfred /* 533184610Salfred * If the URB is not on the URB list, then check if one of 534184610Salfred * the FreeBSD USB transfer are processing the current URB. 535184610Salfred * If so, re-start that transfer, which will lead to the 536184610Salfred * termination of that URB: 537184610Salfred */ 538184610Salfred usb_unlink_bsd(uhe->bsd_xfer[0], urb, drain); 539184610Salfred usb_unlink_bsd(uhe->bsd_xfer[1], urb, drain); 540184610Salfred } 541195966Salfred err = 0; 542195966Salfreddone: 543195966Salfred if (drain) 544195966Salfred urb->kill_count--; 545195966Salfred if (do_unlock) 546195966Salfred mtx_unlock(&Giant); 547195966Salfred return (err); 548184610Salfred} 549184610Salfred 550184610Salfred/*------------------------------------------------------------------------* 551184610Salfred * usb_clear_halt 552184610Salfred * 553184610Salfred * This function must always be used to clear the stall. Stall is when 554184610Salfred * an USB endpoint returns a stall message to the USB host controller. 555184610Salfred * Until the stall is cleared, no data can be transferred. 556184610Salfred *------------------------------------------------------------------------*/ 557184610Salfredint 558184610Salfredusb_clear_halt(struct usb_device *dev, struct usb_host_endpoint *uhe) 559184610Salfred{ 560192984Sthompsa struct usb_config cfg[1]; 561193644Sthompsa struct usb_endpoint *ep; 562184610Salfred uint8_t type; 563184610Salfred uint8_t addr; 564184610Salfred 565184610Salfred if (uhe == NULL) 566184610Salfred return (-EINVAL); 567184610Salfred 568184610Salfred type = uhe->desc.bmAttributes & UE_XFERTYPE; 569184610Salfred addr = uhe->desc.bEndpointAddress; 570184610Salfred 571227461Shselasky memset(cfg, 0, sizeof(cfg)); 572184610Salfred 573184610Salfred cfg[0].type = type; 574184610Salfred cfg[0].endpoint = addr & UE_ADDR; 575184610Salfred cfg[0].direction = addr & (UE_DIR_OUT | UE_DIR_IN); 576184610Salfred 577194228Sthompsa ep = usbd_get_endpoint(dev, uhe->bsd_iface_index, cfg); 578193644Sthompsa if (ep == NULL) 579184610Salfred return (-EINVAL); 580184610Salfred 581194228Sthompsa usbd_clear_data_toggle(dev, ep); 582184610Salfred 583184610Salfred return (usb_control_msg(dev, &dev->ep0, 584184610Salfred UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT, 585184610Salfred UF_ENDPOINT_HALT, addr, NULL, 0, 1000)); 586184610Salfred} 587184610Salfred 588184610Salfred/*------------------------------------------------------------------------* 589184610Salfred * usb_start_wait_urb 590184610Salfred * 591184610Salfred * This is an internal function that is used to perform synchronous 592184610Salfred * Linux USB transfers. 593184610Salfred *------------------------------------------------------------------------*/ 594184610Salfredstatic int 595193045Sthompsausb_start_wait_urb(struct urb *urb, usb_timeout_t timeout, uint16_t *p_actlen) 596184610Salfred{ 597184610Salfred int err; 598195966Salfred uint8_t do_unlock; 599184610Salfred 600184610Salfred /* you must have a timeout! */ 601184610Salfred if (timeout == 0) { 602184610Salfred timeout = 1; 603184610Salfred } 604184610Salfred urb->complete = &usb_linux_wait_complete; 605184610Salfred urb->timeout = timeout; 606184610Salfred urb->transfer_flags |= URB_WAIT_WAKEUP; 607184610Salfred urb->transfer_flags &= ~URB_IS_SLEEPING; 608184610Salfred 609195966Salfred do_unlock = mtx_owned(&Giant) ? 0 : 1; 610195966Salfred if (do_unlock) 611195966Salfred mtx_lock(&Giant); 612184610Salfred err = usb_submit_urb(urb, 0); 613184610Salfred if (err) 614184610Salfred goto done; 615184610Salfred 616184610Salfred /* 617184610Salfred * the URB might have completed before we get here, so check that by 618184610Salfred * using some flags! 619184610Salfred */ 620184610Salfred while (urb->transfer_flags & URB_WAIT_WAKEUP) { 621184610Salfred urb->transfer_flags |= URB_IS_SLEEPING; 622194227Sthompsa cv_wait(&urb->cv_wait, &Giant); 623184610Salfred urb->transfer_flags &= ~URB_IS_SLEEPING; 624184610Salfred } 625184610Salfred 626184610Salfred err = urb->status; 627184610Salfred 628184610Salfreddone: 629195966Salfred if (do_unlock) 630195966Salfred mtx_unlock(&Giant); 631198776Sthompsa if (p_actlen != NULL) { 632198776Sthompsa if (err) 633198776Sthompsa *p_actlen = 0; 634198776Sthompsa else 635198776Sthompsa *p_actlen = urb->actual_length; 636184610Salfred } 637184610Salfred return (err); 638184610Salfred} 639184610Salfred 640184610Salfred/*------------------------------------------------------------------------* 641184610Salfred * usb_control_msg 642184610Salfred * 643184610Salfred * The following function performs a control transfer sequence one any 644184610Salfred * control, bulk or interrupt endpoint, specified by "uhe". A control 645184610Salfred * transfer means that you transfer an 8-byte header first followed by 646184610Salfred * a data-phase as indicated by the 8-byte header. The "timeout" is 647184610Salfred * given in milliseconds. 648184610Salfred * 649184610Salfred * Return values: 650184610Salfred * 0: Success 651184610Salfred * < 0: Failure 652298829Spfg * > 0: Actual length 653184610Salfred *------------------------------------------------------------------------*/ 654184610Salfredint 655184610Salfredusb_control_msg(struct usb_device *dev, struct usb_host_endpoint *uhe, 656184610Salfred uint8_t request, uint8_t requesttype, 657184610Salfred uint16_t value, uint16_t index, void *data, 658193045Sthompsa uint16_t size, usb_timeout_t timeout) 659184610Salfred{ 660192984Sthompsa struct usb_device_request req; 661184610Salfred struct urb *urb; 662184610Salfred int err; 663184610Salfred uint16_t actlen; 664184610Salfred uint8_t type; 665184610Salfred uint8_t addr; 666184610Salfred 667184610Salfred req.bmRequestType = requesttype; 668184610Salfred req.bRequest = request; 669184610Salfred USETW(req.wValue, value); 670184610Salfred USETW(req.wIndex, index); 671184610Salfred USETW(req.wLength, size); 672184610Salfred 673184610Salfred if (uhe == NULL) { 674184610Salfred return (-EINVAL); 675184610Salfred } 676184610Salfred type = (uhe->desc.bmAttributes & UE_XFERTYPE); 677184610Salfred addr = (uhe->desc.bEndpointAddress & UE_ADDR); 678184610Salfred 679184610Salfred if (type != UE_CONTROL) { 680184610Salfred return (-EINVAL); 681184610Salfred } 682184610Salfred if (addr == 0) { 683184610Salfred /* 684184610Salfred * The FreeBSD USB stack supports standard control 685184610Salfred * transfers on control endpoint zero: 686184610Salfred */ 687194228Sthompsa err = usbd_do_request_flags(dev, 688195966Salfred NULL, &req, data, USB_SHORT_XFER_OK, 689184610Salfred &actlen, timeout); 690184610Salfred if (err) { 691184610Salfred err = -EPIPE; 692184610Salfred } else { 693184610Salfred err = actlen; 694184610Salfred } 695184610Salfred return (err); 696184610Salfred } 697192984Sthompsa if (dev->flags.usb_mode != USB_MODE_HOST) { 698184610Salfred /* not supported */ 699184610Salfred return (-EINVAL); 700184610Salfred } 701184610Salfred err = usb_setup_endpoint(dev, uhe, 1 /* dummy */ ); 702184610Salfred 703184610Salfred /* 704184610Salfred * NOTE: we need to allocate real memory here so that we don't 705184610Salfred * transfer data to/from the stack! 706184610Salfred * 707184610Salfred * 0xFFFF is a FreeBSD specific magic value. 708184610Salfred */ 709184610Salfred urb = usb_alloc_urb(0xFFFF, size); 710184610Salfred 711184610Salfred urb->dev = dev; 712193644Sthompsa urb->endpoint = uhe; 713184610Salfred 714227461Shselasky memcpy(urb->setup_packet, &req, sizeof(req)); 715184610Salfred 716184610Salfred if (size && (!(req.bmRequestType & UT_READ))) { 717184610Salfred /* move the data to a real buffer */ 718227461Shselasky memcpy(USB_ADD_BYTES(urb->setup_packet, sizeof(req)), 719227461Shselasky data, size); 720184610Salfred } 721184610Salfred err = usb_start_wait_urb(urb, timeout, &actlen); 722184610Salfred 723184610Salfred if (req.bmRequestType & UT_READ) { 724184610Salfred if (actlen) { 725184610Salfred bcopy(USB_ADD_BYTES(urb->setup_packet, 726184610Salfred sizeof(req)), data, actlen); 727184610Salfred } 728184610Salfred } 729184610Salfred usb_free_urb(urb); 730184610Salfred 731184610Salfred if (err == 0) { 732184610Salfred err = actlen; 733184610Salfred } 734184610Salfred return (err); 735184610Salfred} 736184610Salfred 737184610Salfred/*------------------------------------------------------------------------* 738184610Salfred * usb_set_interface 739184610Salfred * 740184610Salfred * The following function will select which alternate setting of an 741184610Salfred * USB interface you plan to use. By default alternate setting with 742184610Salfred * index zero is selected. Note that "iface_no" is not the interface 743184610Salfred * index, but rather the value of "bInterfaceNumber". 744184610Salfred *------------------------------------------------------------------------*/ 745184610Salfredint 746184610Salfredusb_set_interface(struct usb_device *dev, uint8_t iface_no, uint8_t alt_index) 747184610Salfred{ 748184610Salfred struct usb_interface *p_ui = usb_ifnum_to_if(dev, iface_no); 749184610Salfred int err; 750184610Salfred 751184610Salfred if (p_ui == NULL) 752184610Salfred return (-EINVAL); 753184610Salfred if (alt_index >= p_ui->num_altsetting) 754184610Salfred return (-EINVAL); 755184610Salfred usb_linux_cleanup_interface(dev, p_ui); 756194228Sthompsa err = -usbd_set_alt_interface_index(dev, 757184610Salfred p_ui->bsd_iface_index, alt_index); 758184610Salfred if (err == 0) { 759184610Salfred p_ui->cur_altsetting = p_ui->altsetting + alt_index; 760184610Salfred } 761184610Salfred return (err); 762184610Salfred} 763184610Salfred 764184610Salfred/*------------------------------------------------------------------------* 765184610Salfred * usb_setup_endpoint 766184610Salfred * 767184610Salfred * The following function is an extension to the Linux USB API that 768184610Salfred * allows you to set a maximum buffer size for a given USB endpoint. 769184610Salfred * The maximum buffer size is per URB. If you don't call this function 770184610Salfred * to set a maximum buffer size, the endpoint will not be functional. 771184610Salfred * Note that for isochronous endpoints the maximum buffer size must be 772184610Salfred * a non-zero dummy, hence this function will base the maximum buffer 773184610Salfred * size on "wMaxPacketSize". 774184610Salfred *------------------------------------------------------------------------*/ 775184610Salfredint 776184610Salfredusb_setup_endpoint(struct usb_device *dev, 777193074Sthompsa struct usb_host_endpoint *uhe, usb_size_t bufsize) 778184610Salfred{ 779192984Sthompsa struct usb_config cfg[2]; 780184610Salfred uint8_t type = uhe->desc.bmAttributes & UE_XFERTYPE; 781184610Salfred uint8_t addr = uhe->desc.bEndpointAddress; 782184610Salfred 783184610Salfred if (uhe->fbsd_buf_size == bufsize) { 784184610Salfred /* optimize */ 785184610Salfred return (0); 786184610Salfred } 787194228Sthompsa usbd_transfer_unsetup(uhe->bsd_xfer, 2); 788184610Salfred 789184610Salfred uhe->fbsd_buf_size = bufsize; 790184610Salfred 791184610Salfred if (bufsize == 0) { 792184610Salfred return (0); 793184610Salfred } 794227461Shselasky memset(cfg, 0, sizeof(cfg)); 795184610Salfred 796184610Salfred if (type == UE_ISOCHRONOUS) { 797184610Salfred 798184610Salfred /* 799184610Salfred * Isochronous transfers are special in that they don't fit 800184610Salfred * into the BULK/INTR/CONTROL transfer model. 801184610Salfred */ 802184610Salfred 803184610Salfred cfg[0].type = type; 804184610Salfred cfg[0].endpoint = addr & UE_ADDR; 805184610Salfred cfg[0].direction = addr & (UE_DIR_OUT | UE_DIR_IN); 806190734Sthompsa cfg[0].callback = &usb_linux_isoc_callback; 807190734Sthompsa cfg[0].bufsize = 0; /* use wMaxPacketSize */ 808190734Sthompsa cfg[0].frames = usb_max_isoc_frames(dev); 809190734Sthompsa cfg[0].flags.proxy_buffer = 1; 810184610Salfred#if 0 811184610Salfred /* 812184610Salfred * The Linux USB API allows non back-to-back 813184610Salfred * isochronous frames which we do not support. If the 814184610Salfred * isochronous frames are not back-to-back we need to 815184610Salfred * do a copy, and then we need a buffer for 816184610Salfred * that. Enable this at your own risk. 817184610Salfred */ 818190734Sthompsa cfg[0].flags.ext_buffer = 1; 819184610Salfred#endif 820190734Sthompsa cfg[0].flags.short_xfer_ok = 1; 821184610Salfred 822184610Salfred bcopy(cfg, cfg + 1, sizeof(*cfg)); 823184610Salfred 824184610Salfred /* Allocate and setup two generic FreeBSD USB transfers */ 825184610Salfred 826194228Sthompsa if (usbd_transfer_setup(dev, &uhe->bsd_iface_index, 827184610Salfred uhe->bsd_xfer, cfg, 2, uhe, &Giant)) { 828184610Salfred return (-EINVAL); 829184610Salfred } 830184610Salfred } else { 831184610Salfred if (bufsize > (1 << 22)) { 832184610Salfred /* limit buffer size */ 833184610Salfred bufsize = (1 << 22); 834184610Salfred } 835184610Salfred /* Allocate and setup one generic FreeBSD USB transfer */ 836184610Salfred 837184610Salfred cfg[0].type = type; 838184610Salfred cfg[0].endpoint = addr & UE_ADDR; 839184610Salfred cfg[0].direction = addr & (UE_DIR_OUT | UE_DIR_IN); 840190734Sthompsa cfg[0].callback = &usb_linux_non_isoc_callback; 841190734Sthompsa cfg[0].bufsize = bufsize; 842190734Sthompsa cfg[0].flags.ext_buffer = 1; /* enable zero-copy */ 843190734Sthompsa cfg[0].flags.proxy_buffer = 1; 844190734Sthompsa cfg[0].flags.short_xfer_ok = 1; 845184610Salfred 846194228Sthompsa if (usbd_transfer_setup(dev, &uhe->bsd_iface_index, 847184610Salfred uhe->bsd_xfer, cfg, 1, uhe, &Giant)) { 848184610Salfred return (-EINVAL); 849184610Salfred } 850184610Salfred } 851184610Salfred return (0); 852184610Salfred} 853184610Salfred 854184610Salfred/*------------------------------------------------------------------------* 855184610Salfred * usb_linux_create_usb_device 856184610Salfred * 857184610Salfred * The following function is used to build up a per USB device 858184610Salfred * structure tree, that mimics the Linux one. The root structure 859184610Salfred * is returned by this function. 860184610Salfred *------------------------------------------------------------------------*/ 861192984Sthompsastatic int 862192984Sthompsausb_linux_create_usb_device(struct usb_device *udev, device_t dev) 863184610Salfred{ 864194228Sthompsa struct usb_config_descriptor *cd = usbd_get_config_descriptor(udev); 865192984Sthompsa struct usb_descriptor *desc; 866192984Sthompsa struct usb_interface_descriptor *id; 867192984Sthompsa struct usb_endpoint_descriptor *ed; 868184610Salfred struct usb_interface *p_ui = NULL; 869184610Salfred struct usb_host_interface *p_uhi = NULL; 870184610Salfred struct usb_host_endpoint *p_uhe = NULL; 871193074Sthompsa usb_size_t size; 872184610Salfred uint16_t niface_total; 873184610Salfred uint16_t nedesc; 874184610Salfred uint16_t iface_no_curr; 875184610Salfred uint16_t iface_index; 876184610Salfred uint8_t pass; 877184610Salfred uint8_t iface_no; 878184610Salfred 879184610Salfred /* 880184610Salfred * We do two passes. One pass for computing necessary memory size 881184610Salfred * and one pass to initialize all the allocated memory structures. 882184610Salfred */ 883184610Salfred for (pass = 0; pass < 2; pass++) { 884184610Salfred 885233774Shselasky iface_no_curr = 0xFFFF; 886184610Salfred niface_total = 0; 887184610Salfred iface_index = 0; 888184610Salfred nedesc = 0; 889184610Salfred desc = NULL; 890184610Salfred 891184610Salfred /* 892184610Salfred * Iterate over all the USB descriptors. Use the USB config 893184610Salfred * descriptor pointer provided by the FreeBSD USB stack. 894184610Salfred */ 895194228Sthompsa while ((desc = usb_desc_foreach(cd, desc))) { 896184610Salfred 897184610Salfred /* 898184610Salfred * Build up a tree according to the descriptors we 899184610Salfred * find: 900184610Salfred */ 901184610Salfred switch (desc->bDescriptorType) { 902184610Salfred case UDESC_DEVICE: 903184610Salfred break; 904184610Salfred 905184610Salfred case UDESC_ENDPOINT: 906184610Salfred ed = (void *)desc; 907184610Salfred if ((ed->bLength < sizeof(*ed)) || 908184610Salfred (iface_index == 0)) 909184610Salfred break; 910184610Salfred if (p_uhe) { 911184610Salfred bcopy(ed, &p_uhe->desc, sizeof(p_uhe->desc)); 912184610Salfred p_uhe->bsd_iface_index = iface_index - 1; 913205803Sthompsa TAILQ_INIT(&p_uhe->bsd_urb_list); 914184610Salfred p_uhe++; 915184610Salfred } 916184610Salfred if (p_uhi) { 917184610Salfred (p_uhi - 1)->desc.bNumEndpoints++; 918184610Salfred } 919184610Salfred nedesc++; 920184610Salfred break; 921184610Salfred 922184610Salfred case UDESC_INTERFACE: 923184610Salfred id = (void *)desc; 924184610Salfred if (id->bLength < sizeof(*id)) 925184610Salfred break; 926184610Salfred if (p_uhi) { 927184610Salfred bcopy(id, &p_uhi->desc, sizeof(p_uhi->desc)); 928184610Salfred p_uhi->desc.bNumEndpoints = 0; 929184610Salfred p_uhi->endpoint = p_uhe; 930184610Salfred p_uhi->string = ""; 931184610Salfred p_uhi->bsd_iface_index = iface_index; 932184610Salfred p_uhi++; 933184610Salfred } 934184610Salfred iface_no = id->bInterfaceNumber; 935184610Salfred niface_total++; 936184610Salfred if (iface_no_curr != iface_no) { 937184610Salfred if (p_ui) { 938184610Salfred p_ui->altsetting = p_uhi - 1; 939184610Salfred p_ui->cur_altsetting = p_uhi - 1; 940184610Salfred p_ui->bsd_iface_index = iface_index; 941192984Sthompsa p_ui->linux_udev = udev; 942184610Salfred p_ui++; 943184610Salfred } 944184610Salfred iface_no_curr = iface_no; 945184610Salfred iface_index++; 946184610Salfred } 947184610Salfred break; 948184610Salfred 949184610Salfred default: 950184610Salfred break; 951184610Salfred } 952184610Salfred } 953184610Salfred 954184610Salfred if (pass == 0) { 955184610Salfred 956192984Sthompsa size = (sizeof(*p_uhe) * nedesc) + 957184610Salfred (sizeof(*p_ui) * iface_index) + 958192984Sthompsa (sizeof(*p_uhi) * niface_total); 959184610Salfred 960192984Sthompsa p_uhe = malloc(size, M_USBDEV, M_WAITOK | M_ZERO); 961184610Salfred p_ui = (void *)(p_uhe + nedesc); 962184610Salfred p_uhi = (void *)(p_ui + iface_index); 963184610Salfred 964192984Sthompsa udev->linux_iface_start = p_ui; 965192984Sthompsa udev->linux_iface_end = p_ui + iface_index; 966192984Sthompsa udev->linux_endpoint_start = p_uhe; 967192984Sthompsa udev->linux_endpoint_end = p_uhe + nedesc; 968192984Sthompsa udev->devnum = device_get_unit(dev); 969192984Sthompsa bcopy(&udev->ddesc, &udev->descriptor, 970192984Sthompsa sizeof(udev->descriptor)); 971207080Sthompsa bcopy(udev->ctrl_ep.edesc, &udev->ep0.desc, 972192984Sthompsa sizeof(udev->ep0.desc)); 973184610Salfred } 974184610Salfred } 975192984Sthompsa return (0); 976184610Salfred} 977184610Salfred 978184610Salfred/*------------------------------------------------------------------------* 979184610Salfred * usb_alloc_urb 980184610Salfred * 981184610Salfred * This function should always be used when you allocate an URB for 982184610Salfred * use with the USB Linux stack. In case of an isochronous transfer 983184610Salfred * you must specifiy the maximum number of "iso_packets" which you 984184610Salfred * plan to transfer per URB. This function is always blocking, and 985184610Salfred * "mem_flags" are not regarded like on Linux. 986184610Salfred *------------------------------------------------------------------------*/ 987184610Salfredstruct urb * 988184610Salfredusb_alloc_urb(uint16_t iso_packets, uint16_t mem_flags) 989184610Salfred{ 990184610Salfred struct urb *urb; 991193074Sthompsa usb_size_t size; 992184610Salfred 993184610Salfred if (iso_packets == 0xFFFF) { 994184610Salfred /* 995184610Salfred * FreeBSD specific magic value to ask for control transfer 996184610Salfred * memory allocation: 997184610Salfred */ 998192984Sthompsa size = sizeof(*urb) + sizeof(struct usb_device_request) + mem_flags; 999184610Salfred } else { 1000184610Salfred size = sizeof(*urb) + (iso_packets * sizeof(urb->iso_frame_desc[0])); 1001184610Salfred } 1002184610Salfred 1003184610Salfred urb = malloc(size, M_USBDEV, M_WAITOK | M_ZERO); 1004184610Salfred 1005363665Smarkj cv_init(&urb->cv_wait, "URBWAIT"); 1006363665Smarkj if (iso_packets == 0xFFFF) { 1007363665Smarkj urb->setup_packet = (void *)(urb + 1); 1008363665Smarkj urb->transfer_buffer = (void *)(urb->setup_packet + 1009363665Smarkj sizeof(struct usb_device_request)); 1010363665Smarkj } else { 1011363665Smarkj urb->number_of_packets = iso_packets; 1012184610Salfred } 1013184610Salfred return (urb); 1014184610Salfred} 1015184610Salfred 1016184610Salfred/*------------------------------------------------------------------------* 1017184610Salfred * usb_find_host_endpoint 1018184610Salfred * 1019184610Salfred * The following function will return the Linux USB host endpoint 1020184610Salfred * structure that matches the given endpoint type and endpoint 1021184610Salfred * value. If no match is found, NULL is returned. This function is not 1022184610Salfred * part of the Linux USB API and is only used internally. 1023184610Salfred *------------------------------------------------------------------------*/ 1024184610Salfredstruct usb_host_endpoint * 1025184610Salfredusb_find_host_endpoint(struct usb_device *dev, uint8_t type, uint8_t ep) 1026184610Salfred{ 1027184610Salfred struct usb_host_endpoint *uhe; 1028184610Salfred struct usb_host_endpoint *uhe_end; 1029184610Salfred struct usb_host_interface *uhi; 1030184610Salfred struct usb_interface *ui; 1031184610Salfred uint8_t ea; 1032184610Salfred uint8_t at; 1033184610Salfred uint8_t mask; 1034184610Salfred 1035184610Salfred if (dev == NULL) { 1036184610Salfred return (NULL); 1037184610Salfred } 1038184610Salfred if (type == UE_CONTROL) { 1039184610Salfred mask = UE_ADDR; 1040184610Salfred } else { 1041184610Salfred mask = (UE_DIR_IN | UE_DIR_OUT | UE_ADDR); 1042184610Salfred } 1043184610Salfred 1044184610Salfred ep &= mask; 1045184610Salfred 1046184610Salfred /* 1047184610Salfred * Iterate over all the interfaces searching the selected alternate 1048184610Salfred * setting only, and all belonging endpoints. 1049184610Salfred */ 1050192984Sthompsa for (ui = dev->linux_iface_start; 1051192984Sthompsa ui != dev->linux_iface_end; 1052184610Salfred ui++) { 1053184610Salfred uhi = ui->cur_altsetting; 1054184610Salfred if (uhi) { 1055184610Salfred uhe_end = uhi->endpoint + uhi->desc.bNumEndpoints; 1056184610Salfred for (uhe = uhi->endpoint; 1057184610Salfred uhe != uhe_end; 1058184610Salfred uhe++) { 1059184610Salfred ea = uhe->desc.bEndpointAddress; 1060184610Salfred at = uhe->desc.bmAttributes; 1061184610Salfred 1062184610Salfred if (((ea & mask) == ep) && 1063184610Salfred ((at & UE_XFERTYPE) == type)) { 1064184610Salfred return (uhe); 1065184610Salfred } 1066184610Salfred } 1067184610Salfred } 1068184610Salfred } 1069184610Salfred 1070184610Salfred if ((type == UE_CONTROL) && ((ep & UE_ADDR) == 0)) { 1071184610Salfred return (&dev->ep0); 1072184610Salfred } 1073184610Salfred return (NULL); 1074184610Salfred} 1075184610Salfred 1076184610Salfred/*------------------------------------------------------------------------* 1077184610Salfred * usb_altnum_to_altsetting 1078184610Salfred * 1079184610Salfred * The following function returns a pointer to an alternate setting by 1080184610Salfred * index given a "usb_interface" pointer. If the alternate setting by 1081184610Salfred * index does not exist, NULL is returned. And alternate setting is a 1082184610Salfred * variant of an interface, but usually with slightly different 1083184610Salfred * characteristics. 1084184610Salfred *------------------------------------------------------------------------*/ 1085184610Salfredstruct usb_host_interface * 1086184610Salfredusb_altnum_to_altsetting(const struct usb_interface *intf, uint8_t alt_index) 1087184610Salfred{ 1088184610Salfred if (alt_index >= intf->num_altsetting) { 1089184610Salfred return (NULL); 1090184610Salfred } 1091184610Salfred return (intf->altsetting + alt_index); 1092184610Salfred} 1093184610Salfred 1094184610Salfred/*------------------------------------------------------------------------* 1095184610Salfred * usb_ifnum_to_if 1096184610Salfred * 1097184610Salfred * The following function searches up an USB interface by 1098184610Salfred * "bInterfaceNumber". If no match is found, NULL is returned. 1099184610Salfred *------------------------------------------------------------------------*/ 1100184610Salfredstruct usb_interface * 1101184610Salfredusb_ifnum_to_if(struct usb_device *dev, uint8_t iface_no) 1102184610Salfred{ 1103184610Salfred struct usb_interface *p_ui; 1104184610Salfred 1105192984Sthompsa for (p_ui = dev->linux_iface_start; 1106192984Sthompsa p_ui != dev->linux_iface_end; 1107184610Salfred p_ui++) { 1108184610Salfred if ((p_ui->num_altsetting > 0) && 1109184610Salfred (p_ui->altsetting->desc.bInterfaceNumber == iface_no)) { 1110184610Salfred return (p_ui); 1111184610Salfred } 1112184610Salfred } 1113184610Salfred return (NULL); 1114184610Salfred} 1115184610Salfred 1116184610Salfred/*------------------------------------------------------------------------* 1117184610Salfred * usb_buffer_alloc 1118184610Salfred *------------------------------------------------------------------------*/ 1119184610Salfredvoid * 1120193074Sthompsausb_buffer_alloc(struct usb_device *dev, usb_size_t size, uint16_t mem_flags, uint8_t *dma_addr) 1121184610Salfred{ 1122184610Salfred return (malloc(size, M_USBDEV, M_WAITOK | M_ZERO)); 1123184610Salfred} 1124184610Salfred 1125184610Salfred/*------------------------------------------------------------------------* 1126194228Sthompsa * usbd_get_intfdata 1127184610Salfred *------------------------------------------------------------------------*/ 1128184610Salfredvoid * 1129194228Sthompsausbd_get_intfdata(struct usb_interface *intf) 1130184610Salfred{ 1131184610Salfred return (intf->bsd_priv_sc); 1132184610Salfred} 1133184610Salfred 1134184610Salfred/*------------------------------------------------------------------------* 1135184610Salfred * usb_linux_register 1136184610Salfred * 1137184610Salfred * The following function is used by the "USB_DRIVER_EXPORT()" macro, 1138184610Salfred * and is used to register a Linux USB driver, so that its 1139184610Salfred * "usb_device_id" structures gets searched a probe time. This 1140184610Salfred * function is not part of the Linux USB API, and is for internal use 1141184610Salfred * only. 1142184610Salfred *------------------------------------------------------------------------*/ 1143184610Salfredvoid 1144184610Salfredusb_linux_register(void *arg) 1145184610Salfred{ 1146184610Salfred struct usb_driver *drv = arg; 1147184610Salfred 1148196403Sjhb mtx_lock(&Giant); 1149184610Salfred LIST_INSERT_HEAD(&usb_linux_driver_list, drv, linux_driver_list); 1150196403Sjhb mtx_unlock(&Giant); 1151184610Salfred 1152194228Sthompsa usb_needs_explore_all(); 1153184610Salfred} 1154184610Salfred 1155184610Salfred/*------------------------------------------------------------------------* 1156184610Salfred * usb_linux_deregister 1157184610Salfred * 1158184610Salfred * The following function is used by the "USB_DRIVER_EXPORT()" macro, 1159184610Salfred * and is used to deregister a Linux USB driver. This function will 1160184610Salfred * ensure that all driver instances belonging to the Linux USB device 1161184610Salfred * driver in question, gets detached before the driver is 1162184610Salfred * unloaded. This function is not part of the Linux USB API, and is 1163184610Salfred * for internal use only. 1164184610Salfred *------------------------------------------------------------------------*/ 1165184610Salfredvoid 1166184610Salfredusb_linux_deregister(void *arg) 1167184610Salfred{ 1168184610Salfred struct usb_driver *drv = arg; 1169184610Salfred struct usb_linux_softc *sc; 1170184610Salfred 1171184610Salfredrepeat: 1172196403Sjhb mtx_lock(&Giant); 1173184610Salfred LIST_FOREACH(sc, &usb_linux_attached_list, sc_attached_list) { 1174184610Salfred if (sc->sc_udrv == drv) { 1175196403Sjhb mtx_unlock(&Giant); 1176184610Salfred device_detach(sc->sc_fbsd_dev); 1177184610Salfred goto repeat; 1178184610Salfred } 1179184610Salfred } 1180184610Salfred LIST_REMOVE(drv, linux_driver_list); 1181196403Sjhb mtx_unlock(&Giant); 1182184610Salfred} 1183184610Salfred 1184184610Salfred/*------------------------------------------------------------------------* 1185184610Salfred * usb_linux_free_device 1186184610Salfred * 1187184610Salfred * The following function is only used by the FreeBSD USB stack, to 1188184610Salfred * cleanup and free memory after that a Linux USB device was attached. 1189184610Salfred *------------------------------------------------------------------------*/ 1190184610Salfredvoid 1191184610Salfredusb_linux_free_device(struct usb_device *dev) 1192184610Salfred{ 1193184610Salfred struct usb_host_endpoint *uhe; 1194184610Salfred struct usb_host_endpoint *uhe_end; 1195184610Salfred int err; 1196184610Salfred 1197192984Sthompsa uhe = dev->linux_endpoint_start; 1198192984Sthompsa uhe_end = dev->linux_endpoint_end; 1199184610Salfred while (uhe != uhe_end) { 1200184610Salfred err = usb_setup_endpoint(dev, uhe, 0); 1201184610Salfred uhe++; 1202184610Salfred } 1203184610Salfred err = usb_setup_endpoint(dev, &dev->ep0, 0); 1204194065Sthompsa free(dev->linux_endpoint_start, M_USBDEV); 1205184610Salfred} 1206184610Salfred 1207184610Salfred/*------------------------------------------------------------------------* 1208184610Salfred * usb_buffer_free 1209184610Salfred *------------------------------------------------------------------------*/ 1210184610Salfredvoid 1211193074Sthompsausb_buffer_free(struct usb_device *dev, usb_size_t size, 1212184610Salfred void *addr, uint8_t dma_addr) 1213184610Salfred{ 1214184610Salfred free(addr, M_USBDEV); 1215184610Salfred} 1216184610Salfred 1217184610Salfred/*------------------------------------------------------------------------* 1218184610Salfred * usb_free_urb 1219184610Salfred *------------------------------------------------------------------------*/ 1220184610Salfredvoid 1221184610Salfredusb_free_urb(struct urb *urb) 1222184610Salfred{ 1223184610Salfred if (urb == NULL) { 1224184610Salfred return; 1225184610Salfred } 1226184610Salfred /* make sure that the current URB is not active */ 1227184610Salfred usb_kill_urb(urb); 1228184610Salfred 1229184610Salfred /* destroy condition variable */ 1230194227Sthompsa cv_destroy(&urb->cv_wait); 1231184610Salfred 1232184610Salfred /* just free it */ 1233184610Salfred free(urb, M_USBDEV); 1234184610Salfred} 1235184610Salfred 1236184610Salfred/*------------------------------------------------------------------------* 1237184610Salfred * usb_init_urb 1238184610Salfred * 1239184610Salfred * The following function can be used to initialize a custom URB. It 1240184610Salfred * is not recommended to use this function. Use "usb_alloc_urb()" 1241184610Salfred * instead. 1242184610Salfred *------------------------------------------------------------------------*/ 1243184610Salfredvoid 1244184610Salfredusb_init_urb(struct urb *urb) 1245184610Salfred{ 1246184610Salfred if (urb == NULL) { 1247184610Salfred return; 1248184610Salfred } 1249227461Shselasky memset(urb, 0, sizeof(*urb)); 1250184610Salfred} 1251184610Salfred 1252184610Salfred/*------------------------------------------------------------------------* 1253184610Salfred * usb_kill_urb 1254184610Salfred *------------------------------------------------------------------------*/ 1255184610Salfredvoid 1256184610Salfredusb_kill_urb(struct urb *urb) 1257184610Salfred{ 1258195966Salfred usb_unlink_urb_sub(urb, 1); 1259184610Salfred} 1260184610Salfred 1261184610Salfred/*------------------------------------------------------------------------* 1262184610Salfred * usb_set_intfdata 1263184610Salfred * 1264184610Salfred * The following function sets the per Linux USB interface private 1265184610Salfred * data pointer. It is used by most Linux USB device drivers. 1266184610Salfred *------------------------------------------------------------------------*/ 1267184610Salfredvoid 1268184610Salfredusb_set_intfdata(struct usb_interface *intf, void *data) 1269184610Salfred{ 1270184610Salfred intf->bsd_priv_sc = data; 1271184610Salfred} 1272184610Salfred 1273184610Salfred/*------------------------------------------------------------------------* 1274184610Salfred * usb_linux_cleanup_interface 1275184610Salfred * 1276184610Salfred * The following function will release all FreeBSD USB transfers 1277184610Salfred * associated with a Linux USB interface. It is for internal use only. 1278184610Salfred *------------------------------------------------------------------------*/ 1279184610Salfredstatic void 1280184610Salfredusb_linux_cleanup_interface(struct usb_device *dev, struct usb_interface *iface) 1281184610Salfred{ 1282184610Salfred struct usb_host_interface *uhi; 1283184610Salfred struct usb_host_interface *uhi_end; 1284184610Salfred struct usb_host_endpoint *uhe; 1285184610Salfred struct usb_host_endpoint *uhe_end; 1286184610Salfred int err; 1287184610Salfred 1288184610Salfred uhi = iface->altsetting; 1289184610Salfred uhi_end = iface->altsetting + iface->num_altsetting; 1290184610Salfred while (uhi != uhi_end) { 1291184610Salfred uhe = uhi->endpoint; 1292184610Salfred uhe_end = uhi->endpoint + uhi->desc.bNumEndpoints; 1293184610Salfred while (uhe != uhe_end) { 1294184610Salfred err = usb_setup_endpoint(dev, uhe, 0); 1295184610Salfred uhe++; 1296184610Salfred } 1297184610Salfred uhi++; 1298184610Salfred } 1299184610Salfred} 1300184610Salfred 1301184610Salfred/*------------------------------------------------------------------------* 1302184610Salfred * usb_linux_wait_complete 1303184610Salfred * 1304184610Salfred * The following function is used by "usb_start_wait_urb()" to wake it 1305184610Salfred * up, when an USB transfer has finished. 1306184610Salfred *------------------------------------------------------------------------*/ 1307184610Salfredstatic void 1308184610Salfredusb_linux_wait_complete(struct urb *urb) 1309184610Salfred{ 1310184610Salfred if (urb->transfer_flags & URB_IS_SLEEPING) { 1311194227Sthompsa cv_signal(&urb->cv_wait); 1312184610Salfred } 1313184610Salfred urb->transfer_flags &= ~URB_WAIT_WAKEUP; 1314184610Salfred} 1315184610Salfred 1316184610Salfred/*------------------------------------------------------------------------* 1317184610Salfred * usb_linux_complete 1318184610Salfred *------------------------------------------------------------------------*/ 1319184610Salfredstatic void 1320192984Sthompsausb_linux_complete(struct usb_xfer *xfer) 1321184610Salfred{ 1322184610Salfred struct urb *urb; 1323184610Salfred 1324194677Sthompsa urb = usbd_xfer_get_priv(xfer); 1325194677Sthompsa usbd_xfer_set_priv(xfer, NULL); 1326184610Salfred if (urb->complete) { 1327184610Salfred (urb->complete) (urb); 1328184610Salfred } 1329184610Salfred} 1330184610Salfred 1331184610Salfred/*------------------------------------------------------------------------* 1332184610Salfred * usb_linux_isoc_callback 1333184610Salfred * 1334184610Salfred * The following is the FreeBSD isochronous USB callback. Isochronous 1335184610Salfred * frames are USB packets transferred 1000 or 8000 times per second, 1336184610Salfred * depending on whether a full- or high- speed USB transfer is 1337184610Salfred * used. 1338184610Salfred *------------------------------------------------------------------------*/ 1339184610Salfredstatic void 1340194677Sthompsausb_linux_isoc_callback(struct usb_xfer *xfer, usb_error_t error) 1341184610Salfred{ 1342193045Sthompsa usb_frlength_t max_frame = xfer->max_frame_size; 1343193045Sthompsa usb_frlength_t offset; 1344193045Sthompsa usb_frcount_t x; 1345194677Sthompsa struct urb *urb = usbd_xfer_get_priv(xfer); 1346194677Sthompsa struct usb_host_endpoint *uhe = usbd_xfer_softc(xfer); 1347184610Salfred struct usb_iso_packet_descriptor *uipd; 1348184610Salfred 1349184610Salfred DPRINTF("\n"); 1350184610Salfred 1351184610Salfred switch (USB_GET_STATE(xfer)) { 1352184610Salfred case USB_ST_TRANSFERRED: 1353184610Salfred 1354184610Salfred if (urb->bsd_isread) { 1355184610Salfred 1356184610Salfred /* copy in data with regard to the URB */ 1357184610Salfred 1358184610Salfred offset = 0; 1359184610Salfred 1360184610Salfred for (x = 0; x < urb->number_of_packets; x++) { 1361184610Salfred uipd = urb->iso_frame_desc + x; 1362198776Sthompsa if (uipd->length > xfer->frlengths[x]) { 1363198776Sthompsa if (urb->transfer_flags & URB_SHORT_NOT_OK) { 1364198776Sthompsa /* XXX should be EREMOTEIO */ 1365198776Sthompsa uipd->status = -EPIPE; 1366198776Sthompsa } else { 1367198776Sthompsa uipd->status = 0; 1368198776Sthompsa } 1369198776Sthompsa } else { 1370198776Sthompsa uipd->status = 0; 1371198776Sthompsa } 1372184610Salfred uipd->actual_length = xfer->frlengths[x]; 1373184610Salfred if (!xfer->flags.ext_buffer) { 1374194228Sthompsa usbd_copy_out(xfer->frbuffers, offset, 1375184610Salfred USB_ADD_BYTES(urb->transfer_buffer, 1376184610Salfred uipd->offset), uipd->actual_length); 1377184610Salfred } 1378184610Salfred offset += max_frame; 1379184610Salfred } 1380184610Salfred } else { 1381184610Salfred for (x = 0; x < urb->number_of_packets; x++) { 1382184610Salfred uipd = urb->iso_frame_desc + x; 1383184610Salfred uipd->actual_length = xfer->frlengths[x]; 1384184610Salfred uipd->status = 0; 1385184610Salfred } 1386184610Salfred } 1387184610Salfred 1388184610Salfred urb->actual_length = xfer->actlen; 1389184610Salfred 1390184610Salfred /* check for short transfer */ 1391184610Salfred if (xfer->actlen < xfer->sumlen) { 1392184610Salfred /* short transfer */ 1393184610Salfred if (urb->transfer_flags & URB_SHORT_NOT_OK) { 1394198776Sthompsa /* XXX should be EREMOTEIO */ 1395198776Sthompsa urb->status = -EPIPE; 1396184610Salfred } else { 1397184610Salfred urb->status = 0; 1398184610Salfred } 1399184610Salfred } else { 1400184610Salfred /* success */ 1401184610Salfred urb->status = 0; 1402184610Salfred } 1403184610Salfred 1404184610Salfred /* call callback */ 1405184610Salfred usb_linux_complete(xfer); 1406184610Salfred 1407184610Salfred case USB_ST_SETUP: 1408184610Salfredtr_setup: 1409184610Salfred 1410184610Salfred if (xfer->priv_fifo == NULL) { 1411184610Salfred 1412184610Salfred /* get next transfer */ 1413184610Salfred urb = TAILQ_FIRST(&uhe->bsd_urb_list); 1414184610Salfred if (urb == NULL) { 1415184610Salfred /* nothing to do */ 1416184610Salfred return; 1417184610Salfred } 1418184610Salfred TAILQ_REMOVE(&uhe->bsd_urb_list, urb, bsd_urb_list); 1419184610Salfred urb->bsd_urb_list.tqe_prev = NULL; 1420184610Salfred 1421184610Salfred x = xfer->max_frame_count; 1422184610Salfred if (urb->number_of_packets > x) { 1423184610Salfred /* XXX simply truncate the transfer */ 1424184610Salfred urb->number_of_packets = x; 1425184610Salfred } 1426184610Salfred } else { 1427184610Salfred DPRINTF("Already got a transfer\n"); 1428184610Salfred 1429184610Salfred /* already got a transfer (should not happen) */ 1430194677Sthompsa urb = usbd_xfer_get_priv(xfer); 1431184610Salfred } 1432184610Salfred 1433184610Salfred urb->bsd_isread = (uhe->desc.bEndpointAddress & UE_DIR_IN) ? 1 : 0; 1434184610Salfred 1435194677Sthompsa if (xfer->flags.ext_buffer) { 1436194677Sthompsa /* set virtual address to load */ 1437194677Sthompsa usbd_xfer_set_frame_data(xfer, 0, urb->transfer_buffer, 0); 1438194677Sthompsa } 1439184610Salfred if (!(urb->bsd_isread)) { 1440184610Salfred 1441184610Salfred /* copy out data with regard to the URB */ 1442184610Salfred 1443184610Salfred offset = 0; 1444184610Salfred 1445184610Salfred for (x = 0; x < urb->number_of_packets; x++) { 1446184610Salfred uipd = urb->iso_frame_desc + x; 1447194677Sthompsa usbd_xfer_set_frame_len(xfer, x, uipd->length); 1448184610Salfred if (!xfer->flags.ext_buffer) { 1449194228Sthompsa usbd_copy_in(xfer->frbuffers, offset, 1450184610Salfred USB_ADD_BYTES(urb->transfer_buffer, 1451184610Salfred uipd->offset), uipd->length); 1452184610Salfred } 1453184610Salfred offset += uipd->length; 1454184610Salfred } 1455184610Salfred } else { 1456184610Salfred 1457184610Salfred /* 1458184610Salfred * compute the transfer length into the "offset" 1459184610Salfred * variable 1460184610Salfred */ 1461184610Salfred 1462184610Salfred offset = urb->number_of_packets * max_frame; 1463184610Salfred 1464184610Salfred /* setup "frlengths" array */ 1465184610Salfred 1466184610Salfred for (x = 0; x < urb->number_of_packets; x++) { 1467184610Salfred uipd = urb->iso_frame_desc + x; 1468194677Sthompsa usbd_xfer_set_frame_len(xfer, x, max_frame); 1469184610Salfred } 1470184610Salfred } 1471194677Sthompsa usbd_xfer_set_priv(xfer, urb); 1472184610Salfred xfer->flags.force_short_xfer = 0; 1473184610Salfred xfer->timeout = urb->timeout; 1474184610Salfred xfer->nframes = urb->number_of_packets; 1475194228Sthompsa usbd_transfer_submit(xfer); 1476184610Salfred return; 1477184610Salfred 1478184610Salfred default: /* Error */ 1479184610Salfred if (xfer->error == USB_ERR_CANCELLED) { 1480184610Salfred urb->status = -ECONNRESET; 1481184610Salfred } else { 1482184610Salfred urb->status = -EPIPE; /* stalled */ 1483184610Salfred } 1484184610Salfred 1485184610Salfred /* Set zero for "actual_length" */ 1486184610Salfred urb->actual_length = 0; 1487184610Salfred 1488184610Salfred /* Set zero for "actual_length" */ 1489184610Salfred for (x = 0; x < urb->number_of_packets; x++) { 1490184610Salfred urb->iso_frame_desc[x].actual_length = 0; 1491198776Sthompsa urb->iso_frame_desc[x].status = urb->status; 1492184610Salfred } 1493184610Salfred 1494184610Salfred /* call callback */ 1495184610Salfred usb_linux_complete(xfer); 1496184610Salfred 1497184610Salfred if (xfer->error == USB_ERR_CANCELLED) { 1498184610Salfred /* we need to return in this case */ 1499184610Salfred return; 1500184610Salfred } 1501184610Salfred goto tr_setup; 1502184610Salfred 1503184610Salfred } 1504184610Salfred} 1505184610Salfred 1506184610Salfred/*------------------------------------------------------------------------* 1507184610Salfred * usb_linux_non_isoc_callback 1508184610Salfred * 1509184610Salfred * The following is the FreeBSD BULK/INTERRUPT and CONTROL USB 1510184610Salfred * callback. It dequeues Linux USB stack compatible URB's, transforms 1511184610Salfred * the URB fields into a FreeBSD USB transfer, and defragments the USB 1512184610Salfred * transfer as required. When the transfer is complete the "complete" 1513184610Salfred * callback is called. 1514184610Salfred *------------------------------------------------------------------------*/ 1515184610Salfredstatic void 1516194677Sthompsausb_linux_non_isoc_callback(struct usb_xfer *xfer, usb_error_t error) 1517184610Salfred{ 1518184610Salfred enum { 1519192984Sthompsa REQ_SIZE = sizeof(struct usb_device_request) 1520184610Salfred }; 1521194677Sthompsa struct urb *urb = usbd_xfer_get_priv(xfer); 1522194677Sthompsa struct usb_host_endpoint *uhe = usbd_xfer_softc(xfer); 1523184610Salfred uint8_t *ptr; 1524194677Sthompsa usb_frlength_t max_bulk = usbd_xfer_max_len(xfer); 1525184610Salfred uint8_t data_frame = xfer->flags_int.control_xfr ? 1 : 0; 1526184610Salfred 1527184610Salfred DPRINTF("\n"); 1528184610Salfred 1529184610Salfred switch (USB_GET_STATE(xfer)) { 1530184610Salfred case USB_ST_TRANSFERRED: 1531184610Salfred 1532184610Salfred if (xfer->flags_int.control_xfr) { 1533184610Salfred 1534184610Salfred /* don't transfer the setup packet again: */ 1535184610Salfred 1536194677Sthompsa usbd_xfer_set_frame_len(xfer, 0, 0); 1537184610Salfred } 1538184610Salfred if (urb->bsd_isread && (!xfer->flags.ext_buffer)) { 1539184610Salfred /* copy in data with regard to the URB */ 1540194228Sthompsa usbd_copy_out(xfer->frbuffers + data_frame, 0, 1541184610Salfred urb->bsd_data_ptr, xfer->frlengths[data_frame]); 1542184610Salfred } 1543184610Salfred urb->bsd_length_rem -= xfer->frlengths[data_frame]; 1544184610Salfred urb->bsd_data_ptr += xfer->frlengths[data_frame]; 1545184610Salfred urb->actual_length += xfer->frlengths[data_frame]; 1546184610Salfred 1547184610Salfred /* check for short transfer */ 1548184610Salfred if (xfer->actlen < xfer->sumlen) { 1549184610Salfred urb->bsd_length_rem = 0; 1550184610Salfred 1551184610Salfred /* short transfer */ 1552184610Salfred if (urb->transfer_flags & URB_SHORT_NOT_OK) { 1553184610Salfred urb->status = -EPIPE; 1554184610Salfred } else { 1555184610Salfred urb->status = 0; 1556184610Salfred } 1557184610Salfred } else { 1558184610Salfred /* check remainder */ 1559184610Salfred if (urb->bsd_length_rem > 0) { 1560184610Salfred goto setup_bulk; 1561184610Salfred } 1562184610Salfred /* success */ 1563184610Salfred urb->status = 0; 1564184610Salfred } 1565184610Salfred 1566184610Salfred /* call callback */ 1567184610Salfred usb_linux_complete(xfer); 1568184610Salfred 1569184610Salfred case USB_ST_SETUP: 1570184610Salfredtr_setup: 1571184610Salfred /* get next transfer */ 1572184610Salfred urb = TAILQ_FIRST(&uhe->bsd_urb_list); 1573184610Salfred if (urb == NULL) { 1574184610Salfred /* nothing to do */ 1575184610Salfred return; 1576184610Salfred } 1577184610Salfred TAILQ_REMOVE(&uhe->bsd_urb_list, urb, bsd_urb_list); 1578184610Salfred urb->bsd_urb_list.tqe_prev = NULL; 1579184610Salfred 1580194677Sthompsa usbd_xfer_set_priv(xfer, urb); 1581184610Salfred xfer->flags.force_short_xfer = 0; 1582184610Salfred xfer->timeout = urb->timeout; 1583184610Salfred 1584184610Salfred if (xfer->flags_int.control_xfr) { 1585184610Salfred 1586184610Salfred /* 1587331756Semaste * USB control transfers need special handling. 1588331756Semaste * First copy in the header, then copy in data! 1589331756Semaste */ 1590184610Salfred if (!xfer->flags.ext_buffer) { 1591194228Sthompsa usbd_copy_in(xfer->frbuffers, 0, 1592184610Salfred urb->setup_packet, REQ_SIZE); 1593194677Sthompsa usbd_xfer_set_frame_len(xfer, 0, REQ_SIZE); 1594184610Salfred } else { 1595184610Salfred /* set virtual address to load */ 1596194677Sthompsa usbd_xfer_set_frame_data(xfer, 0, 1597194677Sthompsa urb->setup_packet, REQ_SIZE); 1598184610Salfred } 1599184610Salfred 1600184610Salfred ptr = urb->setup_packet; 1601184610Salfred 1602184610Salfred /* setup data transfer direction and length */ 1603184610Salfred urb->bsd_isread = (ptr[0] & UT_READ) ? 1 : 0; 1604184610Salfred urb->bsd_length_rem = ptr[6] | (ptr[7] << 8); 1605184610Salfred 1606184610Salfred } else { 1607184610Salfred 1608184610Salfred /* setup data transfer direction */ 1609184610Salfred 1610184610Salfred urb->bsd_length_rem = urb->transfer_buffer_length; 1611184610Salfred urb->bsd_isread = (uhe->desc.bEndpointAddress & 1612184610Salfred UE_DIR_IN) ? 1 : 0; 1613184610Salfred } 1614184610Salfred 1615184610Salfred urb->bsd_data_ptr = urb->transfer_buffer; 1616184610Salfred urb->actual_length = 0; 1617184610Salfred 1618184610Salfredsetup_bulk: 1619184610Salfred if (max_bulk > urb->bsd_length_rem) { 1620184610Salfred max_bulk = urb->bsd_length_rem; 1621184610Salfred } 1622184610Salfred /* check if we need to force a short transfer */ 1623184610Salfred 1624184610Salfred if ((max_bulk == urb->bsd_length_rem) && 1625184610Salfred (urb->transfer_flags & URB_ZERO_PACKET) && 1626184610Salfred (!xfer->flags_int.control_xfr)) { 1627184610Salfred xfer->flags.force_short_xfer = 1; 1628184610Salfred } 1629184610Salfred /* check if we need to copy in data */ 1630184610Salfred 1631184610Salfred if (xfer->flags.ext_buffer) { 1632184610Salfred /* set virtual address to load */ 1633194677Sthompsa usbd_xfer_set_frame_data(xfer, data_frame, 1634194677Sthompsa urb->bsd_data_ptr, max_bulk); 1635184610Salfred } else if (!urb->bsd_isread) { 1636184610Salfred /* copy out data with regard to the URB */ 1637194228Sthompsa usbd_copy_in(xfer->frbuffers + data_frame, 0, 1638184610Salfred urb->bsd_data_ptr, max_bulk); 1639194677Sthompsa usbd_xfer_set_frame_len(xfer, data_frame, max_bulk); 1640184610Salfred } 1641184610Salfred if (xfer->flags_int.control_xfr) { 1642184610Salfred if (max_bulk > 0) { 1643184610Salfred xfer->nframes = 2; 1644184610Salfred } else { 1645184610Salfred xfer->nframes = 1; 1646184610Salfred } 1647184610Salfred } else { 1648184610Salfred xfer->nframes = 1; 1649184610Salfred } 1650194228Sthompsa usbd_transfer_submit(xfer); 1651184610Salfred return; 1652184610Salfred 1653184610Salfred default: 1654184610Salfred if (xfer->error == USB_ERR_CANCELLED) { 1655184610Salfred urb->status = -ECONNRESET; 1656184610Salfred } else { 1657184610Salfred urb->status = -EPIPE; 1658184610Salfred } 1659184610Salfred 1660184610Salfred /* Set zero for "actual_length" */ 1661184610Salfred urb->actual_length = 0; 1662184610Salfred 1663184610Salfred /* call callback */ 1664184610Salfred usb_linux_complete(xfer); 1665184610Salfred 1666184610Salfred if (xfer->error == USB_ERR_CANCELLED) { 1667184610Salfred /* we need to return in this case */ 1668184610Salfred return; 1669184610Salfred } 1670184610Salfred goto tr_setup; 1671184610Salfred } 1672184610Salfred} 1673198776Sthompsa 1674198776Sthompsa/*------------------------------------------------------------------------* 1675198776Sthompsa * usb_fill_bulk_urb 1676198776Sthompsa *------------------------------------------------------------------------*/ 1677198776Sthompsavoid 1678198776Sthompsausb_fill_bulk_urb(struct urb *urb, struct usb_device *udev, 1679198776Sthompsa struct usb_host_endpoint *uhe, void *buf, 1680198776Sthompsa int length, usb_complete_t callback, void *arg) 1681198776Sthompsa{ 1682198776Sthompsa urb->dev = udev; 1683198776Sthompsa urb->endpoint = uhe; 1684198776Sthompsa urb->transfer_buffer = buf; 1685198776Sthompsa urb->transfer_buffer_length = length; 1686198776Sthompsa urb->complete = callback; 1687198776Sthompsa urb->context = arg; 1688198776Sthompsa} 1689198776Sthompsa 1690198776Sthompsa/*------------------------------------------------------------------------* 1691198776Sthompsa * usb_bulk_msg 1692198776Sthompsa * 1693198776Sthompsa * NOTE: This function can also be used for interrupt endpoints! 1694198776Sthompsa * 1695198776Sthompsa * Return values: 1696198776Sthompsa * 0: Success 1697198776Sthompsa * Else: Failure 1698198776Sthompsa *------------------------------------------------------------------------*/ 1699198776Sthompsaint 1700198776Sthompsausb_bulk_msg(struct usb_device *udev, struct usb_host_endpoint *uhe, 1701198776Sthompsa void *data, int len, uint16_t *pactlen, usb_timeout_t timeout) 1702198776Sthompsa{ 1703198776Sthompsa struct urb *urb; 1704198776Sthompsa int err; 1705198776Sthompsa 1706198776Sthompsa if (uhe == NULL) 1707198776Sthompsa return (-EINVAL); 1708198776Sthompsa if (len < 0) 1709198776Sthompsa return (-EINVAL); 1710198776Sthompsa 1711198776Sthompsa err = usb_setup_endpoint(udev, uhe, 4096 /* bytes */); 1712198776Sthompsa if (err) 1713198776Sthompsa return (err); 1714198776Sthompsa 1715198776Sthompsa urb = usb_alloc_urb(0, 0); 1716198776Sthompsa 1717331756Semaste usb_fill_bulk_urb(urb, udev, uhe, data, len, 1718198776Sthompsa usb_linux_wait_complete, NULL); 1719198776Sthompsa 1720198776Sthompsa err = usb_start_wait_urb(urb, timeout, pactlen); 1721198776Sthompsa 1722198776Sthompsa usb_free_urb(urb); 1723198776Sthompsa 1724198776Sthompsa return (err); 1725198776Sthompsa} 1726290135ShselaskyMODULE_DEPEND(linuxkpi, usb, 1, 1, 1); 1727290135Shselasky 1728290135Shselaskystatic void 1729290135Shselaskyusb_linux_init(void *arg) 1730290135Shselasky{ 1731290135Shselasky /* register our function */ 1732290135Shselasky usb_linux_free_device_p = &usb_linux_free_device; 1733290135Shselasky} 1734290135ShselaskySYSINIT(usb_linux_init, SI_SUB_LOCK, SI_ORDER_FIRST, usb_linux_init, NULL); 1735290135ShselaskySYSUNINIT(usb_linux_unload, SI_SUB_LOCK, SI_ORDER_ANY, usb_linux_unload, NULL); 1736