usb_util.c revision 190633
1/* $FreeBSD: head/sys/dev/usb/usb_util.c 190633 2009-04-01 20:23:47Z piso $ */ 2/*- 3 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27#include <dev/usb/usb_mfunc.h> 28#include <dev/usb/usb_error.h> 29#include <dev/usb/usb.h> 30 31#include <dev/usb/usb_core.h> 32#include <dev/usb/usb_util.h> 33#include <dev/usb/usb_process.h> 34#include <dev/usb/usb_device.h> 35#include <dev/usb/usb_request.h> 36#include <dev/usb/usb_busdma.h> 37 38#include <dev/usb/usb_controller.h> 39#include <dev/usb/usb_bus.h> 40 41/* function prototypes */ 42#if (USB_HAVE_CONDVAR == 0) 43static int usb2_msleep(void *chan, struct mtx *mtx, int priority, const char *wmesg, int timo); 44 45#endif 46 47/*------------------------------------------------------------------------* 48 * device_delete_all_children - delete all children of a device 49 *------------------------------------------------------------------------*/ 50#ifndef device_delete_all_children 51int 52device_delete_all_children(device_t dev) 53{ 54 device_t *devlist; 55 int devcount; 56 int error; 57 58 error = device_get_children(dev, &devlist, &devcount); 59 if (error == 0) { 60 while (devcount-- > 0) { 61 error = device_delete_child(dev, devlist[devcount]); 62 if (error) { 63 break; 64 } 65 } 66 free(devlist, M_TEMP); 67 } 68 return (error); 69} 70#endif 71 72/*------------------------------------------------------------------------* 73 * device_set_usb2_desc 74 * 75 * This function can be called at probe or attach to set the USB 76 * device supplied textual description for the given device. 77 *------------------------------------------------------------------------*/ 78void 79device_set_usb2_desc(device_t dev) 80{ 81#if USB_HAVE_STRINGS 82 struct usb2_attach_arg *uaa; 83 struct usb2_device *udev; 84 struct usb2_interface *iface; 85 char *temp_p; 86 usb2_error_t err; 87 88 if (dev == NULL) { 89 /* should not happen */ 90 return; 91 } 92 uaa = device_get_ivars(dev); 93 if (uaa == NULL) { 94 /* can happen if called at the wrong time */ 95 return; 96 } 97 udev = uaa->device; 98 iface = uaa->iface; 99 100 if ((iface == NULL) || 101 (iface->idesc == NULL) || 102 (iface->idesc->iInterface == 0)) { 103 err = USB_ERR_INVAL; 104 } else { 105 err = 0; 106 } 107 108 temp_p = (char *)udev->bus->scratch[0].data; 109 110 if (!err) { 111 /* try to get the interface string ! */ 112 err = usb2_req_get_string_any 113 (udev, NULL, temp_p, 114 sizeof(udev->bus->scratch), iface->idesc->iInterface); 115 } 116 if (err) { 117 /* use default description */ 118 usb2_devinfo(udev, temp_p, 119 sizeof(udev->bus->scratch)); 120 } 121 device_set_desc_copy(dev, temp_p); 122 device_printf(dev, "<%s> on %s\n", temp_p, 123 device_get_nameunit(udev->bus->bdev)); 124#endif 125} 126 127/*------------------------------------------------------------------------* 128 * usb2_pause_mtx - factored out code 129 * 130 * This function will delay the code by the passed number of system 131 * ticks. The passed mutex "mtx" will be dropped while waiting, if 132 * "mtx" is not NULL. 133 *------------------------------------------------------------------------*/ 134void 135usb2_pause_mtx(struct mtx *mtx, int _ticks) 136{ 137 if (mtx != NULL) 138 mtx_unlock(mtx); 139 140 if (cold) { 141 /* convert to milliseconds */ 142 _ticks = (_ticks * 1000) / hz; 143 /* convert to microseconds, rounded up */ 144 _ticks = (_ticks + 1) * 1000; 145 DELAY(_ticks); 146 147 } else { 148 149 /* 150 * Add one to the number of ticks so that we don't return 151 * too early! 152 */ 153 _ticks++; 154 155 if (pause("USBWAIT", _ticks)) { 156 /* ignore */ 157 } 158 } 159 if (mtx != NULL) 160 mtx_lock(mtx); 161} 162 163/*------------------------------------------------------------------------* 164 * usb2_printBCD 165 * 166 * This function will print the version number "bcd" to the string 167 * pointed to by "p" having a maximum length of "p_len" bytes 168 * including the terminating zero. 169 *------------------------------------------------------------------------*/ 170void 171usb2_printBCD(char *p, uint16_t p_len, uint16_t bcd) 172{ 173 if (snprintf(p, p_len, "%x.%02x", bcd >> 8, bcd & 0xff)) { 174 /* ignore any errors */ 175 } 176} 177 178/*------------------------------------------------------------------------* 179 * usb2_trim_spaces 180 * 181 * This function removes spaces at the beginning and the end of the string 182 * pointed to by the "p" argument. 183 *------------------------------------------------------------------------*/ 184#if USB_HAVE_STRINGS 185void 186usb2_trim_spaces(char *p) 187{ 188 char *q; 189 char *e; 190 191 if (p == NULL) 192 return; 193 q = e = p; 194 while (*q == ' ') /* skip leading spaces */ 195 q++; 196 while ((*p = *q++)) /* copy string */ 197 if (*p++ != ' ') /* remember last non-space */ 198 e = p; 199 *e = 0; /* kill trailing spaces */ 200} 201#endif 202 203/*------------------------------------------------------------------------* 204 * usb2_make_str_desc - convert an ASCII string into a UNICODE string 205 *------------------------------------------------------------------------*/ 206uint8_t 207usb2_make_str_desc(void *ptr, uint16_t max_len, const char *s) 208{ 209 struct usb2_string_descriptor *p = ptr; 210 uint8_t totlen; 211 int j; 212 213 if (max_len < 2) { 214 /* invalid length */ 215 return (0); 216 } 217 max_len = ((max_len / 2) - 1); 218 219 j = strlen(s); 220 221 if (j < 0) { 222 j = 0; 223 } 224 if (j > 126) { 225 j = 126; 226 } 227 if (max_len > j) { 228 max_len = j; 229 } 230 totlen = (max_len + 1) * 2; 231 232 p->bLength = totlen; 233 p->bDescriptorType = UDESC_STRING; 234 235 while (max_len--) { 236 USETW2(p->bString[max_len], 0, s[max_len]); 237 } 238 return (totlen); 239} 240 241#if (USB_HAVE_CONDVAR == 0) 242 243/*------------------------------------------------------------------------* 244 * usb2_cv_init - wrapper function 245 *------------------------------------------------------------------------*/ 246void 247usb2_cv_init(struct cv *cv, const char *desc) 248{ 249 cv_init(cv, desc); 250} 251 252/*------------------------------------------------------------------------* 253 * usb2_cv_destroy - wrapper function 254 *------------------------------------------------------------------------*/ 255void 256usb2_cv_destroy(struct cv *cv) 257{ 258 cv_destroy(cv); 259} 260 261/*------------------------------------------------------------------------* 262 * usb2_cv_wait - wrapper function 263 *------------------------------------------------------------------------*/ 264void 265usb2_cv_wait(struct cv *cv, struct mtx *mtx) 266{ 267 int err; 268 269 err = usb2_msleep(cv, mtx, 0, cv_wmesg(cv), 0); 270} 271 272/*------------------------------------------------------------------------* 273 * usb2_cv_wait_sig - wrapper function 274 *------------------------------------------------------------------------*/ 275int 276usb2_cv_wait_sig(struct cv *cv, struct mtx *mtx) 277{ 278 int err; 279 280 err = usb2_msleep(cv, mtx, PCATCH, cv_wmesg(cv), 0); 281 return (err); 282} 283 284/*------------------------------------------------------------------------* 285 * usb2_cv_timedwait - wrapper function 286 *------------------------------------------------------------------------*/ 287int 288usb2_cv_timedwait(struct cv *cv, struct mtx *mtx, int timo) 289{ 290 int err; 291 292 if (timo == 0) 293 timo = 1; /* zero means no timeout */ 294 err = usb2_msleep(cv, mtx, 0, cv_wmesg(cv), timo); 295 return (err); 296} 297 298/*------------------------------------------------------------------------* 299 * usb2_cv_signal - wrapper function 300 *------------------------------------------------------------------------*/ 301void 302usb2_cv_signal(struct cv *cv) 303{ 304 wakeup_one(cv); 305} 306 307/*------------------------------------------------------------------------* 308 * usb2_cv_broadcast - wrapper function 309 *------------------------------------------------------------------------*/ 310void 311usb2_cv_broadcast(struct cv *cv) 312{ 313 wakeup(cv); 314} 315 316/*------------------------------------------------------------------------* 317 * usb2_msleep - wrapper function 318 *------------------------------------------------------------------------*/ 319static int 320usb2_msleep(void *chan, struct mtx *mtx, int priority, const char *wmesg, 321 int timo) 322{ 323 int err; 324 325 if (mtx == &Giant) { 326 err = tsleep(chan, priority, wmesg, timo); 327 } else { 328#ifdef mtx_sleep 329 err = mtx_sleep(chan, mtx, priority, wmesg, timo); 330#else 331 err = msleep(chan, mtx, priority, wmesg, timo); 332#endif 333 } 334 return (err); 335} 336 337#endif 338