xref: /netbsd-src/sys/dev/usb/usbdi_util.c (revision df0caa2637da0538ecdf6b878c4d08e684b43d8f)
1 /*	$NetBSD: usbdi_util.c,v 1.44 2005/05/30 04:56:13 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Lennart Augustsson (lennart@augustsson.net) at
9  * Carlstedt Research & Technology.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *        This product includes software developed by the NetBSD
22  *        Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 #include <sys/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: usbdi_util.c,v 1.44 2005/05/30 04:56:13 christos Exp $");
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/malloc.h>
47 #if defined(__NetBSD__) || defined(__OpenBSD__)
48 #include <sys/proc.h>
49 #include <sys/device.h>
50 #elif defined(__FreeBSD__)
51 #include <sys/bus.h>
52 #endif
53 
54 #include <dev/usb/usb.h>
55 #include <dev/usb/usbhid.h>
56 
57 #include <dev/usb/usbdi.h>
58 #include <dev/usb/usbdi_util.h>
59 
60 #ifdef USB_DEBUG
61 #define DPRINTF(x)	if (usbdebug) logprintf x
62 #define DPRINTFN(n,x)	if (usbdebug>(n)) logprintf x
63 extern int usbdebug;
64 #else
65 #define DPRINTF(x)
66 #define DPRINTFN(n,x)
67 #endif
68 
69 usbd_status
70 usbd_get_desc(usbd_device_handle dev, int type, int index, int len, void *desc)
71 {
72 	usb_device_request_t req;
73 
74 	DPRINTFN(3,("usbd_get_desc: type=%d, index=%d, len=%d\n",
75 		    type, index, len));
76 
77 	req.bmRequestType = UT_READ_DEVICE;
78 	req.bRequest = UR_GET_DESCRIPTOR;
79 	USETW2(req.wValue, type, index);
80 	USETW(req.wIndex, 0);
81 	USETW(req.wLength, len);
82 	return (usbd_do_request(dev, &req, desc));
83 }
84 
85 usbd_status
86 usbd_get_config_desc(usbd_device_handle dev, int confidx,
87 		     usb_config_descriptor_t *d)
88 {
89 	usbd_status err;
90 
91 	DPRINTFN(3,("usbd_get_config_desc: confidx=%d\n", confidx));
92 	err = usbd_get_desc(dev, UDESC_CONFIG, confidx,
93 			    USB_CONFIG_DESCRIPTOR_SIZE, d);
94 	if (err)
95 		return (err);
96 	if (d->bDescriptorType != UDESC_CONFIG) {
97 		DPRINTFN(-1,("usbd_get_config_desc: confidx=%d, bad desc "
98 			     "len=%d type=%d\n",
99 			     confidx, d->bLength, d->bDescriptorType));
100 		return (USBD_INVAL);
101 	}
102 	return (USBD_NORMAL_COMPLETION);
103 }
104 
105 usbd_status
106 usbd_get_config_desc_full(usbd_device_handle dev, int conf, void *d, int size)
107 {
108 	DPRINTFN(3,("usbd_get_config_desc_full: conf=%d\n", conf));
109 	return (usbd_get_desc(dev, UDESC_CONFIG, conf, size, d));
110 }
111 
112 usbd_status
113 usbd_get_device_desc(usbd_device_handle dev, usb_device_descriptor_t *d)
114 {
115 	DPRINTFN(3,("usbd_get_device_desc:\n"));
116 	return (usbd_get_desc(dev, UDESC_DEVICE,
117 			     0, USB_DEVICE_DESCRIPTOR_SIZE, d));
118 }
119 
120 usbd_status
121 usbd_get_device_status(usbd_device_handle dev, usb_status_t *st)
122 {
123 	usb_device_request_t req;
124 
125 	req.bmRequestType = UT_READ_DEVICE;
126 	req.bRequest = UR_GET_STATUS;
127 	USETW(req.wValue, 0);
128 	USETW(req.wIndex, 0);
129 	USETW(req.wLength, sizeof(usb_status_t));
130 	return (usbd_do_request(dev, &req, st));
131 }
132 
133 usbd_status
134 usbd_get_hub_status(usbd_device_handle dev, usb_hub_status_t *st)
135 {
136 	usb_device_request_t req;
137 
138 	req.bmRequestType = UT_READ_CLASS_DEVICE;
139 	req.bRequest = UR_GET_STATUS;
140 	USETW(req.wValue, 0);
141 	USETW(req.wIndex, 0);
142 	USETW(req.wLength, sizeof(usb_hub_status_t));
143 	return (usbd_do_request(dev, &req, st));
144 }
145 
146 usbd_status
147 usbd_set_address(usbd_device_handle dev, int addr)
148 {
149 	usb_device_request_t req;
150 
151 	req.bmRequestType = UT_WRITE_DEVICE;
152 	req.bRequest = UR_SET_ADDRESS;
153 	USETW(req.wValue, addr);
154 	USETW(req.wIndex, 0);
155 	USETW(req.wLength, 0);
156 	return usbd_do_request(dev, &req, 0);
157 }
158 
159 usbd_status
160 usbd_get_port_status(usbd_device_handle dev, int port, usb_port_status_t *ps)
161 {
162 	usb_device_request_t req;
163 
164 	req.bmRequestType = UT_READ_CLASS_OTHER;
165 	req.bRequest = UR_GET_STATUS;
166 	USETW(req.wValue, 0);
167 	USETW(req.wIndex, port);
168 	USETW(req.wLength, sizeof *ps);
169 	return (usbd_do_request(dev, &req, ps));
170 }
171 
172 usbd_status
173 usbd_clear_hub_feature(usbd_device_handle dev, int sel)
174 {
175 	usb_device_request_t req;
176 
177 	req.bmRequestType = UT_WRITE_CLASS_DEVICE;
178 	req.bRequest = UR_CLEAR_FEATURE;
179 	USETW(req.wValue, sel);
180 	USETW(req.wIndex, 0);
181 	USETW(req.wLength, 0);
182 	return (usbd_do_request(dev, &req, 0));
183 }
184 
185 usbd_status
186 usbd_set_hub_feature(usbd_device_handle dev, int sel)
187 {
188 	usb_device_request_t req;
189 
190 	req.bmRequestType = UT_WRITE_CLASS_DEVICE;
191 	req.bRequest = UR_SET_FEATURE;
192 	USETW(req.wValue, sel);
193 	USETW(req.wIndex, 0);
194 	USETW(req.wLength, 0);
195 	return (usbd_do_request(dev, &req, 0));
196 }
197 
198 usbd_status
199 usbd_clear_port_feature(usbd_device_handle dev, int port, int sel)
200 {
201 	usb_device_request_t req;
202 
203 	req.bmRequestType = UT_WRITE_CLASS_OTHER;
204 	req.bRequest = UR_CLEAR_FEATURE;
205 	USETW(req.wValue, sel);
206 	USETW(req.wIndex, port);
207 	USETW(req.wLength, 0);
208 	return (usbd_do_request(dev, &req, 0));
209 }
210 
211 usbd_status
212 usbd_set_port_feature(usbd_device_handle dev, int port, int sel)
213 {
214 	usb_device_request_t req;
215 
216 	req.bmRequestType = UT_WRITE_CLASS_OTHER;
217 	req.bRequest = UR_SET_FEATURE;
218 	USETW(req.wValue, sel);
219 	USETW(req.wIndex, port);
220 	USETW(req.wLength, 0);
221 	return (usbd_do_request(dev, &req, 0));
222 }
223 
224 usbd_status
225 usbd_get_protocol(usbd_interface_handle iface, u_int8_t *report)
226 {
227 	usb_interface_descriptor_t *id = usbd_get_interface_descriptor(iface);
228 	usbd_device_handle dev;
229 	usb_device_request_t req;
230 
231 	DPRINTFN(4, ("usbd_get_protocol: iface=%p, endpt=%d\n",
232 		     iface, id->bInterfaceNumber));
233 	if (id == NULL)
234 		return (USBD_IOERROR);
235 	usbd_interface2device_handle(iface, &dev);
236 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
237 	req.bRequest = UR_GET_PROTOCOL;
238 	USETW(req.wValue, 0);
239 	USETW(req.wIndex, id->bInterfaceNumber);
240 	USETW(req.wLength, 1);
241 	return (usbd_do_request(dev, &req, report));
242 }
243 
244 usbd_status
245 usbd_set_protocol(usbd_interface_handle iface, int report)
246 {
247 	usb_interface_descriptor_t *id = usbd_get_interface_descriptor(iface);
248 	usbd_device_handle dev;
249 	usb_device_request_t req;
250 
251 	DPRINTFN(4, ("usbd_set_protocol: iface=%p, report=%d, endpt=%d\n",
252 		     iface, report, id->bInterfaceNumber));
253 	if (id == NULL)
254 		return (USBD_IOERROR);
255 	usbd_interface2device_handle(iface, &dev);
256 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
257 	req.bRequest = UR_SET_PROTOCOL;
258 	USETW(req.wValue, report);
259 	USETW(req.wIndex, id->bInterfaceNumber);
260 	USETW(req.wLength, 0);
261 	return (usbd_do_request(dev, &req, 0));
262 }
263 
264 usbd_status
265 usbd_set_report(usbd_interface_handle iface, int type, int id, void *data,
266 		int len)
267 {
268 	usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
269 	usbd_device_handle dev;
270 	usb_device_request_t req;
271 
272 	DPRINTFN(4, ("usbd_set_report: len=%d\n", len));
273 	if (ifd == NULL)
274 		return (USBD_IOERROR);
275 	usbd_interface2device_handle(iface, &dev);
276 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
277 	req.bRequest = UR_SET_REPORT;
278 	USETW2(req.wValue, type, id);
279 	USETW(req.wIndex, ifd->bInterfaceNumber);
280 	USETW(req.wLength, len);
281 	return (usbd_do_request(dev, &req, data));
282 }
283 
284 usbd_status
285 usbd_set_report_async(usbd_interface_handle iface, int type, int id, void *data,
286 		      int len)
287 {
288 	usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
289 	usbd_device_handle dev;
290 	usb_device_request_t req;
291 
292 	DPRINTFN(4, ("usbd_set_report_async: len=%d\n", len));
293 	if (ifd == NULL)
294 		return (USBD_IOERROR);
295 	usbd_interface2device_handle(iface, &dev);
296 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
297 	req.bRequest = UR_SET_REPORT;
298 	USETW2(req.wValue, type, id);
299 	USETW(req.wIndex, ifd->bInterfaceNumber);
300 	USETW(req.wLength, len);
301 	return (usbd_do_request_async(dev, &req, data));
302 }
303 
304 usbd_status
305 usbd_get_report(usbd_interface_handle iface, int type, int id, void *data,
306 		int len)
307 {
308 	usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
309 	usbd_device_handle dev;
310 	usb_device_request_t req;
311 
312 	DPRINTFN(4, ("usbd_get_report: len=%d\n", len));
313 	if (ifd == NULL)
314 		return (USBD_IOERROR);
315 	usbd_interface2device_handle(iface, &dev);
316 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
317 	req.bRequest = UR_GET_REPORT;
318 	USETW2(req.wValue, type, id);
319 	USETW(req.wIndex, ifd->bInterfaceNumber);
320 	USETW(req.wLength, len);
321 	return (usbd_do_request(dev, &req, data));
322 }
323 
324 usbd_status
325 usbd_set_idle(usbd_interface_handle iface, int duration, int id)
326 {
327 	usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
328 	usbd_device_handle dev;
329 	usb_device_request_t req;
330 
331 	DPRINTFN(4, ("usbd_set_idle: %d %d\n", duration, id));
332 	if (ifd == NULL)
333 		return (USBD_IOERROR);
334 	usbd_interface2device_handle(iface, &dev);
335 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
336 	req.bRequest = UR_SET_IDLE;
337 	USETW2(req.wValue, duration, id);
338 	USETW(req.wIndex, ifd->bInterfaceNumber);
339 	USETW(req.wLength, 0);
340 	return (usbd_do_request(dev, &req, 0));
341 }
342 
343 usbd_status
344 usbd_get_report_descriptor(usbd_device_handle dev, int ifcno,
345 			   int size, void *d)
346 {
347 	usb_device_request_t req;
348 
349 	req.bmRequestType = UT_READ_INTERFACE;
350 	req.bRequest = UR_GET_DESCRIPTOR;
351 	USETW2(req.wValue, UDESC_REPORT, 0); /* report id should be 0 */
352 	USETW(req.wIndex, ifcno);
353 	USETW(req.wLength, size);
354 	return (usbd_do_request(dev, &req, d));
355 }
356 
357 usb_hid_descriptor_t *
358 usbd_get_hid_descriptor(usbd_interface_handle ifc)
359 {
360 	usb_interface_descriptor_t *idesc = usbd_get_interface_descriptor(ifc);
361 	usbd_device_handle dev;
362 	usb_config_descriptor_t *cdesc;
363 	usb_hid_descriptor_t *hd;
364 	char *p, *end;
365 
366 	if (idesc == NULL)
367 		return (NULL);
368 	usbd_interface2device_handle(ifc, &dev);
369 	cdesc = usbd_get_config_descriptor(dev);
370 
371 	p = (char *)idesc + idesc->bLength;
372 	end = (char *)cdesc + UGETW(cdesc->wTotalLength);
373 
374 	for (; p < end; p += hd->bLength) {
375 		hd = (usb_hid_descriptor_t *)p;
376 		if (p + hd->bLength <= end && hd->bDescriptorType == UDESC_HID)
377 			return (hd);
378 		if (hd->bDescriptorType == UDESC_INTERFACE)
379 			break;
380 	}
381 	return (NULL);
382 }
383 
384 usbd_status
385 usbd_read_report_desc(usbd_interface_handle ifc, void **descp, int *sizep,
386 		       usb_malloc_type mem)
387 {
388 	usb_interface_descriptor_t *id;
389 	usb_hid_descriptor_t *hid;
390 	usbd_device_handle dev;
391 	usbd_status err;
392 
393 	usbd_interface2device_handle(ifc, &dev);
394 	id = usbd_get_interface_descriptor(ifc);
395 	if (id == NULL)
396 		return (USBD_INVAL);
397 	hid = usbd_get_hid_descriptor(ifc);
398 	if (hid == NULL)
399 		return (USBD_IOERROR);
400 	*sizep = UGETW(hid->descrs[0].wDescriptorLength);
401 	*descp = malloc(*sizep, mem, M_NOWAIT);
402 	if (*descp == NULL)
403 		return (USBD_NOMEM);
404 	err = usbd_get_report_descriptor(dev, id->bInterfaceNumber,
405 					 *sizep, *descp);
406 	if (err) {
407 		free(*descp, mem);
408 		*descp = NULL;
409 		return (err);
410 	}
411 	return (USBD_NORMAL_COMPLETION);
412 }
413 
414 usbd_status
415 usbd_get_config(usbd_device_handle dev, u_int8_t *conf)
416 {
417 	usb_device_request_t req;
418 
419 	req.bmRequestType = UT_READ_DEVICE;
420 	req.bRequest = UR_GET_CONFIG;
421 	USETW(req.wValue, 0);
422 	USETW(req.wIndex, 0);
423 	USETW(req.wLength, 1);
424 	return (usbd_do_request(dev, &req, conf));
425 }
426 
427 Static void usbd_bulk_transfer_cb(usbd_xfer_handle xfer,
428 				  usbd_private_handle priv, usbd_status status);
429 Static void
430 usbd_bulk_transfer_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
431 		      usbd_status status)
432 {
433 	wakeup(xfer);
434 }
435 
436 usbd_status
437 usbd_bulk_transfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
438 		   u_int16_t flags, u_int32_t timeout, void *buf,
439 		   u_int32_t *size, const char *lbl)
440 {
441 	usbd_status err;
442 	int s, error;
443 
444 	usbd_setup_xfer(xfer, pipe, 0, buf, *size,
445 			flags, timeout, usbd_bulk_transfer_cb);
446 	DPRINTFN(1, ("usbd_bulk_transfer: start transfer %d bytes\n", *size));
447 	s = splusb();		/* don't want callback until tsleep() */
448 	err = usbd_transfer(xfer);
449 	if (err != USBD_IN_PROGRESS) {
450 		splx(s);
451 		return (err);
452 	}
453 	error = tsleep((caddr_t)xfer, PZERO | PCATCH, lbl, 0);
454 	splx(s);
455 	if (error) {
456 		DPRINTF(("usbd_bulk_transfer: tsleep=%d\n", error));
457 		usbd_abort_pipe(pipe);
458 		return (USBD_INTERRUPTED);
459 	}
460 	usbd_get_xfer_status(xfer, NULL, NULL, size, &err);
461 	DPRINTFN(1,("usbd_bulk_transfer: transferred %d\n", *size));
462 	if (err) {
463 		DPRINTF(("usbd_bulk_transfer: error=%d\n", err));
464 		usbd_clear_endpoint_stall(pipe);
465 	}
466 	return (err);
467 }
468 
469 Static void usbd_intr_transfer_cb(usbd_xfer_handle xfer,
470 				  usbd_private_handle priv, usbd_status status);
471 Static void
472 usbd_intr_transfer_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
473 		      usbd_status status)
474 {
475 	wakeup(xfer);
476 }
477 
478 usbd_status
479 usbd_intr_transfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
480 		   u_int16_t flags, u_int32_t timeout, void *buf,
481 		   u_int32_t *size, const char *lbl)
482 {
483 	usbd_status err;
484 	int s, error;
485 
486 	usbd_setup_xfer(xfer, pipe, 0, buf, *size,
487 			flags, timeout, usbd_intr_transfer_cb);
488 	DPRINTFN(1, ("usbd_intr_transfer: start transfer %d bytes\n", *size));
489 	s = splusb();		/* don't want callback until tsleep() */
490 	err = usbd_transfer(xfer);
491 	if (err != USBD_IN_PROGRESS) {
492 		splx(s);
493 		return (err);
494 	}
495 	error = tsleep(xfer, PZERO | PCATCH, lbl, 0);
496 	splx(s);
497 	if (error) {
498 		DPRINTF(("usbd_intr_transfer: tsleep=%d\n", error));
499 		usbd_abort_pipe(pipe);
500 		return (USBD_INTERRUPTED);
501 	}
502 	usbd_get_xfer_status(xfer, NULL, NULL, size, &err);
503 	DPRINTFN(1,("usbd_intr_transfer: transferred %d\n", *size));
504 	if (err) {
505 		DPRINTF(("usbd_intr_transfer: error=%d\n", err));
506 		usbd_clear_endpoint_stall(pipe);
507 	}
508 	return (err);
509 }
510 
511 void
512 usb_detach_wait(device_ptr_t dv)
513 {
514 	DPRINTF(("usb_detach_wait: waiting for %s\n", USBDEVPTRNAME(dv)));
515 	if (tsleep(dv, PZERO, "usbdet", hz * 60))
516 		printf("usb_detach_wait: %s didn't detach\n",
517 		        USBDEVPTRNAME(dv));
518 	DPRINTF(("usb_detach_wait: %s done\n", USBDEVPTRNAME(dv)));
519 }
520 
521 void
522 usb_detach_wakeup(device_ptr_t dv)
523 {
524 	DPRINTF(("usb_detach_wakeup: for %s\n", USBDEVPTRNAME(dv)));
525 	wakeup(dv);
526 }
527 
528 const usb_descriptor_t *
529 usb_find_desc(usbd_device_handle dev, int type, int subtype)
530 {
531 	usbd_desc_iter_t iter;
532 	const usb_descriptor_t *desc;
533 
534 	usb_desc_iter_init(dev, &iter);
535 	for (;;) {
536 		desc = usb_desc_iter_next(&iter);
537 		if (!desc || (desc->bDescriptorType == type &&
538 			      (subtype == USBD_SUBTYPE_ANY ||
539 			       subtype == desc->bDescriptorSubtype)))
540 			break;
541 	}
542 	return desc;
543 }
544 
545 /* same as usb_find_desc(), but searches only in the specified interface. */
546 const usb_descriptor_t *
547 usb_find_desc_if(usbd_device_handle dev, int type, int subtype,
548 		 usb_interface_descriptor_t *id)
549 {
550 	usbd_desc_iter_t iter;
551 	const usb_descriptor_t *desc;
552 
553 	usb_desc_iter_init(dev, &iter);
554 
555 	iter.cur = (void *)id;		/* start from the interface desc */
556 	usb_desc_iter_next(&iter);	/* and skip it */
557 
558 	while ((desc = usb_desc_iter_next(&iter)) != NULL) {
559 		if (desc->bDescriptorType == UDESC_INTERFACE) {
560 			/* we ran into the next interface --- not found */
561 			return NULL;
562 		}
563 		if (desc->bDescriptorType == type &&
564 		    (subtype == USBD_SUBTYPE_ANY ||
565 		     subtype == desc->bDescriptorSubtype))
566 			break;
567 	}
568 	return desc;
569 }
570