1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 /* 30 * Human Interface Device driver (HID) 31 * 32 * The HID driver is a software driver which acts as a class 33 * driver for USB human input devices like keyboard, mouse, 34 * joystick etc and provides the class-specific interfaces 35 * between these client driver modules and the Universal Serial 36 * Bus Driver(USBA). 37 * 38 * NOTE: This driver is not DDI compliant in that it uses undocumented 39 * functions for logging (USB_DPRINTF_L*, usb_alloc_log_hdl, usb_free_log_hdl). 40 * 41 * Undocumented functions may go away in a future Solaris OS release. 42 * 43 * Please see the DDK for sample code of these functions, and for the usbskel 44 * skeleton template driver which contains scaled-down versions of these 45 * functions written in a DDI-compliant way. 46 */ 47 48 #define USBDRV_MAJOR_VER 2 49 #define USBDRV_MINOR_VER 0 50 51 #include <sys/usb/usba.h> 52 #include <sys/usb/usba/genconsole.h> 53 #include <sys/usb/clients/hid/hid.h> 54 #include <sys/usb/clients/hid/hid_polled.h> 55 #include <sys/usb/clients/hidparser/hidparser.h> 56 #include <sys/usb/clients/hid/hidvar.h> 57 #include <sys/usb/clients/hid/hidminor.h> 58 #include <sys/usb/clients/hidparser/hid_parser_driver.h> 59 #include <sys/stropts.h> 60 61 extern int ddi_create_internal_pathname(dev_info_t *, char *, int, minor_t); 62 extern void consconfig_link(major_t major, minor_t minor); 63 extern int consconfig_unlink(major_t major, minor_t minor); 64 65 /* Debugging support */ 66 uint_t hid_errmask = (uint_t)PRINT_MASK_ALL; 67 uint_t hid_errlevel = USB_LOG_L4; 68 uint_t hid_instance_debug = (uint_t)-1; 69 70 /* tunables */ 71 int hid_default_pipe_drain_timeout = HID_DEFAULT_PIPE_DRAIN_TIMEOUT; 72 int hid_pm_mouse = 0; 73 74 /* soft state structures */ 75 #define HID_INITIAL_SOFT_SPACE 4 76 static void *hid_statep; 77 78 /* Callbacks */ 79 static void hid_interrupt_pipe_callback(usb_pipe_handle_t, 80 usb_intr_req_t *); 81 static void hid_default_pipe_callback(usb_pipe_handle_t, usb_ctrl_req_t *); 82 static void hid_interrupt_pipe_exception_callback(usb_pipe_handle_t, 83 usb_intr_req_t *); 84 static void hid_default_pipe_exception_callback(usb_pipe_handle_t, 85 usb_ctrl_req_t *); 86 static int hid_restore_state_event_callback(dev_info_t *); 87 static int hid_disconnect_event_callback(dev_info_t *); 88 static int hid_cpr_suspend(hid_state_t *hidp); 89 static void hid_cpr_resume(hid_state_t *hidp); 90 static void hid_power_change_callback(void *arg, int rval); 91 92 /* Supporting routines */ 93 static size_t hid_parse_hid_descr(usb_hid_descr_t *, size_t, 94 usb_alt_if_data_t *, usb_ep_data_t *); 95 static int hid_parse_hid_descr_failure(hid_state_t *); 96 static int hid_handle_report_descriptor(hid_state_t *, int); 97 static void hid_set_idle(hid_state_t *); 98 static void hid_set_protocol(hid_state_t *, int); 99 static void hid_detach_cleanup(dev_info_t *, hid_state_t *); 100 101 static int hid_start_intr_polling(hid_state_t *); 102 static void hid_close_intr_pipe(hid_state_t *); 103 static int hid_mctl_execute_cmd(hid_state_t *, int, hid_req_t *, mblk_t *); 104 static int hid_mctl_receive(queue_t *, mblk_t *); 105 static int hid_send_async_ctrl_request(hid_state_t *, hid_req_t *, 106 uchar_t, int, ushort_t, hid_default_pipe_arg_t *); 107 static void hid_ioctl(queue_t *, mblk_t *); 108 109 static void hid_create_pm_components(dev_info_t *, hid_state_t *); 110 static int hid_is_pm_enabled(dev_info_t *); 111 static void hid_restore_device_state(dev_info_t *, hid_state_t *); 112 static void hid_save_device_state(hid_state_t *); 113 114 static void hid_qreply_merror(queue_t *, mblk_t *, uchar_t); 115 static mblk_t *hid_data2mblk(uchar_t *, int); 116 static void hid_flush(queue_t *); 117 118 static int hid_pwrlvl0(hid_state_t *); 119 static int hid_pwrlvl1(hid_state_t *); 120 static int hid_pwrlvl2(hid_state_t *); 121 static int hid_pwrlvl3(hid_state_t *); 122 static void hid_pm_busy_component(hid_state_t *); 123 static void hid_pm_idle_component(hid_state_t *); 124 125 static int hid_polled_read(hid_polled_handle_t, uchar_t **); 126 static int hid_polled_input_enter(hid_polled_handle_t); 127 static int hid_polled_input_exit(hid_polled_handle_t); 128 static int hid_polled_input_init(hid_state_t *); 129 static int hid_polled_input_fini(hid_state_t *); 130 131 /* Streams entry points */ 132 static int hid_open(queue_t *, dev_t *, int, int, cred_t *); 133 static int hid_close(queue_t *, int, cred_t *); 134 static int hid_wput(queue_t *, mblk_t *); 135 static int hid_wsrv(queue_t *); 136 137 /* dev_ops entry points */ 138 static int hid_info(dev_info_t *, ddi_info_cmd_t, void *, void **); 139 static int hid_attach(dev_info_t *, ddi_attach_cmd_t); 140 static int hid_detach(dev_info_t *, ddi_detach_cmd_t); 141 static int hid_power(dev_info_t *, int, int); 142 143 /* 144 * Warlock is not aware of the automatic locking mechanisms for 145 * streams drivers. The hid streams enter points are protected by 146 * a per module perimeter. If the locking in hid is a bottleneck 147 * per queue pair or per queue locking may be used. Since warlock 148 * is not aware of the streams perimeters, these notes have been added. 149 * 150 * Note that the perimeters do not protect the driver from callbacks 151 * happening while a streams entry point is executing. So, the hid_mutex 152 * has been created to protect the data. 153 */ 154 _NOTE(SCHEME_PROTECTS_DATA("unique per call", iocblk)) 155 _NOTE(SCHEME_PROTECTS_DATA("unique per call", datab)) 156 _NOTE(SCHEME_PROTECTS_DATA("unique per call", msgb)) 157 _NOTE(SCHEME_PROTECTS_DATA("unique per call", queue)) 158 _NOTE(SCHEME_PROTECTS_DATA("unique per call", usb_ctrl_req)) 159 _NOTE(SCHEME_PROTECTS_DATA("unique per call", usb_intr_req)) 160 161 /* module information */ 162 static struct module_info hid_mod_info = { 163 0x0ffff, /* module id number */ 164 "hid", /* module name */ 165 0, /* min packet size accepted */ 166 INFPSZ, /* max packet size accepted */ 167 512, /* hi-water mark */ 168 128 /* lo-water mark */ 169 }; 170 171 /* read queue information structure */ 172 static struct qinit rinit = { 173 NULL, /* put procedure not needed */ 174 NULL, /* service procedure not needed */ 175 hid_open, /* called on startup */ 176 hid_close, /* called on finish */ 177 NULL, /* for future use */ 178 &hid_mod_info, /* module information structure */ 179 NULL /* module statistics structure */ 180 }; 181 182 /* write queue information structure */ 183 static struct qinit winit = { 184 hid_wput, /* put procedure */ 185 hid_wsrv, /* service procedure */ 186 NULL, /* open not used on write side */ 187 NULL, /* close not used on write side */ 188 NULL, /* for future use */ 189 &hid_mod_info, /* module information structure */ 190 NULL /* module statistics structure */ 191 }; 192 193 struct streamtab hid_streamtab = { 194 &rinit, 195 &winit, 196 NULL, /* not a MUX */ 197 NULL /* not a MUX */ 198 }; 199 200 struct cb_ops hid_cb_ops = { 201 nulldev, /* open */ 202 nulldev, /* close */ 203 nulldev, /* strategy */ 204 nulldev, /* print */ 205 nulldev, /* dump */ 206 nulldev, /* read */ 207 nulldev, /* write */ 208 nulldev, /* ioctl */ 209 nulldev, /* devmap */ 210 nulldev, /* mmap */ 211 nulldev, /* segmap */ 212 nochpoll, /* poll */ 213 ddi_prop_op, /* cb_prop_op */ 214 &hid_streamtab, /* streamtab */ 215 D_MP | D_MTPERQ 216 }; 217 218 219 static struct dev_ops hid_ops = { 220 DEVO_REV, /* devo_rev, */ 221 0, /* refcnt */ 222 hid_info, /* info */ 223 nulldev, /* identify */ 224 nulldev, /* probe */ 225 hid_attach, /* attach */ 226 hid_detach, /* detach */ 227 nodev, /* reset */ 228 &hid_cb_ops, /* driver operations */ 229 NULL, /* bus operations */ 230 hid_power /* power */ 231 }; 232 233 static struct modldrv hidmodldrv = { 234 &mod_driverops, 235 "USB HID Client Driver %I%", 236 &hid_ops /* driver ops */ 237 }; 238 239 static struct modlinkage modlinkage = { 240 MODREV_1, 241 &hidmodldrv, 242 NULL, 243 }; 244 245 static usb_event_t hid_events = { 246 hid_disconnect_event_callback, 247 hid_restore_state_event_callback, 248 NULL, 249 NULL, 250 }; 251 252 253 int 254 _init(void) 255 { 256 int rval; 257 258 if (((rval = ddi_soft_state_init(&hid_statep, sizeof (hid_state_t), 259 HID_INITIAL_SOFT_SPACE)) != 0)) { 260 261 return (rval); 262 } 263 264 if ((rval = mod_install(&modlinkage)) != 0) { 265 ddi_soft_state_fini(&hid_statep); 266 } 267 268 return (rval); 269 } 270 271 272 int 273 _fini(void) 274 { 275 int rval; 276 277 if ((rval = mod_remove(&modlinkage)) != 0) { 278 279 return (rval); 280 } 281 282 ddi_soft_state_fini(&hid_statep); 283 284 return (rval); 285 } 286 287 288 int 289 _info(struct modinfo *modinfop) 290 { 291 return (mod_info(&modlinkage, modinfop)); 292 } 293 294 295 /* 296 * hid_info : 297 * Get minor number, soft state structure etc. 298 */ 299 /*ARGSUSED*/ 300 static int 301 hid_info(dev_info_t *dip, ddi_info_cmd_t infocmd, 302 void *arg, void **result) 303 { 304 hid_state_t *hidp = NULL; 305 int error = DDI_FAILURE; 306 minor_t minor = getminor((dev_t)arg); 307 int instance = HID_MINOR_TO_INSTANCE(minor); 308 309 switch (infocmd) { 310 case DDI_INFO_DEVT2DEVINFO: 311 if ((hidp = ddi_get_soft_state(hid_statep, instance)) != NULL) { 312 *result = hidp->hid_dip; 313 if (*result != NULL) { 314 error = DDI_SUCCESS; 315 } 316 } else 317 *result = NULL; 318 break; 319 case DDI_INFO_DEVT2INSTANCE: 320 *result = (void *)(uintptr_t)instance; 321 error = DDI_SUCCESS; 322 break; 323 default: 324 break; 325 } 326 327 return (error); 328 } 329 330 331 /* 332 * hid_attach : 333 * Gets called at the time of attach. Do allocation, 334 * and initialization of the software structure. 335 * Get all the descriptors, setup the 336 * report descriptor tree by calling hidparser 337 * function. 338 */ 339 static int 340 hid_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 341 { 342 343 int instance = ddi_get_instance(dip); 344 int parse_hid_descr_error = 0; 345 hid_state_t *hidp = NULL; 346 uint32_t usage_page; 347 uint32_t usage; 348 usb_client_dev_data_t *dev_data; 349 usb_alt_if_data_t *altif_data; 350 char minor_name[HID_MINOR_NAME_LEN]; 351 usb_ep_data_t *ep_data; 352 353 switch (cmd) { 354 case DDI_ATTACH: 355 break; 356 case DDI_RESUME: 357 hidp = ddi_get_soft_state(hid_statep, instance); 358 hid_cpr_resume(hidp); 359 return (DDI_SUCCESS); 360 default: 361 362 return (DDI_FAILURE); 363 } 364 365 /* 366 * Allocate softstate information and get softstate pointer 367 */ 368 if (ddi_soft_state_zalloc(hid_statep, instance) == DDI_SUCCESS) { 369 hidp = ddi_get_soft_state(hid_statep, instance); 370 } 371 if (hidp == NULL) { 372 373 goto fail; 374 } 375 376 hidp->hid_log_handle = usb_alloc_log_hdl(dip, NULL, &hid_errlevel, 377 &hid_errmask, &hid_instance_debug, 0); 378 379 hidp->hid_instance = instance; 380 hidp->hid_dip = dip; 381 382 /* 383 * Register with USBA. Just retrieve interface descriptor 384 */ 385 if (usb_client_attach(dip, USBDRV_VERSION, 0) != USB_SUCCESS) { 386 USB_DPRINTF_L2(PRINT_MASK_ATTA, hidp->hid_log_handle, 387 "hid_attach: client attach failed"); 388 389 goto fail; 390 } 391 392 if (usb_get_dev_data(dip, &dev_data, USB_PARSE_LVL_IF, 0) != 393 USB_SUCCESS) { 394 395 USB_DPRINTF_L2(PRINT_MASK_ATTA, hidp->hid_log_handle, 396 "hid_attach: usb_get_dev_data() failed"); 397 398 goto fail; 399 } 400 401 /* initialize mutex */ 402 mutex_init(&hidp->hid_mutex, NULL, MUTEX_DRIVER, 403 dev_data->dev_iblock_cookie); 404 405 hidp->hid_attach_flags |= HID_LOCK_INIT; 406 407 /* get interface data for alternate 0 */ 408 altif_data = &dev_data->dev_curr_cfg-> 409 cfg_if[dev_data->dev_curr_if].if_alt[0]; 410 411 mutex_enter(&hidp->hid_mutex); 412 hidp->hid_dev_data = dev_data; 413 hidp->hid_dev_descr = dev_data->dev_descr; 414 hidp->hid_interfaceno = dev_data->dev_curr_if; 415 hidp->hid_if_descr = altif_data->altif_descr; 416 mutex_exit(&hidp->hid_mutex); 417 418 if ((ep_data = usb_lookup_ep_data(dip, dev_data, 419 hidp->hid_interfaceno, 0, 0, 420 (uint_t)USB_EP_ATTR_INTR, (uint_t)USB_EP_DIR_IN)) == NULL) { 421 422 USB_DPRINTF_L2(PRINT_MASK_ATTA, hidp->hid_log_handle, 423 "no interrupt IN endpoint found"); 424 425 goto fail; 426 } 427 428 mutex_enter(&hidp->hid_mutex); 429 hidp->hid_ep_intr_descr = ep_data->ep_descr; 430 431 /* 432 * Attempt to find the hid descriptor, it could be after interface 433 * or after endpoint descriptors 434 */ 435 if (hid_parse_hid_descr(&hidp->hid_hid_descr, USB_HID_DESCR_SIZE, 436 altif_data, ep_data) != USB_HID_DESCR_SIZE) { 437 /* 438 * If parsing of hid descriptor failed and 439 * the device is a keyboard or mouse, use predefined 440 * length and packet size. 441 */ 442 if (hid_parse_hid_descr_failure(hidp) == USB_FAILURE) { 443 mutex_exit(&hidp->hid_mutex); 444 445 goto fail; 446 } 447 448 /* 449 * hid descriptor was bad but since 450 * the device is a keyboard or mouse, 451 * we will use the default length 452 * and packet size. 453 */ 454 parse_hid_descr_error = HID_BAD_DESCR; 455 } else { 456 /* Parse hid descriptor successful */ 457 458 USB_DPRINTF_L3(PRINT_MASK_ATTA, hidp->hid_log_handle, 459 "Hid descriptor:\n\t" 460 "bLength = 0x%x bDescriptorType = 0x%x " 461 "bcdHID = 0x%x\n\t" 462 "bCountryCode = 0x%x bNumDescriptors = 0x%x\n\t" 463 "bReportDescriptorType = 0x%x\n\t" 464 "wReportDescriptorLength = 0x%x", 465 hidp->hid_hid_descr.bLength, 466 hidp->hid_hid_descr.bDescriptorType, 467 hidp->hid_hid_descr.bcdHID, 468 hidp->hid_hid_descr.bCountryCode, 469 hidp->hid_hid_descr.bNumDescriptors, 470 hidp->hid_hid_descr.bReportDescriptorType, 471 hidp->hid_hid_descr.wReportDescriptorLength); 472 } 473 474 /* 475 * Save a copy of the default pipe for easy reference 476 */ 477 hidp->hid_default_pipe = hidp->hid_dev_data->dev_default_ph; 478 479 /* we copied the descriptors we need, free the dev_data */ 480 usb_free_dev_data(dip, dev_data); 481 hidp->hid_dev_data = NULL; 482 483 /* 484 * Don't get the report descriptor if parsing hid descriptor earlier 485 * failed since device probably won't return valid report descriptor 486 * either. Though parsing of hid descriptor failed, we have reached 487 * this point because the device has been identified as a 488 * keyboard or a mouse successfully and the default packet 489 * size and layout(in case of keyboard only) will be used, so it 490 * is ok to go ahead even if parsing of hid descriptor failed and 491 * we will not try to get the report descriptor. 492 */ 493 if (parse_hid_descr_error != HID_BAD_DESCR) { 494 /* 495 * Sun mouse rev 105 is a bit slow in responding to this 496 * request and requires multiple retries 497 */ 498 int retry; 499 500 /* 501 * Get and parse the report descriptor. 502 * Set the packet size if parsing is successful. 503 * Note that we start retry at 1 to have a delay 504 * in the first iteration. 505 */ 506 mutex_exit(&hidp->hid_mutex); 507 for (retry = 1; retry < HID_RETRY; retry++) { 508 if (hid_handle_report_descriptor(hidp, 509 hidp->hid_interfaceno) == USB_SUCCESS) { 510 break; 511 } 512 delay(retry * drv_usectohz(1000)); 513 } 514 if (retry >= HID_RETRY) { 515 516 goto fail; 517 } 518 mutex_enter(&hidp->hid_mutex); 519 520 /* 521 * If packet size is zero, but the device is identified 522 * as a mouse or a keyboard, use predefined packet 523 * size. 524 */ 525 if (hidp->hid_packet_size == 0) { 526 if (hidp->hid_if_descr.bInterfaceProtocol == 527 KEYBOARD_PROTOCOL) { 528 /* device is a keyboard */ 529 hidp->hid_packet_size = USBKPSZ; 530 } else if (hidp-> 531 hid_if_descr.bInterfaceProtocol == 532 MOUSE_PROTOCOL) { 533 /* device is a mouse */ 534 hidp->hid_packet_size = USBMSSZ; 535 } else { 536 USB_DPRINTF_L2(PRINT_MASK_ATTA, 537 hidp->hid_log_handle, 538 "Failed to find hid packet size"); 539 mutex_exit(&hidp->hid_mutex); 540 541 goto fail; 542 } 543 } 544 } 545 546 /* 547 * initialize the pipe policy for the interrupt pipe. 548 */ 549 hidp->hid_intr_pipe_policy.pp_max_async_reqs = 1; 550 551 /* 552 * Make a clas specific request to SET_IDLE 553 * In this case send no reports if state has not changed. 554 * See HID 7.2.4. 555 */ 556 mutex_exit(&hidp->hid_mutex); 557 hid_set_idle(hidp); 558 559 /* always initialize to report protocol */ 560 hid_set_protocol(hidp, SET_REPORT_PROTOCOL); 561 mutex_enter(&hidp->hid_mutex); 562 563 /* 564 * Create minor node based on information from the 565 * descriptors 566 */ 567 switch (hidp->hid_if_descr.bInterfaceProtocol) { 568 case KEYBOARD_PROTOCOL: 569 (void) strcpy(minor_name, "keyboard"); 570 571 break; 572 case MOUSE_PROTOCOL: 573 (void) strcpy(minor_name, "mouse"); 574 575 break; 576 default: 577 if (hidparser_get_top_level_collection_usage( 578 hidp->hid_report_descr, &usage_page, &usage) != 579 HIDPARSER_FAILURE) { 580 switch (usage_page) { 581 case HID_CONSUMER: 582 switch (usage) { 583 case HID_CONSUMER_CONTROL: 584 (void) strcpy(minor_name, 585 "consumer_control"); 586 587 break; 588 default: 589 (void) sprintf(minor_name, 590 "hid_%d_%d", usage_page, usage); 591 592 break; 593 } 594 595 break; 596 case HID_GENERIC_DESKTOP: 597 switch (usage) { 598 case HID_GD_POINTER: 599 (void) strcpy(minor_name, 600 "pointer"); 601 602 break; 603 case HID_GD_MOUSE: 604 (void) strcpy(minor_name, 605 "mouse"); 606 607 break; 608 case HID_GD_KEYBOARD: 609 (void) strcpy(minor_name, 610 "keyboard"); 611 612 break; 613 default: 614 (void) sprintf(minor_name, 615 "hid_%d_%d", usage_page, usage); 616 617 break; 618 } 619 620 break; 621 default: 622 (void) sprintf(minor_name, 623 "hid_%d_%d", usage_page, usage); 624 625 break; 626 } 627 } else { 628 USB_DPRINTF_L1(PRINT_MASK_ATTA, hidp->hid_log_handle, 629 "hid_attach: Unsupported HID device"); 630 mutex_exit(&hidp->hid_mutex); 631 632 goto fail; 633 } 634 635 break; 636 } 637 638 mutex_exit(&hidp->hid_mutex); 639 640 if ((ddi_create_minor_node(dip, minor_name, S_IFCHR, 641 HID_CONSTRUCT_EXTERNAL_MINOR(instance), 642 DDI_PSEUDO, 0)) != DDI_SUCCESS) { 643 USB_DPRINTF_L1(PRINT_MASK_ATTA, hidp->hid_log_handle, 644 "hid_attach: Could not create minor node"); 645 646 goto fail; 647 } 648 649 hidp->hid_km = B_FALSE; 650 651 /* create internal path for virtual */ 652 if (strcmp(minor_name, "mouse") == 0) { 653 hidp->hid_km = B_TRUE; /* mouse */ 654 if (ddi_create_internal_pathname(dip, "internal_mouse", S_IFCHR, 655 HID_CONSTRUCT_INTERNAL_MINOR(instance)) != DDI_SUCCESS) { 656 657 goto fail; 658 } 659 } 660 661 if (strcmp(minor_name, "keyboard") == 0) { 662 hidp->hid_km = B_TRUE; /* keyboard */ 663 if (ddi_create_internal_pathname(dip, "internal_keyboard", 664 S_IFCHR, HID_CONSTRUCT_INTERNAL_MINOR(instance)) != 665 DDI_SUCCESS) { 666 667 goto fail; 668 } 669 } 670 671 mutex_enter(&hidp->hid_mutex); 672 hidp->hid_attach_flags |= HID_MINOR_NODES; 673 hidp->hid_dev_state = USB_DEV_ONLINE; 674 mutex_exit(&hidp->hid_mutex); 675 676 /* register for all events */ 677 if (usb_register_event_cbs(dip, &hid_events, 0) != USB_SUCCESS) { 678 USB_DPRINTF_L1(PRINT_MASK_ATTA, hidp->hid_log_handle, 679 "usb_register_event_cbs failed"); 680 681 goto fail; 682 } 683 684 /* now create components to power manage this device */ 685 hid_create_pm_components(dip, hidp); 686 hid_pm_busy_component(hidp); 687 (void) pm_raise_power(dip, 0, USB_DEV_OS_FULL_PWR); 688 hid_pm_idle_component(hidp); 689 690 /* 691 * report device 692 */ 693 ddi_report_dev(dip); 694 695 USB_DPRINTF_L4(PRINT_MASK_ATTA, hidp->hid_log_handle, 696 "hid_attach: End"); 697 698 return (DDI_SUCCESS); 699 700 fail: 701 if (hidp) { 702 USB_DPRINTF_L2(PRINT_MASK_ATTA, hidp->hid_log_handle, 703 "hid_attach: fail"); 704 hid_detach_cleanup(dip, hidp); 705 } 706 707 return (DDI_FAILURE); 708 } 709 710 711 /* 712 * hid_detach : 713 * Gets called at the time of detach. 714 */ 715 static int 716 hid_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 717 { 718 int instance = ddi_get_instance(dip); 719 hid_state_t *hidp; 720 int rval = DDI_FAILURE; 721 722 hidp = ddi_get_soft_state(hid_statep, instance); 723 724 USB_DPRINTF_L4(PRINT_MASK_ALL, hidp->hid_log_handle, "hid_detach"); 725 726 switch (cmd) { 727 case DDI_DETACH: 728 /* 729 * Undo what we did in client_attach, freeing resources 730 * and removing things we installed. The system 731 * framework guarantees we are not active with this devinfo 732 * node in any other entry points at this time. 733 */ 734 hid_detach_cleanup(dip, hidp); 735 736 return (DDI_SUCCESS); 737 case DDI_SUSPEND: 738 rval = hid_cpr_suspend(hidp); 739 740 return (rval == USB_SUCCESS ? DDI_SUCCESS : DDI_FAILURE); 741 default: 742 break; 743 } 744 745 return (rval); 746 } 747 748 749 /* 750 * hid_open : 751 * Open entry point: Opens the interrupt pipe. Sets up queues. 752 */ 753 /*ARGSUSED*/ 754 static int 755 hid_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 756 { 757 int no_of_ep = 0; 758 int rval; 759 int instance; 760 hid_state_t *hidp; 761 minor_t minor = getminor(*devp); 762 763 instance = HID_MINOR_TO_INSTANCE(minor); 764 765 hidp = ddi_get_soft_state(hid_statep, instance); 766 if (hidp == NULL) { 767 768 return (ENXIO); 769 } 770 771 USB_DPRINTF_L4(PRINT_MASK_OPEN, hidp->hid_log_handle, 772 "hid_open: Begin"); 773 774 if (sflag) { 775 /* clone open NOT supported here */ 776 777 return (ENXIO); 778 } 779 780 mutex_enter(&hidp->hid_mutex); 781 782 /* 783 * This is a workaround: 784 * Currently, if we open an already disconnected device, and send 785 * a CONSOPENPOLL ioctl to it, the system will panic, please refer 786 * to the processing HID_OPEN_POLLED_INPUT ioctl in the routine 787 * hid_mctl_receive(). 788 * The consconfig_dacf module need this interface to detect if the 789 * device is already disconnnected. 790 */ 791 if (HID_IS_INTERNAL_OPEN(minor) && 792 (hidp->hid_dev_state == USB_DEV_DISCONNECTED)) { 793 mutex_exit(&hidp->hid_mutex); 794 795 return (ENODEV); 796 } 797 798 if (q->q_ptr || (hidp->hid_streams_flags == HID_STREAMS_OPEN)) { 799 /* 800 * Exit if the same minor node is already open 801 */ 802 if (!hidp->hid_km || hidp->hid_minor == minor) { 803 mutex_exit(&hidp->hid_mutex); 804 805 return (0); 806 } 807 808 /* 809 * Check whether it is switch between physical and virtual 810 * 811 * Opening from virtual while the device is being physically 812 * opened by an application should not happen. So we ASSERT 813 * this in DEBUG version, and return error in the non-DEBUG 814 * case. 815 */ 816 ASSERT(!HID_IS_INTERNAL_OPEN(minor)); 817 818 if (HID_IS_INTERNAL_OPEN(minor)) { 819 mutex_exit(&hidp->hid_mutex); 820 821 return (EINVAL); 822 } 823 824 /* 825 * Opening the physical one while it is being underneath 826 * the virtual one. 827 * 828 * consconfig_unlink is called to unlink this device from 829 * the virtual one, thus the old stream serving for this 830 * device under the virtual one is closed, and then the 831 * lower driver's close routine (here is hid_close) is also 832 * called to accomplish the whole stream close. Here we have 833 * to drop the lock because hid_close also needs the lock. 834 * 835 * For keyboard, the old stream is: 836 * conskbd->["pushmod"->]"kbd_vp driver" 837 * For mouse, the old stream is: 838 * consms->["pushmod"->]"mouse_vp driver" 839 * 840 * After the consconfig_unlink returns, the old stream is closed 841 * and we grab the lock again to reopen this device as normal. 842 */ 843 mutex_exit(&hidp->hid_mutex); 844 845 /* 846 * If unlink fails, fail the physical open. 847 */ 848 if ((rval = consconfig_unlink(ddi_driver_major(hidp->hid_dip), 849 HID_MINOR_MAKE_INTERNAL(minor))) != 0) { 850 851 return (rval); 852 } 853 854 mutex_enter(&hidp->hid_mutex); 855 } 856 857 /* Initialize the queue pointers */ 858 q->q_ptr = hidp; 859 WR(q)->q_ptr = hidp; 860 861 hidp->hid_rq_ptr = q; 862 hidp->hid_wq_ptr = WR(q); 863 864 if (flag & FREAD) { 865 hidp->hid_interrupt_pipe = NULL; 866 no_of_ep = hidp->hid_if_descr.bNumEndpoints; 867 868 /* Check if interrupt endpoint exists */ 869 if (no_of_ep > 0) { 870 /* Open the interrupt pipe */ 871 mutex_exit(&hidp->hid_mutex); 872 873 if (usb_pipe_open(hidp->hid_dip, 874 &hidp->hid_ep_intr_descr, 875 &hidp->hid_intr_pipe_policy, USB_FLAGS_SLEEP, 876 &hidp->hid_interrupt_pipe) != 877 USB_SUCCESS) { 878 879 return (EIO); 880 } 881 mutex_enter(&hidp->hid_mutex); 882 } 883 } else { 884 /* NOT FREAD */ 885 mutex_exit(&hidp->hid_mutex); 886 887 return (EIO); 888 } 889 mutex_exit(&hidp->hid_mutex); 890 891 hid_pm_busy_component(hidp); 892 (void) pm_raise_power(hidp->hid_dip, 0, USB_DEV_OS_FULL_PWR); 893 894 mutex_enter(&hidp->hid_mutex); 895 hidp->hid_streams_flags = HID_STREAMS_OPEN; 896 mutex_exit(&hidp->hid_mutex); 897 898 qprocson(q); 899 900 mutex_enter(&hidp->hid_mutex); 901 902 if ((rval = hid_start_intr_polling(hidp)) != USB_SUCCESS) { 903 USB_DPRINTF_L2(PRINT_MASK_OPEN, hidp->hid_log_handle, 904 "unable to start intr pipe polling. rval = %d", rval); 905 906 hidp->hid_streams_flags = HID_STREAMS_DISMANTLING; 907 mutex_exit(&hidp->hid_mutex); 908 909 usb_pipe_close(hidp->hid_dip, hidp->hid_interrupt_pipe, 910 USB_FLAGS_SLEEP, NULL, NULL); 911 912 mutex_enter(&hidp->hid_mutex); 913 hidp->hid_interrupt_pipe = NULL; 914 mutex_exit(&hidp->hid_mutex); 915 916 qprocsoff(q); 917 hid_pm_idle_component(hidp); 918 919 return (EIO); 920 } 921 hidp->hid_minor = minor; 922 mutex_exit(&hidp->hid_mutex); 923 924 USB_DPRINTF_L4(PRINT_MASK_OPEN, hidp->hid_log_handle, "hid_open: End"); 925 926 /* 927 * Keyboard and mouse is Power managed by device activity. 928 * All other devices go busy on open and idle on close. 929 */ 930 switch (hidp->hid_pm->hid_pm_strategy) { 931 case HID_PM_ACTIVITY: 932 hid_pm_idle_component(hidp); 933 934 break; 935 default: 936 937 break; 938 } 939 940 return (0); 941 } 942 943 944 /* 945 * hid_close : 946 * Close entry point. 947 */ 948 /*ARGSUSED*/ 949 static int 950 hid_close(queue_t *q, int flag, cred_t *credp) 951 { 952 hid_state_t *hidp = q->q_ptr; 953 queue_t *wq; 954 mblk_t *mp; 955 956 USB_DPRINTF_L4(PRINT_MASK_CLOSE, hidp->hid_log_handle, "hid_close:"); 957 958 mutex_enter(&hidp->hid_mutex); 959 hidp->hid_streams_flags = HID_STREAMS_DISMANTLING; 960 hid_close_intr_pipe(hidp); 961 mutex_exit(&hidp->hid_mutex); 962 963 /* 964 * In case there are any outstanding requests on 965 * the default pipe, wait forever for them to complete. 966 */ 967 (void) usb_pipe_drain_reqs(hidp->hid_dip, 968 hidp->hid_default_pipe, 0, USB_FLAGS_SLEEP, NULL, 0); 969 970 /* drain any M_CTLS on the WQ */ 971 mutex_enter(&hidp->hid_mutex); 972 wq = hidp->hid_wq_ptr; 973 while (mp = getq(wq)) { 974 hid_qreply_merror(wq, mp, EIO); 975 mutex_exit(&hidp->hid_mutex); 976 hid_pm_idle_component(hidp); 977 mutex_enter(&hidp->hid_mutex); 978 } 979 mutex_exit(&hidp->hid_mutex); 980 981 qprocsoff(q); 982 q->q_ptr = NULL; 983 984 /* 985 * Devices other than keyboard/mouse go idle on close. 986 */ 987 switch (hidp->hid_pm->hid_pm_strategy) { 988 case HID_PM_ACTIVITY: 989 990 break; 991 default: 992 hid_pm_idle_component(hidp); 993 994 break; 995 } 996 USB_DPRINTF_L4(PRINT_MASK_CLOSE, hidp->hid_log_handle, 997 "hid_close: End"); 998 999 if (hidp->hid_km && !HID_IS_INTERNAL_OPEN(hidp->hid_minor)) { 1000 /* 1001 * Closing physical keyboard/mouse 1002 * 1003 * Link it back to virtual keyboard/mouse, 1004 * and hid_open will be called as a result 1005 * of the consconfig_link call. 1006 * 1007 * If linking back fails, this specific device 1008 * will not be available underneath the virtual 1009 * one, and can only be accessed via physical 1010 * open. 1011 */ 1012 consconfig_link(ddi_driver_major(hidp->hid_dip), 1013 HID_MINOR_MAKE_INTERNAL(hidp->hid_minor)); 1014 } 1015 1016 return (0); 1017 } 1018 1019 1020 /* 1021 * hid_wput : 1022 * write put routine for the hid module 1023 */ 1024 static int 1025 hid_wput(queue_t *q, mblk_t *mp) 1026 { 1027 int error = USB_SUCCESS; 1028 hid_state_t *hidp = (hid_state_t *)q->q_ptr; 1029 1030 USB_DPRINTF_L4(PRINT_MASK_ALL, hidp->hid_log_handle, 1031 "hid_wput: Begin"); 1032 1033 /* See if the upper module is passing the right thing */ 1034 ASSERT(mp != NULL); 1035 ASSERT(mp->b_datap != NULL); 1036 1037 switch (mp->b_datap->db_type) { 1038 case M_FLUSH: /* Canonical flush handling */ 1039 if (*mp->b_rptr & FLUSHW) { 1040 flushq(q, FLUSHDATA); 1041 } 1042 1043 /* read queue not used so just send up */ 1044 if (*mp->b_rptr & FLUSHR) { 1045 *mp->b_rptr &= ~FLUSHW; 1046 qreply(q, mp); 1047 } else { 1048 freemsg(mp); 1049 } 1050 1051 break; 1052 case M_IOCTL: 1053 hid_ioctl(q, mp); 1054 1055 break; 1056 case M_CTL: 1057 /* we are busy now */ 1058 hid_pm_busy_component(hidp); 1059 1060 if (q->q_first) { 1061 (void) putq(q, mp); 1062 } else { 1063 error = hid_mctl_receive(q, mp); 1064 switch (error) { 1065 case HID_ENQUEUE: 1066 /* 1067 * put this mblk on the WQ for the wsrv to 1068 * process 1069 */ 1070 (void) putq(q, mp); 1071 1072 break; 1073 case HID_INPROGRESS: 1074 /* request has been queued to the device */ 1075 1076 break; 1077 case HID_SUCCESS: 1078 /* 1079 * returned by M_CTLS that are processed 1080 * immediately 1081 */ 1082 1083 /* FALLTHRU */ 1084 case HID_FAILURE: 1085 default: 1086 hid_pm_idle_component(hidp); 1087 break; 1088 } 1089 } 1090 break; 1091 default: 1092 hid_qreply_merror(q, mp, EINVAL); 1093 error = USB_FAILURE; 1094 break; 1095 } 1096 1097 USB_DPRINTF_L4(PRINT_MASK_ALL, hidp->hid_log_handle, 1098 "hid_wput: End"); 1099 1100 return (DDI_SUCCESS); 1101 } 1102 1103 1104 /* 1105 * hid_wsrv : 1106 * Write service routine for hid. When a message arrives through 1107 * hid_wput(), it is kept in write queue to be serviced later. 1108 */ 1109 static int 1110 hid_wsrv(queue_t *q) 1111 { 1112 int error; 1113 mblk_t *mp; 1114 hid_state_t *hidp = (hid_state_t *)q->q_ptr; 1115 1116 USB_DPRINTF_L4(PRINT_MASK_ALL, hidp->hid_log_handle, 1117 "hid_wsrv: Begin"); 1118 1119 mutex_enter(&hidp->hid_mutex); 1120 USB_DPRINTF_L4(PRINT_MASK_ALL, hidp->hid_log_handle, 1121 "hid_wsrv: dev_state: %s", 1122 usb_str_dev_state(hidp->hid_dev_state)); 1123 1124 /* 1125 * raise power if we are powered down. It is OK to block here since 1126 * we have a separate thread to process this STREAM 1127 */ 1128 if (hidp->hid_dev_state == USB_DEV_PWRED_DOWN) { 1129 mutex_exit(&hidp->hid_mutex); 1130 (void) pm_raise_power(hidp->hid_dip, 0, USB_DEV_OS_FULL_PWR); 1131 mutex_enter(&hidp->hid_mutex); 1132 } 1133 1134 /* 1135 * continue servicing all the M_CTL's till the queue is empty 1136 * or the device gets disconnected or till a hid_close() 1137 */ 1138 while ((hidp->hid_dev_state == USB_DEV_ONLINE) && 1139 (hidp->hid_streams_flags != HID_STREAMS_DISMANTLING) && 1140 ((mp = getq(q)) != NULL)) { 1141 1142 /* Send a message down */ 1143 mutex_exit(&hidp->hid_mutex); 1144 error = hid_mctl_receive(q, mp); 1145 switch (error) { 1146 case HID_ENQUEUE: 1147 /* put this mblk back on q to preserve order */ 1148 (void) putbq(q, mp); 1149 1150 break; 1151 case HID_INPROGRESS: 1152 /* request has been queued to the device */ 1153 1154 break; 1155 case HID_SUCCESS: 1156 case HID_FAILURE: 1157 default: 1158 hid_pm_idle_component(hidp); 1159 1160 break; 1161 } 1162 mutex_enter(&hidp->hid_mutex); 1163 } 1164 mutex_exit(&hidp->hid_mutex); 1165 USB_DPRINTF_L4(PRINT_MASK_ALL, hidp->hid_log_handle, 1166 "hid_wsrv: End"); 1167 1168 return (DDI_SUCCESS); 1169 } 1170 1171 1172 /* 1173 * hid_power: 1174 * power entry point 1175 */ 1176 static int 1177 hid_power(dev_info_t *dip, int comp, int level) 1178 { 1179 int instance = ddi_get_instance(dip); 1180 hid_state_t *hidp; 1181 hid_power_t *hidpm; 1182 int retval; 1183 1184 hidp = ddi_get_soft_state(hid_statep, instance); 1185 1186 USB_DPRINTF_L3(PRINT_MASK_PM, hidp->hid_log_handle, "hid_power:" 1187 " hid_state: comp=%d level=%d", comp, level); 1188 1189 /* check if we are transitioning to a legal power level */ 1190 mutex_enter(&hidp->hid_mutex); 1191 hidpm = hidp->hid_pm; 1192 1193 if (USB_DEV_PWRSTATE_OK(hidpm->hid_pwr_states, level)) { 1194 1195 USB_DPRINTF_L2(PRINT_MASK_PM, hidp->hid_log_handle, 1196 "hid_power: illegal level=%d hid_pwr_states=%d", 1197 level, hidpm->hid_pwr_states); 1198 1199 mutex_exit(&hidp->hid_mutex); 1200 1201 return (DDI_FAILURE); 1202 } 1203 1204 switch (level) { 1205 case USB_DEV_OS_PWR_OFF: 1206 retval = hid_pwrlvl0(hidp); 1207 break; 1208 case USB_DEV_OS_PWR_1: 1209 retval = hid_pwrlvl1(hidp); 1210 break; 1211 case USB_DEV_OS_PWR_2: 1212 retval = hid_pwrlvl2(hidp); 1213 break; 1214 case USB_DEV_OS_FULL_PWR: 1215 retval = hid_pwrlvl3(hidp); 1216 break; 1217 default: 1218 retval = USB_FAILURE; 1219 break; 1220 } 1221 1222 mutex_exit(&hidp->hid_mutex); 1223 1224 return ((retval == USB_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE); 1225 } 1226 1227 1228 /* 1229 * hid_interrupt_pipe_callback: 1230 * Callback function for the hid intr pipe. This function is called by 1231 * USBA when a buffer has been filled. This driver does not cook the data, 1232 * it just sends the message up. 1233 */ 1234 static void 1235 hid_interrupt_pipe_callback(usb_pipe_handle_t pipe, usb_intr_req_t *req) 1236 { 1237 hid_state_t *hidp = (hid_state_t *)req->intr_client_private; 1238 queue_t *q; 1239 1240 USB_DPRINTF_L4(PRINT_MASK_ALL, hidp->hid_log_handle, 1241 "hid_interrupt_pipe_callback: ph = 0x%p req = 0x%p", pipe, req); 1242 1243 hid_pm_busy_component(hidp); 1244 1245 mutex_enter(&hidp->hid_mutex); 1246 1247 /* 1248 * If hid_close() is in progress, we shouldn't try accessing queue 1249 * Otherwise indicate that a putnext is going to happen, so 1250 * if close after this, that should wait for the putnext to finish. 1251 */ 1252 if (hidp->hid_streams_flags != HID_STREAMS_DISMANTLING) { 1253 /* 1254 * Check if data can be put to the next queue. 1255 */ 1256 if (!canputnext(hidp->hid_rq_ptr)) { 1257 USB_DPRINTF_L2(PRINT_MASK_ALL, hidp->hid_log_handle, 1258 "Buffer flushed when overflowed."); 1259 1260 /* Flush the queue above */ 1261 hid_flush(hidp->hid_rq_ptr); 1262 mutex_exit(&hidp->hid_mutex); 1263 } else { 1264 q = hidp->hid_rq_ptr; 1265 mutex_exit(&hidp->hid_mutex); 1266 1267 /* Put data upstream */ 1268 putnext(q, req->intr_data); 1269 1270 /* usb_free_intr_req should not free data */ 1271 req->intr_data = NULL; 1272 } 1273 } else { 1274 mutex_exit(&hidp->hid_mutex); 1275 } 1276 1277 /* free request and data */ 1278 usb_free_intr_req(req); 1279 hid_pm_idle_component(hidp); 1280 } 1281 1282 1283 /* 1284 * hid_default_pipe_callback : 1285 * Callback routine for the asynchronous control transfer 1286 * Called from hid_send_async_ctrl_request() where we open 1287 * the pipe in exclusive mode 1288 */ 1289 static void 1290 hid_default_pipe_callback(usb_pipe_handle_t pipe, usb_ctrl_req_t *req) 1291 { 1292 hid_default_pipe_arg_t *hid_default_pipe_arg = 1293 (hid_default_pipe_arg_t *)req->ctrl_client_private; 1294 hid_state_t *hidp = hid_default_pipe_arg->hid_default_pipe_arg_hidp; 1295 mblk_t *mctl_mp; 1296 mblk_t *data = NULL; 1297 queue_t *q; 1298 1299 USB_DPRINTF_L4(PRINT_MASK_ALL, hidp->hid_log_handle, 1300 "hid_default_pipe_callback: " 1301 "ph = 0x%p, req = 0x%p, data= 0x%p", pipe, req, data); 1302 1303 ASSERT((req->ctrl_cb_flags & USB_CB_INTR_CONTEXT) == 0); 1304 1305 if (req->ctrl_data) { 1306 data = req->ctrl_data; 1307 req->ctrl_data = NULL; 1308 } 1309 1310 /* 1311 * Free the b_cont of the original message that was sent down. 1312 */ 1313 mctl_mp = hid_default_pipe_arg->hid_default_pipe_arg_mblk; 1314 freemsg(mctl_mp->b_cont); 1315 1316 /* chain the mblk received to the original & send it up */ 1317 mctl_mp->b_cont = data; 1318 mutex_enter(&hidp->hid_mutex); 1319 q = hidp->hid_rq_ptr; 1320 mutex_exit(&hidp->hid_mutex); 1321 if (canputnext(q)) { 1322 putnext(q, mctl_mp); 1323 } else { 1324 freemsg(mctl_mp); /* avoid leak */ 1325 } 1326 1327 /* 1328 * Free the argument for the asynchronous callback 1329 */ 1330 kmem_free(hid_default_pipe_arg, sizeof (hid_default_pipe_arg_t)); 1331 1332 /* 1333 * Free the control pipe request structure. 1334 */ 1335 usb_free_ctrl_req(req); 1336 1337 mutex_enter(&hidp->hid_mutex); 1338 hidp->hid_default_pipe_req--; 1339 ASSERT(hidp->hid_default_pipe_req >= 0); 1340 mutex_exit(&hidp->hid_mutex); 1341 1342 hid_pm_idle_component(hidp); 1343 qenable(hidp->hid_wq_ptr); 1344 } 1345 1346 1347 /* 1348 * hid_interrupt_pipe_exception_callback: 1349 * Exception callback routine for interrupt pipe. If there is any data, 1350 * destroy it. No threads are waiting for the exception callback. 1351 */ 1352 /*ARGSUSED*/ 1353 static void 1354 hid_interrupt_pipe_exception_callback(usb_pipe_handle_t pipe, 1355 usb_intr_req_t *req) 1356 { 1357 hid_state_t *hidp = (hid_state_t *)req->intr_client_private; 1358 mblk_t *data = req->intr_data; 1359 usb_cb_flags_t flags = req->intr_cb_flags; 1360 int rval; 1361 1362 USB_DPRINTF_L2(PRINT_MASK_ALL, hidp->hid_log_handle, 1363 "hid_interrupt_pipe_exception_callback: " 1364 "completion_reason = 0x%x, data = 0x%p, flag = 0x%x", 1365 req->intr_completion_reason, (void *)data, req->intr_cb_flags); 1366 1367 ASSERT((req->intr_cb_flags & USB_CB_INTR_CONTEXT) == 0); 1368 1369 if (((flags & USB_CB_FUNCTIONAL_STALL) != 0) && 1370 ((flags & USB_CB_STALL_CLEARED) == 0)) { 1371 USB_DPRINTF_L2(PRINT_MASK_ALL, 1372 hidp->hid_log_handle, 1373 "hid_interrupt_pipe_exception_callback: " 1374 "unable to clear stall. flags = 0x%x", 1375 req->intr_cb_flags); 1376 } 1377 1378 mutex_enter(&hidp->hid_mutex); 1379 1380 switch (req->intr_completion_reason) { 1381 case USB_CR_STOPPED_POLLING: 1382 case USB_CR_PIPE_CLOSING: 1383 default: 1384 1385 break; 1386 case USB_CR_PIPE_RESET: 1387 case USB_CR_NO_RESOURCES: 1388 if ((hidp->hid_dev_state == USB_DEV_ONLINE) && 1389 ((rval = hid_start_intr_polling(hidp)) != 1390 USB_SUCCESS)) { 1391 USB_DPRINTF_L2(PRINT_MASK_ALL, hidp->hid_log_handle, 1392 "unable to restart interrupt poll. rval = %d", 1393 rval); 1394 } 1395 1396 break; 1397 } 1398 1399 mutex_exit(&hidp->hid_mutex); 1400 1401 usb_free_intr_req(req); 1402 } 1403 1404 1405 /* 1406 * hid_default_pipe_exception_callback: 1407 * Exception callback routine for default pipe. 1408 */ 1409 /*ARGSUSED*/ 1410 static void 1411 hid_default_pipe_exception_callback(usb_pipe_handle_t pipe, 1412 usb_ctrl_req_t *req) 1413 { 1414 hid_default_pipe_arg_t *hid_default_pipe_arg = 1415 (hid_default_pipe_arg_t *)req->ctrl_client_private; 1416 1417 hid_state_t *hidp = hid_default_pipe_arg->hid_default_pipe_arg_hidp; 1418 mblk_t *data = NULL; 1419 usb_cr_t ctrl_completion_reason = req->ctrl_completion_reason; 1420 mblk_t *mp; 1421 queue_t *q; 1422 1423 USB_DPRINTF_L2(PRINT_MASK_ALL, hidp->hid_log_handle, 1424 "hid_default_pipe_exception_callback: " 1425 "completion_reason = 0x%x, data = 0x%p, flag = 0x%x", 1426 ctrl_completion_reason, (void *)data, req->ctrl_cb_flags); 1427 1428 ASSERT((req->ctrl_cb_flags & USB_CB_INTR_CONTEXT) == 0); 1429 1430 /* 1431 * This is an exception callback, no need to pass data up 1432 */ 1433 q = hidp->hid_rq_ptr; 1434 1435 /* 1436 * Pass an error message up. Reuse existing mblk. 1437 */ 1438 if (canputnext(q)) { 1439 mp = hid_default_pipe_arg->hid_default_pipe_arg_mblk; 1440 mp->b_datap->db_type = M_ERROR; 1441 mp->b_rptr = mp->b_datap->db_base; 1442 mp->b_wptr = mp->b_rptr + sizeof (char); 1443 *mp->b_rptr = EIO; 1444 putnext(q, mp); 1445 } else { 1446 freemsg(hid_default_pipe_arg->hid_default_pipe_arg_mblk); 1447 } 1448 kmem_free(hid_default_pipe_arg, sizeof (hid_default_pipe_arg_t)); 1449 1450 mutex_enter(&hidp->hid_mutex); 1451 hidp->hid_default_pipe_req--; 1452 ASSERT(hidp->hid_default_pipe_req >= 0); 1453 mutex_exit(&hidp->hid_mutex); 1454 1455 qenable(hidp->hid_wq_ptr); 1456 usb_free_ctrl_req(req); 1457 hid_pm_idle_component(hidp); 1458 } 1459 1460 1461 /* 1462 * event handling: 1463 * 1464 * hid_reconnect_event_callback: 1465 * the device was disconnected but this instance not detached, probably 1466 * because the device was busy 1467 * 1468 * If the same device, continue with restoring state 1469 */ 1470 static int 1471 hid_restore_state_event_callback(dev_info_t *dip) 1472 { 1473 hid_state_t *hidp = (hid_state_t *)ddi_get_soft_state(hid_statep, 1474 ddi_get_instance(dip)); 1475 1476 ASSERT(hidp != NULL); 1477 1478 USB_DPRINTF_L3(PRINT_MASK_EVENTS, hidp->hid_log_handle, 1479 "hid_restore_state_event_callback: dip=0x%p", dip); 1480 1481 hid_restore_device_state(dip, hidp); 1482 1483 return (USB_SUCCESS); 1484 } 1485 1486 1487 /* 1488 * hid_cpr_suspend 1489 * Fail suspend if we can't finish outstanding i/o activity. 1490 */ 1491 static int 1492 hid_cpr_suspend(hid_state_t *hidp) 1493 { 1494 int rval, prev_state; 1495 int retval = USB_FAILURE; 1496 1497 USB_DPRINTF_L4(PRINT_MASK_EVENTS, hidp->hid_log_handle, 1498 "hid_cpr_suspend: dip=0x%p", hidp->hid_dip); 1499 1500 mutex_enter(&hidp->hid_mutex); 1501 switch (hidp->hid_dev_state) { 1502 case USB_DEV_ONLINE: 1503 case USB_DEV_PWRED_DOWN: 1504 case USB_DEV_DISCONNECTED: 1505 prev_state = hidp->hid_dev_state; 1506 hidp->hid_dev_state = USB_DEV_SUSPENDED; 1507 mutex_exit(&hidp->hid_mutex); 1508 1509 /* drain all request outstanding on the default control pipe */ 1510 rval = usb_pipe_drain_reqs(hidp->hid_dip, 1511 hidp->hid_default_pipe, hid_default_pipe_drain_timeout, 1512 USB_FLAGS_SLEEP, NULL, 0); 1513 1514 /* fail checkpoint if we haven't finished the job yet */ 1515 mutex_enter(&hidp->hid_mutex); 1516 if ((rval != USB_SUCCESS) || (hidp->hid_default_pipe_req > 0)) { 1517 USB_DPRINTF_L2(PRINT_MASK_EVENTS, hidp->hid_log_handle, 1518 "hid_cpr_suspend: " 1519 "device busy - can't checkpoint"); 1520 1521 /* fall back to previous state */ 1522 hidp->hid_dev_state = prev_state; 1523 } else { 1524 retval = USB_SUCCESS; 1525 hid_save_device_state(hidp); 1526 } 1527 1528 break; 1529 case USB_DEV_SUSPENDED: 1530 default: 1531 USB_DPRINTF_L2(PRINT_MASK_EVENTS, hidp->hid_log_handle, 1532 "hid_cpr_suspend: Illegal dev state: %d", 1533 hidp->hid_dev_state); 1534 1535 break; 1536 } 1537 mutex_exit(&hidp->hid_mutex); 1538 1539 return (retval); 1540 } 1541 1542 1543 static void 1544 hid_cpr_resume(hid_state_t *hidp) 1545 { 1546 USB_DPRINTF_L4(PRINT_MASK_EVENTS, hidp->hid_log_handle, 1547 "hid_cpr_resume: dip=0x%p", hidp->hid_dip); 1548 1549 hid_restore_device_state(hidp->hid_dip, hidp); 1550 } 1551 1552 1553 /* 1554 * hid_disconnect_event_callback: 1555 * The device has been disconnected. We either wait for 1556 * detach or a reconnect event. Close all pipes and timeouts. 1557 */ 1558 static int 1559 hid_disconnect_event_callback(dev_info_t *dip) 1560 { 1561 hid_state_t *hidp; 1562 1563 hidp = (hid_state_t *)ddi_get_soft_state(hid_statep, 1564 ddi_get_instance(dip)); 1565 ASSERT(hidp != NULL); 1566 1567 USB_DPRINTF_L4(PRINT_MASK_EVENTS, hidp->hid_log_handle, 1568 "hid_disconnect_event_callback: dip=0x%p", dip); 1569 1570 mutex_enter(&hidp->hid_mutex); 1571 switch (hidp->hid_dev_state) { 1572 case USB_DEV_ONLINE: 1573 case USB_DEV_PWRED_DOWN: 1574 hidp->hid_dev_state = USB_DEV_DISCONNECTED; 1575 if (hidp->hid_streams_flags == HID_STREAMS_OPEN) { 1576 1577 USB_DPRINTF_L2(PRINT_MASK_EVENTS, hidp->hid_log_handle, 1578 "busy device has been disconnected"); 1579 } 1580 hid_save_device_state(hidp); 1581 1582 break; 1583 case USB_DEV_SUSPENDED: 1584 /* we remain suspended */ 1585 1586 break; 1587 default: 1588 USB_DPRINTF_L2(PRINT_MASK_EVENTS, hidp->hid_log_handle, 1589 "hid_disconnect_event_callback: Illegal dev state: %d", 1590 hidp->hid_dev_state); 1591 1592 break; 1593 } 1594 mutex_exit(&hidp->hid_mutex); 1595 1596 return (USB_SUCCESS); 1597 } 1598 1599 1600 /* 1601 * hid_power_change_callback: 1602 * Async callback function to notify pm_raise_power completion 1603 * after hid_power entry point is called. 1604 */ 1605 static void 1606 hid_power_change_callback(void *arg, int rval) 1607 { 1608 hid_state_t *hidp; 1609 queue_t *wq; 1610 1611 hidp = (hid_state_t *)arg; 1612 1613 USB_DPRINTF_L4(PRINT_MASK_PM, hidp->hid_log_handle, 1614 "hid_power_change_callback - rval: %d", rval); 1615 1616 mutex_enter(&hidp->hid_mutex); 1617 hidp->hid_pm->hid_raise_power = B_FALSE; 1618 1619 if (hidp->hid_dev_state == USB_DEV_ONLINE) { 1620 wq = hidp->hid_wq_ptr; 1621 mutex_exit(&hidp->hid_mutex); 1622 1623 qenable(wq); 1624 1625 } else { 1626 mutex_exit(&hidp->hid_mutex); 1627 } 1628 } 1629 1630 1631 /* 1632 * hid_parse_hid_descr: 1633 * Parse the hid descriptor, check after interface and after 1634 * endpoint descriptor 1635 */ 1636 static size_t 1637 hid_parse_hid_descr( 1638 usb_hid_descr_t *ret_descr, 1639 size_t ret_buf_len, 1640 usb_alt_if_data_t *altif_data, 1641 usb_ep_data_t *ep_data) 1642 { 1643 usb_cvs_data_t *cvs; 1644 int which_cvs; 1645 1646 for (which_cvs = 0; which_cvs < altif_data->altif_n_cvs; which_cvs++) { 1647 cvs = &altif_data->altif_cvs[which_cvs]; 1648 if (cvs->cvs_buf == NULL) { 1649 continue; 1650 } 1651 if (cvs->cvs_buf[1] == USB_DESCR_TYPE_HID) { 1652 return (usb_parse_data("ccscccs", 1653 cvs->cvs_buf, cvs->cvs_buf_len, 1654 (void *)ret_descr, 1655 (size_t)ret_buf_len)); 1656 } 1657 } 1658 1659 /* now try after endpoint */ 1660 for (which_cvs = 0; which_cvs < ep_data->ep_n_cvs; which_cvs++) { 1661 cvs = &ep_data->ep_cvs[which_cvs]; 1662 if (cvs->cvs_buf == NULL) { 1663 continue; 1664 } 1665 if (cvs->cvs_buf[1] == USB_DESCR_TYPE_HID) { 1666 return (usb_parse_data("ccscccs", 1667 cvs->cvs_buf, cvs->cvs_buf_len, 1668 (void *)ret_descr, 1669 (size_t)ret_buf_len)); 1670 } 1671 } 1672 1673 return (USB_PARSE_ERROR); 1674 } 1675 1676 1677 /* 1678 * hid_parse_hid_descr_failure: 1679 * If parsing of hid descriptor failed and the device is 1680 * a keyboard or mouse, use predefined length and packet size. 1681 */ 1682 static int 1683 hid_parse_hid_descr_failure(hid_state_t *hidp) 1684 { 1685 /* 1686 * Parsing hid descriptor failed, probably because the 1687 * device did not return a valid hid descriptor. Check to 1688 * see if this is a keyboard or mouse. If so, use the 1689 * predefined hid descriptor length and packet size. 1690 * Otherwise, detach and return failure. 1691 */ 1692 USB_DPRINTF_L1(PRINT_MASK_ATTA, hidp->hid_log_handle, 1693 "Parsing of hid descriptor failed"); 1694 1695 if (hidp->hid_if_descr.bInterfaceProtocol == KEYBOARD_PROTOCOL) { 1696 USB_DPRINTF_L2(PRINT_MASK_ATTA, hidp->hid_log_handle, 1697 "Set hid descriptor length to predefined " 1698 "USB_KB_HID_DESCR_LENGTH for keyboard."); 1699 1700 /* device is a keyboard */ 1701 hidp->hid_hid_descr.wReportDescriptorLength = 1702 USB_KB_HID_DESCR_LENGTH; 1703 1704 hidp->hid_packet_size = USBKPSZ; 1705 1706 } else if (hidp->hid_if_descr.bInterfaceProtocol == 1707 MOUSE_PROTOCOL) { 1708 USB_DPRINTF_L2(PRINT_MASK_ATTA, hidp->hid_log_handle, 1709 "Set hid descriptor length to predefined " 1710 "USB_MS_HID_DESCR_LENGTH for mouse."); 1711 1712 /* device is a mouse */ 1713 hidp->hid_hid_descr.wReportDescriptorLength = 1714 USB_MS_HID_DESCR_LENGTH; 1715 1716 hidp->hid_packet_size = USBMSSZ; 1717 } else { 1718 1719 return (USB_FAILURE); 1720 } 1721 1722 return (USB_SUCCESS); 1723 } 1724 1725 1726 /* 1727 * hid_handle_report_descriptor: 1728 * Get the report descriptor, call hidparser routine to parse 1729 * it and query the hidparser tree to get the packet size 1730 */ 1731 static int 1732 hid_handle_report_descriptor(hid_state_t *hidp, 1733 int interface) 1734 { 1735 usb_cr_t completion_reason; 1736 usb_cb_flags_t cb_flags; 1737 mblk_t *data = NULL; 1738 hidparser_packet_info_t hpack; 1739 int i; 1740 usb_ctrl_setup_t setup = { 1741 USB_DEV_REQ_DEV_TO_HOST | /* bmRequestType */ 1742 USB_DEV_REQ_RCPT_IF, 1743 USB_REQ_GET_DESCR, /* bRequest */ 1744 USB_CLASS_DESCR_TYPE_REPORT, /* wValue */ 1745 0, /* wIndex: interface, fill in later */ 1746 0, /* wLength, fill in later */ 1747 0 /* attributes */ 1748 }; 1749 1750 /* 1751 * Parsing hid desciptor was successful earlier. 1752 * Get Report Descriptor 1753 */ 1754 setup.wIndex = (uint16_t)interface; 1755 setup.wLength = hidp->hid_hid_descr.wReportDescriptorLength; 1756 if (usb_pipe_ctrl_xfer_wait(hidp->hid_default_pipe, 1757 &setup, 1758 &data, /* data */ 1759 &completion_reason, &cb_flags, 0) != USB_SUCCESS) { 1760 1761 USB_DPRINTF_L1(PRINT_MASK_ATTA, hidp->hid_log_handle, 1762 "Failed to receive the Report Descriptor"); 1763 freemsg(data); 1764 1765 return (USB_FAILURE); 1766 1767 } else { 1768 int n = hidp->hid_hid_descr.wReportDescriptorLength; 1769 1770 ASSERT(data); 1771 1772 /* Print the report descriptor */ 1773 for (i = 0; i < n; i++) { 1774 USB_DPRINTF_L3(PRINT_MASK_ATTA, hidp->hid_log_handle, 1775 "Index = %d\tvalue =0x%x", i, 1776 (int)(data->b_rptr[i])); 1777 } 1778 1779 /* Get Report Descriptor was successful */ 1780 if (hidparser_parse_report_descriptor( 1781 data->b_rptr, 1782 hidp->hid_hid_descr.wReportDescriptorLength, 1783 &hidp->hid_hid_descr, 1784 &hidp->hid_report_descr) == HIDPARSER_SUCCESS) { 1785 1786 /* find max intr-in xfer length */ 1787 hidparser_find_max_packet_size_from_report_descriptor( 1788 hidp->hid_report_descr, &hpack); 1789 /* round up to the nearest byte */ 1790 hidp->hid_packet_size = (hpack.max_packet_size + 7) / 8; 1791 1792 /* if report id is used, add more more byte for it */ 1793 if (hpack.report_id != HID_REPORT_ID_UNDEFINED) { 1794 hidp->hid_packet_size++; 1795 } 1796 } else { 1797 USB_DPRINTF_L1(PRINT_MASK_ATTA, hidp->hid_log_handle, 1798 "Invalid Report Descriptor"); 1799 freemsg(data); 1800 1801 return (USB_FAILURE); 1802 } 1803 1804 freemsg(data); 1805 1806 return (USB_SUCCESS); 1807 } 1808 } 1809 1810 1811 /* 1812 * hid_set_idle: 1813 * Make a clas specific request to SET_IDLE. 1814 * In this case send no reports if state has not changed. 1815 * See HID 7.2.4. 1816 */ 1817 /*ARGSUSED*/ 1818 static void 1819 hid_set_idle(hid_state_t *hidp) 1820 { 1821 usb_cr_t completion_reason; 1822 usb_cb_flags_t cb_flags; 1823 usb_ctrl_setup_t setup = { 1824 USB_DEV_REQ_HOST_TO_DEV | /* bmRequestType */ 1825 USB_DEV_REQ_TYPE_CLASS | 1826 USB_DEV_REQ_RCPT_IF, 1827 SET_IDLE, /* bRequest */ 1828 DURATION, /* wValue */ 1829 0, /* wIndex: interface, fill in later */ 1830 0, /* wLength */ 1831 0 /* attributes */ 1832 }; 1833 1834 USB_DPRINTF_L4(PRINT_MASK_ATTA, hidp->hid_log_handle, 1835 "hid_set_idle: Begin"); 1836 1837 setup.wIndex = hidp->hid_if_descr.bInterfaceNumber; 1838 if (usb_pipe_ctrl_xfer_wait( 1839 hidp->hid_default_pipe, 1840 &setup, 1841 NULL, /* no data to send. */ 1842 &completion_reason, &cb_flags, 0) != USB_SUCCESS) { 1843 1844 USB_DPRINTF_L2(PRINT_MASK_ATTA, hidp->hid_log_handle, 1845 "Failed while trying to set idle," 1846 "cr = %d, cb_flags = 0x%x\n", 1847 completion_reason, cb_flags); 1848 } 1849 USB_DPRINTF_L4(PRINT_MASK_ATTA, hidp->hid_log_handle, 1850 "hid_set_idle: End"); 1851 } 1852 1853 1854 /* 1855 * hid_set_protocol: 1856 * Initialize the device to set the preferred protocol 1857 */ 1858 /*ARGSUSED*/ 1859 static void 1860 hid_set_protocol(hid_state_t *hidp, int protocol) 1861 { 1862 usb_cr_t completion_reason; 1863 usb_cb_flags_t cb_flags; 1864 usb_ctrl_setup_t setup; 1865 1866 USB_DPRINTF_L4(PRINT_MASK_ATTA, hidp->hid_log_handle, 1867 "hid_set_protocol(%d): Begin", protocol); 1868 1869 /* initialize the setup request */ 1870 setup.bmRequestType = USB_DEV_REQ_HOST_TO_DEV | 1871 USB_DEV_REQ_TYPE_CLASS | USB_DEV_REQ_RCPT_IF; 1872 setup.bRequest = SET_PROTOCOL; 1873 setup.wValue = protocol; 1874 setup.wIndex = hidp->hid_if_descr.bInterfaceNumber; 1875 setup.wLength = 0; 1876 setup.attrs = 0; 1877 if (usb_pipe_ctrl_xfer_wait( 1878 hidp->hid_default_pipe, /* bmRequestType */ 1879 &setup, 1880 NULL, /* no data to send */ 1881 &completion_reason, &cb_flags, 0) != USB_SUCCESS) { 1882 /* 1883 * Some devices fail to follow the specification 1884 * and instead of STALLing, they continously 1885 * NAK the SET_IDLE command. We need to reset 1886 * the pipe then, so that ohci doesn't panic. 1887 */ 1888 USB_DPRINTF_L2(PRINT_MASK_ATTA, hidp->hid_log_handle, 1889 "Failed while trying to set protocol:%d," 1890 "cr = %d cb_flags = 0x%x\n", 1891 completion_reason, cb_flags, protocol); 1892 } 1893 1894 USB_DPRINTF_L4(PRINT_MASK_ATTA, hidp->hid_log_handle, 1895 "hid_set_protocol: End"); 1896 } 1897 1898 1899 /* 1900 * hid_detach_cleanup: 1901 * called by attach and detach for cleanup. 1902 */ 1903 static void 1904 hid_detach_cleanup(dev_info_t *dip, hid_state_t *hidp) 1905 { 1906 int flags = hidp->hid_attach_flags; 1907 int rval; 1908 hid_power_t *hidpm; 1909 1910 USB_DPRINTF_L4(PRINT_MASK_ALL, hidp->hid_log_handle, 1911 "hid_detach_cleanup: Begin"); 1912 1913 if ((hidp->hid_attach_flags & HID_LOCK_INIT) == 0) { 1914 1915 goto done; 1916 } 1917 1918 /* 1919 * Disable the event callbacks first, after this point, event 1920 * callbacks will never get called. Note we shouldn't hold 1921 * mutex while unregistering events because there may be a 1922 * competing event callback thread. Event callbacks are done 1923 * with ndi mutex held and this can cause a potential deadlock. 1924 */ 1925 usb_unregister_event_cbs(dip, &hid_events); 1926 1927 mutex_enter(&hidp->hid_mutex); 1928 1929 hidpm = hidp->hid_pm; 1930 1931 USB_DPRINTF_L2(PRINT_MASK_ALL, hidp->hid_log_handle, 1932 "hid_detach_cleanup: hidpm=0x%p", hidpm); 1933 1934 if (hidpm && (hidp->hid_dev_state != USB_DEV_DISCONNECTED)) { 1935 1936 mutex_exit(&hidp->hid_mutex); 1937 hid_pm_busy_component(hidp); 1938 if (hid_is_pm_enabled(dip) == USB_SUCCESS) { 1939 1940 if (hidpm->hid_wakeup_enabled) { 1941 1942 /* First bring the device to full power */ 1943 (void) pm_raise_power(dip, 0, 1944 USB_DEV_OS_FULL_PWR); 1945 1946 /* Disable remote wakeup */ 1947 rval = usb_handle_remote_wakeup(dip, 1948 USB_REMOTE_WAKEUP_DISABLE); 1949 1950 if (rval != DDI_SUCCESS) { 1951 USB_DPRINTF_L2(PRINT_MASK_ALL, 1952 hidp->hid_log_handle, 1953 "hid_detach_cleanup: " 1954 "disble remote wakeup failed, " 1955 "rval= %d", rval); 1956 } 1957 } 1958 1959 (void) pm_lower_power(dip, 0, USB_DEV_OS_PWR_OFF); 1960 } 1961 hid_pm_idle_component(hidp); 1962 mutex_enter(&hidp->hid_mutex); 1963 } 1964 1965 if (hidpm) { 1966 freemsg(hidpm->hid_pm_pwrup); 1967 kmem_free(hidpm, sizeof (hid_power_t)); 1968 hidp->hid_pm = NULL; 1969 } 1970 1971 mutex_exit(&hidp->hid_mutex); 1972 1973 if (hidp->hid_report_descr != NULL) { 1974 (void) hidparser_free_report_descriptor_handle( 1975 hidp->hid_report_descr); 1976 } 1977 1978 if (flags & HID_MINOR_NODES) { 1979 ddi_remove_minor_node(dip, NULL); 1980 } 1981 1982 mutex_destroy(&hidp->hid_mutex); 1983 1984 USB_DPRINTF_L4(PRINT_MASK_ALL, hidp->hid_log_handle, 1985 "hid_detach_cleanup: End"); 1986 1987 done: 1988 usb_client_detach(dip, hidp->hid_dev_data); 1989 usb_free_log_hdl(hidp->hid_log_handle); 1990 ddi_soft_state_free(hid_statep, hidp->hid_instance); 1991 1992 ddi_prop_remove_all(dip); 1993 } 1994 1995 1996 /* 1997 * hid_start_intr_polling: 1998 * Allocate an interrupt request structure, initialize, 1999 * and start interrupt transfers. 2000 */ 2001 static int 2002 hid_start_intr_polling(hid_state_t *hidp) 2003 { 2004 usb_intr_req_t *req; 2005 int rval = USB_SUCCESS; 2006 2007 USB_DPRINTF_L4(PRINT_MASK_PM, hidp->hid_log_handle, 2008 "hid_start_intr_polling: " 2009 "dev_state=%s str_flags=%d ph=0x%p", 2010 usb_str_dev_state(hidp->hid_dev_state), hidp->hid_streams_flags, 2011 hidp->hid_interrupt_pipe); 2012 2013 if ((hidp->hid_streams_flags == HID_STREAMS_OPEN) && 2014 (hidp->hid_interrupt_pipe != NULL)) { 2015 /* 2016 * initialize interrupt pipe request structure 2017 */ 2018 req = usb_alloc_intr_req(hidp->hid_dip, 0, USB_FLAGS_SLEEP); 2019 req->intr_client_private = (usb_opaque_t)hidp; 2020 req->intr_attributes = USB_ATTRS_SHORT_XFER_OK | 2021 USB_ATTRS_AUTOCLEARING; 2022 req->intr_len = hidp->hid_packet_size; 2023 req->intr_cb = hid_interrupt_pipe_callback; 2024 req->intr_exc_cb = hid_interrupt_pipe_exception_callback; 2025 2026 /* 2027 * Start polling on the interrupt pipe. 2028 */ 2029 mutex_exit(&hidp->hid_mutex); 2030 2031 if ((rval = usb_pipe_intr_xfer(hidp->hid_interrupt_pipe, req, 2032 USB_FLAGS_SLEEP)) != USB_SUCCESS) { 2033 USB_DPRINTF_L2(PRINT_MASK_PM, hidp->hid_log_handle, 2034 "hid_start_intr_polling failed: rval = %d", 2035 rval); 2036 usb_free_intr_req(req); 2037 } 2038 2039 mutex_enter(&hidp->hid_mutex); 2040 } 2041 2042 USB_DPRINTF_L4(PRINT_MASK_PM, hidp->hid_log_handle, 2043 "hid_start_intr_polling: done, rval = %d", rval); 2044 2045 return (rval); 2046 } 2047 2048 2049 /* 2050 * hid_close_intr_pipe: 2051 * close the interrupt pipe after draining all callbacks 2052 */ 2053 static void 2054 hid_close_intr_pipe(hid_state_t *hidp) 2055 { 2056 USB_DPRINTF_L4(PRINT_MASK_CLOSE, hidp->hid_log_handle, 2057 "hid_close_intr_pipe: Begin"); 2058 2059 if (hidp->hid_interrupt_pipe) { 2060 /* 2061 * Close the interrupt pipe 2062 */ 2063 mutex_exit(&hidp->hid_mutex); 2064 usb_pipe_close(hidp->hid_dip, hidp->hid_interrupt_pipe, 2065 USB_FLAGS_SLEEP, NULL, NULL); 2066 mutex_enter(&hidp->hid_mutex); 2067 hidp->hid_interrupt_pipe = NULL; 2068 } 2069 USB_DPRINTF_L4(PRINT_MASK_CLOSE, hidp->hid_log_handle, 2070 "hid_close_intr_pipe: End"); 2071 } 2072 2073 2074 /* 2075 * hid_mctl_receive: 2076 * Handle M_CTL messages from upper stream. If 2077 * we don't understand the command, free message. 2078 */ 2079 static int 2080 hid_mctl_receive(register queue_t *q, register mblk_t *mp) 2081 { 2082 hid_state_t *hidp = (hid_state_t *)q->q_ptr; 2083 struct iocblk *iocp; 2084 int error = HID_FAILURE; 2085 uchar_t request_type; 2086 hid_req_t *hid_req_data = NULL; 2087 hid_polled_input_callback_t hid_polled_input; 2088 2089 USB_DPRINTF_L4(PRINT_MASK_ALL, hidp->hid_log_handle, 2090 "hid_mctl_receive"); 2091 2092 iocp = (struct iocblk *)mp->b_rptr; 2093 2094 switch (iocp->ioc_cmd) { 2095 case HID_SET_REPORT: 2096 /* FALLTHRU */ 2097 case HID_SET_IDLE: 2098 /* FALLTHRU */ 2099 case HID_SET_PROTOCOL: 2100 request_type = USB_DEV_REQ_HOST_TO_DEV | 2101 USB_DEV_REQ_RCPT_IF | USB_DEV_REQ_TYPE_CLASS; 2102 2103 break; 2104 case HID_GET_REPORT: 2105 /* FALLTHRU */ 2106 case HID_GET_IDLE: 2107 /* FALLTHRU */ 2108 case HID_GET_PROTOCOL: 2109 request_type = USB_DEV_REQ_DEV_TO_HOST | 2110 USB_DEV_REQ_RCPT_IF | USB_DEV_REQ_TYPE_CLASS; 2111 2112 break; 2113 case HID_GET_PARSER_HANDLE: 2114 if (canputnext(RD(q))) { 2115 freemsg(mp->b_cont); 2116 mp->b_cont = hid_data2mblk( 2117 (uchar_t *)&hidp->hid_report_descr, 2118 sizeof (hidp->hid_report_descr)); 2119 if (mp->b_cont == NULL) { 2120 /* 2121 * can't allocate mblk, indicate 2122 * that nothing is returned 2123 */ 2124 iocp->ioc_count = 0; 2125 } else { 2126 iocp->ioc_count = 2127 sizeof (hidp->hid_report_descr); 2128 } 2129 qreply(q, mp); 2130 2131 return (HID_SUCCESS); 2132 } else { 2133 2134 /* retry */ 2135 return (HID_ENQUEUE); 2136 } 2137 case HID_OPEN_POLLED_INPUT: 2138 if (canputnext(RD(q))) { 2139 freemsg(mp->b_cont); 2140 2141 /* Initialize the structure */ 2142 hid_polled_input.hid_polled_version = 2143 HID_POLLED_INPUT_V0; 2144 hid_polled_input.hid_polled_read = hid_polled_read; 2145 hid_polled_input.hid_polled_input_enter = 2146 hid_polled_input_enter; 2147 hid_polled_input.hid_polled_input_exit = 2148 hid_polled_input_exit; 2149 hid_polled_input.hid_polled_input_handle = 2150 (hid_polled_handle_t)hidp; 2151 2152 mp->b_cont = hid_data2mblk( 2153 (uchar_t *)&hid_polled_input, 2154 sizeof (hid_polled_input_callback_t)); 2155 if (mp->b_cont == NULL) { 2156 /* 2157 * can't allocate mblk, indicate that nothing 2158 * is being returned. 2159 */ 2160 iocp->ioc_count = 0; 2161 } else { 2162 /* Call down into USBA */ 2163 (void) hid_polled_input_init(hidp); 2164 2165 iocp->ioc_count = 2166 sizeof (hid_polled_input_callback_t); 2167 } 2168 qreply(q, mp); 2169 2170 return (HID_SUCCESS); 2171 } else { 2172 2173 /* retry */ 2174 return (HID_ENQUEUE); 2175 } 2176 case HID_CLOSE_POLLED_INPUT: 2177 /* Call down into USBA */ 2178 (void) hid_polled_input_fini(hidp); 2179 2180 iocp->ioc_count = 0; 2181 qreply(q, mp); 2182 2183 return (HID_SUCCESS); 2184 default: 2185 hid_qreply_merror(q, mp, EINVAL); 2186 2187 return (HID_FAILURE); 2188 } 2189 2190 /* 2191 * These (device executable) commands require a hid_req_t. 2192 * Make sure one is present 2193 */ 2194 if (mp->b_cont == NULL) { 2195 hid_qreply_merror(q, mp, EINVAL); 2196 2197 return (error); 2198 } else { 2199 hid_req_data = (hid_req_t *)mp->b_cont->b_rptr; 2200 if ((iocp->ioc_cmd == HID_SET_REPORT) && 2201 (hid_req_data->hid_req_data == NULL)) { 2202 hid_qreply_merror(q, mp, EINVAL); 2203 2204 return (error); 2205 } 2206 } 2207 2208 /* 2209 * Check is version no. is correct. This 2210 * is coming from the user 2211 */ 2212 if (hid_req_data->hid_req_version_no != HID_VERSION_V_0) { 2213 hid_qreply_merror(q, mp, EINVAL); 2214 2215 return (error); 2216 } 2217 2218 mutex_enter(&hidp->hid_mutex); 2219 USB_DPRINTF_L4(PRINT_MASK_ALL, hidp->hid_log_handle, 2220 "hid_mctl_receive: dev_state=%s", 2221 usb_str_dev_state(hidp->hid_dev_state)); 2222 2223 switch (hidp->hid_dev_state) { 2224 case USB_DEV_PWRED_DOWN: 2225 /* 2226 * get the device full powered. We get a callback 2227 * which enables the WQ and kicks off IO 2228 */ 2229 hidp->hid_dev_state = USB_DEV_HID_POWER_CHANGE; 2230 mutex_exit(&hidp->hid_mutex); 2231 if (usb_req_raise_power(hidp->hid_dip, 0, 2232 USB_DEV_OS_FULL_PWR, hid_power_change_callback, 2233 hidp, 0) != USB_SUCCESS) { 2234 /* we retry raising power in wsrv */ 2235 mutex_enter(&hidp->hid_mutex); 2236 hidp->hid_dev_state = USB_DEV_PWRED_DOWN; 2237 mutex_exit(&hidp->hid_mutex); 2238 } 2239 error = HID_ENQUEUE; 2240 2241 break; 2242 case USB_DEV_HID_POWER_CHANGE: 2243 mutex_exit(&hidp->hid_mutex); 2244 error = HID_ENQUEUE; 2245 2246 break; 2247 case USB_DEV_ONLINE: 2248 if (hidp->hid_streams_flags != HID_STREAMS_DISMANTLING) { 2249 /* Send a message down */ 2250 mutex_exit(&hidp->hid_mutex); 2251 error = hid_mctl_execute_cmd(hidp, request_type, 2252 hid_req_data, mp); 2253 if (error == HID_FAILURE) { 2254 hid_qreply_merror(q, mp, EIO); 2255 } 2256 } else { 2257 mutex_exit(&hidp->hid_mutex); 2258 hid_qreply_merror(q, mp, EIO); 2259 } 2260 2261 break; 2262 default: 2263 mutex_exit(&hidp->hid_mutex); 2264 hid_qreply_merror(q, mp, EIO); 2265 2266 break; 2267 } 2268 2269 return (error); 2270 } 2271 2272 2273 /* 2274 * hid_mctl_execute_cmd: 2275 * Send the command to the device. 2276 */ 2277 static int 2278 hid_mctl_execute_cmd(hid_state_t *hidp, int request_type, 2279 hid_req_t *hid_req_data, mblk_t *mp) 2280 { 2281 int request_index; 2282 struct iocblk *iocp; 2283 hid_default_pipe_arg_t *def_pipe_arg; 2284 2285 iocp = (struct iocblk *)mp->b_rptr; 2286 USB_DPRINTF_L4(PRINT_MASK_ALL, hidp->hid_log_handle, 2287 "hid_mctl_execute_cmd: iocp=0x%p", iocp); 2288 2289 request_index = hidp->hid_if_descr.bInterfaceNumber; 2290 2291 /* 2292 * Set up the argument to be passed back to hid 2293 * when the asynchronous control callback is 2294 * executed. 2295 */ 2296 def_pipe_arg = kmem_zalloc(sizeof (hid_default_pipe_arg_t), 0); 2297 2298 if (def_pipe_arg == NULL) { 2299 2300 return (HID_FAILURE); 2301 } 2302 2303 def_pipe_arg->hid_default_pipe_arg_mctlmsg.ioc_cmd = iocp->ioc_cmd; 2304 def_pipe_arg->hid_default_pipe_arg_mctlmsg.ioc_count = 0; 2305 def_pipe_arg->hid_default_pipe_arg_hidp = hidp; 2306 def_pipe_arg->hid_default_pipe_arg_mblk = mp; 2307 2308 /* 2309 * Send the command down to USBA through default 2310 * pipe. 2311 */ 2312 if (hid_send_async_ctrl_request(hidp, hid_req_data, 2313 request_type, iocp->ioc_cmd, 2314 request_index, def_pipe_arg) != USB_SUCCESS) { 2315 2316 kmem_free(def_pipe_arg, sizeof (hid_default_pipe_arg_t)); 2317 2318 return (HID_FAILURE); 2319 } 2320 2321 return (HID_INPROGRESS); 2322 } 2323 2324 2325 /* 2326 * hid_send_async_ctrl_request: 2327 * Send an asynchronous control request to USBA. Since hid is a STREAMS 2328 * driver, it is not allowed to wait in its entry points except for the 2329 * open and close entry points. Therefore, hid must use the asynchronous 2330 * USBA calls. 2331 */ 2332 static int 2333 hid_send_async_ctrl_request(hid_state_t *hidp, hid_req_t *hid_request, 2334 uchar_t request_type, int request_request, 2335 ushort_t request_index, 2336 hid_default_pipe_arg_t *hid_default_pipe_arg) 2337 { 2338 usb_ctrl_req_t *ctrl_req; 2339 int rval; 2340 size_t length = 0; 2341 2342 USB_DPRINTF_L4(PRINT_MASK_ALL, hidp->hid_log_handle, 2343 "hid_send_async_ctrl_request: " 2344 "rq_type=%d rq_rq=%d index=%d", 2345 request_type, request_request, request_index); 2346 2347 mutex_enter(&hidp->hid_mutex); 2348 hidp->hid_default_pipe_req++; 2349 mutex_exit(&hidp->hid_mutex); 2350 2351 /* 2352 * Note that ctrl_req->ctrl_data should be allocated by usba 2353 * only for IN requests. OUT request(e.g SET_REPORT) can have a 2354 * non-zero wLength value but ctrl_data would be allocated by 2355 * client for them. 2356 */ 2357 if (request_type & USB_DEV_REQ_DEV_TO_HOST) { 2358 length = hid_request->hid_req_data ? 2359 0 : hid_request->hid_req_wLength; 2360 } 2361 2362 if ((ctrl_req = usb_alloc_ctrl_req(hidp->hid_dip, length, 0)) == NULL) { 2363 USB_DPRINTF_L2(PRINT_MASK_ALL, hidp->hid_log_handle, 2364 "unable to alloc ctrl req. async trans failed"); 2365 mutex_enter(&hidp->hid_mutex); 2366 hidp->hid_default_pipe_req--; 2367 ASSERT(hidp->hid_default_pipe_req >= 0); 2368 mutex_exit(&hidp->hid_mutex); 2369 2370 /* 2371 * Some SET M_CTLs will have non-null 2372 * hid_req_data, free hid_req_data 2373 */ 2374 freemsg((mblk_t *)hid_request->hid_req_data); 2375 hid_request->hid_req_data = NULL; 2376 2377 return (USB_FAILURE); 2378 } 2379 2380 if (request_type & USB_DEV_REQ_HOST_TO_DEV) { 2381 ASSERT((length == 0) && (ctrl_req->ctrl_data == NULL)); 2382 } 2383 2384 ctrl_req->ctrl_bmRequestType = request_type; 2385 ctrl_req->ctrl_bRequest = (uint8_t)request_request; 2386 ctrl_req->ctrl_wValue = hid_request->hid_req_wValue; 2387 ctrl_req->ctrl_wIndex = request_index; 2388 ctrl_req->ctrl_wLength = hid_request->hid_req_wLength; 2389 ctrl_req->ctrl_data = ctrl_req->ctrl_data ? 2390 ctrl_req->ctrl_data : 2391 hid_request->hid_req_data; 2392 ctrl_req->ctrl_attributes = USB_ATTRS_AUTOCLEARING; 2393 ctrl_req->ctrl_client_private = (usb_opaque_t)hid_default_pipe_arg; 2394 ctrl_req->ctrl_cb = hid_default_pipe_callback; 2395 ctrl_req->ctrl_exc_cb = hid_default_pipe_exception_callback; 2396 2397 if ((rval = usb_pipe_ctrl_xfer(hidp->hid_default_pipe, 2398 ctrl_req, 0)) != USB_SUCCESS) { 2399 mutex_enter(&hidp->hid_mutex); 2400 hidp->hid_default_pipe_req--; 2401 ASSERT(hidp->hid_default_pipe_req >= 0); 2402 mutex_exit(&hidp->hid_mutex); 2403 2404 /* caller will free hid_req_data in case of failure */ 2405 ctrl_req->ctrl_data = NULL; 2406 usb_free_ctrl_req(ctrl_req); 2407 USB_DPRINTF_L2(PRINT_MASK_ALL, hidp->hid_log_handle, 2408 "usb_pipe_ctrl_xfer() failed. rval = %d", rval); 2409 2410 return (USB_FAILURE); 2411 } 2412 2413 return (USB_SUCCESS); 2414 } 2415 2416 2417 /* 2418 * hid_ioctl: 2419 * Hid currently doesn't handle any ioctls. NACK 2420 * the ioctl request. 2421 */ 2422 static void 2423 hid_ioctl(register queue_t *q, register mblk_t *mp) 2424 { 2425 register struct iocblk *iocp; 2426 2427 iocp = (struct iocblk *)mp->b_rptr; 2428 2429 iocp->ioc_rval = 0; 2430 2431 iocp->ioc_error = ENOTTY; 2432 2433 mp->b_datap->db_type = M_IOCNAK; 2434 2435 qreply(q, mp); 2436 } 2437 2438 2439 /* 2440 * hid_create_pm_components: 2441 * Create the pm components required for power management. 2442 * For keyboard/mouse, the components is created only if the device 2443 * supports a remote wakeup. 2444 * For other hid devices they are created unconditionally. 2445 */ 2446 static void 2447 hid_create_pm_components(dev_info_t *dip, hid_state_t *hidp) 2448 { 2449 hid_power_t *hidpm; 2450 uint_t pwr_states; 2451 2452 USB_DPRINTF_L4(PRINT_MASK_PM, hidp->hid_log_handle, 2453 "hid_create_pm_components: Begin"); 2454 2455 /* Allocate the state structure */ 2456 hidpm = kmem_zalloc(sizeof (hid_power_t), KM_SLEEP); 2457 hidp->hid_pm = hidpm; 2458 hidpm->hid_state = hidp; 2459 hidpm->hid_raise_power = B_FALSE; 2460 hidpm->hid_pm_capabilities = 0; 2461 hidpm->hid_current_power = USB_DEV_OS_FULL_PWR; 2462 2463 switch (hidp->hid_if_descr.bInterfaceProtocol) { 2464 case KEYBOARD_PROTOCOL: 2465 case MOUSE_PROTOCOL: 2466 hidpm->hid_pm_strategy = HID_PM_ACTIVITY; 2467 if ((hid_is_pm_enabled(dip) == USB_SUCCESS) && 2468 (usb_handle_remote_wakeup(dip, USB_REMOTE_WAKEUP_ENABLE) == 2469 USB_SUCCESS)) { 2470 2471 USB_DPRINTF_L3(PRINT_MASK_PM, hidp->hid_log_handle, 2472 "hid_create_pm_components: Remote Wakeup Enabled"); 2473 2474 if (usb_create_pm_components(dip, &pwr_states) == 2475 USB_SUCCESS) { 2476 hidpm->hid_wakeup_enabled = 1; 2477 hidpm->hid_pwr_states = (uint8_t)pwr_states; 2478 } 2479 } 2480 2481 break; 2482 default: 2483 hidpm->hid_pm_strategy = HID_PM_OPEN_CLOSE; 2484 if ((hid_is_pm_enabled(dip) == USB_SUCCESS) && 2485 (usb_create_pm_components(dip, &pwr_states) == 2486 USB_SUCCESS)) { 2487 hidpm->hid_wakeup_enabled = 0; 2488 hidpm->hid_pwr_states = (uint8_t)pwr_states; 2489 } 2490 2491 break; 2492 } 2493 2494 USB_DPRINTF_L4(PRINT_MASK_PM, hidp->hid_log_handle, 2495 "hid_create_pm_components: END"); 2496 } 2497 2498 2499 /* 2500 * hid_is_pm_enabled 2501 * Check if the device is pm enabled. Always enable 2502 * pm on the new SUN mouse 2503 */ 2504 static int 2505 hid_is_pm_enabled(dev_info_t *dip) 2506 { 2507 hid_state_t *hidp = ddi_get_soft_state(hid_statep, 2508 ddi_get_instance(dip)); 2509 2510 if (strcmp(ddi_node_name(dip), "mouse") == 0) { 2511 /* check for overrides first */ 2512 if (hid_pm_mouse || 2513 (ddi_prop_exists(DDI_DEV_T_ANY, dip, 2514 (DDI_PROP_DONTPASS | DDI_PROP_NOTPROM), 2515 "hid-mouse-pm-enable") == 1)) { 2516 2517 return (USB_SUCCESS); 2518 } 2519 2520 /* 2521 * Always enable PM for 1.05 or greater SUN mouse 2522 * hidp->hid_dev_descr won't be NULL. 2523 */ 2524 if ((hidp->hid_dev_descr->idVendor == 2525 HID_SUN_MOUSE_VENDOR_ID) && 2526 (hidp->hid_dev_descr->idProduct == 2527 HID_SUN_MOUSE_PROD_ID) && 2528 (hidp->hid_dev_descr->bcdDevice >= 2529 HID_SUN_MOUSE_BCDDEVICE)) { 2530 2531 return (USB_SUCCESS); 2532 } 2533 } else { 2534 2535 return (USB_SUCCESS); 2536 } 2537 2538 return (USB_FAILURE); 2539 } 2540 2541 2542 /* 2543 * hid_save_device_state 2544 * Save the current device/driver state. 2545 */ 2546 static void 2547 hid_save_device_state(hid_state_t *hidp) 2548 { 2549 struct iocblk *mctlmsg; 2550 mblk_t *mp; 2551 queue_t *q; 2552 2553 USB_DPRINTF_L4(PRINT_MASK_EVENTS, hidp->hid_log_handle, 2554 "hid_save_device_state"); 2555 2556 if (hidp->hid_streams_flags == HID_STREAMS_OPEN) { 2557 /* 2558 * Send an MCTL up indicating that 2559 * the device will loose its state 2560 */ 2561 q = hidp->hid_rq_ptr; 2562 mutex_exit(&hidp->hid_mutex); 2563 if (canputnext(q)) { 2564 mp = allocb(sizeof (struct iocblk), BPRI_HI); 2565 if (mp != NULL) { 2566 mp->b_datap->db_type = M_CTL; 2567 mctlmsg = (struct iocblk *)mp->b_datap->db_base; 2568 mctlmsg->ioc_cmd = HID_DISCONNECT_EVENT; 2569 mctlmsg->ioc_count = 0; 2570 putnext(q, mp); 2571 } 2572 } 2573 /* stop polling on the intr pipe */ 2574 usb_pipe_stop_intr_polling(hidp->hid_interrupt_pipe, 2575 USB_FLAGS_SLEEP); 2576 2577 mutex_enter(&hidp->hid_mutex); 2578 } 2579 } 2580 2581 2582 /* 2583 * hid_restore_device_state: 2584 * Set original configuration of the device. 2585 * Reopen intr pipe. 2586 * Enable wrq - this starts new transactions on the control pipe. 2587 */ 2588 static void 2589 hid_restore_device_state(dev_info_t *dip, hid_state_t *hidp) 2590 { 2591 int rval; 2592 queue_t *rdq, *wrq; 2593 hid_power_t *hidpm; 2594 struct iocblk *mctlmsg; 2595 mblk_t *mp; 2596 2597 hid_pm_busy_component(hidp); 2598 mutex_enter(&hidp->hid_mutex); 2599 2600 USB_DPRINTF_L4(PRINT_MASK_ATTA, hidp->hid_log_handle, 2601 "hid_restore_device_state: %s", 2602 usb_str_dev_state(hidp->hid_dev_state)); 2603 2604 hidpm = hidp->hid_pm; 2605 mutex_exit(&hidp->hid_mutex); 2606 2607 /* First bring the device to full power */ 2608 (void) pm_raise_power(dip, 0, USB_DEV_OS_FULL_PWR); 2609 2610 mutex_enter(&hidp->hid_mutex); 2611 if (hidp->hid_dev_state == USB_DEV_ONLINE) { 2612 /* 2613 * We failed the checkpoint, there is no need to restore 2614 * the device state 2615 */ 2616 mutex_exit(&hidp->hid_mutex); 2617 hid_pm_idle_component(hidp); 2618 2619 return; 2620 } 2621 mutex_exit(&hidp->hid_mutex); 2622 2623 2624 /* Check if we are talking to the same device */ 2625 if (usb_check_same_device(dip, hidp->hid_log_handle, USB_LOG_L2, 2626 PRINT_MASK_ALL, USB_CHK_BASIC|USB_CHK_CFG, NULL) != USB_SUCCESS) { 2627 2628 /* change the device state from suspended to disconnected */ 2629 mutex_enter(&hidp->hid_mutex); 2630 hidp->hid_dev_state = USB_DEV_DISCONNECTED; 2631 mutex_exit(&hidp->hid_mutex); 2632 hid_pm_idle_component(hidp); 2633 2634 return; 2635 } 2636 2637 hid_set_idle(hidp); 2638 hid_set_protocol(hidp, SET_REPORT_PROTOCOL); 2639 2640 mutex_enter(&hidp->hid_mutex); 2641 /* if the device had remote wakeup earlier, enable it again */ 2642 if (hidpm->hid_wakeup_enabled) { 2643 mutex_exit(&hidp->hid_mutex); 2644 2645 if ((rval = usb_handle_remote_wakeup(hidp->hid_dip, 2646 USB_REMOTE_WAKEUP_ENABLE)) != USB_SUCCESS) { 2647 USB_DPRINTF_L2(PRINT_MASK_ATTA, 2648 hidp->hid_log_handle, 2649 "usb_handle_remote_wakeup failed (%d)", rval); 2650 } 2651 2652 mutex_enter(&hidp->hid_mutex); 2653 } 2654 2655 /* 2656 * restart polling on the interrupt pipe only if the device 2657 * was previously operational (open) 2658 */ 2659 if (hidp->hid_streams_flags == HID_STREAMS_OPEN) { 2660 rdq = hidp->hid_rq_ptr; 2661 wrq = hidp->hid_wq_ptr; 2662 if ((rval = hid_start_intr_polling(hidp)) != USB_SUCCESS) { 2663 USB_DPRINTF_L3(PRINT_MASK_ATTA, hidp->hid_log_handle, 2664 "hid_restore_device_state:" 2665 "unable to restart intr pipe poll" 2666 " rval = %d ", rval); 2667 /* 2668 * change the device state from 2669 * suspended to disconnected 2670 */ 2671 hidp->hid_dev_state = USB_DEV_DISCONNECTED; 2672 mutex_exit(&hidp->hid_mutex); 2673 hid_pm_idle_component(hidp); 2674 2675 return; 2676 } 2677 2678 if (hidp->hid_dev_state == USB_DEV_DISCONNECTED) { 2679 USB_DPRINTF_L2(PRINT_MASK_EVENTS, hidp->hid_log_handle, 2680 "device is being re-connected"); 2681 } 2682 2683 /* set the device state ONLINE */ 2684 hidp->hid_dev_state = USB_DEV_ONLINE; 2685 mutex_exit(&hidp->hid_mutex); 2686 2687 /* inform upstream modules that the device is back */ 2688 if (canputnext(rdq)) { 2689 mp = allocb(sizeof (struct iocblk), BPRI_HI); 2690 if (mp != NULL) { 2691 mp->b_datap->db_type = M_CTL; 2692 mctlmsg = (struct iocblk *)mp->b_datap->db_base; 2693 mctlmsg->ioc_cmd = HID_CONNECT_EVENT; 2694 mctlmsg->ioc_count = 0; 2695 putnext(rdq, mp); 2696 } 2697 } 2698 /* enable write side q */ 2699 qenable(wrq); 2700 mutex_enter(&hidp->hid_mutex); 2701 } else { 2702 /* set the device state ONLINE */ 2703 hidp->hid_dev_state = USB_DEV_ONLINE; 2704 } 2705 2706 mutex_exit(&hidp->hid_mutex); 2707 hid_pm_idle_component(hidp); 2708 } 2709 2710 2711 /* 2712 * hid_qreply_merror: 2713 * Pass an error message up. 2714 */ 2715 static void 2716 hid_qreply_merror(queue_t *q, mblk_t *mp, uchar_t errval) 2717 { 2718 hid_req_t *hid_req = NULL; 2719 2720 mp->b_datap->db_type = M_ERROR; 2721 if (mp->b_cont) { 2722 hid_req = (hid_req_t *)mp->b_cont->b_rptr; 2723 freemsg((mblk_t *)hid_req->hid_req_data); 2724 freemsg(mp->b_cont); 2725 mp->b_cont = NULL; 2726 } 2727 mp->b_rptr = mp->b_datap->db_base; 2728 mp->b_wptr = mp->b_rptr + sizeof (char); 2729 *mp->b_rptr = errval; 2730 2731 qreply(q, mp); 2732 } 2733 2734 2735 /* 2736 * hid_data2mblk: 2737 * Form an mblk from the given data 2738 */ 2739 static mblk_t * 2740 hid_data2mblk(uchar_t *buf, int len) 2741 { 2742 mblk_t *mp = NULL; 2743 2744 if (len >= 0) { 2745 mp = allocb(len, BPRI_HI); 2746 if (mp) { 2747 bcopy(buf, mp->b_datap->db_base, len); 2748 mp->b_wptr += len; 2749 } 2750 } 2751 2752 return (mp); 2753 } 2754 2755 2756 /* 2757 * hid_flush : 2758 * Flush data already sent upstreams to client module. 2759 */ 2760 static void 2761 hid_flush(queue_t *q) 2762 { 2763 /* 2764 * Flush pending data already sent upstream 2765 */ 2766 if ((q != NULL) && (q->q_next != NULL)) { 2767 (void) putnextctl1(q, M_FLUSH, FLUSHR); 2768 } 2769 } 2770 2771 2772 static void 2773 hid_pm_busy_component(hid_state_t *hid_statep) 2774 { 2775 ASSERT(!mutex_owned(&hid_statep->hid_mutex)); 2776 2777 if (hid_statep->hid_pm != NULL) { 2778 mutex_enter(&hid_statep->hid_mutex); 2779 hid_statep->hid_pm->hid_pm_busy++; 2780 2781 USB_DPRINTF_L4(PRINT_MASK_PM, hid_statep->hid_log_handle, 2782 "hid_pm_busy_component: %d", 2783 hid_statep->hid_pm->hid_pm_busy); 2784 2785 mutex_exit(&hid_statep->hid_mutex); 2786 if (pm_busy_component(hid_statep->hid_dip, 0) != DDI_SUCCESS) { 2787 mutex_enter(&hid_statep->hid_mutex); 2788 hid_statep->hid_pm->hid_pm_busy--; 2789 2790 USB_DPRINTF_L2(PRINT_MASK_PM, 2791 hid_statep->hid_log_handle, 2792 "hid_pm_busy_component failed: %d", 2793 hid_statep->hid_pm->hid_pm_busy); 2794 2795 mutex_exit(&hid_statep->hid_mutex); 2796 } 2797 2798 } 2799 } 2800 2801 2802 static void 2803 hid_pm_idle_component(hid_state_t *hid_statep) 2804 { 2805 ASSERT(!mutex_owned(&hid_statep->hid_mutex)); 2806 2807 if (hid_statep->hid_pm != NULL) { 2808 if (pm_idle_component(hid_statep->hid_dip, 0) == DDI_SUCCESS) { 2809 mutex_enter(&hid_statep->hid_mutex); 2810 ASSERT(hid_statep->hid_pm->hid_pm_busy > 0); 2811 hid_statep->hid_pm->hid_pm_busy--; 2812 2813 USB_DPRINTF_L4(PRINT_MASK_PM, 2814 hid_statep->hid_log_handle, 2815 "hid_pm_idle_component: %d", 2816 hid_statep->hid_pm->hid_pm_busy); 2817 2818 mutex_exit(&hid_statep->hid_mutex); 2819 } 2820 } 2821 } 2822 2823 2824 /* 2825 * hid_pwrlvl0: 2826 * Functions to handle power transition for various levels 2827 * These functions act as place holders to issue USB commands 2828 * to the devices to change their power levels 2829 */ 2830 static int 2831 hid_pwrlvl0(hid_state_t *hidp) 2832 { 2833 hid_power_t *hidpm; 2834 int rval; 2835 struct iocblk *mctlmsg; 2836 mblk_t *mp_lowpwr, *mp_fullpwr; 2837 queue_t *q; 2838 2839 hidpm = hidp->hid_pm; 2840 2841 switch (hidp->hid_dev_state) { 2842 case USB_DEV_ONLINE: 2843 /* Deny the powerdown request if the device is busy */ 2844 if (hidpm->hid_pm_busy != 0) { 2845 2846 return (USB_FAILURE); 2847 } 2848 2849 if (hidp->hid_streams_flags == HID_STREAMS_OPEN) { 2850 q = hidp->hid_rq_ptr; 2851 mutex_exit(&hidp->hid_mutex); 2852 if (canputnext(q)) { 2853 /* try to preallocate mblks */ 2854 mp_lowpwr = allocb( 2855 (int)sizeof (struct iocblk), BPRI_HI); 2856 mp_fullpwr = allocb( 2857 (int)sizeof (struct iocblk), BPRI_HI); 2858 if ((mp_lowpwr != NULL) && 2859 (mp_fullpwr != NULL)) { 2860 /* stop polling */ 2861 usb_pipe_stop_intr_polling( 2862 hidp->hid_interrupt_pipe, 2863 USB_FLAGS_SLEEP); 2864 2865 /* 2866 * Send an MCTL up indicating that 2867 * we are powering off 2868 */ 2869 mp_lowpwr->b_datap->db_type = M_CTL; 2870 mctlmsg = (struct iocblk *) 2871 mp_lowpwr->b_datap->db_base; 2872 mctlmsg->ioc_cmd = HID_POWER_OFF; 2873 mctlmsg->ioc_count = 0; 2874 putnext(q, mp_lowpwr); 2875 2876 /* save the full powr mblk */ 2877 mutex_enter(&hidp->hid_mutex); 2878 hidpm->hid_pm_pwrup = mp_fullpwr; 2879 } else { 2880 /* 2881 * Since we failed to allocate one 2882 * or more mblks, we fail attempt 2883 * to go into low power this time 2884 */ 2885 freemsg(mp_lowpwr); 2886 freemsg(mp_fullpwr); 2887 mutex_enter(&hidp->hid_mutex); 2888 2889 return (USB_FAILURE); 2890 } 2891 } else { 2892 /* 2893 * Since we can't send an mblk up, 2894 * we fail this attempt to go to low power 2895 */ 2896 mutex_enter(&hidp->hid_mutex); 2897 2898 return (USB_FAILURE); 2899 } 2900 } 2901 mutex_exit(&hidp->hid_mutex); 2902 /* Issue USB D3 command to the device here */ 2903 rval = usb_set_device_pwrlvl3(hidp->hid_dip); 2904 ASSERT(rval == USB_SUCCESS); 2905 2906 mutex_enter(&hidp->hid_mutex); 2907 hidp->hid_dev_state = USB_DEV_PWRED_DOWN; 2908 hidpm->hid_current_power = USB_DEV_OS_PWR_OFF; 2909 2910 /* FALLTHRU */ 2911 case USB_DEV_DISCONNECTED: 2912 case USB_DEV_SUSPENDED: 2913 case USB_DEV_PWRED_DOWN: 2914 default: 2915 break; 2916 } 2917 2918 return (USB_SUCCESS); 2919 } 2920 2921 2922 /* ARGSUSED */ 2923 static int 2924 hid_pwrlvl1(hid_state_t *hidp) 2925 { 2926 int rval; 2927 2928 /* Issue USB D2 command to the device here */ 2929 rval = usb_set_device_pwrlvl2(hidp->hid_dip); 2930 ASSERT(rval == USB_SUCCESS); 2931 2932 return (USB_FAILURE); 2933 } 2934 2935 2936 /* ARGSUSED */ 2937 static int 2938 hid_pwrlvl2(hid_state_t *hidp) 2939 { 2940 int rval; 2941 2942 rval = usb_set_device_pwrlvl1(hidp->hid_dip); 2943 ASSERT(rval == USB_SUCCESS); 2944 2945 return (USB_FAILURE); 2946 } 2947 2948 2949 static int 2950 hid_pwrlvl3(hid_state_t *hidp) 2951 { 2952 hid_power_t *hidpm; 2953 int rval; 2954 struct iocblk *mctlmsg; 2955 mblk_t *mp; 2956 queue_t *q; 2957 2958 hidpm = hidp->hid_pm; 2959 2960 switch (hidp->hid_dev_state) { 2961 case USB_DEV_HID_POWER_CHANGE: 2962 case USB_DEV_PWRED_DOWN: 2963 /* Issue USB D0 command to the device here */ 2964 rval = usb_set_device_pwrlvl0(hidp->hid_dip); 2965 ASSERT(rval == USB_SUCCESS); 2966 2967 if (hidp->hid_streams_flags == HID_STREAMS_OPEN) { 2968 /* restart polling on intr pipe */ 2969 rval = hid_start_intr_polling(hidp); 2970 if (rval != USB_SUCCESS) { 2971 USB_DPRINTF_L2(PRINT_MASK_EVENTS, 2972 hidp->hid_log_handle, 2973 "unable to restart intr polling rval = %d", 2974 rval); 2975 2976 return (USB_FAILURE); 2977 } 2978 2979 /* Send an MCTL up indicating device in full power */ 2980 q = hidp->hid_rq_ptr; 2981 mp = hidpm->hid_pm_pwrup; 2982 hidpm->hid_pm_pwrup = NULL; 2983 mutex_exit(&hidp->hid_mutex); 2984 if (canputnext(q)) { 2985 mp->b_datap->db_type = M_CTL; 2986 mctlmsg = (struct iocblk *)mp->b_datap->db_base; 2987 mctlmsg->ioc_cmd = HID_FULL_POWER; 2988 mctlmsg->ioc_count = 0; 2989 putnext(q, mp); 2990 } else { 2991 freemsg(mp); 2992 } 2993 mutex_enter(&hidp->hid_mutex); 2994 } 2995 hidp->hid_dev_state = USB_DEV_ONLINE; 2996 hidpm->hid_current_power = USB_DEV_OS_FULL_PWR; 2997 2998 /* FALLTHRU */ 2999 case USB_DEV_DISCONNECTED: 3000 case USB_DEV_SUSPENDED: 3001 case USB_DEV_ONLINE: 3002 3003 return (USB_SUCCESS); 3004 default: 3005 USB_DPRINTF_L2(PRINT_MASK_EVENTS, hidp->hid_log_handle, 3006 "hid_pwrlvl3: Improper State"); 3007 3008 return (USB_FAILURE); 3009 } 3010 } 3011 3012 3013 /* 3014 * hid_polled_input_init : 3015 * This routine calls down to the lower layers to initialize any state 3016 * information. This routine initializes the lower layers for input. 3017 */ 3018 static int 3019 hid_polled_input_init(hid_state_t *hidp) 3020 { 3021 USB_DPRINTF_L4(PRINT_MASK_ALL, hidp->hid_log_handle, 3022 "hid_polled_input_init"); 3023 3024 /* 3025 * Call the lower layers to intialize any state information 3026 * that they will need to provide the polled characters. 3027 */ 3028 if (usb_console_input_init(hidp->hid_dip, hidp->hid_interrupt_pipe, 3029 &hidp->hid_polled_raw_buf, 3030 &hidp->hid_polled_console_info) != USB_SUCCESS) { 3031 /* 3032 * If for some reason the lower layers cannot initialized, then 3033 * bail. 3034 */ 3035 (void) hid_polled_input_fini(hidp); 3036 3037 return (USB_FAILURE); 3038 } 3039 3040 return (USB_SUCCESS); 3041 } 3042 3043 3044 /* 3045 * hid_polled_input_fini: 3046 * This routine is called when we are done using this device as an input 3047 * device. 3048 */ 3049 static int 3050 hid_polled_input_fini(hid_state_t *hidp) 3051 { 3052 USB_DPRINTF_L4(PRINT_MASK_ALL, hidp->hid_log_handle, 3053 "hid_polled_input_fini"); 3054 3055 /* 3056 * Call the lower layers to free any state information 3057 * only if polled input has been initialised. 3058 */ 3059 if ((hidp->hid_polled_console_info) && 3060 (usb_console_input_fini(hidp->hid_polled_console_info) != 3061 USB_SUCCESS)) { 3062 3063 return (USB_FAILURE); 3064 } 3065 hidp->hid_polled_console_info = NULL; 3066 3067 return (USB_SUCCESS); 3068 } 3069 3070 3071 /* 3072 * hid_polled_input_enter: 3073 * This is the routine that is called in polled mode to save the USB 3074 * state information before using the USB keyboard as an input device. 3075 * This routine, and all of the routines that it calls, are responsible 3076 * for saving any state information so that it can be restored when 3077 * polling mode is over. 3078 */ 3079 static int 3080 /* ARGSUSED */ 3081 hid_polled_input_enter(hid_polled_handle_t hid_polled_inputp) 3082 { 3083 hid_state_t *hidp = (hid_state_t *)hid_polled_inputp; 3084 3085 /* 3086 * Call the lower layers to tell them to save any state information. 3087 */ 3088 (void) usb_console_input_enter(hidp->hid_polled_console_info); 3089 3090 return (USB_SUCCESS); 3091 } 3092 3093 3094 /* 3095 * hid_polled_read : 3096 * This is the routine that is called in polled mode when it wants to read 3097 * a character. We will call to the lower layers to see if there is any 3098 * input data available. If there is USB scancodes available, we will 3099 * give them back. 3100 */ 3101 static int 3102 hid_polled_read(hid_polled_handle_t hid_polled_input, uchar_t **buffer) 3103 { 3104 hid_state_t *hidp = (hid_state_t *)hid_polled_input; 3105 uint_t num_bytes; 3106 3107 /* 3108 * Call the lower layers to get the character from the controller. 3109 * The lower layers will return the number of characters that 3110 * were put in the raw buffer. The address of the raw buffer 3111 * was passed down to the lower layers during hid_polled_init. 3112 */ 3113 if (usb_console_read(hidp->hid_polled_console_info, 3114 &num_bytes) != USB_SUCCESS) { 3115 3116 return (0); 3117 } 3118 3119 /*LINTED*/ 3120 _NOTE(NO_COMPETING_THREADS_NOW); 3121 3122 *buffer = hidp->hid_polled_raw_buf; 3123 3124 /*LINTED*/ 3125 _NOTE(COMPETING_THREADS_NOW); 3126 3127 /* 3128 * Return the number of characters that were copied into the 3129 * polled buffer. 3130 */ 3131 return (num_bytes); 3132 } 3133 3134 3135 /* 3136 * hid_polled_input_exit : 3137 * This is the routine that is called in polled mode when it is giving up 3138 * control of the USB keyboard. This routine, and the lower layer routines 3139 * that it calls, are responsible for restoring the controller state to the 3140 * state it was in before polled mode. 3141 */ 3142 static int 3143 hid_polled_input_exit(hid_polled_handle_t hid_polled_inputp) 3144 { 3145 hid_state_t *hidp = (hid_state_t *)hid_polled_inputp; 3146 3147 /* 3148 * Call the lower layers to restore any state information. 3149 */ 3150 (void) usb_console_input_exit(hidp->hid_polled_console_info); 3151 3152 return (0); 3153 } 3154