]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/usb/usbdi_util.c
Use __FBSDID().
[FreeBSD/FreeBSD.git] / sys / dev / usb / usbdi_util.c
1 /*      $NetBSD: usbdi_util.c,v 1.36 2001/11/13 06:24:57 lukem 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 (lennart@augustsson.net) 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/cdefs.h>
41 __FBSDID("$FreeBSD$");
42
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__)
48 #include <sys/proc.h>
49 #include <sys/device.h>
50 #elif defined(__FreeBSD__)
51 #include <sys/bus.h>
52 #endif
53
54 #include <dev/usb/usb.h>
55 #include <dev/usb/usbhid.h>
56
57 #include <dev/usb/usbdi.h>
58 #include <dev/usb/usbdi_util.h>
59
60 #ifdef USB_DEBUG
61 #define DPRINTF(x)      if (usbdebug) logprintf x
62 #define DPRINTFN(n,x)   if (usbdebug>(n)) logprintf x
63 extern int usbdebug;
64 #else
65 #define DPRINTF(x)
66 #define DPRINTFN(n,x)
67 #endif
68
69 usbd_status
70 usbd_get_desc(usbd_device_handle dev, int type, int index, int len, void *desc)
71 {
72         usb_device_request_t req;
73
74         DPRINTFN(3,("usbd_get_desc: type=%d, index=%d, len=%d\n",
75                     type, index, len));
76
77         req.bmRequestType = UT_READ_DEVICE;
78         req.bRequest = UR_GET_DESCRIPTOR;
79         USETW2(req.wValue, type, index);
80         USETW(req.wIndex, 0);
81         USETW(req.wLength, len);
82         return (usbd_do_request(dev, &req, desc));
83 }
84
85 usbd_status
86 usbd_get_config_desc(usbd_device_handle dev, int confidx,
87                      usb_config_descriptor_t *d)
88 {
89         usbd_status err;
90
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);
94         if (err)
95                 return (err);
96         if (d->bDescriptorType != UDESC_CONFIG) {
97                 DPRINTFN(-1,("usbd_get_config_desc: confidx=%d, bad desc "
98                              "len=%d type=%d\n",
99                              confidx, d->bLength, d->bDescriptorType));
100                 return (USBD_INVAL);
101         }
102         return (USBD_NORMAL_COMPLETION);
103 }
104
105 usbd_status
106 usbd_get_config_desc_full(usbd_device_handle dev, int conf, void *d, int size)
107 {
108         DPRINTFN(3,("usbd_get_config_desc_full: conf=%d\n", conf));
109         return (usbd_get_desc(dev, UDESC_CONFIG, conf, size, d));
110 }
111
112 usbd_status
113 usbd_get_device_desc(usbd_device_handle dev, usb_device_descriptor_t *d)
114 {
115         DPRINTFN(3,("usbd_get_device_desc:\n"));
116         return (usbd_get_desc(dev, UDESC_DEVICE,
117                              0, USB_DEVICE_DESCRIPTOR_SIZE, d));
118 }
119
120 usbd_status
121 usbd_get_device_status(usbd_device_handle dev, usb_status_t *st)
122 {
123         usb_device_request_t req;
124
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));
131 }
132
133 usbd_status
134 usbd_get_hub_status(usbd_device_handle dev, usb_hub_status_t *st)
135 {
136         usb_device_request_t req;
137
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));
144 }
145
146 usbd_status
147 usbd_set_address(usbd_device_handle dev, int addr)
148 {
149         usb_device_request_t req;
150
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);
157 }
158
159 usbd_status
160 usbd_get_port_status(usbd_device_handle dev, int port, usb_port_status_t *ps)
161 {
162         usb_device_request_t req;
163
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));
170 }
171
172 usbd_status
173 usbd_clear_hub_feature(usbd_device_handle dev, int sel)
174 {
175         usb_device_request_t req;
176
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));
183 }
184
185 usbd_status
186 usbd_set_hub_feature(usbd_device_handle dev, int sel)
187 {
188         usb_device_request_t req;
189
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));
196 }
197
198 usbd_status
199 usbd_clear_port_feature(usbd_device_handle dev, int port, int sel)
200 {
201         usb_device_request_t req;
202
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));
209 }
210
211 usbd_status
212 usbd_set_port_feature(usbd_device_handle dev, int port, int sel)
213 {
214         usb_device_request_t req;
215
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));
222 }
223
224
225 usbd_status
226 usbd_set_protocol(usbd_interface_handle iface, int report)
227 {
228         usb_interface_descriptor_t *id = usbd_get_interface_descriptor(iface);
229         usbd_device_handle dev;
230         usb_device_request_t req;
231
232         DPRINTFN(4, ("usbd_set_protocol: iface=%p, report=%d, endpt=%d\n",
233                      iface, report, id->bInterfaceNumber));
234         if (id == NULL)
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));
243 }
244
245 usbd_status
246 usbd_set_report(usbd_interface_handle iface, int type, int id, void *data,
247                 int len)
248 {
249         usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
250         usbd_device_handle dev;
251         usb_device_request_t req;
252
253         DPRINTFN(4, ("usbd_set_report: len=%d\n", len));
254         if (ifd == NULL)
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));
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
273         DPRINTFN(4, ("usbd_set_report_async: len=%d\n", len));
274         if (ifd == NULL)
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));
283 }
284
285 usbd_status
286 usbd_get_report(usbd_interface_handle iface, int type, int id, void *data,
287                 int len)
288 {
289         usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
290         usbd_device_handle dev;
291         usb_device_request_t req;
292
293         DPRINTFN(4, ("usbd_set_report: len=%d\n", len));
294         if (id == 0)
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));
303 }
304
305 usbd_status
306 usbd_set_idle(usbd_interface_handle iface, int duration, int id)
307 {
308         usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
309         usbd_device_handle dev;
310         usb_device_request_t req;
311
312         DPRINTFN(4, ("usbd_set_idle: %d %d\n", duration, id));
313         if (ifd == NULL)
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));
322 }
323
324 usbd_status
325 usbd_get_report_descriptor(usbd_device_handle dev, int ifcno,
326                            int size, void *d)
327 {
328         usb_device_request_t req;
329
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));
336 }
337
338 usb_hid_descriptor_t *
339 usbd_get_hid_descriptor(usbd_interface_handle ifc)
340 {
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;
345         char *p, *end;
346
347         if (idesc == NULL)
348                 return (0);
349         usbd_interface2device_handle(ifc, &dev);
350         cdesc = usbd_get_config_descriptor(dev);
351
352         p = (char *)idesc + idesc->bLength;
353         end = (char *)cdesc + UGETW(cdesc->wTotalLength);
354
355         for (; p < end; p += hd->bLength) {
356                 hd = (usb_hid_descriptor_t *)p;
357                 if (p + hd->bLength <= end && hd->bDescriptorType == UDESC_HID)
358                         return (hd);
359                 if (hd->bDescriptorType == UDESC_INTERFACE)
360                         break;
361         }
362         return (0);
363 }
364
365 usbd_status
366 usbd_read_report_desc(usbd_interface_handle ifc, void **descp, int *sizep,
367                       usb_malloc_type mem)
368 {
369         usb_interface_descriptor_t *id;
370         usb_hid_descriptor_t *hid;
371         usbd_device_handle dev;
372         usbd_status err;
373
374         usbd_interface2device_handle(ifc, &dev);
375         id = usbd_get_interface_descriptor(ifc);
376         if (id == NULL)
377                 return (USBD_INVAL);
378         hid = usbd_get_hid_descriptor(ifc);
379         if (hid == NULL)
380                 return (USBD_IOERROR);
381         *sizep = UGETW(hid->descrs[0].wDescriptorLength);
382         *descp = malloc(*sizep, mem, M_NOWAIT);
383         if (*descp == NULL)
384                 return (USBD_NOMEM);
385         err = usbd_get_report_descriptor(dev, id->bInterfaceNumber,
386                                          *sizep, *descp);
387         if (err) {
388                 free(*descp, mem);
389                 *descp = NULL;
390                 return (err);
391         }
392         return (USBD_NORMAL_COMPLETION);
393 }
394
395 usbd_status
396 usbd_get_config(usbd_device_handle dev, u_int8_t *conf)
397 {
398         usb_device_request_t req;
399
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));
406 }
407
408 Static void usbd_bulk_transfer_cb(usbd_xfer_handle xfer,
409                                   usbd_private_handle priv, usbd_status status);
410 Static void
411 usbd_bulk_transfer_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
412                       usbd_status status)
413 {
414         wakeup(xfer);
415 }
416
417 usbd_status
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)
421 {
422         usbd_status err;
423         int s, error;
424
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) {
431                 splx(s);
432                 return (err);
433         }
434         error = tsleep(xfer, PZERO | PCATCH, lbl, 0);
435         splx(s);
436         if (error) {
437                 DPRINTF(("usbd_bulk_transfer: tsleep=%d\n", error));
438                 usbd_abort_pipe(pipe);
439                 return (USBD_INTERRUPTED);
440         }
441         usbd_get_xfer_status(xfer, NULL, NULL, size, &err);
442         DPRINTFN(1,("usbd_bulk_transfer: transferred %d\n", *size));
443         if (err) {
444                 DPRINTF(("usbd_bulk_transfer: error=%d\n", err));
445                 usbd_clear_endpoint_stall(pipe);
446         }
447         return (err);
448 }
449
450 Static void usbd_intr_transfer_cb(usbd_xfer_handle xfer,
451                                   usbd_private_handle priv, usbd_status status);
452 Static void
453 usbd_intr_transfer_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
454                       usbd_status status)
455 {
456         wakeup(xfer);
457 }
458
459 usbd_status
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)
463 {
464         usbd_status err;
465         int s, error;
466
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) {
473                 splx(s);
474                 return (err);
475         }
476         error = tsleep(xfer, PZERO | PCATCH, lbl, 0);
477         splx(s);
478         if (error) {
479                 DPRINTF(("usbd_intr_transfer: tsleep=%d\n", error));
480                 usbd_abort_pipe(pipe);
481                 return (USBD_INTERRUPTED);
482         }
483         usbd_get_xfer_status(xfer, NULL, NULL, size, &err);
484         DPRINTFN(1,("usbd_intr_transfer: transferred %d\n", *size));
485         if (err) {
486                 DPRINTF(("usbd_intr_transfer: error=%d\n", err));
487                 usbd_clear_endpoint_stall(pipe);
488         }
489         return (err);
490 }
491
492 void
493 usb_detach_wait(device_ptr_t dv)
494 {
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",
498                         USBDEVPTRNAME(dv));
499         DPRINTF(("usb_detach_wait: %s done\n", USBDEVPTRNAME(dv)));
500 }
501
502 void
503 usb_detach_wakeup(device_ptr_t dv)
504 {
505         DPRINTF(("usb_detach_wakeup: for %s\n", USBDEVPTRNAME(dv)));
506         wakeup(dv);
507 }