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