[BACK]Return to mouse.c CVS log [TXT][DIR] Up to [local] / prex-old / dev / i386 / pc

Annotation of prex-old/dev/i386/pc/mouse.c, Revision 1.1.1.1

1.1       nbrk        1: /*-
                      2:  * Copyright (c) 2005, Kohsuke Ohtani
                      3:  * All rights reserved.
                      4:  *
                      5:  * Redistribution and use in source and binary forms, with or without
                      6:  * modification, are permitted provided that the following conditions
                      7:  * are met:
                      8:  * 1. Redistributions of source code must retain the above copyright
                      9:  *    notice, this list of conditions and the following disclaimer.
                     10:  * 2. Redistributions in binary form must reproduce the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer in the
                     12:  *    documentation and/or other materials provided with the distribution.
                     13:  * 3. Neither the name of the author nor the names of any co-contributors
                     14:  *    may be used to endorse or promote products derived from this software
                     15:  *    without specific prior written permission.
                     16:  *
                     17:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
                     18:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     19:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     20:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
                     21:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     22:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     23:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     24:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     25:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     26:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     27:  * SUCH DAMAGE.
                     28:  */
                     29:
                     30: /*
                     31:  * mouse.c - ps2 mouse support
                     32:  */
                     33:
                     34: /*
                     35:  * PS/2 mouse packet
                     36:  *
                     37:  *         Bit7   Bit6   Bit5   Bit4   Bit3  Bit2   Bit1   Bit0
                     38:  *  ------ ------ ------ ------ ------ ----- ------ ------ ------
                     39:  *  Byte 1 Yovf   Xovf   Ysign  Xsign    1   MidBtn RgtBtn LftBtn
                     40:  *  Byte 2 X movement
                     41:  *  Byte 3 Y movement
                     42:  */
                     43:
                     44: #include <driver.h>
                     45: #include <cpu.h>
                     46:
                     47: #include "kmc.h"
                     48:
                     49: /* #define DEBUG_MOUSE */
                     50:
                     51: #ifdef DEBUG_MOUSE
                     52: #define mou_printf(fmt, args...)       printk("%s: " fmt, __FUNCTION__ , ## args)
                     53: #else
                     54: #define mou_printf(fmt, args...)       do {} while (0)
                     55: #endif
                     56:
                     57:
                     58: #define MOUSE_IRQ      12
                     59:
                     60: static int mouse_init(void);
                     61: static int mouse_open(device_t, int);
                     62: static int mouse_close(device_t);
                     63: static int mouse_read(device_t, char *, size_t *, int);
                     64:
                     65: /*
                     66:  * Driver structure
                     67:  */
                     68: struct driver mouse_drv = {
                     69:        /* name */      "PS/2 Mouse",
                     70:        /* order */     6,
                     71:        /* init */      mouse_init,
                     72: };
                     73:
                     74: static struct devio mouse_io = {
                     75:        /* open */      mouse_open,
                     76:        /* close */     mouse_close,
                     77:        /* read */      mouse_read,
                     78:        /* write */     NULL,
                     79:        /* ioctl */     NULL,
                     80:        /* event */     NULL,
                     81: };
                     82:
                     83: static device_t mouse_dev;     /* Mouse object */
                     84: static int mouse_irq;          /* Handle for mouse irq */
                     85: static u_char packet[3];       /* Mouse packet */
                     86: static int index = 0;
                     87:
                     88: /*
                     89:  * Write aux device command
                     90:  */
                     91: static void
                     92: aux_command(int val)
                     93: {
                     94:
                     95:        mou_printf("%x\n", val);
                     96:        wait_ibe();
                     97:        outb(0x60, KMC_CMD);
                     98:        wait_ibe();
                     99:        outb(val, KMC_DATA);
                    100: }
                    101:
                    102: /*
                    103:  * Returns 0 on success, -1 on failure.
                    104:  */
                    105: static int
                    106: aux_write(int val)
                    107: {
                    108:        int rc = -1;
                    109:
                    110:        mou_printf("val=%x\n", val);
                    111:        irq_lock();
                    112:
                    113:        /* Write the value to the device */
                    114:        wait_ibe();
                    115:        outb(0xd4, KMC_CMD);
                    116:        wait_ibe();
                    117:        outb(val, KMC_DATA);
                    118:
                    119:        /* Get the ack */
                    120:        wait_obf();
                    121:        if ((inb(KMC_STS) & 0x20) == 0x20) {
                    122:                if (inb(KMC_DATA) == 0xfa)
                    123:                        rc = 0;
                    124:        }
                    125:        irq_unlock();
                    126: #ifdef DEBUG_MOUSE
                    127:        if (rc)
                    128:                mou_printf("error val=%x\n", val);
                    129: #endif
                    130:        return rc;
                    131: }
                    132:
                    133: /*
                    134:  * Interrupt handler
                    135:  */
                    136: static int
                    137: mouse_isr(int irq)
                    138: {
                    139:        u_char dat, id;
                    140:
                    141:        if ((inb(KMC_STS) & 0x21) != 0x21)
                    142:                return 0;
                    143:
                    144:        dat = inb(KMC_DATA);
                    145:        if (dat == 0xaa) {      /* BAT comp (reconnect) ? */
                    146:                printk("BAT comp");
                    147:                index = 0;
                    148:                wait_obf();
                    149:                if ((inb(KMC_STS) & 0x20) == 0x20) {
                    150:                        id = inb(KMC_DATA);
                    151:                        printk("Mouse ID=%x\n", id);
                    152:                }
                    153:                aux_write(0xf4);        /* Enable aux device */
                    154:                return 0;
                    155:        }
                    156:
                    157:        packet[index++] = dat;
                    158:        if (index < 3)
                    159:                return 0;
                    160:        index = 0;
                    161:        mou_printf("%x:%d:%d\n", packet[0], packet[1], packet[2]);
                    162:        return 0;
                    163: }
                    164:
                    165: /*
                    166:  * Open
                    167:  */
                    168: static int
                    169: mouse_open(device_t dev, int mode)
                    170: {
                    171:        mou_printf("dev=%x\n", dev);
                    172:        return 0;
                    173: }
                    174:
                    175: /*
                    176:  * Close
                    177:  */
                    178: static int
                    179: mouse_close(device_t dev)
                    180: {
                    181:        mou_printf("dev=%x\n", dev);
                    182:        return 0;
                    183: }
                    184:
                    185: /*
                    186:  * Read
                    187:  */
                    188: static int
                    189: mouse_read(device_t dev, char *buf, size_t *nbyte, int blkno)
                    190: {
                    191:        return 0;
                    192: }
                    193:
                    194: /*
                    195:  * Initialize
                    196:  */
                    197: static int
                    198: mouse_init(void)
                    199: {
                    200:
                    201:        printk("Mouse sampling rate=100 samples/sec\n");
                    202:
                    203:        /* Create device object */
                    204:        mouse_dev = device_create(&mouse_io, "mouse", DF_CHR);
                    205:        ASSERT(mouse_dev);
                    206:
                    207:        /* Allocate IRQ */
                    208:        mouse_irq = irq_attach(MOUSE_IRQ, IPL_INPUT, 0, mouse_isr, NULL);
                    209:        ASSERT(mouse_irq != -1);
                    210:
                    211:        wait_ibe();
                    212:        outb(0xa8, KMC_CMD);    /* Enable aux */
                    213:
                    214:        aux_write(0xf3);        /* Set sample rate */
                    215:        aux_write(100);         /* 100 samples/sec */
                    216:
                    217:        aux_write(0xe8);        /* Set resolution */
                    218:        aux_write(3);           /* 8 counts per mm */
                    219:        aux_write(0xe7);        /* 2:1 scaling */
                    220:
                    221:        aux_write(0xf4);        /* Enable aux device */
                    222:        aux_command(0x47);      /* Enable controller ints */
                    223:        return 0;
                    224: }

CVSweb