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