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_L2(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_L2(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_L2(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 hid_vid_pid_t hid_vid_pid; 2089 2090 USB_DPRINTF_L4(PRINT_MASK_ALL, hidp->hid_log_handle, 2091 "hid_mctl_receive"); 2092 2093 iocp = (struct iocblk *)mp->b_rptr; 2094 2095 switch (iocp->ioc_cmd) { 2096 case HID_SET_REPORT: 2097 /* FALLTHRU */ 2098 case HID_SET_IDLE: 2099 /* FALLTHRU */ 2100 case HID_SET_PROTOCOL: 2101 request_type = USB_DEV_REQ_HOST_TO_DEV | 2102 USB_DEV_REQ_RCPT_IF | USB_DEV_REQ_TYPE_CLASS; 2103 2104 break; 2105 case HID_GET_REPORT: 2106 /* FALLTHRU */ 2107 case HID_GET_IDLE: 2108 /* FALLTHRU */ 2109 case HID_GET_PROTOCOL: 2110 request_type = USB_DEV_REQ_DEV_TO_HOST | 2111 USB_DEV_REQ_RCPT_IF | USB_DEV_REQ_TYPE_CLASS; 2112 2113 break; 2114 case HID_GET_PARSER_HANDLE: 2115 if (canputnext(RD(q))) { 2116 freemsg(mp->b_cont); 2117 mp->b_cont = hid_data2mblk( 2118 (uchar_t *)&hidp->hid_report_descr, 2119 sizeof (hidp->hid_report_descr)); 2120 if (mp->b_cont == NULL) { 2121 /* 2122 * can't allocate mblk, indicate 2123 * that nothing is returned 2124 */ 2125 iocp->ioc_count = 0; 2126 } else { 2127 iocp->ioc_count = 2128 sizeof (hidp->hid_report_descr); 2129 } 2130 qreply(q, mp); 2131 2132 return (HID_SUCCESS); 2133 } else { 2134 2135 /* retry */ 2136 return (HID_ENQUEUE); 2137 } 2138 case HID_GET_VID_PID: 2139 if (canputnext(RD(q))) { 2140 freemsg(mp->b_cont); 2141 2142 hid_vid_pid.VendorId = 2143 hidp->hid_dev_descr->idVendor; 2144 hid_vid_pid.ProductId = 2145 hidp->hid_dev_descr->idProduct; 2146 2147 mp->b_cont = hid_data2mblk( 2148 (uchar_t *)&hid_vid_pid, sizeof (hid_vid_pid_t)); 2149 if (mp->b_cont == NULL) { 2150 /* 2151 * can't allocate mblk, indicate that nothing 2152 * is being returned. 2153 */ 2154 iocp->ioc_count = 0; 2155 } else { 2156 iocp->ioc_count = 2157 sizeof (hid_vid_pid_t); 2158 } 2159 qreply(q, mp); 2160 2161 return (HID_SUCCESS); 2162 } else { 2163 2164 /* retry */ 2165 return (HID_ENQUEUE); 2166 } 2167 case HID_OPEN_POLLED_INPUT: 2168 if (canputnext(RD(q))) { 2169 freemsg(mp->b_cont); 2170 2171 /* Initialize the structure */ 2172 hid_polled_input.hid_polled_version = 2173 HID_POLLED_INPUT_V0; 2174 hid_polled_input.hid_polled_read = hid_polled_read; 2175 hid_polled_input.hid_polled_input_enter = 2176 hid_polled_input_enter; 2177 hid_polled_input.hid_polled_input_exit = 2178 hid_polled_input_exit; 2179 hid_polled_input.hid_polled_input_handle = 2180 (hid_polled_handle_t)hidp; 2181 2182 mp->b_cont = hid_data2mblk( 2183 (uchar_t *)&hid_polled_input, 2184 sizeof (hid_polled_input_callback_t)); 2185 if (mp->b_cont == NULL) { 2186 /* 2187 * can't allocate mblk, indicate that nothing 2188 * is being returned. 2189 */ 2190 iocp->ioc_count = 0; 2191 } else { 2192 /* Call down into USBA */ 2193 (void) hid_polled_input_init(hidp); 2194 2195 iocp->ioc_count = 2196 sizeof (hid_polled_input_callback_t); 2197 } 2198 qreply(q, mp); 2199 2200 return (HID_SUCCESS); 2201 } else { 2202 2203 /* retry */ 2204 return (HID_ENQUEUE); 2205 } 2206 case HID_CLOSE_POLLED_INPUT: 2207 /* Call down into USBA */ 2208 (void) hid_polled_input_fini(hidp); 2209 2210 iocp->ioc_count = 0; 2211 qreply(q, mp); 2212 2213 return (HID_SUCCESS); 2214 default: 2215 hid_qreply_merror(q, mp, EINVAL); 2216 2217 return (HID_FAILURE); 2218 } 2219 2220 /* 2221 * These (device executable) commands require a hid_req_t. 2222 * Make sure one is present 2223 */ 2224 if (mp->b_cont == NULL) { 2225 hid_qreply_merror(q, mp, EINVAL); 2226 2227 return (error); 2228 } else { 2229 hid_req_data = (hid_req_t *)mp->b_cont->b_rptr; 2230 if ((iocp->ioc_cmd == HID_SET_REPORT) && 2231 (hid_req_data->hid_req_data == NULL)) { 2232 hid_qreply_merror(q, mp, EINVAL); 2233 2234 return (error); 2235 } 2236 } 2237 2238 /* 2239 * Check is version no. is correct. This 2240 * is coming from the user 2241 */ 2242 if (hid_req_data->hid_req_version_no != HID_VERSION_V_0) { 2243 hid_qreply_merror(q, mp, EINVAL); 2244 2245 return (error); 2246 } 2247 2248 mutex_enter(&hidp->hid_mutex); 2249 USB_DPRINTF_L4(PRINT_MASK_ALL, hidp->hid_log_handle, 2250 "hid_mctl_receive: dev_state=%s", 2251 usb_str_dev_state(hidp->hid_dev_state)); 2252 2253 switch (hidp->hid_dev_state) { 2254 case USB_DEV_PWRED_DOWN: 2255 /* 2256 * get the device full powered. We get a callback 2257 * which enables the WQ and kicks off IO 2258 */ 2259 hidp->hid_dev_state = USB_DEV_HID_POWER_CHANGE; 2260 mutex_exit(&hidp->hid_mutex); 2261 if (usb_req_raise_power(hidp->hid_dip, 0, 2262 USB_DEV_OS_FULL_PWR, hid_power_change_callback, 2263 hidp, 0) != USB_SUCCESS) { 2264 /* we retry raising power in wsrv */ 2265 mutex_enter(&hidp->hid_mutex); 2266 hidp->hid_dev_state = USB_DEV_PWRED_DOWN; 2267 mutex_exit(&hidp->hid_mutex); 2268 } 2269 error = HID_ENQUEUE; 2270 2271 break; 2272 case USB_DEV_HID_POWER_CHANGE: 2273 mutex_exit(&hidp->hid_mutex); 2274 error = HID_ENQUEUE; 2275 2276 break; 2277 case USB_DEV_ONLINE: 2278 if (hidp->hid_streams_flags != HID_STREAMS_DISMANTLING) { 2279 /* Send a message down */ 2280 mutex_exit(&hidp->hid_mutex); 2281 error = hid_mctl_execute_cmd(hidp, request_type, 2282 hid_req_data, mp); 2283 if (error == HID_FAILURE) { 2284 hid_qreply_merror(q, mp, EIO); 2285 } 2286 } else { 2287 mutex_exit(&hidp->hid_mutex); 2288 hid_qreply_merror(q, mp, EIO); 2289 } 2290 2291 break; 2292 default: 2293 mutex_exit(&hidp->hid_mutex); 2294 hid_qreply_merror(q, mp, EIO); 2295 2296 break; 2297 } 2298 2299 return (error); 2300 } 2301 2302 2303 /* 2304 * hid_mctl_execute_cmd: 2305 * Send the command to the device. 2306 */ 2307 static int 2308 hid_mctl_execute_cmd(hid_state_t *hidp, int request_type, 2309 hid_req_t *hid_req_data, mblk_t *mp) 2310 { 2311 int request_index; 2312 struct iocblk *iocp; 2313 hid_default_pipe_arg_t *def_pipe_arg; 2314 2315 iocp = (struct iocblk *)mp->b_rptr; 2316 USB_DPRINTF_L4(PRINT_MASK_ALL, hidp->hid_log_handle, 2317 "hid_mctl_execute_cmd: iocp=0x%p", iocp); 2318 2319 request_index = hidp->hid_if_descr.bInterfaceNumber; 2320 2321 /* 2322 * Set up the argument to be passed back to hid 2323 * when the asynchronous control callback is 2324 * executed. 2325 */ 2326 def_pipe_arg = kmem_zalloc(sizeof (hid_default_pipe_arg_t), 0); 2327 2328 if (def_pipe_arg == NULL) { 2329 2330 return (HID_FAILURE); 2331 } 2332 2333 def_pipe_arg->hid_default_pipe_arg_mctlmsg.ioc_cmd = iocp->ioc_cmd; 2334 def_pipe_arg->hid_default_pipe_arg_mctlmsg.ioc_count = 0; 2335 def_pipe_arg->hid_default_pipe_arg_hidp = hidp; 2336 def_pipe_arg->hid_default_pipe_arg_mblk = mp; 2337 2338 /* 2339 * Send the command down to USBA through default 2340 * pipe. 2341 */ 2342 if (hid_send_async_ctrl_request(hidp, hid_req_data, 2343 request_type, iocp->ioc_cmd, 2344 request_index, def_pipe_arg) != USB_SUCCESS) { 2345 2346 kmem_free(def_pipe_arg, sizeof (hid_default_pipe_arg_t)); 2347 2348 return (HID_FAILURE); 2349 } 2350 2351 return (HID_INPROGRESS); 2352 } 2353 2354 2355 /* 2356 * hid_send_async_ctrl_request: 2357 * Send an asynchronous control request to USBA. Since hid is a STREAMS 2358 * driver, it is not allowed to wait in its entry points except for the 2359 * open and close entry points. Therefore, hid must use the asynchronous 2360 * USBA calls. 2361 */ 2362 static int 2363 hid_send_async_ctrl_request(hid_state_t *hidp, hid_req_t *hid_request, 2364 uchar_t request_type, int request_request, 2365 ushort_t request_index, 2366 hid_default_pipe_arg_t *hid_default_pipe_arg) 2367 { 2368 usb_ctrl_req_t *ctrl_req; 2369 int rval; 2370 size_t length = 0; 2371 2372 USB_DPRINTF_L4(PRINT_MASK_ALL, hidp->hid_log_handle, 2373 "hid_send_async_ctrl_request: " 2374 "rq_type=%d rq_rq=%d index=%d", 2375 request_type, request_request, request_index); 2376 2377 mutex_enter(&hidp->hid_mutex); 2378 hidp->hid_default_pipe_req++; 2379 mutex_exit(&hidp->hid_mutex); 2380 2381 /* 2382 * Note that ctrl_req->ctrl_data should be allocated by usba 2383 * only for IN requests. OUT request(e.g SET_REPORT) can have a 2384 * non-zero wLength value but ctrl_data would be allocated by 2385 * client for them. 2386 */ 2387 if (request_type & USB_DEV_REQ_DEV_TO_HOST) { 2388 length = hid_request->hid_req_data ? 2389 0 : hid_request->hid_req_wLength; 2390 } 2391 2392 if ((ctrl_req = usb_alloc_ctrl_req(hidp->hid_dip, length, 0)) == NULL) { 2393 USB_DPRINTF_L2(PRINT_MASK_ALL, hidp->hid_log_handle, 2394 "unable to alloc ctrl req. async trans failed"); 2395 mutex_enter(&hidp->hid_mutex); 2396 hidp->hid_default_pipe_req--; 2397 ASSERT(hidp->hid_default_pipe_req >= 0); 2398 mutex_exit(&hidp->hid_mutex); 2399 2400 /* 2401 * Some SET M_CTLs will have non-null 2402 * hid_req_data, free hid_req_data 2403 */ 2404 freemsg((mblk_t *)hid_request->hid_req_data); 2405 hid_request->hid_req_data = NULL; 2406 2407 return (USB_FAILURE); 2408 } 2409 2410 if (request_type & USB_DEV_REQ_HOST_TO_DEV) { 2411 ASSERT((length == 0) && (ctrl_req->ctrl_data == NULL)); 2412 } 2413 2414 ctrl_req->ctrl_bmRequestType = request_type; 2415 ctrl_req->ctrl_bRequest = (uint8_t)request_request; 2416 ctrl_req->ctrl_wValue = hid_request->hid_req_wValue; 2417 ctrl_req->ctrl_wIndex = request_index; 2418 ctrl_req->ctrl_wLength = hid_request->hid_req_wLength; 2419 ctrl_req->ctrl_data = ctrl_req->ctrl_data ? 2420 ctrl_req->ctrl_data : 2421 hid_request->hid_req_data; 2422 ctrl_req->ctrl_attributes = USB_ATTRS_AUTOCLEARING; 2423 ctrl_req->ctrl_client_private = (usb_opaque_t)hid_default_pipe_arg; 2424 ctrl_req->ctrl_cb = hid_default_pipe_callback; 2425 ctrl_req->ctrl_exc_cb = hid_default_pipe_exception_callback; 2426 2427 if ((rval = usb_pipe_ctrl_xfer(hidp->hid_default_pipe, 2428 ctrl_req, 0)) != USB_SUCCESS) { 2429 mutex_enter(&hidp->hid_mutex); 2430 hidp->hid_default_pipe_req--; 2431 ASSERT(hidp->hid_default_pipe_req >= 0); 2432 mutex_exit(&hidp->hid_mutex); 2433 2434 /* caller will free hid_req_data in case of failure */ 2435 ctrl_req->ctrl_data = NULL; 2436 usb_free_ctrl_req(ctrl_req); 2437 USB_DPRINTF_L2(PRINT_MASK_ALL, hidp->hid_log_handle, 2438 "usb_pipe_ctrl_xfer() failed. rval = %d", rval); 2439 2440 return (USB_FAILURE); 2441 } 2442 2443 return (USB_SUCCESS); 2444 } 2445 2446 2447 /* 2448 * hid_ioctl: 2449 * Hid currently doesn't handle any ioctls. NACK 2450 * the ioctl request. 2451 */ 2452 static void 2453 hid_ioctl(register queue_t *q, register mblk_t *mp) 2454 { 2455 register struct iocblk *iocp; 2456 2457 iocp = (struct iocblk *)mp->b_rptr; 2458 2459 iocp->ioc_rval = 0; 2460 2461 iocp->ioc_error = ENOTTY; 2462 2463 mp->b_datap->db_type = M_IOCNAK; 2464 2465 qreply(q, mp); 2466 } 2467 2468 2469 /* 2470 * hid_create_pm_components: 2471 * Create the pm components required for power management. 2472 * For keyboard/mouse, the components is created only if the device 2473 * supports a remote wakeup. 2474 * For other hid devices they are created unconditionally. 2475 */ 2476 static void 2477 hid_create_pm_components(dev_info_t *dip, hid_state_t *hidp) 2478 { 2479 hid_power_t *hidpm; 2480 uint_t pwr_states; 2481 2482 USB_DPRINTF_L4(PRINT_MASK_PM, hidp->hid_log_handle, 2483 "hid_create_pm_components: Begin"); 2484 2485 /* Allocate the state structure */ 2486 hidpm = kmem_zalloc(sizeof (hid_power_t), KM_SLEEP); 2487 hidp->hid_pm = hidpm; 2488 hidpm->hid_state = hidp; 2489 hidpm->hid_raise_power = B_FALSE; 2490 hidpm->hid_pm_capabilities = 0; 2491 hidpm->hid_current_power = USB_DEV_OS_FULL_PWR; 2492 2493 switch (hidp->hid_if_descr.bInterfaceProtocol) { 2494 case KEYBOARD_PROTOCOL: 2495 case MOUSE_PROTOCOL: 2496 hidpm->hid_pm_strategy = HID_PM_ACTIVITY; 2497 if ((hid_is_pm_enabled(dip) == USB_SUCCESS) && 2498 (usb_handle_remote_wakeup(dip, USB_REMOTE_WAKEUP_ENABLE) == 2499 USB_SUCCESS)) { 2500 2501 USB_DPRINTF_L3(PRINT_MASK_PM, hidp->hid_log_handle, 2502 "hid_create_pm_components: Remote Wakeup Enabled"); 2503 2504 if (usb_create_pm_components(dip, &pwr_states) == 2505 USB_SUCCESS) { 2506 hidpm->hid_wakeup_enabled = 1; 2507 hidpm->hid_pwr_states = (uint8_t)pwr_states; 2508 } 2509 } 2510 2511 break; 2512 default: 2513 hidpm->hid_pm_strategy = HID_PM_OPEN_CLOSE; 2514 if ((hid_is_pm_enabled(dip) == USB_SUCCESS) && 2515 (usb_create_pm_components(dip, &pwr_states) == 2516 USB_SUCCESS)) { 2517 hidpm->hid_wakeup_enabled = 0; 2518 hidpm->hid_pwr_states = (uint8_t)pwr_states; 2519 } 2520 2521 break; 2522 } 2523 2524 USB_DPRINTF_L4(PRINT_MASK_PM, hidp->hid_log_handle, 2525 "hid_create_pm_components: END"); 2526 } 2527 2528 2529 /* 2530 * hid_is_pm_enabled 2531 * Check if the device is pm enabled. Always enable 2532 * pm on the new SUN mouse 2533 */ 2534 static int 2535 hid_is_pm_enabled(dev_info_t *dip) 2536 { 2537 hid_state_t *hidp = ddi_get_soft_state(hid_statep, 2538 ddi_get_instance(dip)); 2539 2540 if (strcmp(ddi_node_name(dip), "mouse") == 0) { 2541 /* check for overrides first */ 2542 if (hid_pm_mouse || 2543 (ddi_prop_exists(DDI_DEV_T_ANY, dip, 2544 (DDI_PROP_DONTPASS | DDI_PROP_NOTPROM), 2545 "hid-mouse-pm-enable") == 1)) { 2546 2547 return (USB_SUCCESS); 2548 } 2549 2550 /* 2551 * Always enable PM for 1.05 or greater SUN mouse 2552 * hidp->hid_dev_descr won't be NULL. 2553 */ 2554 if ((hidp->hid_dev_descr->idVendor == 2555 HID_SUN_MOUSE_VENDOR_ID) && 2556 (hidp->hid_dev_descr->idProduct == 2557 HID_SUN_MOUSE_PROD_ID) && 2558 (hidp->hid_dev_descr->bcdDevice >= 2559 HID_SUN_MOUSE_BCDDEVICE)) { 2560 2561 return (USB_SUCCESS); 2562 } 2563 } else { 2564 2565 return (USB_SUCCESS); 2566 } 2567 2568 return (USB_FAILURE); 2569 } 2570 2571 2572 /* 2573 * hid_save_device_state 2574 * Save the current device/driver state. 2575 */ 2576 static void 2577 hid_save_device_state(hid_state_t *hidp) 2578 { 2579 struct iocblk *mctlmsg; 2580 mblk_t *mp; 2581 queue_t *q; 2582 2583 USB_DPRINTF_L4(PRINT_MASK_EVENTS, hidp->hid_log_handle, 2584 "hid_save_device_state"); 2585 2586 if (hidp->hid_streams_flags == HID_STREAMS_OPEN) { 2587 /* 2588 * Send an MCTL up indicating that 2589 * the device will loose its state 2590 */ 2591 q = hidp->hid_rq_ptr; 2592 mutex_exit(&hidp->hid_mutex); 2593 if (canputnext(q)) { 2594 mp = allocb(sizeof (struct iocblk), BPRI_HI); 2595 if (mp != NULL) { 2596 mp->b_datap->db_type = M_CTL; 2597 mctlmsg = (struct iocblk *)mp->b_datap->db_base; 2598 mctlmsg->ioc_cmd = HID_DISCONNECT_EVENT; 2599 mctlmsg->ioc_count = 0; 2600 putnext(q, mp); 2601 } 2602 } 2603 /* stop polling on the intr pipe */ 2604 usb_pipe_stop_intr_polling(hidp->hid_interrupt_pipe, 2605 USB_FLAGS_SLEEP); 2606 2607 mutex_enter(&hidp->hid_mutex); 2608 } 2609 } 2610 2611 2612 /* 2613 * hid_restore_device_state: 2614 * Set original configuration of the device. 2615 * Reopen intr pipe. 2616 * Enable wrq - this starts new transactions on the control pipe. 2617 */ 2618 static void 2619 hid_restore_device_state(dev_info_t *dip, hid_state_t *hidp) 2620 { 2621 int rval; 2622 queue_t *rdq, *wrq; 2623 hid_power_t *hidpm; 2624 struct iocblk *mctlmsg; 2625 mblk_t *mp; 2626 2627 hid_pm_busy_component(hidp); 2628 mutex_enter(&hidp->hid_mutex); 2629 2630 USB_DPRINTF_L4(PRINT_MASK_ATTA, hidp->hid_log_handle, 2631 "hid_restore_device_state: %s", 2632 usb_str_dev_state(hidp->hid_dev_state)); 2633 2634 hidpm = hidp->hid_pm; 2635 mutex_exit(&hidp->hid_mutex); 2636 2637 /* First bring the device to full power */ 2638 (void) pm_raise_power(dip, 0, USB_DEV_OS_FULL_PWR); 2639 2640 mutex_enter(&hidp->hid_mutex); 2641 if (hidp->hid_dev_state == USB_DEV_ONLINE) { 2642 /* 2643 * We failed the checkpoint, there is no need to restore 2644 * the device state 2645 */ 2646 mutex_exit(&hidp->hid_mutex); 2647 hid_pm_idle_component(hidp); 2648 2649 return; 2650 } 2651 mutex_exit(&hidp->hid_mutex); 2652 2653 2654 /* Check if we are talking to the same device */ 2655 if (usb_check_same_device(dip, hidp->hid_log_handle, USB_LOG_L2, 2656 PRINT_MASK_ALL, USB_CHK_BASIC|USB_CHK_CFG, NULL) != USB_SUCCESS) { 2657 2658 /* change the device state from suspended to disconnected */ 2659 mutex_enter(&hidp->hid_mutex); 2660 hidp->hid_dev_state = USB_DEV_DISCONNECTED; 2661 mutex_exit(&hidp->hid_mutex); 2662 hid_pm_idle_component(hidp); 2663 2664 return; 2665 } 2666 2667 hid_set_idle(hidp); 2668 hid_set_protocol(hidp, SET_REPORT_PROTOCOL); 2669 2670 mutex_enter(&hidp->hid_mutex); 2671 /* if the device had remote wakeup earlier, enable it again */ 2672 if (hidpm->hid_wakeup_enabled) { 2673 mutex_exit(&hidp->hid_mutex); 2674 2675 if ((rval = usb_handle_remote_wakeup(hidp->hid_dip, 2676 USB_REMOTE_WAKEUP_ENABLE)) != USB_SUCCESS) { 2677 USB_DPRINTF_L2(PRINT_MASK_ATTA, 2678 hidp->hid_log_handle, 2679 "usb_handle_remote_wakeup failed (%d)", rval); 2680 } 2681 2682 mutex_enter(&hidp->hid_mutex); 2683 } 2684 2685 /* 2686 * restart polling on the interrupt pipe only if the device 2687 * was previously operational (open) 2688 */ 2689 if (hidp->hid_streams_flags == HID_STREAMS_OPEN) { 2690 rdq = hidp->hid_rq_ptr; 2691 wrq = hidp->hid_wq_ptr; 2692 if ((rval = hid_start_intr_polling(hidp)) != USB_SUCCESS) { 2693 USB_DPRINTF_L3(PRINT_MASK_ATTA, hidp->hid_log_handle, 2694 "hid_restore_device_state:" 2695 "unable to restart intr pipe poll" 2696 " rval = %d ", rval); 2697 /* 2698 * change the device state from 2699 * suspended to disconnected 2700 */ 2701 hidp->hid_dev_state = USB_DEV_DISCONNECTED; 2702 mutex_exit(&hidp->hid_mutex); 2703 hid_pm_idle_component(hidp); 2704 2705 return; 2706 } 2707 2708 if (hidp->hid_dev_state == USB_DEV_DISCONNECTED) { 2709 USB_DPRINTF_L2(PRINT_MASK_EVENTS, hidp->hid_log_handle, 2710 "device is being re-connected"); 2711 } 2712 2713 /* set the device state ONLINE */ 2714 hidp->hid_dev_state = USB_DEV_ONLINE; 2715 mutex_exit(&hidp->hid_mutex); 2716 2717 /* inform upstream modules that the device is back */ 2718 if (canputnext(rdq)) { 2719 mp = allocb(sizeof (struct iocblk), BPRI_HI); 2720 if (mp != NULL) { 2721 mp->b_datap->db_type = M_CTL; 2722 mctlmsg = (struct iocblk *)mp->b_datap->db_base; 2723 mctlmsg->ioc_cmd = HID_CONNECT_EVENT; 2724 mctlmsg->ioc_count = 0; 2725 putnext(rdq, mp); 2726 } 2727 } 2728 /* enable write side q */ 2729 qenable(wrq); 2730 mutex_enter(&hidp->hid_mutex); 2731 } else { 2732 /* set the device state ONLINE */ 2733 hidp->hid_dev_state = USB_DEV_ONLINE; 2734 } 2735 2736 mutex_exit(&hidp->hid_mutex); 2737 hid_pm_idle_component(hidp); 2738 } 2739 2740 2741 /* 2742 * hid_qreply_merror: 2743 * Pass an error message up. 2744 */ 2745 static void 2746 hid_qreply_merror(queue_t *q, mblk_t *mp, uchar_t errval) 2747 { 2748 hid_req_t *hid_req = NULL; 2749 2750 mp->b_datap->db_type = M_ERROR; 2751 if (mp->b_cont) { 2752 hid_req = (hid_req_t *)mp->b_cont->b_rptr; 2753 freemsg((mblk_t *)hid_req->hid_req_data); 2754 freemsg(mp->b_cont); 2755 mp->b_cont = NULL; 2756 } 2757 mp->b_rptr = mp->b_datap->db_base; 2758 mp->b_wptr = mp->b_rptr + sizeof (char); 2759 *mp->b_rptr = errval; 2760 2761 qreply(q, mp); 2762 } 2763 2764 2765 /* 2766 * hid_data2mblk: 2767 * Form an mblk from the given data 2768 */ 2769 static mblk_t * 2770 hid_data2mblk(uchar_t *buf, int len) 2771 { 2772 mblk_t *mp = NULL; 2773 2774 if (len >= 0) { 2775 mp = allocb(len, BPRI_HI); 2776 if (mp) { 2777 bcopy(buf, mp->b_datap->db_base, len); 2778 mp->b_wptr += len; 2779 } 2780 } 2781 2782 return (mp); 2783 } 2784 2785 2786 /* 2787 * hid_flush : 2788 * Flush data already sent upstreams to client module. 2789 */ 2790 static void 2791 hid_flush(queue_t *q) 2792 { 2793 /* 2794 * Flush pending data already sent upstream 2795 */ 2796 if ((q != NULL) && (q->q_next != NULL)) { 2797 (void) putnextctl1(q, M_FLUSH, FLUSHR); 2798 } 2799 } 2800 2801 2802 static void 2803 hid_pm_busy_component(hid_state_t *hid_statep) 2804 { 2805 ASSERT(!mutex_owned(&hid_statep->hid_mutex)); 2806 2807 if (hid_statep->hid_pm != NULL) { 2808 mutex_enter(&hid_statep->hid_mutex); 2809 hid_statep->hid_pm->hid_pm_busy++; 2810 2811 USB_DPRINTF_L4(PRINT_MASK_PM, hid_statep->hid_log_handle, 2812 "hid_pm_busy_component: %d", 2813 hid_statep->hid_pm->hid_pm_busy); 2814 2815 mutex_exit(&hid_statep->hid_mutex); 2816 if (pm_busy_component(hid_statep->hid_dip, 0) != DDI_SUCCESS) { 2817 mutex_enter(&hid_statep->hid_mutex); 2818 hid_statep->hid_pm->hid_pm_busy--; 2819 2820 USB_DPRINTF_L2(PRINT_MASK_PM, 2821 hid_statep->hid_log_handle, 2822 "hid_pm_busy_component failed: %d", 2823 hid_statep->hid_pm->hid_pm_busy); 2824 2825 mutex_exit(&hid_statep->hid_mutex); 2826 } 2827 2828 } 2829 } 2830 2831 2832 static void 2833 hid_pm_idle_component(hid_state_t *hid_statep) 2834 { 2835 ASSERT(!mutex_owned(&hid_statep->hid_mutex)); 2836 2837 if (hid_statep->hid_pm != NULL) { 2838 if (pm_idle_component(hid_statep->hid_dip, 0) == DDI_SUCCESS) { 2839 mutex_enter(&hid_statep->hid_mutex); 2840 ASSERT(hid_statep->hid_pm->hid_pm_busy > 0); 2841 hid_statep->hid_pm->hid_pm_busy--; 2842 2843 USB_DPRINTF_L4(PRINT_MASK_PM, 2844 hid_statep->hid_log_handle, 2845 "hid_pm_idle_component: %d", 2846 hid_statep->hid_pm->hid_pm_busy); 2847 2848 mutex_exit(&hid_statep->hid_mutex); 2849 } 2850 } 2851 } 2852 2853 2854 /* 2855 * hid_pwrlvl0: 2856 * Functions to handle power transition for various levels 2857 * These functions act as place holders to issue USB commands 2858 * to the devices to change their power levels 2859 */ 2860 static int 2861 hid_pwrlvl0(hid_state_t *hidp) 2862 { 2863 hid_power_t *hidpm; 2864 int rval; 2865 struct iocblk *mctlmsg; 2866 mblk_t *mp_lowpwr, *mp_fullpwr; 2867 queue_t *q; 2868 2869 hidpm = hidp->hid_pm; 2870 2871 switch (hidp->hid_dev_state) { 2872 case USB_DEV_ONLINE: 2873 /* Deny the powerdown request if the device is busy */ 2874 if (hidpm->hid_pm_busy != 0) { 2875 2876 return (USB_FAILURE); 2877 } 2878 2879 if (hidp->hid_streams_flags == HID_STREAMS_OPEN) { 2880 q = hidp->hid_rq_ptr; 2881 mutex_exit(&hidp->hid_mutex); 2882 if (canputnext(q)) { 2883 /* try to preallocate mblks */ 2884 mp_lowpwr = allocb( 2885 (int)sizeof (struct iocblk), BPRI_HI); 2886 mp_fullpwr = allocb( 2887 (int)sizeof (struct iocblk), BPRI_HI); 2888 if ((mp_lowpwr != NULL) && 2889 (mp_fullpwr != NULL)) { 2890 /* stop polling */ 2891 usb_pipe_stop_intr_polling( 2892 hidp->hid_interrupt_pipe, 2893 USB_FLAGS_SLEEP); 2894 2895 /* 2896 * Send an MCTL up indicating that 2897 * we are powering off 2898 */ 2899 mp_lowpwr->b_datap->db_type = M_CTL; 2900 mctlmsg = (struct iocblk *) 2901 mp_lowpwr->b_datap->db_base; 2902 mctlmsg->ioc_cmd = HID_POWER_OFF; 2903 mctlmsg->ioc_count = 0; 2904 putnext(q, mp_lowpwr); 2905 2906 /* save the full powr mblk */ 2907 mutex_enter(&hidp->hid_mutex); 2908 hidpm->hid_pm_pwrup = mp_fullpwr; 2909 } else { 2910 /* 2911 * Since we failed to allocate one 2912 * or more mblks, we fail attempt 2913 * to go into low power this time 2914 */ 2915 freemsg(mp_lowpwr); 2916 freemsg(mp_fullpwr); 2917 mutex_enter(&hidp->hid_mutex); 2918 2919 return (USB_FAILURE); 2920 } 2921 } else { 2922 /* 2923 * Since we can't send an mblk up, 2924 * we fail this attempt to go to low power 2925 */ 2926 mutex_enter(&hidp->hid_mutex); 2927 2928 return (USB_FAILURE); 2929 } 2930 } 2931 mutex_exit(&hidp->hid_mutex); 2932 /* Issue USB D3 command to the device here */ 2933 rval = usb_set_device_pwrlvl3(hidp->hid_dip); 2934 ASSERT(rval == USB_SUCCESS); 2935 2936 mutex_enter(&hidp->hid_mutex); 2937 hidp->hid_dev_state = USB_DEV_PWRED_DOWN; 2938 hidpm->hid_current_power = USB_DEV_OS_PWR_OFF; 2939 2940 /* FALLTHRU */ 2941 case USB_DEV_DISCONNECTED: 2942 case USB_DEV_SUSPENDED: 2943 case USB_DEV_PWRED_DOWN: 2944 default: 2945 break; 2946 } 2947 2948 return (USB_SUCCESS); 2949 } 2950 2951 2952 /* ARGSUSED */ 2953 static int 2954 hid_pwrlvl1(hid_state_t *hidp) 2955 { 2956 int rval; 2957 2958 /* Issue USB D2 command to the device here */ 2959 rval = usb_set_device_pwrlvl2(hidp->hid_dip); 2960 ASSERT(rval == USB_SUCCESS); 2961 2962 return (USB_FAILURE); 2963 } 2964 2965 2966 /* ARGSUSED */ 2967 static int 2968 hid_pwrlvl2(hid_state_t *hidp) 2969 { 2970 int rval; 2971 2972 rval = usb_set_device_pwrlvl1(hidp->hid_dip); 2973 ASSERT(rval == USB_SUCCESS); 2974 2975 return (USB_FAILURE); 2976 } 2977 2978 2979 static int 2980 hid_pwrlvl3(hid_state_t *hidp) 2981 { 2982 hid_power_t *hidpm; 2983 int rval; 2984 struct iocblk *mctlmsg; 2985 mblk_t *mp; 2986 queue_t *q; 2987 2988 hidpm = hidp->hid_pm; 2989 2990 switch (hidp->hid_dev_state) { 2991 case USB_DEV_HID_POWER_CHANGE: 2992 case USB_DEV_PWRED_DOWN: 2993 /* Issue USB D0 command to the device here */ 2994 rval = usb_set_device_pwrlvl0(hidp->hid_dip); 2995 ASSERT(rval == USB_SUCCESS); 2996 2997 if (hidp->hid_streams_flags == HID_STREAMS_OPEN) { 2998 /* restart polling on intr pipe */ 2999 rval = hid_start_intr_polling(hidp); 3000 if (rval != USB_SUCCESS) { 3001 USB_DPRINTF_L2(PRINT_MASK_EVENTS, 3002 hidp->hid_log_handle, 3003 "unable to restart intr polling rval = %d", 3004 rval); 3005 3006 return (USB_FAILURE); 3007 } 3008 3009 /* Send an MCTL up indicating device in full power */ 3010 q = hidp->hid_rq_ptr; 3011 mp = hidpm->hid_pm_pwrup; 3012 hidpm->hid_pm_pwrup = NULL; 3013 mutex_exit(&hidp->hid_mutex); 3014 if (canputnext(q)) { 3015 mp->b_datap->db_type = M_CTL; 3016 mctlmsg = (struct iocblk *)mp->b_datap->db_base; 3017 mctlmsg->ioc_cmd = HID_FULL_POWER; 3018 mctlmsg->ioc_count = 0; 3019 putnext(q, mp); 3020 } else { 3021 freemsg(mp); 3022 } 3023 mutex_enter(&hidp->hid_mutex); 3024 } 3025 hidp->hid_dev_state = USB_DEV_ONLINE; 3026 hidpm->hid_current_power = USB_DEV_OS_FULL_PWR; 3027 3028 /* FALLTHRU */ 3029 case USB_DEV_DISCONNECTED: 3030 case USB_DEV_SUSPENDED: 3031 case USB_DEV_ONLINE: 3032 3033 return (USB_SUCCESS); 3034 default: 3035 USB_DPRINTF_L2(PRINT_MASK_EVENTS, hidp->hid_log_handle, 3036 "hid_pwrlvl3: Improper State"); 3037 3038 return (USB_FAILURE); 3039 } 3040 } 3041 3042 3043 /* 3044 * hid_polled_input_init : 3045 * This routine calls down to the lower layers to initialize any state 3046 * information. This routine initializes the lower layers for input. 3047 */ 3048 static int 3049 hid_polled_input_init(hid_state_t *hidp) 3050 { 3051 USB_DPRINTF_L4(PRINT_MASK_ALL, hidp->hid_log_handle, 3052 "hid_polled_input_init"); 3053 3054 /* 3055 * Call the lower layers to intialize any state information 3056 * that they will need to provide the polled characters. 3057 */ 3058 if (usb_console_input_init(hidp->hid_dip, hidp->hid_interrupt_pipe, 3059 &hidp->hid_polled_raw_buf, 3060 &hidp->hid_polled_console_info) != USB_SUCCESS) { 3061 /* 3062 * If for some reason the lower layers cannot initialized, then 3063 * bail. 3064 */ 3065 (void) hid_polled_input_fini(hidp); 3066 3067 return (USB_FAILURE); 3068 } 3069 3070 return (USB_SUCCESS); 3071 } 3072 3073 3074 /* 3075 * hid_polled_input_fini: 3076 * This routine is called when we are done using this device as an input 3077 * device. 3078 */ 3079 static int 3080 hid_polled_input_fini(hid_state_t *hidp) 3081 { 3082 USB_DPRINTF_L4(PRINT_MASK_ALL, hidp->hid_log_handle, 3083 "hid_polled_input_fini"); 3084 3085 /* 3086 * Call the lower layers to free any state information 3087 * only if polled input has been initialised. 3088 */ 3089 if ((hidp->hid_polled_console_info) && 3090 (usb_console_input_fini(hidp->hid_polled_console_info) != 3091 USB_SUCCESS)) { 3092 3093 return (USB_FAILURE); 3094 } 3095 hidp->hid_polled_console_info = NULL; 3096 3097 return (USB_SUCCESS); 3098 } 3099 3100 3101 /* 3102 * hid_polled_input_enter: 3103 * This is the routine that is called in polled mode to save the USB 3104 * state information before using the USB keyboard as an input device. 3105 * This routine, and all of the routines that it calls, are responsible 3106 * for saving any state information so that it can be restored when 3107 * polling mode is over. 3108 */ 3109 static int 3110 /* ARGSUSED */ 3111 hid_polled_input_enter(hid_polled_handle_t hid_polled_inputp) 3112 { 3113 hid_state_t *hidp = (hid_state_t *)hid_polled_inputp; 3114 3115 /* 3116 * Call the lower layers to tell them to save any state information. 3117 */ 3118 (void) usb_console_input_enter(hidp->hid_polled_console_info); 3119 3120 return (USB_SUCCESS); 3121 } 3122 3123 3124 /* 3125 * hid_polled_read : 3126 * This is the routine that is called in polled mode when it wants to read 3127 * a character. We will call to the lower layers to see if there is any 3128 * input data available. If there is USB scancodes available, we will 3129 * give them back. 3130 */ 3131 static int 3132 hid_polled_read(hid_polled_handle_t hid_polled_input, uchar_t **buffer) 3133 { 3134 hid_state_t *hidp = (hid_state_t *)hid_polled_input; 3135 uint_t num_bytes; 3136 3137 /* 3138 * Call the lower layers to get the character from the controller. 3139 * The lower layers will return the number of characters that 3140 * were put in the raw buffer. The address of the raw buffer 3141 * was passed down to the lower layers during hid_polled_init. 3142 */ 3143 if (usb_console_read(hidp->hid_polled_console_info, 3144 &num_bytes) != USB_SUCCESS) { 3145 3146 return (0); 3147 } 3148 3149 /*LINTED*/ 3150 _NOTE(NO_COMPETING_THREADS_NOW); 3151 3152 *buffer = hidp->hid_polled_raw_buf; 3153 3154 /*LINTED*/ 3155 _NOTE(COMPETING_THREADS_NOW); 3156 3157 /* 3158 * Return the number of characters that were copied into the 3159 * polled buffer. 3160 */ 3161 return (num_bytes); 3162 } 3163 3164 3165 /* 3166 * hid_polled_input_exit : 3167 * This is the routine that is called in polled mode when it is giving up 3168 * control of the USB keyboard. This routine, and the lower layer routines 3169 * that it calls, are responsible for restoring the controller state to the 3170 * state it was in before polled mode. 3171 */ 3172 static int 3173 hid_polled_input_exit(hid_polled_handle_t hid_polled_inputp) 3174 { 3175 hid_state_t *hidp = (hid_state_t *)hid_polled_inputp; 3176 3177 /* 3178 * Call the lower layers to restore any state information. 3179 */ 3180 (void) usb_console_input_exit(hidp->hid_polled_console_info); 3181 3182 return (0); 3183 } 3184