Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / bus / usb / usbdi_util.c
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 $ */
3
4 /*
5  * Copyright (c) 1998 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Lennart Augustsson (lennart@augustsson.net) at
10  * Carlstedt Research & Technology.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
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.
27  *
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.
39  */
40
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__)
46 #include <sys/proc.h>
47 #include <sys/device.h>
48 #elif defined(__FreeBSD__)
49 #include <sys/bus.h>
50 #endif
51
52 #include <dev/usb/usb.h>
53 #include <dev/usb/usbhid.h>
54
55 #include <dev/usb/usbdi.h>
56 #include <dev/usb/usbdi_util.h>
57
58 #ifdef USB_DEBUG
59 #define DPRINTF(x)      if (usbdebug) logprintf x
60 #define DPRINTFN(n,x)   if (usbdebug>(n)) logprintf x
61 extern int usbdebug;
62 #else
63 #define DPRINTF(x)
64 #define DPRINTFN(n,x)
65 #endif
66
67 usbd_status
68 usbd_get_desc(usbd_device_handle dev, int type, int index, int len, void *desc)
69 {
70         usb_device_request_t req;
71
72         req.bmRequestType = UT_READ_DEVICE;
73         req.bRequest = UR_GET_DESCRIPTOR;
74         USETW2(req.wValue, type, index);
75         USETW(req.wIndex, 0);
76         USETW(req.wLength, len);
77         return (usbd_do_request(dev, &req, desc));
78 }
79
80 usbd_status
81 usbd_get_config_desc(usbd_device_handle dev, int conf,
82                      usb_config_descriptor_t *d)
83 {
84         usbd_status err;
85
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);
89         if (err)
90                 return (err);
91         if (d->bDescriptorType != UDESC_CONFIG) {
92                 DPRINTFN(-1,("usbd_get_config_desc: conf %d, bad desc %d\n",
93                              conf, d->bDescriptorType));
94                 return (USBD_INVAL);
95         }
96         return (USBD_NORMAL_COMPLETION);
97 }
98
99 usbd_status
100 usbd_get_config_desc_full(usbd_device_handle dev, int conf, void *d, int size)
101 {
102         DPRINTFN(3,("usbd_get_config_desc_full: conf=%d\n", conf));
103         return (usbd_get_desc(dev, UDESC_CONFIG, conf, size, d));
104 }
105
106 usbd_status
107 usbd_get_device_desc(usbd_device_handle dev, usb_device_descriptor_t *d)
108 {
109         DPRINTFN(3,("usbd_get_device_desc:\n"));
110         return (usbd_get_desc(dev, UDESC_DEVICE, 
111                              0, USB_DEVICE_DESCRIPTOR_SIZE, d));
112 }
113
114 usbd_status
115 usbd_get_device_status(usbd_device_handle dev, usb_status_t *st)
116 {
117         usb_device_request_t req;
118
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));
125 }       
126
127 usbd_status
128 usbd_get_hub_status(usbd_device_handle dev, usb_hub_status_t *st)
129 {
130         usb_device_request_t req;
131
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));
138 }       
139
140 usbd_status
141 usbd_set_address(usbd_device_handle dev, int addr)
142 {
143         usb_device_request_t req;
144
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);
151 }
152
153 usbd_status
154 usbd_get_port_status(usbd_device_handle dev, int port, usb_port_status_t *ps)
155 {
156         usb_device_request_t req;
157
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));
164 }
165
166 usbd_status
167 usbd_clear_hub_feature(usbd_device_handle dev, int sel)
168 {
169         usb_device_request_t req;
170
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));
177 }
178
179 usbd_status
180 usbd_set_hub_feature(usbd_device_handle dev, int sel)
181 {
182         usb_device_request_t req;
183
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));
190 }
191
192 usbd_status
193 usbd_clear_port_feature(usbd_device_handle dev, int port, int sel)
194 {
195         usb_device_request_t req;
196
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));
203 }
204
205 usbd_status
206 usbd_set_port_feature(usbd_device_handle dev, int port, int sel)
207 {
208         usb_device_request_t req;
209
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));
216 }
217
218
219 usbd_status
220 usbd_set_protocol(usbd_interface_handle iface, int report)
221 {
222         usb_interface_descriptor_t *id = usbd_get_interface_descriptor(iface);
223         usbd_device_handle dev;
224         usb_device_request_t req;
225         usbd_status err;
226
227         DPRINTFN(4, ("usbd_set_protocol: iface=%p, report=%d, endpt=%d\n",
228                      iface, report, id->bInterfaceNumber));
229         if (id == NULL)
230                 return (USBD_IOERROR);
231         err = usbd_interface2device_handle(iface, &dev);
232         if (err)
233                 return (err);
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));
240 }
241
242 usbd_status
243 usbd_set_report(usbd_interface_handle iface, int type, int id, void *data,
244                 int len)
245 {
246         usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
247         usbd_device_handle dev;
248         usb_device_request_t req;
249         usbd_status err;
250
251         DPRINTFN(4, ("usbd_set_report: len=%d\n", len));
252         if (ifd == NULL)
253                 return (USBD_IOERROR);
254         err = usbd_interface2device_handle(iface, &dev);
255         if (err)
256                 return (err);
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));
263 }
264
265 usbd_status
266 usbd_set_report_async(usbd_interface_handle iface, int type, int id, void *data,
267                       int len)
268 {
269         usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
270         usbd_device_handle dev;
271         usb_device_request_t req;
272         usbd_status err;
273
274         DPRINTFN(4, ("usbd_set_report_async: len=%d\n", len));
275         if (ifd == NULL)
276                 return (USBD_IOERROR);
277         err = usbd_interface2device_handle(iface, &dev);
278         if (err)
279                 return (err);
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));
286 }
287
288 usbd_status
289 usbd_get_report(usbd_interface_handle iface, int type, int id, void *data,
290                 int len)
291 {
292         usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
293         usbd_device_handle dev;
294         usb_device_request_t req;
295         usbd_status err;
296
297         DPRINTFN(4, ("usbd_set_report: len=%d\n", len));
298         if (id == NULL)
299                 return (USBD_IOERROR);
300         err = usbd_interface2device_handle(iface, &dev);
301         if (err)
302                 return (err);
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));
309 }
310
311 usbd_status
312 usbd_set_idle(usbd_interface_handle iface, int duration, int id)
313 {
314         usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
315         usbd_device_handle dev;
316         usb_device_request_t req;
317         usbd_status err;
318
319         DPRINTFN(4, ("usbd_set_idle: %d %d\n", duration, id));
320         if (ifd == NULL)
321                 return (USBD_IOERROR);
322         err = usbd_interface2device_handle(iface, &dev);
323         if (err)
324                 return (err);
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));
331 }
332
333 usbd_status
334 usbd_get_report_descriptor(usbd_device_handle dev, int ifcno, int repid,
335                            int size, void *d)
336 {
337         usb_device_request_t req;
338
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));
345 }
346
347 usb_hid_descriptor_t *
348 usbd_get_hid_descriptor(usbd_interface_handle ifc)
349 {
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;
354         char *p, *end;
355         usbd_status err;
356
357         if (idesc == NULL)
358                 return (0);
359         err = usbd_interface2device_handle(ifc, &dev);
360         if (err)
361                 return (0);
362         cdesc = usbd_get_config_descriptor(dev);
363
364         p = (char *)idesc + idesc->bLength;
365         end = (char *)cdesc + UGETW(cdesc->wTotalLength);
366
367         for (; p < end; p += hd->bLength) {
368                 hd = (usb_hid_descriptor_t *)p;
369                 if (p + hd->bLength <= end && hd->bDescriptorType == UDESC_HID)
370                         return (hd);
371                 if (hd->bDescriptorType == UDESC_INTERFACE)
372                         break;
373         }
374         return (0);
375 }
376
377 usbd_status
378 usbd_alloc_report_desc(usbd_interface_handle ifc, void **descp, int *sizep,
379                        struct malloc_type *mem)
380 {
381         usb_interface_descriptor_t *id;
382         usb_hid_descriptor_t *hid;
383         usbd_device_handle dev;
384         usbd_status err;
385
386         err = usbd_interface2device_handle(ifc, &dev);
387         if (err)
388                 return (err);
389         id = usbd_get_interface_descriptor(ifc);
390         if (id == NULL)
391                 return (USBD_INVAL);
392         hid = usbd_get_hid_descriptor(ifc);
393         if (hid == NULL)
394                 return (USBD_IOERROR);
395         *sizep = UGETW(hid->descrs[0].wDescriptorLength);
396         *descp = malloc(*sizep, mem, M_NOWAIT);
397         if (*descp == NULL)
398                 return (USBD_NOMEM);
399         /* XXX should not use 0 Report ID */
400         err = usbd_get_report_descriptor(dev, id->bInterfaceNumber, 0, 
401                                        *sizep, *descp);
402         if (err) {
403                 free(*descp, mem);
404                 *descp = NULL;
405                 return (err);
406         }
407         return (USBD_NORMAL_COMPLETION);
408 }
409
410 usbd_status 
411 usbd_get_config(usbd_device_handle dev, u_int8_t *conf)
412 {
413         usb_device_request_t req;
414
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));
421 }
422
423 Static void
424 usbd_bulk_transfer_cb(usbd_xfer_handle xfer, 
425                       usbd_private_handle priv, usbd_status status);
426 Static void
427 usbd_bulk_transfer_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
428                       usbd_status status)
429 {
430         wakeup(xfer);
431 }
432
433 usbd_status
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)
437 {
438         usbd_status err;
439         int s, error;
440
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) {
447                 splx(s);
448                 return (err);
449         }
450         error = tsleep((caddr_t)xfer, PZERO | PCATCH, lbl, 0);
451         splx(s);
452         if (error) {
453                 DPRINTF(("usbd_bulk_transfer: tsleep=%d\n", error));
454                 usbd_abort_pipe(pipe);
455                 return (USBD_INTERRUPTED);
456         }
457         usbd_get_xfer_status(xfer, 0, 0, size, &err);
458         DPRINTFN(1,("usbd_bulk_transfer: transferred %d\n", *size));
459         if (err) {
460                 DPRINTF(("usbd_bulk_transfer: error=%d\n", err));
461                 usbd_clear_endpoint_stall(pipe);
462         }
463         return (err);
464 }
465
466 void
467 usb_detach_wait(device_ptr_t dv)
468 {
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",
472                         USBDEVPTRNAME(dv));
473         DPRINTF(("usb_detach_wait: %s done\n", USBDEVPTRNAME(dv)));
474 }       
475
476 void
477 usb_detach_wakeup(device_ptr_t dv)
478 {
479         DPRINTF(("usb_detach_wakeup: for %s\n", USBDEVPTRNAME(dv)));
480         wakeup(dv);
481 }