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