xref: /freebsd/sys/dev/usb/usb_util.c (revision 884a2a699669ec61e2366e3e358342dbc94be24a)
1 /* $FreeBSD$ */
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 <sys/stdint.h>
28 #include <sys/stddef.h>
29 #include <sys/param.h>
30 #include <sys/queue.h>
31 #include <sys/types.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/bus.h>
35 #include <sys/module.h>
36 #include <sys/lock.h>
37 #include <sys/mutex.h>
38 #include <sys/condvar.h>
39 #include <sys/sysctl.h>
40 #include <sys/sx.h>
41 #include <sys/unistd.h>
42 #include <sys/callout.h>
43 #include <sys/malloc.h>
44 #include <sys/priv.h>
45 
46 #include <dev/usb/usb.h>
47 #include <dev/usb/usbdi.h>
48 #include <dev/usb/usbdi_util.h>
49 
50 #include <dev/usb/usb_core.h>
51 #include <dev/usb/usb_util.h>
52 #include <dev/usb/usb_process.h>
53 #include <dev/usb/usb_device.h>
54 #include <dev/usb/usb_request.h>
55 #include <dev/usb/usb_busdma.h>
56 
57 #include <dev/usb/usb_controller.h>
58 #include <dev/usb/usb_bus.h>
59 
60 /*------------------------------------------------------------------------*
61  * device_delete_all_children - delete all children of a device
62  *------------------------------------------------------------------------*/
63 #ifndef device_delete_all_children
64 int
65 device_delete_all_children(device_t dev)
66 {
67 	device_t *devlist;
68 	int devcount;
69 	int error;
70 
71 	error = device_get_children(dev, &devlist, &devcount);
72 	if (error == 0) {
73 		while (devcount-- > 0) {
74 			error = device_delete_child(dev, devlist[devcount]);
75 			if (error) {
76 				break;
77 			}
78 		}
79 		free(devlist, M_TEMP);
80 	}
81 	return (error);
82 }
83 #endif
84 
85 /*------------------------------------------------------------------------*
86  *	device_set_usb_desc
87  *
88  * This function can be called at probe or attach to set the USB
89  * device supplied textual description for the given device.
90  *------------------------------------------------------------------------*/
91 void
92 device_set_usb_desc(device_t dev)
93 {
94 	struct usb_attach_arg *uaa;
95 	struct usb_device *udev;
96 	struct usb_interface *iface;
97 	char *temp_p;
98 	usb_error_t err;
99 
100 	if (dev == NULL) {
101 		/* should not happen */
102 		return;
103 	}
104 	uaa = device_get_ivars(dev);
105 	if (uaa == NULL) {
106 		/* can happen if called at the wrong time */
107 		return;
108 	}
109 	udev = uaa->device;
110 	iface = uaa->iface;
111 
112 	if ((iface == NULL) ||
113 	    (iface->idesc == NULL) ||
114 	    (iface->idesc->iInterface == 0)) {
115 		err = USB_ERR_INVAL;
116 	} else {
117 		err = 0;
118 	}
119 
120 	temp_p = (char *)udev->bus->scratch[0].data;
121 
122 	if (!err) {
123 		/* try to get the interface string ! */
124 		err = usbd_req_get_string_any
125 		    (udev, NULL, temp_p,
126 		    sizeof(udev->bus->scratch), iface->idesc->iInterface);
127 	}
128 	if (err) {
129 		/* use default description */
130 		usb_devinfo(udev, temp_p,
131 		    sizeof(udev->bus->scratch));
132 	}
133 	device_set_desc_copy(dev, temp_p);
134 	device_printf(dev, "<%s> on %s\n", temp_p,
135 	    device_get_nameunit(udev->bus->bdev));
136 }
137 
138 /*------------------------------------------------------------------------*
139  *	 usb_pause_mtx - factored out code
140  *
141  * This function will delay the code by the passed number of system
142  * ticks. The passed mutex "mtx" will be dropped while waiting, if
143  * "mtx" is not NULL.
144  *------------------------------------------------------------------------*/
145 void
146 usb_pause_mtx(struct mtx *mtx, int _ticks)
147 {
148 	if (mtx != NULL)
149 		mtx_unlock(mtx);
150 
151 	if (cold) {
152 		/* convert to milliseconds */
153 		_ticks = (_ticks * 1000) / hz;
154 		/* convert to microseconds, rounded up */
155 		_ticks = (_ticks + 1) * 1000;
156 		DELAY(_ticks);
157 
158 	} else {
159 
160 		/*
161 		 * Add one to the number of ticks so that we don't return
162 		 * too early!
163 		 */
164 		_ticks++;
165 
166 		if (pause("USBWAIT", _ticks)) {
167 			/* ignore */
168 		}
169 	}
170 	if (mtx != NULL)
171 		mtx_lock(mtx);
172 }
173 
174 /*------------------------------------------------------------------------*
175  *	usb_printbcd
176  *
177  * This function will print the version number "bcd" to the string
178  * pointed to by "p" having a maximum length of "p_len" bytes
179  * including the terminating zero.
180  *------------------------------------------------------------------------*/
181 void
182 usb_printbcd(char *p, uint16_t p_len, uint16_t bcd)
183 {
184 	if (snprintf(p, p_len, "%x.%02x", bcd >> 8, bcd & 0xff)) {
185 		/* ignore any errors */
186 	}
187 }
188 
189 /*------------------------------------------------------------------------*
190  *	usb_trim_spaces
191  *
192  * This function removes spaces at the beginning and the end of the string
193  * pointed to by the "p" argument.
194  *------------------------------------------------------------------------*/
195 void
196 usb_trim_spaces(char *p)
197 {
198 	char *q;
199 	char *e;
200 
201 	if (p == NULL)
202 		return;
203 	q = e = p;
204 	while (*q == ' ')		/* skip leading spaces */
205 		q++;
206 	while ((*p = *q++))		/* copy string */
207 		if (*p++ != ' ')	/* remember last non-space */
208 			e = p;
209 	*e = 0;				/* kill trailing spaces */
210 }
211 
212 /*------------------------------------------------------------------------*
213  *	usb_make_str_desc - convert an ASCII string into a UNICODE string
214  *------------------------------------------------------------------------*/
215 uint8_t
216 usb_make_str_desc(void *ptr, uint16_t max_len, const char *s)
217 {
218 	struct usb_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