12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #define MOUSEDEV_MINOR_BASE 32
15 #define MOUSEDEV_MINORS 31
16 #define MOUSEDEV_MIX 63
18 #include <linux/sched.h>
19 #include <linux/slab.h>
20 #include <linux/poll.h>
21 #include <linux/module.h>
23 #include <linux/input.h>
24 #include <linux/random.h>
26 #include <linux/device.h>
28 #include <linux/kernel.h>
34 #ifndef CONFIG_INPUT_MOUSEDEV_SCREEN_X
35 #define CONFIG_INPUT_MOUSEDEV_SCREEN_X 1024
37 #ifndef CONFIG_INPUT_MOUSEDEV_SCREEN_Y
38 #define CONFIG_INPUT_MOUSEDEV_SCREEN_Y 768
49 static unsigned tap_time = 200;
51 MODULE_PARM_DESC(tap_time,
"Tap time for touchpads in absolute mode (msecs)");
93 #define PACKET_QUEUE_LEN 16
111 #define MOUSEDEV_SEQ_LEN 6
113 static unsigned char mousedev_imps_seq[] = { 0xf3, 200, 0xf3, 100, 0xf3, 80 };
114 static unsigned char mousedev_imex_seq[] = { 0xf3, 200, 0xf3, 200, 0xf3, 80 };
116 static struct mousedev *mousedev_mix;
119 static void mixdev_open_devices(
void);
120 static void mixdev_close_devices(
void);
122 #define fx(i) (mousedev->old_x[(mousedev->pkt_count - (i)) & 03])
123 #define fy(i) (mousedev->old_y[(mousedev->pkt_count - (i)) & 03])
125 static void mousedev_touchpad_event(
struct input_dev *
dev,
130 enum { FRACTION_DENOM = 128 };
138 size = input_abs_get_max(dev,
ABS_X) -
139 input_abs_get_min(dev,
ABS_X);
143 tmp = ((value -
fx(2)) * 256 * FRACTION_DENOM) /
size;
145 mousedev->
packet.dx = tmp / FRACTION_DENOM;
147 tmp - mousedev->
packet.dx * FRACTION_DENOM;
155 size = input_abs_get_max(dev,
ABS_X) -
156 input_abs_get_min(dev,
ABS_X);
160 tmp = -((value -
fy(2)) * 256 * FRACTION_DENOM) /
size;
162 mousedev->
packet.dy = tmp / FRACTION_DENOM;
164 mousedev->
packet.dy * FRACTION_DENOM;
178 min = input_abs_get_min(dev,
ABS_X);
179 max = input_abs_get_max(dev,
ABS_X);
185 value =
clamp(value, min, max);
188 mousedev->
packet.abs_event = 1;
192 min = input_abs_get_min(dev,
ABS_Y);
193 max = input_abs_get_max(dev,
ABS_Y);
199 value =
clamp(value, min, max);
202 mousedev->
packet.abs_event = 1;
207 static void mousedev_rel_event(
struct mousedev *mousedev,
208 unsigned int code,
int value)
225 static void mousedev_key_event(
struct mousedev *mousedev,
226 unsigned int code,
int value)
264 static void mousedev_notify_readers(
struct mousedev *mousedev,
269 unsigned int new_head;
270 int wake_readers = 0;
281 if (new_head != client->
tail) {
290 client->
pos_x = packet->
x;
291 client->
pos_y = packet->
y;
296 0 : (client->
pos_x >= xres ? xres : client->
pos_x);
299 0 : (client->
pos_y >= yres ? yres : client->
pos_y);
306 if (p->
dx || p->
dy || p->
dz ||
323 static void mousedev_touchpad_touch(
struct mousedev *mousedev,
int value)
326 if (mousedev->
touch &&
336 mousedev_notify_readers(mousedev, &mousedev_mix->
packet);
337 mousedev_notify_readers(mousedev_mix,
346 }
else if (!mousedev->
touch)
350 static void mousedev_event(
struct input_handle *
handle,
351 unsigned int type,
unsigned int code,
int value)
353 struct mousedev *mousedev = handle->private;
363 mousedev_touchpad_event(handle->dev,
364 mousedev, code, value);
366 mousedev_abs_event(handle->dev, mousedev, code, value);
371 mousedev_rel_event(mousedev, code, value);
378 mousedev_touchpad_touch(mousedev, value);
380 mousedev_key_event(mousedev, code, value);
386 if (mousedev->
touch) {
397 mousedev_notify_readers(mousedev, &mousedev->
packet);
398 mousedev_notify_readers(mousedev_mix, &mousedev->
packet);
402 mousedev->
packet.abs_event = 0;
408 static int mousedev_fasync(
int fd,
struct file *
file,
int on)
415 static void mousedev_free(
struct device *dev)
417 struct mousedev *mousedev =
container_of(dev,
struct mousedev, dev);
419 input_put_device(mousedev->
handle.dev);
423 static int mousedev_open_device(
struct mousedev *mousedev)
432 mixdev_open_devices();
433 else if (!mousedev->
exist)
435 else if (!mousedev->
open++) {
436 retval = input_open_device(&mousedev->
handle);
445 static void mousedev_close_device(
struct mousedev *mousedev)
450 mixdev_close_devices();
451 else if (mousedev->
exist && !--mousedev->
open)
452 input_close_device(&mousedev->
handle);
462 static void mixdev_open_devices(
void)
464 struct mousedev *mousedev;
466 if (mousedev_mix->
open++)
471 if (mousedev_open_device(mousedev))
484 static void mixdev_close_devices(
void)
486 struct mousedev *mousedev;
488 if (--mousedev_mix->
open)
494 mousedev_close_device(mousedev);
500 static void mousedev_attach_client(
struct mousedev *mousedev,
508 static void mousedev_detach_client(
struct mousedev *mousedev,
512 list_del_rcu(&client->
node);
520 struct mousedev *mousedev = client->
mousedev;
522 mousedev_detach_client(mousedev, client);
525 mousedev_close_device(mousedev);
533 struct mousedev *mousedev;
536 #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
538 mousedev = mousedev_mix;
548 client->
pos_x = xres / 2;
549 client->
pos_y = yres / 2;
551 mousedev_attach_client(mousedev, client);
553 error = mousedev_open_device(mousedev);
555 goto err_free_client;
563 mousedev_detach_client(mousedev, client);
568 static inline int mousedev_limit_delta(
int delta,
int limit)
570 return delta > limit ? limit : (delta < -limit ? -limit :
delta);
574 signed char *ps2_data)
579 ((p->
dx < 0) << 4) | ((p->
dy < 0) << 5) | (p->
buttons & 0x07);
580 ps2_data[1] = mousedev_limit_delta(p->
dx, 127);
581 ps2_data[2] = mousedev_limit_delta(p->
dy, 127);
582 p->
dx -= ps2_data[1];
583 p->
dy -= ps2_data[2];
585 switch (client->
mode) {
587 ps2_data[3] = mousedev_limit_delta(p->
dz, 7);
588 p->
dz -= ps2_data[3];
589 ps2_data[3] = (ps2_data[3] & 0x0f) | ((p->
buttons & 0x18) << 1);
596 ps2_data[3] = mousedev_limit_delta(p->
dz, 127);
597 p->
dz -= ps2_data[3];
610 if (!p->
dx && !p->
dy && !p->
dz) {
622 client->
ps2[0] = 0xfa;
627 mousedev_packet(client, &client->
ps2[1]);
632 switch (client->
mode) {
647 client->
ps2[1] = 0x60; client->
ps2[2] = 3; client->
ps2[3] = 200;
654 client->
ps2[1] = 0xaa; client->
ps2[2] = 0x00;
665 static ssize_t mousedev_write(
struct file *file,
const char __user *
buffer,
666 size_t count, loff_t *ppos)
672 for (i = 0; i <
count; i++) {
679 if (c == mousedev_imex_seq[client->
imexseq]) {
687 if (c == mousedev_imps_seq[client->
impsseq]) {
695 mousedev_generate_response(client, c);
706 static ssize_t mousedev_read(
struct file *file,
char __user *buffer,
707 size_t count, loff_t *ppos)
710 struct mousedev *mousedev = client->
mousedev;
711 signed char data[
sizeof(client->
ps2)];
723 if (!mousedev->
exist)
729 mousedev_packet(client, client->
ps2);
733 if (count > client->
buffer)
748 static unsigned int mousedev_poll(
struct file *file,
poll_table *
wait)
751 struct mousedev *mousedev = client->
mousedev;
754 poll_wait(file, &mousedev->
wait, wait);
765 .read = mousedev_read,
766 .write = mousedev_write,
767 .poll = mousedev_poll,
768 .open = mousedev_open,
769 .release = mousedev_release,
770 .fasync = mousedev_fasync,
779 static void mousedev_mark_dead(
struct mousedev *mousedev)
782 mousedev->
exist =
false;
790 static void mousedev_hangup(
struct mousedev *mousedev)
797 spin_unlock(&mousedev->client_lock);
802 static
void mousedev_cleanup(
struct mousedev *mousedev)
804 struct input_handle *handle = &mousedev->handle;
806 mousedev_mark_dead(mousedev);
807 mousedev_hangup(mousedev);
813 input_close_device(handle);
816 static int mousedev_reserve_minor(
bool mixdev)
823 pr_err(
"failed to reserve mixdev minor: %d\n", minor);
828 pr_err(
"failed to reserve new minor: %d\n", minor);
834 static struct mousedev *mousedev_create(
struct input_dev *dev,
835 struct input_handler *handler,
838 struct mousedev *mousedev;
842 minor = mousedev_reserve_minor(mixdev);
848 mousedev = kzalloc(
sizeof(
struct mousedev),
GFP_KERNEL);
872 mousedev->
exist =
true;
874 mousedev->
handle.dev = input_get_device(dev);
875 mousedev->
handle.name = dev_name(&mousedev->
dev);
876 mousedev->
handle.handler = handler;
877 mousedev->
handle.private = mousedev;
879 mousedev->
dev.class = &input_class;
881 mousedev->
dev.parent = &dev->dev;
883 mousedev->
dev.release = mousedev_free;
887 error = input_register_handle(&mousedev->
handle);
889 goto err_free_mousedev;
893 mousedev->
cdev.kobj.parent = &mousedev->
dev.kobj;
896 goto err_unregister_handle;
900 goto err_cleanup_mousedev;
904 err_cleanup_mousedev:
905 mousedev_cleanup(mousedev);
906 err_unregister_handle:
908 input_unregister_handle(&mousedev->
handle);
912 input_free_minor(minor);
914 return ERR_PTR(error);
917 static void mousedev_destroy(
struct mousedev *mousedev)
920 mousedev_cleanup(mousedev);
921 input_free_minor(
MINOR(mousedev->
dev.devt));
923 input_unregister_handle(&mousedev->
handle);
927 static int mixdev_add_device(
struct mousedev *mousedev)
935 if (mousedev_mix->
open) {
936 retval = mousedev_open_device(mousedev);
951 static void mixdev_remove_device(
struct mousedev *mousedev)
957 mousedev_close_device(mousedev);
966 static int mousedev_connect(
struct input_handler *handler,
967 struct input_dev *dev,
970 struct mousedev *mousedev;
973 mousedev = mousedev_create(dev, handler,
false);
974 if (IS_ERR(mousedev))
975 return PTR_ERR(mousedev);
977 error = mixdev_add_device(mousedev);
979 mousedev_destroy(mousedev);
986 static void mousedev_disconnect(
struct input_handle *handle)
988 struct mousedev *mousedev = handle->private;
990 mixdev_remove_device(mousedev);
991 mousedev_destroy(mousedev);
1045 static struct input_handler mousedev_handler = {
1046 .event = mousedev_event,
1047 .connect = mousedev_connect,
1048 .disconnect = mousedev_disconnect,
1049 .legacy_minors =
true,
1052 .id_table = mousedev_ids,
1055 #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
1061 .fops = &mousedev_fops,
1064 static bool psaux_registered;
1066 static void __init mousedev_psaux_register(
void)
1072 pr_warn(
"could not register psaux device, error: %d\n",
1075 psaux_registered =
true;
1078 static void __exit mousedev_psaux_unregister(
void)
1080 if (psaux_registered)
1084 static inline void mousedev_psaux_register(
void) { }
1085 static inline void mousedev_psaux_unregister(
void) { }
1088 static int __init mousedev_init(
void)
1092 mousedev_mix = mousedev_create(
NULL, &mousedev_handler,
true);
1093 if (IS_ERR(mousedev_mix))
1094 return PTR_ERR(mousedev_mix);
1096 error = input_register_handler(&mousedev_handler);
1098 mousedev_destroy(mousedev_mix);
1102 mousedev_psaux_register();
1104 pr_info(
"PS/2 mouse device common for all mice\n");
1109 static void __exit mousedev_exit(
void)
1111 mousedev_psaux_unregister();
1112 input_unregister_handler(&mousedev_handler);
1113 mousedev_destroy(mousedev_mix);