Import 2.4.0-test2pre7
[davej-history.git] / drivers / char / pc110pad.c
blobdfef66e394908cd698a32c7d73e1d88cf596455d
1 /*
2 * Linux driver for the PC110 pad
3 */
5 /**
6 * DOC: PC110 Digitizer Hardware
8 * The pad provides triples of data. The first byte has
9 * 0x80=bit 8 X, 0x01=bit 7 X, 0x08=bit 8 Y, 0x01=still down
10 * The second byte is bits 0-6 X
11 * The third is bits 0-6 Y
13 * This is read internally and used to synthesize a stream of
14 * triples in the form expected from a PS/2 device. Specialist
15 * applications can choose to obtain the pad data in other formats
16 * including a debugging mode.
18 * It would be good to add a joystick driver mode to this pad so
19 * that doom and other game playing are better. One possible approach
20 * would be to deactive the mouse mode while the joystick port is opened.
24 * History
26 * 0.0 1997-05-16 Alan Cox <alan@redhat.com> - Pad reader
27 * 0.1 1997-05-19 Robin O'Leary <robin@acm.org> - PS/2 emulation
28 * 0.2 1997-06-03 Robin O'Leary <robin@acm.org> - tap gesture
29 * 0.3 1997-06-27 Alan Cox <alan@redhat.com> - 2.1 commit
30 * 0.4 1997-11-09 Alan Cox <alan@redhat.com> - Single Unix VFS API changes
31 * 0.5 2000-02-10 Alan Cox <alan@redhat.com> - 2.3.x cleanup, documentation
34 #include <linux/module.h>
35 #include <linux/kernel.h>
36 #include <linux/signal.h>
37 #include <linux/errno.h>
38 #include <linux/mm.h>
39 #include <linux/miscdevice.h>
40 #include <linux/ptrace.h>
41 #include <linux/poll.h>
42 #include <linux/ioport.h>
43 #include <linux/interrupt.h>
45 #include <asm/signal.h>
46 #include <asm/io.h>
47 #include <asm/irq.h>
48 #include <asm/semaphore.h>
49 #include <linux/spinlock.h>
50 #include <asm/uaccess.h>
52 #include "pc110pad.h"
55 static struct pc110pad_params default_params = {
56 PC110PAD_PS2, /* read mode */
57 50 MS, /* bounce interval */
58 200 MS, /* tap interval */
59 10, /* IRQ */
60 0x15E0, /* I/O port */
64 static struct pc110pad_params current_params;
67 /* driver/filesystem interface management */
68 static wait_queue_head_t queue;
69 static struct fasync_struct *asyncptr;
70 static int active=0; /* number of concurrent open()s */
71 static struct semaphore reader_lock;
73 /**
74 * wake_readers:
76 * Take care of letting any waiting processes know that
77 * now would be a good time to do a read(). Called
78 * whenever a state transition occurs, real or synthetic. Also
79 * issue any SIGIO's to programs that use SIGIO on mice (eg
80 * Executor)
83 static void wake_readers(void)
85 wake_up_interruptible(&queue);
86 kill_fasync(&asyncptr, SIGIO, POLL_IN);
90 /*****************************************************************************/
92 * Deal with the messy business of synthesizing button tap and drag
93 * events.
95 * Exports:
96 * notify_pad_up_down()
97 * Must be called whenever debounced pad up/down state changes.
98 * button_pending
99 * Flag is set whenever read_button() has new values
100 * to return.
101 * read_button()
102 * Obtains the current synthetic mouse button state.
106 * These keep track of up/down transitions needed to generate the
107 * synthetic mouse button events. While recent_transition is set,
108 * up/down events cause transition_count to increment. tap_timer
109 * turns off the recent_transition flag and may cause some synthetic
110 * up/down mouse events to be created by incrementing synthesize_tap.
113 static int button_pending=0;
114 static int recent_transition=0;
115 static int transition_count=0;
116 static int synthesize_tap=0;
117 static void tap_timeout(unsigned long data);
118 static struct timer_list tap_timer = { function: tap_timeout };
122 * tap_timeout:
123 * @data: Unused
125 * This callback goes off a short time after an up/down transition;
126 * before it goes off, transitions will be considered part of a
127 * single PS/2 event and counted in transition_count. Once the
128 * timeout occurs the recent_transition flag is cleared and
129 * any synthetic mouse up/down events are generated.
132 static void tap_timeout(unsigned long data)
134 if(!recent_transition)
136 printk("pc110pad: tap_timeout but no recent transition!\n");
138 if( transition_count==2 || transition_count==4 || transition_count==6 )
140 synthesize_tap+=transition_count;
141 button_pending = 1;
142 wake_readers();
144 recent_transition=0;
149 * notify_pad_up_down:
151 * Called by the raw pad read routines when a (debounced) up/down
152 * transition is detected.
155 void notify_pad_up_down(void)
157 if(recent_transition)
159 transition_count++;
161 else
163 transition_count=1;
164 recent_transition=1;
166 mod_timer(&tap_timer, jiffies + current_params.tap_interval);
168 /* changes to transition_count can cause reported button to change */
169 button_pending = 1;
170 wake_readers();
174 * read_button:
175 * @b: pointer to the button status.
177 * The actual button state depends on what we are seeing. We have to check
178 * for the tap gesture and also for dragging.
181 static void read_button(int *b)
183 if(synthesize_tap)
185 *b=--synthesize_tap & 1;
187 else
189 *b=(!recent_transition && transition_count==3); /* drag */
191 button_pending=(synthesize_tap>0);
195 /*****************************************************************************/
197 * Read pad absolute co-ordinates and debounced up/down state.
199 * Exports:
200 * pad_irq()
201 * Function to be called whenever the pad signals
202 * that it has new data available.
203 * read_raw_pad()
204 * Returns the most current pad state.
205 * xy_pending
206 * Flag is set whenever read_raw_pad() has new values
207 * to return.
208 * Imports:
209 * wake_readers()
210 * Called when movement occurs.
211 * notify_pad_up_down()
212 * Called when debounced up/down status changes.
216 * These are up/down state and absolute co-ords read directly from pad
219 static int raw_data[3];
220 static int raw_data_count=0;
221 static int raw_x=0, raw_y=0; /* most recent absolute co-ords read */
222 static int raw_down=0; /* raw up/down state */
223 static int debounced_down=0; /* up/down state after debounce processing */
224 static enum { NO_BOUNCE, JUST_GONE_UP, JUST_GONE_DOWN } bounce=NO_BOUNCE;
225 /* set just after an up/down transition */
226 static int xy_pending=0; /* set if new data have not yet been read */
229 * Timer goes off a short while after an up/down transition and copies
230 * the value of raw_down to debounced_down.
233 static void bounce_timeout(unsigned long data);
234 static struct timer_list bounce_timer = { function: bounce_timeout };
239 * bounce_timeout:
240 * @data: Unused
242 * No further up/down transitions happened within the
243 * bounce period, so treat this as a genuine transition.
246 static void bounce_timeout(unsigned long data)
248 switch(bounce)
250 case NO_BOUNCE:
253 * Strange; the timer callback should only go off if
254 * we were expecting to do bounce processing!
256 printk(KERN_WARNING "pc110pad, bounce_timeout: bounce flag not set!\n");
257 break;
259 case JUST_GONE_UP:
262 * The last up we spotted really was an up, so set
263 * debounced state the same as raw state.
265 bounce=NO_BOUNCE;
266 if(debounced_down==raw_down)
268 printk(KERN_WARNING "pc110pad, bounce_timeout: raw already debounced!\n");
270 debounced_down=raw_down;
272 notify_pad_up_down();
273 break;
275 case JUST_GONE_DOWN:
278 * We don't debounce down events, but we still time
279 * out soon after one occurs so we can avoid the (x,y)
280 * skittering that sometimes happens.
282 bounce=NO_BOUNCE;
283 break;
290 * pad_irq:
291 * @irq: Interrupt number
292 * @ptr: Unused
293 * @regs: Unused
295 * Callback when pad's irq goes off; copies values in to raw_* globals;
296 * initiates debounce processing. This isn't SMP safe however there are
297 * no SMP machines with a PC110 touchpad on them.
300 static void pad_irq(int irq, void *ptr, struct pt_regs *regs)
303 /* Obtain byte from pad and prime for next byte */
305 int value=inb_p(current_params.io);
306 int handshake=inb_p(current_params.io+2);
307 outb_p(handshake | 1, current_params.io+2);
308 outb_p(handshake &~1, current_params.io+2);
309 inb_p(0x64);
311 raw_data[raw_data_count++]=value;
314 if(raw_data_count==3)
316 int new_down=raw_data[0]&0x01;
317 int new_x=raw_data[1];
318 int new_y=raw_data[2];
319 if(raw_data[0]&0x10) new_x+=128;
320 if(raw_data[0]&0x80) new_x+=256;
321 if(raw_data[0]&0x08) new_y+=128;
323 if( (raw_x!=new_x) || (raw_y!=new_y) )
325 raw_x=new_x;
326 raw_y=new_y;
327 xy_pending=1;
330 if(new_down != raw_down)
332 /* Down state has changed. raw_down always holds
333 * the most recently observed state.
335 raw_down=new_down;
337 /* Forget any earlier bounce processing */
338 if(bounce)
340 del_timer(&bounce_timer);
341 bounce=NO_BOUNCE;
344 if(new_down)
346 if(debounced_down)
348 /* pad gone down, but we were reporting
349 * it down anyway because we suspected
350 * (correctly) that the last up was just
351 * a bounce
354 else
356 bounce=JUST_GONE_DOWN;
357 mod_timer(&bounce_timer,
358 jiffies+current_params.bounce_interval);
359 /* start new stroke/tap */
360 debounced_down=new_down;
361 notify_pad_up_down();
364 else /* just gone up */
366 if(recent_transition)
368 /* early bounces are probably part of
369 * a multi-tap gesture, so process
370 * immediately
372 debounced_down=new_down;
373 notify_pad_up_down();
375 else
377 /* don't trust it yet */
378 bounce=JUST_GONE_UP;
379 mod_timer(&bounce_timer,
380 jiffies+current_params.bounce_interval);
384 wake_readers();
385 raw_data_count=0;
390 * read_raw_pad:
391 * @down: set if the pen is down
392 * @debounced: set if the debounced pen position is down
393 * @x: X position
394 * @y: Y position
396 * Retrieve the data saved by the interrupt handler and indicate we
397 * have no more pending XY to do.
399 * FIXME: We should switch to a spinlock for this.
402 static void read_raw_pad(int *down, int *debounced, int *x, int *y)
404 disable_irq(current_params.irq);
406 *down=raw_down;
407 *debounced=debounced_down;
408 *x=raw_x;
409 *y=raw_y;
410 xy_pending = 0;
412 enable_irq(current_params.irq);
415 /*****************************************************************************/
417 * Filesystem interface
421 * Read returns byte triples, so we need to keep track of
422 * how much of a triple has been read. This is shared across
423 * all processes which have this device open---not that anything
424 * will make much sense in that case.
426 static int read_bytes[3];
427 static int read_byte_count=0;
430 * sample_raw:
431 * @d: sample buffer
433 * Retrieve a triple of sample data.
437 static void sample_raw(int d[3])
439 d[0]=raw_data[0];
440 d[1]=raw_data[1];
441 d[2]=raw_data[2];
445 * sample_rare:
446 * @d: sample buffer
448 * Retrieve a triple of sample data and sanitize it. We do the needed
449 * scaling and masking to get the current status.
453 static void sample_rare(int d[3])
455 int thisd, thisdd, thisx, thisy;
457 read_raw_pad(&thisd, &thisdd, &thisx, &thisy);
459 d[0]=(thisd?0x80:0)
460 | (thisx/256)<<4
461 | (thisdd?0x08:0)
462 | (thisy/256)
464 d[1]=thisx%256;
465 d[2]=thisy%256;
469 * sample_debug:
470 * @d: sample buffer
472 * Retrieve a triple of sample data and mix it up with the state
473 * information in the gesture parser. Not useful for normal users but
474 * handy when debugging
477 static void sample_debug(int d[3])
479 int thisd, thisdd, thisx, thisy;
480 int b;
481 unsigned long flags;
483 save_flags(flags);
484 cli();
485 read_raw_pad(&thisd, &thisdd, &thisx, &thisy);
486 d[0]=(thisd?0x80:0) | (thisdd?0x40:0) | bounce;
487 d[1]=(recent_transition?0x80:0)+transition_count;
488 read_button(&b);
489 d[2]=(synthesize_tap<<4) | (b?0x01:0);
490 restore_flags(flags);
494 * sample_ps2:
495 * @d: sample buffer
497 * Retrieve a triple of sample data and turn the debounced tap and
498 * stroke information into what appears to be a PS/2 mouse. This means
499 * the PC110 pad needs no funny application side support.
503 static void sample_ps2(int d[3])
505 static int lastx, lasty, lastd;
507 int thisd, thisdd, thisx, thisy;
508 int dx, dy, b;
511 * Obtain the current mouse parameters and limit as appropriate for
512 * the return data format. Interrupts are only disabled while
513 * obtaining the parameters, NOT during the puts_fs_byte() calls,
514 * so paging in put_user() does not affect mouse tracking.
516 read_raw_pad(&thisd, &thisdd, &thisx, &thisy);
517 read_button(&b);
519 /* Now compare with previous readings. Note that we use the
520 * raw down flag rather than the debounced one.
522 if( (thisd && !lastd) /* new stroke */
523 || (bounce!=NO_BOUNCE) )
525 dx=0;
526 dy=0;
528 else
530 dx = (thisx-lastx);
531 dy = -(thisy-lasty);
533 lastx=thisx;
534 lasty=thisy;
535 lastd=thisd;
538 d[0]= ((dy<0)?0x20:0)
539 | ((dx<0)?0x10:0)
540 | 0x08
541 | (b? 0x01:0x00)
544 d[0]= ((dy<0)?0x20:0)
545 | ((dx<0)?0x10:0)
546 | (b? 0x00:0x08)
548 d[1]=dx;
549 d[2]=dy;
554 * fasync_pad:
555 * @fd: file number for the file
556 * @filp: file handle
557 * @on: 1 to add, 0 to remove a notifier
559 * Update the queue of asynchronous event notifiers. We can use the
560 * same helper the mice do and that does almost everything we need.
563 static int fasync_pad(int fd, struct file *filp, int on)
565 int retval;
567 retval = fasync_helper(fd, filp, on, &asyncptr);
568 if (retval < 0)
569 return retval;
570 return 0;
575 * close_pad:
576 * @inode: inode of pad
577 * @file: file handle to pad
579 * Close access to the pad. We turn the pad power off if this is the
580 * last user of the pad. I've not actually measured the power draw but
581 * the DOS driver is careful to do this so we follow suit.
584 static int close_pad(struct inode * inode, struct file * file)
586 fasync_pad(-1, file, 0);
587 if (--active)
588 return 0;
589 outb(0x30, current_params.io+2); /* switch off digitiser */
590 return 0;
595 * open_pad:
596 * @inode: inode of pad
597 * @file: file handle to pad
599 * Open access to the pad. We turn the pad off first (we turned it off
600 * on close but if this is the first open after a crash the state is
601 * indeterminate). The device has a small fifo so we empty that before
602 * we kick it back into action.
605 static int open_pad(struct inode * inode, struct file * file)
607 unsigned long flags;
609 if (active++)
610 return 0;
612 save_flags(flags);
613 cli();
614 outb(0x30, current_params.io+2); /* switch off digitiser */
615 pad_irq(0,0,0); /* read to flush any pending bytes */
616 pad_irq(0,0,0); /* read to flush any pending bytes */
617 pad_irq(0,0,0); /* read to flush any pending bytes */
618 outb(0x38, current_params.io+2); /* switch on digitiser */
619 current_params = default_params;
620 raw_data_count=0; /* re-sync input byte counter */
621 read_byte_count=0; /* re-sync output byte counter */
622 button_pending=0;
623 recent_transition=0;
624 transition_count=0;
625 synthesize_tap=0;
626 del_timer(&bounce_timer);
627 del_timer(&tap_timer);
628 restore_flags(flags);
630 return 0;
635 * write_pad:
636 * @file: File handle to the pad
637 * @buffer: Unused
638 * @count: Unused
639 * @ppos: Unused
641 * Writes are disallowed. A true PS/2 mouse lets you write stuff. Everyone
642 * seems happy with this and not faking the write modes.
645 static ssize_t write_pad(struct file * file, const char * buffer, size_t count, loff_t *ppos)
647 return -EINVAL;
652 * new_sample:
653 * @d: sample buffer
655 * Fetch a new sample according the current mouse mode the pad is
656 * using.
659 void new_sample(int d[3])
661 switch(current_params.mode)
663 case PC110PAD_RAW: sample_raw(d); break;
664 case PC110PAD_RARE: sample_rare(d); break;
665 case PC110PAD_DEBUG: sample_debug(d); break;
666 case PC110PAD_PS2: sample_ps2(d); break;
672 * read_pad:
673 * @file: File handle to pad
674 * @buffer: Target for the mouse data
675 * @count: Buffer length
676 * @ppos: Offset (unused)
678 * Read data from the pad. We use the reader_lock to avoid mess when there are
679 * two readers. This shouldnt be happening anyway but we play safe.
682 static ssize_t read_pad(struct file * file, char * buffer, size_t count, loff_t *ppos)
684 int r;
686 down(&reader_lock);
687 for(r=0; r<count; r++)
689 if(!read_byte_count)
690 new_sample(read_bytes);
691 if(put_user(read_bytes[read_byte_count], buffer+r))
693 r = -EFAULT;
694 break;
696 read_byte_count = (read_byte_count+1)%3;
698 up(&reader_lock);
699 return r;
704 * pad_poll:
705 * @file: File of the pad device
706 * @wait: Poll table
708 * The pad is ready to read if there is a button or any position change
709 * pending in the queue. The reading and interrupt routines maintain the
710 * required state for us and do needed wakeups.
713 static unsigned int pad_poll(struct file *file, poll_table * wait)
715 poll_wait(file, &queue, wait);
716 if(button_pending || xy_pending)
717 return POLLIN | POLLRDNORM;
718 return 0;
723 * pad_ioctl;
724 * @inode: Inode of the pad
725 * @file: File handle to the pad
726 * @cmd: Ioctl command
727 * @arg: Argument pointer
729 * The PC110 pad supports two ioctls both of which use the pc110pad_params
730 * structure. GETP queries the current pad status. SETP changes the pad
731 * configuration. Changing configuration during normal mouse operations
732 * may give momentarily odd results as things like tap gesture state
733 * may be lost.
736 static int pad_ioctl(struct inode *inode, struct file * file,
737 unsigned int cmd, unsigned long arg)
739 struct pc110pad_params new;
741 if (!inode)
742 return -EINVAL;
744 switch (cmd) {
745 case PC110PADIOCGETP:
746 new = current_params;
747 if(copy_to_user((void *)arg, &new, sizeof(new)))
748 return -EFAULT;
749 return 0;
751 case PC110PADIOCSETP:
752 if(copy_from_user(&new, (void *)arg, sizeof(new)))
753 return -EFAULT;
755 if( (new.mode<PC110PAD_RAW)
756 || (new.mode>PC110PAD_PS2)
757 || (new.bounce_interval<0)
758 || (new.tap_interval<0)
760 return -EINVAL;
762 current_params.mode = new.mode;
763 current_params.bounce_interval = new.bounce_interval;
764 current_params.tap_interval = new.tap_interval;
765 return 0;
767 return -ENOIOCTLCMD;
771 static struct file_operations pad_fops = {
772 owner: THIS_MODULE,
773 read: read_pad,
774 write: write_pad,
775 poll: pad_poll,
776 ioctl: pad_ioctl,
777 open: open_pad,
778 release: close_pad,
779 fasync: fasync_pad,
783 static struct miscdevice pc110_pad = {
784 PC110PAD_MINOR, "pc110 pad", &pad_fops
789 * pc110pad_init:
791 * We configure the pad with the default parameters (that is PS/2
792 * emulation mode. We then claim the needed I/O and interrupt resources.
793 * Finally as a matter of paranoia we turn the pad off until we are
794 * asked to open it by an application.
797 int pc110pad_init(void)
799 current_params = default_params;
801 if(request_irq(current_params.irq, pad_irq, 0, "pc110pad", 0))
803 printk("pc110pad: Unable to get IRQ.\n");
804 return -EBUSY;
806 if(check_region(current_params.io, 4))
808 printk("pc110pad: I/O area in use.\n");
809 free_irq(current_params.irq,0);
810 return -EBUSY;
812 request_region(current_params.io, 4, "pc110pad");
813 init_waitqueue_head(&queue);
814 printk("PC110 digitizer pad at 0x%X, irq %d.\n",
815 current_params.io,current_params.irq);
816 misc_register(&pc110_pad);
817 outb(0x30, current_params.io+2); /* switch off digitiser */
819 return 0;
822 #ifdef MODULE
825 * pc110pad_unload:
827 * Free the resources we acquired when the module was loaded. We also
828 * turn the pad off to be sure we don't leave it using power.
831 static void pc110pad_unload(void)
833 outb(0x30, current_params.io+2); /* switch off digitiser */
834 if(current_params.irq)
835 free_irq(current_params.irq, 0);
836 current_params.irq=0;
837 release_region(current_params.io, 4);
838 misc_deregister(&pc110_pad);
843 int init_module(void)
845 init_MUTEX(&reader_lock);
846 return pc110pad_init();
849 void cleanup_module(void)
851 pc110pad_unload();
853 #endif