1 /* $NetBSD: usbdi_util.c,v 1.52 2009/08/16 13:20:40 martin 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 (lennart@augustsson.net) 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 * 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.52 2009/08/16 13:20:40 martin 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 43 #include <dev/usb/usb.h> 44 #include <dev/usb/usbhid.h> 45 46 #include <dev/usb/usbdi.h> 47 #include <dev/usb/usbdi_util.h> 48 49 #ifdef USB_DEBUG 50 #define DPRINTF(x) if (usbdebug) logprintf x 51 #define DPRINTFN(n,x) if (usbdebug>(n)) logprintf x 52 extern int usbdebug; 53 #else 54 #define DPRINTF(x) 55 #define DPRINTFN(n,x) 56 #endif 57 58 usbd_status 59 usbd_get_desc(usbd_device_handle dev, int type, int index, int len, void *desc) 60 { 61 usb_device_request_t req; 62 63 DPRINTFN(3,("usbd_get_desc: type=%d, index=%d, len=%d\n", 64 type, index, len)); 65 66 req.bmRequestType = UT_READ_DEVICE; 67 req.bRequest = UR_GET_DESCRIPTOR; 68 USETW2(req.wValue, type, index); 69 USETW(req.wIndex, 0); 70 USETW(req.wLength, len); 71 return (usbd_do_request_flags(dev, &req, desc, 0, 0, 72 USBD_CONFIG_TIMEOUT)); 73 } 74 75 usbd_status 76 usbd_get_config_desc(usbd_device_handle dev, int confidx, 77 usb_config_descriptor_t *d) 78 { 79 usbd_status err; 80 81 DPRINTFN(3,("usbd_get_config_desc: confidx=%d\n", confidx)); 82 err = usbd_get_desc(dev, UDESC_CONFIG, confidx, 83 USB_CONFIG_DESCRIPTOR_SIZE, d); 84 if (err) 85 return (err); 86 if (d->bDescriptorType != UDESC_CONFIG) { 87 DPRINTFN(-1,("usbd_get_config_desc: confidx=%d, bad desc " 88 "len=%d type=%d\n", 89 confidx, d->bLength, d->bDescriptorType)); 90 return (USBD_INVAL); 91 } 92 return (USBD_NORMAL_COMPLETION); 93 } 94 95 usbd_status 96 usbd_get_config_desc_full(usbd_device_handle dev, int conf, void *d, int size) 97 { 98 DPRINTFN(3,("usbd_get_config_desc_full: conf=%d\n", conf)); 99 return (usbd_get_desc(dev, UDESC_CONFIG, conf, size, d)); 100 } 101 102 usbd_status 103 usbd_get_device_desc(usbd_device_handle dev, usb_device_descriptor_t *d) 104 { 105 DPRINTFN(3,("usbd_get_device_desc:\n")); 106 return (usbd_get_desc(dev, UDESC_DEVICE, 107 0, USB_DEVICE_DESCRIPTOR_SIZE, d)); 108 } 109 110 usbd_status 111 usbd_get_device_status(usbd_device_handle dev, usb_status_t *st) 112 { 113 usb_device_request_t req; 114 115 req.bmRequestType = UT_READ_DEVICE; 116 req.bRequest = UR_GET_STATUS; 117 USETW(req.wValue, 0); 118 USETW(req.wIndex, 0); 119 USETW(req.wLength, sizeof(usb_status_t)); 120 return (usbd_do_request(dev, &req, st)); 121 } 122 123 usbd_status 124 usbd_get_hub_status(usbd_device_handle dev, usb_hub_status_t *st) 125 { 126 usb_device_request_t req; 127 128 req.bmRequestType = UT_READ_CLASS_DEVICE; 129 req.bRequest = UR_GET_STATUS; 130 USETW(req.wValue, 0); 131 USETW(req.wIndex, 0); 132 USETW(req.wLength, sizeof(usb_hub_status_t)); 133 return (usbd_do_request(dev, &req, st)); 134 } 135 136 usbd_status 137 usbd_set_address(usbd_device_handle dev, int addr) 138 { 139 usb_device_request_t req; 140 141 req.bmRequestType = UT_WRITE_DEVICE; 142 req.bRequest = UR_SET_ADDRESS; 143 USETW(req.wValue, addr); 144 USETW(req.wIndex, 0); 145 USETW(req.wLength, 0); 146 return usbd_do_request(dev, &req, 0); 147 } 148 149 usbd_status 150 usbd_get_port_status(usbd_device_handle dev, int port, usb_port_status_t *ps) 151 { 152 usb_device_request_t req; 153 154 req.bmRequestType = UT_READ_CLASS_OTHER; 155 req.bRequest = UR_GET_STATUS; 156 USETW(req.wValue, 0); 157 USETW(req.wIndex, port); 158 USETW(req.wLength, sizeof *ps); 159 return (usbd_do_request(dev, &req, ps)); 160 } 161 162 usbd_status 163 usbd_clear_hub_feature(usbd_device_handle dev, int sel) 164 { 165 usb_device_request_t req; 166 167 req.bmRequestType = UT_WRITE_CLASS_DEVICE; 168 req.bRequest = UR_CLEAR_FEATURE; 169 USETW(req.wValue, sel); 170 USETW(req.wIndex, 0); 171 USETW(req.wLength, 0); 172 return (usbd_do_request(dev, &req, 0)); 173 } 174 175 usbd_status 176 usbd_set_hub_feature(usbd_device_handle dev, int sel) 177 { 178 usb_device_request_t req; 179 180 req.bmRequestType = UT_WRITE_CLASS_DEVICE; 181 req.bRequest = UR_SET_FEATURE; 182 USETW(req.wValue, sel); 183 USETW(req.wIndex, 0); 184 USETW(req.wLength, 0); 185 return (usbd_do_request(dev, &req, 0)); 186 } 187 188 usbd_status 189 usbd_clear_port_feature(usbd_device_handle dev, int port, int sel) 190 { 191 usb_device_request_t req; 192 193 req.bmRequestType = UT_WRITE_CLASS_OTHER; 194 req.bRequest = UR_CLEAR_FEATURE; 195 USETW(req.wValue, sel); 196 USETW(req.wIndex, port); 197 USETW(req.wLength, 0); 198 return (usbd_do_request(dev, &req, 0)); 199 } 200 201 usbd_status 202 usbd_set_port_feature(usbd_device_handle dev, int port, int sel) 203 { 204 usb_device_request_t req; 205 206 req.bmRequestType = UT_WRITE_CLASS_OTHER; 207 req.bRequest = UR_SET_FEATURE; 208 USETW(req.wValue, sel); 209 USETW(req.wIndex, port); 210 USETW(req.wLength, 0); 211 return (usbd_do_request(dev, &req, 0)); 212 } 213 214 usbd_status 215 usbd_get_protocol(usbd_interface_handle iface, u_int8_t *report) 216 { 217 usb_interface_descriptor_t *id = usbd_get_interface_descriptor(iface); 218 usbd_device_handle dev; 219 usb_device_request_t req; 220 221 DPRINTFN(4, ("usbd_get_protocol: iface=%p, endpt=%d\n", 222 iface, id->bInterfaceNumber)); 223 if (id == NULL) 224 return (USBD_IOERROR); 225 usbd_interface2device_handle(iface, &dev); 226 req.bmRequestType = UT_READ_CLASS_INTERFACE; 227 req.bRequest = UR_GET_PROTOCOL; 228 USETW(req.wValue, 0); 229 USETW(req.wIndex, id->bInterfaceNumber); 230 USETW(req.wLength, 1); 231 return (usbd_do_request(dev, &req, report)); 232 } 233 234 usbd_status 235 usbd_set_protocol(usbd_interface_handle iface, int report) 236 { 237 usb_interface_descriptor_t *id = usbd_get_interface_descriptor(iface); 238 usbd_device_handle dev; 239 usb_device_request_t req; 240 241 DPRINTFN(4, ("usbd_set_protocol: iface=%p, report=%d, endpt=%d\n", 242 iface, report, id->bInterfaceNumber)); 243 if (id == NULL) 244 return (USBD_IOERROR); 245 usbd_interface2device_handle(iface, &dev); 246 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 247 req.bRequest = UR_SET_PROTOCOL; 248 USETW(req.wValue, report); 249 USETW(req.wIndex, id->bInterfaceNumber); 250 USETW(req.wLength, 0); 251 return (usbd_do_request(dev, &req, 0)); 252 } 253 254 usbd_status 255 usbd_set_report(usbd_interface_handle iface, int type, int id, void *data, 256 int len) 257 { 258 usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface); 259 usbd_device_handle dev; 260 usb_device_request_t req; 261 262 DPRINTFN(4, ("usbd_set_report: len=%d\n", len)); 263 if (ifd == NULL) 264 return (USBD_IOERROR); 265 usbd_interface2device_handle(iface, &dev); 266 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 267 req.bRequest = UR_SET_REPORT; 268 USETW2(req.wValue, type, id); 269 USETW(req.wIndex, ifd->bInterfaceNumber); 270 USETW(req.wLength, len); 271 return (usbd_do_request(dev, &req, data)); 272 } 273 274 usbd_status 275 usbd_set_report_async(usbd_interface_handle iface, int type, int id, void *data, 276 int len) 277 { 278 usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface); 279 usbd_device_handle dev; 280 usb_device_request_t req; 281 282 DPRINTFN(4, ("usbd_set_report_async: len=%d\n", len)); 283 if (ifd == NULL) 284 return (USBD_IOERROR); 285 usbd_interface2device_handle(iface, &dev); 286 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 287 req.bRequest = UR_SET_REPORT; 288 USETW2(req.wValue, type, id); 289 USETW(req.wIndex, ifd->bInterfaceNumber); 290 USETW(req.wLength, len); 291 return (usbd_do_request_async(dev, &req, data)); 292 } 293 294 usbd_status 295 usbd_get_report(usbd_interface_handle iface, int type, int id, void *data, 296 int len) 297 { 298 usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface); 299 usbd_device_handle dev; 300 usb_device_request_t req; 301 302 DPRINTFN(4, ("usbd_get_report: len=%d\n", len)); 303 if (ifd == NULL) 304 return (USBD_IOERROR); 305 usbd_interface2device_handle(iface, &dev); 306 req.bmRequestType = UT_READ_CLASS_INTERFACE; 307 req.bRequest = UR_GET_REPORT; 308 USETW2(req.wValue, type, id); 309 USETW(req.wIndex, ifd->bInterfaceNumber); 310 USETW(req.wLength, len); 311 return (usbd_do_request(dev, &req, data)); 312 } 313 314 usbd_status 315 usbd_set_idle(usbd_interface_handle iface, int duration, int id) 316 { 317 usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface); 318 usbd_device_handle dev; 319 usb_device_request_t req; 320 321 DPRINTFN(4, ("usbd_set_idle: %d %d\n", duration, id)); 322 if (ifd == NULL) 323 return (USBD_IOERROR); 324 usbd_interface2device_handle(iface, &dev); 325 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 326 req.bRequest = UR_SET_IDLE; 327 USETW2(req.wValue, duration, id); 328 USETW(req.wIndex, ifd->bInterfaceNumber); 329 USETW(req.wLength, 0); 330 return (usbd_do_request(dev, &req, 0)); 331 } 332 333 usbd_status 334 usbd_get_report_descriptor(usbd_device_handle dev, int ifcno, 335 int size, void *d) 336 { 337 usb_device_request_t req; 338 339 req.bmRequestType = UT_READ_INTERFACE; 340 req.bRequest = UR_GET_DESCRIPTOR; 341 USETW2(req.wValue, UDESC_REPORT, 0); /* report id should be 0 */ 342 USETW(req.wIndex, ifcno); 343 USETW(req.wLength, size); 344 return (usbd_do_request(dev, &req, d)); 345 } 346 347 usb_hid_descriptor_t * 348 usbd_get_hid_descriptor(usbd_interface_handle ifc) 349 { 350 usb_interface_descriptor_t *idesc = usbd_get_interface_descriptor(ifc); 351 usbd_device_handle dev; 352 usb_config_descriptor_t *cdesc; 353 usb_hid_descriptor_t *hd; 354 char *p, *end; 355 356 if (idesc == NULL) 357 return (NULL); 358 usbd_interface2device_handle(ifc, &dev); 359 cdesc = usbd_get_config_descriptor(dev); 360 361 p = (char *)idesc + idesc->bLength; 362 end = (char *)cdesc + UGETW(cdesc->wTotalLength); 363 364 for (; p < end; p += hd->bLength) { 365 hd = (usb_hid_descriptor_t *)p; 366 if (p + hd->bLength <= end && hd->bDescriptorType == UDESC_HID) 367 return (hd); 368 if (hd->bDescriptorType == UDESC_INTERFACE) 369 break; 370 } 371 return (NULL); 372 } 373 374 usbd_status 375 usbd_read_report_desc(usbd_interface_handle ifc, void **descp, int *sizep, 376 usb_malloc_type mem) 377 { 378 usb_interface_descriptor_t *id; 379 usb_hid_descriptor_t *hid; 380 usbd_device_handle dev; 381 usbd_status err; 382 383 usbd_interface2device_handle(ifc, &dev); 384 id = usbd_get_interface_descriptor(ifc); 385 if (id == NULL) 386 return (USBD_INVAL); 387 hid = usbd_get_hid_descriptor(ifc); 388 if (hid == NULL) 389 return (USBD_IOERROR); 390 *sizep = UGETW(hid->descrs[0].wDescriptorLength); 391 *descp = malloc(*sizep, mem, M_NOWAIT); 392 if (*descp == NULL) 393 return (USBD_NOMEM); 394 err = usbd_get_report_descriptor(dev, id->bInterfaceNumber, 395 *sizep, *descp); 396 if (err) { 397 free(*descp, mem); 398 *descp = NULL; 399 return (err); 400 } 401 return (USBD_NORMAL_COMPLETION); 402 } 403 404 usbd_status 405 usbd_get_config(usbd_device_handle dev, u_int8_t *conf) 406 { 407 usb_device_request_t req; 408 409 req.bmRequestType = UT_READ_DEVICE; 410 req.bRequest = UR_GET_CONFIG; 411 USETW(req.wValue, 0); 412 USETW(req.wIndex, 0); 413 USETW(req.wLength, 1); 414 return (usbd_do_request(dev, &req, conf)); 415 } 416 417 Static void usbd_bulk_transfer_cb(usbd_xfer_handle xfer, 418 usbd_private_handle priv, usbd_status status); 419 Static void 420 usbd_bulk_transfer_cb(usbd_xfer_handle xfer, usbd_private_handle priv, 421 usbd_status status) 422 { 423 wakeup(xfer); 424 } 425 426 usbd_status 427 usbd_bulk_transfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe, 428 u_int16_t flags, u_int32_t timeout, void *buf, 429 u_int32_t *size, const char *lbl) 430 { 431 usbd_status err; 432 int s, error; 433 434 usbd_setup_xfer(xfer, pipe, 0, buf, *size, 435 flags, timeout, usbd_bulk_transfer_cb); 436 DPRINTFN(1, ("usbd_bulk_transfer: start transfer %d bytes\n", *size)); 437 s = splusb(); /* don't want callback until tsleep() */ 438 err = usbd_transfer(xfer); 439 if (err != USBD_IN_PROGRESS) { 440 splx(s); 441 return (err); 442 } 443 error = tsleep((void *)xfer, PZERO | PCATCH, lbl, 0); 444 splx(s); 445 if (error) { 446 DPRINTF(("usbd_bulk_transfer: tsleep=%d\n", error)); 447 usbd_abort_pipe(pipe); 448 return (USBD_INTERRUPTED); 449 } 450 usbd_get_xfer_status(xfer, NULL, NULL, size, &err); 451 DPRINTFN(1,("usbd_bulk_transfer: transferred %d\n", *size)); 452 if (err) { 453 DPRINTF(("usbd_bulk_transfer: error=%d\n", err)); 454 usbd_clear_endpoint_stall(pipe); 455 } 456 return (err); 457 } 458 459 Static void usbd_intr_transfer_cb(usbd_xfer_handle xfer, 460 usbd_private_handle priv, usbd_status status); 461 Static void 462 usbd_intr_transfer_cb(usbd_xfer_handle xfer, usbd_private_handle priv, 463 usbd_status status) 464 { 465 wakeup(xfer); 466 } 467 468 usbd_status 469 usbd_intr_transfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe, 470 u_int16_t flags, u_int32_t timeout, void *buf, 471 u_int32_t *size, const char *lbl) 472 { 473 usbd_status err; 474 int s, error; 475 476 usbd_setup_xfer(xfer, pipe, 0, buf, *size, 477 flags, timeout, usbd_intr_transfer_cb); 478 DPRINTFN(1, ("usbd_intr_transfer: start transfer %d bytes\n", *size)); 479 s = splusb(); /* don't want callback until tsleep() */ 480 err = usbd_transfer(xfer); 481 if (err != USBD_IN_PROGRESS) { 482 splx(s); 483 return (err); 484 } 485 error = tsleep(xfer, PZERO | PCATCH, lbl, 0); 486 splx(s); 487 if (error) { 488 DPRINTF(("usbd_intr_transfer: tsleep=%d\n", error)); 489 usbd_abort_pipe(pipe); 490 return (USBD_INTERRUPTED); 491 } 492 usbd_get_xfer_status(xfer, NULL, NULL, size, &err); 493 DPRINTFN(1,("usbd_intr_transfer: transferred %d\n", *size)); 494 if (err) { 495 DPRINTF(("usbd_intr_transfer: error=%d\n", err)); 496 usbd_clear_endpoint_stall(pipe); 497 } 498 return (err); 499 } 500 501 void 502 usb_detach_wait(device_ptr_t dv) 503 { 504 DPRINTF(("usb_detach_wait: waiting for %s\n", USBDEVPTRNAME(dv))); 505 if (tsleep(dv, PZERO, "usbdet", hz * 60)) 506 printf("usb_detach_wait: %s didn't detach\n", 507 USBDEVPTRNAME(dv)); 508 DPRINTF(("usb_detach_wait: %s done\n", USBDEVPTRNAME(dv))); 509 } 510 511 void 512 usb_detach_wakeup(device_ptr_t dv) 513 { 514 DPRINTF(("usb_detach_wakeup: for %s\n", USBDEVPTRNAME(dv))); 515 wakeup(dv); 516 } 517 518 const usb_cdc_descriptor_t * 519 usb_find_desc(usbd_device_handle dev, int type, int subtype) 520 { 521 usbd_desc_iter_t iter; 522 const usb_cdc_descriptor_t *desc; 523 524 usb_desc_iter_init(dev, &iter); 525 for (;;) { 526 desc = (const usb_cdc_descriptor_t *)usb_desc_iter_next(&iter); 527 if (!desc || (desc->bDescriptorType == type && 528 (subtype == USBD_CDCSUBTYPE_ANY || 529 subtype == desc->bDescriptorSubtype))) 530 break; 531 } 532 return desc; 533 } 534 535 /* same as usb_find_desc(), but searches only in the specified interface. */ 536 const usb_cdc_descriptor_t * 537 usb_find_desc_if(usbd_device_handle dev, int type, int subtype, 538 usb_interface_descriptor_t *id) 539 { 540 usbd_desc_iter_t iter; 541 const usb_cdc_descriptor_t *desc; 542 543 usb_desc_iter_init(dev, &iter); 544 545 iter.cur = (void *)id; /* start from the interface desc */ 546 usb_desc_iter_next(&iter); /* and skip it */ 547 548 while ((desc = (const usb_cdc_descriptor_t *)usb_desc_iter_next(&iter)) 549 != NULL) { 550 if (desc->bDescriptorType == UDESC_INTERFACE) { 551 /* we ran into the next interface --- not found */ 552 return NULL; 553 } 554 if (desc->bDescriptorType == type && 555 (subtype == USBD_CDCSUBTYPE_ANY || 556 subtype == desc->bDescriptorSubtype)) 557 break; 558 } 559 return desc; 560 } 561