usb ehci - Fix machine freezes from shutdown race (missing commit)
[dragonfly.git] / sys / bus / usb / usbdi_util.c
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.16 2007/08/02 16:19:17 hasso Exp $ */
4
5 /*
6  * Copyright (c) 1998 The NetBSD Foundation, Inc.
7  * All rights reserved.
8  *
9  * This code is derived from software contributed to The NetBSD Foundation
10  * by Lennart Augustsson (lennart@augustsson.net) at
11  * Carlstedt Research & Technology.
12  *
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.
28  *
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.
40  */
41
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>
49
50 #include <bus/usb/usb.h>
51 #include <bus/usb/usbhid.h>
52
53 #include <bus/usb/usbdi.h>
54 #include <bus/usb/usbdi_util.h>
55
56 #ifdef USB_DEBUG
57 #define DPRINTF(x)      if (usbdebug) kprintf x
58 #define DPRINTFN(n,x)   if (usbdebug>(n)) kprintf 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(usbd_device_handle dev, int type, int index, int len, void *desc)
67 {
68         usb_device_request_t req;
69
70         DPRINTFN(3,("usbd_get_desc: type=%d, index=%d, len=%d\n",
71                     type, index, len));
72
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));
79 }
80
81 usbd_status
82 usbd_get_config_desc(usbd_device_handle dev, int confidx,
83                      usb_config_descriptor_t *d)
84 {
85         usbd_status err;
86
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);
97         }
98         return (USBD_NORMAL_COMPLETION);
99 }
100
101 usbd_status
102 usbd_get_config_desc_full(usbd_device_handle dev, int conf, void *d, int size)
103 {
104         DPRINTFN(3,("usbd_get_config_desc_full: conf=%d\n", conf));
105         return (usbd_get_desc(dev, UDESC_CONFIG, conf, size, d));
106 }
107
108 usbd_status
109 usbd_get_device_desc(usbd_device_handle dev, usb_device_descriptor_t *d)
110 {
111         DPRINTFN(3,("usbd_get_device_desc:\n"));
112         return (usbd_get_desc(dev, UDESC_DEVICE,
113                              0, USB_DEVICE_DESCRIPTOR_SIZE, d));
114 }
115
116 usbd_status
117 usbd_get_device_status(usbd_device_handle dev, usb_status_t *st)
118 {
119         usb_device_request_t req;
120
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));
127 }
128
129 usbd_status
130 usbd_get_hub_status(usbd_device_handle dev, usb_hub_status_t *st)
131 {
132         usb_device_request_t req;
133
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));
140 }
141
142 usbd_status
143 usbd_set_address(usbd_device_handle dev, int addr)
144 {
145         usb_device_request_t req;
146
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);
153 }
154
155 usbd_status
156 usbd_get_port_status(usbd_device_handle dev, int port, usb_port_status_t *ps)
157 {
158         usb_device_request_t req;
159
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));
166 }
167
168 usbd_status
169 usbd_clear_hub_feature(usbd_device_handle dev, int sel)
170 {
171         usb_device_request_t req;
172
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));
179 }
180
181 usbd_status
182 usbd_set_hub_feature(usbd_device_handle dev, int sel)
183 {
184         usb_device_request_t req;
185
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));
192 }
193
194 usbd_status
195 usbd_clear_port_feature(usbd_device_handle dev, int port, int sel)
196 {
197         usb_device_request_t req;
198
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));
205 }
206
207 usbd_status
208 usbd_set_port_feature(usbd_device_handle dev, int port, int sel)
209 {
210         usb_device_request_t req;
211
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));
218 }
219
220 usbd_status
221 usbd_get_protocol(usbd_interface_handle iface, u_int8_t *report)
222 {
223         usb_interface_descriptor_t *id = usbd_get_interface_descriptor(iface);
224         usbd_device_handle dev;
225         usb_device_request_t req;
226
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));
238 }
239
240 usbd_status
241 usbd_set_protocol(usbd_interface_handle iface, int report)
242 {
243         usb_interface_descriptor_t *id = usbd_get_interface_descriptor(iface);
244         usbd_device_handle dev;
245         usb_device_request_t req;
246
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));
258 }
259
260 usbd_status
261 usbd_set_report(usbd_interface_handle iface, int type, int id, void *data,
262                 int len)
263 {
264         usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
265         usbd_device_handle dev;
266         usb_device_request_t req;
267
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));
278 }
279
280 usbd_status
281 usbd_set_report_async(usbd_interface_handle iface, int type, int id, void *data,
282                       int len)
283 {
284         usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
285         usbd_device_handle dev;
286         usb_device_request_t req;
287
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));
298 }
299
300 usbd_status
301 usbd_get_report(usbd_interface_handle iface, int type, int id, void *data,
302                 int len)
303 {
304         usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
305         usbd_device_handle dev;
306         usb_device_request_t req;
307
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));
318 }
319
320 usbd_status
321 usbd_set_idle(usbd_interface_handle iface, int duration, int id)
322 {
323         usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
324         usbd_device_handle dev;
325         usb_device_request_t req;
326
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));
337 }
338
339 usbd_status
340 usbd_get_report_descriptor(usbd_device_handle dev, int ifcno,
341                            int size, void *d)
342 {
343         usb_device_request_t req;
344
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));
351 }
352
353 usb_hid_descriptor_t *
354 usbd_get_hid_descriptor(usbd_interface_handle ifc)
355 {
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;
361
362         if (idesc == NULL)
363                 return (NULL);
364         usbd_interface2device_handle(ifc, &dev);
365         cdesc = usbd_get_config_descriptor(dev);
366
367         p = (char *)idesc + idesc->bLength;
368         end = (char *)cdesc + UGETW(cdesc->wTotalLength);
369
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;
376         }
377         return (NULL);
378 }
379
380 usbd_status
381 usbd_read_report_desc(usbd_interface_handle ifc, void **descp, int *sizep,
382                       struct malloc_type *mem)
383 {
384         usb_interface_descriptor_t *id;
385         usb_hid_descriptor_t *hid;
386         usbd_device_handle dev;
387         usbd_status err;
388
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);
404         }
405         return (USBD_NORMAL_COMPLETION);
406 }
407
408 usbd_status
409 usbd_get_config(usbd_device_handle dev, u_int8_t *conf)
410 {
411         usb_device_request_t req;
412
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));
419 }
420
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)
426 {
427         wakeup(xfer);
428 }
429
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)
434 {
435         usbd_status err;
436         int error;
437
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);
446         }
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);
453         }
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);
459         }
460         return (err);
461 }
462
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)
468 {
469         wakeup(xfer);
470 }
471
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)
476 {
477         usbd_status err;
478         int error;
479
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);
488         }
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);
495         }
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);
501         }
502         return (err);
503 }
504
505 void
506 usb_detach_wait(device_t dv)
507 {
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)));
513 }
514
515 void
516 usb_detach_wakeup(device_t dv)
517 {
518         DPRINTF(("usb_detach_wakeup: for %s\n", device_get_nameunit(dv)));
519         wakeup(dv);
520 }
521
522 const usb_descriptor_t *
523 usb_find_desc(usbd_device_handle dev, int type, int subtype)
524 {
525         usbd_desc_iter_t iter;
526         const usb_descriptor_t *desc;
527
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;
535         }
536         return desc;
537 }
538
539 /* same as usb_find_desc(), but searches only in the specified interface. */
540 const usb_descriptor_t *
541 usb_find_desc_if(usbd_device_handle dev, int type, int subtype,
542                  usb_interface_descriptor_t *id)
543 {
544         usbd_desc_iter_t iter;
545         const usb_descriptor_t *desc;
546
547         usb_desc_iter_init(dev, &iter);
548
549         iter.cur = (void *)id;          /* start from the interface desc */
550         usb_desc_iter_next(&iter);      /* and skip it */
551
552         while ((desc = usb_desc_iter_next(&iter)) != NULL) {
553                 if (desc->bDescriptorType == UDESC_INTERFACE) {
554                         /* we ran into the next interface --- not found */
555                         return NULL;
556                 }
557                 if (desc->bDescriptorType == type &&
558                      (subtype == USBD_SUBTYPE_ANY ||
559                       subtype == desc->bDescriptorSubtype))
560                         break;
561         }
562         return desc;
563 }
564