Deleted Added
full compact
usb_util.c (193045) usb_util.c (194227)
1/* $FreeBSD: head/sys/dev/usb/usb_util.c 193045 2009-05-29 18:46:57Z thompsa $ */
1/* $FreeBSD: head/sys/dev/usb/usb_util.c 194227 2009-06-15 00:33:18Z thompsa $ */
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
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/* function prototypes */
42#if (USB_HAVE_CONDVAR == 0)
43static int usb2_msleep(void *chan, struct mtx *mtx, int priority, const char *wmesg, int timo);
44
41
45#endif
46
47/*------------------------------------------------------------------------*
48 * device_delete_all_children - delete all children of a device
49 *------------------------------------------------------------------------*/
50#ifndef device_delete_all_children
51int
52device_delete_all_children(device_t dev)
53{
54 device_t *devlist;
55 int devcount;
56 int error;
57
58 error = device_get_children(dev, &devlist, &devcount);
59 if (error == 0) {
60 while (devcount-- > 0) {
61 error = device_delete_child(dev, devlist[devcount]);
62 if (error) {
63 break;
64 }
65 }
66 free(devlist, M_TEMP);
67 }
68 return (error);
69}
70#endif
71
72/*------------------------------------------------------------------------*
73 * device_set_usb2_desc
74 *
75 * This function can be called at probe or attach to set the USB
76 * device supplied textual description for the given device.
77 *------------------------------------------------------------------------*/
78void
79device_set_usb2_desc(device_t dev)
80{
81 struct usb_attach_arg *uaa;
82 struct usb_device *udev;
83 struct usb_interface *iface;
84 char *temp_p;
85 usb_error_t err;
86
87 if (dev == NULL) {
88 /* should not happen */
89 return;
90 }
91 uaa = device_get_ivars(dev);
92 if (uaa == NULL) {
93 /* can happen if called at the wrong time */
94 return;
95 }
96 udev = uaa->device;
97 iface = uaa->iface;
98
99 if ((iface == NULL) ||
100 (iface->idesc == NULL) ||
101 (iface->idesc->iInterface == 0)) {
102 err = USB_ERR_INVAL;
103 } else {
104 err = 0;
105 }
106
107 temp_p = (char *)udev->bus->scratch[0].data;
108
109 if (!err) {
110 /* try to get the interface string ! */
111 err = usb2_req_get_string_any
112 (udev, NULL, temp_p,
113 sizeof(udev->bus->scratch), iface->idesc->iInterface);
114 }
115 if (err) {
116 /* use default description */
117 usb2_devinfo(udev, temp_p,
118 sizeof(udev->bus->scratch));
119 }
120 device_set_desc_copy(dev, temp_p);
121 device_printf(dev, "<%s> on %s\n", temp_p,
122 device_get_nameunit(udev->bus->bdev));
123}
124
125/*------------------------------------------------------------------------*
126 * usb2_pause_mtx - factored out code
127 *
128 * This function will delay the code by the passed number of system
129 * ticks. The passed mutex "mtx" will be dropped while waiting, if
130 * "mtx" is not NULL.
131 *------------------------------------------------------------------------*/
132void
133usb2_pause_mtx(struct mtx *mtx, int _ticks)
134{
135 if (mtx != NULL)
136 mtx_unlock(mtx);
137
138 if (cold) {
139 /* convert to milliseconds */
140 _ticks = (_ticks * 1000) / hz;
141 /* convert to microseconds, rounded up */
142 _ticks = (_ticks + 1) * 1000;
143 DELAY(_ticks);
144
145 } else {
146
147 /*
148 * Add one to the number of ticks so that we don't return
149 * too early!
150 */
151 _ticks++;
152
153 if (pause("USBWAIT", _ticks)) {
154 /* ignore */
155 }
156 }
157 if (mtx != NULL)
158 mtx_lock(mtx);
159}
160
161/*------------------------------------------------------------------------*
162 * usb2_printBCD
163 *
164 * This function will print the version number "bcd" to the string
165 * pointed to by "p" having a maximum length of "p_len" bytes
166 * including the terminating zero.
167 *------------------------------------------------------------------------*/
168void
169usb2_printBCD(char *p, uint16_t p_len, uint16_t bcd)
170{
171 if (snprintf(p, p_len, "%x.%02x", bcd >> 8, bcd & 0xff)) {
172 /* ignore any errors */
173 }
174}
175
176/*------------------------------------------------------------------------*
177 * usb2_trim_spaces
178 *
179 * This function removes spaces at the beginning and the end of the string
180 * pointed to by the "p" argument.
181 *------------------------------------------------------------------------*/
182void
183usb2_trim_spaces(char *p)
184{
185 char *q;
186 char *e;
187
188 if (p == NULL)
189 return;
190 q = e = p;
191 while (*q == ' ') /* skip leading spaces */
192 q++;
193 while ((*p = *q++)) /* copy string */
194 if (*p++ != ' ') /* remember last non-space */
195 e = p;
196 *e = 0; /* kill trailing spaces */
197}
198
199/*------------------------------------------------------------------------*
200 * usb2_make_str_desc - convert an ASCII string into a UNICODE string
201 *------------------------------------------------------------------------*/
202uint8_t
203usb2_make_str_desc(void *ptr, uint16_t max_len, const char *s)
204{
205 struct usb_string_descriptor *p = ptr;
206 uint8_t totlen;
207 int j;
208
209 if (max_len < 2) {
210 /* invalid length */
211 return (0);
212 }
213 max_len = ((max_len / 2) - 1);
214
215 j = strlen(s);
216
217 if (j < 0) {
218 j = 0;
219 }
220 if (j > 126) {
221 j = 126;
222 }
223 if (max_len > j) {
224 max_len = j;
225 }
226 totlen = (max_len + 1) * 2;
227
228 p->bLength = totlen;
229 p->bDescriptorType = UDESC_STRING;
230
231 while (max_len--) {
232 USETW2(p->bString[max_len], 0, s[max_len]);
233 }
234 return (totlen);
235}
42/*------------------------------------------------------------------------*
43 * device_delete_all_children - delete all children of a device
44 *------------------------------------------------------------------------*/
45#ifndef device_delete_all_children
46int
47device_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_usb2_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 *------------------------------------------------------------------------*/
73void
74device_set_usb2_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 = usb2_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 usb2_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 * usb2_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 *------------------------------------------------------------------------*/
127void
128usb2_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 * usb2_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 *------------------------------------------------------------------------*/
163void
164usb2_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 * usb2_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 *------------------------------------------------------------------------*/
177void
178usb2_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 * usb2_make_str_desc - convert an ASCII string into a UNICODE string
196 *------------------------------------------------------------------------*/
197uint8_t
198usb2_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}
236
237#if (USB_HAVE_CONDVAR == 0)
238
239/*------------------------------------------------------------------------*
240 * usb2_cv_init - wrapper function
241 *------------------------------------------------------------------------*/
242void
243usb2_cv_init(struct cv *cv, const char *desc)
244{
245 cv_init(cv, desc);
246}
247
248/*------------------------------------------------------------------------*
249 * usb2_cv_destroy - wrapper function
250 *------------------------------------------------------------------------*/
251void
252usb2_cv_destroy(struct cv *cv)
253{
254 cv_destroy(cv);
255}
256
257/*------------------------------------------------------------------------*
258 * usb2_cv_wait - wrapper function
259 *------------------------------------------------------------------------*/
260void
261usb2_cv_wait(struct cv *cv, struct mtx *mtx)
262{
263 int err;
264
265 err = usb2_msleep(cv, mtx, 0, cv_wmesg(cv), 0);
266}
267
268/*------------------------------------------------------------------------*
269 * usb2_cv_wait_sig - wrapper function
270 *------------------------------------------------------------------------*/
271int
272usb2_cv_wait_sig(struct cv *cv, struct mtx *mtx)
273{
274 int err;
275
276 err = usb2_msleep(cv, mtx, PCATCH, cv_wmesg(cv), 0);
277 return (err);
278}
279
280/*------------------------------------------------------------------------*
281 * usb2_cv_timedwait - wrapper function
282 *------------------------------------------------------------------------*/
283int
284usb2_cv_timedwait(struct cv *cv, struct mtx *mtx, int timo)
285{
286 int err;
287
288 if (timo == 0)
289 timo = 1; /* zero means no timeout */
290 err = usb2_msleep(cv, mtx, 0, cv_wmesg(cv), timo);
291 return (err);
292}
293
294/*------------------------------------------------------------------------*
295 * usb2_cv_signal - wrapper function
296 *------------------------------------------------------------------------*/
297void
298usb2_cv_signal(struct cv *cv)
299{
300 wakeup_one(cv);
301}
302
303/*------------------------------------------------------------------------*
304 * usb2_cv_broadcast - wrapper function
305 *------------------------------------------------------------------------*/
306void
307usb2_cv_broadcast(struct cv *cv)
308{
309 wakeup(cv);
310}
311
312/*------------------------------------------------------------------------*
313 * usb2_msleep - wrapper function
314 *------------------------------------------------------------------------*/
315static int
316usb2_msleep(void *chan, struct mtx *mtx, int priority, const char *wmesg,
317 int timo)
318{
319 int err;
320
321 if (mtx == &Giant) {
322 err = tsleep(chan, priority, wmesg, timo);
323 } else {
324#ifdef mtx_sleep
325 err = mtx_sleep(chan, mtx, priority, wmesg, timo);
326#else
327 err = msleep(chan, mtx, priority, wmesg, timo);
328#endif
329 }
330 return (err);
331}
332
333#endif