Home | History | Annotate | Line # | Download | only in usb
usbdi_util.c revision 1.61
      1 /*	$NetBSD: usbdi_util.c,v 1.61 2013/08/30 12:59:19 skrll Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1998, 2012 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 (lennart (at) augustsson.net) at
      9  * Carlstedt Research & Technology and Matthew R. Green (mrg (at) eterna.com.au).
     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  *
     20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  * POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: usbdi_util.c,v 1.61 2013/08/30 12:59:19 skrll Exp $");
     35 
     36 #include <sys/param.h>
     37 #include <sys/systm.h>
     38 #include <sys/kernel.h>
     39 #include <sys/malloc.h>
     40 #include <sys/proc.h>
     41 #include <sys/device.h>
     42 #include <sys/bus.h>
     43 
     44 #include <dev/usb/usb.h>
     45 #include <dev/usb/usbhid.h>
     46 
     47 #include <dev/usb/usbdi.h>
     48 #include <dev/usb/usbdivar.h>
     49 #include <dev/usb/usbdi_util.h>
     50 
     51 #ifdef USB_DEBUG
     52 #define DPRINTF(x)	if (usbdebug) printf x
     53 #define DPRINTFN(n,x)	if (usbdebug>(n)) printf x
     54 extern int usbdebug;
     55 #else
     56 #define DPRINTF(x)
     57 #define DPRINTFN(n,x)
     58 #endif
     59 
     60 usbd_status
     61 usbd_get_desc(usbd_device_handle dev, int type, int index, int len, void *desc)
     62 {
     63 	usb_device_request_t req;
     64 
     65 	DPRINTFN(3,("usbd_get_desc: type=%d, index=%d, len=%d\n",
     66 		    type, index, len));
     67 
     68 	req.bmRequestType = UT_READ_DEVICE;
     69 	req.bRequest = UR_GET_DESCRIPTOR;
     70 	USETW2(req.wValue, type, index);
     71 	USETW(req.wIndex, 0);
     72 	USETW(req.wLength, len);
     73 	return (usbd_do_request(dev, &req, desc));
     74 }
     75 
     76 usbd_status
     77 usbd_get_config_desc(usbd_device_handle dev, int confidx,
     78 		     usb_config_descriptor_t *d)
     79 {
     80 	usbd_status err;
     81 
     82 	DPRINTFN(3,("usbd_get_config_desc: confidx=%d\n", confidx));
     83 	err = usbd_get_desc(dev, UDESC_CONFIG, confidx,
     84 			    USB_CONFIG_DESCRIPTOR_SIZE, d);
     85 	if (err)
     86 		return (err);
     87 	if (d->bDescriptorType != UDESC_CONFIG) {
     88 		DPRINTFN(-1,("usbd_get_config_desc: confidx=%d, bad desc "
     89 			     "len=%d type=%d\n",
     90 			     confidx, d->bLength, d->bDescriptorType));
     91 		return (USBD_INVAL);
     92 	}
     93 	return (USBD_NORMAL_COMPLETION);
     94 }
     95 
     96 usbd_status
     97 usbd_get_config_desc_full(usbd_device_handle dev, int conf, void *d, int size)
     98 {
     99 	DPRINTFN(3,("usbd_get_config_desc_full: conf=%d\n", conf));
    100 	return (usbd_get_desc(dev, UDESC_CONFIG, conf, size, d));
    101 }
    102 
    103 usbd_status
    104 usbd_get_device_desc(usbd_device_handle dev, usb_device_descriptor_t *d)
    105 {
    106 	DPRINTFN(3,("usbd_get_device_desc:\n"));
    107 	return (usbd_get_desc(dev, UDESC_DEVICE,
    108 			     0, USB_DEVICE_DESCRIPTOR_SIZE, d));
    109 }
    110 
    111 usbd_status
    112 usbd_get_device_status(usbd_device_handle dev, usb_status_t *st)
    113 {
    114 	usb_device_request_t req;
    115 
    116 	req.bmRequestType = UT_READ_DEVICE;
    117 	req.bRequest = UR_GET_STATUS;
    118 	USETW(req.wValue, 0);
    119 	USETW(req.wIndex, 0);
    120 	USETW(req.wLength, sizeof(usb_status_t));
    121 	return (usbd_do_request(dev, &req, st));
    122 }
    123 
    124 usbd_status
    125 usbd_get_hub_status(usbd_device_handle dev, usb_hub_status_t *st)
    126 {
    127 	usb_device_request_t req;
    128 
    129 	req.bmRequestType = UT_READ_CLASS_DEVICE;
    130 	req.bRequest = UR_GET_STATUS;
    131 	USETW(req.wValue, 0);
    132 	USETW(req.wIndex, 0);
    133 	USETW(req.wLength, sizeof(usb_hub_status_t));
    134 	return (usbd_do_request(dev, &req, st));
    135 }
    136 
    137 usbd_status
    138 usbd_set_address(usbd_device_handle dev, int addr)
    139 {
    140 	usb_device_request_t req;
    141 
    142 	req.bmRequestType = UT_WRITE_DEVICE;
    143 	req.bRequest = UR_SET_ADDRESS;
    144 	USETW(req.wValue, addr);
    145 	USETW(req.wIndex, 0);
    146 	USETW(req.wLength, 0);
    147 	return usbd_do_request(dev, &req, 0);
    148 }
    149 
    150 usbd_status
    151 usbd_get_port_status(usbd_device_handle dev, int port, usb_port_status_t *ps)
    152 {
    153 	usb_device_request_t req;
    154 
    155 	req.bmRequestType = UT_READ_CLASS_OTHER;
    156 	req.bRequest = UR_GET_STATUS;
    157 	USETW(req.wValue, 0);
    158 	USETW(req.wIndex, port);
    159 	USETW(req.wLength, sizeof *ps);
    160 	return (usbd_do_request(dev, &req, ps));
    161 }
    162 
    163 usbd_status
    164 usbd_clear_hub_feature(usbd_device_handle dev, int sel)
    165 {
    166 	usb_device_request_t req;
    167 
    168 	req.bmRequestType = UT_WRITE_CLASS_DEVICE;
    169 	req.bRequest = UR_CLEAR_FEATURE;
    170 	USETW(req.wValue, sel);
    171 	USETW(req.wIndex, 0);
    172 	USETW(req.wLength, 0);
    173 	return (usbd_do_request(dev, &req, 0));
    174 }
    175 
    176 usbd_status
    177 usbd_set_hub_feature(usbd_device_handle dev, int sel)
    178 {
    179 	usb_device_request_t req;
    180 
    181 	req.bmRequestType = UT_WRITE_CLASS_DEVICE;
    182 	req.bRequest = UR_SET_FEATURE;
    183 	USETW(req.wValue, sel);
    184 	USETW(req.wIndex, 0);
    185 	USETW(req.wLength, 0);
    186 	return (usbd_do_request(dev, &req, 0));
    187 }
    188 
    189 usbd_status
    190 usbd_clear_port_feature(usbd_device_handle dev, int port, int sel)
    191 {
    192 	usb_device_request_t req;
    193 
    194 	req.bmRequestType = UT_WRITE_CLASS_OTHER;
    195 	req.bRequest = UR_CLEAR_FEATURE;
    196 	USETW(req.wValue, sel);
    197 	USETW(req.wIndex, port);
    198 	USETW(req.wLength, 0);
    199 	return (usbd_do_request(dev, &req, 0));
    200 }
    201 
    202 usbd_status
    203 usbd_set_port_feature(usbd_device_handle dev, int port, int sel)
    204 {
    205 	usb_device_request_t req;
    206 
    207 	req.bmRequestType = UT_WRITE_CLASS_OTHER;
    208 	req.bRequest = UR_SET_FEATURE;
    209 	USETW(req.wValue, sel);
    210 	USETW(req.wIndex, port);
    211 	USETW(req.wLength, 0);
    212 	return (usbd_do_request(dev, &req, 0));
    213 }
    214 
    215 usbd_status
    216 usbd_get_protocol(usbd_interface_handle iface, u_int8_t *report)
    217 {
    218 	usb_interface_descriptor_t *id = usbd_get_interface_descriptor(iface);
    219 	usbd_device_handle dev;
    220 	usb_device_request_t req;
    221 
    222 	DPRINTFN(4, ("usbd_get_protocol: iface=%p, endpt=%d\n",
    223 		     iface, id->bInterfaceNumber));
    224 	if (id == NULL)
    225 		return (USBD_IOERROR);
    226 	usbd_interface2device_handle(iface, &dev);
    227 	req.bmRequestType = UT_READ_CLASS_INTERFACE;
    228 	req.bRequest = UR_GET_PROTOCOL;
    229 	USETW(req.wValue, 0);
    230 	USETW(req.wIndex, id->bInterfaceNumber);
    231 	USETW(req.wLength, 1);
    232 	return (usbd_do_request(dev, &req, report));
    233 }
    234 
    235 usbd_status
    236 usbd_set_protocol(usbd_interface_handle iface, int report)
    237 {
    238 	usb_interface_descriptor_t *id = usbd_get_interface_descriptor(iface);
    239 	usbd_device_handle dev;
    240 	usb_device_request_t req;
    241 
    242 	DPRINTFN(4, ("usbd_set_protocol: iface=%p, report=%d, endpt=%d\n",
    243 		     iface, report, id->bInterfaceNumber));
    244 	if (id == NULL)
    245 		return (USBD_IOERROR);
    246 	usbd_interface2device_handle(iface, &dev);
    247 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    248 	req.bRequest = UR_SET_PROTOCOL;
    249 	USETW(req.wValue, report);
    250 	USETW(req.wIndex, id->bInterfaceNumber);
    251 	USETW(req.wLength, 0);
    252 	return (usbd_do_request(dev, &req, 0));
    253 }
    254 
    255 usbd_status
    256 usbd_set_report(usbd_interface_handle iface, int type, int id, void *data,
    257 		int len)
    258 {
    259 	usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
    260 	usbd_device_handle dev;
    261 	usb_device_request_t req;
    262 
    263 	DPRINTFN(4, ("usbd_set_report: len=%d\n", len));
    264 	if (ifd == NULL)
    265 		return (USBD_IOERROR);
    266 	usbd_interface2device_handle(iface, &dev);
    267 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    268 	req.bRequest = UR_SET_REPORT;
    269 	USETW2(req.wValue, type, id);
    270 	USETW(req.wIndex, ifd->bInterfaceNumber);
    271 	USETW(req.wLength, len);
    272 	return (usbd_do_request(dev, &req, data));
    273 }
    274 
    275 usbd_status
    276 usbd_set_report_async(usbd_interface_handle iface, int type, int id, void *data,
    277 		      int len)
    278 {
    279 	usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface);
    280 	usbd_device_handle dev;
    281 	usb_device_request_t req;
    282 
    283 	DPRINTFN(4, ("usbd_set_report_async: len=%d\n", len));
    284 	if (ifd == NULL)
    285 		return (USBD_IOERROR);
    286 	usbd_interface2device_handle(iface, &dev);
    287 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    288 	req.bRequest = UR_SET_REPORT;
    289 	USETW2(req.wValue, type, id);
    290 	USETW(req.wIndex, ifd->bInterfaceNumber);
    291 	USETW(req.wLength, len);
    292 	return (usbd_do_request_async(dev, &req, data));
    293 }
    294 
    295 usbd_status
    296 usbd_get_report(usbd_interface_handle iface, int type, int id, 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 
    303 	DPRINTFN(4, ("usbd_get_report: len=%d\n", len));
    304 	if (ifd == NULL)
    305 		return (USBD_IOERROR);
    306 	usbd_interface2device_handle(iface, &dev);
    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 
    322 	DPRINTFN(4, ("usbd_set_idle: %d %d\n", duration, id));
    323 	if (ifd == NULL)
    324 		return (USBD_IOERROR);
    325 	usbd_interface2device_handle(iface, &dev);
    326 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    327 	req.bRequest = UR_SET_IDLE;
    328 	USETW2(req.wValue, duration, id);
    329 	USETW(req.wIndex, ifd->bInterfaceNumber);
    330 	USETW(req.wLength, 0);
    331 	return (usbd_do_request(dev, &req, 0));
    332 }
    333 
    334 usbd_status
    335 usbd_get_report_descriptor(usbd_device_handle dev, int ifcno,
    336 			   int size, void *d)
    337 {
    338 	usb_device_request_t req;
    339 
    340 	req.bmRequestType = UT_READ_INTERFACE;
    341 	req.bRequest = UR_GET_DESCRIPTOR;
    342 	USETW2(req.wValue, UDESC_REPORT, 0); /* report id should be 0 */
    343 	USETW(req.wIndex, ifcno);
    344 	USETW(req.wLength, size);
    345 	return (usbd_do_request(dev, &req, d));
    346 }
    347 
    348 usb_hid_descriptor_t *
    349 usbd_get_hid_descriptor(usbd_interface_handle ifc)
    350 {
    351 	usb_interface_descriptor_t *idesc = usbd_get_interface_descriptor(ifc);
    352 	usbd_device_handle dev;
    353 	usb_config_descriptor_t *cdesc;
    354 	usb_hid_descriptor_t *hd;
    355 	char *p, *end;
    356 
    357 	if (idesc == NULL)
    358 		return (NULL);
    359 	usbd_interface2device_handle(ifc, &dev);
    360 	cdesc = usbd_get_config_descriptor(dev);
    361 
    362 	p = (char *)idesc + idesc->bLength;
    363 	end = (char *)cdesc + UGETW(cdesc->wTotalLength);
    364 
    365 	for (; p < end; p += hd->bLength) {
    366 		hd = (usb_hid_descriptor_t *)p;
    367 		if (p + hd->bLength <= end && hd->bDescriptorType == UDESC_HID)
    368 			return (hd);
    369 		if (hd->bDescriptorType == UDESC_INTERFACE)
    370 			break;
    371 	}
    372 	return (NULL);
    373 }
    374 
    375 usbd_status
    376 usbd_read_report_desc(usbd_interface_handle ifc, void **descp, int *sizep,
    377 		       struct malloc_type * mem)
    378 {
    379 	usb_interface_descriptor_t *id;
    380 	usb_hid_descriptor_t *hid;
    381 	usbd_device_handle dev;
    382 	usbd_status err;
    383 
    384 	usbd_interface2device_handle(ifc, &dev);
    385 	id = usbd_get_interface_descriptor(ifc);
    386 	if (id == NULL)
    387 		return (USBD_INVAL);
    388 	hid = usbd_get_hid_descriptor(ifc);
    389 	if (hid == NULL)
    390 		return (USBD_IOERROR);
    391 	*sizep = UGETW(hid->descrs[0].wDescriptorLength);
    392 	*descp = malloc(*sizep, mem, M_NOWAIT);
    393 	if (*descp == NULL)
    394 		return (USBD_NOMEM);
    395 	err = usbd_get_report_descriptor(dev, id->bInterfaceNumber,
    396 					 *sizep, *descp);
    397 	if (err) {
    398 		free(*descp, mem);
    399 		*descp = NULL;
    400 		return (err);
    401 	}
    402 	return (USBD_NORMAL_COMPLETION);
    403 }
    404 
    405 usbd_status
    406 usbd_get_config(usbd_device_handle dev, u_int8_t *conf)
    407 {
    408 	usb_device_request_t req;
    409 
    410 	req.bmRequestType = UT_READ_DEVICE;
    411 	req.bRequest = UR_GET_CONFIG;
    412 	USETW(req.wValue, 0);
    413 	USETW(req.wIndex, 0);
    414 	USETW(req.wLength, 1);
    415 	return (usbd_do_request(dev, &req, conf));
    416 }
    417 
    418 usbd_status
    419 usbd_bulk_transfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
    420 		   u_int16_t flags, u_int32_t timeout, void *buf,
    421 		   u_int32_t *size, const char *lbl)
    422 {
    423 	usbd_status err;
    424 
    425 	usbd_setup_xfer(xfer, pipe, 0, buf, *size, flags, timeout, NULL);
    426 
    427 	DPRINTFN(1, ("usbd_bulk_transfer: start transfer %d bytes\n", *size));
    428 	err = usbd_sync_transfer_sig(xfer);
    429 
    430 	usbd_get_xfer_status(xfer, NULL, NULL, size, NULL);
    431 	DPRINTFN(1,("usbd_bulk_transfer: transferred %d\n", *size));
    432 	if (err) {
    433 		DPRINTF(("usbd_bulk_transfer: error=%d\n", err));
    434 		usbd_clear_endpoint_stall(pipe);
    435 	}
    436 
    437 	return (err);
    438 }
    439 
    440 usbd_status
    441 usbd_intr_transfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe,
    442 		   u_int16_t flags, u_int32_t timeout, void *buf,
    443 		   u_int32_t *size, const char *lbl)
    444 {
    445 	usbd_status err;
    446 
    447 	usbd_setup_xfer(xfer, pipe, 0, buf, *size, flags, timeout, NULL);
    448 
    449 	DPRINTFN(1, ("usbd_intr_transfer: start transfer %d bytes\n", *size));
    450 	err = usbd_sync_transfer_sig(xfer);
    451 
    452 	usbd_get_xfer_status(xfer, NULL, NULL, size, NULL);
    453 
    454 	DPRINTFN(1,("usbd_intr_transfer: transferred %d\n", *size));
    455 	if (err) {
    456 		DPRINTF(("usbd_intr_transfer: error=%d\n", err));
    457 		usbd_clear_endpoint_stall(pipe);
    458 	}
    459 	return (err);
    460 }
    461 
    462 void
    463 usb_detach_wait(device_t dv, kcondvar_t *cv, kmutex_t *lock)
    464 {
    465 	DPRINTF(("usb_detach_wait: waiting for %s\n", device_xname(dv)));
    466 	if (cv_timedwait(cv, lock, hz * 60))	// dv, PZERO, "usbdet", hz * 60
    467 		printf("usb_detach_wait: %s didn't detach\n",
    468 		        device_xname(dv));
    469 	DPRINTF(("usb_detach_wait: %s done\n", device_xname(dv)));
    470 }
    471 
    472 void
    473 usb_detach_broadcast(device_t dv, kcondvar_t *cv)
    474 {
    475 	DPRINTF(("usb_detach_broadcast: for %s\n", device_xname(dv)));
    476 	cv_broadcast(cv);
    477 }
    478 
    479 void
    480 usb_detach_waitold(device_t dv)
    481 {
    482 	DPRINTF(("usb_detach_waitold: waiting for %s\n", device_xname(dv)));
    483 	if (tsleep(dv, PZERO, "usbdet", hz * 60)) /* XXXSMP ok */
    484 		printf("usb_detach_waitold: %s didn't detach\n",
    485 		        device_xname(dv));
    486 	DPRINTF(("usb_detach_waitold: %s done\n", device_xname(dv)));
    487 }
    488 
    489 void
    490 usb_detach_wakeupold(device_t dv)
    491 {
    492 	DPRINTF(("usb_detach_wakeupold: for %s\n", device_xname(dv)));
    493 	wakeup(dv); /* XXXSMP ok */
    494 }
    495 
    496 const usb_cdc_descriptor_t *
    497 usb_find_desc(usbd_device_handle dev, int type, int subtype)
    498 {
    499 	usbd_desc_iter_t iter;
    500 	const usb_cdc_descriptor_t *desc;
    501 
    502 	usb_desc_iter_init(dev, &iter);
    503 	for (;;) {
    504 		desc = (const usb_cdc_descriptor_t *)usb_desc_iter_next(&iter);
    505 		if (!desc || (desc->bDescriptorType == type &&
    506 			      (subtype == USBD_CDCSUBTYPE_ANY ||
    507 			       subtype == desc->bDescriptorSubtype)))
    508 			break;
    509 	}
    510 	return desc;
    511 }
    512 
    513 /* same as usb_find_desc(), but searches only in the specified interface. */
    514 const usb_cdc_descriptor_t *
    515 usb_find_desc_if(usbd_device_handle dev, int type, int subtype,
    516 		 usb_interface_descriptor_t *id)
    517 {
    518 	usbd_desc_iter_t iter;
    519 	const usb_cdc_descriptor_t *desc;
    520 
    521 	if (id == NULL)
    522 		return usb_find_desc(dev, type, subtype);
    523 
    524 	usb_desc_iter_init(dev, &iter);
    525 
    526 	iter.cur = (void *)id;		/* start from the interface desc */
    527 	usb_desc_iter_next(&iter);	/* and skip it */
    528 
    529 	while ((desc = (const usb_cdc_descriptor_t *)usb_desc_iter_next(&iter))
    530 	       != NULL) {
    531 		if (desc->bDescriptorType == UDESC_INTERFACE) {
    532 			/* we ran into the next interface --- not found */
    533 			return NULL;
    534 		}
    535 		if (desc->bDescriptorType == type &&
    536 		    (subtype == USBD_CDCSUBTYPE_ANY ||
    537 		     subtype == desc->bDescriptorSubtype))
    538 			break;
    539 	}
    540 	return desc;
    541 }
    542