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