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