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