1 /* $NetBSD: usbdi_util.c,v 1.36 2001/11/13 06:24:57 lukem Exp $ */
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (lennart@augustsson.net) at
9 * Carlstedt Research & Technology.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/malloc.h>
47 #if defined(__NetBSD__) || defined(__OpenBSD__)
49 #include <sys/device.h>
50 #elif defined(__FreeBSD__)
54 #include <dev/usb/usb.h>
55 #include <dev/usb/usbhid.h>
57 #include <dev/usb/usbdi.h>
58 #include <dev/usb/usbdi_util.h>
61 #define DPRINTF(x) if (usbdebug) logprintf x
62 #define DPRINTFN(n,x) if (usbdebug>(n)) logprintf x
70 usbd_get_desc(usbd_device_handle dev, int type, int index, int len, void *desc)
72 usb_device_request_t req;
74 DPRINTFN(3,("usbd_get_desc: type=%d, index=%d, len=%d\n",
77 req.bmRequestType = UT_READ_DEVICE;
78 req.bRequest = UR_GET_DESCRIPTOR;
79 USETW2(req.wValue, type, index);
81 USETW(req.wLength, len);
82 return (usbd_do_request(dev, &req, desc));
86 usbd_get_config_desc(usbd_device_handle dev, int confidx,
87 usb_config_descriptor_t *d)
91 DPRINTFN(3,("usbd_get_config_desc: confidx=%d\n", confidx));
92 err = usbd_get_desc(dev, UDESC_CONFIG, confidx,
93 USB_CONFIG_DESCRIPTOR_SIZE, d);
96 if (d->bDescriptorType != UDESC_CONFIG) {
97 DPRINTFN(-1,("usbd_get_config_desc: confidx=%d, bad desc "
99 confidx, d->bLength, d->bDescriptorType));
102 return (USBD_NORMAL_COMPLETION);
106 usbd_get_config_desc_full(usbd_device_handle dev, int conf, void *d, int size)
108 DPRINTFN(3,("usbd_get_config_desc_full: conf=%d\n", conf));
109 return (usbd_get_desc(dev, UDESC_CONFIG, conf, size, d));
113 usbd_get_device_desc(usbd_device_handle dev, usb_device_descriptor_t *d)
115 DPRINTFN(3,("usbd_get_device_desc:\n"));
116 return (usbd_get_desc(dev, UDESC_DEVICE,
117 0, USB_DEVICE_DESCRIPTOR_SIZE, d));
121 usbd_get_device_status(usbd_device_handle dev, usb_status_t *st)
123 usb_device_request_t req;
125 req.bmRequestType = UT_READ_DEVICE;
126 req.bRequest = UR_GET_STATUS;
127 USETW(req.wValue, 0);
128 USETW(req.wIndex, 0);
129 USETW(req.wLength, sizeof(usb_status_t));
130 return (usbd_do_request(dev, &req, st));
134 usbd_get_hub_status(usbd_device_handle dev, usb_hub_status_t *st)
136 usb_device_request_t req;
138 req.bmRequestType = UT_READ_CLASS_DEVICE;
139 req.bRequest = UR_GET_STATUS;
140 USETW(req.wValue, 0);
141 USETW(req.wIndex, 0);
142 USETW(req.wLength, sizeof(usb_hub_status_t));
143 return (usbd_do_request(dev, &req, st));
147 usbd_set_address(usbd_device_handle dev, int addr)
149 usb_device_request_t req;
151 req.bmRequestType = UT_WRITE_DEVICE;
152 req.bRequest = UR_SET_ADDRESS;
153 USETW(req.wValue, addr);
154 USETW(req.wIndex, 0);
155 USETW(req.wLength, 0);
156 return usbd_do_request(dev, &req, 0);
160 usbd_get_port_status(usbd_device_handle dev, int port, usb_port_status_t *ps)
162 usb_device_request_t req;
164 req.bmRequestType = UT_READ_CLASS_OTHER;
165 req.bRequest = UR_GET_STATUS;
166 USETW(req.wValue, 0);
167 USETW(req.wIndex, port);
168 USETW(req.wLength, sizeof *ps);
169 return (usbd_do_request(dev, &req, ps));
173 usbd_clear_hub_feature(usbd_device_handle dev, int sel)
175 usb_device_request_t req;
177 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
178 req.bRequest = UR_CLEAR_FEATURE;
179 USETW(req.wValue, sel);
180 USETW(req.wIndex, 0);
181 USETW(req.wLength, 0);
182 return (usbd_do_request(dev, &req, 0));
186 usbd_set_hub_feature(usbd_device_handle dev, int sel)
188 usb_device_request_t req;
190 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
191 req.bRequest = UR_SET_FEATURE;
192 USETW(req.wValue, sel);
193 USETW(req.wIndex, 0);
194 USETW(req.wLength, 0);
195 return (usbd_do_request(dev, &req, 0));
199 usbd_clear_port_feature(usbd_device_handle dev, int port, int sel)
201 usb_device_request_t req;
203 req.bmRequestType = UT_WRITE_CLASS_OTHER;
204 req.bRequest = UR_CLEAR_FEATURE;
205 USETW(req.wValue, sel);
206 USETW(req.wIndex, port);
207 USETW(req.wLength, 0);
208 return (usbd_do_request(dev, &req, 0));
212 usbd_set_port_feature(usbd_device_handle dev, int port, int sel)
214 usb_device_request_t req;
216 req.bmRequestType = UT_WRITE_CLASS_OTHER;
217 req.bRequest = UR_SET_FEATURE;
218 USETW(req.wValue, sel);
219 USETW(req.wIndex, port);
220 USETW(req.wLength, 0);
221 return (usbd_do_request(dev, &req, 0));
226 usbd_set_protocol(usbd_interface_handle iface, int report)
228 usb_interface_descriptor_t *id = usbd_get_interface_descriptor(iface);
229 usbd_device_handle dev;
230 usb_device_request_t req;
232 DPRINTFN(4, ("usbd_set_protocol: iface=%p, report=%d, endpt=%d\n",
233 iface, report, id->bInterfaceNumber));
235 return (USBD_IOERROR);
236 usbd_interface2device_handle(iface, &dev);
237 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
238 req.bRequest = UR_SET_PROTOCOL;
239 USETW(req.wValue, report);
240 USETW(req.wIndex, id->bInterfaceNumber);
241 USETW(req.wLength, 0);
242 return (usbd_do_request(dev, &req, 0));
246 usbd_set_report(usbd_interface_handle iface, int type, int id, void *data,
249 usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
250 usbd_device_handle dev;
251 usb_device_request_t req;
253 DPRINTFN(4, ("usbd_set_report: len=%d\n", len));
255 return (USBD_IOERROR);
256 usbd_interface2device_handle(iface, &dev);
257 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
258 req.bRequest = UR_SET_REPORT;
259 USETW2(req.wValue, type, id);
260 USETW(req.wIndex, ifd->bInterfaceNumber);
261 USETW(req.wLength, len);
262 return (usbd_do_request(dev, &req, data));
266 usbd_set_report_async(usbd_interface_handle iface, int type, int id, void *data,
269 usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
270 usbd_device_handle dev;
271 usb_device_request_t req;
273 DPRINTFN(4, ("usbd_set_report_async: len=%d\n", len));
275 return (USBD_IOERROR);
276 usbd_interface2device_handle(iface, &dev);
277 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
278 req.bRequest = UR_SET_REPORT;
279 USETW2(req.wValue, type, id);
280 USETW(req.wIndex, ifd->bInterfaceNumber);
281 USETW(req.wLength, len);
282 return (usbd_do_request_async(dev, &req, data));
286 usbd_get_report(usbd_interface_handle iface, int type, int id, void *data,
289 usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
290 usbd_device_handle dev;
291 usb_device_request_t req;
293 DPRINTFN(4, ("usbd_set_report: len=%d\n", len));
295 return (USBD_IOERROR);
296 usbd_interface2device_handle(iface, &dev);
297 req.bmRequestType = UT_READ_CLASS_INTERFACE;
298 req.bRequest = UR_GET_REPORT;
299 USETW2(req.wValue, type, id);
300 USETW(req.wIndex, ifd->bInterfaceNumber);
301 USETW(req.wLength, len);
302 return (usbd_do_request(dev, &req, data));
306 usbd_set_idle(usbd_interface_handle iface, int duration, int id)
308 usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
309 usbd_device_handle dev;
310 usb_device_request_t req;
312 DPRINTFN(4, ("usbd_set_idle: %d %d\n", duration, id));
314 return (USBD_IOERROR);
315 usbd_interface2device_handle(iface, &dev);
316 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
317 req.bRequest = UR_SET_IDLE;
318 USETW2(req.wValue, duration, id);
319 USETW(req.wIndex, ifd->bInterfaceNumber);
320 USETW(req.wLength, 0);
321 return (usbd_do_request(dev, &req, 0));
325 usbd_get_report_descriptor(usbd_device_handle dev, int ifcno,
328 usb_device_request_t req;
330 req.bmRequestType = UT_READ_INTERFACE;
331 req.bRequest = UR_GET_DESCRIPTOR;
332 USETW2(req.wValue, UDESC_REPORT, 0); /* report id should be 0 */
333 USETW(req.wIndex, ifcno);
334 USETW(req.wLength, size);
335 return (usbd_do_request(dev, &req, d));
338 usb_hid_descriptor_t *
339 usbd_get_hid_descriptor(usbd_interface_handle ifc)
341 usb_interface_descriptor_t *idesc = usbd_get_interface_descriptor(ifc);
342 usbd_device_handle dev;
343 usb_config_descriptor_t *cdesc;
344 usb_hid_descriptor_t *hd;
349 usbd_interface2device_handle(ifc, &dev);
350 cdesc = usbd_get_config_descriptor(dev);
352 p = (char *)idesc + idesc->bLength;
353 end = (char *)cdesc + UGETW(cdesc->wTotalLength);
355 for (; p < end; p += hd->bLength) {
356 hd = (usb_hid_descriptor_t *)p;
357 if (p + hd->bLength <= end && hd->bDescriptorType == UDESC_HID)
359 if (hd->bDescriptorType == UDESC_INTERFACE)
366 usbd_read_report_desc(usbd_interface_handle ifc, void **descp, int *sizep,
369 usb_interface_descriptor_t *id;
370 usb_hid_descriptor_t *hid;
371 usbd_device_handle dev;
374 usbd_interface2device_handle(ifc, &dev);
375 id = usbd_get_interface_descriptor(ifc);
378 hid = usbd_get_hid_descriptor(ifc);
380 return (USBD_IOERROR);
381 *sizep = UGETW(hid->descrs[0].wDescriptorLength);
382 *descp = malloc(*sizep, mem, M_NOWAIT);
385 err = usbd_get_report_descriptor(dev, id->bInterfaceNumber,
392 return (USBD_NORMAL_COMPLETION);
396 usbd_get_config(usbd_device_handle dev, u_int8_t *conf)
398 usb_device_request_t req;
400 req.bmRequestType = UT_READ_DEVICE;
401 req.bRequest = UR_GET_CONFIG;
402 USETW(req.wValue, 0);
403 USETW(req.wIndex, 0);
404 USETW(req.wLength, 1);
405 return (usbd_do_request(dev, &req, conf));
408 Static void usbd_bulk_transfer_cb(usbd_xfer_handle xfer,
409 usbd_private_handle priv, usbd_status status);
411 usbd_bulk_transfer_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
418 usbd_bulk_transfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
419 u_int16_t flags, u_int32_t timeout, void *buf,
420 u_int32_t *size, char *lbl)
425 usbd_setup_xfer(xfer, pipe, 0, buf, *size,
426 flags, timeout, usbd_bulk_transfer_cb);
427 DPRINTFN(1, ("usbd_bulk_transfer: start transfer %d bytes\n", *size));
428 s = splusb(); /* don't want callback until tsleep() */
429 err = usbd_transfer(xfer);
430 if (err != USBD_IN_PROGRESS) {
434 error = tsleep(xfer, PZERO | PCATCH, lbl, 0);
437 DPRINTF(("usbd_bulk_transfer: tsleep=%d\n", error));
438 usbd_abort_pipe(pipe);
439 return (USBD_INTERRUPTED);
441 usbd_get_xfer_status(xfer, NULL, NULL, size, &err);
442 DPRINTFN(1,("usbd_bulk_transfer: transferred %d\n", *size));
444 DPRINTF(("usbd_bulk_transfer: error=%d\n", err));
445 usbd_clear_endpoint_stall(pipe);
450 Static void usbd_intr_transfer_cb(usbd_xfer_handle xfer,
451 usbd_private_handle priv, usbd_status status);
453 usbd_intr_transfer_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
460 usbd_intr_transfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
461 u_int16_t flags, u_int32_t timeout, void *buf,
462 u_int32_t *size, char *lbl)
467 usbd_setup_xfer(xfer, pipe, 0, buf, *size,
468 flags, timeout, usbd_intr_transfer_cb);
469 DPRINTFN(1, ("usbd_intr_transfer: start transfer %d bytes\n", *size));
470 s = splusb(); /* don't want callback until tsleep() */
471 err = usbd_transfer(xfer);
472 if (err != USBD_IN_PROGRESS) {
476 error = tsleep(xfer, PZERO | PCATCH, lbl, 0);
479 DPRINTF(("usbd_intr_transfer: tsleep=%d\n", error));
480 usbd_abort_pipe(pipe);
481 return (USBD_INTERRUPTED);
483 usbd_get_xfer_status(xfer, NULL, NULL, size, &err);
484 DPRINTFN(1,("usbd_intr_transfer: transferred %d\n", *size));
486 DPRINTF(("usbd_intr_transfer: error=%d\n", err));
487 usbd_clear_endpoint_stall(pipe);
493 usb_detach_wait(device_ptr_t dv)
495 DPRINTF(("usb_detach_wait: waiting for %s\n", USBDEVPTRNAME(dv)));
496 if (tsleep(dv, PZERO, "usbdet", hz * 60))
497 printf("usb_detach_wait: %s didn't detach\n",
499 DPRINTF(("usb_detach_wait: %s done\n", USBDEVPTRNAME(dv)));
503 usb_detach_wakeup(device_ptr_t dv)
505 DPRINTF(("usb_detach_wakeup: for %s\n", USBDEVPTRNAME(dv)));