blob: f6a62687d9e46947e3c0d271d84cc4ea5f387df6 [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
69 struct mousedev_hw_data packet;
70 unsigned int pkt_count;
71 int old_x[4], old_y[4];
72 int frac_dx, frac_dy;
73 unsigned long touch;
74};
75
76enum mousedev_emul {
77 MOUSEDEV_EMUL_PS2,
78 MOUSEDEV_EMUL_IMPS,
79 MOUSEDEV_EMUL_EXPS
80};
81
82struct mousedev_motion {
83 int dx, dy, dz;
84 unsigned long buttons;
85};
86
87#define PACKET_QUEUE_LEN 16
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -040088struct mousedev_client {
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 struct fasync_struct *fasync;
90 struct mousedev *mousedev;
91 struct list_head node;
92
93 struct mousedev_motion packets[PACKET_QUEUE_LEN];
94 unsigned int head, tail;
95 spinlock_t packet_lock;
96 int pos_x, pos_y;
97
98 signed char ps2[6];
99 unsigned char ready, buffer, bufsiz;
100 unsigned char imexseq, impsseq;
101 enum mousedev_emul mode;
Pavel Machekc1e4c8d2005-05-27 12:53:03 -0700102 unsigned long last_buttons;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103};
104
105#define MOUSEDEV_SEQ_LEN 6
106
107static unsigned char mousedev_imps_seq[] = { 0xf3, 200, 0xf3, 100, 0xf3, 80 };
108static unsigned char mousedev_imex_seq[] = { 0xf3, 200, 0xf3, 200, 0xf3, 80 };
109
110static struct input_handler mousedev_handler;
111
112static struct mousedev *mousedev_table[MOUSEDEV_MINORS];
113static struct mousedev mousedev_mix;
114
115#define fx(i) (mousedev->old_x[(mousedev->pkt_count - (i)) & 03])
116#define fy(i) (mousedev->old_y[(mousedev->pkt_count - (i)) & 03])
117
118static void mousedev_touchpad_event(struct input_dev *dev, struct mousedev *mousedev, unsigned int code, int value)
119{
120 int size, tmp;
121 enum { FRACTION_DENOM = 128 };
122
123 if (mousedev->touch) {
124 size = dev->absmax[ABS_X] - dev->absmin[ABS_X];
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400125 if (size == 0)
126 size = 256 * 2;
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 switch (code) {
129 case ABS_X:
130 fx(0) = value;
131 if (mousedev->pkt_count >= 2) {
132 tmp = ((value - fx(2)) * (256 * FRACTION_DENOM)) / size;
133 tmp += mousedev->frac_dx;
134 mousedev->packet.dx = tmp / FRACTION_DENOM;
135 mousedev->frac_dx = tmp - mousedev->packet.dx * FRACTION_DENOM;
136 }
137 break;
138
139 case ABS_Y:
140 fy(0) = value;
141 if (mousedev->pkt_count >= 2) {
142 tmp = -((value - fy(2)) * (256 * FRACTION_DENOM)) / size;
143 tmp += mousedev->frac_dy;
144 mousedev->packet.dy = tmp / FRACTION_DENOM;
145 mousedev->frac_dy = tmp - mousedev->packet.dy * FRACTION_DENOM;
146 }
147 break;
148 }
149 }
150}
151
152static void mousedev_abs_event(struct input_dev *dev, struct mousedev *mousedev, unsigned int code, int value)
153{
154 int size;
155
156 switch (code) {
157 case ABS_X:
158 size = dev->absmax[ABS_X] - dev->absmin[ABS_X];
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400159 if (size == 0)
160 size = xres ? : 1;
161 if (value > dev->absmax[ABS_X])
162 value = dev->absmax[ABS_X];
163 if (value < dev->absmin[ABS_X])
164 value = dev->absmin[ABS_X];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 mousedev->packet.x = ((value - dev->absmin[ABS_X]) * xres) / size;
166 mousedev->packet.abs_event = 1;
167 break;
168
169 case ABS_Y:
170 size = dev->absmax[ABS_Y] - dev->absmin[ABS_Y];
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400171 if (size == 0)
172 size = yres ? : 1;
173 if (value > dev->absmax[ABS_Y])
174 value = dev->absmax[ABS_Y];
175 if (value < dev->absmin[ABS_Y])
176 value = dev->absmin[ABS_Y];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 mousedev->packet.y = yres - ((value - dev->absmin[ABS_Y]) * yres) / size;
178 mousedev->packet.abs_event = 1;
179 break;
180 }
181}
182
183static void mousedev_rel_event(struct mousedev *mousedev, unsigned int code, int value)
184{
185 switch (code) {
186 case REL_X: mousedev->packet.dx += value; break;
187 case REL_Y: mousedev->packet.dy -= value; break;
188 case REL_WHEEL: mousedev->packet.dz -= value; break;
189 }
190}
191
192static void mousedev_key_event(struct mousedev *mousedev, unsigned int code, int value)
193{
194 int index;
195
196 switch (code) {
197 case BTN_TOUCH:
198 case BTN_0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 case BTN_LEFT: index = 0; break;
200 case BTN_STYLUS:
201 case BTN_1:
202 case BTN_RIGHT: index = 1; break;
203 case BTN_2:
Marton Nemeth6c595fb2006-11-17 01:06:54 -0500204 case BTN_FORWARD:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 case BTN_STYLUS2:
206 case BTN_MIDDLE: index = 2; break;
207 case BTN_3:
208 case BTN_BACK:
209 case BTN_SIDE: index = 3; break;
210 case BTN_4:
211 case BTN_EXTRA: index = 4; break;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400212 default: return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 }
214
215 if (value) {
216 set_bit(index, &mousedev->packet.buttons);
217 set_bit(index, &mousedev_mix.packet.buttons);
218 } else {
219 clear_bit(index, &mousedev->packet.buttons);
220 clear_bit(index, &mousedev_mix.packet.buttons);
221 }
222}
223
224static void mousedev_notify_readers(struct mousedev *mousedev, struct mousedev_hw_data *packet)
225{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400226 struct mousedev_client *client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 struct mousedev_motion *p;
228 unsigned long flags;
Dmitry Torokhov81211522005-06-01 02:39:36 -0500229 int wake_readers = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400231 list_for_each_entry(client, &mousedev->client_list, node) {
232 spin_lock_irqsave(&client->packet_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400234 p = &client->packets[client->head];
235 if (client->ready && p->buttons != mousedev->packet.buttons) {
236 unsigned int new_head = (client->head + 1) % PACKET_QUEUE_LEN;
237 if (new_head != client->tail) {
238 p = &client->packets[client->head = new_head];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 memset(p, 0, sizeof(struct mousedev_motion));
240 }
241 }
242
243 if (packet->abs_event) {
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400244 p->dx += packet->x - client->pos_x;
245 p->dy += packet->y - client->pos_y;
246 client->pos_x = packet->x;
247 client->pos_y = packet->y;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 }
249
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400250 client->pos_x += packet->dx;
251 client->pos_x = client->pos_x < 0 ? 0 : (client->pos_x >= xres ? xres : client->pos_x);
252 client->pos_y += packet->dy;
253 client->pos_y = client->pos_y < 0 ? 0 : (client->pos_y >= yres ? yres : client->pos_y);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 p->dx += packet->dx;
256 p->dy += packet->dy;
257 p->dz += packet->dz;
258 p->buttons = mousedev->packet.buttons;
259
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400260 if (p->dx || p->dy || p->dz || p->buttons != client->last_buttons)
261 client->ready = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400263 spin_unlock_irqrestore(&client->packet_lock, flags);
Pavel Machekc1e4c8d2005-05-27 12:53:03 -0700264
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400265 if (client->ready) {
266 kill_fasync(&client->fasync, SIGIO, POLL_IN);
Dmitry Torokhov81211522005-06-01 02:39:36 -0500267 wake_readers = 1;
268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 }
270
Dmitry Torokhov81211522005-06-01 02:39:36 -0500271 if (wake_readers)
272 wake_up_interruptible(&mousedev->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
274
275static void mousedev_touchpad_touch(struct mousedev *mousedev, int value)
276{
277 if (!value) {
278 if (mousedev->touch &&
279 time_before(jiffies, mousedev->touch + msecs_to_jiffies(tap_time))) {
280 /*
281 * Toggle left button to emulate tap.
282 * We rely on the fact that mousedev_mix always has 0
283 * motion packet so we won't mess current position.
284 */
285 set_bit(0, &mousedev->packet.buttons);
286 set_bit(0, &mousedev_mix.packet.buttons);
287 mousedev_notify_readers(mousedev, &mousedev_mix.packet);
288 mousedev_notify_readers(&mousedev_mix, &mousedev_mix.packet);
289 clear_bit(0, &mousedev->packet.buttons);
290 clear_bit(0, &mousedev_mix.packet.buttons);
291 }
292 mousedev->touch = mousedev->pkt_count = 0;
293 mousedev->frac_dx = 0;
294 mousedev->frac_dy = 0;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400295
296 } else if (!mousedev->touch)
297 mousedev->touch = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
300static void mousedev_event(struct input_handle *handle, unsigned int type, unsigned int code, int value)
301{
302 struct mousedev *mousedev = handle->private;
303
304 switch (type) {
305 case EV_ABS:
306 /* Ignore joysticks */
307 if (test_bit(BTN_TRIGGER, handle->dev->keybit))
308 return;
309
310 if (test_bit(BTN_TOOL_FINGER, handle->dev->keybit))
311 mousedev_touchpad_event(handle->dev, mousedev, code, value);
312 else
313 mousedev_abs_event(handle->dev, mousedev, code, value);
314
315 break;
316
317 case EV_REL:
318 mousedev_rel_event(mousedev, code, value);
319 break;
320
321 case EV_KEY:
322 if (value != 2) {
323 if (code == BTN_TOUCH && test_bit(BTN_TOOL_FINGER, handle->dev->keybit))
324 mousedev_touchpad_touch(mousedev, value);
325 else
326 mousedev_key_event(mousedev, code, value);
327 }
328 break;
329
330 case EV_SYN:
331 if (code == SYN_REPORT) {
332 if (mousedev->touch) {
333 mousedev->pkt_count++;
334 /* Input system eats duplicate events, but we need all of them
335 * to do correct averaging so apply present one forward
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400336 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 fx(0) = fx(1);
338 fy(0) = fy(1);
339 }
340
341 mousedev_notify_readers(mousedev, &mousedev->packet);
342 mousedev_notify_readers(&mousedev_mix, &mousedev->packet);
343
344 mousedev->packet.dx = mousedev->packet.dy = mousedev->packet.dz = 0;
345 mousedev->packet.abs_event = 0;
346 }
347 break;
348 }
349}
350
351static int mousedev_fasync(int fd, struct file *file, int on)
352{
353 int retval;
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400354 struct mousedev_client *client = file->private_data;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400355
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400356 retval = fasync_helper(fd, file, on, &client->fasync);
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 return retval < 0 ? retval : 0;
359}
360
361static void mousedev_free(struct mousedev *mousedev)
362{
363 mousedev_table[mousedev->minor] = NULL;
364 kfree(mousedev);
365}
366
Kimball Murray74570d42006-01-29 21:50:59 -0500367static void mixdev_release(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
369 struct input_handle *handle;
370
371 list_for_each_entry(handle, &mousedev_handler.h_list, h_node) {
372 struct mousedev *mousedev = handle->private;
373
374 if (!mousedev->open) {
375 if (mousedev->exist)
376 input_close_device(&mousedev->handle);
377 else
378 mousedev_free(mousedev);
379 }
380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381}
382
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400383static int mousedev_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400385 struct mousedev_client *client = file->private_data;
386 struct mousedev *mousedev = client->mousedev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 mousedev_fasync(-1, file, 0);
389
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400390 list_del(&client->node);
391 kfree(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400393 if (!--mousedev->open) {
394 if (mousedev->minor == MOUSEDEV_MIX)
Kimball Murray74570d42006-01-29 21:50:59 -0500395 mixdev_release();
396 else if (!mousedev_mix.open) {
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400397 if (mousedev->exist)
398 input_close_device(&mousedev->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 else
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400400 mousedev_free(mousedev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 }
402 }
403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 return 0;
405}
406
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400407static int mousedev_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400409 struct mousedev_client *client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 struct input_handle *handle;
411 struct mousedev *mousedev;
412 int i;
413
414#ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
415 if (imajor(inode) == MISC_MAJOR)
416 i = MOUSEDEV_MIX;
417 else
418#endif
419 i = iminor(inode) - MOUSEDEV_MINOR_BASE;
420
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400421 if (i >= MOUSEDEV_MINORS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 return -ENODEV;
423
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400424 mousedev = mousedev_table[i];
425 if (!mousedev)
426 return -ENODEV;
427
428 client = kzalloc(sizeof(struct mousedev_client), GFP_KERNEL);
429 if (!client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400432 spin_lock_init(&client->packet_lock);
433 client->pos_x = xres / 2;
434 client->pos_y = yres / 2;
435 client->mousedev = mousedev;
436 list_add_tail(&client->node, &mousedev->client_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400438 if (!mousedev->open++) {
439 if (mousedev->minor == MOUSEDEV_MIX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 list_for_each_entry(handle, &mousedev_handler.h_list, h_node) {
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400441 struct mousedev *md = handle->private;
442 if (!md->open && md->exist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 input_open_device(handle);
444 }
445 } else
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400446 if (!mousedev_mix.open && mousedev->exist)
447 input_open_device(&mousedev->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 }
449
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400450 file->private_data = client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 return 0;
452}
453
454static inline int mousedev_limit_delta(int delta, int limit)
455{
456 return delta > limit ? limit : (delta < -limit ? -limit : delta);
457}
458
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400459static void mousedev_packet(struct mousedev_client *client, signed char *ps2_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
461 struct mousedev_motion *p;
462 unsigned long flags;
463
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400464 spin_lock_irqsave(&client->packet_lock, flags);
465 p = &client->packets[client->tail];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 ps2_data[0] = 0x08 | ((p->dx < 0) << 4) | ((p->dy < 0) << 5) | (p->buttons & 0x07);
468 ps2_data[1] = mousedev_limit_delta(p->dx, 127);
469 ps2_data[2] = mousedev_limit_delta(p->dy, 127);
470 p->dx -= ps2_data[1];
471 p->dy -= ps2_data[2];
472
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400473 switch (client->mode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 case MOUSEDEV_EMUL_EXPS:
475 ps2_data[3] = mousedev_limit_delta(p->dz, 7);
476 p->dz -= ps2_data[3];
477 ps2_data[3] = (ps2_data[3] & 0x0f) | ((p->buttons & 0x18) << 1);
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400478 client->bufsiz = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 break;
480
481 case MOUSEDEV_EMUL_IMPS:
482 ps2_data[0] |= ((p->buttons & 0x10) >> 3) | ((p->buttons & 0x08) >> 1);
483 ps2_data[3] = mousedev_limit_delta(p->dz, 127);
484 p->dz -= ps2_data[3];
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400485 client->bufsiz = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 break;
487
488 case MOUSEDEV_EMUL_PS2:
489 default:
490 ps2_data[0] |= ((p->buttons & 0x10) >> 3) | ((p->buttons & 0x08) >> 1);
491 p->dz = 0;
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400492 client->bufsiz = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 break;
494 }
495
496 if (!p->dx && !p->dy && !p->dz) {
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400497 if (client->tail == client->head) {
498 client->ready = 0;
499 client->last_buttons = p->buttons;
Pavel Machekc1e4c8d2005-05-27 12:53:03 -0700500 } else
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400501 client->tail = (client->tail + 1) % PACKET_QUEUE_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 }
503
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400504 spin_unlock_irqrestore(&client->packet_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
506
507
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400508static ssize_t mousedev_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400510 struct mousedev_client *client = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 unsigned char c;
512 unsigned int i;
513
514 for (i = 0; i < count; i++) {
515
516 if (get_user(c, buffer + i))
517 return -EFAULT;
518
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400519 if (c == mousedev_imex_seq[client->imexseq]) {
520 if (++client->imexseq == MOUSEDEV_SEQ_LEN) {
521 client->imexseq = 0;
522 client->mode = MOUSEDEV_EMUL_EXPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 }
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400524 } else
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400525 client->imexseq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400527 if (c == mousedev_imps_seq[client->impsseq]) {
528 if (++client->impsseq == MOUSEDEV_SEQ_LEN) {
529 client->impsseq = 0;
530 client->mode = MOUSEDEV_EMUL_IMPS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 }
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400532 } else
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400533 client->impsseq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400535 client->ps2[0] = 0xfa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 switch (c) {
538
539 case 0xeb: /* Poll */
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400540 mousedev_packet(client, &client->ps2[1]);
541 client->bufsiz++; /* account for leading ACK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 break;
543
544 case 0xf2: /* Get ID */
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400545 switch (client->mode) {
546 case MOUSEDEV_EMUL_PS2: client->ps2[1] = 0; break;
547 case MOUSEDEV_EMUL_IMPS: client->ps2[1] = 3; break;
548 case MOUSEDEV_EMUL_EXPS: client->ps2[1] = 4; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 }
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400550 client->bufsiz = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 break;
552
553 case 0xe9: /* Get info */
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400554 client->ps2[1] = 0x60; client->ps2[2] = 3; client->ps2[3] = 200;
555 client->bufsiz = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 break;
557
558 case 0xff: /* Reset */
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400559 client->impsseq = client->imexseq = 0;
560 client->mode = MOUSEDEV_EMUL_PS2;
561 client->ps2[1] = 0xaa; client->ps2[2] = 0x00;
562 client->bufsiz = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 break;
564
565 default:
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400566 client->bufsiz = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 break;
568 }
569
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400570 client->buffer = client->bufsiz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 }
572
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400573 kill_fasync(&client->fasync, SIGIO, POLL_IN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400575 wake_up_interruptible(&client->mousedev->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 return count;
578}
579
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400580static ssize_t mousedev_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400582 struct mousedev_client *client = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 int retval = 0;
584
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400585 if (!client->ready && !client->buffer && (file->f_flags & O_NONBLOCK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 return -EAGAIN;
587
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400588 retval = wait_event_interruptible(client->mousedev->wait,
589 !client->mousedev->exist || client->ready || client->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
591 if (retval)
592 return retval;
593
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400594 if (!client->mousedev->exist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 return -ENODEV;
596
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400597 if (!client->buffer && client->ready) {
598 mousedev_packet(client, client->ps2);
599 client->buffer = client->bufsiz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 }
601
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400602 if (count > client->buffer)
603 count = client->buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400605 client->buffer -= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400607 if (copy_to_user(buffer, client->ps2 + client->bufsiz - client->buffer - count, count))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 return -EFAULT;
609
610 return count;
611}
612
613/* No kernel lock - fine */
614static unsigned int mousedev_poll(struct file *file, poll_table *wait)
615{
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400616 struct mousedev_client *client = file->private_data;
617 struct mousedev *mousedev = client->mousedev;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400618
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400619 poll_wait(file, &mousedev->wait, wait);
620 return ((client->ready || client->buffer) ? (POLLIN | POLLRDNORM) : 0) |
621 (mousedev->exist ? 0 : (POLLHUP | POLLERR));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622}
623
Dmitry Torokhov66e66112006-09-14 01:31:59 -0400624static const struct file_operations mousedev_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 .owner = THIS_MODULE,
626 .read = mousedev_read,
627 .write = mousedev_write,
628 .poll = mousedev_poll,
629 .open = mousedev_open,
630 .release = mousedev_release,
631 .fasync = mousedev_fasync,
632};
633
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400634static int mousedev_connect(struct input_handler *handler, struct input_dev *dev,
635 const struct input_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
637 struct mousedev *mousedev;
Greg Kroah-Hartmanc9bcd582005-10-27 22:25:43 -0700638 struct class_device *cdev;
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400639 dev_t devt;
640 int minor;
641 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643 for (minor = 0; minor < MOUSEDEV_MINORS && mousedev_table[minor]; minor++);
644 if (minor == MOUSEDEV_MINORS) {
645 printk(KERN_ERR "mousedev: no more free mousedev devices\n");
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400646 return -ENFILE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
648
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400649 mousedev = kzalloc(sizeof(struct mousedev), GFP_KERNEL);
650 if (!mousedev)
651 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400653 INIT_LIST_HEAD(&mousedev->client_list);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400654 INIT_LIST_HEAD(&mousedev->mixdev_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 init_waitqueue_head(&mousedev->wait);
656
657 mousedev->minor = minor;
658 mousedev->exist = 1;
659 mousedev->handle.dev = dev;
660 mousedev->handle.name = mousedev->name;
661 mousedev->handle.handler = handler;
662 mousedev->handle.private = mousedev;
663 sprintf(mousedev->name, "mouse%d", minor);
664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 mousedev_table[minor] = mousedev;
666
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400667 devt = MKDEV(INPUT_MAJOR, MOUSEDEV_MINOR_BASE + minor),
668
669 cdev = class_device_create(&input_class, &dev->cdev, devt,
670 dev->cdev.dev, mousedev->name);
671 if (IS_ERR(cdev)) {
672 error = PTR_ERR(cdev);
673 goto err_free_mousedev;
674 }
Greg Kroah-Hartmanc9bcd582005-10-27 22:25:43 -0700675
676 /* temporary symlink to keep userspace happy */
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400677 error = sysfs_create_link(&input_class.subsys.kset.kobj,
678 &cdev->kobj, mousedev->name);
679 if (error)
680 goto err_cdev_destroy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400682 error = input_register_handle(&mousedev->handle);
683 if (error)
684 goto err_remove_link;
685
686 if (mousedev_mix.open) {
687 error = input_open_device(&mousedev->handle);
688 if (error)
689 goto err_unregister_handle;
690 }
691
692 return 0;
693
694 err_unregister_handle:
695 input_unregister_handle(&mousedev->handle);
696 err_remove_link:
697 sysfs_remove_link(&input_class.subsys.kset.kobj, mousedev->name);
698 err_cdev_destroy:
699 class_device_destroy(&input_class, devt);
700 err_free_mousedev:
701 mousedev_table[minor] = NULL;
702 kfree(mousedev);
703 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704}
705
706static void mousedev_disconnect(struct input_handle *handle)
707{
708 struct mousedev *mousedev = handle->private;
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400709 struct mousedev_client *client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400711 input_unregister_handle(handle);
712
Greg Kroah-Hartmanc9bcd582005-10-27 22:25:43 -0700713 sysfs_remove_link(&input_class.subsys.kset.kobj, mousedev->name);
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -0700714 class_device_destroy(&input_class,
gregkh@suse.de12356862005-03-15 14:26:30 -0800715 MKDEV(INPUT_MAJOR, MOUSEDEV_MINOR_BASE + mousedev->minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 mousedev->exist = 0;
717
718 if (mousedev->open) {
719 input_close_device(handle);
720 wake_up_interruptible(&mousedev->wait);
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400721 list_for_each_entry(client, &mousedev->client_list, node)
722 kill_fasync(&client->fasync, SIGIO, POLL_HUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 } else {
724 if (mousedev_mix.open)
725 input_close_device(handle);
726 mousedev_free(mousedev);
727 }
728}
729
Dmitry Torokhov66e66112006-09-14 01:31:59 -0400730static const struct input_device_id mousedev_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 {
732 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_RELBIT,
733 .evbit = { BIT(EV_KEY) | BIT(EV_REL) },
734 .keybit = { [LONG(BTN_LEFT)] = BIT(BTN_LEFT) },
735 .relbit = { BIT(REL_X) | BIT(REL_Y) },
736 }, /* A mouse like device, at least one button, two relative axes */
737 {
738 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_RELBIT,
739 .evbit = { BIT(EV_KEY) | BIT(EV_REL) },
740 .relbit = { BIT(REL_WHEEL) },
741 }, /* A separate scrollwheel */
742 {
743 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_ABSBIT,
744 .evbit = { BIT(EV_KEY) | BIT(EV_ABS) },
745 .keybit = { [LONG(BTN_TOUCH)] = BIT(BTN_TOUCH) },
746 .absbit = { BIT(ABS_X) | BIT(ABS_Y) },
747 }, /* A tablet like device, at least touch detection, two absolute axes */
748 {
749 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_ABSBIT,
750 .evbit = { BIT(EV_KEY) | BIT(EV_ABS) },
751 .keybit = { [LONG(BTN_TOOL_FINGER)] = BIT(BTN_TOOL_FINGER) },
752 .absbit = { BIT(ABS_X) | BIT(ABS_Y) | BIT(ABS_PRESSURE) | BIT(ABS_TOOL_WIDTH) },
753 }, /* A touchpad */
754
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400755 { }, /* Terminating entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756};
757
758MODULE_DEVICE_TABLE(input, mousedev_ids);
759
760static struct input_handler mousedev_handler = {
761 .event = mousedev_event,
762 .connect = mousedev_connect,
763 .disconnect = mousedev_disconnect,
764 .fops = &mousedev_fops,
765 .minor = MOUSEDEV_MINOR_BASE,
766 .name = "mousedev",
767 .id_table = mousedev_ids,
768};
769
770#ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
771static struct miscdevice psaux_mouse = {
772 PSMOUSE_MINOR, "psaux", &mousedev_fops
773};
774static int psaux_registered;
775#endif
776
777static int __init mousedev_init(void)
778{
Dmitry Torokhov4263cf02006-09-14 01:32:39 -0400779 struct class_device *cdev;
780 int error;
781
782 error = input_register_handler(&mousedev_handler);
783 if (error)
784 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
786 memset(&mousedev_mix, 0, sizeof(struct mousedev));
Dmitry Torokhovd0ffb9b2007-04-12 01:30:00 -0400787 INIT_LIST_HEAD(&mousedev_mix.client_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 init_waitqueue_head(&mousedev_mix.wait);
789 mousedev_table[MOUSEDEV_MIX] = &mousedev_mix;
790 mousedev_mix.exist = 1;
791 mousedev_mix.minor = MOUSEDEV_MIX;
792
Dmitry Torokhov4263cf02006-09-14 01:32:39 -0400793 cdev = class_device_create(&input_class, NULL,
gregkh@suse.de12356862005-03-15 14:26:30 -0800794 MKDEV(INPUT_MAJOR, MOUSEDEV_MINOR_BASE + MOUSEDEV_MIX), NULL, "mice");
Dmitry Torokhov4263cf02006-09-14 01:32:39 -0400795 if (IS_ERR(cdev)) {
796 input_unregister_handler(&mousedev_handler);
797 return PTR_ERR(cdev);
798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
800#ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
Dmitry Torokhov4263cf02006-09-14 01:32:39 -0400801 error = misc_register(&psaux_mouse);
802 if (error)
803 printk(KERN_WARNING "mice: could not register psaux device, "
804 "error: %d\n", error);
805 else
806 psaux_registered = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807#endif
808
809 printk(KERN_INFO "mice: PS/2 mouse device common for all mice\n");
810
811 return 0;
812}
813
814static void __exit mousedev_exit(void)
815{
816#ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
817 if (psaux_registered)
818 misc_deregister(&psaux_mouse);
819#endif
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -0700820 class_device_destroy(&input_class,
gregkh@suse.de12356862005-03-15 14:26:30 -0800821 MKDEV(INPUT_MAJOR, MOUSEDEV_MINOR_BASE + MOUSEDEV_MIX));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 input_unregister_handler(&mousedev_handler);
823}
824
825module_init(mousedev_init);
826module_exit(mousedev_exit);