2 * Linux driver for the PC110 pad
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.
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>
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>
48 #include <asm/semaphore.h>
49 #include <linux/spinlock.h>
50 #include <asm/uaccess.h>
55 static struct pc110pad_params default_params
= {
56 PC110PAD_PS2
, /* read mode */
57 50 MS
, /* bounce interval */
58 200 MS
, /* tap interval */
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
;
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
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
96 * notify_pad_up_down()
97 * Must be called whenever debounced pad up/down state changes.
99 * Flag is set whenever read_button() has new values
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
};
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
;
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
)
166 mod_timer(&tap_timer
, jiffies
+ current_params
.tap_interval
);
168 /* changes to transition_count can cause reported button to change */
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
)
185 *b
=--synthesize_tap
& 1;
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.
201 * Function to be called whenever the pad signals
202 * that it has new data available.
204 * Returns the most current pad state.
206 * Flag is set whenever read_raw_pad() has new values
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
};
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
)
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");
262 * The last up we spotted really was an up, so set
263 * debounced state the same as raw state.
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();
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.
291 * @irq: Interrupt number
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);
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
) )
330 if(new_down
!= raw_down
)
332 /* Down state has changed. raw_down always holds
333 * the most recently observed state.
337 /* Forget any earlier bounce processing */
340 del_timer(&bounce_timer
);
348 /* pad gone down, but we were reporting
349 * it down anyway because we suspected
350 * (correctly) that the last up was just
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
372 debounced_down
=new_down
;
373 notify_pad_up_down();
377 /* don't trust it yet */
379 mod_timer(&bounce_timer
,
380 jiffies
+current_params
.bounce_interval
);
391 * @down: set if the pen is down
392 * @debounced: set if the debounced pen position is down
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
);
407 *debounced
=debounced_down
;
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;
433 * Retrieve a triple of sample data.
437 static void sample_raw(int d
[3])
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
);
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
;
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
;
489 d
[2]=(synthesize_tap
<<4) | (b
?0x01:0);
490 restore_flags(flags
);
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
;
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
);
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
) )
538 d[0]= ((dy<0)?0x20:0)
544 d
[0]= ((dy
<0)?0x20:0)
555 * @fd: file number for the file
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
)
567 retval
= fasync_helper(fd
, filp
, on
, &asyncptr
);
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);
589 outb(0x30, current_params
.io
+2); /* switch off digitiser */
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
)
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 */
626 del_timer(&bounce_timer
);
627 del_timer(&tap_timer
);
628 restore_flags(flags
);
636 * @file: File handle to the pad
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
)
655 * Fetch a new sample according the current mouse mode the pad is
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;
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
)
687 for(r
=0; r
<count
; r
++)
690 new_sample(read_bytes
);
691 if(put_user(read_bytes
[read_byte_count
], buffer
+r
))
696 read_byte_count
= (read_byte_count
+1)%3;
705 * @file: File of the pad device
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
;
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
736 static int pad_ioctl(struct inode
*inode
, struct file
* file
,
737 unsigned int cmd
, unsigned long arg
)
739 struct pc110pad_params
new;
745 case PC110PADIOCGETP
:
746 new = current_params
;
747 if(copy_to_user((void *)arg
, &new, sizeof(new)))
751 case PC110PADIOCSETP
:
752 if(copy_from_user(&new, (void *)arg
, sizeof(new)))
755 if( (new.mode
<PC110PAD_RAW
)
756 || (new.mode
>PC110PAD_PS2
)
757 || (new.bounce_interval
<0)
758 || (new.tap_interval
<0)
762 current_params
.mode
= new.mode
;
763 current_params
.bounce_interval
= new.bounce_interval
;
764 current_params
.tap_interval
= new.tap_interval
;
771 static struct file_operations pad_fops
= {
783 static struct miscdevice pc110_pad
= {
784 PC110PAD_MINOR
, "pc110 pad", &pad_fops
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");
806 if(check_region(current_params
.io
, 4))
808 printk("pc110pad: I/O area in use.\n");
809 free_irq(current_params
.irq
,0);
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 */
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)