Nuke device_ptr_t, USBBASEDEVICE, USBDEVNAME(), USBDEVUNIT(), USBGETSOFTC(),
[dragonfly/vkernel-mp.git] / sys / bus / usb / usbdi_util.c
blob5c5caae0cf31a0a372ffd669e528c637861b108d
1 /* $NetBSD: usbdi_util.c,v 1.42 2004/12/03 08:53:40 augustss Exp $ */
2 /* $FreeBSD: src/sys/dev/usb/usbdi_util.c,v 1.34 2005/03/01 08:01:22 sobomax Exp $ */
3 /* $DragonFly: src/sys/bus/usb/usbdi_util.c,v 1.13 2007/06/28 06:32:31 hasso Exp $ */
5 /*
6 * Copyright (c) 1998 The NetBSD Foundation, Inc.
7 * All rights reserved.
9 * This code is derived from software contributed to The NetBSD Foundation
10 * by Lennart Augustsson (lennart@augustsson.net) at
11 * Carlstedt Research & Technology.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the NetBSD
24 * Foundation, Inc. and its contributors.
25 * 4. Neither the name of The NetBSD Foundation nor the names of its
26 * contributors may be used to endorse or promote products derived
27 * from this software without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
30 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
31 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
33 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 * POSSIBILITY OF SUCH DAMAGE.
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/module.h>
46 #include <sys/malloc.h>
47 #include <sys/bus.h>
48 #include <sys/thread2.h>
50 #include <bus/usb/usb.h>
51 #include <bus/usb/usbhid.h>
53 #include <bus/usb/usbdi.h>
54 #include <bus/usb/usbdi_util.h>
56 #ifdef USB_DEBUG
57 #define DPRINTF(x) if (usbdebug) logprintf x
58 #define DPRINTFN(n,x) if (usbdebug>(n)) logprintf x
59 extern int usbdebug;
60 #else
61 #define DPRINTF(x)
62 #define DPRINTFN(n,x)
63 #endif
65 usbd_status
66 usbd_get_desc(usbd_device_handle dev, int type, int index, int len, void *desc)
68 usb_device_request_t req;
70 DPRINTFN(3,("usbd_get_desc: type=%d, index=%d, len=%d\n",
71 type, index, len));
73 req.bmRequestType = UT_READ_DEVICE;
74 req.bRequest = UR_GET_DESCRIPTOR;
75 USETW2(req.wValue, type, index);
76 USETW(req.wIndex, 0);
77 USETW(req.wLength, len);
78 return (usbd_do_request(dev, &req, desc));
81 usbd_status
82 usbd_get_config_desc(usbd_device_handle dev, int confidx,
83 usb_config_descriptor_t *d)
85 usbd_status err;
87 DPRINTFN(3,("usbd_get_config_desc: confidx=%d\n", confidx));
88 err = usbd_get_desc(dev, UDESC_CONFIG, confidx,
89 USB_CONFIG_DESCRIPTOR_SIZE, d);
90 if (err)
91 return (err);
92 if (d->bDescriptorType != UDESC_CONFIG) {
93 DPRINTFN(-1,("usbd_get_config_desc: confidx=%d, bad desc "
94 "len=%d type=%d\n",
95 confidx, d->bLength, d->bDescriptorType));
96 return (USBD_INVAL);
98 return (USBD_NORMAL_COMPLETION);
101 usbd_status
102 usbd_get_config_desc_full(usbd_device_handle dev, int conf, void *d, int size)
104 DPRINTFN(3,("usbd_get_config_desc_full: conf=%d\n", conf));
105 return (usbd_get_desc(dev, UDESC_CONFIG, conf, size, d));
108 usbd_status
109 usbd_get_device_desc(usbd_device_handle dev, usb_device_descriptor_t *d)
111 DPRINTFN(3,("usbd_get_device_desc:\n"));
112 return (usbd_get_desc(dev, UDESC_DEVICE,
113 0, USB_DEVICE_DESCRIPTOR_SIZE, d));
116 usbd_status
117 usbd_get_device_status(usbd_device_handle dev, usb_status_t *st)
119 usb_device_request_t req;
121 req.bmRequestType = UT_READ_DEVICE;
122 req.bRequest = UR_GET_STATUS;
123 USETW(req.wValue, 0);
124 USETW(req.wIndex, 0);
125 USETW(req.wLength, sizeof(usb_status_t));
126 return (usbd_do_request(dev, &req, st));
129 usbd_status
130 usbd_get_hub_status(usbd_device_handle dev, usb_hub_status_t *st)
132 usb_device_request_t req;
134 req.bmRequestType = UT_READ_CLASS_DEVICE;
135 req.bRequest = UR_GET_STATUS;
136 USETW(req.wValue, 0);
137 USETW(req.wIndex, 0);
138 USETW(req.wLength, sizeof(usb_hub_status_t));
139 return (usbd_do_request(dev, &req, st));
142 usbd_status
143 usbd_set_address(usbd_device_handle dev, int addr)
145 usb_device_request_t req;
147 req.bmRequestType = UT_WRITE_DEVICE;
148 req.bRequest = UR_SET_ADDRESS;
149 USETW(req.wValue, addr);
150 USETW(req.wIndex, 0);
151 USETW(req.wLength, 0);
152 return usbd_do_request(dev, &req, 0);
155 usbd_status
156 usbd_get_port_status(usbd_device_handle dev, int port, usb_port_status_t *ps)
158 usb_device_request_t req;
160 req.bmRequestType = UT_READ_CLASS_OTHER;
161 req.bRequest = UR_GET_STATUS;
162 USETW(req.wValue, 0);
163 USETW(req.wIndex, port);
164 USETW(req.wLength, sizeof *ps);
165 return (usbd_do_request(dev, &req, ps));
168 usbd_status
169 usbd_clear_hub_feature(usbd_device_handle dev, int sel)
171 usb_device_request_t req;
173 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
174 req.bRequest = UR_CLEAR_FEATURE;
175 USETW(req.wValue, sel);
176 USETW(req.wIndex, 0);
177 USETW(req.wLength, 0);
178 return (usbd_do_request(dev, &req, 0));
181 usbd_status
182 usbd_set_hub_feature(usbd_device_handle dev, int sel)
184 usb_device_request_t req;
186 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
187 req.bRequest = UR_SET_FEATURE;
188 USETW(req.wValue, sel);
189 USETW(req.wIndex, 0);
190 USETW(req.wLength, 0);
191 return (usbd_do_request(dev, &req, 0));
194 usbd_status
195 usbd_clear_port_feature(usbd_device_handle dev, int port, int sel)
197 usb_device_request_t req;
199 req.bmRequestType = UT_WRITE_CLASS_OTHER;
200 req.bRequest = UR_CLEAR_FEATURE;
201 USETW(req.wValue, sel);
202 USETW(req.wIndex, port);
203 USETW(req.wLength, 0);
204 return (usbd_do_request(dev, &req, 0));
207 usbd_status
208 usbd_set_port_feature(usbd_device_handle dev, int port, int sel)
210 usb_device_request_t req;
212 req.bmRequestType = UT_WRITE_CLASS_OTHER;
213 req.bRequest = UR_SET_FEATURE;
214 USETW(req.wValue, sel);
215 USETW(req.wIndex, port);
216 USETW(req.wLength, 0);
217 return (usbd_do_request(dev, &req, 0));
220 usbd_status
221 usbd_get_protocol(usbd_interface_handle iface, u_int8_t *report)
223 usb_interface_descriptor_t *id = usbd_get_interface_descriptor(iface);
224 usbd_device_handle dev;
225 usb_device_request_t req;
227 DPRINTFN(4, ("usbd_get_protocol: iface=%p, endpt=%d\n",
228 iface, id->bInterfaceNumber));
229 if (id == NULL)
230 return (USBD_IOERROR);
231 usbd_interface2device_handle(iface, &dev);
232 req.bmRequestType = UT_READ_CLASS_INTERFACE;
233 req.bRequest = UR_GET_PROTOCOL;
234 USETW(req.wValue, 0);
235 USETW(req.wIndex, id->bInterfaceNumber);
236 USETW(req.wLength, 1);
237 return (usbd_do_request(dev, &req, report));
240 usbd_status
241 usbd_set_protocol(usbd_interface_handle iface, int report)
243 usb_interface_descriptor_t *id = usbd_get_interface_descriptor(iface);
244 usbd_device_handle dev;
245 usb_device_request_t req;
247 DPRINTFN(4, ("usbd_set_protocol: iface=%p, report=%d, endpt=%d\n",
248 iface, report, id->bInterfaceNumber));
249 if (id == NULL)
250 return (USBD_IOERROR);
251 usbd_interface2device_handle(iface, &dev);
252 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
253 req.bRequest = UR_SET_PROTOCOL;
254 USETW(req.wValue, report);
255 USETW(req.wIndex, id->bInterfaceNumber);
256 USETW(req.wLength, 0);
257 return (usbd_do_request(dev, &req, 0));
260 usbd_status
261 usbd_set_report(usbd_interface_handle iface, int type, int id, void *data,
262 int len)
264 usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
265 usbd_device_handle dev;
266 usb_device_request_t req;
268 DPRINTFN(4, ("usbd_set_report: len=%d\n", len));
269 if (ifd == NULL)
270 return (USBD_IOERROR);
271 usbd_interface2device_handle(iface, &dev);
272 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
273 req.bRequest = UR_SET_REPORT;
274 USETW2(req.wValue, type, id);
275 USETW(req.wIndex, ifd->bInterfaceNumber);
276 USETW(req.wLength, len);
277 return (usbd_do_request(dev, &req, data));
280 usbd_status
281 usbd_set_report_async(usbd_interface_handle iface, int type, int id, void *data,
282 int len)
284 usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
285 usbd_device_handle dev;
286 usb_device_request_t req;
288 DPRINTFN(4, ("usbd_set_report_async: len=%d\n", len));
289 if (ifd == NULL)
290 return (USBD_IOERROR);
291 usbd_interface2device_handle(iface, &dev);
292 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
293 req.bRequest = UR_SET_REPORT;
294 USETW2(req.wValue, type, id);
295 USETW(req.wIndex, ifd->bInterfaceNumber);
296 USETW(req.wLength, len);
297 return (usbd_do_request_async(dev, &req, data));
300 usbd_status
301 usbd_get_report(usbd_interface_handle iface, int type, int id, void *data,
302 int len)
304 usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
305 usbd_device_handle dev;
306 usb_device_request_t req;
308 DPRINTFN(4, ("usbd_get_report: len=%d\n", len));
309 if (ifd == NULL)
310 return (USBD_IOERROR);
311 usbd_interface2device_handle(iface, &dev);
312 req.bmRequestType = UT_READ_CLASS_INTERFACE;
313 req.bRequest = UR_GET_REPORT;
314 USETW2(req.wValue, type, id);
315 USETW(req.wIndex, ifd->bInterfaceNumber);
316 USETW(req.wLength, len);
317 return (usbd_do_request(dev, &req, data));
320 usbd_status
321 usbd_set_idle(usbd_interface_handle iface, int duration, int id)
323 usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
324 usbd_device_handle dev;
325 usb_device_request_t req;
327 DPRINTFN(4, ("usbd_set_idle: %d %d\n", duration, id));
328 if (ifd == NULL)
329 return (USBD_IOERROR);
330 usbd_interface2device_handle(iface, &dev);
331 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
332 req.bRequest = UR_SET_IDLE;
333 USETW2(req.wValue, duration, id);
334 USETW(req.wIndex, ifd->bInterfaceNumber);
335 USETW(req.wLength, 0);
336 return (usbd_do_request(dev, &req, 0));
339 usbd_status
340 usbd_get_report_descriptor(usbd_device_handle dev, int ifcno,
341 int size, void *d)
343 usb_device_request_t req;
345 req.bmRequestType = UT_READ_INTERFACE;
346 req.bRequest = UR_GET_DESCRIPTOR;
347 USETW2(req.wValue, UDESC_REPORT, 0); /* report id should be 0 */
348 USETW(req.wIndex, ifcno);
349 USETW(req.wLength, size);
350 return (usbd_do_request(dev, &req, d));
353 usb_hid_descriptor_t *
354 usbd_get_hid_descriptor(usbd_interface_handle ifc)
356 usb_interface_descriptor_t *idesc = usbd_get_interface_descriptor(ifc);
357 usbd_device_handle dev;
358 usb_config_descriptor_t *cdesc;
359 usb_hid_descriptor_t *hd;
360 char *p, *end;
362 if (idesc == NULL)
363 return (NULL);
364 usbd_interface2device_handle(ifc, &dev);
365 cdesc = usbd_get_config_descriptor(dev);
367 p = (char *)idesc + idesc->bLength;
368 end = (char *)cdesc + UGETW(cdesc->wTotalLength);
370 for (; p < end; p += hd->bLength) {
371 hd = (usb_hid_descriptor_t *)p;
372 if (p + hd->bLength <= end && hd->bDescriptorType == UDESC_HID)
373 return (hd);
374 if (hd->bDescriptorType == UDESC_INTERFACE)
375 break;
377 return (NULL);
380 usbd_status
381 usbd_read_report_desc(usbd_interface_handle ifc, void **descp, int *sizep,
382 usb_malloc_type mem)
384 usb_interface_descriptor_t *id;
385 usb_hid_descriptor_t *hid;
386 usbd_device_handle dev;
387 usbd_status err;
389 usbd_interface2device_handle(ifc, &dev);
390 id = usbd_get_interface_descriptor(ifc);
391 if (id == NULL)
392 return (USBD_INVAL);
393 hid = usbd_get_hid_descriptor(ifc);
394 if (hid == NULL)
395 return (USBD_IOERROR);
396 *sizep = UGETW(hid->descrs[0].wDescriptorLength);
397 *descp = kmalloc(*sizep, mem, M_INTWAIT);
398 err = usbd_get_report_descriptor(dev, id->bInterfaceNumber,
399 *sizep, *descp);
400 if (err) {
401 kfree(*descp, mem);
402 *descp = NULL;
403 return (err);
405 return (USBD_NORMAL_COMPLETION);
408 usbd_status
409 usbd_get_config(usbd_device_handle dev, u_int8_t *conf)
411 usb_device_request_t req;
413 req.bmRequestType = UT_READ_DEVICE;
414 req.bRequest = UR_GET_CONFIG;
415 USETW(req.wValue, 0);
416 USETW(req.wIndex, 0);
417 USETW(req.wLength, 1);
418 return (usbd_do_request(dev, &req, conf));
421 static void usbd_bulk_transfer_cb(usbd_xfer_handle xfer,
422 usbd_private_handle priv, usbd_status status);
423 static void
424 usbd_bulk_transfer_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
425 usbd_status status)
427 wakeup(xfer);
430 usbd_status
431 usbd_bulk_transfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
432 u_int16_t flags, u_int32_t timeout, void *buf,
433 u_int32_t *size, char *lbl)
435 usbd_status err;
436 int error;
438 usbd_setup_xfer(xfer, pipe, 0, buf, *size,
439 flags, timeout, usbd_bulk_transfer_cb);
440 DPRINTFN(1, ("usbd_bulk_transfer: start transfer %d bytes\n", *size));
441 crit_enter();
442 err = usbd_transfer(xfer);
443 if (err != USBD_IN_PROGRESS) {
444 crit_exit();
445 return (err);
447 error = tsleep((caddr_t)xfer, PCATCH, lbl, 0);
448 crit_exit();
449 if (error) {
450 DPRINTF(("usbd_bulk_transfer: tsleep=%d\n", error));
451 usbd_abort_pipe(pipe);
452 return (USBD_INTERRUPTED);
454 usbd_get_xfer_status(xfer, NULL, NULL, size, &err);
455 DPRINTFN(1,("usbd_bulk_transfer: transferred %d\n", *size));
456 if (err) {
457 DPRINTF(("usbd_bulk_transfer: error=%d\n", err));
458 usbd_clear_endpoint_stall(pipe);
460 return (err);
463 static void usbd_intr_transfer_cb(usbd_xfer_handle xfer,
464 usbd_private_handle priv, usbd_status status);
465 static void
466 usbd_intr_transfer_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
467 usbd_status status)
469 wakeup(xfer);
472 usbd_status
473 usbd_intr_transfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
474 u_int16_t flags, u_int32_t timeout, void *buf,
475 u_int32_t *size, char *lbl)
477 usbd_status err;
478 int error;
480 usbd_setup_xfer(xfer, pipe, 0, buf, *size,
481 flags, timeout, usbd_intr_transfer_cb);
482 DPRINTFN(1, ("usbd_intr_transfer: start transfer %d bytes\n", *size));
483 crit_enter(); /* don't want callback until tsleep() */
484 err = usbd_transfer(xfer);
485 if (err != USBD_IN_PROGRESS) {
486 crit_exit();
487 return (err);
489 error = tsleep(xfer, PCATCH, lbl, 0);
490 crit_exit();
491 if (error) {
492 DPRINTF(("usbd_intr_transfer: tsleep=%d\n", error));
493 usbd_abort_pipe(pipe);
494 return (USBD_INTERRUPTED);
496 usbd_get_xfer_status(xfer, NULL, NULL, size, &err);
497 DPRINTFN(1,("usbd_intr_transfer: transferred %d\n", *size));
498 if (err) {
499 DPRINTF(("usbd_intr_transfer: error=%d\n", err));
500 usbd_clear_endpoint_stall(pipe);
502 return (err);
505 void
506 usb_detach_wait(device_t dv)
508 DPRINTF(("usb_detach_wait: waiting for %s\n", device_get_nameunit(dv)));
509 if (tsleep(dv, 0, "usbdet", hz * 60))
510 kprintf("usb_detach_wait: %s didn't detach\n",
511 device_get_nameunit(dv));
512 DPRINTF(("usb_detach_wait: %s done\n", device_get_nameunit(dv)));
515 void
516 usb_detach_wakeup(device_t dv)
518 DPRINTF(("usb_detach_wakeup: for %s\n", device_get_nameunit(dv)));
519 wakeup(dv);
522 const usb_descriptor_t *
523 usb_find_desc(usbd_device_handle dev, int type, int subtype)
525 usbd_desc_iter_t iter;
526 const usb_descriptor_t *desc;
528 usb_desc_iter_init(dev, &iter);
529 for (;;) {
530 desc = usb_desc_iter_next(&iter);
531 if (!desc || (desc->bDescriptorType == type &&
532 (subtype == USBD_SUBTYPE_ANY ||
533 subtype == desc->bDescriptorSubtype)))
534 break;
536 return desc;