1 /* $NetBSD: usbdi_util.c,v 1.59 2013/01/05 23:34:21 christos 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@augustsson.net) at 9 * Carlstedt Research & Technology and Matthew R. Green (mrg@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.59 2013/01/05 23:34:21 christos 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 Static void usbd_bulk_transfer_cb(usbd_xfer_handle xfer, 419 usbd_private_handle priv, usbd_status status); 420 Static void 421 usbd_bulk_transfer_cb(usbd_xfer_handle xfer, usbd_private_handle priv, 422 usbd_status status) 423 { 424 425 if (xfer->pipe->device->bus->lock) 426 cv_broadcast(&xfer->cv); 427 else 428 wakeup(xfer); /* XXXSMP ok */ 429 } 430 431 usbd_status 432 usbd_bulk_transfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe, 433 u_int16_t flags, u_int32_t timeout, void *buf, 434 u_int32_t *size, const char *lbl) 435 { 436 usbd_status err; 437 438 usbd_setup_xfer(xfer, pipe, 0, buf, *size, 439 flags, timeout, usbd_bulk_transfer_cb); 440 DPRINTFN(1, ("usbd_bulk_transfer: start transfer %d bytes\n", *size)); 441 442 err = usbd_sync_transfer_sig(xfer); 443 usbd_get_xfer_status(xfer, NULL, NULL, size, NULL); 444 DPRINTFN(1,("usbd_bulk_transfer: transferred %d\n", *size)); 445 if (err) { 446 DPRINTF(("usbd_bulk_transfer: error=%d\n", err)); 447 usbd_clear_endpoint_stall(pipe); 448 } 449 return (err); 450 } 451 452 Static void usbd_intr_transfer_cb(usbd_xfer_handle xfer, 453 usbd_private_handle priv, usbd_status status); 454 Static void 455 usbd_intr_transfer_cb(usbd_xfer_handle xfer, usbd_private_handle priv, 456 usbd_status status) 457 { 458 459 if (xfer->pipe->device->bus->lock) 460 cv_broadcast(&xfer->cv); 461 else 462 wakeup(xfer); /* XXXSMP ok */ 463 } 464 465 usbd_status 466 usbd_intr_transfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe, 467 u_int16_t flags, u_int32_t timeout, void *buf, 468 u_int32_t *size, const char *lbl) 469 { 470 usbd_status err; 471 472 usbd_setup_xfer(xfer, pipe, 0, buf, *size, 473 flags, timeout, usbd_intr_transfer_cb); 474 DPRINTFN(1, ("usbd_intr_transfer: start transfer %d bytes\n", *size)); 475 err = usbd_sync_transfer_sig(xfer); 476 usbd_get_xfer_status(xfer, NULL, NULL, size, NULL); 477 DPRINTFN(1,("usbd_intr_transfer: transferred %d\n", *size)); 478 if (err) { 479 DPRINTF(("usbd_intr_transfer: error=%d\n", err)); 480 usbd_clear_endpoint_stall(pipe); 481 } 482 return (err); 483 } 484 485 void 486 usb_detach_wait(device_t dv, kcondvar_t *cv, kmutex_t *lock) 487 { 488 DPRINTF(("usb_detach_wait: waiting for %s\n", device_xname(dv))); 489 if (cv_timedwait(cv, lock, hz * 60)) // dv, PZERO, "usbdet", hz * 60 490 printf("usb_detach_wait: %s didn't detach\n", 491 device_xname(dv)); 492 DPRINTF(("usb_detach_waitold: %s done\n", device_xname(dv))); 493 } 494 495 void 496 usb_detach_broadcast(device_t dv, kcondvar_t *cv) 497 { 498 DPRINTF(("usb_detach_broadcast: for %s\n", device_xname(dv))); 499 cv_broadcast(cv); 500 } 501 502 void 503 usb_detach_waitold(device_t dv) 504 { 505 DPRINTF(("usb_detach_waitold: waiting for %s\n", device_xname(dv))); 506 if (tsleep(dv, PZERO, "usbdet", hz * 60)) /* XXXSMP ok */ 507 printf("usb_detach_waitold: %s didn't detach\n", 508 device_xname(dv)); 509 DPRINTF(("usb_detach_waitold: %s done\n", device_xname(dv))); 510 } 511 512 void 513 usb_detach_wakeupold(device_t dv) 514 { 515 DPRINTF(("usb_detach_wakeupold: for %s\n", device_xname(dv))); 516 wakeup(dv); /* XXXSMP ok */ 517 } 518 519 const usb_cdc_descriptor_t * 520 usb_find_desc(usbd_device_handle dev, int type, int subtype) 521 { 522 usbd_desc_iter_t iter; 523 const usb_cdc_descriptor_t *desc; 524 525 usb_desc_iter_init(dev, &iter); 526 for (;;) { 527 desc = (const usb_cdc_descriptor_t *)usb_desc_iter_next(&iter); 528 if (!desc || (desc->bDescriptorType == type && 529 (subtype == USBD_CDCSUBTYPE_ANY || 530 subtype == desc->bDescriptorSubtype))) 531 break; 532 } 533 return desc; 534 } 535 536 /* same as usb_find_desc(), but searches only in the specified interface. */ 537 const usb_cdc_descriptor_t * 538 usb_find_desc_if(usbd_device_handle dev, int type, int subtype, 539 usb_interface_descriptor_t *id) 540 { 541 usbd_desc_iter_t iter; 542 const usb_cdc_descriptor_t *desc; 543 544 if (id == NULL) 545 return usb_find_desc(dev, type, subtype); 546 547 usb_desc_iter_init(dev, &iter); 548 549 iter.cur = (void *)id; /* start from the interface desc */ 550 usb_desc_iter_next(&iter); /* and skip it */ 551 552 while ((desc = (const usb_cdc_descriptor_t *)usb_desc_iter_next(&iter)) 553 != NULL) { 554 if (desc->bDescriptorType == UDESC_INTERFACE) { 555 /* we ran into the next interface --- not found */ 556 return NULL; 557 } 558 if (desc->bDescriptorType == type && 559 (subtype == USBD_CDCSUBTYPE_ANY || 560 subtype == desc->bDescriptorSubtype)) 561 break; 562 } 563 return desc; 564 } 565