10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 55202Smeem * Common Development and Distribution License (the "License"). 65202Smeem * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 22*6710Syz224750 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 270Sstevel@tonic-gate 280Sstevel@tonic-gate 290Sstevel@tonic-gate /* 300Sstevel@tonic-gate * VUIDMICE module: put mouse events into vuid format 310Sstevel@tonic-gate */ 320Sstevel@tonic-gate 330Sstevel@tonic-gate #include <sys/param.h> 340Sstevel@tonic-gate #include <sys/stream.h> 350Sstevel@tonic-gate #include <sys/stropts.h> 360Sstevel@tonic-gate #include <sys/strsun.h> 370Sstevel@tonic-gate #include <sys/errno.h> 380Sstevel@tonic-gate #include <sys/debug.h> 390Sstevel@tonic-gate #include <sys/cmn_err.h> 400Sstevel@tonic-gate #include <sys/sad.h> 410Sstevel@tonic-gate #include <sys/vuid_event.h> 420Sstevel@tonic-gate #include <sys/vuidmice.h> 430Sstevel@tonic-gate #include <sys/vuid_wheel.h> 440Sstevel@tonic-gate #include <sys/msio.h> 450Sstevel@tonic-gate 460Sstevel@tonic-gate #include <sys/conf.h> 470Sstevel@tonic-gate #include <sys/modctl.h> 480Sstevel@tonic-gate 490Sstevel@tonic-gate #include <sys/kmem.h> 500Sstevel@tonic-gate #include <sys/ddi.h> 510Sstevel@tonic-gate #include <sys/sunddi.h> 520Sstevel@tonic-gate 530Sstevel@tonic-gate static int vuidmice_open(queue_t *const, const dev_t *const, 540Sstevel@tonic-gate const int, const int, const cred_t *const); 550Sstevel@tonic-gate static int vuidmice_close(queue_t *const, const int, const cred_t *const); 560Sstevel@tonic-gate static int vuidmice_rput(queue_t *const, mblk_t *); 570Sstevel@tonic-gate static int vuidmice_rsrv(queue_t *const); 580Sstevel@tonic-gate static int vuidmice_wput(queue_t *const, mblk_t *); 590Sstevel@tonic-gate static void vuidmice_miocdata(queue_t *const, mblk_t *); 600Sstevel@tonic-gate static int vuidmice_handle_wheel_resolution_ioctl(queue_t *const, 610Sstevel@tonic-gate mblk_t *, int); 620Sstevel@tonic-gate 630Sstevel@tonic-gate static int vuidmice_service_wheel_info(mblk_t *); 640Sstevel@tonic-gate static int vuidmice_service_wheel_state(queue_t *, mblk_t *, uint_t); 650Sstevel@tonic-gate 660Sstevel@tonic-gate void VUID_QUEUE(queue_t *const, mblk_t *); 670Sstevel@tonic-gate int VUID_OPEN(queue_t *const); 680Sstevel@tonic-gate void VUID_CLOSE(queue_t *const); 690Sstevel@tonic-gate 700Sstevel@tonic-gate static kmutex_t vuidmice_lock; 710Sstevel@tonic-gate 720Sstevel@tonic-gate static struct module_info vuidmice_iinfo = { 730Sstevel@tonic-gate 0, 740Sstevel@tonic-gate VUID_NAME, 750Sstevel@tonic-gate 0, 760Sstevel@tonic-gate INFPSZ, 770Sstevel@tonic-gate 1000, 780Sstevel@tonic-gate 100 790Sstevel@tonic-gate }; 800Sstevel@tonic-gate 810Sstevel@tonic-gate static struct qinit vuidmice_rinit = { 820Sstevel@tonic-gate vuidmice_rput, 830Sstevel@tonic-gate vuidmice_rsrv, 840Sstevel@tonic-gate vuidmice_open, 850Sstevel@tonic-gate vuidmice_close, 860Sstevel@tonic-gate NULL, 870Sstevel@tonic-gate &vuidmice_iinfo, 880Sstevel@tonic-gate NULL 890Sstevel@tonic-gate }; 900Sstevel@tonic-gate 910Sstevel@tonic-gate static struct module_info vuidmice_oinfo = { 920Sstevel@tonic-gate 0, 930Sstevel@tonic-gate VUID_NAME, 940Sstevel@tonic-gate 0, 950Sstevel@tonic-gate INFPSZ, 960Sstevel@tonic-gate 1000, 970Sstevel@tonic-gate 100 980Sstevel@tonic-gate }; 990Sstevel@tonic-gate 1000Sstevel@tonic-gate static struct qinit vuidmice_winit = { 1010Sstevel@tonic-gate vuidmice_wput, 1020Sstevel@tonic-gate NULL, 1030Sstevel@tonic-gate NULL, 1040Sstevel@tonic-gate NULL, 1050Sstevel@tonic-gate NULL, 1060Sstevel@tonic-gate &vuidmice_oinfo, 1070Sstevel@tonic-gate NULL 1080Sstevel@tonic-gate }; 1090Sstevel@tonic-gate 1100Sstevel@tonic-gate struct streamtab vuidmice_info = { 1110Sstevel@tonic-gate &vuidmice_rinit, 1120Sstevel@tonic-gate &vuidmice_winit, 1130Sstevel@tonic-gate NULL, 1140Sstevel@tonic-gate NULL 1150Sstevel@tonic-gate }; 1160Sstevel@tonic-gate 1170Sstevel@tonic-gate /* 1180Sstevel@tonic-gate * This is the loadable module wrapper. 1190Sstevel@tonic-gate */ 1200Sstevel@tonic-gate 1210Sstevel@tonic-gate /* 1220Sstevel@tonic-gate * D_MTQPAIR effectively makes the module single threaded. 1230Sstevel@tonic-gate * There can be only one thread active in the module at any time. 1240Sstevel@tonic-gate * It may be a read or write thread. 1250Sstevel@tonic-gate */ 1260Sstevel@tonic-gate #define VUIDMICE_CONF_FLAG (D_MP | D_MTQPAIR) 1270Sstevel@tonic-gate 1280Sstevel@tonic-gate static struct fmodsw fsw = { 1290Sstevel@tonic-gate VUID_NAME, 1300Sstevel@tonic-gate &vuidmice_info, 1310Sstevel@tonic-gate VUIDMICE_CONF_FLAG 1320Sstevel@tonic-gate }; 1330Sstevel@tonic-gate 1340Sstevel@tonic-gate static struct modlstrmod modlstrmod = { 1350Sstevel@tonic-gate &mod_strmodops, 1360Sstevel@tonic-gate "mouse events to vuid events", 1370Sstevel@tonic-gate &fsw 1380Sstevel@tonic-gate }; 1390Sstevel@tonic-gate 1400Sstevel@tonic-gate /* 1410Sstevel@tonic-gate * Module linkage information for the kernel. 1420Sstevel@tonic-gate */ 1430Sstevel@tonic-gate static struct modlinkage modlinkage = { 1440Sstevel@tonic-gate MODREV_1, 1450Sstevel@tonic-gate &modlstrmod, 1460Sstevel@tonic-gate NULL 1470Sstevel@tonic-gate }; 1480Sstevel@tonic-gate 1490Sstevel@tonic-gate static int module_open = 0; /* allow only one open of this module */ 1500Sstevel@tonic-gate 1510Sstevel@tonic-gate int 1520Sstevel@tonic-gate _init(void) 1530Sstevel@tonic-gate { 1540Sstevel@tonic-gate register int rc; 1550Sstevel@tonic-gate 1560Sstevel@tonic-gate mutex_init(&vuidmice_lock, NULL, MUTEX_DEFAULT, NULL); 1570Sstevel@tonic-gate if ((rc = mod_install(&modlinkage)) != 0) { 1580Sstevel@tonic-gate mutex_destroy(&vuidmice_lock); 1590Sstevel@tonic-gate } 1600Sstevel@tonic-gate return (rc); 1610Sstevel@tonic-gate } 1620Sstevel@tonic-gate 1630Sstevel@tonic-gate int 1640Sstevel@tonic-gate _fini(void) 1650Sstevel@tonic-gate { 1660Sstevel@tonic-gate register int rc; 1670Sstevel@tonic-gate 1680Sstevel@tonic-gate if ((rc = mod_remove(&modlinkage)) == 0) 1690Sstevel@tonic-gate mutex_destroy(&vuidmice_lock); 1700Sstevel@tonic-gate return (rc); 1710Sstevel@tonic-gate } 1720Sstevel@tonic-gate 1730Sstevel@tonic-gate int 1740Sstevel@tonic-gate _info(struct modinfo *modinfop) 1750Sstevel@tonic-gate { 1760Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop)); 1770Sstevel@tonic-gate } 1780Sstevel@tonic-gate 1790Sstevel@tonic-gate 1800Sstevel@tonic-gate /* ARGSUSED1 */ 1810Sstevel@tonic-gate static int 1820Sstevel@tonic-gate vuidmice_open(queue_t *const qp, const dev_t *const devp, 1830Sstevel@tonic-gate const int oflag, const int sflag, const cred_t *const crp) 1840Sstevel@tonic-gate { 1855202Smeem if (qp->q_ptr != NULL) 1865202Smeem return (0); /* reopen */ 1875202Smeem 1880Sstevel@tonic-gate mutex_enter(&vuidmice_lock); 1890Sstevel@tonic-gate 1900Sstevel@tonic-gate /* Allow only 1 open of this module */ 1910Sstevel@tonic-gate if (module_open) { 1920Sstevel@tonic-gate mutex_exit(&vuidmice_lock); 1930Sstevel@tonic-gate return (EBUSY); 1940Sstevel@tonic-gate } 1950Sstevel@tonic-gate 1960Sstevel@tonic-gate module_open++; 1975202Smeem mutex_exit(&vuidmice_lock); 1980Sstevel@tonic-gate 1990Sstevel@tonic-gate /* 2000Sstevel@tonic-gate * Both the read and write queues share the same state structures. 2010Sstevel@tonic-gate */ 2020Sstevel@tonic-gate qp->q_ptr = kmem_zalloc(sizeof (struct MouseStateInfo), KM_SLEEP); 2030Sstevel@tonic-gate WR(qp)->q_ptr = qp->q_ptr; 2040Sstevel@tonic-gate 2050Sstevel@tonic-gate /* initialize state */ 2060Sstevel@tonic-gate STATEP->format = VUID_NATIVE; 2070Sstevel@tonic-gate 2080Sstevel@tonic-gate qprocson(qp); 2090Sstevel@tonic-gate 2100Sstevel@tonic-gate #ifdef VUID_OPEN 2110Sstevel@tonic-gate if (VUID_OPEN(qp) != 0) { 2120Sstevel@tonic-gate qprocsoff(qp); 2130Sstevel@tonic-gate 2140Sstevel@tonic-gate mutex_enter(&vuidmice_lock); 2150Sstevel@tonic-gate module_open--; 2165202Smeem mutex_exit(&vuidmice_lock); 2170Sstevel@tonic-gate kmem_free(qp->q_ptr, sizeof (struct MouseStateInfo)); 2180Sstevel@tonic-gate qp->q_ptr = NULL; 2190Sstevel@tonic-gate return (ENXIO); 2200Sstevel@tonic-gate } 2210Sstevel@tonic-gate #endif 2220Sstevel@tonic-gate 2230Sstevel@tonic-gate return (0); 2240Sstevel@tonic-gate } 2250Sstevel@tonic-gate 2260Sstevel@tonic-gate /* ARGSUSED1 */ 2270Sstevel@tonic-gate static int 2280Sstevel@tonic-gate vuidmice_close(queue_t *const qp, const int flag, const cred_t *const crp) 2290Sstevel@tonic-gate { 2300Sstevel@tonic-gate ASSERT(qp != NULL); 2310Sstevel@tonic-gate 2320Sstevel@tonic-gate qprocsoff(qp); 2330Sstevel@tonic-gate flushq(qp, FLUSHALL); 2340Sstevel@tonic-gate flushq(OTHERQ(qp), FLUSHALL); 2350Sstevel@tonic-gate 2360Sstevel@tonic-gate #ifdef VUID_CLOSE 2370Sstevel@tonic-gate VUID_CLOSE(qp); 2380Sstevel@tonic-gate #endif 2395202Smeem mutex_enter(&vuidmice_lock); 2400Sstevel@tonic-gate module_open--; 2415202Smeem mutex_exit(&vuidmice_lock); 2420Sstevel@tonic-gate kmem_free(qp->q_ptr, sizeof (struct MouseStateInfo)); 2435202Smeem qp->q_ptr = NULL; 2440Sstevel@tonic-gate 2450Sstevel@tonic-gate return (0); 2460Sstevel@tonic-gate } 2470Sstevel@tonic-gate 2480Sstevel@tonic-gate /* 2490Sstevel@tonic-gate * Put procedure for input from driver end of stream (read queue). 2500Sstevel@tonic-gate */ 2510Sstevel@tonic-gate static int 2520Sstevel@tonic-gate vuidmice_rput(queue_t *const qp, mblk_t *mp) 2530Sstevel@tonic-gate { 2540Sstevel@tonic-gate ASSERT(qp != NULL); 2550Sstevel@tonic-gate ASSERT(mp != NULL); 2560Sstevel@tonic-gate 2570Sstevel@tonic-gate /* 2580Sstevel@tonic-gate * Handle all the related high priority messages here, hence 2590Sstevel@tonic-gate * should spend the least amount of time here. 2600Sstevel@tonic-gate */ 2610Sstevel@tonic-gate 2620Sstevel@tonic-gate if (DB_TYPE(mp) == M_DATA) { 2630Sstevel@tonic-gate if ((int)STATEP->format == VUID_FIRM_EVENT) 2640Sstevel@tonic-gate return (putq(qp, mp)); /* queue message & return */ 2650Sstevel@tonic-gate } else if (DB_TYPE(mp) == M_FLUSH) { 2660Sstevel@tonic-gate if (*mp->b_rptr & FLUSHR) 2670Sstevel@tonic-gate flushq(qp, FLUSHALL); 2680Sstevel@tonic-gate } 2690Sstevel@tonic-gate 2700Sstevel@tonic-gate putnext(qp, mp); /* pass it on */ 2710Sstevel@tonic-gate return (0); 2720Sstevel@tonic-gate } 2730Sstevel@tonic-gate 2740Sstevel@tonic-gate static int 2750Sstevel@tonic-gate vuidmice_rsrv(queue_t *const qp) 2760Sstevel@tonic-gate { 2770Sstevel@tonic-gate register mblk_t *mp; 2780Sstevel@tonic-gate 2790Sstevel@tonic-gate ASSERT(qp != NULL); 2800Sstevel@tonic-gate 2810Sstevel@tonic-gate while ((mp = getq(qp)) != NULL) { 2820Sstevel@tonic-gate ASSERT(DB_TYPE(mp) == M_DATA); 2830Sstevel@tonic-gate 2840Sstevel@tonic-gate if (!canputnext(qp)) 2850Sstevel@tonic-gate return (putbq(qp, mp)); /* read side is blocked */ 2860Sstevel@tonic-gate 2870Sstevel@tonic-gate switch (DB_TYPE(mp)) { 2885202Smeem case M_DATA: 2895202Smeem if ((int)STATEP->format == VUID_FIRM_EVENT) 2905202Smeem (void) VUID_QUEUE(qp, mp); 2915202Smeem else 2925202Smeem (void) putnext(qp, mp); 2935202Smeem break; 2940Sstevel@tonic-gate 2955202Smeem default: 2965202Smeem cmn_err(CE_WARN, 2975202Smeem "vuidmice_rsrv: bad message type (0x%x)\n", 2985202Smeem DB_TYPE(mp)); 2990Sstevel@tonic-gate 3005202Smeem (void) putnext(qp, mp); 3015202Smeem break; 3020Sstevel@tonic-gate } 3030Sstevel@tonic-gate } 3040Sstevel@tonic-gate return (0); 3050Sstevel@tonic-gate } 3060Sstevel@tonic-gate 3070Sstevel@tonic-gate /* 3080Sstevel@tonic-gate * Put procedure for write from user end of stream (write queue). 3090Sstevel@tonic-gate */ 3100Sstevel@tonic-gate static int 3110Sstevel@tonic-gate vuidmice_wput(queue_t *const qp, mblk_t *mp) 3120Sstevel@tonic-gate { 3130Sstevel@tonic-gate int error = 0; 3140Sstevel@tonic-gate 3150Sstevel@tonic-gate ASSERT(qp != NULL); 3160Sstevel@tonic-gate ASSERT(mp != NULL); 3170Sstevel@tonic-gate 3180Sstevel@tonic-gate /* 3190Sstevel@tonic-gate * Handle all the related high priority messages here, hence 3200Sstevel@tonic-gate * should spend the least amount of time here. 3210Sstevel@tonic-gate */ 3220Sstevel@tonic-gate switch (DB_TYPE(mp)) { /* handle hi pri messages here */ 3230Sstevel@tonic-gate case M_FLUSH: 3240Sstevel@tonic-gate if (*mp->b_rptr & FLUSHW) 3250Sstevel@tonic-gate flushq(qp, FLUSHALL); 3260Sstevel@tonic-gate putnext(qp, mp); /* pass it on */ 3270Sstevel@tonic-gate return (0); 3280Sstevel@tonic-gate 3290Sstevel@tonic-gate case M_IOCTL: { 3300Sstevel@tonic-gate struct iocblk *iocbp = (struct iocblk *)mp->b_rptr; 3310Sstevel@tonic-gate 3320Sstevel@tonic-gate switch (iocbp->ioc_cmd) { 3330Sstevel@tonic-gate case VUIDSFORMAT: 3340Sstevel@tonic-gate 3350Sstevel@tonic-gate /* 3360Sstevel@tonic-gate * VUIDSFORMAT is known to the stream head and thus 3370Sstevel@tonic-gate * is guaranteed to be an I_STR ioctl. 3380Sstevel@tonic-gate */ 3390Sstevel@tonic-gate if (iocbp->ioc_count == TRANSPARENT) { 3400Sstevel@tonic-gate miocnak(qp, mp, 0, EINVAL); 3410Sstevel@tonic-gate return (0); 3420Sstevel@tonic-gate } else { 3430Sstevel@tonic-gate int format_type; 3440Sstevel@tonic-gate 3450Sstevel@tonic-gate error = miocpullup(mp, sizeof (int)); 3460Sstevel@tonic-gate if (error != 0) { 3470Sstevel@tonic-gate miocnak(qp, mp, 0, error); 3480Sstevel@tonic-gate return (0); 3490Sstevel@tonic-gate } 3500Sstevel@tonic-gate 3510Sstevel@tonic-gate format_type = *(int *)mp->b_cont->b_rptr; 3520Sstevel@tonic-gate STATEP->format = (uchar_t)format_type; 3530Sstevel@tonic-gate iocbp->ioc_rval = 0; 3540Sstevel@tonic-gate iocbp->ioc_count = 0; 3550Sstevel@tonic-gate iocbp->ioc_error = 0; 3560Sstevel@tonic-gate mp->b_datap->db_type = M_IOCACK; 3570Sstevel@tonic-gate } 3580Sstevel@tonic-gate 3590Sstevel@tonic-gate /* return buffer to pool ASAP */ 3600Sstevel@tonic-gate if (mp->b_cont) { 3610Sstevel@tonic-gate freemsg(mp->b_cont); 3620Sstevel@tonic-gate mp->b_cont = NULL; 3630Sstevel@tonic-gate } 3640Sstevel@tonic-gate 3650Sstevel@tonic-gate qreply(qp, mp); 3660Sstevel@tonic-gate return (0); 3670Sstevel@tonic-gate 3680Sstevel@tonic-gate case VUIDGFORMAT: 3690Sstevel@tonic-gate 3700Sstevel@tonic-gate /* return buffer to pool ASAP */ 3710Sstevel@tonic-gate if (mp->b_cont) { 3720Sstevel@tonic-gate freemsg(mp->b_cont); /* over written below */ 3730Sstevel@tonic-gate mp->b_cont = NULL; 3740Sstevel@tonic-gate } 3750Sstevel@tonic-gate 3760Sstevel@tonic-gate /* 3770Sstevel@tonic-gate * VUIDGFORMAT is known to the stream head and thus 3780Sstevel@tonic-gate * is guaranteed to be an I_STR ioctl. 3790Sstevel@tonic-gate */ 3800Sstevel@tonic-gate if (iocbp->ioc_count == TRANSPARENT) { 3810Sstevel@tonic-gate miocnak(qp, mp, 0, EINVAL); 3820Sstevel@tonic-gate return (0); 3830Sstevel@tonic-gate } 3840Sstevel@tonic-gate 3850Sstevel@tonic-gate mp->b_cont = allocb(sizeof (int), BPRI_MED); 3860Sstevel@tonic-gate if (mp->b_cont == NULL) { 3870Sstevel@tonic-gate miocnak(qp, mp, 0, EAGAIN); 3880Sstevel@tonic-gate return (0); 3890Sstevel@tonic-gate } 3900Sstevel@tonic-gate 3910Sstevel@tonic-gate *(int *)mp->b_cont->b_rptr = (int)STATEP->format; 3920Sstevel@tonic-gate mp->b_cont->b_wptr += sizeof (int); 3930Sstevel@tonic-gate 3940Sstevel@tonic-gate iocbp->ioc_count = sizeof (int); 3950Sstevel@tonic-gate mp->b_datap->db_type = M_IOCACK; 3960Sstevel@tonic-gate qreply(qp, mp); 3970Sstevel@tonic-gate return (0); 3980Sstevel@tonic-gate 3990Sstevel@tonic-gate case VUID_NATIVE: 4000Sstevel@tonic-gate case VUIDSADDR: 4010Sstevel@tonic-gate case VUIDGADDR: 4020Sstevel@tonic-gate miocnak(qp, mp, 0, ENOTTY); 4030Sstevel@tonic-gate return (0); 4040Sstevel@tonic-gate 4050Sstevel@tonic-gate case MSIOBUTTONS: 4060Sstevel@tonic-gate /* return buffer to pool ASAP */ 4070Sstevel@tonic-gate if (mp->b_cont) { 4080Sstevel@tonic-gate freemsg(mp->b_cont); /* over written below */ 4090Sstevel@tonic-gate mp->b_cont = NULL; 4100Sstevel@tonic-gate } 4110Sstevel@tonic-gate 4120Sstevel@tonic-gate /* 4130Sstevel@tonic-gate * MSIOBUTTONS is known to streamio.c and this 4140Sstevel@tonic-gate * is assume to be non-I_STR & non-TRANSPARENT ioctl 4150Sstevel@tonic-gate */ 4160Sstevel@tonic-gate 4170Sstevel@tonic-gate if (iocbp->ioc_count == TRANSPARENT) { 4180Sstevel@tonic-gate miocnak(qp, mp, 0, EINVAL); 4190Sstevel@tonic-gate return (0); 4200Sstevel@tonic-gate } 4210Sstevel@tonic-gate 4220Sstevel@tonic-gate if (STATEP->nbuttons == 0) { 4230Sstevel@tonic-gate miocnak(qp, mp, 0, EINVAL); 4240Sstevel@tonic-gate return (0); 4250Sstevel@tonic-gate } 4260Sstevel@tonic-gate 4270Sstevel@tonic-gate mp->b_cont = allocb(sizeof (int), BPRI_MED); 4280Sstevel@tonic-gate if (mp->b_cont == NULL) { 4290Sstevel@tonic-gate miocnak(qp, mp, 0, EAGAIN); 4300Sstevel@tonic-gate return (0); 4310Sstevel@tonic-gate } 4320Sstevel@tonic-gate 4330Sstevel@tonic-gate *(int *)mp->b_cont->b_rptr = (int)STATEP->nbuttons; 4340Sstevel@tonic-gate mp->b_cont->b_wptr += sizeof (int); 4350Sstevel@tonic-gate 4360Sstevel@tonic-gate iocbp->ioc_count = sizeof (int); 4370Sstevel@tonic-gate mp->b_datap->db_type = M_IOCACK; 4380Sstevel@tonic-gate qreply(qp, mp); 4390Sstevel@tonic-gate return (0); 4400Sstevel@tonic-gate 4410Sstevel@tonic-gate /* 4420Sstevel@tonic-gate * New IOCTL support. Since it's explicitly mentioned 4430Sstevel@tonic-gate * that you can't add more ioctls to stream head's 4440Sstevel@tonic-gate * hard coded list, we have to do the transparent 4450Sstevel@tonic-gate * ioctl processing which is not very exciting. 4460Sstevel@tonic-gate */ 4470Sstevel@tonic-gate case VUIDGWHEELCOUNT: 4480Sstevel@tonic-gate case VUIDGWHEELINFO: 4490Sstevel@tonic-gate case VUIDGWHEELSTATE: 4500Sstevel@tonic-gate case VUIDSWHEELSTATE: 4510Sstevel@tonic-gate case MSIOSRESOLUTION: 4520Sstevel@tonic-gate error = vuidmice_handle_wheel_resolution_ioctl(qp, 4535202Smeem mp, iocbp->ioc_cmd); 4540Sstevel@tonic-gate if (!error) { 4550Sstevel@tonic-gate return (0); 4560Sstevel@tonic-gate } else { 4570Sstevel@tonic-gate miocnak(qp, mp, 0, error); 4580Sstevel@tonic-gate return (0); 4590Sstevel@tonic-gate } 4600Sstevel@tonic-gate default: 4610Sstevel@tonic-gate putnext(qp, mp); /* nothing to process here */ 4620Sstevel@tonic-gate 4630Sstevel@tonic-gate return (0); 4640Sstevel@tonic-gate } 4650Sstevel@tonic-gate 4660Sstevel@tonic-gate } /* End of case M_IOCTL */ 4670Sstevel@tonic-gate 4680Sstevel@tonic-gate case M_IOCDATA: 4690Sstevel@tonic-gate vuidmice_miocdata(qp, mp); 4700Sstevel@tonic-gate 4710Sstevel@tonic-gate return (0); 4720Sstevel@tonic-gate default: 4730Sstevel@tonic-gate putnext(qp, mp); /* pass it on */ 4740Sstevel@tonic-gate return (0); 4750Sstevel@tonic-gate } 4760Sstevel@tonic-gate /*NOTREACHED*/ 4770Sstevel@tonic-gate } 4780Sstevel@tonic-gate 4790Sstevel@tonic-gate void 4800Sstevel@tonic-gate VUID_PUTNEXT(queue_t *const qp, uchar_t event_id, uchar_t event_pair_type, 4810Sstevel@tonic-gate uchar_t event_pair, int event_value) 4820Sstevel@tonic-gate { 4830Sstevel@tonic-gate int strikes = 1; 4840Sstevel@tonic-gate mblk_t *bp; 4850Sstevel@tonic-gate Firm_event *fep; 4860Sstevel@tonic-gate 4870Sstevel@tonic-gate /* 4880Sstevel@tonic-gate * Give this event 3 chances to allocate blocks, 4890Sstevel@tonic-gate * otherwise discard this mouse event. 3 Strikes and you're out. 4900Sstevel@tonic-gate */ 4910Sstevel@tonic-gate while ((bp = allocb((int)sizeof (Firm_event), BPRI_HI)) == NULL) { 4920Sstevel@tonic-gate if (++strikes > 3) 4930Sstevel@tonic-gate return; 4940Sstevel@tonic-gate drv_usecwait(10); 4950Sstevel@tonic-gate } 4960Sstevel@tonic-gate 4970Sstevel@tonic-gate fep = (Firm_event *)bp->b_wptr; 4980Sstevel@tonic-gate fep->id = vuid_id_addr(VKEY_FIRST) | vuid_id_offset(event_id); 4990Sstevel@tonic-gate 5000Sstevel@tonic-gate fep->pair_type = event_pair_type; 5010Sstevel@tonic-gate fep->pair = event_pair; 5020Sstevel@tonic-gate fep->value = event_value; 5030Sstevel@tonic-gate uniqtime32(&fep->time); 5040Sstevel@tonic-gate bp->b_wptr += sizeof (Firm_event); 5050Sstevel@tonic-gate 5060Sstevel@tonic-gate if (canput(qp->q_next)) 5070Sstevel@tonic-gate putnext(qp, bp); 5080Sstevel@tonic-gate else 5090Sstevel@tonic-gate (void) putbq(qp, bp); /* read side is blocked */ 5100Sstevel@tonic-gate } 5110Sstevel@tonic-gate 5120Sstevel@tonic-gate 5130Sstevel@tonic-gate /* 5140Sstevel@tonic-gate * vuidmice_miocdata 5150Sstevel@tonic-gate * M_IOCDATA processing for IOCTL's: VUIDGWHEELCOUNT, VUIDGWHEELINFO, 5160Sstevel@tonic-gate * VUIDGWHEELSTATE, VUIDSWHEELSTATE & MSIOSRESOLUTION. 5170Sstevel@tonic-gate */ 5180Sstevel@tonic-gate static void 5190Sstevel@tonic-gate vuidmice_miocdata(queue_t *qp, mblk_t *mp) 5200Sstevel@tonic-gate { 5210Sstevel@tonic-gate struct copyresp *copyresp; 5220Sstevel@tonic-gate struct iocblk *iocbp; 5230Sstevel@tonic-gate mblk_t *ioctmp; 5240Sstevel@tonic-gate mblk_t *datap; 5250Sstevel@tonic-gate Mouse_iocstate_t *Mouseioc; 526*6710Syz224750 size_t size; 5270Sstevel@tonic-gate int err = 0; 5280Sstevel@tonic-gate 5290Sstevel@tonic-gate 5300Sstevel@tonic-gate copyresp = (struct copyresp *)mp->b_rptr; 5310Sstevel@tonic-gate iocbp = (struct iocblk *)mp->b_rptr; 5320Sstevel@tonic-gate 5330Sstevel@tonic-gate if (copyresp->cp_rval) { 5340Sstevel@tonic-gate err = EAGAIN; 5350Sstevel@tonic-gate 5360Sstevel@tonic-gate goto err; 5370Sstevel@tonic-gate } 5380Sstevel@tonic-gate switch (copyresp->cp_cmd) { 5390Sstevel@tonic-gate case VUIDGWHEELCOUNT: 5400Sstevel@tonic-gate mp->b_datap->db_type = M_IOCACK; 5410Sstevel@tonic-gate mp->b_wptr = mp->b_rptr + sizeof (struct iocblk); 5420Sstevel@tonic-gate iocbp->ioc_error = 0; 5430Sstevel@tonic-gate iocbp->ioc_count = 0; 5440Sstevel@tonic-gate iocbp->ioc_rval = 0; 5450Sstevel@tonic-gate if (mp->b_cont != NULL) { 5460Sstevel@tonic-gate freemsg(mp->b_cont); 5470Sstevel@tonic-gate mp->b_cont = NULL; 5480Sstevel@tonic-gate } 5490Sstevel@tonic-gate 5500Sstevel@tonic-gate break; 5510Sstevel@tonic-gate case VUIDGWHEELINFO: 5520Sstevel@tonic-gate case VUIDGWHEELSTATE: 5530Sstevel@tonic-gate ioctmp = (mblk_t *)copyresp->cp_private; 5540Sstevel@tonic-gate Mouseioc = (Mouse_iocstate_t *)ioctmp->b_rptr; 5550Sstevel@tonic-gate if (Mouseioc->ioc_state == GETSTRUCT) { 5560Sstevel@tonic-gate if (mp->b_cont == NULL) { 5570Sstevel@tonic-gate err = EINVAL; 5580Sstevel@tonic-gate 5590Sstevel@tonic-gate break; 5600Sstevel@tonic-gate } 5610Sstevel@tonic-gate datap = mp->b_cont; 5620Sstevel@tonic-gate if (copyresp->cp_cmd == VUIDGWHEELSTATE) { 5630Sstevel@tonic-gate err = vuidmice_service_wheel_state(qp, datap, 5645202Smeem VUIDGWHEELSTATE); 5650Sstevel@tonic-gate } else { 5660Sstevel@tonic-gate err = vuidmice_service_wheel_info(datap); 5670Sstevel@tonic-gate } 5680Sstevel@tonic-gate if (err) { 5690Sstevel@tonic-gate break; 5700Sstevel@tonic-gate } 5710Sstevel@tonic-gate 5720Sstevel@tonic-gate if (copyresp->cp_cmd == VUIDGWHEELSTATE) { 573*6710Syz224750 size = sizeof (wheel_state); 5740Sstevel@tonic-gate } else { 575*6710Syz224750 size = sizeof (wheel_info); 5760Sstevel@tonic-gate } 5770Sstevel@tonic-gate 5780Sstevel@tonic-gate Mouseioc->ioc_state = GETRESULT; 579*6710Syz224750 ASSERT(Mouseioc->u_addr != NULL); 580*6710Syz224750 mcopyout(mp, ioctmp, size, Mouseioc->u_addr, NULL); 5810Sstevel@tonic-gate } else if (Mouseioc->ioc_state == GETRESULT) { 5820Sstevel@tonic-gate freemsg(ioctmp); 5830Sstevel@tonic-gate mp->b_datap->db_type = M_IOCACK; 5840Sstevel@tonic-gate mp->b_wptr = mp->b_rptr + sizeof (struct iocblk); 5850Sstevel@tonic-gate iocbp->ioc_error = 0; 5860Sstevel@tonic-gate iocbp->ioc_count = 0; 5870Sstevel@tonic-gate iocbp->ioc_rval = 0; 5880Sstevel@tonic-gate if (mp->b_cont != NULL) { 5890Sstevel@tonic-gate freemsg(mp->b_cont); 5900Sstevel@tonic-gate mp->b_cont = NULL; 5910Sstevel@tonic-gate } 5920Sstevel@tonic-gate } 5930Sstevel@tonic-gate 5940Sstevel@tonic-gate break; 5950Sstevel@tonic-gate case VUIDSWHEELSTATE: 5960Sstevel@tonic-gate case MSIOSRESOLUTION: 5970Sstevel@tonic-gate ioctmp = (mblk_t *)copyresp->cp_private; 5980Sstevel@tonic-gate Mouseioc = (Mouse_iocstate_t *)ioctmp->b_rptr; 5990Sstevel@tonic-gate if (mp->b_cont == NULL) { 6000Sstevel@tonic-gate err = EINVAL; 6010Sstevel@tonic-gate 6020Sstevel@tonic-gate break; 6030Sstevel@tonic-gate } 6040Sstevel@tonic-gate datap = mp->b_cont; 6050Sstevel@tonic-gate 6060Sstevel@tonic-gate if (copyresp->cp_cmd == VUIDSWHEELSTATE) { 6070Sstevel@tonic-gate err = vuidmice_service_wheel_state(qp, 6080Sstevel@tonic-gate datap, VUIDSWHEELSTATE); 6090Sstevel@tonic-gate } 6100Sstevel@tonic-gate 6110Sstevel@tonic-gate if (err) { 6120Sstevel@tonic-gate break; 6130Sstevel@tonic-gate } 6140Sstevel@tonic-gate 6150Sstevel@tonic-gate if (mp->b_cont) { 6160Sstevel@tonic-gate freemsg(mp->b_cont); 6170Sstevel@tonic-gate mp->b_cont = (mblk_t *)NULL; 6180Sstevel@tonic-gate } 6190Sstevel@tonic-gate freemsg(ioctmp); 6200Sstevel@tonic-gate iocbp->ioc_count = 0; 6210Sstevel@tonic-gate iocbp->ioc_error = 0; 6220Sstevel@tonic-gate iocbp->ioc_rval = 0; 6230Sstevel@tonic-gate mp->b_datap->db_type = M_IOCACK; 6240Sstevel@tonic-gate 6250Sstevel@tonic-gate break; 6260Sstevel@tonic-gate default: 6270Sstevel@tonic-gate err = EINVAL; 6280Sstevel@tonic-gate 6290Sstevel@tonic-gate break; 6300Sstevel@tonic-gate } 6310Sstevel@tonic-gate 6320Sstevel@tonic-gate err: 6330Sstevel@tonic-gate if (err) { 6340Sstevel@tonic-gate mp->b_datap->db_type = M_IOCNAK; 6350Sstevel@tonic-gate if (mp->b_cont) { 6360Sstevel@tonic-gate freemsg(mp->b_cont); 6370Sstevel@tonic-gate mp->b_cont = (mblk_t *)NULL; 6380Sstevel@tonic-gate } 6390Sstevel@tonic-gate if (copyresp->cp_private) { 6400Sstevel@tonic-gate freemsg((mblk_t *)copyresp->cp_private); 6410Sstevel@tonic-gate copyresp->cp_private = (mblk_t *)NULL; 6420Sstevel@tonic-gate } 6430Sstevel@tonic-gate iocbp->ioc_count = 0; 6440Sstevel@tonic-gate iocbp->ioc_error = err; 6450Sstevel@tonic-gate } 6460Sstevel@tonic-gate qreply(qp, mp); 6470Sstevel@tonic-gate } 6480Sstevel@tonic-gate 6490Sstevel@tonic-gate 6500Sstevel@tonic-gate /* 6510Sstevel@tonic-gate * vuidmice_handle_wheel_resolution_ioctl 6520Sstevel@tonic-gate * Handle wheel mouse and MSIOSRESOLUTION ioctls. 6530Sstevel@tonic-gate * 6540Sstevel@tonic-gate * Here we also support non-transparent way of these ioctls 6550Sstevel@tonic-gate * just like usb mouse driver does, so the consms module is 6560Sstevel@tonic-gate * very simple to deal with these ioctls. 6570Sstevel@tonic-gate */ 6580Sstevel@tonic-gate static int 6590Sstevel@tonic-gate vuidmice_handle_wheel_resolution_ioctl(queue_t *qp, mblk_t *mp, int cmd) 6600Sstevel@tonic-gate { 6610Sstevel@tonic-gate int err = 0; 6620Sstevel@tonic-gate Mouse_iocstate_t *Mouseioc; 663*6710Syz224750 caddr_t useraddr; 664*6710Syz224750 size_t size; 6650Sstevel@tonic-gate mblk_t *ioctmp; 6660Sstevel@tonic-gate mblk_t *datap; 6670Sstevel@tonic-gate 6680Sstevel@tonic-gate struct iocblk *iocbp = (struct iocblk *)mp->b_rptr; 6690Sstevel@tonic-gate 6700Sstevel@tonic-gate if (iocbp->ioc_count == TRANSPARENT) { 671*6710Syz224750 if (mp->b_cont == NULL) 672*6710Syz224750 return (EINVAL); 673*6710Syz224750 useraddr = (caddr_t)*((caddr_t *)mp->b_cont->b_rptr); 6740Sstevel@tonic-gate switch (cmd) { 6750Sstevel@tonic-gate case VUIDGWHEELCOUNT: 676*6710Syz224750 size = sizeof (int); 677*6710Syz224750 if ((datap = allocb(sizeof (int), BPRI_HI)) == NULL) 678*6710Syz224750 return (EAGAIN); 6795202Smeem *((int *)datap->b_wptr) = STATEP->vuid_mouse_mode; 680*6710Syz224750 mcopyout(mp, NULL, size, NULL, datap); 6810Sstevel@tonic-gate qreply(qp, mp); 6820Sstevel@tonic-gate 6830Sstevel@tonic-gate return (err); 6840Sstevel@tonic-gate case VUIDGWHEELINFO: 685*6710Syz224750 size = sizeof (wheel_info); 6860Sstevel@tonic-gate break; 6870Sstevel@tonic-gate 6880Sstevel@tonic-gate case VUIDSWHEELSTATE: 6890Sstevel@tonic-gate case VUIDGWHEELSTATE: 690*6710Syz224750 size = sizeof (wheel_state); 6910Sstevel@tonic-gate break; 6920Sstevel@tonic-gate 6930Sstevel@tonic-gate case MSIOSRESOLUTION: 694*6710Syz224750 size = sizeof (Ms_screen_resolution); 6950Sstevel@tonic-gate break; 6960Sstevel@tonic-gate } 6970Sstevel@tonic-gate 6980Sstevel@tonic-gate if ((ioctmp = (mblk_t *)allocb(sizeof (Mouse_iocstate_t), 699*6710Syz224750 BPRI_MED)) == NULL) 700*6710Syz224750 return (EAGAIN); 7010Sstevel@tonic-gate Mouseioc = (Mouse_iocstate_t *)ioctmp->b_rptr; 7020Sstevel@tonic-gate Mouseioc->ioc_state = GETSTRUCT; 703*6710Syz224750 Mouseioc->u_addr = useraddr; 7040Sstevel@tonic-gate ioctmp->b_wptr = ioctmp->b_rptr + sizeof (Mouse_iocstate_t); 705*6710Syz224750 mcopyin(mp, ioctmp, size, NULL); 7060Sstevel@tonic-gate qreply(qp, mp); 7070Sstevel@tonic-gate 7080Sstevel@tonic-gate return (err); 7090Sstevel@tonic-gate } else { 7100Sstevel@tonic-gate switch (cmd) { 7110Sstevel@tonic-gate case VUIDGWHEELCOUNT: 7120Sstevel@tonic-gate if (mp->b_cont) { 7130Sstevel@tonic-gate freemsg(mp->b_cont); 7140Sstevel@tonic-gate mp->b_cont = NULL; 7150Sstevel@tonic-gate } 716*6710Syz224750 if ((datap = allocb(sizeof (int), BPRI_HI)) == NULL) { 717*6710Syz224750 err = EAGAIN; 718*6710Syz224750 break; 719*6710Syz224750 } 7205202Smeem *((int *)datap->b_wptr) = STATEP->vuid_mouse_mode; 7210Sstevel@tonic-gate datap->b_wptr += sizeof (int); 7220Sstevel@tonic-gate mp->b_cont = datap; 7230Sstevel@tonic-gate break; 7240Sstevel@tonic-gate 7250Sstevel@tonic-gate case VUIDGWHEELINFO: 7260Sstevel@tonic-gate if (mp->b_cont == NULL || 7270Sstevel@tonic-gate iocbp->ioc_count != sizeof (wheel_info)) { 7280Sstevel@tonic-gate err = EINVAL; 7290Sstevel@tonic-gate break; 7300Sstevel@tonic-gate } 7310Sstevel@tonic-gate datap = mp->b_cont; 7320Sstevel@tonic-gate err = vuidmice_service_wheel_info(datap); 7330Sstevel@tonic-gate break; 7340Sstevel@tonic-gate 7350Sstevel@tonic-gate case VUIDSWHEELSTATE: 7360Sstevel@tonic-gate case VUIDGWHEELSTATE: 7370Sstevel@tonic-gate if (mp->b_cont == NULL || 7380Sstevel@tonic-gate iocbp->ioc_count != sizeof (wheel_state)) { 7390Sstevel@tonic-gate err = EINVAL; 7400Sstevel@tonic-gate break; 7410Sstevel@tonic-gate } 7420Sstevel@tonic-gate datap = mp->b_cont; 7430Sstevel@tonic-gate err = vuidmice_service_wheel_state(qp, datap, cmd); 7440Sstevel@tonic-gate break; 7450Sstevel@tonic-gate 7460Sstevel@tonic-gate case MSIOSRESOLUTION: 7470Sstevel@tonic-gate /* 7480Sstevel@tonic-gate * Now we just make Xserver and 7490Sstevel@tonic-gate * the virtual mouse happy. Of course, 7500Sstevel@tonic-gate * the screen resolution value may 7510Sstevel@tonic-gate * be used later for absolute PS/2 mouse. 7520Sstevel@tonic-gate */ 7530Sstevel@tonic-gate err = 0; 7540Sstevel@tonic-gate break; 7550Sstevel@tonic-gate } 7560Sstevel@tonic-gate 7570Sstevel@tonic-gate if (!err) { 7580Sstevel@tonic-gate mp->b_datap->db_type = M_IOCACK; 7590Sstevel@tonic-gate iocbp->ioc_rval = 0; 7600Sstevel@tonic-gate iocbp->ioc_error = 0; 7610Sstevel@tonic-gate qreply(qp, mp); 7620Sstevel@tonic-gate } 7630Sstevel@tonic-gate 7640Sstevel@tonic-gate return (err); 7650Sstevel@tonic-gate } 7660Sstevel@tonic-gate } 7670Sstevel@tonic-gate 7680Sstevel@tonic-gate static int 7690Sstevel@tonic-gate vuidmice_service_wheel_info(register mblk_t *datap) 7700Sstevel@tonic-gate { 7710Sstevel@tonic-gate wheel_info *wi; 7720Sstevel@tonic-gate int err = 0; 7730Sstevel@tonic-gate 7740Sstevel@tonic-gate wi = (wheel_info *)datap->b_rptr; 7750Sstevel@tonic-gate if (wi->vers != VUID_WHEEL_INFO_VERS) { 7760Sstevel@tonic-gate err = EINVAL; 7770Sstevel@tonic-gate return (err); 7780Sstevel@tonic-gate } 7790Sstevel@tonic-gate 7800Sstevel@tonic-gate if (wi->id > (VUIDMICE_NUM_WHEELS - 1)) { 7810Sstevel@tonic-gate err = EINVAL; 7820Sstevel@tonic-gate return (err); 7830Sstevel@tonic-gate } 7845202Smeem wi->format = (wi->id == VUIDMICE_VERTICAL_WHEEL_ID) ? 7855202Smeem VUID_WHEEL_FORMAT_VERTICAL : VUID_WHEEL_FORMAT_HORIZONTAL; 7860Sstevel@tonic-gate 7870Sstevel@tonic-gate return (err); 7880Sstevel@tonic-gate } 7890Sstevel@tonic-gate 7900Sstevel@tonic-gate 7910Sstevel@tonic-gate static int 7920Sstevel@tonic-gate vuidmice_service_wheel_state(register queue_t *qp, 7930Sstevel@tonic-gate register mblk_t *datap, 7940Sstevel@tonic-gate register uint_t cmd) 7950Sstevel@tonic-gate { 7960Sstevel@tonic-gate wheel_state *ws; 7970Sstevel@tonic-gate uint_t err = 0; 7980Sstevel@tonic-gate 7990Sstevel@tonic-gate ws = (wheel_state *)datap->b_rptr; 8000Sstevel@tonic-gate if (ws->vers != VUID_WHEEL_STATE_VERS) { 8010Sstevel@tonic-gate err = EINVAL; 8020Sstevel@tonic-gate return (err); 8030Sstevel@tonic-gate } 8040Sstevel@tonic-gate 8050Sstevel@tonic-gate if (ws->id > (VUIDMICE_NUM_WHEELS - 1)) { 8060Sstevel@tonic-gate err = EINVAL; 8070Sstevel@tonic-gate return (err); 8080Sstevel@tonic-gate } 8090Sstevel@tonic-gate 8100Sstevel@tonic-gate switch (cmd) { 8110Sstevel@tonic-gate case VUIDGWHEELSTATE: 8120Sstevel@tonic-gate ws->stateflags = 8130Sstevel@tonic-gate (STATEP->wheel_state_bf >> ws->id) & 1; 8140Sstevel@tonic-gate 8150Sstevel@tonic-gate break; 8160Sstevel@tonic-gate case VUIDSWHEELSTATE: 8170Sstevel@tonic-gate STATEP->wheel_state_bf = (ws->stateflags << ws->id) | 8180Sstevel@tonic-gate (STATEP->wheel_state_bf & ~(1 << ws->id)); 8190Sstevel@tonic-gate 8200Sstevel@tonic-gate break; 8210Sstevel@tonic-gate default: 8220Sstevel@tonic-gate err = EINVAL; 8230Sstevel@tonic-gate 8240Sstevel@tonic-gate return (err); 8250Sstevel@tonic-gate } 8260Sstevel@tonic-gate 8270Sstevel@tonic-gate return (err); 8280Sstevel@tonic-gate } 829