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