Home | History | Annotate | Line # | Download | only in usb
usbdi_util.c revision 1.8
      1 /*	$NetBSD: usbdi_util.c,v 1.8 1998/12/09 00:18:12 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/device.h>
     45 #include <sys/proc.h>
     46 #include <sys/select.h>
     47 
     48 #include <dev/usb/usb.h>
     49 #include <dev/usb/usbhid.h>
     50 
     51 #include <dev/usb/usbdi.h>
     52 #include <dev/usb/usbdi_util.h>
     53 
     54 #include "opt_usbverbose.h"
     55 
     56 #ifdef USB_DEBUG
     57 #define DPRINTF(x)	if (usbdebug) printf x
     58 #define DPRINTFN(n,x)	if (usbdebug>(n)) printf x
     59 extern int usbdebug;
     60 #else
     61 #define DPRINTF(x)
     62 #define DPRINTFN(n,x)
     63 #endif
     64 
     65 usbd_status
     66 usbd_get_desc(dev, type, index, len, desc)
     67 	usbd_device_handle dev;
     68 	int type, index;
     69 	int len;
     70 	void *desc;
     71 {
     72 	usb_device_request_t req;
     73 
     74 	req.bmRequestType = UT_READ_DEVICE;
     75 	req.bRequest = UR_GET_DESCRIPTOR;
     76 	USETW2(req.wValue, type, index);
     77 	USETW(req.wIndex, 0);
     78 	USETW(req.wLength, len);
     79 	return (usbd_do_request(dev, &req, desc));
     80 }
     81 
     82 usbd_status
     83 usbd_get_config_desc(dev, conf, d)
     84 	usbd_device_handle dev;
     85 	int conf;
     86 	usb_config_descriptor_t *d;
     87 {
     88 	DPRINTFN(3,("usbd_get_config_desc: conf=%d\n", conf));
     89 	return (usbd_get_desc(dev, UDESC_CONFIG,
     90 			      conf, USB_CONFIG_DESCRIPTOR_SIZE, d));
     91 }
     92 
     93 usbd_status
     94 usbd_get_config_desc_full(dev, conf, d, size)
     95 	usbd_device_handle dev;
     96 	int conf;
     97 	void *d;
     98 	int size;
     99 {
    100 	DPRINTFN(3,("usbd_get_config_desc_full: conf=%d\n", conf));
    101 	return (usbd_get_desc(dev, UDESC_CONFIG, conf, size, d));
    102 }
    103 
    104 usbd_status
    105 usbd_get_device_desc(dev, d)
    106 	usbd_device_handle dev;
    107 	usb_device_descriptor_t *d;
    108 {
    109 	DPRINTFN(3,("usbd_get_device_desc:\n"));
    110 	return (usbd_get_desc(dev, UDESC_DEVICE,
    111 			     0, USB_DEVICE_DESCRIPTOR_SIZE, d));
    112 }
    113 
    114 usbd_status
    115 usbd_get_device_status(dev, st)
    116 	usbd_device_handle dev;
    117 	usb_status_t *st;
    118 {
    119 	usb_device_request_t req;
    120 
    121 	req.bmRequestType = UT_READ_DEVICE;
    122 	req.bRequest = UR_GET_STATUS;
    123 	USETW(req.wValue, 0);
    124 	USETW(req.wIndex, 0);
    125 	USETW(req.wLength, sizeof(usb_status_t));
    126 	return (usbd_do_request(dev, &req, st));
    127 }
    128 
    129 usbd_status
    130 usbd_get_hub_status(dev, st)
    131 	usbd_device_handle dev;
    132 	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(dev, addr)
    146 	usbd_device_handle dev;
    147 	int addr;
    148 {
    149 	usb_device_request_t req;
    150 
    151 	req.bmRequestType = UT_WRITE_DEVICE;
    152 	req.bRequest = UR_SET_ADDRESS;
    153 	USETW(req.wValue, addr);
    154 	USETW(req.wIndex, 0);
    155 	USETW(req.wLength, 0);
    156 	return usbd_do_request(dev, &req, 0);
    157 }
    158 
    159 usbd_status
    160 usbd_get_port_status(dev, port, ps)
    161 	usbd_device_handle dev;
    162 	int port;
    163 	usb_port_status_t *ps;
    164 {
    165 	usb_device_request_t req;
    166 
    167 	req.bmRequestType = UT_READ_CLASS_OTHER;
    168 	req.bRequest = UR_GET_STATUS;
    169 	USETW(req.wValue, 0);
    170 	USETW(req.wIndex, port);
    171 	USETW(req.wLength, sizeof *ps);
    172 	return (usbd_do_request(dev, &req, ps));
    173 }
    174 
    175 usbd_status
    176 usbd_clear_port_feature(dev, port, sel)
    177 	usbd_device_handle dev;
    178 	int port, sel;
    179 {
    180 	usb_device_request_t req;
    181 
    182 	req.bmRequestType = UT_WRITE_CLASS_OTHER;
    183 	req.bRequest = UR_CLEAR_FEATURE;
    184 	USETW(req.wValue, sel);
    185 	USETW(req.wIndex, port);
    186 	USETW(req.wLength, 0);
    187 	return (usbd_do_request(dev, &req, 0));
    188 }
    189 
    190 usbd_status
    191 usbd_set_port_feature(dev, port, sel)
    192 	usbd_device_handle dev;
    193 	int port, sel;
    194 {
    195 	usb_device_request_t req;
    196 
    197 	req.bmRequestType = UT_WRITE_CLASS_OTHER;
    198 	req.bRequest = UR_SET_FEATURE;
    199 	USETW(req.wValue, sel);
    200 	USETW(req.wIndex, port);
    201 	USETW(req.wLength, 0);
    202 	return (usbd_do_request(dev, &req, 0));
    203 }
    204 
    205 
    206 usbd_status
    207 usbd_set_protocol(iface, report)
    208 	usbd_interface_handle iface;
    209 	int report;
    210 {
    211 	usb_interface_descriptor_t *id = usbd_get_interface_descriptor(iface);
    212 	usbd_device_handle dev;
    213 	usb_device_request_t req;
    214 	usbd_status r;
    215 
    216 	DPRINTFN(4, ("usbd_set_protocol: iface=%p, report=%d, endpt=%d\n",
    217 		     iface, report, id->bInterfaceNumber));
    218 	if (!id)
    219 		return (USBD_IOERROR);
    220 	r = usbd_interface2device_handle(iface, &dev);
    221 	if (r != USBD_NORMAL_COMPLETION)
    222 		return (r);
    223 	if (!id)
    224 		return (USBD_INVAL);
    225 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    226 	req.bRequest = UR_SET_PROTOCOL;
    227 	USETW(req.wValue, report);
    228 	USETW(req.wIndex, id->bInterfaceNumber);
    229 	USETW(req.wLength, 0);
    230 	return (usbd_do_request(dev, &req, 0));
    231 }
    232 
    233 usbd_status
    234 usbd_set_report(iface, type, id, data, len)
    235 	usbd_interface_handle iface;
    236 	int type;
    237 	int id;
    238 	void *data;
    239 	int len;
    240 {
    241 	usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
    242 	usbd_device_handle dev;
    243 	usb_device_request_t req;
    244 	usbd_status r;
    245 
    246 	DPRINTFN(4, ("usbd_set_report: len=%d\n", len));
    247 	if (!ifd)
    248 		return (USBD_IOERROR);
    249 	r = usbd_interface2device_handle(iface, &dev);
    250 	if (r != USBD_NORMAL_COMPLETION)
    251 		return (r);
    252 	if (!ifd)
    253 		return (USBD_INVAL);
    254 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    255 	req.bRequest = UR_SET_REPORT;
    256 	USETW2(req.wValue, type, id);
    257 	USETW(req.wIndex, ifd->bInterfaceNumber);
    258 	USETW(req.wLength, len);
    259 	return (usbd_do_request(dev, &req, data));
    260 }
    261 
    262 usbd_status
    263 usbd_set_report_async(iface, type, id, data, len)
    264 	usbd_interface_handle iface;
    265 	int type;
    266 	int id;
    267 	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 	usbd_status r;
    274 
    275 	DPRINTFN(4, ("usbd_set_report_async: len=%d\n", len));
    276 	if (!ifd)
    277 		return (USBD_IOERROR);
    278 	r = usbd_interface2device_handle(iface, &dev);
    279 	if (r != USBD_NORMAL_COMPLETION)
    280 		return (r);
    281 	if (!ifd)
    282 		return (USBD_INVAL);
    283 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    284 	req.bRequest = UR_SET_REPORT;
    285 	USETW2(req.wValue, type, id);
    286 	USETW(req.wIndex, ifd->bInterfaceNumber);
    287 	USETW(req.wLength, len);
    288 	return (usbd_do_request_async(dev, &req, data));
    289 }
    290 
    291 usbd_status
    292 usbd_get_report(iface, type, id, data, len)
    293 	usbd_interface_handle iface;
    294 	int type;
    295 	int id;
    296 	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 r;
    303 
    304 	DPRINTFN(4, ("usbd_set_report: len=%d\n", len));
    305 	if (!id)
    306 		return (USBD_IOERROR);
    307 	r = usbd_interface2device_handle(iface, &dev);
    308 	if (r != USBD_NORMAL_COMPLETION)
    309 		return (r);
    310 	if (!ifd)
    311 		return (USBD_INVAL);
    312 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
    313 	req.bRequest = UR_GET_REPORT;
    314 	USETW2(req.wValue, type, id);
    315 	USETW(req.wIndex, ifd->bInterfaceNumber);
    316 	USETW(req.wLength, len);
    317 	return (usbd_do_request(dev, &req, data));
    318 }
    319 
    320 usbd_status
    321 usbd_set_idle(iface, duration, id)
    322 	usbd_interface_handle iface;
    323 	int duration;
    324 	int id;
    325 {
    326 	usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
    327 	usbd_device_handle dev;
    328 	usb_device_request_t req;
    329 	usbd_status r;
    330 
    331 	DPRINTFN(4, ("usbd_set_idle: %d %d\n", duration, id));
    332 	if (!ifd)
    333 		return (USBD_IOERROR);
    334 	r = usbd_interface2device_handle(iface, &dev);
    335 	if (r != USBD_NORMAL_COMPLETION)
    336 		return (r);
    337 	if (!ifd)
    338 		return (USBD_INVAL);
    339 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    340 	req.bRequest = UR_SET_IDLE;
    341 	USETW2(req.wValue, duration, id);
    342 	USETW(req.wIndex, ifd->bInterfaceNumber);
    343 	USETW(req.wLength, 0);
    344 	return (usbd_do_request(dev, &req, 0));
    345 }
    346 
    347 usbd_status
    348 usbd_get_report_descriptor(dev, ifcno, repid, size, d)
    349 	usbd_device_handle dev;
    350 	int ifcno;
    351 	int repid;
    352 	int size;
    353 	void *d;
    354 {
    355 	usb_device_request_t req;
    356 
    357 	req.bmRequestType = UT_READ_INTERFACE;
    358 	req.bRequest = UR_GET_DESCRIPTOR;
    359 	USETW2(req.wValue, UDESC_REPORT, repid);
    360 	USETW(req.wIndex, ifcno);
    361 	USETW(req.wLength, size);
    362 	return (usbd_do_request(dev, &req, d));
    363 }
    364 
    365 usb_hid_descriptor_t *
    366 usbd_get_hid_descriptor(ifc)
    367 	usbd_interface_handle ifc;
    368 {
    369 	usb_interface_descriptor_t *idesc = usbd_get_interface_descriptor(ifc);
    370 	usbd_device_handle dev;
    371 	usb_config_descriptor_t *cdesc;
    372 	usb_hid_descriptor_t *hd;
    373 	char *p, *end;
    374 	usbd_status r;
    375 
    376 	if (!idesc)
    377 		return (0);
    378 	r = usbd_interface2device_handle(ifc, &dev);
    379 	if (r != USBD_NORMAL_COMPLETION)
    380 		return (0);
    381 	cdesc = usbd_get_config_descriptor(dev);
    382 
    383 	p = (char *)idesc + idesc->bLength;
    384 	end = (char *)cdesc + UGETW(cdesc->wTotalLength);
    385 
    386 	for (; p < end; p += hd->bLength) {
    387 		hd = (usb_hid_descriptor_t *)p;
    388 		if (p + hd->bLength <= end && hd->bDescriptorType == UDESC_HID)
    389 			return (hd);
    390 		if (hd->bDescriptorType == UDESC_INTERFACE)
    391 			break;
    392 	}
    393 	return (0);
    394 }
    395 
    396 usbd_status
    397 usbd_alloc_report_desc(ifc, descp, sizep, mem)
    398 	usbd_interface_handle ifc;
    399 	void **descp;
    400 	int *sizep;
    401 	int mem;
    402 {
    403 	usb_interface_descriptor_t *id;
    404 	usb_hid_descriptor_t *hid;
    405 	usbd_device_handle dev;
    406 	usbd_status r;
    407 
    408 	r = usbd_interface2device_handle(ifc, &dev);
    409 	if (r != USBD_NORMAL_COMPLETION)
    410 		return (r);
    411 	id = usbd_get_interface_descriptor(ifc);
    412 	if (!id)
    413 		return (USBD_INVAL);
    414 	hid = usbd_get_hid_descriptor(ifc);
    415 	if (!hid)
    416 		return (USBD_IOERROR);
    417 	*sizep = UGETW(hid->descrs[0].wDescriptorLength);
    418 	*descp = malloc(*sizep, mem, M_NOWAIT);
    419 	if (!*descp)
    420 		return (USBD_NOMEM);
    421 	/* XXX should not use 0 Report ID */
    422 	r = usbd_get_report_descriptor(dev, id->bInterfaceNumber, 0,
    423 				       *sizep, *descp);
    424 	if (r != USBD_NORMAL_COMPLETION) {
    425 		free(*descp, mem);
    426 		return (r);
    427 	}
    428 	return (USBD_NORMAL_COMPLETION);
    429 }
    430 
    431 usbd_status
    432 usbd_get_config(dev, conf)
    433 	usbd_device_handle dev;
    434 	u_int8_t *conf;
    435 {
    436 	usb_device_request_t req;
    437 
    438 	req.bmRequestType = UT_READ_DEVICE;
    439 	req.bRequest = UR_GET_CONFIG;
    440 	USETW(req.wValue, 0);
    441 	USETW(req.wIndex, 0);
    442 	USETW(req.wLength, 1);
    443 	return (usbd_do_request(dev, &req, conf));
    444 }
    445