ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / input / mouse / logibm.c
1 /*
2  * $Id: logibm.c,v 1.11 2001/09/25 10:12:07 vojtech Exp $
3  *
4  *  Copyright (c) 1999-2001 Vojtech Pavlik
5  *
6  *  Based on the work of:
7  *      James Banks             Matthew Dillon
8  *      David Giller            Nathan Laredo
9  *      Linus Torvalds          Johan Myreen
10  *      Cliff Matthews          Philip Blundell
11  *      Russell King
12  */
13
14 /*
15  * Logitech Bus Mouse Driver for Linux
16  */
17
18 /*
19  * This program is free software; you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation; either version 2 of the License, or 
22  * (at your option) any later version.
23  * 
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27  * GNU General Public License for more details.
28  * 
29  * You should have received a copy of the GNU General Public License
30  * along with this program; if not, write to the Free Software
31  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32  * 
33  * Should you need to contact me, the author, you can do so either by
34  * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
35  * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
36  */
37
38 #include <linux/module.h>
39 #include <linux/moduleparam.h>
40 #include <linux/delay.h>
41 #include <linux/ioport.h>
42 #include <linux/init.h>
43 #include <linux/input.h>
44 #include <linux/interrupt.h>
45
46 #include <asm/io.h>
47 #include <asm/irq.h>
48
49 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
50 MODULE_DESCRIPTION("Logitech busmouse driver");
51 MODULE_LICENSE("GPL");
52
53 #define LOGIBM_BASE             0x23c
54 #define LOGIBM_EXTENT           4
55
56 #define LOGIBM_DATA_PORT        LOGIBM_BASE + 0
57 #define LOGIBM_SIGNATURE_PORT   LOGIBM_BASE + 1
58 #define LOGIBM_CONTROL_PORT     LOGIBM_BASE + 2
59 #define LOGIBM_CONFIG_PORT      LOGIBM_BASE + 3
60
61 #define LOGIBM_ENABLE_IRQ       0x00
62 #define LOGIBM_DISABLE_IRQ      0x10
63 #define LOGIBM_READ_X_LOW       0x80
64 #define LOGIBM_READ_X_HIGH      0xa0
65 #define LOGIBM_READ_Y_LOW       0xc0
66 #define LOGIBM_READ_Y_HIGH      0xe0
67
68 #define LOGIBM_DEFAULT_MODE     0x90
69 #define LOGIBM_CONFIG_BYTE      0x91
70 #define LOGIBM_SIGNATURE_BYTE   0xa5
71
72 #define LOGIBM_IRQ              5
73
74 static int logibm_irq = LOGIBM_IRQ;
75 module_param_named(irq, logibm_irq, uint, 0);
76 MODULE_PARM_DESC(irq, "IRQ number (5=default)");
77
78 __obsolete_setup("logibm_irq=");
79
80 static int logibm_used = 0;
81
82 static irqreturn_t logibm_interrupt(int irq, void *dev_id, struct pt_regs *regs);
83
84 static int logibm_open(struct input_dev *dev)
85 {
86         if (logibm_used++)
87                 return 0;
88         if (request_irq(logibm_irq, logibm_interrupt, 0, "logibm", NULL)) {
89                 logibm_used--;
90                 printk(KERN_ERR "logibm.c: Can't allocate irq %d\n", logibm_irq);
91                 return -EBUSY;
92         }
93         outb(LOGIBM_ENABLE_IRQ, LOGIBM_CONTROL_PORT);
94         return 0;
95 }
96
97 static void logibm_close(struct input_dev *dev)
98 {
99         if (--logibm_used)
100                 return;
101         outb(LOGIBM_DISABLE_IRQ, LOGIBM_CONTROL_PORT);
102         free_irq(logibm_irq, NULL);
103 }
104
105 static struct input_dev logibm_dev = {
106         .evbit  = { BIT(EV_KEY) | BIT(EV_REL) },
107         .keybit = { [LONG(BTN_LEFT)] = BIT(BTN_LEFT) | BIT(BTN_MIDDLE) | BIT(BTN_RIGHT) },
108         .relbit = { BIT(REL_X) | BIT(REL_Y) },
109         .open   = logibm_open,
110         .close  = logibm_close,
111         .name   = "Logitech bus mouse",
112         .phys   = "isa023c/input0",
113         .id     = {
114                 .bustype = BUS_ISA,
115                 .vendor  = 0x0003,
116                 .product = 0x0001,
117                 .version = 0x0100,
118         },
119 };
120
121 static irqreturn_t logibm_interrupt(int irq, void *dev_id, struct pt_regs *regs)
122 {
123         char dx, dy;
124         unsigned char buttons;
125
126         outb(LOGIBM_READ_X_LOW, LOGIBM_CONTROL_PORT);
127         dx = (inb(LOGIBM_DATA_PORT) & 0xf);
128         outb(LOGIBM_READ_X_HIGH, LOGIBM_CONTROL_PORT);
129         dx |= (inb(LOGIBM_DATA_PORT) & 0xf) << 4;
130         outb(LOGIBM_READ_Y_LOW, LOGIBM_CONTROL_PORT);
131         dy = (inb(LOGIBM_DATA_PORT) & 0xf);
132         outb(LOGIBM_READ_Y_HIGH, LOGIBM_CONTROL_PORT);
133         buttons = inb(LOGIBM_DATA_PORT);
134         dy |= (buttons & 0xf) << 4;
135         buttons = ~buttons >> 5;
136
137         input_regs(&logibm_dev, regs);
138         input_report_rel(&logibm_dev, REL_X, dx);
139         input_report_rel(&logibm_dev, REL_Y, dy);
140         input_report_key(&logibm_dev, BTN_RIGHT,  buttons & 1);
141         input_report_key(&logibm_dev, BTN_MIDDLE, buttons & 2);
142         input_report_key(&logibm_dev, BTN_LEFT,   buttons & 4);
143         input_sync(&logibm_dev);
144
145         outb(LOGIBM_ENABLE_IRQ, LOGIBM_CONTROL_PORT);
146         return IRQ_HANDLED;
147 }
148
149 static int __init logibm_init(void)
150 {
151         if (!request_region(LOGIBM_BASE, LOGIBM_EXTENT, "logibm")) {
152                 printk(KERN_ERR "logibm.c: Can't allocate ports at %#x\n", LOGIBM_BASE);
153                 return -EBUSY;
154         }
155
156         outb(LOGIBM_CONFIG_BYTE, LOGIBM_CONFIG_PORT);
157         outb(LOGIBM_SIGNATURE_BYTE, LOGIBM_SIGNATURE_PORT);
158         udelay(100);
159
160         if (inb(LOGIBM_SIGNATURE_PORT) != LOGIBM_SIGNATURE_BYTE) {
161                 release_region(LOGIBM_BASE, LOGIBM_EXTENT);
162                 printk(KERN_ERR "logibm.c: Didn't find Logitech busmouse at %#x\n", LOGIBM_BASE);
163                 return -ENODEV;
164         }
165
166         outb(LOGIBM_DEFAULT_MODE, LOGIBM_CONFIG_PORT);
167         outb(LOGIBM_DISABLE_IRQ, LOGIBM_CONTROL_PORT);
168
169         input_register_device(&logibm_dev);
170         
171         printk(KERN_INFO "input: Logitech bus mouse at %#x irq %d\n", LOGIBM_BASE, logibm_irq);
172
173         return 0;
174 }
175
176 static void __exit logibm_exit(void)
177 {
178         input_unregister_device(&logibm_dev);
179         release_region(LOGIBM_BASE, LOGIBM_EXTENT);
180 }
181
182 module_init(logibm_init);
183 module_exit(logibm_exit);