xref: /openbsd/sys/dev/usb/usbdi_util.c (revision 1756c2e3)
1 /*	$OpenBSD: usbdi_util.c,v 1.36 2014/04/24 09:40:28 mpi Exp $ */
2 /*	$NetBSD: usbdi_util.c,v 1.40 2002/07/11 21:14:36 augustss Exp $	*/
3 /*	$FreeBSD: src/sys/dev/usb/usbdi_util.c,v 1.14 1999/11/17 22:33:50 n_hibma Exp $	*/
4 
5 /*
6  * Copyright (c) 1998 The NetBSD Foundation, Inc.
7  * All rights reserved.
8  *
9  * This code is derived from software contributed to The NetBSD Foundation
10  * by Lennart Augustsson (lennart@augustsson.net) at
11  * Carlstedt Research & Technology.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/device.h>
40 
41 #include <machine/bus.h>
42 
43 #include <dev/usb/usb.h>
44 #include <dev/usb/usbhid.h>
45 
46 #include <dev/usb/usbdi.h>
47 #include <dev/usb/usbdi_util.h>
48 #include <dev/usb/usbdivar.h>
49 
50 #ifdef USB_DEBUG
51 #define DPRINTF(x)	do { if (usbdebug) printf x; } while (0)
52 #define DPRINTFN(n,x)	do { if (usbdebug>(n)) printf x; } while (0)
53 extern int usbdebug;
54 #else
55 #define DPRINTF(x)
56 #define DPRINTFN(n,x)
57 #endif
58 
59 usbd_status
60 usbd_get_desc(struct usbd_device *dev, int type, int index, int len, void *desc)
61 {
62 	usb_device_request_t req;
63 
64 	DPRINTFN(3,("usbd_get_desc: type=%d, index=%d, len=%d\n", type, index,
65 	    len));
66 
67 	req.bmRequestType = UT_READ_DEVICE;
68 	req.bRequest = UR_GET_DESCRIPTOR;
69 	USETW2(req.wValue, type, index);
70 	USETW(req.wIndex, 0);
71 	USETW(req.wLength, len);
72 	return (usbd_do_request(dev, &req, desc));
73 }
74 
75 usbd_status
76 usbd_get_device_status(struct usbd_device *dev, usb_status_t *st)
77 {
78 	usb_device_request_t req;
79 
80 	req.bmRequestType = UT_READ_DEVICE;
81 	req.bRequest = UR_GET_STATUS;
82 	USETW(req.wValue, 0);
83 	USETW(req.wIndex, 0);
84 	USETW(req.wLength, sizeof(usb_status_t));
85 	return (usbd_do_request(dev, &req, st));
86 }
87 
88 usbd_status
89 usbd_get_hub_status(struct usbd_device *dev, usb_hub_status_t *st)
90 {
91 	usb_device_request_t req;
92 
93 	req.bmRequestType = UT_READ_CLASS_DEVICE;
94 	req.bRequest = UR_GET_STATUS;
95 	USETW(req.wValue, 0);
96 	USETW(req.wIndex, 0);
97 	USETW(req.wLength, sizeof(usb_hub_status_t));
98 	return (usbd_do_request(dev, &req, st));
99 }
100 
101 usbd_status
102 usbd_get_hub_descriptor(struct usbd_device *dev, usb_hub_descriptor_t *hd,
103     uint8_t nports)
104 {
105 	usb_device_request_t req;
106 	uint16_t len = USB_HUB_DESCRIPTOR_SIZE + (nports + 1) / 8;
107 
108 	req.bmRequestType = UT_READ_CLASS_DEVICE;
109 	req.bRequest = UR_GET_DESCRIPTOR;
110 	USETW2(req.wValue, UDESC_HUB, 0);
111 	USETW(req.wIndex, 0);
112 	USETW(req.wLength, len);
113 	return (usbd_do_request(dev, &req, hd));
114 }
115 
116 usbd_status
117 usbd_set_address(struct usbd_device *dev, int addr)
118 {
119 	usb_device_request_t req;
120 
121 	req.bmRequestType = UT_WRITE_DEVICE;
122 	req.bRequest = UR_SET_ADDRESS;
123 	USETW(req.wValue, addr);
124 	USETW(req.wIndex, 0);
125 	USETW(req.wLength, 0);
126 	return usbd_do_request(dev, &req, 0);
127 }
128 
129 usbd_status
130 usbd_get_port_status(struct usbd_device *dev, int port, usb_port_status_t *ps)
131 {
132 	usb_device_request_t req;
133 
134 	req.bmRequestType = UT_READ_CLASS_OTHER;
135 	req.bRequest = UR_GET_STATUS;
136 	USETW(req.wValue, 0);
137 	USETW(req.wIndex, port);
138 	USETW(req.wLength, sizeof *ps);
139 	return (usbd_do_request(dev, &req, ps));
140 }
141 
142 usbd_status
143 usbd_clear_hub_feature(struct usbd_device *dev, int sel)
144 {
145 	usb_device_request_t req;
146 
147 	req.bmRequestType = UT_WRITE_CLASS_DEVICE;
148 	req.bRequest = UR_CLEAR_FEATURE;
149 	USETW(req.wValue, sel);
150 	USETW(req.wIndex, 0);
151 	USETW(req.wLength, 0);
152 	return (usbd_do_request(dev, &req, 0));
153 }
154 
155 usbd_status
156 usbd_set_hub_feature(struct usbd_device *dev, int sel)
157 {
158 	usb_device_request_t req;
159 
160 	req.bmRequestType = UT_WRITE_CLASS_DEVICE;
161 	req.bRequest = UR_SET_FEATURE;
162 	USETW(req.wValue, sel);
163 	USETW(req.wIndex, 0);
164 	USETW(req.wLength, 0);
165 	return (usbd_do_request(dev, &req, 0));
166 }
167 
168 usbd_status
169 usbd_clear_port_feature(struct usbd_device *dev, int port, int sel)
170 {
171 	usb_device_request_t req;
172 
173 	req.bmRequestType = UT_WRITE_CLASS_OTHER;
174 	req.bRequest = UR_CLEAR_FEATURE;
175 	USETW(req.wValue, sel);
176 	USETW(req.wIndex, port);
177 	USETW(req.wLength, 0);
178 	return (usbd_do_request(dev, &req, 0));
179 }
180 
181 usbd_status
182 usbd_set_port_feature(struct usbd_device *dev, int port, int sel)
183 {
184 	usb_device_request_t req;
185 
186 	req.bmRequestType = UT_WRITE_CLASS_OTHER;
187 	req.bRequest = UR_SET_FEATURE;
188 	USETW(req.wValue, sel);
189 	USETW(req.wIndex, port);
190 	USETW(req.wLength, 0);
191 	return (usbd_do_request(dev, &req, 0));
192 }
193 
194 usbd_status
195 usbd_set_report(struct usbd_device *dev, int ifaceno, int type, int id,
196     void *data, int len)
197 {
198 	usb_device_request_t req;
199 
200 	DPRINTFN(4, ("usbd_set_report: len=%d\n", len));
201 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
202 	req.bRequest = UR_SET_REPORT;
203 	USETW2(req.wValue, type, id);
204 	USETW(req.wIndex, ifaceno);
205 	USETW(req.wLength, len);
206 	return (usbd_do_request(dev, &req, data));
207 }
208 
209 usbd_status
210 usbd_set_report_async(struct usbd_device *dev, int ifaceno, int type, int id,
211     void *data, int len)
212 {
213 	usb_device_request_t req;
214 
215 	DPRINTFN(4, ("usbd_set_report_async: len=%d\n", len));
216 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
217 	req.bRequest = UR_SET_REPORT;
218 	USETW2(req.wValue, type, id);
219 	USETW(req.wIndex, ifaceno);
220 	USETW(req.wLength, len);
221 	return (usbd_do_request_async(dev, &req, data));
222 }
223 
224 usbd_status
225 usbd_get_report(struct usbd_device *dev, int ifaceno, int type, int id,
226     void *data, int len)
227 {
228 	usb_device_request_t req;
229 
230 	DPRINTFN(4, ("usbd_get_report: len=%d\n", len));
231 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
232 	req.bRequest = UR_GET_REPORT;
233 	USETW2(req.wValue, type, id);
234 	USETW(req.wIndex, ifaceno);
235 	USETW(req.wLength, len);
236 	return (usbd_do_request(dev, &req, data));
237 }
238 
239 usbd_status
240 usbd_set_idle(struct usbd_device *dev, int ifaceno, int duration, int id)
241 {
242 	usb_device_request_t req;
243 
244 	DPRINTFN(4, ("usbd_set_idle: %d %d\n", duration, id));
245 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
246 	req.bRequest = UR_SET_IDLE;
247 	USETW2(req.wValue, duration, id);
248 	USETW(req.wIndex, ifaceno);
249 	USETW(req.wLength, 0);
250 	return (usbd_do_request(dev, &req, 0));
251 }
252 
253 usbd_status
254 usbd_get_report_descriptor(struct usbd_device *dev, int ifaceno,
255     void *data, int len)
256 {
257 	usb_device_request_t req;
258 
259 	req.bmRequestType = UT_READ_INTERFACE;
260 	req.bRequest = UR_GET_DESCRIPTOR;
261 	USETW2(req.wValue, UDESC_REPORT, 0); /* report id should be 0 */
262 	USETW(req.wIndex, ifaceno);
263 	USETW(req.wLength, len);
264 	return (usbd_do_request(dev, &req, data));
265 }
266 
267 struct usb_hid_descriptor *
268 usbd_get_hid_descriptor(struct usbd_device *dev, usb_interface_descriptor_t *id)
269 {
270 	usb_config_descriptor_t *cdesc = usbd_get_config_descriptor(dev);
271 	struct usb_hid_descriptor *hd;
272 	char *p, *end;
273 
274 	p = (char *)id + id->bLength;
275 	end = (char *)cdesc + UGETW(cdesc->wTotalLength);
276 
277 	for (; p < end; p += hd->bLength) {
278 		hd = (struct usb_hid_descriptor *)p;
279 		if (p + hd->bLength <= end && hd->bDescriptorType == UDESC_HID)
280 			return (hd);
281 		if (hd->bDescriptorType == UDESC_INTERFACE)
282 			break;
283 	}
284 	return (0);
285 }
286 
287 usbd_status
288 usbd_get_config(struct usbd_device *dev, u_int8_t *conf)
289 {
290 	usb_device_request_t req;
291 
292 	req.bmRequestType = UT_READ_DEVICE;
293 	req.bRequest = UR_GET_CONFIG;
294 	USETW(req.wValue, 0);
295 	USETW(req.wIndex, 0);
296 	USETW(req.wLength, 1);
297 	return (usbd_do_request(dev, &req, conf));
298 }
299 
300 void
301 usb_detach_wait(struct device *dv)
302 {
303 	DPRINTF(("usb_detach_wait: waiting for %s\n", dv->dv_xname));
304 	if (tsleep(dv, PZERO, "usbdet", hz * 60))
305 		printf("usb_detach_wait: %s didn't detach\n", dv->dv_xname);
306 	DPRINTF(("usb_detach_wait: %s done\n", dv->dv_xname));
307 }
308 
309 void
310 usb_detach_wakeup(struct device *dv)
311 {
312 	DPRINTF(("usb_detach_wakeup: for %s\n", dv->dv_xname));
313 	wakeup(dv);
314 }
315