xref: /freebsd/sys/dev/usb/usb_util.c (revision aa1a8ff2d6dbc51ef058f46f3db5a8bb77967145)
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 	char *temp_p;
77 	uint8_t do_unlock;
78 
79 	if (dev == NULL) {
80 		/* should not happen */
81 		return;
82 	}
83 	uaa = device_get_ivars(dev);
84 	if (uaa == NULL) {
85 		/* can happen if called at the wrong time */
86 		return;
87 	}
88 	udev = uaa->device;
89 
90 	/* Protect scratch area */
91 	do_unlock = usbd_ctrl_lock(udev);
92 	temp_p = (char *)udev->scratch.data;
93 	usb_devinfo(udev, temp_p, sizeof(udev->scratch.data));
94 	if (do_unlock)
95 		usbd_ctrl_unlock(udev);
96 
97 	device_set_desc_copy(dev, temp_p);
98 	device_printf(dev, "<%s> on %s\n", temp_p,
99 	    device_get_nameunit(udev->bus->bdev));
100 }
101 
102 /*------------------------------------------------------------------------*
103  *	 usb_pause_mtx - factored out code
104  *
105  * This function will delay the code by the passed number of system
106  * ticks. The passed mutex "mtx" will be dropped while waiting, if
107  * "mtx" is different from NULL.
108  *------------------------------------------------------------------------*/
109 void
110 usb_pause_mtx(struct mtx *mtx, int timo)
111 {
112 	if (mtx != NULL)
113 		mtx_unlock(mtx);
114 
115 	/*
116 	 * Add one tick to the timeout so that we don't return too
117 	 * early! Note that pause() will assert that the passed
118 	 * timeout is positive and non-zero!
119 	 */
120 	pause("USBWAIT", timo + 1);
121 
122 	if (mtx != NULL)
123 		mtx_lock(mtx);
124 }
125 
126 /*------------------------------------------------------------------------*
127  *	usb_printbcd
128  *
129  * This function will print the version number "bcd" to the string
130  * pointed to by "p" having a maximum length of "p_len" bytes
131  * including the terminating zero.
132  *------------------------------------------------------------------------*/
133 void
134 usb_printbcd(char *p, uint16_t p_len, uint16_t bcd)
135 {
136 	if (snprintf(p, p_len, "%x.%02x", bcd >> 8, bcd & 0xff)) {
137 		/* ignore any errors */
138 	}
139 }
140 
141 /*------------------------------------------------------------------------*
142  *	usb_trim_spaces
143  *
144  * This function removes spaces at the beginning and the end of the string
145  * pointed to by the "p" argument.
146  *------------------------------------------------------------------------*/
147 void
148 usb_trim_spaces(char *p)
149 {
150 	char *q;
151 	char *e;
152 
153 	if (p == NULL)
154 		return;
155 	q = e = p;
156 	while (*q == ' ')		/* skip leading spaces */
157 		q++;
158 	while ((*p = *q++))		/* copy string */
159 		if (*p++ != ' ')	/* remember last non-space */
160 			e = p;
161 	*e = 0;				/* kill trailing spaces */
162 }
163 
164 /*------------------------------------------------------------------------*
165  *	usb_make_str_desc - convert an ASCII string into a UNICODE string
166  *------------------------------------------------------------------------*/
167 uint8_t
168 usb_make_str_desc(void *ptr, uint16_t max_len, const char *s)
169 {
170 	struct usb_string_descriptor *p = ptr;
171 	uint8_t totlen;
172 	int j;
173 
174 	if (max_len < 2) {
175 		/* invalid length */
176 		return (0);
177 	}
178 	max_len = ((max_len / 2) - 1);
179 
180 	j = strlen(s);
181 
182 	if (j < 0) {
183 		j = 0;
184 	}
185 	if (j > 126) {
186 		j = 126;
187 	}
188 	if (max_len > j) {
189 		max_len = j;
190 	}
191 	totlen = (max_len + 1) * 2;
192 
193 	p->bLength = totlen;
194 	p->bDescriptorType = UDESC_STRING;
195 
196 	while (max_len--) {
197 		USETW2(p->bString[max_len], 0, s[max_len]);
198 	}
199 	return (totlen);
200 }
201 
202 /*------------------------------------------------------------------------*
203  *	usb_check_request - prevent damaging USB requests
204  *
205  * Return values:
206  * 0: Access allowed
207  * Else: No access
208  *------------------------------------------------------------------------*/
209 #if USB_HAVE_UGEN
210 int
211 usb_check_request(struct usb_device *udev, struct usb_device_request *req)
212 {
213 	struct usb_endpoint *ep;
214 	int error;
215 
216 	/*
217 	 * Avoid requests that would damage the bus integrity:
218 	 */
219 	if (((req->bmRequestType == UT_WRITE_DEVICE) &&
220 	    (req->bRequest == UR_SET_ADDRESS)) ||
221 	    ((req->bmRequestType == UT_WRITE_DEVICE) &&
222 	    (req->bRequest == UR_SET_CONFIG)) ||
223 	    ((req->bmRequestType == UT_WRITE_INTERFACE) &&
224 	    (req->bRequest == UR_SET_INTERFACE))) {
225 		/*
226 		 * These requests can be useful for testing USB drivers.
227 		 */
228 		error = priv_check(curthread, PRIV_DRIVER);
229 		if (error)
230 			return (error);
231 	}
232 
233 	/*
234 	 * Special case - handle clearing of stall
235 	 */
236 	if (req->bmRequestType == UT_WRITE_ENDPOINT) {
237 		ep = usbd_get_ep_by_addr(udev, req->wIndex[0]);
238 		if (ep == NULL)
239 			return (EINVAL);
240 		if ((req->bRequest == UR_CLEAR_FEATURE) &&
241 		    (UGETW(req->wValue) == UF_ENDPOINT_HALT))
242 			usbd_clear_data_toggle(udev, ep);
243 	}
244 
245 	/* TODO: add more checks to verify the interface index */
246 
247 	return (0);
248 }
249 #endif
250