xref: /netbsd-src/sys/dev/usb/usbdi_util.c (revision 06be8101a16cc95f40783b3cb7afd12112103a9a)
1 /*	$NetBSD: usbdi_util.c,v 1.36 2001/11/13 06:24:57 lukem 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.36 2001/11/13 06:24:57 lukem 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 	usbd_status err;
233 
234 	DPRINTFN(4, ("usbd_set_protocol: iface=%p, report=%d, endpt=%d\n",
235 		     iface, report, id->bInterfaceNumber));
236 	if (id == NULL)
237 		return (USBD_IOERROR);
238 	err = usbd_interface2device_handle(iface, &dev);
239 	if (err)
240 		return (err);
241 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
242 	req.bRequest = UR_SET_PROTOCOL;
243 	USETW(req.wValue, report);
244 	USETW(req.wIndex, id->bInterfaceNumber);
245 	USETW(req.wLength, 0);
246 	return (usbd_do_request(dev, &req, 0));
247 }
248 
249 usbd_status
250 usbd_set_report(usbd_interface_handle iface, int type, int id, void *data,
251 		int len)
252 {
253 	usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
254 	usbd_device_handle dev;
255 	usb_device_request_t req;
256 	usbd_status err;
257 
258 	DPRINTFN(4, ("usbd_set_report: len=%d\n", len));
259 	if (ifd == NULL)
260 		return (USBD_IOERROR);
261 	err = usbd_interface2device_handle(iface, &dev);
262 	if (err)
263 		return (err);
264 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
265 	req.bRequest = UR_SET_REPORT;
266 	USETW2(req.wValue, type, id);
267 	USETW(req.wIndex, ifd->bInterfaceNumber);
268 	USETW(req.wLength, len);
269 	return (usbd_do_request(dev, &req, data));
270 }
271 
272 usbd_status
273 usbd_set_report_async(usbd_interface_handle iface, int type, int id, void *data,
274 		      int len)
275 {
276 	usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
277 	usbd_device_handle dev;
278 	usb_device_request_t req;
279 	usbd_status err;
280 
281 	DPRINTFN(4, ("usbd_set_report_async: len=%d\n", len));
282 	if (ifd == NULL)
283 		return (USBD_IOERROR);
284 	err = usbd_interface2device_handle(iface, &dev);
285 	if (err)
286 		return (err);
287 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
288 	req.bRequest = UR_SET_REPORT;
289 	USETW2(req.wValue, type, id);
290 	USETW(req.wIndex, ifd->bInterfaceNumber);
291 	USETW(req.wLength, len);
292 	return (usbd_do_request_async(dev, &req, data));
293 }
294 
295 usbd_status
296 usbd_get_report(usbd_interface_handle iface, int type, int id, void *data,
297 		int len)
298 {
299 	usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
300 	usbd_device_handle dev;
301 	usb_device_request_t req;
302 	usbd_status err;
303 
304 	DPRINTFN(4, ("usbd_get_report: len=%d\n", len));
305 	if (ifd == NULL)
306 		return (USBD_IOERROR);
307 	err = usbd_interface2device_handle(iface, &dev);
308 	if (err)
309 		return (err);
310 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
311 	req.bRequest = UR_GET_REPORT;
312 	USETW2(req.wValue, type, id);
313 	USETW(req.wIndex, ifd->bInterfaceNumber);
314 	USETW(req.wLength, len);
315 	return (usbd_do_request(dev, &req, data));
316 }
317 
318 usbd_status
319 usbd_set_idle(usbd_interface_handle iface, int duration, int id)
320 {
321 	usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
322 	usbd_device_handle dev;
323 	usb_device_request_t req;
324 	usbd_status err;
325 
326 	DPRINTFN(4, ("usbd_set_idle: %d %d\n", duration, id));
327 	if (ifd == NULL)
328 		return (USBD_IOERROR);
329 	err = usbd_interface2device_handle(iface, &dev);
330 	if (err)
331 		return (err);
332 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
333 	req.bRequest = UR_SET_IDLE;
334 	USETW2(req.wValue, duration, id);
335 	USETW(req.wIndex, ifd->bInterfaceNumber);
336 	USETW(req.wLength, 0);
337 	return (usbd_do_request(dev, &req, 0));
338 }
339 
340 usbd_status
341 usbd_get_report_descriptor(usbd_device_handle dev, int ifcno,
342 			   int size, void *d)
343 {
344 	usb_device_request_t req;
345 
346 	req.bmRequestType = UT_READ_INTERFACE;
347 	req.bRequest = UR_GET_DESCRIPTOR;
348 	USETW2(req.wValue, UDESC_REPORT, 0); /* report id should be 0 */
349 	USETW(req.wIndex, ifcno);
350 	USETW(req.wLength, size);
351 	return (usbd_do_request(dev, &req, d));
352 }
353 
354 usb_hid_descriptor_t *
355 usbd_get_hid_descriptor(usbd_interface_handle ifc)
356 {
357 	usb_interface_descriptor_t *idesc = usbd_get_interface_descriptor(ifc);
358 	usbd_device_handle dev;
359 	usb_config_descriptor_t *cdesc;
360 	usb_hid_descriptor_t *hd;
361 	char *p, *end;
362 	usbd_status err;
363 
364 	if (idesc == NULL)
365 		return (0);
366 	err = usbd_interface2device_handle(ifc, &dev);
367 	if (err)
368 		return (0);
369 	cdesc = usbd_get_config_descriptor(dev);
370 
371 	p = (char *)idesc + idesc->bLength;
372 	end = (char *)cdesc + UGETW(cdesc->wTotalLength);
373 
374 	for (; p < end; p += hd->bLength) {
375 		hd = (usb_hid_descriptor_t *)p;
376 		if (p + hd->bLength <= end && hd->bDescriptorType == UDESC_HID)
377 			return (hd);
378 		if (hd->bDescriptorType == UDESC_INTERFACE)
379 			break;
380 	}
381 	return (0);
382 }
383 
384 usbd_status
385 usbd_read_report_desc(usbd_interface_handle ifc, void **descp, int *sizep,
386 		       usb_malloc_type mem)
387 {
388 	usb_interface_descriptor_t *id;
389 	usb_hid_descriptor_t *hid;
390 	usbd_device_handle dev;
391 	usbd_status err;
392 
393 	err = usbd_interface2device_handle(ifc, &dev);
394 	if (err)
395 		return (err);
396 	id = usbd_get_interface_descriptor(ifc);
397 	if (id == NULL)
398 		return (USBD_INVAL);
399 	hid = usbd_get_hid_descriptor(ifc);
400 	if (hid == NULL)
401 		return (USBD_IOERROR);
402 	*sizep = UGETW(hid->descrs[0].wDescriptorLength);
403 	*descp = malloc(*sizep, mem, M_NOWAIT);
404 	if (*descp == NULL)
405 		return (USBD_NOMEM);
406 	err = usbd_get_report_descriptor(dev, id->bInterfaceNumber,
407 					 *sizep, *descp);
408 	if (err) {
409 		free(*descp, mem);
410 		*descp = NULL;
411 		return (err);
412 	}
413 	return (USBD_NORMAL_COMPLETION);
414 }
415 
416 usbd_status
417 usbd_get_config(usbd_device_handle dev, u_int8_t *conf)
418 {
419 	usb_device_request_t req;
420 
421 	req.bmRequestType = UT_READ_DEVICE;
422 	req.bRequest = UR_GET_CONFIG;
423 	USETW(req.wValue, 0);
424 	USETW(req.wIndex, 0);
425 	USETW(req.wLength, 1);
426 	return (usbd_do_request(dev, &req, conf));
427 }
428 
429 Static void usbd_bulk_transfer_cb(usbd_xfer_handle xfer,
430 				  usbd_private_handle priv, usbd_status status);
431 Static void
432 usbd_bulk_transfer_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
433 		      usbd_status status)
434 {
435 	wakeup(xfer);
436 }
437 
438 usbd_status
439 usbd_bulk_transfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
440 		   u_int16_t flags, u_int32_t timeout, void *buf,
441 		   u_int32_t *size, char *lbl)
442 {
443 	usbd_status err;
444 	int s, error;
445 
446 	usbd_setup_xfer(xfer, pipe, 0, buf, *size,
447 			flags, timeout, usbd_bulk_transfer_cb);
448 	DPRINTFN(1, ("usbd_bulk_transfer: start transfer %d bytes\n", *size));
449 	s = splusb();		/* don't want callback until tsleep() */
450 	err = usbd_transfer(xfer);
451 	if (err != USBD_IN_PROGRESS) {
452 		splx(s);
453 		return (err);
454 	}
455 	error = tsleep((caddr_t)xfer, PZERO | PCATCH, lbl, 0);
456 	splx(s);
457 	if (error) {
458 		DPRINTF(("usbd_bulk_transfer: tsleep=%d\n", error));
459 		usbd_abort_pipe(pipe);
460 		return (USBD_INTERRUPTED);
461 	}
462 	usbd_get_xfer_status(xfer, NULL, NULL, size, &err);
463 	DPRINTFN(1,("usbd_bulk_transfer: transferred %d\n", *size));
464 	if (err) {
465 		DPRINTF(("usbd_bulk_transfer: error=%d\n", err));
466 		usbd_clear_endpoint_stall(pipe);
467 	}
468 	return (err);
469 }
470 
471 void
472 usb_detach_wait(device_ptr_t dv)
473 {
474 	DPRINTF(("usb_detach_wait: waiting for %s\n", USBDEVPTRNAME(dv)));
475 	if (tsleep(dv, PZERO, "usbdet", hz * 60))
476 		printf("usb_detach_wait: %s didn't detach\n",
477 		        USBDEVPTRNAME(dv));
478 	DPRINTF(("usb_detach_wait: %s done\n", USBDEVPTRNAME(dv)));
479 }
480 
481 void
482 usb_detach_wakeup(device_ptr_t dv)
483 {
484 	DPRINTF(("usb_detach_wakeup: for %s\n", USBDEVPTRNAME(dv)));
485 	wakeup(dv);
486 }
487