xref: /openbsd-src/sys/dev/usb/usbdi_util.c (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1 /*	$OpenBSD: usbdi_util.c,v 1.41 2015/03/14 03:38:50 jsg 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/malloc.h>
39 #include <sys/device.h>
40 
41 #include <machine/bus.h>
42 
43 #include <dev/usb/usb.h>
44 #include <dev/usb/usbhid.h>
45 
46 #include <dev/usb/usbdi.h>
47 #include <dev/usb/usbdi_util.h>
48 
49 #ifdef USB_DEBUG
50 #define DPRINTF(x)	do { if (usbdebug) printf x; } while (0)
51 #define DPRINTFN(n,x)	do { if (usbdebug>(n)) printf x; } while (0)
52 extern int usbdebug;
53 #else
54 #define DPRINTF(x)
55 #define DPRINTFN(n,x)
56 #endif
57 
58 usbd_status
59 usbd_get_desc(struct usbd_device *dev, int type, int index, int len, void *desc)
60 {
61 	usb_device_request_t req;
62 
63 	DPRINTFN(3,("usbd_get_desc: type=%d, index=%d, len=%d\n", type, index,
64 	    len));
65 
66 	req.bmRequestType = UT_READ_DEVICE;
67 	req.bRequest = UR_GET_DESCRIPTOR;
68 	USETW2(req.wValue, type, index);
69 	USETW(req.wIndex, 0);
70 	USETW(req.wLength, len);
71 	return (usbd_do_request(dev, &req, desc));
72 }
73 
74 usbd_status
75 usbd_get_device_status(struct usbd_device *dev, usb_status_t *st)
76 {
77 	usb_device_request_t req;
78 
79 	req.bmRequestType = UT_READ_DEVICE;
80 	req.bRequest = UR_GET_STATUS;
81 	USETW(req.wValue, 0);
82 	USETW(req.wIndex, 0);
83 	USETW(req.wLength, sizeof(usb_status_t));
84 	return (usbd_do_request(dev, &req, st));
85 }
86 
87 usbd_status
88 usbd_get_hub_status(struct usbd_device *dev, usb_hub_status_t *st)
89 {
90 	usb_device_request_t req;
91 
92 	req.bmRequestType = UT_READ_CLASS_DEVICE;
93 	req.bRequest = UR_GET_STATUS;
94 	USETW(req.wValue, 0);
95 	USETW(req.wIndex, 0);
96 	USETW(req.wLength, sizeof(usb_hub_status_t));
97 	return (usbd_do_request(dev, &req, st));
98 }
99 
100 usbd_status
101 usbd_get_hub_descriptor(struct usbd_device *dev, usb_hub_descriptor_t *hd,
102     uint8_t nports)
103 {
104 	usb_device_request_t req;
105 	uint16_t len = USB_HUB_DESCRIPTOR_SIZE + (nports + 1) / 8;
106 
107 	req.bmRequestType = UT_READ_CLASS_DEVICE;
108 	req.bRequest = UR_GET_DESCRIPTOR;
109 	USETW2(req.wValue, UDESC_HUB, 0);
110 	USETW(req.wIndex, 0);
111 	USETW(req.wLength, len);
112 	return (usbd_do_request(dev, &req, hd));
113 }
114 
115 usbd_status
116 usbd_get_hub_ss_descriptor(struct usbd_device *dev, usb_hub_ss_descriptor_t *hd,
117     uint8_t nports)
118 {
119 	usb_device_request_t req;
120 	uint16_t len = USB_HUB_SS_DESCRIPTOR_SIZE + (nports + 1) / 8;
121 
122 	req.bmRequestType = UT_READ_CLASS_DEVICE;
123 	req.bRequest = UR_GET_DESCRIPTOR;
124 	USETW2(req.wValue, UDESC_SS_HUB, 0);
125 	USETW(req.wIndex, 0);
126 	USETW(req.wLength, len);
127 	return (usbd_do_request(dev, &req, hd));
128 }
129 
130 usbd_status
131 usbd_get_port_status(struct usbd_device *dev, int port, usb_port_status_t *ps)
132 {
133 	usb_device_request_t req;
134 
135 	req.bmRequestType = UT_READ_CLASS_OTHER;
136 	req.bRequest = UR_GET_STATUS;
137 	USETW(req.wValue, 0);
138 	USETW(req.wIndex, port);
139 	USETW(req.wLength, sizeof *ps);
140 	return (usbd_do_request(dev, &req, ps));
141 }
142 
143 usbd_status
144 usbd_clear_hub_feature(struct usbd_device *dev, int sel)
145 {
146 	usb_device_request_t req;
147 
148 	req.bmRequestType = UT_WRITE_CLASS_DEVICE;
149 	req.bRequest = UR_CLEAR_FEATURE;
150 	USETW(req.wValue, sel);
151 	USETW(req.wIndex, 0);
152 	USETW(req.wLength, 0);
153 	return (usbd_do_request(dev, &req, 0));
154 }
155 
156 usbd_status
157 usbd_set_hub_feature(struct usbd_device *dev, int sel)
158 {
159 	usb_device_request_t req;
160 
161 	req.bmRequestType = UT_WRITE_CLASS_DEVICE;
162 	req.bRequest = UR_SET_FEATURE;
163 	USETW(req.wValue, sel);
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_set_hub_depth(struct usbd_device *dev, int depth)
171 {
172 	usb_device_request_t req;
173 
174 	req.bmRequestType = UT_WRITE_CLASS_DEVICE;
175 	req.bRequest = UR_SET_DEPTH;
176 	USETW(req.wValue, depth);
177 	USETW(req.wIndex, 0);
178 	USETW(req.wLength, 0);
179 	return usbd_do_request(dev, &req, 0);
180 }
181 
182 usbd_status
183 usbd_clear_port_feature(struct usbd_device *dev, int port, int sel)
184 {
185 	usb_device_request_t req;
186 
187 	req.bmRequestType = UT_WRITE_CLASS_OTHER;
188 	req.bRequest = UR_CLEAR_FEATURE;
189 	USETW(req.wValue, sel);
190 	USETW(req.wIndex, port);
191 	USETW(req.wLength, 0);
192 	return (usbd_do_request(dev, &req, 0));
193 }
194 
195 usbd_status
196 usbd_set_port_feature(struct usbd_device *dev, int port, int sel)
197 {
198 	usb_device_request_t req;
199 
200 	req.bmRequestType = UT_WRITE_CLASS_OTHER;
201 	req.bRequest = UR_SET_FEATURE;
202 	USETW(req.wValue, sel);
203 	USETW(req.wIndex, port);
204 	USETW(req.wLength, 0);
205 	return (usbd_do_request(dev, &req, 0));
206 }
207 
208 usbd_status
209 usbd_set_idle(struct usbd_device *dev, int ifaceno, int duration, int id)
210 {
211 	usb_device_request_t req;
212 
213 	DPRINTFN(4, ("usbd_set_idle: %d %d\n", duration, id));
214 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
215 	req.bRequest = UR_SET_IDLE;
216 	USETW2(req.wValue, duration, id);
217 	USETW(req.wIndex, ifaceno);
218 	USETW(req.wLength, 0);
219 	return (usbd_do_request(dev, &req, 0));
220 }
221 
222 usbd_status
223 usbd_get_report_descriptor(struct usbd_device *dev, int ifaceno,
224     void *data, int len)
225 {
226 	usb_device_request_t req;
227 
228 	req.bmRequestType = UT_READ_INTERFACE;
229 	req.bRequest = UR_GET_DESCRIPTOR;
230 	USETW2(req.wValue, UDESC_REPORT, 0); /* report id should be 0 */
231 	USETW(req.wIndex, ifaceno);
232 	USETW(req.wLength, len);
233 	return (usbd_do_request(dev, &req, data));
234 }
235 
236 struct usb_hid_descriptor *
237 usbd_get_hid_descriptor(struct usbd_device *dev, usb_interface_descriptor_t *id)
238 {
239 	usb_config_descriptor_t *cdesc = usbd_get_config_descriptor(dev);
240 	struct usb_hid_descriptor *hd;
241 	char *p, *end;
242 
243 	p = (char *)id + id->bLength;
244 	end = (char *)cdesc + UGETW(cdesc->wTotalLength);
245 
246 	for (; p < end; p += hd->bLength) {
247 		hd = (struct usb_hid_descriptor *)p;
248 		if (p + hd->bLength <= end && hd->bDescriptorType == UDESC_HID)
249 			return (hd);
250 		if (hd->bDescriptorType == UDESC_INTERFACE)
251 			break;
252 	}
253 	return (0);
254 }
255 
256 usbd_status
257 usbd_get_config(struct usbd_device *dev, u_int8_t *conf)
258 {
259 	usb_device_request_t req;
260 
261 	req.bmRequestType = UT_READ_DEVICE;
262 	req.bRequest = UR_GET_CONFIG;
263 	USETW(req.wValue, 0);
264 	USETW(req.wIndex, 0);
265 	USETW(req.wLength, 1);
266 	return (usbd_do_request(dev, &req, conf));
267 }
268 
269 void
270 usb_detach_wait(struct device *dv)
271 {
272 	DPRINTF(("usb_detach_wait: waiting for %s\n", dv->dv_xname));
273 	if (tsleep(dv, PZERO, "usbdet", hz * 60))
274 		printf("usb_detach_wait: %s didn't detach\n", dv->dv_xname);
275 	DPRINTF(("usb_detach_wait: %s done\n", dv->dv_xname));
276 }
277 
278 void
279 usb_detach_wakeup(struct device *dv)
280 {
281 	DPRINTF(("usb_detach_wakeup: for %s\n", dv->dv_xname));
282 	wakeup(dv);
283 }
284