usb_util.c revision 185087
1278307Srpaulo/* $FreeBSD: head/sys/dev/usb2/core/usb2_util.c 185087 2008-11-19 08:56:35Z alfred $ */ 2278307Srpaulo/*- 3278307Srpaulo * Copyright (c) 2008 Hans Petter Selasky. All rights reserved. 4278307Srpaulo * 5278307Srpaulo * Redistribution and use in source and binary forms, with or without 6278307Srpaulo * modification, are permitted provided that the following conditions 7278307Srpaulo * are met: 8278307Srpaulo * 1. Redistributions of source code must retain the above copyright 9278307Srpaulo * notice, this list of conditions and the following disclaimer. 10278307Srpaulo * 2. Redistributions in binary form must reproduce the above copyright 11278307Srpaulo * notice, this list of conditions and the following disclaimer in the 12278307Srpaulo * documentation and/or other materials provided with the distribution. 13278307Srpaulo * 14278307Srpaulo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15278307Srpaulo * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16278307Srpaulo * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17278307Srpaulo * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18278307Srpaulo * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19278307Srpaulo * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20278307Srpaulo * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21278307Srpaulo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22278307Srpaulo * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23278307Srpaulo * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24278307Srpaulo * SUCH DAMAGE. 25 */ 26 27#include <dev/usb2/include/usb2_defs.h> 28#include <dev/usb2/include/usb2_mfunc.h> 29#include <dev/usb2/include/usb2_error.h> 30#include <dev/usb2/include/usb2_standard.h> 31 32#include <dev/usb2/core/usb2_core.h> 33#include <dev/usb2/core/usb2_util.h> 34#include <dev/usb2/core/usb2_process.h> 35#include <dev/usb2/core/usb2_device.h> 36#include <dev/usb2/core/usb2_request.h> 37#include <dev/usb2/core/usb2_busdma.h> 38 39#include <dev/usb2/controller/usb2_controller.h> 40#include <dev/usb2/controller/usb2_bus.h> 41 42/* function prototypes */ 43#if (USB_USE_CONDVAR == 0) 44static int usb2_msleep(void *chan, struct mtx *mtx, int priority, const char *wmesg, int timo); 45 46#endif 47 48/*------------------------------------------------------------------------* 49 * device_delete_all_children - delete all children of a device 50 *------------------------------------------------------------------------*/ 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 71/*------------------------------------------------------------------------* 72 * device_set_usb2_desc 73 * 74 * This function can be called at probe or attach to set the USB 75 * device supplied textual description for the given device. 76 *------------------------------------------------------------------------*/ 77void 78device_set_usb2_desc(device_t dev) 79{ 80 struct usb2_attach_arg *uaa; 81 struct usb2_device *udev; 82 struct usb2_interface *iface; 83 char *temp_p; 84 usb2_error_t err; 85 86 if (dev == NULL) { 87 /* should not happen */ 88 return; 89 } 90 uaa = device_get_ivars(dev); 91 if (uaa == NULL) { 92 /* can happen if called at the wrong time */ 93 return; 94 } 95 udev = uaa->device; 96 iface = uaa->iface; 97 98 if ((iface == NULL) || 99 (iface->idesc == NULL) || 100 (iface->idesc->iInterface == 0)) { 101 err = USB_ERR_INVAL; 102 } else { 103 err = 0; 104 } 105 106 temp_p = (char *)udev->bus->scratch[0].data; 107 108 if (!err) { 109 /* try to get the interface string ! */ 110 err = usb2_req_get_string_any 111 (udev, NULL, temp_p, 112 sizeof(udev->bus->scratch), iface->idesc->iInterface); 113 } 114 if (err) { 115 /* use default description */ 116 usb2_devinfo(udev, temp_p, 117 sizeof(udev->bus->scratch)); 118 } 119 device_set_desc_copy(dev, temp_p); 120 device_printf(dev, "<%s> on %s\n", temp_p, 121 device_get_nameunit(udev->bus->bdev)); 122 return; 123} 124 125/*------------------------------------------------------------------------* 126 * usb2_pause_mtx - factored out code 127 * 128 * This function will delay the code by the passed number of 129 * milliseconds. The passed mutex "mtx" will be dropped while waiting, 130 * if "mtx" is not NULL. The number of milliseconds per second is 1024 131 * for sake of optimisation. 132 *------------------------------------------------------------------------*/ 133void 134usb2_pause_mtx(struct mtx *mtx, uint32_t ms) 135{ 136 if (cold) { 137 ms = (ms + 1) * 1024; 138 DELAY(ms); 139 140 } else { 141 142 ms = USB_MS_TO_TICKS(ms); 143 /* 144 * Add one to the number of ticks so that we don't return 145 * too early! 146 */ 147 ms++; 148 149 if (mtx != NULL) 150 mtx_unlock(mtx); 151 152 if (pause("USBWAIT", ms)) { 153 /* ignore */ 154 } 155 if (mtx != NULL) 156 mtx_lock(mtx); 157 } 158 return; 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 return; 175} 176 177/*------------------------------------------------------------------------* 178 * usb2_trim_spaces 179 * 180 * This function removes spaces at the beginning and the end of the string 181 * pointed to by the "p" argument. 182 *------------------------------------------------------------------------*/ 183void 184usb2_trim_spaces(char *p) 185{ 186 char *q; 187 char *e; 188 189 if (p == NULL) 190 return; 191 q = e = p; 192 while (*q == ' ') /* skip leading spaces */ 193 q++; 194 while ((*p = *q++)) /* copy string */ 195 if (*p++ != ' ') /* remember last non-space */ 196 e = p; 197 *e = 0; /* kill trailing spaces */ 198 return; 199} 200 201/*------------------------------------------------------------------------* 202 * usb2_get_devid 203 * 204 * This function returns the USB Vendor and Product ID like a 32-bit 205 * unsigned integer. 206 *------------------------------------------------------------------------*/ 207uint32_t 208usb2_get_devid(device_t dev) 209{ 210 struct usb2_attach_arg *uaa = device_get_ivars(dev); 211 212 return ((uaa->info.idVendor << 16) | (uaa->info.idProduct)); 213} 214 215/*------------------------------------------------------------------------* 216 * usb2_make_str_desc - convert an ASCII string into a UNICODE string 217 *------------------------------------------------------------------------*/ 218uint8_t 219usb2_make_str_desc(void *ptr, uint16_t max_len, const char *s) 220{ 221 struct usb2_string_descriptor *p = ptr; 222 uint8_t totlen; 223 int j; 224 225 if (max_len < 2) { 226 /* invalid length */ 227 return (0); 228 } 229 max_len = ((max_len / 2) - 1); 230 231 j = strlen(s); 232 233 if (j < 0) { 234 j = 0; 235 } 236 if (j > 126) { 237 j = 126; 238 } 239 if (max_len > j) { 240 max_len = j; 241 } 242 totlen = (max_len + 1) * 2; 243 244 p->bLength = totlen; 245 p->bDescriptorType = UDESC_STRING; 246 247 while (max_len--) { 248 USETW2(p->bString[max_len], 0, s[max_len]); 249 } 250 return (totlen); 251} 252 253#if (USB_USE_CONDVAR == 0) 254 255/*------------------------------------------------------------------------* 256 * usb2_cv_init - wrapper function 257 *------------------------------------------------------------------------*/ 258void 259usb2_cv_init(struct cv *cv, const char *desc) 260{ 261 cv_init(cv, desc); 262 return; 263} 264 265/*------------------------------------------------------------------------* 266 * usb2_cv_destroy - wrapper function 267 *------------------------------------------------------------------------*/ 268void 269usb2_cv_destroy(struct cv *cv) 270{ 271 cv_destroy(cv); 272 return; 273} 274 275/*------------------------------------------------------------------------* 276 * usb2_cv_wait - wrapper function 277 *------------------------------------------------------------------------*/ 278void 279usb2_cv_wait(struct cv *cv, struct mtx *mtx) 280{ 281 int err; 282 283 err = usb2_msleep(cv, mtx, 0, cv_wmesg(cv), 0); 284 return; 285} 286 287/*------------------------------------------------------------------------* 288 * usb2_cv_wait_sig - wrapper function 289 *------------------------------------------------------------------------*/ 290int 291usb2_cv_wait_sig(struct cv *cv, struct mtx *mtx) 292{ 293 int err; 294 295 err = usb2_msleep(cv, mtx, PCATCH, cv_wmesg(cv), 0); 296 return (err); 297} 298 299/*------------------------------------------------------------------------* 300 * usb2_cv_timedwait - wrapper function 301 *------------------------------------------------------------------------*/ 302int 303usb2_cv_timedwait(struct cv *cv, struct mtx *mtx, int timo) 304{ 305 int err; 306 307 if (timo == 0) 308 timo = 1; /* zero means no timeout */ 309 err = usb2_msleep(cv, mtx, 0, cv_wmesg(cv), timo); 310 return (err); 311} 312 313/*------------------------------------------------------------------------* 314 * usb2_cv_signal - wrapper function 315 *------------------------------------------------------------------------*/ 316void 317usb2_cv_signal(struct cv *cv) 318{ 319 wakeup_one(cv); 320 return; 321} 322 323/*------------------------------------------------------------------------* 324 * usb2_cv_broadcast - wrapper function 325 *------------------------------------------------------------------------*/ 326void 327usb2_cv_broadcast(struct cv *cv) 328{ 329 wakeup(cv); 330 return; 331} 332 333/*------------------------------------------------------------------------* 334 * usb2_msleep - wrapper function 335 *------------------------------------------------------------------------*/ 336static int 337usb2_msleep(void *chan, struct mtx *mtx, int priority, const char *wmesg, 338 int timo) 339{ 340 int err; 341 342 if (mtx == &Giant) { 343 err = tsleep(chan, priority, wmesg, timo); 344 } else { 345#ifdef mtx_sleep 346 err = mtx_sleep(chan, mtx, priority, wmesg, timo); 347#else 348 err = msleep(chan, mtx, priority, wmesg, timo); 349#endif 350 } 351 return (err); 352} 353 354#endif 355