1 /* $FreeBSD$ */ 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 <sys/stdint.h> 28 #include <sys/stddef.h> 29 #include <sys/param.h> 30 #include <sys/queue.h> 31 #include <sys/types.h> 32 #include <sys/systm.h> 33 #include <sys/kernel.h> 34 #include <sys/bus.h> 35 #include <sys/linker_set.h> 36 #include <sys/module.h> 37 #include <sys/lock.h> 38 #include <sys/mutex.h> 39 #include <sys/condvar.h> 40 #include <sys/sysctl.h> 41 #include <sys/sx.h> 42 #include <sys/unistd.h> 43 #include <sys/callout.h> 44 #include <sys/malloc.h> 45 #include <sys/priv.h> 46 47 #include <dev/usb/usb.h> 48 #include <dev/usb/usbdi.h> 49 #include <dev/usb/usbdi_util.h> 50 51 #include <dev/usb/usb_core.h> 52 #include <dev/usb/usb_util.h> 53 #include <dev/usb/usb_process.h> 54 #include <dev/usb/usb_device.h> 55 #include <dev/usb/usb_request.h> 56 #include <dev/usb/usb_busdma.h> 57 58 #include <dev/usb/usb_controller.h> 59 #include <dev/usb/usb_bus.h> 60 61 /*------------------------------------------------------------------------* 62 * device_delete_all_children - delete all children of a device 63 *------------------------------------------------------------------------*/ 64 #ifndef device_delete_all_children 65 int 66 device_delete_all_children(device_t dev) 67 { 68 device_t *devlist; 69 int devcount; 70 int error; 71 72 error = device_get_children(dev, &devlist, &devcount); 73 if (error == 0) { 74 while (devcount-- > 0) { 75 error = device_delete_child(dev, devlist[devcount]); 76 if (error) { 77 break; 78 } 79 } 80 free(devlist, M_TEMP); 81 } 82 return (error); 83 } 84 #endif 85 86 /*------------------------------------------------------------------------* 87 * device_set_usb_desc 88 * 89 * This function can be called at probe or attach to set the USB 90 * device supplied textual description for the given device. 91 *------------------------------------------------------------------------*/ 92 void 93 device_set_usb_desc(device_t dev) 94 { 95 struct usb_attach_arg *uaa; 96 struct usb_device *udev; 97 struct usb_interface *iface; 98 char *temp_p; 99 usb_error_t err; 100 101 if (dev == NULL) { 102 /* should not happen */ 103 return; 104 } 105 uaa = device_get_ivars(dev); 106 if (uaa == NULL) { 107 /* can happen if called at the wrong time */ 108 return; 109 } 110 udev = uaa->device; 111 iface = uaa->iface; 112 113 if ((iface == NULL) || 114 (iface->idesc == NULL) || 115 (iface->idesc->iInterface == 0)) { 116 err = USB_ERR_INVAL; 117 } else { 118 err = 0; 119 } 120 121 temp_p = (char *)udev->bus->scratch[0].data; 122 123 if (!err) { 124 /* try to get the interface string ! */ 125 err = usbd_req_get_string_any 126 (udev, NULL, temp_p, 127 sizeof(udev->bus->scratch), iface->idesc->iInterface); 128 } 129 if (err) { 130 /* use default description */ 131 usb_devinfo(udev, temp_p, 132 sizeof(udev->bus->scratch)); 133 } 134 device_set_desc_copy(dev, temp_p); 135 device_printf(dev, "<%s> on %s\n", temp_p, 136 device_get_nameunit(udev->bus->bdev)); 137 } 138 139 /*------------------------------------------------------------------------* 140 * usb_pause_mtx - factored out code 141 * 142 * This function will delay the code by the passed number of system 143 * ticks. The passed mutex "mtx" will be dropped while waiting, if 144 * "mtx" is not NULL. 145 *------------------------------------------------------------------------*/ 146 void 147 usb_pause_mtx(struct mtx *mtx, int _ticks) 148 { 149 if (mtx != NULL) 150 mtx_unlock(mtx); 151 152 if (cold) { 153 /* convert to milliseconds */ 154 _ticks = (_ticks * 1000) / hz; 155 /* convert to microseconds, rounded up */ 156 _ticks = (_ticks + 1) * 1000; 157 DELAY(_ticks); 158 159 } else { 160 161 /* 162 * Add one to the number of ticks so that we don't return 163 * too early! 164 */ 165 _ticks++; 166 167 if (pause("USBWAIT", _ticks)) { 168 /* ignore */ 169 } 170 } 171 if (mtx != NULL) 172 mtx_lock(mtx); 173 } 174 175 /*------------------------------------------------------------------------* 176 * usb_printbcd 177 * 178 * This function will print the version number "bcd" to the string 179 * pointed to by "p" having a maximum length of "p_len" bytes 180 * including the terminating zero. 181 *------------------------------------------------------------------------*/ 182 void 183 usb_printbcd(char *p, uint16_t p_len, uint16_t bcd) 184 { 185 if (snprintf(p, p_len, "%x.%02x", bcd >> 8, bcd & 0xff)) { 186 /* ignore any errors */ 187 } 188 } 189 190 /*------------------------------------------------------------------------* 191 * usb_trim_spaces 192 * 193 * This function removes spaces at the beginning and the end of the string 194 * pointed to by the "p" argument. 195 *------------------------------------------------------------------------*/ 196 void 197 usb_trim_spaces(char *p) 198 { 199 char *q; 200 char *e; 201 202 if (p == NULL) 203 return; 204 q = e = p; 205 while (*q == ' ') /* skip leading spaces */ 206 q++; 207 while ((*p = *q++)) /* copy string */ 208 if (*p++ != ' ') /* remember last non-space */ 209 e = p; 210 *e = 0; /* kill trailing spaces */ 211 } 212 213 /*------------------------------------------------------------------------* 214 * usb_make_str_desc - convert an ASCII string into a UNICODE string 215 *------------------------------------------------------------------------*/ 216 uint8_t 217 usb_make_str_desc(void *ptr, uint16_t max_len, const char *s) 218 { 219 struct usb_string_descriptor *p = ptr; 220 uint8_t totlen; 221 int j; 222 223 if (max_len < 2) { 224 /* invalid length */ 225 return (0); 226 } 227 max_len = ((max_len / 2) - 1); 228 229 j = strlen(s); 230 231 if (j < 0) { 232 j = 0; 233 } 234 if (j > 126) { 235 j = 126; 236 } 237 if (max_len > j) { 238 max_len = j; 239 } 240 totlen = (max_len + 1) * 2; 241 242 p->bLength = totlen; 243 p->bDescriptorType = UDESC_STRING; 244 245 while (max_len--) { 246 USETW2(p->bString[max_len], 0, s[max_len]); 247 } 248 return (totlen); 249 } 250