]>
Commit | Line | Data |
---|---|---|
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/smp_lock.h> | |
23 | #include <linux/random.h> | |
24 | #include <linux/major.h> | |
25 | #include <linux/device.h> | |
26 | #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX | |
27 | #include <linux/miscdevice.h> | |
28 | #endif | |
29 | ||
30 | MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>"); | |
31 | MODULE_DESCRIPTION("Mouse (ExplorerPS/2) device interfaces"); | |
32 | MODULE_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 | ||
41 | static int xres = CONFIG_INPUT_MOUSEDEV_SCREEN_X; | |
42 | module_param(xres, uint, 0644); | |
43 | MODULE_PARM_DESC(xres, "Horizontal screen resolution"); | |
44 | ||
45 | static int yres = CONFIG_INPUT_MOUSEDEV_SCREEN_Y; | |
46 | module_param(yres, uint, 0644); | |
47 | MODULE_PARM_DESC(yres, "Vertical screen resolution"); | |
48 | ||
49 | static unsigned tap_time = 200; | |
50 | module_param(tap_time, uint, 0644); | |
51 | MODULE_PARM_DESC(tap_time, "Tap time for touchpads in absolute mode (msecs)"); | |
52 | ||
53 | struct mousedev_hw_data { | |
54 | int dx, dy, dz; | |
55 | int x, y; | |
56 | int abs_event; | |
57 | unsigned long buttons; | |
58 | }; | |
59 | ||
60 | struct mousedev { | |
61 | int exist; | |
62 | int open; | |
63 | int minor; | |
64 | char name[16]; | |
65 | wait_queue_head_t wait; | |
66 | struct list_head list; | |
67 | 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 | ||
76 | enum mousedev_emul { | |
77 | MOUSEDEV_EMUL_PS2, | |
78 | MOUSEDEV_EMUL_IMPS, | |
79 | MOUSEDEV_EMUL_EXPS | |
80 | }; | |
81 | ||
82 | struct mousedev_motion { | |
83 | int dx, dy, dz; | |
84 | unsigned long buttons; | |
85 | }; | |
86 | ||
87 | #define PACKET_QUEUE_LEN 16 | |
88 | struct mousedev_list { | |
89 | 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; | |
102 | unsigned long last_buttons; | |
103 | }; | |
104 | ||
105 | #define MOUSEDEV_SEQ_LEN 6 | |
106 | ||
107 | static unsigned char mousedev_imps_seq[] = { 0xf3, 200, 0xf3, 100, 0xf3, 80 }; | |
108 | static unsigned char mousedev_imex_seq[] = { 0xf3, 200, 0xf3, 200, 0xf3, 80 }; | |
109 | ||
110 | static struct input_handler mousedev_handler; | |
111 | ||
112 | static struct mousedev *mousedev_table[MOUSEDEV_MINORS]; | |
113 | static 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 | ||
118 | static 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]; | |
125 | if (size == 0) | |
126 | size = 256 * 2; | |
127 | ||
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) | |
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]; | |
165 | 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]; | |
171 | 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]; | |
177 | mousedev->packet.y = yres - ((value - dev->absmin[ABS_Y]) * yres) / size; | |
178 | mousedev->packet.abs_event = 1; | |
179 | break; | |
180 | } | |
181 | } | |
182 | ||
183 | static 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 | ||
192 | static 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: | |
199 | 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: | |
204 | case BTN_FORWARD: | |
205 | 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; | |
212 | default: return; | |
213 | } | |
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 | ||
224 | static void mousedev_notify_readers(struct mousedev *mousedev, struct mousedev_hw_data *packet) | |
225 | { | |
226 | struct mousedev_list *list; | |
227 | struct mousedev_motion *p; | |
228 | unsigned long flags; | |
229 | int wake_readers = 0; | |
230 | ||
231 | list_for_each_entry(list, &mousedev->list, node) { | |
232 | spin_lock_irqsave(&list->packet_lock, flags); | |
233 | ||
234 | p = &list->packets[list->head]; | |
235 | if (list->ready && p->buttons != mousedev->packet.buttons) { | |
236 | unsigned int new_head = (list->head + 1) % PACKET_QUEUE_LEN; | |
237 | if (new_head != list->tail) { | |
238 | p = &list->packets[list->head = new_head]; | |
239 | memset(p, 0, sizeof(struct mousedev_motion)); | |
240 | } | |
241 | } | |
242 | ||
243 | if (packet->abs_event) { | |
244 | p->dx += packet->x - list->pos_x; | |
245 | p->dy += packet->y - list->pos_y; | |
246 | list->pos_x = packet->x; | |
247 | list->pos_y = packet->y; | |
248 | } | |
249 | ||
250 | list->pos_x += packet->dx; | |
251 | list->pos_x = list->pos_x < 0 ? 0 : (list->pos_x >= xres ? xres : list->pos_x); | |
252 | list->pos_y += packet->dy; | |
253 | list->pos_y = list->pos_y < 0 ? 0 : (list->pos_y >= yres ? yres : list->pos_y); | |
254 | ||
255 | p->dx += packet->dx; | |
256 | p->dy += packet->dy; | |
257 | p->dz += packet->dz; | |
258 | p->buttons = mousedev->packet.buttons; | |
259 | ||
260 | if (p->dx || p->dy || p->dz || p->buttons != list->last_buttons) | |
261 | list->ready = 1; | |
262 | ||
263 | spin_unlock_irqrestore(&list->packet_lock, flags); | |
264 | ||
265 | if (list->ready) { | |
266 | kill_fasync(&list->fasync, SIGIO, POLL_IN); | |
267 | wake_readers = 1; | |
268 | } | |
269 | } | |
270 | ||
271 | if (wake_readers) | |
272 | wake_up_interruptible(&mousedev->wait); | |
273 | } | |
274 | ||
275 | static 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; | |
295 | ||
296 | } else if (!mousedev->touch) | |
297 | mousedev->touch = jiffies; | |
298 | } | |
299 | ||
300 | static 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 | |
336 | */ | |
337 | 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 | ||
351 | static int mousedev_fasync(int fd, struct file *file, int on) | |
352 | { | |
353 | int retval; | |
354 | struct mousedev_list *list = file->private_data; | |
355 | ||
356 | retval = fasync_helper(fd, file, on, &list->fasync); | |
357 | ||
358 | return retval < 0 ? retval : 0; | |
359 | } | |
360 | ||
361 | static void mousedev_free(struct mousedev *mousedev) | |
362 | { | |
363 | mousedev_table[mousedev->minor] = NULL; | |
364 | kfree(mousedev); | |
365 | } | |
366 | ||
367 | static void mixdev_release(void) | |
368 | { | |
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 | } | |
381 | } | |
382 | ||
383 | static int mousedev_release(struct inode * inode, struct file * file) | |
384 | { | |
385 | struct mousedev_list *list = file->private_data; | |
386 | ||
387 | mousedev_fasync(-1, file, 0); | |
388 | ||
389 | list_del(&list->node); | |
390 | ||
391 | if (!--list->mousedev->open) { | |
392 | if (list->mousedev->minor == MOUSEDEV_MIX) | |
393 | mixdev_release(); | |
394 | else if (!mousedev_mix.open) { | |
395 | if (list->mousedev->exist) | |
396 | input_close_device(&list->mousedev->handle); | |
397 | else | |
398 | mousedev_free(list->mousedev); | |
399 | } | |
400 | } | |
401 | ||
402 | kfree(list); | |
403 | return 0; | |
404 | } | |
405 | ||
406 | static int mousedev_open(struct inode * inode, struct file * file) | |
407 | { | |
408 | struct mousedev_list *list; | |
409 | struct input_handle *handle; | |
410 | struct mousedev *mousedev; | |
411 | int i; | |
412 | ||
413 | #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX | |
414 | if (imajor(inode) == MISC_MAJOR) | |
415 | i = MOUSEDEV_MIX; | |
416 | else | |
417 | #endif | |
418 | i = iminor(inode) - MOUSEDEV_MINOR_BASE; | |
419 | ||
420 | if (i >= MOUSEDEV_MINORS || !mousedev_table[i]) | |
421 | return -ENODEV; | |
422 | ||
423 | if (!(list = kzalloc(sizeof(struct mousedev_list), GFP_KERNEL))) | |
424 | return -ENOMEM; | |
425 | ||
426 | spin_lock_init(&list->packet_lock); | |
427 | list->pos_x = xres / 2; | |
428 | list->pos_y = yres / 2; | |
429 | list->mousedev = mousedev_table[i]; | |
430 | list_add_tail(&list->node, &mousedev_table[i]->list); | |
431 | file->private_data = list; | |
432 | ||
433 | if (!list->mousedev->open++) { | |
434 | if (list->mousedev->minor == MOUSEDEV_MIX) { | |
435 | list_for_each_entry(handle, &mousedev_handler.h_list, h_node) { | |
436 | mousedev = handle->private; | |
437 | if (!mousedev->open && mousedev->exist) | |
438 | input_open_device(handle); | |
439 | } | |
440 | } else | |
441 | if (!mousedev_mix.open && list->mousedev->exist) | |
442 | input_open_device(&list->mousedev->handle); | |
443 | } | |
444 | ||
445 | return 0; | |
446 | } | |
447 | ||
448 | static inline int mousedev_limit_delta(int delta, int limit) | |
449 | { | |
450 | return delta > limit ? limit : (delta < -limit ? -limit : delta); | |
451 | } | |
452 | ||
453 | static void mousedev_packet(struct mousedev_list *list, signed char *ps2_data) | |
454 | { | |
455 | struct mousedev_motion *p; | |
456 | unsigned long flags; | |
457 | ||
458 | spin_lock_irqsave(&list->packet_lock, flags); | |
459 | p = &list->packets[list->tail]; | |
460 | ||
461 | ps2_data[0] = 0x08 | ((p->dx < 0) << 4) | ((p->dy < 0) << 5) | (p->buttons & 0x07); | |
462 | ps2_data[1] = mousedev_limit_delta(p->dx, 127); | |
463 | ps2_data[2] = mousedev_limit_delta(p->dy, 127); | |
464 | p->dx -= ps2_data[1]; | |
465 | p->dy -= ps2_data[2]; | |
466 | ||
467 | switch (list->mode) { | |
468 | case MOUSEDEV_EMUL_EXPS: | |
469 | ps2_data[3] = mousedev_limit_delta(p->dz, 7); | |
470 | p->dz -= ps2_data[3]; | |
471 | ps2_data[3] = (ps2_data[3] & 0x0f) | ((p->buttons & 0x18) << 1); | |
472 | list->bufsiz = 4; | |
473 | break; | |
474 | ||
475 | case MOUSEDEV_EMUL_IMPS: | |
476 | ps2_data[0] |= ((p->buttons & 0x10) >> 3) | ((p->buttons & 0x08) >> 1); | |
477 | ps2_data[3] = mousedev_limit_delta(p->dz, 127); | |
478 | p->dz -= ps2_data[3]; | |
479 | list->bufsiz = 4; | |
480 | break; | |
481 | ||
482 | case MOUSEDEV_EMUL_PS2: | |
483 | default: | |
484 | ps2_data[0] |= ((p->buttons & 0x10) >> 3) | ((p->buttons & 0x08) >> 1); | |
485 | p->dz = 0; | |
486 | list->bufsiz = 3; | |
487 | break; | |
488 | } | |
489 | ||
490 | if (!p->dx && !p->dy && !p->dz) { | |
491 | if (list->tail == list->head) { | |
492 | list->ready = 0; | |
493 | list->last_buttons = p->buttons; | |
494 | } else | |
495 | list->tail = (list->tail + 1) % PACKET_QUEUE_LEN; | |
496 | } | |
497 | ||
498 | spin_unlock_irqrestore(&list->packet_lock, flags); | |
499 | } | |
500 | ||
501 | ||
502 | static ssize_t mousedev_write(struct file * file, const char __user * buffer, size_t count, loff_t *ppos) | |
503 | { | |
504 | struct mousedev_list *list = file->private_data; | |
505 | unsigned char c; | |
506 | unsigned int i; | |
507 | ||
508 | for (i = 0; i < count; i++) { | |
509 | ||
510 | if (get_user(c, buffer + i)) | |
511 | return -EFAULT; | |
512 | ||
513 | if (c == mousedev_imex_seq[list->imexseq]) { | |
514 | if (++list->imexseq == MOUSEDEV_SEQ_LEN) { | |
515 | list->imexseq = 0; | |
516 | list->mode = MOUSEDEV_EMUL_EXPS; | |
517 | } | |
518 | } else | |
519 | list->imexseq = 0; | |
520 | ||
521 | if (c == mousedev_imps_seq[list->impsseq]) { | |
522 | if (++list->impsseq == MOUSEDEV_SEQ_LEN) { | |
523 | list->impsseq = 0; | |
524 | list->mode = MOUSEDEV_EMUL_IMPS; | |
525 | } | |
526 | } else | |
527 | list->impsseq = 0; | |
528 | ||
529 | list->ps2[0] = 0xfa; | |
530 | ||
531 | switch (c) { | |
532 | ||
533 | case 0xeb: /* Poll */ | |
534 | mousedev_packet(list, &list->ps2[1]); | |
535 | list->bufsiz++; /* account for leading ACK */ | |
536 | break; | |
537 | ||
538 | case 0xf2: /* Get ID */ | |
539 | switch (list->mode) { | |
540 | case MOUSEDEV_EMUL_PS2: list->ps2[1] = 0; break; | |
541 | case MOUSEDEV_EMUL_IMPS: list->ps2[1] = 3; break; | |
542 | case MOUSEDEV_EMUL_EXPS: list->ps2[1] = 4; break; | |
543 | } | |
544 | list->bufsiz = 2; | |
545 | break; | |
546 | ||
547 | case 0xe9: /* Get info */ | |
548 | list->ps2[1] = 0x60; list->ps2[2] = 3; list->ps2[3] = 200; | |
549 | list->bufsiz = 4; | |
550 | break; | |
551 | ||
552 | case 0xff: /* Reset */ | |
553 | list->impsseq = list->imexseq = 0; | |
554 | list->mode = MOUSEDEV_EMUL_PS2; | |
555 | list->ps2[1] = 0xaa; list->ps2[2] = 0x00; | |
556 | list->bufsiz = 3; | |
557 | break; | |
558 | ||
559 | default: | |
560 | list->bufsiz = 1; | |
561 | break; | |
562 | } | |
563 | ||
564 | list->buffer = list->bufsiz; | |
565 | } | |
566 | ||
567 | kill_fasync(&list->fasync, SIGIO, POLL_IN); | |
568 | ||
569 | wake_up_interruptible(&list->mousedev->wait); | |
570 | ||
571 | return count; | |
572 | } | |
573 | ||
574 | static ssize_t mousedev_read(struct file * file, char __user * buffer, size_t count, loff_t *ppos) | |
575 | { | |
576 | struct mousedev_list *list = file->private_data; | |
577 | int retval = 0; | |
578 | ||
579 | if (!list->ready && !list->buffer && (file->f_flags & O_NONBLOCK)) | |
580 | return -EAGAIN; | |
581 | ||
582 | retval = wait_event_interruptible(list->mousedev->wait, | |
583 | !list->mousedev->exist || list->ready || list->buffer); | |
584 | ||
585 | if (retval) | |
586 | return retval; | |
587 | ||
588 | if (!list->mousedev->exist) | |
589 | return -ENODEV; | |
590 | ||
591 | if (!list->buffer && list->ready) { | |
592 | mousedev_packet(list, list->ps2); | |
593 | list->buffer = list->bufsiz; | |
594 | } | |
595 | ||
596 | if (count > list->buffer) | |
597 | count = list->buffer; | |
598 | ||
599 | list->buffer -= count; | |
600 | ||
601 | if (copy_to_user(buffer, list->ps2 + list->bufsiz - list->buffer - count, count)) | |
602 | return -EFAULT; | |
603 | ||
604 | return count; | |
605 | } | |
606 | ||
607 | /* No kernel lock - fine */ | |
608 | static unsigned int mousedev_poll(struct file *file, poll_table *wait) | |
609 | { | |
610 | struct mousedev_list *list = file->private_data; | |
611 | ||
612 | poll_wait(file, &list->mousedev->wait, wait); | |
613 | return ((list->ready || list->buffer) ? (POLLIN | POLLRDNORM) : 0) | | |
614 | (list->mousedev->exist ? 0 : (POLLHUP | POLLERR)); | |
615 | } | |
616 | ||
617 | static const struct file_operations mousedev_fops = { | |
618 | .owner = THIS_MODULE, | |
619 | .read = mousedev_read, | |
620 | .write = mousedev_write, | |
621 | .poll = mousedev_poll, | |
622 | .open = mousedev_open, | |
623 | .release = mousedev_release, | |
624 | .fasync = mousedev_fasync, | |
625 | }; | |
626 | ||
627 | static int mousedev_connect(struct input_handler *handler, struct input_dev *dev, | |
628 | const struct input_device_id *id) | |
629 | { | |
630 | struct mousedev *mousedev; | |
631 | struct class_device *cdev; | |
632 | dev_t devt; | |
633 | int minor; | |
634 | int error; | |
635 | ||
636 | for (minor = 0; minor < MOUSEDEV_MINORS && mousedev_table[minor]; minor++); | |
637 | if (minor == MOUSEDEV_MINORS) { | |
638 | printk(KERN_ERR "mousedev: no more free mousedev devices\n"); | |
639 | return -ENFILE; | |
640 | } | |
641 | ||
642 | mousedev = kzalloc(sizeof(struct mousedev), GFP_KERNEL); | |
643 | if (!mousedev) | |
644 | return -ENOMEM; | |
645 | ||
646 | INIT_LIST_HEAD(&mousedev->list); | |
647 | INIT_LIST_HEAD(&mousedev->mixdev_node); | |
648 | init_waitqueue_head(&mousedev->wait); | |
649 | ||
650 | mousedev->minor = minor; | |
651 | mousedev->exist = 1; | |
652 | mousedev->handle.dev = dev; | |
653 | mousedev->handle.name = mousedev->name; | |
654 | mousedev->handle.handler = handler; | |
655 | mousedev->handle.private = mousedev; | |
656 | sprintf(mousedev->name, "mouse%d", minor); | |
657 | ||
658 | mousedev_table[minor] = mousedev; | |
659 | ||
660 | devt = MKDEV(INPUT_MAJOR, MOUSEDEV_MINOR_BASE + minor), | |
661 | ||
662 | cdev = class_device_create(&input_class, &dev->cdev, devt, | |
663 | dev->cdev.dev, mousedev->name); | |
664 | if (IS_ERR(cdev)) { | |
665 | error = PTR_ERR(cdev); | |
666 | goto err_free_mousedev; | |
667 | } | |
668 | ||
669 | /* temporary symlink to keep userspace happy */ | |
670 | error = sysfs_create_link(&input_class.subsys.kset.kobj, | |
671 | &cdev->kobj, mousedev->name); | |
672 | if (error) | |
673 | goto err_cdev_destroy; | |
674 | ||
675 | error = input_register_handle(&mousedev->handle); | |
676 | if (error) | |
677 | goto err_remove_link; | |
678 | ||
679 | if (mousedev_mix.open) { | |
680 | error = input_open_device(&mousedev->handle); | |
681 | if (error) | |
682 | goto err_unregister_handle; | |
683 | } | |
684 | ||
685 | return 0; | |
686 | ||
687 | err_unregister_handle: | |
688 | input_unregister_handle(&mousedev->handle); | |
689 | err_remove_link: | |
690 | sysfs_remove_link(&input_class.subsys.kset.kobj, mousedev->name); | |
691 | err_cdev_destroy: | |
692 | class_device_destroy(&input_class, devt); | |
693 | err_free_mousedev: | |
694 | mousedev_table[minor] = NULL; | |
695 | kfree(mousedev); | |
696 | return error; | |
697 | } | |
698 | ||
699 | static void mousedev_disconnect(struct input_handle *handle) | |
700 | { | |
701 | struct mousedev *mousedev = handle->private; | |
702 | struct mousedev_list *list; | |
703 | ||
704 | input_unregister_handle(handle); | |
705 | ||
706 | sysfs_remove_link(&input_class.subsys.kset.kobj, mousedev->name); | |
707 | class_device_destroy(&input_class, | |
708 | MKDEV(INPUT_MAJOR, MOUSEDEV_MINOR_BASE + mousedev->minor)); | |
709 | mousedev->exist = 0; | |
710 | ||
711 | if (mousedev->open) { | |
712 | input_close_device(handle); | |
713 | wake_up_interruptible(&mousedev->wait); | |
714 | list_for_each_entry(list, &mousedev->list, node) | |
715 | kill_fasync(&list->fasync, SIGIO, POLL_HUP); | |
716 | } else { | |
717 | if (mousedev_mix.open) | |
718 | input_close_device(handle); | |
719 | mousedev_free(mousedev); | |
720 | } | |
721 | } | |
722 | ||
723 | static const struct input_device_id mousedev_ids[] = { | |
724 | { | |
725 | .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_RELBIT, | |
726 | .evbit = { BIT(EV_KEY) | BIT(EV_REL) }, | |
727 | .keybit = { [LONG(BTN_LEFT)] = BIT(BTN_LEFT) }, | |
728 | .relbit = { BIT(REL_X) | BIT(REL_Y) }, | |
729 | }, /* A mouse like device, at least one button, two relative axes */ | |
730 | { | |
731 | .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_RELBIT, | |
732 | .evbit = { BIT(EV_KEY) | BIT(EV_REL) }, | |
733 | .relbit = { BIT(REL_WHEEL) }, | |
734 | }, /* A separate scrollwheel */ | |
735 | { | |
736 | .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_ABSBIT, | |
737 | .evbit = { BIT(EV_KEY) | BIT(EV_ABS) }, | |
738 | .keybit = { [LONG(BTN_TOUCH)] = BIT(BTN_TOUCH) }, | |
739 | .absbit = { BIT(ABS_X) | BIT(ABS_Y) }, | |
740 | }, /* A tablet like device, at least touch detection, two absolute axes */ | |
741 | { | |
742 | .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_ABSBIT, | |
743 | .evbit = { BIT(EV_KEY) | BIT(EV_ABS) }, | |
744 | .keybit = { [LONG(BTN_TOOL_FINGER)] = BIT(BTN_TOOL_FINGER) }, | |
745 | .absbit = { BIT(ABS_X) | BIT(ABS_Y) | BIT(ABS_PRESSURE) | BIT(ABS_TOOL_WIDTH) }, | |
746 | }, /* A touchpad */ | |
747 | ||
748 | { }, /* Terminating entry */ | |
749 | }; | |
750 | ||
751 | MODULE_DEVICE_TABLE(input, mousedev_ids); | |
752 | ||
753 | static struct input_handler mousedev_handler = { | |
754 | .event = mousedev_event, | |
755 | .connect = mousedev_connect, | |
756 | .disconnect = mousedev_disconnect, | |
757 | .fops = &mousedev_fops, | |
758 | .minor = MOUSEDEV_MINOR_BASE, | |
759 | .name = "mousedev", | |
760 | .id_table = mousedev_ids, | |
761 | }; | |
762 | ||
763 | #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX | |
764 | static struct miscdevice psaux_mouse = { | |
765 | PSMOUSE_MINOR, "psaux", &mousedev_fops | |
766 | }; | |
767 | static int psaux_registered; | |
768 | #endif | |
769 | ||
770 | static int __init mousedev_init(void) | |
771 | { | |
772 | struct class_device *cdev; | |
773 | int error; | |
774 | ||
775 | error = input_register_handler(&mousedev_handler); | |
776 | if (error) | |
777 | return error; | |
778 | ||
779 | memset(&mousedev_mix, 0, sizeof(struct mousedev)); | |
780 | INIT_LIST_HEAD(&mousedev_mix.list); | |
781 | init_waitqueue_head(&mousedev_mix.wait); | |
782 | mousedev_table[MOUSEDEV_MIX] = &mousedev_mix; | |
783 | mousedev_mix.exist = 1; | |
784 | mousedev_mix.minor = MOUSEDEV_MIX; | |
785 | ||
786 | cdev = class_device_create(&input_class, NULL, | |
787 | MKDEV(INPUT_MAJOR, MOUSEDEV_MINOR_BASE + MOUSEDEV_MIX), NULL, "mice"); | |
788 | if (IS_ERR(cdev)) { | |
789 | input_unregister_handler(&mousedev_handler); | |
790 | return PTR_ERR(cdev); | |
791 | } | |
792 | ||
793 | #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX | |
794 | error = misc_register(&psaux_mouse); | |
795 | if (error) | |
796 | printk(KERN_WARNING "mice: could not register psaux device, " | |
797 | "error: %d\n", error); | |
798 | else | |
799 | psaux_registered = 1; | |
800 | #endif | |
801 | ||
802 | printk(KERN_INFO "mice: PS/2 mouse device common for all mice\n"); | |
803 | ||
804 | return 0; | |
805 | } | |
806 | ||
807 | static void __exit mousedev_exit(void) | |
808 | { | |
809 | #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX | |
810 | if (psaux_registered) | |
811 | misc_deregister(&psaux_mouse); | |
812 | #endif | |
813 | class_device_destroy(&input_class, | |
814 | MKDEV(INPUT_MAJOR, MOUSEDEV_MINOR_BASE + MOUSEDEV_MIX)); | |
815 | input_unregister_handler(&mousedev_handler); | |
816 | } | |
817 | ||
818 | module_init(mousedev_init); | |
819 | module_exit(mousedev_exit); |