xref: /openbsd-src/sys/dev/usb/usbdi_util.c (revision ab0b1be78a33b04be9f7ec4a3d24ce14843759cb)
1 /*	$OpenBSD: usbdi_util.c,v 1.31 2013/04/15 09:23:02 mglocker 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_config_desc(struct usbd_device *dev, int confidx,
74     usb_config_descriptor_t *d)
75 {
76 	usbd_status err;
77 
78 	DPRINTFN(3,("usbd_get_config_desc: confidx=%d\n", confidx));
79 	err = usbd_get_desc(dev, UDESC_CONFIG, confidx,
80 	    USB_CONFIG_DESCRIPTOR_SIZE, d);
81 	if (err)
82 		return (err);
83 	if (d->bDescriptorType != UDESC_CONFIG) {
84 		DPRINTFN(-1,("usbd_get_config_desc: confidx=%d, bad desc "
85 		    "len=%d type=%d\n", confidx, d->bLength,
86 		    d->bDescriptorType));
87 		return (USBD_INVAL);
88 	}
89 	return (USBD_NORMAL_COMPLETION);
90 }
91 
92 usbd_status
93 usbd_get_device_status(struct usbd_device *dev, usb_status_t *st)
94 {
95 	usb_device_request_t req;
96 
97 	req.bmRequestType = UT_READ_DEVICE;
98 	req.bRequest = UR_GET_STATUS;
99 	USETW(req.wValue, 0);
100 	USETW(req.wIndex, 0);
101 	USETW(req.wLength, sizeof(usb_status_t));
102 	return (usbd_do_request(dev, &req, st));
103 }
104 
105 usbd_status
106 usbd_get_hub_status(struct usbd_device *dev, usb_hub_status_t *st)
107 {
108 	usb_device_request_t req;
109 
110 	req.bmRequestType = UT_READ_CLASS_DEVICE;
111 	req.bRequest = UR_GET_STATUS;
112 	USETW(req.wValue, 0);
113 	USETW(req.wIndex, 0);
114 	USETW(req.wLength, sizeof(usb_hub_status_t));
115 	return (usbd_do_request(dev, &req, st));
116 }
117 
118 usbd_status
119 usbd_set_address(struct usbd_device *dev, int addr)
120 {
121 	usb_device_request_t req;
122 
123 	req.bmRequestType = UT_WRITE_DEVICE;
124 	req.bRequest = UR_SET_ADDRESS;
125 	USETW(req.wValue, addr);
126 	USETW(req.wIndex, 0);
127 	USETW(req.wLength, 0);
128 	return usbd_do_request(dev, &req, 0);
129 }
130 
131 usbd_status
132 usbd_get_port_status(struct usbd_device *dev, int port, usb_port_status_t *ps)
133 {
134 	usb_device_request_t req;
135 
136 	req.bmRequestType = UT_READ_CLASS_OTHER;
137 	req.bRequest = UR_GET_STATUS;
138 	USETW(req.wValue, 0);
139 	USETW(req.wIndex, port);
140 	USETW(req.wLength, sizeof *ps);
141 	return (usbd_do_request(dev, &req, ps));
142 }
143 
144 usbd_status
145 usbd_clear_hub_feature(struct usbd_device *dev, int sel)
146 {
147 	usb_device_request_t req;
148 
149 	req.bmRequestType = UT_WRITE_CLASS_DEVICE;
150 	req.bRequest = UR_CLEAR_FEATURE;
151 	USETW(req.wValue, sel);
152 	USETW(req.wIndex, 0);
153 	USETW(req.wLength, 0);
154 	return (usbd_do_request(dev, &req, 0));
155 }
156 
157 usbd_status
158 usbd_set_hub_feature(struct usbd_device *dev, int sel)
159 {
160 	usb_device_request_t req;
161 
162 	req.bmRequestType = UT_WRITE_CLASS_DEVICE;
163 	req.bRequest = UR_SET_FEATURE;
164 	USETW(req.wValue, sel);
165 	USETW(req.wIndex, 0);
166 	USETW(req.wLength, 0);
167 	return (usbd_do_request(dev, &req, 0));
168 }
169 
170 usbd_status
171 usbd_clear_port_feature(struct usbd_device *dev, int port, int sel)
172 {
173 	usb_device_request_t req;
174 
175 	req.bmRequestType = UT_WRITE_CLASS_OTHER;
176 	req.bRequest = UR_CLEAR_FEATURE;
177 	USETW(req.wValue, sel);
178 	USETW(req.wIndex, port);
179 	USETW(req.wLength, 0);
180 	return (usbd_do_request(dev, &req, 0));
181 }
182 
183 usbd_status
184 usbd_set_port_feature(struct usbd_device *dev, int port, int sel)
185 {
186 	usb_device_request_t req;
187 
188 	req.bmRequestType = UT_WRITE_CLASS_OTHER;
189 	req.bRequest = UR_SET_FEATURE;
190 	USETW(req.wValue, sel);
191 	USETW(req.wIndex, port);
192 	USETW(req.wLength, 0);
193 	return (usbd_do_request(dev, &req, 0));
194 }
195 
196 usbd_status
197 usbd_get_protocol(struct usbd_interface *iface, u_int8_t *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_get_protocol: iface=%p, endpt=%d\n", iface,
204 	    id->bInterfaceNumber));
205 	if (id == NULL)
206 		return (USBD_IOERROR);
207 	usbd_interface2device_handle(iface, &dev);
208 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
209 	req.bRequest = UR_GET_PROTOCOL;
210 	USETW(req.wValue, 0);
211 	USETW(req.wIndex, id->bInterfaceNumber);
212 	USETW(req.wLength, 1);
213 	return (usbd_do_request(dev, &req, report));
214 }
215 
216 usbd_status
217 usbd_set_protocol(struct usbd_interface *iface, int report)
218 {
219 	usb_interface_descriptor_t *id = usbd_get_interface_descriptor(iface);
220 	struct usbd_device *dev;
221 	usb_device_request_t req;
222 
223 	DPRINTFN(4, ("usbd_set_protocol: iface=%p, report=%d, endpt=%d\n",
224 	    iface, report, id->bInterfaceNumber));
225 	if (id == NULL)
226 		return (USBD_IOERROR);
227 	usbd_interface2device_handle(iface, &dev);
228 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
229 	req.bRequest = UR_SET_PROTOCOL;
230 	USETW(req.wValue, report);
231 	USETW(req.wIndex, id->bInterfaceNumber);
232 	USETW(req.wLength, 0);
233 	return (usbd_do_request(dev, &req, 0));
234 }
235 
236 usbd_status
237 usbd_set_report(struct usbd_interface *iface, int type, int id, void *data,
238     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: 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(dev, &req, data));
254 }
255 
256 usbd_status
257 usbd_set_report_async(struct usbd_interface *iface, int type, int id,
258     void *data, 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_set_report_async: len=%d\n", len));
265 	if (ifd == NULL)
266 		return (USBD_IOERROR);
267 	usbd_interface2device_handle(iface, &dev);
268 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
269 	req.bRequest = UR_SET_REPORT;
270 	USETW2(req.wValue, type, id);
271 	USETW(req.wIndex, ifd->bInterfaceNumber);
272 	USETW(req.wLength, len);
273 	return (usbd_do_request_async(dev, &req, data));
274 }
275 
276 usbd_status
277 usbd_get_report(struct usbd_interface *iface, int type, int id, void *data,
278     int len)
279 {
280 	usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
281 	struct usbd_device *dev;
282 	usb_device_request_t req;
283 
284 	DPRINTFN(4, ("usbd_get_report: len=%d\n", len));
285 	if (ifd == NULL)
286 		return (USBD_IOERROR);
287 	usbd_interface2device_handle(iface, &dev);
288 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
289 	req.bRequest = UR_GET_REPORT;
290 	USETW2(req.wValue, type, id);
291 	USETW(req.wIndex, ifd->bInterfaceNumber);
292 	USETW(req.wLength, len);
293 	return (usbd_do_request(dev, &req, data));
294 }
295 
296 usbd_status
297 usbd_set_idle(struct usbd_interface *iface, int duration, int id)
298 {
299 	usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
300 	struct usbd_device *dev;
301 	usb_device_request_t req;
302 
303 	DPRINTFN(4, ("usbd_set_idle: %d %d\n", duration, id));
304 	if (ifd == NULL)
305 		return (USBD_IOERROR);
306 	usbd_interface2device_handle(iface, &dev);
307 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
308 	req.bRequest = UR_SET_IDLE;
309 	USETW2(req.wValue, duration, id);
310 	USETW(req.wIndex, ifd->bInterfaceNumber);
311 	USETW(req.wLength, 0);
312 	return (usbd_do_request(dev, &req, 0));
313 }
314 
315 usbd_status
316 usbd_get_report_descriptor(struct usbd_device *dev, int ifcno, int size,
317     void *d)
318 {
319 	usb_device_request_t req;
320 
321 	req.bmRequestType = UT_READ_INTERFACE;
322 	req.bRequest = UR_GET_DESCRIPTOR;
323 	USETW2(req.wValue, UDESC_REPORT, 0); /* report id should be 0 */
324 	USETW(req.wIndex, ifcno);
325 	USETW(req.wLength, size);
326 	return (usbd_do_request(dev, &req, d));
327 }
328 
329 struct usb_hid_descriptor *
330 usbd_get_hid_descriptor(struct usbd_interface *ifc)
331 {
332 	usb_interface_descriptor_t *idesc = usbd_get_interface_descriptor(ifc);
333 	struct usbd_device *dev;
334 	usb_config_descriptor_t *cdesc;
335 	struct usb_hid_descriptor *hd;
336 	char *p, *end;
337 
338 	if (idesc == NULL)
339 		return (0);
340 	usbd_interface2device_handle(ifc, &dev);
341 	cdesc = usbd_get_config_descriptor(dev);
342 
343 	p = (char *)idesc + idesc->bLength;
344 	end = (char *)cdesc + UGETW(cdesc->wTotalLength);
345 
346 	for (; p < end; p += hd->bLength) {
347 		hd = (struct usb_hid_descriptor *)p;
348 		if (p + hd->bLength <= end && hd->bDescriptorType == UDESC_HID)
349 			return (hd);
350 		if (hd->bDescriptorType == UDESC_INTERFACE)
351 			break;
352 	}
353 	return (0);
354 }
355 
356 usbd_status
357 usbd_read_report_desc(struct usbd_interface *ifc, void **descp, int *sizep,
358     int mem)
359 {
360 	usb_interface_descriptor_t *id;
361 	struct usb_hid_descriptor *hid;
362 	struct usbd_device *dev;
363 	usbd_status err;
364 
365 	usbd_interface2device_handle(ifc, &dev);
366 	id = usbd_get_interface_descriptor(ifc);
367 	if (id == NULL)
368 		return (USBD_INVAL);
369 	hid = usbd_get_hid_descriptor(ifc);
370 	if (hid == NULL)
371 		return (USBD_IOERROR);
372 	*sizep = UGETW(hid->descrs[0].wDescriptorLength);
373 	*descp = malloc(*sizep, mem, M_NOWAIT);
374 	if (*descp == NULL)
375 		return (USBD_NOMEM);
376 	err = usbd_get_report_descriptor(dev, id->bInterfaceNumber, *sizep,
377 	    *descp);
378 	if (err) {
379 		free(*descp, mem);
380 		*descp = NULL;
381 		return (err);
382 	}
383 	return (USBD_NORMAL_COMPLETION);
384 }
385 
386 usbd_status
387 usbd_get_config(struct usbd_device *dev, u_int8_t *conf)
388 {
389 	usb_device_request_t req;
390 
391 	req.bmRequestType = UT_READ_DEVICE;
392 	req.bRequest = UR_GET_CONFIG;
393 	USETW(req.wValue, 0);
394 	USETW(req.wIndex, 0);
395 	USETW(req.wLength, 1);
396 	return (usbd_do_request(dev, &req, conf));
397 }
398 
399 void usbd_bulk_transfer_cb(struct usbd_xfer *xfer, void *priv,
400     usbd_status status);
401 void
402 usbd_bulk_transfer_cb(struct usbd_xfer *xfer, void *priv,
403     usbd_status status)
404 {
405 	wakeup(xfer);
406 }
407 
408 usbd_status
409 usbd_bulk_transfer(struct usbd_xfer *xfer, struct usbd_pipe *pipe,
410     u_int16_t flags, u_int32_t timeout, void *buf, u_int32_t *size, char *lbl)
411 {
412 	usbd_status err;
413 	int s, error, pri;
414 
415 	usbd_setup_xfer(xfer, pipe, 0, buf, *size, flags, timeout,
416 	    usbd_bulk_transfer_cb);
417 	DPRINTFN(1, ("usbd_bulk_transfer: start transfer %d bytes\n", *size));
418 	s = splusb();		/* don't want callback until tsleep() */
419 	err = usbd_transfer(xfer);
420 	if (err != USBD_IN_PROGRESS) {
421 		splx(s);
422 		return (err);
423 	}
424 	pri = timeout == 0 ? (PZERO | PCATCH) : PZERO;
425 	error = tsleep((caddr_t)xfer, pri, lbl, 0);
426 	splx(s);
427 	if (error) {
428 		DPRINTF(("usbd_bulk_transfer: tsleep=%d\n", error));
429 		usbd_abort_pipe(pipe);
430 		return (USBD_INTERRUPTED);
431 	}
432 	usbd_get_xfer_status(xfer, NULL, NULL, size, &err);
433 	DPRINTFN(1,("usbd_bulk_transfer: transferred %d\n", *size));
434 	if (err) {
435 		DPRINTF(("usbd_bulk_transfer: error=%d\n", err));
436 		usbd_clear_endpoint_stall(pipe);
437 	}
438 	return (err);
439 }
440 
441 void usbd_intr_transfer_cb(struct usbd_xfer *xfer, void *priv,
442     usbd_status status);
443 void
444 usbd_intr_transfer_cb(struct usbd_xfer *xfer, void *priv,
445     usbd_status status)
446 {
447 	wakeup(xfer);
448 }
449 
450 usbd_status
451 usbd_intr_transfer(struct usbd_xfer *xfer, struct usbd_pipe *pipe,
452     u_int16_t flags, u_int32_t timeout, void *buf, u_int32_t *size, char *lbl)
453 {
454 	usbd_status err;
455 	int s, error, pri;
456 
457 	usbd_setup_xfer(xfer, pipe, 0, buf, *size, flags, timeout,
458 	    usbd_intr_transfer_cb);
459 	DPRINTFN(1, ("usbd_intr_transfer: start transfer %d bytes\n", *size));
460 	s = splusb();		/* don't want callback until tsleep() */
461 	err = usbd_transfer(xfer);
462 	if (err != USBD_IN_PROGRESS) {
463 		splx(s);
464 		return (err);
465 	}
466 	pri = timeout == 0 ? (PZERO | PCATCH) : PZERO;
467 	error = tsleep(xfer, pri, lbl, 0);
468 	splx(s);
469 	if (error) {
470 		DPRINTF(("usbd_intr_transfer: tsleep=%d\n", error));
471 		usbd_abort_pipe(pipe);
472 		return (USBD_INTERRUPTED);
473 	}
474 	usbd_get_xfer_status(xfer, NULL, NULL, size, &err);
475 	DPRINTFN(1,("usbd_intr_transfer: transferred %d\n", *size));
476 	if (err) {
477 		DPRINTF(("usbd_intr_transfer: error=%d\n", err));
478 		usbd_clear_endpoint_stall(pipe);
479 	}
480 	return (err);
481 }
482 
483 void
484 usb_detach_wait(struct device *dv)
485 {
486 	DPRINTF(("usb_detach_wait: waiting for %s\n", dv->dv_xname));
487 	if (tsleep(dv, PZERO, "usbdet", hz * 60))
488 		printf("usb_detach_wait: %s didn't detach\n", dv->dv_xname);
489 	DPRINTF(("usb_detach_wait: %s done\n", dv->dv_xname));
490 }
491 
492 void
493 usb_detach_wakeup(struct device *dv)
494 {
495 	DPRINTF(("usb_detach_wakeup: for %s\n", dv->dv_xname));
496 	wakeup(dv);
497 }
498