Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 12 | #define MOUSEDEV_MINOR_BASE 32 |
| 13 | #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> |
| 27 | #include <linux/devfs_fs_kernel.h> |
| 28 | #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX |
| 29 | #include <linux/miscdevice.h> |
| 30 | #endif |
| 31 | |
| 32 | MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>"); |
| 33 | MODULE_DESCRIPTION("Mouse (ExplorerPS/2) device interfaces"); |
| 34 | MODULE_LICENSE("GPL"); |
| 35 | |
| 36 | #ifndef CONFIG_INPUT_MOUSEDEV_SCREEN_X |
| 37 | #define CONFIG_INPUT_MOUSEDEV_SCREEN_X 1024 |
| 38 | #endif |
| 39 | #ifndef CONFIG_INPUT_MOUSEDEV_SCREEN_Y |
| 40 | #define CONFIG_INPUT_MOUSEDEV_SCREEN_Y 768 |
| 41 | #endif |
| 42 | |
| 43 | static int xres = CONFIG_INPUT_MOUSEDEV_SCREEN_X; |
| 44 | module_param(xres, uint, 0); |
| 45 | MODULE_PARM_DESC(xres, "Horizontal screen resolution"); |
| 46 | |
| 47 | static int yres = CONFIG_INPUT_MOUSEDEV_SCREEN_Y; |
| 48 | module_param(yres, uint, 0); |
| 49 | MODULE_PARM_DESC(yres, "Vertical screen resolution"); |
| 50 | |
| 51 | static unsigned tap_time = 200; |
| 52 | module_param(tap_time, uint, 0); |
| 53 | MODULE_PARM_DESC(tap_time, "Tap time for touchpads in absolute mode (msecs)"); |
| 54 | |
| 55 | struct mousedev_hw_data { |
| 56 | int dx, dy, dz; |
| 57 | int x, y; |
| 58 | int abs_event; |
| 59 | unsigned long buttons; |
| 60 | }; |
| 61 | |
| 62 | struct mousedev { |
| 63 | int exist; |
| 64 | int open; |
| 65 | int minor; |
| 66 | char name[16]; |
| 67 | wait_queue_head_t wait; |
| 68 | struct list_head list; |
| 69 | struct input_handle handle; |
| 70 | |
| 71 | struct mousedev_hw_data packet; |
| 72 | unsigned int pkt_count; |
| 73 | int old_x[4], old_y[4]; |
| 74 | int frac_dx, frac_dy; |
| 75 | unsigned long touch; |
| 76 | }; |
| 77 | |
| 78 | enum mousedev_emul { |
| 79 | MOUSEDEV_EMUL_PS2, |
| 80 | MOUSEDEV_EMUL_IMPS, |
| 81 | MOUSEDEV_EMUL_EXPS |
| 82 | }; |
| 83 | |
| 84 | struct mousedev_motion { |
| 85 | int dx, dy, dz; |
| 86 | unsigned long buttons; |
| 87 | }; |
| 88 | |
| 89 | #define PACKET_QUEUE_LEN 16 |
| 90 | struct mousedev_list { |
| 91 | struct fasync_struct *fasync; |
| 92 | struct mousedev *mousedev; |
| 93 | struct list_head node; |
| 94 | |
| 95 | struct mousedev_motion packets[PACKET_QUEUE_LEN]; |
| 96 | unsigned int head, tail; |
| 97 | spinlock_t packet_lock; |
| 98 | int pos_x, pos_y; |
| 99 | |
| 100 | signed char ps2[6]; |
| 101 | unsigned char ready, buffer, bufsiz; |
| 102 | unsigned char imexseq, impsseq; |
| 103 | enum mousedev_emul mode; |
Pavel Machek | c1e4c8d | 2005-05-27 12:53:03 -0700 | [diff] [blame] | 104 | unsigned long last_buttons; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | #define MOUSEDEV_SEQ_LEN 6 |
| 108 | |
| 109 | static unsigned char mousedev_imps_seq[] = { 0xf3, 200, 0xf3, 100, 0xf3, 80 }; |
| 110 | static unsigned char mousedev_imex_seq[] = { 0xf3, 200, 0xf3, 200, 0xf3, 80 }; |
| 111 | |
| 112 | static struct input_handler mousedev_handler; |
| 113 | |
| 114 | static struct mousedev *mousedev_table[MOUSEDEV_MINORS]; |
| 115 | static struct mousedev mousedev_mix; |
| 116 | |
| 117 | #define fx(i) (mousedev->old_x[(mousedev->pkt_count - (i)) & 03]) |
| 118 | #define fy(i) (mousedev->old_y[(mousedev->pkt_count - (i)) & 03]) |
| 119 | |
| 120 | static void mousedev_touchpad_event(struct input_dev *dev, struct mousedev *mousedev, unsigned int code, int value) |
| 121 | { |
| 122 | int size, tmp; |
| 123 | enum { FRACTION_DENOM = 128 }; |
| 124 | |
| 125 | if (mousedev->touch) { |
| 126 | size = dev->absmax[ABS_X] - dev->absmin[ABS_X]; |
| 127 | if (size == 0) size = 256 * 2; |
| 128 | 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 | |
| 152 | static 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]; |
| 159 | if (size == 0) size = xres; |
| 160 | if (value > dev->absmax[ABS_X]) value = dev->absmax[ABS_X]; |
| 161 | if (value < dev->absmin[ABS_X]) value = dev->absmin[ABS_X]; |
| 162 | mousedev->packet.x = ((value - dev->absmin[ABS_X]) * xres) / size; |
| 163 | mousedev->packet.abs_event = 1; |
| 164 | break; |
| 165 | |
| 166 | case ABS_Y: |
| 167 | size = dev->absmax[ABS_Y] - dev->absmin[ABS_Y]; |
| 168 | if (size == 0) size = yres; |
| 169 | if (value > dev->absmax[ABS_Y]) value = dev->absmax[ABS_Y]; |
| 170 | if (value < dev->absmin[ABS_Y]) value = dev->absmin[ABS_Y]; |
| 171 | mousedev->packet.y = yres - ((value - dev->absmin[ABS_Y]) * yres) / size; |
| 172 | mousedev->packet.abs_event = 1; |
| 173 | break; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | static void mousedev_rel_event(struct mousedev *mousedev, unsigned int code, int value) |
| 178 | { |
| 179 | switch (code) { |
| 180 | case REL_X: mousedev->packet.dx += value; break; |
| 181 | case REL_Y: mousedev->packet.dy -= value; break; |
| 182 | case REL_WHEEL: mousedev->packet.dz -= value; break; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | static void mousedev_key_event(struct mousedev *mousedev, unsigned int code, int value) |
| 187 | { |
| 188 | int index; |
| 189 | |
| 190 | switch (code) { |
| 191 | case BTN_TOUCH: |
| 192 | case BTN_0: |
| 193 | case BTN_FORWARD: |
| 194 | case BTN_LEFT: index = 0; break; |
| 195 | case BTN_STYLUS: |
| 196 | case BTN_1: |
| 197 | case BTN_RIGHT: index = 1; break; |
| 198 | case BTN_2: |
| 199 | case BTN_STYLUS2: |
| 200 | case BTN_MIDDLE: index = 2; break; |
| 201 | case BTN_3: |
| 202 | case BTN_BACK: |
| 203 | case BTN_SIDE: index = 3; break; |
| 204 | case BTN_4: |
| 205 | case BTN_EXTRA: index = 4; break; |
| 206 | default: return; |
| 207 | } |
| 208 | |
| 209 | if (value) { |
| 210 | set_bit(index, &mousedev->packet.buttons); |
| 211 | set_bit(index, &mousedev_mix.packet.buttons); |
| 212 | } else { |
| 213 | clear_bit(index, &mousedev->packet.buttons); |
| 214 | clear_bit(index, &mousedev_mix.packet.buttons); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | static void mousedev_notify_readers(struct mousedev *mousedev, struct mousedev_hw_data *packet) |
| 219 | { |
| 220 | struct mousedev_list *list; |
| 221 | struct mousedev_motion *p; |
| 222 | unsigned long flags; |
| 223 | |
| 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 Machek | c1e4c8d | 2005-05-27 12:53:03 -0700 | [diff] [blame] | 228 | if (list->ready && p->buttons != mousedev->packet.buttons) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | 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 Machek | c1e4c8d | 2005-05-27 12:53:03 -0700 | [diff] [blame] | 253 | if (p->dx || p->dy || p->dz || p->buttons != list->last_buttons) |
| 254 | list->ready = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | |
| 256 | spin_unlock_irqrestore(&list->packet_lock, flags); |
Pavel Machek | c1e4c8d | 2005-05-27 12:53:03 -0700 | [diff] [blame] | 257 | |
| 258 | if (list->ready) |
| 259 | kill_fasync(&list->fasync, SIGIO, POLL_IN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | wake_up_interruptible(&mousedev->wait); |
| 263 | } |
| 264 | |
| 265 | static void mousedev_touchpad_touch(struct mousedev *mousedev, int value) |
| 266 | { |
| 267 | if (!value) { |
| 268 | if (mousedev->touch && |
| 269 | time_before(jiffies, mousedev->touch + msecs_to_jiffies(tap_time))) { |
| 270 | /* |
| 271 | * Toggle left button to emulate tap. |
| 272 | * We rely on the fact that mousedev_mix always has 0 |
| 273 | * motion packet so we won't mess current position. |
| 274 | */ |
| 275 | set_bit(0, &mousedev->packet.buttons); |
| 276 | set_bit(0, &mousedev_mix.packet.buttons); |
| 277 | mousedev_notify_readers(mousedev, &mousedev_mix.packet); |
| 278 | mousedev_notify_readers(&mousedev_mix, &mousedev_mix.packet); |
| 279 | clear_bit(0, &mousedev->packet.buttons); |
| 280 | clear_bit(0, &mousedev_mix.packet.buttons); |
| 281 | } |
| 282 | mousedev->touch = mousedev->pkt_count = 0; |
| 283 | mousedev->frac_dx = 0; |
| 284 | mousedev->frac_dy = 0; |
| 285 | } |
| 286 | else |
| 287 | if (!mousedev->touch) |
| 288 | mousedev->touch = jiffies; |
| 289 | } |
| 290 | |
| 291 | static void mousedev_event(struct input_handle *handle, unsigned int type, unsigned int code, int value) |
| 292 | { |
| 293 | struct mousedev *mousedev = handle->private; |
| 294 | |
| 295 | switch (type) { |
| 296 | case EV_ABS: |
| 297 | /* Ignore joysticks */ |
| 298 | if (test_bit(BTN_TRIGGER, handle->dev->keybit)) |
| 299 | return; |
| 300 | |
| 301 | if (test_bit(BTN_TOOL_FINGER, handle->dev->keybit)) |
| 302 | mousedev_touchpad_event(handle->dev, mousedev, code, value); |
| 303 | else |
| 304 | mousedev_abs_event(handle->dev, mousedev, code, value); |
| 305 | |
| 306 | break; |
| 307 | |
| 308 | case EV_REL: |
| 309 | mousedev_rel_event(mousedev, code, value); |
| 310 | break; |
| 311 | |
| 312 | case EV_KEY: |
| 313 | if (value != 2) { |
| 314 | if (code == BTN_TOUCH && test_bit(BTN_TOOL_FINGER, handle->dev->keybit)) |
| 315 | mousedev_touchpad_touch(mousedev, value); |
| 316 | else |
| 317 | mousedev_key_event(mousedev, code, value); |
| 318 | } |
| 319 | break; |
| 320 | |
| 321 | case EV_SYN: |
| 322 | if (code == SYN_REPORT) { |
| 323 | if (mousedev->touch) { |
| 324 | mousedev->pkt_count++; |
| 325 | /* Input system eats duplicate events, but we need all of them |
| 326 | * to do correct averaging so apply present one forward |
| 327 | */ |
| 328 | fx(0) = fx(1); |
| 329 | fy(0) = fy(1); |
| 330 | } |
| 331 | |
| 332 | mousedev_notify_readers(mousedev, &mousedev->packet); |
| 333 | mousedev_notify_readers(&mousedev_mix, &mousedev->packet); |
| 334 | |
| 335 | mousedev->packet.dx = mousedev->packet.dy = mousedev->packet.dz = 0; |
| 336 | mousedev->packet.abs_event = 0; |
| 337 | } |
| 338 | break; |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | static int mousedev_fasync(int fd, struct file *file, int on) |
| 343 | { |
| 344 | int retval; |
| 345 | struct mousedev_list *list = file->private_data; |
| 346 | retval = fasync_helper(fd, file, on, &list->fasync); |
| 347 | return retval < 0 ? retval : 0; |
| 348 | } |
| 349 | |
| 350 | static void mousedev_free(struct mousedev *mousedev) |
| 351 | { |
| 352 | mousedev_table[mousedev->minor] = NULL; |
| 353 | kfree(mousedev); |
| 354 | } |
| 355 | |
| 356 | static int mixdev_release(void) |
| 357 | { |
| 358 | struct input_handle *handle; |
| 359 | |
| 360 | list_for_each_entry(handle, &mousedev_handler.h_list, h_node) { |
| 361 | struct mousedev *mousedev = handle->private; |
| 362 | |
| 363 | if (!mousedev->open) { |
| 364 | if (mousedev->exist) |
| 365 | input_close_device(&mousedev->handle); |
| 366 | else |
| 367 | mousedev_free(mousedev); |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | return 0; |
| 372 | } |
| 373 | |
| 374 | static int mousedev_release(struct inode * inode, struct file * file) |
| 375 | { |
| 376 | struct mousedev_list *list = file->private_data; |
| 377 | |
| 378 | mousedev_fasync(-1, file, 0); |
| 379 | |
| 380 | list_del(&list->node); |
| 381 | |
| 382 | if (!--list->mousedev->open) { |
| 383 | if (list->mousedev->minor == MOUSEDEV_MIX) |
| 384 | return mixdev_release(); |
| 385 | |
| 386 | if (!mousedev_mix.open) { |
| 387 | 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 | |
| 398 | static 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 | |
| 415 | if (!(list = kmalloc(sizeof(struct mousedev_list), GFP_KERNEL))) |
| 416 | return -ENOMEM; |
| 417 | memset(list, 0, sizeof(struct mousedev_list)); |
| 418 | |
| 419 | spin_lock_init(&list->packet_lock); |
| 420 | list->pos_x = xres / 2; |
| 421 | list->pos_y = yres / 2; |
| 422 | list->mousedev = mousedev_table[i]; |
| 423 | list_add_tail(&list->node, &mousedev_table[i]->list); |
| 424 | file->private_data = list; |
| 425 | |
| 426 | if (!list->mousedev->open++) { |
| 427 | if (list->mousedev->minor == MOUSEDEV_MIX) { |
| 428 | list_for_each_entry(handle, &mousedev_handler.h_list, h_node) { |
| 429 | mousedev = handle->private; |
| 430 | if (!mousedev->open && mousedev->exist) |
| 431 | input_open_device(handle); |
| 432 | } |
| 433 | } else |
| 434 | if (!mousedev_mix.open && list->mousedev->exist) |
| 435 | input_open_device(&list->mousedev->handle); |
| 436 | } |
| 437 | |
| 438 | return 0; |
| 439 | } |
| 440 | |
| 441 | static inline int mousedev_limit_delta(int delta, int limit) |
| 442 | { |
| 443 | return delta > limit ? limit : (delta < -limit ? -limit : delta); |
| 444 | } |
| 445 | |
| 446 | static void mousedev_packet(struct mousedev_list *list, signed char *ps2_data) |
| 447 | { |
| 448 | struct mousedev_motion *p; |
| 449 | unsigned long flags; |
| 450 | |
| 451 | spin_lock_irqsave(&list->packet_lock, flags); |
| 452 | p = &list->packets[list->tail]; |
| 453 | |
| 454 | ps2_data[0] = 0x08 | ((p->dx < 0) << 4) | ((p->dy < 0) << 5) | (p->buttons & 0x07); |
| 455 | ps2_data[1] = mousedev_limit_delta(p->dx, 127); |
| 456 | ps2_data[2] = mousedev_limit_delta(p->dy, 127); |
| 457 | p->dx -= ps2_data[1]; |
| 458 | p->dy -= ps2_data[2]; |
| 459 | |
| 460 | switch (list->mode) { |
| 461 | case MOUSEDEV_EMUL_EXPS: |
| 462 | ps2_data[3] = mousedev_limit_delta(p->dz, 7); |
| 463 | p->dz -= ps2_data[3]; |
| 464 | ps2_data[3] = (ps2_data[3] & 0x0f) | ((p->buttons & 0x18) << 1); |
| 465 | list->bufsiz = 4; |
| 466 | break; |
| 467 | |
| 468 | case MOUSEDEV_EMUL_IMPS: |
| 469 | ps2_data[0] |= ((p->buttons & 0x10) >> 3) | ((p->buttons & 0x08) >> 1); |
| 470 | ps2_data[3] = mousedev_limit_delta(p->dz, 127); |
| 471 | p->dz -= ps2_data[3]; |
| 472 | list->bufsiz = 4; |
| 473 | break; |
| 474 | |
| 475 | case MOUSEDEV_EMUL_PS2: |
| 476 | default: |
| 477 | ps2_data[0] |= ((p->buttons & 0x10) >> 3) | ((p->buttons & 0x08) >> 1); |
| 478 | p->dz = 0; |
| 479 | list->bufsiz = 3; |
| 480 | break; |
| 481 | } |
| 482 | |
| 483 | if (!p->dx && !p->dy && !p->dz) { |
Pavel Machek | c1e4c8d | 2005-05-27 12:53:03 -0700 | [diff] [blame] | 484 | if (list->tail == list->head) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | list->ready = 0; |
Pavel Machek | c1e4c8d | 2005-05-27 12:53:03 -0700 | [diff] [blame] | 486 | list->last_buttons = p->buttons; |
| 487 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | list->tail = (list->tail + 1) % PACKET_QUEUE_LEN; |
| 489 | } |
| 490 | |
| 491 | spin_unlock_irqrestore(&list->packet_lock, flags); |
| 492 | } |
| 493 | |
| 494 | |
| 495 | static ssize_t mousedev_write(struct file * file, const char __user * buffer, size_t count, loff_t *ppos) |
| 496 | { |
| 497 | struct mousedev_list *list = file->private_data; |
| 498 | unsigned char c; |
| 499 | unsigned int i; |
| 500 | |
| 501 | for (i = 0; i < count; i++) { |
| 502 | |
| 503 | if (get_user(c, buffer + i)) |
| 504 | return -EFAULT; |
| 505 | |
| 506 | if (c == mousedev_imex_seq[list->imexseq]) { |
| 507 | if (++list->imexseq == MOUSEDEV_SEQ_LEN) { |
| 508 | list->imexseq = 0; |
| 509 | list->mode = MOUSEDEV_EMUL_EXPS; |
| 510 | } |
| 511 | } else list->imexseq = 0; |
| 512 | |
| 513 | if (c == mousedev_imps_seq[list->impsseq]) { |
| 514 | if (++list->impsseq == MOUSEDEV_SEQ_LEN) { |
| 515 | list->impsseq = 0; |
| 516 | list->mode = MOUSEDEV_EMUL_IMPS; |
| 517 | } |
| 518 | } else list->impsseq = 0; |
| 519 | |
| 520 | list->ps2[0] = 0xfa; |
| 521 | |
| 522 | switch (c) { |
| 523 | |
| 524 | case 0xeb: /* Poll */ |
| 525 | mousedev_packet(list, &list->ps2[1]); |
| 526 | list->bufsiz++; /* account for leading ACK */ |
| 527 | break; |
| 528 | |
| 529 | case 0xf2: /* Get ID */ |
| 530 | switch (list->mode) { |
| 531 | case MOUSEDEV_EMUL_PS2: list->ps2[1] = 0; break; |
| 532 | case MOUSEDEV_EMUL_IMPS: list->ps2[1] = 3; break; |
| 533 | case MOUSEDEV_EMUL_EXPS: list->ps2[1] = 4; break; |
| 534 | } |
| 535 | list->bufsiz = 2; |
| 536 | break; |
| 537 | |
| 538 | case 0xe9: /* Get info */ |
| 539 | list->ps2[1] = 0x60; list->ps2[2] = 3; list->ps2[3] = 200; |
| 540 | list->bufsiz = 4; |
| 541 | break; |
| 542 | |
| 543 | case 0xff: /* Reset */ |
| 544 | list->impsseq = list->imexseq = 0; |
| 545 | list->mode = MOUSEDEV_EMUL_PS2; |
| 546 | list->ps2[1] = 0xaa; list->ps2[2] = 0x00; |
| 547 | list->bufsiz = 3; |
| 548 | break; |
| 549 | |
| 550 | default: |
| 551 | list->bufsiz = 1; |
| 552 | break; |
| 553 | } |
| 554 | |
| 555 | list->buffer = list->bufsiz; |
| 556 | } |
| 557 | |
| 558 | kill_fasync(&list->fasync, SIGIO, POLL_IN); |
| 559 | |
| 560 | wake_up_interruptible(&list->mousedev->wait); |
| 561 | |
| 562 | return count; |
| 563 | } |
| 564 | |
| 565 | static ssize_t mousedev_read(struct file * file, char __user * buffer, size_t count, loff_t *ppos) |
| 566 | { |
| 567 | struct mousedev_list *list = file->private_data; |
| 568 | int retval = 0; |
| 569 | |
| 570 | if (!list->ready && !list->buffer && (file->f_flags & O_NONBLOCK)) |
| 571 | return -EAGAIN; |
| 572 | |
| 573 | retval = wait_event_interruptible(list->mousedev->wait, |
| 574 | !list->mousedev->exist || list->ready || list->buffer); |
| 575 | |
| 576 | if (retval) |
| 577 | return retval; |
| 578 | |
| 579 | if (!list->mousedev->exist) |
| 580 | return -ENODEV; |
| 581 | |
| 582 | if (!list->buffer && list->ready) { |
| 583 | mousedev_packet(list, list->ps2); |
| 584 | list->buffer = list->bufsiz; |
| 585 | } |
| 586 | |
| 587 | if (count > list->buffer) |
| 588 | count = list->buffer; |
| 589 | |
| 590 | list->buffer -= count; |
| 591 | |
| 592 | if (copy_to_user(buffer, list->ps2 + list->bufsiz - list->buffer - count, count)) |
| 593 | return -EFAULT; |
| 594 | |
| 595 | return count; |
| 596 | } |
| 597 | |
| 598 | /* No kernel lock - fine */ |
| 599 | static unsigned int mousedev_poll(struct file *file, poll_table *wait) |
| 600 | { |
| 601 | struct mousedev_list *list = file->private_data; |
| 602 | poll_wait(file, &list->mousedev->wait, wait); |
| 603 | return ((list->ready || list->buffer) ? (POLLIN | POLLRDNORM) : 0) | |
| 604 | (list->mousedev->exist ? 0 : (POLLHUP | POLLERR)); |
| 605 | } |
| 606 | |
| 607 | static struct file_operations mousedev_fops = { |
| 608 | .owner = THIS_MODULE, |
| 609 | .read = mousedev_read, |
| 610 | .write = mousedev_write, |
| 611 | .poll = mousedev_poll, |
| 612 | .open = mousedev_open, |
| 613 | .release = mousedev_release, |
| 614 | .fasync = mousedev_fasync, |
| 615 | }; |
| 616 | |
| 617 | static struct input_handle *mousedev_connect(struct input_handler *handler, struct input_dev *dev, struct input_device_id *id) |
| 618 | { |
| 619 | struct mousedev *mousedev; |
| 620 | 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 | |
| 628 | if (!(mousedev = kmalloc(sizeof(struct mousedev), GFP_KERNEL))) |
| 629 | return NULL; |
| 630 | memset(mousedev, 0, sizeof(struct mousedev)); |
| 631 | |
| 632 | INIT_LIST_HEAD(&mousedev->list); |
| 633 | init_waitqueue_head(&mousedev->wait); |
| 634 | |
| 635 | mousedev->minor = minor; |
| 636 | mousedev->exist = 1; |
| 637 | mousedev->handle.dev = dev; |
| 638 | mousedev->handle.name = mousedev->name; |
| 639 | mousedev->handle.handler = handler; |
| 640 | mousedev->handle.private = mousedev; |
| 641 | sprintf(mousedev->name, "mouse%d", minor); |
| 642 | |
| 643 | if (mousedev_mix.open) |
| 644 | input_open_device(&mousedev->handle); |
| 645 | |
| 646 | mousedev_table[minor] = mousedev; |
| 647 | |
| 648 | devfs_mk_cdev(MKDEV(INPUT_MAJOR, MOUSEDEV_MINOR_BASE + minor), |
| 649 | S_IFCHR|S_IRUGO|S_IWUSR, "input/mouse%d", minor); |
gregkh@suse.de | 1235686 | 2005-03-15 14:26:30 -0800 | [diff] [blame^] | 650 | class_device_create(input_class, |
| 651 | MKDEV(INPUT_MAJOR, MOUSEDEV_MINOR_BASE + minor), |
| 652 | dev->dev, "mouse%d", minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | |
| 654 | return &mousedev->handle; |
| 655 | } |
| 656 | |
| 657 | static void mousedev_disconnect(struct input_handle *handle) |
| 658 | { |
| 659 | struct mousedev *mousedev = handle->private; |
| 660 | struct mousedev_list *list; |
| 661 | |
gregkh@suse.de | 1235686 | 2005-03-15 14:26:30 -0800 | [diff] [blame^] | 662 | class_device_destroy(input_class, |
| 663 | MKDEV(INPUT_MAJOR, MOUSEDEV_MINOR_BASE + mousedev->minor)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | devfs_remove("input/mouse%d", mousedev->minor); |
| 665 | mousedev->exist = 0; |
| 666 | |
| 667 | if (mousedev->open) { |
| 668 | input_close_device(handle); |
| 669 | wake_up_interruptible(&mousedev->wait); |
| 670 | list_for_each_entry(list, &mousedev->list, node) |
| 671 | kill_fasync(&list->fasync, SIGIO, POLL_HUP); |
| 672 | } else { |
| 673 | if (mousedev_mix.open) |
| 674 | input_close_device(handle); |
| 675 | mousedev_free(mousedev); |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | static struct input_device_id mousedev_ids[] = { |
| 680 | { |
| 681 | .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_RELBIT, |
| 682 | .evbit = { BIT(EV_KEY) | BIT(EV_REL) }, |
| 683 | .keybit = { [LONG(BTN_LEFT)] = BIT(BTN_LEFT) }, |
| 684 | .relbit = { BIT(REL_X) | BIT(REL_Y) }, |
| 685 | }, /* A mouse like device, at least one button, two relative axes */ |
| 686 | { |
| 687 | .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_RELBIT, |
| 688 | .evbit = { BIT(EV_KEY) | BIT(EV_REL) }, |
| 689 | .relbit = { BIT(REL_WHEEL) }, |
| 690 | }, /* A separate scrollwheel */ |
| 691 | { |
| 692 | .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_ABSBIT, |
| 693 | .evbit = { BIT(EV_KEY) | BIT(EV_ABS) }, |
| 694 | .keybit = { [LONG(BTN_TOUCH)] = BIT(BTN_TOUCH) }, |
| 695 | .absbit = { BIT(ABS_X) | BIT(ABS_Y) }, |
| 696 | }, /* A tablet like device, at least touch detection, two absolute axes */ |
| 697 | { |
| 698 | .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_ABSBIT, |
| 699 | .evbit = { BIT(EV_KEY) | BIT(EV_ABS) }, |
| 700 | .keybit = { [LONG(BTN_TOOL_FINGER)] = BIT(BTN_TOOL_FINGER) }, |
| 701 | .absbit = { BIT(ABS_X) | BIT(ABS_Y) | BIT(ABS_PRESSURE) | BIT(ABS_TOOL_WIDTH) }, |
| 702 | }, /* A touchpad */ |
| 703 | |
| 704 | { }, /* Terminating entry */ |
| 705 | }; |
| 706 | |
| 707 | MODULE_DEVICE_TABLE(input, mousedev_ids); |
| 708 | |
| 709 | static struct input_handler mousedev_handler = { |
| 710 | .event = mousedev_event, |
| 711 | .connect = mousedev_connect, |
| 712 | .disconnect = mousedev_disconnect, |
| 713 | .fops = &mousedev_fops, |
| 714 | .minor = MOUSEDEV_MINOR_BASE, |
| 715 | .name = "mousedev", |
| 716 | .id_table = mousedev_ids, |
| 717 | }; |
| 718 | |
| 719 | #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX |
| 720 | static struct miscdevice psaux_mouse = { |
| 721 | PSMOUSE_MINOR, "psaux", &mousedev_fops |
| 722 | }; |
| 723 | static int psaux_registered; |
| 724 | #endif |
| 725 | |
| 726 | static int __init mousedev_init(void) |
| 727 | { |
| 728 | input_register_handler(&mousedev_handler); |
| 729 | |
| 730 | memset(&mousedev_mix, 0, sizeof(struct mousedev)); |
| 731 | INIT_LIST_HEAD(&mousedev_mix.list); |
| 732 | init_waitqueue_head(&mousedev_mix.wait); |
| 733 | mousedev_table[MOUSEDEV_MIX] = &mousedev_mix; |
| 734 | mousedev_mix.exist = 1; |
| 735 | mousedev_mix.minor = MOUSEDEV_MIX; |
| 736 | |
| 737 | devfs_mk_cdev(MKDEV(INPUT_MAJOR, MOUSEDEV_MINOR_BASE + MOUSEDEV_MIX), |
| 738 | S_IFCHR|S_IRUGO|S_IWUSR, "input/mice"); |
gregkh@suse.de | 1235686 | 2005-03-15 14:26:30 -0800 | [diff] [blame^] | 739 | class_device_create(input_class, |
| 740 | MKDEV(INPUT_MAJOR, MOUSEDEV_MINOR_BASE + MOUSEDEV_MIX), NULL, "mice"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | |
| 742 | #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX |
| 743 | if (!(psaux_registered = !misc_register(&psaux_mouse))) |
| 744 | printk(KERN_WARNING "mice: could not misc_register the device\n"); |
| 745 | #endif |
| 746 | |
| 747 | printk(KERN_INFO "mice: PS/2 mouse device common for all mice\n"); |
| 748 | |
| 749 | return 0; |
| 750 | } |
| 751 | |
| 752 | static void __exit mousedev_exit(void) |
| 753 | { |
| 754 | #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX |
| 755 | if (psaux_registered) |
| 756 | misc_deregister(&psaux_mouse); |
| 757 | #endif |
| 758 | devfs_remove("input/mice"); |
gregkh@suse.de | 1235686 | 2005-03-15 14:26:30 -0800 | [diff] [blame^] | 759 | class_device_destroy(input_class, |
| 760 | MKDEV(INPUT_MAJOR, MOUSEDEV_MINOR_BASE + MOUSEDEV_MIX)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | input_unregister_handler(&mousedev_handler); |
| 762 | } |
| 763 | |
| 764 | module_init(mousedev_init); |
| 765 | module_exit(mousedev_exit); |