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:
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
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>
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:
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
76 Handler 4 -- Apple Extended mouse protocol.
78 For Apple's 3-button mouse protocol the data array will contain the
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; ");
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; ");
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
);
147 wake_up_interruptible(&mouse
.wait
);
149 kill_fasync(mouse
.fasyncptr
, SIGIO
);
153 static int fasync_mouse(int fd
, struct file
*filp
, int on
)
157 retval
= fasync_helper(fd
, filp
, on
, &mouse
.fasyncptr
);
163 static int release_mouse(struct inode
*inode
, struct file
*file
)
165 fasync_mouse(-1, file
, 0);
169 mac_mouse_interrupt_hook
= NULL
;
174 static int open_mouse(struct inode
*inode
, struct file
*file
)
181 mouse
.dx
= mouse
.dy
= 0;
182 mac_mouse_buttons
= 0;
184 mac_mouse_interrupt_hook
= mac_mouse_interrupt
;
188 static ssize_t
write_mouse(struct file
*file
, const char *buffer
,
189 size_t count
, loff_t
*ppos
)
194 static ssize_t
read_mouse(struct file
*file
, char *buffer
, size_t count
,
205 buttons
= mouse
.buttons
;
216 if (mouse
.dx
== 0 && mouse
.dy
== 0)
218 if (put_user(buttons
| 0x80, buffer
++) ||
219 put_user((char) dx
, buffer
++) ||
220 put_user((char) dy
, buffer
++))
223 if (clear_user(buffer
, count
- 3))
228 static unsigned int mouse_poll(struct file
*file
, poll_table
*wait
)
230 poll_wait(file
, &mouse
.wait
, wait
);
232 return POLLIN
| POLLRDNORM
;
236 struct file_operations mac_mouse_fops
= {
237 NULL
, /* mouse_seek */
240 NULL
, /* mouse_readdir */
242 NULL
, /* mouse_ioctl */
243 NULL
, /* mouse_mmap */
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))
266 printk(KERN_INFO
"Macintosh ADB mouse installed.\n");
267 misc_register(&mac_mouse
);
272 #define MIN_THRESHOLD 1
273 #define MAX_THRESHOLD 20 /* more seems not reasonable... */
275 __initfunc(void mac_mouse_setup(char *str
, int *ints
))
278 printk( "mac_mouse_setup: no arguments!\n" );
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" );
288 mac_mouse_x_threshold
= ints
[1];
289 mac_mouse_y_threshold
= ints
[1];
291 if (ints
[2] < MIN_THRESHOLD
|| ints
[2] > MAX_THRESHOLD
)
292 printk("mac_mouse_setup: bad threshold value (ignored)\n" );
294 mac_mouse_y_threshold
= ints
[2];
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
);