xref: /illumos-gate/usr/src/uts/common/io/usb/clients/usbms/usbms.c (revision 1a220b56b93ff1dc80855691548503117af4cc10)
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 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/usb/usba/usbai_version.h>
29 #include <sys/usb/usba.h>
30 #include <sys/usb/clients/hid/hid.h>
31 #include <sys/usb/clients/hidparser/hidparser.h>
32 
33 #include <sys/stropts.h>
34 #include <sys/strsun.h>
35 #include <sys/vuid_event.h>
36 #include <sys/vuid_wheel.h>
37 #include <sys/termios.h>
38 #include <sys/termio.h>
39 #include <sys/strtty.h>
40 #include <sys/msreg.h>
41 #include <sys/msio.h>
42 
43 #include <sys/usb/clients/usbms/usbms.h>
44 
45 /* debugging information */
46 uint_t	usbms_errmask = (uint_t)PRINT_MASK_ALL;
47 uint_t	usbms_errlevel = USB_LOG_L2;
48 static usb_log_handle_t usbms_log_handle;
49 
50 static struct streamtab		usbms_streamtab;
51 
52 static struct fmodsw fsw = {
53 			"usbms",
54 			&usbms_streamtab,
55 			D_MP | D_MTPERMOD
56 };
57 
58 /*
59  * Module linkage information for the kernel.
60  */
61 static struct modlstrmod modlstrmod = {
62 	&mod_strmodops,
63 	"USB mouse streams %I%",
64 	&fsw
65 };
66 
67 static struct modlinkage modlinkage = {
68 	MODREV_1,
69 	(void *)&modlstrmod,
70 	NULL
71 };
72 
73 
74 int
75 _init(void)
76 {
77 	int rval = mod_install(&modlinkage);
78 
79 	if (rval == 0) {
80 		usbms_log_handle = usb_alloc_log_hdl(NULL, "usbms",
81 			&usbms_errlevel, &usbms_errmask, NULL, 0);
82 	}
83 
84 	return (rval);
85 }
86 
87 int
88 _fini(void)
89 {
90 	int rval = mod_remove(&modlinkage);
91 
92 	if (rval == 0) {
93 		usb_free_log_hdl(usbms_log_handle);
94 	}
95 
96 	return (rval);
97 }
98 
99 
100 int
101 _info(struct modinfo *modinfop)
102 {
103 
104 	return (mod_info(&modlinkage, modinfop));
105 }
106 
107 
108 /* Function prototypes */
109 static void		usbms_reioctl(void *);
110 static void		usbms_ioctl(queue_t *, mblk_t *);
111 static int		usbms_open();
112 static int		usbms_close();
113 static int		usbms_wput();
114 static void		usbms_rput();
115 static void		usbms_mctl_receive(
116 				register queue_t	*q,
117 				register mblk_t		*mp);
118 
119 static void		usbms_rserv(queue_t		*q);
120 static void		usbms_miocdata(
121 				register queue_t 	*q,
122 				register mblk_t 	*mp);
123 
124 static void		usbms_resched(void *);
125 
126 static int		usbms_getparms(
127 				register Ms_parms	*data,
128 				usbms_state_t		*usbmsp);
129 
130 static int		usbms_setparms(
131 				register Ms_parms	*data,
132 				usbms_state_t		*usbmsp);
133 
134 static int		usbms_get_screen_parms(
135 				register queue_t	*q,
136 				register mblk_t		*datap);
137 
138 static void		usbms_flush(usbms_state_t	*usbmsp);
139 
140 static void		usbms_incr(void *);
141 static void		usbms_input(
142 				usbms_state_t		*usbmsp,
143 				mblk_t			*mp);
144 static void		usbms_rserv_vuid_button(
145 				queue_t			*q,
146 				struct usbmouseinfo	*mi,
147 				mblk_t			**bpaddr);
148 
149 static void		usbms_rserv_vuid_event_y(
150 				queue_t			*q,
151 				struct usbmouseinfo	*mi,
152 				mblk_t			**bpaddr);
153 static void		usbms_rserv_vuid_event_x(
154 				queue_t			*q,
155 				struct usbmouseinfo	*mi,
156 				mblk_t			**bpaddr);
157 static void 		usbms_rserv_vuid_event_wheel(
158 				queue_t *,
159 				struct usbmouseinfo *,
160 				mblk_t **,
161 				ushort_t id);
162 static int		usbms_check_for_wheels(usbms_state_t *);
163 static int		usbms_make_copyreq(
164 				mblk_t 	*,
165 				uint_t 	pvtsize,
166 				uint_t	state,
167 				uint_t	reqsize,
168 				uint_t	contsize,
169 				uint_t	copytype);
170 static int		usbms_service_wheel_info(
171 				queue_t	*,
172 				mblk_t	*);
173 static int		usbms_service_wheel_state(
174 				queue_t	*,
175 				mblk_t	*,
176 				uint_t	cmd);
177 static void		usbms_ack_ioctl(mblk_t	*);
178 static int		usbms_read_input_data_format(usbms_state_t *);
179 static mblk_t		*usbms_setup_abs_mouse_event();
180 static int		usbms_get_coordinate(
181 				uint_t			pos,
182 				uint_t			len,
183 				mblk_t			*mp);
184 extern void		uniqtime32();
185 
186 /*
187  * Device driver qinit functions
188  */
189 static struct module_info usbms_mod_info = {
190 	0x0ffff,		/* module id number */
191 	"usbms",		/* module name */
192 	0,			/* min packet size accepted */
193 	INFPSZ,			/* max packet size accepted */
194 	512,			/* hi-water mark */
195 	128			/* lo-water mark */
196 };
197 
198 /* read side queue information structure */
199 static struct qinit rinit = {
200 	(int (*)())usbms_rput,	/* put procedure not needed */
201 	(int (*)())usbms_rserv, /* service procedure */
202 	usbms_open,		/* called on startup */
203 	usbms_close,		/* called on finish */
204 	NULL,			/* for future use */
205 	&usbms_mod_info,	/* module information structure */
206 	NULL			/* module statistics structure */
207 };
208 
209 /* write side queue information structure */
210 static struct qinit winit = {
211 	usbms_wput,		/* put procedure */
212 	NULL,			/* no service proecedure needed */
213 	NULL,			/* open not used on write side */
214 	NULL,			/* close not used on write side */
215 	NULL,			/* for future use */
216 	&usbms_mod_info,	/* module information structure */
217 	NULL			/* module statistics structure */
218 };
219 
220 static struct streamtab usbms_streamtab = {
221 	&rinit,
222 	&winit,
223 	NULL,			/* not a MUX */
224 	NULL			/* not a MUX */
225 };
226 
227 /*
228  * Message when overrun circular buffer
229  */
230 static int			overrun_msg;
231 
232 /* Increment when overrun circular buffer */
233 static int			overrun_cnt;
234 
235 extern int			hz;
236 
237 /*
238  * Mouse buffer size in bytes.	Place here as variable so that one could
239  * massage it using adb if it turns out to be too small.
240  */
241 static uint16_t			usbms_buf_bytes = USBMS_BUF_BYTES;
242 
243 
244 /*
245  * Regular STREAMS Entry points
246  */
247 
248 /*
249  * usbms_open() :
250  *	open() entry point for the USB mouse module.
251  */
252 /*ARGSUSED*/
253 static int
254 usbms_open(queue_t			*q,
255 	dev_t				*devp,
256 	int				flag,
257 	int				sflag,
258 	cred_t				*credp)
259 
260 {
261 	register struct usbmousebuf	*mousebufp;
262 	register struct ms_softc	*msd_soft;
263 	usbms_state_t			*usbmsp;
264 	struct iocblk			mctlmsg;
265 	mblk_t				*mctl_ptr;
266 
267 
268 	/* Clone opens are not allowed */
269 	if (sflag != MODOPEN)
270 		return (EINVAL);
271 
272 	/* If the module is already open, just return */
273 	if (q->q_ptr) {
274 		return (0);
275 	}
276 
277 	/* allocate usbms state structure */
278 	usbmsp = kmem_zalloc(sizeof (usbms_state_t), KM_SLEEP);
279 
280 	q->q_ptr = usbmsp;
281 	WR(q)->q_ptr = usbmsp;
282 
283 	usbmsp->usbms_rq_ptr = q;
284 	usbmsp->usbms_wq_ptr = WR(q);
285 
286 	qprocson(q);
287 
288 	/*
289 	 * Set up private data.
290 	 */
291 	usbmsp->usbms_state = USBMS_WAIT_BUTN;
292 	usbmsp->usbms_iocpending = NULL;
293 	usbmsp->usbms_jitter_thresh = USBMS_JITTER_THRESH;
294 	usbmsp->usbms_speedlimit = USBMS_SPEEDLIMIT;
295 	usbmsp->usbms_speedlaw = USBMS_SPEEDLAW;
296 	usbmsp->usbms_speed_count = USBMS_SPEED_COUNT;
297 
298 	msd_soft = &usbmsp->usbms_softc;
299 
300 	/*
301 	 * Initially set the format to MS_VUID_FORMAT
302 	 */
303 	msd_soft->ms_readformat = MS_VUID_FORMAT;
304 
305 	/*
306 	 * Allocate buffer and initialize data.
307 	 */
308 	msd_soft->ms_bufbytes = usbms_buf_bytes;
309 	mousebufp = kmem_zalloc((uint_t)msd_soft->ms_bufbytes,
310 				KM_SLEEP);
311 
312 	/* Truncation  will happen */
313 	mousebufp->mb_size = (uint16_t)((msd_soft->ms_bufbytes -
314 				    sizeof (struct usbmousebuf)) /
315 				    sizeof (struct usbmouseinfo));
316 	mousebufp->mb_info = (struct usbmouseinfo *)((char *)mousebufp +
317 				    sizeof (struct usbmousebuf));
318 	usbmsp->usbms_buf = mousebufp;
319 	msd_soft->ms_vuidaddr = VKEY_FIRST;
320 	usbmsp->usbms_jittertimeout = JITTER_TIMEOUT;
321 
322 	/* request hid report descriptor from HID */
323 	mctlmsg.ioc_cmd = HID_GET_PARSER_HANDLE;
324 	mctlmsg.ioc_count = 0;
325 
326 	mctl_ptr = usba_mk_mctl(mctlmsg, NULL, 0);
327 	if (mctl_ptr == NULL) {
328 		qprocsoff(q);
329 		kmem_free(usbmsp->usbms_buf, msd_soft->ms_bufbytes);
330 		kmem_free(usbmsp, sizeof (usbms_state_t));
331 
332 		return (ENOMEM);
333 	}
334 
335 	usbmsp->usbms_flags |= USBMS_QWAIT;
336 	putnext(usbmsp->usbms_wq_ptr, mctl_ptr);
337 
338 	/*
339 	 * Now that signal has been sent, wait for report descriptor.  Cleanup
340 	 * if user signals in the mean time (as when this gets opened in an
341 	 * inappropriate context and the user types a ^C).
342 	 */
343 	while (usbmsp->usbms_flags & USBMS_QWAIT) {
344 
345 		if (qwait_sig(q) == 0) {
346 			qprocsoff(q);
347 			kmem_free(usbmsp->usbms_buf, msd_soft->ms_bufbytes);
348 			kmem_free(usbmsp, sizeof (usbms_state_t));
349 
350 			return (EINTR);
351 		}
352 	}
353 
354 	if (usbmsp->usbms_report_descr_handle != NULL) {
355 		if (hidparser_get_usage_attribute(
356 				usbmsp->usbms_report_descr_handle,
357 				0,
358 				HIDPARSER_ITEM_INPUT,
359 				USBMS_USAGE_PAGE_BUTTON,
360 				0,
361 				HIDPARSER_ITEM_REPORT_COUNT,
362 				(int32_t *)&usbmsp->usbms_num_buttons) ==
363 				HIDPARSER_SUCCESS) {
364 			if (usbmsp->usbms_num_buttons > USB_MS_MAX_BUTTON_NO)
365 				usbmsp->usbms_num_buttons =
366 					USB_MS_MAX_BUTTON_NO;
367 			USB_DPRINTF_L2(PRINT_MASK_ALL,
368 				usbms_log_handle, "Num of buttons is : %d",
369 				usbmsp->usbms_num_buttons);
370 		} else {
371 			USB_DPRINTF_L3(PRINT_MASK_OPEN,
372 				usbms_log_handle,
373 				"hidparser_get_usage_attribute failed : "
374 				"Set to default number of buttons(3).");
375 
376 			usbmsp->usbms_num_buttons = USB_MS_DEFAULT_BUTTON_NO;
377 		}
378 	} else {
379 		USB_DPRINTF_L1(PRINT_MASK_ALL,
380 			usbms_log_handle, "Invalid HID "
381 			"Descriptor Tree. Set to default value(3 buttons).");
382 		usbmsp->usbms_num_buttons = USB_MS_DEFAULT_BUTTON_NO;
383 	}
384 
385 	/* check if this mouse has wheel */
386 	if (usbms_check_for_wheels(usbmsp) == USB_FAILURE) {
387 		USB_DPRINTF_L2(PRINT_MASK_ALL, usbms_log_handle,
388 		    "No wheels detected");
389 	} else {
390 		USB_DPRINTF_L2(PRINT_MASK_ALL, usbms_log_handle,
391 		    "Wheel detected");
392 	}
393 
394 	usbms_flush(usbmsp);
395 
396 	/* get the data format from the hid descriptor */
397 	if (usbms_read_input_data_format(usbmsp) != USB_SUCCESS) {
398 
399 		qprocsoff(q);
400 		kmem_free(usbmsp->usbms_buf, msd_soft->ms_bufbytes);
401 		kmem_free(usbmsp, sizeof (usbms_state_t));
402 
403 		return (EINVAL);
404 	}
405 
406 	usbmsp->usbms_flags |= USBMS_OPEN;
407 
408 	USB_DPRINTF_L3(PRINT_MASK_OPEN, usbms_log_handle,
409 	    "usbms_open exiting");
410 
411 	return (0);
412 }
413 
414 
415 /*
416  * usbms_close() :
417  *	close() entry point for the USB mouse module.
418  */
419 /*ARGSUSED*/
420 static int
421 usbms_close(queue_t			*q,
422 	int 				flag,
423 	cred_t 				*credp)
424 {
425 	usbms_state_t			*usbmsp = q->q_ptr;
426 	register struct	ms_softc	*ms = &usbmsp->usbms_softc;
427 
428 	USB_DPRINTF_L3(PRINT_MASK_CLOSE, usbms_log_handle,
429 		"usbms_close entering");
430 
431 	qprocsoff(q);
432 
433 	if (usbmsp->usbms_jitter) {
434 		(void) quntimeout(q,
435 			(timeout_id_t)(long)usbmsp->usbms_timeout_id);
436 		usbmsp->usbms_jitter = 0;
437 	}
438 	if (usbmsp->usbms_reioctl_id) {
439 		qunbufcall(q, (bufcall_id_t)(long)usbmsp->usbms_reioctl_id);
440 		usbmsp->usbms_reioctl_id = 0;
441 	}
442 	if (usbmsp->usbms_resched_id) {
443 		qunbufcall(q, (bufcall_id_t)usbmsp->usbms_resched_id);
444 		usbmsp->usbms_resched_id = 0;
445 	}
446 	if (usbmsp->usbms_iocpending != NULL) {
447 		/*
448 		 * We were holding an "ioctl" response pending the
449 		 * availability of an "mblk" to hold data to be passed up;
450 		 * another "ioctl" came through, which means that "ioctl"
451 		 * must have timed out or been aborted.
452 		 */
453 		freemsg(usbmsp->usbms_iocpending);
454 		usbmsp->usbms_iocpending = NULL;
455 	}
456 
457 
458 	/* Free mouse buffer */
459 	if (usbmsp->usbms_buf != NULL) {
460 		kmem_free(usbmsp->usbms_buf, ms->ms_bufbytes);
461 	}
462 
463 	kmem_free(usbmsp, sizeof (usbms_state_t));
464 
465 	q->q_ptr = NULL;
466 	WR(q)->q_ptr = NULL;
467 
468 
469 	USB_DPRINTF_L3(PRINT_MASK_CLOSE, usbms_log_handle,
470 		"usbms_close exiting");
471 
472 	return (0);
473 }
474 
475 
476 /*
477  * usbms_rserv() :
478  *	Read queue service routine.
479  *	Turn buffered mouse events into stream messages.
480  */
481 static void
482 usbms_rserv(queue_t		*q)
483 {
484 	usbms_state_t		*usbmsp = q->q_ptr;
485 	struct ms_softc		*ms;
486 	struct usbmousebuf	*b;
487 	struct usbmouseinfo	*mi;
488 	mblk_t			*bp;
489 	ushort_t 		i, loop;
490 	uchar_t			nbutt = (uchar_t)usbmsp->usbms_num_buttons;
491 
492 	ms = &usbmsp->usbms_softc;
493 	b = usbmsp->usbms_buf;
494 
495 	USB_DPRINTF_L3(PRINT_MASK_SERV, usbms_log_handle,
496 		"usbms_rserv entering");
497 
498 	while (canputnext(q) && ms->ms_oldoff != b->mb_off) {
499 		mi = &b->mb_info[ms->ms_oldoff];
500 		switch (ms->ms_readformat) {
501 
502 		case MS_3BYTE_FORMAT: {
503 			register char	*cp;
504 
505 			if ((usbmsp->usbms_idf).xlen != 1) {
506 				USB_DPRINTF_L3(PRINT_MASK_SERV,
507 				    usbms_log_handle,
508 				    "Can't set to 3 byte format. Length != 1");
509 
510 				return;
511 			}
512 			if ((bp = allocb(3, BPRI_HI)) != NULL) {
513 				cp = (char *)bp->b_wptr;
514 
515 				*cp++ = 0x80 | (mi->mi_buttons & 0xFF);
516 				/* Update read buttons */
517 				ms->ms_prevbuttons = mi->mi_buttons;
518 
519 				*cp++ = (mi->mi_x & 0xFF);
520 				*cp++ = ((-mi->mi_y) & 0xFF);
521 				/* lower pri to avoid mouse droppings */
522 				bp->b_wptr = (uchar_t *)cp;
523 				putnext(q, bp);
524 			} else {
525 				if (usbmsp->usbms_resched_id) {
526 					qunbufcall(q,
527 					(bufcall_id_t)usbmsp->usbms_resched_id);
528 				}
529 				usbmsp->usbms_resched_id = qbufcall(q,
530 							(size_t)3,
531 							(uint_t)BPRI_HI,
532 						(void (*)())usbms_resched,
533 							(void *) usbmsp);
534 				if (usbmsp->usbms_resched_id == 0)
535 
536 					return;	/* try again later */
537 				/* bufcall failed; just pitch this event */
538 				/* or maybe flush queue? */
539 			}
540 			ms->ms_oldoff++;	/* next event */
541 
542 			/* circular buffer wraparound */
543 			if (ms->ms_oldoff >= b->mb_size) {
544 				ms->ms_oldoff = 0;
545 			}
546 			break;
547 		}
548 
549 		case MS_VUID_FORMAT:
550 		default: {
551 
552 			do {
553 				bp = NULL;
554 
555 				switch (ms->ms_eventstate) {
556 
557 				case EVENT_WHEEL:
558 					loop = (usbmsp->usbms_num_wheels ?
559 						1 : 0);
560 
561 					if (usbmsp->usbms_num_wheels) {
562 						for (i = 0; i < loop; i++) {
563 						    usbms_rserv_vuid_event_wheel
564 							    (q, mi, &bp, i);
565 						}
566 					}
567 
568 					break;
569 				case EVENT_BUT8:
570 				case EVENT_BUT7:
571 				case EVENT_BUT6:
572 				case EVENT_BUT5:
573 				case EVENT_BUT4:
574 				case EVENT_BUT3:  /* Send right button */
575 				case EVENT_BUT2:  /* Send middle button */
576 				case EVENT_BUT1:  /* Send left button */
577 					usbms_rserv_vuid_button(q, mi, &bp);
578 
579 					break;
580 				case EVENT_Y:
581 					usbms_rserv_vuid_event_y(q, mi, &bp);
582 
583 					break;
584 				case EVENT_X:
585 					usbms_rserv_vuid_event_x(q, mi, &bp);
586 
587 					break;
588 				default:
589 					/* start again */
590 					ms->ms_eventstate = EVENT_WHEEL;
591 
592 					break;
593 				}
594 				if (bp != NULL) {
595 					/* lower pri to avoid mouse droppings */
596 					bp->b_wptr += sizeof (Firm_event);
597 					putnext(q, bp);
598 				}
599 				if (ms->ms_eventstate == EVENT_X) {
600 					ms->ms_eventstate = EVENT_WHEEL;
601 				} else if (ms->ms_eventstate == EVENT_WHEEL) {
602 					ms->ms_oldoff++;	/* next event */
603 					/* circular buffer wraparound */
604 					if (ms->ms_oldoff >= b->mb_size) {
605 						ms->ms_oldoff = 0;
606 					}
607 					ms->ms_eventstate = EVENT_BUT(nbutt);
608 				} else
609 					ms->ms_eventstate--;
610 			} while (ms->ms_eventstate != EVENT_BUT(nbutt));
611 		}
612 		}
613 	}
614 	USB_DPRINTF_L3(PRINT_MASK_SERV, usbms_log_handle,
615 		"usbms_rserv exiting");
616 }
617 
618 
619 /*
620  * usbms_rserv_vuid_event_wheel
621  *	convert wheel data to firm events
622  */
623 static void
624 usbms_rserv_vuid_event_wheel(queue_t		*q,
625 			struct usbmouseinfo	*mi,
626 			mblk_t			**bpaddr,
627 			ushort_t 		id)
628 {
629 	Firm_event *fep;
630 	mblk_t *tmp;
631 	struct ms_softc *ms;
632 	usbms_state_t *usbmsp = (usbms_state_t *)q->q_ptr;
633 
634 	if (!(usbmsp->usbms_wheel_state_bf & (1 << id))) {
635 
636 		return;
637 	}
638 	ms = &usbmsp->usbms_softc;
639 	if (mi->mi_z) {
640 		if ((tmp = allocb(sizeof (Firm_event), BPRI_HI)) != NULL) {
641 			fep = (Firm_event *)tmp->b_wptr;
642 			fep->id = vuid_id_addr(vuid_first(VUID_WHEEL)) |
643 			    vuid_id_offset(id);
644 			fep->pair_type = FE_PAIR_NONE;
645 			fep->pair = NULL;
646 			fep->value = mi->mi_z;
647 			fep->time = mi->mi_time;
648 			*bpaddr = tmp;
649 		} else {
650 			if (usbmsp->usbms_resched_id) {
651 				qunbufcall(q,
652 				(bufcall_id_t)usbmsp->usbms_resched_id);
653 			}
654 			usbmsp->usbms_resched_id =
655 			    qbufcall(q, sizeof (Firm_event), BPRI_HI,
656 			    (void (*)())usbms_resched, (void *) usbmsp);
657 			if (usbmsp->usbms_resched_id == 0) {
658 				/* try again later */
659 
660 				return;
661 			}
662 
663 			/* flush the queue */
664 			ms->ms_eventstate = EVENT_WHEEL;
665 		}
666 	}
667 }
668 
669 
670 /*
671  * usbms_rserv_vuid_button() :
672  *	Process a VUID button event
673  */
674 static void
675 usbms_rserv_vuid_button(queue_t			*q,
676 			struct usbmouseinfo	*mi,
677 			mblk_t			**bpaddr)
678 {
679 	usbms_state_t		*usbmsp = q->q_ptr;
680 	struct ms_softc	*ms;
681 	int			button_number;
682 	uchar_t			hwbit = 0x0;
683 	Firm_event		*fep;
684 	mblk_t			*bp;
685 	uchar_t			nbutt;
686 
687 	ms = &usbmsp->usbms_softc;
688 
689 	/* Test button. Send an event if it changed. */
690 	nbutt = (uchar_t)usbmsp->usbms_num_buttons;
691 	button_number = nbutt - (EVENT_BUT(nbutt) - ms->ms_eventstate) - 1;
692 	switch (button_number) {
693 	case	2:
694 		/* Right button */
695 		hwbit = 0x01;
696 
697 		break;
698 	case	1:
699 		/*
700 		 * On two-button mice, the second button is the "right"
701 		 * button.  There is no "middle".  The vuidps2.c file has
702 		 * a bmap[] array in sendButtonEvent().  We do something
703 		 * equivalent here ONLY in the case of two-button mice.
704 		 */
705 		if (nbutt == 2) {
706 			hwbit = 0x01;
707 			/*
708 			 * Trick the vuid message into thinking it's a
709 			 * right-button click also.
710 			 */
711 			button_number = 2;
712 		} else {
713 			/* ... otherwise, it's just the middle button */
714 			hwbit = 0x02;
715 		}
716 		break;
717 	case	0:
718 		/* Left button */
719 		hwbit = 0x04;
720 
721 		break;
722 	default	:
723 		/* Any other button */
724 		hwbit = USBMS_BUT(nbutt) >> (EVENT_BUT(nbutt) -
725 		    ms->ms_eventstate);
726 
727 		break;
728 	}
729 
730 	if ((ms->ms_prevbuttons & hwbit) !=
731 			(mi->mi_buttons & hwbit)) {
732 		if ((bp = allocb(sizeof (Firm_event),
733 			BPRI_HI)) != NULL) {
734 			*bpaddr = bp;
735 			fep = (Firm_event *)bp->b_wptr;
736 			fep->id = vuid_id_addr(
737 				ms->ms_vuidaddr) |
738 				vuid_id_offset(BUT(1)
739 				+ button_number);
740 			fep->pair_type = FE_PAIR_NONE;
741 			fep->pair = 0;
742 
743 			/*
744 			 * Update read buttons and set
745 			 * value
746 			 */
747 			if (mi->mi_buttons & hwbit) {
748 				fep->value = 0;
749 				ms->ms_prevbuttons |=
750 					hwbit;
751 			} else {
752 				fep->value = 1;
753 				ms->ms_prevbuttons &=
754 						~hwbit;
755 			}
756 			fep->time = mi->mi_time;
757 		} else {
758 			if (usbmsp->usbms_resched_id) {
759 				qunbufcall(q,
760 				(bufcall_id_t)usbmsp->usbms_resched_id);
761 			}
762 			usbmsp->usbms_resched_id =
763 				qbufcall(q,
764 				sizeof (Firm_event),
765 				BPRI_HI,
766 			(void (*)())usbms_resched,
767 				(void *) usbmsp);
768 			if (usbmsp->usbms_resched_id == 0)
769 				/* try again later */
770 				return;
771 			/*
772 			 * bufcall failed; just pitch
773 			 * this event
774 			 */
775 			/* or maybe flush queue? */
776 			ms->ms_eventstate = EVENT_WHEEL;
777 		}
778 	}
779 }
780 
781 /*
782  * usbms_rserv_vuid_event_y() :
783  *	Process a VUID y-event
784  */
785 static void
786 usbms_rserv_vuid_event_y(register queue_t		*q,
787 			register struct usbmouseinfo	*mi,
788 			mblk_t				**bpaddr)
789 {
790 	usbms_state_t			*usbmsp = q->q_ptr;
791 	register struct ms_softc	*ms;
792 	register Firm_event		*fep;
793 	mblk_t				*bp;
794 
795 	ms = &usbmsp->usbms_softc;
796 
797 	/*
798 	 * The (max, 0) message and (0, max) message are always sent before
799 	 * the button click message is sent on the IBM Bladecenter. Stop
800 	 * their sending may prevent the coordinate from moving to the
801 	 * (max, max).
802 	 */
803 	if (!(((usbmsp->usbms_idf).yattr) & HID_MAIN_ITEM_RELATIVE)) {
804 		if ((mi->mi_x == 0) &&
805 		    (mi->mi_y == usbmsp->usbms_logical_Ymax)) {
806 
807 			return;
808 		}
809 	}
810 
811 	/* Send y if changed. */
812 	if (mi->mi_y != 0) {
813 		if ((bp = allocb(sizeof (Firm_event),
814 			BPRI_HI)) != NULL) {
815 			*bpaddr = bp;
816 			fep = (Firm_event *)bp->b_wptr;
817 			if (((usbmsp->usbms_idf).yattr) &
818 			    HID_MAIN_ITEM_RELATIVE) {
819 				fep->id = vuid_id_addr(
820 					ms->ms_vuidaddr) |
821 					vuid_id_offset(
822 						LOC_Y_DELTA);
823 				fep->pair_type =
824 					FE_PAIR_ABSOLUTE;
825 				fep->pair =
826 					(uchar_t)LOC_Y_ABSOLUTE;
827 				fep->value = -(mi->mi_y);
828 			} else {
829 				fep->id = vuid_id_addr(
830 					ms->ms_vuidaddr) |
831 					vuid_id_offset(
832 						LOC_Y_ABSOLUTE);
833 				fep->pair_type = FE_PAIR_DELTA;
834 				fep->pair = (uchar_t)LOC_Y_DELTA;
835 				fep->value = (mi->mi_y *
836 				    ((usbmsp->usbms_resolution).height) /
837 				    usbmsp->usbms_logical_Ymax);
838 				if ((mi->mi_y *
839 				    ((usbmsp->usbms_resolution).height) %
840 				    usbmsp->usbms_logical_Ymax) >=
841 				    (usbmsp->usbms_logical_Ymax / 2)) {
842 					fep->value ++;
843 				}
844 			}
845 			fep->time = mi->mi_time;
846 		} else {
847 			if (usbmsp->usbms_resched_id) {
848 				qunbufcall(q,
849 		(bufcall_id_t)usbmsp->usbms_resched_id);
850 			}
851 			usbmsp->usbms_resched_id =
852 				qbufcall(q,
853 				sizeof (Firm_event),
854 				BPRI_HI,
855 			(void (*)())usbms_resched,
856 				(void *)usbmsp);
857 			if (usbmsp->usbms_resched_id == 0) {
858 				/* try again later */
859 				return;
860 			}
861 
862 			/*
863 			 * bufcall failed; just pitch
864 			 * this event
865 			 */
866 			/* or maybe flush queue? */
867 			ms->ms_eventstate = EVENT_WHEEL;
868 		}
869 	}
870 }
871 
872 /*
873  * usbms_rserv_vuid_event_x() :
874  *	Process a VUID x-event
875  */
876 static void
877 usbms_rserv_vuid_event_x(register queue_t		*q,
878 			register struct usbmouseinfo	*mi,
879 			mblk_t				**bpaddr)
880 {
881 	usbms_state_t			*usbmsp = q->q_ptr;
882 	register struct ms_softc	*ms;
883 	register Firm_event		*fep;
884 	mblk_t				*bp;
885 
886 	ms = &usbmsp->usbms_softc;
887 
888 	/*
889 	 * The (max, 0) message and (0, max) message are always sent before
890 	 * the button click message is sent on the IBM Bladecenter. Stop
891 	 * their sending may prevent the coordinate from moving to the
892 	 * (max, max).
893 	 */
894 	if (!(((usbmsp->usbms_idf).xattr) & HID_MAIN_ITEM_RELATIVE)) {
895 		if ((mi->mi_y == 0) &&
896 		    (mi->mi_x == usbmsp->usbms_logical_Xmax)) {
897 
898 		return;
899 		}
900 	}
901 
902 	/* Send x if changed. */
903 	if (mi->mi_x != 0) {
904 		if ((bp = allocb(sizeof (Firm_event),
905 			BPRI_HI)) != NULL) {
906 			*bpaddr = bp;
907 			fep = (Firm_event *)bp->b_wptr;
908 			if (((usbmsp->usbms_idf).xattr) &
909 			    HID_MAIN_ITEM_RELATIVE) {
910 				fep->id = vuid_id_addr(
911 					ms->ms_vuidaddr) |
912 				vuid_id_offset(LOC_X_DELTA);
913 				fep->pair_type =
914 					FE_PAIR_ABSOLUTE;
915 				fep->pair =
916 					(uchar_t)LOC_X_ABSOLUTE;
917 				fep->value = mi->mi_x;
918 			} else {
919 				fep->id = vuid_id_addr(ms->ms_vuidaddr) |
920 					    vuid_id_offset(LOC_X_ABSOLUTE);
921 				fep->pair_type = FE_PAIR_DELTA;
922 				fep->pair = (uchar_t)LOC_X_DELTA;
923 				fep->value = (mi->mi_x *
924 				    ((usbmsp->usbms_resolution).width) /
925 				    usbmsp->usbms_logical_Xmax);
926 				if ((mi->mi_x *
927 				    ((usbmsp->usbms_resolution).width) %
928 				    usbmsp->usbms_logical_Xmax) >=
929 				    (usbmsp->usbms_logical_Xmax / 2)) {
930 					fep->value ++;
931 				}
932 			}
933 			fep->time = mi->mi_time;
934 		} else {
935 			if (usbmsp->usbms_resched_id)
936 				qunbufcall(q,
937 	(bufcall_id_t)usbmsp->usbms_resched_id);
938 			usbmsp->usbms_resched_id =
939 				qbufcall(q,
940 				sizeof (Firm_event),
941 				BPRI_HI,
942 		(void (*)())usbms_resched,
943 				(void *) usbmsp);
944 			if (usbmsp->usbms_resched_id == 0)
945 				/* try again later */
946 				return;
947 
948 			/*
949 			 * bufcall failed; just
950 			 * pitch this event
951 			 */
952 			/* or maybe flush queue? */
953 			ms->ms_eventstate = EVENT_WHEEL;
954 		}
955 	}
956 }
957 
958 /*
959  * usbms_resched() :
960  *	Callback routine for the qbufcall() in case
961  *	of allocb() failure. When buffer becomes
962  *	available, this function is called and
963  *	enables the queue.
964  */
965 static void
966 usbms_resched(void 	* usbmsp)
967 {
968 	register queue_t	*q;
969 	register usbms_state_t	*tmp_usbmsp = (usbms_state_t *)usbmsp;
970 
971 	tmp_usbmsp->usbms_resched_id = 0;
972 	if ((q = tmp_usbmsp->usbms_rq_ptr) != 0)
973 		qenable(q);	/* run the service procedure */
974 }
975 
976 /*
977  * usbms_wput() :
978  *	wput() routine for the mouse module.
979  *	Module below : hid, module above : consms
980  */
981 static int
982 usbms_wput(queue_t		*q,
983 	mblk_t			*mp)
984 {
985 	USB_DPRINTF_L3(PRINT_MASK_ALL, usbms_log_handle,
986 			"usbms_wput entering");
987 	switch (mp->b_datap->db_type) {
988 
989 	case M_FLUSH:  /* Canonical flush handling */
990 		if (*mp->b_rptr & FLUSHW) {
991 			flushq(q, FLUSHDATA);
992 		}
993 
994 		if (*mp->b_rptr & FLUSHR) {
995 			flushq(RD(q), FLUSHDATA);
996 		}
997 
998 		putnext(q, mp); /* pass it down the line. */
999 		break;
1000 
1001 	case M_IOCTL:
1002 		usbms_ioctl(q, mp);
1003 		break;
1004 
1005 	case M_IOCDATA:
1006 		usbms_miocdata(q, mp);
1007 
1008 		break;
1009 	default:
1010 		putnext(q, mp); /* pass it down the line. */
1011 	}
1012 
1013 	USB_DPRINTF_L3(PRINT_MASK_ALL, usbms_log_handle,
1014 		"usbms_wput exiting");
1015 
1016 	return (0);
1017 }
1018 
1019 
1020 /*
1021  * usbms_ioctl() :
1022  *	Process ioctls we recognize and own.  Otherwise, NAK.
1023  */
1024 static void
1025 usbms_ioctl(register queue_t		*q,
1026 		register mblk_t		*mp)
1027 {
1028 	usbms_state_t *usbmsp = (usbms_state_t *)q->q_ptr;
1029 	register struct ms_softc 	*ms;
1030 	register struct iocblk 		*iocp;
1031 	Vuid_addr_probe			*addr_probe;
1032 	uint_t				ioctlrespsize;
1033 	int				err = 0;
1034 	mblk_t				*datap;
1035 	ushort_t			transparent = 0;
1036 	boolean_t			report_abs = B_FALSE;
1037 	mblk_t	*mb;
1038 
1039 	USB_DPRINTF_L3(PRINT_MASK_IOCTL, usbms_log_handle,
1040 		"usbms_ioctl entering");
1041 
1042 	if (usbmsp == NULL) {
1043 		miocnak(q, mp, 0, EINVAL);
1044 
1045 		return;
1046 	}
1047 	ms = &usbmsp->usbms_softc;
1048 
1049 	iocp = (struct iocblk *)mp->b_rptr;
1050 	switch (iocp->ioc_cmd) {
1051 
1052 	case VUIDSFORMAT:
1053 		err = miocpullup(mp, sizeof (int));
1054 		if (err != 0)
1055 			break;
1056 
1057 		if (*(int *)mp->b_cont->b_rptr == ms->ms_readformat) {
1058 			break;
1059 		}
1060 		ms->ms_readformat = *(int *)mp->b_cont->b_rptr;
1061 		/*
1062 		 * Flush mouse buffer because the messages upstream of us
1063 		 * are in the old format.
1064 		 */
1065 
1066 		usbms_flush(usbmsp);
1067 		break;
1068 
1069 	case VUIDGFORMAT:
1070 		if ((datap = allocb(sizeof (int), BPRI_HI)) == NULL) {
1071 			ioctlrespsize = sizeof (int);
1072 			goto allocfailure;
1073 		}
1074 		*(int *)datap->b_wptr = ms->ms_readformat;
1075 		datap->b_wptr += sizeof (int);
1076 		freemsg(mp->b_cont);
1077 		mp->b_cont = datap;
1078 		iocp->ioc_count = sizeof (int);
1079 		break;
1080 
1081 	case VUIDGADDR:
1082 	case VUIDSADDR:
1083 		err = miocpullup(mp, sizeof (Vuid_addr_probe));
1084 		if (err != 0)
1085 			break;
1086 
1087 		addr_probe = (Vuid_addr_probe *)mp->b_cont->b_rptr;
1088 		if (addr_probe->base != VKEY_FIRST) {
1089 			err = ENODEV;
1090 			break;
1091 		}
1092 		if (iocp->ioc_cmd == VUIDSADDR)
1093 			ms->ms_vuidaddr = addr_probe->data.next;
1094 		else
1095 			addr_probe->data.current = ms->ms_vuidaddr;
1096 		break;
1097 
1098 	case MSIOGETPARMS:
1099 		if ((datap = allocb(sizeof (Ms_parms), BPRI_HI)) == NULL) {
1100 			ioctlrespsize = sizeof (Ms_parms);
1101 			goto allocfailure;
1102 		}
1103 		err = usbms_getparms((Ms_parms *)datap->b_wptr, usbmsp);
1104 		datap->b_wptr += sizeof (Ms_parms);
1105 		freemsg(mp->b_cont);
1106 		mp->b_cont = datap;
1107 		iocp->ioc_count = sizeof (Ms_parms);
1108 		break;
1109 
1110 	case MSIOSETPARMS:
1111 		err = miocpullup(mp, sizeof (Ms_parms));
1112 		if (err != 0)
1113 			break;
1114 		err = usbms_setparms((Ms_parms *)mp->b_cont->b_rptr, usbmsp);
1115 		break;
1116 
1117 	case MSIOBUTTONS:
1118 		if ((datap = allocb(sizeof (int), BPRI_HI)) == NULL) {
1119 			ioctlrespsize = sizeof (int);
1120 			goto allocfailure;
1121 		}
1122 		*(int *)datap->b_wptr = (int)usbmsp->usbms_num_buttons;
1123 		datap->b_wptr += sizeof (int);
1124 		freemsg(mp->b_cont);
1125 		mp->b_cont = datap;
1126 		iocp->ioc_count = sizeof (int);
1127 
1128 		break;
1129 	case VUIDGWHEELCOUNT:
1130 		/*
1131 		 * New IOCTL support. Since it's explicitly mentioned that
1132 		 * you can't add more ioctls to stream head's hard coded
1133 		 * list, we have to do the transparent ioctl processing
1134 		 * which is heavy.
1135 		 */
1136 
1137 		/* Currently support for only one wheel */
1138 
1139 		if (iocp->ioc_count == TRANSPARENT) {
1140 			transparent = 1;
1141 			if (err = usbms_make_copyreq(mp, 0, 0, sizeof (int),
1142 			    0, M_COPYOUT)) {
1143 
1144 				break;
1145 			}
1146 		}
1147 		if ((datap = allocb(sizeof (int), BPRI_HI)) == NULL) {
1148 			ioctlrespsize = sizeof (int);
1149 
1150 			goto allocfailure;
1151 		}
1152 		*((int *)datap->b_wptr) = (usbmsp->usbms_num_wheels ? 1 : 0);
1153 		datap->b_wptr +=  sizeof (int);
1154 		if (mp->b_cont) {
1155 			freemsg(mp->b_cont);
1156 			mp->b_cont = NULL;
1157 		}
1158 		mp->b_cont = datap;
1159 		if (transparent) {
1160 			qreply(q, mp);
1161 
1162 			return;
1163 		}
1164 
1165 		break;
1166 	case VUIDGWHEELINFO:
1167 		if (iocp->ioc_count == TRANSPARENT) {
1168 			if (err = usbms_make_copyreq(mp,
1169 			    sizeof (usbms_iocstate_t),
1170 			    USBMS_GETSTRUCT,
1171 			    sizeof (wheel_info),
1172 			    0,
1173 			    M_COPYIN)) {
1174 
1175 				break;
1176 			}
1177 			/*
1178 			 * If there is no b_cont the earlier func. will fail.
1179 			 * Hence there is no need for an explicit check here.
1180 			 */
1181 			freemsg(mp->b_cont);
1182 			mp->b_cont = (mblk_t *)NULL;
1183 			qreply(q, mp);
1184 
1185 			return;
1186 		}
1187 		if (mp->b_cont == NULL || iocp->ioc_count !=
1188 					    sizeof (wheel_info)) {
1189 			err = EINVAL;
1190 			break;
1191 		}
1192 		datap = mp->b_cont;
1193 		err = usbms_service_wheel_info(q, datap);
1194 
1195 		break;
1196 	case VUIDGWHEELSTATE:
1197 		if (iocp->ioc_count == TRANSPARENT) {
1198 			if (err = usbms_make_copyreq(mp,
1199 			    sizeof (usbms_iocstate_t),
1200 			    USBMS_GETSTRUCT,
1201 			    sizeof (wheel_state),
1202 			    0,
1203 			    M_COPYIN)) {
1204 
1205 				break;
1206 			}
1207 			freemsg(mp->b_cont);
1208 			mp->b_cont = (mblk_t *)NULL;
1209 			qreply(q, mp);
1210 
1211 			return;
1212 		}
1213 		if ((mp->b_cont == NULL) ||
1214 		    (iocp->ioc_count != sizeof (wheel_state))) {
1215 			err = EINVAL;
1216 
1217 			break;
1218 		}
1219 		datap = mp->b_cont;
1220 		err = usbms_service_wheel_state(q, datap, VUIDGWHEELSTATE);
1221 
1222 		break;
1223 	case VUIDSWHEELSTATE:
1224 		if (iocp->ioc_count == TRANSPARENT) {
1225 			if (err = usbms_make_copyreq(mp,
1226 			    sizeof (usbms_iocstate_t),
1227 			    USBMS_GETSTRUCT,
1228 			    sizeof (wheel_state),
1229 			    0,
1230 			    M_COPYIN)) {
1231 
1232 				break;
1233 			}
1234 			freemsg(mp->b_cont);
1235 			mp->b_cont = (mblk_t *)NULL;
1236 			qreply(q, mp);
1237 
1238 			return;
1239 		}
1240 		if (mp->b_cont == NULL) {
1241 			err = EINVAL;
1242 
1243 			break;
1244 		}
1245 		datap = mp->b_cont;
1246 		err = usbms_service_wheel_state(q, datap, VUIDSWHEELSTATE);
1247 
1248 		break;
1249 	case MSIOSRESOLUTION:
1250 		if (iocp->ioc_count == TRANSPARENT) {
1251 			if (err = usbms_make_copyreq(mp,
1252 			    sizeof (usbms_iocstate_t),
1253 			    USBMS_GETSTRUCT,
1254 			    sizeof (Ms_screen_resolution),
1255 			    0,
1256 			    M_COPYIN)) {
1257 
1258 				break;
1259 			}
1260 
1261 			freemsg(mp->b_cont);
1262 			mp->b_cont = (mblk_t *)NULL;
1263 			qreply(q, mp);
1264 
1265 			return;
1266 		}
1267 		if (mp->b_cont == NULL) {
1268 			err = EINVAL;
1269 
1270 			break;
1271 		}
1272 		datap = mp->b_cont;
1273 		err = usbms_get_screen_parms(q, datap);
1274 		/*
1275 		 * Create the absolute mouse type event.
1276 		 * It is used for the hotplug absolute mouse.
1277 		 */
1278 		if ((!((usbmsp->usbms_idf).xattr & HID_MAIN_ITEM_RELATIVE)) &&
1279 		    (usbmsp->usbms_rpt_abs == B_FALSE)) {
1280 			report_abs = B_TRUE;
1281 		}
1282 
1283 		break;
1284 
1285 	default:
1286 		putnext(q, mp); /* pass it down the line */
1287 
1288 		return;
1289 	} /* switch */
1290 
1291 	if (err != 0)
1292 		miocnak(q, mp, 0, err);
1293 	else {
1294 		iocp->ioc_rval = 0;
1295 		iocp->ioc_error = 0;
1296 		mp->b_datap->db_type = M_IOCACK;
1297 		qreply(q, mp);
1298 
1299 		if (report_abs == B_TRUE) {
1300 			/* send the abs mouse type event to the upper level */
1301 			if ((mb = usbms_setup_abs_mouse_event()) != NULL) {
1302 				usbmsp->usbms_rpt_abs = B_TRUE;
1303 				qreply(q, mb);
1304 			}
1305 		}
1306 	}
1307 
1308 	return;
1309 
1310 allocfailure:
1311 	/*
1312 	 * We needed to allocate something to handle this "ioctl", but
1313 	 * couldn't; save this "ioctl" and arrange to get called back when
1314 	 * it's more likely that we can get what we need.
1315 	 * If there's already one being saved, throw it out, since it
1316 	 * must have timed out.
1317 	 */
1318 	freemsg(usbmsp->usbms_iocpending);
1319 	usbmsp->usbms_iocpending = mp;
1320 	if (usbmsp->usbms_reioctl_id) {
1321 		qunbufcall(q, (bufcall_id_t)usbmsp->usbms_reioctl_id);
1322 	}
1323 	usbmsp->usbms_reioctl_id = qbufcall(q, ioctlrespsize, BPRI_HI,
1324 					(void (*)())usbms_reioctl,
1325 					(void *)usbmsp);
1326 }
1327 
1328 
1329 /*
1330  * M_IOCDATA processing for IOCTL's: VUIDGWHEELCOUNT, VUIDGWHEELINFO,
1331  * VUIDGWHEELSTATE, VUIDSWHEELSTATE & MSIOSRESOLUTION.
1332  */
1333 static void
1334 usbms_miocdata(register 	queue_t *q,
1335 		register 	mblk_t  *mp)
1336 {
1337 	struct copyresp *copyresp;
1338 	struct iocblk *iocbp;
1339 	mblk_t *datap;
1340 	mblk_t *ioctmp;
1341 	usbms_iocstate_t *usbmsioc;
1342 	int err = 0;
1343 
1344 	copyresp = (struct copyresp *)mp->b_rptr;
1345 	iocbp = (struct iocblk *)mp->b_rptr;
1346 	if (copyresp->cp_rval) {
1347 		err = EAGAIN;
1348 
1349 		goto err;
1350 	}
1351 	switch (copyresp->cp_cmd) {
1352 
1353 	case VUIDGWHEELCOUNT:
1354 		usbms_ack_ioctl(mp);
1355 
1356 		break;
1357 	case VUIDGWHEELINFO:
1358 		ioctmp = copyresp->cp_private;
1359 		usbmsioc = (usbms_iocstate_t *)ioctmp->b_rptr;
1360 		if (usbmsioc->ioc_state == USBMS_GETSTRUCT) {
1361 			if (mp->b_cont == NULL) {
1362 				err = EINVAL;
1363 
1364 				break;
1365 			}
1366 			datap = (mblk_t *)mp->b_cont;
1367 			if (err = usbms_service_wheel_info(q, datap)) {
1368 
1369 				goto err;
1370 			}
1371 			if (err = usbms_make_copyreq(mp, 0, USBMS_GETRESULT,
1372 			    sizeof (wheel_info), 0, M_COPYOUT)) {
1373 
1374 				goto err;
1375 			}
1376 		} else if (usbmsioc->ioc_state == USBMS_GETRESULT) {
1377 			freemsg(ioctmp);
1378 			usbms_ack_ioctl(mp);
1379 		}
1380 
1381 		break;
1382 	case VUIDGWHEELSTATE:
1383 		ioctmp = (mblk_t *)copyresp->cp_private;
1384 		usbmsioc = (usbms_iocstate_t *)ioctmp->b_rptr;
1385 		if (usbmsioc->ioc_state == USBMS_GETSTRUCT) {
1386 			if (mp->b_cont == NULL) {
1387 				err = EINVAL;
1388 
1389 				break;
1390 			}
1391 			if (err = usbms_service_wheel_state(q, mp->b_cont,
1392 			    VUIDGWHEELSTATE)) {
1393 				goto err;
1394 			}
1395 			if (err = usbms_make_copyreq(mp, 0, USBMS_GETRESULT,
1396 			    sizeof (wheel_state), 0, M_COPYOUT)) {
1397 
1398 				goto err;
1399 			}
1400 		} else if (usbmsioc->ioc_state == USBMS_GETRESULT) {
1401 			freemsg(ioctmp);
1402 			usbms_ack_ioctl(mp);
1403 		}
1404 
1405 		break;
1406 	case VUIDSWHEELSTATE:
1407 		ioctmp = (mblk_t *)copyresp->cp_private;
1408 		usbmsioc = (usbms_iocstate_t *)ioctmp->b_rptr;
1409 		if (mp->b_cont == NULL) {
1410 			err = EINVAL;
1411 
1412 			break;
1413 		}
1414 		if (err = usbms_service_wheel_state(q, mp->b_cont,
1415 		    VUIDSWHEELSTATE)) {
1416 
1417 			goto err;
1418 		}
1419 		freemsg(ioctmp);
1420 		usbms_ack_ioctl(mp);
1421 
1422 		break;
1423 	case MSIOSRESOLUTION:
1424 		ioctmp = (mblk_t *)copyresp->cp_private;
1425 		usbmsioc = (usbms_iocstate_t *)ioctmp->b_rptr;
1426 		if (mp->b_cont == NULL) {
1427 			err = EINVAL;
1428 
1429 			break;
1430 		}
1431 		if (err = usbms_get_screen_parms(q, mp->b_cont)) {
1432 
1433 			goto err;
1434 		}
1435 		freemsg(ioctmp);
1436 		usbms_ack_ioctl(mp);
1437 
1438 		break;
1439 	default:
1440 		err = EINVAL;
1441 		break;
1442 	}
1443 
1444 err:
1445 	if (err) {
1446 		mp->b_datap->db_type = M_IOCNAK;
1447 		if (mp->b_cont) {
1448 			freemsg(mp->b_cont);
1449 			mp->b_cont = (mblk_t *)NULL;
1450 		}
1451 		if (copyresp->cp_private) {
1452 			freemsg((mblk_t *)copyresp->cp_private);
1453 			copyresp->cp_private = (mblk_t *)NULL;
1454 		}
1455 		iocbp->ioc_count = 0;
1456 		iocbp->ioc_error = err;
1457 	}
1458 	qreply(q, mp);
1459 }
1460 
1461 
1462 /*
1463  * usbms_reioctl() :
1464  *	This function is set up as call-back function should an ioctl fail.
1465  *	It retries the ioctl.
1466  */
1467 static void
1468 usbms_reioctl(void	* usbms_addr)
1469 {
1470 	usbms_state_t *usbmsp = (usbms_state_t *)usbms_addr;
1471 	register queue_t 	*q;
1472 	register mblk_t 	*mp;
1473 
1474 	q = usbmsp->usbms_wq_ptr;
1475 	if ((mp = usbmsp->usbms_iocpending) != NULL) {
1476 		usbmsp->usbms_iocpending = NULL; /* not pending any more */
1477 		usbms_ioctl(q, mp);
1478 	}
1479 }
1480 
1481 /*
1482  * usbms_getparms() :
1483  *	Called from MSIOGETPARMS ioctl to get the
1484  *	current jitter_thesh, speed_law and speed_limit
1485  *	values.
1486  */
1487 static int
1488 usbms_getparms(register Ms_parms	*data,
1489 		usbms_state_t		*usbmsp)
1490 {
1491 	data->jitter_thresh = usbmsp->usbms_jitter_thresh;
1492 	data->speed_law = usbmsp->usbms_speedlaw;
1493 	data->speed_limit = usbmsp->usbms_speedlimit;
1494 
1495 	return (0);
1496 }
1497 
1498 
1499 /*
1500  * usbms_setparms() :
1501  *	Called from MSIOSETPARMS ioctl to set the
1502  *	current jitter_thesh, speed_law and speed_limit
1503  *	values.
1504  */
1505 static int
1506 usbms_setparms(register Ms_parms	*data,
1507 		usbms_state_t		*usbmsp)
1508 {
1509 	usbmsp->usbms_jitter_thresh = data->jitter_thresh;
1510 	usbmsp->usbms_speedlaw = data->speed_law;
1511 	usbmsp->usbms_speedlimit = data->speed_limit;
1512 
1513 	return (0);
1514 }
1515 
1516 /*
1517  * usbms_flush() :
1518  *	Resets the ms_softc structure to default values
1519  *	and sends M_FLUSH above.
1520  */
1521 static void
1522 usbms_flush(usbms_state_t		*usbmsp)
1523 {
1524 	register struct ms_softc *ms = &usbmsp->usbms_softc;
1525 	register queue_t		*q;
1526 
1527 	USB_DPRINTF_L3(PRINT_MASK_ALL, usbms_log_handle,
1528 		"usbms_flush entering");
1529 
1530 	ms->ms_oldoff = 0;
1531 	ms->ms_eventstate = EVENT_BUT(usbmsp->usbms_num_buttons);
1532 	usbmsp->usbms_buf->mb_off = 0;
1533 	ms->ms_prevbuttons = (char)USB_NO_BUT_PRESSED;
1534 	usbmsp->usbms_oldbutt = ms->ms_prevbuttons;
1535 	if ((q = usbmsp->usbms_rq_ptr) != NULL && q->q_next != NULL) {
1536 		(void) putnextctl1(q, M_FLUSH, FLUSHR);
1537 	}
1538 
1539 	USB_DPRINTF_L3(PRINT_MASK_ALL, usbms_log_handle,
1540 		"usbms_flush exiting");
1541 }
1542 
1543 
1544 /*
1545  * usbms_rput() :
1546  *	Put procedure for input from driver end of stream (read queue).
1547  */
1548 static void
1549 usbms_rput(queue_t		*q,
1550 		mblk_t		*mp)
1551 {
1552 	usbms_state_t *usbmsp = q->q_ptr;
1553 	mblk_t	*tmp_mp;
1554 	ushort_t limit = (usbmsp->usbms_idf).tlen;
1555 
1556 	/* Maintain the original mp */
1557 	tmp_mp = mp;
1558 
1559 	if (usbmsp == 0) {
1560 		freemsg(mp);	/* nobody's listening */
1561 
1562 		return;
1563 	}
1564 
1565 	switch (mp->b_datap->db_type) {
1566 
1567 	case M_FLUSH:
1568 		if (*mp->b_rptr & FLUSHW)
1569 			flushq(WR(q), FLUSHDATA);
1570 		if (*mp->b_rptr & FLUSHR)
1571 			flushq(q, FLUSHDATA);
1572 		freemsg(mp);
1573 
1574 		return;
1575 
1576 	case M_BREAK:
1577 		/*
1578 		 * We don't have to handle this
1579 		 * because nothing is sent from the downstream
1580 		 */
1581 
1582 		freemsg(mp);
1583 
1584 		return;
1585 
1586 	case M_DATA:
1587 		if (!(usbmsp->usbms_flags & USBMS_OPEN)) {
1588 			freemsg(mp);	/* not ready to listen */
1589 
1590 			return;
1591 		}
1592 		break;
1593 
1594 	case M_CTL:
1595 		usbms_mctl_receive(q, mp);
1596 
1597 		return;
1598 
1599 	case M_ERROR:
1600 		usbmsp->usbms_protoerr = 1;
1601 		usbmsp->usbms_flags &= ~USBMS_QWAIT;
1602 		freemsg(mp);
1603 
1604 		return;
1605 	default:
1606 		putnext(q, mp);
1607 
1608 		return;
1609 	}
1610 
1611 	/*
1612 	 * A data message, consisting of bytes from the mouse.
1613 	 * Make sure there are atleast "limit" number of bytes.
1614 	 */
1615 	if (((tmp_mp->b_wptr - tmp_mp->b_rptr) < limit) ||
1616 	    (((tmp_mp->b_wptr - tmp_mp->b_rptr) == limit) &&
1617 	    (usbmsp->usbms_rptid != HID_REPORT_ID_UNDEFINED))) {
1618 		freemsg(mp);
1619 		return;
1620 	}
1621 	do {
1622 		if (usbmsp->usbms_rptid != HID_REPORT_ID_UNDEFINED) {
1623 			if (*(tmp_mp->b_rptr) != usbmsp->usbms_rptid) {
1624 				freemsg(mp);
1625 
1626 				return;
1627 			} else {
1628 				/* We skip the report id prefix. */
1629 				tmp_mp->b_rptr++;
1630 			}
1631 		}
1632 
1633 		usbms_input(usbmsp, tmp_mp);
1634 	} while ((tmp_mp = tmp_mp->b_cont) != NULL);   /* next block, if any */
1635 
1636 	freemsg(mp);
1637 }
1638 
1639 
1640 /*
1641  * usbms_mctl_receive() :
1642  *	Handle M_CTL messages from hid.  If
1643  *	we don't understand the command, free message.
1644  */
1645 static void
1646 usbms_mctl_receive(register queue_t		*q,
1647 			register mblk_t		*mp)
1648 {
1649 	usbms_state_t *usbmsd = (usbms_state_t *)q->q_ptr;
1650 	struct iocblk				*iocp;
1651 	caddr_t					data;
1652 
1653 
1654 	iocp = (struct iocblk *)mp->b_rptr;
1655 	if (mp->b_cont != NULL)
1656 		data = (caddr_t)mp->b_cont->b_rptr;
1657 
1658 	switch (iocp->ioc_cmd) {
1659 
1660 	case HID_GET_PARSER_HANDLE:
1661 		if ((data != NULL) &&
1662 		    (iocp->ioc_count == sizeof (hidparser_handle_t)) &&
1663 		    ((mp->b_cont->b_wptr - mp->b_cont->b_rptr) ==
1664 		    iocp->ioc_count)) {
1665 			usbmsd->usbms_report_descr_handle =
1666 			    *(hidparser_handle_t *)data;
1667 		} else {
1668 			usbmsd->usbms_report_descr_handle = NULL;
1669 		}
1670 		freemsg(mp);
1671 		usbmsd->usbms_flags &= ~USBMS_QWAIT;
1672 		break;
1673 	case HID_SET_PROTOCOL:
1674 		usbmsd->usbms_flags &= ~USBMS_QWAIT;
1675 
1676 		/* FALLTHRU */
1677 	default:
1678 	    freemsg(mp);
1679 	    break;
1680 	}
1681 }
1682 
1683 
1684 /*
1685  * usbms_input() :
1686  *
1687  *	Mouse input routine; process a byte received from a mouse and
1688  *	assemble into a mouseinfo message for the window system.
1689  *
1690  *	The USB mouse send a three-byte packet organized as
1691  *		button, dx, dy
1692  *	where dx and dy can be any signed byte value. The mouseinfo message
1693  *	is organized as
1694  *		dx, dy, button, timestamp
1695  *	Our strategy is to collect but, dx & dy three-byte packet, then
1696  *	send the mouseinfo message up.
1697  *
1698  *	Basic algorithm: throw away bytes until we get a [potential]
1699  *	button byte. Collect button; Collect dx; Collect dy; Send button,
1700  *	dx, dy, timestamp.
1701  *
1702  *	Watch out for overflow!
1703  */
1704 static void
1705 usbms_input(usbms_state_t		*usbmsp,
1706 		mblk_t			*mp)
1707 {
1708 	register struct usbmousebuf	*b;
1709 	register struct usbmouseinfo	*mi;
1710 	register int			jitter_radius;
1711 	register int32_t		nbutt;
1712 	ushort_t			i;
1713 	char				c;
1714 
1715 	nbutt = usbmsp->usbms_num_buttons;
1716 	b = usbmsp->usbms_buf;
1717 
1718 	USB_DPRINTF_L3(PRINT_MASK_INPUT_INCR, usbms_log_handle,
1719 		"usbms_input entering");
1720 
1721 	if (b == NULL) {
1722 
1723 		return;
1724 	}
1725 
1726 	mi = &b->mb_info[b->mb_off];
1727 
1728 	/*
1729 	 * Lower 3 bits are middle, right, left.
1730 	 */
1731 	c = mp->b_rptr[(usbmsp->usbms_idf).bpos];
1732 	mi->mi_buttons = (char)USB_NO_BUT_PRESSED;
1733 	if (c & USBMS_BUT(1)) {	 /* left button is pressed */
1734 		mi->mi_buttons = mi->mi_buttons & USB_LEFT_BUT_PRESSED;
1735 		USB_DPRINTF_L3(PRINT_MASK_INPUT_INCR,
1736 			usbms_log_handle,
1737 			"left button pressed");
1738 	}
1739 	if (c & USBMS_BUT(2)) {	/* right button is pressed */
1740 		mi->mi_buttons = mi->mi_buttons & USB_RIGHT_BUT_PRESSED;
1741 		USB_DPRINTF_L3(PRINT_MASK_INPUT_INCR,
1742 			usbms_log_handle,
1743 			"right button pressed");
1744 	}
1745 	if (c & USBMS_BUT(3)) {   /* middle button is pressed */
1746 		mi->mi_buttons = mi->mi_buttons &
1747 					USB_MIDDLE_BUT_PRESSED;
1748 		USB_DPRINTF_L3(PRINT_MASK_INPUT_INCR,
1749 			usbms_log_handle,
1750 			"middle button pressed");
1751 	}
1752 
1753 	if (nbutt > 3) {
1754 		for (i = 4; i < (nbutt + 1); i++) {
1755 			if (c & USBMS_BUT(i)) {
1756 				mi->mi_buttons = mi->mi_buttons &
1757 					USB_BUT_PRESSED(i);
1758 				USB_DPRINTF_L3(PRINT_MASK_INPUT_INCR,
1759 					usbms_log_handle,
1760 					"%d button pressed", i);
1761 			}
1762 		}
1763 	}
1764 
1765 	/* get the delta X and Y from the sample */
1766 	mi->mi_x += usbms_get_coordinate((usbmsp->usbms_idf).xpos,
1767 				    (usbmsp->usbms_idf).xlen, mp);
1768 
1769 	USB_DPRINTF_L3(PRINT_MASK_INPUT_INCR,
1770 		usbms_log_handle, "x = %d", (int)mi->mi_x);
1771 
1772 	uniqtime32(&mi->mi_time); /* record time when sample arrived */
1773 
1774 	mi->mi_y += usbms_get_coordinate((usbmsp->usbms_idf).ypos,
1775 				    (usbmsp->usbms_idf).ylen, mp);
1776 
1777 	USB_DPRINTF_L3(PRINT_MASK_INPUT_INCR, usbms_log_handle,
1778 		"y = %d", (int)mi->mi_y);
1779 
1780 	/*
1781 	 * Check the wheel data in the current event.
1782 	 * If it exists, the wheel data is got from the sample.
1783 	 */
1784 
1785 	if (usbmsp->usbms_num_wheels) {
1786 		mi->mi_z += usbms_get_coordinate((usbmsp->usbms_idf).zpos,
1787 					    (usbmsp->usbms_idf).zlen, mp);
1788 
1789 		USB_DPRINTF_L3(PRINT_MASK_INPUT_INCR, usbms_log_handle,
1790 			"z = %d", (int)mi->mi_z);
1791 	}
1792 
1793 	if (usbmsp->usbms_jitter) {
1794 		(void) quntimeout(usbmsp->usbms_rq_ptr,
1795 				(timeout_id_t)usbmsp->usbms_timeout_id);
1796 		usbmsp->usbms_jitter = 0;
1797 	}
1798 
1799 	if (!usbmsp->usbms_num_wheels) {
1800 		mi->mi_z = 0;
1801 	}
1802 
1803 	/*
1804 	 * If there is a wheel movement or a change in the button state,
1805 	 * send the data up immediately.
1806 	 */
1807 	if (!(mi->mi_z) && (mi->mi_buttons == usbmsp->usbms_oldbutt)) {
1808 		/*
1809 		 * Buttons did not change; did position?
1810 		 */
1811 		if (mi->mi_x == 0 && mi->mi_y == 0) {
1812 			/* no, position did not change */
1813 
1814 			return;
1815 		}
1816 
1817 		/*
1818 		 * Did the mouse move more than the jitter threshhold?
1819 		 */
1820 		jitter_radius = usbmsp->usbms_jitter_thresh;
1821 		if (USB_ABS((int)mi->mi_x) <= jitter_radius &&
1822 			USB_ABS((int)mi->mi_y) <= jitter_radius) {
1823 			/*
1824 			 * Mouse moved less than the jitter threshhold.
1825 			 * Don't indicate an event; keep accumulating motions.
1826 			 * After "jittertimeout" ticks expire, treat
1827 			 * the accumulated delta as the real delta.
1828 			 */
1829 			usbmsp->usbms_jitter = 1;
1830 			usbmsp->usbms_timeout_id =
1831 			qtimeout(usbmsp->usbms_rq_ptr, (void (*)())usbms_incr,
1832 			(void *)usbmsp, (clock_t)usbmsp->usbms_jittertimeout);
1833 
1834 			return;
1835 		}
1836 	}
1837 	usbmsp->usbms_oldbutt = mi->mi_buttons;
1838 	usbms_incr(usbmsp);
1839 
1840 	USB_DPRINTF_L3(PRINT_MASK_INPUT_INCR, usbms_log_handle,
1841 			"usbms_input exiting");
1842 }
1843 
1844 
1845 /*
1846  * usbms_get_coordinate():
1847  * get the X, Y, WHEEL coordinate values
1848  */
1849 static int
1850 usbms_get_coordinate(uint_t pos, uint_t len, mblk_t *mp)
1851 {
1852 	uint_t utmp, bitval, val;
1853 	int i, xyz;
1854 
1855 	/* get the unsigned int value from the bit stream */
1856 	utmp = 0;
1857 	for (i = (pos + len - 1); i >= pos; i--) {
1858 		bitval = (mp->b_rptr[i/8] & (1 << (i%8))) >> (i%8);
1859 		utmp = utmp * 2 + bitval;
1860 	}
1861 
1862 	/* convert the unsigned int value into int value */
1863 	val = 1 << (len - 1);
1864 	xyz = (int)(utmp - val);
1865 	if (xyz < 0)
1866 		xyz += val;
1867 	else if (xyz == 0)
1868 		xyz = -(val - 1);
1869 	else
1870 		xyz -= val;
1871 
1872 	return (xyz);
1873 }
1874 
1875 
1876 /*
1877  * usbms_incr() :
1878  *	Increment the mouse sample pointer.
1879  *	Called either immediately after a sample or after a jitter timeout.
1880  */
1881 static void
1882 usbms_incr(void				*arg)
1883 {
1884 	usbms_state_t			*usbmsp = arg;
1885 	register struct ms_softc	*ms = &usbmsp->usbms_softc;
1886 	register struct usbmousebuf	*b;
1887 	register struct usbmouseinfo	*mi;
1888 	register int			xc, yc, zc;
1889 	register int			wake;
1890 	register int			speedl = usbmsp->usbms_speedlimit;
1891 	register int			xabs, yabs;
1892 
1893 	/*
1894 	 * No longer waiting for jitter timeout
1895 	 */
1896 	usbmsp->usbms_jitter = 0;
1897 
1898 	b = usbmsp->usbms_buf;
1899 
1900 	USB_DPRINTF_L3(PRINT_MASK_INPUT_INCR, usbms_log_handle,
1901 		"usbms_incr entering");
1902 
1903 	if (b == NULL) {
1904 
1905 		return;
1906 	}
1907 	mi = &b->mb_info[b->mb_off];
1908 	if (usbmsp->usbms_speedlaw) {
1909 		xabs = USB_ABS((int)mi->mi_x);
1910 		yabs = USB_ABS((int)mi->mi_y);
1911 		if (xabs > speedl || yabs > speedl) {
1912 			usbmsp->usbms_speed_count++;
1913 		}
1914 		if (xabs > speedl) {
1915 			mi->mi_x = 0;
1916 		}
1917 		if (yabs > speedl) {
1918 			mi->mi_y = 0;
1919 		}
1920 	}
1921 
1922 
1923 	xc = yc = zc = 0;
1924 
1925 	/* See if we need to wake up anyone waiting for input */
1926 	wake = b->mb_off == ms->ms_oldoff;
1927 
1928 	/* Adjust circular buffer pointer */
1929 	if (++b->mb_off >= b->mb_size) {
1930 		b->mb_off = 0;
1931 		mi = b->mb_info;
1932 	} else {
1933 		mi++;
1934 	}
1935 
1936 	/*
1937 	 * If over-took read index then flush buffer so that mouse state
1938 	 * is consistent.
1939 	 */
1940 	if (b->mb_off == ms->ms_oldoff) {
1941 		if (overrun_msg) {
1942 			USB_DPRINTF_L1(PRINT_MASK_ALL, usbms_log_handle,
1943 				"Mouse buffer flushed when overrun.");
1944 		}
1945 		usbms_flush(usbmsp);
1946 		overrun_cnt++;
1947 		mi = b->mb_info;
1948 	}
1949 
1950 	/* Remember current buttons and fractional part of x & y */
1951 	mi->mi_buttons = (char)USB_NO_BUT_PRESSED;
1952 	mi->mi_x = xc;
1953 	mi->mi_y = yc;
1954 	mi->mi_z = zc;
1955 
1956 	if (wake) {
1957 		USB_DPRINTF_L3(PRINT_MASK_INPUT_INCR, usbms_log_handle,
1958 			"usbms_incr run service");
1959 		qenable(usbmsp->usbms_rq_ptr);	/* run the service proc */
1960 	}
1961 	USB_DPRINTF_L3(PRINT_MASK_INPUT_INCR, usbms_log_handle,
1962 		"usbms_incr exiting");
1963 }
1964 
1965 
1966 /*
1967  * usbms_check_for_wheels
1968  *	return SUCCESS if wheel is found, else return FAILURE
1969  */
1970 static int
1971 usbms_check_for_wheels(usbms_state_t *usbmsp)
1972 {
1973 	int rval, report_id;
1974 
1975 
1976 	if (usbmsp->usbms_report_descr_handle) {
1977 		/* Get the report id that has mouse data */
1978 		if (hidparser_get_usage_attribute(
1979 		    usbmsp->usbms_report_descr_handle,
1980 		    0, /* Doesn't matter */
1981 		    HIDPARSER_ITEM_INPUT,
1982 		    HID_GENERIC_DESKTOP,
1983 		    HID_GD_X,
1984 		    HIDPARSER_ITEM_REPORT_ID,
1985 		    &usbmsp->usbms_rptid) == HIDPARSER_NOT_FOUND) {
1986 			usbmsp->usbms_rptid = HID_REPORT_ID_UNDEFINED;
1987 			report_id = 0;
1988 		} else {
1989 			report_id = usbmsp->usbms_rptid;
1990 		}
1991 
1992 		/* find no. of wheels in this report */
1993 		rval = hidparser_get_usage_attribute(
1994 			usbmsp->usbms_report_descr_handle,
1995 			report_id,
1996 			HIDPARSER_ITEM_INPUT,
1997 			HID_GENERIC_DESKTOP,
1998 			HID_GD_WHEEL,
1999 			HIDPARSER_ITEM_REPORT_COUNT,
2000 			&usbmsp->usbms_num_wheels);
2001 		if (rval == HIDPARSER_SUCCESS) {
2002 			/*
2003 			 * Found wheel. By default enable the wheel.
2004 			 * Currently only enable only the first wheel.
2005 			 */
2006 			usbmsp->usbms_wheel_state_bf |=
2007 					VUID_WHEEL_STATE_ENABLED;
2008 
2009 			return (USB_SUCCESS);
2010 		}
2011 	}
2012 	usbmsp->usbms_num_wheels = 0;
2013 
2014 	return (USB_FAILURE);
2015 }
2016 
2017 
2018 /*
2019  * usbms_make_copyreq
2020  *	helper function for usbms ioctls
2021  */
2022 static int
2023 usbms_make_copyreq(mblk_t 	*mp,
2024 		    uint_t 	pvtsize,
2025 		    uint_t 	state,
2026 		    uint_t 	reqsize,
2027 		    uint_t 	contsize,
2028 		    uint_t 	copytype)
2029 {
2030 
2031 	struct copyreq		*cq;
2032 	struct copyresp		*cr;
2033 	mblk_t			*ioctmp;
2034 	mblk_t			*conttmp;
2035 	usbms_iocstate_t	*usbmsioc;
2036 
2037 	if ((!pvtsize) && state) {
2038 		cr = (struct copyresp *)mp->b_rptr;
2039 		ioctmp = cr->cp_private;
2040 	}
2041 	cq = (struct copyreq *)mp->b_rptr;
2042 	if (mp->b_cont == NULL) {
2043 
2044 		return (EINVAL);
2045 	}
2046 	cq->cq_addr = *((caddr_t *)mp->b_cont->b_rptr);
2047 	cq->cq_size = reqsize;
2048 	cq->cq_flag = 0;
2049 	if (pvtsize) {
2050 		ioctmp = (mblk_t *)allocb(pvtsize, BPRI_MED);
2051 		if (ioctmp == NULL) {
2052 
2053 			return (EAGAIN);
2054 		}
2055 		cq->cq_private = ioctmp;
2056 		ioctmp = cq->cq_private;
2057 	}
2058 	if (state) {
2059 		usbmsioc = (usbms_iocstate_t *)ioctmp->b_rptr;
2060 		usbmsioc->ioc_state = state;
2061 		if (pvtsize) {
2062 			usbmsioc->u_addr = cq->cq_addr;
2063 		} else {
2064 			cq->cq_addr = usbmsioc->u_addr;
2065 			cq->cq_private = ioctmp;
2066 		}
2067 		ioctmp->b_wptr = ioctmp->b_rptr + sizeof (usbms_iocstate_t);
2068 	}
2069 	if (contsize) {
2070 		conttmp = (mblk_t *)allocb(contsize, BPRI_MED);
2071 		if (conttmp == NULL) {
2072 
2073 			return (EAGAIN);
2074 		}
2075 		if (mp->b_cont) {
2076 			freemsg(mp->b_cont);
2077 			mp->b_cont = conttmp;
2078 		}
2079 	}
2080 	mp->b_datap->db_type = copytype;
2081 	mp->b_wptr = mp->b_rptr + sizeof (struct copyreq);
2082 
2083 	return (USB_SUCCESS);
2084 }
2085 
2086 
2087 static int
2088 usbms_service_wheel_info(register queue_t *q, register mblk_t	*datap)
2089 {
2090 
2091 	wheel_info		*wi;
2092 	usbms_state_t		*usbmsp = (usbms_state_t *)q->q_ptr;
2093 	uint_t			err;
2094 
2095 	wi = (wheel_info *)datap->b_rptr;
2096 	if (wi->vers != VUID_WHEEL_INFO_VERS) {
2097 		err = EINVAL;
2098 
2099 		return (err);
2100 	}
2101 	if (wi->id > (usbmsp->usbms_num_wheels - 1)) {
2102 		err = EINVAL;
2103 
2104 		return (err);
2105 	}
2106 	wi->format = (usbmsp->usbms_wheel_orient_bf & (1 << wi->id)) ?
2107 	    VUID_WHEEL_FORMAT_HORIZONTAL : VUID_WHEEL_FORMAT_VERTICAL;
2108 
2109 	return (USB_SUCCESS);
2110 }
2111 
2112 
2113 static int
2114 usbms_service_wheel_state(register queue_t	*q,
2115 			    register mblk_t	*datap,
2116 			    register uint_t	cmd)
2117 {
2118 
2119 	wheel_state	*ws;
2120 	uint_t		err;
2121 	usbms_state_t	*usbmsp = (usbms_state_t *)q->q_ptr;
2122 
2123 	ws = (wheel_state *)datap->b_rptr;
2124 	if (ws->vers != VUID_WHEEL_STATE_VERS) {
2125 		err = EINVAL;
2126 
2127 		return (err);
2128 	}
2129 	if (ws->id > (usbmsp->usbms_num_wheels - 1)) {
2130 		err = EINVAL;
2131 
2132 		return (err);
2133 	}
2134 
2135 	switch (cmd) {
2136 	case	VUIDGWHEELSTATE:
2137 		ws->stateflags = (usbmsp->usbms_wheel_state_bf >> ws->id) &
2138 		    VUID_WHEEL_STATE_ENABLED;
2139 
2140 		break;
2141 	case	VUIDSWHEELSTATE:
2142 		usbmsp->usbms_wheel_state_bf = (ws->stateflags << ws->id) |
2143 		    (~(1 << ws->id) & usbmsp->usbms_wheel_state_bf);
2144 
2145 		break;
2146 	default:
2147 		err = EINVAL;
2148 
2149 		return (err);
2150 	}
2151 
2152 	return (USB_SUCCESS);
2153 }
2154 
2155 
2156 /*
2157  * usbms_get_screen_parms() :
2158  *	Called from MSIOSRESOLUTION ioctl to get the
2159  *	current screen height/width params from X.
2160  */
2161 static int
2162 usbms_get_screen_parms(register queue_t	*q,
2163 			    register mblk_t	*datap)
2164 {
2165 
2166 	usbms_state_t	*usbmsp = (usbms_state_t *)q->q_ptr;
2167 	Ms_screen_resolution	*res = &(usbmsp->usbms_resolution);
2168 	Ms_screen_resolution	*data;
2169 
2170 	data = (Ms_screen_resolution *)datap->b_rptr;
2171 	res->height = data->height;
2172 	res->width = data->width;
2173 
2174 	return (USB_SUCCESS);
2175 }
2176 
2177 
2178 static void
2179 usbms_ack_ioctl(mblk_t	*mp)
2180 {
2181 
2182 	struct iocblk	*iocbp = (struct iocblk *)mp->b_rptr;
2183 
2184 	mp->b_datap->db_type = M_IOCACK;
2185 	mp->b_wptr = mp->b_rptr + sizeof (struct iocblk);
2186 	iocbp->ioc_error = 0;
2187 	iocbp->ioc_count = 0;
2188 	iocbp->ioc_rval = 0;
2189 	if (mp->b_cont != NULL) {
2190 		freemsg(mp->b_cont);
2191 		mp->b_cont = NULL;
2192 	}
2193 }
2194 
2195 
2196 /*
2197  * usbms_setup_abs_mouse_event() :
2198  *	Called from MSIOSRESOLUTION ioctl to create
2199  *	the absolute mouse type firm event.
2200  */
2201 static mblk_t *
2202 usbms_setup_abs_mouse_event()
2203 {
2204 	mblk_t	*mb;
2205 	Firm_event *fep;
2206 
2207 	if ((mb = allocb(sizeof (Firm_event), BPRI_HI)) != NULL) {
2208 		fep = (Firm_event *)mb->b_wptr;
2209 		fep->id = MOUSE_TYPE_ABSOLUTE;
2210 		fep->pair_type = FE_PAIR_NONE;
2211 		fep->pair = NULL;
2212 		fep->value = NULL;
2213 		mb->b_wptr += sizeof (Firm_event);
2214 	} else {
2215 		USB_DPRINTF_L3(PRINT_MASK_ALL, usbms_log_handle,
2216 		    "No resource to report ABS mouse event");
2217 	}
2218 
2219 	return (mb);
2220 }
2221 
2222 
2223 /*
2224  * usbms_read_input_data_format() :
2225  *	Get the mouse packet length and usages' length.
2226  *	Check whether X and Y are relative or absolute.
2227  *
2228  *	If they are absolute, the X and Y logical max values
2229  *	will be got. A firm event will be created and sent
2230  *	to the upper level.
2231  */
2232 int
2233 usbms_read_input_data_format(usbms_state_t *usbmsp)
2234 {
2235 
2236 	hidparser_rpt_t *ms_rpt;
2237 	uint_t i, button_page;
2238 	uint_t limit = 0;
2239 	uint32_t	rptcnt, rptsz;
2240 	usbms_idf *idf = &(usbmsp->usbms_idf);
2241 	Ms_screen_resolution *res = &(usbmsp->usbms_resolution);
2242 	mblk_t *mb;
2243 	register queue_t 	*q;
2244 	int	rval;
2245 
2246 	usbmsp->usbms_rpt_abs = B_FALSE;
2247 
2248 	/* allocate hidparser report structure */
2249 	ms_rpt = kmem_zalloc(sizeof (hidparser_rpt_t), KM_SLEEP);
2250 
2251 	/*
2252 	 * Check what is the total length of the mouse packet
2253 	 * and get the usages and their lengths in order
2254 	 */
2255 
2256 	rval = hidparser_get_usage_list_in_order(
2257 		usbmsp->usbms_report_descr_handle,
2258 		usbmsp->usbms_rptid,
2259 		HIDPARSER_ITEM_INPUT,
2260 		ms_rpt);
2261 
2262 	if (rval != HIDPARSER_SUCCESS) {
2263 
2264 		kmem_free(ms_rpt, sizeof (hidparser_rpt_t));
2265 		return (USB_FAILURE);
2266 	}
2267 
2268 	button_page = 0;
2269 	for (i = 0; i < ms_rpt->no_of_usages; i++) {
2270 		rptcnt = ms_rpt->usage_descr[i].rptcnt;
2271 		rptsz = ms_rpt->usage_descr[i].rptsz;
2272 		if ((ms_rpt->usage_descr[i].usage_page ==
2273 				    HID_BUTTON_PAGE) && (!button_page)) {
2274 			idf->bpos = limit;
2275 			limit += (rptcnt * rptsz);
2276 			button_page = 1;
2277 			continue;
2278 		}
2279 
2280 		switch (ms_rpt->usage_descr[i].usage_id) {
2281 
2282 		case HID_GD_X:
2283 			idf->xpos = limit;
2284 			idf->xlen = rptsz;
2285 			limit += rptsz;
2286 			break;
2287 		case HID_GD_Y:
2288 			idf->ypos = limit;
2289 			idf->ylen = rptsz;
2290 			limit += rptsz;
2291 			break;
2292 		case HID_GD_WHEEL:
2293 			idf->zpos = limit;
2294 			idf->zlen = rptsz;
2295 			limit += rptsz;
2296 			break;
2297 		default:
2298 			limit += rptcnt * rptsz;
2299 			break;
2300 		}
2301 	}
2302 
2303 	kmem_free(ms_rpt, sizeof (hidparser_rpt_t));
2304 
2305 	/* get the length of sending data */
2306 	idf->tlen = limit / 8;
2307 
2308 	/* Check whether X and Y are relative or absolute */
2309 	rval = hidparser_get_main_item_data_descr(
2310 		usbmsp->usbms_report_descr_handle,
2311 		usbmsp->usbms_rptid,
2312 		HIDPARSER_ITEM_INPUT,
2313 		HID_GENERIC_DESKTOP,
2314 		HID_GD_X,
2315 		&idf->xattr);
2316 
2317 	if (rval != HIDPARSER_SUCCESS) {
2318 
2319 		return (USB_FAILURE);
2320 	}
2321 
2322 	/* For the time being assume that Y also has the same attr */
2323 	idf->yattr = idf->xattr;
2324 
2325 	/* get the logical_maximum for X and Y respectively */
2326 	if (!(idf->xattr & HID_MAIN_ITEM_RELATIVE)) {
2327 
2328 		/* the data format can't be parsed correctly */
2329 		if (limit % 8) {
2330 			USB_DPRINTF_L3(PRINT_MASK_ALL, usbms_log_handle,
2331 			    "Wrong data packet include %d bits", limit);
2332 
2333 			return (USB_FAILURE);
2334 		}
2335 		if (hidparser_get_usage_attribute(
2336 			    usbmsp->usbms_report_descr_handle,
2337 			    usbmsp->usbms_rptid,
2338 			    HIDPARSER_ITEM_INPUT,
2339 			    HID_GENERIC_DESKTOP,
2340 			    HID_GD_X,
2341 			    HIDPARSER_ITEM_LOGICAL_MAXIMUM,
2342 			    &usbmsp->usbms_logical_Xmax) != HIDPARSER_SUCCESS) {
2343 
2344 			USB_DPRINTF_L3(PRINT_MASK_ALL, usbms_log_handle,
2345 			    "fail to get X logical max.");
2346 
2347 			return (USB_FAILURE);
2348 		}
2349 		if (hidparser_get_usage_attribute(
2350 			    usbmsp->usbms_report_descr_handle,
2351 			    usbmsp->usbms_rptid,
2352 			    HIDPARSER_ITEM_INPUT,
2353 			    HID_GENERIC_DESKTOP,
2354 			    HID_GD_Y,
2355 			    HIDPARSER_ITEM_LOGICAL_MAXIMUM,
2356 			    &usbmsp->usbms_logical_Ymax) != HIDPARSER_SUCCESS) {
2357 
2358 			USB_DPRINTF_L3(PRINT_MASK_ALL, usbms_log_handle,
2359 			    "fail to get Y logical max.");
2360 
2361 			return (USB_FAILURE);
2362 		}
2363 
2364 		if (usbmsp->usbms_logical_Xmax == 0) {
2365 			USB_DPRINTF_L3(PRINT_MASK_ALL,
2366 			    usbms_log_handle,
2367 			    "X logical max value is zero");
2368 
2369 			return (USB_FAILURE);
2370 		}
2371 
2372 		if (usbmsp->usbms_logical_Ymax == 0) {
2373 			USB_DPRINTF_L3(PRINT_MASK_ALL,
2374 			    usbms_log_handle,
2375 			    "Y logical max value is zero");
2376 
2377 			return (USB_FAILURE);
2378 		}
2379 
2380 		res->height = USBMS_DEFAULT_RES_HEIGHT;
2381 		res->width = USBMS_DEFAULT_RES_WIDTH;
2382 
2383 		/* The wheel is not supported in current remote kvms. */
2384 		usbmsp->usbms_num_wheels = 0;
2385 		q = usbmsp->usbms_rq_ptr;
2386 		if ((mb = usbms_setup_abs_mouse_event()) != NULL) {
2387 			putnext(q, mb);
2388 		} else {
2389 
2390 			return (USB_NO_RESOURCES);
2391 		}
2392 	}
2393 
2394 	return (USB_SUCCESS);
2395 }
2396