1 /* $NetBSD: usbdi_util.c,v 1.24 1999/11/17 23:00:50 augustss Exp $ */
2 /* $FreeBSD: src/sys/dev/usb/usbdi_util.c,v 1.15.2.5 2002/11/06 14:03:37 joe Exp $ */
5 * Copyright (c) 1998 The NetBSD Foundation, Inc.
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Lennart Augustsson (lennart@augustsson.net) at
10 * Carlstedt Research & Technology.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the NetBSD
23 * Foundation, Inc. and its contributors.
24 * 4. Neither the name of The NetBSD Foundation nor the names of its
25 * contributors may be used to endorse or promote products derived
26 * from this software without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/malloc.h>
45 #if defined(__NetBSD__) || defined(__OpenBSD__)
47 #include <sys/device.h>
48 #elif defined(__FreeBSD__)
52 #include <dev/usb/usb.h>
53 #include <dev/usb/usbhid.h>
55 #include <dev/usb/usbdi.h>
56 #include <dev/usb/usbdi_util.h>
59 #define DPRINTF(x) if (usbdebug) logprintf x
60 #define DPRINTFN(n,x) if (usbdebug>(n)) logprintf x
68 usbd_get_desc(usbd_device_handle dev, int type, int index, int len, void *desc)
70 usb_device_request_t req;
72 req.bmRequestType = UT_READ_DEVICE;
73 req.bRequest = UR_GET_DESCRIPTOR;
74 USETW2(req.wValue, type, index);
76 USETW(req.wLength, len);
77 return (usbd_do_request(dev, &req, desc));
81 usbd_get_config_desc(usbd_device_handle dev, int conf,
82 usb_config_descriptor_t *d)
86 DPRINTFN(3,("usbd_get_config_desc: conf=%d\n", conf));
87 err = usbd_get_desc(dev, UDESC_CONFIG, conf,
88 USB_CONFIG_DESCRIPTOR_SIZE, d);
91 if (d->bDescriptorType != UDESC_CONFIG) {
92 DPRINTFN(-1,("usbd_get_config_desc: conf %d, bad desc %d\n",
93 conf, d->bDescriptorType));
96 return (USBD_NORMAL_COMPLETION);
100 usbd_get_config_desc_full(usbd_device_handle dev, int conf, void *d, int size)
102 DPRINTFN(3,("usbd_get_config_desc_full: conf=%d\n", conf));
103 return (usbd_get_desc(dev, UDESC_CONFIG, conf, size, d));
107 usbd_get_device_desc(usbd_device_handle dev, usb_device_descriptor_t *d)
109 DPRINTFN(3,("usbd_get_device_desc:\n"));
110 return (usbd_get_desc(dev, UDESC_DEVICE,
111 0, USB_DEVICE_DESCRIPTOR_SIZE, d));
115 usbd_get_device_status(usbd_device_handle dev, usb_status_t *st)
117 usb_device_request_t req;
119 req.bmRequestType = UT_READ_DEVICE;
120 req.bRequest = UR_GET_STATUS;
121 USETW(req.wValue, 0);
122 USETW(req.wIndex, 0);
123 USETW(req.wLength, sizeof(usb_status_t));
124 return (usbd_do_request(dev, &req, st));
128 usbd_get_hub_status(usbd_device_handle dev, usb_hub_status_t *st)
130 usb_device_request_t req;
132 req.bmRequestType = UT_READ_CLASS_DEVICE;
133 req.bRequest = UR_GET_STATUS;
134 USETW(req.wValue, 0);
135 USETW(req.wIndex, 0);
136 USETW(req.wLength, sizeof(usb_hub_status_t));
137 return (usbd_do_request(dev, &req, st));
141 usbd_set_address(usbd_device_handle dev, int addr)
143 usb_device_request_t req;
145 req.bmRequestType = UT_WRITE_DEVICE;
146 req.bRequest = UR_SET_ADDRESS;
147 USETW(req.wValue, addr);
148 USETW(req.wIndex, 0);
149 USETW(req.wLength, 0);
150 return usbd_do_request(dev, &req, 0);
154 usbd_get_port_status(usbd_device_handle dev, int port, usb_port_status_t *ps)
156 usb_device_request_t req;
158 req.bmRequestType = UT_READ_CLASS_OTHER;
159 req.bRequest = UR_GET_STATUS;
160 USETW(req.wValue, 0);
161 USETW(req.wIndex, port);
162 USETW(req.wLength, sizeof *ps);
163 return (usbd_do_request(dev, &req, ps));
167 usbd_clear_hub_feature(usbd_device_handle dev, int sel)
169 usb_device_request_t req;
171 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
172 req.bRequest = UR_CLEAR_FEATURE;
173 USETW(req.wValue, sel);
174 USETW(req.wIndex, 0);
175 USETW(req.wLength, 0);
176 return (usbd_do_request(dev, &req, 0));
180 usbd_set_hub_feature(usbd_device_handle dev, int sel)
182 usb_device_request_t req;
184 req.bmRequestType = UT_WRITE_CLASS_DEVICE;
185 req.bRequest = UR_SET_FEATURE;
186 USETW(req.wValue, sel);
187 USETW(req.wIndex, 0);
188 USETW(req.wLength, 0);
189 return (usbd_do_request(dev, &req, 0));
193 usbd_clear_port_feature(usbd_device_handle dev, int port, int sel)
195 usb_device_request_t req;
197 req.bmRequestType = UT_WRITE_CLASS_OTHER;
198 req.bRequest = UR_CLEAR_FEATURE;
199 USETW(req.wValue, sel);
200 USETW(req.wIndex, port);
201 USETW(req.wLength, 0);
202 return (usbd_do_request(dev, &req, 0));
206 usbd_set_port_feature(usbd_device_handle dev, int port, int sel)
208 usb_device_request_t req;
210 req.bmRequestType = UT_WRITE_CLASS_OTHER;
211 req.bRequest = UR_SET_FEATURE;
212 USETW(req.wValue, sel);
213 USETW(req.wIndex, port);
214 USETW(req.wLength, 0);
215 return (usbd_do_request(dev, &req, 0));
220 usbd_set_protocol(usbd_interface_handle iface, int report)
222 usb_interface_descriptor_t *id = usbd_get_interface_descriptor(iface);
223 usbd_device_handle dev;
224 usb_device_request_t req;
227 DPRINTFN(4, ("usbd_set_protocol: iface=%p, report=%d, endpt=%d\n",
228 iface, report, id->bInterfaceNumber));
230 return (USBD_IOERROR);
231 err = usbd_interface2device_handle(iface, &dev);
234 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
235 req.bRequest = UR_SET_PROTOCOL;
236 USETW(req.wValue, report);
237 USETW(req.wIndex, id->bInterfaceNumber);
238 USETW(req.wLength, 0);
239 return (usbd_do_request(dev, &req, 0));
243 usbd_set_report(usbd_interface_handle iface, int type, int id, void *data,
246 usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
247 usbd_device_handle dev;
248 usb_device_request_t req;
251 DPRINTFN(4, ("usbd_set_report: len=%d\n", len));
253 return (USBD_IOERROR);
254 err = 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;
274 DPRINTFN(4, ("usbd_set_report_async: len=%d\n", len));
276 return (USBD_IOERROR);
277 err = usbd_interface2device_handle(iface, &dev);
280 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
281 req.bRequest = UR_SET_REPORT;
282 USETW2(req.wValue, type, id);
283 USETW(req.wIndex, ifd->bInterfaceNumber);
284 USETW(req.wLength, len);
285 return (usbd_do_request_async(dev, &req, data));
289 usbd_get_report(usbd_interface_handle iface, int type, int id, void *data,
292 usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
293 usbd_device_handle dev;
294 usb_device_request_t req;
297 DPRINTFN(4, ("usbd_set_report: len=%d\n", len));
299 return (USBD_IOERROR);
300 err = usbd_interface2device_handle(iface, &dev);
303 req.bmRequestType = UT_READ_CLASS_INTERFACE;
304 req.bRequest = UR_GET_REPORT;
305 USETW2(req.wValue, type, id);
306 USETW(req.wIndex, ifd->bInterfaceNumber);
307 USETW(req.wLength, len);
308 return (usbd_do_request(dev, &req, data));
312 usbd_set_idle(usbd_interface_handle iface, int duration, int id)
314 usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
315 usbd_device_handle dev;
316 usb_device_request_t req;
319 DPRINTFN(4, ("usbd_set_idle: %d %d\n", duration, id));
321 return (USBD_IOERROR);
322 err = usbd_interface2device_handle(iface, &dev);
325 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
326 req.bRequest = UR_SET_IDLE;
327 USETW2(req.wValue, duration, id);
328 USETW(req.wIndex, ifd->bInterfaceNumber);
329 USETW(req.wLength, 0);
330 return (usbd_do_request(dev, &req, 0));
334 usbd_get_report_descriptor(usbd_device_handle dev, int ifcno, int repid,
337 usb_device_request_t req;
339 req.bmRequestType = UT_READ_INTERFACE;
340 req.bRequest = UR_GET_DESCRIPTOR;
341 USETW2(req.wValue, UDESC_REPORT, repid);
342 USETW(req.wIndex, ifcno);
343 USETW(req.wLength, size);
344 return (usbd_do_request(dev, &req, d));
347 usb_hid_descriptor_t *
348 usbd_get_hid_descriptor(usbd_interface_handle ifc)
350 usb_interface_descriptor_t *idesc = usbd_get_interface_descriptor(ifc);
351 usbd_device_handle dev;
352 usb_config_descriptor_t *cdesc;
353 usb_hid_descriptor_t *hd;
359 err = usbd_interface2device_handle(ifc, &dev);
362 cdesc = usbd_get_config_descriptor(dev);
364 p = (char *)idesc + idesc->bLength;
365 end = (char *)cdesc + UGETW(cdesc->wTotalLength);
367 for (; p < end; p += hd->bLength) {
368 hd = (usb_hid_descriptor_t *)p;
369 if (p + hd->bLength <= end && hd->bDescriptorType == UDESC_HID)
371 if (hd->bDescriptorType == UDESC_INTERFACE)
378 usbd_alloc_report_desc(usbd_interface_handle ifc, void **descp, int *sizep,
379 struct malloc_type *mem)
381 usb_interface_descriptor_t *id;
382 usb_hid_descriptor_t *hid;
383 usbd_device_handle dev;
386 err = usbd_interface2device_handle(ifc, &dev);
389 id = usbd_get_interface_descriptor(ifc);
392 hid = usbd_get_hid_descriptor(ifc);
394 return (USBD_IOERROR);
395 *sizep = UGETW(hid->descrs[0].wDescriptorLength);
396 *descp = malloc(*sizep, mem, M_NOWAIT);
399 /* XXX should not use 0 Report ID */
400 err = usbd_get_report_descriptor(dev, id->bInterfaceNumber, 0,
407 return (USBD_NORMAL_COMPLETION);
411 usbd_get_config(usbd_device_handle dev, u_int8_t *conf)
413 usb_device_request_t req;
415 req.bmRequestType = UT_READ_DEVICE;
416 req.bRequest = UR_GET_CONFIG;
417 USETW(req.wValue, 0);
418 USETW(req.wIndex, 0);
419 USETW(req.wLength, 1);
420 return (usbd_do_request(dev, &req, conf));
424 usbd_bulk_transfer_cb(usbd_xfer_handle xfer,
425 usbd_private_handle priv, usbd_status status);
427 usbd_bulk_transfer_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
434 usbd_bulk_transfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
435 u_int16_t flags, u_int32_t timeout, void *buf,
436 u_int32_t *size, char *lbl)
441 usbd_setup_xfer(xfer, pipe, 0, buf, *size,
442 flags, timeout, usbd_bulk_transfer_cb);
443 DPRINTFN(1, ("usbd_bulk_transfer: start transfer %d bytes\n", *size));
444 s = splusb(); /* don't want callback until tsleep() */
445 err = usbd_transfer(xfer);
446 if (err != USBD_IN_PROGRESS) {
450 error = tsleep((caddr_t)xfer, PZERO | PCATCH, lbl, 0);
453 DPRINTF(("usbd_bulk_transfer: tsleep=%d\n", error));
454 usbd_abort_pipe(pipe);
455 return (USBD_INTERRUPTED);
457 usbd_get_xfer_status(xfer, 0, 0, size, &err);
458 DPRINTFN(1,("usbd_bulk_transfer: transferred %d\n", *size));
460 DPRINTF(("usbd_bulk_transfer: error=%d\n", err));
461 usbd_clear_endpoint_stall(pipe);
467 usb_detach_wait(device_ptr_t dv)
469 DPRINTF(("usb_detach_wait: waiting for %s\n", USBDEVPTRNAME(dv)));
470 if (tsleep(dv, PZERO, "usbdet", hz * 60))
471 printf("usb_detach_wait: %s didn't detach\n",
473 DPRINTF(("usb_detach_wait: %s done\n", USBDEVPTRNAME(dv)));
477 usb_detach_wakeup(device_ptr_t dv)
479 DPRINTF(("usb_detach_wakeup: for %s\n", USBDEVPTRNAME(dv)));