1184610Salfred/* $FreeBSD$ */ 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; 74247090Shselasky uint8_t do_unlock; 75184610Salfred 76184610Salfred if (dev == NULL) { 77184610Salfred /* should not happen */ 78184610Salfred return; 79184610Salfred } 80184610Salfred uaa = device_get_ivars(dev); 81184610Salfred if (uaa == NULL) { 82185087Salfred /* can happen if called at the wrong time */ 83184610Salfred return; 84184610Salfred } 85184610Salfred udev = uaa->device; 86184610Salfred iface = uaa->iface; 87184610Salfred 88184610Salfred if ((iface == NULL) || 89184610Salfred (iface->idesc == NULL) || 90184610Salfred (iface->idesc->iInterface == 0)) { 91184610Salfred err = USB_ERR_INVAL; 92184610Salfred } else { 93184610Salfred err = 0; 94184610Salfred } 95184610Salfred 96247090Shselasky /* Protect scratch area */ 97247090Shselasky do_unlock = usbd_enum_lock(udev); 98184610Salfred 99247090Shselasky temp_p = (char *)udev->scratch.data; 100247090Shselasky 101247090Shselasky if (err == 0) { 102184610Salfred /* try to get the interface string ! */ 103247090Shselasky err = usbd_req_get_string_any(udev, NULL, temp_p, 104247090Shselasky sizeof(udev->scratch.data), 105247090Shselasky iface->idesc->iInterface); 106184610Salfred } 107247090Shselasky if (err != 0) { 108184610Salfred /* use default description */ 109194228Sthompsa usb_devinfo(udev, temp_p, 110247090Shselasky sizeof(udev->scratch.data)); 111184610Salfred } 112247090Shselasky 113247090Shselasky if (do_unlock) 114247090Shselasky usbd_enum_unlock(udev); 115247090Shselasky 116184610Salfred device_set_desc_copy(dev, temp_p); 117184610Salfred device_printf(dev, "<%s> on %s\n", temp_p, 118184610Salfred device_get_nameunit(udev->bus->bdev)); 119184610Salfred} 120184610Salfred 121184610Salfred/*------------------------------------------------------------------------* 122194228Sthompsa * usb_pause_mtx - factored out code 123184610Salfred * 124188411Sthompsa * This function will delay the code by the passed number of system 125188411Sthompsa * ticks. The passed mutex "mtx" will be dropped while waiting, if 126229119Shselasky * "mtx" is different from NULL. 127184610Salfred *------------------------------------------------------------------------*/ 128184610Salfredvoid 129229119Shselaskyusb_pause_mtx(struct mtx *mtx, int timo) 130184610Salfred{ 131188411Sthompsa if (mtx != NULL) 132188411Sthompsa mtx_unlock(mtx); 133188411Sthompsa 134229119Shselasky /* 135229119Shselasky * Add one tick to the timeout so that we don't return too 136229119Shselasky * early! Note that pause() will assert that the passed 137229119Shselasky * timeout is positive and non-zero! 138229119Shselasky */ 139229119Shselasky pause("USBWAIT", timo + 1); 140184610Salfred 141188411Sthompsa if (mtx != NULL) 142188411Sthompsa mtx_lock(mtx); 143184610Salfred} 144184610Salfred 145184610Salfred/*------------------------------------------------------------------------* 146194228Sthompsa * usb_printbcd 147184610Salfred * 148184610Salfred * This function will print the version number "bcd" to the string 149184610Salfred * pointed to by "p" having a maximum length of "p_len" bytes 150184610Salfred * including the terminating zero. 151184610Salfred *------------------------------------------------------------------------*/ 152184610Salfredvoid 153194228Sthompsausb_printbcd(char *p, uint16_t p_len, uint16_t bcd) 154184610Salfred{ 155184610Salfred if (snprintf(p, p_len, "%x.%02x", bcd >> 8, bcd & 0xff)) { 156184610Salfred /* ignore any errors */ 157184610Salfred } 158184610Salfred} 159184610Salfred 160184610Salfred/*------------------------------------------------------------------------* 161194228Sthompsa * usb_trim_spaces 162184610Salfred * 163184610Salfred * This function removes spaces at the beginning and the end of the string 164184610Salfred * pointed to by the "p" argument. 165184610Salfred *------------------------------------------------------------------------*/ 166184610Salfredvoid 167194228Sthompsausb_trim_spaces(char *p) 168184610Salfred{ 169184610Salfred char *q; 170184610Salfred char *e; 171184610Salfred 172184610Salfred if (p == NULL) 173184610Salfred return; 174184610Salfred q = e = p; 175184610Salfred while (*q == ' ') /* skip leading spaces */ 176184610Salfred q++; 177184610Salfred while ((*p = *q++)) /* copy string */ 178184610Salfred if (*p++ != ' ') /* remember last non-space */ 179184610Salfred e = p; 180184610Salfred *e = 0; /* kill trailing spaces */ 181184610Salfred} 182184610Salfred 183184610Salfred/*------------------------------------------------------------------------* 184194228Sthompsa * usb_make_str_desc - convert an ASCII string into a UNICODE string 185184610Salfred *------------------------------------------------------------------------*/ 186184610Salfreduint8_t 187194228Sthompsausb_make_str_desc(void *ptr, uint16_t max_len, const char *s) 188184610Salfred{ 189192984Sthompsa struct usb_string_descriptor *p = ptr; 190184610Salfred uint8_t totlen; 191184610Salfred int j; 192184610Salfred 193184610Salfred if (max_len < 2) { 194184610Salfred /* invalid length */ 195184610Salfred return (0); 196184610Salfred } 197184610Salfred max_len = ((max_len / 2) - 1); 198184610Salfred 199184610Salfred j = strlen(s); 200184610Salfred 201184610Salfred if (j < 0) { 202184610Salfred j = 0; 203184610Salfred } 204184610Salfred if (j > 126) { 205184610Salfred j = 126; 206184610Salfred } 207184610Salfred if (max_len > j) { 208184610Salfred max_len = j; 209184610Salfred } 210184610Salfred totlen = (max_len + 1) * 2; 211184610Salfred 212184610Salfred p->bLength = totlen; 213184610Salfred p->bDescriptorType = UDESC_STRING; 214184610Salfred 215184610Salfred while (max_len--) { 216184610Salfred USETW2(p->bString[max_len], 0, s[max_len]); 217184610Salfred } 218184610Salfred return (totlen); 219184610Salfred} 220