Import 2.1.118
[davej-history.git] / drivers / char / adbmouse.c
blob43930ff1b5349f17febb28567a8bb8b1e3ee18ca
1 /*
2 * Macintosh ADB Mouse driver for Linux
4 * 27 Oct 1997 Michael Schmitz
5 * logitech fixes by anthony tong
7 * Apple mouse protocol according to:
9 * Device code shamelessly stolen from:
12 * Atari Mouse Driver for Linux
13 * by Robert de Vries (robert@and.nl) 19Jul93
15 * 16 Nov 1994 Andreas Schwab
16 * Compatibility with busmouse
17 * Support for three button mouse (shamelessly stolen from MiNT)
18 * third button wired to one of the joystick directions on joystick 1
20 * 1996/02/11 Andreas Schwab
21 * Module support
22 * Allow multiple open's
25 #include <linux/module.h>
27 #include <linux/sched.h>
28 #include <linux/errno.h>
29 #include <linux/miscdevice.h>
30 #include <linux/mm.h>
31 #include <linux/random.h>
32 #include <linux/poll.h>
33 #include <linux/init.h>
35 #include <asm/adb_mouse.h>
36 #include <asm/segment.h>
37 #include <asm/processor.h>
39 static struct mouse_status mouse;
40 static int adb_mouse_x_threshold = 2, adb_mouse_y_threshold = 2;
41 static int adb_mouse_buttons = 0;
43 extern void (*adb_mouse_interrupt_hook) (char *, int);
45 extern int console_loglevel;
48 * XXX: need to figure out what ADB mouse packets mean ...
49 * This is the stuff stolen from the Atari driver ...
51 static void adb_mouse_interrupt(char *buf, int nb)
53 static int buttons = 7;
56 Handler 1 -- 100cpi original Apple mouse protocol.
57 Handler 2 -- 200cpi original Apple mouse protocol.
59 For Apple's standard one-button mouse protocol the data array will
60 contain the following values:
62 BITS COMMENTS
63 data[0] = 0000 0000 ADB packet identifer.
64 data[1] = ???? ???? (?)
65 data[2] = ???? ??00 Bits 0-1 should be zero for a mouse device.
66 data[3] = bxxx xxxx First button and x-axis motion.
67 data[4] = byyy yyyy Second button and y-axis motion.
69 NOTE: data[0] is confirmed by the parent function and need not be
70 checked here.
74 Handler 4 -- Apple Extended mouse protocol.
76 For Apple's 3-button mouse protocol the data array will contain the
77 following values:
79 BITS COMMENTS
80 data[0] = 0000 0000 ADB packet identifer.
81 data[1] = 0100 0000 Extended protocol register.
82 Bits 6-7 are the device id, which should be 1.
83 Bits 4-5 are resolution which is in "units/inch".
84 The Logitech MouseMan returns these bits clear but it has
85 200/300cpi resolution.
86 Bits 0-3 are unique vendor id.
87 data[2] = 0011 1100 Bits 0-1 should be zero for a mouse device.
88 Bits 2-3 should be 8 + 4.
89 Bits 4-7 should be 3 for a mouse device.
90 data[3] = bxxx xxxx Left button and x-axis motion.
91 data[4] = byyy yyyy Second button and y-axis motion.
92 data[5] = byyy bxxx Third button and fourth button.
93 Y is additiona. high bits of y-axis motion.
94 X is additional high bits of x-axis motion.
96 NOTE: data[0] and data[2] are confirmed by the parent function and
97 need not be checked here.
101 * 'buttons' here means 'button down' states!
102 * Button 1 (left) : bit 2, busmouse button 3
103 * Button 2 (right) : bit 0, busmouse button 1
104 * Button 3 (middle): bit 1, busmouse button 2
107 /* x/y and buttons swapped */
109 if (nb > 0) { /* real packet : use buttons? */
110 if (console_loglevel >= 8)
111 printk("adb_mouse: real data; ");
112 /* button 1 (left, bit 2) : always significant ! */
113 buttons = (buttons&3) | (buf[1] & 0x80 ? 4 : 0); /* 1+2 unchanged */
114 /* button 2 (middle) */
115 buttons = (buttons&5) | (buf[2] & 0x80 ? 2 : 0); /* 2+3 unchanged */
116 /* button 3 (right) present?
117 * on a logitech mouseman, the right and mid buttons sometimes behave
118 * strangely until they both have been pressed after booting. */
119 /* data valid only if extended mouse format ! (buf[3] = 0 else) */
120 if ( nb == 6 )
121 buttons = (buttons&6) | (buf[3] & 0x80 ? 1 : 0); /* 1+3 unchanged */
122 } else { /* fake packet : use 2+3 */
123 if (console_loglevel >= 8)
124 printk("adb_mouse: fake data; ");
125 /* we only see state changes here, but the fake driver takes care
126 * to preserve state... button 1 state must stay unchanged! */
127 buttons = (buttons&4) | ((buf[2] & 0x80 ? 1 : 0) |
128 (buf[3] & 0x80 ? 2 : 0));
131 add_mouse_randomness(((~buttons & 7) << 16) + ((buf[2]&0x7f) << 8) + (buf[1]&0x7f));
132 mouse.buttons = buttons & 7;
133 mouse.dx += ((buf[2]&0x7f) < 64 ? (buf[2]&0x7f) : (buf[2]&0x7f)-128 );
134 mouse.dy -= ((buf[1]&0x7f) < 64 ? (buf[1]&0x7f) : (buf[1]&0x7f)-128 );
136 if (console_loglevel >= 8)
137 printk(" %X %X %X buttons %x dx %d dy %d \n",
138 buf[1], buf[2], buf[3], mouse.buttons, mouse.dx, mouse.dy);
140 mouse.ready = 1;
141 wake_up_interruptible(&mouse.wait);
142 if (mouse.fasyncptr)
143 kill_fasync(mouse.fasyncptr, SIGIO);
147 static int fasync_mouse(int fd, struct file *filp, int on)
149 int retval;
151 retval = fasync_helper(fd, filp, on, &mouse.fasyncptr);
152 if (retval < 0)
153 return retval;
154 return 0;
157 static int release_mouse(struct inode *inode, struct file *file)
159 fasync_mouse(-1, file, 0);
160 if (--mouse.active)
161 return 0;
163 adb_mouse_interrupt_hook = NULL;
164 MOD_DEC_USE_COUNT;
165 return 0;
168 static int open_mouse(struct inode *inode, struct file *file)
170 if (mouse.active++)
171 return 0;
173 mouse.ready = 0;
175 mouse.dx = mouse.dy = 0;
176 adb_mouse_buttons = 0;
177 MOD_INC_USE_COUNT;
178 adb_mouse_interrupt_hook = adb_mouse_interrupt;
179 return 0;
182 static ssize_t write_mouse(struct file *file, const char *buffer,
183 size_t count, loff_t *ppos)
185 return -EINVAL;
188 static ssize_t read_mouse(struct file *file, char *buffer, size_t count,
189 loff_t *ppos)
191 int dx, dy, buttons;
193 if (count < 3)
194 return -EINVAL;
195 if (!mouse.ready)
196 return -EAGAIN;
197 dx = mouse.dx;
198 dy = mouse.dy;
199 buttons = mouse.buttons;
200 if (dx > 127)
201 dx = 127;
202 else if (dx < -128)
203 dx = -128;
204 if (dy > 127)
205 dy = 127;
206 else if (dy < -128)
207 dy = -128;
208 mouse.dx -= dx;
209 mouse.dy -= dy;
210 if (mouse.dx == 0 && mouse.dy == 0)
211 mouse.ready = 0;
212 if (put_user(buttons | 0x80, buffer++) ||
213 put_user((char) dx, buffer++) ||
214 put_user((char) dy, buffer++))
215 return -EFAULT;
216 if (count > 3)
217 if (clear_user(buffer, count - 3))
218 return -EFAULT;
219 return count;
222 static unsigned int mouse_poll(struct file *file, poll_table *wait)
224 poll_wait(file, &mouse.wait, wait);
225 if (mouse.ready)
226 return POLLIN | POLLRDNORM;
227 return 0;
230 struct file_operations adb_mouse_fops = {
231 NULL, /* mouse_seek */
232 read_mouse,
233 write_mouse,
234 NULL, /* mouse_readdir */
235 mouse_poll,
236 NULL, /* mouse_ioctl */
237 NULL, /* mouse_mmap */
238 open_mouse,
239 NULL, /* flush */
240 release_mouse,
241 NULL,
242 fasync_mouse,
245 #define ADB_MOUSE_MINOR 10
247 static struct miscdevice adb_mouse = {
248 ADB_MOUSE_MINOR, "adbmouse", &adb_mouse_fops
251 __initfunc(int adb_mouse_init(void))
253 mouse.active = 0;
254 mouse.ready = 0;
255 mouse.wait = NULL;
257 if ( (_machine != _MACH_chrp) && (_machine != _MACH_Pmac) )
258 return -ENODEV;
259 printk(KERN_INFO "Macintosh ADB mouse installed.\n");
260 misc_register(&adb_mouse);
261 return 0;
265 #define MIN_THRESHOLD 1
266 #define MAX_THRESHOLD 20 /* more seems not reasonable... */
268 __initfunc(void adb_mouse_setup(char *str, int *ints))
270 if (ints[0] < 1) {
271 printk( "adb_mouse_setup: no arguments!\n" );
272 return;
274 else if (ints[0] > 2) {
275 printk( "adb_mouse_setup: too many arguments\n" );
278 if (ints[1] < MIN_THRESHOLD || ints[1] > MAX_THRESHOLD)
279 printk( "adb_mouse_setup: bad threshold value (ignored)\n" );
280 else {
281 adb_mouse_x_threshold = ints[1];
282 adb_mouse_y_threshold = ints[1];
283 if (ints[0] > 1) {
284 if (ints[2] < MIN_THRESHOLD || ints[2] > MAX_THRESHOLD)
285 printk("adb_mouse_setup: bad threshold value (ignored)\n" );
286 else
287 adb_mouse_y_threshold = ints[2];
293 #ifdef MODULE
294 #include <asm/setup.h>
296 int init_module(void)
298 return adb_mouse_init();
301 void cleanup_module(void)
303 misc_deregister(&adb_mouse);
305 #endif