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