usbdi_util.c revision 1.78 1 /* $NetBSD: usbdi_util.c,v 1.78 2020/02/08 08:47:27 maxv 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.78 2020/02/08 08:47:27 maxv 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/usb_quirks.h>
54 #include <dev/usb/usbhist.h>
55
56 #define DPRINTF(FMT,A,B,C,D) USBHIST_LOGN(usbdebug,1,FMT,A,B,C,D)
57 #define DPRINTFN(N,FMT,A,B,C,D) USBHIST_LOGN(usbdebug,N,FMT,A,B,C,D)
58
59 usbd_status
60 usbd_get_desc(struct usbd_device *dev, int type, int index, int len, void *desc)
61 {
62 usb_device_request_t req;
63 usbd_status err;
64
65 USBHIST_FUNC();
66 USBHIST_CALLARGS(usbdebug, "type=%jd, index=%jd, len=%jd",
67 type, index, len, 0);
68
69 /*
70 * Provide hard-coded configuration descriptors
71 * for devices that may corrupt it. This cannot
72 * be done for device descriptors which are used
73 * to identify the device.
74 */
75 if (type != UDESC_DEVICE &&
76 dev->ud_quirks->uq_flags & UQ_DESC_CORRUPT) {
77 err = usbd_get_desc_fake(dev, type, index, len, desc);
78 goto out;
79 }
80
81 req.bmRequestType = UT_READ_DEVICE;
82 req.bRequest = UR_GET_DESCRIPTOR;
83 USETW2(req.wValue, type, index);
84 USETW(req.wIndex, 0);
85 USETW(req.wLength, len);
86 err = usbd_do_request(dev, &req, desc);
87
88 out:
89 return err;
90 }
91
92 usbd_status
93 usbd_get_config_desc(struct usbd_device *dev, int confidx,
94 usb_config_descriptor_t *d)
95 {
96 USBHIST_FUNC();
97 USBHIST_CALLARGS(usbdebug, "confidx=%jd", confidx, 0, 0, 0);
98 usbd_status err;
99
100 err = usbd_get_desc(dev, UDESC_CONFIG, confidx,
101 USB_CONFIG_DESCRIPTOR_SIZE, d);
102 if (err)
103 return err;
104 if (d->bDescriptorType != UDESC_CONFIG) {
105 DPRINTFN(1, "confidx=%jd, bad desc len=%d type=%d",
106 confidx, d->bLength, d->bDescriptorType, 0);
107 return USBD_INVAL;
108 }
109 return USBD_NORMAL_COMPLETION;
110 }
111
112 usbd_status
113 usbd_get_config_desc_full(struct usbd_device *dev, int conf, void *d, int size)
114 {
115 USBHIST_FUNC(); USBHIST_CALLARGS(usbdebug, "conf=%jd", conf, 0, 0, 0);
116
117 return usbd_get_desc(dev, UDESC_CONFIG, conf, size, d);
118 }
119
120 usbd_status
121 usbd_get_bos_desc(struct usbd_device *dev, int confidx,
122 usb_bos_descriptor_t *d)
123 {
124 USBHIST_FUNC();
125 USBHIST_CALLARGS(usbdebug, "confidx=%jd", confidx, 0, 0, 0);
126 usbd_status err;
127
128 err = usbd_get_desc(dev, UDESC_BOS, confidx,
129 USB_BOS_DESCRIPTOR_SIZE, d);
130 if (err)
131 return err;
132 if (d->bDescriptorType != UDESC_BOS) {
133 DPRINTFN(1, "confidx=%jd, bad desc len=%d type=%d",
134 confidx, d->bLength, d->bDescriptorType, 0);
135 return USBD_INVAL;
136 }
137 return USBD_NORMAL_COMPLETION;
138 }
139
140 usbd_status
141 usbd_get_bos_desc_full(struct usbd_device *dev, int conf, void *d, int size)
142 {
143 USBHIST_FUNC(); USBHIST_CALLARGS(usbdebug, "conf=%jd", conf, 0, 0, 0);
144
145 return usbd_get_desc(dev, UDESC_BOS, conf, size, d);
146 }
147
148 usbd_status
149 usbd_get_device_desc(struct usbd_device *dev, usb_device_descriptor_t *d)
150 {
151 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
152
153 return usbd_get_desc(dev, UDESC_DEVICE,
154 0, USB_DEVICE_DESCRIPTOR_SIZE, d);
155 }
156
157 /*
158 * Get the first 8 bytes of the device descriptor.
159 * Do as Windows does: try to read 64 bytes -- there are devices which
160 * recognize the initial descriptor fetch (before the control endpoint's
161 * MaxPacketSize is known by the host) by exactly this length.
162 */
163 usbd_status
164 usbd_get_initial_ddesc(struct usbd_device *dev, usb_device_descriptor_t *desc)
165 {
166 USBHIST_FUNC();
167 USBHIST_CALLARGS(usbdebug, "dev %#jx", (uintptr_t)dev, 0, 0, 0);
168 usb_device_request_t req;
169 char buf[64];
170 int res, actlen;
171
172 req.bmRequestType = UT_READ_DEVICE;
173 req.bRequest = UR_GET_DESCRIPTOR;
174 USETW2(req.wValue, UDESC_DEVICE, 0);
175 USETW(req.wIndex, 0);
176 USETW(req.wLength, 8);
177 res = usbd_do_request_flags(dev, &req, buf, USBD_SHORT_XFER_OK,
178 &actlen, USBD_DEFAULT_TIMEOUT);
179 if (res)
180 return res;
181 if (actlen < 8)
182 return USBD_SHORT_XFER;
183 memcpy(desc, buf, 8);
184 return USBD_NORMAL_COMPLETION;
185 }
186
187 usbd_status
188 usbd_get_string_desc(struct usbd_device *dev, int sindex, int langid,
189 usb_string_descriptor_t *sdesc, int *sizep)
190 {
191 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
192 usb_device_request_t req;
193 usbd_status err;
194 int actlen;
195
196 /*
197 * Pass a full-sized buffer to usbd_do_request_len(). At least
198 * one device has been seen returning additional data beyond the
199 * provided buffers (2-bytes written shortly after the request
200 * claims to have completed and returned the 2 byte header,
201 * corrupting other memory.)
202 */
203 req.bmRequestType = UT_READ_DEVICE;
204 req.bRequest = UR_GET_DESCRIPTOR;
205 USETW2(req.wValue, UDESC_STRING, sindex);
206 USETW(req.wIndex, langid);
207 USETW(req.wLength, 2); /* only size byte first */
208 err = usbd_do_request_len(dev, &req, sizeof(*sdesc), sdesc,
209 USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT);
210 if (err)
211 return err;
212
213 if (actlen < 2)
214 return USBD_SHORT_XFER;
215
216 if (sdesc->bLength > sizeof(*sdesc))
217 return USBD_INVAL;
218 USETW(req.wLength, sdesc->bLength); /* the whole string */
219 err = usbd_do_request_len(dev, &req, sizeof(*sdesc), sdesc,
220 USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT);
221 if (err)
222 return err;
223
224 if (actlen != sdesc->bLength) {
225 DPRINTF("expected %jd, got %jd", sdesc->bLength, actlen, 0, 0);
226 }
227
228 *sizep = actlen;
229 return USBD_NORMAL_COMPLETION;
230 }
231
232 /* -------------------------------------------------------------------------- */
233
234 usbd_status
235 usbd_get_device_status(struct usbd_device *dev, usb_status_t *st)
236 {
237 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
238 usb_device_request_t req;
239
240 req.bmRequestType = UT_READ_DEVICE;
241 req.bRequest = UR_GET_STATUS;
242 USETW(req.wValue, 0);
243 USETW(req.wIndex, 0);
244 USETW(req.wLength, sizeof(usb_status_t));
245 return usbd_do_request(dev, &req, st);
246 }
247
248 usbd_status
249 usbd_get_hub_status(struct usbd_device *dev, usb_hub_status_t *st)
250 {
251 USBHIST_FUNC();
252 USBHIST_CALLARGS(usbdebug, "dev %#jx", (uintptr_t)dev, 0, 0, 0);
253 usb_device_request_t req;
254
255 req.bmRequestType = UT_READ_CLASS_DEVICE;
256 req.bRequest = UR_GET_STATUS;
257 USETW(req.wValue, 0);
258 USETW(req.wIndex, 0);
259 USETW(req.wLength, sizeof(usb_hub_status_t));
260 return usbd_do_request(dev, &req, st);
261 }
262
263 usbd_status
264 usbd_get_port_status(struct usbd_device *dev, int port, usb_port_status_t *ps)
265 {
266 USBHIST_FUNC();
267 USBHIST_CALLARGS(usbdebug, "dev %#jx port %jd",
268 (uintptr_t)dev, port, 0, 0);
269 usb_device_request_t req;
270
271 req.bmRequestType = UT_READ_CLASS_OTHER;
272 req.bRequest = UR_GET_STATUS;
273 USETW(req.wValue, 0);
274 USETW(req.wIndex, port);
275 USETW(req.wLength, sizeof(*ps));
276 return usbd_do_request(dev, &req, ps);
277 }
278
279 /* USB 3.1 10.16.2.6, 10.16.2.6.3 */
280 usbd_status
281 usbd_get_port_status_ext(struct usbd_device *dev, int port,
282 usb_port_status_ext_t *pse)
283 {
284 USBHIST_FUNC();
285 USBHIST_CALLARGS(usbdebug, "dev %#jx port %jd",
286 (uintptr_t)dev, port, 0, 0);
287 usb_device_request_t req;
288
289 req.bmRequestType = UT_READ_CLASS_OTHER;
290 req.bRequest = UR_GET_STATUS;
291 USETW2(req.wValue, 0, UR_PST_EXT_PORT_STATUS);
292 USETW(req.wIndex, port);
293 USETW(req.wLength, sizeof(*pse));
294 return usbd_do_request(dev, &req, pse);
295 }
296
297 /* -------------------------------------------------------------------------- */
298
299 usbd_status
300 usbd_clear_hub_feature(struct usbd_device *dev, int sel)
301 {
302 USBHIST_FUNC();
303 USBHIST_CALLARGS(usbdebug, "dev %#jx sel %jd",
304 (uintptr_t)dev, sel, 0, 0);
305 usb_device_request_t req;
306
307 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
308 req.bRequest = UR_CLEAR_FEATURE;
309 USETW(req.wValue, sel);
310 USETW(req.wIndex, 0);
311 USETW(req.wLength, 0);
312 return usbd_do_request(dev, &req, 0);
313 }
314
315 usbd_status
316 usbd_set_hub_feature(struct usbd_device *dev, int sel)
317 {
318 USBHIST_FUNC();
319 USBHIST_CALLARGS(usbdebug,
320 "dev %#jx sel %jd", (uintptr_t)dev, sel, 0, 0);
321 usb_device_request_t req;
322
323 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
324 req.bRequest = UR_SET_FEATURE;
325 USETW(req.wValue, sel);
326 USETW(req.wIndex, 0);
327 USETW(req.wLength, 0);
328 return usbd_do_request(dev, &req, 0);
329 }
330
331 usbd_status
332 usbd_clear_port_feature(struct usbd_device *dev, int port, int sel)
333 {
334 USBHIST_FUNC();
335 USBHIST_CALLARGS(usbdebug, "dev %#jx port %jd sel %jd",
336 (uintptr_t)dev, port, sel, 0);
337 usb_device_request_t req;
338
339 req.bmRequestType = UT_WRITE_CLASS_OTHER;
340 req.bRequest = UR_CLEAR_FEATURE;
341 USETW(req.wValue, sel);
342 USETW(req.wIndex, port);
343 USETW(req.wLength, 0);
344 return usbd_do_request(dev, &req, 0);
345 }
346
347 usbd_status
348 usbd_set_port_feature(struct usbd_device *dev, int port, int sel)
349 {
350 USBHIST_FUNC();
351 USBHIST_CALLARGS(usbdebug, "dev %#jx port %jd sel %.d",
352 (uintptr_t)dev, sel, 0, 0);
353 usb_device_request_t req;
354
355 req.bmRequestType = UT_WRITE_CLASS_OTHER;
356 req.bRequest = UR_SET_FEATURE;
357 USETW(req.wValue, sel);
358 USETW(req.wIndex, port);
359 USETW(req.wLength, 0);
360 return usbd_do_request(dev, &req, 0);
361 }
362
363 usbd_status
364 usbd_set_port_u1_timeout(struct usbd_device *dev, int port, int timeout)
365 {
366 USBHIST_FUNC();
367 USBHIST_CALLARGS(usbdebug, "dev %#jx port %jd timeout %.d",
368 (uintptr_t)dev, port, timeout, 0);
369 usb_device_request_t req;
370
371 req.bmRequestType = UT_WRITE_CLASS_OTHER;
372 req.bRequest = UR_SET_FEATURE;
373 USETW(req.wValue, UHF_PORT_U1_TIMEOUT);
374 USETW2(req.wIndex, timeout, port);
375 USETW(req.wLength, 0);
376 return usbd_do_request(dev, &req, 0);
377 }
378
379 usbd_status
380 usbd_set_port_u2_timeout(struct usbd_device *dev, int port, int timeout)
381 {
382 USBHIST_FUNC();
383 USBHIST_CALLARGS(usbdebug, "dev %#jx port %jd timeout %jd",
384 (uintptr_t)dev, port, timeout, 0);
385 usb_device_request_t req;
386
387 req.bmRequestType = UT_WRITE_CLASS_OTHER;
388 req.bRequest = UR_SET_FEATURE;
389 USETW(req.wValue, UHF_PORT_U2_TIMEOUT);
390 USETW2(req.wIndex, timeout, port);
391 USETW(req.wLength, 0);
392 return usbd_do_request(dev, &req, 0);
393 }
394
395 usbd_status
396 usbd_clear_endpoint_feature(struct usbd_device *dev, int epaddr, int sel)
397 {
398 USBHIST_FUNC();
399 usb_device_request_t req;
400
401 req.bmRequestType = UT_WRITE_ENDPOINT;
402 req.bRequest = UR_CLEAR_FEATURE;
403 USETW(req.wValue, sel);
404 USETW(req.wIndex, epaddr);
405 USETW(req.wLength, 0);
406 return usbd_do_request(dev, &req, 0);
407 }
408
409 /* -------------------------------------------------------------------------- */
410
411 usbd_status
412 usbd_get_config(struct usbd_device *dev, uint8_t *conf)
413 {
414 USBHIST_FUNC();
415 USBHIST_CALLARGS(usbdebug, "dev %#jx", (uintptr_t)dev, 0, 0, 0);
416 usb_device_request_t req;
417
418 req.bmRequestType = UT_READ_DEVICE;
419 req.bRequest = UR_GET_CONFIG;
420 USETW(req.wValue, 0);
421 USETW(req.wIndex, 0);
422 USETW(req.wLength, 1);
423 return usbd_do_request(dev, &req, conf);
424 }
425
426 usbd_status
427 usbd_set_config(struct usbd_device *dev, int conf)
428 {
429 USBHIST_FUNC();
430 USBHIST_CALLARGS(usbdebug, "dev %#jx conf %jd",
431 (uintptr_t)dev, conf, 0, 0);
432 usb_device_request_t req;
433
434 req.bmRequestType = UT_WRITE_DEVICE;
435 req.bRequest = UR_SET_CONFIG;
436 USETW(req.wValue, conf);
437 USETW(req.wIndex, 0);
438 USETW(req.wLength, 0);
439 return usbd_do_request(dev, &req, 0);
440 }
441
442 usbd_status
443 usbd_set_address(struct usbd_device *dev, int addr)
444 {
445 USBHIST_FUNC();
446 USBHIST_CALLARGS(usbdebug, "dev %#jx addr %jd",
447 (uintptr_t)dev, addr, 0, 0);
448 usb_device_request_t req;
449
450 req.bmRequestType = UT_WRITE_DEVICE;
451 req.bRequest = UR_SET_ADDRESS;
452 USETW(req.wValue, addr);
453 USETW(req.wIndex, 0);
454 USETW(req.wLength, 0);
455 return usbd_do_request(dev, &req, 0);
456 }
457
458 usbd_status
459 usbd_set_idle(struct usbd_interface *iface, int duration, int id)
460 {
461 usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
462 struct usbd_device *dev;
463 usb_device_request_t req;
464
465 USBHIST_FUNC();
466 USBHIST_CALLARGS(usbdebug, "duration %jd id %jd", duration, id, 0, 0);
467
468 if (ifd == NULL)
469 return USBD_IOERROR;
470 usbd_interface2device_handle(iface, &dev);
471 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
472 req.bRequest = UR_SET_IDLE;
473 USETW2(req.wValue, duration, id);
474 USETW(req.wIndex, ifd->bInterfaceNumber);
475 USETW(req.wLength, 0);
476 return usbd_do_request(dev, &req, 0);
477 }
478
479 /* -------------------------------------------------------------------------- */
480
481 usbd_status
482 usbd_get_protocol(struct usbd_interface *iface, uint8_t *report)
483 {
484 usb_interface_descriptor_t *id = usbd_get_interface_descriptor(iface);
485 struct usbd_device *dev;
486 usb_device_request_t req;
487
488 USBHIST_FUNC();
489 USBHIST_CALLARGS(usbdebug, "iface=%#jx, endpt=%jd",
490 (uintptr_t)iface, id->bInterfaceNumber, 0, 0);
491
492 if (id == NULL)
493 return USBD_IOERROR;
494
495 usbd_interface2device_handle(iface, &dev);
496 req.bmRequestType = UT_READ_CLASS_INTERFACE;
497 req.bRequest = UR_GET_PROTOCOL;
498 USETW(req.wValue, 0);
499 USETW(req.wIndex, id->bInterfaceNumber);
500 USETW(req.wLength, 1);
501 return usbd_do_request(dev, &req, report);
502 }
503
504 usbd_status
505 usbd_set_protocol(struct usbd_interface *iface, int report)
506 {
507 usb_interface_descriptor_t *id = usbd_get_interface_descriptor(iface);
508 struct usbd_device *dev;
509 usb_device_request_t req;
510
511 USBHIST_FUNC();
512 USBHIST_CALLARGS(usbdebug, "iface=%#jx, report=%jd, endpt=%jd",
513 (uintptr_t)iface, report, id->bInterfaceNumber, 0);
514
515 if (id == NULL)
516 return USBD_IOERROR;
517
518 usbd_interface2device_handle(iface, &dev);
519 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
520 req.bRequest = UR_SET_PROTOCOL;
521 USETW(req.wValue, report);
522 USETW(req.wIndex, id->bInterfaceNumber);
523 USETW(req.wLength, 0);
524 return usbd_do_request(dev, &req, 0);
525 }
526
527 /* -------------------------------------------------------------------------- */
528
529 usbd_status
530 usbd_set_report(struct usbd_interface *iface, int type, int id, void *data,
531 int len)
532 {
533 usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
534 struct usbd_device *dev;
535 usb_device_request_t req;
536
537 USBHIST_FUNC();
538 USBHIST_CALLARGS(usbdebug, "len=%jd", len, 0, 0, 0);
539
540 if (ifd == NULL)
541 return USBD_IOERROR;
542 usbd_interface2device_handle(iface, &dev);
543 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
544 req.bRequest = UR_SET_REPORT;
545 USETW2(req.wValue, type, id);
546 USETW(req.wIndex, ifd->bInterfaceNumber);
547 USETW(req.wLength, len);
548 return usbd_do_request(dev, &req, data);
549 }
550
551 usbd_status
552 usbd_get_report(struct usbd_interface *iface, int type, int id, void *data,
553 int len)
554 {
555 usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
556 struct usbd_device *dev;
557 usb_device_request_t req;
558
559 USBHIST_FUNC(); USBHIST_CALLARGS(usbdebug, "len=%jd", len, 0, 0, 0);
560
561 if (ifd == NULL)
562 return USBD_IOERROR;
563 usbd_interface2device_handle(iface, &dev);
564 req.bmRequestType = UT_READ_CLASS_INTERFACE;
565 req.bRequest = UR_GET_REPORT;
566 USETW2(req.wValue, type, id);
567 USETW(req.wIndex, ifd->bInterfaceNumber);
568 USETW(req.wLength, len);
569 return usbd_do_request(dev, &req, data);
570 }
571
572 usbd_status
573 usbd_get_report_descriptor(struct usbd_device *dev, int ifcno,
574 int size, void *d)
575 {
576 USBHIST_FUNC();
577 USBHIST_CALLARGS(usbdebug, "dev %#jx ifcno %jd size %jd",
578 (uintptr_t)dev, ifcno, size, 0);
579 usb_device_request_t req;
580
581 req.bmRequestType = UT_READ_INTERFACE;
582 req.bRequest = UR_GET_DESCRIPTOR;
583 USETW2(req.wValue, UDESC_REPORT, 0); /* report id should be 0 */
584 USETW(req.wIndex, ifcno);
585 USETW(req.wLength, size);
586 return usbd_do_request(dev, &req, d);
587 }
588
589 /* -------------------------------------------------------------------------- */
590
591 usb_hid_descriptor_t *
592 usbd_get_hid_descriptor(struct usbd_interface *ifc)
593 {
594 usb_interface_descriptor_t *idesc = usbd_get_interface_descriptor(ifc);
595 struct usbd_device *dev;
596 usb_config_descriptor_t *cdesc;
597 usb_hid_descriptor_t *hd;
598 char *p, *end;
599
600 if (idesc == NULL)
601 return NULL;
602 usbd_interface2device_handle(ifc, &dev);
603 cdesc = usbd_get_config_descriptor(dev);
604
605 p = (char *)idesc + idesc->bLength;
606 end = (char *)cdesc + UGETW(cdesc->wTotalLength);
607
608 for (; p < end; p += hd->bLength) {
609 hd = (usb_hid_descriptor_t *)p;
610 if (p + hd->bLength <= end &&
611 hd->bLength >= USB_HID_DESCRIPTOR_SIZE(0) &&
612 hd->bDescriptorType == UDESC_HID)
613 return hd;
614 if (hd->bDescriptorType == UDESC_INTERFACE)
615 break;
616 }
617 return NULL;
618 }
619
620 usbd_status
621 usbd_read_report_desc(struct usbd_interface *ifc, void **descp, int *sizep)
622 {
623 usb_interface_descriptor_t *id;
624 usb_hid_descriptor_t *hid;
625 struct usbd_device *dev;
626 usbd_status err;
627
628 usbd_interface2device_handle(ifc, &dev);
629 id = usbd_get_interface_descriptor(ifc);
630 if (id == NULL)
631 return USBD_INVAL;
632 hid = usbd_get_hid_descriptor(ifc);
633 if (hid == NULL)
634 return USBD_IOERROR;
635 *sizep = UGETW(hid->descrs[0].wDescriptorLength);
636 if (*sizep == 0)
637 return USBD_INVAL;
638 *descp = kmem_alloc(*sizep, KM_SLEEP);
639 err = usbd_get_report_descriptor(dev, id->bInterfaceNumber,
640 *sizep, *descp);
641 if (err) {
642 kmem_free(*descp, *sizep);
643 *descp = NULL;
644 return err;
645 }
646 return USBD_NORMAL_COMPLETION;
647 }
648
649 usbd_status
650 usbd_bulk_transfer(struct usbd_xfer *xfer, struct usbd_pipe *pipe,
651 uint16_t flags, uint32_t timeout, void *buf, uint32_t *size)
652 {
653 usbd_status err;
654
655 USBHIST_FUNC();
656 USBHIST_CALLARGS(usbdebug, "start transfer %jd bytes", *size, 0, 0, 0);
657
658 usbd_setup_xfer(xfer, 0, buf, *size, flags, timeout, NULL);
659 err = usbd_sync_transfer_sig(xfer);
660
661 usbd_get_xfer_status(xfer, NULL, NULL, size, NULL);
662 DPRINTFN(1, "transferred %jd", *size, 0, 0, 0);
663 if (err) {
664 usbd_clear_endpoint_stall(pipe);
665 }
666 USBHIST_LOG(usbdebug, "<- done xfer %#jx err %d", (uintptr_t)xfer,
667 err, 0, 0);
668
669 return err;
670 }
671
672 usbd_status
673 usbd_intr_transfer(struct usbd_xfer *xfer, struct usbd_pipe *pipe,
674 uint16_t flags, uint32_t timeout, void *buf, uint32_t *size)
675 {
676 usbd_status err;
677
678 USBHIST_FUNC();
679 USBHIST_CALLARGS(usbdebug, "start transfer %jd bytes", *size, 0, 0, 0);
680
681 usbd_setup_xfer(xfer, 0, buf, *size, flags, timeout, NULL);
682
683 err = usbd_sync_transfer_sig(xfer);
684
685 usbd_get_xfer_status(xfer, NULL, NULL, size, NULL);
686
687 DPRINTFN(1, "transferred %jd", *size, 0, 0, 0);
688 if (err) {
689 usbd_clear_endpoint_stall(pipe);
690 }
691 USBHIST_LOG(usbdebug, "<- done xfer %#jx err %jd", (uintptr_t)xfer,
692 err, 0, 0);
693
694 return err;
695 }
696
697 void
698 usb_detach_waitold(device_t dv)
699 {
700 USBHIST_FUNC();
701 USBHIST_CALLARGS(usbdebug, "waiting for dv %#jx",
702 (uintptr_t)dv, 0, 0, 0);
703
704 if (tsleep(dv, PZERO, "usbdet", hz * 60)) /* XXXSMP ok */
705 aprint_error_dev(dv, "usb_detach_waitold: didn't detach\n");
706 DPRINTFN(1, "done", 0, 0, 0, 0);
707 }
708
709 void
710 usb_detach_wakeupold(device_t dv)
711 {
712 USBHIST_FUNC();
713 USBHIST_CALLARGS(usbdebug, "for dv %#jx", (uintptr_t)dv, 0, 0, 0);
714
715 wakeup(dv); /* XXXSMP ok */
716 }
717
718 const usb_cdc_descriptor_t *
719 usb_find_desc(struct usbd_device *dev, int type, int subtype)
720 {
721 usbd_desc_iter_t iter;
722 const usb_cdc_descriptor_t *desc;
723
724 usb_desc_iter_init(dev, &iter);
725 for (;;) {
726 desc = (const usb_cdc_descriptor_t *)usb_desc_iter_next(&iter);
727 if (!desc || (desc->bDescriptorType == type &&
728 (subtype == USBD_CDCSUBTYPE_ANY ||
729 subtype == desc->bDescriptorSubtype)))
730 break;
731 }
732 return desc;
733 }
734
735 /* same as usb_find_desc(), but searches only in the specified interface. */
736 const usb_cdc_descriptor_t *
737 usb_find_desc_if(struct usbd_device *dev, int type, int subtype,
738 usb_interface_descriptor_t *id)
739 {
740 usbd_desc_iter_t iter;
741 const usb_cdc_descriptor_t *desc;
742
743 if (id == NULL)
744 return usb_find_desc(dev, type, subtype);
745
746 usb_desc_iter_init(dev, &iter);
747
748 iter.cur = (void *)id; /* start from the interface desc */
749 usb_desc_iter_next(&iter); /* and skip it */
750
751 while ((desc = (const usb_cdc_descriptor_t *)usb_desc_iter_next(&iter))
752 != NULL) {
753 if (desc->bDescriptorType == UDESC_INTERFACE) {
754 /* we ran into the next interface --- not found */
755 return NULL;
756 }
757 if (desc->bDescriptorType == type &&
758 (subtype == USBD_CDCSUBTYPE_ANY ||
759 subtype == desc->bDescriptorSubtype))
760 break;
761 }
762 return desc;
763 }
764