xref: /netbsd-src/sys/dev/usb/usbdi_util.c (revision 3b01aba77a7a698587faaae455bbfe740923c1f5)
1 /*	$NetBSD: usbdi_util.c,v 1.34 2001/07/04 16:23:40 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/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/malloc.h>
45 #if defined(__NetBSD__) || defined(__OpenBSD__)
46 #include <sys/proc.h>
47 #include <sys/device.h>
48 #elif defined(__FreeBSD__)
49 #include <sys/bus.h>
50 #endif
51 
52 #include <dev/usb/usb.h>
53 #include <dev/usb/usbhid.h>
54 
55 #include <dev/usb/usbdi.h>
56 #include <dev/usb/usbdi_util.h>
57 
58 #ifdef USB_DEBUG
59 #define DPRINTF(x)	if (usbdebug) logprintf x
60 #define DPRINTFN(n,x)	if (usbdebug>(n)) logprintf x
61 extern int usbdebug;
62 #else
63 #define DPRINTF(x)
64 #define DPRINTFN(n,x)
65 #endif
66 
67 usbd_status
68 usbd_get_desc(usbd_device_handle dev, int type, int index, int len, void *desc)
69 {
70 	usb_device_request_t req;
71 
72 	DPRINTFN(3,("usbd_get_desc: type=%d, index=%d, len=%d\n",
73 		    type, index, len));
74 
75 	req.bmRequestType = UT_READ_DEVICE;
76 	req.bRequest = UR_GET_DESCRIPTOR;
77 	USETW2(req.wValue, type, index);
78 	USETW(req.wIndex, 0);
79 	USETW(req.wLength, len);
80 	return (usbd_do_request(dev, &req, desc));
81 }
82 
83 usbd_status
84 usbd_get_config_desc(usbd_device_handle dev, int confidx,
85 		     usb_config_descriptor_t *d)
86 {
87 	usbd_status err;
88 
89 	DPRINTFN(3,("usbd_get_config_desc: confidx=%d\n", confidx));
90 	err = usbd_get_desc(dev, UDESC_CONFIG, confidx,
91 			    USB_CONFIG_DESCRIPTOR_SIZE, d);
92 	if (err)
93 		return (err);
94 	if (d->bDescriptorType != UDESC_CONFIG) {
95 		DPRINTFN(-1,("usbd_get_config_desc: confidx=%d, bad desc "
96 			     "len=%d type=%d\n",
97 			     confidx, d->bLength, d->bDescriptorType));
98 		return (USBD_INVAL);
99 	}
100 	return (USBD_NORMAL_COMPLETION);
101 }
102 
103 usbd_status
104 usbd_get_config_desc_full(usbd_device_handle dev, int conf, void *d, int size)
105 {
106 	DPRINTFN(3,("usbd_get_config_desc_full: conf=%d\n", conf));
107 	return (usbd_get_desc(dev, UDESC_CONFIG, conf, size, d));
108 }
109 
110 usbd_status
111 usbd_get_device_desc(usbd_device_handle dev, usb_device_descriptor_t *d)
112 {
113 	DPRINTFN(3,("usbd_get_device_desc:\n"));
114 	return (usbd_get_desc(dev, UDESC_DEVICE,
115 			     0, USB_DEVICE_DESCRIPTOR_SIZE, d));
116 }
117 
118 usbd_status
119 usbd_get_device_status(usbd_device_handle dev, usb_status_t *st)
120 {
121 	usb_device_request_t req;
122 
123 	req.bmRequestType = UT_READ_DEVICE;
124 	req.bRequest = UR_GET_STATUS;
125 	USETW(req.wValue, 0);
126 	USETW(req.wIndex, 0);
127 	USETW(req.wLength, sizeof(usb_status_t));
128 	return (usbd_do_request(dev, &req, st));
129 }
130 
131 usbd_status
132 usbd_get_hub_status(usbd_device_handle dev, usb_hub_status_t *st)
133 {
134 	usb_device_request_t req;
135 
136 	req.bmRequestType = UT_READ_CLASS_DEVICE;
137 	req.bRequest = UR_GET_STATUS;
138 	USETW(req.wValue, 0);
139 	USETW(req.wIndex, 0);
140 	USETW(req.wLength, sizeof(usb_hub_status_t));
141 	return (usbd_do_request(dev, &req, st));
142 }
143 
144 usbd_status
145 usbd_set_address(usbd_device_handle dev, int addr)
146 {
147 	usb_device_request_t req;
148 
149 	req.bmRequestType = UT_WRITE_DEVICE;
150 	req.bRequest = UR_SET_ADDRESS;
151 	USETW(req.wValue, addr);
152 	USETW(req.wIndex, 0);
153 	USETW(req.wLength, 0);
154 	return usbd_do_request(dev, &req, 0);
155 }
156 
157 usbd_status
158 usbd_get_port_status(usbd_device_handle dev, int port, usb_port_status_t *ps)
159 {
160 	usb_device_request_t req;
161 
162 	req.bmRequestType = UT_READ_CLASS_OTHER;
163 	req.bRequest = UR_GET_STATUS;
164 	USETW(req.wValue, 0);
165 	USETW(req.wIndex, port);
166 	USETW(req.wLength, sizeof *ps);
167 	return (usbd_do_request(dev, &req, ps));
168 }
169 
170 usbd_status
171 usbd_clear_hub_feature(usbd_device_handle dev, int sel)
172 {
173 	usb_device_request_t req;
174 
175 	req.bmRequestType = UT_WRITE_CLASS_DEVICE;
176 	req.bRequest = UR_CLEAR_FEATURE;
177 	USETW(req.wValue, sel);
178 	USETW(req.wIndex, 0);
179 	USETW(req.wLength, 0);
180 	return (usbd_do_request(dev, &req, 0));
181 }
182 
183 usbd_status
184 usbd_set_hub_feature(usbd_device_handle dev, int sel)
185 {
186 	usb_device_request_t req;
187 
188 	req.bmRequestType = UT_WRITE_CLASS_DEVICE;
189 	req.bRequest = UR_SET_FEATURE;
190 	USETW(req.wValue, sel);
191 	USETW(req.wIndex, 0);
192 	USETW(req.wLength, 0);
193 	return (usbd_do_request(dev, &req, 0));
194 }
195 
196 usbd_status
197 usbd_clear_port_feature(usbd_device_handle dev, int port, int sel)
198 {
199 	usb_device_request_t req;
200 
201 	req.bmRequestType = UT_WRITE_CLASS_OTHER;
202 	req.bRequest = UR_CLEAR_FEATURE;
203 	USETW(req.wValue, sel);
204 	USETW(req.wIndex, port);
205 	USETW(req.wLength, 0);
206 	return (usbd_do_request(dev, &req, 0));
207 }
208 
209 usbd_status
210 usbd_set_port_feature(usbd_device_handle dev, int port, int sel)
211 {
212 	usb_device_request_t req;
213 
214 	req.bmRequestType = UT_WRITE_CLASS_OTHER;
215 	req.bRequest = UR_SET_FEATURE;
216 	USETW(req.wValue, sel);
217 	USETW(req.wIndex, port);
218 	USETW(req.wLength, 0);
219 	return (usbd_do_request(dev, &req, 0));
220 }
221 
222 
223 usbd_status
224 usbd_set_protocol(usbd_interface_handle iface, int report)
225 {
226 	usb_interface_descriptor_t *id = usbd_get_interface_descriptor(iface);
227 	usbd_device_handle dev;
228 	usb_device_request_t req;
229 	usbd_status err;
230 
231 	DPRINTFN(4, ("usbd_set_protocol: iface=%p, report=%d, endpt=%d\n",
232 		     iface, report, id->bInterfaceNumber));
233 	if (id == NULL)
234 		return (USBD_IOERROR);
235 	err = usbd_interface2device_handle(iface, &dev);
236 	if (err)
237 		return (err);
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 	usbd_status err;
254 
255 	DPRINTFN(4, ("usbd_set_report: len=%d\n", len));
256 	if (ifd == NULL)
257 		return (USBD_IOERROR);
258 	err = usbd_interface2device_handle(iface, &dev);
259 	if (err)
260 		return (err);
261 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
262 	req.bRequest = UR_SET_REPORT;
263 	USETW2(req.wValue, type, id);
264 	USETW(req.wIndex, ifd->bInterfaceNumber);
265 	USETW(req.wLength, len);
266 	return (usbd_do_request(dev, &req, data));
267 }
268 
269 usbd_status
270 usbd_set_report_async(usbd_interface_handle iface, int type, int id, void *data,
271 		      int len)
272 {
273 	usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
274 	usbd_device_handle dev;
275 	usb_device_request_t req;
276 	usbd_status err;
277 
278 	DPRINTFN(4, ("usbd_set_report_async: len=%d\n", len));
279 	if (ifd == NULL)
280 		return (USBD_IOERROR);
281 	err = usbd_interface2device_handle(iface, &dev);
282 	if (err)
283 		return (err);
284 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
285 	req.bRequest = UR_SET_REPORT;
286 	USETW2(req.wValue, type, id);
287 	USETW(req.wIndex, ifd->bInterfaceNumber);
288 	USETW(req.wLength, len);
289 	return (usbd_do_request_async(dev, &req, data));
290 }
291 
292 usbd_status
293 usbd_get_report(usbd_interface_handle iface, int type, int id, void *data,
294 		int len)
295 {
296 	usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
297 	usbd_device_handle dev;
298 	usb_device_request_t req;
299 	usbd_status err;
300 
301 	DPRINTFN(4, ("usbd_get_report: len=%d\n", len));
302 	if (ifd == NULL)
303 		return (USBD_IOERROR);
304 	err = usbd_interface2device_handle(iface, &dev);
305 	if (err)
306 		return (err);
307 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
308 	req.bRequest = UR_GET_REPORT;
309 	USETW2(req.wValue, type, id);
310 	USETW(req.wIndex, ifd->bInterfaceNumber);
311 	USETW(req.wLength, len);
312 	return (usbd_do_request(dev, &req, data));
313 }
314 
315 usbd_status
316 usbd_set_idle(usbd_interface_handle iface, int duration, int id)
317 {
318 	usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
319 	usbd_device_handle dev;
320 	usb_device_request_t req;
321 	usbd_status err;
322 
323 	DPRINTFN(4, ("usbd_set_idle: %d %d\n", duration, id));
324 	if (ifd == NULL)
325 		return (USBD_IOERROR);
326 	err = usbd_interface2device_handle(iface, &dev);
327 	if (err)
328 		return (err);
329 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
330 	req.bRequest = UR_SET_IDLE;
331 	USETW2(req.wValue, duration, id);
332 	USETW(req.wIndex, ifd->bInterfaceNumber);
333 	USETW(req.wLength, 0);
334 	return (usbd_do_request(dev, &req, 0));
335 }
336 
337 usbd_status
338 usbd_get_report_descriptor(usbd_device_handle dev, int ifcno, int repid,
339 			   int size, void *d)
340 {
341 	usb_device_request_t req;
342 
343 	req.bmRequestType = UT_READ_INTERFACE;
344 	req.bRequest = UR_GET_DESCRIPTOR;
345 	USETW2(req.wValue, UDESC_REPORT, repid);
346 	USETW(req.wIndex, ifcno);
347 	USETW(req.wLength, size);
348 	return (usbd_do_request(dev, &req, d));
349 }
350 
351 usb_hid_descriptor_t *
352 usbd_get_hid_descriptor(usbd_interface_handle ifc)
353 {
354 	usb_interface_descriptor_t *idesc = usbd_get_interface_descriptor(ifc);
355 	usbd_device_handle dev;
356 	usb_config_descriptor_t *cdesc;
357 	usb_hid_descriptor_t *hd;
358 	char *p, *end;
359 	usbd_status err;
360 
361 	if (idesc == NULL)
362 		return (0);
363 	err = usbd_interface2device_handle(ifc, &dev);
364 	if (err)
365 		return (0);
366 	cdesc = usbd_get_config_descriptor(dev);
367 
368 	p = (char *)idesc + idesc->bLength;
369 	end = (char *)cdesc + UGETW(cdesc->wTotalLength);
370 
371 	for (; p < end; p += hd->bLength) {
372 		hd = (usb_hid_descriptor_t *)p;
373 		if (p + hd->bLength <= end && hd->bDescriptorType == UDESC_HID)
374 			return (hd);
375 		if (hd->bDescriptorType == UDESC_INTERFACE)
376 			break;
377 	}
378 	return (0);
379 }
380 
381 usbd_status
382 usbd_alloc_report_desc(usbd_interface_handle ifc, void **descp, int *sizep,
383 		       usb_malloc_type mem)
384 {
385 	usb_interface_descriptor_t *id;
386 	usb_hid_descriptor_t *hid;
387 	usbd_device_handle dev;
388 	usbd_status err;
389 
390 	err = usbd_interface2device_handle(ifc, &dev);
391 	if (err)
392 		return (err);
393 	id = usbd_get_interface_descriptor(ifc);
394 	if (id == NULL)
395 		return (USBD_INVAL);
396 	hid = usbd_get_hid_descriptor(ifc);
397 	if (hid == NULL)
398 		return (USBD_IOERROR);
399 	*sizep = UGETW(hid->descrs[0].wDescriptorLength);
400 	*descp = malloc(*sizep, mem, M_NOWAIT);
401 	if (*descp == NULL)
402 		return (USBD_NOMEM);
403 	/* XXX should not use 0 Report ID */
404 	err = usbd_get_report_descriptor(dev, id->bInterfaceNumber, 0,
405 				       *sizep, *descp);
406 	if (err) {
407 		free(*descp, mem);
408 		*descp = NULL;
409 		return (err);
410 	}
411 	return (USBD_NORMAL_COMPLETION);
412 }
413 
414 usbd_status
415 usbd_get_config(usbd_device_handle dev, u_int8_t *conf)
416 {
417 	usb_device_request_t req;
418 
419 	req.bmRequestType = UT_READ_DEVICE;
420 	req.bRequest = UR_GET_CONFIG;
421 	USETW(req.wValue, 0);
422 	USETW(req.wIndex, 0);
423 	USETW(req.wLength, 1);
424 	return (usbd_do_request(dev, &req, conf));
425 }
426 
427 Static void usbd_bulk_transfer_cb(usbd_xfer_handle xfer,
428 				  usbd_private_handle priv, usbd_status status);
429 Static void
430 usbd_bulk_transfer_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
431 		      usbd_status status)
432 {
433 	wakeup(xfer);
434 }
435 
436 usbd_status
437 usbd_bulk_transfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
438 		   u_int16_t flags, u_int32_t timeout, void *buf,
439 		   u_int32_t *size, char *lbl)
440 {
441 	usbd_status err;
442 	int s, error;
443 
444 	usbd_setup_xfer(xfer, pipe, 0, buf, *size,
445 			flags, timeout, usbd_bulk_transfer_cb);
446 	DPRINTFN(1, ("usbd_bulk_transfer: start transfer %d bytes\n", *size));
447 	s = splusb();		/* don't want callback until tsleep() */
448 	err = usbd_transfer(xfer);
449 	if (err != USBD_IN_PROGRESS) {
450 		splx(s);
451 		return (err);
452 	}
453 	error = tsleep((caddr_t)xfer, PZERO | PCATCH, lbl, 0);
454 	splx(s);
455 	if (error) {
456 		DPRINTF(("usbd_bulk_transfer: tsleep=%d\n", error));
457 		usbd_abort_pipe(pipe);
458 		return (USBD_INTERRUPTED);
459 	}
460 	usbd_get_xfer_status(xfer, NULL, NULL, size, &err);
461 	DPRINTFN(1,("usbd_bulk_transfer: transferred %d\n", *size));
462 	if (err) {
463 		DPRINTF(("usbd_bulk_transfer: error=%d\n", err));
464 		usbd_clear_endpoint_stall(pipe);
465 	}
466 	return (err);
467 }
468 
469 void
470 usb_detach_wait(device_ptr_t dv)
471 {
472 	DPRINTF(("usb_detach_wait: waiting for %s\n", USBDEVPTRNAME(dv)));
473 	if (tsleep(dv, PZERO, "usbdet", hz * 60))
474 		printf("usb_detach_wait: %s didn't detach\n",
475 		        USBDEVPTRNAME(dv));
476 	DPRINTF(("usb_detach_wait: %s done\n", USBDEVPTRNAME(dv)));
477 }
478 
479 void
480 usb_detach_wakeup(device_ptr_t dv)
481 {
482 	DPRINTF(("usb_detach_wakeup: for %s\n", USBDEVPTRNAME(dv)));
483 	wakeup(dv);
484 }
485