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