Import 2.1.118
[davej-history.git] / drivers / char / msbusmouse.c
blobb49bfe9a6208f77a9c6bd2478f0daa7fcba024e9
1 /*
2 * Microsoft busmouse driver based on Logitech driver (see busmouse.c)
4 * Microsoft BusMouse support by Teemu Rantanen (tvr@cs.hut.fi) (02AUG92)
6 * Microsoft Bus Mouse support modified by Derrick Cole (cole@concert.net)
7 * 8/28/92
9 * Microsoft Bus Mouse support folded into 0.97pl4 code
10 * by Peter Cervasio (pete%q106fm.uucp@wupost.wustl.edu) (08SEP92)
11 * Changes: Logitech and Microsoft support in the same kernel.
12 * Defined new constants in busmouse.h for MS mice.
13 * Added int mse_busmouse_type to distinguish busmouse types
14 * Added a couple of new functions to handle differences in using
15 * MS vs. Logitech (where the int variable wasn't appropriate).
17 * Modified by Peter Cervasio (address above) (26SEP92)
18 * Changes: Included code to (properly?) detect when a Microsoft mouse is
19 * really attached to the machine. Don't know what this does to
20 * Logitech bus mice, but all it does is read ports.
22 * Modified by Christoph Niemann (niemann@rubdv15.etdv.ruhr-uni-bochum.de)
23 * Changes: Better interrupt-handler (like in busmouse.c).
24 * Some changes to reduce code-size.
25 * Changed detection code to use inb_p() instead of doing empty
26 * loops to delay i/o.
28 * Modularised 8-Sep-95 Philip Blundell <pjb27@cam.ac.uk>
30 * version 0.3b
33 #include <linux/module.h>
35 #include <linux/kernel.h>
36 #include <linux/ioport.h>
37 #include <linux/sched.h>
38 #include <linux/busmouse.h>
39 #include <linux/signal.h>
40 #include <linux/errno.h>
41 #include <linux/miscdevice.h>
42 #include <linux/random.h>
43 #include <linux/poll.h>
44 #include <linux/init.h>
46 #include <asm/io.h>
47 #include <asm/uaccess.h>
48 #include <asm/system.h>
49 #include <asm/irq.h>
51 static struct mouse_status mouse;
52 static int mouse_irq = MOUSE_IRQ;
54 __initfunc(void msmouse_setup(char *str, int *ints))
56 if (ints[0] > 0)
57 mouse_irq=ints[1];
60 static void ms_mouse_interrupt(int irq, void *dev_id, struct pt_regs * regs)
62 char dx, dy;
63 unsigned char buttons;
65 outb(MS_MSE_COMMAND_MODE, MS_MSE_CONTROL_PORT);
66 outb((inb(MS_MSE_DATA_PORT) | 0x20), MS_MSE_DATA_PORT);
68 outb(MS_MSE_READ_X, MS_MSE_CONTROL_PORT);
69 dx = inb(MS_MSE_DATA_PORT);
71 outb(MS_MSE_READ_Y, MS_MSE_CONTROL_PORT);
72 dy = inb(MS_MSE_DATA_PORT);
74 outb(MS_MSE_READ_BUTTONS, MS_MSE_CONTROL_PORT);
75 buttons = ~(inb(MS_MSE_DATA_PORT)) & 0x07;
77 outb(MS_MSE_COMMAND_MODE, MS_MSE_CONTROL_PORT);
78 outb((inb(MS_MSE_DATA_PORT) & 0xdf), MS_MSE_DATA_PORT);
80 if (dx != 0 || dy != 0 || buttons != mouse.buttons || ((~buttons) & 0x07)) {
81 add_mouse_randomness((buttons << 16) + (dy << 8) + dx);
82 mouse.buttons = buttons;
83 mouse.dx += dx;
84 mouse.dy += dy;
85 mouse.ready = 1;
86 wake_up_interruptible(&mouse.wait);
87 if (mouse.fasyncptr)
88 kill_fasync(mouse.fasyncptr, SIGIO);
92 static int fasync_mouse(int fd, struct file *filp, int on)
94 int retval;
96 retval = fasync_helper(fd, filp, on, &mouse.fasyncptr);
97 if (retval < 0)
98 return retval;
99 return 0;
102 static int release_mouse(struct inode * inode, struct file * file)
104 fasync_mouse(-1, file, 0);
105 if (--mouse.active)
106 return 0;
107 MS_MSE_INT_OFF();
108 mouse.ready = 0;
109 free_irq(mouse_irq, NULL);
110 MOD_DEC_USE_COUNT;
111 return 0;
114 static int open_mouse(struct inode * inode, struct file * file)
116 if (!mouse.present)
117 return -EINVAL;
118 if (mouse.active++)
119 return 0;
120 if (request_irq(mouse_irq, ms_mouse_interrupt, 0, "MS Busmouse", NULL)) {
121 mouse.active--;
122 return -EBUSY;
124 mouse.ready = mouse.dx = mouse.dy = 0;
125 mouse.buttons = 0x80;
126 outb(MS_MSE_START, MS_MSE_CONTROL_PORT);
127 MOD_INC_USE_COUNT;
128 MS_MSE_INT_ON();
129 return 0;
132 static ssize_t write_mouse(struct file * file,
133 const char * buffer, size_t count, loff_t *ppos)
135 return -EINVAL;
138 static ssize_t read_mouse(struct file * file,
139 char * buffer, size_t count, loff_t *ppos)
141 int i, dx, dy;
143 if (count < 3)
144 return -EINVAL;
145 if (!mouse.ready)
146 return -EAGAIN;
147 put_user(mouse.buttons | 0x80, buffer);
148 dx = mouse.dx < -127 ? -127 : mouse.dx > 127 ? 127 : mouse.dx;
149 dy = mouse.dy < -127 ? 127 : mouse.dy > 127 ? -127 : -mouse.dy;
150 put_user((char)dx, buffer + 1);
151 put_user((char)dy, buffer + 2);
152 for (i = 3; i < count; i++)
153 put_user(0x00, buffer + i);
154 mouse.dx -= dx;
155 mouse.dy += dy;
156 mouse.ready = 0;
157 return i;
160 static unsigned int mouse_poll(struct file *file, poll_table * wait)
162 poll_wait(file, &mouse.wait, wait);
163 if (mouse.ready)
164 return POLLIN | POLLRDNORM;
165 return 0;
168 struct file_operations ms_bus_mouse_fops = {
169 NULL, /* mouse_seek */
170 read_mouse,
171 write_mouse,
172 NULL, /* mouse_readdir */
173 mouse_poll, /* mouse_poll */
174 NULL, /* mouse_ioctl */
175 NULL, /* mouse_mmap */
176 open_mouse,
177 NULL, /* flush */
178 release_mouse,
179 NULL,
180 fasync_mouse,
183 static struct miscdevice ms_bus_mouse = {
184 MICROSOFT_BUSMOUSE, "msbusmouse", &ms_bus_mouse_fops
187 __initfunc(int ms_bus_mouse_init(void))
189 int mse_byte, i;
191 mouse.present = mouse.active = mouse.ready = 0;
192 mouse.buttons = 0x80;
193 mouse.dx = mouse.dy = 0;
194 mouse.wait = NULL;
196 if (check_region(MS_MSE_CONTROL_PORT, 0x04))
197 return -ENODEV;
199 if (inb_p(MS_MSE_SIGNATURE_PORT) == 0xde) {
201 mse_byte = inb_p(MS_MSE_SIGNATURE_PORT);
203 for (i = 0; i < 4; i++) {
204 if (inb_p(MS_MSE_SIGNATURE_PORT) == 0xde) {
205 if (inb_p(MS_MSE_SIGNATURE_PORT) == mse_byte)
206 mouse.present = 1;
207 else
208 mouse.present = 0;
209 } else
210 mouse.present = 0;
213 if (mouse.present == 0)
214 return -EIO;
215 MS_MSE_INT_OFF();
216 request_region(MS_MSE_CONTROL_PORT, 0x04, "MS Busmouse");
217 printk(KERN_INFO "Microsoft BusMouse detected and installed.\n");
218 misc_register(&ms_bus_mouse);
219 return 0;
222 #ifdef MODULE
223 int init_module(void)
225 return ms_bus_mouse_init();
228 void cleanup_module(void)
230 misc_deregister(&ms_bus_mouse);
231 release_region(MS_MSE_CONTROL_PORT, 0x04);
233 #endif