Home | History | Annotate | Line # | Download | only in usb
usbdi_util.c revision 1.7
      1 /*	$NetBSD: usbdi_util.c,v 1.7 1998/12/08 15:18:45 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 	r = usbd_interface2device_handle(iface, &dev);
    219 	if (r != USBD_NORMAL_COMPLETION)
    220 		return (r);
    221 	if (!id)
    222 		return (USBD_INVAL);
    223 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    224 	req.bRequest = UR_SET_PROTOCOL;
    225 	USETW(req.wValue, report);
    226 	USETW(req.wIndex, id->bInterfaceNumber);
    227 	USETW(req.wLength, 0);
    228 	return (usbd_do_request(dev, &req, 0));
    229 }
    230 
    231 usbd_status
    232 usbd_set_report(iface, type, id, data, len)
    233 	usbd_interface_handle iface;
    234 	int type;
    235 	int id;
    236 	void *data;
    237 	int len;
    238 {
    239 	usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
    240 	usbd_device_handle dev;
    241 	usb_device_request_t req;
    242 	usbd_status r;
    243 
    244 	DPRINTFN(4, ("usbd_set_report: len=%d\n", len));
    245 	r = usbd_interface2device_handle(iface, &dev);
    246 	if (r != USBD_NORMAL_COMPLETION)
    247 		return (r);
    248 	if (!ifd)
    249 		return (USBD_INVAL);
    250 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    251 	req.bRequest = UR_SET_REPORT;
    252 	USETW2(req.wValue, type, id);
    253 	USETW(req.wIndex, ifd->bInterfaceNumber);
    254 	USETW(req.wLength, len);
    255 	return (usbd_do_request(dev, &req, data));
    256 }
    257 
    258 usbd_status
    259 usbd_set_report_async(iface, type, id, data, len)
    260 	usbd_interface_handle iface;
    261 	int type;
    262 	int id;
    263 	void *data;
    264 	int len;
    265 {
    266 	usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
    267 	usbd_device_handle dev;
    268 	usb_device_request_t req;
    269 	usbd_status r;
    270 
    271 	DPRINTFN(4, ("usbd_set_report_async: len=%d\n", len));
    272 	r = usbd_interface2device_handle(iface, &dev);
    273 	if (r != USBD_NORMAL_COMPLETION)
    274 		return (r);
    275 	if (!ifd)
    276 		return (USBD_INVAL);
    277 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    278 	req.bRequest = UR_SET_REPORT;
    279 	USETW2(req.wValue, type, id);
    280 	USETW(req.wIndex, ifd->bInterfaceNumber);
    281 	USETW(req.wLength, len);
    282 	return (usbd_do_request_async(dev, &req, data));
    283 }
    284 
    285 usbd_status
    286 usbd_get_report(iface, type, id, data, len)
    287 	usbd_interface_handle iface;
    288 	int type;
    289 	int id;
    290 	void *data;
    291 	int len;
    292 {
    293 	usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
    294 	usbd_device_handle dev;
    295 	usb_device_request_t req;
    296 	usbd_status r;
    297 
    298 	DPRINTFN(4, ("usbd_set_report: len=%d\n", len));
    299 	r = usbd_interface2device_handle(iface, &dev);
    300 	if (r != USBD_NORMAL_COMPLETION)
    301 		return (r);
    302 	if (!ifd)
    303 		return (USBD_INVAL);
    304 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
    305 	req.bRequest = UR_GET_REPORT;
    306 	USETW2(req.wValue, type, id);
    307 	USETW(req.wIndex, ifd->bInterfaceNumber);
    308 	USETW(req.wLength, len);
    309 	return (usbd_do_request(dev, &req, data));
    310 }
    311 
    312 usbd_status
    313 usbd_set_idle(iface, duration, id)
    314 	usbd_interface_handle iface;
    315 	int duration;
    316 	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 r;
    322 
    323 	DPRINTFN(4, ("usbd_set_idle: %d %d\n", duration, id));
    324 	r = usbd_interface2device_handle(iface, &dev);
    325 	if (r != USBD_NORMAL_COMPLETION)
    326 		return (r);
    327 	if (!ifd)
    328 		return (USBD_INVAL);
    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(dev, ifcno, repid, size, d)
    339 	usbd_device_handle dev;
    340 	int ifcno;
    341 	int repid;
    342 	int size;
    343 	void *d;
    344 {
    345 	usb_device_request_t req;
    346 
    347 	req.bmRequestType = UT_READ_INTERFACE;
    348 	req.bRequest = UR_GET_DESCRIPTOR;
    349 	USETW2(req.wValue, UDESC_REPORT, repid);
    350 	USETW(req.wIndex, ifcno);
    351 	USETW(req.wLength, size);
    352 	return (usbd_do_request(dev, &req, d));
    353 }
    354 
    355 usb_hid_descriptor_t *
    356 usbd_get_hid_descriptor(ifc)
    357 	usbd_interface_handle ifc;
    358 {
    359 	usb_interface_descriptor_t *idesc = usbd_get_interface_descriptor(ifc);
    360 	usbd_device_handle dev;
    361 	usb_config_descriptor_t *cdesc;
    362 	usb_hid_descriptor_t *hd;
    363 	char *p, *end;
    364 	usbd_status r;
    365 
    366 	r = usbd_interface2device_handle(ifc, &dev);
    367 	if (r != USBD_NORMAL_COMPLETION)
    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_alloc_report_desc(ifc, descp, sizep, mem)
    386 	usbd_interface_handle ifc;
    387 	void **descp;
    388 	int *sizep;
    389 	int mem;
    390 {
    391 	usb_interface_descriptor_t *id;
    392 	usb_hid_descriptor_t *hid;
    393 	usbd_device_handle dev;
    394 	usbd_status r;
    395 
    396 	r = usbd_interface2device_handle(ifc, &dev);
    397 	if (r != USBD_NORMAL_COMPLETION)
    398 		return (r);
    399 	id = usbd_get_interface_descriptor(ifc);
    400 	if (!id)
    401 		return (USBD_INVAL);
    402 	hid = usbd_get_hid_descriptor(ifc);
    403 	if (!hid)
    404 		return (USBD_IOERROR);
    405 	*sizep = UGETW(hid->descrs[0].wDescriptorLength);
    406 	*descp = malloc(*sizep, mem, M_NOWAIT);
    407 	if (!*descp)
    408 		return (USBD_NOMEM);
    409 	/* XXX should not use 0 Report ID */
    410 	r = usbd_get_report_descriptor(dev, id->bInterfaceNumber, 0,
    411 				       *sizep, *descp);
    412 	if (r != USBD_NORMAL_COMPLETION) {
    413 		free(*descp, mem);
    414 		return (r);
    415 	}
    416 	return (USBD_NORMAL_COMPLETION);
    417 }
    418 
    419 usbd_status
    420 usbd_get_config(dev, conf)
    421 	usbd_device_handle dev;
    422 	u_int8_t *conf;
    423 {
    424 	usb_device_request_t req;
    425 
    426 	req.bmRequestType = UT_READ_DEVICE;
    427 	req.bRequest = UR_GET_CONFIG;
    428 	USETW(req.wValue, 0);
    429 	USETW(req.wIndex, 0);
    430 	USETW(req.wLength, 1);
    431 	return (usbd_do_request(dev, &req, conf));
    432 }
    433