1 /* $OpenBSD: usbdi_util.c,v 1.27 2013/03/28 03:58:03 tedu Exp $ */ 2 /* $NetBSD: usbdi_util.c,v 1.40 2002/07/11 21:14:36 augustss Exp $ */ 3 /* $FreeBSD: src/sys/dev/usb/usbdi_util.c,v 1.14 1999/11/17 22:33:50 n_hibma Exp $ */ 4 5 /* 6 * Copyright (c) 1998 The NetBSD Foundation, Inc. 7 * All rights reserved. 8 * 9 * This code is derived from software contributed to The NetBSD Foundation 10 * by Lennart Augustsson (lennart@augustsson.net) at 11 * Carlstedt Research & Technology. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 * POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/kernel.h> 38 #include <sys/malloc.h> 39 #include <sys/device.h> 40 41 #include <dev/usb/usb.h> 42 #include <dev/usb/usbhid.h> 43 44 #include <dev/usb/usbdi.h> 45 #include <dev/usb/usbdi_util.h> 46 47 #ifdef USB_DEBUG 48 #define DPRINTF(x) do { if (usbdebug) printf x; } while (0) 49 #define DPRINTFN(n,x) do { if (usbdebug>(n)) printf x; } while (0) 50 extern int usbdebug; 51 #else 52 #define DPRINTF(x) 53 #define DPRINTFN(n,x) 54 #endif 55 56 usbd_status 57 usbd_get_desc(usbd_device_handle dev, int type, int index, int len, void *desc) 58 { 59 usb_device_request_t req; 60 61 DPRINTFN(3,("usbd_get_desc: type=%d, index=%d, len=%d\n", type, index, 62 len)); 63 64 req.bmRequestType = UT_READ_DEVICE; 65 req.bRequest = UR_GET_DESCRIPTOR; 66 USETW2(req.wValue, type, index); 67 USETW(req.wIndex, 0); 68 USETW(req.wLength, len); 69 return (usbd_do_request(dev, &req, desc)); 70 } 71 72 usbd_status 73 usbd_get_config_desc(usbd_device_handle dev, int confidx, 74 usb_config_descriptor_t *d) 75 { 76 usbd_status err; 77 78 DPRINTFN(3,("usbd_get_config_desc: confidx=%d\n", confidx)); 79 err = usbd_get_desc(dev, UDESC_CONFIG, confidx, 80 USB_CONFIG_DESCRIPTOR_SIZE, d); 81 if (err) 82 return (err); 83 if (d->bDescriptorType != UDESC_CONFIG) { 84 DPRINTFN(-1,("usbd_get_config_desc: confidx=%d, bad desc " 85 "len=%d type=%d\n", confidx, d->bLength, 86 d->bDescriptorType)); 87 return (USBD_INVAL); 88 } 89 return (USBD_NORMAL_COMPLETION); 90 } 91 92 usbd_status 93 usbd_get_config_desc_full(usbd_device_handle dev, int conf, void *d, int size) 94 { 95 DPRINTFN(3,("usbd_get_config_desc_full: conf=%d\n", conf)); 96 return (usbd_get_desc(dev, UDESC_CONFIG, conf, size, d)); 97 } 98 99 usbd_status 100 usbd_get_device_desc(usbd_device_handle dev, usb_device_descriptor_t *d) 101 { 102 DPRINTFN(3,("usbd_get_device_desc:\n")); 103 return (usbd_get_desc(dev, UDESC_DEVICE, 0, USB_DEVICE_DESCRIPTOR_SIZE, 104 d)); 105 } 106 107 usbd_status 108 usbd_get_device_status(usbd_device_handle dev, usb_status_t *st) 109 { 110 usb_device_request_t req; 111 112 req.bmRequestType = UT_READ_DEVICE; 113 req.bRequest = UR_GET_STATUS; 114 USETW(req.wValue, 0); 115 USETW(req.wIndex, 0); 116 USETW(req.wLength, sizeof(usb_status_t)); 117 return (usbd_do_request(dev, &req, st)); 118 } 119 120 usbd_status 121 usbd_get_hub_status(usbd_device_handle dev, usb_hub_status_t *st) 122 { 123 usb_device_request_t req; 124 125 req.bmRequestType = UT_READ_CLASS_DEVICE; 126 req.bRequest = UR_GET_STATUS; 127 USETW(req.wValue, 0); 128 USETW(req.wIndex, 0); 129 USETW(req.wLength, sizeof(usb_hub_status_t)); 130 return (usbd_do_request(dev, &req, st)); 131 } 132 133 usbd_status 134 usbd_set_address(usbd_device_handle dev, int addr) 135 { 136 usb_device_request_t req; 137 138 req.bmRequestType = UT_WRITE_DEVICE; 139 req.bRequest = UR_SET_ADDRESS; 140 USETW(req.wValue, addr); 141 USETW(req.wIndex, 0); 142 USETW(req.wLength, 0); 143 return usbd_do_request(dev, &req, 0); 144 } 145 146 usbd_status 147 usbd_get_port_status(usbd_device_handle dev, int port, usb_port_status_t *ps) 148 { 149 usb_device_request_t req; 150 151 req.bmRequestType = UT_READ_CLASS_OTHER; 152 req.bRequest = UR_GET_STATUS; 153 USETW(req.wValue, 0); 154 USETW(req.wIndex, port); 155 USETW(req.wLength, sizeof *ps); 156 return (usbd_do_request(dev, &req, ps)); 157 } 158 159 usbd_status 160 usbd_clear_hub_feature(usbd_device_handle dev, int sel) 161 { 162 usb_device_request_t req; 163 164 req.bmRequestType = UT_WRITE_CLASS_DEVICE; 165 req.bRequest = UR_CLEAR_FEATURE; 166 USETW(req.wValue, sel); 167 USETW(req.wIndex, 0); 168 USETW(req.wLength, 0); 169 return (usbd_do_request(dev, &req, 0)); 170 } 171 172 usbd_status 173 usbd_set_hub_feature(usbd_device_handle dev, int sel) 174 { 175 usb_device_request_t req; 176 177 req.bmRequestType = UT_WRITE_CLASS_DEVICE; 178 req.bRequest = UR_SET_FEATURE; 179 USETW(req.wValue, sel); 180 USETW(req.wIndex, 0); 181 USETW(req.wLength, 0); 182 return (usbd_do_request(dev, &req, 0)); 183 } 184 185 usbd_status 186 usbd_clear_port_feature(usbd_device_handle dev, int port, int sel) 187 { 188 usb_device_request_t req; 189 190 req.bmRequestType = UT_WRITE_CLASS_OTHER; 191 req.bRequest = UR_CLEAR_FEATURE; 192 USETW(req.wValue, sel); 193 USETW(req.wIndex, port); 194 USETW(req.wLength, 0); 195 return (usbd_do_request(dev, &req, 0)); 196 } 197 198 usbd_status 199 usbd_set_port_feature(usbd_device_handle dev, int port, int sel) 200 { 201 usb_device_request_t req; 202 203 req.bmRequestType = UT_WRITE_CLASS_OTHER; 204 req.bRequest = UR_SET_FEATURE; 205 USETW(req.wValue, sel); 206 USETW(req.wIndex, port); 207 USETW(req.wLength, 0); 208 return (usbd_do_request(dev, &req, 0)); 209 } 210 211 usbd_status 212 usbd_get_protocol(usbd_interface_handle iface, u_int8_t *report) 213 { 214 usb_interface_descriptor_t *id = usbd_get_interface_descriptor(iface); 215 usbd_device_handle dev; 216 usb_device_request_t req; 217 218 DPRINTFN(4, ("usbd_get_protocol: iface=%p, endpt=%d\n", iface, 219 id->bInterfaceNumber)); 220 if (id == NULL) 221 return (USBD_IOERROR); 222 usbd_interface2device_handle(iface, &dev); 223 req.bmRequestType = UT_READ_CLASS_INTERFACE; 224 req.bRequest = UR_GET_PROTOCOL; 225 USETW(req.wValue, 0); 226 USETW(req.wIndex, id->bInterfaceNumber); 227 USETW(req.wLength, 1); 228 return (usbd_do_request(dev, &req, report)); 229 } 230 231 usbd_status 232 usbd_set_protocol(usbd_interface_handle iface, int report) 233 { 234 usb_interface_descriptor_t *id = usbd_get_interface_descriptor(iface); 235 usbd_device_handle dev; 236 usb_device_request_t req; 237 238 DPRINTFN(4, ("usbd_set_protocol: iface=%p, report=%d, endpt=%d\n", 239 iface, report, id->bInterfaceNumber)); 240 if (id == NULL) 241 return (USBD_IOERROR); 242 usbd_interface2device_handle(iface, &dev); 243 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 244 req.bRequest = UR_SET_PROTOCOL; 245 USETW(req.wValue, report); 246 USETW(req.wIndex, id->bInterfaceNumber); 247 USETW(req.wLength, 0); 248 return (usbd_do_request(dev, &req, 0)); 249 } 250 251 usbd_status 252 usbd_set_report(usbd_interface_handle iface, int type, int id, void *data, 253 int len) 254 { 255 usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface); 256 usbd_device_handle dev; 257 usb_device_request_t req; 258 259 DPRINTFN(4, ("usbd_set_report: len=%d\n", len)); 260 if (ifd == NULL) 261 return (USBD_IOERROR); 262 usbd_interface2device_handle(iface, &dev); 263 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 264 req.bRequest = UR_SET_REPORT; 265 USETW2(req.wValue, type, id); 266 USETW(req.wIndex, ifd->bInterfaceNumber); 267 USETW(req.wLength, len); 268 return (usbd_do_request(dev, &req, data)); 269 } 270 271 usbd_status 272 usbd_set_report_async(usbd_interface_handle iface, int type, int id, 273 void *data, int len) 274 { 275 usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface); 276 usbd_device_handle dev; 277 usb_device_request_t req; 278 279 DPRINTFN(4, ("usbd_set_report_async: len=%d\n", len)); 280 if (ifd == NULL) 281 return (USBD_IOERROR); 282 usbd_interface2device_handle(iface, &dev); 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(usbd_interface_handle iface, int type, int id, void *data, 293 int len) 294 { 295 usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface); 296 usbd_device_handle dev; 297 usb_device_request_t req; 298 299 DPRINTFN(4, ("usbd_get_report: len=%d\n", len)); 300 if (ifd == NULL) 301 return (USBD_IOERROR); 302 usbd_interface2device_handle(iface, &dev); 303 req.bmRequestType = UT_READ_CLASS_INTERFACE; 304 req.bRequest = UR_GET_REPORT; 305 USETW2(req.wValue, type, id); 306 USETW(req.wIndex, ifd->bInterfaceNumber); 307 USETW(req.wLength, len); 308 return (usbd_do_request(dev, &req, data)); 309 } 310 311 usbd_status 312 usbd_set_idle(usbd_interface_handle iface, int duration, int id) 313 { 314 usb_interface_descriptor_t *ifd = usbd_get_interface_descriptor(iface); 315 usbd_device_handle dev; 316 usb_device_request_t req; 317 318 DPRINTFN(4, ("usbd_set_idle: %d %d\n", duration, id)); 319 if (ifd == NULL) 320 return (USBD_IOERROR); 321 usbd_interface2device_handle(iface, &dev); 322 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 323 req.bRequest = UR_SET_IDLE; 324 USETW2(req.wValue, duration, id); 325 USETW(req.wIndex, ifd->bInterfaceNumber); 326 USETW(req.wLength, 0); 327 return (usbd_do_request(dev, &req, 0)); 328 } 329 330 usbd_status 331 usbd_get_report_descriptor(usbd_device_handle dev, int ifcno, int size, 332 void *d) 333 { 334 usb_device_request_t req; 335 336 req.bmRequestType = UT_READ_INTERFACE; 337 req.bRequest = UR_GET_DESCRIPTOR; 338 USETW2(req.wValue, UDESC_REPORT, 0); /* report id should be 0 */ 339 USETW(req.wIndex, ifcno); 340 USETW(req.wLength, size); 341 return (usbd_do_request(dev, &req, d)); 342 } 343 344 usb_hid_descriptor_t * 345 usbd_get_hid_descriptor(usbd_interface_handle ifc) 346 { 347 usb_interface_descriptor_t *idesc = usbd_get_interface_descriptor(ifc); 348 usbd_device_handle dev; 349 usb_config_descriptor_t *cdesc; 350 usb_hid_descriptor_t *hd; 351 char *p, *end; 352 353 if (idesc == NULL) 354 return (0); 355 usbd_interface2device_handle(ifc, &dev); 356 cdesc = usbd_get_config_descriptor(dev); 357 358 p = (char *)idesc + idesc->bLength; 359 end = (char *)cdesc + UGETW(cdesc->wTotalLength); 360 361 for (; p < end; p += hd->bLength) { 362 hd = (usb_hid_descriptor_t *)p; 363 if (p + hd->bLength <= end && hd->bDescriptorType == UDESC_HID) 364 return (hd); 365 if (hd->bDescriptorType == UDESC_INTERFACE) 366 break; 367 } 368 return (0); 369 } 370 371 usbd_status 372 usbd_read_report_desc(usbd_interface_handle ifc, void **descp, int *sizep, 373 int mem) 374 { 375 usb_interface_descriptor_t *id; 376 usb_hid_descriptor_t *hid; 377 usbd_device_handle dev; 378 usbd_status err; 379 380 usbd_interface2device_handle(ifc, &dev); 381 id = usbd_get_interface_descriptor(ifc); 382 if (id == NULL) 383 return (USBD_INVAL); 384 hid = usbd_get_hid_descriptor(ifc); 385 if (hid == NULL) 386 return (USBD_IOERROR); 387 *sizep = UGETW(hid->descrs[0].wDescriptorLength); 388 *descp = malloc(*sizep, mem, M_NOWAIT); 389 if (*descp == NULL) 390 return (USBD_NOMEM); 391 err = usbd_get_report_descriptor(dev, id->bInterfaceNumber, *sizep, 392 *descp); 393 if (err) { 394 free(*descp, mem); 395 *descp = NULL; 396 return (err); 397 } 398 return (USBD_NORMAL_COMPLETION); 399 } 400 401 usbd_status 402 usbd_get_config(usbd_device_handle dev, u_int8_t *conf) 403 { 404 usb_device_request_t req; 405 406 req.bmRequestType = UT_READ_DEVICE; 407 req.bRequest = UR_GET_CONFIG; 408 USETW(req.wValue, 0); 409 USETW(req.wIndex, 0); 410 USETW(req.wLength, 1); 411 return (usbd_do_request(dev, &req, conf)); 412 } 413 414 void usbd_bulk_transfer_cb(usbd_xfer_handle xfer, usbd_private_handle priv, 415 usbd_status status); 416 void 417 usbd_bulk_transfer_cb(usbd_xfer_handle xfer, usbd_private_handle priv, 418 usbd_status status) 419 { 420 wakeup(xfer); 421 } 422 423 usbd_status 424 usbd_bulk_transfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe, 425 u_int16_t flags, u_int32_t timeout, void *buf, u_int32_t *size, char *lbl) 426 { 427 usbd_status err; 428 int s, error, pri; 429 430 usbd_setup_xfer(xfer, pipe, 0, buf, *size, flags, timeout, 431 usbd_bulk_transfer_cb); 432 DPRINTFN(1, ("usbd_bulk_transfer: start transfer %d bytes\n", *size)); 433 s = splusb(); /* don't want callback until tsleep() */ 434 err = usbd_transfer(xfer); 435 if (err != USBD_IN_PROGRESS) { 436 splx(s); 437 return (err); 438 } 439 pri = timeout == 0 ? (PZERO | PCATCH) : PZERO; 440 error = tsleep((caddr_t)xfer, pri, lbl, 0); 441 splx(s); 442 if (error) { 443 DPRINTF(("usbd_bulk_transfer: tsleep=%d\n", error)); 444 usbd_abort_pipe(pipe); 445 return (USBD_INTERRUPTED); 446 } 447 usbd_get_xfer_status(xfer, NULL, NULL, size, &err); 448 DPRINTFN(1,("usbd_bulk_transfer: transferred %d\n", *size)); 449 if (err) { 450 DPRINTF(("usbd_bulk_transfer: error=%d\n", err)); 451 usbd_clear_endpoint_stall(pipe); 452 } 453 return (err); 454 } 455 456 void usbd_intr_transfer_cb(usbd_xfer_handle xfer, usbd_private_handle priv, 457 usbd_status status); 458 void 459 usbd_intr_transfer_cb(usbd_xfer_handle xfer, usbd_private_handle priv, 460 usbd_status status) 461 { 462 wakeup(xfer); 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, u_int32_t *size, char *lbl) 468 { 469 usbd_status err; 470 int s, error, pri; 471 472 usbd_setup_xfer(xfer, pipe, 0, buf, *size, flags, timeout, 473 usbd_intr_transfer_cb); 474 DPRINTFN(1, ("usbd_intr_transfer: start transfer %d bytes\n", *size)); 475 s = splusb(); /* don't want callback until tsleep() */ 476 err = usbd_transfer(xfer); 477 if (err != USBD_IN_PROGRESS) { 478 splx(s); 479 return (err); 480 } 481 pri = timeout == 0 ? (PZERO | PCATCH) : PZERO; 482 error = tsleep(xfer, pri, lbl, 0); 483 splx(s); 484 if (error) { 485 DPRINTF(("usbd_intr_transfer: tsleep=%d\n", error)); 486 usbd_abort_pipe(pipe); 487 return (USBD_INTERRUPTED); 488 } 489 usbd_get_xfer_status(xfer, NULL, NULL, size, &err); 490 DPRINTFN(1,("usbd_intr_transfer: transferred %d\n", *size)); 491 if (err) { 492 DPRINTF(("usbd_intr_transfer: error=%d\n", err)); 493 usbd_clear_endpoint_stall(pipe); 494 } 495 return (err); 496 } 497 498 void 499 usb_detach_wait(struct device *dv) 500 { 501 DPRINTF(("usb_detach_wait: waiting for %s\n", dv->dv_xname)); 502 if (tsleep(dv, PZERO, "usbdet", hz * 60)) 503 printf("usb_detach_wait: %s didn't detach\n", dv->dv_xname); 504 DPRINTF(("usb_detach_wait: %s done\n", dv->dv_xname)); 505 } 506 507 void 508 usb_detach_wakeup(struct device *dv) 509 { 510 DPRINTF(("usb_detach_wakeup: for %s\n", dv->dv_xname)); 511 wakeup(dv); 512 } 513 514 usb_descriptor_t * 515 usb_find_desc(usbd_device_handle dev, int type) 516 { 517 usb_descriptor_t *desc; 518 usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev); 519 uByte *p = (uByte *)cd; 520 uByte *end = p + UGETW(cd->wTotalLength); 521 522 while (p < end) { 523 desc = (usb_descriptor_t *)p; 524 if (desc->bDescriptorType == type) 525 return (desc); 526 p += desc->bLength; 527 } 528 529 return (NULL); 530 } 531