usb_request.c revision 213435
1184610Salfred/* $FreeBSD: head/sys/dev/usb/usb_request.c 213435 2010-10-04 23:18:05Z hselasky $ */ 2184610Salfred/*- 3184610Salfred * Copyright (c) 1998 The NetBSD Foundation, Inc. All rights reserved. 4184610Salfred * Copyright (c) 1998 Lennart Augustsson. All rights reserved. 5184610Salfred * Copyright (c) 2008 Hans Petter Selasky. All rights reserved. 6184610Salfred * 7184610Salfred * Redistribution and use in source and binary forms, with or without 8184610Salfred * modification, are permitted provided that the following conditions 9184610Salfred * are met: 10184610Salfred * 1. Redistributions of source code must retain the above copyright 11184610Salfred * notice, this list of conditions and the following disclaimer. 12184610Salfred * 2. Redistributions in binary form must reproduce the above copyright 13184610Salfred * notice, this list of conditions and the following disclaimer in the 14184610Salfred * documentation and/or other materials provided with the distribution. 15184610Salfred * 16184610Salfred * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17184610Salfred * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18184610Salfred * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19184610Salfred * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20184610Salfred * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21184610Salfred * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22184610Salfred * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23184610Salfred * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24184610Salfred * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25184610Salfred * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26184610Salfred * SUCH DAMAGE. 27190754Sthompsa */ 28184610Salfred 29194677Sthompsa#include <sys/stdint.h> 30194677Sthompsa#include <sys/stddef.h> 31194677Sthompsa#include <sys/param.h> 32194677Sthompsa#include <sys/queue.h> 33194677Sthompsa#include <sys/types.h> 34194677Sthompsa#include <sys/systm.h> 35194677Sthompsa#include <sys/kernel.h> 36194677Sthompsa#include <sys/bus.h> 37194677Sthompsa#include <sys/linker_set.h> 38194677Sthompsa#include <sys/module.h> 39194677Sthompsa#include <sys/lock.h> 40194677Sthompsa#include <sys/mutex.h> 41194677Sthompsa#include <sys/condvar.h> 42194677Sthompsa#include <sys/sysctl.h> 43194677Sthompsa#include <sys/sx.h> 44194677Sthompsa#include <sys/unistd.h> 45194677Sthompsa#include <sys/callout.h> 46194677Sthompsa#include <sys/malloc.h> 47194677Sthompsa#include <sys/priv.h> 48194677Sthompsa 49188942Sthompsa#include <dev/usb/usb.h> 50194677Sthompsa#include <dev/usb/usbdi.h> 51194677Sthompsa#include <dev/usb/usbdi_util.h> 52188942Sthompsa#include <dev/usb/usb_ioctl.h> 53188942Sthompsa#include <dev/usb/usbhid.h> 54184610Salfred 55194228Sthompsa#define USB_DEBUG_VAR usb_debug 56184610Salfred 57188942Sthompsa#include <dev/usb/usb_core.h> 58188942Sthompsa#include <dev/usb/usb_busdma.h> 59188942Sthompsa#include <dev/usb/usb_request.h> 60188942Sthompsa#include <dev/usb/usb_process.h> 61188942Sthompsa#include <dev/usb/usb_transfer.h> 62188942Sthompsa#include <dev/usb/usb_debug.h> 63188942Sthompsa#include <dev/usb/usb_device.h> 64188942Sthompsa#include <dev/usb/usb_util.h> 65188942Sthompsa#include <dev/usb/usb_dynamic.h> 66184610Salfred 67188942Sthompsa#include <dev/usb/usb_controller.h> 68188942Sthompsa#include <dev/usb/usb_bus.h> 69184610Salfred#include <sys/ctype.h> 70184610Salfred 71207077Sthompsa#ifdef USB_DEBUG 72194228Sthompsastatic int usb_pr_poll_delay = USB_PORT_RESET_DELAY; 73194228Sthompsastatic int usb_pr_recovery_delay = USB_PORT_RESET_RECOVERY; 74184610Salfred 75192502SthompsaSYSCTL_INT(_hw_usb, OID_AUTO, pr_poll_delay, CTLFLAG_RW, 76194228Sthompsa &usb_pr_poll_delay, 0, "USB port reset poll delay in ms"); 77192502SthompsaSYSCTL_INT(_hw_usb, OID_AUTO, pr_recovery_delay, CTLFLAG_RW, 78194228Sthompsa &usb_pr_recovery_delay, 0, "USB port reset recovery delay in ms"); 79184610Salfred 80208018Sthompsa#ifdef USB_REQ_DEBUG 81208018Sthompsa/* The following structures are used in connection to fault injection. */ 82208018Sthompsastruct usb_ctrl_debug { 83208018Sthompsa int bus_index; /* target bus */ 84208018Sthompsa int dev_index; /* target address */ 85208018Sthompsa int ds_fail; /* fail data stage */ 86208018Sthompsa int ss_fail; /* fail data stage */ 87208018Sthompsa int ds_delay; /* data stage delay in ms */ 88208018Sthompsa int ss_delay; /* status stage delay in ms */ 89208018Sthompsa int bmRequestType_value; 90208018Sthompsa int bRequest_value; 91208018Sthompsa}; 92208018Sthompsa 93208018Sthompsastruct usb_ctrl_debug_bits { 94208018Sthompsa uint16_t ds_delay; 95208018Sthompsa uint16_t ss_delay; 96208018Sthompsa uint8_t ds_fail:1; 97208018Sthompsa uint8_t ss_fail:1; 98208018Sthompsa uint8_t enabled:1; 99208018Sthompsa}; 100208018Sthompsa 101208018Sthompsa/* The default is to disable fault injection. */ 102208018Sthompsa 103208018Sthompsastatic struct usb_ctrl_debug usb_ctrl_debug = { 104208018Sthompsa .bus_index = -1, 105208018Sthompsa .dev_index = -1, 106208018Sthompsa .bmRequestType_value = -1, 107208018Sthompsa .bRequest_value = -1, 108208018Sthompsa}; 109208018Sthompsa 110208018SthompsaSYSCTL_INT(_hw_usb, OID_AUTO, ctrl_bus_fail, CTLFLAG_RW, 111208018Sthompsa &usb_ctrl_debug.bus_index, 0, "USB controller index to fail"); 112208018SthompsaSYSCTL_INT(_hw_usb, OID_AUTO, ctrl_dev_fail, CTLFLAG_RW, 113208018Sthompsa &usb_ctrl_debug.dev_index, 0, "USB device address to fail"); 114208018SthompsaSYSCTL_INT(_hw_usb, OID_AUTO, ctrl_ds_fail, CTLFLAG_RW, 115208018Sthompsa &usb_ctrl_debug.ds_fail, 0, "USB fail data stage"); 116208018SthompsaSYSCTL_INT(_hw_usb, OID_AUTO, ctrl_ss_fail, CTLFLAG_RW, 117208018Sthompsa &usb_ctrl_debug.ss_fail, 0, "USB fail status stage"); 118208018SthompsaSYSCTL_INT(_hw_usb, OID_AUTO, ctrl_ds_delay, CTLFLAG_RW, 119208018Sthompsa &usb_ctrl_debug.ds_delay, 0, "USB data stage delay in ms"); 120208018SthompsaSYSCTL_INT(_hw_usb, OID_AUTO, ctrl_ss_delay, CTLFLAG_RW, 121208018Sthompsa &usb_ctrl_debug.ss_delay, 0, "USB status stage delay in ms"); 122208018SthompsaSYSCTL_INT(_hw_usb, OID_AUTO, ctrl_rt_fail, CTLFLAG_RW, 123208018Sthompsa &usb_ctrl_debug.bmRequestType_value, 0, "USB bmRequestType to fail"); 124208018SthompsaSYSCTL_INT(_hw_usb, OID_AUTO, ctrl_rv_fail, CTLFLAG_RW, 125208018Sthompsa &usb_ctrl_debug.bRequest_value, 0, "USB bRequest to fail"); 126208018Sthompsa 127184610Salfred/*------------------------------------------------------------------------* 128208018Sthompsa * usbd_get_debug_bits 129208018Sthompsa * 130208018Sthompsa * This function is only useful in USB host mode. 131208018Sthompsa *------------------------------------------------------------------------*/ 132208018Sthompsastatic void 133208018Sthompsausbd_get_debug_bits(struct usb_device *udev, struct usb_device_request *req, 134208018Sthompsa struct usb_ctrl_debug_bits *dbg) 135208018Sthompsa{ 136208018Sthompsa int temp; 137208018Sthompsa 138208018Sthompsa memset(dbg, 0, sizeof(*dbg)); 139208018Sthompsa 140208018Sthompsa /* Compute data stage delay */ 141208018Sthompsa 142208018Sthompsa temp = usb_ctrl_debug.ds_delay; 143208018Sthompsa if (temp < 0) 144208018Sthompsa temp = 0; 145208018Sthompsa else if (temp > (16*1024)) 146208018Sthompsa temp = (16*1024); 147208018Sthompsa 148208018Sthompsa dbg->ds_delay = temp; 149208018Sthompsa 150208018Sthompsa /* Compute status stage delay */ 151208018Sthompsa 152208018Sthompsa temp = usb_ctrl_debug.ss_delay; 153208018Sthompsa if (temp < 0) 154208018Sthompsa temp = 0; 155208018Sthompsa else if (temp > (16*1024)) 156208018Sthompsa temp = (16*1024); 157208018Sthompsa 158208018Sthompsa dbg->ss_delay = temp; 159208018Sthompsa 160208018Sthompsa /* Check if this control request should be failed */ 161208018Sthompsa 162208018Sthompsa if (usbd_get_bus_index(udev) != usb_ctrl_debug.bus_index) 163208018Sthompsa return; 164208018Sthompsa 165208018Sthompsa if (usbd_get_device_index(udev) != usb_ctrl_debug.dev_index) 166208018Sthompsa return; 167208018Sthompsa 168208018Sthompsa temp = usb_ctrl_debug.bmRequestType_value; 169208018Sthompsa 170208018Sthompsa if ((temp != req->bmRequestType) && (temp >= 0) && (temp <= 255)) 171208018Sthompsa return; 172208018Sthompsa 173208018Sthompsa temp = usb_ctrl_debug.bRequest_value; 174208018Sthompsa 175208018Sthompsa if ((temp != req->bRequest) && (temp >= 0) && (temp <= 255)) 176208018Sthompsa return; 177208018Sthompsa 178208018Sthompsa temp = usb_ctrl_debug.ds_fail; 179208018Sthompsa if (temp) 180208018Sthompsa dbg->ds_fail = 1; 181208018Sthompsa 182208018Sthompsa temp = usb_ctrl_debug.ss_fail; 183208018Sthompsa if (temp) 184208018Sthompsa dbg->ss_fail = 1; 185208018Sthompsa 186208018Sthompsa dbg->enabled = 1; 187208018Sthompsa} 188208018Sthompsa#endif /* USB_REQ_DEBUG */ 189208018Sthompsa#endif /* USB_DEBUG */ 190208018Sthompsa 191208018Sthompsa/*------------------------------------------------------------------------* 192194228Sthompsa * usbd_do_request_callback 193184610Salfred * 194184610Salfred * This function is the USB callback for generic USB Host control 195184610Salfred * transfers. 196184610Salfred *------------------------------------------------------------------------*/ 197184610Salfredvoid 198194677Sthompsausbd_do_request_callback(struct usb_xfer *xfer, usb_error_t error) 199184610Salfred{ 200184610Salfred ; /* workaround for a bug in "indent" */ 201184610Salfred 202184610Salfred DPRINTF("st=%u\n", USB_GET_STATE(xfer)); 203184610Salfred 204184610Salfred switch (USB_GET_STATE(xfer)) { 205184610Salfred case USB_ST_SETUP: 206194228Sthompsa usbd_transfer_submit(xfer); 207184610Salfred break; 208184610Salfred default: 209207079Sthompsa cv_signal(&xfer->xroot->udev->ctrlreq_cv); 210184610Salfred break; 211184610Salfred } 212184610Salfred} 213184610Salfred 214184610Salfred/*------------------------------------------------------------------------* 215194228Sthompsa * usb_do_clear_stall_callback 216184610Salfred * 217184610Salfred * This function is the USB callback for generic clear stall requests. 218184610Salfred *------------------------------------------------------------------------*/ 219184610Salfredvoid 220194677Sthompsausb_do_clear_stall_callback(struct usb_xfer *xfer, usb_error_t error) 221184610Salfred{ 222192984Sthompsa struct usb_device_request req; 223192984Sthompsa struct usb_device *udev; 224193644Sthompsa struct usb_endpoint *ep; 225193644Sthompsa struct usb_endpoint *ep_end; 226193644Sthompsa struct usb_endpoint *ep_first; 227190731Sthompsa uint8_t to; 228184610Salfred 229187173Sthompsa udev = xfer->xroot->udev; 230184610Salfred 231187173Sthompsa USB_BUS_LOCK(udev->bus); 232187173Sthompsa 233193644Sthompsa /* round robin endpoint clear stall */ 234184610Salfred 235193644Sthompsa ep = udev->ep_curr; 236193644Sthompsa ep_end = udev->endpoints + udev->endpoints_max; 237193644Sthompsa ep_first = udev->endpoints; 238193644Sthompsa to = udev->endpoints_max; 239193318Sthompsa 240184610Salfred switch (USB_GET_STATE(xfer)) { 241184610Salfred case USB_ST_TRANSFERRED: 242193644Sthompsa if (ep == NULL) 243193318Sthompsa goto tr_setup; /* device was unconfigured */ 244193644Sthompsa if (ep->edesc && 245193644Sthompsa ep->is_stalled) { 246193644Sthompsa ep->toggle_next = 0; 247193644Sthompsa ep->is_stalled = 0; 248213435Shselasky /* some hardware needs a callback to clear the data toggle */ 249213435Shselasky usbd_clear_stall_locked(udev, ep); 250184610Salfred /* start up the current or next transfer, if any */ 251194228Sthompsa usb_command_wrapper(&ep->endpoint_q, 252193644Sthompsa ep->endpoint_q.curr); 253184610Salfred } 254193644Sthompsa ep++; 255184610Salfred 256184610Salfred case USB_ST_SETUP: 257184610Salfredtr_setup: 258193318Sthompsa if (to == 0) 259193644Sthompsa break; /* no endpoints - nothing to do */ 260193644Sthompsa if ((ep < ep_first) || (ep >= ep_end)) 261193644Sthompsa ep = ep_first; /* endpoint wrapped around */ 262193644Sthompsa if (ep->edesc && 263193644Sthompsa ep->is_stalled) { 264184610Salfred 265184610Salfred /* setup a clear-stall packet */ 266184610Salfred 267184610Salfred req.bmRequestType = UT_WRITE_ENDPOINT; 268184610Salfred req.bRequest = UR_CLEAR_FEATURE; 269184610Salfred USETW(req.wValue, UF_ENDPOINT_HALT); 270193644Sthompsa req.wIndex[0] = ep->edesc->bEndpointAddress; 271184610Salfred req.wIndex[1] = 0; 272184610Salfred USETW(req.wLength, 0); 273184610Salfred 274184610Salfred /* copy in the transfer */ 275184610Salfred 276194228Sthompsa usbd_copy_in(xfer->frbuffers, 0, &req, sizeof(req)); 277184610Salfred 278184610Salfred /* set length */ 279194677Sthompsa usbd_xfer_set_frame_len(xfer, 0, sizeof(req)); 280184610Salfred xfer->nframes = 1; 281187173Sthompsa USB_BUS_UNLOCK(udev->bus); 282184610Salfred 283194228Sthompsa usbd_transfer_submit(xfer); 284184610Salfred 285187173Sthompsa USB_BUS_LOCK(udev->bus); 286184610Salfred break; 287184610Salfred } 288193644Sthompsa ep++; 289193318Sthompsa to--; 290193318Sthompsa goto tr_setup; 291184610Salfred 292184610Salfred default: 293184610Salfred if (xfer->error == USB_ERR_CANCELLED) { 294184610Salfred break; 295184610Salfred } 296184610Salfred goto tr_setup; 297184610Salfred } 298184610Salfred 299193644Sthompsa /* store current endpoint */ 300193644Sthompsa udev->ep_curr = ep; 301187173Sthompsa USB_BUS_UNLOCK(udev->bus); 302184610Salfred} 303184610Salfred 304193045Sthompsastatic usb_handle_req_t * 305194228Sthompsausbd_get_hr_func(struct usb_device *udev) 306191402Sthompsa{ 307191402Sthompsa /* figure out if there is a Handle Request function */ 308192499Sthompsa if (udev->flags.usb_mode == USB_MODE_DEVICE) 309194228Sthompsa return (usb_temp_get_desc_p); 310191402Sthompsa else if (udev->parent_hub == NULL) 311191402Sthompsa return (udev->bus->methods->roothub_exec); 312191402Sthompsa else 313191402Sthompsa return (NULL); 314191402Sthompsa} 315191402Sthompsa 316184610Salfred/*------------------------------------------------------------------------* 317194228Sthompsa * usbd_do_request_flags and usbd_do_request 318184610Salfred * 319184610Salfred * Description of arguments passed to these functions: 320184610Salfred * 321192984Sthompsa * "udev" - this is the "usb_device" structure pointer on which the 322184610Salfred * request should be performed. It is possible to call this function 323184610Salfred * in both Host Side mode and Device Side mode. 324184610Salfred * 325184610Salfred * "mtx" - if this argument is non-NULL the mutex pointed to by it 326184610Salfred * will get dropped and picked up during the execution of this 327184610Salfred * function, hence this function sometimes needs to sleep. If this 328184610Salfred * argument is NULL it has no effect. 329184610Salfred * 330184610Salfred * "req" - this argument must always be non-NULL and points to an 331184610Salfred * 8-byte structure holding the USB request to be done. The USB 332184610Salfred * request structure has a bit telling the direction of the USB 333184610Salfred * request, if it is a read or a write. 334184610Salfred * 335184610Salfred * "data" - if the "wLength" part of the structure pointed to by "req" 336184610Salfred * is non-zero this argument must point to a valid kernel buffer which 337184610Salfred * can hold at least "wLength" bytes. If "wLength" is zero "data" can 338184610Salfred * be NULL. 339184610Salfred * 340184610Salfred * "flags" - here is a list of valid flags: 341184610Salfred * 342184610Salfred * o USB_SHORT_XFER_OK: allows the data transfer to be shorter than 343184610Salfred * specified 344184610Salfred * 345184610Salfred * o USB_DELAY_STATUS_STAGE: allows the status stage to be performed 346184610Salfred * at a later point in time. This is tunable by the "hw.usb.ss_delay" 347184610Salfred * sysctl. This flag is mostly useful for debugging. 348184610Salfred * 349184610Salfred * o USB_USER_DATA_PTR: treat the "data" pointer like a userland 350184610Salfred * pointer. 351184610Salfred * 352184610Salfred * "actlen" - if non-NULL the actual transfer length will be stored in 353184610Salfred * the 16-bit unsigned integer pointed to by "actlen". This 354184610Salfred * information is mostly useful when the "USB_SHORT_XFER_OK" flag is 355184610Salfred * used. 356184610Salfred * 357184610Salfred * "timeout" - gives the timeout for the control transfer in 358184610Salfred * milliseconds. A "timeout" value less than 50 milliseconds is 359184610Salfred * treated like a 50 millisecond timeout. A "timeout" value greater 360184610Salfred * than 30 seconds is treated like a 30 second timeout. This USB stack 361184610Salfred * does not allow control requests without a timeout. 362184610Salfred * 363184610Salfred * NOTE: This function is thread safe. All calls to 364194228Sthompsa * "usbd_do_request_flags" will be serialised by the use of an 365184610Salfred * internal "sx_lock". 366184610Salfred * 367184610Salfred * Returns: 368184610Salfred * 0: Success 369184610Salfred * Else: Failure 370184610Salfred *------------------------------------------------------------------------*/ 371193045Sthompsausb_error_t 372194228Sthompsausbd_do_request_flags(struct usb_device *udev, struct mtx *mtx, 373192984Sthompsa struct usb_device_request *req, void *data, uint16_t flags, 374193045Sthompsa uint16_t *actlen, usb_timeout_t timeout) 375184610Salfred{ 376208018Sthompsa#ifdef USB_REQ_DEBUG 377208018Sthompsa struct usb_ctrl_debug_bits dbg; 378208018Sthompsa#endif 379193045Sthompsa usb_handle_req_t *hr_func; 380192984Sthompsa struct usb_xfer *xfer; 381184610Salfred const void *desc; 382184610Salfred int err = 0; 383193045Sthompsa usb_ticks_t start_ticks; 384193045Sthompsa usb_ticks_t delta_ticks; 385193045Sthompsa usb_ticks_t max_ticks; 386184610Salfred uint16_t length; 387184610Salfred uint16_t temp; 388208018Sthompsa uint16_t acttemp; 389208008Sthompsa uint8_t enum_locked; 390184610Salfred 391184610Salfred if (timeout < 50) { 392184610Salfred /* timeout is too small */ 393184610Salfred timeout = 50; 394184610Salfred } 395184610Salfred if (timeout > 30000) { 396184610Salfred /* timeout is too big */ 397184610Salfred timeout = 30000; 398184610Salfred } 399184610Salfred length = UGETW(req->wLength); 400184610Salfred 401208008Sthompsa enum_locked = usbd_enum_is_locked(udev); 402208008Sthompsa 403184610Salfred DPRINTFN(5, "udev=%p bmRequestType=0x%02x bRequest=0x%02x " 404184610Salfred "wValue=0x%02x%02x wIndex=0x%02x%02x wLength=0x%02x%02x\n", 405184610Salfred udev, req->bmRequestType, req->bRequest, 406184610Salfred req->wValue[1], req->wValue[0], 407184610Salfred req->wIndex[1], req->wIndex[0], 408184610Salfred req->wLength[1], req->wLength[0]); 409184610Salfred 410191494Sthompsa /* Check if the device is still alive */ 411191494Sthompsa if (udev->state < USB_STATE_POWERED) { 412191494Sthompsa DPRINTF("usb device has gone\n"); 413191494Sthompsa return (USB_ERR_NOT_CONFIGURED); 414191494Sthompsa } 415191494Sthompsa 416184610Salfred /* 417184610Salfred * Set "actlen" to a known value in case the caller does not 418184610Salfred * check the return value: 419184610Salfred */ 420190735Sthompsa if (actlen) 421184610Salfred *actlen = 0; 422190735Sthompsa 423190180Sthompsa#if (USB_HAVE_USER_IO == 0) 424190180Sthompsa if (flags & USB_USER_DATA_PTR) 425190180Sthompsa return (USB_ERR_INVAL); 426190180Sthompsa#endif 427208008Sthompsa if ((mtx != NULL) && (mtx != &Giant)) { 428184610Salfred mtx_unlock(mtx); 429208008Sthompsa mtx_assert(mtx, MA_NOTOWNED); 430184610Salfred } 431208008Sthompsa 432184610Salfred /* 433208008Sthompsa * We need to allow suspend and resume at this point, else the 434208008Sthompsa * control transfer will timeout if the device is suspended! 435208008Sthompsa */ 436208008Sthompsa if (enum_locked) 437208008Sthompsa usbd_sr_unlock(udev); 438208008Sthompsa 439208008Sthompsa /* 440184610Salfred * Grab the default sx-lock so that serialisation 441184610Salfred * is achieved when multiple threads are involved: 442184610Salfred */ 443207079Sthompsa sx_xlock(&udev->ctrl_sx); 444184610Salfred 445194228Sthompsa hr_func = usbd_get_hr_func(udev); 446190735Sthompsa 447191402Sthompsa if (hr_func != NULL) { 448191402Sthompsa DPRINTF("Handle Request function is set\n"); 449190735Sthompsa 450191402Sthompsa desc = NULL; 451191402Sthompsa temp = 0; 452191402Sthompsa 453191402Sthompsa if (!(req->bmRequestType & UT_READ)) { 454190735Sthompsa if (length != 0) { 455191402Sthompsa DPRINTFN(1, "The handle request function " 456191402Sthompsa "does not support writing data!\n"); 457191402Sthompsa err = USB_ERR_INVAL; 458191402Sthompsa goto done; 459190735Sthompsa } 460190735Sthompsa } 461190735Sthompsa 462191402Sthompsa /* The root HUB code needs the BUS lock locked */ 463191402Sthompsa 464190735Sthompsa USB_BUS_LOCK(udev->bus); 465191402Sthompsa err = (hr_func) (udev, req, &desc, &temp); 466190735Sthompsa USB_BUS_UNLOCK(udev->bus); 467190735Sthompsa 468190735Sthompsa if (err) 469190735Sthompsa goto done; 470190735Sthompsa 471191402Sthompsa if (length > temp) { 472190735Sthompsa if (!(flags & USB_SHORT_XFER_OK)) { 473190735Sthompsa err = USB_ERR_SHORT_XFER; 474190735Sthompsa goto done; 475190735Sthompsa } 476191402Sthompsa length = temp; 477190735Sthompsa } 478190735Sthompsa if (actlen) 479190735Sthompsa *actlen = length; 480190735Sthompsa 481190735Sthompsa if (length > 0) { 482190735Sthompsa#if USB_HAVE_USER_IO 483190735Sthompsa if (flags & USB_USER_DATA_PTR) { 484191402Sthompsa if (copyout(desc, data, length)) { 485190735Sthompsa err = USB_ERR_INVAL; 486190735Sthompsa goto done; 487190735Sthompsa } 488190735Sthompsa } else 489190735Sthompsa#endif 490191402Sthompsa bcopy(desc, data, length); 491190735Sthompsa } 492191402Sthompsa goto done; /* success */ 493190735Sthompsa } 494190735Sthompsa 495184610Salfred /* 496184610Salfred * Setup a new USB transfer or use the existing one, if any: 497184610Salfred */ 498207080Sthompsa usbd_ctrl_transfer_setup(udev); 499184610Salfred 500207080Sthompsa xfer = udev->ctrl_xfer[0]; 501184610Salfred if (xfer == NULL) { 502184610Salfred /* most likely out of memory */ 503184610Salfred err = USB_ERR_NOMEM; 504184610Salfred goto done; 505184610Salfred } 506208018Sthompsa 507208018Sthompsa#ifdef USB_REQ_DEBUG 508208018Sthompsa /* Get debug bits */ 509208018Sthompsa usbd_get_debug_bits(udev, req, &dbg); 510208018Sthompsa 511208018Sthompsa /* Check for fault injection */ 512208018Sthompsa if (dbg.enabled) 513208018Sthompsa flags |= USB_DELAY_STATUS_STAGE; 514208018Sthompsa#endif 515184824Sthompsa USB_XFER_LOCK(xfer); 516184610Salfred 517190734Sthompsa if (flags & USB_DELAY_STATUS_STAGE) 518184610Salfred xfer->flags.manual_status = 1; 519190734Sthompsa else 520184610Salfred xfer->flags.manual_status = 0; 521184610Salfred 522190734Sthompsa if (flags & USB_SHORT_XFER_OK) 523190734Sthompsa xfer->flags.short_xfer_ok = 1; 524190734Sthompsa else 525190734Sthompsa xfer->flags.short_xfer_ok = 0; 526190734Sthompsa 527184610Salfred xfer->timeout = timeout; 528184610Salfred 529184610Salfred start_ticks = ticks; 530184610Salfred 531184610Salfred max_ticks = USB_MS_TO_TICKS(timeout); 532184610Salfred 533194228Sthompsa usbd_copy_in(xfer->frbuffers, 0, req, sizeof(*req)); 534184610Salfred 535194677Sthompsa usbd_xfer_set_frame_len(xfer, 0, sizeof(*req)); 536184610Salfred 537184610Salfred while (1) { 538184610Salfred temp = length; 539208018Sthompsa if (temp > usbd_xfer_max_len(xfer)) { 540194677Sthompsa temp = usbd_xfer_max_len(xfer); 541184610Salfred } 542208018Sthompsa#ifdef USB_REQ_DEBUG 543208018Sthompsa if (xfer->flags.manual_status) { 544208018Sthompsa if (usbd_xfer_frame_len(xfer, 0) != 0) { 545208018Sthompsa /* Execute data stage separately */ 546208018Sthompsa temp = 0; 547208018Sthompsa } else if (temp > 0) { 548208018Sthompsa if (dbg.ds_fail) { 549208018Sthompsa err = USB_ERR_INVAL; 550208018Sthompsa break; 551208018Sthompsa } 552208018Sthompsa if (dbg.ds_delay > 0) { 553208018Sthompsa usb_pause_mtx( 554208018Sthompsa xfer->xroot->xfer_mtx, 555208018Sthompsa USB_MS_TO_TICKS(dbg.ds_delay)); 556208018Sthompsa /* make sure we don't time out */ 557208018Sthompsa start_ticks = ticks; 558208018Sthompsa } 559208018Sthompsa } 560208018Sthompsa } 561208018Sthompsa#endif 562194677Sthompsa usbd_xfer_set_frame_len(xfer, 1, temp); 563184610Salfred 564184610Salfred if (temp > 0) { 565184610Salfred if (!(req->bmRequestType & UT_READ)) { 566190180Sthompsa#if USB_HAVE_USER_IO 567184610Salfred if (flags & USB_USER_DATA_PTR) { 568184824Sthompsa USB_XFER_UNLOCK(xfer); 569194228Sthompsa err = usbd_copy_in_user(xfer->frbuffers + 1, 570184610Salfred 0, data, temp); 571184824Sthompsa USB_XFER_LOCK(xfer); 572184610Salfred if (err) { 573184610Salfred err = USB_ERR_INVAL; 574184610Salfred break; 575184610Salfred } 576190180Sthompsa } else 577190180Sthompsa#endif 578194228Sthompsa usbd_copy_in(xfer->frbuffers + 1, 579190180Sthompsa 0, data, temp); 580184610Salfred } 581208018Sthompsa usbd_xfer_set_frames(xfer, 2); 582184610Salfred } else { 583208018Sthompsa if (usbd_xfer_frame_len(xfer, 0) == 0) { 584184610Salfred if (xfer->flags.manual_status) { 585208018Sthompsa#ifdef USB_REQ_DEBUG 586208018Sthompsa if (dbg.ss_fail) { 587208018Sthompsa err = USB_ERR_INVAL; 588208018Sthompsa break; 589184610Salfred } 590208018Sthompsa if (dbg.ss_delay > 0) { 591194228Sthompsa usb_pause_mtx( 592187173Sthompsa xfer->xroot->xfer_mtx, 593208018Sthompsa USB_MS_TO_TICKS(dbg.ss_delay)); 594208018Sthompsa /* make sure we don't time out */ 595208018Sthompsa start_ticks = ticks; 596184610Salfred } 597184610Salfred#endif 598184610Salfred xfer->flags.manual_status = 0; 599184610Salfred } else { 600184610Salfred break; 601184610Salfred } 602184610Salfred } 603208018Sthompsa usbd_xfer_set_frames(xfer, 1); 604184610Salfred } 605184610Salfred 606194228Sthompsa usbd_transfer_start(xfer); 607184610Salfred 608194228Sthompsa while (usbd_transfer_pending(xfer)) { 609207079Sthompsa cv_wait(&udev->ctrlreq_cv, 610188983Sthompsa xfer->xroot->xfer_mtx); 611184610Salfred } 612184610Salfred 613184610Salfred err = xfer->error; 614184610Salfred 615184610Salfred if (err) { 616184610Salfred break; 617184610Salfred } 618184610Salfred 619208018Sthompsa /* get actual length of DATA stage */ 620208018Sthompsa 621208018Sthompsa if (xfer->aframes < 2) { 622208018Sthompsa acttemp = 0; 623184610Salfred } else { 624208018Sthompsa acttemp = usbd_xfer_frame_len(xfer, 1); 625184610Salfred } 626184610Salfred 627184610Salfred /* check for short packet */ 628184610Salfred 629208018Sthompsa if (temp > acttemp) { 630208018Sthompsa temp = acttemp; 631184610Salfred length = temp; 632184610Salfred } 633184610Salfred if (temp > 0) { 634184610Salfred if (req->bmRequestType & UT_READ) { 635190180Sthompsa#if USB_HAVE_USER_IO 636184610Salfred if (flags & USB_USER_DATA_PTR) { 637184824Sthompsa USB_XFER_UNLOCK(xfer); 638194228Sthompsa err = usbd_copy_out_user(xfer->frbuffers + 1, 639184610Salfred 0, data, temp); 640184824Sthompsa USB_XFER_LOCK(xfer); 641184610Salfred if (err) { 642184610Salfred err = USB_ERR_INVAL; 643184610Salfred break; 644184610Salfred } 645190180Sthompsa } else 646190180Sthompsa#endif 647194228Sthompsa usbd_copy_out(xfer->frbuffers + 1, 648184610Salfred 0, data, temp); 649184610Salfred } 650184610Salfred } 651184610Salfred /* 652184610Salfred * Clear "frlengths[0]" so that we don't send the setup 653184610Salfred * packet again: 654184610Salfred */ 655194677Sthompsa usbd_xfer_set_frame_len(xfer, 0, 0); 656184610Salfred 657184610Salfred /* update length and data pointer */ 658184610Salfred length -= temp; 659184610Salfred data = USB_ADD_BYTES(data, temp); 660184610Salfred 661184610Salfred if (actlen) { 662184610Salfred (*actlen) += temp; 663184610Salfred } 664184610Salfred /* check for timeout */ 665184610Salfred 666184610Salfred delta_ticks = ticks - start_ticks; 667184610Salfred if (delta_ticks > max_ticks) { 668184610Salfred if (!err) { 669184610Salfred err = USB_ERR_TIMEOUT; 670184610Salfred } 671184610Salfred } 672184610Salfred if (err) { 673184610Salfred break; 674184610Salfred } 675184610Salfred } 676184610Salfred 677184610Salfred if (err) { 678184610Salfred /* 679184610Salfred * Make sure that the control endpoint is no longer 680184610Salfred * blocked in case of a non-transfer related error: 681184610Salfred */ 682194228Sthompsa usbd_transfer_stop(xfer); 683184610Salfred } 684184824Sthompsa USB_XFER_UNLOCK(xfer); 685184610Salfred 686184610Salfreddone: 687207079Sthompsa sx_xunlock(&udev->ctrl_sx); 688184610Salfred 689208008Sthompsa if (enum_locked) 690208008Sthompsa usbd_sr_lock(udev); 691208008Sthompsa 692208008Sthompsa if ((mtx != NULL) && (mtx != &Giant)) 693184610Salfred mtx_lock(mtx); 694208008Sthompsa 695193045Sthompsa return ((usb_error_t)err); 696184610Salfred} 697184610Salfred 698184610Salfred/*------------------------------------------------------------------------* 699194228Sthompsa * usbd_do_request_proc - factored out code 700188411Sthompsa * 701188411Sthompsa * This function is factored out code. It does basically the same like 702194228Sthompsa * usbd_do_request_flags, except it will check the status of the 703188411Sthompsa * passed process argument before doing the USB request. If the 704188411Sthompsa * process is draining the USB_ERR_IOERROR code will be returned. It 705188411Sthompsa * is assumed that the mutex associated with the process is locked 706188411Sthompsa * when calling this function. 707188411Sthompsa *------------------------------------------------------------------------*/ 708193045Sthompsausb_error_t 709194228Sthompsausbd_do_request_proc(struct usb_device *udev, struct usb_process *pproc, 710192984Sthompsa struct usb_device_request *req, void *data, uint16_t flags, 711193045Sthompsa uint16_t *actlen, usb_timeout_t timeout) 712188411Sthompsa{ 713193045Sthompsa usb_error_t err; 714188411Sthompsa uint16_t len; 715188411Sthompsa 716188411Sthompsa /* get request data length */ 717188411Sthompsa len = UGETW(req->wLength); 718188411Sthompsa 719188411Sthompsa /* check if the device is being detached */ 720194228Sthompsa if (usb_proc_is_gone(pproc)) { 721188411Sthompsa err = USB_ERR_IOERROR; 722188411Sthompsa goto done; 723188411Sthompsa } 724188411Sthompsa 725188411Sthompsa /* forward the USB request */ 726194228Sthompsa err = usbd_do_request_flags(udev, pproc->up_mtx, 727188411Sthompsa req, data, flags, actlen, timeout); 728188411Sthompsa 729188411Sthompsadone: 730188411Sthompsa /* on failure we zero the data */ 731188411Sthompsa /* on short packet we zero the unused data */ 732188411Sthompsa if ((len != 0) && (req->bmRequestType & UE_DIR_IN)) { 733188411Sthompsa if (err) 734188411Sthompsa memset(data, 0, len); 735188411Sthompsa else if (actlen && *actlen != len) 736188411Sthompsa memset(((uint8_t *)data) + *actlen, 0, len - *actlen); 737188411Sthompsa } 738188411Sthompsa return (err); 739188411Sthompsa} 740188411Sthompsa 741188411Sthompsa/*------------------------------------------------------------------------* 742194228Sthompsa * usbd_req_reset_port 743184610Salfred * 744184610Salfred * This function will instruct an USB HUB to perform a reset sequence 745184610Salfred * on the specified port number. 746184610Salfred * 747184610Salfred * Returns: 748184610Salfred * 0: Success. The USB device should now be at address zero. 749184610Salfred * Else: Failure. No USB device is present and the USB port should be 750184610Salfred * disabled. 751184610Salfred *------------------------------------------------------------------------*/ 752193045Sthompsausb_error_t 753194228Sthompsausbd_req_reset_port(struct usb_device *udev, struct mtx *mtx, uint8_t port) 754184610Salfred{ 755192984Sthompsa struct usb_port_status ps; 756193045Sthompsa usb_error_t err; 757184610Salfred uint16_t n; 758184610Salfred 759207077Sthompsa#ifdef USB_DEBUG 760184610Salfred uint16_t pr_poll_delay; 761184610Salfred uint16_t pr_recovery_delay; 762184610Salfred 763184610Salfred#endif 764194228Sthompsa err = usbd_req_set_port_feature(udev, mtx, port, UHF_PORT_RESET); 765184610Salfred if (err) { 766184610Salfred goto done; 767184610Salfred } 768207077Sthompsa#ifdef USB_DEBUG 769184610Salfred /* range check input parameters */ 770194228Sthompsa pr_poll_delay = usb_pr_poll_delay; 771184610Salfred if (pr_poll_delay < 1) { 772184610Salfred pr_poll_delay = 1; 773184610Salfred } else if (pr_poll_delay > 1000) { 774184610Salfred pr_poll_delay = 1000; 775184610Salfred } 776194228Sthompsa pr_recovery_delay = usb_pr_recovery_delay; 777184610Salfred if (pr_recovery_delay > 1000) { 778184610Salfred pr_recovery_delay = 1000; 779184610Salfred } 780184610Salfred#endif 781184610Salfred n = 0; 782184610Salfred while (1) { 783207077Sthompsa#ifdef USB_DEBUG 784184610Salfred /* wait for the device to recover from reset */ 785194228Sthompsa usb_pause_mtx(mtx, USB_MS_TO_TICKS(pr_poll_delay)); 786184610Salfred n += pr_poll_delay; 787184610Salfred#else 788184610Salfred /* wait for the device to recover from reset */ 789194228Sthompsa usb_pause_mtx(mtx, USB_MS_TO_TICKS(USB_PORT_RESET_DELAY)); 790184610Salfred n += USB_PORT_RESET_DELAY; 791184610Salfred#endif 792194228Sthompsa err = usbd_req_get_port_status(udev, mtx, &ps, port); 793184610Salfred if (err) { 794184610Salfred goto done; 795184610Salfred } 796184610Salfred /* if the device disappeared, just give up */ 797184610Salfred if (!(UGETW(ps.wPortStatus) & UPS_CURRENT_CONNECT_STATUS)) { 798184610Salfred goto done; 799184610Salfred } 800184610Salfred /* check if reset is complete */ 801184610Salfred if (UGETW(ps.wPortChange) & UPS_C_PORT_RESET) { 802184610Salfred break; 803184610Salfred } 804184610Salfred /* check for timeout */ 805184610Salfred if (n > 1000) { 806184610Salfred n = 0; 807184610Salfred break; 808184610Salfred } 809184610Salfred } 810184610Salfred 811184610Salfred /* clear port reset first */ 812194228Sthompsa err = usbd_req_clear_port_feature( 813184610Salfred udev, mtx, port, UHF_C_PORT_RESET); 814184610Salfred if (err) { 815184610Salfred goto done; 816184610Salfred } 817184610Salfred /* check for timeout */ 818184610Salfred if (n == 0) { 819184610Salfred err = USB_ERR_TIMEOUT; 820184610Salfred goto done; 821184610Salfred } 822207077Sthompsa#ifdef USB_DEBUG 823184610Salfred /* wait for the device to recover from reset */ 824194228Sthompsa usb_pause_mtx(mtx, USB_MS_TO_TICKS(pr_recovery_delay)); 825184610Salfred#else 826184610Salfred /* wait for the device to recover from reset */ 827194228Sthompsa usb_pause_mtx(mtx, USB_MS_TO_TICKS(USB_PORT_RESET_RECOVERY)); 828184610Salfred#endif 829184610Salfred 830184610Salfreddone: 831184610Salfred DPRINTFN(2, "port %d reset returning error=%s\n", 832194228Sthompsa port, usbd_errstr(err)); 833184610Salfred return (err); 834184610Salfred} 835184610Salfred 836184610Salfred/*------------------------------------------------------------------------* 837194228Sthompsa * usbd_req_get_desc 838184610Salfred * 839184610Salfred * This function can be used to retrieve USB descriptors. It contains 840184610Salfred * some additional logic like zeroing of missing descriptor bytes and 841184610Salfred * retrying an USB descriptor in case of failure. The "min_len" 842184610Salfred * argument specifies the minimum descriptor length. The "max_len" 843184610Salfred * argument specifies the maximum descriptor length. If the real 844184610Salfred * descriptor length is less than the minimum length the missing 845188985Sthompsa * byte(s) will be zeroed. The type field, the second byte of the USB 846188985Sthompsa * descriptor, will get forced to the correct type. If the "actlen" 847188985Sthompsa * pointer is non-NULL, the actual length of the transfer will get 848188985Sthompsa * stored in the 16-bit unsigned integer which it is pointing to. The 849188985Sthompsa * first byte of the descriptor will not get updated. If the "actlen" 850188985Sthompsa * pointer is NULL the first byte of the descriptor will get updated 851188985Sthompsa * to reflect the actual length instead. If "min_len" is not equal to 852188985Sthompsa * "max_len" then this function will try to retrive the beginning of 853188985Sthompsa * the descriptor and base the maximum length on the first byte of the 854188985Sthompsa * descriptor. 855184610Salfred * 856184610Salfred * Returns: 857184610Salfred * 0: Success 858184610Salfred * Else: Failure 859184610Salfred *------------------------------------------------------------------------*/ 860193045Sthompsausb_error_t 861194228Sthompsausbd_req_get_desc(struct usb_device *udev, 862188985Sthompsa struct mtx *mtx, uint16_t *actlen, void *desc, 863184610Salfred uint16_t min_len, uint16_t max_len, 864184610Salfred uint16_t id, uint8_t type, uint8_t index, 865184610Salfred uint8_t retries) 866184610Salfred{ 867192984Sthompsa struct usb_device_request req; 868184610Salfred uint8_t *buf; 869193045Sthompsa usb_error_t err; 870184610Salfred 871184610Salfred DPRINTFN(4, "id=%d, type=%d, index=%d, max_len=%d\n", 872184610Salfred id, type, index, max_len); 873184610Salfred 874184610Salfred req.bmRequestType = UT_READ_DEVICE; 875184610Salfred req.bRequest = UR_GET_DESCRIPTOR; 876184610Salfred USETW2(req.wValue, type, index); 877184610Salfred USETW(req.wIndex, id); 878184610Salfred 879184610Salfred while (1) { 880184610Salfred 881184610Salfred if ((min_len < 2) || (max_len < 2)) { 882184610Salfred err = USB_ERR_INVAL; 883184610Salfred goto done; 884184610Salfred } 885184610Salfred USETW(req.wLength, min_len); 886184610Salfred 887194228Sthompsa err = usbd_do_request_flags(udev, mtx, &req, 888186730Salfred desc, 0, NULL, 1000); 889184610Salfred 890184610Salfred if (err) { 891184610Salfred if (!retries) { 892184610Salfred goto done; 893184610Salfred } 894184610Salfred retries--; 895184610Salfred 896194228Sthompsa usb_pause_mtx(mtx, hz / 5); 897184610Salfred 898184610Salfred continue; 899184610Salfred } 900184610Salfred buf = desc; 901184610Salfred 902184610Salfred if (min_len == max_len) { 903184610Salfred 904188985Sthompsa /* enforce correct length */ 905188985Sthompsa if ((buf[0] > min_len) && (actlen == NULL)) 906188985Sthompsa buf[0] = min_len; 907184610Salfred 908188985Sthompsa /* enforce correct type */ 909184610Salfred buf[1] = type; 910184610Salfred 911184610Salfred goto done; 912184610Salfred } 913184610Salfred /* range check */ 914184610Salfred 915184610Salfred if (max_len > buf[0]) { 916184610Salfred max_len = buf[0]; 917184610Salfred } 918184610Salfred /* zero minimum data */ 919184610Salfred 920184610Salfred while (min_len > max_len) { 921184610Salfred min_len--; 922184610Salfred buf[min_len] = 0; 923184610Salfred } 924184610Salfred 925184610Salfred /* set new minimum length */ 926184610Salfred 927184610Salfred min_len = max_len; 928184610Salfred } 929184610Salfreddone: 930188985Sthompsa if (actlen != NULL) { 931188985Sthompsa if (err) 932188985Sthompsa *actlen = 0; 933188985Sthompsa else 934188985Sthompsa *actlen = min_len; 935188985Sthompsa } 936184610Salfred return (err); 937184610Salfred} 938184610Salfred 939184610Salfred/*------------------------------------------------------------------------* 940194228Sthompsa * usbd_req_get_string_any 941184610Salfred * 942184610Salfred * This function will return the string given by "string_index" 943184610Salfred * using the first language ID. The maximum length "len" includes 944184610Salfred * the terminating zero. The "len" argument should be twice as 945184610Salfred * big pluss 2 bytes, compared with the actual maximum string length ! 946184610Salfred * 947184610Salfred * Returns: 948184610Salfred * 0: Success 949184610Salfred * Else: Failure 950184610Salfred *------------------------------------------------------------------------*/ 951193045Sthompsausb_error_t 952194228Sthompsausbd_req_get_string_any(struct usb_device *udev, struct mtx *mtx, char *buf, 953184610Salfred uint16_t len, uint8_t string_index) 954184610Salfred{ 955184610Salfred char *s; 956184610Salfred uint8_t *temp; 957184610Salfred uint16_t i; 958184610Salfred uint16_t n; 959184610Salfred uint16_t c; 960184610Salfred uint8_t swap; 961193045Sthompsa usb_error_t err; 962184610Salfred 963184610Salfred if (len == 0) { 964184610Salfred /* should not happen */ 965184610Salfred return (USB_ERR_NORMAL_COMPLETION); 966184610Salfred } 967184610Salfred if (string_index == 0) { 968184610Salfred /* this is the language table */ 969185087Salfred buf[0] = 0; 970184610Salfred return (USB_ERR_INVAL); 971184610Salfred } 972184610Salfred if (udev->flags.no_strings) { 973185087Salfred buf[0] = 0; 974184610Salfred return (USB_ERR_STALLED); 975184610Salfred } 976194228Sthompsa err = usbd_req_get_string_desc 977184610Salfred (udev, mtx, buf, len, udev->langid, string_index); 978184610Salfred if (err) { 979185087Salfred buf[0] = 0; 980184610Salfred return (err); 981184610Salfred } 982184610Salfred temp = (uint8_t *)buf; 983184610Salfred 984184610Salfred if (temp[0] < 2) { 985184610Salfred /* string length is too short */ 986185087Salfred buf[0] = 0; 987184610Salfred return (USB_ERR_INVAL); 988184610Salfred } 989184610Salfred /* reserve one byte for terminating zero */ 990184610Salfred len--; 991184610Salfred 992184610Salfred /* find maximum length */ 993184610Salfred s = buf; 994184610Salfred n = (temp[0] / 2) - 1; 995184610Salfred if (n > len) { 996184610Salfred n = len; 997184610Salfred } 998184610Salfred /* skip descriptor header */ 999184610Salfred temp += 2; 1000184610Salfred 1001184610Salfred /* reset swap state */ 1002184610Salfred swap = 3; 1003184610Salfred 1004184610Salfred /* convert and filter */ 1005184610Salfred for (i = 0; (i != n); i++) { 1006184610Salfred c = UGETW(temp + (2 * i)); 1007184610Salfred 1008184610Salfred /* convert from Unicode, handle buggy strings */ 1009184610Salfred if (((c & 0xff00) == 0) && (swap & 1)) { 1010184610Salfred /* Little Endian, default */ 1011184610Salfred *s = c; 1012184610Salfred swap = 1; 1013184610Salfred } else if (((c & 0x00ff) == 0) && (swap & 2)) { 1014184610Salfred /* Big Endian */ 1015184610Salfred *s = c >> 8; 1016184610Salfred swap = 2; 1017184610Salfred } else { 1018185087Salfred /* silently skip bad character */ 1019185087Salfred continue; 1020184610Salfred } 1021184610Salfred 1022184610Salfred /* 1023213433Shselasky * Filter by default - We only allow alphanumerical 1024213433Shselasky * and a few more to avoid any problems with scripts 1025213433Shselasky * and daemons. 1026184610Salfred */ 1027213433Shselasky if (isalpha(*s) || 1028213433Shselasky isdigit(*s) || 1029213433Shselasky *s == '-' || 1030213433Shselasky *s == '+' || 1031213433Shselasky *s == ' ' || 1032213433Shselasky *s == '.' || 1033213433Shselasky *s == ',') { 1034213433Shselasky /* allowed */ 1035213433Shselasky s++; 1036184610Salfred } 1037213433Shselasky /* silently skip bad character */ 1038184610Salfred } 1039185087Salfred *s = 0; /* zero terminate resulting string */ 1040184610Salfred return (USB_ERR_NORMAL_COMPLETION); 1041184610Salfred} 1042184610Salfred 1043184610Salfred/*------------------------------------------------------------------------* 1044194228Sthompsa * usbd_req_get_string_desc 1045184610Salfred * 1046184610Salfred * If you don't know the language ID, consider using 1047194228Sthompsa * "usbd_req_get_string_any()". 1048184610Salfred * 1049184610Salfred * Returns: 1050184610Salfred * 0: Success 1051184610Salfred * Else: Failure 1052184610Salfred *------------------------------------------------------------------------*/ 1053193045Sthompsausb_error_t 1054194228Sthompsausbd_req_get_string_desc(struct usb_device *udev, struct mtx *mtx, void *sdesc, 1055184610Salfred uint16_t max_len, uint16_t lang_id, 1056184610Salfred uint8_t string_index) 1057184610Salfred{ 1058194228Sthompsa return (usbd_req_get_desc(udev, mtx, NULL, sdesc, 2, max_len, lang_id, 1059184610Salfred UDESC_STRING, string_index, 0)); 1060184610Salfred} 1061184610Salfred 1062184610Salfred/*------------------------------------------------------------------------* 1063194228Sthompsa * usbd_req_get_config_desc_ptr 1064190727Sthompsa * 1065190727Sthompsa * This function is used in device side mode to retrieve the pointer 1066190727Sthompsa * to the generated config descriptor. This saves allocating space for 1067190727Sthompsa * an additional config descriptor when setting the configuration. 1068190727Sthompsa * 1069190727Sthompsa * Returns: 1070190727Sthompsa * 0: Success 1071190727Sthompsa * Else: Failure 1072190727Sthompsa *------------------------------------------------------------------------*/ 1073193045Sthompsausb_error_t 1074194228Sthompsausbd_req_get_descriptor_ptr(struct usb_device *udev, 1075192984Sthompsa struct usb_config_descriptor **ppcd, uint16_t wValue) 1076190727Sthompsa{ 1077192984Sthompsa struct usb_device_request req; 1078193045Sthompsa usb_handle_req_t *hr_func; 1079191402Sthompsa const void *ptr; 1080190727Sthompsa uint16_t len; 1081193045Sthompsa usb_error_t err; 1082190727Sthompsa 1083190731Sthompsa req.bmRequestType = UT_READ_DEVICE; 1084190727Sthompsa req.bRequest = UR_GET_DESCRIPTOR; 1085191402Sthompsa USETW(req.wValue, wValue); 1086190727Sthompsa USETW(req.wIndex, 0); 1087190727Sthompsa USETW(req.wLength, 0); 1088190727Sthompsa 1089191402Sthompsa ptr = NULL; 1090191402Sthompsa len = 0; 1091190727Sthompsa 1092194228Sthompsa hr_func = usbd_get_hr_func(udev); 1093191402Sthompsa 1094191402Sthompsa if (hr_func == NULL) 1095191402Sthompsa err = USB_ERR_INVAL; 1096191402Sthompsa else { 1097191402Sthompsa USB_BUS_LOCK(udev->bus); 1098191402Sthompsa err = (hr_func) (udev, &req, &ptr, &len); 1099191402Sthompsa USB_BUS_UNLOCK(udev->bus); 1100191402Sthompsa } 1101191402Sthompsa 1102191402Sthompsa if (err) 1103191402Sthompsa ptr = NULL; 1104191402Sthompsa else if (ptr == NULL) 1105191402Sthompsa err = USB_ERR_INVAL; 1106191402Sthompsa 1107192984Sthompsa *ppcd = __DECONST(struct usb_config_descriptor *, ptr); 1108191402Sthompsa 1109191402Sthompsa return (err); 1110190727Sthompsa} 1111190727Sthompsa 1112190727Sthompsa/*------------------------------------------------------------------------* 1113194228Sthompsa * usbd_req_get_config_desc 1114184610Salfred * 1115184610Salfred * Returns: 1116184610Salfred * 0: Success 1117184610Salfred * Else: Failure 1118184610Salfred *------------------------------------------------------------------------*/ 1119193045Sthompsausb_error_t 1120194228Sthompsausbd_req_get_config_desc(struct usb_device *udev, struct mtx *mtx, 1121192984Sthompsa struct usb_config_descriptor *d, uint8_t conf_index) 1122184610Salfred{ 1123193045Sthompsa usb_error_t err; 1124184610Salfred 1125184610Salfred DPRINTFN(4, "confidx=%d\n", conf_index); 1126184610Salfred 1127194228Sthompsa err = usbd_req_get_desc(udev, mtx, NULL, d, sizeof(*d), 1128184610Salfred sizeof(*d), 0, UDESC_CONFIG, conf_index, 0); 1129184610Salfred if (err) { 1130184610Salfred goto done; 1131184610Salfred } 1132184610Salfred /* Extra sanity checking */ 1133184610Salfred if (UGETW(d->wTotalLength) < sizeof(*d)) { 1134184610Salfred err = USB_ERR_INVAL; 1135184610Salfred } 1136184610Salfreddone: 1137184610Salfred return (err); 1138184610Salfred} 1139184610Salfred 1140184610Salfred/*------------------------------------------------------------------------* 1141194228Sthompsa * usbd_req_get_config_desc_full 1142184610Salfred * 1143184610Salfred * This function gets the complete USB configuration descriptor and 1144184610Salfred * ensures that "wTotalLength" is correct. 1145184610Salfred * 1146184610Salfred * Returns: 1147184610Salfred * 0: Success 1148184610Salfred * Else: Failure 1149184610Salfred *------------------------------------------------------------------------*/ 1150193045Sthompsausb_error_t 1151194228Sthompsausbd_req_get_config_desc_full(struct usb_device *udev, struct mtx *mtx, 1152192984Sthompsa struct usb_config_descriptor **ppcd, struct malloc_type *mtype, 1153184610Salfred uint8_t index) 1154184610Salfred{ 1155192984Sthompsa struct usb_config_descriptor cd; 1156192984Sthompsa struct usb_config_descriptor *cdesc; 1157184610Salfred uint16_t len; 1158193045Sthompsa usb_error_t err; 1159184610Salfred 1160184610Salfred DPRINTFN(4, "index=%d\n", index); 1161184610Salfred 1162184610Salfred *ppcd = NULL; 1163184610Salfred 1164194228Sthompsa err = usbd_req_get_config_desc(udev, mtx, &cd, index); 1165184610Salfred if (err) { 1166184610Salfred return (err); 1167184610Salfred } 1168184610Salfred /* get full descriptor */ 1169184610Salfred len = UGETW(cd.wTotalLength); 1170184610Salfred if (len < sizeof(*cdesc)) { 1171184610Salfred /* corrupt descriptor */ 1172184610Salfred return (USB_ERR_INVAL); 1173184610Salfred } 1174184610Salfred cdesc = malloc(len, mtype, M_WAITOK); 1175184610Salfred if (cdesc == NULL) { 1176184610Salfred return (USB_ERR_NOMEM); 1177184610Salfred } 1178194228Sthompsa err = usbd_req_get_desc(udev, mtx, NULL, cdesc, len, len, 0, 1179184610Salfred UDESC_CONFIG, index, 3); 1180184610Salfred if (err) { 1181184610Salfred free(cdesc, mtype); 1182184610Salfred return (err); 1183184610Salfred } 1184184610Salfred /* make sure that the device is not fooling us: */ 1185184610Salfred USETW(cdesc->wTotalLength, len); 1186184610Salfred 1187184610Salfred *ppcd = cdesc; 1188184610Salfred 1189184610Salfred return (0); /* success */ 1190184610Salfred} 1191184610Salfred 1192184610Salfred/*------------------------------------------------------------------------* 1193194228Sthompsa * usbd_req_get_device_desc 1194184610Salfred * 1195184610Salfred * Returns: 1196184610Salfred * 0: Success 1197184610Salfred * Else: Failure 1198184610Salfred *------------------------------------------------------------------------*/ 1199193045Sthompsausb_error_t 1200194228Sthompsausbd_req_get_device_desc(struct usb_device *udev, struct mtx *mtx, 1201192984Sthompsa struct usb_device_descriptor *d) 1202184610Salfred{ 1203184610Salfred DPRINTFN(4, "\n"); 1204194228Sthompsa return (usbd_req_get_desc(udev, mtx, NULL, d, sizeof(*d), 1205184610Salfred sizeof(*d), 0, UDESC_DEVICE, 0, 3)); 1206184610Salfred} 1207184610Salfred 1208184610Salfred/*------------------------------------------------------------------------* 1209194228Sthompsa * usbd_req_get_alt_interface_no 1210184610Salfred * 1211184610Salfred * Returns: 1212184610Salfred * 0: Success 1213184610Salfred * Else: Failure 1214184610Salfred *------------------------------------------------------------------------*/ 1215193045Sthompsausb_error_t 1216194228Sthompsausbd_req_get_alt_interface_no(struct usb_device *udev, struct mtx *mtx, 1217184610Salfred uint8_t *alt_iface_no, uint8_t iface_index) 1218184610Salfred{ 1219194228Sthompsa struct usb_interface *iface = usbd_get_iface(udev, iface_index); 1220192984Sthompsa struct usb_device_request req; 1221184610Salfred 1222195963Salfred if ((iface == NULL) || (iface->idesc == NULL)) 1223184610Salfred return (USB_ERR_INVAL); 1224195963Salfred 1225184610Salfred req.bmRequestType = UT_READ_INTERFACE; 1226184610Salfred req.bRequest = UR_GET_INTERFACE; 1227184610Salfred USETW(req.wValue, 0); 1228184610Salfred req.wIndex[0] = iface->idesc->bInterfaceNumber; 1229184610Salfred req.wIndex[1] = 0; 1230184610Salfred USETW(req.wLength, 1); 1231194228Sthompsa return (usbd_do_request(udev, mtx, &req, alt_iface_no)); 1232184610Salfred} 1233184610Salfred 1234184610Salfred/*------------------------------------------------------------------------* 1235194228Sthompsa * usbd_req_set_alt_interface_no 1236184610Salfred * 1237184610Salfred * Returns: 1238184610Salfred * 0: Success 1239184610Salfred * Else: Failure 1240184610Salfred *------------------------------------------------------------------------*/ 1241193045Sthompsausb_error_t 1242194228Sthompsausbd_req_set_alt_interface_no(struct usb_device *udev, struct mtx *mtx, 1243184610Salfred uint8_t iface_index, uint8_t alt_no) 1244184610Salfred{ 1245194228Sthompsa struct usb_interface *iface = usbd_get_iface(udev, iface_index); 1246192984Sthompsa struct usb_device_request req; 1247184610Salfred 1248195963Salfred if ((iface == NULL) || (iface->idesc == NULL)) 1249184610Salfred return (USB_ERR_INVAL); 1250195963Salfred 1251184610Salfred req.bmRequestType = UT_WRITE_INTERFACE; 1252184610Salfred req.bRequest = UR_SET_INTERFACE; 1253184610Salfred req.wValue[0] = alt_no; 1254184610Salfred req.wValue[1] = 0; 1255184610Salfred req.wIndex[0] = iface->idesc->bInterfaceNumber; 1256184610Salfred req.wIndex[1] = 0; 1257184610Salfred USETW(req.wLength, 0); 1258194228Sthompsa return (usbd_do_request(udev, mtx, &req, 0)); 1259184610Salfred} 1260184610Salfred 1261184610Salfred/*------------------------------------------------------------------------* 1262194228Sthompsa * usbd_req_get_device_status 1263184610Salfred * 1264184610Salfred * Returns: 1265184610Salfred * 0: Success 1266184610Salfred * Else: Failure 1267184610Salfred *------------------------------------------------------------------------*/ 1268193045Sthompsausb_error_t 1269194228Sthompsausbd_req_get_device_status(struct usb_device *udev, struct mtx *mtx, 1270192984Sthompsa struct usb_status *st) 1271184610Salfred{ 1272192984Sthompsa struct usb_device_request req; 1273184610Salfred 1274184610Salfred req.bmRequestType = UT_READ_DEVICE; 1275184610Salfred req.bRequest = UR_GET_STATUS; 1276184610Salfred USETW(req.wValue, 0); 1277184610Salfred USETW(req.wIndex, 0); 1278184610Salfred USETW(req.wLength, sizeof(*st)); 1279194228Sthompsa return (usbd_do_request(udev, mtx, &req, st)); 1280184610Salfred} 1281184610Salfred 1282184610Salfred/*------------------------------------------------------------------------* 1283194228Sthompsa * usbd_req_get_hub_descriptor 1284184610Salfred * 1285184610Salfred * Returns: 1286184610Salfred * 0: Success 1287184610Salfred * Else: Failure 1288184610Salfred *------------------------------------------------------------------------*/ 1289193045Sthompsausb_error_t 1290194228Sthompsausbd_req_get_hub_descriptor(struct usb_device *udev, struct mtx *mtx, 1291192984Sthompsa struct usb_hub_descriptor *hd, uint8_t nports) 1292184610Salfred{ 1293192984Sthompsa struct usb_device_request req; 1294184610Salfred uint16_t len = (nports + 7 + (8 * 8)) / 8; 1295184610Salfred 1296184610Salfred req.bmRequestType = UT_READ_CLASS_DEVICE; 1297184610Salfred req.bRequest = UR_GET_DESCRIPTOR; 1298184610Salfred USETW2(req.wValue, UDESC_HUB, 0); 1299184610Salfred USETW(req.wIndex, 0); 1300184610Salfred USETW(req.wLength, len); 1301194228Sthompsa return (usbd_do_request(udev, mtx, &req, hd)); 1302184610Salfred} 1303184610Salfred 1304184610Salfred/*------------------------------------------------------------------------* 1305213435Shselasky * usbd_req_get_ss_hub_descriptor 1306213435Shselasky * 1307213435Shselasky * Returns: 1308213435Shselasky * 0: Success 1309213435Shselasky * Else: Failure 1310213435Shselasky *------------------------------------------------------------------------*/ 1311213435Shselaskyusb_error_t 1312213435Shselaskyusbd_req_get_ss_hub_descriptor(struct usb_device *udev, struct mtx *mtx, 1313213435Shselasky struct usb_hub_ss_descriptor *hd, uint8_t nports) 1314213435Shselasky{ 1315213435Shselasky struct usb_device_request req; 1316213435Shselasky uint16_t len = sizeof(*hd) - 32 + 1 + ((nports + 7) / 8); 1317213435Shselasky 1318213435Shselasky req.bmRequestType = UT_READ_CLASS_DEVICE; 1319213435Shselasky req.bRequest = UR_GET_DESCRIPTOR; 1320213435Shselasky USETW2(req.wValue, UDESC_SS_HUB, 0); 1321213435Shselasky USETW(req.wIndex, 0); 1322213435Shselasky USETW(req.wLength, len); 1323213435Shselasky return (usbd_do_request(udev, mtx, &req, hd)); 1324213435Shselasky} 1325213435Shselasky 1326213435Shselasky/*------------------------------------------------------------------------* 1327194228Sthompsa * usbd_req_get_hub_status 1328184610Salfred * 1329184610Salfred * Returns: 1330184610Salfred * 0: Success 1331184610Salfred * Else: Failure 1332184610Salfred *------------------------------------------------------------------------*/ 1333193045Sthompsausb_error_t 1334194228Sthompsausbd_req_get_hub_status(struct usb_device *udev, struct mtx *mtx, 1335192984Sthompsa struct usb_hub_status *st) 1336184610Salfred{ 1337192984Sthompsa struct usb_device_request req; 1338184610Salfred 1339184610Salfred req.bmRequestType = UT_READ_CLASS_DEVICE; 1340184610Salfred req.bRequest = UR_GET_STATUS; 1341184610Salfred USETW(req.wValue, 0); 1342184610Salfred USETW(req.wIndex, 0); 1343192984Sthompsa USETW(req.wLength, sizeof(struct usb_hub_status)); 1344194228Sthompsa return (usbd_do_request(udev, mtx, &req, st)); 1345184610Salfred} 1346184610Salfred 1347184610Salfred/*------------------------------------------------------------------------* 1348194228Sthompsa * usbd_req_set_address 1349184610Salfred * 1350184610Salfred * This function is used to set the address for an USB device. After 1351184610Salfred * port reset the USB device will respond at address zero. 1352184610Salfred * 1353184610Salfred * Returns: 1354184610Salfred * 0: Success 1355184610Salfred * Else: Failure 1356184610Salfred *------------------------------------------------------------------------*/ 1357193045Sthompsausb_error_t 1358194228Sthompsausbd_req_set_address(struct usb_device *udev, struct mtx *mtx, uint16_t addr) 1359184610Salfred{ 1360192984Sthompsa struct usb_device_request req; 1361213435Shselasky usb_error_t err; 1362184610Salfred 1363184610Salfred DPRINTFN(6, "setting device address=%d\n", addr); 1364184610Salfred 1365184610Salfred req.bmRequestType = UT_WRITE_DEVICE; 1366184610Salfred req.bRequest = UR_SET_ADDRESS; 1367184610Salfred USETW(req.wValue, addr); 1368184610Salfred USETW(req.wIndex, 0); 1369184610Salfred USETW(req.wLength, 0); 1370184610Salfred 1371213435Shselasky err = USB_ERR_INVAL; 1372213435Shselasky 1373213435Shselasky /* check if USB controller handles set address */ 1374213435Shselasky if (udev->bus->methods->set_address != NULL) 1375213435Shselasky err = (udev->bus->methods->set_address) (udev, mtx, addr); 1376213435Shselasky 1377213435Shselasky if (err != USB_ERR_INVAL) 1378213435Shselasky goto done; 1379213435Shselasky 1380184610Salfred /* Setting the address should not take more than 1 second ! */ 1381213435Shselasky err = usbd_do_request_flags(udev, mtx, &req, NULL, 1382213435Shselasky USB_DELAY_STATUS_STAGE, NULL, 1000); 1383213435Shselasky 1384213435Shselaskydone: 1385213435Shselasky /* allow device time to set new address */ 1386213435Shselasky usb_pause_mtx(mtx, 1387213435Shselasky USB_MS_TO_TICKS(USB_SET_ADDRESS_SETTLE)); 1388213435Shselasky 1389213435Shselasky return (err); 1390184610Salfred} 1391184610Salfred 1392184610Salfred/*------------------------------------------------------------------------* 1393194228Sthompsa * usbd_req_get_port_status 1394184610Salfred * 1395184610Salfred * Returns: 1396184610Salfred * 0: Success 1397184610Salfred * Else: Failure 1398184610Salfred *------------------------------------------------------------------------*/ 1399193045Sthompsausb_error_t 1400194228Sthompsausbd_req_get_port_status(struct usb_device *udev, struct mtx *mtx, 1401192984Sthompsa struct usb_port_status *ps, uint8_t port) 1402184610Salfred{ 1403192984Sthompsa struct usb_device_request req; 1404184610Salfred 1405184610Salfred req.bmRequestType = UT_READ_CLASS_OTHER; 1406184610Salfred req.bRequest = UR_GET_STATUS; 1407184610Salfred USETW(req.wValue, 0); 1408184610Salfred req.wIndex[0] = port; 1409184610Salfred req.wIndex[1] = 0; 1410184610Salfred USETW(req.wLength, sizeof *ps); 1411194228Sthompsa return (usbd_do_request(udev, mtx, &req, ps)); 1412184610Salfred} 1413184610Salfred 1414184610Salfred/*------------------------------------------------------------------------* 1415194228Sthompsa * usbd_req_clear_hub_feature 1416184610Salfred * 1417184610Salfred * Returns: 1418184610Salfred * 0: Success 1419184610Salfred * Else: Failure 1420184610Salfred *------------------------------------------------------------------------*/ 1421193045Sthompsausb_error_t 1422194228Sthompsausbd_req_clear_hub_feature(struct usb_device *udev, struct mtx *mtx, 1423184610Salfred uint16_t sel) 1424184610Salfred{ 1425192984Sthompsa struct usb_device_request req; 1426184610Salfred 1427184610Salfred req.bmRequestType = UT_WRITE_CLASS_DEVICE; 1428184610Salfred req.bRequest = UR_CLEAR_FEATURE; 1429184610Salfred USETW(req.wValue, sel); 1430184610Salfred USETW(req.wIndex, 0); 1431184610Salfred USETW(req.wLength, 0); 1432194228Sthompsa return (usbd_do_request(udev, mtx, &req, 0)); 1433184610Salfred} 1434184610Salfred 1435184610Salfred/*------------------------------------------------------------------------* 1436194228Sthompsa * usbd_req_set_hub_feature 1437184610Salfred * 1438184610Salfred * Returns: 1439184610Salfred * 0: Success 1440184610Salfred * Else: Failure 1441184610Salfred *------------------------------------------------------------------------*/ 1442193045Sthompsausb_error_t 1443194228Sthompsausbd_req_set_hub_feature(struct usb_device *udev, struct mtx *mtx, 1444184610Salfred uint16_t sel) 1445184610Salfred{ 1446192984Sthompsa struct usb_device_request req; 1447184610Salfred 1448184610Salfred req.bmRequestType = UT_WRITE_CLASS_DEVICE; 1449184610Salfred req.bRequest = UR_SET_FEATURE; 1450184610Salfred USETW(req.wValue, sel); 1451184610Salfred USETW(req.wIndex, 0); 1452184610Salfred USETW(req.wLength, 0); 1453194228Sthompsa return (usbd_do_request(udev, mtx, &req, 0)); 1454184610Salfred} 1455184610Salfred 1456184610Salfred/*------------------------------------------------------------------------* 1457213435Shselasky * usbd_req_set_hub_u1_timeout 1458213435Shselasky * 1459213435Shselasky * Returns: 1460213435Shselasky * 0: Success 1461213435Shselasky * Else: Failure 1462213435Shselasky *------------------------------------------------------------------------*/ 1463213435Shselaskyusb_error_t 1464213435Shselaskyusbd_req_set_hub_u1_timeout(struct usb_device *udev, struct mtx *mtx, 1465213435Shselasky uint8_t port, uint8_t timeout) 1466213435Shselasky{ 1467213435Shselasky struct usb_device_request req; 1468213435Shselasky 1469213435Shselasky req.bmRequestType = UT_WRITE_CLASS_OTHER; 1470213435Shselasky req.bRequest = UR_SET_FEATURE; 1471213435Shselasky USETW(req.wValue, UHF_PORT_U1_TIMEOUT); 1472213435Shselasky req.wIndex[0] = port; 1473213435Shselasky req.wIndex[1] = timeout; 1474213435Shselasky USETW(req.wLength, 0); 1475213435Shselasky return (usbd_do_request(udev, mtx, &req, 0)); 1476213435Shselasky} 1477213435Shselasky 1478213435Shselasky/*------------------------------------------------------------------------* 1479213435Shselasky * usbd_req_set_hub_u2_timeout 1480213435Shselasky * 1481213435Shselasky * Returns: 1482213435Shselasky * 0: Success 1483213435Shselasky * Else: Failure 1484213435Shselasky *------------------------------------------------------------------------*/ 1485213435Shselaskyusb_error_t 1486213435Shselaskyusbd_req_set_hub_u2_timeout(struct usb_device *udev, struct mtx *mtx, 1487213435Shselasky uint8_t port, uint8_t timeout) 1488213435Shselasky{ 1489213435Shselasky struct usb_device_request req; 1490213435Shselasky 1491213435Shselasky req.bmRequestType = UT_WRITE_CLASS_OTHER; 1492213435Shselasky req.bRequest = UR_SET_FEATURE; 1493213435Shselasky USETW(req.wValue, UHF_PORT_U2_TIMEOUT); 1494213435Shselasky req.wIndex[0] = port; 1495213435Shselasky req.wIndex[1] = timeout; 1496213435Shselasky USETW(req.wLength, 0); 1497213435Shselasky return (usbd_do_request(udev, mtx, &req, 0)); 1498213435Shselasky} 1499213435Shselasky 1500213435Shselasky/*------------------------------------------------------------------------* 1501213435Shselasky * usbd_req_set_hub_depth 1502213435Shselasky * 1503213435Shselasky * Returns: 1504213435Shselasky * 0: Success 1505213435Shselasky * Else: Failure 1506213435Shselasky *------------------------------------------------------------------------*/ 1507213435Shselaskyusb_error_t 1508213435Shselaskyusbd_req_set_hub_depth(struct usb_device *udev, struct mtx *mtx, 1509213435Shselasky uint16_t depth) 1510213435Shselasky{ 1511213435Shselasky struct usb_device_request req; 1512213435Shselasky 1513213435Shselasky req.bmRequestType = UT_WRITE_CLASS_DEVICE; 1514213435Shselasky req.bRequest = UR_SET_HUB_DEPTH; 1515213435Shselasky USETW(req.wValue, depth); 1516213435Shselasky USETW(req.wIndex, 0); 1517213435Shselasky USETW(req.wLength, 0); 1518213435Shselasky return (usbd_do_request(udev, mtx, &req, 0)); 1519213435Shselasky} 1520213435Shselasky 1521213435Shselasky/*------------------------------------------------------------------------* 1522194228Sthompsa * usbd_req_clear_port_feature 1523184610Salfred * 1524184610Salfred * Returns: 1525184610Salfred * 0: Success 1526184610Salfred * Else: Failure 1527184610Salfred *------------------------------------------------------------------------*/ 1528193045Sthompsausb_error_t 1529194228Sthompsausbd_req_clear_port_feature(struct usb_device *udev, struct mtx *mtx, 1530184610Salfred uint8_t port, uint16_t sel) 1531184610Salfred{ 1532192984Sthompsa struct usb_device_request req; 1533184610Salfred 1534184610Salfred req.bmRequestType = UT_WRITE_CLASS_OTHER; 1535184610Salfred req.bRequest = UR_CLEAR_FEATURE; 1536184610Salfred USETW(req.wValue, sel); 1537184610Salfred req.wIndex[0] = port; 1538184610Salfred req.wIndex[1] = 0; 1539184610Salfred USETW(req.wLength, 0); 1540194228Sthompsa return (usbd_do_request(udev, mtx, &req, 0)); 1541184610Salfred} 1542184610Salfred 1543184610Salfred/*------------------------------------------------------------------------* 1544194228Sthompsa * usbd_req_set_port_feature 1545184610Salfred * 1546184610Salfred * Returns: 1547184610Salfred * 0: Success 1548184610Salfred * Else: Failure 1549184610Salfred *------------------------------------------------------------------------*/ 1550193045Sthompsausb_error_t 1551194228Sthompsausbd_req_set_port_feature(struct usb_device *udev, struct mtx *mtx, 1552184610Salfred uint8_t port, uint16_t sel) 1553184610Salfred{ 1554192984Sthompsa struct usb_device_request req; 1555184610Salfred 1556184610Salfred req.bmRequestType = UT_WRITE_CLASS_OTHER; 1557184610Salfred req.bRequest = UR_SET_FEATURE; 1558184610Salfred USETW(req.wValue, sel); 1559184610Salfred req.wIndex[0] = port; 1560184610Salfred req.wIndex[1] = 0; 1561184610Salfred USETW(req.wLength, 0); 1562194228Sthompsa return (usbd_do_request(udev, mtx, &req, 0)); 1563184610Salfred} 1564184610Salfred 1565184610Salfred/*------------------------------------------------------------------------* 1566194228Sthompsa * usbd_req_set_protocol 1567184610Salfred * 1568184610Salfred * Returns: 1569184610Salfred * 0: Success 1570184610Salfred * Else: Failure 1571184610Salfred *------------------------------------------------------------------------*/ 1572193045Sthompsausb_error_t 1573194228Sthompsausbd_req_set_protocol(struct usb_device *udev, struct mtx *mtx, 1574184610Salfred uint8_t iface_index, uint16_t report) 1575184610Salfred{ 1576194228Sthompsa struct usb_interface *iface = usbd_get_iface(udev, iface_index); 1577192984Sthompsa struct usb_device_request req; 1578184610Salfred 1579184610Salfred if ((iface == NULL) || (iface->idesc == NULL)) { 1580184610Salfred return (USB_ERR_INVAL); 1581184610Salfred } 1582184610Salfred DPRINTFN(5, "iface=%p, report=%d, endpt=%d\n", 1583184610Salfred iface, report, iface->idesc->bInterfaceNumber); 1584184610Salfred 1585184610Salfred req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 1586184610Salfred req.bRequest = UR_SET_PROTOCOL; 1587184610Salfred USETW(req.wValue, report); 1588184610Salfred req.wIndex[0] = iface->idesc->bInterfaceNumber; 1589184610Salfred req.wIndex[1] = 0; 1590184610Salfred USETW(req.wLength, 0); 1591194228Sthompsa return (usbd_do_request(udev, mtx, &req, 0)); 1592184610Salfred} 1593184610Salfred 1594184610Salfred/*------------------------------------------------------------------------* 1595194228Sthompsa * usbd_req_set_report 1596184610Salfred * 1597184610Salfred * Returns: 1598184610Salfred * 0: Success 1599184610Salfred * Else: Failure 1600184610Salfred *------------------------------------------------------------------------*/ 1601193045Sthompsausb_error_t 1602194228Sthompsausbd_req_set_report(struct usb_device *udev, struct mtx *mtx, void *data, uint16_t len, 1603184610Salfred uint8_t iface_index, uint8_t type, uint8_t id) 1604184610Salfred{ 1605194228Sthompsa struct usb_interface *iface = usbd_get_iface(udev, iface_index); 1606192984Sthompsa struct usb_device_request req; 1607184610Salfred 1608184610Salfred if ((iface == NULL) || (iface->idesc == NULL)) { 1609184610Salfred return (USB_ERR_INVAL); 1610184610Salfred } 1611184610Salfred DPRINTFN(5, "len=%d\n", len); 1612184610Salfred 1613184610Salfred req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 1614184610Salfred req.bRequest = UR_SET_REPORT; 1615184610Salfred USETW2(req.wValue, type, id); 1616184610Salfred req.wIndex[0] = iface->idesc->bInterfaceNumber; 1617184610Salfred req.wIndex[1] = 0; 1618184610Salfred USETW(req.wLength, len); 1619194228Sthompsa return (usbd_do_request(udev, mtx, &req, data)); 1620184610Salfred} 1621184610Salfred 1622184610Salfred/*------------------------------------------------------------------------* 1623194228Sthompsa * usbd_req_get_report 1624184610Salfred * 1625184610Salfred * Returns: 1626184610Salfred * 0: Success 1627184610Salfred * Else: Failure 1628184610Salfred *------------------------------------------------------------------------*/ 1629193045Sthompsausb_error_t 1630194228Sthompsausbd_req_get_report(struct usb_device *udev, struct mtx *mtx, void *data, 1631184610Salfred uint16_t len, uint8_t iface_index, uint8_t type, uint8_t id) 1632184610Salfred{ 1633194228Sthompsa struct usb_interface *iface = usbd_get_iface(udev, iface_index); 1634192984Sthompsa struct usb_device_request req; 1635184610Salfred 1636184610Salfred if ((iface == NULL) || (iface->idesc == NULL) || (id == 0)) { 1637184610Salfred return (USB_ERR_INVAL); 1638184610Salfred } 1639184610Salfred DPRINTFN(5, "len=%d\n", len); 1640184610Salfred 1641184610Salfred req.bmRequestType = UT_READ_CLASS_INTERFACE; 1642184610Salfred req.bRequest = UR_GET_REPORT; 1643184610Salfred USETW2(req.wValue, type, id); 1644184610Salfred req.wIndex[0] = iface->idesc->bInterfaceNumber; 1645184610Salfred req.wIndex[1] = 0; 1646184610Salfred USETW(req.wLength, len); 1647194228Sthompsa return (usbd_do_request(udev, mtx, &req, data)); 1648184610Salfred} 1649184610Salfred 1650184610Salfred/*------------------------------------------------------------------------* 1651194228Sthompsa * usbd_req_set_idle 1652184610Salfred * 1653184610Salfred * Returns: 1654184610Salfred * 0: Success 1655184610Salfred * Else: Failure 1656184610Salfred *------------------------------------------------------------------------*/ 1657193045Sthompsausb_error_t 1658194228Sthompsausbd_req_set_idle(struct usb_device *udev, struct mtx *mtx, 1659184610Salfred uint8_t iface_index, uint8_t duration, uint8_t id) 1660184610Salfred{ 1661194228Sthompsa struct usb_interface *iface = usbd_get_iface(udev, iface_index); 1662192984Sthompsa struct usb_device_request req; 1663184610Salfred 1664184610Salfred if ((iface == NULL) || (iface->idesc == NULL)) { 1665184610Salfred return (USB_ERR_INVAL); 1666184610Salfred } 1667184610Salfred DPRINTFN(5, "%d %d\n", duration, id); 1668184610Salfred 1669184610Salfred req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 1670184610Salfred req.bRequest = UR_SET_IDLE; 1671184610Salfred USETW2(req.wValue, duration, id); 1672184610Salfred req.wIndex[0] = iface->idesc->bInterfaceNumber; 1673184610Salfred req.wIndex[1] = 0; 1674184610Salfred USETW(req.wLength, 0); 1675194228Sthompsa return (usbd_do_request(udev, mtx, &req, 0)); 1676184610Salfred} 1677184610Salfred 1678184610Salfred/*------------------------------------------------------------------------* 1679194228Sthompsa * usbd_req_get_report_descriptor 1680184610Salfred * 1681184610Salfred * Returns: 1682184610Salfred * 0: Success 1683184610Salfred * Else: Failure 1684184610Salfred *------------------------------------------------------------------------*/ 1685193045Sthompsausb_error_t 1686194228Sthompsausbd_req_get_report_descriptor(struct usb_device *udev, struct mtx *mtx, 1687184610Salfred void *d, uint16_t size, uint8_t iface_index) 1688184610Salfred{ 1689194228Sthompsa struct usb_interface *iface = usbd_get_iface(udev, iface_index); 1690192984Sthompsa struct usb_device_request req; 1691184610Salfred 1692184610Salfred if ((iface == NULL) || (iface->idesc == NULL)) { 1693184610Salfred return (USB_ERR_INVAL); 1694184610Salfred } 1695184610Salfred req.bmRequestType = UT_READ_INTERFACE; 1696184610Salfred req.bRequest = UR_GET_DESCRIPTOR; 1697184610Salfred USETW2(req.wValue, UDESC_REPORT, 0); /* report id should be 0 */ 1698184610Salfred req.wIndex[0] = iface->idesc->bInterfaceNumber; 1699184610Salfred req.wIndex[1] = 0; 1700184610Salfred USETW(req.wLength, size); 1701194228Sthompsa return (usbd_do_request(udev, mtx, &req, d)); 1702184610Salfred} 1703184610Salfred 1704184610Salfred/*------------------------------------------------------------------------* 1705194228Sthompsa * usbd_req_set_config 1706184610Salfred * 1707184610Salfred * This function is used to select the current configuration number in 1708184610Salfred * both USB device side mode and USB host side mode. When setting the 1709184610Salfred * configuration the function of the interfaces can change. 1710184610Salfred * 1711184610Salfred * Returns: 1712184610Salfred * 0: Success 1713184610Salfred * Else: Failure 1714184610Salfred *------------------------------------------------------------------------*/ 1715193045Sthompsausb_error_t 1716194228Sthompsausbd_req_set_config(struct usb_device *udev, struct mtx *mtx, uint8_t conf) 1717184610Salfred{ 1718192984Sthompsa struct usb_device_request req; 1719184610Salfred 1720184610Salfred DPRINTF("setting config %d\n", conf); 1721184610Salfred 1722184610Salfred /* do "set configuration" request */ 1723184610Salfred 1724184610Salfred req.bmRequestType = UT_WRITE_DEVICE; 1725184610Salfred req.bRequest = UR_SET_CONFIG; 1726184610Salfred req.wValue[0] = conf; 1727184610Salfred req.wValue[1] = 0; 1728184610Salfred USETW(req.wIndex, 0); 1729184610Salfred USETW(req.wLength, 0); 1730194228Sthompsa return (usbd_do_request(udev, mtx, &req, 0)); 1731184610Salfred} 1732184610Salfred 1733184610Salfred/*------------------------------------------------------------------------* 1734194228Sthompsa * usbd_req_get_config 1735184610Salfred * 1736184610Salfred * Returns: 1737184610Salfred * 0: Success 1738184610Salfred * Else: Failure 1739184610Salfred *------------------------------------------------------------------------*/ 1740193045Sthompsausb_error_t 1741194228Sthompsausbd_req_get_config(struct usb_device *udev, struct mtx *mtx, uint8_t *pconf) 1742184610Salfred{ 1743192984Sthompsa struct usb_device_request req; 1744184610Salfred 1745184610Salfred req.bmRequestType = UT_READ_DEVICE; 1746184610Salfred req.bRequest = UR_GET_CONFIG; 1747184610Salfred USETW(req.wValue, 0); 1748184610Salfred USETW(req.wIndex, 0); 1749184610Salfred USETW(req.wLength, 1); 1750194228Sthompsa return (usbd_do_request(udev, mtx, &req, pconf)); 1751184610Salfred} 1752184610Salfred 1753184610Salfred/*------------------------------------------------------------------------* 1754213435Shselasky * usbd_setup_device_desc 1755213435Shselasky *------------------------------------------------------------------------*/ 1756213435Shselaskyusb_error_t 1757213435Shselaskyusbd_setup_device_desc(struct usb_device *udev, struct mtx *mtx) 1758213435Shselasky{ 1759213435Shselasky usb_error_t err; 1760213435Shselasky 1761213435Shselasky /* 1762213435Shselasky * Get the first 8 bytes of the device descriptor ! 1763213435Shselasky * 1764213435Shselasky * NOTE: "usbd_do_request()" will check the device descriptor 1765213435Shselasky * next time we do a request to see if the maximum packet size 1766213435Shselasky * changed! The 8 first bytes of the device descriptor 1767213435Shselasky * contains the maximum packet size to use on control endpoint 1768213435Shselasky * 0. If this value is different from "USB_MAX_IPACKET" a new 1769213435Shselasky * USB control request will be setup! 1770213435Shselasky */ 1771213435Shselasky switch (udev->speed) { 1772213435Shselasky case USB_SPEED_FULL: 1773213435Shselasky case USB_SPEED_LOW: 1774213435Shselasky err = usbd_req_get_desc(udev, mtx, NULL, &udev->ddesc, 1775213435Shselasky USB_MAX_IPACKET, USB_MAX_IPACKET, 0, UDESC_DEVICE, 0, 0); 1776213435Shselasky if (err != 0) { 1777213435Shselasky DPRINTFN(0, "getting device descriptor " 1778213435Shselasky "at addr %d failed, %s\n", udev->address, 1779213435Shselasky usbd_errstr(err)); 1780213435Shselasky return (err); 1781213435Shselasky } 1782213435Shselasky break; 1783213435Shselasky default: 1784213435Shselasky DPRINTF("Minimum MaxPacketSize is large enough " 1785213435Shselasky "to hold the complete device descriptor\n"); 1786213435Shselasky break; 1787213435Shselasky } 1788213435Shselasky 1789213435Shselasky /* get the full device descriptor */ 1790213435Shselasky err = usbd_req_get_device_desc(udev, mtx, &udev->ddesc); 1791213435Shselasky 1792213435Shselasky /* try one more time, if error */ 1793213435Shselasky if (err) 1794213435Shselasky err = usbd_req_get_device_desc(udev, mtx, &udev->ddesc); 1795213435Shselasky 1796213435Shselasky if (err) { 1797213435Shselasky DPRINTF("addr=%d, getting full desc failed\n", 1798213435Shselasky udev->address); 1799213435Shselasky return (err); 1800213435Shselasky } 1801213435Shselasky 1802213435Shselasky DPRINTF("adding unit addr=%d, rev=%02x, class=%d, " 1803213435Shselasky "subclass=%d, protocol=%d, maxpacket=%d, len=%d, speed=%d\n", 1804213435Shselasky udev->address, UGETW(udev->ddesc.bcdUSB), 1805213435Shselasky udev->ddesc.bDeviceClass, 1806213435Shselasky udev->ddesc.bDeviceSubClass, 1807213435Shselasky udev->ddesc.bDeviceProtocol, 1808213435Shselasky udev->ddesc.bMaxPacketSize, 1809213435Shselasky udev->ddesc.bLength, 1810213435Shselasky udev->speed); 1811213435Shselasky 1812213435Shselasky return (err); 1813213435Shselasky} 1814213435Shselasky 1815213435Shselasky/*------------------------------------------------------------------------* 1816194228Sthompsa * usbd_req_re_enumerate 1817184610Salfred * 1818185087Salfred * NOTE: After this function returns the hardware is in the 1819185087Salfred * unconfigured state! The application is responsible for setting a 1820185087Salfred * new configuration. 1821185087Salfred * 1822184610Salfred * Returns: 1823184610Salfred * 0: Success 1824184610Salfred * Else: Failure 1825184610Salfred *------------------------------------------------------------------------*/ 1826193045Sthompsausb_error_t 1827194228Sthompsausbd_req_re_enumerate(struct usb_device *udev, struct mtx *mtx) 1828184610Salfred{ 1829192984Sthompsa struct usb_device *parent_hub; 1830193045Sthompsa usb_error_t err; 1831184610Salfred uint8_t old_addr; 1832186730Salfred uint8_t do_retry = 1; 1833184610Salfred 1834192499Sthompsa if (udev->flags.usb_mode != USB_MODE_HOST) { 1835185290Salfred return (USB_ERR_INVAL); 1836185290Salfred } 1837184610Salfred old_addr = udev->address; 1838184610Salfred parent_hub = udev->parent_hub; 1839184610Salfred if (parent_hub == NULL) { 1840185290Salfred return (USB_ERR_INVAL); 1841184610Salfred } 1842186730Salfredretry: 1843194228Sthompsa err = usbd_req_reset_port(parent_hub, mtx, udev->port_no); 1844184610Salfred if (err) { 1845190739Sthompsa DPRINTFN(0, "addr=%d, port reset failed, %s\n", 1846194228Sthompsa old_addr, usbd_errstr(err)); 1847184610Salfred goto done; 1848184610Salfred } 1849213435Shselasky 1850184610Salfred /* 1851184610Salfred * After that the port has been reset our device should be at 1852184610Salfred * address zero: 1853184610Salfred */ 1854184610Salfred udev->address = USB_START_ADDR; 1855184610Salfred 1856185290Salfred /* reset "bMaxPacketSize" */ 1857185290Salfred udev->ddesc.bMaxPacketSize = USB_MAX_IPACKET; 1858185290Salfred 1859213435Shselasky /* reset USB state */ 1860213435Shselasky usb_set_device_state(udev, USB_STATE_POWERED); 1861213435Shselasky 1862184610Salfred /* 1863184610Salfred * Restore device address: 1864184610Salfred */ 1865194228Sthompsa err = usbd_req_set_address(udev, mtx, old_addr); 1866184610Salfred if (err) { 1867184610Salfred /* XXX ignore any errors! */ 1868190739Sthompsa DPRINTFN(0, "addr=%d, set address failed! (%s, ignored)\n", 1869194228Sthompsa old_addr, usbd_errstr(err)); 1870184610Salfred } 1871213435Shselasky /* 1872213435Shselasky * Restore device address, if the controller driver did not 1873213435Shselasky * set a new one: 1874213435Shselasky */ 1875213435Shselasky if (udev->address == USB_START_ADDR) 1876213435Shselasky udev->address = old_addr; 1877184610Salfred 1878213435Shselasky /* setup the device descriptor and the initial "wMaxPacketSize" */ 1879213435Shselasky err = usbd_setup_device_desc(udev, mtx); 1880184610Salfred 1881184610Salfreddone: 1882186730Salfred if (err && do_retry) { 1883186730Salfred /* give the USB firmware some time to load */ 1884194228Sthompsa usb_pause_mtx(mtx, hz / 2); 1885186730Salfred /* no more retries after this retry */ 1886186730Salfred do_retry = 0; 1887186730Salfred /* try again */ 1888186730Salfred goto retry; 1889186730Salfred } 1890184610Salfred /* restore address */ 1891213435Shselasky if (udev->address == USB_START_ADDR) 1892213435Shselasky udev->address = old_addr; 1893213435Shselasky /* update state, if successful */ 1894213435Shselasky if (err == 0) 1895213435Shselasky usb_set_device_state(udev, USB_STATE_ADDRESSED); 1896184610Salfred return (err); 1897184610Salfred} 1898186730Salfred 1899186730Salfred/*------------------------------------------------------------------------* 1900194228Sthompsa * usbd_req_clear_device_feature 1901186730Salfred * 1902186730Salfred * Returns: 1903186730Salfred * 0: Success 1904186730Salfred * Else: Failure 1905186730Salfred *------------------------------------------------------------------------*/ 1906193045Sthompsausb_error_t 1907194228Sthompsausbd_req_clear_device_feature(struct usb_device *udev, struct mtx *mtx, 1908186730Salfred uint16_t sel) 1909186730Salfred{ 1910192984Sthompsa struct usb_device_request req; 1911186730Salfred 1912186730Salfred req.bmRequestType = UT_WRITE_DEVICE; 1913186730Salfred req.bRequest = UR_CLEAR_FEATURE; 1914186730Salfred USETW(req.wValue, sel); 1915186730Salfred USETW(req.wIndex, 0); 1916186730Salfred USETW(req.wLength, 0); 1917194228Sthompsa return (usbd_do_request(udev, mtx, &req, 0)); 1918186730Salfred} 1919186730Salfred 1920186730Salfred/*------------------------------------------------------------------------* 1921194228Sthompsa * usbd_req_set_device_feature 1922186730Salfred * 1923186730Salfred * Returns: 1924186730Salfred * 0: Success 1925186730Salfred * Else: Failure 1926186730Salfred *------------------------------------------------------------------------*/ 1927193045Sthompsausb_error_t 1928194228Sthompsausbd_req_set_device_feature(struct usb_device *udev, struct mtx *mtx, 1929186730Salfred uint16_t sel) 1930186730Salfred{ 1931192984Sthompsa struct usb_device_request req; 1932186730Salfred 1933186730Salfred req.bmRequestType = UT_WRITE_DEVICE; 1934186730Salfred req.bRequest = UR_SET_FEATURE; 1935186730Salfred USETW(req.wValue, sel); 1936186730Salfred USETW(req.wIndex, 0); 1937186730Salfred USETW(req.wLength, 0); 1938194228Sthompsa return (usbd_do_request(udev, mtx, &req, 0)); 1939186730Salfred} 1940