1 /* $FreeBSD$ */ 2 /*- 3 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 4 * 5 * Copyright (c) 2008-2022 Hans Petter Selasky 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #ifdef USB_GLOBAL_INCLUDE_FILE 30 #include USB_GLOBAL_INCLUDE_FILE 31 #else 32 #include <sys/stdint.h> 33 #include <sys/stddef.h> 34 #include <sys/param.h> 35 #include <sys/queue.h> 36 #include <sys/types.h> 37 #include <sys/systm.h> 38 #include <sys/kernel.h> 39 #include <sys/bus.h> 40 #include <sys/module.h> 41 #include <sys/lock.h> 42 #include <sys/mutex.h> 43 #include <sys/condvar.h> 44 #include <sys/sysctl.h> 45 #include <sys/sx.h> 46 #include <sys/unistd.h> 47 #include <sys/callout.h> 48 #include <sys/malloc.h> 49 #include <sys/priv.h> 50 51 #include <dev/usb/usb.h> 52 #include <dev/usb/usbdi.h> 53 #include <dev/usb/usbdi_util.h> 54 55 #include <dev/usb/usb_core.h> 56 #include <dev/usb/usb_util.h> 57 #include <dev/usb/usb_process.h> 58 #include <dev/usb/usb_device.h> 59 #include <dev/usb/usb_request.h> 60 #include <dev/usb/usb_busdma.h> 61 62 #include <dev/usb/usb_controller.h> 63 #include <dev/usb/usb_bus.h> 64 #endif /* USB_GLOBAL_INCLUDE_FILE */ 65 66 /*------------------------------------------------------------------------* 67 * device_set_usb_desc 68 * 69 * This function can be called at probe or attach to set the USB 70 * device supplied textual description for the given device. 71 *------------------------------------------------------------------------*/ 72 void 73 device_set_usb_desc(device_t dev) 74 { 75 struct usb_attach_arg *uaa; 76 struct usb_device *udev; 77 struct usb_interface *iface; 78 char *temp_p; 79 usb_error_t err; 80 uint8_t do_unlock; 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 /* Protect scratch area */ 103 do_unlock = usbd_ctrl_lock(udev); 104 105 temp_p = (char *)udev->scratch.data; 106 107 if (err == 0) { 108 /* try to get the interface string ! */ 109 err = usbd_req_get_string_any(udev, NULL, temp_p, 110 sizeof(udev->scratch.data), 111 iface->idesc->iInterface); 112 } 113 if (err != 0) { 114 /* use default description */ 115 usb_devinfo(udev, temp_p, 116 sizeof(udev->scratch.data)); 117 } 118 119 if (do_unlock) 120 usbd_ctrl_unlock(udev); 121 122 device_set_desc_copy(dev, temp_p); 123 device_printf(dev, "<%s> on %s\n", temp_p, 124 device_get_nameunit(udev->bus->bdev)); 125 } 126 127 /*------------------------------------------------------------------------* 128 * usb_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 different from NULL. 133 *------------------------------------------------------------------------*/ 134 void 135 usb_pause_mtx(struct mtx *mtx, int timo) 136 { 137 if (mtx != NULL) 138 mtx_unlock(mtx); 139 140 /* 141 * Add one tick to the timeout so that we don't return too 142 * early! Note that pause() will assert that the passed 143 * timeout is positive and non-zero! 144 */ 145 pause("USBWAIT", timo + 1); 146 147 if (mtx != NULL) 148 mtx_lock(mtx); 149 } 150 151 /*------------------------------------------------------------------------* 152 * usb_printbcd 153 * 154 * This function will print the version number "bcd" to the string 155 * pointed to by "p" having a maximum length of "p_len" bytes 156 * including the terminating zero. 157 *------------------------------------------------------------------------*/ 158 void 159 usb_printbcd(char *p, uint16_t p_len, uint16_t bcd) 160 { 161 if (snprintf(p, p_len, "%x.%02x", bcd >> 8, bcd & 0xff)) { 162 /* ignore any errors */ 163 } 164 } 165 166 /*------------------------------------------------------------------------* 167 * usb_trim_spaces 168 * 169 * This function removes spaces at the beginning and the end of the string 170 * pointed to by the "p" argument. 171 *------------------------------------------------------------------------*/ 172 void 173 usb_trim_spaces(char *p) 174 { 175 char *q; 176 char *e; 177 178 if (p == NULL) 179 return; 180 q = e = p; 181 while (*q == ' ') /* skip leading spaces */ 182 q++; 183 while ((*p = *q++)) /* copy string */ 184 if (*p++ != ' ') /* remember last non-space */ 185 e = p; 186 *e = 0; /* kill trailing spaces */ 187 } 188 189 /*------------------------------------------------------------------------* 190 * usb_make_str_desc - convert an ASCII string into a UNICODE string 191 *------------------------------------------------------------------------*/ 192 uint8_t 193 usb_make_str_desc(void *ptr, uint16_t max_len, const char *s) 194 { 195 struct usb_string_descriptor *p = ptr; 196 uint8_t totlen; 197 int j; 198 199 if (max_len < 2) { 200 /* invalid length */ 201 return (0); 202 } 203 max_len = ((max_len / 2) - 1); 204 205 j = strlen(s); 206 207 if (j < 0) { 208 j = 0; 209 } 210 if (j > 126) { 211 j = 126; 212 } 213 if (max_len > j) { 214 max_len = j; 215 } 216 totlen = (max_len + 1) * 2; 217 218 p->bLength = totlen; 219 p->bDescriptorType = UDESC_STRING; 220 221 while (max_len--) { 222 USETW2(p->bString[max_len], 0, s[max_len]); 223 } 224 return (totlen); 225 } 226 227 /*------------------------------------------------------------------------* 228 * usb_check_request - prevent damaging USB requests 229 * 230 * Return values: 231 * 0: Access allowed 232 * Else: No access 233 *------------------------------------------------------------------------*/ 234 #if USB_HAVE_UGEN 235 int 236 usb_check_request(struct usb_device *udev, struct usb_device_request *req) 237 { 238 struct usb_endpoint *ep; 239 int error; 240 241 /* 242 * Avoid requests that would damage the bus integrity: 243 */ 244 if (((req->bmRequestType == UT_WRITE_DEVICE) && 245 (req->bRequest == UR_SET_ADDRESS)) || 246 ((req->bmRequestType == UT_WRITE_DEVICE) && 247 (req->bRequest == UR_SET_CONFIG)) || 248 ((req->bmRequestType == UT_WRITE_INTERFACE) && 249 (req->bRequest == UR_SET_INTERFACE))) { 250 /* 251 * These requests can be useful for testing USB drivers. 252 */ 253 error = priv_check(curthread, PRIV_DRIVER); 254 if (error) 255 return (error); 256 } 257 258 /* 259 * Special case - handle clearing of stall 260 */ 261 if (req->bmRequestType == UT_WRITE_ENDPOINT) { 262 ep = usbd_get_ep_by_addr(udev, req->wIndex[0]); 263 if (ep == NULL) 264 return (EINVAL); 265 if ((req->bRequest == UR_CLEAR_FEATURE) && 266 (UGETW(req->wValue) == UF_ENDPOINT_HALT)) 267 usbd_clear_data_toggle(udev, ep); 268 } 269 270 /* TODO: add more checks to verify the interface index */ 271 272 return (0); 273 } 274 #endif 275