usb_util.c revision 192984
1184610Salfred/* $FreeBSD: head/sys/dev/usb/usb_util.c 192984 2009-05-28 17:36:36Z thompsa $ */ 2184610Salfred/*- 3184610Salfred * Copyright (c) 2008 Hans Petter Selasky. All rights reserved. 4184610Salfred * 5184610Salfred * Redistribution and use in source and binary forms, with or without 6184610Salfred * modification, are permitted provided that the following conditions 7184610Salfred * are met: 8184610Salfred * 1. Redistributions of source code must retain the above copyright 9184610Salfred * notice, this list of conditions and the following disclaimer. 10184610Salfred * 2. Redistributions in binary form must reproduce the above copyright 11184610Salfred * notice, this list of conditions and the following disclaimer in the 12184610Salfred * documentation and/or other materials provided with the distribution. 13184610Salfred * 14184610Salfred * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15184610Salfred * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16184610Salfred * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17184610Salfred * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18184610Salfred * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19184610Salfred * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20184610Salfred * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21184610Salfred * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22184610Salfred * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23184610Salfred * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24184610Salfred * SUCH DAMAGE. 25184610Salfred */ 26184610Salfred 27188942Sthompsa#include <dev/usb/usb_mfunc.h> 28188942Sthompsa#include <dev/usb/usb_error.h> 29188942Sthompsa#include <dev/usb/usb.h> 30184610Salfred 31188942Sthompsa#include <dev/usb/usb_core.h> 32188942Sthompsa#include <dev/usb/usb_util.h> 33188942Sthompsa#include <dev/usb/usb_process.h> 34188942Sthompsa#include <dev/usb/usb_device.h> 35188942Sthompsa#include <dev/usb/usb_request.h> 36188942Sthompsa#include <dev/usb/usb_busdma.h> 37184610Salfred 38188942Sthompsa#include <dev/usb/usb_controller.h> 39188942Sthompsa#include <dev/usb/usb_bus.h> 40184610Salfred 41184610Salfred/* function prototypes */ 42190180Sthompsa#if (USB_HAVE_CONDVAR == 0) 43184610Salfredstatic int usb2_msleep(void *chan, struct mtx *mtx, int priority, const char *wmesg, int timo); 44184610Salfred 45184610Salfred#endif 46184610Salfred 47184610Salfred/*------------------------------------------------------------------------* 48184610Salfred * device_delete_all_children - delete all children of a device 49184610Salfred *------------------------------------------------------------------------*/ 50190180Sthompsa#ifndef device_delete_all_children 51184610Salfredint 52184610Salfreddevice_delete_all_children(device_t dev) 53184610Salfred{ 54184610Salfred device_t *devlist; 55184610Salfred int devcount; 56184610Salfred int error; 57184610Salfred 58184610Salfred error = device_get_children(dev, &devlist, &devcount); 59184610Salfred if (error == 0) { 60184610Salfred while (devcount-- > 0) { 61184610Salfred error = device_delete_child(dev, devlist[devcount]); 62184610Salfred if (error) { 63184610Salfred break; 64184610Salfred } 65184610Salfred } 66184610Salfred free(devlist, M_TEMP); 67184610Salfred } 68184610Salfred return (error); 69184610Salfred} 70190180Sthompsa#endif 71184610Salfred 72184610Salfred/*------------------------------------------------------------------------* 73184610Salfred * device_set_usb2_desc 74184610Salfred * 75184610Salfred * This function can be called at probe or attach to set the USB 76184610Salfred * device supplied textual description for the given device. 77184610Salfred *------------------------------------------------------------------------*/ 78184610Salfredvoid 79184610Salfreddevice_set_usb2_desc(device_t dev) 80184610Salfred{ 81192984Sthompsa struct usb_attach_arg *uaa; 82192984Sthompsa struct usb_device *udev; 83192984Sthompsa struct usb_interface *iface; 84184610Salfred char *temp_p; 85184610Salfred usb2_error_t err; 86184610Salfred 87184610Salfred if (dev == NULL) { 88184610Salfred /* should not happen */ 89184610Salfred return; 90184610Salfred } 91184610Salfred uaa = device_get_ivars(dev); 92184610Salfred if (uaa == NULL) { 93185087Salfred /* can happen if called at the wrong time */ 94184610Salfred return; 95184610Salfred } 96184610Salfred udev = uaa->device; 97184610Salfred iface = uaa->iface; 98184610Salfred 99184610Salfred if ((iface == NULL) || 100184610Salfred (iface->idesc == NULL) || 101184610Salfred (iface->idesc->iInterface == 0)) { 102184610Salfred err = USB_ERR_INVAL; 103184610Salfred } else { 104184610Salfred err = 0; 105184610Salfred } 106184610Salfred 107184610Salfred temp_p = (char *)udev->bus->scratch[0].data; 108184610Salfred 109184610Salfred if (!err) { 110184610Salfred /* try to get the interface string ! */ 111184610Salfred err = usb2_req_get_string_any 112184610Salfred (udev, NULL, temp_p, 113184610Salfred sizeof(udev->bus->scratch), iface->idesc->iInterface); 114184610Salfred } 115184610Salfred if (err) { 116184610Salfred /* use default description */ 117184610Salfred usb2_devinfo(udev, temp_p, 118184610Salfred sizeof(udev->bus->scratch)); 119184610Salfred } 120184610Salfred device_set_desc_copy(dev, temp_p); 121184610Salfred device_printf(dev, "<%s> on %s\n", temp_p, 122184610Salfred device_get_nameunit(udev->bus->bdev)); 123184610Salfred} 124184610Salfred 125184610Salfred/*------------------------------------------------------------------------* 126184610Salfred * usb2_pause_mtx - factored out code 127184610Salfred * 128188411Sthompsa * This function will delay the code by the passed number of system 129188411Sthompsa * ticks. The passed mutex "mtx" will be dropped while waiting, if 130188411Sthompsa * "mtx" is not NULL. 131184610Salfred *------------------------------------------------------------------------*/ 132184610Salfredvoid 133188411Sthompsausb2_pause_mtx(struct mtx *mtx, int _ticks) 134184610Salfred{ 135188411Sthompsa if (mtx != NULL) 136188411Sthompsa mtx_unlock(mtx); 137188411Sthompsa 138184610Salfred if (cold) { 139188411Sthompsa /* convert to milliseconds */ 140188411Sthompsa _ticks = (_ticks * 1000) / hz; 141188411Sthompsa /* convert to microseconds, rounded up */ 142188411Sthompsa _ticks = (_ticks + 1) * 1000; 143188411Sthompsa DELAY(_ticks); 144184610Salfred 145184610Salfred } else { 146184610Salfred 147184610Salfred /* 148184610Salfred * Add one to the number of ticks so that we don't return 149184610Salfred * too early! 150184610Salfred */ 151188411Sthompsa _ticks++; 152184610Salfred 153188411Sthompsa if (pause("USBWAIT", _ticks)) { 154184610Salfred /* ignore */ 155184610Salfred } 156184610Salfred } 157188411Sthompsa if (mtx != NULL) 158188411Sthompsa mtx_lock(mtx); 159184610Salfred} 160184610Salfred 161184610Salfred/*------------------------------------------------------------------------* 162184610Salfred * usb2_printBCD 163184610Salfred * 164184610Salfred * This function will print the version number "bcd" to the string 165184610Salfred * pointed to by "p" having a maximum length of "p_len" bytes 166184610Salfred * including the terminating zero. 167184610Salfred *------------------------------------------------------------------------*/ 168184610Salfredvoid 169184610Salfredusb2_printBCD(char *p, uint16_t p_len, uint16_t bcd) 170184610Salfred{ 171184610Salfred if (snprintf(p, p_len, "%x.%02x", bcd >> 8, bcd & 0xff)) { 172184610Salfred /* ignore any errors */ 173184610Salfred } 174184610Salfred} 175184610Salfred 176184610Salfred/*------------------------------------------------------------------------* 177184610Salfred * usb2_trim_spaces 178184610Salfred * 179184610Salfred * This function removes spaces at the beginning and the end of the string 180184610Salfred * pointed to by the "p" argument. 181184610Salfred *------------------------------------------------------------------------*/ 182184610Salfredvoid 183184610Salfredusb2_trim_spaces(char *p) 184184610Salfred{ 185184610Salfred char *q; 186184610Salfred char *e; 187184610Salfred 188184610Salfred if (p == NULL) 189184610Salfred return; 190184610Salfred q = e = p; 191184610Salfred while (*q == ' ') /* skip leading spaces */ 192184610Salfred q++; 193184610Salfred while ((*p = *q++)) /* copy string */ 194184610Salfred if (*p++ != ' ') /* remember last non-space */ 195184610Salfred e = p; 196184610Salfred *e = 0; /* kill trailing spaces */ 197184610Salfred} 198184610Salfred 199184610Salfred/*------------------------------------------------------------------------* 200184610Salfred * usb2_make_str_desc - convert an ASCII string into a UNICODE string 201184610Salfred *------------------------------------------------------------------------*/ 202184610Salfreduint8_t 203184610Salfredusb2_make_str_desc(void *ptr, uint16_t max_len, const char *s) 204184610Salfred{ 205192984Sthompsa struct usb_string_descriptor *p = ptr; 206184610Salfred uint8_t totlen; 207184610Salfred int j; 208184610Salfred 209184610Salfred if (max_len < 2) { 210184610Salfred /* invalid length */ 211184610Salfred return (0); 212184610Salfred } 213184610Salfred max_len = ((max_len / 2) - 1); 214184610Salfred 215184610Salfred j = strlen(s); 216184610Salfred 217184610Salfred if (j < 0) { 218184610Salfred j = 0; 219184610Salfred } 220184610Salfred if (j > 126) { 221184610Salfred j = 126; 222184610Salfred } 223184610Salfred if (max_len > j) { 224184610Salfred max_len = j; 225184610Salfred } 226184610Salfred totlen = (max_len + 1) * 2; 227184610Salfred 228184610Salfred p->bLength = totlen; 229184610Salfred p->bDescriptorType = UDESC_STRING; 230184610Salfred 231184610Salfred while (max_len--) { 232184610Salfred USETW2(p->bString[max_len], 0, s[max_len]); 233184610Salfred } 234184610Salfred return (totlen); 235184610Salfred} 236184610Salfred 237190180Sthompsa#if (USB_HAVE_CONDVAR == 0) 238184610Salfred 239184610Salfred/*------------------------------------------------------------------------* 240184610Salfred * usb2_cv_init - wrapper function 241184610Salfred *------------------------------------------------------------------------*/ 242184610Salfredvoid 243184610Salfredusb2_cv_init(struct cv *cv, const char *desc) 244184610Salfred{ 245184610Salfred cv_init(cv, desc); 246184610Salfred} 247184610Salfred 248184610Salfred/*------------------------------------------------------------------------* 249184610Salfred * usb2_cv_destroy - wrapper function 250184610Salfred *------------------------------------------------------------------------*/ 251184610Salfredvoid 252184610Salfredusb2_cv_destroy(struct cv *cv) 253184610Salfred{ 254184610Salfred cv_destroy(cv); 255184610Salfred} 256184610Salfred 257184610Salfred/*------------------------------------------------------------------------* 258184610Salfred * usb2_cv_wait - wrapper function 259184610Salfred *------------------------------------------------------------------------*/ 260184610Salfredvoid 261184610Salfredusb2_cv_wait(struct cv *cv, struct mtx *mtx) 262184610Salfred{ 263184610Salfred int err; 264184610Salfred 265184610Salfred err = usb2_msleep(cv, mtx, 0, cv_wmesg(cv), 0); 266184610Salfred} 267184610Salfred 268184610Salfred/*------------------------------------------------------------------------* 269184610Salfred * usb2_cv_wait_sig - wrapper function 270184610Salfred *------------------------------------------------------------------------*/ 271184610Salfredint 272184610Salfredusb2_cv_wait_sig(struct cv *cv, struct mtx *mtx) 273184610Salfred{ 274184610Salfred int err; 275184610Salfred 276184610Salfred err = usb2_msleep(cv, mtx, PCATCH, cv_wmesg(cv), 0); 277184610Salfred return (err); 278184610Salfred} 279184610Salfred 280184610Salfred/*------------------------------------------------------------------------* 281184610Salfred * usb2_cv_timedwait - wrapper function 282184610Salfred *------------------------------------------------------------------------*/ 283184610Salfredint 284184610Salfredusb2_cv_timedwait(struct cv *cv, struct mtx *mtx, int timo) 285184610Salfred{ 286184610Salfred int err; 287184610Salfred 288184610Salfred if (timo == 0) 289184610Salfred timo = 1; /* zero means no timeout */ 290184610Salfred err = usb2_msleep(cv, mtx, 0, cv_wmesg(cv), timo); 291184610Salfred return (err); 292184610Salfred} 293184610Salfred 294184610Salfred/*------------------------------------------------------------------------* 295184610Salfred * usb2_cv_signal - wrapper function 296184610Salfred *------------------------------------------------------------------------*/ 297184610Salfredvoid 298184610Salfredusb2_cv_signal(struct cv *cv) 299184610Salfred{ 300184610Salfred wakeup_one(cv); 301184610Salfred} 302184610Salfred 303184610Salfred/*------------------------------------------------------------------------* 304184610Salfred * usb2_cv_broadcast - wrapper function 305184610Salfred *------------------------------------------------------------------------*/ 306184610Salfredvoid 307184610Salfredusb2_cv_broadcast(struct cv *cv) 308184610Salfred{ 309184610Salfred wakeup(cv); 310184610Salfred} 311184610Salfred 312184610Salfred/*------------------------------------------------------------------------* 313184610Salfred * usb2_msleep - wrapper function 314184610Salfred *------------------------------------------------------------------------*/ 315184610Salfredstatic int 316184610Salfredusb2_msleep(void *chan, struct mtx *mtx, int priority, const char *wmesg, 317184610Salfred int timo) 318184610Salfred{ 319184610Salfred int err; 320184610Salfred 321184610Salfred if (mtx == &Giant) { 322184610Salfred err = tsleep(chan, priority, wmesg, timo); 323184610Salfred } else { 324184610Salfred#ifdef mtx_sleep 325184610Salfred err = mtx_sleep(chan, mtx, priority, wmesg, timo); 326184610Salfred#else 327184610Salfred err = msleep(chan, mtx, priority, wmesg, timo); 328184610Salfred#endif 329184610Salfred } 330184610Salfred return (err); 331184610Salfred} 332184610Salfred 333184610Salfred#endif 334