xref: /illumos-gate/usr/src/uts/common/io/usb/clients/usbkbm/usbkbm.c (revision 92a0208178405fef708b0283ffcaa02fbc3468ff)
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  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 
27 /*
28  * USB keyboard input streams module - processes USB keypacket
29  * received from HID driver below to either ASCII or event
30  * format for windowing system.
31  */
32 #include <sys/usb/usba/usbai_version.h>
33 
34 #define	KEYMAP_SIZE_VARIABLE
35 #include <sys/usb/usba.h>
36 #include <sys/usb/clients/hid/hid.h>
37 #include <sys/usb/clients/hid/hid_polled.h>
38 #include <sys/usb/clients/hidparser/hidparser.h>
39 #include <sys/stropts.h>
40 #include <sys/stream.h>
41 #include <sys/strsun.h>
42 #include <sys/kbio.h>
43 #include <sys/vuid_event.h>
44 #include <sys/kbd.h>
45 #include <sys/consdev.h>
46 #include <sys/kbtrans.h>
47 #include <sys/usb/clients/usbkbm/usbkbm.h>
48 #include <sys/beep.h>
49 #include <sys/policy.h>
50 #include <sys/inttypes.h>
51 
52 /* debugging information */
53 uint_t	usbkbm_errmask = (uint_t)PRINT_MASK_ALL;
54 uint_t	usbkbm_errlevel = USB_LOG_L2;
55 static usb_log_handle_t usbkbm_log_handle;
56 
57 typedef void (*process_key_callback_t)(usbkbm_state_t *, int, enum keystate);
58 
59 /*
60  * Internal Function Prototypes
61  */
62 static void usbkbm_streams_setled(struct kbtrans_hardware *, int);
63 static void usbkbm_polled_setled(struct kbtrans_hardware *, int);
64 static boolean_t usbkbm_polled_keycheck(struct kbtrans_hardware *,
65 			int *, enum keystate *);
66 static void usbkbm_poll_callback(usbkbm_state_t *, int, enum keystate);
67 static void usbkbm_streams_callback(usbkbm_state_t *, int, enum keystate);
68 static void usbkbm_unpack_usb_packet(usbkbm_state_t *, process_key_callback_t,
69 			uchar_t *, int);
70 static boolean_t usbkbm_is_modkey(uchar_t);
71 static void usbkbm_reioctl(void	*);
72 static int usbkbm_polled_getchar(cons_polledio_arg_t);
73 static boolean_t usbkbm_polled_ischar(cons_polledio_arg_t);
74 static void usbkbm_polled_enter(cons_polledio_arg_t);
75 static void usbkbm_polled_exit(cons_polledio_arg_t);
76 static void usbkbm_mctl_receive(queue_t *, mblk_t *);
77 static enum kbtrans_message_response usbkbm_ioctl(queue_t *, mblk_t *);
78 static int usbkbm_kioccmd(usbkbm_state_t *, mblk_t *, char, size_t *);
79 static void	usbkbm_usb2pc_xlate(usbkbm_state_t *, int, enum keystate);
80 static void	usbkbm_wrap_kbtrans(usbkbm_state_t *, int, enum keystate);
81 static int 	usbkbm_set_protocol(usbkbm_state_t *, uint16_t);
82 static int 	usbkbm_get_vid_pid(usbkbm_state_t *);
83 
84 /* stream qinit functions defined here */
85 static int	usbkbm_open(queue_t *, dev_t *, int, int, cred_t *);
86 static int	usbkbm_close(queue_t *, int, cred_t *);
87 static void	usbkbm_wput(queue_t *, mblk_t *);
88 static void	usbkbm_rput(queue_t *, mblk_t *);
89 static ushort_t	usbkbm_get_state(usbkbm_state_t *);
90 static void	usbkbm_get_scancode(usbkbm_state_t *, int *, enum keystate *);
91 
92 static struct keyboard *usbkbm_keyindex;
93 
94 /* External Functions */
95 extern void space_free(char *);
96 extern uintptr_t space_fetch(char *);
97 extern int space_store(char *, uintptr_t);
98 extern struct keyboard *kbtrans_usbkb_maptab_init(void);
99 extern void kbtrans_usbkb_maptab_fini(struct keyboard **);
100 extern keymap_entry_t kbtrans_keycode_usb2pc(int);
101 
102 /*
103  * Structure to setup callbacks
104  */
105 struct kbtrans_callbacks kbd_usb_callbacks = {
106 	usbkbm_streams_setled,
107 	usbkbm_polled_setled,
108 	usbkbm_polled_keycheck,
109 };
110 
111 /*
112  * Global Variables
113  */
114 
115 /* This variable saves the LED state across hotplugging. */
116 static uchar_t  usbkbm_led_state = 0;
117 
118 /* This variable saves the layout state */
119 static uint16_t usbkbm_layout = 0;
120 
121 /*
122  * Function pointer array for mapping of scancodes.
123  */
124 void (*usbkbm_xlate[2])(usbkbm_state_t *, int, enum keystate) = {
125 	usbkbm_wrap_kbtrans,
126 	usbkbm_usb2pc_xlate
127 };
128 
129 static struct streamtab usbkbm_info;
130 static struct fmodsw fsw = {
131 	"usbkbm",
132 	&usbkbm_info,
133 	D_MP | D_MTPERMOD
134 };
135 
136 
137 /*
138  * Module linkage information for the kernel.
139  */
140 static struct modlstrmod modlstrmod = {
141 	&mod_strmodops,
142 	"USB keyboard streams 1.44",
143 	&fsw
144 };
145 
146 static struct modlinkage modlinkage = {
147 	MODREV_1,
148 	(void *)&modlstrmod,
149 	NULL
150 };
151 
152 
153 int
154 _init(void)
155 {
156 	int	rval = mod_install(&modlinkage);
157 	usbkbm_save_state_t *sp;
158 
159 	if (rval != 0) {
160 
161 		return (rval);
162 	}
163 
164 	usbkbm_keyindex = kbtrans_usbkb_maptab_init();
165 
166 	usbkbm_log_handle = usb_alloc_log_hdl(NULL, "usbkbm",
167 	    &usbkbm_errlevel, &usbkbm_errmask, NULL, 0);
168 
169 	sp = (usbkbm_save_state_t *)space_fetch("SUNW,usbkbm_state");
170 
171 	if (sp == NULL) {
172 
173 		return (0);
174 	}
175 
176 	/* Restore LED information */
177 	usbkbm_led_state = sp->usbkbm_save_led;
178 
179 	/* Restore the Layout */
180 	usbkbm_layout = sp->usbkbm_layout;
181 
182 	/* Restore abort information */
183 	usbkbm_keyindex->k_abort1 =
184 	    sp->usbkbm_save_keyindex.k_abort1;
185 
186 	usbkbm_keyindex->k_abort2 =
187 	    sp->usbkbm_save_keyindex.k_abort2;
188 
189 	usbkbm_keyindex->k_newabort1 =
190 	    sp->usbkbm_save_keyindex.k_newabort1;
191 
192 	usbkbm_keyindex->k_newabort2 =
193 	    sp->usbkbm_save_keyindex.k_newabort2;
194 
195 	/* Restore keytables */
196 	bcopy(sp->usbkbm_save_keyindex.k_normal,
197 	    usbkbm_keyindex->k_normal, USB_KEYTABLE_SIZE);
198 
199 	bcopy(sp->usbkbm_save_keyindex.k_shifted,
200 	    usbkbm_keyindex->k_shifted, USB_KEYTABLE_SIZE);
201 
202 	bcopy(sp->usbkbm_save_keyindex.k_caps,
203 	    usbkbm_keyindex->k_caps, USB_KEYTABLE_SIZE);
204 
205 	bcopy(sp->usbkbm_save_keyindex.k_altgraph,
206 	    usbkbm_keyindex->k_altgraph, USB_KEYTABLE_SIZE);
207 
208 	bcopy(sp->usbkbm_save_keyindex.k_numlock,
209 	    usbkbm_keyindex->k_numlock, USB_KEYTABLE_SIZE);
210 
211 	bcopy(sp->usbkbm_save_keyindex.k_control,
212 	    usbkbm_keyindex->k_control, USB_KEYTABLE_SIZE);
213 
214 	bcopy(sp->usbkbm_save_keyindex.k_up,
215 	    usbkbm_keyindex->k_up, USB_KEYTABLE_SIZE);
216 
217 	kmem_free(sp->usbkbm_save_keyindex.k_normal,
218 	    USB_KEYTABLE_SIZE);
219 	kmem_free(sp->usbkbm_save_keyindex.k_shifted,
220 	    USB_KEYTABLE_SIZE);
221 	kmem_free(sp->usbkbm_save_keyindex.k_caps,
222 	    USB_KEYTABLE_SIZE);
223 	kmem_free(sp->usbkbm_save_keyindex.k_altgraph,
224 	    USB_KEYTABLE_SIZE);
225 	kmem_free(sp->usbkbm_save_keyindex.k_numlock,
226 	    USB_KEYTABLE_SIZE);
227 	kmem_free(sp->usbkbm_save_keyindex.k_control,
228 	    USB_KEYTABLE_SIZE);
229 	kmem_free(sp->usbkbm_save_keyindex.k_up,
230 	    USB_KEYTABLE_SIZE);
231 
232 	kmem_free(sp, sizeof (usbkbm_save_state_t));
233 	space_free("SUNW,usbkbm_state");
234 
235 	return (0);
236 }
237 
238 int
239 _fini(void)
240 {
241 	usbkbm_save_state_t *sp;
242 	int sval;
243 	int rval;
244 
245 	sp = kmem_alloc(sizeof (usbkbm_save_state_t), KM_SLEEP);
246 	sval = space_store("SUNW,usbkbm_state", (uintptr_t)sp);
247 
248 	/*
249 	 * If it's not possible to store the state, return
250 	 * EBUSY.
251 	 */
252 	if (sval != 0) {
253 		kmem_free(sp, sizeof (usbkbm_save_state_t));
254 
255 		return (EBUSY);
256 	}
257 
258 	rval = mod_remove(&modlinkage);
259 
260 	if (rval != 0) {
261 		kmem_free(sp, sizeof (usbkbm_save_state_t));
262 		space_free("SUNW,usbkbm_state");
263 
264 		return (rval);
265 	}
266 
267 	usb_free_log_hdl(usbkbm_log_handle);
268 
269 	/* Save the LED state */
270 	sp->usbkbm_save_led = usbkbm_led_state;
271 
272 	/* Save the layout */
273 	sp->usbkbm_layout = (uchar_t)usbkbm_layout;
274 
275 	/*
276 	 * Save entries of the keyboard structure that
277 	 * have changed.
278 	 */
279 	sp->usbkbm_save_keyindex.k_abort1 = usbkbm_keyindex->k_abort1;
280 	sp->usbkbm_save_keyindex.k_abort2 = usbkbm_keyindex->k_abort2;
281 
282 	sp->usbkbm_save_keyindex.k_newabort1 = usbkbm_keyindex->k_newabort1;
283 	sp->usbkbm_save_keyindex.k_newabort2 = usbkbm_keyindex->k_newabort2;
284 
285 	/* Allocate space for keytables to be stored */
286 	sp->usbkbm_save_keyindex.k_normal =
287 	    kmem_alloc(USB_KEYTABLE_SIZE, KM_SLEEP);
288 	sp->usbkbm_save_keyindex.k_shifted =
289 	    kmem_alloc(USB_KEYTABLE_SIZE, KM_SLEEP);
290 	sp->usbkbm_save_keyindex.k_caps =
291 	    kmem_alloc(USB_KEYTABLE_SIZE, KM_SLEEP);
292 	sp->usbkbm_save_keyindex.k_altgraph =
293 	    kmem_alloc(USB_KEYTABLE_SIZE, KM_SLEEP);
294 	sp->usbkbm_save_keyindex.k_numlock =
295 	    kmem_alloc(USB_KEYTABLE_SIZE, KM_SLEEP);
296 	sp->usbkbm_save_keyindex.k_control =
297 	    kmem_alloc(USB_KEYTABLE_SIZE, KM_SLEEP);
298 	sp->usbkbm_save_keyindex.k_up =
299 	    kmem_alloc(USB_KEYTABLE_SIZE, KM_SLEEP);
300 
301 	/* Copy over the keytables */
302 	bcopy(usbkbm_keyindex->k_normal,
303 	    sp->usbkbm_save_keyindex.k_normal, USB_KEYTABLE_SIZE);
304 
305 	bcopy(usbkbm_keyindex->k_shifted,
306 	    sp->usbkbm_save_keyindex.k_shifted, USB_KEYTABLE_SIZE);
307 
308 	bcopy(usbkbm_keyindex->k_caps,
309 	    sp->usbkbm_save_keyindex.k_caps, USB_KEYTABLE_SIZE);
310 
311 	bcopy(usbkbm_keyindex->k_altgraph,
312 	    sp->usbkbm_save_keyindex.k_altgraph, USB_KEYTABLE_SIZE);
313 
314 	bcopy(usbkbm_keyindex->k_numlock,
315 	    sp->usbkbm_save_keyindex.k_numlock, USB_KEYTABLE_SIZE);
316 
317 	bcopy(usbkbm_keyindex->k_control,
318 	    sp->usbkbm_save_keyindex.k_control, USB_KEYTABLE_SIZE);
319 
320 	bcopy(usbkbm_keyindex->k_up,
321 	    sp->usbkbm_save_keyindex.k_up, USB_KEYTABLE_SIZE);
322 
323 	kbtrans_usbkb_maptab_fini(&usbkbm_keyindex);
324 
325 	return (0);
326 }
327 
328 int
329 _info(struct modinfo *modinfop)
330 {
331 	return (mod_info(&modlinkage, modinfop));
332 }
333 
334 /*
335  * Module qinit functions
336  */
337 
338 static struct module_info usbkbm_minfo = {
339 	0,		/* module id number */
340 	"usbkbm",	/* module name */
341 	0,		/* min packet size accepted */
342 	INFPSZ,		/* max packet size accepted */
343 	2048,		/* hi-water mark */
344 	128		/* lo-water mark */
345 	};
346 
347 /* read side for key data and ioctl replies */
348 static struct qinit usbkbm_rinit = {
349 	(int (*)())usbkbm_rput,
350 	(int (*)())NULL,		/* service not used */
351 	usbkbm_open,
352 	usbkbm_close,
353 	(int (*)())NULL,
354 	&usbkbm_minfo
355 	};
356 
357 /* write side for ioctls */
358 static struct qinit usbkbm_winit = {
359 	(int (*)())usbkbm_wput,
360 	(int (*)())NULL,
361 	usbkbm_open,
362 	usbkbm_close,
363 	(int (*)())NULL,
364 	&usbkbm_minfo
365 	};
366 
367 static struct streamtab usbkbm_info = {
368 	&usbkbm_rinit,
369 	&usbkbm_winit,
370 	NULL,		/* for muxes */
371 	NULL,		/* for muxes */
372 };
373 
374 /*
375  * usbkbm_open :
376  *	Open a keyboard
377  */
378 /* ARGSUSED */
379 static int
380 usbkbm_open(queue_t *q, dev_t *devp, int oflag, int sflag, cred_t *crp)
381 {
382 	usbkbm_state_t	*usbkbmd;
383 	struct iocblk	mctlmsg;
384 	mblk_t		*mctl_ptr;
385 	int		error, ret;
386 
387 	if (q->q_ptr) {
388 		USB_DPRINTF_L3(PRINT_MASK_OPEN, usbkbm_log_handle,
389 		    "usbkbm_open already opened");
390 
391 		return (0); /* already opened */
392 	}
393 
394 	/*
395 	 * Only allow open requests to succeed for privileged users.  This
396 	 * necessary to prevent users from pushing the "usbkbm" module again
397 	 * on the stream associated with /dev/kbd.
398 	 */
399 	if (secpolicy_console(crp) != 0)
400 		return (EPERM);
401 
402 	switch (sflag) {
403 
404 	case MODOPEN:
405 		break;
406 
407 	case CLONEOPEN:
408 		USB_DPRINTF_L3(PRINT_MASK_OPEN, usbkbm_log_handle,
409 		    "usbkbm_open: Clone open not supported");
410 
411 		/* FALLTHRU */
412 	default:
413 
414 		return (EINVAL);
415 	}
416 
417 	/* allocate usb keyboard state structure */
418 
419 	usbkbmd = kmem_zalloc(sizeof (usbkbm_state_t), KM_SLEEP);
420 
421 	USB_DPRINTF_L3(PRINT_MASK_OPEN, usbkbm_log_handle,
422 	    "usbkbm_state= %p", (void *)usbkbmd);
423 
424 	/*
425 	 * Set up private data.
426 	 */
427 	usbkbmd->usbkbm_readq = q;
428 	usbkbmd->usbkbm_writeq = WR(q);
429 
430 	usbkbmd->usbkbm_vkbd_type = KB_USB;
431 	/*
432 	 * Set up queue pointers, so that the "put" procedure will accept
433 	 * the reply to the "ioctl" message we send down.
434 	 */
435 	q->q_ptr = (caddr_t)usbkbmd;
436 	WR(q)->q_ptr = (caddr_t)usbkbmd;
437 
438 	error = kbtrans_streams_init(q, sflag, crp,
439 	    (struct kbtrans_hardware *)usbkbmd, &kbd_usb_callbacks,
440 	    &usbkbmd->usbkbm_kbtrans, usbkbm_led_state, 0);
441 
442 	if (error != 0) {
443 		USB_DPRINTF_L3(PRINT_MASK_OPEN, usbkbm_log_handle,
444 		    "kbdopen:  kbtrans_streams_init failed\n");
445 		kmem_free(usbkbmd, sizeof (*usbkbmd));
446 
447 		return (error);
448 	}
449 
450 	/*
451 	 * Set the polled information in the state structure.
452 	 * This information is set once, and doesn't change
453 	 */
454 	usbkbmd->usbkbm_polled_info.cons_polledio_version =
455 	    CONSPOLLEDIO_V1;
456 
457 	usbkbmd->usbkbm_polled_info.cons_polledio_argument =
458 	    (cons_polledio_arg_t)usbkbmd;
459 
460 	usbkbmd->usbkbm_polled_info.cons_polledio_putchar = NULL;
461 
462 	usbkbmd->usbkbm_polled_info.cons_polledio_getchar =
463 	    usbkbm_polled_getchar;
464 
465 	usbkbmd->usbkbm_polled_info.cons_polledio_ischar =
466 	    usbkbm_polled_ischar;
467 
468 	usbkbmd->usbkbm_polled_info.cons_polledio_enter =
469 	    usbkbm_polled_enter;
470 
471 	usbkbmd->usbkbm_polled_info.cons_polledio_exit =
472 	    usbkbm_polled_exit;
473 
474 	usbkbmd->usbkbm_polled_info.cons_polledio_setled =
475 	    (void (*)(cons_polledio_arg_t, int))usbkbm_polled_setled;
476 
477 	usbkbmd->usbkbm_polled_info.cons_polledio_keycheck =
478 	    (boolean_t (*)(cons_polledio_arg_t, int *,
479 	    enum keystate *))usbkbm_polled_keycheck;
480 	/*
481 	 * The head and the tail pointing at the same byte means empty or
482 	 * full. usbkbm_polled_buffer_num_characters is used to
483 	 * tell the difference.
484 	 */
485 	usbkbmd->usbkbm_polled_buffer_head =
486 	    usbkbmd->usbkbm_polled_scancode_buffer;
487 	usbkbmd->usbkbm_polled_buffer_tail =
488 	    usbkbmd->usbkbm_polled_scancode_buffer;
489 	usbkbmd->usbkbm_polled_buffer_num_characters = 0;
490 
491 	qprocson(q);
492 
493 	/*
494 	 * The hid module already configured this keyboard for report mode,
495 	 * but usbkbm only knows how to deal with boot-protocol mode,
496 	 * so switch into boot-protocol mode now.
497 	 */
498 	if (ret = usbkbm_set_protocol(usbkbmd, SET_BOOT_PROTOCOL)) {
499 
500 		return (ret);
501 	}
502 
503 	/*
504 	 * USB keyboards are expected to send well-defined 8-byte data
505 	 * packets in boot-protocol mode (the format of which is documented
506 	 * in the HID specification).
507 	 *
508 	 * Note: We do not look at the interface's HID report descriptors to
509 	 * derive the report size, because the HID report descriptor describes
510 	 * the format of each report in report mode.  This format might be
511 	 * different from the format used in boot-protocol mode.  The internal
512 	 * USB keyboard in a recent version of the Apple MacBook Pro is one
513 	 * example of a USB keyboard that uses different formats for
514 	 * boot-protocol-mode reports and report-mode reports.
515 	 */
516 	usbkbmd->usbkbm_packet_size = USB_KBD_BOOT_PROTOCOL_PACKET_SIZE;
517 
518 	/* request hid report descriptor from HID */
519 	mctlmsg.ioc_cmd = HID_GET_PARSER_HANDLE;
520 	mctlmsg.ioc_count = 0;
521 	mctl_ptr = usba_mk_mctl(mctlmsg, NULL, 0);
522 	if (mctl_ptr == NULL) {
523 		/* failure to allocate M_CTL message */
524 		(void) kbtrans_streams_fini(usbkbmd->usbkbm_kbtrans);
525 		qprocsoff(q);
526 		kmem_free(usbkbmd, sizeof (*usbkbmd));
527 
528 		return (ENOMEM);
529 	}
530 
531 	/* send message to hid */
532 	putnext(usbkbmd->usbkbm_writeq, mctl_ptr);
533 
534 	/*
535 	 * Now that M_CTL has been sent, wait for report descriptor.  Cleanup
536 	 * if user signals in the mean time (as when this gets opened in an
537 	 * inappropriate context and the user types a ^C).
538 	 */
539 	usbkbmd->usbkbm_flags |= USBKBM_QWAIT;
540 	while (usbkbmd->usbkbm_flags & USBKBM_QWAIT) {
541 
542 		if (qwait_sig(q) == 0) {
543 			usbkbmd->usbkbm_flags = 0;
544 			(void) kbtrans_streams_fini(usbkbmd->usbkbm_kbtrans);
545 			qprocsoff(q);
546 			kmem_free(usbkbmd, sizeof (*usbkbmd));
547 
548 			return (EINTR);
549 		}
550 	}
551 
552 	if (usbkbmd->usbkbm_report_descr != NULL) {
553 		if (hidparser_get_country_code(usbkbmd->usbkbm_report_descr,
554 		    (uint16_t *)&usbkbmd->usbkbm_layout) ==
555 		    HIDPARSER_FAILURE) {
556 
557 			USB_DPRINTF_L3(PRINT_MASK_OPEN,
558 			    usbkbm_log_handle, "get_country_code failed"
559 			    "setting default layout(0)");
560 
561 			usbkbmd->usbkbm_layout = usbkbm_layout;
562 		}
563 	} else {
564 		USB_DPRINTF_L3(PRINT_MASK_OPEN, usbkbm_log_handle,
565 		    "usbkbm: Invalid HID Descriptor Tree."
566 		    "setting default layout(0)");
567 
568 		usbkbmd->usbkbm_layout = usbkbm_layout;
569 	}
570 
571 	/*
572 	 * Although Sun Japanese type6 and type7 keyboards have the same
573 	 * layout number(15), they should be recognized for loading the
574 	 * different keytables on upper apps (e.g. X). The new layout
575 	 * number (271) is defined for the Sun Japanese type6 keyboards.
576 	 * The layout number (15) specified in HID spec is used for other
577 	 * Japanese keyboards. It is a workaround for the old Sun Japanese
578 	 * type6 keyboards defect.
579 	 */
580 	if (usbkbmd->usbkbm_layout == SUN_JAPANESE_TYPE7) {
581 
582 		if ((ret = usbkbm_get_vid_pid(usbkbmd)) != 0) {
583 
584 			return (ret);
585 		}
586 
587 		if ((usbkbmd->usbkbm_vid_pid.VendorId ==
588 		    HID_SUN_JAPANESE_TYPE6_KBD_VID) &&
589 		    (usbkbmd->usbkbm_vid_pid.ProductId ==
590 		    HID_SUN_JAPANESE_TYPE6_KBD_PID)) {
591 			usbkbmd->usbkbm_layout = SUN_JAPANESE_TYPE6;
592 		}
593 	}
594 
595 	kbtrans_streams_set_keyboard(usbkbmd->usbkbm_kbtrans, KB_USB,
596 	    usbkbm_keyindex);
597 
598 	usbkbmd->usbkbm_flags = USBKBM_OPEN;
599 
600 	kbtrans_streams_enable(usbkbmd->usbkbm_kbtrans);
601 
602 	USB_DPRINTF_L3(PRINT_MASK_OPEN, usbkbm_log_handle,
603 	    "usbkbm_open exiting");
604 	return (0);
605 }
606 
607 
608 /*
609  * usbkbm_close :
610  *	Close a keyboard.
611  */
612 /* ARGSUSED1 */
613 static int
614 usbkbm_close(register queue_t *q, int flag, cred_t *crp)
615 {
616 	usbkbm_state_t *usbkbmd = (usbkbm_state_t *)q->q_ptr;
617 
618 	/* If a beep is in progress, stop that */
619 	(void) beeper_off();
620 
621 	(void) kbtrans_streams_fini(usbkbmd->usbkbm_kbtrans);
622 
623 	qprocsoff(q);
624 	/*
625 	 * Since we're about to destroy our private data, turn off
626 	 * our open flag first, so we don't accept any more input
627 	 * and try to use that data.
628 	 */
629 	usbkbmd->usbkbm_flags = 0;
630 
631 	kmem_free(usbkbmd, sizeof (usbkbm_state_t));
632 
633 	USB_DPRINTF_L3(PRINT_MASK_CLOSE, usbkbm_log_handle,
634 	    "usbkbm_close exiting");
635 
636 	return (0);
637 }
638 
639 
640 /*
641  * usbkbm_wput :
642  *	usb keyboard module output queue put procedure: handles M_IOCTL
643  *	messages.
644  */
645 static void
646 usbkbm_wput(register queue_t *q, register mblk_t *mp)
647 {
648 	usbkbm_state_t			*usbkbmd;
649 	enum kbtrans_message_response	ret;
650 
651 	USB_DPRINTF_L3(PRINT_MASK_ALL, usbkbm_log_handle,
652 	    "usbkbm_wput entering");
653 
654 	usbkbmd = (usbkbm_state_t *)q->q_ptr;
655 
656 	/* First, see if kbtrans will handle the message */
657 	ret = kbtrans_streams_message(usbkbmd->usbkbm_kbtrans, mp);
658 
659 	if (ret == KBTRANS_MESSAGE_HANDLED) {
660 
661 		USB_DPRINTF_L3(PRINT_MASK_ALL, usbkbm_log_handle,
662 		    "usbkbm_wput exiting:2");
663 
664 		return;
665 	}
666 
667 	/* kbtrans didn't handle the message.  Try to handle it here */
668 
669 	switch (mp->b_datap->db_type) {
670 
671 	case M_FLUSH:
672 		if (*mp->b_rptr & FLUSHW) {
673 			flushq(q, FLUSHDATA);
674 		}
675 
676 		if (*mp->b_rptr & FLUSHR) {
677 			flushq(RD(q), FLUSHDATA);
678 		}
679 
680 		break;
681 
682 	case M_IOCTL:
683 		ret = usbkbm_ioctl(q, mp);
684 
685 		if (ret == KBTRANS_MESSAGE_HANDLED) {
686 
687 			USB_DPRINTF_L3(PRINT_MASK_ALL, usbkbm_log_handle,
688 			    "usbkbm_wput exiting:1");
689 
690 			return;
691 		}
692 	default:
693 		break;
694 	}
695 
696 	/*
697 	 * The message has not been handled
698 	 * by kbtrans or this module.  Pass it down the stream
699 	 */
700 	putnext(q, mp);
701 
702 	USB_DPRINTF_L3(PRINT_MASK_ALL, usbkbm_log_handle,
703 	    "usbkbm_wput exiting:3");
704 }
705 
706 /*
707  * usbkbm_ioctl :
708  *	Handles the ioctls sent from upper module. Returns
709  *	ACK/NACK back.
710  */
711 static enum kbtrans_message_response
712 usbkbm_ioctl(register queue_t *q, register mblk_t *mp)
713 {
714 	usbkbm_state_t		*usbkbmd;
715 	struct iocblk		mctlmsg;
716 	struct iocblk		*iocp;
717 	mblk_t			*datap, *mctl_ptr;
718 	size_t			ioctlrespsize;
719 	int			err;
720 	int			tmp;
721 	int			cycles;
722 	int			frequency;
723 	int			msecs;
724 	char			command;
725 
726 	err = 0;
727 
728 	usbkbmd = (usbkbm_state_t *)q->q_ptr;
729 	iocp = (struct iocblk *)mp->b_rptr;
730 
731 	switch (iocp->ioc_cmd) {
732 	case CONSSETKBDTYPE:
733 		err = miocpullup(mp, sizeof (int));
734 		if (err != 0) {
735 			break;
736 		}
737 		tmp = *(int *)mp->b_cont->b_rptr;
738 		if (tmp != KB_PC && tmp != KB_USB) {
739 			err = EINVAL;
740 			break;
741 		}
742 		usbkbmd->usbkbm_vkbd_type = tmp;
743 		break;
744 	case KIOCLAYOUT:
745 
746 		datap = allocb(sizeof (int), BPRI_HI);
747 		if (datap == NULL) {
748 			ioctlrespsize = sizeof (int);
749 
750 			goto allocfailure;
751 		}
752 
753 		*(int *)datap->b_wptr = usbkbmd->usbkbm_layout;
754 		datap->b_wptr += sizeof (int);
755 
756 		freemsg(mp->b_cont);
757 
758 		mp->b_cont = datap;
759 		iocp->ioc_count = sizeof (int);
760 		break;
761 
762 	case KIOCSLAYOUT:
763 		/*
764 		 * Supply a layout if not specified by the hardware, or
765 		 * override any that was specified.
766 		 */
767 		if (iocp->ioc_count != TRANSPARENT) {
768 			err = EINVAL;
769 			break;
770 		}
771 
772 		usbkbmd->usbkbm_layout = *(intptr_t *)mp->b_cont->b_rptr;
773 
774 		/*
775 		 * Save the layout in usbkbm_layout so as to handle the
776 		 * the case when the user has re-plugged in the non-self
777 		 * identifying non US keyboard. In this the layout is saved
778 		 * in global variable, so the user does not have to run
779 		 * kdmconfig again after the X server reset
780 		 */
781 
782 		usbkbm_layout = usbkbmd->usbkbm_layout;
783 		break;
784 
785 	case KIOCCMD:
786 		/*
787 		 * Check if we have at least the subcommand field; any
788 		 * other argument validation has to occur inside
789 		 * usbkbm_kioccmd().
790 		 */
791 		err = miocpullup(mp, sizeof (int));
792 		if (err != 0)
793 			break;
794 
795 		/* Subcommand */
796 		command = (char)(*(int *)mp->b_cont->b_rptr);
797 
798 		/*
799 		 * Check if this ioctl is followed by a previous
800 		 * KBD_CMD_SETLED command, in which case we take
801 		 * the command byte as the data for setting the LED
802 		 */
803 		if (usbkbmd->usbkbm_setled_second_byte) {
804 			usbkbm_streams_setled((struct kbtrans_hardware *)
805 			    usbkbmd, command);
806 			usbkbmd->usbkbm_setled_second_byte = 0;
807 			break;
808 		}
809 
810 		/*
811 		 * In  case of allocb failure, this will
812 		 * return the size of the allocation which
813 		 * failed so that it can be allocated later
814 		 * through bufcall.
815 		 */
816 		ioctlrespsize = 0;
817 
818 		err = usbkbm_kioccmd(usbkbmd, mp, command, &ioctlrespsize);
819 
820 		if (ioctlrespsize != 0) {
821 
822 			goto allocfailure;
823 		}
824 
825 		break;
826 
827 	case CONSOPENPOLLEDIO:
828 		USB_DPRINTF_L3(PRINT_MASK_ALL, usbkbm_log_handle,
829 		    "usbkbm_ioctl CONSOPENPOLLEDIO");
830 
831 		err = miocpullup(mp, sizeof (struct cons_polledio *));
832 		if (err != 0) {
833 			USB_DPRINTF_L2(PRINT_MASK_ALL, usbkbm_log_handle,
834 			    "usbkbm_ioctl: malformed request");
835 			break;
836 		}
837 
838 		usbkbmd->usbkbm_pending_link = mp;
839 
840 		/*
841 		 * Get the polled input structure from hid
842 		 */
843 		mctlmsg.ioc_cmd = HID_OPEN_POLLED_INPUT;
844 		mctlmsg.ioc_count = 0;
845 		mctl_ptr = usba_mk_mctl(mctlmsg, NULL, 0);
846 		if (mctl_ptr == NULL) {
847 			ioctlrespsize = sizeof (mctlmsg);
848 
849 			goto allocfailure;
850 		}
851 
852 		putnext(usbkbmd->usbkbm_writeq, mctl_ptr);
853 
854 		/*
855 		 * Do not ack or nack the message, we will wait for the
856 		 * result of HID_OPEN_POLLED_INPUT
857 		 */
858 
859 		return (KBTRANS_MESSAGE_HANDLED);
860 
861 	case CONSCLOSEPOLLEDIO:
862 		USB_DPRINTF_L3(PRINT_MASK_ALL, usbkbm_log_handle,
863 		    "usbkbm_ioctl CONSCLOSEPOLLEDIO mp = 0x%p", (void *)mp);
864 
865 		usbkbmd->usbkbm_pending_link = mp;
866 
867 		/*
868 		 * Get the polled input structure from hid
869 		 */
870 		mctlmsg.ioc_cmd = HID_CLOSE_POLLED_INPUT;
871 		mctlmsg.ioc_count = 0;
872 		mctl_ptr = usba_mk_mctl(mctlmsg, NULL, 0);
873 		if (mctl_ptr == NULL) {
874 			ioctlrespsize = sizeof (mctlmsg);
875 
876 			goto allocfailure;
877 		}
878 
879 		putnext(usbkbmd->usbkbm_writeq, mctl_ptr);
880 
881 		/*
882 		 * Do not ack or nack the message, we will wait for the
883 		 * result of HID_CLOSE_POLLED_INPUT
884 		 */
885 
886 		return (KBTRANS_MESSAGE_HANDLED);
887 
888 	case CONSSETABORTENABLE:
889 		/*
890 		 * Nothing special to do for USB.
891 		 */
892 		break;
893 
894 
895 	case KIOCMKTONE:
896 		if (iocp->ioc_count != TRANSPARENT) {
897 			err = EINVAL;
898 			break;
899 		}
900 
901 		tmp = (int)(*(intptr_t *)mp->b_cont->b_rptr);
902 		cycles = tmp & 0xffff;
903 		msecs = (tmp >> 16) & 0xffff;
904 
905 		if (cycles == 0)
906 			frequency = UINT16_MAX;
907 		else if (cycles == UINT16_MAX)
908 			frequency = 0;
909 		else {
910 			frequency = (PIT_HZ + cycles / 2) / cycles;
911 			if (frequency > UINT16_MAX)
912 				frequency = UINT16_MAX;
913 		}
914 
915 		err = beep_mktone(frequency, msecs);
916 		break;
917 
918 	default:
919 
920 		return (KBTRANS_MESSAGE_NOT_HANDLED);
921 	}
922 
923 	/*
924 	 * Send ACK/NACK to upper module for
925 	 * the messages that have been handled.
926 	 */
927 	if (err != 0) {
928 		iocp->ioc_rval = 0;
929 		iocp->ioc_error = err;
930 		mp->b_datap->db_type = M_IOCNAK;
931 	} else {
932 		iocp->ioc_rval = 0;
933 		iocp->ioc_error = 0;	/* brain rot */
934 		mp->b_datap->db_type = M_IOCACK;
935 	}
936 
937 	/* Send the response back up the stream */
938 	putnext(usbkbmd->usbkbm_readq, mp);
939 
940 	return (KBTRANS_MESSAGE_HANDLED);
941 
942 allocfailure:
943 	/*
944 	 * We needed to allocate something to handle this "ioctl", but
945 	 * couldn't; save this "ioctl" and arrange to get called back when
946 	 * it's more likely that we can get what we need.
947 	 * If there's already one being saved, throw it out, since it
948 	 * must have timed out.
949 	 */
950 	freemsg(usbkbmd->usbkbm_streams_iocpending);
951 	usbkbmd->usbkbm_streams_iocpending = mp;
952 
953 	if (usbkbmd->usbkbm_streams_bufcallid) {
954 
955 		qunbufcall(usbkbmd->usbkbm_readq,
956 		    usbkbmd->usbkbm_streams_bufcallid);
957 	}
958 	usbkbmd->usbkbm_streams_bufcallid =
959 	    qbufcall(usbkbmd->usbkbm_readq, ioctlrespsize, BPRI_HI,
960 	    usbkbm_reioctl, usbkbmd);
961 
962 	return (KBTRANS_MESSAGE_HANDLED);
963 }
964 
965 /*
966  * usbkbm_kioccmd :
967  *	Handles KIOCCMD ioctl.
968  */
969 static int
970 usbkbm_kioccmd(usbkbm_state_t *usbkbmd, register mblk_t *mp,
971 		char command, size_t *ioctlrepsize)
972 {
973 	register mblk_t			*datap;
974 	register struct iocblk		*iocp;
975 	int				err = 0;
976 
977 	iocp = (struct iocblk *)mp->b_rptr;
978 
979 	switch (command) {
980 
981 		/* Keyboard layout command */
982 		case KBD_CMD_GETLAYOUT:
983 			/* layout learned at attached time. */
984 			datap = allocb(sizeof (int), BPRI_HI);
985 
986 			/* Return error  on allocation failure */
987 			if (datap == NULL) {
988 				*ioctlrepsize = sizeof (int);
989 
990 				return (EIO);
991 			}
992 
993 			*(int *)datap->b_wptr = usbkbmd->usbkbm_layout;
994 			datap->b_wptr += sizeof (int);
995 			freemsg(mp->b_cont);
996 			mp->b_cont = datap;
997 			iocp->ioc_count = sizeof (int);
998 			break;
999 
1000 		case KBD_CMD_SETLED:
1001 			/*
1002 			 * Emulate type 4 keyboard :
1003 			 * Ignore this ioctl; the following
1004 			 * ioctl will specify the data byte for
1005 			 * setting the LEDs; setting usbkbm_setled_second_byte
1006 			 * will help recognizing that ioctl
1007 			 */
1008 			usbkbmd->usbkbm_setled_second_byte = 1;
1009 			break;
1010 
1011 		case KBD_CMD_RESET:
1012 			break;
1013 
1014 		case KBD_CMD_BELL:
1015 			/*
1016 			 * USB keyboards do not have a beeper
1017 			 * in it, the generic beeper interface
1018 			 * is used. Turn the beeper on.
1019 			 */
1020 			(void) beeper_on(BEEP_TYPE4);
1021 			break;
1022 
1023 		case KBD_CMD_NOBELL:
1024 			/*
1025 			 * USB keyboards do not have a beeper
1026 			 * in it, the generic beeper interface
1027 			 * is used. Turn the beeper off.
1028 			 */
1029 			(void) beeper_off();
1030 			break;
1031 
1032 		case KBD_CMD_CLICK:
1033 			/* FALLTHRU */
1034 		case KBD_CMD_NOCLICK:
1035 			break;
1036 
1037 		default:
1038 			err = EIO;
1039 			break;
1040 
1041 	}
1042 
1043 	return (err);
1044 }
1045 
1046 
1047 /*
1048  * usbkbm_rput :
1049  *	Put procedure for input from driver end of stream (read queue).
1050  */
1051 static void
1052 usbkbm_rput(register queue_t *q, register mblk_t *mp)
1053 {
1054 	usbkbm_state_t		*usbkbmd;
1055 
1056 	usbkbmd = (usbkbm_state_t *)q->q_ptr;
1057 
1058 	USB_DPRINTF_L3(PRINT_MASK_ALL, usbkbm_log_handle,
1059 	    "usbkbm_rput");
1060 
1061 	if (usbkbmd == 0) {
1062 		freemsg(mp);	/* nobody's listening */
1063 
1064 		return;
1065 	}
1066 
1067 	switch (mp->b_datap->db_type) {
1068 
1069 	case M_FLUSH:
1070 		if (*mp->b_rptr & FLUSHW)
1071 			flushq(WR(q), FLUSHDATA);
1072 		if (*mp->b_rptr & FLUSHR)
1073 			flushq(q, FLUSHDATA);
1074 
1075 		freemsg(mp);
1076 
1077 		return;
1078 	case M_BREAK:
1079 		/*
1080 		 * Will get M_BREAK only if this is not the system
1081 		 * keyboard, otherwise serial port will eat break
1082 		 * and call kmdb/OBP, without passing anything up.
1083 		 */
1084 		freemsg(mp);
1085 
1086 		return;
1087 	case M_DATA:
1088 		if (!(usbkbmd->usbkbm_flags & USBKBM_OPEN)) {
1089 			freemsg(mp);	/* not ready to listen */
1090 
1091 			return;
1092 		}
1093 
1094 		break;
1095 	case M_CTL:
1096 		usbkbm_mctl_receive(q, mp);
1097 
1098 		return;
1099 	case M_ERROR:
1100 		usbkbmd->usbkbm_flags &= ~USBKBM_QWAIT;
1101 		freemsg(mp);
1102 
1103 		return;
1104 	case M_IOCACK:
1105 	case M_IOCNAK:
1106 		putnext(q, mp);
1107 
1108 		return;
1109 	default:
1110 		putnext(q, mp);
1111 
1112 		return;
1113 	}
1114 
1115 	/*
1116 	 * A data message, consisting of bytes from the keyboard.
1117 	 * Ram them through the translator, only if there are
1118 	 * correct no. of bytes.
1119 	 */
1120 	if (MBLKL(mp) == usbkbmd->usbkbm_packet_size) {
1121 		usbkbm_unpack_usb_packet(usbkbmd, usbkbm_streams_callback,
1122 		    (uchar_t *)mp->b_rptr, usbkbmd->usbkbm_packet_size);
1123 	}
1124 
1125 	freemsg(mp);
1126 }
1127 
1128 /*
1129  * usbkbm_mctl_receive :
1130  *	Handle M_CTL messages from hid. If we don't understand
1131  *	the command, send it up.
1132  */
1133 static void
1134 usbkbm_mctl_receive(register queue_t *q, register mblk_t *mp)
1135 {
1136 	register usbkbm_state_t *usbkbmd = (usbkbm_state_t *)q->q_ptr;
1137 	register struct iocblk *iocp, mctlmsg;
1138 	caddr_t  data = NULL;
1139 	mblk_t	*reply_mp, *mctl_ptr;
1140 	uchar_t	new_buffer[USBKBM_MAXPKTSIZE];
1141 	size_t   size;
1142 	hid_req_t buf;
1143 	size_t len = sizeof (buf);
1144 
1145 
1146 
1147 	iocp = (struct iocblk *)mp->b_rptr;
1148 	if (mp->b_cont != NULL)
1149 		data = (caddr_t)mp->b_cont->b_rptr;
1150 
1151 	switch (iocp->ioc_cmd) {
1152 
1153 	case HID_SET_REPORT:
1154 		USB_DPRINTF_L3(PRINT_MASK_ALL, usbkbm_log_handle,
1155 		    "usbkbm_mctl_receive HID_SET mctl");
1156 		freemsg(mp);
1157 		/* Setting of the LED is not waiting for this message */
1158 
1159 		break;
1160 	case HID_SET_PROTOCOL:
1161 		freemsg(mp);
1162 		usbkbmd->usbkbm_flags &= ~USBKBM_QWAIT;
1163 
1164 		break;
1165 	case HID_GET_PARSER_HANDLE:
1166 		if ((data != NULL) &&
1167 		    (iocp->ioc_count == sizeof (hidparser_handle_t)) &&
1168 		    (MBLKL(mp->b_cont) == iocp->ioc_count)) {
1169 			usbkbmd->usbkbm_report_descr =
1170 			    *(hidparser_handle_t *)data;
1171 		} else {
1172 			usbkbmd->usbkbm_report_descr = NULL;
1173 		}
1174 		freemsg(mp);
1175 		usbkbmd->usbkbm_flags &= ~USBKBM_QWAIT;
1176 
1177 		break;
1178 	case HID_GET_VID_PID:
1179 		if ((data != NULL) &&
1180 		    (iocp->ioc_count == sizeof (hid_vid_pid_t)) &&
1181 		    (MBLKL(mp->b_cont) == iocp->ioc_count)) {
1182 			bcopy(data, &usbkbmd->usbkbm_vid_pid, iocp->ioc_count);
1183 		}
1184 		freemsg(mp);
1185 		usbkbmd->usbkbm_flags &= ~USBKBM_QWAIT;
1186 
1187 		break;
1188 	case HID_OPEN_POLLED_INPUT:
1189 		USB_DPRINTF_L3(PRINT_MASK_ALL, usbkbm_log_handle,
1190 		    "usbkbm_mctl_receive HID_OPEN_POLLED_INPUT");
1191 
1192 		size = sizeof (hid_polled_input_callback_t);
1193 		reply_mp = usbkbmd->usbkbm_pending_link;
1194 		if ((data != NULL) &&
1195 		    (iocp->ioc_count == size) &&
1196 		    (MBLKL(mp->b_cont) == size)) {
1197 			/*
1198 			 *  Copy the information from hid into the
1199 			 * state structure
1200 			 */
1201 			bcopy(data, &usbkbmd->usbkbm_hid_callback, size);
1202 			reply_mp->b_datap->db_type = M_IOCACK;
1203 
1204 			/*
1205 			 * We are given an appropriate-sized data block,
1206 			 * and return a pointer to our structure in it.
1207 			 * The structure is saved in the states structure
1208 			 */
1209 			*(cons_polledio_t **)reply_mp->b_cont->b_rptr =
1210 			    &usbkbmd->usbkbm_polled_info;
1211 
1212 		} else {
1213 			reply_mp->b_datap->db_type = M_IOCNAK;
1214 		}
1215 		freemsg(mp);
1216 
1217 		usbkbmd->usbkbm_pending_link = NULL;
1218 
1219 		putnext(q, reply_mp);
1220 
1221 		break;
1222 	case HID_CLOSE_POLLED_INPUT:
1223 		USB_DPRINTF_L3(PRINT_MASK_ALL, usbkbm_log_handle,
1224 		    "usbkbm_mctl_receive HID_CLOSE_POLLED_INPUT");
1225 
1226 
1227 		bzero(&usbkbmd->usbkbm_hid_callback,
1228 		    sizeof (hid_polled_input_callback_t));
1229 
1230 		freemsg(mp);
1231 
1232 		reply_mp = usbkbmd->usbkbm_pending_link;
1233 
1234 		iocp = (struct iocblk *)reply_mp->b_rptr;
1235 
1236 		USB_DPRINTF_L3(PRINT_MASK_ALL, usbkbm_log_handle,
1237 		    "usbkbm_mctl_receive reply reply_mp 0x%p cmd 0x%x",
1238 		    (void *)reply_mp, iocp->ioc_cmd);
1239 
1240 
1241 		reply_mp->b_datap->db_type = M_IOCACK;
1242 
1243 		usbkbmd->usbkbm_pending_link = NULL;
1244 
1245 		putnext(q, reply_mp);
1246 
1247 		break;
1248 	case HID_DISCONNECT_EVENT :
1249 	case HID_POWER_OFF:
1250 		USB_DPRINTF_L3(PRINT_MASK_ALL, usbkbm_log_handle,
1251 		    "usbkbm_mctl_receive HID_DISCONNECT_EVENT/HID_POWER_OFF");
1252 
1253 		/* Indicate all keys have been released */
1254 		bzero(new_buffer, USBKBM_MAXPKTSIZE);
1255 		usbkbm_unpack_usb_packet(usbkbmd, usbkbm_streams_callback,
1256 		    new_buffer, usbkbmd->usbkbm_packet_size);
1257 
1258 		freemsg(mp);
1259 
1260 		break;
1261 	case HID_CONNECT_EVENT:
1262 		mctlmsg.ioc_cmd = HID_SET_PROTOCOL;
1263 		mctlmsg.ioc_count = 0;
1264 		buf.hid_req_version_no = HID_VERSION_V_0;
1265 		buf.hid_req_wValue = SET_BOOT_PROTOCOL;
1266 		buf.hid_req_wLength = 0;
1267 		mctl_ptr = usba_mk_mctl(mctlmsg, &buf, len);
1268 		if (mctl_ptr == NULL) {
1269 			USB_DPRINTF_L2(PRINT_MASK_ALL, usbkbm_log_handle,
1270 			    "usbkbm_mctl_receive HID_CONNECT_EVENT: "
1271 			    "Set protocol failed");
1272 		} else {
1273 			putnext(usbkbmd->usbkbm_writeq, mctl_ptr);
1274 		}
1275 
1276 		/* FALLTHRU */
1277 	case HID_FULL_POWER :
1278 		USB_DPRINTF_L3(PRINT_MASK_ALL, usbkbm_log_handle,
1279 		    "usbkbm_mctl_receive restore LEDs");
1280 
1281 		/* send setled command down to restore LED states */
1282 		usbkbm_streams_setled((struct kbtrans_hardware *)usbkbmd,
1283 		    usbkbm_led_state);
1284 
1285 		freemsg(mp);
1286 
1287 		break;
1288 	default:
1289 		putnext(q, mp);
1290 
1291 		break;
1292 	}
1293 }
1294 
1295 
1296 /*
1297  * usbkbm_streams_setled :
1298  *	Update the keyboard LEDs to match the current keyboard state.
1299  *	Send LED state downstreams to hid driver.
1300  */
1301 static void
1302 usbkbm_streams_setled(struct kbtrans_hardware *kbtrans_hw, int state)
1303 {
1304 	struct iocblk	mctlmsg;
1305 	mblk_t		*mctl_ptr;
1306 	hid_req_t	*LED_report;
1307 	usbkbm_state_t	*usbkbmd;
1308 	uchar_t		led_state;
1309 
1310 	usbkbm_led_state = (uchar_t)state;
1311 
1312 	usbkbmd = (usbkbm_state_t *)kbtrans_hw;
1313 
1314 	LED_report = kmem_zalloc(sizeof (hid_req_t), KM_NOSLEEP);
1315 	if (LED_report == NULL) {
1316 
1317 		return;
1318 	}
1319 
1320 	/*
1321 	 * Send the request to the hid driver to set LED.
1322 	 */
1323 
1324 	led_state = 0;
1325 
1326 	/*
1327 	 * Set the led state based on the state that is passed in.
1328 	 */
1329 	if (state & LED_NUM_LOCK) {
1330 		led_state |= USB_LED_NUM_LOCK;
1331 	}
1332 
1333 	if (state & LED_COMPOSE) {
1334 		led_state |= USB_LED_COMPOSE;
1335 	}
1336 
1337 	if (state & LED_SCROLL_LOCK) {
1338 		led_state |= USB_LED_SCROLL_LOCK;
1339 	}
1340 
1341 	if (state & LED_CAPS_LOCK) {
1342 		led_state |= USB_LED_CAPS_LOCK;
1343 	}
1344 
1345 	if (state & LED_KANA) {
1346 		led_state |= USB_LED_KANA;
1347 	}
1348 
1349 	LED_report->hid_req_version_no = HID_VERSION_V_0;
1350 	LED_report->hid_req_wValue = REPORT_TYPE_OUTPUT;
1351 	LED_report->hid_req_wLength = sizeof (uchar_t);
1352 	LED_report->hid_req_data[0] = led_state;
1353 
1354 	mctlmsg.ioc_cmd = HID_SET_REPORT;
1355 	mctlmsg.ioc_count = sizeof (LED_report);
1356 	mctl_ptr = usba_mk_mctl(mctlmsg, LED_report, sizeof (hid_req_t));
1357 	if (mctl_ptr != NULL) {
1358 		putnext(usbkbmd->usbkbm_writeq, mctl_ptr);
1359 	}
1360 
1361 	/*
1362 	 * We are not waiting for response of HID_SET_REPORT
1363 	 * mctl for setting the LED.
1364 	 */
1365 	kmem_free(LED_report, sizeof (hid_req_t));
1366 }
1367 
1368 
1369 /*
1370  * usbkbm_polled_keycheck :
1371  *	This routine is called to determine if there is a scancode that
1372  *	is available for input.  This routine is called at poll time and
1373  *	returns a key/state pair to the caller.  If there are characters
1374  *	buffered up, the routine returns right away with the key/state pair.
1375  *	Otherwise, the routine calls down to check for characters and returns
1376  *	the first key/state pair if there are any characters pending.
1377  */
1378 static boolean_t
1379 usbkbm_polled_keycheck(struct kbtrans_hardware *hw,
1380 	int *key, enum keystate *state)
1381 {
1382 	usbkbm_state_t			*usbkbmd;
1383 	uchar_t				*buffer;
1384 	unsigned			num_keys;
1385 	hid_polled_handle_t		hid_polled_handle;
1386 
1387 	usbkbmd = (usbkbm_state_t *)hw;
1388 
1389 	/*
1390 	 * If there are already characters buffered up, then we are done.
1391 	 */
1392 	if (usbkbmd->usbkbm_polled_buffer_num_characters != 0) {
1393 
1394 		usbkbm_get_scancode(usbkbmd, key, state);
1395 
1396 		return (B_TRUE);
1397 	}
1398 
1399 	hid_polled_handle =
1400 	    usbkbmd->usbkbm_hid_callback.hid_polled_input_handle;
1401 
1402 	num_keys = (usbkbmd->usbkbm_hid_callback.hid_polled_read)
1403 	    (hid_polled_handle, &buffer);
1404 
1405 	/*
1406 	 * If we don't get any characters back then indicate that, and we
1407 	 * are done.
1408 	 */
1409 	if (num_keys == 0) {
1410 
1411 		return (B_FALSE);
1412 	}
1413 
1414 	/*
1415 	 * We have a usb packet, so pass this packet to
1416 	 * usbkbm_unpack_usb_packet so that it can be broken up into
1417 	 * individual key/state values.
1418 	 */
1419 	usbkbm_unpack_usb_packet(usbkbmd, usbkbm_poll_callback,
1420 	    buffer, num_keys);
1421 
1422 	/*
1423 	 * If a scancode was returned as a result of this packet,
1424 	 * then translate the scancode.
1425 	 */
1426 	if (usbkbmd->usbkbm_polled_buffer_num_characters != 0) {
1427 
1428 		usbkbm_get_scancode(usbkbmd, key, state);
1429 
1430 		return (B_TRUE);
1431 	}
1432 
1433 	return (B_FALSE);
1434 }
1435 
1436 static ushort_t	usbkbm_get_state(usbkbm_state_t *usbkbmd)
1437 {
1438 	ushort_t	ret;
1439 
1440 	ASSERT(usbkbmd->usbkbm_vkbd_type == KB_PC ||
1441 	    usbkbmd->usbkbm_vkbd_type == KB_USB);
1442 
1443 	if (usbkbmd->usbkbm_vkbd_type == KB_PC)
1444 		ret = INDEXTO_PC;
1445 	else
1446 		ret = INDEXTO_USB;
1447 
1448 	return (ret);
1449 }
1450 /*
1451  * usbkbm_streams_callback :
1452  *	This is the routine that is going to be called when unpacking
1453  *	usb packets for normal streams-based input.  We pass a pointer
1454  *	to this routine to usbkbm_unpack_usb_packet.  This routine will
1455  *	get called with an unpacked key (scancode) and state (press/release).
1456  *	We pass it to the generic keyboard module.
1457  *
1458  * 	'index' and the function pointers:
1459  *	Map USB scancodes to PC scancodes by lookup table.
1460  *	This fix is mainly meant for x86 platforms. For SPARC systems
1461  *	this fix doesn't change the way in which the scancodes are processed.
1462  */
1463 static void
1464 usbkbm_streams_callback(usbkbm_state_t *usbkbmd, int key, enum keystate state)
1465 {
1466 	ushort_t index = usbkbm_get_state(usbkbmd);
1467 	(*usbkbm_xlate[index])(usbkbmd, key, state);
1468 }
1469 
1470 /*
1471  * Don't do any translations. Send to 'kbtrans' for processing.
1472  */
1473 static void
1474 usbkbm_wrap_kbtrans(usbkbm_state_t *usbkbmd, int key, enum keystate state)
1475 {
1476 	kbtrans_streams_key(usbkbmd->usbkbm_kbtrans, key, state);
1477 }
1478 
1479 /*
1480  * Translate USB scancodes to PC scancodes before sending it to 'kbtrans'
1481  */
1482 void
1483 usbkbm_usb2pc_xlate(usbkbm_state_t *usbkbmd, int key, enum keystate state)
1484 {
1485 	key = kbtrans_keycode_usb2pc(key);
1486 	kbtrans_streams_key(usbkbmd->usbkbm_kbtrans, key, state);
1487 }
1488 
1489 /*
1490  * usbkbm_poll_callback :
1491  *	This is the routine that is going to be called when unpacking
1492  *	usb packets for polled input.  We pass a pointer to this routine
1493  *	to usbkbm_unpack_usb_packet.  This routine will get called with
1494  *	an unpacked key (scancode) and state (press/release).  We will
1495  *	store the key/state pair into a circular buffer so that it can
1496  *	be translated into an ascii key later.
1497  */
1498 static void
1499 usbkbm_poll_callback(usbkbm_state_t *usbkbmd, int key, enum keystate state)
1500 {
1501 	/*
1502 	 * Check to make sure that the buffer isn't already full
1503 	 */
1504 	if (usbkbmd->usbkbm_polled_buffer_num_characters ==
1505 	    USB_POLLED_BUFFER_SIZE) {
1506 
1507 		/*
1508 		 * The buffer is full, we will drop this character.
1509 		 */
1510 		return;
1511 	}
1512 
1513 	/*
1514 	 * Save the scancode in the buffer
1515 	 */
1516 	usbkbmd->usbkbm_polled_buffer_head->poll_key = key;
1517 	usbkbmd->usbkbm_polled_buffer_head->poll_state = state;
1518 
1519 	/*
1520 	 * We have one more character in the buffer
1521 	 */
1522 	usbkbmd->usbkbm_polled_buffer_num_characters++;
1523 
1524 	/*
1525 	 * Increment to the next available slot.
1526 	 */
1527 	usbkbmd->usbkbm_polled_buffer_head++;
1528 
1529 	/*
1530 	 * Check to see if the tail has wrapped.
1531 	 */
1532 	if (usbkbmd->usbkbm_polled_buffer_head -
1533 	    usbkbmd->usbkbm_polled_scancode_buffer ==
1534 	    USB_POLLED_BUFFER_SIZE) {
1535 
1536 		usbkbmd->usbkbm_polled_buffer_head =
1537 		    usbkbmd->usbkbm_polled_scancode_buffer;
1538 	}
1539 }
1540 
1541 /*
1542  * usbkbm_get_scancode :
1543  *	This routine retreives a key/state pair from the circular buffer.
1544  *	The pair was put in the buffer by usbkbm_poll_callback when a
1545  *	USB packet was translated into a key/state by usbkbm_unpack_usb_packet.
1546  */
1547 static void
1548 usbkbm_get_scancode(usbkbm_state_t *usbkbmd, int *key, enum keystate *state)
1549 {
1550 	/*
1551 	 * Copy the character.
1552 	 */
1553 	*key = usbkbmd->usbkbm_polled_buffer_tail->poll_key;
1554 	*state = usbkbmd->usbkbm_polled_buffer_tail->poll_state;
1555 
1556 	/*
1557 	 * Increment to the next character to be copied from
1558 	 * and to.
1559 	 */
1560 	usbkbmd->usbkbm_polled_buffer_tail++;
1561 
1562 	/*
1563 	 * Check to see if the tail has wrapped.
1564 	 */
1565 	if (usbkbmd->usbkbm_polled_buffer_tail -
1566 	    usbkbmd->usbkbm_polled_scancode_buffer ==
1567 	    USB_POLLED_BUFFER_SIZE) {
1568 
1569 		usbkbmd->usbkbm_polled_buffer_tail =
1570 		    usbkbmd->usbkbm_polled_scancode_buffer;
1571 	}
1572 
1573 	/*
1574 	 * We have one less character in the buffer.
1575 	 */
1576 	usbkbmd->usbkbm_polled_buffer_num_characters--;
1577 }
1578 
1579 /*
1580  * usbkbm_polled_setled :
1581  *	This routine is a place holder.  Someday, we may want to allow led
1582  *	state to be updated from within polled mode.
1583  */
1584 /* ARGSUSED */
1585 static void
1586 usbkbm_polled_setled(struct kbtrans_hardware *hw, int led_state)
1587 {
1588 	/* nothing to do for now */
1589 }
1590 
1591 /*
1592  * This is a pass-thru routine to get a character at poll time.
1593  */
1594 static int
1595 usbkbm_polled_getchar(cons_polledio_arg_t arg)
1596 {
1597 	usbkbm_state_t			*usbkbmd;
1598 
1599 	usbkbmd = (usbkbm_state_t *)arg;
1600 
1601 	return (kbtrans_getchar(usbkbmd->usbkbm_kbtrans));
1602 }
1603 
1604 /*
1605  * This is a pass-thru routine to test if character is available for reading
1606  * at poll time.
1607  */
1608 static boolean_t
1609 usbkbm_polled_ischar(cons_polledio_arg_t arg)
1610 {
1611 	usbkbm_state_t			*usbkbmd;
1612 
1613 	usbkbmd = (usbkbm_state_t *)arg;
1614 
1615 	return (kbtrans_ischar(usbkbmd->usbkbm_kbtrans));
1616 }
1617 
1618 /*
1619  * usbkbm_polled_input_enter :
1620  *	This is a pass-thru initialization routine for the lower layer drivers.
1621  *	This routine is called at poll time to set the state for polled input.
1622  */
1623 static void
1624 usbkbm_polled_enter(cons_polledio_arg_t arg)
1625 {
1626 	usbkbm_state_t			*usbkbmd;
1627 	hid_polled_handle_t		hid_polled_handle;
1628 	uint_t				uindex;
1629 
1630 	usbkbmd = (usbkbm_state_t *)arg;
1631 
1632 	/*
1633 	 * Before switching to POLLED mode, copy the contents of
1634 	 * usbkbm_pendingusbpacket to usbkbm_lastusbpacket since
1635 	 * usbkbm_pendingusbpacket field has currently processed
1636 	 * key events of the current OS mode usb keyboard packet.
1637 	 */
1638 	for (uindex = 2; uindex < USBKBM_MAXPKTSIZE; uindex ++) {
1639 		usbkbmd->usbkbm_lastusbpacket[uindex] =
1640 		    usbkbmd->usbkbm_pendingusbpacket[uindex];
1641 
1642 		usbkbmd->usbkbm_pendingusbpacket[uindex] = 0;
1643 	}
1644 
1645 	hid_polled_handle =
1646 	    usbkbmd->usbkbm_hid_callback.hid_polled_input_handle;
1647 
1648 	(void) (usbkbmd->usbkbm_hid_callback.hid_polled_input_enter)
1649 	    (hid_polled_handle);
1650 }
1651 
1652 /*
1653  * usbkbm_polled_input_exit :
1654  *	This is a pass-thru restoration routine for the lower layer drivers.
1655  *	This routine is called at poll time to reset the state back to streams
1656  *	input.
1657  */
1658 static void
1659 usbkbm_polled_exit(cons_polledio_arg_t arg)
1660 {
1661 	usbkbm_state_t			*usbkbmd;
1662 	hid_polled_handle_t		hid_polled_handle;
1663 	uint_t				uindex;
1664 
1665 	usbkbmd = (usbkbm_state_t *)arg;
1666 
1667 	/*
1668 	 * Before returning to OS mode, copy the contents of
1669 	 * usbkbm_lastusbpacket to usbkbm_pendingusbpacket since
1670 	 * usbkbm_lastusbpacket field has processed key events
1671 	 * of the last POLLED mode usb keyboard packet.
1672 	 */
1673 	for (uindex = 2; uindex < USBKBM_MAXPKTSIZE; uindex ++) {
1674 		usbkbmd->usbkbm_pendingusbpacket[uindex] =
1675 		    usbkbmd->usbkbm_lastusbpacket[uindex];
1676 
1677 		usbkbmd->usbkbm_lastusbpacket[uindex] = 0;
1678 	}
1679 
1680 	hid_polled_handle =
1681 	    usbkbmd->usbkbm_hid_callback.hid_polled_input_handle;
1682 
1683 	(void) (usbkbmd->usbkbm_hid_callback.hid_polled_input_exit)
1684 	    (hid_polled_handle);
1685 }
1686 
1687 /*
1688  * usbkbm_unpack_usb_packet :
1689  *	USB key packets contain 8 bytes while in boot-protocol mode.
1690  *	The first byte contains bit packed modifier key information.
1691  *	Second byte is reserved. The last 6 bytes contain bytes of
1692  *	currently pressed keys. If a key was not recorded on the
1693  *	previous packet, but present in the current packet, then set
1694  *	state to KEY_PRESSED. If a key was recorded in the previous packet,
1695  *	but not present in the current packet, then state to KEY_RELEASED
1696  *	Follow a similar algorithm for bit packed modifier keys.
1697  */
1698 static void
1699 usbkbm_unpack_usb_packet(usbkbm_state_t *usbkbmd, process_key_callback_t func,
1700 	uchar_t *usbpacket, int packet_size)
1701 {
1702 	uchar_t		mkb;
1703 	uchar_t		lastmkb;
1704 	uchar_t		*lastusbpacket = usbkbmd->usbkbm_lastusbpacket;
1705 	int		uindex, lindex, rollover;
1706 
1707 	mkb = usbpacket[0];
1708 
1709 	lastmkb = lastusbpacket[0];
1710 
1711 	for (uindex = 0; uindex < packet_size; uindex++) {
1712 
1713 		USB_DPRINTF_L3(PRINT_MASK_PACKET, usbkbm_log_handle,
1714 		    " %x ", usbpacket[uindex]);
1715 	}
1716 
1717 	USB_DPRINTF_L3(PRINT_MASK_PACKET, usbkbm_log_handle,
1718 	    " is the usbkeypacket");
1719 
1720 	/* check to see if modifier keys are different */
1721 	if (mkb != lastmkb) {
1722 
1723 		if ((mkb & USB_LSHIFTBIT) != (lastmkb & USB_LSHIFTBIT)) {
1724 			(*func)(usbkbmd, USB_LSHIFTKEY, (mkb&USB_LSHIFTBIT) ?
1725 			    KEY_PRESSED : KEY_RELEASED);
1726 			USB_DPRINTF_L3(PRINT_MASK_ALL, usbkbm_log_handle,
1727 			    "unpack: sending USB_LSHIFTKEY");
1728 		}
1729 
1730 		if ((mkb & USB_LCTLBIT) != (lastmkb & USB_LCTLBIT)) {
1731 			(*func)(usbkbmd, USB_LCTLCKEY, mkb&USB_LCTLBIT ?
1732 			    KEY_PRESSED : KEY_RELEASED);
1733 		}
1734 
1735 		if ((mkb & USB_LALTBIT) != (lastmkb & USB_LALTBIT)) {
1736 			(*func)(usbkbmd, USB_LALTKEY, mkb&USB_LALTBIT ?
1737 			    KEY_PRESSED : KEY_RELEASED);
1738 		}
1739 
1740 		if ((mkb & USB_LMETABIT) != (lastmkb & USB_LMETABIT)) {
1741 			(*func)(usbkbmd, USB_LMETAKEY, mkb&USB_LMETABIT ?
1742 			    KEY_PRESSED : KEY_RELEASED);
1743 		}
1744 
1745 		if ((mkb & USB_RMETABIT) != (lastmkb & USB_RMETABIT)) {
1746 			(*func)(usbkbmd, USB_RMETAKEY, mkb&USB_RMETABIT ?
1747 			    KEY_PRESSED : KEY_RELEASED);
1748 		}
1749 
1750 		if ((mkb & USB_RALTBIT) != (lastmkb & USB_RALTBIT)) {
1751 			(*func)(usbkbmd, USB_RALTKEY, mkb&USB_RALTBIT ?
1752 			    KEY_PRESSED : KEY_RELEASED);
1753 		}
1754 
1755 		if ((mkb & USB_RCTLBIT) != (lastmkb & USB_RCTLBIT)) {
1756 			(*func)(usbkbmd, USB_RCTLCKEY, mkb&USB_RCTLBIT ?
1757 			    KEY_PRESSED : KEY_RELEASED);
1758 		}
1759 
1760 		if ((mkb & USB_RSHIFTBIT) != (lastmkb & USB_RSHIFTBIT)) {
1761 			(*func)(usbkbmd, USB_RSHIFTKEY, mkb&USB_RSHIFTBIT ?
1762 			    KEY_PRESSED : KEY_RELEASED);
1763 			USB_DPRINTF_L3(PRINT_MASK_ALL, usbkbm_log_handle,
1764 			    "unpack: sending USB_RSHIFTKEY");
1765 		}
1766 	}
1767 
1768 	/* save modifier bits */
1769 	lastusbpacket[0] = usbpacket[0];
1770 
1771 	/* Check Keyboard rollover error. */
1772 	if (usbpacket[2] == USB_ERRORROLLOVER) {
1773 		rollover = 1;
1774 		for (uindex = 3; uindex < packet_size;
1775 		    uindex++) {
1776 			if (usbpacket[uindex] != USB_ERRORROLLOVER) {
1777 				rollover = 0;
1778 				break;
1779 			}
1780 		}
1781 		if (rollover) {
1782 			USB_DPRINTF_L3(PRINT_MASK_ALL, usbkbm_log_handle,
1783 			    "unpack: errorrollover");
1784 			return;
1785 		}
1786 	}
1787 
1788 	/* check for released keys */
1789 	for (lindex = 2; lindex < packet_size; lindex++) {
1790 		int released = 1;
1791 
1792 		if (lastusbpacket[lindex] == 0) {
1793 			continue;
1794 		}
1795 		for (uindex = 2; uindex < packet_size; uindex++)
1796 			if (usbpacket[uindex] == lastusbpacket[lindex]) {
1797 				released = 0;
1798 				break;
1799 			}
1800 		if (released) {
1801 			(*func)(usbkbmd, lastusbpacket[lindex], KEY_RELEASED);
1802 		}
1803 	}
1804 
1805 	/* check for new presses */
1806 	for (uindex = 2; uindex < packet_size; uindex++) {
1807 		int newkey = 1;
1808 
1809 		usbkbmd->usbkbm_pendingusbpacket[uindex] = usbpacket[uindex];
1810 
1811 		if (usbpacket[uindex] == 0) {
1812 			continue;
1813 		}
1814 
1815 		for (lindex = 2; lindex < packet_size; lindex++) {
1816 			if (usbpacket[uindex] == lastusbpacket[lindex]) {
1817 				newkey = 0;
1818 				break;
1819 			}
1820 		}
1821 
1822 		if (newkey) {
1823 			/*
1824 			 * Modifier keys can be present as part of both the
1825 			 * first byte and as separate key bytes. In the sec-
1826 			 * ond case ignore it.
1827 			 */
1828 
1829 			if (!usbkbm_is_modkey(usbpacket[uindex])) {
1830 				(*func)(usbkbmd, usbpacket[uindex],
1831 				    KEY_PRESSED);
1832 			} else {
1833 				usbkbmd->usbkbm_pendingusbpacket[uindex] = 0;
1834 
1835 				continue;
1836 			}
1837 		}
1838 	}
1839 
1840 	/*
1841 	 * Copy the processed key events of the current usb keyboard
1842 	 * packet, which is saved in the usbkbm_pendingusbpacket field
1843 	 * to the usbkbm_lastusbpacket field.
1844 	 */
1845 	for (uindex = 2; uindex < USBKBM_MAXPKTSIZE; uindex++) {
1846 		lastusbpacket[uindex] =
1847 		    usbkbmd->usbkbm_pendingusbpacket[uindex];
1848 		usbkbmd->usbkbm_pendingusbpacket[uindex] = 0;
1849 	}
1850 }
1851 
1852 static boolean_t
1853 usbkbm_is_modkey(uchar_t key)
1854 {
1855 
1856 	switch (key) {
1857 
1858 	case USB_LSHIFTKEY:
1859 	case USB_LCTLCKEY:
1860 	case USB_LALTKEY:
1861 	case USB_LMETAKEY:
1862 	case USB_RCTLCKEY:
1863 	case USB_RSHIFTKEY:
1864 	case USB_RMETAKEY:
1865 	case USB_RALTKEY:
1866 
1867 		return (B_TRUE);
1868 
1869 	default:
1870 
1871 		break;
1872 	}
1873 
1874 	return (B_FALSE);
1875 }
1876 
1877 /*
1878  * usbkbm_reioctl :
1879  *	This function is set up as call-back function should an ioctl fail.
1880  *	It retries the ioctl
1881  */
1882 static void
1883 usbkbm_reioctl(void	*arg)
1884 {
1885 	usbkbm_state_t	*usbkbmd;
1886 	mblk_t *mp;
1887 
1888 	usbkbmd = (usbkbm_state_t *)arg;
1889 
1890 	usbkbmd->usbkbm_streams_bufcallid = 0;
1891 
1892 	if ((mp = usbkbmd->usbkbm_streams_iocpending) != NULL) {
1893 
1894 		/* not pending any more */
1895 		usbkbmd->usbkbm_streams_iocpending = NULL;
1896 
1897 		(void) usbkbm_ioctl(usbkbmd->usbkbm_writeq, mp);
1898 	}
1899 }
1900 
1901 
1902 /*
1903  * usbkbm_set_protocol
1904  *	Issue an M_CTL to hid to set the desired protocol
1905  */
1906 static int
1907 usbkbm_set_protocol(usbkbm_state_t *usbkbmd, uint16_t protocol)
1908 {
1909 	struct iocblk mctlmsg;
1910 	hid_req_t buf;
1911 	mblk_t *mctl_ptr;
1912 	size_t len = sizeof (buf);
1913 	queue_t *q = usbkbmd->usbkbm_readq;
1914 
1915 	mctlmsg.ioc_cmd = HID_SET_PROTOCOL;
1916 	mctlmsg.ioc_count = 0;
1917 	buf.hid_req_version_no = HID_VERSION_V_0;
1918 	buf.hid_req_wValue = protocol;
1919 	buf.hid_req_wLength = 0;
1920 	mctl_ptr = usba_mk_mctl(mctlmsg, &buf, len);
1921 	if (mctl_ptr == NULL) {
1922 		usbkbmd->usbkbm_flags = 0;
1923 		(void) kbtrans_streams_fini(usbkbmd->usbkbm_kbtrans);
1924 		qprocsoff(q);
1925 		kmem_free(usbkbmd, sizeof (usbkbm_state_t));
1926 
1927 		return (ENOMEM);
1928 	}
1929 
1930 	usbkbmd->usbkbm_flags |= USBKBM_QWAIT;
1931 	putnext(usbkbmd->usbkbm_writeq, mctl_ptr);
1932 
1933 	while (usbkbmd->usbkbm_flags & USBKBM_QWAIT) {
1934 		if (qwait_sig(q) == 0) {
1935 			usbkbmd->usbkbm_flags = 0;
1936 			(void) kbtrans_streams_fini(usbkbmd->usbkbm_kbtrans);
1937 			qprocsoff(q);
1938 			kmem_free(usbkbmd, sizeof (usbkbm_state_t));
1939 
1940 			return (EINTR);
1941 		}
1942 	}
1943 
1944 	return (0);
1945 }
1946 
1947 
1948 /*
1949  * usbkbm_get_vid_pid
1950  *	Issue a M_CTL to hid to get the device info
1951  */
1952 static int
1953 usbkbm_get_vid_pid(usbkbm_state_t *usbkbmd)
1954 {
1955 	struct iocblk mctlmsg;
1956 	mblk_t *mctl_ptr;
1957 	queue_t *q = usbkbmd->usbkbm_readq;
1958 
1959 	mctlmsg.ioc_cmd = HID_GET_VID_PID;
1960 	mctlmsg.ioc_count = 0;
1961 
1962 	mctl_ptr = usba_mk_mctl(mctlmsg, NULL, 0);
1963 	if (mctl_ptr == NULL) {
1964 		(void) kbtrans_streams_fini(usbkbmd->usbkbm_kbtrans);
1965 		qprocsoff(q);
1966 		kmem_free(usbkbmd, sizeof (usbkbm_state_t));
1967 
1968 		return (ENOMEM);
1969 	}
1970 
1971 	putnext(usbkbmd->usbkbm_writeq, mctl_ptr);
1972 	usbkbmd->usbkbm_flags |= USBKBM_QWAIT;
1973 	while (usbkbmd->usbkbm_flags & USBKBM_QWAIT) {
1974 		if (qwait_sig(q) == 0) {
1975 			usbkbmd->usbkbm_flags = 0;
1976 			(void) kbtrans_streams_fini(usbkbmd->usbkbm_kbtrans);
1977 			qprocsoff(q);
1978 			kmem_free(usbkbmd, sizeof (usbkbm_state_t));
1979 
1980 			return (EINTR);
1981 		}
1982 	}
1983 
1984 	return (0);
1985 }
1986