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