usb_util.c revision 227706
1184610Salfred/* $FreeBSD: head/sys/dev/usb/usb_util.c 227706 2011-11-19 11:17:27Z hselasky $ */ 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 27194677Sthompsa#include <sys/stdint.h> 28194677Sthompsa#include <sys/stddef.h> 29194677Sthompsa#include <sys/param.h> 30194677Sthompsa#include <sys/queue.h> 31194677Sthompsa#include <sys/types.h> 32194677Sthompsa#include <sys/systm.h> 33194677Sthompsa#include <sys/kernel.h> 34194677Sthompsa#include <sys/bus.h> 35194677Sthompsa#include <sys/module.h> 36194677Sthompsa#include <sys/lock.h> 37194677Sthompsa#include <sys/mutex.h> 38194677Sthompsa#include <sys/condvar.h> 39194677Sthompsa#include <sys/sysctl.h> 40194677Sthompsa#include <sys/sx.h> 41194677Sthompsa#include <sys/unistd.h> 42194677Sthompsa#include <sys/callout.h> 43194677Sthompsa#include <sys/malloc.h> 44194677Sthompsa#include <sys/priv.h> 45194677Sthompsa 46188942Sthompsa#include <dev/usb/usb.h> 47194677Sthompsa#include <dev/usb/usbdi.h> 48194677Sthompsa#include <dev/usb/usbdi_util.h> 49184610Salfred 50188942Sthompsa#include <dev/usb/usb_core.h> 51188942Sthompsa#include <dev/usb/usb_util.h> 52188942Sthompsa#include <dev/usb/usb_process.h> 53188942Sthompsa#include <dev/usb/usb_device.h> 54188942Sthompsa#include <dev/usb/usb_request.h> 55188942Sthompsa#include <dev/usb/usb_busdma.h> 56184610Salfred 57188942Sthompsa#include <dev/usb/usb_controller.h> 58188942Sthompsa#include <dev/usb/usb_bus.h> 59184610Salfred 60184610Salfred/*------------------------------------------------------------------------* 61194228Sthompsa * device_set_usb_desc 62184610Salfred * 63184610Salfred * This function can be called at probe or attach to set the USB 64184610Salfred * device supplied textual description for the given device. 65184610Salfred *------------------------------------------------------------------------*/ 66184610Salfredvoid 67194228Sthompsadevice_set_usb_desc(device_t dev) 68184610Salfred{ 69192984Sthompsa struct usb_attach_arg *uaa; 70192984Sthompsa struct usb_device *udev; 71192984Sthompsa struct usb_interface *iface; 72184610Salfred char *temp_p; 73193045Sthompsa usb_error_t err; 74184610Salfred 75184610Salfred if (dev == NULL) { 76184610Salfred /* should not happen */ 77184610Salfred return; 78184610Salfred } 79184610Salfred uaa = device_get_ivars(dev); 80184610Salfred if (uaa == NULL) { 81185087Salfred /* can happen if called at the wrong time */ 82184610Salfred return; 83184610Salfred } 84184610Salfred udev = uaa->device; 85184610Salfred iface = uaa->iface; 86184610Salfred 87184610Salfred if ((iface == NULL) || 88184610Salfred (iface->idesc == NULL) || 89184610Salfred (iface->idesc->iInterface == 0)) { 90184610Salfred err = USB_ERR_INVAL; 91184610Salfred } else { 92184610Salfred err = 0; 93184610Salfred } 94184610Salfred 95184610Salfred temp_p = (char *)udev->bus->scratch[0].data; 96184610Salfred 97184610Salfred if (!err) { 98184610Salfred /* try to get the interface string ! */ 99194228Sthompsa err = usbd_req_get_string_any 100184610Salfred (udev, NULL, temp_p, 101184610Salfred sizeof(udev->bus->scratch), iface->idesc->iInterface); 102184610Salfred } 103184610Salfred if (err) { 104184610Salfred /* use default description */ 105194228Sthompsa usb_devinfo(udev, temp_p, 106184610Salfred sizeof(udev->bus->scratch)); 107184610Salfred } 108184610Salfred device_set_desc_copy(dev, temp_p); 109184610Salfred device_printf(dev, "<%s> on %s\n", temp_p, 110184610Salfred device_get_nameunit(udev->bus->bdev)); 111184610Salfred} 112184610Salfred 113184610Salfred/*------------------------------------------------------------------------* 114194228Sthompsa * usb_pause_mtx - factored out code 115184610Salfred * 116188411Sthompsa * This function will delay the code by the passed number of system 117188411Sthompsa * ticks. The passed mutex "mtx" will be dropped while waiting, if 118227706Shselasky * "mtx" is different from NULL. 119184610Salfred *------------------------------------------------------------------------*/ 120184610Salfredvoid 121227706Shselaskyusb_pause_mtx(struct mtx *mtx, int timo) 122184610Salfred{ 123188411Sthompsa if (mtx != NULL) 124188411Sthompsa mtx_unlock(mtx); 125188411Sthompsa 126227706Shselasky /* 127227706Shselasky * Add one tick to the timeout so that we don't return too 128227706Shselasky * early! Note that pause() will assert that the passed 129227706Shselasky * timeout is positive and non-zero! 130227706Shselasky */ 131227706Shselasky pause("USBWAIT", timo + 1); 132184610Salfred 133188411Sthompsa if (mtx != NULL) 134188411Sthompsa mtx_lock(mtx); 135184610Salfred} 136184610Salfred 137184610Salfred/*------------------------------------------------------------------------* 138194228Sthompsa * usb_printbcd 139184610Salfred * 140184610Salfred * This function will print the version number "bcd" to the string 141184610Salfred * pointed to by "p" having a maximum length of "p_len" bytes 142184610Salfred * including the terminating zero. 143184610Salfred *------------------------------------------------------------------------*/ 144184610Salfredvoid 145194228Sthompsausb_printbcd(char *p, uint16_t p_len, uint16_t bcd) 146184610Salfred{ 147184610Salfred if (snprintf(p, p_len, "%x.%02x", bcd >> 8, bcd & 0xff)) { 148184610Salfred /* ignore any errors */ 149184610Salfred } 150184610Salfred} 151184610Salfred 152184610Salfred/*------------------------------------------------------------------------* 153194228Sthompsa * usb_trim_spaces 154184610Salfred * 155184610Salfred * This function removes spaces at the beginning and the end of the string 156184610Salfred * pointed to by the "p" argument. 157184610Salfred *------------------------------------------------------------------------*/ 158184610Salfredvoid 159194228Sthompsausb_trim_spaces(char *p) 160184610Salfred{ 161184610Salfred char *q; 162184610Salfred char *e; 163184610Salfred 164184610Salfred if (p == NULL) 165184610Salfred return; 166184610Salfred q = e = p; 167184610Salfred while (*q == ' ') /* skip leading spaces */ 168184610Salfred q++; 169184610Salfred while ((*p = *q++)) /* copy string */ 170184610Salfred if (*p++ != ' ') /* remember last non-space */ 171184610Salfred e = p; 172184610Salfred *e = 0; /* kill trailing spaces */ 173184610Salfred} 174184610Salfred 175184610Salfred/*------------------------------------------------------------------------* 176194228Sthompsa * usb_make_str_desc - convert an ASCII string into a UNICODE string 177184610Salfred *------------------------------------------------------------------------*/ 178184610Salfreduint8_t 179194228Sthompsausb_make_str_desc(void *ptr, uint16_t max_len, const char *s) 180184610Salfred{ 181192984Sthompsa struct usb_string_descriptor *p = ptr; 182184610Salfred uint8_t totlen; 183184610Salfred int j; 184184610Salfred 185184610Salfred if (max_len < 2) { 186184610Salfred /* invalid length */ 187184610Salfred return (0); 188184610Salfred } 189184610Salfred max_len = ((max_len / 2) - 1); 190184610Salfred 191184610Salfred j = strlen(s); 192184610Salfred 193184610Salfred if (j < 0) { 194184610Salfred j = 0; 195184610Salfred } 196184610Salfred if (j > 126) { 197184610Salfred j = 126; 198184610Salfred } 199184610Salfred if (max_len > j) { 200184610Salfred max_len = j; 201184610Salfred } 202184610Salfred totlen = (max_len + 1) * 2; 203184610Salfred 204184610Salfred p->bLength = totlen; 205184610Salfred p->bDescriptorType = UDESC_STRING; 206184610Salfred 207184610Salfred while (max_len--) { 208184610Salfred USETW2(p->bString[max_len], 0, s[max_len]); 209184610Salfred } 210184610Salfred return (totlen); 211184610Salfred} 212