Import 2.1.118
[davej-history.git] / drivers / char / macmouse.c
blobb9236bb9375ef8815712b2251d89f8daee84964a
1 /*
2 * Macintosh ADB Mouse driver for Linux
4 * 27 Oct 1997 Michael Schmitz
6 * Apple mouse protocol according to:
8 * Device code shamelessly stolen from:
9 */
11 * Atari Mouse Driver for Linux
12 * by Robert de Vries (robert@and.nl) 19Jul93
14 * 16 Nov 1994 Andreas Schwab
15 * Compatibility with busmouse
16 * Support for three button mouse (shamelessly stolen from MiNT)
17 * third button wired to one of the joystick directions on joystick 1
19 * 1996/02/11 Andreas Schwab
20 * Module support
21 * Allow multiple open's
24 #include <linux/module.h>
26 #include <linux/sched.h>
27 #include <linux/errno.h>
28 #include <linux/miscdevice.h>
29 #include <linux/mm.h>
30 #include <linux/random.h>
31 #include <linux/poll.h>
32 #include <linux/init.h>
34 #include <asm/setup.h>
35 #include <asm/mac_mouse.h>
36 #include <asm/segment.h>
37 #include <asm/uaccess.h>
39 static struct mouse_status mouse;
40 static int mac_mouse_x_threshold = 2, mac_mouse_y_threshold = 2;
41 static int mac_mouse_buttons = 0;
43 extern void (*mac_mouse_interrupt_hook) (char *, int);
44 extern int mac_emulate_button2;
45 extern int mac_emulate_button3;
47 extern int console_loglevel;
50 * XXX: need to figure out what ADB mouse packets mean ...
51 * This is the stuff stolen from the Atari driver ...
53 static void mac_mouse_interrupt(char *buf, int nb)
55 static int buttons = 7; /* all mouse buttons _up_ !! */
58 Handler 1 -- 100cpi original Apple mouse protocol.
59 Handler 2 -- 200cpi original Apple mouse protocol.
61 For Apple's standard one-button mouse protocol the data array will
62 contain the following values:
64 BITS COMMENTS
65 data[0] = 0000 0000 ADB packet identifer.
66 data[1] = ???? ???? (?)
67 data[2] = ???? ??00 Bits 0-1 should be zero for a mouse device.
68 data[3] = bxxx xxxx First button and x-axis motion.
69 data[4] = byyy yyyy Second button and y-axis motion.
71 NOTE: data[0] is confirmed by the parent function and need not be
72 checked here.
76 Handler 4 -- Apple Extended mouse protocol.
78 For Apple's 3-button mouse protocol the data array will contain the
79 following values:
81 BITS COMMENTS
82 data[0] = 0000 0000 ADB packet identifer.
83 data[1] = 0100 0000 Extended protocol register.
84 Bits 6-7 are the device id, which should be 1.
85 Bits 4-5 are resolution which is in "units/inch".
86 The Logitech MouseMan returns these bits clear but it has
87 200/300cpi resolution.
88 Bits 0-3 are unique vendor id.
89 data[2] = 0011 1100 Bits 0-1 should be zero for a mouse device.
90 Bits 2-3 should be 8 + 4.
91 Bits 4-7 should be 3 for a mouse device.
92 data[3] = bxxx xxxx Left button and x-axis motion.
93 data[4] = byyy yyyy Second button and y-axis motion.
94 data[5] = byyy bxxx Third button and fourth button.
95 Y is additiona. high bits of y-axis motion.
96 X is additional high bits of x-axis motion.
98 NOTE: data[0] and data[2] are confirmed by the parent function and
99 need not be checked here.
103 * 'buttons' here means 'button down' states!
104 * Button 1 (left) : bit 2, busmouse button 3
105 * Button 2 (right) : bit 0, busmouse button 1
106 * Button 3 (middle): bit 1, busmouse button 2
109 /* x/y and buttons swapped */
111 if (buf[0] == 0) { /* real packet : use buttons? */
112 #ifdef DEBUG_ADBMOUSE
113 if (console_loglevel >= 8)
114 printk("mac_mouse: real data; ");
115 #endif
116 /* button 1 (left, bit 2) : always significant ! */
117 buttons = (buttons&3) | (buf[3] & 0x80 ? 4 : 0); /* 1+2 unchanged */
118 /* button 2 (right, bit 0) present ? */
119 if ( !mac_emulate_button2 )
120 buttons = (buttons&6) | (buf[4] & 0x80 ? 1 : 0); /* 2+3 unchanged */
121 /* button 2 (middle) present? */
122 /* data valid only if extended mouse format ! (buf[3] = 0 else)*/
123 if ( !mac_emulate_button3 && buf[1]&0x40 )
124 buttons = (buttons&5) | (buf[5] & 0x80 ? 2 : 0); /* 1+3 unchanged */
125 } else { /* fake packet : use 2+3 */
126 #ifdef DEBUG_ADBMOUSE
127 if (console_loglevel >= 8)
128 printk("mac_mouse: fake data; ");
129 #endif
130 /* we only see state changes here, but the fake driver takes care
131 * to preserve state... button 1 state must stay unchanged! */
132 buttons = (buttons&4) | ((buf[4] & 0x80 ? 1 : 0) | (buf[5] & 0x80 ? 2 : 0));
135 add_mouse_randomness(((~buttons & 7) << 16) + ((buf[2]&0x7f) << 8) + (buf[1]&0x7f));
136 mouse.buttons = buttons & 7;
137 mouse.dx += ((buf[4]&0x7f) < 64 ? (buf[4]&0x7f) : (buf[4]&0x7f)-128 );
138 mouse.dy -= ((buf[3]&0x7f) < 64 ? (buf[3]&0x7f) : (buf[3]&0x7f)-128 );
140 #ifdef DEBUG_ADBMOUSE
141 if (console_loglevel >= 8)
142 printk(" %X %X %X buttons %x dx %d dy %d \n",
143 buf[3], buf[4], buf[5], mouse.buttons, mouse.dx, mouse.dy);
144 #endif
146 mouse.ready = 1;
147 wake_up_interruptible(&mouse.wait);
148 if (mouse.fasyncptr)
149 kill_fasync(mouse.fasyncptr, SIGIO);
153 static int fasync_mouse(int fd, struct file *filp, int on)
155 int retval;
157 retval = fasync_helper(fd, filp, on, &mouse.fasyncptr);
158 if (retval < 0)
159 return retval;
160 return 0;
163 static int release_mouse(struct inode *inode, struct file *file)
165 fasync_mouse(-1, file, 0);
166 if (--mouse.active)
167 return 0;
169 mac_mouse_interrupt_hook = NULL;
170 MOD_DEC_USE_COUNT;
171 return 0;
174 static int open_mouse(struct inode *inode, struct file *file)
176 if (mouse.active++)
177 return 0;
179 mouse.ready = 0;
181 mouse.dx = mouse.dy = 0;
182 mac_mouse_buttons = 0;
183 MOD_INC_USE_COUNT;
184 mac_mouse_interrupt_hook = mac_mouse_interrupt;
185 return 0;
188 static ssize_t write_mouse(struct file *file, const char *buffer,
189 size_t count, loff_t *ppos)
191 return -EINVAL;
194 static ssize_t read_mouse(struct file *file, char *buffer, size_t count,
195 loff_t *ppos)
197 int dx, dy, buttons;
199 if (count < 3)
200 return -EINVAL;
201 if (!mouse.ready)
202 return -EAGAIN;
203 dx = mouse.dx;
204 dy = mouse.dy;
205 buttons = mouse.buttons;
206 if (dx > 127)
207 dx = 127;
208 else if (dx < -128)
209 dx = -128;
210 if (dy > 127)
211 dy = 127;
212 else if (dy < -128)
213 dy = -128;
214 mouse.dx -= dx;
215 mouse.dy -= dy;
216 if (mouse.dx == 0 && mouse.dy == 0)
217 mouse.ready = 0;
218 if (put_user(buttons | 0x80, buffer++) ||
219 put_user((char) dx, buffer++) ||
220 put_user((char) dy, buffer++))
221 return -EFAULT;
222 if (count > 3)
223 if (clear_user(buffer, count - 3))
224 return -EFAULT;
225 return count;
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 mac_mouse_fops = {
237 NULL, /* mouse_seek */
238 read_mouse,
239 write_mouse,
240 NULL, /* mouse_readdir */
241 mouse_poll,
242 NULL, /* mouse_ioctl */
243 NULL, /* mouse_mmap */
244 open_mouse,
245 NULL, /* flush */
246 release_mouse,
247 NULL,
248 fasync_mouse,
251 #define ADB_MOUSE_MINOR 10
253 static struct miscdevice mac_mouse = {
254 ADB_MOUSE_MINOR, "adbmouse", &mac_mouse_fops
257 __initfunc(int mac_mouse_init(void))
259 mouse.active = 0;
260 mouse.ready = 0;
261 mouse.wait = NULL;
263 if (!MACH_IS_MAC)
264 return -ENODEV;
266 printk(KERN_INFO "Macintosh ADB mouse installed.\n");
267 misc_register(&mac_mouse);
268 return 0;
272 #define MIN_THRESHOLD 1
273 #define MAX_THRESHOLD 20 /* more seems not reasonable... */
275 __initfunc(void mac_mouse_setup(char *str, int *ints))
277 if (ints[0] < 1) {
278 printk( "mac_mouse_setup: no arguments!\n" );
279 return;
281 else if (ints[0] > 2) {
282 printk( "mac_mouse_setup: too many arguments\n" );
285 if (ints[1] < MIN_THRESHOLD || ints[1] > MAX_THRESHOLD)
286 printk( "mac_mouse_setup: bad threshold value (ignored)\n" );
287 else {
288 mac_mouse_x_threshold = ints[1];
289 mac_mouse_y_threshold = ints[1];
290 if (ints[0] > 1) {
291 if (ints[2] < MIN_THRESHOLD || ints[2] > MAX_THRESHOLD)
292 printk("mac_mouse_setup: bad threshold value (ignored)\n" );
293 else
294 mac_mouse_y_threshold = ints[2];
300 #ifdef MODULE
301 #include <asm/setup.h>
303 int init_module(void)
305 return mac_mouse_init();
308 void cleanup_module(void)
310 misc_deregister(&mac_mouse);
312 #endif