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; |
| 104 | }; |
| 105 | |
| 106 | #define MOUSEDEV_SEQ_LEN 6 |
| 107 | |
| 108 | static unsigned char mousedev_imps_seq[] = { 0xf3, 200, 0xf3, 100, 0xf3, 80 }; |
| 109 | static unsigned char mousedev_imex_seq[] = { 0xf3, 200, 0xf3, 200, 0xf3, 80 }; |
| 110 | |
| 111 | static struct input_handler mousedev_handler; |
| 112 | |
| 113 | static struct mousedev *mousedev_table[MOUSEDEV_MINORS]; |
| 114 | static 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 | |
| 119 | static 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 | |
| 151 | static 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]; |
| 158 | if (size == 0) size = xres; |
| 159 | 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]; |
| 167 | if (size == 0) size = yres; |
| 168 | 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 | |
| 176 | static 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 | |
| 185 | static 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 | |
| 217 | static 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; |
| 222 | |
| 223 | list_for_each_entry(list, &mousedev->list, node) { |
| 224 | spin_lock_irqsave(&list->packet_lock, flags); |
| 225 | |
| 226 | p = &list->packets[list->head]; |
| 227 | if (list->ready && p->buttons != packet->buttons) { |
| 228 | unsigned int new_head = (list->head + 1) % PACKET_QUEUE_LEN; |
| 229 | if (new_head != list->tail) { |
| 230 | p = &list->packets[list->head = new_head]; |
| 231 | memset(p, 0, sizeof(struct mousedev_motion)); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | if (packet->abs_event) { |
| 236 | p->dx += packet->x - list->pos_x; |
| 237 | p->dy += packet->y - list->pos_y; |
| 238 | list->pos_x = packet->x; |
| 239 | list->pos_y = packet->y; |
| 240 | } |
| 241 | |
| 242 | list->pos_x += packet->dx; |
| 243 | list->pos_x = list->pos_x < 0 ? 0 : (list->pos_x >= xres ? xres : list->pos_x); |
| 244 | list->pos_y += packet->dy; |
| 245 | list->pos_y = list->pos_y < 0 ? 0 : (list->pos_y >= yres ? yres : list->pos_y); |
| 246 | |
| 247 | p->dx += packet->dx; |
| 248 | p->dy += packet->dy; |
| 249 | p->dz += packet->dz; |
| 250 | p->buttons = mousedev->packet.buttons; |
| 251 | |
| 252 | list->ready = 1; |
| 253 | |
| 254 | spin_unlock_irqrestore(&list->packet_lock, flags); |
| 255 | kill_fasync(&list->fasync, SIGIO, POLL_IN); |
| 256 | } |
| 257 | |
| 258 | wake_up_interruptible(&mousedev->wait); |
| 259 | } |
| 260 | |
| 261 | static void mousedev_touchpad_touch(struct mousedev *mousedev, int value) |
| 262 | { |
| 263 | if (!value) { |
| 264 | if (mousedev->touch && |
| 265 | time_before(jiffies, mousedev->touch + msecs_to_jiffies(tap_time))) { |
| 266 | /* |
| 267 | * Toggle left button to emulate tap. |
| 268 | * We rely on the fact that mousedev_mix always has 0 |
| 269 | * motion packet so we won't mess current position. |
| 270 | */ |
| 271 | set_bit(0, &mousedev->packet.buttons); |
| 272 | set_bit(0, &mousedev_mix.packet.buttons); |
| 273 | mousedev_notify_readers(mousedev, &mousedev_mix.packet); |
| 274 | mousedev_notify_readers(&mousedev_mix, &mousedev_mix.packet); |
| 275 | clear_bit(0, &mousedev->packet.buttons); |
| 276 | clear_bit(0, &mousedev_mix.packet.buttons); |
| 277 | } |
| 278 | mousedev->touch = mousedev->pkt_count = 0; |
| 279 | mousedev->frac_dx = 0; |
| 280 | mousedev->frac_dy = 0; |
| 281 | } |
| 282 | else |
| 283 | if (!mousedev->touch) |
| 284 | mousedev->touch = jiffies; |
| 285 | } |
| 286 | |
| 287 | static void mousedev_event(struct input_handle *handle, unsigned int type, unsigned int code, int value) |
| 288 | { |
| 289 | struct mousedev *mousedev = handle->private; |
| 290 | |
| 291 | switch (type) { |
| 292 | case EV_ABS: |
| 293 | /* Ignore joysticks */ |
| 294 | if (test_bit(BTN_TRIGGER, handle->dev->keybit)) |
| 295 | return; |
| 296 | |
| 297 | if (test_bit(BTN_TOOL_FINGER, handle->dev->keybit)) |
| 298 | mousedev_touchpad_event(handle->dev, mousedev, code, value); |
| 299 | else |
| 300 | mousedev_abs_event(handle->dev, mousedev, code, value); |
| 301 | |
| 302 | break; |
| 303 | |
| 304 | case EV_REL: |
| 305 | mousedev_rel_event(mousedev, code, value); |
| 306 | break; |
| 307 | |
| 308 | case EV_KEY: |
| 309 | if (value != 2) { |
| 310 | if (code == BTN_TOUCH && test_bit(BTN_TOOL_FINGER, handle->dev->keybit)) |
| 311 | mousedev_touchpad_touch(mousedev, value); |
| 312 | else |
| 313 | mousedev_key_event(mousedev, code, value); |
| 314 | } |
| 315 | break; |
| 316 | |
| 317 | case EV_SYN: |
| 318 | if (code == SYN_REPORT) { |
| 319 | if (mousedev->touch) { |
| 320 | mousedev->pkt_count++; |
| 321 | /* Input system eats duplicate events, but we need all of them |
| 322 | * to do correct averaging so apply present one forward |
| 323 | */ |
| 324 | fx(0) = fx(1); |
| 325 | fy(0) = fy(1); |
| 326 | } |
| 327 | |
| 328 | mousedev_notify_readers(mousedev, &mousedev->packet); |
| 329 | mousedev_notify_readers(&mousedev_mix, &mousedev->packet); |
| 330 | |
| 331 | mousedev->packet.dx = mousedev->packet.dy = mousedev->packet.dz = 0; |
| 332 | mousedev->packet.abs_event = 0; |
| 333 | } |
| 334 | break; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | static int mousedev_fasync(int fd, struct file *file, int on) |
| 339 | { |
| 340 | int retval; |
| 341 | struct mousedev_list *list = file->private_data; |
| 342 | retval = fasync_helper(fd, file, on, &list->fasync); |
| 343 | return retval < 0 ? retval : 0; |
| 344 | } |
| 345 | |
| 346 | static void mousedev_free(struct mousedev *mousedev) |
| 347 | { |
| 348 | mousedev_table[mousedev->minor] = NULL; |
| 349 | kfree(mousedev); |
| 350 | } |
| 351 | |
| 352 | static int mixdev_release(void) |
| 353 | { |
| 354 | struct input_handle *handle; |
| 355 | |
| 356 | list_for_each_entry(handle, &mousedev_handler.h_list, h_node) { |
| 357 | struct mousedev *mousedev = handle->private; |
| 358 | |
| 359 | if (!mousedev->open) { |
| 360 | if (mousedev->exist) |
| 361 | input_close_device(&mousedev->handle); |
| 362 | else |
| 363 | mousedev_free(mousedev); |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | return 0; |
| 368 | } |
| 369 | |
| 370 | static int mousedev_release(struct inode * inode, struct file * file) |
| 371 | { |
| 372 | struct mousedev_list *list = file->private_data; |
| 373 | |
| 374 | mousedev_fasync(-1, file, 0); |
| 375 | |
| 376 | list_del(&list->node); |
| 377 | |
| 378 | if (!--list->mousedev->open) { |
| 379 | if (list->mousedev->minor == MOUSEDEV_MIX) |
| 380 | return mixdev_release(); |
| 381 | |
| 382 | if (!mousedev_mix.open) { |
| 383 | if (list->mousedev->exist) |
| 384 | input_close_device(&list->mousedev->handle); |
| 385 | else |
| 386 | mousedev_free(list->mousedev); |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | kfree(list); |
| 391 | return 0; |
| 392 | } |
| 393 | |
| 394 | static int mousedev_open(struct inode * inode, struct file * file) |
| 395 | { |
| 396 | struct mousedev_list *list; |
| 397 | struct input_handle *handle; |
| 398 | struct mousedev *mousedev; |
| 399 | int i; |
| 400 | |
| 401 | #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX |
| 402 | if (imajor(inode) == MISC_MAJOR) |
| 403 | i = MOUSEDEV_MIX; |
| 404 | else |
| 405 | #endif |
| 406 | i = iminor(inode) - MOUSEDEV_MINOR_BASE; |
| 407 | |
| 408 | if (i >= MOUSEDEV_MINORS || !mousedev_table[i]) |
| 409 | return -ENODEV; |
| 410 | |
| 411 | if (!(list = kmalloc(sizeof(struct mousedev_list), GFP_KERNEL))) |
| 412 | return -ENOMEM; |
| 413 | memset(list, 0, sizeof(struct mousedev_list)); |
| 414 | |
| 415 | spin_lock_init(&list->packet_lock); |
| 416 | list->pos_x = xres / 2; |
| 417 | list->pos_y = yres / 2; |
| 418 | list->mousedev = mousedev_table[i]; |
| 419 | list_add_tail(&list->node, &mousedev_table[i]->list); |
| 420 | file->private_data = list; |
| 421 | |
| 422 | if (!list->mousedev->open++) { |
| 423 | if (list->mousedev->minor == MOUSEDEV_MIX) { |
| 424 | list_for_each_entry(handle, &mousedev_handler.h_list, h_node) { |
| 425 | mousedev = handle->private; |
| 426 | if (!mousedev->open && mousedev->exist) |
| 427 | input_open_device(handle); |
| 428 | } |
| 429 | } else |
| 430 | if (!mousedev_mix.open && list->mousedev->exist) |
| 431 | input_open_device(&list->mousedev->handle); |
| 432 | } |
| 433 | |
| 434 | return 0; |
| 435 | } |
| 436 | |
| 437 | static inline int mousedev_limit_delta(int delta, int limit) |
| 438 | { |
| 439 | return delta > limit ? limit : (delta < -limit ? -limit : delta); |
| 440 | } |
| 441 | |
| 442 | static void mousedev_packet(struct mousedev_list *list, signed char *ps2_data) |
| 443 | { |
| 444 | struct mousedev_motion *p; |
| 445 | unsigned long flags; |
| 446 | |
| 447 | spin_lock_irqsave(&list->packet_lock, flags); |
| 448 | p = &list->packets[list->tail]; |
| 449 | |
| 450 | ps2_data[0] = 0x08 | ((p->dx < 0) << 4) | ((p->dy < 0) << 5) | (p->buttons & 0x07); |
| 451 | ps2_data[1] = mousedev_limit_delta(p->dx, 127); |
| 452 | ps2_data[2] = mousedev_limit_delta(p->dy, 127); |
| 453 | p->dx -= ps2_data[1]; |
| 454 | p->dy -= ps2_data[2]; |
| 455 | |
| 456 | switch (list->mode) { |
| 457 | case MOUSEDEV_EMUL_EXPS: |
| 458 | ps2_data[3] = mousedev_limit_delta(p->dz, 7); |
| 459 | p->dz -= ps2_data[3]; |
| 460 | ps2_data[3] = (ps2_data[3] & 0x0f) | ((p->buttons & 0x18) << 1); |
| 461 | list->bufsiz = 4; |
| 462 | break; |
| 463 | |
| 464 | case MOUSEDEV_EMUL_IMPS: |
| 465 | ps2_data[0] |= ((p->buttons & 0x10) >> 3) | ((p->buttons & 0x08) >> 1); |
| 466 | ps2_data[3] = mousedev_limit_delta(p->dz, 127); |
| 467 | p->dz -= ps2_data[3]; |
| 468 | list->bufsiz = 4; |
| 469 | break; |
| 470 | |
| 471 | case MOUSEDEV_EMUL_PS2: |
| 472 | default: |
| 473 | ps2_data[0] |= ((p->buttons & 0x10) >> 3) | ((p->buttons & 0x08) >> 1); |
| 474 | p->dz = 0; |
| 475 | list->bufsiz = 3; |
| 476 | break; |
| 477 | } |
| 478 | |
| 479 | if (!p->dx && !p->dy && !p->dz) { |
| 480 | if (list->tail == list->head) |
| 481 | list->ready = 0; |
| 482 | else |
| 483 | list->tail = (list->tail + 1) % PACKET_QUEUE_LEN; |
| 484 | } |
| 485 | |
| 486 | spin_unlock_irqrestore(&list->packet_lock, flags); |
| 487 | } |
| 488 | |
| 489 | |
| 490 | static ssize_t mousedev_write(struct file * file, const char __user * buffer, size_t count, loff_t *ppos) |
| 491 | { |
| 492 | struct mousedev_list *list = file->private_data; |
| 493 | unsigned char c; |
| 494 | unsigned int i; |
| 495 | |
| 496 | for (i = 0; i < count; i++) { |
| 497 | |
| 498 | if (get_user(c, buffer + i)) |
| 499 | return -EFAULT; |
| 500 | |
| 501 | if (c == mousedev_imex_seq[list->imexseq]) { |
| 502 | if (++list->imexseq == MOUSEDEV_SEQ_LEN) { |
| 503 | list->imexseq = 0; |
| 504 | list->mode = MOUSEDEV_EMUL_EXPS; |
| 505 | } |
| 506 | } else list->imexseq = 0; |
| 507 | |
| 508 | if (c == mousedev_imps_seq[list->impsseq]) { |
| 509 | if (++list->impsseq == MOUSEDEV_SEQ_LEN) { |
| 510 | list->impsseq = 0; |
| 511 | list->mode = MOUSEDEV_EMUL_IMPS; |
| 512 | } |
| 513 | } else list->impsseq = 0; |
| 514 | |
| 515 | list->ps2[0] = 0xfa; |
| 516 | |
| 517 | switch (c) { |
| 518 | |
| 519 | case 0xeb: /* Poll */ |
| 520 | mousedev_packet(list, &list->ps2[1]); |
| 521 | list->bufsiz++; /* account for leading ACK */ |
| 522 | break; |
| 523 | |
| 524 | case 0xf2: /* Get ID */ |
| 525 | switch (list->mode) { |
| 526 | case MOUSEDEV_EMUL_PS2: list->ps2[1] = 0; break; |
| 527 | case MOUSEDEV_EMUL_IMPS: list->ps2[1] = 3; break; |
| 528 | case MOUSEDEV_EMUL_EXPS: list->ps2[1] = 4; break; |
| 529 | } |
| 530 | list->bufsiz = 2; |
| 531 | break; |
| 532 | |
| 533 | case 0xe9: /* Get info */ |
| 534 | list->ps2[1] = 0x60; list->ps2[2] = 3; list->ps2[3] = 200; |
| 535 | list->bufsiz = 4; |
| 536 | break; |
| 537 | |
| 538 | case 0xff: /* Reset */ |
| 539 | list->impsseq = list->imexseq = 0; |
| 540 | list->mode = MOUSEDEV_EMUL_PS2; |
| 541 | list->ps2[1] = 0xaa; list->ps2[2] = 0x00; |
| 542 | list->bufsiz = 3; |
| 543 | break; |
| 544 | |
| 545 | default: |
| 546 | list->bufsiz = 1; |
| 547 | break; |
| 548 | } |
| 549 | |
| 550 | list->buffer = list->bufsiz; |
| 551 | } |
| 552 | |
| 553 | kill_fasync(&list->fasync, SIGIO, POLL_IN); |
| 554 | |
| 555 | wake_up_interruptible(&list->mousedev->wait); |
| 556 | |
| 557 | return count; |
| 558 | } |
| 559 | |
| 560 | static ssize_t mousedev_read(struct file * file, char __user * buffer, size_t count, loff_t *ppos) |
| 561 | { |
| 562 | struct mousedev_list *list = file->private_data; |
| 563 | int retval = 0; |
| 564 | |
| 565 | if (!list->ready && !list->buffer && (file->f_flags & O_NONBLOCK)) |
| 566 | return -EAGAIN; |
| 567 | |
| 568 | retval = wait_event_interruptible(list->mousedev->wait, |
| 569 | !list->mousedev->exist || list->ready || list->buffer); |
| 570 | |
| 571 | if (retval) |
| 572 | return retval; |
| 573 | |
| 574 | if (!list->mousedev->exist) |
| 575 | return -ENODEV; |
| 576 | |
| 577 | if (!list->buffer && list->ready) { |
| 578 | mousedev_packet(list, list->ps2); |
| 579 | list->buffer = list->bufsiz; |
| 580 | } |
| 581 | |
| 582 | if (count > list->buffer) |
| 583 | count = list->buffer; |
| 584 | |
| 585 | list->buffer -= count; |
| 586 | |
| 587 | if (copy_to_user(buffer, list->ps2 + list->bufsiz - list->buffer - count, count)) |
| 588 | return -EFAULT; |
| 589 | |
| 590 | return count; |
| 591 | } |
| 592 | |
| 593 | /* No kernel lock - fine */ |
| 594 | static unsigned int mousedev_poll(struct file *file, poll_table *wait) |
| 595 | { |
| 596 | struct mousedev_list *list = file->private_data; |
| 597 | poll_wait(file, &list->mousedev->wait, wait); |
| 598 | return ((list->ready || list->buffer) ? (POLLIN | POLLRDNORM) : 0) | |
| 599 | (list->mousedev->exist ? 0 : (POLLHUP | POLLERR)); |
| 600 | } |
| 601 | |
| 602 | static struct file_operations mousedev_fops = { |
| 603 | .owner = THIS_MODULE, |
| 604 | .read = mousedev_read, |
| 605 | .write = mousedev_write, |
| 606 | .poll = mousedev_poll, |
| 607 | .open = mousedev_open, |
| 608 | .release = mousedev_release, |
| 609 | .fasync = mousedev_fasync, |
| 610 | }; |
| 611 | |
| 612 | static struct input_handle *mousedev_connect(struct input_handler *handler, struct input_dev *dev, struct input_device_id *id) |
| 613 | { |
| 614 | struct mousedev *mousedev; |
| 615 | int minor = 0; |
| 616 | |
| 617 | for (minor = 0; minor < MOUSEDEV_MINORS && mousedev_table[minor]; minor++); |
| 618 | if (minor == MOUSEDEV_MINORS) { |
| 619 | printk(KERN_ERR "mousedev: no more free mousedev devices\n"); |
| 620 | return NULL; |
| 621 | } |
| 622 | |
| 623 | if (!(mousedev = kmalloc(sizeof(struct mousedev), GFP_KERNEL))) |
| 624 | return NULL; |
| 625 | memset(mousedev, 0, sizeof(struct mousedev)); |
| 626 | |
| 627 | INIT_LIST_HEAD(&mousedev->list); |
| 628 | init_waitqueue_head(&mousedev->wait); |
| 629 | |
| 630 | mousedev->minor = minor; |
| 631 | mousedev->exist = 1; |
| 632 | mousedev->handle.dev = dev; |
| 633 | mousedev->handle.name = mousedev->name; |
| 634 | mousedev->handle.handler = handler; |
| 635 | mousedev->handle.private = mousedev; |
| 636 | sprintf(mousedev->name, "mouse%d", minor); |
| 637 | |
| 638 | if (mousedev_mix.open) |
| 639 | input_open_device(&mousedev->handle); |
| 640 | |
| 641 | mousedev_table[minor] = mousedev; |
| 642 | |
| 643 | devfs_mk_cdev(MKDEV(INPUT_MAJOR, MOUSEDEV_MINOR_BASE + minor), |
| 644 | S_IFCHR|S_IRUGO|S_IWUSR, "input/mouse%d", minor); |
| 645 | class_simple_device_add(input_class, |
| 646 | MKDEV(INPUT_MAJOR, MOUSEDEV_MINOR_BASE + minor), |
| 647 | dev->dev, "mouse%d", minor); |
| 648 | |
| 649 | return &mousedev->handle; |
| 650 | } |
| 651 | |
| 652 | static void mousedev_disconnect(struct input_handle *handle) |
| 653 | { |
| 654 | struct mousedev *mousedev = handle->private; |
| 655 | struct mousedev_list *list; |
| 656 | |
| 657 | class_simple_device_remove(MKDEV(INPUT_MAJOR, MOUSEDEV_MINOR_BASE + mousedev->minor)); |
| 658 | devfs_remove("input/mouse%d", mousedev->minor); |
| 659 | mousedev->exist = 0; |
| 660 | |
| 661 | if (mousedev->open) { |
| 662 | input_close_device(handle); |
| 663 | wake_up_interruptible(&mousedev->wait); |
| 664 | list_for_each_entry(list, &mousedev->list, node) |
| 665 | kill_fasync(&list->fasync, SIGIO, POLL_HUP); |
| 666 | } else { |
| 667 | if (mousedev_mix.open) |
| 668 | input_close_device(handle); |
| 669 | mousedev_free(mousedev); |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | static struct input_device_id mousedev_ids[] = { |
| 674 | { |
| 675 | .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_RELBIT, |
| 676 | .evbit = { BIT(EV_KEY) | BIT(EV_REL) }, |
| 677 | .keybit = { [LONG(BTN_LEFT)] = BIT(BTN_LEFT) }, |
| 678 | .relbit = { BIT(REL_X) | BIT(REL_Y) }, |
| 679 | }, /* A mouse like device, at least one button, two relative axes */ |
| 680 | { |
| 681 | .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_RELBIT, |
| 682 | .evbit = { BIT(EV_KEY) | BIT(EV_REL) }, |
| 683 | .relbit = { BIT(REL_WHEEL) }, |
| 684 | }, /* A separate scrollwheel */ |
| 685 | { |
| 686 | .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_ABSBIT, |
| 687 | .evbit = { BIT(EV_KEY) | BIT(EV_ABS) }, |
| 688 | .keybit = { [LONG(BTN_TOUCH)] = BIT(BTN_TOUCH) }, |
| 689 | .absbit = { BIT(ABS_X) | BIT(ABS_Y) }, |
| 690 | }, /* A tablet like device, at least touch detection, two absolute axes */ |
| 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_TOOL_FINGER)] = BIT(BTN_TOOL_FINGER) }, |
| 695 | .absbit = { BIT(ABS_X) | BIT(ABS_Y) | BIT(ABS_PRESSURE) | BIT(ABS_TOOL_WIDTH) }, |
| 696 | }, /* A touchpad */ |
| 697 | |
| 698 | { }, /* Terminating entry */ |
| 699 | }; |
| 700 | |
| 701 | MODULE_DEVICE_TABLE(input, mousedev_ids); |
| 702 | |
| 703 | static struct input_handler mousedev_handler = { |
| 704 | .event = mousedev_event, |
| 705 | .connect = mousedev_connect, |
| 706 | .disconnect = mousedev_disconnect, |
| 707 | .fops = &mousedev_fops, |
| 708 | .minor = MOUSEDEV_MINOR_BASE, |
| 709 | .name = "mousedev", |
| 710 | .id_table = mousedev_ids, |
| 711 | }; |
| 712 | |
| 713 | #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX |
| 714 | static struct miscdevice psaux_mouse = { |
| 715 | PSMOUSE_MINOR, "psaux", &mousedev_fops |
| 716 | }; |
| 717 | static int psaux_registered; |
| 718 | #endif |
| 719 | |
| 720 | static int __init mousedev_init(void) |
| 721 | { |
| 722 | input_register_handler(&mousedev_handler); |
| 723 | |
| 724 | memset(&mousedev_mix, 0, sizeof(struct mousedev)); |
| 725 | INIT_LIST_HEAD(&mousedev_mix.list); |
| 726 | init_waitqueue_head(&mousedev_mix.wait); |
| 727 | mousedev_table[MOUSEDEV_MIX] = &mousedev_mix; |
| 728 | mousedev_mix.exist = 1; |
| 729 | mousedev_mix.minor = MOUSEDEV_MIX; |
| 730 | |
| 731 | devfs_mk_cdev(MKDEV(INPUT_MAJOR, MOUSEDEV_MINOR_BASE + MOUSEDEV_MIX), |
| 732 | S_IFCHR|S_IRUGO|S_IWUSR, "input/mice"); |
| 733 | class_simple_device_add(input_class, MKDEV(INPUT_MAJOR, MOUSEDEV_MINOR_BASE + MOUSEDEV_MIX), |
| 734 | NULL, "mice"); |
| 735 | |
| 736 | #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX |
| 737 | if (!(psaux_registered = !misc_register(&psaux_mouse))) |
| 738 | printk(KERN_WARNING "mice: could not misc_register the device\n"); |
| 739 | #endif |
| 740 | |
| 741 | printk(KERN_INFO "mice: PS/2 mouse device common for all mice\n"); |
| 742 | |
| 743 | return 0; |
| 744 | } |
| 745 | |
| 746 | static void __exit mousedev_exit(void) |
| 747 | { |
| 748 | #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX |
| 749 | if (psaux_registered) |
| 750 | misc_deregister(&psaux_mouse); |
| 751 | #endif |
| 752 | devfs_remove("input/mice"); |
| 753 | class_simple_device_remove(MKDEV(INPUT_MAJOR, MOUSEDEV_MINOR_BASE + MOUSEDEV_MIX)); |
| 754 | input_unregister_handler(&mousedev_handler); |
| 755 | } |
| 756 | |
| 757 | module_init(mousedev_init); |
| 758 | module_exit(mousedev_exit); |