Merge remote-tracking branch 'origin/master'
[unleashed/lotheac.git] / usr / src / uts / common / io / vuidmice / vuidmice.c
blobcbca7db795735bfac57b27265059f0dde9f041df
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
28 * VUIDMICE module: put mouse events into vuid format
31 #include <sys/param.h>
32 #include <sys/stream.h>
33 #include <sys/stropts.h>
34 #include <sys/strsun.h>
35 #include <sys/errno.h>
36 #include <sys/debug.h>
37 #include <sys/cmn_err.h>
38 #include <sys/sad.h>
39 #include <sys/vuid_event.h>
40 #include "vuidmice.h"
41 #include <sys/vuid_wheel.h>
42 #include <sys/msio.h>
44 #include <sys/conf.h>
45 #include <sys/modctl.h>
47 #include <sys/kmem.h>
48 #include <sys/ddi.h>
49 #include <sys/sunddi.h>
51 static int vuidmice_open(queue_t *, dev_t *, int, int, cred_t *);
52 static int vuidmice_close(queue_t *, int, cred_t *);
53 static int vuidmice_rput(queue_t *, mblk_t *);
54 static int vuidmice_rsrv(queue_t *);
55 static int vuidmice_wput(queue_t *, mblk_t *);
56 static void vuidmice_miocdata(queue_t *, mblk_t *);
57 static int vuidmice_handle_wheel_resolution_ioctl(queue_t *, mblk_t *, int);
59 static int vuidmice_service_wheel_info(mblk_t *);
60 static int vuidmice_service_wheel_state(queue_t *, mblk_t *, uint_t);
62 void VUID_QUEUE(queue_t *const, mblk_t *);
63 int VUID_OPEN(queue_t *const);
64 void VUID_CLOSE(queue_t *const);
66 static kmutex_t vuidmice_lock;
68 static struct module_info vuidmice_iinfo = {
70 VUID_NAME,
72 INFPSZ,
73 1000,
74 100
77 static struct qinit vuidmice_rinit = {
78 vuidmice_rput,
79 vuidmice_rsrv,
80 vuidmice_open,
81 vuidmice_close,
82 NULL,
83 &vuidmice_iinfo,
84 NULL
87 static struct module_info vuidmice_oinfo = {
89 VUID_NAME,
91 INFPSZ,
92 1000,
93 100
96 static struct qinit vuidmice_winit = {
97 vuidmice_wput,
98 NULL,
99 NULL,
100 NULL,
101 NULL,
102 &vuidmice_oinfo,
103 NULL
106 struct streamtab vuidmice_info = {
107 &vuidmice_rinit,
108 &vuidmice_winit,
109 NULL,
110 NULL
114 * This is the loadable module wrapper.
118 * D_MTQPAIR effectively makes the module single threaded.
119 * There can be only one thread active in the module at any time.
120 * It may be a read or write thread.
122 #define VUIDMICE_CONF_FLAG (D_MP | D_MTQPAIR)
124 static struct fmodsw fsw = {
125 VUID_NAME,
126 &vuidmice_info,
127 VUIDMICE_CONF_FLAG
130 static struct modlstrmod modlstrmod = {
131 &mod_strmodops,
132 "mouse events to vuid events",
133 &fsw
137 * Module linkage information for the kernel.
139 static struct modlinkage modlinkage = {
140 MODREV_1,
141 &modlstrmod,
142 NULL
145 static int module_open = 0; /* allow only one open of this module */
148 _init(void)
150 register int rc;
152 mutex_init(&vuidmice_lock, NULL, MUTEX_DEFAULT, NULL);
153 if ((rc = mod_install(&modlinkage)) != 0) {
154 mutex_destroy(&vuidmice_lock);
156 return (rc);
160 _fini(void)
162 register int rc;
164 if ((rc = mod_remove(&modlinkage)) == 0)
165 mutex_destroy(&vuidmice_lock);
166 return (rc);
170 _info(struct modinfo *modinfop)
172 return (mod_info(&modlinkage, modinfop));
176 /* ARGSUSED1 */
177 static int
178 vuidmice_open(queue_t *qp, dev_t *devp, int oflag, int sflag, cred_t *crp)
180 if (qp->q_ptr != NULL)
181 return (0); /* reopen */
183 mutex_enter(&vuidmice_lock);
185 /* Allow only 1 open of this module */
186 if (module_open) {
187 mutex_exit(&vuidmice_lock);
188 return (EBUSY);
191 module_open++;
192 mutex_exit(&vuidmice_lock);
195 * Both the read and write queues share the same state structures.
197 qp->q_ptr = kmem_zalloc(sizeof (struct MouseStateInfo), KM_SLEEP);
198 WR(qp)->q_ptr = qp->q_ptr;
200 /* initialize state */
201 STATEP->format = VUID_NATIVE;
203 qprocson(qp);
205 #ifdef VUID_OPEN
206 if (VUID_OPEN(qp) != 0) {
207 qprocsoff(qp);
209 mutex_enter(&vuidmice_lock);
210 module_open--;
211 mutex_exit(&vuidmice_lock);
212 kmem_free(qp->q_ptr, sizeof (struct MouseStateInfo));
213 qp->q_ptr = NULL;
214 return (ENXIO);
216 #endif
218 return (0);
221 /* ARGSUSED1 */
222 static int
223 vuidmice_close(queue_t *qp, int flag, cred_t *crp)
225 ASSERT(qp != NULL);
227 qprocsoff(qp);
228 flushq(qp, FLUSHALL);
229 flushq(OTHERQ(qp), FLUSHALL);
231 #ifdef VUID_CLOSE
232 VUID_CLOSE(qp);
233 #endif
234 mutex_enter(&vuidmice_lock);
235 module_open--;
236 mutex_exit(&vuidmice_lock);
237 kmem_free(qp->q_ptr, sizeof (struct MouseStateInfo));
238 qp->q_ptr = NULL;
240 return (0);
244 * Put procedure for input from driver end of stream (read queue).
246 static int
247 vuidmice_rput(queue_t *const qp, mblk_t *mp)
249 ASSERT(qp != NULL);
250 ASSERT(mp != NULL);
253 * Handle all the related high priority messages here, hence
254 * should spend the least amount of time here.
257 if (DB_TYPE(mp) == M_DATA) {
258 if ((int)STATEP->format == VUID_FIRM_EVENT)
259 return (putq(qp, mp)); /* queue message & return */
260 } else if (DB_TYPE(mp) == M_FLUSH) {
261 if (*mp->b_rptr & FLUSHR)
262 flushq(qp, FLUSHALL);
265 putnext(qp, mp); /* pass it on */
266 return (0);
269 static int
270 vuidmice_rsrv(queue_t *const qp)
272 register mblk_t *mp;
274 ASSERT(qp != NULL);
276 while ((mp = getq(qp)) != NULL) {
277 ASSERT(DB_TYPE(mp) == M_DATA);
279 if (!canputnext(qp))
280 return (putbq(qp, mp)); /* read side is blocked */
282 switch (DB_TYPE(mp)) {
283 case M_DATA:
284 if ((int)STATEP->format == VUID_FIRM_EVENT)
285 (void) VUID_QUEUE(qp, mp);
286 else
287 (void) putnext(qp, mp);
288 break;
290 default:
291 cmn_err(CE_WARN,
292 "vuidmice_rsrv: bad message type (0x%x)\n",
293 DB_TYPE(mp));
295 (void) putnext(qp, mp);
296 break;
299 return (0);
303 * Put procedure for write from user end of stream (write queue).
305 static int
306 vuidmice_wput(queue_t *const qp, mblk_t *mp)
308 int error = 0;
310 ASSERT(qp != NULL);
311 ASSERT(mp != NULL);
314 * Handle all the related high priority messages here, hence
315 * should spend the least amount of time here.
317 switch (DB_TYPE(mp)) { /* handle hi pri messages here */
318 case M_FLUSH:
319 if (*mp->b_rptr & FLUSHW)
320 flushq(qp, FLUSHALL);
321 putnext(qp, mp); /* pass it on */
322 return (0);
324 case M_IOCTL: {
325 struct iocblk *iocbp = (void *)mp->b_rptr;
327 switch (iocbp->ioc_cmd) {
328 case VUIDSFORMAT:
331 * VUIDSFORMAT is known to the stream head and thus
332 * is guaranteed to be an I_STR ioctl.
334 if (iocbp->ioc_count == TRANSPARENT) {
335 miocnak(qp, mp, 0, EINVAL);
336 return (0);
337 } else {
338 int format_type;
340 error = miocpullup(mp, sizeof (int));
341 if (error != 0) {
342 miocnak(qp, mp, 0, error);
343 return (0);
346 format_type =
347 *(int *)(void *)mp->b_cont->b_rptr;
348 STATEP->format = (uchar_t)format_type;
349 iocbp->ioc_rval = 0;
350 iocbp->ioc_count = 0;
351 iocbp->ioc_error = 0;
352 mp->b_datap->db_type = M_IOCACK;
355 /* return buffer to pool ASAP */
356 if (mp->b_cont) {
357 freemsg(mp->b_cont);
358 mp->b_cont = NULL;
361 qreply(qp, mp);
362 return (0);
364 case VUIDGFORMAT:
366 /* return buffer to pool ASAP */
367 if (mp->b_cont) {
368 freemsg(mp->b_cont); /* over written below */
369 mp->b_cont = NULL;
373 * VUIDGFORMAT is known to the stream head and thus
374 * is guaranteed to be an I_STR ioctl.
376 if (iocbp->ioc_count == TRANSPARENT) {
377 miocnak(qp, mp, 0, EINVAL);
378 return (0);
381 mp->b_cont = allocb(sizeof (int), BPRI_MED);
382 if (mp->b_cont == NULL) {
383 miocnak(qp, mp, 0, EAGAIN);
384 return (0);
387 *(int *)(void *)mp->b_cont->b_rptr =
388 (int)STATEP->format;
389 mp->b_cont->b_wptr += sizeof (int);
391 iocbp->ioc_count = sizeof (int);
392 mp->b_datap->db_type = M_IOCACK;
393 qreply(qp, mp);
394 return (0);
396 case VUID_NATIVE:
397 case VUIDSADDR:
398 case VUIDGADDR:
399 miocnak(qp, mp, 0, ENOTTY);
400 return (0);
402 case MSIOBUTTONS:
403 /* return buffer to pool ASAP */
404 if (mp->b_cont) {
405 freemsg(mp->b_cont); /* over written below */
406 mp->b_cont = NULL;
410 * MSIOBUTTONS is known to streamio.c and this
411 * is assume to be non-I_STR & non-TRANSPARENT ioctl
414 if (iocbp->ioc_count == TRANSPARENT) {
415 miocnak(qp, mp, 0, EINVAL);
416 return (0);
419 if (STATEP->nbuttons == 0) {
420 miocnak(qp, mp, 0, EINVAL);
421 return (0);
424 mp->b_cont = allocb(sizeof (int), BPRI_MED);
425 if (mp->b_cont == NULL) {
426 miocnak(qp, mp, 0, EAGAIN);
427 return (0);
430 *(int *)(void *)mp->b_cont->b_rptr =
431 (int)STATEP->nbuttons;
432 mp->b_cont->b_wptr += sizeof (int);
434 iocbp->ioc_count = sizeof (int);
435 mp->b_datap->db_type = M_IOCACK;
436 qreply(qp, mp);
437 return (0);
440 * New IOCTL support. Since it's explicitly mentioned
441 * that you can't add more ioctls to stream head's
442 * hard coded list, we have to do the transparent
443 * ioctl processing which is not very exciting.
445 case VUIDGWHEELCOUNT:
446 case VUIDGWHEELINFO:
447 case VUIDGWHEELSTATE:
448 case VUIDSWHEELSTATE:
449 case MSIOSRESOLUTION:
450 error = vuidmice_handle_wheel_resolution_ioctl(qp,
451 mp, iocbp->ioc_cmd);
452 if (!error) {
453 return (0);
454 } else {
455 miocnak(qp, mp, 0, error);
456 return (0);
458 default:
459 putnext(qp, mp); /* nothing to process here */
461 return (0);
464 } /* End of case M_IOCTL */
466 case M_IOCDATA:
467 vuidmice_miocdata(qp, mp);
469 return (0);
470 default:
471 putnext(qp, mp); /* pass it on */
472 return (0);
474 /*NOTREACHED*/
477 void
478 VUID_PUTNEXT(queue_t *const qp, uchar_t event_id, uchar_t event_pair_type,
479 uchar_t event_pair, int event_value)
481 int strikes = 1;
482 mblk_t *bp;
483 Firm_event *fep;
486 * Give this event 3 chances to allocate blocks,
487 * otherwise discard this mouse event. 3 Strikes and you're out.
489 while ((bp = allocb((int)sizeof (Firm_event), BPRI_HI)) == NULL) {
490 if (++strikes > 3)
491 return;
492 drv_usecwait(10);
495 fep = (void *)bp->b_wptr;
496 fep->id = vuid_id_addr(VKEY_FIRST) | vuid_id_offset(event_id);
498 fep->pair_type = event_pair_type;
499 fep->pair = event_pair;
500 fep->value = event_value;
501 uniqtime32(&fep->time);
502 bp->b_wptr += sizeof (Firm_event);
504 if (canput(qp->q_next))
505 putnext(qp, bp);
506 else
507 (void) putbq(qp, bp); /* read side is blocked */
512 * vuidmice_miocdata
513 * M_IOCDATA processing for IOCTL's: VUIDGWHEELCOUNT, VUIDGWHEELINFO,
514 * VUIDGWHEELSTATE, VUIDSWHEELSTATE & MSIOSRESOLUTION.
516 static void
517 vuidmice_miocdata(queue_t *qp, mblk_t *mp)
519 struct copyresp *copyresp;
520 struct iocblk *iocbp;
521 mblk_t *ioctmp;
522 mblk_t *datap;
523 Mouse_iocstate_t *Mouseioc;
524 size_t size;
525 int err = 0;
528 copyresp = (void *)mp->b_rptr;
529 iocbp = (void *)mp->b_rptr;
531 if (copyresp->cp_rval) {
532 err = EAGAIN;
534 goto err;
536 switch (copyresp->cp_cmd) {
537 case VUIDGWHEELCOUNT:
538 mp->b_datap->db_type = M_IOCACK;
539 mp->b_wptr = mp->b_rptr + sizeof (struct iocblk);
540 iocbp->ioc_error = 0;
541 iocbp->ioc_count = 0;
542 iocbp->ioc_rval = 0;
543 if (mp->b_cont != NULL) {
544 freemsg(mp->b_cont);
545 mp->b_cont = NULL;
548 break;
549 case VUIDGWHEELINFO:
550 case VUIDGWHEELSTATE:
551 ioctmp = copyresp->cp_private;
552 Mouseioc = (void *)ioctmp->b_rptr;
553 if (Mouseioc->ioc_state == GETSTRUCT) {
554 if (mp->b_cont == NULL) {
555 err = EINVAL;
557 break;
559 datap = mp->b_cont;
560 if (copyresp->cp_cmd == VUIDGWHEELSTATE) {
561 err = vuidmice_service_wheel_state(qp, datap,
562 VUIDGWHEELSTATE);
563 } else {
564 err = vuidmice_service_wheel_info(datap);
566 if (err) {
567 break;
570 if (copyresp->cp_cmd == VUIDGWHEELSTATE) {
571 size = sizeof (wheel_state);
572 } else {
573 size = sizeof (wheel_info);
576 Mouseioc->ioc_state = GETRESULT;
577 ASSERT(Mouseioc->u_addr != NULL);
578 mcopyout(mp, ioctmp, size, Mouseioc->u_addr, NULL);
579 } else if (Mouseioc->ioc_state == GETRESULT) {
580 freemsg(ioctmp);
581 mp->b_datap->db_type = M_IOCACK;
582 mp->b_wptr = mp->b_rptr + sizeof (struct iocblk);
583 iocbp->ioc_error = 0;
584 iocbp->ioc_count = 0;
585 iocbp->ioc_rval = 0;
586 if (mp->b_cont != NULL) {
587 freemsg(mp->b_cont);
588 mp->b_cont = NULL;
592 break;
593 case VUIDSWHEELSTATE:
594 case MSIOSRESOLUTION:
595 ioctmp = copyresp->cp_private;
596 Mouseioc = (void *)ioctmp->b_rptr;
597 if (mp->b_cont == NULL) {
598 err = EINVAL;
600 break;
602 datap = mp->b_cont;
604 if (copyresp->cp_cmd == VUIDSWHEELSTATE) {
605 err = vuidmice_service_wheel_state(qp,
606 datap, VUIDSWHEELSTATE);
609 if (err) {
610 break;
613 if (mp->b_cont) {
614 freemsg(mp->b_cont);
615 mp->b_cont = NULL;
617 freemsg(ioctmp);
618 iocbp->ioc_count = 0;
619 iocbp->ioc_error = 0;
620 iocbp->ioc_rval = 0;
621 mp->b_datap->db_type = M_IOCACK;
623 break;
624 default:
625 err = EINVAL;
627 break;
630 err:
631 if (err) {
632 mp->b_datap->db_type = M_IOCNAK;
633 if (mp->b_cont) {
634 freemsg(mp->b_cont);
635 mp->b_cont = NULL;
637 if (copyresp->cp_private) {
638 freemsg(copyresp->cp_private);
639 copyresp->cp_private = NULL;
641 iocbp->ioc_count = 0;
642 iocbp->ioc_error = err;
644 qreply(qp, mp);
649 * vuidmice_handle_wheel_resolution_ioctl
650 * Handle wheel mouse and MSIOSRESOLUTION ioctls.
652 * Here we also support non-transparent way of these ioctls
653 * just like usb mouse driver does, so the consms module is
654 * very simple to deal with these ioctls.
656 static int
657 vuidmice_handle_wheel_resolution_ioctl(queue_t *qp, mblk_t *mp, int cmd)
659 int err = 0;
660 Mouse_iocstate_t *Mouseioc;
661 caddr_t useraddr;
662 size_t size;
663 mblk_t *ioctmp;
664 mblk_t *datap;
666 struct iocblk *iocbp = (void *)mp->b_rptr;
668 if (iocbp->ioc_count == TRANSPARENT) {
669 if (mp->b_cont == NULL)
670 return (EINVAL);
671 useraddr = *((caddr_t *)(void *)mp->b_cont->b_rptr);
672 switch (cmd) {
673 case VUIDGWHEELCOUNT:
674 size = sizeof (int);
675 if ((datap = allocb(sizeof (int), BPRI_HI)) == NULL)
676 return (EAGAIN);
677 *((int *)(void *)datap->b_wptr) =
678 STATEP->vuid_mouse_mode;
679 mcopyout(mp, NULL, size, NULL, datap);
680 qreply(qp, mp);
682 return (err);
683 case VUIDGWHEELINFO:
684 size = sizeof (wheel_info);
685 break;
687 case VUIDSWHEELSTATE:
688 case VUIDGWHEELSTATE:
689 size = sizeof (wheel_state);
690 break;
692 case MSIOSRESOLUTION:
693 size = sizeof (Ms_screen_resolution);
694 break;
697 if ((ioctmp = allocb(sizeof (Mouse_iocstate_t),
698 BPRI_MED)) == NULL)
699 return (EAGAIN);
700 Mouseioc = (void *)ioctmp->b_rptr;
701 Mouseioc->ioc_state = GETSTRUCT;
702 Mouseioc->u_addr = useraddr;
703 ioctmp->b_wptr = ioctmp->b_rptr + sizeof (Mouse_iocstate_t);
704 mcopyin(mp, ioctmp, size, NULL);
705 qreply(qp, mp);
707 return (err);
708 } else {
709 switch (cmd) {
710 case VUIDGWHEELCOUNT:
711 if (mp->b_cont) {
712 freemsg(mp->b_cont);
713 mp->b_cont = NULL;
715 if ((datap = allocb(sizeof (int), BPRI_HI)) == NULL) {
716 err = EAGAIN;
717 break;
719 *((int *)(void *)datap->b_wptr) =
720 STATEP->vuid_mouse_mode;
721 datap->b_wptr += sizeof (int);
722 mp->b_cont = datap;
723 break;
725 case VUIDGWHEELINFO:
726 if (mp->b_cont == NULL ||
727 iocbp->ioc_count != sizeof (wheel_info)) {
728 err = EINVAL;
729 break;
731 datap = mp->b_cont;
732 err = vuidmice_service_wheel_info(datap);
733 break;
735 case VUIDSWHEELSTATE:
736 case VUIDGWHEELSTATE:
737 if (mp->b_cont == NULL ||
738 iocbp->ioc_count != sizeof (wheel_state)) {
739 err = EINVAL;
740 break;
742 datap = mp->b_cont;
743 err = vuidmice_service_wheel_state(qp, datap, cmd);
744 break;
746 case MSIOSRESOLUTION:
748 * Now we just make Xserver and
749 * the virtual mouse happy. Of course,
750 * the screen resolution value may
751 * be used later for absolute PS/2 mouse.
753 err = 0;
754 break;
757 if (!err) {
758 mp->b_datap->db_type = M_IOCACK;
759 iocbp->ioc_rval = 0;
760 iocbp->ioc_error = 0;
761 qreply(qp, mp);
764 return (err);
768 static int
769 vuidmice_service_wheel_info(register mblk_t *datap)
771 wheel_info *wi;
772 int err = 0;
774 wi = (void *)datap->b_rptr;
775 if (wi->vers != VUID_WHEEL_INFO_VERS) {
776 err = EINVAL;
777 return (err);
780 if (wi->id > (VUIDMICE_NUM_WHEELS - 1)) {
781 err = EINVAL;
782 return (err);
784 wi->format = (wi->id == VUIDMICE_VERTICAL_WHEEL_ID) ?
785 VUID_WHEEL_FORMAT_VERTICAL : VUID_WHEEL_FORMAT_HORIZONTAL;
787 return (err);
791 static int
792 vuidmice_service_wheel_state(register queue_t *qp,
793 register mblk_t *datap,
794 register uint_t cmd)
796 wheel_state *ws;
797 uint_t err = 0;
799 ws = (void *)datap->b_rptr;
800 if (ws->vers != VUID_WHEEL_STATE_VERS) {
801 err = EINVAL;
802 return (err);
805 if (ws->id > (VUIDMICE_NUM_WHEELS - 1)) {
806 err = EINVAL;
807 return (err);
810 switch (cmd) {
811 case VUIDGWHEELSTATE:
812 ws->stateflags =
813 (STATEP->wheel_state_bf >> ws->id) & 1;
815 break;
816 case VUIDSWHEELSTATE:
817 STATEP->wheel_state_bf = (ws->stateflags << ws->id) |
818 (STATEP->wheel_state_bf & ~(1 << ws->id));
820 break;
821 default:
822 err = EINVAL;
824 return (err);
827 return (err);