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