usb_util.c revision 188411
1/* $FreeBSD: head/sys/dev/usb2/core/usb2_util.c 188411 2009-02-09 21:56:33Z 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/usb2/include/usb2_defs.h>
28#include <dev/usb2/include/usb2_mfunc.h>
29#include <dev/usb2/include/usb2_error.h>
30#include <dev/usb2/include/usb2_standard.h>
31
32#include <dev/usb2/core/usb2_core.h>
33#include <dev/usb2/core/usb2_util.h>
34#include <dev/usb2/core/usb2_process.h>
35#include <dev/usb2/core/usb2_device.h>
36#include <dev/usb2/core/usb2_request.h>
37#include <dev/usb2/core/usb2_busdma.h>
38
39#include <dev/usb2/controller/usb2_controller.h>
40#include <dev/usb2/controller/usb2_bus.h>
41
42/* function prototypes */
43#if (USB_USE_CONDVAR == 0)
44static int usb2_msleep(void *chan, struct mtx *mtx, int priority, const char *wmesg, int timo);
45
46#endif
47
48/*------------------------------------------------------------------------*
49 * device_delete_all_children - delete all children of a device
50 *------------------------------------------------------------------------*/
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
71/*------------------------------------------------------------------------*
72 *	device_set_usb2_desc
73 *
74 * This function can be called at probe or attach to set the USB
75 * device supplied textual description for the given device.
76 *------------------------------------------------------------------------*/
77void
78device_set_usb2_desc(device_t dev)
79{
80	struct usb2_attach_arg *uaa;
81	struct usb2_device *udev;
82	struct usb2_interface *iface;
83	char *temp_p;
84	usb2_error_t err;
85
86	if (dev == NULL) {
87		/* should not happen */
88		return;
89	}
90	uaa = device_get_ivars(dev);
91	if (uaa == NULL) {
92		/* can happen if called at the wrong time */
93		return;
94	}
95	udev = uaa->device;
96	iface = uaa->iface;
97
98	if ((iface == NULL) ||
99	    (iface->idesc == NULL) ||
100	    (iface->idesc->iInterface == 0)) {
101		err = USB_ERR_INVAL;
102	} else {
103		err = 0;
104	}
105
106	temp_p = (char *)udev->bus->scratch[0].data;
107
108	if (!err) {
109		/* try to get the interface string ! */
110		err = usb2_req_get_string_any
111		    (udev, NULL, temp_p,
112		    sizeof(udev->bus->scratch), iface->idesc->iInterface);
113	}
114	if (err) {
115		/* use default description */
116		usb2_devinfo(udev, temp_p,
117		    sizeof(udev->bus->scratch));
118	}
119	device_set_desc_copy(dev, temp_p);
120	device_printf(dev, "<%s> on %s\n", temp_p,
121	    device_get_nameunit(udev->bus->bdev));
122}
123
124/*------------------------------------------------------------------------*
125 *	 usb2_pause_mtx - factored out code
126 *
127 * This function will delay the code by the passed number of system
128 * ticks. The passed mutex "mtx" will be dropped while waiting, if
129 * "mtx" is not NULL.
130 *------------------------------------------------------------------------*/
131void
132usb2_pause_mtx(struct mtx *mtx, int _ticks)
133{
134	if (mtx != NULL)
135		mtx_unlock(mtx);
136
137	if (cold) {
138		/* convert to milliseconds */
139		_ticks = (_ticks * 1000) / hz;
140		/* convert to microseconds, rounded up */
141		_ticks = (_ticks + 1) * 1000;
142		DELAY(_ticks);
143
144	} else {
145
146		/*
147		 * Add one to the number of ticks so that we don't return
148		 * too early!
149		 */
150		_ticks++;
151
152		if (pause("USBWAIT", _ticks)) {
153			/* ignore */
154		}
155	}
156	if (mtx != NULL)
157		mtx_lock(mtx);
158}
159
160/*------------------------------------------------------------------------*
161 *	usb2_printBCD
162 *
163 * This function will print the version number "bcd" to the string
164 * pointed to by "p" having a maximum length of "p_len" bytes
165 * including the terminating zero.
166 *------------------------------------------------------------------------*/
167void
168usb2_printBCD(char *p, uint16_t p_len, uint16_t bcd)
169{
170	if (snprintf(p, p_len, "%x.%02x", bcd >> 8, bcd & 0xff)) {
171		/* ignore any errors */
172	}
173}
174
175/*------------------------------------------------------------------------*
176 *	usb2_trim_spaces
177 *
178 * This function removes spaces at the beginning and the end of the string
179 * pointed to by the "p" argument.
180 *------------------------------------------------------------------------*/
181void
182usb2_trim_spaces(char *p)
183{
184	char *q;
185	char *e;
186
187	if (p == NULL)
188		return;
189	q = e = p;
190	while (*q == ' ')		/* skip leading spaces */
191		q++;
192	while ((*p = *q++))		/* copy string */
193		if (*p++ != ' ')	/* remember last non-space */
194			e = p;
195	*e = 0;				/* kill trailing spaces */
196}
197
198/*------------------------------------------------------------------------*
199 *	usb2_get_devid
200 *
201 * This function returns the USB Vendor and Product ID like a 32-bit
202 * unsigned integer.
203 *------------------------------------------------------------------------*/
204uint32_t
205usb2_get_devid(device_t dev)
206{
207	struct usb2_attach_arg *uaa = device_get_ivars(dev);
208
209	return ((uaa->info.idVendor << 16) | (uaa->info.idProduct));
210}
211
212/*------------------------------------------------------------------------*
213 *	usb2_make_str_desc - convert an ASCII string into a UNICODE string
214 *------------------------------------------------------------------------*/
215uint8_t
216usb2_make_str_desc(void *ptr, uint16_t max_len, const char *s)
217{
218	struct usb2_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
250#if (USB_USE_CONDVAR == 0)
251
252/*------------------------------------------------------------------------*
253 *	usb2_cv_init - wrapper function
254 *------------------------------------------------------------------------*/
255void
256usb2_cv_init(struct cv *cv, const char *desc)
257{
258	cv_init(cv, desc);
259}
260
261/*------------------------------------------------------------------------*
262 *	usb2_cv_destroy - wrapper function
263 *------------------------------------------------------------------------*/
264void
265usb2_cv_destroy(struct cv *cv)
266{
267	cv_destroy(cv);
268}
269
270/*------------------------------------------------------------------------*
271 *	usb2_cv_wait - wrapper function
272 *------------------------------------------------------------------------*/
273void
274usb2_cv_wait(struct cv *cv, struct mtx *mtx)
275{
276	int err;
277
278	err = usb2_msleep(cv, mtx, 0, cv_wmesg(cv), 0);
279}
280
281/*------------------------------------------------------------------------*
282 *	usb2_cv_wait_sig - wrapper function
283 *------------------------------------------------------------------------*/
284int
285usb2_cv_wait_sig(struct cv *cv, struct mtx *mtx)
286{
287	int err;
288
289	err = usb2_msleep(cv, mtx, PCATCH, cv_wmesg(cv), 0);
290	return (err);
291}
292
293/*------------------------------------------------------------------------*
294 *	usb2_cv_timedwait - wrapper function
295 *------------------------------------------------------------------------*/
296int
297usb2_cv_timedwait(struct cv *cv, struct mtx *mtx, int timo)
298{
299	int err;
300
301	if (timo == 0)
302		timo = 1;		/* zero means no timeout */
303	err = usb2_msleep(cv, mtx, 0, cv_wmesg(cv), timo);
304	return (err);
305}
306
307/*------------------------------------------------------------------------*
308 *	usb2_cv_signal - wrapper function
309 *------------------------------------------------------------------------*/
310void
311usb2_cv_signal(struct cv *cv)
312{
313	wakeup_one(cv);
314}
315
316/*------------------------------------------------------------------------*
317 *	usb2_cv_broadcast - wrapper function
318 *------------------------------------------------------------------------*/
319void
320usb2_cv_broadcast(struct cv *cv)
321{
322	wakeup(cv);
323}
324
325/*------------------------------------------------------------------------*
326 *	usb2_msleep - wrapper function
327 *------------------------------------------------------------------------*/
328static int
329usb2_msleep(void *chan, struct mtx *mtx, int priority, const char *wmesg,
330    int timo)
331{
332	int err;
333
334	if (mtx == &Giant) {
335		err = tsleep(chan, priority, wmesg, timo);
336	} else {
337#ifdef mtx_sleep
338		err = mtx_sleep(chan, mtx, priority, wmesg, timo);
339#else
340		err = msleep(chan, mtx, priority, wmesg, timo);
341#endif
342	}
343	return (err);
344}
345
346#endif
347