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