xref: /openbmc/linux/drivers/input/mousedev.c (revision 64c70b1c)
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/random.h>
23 #include <linux/major.h>
24 #include <linux/device.h>
25 #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
26 #include <linux/miscdevice.h>
27 #endif
28 
29 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
30 MODULE_DESCRIPTION("Mouse (ExplorerPS/2) device interfaces");
31 MODULE_LICENSE("GPL");
32 
33 #ifndef CONFIG_INPUT_MOUSEDEV_SCREEN_X
34 #define CONFIG_INPUT_MOUSEDEV_SCREEN_X	1024
35 #endif
36 #ifndef CONFIG_INPUT_MOUSEDEV_SCREEN_Y
37 #define CONFIG_INPUT_MOUSEDEV_SCREEN_Y	768
38 #endif
39 
40 static int xres = CONFIG_INPUT_MOUSEDEV_SCREEN_X;
41 module_param(xres, uint, 0644);
42 MODULE_PARM_DESC(xres, "Horizontal screen resolution");
43 
44 static int yres = CONFIG_INPUT_MOUSEDEV_SCREEN_Y;
45 module_param(yres, uint, 0644);
46 MODULE_PARM_DESC(yres, "Vertical screen resolution");
47 
48 static unsigned tap_time = 200;
49 module_param(tap_time, uint, 0644);
50 MODULE_PARM_DESC(tap_time, "Tap time for touchpads in absolute mode (msecs)");
51 
52 struct mousedev_hw_data {
53 	int dx, dy, dz;
54 	int x, y;
55 	int abs_event;
56 	unsigned long buttons;
57 };
58 
59 struct mousedev {
60 	int exist;
61 	int open;
62 	int minor;
63 	char name[16];
64 	wait_queue_head_t wait;
65 	struct list_head client_list;
66 	struct input_handle handle;
67 	struct device dev;
68 
69 	struct list_head mixdev_node;
70 	int mixdev_open;
71 
72 	struct mousedev_hw_data packet;
73 	unsigned int pkt_count;
74 	int old_x[4], old_y[4];
75 	int frac_dx, frac_dy;
76 	unsigned long touch;
77 };
78 
79 enum mousedev_emul {
80 	MOUSEDEV_EMUL_PS2,
81 	MOUSEDEV_EMUL_IMPS,
82 	MOUSEDEV_EMUL_EXPS
83 };
84 
85 struct mousedev_motion {
86 	int dx, dy, dz;
87 	unsigned long buttons;
88 };
89 
90 #define PACKET_QUEUE_LEN	16
91 struct mousedev_client {
92 	struct fasync_struct *fasync;
93 	struct mousedev *mousedev;
94 	struct list_head node;
95 
96 	struct mousedev_motion packets[PACKET_QUEUE_LEN];
97 	unsigned int head, tail;
98 	spinlock_t packet_lock;
99 	int pos_x, pos_y;
100 
101 	signed char ps2[6];
102 	unsigned char ready, buffer, bufsiz;
103 	unsigned char imexseq, impsseq;
104 	enum mousedev_emul mode;
105 	unsigned long last_buttons;
106 };
107 
108 #define MOUSEDEV_SEQ_LEN	6
109 
110 static unsigned char mousedev_imps_seq[] = { 0xf3, 200, 0xf3, 100, 0xf3, 80 };
111 static unsigned char mousedev_imex_seq[] = { 0xf3, 200, 0xf3, 200, 0xf3, 80 };
112 
113 static struct input_handler mousedev_handler;
114 
115 static struct mousedev *mousedev_table[MOUSEDEV_MINORS];
116 static struct mousedev *mousedev_mix;
117 static LIST_HEAD(mousedev_mix_list);
118 
119 #define fx(i)  (mousedev->old_x[(mousedev->pkt_count - (i)) & 03])
120 #define fy(i)  (mousedev->old_y[(mousedev->pkt_count - (i)) & 03])
121 
122 static void mousedev_touchpad_event(struct input_dev *dev, struct mousedev *mousedev, unsigned int code, int value)
123 {
124 	int size, tmp;
125 	enum { FRACTION_DENOM = 128 };
126 
127 	switch (code) {
128 		case ABS_X:
129 			fx(0) = value;
130 			if (mousedev->touch && mousedev->pkt_count >= 2) {
131 				size = dev->absmax[ABS_X] - dev->absmin[ABS_X];
132 				if (size == 0)
133 					size = 256 * 2;
134 				tmp = ((value - fx(2)) * (256 * FRACTION_DENOM)) / size;
135 				tmp += mousedev->frac_dx;
136 				mousedev->packet.dx = tmp / FRACTION_DENOM;
137 				mousedev->frac_dx = tmp - mousedev->packet.dx * FRACTION_DENOM;
138 			}
139 			break;
140 
141 		case ABS_Y:
142 			fy(0) = value;
143 			if (mousedev->touch && mousedev->pkt_count >= 2) {
144 				/* use X size to keep the same scale */
145 				size = dev->absmax[ABS_X] - dev->absmin[ABS_X];
146 				if (size == 0)
147 					size = 256 * 2;
148 				tmp = -((value - fy(2)) * (256 * FRACTION_DENOM)) / size;
149 				tmp += mousedev->frac_dy;
150 				mousedev->packet.dy = tmp / FRACTION_DENOM;
151 				mousedev->frac_dy = tmp - mousedev->packet.dy * FRACTION_DENOM;
152 			}
153 			break;
154 	}
155 }
156 
157 static void mousedev_abs_event(struct input_dev *dev, struct mousedev *mousedev, unsigned int code, int value)
158 {
159 	int size;
160 
161 	switch (code) {
162 		case ABS_X:
163 			size = dev->absmax[ABS_X] - dev->absmin[ABS_X];
164 			if (size == 0)
165 				size = xres ? : 1;
166 			if (value > dev->absmax[ABS_X])
167 				value = dev->absmax[ABS_X];
168 			if (value < dev->absmin[ABS_X])
169 				value = dev->absmin[ABS_X];
170 			mousedev->packet.x = ((value - dev->absmin[ABS_X]) * xres) / size;
171 			mousedev->packet.abs_event = 1;
172 			break;
173 
174 		case ABS_Y:
175 			size = dev->absmax[ABS_Y] - dev->absmin[ABS_Y];
176 			if (size == 0)
177 				size = yres ? : 1;
178 			if (value > dev->absmax[ABS_Y])
179 				value = dev->absmax[ABS_Y];
180 			if (value < dev->absmin[ABS_Y])
181 				value = dev->absmin[ABS_Y];
182 			mousedev->packet.y = yres - ((value - dev->absmin[ABS_Y]) * yres) / size;
183 			mousedev->packet.abs_event = 1;
184 			break;
185 	}
186 }
187 
188 static void mousedev_rel_event(struct mousedev *mousedev, unsigned int code, int value)
189 {
190 	switch (code) {
191 		case REL_X:	mousedev->packet.dx += value; break;
192 		case REL_Y:	mousedev->packet.dy -= value; break;
193 		case REL_WHEEL:	mousedev->packet.dz -= value; break;
194 	}
195 }
196 
197 static void mousedev_key_event(struct mousedev *mousedev, unsigned int code, int value)
198 {
199 	int index;
200 
201 	switch (code) {
202 		case BTN_TOUCH:
203 		case BTN_0:
204 		case BTN_LEFT:		index = 0; break;
205 		case BTN_STYLUS:
206 		case BTN_1:
207 		case BTN_RIGHT:		index = 1; break;
208 		case BTN_2:
209 		case BTN_FORWARD:
210 		case BTN_STYLUS2:
211 		case BTN_MIDDLE:	index = 2; break;
212 		case BTN_3:
213 		case BTN_BACK:
214 		case BTN_SIDE:		index = 3; break;
215 		case BTN_4:
216 		case BTN_EXTRA:		index = 4; break;
217 		default:		return;
218 	}
219 
220 	if (value) {
221 		set_bit(index, &mousedev->packet.buttons);
222 		set_bit(index, &mousedev_mix->packet.buttons);
223 	} else {
224 		clear_bit(index, &mousedev->packet.buttons);
225 		clear_bit(index, &mousedev_mix->packet.buttons);
226 	}
227 }
228 
229 static void mousedev_notify_readers(struct mousedev *mousedev, struct mousedev_hw_data *packet)
230 {
231 	struct mousedev_client *client;
232 	struct mousedev_motion *p;
233 	unsigned long flags;
234 	int wake_readers = 0;
235 
236 	list_for_each_entry(client, &mousedev->client_list, node) {
237 		spin_lock_irqsave(&client->packet_lock, flags);
238 
239 		p = &client->packets[client->head];
240 		if (client->ready && p->buttons != mousedev->packet.buttons) {
241 			unsigned int new_head = (client->head + 1) % PACKET_QUEUE_LEN;
242 			if (new_head != client->tail) {
243 				p = &client->packets[client->head = new_head];
244 				memset(p, 0, sizeof(struct mousedev_motion));
245 			}
246 		}
247 
248 		if (packet->abs_event) {
249 			p->dx += packet->x - client->pos_x;
250 			p->dy += packet->y - client->pos_y;
251 			client->pos_x = packet->x;
252 			client->pos_y = packet->y;
253 		}
254 
255 		client->pos_x += packet->dx;
256 		client->pos_x = client->pos_x < 0 ? 0 : (client->pos_x >= xres ? xres : client->pos_x);
257 		client->pos_y += packet->dy;
258 		client->pos_y = client->pos_y < 0 ? 0 : (client->pos_y >= yres ? yres : client->pos_y);
259 
260 		p->dx += packet->dx;
261 		p->dy += packet->dy;
262 		p->dz += packet->dz;
263 		p->buttons = mousedev->packet.buttons;
264 
265 		if (p->dx || p->dy || p->dz || p->buttons != client->last_buttons)
266 			client->ready = 1;
267 
268 		spin_unlock_irqrestore(&client->packet_lock, flags);
269 
270 		if (client->ready) {
271 			kill_fasync(&client->fasync, SIGIO, POLL_IN);
272 			wake_readers = 1;
273 		}
274 	}
275 
276 	if (wake_readers)
277 		wake_up_interruptible(&mousedev->wait);
278 }
279 
280 static void mousedev_touchpad_touch(struct mousedev *mousedev, int value)
281 {
282 	if (!value) {
283 		if (mousedev->touch &&
284 		    time_before(jiffies, mousedev->touch + msecs_to_jiffies(tap_time))) {
285 			/*
286 			 * Toggle left button to emulate tap.
287 			 * We rely on the fact that mousedev_mix always has 0
288 			 * motion packet so we won't mess current position.
289 			 */
290 			set_bit(0, &mousedev->packet.buttons);
291 			set_bit(0, &mousedev_mix->packet.buttons);
292 			mousedev_notify_readers(mousedev, &mousedev_mix->packet);
293 			mousedev_notify_readers(mousedev_mix, &mousedev_mix->packet);
294 			clear_bit(0, &mousedev->packet.buttons);
295 			clear_bit(0, &mousedev_mix->packet.buttons);
296 		}
297 		mousedev->touch = mousedev->pkt_count = 0;
298 		mousedev->frac_dx = 0;
299 		mousedev->frac_dy = 0;
300 
301 	} else if (!mousedev->touch)
302 		mousedev->touch = jiffies;
303 }
304 
305 static void mousedev_event(struct input_handle *handle, unsigned int type, unsigned int code, int value)
306 {
307 	struct mousedev *mousedev = handle->private;
308 
309 	switch (type) {
310 		case EV_ABS:
311 			/* Ignore joysticks */
312 			if (test_bit(BTN_TRIGGER, handle->dev->keybit))
313 				return;
314 
315 			if (test_bit(BTN_TOOL_FINGER, handle->dev->keybit))
316 				mousedev_touchpad_event(handle->dev, mousedev, code, value);
317 			else
318 				mousedev_abs_event(handle->dev, mousedev, code, value);
319 
320 			break;
321 
322 		case EV_REL:
323 			mousedev_rel_event(mousedev, code, value);
324 			break;
325 
326 		case EV_KEY:
327 			if (value != 2) {
328 				if (code == BTN_TOUCH && test_bit(BTN_TOOL_FINGER, handle->dev->keybit))
329 					mousedev_touchpad_touch(mousedev, value);
330 				else
331 					mousedev_key_event(mousedev, code, value);
332 			}
333 			break;
334 
335 		case EV_SYN:
336 			if (code == SYN_REPORT) {
337 				if (mousedev->touch) {
338 					mousedev->pkt_count++;
339 					/* Input system eats duplicate events, but we need all of them
340 					 * to do correct averaging so apply present one forward
341 					 */
342 					fx(0) = fx(1);
343 					fy(0) = fy(1);
344 				}
345 
346 				mousedev_notify_readers(mousedev, &mousedev->packet);
347 				mousedev_notify_readers(mousedev_mix, &mousedev->packet);
348 
349 				mousedev->packet.dx = mousedev->packet.dy = mousedev->packet.dz = 0;
350 				mousedev->packet.abs_event = 0;
351 			}
352 			break;
353 	}
354 }
355 
356 static int mousedev_fasync(int fd, struct file *file, int on)
357 {
358 	int retval;
359 	struct mousedev_client *client = file->private_data;
360 
361 	retval = fasync_helper(fd, file, on, &client->fasync);
362 
363 	return retval < 0 ? retval : 0;
364 }
365 
366 static void mousedev_free(struct device *dev)
367 {
368 	struct mousedev *mousedev = container_of(dev, struct mousedev, dev);
369 
370 	mousedev_table[mousedev->minor] = NULL;
371 	kfree(mousedev);
372 }
373 
374 static int mixdev_add_device(struct mousedev *mousedev)
375 {
376 	int error;
377 
378 	if (mousedev_mix->open) {
379 		error = input_open_device(&mousedev->handle);
380 		if (error)
381 			return error;
382 
383 		mousedev->open++;
384 		mousedev->mixdev_open = 1;
385 	}
386 
387 	get_device(&mousedev->dev);
388 	list_add_tail(&mousedev->mixdev_node, &mousedev_mix_list);
389 
390 	return 0;
391 }
392 
393 static void mixdev_remove_device(struct mousedev *mousedev)
394 {
395 	if (mousedev->mixdev_open) {
396 		mousedev->mixdev_open = 0;
397 		if (!--mousedev->open && mousedev->exist)
398 			input_close_device(&mousedev->handle);
399 	}
400 
401 	list_del_init(&mousedev->mixdev_node);
402 	put_device(&mousedev->dev);
403 }
404 
405 static void mixdev_open_devices(void)
406 {
407 	struct mousedev *mousedev;
408 
409 	if (mousedev_mix->open++)
410 		return;
411 
412 	list_for_each_entry(mousedev, &mousedev_mix_list, mixdev_node) {
413 		if (!mousedev->mixdev_open) {
414 			if (!mousedev->open && mousedev->exist)
415 				if (input_open_device(&mousedev->handle))
416 					continue;
417 
418 			mousedev->open++;
419 			mousedev->mixdev_open = 1;
420 		}
421 	}
422 }
423 
424 static void mixdev_close_devices(void)
425 {
426 	struct mousedev *mousedev;
427 
428 	if (--mousedev_mix->open)
429 		return;
430 
431 	list_for_each_entry(mousedev, &mousedev_mix_list, mixdev_node) {
432 		if (mousedev->mixdev_open) {
433 			mousedev->mixdev_open = 0;
434 			if (!--mousedev->open && mousedev->exist)
435 				input_close_device(&mousedev->handle);
436 		}
437 	}
438 }
439 
440 static int mousedev_release(struct inode *inode, struct file *file)
441 {
442 	struct mousedev_client *client = file->private_data;
443 	struct mousedev *mousedev = client->mousedev;
444 
445 	mousedev_fasync(-1, file, 0);
446 
447 	list_del(&client->node);
448 	kfree(client);
449 
450 	if (mousedev->minor == MOUSEDEV_MIX)
451 		mixdev_close_devices();
452 	else if (!--mousedev->open && mousedev->exist)
453 		input_close_device(&mousedev->handle);
454 
455 	put_device(&mousedev->dev);
456 
457 	return 0;
458 }
459 
460 
461 static int mousedev_open(struct inode *inode, struct file *file)
462 {
463 	struct mousedev_client *client;
464 	struct mousedev *mousedev;
465 	int error;
466 	int i;
467 
468 #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
469 	if (imajor(inode) == MISC_MAJOR)
470 		i = MOUSEDEV_MIX;
471 	else
472 #endif
473 		i = iminor(inode) - MOUSEDEV_MINOR_BASE;
474 
475 	if (i >= MOUSEDEV_MINORS)
476 		return -ENODEV;
477 
478 	mousedev = mousedev_table[i];
479 	if (!mousedev)
480 		return -ENODEV;
481 
482 	get_device(&mousedev->dev);
483 
484 	client = kzalloc(sizeof(struct mousedev_client), GFP_KERNEL);
485 	if (!client) {
486 		error = -ENOMEM;
487 		goto err_put_mousedev;
488 	}
489 
490 	spin_lock_init(&client->packet_lock);
491 	client->pos_x = xres / 2;
492 	client->pos_y = yres / 2;
493 	client->mousedev = mousedev;
494 	list_add_tail(&client->node, &mousedev->client_list);
495 
496 	if (mousedev->minor == MOUSEDEV_MIX)
497 		mixdev_open_devices();
498 	else if (!mousedev->open++ && mousedev->exist) {
499 		error = input_open_device(&mousedev->handle);
500 		if (error)
501 			goto err_free_client;
502 	}
503 
504 	file->private_data = client;
505 	return 0;
506 
507  err_free_client:
508 	list_del(&client->node);
509 	kfree(client);
510  err_put_mousedev:
511 	put_device(&mousedev->dev);
512 	return error;
513 }
514 
515 static inline int mousedev_limit_delta(int delta, int limit)
516 {
517 	return delta > limit ? limit : (delta < -limit ? -limit : delta);
518 }
519 
520 static void mousedev_packet(struct mousedev_client *client, signed char *ps2_data)
521 {
522 	struct mousedev_motion *p;
523 	unsigned long flags;
524 
525 	spin_lock_irqsave(&client->packet_lock, flags);
526 	p = &client->packets[client->tail];
527 
528 	ps2_data[0] = 0x08 | ((p->dx < 0) << 4) | ((p->dy < 0) << 5) | (p->buttons & 0x07);
529 	ps2_data[1] = mousedev_limit_delta(p->dx, 127);
530 	ps2_data[2] = mousedev_limit_delta(p->dy, 127);
531 	p->dx -= ps2_data[1];
532 	p->dy -= ps2_data[2];
533 
534 	switch (client->mode) {
535 		case MOUSEDEV_EMUL_EXPS:
536 			ps2_data[3] = mousedev_limit_delta(p->dz, 7);
537 			p->dz -= ps2_data[3];
538 			ps2_data[3] = (ps2_data[3] & 0x0f) | ((p->buttons & 0x18) << 1);
539 			client->bufsiz = 4;
540 			break;
541 
542 		case MOUSEDEV_EMUL_IMPS:
543 			ps2_data[0] |= ((p->buttons & 0x10) >> 3) | ((p->buttons & 0x08) >> 1);
544 			ps2_data[3] = mousedev_limit_delta(p->dz, 127);
545 			p->dz -= ps2_data[3];
546 			client->bufsiz = 4;
547 			break;
548 
549 		case MOUSEDEV_EMUL_PS2:
550 		default:
551 			ps2_data[0] |= ((p->buttons & 0x10) >> 3) | ((p->buttons & 0x08) >> 1);
552 			p->dz = 0;
553 			client->bufsiz = 3;
554 			break;
555 	}
556 
557 	if (!p->dx && !p->dy && !p->dz) {
558 		if (client->tail == client->head) {
559 			client->ready = 0;
560 			client->last_buttons = p->buttons;
561 		} else
562 			client->tail = (client->tail + 1) % PACKET_QUEUE_LEN;
563 	}
564 
565 	spin_unlock_irqrestore(&client->packet_lock, flags);
566 }
567 
568 
569 static ssize_t mousedev_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
570 {
571 	struct mousedev_client *client = file->private_data;
572 	unsigned char c;
573 	unsigned int i;
574 
575 	for (i = 0; i < count; i++) {
576 
577 		if (get_user(c, buffer + i))
578 			return -EFAULT;
579 
580 		if (c == mousedev_imex_seq[client->imexseq]) {
581 			if (++client->imexseq == MOUSEDEV_SEQ_LEN) {
582 				client->imexseq = 0;
583 				client->mode = MOUSEDEV_EMUL_EXPS;
584 			}
585 		} else
586 			client->imexseq = 0;
587 
588 		if (c == mousedev_imps_seq[client->impsseq]) {
589 			if (++client->impsseq == MOUSEDEV_SEQ_LEN) {
590 				client->impsseq = 0;
591 				client->mode = MOUSEDEV_EMUL_IMPS;
592 			}
593 		} else
594 			client->impsseq = 0;
595 
596 		client->ps2[0] = 0xfa;
597 
598 		switch (c) {
599 
600 			case 0xeb: /* Poll */
601 				mousedev_packet(client, &client->ps2[1]);
602 				client->bufsiz++; /* account for leading ACK */
603 				break;
604 
605 			case 0xf2: /* Get ID */
606 				switch (client->mode) {
607 					case MOUSEDEV_EMUL_PS2:  client->ps2[1] = 0; break;
608 					case MOUSEDEV_EMUL_IMPS: client->ps2[1] = 3; break;
609 					case MOUSEDEV_EMUL_EXPS: client->ps2[1] = 4; break;
610 				}
611 				client->bufsiz = 2;
612 				break;
613 
614 			case 0xe9: /* Get info */
615 				client->ps2[1] = 0x60; client->ps2[2] = 3; client->ps2[3] = 200;
616 				client->bufsiz = 4;
617 				break;
618 
619 			case 0xff: /* Reset */
620 				client->impsseq = client->imexseq = 0;
621 				client->mode = MOUSEDEV_EMUL_PS2;
622 				client->ps2[1] = 0xaa; client->ps2[2] = 0x00;
623 				client->bufsiz = 3;
624 				break;
625 
626 			default:
627 				client->bufsiz = 1;
628 				break;
629 		}
630 
631 		client->buffer = client->bufsiz;
632 	}
633 
634 	kill_fasync(&client->fasync, SIGIO, POLL_IN);
635 
636 	wake_up_interruptible(&client->mousedev->wait);
637 
638 	return count;
639 }
640 
641 static ssize_t mousedev_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
642 {
643 	struct mousedev_client *client = file->private_data;
644 	int retval = 0;
645 
646 	if (!client->ready && !client->buffer && (file->f_flags & O_NONBLOCK))
647 		return -EAGAIN;
648 
649 	retval = wait_event_interruptible(client->mousedev->wait,
650 					  !client->mousedev->exist || client->ready || client->buffer);
651 
652 	if (retval)
653 		return retval;
654 
655 	if (!client->mousedev->exist)
656 		return -ENODEV;
657 
658 	if (!client->buffer && client->ready) {
659 		mousedev_packet(client, client->ps2);
660 		client->buffer = client->bufsiz;
661 	}
662 
663 	if (count > client->buffer)
664 		count = client->buffer;
665 
666 	client->buffer -= count;
667 
668 	if (copy_to_user(buffer, client->ps2 + client->bufsiz - client->buffer - count, count))
669 		return -EFAULT;
670 
671 	return count;
672 }
673 
674 /* No kernel lock - fine */
675 static unsigned int mousedev_poll(struct file *file, poll_table *wait)
676 {
677 	struct mousedev_client *client = file->private_data;
678 	struct mousedev *mousedev = client->mousedev;
679 
680 	poll_wait(file, &mousedev->wait, wait);
681 	return ((client->ready || client->buffer) ? (POLLIN | POLLRDNORM) : 0) |
682 		(mousedev->exist ? 0 : (POLLHUP | POLLERR));
683 }
684 
685 static const struct file_operations mousedev_fops = {
686 	.owner =	THIS_MODULE,
687 	.read =		mousedev_read,
688 	.write =	mousedev_write,
689 	.poll =		mousedev_poll,
690 	.open =		mousedev_open,
691 	.release =	mousedev_release,
692 	.fasync =	mousedev_fasync,
693 };
694 
695 static struct mousedev *mousedev_create(struct input_dev *dev,
696 					struct input_handler *handler,
697 					int minor)
698 {
699 	struct mousedev *mousedev;
700 	int error;
701 
702 	mousedev = kzalloc(sizeof(struct mousedev), GFP_KERNEL);
703 	if (!mousedev) {
704 		error = -ENOMEM;
705 		goto err_out;
706 	}
707 
708 	INIT_LIST_HEAD(&mousedev->client_list);
709 	INIT_LIST_HEAD(&mousedev->mixdev_node);
710 	init_waitqueue_head(&mousedev->wait);
711 
712 	if (minor == MOUSEDEV_MIX)
713 		strlcpy(mousedev->name, "mice", sizeof(mousedev->name));
714 	else
715 		snprintf(mousedev->name, sizeof(mousedev->name),
716 			 "mouse%d", minor);
717 
718 	mousedev->minor = minor;
719 	mousedev->exist = 1;
720 	mousedev->handle.dev = dev;
721 	mousedev->handle.name = mousedev->name;
722 	mousedev->handle.handler = handler;
723 	mousedev->handle.private = mousedev;
724 
725 	strlcpy(mousedev->dev.bus_id, mousedev->name,
726 		sizeof(mousedev->dev.bus_id));
727 	mousedev->dev.class = &input_class;
728 	if (dev)
729 		mousedev->dev.parent = &dev->dev;
730 	mousedev->dev.devt = MKDEV(INPUT_MAJOR, MOUSEDEV_MINOR_BASE + minor);
731 	mousedev->dev.release = mousedev_free;
732 	device_initialize(&mousedev->dev);
733 
734 	mousedev_table[minor] = mousedev;
735 
736 	error = device_add(&mousedev->dev);
737 	if (error)
738 		goto err_free_mousedev;
739 
740 	return mousedev;
741 
742  err_free_mousedev:
743 	put_device(&mousedev->dev);
744  err_out:
745 	return ERR_PTR(error);
746 }
747 
748 static void mousedev_destroy(struct mousedev *mousedev)
749 {
750 	struct mousedev_client *client;
751 
752 	device_del(&mousedev->dev);
753 	mousedev->exist = 0;
754 
755 	if (mousedev->open) {
756 		input_close_device(&mousedev->handle);
757 		list_for_each_entry(client, &mousedev->client_list, node)
758 			kill_fasync(&client->fasync, SIGIO, POLL_HUP);
759 		wake_up_interruptible(&mousedev->wait);
760 	}
761 
762 	put_device(&mousedev->dev);
763 }
764 
765 static int mousedev_connect(struct input_handler *handler, struct input_dev *dev,
766 			    const struct input_device_id *id)
767 {
768 	struct mousedev *mousedev;
769 	int minor;
770 	int error;
771 
772 	for (minor = 0; minor < MOUSEDEV_MINORS && mousedev_table[minor]; minor++);
773 	if (minor == MOUSEDEV_MINORS) {
774 		printk(KERN_ERR "mousedev: no more free mousedev devices\n");
775 		return -ENFILE;
776 	}
777 
778 	mousedev = mousedev_create(dev, handler, minor);
779 	if (IS_ERR(mousedev))
780 		return PTR_ERR(mousedev);
781 
782 	error = input_register_handle(&mousedev->handle);
783 	if (error)
784 		goto err_delete_mousedev;
785 
786 	error = mixdev_add_device(mousedev);
787 	if (error)
788 		goto err_unregister_handle;
789 
790 	return 0;
791 
792  err_unregister_handle:
793 	input_unregister_handle(&mousedev->handle);
794  err_delete_mousedev:
795 	device_unregister(&mousedev->dev);
796 	return error;
797 }
798 
799 static void mousedev_disconnect(struct input_handle *handle)
800 {
801 	struct mousedev *mousedev = handle->private;
802 
803 	mixdev_remove_device(mousedev);
804 	input_unregister_handle(handle);
805 	mousedev_destroy(mousedev);
806 }
807 
808 static const struct input_device_id mousedev_ids[] = {
809 	{
810 		.flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_RELBIT,
811 		.evbit = { BIT(EV_KEY) | BIT(EV_REL) },
812 		.keybit = { [LONG(BTN_LEFT)] = BIT(BTN_LEFT) },
813 		.relbit = { BIT(REL_X) | BIT(REL_Y) },
814 	},	/* A mouse like device, at least one button, two relative axes */
815 	{
816 		.flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_RELBIT,
817 		.evbit = { BIT(EV_KEY) | BIT(EV_REL) },
818 		.relbit = { BIT(REL_WHEEL) },
819 	},	/* A separate scrollwheel */
820 	{
821 		.flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_ABSBIT,
822 		.evbit = { BIT(EV_KEY) | BIT(EV_ABS) },
823 		.keybit = { [LONG(BTN_TOUCH)] = BIT(BTN_TOUCH) },
824 		.absbit = { BIT(ABS_X) | BIT(ABS_Y) },
825 	},	/* A tablet like device, at least touch detection, two absolute axes */
826 	{
827 		.flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_ABSBIT,
828 		.evbit = { BIT(EV_KEY) | BIT(EV_ABS) },
829 		.keybit = { [LONG(BTN_TOOL_FINGER)] = BIT(BTN_TOOL_FINGER) },
830 		.absbit = { BIT(ABS_X) | BIT(ABS_Y) | BIT(ABS_PRESSURE) | BIT(ABS_TOOL_WIDTH) },
831 	},	/* A touchpad */
832 
833 	{ },	/* Terminating entry */
834 };
835 
836 MODULE_DEVICE_TABLE(input, mousedev_ids);
837 
838 static struct input_handler mousedev_handler = {
839 	.event =	mousedev_event,
840 	.connect =	mousedev_connect,
841 	.disconnect =	mousedev_disconnect,
842 	.fops =		&mousedev_fops,
843 	.minor =	MOUSEDEV_MINOR_BASE,
844 	.name =		"mousedev",
845 	.id_table =	mousedev_ids,
846 };
847 
848 #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
849 static struct miscdevice psaux_mouse = {
850 	PSMOUSE_MINOR, "psaux", &mousedev_fops
851 };
852 static int psaux_registered;
853 #endif
854 
855 static int __init mousedev_init(void)
856 {
857 	int error;
858 
859 	mousedev_mix = mousedev_create(NULL, &mousedev_handler, MOUSEDEV_MIX);
860 	if (IS_ERR(mousedev_mix))
861 		return PTR_ERR(mousedev_mix);
862 
863 	error = input_register_handler(&mousedev_handler);
864 	if (error) {
865 		mousedev_destroy(mousedev_mix);
866 		return error;
867 	}
868 
869 #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
870 	error = misc_register(&psaux_mouse);
871 	if (error)
872 		printk(KERN_WARNING "mice: could not register psaux device, "
873 			"error: %d\n", error);
874 	else
875 		psaux_registered = 1;
876 #endif
877 
878 	printk(KERN_INFO "mice: PS/2 mouse device common for all mice\n");
879 
880 	return 0;
881 }
882 
883 static void __exit mousedev_exit(void)
884 {
885 #ifdef CONFIG_INPUT_MOUSEDEV_PSAUX
886 	if (psaux_registered)
887 		misc_deregister(&psaux_mouse);
888 #endif
889 	input_unregister_handler(&mousedev_handler);
890 	mousedev_destroy(mousedev_mix);
891 }
892 
893 module_init(mousedev_init);
894 module_exit(mousedev_exit);
895