usb_util.c revision 193045
1/* $FreeBSD: head/sys/dev/usb/usb_util.c 193045 2009-05-29 18:46:57Z thompsa $ */ 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 struct usb_attach_arg *uaa; 82 struct usb_device *udev; 83 struct usb_interface *iface; 84 char *temp_p; 85 usb_error_t err; 86 87 if (dev == NULL) { 88 /* should not happen */ 89 return; 90 } 91 uaa = device_get_ivars(dev); 92 if (uaa == NULL) { 93 /* can happen if called at the wrong time */ 94 return; 95 } 96 udev = uaa->device; 97 iface = uaa->iface; 98 99 if ((iface == NULL) || 100 (iface->idesc == NULL) || 101 (iface->idesc->iInterface == 0)) { 102 err = USB_ERR_INVAL; 103 } else { 104 err = 0; 105 } 106 107 temp_p = (char *)udev->bus->scratch[0].data; 108 109 if (!err) { 110 /* try to get the interface string ! */ 111 err = usb2_req_get_string_any 112 (udev, NULL, temp_p, 113 sizeof(udev->bus->scratch), iface->idesc->iInterface); 114 } 115 if (err) { 116 /* use default description */ 117 usb2_devinfo(udev, temp_p, 118 sizeof(udev->bus->scratch)); 119 } 120 device_set_desc_copy(dev, temp_p); 121 device_printf(dev, "<%s> on %s\n", temp_p, 122 device_get_nameunit(udev->bus->bdev)); 123} 124 125/*------------------------------------------------------------------------* 126 * usb2_pause_mtx - factored out code 127 * 128 * This function will delay the code by the passed number of system 129 * ticks. The passed mutex "mtx" will be dropped while waiting, if 130 * "mtx" is not NULL. 131 *------------------------------------------------------------------------*/ 132void 133usb2_pause_mtx(struct mtx *mtx, int _ticks) 134{ 135 if (mtx != NULL) 136 mtx_unlock(mtx); 137 138 if (cold) { 139 /* convert to milliseconds */ 140 _ticks = (_ticks * 1000) / hz; 141 /* convert to microseconds, rounded up */ 142 _ticks = (_ticks + 1) * 1000; 143 DELAY(_ticks); 144 145 } else { 146 147 /* 148 * Add one to the number of ticks so that we don't return 149 * too early! 150 */ 151 _ticks++; 152 153 if (pause("USBWAIT", _ticks)) { 154 /* ignore */ 155 } 156 } 157 if (mtx != NULL) 158 mtx_lock(mtx); 159} 160 161/*------------------------------------------------------------------------* 162 * usb2_printBCD 163 * 164 * This function will print the version number "bcd" to the string 165 * pointed to by "p" having a maximum length of "p_len" bytes 166 * including the terminating zero. 167 *------------------------------------------------------------------------*/ 168void 169usb2_printBCD(char *p, uint16_t p_len, uint16_t bcd) 170{ 171 if (snprintf(p, p_len, "%x.%02x", bcd >> 8, bcd & 0xff)) { 172 /* ignore any errors */ 173 } 174} 175 176/*------------------------------------------------------------------------* 177 * usb2_trim_spaces 178 * 179 * This function removes spaces at the beginning and the end of the string 180 * pointed to by the "p" argument. 181 *------------------------------------------------------------------------*/ 182void 183usb2_trim_spaces(char *p) 184{ 185 char *q; 186 char *e; 187 188 if (p == NULL) 189 return; 190 q = e = p; 191 while (*q == ' ') /* skip leading spaces */ 192 q++; 193 while ((*p = *q++)) /* copy string */ 194 if (*p++ != ' ') /* remember last non-space */ 195 e = p; 196 *e = 0; /* kill trailing spaces */ 197} 198 199/*------------------------------------------------------------------------* 200 * usb2_make_str_desc - convert an ASCII string into a UNICODE string 201 *------------------------------------------------------------------------*/ 202uint8_t 203usb2_make_str_desc(void *ptr, uint16_t max_len, const char *s) 204{ 205 struct usb_string_descriptor *p = ptr; 206 uint8_t totlen; 207 int j; 208 209 if (max_len < 2) { 210 /* invalid length */ 211 return (0); 212 } 213 max_len = ((max_len / 2) - 1); 214 215 j = strlen(s); 216 217 if (j < 0) { 218 j = 0; 219 } 220 if (j > 126) { 221 j = 126; 222 } 223 if (max_len > j) { 224 max_len = j; 225 } 226 totlen = (max_len + 1) * 2; 227 228 p->bLength = totlen; 229 p->bDescriptorType = UDESC_STRING; 230 231 while (max_len--) { 232 USETW2(p->bString[max_len], 0, s[max_len]); 233 } 234 return (totlen); 235} 236 237#if (USB_HAVE_CONDVAR == 0) 238 239/*------------------------------------------------------------------------* 240 * usb2_cv_init - wrapper function 241 *------------------------------------------------------------------------*/ 242void 243usb2_cv_init(struct cv *cv, const char *desc) 244{ 245 cv_init(cv, desc); 246} 247 248/*------------------------------------------------------------------------* 249 * usb2_cv_destroy - wrapper function 250 *------------------------------------------------------------------------*/ 251void 252usb2_cv_destroy(struct cv *cv) 253{ 254 cv_destroy(cv); 255} 256 257/*------------------------------------------------------------------------* 258 * usb2_cv_wait - wrapper function 259 *------------------------------------------------------------------------*/ 260void 261usb2_cv_wait(struct cv *cv, struct mtx *mtx) 262{ 263 int err; 264 265 err = usb2_msleep(cv, mtx, 0, cv_wmesg(cv), 0); 266} 267 268/*------------------------------------------------------------------------* 269 * usb2_cv_wait_sig - wrapper function 270 *------------------------------------------------------------------------*/ 271int 272usb2_cv_wait_sig(struct cv *cv, struct mtx *mtx) 273{ 274 int err; 275 276 err = usb2_msleep(cv, mtx, PCATCH, cv_wmesg(cv), 0); 277 return (err); 278} 279 280/*------------------------------------------------------------------------* 281 * usb2_cv_timedwait - wrapper function 282 *------------------------------------------------------------------------*/ 283int 284usb2_cv_timedwait(struct cv *cv, struct mtx *mtx, int timo) 285{ 286 int err; 287 288 if (timo == 0) 289 timo = 1; /* zero means no timeout */ 290 err = usb2_msleep(cv, mtx, 0, cv_wmesg(cv), timo); 291 return (err); 292} 293 294/*------------------------------------------------------------------------* 295 * usb2_cv_signal - wrapper function 296 *------------------------------------------------------------------------*/ 297void 298usb2_cv_signal(struct cv *cv) 299{ 300 wakeup_one(cv); 301} 302 303/*------------------------------------------------------------------------* 304 * usb2_cv_broadcast - wrapper function 305 *------------------------------------------------------------------------*/ 306void 307usb2_cv_broadcast(struct cv *cv) 308{ 309 wakeup(cv); 310} 311 312/*------------------------------------------------------------------------* 313 * usb2_msleep - wrapper function 314 *------------------------------------------------------------------------*/ 315static int 316usb2_msleep(void *chan, struct mtx *mtx, int priority, const char *wmesg, 317 int timo) 318{ 319 int err; 320 321 if (mtx == &Giant) { 322 err = tsleep(chan, priority, wmesg, timo); 323 } else { 324#ifdef mtx_sleep 325 err = mtx_sleep(chan, mtx, priority, wmesg, timo); 326#else 327 err = msleep(chan, mtx, priority, wmesg, timo); 328#endif 329 } 330 return (err); 331} 332 333#endif 334