usb_util.c revision 188942
1184610Salfred/* $FreeBSD: head/sys/dev/usb/usb_util.c 188942 2009-02-23 18:31:00Z thompsa $ */
2184610Salfred/*-
3184610Salfred * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
4184610Salfred *
5184610Salfred * Redistribution and use in source and binary forms, with or without
6184610Salfred * modification, are permitted provided that the following conditions
7184610Salfred * are met:
8184610Salfred * 1. Redistributions of source code must retain the above copyright
9184610Salfred *    notice, this list of conditions and the following disclaimer.
10184610Salfred * 2. Redistributions in binary form must reproduce the above copyright
11184610Salfred *    notice, this list of conditions and the following disclaimer in the
12184610Salfred *    documentation and/or other materials provided with the distribution.
13184610Salfred *
14184610Salfred * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15184610Salfred * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16184610Salfred * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17184610Salfred * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18184610Salfred * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19184610Salfred * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20184610Salfred * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21184610Salfred * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22184610Salfred * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23184610Salfred * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24184610Salfred * SUCH DAMAGE.
25184610Salfred */
26184610Salfred
27188942Sthompsa#include <dev/usb/usb_defs.h>
28188942Sthompsa#include <dev/usb/usb_mfunc.h>
29188942Sthompsa#include <dev/usb/usb_error.h>
30188942Sthompsa#include <dev/usb/usb.h>
31184610Salfred
32188942Sthompsa#include <dev/usb/usb_core.h>
33188942Sthompsa#include <dev/usb/usb_util.h>
34188942Sthompsa#include <dev/usb/usb_process.h>
35188942Sthompsa#include <dev/usb/usb_device.h>
36188942Sthompsa#include <dev/usb/usb_request.h>
37188942Sthompsa#include <dev/usb/usb_busdma.h>
38184610Salfred
39188942Sthompsa#include <dev/usb/usb_controller.h>
40188942Sthompsa#include <dev/usb/usb_bus.h>
41184610Salfred
42184610Salfred/* function prototypes */
43184610Salfred#if (USB_USE_CONDVAR == 0)
44184610Salfredstatic int usb2_msleep(void *chan, struct mtx *mtx, int priority, const char *wmesg, int timo);
45184610Salfred
46184610Salfred#endif
47184610Salfred
48184610Salfred/*------------------------------------------------------------------------*
49184610Salfred * device_delete_all_children - delete all children of a device
50184610Salfred *------------------------------------------------------------------------*/
51184610Salfredint
52184610Salfreddevice_delete_all_children(device_t dev)
53184610Salfred{
54184610Salfred	device_t *devlist;
55184610Salfred	int devcount;
56184610Salfred	int error;
57184610Salfred
58184610Salfred	error = device_get_children(dev, &devlist, &devcount);
59184610Salfred	if (error == 0) {
60184610Salfred		while (devcount-- > 0) {
61184610Salfred			error = device_delete_child(dev, devlist[devcount]);
62184610Salfred			if (error) {
63184610Salfred				break;
64184610Salfred			}
65184610Salfred		}
66184610Salfred		free(devlist, M_TEMP);
67184610Salfred	}
68184610Salfred	return (error);
69184610Salfred}
70184610Salfred
71184610Salfred/*------------------------------------------------------------------------*
72184610Salfred *	device_set_usb2_desc
73184610Salfred *
74184610Salfred * This function can be called at probe or attach to set the USB
75184610Salfred * device supplied textual description for the given device.
76184610Salfred *------------------------------------------------------------------------*/
77184610Salfredvoid
78184610Salfreddevice_set_usb2_desc(device_t dev)
79184610Salfred{
80184610Salfred	struct usb2_attach_arg *uaa;
81184610Salfred	struct usb2_device *udev;
82184610Salfred	struct usb2_interface *iface;
83184610Salfred	char *temp_p;
84184610Salfred	usb2_error_t err;
85184610Salfred
86184610Salfred	if (dev == NULL) {
87184610Salfred		/* should not happen */
88184610Salfred		return;
89184610Salfred	}
90184610Salfred	uaa = device_get_ivars(dev);
91184610Salfred	if (uaa == NULL) {
92185087Salfred		/* can happen if called at the wrong time */
93184610Salfred		return;
94184610Salfred	}
95184610Salfred	udev = uaa->device;
96184610Salfred	iface = uaa->iface;
97184610Salfred
98184610Salfred	if ((iface == NULL) ||
99184610Salfred	    (iface->idesc == NULL) ||
100184610Salfred	    (iface->idesc->iInterface == 0)) {
101184610Salfred		err = USB_ERR_INVAL;
102184610Salfred	} else {
103184610Salfred		err = 0;
104184610Salfred	}
105184610Salfred
106184610Salfred	temp_p = (char *)udev->bus->scratch[0].data;
107184610Salfred
108184610Salfred	if (!err) {
109184610Salfred		/* try to get the interface string ! */
110184610Salfred		err = usb2_req_get_string_any
111184610Salfred		    (udev, NULL, temp_p,
112184610Salfred		    sizeof(udev->bus->scratch), iface->idesc->iInterface);
113184610Salfred	}
114184610Salfred	if (err) {
115184610Salfred		/* use default description */
116184610Salfred		usb2_devinfo(udev, temp_p,
117184610Salfred		    sizeof(udev->bus->scratch));
118184610Salfred	}
119184610Salfred	device_set_desc_copy(dev, temp_p);
120184610Salfred	device_printf(dev, "<%s> on %s\n", temp_p,
121184610Salfred	    device_get_nameunit(udev->bus->bdev));
122184610Salfred}
123184610Salfred
124184610Salfred/*------------------------------------------------------------------------*
125184610Salfred *	 usb2_pause_mtx - factored out code
126184610Salfred *
127188411Sthompsa * This function will delay the code by the passed number of system
128188411Sthompsa * ticks. The passed mutex "mtx" will be dropped while waiting, if
129188411Sthompsa * "mtx" is not NULL.
130184610Salfred *------------------------------------------------------------------------*/
131184610Salfredvoid
132188411Sthompsausb2_pause_mtx(struct mtx *mtx, int _ticks)
133184610Salfred{
134188411Sthompsa	if (mtx != NULL)
135188411Sthompsa		mtx_unlock(mtx);
136188411Sthompsa
137184610Salfred	if (cold) {
138188411Sthompsa		/* convert to milliseconds */
139188411Sthompsa		_ticks = (_ticks * 1000) / hz;
140188411Sthompsa		/* convert to microseconds, rounded up */
141188411Sthompsa		_ticks = (_ticks + 1) * 1000;
142188411Sthompsa		DELAY(_ticks);
143184610Salfred
144184610Salfred	} else {
145184610Salfred
146184610Salfred		/*
147184610Salfred		 * Add one to the number of ticks so that we don't return
148184610Salfred		 * too early!
149184610Salfred		 */
150188411Sthompsa		_ticks++;
151184610Salfred
152188411Sthompsa		if (pause("USBWAIT", _ticks)) {
153184610Salfred			/* ignore */
154184610Salfred		}
155184610Salfred	}
156188411Sthompsa	if (mtx != NULL)
157188411Sthompsa		mtx_lock(mtx);
158184610Salfred}
159184610Salfred
160184610Salfred/*------------------------------------------------------------------------*
161184610Salfred *	usb2_printBCD
162184610Salfred *
163184610Salfred * This function will print the version number "bcd" to the string
164184610Salfred * pointed to by "p" having a maximum length of "p_len" bytes
165184610Salfred * including the terminating zero.
166184610Salfred *------------------------------------------------------------------------*/
167184610Salfredvoid
168184610Salfredusb2_printBCD(char *p, uint16_t p_len, uint16_t bcd)
169184610Salfred{
170184610Salfred	if (snprintf(p, p_len, "%x.%02x", bcd >> 8, bcd & 0xff)) {
171184610Salfred		/* ignore any errors */
172184610Salfred	}
173184610Salfred}
174184610Salfred
175184610Salfred/*------------------------------------------------------------------------*
176184610Salfred *	usb2_trim_spaces
177184610Salfred *
178184610Salfred * This function removes spaces at the beginning and the end of the string
179184610Salfred * pointed to by the "p" argument.
180184610Salfred *------------------------------------------------------------------------*/
181184610Salfredvoid
182184610Salfredusb2_trim_spaces(char *p)
183184610Salfred{
184184610Salfred	char *q;
185184610Salfred	char *e;
186184610Salfred
187184610Salfred	if (p == NULL)
188184610Salfred		return;
189184610Salfred	q = e = p;
190184610Salfred	while (*q == ' ')		/* skip leading spaces */
191184610Salfred		q++;
192184610Salfred	while ((*p = *q++))		/* copy string */
193184610Salfred		if (*p++ != ' ')	/* remember last non-space */
194184610Salfred			e = p;
195184610Salfred	*e = 0;				/* kill trailing spaces */
196184610Salfred}
197184610Salfred
198184610Salfred/*------------------------------------------------------------------------*
199184610Salfred *	usb2_get_devid
200184610Salfred *
201184610Salfred * This function returns the USB Vendor and Product ID like a 32-bit
202184610Salfred * unsigned integer.
203184610Salfred *------------------------------------------------------------------------*/
204184610Salfreduint32_t
205184610Salfredusb2_get_devid(device_t dev)
206184610Salfred{
207184610Salfred	struct usb2_attach_arg *uaa = device_get_ivars(dev);
208184610Salfred
209184610Salfred	return ((uaa->info.idVendor << 16) | (uaa->info.idProduct));
210184610Salfred}
211184610Salfred
212184610Salfred/*------------------------------------------------------------------------*
213184610Salfred *	usb2_make_str_desc - convert an ASCII string into a UNICODE string
214184610Salfred *------------------------------------------------------------------------*/
215184610Salfreduint8_t
216184610Salfredusb2_make_str_desc(void *ptr, uint16_t max_len, const char *s)
217184610Salfred{
218184610Salfred	struct usb2_string_descriptor *p = ptr;
219184610Salfred	uint8_t totlen;
220184610Salfred	int j;
221184610Salfred
222184610Salfred	if (max_len < 2) {
223184610Salfred		/* invalid length */
224184610Salfred		return (0);
225184610Salfred	}
226184610Salfred	max_len = ((max_len / 2) - 1);
227184610Salfred
228184610Salfred	j = strlen(s);
229184610Salfred
230184610Salfred	if (j < 0) {
231184610Salfred		j = 0;
232184610Salfred	}
233184610Salfred	if (j > 126) {
234184610Salfred		j = 126;
235184610Salfred	}
236184610Salfred	if (max_len > j) {
237184610Salfred		max_len = j;
238184610Salfred	}
239184610Salfred	totlen = (max_len + 1) * 2;
240184610Salfred
241184610Salfred	p->bLength = totlen;
242184610Salfred	p->bDescriptorType = UDESC_STRING;
243184610Salfred
244184610Salfred	while (max_len--) {
245184610Salfred		USETW2(p->bString[max_len], 0, s[max_len]);
246184610Salfred	}
247184610Salfred	return (totlen);
248184610Salfred}
249184610Salfred
250184610Salfred#if (USB_USE_CONDVAR == 0)
251184610Salfred
252184610Salfred/*------------------------------------------------------------------------*
253184610Salfred *	usb2_cv_init - wrapper function
254184610Salfred *------------------------------------------------------------------------*/
255184610Salfredvoid
256184610Salfredusb2_cv_init(struct cv *cv, const char *desc)
257184610Salfred{
258184610Salfred	cv_init(cv, desc);
259184610Salfred}
260184610Salfred
261184610Salfred/*------------------------------------------------------------------------*
262184610Salfred *	usb2_cv_destroy - wrapper function
263184610Salfred *------------------------------------------------------------------------*/
264184610Salfredvoid
265184610Salfredusb2_cv_destroy(struct cv *cv)
266184610Salfred{
267184610Salfred	cv_destroy(cv);
268184610Salfred}
269184610Salfred
270184610Salfred/*------------------------------------------------------------------------*
271184610Salfred *	usb2_cv_wait - wrapper function
272184610Salfred *------------------------------------------------------------------------*/
273184610Salfredvoid
274184610Salfredusb2_cv_wait(struct cv *cv, struct mtx *mtx)
275184610Salfred{
276184610Salfred	int err;
277184610Salfred
278184610Salfred	err = usb2_msleep(cv, mtx, 0, cv_wmesg(cv), 0);
279184610Salfred}
280184610Salfred
281184610Salfred/*------------------------------------------------------------------------*
282184610Salfred *	usb2_cv_wait_sig - wrapper function
283184610Salfred *------------------------------------------------------------------------*/
284184610Salfredint
285184610Salfredusb2_cv_wait_sig(struct cv *cv, struct mtx *mtx)
286184610Salfred{
287184610Salfred	int err;
288184610Salfred
289184610Salfred	err = usb2_msleep(cv, mtx, PCATCH, cv_wmesg(cv), 0);
290184610Salfred	return (err);
291184610Salfred}
292184610Salfred
293184610Salfred/*------------------------------------------------------------------------*
294184610Salfred *	usb2_cv_timedwait - wrapper function
295184610Salfred *------------------------------------------------------------------------*/
296184610Salfredint
297184610Salfredusb2_cv_timedwait(struct cv *cv, struct mtx *mtx, int timo)
298184610Salfred{
299184610Salfred	int err;
300184610Salfred
301184610Salfred	if (timo == 0)
302184610Salfred		timo = 1;		/* zero means no timeout */
303184610Salfred	err = usb2_msleep(cv, mtx, 0, cv_wmesg(cv), timo);
304184610Salfred	return (err);
305184610Salfred}
306184610Salfred
307184610Salfred/*------------------------------------------------------------------------*
308184610Salfred *	usb2_cv_signal - wrapper function
309184610Salfred *------------------------------------------------------------------------*/
310184610Salfredvoid
311184610Salfredusb2_cv_signal(struct cv *cv)
312184610Salfred{
313184610Salfred	wakeup_one(cv);
314184610Salfred}
315184610Salfred
316184610Salfred/*------------------------------------------------------------------------*
317184610Salfred *	usb2_cv_broadcast - wrapper function
318184610Salfred *------------------------------------------------------------------------*/
319184610Salfredvoid
320184610Salfredusb2_cv_broadcast(struct cv *cv)
321184610Salfred{
322184610Salfred	wakeup(cv);
323184610Salfred}
324184610Salfred
325184610Salfred/*------------------------------------------------------------------------*
326184610Salfred *	usb2_msleep - wrapper function
327184610Salfred *------------------------------------------------------------------------*/
328184610Salfredstatic int
329184610Salfredusb2_msleep(void *chan, struct mtx *mtx, int priority, const char *wmesg,
330184610Salfred    int timo)
331184610Salfred{
332184610Salfred	int err;
333184610Salfred
334184610Salfred	if (mtx == &Giant) {
335184610Salfred		err = tsleep(chan, priority, wmesg, timo);
336184610Salfred	} else {
337184610Salfred#ifdef mtx_sleep
338184610Salfred		err = mtx_sleep(chan, mtx, priority, wmesg, timo);
339184610Salfred#else
340184610Salfred		err = msleep(chan, mtx, priority, wmesg, timo);
341184610Salfred#endif
342184610Salfred	}
343184610Salfred	return (err);
344184610Salfred}
345184610Salfred
346184610Salfred#endif
347