xref: /freebsd/sys/dev/usb/usb_util.c (revision 2008043f386721d58158e37e0d7e50df8095942d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2008-2022 Hans Petter Selasky
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #ifdef USB_GLOBAL_INCLUDE_FILE
29 #include USB_GLOBAL_INCLUDE_FILE
30 #else
31 #include <sys/stdint.h>
32 #include <sys/stddef.h>
33 #include <sys/param.h>
34 #include <sys/queue.h>
35 #include <sys/types.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/bus.h>
39 #include <sys/module.h>
40 #include <sys/lock.h>
41 #include <sys/mutex.h>
42 #include <sys/condvar.h>
43 #include <sys/sysctl.h>
44 #include <sys/sx.h>
45 #include <sys/unistd.h>
46 #include <sys/callout.h>
47 #include <sys/malloc.h>
48 #include <sys/priv.h>
49 
50 #include <dev/usb/usb.h>
51 #include <dev/usb/usbdi.h>
52 #include <dev/usb/usbdi_util.h>
53 
54 #include <dev/usb/usb_core.h>
55 #include <dev/usb/usb_util.h>
56 #include <dev/usb/usb_process.h>
57 #include <dev/usb/usb_device.h>
58 #include <dev/usb/usb_request.h>
59 #include <dev/usb/usb_busdma.h>
60 
61 #include <dev/usb/usb_controller.h>
62 #include <dev/usb/usb_bus.h>
63 #endif			/* USB_GLOBAL_INCLUDE_FILE */
64 
65 /*------------------------------------------------------------------------*
66  *	device_set_usb_desc
67  *
68  * This function can be called at probe or attach to set the USB
69  * device supplied textual description for the given device.
70  *------------------------------------------------------------------------*/
71 void
72 device_set_usb_desc(device_t dev)
73 {
74 	struct usb_attach_arg *uaa;
75 	struct usb_device *udev;
76 	struct usb_interface *iface;
77 	char *temp_p;
78 	usb_error_t err;
79 	uint8_t do_unlock;
80 
81 	if (dev == NULL) {
82 		/* should not happen */
83 		return;
84 	}
85 	uaa = device_get_ivars(dev);
86 	if (uaa == NULL) {
87 		/* can happen if called at the wrong time */
88 		return;
89 	}
90 	udev = uaa->device;
91 	iface = uaa->iface;
92 
93 	if ((iface == NULL) ||
94 	    (iface->idesc == NULL) ||
95 	    (iface->idesc->iInterface == 0)) {
96 		err = USB_ERR_INVAL;
97 	} else {
98 		err = 0;
99 	}
100 
101 	/* Protect scratch area */
102 	do_unlock = usbd_ctrl_lock(udev);
103 
104 	temp_p = (char *)udev->scratch.data;
105 
106 	if (err == 0) {
107 		/* try to get the interface string ! */
108 		err = usbd_req_get_string_any(udev, NULL, temp_p,
109 		    sizeof(udev->scratch.data),
110 		    iface->idesc->iInterface);
111 	}
112 	if (err != 0) {
113 		/* use default description */
114 		usb_devinfo(udev, temp_p,
115 		    sizeof(udev->scratch.data));
116 	}
117 
118 	if (do_unlock)
119 		usbd_ctrl_unlock(udev);
120 
121 	device_set_desc_copy(dev, temp_p);
122 	device_printf(dev, "<%s> on %s\n", temp_p,
123 	    device_get_nameunit(udev->bus->bdev));
124 }
125 
126 /*------------------------------------------------------------------------*
127  *	 usb_pause_mtx - factored out code
128  *
129  * This function will delay the code by the passed number of system
130  * ticks. The passed mutex "mtx" will be dropped while waiting, if
131  * "mtx" is different from NULL.
132  *------------------------------------------------------------------------*/
133 void
134 usb_pause_mtx(struct mtx *mtx, int timo)
135 {
136 	if (mtx != NULL)
137 		mtx_unlock(mtx);
138 
139 	/*
140 	 * Add one tick to the timeout so that we don't return too
141 	 * early! Note that pause() will assert that the passed
142 	 * timeout is positive and non-zero!
143 	 */
144 	pause("USBWAIT", timo + 1);
145 
146 	if (mtx != NULL)
147 		mtx_lock(mtx);
148 }
149 
150 /*------------------------------------------------------------------------*
151  *	usb_printbcd
152  *
153  * This function will print the version number "bcd" to the string
154  * pointed to by "p" having a maximum length of "p_len" bytes
155  * including the terminating zero.
156  *------------------------------------------------------------------------*/
157 void
158 usb_printbcd(char *p, uint16_t p_len, uint16_t bcd)
159 {
160 	if (snprintf(p, p_len, "%x.%02x", bcd >> 8, bcd & 0xff)) {
161 		/* ignore any errors */
162 	}
163 }
164 
165 /*------------------------------------------------------------------------*
166  *	usb_trim_spaces
167  *
168  * This function removes spaces at the beginning and the end of the string
169  * pointed to by the "p" argument.
170  *------------------------------------------------------------------------*/
171 void
172 usb_trim_spaces(char *p)
173 {
174 	char *q;
175 	char *e;
176 
177 	if (p == NULL)
178 		return;
179 	q = e = p;
180 	while (*q == ' ')		/* skip leading spaces */
181 		q++;
182 	while ((*p = *q++))		/* copy string */
183 		if (*p++ != ' ')	/* remember last non-space */
184 			e = p;
185 	*e = 0;				/* kill trailing spaces */
186 }
187 
188 /*------------------------------------------------------------------------*
189  *	usb_make_str_desc - convert an ASCII string into a UNICODE string
190  *------------------------------------------------------------------------*/
191 uint8_t
192 usb_make_str_desc(void *ptr, uint16_t max_len, const char *s)
193 {
194 	struct usb_string_descriptor *p = ptr;
195 	uint8_t totlen;
196 	int j;
197 
198 	if (max_len < 2) {
199 		/* invalid length */
200 		return (0);
201 	}
202 	max_len = ((max_len / 2) - 1);
203 
204 	j = strlen(s);
205 
206 	if (j < 0) {
207 		j = 0;
208 	}
209 	if (j > 126) {
210 		j = 126;
211 	}
212 	if (max_len > j) {
213 		max_len = j;
214 	}
215 	totlen = (max_len + 1) * 2;
216 
217 	p->bLength = totlen;
218 	p->bDescriptorType = UDESC_STRING;
219 
220 	while (max_len--) {
221 		USETW2(p->bString[max_len], 0, s[max_len]);
222 	}
223 	return (totlen);
224 }
225 
226 /*------------------------------------------------------------------------*
227  *	usb_check_request - prevent damaging USB requests
228  *
229  * Return values:
230  * 0: Access allowed
231  * Else: No access
232  *------------------------------------------------------------------------*/
233 #if USB_HAVE_UGEN
234 int
235 usb_check_request(struct usb_device *udev, struct usb_device_request *req)
236 {
237 	struct usb_endpoint *ep;
238 	int error;
239 
240 	/*
241 	 * Avoid requests that would damage the bus integrity:
242 	 */
243 	if (((req->bmRequestType == UT_WRITE_DEVICE) &&
244 	    (req->bRequest == UR_SET_ADDRESS)) ||
245 	    ((req->bmRequestType == UT_WRITE_DEVICE) &&
246 	    (req->bRequest == UR_SET_CONFIG)) ||
247 	    ((req->bmRequestType == UT_WRITE_INTERFACE) &&
248 	    (req->bRequest == UR_SET_INTERFACE))) {
249 		/*
250 		 * These requests can be useful for testing USB drivers.
251 		 */
252 		error = priv_check(curthread, PRIV_DRIVER);
253 		if (error)
254 			return (error);
255 	}
256 
257 	/*
258 	 * Special case - handle clearing of stall
259 	 */
260 	if (req->bmRequestType == UT_WRITE_ENDPOINT) {
261 		ep = usbd_get_ep_by_addr(udev, req->wIndex[0]);
262 		if (ep == NULL)
263 			return (EINVAL);
264 		if ((req->bRequest == UR_CLEAR_FEATURE) &&
265 		    (UGETW(req->wValue) == UF_ENDPOINT_HALT))
266 			usbd_clear_data_toggle(udev, ep);
267 	}
268 
269 	/* TODO: add more checks to verify the interface index */
270 
271 	return (0);
272 }
273 #endif
274