xref: /openbsd-src/sys/dev/usb/usbdi_util.c (revision 9d06db31bf2f56de261a0c292fc3fd6349388c08)
1 /*	$OpenBSD: usbdi_util.c,v 1.33 2013/11/02 12:23:58 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 <dev/usb/usb.h>
42 #include <dev/usb/usbhid.h>
43 
44 #include <dev/usb/usbdi.h>
45 #include <dev/usb/usbdi_util.h>
46 
47 #ifdef USB_DEBUG
48 #define DPRINTF(x)	do { if (usbdebug) printf x; } while (0)
49 #define DPRINTFN(n,x)	do { if (usbdebug>(n)) printf x; } while (0)
50 extern int usbdebug;
51 #else
52 #define DPRINTF(x)
53 #define DPRINTFN(n,x)
54 #endif
55 
56 usbd_status
57 usbd_get_desc(struct usbd_device *dev, int type, int index, int len, void *desc)
58 {
59 	usb_device_request_t req;
60 
61 	DPRINTFN(3,("usbd_get_desc: type=%d, index=%d, len=%d\n", type, index,
62 	    len));
63 
64 	req.bmRequestType = UT_READ_DEVICE;
65 	req.bRequest = UR_GET_DESCRIPTOR;
66 	USETW2(req.wValue, type, index);
67 	USETW(req.wIndex, 0);
68 	USETW(req.wLength, len);
69 	return (usbd_do_request(dev, &req, desc));
70 }
71 
72 usbd_status
73 usbd_get_device_status(struct usbd_device *dev, usb_status_t *st)
74 {
75 	usb_device_request_t req;
76 
77 	req.bmRequestType = UT_READ_DEVICE;
78 	req.bRequest = UR_GET_STATUS;
79 	USETW(req.wValue, 0);
80 	USETW(req.wIndex, 0);
81 	USETW(req.wLength, sizeof(usb_status_t));
82 	return (usbd_do_request(dev, &req, st));
83 }
84 
85 usbd_status
86 usbd_get_hub_status(struct usbd_device *dev, usb_hub_status_t *st)
87 {
88 	usb_device_request_t req;
89 
90 	req.bmRequestType = UT_READ_CLASS_DEVICE;
91 	req.bRequest = UR_GET_STATUS;
92 	USETW(req.wValue, 0);
93 	USETW(req.wIndex, 0);
94 	USETW(req.wLength, sizeof(usb_hub_status_t));
95 	return (usbd_do_request(dev, &req, st));
96 }
97 
98 usbd_status
99 usbd_set_address(struct usbd_device *dev, int addr)
100 {
101 	usb_device_request_t req;
102 
103 	req.bmRequestType = UT_WRITE_DEVICE;
104 	req.bRequest = UR_SET_ADDRESS;
105 	USETW(req.wValue, addr);
106 	USETW(req.wIndex, 0);
107 	USETW(req.wLength, 0);
108 	return usbd_do_request(dev, &req, 0);
109 }
110 
111 usbd_status
112 usbd_get_port_status(struct usbd_device *dev, int port, usb_port_status_t *ps)
113 {
114 	usb_device_request_t req;
115 
116 	req.bmRequestType = UT_READ_CLASS_OTHER;
117 	req.bRequest = UR_GET_STATUS;
118 	USETW(req.wValue, 0);
119 	USETW(req.wIndex, port);
120 	USETW(req.wLength, sizeof *ps);
121 	return (usbd_do_request(dev, &req, ps));
122 }
123 
124 usbd_status
125 usbd_clear_hub_feature(struct usbd_device *dev, int sel)
126 {
127 	usb_device_request_t req;
128 
129 	req.bmRequestType = UT_WRITE_CLASS_DEVICE;
130 	req.bRequest = UR_CLEAR_FEATURE;
131 	USETW(req.wValue, sel);
132 	USETW(req.wIndex, 0);
133 	USETW(req.wLength, 0);
134 	return (usbd_do_request(dev, &req, 0));
135 }
136 
137 usbd_status
138 usbd_set_hub_feature(struct usbd_device *dev, int sel)
139 {
140 	usb_device_request_t req;
141 
142 	req.bmRequestType = UT_WRITE_CLASS_DEVICE;
143 	req.bRequest = UR_SET_FEATURE;
144 	USETW(req.wValue, sel);
145 	USETW(req.wIndex, 0);
146 	USETW(req.wLength, 0);
147 	return (usbd_do_request(dev, &req, 0));
148 }
149 
150 usbd_status
151 usbd_clear_port_feature(struct usbd_device *dev, int port, int sel)
152 {
153 	usb_device_request_t req;
154 
155 	req.bmRequestType = UT_WRITE_CLASS_OTHER;
156 	req.bRequest = UR_CLEAR_FEATURE;
157 	USETW(req.wValue, sel);
158 	USETW(req.wIndex, port);
159 	USETW(req.wLength, 0);
160 	return (usbd_do_request(dev, &req, 0));
161 }
162 
163 usbd_status
164 usbd_set_port_feature(struct usbd_device *dev, int port, int sel)
165 {
166 	usb_device_request_t req;
167 
168 	req.bmRequestType = UT_WRITE_CLASS_OTHER;
169 	req.bRequest = UR_SET_FEATURE;
170 	USETW(req.wValue, sel);
171 	USETW(req.wIndex, port);
172 	USETW(req.wLength, 0);
173 	return (usbd_do_request(dev, &req, 0));
174 }
175 
176 usbd_status
177 usbd_get_protocol(struct usbd_interface *iface, u_int8_t *report)
178 {
179 	usb_interface_descriptor_t *id = usbd_get_interface_descriptor(iface);
180 	struct usbd_device *dev;
181 	usb_device_request_t req;
182 
183 	DPRINTFN(4, ("usbd_get_protocol: iface=%p, endpt=%d\n", iface,
184 	    id->bInterfaceNumber));
185 	if (id == NULL)
186 		return (USBD_IOERROR);
187 	usbd_interface2device_handle(iface, &dev);
188 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
189 	req.bRequest = UR_GET_PROTOCOL;
190 	USETW(req.wValue, 0);
191 	USETW(req.wIndex, id->bInterfaceNumber);
192 	USETW(req.wLength, 1);
193 	return (usbd_do_request(dev, &req, report));
194 }
195 
196 usbd_status
197 usbd_set_protocol(struct usbd_interface *iface, int report)
198 {
199 	usb_interface_descriptor_t *id = usbd_get_interface_descriptor(iface);
200 	struct usbd_device *dev;
201 	usb_device_request_t req;
202 
203 	DPRINTFN(4, ("usbd_set_protocol: iface=%p, report=%d, endpt=%d\n",
204 	    iface, report, id->bInterfaceNumber));
205 	if (id == NULL)
206 		return (USBD_IOERROR);
207 	usbd_interface2device_handle(iface, &dev);
208 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
209 	req.bRequest = UR_SET_PROTOCOL;
210 	USETW(req.wValue, report);
211 	USETW(req.wIndex, id->bInterfaceNumber);
212 	USETW(req.wLength, 0);
213 	return (usbd_do_request(dev, &req, 0));
214 }
215 
216 usbd_status
217 usbd_set_report(struct usbd_interface *iface, int type, int id, void *data,
218     int len)
219 {
220 	usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
221 	struct usbd_device *dev;
222 	usb_device_request_t req;
223 
224 	DPRINTFN(4, ("usbd_set_report: len=%d\n", len));
225 	if (ifd == NULL)
226 		return (USBD_IOERROR);
227 	usbd_interface2device_handle(iface, &dev);
228 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
229 	req.bRequest = UR_SET_REPORT;
230 	USETW2(req.wValue, type, id);
231 	USETW(req.wIndex, ifd->bInterfaceNumber);
232 	USETW(req.wLength, len);
233 	return (usbd_do_request(dev, &req, data));
234 }
235 
236 usbd_status
237 usbd_set_report_async(struct usbd_interface *iface, int type, int id,
238     void *data, int len)
239 {
240 	usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
241 	struct usbd_device *dev;
242 	usb_device_request_t req;
243 
244 	DPRINTFN(4, ("usbd_set_report_async: len=%d\n", len));
245 	if (ifd == NULL)
246 		return (USBD_IOERROR);
247 	usbd_interface2device_handle(iface, &dev);
248 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
249 	req.bRequest = UR_SET_REPORT;
250 	USETW2(req.wValue, type, id);
251 	USETW(req.wIndex, ifd->bInterfaceNumber);
252 	USETW(req.wLength, len);
253 	return (usbd_do_request_async(dev, &req, data));
254 }
255 
256 usbd_status
257 usbd_get_report(struct usbd_interface *iface, int type, int id, void *data,
258     int len)
259 {
260 	usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
261 	struct usbd_device *dev;
262 	usb_device_request_t req;
263 
264 	DPRINTFN(4, ("usbd_get_report: len=%d\n", len));
265 	if (ifd == NULL)
266 		return (USBD_IOERROR);
267 	usbd_interface2device_handle(iface, &dev);
268 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
269 	req.bRequest = UR_GET_REPORT;
270 	USETW2(req.wValue, type, id);
271 	USETW(req.wIndex, ifd->bInterfaceNumber);
272 	USETW(req.wLength, len);
273 	return (usbd_do_request(dev, &req, data));
274 }
275 
276 usbd_status
277 usbd_set_idle(struct usbd_interface *iface, int duration, int id)
278 {
279 	usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
280 	struct usbd_device *dev;
281 	usb_device_request_t req;
282 
283 	DPRINTFN(4, ("usbd_set_idle: %d %d\n", duration, id));
284 	if (ifd == NULL)
285 		return (USBD_IOERROR);
286 	usbd_interface2device_handle(iface, &dev);
287 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
288 	req.bRequest = UR_SET_IDLE;
289 	USETW2(req.wValue, duration, id);
290 	USETW(req.wIndex, ifd->bInterfaceNumber);
291 	USETW(req.wLength, 0);
292 	return (usbd_do_request(dev, &req, 0));
293 }
294 
295 usbd_status
296 usbd_get_report_descriptor(struct usbd_device *dev, int ifcno, int size,
297     void *d)
298 {
299 	usb_device_request_t req;
300 
301 	req.bmRequestType = UT_READ_INTERFACE;
302 	req.bRequest = UR_GET_DESCRIPTOR;
303 	USETW2(req.wValue, UDESC_REPORT, 0); /* report id should be 0 */
304 	USETW(req.wIndex, ifcno);
305 	USETW(req.wLength, size);
306 	return (usbd_do_request(dev, &req, d));
307 }
308 
309 struct usb_hid_descriptor *
310 usbd_get_hid_descriptor(struct usbd_interface *ifc)
311 {
312 	usb_interface_descriptor_t *idesc = usbd_get_interface_descriptor(ifc);
313 	struct usbd_device *dev;
314 	usb_config_descriptor_t *cdesc;
315 	struct usb_hid_descriptor *hd;
316 	char *p, *end;
317 
318 	if (idesc == NULL)
319 		return (0);
320 	usbd_interface2device_handle(ifc, &dev);
321 	cdesc = usbd_get_config_descriptor(dev);
322 
323 	p = (char *)idesc + idesc->bLength;
324 	end = (char *)cdesc + UGETW(cdesc->wTotalLength);
325 
326 	for (; p < end; p += hd->bLength) {
327 		hd = (struct usb_hid_descriptor *)p;
328 		if (p + hd->bLength <= end && hd->bDescriptorType == UDESC_HID)
329 			return (hd);
330 		if (hd->bDescriptorType == UDESC_INTERFACE)
331 			break;
332 	}
333 	return (0);
334 }
335 
336 usbd_status
337 usbd_read_report_desc(struct usbd_interface *ifc, void **descp, int *sizep,
338     int mem)
339 {
340 	usb_interface_descriptor_t *id;
341 	struct usb_hid_descriptor *hid;
342 	struct usbd_device *dev;
343 	usbd_status err;
344 
345 	usbd_interface2device_handle(ifc, &dev);
346 	id = usbd_get_interface_descriptor(ifc);
347 	if (id == NULL)
348 		return (USBD_INVAL);
349 	hid = usbd_get_hid_descriptor(ifc);
350 	if (hid == NULL)
351 		return (USBD_IOERROR);
352 	*sizep = UGETW(hid->descrs[0].wDescriptorLength);
353 	*descp = malloc(*sizep, mem, M_NOWAIT);
354 	if (*descp == NULL)
355 		return (USBD_NOMEM);
356 	err = usbd_get_report_descriptor(dev, id->bInterfaceNumber, *sizep,
357 	    *descp);
358 	if (err) {
359 		free(*descp, mem);
360 		*descp = NULL;
361 		return (err);
362 	}
363 	return (USBD_NORMAL_COMPLETION);
364 }
365 
366 usbd_status
367 usbd_get_config(struct usbd_device *dev, u_int8_t *conf)
368 {
369 	usb_device_request_t req;
370 
371 	req.bmRequestType = UT_READ_DEVICE;
372 	req.bRequest = UR_GET_CONFIG;
373 	USETW(req.wValue, 0);
374 	USETW(req.wIndex, 0);
375 	USETW(req.wLength, 1);
376 	return (usbd_do_request(dev, &req, conf));
377 }
378 
379 void
380 usb_detach_wait(struct device *dv)
381 {
382 	DPRINTF(("usb_detach_wait: waiting for %s\n", dv->dv_xname));
383 	if (tsleep(dv, PZERO, "usbdet", hz * 60))
384 		printf("usb_detach_wait: %s didn't detach\n", dv->dv_xname);
385 	DPRINTF(("usb_detach_wait: %s done\n", dv->dv_xname));
386 }
387 
388 void
389 usb_detach_wakeup(struct device *dv)
390 {
391 	DPRINTF(("usb_detach_wakeup: for %s\n", dv->dv_xname));
392 	wakeup(dv);
393 }
394