Path: blob/main/sys/compat/linuxkpi/common/src/linux_usb.c
39586 views
/*-1* Copyright (c) 2007 Luigi Rizzo - Universita` di Pisa. All rights reserved.2* Copyright (c) 2007 Hans Petter Selasky. All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*/2526#ifdef USB_GLOBAL_INCLUDE_FILE27#include USB_GLOBAL_INCLUDE_FILE28#else29#include <sys/stdint.h>30#include <sys/stddef.h>31#include <sys/param.h>32#include <sys/queue.h>33#include <sys/types.h>34#include <sys/systm.h>35#include <sys/kernel.h>36#include <sys/bus.h>37#include <sys/module.h>38#include <sys/lock.h>39#include <sys/mutex.h>40#include <sys/condvar.h>41#include <sys/sysctl.h>42#include <sys/sx.h>43#include <sys/unistd.h>44#include <sys/callout.h>45#include <sys/malloc.h>46#include <sys/priv.h>4748#include <dev/usb/usb.h>49#include <dev/usb/usbdi.h>50#include <dev/usb/usbdi_util.h>5152#define USB_DEBUG_VAR usb_debug5354#include <dev/usb/usb_core.h>55#include <linux/usb.h>56#include <dev/usb/usb_process.h>57#include <dev/usb/usb_device.h>58#include <dev/usb/usb_util.h>59#include <dev/usb/usb_busdma.h>60#include <dev/usb/usb_transfer.h>61#include <dev/usb/usb_hub.h>62#include <dev/usb/usb_request.h>63#include <dev/usb/usb_debug.h>64#include <dev/usb/usb_dynamic.h>65#endif /* USB_GLOBAL_INCLUDE_FILE */6667struct usb_linux_softc {68LIST_ENTRY(usb_linux_softc) sc_attached_list;6970device_t sc_fbsd_dev;71struct usb_device *sc_fbsd_udev;72struct usb_interface *sc_ui;73struct usb_driver *sc_udrv;74};7576/* prototypes */77static device_probe_t usb_linux_probe;78static device_attach_t usb_linux_attach;79static device_detach_t usb_linux_detach;80static device_suspend_t usb_linux_suspend;81static device_resume_t usb_linux_resume;8283static usb_callback_t usb_linux_isoc_callback;84static usb_callback_t usb_linux_non_isoc_callback;8586static usb_complete_t usb_linux_wait_complete;8788static uint16_t usb_max_isoc_frames(struct usb_device *);89static int usb_start_wait_urb(struct urb *, usb_timeout_t, uint16_t *);90static const struct usb_device_id *usb_linux_lookup_id(91const struct usb_device_id *, struct usb_attach_arg *);92static struct usb_driver *usb_linux_get_usb_driver(struct usb_linux_softc *);93static int usb_linux_create_usb_device(struct usb_device *, device_t);94static void usb_linux_cleanup_interface(struct usb_device *,95struct usb_interface *);96static void usb_linux_complete(struct usb_xfer *);97static int usb_unlink_urb_sub(struct urb *, uint8_t);9899/*------------------------------------------------------------------------*100* FreeBSD USB interface101*------------------------------------------------------------------------*/102103static LIST_HEAD(, usb_linux_softc) usb_linux_attached_list;104static LIST_HEAD(, usb_driver) usb_linux_driver_list;105106static device_method_t usb_linux_methods[] = {107/* Device interface */108DEVMETHOD(device_probe, usb_linux_probe),109DEVMETHOD(device_attach, usb_linux_attach),110DEVMETHOD(device_detach, usb_linux_detach),111DEVMETHOD(device_suspend, usb_linux_suspend),112DEVMETHOD(device_resume, usb_linux_resume),113114DEVMETHOD_END115};116117static driver_t usb_linux_driver = {118.name = "usb_linux",119.methods = usb_linux_methods,120.size = sizeof(struct usb_linux_softc),121};122123DRIVER_MODULE(usb_linux, uhub, usb_linux_driver, NULL, NULL);124MODULE_VERSION(usb_linux, 1);125126/*------------------------------------------------------------------------*127* usb_linux_lookup_id128*129* This functions takes an array of "struct usb_device_id" and tries130* to match the entries with the information in "struct usb_attach_arg".131* If it finds a match the matching entry will be returned.132* Else "NULL" will be returned.133*------------------------------------------------------------------------*/134static const struct usb_device_id *135usb_linux_lookup_id(const struct usb_device_id *id, struct usb_attach_arg *uaa)136{137if (id == NULL) {138goto done;139}140/*141* Keep on matching array entries until we find one with142* "match_flags" equal to zero, which indicates the end of the143* array:144*/145for (; id->match_flags; id++) {146if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&147(id->idVendor != uaa->info.idVendor)) {148continue;149}150if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&151(id->idProduct != uaa->info.idProduct)) {152continue;153}154if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&155(id->bcdDevice_lo > uaa->info.bcdDevice)) {156continue;157}158if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&159(id->bcdDevice_hi < uaa->info.bcdDevice)) {160continue;161}162if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&163(id->bDeviceClass != uaa->info.bDeviceClass)) {164continue;165}166if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&167(id->bDeviceSubClass != uaa->info.bDeviceSubClass)) {168continue;169}170if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&171(id->bDeviceProtocol != uaa->info.bDeviceProtocol)) {172continue;173}174if ((uaa->info.bDeviceClass == 0xFF) &&175!(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&176(id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |177USB_DEVICE_ID_MATCH_INT_SUBCLASS |178USB_DEVICE_ID_MATCH_INT_PROTOCOL))) {179continue;180}181if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&182(id->bInterfaceClass != uaa->info.bInterfaceClass)) {183continue;184}185if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&186(id->bInterfaceSubClass != uaa->info.bInterfaceSubClass)) {187continue;188}189if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&190(id->bInterfaceProtocol != uaa->info.bInterfaceProtocol)) {191continue;192}193/* we found a match! */194return (id);195}196197done:198return (NULL);199}200201/*------------------------------------------------------------------------*202* usb_linux_probe203*204* This function is the FreeBSD probe callback. It is called from the205* FreeBSD USB stack through the "device_probe_and_attach()" function.206*------------------------------------------------------------------------*/207static int208usb_linux_probe(device_t dev)209{210struct usb_attach_arg *uaa = device_get_ivars(dev);211struct usb_driver *udrv;212int err = ENXIO;213214if (uaa->usb_mode != USB_MODE_HOST) {215return (ENXIO);216}217mtx_lock(&Giant);218LIST_FOREACH(udrv, &usb_linux_driver_list, linux_driver_list) {219if (usb_linux_lookup_id(udrv->id_table, uaa)) {220err = BUS_PROBE_DEFAULT;221break;222}223}224mtx_unlock(&Giant);225226return (err);227}228229/*------------------------------------------------------------------------*230* usb_linux_get_usb_driver231*232* This function returns the pointer to the "struct usb_driver" where233* the Linux USB device driver "struct usb_device_id" match was found.234* We apply a lock before reading out the pointer to avoid races.235*------------------------------------------------------------------------*/236static struct usb_driver *237usb_linux_get_usb_driver(struct usb_linux_softc *sc)238{239struct usb_driver *udrv;240241mtx_lock(&Giant);242udrv = sc->sc_udrv;243mtx_unlock(&Giant);244return (udrv);245}246247/*------------------------------------------------------------------------*248* usb_linux_attach249*250* This function is the FreeBSD attach callback. It is called from the251* FreeBSD USB stack through the "device_probe_and_attach()" function.252* This function is called when "usb_linux_probe()" returns zero.253*------------------------------------------------------------------------*/254static int255usb_linux_attach(device_t dev)256{257struct usb_attach_arg *uaa = device_get_ivars(dev);258struct usb_linux_softc *sc = device_get_softc(dev);259struct usb_driver *udrv;260const struct usb_device_id *id = NULL;261262mtx_lock(&Giant);263LIST_FOREACH(udrv, &usb_linux_driver_list, linux_driver_list) {264id = usb_linux_lookup_id(udrv->id_table, uaa);265if (id)266break;267}268mtx_unlock(&Giant);269270if (id == NULL) {271return (ENXIO);272}273if (usb_linux_create_usb_device(uaa->device, dev) != 0)274return (ENOMEM);275device_set_usb_desc(dev);276277sc->sc_fbsd_udev = uaa->device;278sc->sc_fbsd_dev = dev;279sc->sc_udrv = udrv;280sc->sc_ui = usb_ifnum_to_if(uaa->device, uaa->info.bIfaceNum);281if (sc->sc_ui == NULL) {282return (EINVAL);283}284if (udrv->probe) {285if ((udrv->probe) (sc->sc_ui, id)) {286return (ENXIO);287}288}289mtx_lock(&Giant);290LIST_INSERT_HEAD(&usb_linux_attached_list, sc, sc_attached_list);291mtx_unlock(&Giant);292293/* success */294return (0);295}296297/*------------------------------------------------------------------------*298* usb_linux_detach299*300* This function is the FreeBSD detach callback. It is called from the301* FreeBSD USB stack through the "device_detach()" function.302*------------------------------------------------------------------------*/303static int304usb_linux_detach(device_t dev)305{306struct usb_linux_softc *sc = device_get_softc(dev);307struct usb_driver *udrv = NULL;308309mtx_lock(&Giant);310if (sc->sc_attached_list.le_prev) {311LIST_REMOVE(sc, sc_attached_list);312sc->sc_attached_list.le_prev = NULL;313udrv = sc->sc_udrv;314sc->sc_udrv = NULL;315}316mtx_unlock(&Giant);317318if (udrv && udrv->disconnect) {319(udrv->disconnect) (sc->sc_ui);320}321/*322* Make sure that we free all FreeBSD USB transfers belonging to323* this Linux "usb_interface", hence they will most likely not be324* needed any more.325*/326usb_linux_cleanup_interface(sc->sc_fbsd_udev, sc->sc_ui);327return (0);328}329330/*------------------------------------------------------------------------*331* usb_linux_suspend332*333* This function is the FreeBSD suspend callback. Usually it does nothing.334*------------------------------------------------------------------------*/335static int336usb_linux_suspend(device_t dev)337{338struct usb_linux_softc *sc = device_get_softc(dev);339struct usb_driver *udrv = usb_linux_get_usb_driver(sc);340pm_message_t pm_msg;341int err;342343err = 0;344if (udrv && udrv->suspend) {345pm_msg.event = 0; /* XXX */346err = (udrv->suspend) (sc->sc_ui, pm_msg);347}348return (-err);349}350351/*------------------------------------------------------------------------*352* usb_linux_resume353*354* This function is the FreeBSD resume callback. Usually it does nothing.355*------------------------------------------------------------------------*/356static int357usb_linux_resume(device_t dev)358{359struct usb_linux_softc *sc = device_get_softc(dev);360struct usb_driver *udrv = usb_linux_get_usb_driver(sc);361int err;362363err = 0;364if (udrv && udrv->resume)365err = (udrv->resume) (sc->sc_ui);366return (-err);367}368369/*------------------------------------------------------------------------*370* Linux emulation layer371*------------------------------------------------------------------------*/372373/*------------------------------------------------------------------------*374* usb_max_isoc_frames375*376* The following function returns the maximum number of isochronous377* frames that we support per URB. It is not part of the Linux USB API.378*------------------------------------------------------------------------*/379static uint16_t380usb_max_isoc_frames(struct usb_device *dev)381{382; /* indent fix */383switch (usbd_get_speed(dev)) {384case USB_SPEED_LOW:385case USB_SPEED_FULL:386return (USB_MAX_FULL_SPEED_ISOC_FRAMES);387default:388return (USB_MAX_HIGH_SPEED_ISOC_FRAMES);389}390}391392/*------------------------------------------------------------------------*393* usb_submit_urb394*395* This function is used to queue an URB after that it has been396* initialized. If it returns non-zero, it means that the URB was not397* queued.398*------------------------------------------------------------------------*/399int400usb_submit_urb(struct urb *urb, uint16_t mem_flags)401{402struct usb_host_endpoint *uhe;403uint8_t do_unlock;404int err;405406if (urb == NULL)407return (-EINVAL);408409do_unlock = mtx_owned(&Giant) ? 0 : 1;410if (do_unlock)411mtx_lock(&Giant);412413if (urb->endpoint == NULL) {414err = -EINVAL;415goto done;416}417418/*419* Check to see if the urb is in the process of being killed420* and stop a urb that is in the process of being killed from421* being re-submitted (e.g. from its completion callback422* function).423*/424if (urb->kill_count != 0) {425err = -EPERM;426goto done;427}428429uhe = urb->endpoint;430431/*432* Check that we have got a FreeBSD USB transfer that will dequeue433* the URB structure and do the real transfer. If there are no USB434* transfers, then we return an error.435*/436if (uhe->bsd_xfer[0] ||437uhe->bsd_xfer[1]) {438/* we are ready! */439440TAILQ_INSERT_TAIL(&uhe->bsd_urb_list, urb, bsd_urb_list);441442urb->status = -EINPROGRESS;443444usbd_transfer_start(uhe->bsd_xfer[0]);445usbd_transfer_start(uhe->bsd_xfer[1]);446err = 0;447} else {448/* no pipes have been setup yet! */449urb->status = -EINVAL;450err = -EINVAL;451}452done:453if (do_unlock)454mtx_unlock(&Giant);455return (err);456}457458/*------------------------------------------------------------------------*459* usb_unlink_urb460*461* This function is used to stop an URB after that it is been462* submitted, but before the "complete" callback has been called. On463*------------------------------------------------------------------------*/464int465usb_unlink_urb(struct urb *urb)466{467return (usb_unlink_urb_sub(urb, 0));468}469470static void471usb_unlink_bsd(struct usb_xfer *xfer,472struct urb *urb, uint8_t drain)473{474if (xfer == NULL)475return;476if (!usbd_transfer_pending(xfer))477return;478if (xfer->priv_fifo == (void *)urb) {479if (drain) {480mtx_unlock(&Giant);481usbd_transfer_drain(xfer);482mtx_lock(&Giant);483} else {484usbd_transfer_stop(xfer);485}486usbd_transfer_start(xfer);487}488}489490static int491usb_unlink_urb_sub(struct urb *urb, uint8_t drain)492{493struct usb_host_endpoint *uhe;494uint16_t x;495uint8_t do_unlock;496int err;497498if (urb == NULL)499return (-EINVAL);500501do_unlock = mtx_owned(&Giant) ? 0 : 1;502if (do_unlock)503mtx_lock(&Giant);504if (drain)505urb->kill_count++;506507if (urb->endpoint == NULL) {508err = -EINVAL;509goto done;510}511uhe = urb->endpoint;512513if (urb->bsd_urb_list.tqe_prev) {514/* not started yet, just remove it from the queue */515TAILQ_REMOVE(&uhe->bsd_urb_list, urb, bsd_urb_list);516urb->bsd_urb_list.tqe_prev = NULL;517urb->status = -ECONNRESET;518urb->actual_length = 0;519520for (x = 0; x < urb->number_of_packets; x++) {521urb->iso_frame_desc[x].actual_length = 0;522}523524if (urb->complete) {525(urb->complete) (urb);526}527} else {528/*529* If the URB is not on the URB list, then check if one of530* the FreeBSD USB transfer are processing the current URB.531* If so, re-start that transfer, which will lead to the532* termination of that URB:533*/534usb_unlink_bsd(uhe->bsd_xfer[0], urb, drain);535usb_unlink_bsd(uhe->bsd_xfer[1], urb, drain);536}537err = 0;538done:539if (drain)540urb->kill_count--;541if (do_unlock)542mtx_unlock(&Giant);543return (err);544}545546/*------------------------------------------------------------------------*547* usb_clear_halt548*549* This function must always be used to clear the stall. Stall is when550* an USB endpoint returns a stall message to the USB host controller.551* Until the stall is cleared, no data can be transferred.552*------------------------------------------------------------------------*/553int554usb_clear_halt(struct usb_device *dev, struct usb_host_endpoint *uhe)555{556struct usb_config cfg[1];557struct usb_endpoint *ep;558uint8_t type;559uint8_t addr;560561if (uhe == NULL)562return (-EINVAL);563564type = uhe->desc.bmAttributes & UE_XFERTYPE;565addr = uhe->desc.bEndpointAddress;566567memset(cfg, 0, sizeof(cfg));568569cfg[0].type = type;570cfg[0].endpoint = addr & UE_ADDR;571cfg[0].direction = addr & (UE_DIR_OUT | UE_DIR_IN);572573ep = usbd_get_endpoint(dev, uhe->bsd_iface_index, cfg);574if (ep == NULL)575return (-EINVAL);576577usbd_clear_data_toggle(dev, ep);578579return (usb_control_msg(dev, &dev->ep0,580UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT,581UF_ENDPOINT_HALT, addr, NULL, 0, 1000));582}583584/*------------------------------------------------------------------------*585* usb_start_wait_urb586*587* This is an internal function that is used to perform synchronous588* Linux USB transfers.589*------------------------------------------------------------------------*/590static int591usb_start_wait_urb(struct urb *urb, usb_timeout_t timeout, uint16_t *p_actlen)592{593int err;594uint8_t do_unlock;595596/* you must have a timeout! */597if (timeout == 0) {598timeout = 1;599}600urb->complete = &usb_linux_wait_complete;601urb->timeout = timeout;602urb->transfer_flags |= URB_WAIT_WAKEUP;603urb->transfer_flags &= ~URB_IS_SLEEPING;604605do_unlock = mtx_owned(&Giant) ? 0 : 1;606if (do_unlock)607mtx_lock(&Giant);608err = usb_submit_urb(urb, 0);609if (err)610goto done;611612/*613* the URB might have completed before we get here, so check that by614* using some flags!615*/616while (urb->transfer_flags & URB_WAIT_WAKEUP) {617urb->transfer_flags |= URB_IS_SLEEPING;618cv_wait(&urb->cv_wait, &Giant);619urb->transfer_flags &= ~URB_IS_SLEEPING;620}621622err = urb->status;623624done:625if (do_unlock)626mtx_unlock(&Giant);627if (p_actlen != NULL) {628if (err)629*p_actlen = 0;630else631*p_actlen = urb->actual_length;632}633return (err);634}635636/*------------------------------------------------------------------------*637* usb_control_msg638*639* The following function performs a control transfer sequence one any640* control, bulk or interrupt endpoint, specified by "uhe". A control641* transfer means that you transfer an 8-byte header first followed by642* a data-phase as indicated by the 8-byte header. The "timeout" is643* given in milliseconds.644*645* Return values:646* 0: Success647* < 0: Failure648* > 0: Actual length649*------------------------------------------------------------------------*/650int651usb_control_msg(struct usb_device *dev, struct usb_host_endpoint *uhe,652uint8_t request, uint8_t requesttype,653uint16_t value, uint16_t index, void *data,654uint16_t size, usb_timeout_t timeout)655{656struct usb_device_request req;657struct urb *urb;658int err;659uint16_t actlen;660uint8_t type;661uint8_t addr;662663req.bmRequestType = requesttype;664req.bRequest = request;665USETW(req.wValue, value);666USETW(req.wIndex, index);667USETW(req.wLength, size);668669if (uhe == NULL) {670return (-EINVAL);671}672type = (uhe->desc.bmAttributes & UE_XFERTYPE);673addr = (uhe->desc.bEndpointAddress & UE_ADDR);674675if (type != UE_CONTROL) {676return (-EINVAL);677}678if (addr == 0) {679/*680* The FreeBSD USB stack supports standard control681* transfers on control endpoint zero:682*/683err = usbd_do_request_flags(dev,684NULL, &req, data, USB_SHORT_XFER_OK,685&actlen, timeout);686if (err) {687err = -EPIPE;688} else {689err = actlen;690}691return (err);692}693if (dev->flags.usb_mode != USB_MODE_HOST) {694/* not supported */695return (-EINVAL);696}697err = usb_setup_endpoint(dev, uhe, 1 /* dummy */ );698699/*700* NOTE: we need to allocate real memory here so that we don't701* transfer data to/from the stack!702*703* 0xFFFF is a FreeBSD specific magic value.704*/705urb = usb_alloc_urb(0xFFFF, size);706707urb->dev = dev;708urb->endpoint = uhe;709710memcpy(urb->setup_packet, &req, sizeof(req));711712if (size && (!(req.bmRequestType & UT_READ))) {713/* move the data to a real buffer */714memcpy(USB_ADD_BYTES(urb->setup_packet, sizeof(req)),715data, size);716}717err = usb_start_wait_urb(urb, timeout, &actlen);718719if (req.bmRequestType & UT_READ) {720if (actlen) {721bcopy(USB_ADD_BYTES(urb->setup_packet,722sizeof(req)), data, actlen);723}724}725usb_free_urb(urb);726727if (err == 0) {728err = actlen;729}730return (err);731}732733/*------------------------------------------------------------------------*734* usb_set_interface735*736* The following function will select which alternate setting of an737* USB interface you plan to use. By default alternate setting with738* index zero is selected. Note that "iface_no" is not the interface739* index, but rather the value of "bInterfaceNumber".740*------------------------------------------------------------------------*/741int742usb_set_interface(struct usb_device *dev, uint8_t iface_no, uint8_t alt_index)743{744struct usb_interface *p_ui = usb_ifnum_to_if(dev, iface_no);745int err;746747if (p_ui == NULL)748return (-EINVAL);749if (alt_index >= p_ui->num_altsetting)750return (-EINVAL);751usb_linux_cleanup_interface(dev, p_ui);752err = -usbd_set_alt_interface_index(dev,753p_ui->bsd_iface_index, alt_index);754if (err == 0) {755p_ui->cur_altsetting = p_ui->altsetting + alt_index;756}757return (err);758}759760/*------------------------------------------------------------------------*761* usb_setup_endpoint762*763* The following function is an extension to the Linux USB API that764* allows you to set a maximum buffer size for a given USB endpoint.765* The maximum buffer size is per URB. If you don't call this function766* to set a maximum buffer size, the endpoint will not be functional.767* Note that for isochronous endpoints the maximum buffer size must be768* a non-zero dummy, hence this function will base the maximum buffer769* size on "wMaxPacketSize".770*------------------------------------------------------------------------*/771int772usb_setup_endpoint(struct usb_device *dev,773struct usb_host_endpoint *uhe, usb_size_t bufsize)774{775struct usb_config cfg[2];776uint8_t type = uhe->desc.bmAttributes & UE_XFERTYPE;777uint8_t addr = uhe->desc.bEndpointAddress;778779if (uhe->fbsd_buf_size == bufsize) {780/* optimize */781return (0);782}783usbd_transfer_unsetup(uhe->bsd_xfer, 2);784785uhe->fbsd_buf_size = bufsize;786787if (bufsize == 0) {788return (0);789}790memset(cfg, 0, sizeof(cfg));791792if (type == UE_ISOCHRONOUS) {793/*794* Isochronous transfers are special in that they don't fit795* into the BULK/INTR/CONTROL transfer model.796*/797798cfg[0].type = type;799cfg[0].endpoint = addr & UE_ADDR;800cfg[0].direction = addr & (UE_DIR_OUT | UE_DIR_IN);801cfg[0].callback = &usb_linux_isoc_callback;802cfg[0].bufsize = 0; /* use wMaxPacketSize */803cfg[0].frames = usb_max_isoc_frames(dev);804cfg[0].flags.proxy_buffer = 1;805#if 0806/*807* The Linux USB API allows non back-to-back808* isochronous frames which we do not support. If the809* isochronous frames are not back-to-back we need to810* do a copy, and then we need a buffer for811* that. Enable this at your own risk.812*/813cfg[0].flags.ext_buffer = 1;814#endif815cfg[0].flags.short_xfer_ok = 1;816817bcopy(cfg, cfg + 1, sizeof(*cfg));818819/* Allocate and setup two generic FreeBSD USB transfers */820821if (usbd_transfer_setup(dev, &uhe->bsd_iface_index,822uhe->bsd_xfer, cfg, 2, uhe, &Giant)) {823return (-EINVAL);824}825} else {826if (bufsize > (1 << 22)) {827/* limit buffer size */828bufsize = (1 << 22);829}830/* Allocate and setup one generic FreeBSD USB transfer */831832cfg[0].type = type;833cfg[0].endpoint = addr & UE_ADDR;834cfg[0].direction = addr & (UE_DIR_OUT | UE_DIR_IN);835cfg[0].callback = &usb_linux_non_isoc_callback;836cfg[0].bufsize = bufsize;837cfg[0].flags.ext_buffer = 1; /* enable zero-copy */838cfg[0].flags.proxy_buffer = 1;839cfg[0].flags.short_xfer_ok = 1;840841if (usbd_transfer_setup(dev, &uhe->bsd_iface_index,842uhe->bsd_xfer, cfg, 1, uhe, &Giant)) {843return (-EINVAL);844}845}846return (0);847}848849/*------------------------------------------------------------------------*850* usb_linux_create_usb_device851*852* The following function is used to build up a per USB device853* structure tree, that mimics the Linux one. The root structure854* is returned by this function.855*------------------------------------------------------------------------*/856static int857usb_linux_create_usb_device(struct usb_device *udev, device_t dev)858{859struct usb_config_descriptor *cd = usbd_get_config_descriptor(udev);860struct usb_descriptor *desc;861struct usb_interface_descriptor *id;862struct usb_endpoint_descriptor *ed;863struct usb_interface *p_ui = NULL;864struct usb_host_interface *p_uhi = NULL;865struct usb_host_endpoint *p_uhe = NULL;866usb_size_t size;867uint16_t niface_total;868uint16_t nedesc;869uint16_t iface_no_curr;870uint16_t iface_index;871uint8_t pass;872uint8_t iface_no;873874/*875* We do two passes. One pass for computing necessary memory size876* and one pass to initialize all the allocated memory structures.877*/878for (pass = 0; pass < 2; pass++) {879iface_no_curr = 0xFFFF;880niface_total = 0;881iface_index = 0;882nedesc = 0;883desc = NULL;884885/*886* Iterate over all the USB descriptors. Use the USB config887* descriptor pointer provided by the FreeBSD USB stack.888*/889while ((desc = usb_desc_foreach(cd, desc))) {890/*891* Build up a tree according to the descriptors we892* find:893*/894switch (desc->bDescriptorType) {895case UDESC_DEVICE:896break;897898case UDESC_ENDPOINT:899ed = (void *)desc;900if ((ed->bLength < sizeof(*ed)) ||901(iface_index == 0))902break;903if (p_uhe) {904bcopy(ed, &p_uhe->desc, sizeof(p_uhe->desc));905p_uhe->bsd_iface_index = iface_index - 1;906TAILQ_INIT(&p_uhe->bsd_urb_list);907p_uhe++;908}909if (p_uhi) {910(p_uhi - 1)->desc.bNumEndpoints++;911}912nedesc++;913break;914915case UDESC_INTERFACE:916id = (void *)desc;917if (id->bLength < sizeof(*id))918break;919if (p_uhi) {920bcopy(id, &p_uhi->desc, sizeof(p_uhi->desc));921p_uhi->desc.bNumEndpoints = 0;922p_uhi->endpoint = p_uhe;923p_uhi->string = "";924p_uhi->bsd_iface_index = iface_index;925p_uhi++;926}927iface_no = id->bInterfaceNumber;928niface_total++;929if (iface_no_curr != iface_no) {930if (p_ui) {931p_ui->altsetting = p_uhi - 1;932p_ui->cur_altsetting = p_uhi - 1;933p_ui->bsd_iface_index = iface_index;934p_ui->linux_udev = udev;935p_ui++;936}937iface_no_curr = iface_no;938iface_index++;939}940break;941942default:943break;944}945}946947if (pass == 0) {948size = (sizeof(*p_uhe) * nedesc) +949(sizeof(*p_ui) * iface_index) +950(sizeof(*p_uhi) * niface_total);951952p_uhe = malloc(size, M_USBDEV, M_WAITOK | M_ZERO);953p_ui = (void *)(p_uhe + nedesc);954p_uhi = (void *)(p_ui + iface_index);955956udev->linux_iface_start = p_ui;957udev->linux_iface_end = p_ui + iface_index;958udev->linux_endpoint_start = p_uhe;959udev->linux_endpoint_end = p_uhe + nedesc;960udev->devnum = device_get_unit(dev);961bcopy(&udev->ddesc, &udev->descriptor,962sizeof(udev->descriptor));963bcopy(udev->ctrl_ep.edesc, &udev->ep0.desc,964sizeof(udev->ep0.desc));965}966}967return (0);968}969970/*------------------------------------------------------------------------*971* usb_alloc_urb972*973* This function should always be used when you allocate an URB for974* use with the USB Linux stack. In case of an isochronous transfer975* you must specifiy the maximum number of "iso_packets" which you976* plan to transfer per URB. This function is always blocking, and977* "mem_flags" are not regarded like on Linux.978*------------------------------------------------------------------------*/979struct urb *980usb_alloc_urb(uint16_t iso_packets, uint16_t mem_flags)981{982struct urb *urb;983usb_size_t size;984985if (iso_packets == 0xFFFF) {986/*987* FreeBSD specific magic value to ask for control transfer988* memory allocation:989*/990size = sizeof(*urb) + sizeof(struct usb_device_request) + mem_flags;991} else {992size = sizeof(*urb) + (iso_packets * sizeof(urb->iso_frame_desc[0]));993}994995urb = malloc(size, M_USBDEV, M_WAITOK | M_ZERO);996997cv_init(&urb->cv_wait, "URBWAIT");998if (iso_packets == 0xFFFF) {999urb->setup_packet = (void *)(urb + 1);1000urb->transfer_buffer = (void *)(urb->setup_packet +1001sizeof(struct usb_device_request));1002} else {1003urb->number_of_packets = iso_packets;1004}1005return (urb);1006}10071008/*------------------------------------------------------------------------*1009* usb_find_host_endpoint1010*1011* The following function will return the Linux USB host endpoint1012* structure that matches the given endpoint type and endpoint1013* value. If no match is found, NULL is returned. This function is not1014* part of the Linux USB API and is only used internally.1015*------------------------------------------------------------------------*/1016struct usb_host_endpoint *1017usb_find_host_endpoint(struct usb_device *dev, uint8_t type, uint8_t ep)1018{1019struct usb_host_endpoint *uhe;1020struct usb_host_endpoint *uhe_end;1021struct usb_host_interface *uhi;1022struct usb_interface *ui;1023uint8_t ea;1024uint8_t at;1025uint8_t mask;10261027if (dev == NULL) {1028return (NULL);1029}1030if (type == UE_CONTROL) {1031mask = UE_ADDR;1032} else {1033mask = (UE_DIR_IN | UE_DIR_OUT | UE_ADDR);1034}10351036ep &= mask;10371038/*1039* Iterate over all the interfaces searching the selected alternate1040* setting only, and all belonging endpoints.1041*/1042for (ui = dev->linux_iface_start;1043ui != dev->linux_iface_end;1044ui++) {1045uhi = ui->cur_altsetting;1046if (uhi) {1047uhe_end = uhi->endpoint + uhi->desc.bNumEndpoints;1048for (uhe = uhi->endpoint;1049uhe != uhe_end;1050uhe++) {1051ea = uhe->desc.bEndpointAddress;1052at = uhe->desc.bmAttributes;10531054if (((ea & mask) == ep) &&1055((at & UE_XFERTYPE) == type)) {1056return (uhe);1057}1058}1059}1060}10611062if ((type == UE_CONTROL) && ((ep & UE_ADDR) == 0)) {1063return (&dev->ep0);1064}1065return (NULL);1066}10671068/*------------------------------------------------------------------------*1069* usb_altnum_to_altsetting1070*1071* The following function returns a pointer to an alternate setting by1072* index given a "usb_interface" pointer. If the alternate setting by1073* index does not exist, NULL is returned. And alternate setting is a1074* variant of an interface, but usually with slightly different1075* characteristics.1076*------------------------------------------------------------------------*/1077struct usb_host_interface *1078usb_altnum_to_altsetting(const struct usb_interface *intf, uint8_t alt_index)1079{1080if (alt_index >= intf->num_altsetting) {1081return (NULL);1082}1083return (intf->altsetting + alt_index);1084}10851086/*------------------------------------------------------------------------*1087* usb_ifnum_to_if1088*1089* The following function searches up an USB interface by1090* "bInterfaceNumber". If no match is found, NULL is returned.1091*------------------------------------------------------------------------*/1092struct usb_interface *1093usb_ifnum_to_if(struct usb_device *dev, uint8_t iface_no)1094{1095struct usb_interface *p_ui;10961097for (p_ui = dev->linux_iface_start;1098p_ui != dev->linux_iface_end;1099p_ui++) {1100if ((p_ui->num_altsetting > 0) &&1101(p_ui->altsetting->desc.bInterfaceNumber == iface_no)) {1102return (p_ui);1103}1104}1105return (NULL);1106}11071108/*------------------------------------------------------------------------*1109* usb_buffer_alloc1110*------------------------------------------------------------------------*/1111void *1112usb_buffer_alloc(struct usb_device *dev, usb_size_t size, uint16_t mem_flags, uint8_t *dma_addr)1113{1114return (malloc(size, M_USBDEV, M_WAITOK | M_ZERO));1115}11161117/*------------------------------------------------------------------------*1118* usbd_get_intfdata1119*------------------------------------------------------------------------*/1120void *1121usbd_get_intfdata(struct usb_interface *intf)1122{1123return (intf->bsd_priv_sc);1124}11251126/*------------------------------------------------------------------------*1127* usb_linux_register1128*1129* The following function is used by the "USB_DRIVER_EXPORT()" macro,1130* and is used to register a Linux USB driver, so that its1131* "usb_device_id" structures gets searched a probe time. This1132* function is not part of the Linux USB API, and is for internal use1133* only.1134*------------------------------------------------------------------------*/1135void1136usb_linux_register(void *arg)1137{1138struct usb_driver *drv = arg;11391140mtx_lock(&Giant);1141LIST_INSERT_HEAD(&usb_linux_driver_list, drv, linux_driver_list);1142mtx_unlock(&Giant);11431144usb_needs_explore_all();1145}11461147/*------------------------------------------------------------------------*1148* usb_linux_deregister1149*1150* The following function is used by the "USB_DRIVER_EXPORT()" macro,1151* and is used to deregister a Linux USB driver. This function will1152* ensure that all driver instances belonging to the Linux USB device1153* driver in question, gets detached before the driver is1154* unloaded. This function is not part of the Linux USB API, and is1155* for internal use only.1156*------------------------------------------------------------------------*/1157void1158usb_linux_deregister(void *arg)1159{1160struct usb_driver *drv = arg;1161struct usb_linux_softc *sc;11621163repeat:1164mtx_lock(&Giant);1165LIST_FOREACH(sc, &usb_linux_attached_list, sc_attached_list) {1166if (sc->sc_udrv == drv) {1167mtx_unlock(&Giant);1168bus_topo_lock();1169device_detach(sc->sc_fbsd_dev);1170bus_topo_unlock();1171goto repeat;1172}1173}1174LIST_REMOVE(drv, linux_driver_list);1175mtx_unlock(&Giant);1176}11771178/*------------------------------------------------------------------------*1179* usb_linux_free_device1180*1181* The following function is only used by the FreeBSD USB stack, to1182* cleanup and free memory after that a Linux USB device was attached.1183*------------------------------------------------------------------------*/1184void1185usb_linux_free_device(struct usb_device *dev)1186{1187struct usb_host_endpoint *uhe;1188struct usb_host_endpoint *uhe_end;11891190uhe = dev->linux_endpoint_start;1191uhe_end = dev->linux_endpoint_end;1192while (uhe != uhe_end) {1193usb_setup_endpoint(dev, uhe, 0);1194uhe++;1195}1196usb_setup_endpoint(dev, &dev->ep0, 0);1197free(dev->linux_endpoint_start, M_USBDEV);1198}11991200/*------------------------------------------------------------------------*1201* usb_buffer_free1202*------------------------------------------------------------------------*/1203void1204usb_buffer_free(struct usb_device *dev, usb_size_t size,1205void *addr, uint8_t dma_addr)1206{1207free(addr, M_USBDEV);1208}12091210/*------------------------------------------------------------------------*1211* usb_free_urb1212*------------------------------------------------------------------------*/1213void1214usb_free_urb(struct urb *urb)1215{1216if (urb == NULL) {1217return;1218}1219/* make sure that the current URB is not active */1220usb_kill_urb(urb);12211222/* destroy condition variable */1223cv_destroy(&urb->cv_wait);12241225/* just free it */1226free(urb, M_USBDEV);1227}12281229/*------------------------------------------------------------------------*1230* usb_init_urb1231*1232* The following function can be used to initialize a custom URB. It1233* is not recommended to use this function. Use "usb_alloc_urb()"1234* instead.1235*------------------------------------------------------------------------*/1236void1237usb_init_urb(struct urb *urb)1238{1239if (urb == NULL) {1240return;1241}1242memset(urb, 0, sizeof(*urb));1243}12441245/*------------------------------------------------------------------------*1246* usb_kill_urb1247*------------------------------------------------------------------------*/1248void1249usb_kill_urb(struct urb *urb)1250{1251usb_unlink_urb_sub(urb, 1);1252}12531254/*------------------------------------------------------------------------*1255* usb_set_intfdata1256*1257* The following function sets the per Linux USB interface private1258* data pointer. It is used by most Linux USB device drivers.1259*------------------------------------------------------------------------*/1260void1261usb_set_intfdata(struct usb_interface *intf, void *data)1262{1263intf->bsd_priv_sc = data;1264}12651266/*------------------------------------------------------------------------*1267* usb_linux_cleanup_interface1268*1269* The following function will release all FreeBSD USB transfers1270* associated with a Linux USB interface. It is for internal use only.1271*------------------------------------------------------------------------*/1272static void1273usb_linux_cleanup_interface(struct usb_device *dev, struct usb_interface *iface)1274{1275struct usb_host_interface *uhi;1276struct usb_host_interface *uhi_end;1277struct usb_host_endpoint *uhe;1278struct usb_host_endpoint *uhe_end;12791280uhi = iface->altsetting;1281uhi_end = iface->altsetting + iface->num_altsetting;1282while (uhi != uhi_end) {1283uhe = uhi->endpoint;1284uhe_end = uhi->endpoint + uhi->desc.bNumEndpoints;1285while (uhe != uhe_end) {1286usb_setup_endpoint(dev, uhe, 0);1287uhe++;1288}1289uhi++;1290}1291}12921293/*------------------------------------------------------------------------*1294* usb_linux_wait_complete1295*1296* The following function is used by "usb_start_wait_urb()" to wake it1297* up, when an USB transfer has finished.1298*------------------------------------------------------------------------*/1299static void1300usb_linux_wait_complete(struct urb *urb)1301{1302if (urb->transfer_flags & URB_IS_SLEEPING) {1303cv_signal(&urb->cv_wait);1304}1305urb->transfer_flags &= ~URB_WAIT_WAKEUP;1306}13071308/*------------------------------------------------------------------------*1309* usb_linux_complete1310*------------------------------------------------------------------------*/1311static void1312usb_linux_complete(struct usb_xfer *xfer)1313{1314struct urb *urb;13151316urb = usbd_xfer_get_priv(xfer);1317usbd_xfer_set_priv(xfer, NULL);1318if (urb->complete) {1319(urb->complete) (urb);1320}1321}13221323/*------------------------------------------------------------------------*1324* usb_linux_isoc_callback1325*1326* The following is the FreeBSD isochronous USB callback. Isochronous1327* frames are USB packets transferred 1000 or 8000 times per second,1328* depending on whether a full- or high- speed USB transfer is1329* used.1330*------------------------------------------------------------------------*/1331static void1332usb_linux_isoc_callback(struct usb_xfer *xfer, usb_error_t error)1333{1334usb_frlength_t max_frame = xfer->max_frame_size;1335usb_frlength_t offset;1336usb_frcount_t x;1337struct urb *urb = usbd_xfer_get_priv(xfer);1338struct usb_host_endpoint *uhe = usbd_xfer_softc(xfer);1339struct usb_iso_packet_descriptor *uipd;13401341DPRINTF("\n");13421343switch (USB_GET_STATE(xfer)) {1344case USB_ST_TRANSFERRED:13451346if (urb->bsd_isread) {1347/* copy in data with regard to the URB */13481349offset = 0;13501351for (x = 0; x < urb->number_of_packets; x++) {1352uipd = urb->iso_frame_desc + x;1353if (uipd->length > xfer->frlengths[x]) {1354if (urb->transfer_flags & URB_SHORT_NOT_OK) {1355/* XXX should be EREMOTEIO */1356uipd->status = -EPIPE;1357} else {1358uipd->status = 0;1359}1360} else {1361uipd->status = 0;1362}1363uipd->actual_length = xfer->frlengths[x];1364if (!xfer->flags.ext_buffer) {1365usbd_copy_out(xfer->frbuffers, offset,1366USB_ADD_BYTES(urb->transfer_buffer,1367uipd->offset), uipd->actual_length);1368}1369offset += max_frame;1370}1371} else {1372for (x = 0; x < urb->number_of_packets; x++) {1373uipd = urb->iso_frame_desc + x;1374uipd->actual_length = xfer->frlengths[x];1375uipd->status = 0;1376}1377}13781379urb->actual_length = xfer->actlen;13801381/* check for short transfer */1382if (xfer->actlen < xfer->sumlen) {1383/* short transfer */1384if (urb->transfer_flags & URB_SHORT_NOT_OK) {1385/* XXX should be EREMOTEIO */1386urb->status = -EPIPE;1387} else {1388urb->status = 0;1389}1390} else {1391/* success */1392urb->status = 0;1393}13941395/* call callback */1396usb_linux_complete(xfer);13971398case USB_ST_SETUP:1399tr_setup:14001401if (xfer->priv_fifo == NULL) {1402/* get next transfer */1403urb = TAILQ_FIRST(&uhe->bsd_urb_list);1404if (urb == NULL) {1405/* nothing to do */1406return;1407}1408TAILQ_REMOVE(&uhe->bsd_urb_list, urb, bsd_urb_list);1409urb->bsd_urb_list.tqe_prev = NULL;14101411x = xfer->max_frame_count;1412if (urb->number_of_packets > x) {1413/* XXX simply truncate the transfer */1414urb->number_of_packets = x;1415}1416} else {1417DPRINTF("Already got a transfer\n");14181419/* already got a transfer (should not happen) */1420urb = usbd_xfer_get_priv(xfer);1421}14221423urb->bsd_isread = (uhe->desc.bEndpointAddress & UE_DIR_IN) ? 1 : 0;14241425if (xfer->flags.ext_buffer) {1426/* set virtual address to load */1427usbd_xfer_set_frame_data(xfer, 0, urb->transfer_buffer, 0);1428}1429if (!(urb->bsd_isread)) {1430/* copy out data with regard to the URB */14311432offset = 0;14331434for (x = 0; x < urb->number_of_packets; x++) {1435uipd = urb->iso_frame_desc + x;1436usbd_xfer_set_frame_len(xfer, x, uipd->length);1437if (!xfer->flags.ext_buffer) {1438usbd_copy_in(xfer->frbuffers, offset,1439USB_ADD_BYTES(urb->transfer_buffer,1440uipd->offset), uipd->length);1441}1442offset += uipd->length;1443}1444} else {1445/*1446* compute the transfer length into the "offset"1447* variable1448*/14491450offset = urb->number_of_packets * max_frame;14511452/* setup "frlengths" array */14531454for (x = 0; x < urb->number_of_packets; x++) {1455uipd = urb->iso_frame_desc + x;1456usbd_xfer_set_frame_len(xfer, x, max_frame);1457}1458}1459usbd_xfer_set_priv(xfer, urb);1460xfer->flags.force_short_xfer = 0;1461xfer->timeout = urb->timeout;1462xfer->nframes = urb->number_of_packets;1463usbd_transfer_submit(xfer);1464return;14651466default: /* Error */1467if (xfer->error == USB_ERR_CANCELLED) {1468urb->status = -ECONNRESET;1469} else {1470urb->status = -EPIPE; /* stalled */1471}14721473/* Set zero for "actual_length" */1474urb->actual_length = 0;14751476/* Set zero for "actual_length" */1477for (x = 0; x < urb->number_of_packets; x++) {1478urb->iso_frame_desc[x].actual_length = 0;1479urb->iso_frame_desc[x].status = urb->status;1480}14811482/* call callback */1483usb_linux_complete(xfer);14841485if (xfer->error == USB_ERR_CANCELLED) {1486/* we need to return in this case */1487return;1488}1489goto tr_setup;1490}1491}14921493/*------------------------------------------------------------------------*1494* usb_linux_non_isoc_callback1495*1496* The following is the FreeBSD BULK/INTERRUPT and CONTROL USB1497* callback. It dequeues Linux USB stack compatible URB's, transforms1498* the URB fields into a FreeBSD USB transfer, and defragments the USB1499* transfer as required. When the transfer is complete the "complete"1500* callback is called.1501*------------------------------------------------------------------------*/1502static void1503usb_linux_non_isoc_callback(struct usb_xfer *xfer, usb_error_t error)1504{1505enum {1506REQ_SIZE = sizeof(struct usb_device_request)1507};1508struct urb *urb = usbd_xfer_get_priv(xfer);1509struct usb_host_endpoint *uhe = usbd_xfer_softc(xfer);1510uint8_t *ptr;1511usb_frlength_t max_bulk = usbd_xfer_max_len(xfer);1512uint8_t data_frame = xfer->flags_int.control_xfr ? 1 : 0;15131514DPRINTF("\n");15151516switch (USB_GET_STATE(xfer)) {1517case USB_ST_TRANSFERRED:15181519if (xfer->flags_int.control_xfr) {1520/* don't transfer the setup packet again: */15211522usbd_xfer_set_frame_len(xfer, 0, 0);1523}1524if (urb->bsd_isread && (!xfer->flags.ext_buffer)) {1525/* copy in data with regard to the URB */1526usbd_copy_out(xfer->frbuffers + data_frame, 0,1527urb->bsd_data_ptr, xfer->frlengths[data_frame]);1528}1529urb->bsd_length_rem -= xfer->frlengths[data_frame];1530urb->bsd_data_ptr += xfer->frlengths[data_frame];1531urb->actual_length += xfer->frlengths[data_frame];15321533/* check for short transfer */1534if (xfer->actlen < xfer->sumlen) {1535urb->bsd_length_rem = 0;15361537/* short transfer */1538if (urb->transfer_flags & URB_SHORT_NOT_OK) {1539urb->status = -EPIPE;1540} else {1541urb->status = 0;1542}1543} else {1544/* check remainder */1545if (urb->bsd_length_rem > 0) {1546goto setup_bulk;1547}1548/* success */1549urb->status = 0;1550}15511552/* call callback */1553usb_linux_complete(xfer);15541555case USB_ST_SETUP:1556tr_setup:1557/* get next transfer */1558urb = TAILQ_FIRST(&uhe->bsd_urb_list);1559if (urb == NULL) {1560/* nothing to do */1561return;1562}1563TAILQ_REMOVE(&uhe->bsd_urb_list, urb, bsd_urb_list);1564urb->bsd_urb_list.tqe_prev = NULL;15651566usbd_xfer_set_priv(xfer, urb);1567xfer->flags.force_short_xfer = 0;1568xfer->timeout = urb->timeout;15691570if (xfer->flags_int.control_xfr) {1571/*1572* USB control transfers need special handling.1573* First copy in the header, then copy in data!1574*/1575if (!xfer->flags.ext_buffer) {1576usbd_copy_in(xfer->frbuffers, 0,1577urb->setup_packet, REQ_SIZE);1578usbd_xfer_set_frame_len(xfer, 0, REQ_SIZE);1579} else {1580/* set virtual address to load */1581usbd_xfer_set_frame_data(xfer, 0,1582urb->setup_packet, REQ_SIZE);1583}15841585ptr = urb->setup_packet;15861587/* setup data transfer direction and length */1588urb->bsd_isread = (ptr[0] & UT_READ) ? 1 : 0;1589urb->bsd_length_rem = ptr[6] | (ptr[7] << 8);15901591} else {1592/* setup data transfer direction */15931594urb->bsd_length_rem = urb->transfer_buffer_length;1595urb->bsd_isread = (uhe->desc.bEndpointAddress &1596UE_DIR_IN) ? 1 : 0;1597}15981599urb->bsd_data_ptr = urb->transfer_buffer;1600urb->actual_length = 0;16011602setup_bulk:1603if (max_bulk > urb->bsd_length_rem) {1604max_bulk = urb->bsd_length_rem;1605}1606/* check if we need to force a short transfer */16071608if ((max_bulk == urb->bsd_length_rem) &&1609(urb->transfer_flags & URB_ZERO_PACKET) &&1610(!xfer->flags_int.control_xfr)) {1611xfer->flags.force_short_xfer = 1;1612}1613/* check if we need to copy in data */16141615if (xfer->flags.ext_buffer) {1616/* set virtual address to load */1617usbd_xfer_set_frame_data(xfer, data_frame,1618urb->bsd_data_ptr, max_bulk);1619} else if (!urb->bsd_isread) {1620/* copy out data with regard to the URB */1621usbd_copy_in(xfer->frbuffers + data_frame, 0,1622urb->bsd_data_ptr, max_bulk);1623usbd_xfer_set_frame_len(xfer, data_frame, max_bulk);1624}1625if (xfer->flags_int.control_xfr) {1626if (max_bulk > 0) {1627xfer->nframes = 2;1628} else {1629xfer->nframes = 1;1630}1631} else {1632xfer->nframes = 1;1633}1634usbd_transfer_submit(xfer);1635return;16361637default:1638if (xfer->error == USB_ERR_CANCELLED) {1639urb->status = -ECONNRESET;1640} else {1641urb->status = -EPIPE;1642}16431644/* Set zero for "actual_length" */1645urb->actual_length = 0;16461647/* call callback */1648usb_linux_complete(xfer);16491650if (xfer->error == USB_ERR_CANCELLED) {1651/* we need to return in this case */1652return;1653}1654goto tr_setup;1655}1656}16571658/*------------------------------------------------------------------------*1659* usb_fill_bulk_urb1660*------------------------------------------------------------------------*/1661void1662usb_fill_bulk_urb(struct urb *urb, struct usb_device *udev,1663struct usb_host_endpoint *uhe, void *buf,1664int length, usb_complete_t callback, void *arg)1665{1666urb->dev = udev;1667urb->endpoint = uhe;1668urb->transfer_buffer = buf;1669urb->transfer_buffer_length = length;1670urb->complete = callback;1671urb->context = arg;1672}16731674/*------------------------------------------------------------------------*1675* usb_bulk_msg1676*1677* NOTE: This function can also be used for interrupt endpoints!1678*1679* Return values:1680* 0: Success1681* Else: Failure1682*------------------------------------------------------------------------*/1683int1684usb_bulk_msg(struct usb_device *udev, struct usb_host_endpoint *uhe,1685void *data, int len, uint16_t *pactlen, usb_timeout_t timeout)1686{1687struct urb *urb;1688int err;16891690if (uhe == NULL)1691return (-EINVAL);1692if (len < 0)1693return (-EINVAL);16941695err = usb_setup_endpoint(udev, uhe, 4096 /* bytes */);1696if (err)1697return (err);16981699urb = usb_alloc_urb(0, 0);17001701usb_fill_bulk_urb(urb, udev, uhe, data, len,1702usb_linux_wait_complete, NULL);17031704err = usb_start_wait_urb(urb, timeout, pactlen);17051706usb_free_urb(urb);17071708return (err);1709}1710MODULE_DEPEND(linuxkpi, usb, 1, 1, 1);17111712static void1713usb_linux_init(void *arg)1714{1715/* register our function */1716usb_linux_free_device_p = &usb_linux_free_device;1717}1718SYSINIT(usb_linux_init, SI_SUB_LOCK, SI_ORDER_FIRST, usb_linux_init, NULL);1719SYSUNINIT(usb_linux_unload, SI_SUB_LOCK, SI_ORDER_ANY, usb_linux_unload, NULL);172017211722