xref: /illumos-gate/usr/src/uts/common/io/usb/clients/hid/hid.c (revision 5279807d7e1818eac6f90ac640b7a89cdb37522d)
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 	hidp->hid_km = B_FALSE;
669 
670 	/* create internal path for virtual */
671 	if (strcmp(minor_name, "mouse") == 0) {
672 		hidp->hid_km = B_TRUE;	/* mouse */
673 		if (ddi_create_internal_pathname(dip, "internal_mouse", S_IFCHR,
674 		    HID_CONSTRUCT_INTERNAL_MINOR(instance)) != DDI_SUCCESS) {
675 
676 			goto fail;
677 		}
678 	}
679 
680 	if (strcmp(minor_name, "keyboard") == 0) {
681 		hidp->hid_km = B_TRUE;	/* keyboard */
682 		if (ddi_create_internal_pathname(dip, "internal_keyboard",
683 		    S_IFCHR, HID_CONSTRUCT_INTERNAL_MINOR(instance)) !=
684 		    DDI_SUCCESS) {
685 
686 			goto fail;
687 		}
688 	}
689 
690 	mutex_enter(&hidp->hid_mutex);
691 	hidp->hid_attach_flags |= HID_MINOR_NODES;
692 	hidp->hid_dev_state = USB_DEV_ONLINE;
693 	mutex_exit(&hidp->hid_mutex);
694 
695 	/* register for all events */
696 	if (usb_register_event_cbs(dip, &hid_events, 0) != USB_SUCCESS) {
697 		USB_DPRINTF_L2(PRINT_MASK_ATTA, hidp->hid_log_handle,
698 		    "usb_register_event_cbs failed");
699 
700 		goto fail;
701 	}
702 
703 	/* now create components to power manage this device */
704 	hid_create_pm_components(dip, hidp);
705 	hid_pm_busy_component(hidp);
706 	(void) pm_raise_power(dip, 0, USB_DEV_OS_FULL_PWR);
707 	hid_pm_idle_component(hidp);
708 
709 	/*
710 	 * report device
711 	 */
712 	ddi_report_dev(dip);
713 
714 	USB_DPRINTF_L4(PRINT_MASK_ATTA, hidp->hid_log_handle,
715 	    "hid_attach: End");
716 
717 	return (DDI_SUCCESS);
718 
719 fail:
720 	if (hidp) {
721 		USB_DPRINTF_L2(PRINT_MASK_ATTA, hidp->hid_log_handle,
722 		    "hid_attach: fail");
723 		hid_detach_cleanup(dip, hidp);
724 	}
725 
726 	return (DDI_FAILURE);
727 }
728 
729 
730 /*
731  * hid_detach :
732  *	Gets called at the time of detach.
733  */
734 static int
735 hid_detach(dev_info_t *dip, ddi_detach_cmd_t	cmd)
736 {
737 	int instance = ddi_get_instance(dip);
738 	hid_state_t	*hidp;
739 	int		rval = DDI_FAILURE;
740 
741 	hidp = ddi_get_soft_state(hid_statep, instance);
742 
743 	USB_DPRINTF_L4(PRINT_MASK_ALL, hidp->hid_log_handle, "hid_detach");
744 
745 	switch (cmd) {
746 	case DDI_DETACH:
747 		/*
748 		 * Undo	what we	did in client_attach, freeing resources
749 		 * and removing	things we installed.  The system
750 		 * framework guarantees	we are not active with this devinfo
751 		 * node	in any other entry points at this time.
752 		 */
753 		hid_detach_cleanup(dip, hidp);
754 
755 		return (DDI_SUCCESS);
756 	case DDI_SUSPEND:
757 		rval = hid_cpr_suspend(hidp);
758 
759 		return (rval == USB_SUCCESS ? DDI_SUCCESS : DDI_FAILURE);
760 	default:
761 		break;
762 	}
763 
764 	return (rval);
765 }
766 
767 /*
768  * hid_open :
769  *	Open entry point: Opens the interrupt pipe.  Sets up queues.
770  */
771 /*ARGSUSED*/
772 static int
773 hid_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
774 {
775 	int no_of_ep = 0;
776 	int rval;
777 	int instance;
778 	hid_state_t *hidp;
779 	hid_queue_t *hidq, *tmpq;
780 	minor_t minor = getminor(*devp);
781 
782 	instance = HID_MINOR_TO_INSTANCE(minor);
783 
784 	hidp = ddi_get_soft_state(hid_statep, instance);
785 	if (hidp == NULL) {
786 
787 		return (ENXIO);
788 	}
789 
790 	USB_DPRINTF_L4(PRINT_MASK_OPEN, hidp->hid_log_handle,
791 	    "hid_open: Begin");
792 
793 	if (sflag) {
794 		/* clone open NOT supported here */
795 		return (ENXIO);
796 	}
797 
798 	if (!(flag & FREAD)) {
799 		return (EIO);
800 	}
801 
802 	/*
803 	 * This is a workaround:
804 	 *	Currently, if we open an already disconnected device, and send
805 	 *	a CONSOPENPOLL ioctl to it, the system will panic, please refer
806 	 *	to the processing HID_OPEN_POLLED_INPUT ioctl in the routine
807 	 *	hid_mctl_receive().
808 	 *	The consconfig_dacf module need this interface to detect if the
809 	 *	device is already disconnnected.
810 	 */
811 	mutex_enter(&hidp->hid_mutex);
812 	if (!hidp->hid_km || (HID_IS_INTERNAL_OPEN(minor) &&
813 	    (hidp->hid_dev_state == USB_DEV_DISCONNECTED))) {
814 		mutex_exit(&hidp->hid_mutex);
815 		return (ENODEV);
816 	}
817 
818 	tmpq = hidp->hid_queue_list;
819 	while (tmpq != NULL) {
820 		if (minor == tmpq->hidq_minor) {
821 			if (q == tmpq->hidq_queue) {
822 				mutex_exit(&hidp->hid_mutex);
823 				return (0);
824 			} else {
825 				mutex_exit(&hidp->hid_mutex);
826 				return (EBUSY);
827 			}
828 		}
829 		tmpq = tmpq->hidq_next;
830 	}
831 	mutex_exit(&hidp->hid_mutex);
832 
833 	/*
834 	 * Add this queue to the head of the queue list. Only the list
835 	 * head (active queue) gets input. Other (older) queues will
836 	 * be activated after the (current) active one is closed.
837 	 */
838 	hidq = kmem_zalloc(sizeof (hid_queue_t), KM_SLEEP);
839 	hidq->hidq_statep = hidp;
840 	hidq->hidq_queue = q;
841 	hidq->hidq_minor = minor;
842 	q->q_ptr = hidq;
843 	WR(q)->q_ptr = hidq;
844 
845 	mutex_enter(&hidp->hid_mutex);
846 	hidq->hidq_next = hidp->hid_queue_list;
847 	hidp->hid_queue_list = hidq;
848 
849 	/* just return in case that pipes already open */
850 	if (hidp->hid_queue_list->hidq_next) {
851 		/*
852 		 * Two queues are supported by now:
853 		 * one external (aka. physical) and one virtual (aka. internal)
854 		 */
855 		mutex_exit(&hidp->hid_mutex);
856 		qprocson(q);
857 		return (0);
858 	}
859 
860 	hidp->hid_interrupt_pipe = NULL;
861 	no_of_ep = hidp->hid_if_descr.bNumEndpoints;
862 	mutex_exit(&hidp->hid_mutex);
863 
864 	/* Check if interrupt endpoint exists */
865 	if (no_of_ep > 0) {
866 		/* Open the interrupt pipe */
867 		if (usb_pipe_open(hidp->hid_dip,
868 		    &hidp->hid_ep_intr_descr,
869 		    &hidp->hid_intr_pipe_policy, USB_FLAGS_SLEEP,
870 		    &hidp->hid_interrupt_pipe) !=
871 		    USB_SUCCESS) {
872 
873 			mutex_enter(&hidp->hid_mutex);
874 			ASSERT(hidq == hidp->hid_queue_list);
875 			hidp->hid_queue_list = hidq->hidq_next;
876 			mutex_exit(&hidp->hid_mutex);
877 
878 			q->q_ptr = NULL;
879 			WR(q)->q_ptr = NULL;
880 			kmem_free(hidq, sizeof (hid_queue_t));
881 
882 			return (EIO);
883 		}
884 	}
885 
886 	hid_pm_busy_component(hidp);
887 	(void) pm_raise_power(hidp->hid_dip, 0, USB_DEV_OS_FULL_PWR);
888 
889 	mutex_enter(&hidp->hid_mutex);
890 	hidp->hid_streams_flags = HID_STREAMS_OPEN;
891 	mutex_exit(&hidp->hid_mutex);
892 
893 	qprocson(q);
894 
895 	mutex_enter(&hidp->hid_mutex);
896 
897 	if ((rval = hid_start_intr_polling(hidp)) != USB_SUCCESS) {
898 		USB_DPRINTF_L2(PRINT_MASK_OPEN, hidp->hid_log_handle,
899 		    "unable to start intr pipe polling. rval = %d", rval);
900 
901 		hidp->hid_streams_flags = HID_STREAMS_DISMANTLING;
902 		mutex_exit(&hidp->hid_mutex);
903 
904 		usb_pipe_close(hidp->hid_dip, hidp->hid_interrupt_pipe,
905 		    USB_FLAGS_SLEEP, NULL, NULL);
906 
907 		mutex_enter(&hidp->hid_mutex);
908 		hidp->hid_interrupt_pipe = NULL;
909 		hidp->hid_queue_list = hidq->hidq_next;
910 		mutex_exit(&hidp->hid_mutex);
911 
912 		qprocsoff(q);
913 		q->q_ptr = NULL;
914 		WR(q)->q_ptr = NULL;
915 		kmem_free(hidq, sizeof (hid_queue_t));
916 
917 		hid_pm_idle_component(hidp);
918 
919 		return (EIO);
920 	}
921 	mutex_exit(&hidp->hid_mutex);
922 
923 	USB_DPRINTF_L4(PRINT_MASK_OPEN, hidp->hid_log_handle, "hid_open: End");
924 
925 	/*
926 	 * Keyboard and mouse is Power managed by device activity.
927 	 * All other devices go busy on open and idle on close.
928 	 */
929 	switch (hidp->hid_pm->hid_pm_strategy) {
930 	case HID_PM_ACTIVITY:
931 		hid_pm_idle_component(hidp);
932 
933 		break;
934 	default:
935 
936 		break;
937 	}
938 
939 	return (0);
940 }
941 
942 
943 /*
944  * hid_close :
945  *	Close entry point.
946  */
947 /*ARGSUSED*/
948 static int
949 hid_close(queue_t *q, int flag, cred_t *credp)
950 {
951 	hid_queue_t	*hidq = (hid_queue_t *)q->q_ptr, *prevq = NULL;
952 	hid_state_t	*hidp = hidq->hidq_statep;
953 	queue_t		*wq;
954 	mblk_t		*mp;
955 	int		str_flags;
956 
957 	USB_DPRINTF_L4(PRINT_MASK_CLOSE, hidp->hid_log_handle, "hid_close:");
958 
959 	mutex_enter(&hidp->hid_mutex);
960 	str_flags = hidp->hid_streams_flags;
961 	hidp->hid_streams_flags = HID_STREAMS_DISMANTLING;
962 	mutex_exit(&hidp->hid_mutex);
963 
964 	/*
965 	 * In case there are any outstanding requests on
966 	 * the default pipe, wait forever for them to complete.
967 	 */
968 	(void) usb_pipe_drain_reqs(hidp->hid_dip,
969 	    hidp->hid_default_pipe, 0, USB_FLAGS_SLEEP, NULL, 0);
970 
971 	mutex_enter(&hidp->hid_mutex);
972 	wq = WR(q);
973 	/* drain any M_CTLS on the WQ */
974 	while (mp = getq(wq)) {
975 		hid_qreply_merror(wq, mp, EIO);
976 		mutex_exit(&hidp->hid_mutex);
977 		hid_pm_idle_component(hidp);
978 		mutex_enter(&hidp->hid_mutex);
979 	}
980 	mutex_exit(&hidp->hid_mutex);
981 
982 	qprocsoff(q);
983 
984 	mutex_enter(&hidp->hid_mutex);
985 	hidq = hidp->hid_queue_list;
986 	while (hidq != NULL) {
987 		if (hidq->hidq_queue == q) {
988 			break;
989 		}
990 		prevq = hidq;
991 		hidq = hidq->hidq_next;
992 	}
993 	ASSERT(hidq && hidq == q->q_ptr);
994 	q->q_ptr = NULL;
995 	wq->q_ptr = NULL;
996 
997 	if (prevq != NULL) {
998 		prevq->hidq_next = hidq->hidq_next;
999 	} else {
1000 		hidp->hid_queue_list = hidq->hidq_next;
1001 	}
1002 	kmem_free(hidq, sizeof (hid_queue_t));
1003 
1004 	/* just return in case that any queue is active */
1005 	if (hidp->hid_queue_list) {
1006 		hidp->hid_streams_flags = str_flags;
1007 		mutex_exit(&hidp->hid_mutex);
1008 		return (0);
1009 	}
1010 
1011 	/* all queues are closed, close USB pipes */
1012 	hid_close_intr_pipe(hidp);
1013 	mutex_exit(&hidp->hid_mutex);
1014 
1015 	/*
1016 	 * Devices other than keyboard/mouse go idle on close.
1017 	 */
1018 	switch (hidp->hid_pm->hid_pm_strategy) {
1019 	case HID_PM_ACTIVITY:
1020 
1021 		break;
1022 	default:
1023 		hid_pm_idle_component(hidp);
1024 
1025 		break;
1026 	}
1027 	USB_DPRINTF_L4(PRINT_MASK_CLOSE, hidp->hid_log_handle,
1028 	    "hid_close: End");
1029 
1030 	return (0);
1031 }
1032 
1033 
1034 /*
1035  * hid_wput :
1036  *	write put routine for the hid module
1037  */
1038 static int
1039 hid_wput(queue_t *q, mblk_t *mp)
1040 {
1041 	hid_queue_t	*hidq = (hid_queue_t *)q->q_ptr;
1042 	hid_state_t	*hidp = hidq->hidq_statep;
1043 	int		error = USB_SUCCESS;
1044 
1045 	USB_DPRINTF_L4(PRINT_MASK_ALL, hidp->hid_log_handle,
1046 	    "hid_wput: Begin");
1047 
1048 	/* See if the upper module is passing the right thing */
1049 	ASSERT(mp != NULL);
1050 	ASSERT(mp->b_datap != NULL);
1051 
1052 	switch (mp->b_datap->db_type) {
1053 	case M_FLUSH:  /* Canonical flush handling */
1054 		if (*mp->b_rptr & FLUSHW) {
1055 			flushq(q, FLUSHDATA);
1056 		}
1057 
1058 		/* read queue not used so just send up */
1059 		if (*mp->b_rptr & FLUSHR) {
1060 			*mp->b_rptr &= ~FLUSHW;
1061 			qreply(q, mp);
1062 		} else {
1063 			freemsg(mp);
1064 		}
1065 
1066 		break;
1067 	case M_IOCTL:
1068 		hid_ioctl(q, mp);
1069 
1070 		break;
1071 	case M_CTL:
1072 		/* we are busy now */
1073 		hid_pm_busy_component(hidp);
1074 
1075 		if (q->q_first) {
1076 			(void) putq(q, mp);
1077 		} else {
1078 			error = hid_mctl_receive(q, mp);
1079 			switch (error) {
1080 			case HID_ENQUEUE:
1081 				/*
1082 				 * put this mblk on the WQ for the wsrv to
1083 				 * process
1084 				 */
1085 				(void) putq(q, mp);
1086 
1087 				break;
1088 			case HID_INPROGRESS:
1089 				/* request has been queued to the device */
1090 
1091 				break;
1092 			case HID_SUCCESS:
1093 				/*
1094 				 * returned by M_CTLS that are processed
1095 				 * immediately
1096 				 */
1097 
1098 				/* FALLTHRU */
1099 			case HID_FAILURE:
1100 			default:
1101 				hid_pm_idle_component(hidp);
1102 				break;
1103 			}
1104 		}
1105 		break;
1106 	default:
1107 		hid_qreply_merror(q, mp, EINVAL);
1108 		error = USB_FAILURE;
1109 		break;
1110 	}
1111 
1112 	USB_DPRINTF_L4(PRINT_MASK_ALL, hidp->hid_log_handle,
1113 	    "hid_wput: End");
1114 
1115 	return (DDI_SUCCESS);
1116 }
1117 
1118 
1119 /*
1120  * hid_wsrv :
1121  *	Write service routine for hid. When a message arrives through
1122  *	hid_wput(), it is kept in write queue to be serviced later.
1123  */
1124 static int
1125 hid_wsrv(queue_t *q)
1126 {
1127 	hid_queue_t	*hidq = (hid_queue_t *)q->q_ptr;
1128 	hid_state_t	*hidp = hidq->hidq_statep;
1129 	int		error;
1130 	mblk_t		*mp;
1131 
1132 	USB_DPRINTF_L4(PRINT_MASK_ALL, hidp->hid_log_handle,
1133 	    "hid_wsrv: Begin");
1134 
1135 	mutex_enter(&hidp->hid_mutex);
1136 	USB_DPRINTF_L4(PRINT_MASK_ALL, hidp->hid_log_handle,
1137 	    "hid_wsrv: dev_state: %s",
1138 	    usb_str_dev_state(hidp->hid_dev_state));
1139 
1140 	/*
1141 	 * raise power if we are powered down. It is OK to block here since
1142 	 * we have a separate thread to process this STREAM
1143 	 */
1144 	if (hidp->hid_dev_state == USB_DEV_PWRED_DOWN) {
1145 		mutex_exit(&hidp->hid_mutex);
1146 		(void) pm_raise_power(hidp->hid_dip, 0, USB_DEV_OS_FULL_PWR);
1147 		mutex_enter(&hidp->hid_mutex);
1148 	}
1149 
1150 	/*
1151 	 * continue servicing all the M_CTL's till the queue is empty
1152 	 * or the device gets disconnected or till a hid_close()
1153 	 */
1154 	while ((hidp->hid_dev_state == USB_DEV_ONLINE) &&
1155 	    (hidp->hid_streams_flags != HID_STREAMS_DISMANTLING) &&
1156 	    ((mp = getq(q)) != NULL)) {
1157 
1158 		/* Send a message down */
1159 		mutex_exit(&hidp->hid_mutex);
1160 		error = hid_mctl_receive(q, mp);
1161 		switch (error) {
1162 		case HID_ENQUEUE:
1163 			/* put this mblk back on q to preserve order */
1164 			(void) putbq(q, mp);
1165 
1166 			break;
1167 		case HID_INPROGRESS:
1168 			/* request has been queued to the device */
1169 
1170 			break;
1171 		case HID_SUCCESS:
1172 		case HID_FAILURE:
1173 		default:
1174 			hid_pm_idle_component(hidp);
1175 
1176 			break;
1177 		}
1178 		mutex_enter(&hidp->hid_mutex);
1179 	}
1180 	mutex_exit(&hidp->hid_mutex);
1181 	USB_DPRINTF_L4(PRINT_MASK_ALL, hidp->hid_log_handle,
1182 	    "hid_wsrv: End");
1183 
1184 	return (DDI_SUCCESS);
1185 }
1186 
1187 
1188 /*
1189  * hid_power:
1190  *	power entry point
1191  */
1192 static int
1193 hid_power(dev_info_t *dip, int comp, int level)
1194 {
1195 	int		instance = ddi_get_instance(dip);
1196 	hid_state_t	*hidp;
1197 	hid_power_t	*hidpm;
1198 	int		retval;
1199 
1200 	hidp = ddi_get_soft_state(hid_statep, instance);
1201 
1202 	USB_DPRINTF_L3(PRINT_MASK_PM, hidp->hid_log_handle, "hid_power:"
1203 	    " hid_state: comp=%d level=%d", comp, level);
1204 
1205 	/* check if we are transitioning to a legal power level */
1206 	mutex_enter(&hidp->hid_mutex);
1207 	hidpm = hidp->hid_pm;
1208 
1209 	if (USB_DEV_PWRSTATE_OK(hidpm->hid_pwr_states, level)) {
1210 
1211 		USB_DPRINTF_L2(PRINT_MASK_PM, hidp->hid_log_handle,
1212 		    "hid_power: illegal level=%d hid_pwr_states=%d",
1213 		    level, hidpm->hid_pwr_states);
1214 
1215 		mutex_exit(&hidp->hid_mutex);
1216 
1217 		return (DDI_FAILURE);
1218 	}
1219 
1220 	switch (level) {
1221 	case USB_DEV_OS_PWR_OFF:
1222 		retval = hid_pwrlvl0(hidp);
1223 		break;
1224 	case USB_DEV_OS_PWR_1:
1225 		retval = hid_pwrlvl1(hidp);
1226 		break;
1227 	case USB_DEV_OS_PWR_2:
1228 		retval = hid_pwrlvl2(hidp);
1229 		break;
1230 	case USB_DEV_OS_FULL_PWR:
1231 		retval = hid_pwrlvl3(hidp);
1232 		break;
1233 	default:
1234 		retval = USB_FAILURE;
1235 		break;
1236 	}
1237 
1238 	mutex_exit(&hidp->hid_mutex);
1239 
1240 	return ((retval == USB_SUCCESS) ? DDI_SUCCESS : DDI_FAILURE);
1241 }
1242 
1243 
1244 /*
1245  * hid_interrupt_pipe_callback:
1246  *	Callback function for the hid intr pipe. This function is called by
1247  *	USBA when a buffer has been filled. This driver does not cook the data,
1248  *	it just sends the message up.
1249  */
1250 static void
1251 hid_interrupt_pipe_callback(usb_pipe_handle_t pipe, usb_intr_req_t *req)
1252 {
1253 	hid_state_t *hidp = (hid_state_t *)req->intr_client_private;
1254 	queue_t	*q;
1255 
1256 	USB_DPRINTF_L4(PRINT_MASK_ALL, hidp->hid_log_handle,
1257 	    "hid_interrupt_pipe_callback: ph = 0x%p req = 0x%p",
1258 	    (void *)pipe, (void *)req);
1259 
1260 	hid_pm_busy_component(hidp);
1261 
1262 	mutex_enter(&hidp->hid_mutex);
1263 
1264 	/*
1265 	 * If hid_close() is in progress, we shouldn't try accessing queue
1266 	 * Otherwise indicate that a putnext is going to happen, so
1267 	 * if close after this, that should wait for the putnext to finish.
1268 	 */
1269 	if (hidp->hid_streams_flags != HID_STREAMS_DISMANTLING) {
1270 		/*
1271 		 * Check if data can be put to the next queue.
1272 		 */
1273 		if (!canputnext(hidp->hid_queue_list->hidq_queue)) {
1274 			USB_DPRINTF_L2(PRINT_MASK_ALL, hidp->hid_log_handle,
1275 			    "Buffer flushed when overflowed.");
1276 
1277 			/* Flush the queue above */
1278 			hid_flush(hidp->hid_queue_list->hidq_queue);
1279 			mutex_exit(&hidp->hid_mutex);
1280 		} else {
1281 			q = hidp->hid_queue_list->hidq_queue;
1282 			mutex_exit(&hidp->hid_mutex);
1283 
1284 			/* Put data upstream */
1285 			putnext(q, req->intr_data);
1286 
1287 			/* usb_free_intr_req should not free data */
1288 			req->intr_data = NULL;
1289 		}
1290 	} else {
1291 		mutex_exit(&hidp->hid_mutex);
1292 	}
1293 
1294 	/* free request and data */
1295 	usb_free_intr_req(req);
1296 	hid_pm_idle_component(hidp);
1297 }
1298 
1299 
1300 /*
1301  * hid_default_pipe_callback :
1302  *	Callback routine for the asynchronous control transfer
1303  *	Called from hid_send_async_ctrl_request() where we open
1304  *	the pipe in exclusive mode
1305  */
1306 static void
1307 hid_default_pipe_callback(usb_pipe_handle_t pipe, usb_ctrl_req_t *req)
1308 {
1309 	hid_default_pipe_arg_t *hid_default_pipe_arg =
1310 	    (hid_default_pipe_arg_t *)req->ctrl_client_private;
1311 	queue_t		*wq = hid_default_pipe_arg->hid_default_pipe_arg_queue;
1312 	queue_t		*rq = RD(wq);
1313 	hid_queue_t	*hidq = (hid_queue_t *)wq->q_ptr;
1314 	hid_state_t	*hidp = hidq->hidq_statep;
1315 	mblk_t		*mctl_mp;
1316 	mblk_t		*data = NULL;
1317 
1318 	USB_DPRINTF_L4(PRINT_MASK_ALL, hidp->hid_log_handle,
1319 	    "hid_default_pipe_callback: "
1320 	    "ph = 0x%p, req = 0x%p, data= 0x%p",
1321 	    (void *)pipe, (void *)req, (void *)data);
1322 
1323 	ASSERT((req->ctrl_cb_flags & USB_CB_INTR_CONTEXT) == 0);
1324 
1325 	if (req->ctrl_data) {
1326 		data = req->ctrl_data;
1327 		req->ctrl_data = NULL;
1328 	}
1329 
1330 	/*
1331 	 * Free the b_cont of the original message that was sent down.
1332 	 */
1333 	mctl_mp = hid_default_pipe_arg->hid_default_pipe_arg_mblk;
1334 	freemsg(mctl_mp->b_cont);
1335 
1336 	/* chain the mblk received to the original & send it up */
1337 	mctl_mp->b_cont = data;
1338 
1339 	if (canputnext(rq)) {
1340 		putnext(rq, mctl_mp);
1341 	} else {
1342 		freemsg(mctl_mp); /* avoid leak */
1343 	}
1344 
1345 	/*
1346 	 * Free the argument for the asynchronous callback
1347 	 */
1348 	kmem_free(hid_default_pipe_arg, sizeof (hid_default_pipe_arg_t));
1349 
1350 	/*
1351 	 * Free the control pipe request structure.
1352 	 */
1353 	usb_free_ctrl_req(req);
1354 
1355 	mutex_enter(&hidp->hid_mutex);
1356 	hidp->hid_default_pipe_req--;
1357 	ASSERT(hidp->hid_default_pipe_req >= 0);
1358 	mutex_exit(&hidp->hid_mutex);
1359 
1360 	hid_pm_idle_component(hidp);
1361 	qenable(wq);
1362 }
1363 
1364 
1365 /*
1366  * hid_interrupt_pipe_exception_callback:
1367  *	Exception callback routine for interrupt pipe. If there is any data,
1368  *	destroy it. No threads are waiting for the exception callback.
1369  */
1370 /*ARGSUSED*/
1371 static void
1372 hid_interrupt_pipe_exception_callback(usb_pipe_handle_t pipe,
1373     usb_intr_req_t *req)
1374 {
1375 	hid_state_t	*hidp = (hid_state_t *)req->intr_client_private;
1376 	mblk_t		*data = req->intr_data;
1377 	usb_cb_flags_t	flags = req->intr_cb_flags;
1378 	int		rval;
1379 
1380 	USB_DPRINTF_L2(PRINT_MASK_ALL, hidp->hid_log_handle,
1381 	    "hid_interrupt_pipe_exception_callback: "
1382 	    "completion_reason = 0x%x, data = 0x%p, flag = 0x%x",
1383 	    req->intr_completion_reason, (void *)data, req->intr_cb_flags);
1384 
1385 	ASSERT((req->intr_cb_flags & USB_CB_INTR_CONTEXT) == 0);
1386 
1387 	if (((flags & USB_CB_FUNCTIONAL_STALL) != 0) &&
1388 	    ((flags & USB_CB_STALL_CLEARED) == 0)) {
1389 		USB_DPRINTF_L2(PRINT_MASK_ALL,
1390 		    hidp->hid_log_handle,
1391 		    "hid_interrupt_pipe_exception_callback: "
1392 		    "unable to clear stall.  flags = 0x%x",
1393 		    req->intr_cb_flags);
1394 	}
1395 
1396 	mutex_enter(&hidp->hid_mutex);
1397 
1398 	switch (req->intr_completion_reason) {
1399 	case USB_CR_STOPPED_POLLING:
1400 	case USB_CR_PIPE_CLOSING:
1401 	default:
1402 
1403 		break;
1404 	case USB_CR_PIPE_RESET:
1405 	case USB_CR_NO_RESOURCES:
1406 		if ((hidp->hid_dev_state == USB_DEV_ONLINE) &&
1407 		    ((rval = hid_start_intr_polling(hidp)) !=
1408 		    USB_SUCCESS)) {
1409 			USB_DPRINTF_L2(PRINT_MASK_ALL, hidp->hid_log_handle,
1410 			    "unable to restart interrupt poll. rval = %d",
1411 			    rval);
1412 		}
1413 
1414 		break;
1415 	}
1416 
1417 	mutex_exit(&hidp->hid_mutex);
1418 
1419 	usb_free_intr_req(req);
1420 }
1421 
1422 
1423 /*
1424  * hid_default_pipe_exception_callback:
1425  *	Exception callback routine for default pipe.
1426  */
1427 /*ARGSUSED*/
1428 static void
1429 hid_default_pipe_exception_callback(usb_pipe_handle_t pipe,
1430     usb_ctrl_req_t *req)
1431 {
1432 	hid_default_pipe_arg_t *hid_default_pipe_arg =
1433 	    (hid_default_pipe_arg_t *)req->ctrl_client_private;
1434 	queue_t		*wq = hid_default_pipe_arg->hid_default_pipe_arg_queue;
1435 	queue_t		*rq = RD(wq);
1436 	hid_queue_t	*hidq = wq->q_ptr;
1437 	hid_state_t	*hidp = hidq->hidq_statep;
1438 	usb_cr_t	ctrl_completion_reason = req->ctrl_completion_reason;
1439 	mblk_t		*mp, *data = NULL;
1440 
1441 	USB_DPRINTF_L2(PRINT_MASK_ALL, hidp->hid_log_handle,
1442 	    "hid_default_pipe_exception_callback: "
1443 	    "completion_reason = 0x%x, data = 0x%p, flag = 0x%x",
1444 	    ctrl_completion_reason, (void *)data, req->ctrl_cb_flags);
1445 
1446 	ASSERT((req->ctrl_cb_flags & USB_CB_INTR_CONTEXT) == 0);
1447 
1448 	mp = hid_default_pipe_arg->hid_default_pipe_arg_mblk;
1449 
1450 	/*
1451 	 * Pass an error message up. Reuse existing mblk.
1452 	 */
1453 	if (canputnext(rq)) {
1454 		mp->b_datap->db_type = M_ERROR;
1455 		mp->b_rptr = mp->b_datap->db_base;
1456 		mp->b_wptr = mp->b_rptr + sizeof (char);
1457 		*mp->b_rptr = EIO;
1458 		putnext(rq, mp);
1459 	} else {
1460 		freemsg(mp);
1461 	}
1462 
1463 	kmem_free(hid_default_pipe_arg, sizeof (hid_default_pipe_arg_t));
1464 
1465 	mutex_enter(&hidp->hid_mutex);
1466 	hidp->hid_default_pipe_req--;
1467 	ASSERT(hidp->hid_default_pipe_req >= 0);
1468 	mutex_exit(&hidp->hid_mutex);
1469 
1470 	qenable(wq);
1471 	usb_free_ctrl_req(req);
1472 	hid_pm_idle_component(hidp);
1473 }
1474 
1475 
1476 /*
1477  * event handling:
1478  *
1479  * hid_reconnect_event_callback:
1480  *	the device was disconnected but this instance not detached, probably
1481  *	because the device was busy
1482  *
1483  *	If the same device, continue with restoring state
1484  */
1485 static int
1486 hid_restore_state_event_callback(dev_info_t *dip)
1487 {
1488 	hid_state_t	*hidp = (hid_state_t *)ddi_get_soft_state(hid_statep,
1489 	    ddi_get_instance(dip));
1490 
1491 	ASSERT(hidp != NULL);
1492 
1493 	USB_DPRINTF_L3(PRINT_MASK_EVENTS, hidp->hid_log_handle,
1494 	    "hid_restore_state_event_callback: dip=0x%p", (void *)dip);
1495 
1496 	hid_restore_device_state(dip, hidp);
1497 
1498 	return (USB_SUCCESS);
1499 }
1500 
1501 
1502 /*
1503  * hid_cpr_suspend
1504  *	Fail suspend if we can't finish outstanding i/o activity.
1505  */
1506 static int
1507 hid_cpr_suspend(hid_state_t *hidp)
1508 {
1509 	int		rval, prev_state;
1510 	int		retval = USB_FAILURE;
1511 
1512 	USB_DPRINTF_L4(PRINT_MASK_EVENTS, hidp->hid_log_handle,
1513 	    "hid_cpr_suspend: dip=0x%p", (void *)hidp->hid_dip);
1514 
1515 	mutex_enter(&hidp->hid_mutex);
1516 	switch (hidp->hid_dev_state) {
1517 	case USB_DEV_ONLINE:
1518 	case USB_DEV_PWRED_DOWN:
1519 	case USB_DEV_DISCONNECTED:
1520 		prev_state = hidp->hid_dev_state;
1521 		hidp->hid_dev_state = USB_DEV_SUSPENDED;
1522 		mutex_exit(&hidp->hid_mutex);
1523 
1524 		/* drain all request outstanding on the default control pipe */
1525 		rval = usb_pipe_drain_reqs(hidp->hid_dip,
1526 		    hidp->hid_default_pipe, hid_default_pipe_drain_timeout,
1527 		    USB_FLAGS_SLEEP, NULL, 0);
1528 
1529 		/* fail checkpoint if we haven't finished the job yet */
1530 		mutex_enter(&hidp->hid_mutex);
1531 		if ((rval != USB_SUCCESS) || (hidp->hid_default_pipe_req > 0)) {
1532 			USB_DPRINTF_L2(PRINT_MASK_EVENTS, hidp->hid_log_handle,
1533 			    "hid_cpr_suspend: "
1534 			    "device busy - can't checkpoint");
1535 
1536 			/* fall back to previous state */
1537 			hidp->hid_dev_state = prev_state;
1538 		} else {
1539 			retval = USB_SUCCESS;
1540 			hid_save_device_state(hidp);
1541 		}
1542 
1543 		break;
1544 	case USB_DEV_SUSPENDED:
1545 	default:
1546 		USB_DPRINTF_L2(PRINT_MASK_EVENTS, hidp->hid_log_handle,
1547 		    "hid_cpr_suspend: Illegal dev state: %d",
1548 		    hidp->hid_dev_state);
1549 
1550 		break;
1551 	}
1552 	mutex_exit(&hidp->hid_mutex);
1553 
1554 	return (retval);
1555 }
1556 
1557 
1558 static void
1559 hid_cpr_resume(hid_state_t *hidp)
1560 {
1561 	USB_DPRINTF_L4(PRINT_MASK_EVENTS, hidp->hid_log_handle,
1562 	    "hid_cpr_resume: dip=0x%p", (void *)hidp->hid_dip);
1563 
1564 	hid_restore_device_state(hidp->hid_dip, hidp);
1565 }
1566 
1567 
1568 /*
1569  * hid_disconnect_event_callback:
1570  *	The device has been disconnected. We either wait for
1571  *	detach or a reconnect event. Close all pipes and timeouts.
1572  */
1573 static int
1574 hid_disconnect_event_callback(dev_info_t *dip)
1575 {
1576 	hid_state_t	*hidp;
1577 	hid_queue_t	*hidq;
1578 	mblk_t		*mp;
1579 
1580 	hidp = (hid_state_t *)ddi_get_soft_state(hid_statep,
1581 	    ddi_get_instance(dip));
1582 	ASSERT(hidp != NULL);
1583 
1584 	USB_DPRINTF_L4(PRINT_MASK_EVENTS, hidp->hid_log_handle,
1585 	    "hid_disconnect_event_callback: dip=0x%p", (void *)dip);
1586 
1587 	mutex_enter(&hidp->hid_mutex);
1588 	switch (hidp->hid_dev_state) {
1589 	case USB_DEV_ONLINE:
1590 	case USB_DEV_PWRED_DOWN:
1591 		hidp->hid_dev_state = USB_DEV_DISCONNECTED;
1592 		if (hidp->hid_streams_flags == HID_STREAMS_OPEN) {
1593 
1594 			USB_DPRINTF_L2(PRINT_MASK_EVENTS, hidp->hid_log_handle,
1595 			    "busy device has been disconnected");
1596 		}
1597 		hid_save_device_state(hidp);
1598 
1599 		/*
1600 		 * Notify applications about device removal, this only
1601 		 * applies to an external (aka. physical) open. For an
1602 		 * internal open, consconfig_dacf closes the queue.
1603 		 */
1604 		hidq = hidp->hid_queue_list;
1605 		while (hidq != NULL) {
1606 			if (!HID_IS_INTERNAL_OPEN(hidq->hidq_minor)) {
1607 				mutex_exit(&hidp->hid_mutex);
1608 				mp = allocb(sizeof (uchar_t), BPRI_HI);
1609 				if (mp != NULL) {
1610 					mp->b_datap->db_type = M_ERROR;
1611 					mp->b_rptr = mp->b_datap->db_base;
1612 					mp->b_wptr = mp->b_rptr + sizeof (char);
1613 					*mp->b_rptr = ENODEV;
1614 					putnext(hidq->hidq_queue, mp);
1615 				}
1616 				mutex_enter(&hidp->hid_mutex);
1617 			}
1618 			hidq = hidq->hidq_next;
1619 		}
1620 
1621 		break;
1622 	case USB_DEV_SUSPENDED:
1623 		/* we remain suspended */
1624 
1625 		break;
1626 	default:
1627 		USB_DPRINTF_L2(PRINT_MASK_EVENTS, hidp->hid_log_handle,
1628 		    "hid_disconnect_event_callback: Illegal dev state: %d",
1629 		    hidp->hid_dev_state);
1630 
1631 		break;
1632 	}
1633 	mutex_exit(&hidp->hid_mutex);
1634 
1635 	return (USB_SUCCESS);
1636 }
1637 
1638 
1639 /*
1640  * hid_power_change_callback:
1641  *	Async callback function to notify pm_raise_power completion
1642  *	after hid_power entry point is called.
1643  */
1644 static void
1645 hid_power_change_callback(void *arg, int rval)
1646 {
1647 	hid_state_t	*hidp;
1648 	queue_t		*wq;
1649 
1650 	hidp = (hid_state_t *)arg;
1651 
1652 	USB_DPRINTF_L4(PRINT_MASK_PM, hidp->hid_log_handle,
1653 	    "hid_power_change_callback - rval: %d", rval);
1654 
1655 	mutex_enter(&hidp->hid_mutex);
1656 	hidp->hid_pm->hid_raise_power = B_FALSE;
1657 
1658 	if (hidp->hid_dev_state == USB_DEV_ONLINE) {
1659 		wq = WR(hidp->hid_queue_list->hidq_queue);
1660 		mutex_exit(&hidp->hid_mutex);
1661 
1662 		qenable(wq);
1663 
1664 	} else {
1665 		mutex_exit(&hidp->hid_mutex);
1666 	}
1667 }
1668 
1669 
1670 /*
1671  * hid_parse_hid_descr:
1672  *	Parse the hid descriptor, check after interface and after
1673  *	endpoint descriptor
1674  */
1675 static size_t
1676 hid_parse_hid_descr(
1677 	usb_hid_descr_t		*ret_descr,
1678 	size_t			ret_buf_len,
1679 	usb_alt_if_data_t	*altif_data,
1680 	usb_ep_data_t		*ep_data)
1681 {
1682 	usb_cvs_data_t *cvs;
1683 	int		which_cvs;
1684 
1685 	for (which_cvs = 0; which_cvs < altif_data->altif_n_cvs; which_cvs++) {
1686 		cvs = &altif_data->altif_cvs[which_cvs];
1687 		if (cvs->cvs_buf == NULL) {
1688 			continue;
1689 		}
1690 		if (cvs->cvs_buf[1] == USB_DESCR_TYPE_HID) {
1691 			return (usb_parse_data("ccscccs",
1692 			    cvs->cvs_buf, cvs->cvs_buf_len,
1693 			    (void *)ret_descr,
1694 			    (size_t)ret_buf_len));
1695 		}
1696 	}
1697 
1698 	/* now try after endpoint */
1699 	for (which_cvs = 0; which_cvs < ep_data->ep_n_cvs; which_cvs++) {
1700 		cvs = &ep_data->ep_cvs[which_cvs];
1701 		if (cvs->cvs_buf == NULL) {
1702 			continue;
1703 		}
1704 		if (cvs->cvs_buf[1] == USB_DESCR_TYPE_HID) {
1705 			return (usb_parse_data("ccscccs",
1706 			    cvs->cvs_buf, cvs->cvs_buf_len,
1707 			    (void *)ret_descr,
1708 			    (size_t)ret_buf_len));
1709 		}
1710 	}
1711 
1712 	return (USB_PARSE_ERROR);
1713 }
1714 
1715 
1716 /*
1717  * hid_parse_hid_descr_failure:
1718  *	If parsing of hid descriptor failed and the device is
1719  *	a keyboard or mouse, use predefined length and packet size.
1720  */
1721 static int
1722 hid_parse_hid_descr_failure(hid_state_t	*hidp)
1723 {
1724 	/*
1725 	 * Parsing hid descriptor failed, probably because the
1726 	 * device did not return a valid hid descriptor. Check to
1727 	 * see if this is a keyboard or mouse. If so, use the
1728 	 * predefined hid descriptor length and packet size.
1729 	 * Otherwise, detach and return failure.
1730 	 */
1731 	USB_DPRINTF_L1(PRINT_MASK_ATTA, hidp->hid_log_handle,
1732 	    "Parsing of hid descriptor failed");
1733 
1734 	if (hidp->hid_if_descr.bInterfaceProtocol == KEYBOARD_PROTOCOL) {
1735 		USB_DPRINTF_L2(PRINT_MASK_ATTA, hidp->hid_log_handle,
1736 		    "Set hid descriptor length to predefined "
1737 		    "USB_KB_HID_DESCR_LENGTH for keyboard.");
1738 
1739 		/* device is a keyboard */
1740 		hidp->hid_hid_descr.wReportDescriptorLength =
1741 		    USB_KB_HID_DESCR_LENGTH;
1742 
1743 		hidp->hid_packet_size = USBKPSZ;
1744 
1745 	} else if (hidp->hid_if_descr.bInterfaceProtocol ==
1746 	    MOUSE_PROTOCOL) {
1747 		USB_DPRINTF_L2(PRINT_MASK_ATTA, hidp->hid_log_handle,
1748 		    "Set hid descriptor length to predefined "
1749 		    "USB_MS_HID_DESCR_LENGTH for mouse.");
1750 
1751 		/* device is a mouse */
1752 		hidp->hid_hid_descr.wReportDescriptorLength =
1753 		    USB_MS_HID_DESCR_LENGTH;
1754 
1755 		hidp->hid_packet_size = USBMSSZ;
1756 	} else {
1757 
1758 		return (USB_FAILURE);
1759 	}
1760 
1761 	return (USB_SUCCESS);
1762 }
1763 
1764 
1765 /*
1766  * hid_handle_report_descriptor:
1767  *	Get the report descriptor, call hidparser routine to parse
1768  *	it and query the hidparser tree to get the packet size
1769  */
1770 static int
1771 hid_handle_report_descriptor(hid_state_t	*hidp,
1772 				int		interface)
1773 {
1774 	usb_cr_t		completion_reason;
1775 	usb_cb_flags_t		cb_flags;
1776 	mblk_t			*data = NULL;
1777 	hidparser_packet_info_t	hpack;
1778 	int			i;
1779 	usb_ctrl_setup_t setup = {
1780 	    USB_DEV_REQ_DEV_TO_HOST |	/* bmRequestType */
1781 	    USB_DEV_REQ_RCPT_IF,
1782 	    USB_REQ_GET_DESCR,		/* bRequest */
1783 	    USB_CLASS_DESCR_TYPE_REPORT, /* wValue */
1784 	    0,				/* wIndex: interface, fill in later */
1785 	    0,				/* wLength, fill in later  */
1786 	    0				/* attributes */
1787 	    };
1788 
1789 	/*
1790 	 * Parsing hid desciptor was successful earlier.
1791 	 * Get Report Descriptor
1792 	 */
1793 	setup.wIndex = (uint16_t)interface;
1794 	setup.wLength = hidp->hid_hid_descr.wReportDescriptorLength;
1795 	if (usb_pipe_ctrl_xfer_wait(hidp->hid_default_pipe,
1796 	    &setup,
1797 	    &data,				/* data */
1798 	    &completion_reason, &cb_flags, 0) != USB_SUCCESS) {
1799 
1800 		USB_DPRINTF_L2(PRINT_MASK_ATTA, hidp->hid_log_handle,
1801 		    "Failed to receive the Report Descriptor");
1802 		freemsg(data);
1803 
1804 		return (USB_FAILURE);
1805 
1806 	} else {
1807 		int n =  hidp->hid_hid_descr.wReportDescriptorLength;
1808 
1809 		ASSERT(data);
1810 
1811 		/* Print the report descriptor */
1812 		for (i = 0; i < n; i++) {
1813 			USB_DPRINTF_L3(PRINT_MASK_ATTA, hidp->hid_log_handle,
1814 			    "Index = %d\tvalue =0x%x", i,
1815 			    (int)(data->b_rptr[i]));
1816 		}
1817 
1818 		/* Get Report Descriptor was successful */
1819 		if (hidparser_parse_report_descriptor(
1820 		    data->b_rptr,
1821 		    hidp->hid_hid_descr.wReportDescriptorLength,
1822 		    &hidp->hid_hid_descr,
1823 		    &hidp->hid_report_descr) == HIDPARSER_SUCCESS) {
1824 
1825 			/* find max intr-in xfer length */
1826 			hidparser_find_max_packet_size_from_report_descriptor(
1827 			    hidp->hid_report_descr, &hpack);
1828 			/* round up to the nearest byte */
1829 			hidp->hid_packet_size = (hpack.max_packet_size + 7) / 8;
1830 
1831 			/* if report id is used, add more more byte for it */
1832 			if (hpack.report_id != HID_REPORT_ID_UNDEFINED) {
1833 				hidp->hid_packet_size++;
1834 			}
1835 		} else {
1836 			USB_DPRINTF_L1(PRINT_MASK_ATTA, hidp->hid_log_handle,
1837 			    "Invalid Report Descriptor");
1838 			freemsg(data);
1839 
1840 			return (USB_FAILURE);
1841 		}
1842 
1843 		freemsg(data);
1844 
1845 		return (USB_SUCCESS);
1846 	}
1847 }
1848 
1849 
1850 /*
1851  * hid_set_idle:
1852  *	Make a clas specific request to SET_IDLE.
1853  *	In this case send no reports if state has not changed.
1854  *	See HID 7.2.4.
1855  */
1856 /*ARGSUSED*/
1857 static void
1858 hid_set_idle(hid_state_t	*hidp)
1859 {
1860 	usb_cr_t	completion_reason;
1861 	usb_cb_flags_t	cb_flags;
1862 	usb_ctrl_setup_t setup = {
1863 	    USB_DEV_REQ_HOST_TO_DEV |	/* bmRequestType */
1864 	    USB_DEV_REQ_TYPE_CLASS |
1865 	    USB_DEV_REQ_RCPT_IF,
1866 	    SET_IDLE,			/* bRequest */
1867 	    DURATION,			/* wValue */
1868 	    0,				/* wIndex: interface, fill in later */
1869 	    0,				/* wLength */
1870 	    0				/* attributes */
1871 	    };
1872 
1873 	USB_DPRINTF_L4(PRINT_MASK_ATTA, hidp->hid_log_handle,
1874 	    "hid_set_idle: Begin");
1875 
1876 	setup.wIndex = hidp->hid_if_descr.bInterfaceNumber;
1877 	if (usb_pipe_ctrl_xfer_wait(
1878 	    hidp->hid_default_pipe,
1879 	    &setup,
1880 	    NULL,			/* no data to send. */
1881 	    &completion_reason, &cb_flags, 0) != USB_SUCCESS) {
1882 
1883 		USB_DPRINTF_L2(PRINT_MASK_ATTA, hidp->hid_log_handle,
1884 		    "Failed while trying to set idle,"
1885 		    "cr = %d, cb_flags = 0x%x\n",
1886 		    completion_reason, cb_flags);
1887 	}
1888 	USB_DPRINTF_L4(PRINT_MASK_ATTA, hidp->hid_log_handle,
1889 	    "hid_set_idle: End");
1890 }
1891 
1892 
1893 /*
1894  * hid_set_protocol:
1895  *	Initialize the device to set the preferred protocol
1896  */
1897 /*ARGSUSED*/
1898 static void
1899 hid_set_protocol(hid_state_t *hidp, int protocol)
1900 {
1901 	usb_cr_t	completion_reason;
1902 	usb_cb_flags_t	cb_flags;
1903 	usb_ctrl_setup_t setup;
1904 
1905 	USB_DPRINTF_L4(PRINT_MASK_ATTA, hidp->hid_log_handle,
1906 	    "hid_set_protocol(%d): Begin", protocol);
1907 
1908 	/* initialize the setup request */
1909 	setup.bmRequestType = USB_DEV_REQ_HOST_TO_DEV |
1910 	    USB_DEV_REQ_TYPE_CLASS | USB_DEV_REQ_RCPT_IF;
1911 	setup.bRequest = SET_PROTOCOL;
1912 	setup.wValue = (uint16_t)protocol;
1913 	setup.wIndex = hidp->hid_if_descr.bInterfaceNumber;
1914 	setup.wLength = 0;
1915 	setup.attrs = 0;
1916 	if (usb_pipe_ctrl_xfer_wait(
1917 	    hidp->hid_default_pipe,	/* bmRequestType */
1918 	    &setup,
1919 	    NULL,			/* no data to send */
1920 	    &completion_reason, &cb_flags, 0) != USB_SUCCESS) {
1921 		/*
1922 		 * Some devices fail to follow the specification
1923 		 * and instead of STALLing, they continously
1924 		 * NAK the SET_IDLE command. We need to reset
1925 		 * the pipe then, so that ohci doesn't panic.
1926 		 */
1927 		USB_DPRINTF_L2(PRINT_MASK_ATTA, hidp->hid_log_handle,
1928 		    "Failed while trying to set protocol:%d,"
1929 		    "cr =  %d cb_flags = 0x%x\n",
1930 		    completion_reason, cb_flags, protocol);
1931 	}
1932 
1933 	USB_DPRINTF_L4(PRINT_MASK_ATTA, hidp->hid_log_handle,
1934 	    "hid_set_protocol: End");
1935 }
1936 
1937 
1938 /*
1939  * hid_detach_cleanup:
1940  *	called by attach and detach for cleanup.
1941  */
1942 static void
1943 hid_detach_cleanup(dev_info_t *dip, hid_state_t *hidp)
1944 {
1945 	int	flags = hidp->hid_attach_flags;
1946 	int	rval;
1947 	hid_power_t	*hidpm;
1948 
1949 	USB_DPRINTF_L4(PRINT_MASK_ALL, hidp->hid_log_handle,
1950 	    "hid_detach_cleanup: Begin");
1951 
1952 	if ((hidp->hid_attach_flags & HID_LOCK_INIT) == 0) {
1953 
1954 		goto done;
1955 	}
1956 
1957 	/*
1958 	 * Disable the event callbacks first, after this point, event
1959 	 * callbacks will never get called. Note we shouldn't hold
1960 	 * mutex while unregistering events because there may be a
1961 	 * competing event callback thread. Event callbacks are done
1962 	 * with ndi mutex held and this can cause a potential deadlock.
1963 	 */
1964 	usb_unregister_event_cbs(dip, &hid_events);
1965 
1966 	mutex_enter(&hidp->hid_mutex);
1967 
1968 	hidpm = hidp->hid_pm;
1969 
1970 	USB_DPRINTF_L2(PRINT_MASK_ALL, hidp->hid_log_handle,
1971 	    "hid_detach_cleanup: hidpm=0x%p", (void *)hidpm);
1972 
1973 	if (hidpm && (hidp->hid_dev_state != USB_DEV_DISCONNECTED)) {
1974 
1975 		mutex_exit(&hidp->hid_mutex);
1976 		hid_pm_busy_component(hidp);
1977 		if (hid_is_pm_enabled(dip) == USB_SUCCESS) {
1978 
1979 			if (hidpm->hid_wakeup_enabled) {
1980 
1981 				/* First bring the device to full power */
1982 				(void) pm_raise_power(dip, 0,
1983 				    USB_DEV_OS_FULL_PWR);
1984 
1985 				/* Disable remote wakeup */
1986 				rval = usb_handle_remote_wakeup(dip,
1987 				    USB_REMOTE_WAKEUP_DISABLE);
1988 
1989 				if (rval != DDI_SUCCESS) {
1990 					USB_DPRINTF_L2(PRINT_MASK_ALL,
1991 					    hidp->hid_log_handle,
1992 					    "hid_detach_cleanup: "
1993 					    "disble remote wakeup failed, "
1994 					    "rval= %d", rval);
1995 				}
1996 			}
1997 
1998 			(void) pm_lower_power(dip, 0, USB_DEV_OS_PWR_OFF);
1999 		}
2000 		hid_pm_idle_component(hidp);
2001 		mutex_enter(&hidp->hid_mutex);
2002 	}
2003 
2004 	if (hidpm) {
2005 		freemsg(hidpm->hid_pm_pwrup);
2006 		kmem_free(hidpm, sizeof (hid_power_t));
2007 		hidp->hid_pm = NULL;
2008 	}
2009 
2010 	mutex_exit(&hidp->hid_mutex);
2011 
2012 	if (hidp->hid_report_descr != NULL) {
2013 		(void) hidparser_free_report_descriptor_handle(
2014 		    hidp->hid_report_descr);
2015 	}
2016 
2017 	if (flags & HID_MINOR_NODES) {
2018 		ddi_remove_minor_node(dip, NULL);
2019 	}
2020 
2021 	mutex_destroy(&hidp->hid_mutex);
2022 
2023 	USB_DPRINTF_L4(PRINT_MASK_ALL, hidp->hid_log_handle,
2024 	    "hid_detach_cleanup: End");
2025 
2026 done:
2027 	usb_client_detach(dip, hidp->hid_dev_data);
2028 	usb_free_log_hdl(hidp->hid_log_handle);
2029 	ddi_soft_state_free(hid_statep, hidp->hid_instance);
2030 
2031 	ddi_prop_remove_all(dip);
2032 }
2033 
2034 
2035 /*
2036  * hid_start_intr_polling:
2037  *	Allocate an interrupt request structure, initialize,
2038  *	and start interrupt transfers.
2039  */
2040 static int
2041 hid_start_intr_polling(hid_state_t *hidp)
2042 {
2043 	usb_intr_req_t	*req;
2044 	int rval = USB_SUCCESS;
2045 
2046 	USB_DPRINTF_L4(PRINT_MASK_PM, hidp->hid_log_handle,
2047 	    "hid_start_intr_polling: "
2048 	    "dev_state=%s str_flags=%d ph=0x%p",
2049 	    usb_str_dev_state(hidp->hid_dev_state), hidp->hid_streams_flags,
2050 	    (void *)hidp->hid_interrupt_pipe);
2051 
2052 	if ((hidp->hid_streams_flags == HID_STREAMS_OPEN) &&
2053 	    (hidp->hid_interrupt_pipe != NULL)) {
2054 		/*
2055 		 * initialize interrupt pipe request structure
2056 		 */
2057 		req = usb_alloc_intr_req(hidp->hid_dip, 0, USB_FLAGS_SLEEP);
2058 		req->intr_client_private = (usb_opaque_t)hidp;
2059 		req->intr_attributes = USB_ATTRS_SHORT_XFER_OK |
2060 		    USB_ATTRS_AUTOCLEARING;
2061 		req->intr_len = hidp->hid_packet_size;
2062 		req->intr_cb = hid_interrupt_pipe_callback;
2063 		req->intr_exc_cb = hid_interrupt_pipe_exception_callback;
2064 
2065 		/*
2066 		 * Start polling on the interrupt pipe.
2067 		 */
2068 		mutex_exit(&hidp->hid_mutex);
2069 
2070 		if ((rval = usb_pipe_intr_xfer(hidp->hid_interrupt_pipe, req,
2071 		    USB_FLAGS_SLEEP)) != USB_SUCCESS) {
2072 			USB_DPRINTF_L2(PRINT_MASK_PM, hidp->hid_log_handle,
2073 			    "hid_start_intr_polling failed: rval = %d",
2074 			    rval);
2075 			usb_free_intr_req(req);
2076 		}
2077 
2078 		mutex_enter(&hidp->hid_mutex);
2079 	}
2080 
2081 	USB_DPRINTF_L4(PRINT_MASK_PM, hidp->hid_log_handle,
2082 	    "hid_start_intr_polling: done, rval = %d", rval);
2083 
2084 	return (rval);
2085 }
2086 
2087 
2088 /*
2089  * hid_close_intr_pipe:
2090  *	close the interrupt pipe after draining all callbacks
2091  */
2092 static void
2093 hid_close_intr_pipe(hid_state_t *hidp)
2094 {
2095 	USB_DPRINTF_L4(PRINT_MASK_CLOSE, hidp->hid_log_handle,
2096 	    "hid_close_intr_pipe: Begin");
2097 
2098 	if (hidp->hid_interrupt_pipe) {
2099 		/*
2100 		 * Close the interrupt pipe
2101 		 */
2102 		mutex_exit(&hidp->hid_mutex);
2103 		usb_pipe_close(hidp->hid_dip, hidp->hid_interrupt_pipe,
2104 		    USB_FLAGS_SLEEP, NULL, NULL);
2105 		mutex_enter(&hidp->hid_mutex);
2106 		hidp->hid_interrupt_pipe = NULL;
2107 	}
2108 	USB_DPRINTF_L4(PRINT_MASK_CLOSE, hidp->hid_log_handle,
2109 	    "hid_close_intr_pipe: End");
2110 }
2111 
2112 
2113 /*
2114  * hid_mctl_receive:
2115  *	Handle M_CTL messages from upper stream.  If
2116  *	we don't understand the command, free message.
2117  */
2118 static int
2119 hid_mctl_receive(register queue_t *q, register mblk_t *mp)
2120 {
2121 	hid_queue_t	*hidq = (hid_queue_t *)q->q_ptr;
2122 	hid_state_t	*hidp = hidq->hidq_statep;
2123 	struct iocblk	*iocp;
2124 	int		error = HID_FAILURE;
2125 	uchar_t		request_type;
2126 	hid_req_t	*hid_req_data = NULL;
2127 	hid_polled_input_callback_t hid_polled_input;
2128 	hid_vid_pid_t	hid_vid_pid;
2129 
2130 	USB_DPRINTF_L4(PRINT_MASK_ALL, hidp->hid_log_handle,
2131 	    "hid_mctl_receive");
2132 
2133 	iocp = (struct iocblk *)mp->b_rptr;
2134 
2135 	switch (iocp->ioc_cmd) {
2136 	case HID_SET_REPORT:
2137 		/* FALLTHRU */
2138 	case HID_SET_IDLE:
2139 		/* FALLTHRU */
2140 	case HID_SET_PROTOCOL:
2141 		request_type = USB_DEV_REQ_HOST_TO_DEV |
2142 		    USB_DEV_REQ_RCPT_IF | USB_DEV_REQ_TYPE_CLASS;
2143 
2144 		break;
2145 	case HID_GET_REPORT:
2146 		/* FALLTHRU */
2147 	case HID_GET_IDLE:
2148 		/* FALLTHRU */
2149 	case HID_GET_PROTOCOL:
2150 		request_type = USB_DEV_REQ_DEV_TO_HOST |
2151 		    USB_DEV_REQ_RCPT_IF | USB_DEV_REQ_TYPE_CLASS;
2152 
2153 		break;
2154 	case HID_GET_PARSER_HANDLE:
2155 		if (canputnext(RD(q))) {
2156 			freemsg(mp->b_cont);
2157 			mp->b_cont = hid_data2mblk(
2158 			    (uchar_t *)&hidp->hid_report_descr,
2159 			    sizeof (hidp->hid_report_descr));
2160 			if (mp->b_cont == NULL) {
2161 				/*
2162 				 * can't allocate mblk, indicate
2163 				 * that nothing is returned
2164 				 */
2165 				iocp->ioc_count = 0;
2166 			} else {
2167 				iocp->ioc_count =
2168 				    sizeof (hidp->hid_report_descr);
2169 			}
2170 			qreply(q, mp);
2171 
2172 			return (HID_SUCCESS);
2173 		} else {
2174 
2175 			/* retry */
2176 			return (HID_ENQUEUE);
2177 		}
2178 	case HID_GET_VID_PID:
2179 		if (canputnext(RD(q))) {
2180 			freemsg(mp->b_cont);
2181 
2182 			hid_vid_pid.VendorId =
2183 			    hidp->hid_dev_descr->idVendor;
2184 			hid_vid_pid.ProductId =
2185 			    hidp->hid_dev_descr->idProduct;
2186 
2187 			mp->b_cont = hid_data2mblk(
2188 			    (uchar_t *)&hid_vid_pid, sizeof (hid_vid_pid_t));
2189 			if (mp->b_cont == NULL) {
2190 				/*
2191 				 * can't allocate mblk, indicate that nothing
2192 				 * is being returned.
2193 				 */
2194 				iocp->ioc_count = 0;
2195 			} else {
2196 				iocp->ioc_count =
2197 				    sizeof (hid_vid_pid_t);
2198 			}
2199 			qreply(q, mp);
2200 
2201 			return (HID_SUCCESS);
2202 		} else {
2203 
2204 			/* retry */
2205 			return (HID_ENQUEUE);
2206 		}
2207 	case HID_OPEN_POLLED_INPUT:
2208 		if (canputnext(RD(q))) {
2209 			freemsg(mp->b_cont);
2210 
2211 			/* Initialize the structure */
2212 			hid_polled_input.hid_polled_version =
2213 			    HID_POLLED_INPUT_V0;
2214 			hid_polled_input.hid_polled_read = hid_polled_read;
2215 			hid_polled_input.hid_polled_input_enter =
2216 			    hid_polled_input_enter;
2217 			hid_polled_input.hid_polled_input_exit =
2218 			    hid_polled_input_exit;
2219 			hid_polled_input.hid_polled_input_handle =
2220 			    (hid_polled_handle_t)hidp;
2221 
2222 			mp->b_cont = hid_data2mblk(
2223 			    (uchar_t *)&hid_polled_input,
2224 			    sizeof (hid_polled_input_callback_t));
2225 			if (mp->b_cont == NULL) {
2226 				/*
2227 				 * can't allocate mblk, indicate that nothing
2228 				 * is being returned.
2229 				 */
2230 				iocp->ioc_count = 0;
2231 			} else {
2232 				/* Call down into USBA */
2233 				(void) hid_polled_input_init(hidp);
2234 
2235 				iocp->ioc_count =
2236 				    sizeof (hid_polled_input_callback_t);
2237 			}
2238 			qreply(q, mp);
2239 
2240 			return (HID_SUCCESS);
2241 		} else {
2242 
2243 			/* retry */
2244 			return (HID_ENQUEUE);
2245 		}
2246 	case HID_CLOSE_POLLED_INPUT:
2247 		/* Call down into USBA */
2248 		(void) hid_polled_input_fini(hidp);
2249 
2250 		iocp->ioc_count = 0;
2251 		qreply(q, mp);
2252 
2253 		return (HID_SUCCESS);
2254 	default:
2255 		hid_qreply_merror(q, mp, EINVAL);
2256 
2257 		return (HID_FAILURE);
2258 	}
2259 
2260 	/*
2261 	 * These (device executable) commands require a hid_req_t.
2262 	 * Make sure one is present
2263 	 */
2264 	if (mp->b_cont == NULL) {
2265 		hid_qreply_merror(q, mp, EINVAL);
2266 
2267 		return (error);
2268 	} else {
2269 		hid_req_data = (hid_req_t *)mp->b_cont->b_rptr;
2270 		if ((iocp->ioc_cmd == HID_SET_REPORT) &&
2271 		    (hid_req_data->hid_req_wLength == 0)) {
2272 			hid_qreply_merror(q, mp, EINVAL);
2273 
2274 			return (error);
2275 		}
2276 	}
2277 
2278 	/*
2279 	 * Check is version no. is correct. This
2280 	 * is coming from the user
2281 	 */
2282 	if (hid_req_data->hid_req_version_no != HID_VERSION_V_0) {
2283 		hid_qreply_merror(q, mp, EINVAL);
2284 
2285 		return (error);
2286 	}
2287 
2288 	mutex_enter(&hidp->hid_mutex);
2289 	USB_DPRINTF_L4(PRINT_MASK_ALL, hidp->hid_log_handle,
2290 	    "hid_mctl_receive: dev_state=%s",
2291 	    usb_str_dev_state(hidp->hid_dev_state));
2292 
2293 	switch (hidp->hid_dev_state) {
2294 	case USB_DEV_PWRED_DOWN:
2295 		/*
2296 		 * get the device full powered. We get a callback
2297 		 * which enables the WQ and kicks off IO
2298 		 */
2299 		hidp->hid_dev_state = USB_DEV_HID_POWER_CHANGE;
2300 		mutex_exit(&hidp->hid_mutex);
2301 		if (usb_req_raise_power(hidp->hid_dip, 0,
2302 		    USB_DEV_OS_FULL_PWR, hid_power_change_callback,
2303 		    hidp, 0) != USB_SUCCESS) {
2304 			/* we retry raising power in wsrv */
2305 			mutex_enter(&hidp->hid_mutex);
2306 			hidp->hid_dev_state = USB_DEV_PWRED_DOWN;
2307 			mutex_exit(&hidp->hid_mutex);
2308 		}
2309 		error = HID_ENQUEUE;
2310 
2311 		break;
2312 	case USB_DEV_HID_POWER_CHANGE:
2313 		mutex_exit(&hidp->hid_mutex);
2314 		error = HID_ENQUEUE;
2315 
2316 		break;
2317 	case USB_DEV_ONLINE:
2318 		if (hidp->hid_streams_flags != HID_STREAMS_DISMANTLING) {
2319 			/* Send a message down */
2320 			mutex_exit(&hidp->hid_mutex);
2321 			error = hid_mctl_execute_cmd(q, request_type,
2322 			    hid_req_data, mp);
2323 			if (error == HID_FAILURE) {
2324 				hid_qreply_merror(q, mp, EIO);
2325 			}
2326 		} else {
2327 			mutex_exit(&hidp->hid_mutex);
2328 			hid_qreply_merror(q, mp, EIO);
2329 		}
2330 
2331 		break;
2332 	default:
2333 		mutex_exit(&hidp->hid_mutex);
2334 		hid_qreply_merror(q, mp, EIO);
2335 
2336 		break;
2337 	}
2338 
2339 	return (error);
2340 }
2341 
2342 
2343 /*
2344  * hid_mctl_execute_cmd:
2345  *	Send the command to the device.
2346  */
2347 static int
2348 hid_mctl_execute_cmd(queue_t *q, int request_type, hid_req_t *hid_req_data,
2349     mblk_t *mp)
2350 {
2351 	int		request_index;
2352 	struct iocblk	*iocp;
2353 	hid_default_pipe_arg_t	*def_pipe_arg;
2354 	hid_state_t	*hidp = ((hid_queue_t *)q->q_ptr)->hidq_statep;
2355 
2356 	iocp = (struct iocblk *)mp->b_rptr;
2357 	USB_DPRINTF_L4(PRINT_MASK_ALL, hidp->hid_log_handle,
2358 	    "hid_mctl_execute_cmd: iocp=0x%p", (void *)iocp);
2359 
2360 	request_index = hidp->hid_if_descr.bInterfaceNumber;
2361 
2362 	/*
2363 	 * Set up the argument to be passed back to hid
2364 	 * when the asynchronous control callback is
2365 	 * executed.
2366 	 */
2367 	def_pipe_arg = kmem_zalloc(sizeof (hid_default_pipe_arg_t), 0);
2368 
2369 	if (def_pipe_arg == NULL) {
2370 
2371 		return (HID_FAILURE);
2372 	}
2373 
2374 	def_pipe_arg->hid_default_pipe_arg_queue = q;
2375 	def_pipe_arg->hid_default_pipe_arg_mctlmsg.ioc_cmd = iocp->ioc_cmd;
2376 	def_pipe_arg->hid_default_pipe_arg_mctlmsg.ioc_count = 0;
2377 	def_pipe_arg->hid_default_pipe_arg_mblk = mp;
2378 
2379 	/*
2380 	 * Send the command down to USBA through default
2381 	 * pipe.
2382 	 */
2383 	if (hid_send_async_ctrl_request(def_pipe_arg, hid_req_data,
2384 	    request_type, iocp->ioc_cmd, request_index) != USB_SUCCESS) {
2385 
2386 		kmem_free(def_pipe_arg, sizeof (hid_default_pipe_arg_t));
2387 
2388 		return (HID_FAILURE);
2389 	}
2390 
2391 	return (HID_INPROGRESS);
2392 }
2393 
2394 
2395 /*
2396  * hid_send_async_ctrl_request:
2397  *	Send an asynchronous control request to USBA.  Since hid is a STREAMS
2398  *	driver, it is not allowed to wait in its entry points except for the
2399  *	open and close entry points.  Therefore, hid must use the asynchronous
2400  *	USBA calls.
2401  */
2402 static int
2403 hid_send_async_ctrl_request(hid_default_pipe_arg_t *hid_default_pipe_arg,
2404 			hid_req_t *hid_request,
2405 			uchar_t request_type, int request_request,
2406 			ushort_t request_index)
2407 {
2408 	queue_t		*q = hid_default_pipe_arg->hid_default_pipe_arg_queue;
2409 	hid_queue_t	*hidq = (hid_queue_t *)q->q_ptr;
2410 	hid_state_t	*hidp = hidq->hidq_statep;
2411 	usb_ctrl_req_t	*ctrl_req;
2412 	int		rval;
2413 	size_t		length = 0;
2414 
2415 	USB_DPRINTF_L4(PRINT_MASK_ALL, hidp->hid_log_handle,
2416 	    "hid_send_async_ctrl_request: "
2417 	    "rq_type=%d rq_rq=%d index=%d",
2418 	    request_type, request_request, request_index);
2419 
2420 	mutex_enter(&hidp->hid_mutex);
2421 	hidp->hid_default_pipe_req++;
2422 	mutex_exit(&hidp->hid_mutex);
2423 
2424 	/*
2425 	 * Note that ctrl_req->ctrl_data should be allocated by usba
2426 	 * only for IN requests. OUT request(e.g SET_REPORT) can have a
2427 	 * non-zero wLength value but ctrl_data would be allocated by
2428 	 * client for them.
2429 	 */
2430 	if (hid_request->hid_req_wLength >= MAX_REPORT_DATA) {
2431 		USB_DPRINTF_L2(PRINT_MASK_ALL, hidp->hid_log_handle,
2432 		    "hid_req_wLength is exceeded");
2433 		return (USB_FAILURE);
2434 	}
2435 	if ((request_type & USB_DEV_REQ_DIR_MASK) == USB_DEV_REQ_DEV_TO_HOST) {
2436 		length = hid_request->hid_req_wLength;
2437 	}
2438 
2439 	if ((ctrl_req = usb_alloc_ctrl_req(hidp->hid_dip, length, 0)) == NULL) {
2440 		USB_DPRINTF_L2(PRINT_MASK_ALL, hidp->hid_log_handle,
2441 		    "unable to alloc ctrl req. async trans failed");
2442 		mutex_enter(&hidp->hid_mutex);
2443 		hidp->hid_default_pipe_req--;
2444 		ASSERT(hidp->hid_default_pipe_req >= 0);
2445 		mutex_exit(&hidp->hid_mutex);
2446 
2447 		return (USB_FAILURE);
2448 	}
2449 
2450 	if ((request_type & USB_DEV_REQ_DIR_MASK) == USB_DEV_REQ_HOST_TO_DEV) {
2451 		ASSERT((length == 0) && (ctrl_req->ctrl_data == NULL));
2452 	}
2453 
2454 	ctrl_req->ctrl_bmRequestType	= request_type;
2455 	ctrl_req->ctrl_bRequest 	= (uint8_t)request_request;
2456 	ctrl_req->ctrl_wValue		= hid_request->hid_req_wValue;
2457 	ctrl_req->ctrl_wIndex		= request_index;
2458 	ctrl_req->ctrl_wLength		= hid_request->hid_req_wLength;
2459 	/* host to device: create a msg from hid_req_data */
2460 	if ((request_type & USB_DEV_REQ_DIR_MASK) == USB_DEV_REQ_HOST_TO_DEV) {
2461 		mblk_t *pblk = allocb(hid_request->hid_req_wLength, BPRI_HI);
2462 		if (pblk == NULL) {
2463 			usb_free_ctrl_req(ctrl_req);
2464 			return (USB_FAILURE);
2465 		}
2466 		bcopy(hid_request->hid_req_data, pblk->b_wptr,
2467 		    hid_request->hid_req_wLength);
2468 		pblk->b_wptr += hid_request->hid_req_wLength;
2469 		ctrl_req->ctrl_data = pblk;
2470 	}
2471 	ctrl_req->ctrl_attributes	= USB_ATTRS_AUTOCLEARING;
2472 	ctrl_req->ctrl_client_private	= (usb_opaque_t)hid_default_pipe_arg;
2473 	ctrl_req->ctrl_cb		= hid_default_pipe_callback;
2474 	ctrl_req->ctrl_exc_cb		= hid_default_pipe_exception_callback;
2475 
2476 	if ((rval = usb_pipe_ctrl_xfer(hidp->hid_default_pipe,
2477 	    ctrl_req, 0)) != USB_SUCCESS) {
2478 		mutex_enter(&hidp->hid_mutex);
2479 		hidp->hid_default_pipe_req--;
2480 		ASSERT(hidp->hid_default_pipe_req >= 0);
2481 		mutex_exit(&hidp->hid_mutex);
2482 
2483 		usb_free_ctrl_req(ctrl_req);
2484 		USB_DPRINTF_L2(PRINT_MASK_ALL, hidp->hid_log_handle,
2485 		    "usb_pipe_ctrl_xfer() failed. rval = %d", rval);
2486 
2487 		return (USB_FAILURE);
2488 	}
2489 
2490 	return (USB_SUCCESS);
2491 }
2492 
2493 
2494 /*
2495  * hid_ioctl:
2496  *	Hid currently doesn't handle any ioctls.  NACK
2497  *	the ioctl request.
2498  */
2499 static void
2500 hid_ioctl(register queue_t *q, register mblk_t *mp)
2501 {
2502 	register struct iocblk *iocp;
2503 
2504 	iocp = (struct iocblk *)mp->b_rptr;
2505 
2506 	iocp->ioc_rval = 0;
2507 
2508 	iocp->ioc_error = ENOTTY;
2509 
2510 	mp->b_datap->db_type = M_IOCNAK;
2511 
2512 	qreply(q, mp);
2513 }
2514 
2515 
2516 /*
2517  * hid_create_pm_components:
2518  *	Create the pm components required for power management.
2519  *	For keyboard/mouse, the components is created only if the device
2520  *	supports a remote wakeup.
2521  *	For other hid devices they are created unconditionally.
2522  */
2523 static void
2524 hid_create_pm_components(dev_info_t *dip, hid_state_t *hidp)
2525 {
2526 	hid_power_t	*hidpm;
2527 	uint_t		pwr_states;
2528 
2529 	USB_DPRINTF_L4(PRINT_MASK_PM, hidp->hid_log_handle,
2530 	    "hid_create_pm_components: Begin");
2531 
2532 	/* Allocate the state structure */
2533 	hidpm = kmem_zalloc(sizeof (hid_power_t), KM_SLEEP);
2534 	hidp->hid_pm = hidpm;
2535 	hidpm->hid_state = hidp;
2536 	hidpm->hid_raise_power = B_FALSE;
2537 	hidpm->hid_pm_capabilities = 0;
2538 	hidpm->hid_current_power = USB_DEV_OS_FULL_PWR;
2539 
2540 	switch (hidp->hid_if_descr.bInterfaceProtocol) {
2541 	case KEYBOARD_PROTOCOL:
2542 	case MOUSE_PROTOCOL:
2543 		hidpm->hid_pm_strategy = HID_PM_ACTIVITY;
2544 		if ((hid_is_pm_enabled(dip) == USB_SUCCESS) &&
2545 		    (usb_handle_remote_wakeup(dip, USB_REMOTE_WAKEUP_ENABLE) ==
2546 		    USB_SUCCESS)) {
2547 
2548 			USB_DPRINTF_L3(PRINT_MASK_PM, hidp->hid_log_handle,
2549 			    "hid_create_pm_components: Remote Wakeup Enabled");
2550 
2551 			if (usb_create_pm_components(dip, &pwr_states) ==
2552 			    USB_SUCCESS) {
2553 				hidpm->hid_wakeup_enabled = 1;
2554 				hidpm->hid_pwr_states = (uint8_t)pwr_states;
2555 			}
2556 		}
2557 
2558 		break;
2559 	default:
2560 		hidpm->hid_pm_strategy = HID_PM_OPEN_CLOSE;
2561 		if ((hid_is_pm_enabled(dip) == USB_SUCCESS) &&
2562 		    (usb_create_pm_components(dip, &pwr_states) ==
2563 		    USB_SUCCESS)) {
2564 			hidpm->hid_wakeup_enabled = 0;
2565 			hidpm->hid_pwr_states = (uint8_t)pwr_states;
2566 		}
2567 
2568 		break;
2569 	}
2570 
2571 	USB_DPRINTF_L4(PRINT_MASK_PM, hidp->hid_log_handle,
2572 	    "hid_create_pm_components: END");
2573 }
2574 
2575 
2576 /*
2577  * hid_is_pm_enabled
2578  *	Check if the device is pm enabled. Always enable
2579  *	pm on the new SUN mouse
2580  */
2581 static int
2582 hid_is_pm_enabled(dev_info_t *dip)
2583 {
2584 	hid_state_t	*hidp = ddi_get_soft_state(hid_statep,
2585 	    ddi_get_instance(dip));
2586 
2587 	if (strcmp(ddi_node_name(dip), "mouse") == 0) {
2588 		/* check for overrides first */
2589 		if (hid_pm_mouse ||
2590 		    (ddi_prop_exists(DDI_DEV_T_ANY, dip,
2591 		    (DDI_PROP_DONTPASS | DDI_PROP_NOTPROM),
2592 		    "hid-mouse-pm-enable") == 1)) {
2593 
2594 			return (USB_SUCCESS);
2595 		}
2596 
2597 		/*
2598 		 * Always enable PM for 1.05 or greater SUN mouse
2599 		 * hidp->hid_dev_descr won't be NULL.
2600 		 */
2601 		if ((hidp->hid_dev_descr->idVendor ==
2602 		    HID_SUN_MOUSE_VENDOR_ID) &&
2603 		    (hidp->hid_dev_descr->idProduct ==
2604 		    HID_SUN_MOUSE_PROD_ID) &&
2605 		    (hidp->hid_dev_descr->bcdDevice >=
2606 		    HID_SUN_MOUSE_BCDDEVICE)) {
2607 
2608 			return (USB_SUCCESS);
2609 		}
2610 	} else {
2611 
2612 		return (USB_SUCCESS);
2613 	}
2614 
2615 	return (USB_FAILURE);
2616 }
2617 
2618 
2619 /*
2620  * hid_save_device_state
2621  *	Save the current device/driver state.
2622  */
2623 static void
2624 hid_save_device_state(hid_state_t *hidp)
2625 {
2626 	hid_queue_t	*hidq;
2627 	struct iocblk	*mctlmsg;
2628 	mblk_t		*mp;
2629 	queue_t		*q;
2630 
2631 	USB_DPRINTF_L4(PRINT_MASK_EVENTS, hidp->hid_log_handle,
2632 	    "hid_save_device_state");
2633 
2634 	if (hidp->hid_streams_flags == HID_STREAMS_OPEN) {
2635 		/*
2636 		 * Send MCTLs up indicating that the device
2637 		 * will loose its state
2638 		 */
2639 		hidq = hidp->hid_queue_list;
2640 		while (hidq != NULL) {
2641 			q = hidq->hidq_queue;
2642 
2643 			mutex_exit(&hidp->hid_mutex);
2644 			if (canputnext(q)) {
2645 				mp = allocb(sizeof (struct iocblk), BPRI_HI);
2646 				if (mp != NULL) {
2647 					mp->b_datap->db_type = M_CTL;
2648 					mctlmsg = (struct iocblk *)
2649 					    mp->b_datap->db_base;
2650 					mctlmsg->ioc_cmd = HID_DISCONNECT_EVENT;
2651 					mctlmsg->ioc_count = 0;
2652 					putnext(q, mp);
2653 				}
2654 			}
2655 			mutex_enter(&hidp->hid_mutex);
2656 
2657 			hidq = hidq->hidq_next;
2658 		}
2659 		mutex_exit(&hidp->hid_mutex);
2660 		/* stop polling on the intr pipe */
2661 		usb_pipe_stop_intr_polling(hidp->hid_interrupt_pipe,
2662 		    USB_FLAGS_SLEEP);
2663 		mutex_enter(&hidp->hid_mutex);
2664 	}
2665 }
2666 
2667 
2668 /*
2669  * hid_restore_device_state:
2670  *	Set original configuration of the device.
2671  *	Reopen intr pipe.
2672  *	Enable wrq - this starts new transactions on the control pipe.
2673  */
2674 static void
2675 hid_restore_device_state(dev_info_t *dip, hid_state_t *hidp)
2676 {
2677 	hid_queue_t	*hidq;
2678 	int		rval;
2679 	queue_t		*rdq, *wrq;
2680 	hid_power_t	*hidpm;
2681 	struct iocblk	*mctlmsg;
2682 	mblk_t		*mp;
2683 
2684 	hid_pm_busy_component(hidp);
2685 	mutex_enter(&hidp->hid_mutex);
2686 
2687 	USB_DPRINTF_L4(PRINT_MASK_ATTA, hidp->hid_log_handle,
2688 	    "hid_restore_device_state: %s",
2689 	    usb_str_dev_state(hidp->hid_dev_state));
2690 
2691 	hidpm = hidp->hid_pm;
2692 	mutex_exit(&hidp->hid_mutex);
2693 
2694 	/* First bring the device to full power */
2695 	(void) pm_raise_power(dip, 0, USB_DEV_OS_FULL_PWR);
2696 
2697 	mutex_enter(&hidp->hid_mutex);
2698 	if (hidp->hid_dev_state == USB_DEV_ONLINE) {
2699 		/*
2700 		 * We failed the checkpoint, there is no need to restore
2701 		 * the device state
2702 		 */
2703 		mutex_exit(&hidp->hid_mutex);
2704 		hid_pm_idle_component(hidp);
2705 
2706 		return;
2707 	}
2708 	mutex_exit(&hidp->hid_mutex);
2709 
2710 
2711 	/* Check if we are talking to the same device */
2712 	if (usb_check_same_device(dip, hidp->hid_log_handle, USB_LOG_L2,
2713 	    PRINT_MASK_ALL, USB_CHK_BASIC|USB_CHK_CFG, NULL) != USB_SUCCESS) {
2714 
2715 		/* change the device state from suspended to disconnected */
2716 		mutex_enter(&hidp->hid_mutex);
2717 		hidp->hid_dev_state = USB_DEV_DISCONNECTED;
2718 		mutex_exit(&hidp->hid_mutex);
2719 		hid_pm_idle_component(hidp);
2720 		goto nodev;
2721 	}
2722 
2723 	hid_set_idle(hidp);
2724 	hid_set_protocol(hidp, SET_REPORT_PROTOCOL);
2725 
2726 	mutex_enter(&hidp->hid_mutex);
2727 	/* if the device had remote wakeup earlier, enable it again */
2728 	if (hidpm->hid_wakeup_enabled) {
2729 		mutex_exit(&hidp->hid_mutex);
2730 
2731 		if ((rval = usb_handle_remote_wakeup(hidp->hid_dip,
2732 		    USB_REMOTE_WAKEUP_ENABLE)) != USB_SUCCESS) {
2733 			USB_DPRINTF_L2(PRINT_MASK_ATTA,
2734 			    hidp->hid_log_handle,
2735 			    "usb_handle_remote_wakeup failed (%d)", rval);
2736 		}
2737 
2738 		mutex_enter(&hidp->hid_mutex);
2739 	}
2740 
2741 	/*
2742 	 * restart polling on the interrupt pipe only if the device
2743 	 * was previously operational (open)
2744 	 */
2745 	if (hidp->hid_streams_flags == HID_STREAMS_OPEN) {
2746 		if ((rval = hid_start_intr_polling(hidp)) != USB_SUCCESS) {
2747 			USB_DPRINTF_L3(PRINT_MASK_ATTA, hidp->hid_log_handle,
2748 			    "hid_restore_device_state:"
2749 			    "unable to restart intr pipe poll"
2750 			    " rval = %d ", rval);
2751 			/*
2752 			 * change the device state from
2753 			 * suspended to disconnected
2754 			 */
2755 			hidp->hid_dev_state = USB_DEV_DISCONNECTED;
2756 			mutex_exit(&hidp->hid_mutex);
2757 			hid_pm_idle_component(hidp);
2758 			goto nodev;
2759 		}
2760 
2761 		if (hidp->hid_dev_state == USB_DEV_DISCONNECTED) {
2762 			USB_DPRINTF_L2(PRINT_MASK_EVENTS, hidp->hid_log_handle,
2763 			    "device is being re-connected");
2764 		}
2765 
2766 		/* set the device state ONLINE */
2767 		hidp->hid_dev_state = USB_DEV_ONLINE;
2768 
2769 		/* inform upstream modules that the device is back */
2770 		hidq = hidp->hid_queue_list;
2771 		while (hidq != NULL) {
2772 			rdq = hidq->hidq_queue;
2773 			wrq = WR(rdq);
2774 
2775 			mutex_exit(&hidp->hid_mutex);
2776 			if (canputnext(rdq)) {
2777 				mp = allocb(sizeof (struct iocblk), BPRI_HI);
2778 				if (mp != NULL) {
2779 					mp->b_datap->db_type = M_CTL;
2780 					mctlmsg = (struct iocblk *)
2781 					    mp->b_datap->db_base;
2782 					mctlmsg->ioc_cmd = HID_CONNECT_EVENT;
2783 					mctlmsg->ioc_count = 0;
2784 					putnext(rdq, mp);
2785 				}
2786 			}
2787 			/* enable write side q */
2788 			qenable(wrq);
2789 			mutex_enter(&hidp->hid_mutex);
2790 
2791 			hidq = hidq->hidq_next;
2792 		}
2793 	} else {
2794 		/* set the device state ONLINE */
2795 		hidp->hid_dev_state = USB_DEV_ONLINE;
2796 	}
2797 
2798 	mutex_exit(&hidp->hid_mutex);
2799 	hid_pm_idle_component(hidp);
2800 	return;
2801 
2802 nodev:
2803 	/*
2804 	 * Notify applications about device removal. This only
2805 	 * applies to an external (aka. physical) open. Not sure how to
2806 	 * notify consconfig to close the internal minor node.
2807 	 */
2808 	mutex_enter(&hidp->hid_mutex);
2809 	hidq = hidp->hid_queue_list;
2810 	while (hidq != NULL) {
2811 		if (!HID_IS_INTERNAL_OPEN(hidq->hidq_minor)) {
2812 			mutex_exit(&hidp->hid_mutex);
2813 			mp = allocb(sizeof (uchar_t), BPRI_HI);
2814 			if (mp != NULL) {
2815 				mp->b_datap->db_type = M_ERROR;
2816 				mp->b_rptr = mp->b_datap->db_base;
2817 				mp->b_wptr = mp->b_rptr + sizeof (char);
2818 				*mp->b_rptr = ENODEV;
2819 				putnext(hidq->hidq_queue, mp);
2820 			}
2821 			mutex_enter(&hidp->hid_mutex);
2822 		}
2823 		hidq = hidq->hidq_next;
2824 	}
2825 	mutex_exit(&hidp->hid_mutex);
2826 }
2827 
2828 
2829 /*
2830  * hid_qreply_merror:
2831  *	Pass an error message up.
2832  */
2833 static void
2834 hid_qreply_merror(queue_t *q, mblk_t *mp, uchar_t errval)
2835 {
2836 	mp->b_datap->db_type = M_ERROR;
2837 	if (mp->b_cont) {
2838 		freemsg(mp->b_cont);
2839 		mp->b_cont = NULL;
2840 	}
2841 	mp->b_rptr = mp->b_datap->db_base;
2842 	mp->b_wptr = mp->b_rptr + sizeof (char);
2843 	*mp->b_rptr = errval;
2844 
2845 	qreply(q, mp);
2846 }
2847 
2848 
2849 /*
2850  * hid_data2mblk:
2851  *	Form an mblk from the given data
2852  */
2853 static mblk_t *
2854 hid_data2mblk(uchar_t *buf, int len)
2855 {
2856 	mblk_t	*mp = NULL;
2857 
2858 	if (len >= 0) {
2859 		mp = allocb(len, BPRI_HI);
2860 		if (mp) {
2861 			bcopy(buf, mp->b_datap->db_base, len);
2862 			mp->b_wptr += len;
2863 		}
2864 	}
2865 
2866 	return (mp);
2867 }
2868 
2869 
2870 /*
2871  * hid_flush :
2872  *	Flush data already sent upstreams to client module.
2873  */
2874 static void
2875 hid_flush(queue_t *q)
2876 {
2877 	/*
2878 	 * Flush pending data already sent upstream
2879 	 */
2880 	if ((q != NULL) && (q->q_next != NULL)) {
2881 		(void) putnextctl1(q, M_FLUSH, FLUSHR);
2882 	}
2883 }
2884 
2885 
2886 static void
2887 hid_pm_busy_component(hid_state_t *hid_statep)
2888 {
2889 	ASSERT(!mutex_owned(&hid_statep->hid_mutex));
2890 
2891 	if (hid_statep->hid_pm != NULL) {
2892 		mutex_enter(&hid_statep->hid_mutex);
2893 		hid_statep->hid_pm->hid_pm_busy++;
2894 
2895 		USB_DPRINTF_L4(PRINT_MASK_PM, hid_statep->hid_log_handle,
2896 		    "hid_pm_busy_component: %d",
2897 		    hid_statep->hid_pm->hid_pm_busy);
2898 
2899 		mutex_exit(&hid_statep->hid_mutex);
2900 		if (pm_busy_component(hid_statep->hid_dip, 0) != DDI_SUCCESS) {
2901 			mutex_enter(&hid_statep->hid_mutex);
2902 			hid_statep->hid_pm->hid_pm_busy--;
2903 
2904 			USB_DPRINTF_L2(PRINT_MASK_PM,
2905 			    hid_statep->hid_log_handle,
2906 			    "hid_pm_busy_component failed: %d",
2907 			    hid_statep->hid_pm->hid_pm_busy);
2908 
2909 			mutex_exit(&hid_statep->hid_mutex);
2910 		}
2911 
2912 	}
2913 }
2914 
2915 
2916 static void
2917 hid_pm_idle_component(hid_state_t *hid_statep)
2918 {
2919 	ASSERT(!mutex_owned(&hid_statep->hid_mutex));
2920 
2921 	if (hid_statep->hid_pm != NULL) {
2922 		if (pm_idle_component(hid_statep->hid_dip, 0) == DDI_SUCCESS) {
2923 			mutex_enter(&hid_statep->hid_mutex);
2924 			ASSERT(hid_statep->hid_pm->hid_pm_busy > 0);
2925 			hid_statep->hid_pm->hid_pm_busy--;
2926 
2927 			USB_DPRINTF_L4(PRINT_MASK_PM,
2928 			    hid_statep->hid_log_handle,
2929 			    "hid_pm_idle_component: %d",
2930 			    hid_statep->hid_pm->hid_pm_busy);
2931 
2932 			mutex_exit(&hid_statep->hid_mutex);
2933 		}
2934 	}
2935 }
2936 
2937 
2938 /*
2939  * hid_pwrlvl0:
2940  *	Functions to handle power transition for various levels
2941  *	These functions act as place holders to issue USB commands
2942  *	to the devices to change their power levels
2943  */
2944 static int
2945 hid_pwrlvl0(hid_state_t *hidp)
2946 {
2947 	hid_power_t	*hidpm;
2948 	int		rval;
2949 	struct iocblk	*mctlmsg;
2950 	mblk_t		*mp_lowpwr, *mp_fullpwr;
2951 	queue_t		*q;
2952 
2953 	hidpm = hidp->hid_pm;
2954 
2955 	switch (hidp->hid_dev_state) {
2956 	case USB_DEV_ONLINE:
2957 		/* Deny the powerdown request if the device is busy */
2958 		if (hidpm->hid_pm_busy != 0) {
2959 
2960 			return (USB_FAILURE);
2961 		}
2962 
2963 		if (hidp->hid_streams_flags == HID_STREAMS_OPEN) {
2964 			q = hidp->hid_queue_list->hidq_queue;
2965 			mutex_exit(&hidp->hid_mutex);
2966 			if (canputnext(q)) {
2967 				/* try to preallocate mblks */
2968 				mp_lowpwr = allocb(
2969 				    (int)sizeof (struct iocblk), BPRI_HI);
2970 				mp_fullpwr = allocb(
2971 				    (int)sizeof (struct iocblk), BPRI_HI);
2972 				if ((mp_lowpwr != NULL) &&
2973 				    (mp_fullpwr != NULL)) {
2974 					/* stop polling */
2975 					usb_pipe_stop_intr_polling(
2976 					    hidp->hid_interrupt_pipe,
2977 					    USB_FLAGS_SLEEP);
2978 
2979 					/*
2980 					 * Send an MCTL up indicating that
2981 					 * we are powering off
2982 					 */
2983 					mp_lowpwr->b_datap->db_type = M_CTL;
2984 					mctlmsg = (struct iocblk *)
2985 					    mp_lowpwr->b_datap->db_base;
2986 					mctlmsg->ioc_cmd = HID_POWER_OFF;
2987 					mctlmsg->ioc_count = 0;
2988 					putnext(q, mp_lowpwr);
2989 
2990 					/* save the full powr mblk */
2991 					mutex_enter(&hidp->hid_mutex);
2992 					hidpm->hid_pm_pwrup = mp_fullpwr;
2993 				} else {
2994 					/*
2995 					 * Since we failed to allocate one
2996 					 * or more mblks, we fail attempt
2997 					 * to go into low power this time
2998 					 */
2999 					freemsg(mp_lowpwr);
3000 					freemsg(mp_fullpwr);
3001 					mutex_enter(&hidp->hid_mutex);
3002 
3003 					return (USB_FAILURE);
3004 				}
3005 			} else {
3006 				/*
3007 				 * Since we can't send an mblk up,
3008 				 * we fail this attempt to go to low power
3009 				 */
3010 				mutex_enter(&hidp->hid_mutex);
3011 
3012 				return (USB_FAILURE);
3013 			}
3014 		}
3015 		mutex_exit(&hidp->hid_mutex);
3016 		/* Issue USB D3 command to the device here */
3017 		rval = usb_set_device_pwrlvl3(hidp->hid_dip);
3018 		ASSERT(rval == USB_SUCCESS);
3019 
3020 		mutex_enter(&hidp->hid_mutex);
3021 		hidp->hid_dev_state = USB_DEV_PWRED_DOWN;
3022 		hidpm->hid_current_power = USB_DEV_OS_PWR_OFF;
3023 
3024 		/* FALLTHRU */
3025 	case USB_DEV_DISCONNECTED:
3026 	case USB_DEV_SUSPENDED:
3027 	case USB_DEV_PWRED_DOWN:
3028 	default:
3029 		break;
3030 	}
3031 
3032 	return (USB_SUCCESS);
3033 }
3034 
3035 
3036 /* ARGSUSED */
3037 static int
3038 hid_pwrlvl1(hid_state_t *hidp)
3039 {
3040 	int		rval;
3041 
3042 	/* Issue USB D2 command to the device here */
3043 	rval = usb_set_device_pwrlvl2(hidp->hid_dip);
3044 	ASSERT(rval == USB_SUCCESS);
3045 
3046 	return (USB_FAILURE);
3047 }
3048 
3049 
3050 /* ARGSUSED */
3051 static int
3052 hid_pwrlvl2(hid_state_t *hidp)
3053 {
3054 	int		rval;
3055 
3056 	rval = usb_set_device_pwrlvl1(hidp->hid_dip);
3057 	ASSERT(rval == USB_SUCCESS);
3058 
3059 	return (USB_FAILURE);
3060 }
3061 
3062 
3063 static int
3064 hid_pwrlvl3(hid_state_t *hidp)
3065 {
3066 	hid_power_t	*hidpm;
3067 	int		rval;
3068 	struct iocblk	*mctlmsg;
3069 	mblk_t		*mp;
3070 	queue_t		*q;
3071 
3072 	hidpm = hidp->hid_pm;
3073 
3074 	switch (hidp->hid_dev_state) {
3075 	case USB_DEV_HID_POWER_CHANGE:
3076 	case USB_DEV_PWRED_DOWN:
3077 		/* Issue USB D0 command to the device here */
3078 		rval = usb_set_device_pwrlvl0(hidp->hid_dip);
3079 		ASSERT(rval == USB_SUCCESS);
3080 
3081 		if (hidp->hid_streams_flags == HID_STREAMS_OPEN) {
3082 			/* restart polling on intr pipe */
3083 			rval = hid_start_intr_polling(hidp);
3084 			if (rval != USB_SUCCESS) {
3085 				USB_DPRINTF_L2(PRINT_MASK_EVENTS,
3086 				    hidp->hid_log_handle,
3087 				    "unable to restart intr polling rval = %d",
3088 				    rval);
3089 
3090 				return (USB_FAILURE);
3091 			}
3092 
3093 			/* Send an MCTL up indicating device in full  power */
3094 			q = hidp->hid_queue_list->hidq_queue;
3095 			mp = hidpm->hid_pm_pwrup;
3096 			hidpm->hid_pm_pwrup = NULL;
3097 			mutex_exit(&hidp->hid_mutex);
3098 			if (canputnext(q)) {
3099 				mp->b_datap->db_type = M_CTL;
3100 				mctlmsg = (struct iocblk *)mp->b_datap->db_base;
3101 				mctlmsg->ioc_cmd = HID_FULL_POWER;
3102 				mctlmsg->ioc_count = 0;
3103 				putnext(q, mp);
3104 			} else {
3105 				freemsg(mp);
3106 			}
3107 			mutex_enter(&hidp->hid_mutex);
3108 		}
3109 		hidp->hid_dev_state = USB_DEV_ONLINE;
3110 		hidpm->hid_current_power = USB_DEV_OS_FULL_PWR;
3111 
3112 		/* FALLTHRU */
3113 	case USB_DEV_DISCONNECTED:
3114 	case USB_DEV_SUSPENDED:
3115 	case USB_DEV_ONLINE:
3116 
3117 		return (USB_SUCCESS);
3118 	default:
3119 		USB_DPRINTF_L2(PRINT_MASK_EVENTS, hidp->hid_log_handle,
3120 		    "hid_pwrlvl3: Improper State");
3121 
3122 		return (USB_FAILURE);
3123 	}
3124 }
3125 
3126 
3127 /*
3128  * hid_polled_input_init :
3129  *	This routine calls down to the lower layers to initialize any state
3130  *	information.  This routine initializes the lower layers for input.
3131  */
3132 static int
3133 hid_polled_input_init(hid_state_t *hidp)
3134 {
3135 	USB_DPRINTF_L4(PRINT_MASK_ALL, hidp->hid_log_handle,
3136 	    "hid_polled_input_init");
3137 
3138 	/*
3139 	 * Call the lower layers to intialize any state information
3140 	 * that they will need to provide the polled characters.
3141 	 */
3142 	if (usb_console_input_init(hidp->hid_dip, hidp->hid_interrupt_pipe,
3143 	    &hidp->hid_polled_raw_buf,
3144 	    &hidp->hid_polled_console_info) != USB_SUCCESS) {
3145 		/*
3146 		 * If for some reason the lower layers cannot initialized, then
3147 		 * bail.
3148 		 */
3149 		(void) hid_polled_input_fini(hidp);
3150 
3151 		return (USB_FAILURE);
3152 	}
3153 
3154 	return (USB_SUCCESS);
3155 }
3156 
3157 
3158 /*
3159  * hid_polled_input_fini:
3160  *	This routine is called when we are done using this device as an input
3161  *	device.
3162  */
3163 static int
3164 hid_polled_input_fini(hid_state_t *hidp)
3165 {
3166 	USB_DPRINTF_L4(PRINT_MASK_ALL, hidp->hid_log_handle,
3167 	    "hid_polled_input_fini");
3168 
3169 	/*
3170 	 * Call the lower layers to free any state information
3171 	 * only if polled input has been initialised.
3172 	 */
3173 	if ((hidp->hid_polled_console_info) &&
3174 	    (usb_console_input_fini(hidp->hid_polled_console_info) !=
3175 	    USB_SUCCESS)) {
3176 
3177 		return (USB_FAILURE);
3178 	}
3179 	hidp->hid_polled_console_info = NULL;
3180 
3181 	return (USB_SUCCESS);
3182 }
3183 
3184 
3185 /*
3186  * hid_polled_input_enter:
3187  *	This is the routine that is called in polled mode to save the USB
3188  *	state information before using the USB keyboard as an input device.
3189  *	This routine, and all of the routines that it calls, are responsible
3190  *	for saving any state information so that it can be restored when
3191  *	polling mode is over.
3192  */
3193 static int
3194 /* ARGSUSED */
3195 hid_polled_input_enter(hid_polled_handle_t hid_polled_inputp)
3196 {
3197 	hid_state_t *hidp = (hid_state_t *)hid_polled_inputp;
3198 
3199 	/*
3200 	 * Call the lower layers to tell them to save any state information.
3201 	 */
3202 	(void) usb_console_input_enter(hidp->hid_polled_console_info);
3203 
3204 	return (USB_SUCCESS);
3205 }
3206 
3207 
3208 /*
3209  * hid_polled_read :
3210  *	This is the routine that is called in polled mode when it wants to read
3211  *	a character.  We will call to the lower layers to see if there is any
3212  *	input data available.  If there is USB scancodes available, we will
3213  *	give them back.
3214  */
3215 static int
3216 hid_polled_read(hid_polled_handle_t hid_polled_input, uchar_t **buffer)
3217 {
3218 	hid_state_t *hidp = (hid_state_t *)hid_polled_input;
3219 	uint_t			num_bytes;
3220 
3221 	/*
3222 	 * Call the lower layers to get the character from the controller.
3223 	 * The lower layers will return the number of characters that
3224 	 * were put in the raw buffer.	The address of the raw buffer
3225 	 * was passed down to the lower layers during hid_polled_init.
3226 	 */
3227 	if (usb_console_read(hidp->hid_polled_console_info,
3228 	    &num_bytes) != USB_SUCCESS) {
3229 
3230 		return (0);
3231 	}
3232 
3233 	_NOTE(NO_COMPETING_THREADS_NOW);
3234 
3235 	*buffer = hidp->hid_polled_raw_buf;
3236 
3237 	_NOTE(COMPETING_THREADS_NOW);
3238 
3239 	/*
3240 	 * Return the number of characters that were copied into the
3241 	 * polled buffer.
3242 	 */
3243 	return (num_bytes);
3244 }
3245 
3246 
3247 /*
3248  * hid_polled_input_exit :
3249  *	This is the routine that is called in polled mode  when it is giving up
3250  *	control of the USB keyboard.  This routine, and the lower layer routines
3251  *	that it calls, are responsible for restoring the controller state to the
3252  *	state it was in before polled mode.
3253  */
3254 static int
3255 hid_polled_input_exit(hid_polled_handle_t hid_polled_inputp)
3256 {
3257 	hid_state_t *hidp = (hid_state_t *)hid_polled_inputp;
3258 
3259 	/*
3260 	 * Call the lower layers to restore any state information.
3261 	 */
3262 	(void) usb_console_input_exit(hidp->hid_polled_console_info);
3263 
3264 	return (0);
3265 }
3266