blob: b685a507955d2c81aa5d6ad239d92d6a04c228a0 [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>
22#include <linux/config.h>
23#include <linux/smp_lock.h>
24#include <linux/random.h>
25#include <linux/major.h>
26#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
28#include <linux/miscdevice.h>
29#endif
30
31MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
32MODULE_DESCRIPTION("Mouse (ExplorerPS/2) device interfaces");
33MODULE_LICENSE("GPL");
34
35#ifndef CONFIG_INPUT_MOUSEDEV_SCREEN_X
36#define CONFIG_INPUT_MOUSEDEV_SCREEN_X 1024
37#endif
38#ifndef CONFIG_INPUT_MOUSEDEV_SCREEN_Y
39#define CONFIG_INPUT_MOUSEDEV_SCREEN_Y 768
40#endif
41
42static int xres = CONFIG_INPUT_MOUSEDEV_SCREEN_X;
Dmitry Torokhov84c12b22005-12-11 12:41:03 -050043module_param(xres, uint, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044MODULE_PARM_DESC(xres, "Horizontal screen resolution");
45
46static int yres = CONFIG_INPUT_MOUSEDEV_SCREEN_Y;
Dmitry Torokhov84c12b22005-12-11 12:41:03 -050047module_param(yres, uint, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048MODULE_PARM_DESC(yres, "Vertical screen resolution");
49
50static unsigned tap_time = 200;
Dmitry Torokhov84c12b22005-12-11 12:41:03 -050051module_param(tap_time, uint, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052MODULE_PARM_DESC(tap_time, "Tap time for touchpads in absolute mode (msecs)");
53
54struct mousedev_hw_data {
55 int dx, dy, dz;
56 int x, y;
57 int abs_event;
58 unsigned long buttons;
59};
60
61struct mousedev {
62 int exist;
63 int open;
64 int minor;
65 char name[16];
66 wait_queue_head_t wait;
67 struct list_head list;
68 struct input_handle handle;
69
70 struct mousedev_hw_data packet;
71 unsigned int pkt_count;
72 int old_x[4], old_y[4];
73 int frac_dx, frac_dy;
74 unsigned long touch;
75};
76
77enum mousedev_emul {
78 MOUSEDEV_EMUL_PS2,
79 MOUSEDEV_EMUL_IMPS,
80 MOUSEDEV_EMUL_EXPS
81};
82
83struct mousedev_motion {
84 int dx, dy, dz;
85 unsigned long buttons;
86};
87
88#define PACKET_QUEUE_LEN 16
89struct mousedev_list {
90 struct fasync_struct *fasync;
91 struct mousedev *mousedev;
92 struct list_head node;
93
94 struct mousedev_motion packets[PACKET_QUEUE_LEN];
95 unsigned int head, tail;
96 spinlock_t packet_lock;
97 int pos_x, pos_y;
98
99 signed char ps2[6];
100 unsigned char ready, buffer, bufsiz;
101 unsigned char imexseq, impsseq;
102 enum mousedev_emul mode;
Pavel Machekc1e4c8d2005-05-27 12:53:03 -0700103 unsigned long last_buttons;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104};
105
106#define MOUSEDEV_SEQ_LEN 6
107
108static unsigned char mousedev_imps_seq[] = { 0xf3, 200, 0xf3, 100, 0xf3, 80 };
109static unsigned char mousedev_imex_seq[] = { 0xf3, 200, 0xf3, 200, 0xf3, 80 };
110
111static struct input_handler mousedev_handler;
112
113static struct mousedev *mousedev_table[MOUSEDEV_MINORS];
114static struct mousedev mousedev_mix;
115
116#define fx(i) (mousedev->old_x[(mousedev->pkt_count - (i)) & 03])
117#define fy(i) (mousedev->old_y[(mousedev->pkt_count - (i)) & 03])
118
119static void mousedev_touchpad_event(struct input_dev *dev, struct mousedev *mousedev, unsigned int code, int value)
120{
121 int size, tmp;
122 enum { FRACTION_DENOM = 128 };
123
124 if (mousedev->touch) {
125 size = dev->absmax[ABS_X] - dev->absmin[ABS_X];
126 if (size == 0) size = 256 * 2;
127 switch (code) {
128 case ABS_X:
129 fx(0) = value;
130 if (mousedev->pkt_count >= 2) {
131 tmp = ((value - fx(2)) * (256 * FRACTION_DENOM)) / size;
132 tmp += mousedev->frac_dx;
133 mousedev->packet.dx = tmp / FRACTION_DENOM;
134 mousedev->frac_dx = tmp - mousedev->packet.dx * FRACTION_DENOM;
135 }
136 break;
137
138 case ABS_Y:
139 fy(0) = value;
140 if (mousedev->pkt_count >= 2) {
141 tmp = -((value - fy(2)) * (256 * FRACTION_DENOM)) / size;
142 tmp += mousedev->frac_dy;
143 mousedev->packet.dy = tmp / FRACTION_DENOM;
144 mousedev->frac_dy = tmp - mousedev->packet.dy * FRACTION_DENOM;
145 }
146 break;
147 }
148 }
149}
150
151static void mousedev_abs_event(struct input_dev *dev, struct mousedev *mousedev, unsigned int code, int value)
152{
153 int size;
154
155 switch (code) {
156 case ABS_X:
157 size = dev->absmax[ABS_X] - dev->absmin[ABS_X];
Dmitry Torokhov84c12b22005-12-11 12:41:03 -0500158 if (size == 0) size = xres ? : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 if (value > dev->absmax[ABS_X]) value = dev->absmax[ABS_X];
160 if (value < dev->absmin[ABS_X]) value = dev->absmin[ABS_X];
161 mousedev->packet.x = ((value - dev->absmin[ABS_X]) * xres) / size;
162 mousedev->packet.abs_event = 1;
163 break;
164
165 case ABS_Y:
166 size = dev->absmax[ABS_Y] - dev->absmin[ABS_Y];
Dmitry Torokhov84c12b22005-12-11 12:41:03 -0500167 if (size == 0) size = yres ? : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 if (value > dev->absmax[ABS_Y]) value = dev->absmax[ABS_Y];
169 if (value < dev->absmin[ABS_Y]) value = dev->absmin[ABS_Y];
170 mousedev->packet.y = yres - ((value - dev->absmin[ABS_Y]) * yres) / size;
171 mousedev->packet.abs_event = 1;
172 break;
173 }
174}
175
176static void mousedev_rel_event(struct mousedev *mousedev, unsigned int code, int value)
177{
178 switch (code) {
179 case REL_X: mousedev->packet.dx += value; break;
180 case REL_Y: mousedev->packet.dy -= value; break;
181 case REL_WHEEL: mousedev->packet.dz -= value; break;
182 }
183}
184
185static void mousedev_key_event(struct mousedev *mousedev, unsigned int code, int value)
186{
187 int index;
188
189 switch (code) {
190 case BTN_TOUCH:
191 case BTN_0:
192 case BTN_FORWARD:
193 case BTN_LEFT: index = 0; break;
194 case BTN_STYLUS:
195 case BTN_1:
196 case BTN_RIGHT: index = 1; break;
197 case BTN_2:
198 case BTN_STYLUS2:
199 case BTN_MIDDLE: index = 2; break;
200 case BTN_3:
201 case BTN_BACK:
202 case BTN_SIDE: index = 3; break;
203 case BTN_4:
204 case BTN_EXTRA: index = 4; break;
205 default: return;
206 }
207
208 if (value) {
209 set_bit(index, &mousedev->packet.buttons);
210 set_bit(index, &mousedev_mix.packet.buttons);
211 } else {
212 clear_bit(index, &mousedev->packet.buttons);
213 clear_bit(index, &mousedev_mix.packet.buttons);
214 }
215}
216
217static void mousedev_notify_readers(struct mousedev *mousedev, struct mousedev_hw_data *packet)
218{
219 struct mousedev_list *list;
220 struct mousedev_motion *p;
221 unsigned long flags;
Dmitry Torokhov81211522005-06-01 02:39:36 -0500222 int wake_readers = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 list_for_each_entry(list, &mousedev->list, node) {
225 spin_lock_irqsave(&list->packet_lock, flags);
226
227 p = &list->packets[list->head];
Pavel Machekc1e4c8d2005-05-27 12:53:03 -0700228 if (list->ready && p->buttons != mousedev->packet.buttons) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 unsigned int new_head = (list->head + 1) % PACKET_QUEUE_LEN;
230 if (new_head != list->tail) {
231 p = &list->packets[list->head = new_head];
232 memset(p, 0, sizeof(struct mousedev_motion));
233 }
234 }
235
236 if (packet->abs_event) {
237 p->dx += packet->x - list->pos_x;
238 p->dy += packet->y - list->pos_y;
239 list->pos_x = packet->x;
240 list->pos_y = packet->y;
241 }
242
243 list->pos_x += packet->dx;
244 list->pos_x = list->pos_x < 0 ? 0 : (list->pos_x >= xres ? xres : list->pos_x);
245 list->pos_y += packet->dy;
246 list->pos_y = list->pos_y < 0 ? 0 : (list->pos_y >= yres ? yres : list->pos_y);
247
248 p->dx += packet->dx;
249 p->dy += packet->dy;
250 p->dz += packet->dz;
251 p->buttons = mousedev->packet.buttons;
252
Pavel Machekc1e4c8d2005-05-27 12:53:03 -0700253 if (p->dx || p->dy || p->dz || p->buttons != list->last_buttons)
254 list->ready = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 spin_unlock_irqrestore(&list->packet_lock, flags);
Pavel Machekc1e4c8d2005-05-27 12:53:03 -0700257
Dmitry Torokhov81211522005-06-01 02:39:36 -0500258 if (list->ready) {
Pavel Machekc1e4c8d2005-05-27 12:53:03 -0700259 kill_fasync(&list->fasync, SIGIO, POLL_IN);
Dmitry Torokhov81211522005-06-01 02:39:36 -0500260 wake_readers = 1;
261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 }
263
Dmitry Torokhov81211522005-06-01 02:39:36 -0500264 if (wake_readers)
265 wake_up_interruptible(&mousedev->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
267
268static void mousedev_touchpad_touch(struct mousedev *mousedev, int value)
269{
270 if (!value) {
271 if (mousedev->touch &&
272 time_before(jiffies, mousedev->touch + msecs_to_jiffies(tap_time))) {
273 /*
274 * Toggle left button to emulate tap.
275 * We rely on the fact that mousedev_mix always has 0
276 * motion packet so we won't mess current position.
277 */
278 set_bit(0, &mousedev->packet.buttons);
279 set_bit(0, &mousedev_mix.packet.buttons);
280 mousedev_notify_readers(mousedev, &mousedev_mix.packet);
281 mousedev_notify_readers(&mousedev_mix, &mousedev_mix.packet);
282 clear_bit(0, &mousedev->packet.buttons);
283 clear_bit(0, &mousedev_mix.packet.buttons);
284 }
285 mousedev->touch = mousedev->pkt_count = 0;
286 mousedev->frac_dx = 0;
287 mousedev->frac_dy = 0;
288 }
289 else
290 if (!mousedev->touch)
291 mousedev->touch = jiffies;
292}
293
294static void mousedev_event(struct input_handle *handle, unsigned int type, unsigned int code, int value)
295{
296 struct mousedev *mousedev = handle->private;
297
298 switch (type) {
299 case EV_ABS:
300 /* Ignore joysticks */
301 if (test_bit(BTN_TRIGGER, handle->dev->keybit))
302 return;
303
304 if (test_bit(BTN_TOOL_FINGER, handle->dev->keybit))
305 mousedev_touchpad_event(handle->dev, mousedev, code, value);
306 else
307 mousedev_abs_event(handle->dev, mousedev, code, value);
308
309 break;
310
311 case EV_REL:
312 mousedev_rel_event(mousedev, code, value);
313 break;
314
315 case EV_KEY:
316 if (value != 2) {
317 if (code == BTN_TOUCH && test_bit(BTN_TOOL_FINGER, handle->dev->keybit))
318 mousedev_touchpad_touch(mousedev, value);
319 else
320 mousedev_key_event(mousedev, code, value);
321 }
322 break;
323
324 case EV_SYN:
325 if (code == SYN_REPORT) {
326 if (mousedev->touch) {
327 mousedev->pkt_count++;
328 /* Input system eats duplicate events, but we need all of them
329 * to do correct averaging so apply present one forward
330 */
331 fx(0) = fx(1);
332 fy(0) = fy(1);
333 }
334
335 mousedev_notify_readers(mousedev, &mousedev->packet);
336 mousedev_notify_readers(&mousedev_mix, &mousedev->packet);
337
338 mousedev->packet.dx = mousedev->packet.dy = mousedev->packet.dz = 0;
339 mousedev->packet.abs_event = 0;
340 }
341 break;
342 }
343}
344
345static int mousedev_fasync(int fd, struct file *file, int on)
346{
347 int retval;
348 struct mousedev_list *list = file->private_data;
349 retval = fasync_helper(fd, file, on, &list->fasync);
350 return retval < 0 ? retval : 0;
351}
352
353static void mousedev_free(struct mousedev *mousedev)
354{
355 mousedev_table[mousedev->minor] = NULL;
356 kfree(mousedev);
357}
358
Kimball Murray74570d42006-01-29 21:50:59 -0500359static void mixdev_release(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
361 struct input_handle *handle;
362
363 list_for_each_entry(handle, &mousedev_handler.h_list, h_node) {
364 struct mousedev *mousedev = handle->private;
365
366 if (!mousedev->open) {
367 if (mousedev->exist)
368 input_close_device(&mousedev->handle);
369 else
370 mousedev_free(mousedev);
371 }
372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
374
375static int mousedev_release(struct inode * inode, struct file * file)
376{
377 struct mousedev_list *list = file->private_data;
378
379 mousedev_fasync(-1, file, 0);
380
381 list_del(&list->node);
382
383 if (!--list->mousedev->open) {
384 if (list->mousedev->minor == MOUSEDEV_MIX)
Kimball Murray74570d42006-01-29 21:50:59 -0500385 mixdev_release();
386 else if (!mousedev_mix.open) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 if (list->mousedev->exist)
388 input_close_device(&list->mousedev->handle);
389 else
390 mousedev_free(list->mousedev);
391 }
392 }
393
394 kfree(list);
395 return 0;
396}
397
398static int mousedev_open(struct inode * inode, struct file * file)
399{
400 struct mousedev_list *list;
401 struct input_handle *handle;
402 struct mousedev *mousedev;
403 int i;
404
405#ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
406 if (imajor(inode) == MISC_MAJOR)
407 i = MOUSEDEV_MIX;
408 else
409#endif
410 i = iminor(inode) - MOUSEDEV_MINOR_BASE;
411
412 if (i >= MOUSEDEV_MINORS || !mousedev_table[i])
413 return -ENODEV;
414
Eric Sesterhennb39787a2006-03-14 00:09:16 -0500415 if (!(list = kzalloc(sizeof(struct mousedev_list), GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418 spin_lock_init(&list->packet_lock);
419 list->pos_x = xres / 2;
420 list->pos_y = yres / 2;
421 list->mousedev = mousedev_table[i];
422 list_add_tail(&list->node, &mousedev_table[i]->list);
423 file->private_data = list;
424
425 if (!list->mousedev->open++) {
426 if (list->mousedev->minor == MOUSEDEV_MIX) {
427 list_for_each_entry(handle, &mousedev_handler.h_list, h_node) {
428 mousedev = handle->private;
429 if (!mousedev->open && mousedev->exist)
430 input_open_device(handle);
431 }
432 } else
433 if (!mousedev_mix.open && list->mousedev->exist)
434 input_open_device(&list->mousedev->handle);
435 }
436
437 return 0;
438}
439
440static inline int mousedev_limit_delta(int delta, int limit)
441{
442 return delta > limit ? limit : (delta < -limit ? -limit : delta);
443}
444
445static void mousedev_packet(struct mousedev_list *list, signed char *ps2_data)
446{
447 struct mousedev_motion *p;
448 unsigned long flags;
449
450 spin_lock_irqsave(&list->packet_lock, flags);
451 p = &list->packets[list->tail];
452
453 ps2_data[0] = 0x08 | ((p->dx < 0) << 4) | ((p->dy < 0) << 5) | (p->buttons & 0x07);
454 ps2_data[1] = mousedev_limit_delta(p->dx, 127);
455 ps2_data[2] = mousedev_limit_delta(p->dy, 127);
456 p->dx -= ps2_data[1];
457 p->dy -= ps2_data[2];
458
459 switch (list->mode) {
460 case MOUSEDEV_EMUL_EXPS:
461 ps2_data[3] = mousedev_limit_delta(p->dz, 7);
462 p->dz -= ps2_data[3];
463 ps2_data[3] = (ps2_data[3] & 0x0f) | ((p->buttons & 0x18) << 1);
464 list->bufsiz = 4;
465 break;
466
467 case MOUSEDEV_EMUL_IMPS:
468 ps2_data[0] |= ((p->buttons & 0x10) >> 3) | ((p->buttons & 0x08) >> 1);
469 ps2_data[3] = mousedev_limit_delta(p->dz, 127);
470 p->dz -= ps2_data[3];
471 list->bufsiz = 4;
472 break;
473
474 case MOUSEDEV_EMUL_PS2:
475 default:
476 ps2_data[0] |= ((p->buttons & 0x10) >> 3) | ((p->buttons & 0x08) >> 1);
477 p->dz = 0;
478 list->bufsiz = 3;
479 break;
480 }
481
482 if (!p->dx && !p->dy && !p->dz) {
Pavel Machekc1e4c8d2005-05-27 12:53:03 -0700483 if (list->tail == list->head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 list->ready = 0;
Pavel Machekc1e4c8d2005-05-27 12:53:03 -0700485 list->last_buttons = p->buttons;
486 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 list->tail = (list->tail + 1) % PACKET_QUEUE_LEN;
488 }
489
490 spin_unlock_irqrestore(&list->packet_lock, flags);
491}
492
493
494static ssize_t mousedev_write(struct file * file, const char __user * buffer, size_t count, loff_t *ppos)
495{
496 struct mousedev_list *list = file->private_data;
497 unsigned char c;
498 unsigned int i;
499
500 for (i = 0; i < count; i++) {
501
502 if (get_user(c, buffer + i))
503 return -EFAULT;
504
505 if (c == mousedev_imex_seq[list->imexseq]) {
506 if (++list->imexseq == MOUSEDEV_SEQ_LEN) {
507 list->imexseq = 0;
508 list->mode = MOUSEDEV_EMUL_EXPS;
509 }
510 } else list->imexseq = 0;
511
512 if (c == mousedev_imps_seq[list->impsseq]) {
513 if (++list->impsseq == MOUSEDEV_SEQ_LEN) {
514 list->impsseq = 0;
515 list->mode = MOUSEDEV_EMUL_IMPS;
516 }
517 } else list->impsseq = 0;
518
519 list->ps2[0] = 0xfa;
520
521 switch (c) {
522
523 case 0xeb: /* Poll */
524 mousedev_packet(list, &list->ps2[1]);
525 list->bufsiz++; /* account for leading ACK */
526 break;
527
528 case 0xf2: /* Get ID */
529 switch (list->mode) {
530 case MOUSEDEV_EMUL_PS2: list->ps2[1] = 0; break;
531 case MOUSEDEV_EMUL_IMPS: list->ps2[1] = 3; break;
532 case MOUSEDEV_EMUL_EXPS: list->ps2[1] = 4; break;
533 }
534 list->bufsiz = 2;
535 break;
536
537 case 0xe9: /* Get info */
538 list->ps2[1] = 0x60; list->ps2[2] = 3; list->ps2[3] = 200;
539 list->bufsiz = 4;
540 break;
541
542 case 0xff: /* Reset */
543 list->impsseq = list->imexseq = 0;
544 list->mode = MOUSEDEV_EMUL_PS2;
545 list->ps2[1] = 0xaa; list->ps2[2] = 0x00;
546 list->bufsiz = 3;
547 break;
548
549 default:
550 list->bufsiz = 1;
551 break;
552 }
553
554 list->buffer = list->bufsiz;
555 }
556
557 kill_fasync(&list->fasync, SIGIO, POLL_IN);
558
559 wake_up_interruptible(&list->mousedev->wait);
560
561 return count;
562}
563
564static ssize_t mousedev_read(struct file * file, char __user * buffer, size_t count, loff_t *ppos)
565{
566 struct mousedev_list *list = file->private_data;
567 int retval = 0;
568
569 if (!list->ready && !list->buffer && (file->f_flags & O_NONBLOCK))
570 return -EAGAIN;
571
572 retval = wait_event_interruptible(list->mousedev->wait,
573 !list->mousedev->exist || list->ready || list->buffer);
574
575 if (retval)
576 return retval;
577
578 if (!list->mousedev->exist)
579 return -ENODEV;
580
581 if (!list->buffer && list->ready) {
582 mousedev_packet(list, list->ps2);
583 list->buffer = list->bufsiz;
584 }
585
586 if (count > list->buffer)
587 count = list->buffer;
588
589 list->buffer -= count;
590
591 if (copy_to_user(buffer, list->ps2 + list->bufsiz - list->buffer - count, count))
592 return -EFAULT;
593
594 return count;
595}
596
597/* No kernel lock - fine */
598static unsigned int mousedev_poll(struct file *file, poll_table *wait)
599{
600 struct mousedev_list *list = file->private_data;
601 poll_wait(file, &list->mousedev->wait, wait);
602 return ((list->ready || list->buffer) ? (POLLIN | POLLRDNORM) : 0) |
603 (list->mousedev->exist ? 0 : (POLLHUP | POLLERR));
604}
605
606static struct file_operations mousedev_fops = {
607 .owner = THIS_MODULE,
608 .read = mousedev_read,
609 .write = mousedev_write,
610 .poll = mousedev_poll,
611 .open = mousedev_open,
612 .release = mousedev_release,
613 .fasync = mousedev_fasync,
614};
615
616static struct input_handle *mousedev_connect(struct input_handler *handler, struct input_dev *dev, struct input_device_id *id)
617{
618 struct mousedev *mousedev;
Greg Kroah-Hartmanc9bcd582005-10-27 22:25:43 -0700619 struct class_device *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 int minor = 0;
621
622 for (minor = 0; minor < MOUSEDEV_MINORS && mousedev_table[minor]; minor++);
623 if (minor == MOUSEDEV_MINORS) {
624 printk(KERN_ERR "mousedev: no more free mousedev devices\n");
625 return NULL;
626 }
627
Eric Sesterhennb39787a2006-03-14 00:09:16 -0500628 if (!(mousedev = kzalloc(sizeof(struct mousedev), GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
631 INIT_LIST_HEAD(&mousedev->list);
632 init_waitqueue_head(&mousedev->wait);
633
634 mousedev->minor = minor;
635 mousedev->exist = 1;
636 mousedev->handle.dev = dev;
637 mousedev->handle.name = mousedev->name;
638 mousedev->handle.handler = handler;
639 mousedev->handle.private = mousedev;
640 sprintf(mousedev->name, "mouse%d", minor);
641
642 if (mousedev_mix.open)
643 input_open_device(&mousedev->handle);
644
645 mousedev_table[minor] = mousedev;
646
Greg Kroah-Hartmanc9bcd582005-10-27 22:25:43 -0700647 cdev = class_device_create(&input_class, &dev->cdev,
gregkh@suse.de12356862005-03-15 14:26:30 -0800648 MKDEV(INPUT_MAJOR, MOUSEDEV_MINOR_BASE + minor),
Greg Kroah-Hartmanc9bcd582005-10-27 22:25:43 -0700649 dev->cdev.dev, mousedev->name);
650
651 /* temporary symlink to keep userspace happy */
652 sysfs_create_link(&input_class.subsys.kset.kobj, &cdev->kobj,
653 mousedev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 return &mousedev->handle;
656}
657
658static void mousedev_disconnect(struct input_handle *handle)
659{
660 struct mousedev *mousedev = handle->private;
661 struct mousedev_list *list;
662
Greg Kroah-Hartmanc9bcd582005-10-27 22:25:43 -0700663 sysfs_remove_link(&input_class.subsys.kset.kobj, mousedev->name);
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -0700664 class_device_destroy(&input_class,
gregkh@suse.de12356862005-03-15 14:26:30 -0800665 MKDEV(INPUT_MAJOR, MOUSEDEV_MINOR_BASE + mousedev->minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 mousedev->exist = 0;
667
668 if (mousedev->open) {
669 input_close_device(handle);
670 wake_up_interruptible(&mousedev->wait);
671 list_for_each_entry(list, &mousedev->list, node)
672 kill_fasync(&list->fasync, SIGIO, POLL_HUP);
673 } else {
674 if (mousedev_mix.open)
675 input_close_device(handle);
676 mousedev_free(mousedev);
677 }
678}
679
680static struct input_device_id mousedev_ids[] = {
681 {
682 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_RELBIT,
683 .evbit = { BIT(EV_KEY) | BIT(EV_REL) },
684 .keybit = { [LONG(BTN_LEFT)] = BIT(BTN_LEFT) },
685 .relbit = { BIT(REL_X) | BIT(REL_Y) },
686 }, /* A mouse like device, at least one button, two relative axes */
687 {
688 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_RELBIT,
689 .evbit = { BIT(EV_KEY) | BIT(EV_REL) },
690 .relbit = { BIT(REL_WHEEL) },
691 }, /* A separate scrollwheel */
692 {
693 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_ABSBIT,
694 .evbit = { BIT(EV_KEY) | BIT(EV_ABS) },
695 .keybit = { [LONG(BTN_TOUCH)] = BIT(BTN_TOUCH) },
696 .absbit = { BIT(ABS_X) | BIT(ABS_Y) },
697 }, /* A tablet like device, at least touch detection, two absolute axes */
698 {
699 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_ABSBIT,
700 .evbit = { BIT(EV_KEY) | BIT(EV_ABS) },
701 .keybit = { [LONG(BTN_TOOL_FINGER)] = BIT(BTN_TOOL_FINGER) },
702 .absbit = { BIT(ABS_X) | BIT(ABS_Y) | BIT(ABS_PRESSURE) | BIT(ABS_TOOL_WIDTH) },
703 }, /* A touchpad */
704
705 { }, /* Terminating entry */
706};
707
708MODULE_DEVICE_TABLE(input, mousedev_ids);
709
710static struct input_handler mousedev_handler = {
711 .event = mousedev_event,
712 .connect = mousedev_connect,
713 .disconnect = mousedev_disconnect,
714 .fops = &mousedev_fops,
715 .minor = MOUSEDEV_MINOR_BASE,
716 .name = "mousedev",
717 .id_table = mousedev_ids,
718};
719
720#ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
721static struct miscdevice psaux_mouse = {
722 PSMOUSE_MINOR, "psaux", &mousedev_fops
723};
724static int psaux_registered;
725#endif
726
727static int __init mousedev_init(void)
728{
729 input_register_handler(&mousedev_handler);
730
731 memset(&mousedev_mix, 0, sizeof(struct mousedev));
732 INIT_LIST_HEAD(&mousedev_mix.list);
733 init_waitqueue_head(&mousedev_mix.wait);
734 mousedev_table[MOUSEDEV_MIX] = &mousedev_mix;
735 mousedev_mix.exist = 1;
736 mousedev_mix.minor = MOUSEDEV_MIX;
737
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -0700738 class_device_create(&input_class, NULL,
gregkh@suse.de12356862005-03-15 14:26:30 -0800739 MKDEV(INPUT_MAJOR, MOUSEDEV_MINOR_BASE + MOUSEDEV_MIX), NULL, "mice");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
741#ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
742 if (!(psaux_registered = !misc_register(&psaux_mouse)))
743 printk(KERN_WARNING "mice: could not misc_register the device\n");
744#endif
745
746 printk(KERN_INFO "mice: PS/2 mouse device common for all mice\n");
747
748 return 0;
749}
750
751static void __exit mousedev_exit(void)
752{
753#ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
754 if (psaux_registered)
755 misc_deregister(&psaux_mouse);
756#endif
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -0700757 class_device_destroy(&input_class,
gregkh@suse.de12356862005-03-15 14:26:30 -0800758 MKDEV(INPUT_MAJOR, MOUSEDEV_MINOR_BASE + MOUSEDEV_MIX));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 input_unregister_handler(&mousedev_handler);
760}
761
762module_init(mousedev_init);
763module_exit(mousedev_exit);