1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
|
/* $OpenBSD: usbdi_util.c,v 1.35 2014/03/07 18:57:23 mpi Exp $ */
/* $NetBSD: usbdi_util.c,v 1.40 2002/07/11 21:14:36 augustss Exp $ */
/* $FreeBSD: src/sys/dev/usb/usbdi_util.c,v 1.14 1999/11/17 22:33:50 n_hibma Exp $ */
/*
* Copyright (c) 1998 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Lennart Augustsson (lennart@augustsson.net) at
* Carlstedt Research & Technology.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/device.h>
#include <machine/bus.h>
#include <dev/usb/usb.h>
#include <dev/usb/usbhid.h>
#include <dev/usb/usbdi.h>
#include <dev/usb/usbdi_util.h>
#include <dev/usb/usbdivar.h>
#ifdef USB_DEBUG
#define DPRINTF(x) do { if (usbdebug) printf x; } while (0)
#define DPRINTFN(n,x) do { if (usbdebug>(n)) printf x; } while (0)
extern int usbdebug;
#else
#define DPRINTF(x)
#define DPRINTFN(n,x)
#endif
static void
usbd_interface2device_handle(struct usbd_interface *iface,
struct usbd_device **dev)
{
*dev = iface->device;
}
usbd_status
usbd_get_desc(struct usbd_device *dev, int type, int index, int len, void *desc)
{
usb_device_request_t req;
DPRINTFN(3,("usbd_get_desc: type=%d, index=%d, len=%d\n", type, index,
len));
req.bmRequestType = UT_READ_DEVICE;
req.bRequest = UR_GET_DESCRIPTOR;
USETW2(req.wValue, type, index);
USETW(req.wIndex, 0);
USETW(req.wLength, len);
return (usbd_do_request(dev, &req, desc));
}
usbd_status
usbd_get_device_status(struct usbd_device *dev, usb_status_t *st)
{
usb_device_request_t req;
req.bmRequestType = UT_READ_DEVICE;
req.bRequest = UR_GET_STATUS;
USETW(req.wValue, 0);
USETW(req.wIndex, 0);
USETW(req.wLength, sizeof(usb_status_t));
return (usbd_do_request(dev, &req, st));
}
usbd_status
usbd_get_hub_status(struct usbd_device *dev, usb_hub_status_t *st)
{
usb_device_request_t req;
req.bmRequestType = UT_READ_CLASS_DEVICE;
req.bRequest = UR_GET_STATUS;
USETW(req.wValue, 0);
USETW(req.wIndex, 0);
USETW(req.wLength, sizeof(usb_hub_status_t));
return (usbd_do_request(dev, &req, st));
}
usbd_status
usbd_get_hub_descriptor(struct usbd_device *dev, usb_hub_descriptor_t *hd,
uint8_t nports)
{
usb_device_request_t req;
uint16_t len = USB_HUB_DESCRIPTOR_SIZE + (nports + 1) / 8;
req.bmRequestType = UT_READ_CLASS_DEVICE;
req.bRequest = UR_GET_DESCRIPTOR;
USETW2(req.wValue, UDESC_HUB, 0);
USETW(req.wIndex, 0);
USETW(req.wLength, len);
return (usbd_do_request(dev, &req, hd));
}
usbd_status
usbd_set_address(struct usbd_device *dev, int addr)
{
usb_device_request_t req;
req.bmRequestType = UT_WRITE_DEVICE;
req.bRequest = UR_SET_ADDRESS;
USETW(req.wValue, addr);
USETW(req.wIndex, 0);
USETW(req.wLength, 0);
return usbd_do_request(dev, &req, 0);
}
usbd_status
usbd_get_port_status(struct usbd_device *dev, int port, usb_port_status_t *ps)
{
usb_device_request_t req;
req.bmRequestType = UT_READ_CLASS_OTHER;
req.bRequest = UR_GET_STATUS;
USETW(req.wValue, 0);
USETW(req.wIndex, port);
USETW(req.wLength, sizeof *ps);
return (usbd_do_request(dev, &req, ps));
}
usbd_status
usbd_clear_hub_feature(struct usbd_device *dev, int sel)
{
usb_device_request_t req;
req.bmRequestType = UT_WRITE_CLASS_DEVICE;
req.bRequest = UR_CLEAR_FEATURE;
USETW(req.wValue, sel);
USETW(req.wIndex, 0);
USETW(req.wLength, 0);
return (usbd_do_request(dev, &req, 0));
}
usbd_status
usbd_set_hub_feature(struct usbd_device *dev, int sel)
{
usb_device_request_t req;
req.bmRequestType = UT_WRITE_CLASS_DEVICE;
req.bRequest = UR_SET_FEATURE;
USETW(req.wValue, sel);
USETW(req.wIndex, 0);
USETW(req.wLength, 0);
return (usbd_do_request(dev, &req, 0));
}
usbd_status
usbd_clear_port_feature(struct usbd_device *dev, int port, int sel)
{
usb_device_request_t req;
req.bmRequestType = UT_WRITE_CLASS_OTHER;
req.bRequest = UR_CLEAR_FEATURE;
USETW(req.wValue, sel);
USETW(req.wIndex, port);
USETW(req.wLength, 0);
return (usbd_do_request(dev, &req, 0));
}
usbd_status
usbd_set_port_feature(struct usbd_device *dev, int port, int sel)
{
usb_device_request_t req;
req.bmRequestType = UT_WRITE_CLASS_OTHER;
req.bRequest = UR_SET_FEATURE;
USETW(req.wValue, sel);
USETW(req.wIndex, port);
USETW(req.wLength, 0);
return (usbd_do_request(dev, &req, 0));
}
usbd_status
usbd_get_protocol(struct usbd_interface *iface, u_int8_t *report)
{
usb_interface_descriptor_t *id = usbd_get_interface_descriptor(iface);
struct usbd_device *dev;
usb_device_request_t req;
DPRINTFN(4, ("usbd_get_protocol: iface=%p, endpt=%d\n", iface,
id->bInterfaceNumber));
if (id == NULL)
return (USBD_IOERROR);
usbd_interface2device_handle(iface, &dev);
req.bmRequestType = UT_READ_CLASS_INTERFACE;
req.bRequest = UR_GET_PROTOCOL;
USETW(req.wValue, 0);
USETW(req.wIndex, id->bInterfaceNumber);
USETW(req.wLength, 1);
return (usbd_do_request(dev, &req, report));
}
usbd_status
usbd_set_protocol(struct usbd_interface *iface, int report)
{
usb_interface_descriptor_t *id = usbd_get_interface_descriptor(iface);
struct usbd_device *dev;
usb_device_request_t req;
DPRINTFN(4, ("usbd_set_protocol: iface=%p, report=%d, endpt=%d\n",
iface, report, id->bInterfaceNumber));
if (id == NULL)
return (USBD_IOERROR);
usbd_interface2device_handle(iface, &dev);
req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
req.bRequest = UR_SET_PROTOCOL;
USETW(req.wValue, report);
USETW(req.wIndex, id->bInterfaceNumber);
USETW(req.wLength, 0);
return (usbd_do_request(dev, &req, 0));
}
usbd_status
usbd_set_report(struct usbd_interface *iface, int type, int id, void *data,
int len)
{
usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
struct usbd_device *dev;
usb_device_request_t req;
DPRINTFN(4, ("usbd_set_report: len=%d\n", len));
if (ifd == NULL)
return (USBD_IOERROR);
usbd_interface2device_handle(iface, &dev);
req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
req.bRequest = UR_SET_REPORT;
USETW2(req.wValue, type, id);
USETW(req.wIndex, ifd->bInterfaceNumber);
USETW(req.wLength, len);
return (usbd_do_request(dev, &req, data));
}
usbd_status
usbd_set_report_async(struct usbd_interface *iface, int type, int id,
void *data, int len)
{
usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
struct usbd_device *dev;
usb_device_request_t req;
DPRINTFN(4, ("usbd_set_report_async: len=%d\n", len));
if (ifd == NULL)
return (USBD_IOERROR);
usbd_interface2device_handle(iface, &dev);
req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
req.bRequest = UR_SET_REPORT;
USETW2(req.wValue, type, id);
USETW(req.wIndex, ifd->bInterfaceNumber);
USETW(req.wLength, len);
return (usbd_do_request_async(dev, &req, data));
}
usbd_status
usbd_get_report(struct usbd_interface *iface, int type, int id, void *data,
int len)
{
usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
struct usbd_device *dev;
usb_device_request_t req;
DPRINTFN(4, ("usbd_get_report: len=%d\n", len));
if (ifd == NULL)
return (USBD_IOERROR);
usbd_interface2device_handle(iface, &dev);
req.bmRequestType = UT_READ_CLASS_INTERFACE;
req.bRequest = UR_GET_REPORT;
USETW2(req.wValue, type, id);
USETW(req.wIndex, ifd->bInterfaceNumber);
USETW(req.wLength, len);
return (usbd_do_request(dev, &req, data));
}
usbd_status
usbd_set_idle(struct usbd_interface *iface, int duration, int id)
{
usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
struct usbd_device *dev;
usb_device_request_t req;
DPRINTFN(4, ("usbd_set_idle: %d %d\n", duration, id));
if (ifd == NULL)
return (USBD_IOERROR);
usbd_interface2device_handle(iface, &dev);
req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
req.bRequest = UR_SET_IDLE;
USETW2(req.wValue, duration, id);
USETW(req.wIndex, ifd->bInterfaceNumber);
USETW(req.wLength, 0);
return (usbd_do_request(dev, &req, 0));
}
usbd_status
usbd_get_report_descriptor(struct usbd_device *dev, int ifcno, int size,
void *d)
{
usb_device_request_t req;
req.bmRequestType = UT_READ_INTERFACE;
req.bRequest = UR_GET_DESCRIPTOR;
USETW2(req.wValue, UDESC_REPORT, 0); /* report id should be 0 */
USETW(req.wIndex, ifcno);
USETW(req.wLength, size);
return (usbd_do_request(dev, &req, d));
}
struct usb_hid_descriptor *
usbd_get_hid_descriptor(struct usbd_interface *ifc)
{
usb_interface_descriptor_t *idesc = usbd_get_interface_descriptor(ifc);
struct usbd_device *dev;
usb_config_descriptor_t *cdesc;
struct usb_hid_descriptor *hd;
char *p, *end;
if (idesc == NULL)
return (0);
usbd_interface2device_handle(ifc, &dev);
cdesc = usbd_get_config_descriptor(dev);
p = (char *)idesc + idesc->bLength;
end = (char *)cdesc + UGETW(cdesc->wTotalLength);
for (; p < end; p += hd->bLength) {
hd = (struct usb_hid_descriptor *)p;
if (p + hd->bLength <= end && hd->bDescriptorType == UDESC_HID)
return (hd);
if (hd->bDescriptorType == UDESC_INTERFACE)
break;
}
return (0);
}
usbd_status
usbd_read_report_desc(struct usbd_interface *ifc, void **descp, int *sizep,
int mem)
{
usb_interface_descriptor_t *id;
struct usb_hid_descriptor *hid;
struct usbd_device *dev;
usbd_status err;
usbd_interface2device_handle(ifc, &dev);
id = usbd_get_interface_descriptor(ifc);
if (id == NULL)
return (USBD_INVAL);
hid = usbd_get_hid_descriptor(ifc);
if (hid == NULL)
return (USBD_IOERROR);
*sizep = UGETW(hid->descrs[0].wDescriptorLength);
*descp = malloc(*sizep, mem, M_NOWAIT);
if (*descp == NULL)
return (USBD_NOMEM);
err = usbd_get_report_descriptor(dev, id->bInterfaceNumber, *sizep,
*descp);
if (err) {
free(*descp, mem);
*descp = NULL;
return (err);
}
return (USBD_NORMAL_COMPLETION);
}
usbd_status
usbd_get_config(struct usbd_device *dev, u_int8_t *conf)
{
usb_device_request_t req;
req.bmRequestType = UT_READ_DEVICE;
req.bRequest = UR_GET_CONFIG;
USETW(req.wValue, 0);
USETW(req.wIndex, 0);
USETW(req.wLength, 1);
return (usbd_do_request(dev, &req, conf));
}
void
usb_detach_wait(struct device *dv)
{
DPRINTF(("usb_detach_wait: waiting for %s\n", dv->dv_xname));
if (tsleep(dv, PZERO, "usbdet", hz * 60))
printf("usb_detach_wait: %s didn't detach\n", dv->dv_xname);
DPRINTF(("usb_detach_wait: %s done\n", dv->dv_xname));
}
void
usb_detach_wakeup(struct device *dv)
{
DPRINTF(("usb_detach_wakeup: for %s\n", dv->dv_xname));
wakeup(dv);
}
|