1 /* sunmouse.c: Sun mouse driver for the Sparc
3 * Copyright (C) 1995, 1996, 1997 David S. Miller (davem@caip.rutgers.edu)
4 * Copyright (C) 1995 Miguel de Icaza (miguel@nuclecu.unam.mx)
6 * Parts based on the psaux.c driver written by:
9 * Dec/19/95 Added SunOS mouse ioctls - miguel.
10 * Jan/5/96 Added VUID support, sigio support - miguel.
11 * Mar/5/96 Added proper mouse stream support - miguel.
12 * Sep/96 Allow more than one reader -miguel.
13 * Aug/97 Added PCI 8042 controller support -DaveM
16 /* The mouse is run off of one of the Zilog serial ports. On
17 * that port is the mouse and the keyboard, each gets a zs channel.
18 * The mouse itself is mouse-systems in nature. So the protocol is:
20 * Byte 1) Button state which is bit-encoded as
21 * 0x4 == left-button down, else up
22 * 0x2 == middle-button down, else up
23 * 0x1 == right-button down, else up
27 * Byte 4) Delta-x again
28 * Byte 5) Delta-y again
30 * One day this driver will have to support more than one mouse in the system.
32 * This driver has two modes of operation: the default VUID_NATIVE is
33 * set when the device is opened and allows the application to see the
34 * mouse character stream as we get it from the serial (for gpm for
35 * example). The second method, VUID_FIRM_EVENT will provide cooked
36 * events in Firm_event records as expected by SunOS/Solaris applications.
38 * FIXME: We need to support more than one mouse.
41 #include <linux/config.h>
42 #include <linux/kernel.h>
43 #include <linux/sched.h>
44 #include <linux/fcntl.h>
45 #include <linux/signal.h>
46 #include <linux/timer.h>
47 #include <linux/errno.h>
48 #include <linux/miscdevice.h>
50 #include <linux/poll.h>
51 #include <linux/spinlock.h>
52 #include <linux/smp_lock.h>
53 #include <linux/init.h>
54 #include <asm/uaccess.h>
55 #include <asm/system.h>
56 #include <asm/vuid_event.h>
57 #include <linux/random.h>
58 /* The following keeps track of software state for the Sun
61 #define STREAM_SIZE 2048
62 #define EV_SIZE (STREAM_SIZE/sizeof (Firm_event))
64 #define BUTTON_MIDDLE 2
65 #define BUTTON_RIGHT 1
68 unsigned char transaction[5]; /* Each protocol transaction */
69 unsigned char byte; /* Counter, starts at 0 */
70 unsigned char button_state; /* Current button state */
71 unsigned char prev_state; /* Previous button state */
72 int delta_x; /* Current delta-x */
73 int delta_y; /* Current delta-y */
74 int active; /* set if device is open */
75 int vuid_mode; /* VUID_NATIVE or VUID_FIRM_EVENT */
76 wait_queue_head_t proc_list;
77 struct fasync_struct *fasync;
79 /* The event/stream queue */
84 char stream [STREAM_SIZE];
85 Firm_event ev [EV_SIZE];
89 static struct sun_mouse sunmouse;
90 #define gen_events (sunmouse.vuid_mode != VUID_NATIVE)
91 #define bstate sunmouse.button_state
92 #define pstate sunmouse.prev_state
94 extern void mouse_put_char(char ch);
99 push_event (Firm_event *ev)
104 spin_lock_irqsave(&sunmouse.lock, flags);
106 next = (sunmouse.head + 1) % EV_SIZE;
108 if (next != sunmouse.tail) {
109 sunmouse.queue.ev [sunmouse.head] = *ev;
110 sunmouse.head = next;
114 spin_unlock_irqrestore(&sunmouse.lock, flags);
122 return sunmouse.head == sunmouse.tail;
125 /* Must be invoked under the sunmouse.lock */
126 static void get_from_queue (Firm_event *p)
128 *p = sunmouse.queue.ev [sunmouse.tail];
129 sunmouse.tail = (sunmouse.tail + 1) % EV_SIZE;
138 spin_lock_irqsave(&sunmouse.lock, flags);
140 next = (sunmouse.head + 1) % STREAM_SIZE;
141 if (next != sunmouse.tail) {
143 printk("P<%02x>\n", (unsigned char)c);
145 sunmouse.queue.stream [sunmouse.head] = c;
146 sunmouse.head = next;
149 spin_unlock_irqrestore(&sunmouse.lock, flags);
151 kill_fasync (&sunmouse.fasync, SIGIO, POLL_IN);
152 wake_up_interruptible (&sunmouse.proc_list);
155 /* Auto baud rate "detection". ;-) */
156 static int mouse_baud = 4800; /* Initial rate set by zilog driver. */
158 /* Change the baud rate after receiving too many "bogon bytes". */
159 void sun_mouse_change_baud(void)
161 extern void rs_change_mouse_baud(int newbaud);
163 if(mouse_baud == 1200)
165 else if(mouse_baud == 2400)
167 else if(mouse_baud == 4800)
172 rs_change_mouse_baud(mouse_baud);
175 /* This tries to monitor the mouse state so that it
176 * can automatically adjust to the correct baud rate.
177 * The mouse spits out BRKs when the baud rate is
180 * It returns non-zero if we should ignore this byte.
182 int mouse_baud_detection(unsigned char c, int is_break)
184 static int mouse_got_break = 0;
188 /* Let a few normal bytes go by before
189 * we jump the gun and say we need to
190 * try another baud rate.
192 if (mouse_got_break && ctr < 8)
195 /* OK, we need to try another baud rate. */
196 sun_mouse_change_baud();
201 if (mouse_got_break) {
204 printk(KERN_INFO "sunmouse: Successfully "
205 "adjusted to %d baud.\n", mouse_baud);
214 /* You ask me, why does this cap the lower bound at -127 and not
215 * -128? Because the xf86 mouse code is crap and treats -128
216 * as an illegal value and resets it's protocol state machine
217 * when it sees this value.
219 #define CLIP(__X) (((__X) > 127) ? 127 : (((__X) < -127) ? -127 : (__X)))
221 /* The following is called from the serial driver when bytes/breaks
222 * are received on the Mouse line.
225 sun_mouse_inbyte(unsigned char byte, int is_break)
231 add_mouse_randomness (byte);
235 printk("mouse(%02x:%d) ",
243 if (mouse_baud_detection(byte, is_break))
249 /* Ignore this if it is garbage. */
250 if (sunmouse.byte == 69) {
254 /* Ok, we've begun the state machine. */
258 /* If the mouse sends us a byte from 0x80 to 0x87
259 * we are starting at byte zero in the transaction
262 if((byte & ~0x0f) == 0x80)
266 mvalue = (signed char) byte;
267 switch(sunmouse.byte) {
269 /* If we get a bogus button byte, just skip it.
270 * When we get here the baud detection code has
271 * passed, so the only other things which can
272 * cause this are dropped serial characters and
273 * confused mouse. We check this because otherwise
274 * begin posting erroneous mouse events.
276 if ((byte & 0xf0) != 0x80)
280 sunmouse.button_state = (~byte) & 0x7;
282 printk("B<Left %s, Middle %s, Right %s>",
283 ((sunmouse.button_state & 0x4) ? "DOWN" : "UP"),
284 ((sunmouse.button_state & 0x2) ? "DOWN" : "UP"),
285 ((sunmouse.button_state & 0x1) ? "DOWN" : "UP"));
287 /* To deal with the Sparcbook 3 */
290 sunmouse.delta_y = 0;
291 sunmouse.delta_x = 0;
298 printk("DX1<%d>", mvalue);
300 sunmouse.delta_x = mvalue;
306 printk("DY1<%d>", mvalue);
308 sunmouse.delta_y = mvalue;
314 printk("DX2<%d>", mvalue);
316 sunmouse.delta_x += mvalue;
317 sunmouse.delta_x = CLIP(sunmouse.delta_x);
321 /* Last byte, Delta-y 2 */
323 printk("DY2<%d>", mvalue);
325 sunmouse.delta_y += mvalue;
326 sunmouse.delta_y = CLIP(sunmouse.delta_y);
327 sunmouse.byte = 0; /* Back to button state */
330 /* Until we get the (0x80 -> 0x87) value we aren't
331 * in the middle of a real transaction, so just
336 printk("sunmouse: bogon transaction state\n");
337 sunmouse.byte = 69; /* What could cause this? */
342 push_char (~sunmouse.button_state & 0x87);
343 push_char (sunmouse.delta_x);
344 push_char (sunmouse.delta_y);
351 if (d & BUTTON_LEFT) {
353 ev.value = bstate & BUTTON_LEFT;
355 if (d & BUTTON_RIGHT) {
357 ev.value = bstate & BUTTON_RIGHT;
359 if (d & BUTTON_MIDDLE) {
361 ev.value = bstate & BUTTON_MIDDLE;
364 ev.value = ev.value ? VKEY_DOWN : VKEY_UP;
365 pushed += push_event (&ev);
367 if (sunmouse.delta_x) {
370 ev.value = sunmouse.delta_x;
371 pushed += push_event (&ev);
372 sunmouse.delta_x = 0;
374 if (sunmouse.delta_y) {
377 ev.value = sunmouse.delta_y;
378 pushed += push_event (&ev);
382 /* We just completed a transaction, wake up whoever is awaiting
385 kill_fasync (&sunmouse.fasync, SIGIO, POLL_IN);
386 wake_up_interruptible(&sunmouse.proc_list);
392 sun_mouse_open(struct inode * inode, struct file * file)
394 spin_lock_irq(&sunmouse.lock);
395 if (sunmouse.active++)
397 sunmouse.delta_x = sunmouse.delta_y = 0;
398 sunmouse.button_state = 0x80;
399 sunmouse.vuid_mode = VUID_NATIVE;
401 spin_unlock_irq(&sunmouse.lock);
405 static int sun_mouse_fasync (int fd, struct file *filp, int on)
409 retval = fasync_helper (fd, filp, on, &sunmouse.fasync);
416 sun_mouse_close(struct inode *inode, struct file *file)
418 sun_mouse_fasync (-1, file, 0);
420 spin_lock_irq(&sunmouse.lock);
422 spin_unlock_irq(&sunmouse.lock);
428 sun_mouse_write(struct file *file, const char *buffer,
429 size_t count, loff_t *ppos)
431 return -EINVAL; /* foo on you */
435 sun_mouse_read(struct file *file, char *buffer,
436 size_t count, loff_t *ppos)
438 DECLARE_WAITQUEUE(wait, current);
441 if (queue_empty ()) {
442 if (file->f_flags & O_NONBLOCK)
444 add_wait_queue (&sunmouse.proc_list, &wait);
446 set_current_state(TASK_INTERRUPTIBLE);
447 if (queue_empty() && !signal_pending(current)) {
451 current->state = TASK_RUNNING;
452 remove_wait_queue (&sunmouse.proc_list, &wait);
455 char *p = buffer, *end = buffer+count;
457 spin_lock_irqsave(&sunmouse.lock, flags);
458 while (p < end && !queue_empty ()){
459 Firm_event this_event;
461 get_from_queue(&this_event);
462 spin_unlock_irqrestore(&sunmouse.lock, flags);
464 #ifdef CONFIG_SPARC32_COMPAT
465 if (current->thread.flags & SPARC_FLAG_32BIT) {
467 ((sizeof(Firm_event) - sizeof(struct timeval) +
470 if (copy_to_user((Firm_event *)p, &this_event,
471 sizeof(Firm_event)-sizeof(struct timeval)))
473 p += sizeof(Firm_event)-sizeof(struct timeval);
474 if (__put_user(this_event.time.tv_sec, (u32 *)p))
477 if (__put_user(this_event.time.tv_usec, (u32 *)p))
483 if ((end - p) < sizeof(Firm_event))
485 if (copy_to_user((Firm_event *)p, &this_event,
488 p += sizeof (Firm_event);
490 spin_lock_irqsave(&sunmouse.lock, flags);
492 spin_unlock_irqrestore(&sunmouse.lock, flags);
493 file->f_dentry->d_inode->i_atime = CURRENT_TIME;
500 for (c = 0; c < limit; c++) {
504 spin_lock_irqsave(&sunmouse.lock, flags);
509 val = sunmouse.queue.stream[sunmouse.tail];
510 sunmouse.tail = (sunmouse.tail + 1) % STREAM_SIZE;
512 spin_unlock_irqrestore(&sunmouse.lock, flags);
517 put_user(val, buffer);
527 file->f_dentry->d_inode->i_atime = CURRENT_TIME;
530 /* Only called if nothing was sent */
531 if (signal_pending(current))
536 static unsigned int sun_mouse_poll(struct file *file, poll_table *wait)
538 poll_wait(file, &sunmouse.proc_list, wait);
540 return POLLIN | POLLRDNORM;
544 sun_mouse_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
549 /* VUIDGFORMAT - Get input device byte stream format */
550 case _IOR('v', 2, int):
551 if (put_user(sunmouse.vuid_mode, (int *) arg))
555 /* VUIDSFORMAT - Set input device byte stream format*/
556 case _IOW('v', 1, int):
557 if (get_user(i, (int *) arg))
559 if (i == VUID_NATIVE || i == VUID_FIRM_EVENT){
562 if (get_user(value, (int *)arg))
565 spin_lock_irq(&sunmouse.lock);
566 sunmouse.vuid_mode = value;
567 sunmouse.head = sunmouse.tail = 0;
568 spin_unlock_irq(&sunmouse.lock);
575 /* This is a buggy application doing termios on the mouse driver */
576 /* we ignore it. I keep this check here so that we will notice */
577 /* future mouse vuid ioctls */
582 printk ("[MOUSE-ioctl: %8.8x]\n", cmd);
589 struct file_operations sun_mouse_fops = {
590 read: sun_mouse_read,
591 write: sun_mouse_write,
592 poll: sun_mouse_poll,
593 ioctl: sun_mouse_ioctl,
594 open: sun_mouse_open,
595 release: sun_mouse_close,
596 fasync: sun_mouse_fasync,
599 static struct miscdevice sun_mouse_mouse = {
600 SUN_MOUSE_MINOR, "sunmouse", &sun_mouse_fops
603 void sun_mouse_zsinit(void)
605 printk("Sun Mouse-Systems mouse driver version 1.00\n");
608 misc_register (&sun_mouse_mouse);
609 sunmouse.delta_x = sunmouse.delta_y = 0;
610 sunmouse.button_state = 0x80;
611 init_waitqueue_head(&sunmouse.proc_list);
612 spin_lock_init(&sunmouse.lock);