xref: /openbsd-src/sys/dev/usb/usbdi_util.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: usbdi_util.c,v 1.10 2000/11/08 18:10:39 aaron Exp $ */
2 /*	$NetBSD: usbdi_util.c,v 1.33 2000/06/01 15:51:27 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  * 3. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgement:
23  *        This product includes software developed by the NetBSD
24  *        Foundation, Inc. and its contributors.
25  * 4. Neither the name of The NetBSD Foundation nor the names of its
26  *    contributors may be used to endorse or promote products derived
27  *    from this software without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
30  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
31  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
33  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39  * POSSIBILITY OF SUCH DAMAGE.
40  */
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/malloc.h>
46 #if defined(__NetBSD__) || defined(__OpenBSD__)
47 #include <sys/proc.h>
48 #include <sys/device.h>
49 #elif defined(__FreeBSD__)
50 #include <sys/bus.h>
51 #endif
52 
53 #include <dev/usb/usb.h>
54 #include <dev/usb/usbhid.h>
55 
56 #include <dev/usb/usbdi.h>
57 #include <dev/usb/usbdi_util.h>
58 
59 #ifdef USB_DEBUG
60 #define DPRINTF(x)	if (usbdebug) logprintf x
61 #define DPRINTFN(n,x)	if (usbdebug>(n)) logprintf x
62 extern int usbdebug;
63 #else
64 #define DPRINTF(x)
65 #define DPRINTFN(n,x)
66 #endif
67 
68 usbd_status
69 usbd_get_desc(usbd_device_handle dev, int type, int index, int len, void *desc)
70 {
71 	usb_device_request_t req;
72 
73 	DPRINTFN(3,("usbd_get_desc: type=%d, index=%d, len=%d\n",
74 		    type, index, len));
75 
76 	req.bmRequestType = UT_READ_DEVICE;
77 	req.bRequest = UR_GET_DESCRIPTOR;
78 	USETW2(req.wValue, type, index);
79 	USETW(req.wIndex, 0);
80 	USETW(req.wLength, len);
81 	return (usbd_do_request(dev, &req, desc));
82 }
83 
84 usbd_status
85 usbd_get_config_desc(usbd_device_handle dev, int confidx,
86 		     usb_config_descriptor_t *d)
87 {
88 	usbd_status err;
89 
90 	DPRINTFN(3,("usbd_get_config_desc: confidx=%d\n", confidx));
91 	err = usbd_get_desc(dev, UDESC_CONFIG, confidx,
92 			    USB_CONFIG_DESCRIPTOR_SIZE, d);
93 	if (err)
94 		return (err);
95 	if (d->bDescriptorType != UDESC_CONFIG) {
96 		DPRINTFN(-1,("usbd_get_config_desc: confidx=%d, bad desc "
97 			     "len=%d type=%d\n",
98 			     confidx, d->bLength, d->bDescriptorType));
99 		return (USBD_INVAL);
100 	}
101 	return (USBD_NORMAL_COMPLETION);
102 }
103 
104 usbd_status
105 usbd_get_config_desc_full(usbd_device_handle dev, int conf, void *d, int size)
106 {
107 	DPRINTFN(3,("usbd_get_config_desc_full: conf=%d\n", conf));
108 	return (usbd_get_desc(dev, UDESC_CONFIG, conf, size, d));
109 }
110 
111 usbd_status
112 usbd_get_device_desc(usbd_device_handle dev, usb_device_descriptor_t *d)
113 {
114 	DPRINTFN(3,("usbd_get_device_desc:\n"));
115 	return (usbd_get_desc(dev, UDESC_DEVICE,
116 			     0, USB_DEVICE_DESCRIPTOR_SIZE, d));
117 }
118 
119 usbd_status
120 usbd_get_device_status(usbd_device_handle dev, usb_status_t *st)
121 {
122 	usb_device_request_t req;
123 
124 	req.bmRequestType = UT_READ_DEVICE;
125 	req.bRequest = UR_GET_STATUS;
126 	USETW(req.wValue, 0);
127 	USETW(req.wIndex, 0);
128 	USETW(req.wLength, sizeof(usb_status_t));
129 	return (usbd_do_request(dev, &req, st));
130 }
131 
132 usbd_status
133 usbd_get_hub_status(usbd_device_handle dev, usb_hub_status_t *st)
134 {
135 	usb_device_request_t req;
136 
137 	req.bmRequestType = UT_READ_CLASS_DEVICE;
138 	req.bRequest = UR_GET_STATUS;
139 	USETW(req.wValue, 0);
140 	USETW(req.wIndex, 0);
141 	USETW(req.wLength, sizeof(usb_hub_status_t));
142 	return (usbd_do_request(dev, &req, st));
143 }
144 
145 usbd_status
146 usbd_set_address(usbd_device_handle dev, int addr)
147 {
148 	usb_device_request_t req;
149 
150 	req.bmRequestType = UT_WRITE_DEVICE;
151 	req.bRequest = UR_SET_ADDRESS;
152 	USETW(req.wValue, addr);
153 	USETW(req.wIndex, 0);
154 	USETW(req.wLength, 0);
155 	return usbd_do_request(dev, &req, 0);
156 }
157 
158 usbd_status
159 usbd_get_port_status(usbd_device_handle dev, int port, usb_port_status_t *ps)
160 {
161 	usb_device_request_t req;
162 
163 	req.bmRequestType = UT_READ_CLASS_OTHER;
164 	req.bRequest = UR_GET_STATUS;
165 	USETW(req.wValue, 0);
166 	USETW(req.wIndex, port);
167 	USETW(req.wLength, sizeof *ps);
168 	return (usbd_do_request(dev, &req, ps));
169 }
170 
171 usbd_status
172 usbd_clear_hub_feature(usbd_device_handle dev, int sel)
173 {
174 	usb_device_request_t req;
175 
176 	req.bmRequestType = UT_WRITE_CLASS_DEVICE;
177 	req.bRequest = UR_CLEAR_FEATURE;
178 	USETW(req.wValue, sel);
179 	USETW(req.wIndex, 0);
180 	USETW(req.wLength, 0);
181 	return (usbd_do_request(dev, &req, 0));
182 }
183 
184 usbd_status
185 usbd_set_hub_feature(usbd_device_handle dev, int sel)
186 {
187 	usb_device_request_t req;
188 
189 	req.bmRequestType = UT_WRITE_CLASS_DEVICE;
190 	req.bRequest = UR_SET_FEATURE;
191 	USETW(req.wValue, sel);
192 	USETW(req.wIndex, 0);
193 	USETW(req.wLength, 0);
194 	return (usbd_do_request(dev, &req, 0));
195 }
196 
197 usbd_status
198 usbd_clear_port_feature(usbd_device_handle dev, int port, int sel)
199 {
200 	usb_device_request_t req;
201 
202 	req.bmRequestType = UT_WRITE_CLASS_OTHER;
203 	req.bRequest = UR_CLEAR_FEATURE;
204 	USETW(req.wValue, sel);
205 	USETW(req.wIndex, port);
206 	USETW(req.wLength, 0);
207 	return (usbd_do_request(dev, &req, 0));
208 }
209 
210 usbd_status
211 usbd_set_port_feature(usbd_device_handle dev, int port, int sel)
212 {
213 	usb_device_request_t req;
214 
215 	req.bmRequestType = UT_WRITE_CLASS_OTHER;
216 	req.bRequest = UR_SET_FEATURE;
217 	USETW(req.wValue, sel);
218 	USETW(req.wIndex, port);
219 	USETW(req.wLength, 0);
220 	return (usbd_do_request(dev, &req, 0));
221 }
222 
223 
224 usbd_status
225 usbd_set_protocol(usbd_interface_handle iface, int report)
226 {
227 	usb_interface_descriptor_t *id = usbd_get_interface_descriptor(iface);
228 	usbd_device_handle dev;
229 	usb_device_request_t req;
230 	usbd_status err;
231 
232 	DPRINTFN(4, ("usbd_set_protocol: iface=%p, report=%d, endpt=%d\n",
233 		     iface, report, id->bInterfaceNumber));
234 	if (id == NULL)
235 		return (USBD_IOERROR);
236 	err = usbd_interface2device_handle(iface, &dev);
237 	if (err)
238 		return (err);
239 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
240 	req.bRequest = UR_SET_PROTOCOL;
241 	USETW(req.wValue, report);
242 	USETW(req.wIndex, id->bInterfaceNumber);
243 	USETW(req.wLength, 0);
244 	return (usbd_do_request(dev, &req, 0));
245 }
246 
247 usbd_status
248 usbd_set_report(usbd_interface_handle iface, int type, int id, void *data,
249 		int len)
250 {
251 	usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
252 	usbd_device_handle dev;
253 	usb_device_request_t req;
254 	usbd_status err;
255 
256 	DPRINTFN(4, ("usbd_set_report: len=%d\n", len));
257 	if (ifd == NULL)
258 		return (USBD_IOERROR);
259 	err = usbd_interface2device_handle(iface, &dev);
260 	if (err)
261 		return (err);
262 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
263 	req.bRequest = UR_SET_REPORT;
264 	USETW2(req.wValue, type, id);
265 	USETW(req.wIndex, ifd->bInterfaceNumber);
266 	USETW(req.wLength, len);
267 	return (usbd_do_request(dev, &req, data));
268 }
269 
270 usbd_status
271 usbd_set_report_async(usbd_interface_handle iface, int type, int id, void *data,
272 		      int len)
273 {
274 	usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
275 	usbd_device_handle dev;
276 	usb_device_request_t req;
277 	usbd_status err;
278 
279 	DPRINTFN(4, ("usbd_set_report_async: len=%d\n", len));
280 	if (ifd == NULL)
281 		return (USBD_IOERROR);
282 	err = usbd_interface2device_handle(iface, &dev);
283 	if (err)
284 		return (err);
285 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
286 	req.bRequest = UR_SET_REPORT;
287 	USETW2(req.wValue, type, id);
288 	USETW(req.wIndex, ifd->bInterfaceNumber);
289 	USETW(req.wLength, len);
290 	return (usbd_do_request_async(dev, &req, data));
291 }
292 
293 usbd_status
294 usbd_get_report(usbd_interface_handle iface, int type, int id, void *data,
295 		int len)
296 {
297 	usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
298 	usbd_device_handle dev;
299 	usb_device_request_t req;
300 	usbd_status err;
301 
302 	DPRINTFN(4, ("usbd_get_report: len=%d\n", len));
303 	if (ifd == NULL)
304 		return (USBD_IOERROR);
305 	err = usbd_interface2device_handle(iface, &dev);
306 	if (err)
307 		return (err);
308 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
309 	req.bRequest = UR_GET_REPORT;
310 	USETW2(req.wValue, type, id);
311 	USETW(req.wIndex, ifd->bInterfaceNumber);
312 	USETW(req.wLength, len);
313 	return (usbd_do_request(dev, &req, data));
314 }
315 
316 usbd_status
317 usbd_set_idle(usbd_interface_handle iface, int duration, int id)
318 {
319 	usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
320 	usbd_device_handle dev;
321 	usb_device_request_t req;
322 	usbd_status err;
323 
324 	DPRINTFN(4, ("usbd_set_idle: %d %d\n", duration, id));
325 	if (ifd == NULL)
326 		return (USBD_IOERROR);
327 	err = usbd_interface2device_handle(iface, &dev);
328 	if (err)
329 		return (err);
330 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
331 	req.bRequest = UR_SET_IDLE;
332 	USETW2(req.wValue, duration, id);
333 	USETW(req.wIndex, ifd->bInterfaceNumber);
334 	USETW(req.wLength, 0);
335 	return (usbd_do_request(dev, &req, 0));
336 }
337 
338 usbd_status
339 usbd_get_report_descriptor(usbd_device_handle dev, int ifcno, int repid,
340 			   int size, void *d)
341 {
342 	usb_device_request_t req;
343 
344 	req.bmRequestType = UT_READ_INTERFACE;
345 	req.bRequest = UR_GET_DESCRIPTOR;
346 	USETW2(req.wValue, UDESC_REPORT, repid);
347 	USETW(req.wIndex, ifcno);
348 	USETW(req.wLength, size);
349 	return (usbd_do_request(dev, &req, d));
350 }
351 
352 usb_hid_descriptor_t *
353 usbd_get_hid_descriptor(usbd_interface_handle ifc)
354 {
355 	usb_interface_descriptor_t *idesc = usbd_get_interface_descriptor(ifc);
356 	usbd_device_handle dev;
357 	usb_config_descriptor_t *cdesc;
358 	usb_hid_descriptor_t *hd;
359 	char *p, *end;
360 	usbd_status err;
361 
362 	if (idesc == NULL)
363 		return (0);
364 	err = usbd_interface2device_handle(ifc, &dev);
365 	if (err)
366 		return (0);
367 	cdesc = usbd_get_config_descriptor(dev);
368 
369 	p = (char *)idesc + idesc->bLength;
370 	end = (char *)cdesc + UGETW(cdesc->wTotalLength);
371 
372 	for (; p < end; p += hd->bLength) {
373 		hd = (usb_hid_descriptor_t *)p;
374 		if (p + hd->bLength <= end && hd->bDescriptorType == UDESC_HID)
375 			return (hd);
376 		if (hd->bDescriptorType == UDESC_INTERFACE)
377 			break;
378 	}
379 	return (0);
380 }
381 
382 usbd_status
383 usbd_alloc_report_desc(usbd_interface_handle ifc, void **descp, int *sizep,
384 		       usb_malloc_type mem)
385 {
386 	usb_interface_descriptor_t *id;
387 	usb_hid_descriptor_t *hid;
388 	usbd_device_handle dev;
389 	usbd_status err;
390 
391 	err = usbd_interface2device_handle(ifc, &dev);
392 	if (err)
393 		return (err);
394 	id = usbd_get_interface_descriptor(ifc);
395 	if (id == NULL)
396 		return (USBD_INVAL);
397 	hid = usbd_get_hid_descriptor(ifc);
398 	if (hid == NULL)
399 		return (USBD_IOERROR);
400 	*sizep = UGETW(hid->descrs[0].wDescriptorLength);
401 	*descp = malloc(*sizep, mem, M_NOWAIT);
402 	if (*descp == NULL)
403 		return (USBD_NOMEM);
404 	/* XXX should not use 0 Report ID */
405 	err = usbd_get_report_descriptor(dev, id->bInterfaceNumber, 0,
406 				       *sizep, *descp);
407 	if (err) {
408 		free(*descp, mem);
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