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