usb_util.c revision 194677
1184610Salfred/* $FreeBSD: head/sys/dev/usb/usb_util.c 194677 2009-06-23 02:19:59Z 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
27194677Sthompsa#include <sys/stdint.h>
28194677Sthompsa#include <sys/stddef.h>
29194677Sthompsa#include <sys/param.h>
30194677Sthompsa#include <sys/queue.h>
31194677Sthompsa#include <sys/types.h>
32194677Sthompsa#include <sys/systm.h>
33194677Sthompsa#include <sys/kernel.h>
34194677Sthompsa#include <sys/bus.h>
35194677Sthompsa#include <sys/linker_set.h>
36194677Sthompsa#include <sys/module.h>
37194677Sthompsa#include <sys/lock.h>
38194677Sthompsa#include <sys/mutex.h>
39194677Sthompsa#include <sys/condvar.h>
40194677Sthompsa#include <sys/sysctl.h>
41194677Sthompsa#include <sys/sx.h>
42194677Sthompsa#include <sys/unistd.h>
43194677Sthompsa#include <sys/callout.h>
44194677Sthompsa#include <sys/malloc.h>
45194677Sthompsa#include <sys/priv.h>
46194677Sthompsa
47188942Sthompsa#include <dev/usb/usb.h>
48194677Sthompsa#include <dev/usb/usbdi.h>
49194677Sthompsa#include <dev/usb/usbdi_util.h>
50184610Salfred
51188942Sthompsa#include <dev/usb/usb_core.h>
52188942Sthompsa#include <dev/usb/usb_util.h>
53188942Sthompsa#include <dev/usb/usb_process.h>
54188942Sthompsa#include <dev/usb/usb_device.h>
55188942Sthompsa#include <dev/usb/usb_request.h>
56188942Sthompsa#include <dev/usb/usb_busdma.h>
57184610Salfred
58188942Sthompsa#include <dev/usb/usb_controller.h>
59188942Sthompsa#include <dev/usb/usb_bus.h>
60184610Salfred
61184610Salfred/*------------------------------------------------------------------------*
62184610Salfred * device_delete_all_children - delete all children of a device
63184610Salfred *------------------------------------------------------------------------*/
64190180Sthompsa#ifndef device_delete_all_children
65184610Salfredint
66184610Salfreddevice_delete_all_children(device_t dev)
67184610Salfred{
68184610Salfred	device_t *devlist;
69184610Salfred	int devcount;
70184610Salfred	int error;
71184610Salfred
72184610Salfred	error = device_get_children(dev, &devlist, &devcount);
73184610Salfred	if (error == 0) {
74184610Salfred		while (devcount-- > 0) {
75184610Salfred			error = device_delete_child(dev, devlist[devcount]);
76184610Salfred			if (error) {
77184610Salfred				break;
78184610Salfred			}
79184610Salfred		}
80184610Salfred		free(devlist, M_TEMP);
81184610Salfred	}
82184610Salfred	return (error);
83184610Salfred}
84190180Sthompsa#endif
85184610Salfred
86184610Salfred/*------------------------------------------------------------------------*
87194228Sthompsa *	device_set_usb_desc
88184610Salfred *
89184610Salfred * This function can be called at probe or attach to set the USB
90184610Salfred * device supplied textual description for the given device.
91184610Salfred *------------------------------------------------------------------------*/
92184610Salfredvoid
93194228Sthompsadevice_set_usb_desc(device_t dev)
94184610Salfred{
95192984Sthompsa	struct usb_attach_arg *uaa;
96192984Sthompsa	struct usb_device *udev;
97192984Sthompsa	struct usb_interface *iface;
98184610Salfred	char *temp_p;
99193045Sthompsa	usb_error_t err;
100184610Salfred
101184610Salfred	if (dev == NULL) {
102184610Salfred		/* should not happen */
103184610Salfred		return;
104184610Salfred	}
105184610Salfred	uaa = device_get_ivars(dev);
106184610Salfred	if (uaa == NULL) {
107185087Salfred		/* can happen if called at the wrong time */
108184610Salfred		return;
109184610Salfred	}
110184610Salfred	udev = uaa->device;
111184610Salfred	iface = uaa->iface;
112184610Salfred
113184610Salfred	if ((iface == NULL) ||
114184610Salfred	    (iface->idesc == NULL) ||
115184610Salfred	    (iface->idesc->iInterface == 0)) {
116184610Salfred		err = USB_ERR_INVAL;
117184610Salfred	} else {
118184610Salfred		err = 0;
119184610Salfred	}
120184610Salfred
121184610Salfred	temp_p = (char *)udev->bus->scratch[0].data;
122184610Salfred
123184610Salfred	if (!err) {
124184610Salfred		/* try to get the interface string ! */
125194228Sthompsa		err = usbd_req_get_string_any
126184610Salfred		    (udev, NULL, temp_p,
127184610Salfred		    sizeof(udev->bus->scratch), iface->idesc->iInterface);
128184610Salfred	}
129184610Salfred	if (err) {
130184610Salfred		/* use default description */
131194228Sthompsa		usb_devinfo(udev, temp_p,
132184610Salfred		    sizeof(udev->bus->scratch));
133184610Salfred	}
134184610Salfred	device_set_desc_copy(dev, temp_p);
135184610Salfred	device_printf(dev, "<%s> on %s\n", temp_p,
136184610Salfred	    device_get_nameunit(udev->bus->bdev));
137184610Salfred}
138184610Salfred
139184610Salfred/*------------------------------------------------------------------------*
140194228Sthompsa *	 usb_pause_mtx - factored out code
141184610Salfred *
142188411Sthompsa * This function will delay the code by the passed number of system
143188411Sthompsa * ticks. The passed mutex "mtx" will be dropped while waiting, if
144188411Sthompsa * "mtx" is not NULL.
145184610Salfred *------------------------------------------------------------------------*/
146184610Salfredvoid
147194228Sthompsausb_pause_mtx(struct mtx *mtx, int _ticks)
148184610Salfred{
149188411Sthompsa	if (mtx != NULL)
150188411Sthompsa		mtx_unlock(mtx);
151188411Sthompsa
152184610Salfred	if (cold) {
153188411Sthompsa		/* convert to milliseconds */
154188411Sthompsa		_ticks = (_ticks * 1000) / hz;
155188411Sthompsa		/* convert to microseconds, rounded up */
156188411Sthompsa		_ticks = (_ticks + 1) * 1000;
157188411Sthompsa		DELAY(_ticks);
158184610Salfred
159184610Salfred	} else {
160184610Salfred
161184610Salfred		/*
162184610Salfred		 * Add one to the number of ticks so that we don't return
163184610Salfred		 * too early!
164184610Salfred		 */
165188411Sthompsa		_ticks++;
166184610Salfred
167188411Sthompsa		if (pause("USBWAIT", _ticks)) {
168184610Salfred			/* ignore */
169184610Salfred		}
170184610Salfred	}
171188411Sthompsa	if (mtx != NULL)
172188411Sthompsa		mtx_lock(mtx);
173184610Salfred}
174184610Salfred
175184610Salfred/*------------------------------------------------------------------------*
176194228Sthompsa *	usb_printbcd
177184610Salfred *
178184610Salfred * This function will print the version number "bcd" to the string
179184610Salfred * pointed to by "p" having a maximum length of "p_len" bytes
180184610Salfred * including the terminating zero.
181184610Salfred *------------------------------------------------------------------------*/
182184610Salfredvoid
183194228Sthompsausb_printbcd(char *p, uint16_t p_len, uint16_t bcd)
184184610Salfred{
185184610Salfred	if (snprintf(p, p_len, "%x.%02x", bcd >> 8, bcd & 0xff)) {
186184610Salfred		/* ignore any errors */
187184610Salfred	}
188184610Salfred}
189184610Salfred
190184610Salfred/*------------------------------------------------------------------------*
191194228Sthompsa *	usb_trim_spaces
192184610Salfred *
193184610Salfred * This function removes spaces at the beginning and the end of the string
194184610Salfred * pointed to by the "p" argument.
195184610Salfred *------------------------------------------------------------------------*/
196184610Salfredvoid
197194228Sthompsausb_trim_spaces(char *p)
198184610Salfred{
199184610Salfred	char *q;
200184610Salfred	char *e;
201184610Salfred
202184610Salfred	if (p == NULL)
203184610Salfred		return;
204184610Salfred	q = e = p;
205184610Salfred	while (*q == ' ')		/* skip leading spaces */
206184610Salfred		q++;
207184610Salfred	while ((*p = *q++))		/* copy string */
208184610Salfred		if (*p++ != ' ')	/* remember last non-space */
209184610Salfred			e = p;
210184610Salfred	*e = 0;				/* kill trailing spaces */
211184610Salfred}
212184610Salfred
213184610Salfred/*------------------------------------------------------------------------*
214194228Sthompsa *	usb_make_str_desc - convert an ASCII string into a UNICODE string
215184610Salfred *------------------------------------------------------------------------*/
216184610Salfreduint8_t
217194228Sthompsausb_make_str_desc(void *ptr, uint16_t max_len, const char *s)
218184610Salfred{
219192984Sthompsa	struct usb_string_descriptor *p = ptr;
220184610Salfred	uint8_t totlen;
221184610Salfred	int j;
222184610Salfred
223184610Salfred	if (max_len < 2) {
224184610Salfred		/* invalid length */
225184610Salfred		return (0);
226184610Salfred	}
227184610Salfred	max_len = ((max_len / 2) - 1);
228184610Salfred
229184610Salfred	j = strlen(s);
230184610Salfred
231184610Salfred	if (j < 0) {
232184610Salfred		j = 0;
233184610Salfred	}
234184610Salfred	if (j > 126) {
235184610Salfred		j = 126;
236184610Salfred	}
237184610Salfred	if (max_len > j) {
238184610Salfred		max_len = j;
239184610Salfred	}
240184610Salfred	totlen = (max_len + 1) * 2;
241184610Salfred
242184610Salfred	p->bLength = totlen;
243184610Salfred	p->bDescriptorType = UDESC_STRING;
244184610Salfred
245184610Salfred	while (max_len--) {
246184610Salfred		USETW2(p->bString[max_len], 0, s[max_len]);
247184610Salfred	}
248184610Salfred	return (totlen);
249184610Salfred}
250