blob: 9173916b8be5f34b84a02d1697ad8bdcc0483676 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Input driver to ExplorerPS/2 device driver module.
3 *
4 * Copyright (c) 1999-2002 Vojtech Pavlik
5 * Copyright (c) 2004 Dmitry Torokhov
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as published by
9 * the Free Software Foundation.
10 */
11
Dmitry Torokhov4f004692005-09-15 02:01:38 -050012#define MOUSEDEV_MINOR_BASE 32
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#define MOUSEDEV_MINORS 32
14#define MOUSEDEV_MIX 31
15
16#include <linux/slab.h>
17#include <linux/poll.h>
18#include <linux/module.h>
19#include <linux/moduleparam.h>
20#include <linux/init.h>
21#include <linux/input.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/random.h>
23#include <linux/major.h>
24#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
26#include <linux/miscdevice.h>
27#endif
28
29MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
30MODULE_DESCRIPTION("Mouse (ExplorerPS/2) device interfaces");
31MODULE_LICENSE("GPL");
32
33#ifndef CONFIG_INPUT_MOUSEDEV_SCREEN_X
34#define CONFIG_INPUT_MOUSEDEV_SCREEN_X 1024
35#endif
36#ifndef CONFIG_INPUT_MOUSEDEV_SCREEN_Y
37#define CONFIG_INPUT_MOUSEDEV_SCREEN_Y 768
38#endif
39
40static int xres = CONFIG_INPUT_MOUSEDEV_SCREEN_X;
Dmitry Torokhov84c12b22005-12-11 12:41:03 -050041module_param(xres, uint, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042MODULE_PARM_DESC(xres, "Horizontal screen resolution");
43
44static int yres = CONFIG_INPUT_MOUSEDEV_SCREEN_Y;
Dmitry Torokhov84c12b22005-12-11 12:41:03 -050045module_param(yres, uint, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046MODULE_PARM_DESC(yres, "Vertical screen resolution");
47
48static unsigned tap_time = 200;
Dmitry Torokhov84c12b22005-12-11 12:41:03 -050049module_param(tap_time, uint, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050MODULE_PARM_DESC(tap_time, "Tap time for touchpads in absolute mode (msecs)");
51
52struct mousedev_hw_data {
53 int dx, dy, dz;
54 int x, y;
55 int abs_event;
56 unsigned long buttons;
57};
58
59struct mousedev {
60 int exist;
61 int open;
62 int minor;
63 char name[16];
64 wait_queue_head_t wait;
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -040065 struct list_head client_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 struct input_handle handle;
Dmitry Torokhov9657d752007-06-14 23:32:24 -040067 struct device dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Dmitry Torokhovd542ed82007-04-12 01:30:15 -040069 struct list_head mixdev_node;
70 int mixdev_open;
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 struct mousedev_hw_data packet;
73 unsigned int pkt_count;
74 int old_x[4], old_y[4];
75 int frac_dx, frac_dy;
76 unsigned long touch;
77};
78
79enum mousedev_emul {
80 MOUSEDEV_EMUL_PS2,
81 MOUSEDEV_EMUL_IMPS,
82 MOUSEDEV_EMUL_EXPS
83};
84
85struct mousedev_motion {
86 int dx, dy, dz;
87 unsigned long buttons;
88};
89
90#define PACKET_QUEUE_LEN 16
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -040091struct mousedev_client {
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 struct fasync_struct *fasync;
93 struct mousedev *mousedev;
94 struct list_head node;
95
96 struct mousedev_motion packets[PACKET_QUEUE_LEN];
97 unsigned int head, tail;
98 spinlock_t packet_lock;
99 int pos_x, pos_y;
100
101 signed char ps2[6];
102 unsigned char ready, buffer, bufsiz;
103 unsigned char imexseq, impsseq;
104 enum mousedev_emul mode;
Pavel Machekc1e4c8d2005-05-27 12:53:03 -0700105 unsigned long last_buttons;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106};
107
108#define MOUSEDEV_SEQ_LEN 6
109
110static unsigned char mousedev_imps_seq[] = { 0xf3, 200, 0xf3, 100, 0xf3, 80 };
111static unsigned char mousedev_imex_seq[] = { 0xf3, 200, 0xf3, 200, 0xf3, 80 };
112
113static struct input_handler mousedev_handler;
114
115static struct mousedev *mousedev_table[MOUSEDEV_MINORS];
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400116static struct mousedev *mousedev_mix;
Dmitry Torokhovd542ed82007-04-12 01:30:15 -0400117static LIST_HEAD(mousedev_mix_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119#define fx(i) (mousedev->old_x[(mousedev->pkt_count - (i)) & 03])
120#define fy(i) (mousedev->old_y[(mousedev->pkt_count - (i)) & 03])
121
122static void mousedev_touchpad_event(struct input_dev *dev, struct mousedev *mousedev, unsigned int code, int value)
123{
124 int size, tmp;
125 enum { FRACTION_DENOM = 128 };
126
Dmitry Torokhov0d9d93c2007-04-12 01:31:55 -0400127 switch (code) {
128 case ABS_X:
129 fx(0) = value;
130 if (mousedev->touch && mousedev->pkt_count >= 2) {
131 size = dev->absmax[ABS_X] - dev->absmin[ABS_X];
132 if (size == 0)
133 size = 256 * 2;
134 tmp = ((value - fx(2)) * (256 * FRACTION_DENOM)) / size;
135 tmp += mousedev->frac_dx;
136 mousedev->packet.dx = tmp / FRACTION_DENOM;
137 mousedev->frac_dx = tmp - mousedev->packet.dx * FRACTION_DENOM;
138 }
139 break;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400140
Dmitry Torokhov0d9d93c2007-04-12 01:31:55 -0400141 case ABS_Y:
142 fy(0) = value;
143 if (mousedev->touch && mousedev->pkt_count >= 2) {
144 /* use X size to keep the same scale */
145 size = dev->absmax[ABS_X] - dev->absmin[ABS_X];
146 if (size == 0)
147 size = 256 * 2;
148 tmp = -((value - fy(2)) * (256 * FRACTION_DENOM)) / size;
149 tmp += mousedev->frac_dy;
150 mousedev->packet.dy = tmp / FRACTION_DENOM;
151 mousedev->frac_dy = tmp - mousedev->packet.dy * FRACTION_DENOM;
152 }
153 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 }
155}
156
157static void mousedev_abs_event(struct input_dev *dev, struct mousedev *mousedev, unsigned int code, int value)
158{
159 int size;
160
161 switch (code) {
162 case ABS_X:
163 size = dev->absmax[ABS_X] - dev->absmin[ABS_X];
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400164 if (size == 0)
165 size = xres ? : 1;
166 if (value > dev->absmax[ABS_X])
167 value = dev->absmax[ABS_X];
168 if (value < dev->absmin[ABS_X])
169 value = dev->absmin[ABS_X];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 mousedev->packet.x = ((value - dev->absmin[ABS_X]) * xres) / size;
171 mousedev->packet.abs_event = 1;
172 break;
173
174 case ABS_Y:
175 size = dev->absmax[ABS_Y] - dev->absmin[ABS_Y];
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400176 if (size == 0)
177 size = yres ? : 1;
178 if (value > dev->absmax[ABS_Y])
179 value = dev->absmax[ABS_Y];
180 if (value < dev->absmin[ABS_Y])
181 value = dev->absmin[ABS_Y];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 mousedev->packet.y = yres - ((value - dev->absmin[ABS_Y]) * yres) / size;
183 mousedev->packet.abs_event = 1;
184 break;
185 }
186}
187
188static void mousedev_rel_event(struct mousedev *mousedev, unsigned int code, int value)
189{
190 switch (code) {
191 case REL_X: mousedev->packet.dx += value; break;
192 case REL_Y: mousedev->packet.dy -= value; break;
193 case REL_WHEEL: mousedev->packet.dz -= value; break;
194 }
195}
196
197static void mousedev_key_event(struct mousedev *mousedev, unsigned int code, int value)
198{
199 int index;
200
201 switch (code) {
202 case BTN_TOUCH:
203 case BTN_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 case BTN_LEFT: index = 0; break;
205 case BTN_STYLUS:
206 case BTN_1:
207 case BTN_RIGHT: index = 1; break;
208 case BTN_2:
Marton Nemeth6c595fb2006-11-17 01:06:54 -0500209 case BTN_FORWARD:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 case BTN_STYLUS2:
211 case BTN_MIDDLE: index = 2; break;
212 case BTN_3:
213 case BTN_BACK:
214 case BTN_SIDE: index = 3; break;
215 case BTN_4:
216 case BTN_EXTRA: index = 4; break;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400217 default: return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 }
219
220 if (value) {
221 set_bit(index, &mousedev->packet.buttons);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400222 set_bit(index, &mousedev_mix->packet.buttons);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 } else {
224 clear_bit(index, &mousedev->packet.buttons);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400225 clear_bit(index, &mousedev_mix->packet.buttons);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 }
227}
228
229static void mousedev_notify_readers(struct mousedev *mousedev, struct mousedev_hw_data *packet)
230{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400231 struct mousedev_client *client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 struct mousedev_motion *p;
233 unsigned long flags;
Dmitry Torokhov81211522005-06-01 02:39:36 -0500234 int wake_readers = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400236 list_for_each_entry(client, &mousedev->client_list, node) {
237 spin_lock_irqsave(&client->packet_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400239 p = &client->packets[client->head];
240 if (client->ready && p->buttons != mousedev->packet.buttons) {
241 unsigned int new_head = (client->head + 1) % PACKET_QUEUE_LEN;
242 if (new_head != client->tail) {
243 p = &client->packets[client->head = new_head];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 memset(p, 0, sizeof(struct mousedev_motion));
245 }
246 }
247
248 if (packet->abs_event) {
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400249 p->dx += packet->x - client->pos_x;
250 p->dy += packet->y - client->pos_y;
251 client->pos_x = packet->x;
252 client->pos_y = packet->y;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 }
254
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400255 client->pos_x += packet->dx;
256 client->pos_x = client->pos_x < 0 ? 0 : (client->pos_x >= xres ? xres : client->pos_x);
257 client->pos_y += packet->dy;
258 client->pos_y = client->pos_y < 0 ? 0 : (client->pos_y >= yres ? yres : client->pos_y);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260 p->dx += packet->dx;
261 p->dy += packet->dy;
262 p->dz += packet->dz;
263 p->buttons = mousedev->packet.buttons;
264
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400265 if (p->dx || p->dy || p->dz || p->buttons != client->last_buttons)
266 client->ready = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400268 spin_unlock_irqrestore(&client->packet_lock, flags);
Pavel Machekc1e4c8d2005-05-27 12:53:03 -0700269
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400270 if (client->ready) {
271 kill_fasync(&client->fasync, SIGIO, POLL_IN);
Dmitry Torokhov81211522005-06-01 02:39:36 -0500272 wake_readers = 1;
273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 }
275
Dmitry Torokhov81211522005-06-01 02:39:36 -0500276 if (wake_readers)
277 wake_up_interruptible(&mousedev->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
280static void mousedev_touchpad_touch(struct mousedev *mousedev, int value)
281{
282 if (!value) {
283 if (mousedev->touch &&
284 time_before(jiffies, mousedev->touch + msecs_to_jiffies(tap_time))) {
285 /*
286 * Toggle left button to emulate tap.
287 * We rely on the fact that mousedev_mix always has 0
288 * motion packet so we won't mess current position.
289 */
290 set_bit(0, &mousedev->packet.buttons);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400291 set_bit(0, &mousedev_mix->packet.buttons);
292 mousedev_notify_readers(mousedev, &mousedev_mix->packet);
293 mousedev_notify_readers(mousedev_mix, &mousedev_mix->packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 clear_bit(0, &mousedev->packet.buttons);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400295 clear_bit(0, &mousedev_mix->packet.buttons);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 }
297 mousedev->touch = mousedev->pkt_count = 0;
298 mousedev->frac_dx = 0;
299 mousedev->frac_dy = 0;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400300
301 } else if (!mousedev->touch)
302 mousedev->touch = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
305static void mousedev_event(struct input_handle *handle, unsigned int type, unsigned int code, int value)
306{
307 struct mousedev *mousedev = handle->private;
308
309 switch (type) {
310 case EV_ABS:
311 /* Ignore joysticks */
312 if (test_bit(BTN_TRIGGER, handle->dev->keybit))
313 return;
314
315 if (test_bit(BTN_TOOL_FINGER, handle->dev->keybit))
316 mousedev_touchpad_event(handle->dev, mousedev, code, value);
317 else
318 mousedev_abs_event(handle->dev, mousedev, code, value);
319
320 break;
321
322 case EV_REL:
323 mousedev_rel_event(mousedev, code, value);
324 break;
325
326 case EV_KEY:
327 if (value != 2) {
328 if (code == BTN_TOUCH && test_bit(BTN_TOOL_FINGER, handle->dev->keybit))
329 mousedev_touchpad_touch(mousedev, value);
330 else
331 mousedev_key_event(mousedev, code, value);
332 }
333 break;
334
335 case EV_SYN:
336 if (code == SYN_REPORT) {
337 if (mousedev->touch) {
338 mousedev->pkt_count++;
339 /* Input system eats duplicate events, but we need all of them
340 * to do correct averaging so apply present one forward
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400341 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 fx(0) = fx(1);
343 fy(0) = fy(1);
344 }
345
346 mousedev_notify_readers(mousedev, &mousedev->packet);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400347 mousedev_notify_readers(mousedev_mix, &mousedev->packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349 mousedev->packet.dx = mousedev->packet.dy = mousedev->packet.dz = 0;
350 mousedev->packet.abs_event = 0;
351 }
352 break;
353 }
354}
355
356static int mousedev_fasync(int fd, struct file *file, int on)
357{
358 int retval;
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400359 struct mousedev_client *client = file->private_data;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400360
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400361 retval = fasync_helper(fd, file, on, &client->fasync);
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 return retval < 0 ? retval : 0;
364}
365
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400366static void mousedev_free(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400368 struct mousedev *mousedev = container_of(dev, struct mousedev, dev);
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 mousedev_table[mousedev->minor] = NULL;
371 kfree(mousedev);
372}
373
Dmitry Torokhovd542ed82007-04-12 01:30:15 -0400374static int mixdev_add_device(struct mousedev *mousedev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
Dmitry Torokhovd542ed82007-04-12 01:30:15 -0400376 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400378 if (mousedev_mix->open) {
Dmitry Torokhovd542ed82007-04-12 01:30:15 -0400379 error = input_open_device(&mousedev->handle);
380 if (error)
381 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Dmitry Torokhovd542ed82007-04-12 01:30:15 -0400383 mousedev->open++;
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400384 mousedev->mixdev_open = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 }
386
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400387 get_device(&mousedev->dev);
Dmitry Torokhovd542ed82007-04-12 01:30:15 -0400388 list_add_tail(&mousedev->mixdev_node, &mousedev_mix_list);
389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 return 0;
391}
392
Dmitry Torokhovd542ed82007-04-12 01:30:15 -0400393static void mixdev_remove_device(struct mousedev *mousedev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
Dmitry Torokhovd542ed82007-04-12 01:30:15 -0400395 if (mousedev->mixdev_open) {
396 mousedev->mixdev_open = 0;
397 if (!--mousedev->open && mousedev->exist)
398 input_close_device(&mousedev->handle);
399 }
400
401 list_del_init(&mousedev->mixdev_node);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400402 put_device(&mousedev->dev);
Dmitry Torokhovd542ed82007-04-12 01:30:15 -0400403}
404
405static void mixdev_open_devices(void)
406{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 struct mousedev *mousedev;
Dmitry Torokhovd542ed82007-04-12 01:30:15 -0400408
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400409 if (mousedev_mix->open++)
410 return;
411
Dmitry Torokhovd542ed82007-04-12 01:30:15 -0400412 list_for_each_entry(mousedev, &mousedev_mix_list, mixdev_node) {
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400413 if (!mousedev->mixdev_open) {
414 if (!mousedev->open && mousedev->exist)
415 if (input_open_device(&mousedev->handle))
416 continue;
Dmitry Torokhovd542ed82007-04-12 01:30:15 -0400417
418 mousedev->open++;
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400419 mousedev->mixdev_open = 1;
Dmitry Torokhovd542ed82007-04-12 01:30:15 -0400420 }
421 }
422}
423
424static void mixdev_close_devices(void)
425{
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400426 struct mousedev *mousedev;
Dmitry Torokhovd542ed82007-04-12 01:30:15 -0400427
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400428 if (--mousedev_mix->open)
429 return;
430
431 list_for_each_entry(mousedev, &mousedev_mix_list, mixdev_node) {
Dmitry Torokhovd542ed82007-04-12 01:30:15 -0400432 if (mousedev->mixdev_open) {
433 mousedev->mixdev_open = 0;
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400434 if (!--mousedev->open && mousedev->exist)
435 input_close_device(&mousedev->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 }
437 }
438}
439
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400440static int mousedev_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400442 struct mousedev_client *client = file->private_data;
443 struct mousedev *mousedev = client->mousedev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 mousedev_fasync(-1, file, 0);
446
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400447 list_del(&client->node);
448 kfree(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400450 if (mousedev->minor == MOUSEDEV_MIX)
451 mixdev_close_devices();
452 else if (!--mousedev->open && mousedev->exist)
453 input_close_device(&mousedev->handle);
454
455 put_device(&mousedev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 return 0;
458}
459
Dmitry Torokhovd542ed82007-04-12 01:30:15 -0400460
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400461static int mousedev_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400463 struct mousedev_client *client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 struct mousedev *mousedev;
Dmitry Torokhovd542ed82007-04-12 01:30:15 -0400465 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 int i;
467
468#ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
469 if (imajor(inode) == MISC_MAJOR)
470 i = MOUSEDEV_MIX;
471 else
472#endif
473 i = iminor(inode) - MOUSEDEV_MINOR_BASE;
474
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400475 if (i >= MOUSEDEV_MINORS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 return -ENODEV;
477
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400478 mousedev = mousedev_table[i];
479 if (!mousedev)
480 return -ENODEV;
481
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400482 get_device(&mousedev->dev);
483
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400484 client = kzalloc(sizeof(struct mousedev_client), GFP_KERNEL);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400485 if (!client) {
486 error = -ENOMEM;
487 goto err_put_mousedev;
488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400490 spin_lock_init(&client->packet_lock);
491 client->pos_x = xres / 2;
492 client->pos_y = yres / 2;
493 client->mousedev = mousedev;
494 list_add_tail(&client->node, &mousedev->client_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400496 if (mousedev->minor == MOUSEDEV_MIX)
497 mixdev_open_devices();
498 else if (!mousedev->open++ && mousedev->exist) {
499 error = input_open_device(&mousedev->handle);
500 if (error)
501 goto err_free_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 }
503
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400504 file->private_data = client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 return 0;
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400506
507 err_free_client:
508 list_del(&client->node);
509 kfree(client);
510 err_put_mousedev:
511 put_device(&mousedev->dev);
512 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
514
515static inline int mousedev_limit_delta(int delta, int limit)
516{
517 return delta > limit ? limit : (delta < -limit ? -limit : delta);
518}
519
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400520static void mousedev_packet(struct mousedev_client *client, signed char *ps2_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
522 struct mousedev_motion *p;
523 unsigned long flags;
524
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400525 spin_lock_irqsave(&client->packet_lock, flags);
526 p = &client->packets[client->tail];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528 ps2_data[0] = 0x08 | ((p->dx < 0) << 4) | ((p->dy < 0) << 5) | (p->buttons & 0x07);
529 ps2_data[1] = mousedev_limit_delta(p->dx, 127);
530 ps2_data[2] = mousedev_limit_delta(p->dy, 127);
531 p->dx -= ps2_data[1];
532 p->dy -= ps2_data[2];
533
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400534 switch (client->mode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 case MOUSEDEV_EMUL_EXPS:
536 ps2_data[3] = mousedev_limit_delta(p->dz, 7);
537 p->dz -= ps2_data[3];
538 ps2_data[3] = (ps2_data[3] & 0x0f) | ((p->buttons & 0x18) << 1);
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400539 client->bufsiz = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 break;
541
542 case MOUSEDEV_EMUL_IMPS:
543 ps2_data[0] |= ((p->buttons & 0x10) >> 3) | ((p->buttons & 0x08) >> 1);
544 ps2_data[3] = mousedev_limit_delta(p->dz, 127);
545 p->dz -= ps2_data[3];
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400546 client->bufsiz = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 break;
548
549 case MOUSEDEV_EMUL_PS2:
550 default:
551 ps2_data[0] |= ((p->buttons & 0x10) >> 3) | ((p->buttons & 0x08) >> 1);
552 p->dz = 0;
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400553 client->bufsiz = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 break;
555 }
556
557 if (!p->dx && !p->dy && !p->dz) {
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400558 if (client->tail == client->head) {
559 client->ready = 0;
560 client->last_buttons = p->buttons;
Pavel Machekc1e4c8d2005-05-27 12:53:03 -0700561 } else
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400562 client->tail = (client->tail + 1) % PACKET_QUEUE_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 }
564
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400565 spin_unlock_irqrestore(&client->packet_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566}
567
568
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400569static ssize_t mousedev_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400571 struct mousedev_client *client = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 unsigned char c;
573 unsigned int i;
574
575 for (i = 0; i < count; i++) {
576
577 if (get_user(c, buffer + i))
578 return -EFAULT;
579
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400580 if (c == mousedev_imex_seq[client->imexseq]) {
581 if (++client->imexseq == MOUSEDEV_SEQ_LEN) {
582 client->imexseq = 0;
583 client->mode = MOUSEDEV_EMUL_EXPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400585 } else
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400586 client->imexseq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400588 if (c == mousedev_imps_seq[client->impsseq]) {
589 if (++client->impsseq == MOUSEDEV_SEQ_LEN) {
590 client->impsseq = 0;
591 client->mode = MOUSEDEV_EMUL_IMPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 }
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400593 } else
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400594 client->impsseq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400596 client->ps2[0] = 0xfa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 switch (c) {
599
600 case 0xeb: /* Poll */
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400601 mousedev_packet(client, &client->ps2[1]);
602 client->bufsiz++; /* account for leading ACK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 break;
604
605 case 0xf2: /* Get ID */
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400606 switch (client->mode) {
607 case MOUSEDEV_EMUL_PS2: client->ps2[1] = 0; break;
608 case MOUSEDEV_EMUL_IMPS: client->ps2[1] = 3; break;
609 case MOUSEDEV_EMUL_EXPS: client->ps2[1] = 4; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 }
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400611 client->bufsiz = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 break;
613
614 case 0xe9: /* Get info */
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400615 client->ps2[1] = 0x60; client->ps2[2] = 3; client->ps2[3] = 200;
616 client->bufsiz = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 break;
618
619 case 0xff: /* Reset */
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400620 client->impsseq = client->imexseq = 0;
621 client->mode = MOUSEDEV_EMUL_PS2;
622 client->ps2[1] = 0xaa; client->ps2[2] = 0x00;
623 client->bufsiz = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 break;
625
626 default:
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400627 client->bufsiz = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 break;
629 }
630
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400631 client->buffer = client->bufsiz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 }
633
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400634 kill_fasync(&client->fasync, SIGIO, POLL_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400636 wake_up_interruptible(&client->mousedev->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638 return count;
639}
640
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400641static ssize_t mousedev_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400643 struct mousedev_client *client = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 int retval = 0;
645
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400646 if (!client->ready && !client->buffer && (file->f_flags & O_NONBLOCK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 return -EAGAIN;
648
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400649 retval = wait_event_interruptible(client->mousedev->wait,
650 !client->mousedev->exist || client->ready || client->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652 if (retval)
653 return retval;
654
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400655 if (!client->mousedev->exist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 return -ENODEV;
657
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400658 if (!client->buffer && client->ready) {
659 mousedev_packet(client, client->ps2);
660 client->buffer = client->bufsiz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 }
662
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400663 if (count > client->buffer)
664 count = client->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400666 client->buffer -= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400668 if (copy_to_user(buffer, client->ps2 + client->bufsiz - client->buffer - count, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return -EFAULT;
670
671 return count;
672}
673
674/* No kernel lock - fine */
675static unsigned int mousedev_poll(struct file *file, poll_table *wait)
676{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400677 struct mousedev_client *client = file->private_data;
678 struct mousedev *mousedev = client->mousedev;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400679
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400680 poll_wait(file, &mousedev->wait, wait);
681 return ((client->ready || client->buffer) ? (POLLIN | POLLRDNORM) : 0) |
682 (mousedev->exist ? 0 : (POLLHUP | POLLERR));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
684
Dmitry Torokhov66e66112006-09-14 01:31:59 -0400685static const struct file_operations mousedev_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 .owner = THIS_MODULE,
687 .read = mousedev_read,
688 .write = mousedev_write,
689 .poll = mousedev_poll,
690 .open = mousedev_open,
691 .release = mousedev_release,
692 .fasync = mousedev_fasync,
693};
694
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400695static struct mousedev *mousedev_create(struct input_dev *dev,
696 struct input_handler *handler,
697 int minor)
698{
699 struct mousedev *mousedev;
700 int error;
701
702 mousedev = kzalloc(sizeof(struct mousedev), GFP_KERNEL);
703 if (!mousedev) {
704 error = -ENOMEM;
705 goto err_out;
706 }
707
708 INIT_LIST_HEAD(&mousedev->client_list);
709 INIT_LIST_HEAD(&mousedev->mixdev_node);
710 init_waitqueue_head(&mousedev->wait);
711
712 if (minor == MOUSEDEV_MIX)
713 strlcpy(mousedev->name, "mice", sizeof(mousedev->name));
714 else
715 snprintf(mousedev->name, sizeof(mousedev->name),
716 "mouse%d", minor);
717
718 mousedev->minor = minor;
719 mousedev->exist = 1;
720 mousedev->handle.dev = dev;
721 mousedev->handle.name = mousedev->name;
722 mousedev->handle.handler = handler;
723 mousedev->handle.private = mousedev;
724
725 strlcpy(mousedev->dev.bus_id, mousedev->name,
726 sizeof(mousedev->dev.bus_id));
727 mousedev->dev.class = &input_class;
728 if (dev)
729 mousedev->dev.parent = &dev->dev;
730 mousedev->dev.devt = MKDEV(INPUT_MAJOR, MOUSEDEV_MINOR_BASE + minor);
731 mousedev->dev.release = mousedev_free;
732 device_initialize(&mousedev->dev);
733
734 mousedev_table[minor] = mousedev;
735
736 error = device_add(&mousedev->dev);
737 if (error)
738 goto err_free_mousedev;
739
740 return mousedev;
741
742 err_free_mousedev:
743 put_device(&mousedev->dev);
744 err_out:
745 return ERR_PTR(error);
746}
747
748static void mousedev_destroy(struct mousedev *mousedev)
749{
750 struct mousedev_client *client;
751
752 device_del(&mousedev->dev);
753 mousedev->exist = 0;
754
755 if (mousedev->open) {
756 input_close_device(&mousedev->handle);
757 list_for_each_entry(client, &mousedev->client_list, node)
758 kill_fasync(&client->fasync, SIGIO, POLL_HUP);
759 wake_up_interruptible(&mousedev->wait);
760 }
761
762 put_device(&mousedev->dev);
763}
764
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400765static int mousedev_connect(struct input_handler *handler, struct input_dev *dev,
766 const struct input_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
768 struct mousedev *mousedev;
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400769 int minor;
770 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
772 for (minor = 0; minor < MOUSEDEV_MINORS && mousedev_table[minor]; minor++);
773 if (minor == MOUSEDEV_MINORS) {
774 printk(KERN_ERR "mousedev: no more free mousedev devices\n");
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400775 return -ENFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 }
777
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400778 mousedev = mousedev_create(dev, handler, minor);
779 if (IS_ERR(mousedev))
780 return PTR_ERR(mousedev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400782 error = input_register_handle(&mousedev->handle);
783 if (error)
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400784 goto err_delete_mousedev;
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400785
Dmitry Torokhovd542ed82007-04-12 01:30:15 -0400786 error = mixdev_add_device(mousedev);
787 if (error)
788 goto err_unregister_handle;
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400789
790 return 0;
791
792 err_unregister_handle:
793 input_unregister_handle(&mousedev->handle);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400794 err_delete_mousedev:
795 device_unregister(&mousedev->dev);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400796 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797}
798
799static void mousedev_disconnect(struct input_handle *handle)
800{
801 struct mousedev *mousedev = handle->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Dmitry Torokhovd542ed82007-04-12 01:30:15 -0400803 mixdev_remove_device(mousedev);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400804 input_unregister_handle(handle);
805 mousedev_destroy(mousedev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806}
807
Dmitry Torokhov66e66112006-09-14 01:31:59 -0400808static const struct input_device_id mousedev_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 {
810 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_RELBIT,
811 .evbit = { BIT(EV_KEY) | BIT(EV_REL) },
812 .keybit = { [LONG(BTN_LEFT)] = BIT(BTN_LEFT) },
813 .relbit = { BIT(REL_X) | BIT(REL_Y) },
814 }, /* A mouse like device, at least one button, two relative axes */
815 {
816 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_RELBIT,
817 .evbit = { BIT(EV_KEY) | BIT(EV_REL) },
818 .relbit = { BIT(REL_WHEEL) },
819 }, /* A separate scrollwheel */
820 {
821 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_ABSBIT,
822 .evbit = { BIT(EV_KEY) | BIT(EV_ABS) },
823 .keybit = { [LONG(BTN_TOUCH)] = BIT(BTN_TOUCH) },
824 .absbit = { BIT(ABS_X) | BIT(ABS_Y) },
825 }, /* A tablet like device, at least touch detection, two absolute axes */
826 {
827 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_ABSBIT,
828 .evbit = { BIT(EV_KEY) | BIT(EV_ABS) },
829 .keybit = { [LONG(BTN_TOOL_FINGER)] = BIT(BTN_TOOL_FINGER) },
830 .absbit = { BIT(ABS_X) | BIT(ABS_Y) | BIT(ABS_PRESSURE) | BIT(ABS_TOOL_WIDTH) },
831 }, /* A touchpad */
832
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400833 { }, /* Terminating entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834};
835
836MODULE_DEVICE_TABLE(input, mousedev_ids);
837
838static struct input_handler mousedev_handler = {
839 .event = mousedev_event,
840 .connect = mousedev_connect,
841 .disconnect = mousedev_disconnect,
842 .fops = &mousedev_fops,
843 .minor = MOUSEDEV_MINOR_BASE,
844 .name = "mousedev",
845 .id_table = mousedev_ids,
846};
847
848#ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
849static struct miscdevice psaux_mouse = {
850 PSMOUSE_MINOR, "psaux", &mousedev_fops
851};
852static int psaux_registered;
853#endif
854
855static int __init mousedev_init(void)
856{
Dmitry Torokhov4263cf02006-09-14 01:32:39 -0400857 int error;
858
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400859 mousedev_mix = mousedev_create(NULL, &mousedev_handler, MOUSEDEV_MIX);
860 if (IS_ERR(mousedev_mix))
861 return PTR_ERR(mousedev_mix);
862
Dmitry Torokhov4263cf02006-09-14 01:32:39 -0400863 error = input_register_handler(&mousedev_handler);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400864 if (error) {
865 mousedev_destroy(mousedev_mix);
Dmitry Torokhov4263cf02006-09-14 01:32:39 -0400866 return error;
Dmitry Torokhov4263cf02006-09-14 01:32:39 -0400867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
869#ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
Dmitry Torokhov4263cf02006-09-14 01:32:39 -0400870 error = misc_register(&psaux_mouse);
871 if (error)
872 printk(KERN_WARNING "mice: could not register psaux device, "
873 "error: %d\n", error);
874 else
875 psaux_registered = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876#endif
877
878 printk(KERN_INFO "mice: PS/2 mouse device common for all mice\n");
879
880 return 0;
881}
882
883static void __exit mousedev_exit(void)
884{
885#ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
886 if (psaux_registered)
887 misc_deregister(&psaux_mouse);
888#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 input_unregister_handler(&mousedev_handler);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400890 mousedev_destroy(mousedev_mix);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891}
892
893module_init(mousedev_init);
894module_exit(mousedev_exit);