usbdi_util.c revision 1.15 1 /* $NetBSD: usbdi_util.c,v 1.15 1999/08/02 19:50:30 augustss 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 (augustss (at) carlstedt.se) 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/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/proc.h>
45 #include <sys/device.h>
46 #if defined(__FreeBSD__)
47 #include <sys/bus.h>
48 #endif
49
50 #include <dev/usb/usb.h>
51 #include <dev/usb/usbhid.h>
52
53 #include <dev/usb/usbdi.h>
54 #include <dev/usb/usbdi_util.h>
55
56 #ifdef USB_DEBUG
57 #define DPRINTF(x) if (usbdebug) printf x
58 #define DPRINTFN(n,x) if (usbdebug>(n)) printf x
59 extern int usbdebug;
60 #else
61 #define DPRINTF(x)
62 #define DPRINTFN(n,x)
63 #endif
64
65 usbd_status
66 usbd_get_desc(dev, type, index, len, desc)
67 usbd_device_handle dev;
68 int type, index;
69 int len;
70 void *desc;
71 {
72 usb_device_request_t req;
73
74 req.bmRequestType = UT_READ_DEVICE;
75 req.bRequest = UR_GET_DESCRIPTOR;
76 USETW2(req.wValue, type, index);
77 USETW(req.wIndex, 0);
78 USETW(req.wLength, len);
79 return (usbd_do_request(dev, &req, desc));
80 }
81
82 usbd_status
83 usbd_get_config_desc(dev, conf, d)
84 usbd_device_handle dev;
85 int conf;
86 usb_config_descriptor_t *d;
87 {
88 usbd_status r;
89
90 DPRINTFN(3,("usbd_get_config_desc: conf=%d\n", conf));
91 r = usbd_get_desc(dev, UDESC_CONFIG, conf,
92 USB_CONFIG_DESCRIPTOR_SIZE, d);
93 if (r != USBD_NORMAL_COMPLETION)
94 return (r);
95 if (d->bDescriptorType != UDESC_CONFIG) {
96 DPRINTFN(-1,("usbd_get_config_desc: conf %d, bad desc %d\n",
97 conf, d->bDescriptorType));
98 return (USBD_INVAL);
99 }
100 return (USBD_NORMAL_COMPLETION);
101 }
102
103 usbd_status
104 usbd_get_config_desc_full(dev, conf, d, size)
105 usbd_device_handle dev;
106 int conf;
107 void *d;
108 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(dev, d)
116 usbd_device_handle dev;
117 usb_device_descriptor_t *d;
118 {
119 DPRINTFN(3,("usbd_get_device_desc:\n"));
120 return (usbd_get_desc(dev, UDESC_DEVICE,
121 0, USB_DEVICE_DESCRIPTOR_SIZE, d));
122 }
123
124 usbd_status
125 usbd_get_device_status(dev, st)
126 usbd_device_handle dev;
127 usb_status_t *st;
128 {
129 usb_device_request_t req;
130
131 req.bmRequestType = UT_READ_DEVICE;
132 req.bRequest = UR_GET_STATUS;
133 USETW(req.wValue, 0);
134 USETW(req.wIndex, 0);
135 USETW(req.wLength, sizeof(usb_status_t));
136 return (usbd_do_request(dev, &req, st));
137 }
138
139 usbd_status
140 usbd_get_hub_status(dev, st)
141 usbd_device_handle dev;
142 usb_hub_status_t *st;
143 {
144 usb_device_request_t req;
145
146 req.bmRequestType = UT_READ_CLASS_DEVICE;
147 req.bRequest = UR_GET_STATUS;
148 USETW(req.wValue, 0);
149 USETW(req.wIndex, 0);
150 USETW(req.wLength, sizeof(usb_hub_status_t));
151 return (usbd_do_request(dev, &req, st));
152 }
153
154 usbd_status
155 usbd_set_address(dev, addr)
156 usbd_device_handle dev;
157 int addr;
158 {
159 usb_device_request_t req;
160
161 req.bmRequestType = UT_WRITE_DEVICE;
162 req.bRequest = UR_SET_ADDRESS;
163 USETW(req.wValue, addr);
164 USETW(req.wIndex, 0);
165 USETW(req.wLength, 0);
166 return usbd_do_request(dev, &req, 0);
167 }
168
169 usbd_status
170 usbd_get_port_status(dev, port, ps)
171 usbd_device_handle dev;
172 int port;
173 usb_port_status_t *ps;
174 {
175 usb_device_request_t req;
176
177 req.bmRequestType = UT_READ_CLASS_OTHER;
178 req.bRequest = UR_GET_STATUS;
179 USETW(req.wValue, 0);
180 USETW(req.wIndex, port);
181 USETW(req.wLength, sizeof *ps);
182 return (usbd_do_request(dev, &req, ps));
183 }
184
185 usbd_status
186 usbd_clear_port_feature(dev, port, sel)
187 usbd_device_handle dev;
188 int port, sel;
189 {
190 usb_device_request_t req;
191
192 req.bmRequestType = UT_WRITE_CLASS_OTHER;
193 req.bRequest = UR_CLEAR_FEATURE;
194 USETW(req.wValue, sel);
195 USETW(req.wIndex, port);
196 USETW(req.wLength, 0);
197 return (usbd_do_request(dev, &req, 0));
198 }
199
200 usbd_status
201 usbd_set_port_feature(dev, port, sel)
202 usbd_device_handle dev;
203 int port, sel;
204 {
205 usb_device_request_t req;
206
207 req.bmRequestType = UT_WRITE_CLASS_OTHER;
208 req.bRequest = UR_SET_FEATURE;
209 USETW(req.wValue, sel);
210 USETW(req.wIndex, port);
211 USETW(req.wLength, 0);
212 return (usbd_do_request(dev, &req, 0));
213 }
214
215
216 usbd_status
217 usbd_set_protocol(iface, report)
218 usbd_interface_handle iface;
219 int report;
220 {
221 usb_interface_descriptor_t *id = usbd_get_interface_descriptor(iface);
222 usbd_device_handle dev;
223 usb_device_request_t req;
224 usbd_status r;
225
226 DPRINTFN(4, ("usbd_set_protocol: iface=%p, report=%d, endpt=%d\n",
227 iface, report, id->bInterfaceNumber));
228 if (!id)
229 return (USBD_IOERROR);
230 r = usbd_interface2device_handle(iface, &dev);
231 if (r != USBD_NORMAL_COMPLETION)
232 return (r);
233 if (!id)
234 return (USBD_INVAL);
235 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
236 req.bRequest = UR_SET_PROTOCOL;
237 USETW(req.wValue, report);
238 USETW(req.wIndex, id->bInterfaceNumber);
239 USETW(req.wLength, 0);
240 return (usbd_do_request(dev, &req, 0));
241 }
242
243 usbd_status
244 usbd_set_report(iface, type, id, data, len)
245 usbd_interface_handle iface;
246 int type;
247 int id;
248 void *data;
249 int len;
250 {
251 usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
252 usbd_device_handle dev;
253 usb_device_request_t req;
254 usbd_status r;
255
256 DPRINTFN(4, ("usbd_set_report: len=%d\n", len));
257 if (!ifd)
258 return (USBD_IOERROR);
259 r = usbd_interface2device_handle(iface, &dev);
260 if (r != USBD_NORMAL_COMPLETION)
261 return (r);
262 if (!ifd)
263 return (USBD_INVAL);
264 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
265 req.bRequest = UR_SET_REPORT;
266 USETW2(req.wValue, type, id);
267 USETW(req.wIndex, ifd->bInterfaceNumber);
268 USETW(req.wLength, len);
269 return (usbd_do_request(dev, &req, data));
270 }
271
272 usbd_status
273 usbd_set_report_async(iface, type, id, data, len)
274 usbd_interface_handle iface;
275 int type;
276 int id;
277 void *data;
278 int len;
279 {
280 usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
281 usbd_device_handle dev;
282 usb_device_request_t req;
283 usbd_status r;
284
285 DPRINTFN(4, ("usbd_set_report_async: len=%d\n", len));
286 if (!ifd)
287 return (USBD_IOERROR);
288 r = usbd_interface2device_handle(iface, &dev);
289 if (r != USBD_NORMAL_COMPLETION)
290 return (r);
291 if (!ifd)
292 return (USBD_INVAL);
293 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
294 req.bRequest = UR_SET_REPORT;
295 USETW2(req.wValue, type, id);
296 USETW(req.wIndex, ifd->bInterfaceNumber);
297 USETW(req.wLength, len);
298 return (usbd_do_request_async(dev, &req, data));
299 }
300
301 usbd_status
302 usbd_get_report(iface, type, id, data, len)
303 usbd_interface_handle iface;
304 int type;
305 int id;
306 void *data;
307 int len;
308 {
309 usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
310 usbd_device_handle dev;
311 usb_device_request_t req;
312 usbd_status r;
313
314 DPRINTFN(4, ("usbd_set_report: len=%d\n", len));
315 if (!id)
316 return (USBD_IOERROR);
317 r = usbd_interface2device_handle(iface, &dev);
318 if (r != USBD_NORMAL_COMPLETION)
319 return (r);
320 if (!ifd)
321 return (USBD_INVAL);
322 req.bmRequestType = UT_READ_CLASS_INTERFACE;
323 req.bRequest = UR_GET_REPORT;
324 USETW2(req.wValue, type, id);
325 USETW(req.wIndex, ifd->bInterfaceNumber);
326 USETW(req.wLength, len);
327 return (usbd_do_request(dev, &req, data));
328 }
329
330 usbd_status
331 usbd_set_idle(iface, duration, id)
332 usbd_interface_handle iface;
333 int duration;
334 int id;
335 {
336 usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
337 usbd_device_handle dev;
338 usb_device_request_t req;
339 usbd_status r;
340
341 DPRINTFN(4, ("usbd_set_idle: %d %d\n", duration, id));
342 if (!ifd)
343 return (USBD_IOERROR);
344 r = usbd_interface2device_handle(iface, &dev);
345 if (r != USBD_NORMAL_COMPLETION)
346 return (r);
347 if (!ifd)
348 return (USBD_INVAL);
349 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
350 req.bRequest = UR_SET_IDLE;
351 USETW2(req.wValue, duration, id);
352 USETW(req.wIndex, ifd->bInterfaceNumber);
353 USETW(req.wLength, 0);
354 return (usbd_do_request(dev, &req, 0));
355 }
356
357 usbd_status
358 usbd_get_report_descriptor(dev, ifcno, repid, size, d)
359 usbd_device_handle dev;
360 int ifcno;
361 int repid;
362 int size;
363 void *d;
364 {
365 usb_device_request_t req;
366
367 req.bmRequestType = UT_READ_INTERFACE;
368 req.bRequest = UR_GET_DESCRIPTOR;
369 USETW2(req.wValue, UDESC_REPORT, repid);
370 USETW(req.wIndex, ifcno);
371 USETW(req.wLength, size);
372 return (usbd_do_request(dev, &req, d));
373 }
374
375 usb_hid_descriptor_t *
376 usbd_get_hid_descriptor(ifc)
377 usbd_interface_handle ifc;
378 {
379 usb_interface_descriptor_t *idesc = usbd_get_interface_descriptor(ifc);
380 usbd_device_handle dev;
381 usb_config_descriptor_t *cdesc;
382 usb_hid_descriptor_t *hd;
383 char *p, *end;
384 usbd_status r;
385
386 if (!idesc)
387 return (0);
388 r = usbd_interface2device_handle(ifc, &dev);
389 if (r != USBD_NORMAL_COMPLETION)
390 return (0);
391 cdesc = usbd_get_config_descriptor(dev);
392
393 p = (char *)idesc + idesc->bLength;
394 end = (char *)cdesc + UGETW(cdesc->wTotalLength);
395
396 for (; p < end; p += hd->bLength) {
397 hd = (usb_hid_descriptor_t *)p;
398 if (p + hd->bLength <= end && hd->bDescriptorType == UDESC_HID)
399 return (hd);
400 if (hd->bDescriptorType == UDESC_INTERFACE)
401 break;
402 }
403 return (0);
404 }
405
406 usbd_status
407 usbd_alloc_report_desc(ifc, descp, sizep, mem)
408 usbd_interface_handle ifc;
409 void **descp;
410 int *sizep;
411 #if defined(__NetBSD__)
412 int mem;
413 #elif defined(__FreeBSD__)
414 struct malloc_type *mem;
415 #endif
416
417 {
418 usb_interface_descriptor_t *id;
419 usb_hid_descriptor_t *hid;
420 usbd_device_handle dev;
421 usbd_status r;
422
423 r = usbd_interface2device_handle(ifc, &dev);
424 if (r != USBD_NORMAL_COMPLETION)
425 return (r);
426 id = usbd_get_interface_descriptor(ifc);
427 if (!id)
428 return (USBD_INVAL);
429 hid = usbd_get_hid_descriptor(ifc);
430 if (!hid)
431 return (USBD_IOERROR);
432 *sizep = UGETW(hid->descrs[0].wDescriptorLength);
433 *descp = malloc(*sizep, mem, M_NOWAIT);
434 if (!*descp)
435 return (USBD_NOMEM);
436 /* XXX should not use 0 Report ID */
437 r = usbd_get_report_descriptor(dev, id->bInterfaceNumber, 0,
438 *sizep, *descp);
439 if (r != USBD_NORMAL_COMPLETION) {
440 free(*descp, mem);
441 return (r);
442 }
443 return (USBD_NORMAL_COMPLETION);
444 }
445
446 usbd_status
447 usbd_get_config(dev, conf)
448 usbd_device_handle dev;
449 u_int8_t *conf;
450 {
451 usb_device_request_t req;
452
453 req.bmRequestType = UT_READ_DEVICE;
454 req.bRequest = UR_GET_CONFIG;
455 USETW(req.wValue, 0);
456 USETW(req.wIndex, 0);
457 USETW(req.wLength, 1);
458 return (usbd_do_request(dev, &req, conf));
459 }
460
461 static void usbd_bulk_transfer_cb __P((usbd_request_handle reqh,
462 usbd_private_handle priv, usbd_status status));
463 static void
464 usbd_bulk_transfer_cb(reqh, priv, status)
465 usbd_request_handle reqh;
466 usbd_private_handle priv;
467 usbd_status status;
468 {
469 wakeup(reqh);
470 }
471
472 usbd_status
473 usbd_bulk_transfer(reqh, pipe, flags, buf, size, lbl)
474 usbd_request_handle reqh;
475 usbd_pipe_handle pipe;
476 u_int16_t flags;
477 void *buf;
478 u_int32_t *size;
479 char *lbl;
480 {
481 usbd_private_handle priv;
482 void *buffer;
483 usbd_status r;
484 int s, error;
485
486 r = usbd_setup_request(reqh, pipe, 0, buf, *size,
487 flags, USBD_NO_TIMEOUT, usbd_bulk_transfer_cb);
488 if (r != USBD_NORMAL_COMPLETION)
489 return (r);
490 DPRINTFN(1, ("usbd_bulk_transfer: start transfer %d bytes\n", *size));
491 s = splusb(); /* don't want callback until tsleep() */
492 r = usbd_transfer(reqh);
493 if (r != USBD_IN_PROGRESS) {
494 splx(s);
495 return (r);
496 }
497 error = tsleep((caddr_t)reqh, PZERO | PCATCH, lbl, 0);
498 splx(s);
499 if (error) {
500 DPRINTF(("usbd_bulk_transfer: tsleep=%d\n", error));
501 usbd_abort_pipe(pipe);
502 return (USBD_INTERRUPTED);
503 }
504 usbd_get_request_status(reqh, &priv, &buffer, size, &r);
505 DPRINTFN(1,("usbd_bulk_transfer: transferred %d\n", *size));
506 if (r != USBD_NORMAL_COMPLETION) {
507 DPRINTF(("usbd_bulk_transfer: error=%d\n", r));
508 usbd_clear_endpoint_stall(pipe);
509 }
510 return (r);
511 }
512
513 void
514 usb_detach_wait(dv)
515 bdevice *dv;
516 {
517 DPRINTF(("usb_detach_wait: waiting for %s\n", USBDEVNAME(*dv)));
518 if (tsleep(dv, PZERO, "usbdet", hz * 60))
519 printf("usb_detach_wait: %s didn't detach\n",
520 USBDEVNAME(*dv));
521 DPRINTF(("usb_detach_wait: %s done\n", USBDEVNAME(*dv)));
522 }
523
524 void
525 usb_detach_wakeup(dv)
526 bdevice *dv;
527 {
528 DPRINTF(("usb_detach_wakeup: for %s\n", USBDEVNAME(*dv)));
529 wakeup(dv);
530 }
531