usb_util.c revision 246122
1184610Salfred/* $FreeBSD: head/sys/dev/usb/usb_util.c 246122 2013-01-30 15:26:04Z hselasky $ */
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
27246122Shselasky#ifdef USB_GLOBAL_INCLUDE_FILE
28246122Shselasky#include USB_GLOBAL_INCLUDE_FILE
29246122Shselasky#else
30194677Sthompsa#include <sys/stdint.h>
31194677Sthompsa#include <sys/stddef.h>
32194677Sthompsa#include <sys/param.h>
33194677Sthompsa#include <sys/queue.h>
34194677Sthompsa#include <sys/types.h>
35194677Sthompsa#include <sys/systm.h>
36194677Sthompsa#include <sys/kernel.h>
37194677Sthompsa#include <sys/bus.h>
38194677Sthompsa#include <sys/module.h>
39194677Sthompsa#include <sys/lock.h>
40194677Sthompsa#include <sys/mutex.h>
41194677Sthompsa#include <sys/condvar.h>
42194677Sthompsa#include <sys/sysctl.h>
43194677Sthompsa#include <sys/sx.h>
44194677Sthompsa#include <sys/unistd.h>
45194677Sthompsa#include <sys/callout.h>
46194677Sthompsa#include <sys/malloc.h>
47194677Sthompsa#include <sys/priv.h>
48194677Sthompsa
49188942Sthompsa#include <dev/usb/usb.h>
50194677Sthompsa#include <dev/usb/usbdi.h>
51194677Sthompsa#include <dev/usb/usbdi_util.h>
52184610Salfred
53188942Sthompsa#include <dev/usb/usb_core.h>
54188942Sthompsa#include <dev/usb/usb_util.h>
55188942Sthompsa#include <dev/usb/usb_process.h>
56188942Sthompsa#include <dev/usb/usb_device.h>
57188942Sthompsa#include <dev/usb/usb_request.h>
58188942Sthompsa#include <dev/usb/usb_busdma.h>
59184610Salfred
60188942Sthompsa#include <dev/usb/usb_controller.h>
61188942Sthompsa#include <dev/usb/usb_bus.h>
62246122Shselasky#endif			/* USB_GLOBAL_INCLUDE_FILE */
63184610Salfred
64184610Salfred/*------------------------------------------------------------------------*
65194228Sthompsa *	device_set_usb_desc
66184610Salfred *
67184610Salfred * This function can be called at probe or attach to set the USB
68184610Salfred * device supplied textual description for the given device.
69184610Salfred *------------------------------------------------------------------------*/
70184610Salfredvoid
71194228Sthompsadevice_set_usb_desc(device_t dev)
72184610Salfred{
73192984Sthompsa	struct usb_attach_arg *uaa;
74192984Sthompsa	struct usb_device *udev;
75192984Sthompsa	struct usb_interface *iface;
76184610Salfred	char *temp_p;
77193045Sthompsa	usb_error_t err;
78184610Salfred
79184610Salfred	if (dev == NULL) {
80184610Salfred		/* should not happen */
81184610Salfred		return;
82184610Salfred	}
83184610Salfred	uaa = device_get_ivars(dev);
84184610Salfred	if (uaa == NULL) {
85185087Salfred		/* can happen if called at the wrong time */
86184610Salfred		return;
87184610Salfred	}
88184610Salfred	udev = uaa->device;
89184610Salfred	iface = uaa->iface;
90184610Salfred
91184610Salfred	if ((iface == NULL) ||
92184610Salfred	    (iface->idesc == NULL) ||
93184610Salfred	    (iface->idesc->iInterface == 0)) {
94184610Salfred		err = USB_ERR_INVAL;
95184610Salfred	} else {
96184610Salfred		err = 0;
97184610Salfred	}
98184610Salfred
99184610Salfred	temp_p = (char *)udev->bus->scratch[0].data;
100184610Salfred
101184610Salfred	if (!err) {
102184610Salfred		/* try to get the interface string ! */
103194228Sthompsa		err = usbd_req_get_string_any
104184610Salfred		    (udev, NULL, temp_p,
105184610Salfred		    sizeof(udev->bus->scratch), iface->idesc->iInterface);
106184610Salfred	}
107184610Salfred	if (err) {
108184610Salfred		/* use default description */
109194228Sthompsa		usb_devinfo(udev, temp_p,
110184610Salfred		    sizeof(udev->bus->scratch));
111184610Salfred	}
112184610Salfred	device_set_desc_copy(dev, temp_p);
113184610Salfred	device_printf(dev, "<%s> on %s\n", temp_p,
114184610Salfred	    device_get_nameunit(udev->bus->bdev));
115184610Salfred}
116184610Salfred
117184610Salfred/*------------------------------------------------------------------------*
118194228Sthompsa *	 usb_pause_mtx - factored out code
119184610Salfred *
120188411Sthompsa * This function will delay the code by the passed number of system
121188411Sthompsa * ticks. The passed mutex "mtx" will be dropped while waiting, if
122227706Shselasky * "mtx" is different from NULL.
123184610Salfred *------------------------------------------------------------------------*/
124184610Salfredvoid
125227706Shselaskyusb_pause_mtx(struct mtx *mtx, int timo)
126184610Salfred{
127188411Sthompsa	if (mtx != NULL)
128188411Sthompsa		mtx_unlock(mtx);
129188411Sthompsa
130227706Shselasky	/*
131227706Shselasky	 * Add one tick to the timeout so that we don't return too
132227706Shselasky	 * early! Note that pause() will assert that the passed
133227706Shselasky	 * timeout is positive and non-zero!
134227706Shselasky	 */
135227706Shselasky	pause("USBWAIT", timo + 1);
136184610Salfred
137188411Sthompsa	if (mtx != NULL)
138188411Sthompsa		mtx_lock(mtx);
139184610Salfred}
140184610Salfred
141184610Salfred/*------------------------------------------------------------------------*
142194228Sthompsa *	usb_printbcd
143184610Salfred *
144184610Salfred * This function will print the version number "bcd" to the string
145184610Salfred * pointed to by "p" having a maximum length of "p_len" bytes
146184610Salfred * including the terminating zero.
147184610Salfred *------------------------------------------------------------------------*/
148184610Salfredvoid
149194228Sthompsausb_printbcd(char *p, uint16_t p_len, uint16_t bcd)
150184610Salfred{
151184610Salfred	if (snprintf(p, p_len, "%x.%02x", bcd >> 8, bcd & 0xff)) {
152184610Salfred		/* ignore any errors */
153184610Salfred	}
154184610Salfred}
155184610Salfred
156184610Salfred/*------------------------------------------------------------------------*
157194228Sthompsa *	usb_trim_spaces
158184610Salfred *
159184610Salfred * This function removes spaces at the beginning and the end of the string
160184610Salfred * pointed to by the "p" argument.
161184610Salfred *------------------------------------------------------------------------*/
162184610Salfredvoid
163194228Sthompsausb_trim_spaces(char *p)
164184610Salfred{
165184610Salfred	char *q;
166184610Salfred	char *e;
167184610Salfred
168184610Salfred	if (p == NULL)
169184610Salfred		return;
170184610Salfred	q = e = p;
171184610Salfred	while (*q == ' ')		/* skip leading spaces */
172184610Salfred		q++;
173184610Salfred	while ((*p = *q++))		/* copy string */
174184610Salfred		if (*p++ != ' ')	/* remember last non-space */
175184610Salfred			e = p;
176184610Salfred	*e = 0;				/* kill trailing spaces */
177184610Salfred}
178184610Salfred
179184610Salfred/*------------------------------------------------------------------------*
180194228Sthompsa *	usb_make_str_desc - convert an ASCII string into a UNICODE string
181184610Salfred *------------------------------------------------------------------------*/
182184610Salfreduint8_t
183194228Sthompsausb_make_str_desc(void *ptr, uint16_t max_len, const char *s)
184184610Salfred{
185192984Sthompsa	struct usb_string_descriptor *p = ptr;
186184610Salfred	uint8_t totlen;
187184610Salfred	int j;
188184610Salfred
189184610Salfred	if (max_len < 2) {
190184610Salfred		/* invalid length */
191184610Salfred		return (0);
192184610Salfred	}
193184610Salfred	max_len = ((max_len / 2) - 1);
194184610Salfred
195184610Salfred	j = strlen(s);
196184610Salfred
197184610Salfred	if (j < 0) {
198184610Salfred		j = 0;
199184610Salfred	}
200184610Salfred	if (j > 126) {
201184610Salfred		j = 126;
202184610Salfred	}
203184610Salfred	if (max_len > j) {
204184610Salfred		max_len = j;
205184610Salfred	}
206184610Salfred	totlen = (max_len + 1) * 2;
207184610Salfred
208184610Salfred	p->bLength = totlen;
209184610Salfred	p->bDescriptorType = UDESC_STRING;
210184610Salfred
211184610Salfred	while (max_len--) {
212184610Salfred		USETW2(p->bString[max_len], 0, s[max_len]);
213184610Salfred	}
214184610Salfred	return (totlen);
215184610Salfred}
216