1 /* -*- c -*- --------------------------------------------------------- *
3 * linux/drivers/char/mk712.c
5 * Copyright 1999-2002 Transmeta Corporation
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * This driver supports the MK712 touch screen.
22 * based on busmouse.c, pc_keyb.c, and other mouse drivers
24 * 1999-12-18: original version, Daniel Quinlan
25 * 1999-12-19: added anti-jitter code, report pen-up events, fixed mk712_poll
26 * to use queue_empty, Nathan Laredo
27 * 1999-12-20: improved random point rejection, Nathan Laredo
28 * 2000-01-05: checked in new anti-jitter code, changed mouse protocol, fixed
29 * queue code, added module options, other fixes, Daniel Quinlan
30 * 2002-03-15: Clean up for kernel merge <alan@redhat.com>
31 * Fixed multi open race, fixed memory checks, fixed resource
32 * allocation, fixed close/powerdown bug, switched to new init
34 * ------------------------------------------------------------------------- */
36 #include <linux/module.h>
38 #include <linux/kernel.h>
39 #include <linux/sched.h>
40 #include <linux/init.h>
41 #include <linux/signal.h>
42 #include <linux/errno.h>
44 #include <linux/poll.h>
45 #include <linux/miscdevice.h>
46 #include <linux/random.h>
47 #include <linux/delay.h>
48 #include <linux/ioport.h>
49 #include <linux/slab.h>
50 #include <linux/interrupt.h>
53 #include <asm/uaccess.h>
54 #include <asm/system.h>
58 #define SQUARE(x) ((x)*(x))
60 #define MK712_DEFAULT_IO 0x260 /* demo board: 0x200, 0x208, 0x300 */
61 #define MK712_DEFAULT_IRQ 10 /* demo board: 10, 12, 14 or 15 */
63 /* eight 8-bit registers */
64 #define MK712_STATUS_LOW 0 /* READ */
65 #define MK712_STATUS_HIGH 1 /* READ */
66 #define MK712_X_LOW 2 /* READ */
67 #define MK712_X_HIGH 3 /* READ */
68 #define MK712_Y_LOW 4 /* READ */
69 #define MK712_Y_HIGH 5 /* READ */
70 #define MK712_CONTROL 6 /* R/W */
71 #define MK712_RATE 7 /* R/W */
74 #define MK712_STATUS_TOUCH 0x10
75 #define MK712_CONVERSION_COMPLETE 0x80
77 #define MK712_ENABLE_INT 0x01 /* enable interrupts */
78 #define MK712_INT_ON_CONVERSION_COMPLETE 0x02 /* if bit 0 = 1 */
79 #define MK712_INT_ON_CHANGE_IN_TOUCH_STATUS_A 0x04 /* if bit 0 = 1 */
80 #define MK712_INT_ON_CHANGE_IN_TOUCH_STATUS_B 0x08 /* if bit 0 = 1 */
81 #define MK712_ENABLE_PERIODIC_CONVERSIONS 0x10
82 #define MK712_READ_ONE_POINT 0x20
83 #define MK712_POWERDOWN_A 0x40
84 #define MK712_POWERDOWN_B 0x80
86 #define MK712_BUF_SIZE 256 /* a page */
92 unsigned int reserved;
98 wait_queue_head_t proc_list;
99 struct fasync_struct *fasync;
100 struct mk712_packet buf[256];
107 static int mk712_io = MK712_DEFAULT_IO;
108 static int mk712_irq = MK712_DEFAULT_IRQ;
109 static int mk712_users = 0;
110 static spinlock_t mk712_lock = SPIN_LOCK_UNLOCKED;
111 static struct mk712_queue *queue; /* mouse data buffer */
113 static struct mk712_packet get_from_queue(void)
115 struct mk712_packet result;
118 spin_lock_irqsave(&mk712_lock, flags);
119 result = queue->buf[queue->tail];
120 queue->tail = (queue->tail + 1) & (MK712_BUF_SIZE-1);
121 spin_unlock_irqrestore(&mk712_lock, flags);
125 static inline int queue_empty(void)
127 return queue->head == queue->tail;
130 static int mk712_fasync(int fd, struct file *filp, int on)
134 retval = fasync_helper(fd, filp, on, &queue->fasync);
140 static void mk712_output_packet(struct mk712_packet data)
142 int head = queue->head;
144 queue->buf[head] = data;
145 head = (head + 1) & (MK712_BUF_SIZE-1);
146 if (head != queue->tail) {
148 kill_fasync(&queue->fasync, SIGIO, POLL_IN);
149 wake_up_interruptible(&queue->proc_list);
153 static int points = 0; /* number of stored points */
154 static int output_point = 0; /* did I output a point since last release? */
156 static void mk712_output_point(int x, int y)
158 struct mk712_packet t;
165 mk712_output_packet(t);
169 static void mk712_store_point(int x_new, int y_new)
171 static int x[3], y[3];
177 if (points == 1 && abs(x[0] - x[1]) < 88 && abs(y[0] - y[1]) < 88)
179 x_out = (x[0] + x[1]) >> 1;
180 y_out = (y[0] + y[1]) >> 1;
181 mk712_output_point(x_out, y_out);
185 if ((abs(x[1] - x[2]) < 88 && abs(y[1] - y[2]) < 88) &&
186 (abs(x[0] - x[1]) < 88 && abs(y[0] - y[1]) < 88))
188 x_out = (x[0] + x[1] + x[2]) / 3;
189 y_out = (y[0] + y[1] + y[2]) / 3;
190 mk712_output_point(x_out, y_out);
192 else if (abs(x[1] - x[2]) < 88 && abs(y[1] - y[2]) < 88)
194 x_out = (x[1] + x[2]) >> 1;
195 y_out = (y[1] + y[2]) >> 1;
196 mk712_output_point(x_out, y_out);
200 int x_avg, y_avg, d0, d1, d2;
202 x_avg = (x[0] + x[1] + x[2]) / 3;
203 y_avg = (y[0] + y[1] + y[2]) / 3;
205 d0 = SQUARE(x[0] - x_avg) + SQUARE(y[0] - y_avg);
206 d1 = SQUARE(x[1] - x_avg) + SQUARE(y[1] - y_avg);
207 d2 = SQUARE(x[2] - x_avg) + SQUARE(y[2] - y_avg);
209 if (d2 > d1 && d2 > d0)
211 x_out = (x[0] + x[1]) >> 1;
212 y_out = (y[0] + y[1]) >> 1;
214 if (d1 > d0 && d1 > d2)
216 x_out = (x[0] + x[2]) >> 1;
217 y_out = (y[0] + y[2]) >> 1;
221 x_out = (x[1] + x[2]) >> 1;
222 y_out = (y[1] + y[2]) >> 1;
225 mk712_output_point(x_out, y_out);
239 static void mk712_release_event(void)
241 struct mk712_packet t;
250 t.x = t.y = t.reserved = 0;
252 mk712_output_packet(t);
257 static void mk712_interrupt(int irq, void *dev_id, struct pt_regs *regs)
261 unsigned char status;
264 static int drop_next = 1;
267 spin_lock_irqsave(&mk712_lock, flags);
269 status = inb(mk712_io + MK712_STATUS_LOW);
271 if (!(status & MK712_CONVERSION_COMPLETE)) {
277 if (!(status & MK712_STATUS_TOUCH)) /* release event */
282 mk712_release_event();
284 spin_unlock_irqrestore(&mk712_lock, flags);
285 wake_up_interruptible(&queue->proc_list);
290 x = inw(mk712_io + MK712_X_LOW) & 0x0fff;
291 y = inw(mk712_io + MK712_Y_LOW) & 0x0fff;
298 spin_unlock_irqrestore(&mk712_lock, flags);
299 wake_up_interruptible(&queue->proc_list);
305 x = inw(mk712_io + MK712_X_LOW) & 0x0fff;
306 y = inw(mk712_io + MK712_Y_LOW) & 0x0fff;
308 mk712_store_point(x, y);
310 spin_unlock_irqrestore(&mk712_lock, flags);
311 wake_up_interruptible(&queue->proc_list);
314 static int mk712_open(struct inode *inode, struct file *file)
316 unsigned char control;
321 spin_lock_irqsave(&mk712_lock, flags);
324 outb(0, mk712_io + MK712_CONTROL);
326 control |= (MK712_ENABLE_INT |
327 MK712_INT_ON_CONVERSION_COMPLETE |
328 MK712_INT_ON_CHANGE_IN_TOUCH_STATUS_B |
329 MK712_ENABLE_PERIODIC_CONVERSIONS |
331 outb(control, mk712_io + MK712_CONTROL);
333 outb(10, mk712_io + MK712_RATE); /* default count = 10 */
335 queue->head = queue->tail = 0; /* Flush input queue */
337 spin_unlock_irqrestore(&mk712_lock, flags);
341 static int mk712_close(struct inode * inode, struct file * file) {
342 /* power down controller */
344 spin_lock_irqsave(&mk712_lock, flags);
346 outb(0, mk712_io + MK712_CONTROL);
347 spin_unlock_irqrestore(&mk712_lock, flags);
351 static unsigned int mk712_poll(struct file *file, poll_table *wait)
353 poll_wait(file, &queue->proc_list, wait);
355 return POLLIN | POLLRDNORM;
359 static int mk712_ioctl(struct inode *inode, struct file * file,
360 unsigned int cmd, unsigned long arg)
368 static ssize_t mk712_read(struct file *file, char *buffer,
369 size_t count, loff_t *pos)
371 DECLARE_WAITQUEUE(wait, current);
372 ssize_t bytes_read = 0;
373 struct mk712_packet p;
375 /* wait for an event */
377 if (file->f_flags & O_NONBLOCK)
379 add_wait_queue(&queue->proc_list, &wait);
381 set_current_state(TASK_INTERRUPTIBLE);
382 if (queue_empty() && !signal_pending(current)) {
386 current->state = TASK_RUNNING;
387 remove_wait_queue(&queue->proc_list, &wait);
390 while (bytes_read < count && !queue_empty()) {
391 p = get_from_queue();
392 if (copy_to_user (buffer+bytes_read, (void *) &p, sizeof(p)))
394 bytes_read = -EFAULT;
397 bytes_read += sizeof(p);
402 file->f_dentry->d_inode->i_atime = CURRENT_TIME;
406 if (signal_pending(current))
412 static ssize_t mk712_write(struct file *file, const char *buffer, size_t count,
418 struct file_operations mk712_fops = {
425 release: mk712_close,
426 fasync: mk712_fasync,
429 static struct miscdevice mk712_touchscreen = {
430 MK712_MINOR, "mk712_touchscreen", &mk712_fops
433 int __init mk712_init(void)
442 if(!request_region(mk712_io, 8, "mk712_touchscreen"))
444 printk("mk712: unable to get IO region\n");
448 /* set up wait queue */
449 queue = (struct mk712_queue *) kmalloc(sizeof(*queue), GFP_KERNEL);
452 release_region(mk712_io, 8);
455 memset(queue, 0, sizeof(*queue));
456 queue->head = queue->tail = 0;
457 init_waitqueue_head(&queue->proc_list);
459 /* The MK712 is ISA and hard-coded to a particular IRQ, so the
460 driver should keep the IRQ as long as it is loaded. */
461 if(request_irq(mk712_irq, mk712_interrupt, 0, "mk712_touchscreen",
464 printk("mk712: unable to get IRQ\n");
465 release_region(mk712_io, 8);
470 /* register misc device */
471 if(misc_register(&mk712_touchscreen)<0)
473 release_region(mk712_io, 8);
475 free_irq(mk712_irq, queue);
481 static void __exit mk712_exit(void)
483 misc_deregister(&mk712_touchscreen);
484 release_region(mk712_io, 8);
485 free_irq(mk712_irq, queue);
487 printk(KERN_INFO "mk712 touchscreen uninstalled\n");
490 MODULE_AUTHOR("Daniel Quinlan");
491 MODULE_DESCRIPTION("MK712 touch screen driver");
492 MODULE_PARM(io, "i");
493 MODULE_PARM_DESC(io, "I/O base address of MK712 touch screen controller");
494 MODULE_PARM(irq, "i");
495 MODULE_PARM_DESC(irq, "IRQ of MK712 touch screen controller");
496 MODULE_LICENSE("GPL");
498 module_init(mk712_init);
499 module_exit(mk712_exit);
503 * c-file-style: "linux"