linux_usb.c revision 233774
1184610Salfred/* $FreeBSD: head/sys/dev/usb/usb_compat_linux.c 233774 2012-04-02 10:50:42Z 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 28194677Sthompsa#include <sys/stdint.h> 29194677Sthompsa#include <sys/stddef.h> 30194677Sthompsa#include <sys/param.h> 31194677Sthompsa#include <sys/queue.h> 32194677Sthompsa#include <sys/types.h> 33194677Sthompsa#include <sys/systm.h> 34194677Sthompsa#include <sys/kernel.h> 35194677Sthompsa#include <sys/bus.h> 36194677Sthompsa#include <sys/module.h> 37194677Sthompsa#include <sys/lock.h> 38194677Sthompsa#include <sys/mutex.h> 39194677Sthompsa#include <sys/condvar.h> 40194677Sthompsa#include <sys/sysctl.h> 41194677Sthompsa#include <sys/sx.h> 42194677Sthompsa#include <sys/unistd.h> 43194677Sthompsa#include <sys/callout.h> 44194677Sthompsa#include <sys/malloc.h> 45194677Sthompsa#include <sys/priv.h> 46194677Sthompsa 47188942Sthompsa#include <dev/usb/usb.h> 48188942Sthompsa#include <dev/usb/usb_ioctl.h> 49194677Sthompsa#include <dev/usb/usbdi.h> 50194677Sthompsa#include <dev/usb/usbdi_util.h> 51184610Salfred 52194228Sthompsa#define USB_DEBUG_VAR usb_debug 53184610Salfred 54188942Sthompsa#include <dev/usb/usb_core.h> 55188942Sthompsa#include <dev/usb/usb_compat_linux.h> 56188942Sthompsa#include <dev/usb/usb_process.h> 57188942Sthompsa#include <dev/usb/usb_device.h> 58188942Sthompsa#include <dev/usb/usb_util.h> 59188942Sthompsa#include <dev/usb/usb_busdma.h> 60188942Sthompsa#include <dev/usb/usb_transfer.h> 61188942Sthompsa#include <dev/usb/usb_hub.h> 62188942Sthompsa#include <dev/usb/usb_request.h> 63188942Sthompsa#include <dev/usb/usb_debug.h> 64184610Salfred 65184610Salfredstruct usb_linux_softc { 66184610Salfred LIST_ENTRY(usb_linux_softc) sc_attached_list; 67184610Salfred 68184610Salfred device_t sc_fbsd_dev; 69192984Sthompsa struct usb_device *sc_fbsd_udev; 70184610Salfred struct usb_interface *sc_ui; 71184610Salfred struct usb_driver *sc_udrv; 72184610Salfred}; 73184610Salfred 74184610Salfred/* prototypes */ 75184610Salfredstatic device_probe_t usb_linux_probe; 76184610Salfredstatic device_attach_t usb_linux_attach; 77184610Salfredstatic device_detach_t usb_linux_detach; 78184610Salfredstatic device_suspend_t usb_linux_suspend; 79184610Salfredstatic device_resume_t usb_linux_resume; 80184610Salfred 81193045Sthompsastatic usb_callback_t usb_linux_isoc_callback; 82193045Sthompsastatic usb_callback_t usb_linux_non_isoc_callback; 83184610Salfred 84184610Salfredstatic usb_complete_t usb_linux_wait_complete; 85184610Salfred 86185948Sthompsastatic uint16_t usb_max_isoc_frames(struct usb_device *); 87193045Sthompsastatic int usb_start_wait_urb(struct urb *, usb_timeout_t, uint16_t *); 88185948Sthompsastatic const struct usb_device_id *usb_linux_lookup_id( 89192984Sthompsa const struct usb_device_id *, struct usb_attach_arg *); 90185948Sthompsastatic struct usb_driver *usb_linux_get_usb_driver(struct usb_linux_softc *); 91192984Sthompsastatic int usb_linux_create_usb_device(struct usb_device *, device_t); 92185948Sthompsastatic void usb_linux_cleanup_interface(struct usb_device *, 93185948Sthompsa struct usb_interface *); 94192984Sthompsastatic void usb_linux_complete(struct usb_xfer *); 95185948Sthompsastatic int usb_unlink_urb_sub(struct urb *, uint8_t); 96184610Salfred 97184610Salfred/*------------------------------------------------------------------------* 98184610Salfred * FreeBSD USB interface 99184610Salfred *------------------------------------------------------------------------*/ 100184610Salfred 101184610Salfredstatic LIST_HEAD(, usb_linux_softc) usb_linux_attached_list; 102184610Salfredstatic LIST_HEAD(, usb_driver) usb_linux_driver_list; 103184610Salfred 104184610Salfredstatic device_method_t usb_linux_methods[] = { 105184610Salfred /* Device interface */ 106184610Salfred DEVMETHOD(device_probe, usb_linux_probe), 107184610Salfred DEVMETHOD(device_attach, usb_linux_attach), 108184610Salfred DEVMETHOD(device_detach, usb_linux_detach), 109184610Salfred DEVMETHOD(device_suspend, usb_linux_suspend), 110184610Salfred DEVMETHOD(device_resume, usb_linux_resume), 111184610Salfred 112184610Salfred {0, 0} 113184610Salfred}; 114184610Salfred 115184610Salfredstatic driver_t usb_linux_driver = { 116184610Salfred .name = "usb_linux", 117184610Salfred .methods = usb_linux_methods, 118184610Salfred .size = sizeof(struct usb_linux_softc), 119184610Salfred}; 120184610Salfred 121184610Salfredstatic devclass_t usb_linux_devclass; 122184610Salfred 123189275SthompsaDRIVER_MODULE(usb_linux, uhub, usb_linux_driver, usb_linux_devclass, NULL, 0); 124212122SthompsaMODULE_VERSION(usb_linux, 1); 125184610Salfred 126184610Salfred/*------------------------------------------------------------------------* 127184610Salfred * usb_linux_lookup_id 128184610Salfred * 129184610Salfred * This functions takes an array of "struct usb_device_id" and tries 130192984Sthompsa * to match the entries with the information in "struct usb_attach_arg". 131184610Salfred * If it finds a match the matching entry will be returned. 132184610Salfred * Else "NULL" will be returned. 133184610Salfred *------------------------------------------------------------------------*/ 134184610Salfredstatic const struct usb_device_id * 135192984Sthompsausb_linux_lookup_id(const struct usb_device_id *id, struct usb_attach_arg *uaa) 136184610Salfred{ 137184610Salfred if (id == NULL) { 138184610Salfred goto done; 139184610Salfred } 140184610Salfred /* 141184610Salfred * Keep on matching array entries until we find one with 142184610Salfred * "match_flags" equal to zero, which indicates the end of the 143184610Salfred * array: 144184610Salfred */ 145184610Salfred for (; id->match_flags; id++) { 146184610Salfred 147184610Salfred if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) && 148184610Salfred (id->idVendor != uaa->info.idVendor)) { 149184610Salfred continue; 150184610Salfred } 151184610Salfred if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) && 152184610Salfred (id->idProduct != uaa->info.idProduct)) { 153184610Salfred continue; 154184610Salfred } 155184610Salfred if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) && 156184610Salfred (id->bcdDevice_lo > uaa->info.bcdDevice)) { 157184610Salfred continue; 158184610Salfred } 159184610Salfred if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) && 160184610Salfred (id->bcdDevice_hi < uaa->info.bcdDevice)) { 161184610Salfred continue; 162184610Salfred } 163184610Salfred if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) && 164184610Salfred (id->bDeviceClass != uaa->info.bDeviceClass)) { 165184610Salfred continue; 166184610Salfred } 167184610Salfred if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) && 168184610Salfred (id->bDeviceSubClass != uaa->info.bDeviceSubClass)) { 169184610Salfred continue; 170184610Salfred } 171184610Salfred if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) && 172184610Salfred (id->bDeviceProtocol != uaa->info.bDeviceProtocol)) { 173184610Salfred continue; 174184610Salfred } 175184610Salfred if ((uaa->info.bDeviceClass == 0xFF) && 176184610Salfred !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) && 177184610Salfred (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS | 178184610Salfred USB_DEVICE_ID_MATCH_INT_SUBCLASS | 179184610Salfred USB_DEVICE_ID_MATCH_INT_PROTOCOL))) { 180184610Salfred continue; 181184610Salfred } 182184610Salfred if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) && 183184610Salfred (id->bInterfaceClass != uaa->info.bInterfaceClass)) { 184184610Salfred continue; 185184610Salfred } 186184610Salfred if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) && 187184610Salfred (id->bInterfaceSubClass != uaa->info.bInterfaceSubClass)) { 188184610Salfred continue; 189184610Salfred } 190184610Salfred if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) && 191184610Salfred (id->bInterfaceProtocol != uaa->info.bInterfaceProtocol)) { 192184610Salfred continue; 193184610Salfred } 194184610Salfred /* we found a match! */ 195184610Salfred return (id); 196184610Salfred } 197184610Salfred 198184610Salfreddone: 199184610Salfred return (NULL); 200184610Salfred} 201184610Salfred 202184610Salfred/*------------------------------------------------------------------------* 203184610Salfred * usb_linux_probe 204184610Salfred * 205184610Salfred * This function is the FreeBSD probe callback. It is called from the 206184610Salfred * FreeBSD USB stack through the "device_probe_and_attach()" function. 207184610Salfred *------------------------------------------------------------------------*/ 208184610Salfredstatic int 209184610Salfredusb_linux_probe(device_t dev) 210184610Salfred{ 211192984Sthompsa struct usb_attach_arg *uaa = device_get_ivars(dev); 212184610Salfred struct usb_driver *udrv; 213184610Salfred int err = ENXIO; 214184610Salfred 215192499Sthompsa if (uaa->usb_mode != USB_MODE_HOST) { 216184610Salfred return (ENXIO); 217184610Salfred } 218196403Sjhb mtx_lock(&Giant); 219184610Salfred LIST_FOREACH(udrv, &usb_linux_driver_list, linux_driver_list) { 220184610Salfred if (usb_linux_lookup_id(udrv->id_table, uaa)) { 221184610Salfred err = 0; 222184610Salfred break; 223184610Salfred } 224184610Salfred } 225196403Sjhb mtx_unlock(&Giant); 226184610Salfred 227184610Salfred return (err); 228184610Salfred} 229184610Salfred 230184610Salfred/*------------------------------------------------------------------------* 231184610Salfred * usb_linux_get_usb_driver 232184610Salfred * 233184610Salfred * This function returns the pointer to the "struct usb_driver" where 234184610Salfred * the Linux USB device driver "struct usb_device_id" match was found. 235184610Salfred * We apply a lock before reading out the pointer to avoid races. 236184610Salfred *------------------------------------------------------------------------*/ 237184610Salfredstatic struct usb_driver * 238184610Salfredusb_linux_get_usb_driver(struct usb_linux_softc *sc) 239184610Salfred{ 240184610Salfred struct usb_driver *udrv; 241184610Salfred 242196403Sjhb mtx_lock(&Giant); 243184610Salfred udrv = sc->sc_udrv; 244196403Sjhb mtx_unlock(&Giant); 245184610Salfred return (udrv); 246184610Salfred} 247184610Salfred 248184610Salfred/*------------------------------------------------------------------------* 249184610Salfred * usb_linux_attach 250184610Salfred * 251184610Salfred * This function is the FreeBSD attach callback. It is called from the 252184610Salfred * FreeBSD USB stack through the "device_probe_and_attach()" function. 253184610Salfred * This function is called when "usb_linux_probe()" returns zero. 254184610Salfred *------------------------------------------------------------------------*/ 255184610Salfredstatic int 256184610Salfredusb_linux_attach(device_t dev) 257184610Salfred{ 258192984Sthompsa struct usb_attach_arg *uaa = device_get_ivars(dev); 259184610Salfred struct usb_linux_softc *sc = device_get_softc(dev); 260184610Salfred struct usb_driver *udrv; 261184610Salfred const struct usb_device_id *id = NULL; 262184610Salfred 263196403Sjhb mtx_lock(&Giant); 264184610Salfred LIST_FOREACH(udrv, &usb_linux_driver_list, linux_driver_list) { 265184610Salfred id = usb_linux_lookup_id(udrv->id_table, uaa); 266184610Salfred if (id) 267184610Salfred break; 268184610Salfred } 269196403Sjhb mtx_unlock(&Giant); 270184610Salfred 271184610Salfred if (id == NULL) { 272184610Salfred return (ENXIO); 273184610Salfred } 274192984Sthompsa if (usb_linux_create_usb_device(uaa->device, dev) != 0) 275192984Sthompsa return (ENOMEM); 276194228Sthompsa device_set_usb_desc(dev); 277184610Salfred 278184610Salfred sc->sc_fbsd_udev = uaa->device; 279184610Salfred sc->sc_fbsd_dev = dev; 280184610Salfred sc->sc_udrv = udrv; 281192984Sthompsa sc->sc_ui = usb_ifnum_to_if(uaa->device, uaa->info.bIfaceNum); 282184610Salfred if (sc->sc_ui == NULL) { 283184610Salfred return (EINVAL); 284184610Salfred } 285184610Salfred if (udrv->probe) { 286184610Salfred if ((udrv->probe) (sc->sc_ui, id)) { 287184610Salfred return (ENXIO); 288184610Salfred } 289184610Salfred } 290196403Sjhb mtx_lock(&Giant); 291184610Salfred LIST_INSERT_HEAD(&usb_linux_attached_list, sc, sc_attached_list); 292196403Sjhb mtx_unlock(&Giant); 293184610Salfred 294184610Salfred /* success */ 295184610Salfred return (0); 296184610Salfred} 297184610Salfred 298184610Salfred/*------------------------------------------------------------------------* 299184610Salfred * usb_linux_detach 300184610Salfred * 301184610Salfred * This function is the FreeBSD detach callback. It is called from the 302184610Salfred * FreeBSD USB stack through the "device_detach()" function. 303184610Salfred *------------------------------------------------------------------------*/ 304184610Salfredstatic int 305184610Salfredusb_linux_detach(device_t dev) 306184610Salfred{ 307184610Salfred struct usb_linux_softc *sc = device_get_softc(dev); 308184610Salfred struct usb_driver *udrv = NULL; 309184610Salfred 310196403Sjhb mtx_lock(&Giant); 311184610Salfred if (sc->sc_attached_list.le_prev) { 312184610Salfred LIST_REMOVE(sc, sc_attached_list); 313184610Salfred sc->sc_attached_list.le_prev = NULL; 314184610Salfred udrv = sc->sc_udrv; 315184610Salfred sc->sc_udrv = NULL; 316184610Salfred } 317196403Sjhb mtx_unlock(&Giant); 318184610Salfred 319184610Salfred if (udrv && udrv->disconnect) { 320184610Salfred (udrv->disconnect) (sc->sc_ui); 321184610Salfred } 322184610Salfred /* 323184610Salfred * Make sure that we free all FreeBSD USB transfers belonging to 324184610Salfred * this Linux "usb_interface", hence they will most likely not be 325184610Salfred * needed any more. 326184610Salfred */ 327192984Sthompsa usb_linux_cleanup_interface(sc->sc_fbsd_udev, sc->sc_ui); 328184610Salfred return (0); 329184610Salfred} 330184610Salfred 331184610Salfred/*------------------------------------------------------------------------* 332184610Salfred * usb_linux_suspend 333184610Salfred * 334184610Salfred * This function is the FreeBSD suspend callback. Usually it does nothing. 335184610Salfred *------------------------------------------------------------------------*/ 336184610Salfredstatic int 337184610Salfredusb_linux_suspend(device_t dev) 338184610Salfred{ 339184610Salfred struct usb_linux_softc *sc = device_get_softc(dev); 340184610Salfred struct usb_driver *udrv = usb_linux_get_usb_driver(sc); 341184610Salfred int err; 342184610Salfred 343184610Salfred if (udrv && udrv->suspend) { 344184610Salfred err = (udrv->suspend) (sc->sc_ui, 0); 345184610Salfred } 346184610Salfred return (0); 347184610Salfred} 348184610Salfred 349184610Salfred/*------------------------------------------------------------------------* 350184610Salfred * usb_linux_resume 351184610Salfred * 352184610Salfred * This function is the FreeBSD resume callback. Usually it does nothing. 353184610Salfred *------------------------------------------------------------------------*/ 354184610Salfredstatic int 355184610Salfredusb_linux_resume(device_t dev) 356184610Salfred{ 357184610Salfred struct usb_linux_softc *sc = device_get_softc(dev); 358184610Salfred struct usb_driver *udrv = usb_linux_get_usb_driver(sc); 359184610Salfred int err; 360184610Salfred 361184610Salfred if (udrv && udrv->resume) { 362184610Salfred err = (udrv->resume) (sc->sc_ui); 363184610Salfred } 364184610Salfred return (0); 365184610Salfred} 366184610Salfred 367184610Salfred/*------------------------------------------------------------------------* 368184610Salfred * Linux emulation layer 369184610Salfred *------------------------------------------------------------------------*/ 370184610Salfred 371184610Salfred/*------------------------------------------------------------------------* 372184610Salfred * usb_max_isoc_frames 373184610Salfred * 374184610Salfred * The following function returns the maximum number of isochronous 375184610Salfred * frames that we support per URB. It is not part of the Linux USB API. 376184610Salfred *------------------------------------------------------------------------*/ 377184610Salfredstatic uint16_t 378184610Salfredusb_max_isoc_frames(struct usb_device *dev) 379184610Salfred{ 380187180Sthompsa ; /* indent fix */ 381194228Sthompsa switch (usbd_get_speed(dev)) { 382187180Sthompsa case USB_SPEED_LOW: 383187180Sthompsa case USB_SPEED_FULL: 384187180Sthompsa return (USB_MAX_FULL_SPEED_ISOC_FRAMES); 385187180Sthompsa default: 386187180Sthompsa return (USB_MAX_HIGH_SPEED_ISOC_FRAMES); 387187180Sthompsa } 388184610Salfred} 389184610Salfred 390184610Salfred/*------------------------------------------------------------------------* 391184610Salfred * usb_submit_urb 392184610Salfred * 393184610Salfred * This function is used to queue an URB after that it has been 394184610Salfred * initialized. If it returns non-zero, it means that the URB was not 395184610Salfred * queued. 396184610Salfred *------------------------------------------------------------------------*/ 397184610Salfredint 398184610Salfredusb_submit_urb(struct urb *urb, uint16_t mem_flags) 399184610Salfred{ 400184610Salfred struct usb_host_endpoint *uhe; 401195966Salfred uint8_t do_unlock; 402195966Salfred int err; 403184610Salfred 404195966Salfred if (urb == NULL) 405184610Salfred return (-EINVAL); 406184610Salfred 407195966Salfred do_unlock = mtx_owned(&Giant) ? 0 : 1; 408195966Salfred if (do_unlock) 409195966Salfred mtx_lock(&Giant); 410195966Salfred 411193644Sthompsa if (urb->endpoint == NULL) { 412195966Salfred err = -EINVAL; 413195966Salfred goto done; 414184610Salfred } 415195966Salfred 416195966Salfred /* 417195966Salfred * Check to see if the urb is in the process of being killed 418195966Salfred * and stop a urb that is in the process of being killed from 419195966Salfred * being re-submitted (e.g. from its completion callback 420195966Salfred * function). 421195966Salfred */ 422195966Salfred if (urb->kill_count != 0) { 423195966Salfred err = -EPERM; 424195966Salfred goto done; 425195966Salfred } 426195966Salfred 427193644Sthompsa uhe = urb->endpoint; 428184610Salfred 429184610Salfred /* 430184610Salfred * Check that we have got a FreeBSD USB transfer that will dequeue 431184610Salfred * the URB structure and do the real transfer. If there are no USB 432184610Salfred * transfers, then we return an error. 433184610Salfred */ 434184610Salfred if (uhe->bsd_xfer[0] || 435184610Salfred uhe->bsd_xfer[1]) { 436184610Salfred /* we are ready! */ 437184610Salfred 438205803Sthompsa TAILQ_INSERT_TAIL(&uhe->bsd_urb_list, urb, bsd_urb_list); 439184610Salfred 440184610Salfred urb->status = -EINPROGRESS; 441184610Salfred 442194228Sthompsa usbd_transfer_start(uhe->bsd_xfer[0]); 443194228Sthompsa usbd_transfer_start(uhe->bsd_xfer[1]); 444195966Salfred err = 0; 445184610Salfred } else { 446184610Salfred /* no pipes have been setup yet! */ 447184610Salfred urb->status = -EINVAL; 448195966Salfred err = -EINVAL; 449184610Salfred } 450195966Salfreddone: 451195966Salfred if (do_unlock) 452195966Salfred mtx_unlock(&Giant); 453195966Salfred return (err); 454184610Salfred} 455184610Salfred 456184610Salfred/*------------------------------------------------------------------------* 457184610Salfred * usb_unlink_urb 458184610Salfred * 459184610Salfred * This function is used to stop an URB after that it is been 460184610Salfred * submitted, but before the "complete" callback has been called. On 461184610Salfred *------------------------------------------------------------------------*/ 462184610Salfredint 463184610Salfredusb_unlink_urb(struct urb *urb) 464184610Salfred{ 465184610Salfred return (usb_unlink_urb_sub(urb, 0)); 466184610Salfred} 467184610Salfred 468184610Salfredstatic void 469192984Sthompsausb_unlink_bsd(struct usb_xfer *xfer, 470184610Salfred struct urb *urb, uint8_t drain) 471184610Salfred{ 472195966Salfred if (xfer == NULL) 473195966Salfred return; 474195966Salfred if (!usbd_transfer_pending(xfer)) 475195966Salfred return; 476195966Salfred if (xfer->priv_fifo == (void *)urb) { 477196403Sjhb if (drain) { 478196403Sjhb mtx_unlock(&Giant); 479194228Sthompsa usbd_transfer_drain(xfer); 480196403Sjhb mtx_lock(&Giant); 481196403Sjhb } else { 482194228Sthompsa usbd_transfer_stop(xfer); 483196403Sjhb } 484194228Sthompsa usbd_transfer_start(xfer); 485184610Salfred } 486184610Salfred} 487184610Salfred 488184610Salfredstatic int 489184610Salfredusb_unlink_urb_sub(struct urb *urb, uint8_t drain) 490184610Salfred{ 491184610Salfred struct usb_host_endpoint *uhe; 492184610Salfred uint16_t x; 493195966Salfred uint8_t do_unlock; 494195966Salfred int err; 495184610Salfred 496195966Salfred if (urb == NULL) 497184610Salfred return (-EINVAL); 498184610Salfred 499195966Salfred do_unlock = mtx_owned(&Giant) ? 0 : 1; 500195966Salfred if (do_unlock) 501195966Salfred mtx_lock(&Giant); 502195966Salfred if (drain) 503195966Salfred urb->kill_count++; 504195966Salfred 505193644Sthompsa if (urb->endpoint == NULL) { 506195966Salfred err = -EINVAL; 507195966Salfred goto done; 508184610Salfred } 509193644Sthompsa uhe = urb->endpoint; 510184610Salfred 511184610Salfred if (urb->bsd_urb_list.tqe_prev) { 512184610Salfred 513184610Salfred /* not started yet, just remove it from the queue */ 514184610Salfred TAILQ_REMOVE(&uhe->bsd_urb_list, urb, bsd_urb_list); 515184610Salfred urb->bsd_urb_list.tqe_prev = NULL; 516184610Salfred urb->status = -ECONNRESET; 517184610Salfred urb->actual_length = 0; 518184610Salfred 519184610Salfred for (x = 0; x < urb->number_of_packets; x++) { 520184610Salfred urb->iso_frame_desc[x].actual_length = 0; 521184610Salfred } 522184610Salfred 523184610Salfred if (urb->complete) { 524184610Salfred (urb->complete) (urb); 525184610Salfred } 526184610Salfred } else { 527184610Salfred 528184610Salfred /* 529184610Salfred * If the URB is not on the URB list, then check if one of 530184610Salfred * the FreeBSD USB transfer are processing the current URB. 531184610Salfred * If so, re-start that transfer, which will lead to the 532184610Salfred * termination of that URB: 533184610Salfred */ 534184610Salfred usb_unlink_bsd(uhe->bsd_xfer[0], urb, drain); 535184610Salfred usb_unlink_bsd(uhe->bsd_xfer[1], urb, drain); 536184610Salfred } 537195966Salfred err = 0; 538195966Salfreddone: 539195966Salfred if (drain) 540195966Salfred urb->kill_count--; 541195966Salfred if (do_unlock) 542195966Salfred mtx_unlock(&Giant); 543195966Salfred return (err); 544184610Salfred} 545184610Salfred 546184610Salfred/*------------------------------------------------------------------------* 547184610Salfred * usb_clear_halt 548184610Salfred * 549184610Salfred * This function must always be used to clear the stall. Stall is when 550184610Salfred * an USB endpoint returns a stall message to the USB host controller. 551184610Salfred * Until the stall is cleared, no data can be transferred. 552184610Salfred *------------------------------------------------------------------------*/ 553184610Salfredint 554184610Salfredusb_clear_halt(struct usb_device *dev, struct usb_host_endpoint *uhe) 555184610Salfred{ 556192984Sthompsa struct usb_config cfg[1]; 557193644Sthompsa struct usb_endpoint *ep; 558184610Salfred uint8_t type; 559184610Salfred uint8_t addr; 560184610Salfred 561184610Salfred if (uhe == NULL) 562184610Salfred return (-EINVAL); 563184610Salfred 564184610Salfred type = uhe->desc.bmAttributes & UE_XFERTYPE; 565184610Salfred addr = uhe->desc.bEndpointAddress; 566184610Salfred 567227461Shselasky memset(cfg, 0, sizeof(cfg)); 568184610Salfred 569184610Salfred cfg[0].type = type; 570184610Salfred cfg[0].endpoint = addr & UE_ADDR; 571184610Salfred cfg[0].direction = addr & (UE_DIR_OUT | UE_DIR_IN); 572184610Salfred 573194228Sthompsa ep = usbd_get_endpoint(dev, uhe->bsd_iface_index, cfg); 574193644Sthompsa if (ep == NULL) 575184610Salfred return (-EINVAL); 576184610Salfred 577194228Sthompsa usbd_clear_data_toggle(dev, ep); 578184610Salfred 579184610Salfred return (usb_control_msg(dev, &dev->ep0, 580184610Salfred UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT, 581184610Salfred UF_ENDPOINT_HALT, addr, NULL, 0, 1000)); 582184610Salfred} 583184610Salfred 584184610Salfred/*------------------------------------------------------------------------* 585184610Salfred * usb_start_wait_urb 586184610Salfred * 587184610Salfred * This is an internal function that is used to perform synchronous 588184610Salfred * Linux USB transfers. 589184610Salfred *------------------------------------------------------------------------*/ 590184610Salfredstatic int 591193045Sthompsausb_start_wait_urb(struct urb *urb, usb_timeout_t timeout, uint16_t *p_actlen) 592184610Salfred{ 593184610Salfred int err; 594195966Salfred uint8_t do_unlock; 595184610Salfred 596184610Salfred /* you must have a timeout! */ 597184610Salfred if (timeout == 0) { 598184610Salfred timeout = 1; 599184610Salfred } 600184610Salfred urb->complete = &usb_linux_wait_complete; 601184610Salfred urb->timeout = timeout; 602184610Salfred urb->transfer_flags |= URB_WAIT_WAKEUP; 603184610Salfred urb->transfer_flags &= ~URB_IS_SLEEPING; 604184610Salfred 605195966Salfred do_unlock = mtx_owned(&Giant) ? 0 : 1; 606195966Salfred if (do_unlock) 607195966Salfred mtx_lock(&Giant); 608184610Salfred err = usb_submit_urb(urb, 0); 609184610Salfred if (err) 610184610Salfred goto done; 611184610Salfred 612184610Salfred /* 613184610Salfred * the URB might have completed before we get here, so check that by 614184610Salfred * using some flags! 615184610Salfred */ 616184610Salfred while (urb->transfer_flags & URB_WAIT_WAKEUP) { 617184610Salfred urb->transfer_flags |= URB_IS_SLEEPING; 618194227Sthompsa cv_wait(&urb->cv_wait, &Giant); 619184610Salfred urb->transfer_flags &= ~URB_IS_SLEEPING; 620184610Salfred } 621184610Salfred 622184610Salfred err = urb->status; 623184610Salfred 624184610Salfreddone: 625195966Salfred if (do_unlock) 626195966Salfred mtx_unlock(&Giant); 627198776Sthompsa if (p_actlen != NULL) { 628198776Sthompsa if (err) 629198776Sthompsa *p_actlen = 0; 630198776Sthompsa else 631198776Sthompsa *p_actlen = urb->actual_length; 632184610Salfred } 633184610Salfred return (err); 634184610Salfred} 635184610Salfred 636184610Salfred/*------------------------------------------------------------------------* 637184610Salfred * usb_control_msg 638184610Salfred * 639184610Salfred * The following function performs a control transfer sequence one any 640184610Salfred * control, bulk or interrupt endpoint, specified by "uhe". A control 641184610Salfred * transfer means that you transfer an 8-byte header first followed by 642184610Salfred * a data-phase as indicated by the 8-byte header. The "timeout" is 643184610Salfred * given in milliseconds. 644184610Salfred * 645184610Salfred * Return values: 646184610Salfred * 0: Success 647184610Salfred * < 0: Failure 648184610Salfred * > 0: Acutal length 649184610Salfred *------------------------------------------------------------------------*/ 650184610Salfredint 651184610Salfredusb_control_msg(struct usb_device *dev, struct usb_host_endpoint *uhe, 652184610Salfred uint8_t request, uint8_t requesttype, 653184610Salfred uint16_t value, uint16_t index, void *data, 654193045Sthompsa uint16_t size, usb_timeout_t timeout) 655184610Salfred{ 656192984Sthompsa struct usb_device_request req; 657184610Salfred struct urb *urb; 658184610Salfred int err; 659184610Salfred uint16_t actlen; 660184610Salfred uint8_t type; 661184610Salfred uint8_t addr; 662184610Salfred 663184610Salfred req.bmRequestType = requesttype; 664184610Salfred req.bRequest = request; 665184610Salfred USETW(req.wValue, value); 666184610Salfred USETW(req.wIndex, index); 667184610Salfred USETW(req.wLength, size); 668184610Salfred 669184610Salfred if (uhe == NULL) { 670184610Salfred return (-EINVAL); 671184610Salfred } 672184610Salfred type = (uhe->desc.bmAttributes & UE_XFERTYPE); 673184610Salfred addr = (uhe->desc.bEndpointAddress & UE_ADDR); 674184610Salfred 675184610Salfred if (type != UE_CONTROL) { 676184610Salfred return (-EINVAL); 677184610Salfred } 678184610Salfred if (addr == 0) { 679184610Salfred /* 680184610Salfred * The FreeBSD USB stack supports standard control 681184610Salfred * transfers on control endpoint zero: 682184610Salfred */ 683194228Sthompsa err = usbd_do_request_flags(dev, 684195966Salfred NULL, &req, data, USB_SHORT_XFER_OK, 685184610Salfred &actlen, timeout); 686184610Salfred if (err) { 687184610Salfred err = -EPIPE; 688184610Salfred } else { 689184610Salfred err = actlen; 690184610Salfred } 691184610Salfred return (err); 692184610Salfred } 693192984Sthompsa if (dev->flags.usb_mode != USB_MODE_HOST) { 694184610Salfred /* not supported */ 695184610Salfred return (-EINVAL); 696184610Salfred } 697184610Salfred err = usb_setup_endpoint(dev, uhe, 1 /* dummy */ ); 698184610Salfred 699184610Salfred /* 700184610Salfred * NOTE: we need to allocate real memory here so that we don't 701184610Salfred * transfer data to/from the stack! 702184610Salfred * 703184610Salfred * 0xFFFF is a FreeBSD specific magic value. 704184610Salfred */ 705184610Salfred urb = usb_alloc_urb(0xFFFF, size); 706184610Salfred if (urb == NULL) 707184610Salfred return (-ENOMEM); 708184610Salfred 709184610Salfred urb->dev = dev; 710193644Sthompsa urb->endpoint = uhe; 711184610Salfred 712227461Shselasky memcpy(urb->setup_packet, &req, sizeof(req)); 713184610Salfred 714184610Salfred if (size && (!(req.bmRequestType & UT_READ))) { 715184610Salfred /* move the data to a real buffer */ 716227461Shselasky memcpy(USB_ADD_BYTES(urb->setup_packet, sizeof(req)), 717227461Shselasky data, size); 718184610Salfred } 719184610Salfred err = usb_start_wait_urb(urb, timeout, &actlen); 720184610Salfred 721184610Salfred if (req.bmRequestType & UT_READ) { 722184610Salfred if (actlen) { 723184610Salfred bcopy(USB_ADD_BYTES(urb->setup_packet, 724184610Salfred sizeof(req)), data, actlen); 725184610Salfred } 726184610Salfred } 727184610Salfred usb_free_urb(urb); 728184610Salfred 729184610Salfred if (err == 0) { 730184610Salfred err = actlen; 731184610Salfred } 732184610Salfred return (err); 733184610Salfred} 734184610Salfred 735184610Salfred/*------------------------------------------------------------------------* 736184610Salfred * usb_set_interface 737184610Salfred * 738184610Salfred * The following function will select which alternate setting of an 739184610Salfred * USB interface you plan to use. By default alternate setting with 740184610Salfred * index zero is selected. Note that "iface_no" is not the interface 741184610Salfred * index, but rather the value of "bInterfaceNumber". 742184610Salfred *------------------------------------------------------------------------*/ 743184610Salfredint 744184610Salfredusb_set_interface(struct usb_device *dev, uint8_t iface_no, uint8_t alt_index) 745184610Salfred{ 746184610Salfred struct usb_interface *p_ui = usb_ifnum_to_if(dev, iface_no); 747184610Salfred int err; 748184610Salfred 749184610Salfred if (p_ui == NULL) 750184610Salfred return (-EINVAL); 751184610Salfred if (alt_index >= p_ui->num_altsetting) 752184610Salfred return (-EINVAL); 753184610Salfred usb_linux_cleanup_interface(dev, p_ui); 754194228Sthompsa err = -usbd_set_alt_interface_index(dev, 755184610Salfred p_ui->bsd_iface_index, alt_index); 756184610Salfred if (err == 0) { 757184610Salfred p_ui->cur_altsetting = p_ui->altsetting + alt_index; 758184610Salfred } 759184610Salfred return (err); 760184610Salfred} 761184610Salfred 762184610Salfred/*------------------------------------------------------------------------* 763184610Salfred * usb_setup_endpoint 764184610Salfred * 765184610Salfred * The following function is an extension to the Linux USB API that 766184610Salfred * allows you to set a maximum buffer size for a given USB endpoint. 767184610Salfred * The maximum buffer size is per URB. If you don't call this function 768184610Salfred * to set a maximum buffer size, the endpoint will not be functional. 769184610Salfred * Note that for isochronous endpoints the maximum buffer size must be 770184610Salfred * a non-zero dummy, hence this function will base the maximum buffer 771184610Salfred * size on "wMaxPacketSize". 772184610Salfred *------------------------------------------------------------------------*/ 773184610Salfredint 774184610Salfredusb_setup_endpoint(struct usb_device *dev, 775193074Sthompsa struct usb_host_endpoint *uhe, usb_size_t bufsize) 776184610Salfred{ 777192984Sthompsa struct usb_config cfg[2]; 778184610Salfred uint8_t type = uhe->desc.bmAttributes & UE_XFERTYPE; 779184610Salfred uint8_t addr = uhe->desc.bEndpointAddress; 780184610Salfred 781184610Salfred if (uhe->fbsd_buf_size == bufsize) { 782184610Salfred /* optimize */ 783184610Salfred return (0); 784184610Salfred } 785194228Sthompsa usbd_transfer_unsetup(uhe->bsd_xfer, 2); 786184610Salfred 787184610Salfred uhe->fbsd_buf_size = bufsize; 788184610Salfred 789184610Salfred if (bufsize == 0) { 790184610Salfred return (0); 791184610Salfred } 792227461Shselasky memset(cfg, 0, sizeof(cfg)); 793184610Salfred 794184610Salfred if (type == UE_ISOCHRONOUS) { 795184610Salfred 796184610Salfred /* 797184610Salfred * Isochronous transfers are special in that they don't fit 798184610Salfred * into the BULK/INTR/CONTROL transfer model. 799184610Salfred */ 800184610Salfred 801184610Salfred cfg[0].type = type; 802184610Salfred cfg[0].endpoint = addr & UE_ADDR; 803184610Salfred cfg[0].direction = addr & (UE_DIR_OUT | UE_DIR_IN); 804190734Sthompsa cfg[0].callback = &usb_linux_isoc_callback; 805190734Sthompsa cfg[0].bufsize = 0; /* use wMaxPacketSize */ 806190734Sthompsa cfg[0].frames = usb_max_isoc_frames(dev); 807190734Sthompsa cfg[0].flags.proxy_buffer = 1; 808184610Salfred#if 0 809184610Salfred /* 810184610Salfred * The Linux USB API allows non back-to-back 811184610Salfred * isochronous frames which we do not support. If the 812184610Salfred * isochronous frames are not back-to-back we need to 813184610Salfred * do a copy, and then we need a buffer for 814184610Salfred * that. Enable this at your own risk. 815184610Salfred */ 816190734Sthompsa cfg[0].flags.ext_buffer = 1; 817184610Salfred#endif 818190734Sthompsa cfg[0].flags.short_xfer_ok = 1; 819184610Salfred 820184610Salfred bcopy(cfg, cfg + 1, sizeof(*cfg)); 821184610Salfred 822184610Salfred /* Allocate and setup two generic FreeBSD USB transfers */ 823184610Salfred 824194228Sthompsa if (usbd_transfer_setup(dev, &uhe->bsd_iface_index, 825184610Salfred uhe->bsd_xfer, cfg, 2, uhe, &Giant)) { 826184610Salfred return (-EINVAL); 827184610Salfred } 828184610Salfred } else { 829184610Salfred if (bufsize > (1 << 22)) { 830184610Salfred /* limit buffer size */ 831184610Salfred bufsize = (1 << 22); 832184610Salfred } 833184610Salfred /* Allocate and setup one generic FreeBSD USB transfer */ 834184610Salfred 835184610Salfred cfg[0].type = type; 836184610Salfred cfg[0].endpoint = addr & UE_ADDR; 837184610Salfred cfg[0].direction = addr & (UE_DIR_OUT | UE_DIR_IN); 838190734Sthompsa cfg[0].callback = &usb_linux_non_isoc_callback; 839190734Sthompsa cfg[0].bufsize = bufsize; 840190734Sthompsa cfg[0].flags.ext_buffer = 1; /* enable zero-copy */ 841190734Sthompsa cfg[0].flags.proxy_buffer = 1; 842190734Sthompsa cfg[0].flags.short_xfer_ok = 1; 843184610Salfred 844194228Sthompsa if (usbd_transfer_setup(dev, &uhe->bsd_iface_index, 845184610Salfred uhe->bsd_xfer, cfg, 1, uhe, &Giant)) { 846184610Salfred return (-EINVAL); 847184610Salfred } 848184610Salfred } 849184610Salfred return (0); 850184610Salfred} 851184610Salfred 852184610Salfred/*------------------------------------------------------------------------* 853184610Salfred * usb_linux_create_usb_device 854184610Salfred * 855184610Salfred * The following function is used to build up a per USB device 856184610Salfred * structure tree, that mimics the Linux one. The root structure 857184610Salfred * is returned by this function. 858184610Salfred *------------------------------------------------------------------------*/ 859192984Sthompsastatic int 860192984Sthompsausb_linux_create_usb_device(struct usb_device *udev, device_t dev) 861184610Salfred{ 862194228Sthompsa struct usb_config_descriptor *cd = usbd_get_config_descriptor(udev); 863192984Sthompsa struct usb_descriptor *desc; 864192984Sthompsa struct usb_interface_descriptor *id; 865192984Sthompsa struct usb_endpoint_descriptor *ed; 866184610Salfred struct usb_interface *p_ui = NULL; 867184610Salfred struct usb_host_interface *p_uhi = NULL; 868184610Salfred struct usb_host_endpoint *p_uhe = NULL; 869193074Sthompsa usb_size_t size; 870184610Salfred uint16_t niface_total; 871184610Salfred uint16_t nedesc; 872184610Salfred uint16_t iface_no_curr; 873184610Salfred uint16_t iface_index; 874184610Salfred uint8_t pass; 875184610Salfred uint8_t iface_no; 876184610Salfred 877184610Salfred /* 878184610Salfred * We do two passes. One pass for computing necessary memory size 879184610Salfred * and one pass to initialize all the allocated memory structures. 880184610Salfred */ 881184610Salfred for (pass = 0; pass < 2; pass++) { 882184610Salfred 883233774Shselasky iface_no_curr = 0xFFFF; 884184610Salfred niface_total = 0; 885184610Salfred iface_index = 0; 886184610Salfred nedesc = 0; 887184610Salfred desc = NULL; 888184610Salfred 889184610Salfred /* 890184610Salfred * Iterate over all the USB descriptors. Use the USB config 891184610Salfred * descriptor pointer provided by the FreeBSD USB stack. 892184610Salfred */ 893194228Sthompsa while ((desc = usb_desc_foreach(cd, desc))) { 894184610Salfred 895184610Salfred /* 896184610Salfred * Build up a tree according to the descriptors we 897184610Salfred * find: 898184610Salfred */ 899184610Salfred switch (desc->bDescriptorType) { 900184610Salfred case UDESC_DEVICE: 901184610Salfred break; 902184610Salfred 903184610Salfred case UDESC_ENDPOINT: 904184610Salfred ed = (void *)desc; 905184610Salfred if ((ed->bLength < sizeof(*ed)) || 906184610Salfred (iface_index == 0)) 907184610Salfred break; 908184610Salfred if (p_uhe) { 909184610Salfred bcopy(ed, &p_uhe->desc, sizeof(p_uhe->desc)); 910184610Salfred p_uhe->bsd_iface_index = iface_index - 1; 911205803Sthompsa TAILQ_INIT(&p_uhe->bsd_urb_list); 912184610Salfred p_uhe++; 913184610Salfred } 914184610Salfred if (p_uhi) { 915184610Salfred (p_uhi - 1)->desc.bNumEndpoints++; 916184610Salfred } 917184610Salfred nedesc++; 918184610Salfred break; 919184610Salfred 920184610Salfred case UDESC_INTERFACE: 921184610Salfred id = (void *)desc; 922184610Salfred if (id->bLength < sizeof(*id)) 923184610Salfred break; 924184610Salfred if (p_uhi) { 925184610Salfred bcopy(id, &p_uhi->desc, sizeof(p_uhi->desc)); 926184610Salfred p_uhi->desc.bNumEndpoints = 0; 927184610Salfred p_uhi->endpoint = p_uhe; 928184610Salfred p_uhi->string = ""; 929184610Salfred p_uhi->bsd_iface_index = iface_index; 930184610Salfred p_uhi++; 931184610Salfred } 932184610Salfred iface_no = id->bInterfaceNumber; 933184610Salfred niface_total++; 934184610Salfred if (iface_no_curr != iface_no) { 935184610Salfred if (p_ui) { 936184610Salfred p_ui->altsetting = p_uhi - 1; 937184610Salfred p_ui->cur_altsetting = p_uhi - 1; 938184610Salfred p_ui->num_altsetting = 1; 939184610Salfred p_ui->bsd_iface_index = iface_index; 940192984Sthompsa p_ui->linux_udev = udev; 941184610Salfred p_ui++; 942184610Salfred } 943184610Salfred iface_no_curr = iface_no; 944184610Salfred iface_index++; 945184610Salfred } else { 946184610Salfred if (p_ui) { 947184610Salfred (p_ui - 1)->num_altsetting++; 948184610Salfred } 949184610Salfred } 950184610Salfred break; 951184610Salfred 952184610Salfred default: 953184610Salfred break; 954184610Salfred } 955184610Salfred } 956184610Salfred 957184610Salfred if (pass == 0) { 958184610Salfred 959192984Sthompsa size = (sizeof(*p_uhe) * nedesc) + 960184610Salfred (sizeof(*p_ui) * iface_index) + 961192984Sthompsa (sizeof(*p_uhi) * niface_total); 962184610Salfred 963192984Sthompsa p_uhe = malloc(size, M_USBDEV, M_WAITOK | M_ZERO); 964184610Salfred p_ui = (void *)(p_uhe + nedesc); 965184610Salfred p_uhi = (void *)(p_ui + iface_index); 966184610Salfred 967192984Sthompsa udev->linux_iface_start = p_ui; 968192984Sthompsa udev->linux_iface_end = p_ui + iface_index; 969192984Sthompsa udev->linux_endpoint_start = p_uhe; 970192984Sthompsa udev->linux_endpoint_end = p_uhe + nedesc; 971192984Sthompsa udev->devnum = device_get_unit(dev); 972192984Sthompsa bcopy(&udev->ddesc, &udev->descriptor, 973192984Sthompsa sizeof(udev->descriptor)); 974207080Sthompsa bcopy(udev->ctrl_ep.edesc, &udev->ep0.desc, 975192984Sthompsa sizeof(udev->ep0.desc)); 976184610Salfred } 977184610Salfred } 978192984Sthompsa return (0); 979184610Salfred} 980184610Salfred 981184610Salfred/*------------------------------------------------------------------------* 982184610Salfred * usb_alloc_urb 983184610Salfred * 984184610Salfred * This function should always be used when you allocate an URB for 985184610Salfred * use with the USB Linux stack. In case of an isochronous transfer 986184610Salfred * you must specifiy the maximum number of "iso_packets" which you 987184610Salfred * plan to transfer per URB. This function is always blocking, and 988184610Salfred * "mem_flags" are not regarded like on Linux. 989184610Salfred *------------------------------------------------------------------------*/ 990184610Salfredstruct urb * 991184610Salfredusb_alloc_urb(uint16_t iso_packets, uint16_t mem_flags) 992184610Salfred{ 993184610Salfred struct urb *urb; 994193074Sthompsa usb_size_t size; 995184610Salfred 996184610Salfred if (iso_packets == 0xFFFF) { 997184610Salfred /* 998184610Salfred * FreeBSD specific magic value to ask for control transfer 999184610Salfred * memory allocation: 1000184610Salfred */ 1001192984Sthompsa size = sizeof(*urb) + sizeof(struct usb_device_request) + mem_flags; 1002184610Salfred } else { 1003184610Salfred size = sizeof(*urb) + (iso_packets * sizeof(urb->iso_frame_desc[0])); 1004184610Salfred } 1005184610Salfred 1006184610Salfred urb = malloc(size, M_USBDEV, M_WAITOK | M_ZERO); 1007184610Salfred if (urb) { 1008184610Salfred 1009194227Sthompsa cv_init(&urb->cv_wait, "URBWAIT"); 1010184610Salfred if (iso_packets == 0xFFFF) { 1011184610Salfred urb->setup_packet = (void *)(urb + 1); 1012184610Salfred urb->transfer_buffer = (void *)(urb->setup_packet + 1013192984Sthompsa sizeof(struct usb_device_request)); 1014184610Salfred } else { 1015184610Salfred urb->number_of_packets = iso_packets; 1016184610Salfred } 1017184610Salfred } 1018184610Salfred return (urb); 1019184610Salfred} 1020184610Salfred 1021184610Salfred/*------------------------------------------------------------------------* 1022184610Salfred * usb_find_host_endpoint 1023184610Salfred * 1024184610Salfred * The following function will return the Linux USB host endpoint 1025184610Salfred * structure that matches the given endpoint type and endpoint 1026184610Salfred * value. If no match is found, NULL is returned. This function is not 1027184610Salfred * part of the Linux USB API and is only used internally. 1028184610Salfred *------------------------------------------------------------------------*/ 1029184610Salfredstruct usb_host_endpoint * 1030184610Salfredusb_find_host_endpoint(struct usb_device *dev, uint8_t type, uint8_t ep) 1031184610Salfred{ 1032184610Salfred struct usb_host_endpoint *uhe; 1033184610Salfred struct usb_host_endpoint *uhe_end; 1034184610Salfred struct usb_host_interface *uhi; 1035184610Salfred struct usb_interface *ui; 1036184610Salfred uint8_t ea; 1037184610Salfred uint8_t at; 1038184610Salfred uint8_t mask; 1039184610Salfred 1040184610Salfred if (dev == NULL) { 1041184610Salfred return (NULL); 1042184610Salfred } 1043184610Salfred if (type == UE_CONTROL) { 1044184610Salfred mask = UE_ADDR; 1045184610Salfred } else { 1046184610Salfred mask = (UE_DIR_IN | UE_DIR_OUT | UE_ADDR); 1047184610Salfred } 1048184610Salfred 1049184610Salfred ep &= mask; 1050184610Salfred 1051184610Salfred /* 1052184610Salfred * Iterate over all the interfaces searching the selected alternate 1053184610Salfred * setting only, and all belonging endpoints. 1054184610Salfred */ 1055192984Sthompsa for (ui = dev->linux_iface_start; 1056192984Sthompsa ui != dev->linux_iface_end; 1057184610Salfred ui++) { 1058184610Salfred uhi = ui->cur_altsetting; 1059184610Salfred if (uhi) { 1060184610Salfred uhe_end = uhi->endpoint + uhi->desc.bNumEndpoints; 1061184610Salfred for (uhe = uhi->endpoint; 1062184610Salfred uhe != uhe_end; 1063184610Salfred uhe++) { 1064184610Salfred ea = uhe->desc.bEndpointAddress; 1065184610Salfred at = uhe->desc.bmAttributes; 1066184610Salfred 1067184610Salfred if (((ea & mask) == ep) && 1068184610Salfred ((at & UE_XFERTYPE) == type)) { 1069184610Salfred return (uhe); 1070184610Salfred } 1071184610Salfred } 1072184610Salfred } 1073184610Salfred } 1074184610Salfred 1075184610Salfred if ((type == UE_CONTROL) && ((ep & UE_ADDR) == 0)) { 1076184610Salfred return (&dev->ep0); 1077184610Salfred } 1078184610Salfred return (NULL); 1079184610Salfred} 1080184610Salfred 1081184610Salfred/*------------------------------------------------------------------------* 1082184610Salfred * usb_altnum_to_altsetting 1083184610Salfred * 1084184610Salfred * The following function returns a pointer to an alternate setting by 1085184610Salfred * index given a "usb_interface" pointer. If the alternate setting by 1086184610Salfred * index does not exist, NULL is returned. And alternate setting is a 1087184610Salfred * variant of an interface, but usually with slightly different 1088184610Salfred * characteristics. 1089184610Salfred *------------------------------------------------------------------------*/ 1090184610Salfredstruct usb_host_interface * 1091184610Salfredusb_altnum_to_altsetting(const struct usb_interface *intf, uint8_t alt_index) 1092184610Salfred{ 1093184610Salfred if (alt_index >= intf->num_altsetting) { 1094184610Salfred return (NULL); 1095184610Salfred } 1096184610Salfred return (intf->altsetting + alt_index); 1097184610Salfred} 1098184610Salfred 1099184610Salfred/*------------------------------------------------------------------------* 1100184610Salfred * usb_ifnum_to_if 1101184610Salfred * 1102184610Salfred * The following function searches up an USB interface by 1103184610Salfred * "bInterfaceNumber". If no match is found, NULL is returned. 1104184610Salfred *------------------------------------------------------------------------*/ 1105184610Salfredstruct usb_interface * 1106184610Salfredusb_ifnum_to_if(struct usb_device *dev, uint8_t iface_no) 1107184610Salfred{ 1108184610Salfred struct usb_interface *p_ui; 1109184610Salfred 1110192984Sthompsa for (p_ui = dev->linux_iface_start; 1111192984Sthompsa p_ui != dev->linux_iface_end; 1112184610Salfred p_ui++) { 1113184610Salfred if ((p_ui->num_altsetting > 0) && 1114184610Salfred (p_ui->altsetting->desc.bInterfaceNumber == iface_no)) { 1115184610Salfred return (p_ui); 1116184610Salfred } 1117184610Salfred } 1118184610Salfred return (NULL); 1119184610Salfred} 1120184610Salfred 1121184610Salfred/*------------------------------------------------------------------------* 1122184610Salfred * usb_buffer_alloc 1123184610Salfred *------------------------------------------------------------------------*/ 1124184610Salfredvoid * 1125193074Sthompsausb_buffer_alloc(struct usb_device *dev, usb_size_t size, uint16_t mem_flags, uint8_t *dma_addr) 1126184610Salfred{ 1127184610Salfred return (malloc(size, M_USBDEV, M_WAITOK | M_ZERO)); 1128184610Salfred} 1129184610Salfred 1130184610Salfred/*------------------------------------------------------------------------* 1131194228Sthompsa * usbd_get_intfdata 1132184610Salfred *------------------------------------------------------------------------*/ 1133184610Salfredvoid * 1134194228Sthompsausbd_get_intfdata(struct usb_interface *intf) 1135184610Salfred{ 1136184610Salfred return (intf->bsd_priv_sc); 1137184610Salfred} 1138184610Salfred 1139184610Salfred/*------------------------------------------------------------------------* 1140184610Salfred * usb_linux_register 1141184610Salfred * 1142184610Salfred * The following function is used by the "USB_DRIVER_EXPORT()" macro, 1143184610Salfred * and is used to register a Linux USB driver, so that its 1144184610Salfred * "usb_device_id" structures gets searched a probe time. This 1145184610Salfred * function is not part of the Linux USB API, and is for internal use 1146184610Salfred * only. 1147184610Salfred *------------------------------------------------------------------------*/ 1148184610Salfredvoid 1149184610Salfredusb_linux_register(void *arg) 1150184610Salfred{ 1151184610Salfred struct usb_driver *drv = arg; 1152184610Salfred 1153196403Sjhb mtx_lock(&Giant); 1154184610Salfred LIST_INSERT_HEAD(&usb_linux_driver_list, drv, linux_driver_list); 1155196403Sjhb mtx_unlock(&Giant); 1156184610Salfred 1157194228Sthompsa usb_needs_explore_all(); 1158184610Salfred} 1159184610Salfred 1160184610Salfred/*------------------------------------------------------------------------* 1161184610Salfred * usb_linux_deregister 1162184610Salfred * 1163184610Salfred * The following function is used by the "USB_DRIVER_EXPORT()" macro, 1164184610Salfred * and is used to deregister a Linux USB driver. This function will 1165184610Salfred * ensure that all driver instances belonging to the Linux USB device 1166184610Salfred * driver in question, gets detached before the driver is 1167184610Salfred * unloaded. This function is not part of the Linux USB API, and is 1168184610Salfred * for internal use only. 1169184610Salfred *------------------------------------------------------------------------*/ 1170184610Salfredvoid 1171184610Salfredusb_linux_deregister(void *arg) 1172184610Salfred{ 1173184610Salfred struct usb_driver *drv = arg; 1174184610Salfred struct usb_linux_softc *sc; 1175184610Salfred 1176184610Salfredrepeat: 1177196403Sjhb mtx_lock(&Giant); 1178184610Salfred LIST_FOREACH(sc, &usb_linux_attached_list, sc_attached_list) { 1179184610Salfred if (sc->sc_udrv == drv) { 1180196403Sjhb mtx_unlock(&Giant); 1181184610Salfred device_detach(sc->sc_fbsd_dev); 1182184610Salfred goto repeat; 1183184610Salfred } 1184184610Salfred } 1185184610Salfred LIST_REMOVE(drv, linux_driver_list); 1186196403Sjhb mtx_unlock(&Giant); 1187184610Salfred} 1188184610Salfred 1189184610Salfred/*------------------------------------------------------------------------* 1190184610Salfred * usb_linux_free_device 1191184610Salfred * 1192184610Salfred * The following function is only used by the FreeBSD USB stack, to 1193184610Salfred * cleanup and free memory after that a Linux USB device was attached. 1194184610Salfred *------------------------------------------------------------------------*/ 1195184610Salfredvoid 1196184610Salfredusb_linux_free_device(struct usb_device *dev) 1197184610Salfred{ 1198184610Salfred struct usb_host_endpoint *uhe; 1199184610Salfred struct usb_host_endpoint *uhe_end; 1200184610Salfred int err; 1201184610Salfred 1202192984Sthompsa uhe = dev->linux_endpoint_start; 1203192984Sthompsa uhe_end = dev->linux_endpoint_end; 1204184610Salfred while (uhe != uhe_end) { 1205184610Salfred err = usb_setup_endpoint(dev, uhe, 0); 1206184610Salfred uhe++; 1207184610Salfred } 1208184610Salfred err = usb_setup_endpoint(dev, &dev->ep0, 0); 1209194065Sthompsa free(dev->linux_endpoint_start, M_USBDEV); 1210184610Salfred} 1211184610Salfred 1212184610Salfred/*------------------------------------------------------------------------* 1213184610Salfred * usb_buffer_free 1214184610Salfred *------------------------------------------------------------------------*/ 1215184610Salfredvoid 1216193074Sthompsausb_buffer_free(struct usb_device *dev, usb_size_t size, 1217184610Salfred void *addr, uint8_t dma_addr) 1218184610Salfred{ 1219184610Salfred free(addr, M_USBDEV); 1220184610Salfred} 1221184610Salfred 1222184610Salfred/*------------------------------------------------------------------------* 1223184610Salfred * usb_free_urb 1224184610Salfred *------------------------------------------------------------------------*/ 1225184610Salfredvoid 1226184610Salfredusb_free_urb(struct urb *urb) 1227184610Salfred{ 1228184610Salfred if (urb == NULL) { 1229184610Salfred return; 1230184610Salfred } 1231184610Salfred /* make sure that the current URB is not active */ 1232184610Salfred usb_kill_urb(urb); 1233184610Salfred 1234184610Salfred /* destroy condition variable */ 1235194227Sthompsa cv_destroy(&urb->cv_wait); 1236184610Salfred 1237184610Salfred /* just free it */ 1238184610Salfred free(urb, M_USBDEV); 1239184610Salfred} 1240184610Salfred 1241184610Salfred/*------------------------------------------------------------------------* 1242184610Salfred * usb_init_urb 1243184610Salfred * 1244184610Salfred * The following function can be used to initialize a custom URB. It 1245184610Salfred * is not recommended to use this function. Use "usb_alloc_urb()" 1246184610Salfred * instead. 1247184610Salfred *------------------------------------------------------------------------*/ 1248184610Salfredvoid 1249184610Salfredusb_init_urb(struct urb *urb) 1250184610Salfred{ 1251184610Salfred if (urb == NULL) { 1252184610Salfred return; 1253184610Salfred } 1254227461Shselasky memset(urb, 0, sizeof(*urb)); 1255184610Salfred} 1256184610Salfred 1257184610Salfred/*------------------------------------------------------------------------* 1258184610Salfred * usb_kill_urb 1259184610Salfred *------------------------------------------------------------------------*/ 1260184610Salfredvoid 1261184610Salfredusb_kill_urb(struct urb *urb) 1262184610Salfred{ 1263195966Salfred usb_unlink_urb_sub(urb, 1); 1264184610Salfred} 1265184610Salfred 1266184610Salfred/*------------------------------------------------------------------------* 1267184610Salfred * usb_set_intfdata 1268184610Salfred * 1269184610Salfred * The following function sets the per Linux USB interface private 1270184610Salfred * data pointer. It is used by most Linux USB device drivers. 1271184610Salfred *------------------------------------------------------------------------*/ 1272184610Salfredvoid 1273184610Salfredusb_set_intfdata(struct usb_interface *intf, void *data) 1274184610Salfred{ 1275184610Salfred intf->bsd_priv_sc = data; 1276184610Salfred} 1277184610Salfred 1278184610Salfred/*------------------------------------------------------------------------* 1279184610Salfred * usb_linux_cleanup_interface 1280184610Salfred * 1281184610Salfred * The following function will release all FreeBSD USB transfers 1282184610Salfred * associated with a Linux USB interface. It is for internal use only. 1283184610Salfred *------------------------------------------------------------------------*/ 1284184610Salfredstatic void 1285184610Salfredusb_linux_cleanup_interface(struct usb_device *dev, struct usb_interface *iface) 1286184610Salfred{ 1287184610Salfred struct usb_host_interface *uhi; 1288184610Salfred struct usb_host_interface *uhi_end; 1289184610Salfred struct usb_host_endpoint *uhe; 1290184610Salfred struct usb_host_endpoint *uhe_end; 1291184610Salfred int err; 1292184610Salfred 1293184610Salfred uhi = iface->altsetting; 1294184610Salfred uhi_end = iface->altsetting + iface->num_altsetting; 1295184610Salfred while (uhi != uhi_end) { 1296184610Salfred uhe = uhi->endpoint; 1297184610Salfred uhe_end = uhi->endpoint + uhi->desc.bNumEndpoints; 1298184610Salfred while (uhe != uhe_end) { 1299184610Salfred err = usb_setup_endpoint(dev, uhe, 0); 1300184610Salfred uhe++; 1301184610Salfred } 1302184610Salfred uhi++; 1303184610Salfred } 1304184610Salfred} 1305184610Salfred 1306184610Salfred/*------------------------------------------------------------------------* 1307184610Salfred * usb_linux_wait_complete 1308184610Salfred * 1309184610Salfred * The following function is used by "usb_start_wait_urb()" to wake it 1310184610Salfred * up, when an USB transfer has finished. 1311184610Salfred *------------------------------------------------------------------------*/ 1312184610Salfredstatic void 1313184610Salfredusb_linux_wait_complete(struct urb *urb) 1314184610Salfred{ 1315184610Salfred if (urb->transfer_flags & URB_IS_SLEEPING) { 1316194227Sthompsa cv_signal(&urb->cv_wait); 1317184610Salfred } 1318184610Salfred urb->transfer_flags &= ~URB_WAIT_WAKEUP; 1319184610Salfred} 1320184610Salfred 1321184610Salfred/*------------------------------------------------------------------------* 1322184610Salfred * usb_linux_complete 1323184610Salfred *------------------------------------------------------------------------*/ 1324184610Salfredstatic void 1325192984Sthompsausb_linux_complete(struct usb_xfer *xfer) 1326184610Salfred{ 1327184610Salfred struct urb *urb; 1328184610Salfred 1329194677Sthompsa urb = usbd_xfer_get_priv(xfer); 1330194677Sthompsa usbd_xfer_set_priv(xfer, NULL); 1331184610Salfred if (urb->complete) { 1332184610Salfred (urb->complete) (urb); 1333184610Salfred } 1334184610Salfred} 1335184610Salfred 1336184610Salfred/*------------------------------------------------------------------------* 1337184610Salfred * usb_linux_isoc_callback 1338184610Salfred * 1339184610Salfred * The following is the FreeBSD isochronous USB callback. Isochronous 1340184610Salfred * frames are USB packets transferred 1000 or 8000 times per second, 1341184610Salfred * depending on whether a full- or high- speed USB transfer is 1342184610Salfred * used. 1343184610Salfred *------------------------------------------------------------------------*/ 1344184610Salfredstatic void 1345194677Sthompsausb_linux_isoc_callback(struct usb_xfer *xfer, usb_error_t error) 1346184610Salfred{ 1347193045Sthompsa usb_frlength_t max_frame = xfer->max_frame_size; 1348193045Sthompsa usb_frlength_t offset; 1349193045Sthompsa usb_frcount_t x; 1350194677Sthompsa struct urb *urb = usbd_xfer_get_priv(xfer); 1351194677Sthompsa struct usb_host_endpoint *uhe = usbd_xfer_softc(xfer); 1352184610Salfred struct usb_iso_packet_descriptor *uipd; 1353184610Salfred 1354184610Salfred DPRINTF("\n"); 1355184610Salfred 1356184610Salfred switch (USB_GET_STATE(xfer)) { 1357184610Salfred case USB_ST_TRANSFERRED: 1358184610Salfred 1359184610Salfred if (urb->bsd_isread) { 1360184610Salfred 1361184610Salfred /* copy in data with regard to the URB */ 1362184610Salfred 1363184610Salfred offset = 0; 1364184610Salfred 1365184610Salfred for (x = 0; x < urb->number_of_packets; x++) { 1366184610Salfred uipd = urb->iso_frame_desc + x; 1367198776Sthompsa if (uipd->length > xfer->frlengths[x]) { 1368198776Sthompsa if (urb->transfer_flags & URB_SHORT_NOT_OK) { 1369198776Sthompsa /* XXX should be EREMOTEIO */ 1370198776Sthompsa uipd->status = -EPIPE; 1371198776Sthompsa } else { 1372198776Sthompsa uipd->status = 0; 1373198776Sthompsa } 1374198776Sthompsa } else { 1375198776Sthompsa uipd->status = 0; 1376198776Sthompsa } 1377184610Salfred uipd->actual_length = xfer->frlengths[x]; 1378184610Salfred if (!xfer->flags.ext_buffer) { 1379194228Sthompsa usbd_copy_out(xfer->frbuffers, offset, 1380184610Salfred USB_ADD_BYTES(urb->transfer_buffer, 1381184610Salfred uipd->offset), uipd->actual_length); 1382184610Salfred } 1383184610Salfred offset += max_frame; 1384184610Salfred } 1385184610Salfred } else { 1386184610Salfred for (x = 0; x < urb->number_of_packets; x++) { 1387184610Salfred uipd = urb->iso_frame_desc + x; 1388184610Salfred uipd->actual_length = xfer->frlengths[x]; 1389184610Salfred uipd->status = 0; 1390184610Salfred } 1391184610Salfred } 1392184610Salfred 1393184610Salfred urb->actual_length = xfer->actlen; 1394184610Salfred 1395184610Salfred /* check for short transfer */ 1396184610Salfred if (xfer->actlen < xfer->sumlen) { 1397184610Salfred /* short transfer */ 1398184610Salfred if (urb->transfer_flags & URB_SHORT_NOT_OK) { 1399198776Sthompsa /* XXX should be EREMOTEIO */ 1400198776Sthompsa urb->status = -EPIPE; 1401184610Salfred } else { 1402184610Salfred urb->status = 0; 1403184610Salfred } 1404184610Salfred } else { 1405184610Salfred /* success */ 1406184610Salfred urb->status = 0; 1407184610Salfred } 1408184610Salfred 1409184610Salfred /* call callback */ 1410184610Salfred usb_linux_complete(xfer); 1411184610Salfred 1412184610Salfred case USB_ST_SETUP: 1413184610Salfredtr_setup: 1414184610Salfred 1415184610Salfred if (xfer->priv_fifo == NULL) { 1416184610Salfred 1417184610Salfred /* get next transfer */ 1418184610Salfred urb = TAILQ_FIRST(&uhe->bsd_urb_list); 1419184610Salfred if (urb == NULL) { 1420184610Salfred /* nothing to do */ 1421184610Salfred return; 1422184610Salfred } 1423184610Salfred TAILQ_REMOVE(&uhe->bsd_urb_list, urb, bsd_urb_list); 1424184610Salfred urb->bsd_urb_list.tqe_prev = NULL; 1425184610Salfred 1426184610Salfred x = xfer->max_frame_count; 1427184610Salfred if (urb->number_of_packets > x) { 1428184610Salfred /* XXX simply truncate the transfer */ 1429184610Salfred urb->number_of_packets = x; 1430184610Salfred } 1431184610Salfred } else { 1432184610Salfred DPRINTF("Already got a transfer\n"); 1433184610Salfred 1434184610Salfred /* already got a transfer (should not happen) */ 1435194677Sthompsa urb = usbd_xfer_get_priv(xfer); 1436184610Salfred } 1437184610Salfred 1438184610Salfred urb->bsd_isread = (uhe->desc.bEndpointAddress & UE_DIR_IN) ? 1 : 0; 1439184610Salfred 1440194677Sthompsa if (xfer->flags.ext_buffer) { 1441194677Sthompsa /* set virtual address to load */ 1442194677Sthompsa usbd_xfer_set_frame_data(xfer, 0, urb->transfer_buffer, 0); 1443194677Sthompsa } 1444184610Salfred if (!(urb->bsd_isread)) { 1445184610Salfred 1446184610Salfred /* copy out data with regard to the URB */ 1447184610Salfred 1448184610Salfred offset = 0; 1449184610Salfred 1450184610Salfred for (x = 0; x < urb->number_of_packets; x++) { 1451184610Salfred uipd = urb->iso_frame_desc + x; 1452194677Sthompsa usbd_xfer_set_frame_len(xfer, x, uipd->length); 1453184610Salfred if (!xfer->flags.ext_buffer) { 1454194228Sthompsa usbd_copy_in(xfer->frbuffers, offset, 1455184610Salfred USB_ADD_BYTES(urb->transfer_buffer, 1456184610Salfred uipd->offset), uipd->length); 1457184610Salfred } 1458184610Salfred offset += uipd->length; 1459184610Salfred } 1460184610Salfred } else { 1461184610Salfred 1462184610Salfred /* 1463184610Salfred * compute the transfer length into the "offset" 1464184610Salfred * variable 1465184610Salfred */ 1466184610Salfred 1467184610Salfred offset = urb->number_of_packets * max_frame; 1468184610Salfred 1469184610Salfred /* setup "frlengths" array */ 1470184610Salfred 1471184610Salfred for (x = 0; x < urb->number_of_packets; x++) { 1472184610Salfred uipd = urb->iso_frame_desc + x; 1473194677Sthompsa usbd_xfer_set_frame_len(xfer, x, max_frame); 1474184610Salfred } 1475184610Salfred } 1476194677Sthompsa usbd_xfer_set_priv(xfer, urb); 1477184610Salfred xfer->flags.force_short_xfer = 0; 1478184610Salfred xfer->timeout = urb->timeout; 1479184610Salfred xfer->nframes = urb->number_of_packets; 1480194228Sthompsa usbd_transfer_submit(xfer); 1481184610Salfred return; 1482184610Salfred 1483184610Salfred default: /* Error */ 1484184610Salfred if (xfer->error == USB_ERR_CANCELLED) { 1485184610Salfred urb->status = -ECONNRESET; 1486184610Salfred } else { 1487184610Salfred urb->status = -EPIPE; /* stalled */ 1488184610Salfred } 1489184610Salfred 1490184610Salfred /* Set zero for "actual_length" */ 1491184610Salfred urb->actual_length = 0; 1492184610Salfred 1493184610Salfred /* Set zero for "actual_length" */ 1494184610Salfred for (x = 0; x < urb->number_of_packets; x++) { 1495184610Salfred urb->iso_frame_desc[x].actual_length = 0; 1496198776Sthompsa urb->iso_frame_desc[x].status = urb->status; 1497184610Salfred } 1498184610Salfred 1499184610Salfred /* call callback */ 1500184610Salfred usb_linux_complete(xfer); 1501184610Salfred 1502184610Salfred if (xfer->error == USB_ERR_CANCELLED) { 1503184610Salfred /* we need to return in this case */ 1504184610Salfred return; 1505184610Salfred } 1506184610Salfred goto tr_setup; 1507184610Salfred 1508184610Salfred } 1509184610Salfred} 1510184610Salfred 1511184610Salfred/*------------------------------------------------------------------------* 1512184610Salfred * usb_linux_non_isoc_callback 1513184610Salfred * 1514184610Salfred * The following is the FreeBSD BULK/INTERRUPT and CONTROL USB 1515184610Salfred * callback. It dequeues Linux USB stack compatible URB's, transforms 1516184610Salfred * the URB fields into a FreeBSD USB transfer, and defragments the USB 1517184610Salfred * transfer as required. When the transfer is complete the "complete" 1518184610Salfred * callback is called. 1519184610Salfred *------------------------------------------------------------------------*/ 1520184610Salfredstatic void 1521194677Sthompsausb_linux_non_isoc_callback(struct usb_xfer *xfer, usb_error_t error) 1522184610Salfred{ 1523184610Salfred enum { 1524192984Sthompsa REQ_SIZE = sizeof(struct usb_device_request) 1525184610Salfred }; 1526194677Sthompsa struct urb *urb = usbd_xfer_get_priv(xfer); 1527194677Sthompsa struct usb_host_endpoint *uhe = usbd_xfer_softc(xfer); 1528184610Salfred uint8_t *ptr; 1529194677Sthompsa usb_frlength_t max_bulk = usbd_xfer_max_len(xfer); 1530184610Salfred uint8_t data_frame = xfer->flags_int.control_xfr ? 1 : 0; 1531184610Salfred 1532184610Salfred DPRINTF("\n"); 1533184610Salfred 1534184610Salfred switch (USB_GET_STATE(xfer)) { 1535184610Salfred case USB_ST_TRANSFERRED: 1536184610Salfred 1537184610Salfred if (xfer->flags_int.control_xfr) { 1538184610Salfred 1539184610Salfred /* don't transfer the setup packet again: */ 1540184610Salfred 1541194677Sthompsa usbd_xfer_set_frame_len(xfer, 0, 0); 1542184610Salfred } 1543184610Salfred if (urb->bsd_isread && (!xfer->flags.ext_buffer)) { 1544184610Salfred /* copy in data with regard to the URB */ 1545194228Sthompsa usbd_copy_out(xfer->frbuffers + data_frame, 0, 1546184610Salfred urb->bsd_data_ptr, xfer->frlengths[data_frame]); 1547184610Salfred } 1548184610Salfred urb->bsd_length_rem -= xfer->frlengths[data_frame]; 1549184610Salfred urb->bsd_data_ptr += xfer->frlengths[data_frame]; 1550184610Salfred urb->actual_length += xfer->frlengths[data_frame]; 1551184610Salfred 1552184610Salfred /* check for short transfer */ 1553184610Salfred if (xfer->actlen < xfer->sumlen) { 1554184610Salfred urb->bsd_length_rem = 0; 1555184610Salfred 1556184610Salfred /* short transfer */ 1557184610Salfred if (urb->transfer_flags & URB_SHORT_NOT_OK) { 1558184610Salfred urb->status = -EPIPE; 1559184610Salfred } else { 1560184610Salfred urb->status = 0; 1561184610Salfred } 1562184610Salfred } else { 1563184610Salfred /* check remainder */ 1564184610Salfred if (urb->bsd_length_rem > 0) { 1565184610Salfred goto setup_bulk; 1566184610Salfred } 1567184610Salfred /* success */ 1568184610Salfred urb->status = 0; 1569184610Salfred } 1570184610Salfred 1571184610Salfred /* call callback */ 1572184610Salfred usb_linux_complete(xfer); 1573184610Salfred 1574184610Salfred case USB_ST_SETUP: 1575184610Salfredtr_setup: 1576184610Salfred /* get next transfer */ 1577184610Salfred urb = TAILQ_FIRST(&uhe->bsd_urb_list); 1578184610Salfred if (urb == NULL) { 1579184610Salfred /* nothing to do */ 1580184610Salfred return; 1581184610Salfred } 1582184610Salfred TAILQ_REMOVE(&uhe->bsd_urb_list, urb, bsd_urb_list); 1583184610Salfred urb->bsd_urb_list.tqe_prev = NULL; 1584184610Salfred 1585194677Sthompsa usbd_xfer_set_priv(xfer, urb); 1586184610Salfred xfer->flags.force_short_xfer = 0; 1587184610Salfred xfer->timeout = urb->timeout; 1588184610Salfred 1589184610Salfred if (xfer->flags_int.control_xfr) { 1590184610Salfred 1591184610Salfred /* 1592184610Salfred * USB control transfers need special handling. 1593184610Salfred * First copy in the header, then copy in data! 1594184610Salfred */ 1595184610Salfred if (!xfer->flags.ext_buffer) { 1596194228Sthompsa usbd_copy_in(xfer->frbuffers, 0, 1597184610Salfred urb->setup_packet, REQ_SIZE); 1598194677Sthompsa usbd_xfer_set_frame_len(xfer, 0, REQ_SIZE); 1599184610Salfred } else { 1600184610Salfred /* set virtual address to load */ 1601194677Sthompsa usbd_xfer_set_frame_data(xfer, 0, 1602194677Sthompsa urb->setup_packet, REQ_SIZE); 1603184610Salfred } 1604184610Salfred 1605184610Salfred ptr = urb->setup_packet; 1606184610Salfred 1607184610Salfred /* setup data transfer direction and length */ 1608184610Salfred urb->bsd_isread = (ptr[0] & UT_READ) ? 1 : 0; 1609184610Salfred urb->bsd_length_rem = ptr[6] | (ptr[7] << 8); 1610184610Salfred 1611184610Salfred } else { 1612184610Salfred 1613184610Salfred /* setup data transfer direction */ 1614184610Salfred 1615184610Salfred urb->bsd_length_rem = urb->transfer_buffer_length; 1616184610Salfred urb->bsd_isread = (uhe->desc.bEndpointAddress & 1617184610Salfred UE_DIR_IN) ? 1 : 0; 1618184610Salfred } 1619184610Salfred 1620184610Salfred urb->bsd_data_ptr = urb->transfer_buffer; 1621184610Salfred urb->actual_length = 0; 1622184610Salfred 1623184610Salfredsetup_bulk: 1624184610Salfred if (max_bulk > urb->bsd_length_rem) { 1625184610Salfred max_bulk = urb->bsd_length_rem; 1626184610Salfred } 1627184610Salfred /* check if we need to force a short transfer */ 1628184610Salfred 1629184610Salfred if ((max_bulk == urb->bsd_length_rem) && 1630184610Salfred (urb->transfer_flags & URB_ZERO_PACKET) && 1631184610Salfred (!xfer->flags_int.control_xfr)) { 1632184610Salfred xfer->flags.force_short_xfer = 1; 1633184610Salfred } 1634184610Salfred /* check if we need to copy in data */ 1635184610Salfred 1636184610Salfred if (xfer->flags.ext_buffer) { 1637184610Salfred /* set virtual address to load */ 1638194677Sthompsa usbd_xfer_set_frame_data(xfer, data_frame, 1639194677Sthompsa urb->bsd_data_ptr, max_bulk); 1640184610Salfred } else if (!urb->bsd_isread) { 1641184610Salfred /* copy out data with regard to the URB */ 1642194228Sthompsa usbd_copy_in(xfer->frbuffers + data_frame, 0, 1643184610Salfred urb->bsd_data_ptr, max_bulk); 1644194677Sthompsa usbd_xfer_set_frame_len(xfer, data_frame, max_bulk); 1645184610Salfred } 1646184610Salfred if (xfer->flags_int.control_xfr) { 1647184610Salfred if (max_bulk > 0) { 1648184610Salfred xfer->nframes = 2; 1649184610Salfred } else { 1650184610Salfred xfer->nframes = 1; 1651184610Salfred } 1652184610Salfred } else { 1653184610Salfred xfer->nframes = 1; 1654184610Salfred } 1655194228Sthompsa usbd_transfer_submit(xfer); 1656184610Salfred return; 1657184610Salfred 1658184610Salfred default: 1659184610Salfred if (xfer->error == USB_ERR_CANCELLED) { 1660184610Salfred urb->status = -ECONNRESET; 1661184610Salfred } else { 1662184610Salfred urb->status = -EPIPE; 1663184610Salfred } 1664184610Salfred 1665184610Salfred /* Set zero for "actual_length" */ 1666184610Salfred urb->actual_length = 0; 1667184610Salfred 1668184610Salfred /* call callback */ 1669184610Salfred usb_linux_complete(xfer); 1670184610Salfred 1671184610Salfred if (xfer->error == USB_ERR_CANCELLED) { 1672184610Salfred /* we need to return in this case */ 1673184610Salfred return; 1674184610Salfred } 1675184610Salfred goto tr_setup; 1676184610Salfred } 1677184610Salfred} 1678198776Sthompsa 1679198776Sthompsa/*------------------------------------------------------------------------* 1680198776Sthompsa * usb_fill_bulk_urb 1681198776Sthompsa *------------------------------------------------------------------------*/ 1682198776Sthompsavoid 1683198776Sthompsausb_fill_bulk_urb(struct urb *urb, struct usb_device *udev, 1684198776Sthompsa struct usb_host_endpoint *uhe, void *buf, 1685198776Sthompsa int length, usb_complete_t callback, void *arg) 1686198776Sthompsa{ 1687198776Sthompsa urb->dev = udev; 1688198776Sthompsa urb->endpoint = uhe; 1689198776Sthompsa urb->transfer_buffer = buf; 1690198776Sthompsa urb->transfer_buffer_length = length; 1691198776Sthompsa urb->complete = callback; 1692198776Sthompsa urb->context = arg; 1693198776Sthompsa} 1694198776Sthompsa 1695198776Sthompsa/*------------------------------------------------------------------------* 1696198776Sthompsa * usb_bulk_msg 1697198776Sthompsa * 1698198776Sthompsa * NOTE: This function can also be used for interrupt endpoints! 1699198776Sthompsa * 1700198776Sthompsa * Return values: 1701198776Sthompsa * 0: Success 1702198776Sthompsa * Else: Failure 1703198776Sthompsa *------------------------------------------------------------------------*/ 1704198776Sthompsaint 1705198776Sthompsausb_bulk_msg(struct usb_device *udev, struct usb_host_endpoint *uhe, 1706198776Sthompsa void *data, int len, uint16_t *pactlen, usb_timeout_t timeout) 1707198776Sthompsa{ 1708198776Sthompsa struct urb *urb; 1709198776Sthompsa int err; 1710198776Sthompsa 1711198776Sthompsa if (uhe == NULL) 1712198776Sthompsa return (-EINVAL); 1713198776Sthompsa if (len < 0) 1714198776Sthompsa return (-EINVAL); 1715198776Sthompsa 1716198776Sthompsa err = usb_setup_endpoint(udev, uhe, 4096 /* bytes */); 1717198776Sthompsa if (err) 1718198776Sthompsa return (err); 1719198776Sthompsa 1720198776Sthompsa urb = usb_alloc_urb(0, 0); 1721198776Sthompsa if (urb == NULL) 1722198776Sthompsa return (-ENOMEM); 1723198776Sthompsa 1724198776Sthompsa usb_fill_bulk_urb(urb, udev, uhe, data, len, 1725198776Sthompsa usb_linux_wait_complete, NULL); 1726198776Sthompsa 1727198776Sthompsa err = usb_start_wait_urb(urb, timeout, pactlen); 1728198776Sthompsa 1729198776Sthompsa usb_free_urb(urb); 1730198776Sthompsa 1731198776Sthompsa return (err); 1732198776Sthompsa} 1733