ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / input / tsdev.c
1 /*
2  * $Id: tsdev.c,v 1.15 2002/04/10 16:50:19 jsimmons Exp $
3  *
4  *  Copyright (c) 2001 "Crazy" james Simmons 
5  *
6  *  Input driver to Touchscreen device driver module.
7  *
8  *  Sponsored by Transvirtual Technology
9  */
10
11 /*
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or 
15  * (at your option) any later version.
16  * 
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  * 
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25  * 
26  * Should you need to contact me, the author, you can do so either by
27  * e-mail - mail your message to <jsimmons@transvirtual.com>.
28  */
29
30 #define TSDEV_MINOR_BASE        128
31 #define TSDEV_MINORS            32
32 #define TSDEV_BUFFER_SIZE       64
33
34 #include <linux/slab.h>
35 #include <linux/poll.h>
36 #include <linux/module.h>
37 #include <linux/moduleparam.h>
38 #include <linux/init.h>
39 #include <linux/input.h>
40 #include <linux/major.h>
41 #include <linux/config.h>
42 #include <linux/smp_lock.h>
43 #include <linux/random.h>
44 #include <linux/time.h>
45 #include <linux/device.h>
46 #include <linux/devfs_fs_kernel.h>
47
48 #ifndef CONFIG_INPUT_TSDEV_SCREEN_X
49 #define CONFIG_INPUT_TSDEV_SCREEN_X     240
50 #endif
51 #ifndef CONFIG_INPUT_TSDEV_SCREEN_Y
52 #define CONFIG_INPUT_TSDEV_SCREEN_Y     320
53 #endif
54
55 MODULE_AUTHOR("James Simmons <jsimmons@transvirtual.com>");
56 MODULE_DESCRIPTION("Input driver to touchscreen converter");
57 MODULE_LICENSE("GPL");
58
59 static int xres = CONFIG_INPUT_TSDEV_SCREEN_X;
60 module_param(xres, uint, 0);
61 MODULE_PARM_DESC(xres, "Horizontal screen resolution");
62
63 static int yres = CONFIG_INPUT_TSDEV_SCREEN_Y;
64 module_param(yres, uint, 0);
65 MODULE_PARM_DESC(yres, "Vertical screen resolution");
66
67 struct tsdev {
68         int exist;
69         int open;
70         int minor;
71         char name[16];
72         wait_queue_head_t wait;
73         struct list_head list;
74         struct input_handle handle;
75 };
76
77 /* From Compaq's Touch Screen Specification version 0.2 (draft) */
78 typedef struct {
79         short pressure;
80         short x;
81         short y;
82         short millisecs;
83 } TS_EVENT;
84
85 struct tsdev_list {
86         struct fasync_struct *fasync;
87         struct list_head node;
88         struct tsdev *tsdev;
89         int head, tail;
90         int oldx, oldy, pendown;
91         TS_EVENT event[TSDEV_BUFFER_SIZE];
92 };
93
94 static struct input_handler tsdev_handler;
95
96 static struct tsdev *tsdev_table[TSDEV_MINORS];
97
98 static int tsdev_fasync(int fd, struct file *file, int on)
99 {
100         struct tsdev_list *list = file->private_data;
101         int retval;
102
103         retval = fasync_helper(fd, file, on, &list->fasync);
104         return retval < 0 ? retval : 0;
105 }
106
107 static int tsdev_open(struct inode *inode, struct file *file)
108 {
109         int i = iminor(inode) - TSDEV_MINOR_BASE;
110         struct tsdev_list *list;
111
112         if (i >= TSDEV_MINORS || !tsdev_table[i])
113                 return -ENODEV;
114
115         if (!(list = kmalloc(sizeof(struct tsdev_list), GFP_KERNEL)))
116                 return -ENOMEM;
117         memset(list, 0, sizeof(struct tsdev_list));
118
119         list->tsdev = tsdev_table[i];
120         list_add_tail(&list->node, &tsdev_table[i]->list);
121         file->private_data = list;
122
123         if (!list->tsdev->open++)
124                 if (list->tsdev->exist)
125                         input_open_device(&list->tsdev->handle);
126         return 0;
127 }
128
129 static void tsdev_free(struct tsdev *tsdev)
130 {
131         devfs_remove("input/ts%d", tsdev->minor);
132         class_simple_device_remove(MKDEV(INPUT_MAJOR, TSDEV_MINOR_BASE + tsdev->minor));
133         tsdev_table[tsdev->minor] = NULL;
134         kfree(tsdev);
135 }
136
137 static int tsdev_release(struct inode *inode, struct file *file)
138 {
139         struct tsdev_list *list = file->private_data;
140
141         tsdev_fasync(-1, file, 0);
142         list_del(&list->node);
143
144         if (!--list->tsdev->open) {
145                 if (list->tsdev->exist)
146                         input_close_device(&list->tsdev->handle);
147                 else
148                         tsdev_free(list->tsdev);
149         }
150         kfree(list);
151         return 0;
152 }
153
154 static ssize_t tsdev_read(struct file *file, char *buffer, size_t count,
155                           loff_t * ppos)
156 {
157         struct tsdev_list *list = file->private_data;
158         int retval = 0;
159
160         if (list->head == list->tail && list->tsdev->exist && (file->f_flags & O_NONBLOCK))
161                 return -EAGAIN;
162
163         retval = wait_event_interruptible(list->tsdev->wait,
164                 (list->head != list->tail) && list->tsdev->exist);
165
166         if (retval)
167                 return retval;
168
169         if (!list->tsdev->exist)
170                 return -ENODEV;
171
172         while (list->head != list->tail && retval + sizeof(TS_EVENT) <= count) {
173                 if (copy_to_user (buffer + retval, list->event + list->tail, sizeof(TS_EVENT)))
174                         return -EFAULT;
175                 list->tail = (list->tail + 1) & (TSDEV_BUFFER_SIZE - 1);
176                 retval += sizeof(TS_EVENT);
177         }
178
179         return retval;
180 }
181
182 /* No kernel lock - fine */
183 static unsigned int tsdev_poll(struct file *file, poll_table * wait)
184 {
185         struct tsdev_list *list = file->private_data;
186
187         poll_wait(file, &list->tsdev->wait, wait);
188         if (list->head != list->tail)
189                 return POLLIN | POLLRDNORM;
190         return 0;
191 }
192
193 static int tsdev_ioctl(struct inode *inode, struct file *file,
194                        unsigned int cmd, unsigned long arg)
195 {
196 /*
197         struct tsdev_list *list = file->private_data;
198         struct tsdev *evdev = list->tsdev;
199         struct input_dev *dev = tsdev->handle.dev;
200         int retval;
201         
202         switch (cmd) {
203                 case HHEHE:
204                         return 0;
205                 case hjff:
206                         return 0;
207                 default:
208                         return 0;
209         }
210 */
211         return -EINVAL;
212 }
213
214 struct file_operations tsdev_fops = {
215         .owner =        THIS_MODULE,
216         .open =         tsdev_open,
217         .release =      tsdev_release,
218         .read =         tsdev_read,
219         .poll =         tsdev_poll,
220         .fasync =       tsdev_fasync,
221         .ioctl =        tsdev_ioctl,
222 };
223
224 static void tsdev_event(struct input_handle *handle, unsigned int type,
225                         unsigned int code, int value)
226 {
227         struct tsdev *tsdev = handle->private;
228         struct tsdev_list *list;
229         struct timeval time;
230         int size;
231
232         list_for_each_entry(list, &tsdev->list, node) {
233                 switch (type) {
234                 case EV_ABS:
235                         switch (code) {
236                         case ABS_X:
237                                 if (!list->pendown)
238                                         return;
239                                 size = handle->dev->absmax[ABS_X] - handle->dev->absmin[ABS_X];
240                                 if (size > 0)
241                                         list->oldx = ((value - handle->dev->absmin[ABS_X]) * xres / size);
242                                 else
243                                         list->oldx = ((value - handle->dev->absmin[ABS_X]));
244                                 break;
245                         case ABS_Y:
246                                 if (!list->pendown)
247                                         return;
248                                 size = handle->dev->absmax[ABS_Y] - handle->dev->absmin[ABS_Y];
249                                 if (size > 0)
250                                         list->oldy = ((value - handle->dev->absmin[ABS_Y]) * yres / size);
251                                 else
252                                         list->oldy = ((value - handle->dev->absmin[ABS_Y]));
253                                 break;
254                         case ABS_PRESSURE:
255                                 list->pendown = ((value > handle->dev-> absmin[ABS_PRESSURE])) ?
256                                     value - handle->dev->absmin[ABS_PRESSURE] : 0;
257                                 break;
258                         }
259                         break;
260
261                 case EV_REL:
262                         switch (code) {
263                         case REL_X:
264                                 if (!list->pendown)
265                                         return;
266                                 list->oldx += value;
267                                 if (list->oldx < 0)
268                                         list->oldx = 0;
269                                 else if (list->oldx > xres)
270                                         list->oldx = xres;
271                                 break;
272                         case REL_Y:
273                                 if (!list->pendown)
274                                         return;
275                                 list->oldy += value;
276                                 if (list->oldy < 0)
277                                         list->oldy = 0;
278                                 else if (list->oldy > xres)
279                                         list->oldy = xres;
280                                 break;
281                         }
282                         break;
283
284                 case EV_KEY:
285                         if (code == BTN_TOUCH || code == BTN_MOUSE) {
286                                 switch (value) {
287                                 case 0:
288                                         list->pendown = 0;
289                                         break;
290                                 case 1:
291                                         if (!list->pendown)
292                                                 list->pendown = 1;
293                                         break;
294                                 case 2:
295                                         return;
296                                 }
297                         } else
298                                 return;
299                         break;
300                 }
301                 do_gettimeofday(&time);
302                 list->event[list->head].millisecs = time.tv_usec / 100;
303                 list->event[list->head].pressure = list->pendown;
304                 list->event[list->head].x = list->oldx;
305                 list->event[list->head].y = list->oldy;
306                 list->head = (list->head + 1) & (TSDEV_BUFFER_SIZE - 1);
307                 kill_fasync(&list->fasync, SIGIO, POLL_IN);
308         }
309         wake_up_interruptible(&tsdev->wait);
310 }
311
312 static struct input_handle *tsdev_connect(struct input_handler *handler,
313                                           struct input_dev *dev,
314                                           struct input_device_id *id)
315 {
316         struct tsdev *tsdev;
317         int minor;
318
319         for (minor = 0; minor < TSDEV_MINORS && tsdev_table[minor];
320              minor++);
321         if (minor == TSDEV_MINORS) {
322                 printk(KERN_ERR
323                        "tsdev: You have way too many touchscreens\n");
324                 return NULL;
325         }
326
327         if (!(tsdev = kmalloc(sizeof(struct tsdev), GFP_KERNEL)))
328                 return NULL;
329         memset(tsdev, 0, sizeof(struct tsdev));
330
331         INIT_LIST_HEAD(&tsdev->list);
332         init_waitqueue_head(&tsdev->wait);
333
334         sprintf(tsdev->name, "ts%d", minor);
335
336         tsdev->exist = 1;
337         tsdev->minor = minor;
338         tsdev->handle.dev = dev;
339         tsdev->handle.name = tsdev->name;
340         tsdev->handle.handler = handler;
341         tsdev->handle.private = tsdev;
342
343         tsdev_table[minor] = tsdev;
344         
345         devfs_mk_cdev(MKDEV(INPUT_MAJOR, TSDEV_MINOR_BASE + minor),
346                         S_IFCHR|S_IRUGO|S_IWUSR, "input/ts%d", minor);
347         class_simple_device_add(input_class, 
348                                 MKDEV(INPUT_MAJOR, TSDEV_MINOR_BASE + minor),
349                                 dev->dev, "ts%d", minor);
350
351         return &tsdev->handle;
352 }
353
354 static void tsdev_disconnect(struct input_handle *handle)
355 {
356         struct tsdev *tsdev = handle->private;
357
358         tsdev->exist = 0;
359
360         if (tsdev->open) {
361                 input_close_device(handle);
362                 wake_up_interruptible(&tsdev->wait);
363         } else
364                 tsdev_free(tsdev);
365 }
366
367 static struct input_device_id tsdev_ids[] = {
368         {
369               .flags    = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_RELBIT,
370               .evbit    = { BIT(EV_KEY) | BIT(EV_REL) },
371               .keybit   = { [LONG(BTN_LEFT)] = BIT(BTN_LEFT) },
372               .relbit   = { BIT(REL_X) | BIT(REL_Y) },
373          },/* A mouse like device, at least one button, two relative axes */
374
375         {
376               .flags    = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_ABSBIT,
377               .evbit    = { BIT(EV_KEY) | BIT(EV_ABS) },
378               .keybit   = { [LONG(BTN_TOUCH)] = BIT(BTN_TOUCH) },
379               .absbit   = { BIT(ABS_X) | BIT(ABS_Y) },
380          },/* A tablet like device, at least touch detection, two absolute axes */
381
382         {},/* Terminating entry */
383 };
384
385 MODULE_DEVICE_TABLE(input, tsdev_ids);
386
387 static struct input_handler tsdev_handler = {
388         .event =        tsdev_event,
389         .connect =      tsdev_connect,
390         .disconnect =   tsdev_disconnect,
391         .fops =         &tsdev_fops,
392         .minor =        TSDEV_MINOR_BASE,
393         .name =         "tsdev",
394         .id_table =     tsdev_ids,
395 };
396
397 static int __init tsdev_init(void)
398 {
399         input_register_handler(&tsdev_handler);
400         printk(KERN_INFO "ts: Compaq touchscreen protocol output\n");
401         return 0;
402 }
403
404 static void __exit tsdev_exit(void)
405 {
406         input_unregister_handler(&tsdev_handler);
407 }
408
409 module_init(tsdev_init);
410 module_exit(tsdev_exit);