xref: /netbsd-src/sys/dev/usb/usbdi_util.c (revision 6d322f2f4598f0d8a138f10ea648ec4fabe41f8b)
1 /*	$NetBSD: usbdi_util.c,v 1.62 2013/09/26 07:25:31 skrll Exp $	*/
2 
3 /*
4  * Copyright (c) 1998, 2012 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 and Matthew R. Green (mrg@eterna.com.au).
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  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: usbdi_util.c,v 1.62 2013/09/26 07:25:31 skrll Exp $");
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #include <sys/proc.h>
41 #include <sys/device.h>
42 #include <sys/bus.h>
43 
44 #include <dev/usb/usb.h>
45 #include <dev/usb/usbhid.h>
46 
47 #include <dev/usb/usbdi.h>
48 #include <dev/usb/usbdivar.h>
49 #include <dev/usb/usbdi_util.h>
50 
51 #ifdef USB_DEBUG
52 #define DPRINTF(x)	if (usbdebug) printf x
53 #define DPRINTFN(n,x)	if (usbdebug>(n)) printf x
54 extern int usbdebug;
55 #else
56 #define DPRINTF(x)
57 #define DPRINTFN(n,x)
58 #endif
59 
60 usbd_status
61 usbd_get_desc(usbd_device_handle dev, int type, int index, int len, void *desc)
62 {
63 	usb_device_request_t req;
64 
65 	DPRINTFN(3,("usbd_get_desc: type=%d, index=%d, len=%d\n",
66 		    type, index, len));
67 
68 	req.bmRequestType = UT_READ_DEVICE;
69 	req.bRequest = UR_GET_DESCRIPTOR;
70 	USETW2(req.wValue, type, index);
71 	USETW(req.wIndex, 0);
72 	USETW(req.wLength, len);
73 	return (usbd_do_request(dev, &req, desc));
74 }
75 
76 usbd_status
77 usbd_get_config_desc(usbd_device_handle dev, int confidx,
78 		     usb_config_descriptor_t *d)
79 {
80 	usbd_status err;
81 
82 	DPRINTFN(3,("usbd_get_config_desc: confidx=%d\n", confidx));
83 	err = usbd_get_desc(dev, UDESC_CONFIG, confidx,
84 			    USB_CONFIG_DESCRIPTOR_SIZE, d);
85 	if (err)
86 		return (err);
87 	if (d->bDescriptorType != UDESC_CONFIG) {
88 		DPRINTFN(-1,("usbd_get_config_desc: confidx=%d, bad desc "
89 			     "len=%d type=%d\n",
90 			     confidx, d->bLength, d->bDescriptorType));
91 		return (USBD_INVAL);
92 	}
93 	return (USBD_NORMAL_COMPLETION);
94 }
95 
96 usbd_status
97 usbd_get_config_desc_full(usbd_device_handle dev, int conf, void *d, int size)
98 {
99 	DPRINTFN(3,("usbd_get_config_desc_full: conf=%d\n", conf));
100 	return (usbd_get_desc(dev, UDESC_CONFIG, conf, size, d));
101 }
102 
103 usbd_status
104 usbd_get_device_desc(usbd_device_handle dev, usb_device_descriptor_t *d)
105 {
106 	DPRINTFN(3,("usbd_get_device_desc:\n"));
107 	return (usbd_get_desc(dev, UDESC_DEVICE,
108 			     0, USB_DEVICE_DESCRIPTOR_SIZE, d));
109 }
110 
111 usbd_status
112 usbd_get_device_status(usbd_device_handle dev, usb_status_t *st)
113 {
114 	usb_device_request_t req;
115 
116 	req.bmRequestType = UT_READ_DEVICE;
117 	req.bRequest = UR_GET_STATUS;
118 	USETW(req.wValue, 0);
119 	USETW(req.wIndex, 0);
120 	USETW(req.wLength, sizeof(usb_status_t));
121 	return (usbd_do_request(dev, &req, st));
122 }
123 
124 usbd_status
125 usbd_get_hub_status(usbd_device_handle dev, usb_hub_status_t *st)
126 {
127 	usb_device_request_t req;
128 
129 	req.bmRequestType = UT_READ_CLASS_DEVICE;
130 	req.bRequest = UR_GET_STATUS;
131 	USETW(req.wValue, 0);
132 	USETW(req.wIndex, 0);
133 	USETW(req.wLength, sizeof(usb_hub_status_t));
134 	return (usbd_do_request(dev, &req, st));
135 }
136 
137 usbd_status
138 usbd_set_address(usbd_device_handle dev, int addr)
139 {
140 	usb_device_request_t req;
141 
142 	req.bmRequestType = UT_WRITE_DEVICE;
143 	req.bRequest = UR_SET_ADDRESS;
144 	USETW(req.wValue, addr);
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_get_port_status(usbd_device_handle dev, int port, usb_port_status_t *ps)
152 {
153 	usb_device_request_t req;
154 
155 	req.bmRequestType = UT_READ_CLASS_OTHER;
156 	req.bRequest = UR_GET_STATUS;
157 	USETW(req.wValue, 0);
158 	USETW(req.wIndex, port);
159 	USETW(req.wLength, sizeof *ps);
160 	return (usbd_do_request(dev, &req, ps));
161 }
162 
163 usbd_status
164 usbd_clear_hub_feature(usbd_device_handle dev, int sel)
165 {
166 	usb_device_request_t req;
167 
168 	req.bmRequestType = UT_WRITE_CLASS_DEVICE;
169 	req.bRequest = UR_CLEAR_FEATURE;
170 	USETW(req.wValue, sel);
171 	USETW(req.wIndex, 0);
172 	USETW(req.wLength, 0);
173 	return (usbd_do_request(dev, &req, 0));
174 }
175 
176 usbd_status
177 usbd_set_hub_feature(usbd_device_handle dev, int sel)
178 {
179 	usb_device_request_t req;
180 
181 	req.bmRequestType = UT_WRITE_CLASS_DEVICE;
182 	req.bRequest = UR_SET_FEATURE;
183 	USETW(req.wValue, sel);
184 	USETW(req.wIndex, 0);
185 	USETW(req.wLength, 0);
186 	return (usbd_do_request(dev, &req, 0));
187 }
188 
189 usbd_status
190 usbd_clear_port_feature(usbd_device_handle dev, int port, int sel)
191 {
192 	usb_device_request_t req;
193 
194 	req.bmRequestType = UT_WRITE_CLASS_OTHER;
195 	req.bRequest = UR_CLEAR_FEATURE;
196 	USETW(req.wValue, sel);
197 	USETW(req.wIndex, port);
198 	USETW(req.wLength, 0);
199 	return (usbd_do_request(dev, &req, 0));
200 }
201 
202 usbd_status
203 usbd_set_port_feature(usbd_device_handle dev, int port, int sel)
204 {
205 	usb_device_request_t req;
206 
207 	req.bmRequestType = UT_WRITE_CLASS_OTHER;
208 	req.bRequest = UR_SET_FEATURE;
209 	USETW(req.wValue, sel);
210 	USETW(req.wIndex, port);
211 	USETW(req.wLength, 0);
212 	return (usbd_do_request(dev, &req, 0));
213 }
214 
215 usbd_status
216 usbd_get_protocol(usbd_interface_handle iface, u_int8_t *report)
217 {
218 	usb_interface_descriptor_t *id = usbd_get_interface_descriptor(iface);
219 	usbd_device_handle dev;
220 	usb_device_request_t req;
221 
222 	DPRINTFN(4, ("usbd_get_protocol: iface=%p, endpt=%d\n",
223 		     iface, id->bInterfaceNumber));
224 	if (id == NULL)
225 		return (USBD_IOERROR);
226 	usbd_interface2device_handle(iface, &dev);
227 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
228 	req.bRequest = UR_GET_PROTOCOL;
229 	USETW(req.wValue, 0);
230 	USETW(req.wIndex, id->bInterfaceNumber);
231 	USETW(req.wLength, 1);
232 	return (usbd_do_request(dev, &req, report));
233 }
234 
235 usbd_status
236 usbd_set_protocol(usbd_interface_handle iface, int report)
237 {
238 	usb_interface_descriptor_t *id = usbd_get_interface_descriptor(iface);
239 	usbd_device_handle dev;
240 	usb_device_request_t req;
241 
242 	DPRINTFN(4, ("usbd_set_protocol: iface=%p, report=%d, endpt=%d\n",
243 		     iface, report, id->bInterfaceNumber));
244 	if (id == NULL)
245 		return (USBD_IOERROR);
246 	usbd_interface2device_handle(iface, &dev);
247 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
248 	req.bRequest = UR_SET_PROTOCOL;
249 	USETW(req.wValue, report);
250 	USETW(req.wIndex, id->bInterfaceNumber);
251 	USETW(req.wLength, 0);
252 	return (usbd_do_request(dev, &req, 0));
253 }
254 
255 usbd_status
256 usbd_set_report(usbd_interface_handle iface, int type, int id, void *data,
257 		int len)
258 {
259 	usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
260 	usbd_device_handle dev;
261 	usb_device_request_t req;
262 
263 	DPRINTFN(4, ("usbd_set_report: len=%d\n", len));
264 	if (ifd == NULL)
265 		return (USBD_IOERROR);
266 	usbd_interface2device_handle(iface, &dev);
267 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
268 	req.bRequest = UR_SET_REPORT;
269 	USETW2(req.wValue, type, id);
270 	USETW(req.wIndex, ifd->bInterfaceNumber);
271 	USETW(req.wLength, len);
272 	return (usbd_do_request(dev, &req, data));
273 }
274 
275 usbd_status
276 usbd_get_report(usbd_interface_handle iface, int type, int id, void *data,
277 		int len)
278 {
279 	usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
280 	usbd_device_handle dev;
281 	usb_device_request_t req;
282 
283 	DPRINTFN(4, ("usbd_get_report: len=%d\n", len));
284 	if (ifd == NULL)
285 		return (USBD_IOERROR);
286 	usbd_interface2device_handle(iface, &dev);
287 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
288 	req.bRequest = UR_GET_REPORT;
289 	USETW2(req.wValue, type, id);
290 	USETW(req.wIndex, ifd->bInterfaceNumber);
291 	USETW(req.wLength, len);
292 	return (usbd_do_request(dev, &req, data));
293 }
294 
295 usbd_status
296 usbd_set_idle(usbd_interface_handle iface, int duration, int id)
297 {
298 	usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
299 	usbd_device_handle dev;
300 	usb_device_request_t req;
301 
302 	DPRINTFN(4, ("usbd_set_idle: %d %d\n", duration, id));
303 	if (ifd == NULL)
304 		return (USBD_IOERROR);
305 	usbd_interface2device_handle(iface, &dev);
306 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
307 	req.bRequest = UR_SET_IDLE;
308 	USETW2(req.wValue, duration, id);
309 	USETW(req.wIndex, ifd->bInterfaceNumber);
310 	USETW(req.wLength, 0);
311 	return (usbd_do_request(dev, &req, 0));
312 }
313 
314 usbd_status
315 usbd_get_report_descriptor(usbd_device_handle dev, int ifcno,
316 			   int size, void *d)
317 {
318 	usb_device_request_t req;
319 
320 	req.bmRequestType = UT_READ_INTERFACE;
321 	req.bRequest = UR_GET_DESCRIPTOR;
322 	USETW2(req.wValue, UDESC_REPORT, 0); /* report id should be 0 */
323 	USETW(req.wIndex, ifcno);
324 	USETW(req.wLength, size);
325 	return (usbd_do_request(dev, &req, d));
326 }
327 
328 usb_hid_descriptor_t *
329 usbd_get_hid_descriptor(usbd_interface_handle ifc)
330 {
331 	usb_interface_descriptor_t *idesc = usbd_get_interface_descriptor(ifc);
332 	usbd_device_handle dev;
333 	usb_config_descriptor_t *cdesc;
334 	usb_hid_descriptor_t *hd;
335 	char *p, *end;
336 
337 	if (idesc == NULL)
338 		return (NULL);
339 	usbd_interface2device_handle(ifc, &dev);
340 	cdesc = usbd_get_config_descriptor(dev);
341 
342 	p = (char *)idesc + idesc->bLength;
343 	end = (char *)cdesc + UGETW(cdesc->wTotalLength);
344 
345 	for (; p < end; p += hd->bLength) {
346 		hd = (usb_hid_descriptor_t *)p;
347 		if (p + hd->bLength <= end && hd->bDescriptorType == UDESC_HID)
348 			return (hd);
349 		if (hd->bDescriptorType == UDESC_INTERFACE)
350 			break;
351 	}
352 	return (NULL);
353 }
354 
355 usbd_status
356 usbd_read_report_desc(usbd_interface_handle ifc, void **descp, int *sizep,
357 		       struct malloc_type * mem)
358 {
359 	usb_interface_descriptor_t *id;
360 	usb_hid_descriptor_t *hid;
361 	usbd_device_handle dev;
362 	usbd_status err;
363 
364 	usbd_interface2device_handle(ifc, &dev);
365 	id = usbd_get_interface_descriptor(ifc);
366 	if (id == NULL)
367 		return (USBD_INVAL);
368 	hid = usbd_get_hid_descriptor(ifc);
369 	if (hid == NULL)
370 		return (USBD_IOERROR);
371 	*sizep = UGETW(hid->descrs[0].wDescriptorLength);
372 	*descp = malloc(*sizep, mem, M_NOWAIT);
373 	if (*descp == NULL)
374 		return (USBD_NOMEM);
375 	err = usbd_get_report_descriptor(dev, id->bInterfaceNumber,
376 					 *sizep, *descp);
377 	if (err) {
378 		free(*descp, mem);
379 		*descp = NULL;
380 		return (err);
381 	}
382 	return (USBD_NORMAL_COMPLETION);
383 }
384 
385 usbd_status
386 usbd_get_config(usbd_device_handle dev, u_int8_t *conf)
387 {
388 	usb_device_request_t req;
389 
390 	req.bmRequestType = UT_READ_DEVICE;
391 	req.bRequest = UR_GET_CONFIG;
392 	USETW(req.wValue, 0);
393 	USETW(req.wIndex, 0);
394 	USETW(req.wLength, 1);
395 	return (usbd_do_request(dev, &req, conf));
396 }
397 
398 usbd_status
399 usbd_bulk_transfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
400 		   u_int16_t flags, u_int32_t timeout, void *buf,
401 		   u_int32_t *size, const char *lbl)
402 {
403 	usbd_status err;
404 
405 	usbd_setup_xfer(xfer, pipe, 0, buf, *size, flags, timeout, NULL);
406 
407 	DPRINTFN(1, ("usbd_bulk_transfer: start transfer %d bytes\n", *size));
408 	err = usbd_sync_transfer_sig(xfer);
409 
410 	usbd_get_xfer_status(xfer, NULL, NULL, size, NULL);
411 	DPRINTFN(1,("usbd_bulk_transfer: transferred %d\n", *size));
412 	if (err) {
413 		DPRINTF(("usbd_bulk_transfer: error=%d\n", err));
414 		usbd_clear_endpoint_stall(pipe);
415 	}
416 
417 	return (err);
418 }
419 
420 usbd_status
421 usbd_intr_transfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
422 		   u_int16_t flags, u_int32_t timeout, void *buf,
423 		   u_int32_t *size, const char *lbl)
424 {
425 	usbd_status err;
426 
427 	usbd_setup_xfer(xfer, pipe, 0, buf, *size, flags, timeout, NULL);
428 
429 	DPRINTFN(1, ("usbd_intr_transfer: start transfer %d bytes\n", *size));
430 	err = usbd_sync_transfer_sig(xfer);
431 
432 	usbd_get_xfer_status(xfer, NULL, NULL, size, NULL);
433 
434 	DPRINTFN(1,("usbd_intr_transfer: transferred %d\n", *size));
435 	if (err) {
436 		DPRINTF(("usbd_intr_transfer: error=%d\n", err));
437 		usbd_clear_endpoint_stall(pipe);
438 	}
439 	return (err);
440 }
441 
442 void
443 usb_detach_wait(device_t dv, kcondvar_t *cv, kmutex_t *lock)
444 {
445 	DPRINTF(("usb_detach_wait: waiting for %s\n", device_xname(dv)));
446 	if (cv_timedwait(cv, lock, hz * 60))	// dv, PZERO, "usbdet", hz * 60
447 		printf("usb_detach_wait: %s didn't detach\n",
448 		        device_xname(dv));
449 	DPRINTF(("usb_detach_wait: %s done\n", device_xname(dv)));
450 }
451 
452 void
453 usb_detach_broadcast(device_t dv, kcondvar_t *cv)
454 {
455 	DPRINTF(("usb_detach_broadcast: for %s\n", device_xname(dv)));
456 	cv_broadcast(cv);
457 }
458 
459 void
460 usb_detach_waitold(device_t dv)
461 {
462 	DPRINTF(("usb_detach_waitold: waiting for %s\n", device_xname(dv)));
463 	if (tsleep(dv, PZERO, "usbdet", hz * 60)) /* XXXSMP ok */
464 		printf("usb_detach_waitold: %s didn't detach\n",
465 		        device_xname(dv));
466 	DPRINTF(("usb_detach_waitold: %s done\n", device_xname(dv)));
467 }
468 
469 void
470 usb_detach_wakeupold(device_t dv)
471 {
472 	DPRINTF(("usb_detach_wakeupold: for %s\n", device_xname(dv)));
473 	wakeup(dv); /* XXXSMP ok */
474 }
475 
476 const usb_cdc_descriptor_t *
477 usb_find_desc(usbd_device_handle dev, int type, int subtype)
478 {
479 	usbd_desc_iter_t iter;
480 	const usb_cdc_descriptor_t *desc;
481 
482 	usb_desc_iter_init(dev, &iter);
483 	for (;;) {
484 		desc = (const usb_cdc_descriptor_t *)usb_desc_iter_next(&iter);
485 		if (!desc || (desc->bDescriptorType == type &&
486 			      (subtype == USBD_CDCSUBTYPE_ANY ||
487 			       subtype == desc->bDescriptorSubtype)))
488 			break;
489 	}
490 	return desc;
491 }
492 
493 /* same as usb_find_desc(), but searches only in the specified interface. */
494 const usb_cdc_descriptor_t *
495 usb_find_desc_if(usbd_device_handle dev, int type, int subtype,
496 		 usb_interface_descriptor_t *id)
497 {
498 	usbd_desc_iter_t iter;
499 	const usb_cdc_descriptor_t *desc;
500 
501 	if (id == NULL)
502 		return usb_find_desc(dev, type, subtype);
503 
504 	usb_desc_iter_init(dev, &iter);
505 
506 	iter.cur = (void *)id;		/* start from the interface desc */
507 	usb_desc_iter_next(&iter);	/* and skip it */
508 
509 	while ((desc = (const usb_cdc_descriptor_t *)usb_desc_iter_next(&iter))
510 	       != NULL) {
511 		if (desc->bDescriptorType == UDESC_INTERFACE) {
512 			/* we ran into the next interface --- not found */
513 			return NULL;
514 		}
515 		if (desc->bDescriptorType == type &&
516 		    (subtype == USBD_CDCSUBTYPE_ANY ||
517 		     subtype == desc->bDescriptorSubtype))
518 			break;
519 	}
520 	return desc;
521 }
522