Line data Source code
1 : /* $OpenBSD: usbdi_util.c,v 1.42 2017/01/09 14:44:28 mpi Exp $ */
2 : /* $NetBSD: usbdi_util.c,v 1.40 2002/07/11 21:14:36 augustss Exp $ */
3 : /* $FreeBSD: src/sys/dev/usb/usbdi_util.c,v 1.14 1999/11/17 22:33:50 n_hibma 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 : *
22 : * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23 : * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 : * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 : * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26 : * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 : * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 : * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 : * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 : * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 : * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 : * POSSIBILITY OF SUCH DAMAGE.
33 : */
34 :
35 : #include <sys/param.h>
36 : #include <sys/systm.h>
37 : #include <sys/kernel.h>
38 : #include <sys/device.h>
39 :
40 : #include <machine/bus.h>
41 :
42 : #include <dev/usb/usb.h>
43 : #include <dev/usb/usbhid.h>
44 :
45 : #include <dev/usb/usbdi.h>
46 : #include <dev/usb/usbdi_util.h>
47 :
48 : #ifdef USB_DEBUG
49 : #define DPRINTF(x) do { if (usbdebug) printf x; } while (0)
50 : #define DPRINTFN(n,x) do { if (usbdebug>(n)) printf x; } while (0)
51 : extern int usbdebug;
52 : #else
53 : #define DPRINTF(x)
54 : #define DPRINTFN(n,x)
55 : #endif
56 :
57 : usbd_status
58 0 : usbd_get_desc(struct usbd_device *dev, int type, int index, int len, void *desc)
59 : {
60 0 : usb_device_request_t req;
61 :
62 : DPRINTFN(3,("usbd_get_desc: type=%d, index=%d, len=%d\n", type, index,
63 : len));
64 :
65 0 : req.bmRequestType = UT_READ_DEVICE;
66 0 : req.bRequest = UR_GET_DESCRIPTOR;
67 0 : USETW2(req.wValue, type, index);
68 0 : USETW(req.wIndex, 0);
69 0 : USETW(req.wLength, len);
70 0 : return (usbd_do_request(dev, &req, desc));
71 0 : }
72 :
73 : usbd_status
74 0 : usbd_get_device_status(struct usbd_device *dev, usb_status_t *st)
75 : {
76 0 : usb_device_request_t req;
77 :
78 0 : req.bmRequestType = UT_READ_DEVICE;
79 0 : req.bRequest = UR_GET_STATUS;
80 0 : USETW(req.wValue, 0);
81 0 : USETW(req.wIndex, 0);
82 0 : USETW(req.wLength, sizeof(usb_status_t));
83 0 : return (usbd_do_request(dev, &req, st));
84 0 : }
85 :
86 : usbd_status
87 0 : usbd_get_hub_status(struct usbd_device *dev, usb_hub_status_t *st)
88 : {
89 0 : usb_device_request_t req;
90 :
91 0 : req.bmRequestType = UT_READ_CLASS_DEVICE;
92 0 : req.bRequest = UR_GET_STATUS;
93 0 : USETW(req.wValue, 0);
94 0 : USETW(req.wIndex, 0);
95 0 : USETW(req.wLength, sizeof(usb_hub_status_t));
96 0 : return (usbd_do_request(dev, &req, st));
97 0 : }
98 :
99 : usbd_status
100 0 : usbd_get_hub_descriptor(struct usbd_device *dev, usb_hub_descriptor_t *hd,
101 : uint8_t nports)
102 : {
103 0 : usb_device_request_t req;
104 0 : uint16_t len = USB_HUB_DESCRIPTOR_SIZE + (nports + 1) / 8;
105 :
106 0 : req.bmRequestType = UT_READ_CLASS_DEVICE;
107 0 : req.bRequest = UR_GET_DESCRIPTOR;
108 0 : USETW2(req.wValue, UDESC_HUB, 0);
109 0 : USETW(req.wIndex, 0);
110 0 : USETW(req.wLength, len);
111 0 : return (usbd_do_request(dev, &req, hd));
112 0 : }
113 :
114 : usbd_status
115 0 : usbd_get_hub_ss_descriptor(struct usbd_device *dev, usb_hub_ss_descriptor_t *hd,
116 : uint8_t nports)
117 : {
118 0 : usb_device_request_t req;
119 0 : uint16_t len = USB_HUB_SS_DESCRIPTOR_SIZE + (nports + 1) / 8;
120 :
121 0 : req.bmRequestType = UT_READ_CLASS_DEVICE;
122 0 : req.bRequest = UR_GET_DESCRIPTOR;
123 0 : USETW2(req.wValue, UDESC_SS_HUB, 0);
124 0 : USETW(req.wIndex, 0);
125 0 : USETW(req.wLength, len);
126 0 : return (usbd_do_request(dev, &req, hd));
127 0 : }
128 :
129 : usbd_status
130 0 : usbd_get_port_status(struct usbd_device *dev, int port, usb_port_status_t *ps)
131 : {
132 0 : usb_device_request_t req;
133 :
134 0 : req.bmRequestType = UT_READ_CLASS_OTHER;
135 0 : req.bRequest = UR_GET_STATUS;
136 0 : USETW(req.wValue, 0);
137 0 : USETW(req.wIndex, port);
138 0 : USETW(req.wLength, sizeof *ps);
139 0 : return (usbd_do_request(dev, &req, ps));
140 0 : }
141 :
142 : usbd_status
143 0 : usbd_clear_hub_feature(struct usbd_device *dev, int sel)
144 : {
145 0 : usb_device_request_t req;
146 :
147 0 : req.bmRequestType = UT_WRITE_CLASS_DEVICE;
148 0 : req.bRequest = UR_CLEAR_FEATURE;
149 0 : USETW(req.wValue, sel);
150 0 : USETW(req.wIndex, 0);
151 0 : USETW(req.wLength, 0);
152 0 : return (usbd_do_request(dev, &req, 0));
153 0 : }
154 :
155 : usbd_status
156 0 : usbd_set_hub_feature(struct usbd_device *dev, int sel)
157 : {
158 0 : usb_device_request_t req;
159 :
160 0 : req.bmRequestType = UT_WRITE_CLASS_DEVICE;
161 0 : req.bRequest = UR_SET_FEATURE;
162 0 : USETW(req.wValue, sel);
163 0 : USETW(req.wIndex, 0);
164 0 : USETW(req.wLength, 0);
165 0 : return (usbd_do_request(dev, &req, 0));
166 0 : }
167 :
168 : usbd_status
169 0 : usbd_set_hub_depth(struct usbd_device *dev, int depth)
170 : {
171 0 : usb_device_request_t req;
172 :
173 0 : req.bmRequestType = UT_WRITE_CLASS_DEVICE;
174 0 : req.bRequest = UR_SET_DEPTH;
175 0 : USETW(req.wValue, depth);
176 0 : USETW(req.wIndex, 0);
177 0 : USETW(req.wLength, 0);
178 0 : return usbd_do_request(dev, &req, 0);
179 0 : }
180 :
181 : usbd_status
182 0 : usbd_clear_port_feature(struct usbd_device *dev, int port, int sel)
183 : {
184 0 : usb_device_request_t req;
185 :
186 0 : req.bmRequestType = UT_WRITE_CLASS_OTHER;
187 0 : req.bRequest = UR_CLEAR_FEATURE;
188 0 : USETW(req.wValue, sel);
189 0 : USETW(req.wIndex, port);
190 0 : USETW(req.wLength, 0);
191 0 : return (usbd_do_request(dev, &req, 0));
192 0 : }
193 :
194 : usbd_status
195 0 : usbd_set_port_feature(struct usbd_device *dev, int port, int sel)
196 : {
197 0 : usb_device_request_t req;
198 :
199 0 : req.bmRequestType = UT_WRITE_CLASS_OTHER;
200 0 : req.bRequest = UR_SET_FEATURE;
201 0 : USETW(req.wValue, sel);
202 0 : USETW(req.wIndex, port);
203 0 : USETW(req.wLength, 0);
204 0 : return (usbd_do_request(dev, &req, 0));
205 0 : }
206 :
207 : usbd_status
208 0 : usbd_set_idle(struct usbd_device *dev, int ifaceno, int duration, int id)
209 : {
210 0 : usb_device_request_t req;
211 :
212 : DPRINTFN(4, ("usbd_set_idle: %d %d\n", duration, id));
213 0 : req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
214 0 : req.bRequest = UR_SET_IDLE;
215 0 : USETW2(req.wValue, duration, id);
216 0 : USETW(req.wIndex, ifaceno);
217 0 : USETW(req.wLength, 0);
218 0 : return (usbd_do_request(dev, &req, 0));
219 0 : }
220 :
221 : usbd_status
222 0 : usbd_get_report_descriptor(struct usbd_device *dev, int ifaceno,
223 : void *data, int len)
224 : {
225 0 : usb_device_request_t req;
226 :
227 0 : req.bmRequestType = UT_READ_INTERFACE;
228 0 : req.bRequest = UR_GET_DESCRIPTOR;
229 0 : USETW2(req.wValue, UDESC_REPORT, 0); /* report id should be 0 */
230 0 : USETW(req.wIndex, ifaceno);
231 0 : USETW(req.wLength, len);
232 0 : return (usbd_do_request(dev, &req, data));
233 0 : }
234 :
235 : struct usb_hid_descriptor *
236 0 : usbd_get_hid_descriptor(struct usbd_device *dev, usb_interface_descriptor_t *id)
237 : {
238 0 : usb_config_descriptor_t *cdesc = usbd_get_config_descriptor(dev);
239 : struct usb_hid_descriptor *hd;
240 : char *p, *end;
241 :
242 0 : p = (char *)id + id->bLength;
243 0 : end = (char *)cdesc + UGETW(cdesc->wTotalLength);
244 :
245 0 : for (; p < end; p += hd->bLength) {
246 0 : hd = (struct usb_hid_descriptor *)p;
247 0 : if (p + hd->bLength <= end && hd->bDescriptorType == UDESC_HID)
248 0 : return (hd);
249 0 : if (hd->bDescriptorType == UDESC_INTERFACE)
250 : break;
251 : }
252 0 : return (0);
253 0 : }
254 :
255 : usbd_status
256 0 : usbd_get_config(struct usbd_device *dev, u_int8_t *conf)
257 : {
258 0 : usb_device_request_t req;
259 :
260 0 : req.bmRequestType = UT_READ_DEVICE;
261 0 : req.bRequest = UR_GET_CONFIG;
262 0 : USETW(req.wValue, 0);
263 0 : USETW(req.wIndex, 0);
264 0 : USETW(req.wLength, 1);
265 0 : return (usbd_do_request(dev, &req, conf));
266 0 : }
267 :
268 : void
269 0 : usb_detach_wait(struct device *dv)
270 : {
271 : DPRINTF(("usb_detach_wait: waiting for %s\n", dv->dv_xname));
272 0 : if (tsleep(dv, PZERO, "usbdet", hz * 60))
273 0 : printf("usb_detach_wait: %s didn't detach\n", dv->dv_xname);
274 : DPRINTF(("usb_detach_wait: %s done\n", dv->dv_xname));
275 0 : }
276 :
277 : void
278 0 : usb_detach_wakeup(struct device *dv)
279 : {
280 : DPRINTF(("usb_detach_wakeup: for %s\n", dv->dv_xname));
281 0 : wakeup(dv);
282 0 : }
|