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