usbdi_util.c revision 1.63.2.8 1 /* $NetBSD: usbdi_util.c,v 1.63.2.8 2015/04/06 15:18:14 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.8 2015/04/06 15:18:14 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_get_protocol(struct usbd_interface *iface, uint8_t *report)
247 {
248 usb_interface_descriptor_t *id = usbd_get_interface_descriptor(iface);
249 struct usbd_device *dev;
250 usb_device_request_t req;
251
252 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
253
254 DPRINTFN(4, "iface=%p, endpt=%d", iface, id->bInterfaceNumber, 0, 0);
255 if (id == NULL)
256 return USBD_IOERROR;
257 usbd_interface2device_handle(iface, &dev);
258 req.bmRequestType = UT_READ_CLASS_INTERFACE;
259 req.bRequest = UR_GET_PROTOCOL;
260 USETW(req.wValue, 0);
261 USETW(req.wIndex, id->bInterfaceNumber);
262 USETW(req.wLength, 1);
263 return usbd_do_request(dev, &req, report);
264 }
265
266 usbd_status
267 usbd_set_protocol(struct usbd_interface *iface, int report)
268 {
269 usb_interface_descriptor_t *id = usbd_get_interface_descriptor(iface);
270 struct usbd_device *dev;
271 usb_device_request_t req;
272
273 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
274
275 DPRINTFN(4, "iface=%p, report=%d, endpt=%d", iface, report,
276 id->bInterfaceNumber, 0);
277 if (id == NULL)
278 return USBD_IOERROR;
279 usbd_interface2device_handle(iface, &dev);
280 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
281 req.bRequest = UR_SET_PROTOCOL;
282 USETW(req.wValue, report);
283 USETW(req.wIndex, id->bInterfaceNumber);
284 USETW(req.wLength, 0);
285 return usbd_do_request(dev, &req, 0);
286 }
287
288 usbd_status
289 usbd_set_report(struct usbd_interface *iface, int type, int id, void *data,
290 int len)
291 {
292 usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
293 struct usbd_device *dev;
294 usb_device_request_t req;
295
296 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
297
298 DPRINTFN(4, "len=%d", len, 0, 0, 0);
299 if (ifd == NULL)
300 return USBD_IOERROR;
301 usbd_interface2device_handle(iface, &dev);
302 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
303 req.bRequest = UR_SET_REPORT;
304 USETW2(req.wValue, type, id);
305 USETW(req.wIndex, ifd->bInterfaceNumber);
306 USETW(req.wLength, len);
307 return usbd_do_request(dev, &req, data);
308 }
309
310 usbd_status
311 usbd_get_report(struct usbd_interface *iface, int type, int id, void *data,
312 int len)
313 {
314 usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
315 struct usbd_device *dev;
316 usb_device_request_t req;
317
318 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
319
320 DPRINTFN(4, "len=%d", len, 0, 0, 0);
321 if (ifd == NULL)
322 return USBD_IOERROR;
323 usbd_interface2device_handle(iface, &dev);
324 req.bmRequestType = UT_READ_CLASS_INTERFACE;
325 req.bRequest = UR_GET_REPORT;
326 USETW2(req.wValue, type, id);
327 USETW(req.wIndex, ifd->bInterfaceNumber);
328 USETW(req.wLength, len);
329 return usbd_do_request(dev, &req, data);
330 }
331
332 usbd_status
333 usbd_set_idle(struct usbd_interface *iface, int duration, int id)
334 {
335 usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
336 struct usbd_device *dev;
337 usb_device_request_t req;
338
339 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
340
341 DPRINTFN(4, "duration %d id %d", duration, id, 0, 0);
342 if (ifd == NULL)
343 return USBD_IOERROR;
344 usbd_interface2device_handle(iface, &dev);
345 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
346 req.bRequest = UR_SET_IDLE;
347 USETW2(req.wValue, duration, id);
348 USETW(req.wIndex, ifd->bInterfaceNumber);
349 USETW(req.wLength, 0);
350 return usbd_do_request(dev, &req, 0);
351 }
352
353 usbd_status
354 usbd_get_report_descriptor(struct usbd_device *dev, int ifcno,
355 int size, void *d)
356 {
357 usb_device_request_t req;
358
359 req.bmRequestType = UT_READ_INTERFACE;
360 req.bRequest = UR_GET_DESCRIPTOR;
361 USETW2(req.wValue, UDESC_REPORT, 0); /* report id should be 0 */
362 USETW(req.wIndex, ifcno);
363 USETW(req.wLength, size);
364 return usbd_do_request(dev, &req, d);
365 }
366
367 usb_hid_descriptor_t *
368 usbd_get_hid_descriptor(struct usbd_interface *ifc)
369 {
370 usb_interface_descriptor_t *idesc = usbd_get_interface_descriptor(ifc);
371 struct usbd_device *dev;
372 usb_config_descriptor_t *cdesc;
373 usb_hid_descriptor_t *hd;
374 char *p, *end;
375
376 if (idesc == NULL)
377 return NULL;
378 usbd_interface2device_handle(ifc, &dev);
379 cdesc = usbd_get_config_descriptor(dev);
380
381 p = (char *)idesc + idesc->bLength;
382 end = (char *)cdesc + UGETW(cdesc->wTotalLength);
383
384 for (; p < end; p += hd->bLength) {
385 hd = (usb_hid_descriptor_t *)p;
386 if (p + hd->bLength <= end && hd->bDescriptorType == UDESC_HID)
387 return hd;
388 if (hd->bDescriptorType == UDESC_INTERFACE)
389 break;
390 }
391 return NULL;
392 }
393
394 usbd_status
395 usbd_read_report_desc(struct usbd_interface *ifc, void **descp, int *sizep)
396 {
397 usb_interface_descriptor_t *id;
398 usb_hid_descriptor_t *hid;
399 struct usbd_device *dev;
400 usbd_status err;
401
402 usbd_interface2device_handle(ifc, &dev);
403 id = usbd_get_interface_descriptor(ifc);
404 if (id == NULL)
405 return USBD_INVAL;
406 hid = usbd_get_hid_descriptor(ifc);
407 if (hid == NULL)
408 return USBD_IOERROR;
409 *sizep = UGETW(hid->descrs[0].wDescriptorLength);
410 *descp = kmem_alloc(*sizep, KM_SLEEP);
411 if (*descp == NULL)
412 return USBD_NOMEM;
413 err = usbd_get_report_descriptor(dev, id->bInterfaceNumber,
414 *sizep, *descp);
415 if (err) {
416 kmem_free(*descp, *sizep);
417 *descp = NULL;
418 return err;
419 }
420 return USBD_NORMAL_COMPLETION;
421 }
422
423 usbd_status
424 usbd_get_config(struct usbd_device *dev, uint8_t *conf)
425 {
426 usb_device_request_t req;
427
428 req.bmRequestType = UT_READ_DEVICE;
429 req.bRequest = UR_GET_CONFIG;
430 USETW(req.wValue, 0);
431 USETW(req.wIndex, 0);
432 USETW(req.wLength, 1);
433 return usbd_do_request(dev, &req, conf);
434 }
435
436 usbd_status
437 usbd_bulk_transfer(struct usbd_xfer *xfer, struct usbd_pipe *pipe,
438 uint16_t flags, uint32_t timeout, void *buf,
439 uint32_t *size)
440 {
441 usbd_status err;
442
443 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
444
445 usbd_setup_xfer(xfer, pipe, 0, buf, *size, flags, timeout, NULL);
446 DPRINTFN(1, "start transfer %d bytes", *size, 0, 0, 0);
447 err = usbd_sync_transfer_sig(xfer);
448
449 usbd_get_xfer_status(xfer, NULL, NULL, size, NULL);
450 DPRINTFN(1, "transferred %d", *size, 0, 0, 0);
451 if (err) {
452 usbd_clear_endpoint_stall(pipe);
453 }
454 USBHIST_LOG(usbdebug, "<- done xfer %p err %d", xfer, err, 0, 0);
455
456 return err;
457 }
458
459 usbd_status
460 usbd_intr_transfer(struct usbd_xfer *xfer, struct usbd_pipe *pipe,
461 uint16_t flags, uint32_t timeout, void *buf,
462 uint32_t *size)
463 {
464 usbd_status err;
465
466 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
467
468 usbd_setup_xfer(xfer, pipe, 0, buf, *size, flags, timeout, NULL);
469
470 DPRINTFN(1, "start transfer %d bytes", *size, 0, 0, 0);
471 err = usbd_sync_transfer_sig(xfer);
472
473 usbd_get_xfer_status(xfer, NULL, NULL, size, NULL);
474
475 DPRINTFN(1, "transferred %d", *size, 0, 0, 0);
476 if (err) {
477 usbd_clear_endpoint_stall(pipe);
478 }
479 USBHIST_LOG(usbdebug, "<- done xfer %p err %d", xfer, err, 0, 0);
480
481 return err;
482 }
483
484 void
485 usb_detach_wait(device_t dv, kcondvar_t *cv, kmutex_t *lock)
486 {
487 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
488
489 DPRINTFN(1, "waiting for dv %p", dv, 0, 0, 0);
490 if (cv_timedwait(cv, lock, hz * 60)) // dv, PZERO, "usbdet", hz * 60
491 printf("usb_detach_wait: %s didn't detach\n",
492 device_xname(dv));
493 DPRINTFN(1, "done", 0, 0, 0, 0);
494 }
495
496 void
497 usb_detach_broadcast(device_t dv, kcondvar_t *cv)
498 {
499 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
500
501 DPRINTFN(1, "for dv %p", dv, 0, 0, 0);
502 cv_broadcast(cv);
503 }
504
505 void
506 usb_detach_waitold(device_t dv)
507 {
508 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
509
510 DPRINTFN(1, "waiting for dv %p", dv, 0, 0, 0);
511 if (tsleep(dv, PZERO, "usbdet", hz * 60)) /* XXXSMP ok */
512 printf("usb_detach_waitold: %s didn't detach\n",
513 device_xname(dv));
514 DPRINTFN(1, "done", 0, 0, 0, 0);
515 }
516
517 void
518 usb_detach_wakeupold(device_t dv)
519 {
520 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
521
522 DPRINTFN(1, "for dv %p", dv, 0, 0, 0);
523 wakeup(dv); /* XXXSMP ok */
524 }
525
526 const usb_cdc_descriptor_t *
527 usb_find_desc(struct usbd_device *dev, int type, int subtype)
528 {
529 usbd_desc_iter_t iter;
530 const usb_cdc_descriptor_t *desc;
531
532 usb_desc_iter_init(dev, &iter);
533 for (;;) {
534 desc = (const usb_cdc_descriptor_t *)usb_desc_iter_next(&iter);
535 if (!desc || (desc->bDescriptorType == type &&
536 (subtype == USBD_CDCSUBTYPE_ANY ||
537 subtype == desc->bDescriptorSubtype)))
538 break;
539 }
540 return desc;
541 }
542
543 /* same as usb_find_desc(), but searches only in the specified interface. */
544 const usb_cdc_descriptor_t *
545 usb_find_desc_if(struct usbd_device *dev, int type, int subtype,
546 usb_interface_descriptor_t *id)
547 {
548 usbd_desc_iter_t iter;
549 const usb_cdc_descriptor_t *desc;
550
551 if (id == NULL)
552 return usb_find_desc(dev, type, subtype);
553
554 usb_desc_iter_init(dev, &iter);
555
556 iter.cur = (void *)id; /* start from the interface desc */
557 usb_desc_iter_next(&iter); /* and skip it */
558
559 while ((desc = (const usb_cdc_descriptor_t *)usb_desc_iter_next(&iter))
560 != NULL) {
561 if (desc->bDescriptorType == UDESC_INTERFACE) {
562 /* we ran into the next interface --- not found */
563 return NULL;
564 }
565 if (desc->bDescriptorType == type &&
566 (subtype == USBD_CDCSUBTYPE_ANY ||
567 subtype == desc->bDescriptorSubtype))
568 break;
569 }
570 return desc;
571 }
572