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