Import 2.3.10pre5
[davej-history.git] / drivers / char / busmouse.c
blob70b75ecc13cb70f3c690af693437f7781096fb80
1 /*
2 * Logitech Bus Mouse Driver for Linux
3 * by James Banks
5 * Mods by Matthew Dillon
6 * calls verify_area()
7 * tracks better when X is busy or paging
9 * Heavily modified by David Giller
10 * changed from queue- to counter- driven
11 * hacked out a (probably incorrect) mouse_select
13 * Modified again by Nathan Laredo to interface with
14 * 0.96c-pl1 IRQ handling changes (13JUL92)
15 * didn't bother touching select code.
17 * Modified the select() code blindly to conform to the VFS
18 * requirements. 92.07.14 - Linus. Somebody should test it out.
20 * Modified by Johan Myreen to make room for other mice (9AUG92)
21 * removed assignment chr_fops[10] = &mouse_fops; see mouse.c
22 * renamed mouse_fops => bus_mouse_fops, made bus_mouse_fops public.
23 * renamed this file mouse.c => busmouse.c
25 * Minor addition by Cliff Matthews
26 * added fasync support
28 * Modularised 6-Sep-95 Philip Blundell <pjb27@cam.ac.uk>
30 * Replaced dumb busy loop with udelay() 16 Nov 95
31 * Nathan Laredo <laredo@gnu.ai.mit.edu>
33 * Track I/O ports with request_region(). 12 Dec 95 Philip Blundell
36 #include <linux/module.h>
38 #include <linux/kernel.h>
39 #include <linux/sched.h>
40 #include <linux/busmouse.h>
41 #include <linux/signal.h>
42 #include <linux/errno.h>
43 #include <linux/mm.h>
44 #include <linux/poll.h>
45 #include <linux/miscdevice.h>
46 #include <linux/random.h>
47 #include <linux/delay.h>
48 #include <linux/ioport.h>
49 #include <linux/init.h>
51 #include <asm/io.h>
52 #include <asm/uaccess.h>
53 #include <asm/system.h>
54 #include <asm/irq.h>
56 static struct mouse_status mouse;
57 static int mouse_irq = MOUSE_IRQ;
59 #ifdef MODULE
60 MODULE_PARM(mouse_irq, "i");
61 #endif
63 void __init bmouse_setup(char *str, int *ints)
65 if (ints[0] > 0)
66 mouse_irq=ints[1];
69 static void mouse_interrupt(int irq, void *dev_id, struct pt_regs *regs)
71 char dx, dy;
72 unsigned char buttons;
74 outb(MSE_READ_X_LOW, MSE_CONTROL_PORT);
75 dx = (inb(MSE_DATA_PORT) & 0xf);
76 outb(MSE_READ_X_HIGH, MSE_CONTROL_PORT);
77 dx |= (inb(MSE_DATA_PORT) & 0xf) << 4;
78 outb(MSE_READ_Y_LOW, MSE_CONTROL_PORT );
79 dy = (inb(MSE_DATA_PORT) & 0xf);
80 outb(MSE_READ_Y_HIGH, MSE_CONTROL_PORT);
81 buttons = inb(MSE_DATA_PORT);
82 dy |= (buttons & 0xf) << 4;
83 buttons = ((buttons >> 5) & 0x07);
84 if (dx != 0 || dy != 0 || buttons != mouse.buttons) {
85 add_mouse_randomness((buttons << 16) + (dy << 8) + dx);
86 mouse.buttons = buttons;
87 mouse.dx += dx;
88 mouse.dy -= dy;
89 mouse.ready = 1;
90 wake_up_interruptible(&mouse.wait);
93 * keep dx/dy reasonable, but still able to track when X (or
94 * whatever) must page or is busy (i.e. long waits between
95 * reads)
97 if (mouse.dx < -2048)
98 mouse.dx = -2048;
99 if (mouse.dx > 2048)
100 mouse.dx = 2048;
102 if (mouse.dy < -2048)
103 mouse.dy = -2048;
104 if (mouse.dy > 2048)
105 mouse.dy = 2048;
107 if (mouse.fasyncptr)
108 kill_fasync(mouse.fasyncptr, SIGIO);
110 MSE_INT_ON();
113 static int fasync_mouse(int fd, struct file *filp, int on)
115 int retval;
117 retval = fasync_helper(fd, filp, on, &mouse.fasyncptr);
118 if (retval < 0)
119 return retval;
120 return 0;
124 * close access to the mouse
127 static int close_mouse(struct inode * inode, struct file * file)
129 fasync_mouse(-1, file, 0);
130 if (--mouse.active)
131 return 0;
132 MSE_INT_OFF();
133 free_irq(mouse_irq, NULL);
134 MOD_DEC_USE_COUNT;
135 return 0;
139 * open access to the mouse
142 static int open_mouse(struct inode * inode, struct file * file)
144 if (!mouse.present)
145 return -EINVAL;
146 if (mouse.active++)
147 return 0;
148 if (request_irq(mouse_irq, mouse_interrupt, 0, "busmouse", NULL)) {
149 mouse.active--;
150 return -EBUSY;
152 mouse.ready = 0;
153 mouse.dx = 0;
154 mouse.dy = 0;
155 mouse.buttons = 0x87;
156 MOD_INC_USE_COUNT;
157 MSE_INT_ON();
158 return 0;
162 * writes are disallowed
165 static ssize_t write_mouse(struct file * file,
166 const char * buffer, size_t count, loff_t *ppos)
168 return -EINVAL;
172 * read mouse data. Currently never blocks.
175 static ssize_t read_mouse(struct file * file,
176 char * buffer, size_t count, loff_t *ppos)
178 int r;
179 int dx;
180 int dy;
181 unsigned char buttons;
182 /* long flags; */
184 if (count < 3)
185 return -EINVAL;
186 if ((r = verify_area(VERIFY_WRITE, buffer, count)))
187 return r;
188 if (!mouse.ready)
189 return -EAGAIN;
192 * Obtain the current mouse parameters and limit as appropriate for
193 * the return data format. Interrupts are only disabled while
194 * obtaining the parameters, NOT during the puts_fs_byte() calls,
195 * so paging in put_user() does not effect mouse tracking.
198 /* save_flags(flags); cli(); */
199 disable_irq(mouse_irq);
200 dx = mouse.dx;
201 dy = mouse.dy;
202 if (dx < -127)
203 dx = -127;
204 if (dx > 127)
205 dx = 127;
206 if (dy < -127)
207 dy = -127;
208 if (dy > 127)
209 dy = 127;
210 buttons = mouse.buttons;
211 mouse.dx -= dx;
212 mouse.dy -= dy;
213 mouse.ready = 0;
214 enable_irq(mouse_irq);
215 /* restore_flags(flags); */
217 put_user(buttons | 0x80, buffer);
218 put_user((char)dx, buffer + 1);
219 put_user((char)dy, buffer + 2);
220 for (r = 3; r < count; r++)
221 put_user(0x00, buffer + r);
222 return r;
226 * poll for mouse input
228 static unsigned int mouse_poll(struct file *file, poll_table * wait)
230 poll_wait(file, &mouse.wait, wait);
231 if (mouse.ready)
232 return POLLIN | POLLRDNORM;
233 return 0;
236 struct file_operations bus_mouse_fops = {
237 NULL, /* mouse_seek */
238 read_mouse,
239 write_mouse,
240 NULL, /* mouse_readdir */
241 mouse_poll, /* mouse_poll */
242 NULL, /* mouse_ioctl */
243 NULL, /* mouse_mmap */
244 open_mouse,
245 NULL, /* flush */
246 close_mouse,
247 NULL,
248 fasync_mouse,
251 static struct miscdevice bus_mouse = {
252 LOGITECH_BUSMOUSE, "busmouse", &bus_mouse_fops
255 int __init bus_mouse_init(void)
257 if (check_region(LOGIBM_BASE, LOGIBM_EXTENT)) {
258 mouse.present = 0;
259 return -EIO;
262 outb(MSE_CONFIG_BYTE, MSE_CONFIG_PORT);
263 outb(MSE_SIGNATURE_BYTE, MSE_SIGNATURE_PORT);
264 udelay(100L); /* wait for reply from mouse */
265 if (inb(MSE_SIGNATURE_PORT) != MSE_SIGNATURE_BYTE) {
266 mouse.present = 0;
267 return -EIO;
269 outb(MSE_DEFAULT_MODE, MSE_CONFIG_PORT);
270 MSE_INT_OFF();
272 request_region(LOGIBM_BASE, LOGIBM_EXTENT, "busmouse");
274 mouse.present = 1;
275 mouse.active = 0;
276 mouse.ready = 0;
277 mouse.buttons = 0x87;
278 mouse.dx = 0;
279 mouse.dy = 0;
280 init_waitqueue_head(&mouse.wait);
281 printk(KERN_INFO "Logitech bus mouse detected, using IRQ %d.\n",
282 mouse_irq);
283 misc_register(&bus_mouse);
284 return 0;
287 #ifdef MODULE
289 int init_module(void)
291 return bus_mouse_init();
294 void cleanup_module(void)
296 misc_deregister(&bus_mouse);
297 release_region(LOGIBM_BASE, LOGIBM_EXTENT);
299 #endif