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