blob: 764970f5da2e471727d07322b009dcda44346c38 [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/smp_lock.h>
23#include <linux/random.h>
24#include <linux/major.h>
25#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
27#include <linux/miscdevice.h>
28#endif
29
30MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
31MODULE_DESCRIPTION("Mouse (ExplorerPS/2) device interfaces");
32MODULE_LICENSE("GPL");
33
34#ifndef CONFIG_INPUT_MOUSEDEV_SCREEN_X
35#define CONFIG_INPUT_MOUSEDEV_SCREEN_X 1024
36#endif
37#ifndef CONFIG_INPUT_MOUSEDEV_SCREEN_Y
38#define CONFIG_INPUT_MOUSEDEV_SCREEN_Y 768
39#endif
40
41static int xres = CONFIG_INPUT_MOUSEDEV_SCREEN_X;
Dmitry Torokhov84c12b22005-12-11 12:41:03 -050042module_param(xres, uint, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043MODULE_PARM_DESC(xres, "Horizontal screen resolution");
44
45static int yres = CONFIG_INPUT_MOUSEDEV_SCREEN_Y;
Dmitry Torokhov84c12b22005-12-11 12:41:03 -050046module_param(yres, uint, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047MODULE_PARM_DESC(yres, "Vertical screen resolution");
48
49static unsigned tap_time = 200;
Dmitry Torokhov84c12b22005-12-11 12:41:03 -050050module_param(tap_time, uint, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051MODULE_PARM_DESC(tap_time, "Tap time for touchpads in absolute mode (msecs)");
52
53struct mousedev_hw_data {
54 int dx, dy, dz;
55 int x, y;
56 int abs_event;
57 unsigned long buttons;
58};
59
60struct mousedev {
61 int exist;
62 int open;
63 int minor;
64 char name[16];
65 wait_queue_head_t wait;
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -040066 struct list_head client_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 struct input_handle handle;
68
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];
116static 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
127 if (mousedev->touch) {
128 size = dev->absmax[ABS_X] - dev->absmin[ABS_X];
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400129 if (size == 0)
130 size = 256 * 2;
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 switch (code) {
133 case ABS_X:
134 fx(0) = value;
135 if (mousedev->pkt_count >= 2) {
136 tmp = ((value - fx(2)) * (256 * FRACTION_DENOM)) / size;
137 tmp += mousedev->frac_dx;
138 mousedev->packet.dx = tmp / FRACTION_DENOM;
139 mousedev->frac_dx = tmp - mousedev->packet.dx * FRACTION_DENOM;
140 }
141 break;
142
143 case ABS_Y:
144 fy(0) = value;
145 if (mousedev->pkt_count >= 2) {
146 tmp = -((value - fy(2)) * (256 * FRACTION_DENOM)) / size;
147 tmp += mousedev->frac_dy;
148 mousedev->packet.dy = tmp / FRACTION_DENOM;
149 mousedev->frac_dy = tmp - mousedev->packet.dy * FRACTION_DENOM;
150 }
151 break;
152 }
153 }
154}
155
156static void mousedev_abs_event(struct input_dev *dev, struct mousedev *mousedev, unsigned int code, int value)
157{
158 int size;
159
160 switch (code) {
161 case ABS_X:
162 size = dev->absmax[ABS_X] - dev->absmin[ABS_X];
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400163 if (size == 0)
164 size = xres ? : 1;
165 if (value > dev->absmax[ABS_X])
166 value = dev->absmax[ABS_X];
167 if (value < dev->absmin[ABS_X])
168 value = dev->absmin[ABS_X];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 mousedev->packet.x = ((value - dev->absmin[ABS_X]) * xres) / size;
170 mousedev->packet.abs_event = 1;
171 break;
172
173 case ABS_Y:
174 size = dev->absmax[ABS_Y] - dev->absmin[ABS_Y];
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400175 if (size == 0)
176 size = yres ? : 1;
177 if (value > dev->absmax[ABS_Y])
178 value = dev->absmax[ABS_Y];
179 if (value < dev->absmin[ABS_Y])
180 value = dev->absmin[ABS_Y];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 mousedev->packet.y = yres - ((value - dev->absmin[ABS_Y]) * yres) / size;
182 mousedev->packet.abs_event = 1;
183 break;
184 }
185}
186
187static void mousedev_rel_event(struct mousedev *mousedev, unsigned int code, int value)
188{
189 switch (code) {
190 case REL_X: mousedev->packet.dx += value; break;
191 case REL_Y: mousedev->packet.dy -= value; break;
192 case REL_WHEEL: mousedev->packet.dz -= value; break;
193 }
194}
195
196static void mousedev_key_event(struct mousedev *mousedev, unsigned int code, int value)
197{
198 int index;
199
200 switch (code) {
201 case BTN_TOUCH:
202 case BTN_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 case BTN_LEFT: index = 0; break;
204 case BTN_STYLUS:
205 case BTN_1:
206 case BTN_RIGHT: index = 1; break;
207 case BTN_2:
Marton Nemeth6c595fb2006-11-17 01:06:54 -0500208 case BTN_FORWARD:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 case BTN_STYLUS2:
210 case BTN_MIDDLE: index = 2; break;
211 case BTN_3:
212 case BTN_BACK:
213 case BTN_SIDE: index = 3; break;
214 case BTN_4:
215 case BTN_EXTRA: index = 4; break;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400216 default: return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 }
218
219 if (value) {
220 set_bit(index, &mousedev->packet.buttons);
221 set_bit(index, &mousedev_mix.packet.buttons);
222 } else {
223 clear_bit(index, &mousedev->packet.buttons);
224 clear_bit(index, &mousedev_mix.packet.buttons);
225 }
226}
227
228static void mousedev_notify_readers(struct mousedev *mousedev, struct mousedev_hw_data *packet)
229{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400230 struct mousedev_client *client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 struct mousedev_motion *p;
232 unsigned long flags;
Dmitry Torokhov81211522005-06-01 02:39:36 -0500233 int wake_readers = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400235 list_for_each_entry(client, &mousedev->client_list, node) {
236 spin_lock_irqsave(&client->packet_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400238 p = &client->packets[client->head];
239 if (client->ready && p->buttons != mousedev->packet.buttons) {
240 unsigned int new_head = (client->head + 1) % PACKET_QUEUE_LEN;
241 if (new_head != client->tail) {
242 p = &client->packets[client->head = new_head];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 memset(p, 0, sizeof(struct mousedev_motion));
244 }
245 }
246
247 if (packet->abs_event) {
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400248 p->dx += packet->x - client->pos_x;
249 p->dy += packet->y - client->pos_y;
250 client->pos_x = packet->x;
251 client->pos_y = packet->y;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
253
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400254 client->pos_x += packet->dx;
255 client->pos_x = client->pos_x < 0 ? 0 : (client->pos_x >= xres ? xres : client->pos_x);
256 client->pos_y += packet->dy;
257 client->pos_y = client->pos_y < 0 ? 0 : (client->pos_y >= yres ? yres : client->pos_y);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 p->dx += packet->dx;
260 p->dy += packet->dy;
261 p->dz += packet->dz;
262 p->buttons = mousedev->packet.buttons;
263
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400264 if (p->dx || p->dy || p->dz || p->buttons != client->last_buttons)
265 client->ready = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400267 spin_unlock_irqrestore(&client->packet_lock, flags);
Pavel Machekc1e4c8d2005-05-27 12:53:03 -0700268
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400269 if (client->ready) {
270 kill_fasync(&client->fasync, SIGIO, POLL_IN);
Dmitry Torokhov81211522005-06-01 02:39:36 -0500271 wake_readers = 1;
272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 }
274
Dmitry Torokhov81211522005-06-01 02:39:36 -0500275 if (wake_readers)
276 wake_up_interruptible(&mousedev->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278
279static void mousedev_touchpad_touch(struct mousedev *mousedev, int value)
280{
281 if (!value) {
282 if (mousedev->touch &&
283 time_before(jiffies, mousedev->touch + msecs_to_jiffies(tap_time))) {
284 /*
285 * Toggle left button to emulate tap.
286 * We rely on the fact that mousedev_mix always has 0
287 * motion packet so we won't mess current position.
288 */
289 set_bit(0, &mousedev->packet.buttons);
290 set_bit(0, &mousedev_mix.packet.buttons);
291 mousedev_notify_readers(mousedev, &mousedev_mix.packet);
292 mousedev_notify_readers(&mousedev_mix, &mousedev_mix.packet);
293 clear_bit(0, &mousedev->packet.buttons);
294 clear_bit(0, &mousedev_mix.packet.buttons);
295 }
296 mousedev->touch = mousedev->pkt_count = 0;
297 mousedev->frac_dx = 0;
298 mousedev->frac_dy = 0;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400299
300 } else if (!mousedev->touch)
301 mousedev->touch = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}
303
304static void mousedev_event(struct input_handle *handle, unsigned int type, unsigned int code, int value)
305{
306 struct mousedev *mousedev = handle->private;
307
308 switch (type) {
309 case EV_ABS:
310 /* Ignore joysticks */
311 if (test_bit(BTN_TRIGGER, handle->dev->keybit))
312 return;
313
314 if (test_bit(BTN_TOOL_FINGER, handle->dev->keybit))
315 mousedev_touchpad_event(handle->dev, mousedev, code, value);
316 else
317 mousedev_abs_event(handle->dev, mousedev, code, value);
318
319 break;
320
321 case EV_REL:
322 mousedev_rel_event(mousedev, code, value);
323 break;
324
325 case EV_KEY:
326 if (value != 2) {
327 if (code == BTN_TOUCH && test_bit(BTN_TOOL_FINGER, handle->dev->keybit))
328 mousedev_touchpad_touch(mousedev, value);
329 else
330 mousedev_key_event(mousedev, code, value);
331 }
332 break;
333
334 case EV_SYN:
335 if (code == SYN_REPORT) {
336 if (mousedev->touch) {
337 mousedev->pkt_count++;
338 /* Input system eats duplicate events, but we need all of them
339 * to do correct averaging so apply present one forward
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400340 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 fx(0) = fx(1);
342 fy(0) = fy(1);
343 }
344
345 mousedev_notify_readers(mousedev, &mousedev->packet);
346 mousedev_notify_readers(&mousedev_mix, &mousedev->packet);
347
348 mousedev->packet.dx = mousedev->packet.dy = mousedev->packet.dz = 0;
349 mousedev->packet.abs_event = 0;
350 }
351 break;
352 }
353}
354
355static int mousedev_fasync(int fd, struct file *file, int on)
356{
357 int retval;
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400358 struct mousedev_client *client = file->private_data;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400359
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400360 retval = fasync_helper(fd, file, on, &client->fasync);
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 return retval < 0 ? retval : 0;
363}
364
365static void mousedev_free(struct mousedev *mousedev)
366{
367 mousedev_table[mousedev->minor] = NULL;
368 kfree(mousedev);
369}
370
Dmitry Torokhovd542ed82007-04-12 01:30:15 -0400371static int mixdev_add_device(struct mousedev *mousedev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
Dmitry Torokhovd542ed82007-04-12 01:30:15 -0400373 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Dmitry Torokhovd542ed82007-04-12 01:30:15 -0400375 if (mousedev_mix.open) {
376 error = input_open_device(&mousedev->handle);
377 if (error)
378 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Dmitry Torokhovd542ed82007-04-12 01:30:15 -0400380 mousedev->open++;
381 mousedev->mixdev_open++;
382 }
383
384 list_add_tail(&mousedev->mixdev_node, &mousedev_mix_list);
385
386 return 0;
387}
388
389static void mixdev_remove_device(struct mousedev *mousedev)
390{
391 if (mousedev->mixdev_open) {
392 mousedev->mixdev_open = 0;
393 if (!--mousedev->open && mousedev->exist)
394 input_close_device(&mousedev->handle);
395 }
396
397 list_del_init(&mousedev->mixdev_node);
398}
399
400static void mixdev_open_devices(void)
401{
402 struct mousedev *mousedev;
403
404 list_for_each_entry(mousedev, &mousedev_mix_list, mixdev_node) {
405 if (mousedev->exist && !mousedev->open) {
406 if (input_open_device(&mousedev->handle))
407 continue;
408
409 mousedev->open++;
410 mousedev->mixdev_open++;
411 }
412 }
413}
414
415static void mixdev_close_devices(void)
416{
417 struct mousedev *mousedev, *next;
418
419 list_for_each_entry_safe(mousedev, next, &mousedev_mix_list, mixdev_node) {
420 if (mousedev->mixdev_open) {
421 mousedev->mixdev_open = 0;
422 if (!--mousedev->open) {
423 if (mousedev->exist)
424 input_close_device(&mousedev->handle);
425 else
426 mousedev_free(mousedev);
427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400432static int mousedev_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400434 struct mousedev_client *client = file->private_data;
435 struct mousedev *mousedev = client->mousedev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437 mousedev_fasync(-1, file, 0);
438
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400439 list_del(&client->node);
440 kfree(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400442 if (!--mousedev->open) {
443 if (mousedev->minor == MOUSEDEV_MIX)
Dmitry Torokhovd542ed82007-04-12 01:30:15 -0400444 mixdev_close_devices();
445 else if (mousedev->exist)
446 input_close_device(&mousedev->handle);
447 else
448 mousedev_free(mousedev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 }
450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 return 0;
452}
453
Dmitry Torokhovd542ed82007-04-12 01:30:15 -0400454
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400455static int mousedev_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400457 struct mousedev_client *client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 struct mousedev *mousedev;
Dmitry Torokhovd542ed82007-04-12 01:30:15 -0400459 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 int i;
461
462#ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
463 if (imajor(inode) == MISC_MAJOR)
464 i = MOUSEDEV_MIX;
465 else
466#endif
467 i = iminor(inode) - MOUSEDEV_MINOR_BASE;
468
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400469 if (i >= MOUSEDEV_MINORS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 return -ENODEV;
471
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400472 mousedev = mousedev_table[i];
473 if (!mousedev)
474 return -ENODEV;
475
476 client = kzalloc(sizeof(struct mousedev_client), GFP_KERNEL);
477 if (!client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400480 spin_lock_init(&client->packet_lock);
481 client->pos_x = xres / 2;
482 client->pos_y = yres / 2;
483 client->mousedev = mousedev;
484 list_add_tail(&client->node, &mousedev->client_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400486 if (!mousedev->open++) {
Dmitry Torokhovd542ed82007-04-12 01:30:15 -0400487 if (mousedev->minor == MOUSEDEV_MIX)
488 mixdev_open_devices();
489 else if (mousedev->exist) {
490 error = input_open_device(&mousedev->handle);
491 if (error) {
492 list_del(&client->node);
493 kfree(client);
494 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 }
Dmitry Torokhovd542ed82007-04-12 01:30:15 -0400496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 }
498
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400499 file->private_data = client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 return 0;
501}
502
503static inline int mousedev_limit_delta(int delta, int limit)
504{
505 return delta > limit ? limit : (delta < -limit ? -limit : delta);
506}
507
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400508static void mousedev_packet(struct mousedev_client *client, signed char *ps2_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
510 struct mousedev_motion *p;
511 unsigned long flags;
512
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400513 spin_lock_irqsave(&client->packet_lock, flags);
514 p = &client->packets[client->tail];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 ps2_data[0] = 0x08 | ((p->dx < 0) << 4) | ((p->dy < 0) << 5) | (p->buttons & 0x07);
517 ps2_data[1] = mousedev_limit_delta(p->dx, 127);
518 ps2_data[2] = mousedev_limit_delta(p->dy, 127);
519 p->dx -= ps2_data[1];
520 p->dy -= ps2_data[2];
521
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400522 switch (client->mode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 case MOUSEDEV_EMUL_EXPS:
524 ps2_data[3] = mousedev_limit_delta(p->dz, 7);
525 p->dz -= ps2_data[3];
526 ps2_data[3] = (ps2_data[3] & 0x0f) | ((p->buttons & 0x18) << 1);
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400527 client->bufsiz = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 break;
529
530 case MOUSEDEV_EMUL_IMPS:
531 ps2_data[0] |= ((p->buttons & 0x10) >> 3) | ((p->buttons & 0x08) >> 1);
532 ps2_data[3] = mousedev_limit_delta(p->dz, 127);
533 p->dz -= ps2_data[3];
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400534 client->bufsiz = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 break;
536
537 case MOUSEDEV_EMUL_PS2:
538 default:
539 ps2_data[0] |= ((p->buttons & 0x10) >> 3) | ((p->buttons & 0x08) >> 1);
540 p->dz = 0;
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400541 client->bufsiz = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 break;
543 }
544
545 if (!p->dx && !p->dy && !p->dz) {
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400546 if (client->tail == client->head) {
547 client->ready = 0;
548 client->last_buttons = p->buttons;
Pavel Machekc1e4c8d2005-05-27 12:53:03 -0700549 } else
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400550 client->tail = (client->tail + 1) % PACKET_QUEUE_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 }
552
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400553 spin_unlock_irqrestore(&client->packet_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554}
555
556
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400557static ssize_t mousedev_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400559 struct mousedev_client *client = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 unsigned char c;
561 unsigned int i;
562
563 for (i = 0; i < count; i++) {
564
565 if (get_user(c, buffer + i))
566 return -EFAULT;
567
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400568 if (c == mousedev_imex_seq[client->imexseq]) {
569 if (++client->imexseq == MOUSEDEV_SEQ_LEN) {
570 client->imexseq = 0;
571 client->mode = MOUSEDEV_EMUL_EXPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 }
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400573 } else
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400574 client->imexseq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400576 if (c == mousedev_imps_seq[client->impsseq]) {
577 if (++client->impsseq == MOUSEDEV_SEQ_LEN) {
578 client->impsseq = 0;
579 client->mode = MOUSEDEV_EMUL_IMPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 }
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400581 } else
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400582 client->impsseq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400584 client->ps2[0] = 0xfa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586 switch (c) {
587
588 case 0xeb: /* Poll */
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400589 mousedev_packet(client, &client->ps2[1]);
590 client->bufsiz++; /* account for leading ACK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 break;
592
593 case 0xf2: /* Get ID */
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400594 switch (client->mode) {
595 case MOUSEDEV_EMUL_PS2: client->ps2[1] = 0; break;
596 case MOUSEDEV_EMUL_IMPS: client->ps2[1] = 3; break;
597 case MOUSEDEV_EMUL_EXPS: client->ps2[1] = 4; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 }
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400599 client->bufsiz = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 break;
601
602 case 0xe9: /* Get info */
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400603 client->ps2[1] = 0x60; client->ps2[2] = 3; client->ps2[3] = 200;
604 client->bufsiz = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 break;
606
607 case 0xff: /* Reset */
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400608 client->impsseq = client->imexseq = 0;
609 client->mode = MOUSEDEV_EMUL_PS2;
610 client->ps2[1] = 0xaa; client->ps2[2] = 0x00;
611 client->bufsiz = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 break;
613
614 default:
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400615 client->bufsiz = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 break;
617 }
618
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400619 client->buffer = client->bufsiz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 }
621
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400622 kill_fasync(&client->fasync, SIGIO, POLL_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400624 wake_up_interruptible(&client->mousedev->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626 return count;
627}
628
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400629static ssize_t mousedev_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400631 struct mousedev_client *client = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 int retval = 0;
633
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400634 if (!client->ready && !client->buffer && (file->f_flags & O_NONBLOCK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 return -EAGAIN;
636
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400637 retval = wait_event_interruptible(client->mousedev->wait,
638 !client->mousedev->exist || client->ready || client->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640 if (retval)
641 return retval;
642
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400643 if (!client->mousedev->exist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 return -ENODEV;
645
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400646 if (!client->buffer && client->ready) {
647 mousedev_packet(client, client->ps2);
648 client->buffer = client->bufsiz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 }
650
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400651 if (count > client->buffer)
652 count = client->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400654 client->buffer -= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400656 if (copy_to_user(buffer, client->ps2 + client->bufsiz - client->buffer - count, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 return -EFAULT;
658
659 return count;
660}
661
662/* No kernel lock - fine */
663static unsigned int mousedev_poll(struct file *file, poll_table *wait)
664{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400665 struct mousedev_client *client = file->private_data;
666 struct mousedev *mousedev = client->mousedev;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400667
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400668 poll_wait(file, &mousedev->wait, wait);
669 return ((client->ready || client->buffer) ? (POLLIN | POLLRDNORM) : 0) |
670 (mousedev->exist ? 0 : (POLLHUP | POLLERR));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671}
672
Dmitry Torokhov66e66112006-09-14 01:31:59 -0400673static const struct file_operations mousedev_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 .owner = THIS_MODULE,
675 .read = mousedev_read,
676 .write = mousedev_write,
677 .poll = mousedev_poll,
678 .open = mousedev_open,
679 .release = mousedev_release,
680 .fasync = mousedev_fasync,
681};
682
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400683static int mousedev_connect(struct input_handler *handler, struct input_dev *dev,
684 const struct input_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
686 struct mousedev *mousedev;
Greg Kroah-Hartmanc9bcd582005-10-27 22:25:43 -0700687 struct class_device *cdev;
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400688 dev_t devt;
689 int minor;
690 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692 for (minor = 0; minor < MOUSEDEV_MINORS && mousedev_table[minor]; minor++);
693 if (minor == MOUSEDEV_MINORS) {
694 printk(KERN_ERR "mousedev: no more free mousedev devices\n");
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400695 return -ENFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 }
697
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400698 mousedev = kzalloc(sizeof(struct mousedev), GFP_KERNEL);
699 if (!mousedev)
700 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400702 INIT_LIST_HEAD(&mousedev->client_list);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400703 INIT_LIST_HEAD(&mousedev->mixdev_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 init_waitqueue_head(&mousedev->wait);
705
706 mousedev->minor = minor;
707 mousedev->exist = 1;
708 mousedev->handle.dev = dev;
709 mousedev->handle.name = mousedev->name;
710 mousedev->handle.handler = handler;
711 mousedev->handle.private = mousedev;
712 sprintf(mousedev->name, "mouse%d", minor);
713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 mousedev_table[minor] = mousedev;
715
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400716 devt = MKDEV(INPUT_MAJOR, MOUSEDEV_MINOR_BASE + minor),
717
718 cdev = class_device_create(&input_class, &dev->cdev, devt,
719 dev->cdev.dev, mousedev->name);
720 if (IS_ERR(cdev)) {
721 error = PTR_ERR(cdev);
722 goto err_free_mousedev;
723 }
Greg Kroah-Hartmanc9bcd582005-10-27 22:25:43 -0700724
725 /* temporary symlink to keep userspace happy */
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400726 error = sysfs_create_link(&input_class.subsys.kset.kobj,
727 &cdev->kobj, mousedev->name);
728 if (error)
729 goto err_cdev_destroy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400731 error = input_register_handle(&mousedev->handle);
732 if (error)
733 goto err_remove_link;
734
Dmitry Torokhovd542ed82007-04-12 01:30:15 -0400735 error = mixdev_add_device(mousedev);
736 if (error)
737 goto err_unregister_handle;
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400738
739 return 0;
740
741 err_unregister_handle:
742 input_unregister_handle(&mousedev->handle);
743 err_remove_link:
744 sysfs_remove_link(&input_class.subsys.kset.kobj, mousedev->name);
745 err_cdev_destroy:
746 class_device_destroy(&input_class, devt);
747 err_free_mousedev:
748 mousedev_table[minor] = NULL;
749 kfree(mousedev);
750 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751}
752
753static void mousedev_disconnect(struct input_handle *handle)
754{
755 struct mousedev *mousedev = handle->private;
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400756 struct mousedev_client *client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400758 input_unregister_handle(handle);
759
Greg Kroah-Hartmanc9bcd582005-10-27 22:25:43 -0700760 sysfs_remove_link(&input_class.subsys.kset.kobj, mousedev->name);
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -0700761 class_device_destroy(&input_class,
gregkh@suse.de12356862005-03-15 14:26:30 -0800762 MKDEV(INPUT_MAJOR, MOUSEDEV_MINOR_BASE + mousedev->minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 mousedev->exist = 0;
764
Dmitry Torokhovd542ed82007-04-12 01:30:15 -0400765 mixdev_remove_device(mousedev);
766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 if (mousedev->open) {
768 input_close_device(handle);
769 wake_up_interruptible(&mousedev->wait);
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400770 list_for_each_entry(client, &mousedev->client_list, node)
771 kill_fasync(&client->fasync, SIGIO, POLL_HUP);
Dmitry Torokhovd542ed82007-04-12 01:30:15 -0400772 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 mousedev_free(mousedev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774}
775
Dmitry Torokhov66e66112006-09-14 01:31:59 -0400776static const struct input_device_id mousedev_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 {
778 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_RELBIT,
779 .evbit = { BIT(EV_KEY) | BIT(EV_REL) },
780 .keybit = { [LONG(BTN_LEFT)] = BIT(BTN_LEFT) },
781 .relbit = { BIT(REL_X) | BIT(REL_Y) },
782 }, /* A mouse like device, at least one button, two relative axes */
783 {
784 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_RELBIT,
785 .evbit = { BIT(EV_KEY) | BIT(EV_REL) },
786 .relbit = { BIT(REL_WHEEL) },
787 }, /* A separate scrollwheel */
788 {
789 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_ABSBIT,
790 .evbit = { BIT(EV_KEY) | BIT(EV_ABS) },
791 .keybit = { [LONG(BTN_TOUCH)] = BIT(BTN_TOUCH) },
792 .absbit = { BIT(ABS_X) | BIT(ABS_Y) },
793 }, /* A tablet like device, at least touch detection, two absolute axes */
794 {
795 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_ABSBIT,
796 .evbit = { BIT(EV_KEY) | BIT(EV_ABS) },
797 .keybit = { [LONG(BTN_TOOL_FINGER)] = BIT(BTN_TOOL_FINGER) },
798 .absbit = { BIT(ABS_X) | BIT(ABS_Y) | BIT(ABS_PRESSURE) | BIT(ABS_TOOL_WIDTH) },
799 }, /* A touchpad */
800
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400801 { }, /* Terminating entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802};
803
804MODULE_DEVICE_TABLE(input, mousedev_ids);
805
806static struct input_handler mousedev_handler = {
807 .event = mousedev_event,
808 .connect = mousedev_connect,
809 .disconnect = mousedev_disconnect,
810 .fops = &mousedev_fops,
811 .minor = MOUSEDEV_MINOR_BASE,
812 .name = "mousedev",
813 .id_table = mousedev_ids,
814};
815
816#ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
817static struct miscdevice psaux_mouse = {
818 PSMOUSE_MINOR, "psaux", &mousedev_fops
819};
820static int psaux_registered;
821#endif
822
823static int __init mousedev_init(void)
824{
Dmitry Torokhov4263cf02006-09-14 01:32:39 -0400825 struct class_device *cdev;
826 int error;
827
828 error = input_register_handler(&mousedev_handler);
829 if (error)
830 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
832 memset(&mousedev_mix, 0, sizeof(struct mousedev));
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400833 INIT_LIST_HEAD(&mousedev_mix.client_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 init_waitqueue_head(&mousedev_mix.wait);
835 mousedev_table[MOUSEDEV_MIX] = &mousedev_mix;
836 mousedev_mix.exist = 1;
837 mousedev_mix.minor = MOUSEDEV_MIX;
838
Dmitry Torokhov4263cf02006-09-14 01:32:39 -0400839 cdev = class_device_create(&input_class, NULL,
gregkh@suse.de12356862005-03-15 14:26:30 -0800840 MKDEV(INPUT_MAJOR, MOUSEDEV_MINOR_BASE + MOUSEDEV_MIX), NULL, "mice");
Dmitry Torokhov4263cf02006-09-14 01:32:39 -0400841 if (IS_ERR(cdev)) {
842 input_unregister_handler(&mousedev_handler);
843 return PTR_ERR(cdev);
844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
846#ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
Dmitry Torokhov4263cf02006-09-14 01:32:39 -0400847 error = misc_register(&psaux_mouse);
848 if (error)
849 printk(KERN_WARNING "mice: could not register psaux device, "
850 "error: %d\n", error);
851 else
852 psaux_registered = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853#endif
854
855 printk(KERN_INFO "mice: PS/2 mouse device common for all mice\n");
856
857 return 0;
858}
859
860static void __exit mousedev_exit(void)
861{
862#ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
863 if (psaux_registered)
864 misc_deregister(&psaux_mouse);
865#endif
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -0700866 class_device_destroy(&input_class,
gregkh@suse.de12356862005-03-15 14:26:30 -0800867 MKDEV(INPUT_MAJOR, MOUSEDEV_MINOR_BASE + MOUSEDEV_MIX));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 input_unregister_handler(&mousedev_handler);
869}
870
871module_init(mousedev_init);
872module_exit(mousedev_exit);