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