xref: /illumos-gate/usr/src/uts/common/io/usb/clients/hid/hid.c (revision f498645a3eecf2ddd304b4ea9c7f1b4c155ff79e)
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 2006 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", pipe, req);
1289 
1290 	hid_pm_busy_component(hidp);
1291 
1292 	mutex_enter(&hidp->hid_mutex);
1293 
1294 	/*
1295 	 * If hid_close() is in progress, we shouldn't try accessing queue
1296 	 * Otherwise indicate that a putnext is going to happen, so
1297 	 * if close after this, that should wait for the putnext to finish.
1298 	 */
1299 	if (hidp->hid_streams_flags != HID_STREAMS_DISMANTLING) {
1300 		/*
1301 		 * Check if data can be put to the next queue.
1302 		 */
1303 		if (!canputnext(hidp->hid_rq_ptr)) {
1304 			USB_DPRINTF_L2(PRINT_MASK_ALL, hidp->hid_log_handle,
1305 			    "Buffer flushed when overflowed.");
1306 
1307 			/* Flush the queue above */
1308 			hid_flush(hidp->hid_rq_ptr);
1309 			mutex_exit(&hidp->hid_mutex);
1310 		} else {
1311 			q = hidp->hid_rq_ptr;
1312 			mutex_exit(&hidp->hid_mutex);
1313 
1314 			/* Put data upstream */
1315 			putnext(q, req->intr_data);
1316 
1317 			/* usb_free_intr_req should not free data */
1318 			req->intr_data = NULL;
1319 		}
1320 	} else {
1321 		mutex_exit(&hidp->hid_mutex);
1322 	}
1323 
1324 	/* free request and data */
1325 	usb_free_intr_req(req);
1326 	hid_pm_idle_component(hidp);
1327 }
1328 
1329 
1330 /*
1331  * hid_default_pipe_callback :
1332  *	Callback routine for the asynchronous control transfer
1333  *	Called from hid_send_async_ctrl_request() where we open
1334  *	the pipe in exclusive mode
1335  */
1336 static void
1337 hid_default_pipe_callback(usb_pipe_handle_t pipe, usb_ctrl_req_t *req)
1338 {
1339 	hid_default_pipe_arg_t *hid_default_pipe_arg =
1340 	    (hid_default_pipe_arg_t *)req->ctrl_client_private;
1341 	hid_state_t *hidp = hid_default_pipe_arg->hid_default_pipe_arg_hidp;
1342 	mblk_t		*mctl_mp;
1343 	mblk_t		*data = NULL;
1344 	queue_t		*q;
1345 
1346 	USB_DPRINTF_L4(PRINT_MASK_ALL, hidp->hid_log_handle,
1347 	    "hid_default_pipe_callback: "
1348 	    "ph = 0x%p, req = 0x%p, data= 0x%p", pipe, req, data);
1349 
1350 	ASSERT((req->ctrl_cb_flags & USB_CB_INTR_CONTEXT) == 0);
1351 
1352 	if (req->ctrl_data) {
1353 		data = req->ctrl_data;
1354 		req->ctrl_data = NULL;
1355 	}
1356 
1357 	/*
1358 	 * Free the b_cont of the original message that was sent down.
1359 	 */
1360 	mctl_mp = hid_default_pipe_arg->hid_default_pipe_arg_mblk;
1361 	freemsg(mctl_mp->b_cont);
1362 
1363 	/* chain the mblk received to the original & send it up */
1364 	mctl_mp->b_cont = data;
1365 	mutex_enter(&hidp->hid_mutex);
1366 	q = hidp->hid_rq_ptr;
1367 	mutex_exit(&hidp->hid_mutex);
1368 	if (canputnext(q)) {
1369 		putnext(q, mctl_mp);
1370 	} else {
1371 		freemsg(mctl_mp); /* avoid leak */
1372 	}
1373 
1374 	/*
1375 	 * Free the argument for the asynchronous callback
1376 	 */
1377 	kmem_free(hid_default_pipe_arg, sizeof (hid_default_pipe_arg_t));
1378 
1379 	/*
1380 	 * Free the control pipe request structure.
1381 	 */
1382 	usb_free_ctrl_req(req);
1383 
1384 	mutex_enter(&hidp->hid_mutex);
1385 	hidp->hid_default_pipe_req--;
1386 	ASSERT(hidp->hid_default_pipe_req >= 0);
1387 	mutex_exit(&hidp->hid_mutex);
1388 
1389 	hid_pm_idle_component(hidp);
1390 	qenable(hidp->hid_wq_ptr);
1391 }
1392 
1393 
1394 /*
1395  * hid_interrupt_pipe_exception_callback:
1396  *	Exception callback routine for interrupt pipe. If there is any data,
1397  *	destroy it. No threads are waiting for the exception callback.
1398  */
1399 /*ARGSUSED*/
1400 static void
1401 hid_interrupt_pipe_exception_callback(usb_pipe_handle_t pipe,
1402     usb_intr_req_t *req)
1403 {
1404 	hid_state_t	*hidp = (hid_state_t *)req->intr_client_private;
1405 	mblk_t		*data = req->intr_data;
1406 	usb_cb_flags_t	flags = req->intr_cb_flags;
1407 	int		rval;
1408 
1409 	USB_DPRINTF_L2(PRINT_MASK_ALL, hidp->hid_log_handle,
1410 	    "hid_interrupt_pipe_exception_callback: "
1411 	    "completion_reason = 0x%x, data = 0x%p, flag = 0x%x",
1412 	    req->intr_completion_reason, (void *)data, req->intr_cb_flags);
1413 
1414 	ASSERT((req->intr_cb_flags & USB_CB_INTR_CONTEXT) == 0);
1415 
1416 	if (((flags & USB_CB_FUNCTIONAL_STALL) != 0) &&
1417 	    ((flags & USB_CB_STALL_CLEARED) == 0)) {
1418 		USB_DPRINTF_L2(PRINT_MASK_ALL,
1419 		    hidp->hid_log_handle,
1420 		    "hid_interrupt_pipe_exception_callback: "
1421 		    "unable to clear stall.  flags = 0x%x",
1422 		    req->intr_cb_flags);
1423 	}
1424 
1425 	mutex_enter(&hidp->hid_mutex);
1426 
1427 	switch (req->intr_completion_reason) {
1428 	case USB_CR_STOPPED_POLLING:
1429 	case USB_CR_PIPE_CLOSING:
1430 	default:
1431 
1432 		break;
1433 	case USB_CR_PIPE_RESET:
1434 	case USB_CR_NO_RESOURCES:
1435 		if ((hidp->hid_dev_state == USB_DEV_ONLINE) &&
1436 		    ((rval = hid_start_intr_polling(hidp)) !=
1437 		    USB_SUCCESS)) {
1438 			USB_DPRINTF_L2(PRINT_MASK_ALL, hidp->hid_log_handle,
1439 			    "unable to restart interrupt poll. rval = %d",
1440 			    rval);
1441 		}
1442 
1443 		break;
1444 	}
1445 
1446 	mutex_exit(&hidp->hid_mutex);
1447 
1448 	usb_free_intr_req(req);
1449 }
1450 
1451 
1452 /*
1453  * hid_default_pipe_exception_callback:
1454  *	Exception callback routine for default pipe.
1455  */
1456 /*ARGSUSED*/
1457 static void
1458 hid_default_pipe_exception_callback(usb_pipe_handle_t pipe,
1459     usb_ctrl_req_t *req)
1460 {
1461 	hid_default_pipe_arg_t *hid_default_pipe_arg =
1462 	    (hid_default_pipe_arg_t *)req->ctrl_client_private;
1463 
1464 	hid_state_t *hidp = hid_default_pipe_arg->hid_default_pipe_arg_hidp;
1465 	mblk_t		*data = NULL;
1466 	usb_cr_t	ctrl_completion_reason = req->ctrl_completion_reason;
1467 	mblk_t		*mp;
1468 	queue_t		*q;
1469 
1470 	USB_DPRINTF_L2(PRINT_MASK_ALL, hidp->hid_log_handle,
1471 	    "hid_default_pipe_exception_callback: "
1472 	    "completion_reason = 0x%x, data = 0x%p, flag = 0x%x",
1473 	    ctrl_completion_reason, (void *)data, req->ctrl_cb_flags);
1474 
1475 	ASSERT((req->ctrl_cb_flags & USB_CB_INTR_CONTEXT) == 0);
1476 
1477 	/*
1478 	 * This is an exception callback, no need to pass data up
1479 	 */
1480 	q = hidp->hid_rq_ptr;
1481 
1482 	/*
1483 	 * Pass an error message up. Reuse existing mblk.
1484 	 */
1485 	if (canputnext(q)) {
1486 		mp = hid_default_pipe_arg->hid_default_pipe_arg_mblk;
1487 		mp->b_datap->db_type = M_ERROR;
1488 		mp->b_rptr = mp->b_datap->db_base;
1489 		mp->b_wptr = mp->b_rptr + sizeof (char);
1490 		*mp->b_rptr = EIO;
1491 		putnext(q, mp);
1492 	} else {
1493 		freemsg(hid_default_pipe_arg->hid_default_pipe_arg_mblk);
1494 	}
1495 	kmem_free(hid_default_pipe_arg, sizeof (hid_default_pipe_arg_t));
1496 
1497 	mutex_enter(&hidp->hid_mutex);
1498 	hidp->hid_default_pipe_req--;
1499 	ASSERT(hidp->hid_default_pipe_req >= 0);
1500 	mutex_exit(&hidp->hid_mutex);
1501 
1502 	qenable(hidp->hid_wq_ptr);
1503 	usb_free_ctrl_req(req);
1504 	hid_pm_idle_component(hidp);
1505 }
1506 
1507 
1508 /*
1509  * event handling:
1510  *
1511  * hid_reconnect_event_callback:
1512  *	the device was disconnected but this instance not detached, probably
1513  *	because the device was busy
1514  *
1515  *	If the same device, continue with restoring state
1516  */
1517 static int
1518 hid_restore_state_event_callback(dev_info_t *dip)
1519 {
1520 	hid_state_t	*hidp = (hid_state_t *)ddi_get_soft_state(hid_statep,
1521 				ddi_get_instance(dip));
1522 
1523 	ASSERT(hidp != NULL);
1524 
1525 	USB_DPRINTF_L3(PRINT_MASK_EVENTS, hidp->hid_log_handle,
1526 	    "hid_restore_state_event_callback: dip=0x%p", dip);
1527 
1528 	hid_restore_device_state(dip, hidp);
1529 
1530 	return (USB_SUCCESS);
1531 }
1532 
1533 
1534 /*
1535  * hid_cpr_suspend
1536  *	Fail suspend if we can't finish outstanding i/o activity.
1537  */
1538 static int
1539 hid_cpr_suspend(hid_state_t *hidp)
1540 {
1541 	int		rval, prev_state;
1542 	int		retval = USB_FAILURE;
1543 
1544 	USB_DPRINTF_L4(PRINT_MASK_EVENTS, hidp->hid_log_handle,
1545 	    "hid_cpr_suspend: dip=0x%p", hidp->hid_dip);
1546 
1547 	mutex_enter(&hidp->hid_mutex);
1548 	switch (hidp->hid_dev_state) {
1549 	case USB_DEV_ONLINE:
1550 	case USB_DEV_PWRED_DOWN:
1551 	case USB_DEV_DISCONNECTED:
1552 		prev_state = hidp->hid_dev_state;
1553 		hidp->hid_dev_state = USB_DEV_SUSPENDED;
1554 		mutex_exit(&hidp->hid_mutex);
1555 
1556 		/* drain all request outstanding on the default control pipe */
1557 		rval = usb_pipe_drain_reqs(hidp->hid_dip,
1558 		    hidp->hid_default_pipe, hid_default_pipe_drain_timeout,
1559 		    USB_FLAGS_SLEEP, NULL, 0);
1560 
1561 		/* fail checkpoint if we haven't finished the job yet */
1562 		mutex_enter(&hidp->hid_mutex);
1563 		if ((rval != USB_SUCCESS) || (hidp->hid_default_pipe_req > 0)) {
1564 			USB_DPRINTF_L2(PRINT_MASK_EVENTS, hidp->hid_log_handle,
1565 			    "hid_cpr_suspend: "
1566 			    "device busy - can't checkpoint");
1567 
1568 			/* fall back to previous state */
1569 			hidp->hid_dev_state = prev_state;
1570 		} else {
1571 			retval = USB_SUCCESS;
1572 			hid_save_device_state(hidp);
1573 		}
1574 
1575 		break;
1576 	case USB_DEV_SUSPENDED:
1577 	default:
1578 		USB_DPRINTF_L2(PRINT_MASK_EVENTS, hidp->hid_log_handle,
1579 		    "hid_cpr_suspend: Illegal dev state: %d",
1580 		    hidp->hid_dev_state);
1581 
1582 		break;
1583 	}
1584 	mutex_exit(&hidp->hid_mutex);
1585 
1586 	return (retval);
1587 }
1588 
1589 
1590 static void
1591 hid_cpr_resume(hid_state_t *hidp)
1592 {
1593 	USB_DPRINTF_L4(PRINT_MASK_EVENTS, hidp->hid_log_handle,
1594 	    "hid_cpr_resume: dip=0x%p", hidp->hid_dip);
1595 
1596 	hid_restore_device_state(hidp->hid_dip, hidp);
1597 }
1598 
1599 
1600 /*
1601  * hid_disconnect_event_callback:
1602  *	The device has been disconnected. We either wait for
1603  *	detach or a reconnect event. Close all pipes and timeouts.
1604  */
1605 static int
1606 hid_disconnect_event_callback(dev_info_t *dip)
1607 {
1608 	hid_state_t	*hidp;
1609 
1610 	hidp = (hid_state_t *)ddi_get_soft_state(hid_statep,
1611 	    ddi_get_instance(dip));
1612 	ASSERT(hidp != NULL);
1613 
1614 	USB_DPRINTF_L4(PRINT_MASK_EVENTS, hidp->hid_log_handle,
1615 	    "hid_disconnect_event_callback: dip=0x%p", dip);
1616 
1617 	mutex_enter(&hidp->hid_mutex);
1618 	switch (hidp->hid_dev_state) {
1619 	case USB_DEV_ONLINE:
1620 	case USB_DEV_PWRED_DOWN:
1621 		hidp->hid_dev_state = USB_DEV_DISCONNECTED;
1622 		if (hidp->hid_streams_flags == HID_STREAMS_OPEN) {
1623 
1624 			USB_DPRINTF_L2(PRINT_MASK_EVENTS, hidp->hid_log_handle,
1625 			    "busy device has been disconnected");
1626 		}
1627 		hid_save_device_state(hidp);
1628 
1629 		break;
1630 	case USB_DEV_SUSPENDED:
1631 		/* we remain suspended */
1632 
1633 		break;
1634 	default:
1635 		USB_DPRINTF_L2(PRINT_MASK_EVENTS, hidp->hid_log_handle,
1636 		    "hid_disconnect_event_callback: Illegal dev state: %d",
1637 		    hidp->hid_dev_state);
1638 
1639 		break;
1640 	}
1641 	mutex_exit(&hidp->hid_mutex);
1642 
1643 	return (USB_SUCCESS);
1644 }
1645 
1646 
1647 /*
1648  * hid_power_change_callback:
1649  *	Async callback function to notify pm_raise_power completion
1650  *	after hid_power entry point is called.
1651  */
1652 static void
1653 hid_power_change_callback(void *arg, int rval)
1654 {
1655 	hid_state_t	*hidp;
1656 	queue_t		*wq;
1657 
1658 	hidp = (hid_state_t *)arg;
1659 
1660 	USB_DPRINTF_L4(PRINT_MASK_PM, hidp->hid_log_handle,
1661 	    "hid_power_change_callback - rval: %d", rval);
1662 
1663 	mutex_enter(&hidp->hid_mutex);
1664 	hidp->hid_pm->hid_raise_power = B_FALSE;
1665 
1666 	if (hidp->hid_dev_state == USB_DEV_ONLINE) {
1667 		wq = hidp->hid_wq_ptr;
1668 		mutex_exit(&hidp->hid_mutex);
1669 
1670 		qenable(wq);
1671 
1672 	} else {
1673 		mutex_exit(&hidp->hid_mutex);
1674 	}
1675 }
1676 
1677 
1678 /*
1679  * hid_parse_hid_descr:
1680  *	Parse the hid descriptor, check after interface and after
1681  *	endpoint descriptor
1682  */
1683 static size_t
1684 hid_parse_hid_descr(
1685 	usb_hid_descr_t		*ret_descr,
1686 	size_t			ret_buf_len,
1687 	usb_alt_if_data_t	*altif_data,
1688 	usb_ep_data_t		*ep_data)
1689 {
1690 	usb_cvs_data_t *cvs;
1691 	int		which_cvs;
1692 
1693 	for (which_cvs = 0; which_cvs < altif_data->altif_n_cvs; which_cvs++) {
1694 		cvs = &altif_data->altif_cvs[which_cvs];
1695 		if (cvs->cvs_buf == NULL) {
1696 			continue;
1697 		}
1698 		if (cvs->cvs_buf[1] == USB_DESCR_TYPE_HID) {
1699 			return (usb_parse_data("ccscccs",
1700 				cvs->cvs_buf, cvs->cvs_buf_len,
1701 				(void *)ret_descr,
1702 				(size_t)ret_buf_len));
1703 		}
1704 	}
1705 
1706 	/* now try after endpoint */
1707 	for (which_cvs = 0; which_cvs < ep_data->ep_n_cvs; which_cvs++) {
1708 		cvs = &ep_data->ep_cvs[which_cvs];
1709 		if (cvs->cvs_buf == NULL) {
1710 			continue;
1711 		}
1712 		if (cvs->cvs_buf[1] == USB_DESCR_TYPE_HID) {
1713 			return (usb_parse_data("ccscccs",
1714 				cvs->cvs_buf, cvs->cvs_buf_len,
1715 				(void *)ret_descr,
1716 				(size_t)ret_buf_len));
1717 		}
1718 	}
1719 
1720 	return (USB_PARSE_ERROR);
1721 }
1722 
1723 
1724 /*
1725  * hid_parse_hid_descr_failure:
1726  *	If parsing of hid descriptor failed and the device is
1727  *	a keyboard or mouse, use predefined length and packet size.
1728  */
1729 static int
1730 hid_parse_hid_descr_failure(hid_state_t	*hidp)
1731 {
1732 	/*
1733 	 * Parsing hid descriptor failed, probably because the
1734 	 * device did not return a valid hid descriptor. Check to
1735 	 * see if this is a keyboard or mouse. If so, use the
1736 	 * predefined hid descriptor length and packet size.
1737 	 * Otherwise, detach and return failure.
1738 	 */
1739 	USB_DPRINTF_L1(PRINT_MASK_ATTA, hidp->hid_log_handle,
1740 	    "Parsing of hid descriptor failed");
1741 
1742 	if (hidp->hid_if_descr.bInterfaceProtocol == KEYBOARD_PROTOCOL) {
1743 		USB_DPRINTF_L2(PRINT_MASK_ATTA, hidp->hid_log_handle,
1744 		    "Set hid descriptor length to predefined "
1745 		    "USB_KB_HID_DESCR_LENGTH for keyboard.");
1746 
1747 		/* device is a keyboard */
1748 		hidp->hid_hid_descr.wReportDescriptorLength =
1749 				    USB_KB_HID_DESCR_LENGTH;
1750 
1751 		hidp->hid_packet_size = USBKPSZ;
1752 
1753 	} else if (hidp->hid_if_descr.bInterfaceProtocol ==
1754 	    MOUSE_PROTOCOL) {
1755 		USB_DPRINTF_L2(PRINT_MASK_ATTA, hidp->hid_log_handle,
1756 		    "Set hid descriptor length to predefined "
1757 		    "USB_MS_HID_DESCR_LENGTH for mouse.");
1758 
1759 		/* device is a mouse */
1760 		hidp->hid_hid_descr.wReportDescriptorLength =
1761 				    USB_MS_HID_DESCR_LENGTH;
1762 
1763 		hidp->hid_packet_size = USBMSSZ;
1764 	} else {
1765 
1766 		return (USB_FAILURE);
1767 	}
1768 
1769 	return (USB_SUCCESS);
1770 }
1771 
1772 
1773 /*
1774  * hid_handle_report_descriptor:
1775  *	Get the report descriptor, call hidparser routine to parse
1776  *	it and query the hidparser tree to get the packet size
1777  */
1778 static int
1779 hid_handle_report_descriptor(hid_state_t	*hidp,
1780 				int		interface)
1781 {
1782 	usb_cr_t		completion_reason;
1783 	usb_cb_flags_t		cb_flags;
1784 	mblk_t			*data = NULL;
1785 	hidparser_packet_info_t	hpack;
1786 	int			i;
1787 	usb_ctrl_setup_t setup = {
1788 	    USB_DEV_REQ_DEV_TO_HOST |	/* bmRequestType */
1789 		USB_DEV_REQ_RCPT_IF,
1790 	    USB_REQ_GET_DESCR,		/* bRequest */
1791 	    USB_CLASS_DESCR_TYPE_REPORT, /* wValue */
1792 	    0,				/* wIndex: interface, fill in later */
1793 	    0,				/* wLength, fill in later  */
1794 	    0				/* attributes */
1795 	};
1796 
1797 	/*
1798 	 * Parsing hid desciptor was successful earlier.
1799 	 * Get Report Descriptor
1800 	 */
1801 	setup.wIndex = (uint16_t)interface;
1802 	setup.wLength = hidp->hid_hid_descr.wReportDescriptorLength;
1803 	if (usb_pipe_ctrl_xfer_wait(hidp->hid_default_pipe,
1804 	    &setup,
1805 	    &data,				/* data */
1806 	    &completion_reason, &cb_flags, 0) != USB_SUCCESS) {
1807 
1808 		USB_DPRINTF_L2(PRINT_MASK_ATTA, hidp->hid_log_handle,
1809 			"Failed to receive the Report Descriptor");
1810 		freemsg(data);
1811 
1812 		return (USB_FAILURE);
1813 
1814 	} else {
1815 		int n =  hidp->hid_hid_descr.wReportDescriptorLength;
1816 
1817 		ASSERT(data);
1818 
1819 		/* Print the report descriptor */
1820 		for (i = 0; i < n; i++) {
1821 			USB_DPRINTF_L3(PRINT_MASK_ATTA, hidp->hid_log_handle,
1822 				"Index = %d\tvalue =0x%x", i,
1823 				(int)(data->b_rptr[i]));
1824 		}
1825 
1826 		/* Get Report Descriptor was successful */
1827 		if (hidparser_parse_report_descriptor(
1828 		    data->b_rptr,
1829 		    hidp->hid_hid_descr.wReportDescriptorLength,
1830 		    &hidp->hid_hid_descr,
1831 		    &hidp->hid_report_descr) == HIDPARSER_SUCCESS) {
1832 
1833 			/* find max intr-in xfer length */
1834 			hidparser_find_max_packet_size_from_report_descriptor(
1835 			    hidp->hid_report_descr, &hpack);
1836 			/* round up to the nearest byte */
1837 			hidp->hid_packet_size = (hpack.max_packet_size + 7) / 8;
1838 
1839 			/* if report id is used, add more more byte for it */
1840 			if (hpack.report_id != HID_REPORT_ID_UNDEFINED) {
1841 				hidp->hid_packet_size++;
1842 			}
1843 		} else {
1844 			USB_DPRINTF_L1(PRINT_MASK_ATTA, hidp->hid_log_handle,
1845 			    "Invalid Report Descriptor");
1846 			freemsg(data);
1847 
1848 			return (USB_FAILURE);
1849 		}
1850 
1851 		freemsg(data);
1852 
1853 		return (USB_SUCCESS);
1854 	}
1855 }
1856 
1857 
1858 /*
1859  * hid_set_idle:
1860  *	Make a clas specific request to SET_IDLE.
1861  *	In this case send no reports if state has not changed.
1862  *	See HID 7.2.4.
1863  */
1864 /*ARGSUSED*/
1865 static void
1866 hid_set_idle(hid_state_t	*hidp)
1867 {
1868 	usb_cr_t	completion_reason;
1869 	usb_cb_flags_t	cb_flags;
1870 	usb_ctrl_setup_t setup = {
1871 	    USB_DEV_REQ_HOST_TO_DEV |	/* bmRequestType */
1872 		USB_DEV_REQ_TYPE_CLASS |
1873 		USB_DEV_REQ_RCPT_IF,
1874 	    SET_IDLE,			/* bRequest */
1875 	    DURATION,			/* wValue */
1876 	    0,				/* wIndex: interface, fill in later */
1877 	    0,				/* wLength */
1878 	    0				/* attributes */
1879 	};
1880 
1881 	USB_DPRINTF_L4(PRINT_MASK_ATTA, hidp->hid_log_handle,
1882 	    "hid_set_idle: Begin");
1883 
1884 	setup.wIndex = hidp->hid_if_descr.bInterfaceNumber;
1885 	if (usb_pipe_ctrl_xfer_wait(
1886 	    hidp->hid_default_pipe,
1887 	    &setup,
1888 	    NULL,			/* no data to send. */
1889 	    &completion_reason, &cb_flags, 0) != USB_SUCCESS) {
1890 
1891 		USB_DPRINTF_L2(PRINT_MASK_ATTA, hidp->hid_log_handle,
1892 		    "Failed while trying to set idle,"
1893 		    "cr = %d, cb_flags = 0x%x\n",
1894 		    completion_reason, cb_flags);
1895 	}
1896 	USB_DPRINTF_L4(PRINT_MASK_ATTA, hidp->hid_log_handle,
1897 		"hid_set_idle: End");
1898 }
1899 
1900 
1901 /*
1902  * hid_set_protocol:
1903  *	Initialize the device to set the preferred protocol
1904  */
1905 /*ARGSUSED*/
1906 static void
1907 hid_set_protocol(hid_state_t *hidp, int protocol)
1908 {
1909 	usb_cr_t	completion_reason;
1910 	usb_cb_flags_t	cb_flags;
1911 	usb_ctrl_setup_t setup;
1912 
1913 	USB_DPRINTF_L4(PRINT_MASK_ATTA, hidp->hid_log_handle,
1914 	    "hid_set_protocol(%d): Begin", protocol);
1915 
1916 	/* initialize the setup request */
1917 	setup.bmRequestType = USB_DEV_REQ_HOST_TO_DEV |
1918 	    USB_DEV_REQ_TYPE_CLASS | USB_DEV_REQ_RCPT_IF;
1919 	setup.bRequest = SET_PROTOCOL;
1920 	setup.wValue = protocol;
1921 	setup.wIndex = hidp->hid_if_descr.bInterfaceNumber;
1922 	setup.wLength = 0;
1923 	setup.attrs = 0;
1924 	if (usb_pipe_ctrl_xfer_wait(
1925 	    hidp->hid_default_pipe,	/* bmRequestType */
1926 	    &setup,
1927 	    NULL,			/* no data to send */
1928 	    &completion_reason, &cb_flags, 0) != USB_SUCCESS) {
1929 		/*
1930 		 * Some devices fail to follow the specification
1931 		 * and instead of STALLing, they continously
1932 		 * NAK the SET_IDLE command. We need to reset
1933 		 * the pipe then, so that ohci doesn't panic.
1934 		 */
1935 		USB_DPRINTF_L2(PRINT_MASK_ATTA, hidp->hid_log_handle,
1936 		    "Failed while trying to set protocol:%d,"
1937 		    "cr =  %d cb_flags = 0x%x\n",
1938 		    completion_reason, cb_flags, protocol);
1939 	}
1940 
1941 	USB_DPRINTF_L4(PRINT_MASK_ATTA, hidp->hid_log_handle,
1942 		"hid_set_protocol: End");
1943 }
1944 
1945 
1946 /*
1947  * hid_detach_cleanup:
1948  *	called by attach and detach for cleanup.
1949  */
1950 static void
1951 hid_detach_cleanup(dev_info_t *dip, hid_state_t *hidp)
1952 {
1953 	int	flags = hidp->hid_attach_flags;
1954 	int	rval;
1955 	hid_power_t	*hidpm;
1956 
1957 	USB_DPRINTF_L4(PRINT_MASK_ALL, hidp->hid_log_handle,
1958 	    "hid_detach_cleanup: Begin");
1959 
1960 	if ((hidp->hid_attach_flags & HID_LOCK_INIT) == 0) {
1961 
1962 		goto done;
1963 	}
1964 
1965 	/*
1966 	 * Disable the event callbacks first, after this point, event
1967 	 * callbacks will never get called. Note we shouldn't hold
1968 	 * mutex while unregistering events because there may be a
1969 	 * competing event callback thread. Event callbacks are done
1970 	 * with ndi mutex held and this can cause a potential deadlock.
1971 	 */
1972 	usb_unregister_event_cbs(dip, &hid_events);
1973 
1974 	mutex_enter(&hidp->hid_mutex);
1975 
1976 	hidpm = hidp->hid_pm;
1977 
1978 	USB_DPRINTF_L2(PRINT_MASK_ALL, hidp->hid_log_handle,
1979 	    "hid_detach_cleanup: hidpm=0x%p", hidpm);
1980 
1981 	if (hidpm && (hidp->hid_dev_state != USB_DEV_DISCONNECTED)) {
1982 
1983 		mutex_exit(&hidp->hid_mutex);
1984 		hid_pm_busy_component(hidp);
1985 		if (hid_is_pm_enabled(dip) == USB_SUCCESS) {
1986 
1987 			if (hidpm->hid_wakeup_enabled) {
1988 
1989 				/* First bring the device to full power */
1990 				(void) pm_raise_power(dip, 0,
1991 				    USB_DEV_OS_FULL_PWR);
1992 
1993 				/* Disable remote wakeup */
1994 				rval = usb_handle_remote_wakeup(dip,
1995 				    USB_REMOTE_WAKEUP_DISABLE);
1996 
1997 				if (rval != DDI_SUCCESS) {
1998 					USB_DPRINTF_L2(PRINT_MASK_ALL,
1999 					    hidp->hid_log_handle,
2000 					    "hid_detach_cleanup: "
2001 					    "disble remote wakeup failed, "
2002 					    "rval= %d", rval);
2003 				}
2004 			}
2005 
2006 			(void) pm_lower_power(dip, 0, USB_DEV_OS_PWR_OFF);
2007 		}
2008 		hid_pm_idle_component(hidp);
2009 		mutex_enter(&hidp->hid_mutex);
2010 	}
2011 
2012 	if (hidpm) {
2013 		freemsg(hidpm->hid_pm_pwrup);
2014 		kmem_free(hidpm, sizeof (hid_power_t));
2015 		hidp->hid_pm = NULL;
2016 	}
2017 
2018 	mutex_exit(&hidp->hid_mutex);
2019 
2020 	if (hidp->hid_report_descr != NULL) {
2021 		(void) hidparser_free_report_descriptor_handle(
2022 					hidp->hid_report_descr);
2023 	}
2024 
2025 	if (flags & HID_MINOR_NODES) {
2026 		ddi_remove_minor_node(dip, NULL);
2027 	}
2028 
2029 	mutex_destroy(&hidp->hid_mutex);
2030 
2031 	USB_DPRINTF_L4(PRINT_MASK_ALL, hidp->hid_log_handle,
2032 	    "hid_detach_cleanup: End");
2033 
2034 done:
2035 	usb_client_detach(dip, hidp->hid_dev_data);
2036 	usb_free_log_hdl(hidp->hid_log_handle);
2037 	ddi_soft_state_free(hid_statep, hidp->hid_instance);
2038 
2039 	ddi_prop_remove_all(dip);
2040 }
2041 
2042 
2043 /*
2044  * hid_start_intr_polling:
2045  *	Allocate an interrupt request structure, initialize,
2046  *	and start interrupt transfers.
2047  */
2048 static int
2049 hid_start_intr_polling(hid_state_t *hidp)
2050 {
2051 	usb_intr_req_t	*req;
2052 	int rval = USB_SUCCESS;
2053 
2054 	USB_DPRINTF_L4(PRINT_MASK_PM, hidp->hid_log_handle,
2055 	    "hid_start_intr_polling: "
2056 	    "dev_state=%s str_flags=%d ph=0x%p",
2057 	    usb_str_dev_state(hidp->hid_dev_state), hidp->hid_streams_flags,
2058 	    hidp->hid_interrupt_pipe);
2059 
2060 	if ((hidp->hid_streams_flags == HID_STREAMS_OPEN) &&
2061 	    (hidp->hid_interrupt_pipe != NULL)) {
2062 		/*
2063 		 * initialize interrupt pipe request structure
2064 		 */
2065 		req = usb_alloc_intr_req(hidp->hid_dip, 0, USB_FLAGS_SLEEP);
2066 		req->intr_client_private = (usb_opaque_t)hidp;
2067 		req->intr_attributes = USB_ATTRS_SHORT_XFER_OK |
2068 						USB_ATTRS_AUTOCLEARING;
2069 		req->intr_len = hidp->hid_packet_size;
2070 		req->intr_cb = hid_interrupt_pipe_callback;
2071 		req->intr_exc_cb = hid_interrupt_pipe_exception_callback;
2072 
2073 		/*
2074 		 * Start polling on the interrupt pipe.
2075 		 */
2076 		mutex_exit(&hidp->hid_mutex);
2077 
2078 		if ((rval = usb_pipe_intr_xfer(hidp->hid_interrupt_pipe, req,
2079 		    USB_FLAGS_SLEEP)) != USB_SUCCESS) {
2080 			USB_DPRINTF_L2(PRINT_MASK_PM, hidp->hid_log_handle,
2081 			    "hid_start_intr_polling failed: rval = %d",
2082 			    rval);
2083 			usb_free_intr_req(req);
2084 		}
2085 
2086 		mutex_enter(&hidp->hid_mutex);
2087 	}
2088 
2089 	USB_DPRINTF_L4(PRINT_MASK_PM, hidp->hid_log_handle,
2090 	    "hid_start_intr_polling: done, rval = %d", rval);
2091 
2092 	return (rval);
2093 }
2094 
2095 
2096 /*
2097  * hid_close_intr_pipe:
2098  *	close the interrupt pipe after draining all callbacks
2099  */
2100 static void
2101 hid_close_intr_pipe(hid_state_t *hidp)
2102 {
2103 	USB_DPRINTF_L4(PRINT_MASK_CLOSE, hidp->hid_log_handle,
2104 	    "hid_close_intr_pipe: Begin");
2105 
2106 	if (hidp->hid_interrupt_pipe) {
2107 		/*
2108 		 * Close the interrupt pipe
2109 		 */
2110 		mutex_exit(&hidp->hid_mutex);
2111 		usb_pipe_close(hidp->hid_dip, hidp->hid_interrupt_pipe,
2112 		    USB_FLAGS_SLEEP, NULL, NULL);
2113 		mutex_enter(&hidp->hid_mutex);
2114 		hidp->hid_interrupt_pipe = NULL;
2115 	}
2116 	USB_DPRINTF_L4(PRINT_MASK_CLOSE, hidp->hid_log_handle,
2117 	    "hid_close_intr_pipe: End");
2118 }
2119 
2120 
2121 /*
2122  * hid_mctl_receive:
2123  *	Handle M_CTL messages from upper stream.  If
2124  *	we don't understand the command, free message.
2125  */
2126 static int
2127 hid_mctl_receive(register queue_t *q, register mblk_t *mp)
2128 {
2129 	hid_state_t	*hidp = (hid_state_t *)q->q_ptr;
2130 	struct iocblk	*iocp;
2131 	int		error = HID_FAILURE;
2132 	uchar_t		request_type;
2133 	hid_req_t	*hid_req_data = NULL;
2134 	hid_polled_input_callback_t hid_polled_input;
2135 	hid_vid_pid_t	hid_vid_pid;
2136 
2137 	USB_DPRINTF_L4(PRINT_MASK_ALL, hidp->hid_log_handle,
2138 	    "hid_mctl_receive");
2139 
2140 	iocp = (struct iocblk *)mp->b_rptr;
2141 
2142 	switch (iocp->ioc_cmd) {
2143 	case HID_SET_REPORT:
2144 		/* FALLTHRU */
2145 	case HID_SET_IDLE:
2146 		/* FALLTHRU */
2147 	case HID_SET_PROTOCOL:
2148 		request_type = USB_DEV_REQ_HOST_TO_DEV |
2149 		    USB_DEV_REQ_RCPT_IF | USB_DEV_REQ_TYPE_CLASS;
2150 
2151 		break;
2152 	case HID_GET_REPORT:
2153 		/* FALLTHRU */
2154 	case HID_GET_IDLE:
2155 		/* FALLTHRU */
2156 	case HID_GET_PROTOCOL:
2157 		request_type = USB_DEV_REQ_DEV_TO_HOST |
2158 		    USB_DEV_REQ_RCPT_IF | USB_DEV_REQ_TYPE_CLASS;
2159 
2160 		break;
2161 	case HID_GET_PARSER_HANDLE:
2162 		if (canputnext(RD(q))) {
2163 			freemsg(mp->b_cont);
2164 			mp->b_cont = hid_data2mblk(
2165 			    (uchar_t *)&hidp->hid_report_descr,
2166 			    sizeof (hidp->hid_report_descr));
2167 			if (mp->b_cont == NULL) {
2168 				/*
2169 				 * can't allocate mblk, indicate
2170 				 * that nothing is returned
2171 				 */
2172 				iocp->ioc_count = 0;
2173 			} else {
2174 				iocp->ioc_count =
2175 				    sizeof (hidp->hid_report_descr);
2176 			}
2177 			qreply(q, mp);
2178 
2179 			return (HID_SUCCESS);
2180 		} else {
2181 
2182 			/* retry */
2183 			return (HID_ENQUEUE);
2184 		}
2185 	case HID_GET_VID_PID:
2186 		if (canputnext(RD(q))) {
2187 			freemsg(mp->b_cont);
2188 
2189 			hid_vid_pid.VendorId =
2190 				hidp->hid_dev_descr->idVendor;
2191 			hid_vid_pid.ProductId =
2192 				hidp->hid_dev_descr->idProduct;
2193 
2194 			mp->b_cont = hid_data2mblk(
2195 			    (uchar_t *)&hid_vid_pid, sizeof (hid_vid_pid_t));
2196 			if (mp->b_cont == NULL) {
2197 				/*
2198 				 * can't allocate mblk, indicate that nothing
2199 				 * is being returned.
2200 				 */
2201 				iocp->ioc_count = 0;
2202 			} else {
2203 				iocp->ioc_count =
2204 				    sizeof (hid_vid_pid_t);
2205 			}
2206 			qreply(q, mp);
2207 
2208 			return (HID_SUCCESS);
2209 		} else {
2210 
2211 			/* retry */
2212 			return (HID_ENQUEUE);
2213 		}
2214 	case HID_OPEN_POLLED_INPUT:
2215 		if (canputnext(RD(q))) {
2216 			freemsg(mp->b_cont);
2217 
2218 			/* Initialize the structure */
2219 			hid_polled_input.hid_polled_version =
2220 					HID_POLLED_INPUT_V0;
2221 			hid_polled_input.hid_polled_read = hid_polled_read;
2222 			hid_polled_input.hid_polled_input_enter =
2223 				hid_polled_input_enter;
2224 			hid_polled_input.hid_polled_input_exit =
2225 				hid_polled_input_exit;
2226 			hid_polled_input.hid_polled_input_handle =
2227 				(hid_polled_handle_t)hidp;
2228 
2229 			mp->b_cont = hid_data2mblk(
2230 			    (uchar_t *)&hid_polled_input,
2231 			    sizeof (hid_polled_input_callback_t));
2232 			if (mp->b_cont == NULL) {
2233 				/*
2234 				 * can't allocate mblk, indicate that nothing
2235 				 * is being returned.
2236 				 */
2237 				iocp->ioc_count = 0;
2238 			} else {
2239 				/* Call down into USBA */
2240 				(void) hid_polled_input_init(hidp);
2241 
2242 				iocp->ioc_count =
2243 				    sizeof (hid_polled_input_callback_t);
2244 			}
2245 			qreply(q, mp);
2246 
2247 			return (HID_SUCCESS);
2248 		} else {
2249 
2250 			/* retry */
2251 			return (HID_ENQUEUE);
2252 		}
2253 	case HID_CLOSE_POLLED_INPUT:
2254 		/* Call down into USBA */
2255 		(void) hid_polled_input_fini(hidp);
2256 
2257 		iocp->ioc_count = 0;
2258 		qreply(q, mp);
2259 
2260 		return (HID_SUCCESS);
2261 	default:
2262 		hid_qreply_merror(q, mp, EINVAL);
2263 
2264 		return (HID_FAILURE);
2265 	}
2266 
2267 	/*
2268 	 * These (device executable) commands require a hid_req_t.
2269 	 * Make sure one is present
2270 	 */
2271 	if (mp->b_cont == NULL) {
2272 		hid_qreply_merror(q, mp, EINVAL);
2273 
2274 		return (error);
2275 	} else {
2276 		hid_req_data = (hid_req_t *)mp->b_cont->b_rptr;
2277 		if ((iocp->ioc_cmd == HID_SET_REPORT) &&
2278 		    (hid_req_data->hid_req_data == NULL)) {
2279 			hid_qreply_merror(q, mp, EINVAL);
2280 
2281 			return (error);
2282 		}
2283 	}
2284 
2285 	/*
2286 	 * Check is version no. is correct. This
2287 	 * is coming from the user
2288 	 */
2289 	if (hid_req_data->hid_req_version_no != HID_VERSION_V_0) {
2290 		hid_qreply_merror(q, mp, EINVAL);
2291 
2292 		return (error);
2293 	}
2294 
2295 	mutex_enter(&hidp->hid_mutex);
2296 	USB_DPRINTF_L4(PRINT_MASK_ALL, hidp->hid_log_handle,
2297 	    "hid_mctl_receive: dev_state=%s",
2298 	    usb_str_dev_state(hidp->hid_dev_state));
2299 
2300 	switch (hidp->hid_dev_state) {
2301 	case USB_DEV_PWRED_DOWN:
2302 		/*
2303 		 * get the device full powered. We get a callback
2304 		 * which enables the WQ and kicks off IO
2305 		 */
2306 		hidp->hid_dev_state = USB_DEV_HID_POWER_CHANGE;
2307 		mutex_exit(&hidp->hid_mutex);
2308 		if (usb_req_raise_power(hidp->hid_dip, 0,
2309 		    USB_DEV_OS_FULL_PWR, hid_power_change_callback,
2310 		    hidp, 0) != USB_SUCCESS) {
2311 			/* we retry raising power in wsrv */
2312 			mutex_enter(&hidp->hid_mutex);
2313 			hidp->hid_dev_state = USB_DEV_PWRED_DOWN;
2314 			mutex_exit(&hidp->hid_mutex);
2315 		}
2316 		error = HID_ENQUEUE;
2317 
2318 		break;
2319 	case USB_DEV_HID_POWER_CHANGE:
2320 		mutex_exit(&hidp->hid_mutex);
2321 		error = HID_ENQUEUE;
2322 
2323 		break;
2324 	case USB_DEV_ONLINE:
2325 		if (hidp->hid_streams_flags != HID_STREAMS_DISMANTLING) {
2326 			/* Send a message down */
2327 			mutex_exit(&hidp->hid_mutex);
2328 			error = hid_mctl_execute_cmd(hidp, request_type,
2329 			    hid_req_data, mp);
2330 			if (error == HID_FAILURE) {
2331 				hid_qreply_merror(q, mp, EIO);
2332 			}
2333 		} else {
2334 			mutex_exit(&hidp->hid_mutex);
2335 			hid_qreply_merror(q, mp, EIO);
2336 		}
2337 
2338 		break;
2339 	default:
2340 		mutex_exit(&hidp->hid_mutex);
2341 		hid_qreply_merror(q, mp, EIO);
2342 
2343 		break;
2344 	}
2345 
2346 	return (error);
2347 }
2348 
2349 
2350 /*
2351  * hid_mctl_execute_cmd:
2352  *	Send the command to the device.
2353  */
2354 static int
2355 hid_mctl_execute_cmd(hid_state_t *hidp, int request_type,
2356     hid_req_t *hid_req_data, mblk_t *mp)
2357 {
2358 	int		request_index;
2359 	struct iocblk	*iocp;
2360 	hid_default_pipe_arg_t	*def_pipe_arg;
2361 
2362 	iocp = (struct iocblk *)mp->b_rptr;
2363 	USB_DPRINTF_L4(PRINT_MASK_ALL, hidp->hid_log_handle,
2364 	    "hid_mctl_execute_cmd: iocp=0x%p", iocp);
2365 
2366 	request_index = hidp->hid_if_descr.bInterfaceNumber;
2367 
2368 	/*
2369 	 * Set up the argument to be passed back to hid
2370 	 * when the asynchronous control callback is
2371 	 * executed.
2372 	 */
2373 	def_pipe_arg = kmem_zalloc(sizeof (hid_default_pipe_arg_t), 0);
2374 
2375 	if (def_pipe_arg == NULL) {
2376 
2377 		return (HID_FAILURE);
2378 	}
2379 
2380 	def_pipe_arg->hid_default_pipe_arg_mctlmsg.ioc_cmd = iocp->ioc_cmd;
2381 	def_pipe_arg->hid_default_pipe_arg_mctlmsg.ioc_count = 0;
2382 	def_pipe_arg->hid_default_pipe_arg_hidp = hidp;
2383 	def_pipe_arg->hid_default_pipe_arg_mblk = mp;
2384 
2385 	/*
2386 	 * Send the command down to USBA through default
2387 	 * pipe.
2388 	 */
2389 	if (hid_send_async_ctrl_request(hidp, hid_req_data,
2390 	    request_type, iocp->ioc_cmd,
2391 	    request_index, def_pipe_arg) != USB_SUCCESS) {
2392 
2393 		kmem_free(def_pipe_arg, sizeof (hid_default_pipe_arg_t));
2394 
2395 		return (HID_FAILURE);
2396 	}
2397 
2398 	return (HID_INPROGRESS);
2399 }
2400 
2401 
2402 /*
2403  * hid_send_async_ctrl_request:
2404  *	Send an asynchronous control request to USBA.  Since hid is a STREAMS
2405  *	driver, it is not allowed to wait in its entry points except for the
2406  *	open and close entry points.  Therefore, hid must use the asynchronous
2407  *	USBA calls.
2408  */
2409 static int
2410 hid_send_async_ctrl_request(hid_state_t *hidp, hid_req_t *hid_request,
2411 			uchar_t request_type, int request_request,
2412 			ushort_t request_index,
2413 			hid_default_pipe_arg_t *hid_default_pipe_arg)
2414 {
2415 	usb_ctrl_req_t	*ctrl_req;
2416 	int		rval;
2417 	size_t		length = 0;
2418 
2419 	USB_DPRINTF_L4(PRINT_MASK_ALL, hidp->hid_log_handle,
2420 	    "hid_send_async_ctrl_request: "
2421 	    "rq_type=%d rq_rq=%d index=%d",
2422 	    request_type, request_request, request_index);
2423 
2424 	mutex_enter(&hidp->hid_mutex);
2425 	hidp->hid_default_pipe_req++;
2426 	mutex_exit(&hidp->hid_mutex);
2427 
2428 	/*
2429 	 * Note that ctrl_req->ctrl_data should be allocated by usba
2430 	 * only for IN requests. OUT request(e.g SET_REPORT) can have a
2431 	 * non-zero wLength value but ctrl_data would be allocated by
2432 	 * client for them.
2433 	 */
2434 	if (request_type & USB_DEV_REQ_DEV_TO_HOST) {
2435 		length = hid_request->hid_req_data ?
2436 		    0 : hid_request->hid_req_wLength;
2437 	}
2438 
2439 	if ((ctrl_req = usb_alloc_ctrl_req(hidp->hid_dip, length, 0)) == NULL) {
2440 		USB_DPRINTF_L2(PRINT_MASK_ALL, hidp->hid_log_handle,
2441 		    "unable to alloc ctrl req. async trans failed");
2442 		mutex_enter(&hidp->hid_mutex);
2443 		hidp->hid_default_pipe_req--;
2444 		ASSERT(hidp->hid_default_pipe_req >= 0);
2445 		mutex_exit(&hidp->hid_mutex);
2446 
2447 		/*
2448 		 * Some SET M_CTLs will have non-null
2449 		 * hid_req_data, free hid_req_data
2450 		 */
2451 		freemsg((mblk_t *)hid_request->hid_req_data);
2452 		hid_request->hid_req_data = NULL;
2453 
2454 		return (USB_FAILURE);
2455 	}
2456 
2457 	if (request_type & USB_DEV_REQ_HOST_TO_DEV) {
2458 		ASSERT((length == 0) && (ctrl_req->ctrl_data == NULL));
2459 	}
2460 
2461 	ctrl_req->ctrl_bmRequestType	= request_type;
2462 	ctrl_req->ctrl_bRequest 	= (uint8_t)request_request;
2463 	ctrl_req->ctrl_wValue		= hid_request->hid_req_wValue;
2464 	ctrl_req->ctrl_wIndex		= request_index;
2465 	ctrl_req->ctrl_wLength		= hid_request->hid_req_wLength;
2466 	ctrl_req->ctrl_data		= ctrl_req->ctrl_data ?
2467 					    ctrl_req->ctrl_data :
2468 					    hid_request->hid_req_data;
2469 	ctrl_req->ctrl_attributes	= USB_ATTRS_AUTOCLEARING;
2470 	ctrl_req->ctrl_client_private	= (usb_opaque_t)hid_default_pipe_arg;
2471 	ctrl_req->ctrl_cb		= hid_default_pipe_callback;
2472 	ctrl_req->ctrl_exc_cb		= hid_default_pipe_exception_callback;
2473 
2474 	if ((rval = usb_pipe_ctrl_xfer(hidp->hid_default_pipe,
2475 	    ctrl_req, 0)) != USB_SUCCESS) {
2476 		mutex_enter(&hidp->hid_mutex);
2477 		hidp->hid_default_pipe_req--;
2478 		ASSERT(hidp->hid_default_pipe_req >= 0);
2479 		mutex_exit(&hidp->hid_mutex);
2480 
2481 		/* caller will free hid_req_data in case of failure */
2482 		ctrl_req->ctrl_data = NULL;
2483 		usb_free_ctrl_req(ctrl_req);
2484 		USB_DPRINTF_L2(PRINT_MASK_ALL, hidp->hid_log_handle,
2485 		    "usb_pipe_ctrl_xfer() failed. rval = %d", rval);
2486 
2487 		return (USB_FAILURE);
2488 	}
2489 
2490 	return (USB_SUCCESS);
2491 }
2492 
2493 
2494 /*
2495  * hid_ioctl:
2496  *	Hid currently doesn't handle any ioctls.  NACK
2497  *	the ioctl request.
2498  */
2499 static void
2500 hid_ioctl(register queue_t *q, register mblk_t *mp)
2501 {
2502 	register struct iocblk *iocp;
2503 
2504 	iocp = (struct iocblk *)mp->b_rptr;
2505 
2506 	iocp->ioc_rval = 0;
2507 
2508 	iocp->ioc_error = ENOTTY;
2509 
2510 	mp->b_datap->db_type = M_IOCNAK;
2511 
2512 	qreply(q, mp);
2513 }
2514 
2515 
2516 /*
2517  * hid_create_pm_components:
2518  *	Create the pm components required for power management.
2519  *	For keyboard/mouse, the components is created only if the device
2520  *	supports a remote wakeup.
2521  *	For other hid devices they are created unconditionally.
2522  */
2523 static void
2524 hid_create_pm_components(dev_info_t *dip, hid_state_t *hidp)
2525 {
2526 	hid_power_t	*hidpm;
2527 	uint_t		pwr_states;
2528 
2529 	USB_DPRINTF_L4(PRINT_MASK_PM, hidp->hid_log_handle,
2530 	    "hid_create_pm_components: Begin");
2531 
2532 	/* Allocate the state structure */
2533 	hidpm = kmem_zalloc(sizeof (hid_power_t), KM_SLEEP);
2534 	hidp->hid_pm = hidpm;
2535 	hidpm->hid_state = hidp;
2536 	hidpm->hid_raise_power = B_FALSE;
2537 	hidpm->hid_pm_capabilities = 0;
2538 	hidpm->hid_current_power = USB_DEV_OS_FULL_PWR;
2539 
2540 	switch (hidp->hid_if_descr.bInterfaceProtocol) {
2541 	case KEYBOARD_PROTOCOL:
2542 	case MOUSE_PROTOCOL:
2543 		hidpm->hid_pm_strategy = HID_PM_ACTIVITY;
2544 		if ((hid_is_pm_enabled(dip) == USB_SUCCESS) &&
2545 		    (usb_handle_remote_wakeup(dip, USB_REMOTE_WAKEUP_ENABLE) ==
2546 		    USB_SUCCESS)) {
2547 
2548 			USB_DPRINTF_L3(PRINT_MASK_PM, hidp->hid_log_handle,
2549 			    "hid_create_pm_components: Remote Wakeup Enabled");
2550 
2551 			if (usb_create_pm_components(dip, &pwr_states) ==
2552 			    USB_SUCCESS) {
2553 				hidpm->hid_wakeup_enabled = 1;
2554 				hidpm->hid_pwr_states = (uint8_t)pwr_states;
2555 			}
2556 		}
2557 
2558 		break;
2559 	default:
2560 		hidpm->hid_pm_strategy = HID_PM_OPEN_CLOSE;
2561 		if ((hid_is_pm_enabled(dip) == USB_SUCCESS) &&
2562 		    (usb_create_pm_components(dip, &pwr_states) ==
2563 		    USB_SUCCESS)) {
2564 			hidpm->hid_wakeup_enabled = 0;
2565 			hidpm->hid_pwr_states = (uint8_t)pwr_states;
2566 		}
2567 
2568 		break;
2569 	}
2570 
2571 	USB_DPRINTF_L4(PRINT_MASK_PM, hidp->hid_log_handle,
2572 		"hid_create_pm_components: END");
2573 }
2574 
2575 
2576 /*
2577  * hid_is_pm_enabled
2578  *	Check if the device is pm enabled. Always enable
2579  *	pm on the new SUN mouse
2580  */
2581 static int
2582 hid_is_pm_enabled(dev_info_t *dip)
2583 {
2584 	hid_state_t	*hidp = ddi_get_soft_state(hid_statep,
2585 							ddi_get_instance(dip));
2586 
2587 	if (strcmp(ddi_node_name(dip), "mouse") == 0) {
2588 		/* check for overrides first */
2589 		if (hid_pm_mouse ||
2590 		    (ddi_prop_exists(DDI_DEV_T_ANY, dip,
2591 		    (DDI_PROP_DONTPASS | DDI_PROP_NOTPROM),
2592 		    "hid-mouse-pm-enable") == 1)) {
2593 
2594 			return (USB_SUCCESS);
2595 		}
2596 
2597 		/*
2598 		 * Always enable PM for 1.05 or greater SUN mouse
2599 		 * hidp->hid_dev_descr won't be NULL.
2600 		 */
2601 		if ((hidp->hid_dev_descr->idVendor ==
2602 		    HID_SUN_MOUSE_VENDOR_ID) &&
2603 		    (hidp->hid_dev_descr->idProduct ==
2604 		    HID_SUN_MOUSE_PROD_ID) &&
2605 		    (hidp->hid_dev_descr->bcdDevice >=
2606 		    HID_SUN_MOUSE_BCDDEVICE)) {
2607 
2608 			return (USB_SUCCESS);
2609 		}
2610 	} else {
2611 
2612 		return (USB_SUCCESS);
2613 	}
2614 
2615 	return (USB_FAILURE);
2616 }
2617 
2618 
2619 /*
2620  * hid_save_device_state
2621  *	Save the current device/driver state.
2622  */
2623 static void
2624 hid_save_device_state(hid_state_t *hidp)
2625 {
2626 	struct iocblk	*mctlmsg;
2627 	mblk_t		*mp;
2628 	queue_t		*q;
2629 
2630 	USB_DPRINTF_L4(PRINT_MASK_EVENTS, hidp->hid_log_handle,
2631 	    "hid_save_device_state");
2632 
2633 	if (hidp->hid_streams_flags == HID_STREAMS_OPEN) {
2634 		/*
2635 		 * Send an MCTL up indicating that
2636 		 * the device will loose its state
2637 		 */
2638 		q = hidp->hid_rq_ptr;
2639 		mutex_exit(&hidp->hid_mutex);
2640 		if (canputnext(q)) {
2641 			mp = allocb(sizeof (struct iocblk), BPRI_HI);
2642 			if (mp != NULL) {
2643 				mp->b_datap->db_type = M_CTL;
2644 				mctlmsg = (struct iocblk *)mp->b_datap->db_base;
2645 				mctlmsg->ioc_cmd = HID_DISCONNECT_EVENT;
2646 				mctlmsg->ioc_count = 0;
2647 				putnext(q, mp);
2648 			}
2649 		}
2650 		/* stop polling on the intr pipe */
2651 		usb_pipe_stop_intr_polling(hidp->hid_interrupt_pipe,
2652 		    USB_FLAGS_SLEEP);
2653 
2654 		mutex_enter(&hidp->hid_mutex);
2655 	}
2656 }
2657 
2658 
2659 /*
2660  * hid_restore_device_state:
2661  *	Set original configuration of the device.
2662  *	Reopen intr pipe.
2663  *	Enable wrq - this starts new transactions on the control pipe.
2664  */
2665 static void
2666 hid_restore_device_state(dev_info_t *dip, hid_state_t *hidp)
2667 {
2668 	int		rval;
2669 	queue_t		*rdq, *wrq;
2670 	hid_power_t	*hidpm;
2671 	struct iocblk	*mctlmsg;
2672 	mblk_t		*mp;
2673 
2674 	hid_pm_busy_component(hidp);
2675 	mutex_enter(&hidp->hid_mutex);
2676 
2677 	USB_DPRINTF_L4(PRINT_MASK_ATTA, hidp->hid_log_handle,
2678 	    "hid_restore_device_state: %s",
2679 	    usb_str_dev_state(hidp->hid_dev_state));
2680 
2681 	hidpm = hidp->hid_pm;
2682 	mutex_exit(&hidp->hid_mutex);
2683 
2684 	/* First bring the device to full power */
2685 	(void) pm_raise_power(dip, 0, USB_DEV_OS_FULL_PWR);
2686 
2687 	mutex_enter(&hidp->hid_mutex);
2688 	if (hidp->hid_dev_state == USB_DEV_ONLINE) {
2689 		/*
2690 		 * We failed the checkpoint, there is no need to restore
2691 		 * the device state
2692 		 */
2693 		mutex_exit(&hidp->hid_mutex);
2694 		hid_pm_idle_component(hidp);
2695 
2696 		return;
2697 	}
2698 	mutex_exit(&hidp->hid_mutex);
2699 
2700 
2701 	/* Check if we are talking to the same device */
2702 	if (usb_check_same_device(dip, hidp->hid_log_handle, USB_LOG_L2,
2703 	    PRINT_MASK_ALL, USB_CHK_BASIC|USB_CHK_CFG, NULL) != USB_SUCCESS) {
2704 
2705 		/* change the device state from suspended to disconnected */
2706 		mutex_enter(&hidp->hid_mutex);
2707 		hidp->hid_dev_state = USB_DEV_DISCONNECTED;
2708 		mutex_exit(&hidp->hid_mutex);
2709 		hid_pm_idle_component(hidp);
2710 
2711 		return;
2712 	}
2713 
2714 	hid_set_idle(hidp);
2715 	hid_set_protocol(hidp, SET_REPORT_PROTOCOL);
2716 
2717 	mutex_enter(&hidp->hid_mutex);
2718 	/* if the device had remote wakeup earlier, enable it again */
2719 	if (hidpm->hid_wakeup_enabled) {
2720 		mutex_exit(&hidp->hid_mutex);
2721 
2722 		if ((rval = usb_handle_remote_wakeup(hidp->hid_dip,
2723 		    USB_REMOTE_WAKEUP_ENABLE)) != USB_SUCCESS) {
2724 			USB_DPRINTF_L2(PRINT_MASK_ATTA,
2725 			    hidp->hid_log_handle,
2726 			    "usb_handle_remote_wakeup failed (%d)", rval);
2727 		}
2728 
2729 		mutex_enter(&hidp->hid_mutex);
2730 	}
2731 
2732 	/*
2733 	 * restart polling on the interrupt pipe only if the device
2734 	 * was previously operational (open)
2735 	 */
2736 	if (hidp->hid_streams_flags == HID_STREAMS_OPEN) {
2737 		rdq = hidp->hid_rq_ptr;
2738 		wrq = hidp->hid_wq_ptr;
2739 		if ((rval = hid_start_intr_polling(hidp)) != USB_SUCCESS) {
2740 			USB_DPRINTF_L3(PRINT_MASK_ATTA, hidp->hid_log_handle,
2741 			    "hid_restore_device_state:"
2742 			    "unable to restart intr pipe poll"
2743 			    " rval = %d ", rval);
2744 			/*
2745 			 * change the device state from
2746 			 * suspended to disconnected
2747 			 */
2748 			hidp->hid_dev_state = USB_DEV_DISCONNECTED;
2749 			mutex_exit(&hidp->hid_mutex);
2750 			hid_pm_idle_component(hidp);
2751 
2752 			return;
2753 		}
2754 
2755 		if (hidp->hid_dev_state == USB_DEV_DISCONNECTED) {
2756 			USB_DPRINTF_L2(PRINT_MASK_EVENTS, hidp->hid_log_handle,
2757 			    "device is being re-connected");
2758 		}
2759 
2760 		/* set the device state ONLINE */
2761 		hidp->hid_dev_state = USB_DEV_ONLINE;
2762 		mutex_exit(&hidp->hid_mutex);
2763 
2764 		/* inform upstream modules that the device is back */
2765 		if (canputnext(rdq)) {
2766 			mp = allocb(sizeof (struct iocblk), BPRI_HI);
2767 			if (mp != NULL) {
2768 				mp->b_datap->db_type = M_CTL;
2769 				mctlmsg = (struct iocblk *)mp->b_datap->db_base;
2770 				mctlmsg->ioc_cmd = HID_CONNECT_EVENT;
2771 				mctlmsg->ioc_count = 0;
2772 				putnext(rdq, mp);
2773 			}
2774 		}
2775 		/* enable write side q */
2776 		qenable(wrq);
2777 		mutex_enter(&hidp->hid_mutex);
2778 	} else {
2779 		/* set the device state ONLINE */
2780 		hidp->hid_dev_state = USB_DEV_ONLINE;
2781 	}
2782 
2783 	mutex_exit(&hidp->hid_mutex);
2784 	hid_pm_idle_component(hidp);
2785 }
2786 
2787 
2788 /*
2789  * hid_qreply_merror:
2790  *	Pass an error message up.
2791  */
2792 static void
2793 hid_qreply_merror(queue_t *q, mblk_t *mp, uchar_t errval)
2794 {
2795 	hid_req_t	*hid_req = NULL;
2796 
2797 	mp->b_datap->db_type = M_ERROR;
2798 	if (mp->b_cont) {
2799 		hid_req = (hid_req_t *)mp->b_cont->b_rptr;
2800 		freemsg((mblk_t *)hid_req->hid_req_data);
2801 		freemsg(mp->b_cont);
2802 		mp->b_cont = NULL;
2803 	}
2804 	mp->b_rptr = mp->b_datap->db_base;
2805 	mp->b_wptr = mp->b_rptr + sizeof (char);
2806 	*mp->b_rptr = errval;
2807 
2808 	qreply(q, mp);
2809 }
2810 
2811 
2812 /*
2813  * hid_data2mblk:
2814  *	Form an mblk from the given data
2815  */
2816 static mblk_t *
2817 hid_data2mblk(uchar_t *buf, int len)
2818 {
2819 	mblk_t	*mp = NULL;
2820 
2821 	if (len >= 0) {
2822 		mp = allocb(len, BPRI_HI);
2823 		if (mp) {
2824 			bcopy(buf, mp->b_datap->db_base, len);
2825 			mp->b_wptr += len;
2826 		}
2827 	}
2828 
2829 	return (mp);
2830 }
2831 
2832 
2833 /*
2834  * hid_flush :
2835  *	Flush data already sent upstreams to client module.
2836  */
2837 static void
2838 hid_flush(queue_t *q)
2839 {
2840 	/*
2841 	 * Flush pending data already sent upstream
2842 	 */
2843 	if ((q != NULL) && (q->q_next != NULL)) {
2844 		(void) putnextctl1(q, M_FLUSH, FLUSHR);
2845 	}
2846 }
2847 
2848 
2849 static void
2850 hid_pm_busy_component(hid_state_t *hid_statep)
2851 {
2852 	ASSERT(!mutex_owned(&hid_statep->hid_mutex));
2853 
2854 	if (hid_statep->hid_pm != NULL) {
2855 		mutex_enter(&hid_statep->hid_mutex);
2856 		hid_statep->hid_pm->hid_pm_busy++;
2857 
2858 		USB_DPRINTF_L4(PRINT_MASK_PM, hid_statep->hid_log_handle,
2859 		    "hid_pm_busy_component: %d",
2860 		    hid_statep->hid_pm->hid_pm_busy);
2861 
2862 		mutex_exit(&hid_statep->hid_mutex);
2863 		if (pm_busy_component(hid_statep->hid_dip, 0) != DDI_SUCCESS) {
2864 			mutex_enter(&hid_statep->hid_mutex);
2865 			hid_statep->hid_pm->hid_pm_busy--;
2866 
2867 			USB_DPRINTF_L2(PRINT_MASK_PM,
2868 			    hid_statep->hid_log_handle,
2869 			    "hid_pm_busy_component failed: %d",
2870 			    hid_statep->hid_pm->hid_pm_busy);
2871 
2872 			mutex_exit(&hid_statep->hid_mutex);
2873 		}
2874 
2875 	}
2876 }
2877 
2878 
2879 static void
2880 hid_pm_idle_component(hid_state_t *hid_statep)
2881 {
2882 	ASSERT(!mutex_owned(&hid_statep->hid_mutex));
2883 
2884 	if (hid_statep->hid_pm != NULL) {
2885 		if (pm_idle_component(hid_statep->hid_dip, 0) == DDI_SUCCESS) {
2886 			mutex_enter(&hid_statep->hid_mutex);
2887 			ASSERT(hid_statep->hid_pm->hid_pm_busy > 0);
2888 			hid_statep->hid_pm->hid_pm_busy--;
2889 
2890 			USB_DPRINTF_L4(PRINT_MASK_PM,
2891 			    hid_statep->hid_log_handle,
2892 			    "hid_pm_idle_component: %d",
2893 			    hid_statep->hid_pm->hid_pm_busy);
2894 
2895 			mutex_exit(&hid_statep->hid_mutex);
2896 		}
2897 	}
2898 }
2899 
2900 
2901 /*
2902  * hid_pwrlvl0:
2903  *	Functions to handle power transition for various levels
2904  *	These functions act as place holders to issue USB commands
2905  *	to the devices to change their power levels
2906  */
2907 static int
2908 hid_pwrlvl0(hid_state_t *hidp)
2909 {
2910 	hid_power_t	*hidpm;
2911 	int		rval;
2912 	struct iocblk	*mctlmsg;
2913 	mblk_t		*mp_lowpwr, *mp_fullpwr;
2914 	queue_t		*q;
2915 
2916 	hidpm = hidp->hid_pm;
2917 
2918 	switch (hidp->hid_dev_state) {
2919 	case USB_DEV_ONLINE:
2920 		/* Deny the powerdown request if the device is busy */
2921 		if (hidpm->hid_pm_busy != 0) {
2922 
2923 			return (USB_FAILURE);
2924 		}
2925 
2926 		if (hidp->hid_streams_flags == HID_STREAMS_OPEN) {
2927 			q = hidp->hid_rq_ptr;
2928 			mutex_exit(&hidp->hid_mutex);
2929 			if (canputnext(q)) {
2930 				/* try to preallocate mblks */
2931 				mp_lowpwr = allocb(
2932 				    (int)sizeof (struct iocblk), BPRI_HI);
2933 				mp_fullpwr = allocb(
2934 				    (int)sizeof (struct iocblk), BPRI_HI);
2935 				if ((mp_lowpwr != NULL) &&
2936 				    (mp_fullpwr != NULL)) {
2937 					/* stop polling */
2938 					usb_pipe_stop_intr_polling(
2939 					    hidp->hid_interrupt_pipe,
2940 					    USB_FLAGS_SLEEP);
2941 
2942 					/*
2943 					 * Send an MCTL up indicating that
2944 					 * we are powering off
2945 					 */
2946 					mp_lowpwr->b_datap->db_type = M_CTL;
2947 					mctlmsg = (struct iocblk *)
2948 					    mp_lowpwr->b_datap->db_base;
2949 					mctlmsg->ioc_cmd = HID_POWER_OFF;
2950 					mctlmsg->ioc_count = 0;
2951 					putnext(q, mp_lowpwr);
2952 
2953 					/* save the full powr mblk */
2954 					mutex_enter(&hidp->hid_mutex);
2955 					hidpm->hid_pm_pwrup = mp_fullpwr;
2956 				} else {
2957 					/*
2958 					 * Since we failed to allocate one
2959 					 * or more mblks, we fail attempt
2960 					 * to go into low power this time
2961 					 */
2962 					freemsg(mp_lowpwr);
2963 					freemsg(mp_fullpwr);
2964 					mutex_enter(&hidp->hid_mutex);
2965 
2966 					return (USB_FAILURE);
2967 				}
2968 			} else {
2969 				/*
2970 				 * Since we can't send an mblk up,
2971 				 * we fail this attempt to go to low power
2972 				 */
2973 				mutex_enter(&hidp->hid_mutex);
2974 
2975 				return (USB_FAILURE);
2976 			}
2977 		}
2978 		mutex_exit(&hidp->hid_mutex);
2979 		/* Issue USB D3 command to the device here */
2980 		rval = usb_set_device_pwrlvl3(hidp->hid_dip);
2981 		ASSERT(rval == USB_SUCCESS);
2982 
2983 		mutex_enter(&hidp->hid_mutex);
2984 		hidp->hid_dev_state = USB_DEV_PWRED_DOWN;
2985 		hidpm->hid_current_power = USB_DEV_OS_PWR_OFF;
2986 
2987 		/* FALLTHRU */
2988 	case USB_DEV_DISCONNECTED:
2989 	case USB_DEV_SUSPENDED:
2990 	case USB_DEV_PWRED_DOWN:
2991 	default:
2992 		break;
2993 	}
2994 
2995 	return (USB_SUCCESS);
2996 }
2997 
2998 
2999 /* ARGSUSED */
3000 static int
3001 hid_pwrlvl1(hid_state_t *hidp)
3002 {
3003 	int		rval;
3004 
3005 	/* Issue USB D2 command to the device here */
3006 	rval = usb_set_device_pwrlvl2(hidp->hid_dip);
3007 	ASSERT(rval == USB_SUCCESS);
3008 
3009 	return (USB_FAILURE);
3010 }
3011 
3012 
3013 /* ARGSUSED */
3014 static int
3015 hid_pwrlvl2(hid_state_t *hidp)
3016 {
3017 	int		rval;
3018 
3019 	rval = usb_set_device_pwrlvl1(hidp->hid_dip);
3020 	ASSERT(rval == USB_SUCCESS);
3021 
3022 	return (USB_FAILURE);
3023 }
3024 
3025 
3026 static int
3027 hid_pwrlvl3(hid_state_t *hidp)
3028 {
3029 	hid_power_t	*hidpm;
3030 	int		rval;
3031 	struct iocblk	*mctlmsg;
3032 	mblk_t		*mp;
3033 	queue_t		*q;
3034 
3035 	hidpm = hidp->hid_pm;
3036 
3037 	switch (hidp->hid_dev_state) {
3038 	case USB_DEV_HID_POWER_CHANGE:
3039 	case USB_DEV_PWRED_DOWN:
3040 		/* Issue USB D0 command to the device here */
3041 		rval = usb_set_device_pwrlvl0(hidp->hid_dip);
3042 		ASSERT(rval == USB_SUCCESS);
3043 
3044 		if (hidp->hid_streams_flags == HID_STREAMS_OPEN) {
3045 			/* restart polling on intr pipe */
3046 			rval = hid_start_intr_polling(hidp);
3047 			if (rval != USB_SUCCESS) {
3048 				USB_DPRINTF_L2(PRINT_MASK_EVENTS,
3049 				    hidp->hid_log_handle,
3050 				    "unable to restart intr polling rval = %d",
3051 				    rval);
3052 
3053 				return (USB_FAILURE);
3054 			}
3055 
3056 			/* Send an MCTL up indicating device in full  power */
3057 			q = hidp->hid_rq_ptr;
3058 			mp = hidpm->hid_pm_pwrup;
3059 			hidpm->hid_pm_pwrup = NULL;
3060 			mutex_exit(&hidp->hid_mutex);
3061 			if (canputnext(q)) {
3062 				mp->b_datap->db_type = M_CTL;
3063 				mctlmsg = (struct iocblk *)mp->b_datap->db_base;
3064 				mctlmsg->ioc_cmd = HID_FULL_POWER;
3065 				mctlmsg->ioc_count = 0;
3066 				putnext(q, mp);
3067 			} else {
3068 				freemsg(mp);
3069 			}
3070 			mutex_enter(&hidp->hid_mutex);
3071 		}
3072 		hidp->hid_dev_state = USB_DEV_ONLINE;
3073 		hidpm->hid_current_power = USB_DEV_OS_FULL_PWR;
3074 
3075 		/* FALLTHRU */
3076 	case USB_DEV_DISCONNECTED:
3077 	case USB_DEV_SUSPENDED:
3078 	case USB_DEV_ONLINE:
3079 
3080 		return (USB_SUCCESS);
3081 	default:
3082 		USB_DPRINTF_L2(PRINT_MASK_EVENTS, hidp->hid_log_handle,
3083 		    "hid_pwrlvl3: Improper State");
3084 
3085 		return (USB_FAILURE);
3086 	}
3087 }
3088 
3089 
3090 /*
3091  * hid_polled_input_init :
3092  *	This routine calls down to the lower layers to initialize any state
3093  *	information.  This routine initializes the lower layers for input.
3094  */
3095 static int
3096 hid_polled_input_init(hid_state_t *hidp)
3097 {
3098 	USB_DPRINTF_L4(PRINT_MASK_ALL, hidp->hid_log_handle,
3099 	    "hid_polled_input_init");
3100 
3101 	/*
3102 	 * Call the lower layers to intialize any state information
3103 	 * that they will need to provide the polled characters.
3104 	 */
3105 	if (usb_console_input_init(hidp->hid_dip, hidp->hid_interrupt_pipe,
3106 	    &hidp->hid_polled_raw_buf,
3107 	    &hidp->hid_polled_console_info) != USB_SUCCESS) {
3108 		/*
3109 		 * If for some reason the lower layers cannot initialized, then
3110 		 * bail.
3111 		 */
3112 		(void) hid_polled_input_fini(hidp);
3113 
3114 		return (USB_FAILURE);
3115 	}
3116 
3117 	return (USB_SUCCESS);
3118 }
3119 
3120 
3121 /*
3122  * hid_polled_input_fini:
3123  *	This routine is called when we are done using this device as an input
3124  *	device.
3125  */
3126 static int
3127 hid_polled_input_fini(hid_state_t *hidp)
3128 {
3129 	USB_DPRINTF_L4(PRINT_MASK_ALL, hidp->hid_log_handle,
3130 	    "hid_polled_input_fini");
3131 
3132 	/*
3133 	 * Call the lower layers to free any state information
3134 	 * only if polled input has been initialised.
3135 	 */
3136 	if ((hidp->hid_polled_console_info) &&
3137 	    (usb_console_input_fini(hidp->hid_polled_console_info) !=
3138 	    USB_SUCCESS)) {
3139 
3140 		return (USB_FAILURE);
3141 	}
3142 	hidp->hid_polled_console_info = NULL;
3143 
3144 	return (USB_SUCCESS);
3145 }
3146 
3147 
3148 /*
3149  * hid_polled_input_enter:
3150  *	This is the routine that is called in polled mode to save the USB
3151  *	state information before using the USB keyboard as an input device.
3152  *	This routine, and all of the routines that it calls, are responsible
3153  *	for saving any state information so that it can be restored when
3154  *	polling mode is over.
3155  */
3156 static int
3157 /* ARGSUSED */
3158 hid_polled_input_enter(hid_polled_handle_t hid_polled_inputp)
3159 {
3160 	hid_state_t *hidp = (hid_state_t *)hid_polled_inputp;
3161 
3162 	/*
3163 	 * Call the lower layers to tell them to save any state information.
3164 	 */
3165 	(void) usb_console_input_enter(hidp->hid_polled_console_info);
3166 
3167 	return (USB_SUCCESS);
3168 }
3169 
3170 
3171 /*
3172  * hid_polled_read :
3173  *	This is the routine that is called in polled mode when it wants to read
3174  *	a character.  We will call to the lower layers to see if there is any
3175  *	input data available.  If there is USB scancodes available, we will
3176  *	give them back.
3177  */
3178 static int
3179 hid_polled_read(hid_polled_handle_t hid_polled_input, uchar_t **buffer)
3180 {
3181 	hid_state_t *hidp = (hid_state_t *)hid_polled_input;
3182 	uint_t			num_bytes;
3183 
3184 	/*
3185 	 * Call the lower layers to get the character from the controller.
3186 	 * The lower layers will return the number of characters that
3187 	 * were put in the raw buffer.	The address of the raw buffer
3188 	 * was passed down to the lower layers during hid_polled_init.
3189 	 */
3190 	if (usb_console_read(hidp->hid_polled_console_info,
3191 	    &num_bytes) != USB_SUCCESS) {
3192 
3193 		return (0);
3194 	}
3195 
3196 	/*LINTED*/
3197 	_NOTE(NO_COMPETING_THREADS_NOW);
3198 
3199 	*buffer = hidp->hid_polled_raw_buf;
3200 
3201 	/*LINTED*/
3202 	_NOTE(COMPETING_THREADS_NOW);
3203 
3204 	/*
3205 	 * Return the number of characters that were copied into the
3206 	 * polled buffer.
3207 	 */
3208 	return (num_bytes);
3209 }
3210 
3211 
3212 /*
3213  * hid_polled_input_exit :
3214  *	This is the routine that is called in polled mode  when it is giving up
3215  *	control of the USB keyboard.  This routine, and the lower layer routines
3216  *	that it calls, are responsible for restoring the controller state to the
3217  *	state it was in before polled mode.
3218  */
3219 static int
3220 hid_polled_input_exit(hid_polled_handle_t hid_polled_inputp)
3221 {
3222 	hid_state_t *hidp = (hid_state_t *)hid_polled_inputp;
3223 
3224 	/*
3225 	 * Call the lower layers to restore any state information.
3226 	 */
3227 	(void) usb_console_input_exit(hidp->hid_polled_console_info);
3228 
3229 	return (0);
3230 }
3231 
3232 static void
3233 hid_consconfig_relink(void *arg)
3234 {
3235 	hid_state_t *hidp = (hid_state_t *)arg;
3236 
3237 	consconfig_link(ddi_driver_major(hidp->hid_dip),
3238 	    HID_MINOR_MAKE_INTERNAL(hidp->hid_minor));
3239 }
3240