aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/input
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/input')
-rw-r--r--drivers/input/evdev.c716
-rw-r--r--drivers/input/input-polldev.c7
-rw-r--r--drivers/input/input.c666
-rw-r--r--drivers/input/joydev.c746
-rw-r--r--drivers/input/joystick/xpad.c55
-rw-r--r--drivers/input/keyboard/Kconfig31
-rw-r--r--drivers/input/keyboard/Makefile3
-rw-r--r--drivers/input/keyboard/gpio_keys.c81
-rw-r--r--drivers/input/keyboard/jornada680_kbd.c277
-rw-r--r--drivers/input/keyboard/jornada720_kbd.c185
-rw-r--r--drivers/input/keyboard/maple_keyb.c252
-rw-r--r--drivers/input/keyboard/omap-keypad.c22
-rw-r--r--drivers/input/mouse/alps.c2
-rw-r--r--drivers/input/mouse/lifebook.c10
-rw-r--r--drivers/input/mouse/psmouse-base.c5
-rw-r--r--drivers/input/mousedev.c743
-rw-r--r--drivers/input/serio/i8042.c4
-rw-r--r--drivers/input/touchscreen/Kconfig21
-rw-r--r--drivers/input/touchscreen/Makefile1
-rw-r--r--drivers/input/touchscreen/jornada720_ts.c182
-rw-r--r--drivers/input/touchscreen/ucb1400_ts.c3
-rw-r--r--drivers/input/touchscreen/usbtouchscreen.c93
-rw-r--r--drivers/input/tsdev.c391
23 files changed, 3399 insertions, 1097 deletions
diff --git a/drivers/input/evdev.c b/drivers/input/evdev.c
index f1c3d6cebd58..27026f7d3c03 100644
--- a/drivers/input/evdev.c
+++ b/drivers/input/evdev.c
@@ -30,6 +30,8 @@ struct evdev {
30 wait_queue_head_t wait; 30 wait_queue_head_t wait;
31 struct evdev_client *grab; 31 struct evdev_client *grab;
32 struct list_head client_list; 32 struct list_head client_list;
33 spinlock_t client_lock; /* protects client_list */
34 struct mutex mutex;
33 struct device dev; 35 struct device dev;
34}; 36};
35 37
@@ -37,39 +39,53 @@ struct evdev_client {
37 struct input_event buffer[EVDEV_BUFFER_SIZE]; 39 struct input_event buffer[EVDEV_BUFFER_SIZE];
38 int head; 40 int head;
39 int tail; 41 int tail;
42 spinlock_t buffer_lock; /* protects access to buffer, head and tail */
40 struct fasync_struct *fasync; 43 struct fasync_struct *fasync;
41 struct evdev *evdev; 44 struct evdev *evdev;
42 struct list_head node; 45 struct list_head node;
43}; 46};
44 47
45static struct evdev *evdev_table[EVDEV_MINORS]; 48static struct evdev *evdev_table[EVDEV_MINORS];
49static DEFINE_MUTEX(evdev_table_mutex);
46 50
47static void evdev_event(struct input_handle *handle, unsigned int type, unsigned int code, int value) 51static void evdev_pass_event(struct evdev_client *client,
52 struct input_event *event)
53{
54 /*
55 * Interrupts are disabled, just acquire the lock
56 */
57 spin_lock(&client->buffer_lock);
58 client->buffer[client->head++] = *event;
59 client->head &= EVDEV_BUFFER_SIZE - 1;
60 spin_unlock(&client->buffer_lock);
61
62 kill_fasync(&client->fasync, SIGIO, POLL_IN);
63}
64
65/*
66 * Pass incoming event to all connected clients. Note that we are
67 * caleld under a spinlock with interrupts off so we don't need
68 * to use rcu_read_lock() here. Writers will be using syncronize_sched()
69 * instead of synchrnoize_rcu().
70 */
71static void evdev_event(struct input_handle *handle,
72 unsigned int type, unsigned int code, int value)
48{ 73{
49 struct evdev *evdev = handle->private; 74 struct evdev *evdev = handle->private;
50 struct evdev_client *client; 75 struct evdev_client *client;
76 struct input_event event;
51 77
52 if (evdev->grab) { 78 do_gettimeofday(&event.time);
53 client = evdev->grab; 79 event.type = type;
54 80 event.code = code;
55 do_gettimeofday(&client->buffer[client->head].time); 81 event.value = value;
56 client->buffer[client->head].type = type;
57 client->buffer[client->head].code = code;
58 client->buffer[client->head].value = value;
59 client->head = (client->head + 1) & (EVDEV_BUFFER_SIZE - 1);
60
61 kill_fasync(&client->fasync, SIGIO, POLL_IN);
62 } else
63 list_for_each_entry(client, &evdev->client_list, node) {
64
65 do_gettimeofday(&client->buffer[client->head].time);
66 client->buffer[client->head].type = type;
67 client->buffer[client->head].code = code;
68 client->buffer[client->head].value = value;
69 client->head = (client->head + 1) & (EVDEV_BUFFER_SIZE - 1);
70 82
71 kill_fasync(&client->fasync, SIGIO, POLL_IN); 83 client = rcu_dereference(evdev->grab);
72 } 84 if (client)
85 evdev_pass_event(client, &event);
86 else
87 list_for_each_entry_rcu(client, &evdev->client_list, node)
88 evdev_pass_event(client, &event);
73 89
74 wake_up_interruptible(&evdev->wait); 90 wake_up_interruptible(&evdev->wait);
75} 91}
@@ -88,38 +104,145 @@ static int evdev_flush(struct file *file, fl_owner_t id)
88{ 104{
89 struct evdev_client *client = file->private_data; 105 struct evdev_client *client = file->private_data;
90 struct evdev *evdev = client->evdev; 106 struct evdev *evdev = client->evdev;
107 int retval;
108
109 retval = mutex_lock_interruptible(&evdev->mutex);
110 if (retval)
111 return retval;
91 112
92 if (!evdev->exist) 113 if (!evdev->exist)
93 return -ENODEV; 114 retval = -ENODEV;
115 else
116 retval = input_flush_device(&evdev->handle, file);
94 117
95 return input_flush_device(&evdev->handle, file); 118 mutex_unlock(&evdev->mutex);
119 return retval;
96} 120}
97 121
98static void evdev_free(struct device *dev) 122static void evdev_free(struct device *dev)
99{ 123{
100 struct evdev *evdev = container_of(dev, struct evdev, dev); 124 struct evdev *evdev = container_of(dev, struct evdev, dev);
101 125
102 evdev_table[evdev->minor] = NULL;
103 kfree(evdev); 126 kfree(evdev);
104} 127}
105 128
129/*
130 * Grabs an event device (along with underlying input device).
131 * This function is called with evdev->mutex taken.
132 */
133static int evdev_grab(struct evdev *evdev, struct evdev_client *client)
134{
135 int error;
136
137 if (evdev->grab)
138 return -EBUSY;
139
140 error = input_grab_device(&evdev->handle);
141 if (error)
142 return error;
143
144 rcu_assign_pointer(evdev->grab, client);
145 /*
146 * We don't use synchronize_rcu() here because read-side
147 * critical section is protected by a spinlock instead
148 * of rcu_read_lock().
149 */
150 synchronize_sched();
151
152 return 0;
153}
154
155static int evdev_ungrab(struct evdev *evdev, struct evdev_client *client)
156{
157 if (evdev->grab != client)
158 return -EINVAL;
159
160 rcu_assign_pointer(evdev->grab, NULL);
161 synchronize_sched();
162 input_release_device(&evdev->handle);
163
164 return 0;
165}
166
167static void evdev_attach_client(struct evdev *evdev,
168 struct evdev_client *client)
169{
170 spin_lock(&evdev->client_lock);
171 list_add_tail_rcu(&client->node, &evdev->client_list);
172 spin_unlock(&evdev->client_lock);
173 synchronize_sched();
174}
175
176static void evdev_detach_client(struct evdev *evdev,
177 struct evdev_client *client)
178{
179 spin_lock(&evdev->client_lock);
180 list_del_rcu(&client->node);
181 spin_unlock(&evdev->client_lock);
182 synchronize_sched();
183}
184
185static int evdev_open_device(struct evdev *evdev)
186{
187 int retval;
188
189 retval = mutex_lock_interruptible(&evdev->mutex);
190 if (retval)
191 return retval;
192
193 if (!evdev->exist)
194 retval = -ENODEV;
195 else if (!evdev->open++) {
196 retval = input_open_device(&evdev->handle);
197 if (retval)
198 evdev->open--;
199 }
200
201 mutex_unlock(&evdev->mutex);
202 return retval;
203}
204
205static void evdev_close_device(struct evdev *evdev)
206{
207 mutex_lock(&evdev->mutex);
208
209 if (evdev->exist && !--evdev->open)
210 input_close_device(&evdev->handle);
211
212 mutex_unlock(&evdev->mutex);
213}
214
215/*
216 * Wake up users waiting for IO so they can disconnect from
217 * dead device.
218 */
219static void evdev_hangup(struct evdev *evdev)
220{
221 struct evdev_client *client;
222
223 spin_lock(&evdev->client_lock);
224 list_for_each_entry(client, &evdev->client_list, node)
225 kill_fasync(&client->fasync, SIGIO, POLL_HUP);
226 spin_unlock(&evdev->client_lock);
227
228 wake_up_interruptible(&evdev->wait);
229}
230
106static int evdev_release(struct inode *inode, struct file *file) 231static int evdev_release(struct inode *inode, struct file *file)
107{ 232{
108 struct evdev_client *client = file->private_data; 233 struct evdev_client *client = file->private_data;
109 struct evdev *evdev = client->evdev; 234 struct evdev *evdev = client->evdev;
110 235
111 if (evdev->grab == client) { 236 mutex_lock(&evdev->mutex);
112 input_release_device(&evdev->handle); 237 if (evdev->grab == client)
113 evdev->grab = NULL; 238 evdev_ungrab(evdev, client);
114 } 239 mutex_unlock(&evdev->mutex);
115 240
116 evdev_fasync(-1, file, 0); 241 evdev_fasync(-1, file, 0);
117 list_del(&client->node); 242 evdev_detach_client(evdev, client);
118 kfree(client); 243 kfree(client);
119 244
120 if (!--evdev->open && evdev->exist) 245 evdev_close_device(evdev);
121 input_close_device(&evdev->handle);
122
123 put_device(&evdev->dev); 246 put_device(&evdev->dev);
124 247
125 return 0; 248 return 0;
@@ -127,41 +250,44 @@ static int evdev_release(struct inode *inode, struct file *file)
127 250
128static int evdev_open(struct inode *inode, struct file *file) 251static int evdev_open(struct inode *inode, struct file *file)
129{ 252{
130 struct evdev_client *client;
131 struct evdev *evdev; 253 struct evdev *evdev;
254 struct evdev_client *client;
132 int i = iminor(inode) - EVDEV_MINOR_BASE; 255 int i = iminor(inode) - EVDEV_MINOR_BASE;
133 int error; 256 int error;
134 257
135 if (i >= EVDEV_MINORS) 258 if (i >= EVDEV_MINORS)
136 return -ENODEV; 259 return -ENODEV;
137 260
261 error = mutex_lock_interruptible(&evdev_table_mutex);
262 if (error)
263 return error;
138 evdev = evdev_table[i]; 264 evdev = evdev_table[i];
265 if (evdev)
266 get_device(&evdev->dev);
267 mutex_unlock(&evdev_table_mutex);
139 268
140 if (!evdev || !evdev->exist) 269 if (!evdev)
141 return -ENODEV; 270 return -ENODEV;
142 271
143 get_device(&evdev->dev);
144
145 client = kzalloc(sizeof(struct evdev_client), GFP_KERNEL); 272 client = kzalloc(sizeof(struct evdev_client), GFP_KERNEL);
146 if (!client) { 273 if (!client) {
147 error = -ENOMEM; 274 error = -ENOMEM;
148 goto err_put_evdev; 275 goto err_put_evdev;
149 } 276 }
150 277
278 spin_lock_init(&client->buffer_lock);
151 client->evdev = evdev; 279 client->evdev = evdev;
152 list_add_tail(&client->node, &evdev->client_list); 280 evdev_attach_client(evdev, client);
153 281
154 if (!evdev->open++ && evdev->exist) { 282 error = evdev_open_device(evdev);
155 error = input_open_device(&evdev->handle); 283 if (error)
156 if (error) 284 goto err_free_client;
157 goto err_free_client;
158 }
159 285
160 file->private_data = client; 286 file->private_data = client;
161 return 0; 287 return 0;
162 288
163 err_free_client: 289 err_free_client:
164 list_del(&client->node); 290 evdev_detach_client(evdev, client);
165 kfree(client); 291 kfree(client);
166 err_put_evdev: 292 err_put_evdev:
167 put_device(&evdev->dev); 293 put_device(&evdev->dev);
@@ -197,12 +323,14 @@ static inline size_t evdev_event_size(void)
197 sizeof(struct input_event_compat) : sizeof(struct input_event); 323 sizeof(struct input_event_compat) : sizeof(struct input_event);
198} 324}
199 325
200static int evdev_event_from_user(const char __user *buffer, struct input_event *event) 326static int evdev_event_from_user(const char __user *buffer,
327 struct input_event *event)
201{ 328{
202 if (COMPAT_TEST) { 329 if (COMPAT_TEST) {
203 struct input_event_compat compat_event; 330 struct input_event_compat compat_event;
204 331
205 if (copy_from_user(&compat_event, buffer, sizeof(struct input_event_compat))) 332 if (copy_from_user(&compat_event, buffer,
333 sizeof(struct input_event_compat)))
206 return -EFAULT; 334 return -EFAULT;
207 335
208 event->time.tv_sec = compat_event.time.tv_sec; 336 event->time.tv_sec = compat_event.time.tv_sec;
@@ -219,7 +347,8 @@ static int evdev_event_from_user(const char __user *buffer, struct input_event *
219 return 0; 347 return 0;
220} 348}
221 349
222static int evdev_event_to_user(char __user *buffer, const struct input_event *event) 350static int evdev_event_to_user(char __user *buffer,
351 const struct input_event *event)
223{ 352{
224 if (COMPAT_TEST) { 353 if (COMPAT_TEST) {
225 struct input_event_compat compat_event; 354 struct input_event_compat compat_event;
@@ -230,7 +359,8 @@ static int evdev_event_to_user(char __user *buffer, const struct input_event *ev
230 compat_event.code = event->code; 359 compat_event.code = event->code;
231 compat_event.value = event->value; 360 compat_event.value = event->value;
232 361
233 if (copy_to_user(buffer, &compat_event, sizeof(struct input_event_compat))) 362 if (copy_to_user(buffer, &compat_event,
363 sizeof(struct input_event_compat)))
234 return -EFAULT; 364 return -EFAULT;
235 365
236 } else { 366 } else {
@@ -248,7 +378,8 @@ static inline size_t evdev_event_size(void)
248 return sizeof(struct input_event); 378 return sizeof(struct input_event);
249} 379}
250 380
251static int evdev_event_from_user(const char __user *buffer, struct input_event *event) 381static int evdev_event_from_user(const char __user *buffer,
382 struct input_event *event)
252{ 383{
253 if (copy_from_user(event, buffer, sizeof(struct input_event))) 384 if (copy_from_user(event, buffer, sizeof(struct input_event)))
254 return -EFAULT; 385 return -EFAULT;
@@ -256,7 +387,8 @@ static int evdev_event_from_user(const char __user *buffer, struct input_event *
256 return 0; 387 return 0;
257} 388}
258 389
259static int evdev_event_to_user(char __user *buffer, const struct input_event *event) 390static int evdev_event_to_user(char __user *buffer,
391 const struct input_event *event)
260{ 392{
261 if (copy_to_user(buffer, event, sizeof(struct input_event))) 393 if (copy_to_user(buffer, event, sizeof(struct input_event)))
262 return -EFAULT; 394 return -EFAULT;
@@ -266,37 +398,71 @@ static int evdev_event_to_user(char __user *buffer, const struct input_event *ev
266 398
267#endif /* CONFIG_COMPAT */ 399#endif /* CONFIG_COMPAT */
268 400
269static ssize_t evdev_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos) 401static ssize_t evdev_write(struct file *file, const char __user *buffer,
402 size_t count, loff_t *ppos)
270{ 403{
271 struct evdev_client *client = file->private_data; 404 struct evdev_client *client = file->private_data;
272 struct evdev *evdev = client->evdev; 405 struct evdev *evdev = client->evdev;
273 struct input_event event; 406 struct input_event event;
274 int retval = 0; 407 int retval;
275 408
276 if (!evdev->exist) 409 retval = mutex_lock_interruptible(&evdev->mutex);
277 return -ENODEV; 410 if (retval)
411 return retval;
412
413 if (!evdev->exist) {
414 retval = -ENODEV;
415 goto out;
416 }
278 417
279 while (retval < count) { 418 while (retval < count) {
280 419
281 if (evdev_event_from_user(buffer + retval, &event)) 420 if (evdev_event_from_user(buffer + retval, &event)) {
282 return -EFAULT; 421 retval = -EFAULT;
283 input_inject_event(&evdev->handle, event.type, event.code, event.value); 422 goto out;
423 }
424
425 input_inject_event(&evdev->handle,
426 event.type, event.code, event.value);
284 retval += evdev_event_size(); 427 retval += evdev_event_size();
285 } 428 }
286 429
430 out:
431 mutex_unlock(&evdev->mutex);
287 return retval; 432 return retval;
288} 433}
289 434
290static ssize_t evdev_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos) 435static int evdev_fetch_next_event(struct evdev_client *client,
436 struct input_event *event)
437{
438 int have_event;
439
440 spin_lock_irq(&client->buffer_lock);
441
442 have_event = client->head != client->tail;
443 if (have_event) {
444 *event = client->buffer[client->tail++];
445 client->tail &= EVDEV_BUFFER_SIZE - 1;
446 }
447
448 spin_unlock_irq(&client->buffer_lock);
449
450 return have_event;
451}
452
453static ssize_t evdev_read(struct file *file, char __user *buffer,
454 size_t count, loff_t *ppos)
291{ 455{
292 struct evdev_client *client = file->private_data; 456 struct evdev_client *client = file->private_data;
293 struct evdev *evdev = client->evdev; 457 struct evdev *evdev = client->evdev;
458 struct input_event event;
294 int retval; 459 int retval;
295 460
296 if (count < evdev_event_size()) 461 if (count < evdev_event_size())
297 return -EINVAL; 462 return -EINVAL;
298 463
299 if (client->head == client->tail && evdev->exist && (file->f_flags & O_NONBLOCK)) 464 if (client->head == client->tail && evdev->exist &&
465 (file->f_flags & O_NONBLOCK))
300 return -EAGAIN; 466 return -EAGAIN;
301 467
302 retval = wait_event_interruptible(evdev->wait, 468 retval = wait_event_interruptible(evdev->wait,
@@ -307,14 +473,12 @@ static ssize_t evdev_read(struct file *file, char __user *buffer, size_t count,
307 if (!evdev->exist) 473 if (!evdev->exist)
308 return -ENODEV; 474 return -ENODEV;
309 475
310 while (client->head != client->tail && retval + evdev_event_size() <= count) { 476 while (retval + evdev_event_size() <= count &&
477 evdev_fetch_next_event(client, &event)) {
311 478
312 struct input_event *event = (struct input_event *) client->buffer + client->tail; 479 if (evdev_event_to_user(buffer + retval, &event))
313
314 if (evdev_event_to_user(buffer + retval, event))
315 return -EFAULT; 480 return -EFAULT;
316 481
317 client->tail = (client->tail + 1) & (EVDEV_BUFFER_SIZE - 1);
318 retval += evdev_event_size(); 482 retval += evdev_event_size();
319 } 483 }
320 484
@@ -409,8 +573,8 @@ static int str_to_user(const char *str, unsigned int maxlen, void __user *p)
409 return copy_to_user(p, str, len) ? -EFAULT : len; 573 return copy_to_user(p, str, len) ? -EFAULT : len;
410} 574}
411 575
412static long evdev_ioctl_handler(struct file *file, unsigned int cmd, 576static long evdev_do_ioctl(struct file *file, unsigned int cmd,
413 void __user *p, int compat_mode) 577 void __user *p, int compat_mode)
414{ 578{
415 struct evdev_client *client = file->private_data; 579 struct evdev_client *client = file->private_data;
416 struct evdev *evdev = client->evdev; 580 struct evdev *evdev = client->evdev;
@@ -421,215 +585,289 @@ static long evdev_ioctl_handler(struct file *file, unsigned int cmd,
421 int i, t, u, v; 585 int i, t, u, v;
422 int error; 586 int error;
423 587
424 if (!evdev->exist)
425 return -ENODEV;
426
427 switch (cmd) { 588 switch (cmd) {
428 589
429 case EVIOCGVERSION: 590 case EVIOCGVERSION:
430 return put_user(EV_VERSION, ip); 591 return put_user(EV_VERSION, ip);
431 592
432 case EVIOCGID: 593 case EVIOCGID:
433 if (copy_to_user(p, &dev->id, sizeof(struct input_id))) 594 if (copy_to_user(p, &dev->id, sizeof(struct input_id)))
434 return -EFAULT; 595 return -EFAULT;
435 return 0; 596 return 0;
436 597
437 case EVIOCGREP: 598 case EVIOCGREP:
438 if (!test_bit(EV_REP, dev->evbit)) 599 if (!test_bit(EV_REP, dev->evbit))
439 return -ENOSYS; 600 return -ENOSYS;
440 if (put_user(dev->rep[REP_DELAY], ip)) 601 if (put_user(dev->rep[REP_DELAY], ip))
441 return -EFAULT; 602 return -EFAULT;
442 if (put_user(dev->rep[REP_PERIOD], ip + 1)) 603 if (put_user(dev->rep[REP_PERIOD], ip + 1))
443 return -EFAULT; 604 return -EFAULT;
444 return 0; 605 return 0;
445 606
446 case EVIOCSREP: 607 case EVIOCSREP:
447 if (!test_bit(EV_REP, dev->evbit)) 608 if (!test_bit(EV_REP, dev->evbit))
448 return -ENOSYS; 609 return -ENOSYS;
449 if (get_user(u, ip)) 610 if (get_user(u, ip))
450 return -EFAULT; 611 return -EFAULT;
451 if (get_user(v, ip + 1)) 612 if (get_user(v, ip + 1))
452 return -EFAULT; 613 return -EFAULT;
453 614
454 input_inject_event(&evdev->handle, EV_REP, REP_DELAY, u); 615 input_inject_event(&evdev->handle, EV_REP, REP_DELAY, u);
455 input_inject_event(&evdev->handle, EV_REP, REP_PERIOD, v); 616 input_inject_event(&evdev->handle, EV_REP, REP_PERIOD, v);
456 617
457 return 0; 618 return 0;
458 619
459 case EVIOCGKEYCODE: 620 case EVIOCGKEYCODE:
460 if (get_user(t, ip)) 621 if (get_user(t, ip))
461 return -EFAULT; 622 return -EFAULT;
462 623
463 error = dev->getkeycode(dev, t, &v); 624 error = dev->getkeycode(dev, t, &v);
464 if (error) 625 if (error)
465 return error; 626 return error;
466 627
467 if (put_user(v, ip + 1)) 628 if (put_user(v, ip + 1))
468 return -EFAULT; 629 return -EFAULT;
469 630
470 return 0; 631 return 0;
471 632
472 case EVIOCSKEYCODE: 633 case EVIOCSKEYCODE:
473 if (get_user(t, ip) || get_user(v, ip + 1)) 634 if (get_user(t, ip) || get_user(v, ip + 1))
474 return -EFAULT; 635 return -EFAULT;
475 636
476 return dev->setkeycode(dev, t, v); 637 return dev->setkeycode(dev, t, v);
477 638
478 case EVIOCSFF: 639 case EVIOCSFF:
479 if (copy_from_user(&effect, p, sizeof(effect))) 640 if (copy_from_user(&effect, p, sizeof(effect)))
480 return -EFAULT; 641 return -EFAULT;
481 642
482 error = input_ff_upload(dev, &effect, file); 643 error = input_ff_upload(dev, &effect, file);
483 644
484 if (put_user(effect.id, &(((struct ff_effect __user *)p)->id))) 645 if (put_user(effect.id, &(((struct ff_effect __user *)p)->id)))
485 return -EFAULT; 646 return -EFAULT;
486 647
487 return error; 648 return error;
488 649
489 case EVIOCRMFF: 650 case EVIOCRMFF:
490 return input_ff_erase(dev, (int)(unsigned long) p, file); 651 return input_ff_erase(dev, (int)(unsigned long) p, file);
491 652
492 case EVIOCGEFFECTS: 653 case EVIOCGEFFECTS:
493 i = test_bit(EV_FF, dev->evbit) ? dev->ff->max_effects : 0; 654 i = test_bit(EV_FF, dev->evbit) ?
494 if (put_user(i, ip)) 655 dev->ff->max_effects : 0;
495 return -EFAULT; 656 if (put_user(i, ip))
496 return 0; 657 return -EFAULT;
497 658 return 0;
498 case EVIOCGRAB:
499 if (p) {
500 if (evdev->grab)
501 return -EBUSY;
502 if (input_grab_device(&evdev->handle))
503 return -EBUSY;
504 evdev->grab = client;
505 return 0;
506 } else {
507 if (evdev->grab != client)
508 return -EINVAL;
509 input_release_device(&evdev->handle);
510 evdev->grab = NULL;
511 return 0;
512 }
513 659
514 default: 660 case EVIOCGRAB:
661 if (p)
662 return evdev_grab(evdev, client);
663 else
664 return evdev_ungrab(evdev, client);
515 665
516 if (_IOC_TYPE(cmd) != 'E') 666 default:
517 return -EINVAL;
518 667
519 if (_IOC_DIR(cmd) == _IOC_READ) { 668 if (_IOC_TYPE(cmd) != 'E')
669 return -EINVAL;
520 670
521 if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0,0))) { 671 if (_IOC_DIR(cmd) == _IOC_READ) {
522 672
523 unsigned long *bits; 673 if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0, 0))) {
524 int len;
525 674
526 switch (_IOC_NR(cmd) & EV_MAX) { 675 unsigned long *bits;
527 case 0: bits = dev->evbit; len = EV_MAX; break; 676 int len;
528 case EV_KEY: bits = dev->keybit; len = KEY_MAX; break;
529 case EV_REL: bits = dev->relbit; len = REL_MAX; break;
530 case EV_ABS: bits = dev->absbit; len = ABS_MAX; break;
531 case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break;
532 case EV_LED: bits = dev->ledbit; len = LED_MAX; break;
533 case EV_SND: bits = dev->sndbit; len = SND_MAX; break;
534 case EV_FF: bits = dev->ffbit; len = FF_MAX; break;
535 case EV_SW: bits = dev->swbit; len = SW_MAX; break;
536 default: return -EINVAL;
537 }
538 return bits_to_user(bits, len, _IOC_SIZE(cmd), p, compat_mode);
539 }
540 677
541 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGKEY(0))) 678 switch (_IOC_NR(cmd) & EV_MAX) {
542 return bits_to_user(dev->key, KEY_MAX, _IOC_SIZE(cmd),
543 p, compat_mode);
544 679
545 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGLED(0))) 680 case 0: bits = dev->evbit; len = EV_MAX; break;
546 return bits_to_user(dev->led, LED_MAX, _IOC_SIZE(cmd), 681 case EV_KEY: bits = dev->keybit; len = KEY_MAX; break;
547 p, compat_mode); 682 case EV_REL: bits = dev->relbit; len = REL_MAX; break;
683 case EV_ABS: bits = dev->absbit; len = ABS_MAX; break;
684 case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break;
685 case EV_LED: bits = dev->ledbit; len = LED_MAX; break;
686 case EV_SND: bits = dev->sndbit; len = SND_MAX; break;
687 case EV_FF: bits = dev->ffbit; len = FF_MAX; break;
688 case EV_SW: bits = dev->swbit; len = SW_MAX; break;
689 default: return -EINVAL;
690 }
691 return bits_to_user(bits, len, _IOC_SIZE(cmd), p, compat_mode);
692 }
548 693
549 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGSND(0))) 694 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGKEY(0)))
550 return bits_to_user(dev->snd, SND_MAX, _IOC_SIZE(cmd), 695 return bits_to_user(dev->key, KEY_MAX, _IOC_SIZE(cmd),
551 p, compat_mode); 696 p, compat_mode);
552 697
553 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGSW(0))) 698 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGLED(0)))
554 return bits_to_user(dev->sw, SW_MAX, _IOC_SIZE(cmd), 699 return bits_to_user(dev->led, LED_MAX, _IOC_SIZE(cmd),
555 p, compat_mode); 700 p, compat_mode);
556 701
557 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGNAME(0))) 702 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGSND(0)))
558 return str_to_user(dev->name, _IOC_SIZE(cmd), p); 703 return bits_to_user(dev->snd, SND_MAX, _IOC_SIZE(cmd),
704 p, compat_mode);
559 705
560 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGPHYS(0))) 706 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGSW(0)))
561 return str_to_user(dev->phys, _IOC_SIZE(cmd), p); 707 return bits_to_user(dev->sw, SW_MAX, _IOC_SIZE(cmd),
708 p, compat_mode);
562 709
563 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGUNIQ(0))) 710 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGNAME(0)))
564 return str_to_user(dev->uniq, _IOC_SIZE(cmd), p); 711 return str_to_user(dev->name, _IOC_SIZE(cmd), p);
565 712
566 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) { 713 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGPHYS(0)))
714 return str_to_user(dev->phys, _IOC_SIZE(cmd), p);
567 715
568 t = _IOC_NR(cmd) & ABS_MAX; 716 if (_IOC_NR(cmd) == _IOC_NR(EVIOCGUNIQ(0)))
717 return str_to_user(dev->uniq, _IOC_SIZE(cmd), p);
569 718
570 abs.value = dev->abs[t]; 719 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
571 abs.minimum = dev->absmin[t];
572 abs.maximum = dev->absmax[t];
573 abs.fuzz = dev->absfuzz[t];
574 abs.flat = dev->absflat[t];
575 720
576 if (copy_to_user(p, &abs, sizeof(struct input_absinfo))) 721 t = _IOC_NR(cmd) & ABS_MAX;
577 return -EFAULT;
578 722
579 return 0; 723 abs.value = dev->abs[t];
580 } 724 abs.minimum = dev->absmin[t];
725 abs.maximum = dev->absmax[t];
726 abs.fuzz = dev->absfuzz[t];
727 abs.flat = dev->absflat[t];
581 728
729 if (copy_to_user(p, &abs, sizeof(struct input_absinfo)))
730 return -EFAULT;
731
732 return 0;
582 } 733 }
583 734
584 if (_IOC_DIR(cmd) == _IOC_WRITE) { 735 }
585 736
586 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) { 737 if (_IOC_DIR(cmd) == _IOC_WRITE) {
587 738
588 t = _IOC_NR(cmd) & ABS_MAX; 739 if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
589 740
590 if (copy_from_user(&abs, p, sizeof(struct input_absinfo))) 741 t = _IOC_NR(cmd) & ABS_MAX;
591 return -EFAULT;
592 742
593 dev->abs[t] = abs.value; 743 if (copy_from_user(&abs, p,
594 dev->absmin[t] = abs.minimum; 744 sizeof(struct input_absinfo)))
595 dev->absmax[t] = abs.maximum; 745 return -EFAULT;
596 dev->absfuzz[t] = abs.fuzz;
597 dev->absflat[t] = abs.flat;
598 746
599 return 0; 747 /*
600 } 748 * Take event lock to ensure that we are not
749 * changing device parameters in the middle
750 * of event.
751 */
752 spin_lock_irq(&dev->event_lock);
753
754 dev->abs[t] = abs.value;
755 dev->absmin[t] = abs.minimum;
756 dev->absmax[t] = abs.maximum;
757 dev->absfuzz[t] = abs.fuzz;
758 dev->absflat[t] = abs.flat;
759
760 spin_unlock_irq(&dev->event_lock);
761
762 return 0;
601 } 763 }
764 }
602 } 765 }
603 return -EINVAL; 766 return -EINVAL;
604} 767}
605 768
769static long evdev_ioctl_handler(struct file *file, unsigned int cmd,
770 void __user *p, int compat_mode)
771{
772 struct evdev_client *client = file->private_data;
773 struct evdev *evdev = client->evdev;
774 int retval;
775
776 retval = mutex_lock_interruptible(&evdev->mutex);
777 if (retval)
778 return retval;
779
780 if (!evdev->exist) {
781 retval = -ENODEV;
782 goto out;
783 }
784
785 retval = evdev_do_ioctl(file, cmd, p, compat_mode);
786
787 out:
788 mutex_unlock(&evdev->mutex);
789 return retval;
790}
791
606static long evdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 792static long evdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
607{ 793{
608 return evdev_ioctl_handler(file, cmd, (void __user *)arg, 0); 794 return evdev_ioctl_handler(file, cmd, (void __user *)arg, 0);
609} 795}
610 796
611#ifdef CONFIG_COMPAT 797#ifdef CONFIG_COMPAT
612static long evdev_ioctl_compat(struct file *file, unsigned int cmd, unsigned long arg) 798static long evdev_ioctl_compat(struct file *file,
799 unsigned int cmd, unsigned long arg)
613{ 800{
614 return evdev_ioctl_handler(file, cmd, compat_ptr(arg), 1); 801 return evdev_ioctl_handler(file, cmd, compat_ptr(arg), 1);
615} 802}
616#endif 803#endif
617 804
618static const struct file_operations evdev_fops = { 805static const struct file_operations evdev_fops = {
619 .owner = THIS_MODULE, 806 .owner = THIS_MODULE,
620 .read = evdev_read, 807 .read = evdev_read,
621 .write = evdev_write, 808 .write = evdev_write,
622 .poll = evdev_poll, 809 .poll = evdev_poll,
623 .open = evdev_open, 810 .open = evdev_open,
624 .release = evdev_release, 811 .release = evdev_release,
625 .unlocked_ioctl = evdev_ioctl, 812 .unlocked_ioctl = evdev_ioctl,
626#ifdef CONFIG_COMPAT 813#ifdef CONFIG_COMPAT
627 .compat_ioctl = evdev_ioctl_compat, 814 .compat_ioctl = evdev_ioctl_compat,
628#endif 815#endif
629 .fasync = evdev_fasync, 816 .fasync = evdev_fasync,
630 .flush = evdev_flush 817 .flush = evdev_flush
631}; 818};
632 819
820static int evdev_install_chrdev(struct evdev *evdev)
821{
822 /*
823 * No need to do any locking here as calls to connect and
824 * disconnect are serialized by the input core
825 */
826 evdev_table[evdev->minor] = evdev;
827 return 0;
828}
829
830static void evdev_remove_chrdev(struct evdev *evdev)
831{
832 /*
833 * Lock evdev table to prevent race with evdev_open()
834 */
835 mutex_lock(&evdev_table_mutex);
836 evdev_table[evdev->minor] = NULL;
837 mutex_unlock(&evdev_table_mutex);
838}
839
840/*
841 * Mark device non-existent. This disables writes, ioctls and
842 * prevents new users from opening the device. Already posted
843 * blocking reads will stay, however new ones will fail.
844 */
845static void evdev_mark_dead(struct evdev *evdev)
846{
847 mutex_lock(&evdev->mutex);
848 evdev->exist = 0;
849 mutex_unlock(&evdev->mutex);
850}
851
852static void evdev_cleanup(struct evdev *evdev)
853{
854 struct input_handle *handle = &evdev->handle;
855
856 evdev_mark_dead(evdev);
857 evdev_hangup(evdev);
858 evdev_remove_chrdev(evdev);
859
860 /* evdev is marked dead so no one else accesses evdev->open */
861 if (evdev->open) {
862 input_flush_device(handle, NULL);
863 input_close_device(handle);
864 }
865}
866
867/*
868 * Create new evdev device. Note that input core serializes calls
869 * to connect and disconnect so we don't need to lock evdev_table here.
870 */
633static int evdev_connect(struct input_handler *handler, struct input_dev *dev, 871static int evdev_connect(struct input_handler *handler, struct input_dev *dev,
634 const struct input_device_id *id) 872 const struct input_device_id *id)
635{ 873{
@@ -637,7 +875,10 @@ static int evdev_connect(struct input_handler *handler, struct input_dev *dev,
637 int minor; 875 int minor;
638 int error; 876 int error;
639 877
640 for (minor = 0; minor < EVDEV_MINORS && evdev_table[minor]; minor++); 878 for (minor = 0; minor < EVDEV_MINORS; minor++)
879 if (!evdev_table[minor])
880 break;
881
641 if (minor == EVDEV_MINORS) { 882 if (minor == EVDEV_MINORS) {
642 printk(KERN_ERR "evdev: no more free evdev devices\n"); 883 printk(KERN_ERR "evdev: no more free evdev devices\n");
643 return -ENFILE; 884 return -ENFILE;
@@ -648,38 +889,44 @@ static int evdev_connect(struct input_handler *handler, struct input_dev *dev,
648 return -ENOMEM; 889 return -ENOMEM;
649 890
650 INIT_LIST_HEAD(&evdev->client_list); 891 INIT_LIST_HEAD(&evdev->client_list);
892 spin_lock_init(&evdev->client_lock);
893 mutex_init(&evdev->mutex);
651 init_waitqueue_head(&evdev->wait); 894 init_waitqueue_head(&evdev->wait);
652 895
896 snprintf(evdev->name, sizeof(evdev->name), "event%d", minor);
653 evdev->exist = 1; 897 evdev->exist = 1;
654 evdev->minor = minor; 898 evdev->minor = minor;
899
655 evdev->handle.dev = dev; 900 evdev->handle.dev = dev;
656 evdev->handle.name = evdev->name; 901 evdev->handle.name = evdev->name;
657 evdev->handle.handler = handler; 902 evdev->handle.handler = handler;
658 evdev->handle.private = evdev; 903 evdev->handle.private = evdev;
659 snprintf(evdev->name, sizeof(evdev->name), "event%d", minor);
660 904
661 snprintf(evdev->dev.bus_id, sizeof(evdev->dev.bus_id), 905 strlcpy(evdev->dev.bus_id, evdev->name, sizeof(evdev->dev.bus_id));
662 "event%d", minor); 906 evdev->dev.devt = MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor);
663 evdev->dev.class = &input_class; 907 evdev->dev.class = &input_class;
664 evdev->dev.parent = &dev->dev; 908 evdev->dev.parent = &dev->dev;
665 evdev->dev.devt = MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor);
666 evdev->dev.release = evdev_free; 909 evdev->dev.release = evdev_free;
667 device_initialize(&evdev->dev); 910 device_initialize(&evdev->dev);
668 911
669 evdev_table[minor] = evdev; 912 error = input_register_handle(&evdev->handle);
670
671 error = device_add(&evdev->dev);
672 if (error) 913 if (error)
673 goto err_free_evdev; 914 goto err_free_evdev;
674 915
675 error = input_register_handle(&evdev->handle); 916 error = evdev_install_chrdev(evdev);
917 if (error)
918 goto err_unregister_handle;
919
920 error = device_add(&evdev->dev);
676 if (error) 921 if (error)
677 goto err_delete_evdev; 922 goto err_cleanup_evdev;
678 923
679 return 0; 924 return 0;
680 925
681 err_delete_evdev: 926 err_cleanup_evdev:
682 device_del(&evdev->dev); 927 evdev_cleanup(evdev);
928 err_unregister_handle:
929 input_unregister_handle(&evdev->handle);
683 err_free_evdev: 930 err_free_evdev:
684 put_device(&evdev->dev); 931 put_device(&evdev->dev);
685 return error; 932 return error;
@@ -688,21 +935,10 @@ static int evdev_connect(struct input_handler *handler, struct input_dev *dev,
688static void evdev_disconnect(struct input_handle *handle) 935static void evdev_disconnect(struct input_handle *handle)
689{ 936{
690 struct evdev *evdev = handle->private; 937 struct evdev *evdev = handle->private;
691 struct evdev_client *client;
692 938
693 input_unregister_handle(handle);
694 device_del(&evdev->dev); 939 device_del(&evdev->dev);
695 940 evdev_cleanup(evdev);
696 evdev->exist = 0; 941 input_unregister_handle(handle);
697
698 if (evdev->open) {
699 input_flush_device(handle, NULL);
700 input_close_device(handle);
701 list_for_each_entry(client, &evdev->client_list, node)
702 kill_fasync(&client->fasync, SIGIO, POLL_HUP);
703 wake_up_interruptible(&evdev->wait);
704 }
705
706 put_device(&evdev->dev); 942 put_device(&evdev->dev);
707} 943}
708 944
@@ -714,13 +950,13 @@ static const struct input_device_id evdev_ids[] = {
714MODULE_DEVICE_TABLE(input, evdev_ids); 950MODULE_DEVICE_TABLE(input, evdev_ids);
715 951
716static struct input_handler evdev_handler = { 952static struct input_handler evdev_handler = {
717 .event = evdev_event, 953 .event = evdev_event,
718 .connect = evdev_connect, 954 .connect = evdev_connect,
719 .disconnect = evdev_disconnect, 955 .disconnect = evdev_disconnect,
720 .fops = &evdev_fops, 956 .fops = &evdev_fops,
721 .minor = EVDEV_MINOR_BASE, 957 .minor = EVDEV_MINOR_BASE,
722 .name = "evdev", 958 .name = "evdev",
723 .id_table = evdev_ids, 959 .id_table = evdev_ids,
724}; 960};
725 961
726static int __init evdev_init(void) 962static int __init evdev_init(void)
diff --git a/drivers/input/input-polldev.c b/drivers/input/input-polldev.c
index b773d4c756a6..92b359894e81 100644
--- a/drivers/input/input-polldev.c
+++ b/drivers/input/input-polldev.c
@@ -70,6 +70,7 @@ static int input_open_polled_device(struct input_dev *input)
70{ 70{
71 struct input_polled_dev *dev = input->private; 71 struct input_polled_dev *dev = input->private;
72 int error; 72 int error;
73 unsigned long ticks;
73 74
74 error = input_polldev_start_workqueue(); 75 error = input_polldev_start_workqueue();
75 if (error) 76 if (error)
@@ -78,8 +79,10 @@ static int input_open_polled_device(struct input_dev *input)
78 if (dev->flush) 79 if (dev->flush)
79 dev->flush(dev); 80 dev->flush(dev);
80 81
81 queue_delayed_work(polldev_wq, &dev->work, 82 ticks = msecs_to_jiffies(dev->poll_interval);
82 msecs_to_jiffies(dev->poll_interval)); 83 if (ticks >= HZ)
84 ticks = round_jiffies(ticks);
85 queue_delayed_work(polldev_wq, &dev->work, ticks);
83 86
84 return 0; 87 return 0;
85} 88}
diff --git a/drivers/input/input.c b/drivers/input/input.c
index 5dc361c954e2..3070c7aa1237 100644
--- a/drivers/input/input.c
+++ b/drivers/input/input.c
@@ -17,10 +17,10 @@
17#include <linux/major.h> 17#include <linux/major.h>
18#include <linux/proc_fs.h> 18#include <linux/proc_fs.h>
19#include <linux/seq_file.h> 19#include <linux/seq_file.h>
20#include <linux/interrupt.h>
21#include <linux/poll.h> 20#include <linux/poll.h>
22#include <linux/device.h> 21#include <linux/device.h>
23#include <linux/mutex.h> 22#include <linux/mutex.h>
23#include <linux/rcupdate.h>
24 24
25MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>"); 25MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
26MODULE_DESCRIPTION("Input core"); 26MODULE_DESCRIPTION("Input core");
@@ -31,167 +31,244 @@ MODULE_LICENSE("GPL");
31static LIST_HEAD(input_dev_list); 31static LIST_HEAD(input_dev_list);
32static LIST_HEAD(input_handler_list); 32static LIST_HEAD(input_handler_list);
33 33
34/*
35 * input_mutex protects access to both input_dev_list and input_handler_list.
36 * This also causes input_[un]register_device and input_[un]register_handler
37 * be mutually exclusive which simplifies locking in drivers implementing
38 * input handlers.
39 */
40static DEFINE_MUTEX(input_mutex);
41
34static struct input_handler *input_table[8]; 42static struct input_handler *input_table[8];
35 43
36/** 44static inline int is_event_supported(unsigned int code,
37 * input_event() - report new input event 45 unsigned long *bm, unsigned int max)
38 * @dev: device that generated the event
39 * @type: type of the event
40 * @code: event code
41 * @value: value of the event
42 *
43 * This function should be used by drivers implementing various input devices
44 * See also input_inject_event()
45 */
46void input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
47{ 46{
48 struct input_handle *handle; 47 return code <= max && test_bit(code, bm);
48}
49 49
50 if (type > EV_MAX || !test_bit(type, dev->evbit)) 50static int input_defuzz_abs_event(int value, int old_val, int fuzz)
51 return; 51{
52 if (fuzz) {
53 if (value > old_val - fuzz / 2 && value < old_val + fuzz / 2)
54 return old_val;
52 55
53 add_input_randomness(type, code, value); 56 if (value > old_val - fuzz && value < old_val + fuzz)
57 return (old_val * 3 + value) / 4;
54 58
55 switch (type) { 59 if (value > old_val - fuzz * 2 && value < old_val + fuzz * 2)
60 return (old_val + value) / 2;
61 }
56 62
57 case EV_SYN: 63 return value;
58 switch (code) { 64}
59 case SYN_CONFIG:
60 if (dev->event)
61 dev->event(dev, type, code, value);
62 break;
63
64 case SYN_REPORT:
65 if (dev->sync)
66 return;
67 dev->sync = 1;
68 break;
69 }
70 break;
71 65
72 case EV_KEY: 66/*
67 * Pass event through all open handles. This function is called with
68 * dev->event_lock held and interrupts disabled. Because of that we
69 * do not need to use rcu_read_lock() here although we are using RCU
70 * to access handle list. Note that because of that write-side uses
71 * synchronize_sched() instead of synchronize_ru().
72 */
73static void input_pass_event(struct input_dev *dev,
74 unsigned int type, unsigned int code, int value)
75{
76 struct input_handle *handle = rcu_dereference(dev->grab);
73 77
74 if (code > KEY_MAX || !test_bit(code, dev->keybit) || !!test_bit(code, dev->key) == value) 78 if (handle)
75 return; 79 handle->handler->event(handle, type, code, value);
80 else
81 list_for_each_entry_rcu(handle, &dev->h_list, d_node)
82 if (handle->open)
83 handle->handler->event(handle,
84 type, code, value);
85}
76 86
77 if (value == 2) 87/*
78 break; 88 * Generate software autorepeat event. Note that we take
89 * dev->event_lock here to avoid racing with input_event
90 * which may cause keys get "stuck".
91 */
92static void input_repeat_key(unsigned long data)
93{
94 struct input_dev *dev = (void *) data;
95 unsigned long flags;
79 96
80 change_bit(code, dev->key); 97 spin_lock_irqsave(&dev->event_lock, flags);
81 98
82 if (test_bit(EV_REP, dev->evbit) && dev->rep[REP_PERIOD] && dev->rep[REP_DELAY] && dev->timer.data && value) { 99 if (test_bit(dev->repeat_key, dev->key) &&
83 dev->repeat_key = code; 100 is_event_supported(dev->repeat_key, dev->keybit, KEY_MAX)) {
84 mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_DELAY]));
85 }
86 101
87 break; 102 input_pass_event(dev, EV_KEY, dev->repeat_key, 2);
88 103
89 case EV_SW: 104 if (dev->sync) {
105 /*
106 * Only send SYN_REPORT if we are not in a middle
107 * of driver parsing a new hardware packet.
108 * Otherwise assume that the driver will send
109 * SYN_REPORT once it's done.
110 */
111 input_pass_event(dev, EV_SYN, SYN_REPORT, 1);
112 }
90 113
91 if (code > SW_MAX || !test_bit(code, dev->swbit) || !!test_bit(code, dev->sw) == value) 114 if (dev->rep[REP_PERIOD])
92 return; 115 mod_timer(&dev->timer, jiffies +
116 msecs_to_jiffies(dev->rep[REP_PERIOD]));
117 }
93 118
94 change_bit(code, dev->sw); 119 spin_unlock_irqrestore(&dev->event_lock, flags);
120}
95 121
96 break; 122static void input_start_autorepeat(struct input_dev *dev, int code)
123{
124 if (test_bit(EV_REP, dev->evbit) &&
125 dev->rep[REP_PERIOD] && dev->rep[REP_DELAY] &&
126 dev->timer.data) {
127 dev->repeat_key = code;
128 mod_timer(&dev->timer,
129 jiffies + msecs_to_jiffies(dev->rep[REP_DELAY]));
130 }
131}
97 132
98 case EV_ABS: 133#define INPUT_IGNORE_EVENT 0
134#define INPUT_PASS_TO_HANDLERS 1
135#define INPUT_PASS_TO_DEVICE 2
136#define INPUT_PASS_TO_ALL (INPUT_PASS_TO_HANDLERS | INPUT_PASS_TO_DEVICE)
99 137
100 if (code > ABS_MAX || !test_bit(code, dev->absbit)) 138static void input_handle_event(struct input_dev *dev,
101 return; 139 unsigned int type, unsigned int code, int value)
140{
141 int disposition = INPUT_IGNORE_EVENT;
102 142
103 if (dev->absfuzz[code]) { 143 switch (type) {
104 if ((value > dev->abs[code] - (dev->absfuzz[code] >> 1)) &&
105 (value < dev->abs[code] + (dev->absfuzz[code] >> 1)))
106 return;
107 144
108 if ((value > dev->abs[code] - dev->absfuzz[code]) && 145 case EV_SYN:
109 (value < dev->abs[code] + dev->absfuzz[code])) 146 switch (code) {
110 value = (dev->abs[code] * 3 + value) >> 2; 147 case SYN_CONFIG:
148 disposition = INPUT_PASS_TO_ALL;
149 break;
111 150
112 if ((value > dev->abs[code] - (dev->absfuzz[code] << 1)) && 151 case SYN_REPORT:
113 (value < dev->abs[code] + (dev->absfuzz[code] << 1))) 152 if (!dev->sync) {
114 value = (dev->abs[code] + value) >> 1; 153 dev->sync = 1;
154 disposition = INPUT_PASS_TO_HANDLERS;
115 } 155 }
116
117 if (dev->abs[code] == value)
118 return;
119
120 dev->abs[code] = value;
121 break; 156 break;
157 }
158 break;
122 159
123 case EV_REL: 160 case EV_KEY:
161 if (is_event_supported(code, dev->keybit, KEY_MAX) &&
162 !!test_bit(code, dev->key) != value) {
124 163
125 if (code > REL_MAX || !test_bit(code, dev->relbit) || (value == 0)) 164 if (value != 2) {
126 return; 165 __change_bit(code, dev->key);
166 if (value)
167 input_start_autorepeat(dev, code);
168 }
127 169
128 break; 170 disposition = INPUT_PASS_TO_HANDLERS;
171 }
172 break;
129 173
130 case EV_MSC: 174 case EV_SW:
175 if (is_event_supported(code, dev->swbit, SW_MAX) &&
176 !!test_bit(code, dev->sw) != value) {
131 177
132 if (code > MSC_MAX || !test_bit(code, dev->mscbit)) 178 __change_bit(code, dev->sw);
133 return; 179 disposition = INPUT_PASS_TO_HANDLERS;
180 }
181 break;
134 182
135 if (dev->event) 183 case EV_ABS:
136 dev->event(dev, type, code, value); 184 if (is_event_supported(code, dev->absbit, ABS_MAX)) {
137 185
138 break; 186 value = input_defuzz_abs_event(value,
187 dev->abs[code], dev->absfuzz[code]);
139 188
140 case EV_LED: 189 if (dev->abs[code] != value) {
190 dev->abs[code] = value;
191 disposition = INPUT_PASS_TO_HANDLERS;
192 }
193 }
194 break;
141 195
142 if (code > LED_MAX || !test_bit(code, dev->ledbit) || !!test_bit(code, dev->led) == value) 196 case EV_REL:
143 return; 197 if (is_event_supported(code, dev->relbit, REL_MAX) && value)
198 disposition = INPUT_PASS_TO_HANDLERS;
144 199
145 change_bit(code, dev->led); 200 break;
146 201
147 if (dev->event) 202 case EV_MSC:
148 dev->event(dev, type, code, value); 203 if (is_event_supported(code, dev->mscbit, MSC_MAX))
204 disposition = INPUT_PASS_TO_ALL;
149 205
150 break; 206 break;
207
208 case EV_LED:
209 if (is_event_supported(code, dev->ledbit, LED_MAX) &&
210 !!test_bit(code, dev->led) != value) {
151 211
152 case EV_SND: 212 __change_bit(code, dev->led);
213 disposition = INPUT_PASS_TO_ALL;
214 }
215 break;
153 216
154 if (code > SND_MAX || !test_bit(code, dev->sndbit)) 217 case EV_SND:
155 return; 218 if (is_event_supported(code, dev->sndbit, SND_MAX)) {
156 219
157 if (!!test_bit(code, dev->snd) != !!value) 220 if (!!test_bit(code, dev->snd) != !!value)
158 change_bit(code, dev->snd); 221 __change_bit(code, dev->snd);
222 disposition = INPUT_PASS_TO_ALL;
223 }
224 break;
159 225
160 if (dev->event) 226 case EV_REP:
161 dev->event(dev, type, code, value); 227 if (code <= REP_MAX && value >= 0 && dev->rep[code] != value) {
228 dev->rep[code] = value;
229 disposition = INPUT_PASS_TO_ALL;
230 }
231 break;
162 232
163 break; 233 case EV_FF:
234 if (value >= 0)
235 disposition = INPUT_PASS_TO_ALL;
236 break;
237 }
164 238
165 case EV_REP: 239 if (type != EV_SYN)
240 dev->sync = 0;
166 241
167 if (code > REP_MAX || value < 0 || dev->rep[code] == value) 242 if ((disposition & INPUT_PASS_TO_DEVICE) && dev->event)
168 return; 243 dev->event(dev, type, code, value);
169 244
170 dev->rep[code] = value; 245 if (disposition & INPUT_PASS_TO_HANDLERS)
171 if (dev->event) 246 input_pass_event(dev, type, code, value);
172 dev->event(dev, type, code, value); 247}
173 248
174 break; 249/**
250 * input_event() - report new input event
251 * @dev: device that generated the event
252 * @type: type of the event
253 * @code: event code
254 * @value: value of the event
255 *
256 * This function should be used by drivers implementing various input
257 * devices. See also input_inject_event().
258 */
175 259
176 case EV_FF: 260void input_event(struct input_dev *dev,
261 unsigned int type, unsigned int code, int value)
262{
263 unsigned long flags;
177 264
178 if (value < 0) 265 if (is_event_supported(type, dev->evbit, EV_MAX)) {
179 return;
180 266
181 if (dev->event) 267 spin_lock_irqsave(&dev->event_lock, flags);
182 dev->event(dev, type, code, value); 268 add_input_randomness(type, code, value);
183 break; 269 input_handle_event(dev, type, code, value);
270 spin_unlock_irqrestore(&dev->event_lock, flags);
184 } 271 }
185
186 if (type != EV_SYN)
187 dev->sync = 0;
188
189 if (dev->grab)
190 dev->grab->handler->event(dev->grab, type, code, value);
191 else
192 list_for_each_entry(handle, &dev->h_list, d_node)
193 if (handle->open)
194 handle->handler->event(handle, type, code, value);
195} 272}
196EXPORT_SYMBOL(input_event); 273EXPORT_SYMBOL(input_event);
197 274
@@ -202,102 +279,230 @@ EXPORT_SYMBOL(input_event);
202 * @code: event code 279 * @code: event code
203 * @value: value of the event 280 * @value: value of the event
204 * 281 *
205 * Similar to input_event() but will ignore event if device is "grabbed" and handle 282 * Similar to input_event() but will ignore event if device is
206 * injecting event is not the one that owns the device. 283 * "grabbed" and handle injecting event is not the one that owns
284 * the device.
207 */ 285 */
208void input_inject_event(struct input_handle *handle, unsigned int type, unsigned int code, int value) 286void input_inject_event(struct input_handle *handle,
287 unsigned int type, unsigned int code, int value)
209{ 288{
210 if (!handle->dev->grab || handle->dev->grab == handle) 289 struct input_dev *dev = handle->dev;
211 input_event(handle->dev, type, code, value); 290 struct input_handle *grab;
212} 291 unsigned long flags;
213EXPORT_SYMBOL(input_inject_event);
214
215static void input_repeat_key(unsigned long data)
216{
217 struct input_dev *dev = (void *) data;
218 292
219 if (!test_bit(dev->repeat_key, dev->key)) 293 if (is_event_supported(type, dev->evbit, EV_MAX)) {
220 return; 294 spin_lock_irqsave(&dev->event_lock, flags);
221 295
222 input_event(dev, EV_KEY, dev->repeat_key, 2); 296 grab = rcu_dereference(dev->grab);
223 input_sync(dev); 297 if (!grab || grab == handle)
298 input_handle_event(dev, type, code, value);
224 299
225 if (dev->rep[REP_PERIOD]) 300 spin_unlock_irqrestore(&dev->event_lock, flags);
226 mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_PERIOD])); 301 }
227} 302}
303EXPORT_SYMBOL(input_inject_event);
228 304
305/**
306 * input_grab_device - grabs device for exclusive use
307 * @handle: input handle that wants to own the device
308 *
309 * When a device is grabbed by an input handle all events generated by
310 * the device are delivered only to this handle. Also events injected
311 * by other input handles are ignored while device is grabbed.
312 */
229int input_grab_device(struct input_handle *handle) 313int input_grab_device(struct input_handle *handle)
230{ 314{
231 if (handle->dev->grab) 315 struct input_dev *dev = handle->dev;
232 return -EBUSY; 316 int retval;
233 317
234 handle->dev->grab = handle; 318 retval = mutex_lock_interruptible(&dev->mutex);
235 return 0; 319 if (retval)
320 return retval;
321
322 if (dev->grab) {
323 retval = -EBUSY;
324 goto out;
325 }
326
327 rcu_assign_pointer(dev->grab, handle);
328 /*
329 * Not using synchronize_rcu() because read-side is protected
330 * by a spinlock with interrupts off instead of rcu_read_lock().
331 */
332 synchronize_sched();
333
334 out:
335 mutex_unlock(&dev->mutex);
336 return retval;
236} 337}
237EXPORT_SYMBOL(input_grab_device); 338EXPORT_SYMBOL(input_grab_device);
238 339
239void input_release_device(struct input_handle *handle) 340static void __input_release_device(struct input_handle *handle)
240{ 341{
241 struct input_dev *dev = handle->dev; 342 struct input_dev *dev = handle->dev;
242 343
243 if (dev->grab == handle) { 344 if (dev->grab == handle) {
244 dev->grab = NULL; 345 rcu_assign_pointer(dev->grab, NULL);
346 /* Make sure input_pass_event() notices that grab is gone */
347 synchronize_sched();
245 348
246 list_for_each_entry(handle, &dev->h_list, d_node) 349 list_for_each_entry(handle, &dev->h_list, d_node)
247 if (handle->handler->start) 350 if (handle->open && handle->handler->start)
248 handle->handler->start(handle); 351 handle->handler->start(handle);
249 } 352 }
250} 353}
354
355/**
356 * input_release_device - release previously grabbed device
357 * @handle: input handle that owns the device
358 *
359 * Releases previously grabbed device so that other input handles can
360 * start receiving input events. Upon release all handlers attached
361 * to the device have their start() method called so they have a change
362 * to synchronize device state with the rest of the system.
363 */
364void input_release_device(struct input_handle *handle)
365{
366 struct input_dev *dev = handle->dev;
367
368 mutex_lock(&dev->mutex);
369 __input_release_device(handle);
370 mutex_unlock(&dev->mutex);
371}
251EXPORT_SYMBOL(input_release_device); 372EXPORT_SYMBOL(input_release_device);
252 373
374/**
375 * input_open_device - open input device
376 * @handle: handle through which device is being accessed
377 *
378 * This function should be called by input handlers when they
379 * want to start receive events from given input device.
380 */
253int input_open_device(struct input_handle *handle) 381int input_open_device(struct input_handle *handle)
254{ 382{
255 struct input_dev *dev = handle->dev; 383 struct input_dev *dev = handle->dev;
256 int err; 384 int retval;
257 385
258 err = mutex_lock_interruptible(&dev->mutex); 386 retval = mutex_lock_interruptible(&dev->mutex);
259 if (err) 387 if (retval)
260 return err; 388 return retval;
389
390 if (dev->going_away) {
391 retval = -ENODEV;
392 goto out;
393 }
261 394
262 handle->open++; 395 handle->open++;
263 396
264 if (!dev->users++ && dev->open) 397 if (!dev->users++ && dev->open)
265 err = dev->open(dev); 398 retval = dev->open(dev);
266 399
267 if (err) 400 if (retval) {
268 handle->open--; 401 dev->users--;
402 if (!--handle->open) {
403 /*
404 * Make sure we are not delivering any more events
405 * through this handle
406 */
407 synchronize_sched();
408 }
409 }
269 410
411 out:
270 mutex_unlock(&dev->mutex); 412 mutex_unlock(&dev->mutex);
271 413 return retval;
272 return err;
273} 414}
274EXPORT_SYMBOL(input_open_device); 415EXPORT_SYMBOL(input_open_device);
275 416
276int input_flush_device(struct input_handle* handle, struct file* file) 417int input_flush_device(struct input_handle *handle, struct file *file)
277{ 418{
278 if (handle->dev->flush) 419 struct input_dev *dev = handle->dev;
279 return handle->dev->flush(handle->dev, file); 420 int retval;
280 421
281 return 0; 422 retval = mutex_lock_interruptible(&dev->mutex);
423 if (retval)
424 return retval;
425
426 if (dev->flush)
427 retval = dev->flush(dev, file);
428
429 mutex_unlock(&dev->mutex);
430 return retval;
282} 431}
283EXPORT_SYMBOL(input_flush_device); 432EXPORT_SYMBOL(input_flush_device);
284 433
434/**
435 * input_close_device - close input device
436 * @handle: handle through which device is being accessed
437 *
438 * This function should be called by input handlers when they
439 * want to stop receive events from given input device.
440 */
285void input_close_device(struct input_handle *handle) 441void input_close_device(struct input_handle *handle)
286{ 442{
287 struct input_dev *dev = handle->dev; 443 struct input_dev *dev = handle->dev;
288 444
289 input_release_device(handle);
290
291 mutex_lock(&dev->mutex); 445 mutex_lock(&dev->mutex);
292 446
447 __input_release_device(handle);
448
293 if (!--dev->users && dev->close) 449 if (!--dev->users && dev->close)
294 dev->close(dev); 450 dev->close(dev);
295 handle->open--; 451
452 if (!--handle->open) {
453 /*
454 * synchronize_sched() makes sure that input_pass_event()
455 * completed and that no more input events are delivered
456 * through this handle
457 */
458 synchronize_sched();
459 }
296 460
297 mutex_unlock(&dev->mutex); 461 mutex_unlock(&dev->mutex);
298} 462}
299EXPORT_SYMBOL(input_close_device); 463EXPORT_SYMBOL(input_close_device);
300 464
465/*
466 * Prepare device for unregistering
467 */
468static void input_disconnect_device(struct input_dev *dev)
469{
470 struct input_handle *handle;
471 int code;
472
473 /*
474 * Mark device as going away. Note that we take dev->mutex here
475 * not to protect access to dev->going_away but rather to ensure
476 * that there are no threads in the middle of input_open_device()
477 */
478 mutex_lock(&dev->mutex);
479 dev->going_away = 1;
480 mutex_unlock(&dev->mutex);
481
482 spin_lock_irq(&dev->event_lock);
483
484 /*
485 * Simulate keyup events for all pressed keys so that handlers
486 * are not left with "stuck" keys. The driver may continue
487 * generate events even after we done here but they will not
488 * reach any handlers.
489 */
490 if (is_event_supported(EV_KEY, dev->evbit, EV_MAX)) {
491 for (code = 0; code <= KEY_MAX; code++) {
492 if (is_event_supported(code, dev->keybit, KEY_MAX) &&
493 test_bit(code, dev->key)) {
494 input_pass_event(dev, EV_KEY, code, 0);
495 }
496 }
497 input_pass_event(dev, EV_SYN, SYN_REPORT, 1);
498 }
499
500 list_for_each_entry(handle, &dev->h_list, d_node)
501 handle->open = 0;
502
503 spin_unlock_irq(&dev->event_lock);
504}
505
301static int input_fetch_keycode(struct input_dev *dev, int scancode) 506static int input_fetch_keycode(struct input_dev *dev, int scancode)
302{ 507{
303 switch (dev->keycodesize) { 508 switch (dev->keycodesize) {
@@ -473,7 +678,8 @@ static unsigned int input_proc_devices_poll(struct file *file, poll_table *wait)
473 678
474static void *input_devices_seq_start(struct seq_file *seq, loff_t *pos) 679static void *input_devices_seq_start(struct seq_file *seq, loff_t *pos)
475{ 680{
476 /* acquire lock here ... Yes, we do need locking, I knowi, I know... */ 681 if (mutex_lock_interruptible(&input_mutex))
682 return NULL;
477 683
478 return seq_list_start(&input_dev_list, *pos); 684 return seq_list_start(&input_dev_list, *pos);
479} 685}
@@ -485,7 +691,7 @@ static void *input_devices_seq_next(struct seq_file *seq, void *v, loff_t *pos)
485 691
486static void input_devices_seq_stop(struct seq_file *seq, void *v) 692static void input_devices_seq_stop(struct seq_file *seq, void *v)
487{ 693{
488 /* release lock here */ 694 mutex_unlock(&input_mutex);
489} 695}
490 696
491static void input_seq_print_bitmap(struct seq_file *seq, const char *name, 697static void input_seq_print_bitmap(struct seq_file *seq, const char *name,
@@ -569,7 +775,9 @@ static const struct file_operations input_devices_fileops = {
569 775
570static void *input_handlers_seq_start(struct seq_file *seq, loff_t *pos) 776static void *input_handlers_seq_start(struct seq_file *seq, loff_t *pos)
571{ 777{
572 /* acquire lock here ... Yes, we do need locking, I knowi, I know... */ 778 if (mutex_lock_interruptible(&input_mutex))
779 return NULL;
780
573 seq->private = (void *)(unsigned long)*pos; 781 seq->private = (void *)(unsigned long)*pos;
574 return seq_list_start(&input_handler_list, *pos); 782 return seq_list_start(&input_handler_list, *pos);
575} 783}
@@ -582,7 +790,7 @@ static void *input_handlers_seq_next(struct seq_file *seq, void *v, loff_t *pos)
582 790
583static void input_handlers_seq_stop(struct seq_file *seq, void *v) 791static void input_handlers_seq_stop(struct seq_file *seq, void *v)
584{ 792{
585 /* release lock here */ 793 mutex_unlock(&input_mutex);
586} 794}
587 795
588static int input_handlers_seq_show(struct seq_file *seq, void *v) 796static int input_handlers_seq_show(struct seq_file *seq, void *v)
@@ -983,6 +1191,7 @@ struct input_dev *input_allocate_device(void)
983 dev->dev.class = &input_class; 1191 dev->dev.class = &input_class;
984 device_initialize(&dev->dev); 1192 device_initialize(&dev->dev);
985 mutex_init(&dev->mutex); 1193 mutex_init(&dev->mutex);
1194 spin_lock_init(&dev->event_lock);
986 INIT_LIST_HEAD(&dev->h_list); 1195 INIT_LIST_HEAD(&dev->h_list);
987 INIT_LIST_HEAD(&dev->node); 1196 INIT_LIST_HEAD(&dev->node);
988 1197
@@ -1000,7 +1209,7 @@ EXPORT_SYMBOL(input_allocate_device);
1000 * This function should only be used if input_register_device() 1209 * This function should only be used if input_register_device()
1001 * was not called yet or if it failed. Once device was registered 1210 * was not called yet or if it failed. Once device was registered
1002 * use input_unregister_device() and memory will be freed once last 1211 * use input_unregister_device() and memory will be freed once last
1003 * refrence to the device is dropped. 1212 * reference to the device is dropped.
1004 * 1213 *
1005 * Device should be allocated by input_allocate_device(). 1214 * Device should be allocated by input_allocate_device().
1006 * 1215 *
@@ -1070,6 +1279,18 @@ void input_set_capability(struct input_dev *dev, unsigned int type, unsigned int
1070} 1279}
1071EXPORT_SYMBOL(input_set_capability); 1280EXPORT_SYMBOL(input_set_capability);
1072 1281
1282/**
1283 * input_register_device - register device with input core
1284 * @dev: device to be registered
1285 *
1286 * This function registers device with input core. The device must be
1287 * allocated with input_allocate_device() and all it's capabilities
1288 * set up before registering.
1289 * If function fails the device must be freed with input_free_device().
1290 * Once device has been successfully registered it can be unregistered
1291 * with input_unregister_device(); input_free_device() should not be
1292 * called in this case.
1293 */
1073int input_register_device(struct input_dev *dev) 1294int input_register_device(struct input_dev *dev)
1074{ 1295{
1075 static atomic_t input_no = ATOMIC_INIT(0); 1296 static atomic_t input_no = ATOMIC_INIT(0);
@@ -1077,7 +1298,7 @@ int input_register_device(struct input_dev *dev)
1077 const char *path; 1298 const char *path;
1078 int error; 1299 int error;
1079 1300
1080 set_bit(EV_SYN, dev->evbit); 1301 __set_bit(EV_SYN, dev->evbit);
1081 1302
1082 /* 1303 /*
1083 * If delay and period are pre-set by the driver, then autorepeating 1304 * If delay and period are pre-set by the driver, then autorepeating
@@ -1098,8 +1319,6 @@ int input_register_device(struct input_dev *dev)
1098 if (!dev->setkeycode) 1319 if (!dev->setkeycode)
1099 dev->setkeycode = input_default_setkeycode; 1320 dev->setkeycode = input_default_setkeycode;
1100 1321
1101 list_add_tail(&dev->node, &input_dev_list);
1102
1103 snprintf(dev->dev.bus_id, sizeof(dev->dev.bus_id), 1322 snprintf(dev->dev.bus_id, sizeof(dev->dev.bus_id),
1104 "input%ld", (unsigned long) atomic_inc_return(&input_no) - 1); 1323 "input%ld", (unsigned long) atomic_inc_return(&input_no) - 1);
1105 1324
@@ -1115,49 +1334,79 @@ int input_register_device(struct input_dev *dev)
1115 dev->name ? dev->name : "Unspecified device", path ? path : "N/A"); 1334 dev->name ? dev->name : "Unspecified device", path ? path : "N/A");
1116 kfree(path); 1335 kfree(path);
1117 1336
1337 error = mutex_lock_interruptible(&input_mutex);
1338 if (error) {
1339 device_del(&dev->dev);
1340 return error;
1341 }
1342
1343 list_add_tail(&dev->node, &input_dev_list);
1344
1118 list_for_each_entry(handler, &input_handler_list, node) 1345 list_for_each_entry(handler, &input_handler_list, node)
1119 input_attach_handler(dev, handler); 1346 input_attach_handler(dev, handler);
1120 1347
1121 input_wakeup_procfs_readers(); 1348 input_wakeup_procfs_readers();
1122 1349
1350 mutex_unlock(&input_mutex);
1351
1123 return 0; 1352 return 0;
1124} 1353}
1125EXPORT_SYMBOL(input_register_device); 1354EXPORT_SYMBOL(input_register_device);
1126 1355
1356/**
1357 * input_unregister_device - unregister previously registered device
1358 * @dev: device to be unregistered
1359 *
1360 * This function unregisters an input device. Once device is unregistered
1361 * the caller should not try to access it as it may get freed at any moment.
1362 */
1127void input_unregister_device(struct input_dev *dev) 1363void input_unregister_device(struct input_dev *dev)
1128{ 1364{
1129 struct input_handle *handle, *next; 1365 struct input_handle *handle, *next;
1130 int code;
1131 1366
1132 for (code = 0; code <= KEY_MAX; code++) 1367 input_disconnect_device(dev);
1133 if (test_bit(code, dev->key))
1134 input_report_key(dev, code, 0);
1135 input_sync(dev);
1136 1368
1137 del_timer_sync(&dev->timer); 1369 mutex_lock(&input_mutex);
1138 1370
1139 list_for_each_entry_safe(handle, next, &dev->h_list, d_node) 1371 list_for_each_entry_safe(handle, next, &dev->h_list, d_node)
1140 handle->handler->disconnect(handle); 1372 handle->handler->disconnect(handle);
1141 WARN_ON(!list_empty(&dev->h_list)); 1373 WARN_ON(!list_empty(&dev->h_list));
1142 1374
1375 del_timer_sync(&dev->timer);
1143 list_del_init(&dev->node); 1376 list_del_init(&dev->node);
1144 1377
1145 device_unregister(&dev->dev);
1146
1147 input_wakeup_procfs_readers(); 1378 input_wakeup_procfs_readers();
1379
1380 mutex_unlock(&input_mutex);
1381
1382 device_unregister(&dev->dev);
1148} 1383}
1149EXPORT_SYMBOL(input_unregister_device); 1384EXPORT_SYMBOL(input_unregister_device);
1150 1385
1386/**
1387 * input_register_handler - register a new input handler
1388 * @handler: handler to be registered
1389 *
1390 * This function registers a new input handler (interface) for input
1391 * devices in the system and attaches it to all input devices that
1392 * are compatible with the handler.
1393 */
1151int input_register_handler(struct input_handler *handler) 1394int input_register_handler(struct input_handler *handler)
1152{ 1395{
1153 struct input_dev *dev; 1396 struct input_dev *dev;
1397 int retval;
1398
1399 retval = mutex_lock_interruptible(&input_mutex);
1400 if (retval)
1401 return retval;
1154 1402
1155 INIT_LIST_HEAD(&handler->h_list); 1403 INIT_LIST_HEAD(&handler->h_list);
1156 1404
1157 if (handler->fops != NULL) { 1405 if (handler->fops != NULL) {
1158 if (input_table[handler->minor >> 5]) 1406 if (input_table[handler->minor >> 5]) {
1159 return -EBUSY; 1407 retval = -EBUSY;
1160 1408 goto out;
1409 }
1161 input_table[handler->minor >> 5] = handler; 1410 input_table[handler->minor >> 5] = handler;
1162 } 1411 }
1163 1412
@@ -1167,14 +1416,26 @@ int input_register_handler(struct input_handler *handler)
1167 input_attach_handler(dev, handler); 1416 input_attach_handler(dev, handler);
1168 1417
1169 input_wakeup_procfs_readers(); 1418 input_wakeup_procfs_readers();
1170 return 0; 1419
1420 out:
1421 mutex_unlock(&input_mutex);
1422 return retval;
1171} 1423}
1172EXPORT_SYMBOL(input_register_handler); 1424EXPORT_SYMBOL(input_register_handler);
1173 1425
1426/**
1427 * input_unregister_handler - unregisters an input handler
1428 * @handler: handler to be unregistered
1429 *
1430 * This function disconnects a handler from its input devices and
1431 * removes it from lists of known handlers.
1432 */
1174void input_unregister_handler(struct input_handler *handler) 1433void input_unregister_handler(struct input_handler *handler)
1175{ 1434{
1176 struct input_handle *handle, *next; 1435 struct input_handle *handle, *next;
1177 1436
1437 mutex_lock(&input_mutex);
1438
1178 list_for_each_entry_safe(handle, next, &handler->h_list, h_node) 1439 list_for_each_entry_safe(handle, next, &handler->h_list, h_node)
1179 handler->disconnect(handle); 1440 handler->disconnect(handle);
1180 WARN_ON(!list_empty(&handler->h_list)); 1441 WARN_ON(!list_empty(&handler->h_list));
@@ -1185,14 +1446,50 @@ void input_unregister_handler(struct input_handler *handler)
1185 input_table[handler->minor >> 5] = NULL; 1446 input_table[handler->minor >> 5] = NULL;
1186 1447
1187 input_wakeup_procfs_readers(); 1448 input_wakeup_procfs_readers();
1449
1450 mutex_unlock(&input_mutex);
1188} 1451}
1189EXPORT_SYMBOL(input_unregister_handler); 1452EXPORT_SYMBOL(input_unregister_handler);
1190 1453
1454/**
1455 * input_register_handle - register a new input handle
1456 * @handle: handle to register
1457 *
1458 * This function puts a new input handle onto device's
1459 * and handler's lists so that events can flow through
1460 * it once it is opened using input_open_device().
1461 *
1462 * This function is supposed to be called from handler's
1463 * connect() method.
1464 */
1191int input_register_handle(struct input_handle *handle) 1465int input_register_handle(struct input_handle *handle)
1192{ 1466{
1193 struct input_handler *handler = handle->handler; 1467 struct input_handler *handler = handle->handler;
1468 struct input_dev *dev = handle->dev;
1469 int error;
1470
1471 /*
1472 * We take dev->mutex here to prevent race with
1473 * input_release_device().
1474 */
1475 error = mutex_lock_interruptible(&dev->mutex);
1476 if (error)
1477 return error;
1478 list_add_tail_rcu(&handle->d_node, &dev->h_list);
1479 mutex_unlock(&dev->mutex);
1480 /*
1481 * We don't use synchronize_rcu() here because we rely
1482 * on dev->event_lock to protect read-side critical
1483 * section in input_pass_event().
1484 */
1485 synchronize_sched();
1194 1486
1195 list_add_tail(&handle->d_node, &handle->dev->h_list); 1487 /*
1488 * Since we are supposed to be called from ->connect()
1489 * which is mutually exclusive with ->disconnect()
1490 * we can't be racing with input_unregister_handle()
1491 * and so separate lock is not needed here.
1492 */
1196 list_add_tail(&handle->h_node, &handler->h_list); 1493 list_add_tail(&handle->h_node, &handler->h_list);
1197 1494
1198 if (handler->start) 1495 if (handler->start)
@@ -1202,10 +1499,29 @@ int input_register_handle(struct input_handle *handle)
1202} 1499}
1203EXPORT_SYMBOL(input_register_handle); 1500EXPORT_SYMBOL(input_register_handle);
1204 1501
1502/**
1503 * input_unregister_handle - unregister an input handle
1504 * @handle: handle to unregister
1505 *
1506 * This function removes input handle from device's
1507 * and handler's lists.
1508 *
1509 * This function is supposed to be called from handler's
1510 * disconnect() method.
1511 */
1205void input_unregister_handle(struct input_handle *handle) 1512void input_unregister_handle(struct input_handle *handle)
1206{ 1513{
1514 struct input_dev *dev = handle->dev;
1515
1207 list_del_init(&handle->h_node); 1516 list_del_init(&handle->h_node);
1208 list_del_init(&handle->d_node); 1517
1518 /*
1519 * Take dev->mutex to prevent race with input_release_device().
1520 */
1521 mutex_lock(&dev->mutex);
1522 list_del_rcu(&handle->d_node);
1523 mutex_unlock(&dev->mutex);
1524 synchronize_sched();
1209} 1525}
1210EXPORT_SYMBOL(input_unregister_handle); 1526EXPORT_SYMBOL(input_unregister_handle);
1211 1527
diff --git a/drivers/input/joydev.c b/drivers/input/joydev.c
index a9a0180bfd46..f306c97f556d 100644
--- a/drivers/input/joydev.c
+++ b/drivers/input/joydev.c
@@ -43,6 +43,8 @@ struct joydev {
43 struct input_handle handle; 43 struct input_handle handle;
44 wait_queue_head_t wait; 44 wait_queue_head_t wait;
45 struct list_head client_list; 45 struct list_head client_list;
46 spinlock_t client_lock; /* protects client_list */
47 struct mutex mutex;
46 struct device dev; 48 struct device dev;
47 49
48 struct js_corr corr[ABS_MAX + 1]; 50 struct js_corr corr[ABS_MAX + 1];
@@ -61,31 +63,61 @@ struct joydev_client {
61 int head; 63 int head;
62 int tail; 64 int tail;
63 int startup; 65 int startup;
66 spinlock_t buffer_lock; /* protects access to buffer, head and tail */
64 struct fasync_struct *fasync; 67 struct fasync_struct *fasync;
65 struct joydev *joydev; 68 struct joydev *joydev;
66 struct list_head node; 69 struct list_head node;
67}; 70};
68 71
69static struct joydev *joydev_table[JOYDEV_MINORS]; 72static struct joydev *joydev_table[JOYDEV_MINORS];
73static DEFINE_MUTEX(joydev_table_mutex);
70 74
71static int joydev_correct(int value, struct js_corr *corr) 75static int joydev_correct(int value, struct js_corr *corr)
72{ 76{
73 switch (corr->type) { 77 switch (corr->type) {
74 case JS_CORR_NONE: 78
75 break; 79 case JS_CORR_NONE:
76 case JS_CORR_BROKEN: 80 break;
77 value = value > corr->coef[0] ? (value < corr->coef[1] ? 0 : 81
78 ((corr->coef[3] * (value - corr->coef[1])) >> 14)) : 82 case JS_CORR_BROKEN:
79 ((corr->coef[2] * (value - corr->coef[0])) >> 14); 83 value = value > corr->coef[0] ? (value < corr->coef[1] ? 0 :
80 break; 84 ((corr->coef[3] * (value - corr->coef[1])) >> 14)) :
81 default: 85 ((corr->coef[2] * (value - corr->coef[0])) >> 14);
82 return 0; 86 break;
87
88 default:
89 return 0;
83 } 90 }
84 91
85 return value < -32767 ? -32767 : (value > 32767 ? 32767 : value); 92 return value < -32767 ? -32767 : (value > 32767 ? 32767 : value);
86} 93}
87 94
88static void joydev_event(struct input_handle *handle, unsigned int type, unsigned int code, int value) 95static void joydev_pass_event(struct joydev_client *client,
96 struct js_event *event)
97{
98 struct joydev *joydev = client->joydev;
99
100 /*
101 * IRQs already disabled, just acquire the lock
102 */
103 spin_lock(&client->buffer_lock);
104
105 client->buffer[client->head] = *event;
106
107 if (client->startup == joydev->nabs + joydev->nkey) {
108 client->head++;
109 client->head &= JOYDEV_BUFFER_SIZE - 1;
110 if (client->tail == client->head)
111 client->startup = 0;
112 }
113
114 spin_unlock(&client->buffer_lock);
115
116 kill_fasync(&client->fasync, SIGIO, POLL_IN);
117}
118
119static void joydev_event(struct input_handle *handle,
120 unsigned int type, unsigned int code, int value)
89{ 121{
90 struct joydev *joydev = handle->private; 122 struct joydev *joydev = handle->private;
91 struct joydev_client *client; 123 struct joydev_client *client;
@@ -93,39 +125,32 @@ static void joydev_event(struct input_handle *handle, unsigned int type, unsigne
93 125
94 switch (type) { 126 switch (type) {
95 127
96 case EV_KEY: 128 case EV_KEY:
97 if (code < BTN_MISC || value == 2) 129 if (code < BTN_MISC || value == 2)
98 return; 130 return;
99 event.type = JS_EVENT_BUTTON; 131 event.type = JS_EVENT_BUTTON;
100 event.number = joydev->keymap[code - BTN_MISC]; 132 event.number = joydev->keymap[code - BTN_MISC];
101 event.value = value; 133 event.value = value;
102 break; 134 break;
103
104 case EV_ABS:
105 event.type = JS_EVENT_AXIS;
106 event.number = joydev->absmap[code];
107 event.value = joydev_correct(value, joydev->corr + event.number);
108 if (event.value == joydev->abs[event.number])
109 return;
110 joydev->abs[event.number] = event.value;
111 break;
112 135
113 default: 136 case EV_ABS:
137 event.type = JS_EVENT_AXIS;
138 event.number = joydev->absmap[code];
139 event.value = joydev_correct(value,
140 &joydev->corr[event.number]);
141 if (event.value == joydev->abs[event.number])
114 return; 142 return;
143 joydev->abs[event.number] = event.value;
144 break;
145
146 default:
147 return;
115 } 148 }
116 149
117 event.time = jiffies_to_msecs(jiffies); 150 event.time = jiffies_to_msecs(jiffies);
118 151
119 list_for_each_entry(client, &joydev->client_list, node) { 152 list_for_each_entry_rcu(client, &joydev->client_list, node)
120 153 joydev_pass_event(client, &event);
121 memcpy(client->buffer + client->head, &event, sizeof(struct js_event));
122
123 if (client->startup == joydev->nabs + joydev->nkey)
124 if (client->tail == (client->head = (client->head + 1) & (JOYDEV_BUFFER_SIZE - 1)))
125 client->startup = 0;
126
127 kill_fasync(&client->fasync, SIGIO, POLL_IN);
128 }
129 154
130 wake_up_interruptible(&joydev->wait); 155 wake_up_interruptible(&joydev->wait);
131} 156}
@@ -144,23 +169,88 @@ static void joydev_free(struct device *dev)
144{ 169{
145 struct joydev *joydev = container_of(dev, struct joydev, dev); 170 struct joydev *joydev = container_of(dev, struct joydev, dev);
146 171
147 joydev_table[joydev->minor] = NULL;
148 kfree(joydev); 172 kfree(joydev);
149} 173}
150 174
175static void joydev_attach_client(struct joydev *joydev,
176 struct joydev_client *client)
177{
178 spin_lock(&joydev->client_lock);
179 list_add_tail_rcu(&client->node, &joydev->client_list);
180 spin_unlock(&joydev->client_lock);
181 /*
182 * We don't use synchronize_rcu() here because read-side
183 * critical section is protected by a spinlock (dev->event_lock)
184 * instead of rcu_read_lock().
185 */
186 synchronize_sched();
187}
188
189static void joydev_detach_client(struct joydev *joydev,
190 struct joydev_client *client)
191{
192 spin_lock(&joydev->client_lock);
193 list_del_rcu(&client->node);
194 spin_unlock(&joydev->client_lock);
195 synchronize_sched();
196}
197
198static int joydev_open_device(struct joydev *joydev)
199{
200 int retval;
201
202 retval = mutex_lock_interruptible(&joydev->mutex);
203 if (retval)
204 return retval;
205
206 if (!joydev->exist)
207 retval = -ENODEV;
208 else if (!joydev->open++) {
209 retval = input_open_device(&joydev->handle);
210 if (retval)
211 joydev->open--;
212 }
213
214 mutex_unlock(&joydev->mutex);
215 return retval;
216}
217
218static void joydev_close_device(struct joydev *joydev)
219{
220 mutex_lock(&joydev->mutex);
221
222 if (joydev->exist && !--joydev->open)
223 input_close_device(&joydev->handle);
224
225 mutex_unlock(&joydev->mutex);
226}
227
228/*
229 * Wake up users waiting for IO so they can disconnect from
230 * dead device.
231 */
232static void joydev_hangup(struct joydev *joydev)
233{
234 struct joydev_client *client;
235
236 spin_lock(&joydev->client_lock);
237 list_for_each_entry(client, &joydev->client_list, node)
238 kill_fasync(&client->fasync, SIGIO, POLL_HUP);
239 spin_unlock(&joydev->client_lock);
240
241 wake_up_interruptible(&joydev->wait);
242}
243
151static int joydev_release(struct inode *inode, struct file *file) 244static int joydev_release(struct inode *inode, struct file *file)
152{ 245{
153 struct joydev_client *client = file->private_data; 246 struct joydev_client *client = file->private_data;
154 struct joydev *joydev = client->joydev; 247 struct joydev *joydev = client->joydev;
155 248
156 joydev_fasync(-1, file, 0); 249 joydev_fasync(-1, file, 0);
157 250 joydev_detach_client(joydev, client);
158 list_del(&client->node);
159 kfree(client); 251 kfree(client);
160 252
161 if (!--joydev->open && joydev->exist) 253 joydev_close_device(joydev);
162 input_close_device(&joydev->handle);
163
164 put_device(&joydev->dev); 254 put_device(&joydev->dev);
165 255
166 return 0; 256 return 0;
@@ -176,11 +266,16 @@ static int joydev_open(struct inode *inode, struct file *file)
176 if (i >= JOYDEV_MINORS) 266 if (i >= JOYDEV_MINORS)
177 return -ENODEV; 267 return -ENODEV;
178 268
269 error = mutex_lock_interruptible(&joydev_table_mutex);
270 if (error)
271 return error;
179 joydev = joydev_table[i]; 272 joydev = joydev_table[i];
180 if (!joydev || !joydev->exist) 273 if (joydev)
181 return -ENODEV; 274 get_device(&joydev->dev);
275 mutex_unlock(&joydev_table_mutex);
182 276
183 get_device(&joydev->dev); 277 if (!joydev)
278 return -ENODEV;
184 279
185 client = kzalloc(sizeof(struct joydev_client), GFP_KERNEL); 280 client = kzalloc(sizeof(struct joydev_client), GFP_KERNEL);
186 if (!client) { 281 if (!client) {
@@ -188,37 +283,129 @@ static int joydev_open(struct inode *inode, struct file *file)
188 goto err_put_joydev; 283 goto err_put_joydev;
189 } 284 }
190 285
286 spin_lock_init(&client->buffer_lock);
191 client->joydev = joydev; 287 client->joydev = joydev;
192 list_add_tail(&client->node, &joydev->client_list); 288 joydev_attach_client(joydev, client);
193 289
194 if (!joydev->open++ && joydev->exist) { 290 error = joydev_open_device(joydev);
195 error = input_open_device(&joydev->handle); 291 if (error)
196 if (error) 292 goto err_free_client;
197 goto err_free_client;
198 }
199 293
200 file->private_data = client; 294 file->private_data = client;
201 return 0; 295 return 0;
202 296
203 err_free_client: 297 err_free_client:
204 list_del(&client->node); 298 joydev_detach_client(joydev, client);
205 kfree(client); 299 kfree(client);
206 err_put_joydev: 300 err_put_joydev:
207 put_device(&joydev->dev); 301 put_device(&joydev->dev);
208 return error; 302 return error;
209} 303}
210 304
211static ssize_t joydev_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos) 305static int joydev_generate_startup_event(struct joydev_client *client,
306 struct input_dev *input,
307 struct js_event *event)
212{ 308{
213 return -EINVAL; 309 struct joydev *joydev = client->joydev;
310 int have_event;
311
312 spin_lock_irq(&client->buffer_lock);
313
314 have_event = client->startup < joydev->nabs + joydev->nkey;
315
316 if (have_event) {
317
318 event->time = jiffies_to_msecs(jiffies);
319 if (client->startup < joydev->nkey) {
320 event->type = JS_EVENT_BUTTON | JS_EVENT_INIT;
321 event->number = client->startup;
322 event->value = !!test_bit(joydev->keypam[event->number],
323 input->key);
324 } else {
325 event->type = JS_EVENT_AXIS | JS_EVENT_INIT;
326 event->number = client->startup - joydev->nkey;
327 event->value = joydev->abs[event->number];
328 }
329 client->startup++;
330 }
331
332 spin_unlock_irq(&client->buffer_lock);
333
334 return have_event;
335}
336
337static int joydev_fetch_next_event(struct joydev_client *client,
338 struct js_event *event)
339{
340 int have_event;
341
342 spin_lock_irq(&client->buffer_lock);
343
344 have_event = client->head != client->tail;
345 if (have_event) {
346 *event = client->buffer[client->tail++];
347 client->tail &= JOYDEV_BUFFER_SIZE - 1;
348 }
349
350 spin_unlock_irq(&client->buffer_lock);
351
352 return have_event;
353}
354
355/*
356 * Old joystick interface
357 */
358static ssize_t joydev_0x_read(struct joydev_client *client,
359 struct input_dev *input,
360 char __user *buf)
361{
362 struct joydev *joydev = client->joydev;
363 struct JS_DATA_TYPE data;
364 int i;
365
366 spin_lock_irq(&input->event_lock);
367
368 /*
369 * Get device state
370 */
371 for (data.buttons = i = 0; i < 32 && i < joydev->nkey; i++)
372 data.buttons |=
373 test_bit(joydev->keypam[i], input->key) ? (1 << i) : 0;
374 data.x = (joydev->abs[0] / 256 + 128) >> joydev->glue.JS_CORR.x;
375 data.y = (joydev->abs[1] / 256 + 128) >> joydev->glue.JS_CORR.y;
376
377 /*
378 * Reset reader's event queue
379 */
380 spin_lock(&client->buffer_lock);
381 client->startup = 0;
382 client->tail = client->head;
383 spin_unlock(&client->buffer_lock);
384
385 spin_unlock_irq(&input->event_lock);
386
387 if (copy_to_user(buf, &data, sizeof(struct JS_DATA_TYPE)))
388 return -EFAULT;
389
390 return sizeof(struct JS_DATA_TYPE);
391}
392
393static inline int joydev_data_pending(struct joydev_client *client)
394{
395 struct joydev *joydev = client->joydev;
396
397 return client->startup < joydev->nabs + joydev->nkey ||
398 client->head != client->tail;
214} 399}
215 400
216static ssize_t joydev_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) 401static ssize_t joydev_read(struct file *file, char __user *buf,
402 size_t count, loff_t *ppos)
217{ 403{
218 struct joydev_client *client = file->private_data; 404 struct joydev_client *client = file->private_data;
219 struct joydev *joydev = client->joydev; 405 struct joydev *joydev = client->joydev;
220 struct input_dev *input = joydev->handle.dev; 406 struct input_dev *input = joydev->handle.dev;
221 int retval = 0; 407 struct js_event event;
408 int retval;
222 409
223 if (!joydev->exist) 410 if (!joydev->exist)
224 return -ENODEV; 411 return -ENODEV;
@@ -226,68 +413,35 @@ static ssize_t joydev_read(struct file *file, char __user *buf, size_t count, lo
226 if (count < sizeof(struct js_event)) 413 if (count < sizeof(struct js_event))
227 return -EINVAL; 414 return -EINVAL;
228 415
229 if (count == sizeof(struct JS_DATA_TYPE)) { 416 if (count == sizeof(struct JS_DATA_TYPE))
230 417 return joydev_0x_read(client, input, buf);
231 struct JS_DATA_TYPE data;
232 int i;
233
234 for (data.buttons = i = 0; i < 32 && i < joydev->nkey; i++)
235 data.buttons |= test_bit(joydev->keypam[i], input->key) ? (1 << i) : 0;
236 data.x = (joydev->abs[0] / 256 + 128) >> joydev->glue.JS_CORR.x;
237 data.y = (joydev->abs[1] / 256 + 128) >> joydev->glue.JS_CORR.y;
238
239 if (copy_to_user(buf, &data, sizeof(struct JS_DATA_TYPE)))
240 return -EFAULT;
241
242 client->startup = 0;
243 client->tail = client->head;
244 418
245 return sizeof(struct JS_DATA_TYPE); 419 if (!joydev_data_pending(client) && (file->f_flags & O_NONBLOCK))
246 }
247
248 if (client->startup == joydev->nabs + joydev->nkey &&
249 client->head == client->tail && (file->f_flags & O_NONBLOCK))
250 return -EAGAIN; 420 return -EAGAIN;
251 421
252 retval = wait_event_interruptible(joydev->wait, 422 retval = wait_event_interruptible(joydev->wait,
253 !joydev->exist || 423 !joydev->exist || joydev_data_pending(client));
254 client->startup < joydev->nabs + joydev->nkey ||
255 client->head != client->tail);
256 if (retval) 424 if (retval)
257 return retval; 425 return retval;
258 426
259 if (!joydev->exist) 427 if (!joydev->exist)
260 return -ENODEV; 428 return -ENODEV;
261 429
262 while (client->startup < joydev->nabs + joydev->nkey && retval + sizeof(struct js_event) <= count) { 430 while (retval + sizeof(struct js_event) <= count &&
263 431 joydev_generate_startup_event(client, input, &event)) {
264 struct js_event event;
265
266 event.time = jiffies_to_msecs(jiffies);
267
268 if (client->startup < joydev->nkey) {
269 event.type = JS_EVENT_BUTTON | JS_EVENT_INIT;
270 event.number = client->startup;
271 event.value = !!test_bit(joydev->keypam[event.number], input->key);
272 } else {
273 event.type = JS_EVENT_AXIS | JS_EVENT_INIT;
274 event.number = client->startup - joydev->nkey;
275 event.value = joydev->abs[event.number];
276 }
277 432
278 if (copy_to_user(buf + retval, &event, sizeof(struct js_event))) 433 if (copy_to_user(buf + retval, &event, sizeof(struct js_event)))
279 return -EFAULT; 434 return -EFAULT;
280 435
281 client->startup++;
282 retval += sizeof(struct js_event); 436 retval += sizeof(struct js_event);
283 } 437 }
284 438
285 while (client->head != client->tail && retval + sizeof(struct js_event) <= count) { 439 while (retval + sizeof(struct js_event) <= count &&
440 joydev_fetch_next_event(client, &event)) {
286 441
287 if (copy_to_user(buf + retval, client->buffer + client->tail, sizeof(struct js_event))) 442 if (copy_to_user(buf + retval, &event, sizeof(struct js_event)))
288 return -EFAULT; 443 return -EFAULT;
289 444
290 client->tail = (client->tail + 1) & (JOYDEV_BUFFER_SIZE - 1);
291 retval += sizeof(struct js_event); 445 retval += sizeof(struct js_event);
292 } 446 }
293 447
@@ -301,126 +455,144 @@ static unsigned int joydev_poll(struct file *file, poll_table *wait)
301 struct joydev *joydev = client->joydev; 455 struct joydev *joydev = client->joydev;
302 456
303 poll_wait(file, &joydev->wait, wait); 457 poll_wait(file, &joydev->wait, wait);
304 return ((client->head != client->tail || client->startup < joydev->nabs + joydev->nkey) ? 458 return (joydev_data_pending(client) ? (POLLIN | POLLRDNORM) : 0) |
305 (POLLIN | POLLRDNORM) : 0) | (joydev->exist ? 0 : (POLLHUP | POLLERR)); 459 (joydev->exist ? 0 : (POLLHUP | POLLERR));
306} 460}
307 461
308static int joydev_ioctl_common(struct joydev *joydev, unsigned int cmd, void __user *argp) 462static int joydev_ioctl_common(struct joydev *joydev,
463 unsigned int cmd, void __user *argp)
309{ 464{
310 struct input_dev *dev = joydev->handle.dev; 465 struct input_dev *dev = joydev->handle.dev;
311 int i, j; 466 int i, j;
312 467
313 switch (cmd) { 468 switch (cmd) {
314 469
315 case JS_SET_CAL: 470 case JS_SET_CAL:
316 return copy_from_user(&joydev->glue.JS_CORR, argp, 471 return copy_from_user(&joydev->glue.JS_CORR, argp,
317 sizeof(joydev->glue.JS_CORR)) ? -EFAULT : 0; 472 sizeof(joydev->glue.JS_CORR)) ? -EFAULT : 0;
318 473
319 case JS_GET_CAL: 474 case JS_GET_CAL:
320 return copy_to_user(argp, &joydev->glue.JS_CORR, 475 return copy_to_user(argp, &joydev->glue.JS_CORR,
321 sizeof(joydev->glue.JS_CORR)) ? -EFAULT : 0; 476 sizeof(joydev->glue.JS_CORR)) ? -EFAULT : 0;
322 477
323 case JS_SET_TIMEOUT: 478 case JS_SET_TIMEOUT:
324 return get_user(joydev->glue.JS_TIMEOUT, (s32 __user *) argp); 479 return get_user(joydev->glue.JS_TIMEOUT, (s32 __user *) argp);
325 480
326 case JS_GET_TIMEOUT: 481 case JS_GET_TIMEOUT:
327 return put_user(joydev->glue.JS_TIMEOUT, (s32 __user *) argp); 482 return put_user(joydev->glue.JS_TIMEOUT, (s32 __user *) argp);
328 483
329 case JSIOCGVERSION: 484 case JSIOCGVERSION:
330 return put_user(JS_VERSION, (__u32 __user *) argp); 485 return put_user(JS_VERSION, (__u32 __user *) argp);
331 486
332 case JSIOCGAXES: 487 case JSIOCGAXES:
333 return put_user(joydev->nabs, (__u8 __user *) argp); 488 return put_user(joydev->nabs, (__u8 __user *) argp);
334 489
335 case JSIOCGBUTTONS: 490 case JSIOCGBUTTONS:
336 return put_user(joydev->nkey, (__u8 __user *) argp); 491 return put_user(joydev->nkey, (__u8 __user *) argp);
337 492
338 case JSIOCSCORR: 493 case JSIOCSCORR:
339 if (copy_from_user(joydev->corr, argp, 494 if (copy_from_user(joydev->corr, argp,
340 sizeof(joydev->corr[0]) * joydev->nabs)) 495 sizeof(joydev->corr[0]) * joydev->nabs))
341 return -EFAULT; 496 return -EFAULT;
342 for (i = 0; i < joydev->nabs; i++) {
343 j = joydev->abspam[i];
344 joydev->abs[i] = joydev_correct(dev->abs[j], joydev->corr + i);
345 }
346 return 0;
347 497
348 case JSIOCGCORR: 498 for (i = 0; i < joydev->nabs; i++) {
349 return copy_to_user(argp, joydev->corr, 499 j = joydev->abspam[i];
350 sizeof(joydev->corr[0]) * joydev->nabs) ? -EFAULT : 0; 500 joydev->abs[i] = joydev_correct(dev->abs[j],
501 &joydev->corr[i]);
502 }
503 return 0;
351 504
352 case JSIOCSAXMAP: 505 case JSIOCGCORR:
353 if (copy_from_user(joydev->abspam, argp, sizeof(__u8) * (ABS_MAX + 1))) 506 return copy_to_user(argp, joydev->corr,
354 return -EFAULT; 507 sizeof(joydev->corr[0]) * joydev->nabs) ? -EFAULT : 0;
355 for (i = 0; i < joydev->nabs; i++) { 508
356 if (joydev->abspam[i] > ABS_MAX) 509 case JSIOCSAXMAP:
357 return -EINVAL; 510 if (copy_from_user(joydev->abspam, argp,
358 joydev->absmap[joydev->abspam[i]] = i; 511 sizeof(__u8) * (ABS_MAX + 1)))
359 } 512 return -EFAULT;
360 return 0; 513
361 514 for (i = 0; i < joydev->nabs; i++) {
362 case JSIOCGAXMAP: 515 if (joydev->abspam[i] > ABS_MAX)
363 return copy_to_user(argp, joydev->abspam, 516 return -EINVAL;
364 sizeof(__u8) * (ABS_MAX + 1)) ? -EFAULT : 0; 517 joydev->absmap[joydev->abspam[i]] = i;
365 518 }
366 case JSIOCSBTNMAP: 519 return 0;
367 if (copy_from_user(joydev->keypam, argp, sizeof(__u16) * (KEY_MAX - BTN_MISC + 1))) 520
521 case JSIOCGAXMAP:
522 return copy_to_user(argp, joydev->abspam,
523 sizeof(__u8) * (ABS_MAX + 1)) ? -EFAULT : 0;
524
525 case JSIOCSBTNMAP:
526 if (copy_from_user(joydev->keypam, argp,
527 sizeof(__u16) * (KEY_MAX - BTN_MISC + 1)))
528 return -EFAULT;
529
530 for (i = 0; i < joydev->nkey; i++) {
531 if (joydev->keypam[i] > KEY_MAX ||
532 joydev->keypam[i] < BTN_MISC)
533 return -EINVAL;
534 joydev->keymap[joydev->keypam[i] - BTN_MISC] = i;
535 }
536
537 return 0;
538
539 case JSIOCGBTNMAP:
540 return copy_to_user(argp, joydev->keypam,
541 sizeof(__u16) * (KEY_MAX - BTN_MISC + 1)) ? -EFAULT : 0;
542
543 default:
544 if ((cmd & ~IOCSIZE_MASK) == JSIOCGNAME(0)) {
545 int len;
546 if (!dev->name)
547 return 0;
548 len = strlen(dev->name) + 1;
549 if (len > _IOC_SIZE(cmd))
550 len = _IOC_SIZE(cmd);
551 if (copy_to_user(argp, dev->name, len))
368 return -EFAULT; 552 return -EFAULT;
369 for (i = 0; i < joydev->nkey; i++) { 553 return len;
370 if (joydev->keypam[i] > KEY_MAX || joydev->keypam[i] < BTN_MISC) 554 }
371 return -EINVAL;
372 joydev->keymap[joydev->keypam[i] - BTN_MISC] = i;
373 }
374 return 0;
375
376 case JSIOCGBTNMAP:
377 return copy_to_user(argp, joydev->keypam,
378 sizeof(__u16) * (KEY_MAX - BTN_MISC + 1)) ? -EFAULT : 0;
379
380 default:
381 if ((cmd & ~(_IOC_SIZEMASK << _IOC_SIZESHIFT)) == JSIOCGNAME(0)) {
382 int len;
383 if (!dev->name)
384 return 0;
385 len = strlen(dev->name) + 1;
386 if (len > _IOC_SIZE(cmd))
387 len = _IOC_SIZE(cmd);
388 if (copy_to_user(argp, dev->name, len))
389 return -EFAULT;
390 return len;
391 }
392 } 555 }
393 return -EINVAL; 556 return -EINVAL;
394} 557}
395 558
396#ifdef CONFIG_COMPAT 559#ifdef CONFIG_COMPAT
397static long joydev_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 560static long joydev_compat_ioctl(struct file *file,
561 unsigned int cmd, unsigned long arg)
398{ 562{
399 struct joydev_client *client = file->private_data; 563 struct joydev_client *client = file->private_data;
400 struct joydev *joydev = client->joydev; 564 struct joydev *joydev = client->joydev;
401 void __user *argp = (void __user *)arg; 565 void __user *argp = (void __user *)arg;
402 s32 tmp32; 566 s32 tmp32;
403 struct JS_DATA_SAVE_TYPE_32 ds32; 567 struct JS_DATA_SAVE_TYPE_32 ds32;
404 int err; 568 int retval;
405 569
406 if (!joydev->exist) 570 retval = mutex_lock_interruptible(&joydev->mutex);
407 return -ENODEV; 571 if (retval)
572 return retval;
573
574 if (!joydev->exist) {
575 retval = -ENODEV;
576 goto out;
577 }
578
579 switch (cmd) {
408 580
409 switch(cmd) {
410 case JS_SET_TIMELIMIT: 581 case JS_SET_TIMELIMIT:
411 err = get_user(tmp32, (s32 __user *) arg); 582 retval = get_user(tmp32, (s32 __user *) arg);
412 if (err == 0) 583 if (retval == 0)
413 joydev->glue.JS_TIMELIMIT = tmp32; 584 joydev->glue.JS_TIMELIMIT = tmp32;
414 break; 585 break;
586
415 case JS_GET_TIMELIMIT: 587 case JS_GET_TIMELIMIT:
416 tmp32 = joydev->glue.JS_TIMELIMIT; 588 tmp32 = joydev->glue.JS_TIMELIMIT;
417 err = put_user(tmp32, (s32 __user *) arg); 589 retval = put_user(tmp32, (s32 __user *) arg);
418 break; 590 break;
419 591
420 case JS_SET_ALL: 592 case JS_SET_ALL:
421 err = copy_from_user(&ds32, argp, 593 retval = copy_from_user(&ds32, argp,
422 sizeof(ds32)) ? -EFAULT : 0; 594 sizeof(ds32)) ? -EFAULT : 0;
423 if (err == 0) { 595 if (retval == 0) {
424 joydev->glue.JS_TIMEOUT = ds32.JS_TIMEOUT; 596 joydev->glue.JS_TIMEOUT = ds32.JS_TIMEOUT;
425 joydev->glue.BUSY = ds32.BUSY; 597 joydev->glue.BUSY = ds32.BUSY;
426 joydev->glue.JS_EXPIRETIME = ds32.JS_EXPIRETIME; 598 joydev->glue.JS_EXPIRETIME = ds32.JS_EXPIRETIME;
@@ -438,55 +610,119 @@ static long joydev_compat_ioctl(struct file *file, unsigned int cmd, unsigned lo
438 ds32.JS_SAVE = joydev->glue.JS_SAVE; 610 ds32.JS_SAVE = joydev->glue.JS_SAVE;
439 ds32.JS_CORR = joydev->glue.JS_CORR; 611 ds32.JS_CORR = joydev->glue.JS_CORR;
440 612
441 err = copy_to_user(argp, &ds32, sizeof(ds32)) ? -EFAULT : 0; 613 retval = copy_to_user(argp, &ds32, sizeof(ds32)) ? -EFAULT : 0;
442 break; 614 break;
443 615
444 default: 616 default:
445 err = joydev_ioctl_common(joydev, cmd, argp); 617 retval = joydev_ioctl_common(joydev, cmd, argp);
618 break;
446 } 619 }
447 return err; 620
621 out:
622 mutex_unlock(&joydev->mutex);
623 return retval;
448} 624}
449#endif /* CONFIG_COMPAT */ 625#endif /* CONFIG_COMPAT */
450 626
451static int joydev_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg) 627static long joydev_ioctl(struct file *file,
628 unsigned int cmd, unsigned long arg)
452{ 629{
453 struct joydev_client *client = file->private_data; 630 struct joydev_client *client = file->private_data;
454 struct joydev *joydev = client->joydev; 631 struct joydev *joydev = client->joydev;
455 void __user *argp = (void __user *)arg; 632 void __user *argp = (void __user *)arg;
633 int retval;
456 634
457 if (!joydev->exist) 635 retval = mutex_lock_interruptible(&joydev->mutex);
458 return -ENODEV; 636 if (retval)
637 return retval;
638
639 if (!joydev->exist) {
640 retval = -ENODEV;
641 goto out;
642 }
643
644 switch (cmd) {
645
646 case JS_SET_TIMELIMIT:
647 retval = get_user(joydev->glue.JS_TIMELIMIT,
648 (long __user *) arg);
649 break;
650
651 case JS_GET_TIMELIMIT:
652 retval = put_user(joydev->glue.JS_TIMELIMIT,
653 (long __user *) arg);
654 break;
655
656 case JS_SET_ALL:
657 retval = copy_from_user(&joydev->glue, argp,
658 sizeof(joydev->glue)) ? -EFAULT: 0;
659 break;
660
661 case JS_GET_ALL:
662 retval = copy_to_user(argp, &joydev->glue,
663 sizeof(joydev->glue)) ? -EFAULT : 0;
664 break;
459 665
460 switch(cmd) { 666 default:
461 case JS_SET_TIMELIMIT: 667 retval = joydev_ioctl_common(joydev, cmd, argp);
462 return get_user(joydev->glue.JS_TIMELIMIT, (long __user *) arg); 668 break;
463 case JS_GET_TIMELIMIT:
464 return put_user(joydev->glue.JS_TIMELIMIT, (long __user *) arg);
465 case JS_SET_ALL:
466 return copy_from_user(&joydev->glue, argp,
467 sizeof(joydev->glue)) ? -EFAULT : 0;
468 case JS_GET_ALL:
469 return copy_to_user(argp, &joydev->glue,
470 sizeof(joydev->glue)) ? -EFAULT : 0;
471 default:
472 return joydev_ioctl_common(joydev, cmd, argp);
473 } 669 }
670 out:
671 mutex_unlock(&joydev->mutex);
672 return retval;
474} 673}
475 674
476static const struct file_operations joydev_fops = { 675static const struct file_operations joydev_fops = {
477 .owner = THIS_MODULE, 676 .owner = THIS_MODULE,
478 .read = joydev_read, 677 .read = joydev_read,
479 .write = joydev_write, 678 .poll = joydev_poll,
480 .poll = joydev_poll, 679 .open = joydev_open,
481 .open = joydev_open, 680 .release = joydev_release,
482 .release = joydev_release, 681 .unlocked_ioctl = joydev_ioctl,
483 .ioctl = joydev_ioctl,
484#ifdef CONFIG_COMPAT 682#ifdef CONFIG_COMPAT
485 .compat_ioctl = joydev_compat_ioctl, 683 .compat_ioctl = joydev_compat_ioctl,
486#endif 684#endif
487 .fasync = joydev_fasync, 685 .fasync = joydev_fasync,
488}; 686};
489 687
688static int joydev_install_chrdev(struct joydev *joydev)
689{
690 joydev_table[joydev->minor] = joydev;
691 return 0;
692}
693
694static void joydev_remove_chrdev(struct joydev *joydev)
695{
696 mutex_lock(&joydev_table_mutex);
697 joydev_table[joydev->minor] = NULL;
698 mutex_unlock(&joydev_table_mutex);
699}
700
701/*
702 * Mark device non-existant. This disables writes, ioctls and
703 * prevents new users from opening the device. Already posted
704 * blocking reads will stay, however new ones will fail.
705 */
706static void joydev_mark_dead(struct joydev *joydev)
707{
708 mutex_lock(&joydev->mutex);
709 joydev->exist = 0;
710 mutex_unlock(&joydev->mutex);
711}
712
713static void joydev_cleanup(struct joydev *joydev)
714{
715 struct input_handle *handle = &joydev->handle;
716
717 joydev_mark_dead(joydev);
718 joydev_hangup(joydev);
719 joydev_remove_chrdev(joydev);
720
721 /* joydev is marked dead so noone else accesses joydev->open */
722 if (joydev->open)
723 input_close_device(handle);
724}
725
490static int joydev_connect(struct input_handler *handler, struct input_dev *dev, 726static int joydev_connect(struct input_handler *handler, struct input_dev *dev,
491 const struct input_device_id *id) 727 const struct input_device_id *id)
492{ 728{
@@ -494,7 +730,10 @@ static int joydev_connect(struct input_handler *handler, struct input_dev *dev,
494 int i, j, t, minor; 730 int i, j, t, minor;
495 int error; 731 int error;
496 732
497 for (minor = 0; minor < JOYDEV_MINORS && joydev_table[minor]; minor++); 733 for (minor = 0; minor < JOYDEV_MINORS; minor++)
734 if (!joydev_table[minor])
735 break;
736
498 if (minor == JOYDEV_MINORS) { 737 if (minor == JOYDEV_MINORS) {
499 printk(KERN_ERR "joydev: no more free joydev devices\n"); 738 printk(KERN_ERR "joydev: no more free joydev devices\n");
500 return -ENFILE; 739 return -ENFILE;
@@ -505,15 +744,19 @@ static int joydev_connect(struct input_handler *handler, struct input_dev *dev,
505 return -ENOMEM; 744 return -ENOMEM;
506 745
507 INIT_LIST_HEAD(&joydev->client_list); 746 INIT_LIST_HEAD(&joydev->client_list);
747 spin_lock_init(&joydev->client_lock);
748 mutex_init(&joydev->mutex);
508 init_waitqueue_head(&joydev->wait); 749 init_waitqueue_head(&joydev->wait);
509 750
751 snprintf(joydev->name, sizeof(joydev->name), "js%d", minor);
752 joydev->exist = 1;
510 joydev->minor = minor; 753 joydev->minor = minor;
754
511 joydev->exist = 1; 755 joydev->exist = 1;
512 joydev->handle.dev = dev; 756 joydev->handle.dev = dev;
513 joydev->handle.name = joydev->name; 757 joydev->handle.name = joydev->name;
514 joydev->handle.handler = handler; 758 joydev->handle.handler = handler;
515 joydev->handle.private = joydev; 759 joydev->handle.private = joydev;
516 snprintf(joydev->name, sizeof(joydev->name), "js%d", minor);
517 760
518 for (i = 0; i < ABS_MAX + 1; i++) 761 for (i = 0; i < ABS_MAX + 1; i++)
519 if (test_bit(i, dev->absbit)) { 762 if (test_bit(i, dev->absbit)) {
@@ -545,67 +788,65 @@ static int joydev_connect(struct input_handler *handler, struct input_dev *dev,
545 } 788 }
546 joydev->corr[i].type = JS_CORR_BROKEN; 789 joydev->corr[i].type = JS_CORR_BROKEN;
547 joydev->corr[i].prec = dev->absfuzz[j]; 790 joydev->corr[i].prec = dev->absfuzz[j];
548 joydev->corr[i].coef[0] = (dev->absmax[j] + dev->absmin[j]) / 2 - dev->absflat[j]; 791 joydev->corr[i].coef[0] =
549 joydev->corr[i].coef[1] = (dev->absmax[j] + dev->absmin[j]) / 2 + dev->absflat[j]; 792 (dev->absmax[j] + dev->absmin[j]) / 2 - dev->absflat[j];
550 if (!(t = ((dev->absmax[j] - dev->absmin[j]) / 2 - 2 * dev->absflat[j]))) 793 joydev->corr[i].coef[1] =
551 continue; 794 (dev->absmax[j] + dev->absmin[j]) / 2 + dev->absflat[j];
552 joydev->corr[i].coef[2] = (1 << 29) / t; 795
553 joydev->corr[i].coef[3] = (1 << 29) / t; 796 t = (dev->absmax[j] - dev->absmin[j]) / 2 - 2 * dev->absflat[j];
554 797 if (t) {
555 joydev->abs[i] = joydev_correct(dev->abs[j], joydev->corr + i); 798 joydev->corr[i].coef[2] = (1 << 29) / t;
799 joydev->corr[i].coef[3] = (1 << 29) / t;
800
801 joydev->abs[i] = joydev_correct(dev->abs[j],
802 joydev->corr + i);
803 }
556 } 804 }
557 805
558 snprintf(joydev->dev.bus_id, sizeof(joydev->dev.bus_id), 806 strlcpy(joydev->dev.bus_id, joydev->name, sizeof(joydev->dev.bus_id));
559 "js%d", minor); 807 joydev->dev.devt = MKDEV(INPUT_MAJOR, JOYDEV_MINOR_BASE + minor);
560 joydev->dev.class = &input_class; 808 joydev->dev.class = &input_class;
561 joydev->dev.parent = &dev->dev; 809 joydev->dev.parent = &dev->dev;
562 joydev->dev.devt = MKDEV(INPUT_MAJOR, JOYDEV_MINOR_BASE + minor);
563 joydev->dev.release = joydev_free; 810 joydev->dev.release = joydev_free;
564 device_initialize(&joydev->dev); 811 device_initialize(&joydev->dev);
565 812
566 joydev_table[minor] = joydev; 813 error = input_register_handle(&joydev->handle);
567
568 error = device_add(&joydev->dev);
569 if (error) 814 if (error)
570 goto err_free_joydev; 815 goto err_free_joydev;
571 816
572 error = input_register_handle(&joydev->handle); 817 error = joydev_install_chrdev(joydev);
573 if (error) 818 if (error)
574 goto err_delete_joydev; 819 goto err_unregister_handle;
820
821 error = device_add(&joydev->dev);
822 if (error)
823 goto err_cleanup_joydev;
575 824
576 return 0; 825 return 0;
577 826
578 err_delete_joydev: 827 err_cleanup_joydev:
579 device_del(&joydev->dev); 828 joydev_cleanup(joydev);
829 err_unregister_handle:
830 input_unregister_handle(&joydev->handle);
580 err_free_joydev: 831 err_free_joydev:
581 put_device(&joydev->dev); 832 put_device(&joydev->dev);
582 return error; 833 return error;
583} 834}
584 835
585
586static void joydev_disconnect(struct input_handle *handle) 836static void joydev_disconnect(struct input_handle *handle)
587{ 837{
588 struct joydev *joydev = handle->private; 838 struct joydev *joydev = handle->private;
589 struct joydev_client *client;
590 839
591 input_unregister_handle(handle);
592 device_del(&joydev->dev); 840 device_del(&joydev->dev);
593 841 joydev_cleanup(joydev);
594 joydev->exist = 0; 842 input_unregister_handle(handle);
595
596 if (joydev->open) {
597 input_close_device(handle);
598 list_for_each_entry(client, &joydev->client_list, node)
599 kill_fasync(&client->fasync, SIGIO, POLL_HUP);
600 wake_up_interruptible(&joydev->wait);
601 }
602
603 put_device(&joydev->dev); 843 put_device(&joydev->dev);
604} 844}
605 845
606static const struct input_device_id joydev_blacklist[] = { 846static const struct input_device_id joydev_blacklist[] = {
607 { 847 {
608 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT, 848 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
849 INPUT_DEVICE_ID_MATCH_KEYBIT,
609 .evbit = { BIT(EV_KEY) }, 850 .evbit = { BIT(EV_KEY) },
610 .keybit = { [LONG(BTN_TOUCH)] = BIT(BTN_TOUCH) }, 851 .keybit = { [LONG(BTN_TOUCH)] = BIT(BTN_TOUCH) },
611 }, /* Avoid itouchpads, touchscreens and tablets */ 852 }, /* Avoid itouchpads, touchscreens and tablets */
@@ -614,17 +855,20 @@ static const struct input_device_id joydev_blacklist[] = {
614 855
615static const struct input_device_id joydev_ids[] = { 856static const struct input_device_id joydev_ids[] = {
616 { 857 {
617 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_ABSBIT, 858 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
859 INPUT_DEVICE_ID_MATCH_ABSBIT,
618 .evbit = { BIT(EV_ABS) }, 860 .evbit = { BIT(EV_ABS) },
619 .absbit = { BIT(ABS_X) }, 861 .absbit = { BIT(ABS_X) },
620 }, 862 },
621 { 863 {
622 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_ABSBIT, 864 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
865 INPUT_DEVICE_ID_MATCH_ABSBIT,
623 .evbit = { BIT(EV_ABS) }, 866 .evbit = { BIT(EV_ABS) },
624 .absbit = { BIT(ABS_WHEEL) }, 867 .absbit = { BIT(ABS_WHEEL) },
625 }, 868 },
626 { 869 {
627 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_ABSBIT, 870 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
871 INPUT_DEVICE_ID_MATCH_ABSBIT,
628 .evbit = { BIT(EV_ABS) }, 872 .evbit = { BIT(EV_ABS) },
629 .absbit = { BIT(ABS_THROTTLE) }, 873 .absbit = { BIT(ABS_THROTTLE) },
630 }, 874 },
@@ -634,14 +878,14 @@ static const struct input_device_id joydev_ids[] = {
634MODULE_DEVICE_TABLE(input, joydev_ids); 878MODULE_DEVICE_TABLE(input, joydev_ids);
635 879
636static struct input_handler joydev_handler = { 880static struct input_handler joydev_handler = {
637 .event = joydev_event, 881 .event = joydev_event,
638 .connect = joydev_connect, 882 .connect = joydev_connect,
639 .disconnect = joydev_disconnect, 883 .disconnect = joydev_disconnect,
640 .fops = &joydev_fops, 884 .fops = &joydev_fops,
641 .minor = JOYDEV_MINOR_BASE, 885 .minor = JOYDEV_MINOR_BASE,
642 .name = "joydev", 886 .name = "joydev",
643 .id_table = joydev_ids, 887 .id_table = joydev_ids,
644 .blacklist = joydev_blacklist, 888 .blacklist = joydev_blacklist,
645}; 889};
646 890
647static int __init joydev_init(void) 891static int __init joydev_init(void)
diff --git a/drivers/input/joystick/xpad.c b/drivers/input/joystick/xpad.c
index 28080395899c..623629a69b03 100644
--- a/drivers/input/joystick/xpad.c
+++ b/drivers/input/joystick/xpad.c
@@ -223,12 +223,16 @@ static void xpad_process_packet(struct usb_xpad *xpad, u16 cmd, unsigned char *d
223 struct input_dev *dev = xpad->dev; 223 struct input_dev *dev = xpad->dev;
224 224
225 /* left stick */ 225 /* left stick */
226 input_report_abs(dev, ABS_X, (__s16) (((__s16)data[13] << 8) | data[12])); 226 input_report_abs(dev, ABS_X,
227 input_report_abs(dev, ABS_Y, (__s16) (((__s16)data[15] << 8) | data[14])); 227 (__s16) le16_to_cpup((__le16 *)(data + 12)));
228 input_report_abs(dev, ABS_Y,
229 (__s16) le16_to_cpup((__le16 *)(data + 14)));
228 230
229 /* right stick */ 231 /* right stick */
230 input_report_abs(dev, ABS_RX, (__s16) (((__s16)data[17] << 8) | data[16])); 232 input_report_abs(dev, ABS_RX,
231 input_report_abs(dev, ABS_RY, (__s16) (((__s16)data[19] << 8) | data[18])); 233 (__s16) le16_to_cpup((__le16 *)(data + 16)));
234 input_report_abs(dev, ABS_RY,
235 (__s16) le16_to_cpup((__le16 *)(data + 18)));
232 236
233 /* triggers left/right */ 237 /* triggers left/right */
234 input_report_abs(dev, ABS_Z, data[10]); 238 input_report_abs(dev, ABS_Z, data[10]);
@@ -236,8 +240,10 @@ static void xpad_process_packet(struct usb_xpad *xpad, u16 cmd, unsigned char *d
236 240
237 /* digital pad */ 241 /* digital pad */
238 if (xpad->dpad_mapping == MAP_DPAD_TO_AXES) { 242 if (xpad->dpad_mapping == MAP_DPAD_TO_AXES) {
239 input_report_abs(dev, ABS_HAT0X, !!(data[2] & 0x08) - !!(data[2] & 0x04)); 243 input_report_abs(dev, ABS_HAT0X,
240 input_report_abs(dev, ABS_HAT0Y, !!(data[2] & 0x02) - !!(data[2] & 0x01)); 244 !!(data[2] & 0x08) - !!(data[2] & 0x04));
245 input_report_abs(dev, ABS_HAT0Y,
246 !!(data[2] & 0x02) - !!(data[2] & 0x01));
241 } else /* xpad->dpad_mapping == MAP_DPAD_TO_BUTTONS */ { 247 } else /* xpad->dpad_mapping == MAP_DPAD_TO_BUTTONS */ {
242 input_report_key(dev, BTN_LEFT, data[2] & 0x04); 248 input_report_key(dev, BTN_LEFT, data[2] & 0x04);
243 input_report_key(dev, BTN_RIGHT, data[2] & 0x08); 249 input_report_key(dev, BTN_RIGHT, data[2] & 0x08);
@@ -274,14 +280,17 @@ static void xpad_process_packet(struct usb_xpad *xpad, u16 cmd, unsigned char *d
274 * http://www.free60.org/wiki/Gamepad 280 * http://www.free60.org/wiki/Gamepad
275 */ 281 */
276 282
277static void xpad360_process_packet(struct usb_xpad *xpad, u16 cmd, unsigned char *data) 283static void xpad360_process_packet(struct usb_xpad *xpad,
284 u16 cmd, unsigned char *data)
278{ 285{
279 struct input_dev *dev = xpad->dev; 286 struct input_dev *dev = xpad->dev;
280 287
281 /* digital pad */ 288 /* digital pad */
282 if (xpad->dpad_mapping == MAP_DPAD_TO_AXES) { 289 if (xpad->dpad_mapping == MAP_DPAD_TO_AXES) {
283 input_report_abs(dev, ABS_HAT0X, !!(data[2] & 0x08) - !!(data[2] & 0x04)); 290 input_report_abs(dev, ABS_HAT0X,
284 input_report_abs(dev, ABS_HAT0Y, !!(data[2] & 0x02) - !!(data[2] & 0x01)); 291 !!(data[2] & 0x08) - !!(data[2] & 0x04));
292 input_report_abs(dev, ABS_HAT0Y,
293 !!(data[2] & 0x02) - !!(data[2] & 0x01));
285 } else if (xpad->dpad_mapping == MAP_DPAD_TO_BUTTONS) { 294 } else if (xpad->dpad_mapping == MAP_DPAD_TO_BUTTONS) {
286 /* dpad as buttons (right, left, down, up) */ 295 /* dpad as buttons (right, left, down, up) */
287 input_report_key(dev, BTN_LEFT, data[2] & 0x04); 296 input_report_key(dev, BTN_LEFT, data[2] & 0x04);
@@ -308,12 +317,16 @@ static void xpad360_process_packet(struct usb_xpad *xpad, u16 cmd, unsigned char
308 input_report_key(dev, BTN_MODE, data[3] & 0x04); 317 input_report_key(dev, BTN_MODE, data[3] & 0x04);
309 318
310 /* left stick */ 319 /* left stick */
311 input_report_abs(dev, ABS_X, (__s16) (((__s16)data[7] << 8) | (__s16)data[6])); 320 input_report_abs(dev, ABS_X,
312 input_report_abs(dev, ABS_Y, (__s16) (((__s16)data[9] << 8) | (__s16)data[8])); 321 (__s16) le16_to_cpup((__le16 *)(data + 6)));
322 input_report_abs(dev, ABS_Y,
323 (__s16) le16_to_cpup((__le16 *)(data + 8)));
313 324
314 /* right stick */ 325 /* right stick */
315 input_report_abs(dev, ABS_RX, (__s16) (((__s16)data[11] << 8) | (__s16)data[10])); 326 input_report_abs(dev, ABS_RX,
316 input_report_abs(dev, ABS_RY, (__s16) (((__s16)data[13] << 8) | (__s16)data[12])); 327 (__s16) le16_to_cpup((__le16 *)(data + 10)));
328 input_report_abs(dev, ABS_RY,
329 (__s16) le16_to_cpup((__le16 *)(data + 12)));
317 330
318 /* triggers left/right */ 331 /* triggers left/right */
319 input_report_abs(dev, ABS_Z, data[4]); 332 input_report_abs(dev, ABS_Z, data[4]);
@@ -335,10 +348,12 @@ static void xpad_irq_in(struct urb *urb)
335 case -ENOENT: 348 case -ENOENT:
336 case -ESHUTDOWN: 349 case -ESHUTDOWN:
337 /* this urb is terminated, clean up */ 350 /* this urb is terminated, clean up */
338 dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status); 351 dbg("%s - urb shutting down with status: %d",
352 __FUNCTION__, urb->status);
339 return; 353 return;
340 default: 354 default:
341 dbg("%s - nonzero urb status received: %d", __FUNCTION__, urb->status); 355 dbg("%s - nonzero urb status received: %d",
356 __FUNCTION__, urb->status);
342 goto exit; 357 goto exit;
343 } 358 }
344 359
@@ -367,10 +382,12 @@ static void xpad_irq_out(struct urb *urb)
367 case -ENOENT: 382 case -ENOENT:
368 case -ESHUTDOWN: 383 case -ESHUTDOWN:
369 /* this urb is terminated, clean up */ 384 /* this urb is terminated, clean up */
370 dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status); 385 dbg("%s - urb shutting down with status: %d",
386 __FUNCTION__, urb->status);
371 return; 387 return;
372 default: 388 default:
373 dbg("%s - nonzero urb status received: %d", __FUNCTION__, urb->status); 389 dbg("%s - nonzero urb status received: %d",
390 __FUNCTION__, urb->status);
374 goto exit; 391 goto exit;
375 } 392 }
376 393
@@ -378,7 +395,7 @@ exit:
378 retval = usb_submit_urb(urb, GFP_ATOMIC); 395 retval = usb_submit_urb(urb, GFP_ATOMIC);
379 if (retval) 396 if (retval)
380 err("%s - usb_submit_urb failed with result %d", 397 err("%s - usb_submit_urb failed with result %d",
381 __FUNCTION__, retval); 398 __FUNCTION__, retval);
382} 399}
383 400
384static int xpad_init_output(struct usb_interface *intf, struct usb_xpad *xpad) 401static int xpad_init_output(struct usb_interface *intf, struct usb_xpad *xpad)
@@ -595,7 +612,7 @@ static void xpad_set_up_abs(struct input_dev *input_dev, signed short abs)
595 612
596static int xpad_probe(struct usb_interface *intf, const struct usb_device_id *id) 613static int xpad_probe(struct usb_interface *intf, const struct usb_device_id *id)
597{ 614{
598 struct usb_device *udev = interface_to_usbdev (intf); 615 struct usb_device *udev = interface_to_usbdev(intf);
599 struct usb_xpad *xpad; 616 struct usb_xpad *xpad;
600 struct input_dev *input_dev; 617 struct input_dev *input_dev;
601 struct usb_endpoint_descriptor *ep_irq_in; 618 struct usb_endpoint_descriptor *ep_irq_in;
diff --git a/drivers/input/keyboard/Kconfig b/drivers/input/keyboard/Kconfig
index c97d5eb0075d..f6f79e880b20 100644
--- a/drivers/input/keyboard/Kconfig
+++ b/drivers/input/keyboard/Kconfig
@@ -208,6 +208,27 @@ config KEYBOARD_HIL
208 This driver implements support for HIL-keyboards attached 208 This driver implements support for HIL-keyboards attached
209 to your machine, so normally you should say Y here. 209 to your machine, so normally you should say Y here.
210 210
211config KEYBOARD_HP6XX
212 tristate "HP Jornada 6XX Keyboard support"
213 depends on SH_HP6XX
214 select INPUT_POLLDEV
215 help
216 This adds support for the onboard keyboard found on
217 HP Jornada 620/660/680/690.
218
219 To compile this driver as a module, choose M here: the
220 module will be called jornada680_kbd.
221
222config KEYBOARD_HP7XX
223 tristate "HP Jornada 7XX Keyboard Driver"
224 depends on SA1100_JORNADA720_SSP && SA1100_SSP
225 help
226 Say Y here to add support for the HP Jornada 7xx (710/720/728)
227 onboard keyboard.
228
229 To compile this driver as a module, choose M here: the
230 module will be called jornada720_kbd.
231
211config KEYBOARD_OMAP 232config KEYBOARD_OMAP
212 tristate "TI OMAP keypad support" 233 tristate "TI OMAP keypad support"
213 depends on (ARCH_OMAP1 || ARCH_OMAP2) 234 depends on (ARCH_OMAP1 || ARCH_OMAP2)
@@ -253,4 +274,14 @@ config KEYBOARD_GPIO
253 To compile this driver as a module, choose M here: the 274 To compile this driver as a module, choose M here: the
254 module will be called gpio-keys. 275 module will be called gpio-keys.
255 276
277config KEYBOARD_MAPLE
278 tristate "Maple bus keyboard"
279 depends on SH_DREAMCAST && MAPLE
280 help
281 Say Y here if you have a Dreamcast console running Linux and have
282 a keyboard attached to its Maple bus.
283
284 To compile this driver as a module, choose M here: the
285 module will be called maple_keyb.
286
256endif 287endif
diff --git a/drivers/input/keyboard/Makefile b/drivers/input/keyboard/Makefile
index 28d211b87b14..2776aa4a3a95 100644
--- a/drivers/input/keyboard/Makefile
+++ b/drivers/input/keyboard/Makefile
@@ -21,4 +21,7 @@ obj-$(CONFIG_KEYBOARD_OMAP) += omap-keypad.o
21obj-$(CONFIG_KEYBOARD_PXA27x) += pxa27x_keyboard.o 21obj-$(CONFIG_KEYBOARD_PXA27x) += pxa27x_keyboard.o
22obj-$(CONFIG_KEYBOARD_AAED2000) += aaed2000_kbd.o 22obj-$(CONFIG_KEYBOARD_AAED2000) += aaed2000_kbd.o
23obj-$(CONFIG_KEYBOARD_GPIO) += gpio_keys.o 23obj-$(CONFIG_KEYBOARD_GPIO) += gpio_keys.o
24obj-$(CONFIG_KEYBOARD_HP6XX) += jornada680_kbd.o
25obj-$(CONFIG_KEYBOARD_HP7XX) += jornada720_kbd.o
26obj-$(CONFIG_KEYBOARD_MAPLE) += maple_keyb.o
24 27
diff --git a/drivers/input/keyboard/gpio_keys.c b/drivers/input/keyboard/gpio_keys.c
index f0b22b8b2769..e2a3293bc67e 100644
--- a/drivers/input/keyboard/gpio_keys.c
+++ b/drivers/input/keyboard/gpio_keys.c
@@ -54,6 +54,7 @@ static int __devinit gpio_keys_probe(struct platform_device *pdev)
54 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data; 54 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
55 struct input_dev *input; 55 struct input_dev *input;
56 int i, error; 56 int i, error;
57 int wakeup = 0;
57 58
58 input = input_allocate_device(); 59 input = input_allocate_device();
59 if (!input) 60 if (!input)
@@ -77,31 +78,51 @@ static int __devinit gpio_keys_probe(struct platform_device *pdev)
77 int irq = gpio_to_irq(button->gpio); 78 int irq = gpio_to_irq(button->gpio);
78 unsigned int type = button->type ?: EV_KEY; 79 unsigned int type = button->type ?: EV_KEY;
79 80
80 set_irq_type(irq, IRQ_TYPE_EDGE_BOTH); 81 if (irq < 0) {
81 error = request_irq(irq, gpio_keys_isr, IRQF_SAMPLE_RANDOM, 82 error = irq;
82 button->desc ? button->desc : "gpio_keys", 83 printk(KERN_ERR
83 pdev); 84 "gpio-keys: "
85 "Unable to get irq number for GPIO %d,"
86 "error %d\n",
87 button->gpio, error);
88 goto fail;
89 }
90
91 error = request_irq(irq, gpio_keys_isr,
92 IRQF_SAMPLE_RANDOM | IRQF_TRIGGER_RISING |
93 IRQF_TRIGGER_FALLING,
94 button->desc ? button->desc : "gpio_keys",
95 pdev);
84 if (error) { 96 if (error) {
85 printk(KERN_ERR "gpio-keys: unable to claim irq %d; error %d\n", 97 printk(KERN_ERR
98 "gpio-keys: Unable to claim irq %d; error %d\n",
86 irq, error); 99 irq, error);
87 goto fail; 100 goto fail;
88 } 101 }
89 102
103 if (button->wakeup)
104 wakeup = 1;
105
90 input_set_capability(input, type, button->code); 106 input_set_capability(input, type, button->code);
91 } 107 }
92 108
93 error = input_register_device(input); 109 error = input_register_device(input);
94 if (error) { 110 if (error) {
95 printk(KERN_ERR "Unable to register gpio-keys input device\n"); 111 printk(KERN_ERR
112 "gpio-keys: Unable to register input device, "
113 "error: %d\n", error);
96 goto fail; 114 goto fail;
97 } 115 }
98 116
117 device_init_wakeup(&pdev->dev, wakeup);
118
99 return 0; 119 return 0;
100 120
101 fail: 121 fail:
102 for (i = i - 1; i >= 0; i--) 122 while (--i >= 0)
103 free_irq(gpio_to_irq(pdata->buttons[i].gpio), pdev); 123 free_irq(gpio_to_irq(pdata->buttons[i].gpio), pdev);
104 124
125 platform_set_drvdata(pdev, NULL);
105 input_free_device(input); 126 input_free_device(input);
106 127
107 return error; 128 return error;
@@ -113,6 +134,8 @@ static int __devexit gpio_keys_remove(struct platform_device *pdev)
113 struct input_dev *input = platform_get_drvdata(pdev); 134 struct input_dev *input = platform_get_drvdata(pdev);
114 int i; 135 int i;
115 136
137 device_init_wakeup(&pdev->dev, 0);
138
116 for (i = 0; i < pdata->nbuttons; i++) { 139 for (i = 0; i < pdata->nbuttons; i++) {
117 int irq = gpio_to_irq(pdata->buttons[i].gpio); 140 int irq = gpio_to_irq(pdata->buttons[i].gpio);
118 free_irq(irq, pdev); 141 free_irq(irq, pdev);
@@ -123,9 +146,53 @@ static int __devexit gpio_keys_remove(struct platform_device *pdev)
123 return 0; 146 return 0;
124} 147}
125 148
149
150#ifdef CONFIG_PM
151static int gpio_keys_suspend(struct platform_device *pdev, pm_message_t state)
152{
153 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
154 int i;
155
156 if (device_may_wakeup(&pdev->dev)) {
157 for (i = 0; i < pdata->nbuttons; i++) {
158 struct gpio_keys_button *button = &pdata->buttons[i];
159 if (button->wakeup) {
160 int irq = gpio_to_irq(button->gpio);
161 enable_irq_wake(irq);
162 }
163 }
164 }
165
166 return 0;
167}
168
169static int gpio_keys_resume(struct platform_device *pdev)
170{
171 struct gpio_keys_platform_data *pdata = pdev->dev.platform_data;
172 int i;
173
174 if (device_may_wakeup(&pdev->dev)) {
175 for (i = 0; i < pdata->nbuttons; i++) {
176 struct gpio_keys_button *button = &pdata->buttons[i];
177 if (button->wakeup) {
178 int irq = gpio_to_irq(button->gpio);
179 disable_irq_wake(irq);
180 }
181 }
182 }
183
184 return 0;
185}
186#else
187#define gpio_keys_suspend NULL
188#define gpio_keys_resume NULL
189#endif
190
126struct platform_driver gpio_keys_device_driver = { 191struct platform_driver gpio_keys_device_driver = {
127 .probe = gpio_keys_probe, 192 .probe = gpio_keys_probe,
128 .remove = __devexit_p(gpio_keys_remove), 193 .remove = __devexit_p(gpio_keys_remove),
194 .suspend = gpio_keys_suspend,
195 .resume = gpio_keys_resume,
129 .driver = { 196 .driver = {
130 .name = "gpio-keys", 197 .name = "gpio-keys",
131 } 198 }
diff --git a/drivers/input/keyboard/jornada680_kbd.c b/drivers/input/keyboard/jornada680_kbd.c
new file mode 100644
index 000000000000..bec1cf483723
--- /dev/null
+++ b/drivers/input/keyboard/jornada680_kbd.c
@@ -0,0 +1,277 @@
1/*
2 * drivers/input/keyboard/jornada680_kbd.c
3 *
4 * HP Jornada 620/660/680/690 scan keyboard platform driver
5 * Copyright (C) 2007 Kristoffer Ericson <Kristoffer.Ericson@gmail.com>
6 *
7 * Based on hp680_keyb.c
8 * Copyright (C) 2006 Paul Mundt
9 * Copyright (C) 2005 Andriy Skulysh
10 * Split from drivers/input/keyboard/hp600_keyb.c
11 * Copyright (C) 2000 Yaegashi Takeshi (hp6xx kbd scan routine and translation table)
12 * Copyright (C) 2000 Niibe Yutaka (HP620 Keyb translation table)
13 *
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License version 2 as
16 * published by the Free Software Foundation.
17 */
18
19#include <linux/input.h>
20#include <linux/kernel.h>
21#include <linux/module.h>
22#include <linux/init.h>
23#include <linux/input-polldev.h>
24#include <linux/jiffies.h>
25#include <linux/platform_device.h>
26#include <linux/interrupt.h>
27
28#include <asm/delay.h>
29#include <asm/io.h>
30
31#define PCCR 0xa4000104
32#define PDCR 0xa4000106
33#define PECR 0xa4000108
34#define PFCR 0xa400010a
35#define PCDR 0xa4000124
36#define PDDR 0xa4000126
37#define PEDR 0xa4000128
38#define PFDR 0xa400012a
39#define PGDR 0xa400012c
40#define PHDR 0xa400012e
41#define PJDR 0xa4000130
42#define PKDR 0xa4000132
43#define PLDR 0xa4000134
44
45static const unsigned short jornada_scancodes[] = {
46/* PTD1 */ KEY_CAPSLOCK, KEY_MACRO, KEY_LEFTCTRL, 0, KEY_ESC, 0, 0, 0, /* 1 -> 8 */
47 KEY_F1, KEY_F2, KEY_F3, KEY_F8, KEY_F7, KEY_F2, KEY_F4, KEY_F5, /* 9 -> 16 */
48/* PTD5 */ KEY_SLASH, KEY_APOSTROPHE, KEY_ENTER, 0, KEY_Z, 0, 0, 0, /* 17 -> 24 */
49 KEY_X, KEY_C, KEY_V, KEY_DOT, KEY_COMMA, KEY_M, KEY_B, KEY_N, /* 25 -> 32 */
50/* PTD7 */ KEY_KP2, KEY_KP6, 0, 0, 0, 0, 0, 0, /* 33 -> 40 */
51 0, 0, 0, KEY_KP4, 0, 0, KEY_LEFTALT, KEY_HANJA, /* 41 -> 48 */
52/* PTE0 */ 0, 0, 0, 0, KEY_FINANCE, 0, 0, 0, /* 49 -> 56 */
53 KEY_LEFTCTRL, 0, KEY_SPACE, KEY_KPDOT, KEY_VOLUMEUP, 249, 0, 0, /* 57 -> 64 */
54/* PTE1 */ KEY_SEMICOLON, KEY_RIGHTBRACE, KEY_BACKSLASH, 0, KEY_A, 0, 0, 0,/* 65 -> 72 */
55 KEY_S, KEY_D, KEY_F, KEY_L, KEY_K, KEY_J, KEY_G, KEY_H, /* 73 -> 80 */
56/* PTE3 */ KEY_KP8, KEY_LEFTMETA, KEY_RIGHTSHIFT, 0, KEY_TAB, 0, 0,0, /* 81 -> 88 */
57 0, KEY_LEFTSHIFT, 0, 0, 0, 0, 0, 0, /* 89 -> 96 */
58/* PTE6 */ KEY_P, KEY_LEFTBRACE, KEY_BACKSPACE, 0, KEY_Q, 0, 0, 0, /* 97 -> 104 */
59 KEY_W, KEY_E, KEY_R, KEY_O, KEY_I, KEY_U, KEY_T, KEY_R, /* 105 -> 112 */
60/* PTE7 */ KEY_0, KEY_MINUS, KEY_EQUAL, 0, KEY_1, 0, 0, 0, /* 113 -> 120 */
61 KEY_2, KEY_3, KEY_4, KEY_9, KEY_8, KEY_7, KEY_5, KEY_6, /* 121 -> 128 */
62/* **** */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
63 0, 0, 0, 0, 0
64};
65
66#define JORNADA_SCAN_SIZE 18
67
68struct jornadakbd {
69 struct input_polled_dev *poll_dev;
70 unsigned short keymap[ARRAY_SIZE(jornada_scancodes)];
71 unsigned char length;
72 unsigned char old_scan[JORNADA_SCAN_SIZE];
73 unsigned char new_scan[JORNADA_SCAN_SIZE];
74};
75
76static void jornada_parse_kbd(struct jornadakbd *jornadakbd)
77{
78 struct input_dev *input_dev = jornadakbd->poll_dev->input;
79 unsigned short *keymap = jornadakbd->keymap;
80 unsigned int sync_me = 0;
81 unsigned int i, j;
82
83 for (i = 0; i < JORNADA_SCAN_SIZE; i++) {
84 unsigned char new = jornadakbd->new_scan[i];
85 unsigned char old = jornadakbd->old_scan[i];
86 unsigned int xor = new ^ old;
87
88 if (xor == 0)
89 continue;
90
91 for (j = 0; j < 8; j++) {
92 unsigned int bit = 1 << j;
93 if (xor & bit) {
94 unsigned int scancode = (i << 3) + j;
95 input_event(input_dev,
96 EV_MSC, MSC_SCAN, scancode);
97 input_report_key(input_dev,
98 keymap[scancode],
99 !(new & bit));
100 sync_me = 1;
101 }
102 }
103 }
104
105 if (sync_me)
106 input_sync(input_dev);
107}
108
109static void jornada_scan_keyb(unsigned char *s)
110{
111 int i;
112 unsigned short ec_static, dc_static; /* = UINT16_t */
113 unsigned char matrix_switch[] = {
114 0xfd, 0xff, /* PTD1 PD(1) */
115 0xdf, 0xff, /* PTD5 PD(5) */
116 0x7f, 0xff, /* PTD7 PD(7) */
117 0xff, 0xfe, /* PTE0 PE(0) */
118 0xff, 0xfd, /* PTE1 PE(1) */
119 0xff, 0xf7, /* PTE3 PE(3) */
120 0xff, 0xbf, /* PTE6 PE(6) */
121 0xff, 0x7f, /* PTE7 PE(7) */
122 }, *t = matrix_switch;
123 /* PD(x) :
124 1. 0xcc0c & (1~(1 << (2*(x)+1)))))
125 2. (0xf0cf & 0xfffff) */
126 /* PE(x) :
127 1. 0xcc0c & 0xffff
128 2. 0xf0cf & (1~(1 << (2*(x)+1))))) */
129 unsigned short matrix_PDE[] = {
130 0xcc04, 0xf0cf, /* PD(1) */
131 0xc40c, 0xf0cf, /* PD(5) */
132 0x4c0c, 0xf0cf, /* PD(7) */
133 0xcc0c, 0xf0cd, /* PE(0) */
134 0xcc0c, 0xf0c7, /* PE(1) */
135 0xcc0c, 0xf04f, /* PE(3) */
136 0xcc0c, 0xd0cf, /* PE(6) */
137 0xcc0c, 0x70cf, /* PE(7) */
138 }, *y = matrix_PDE;
139
140 /* Save these control reg bits */
141 dc_static = (ctrl_inw(PDCR) & (~0xcc0c));
142 ec_static = (ctrl_inw(PECR) & (~0xf0cf));
143
144 for (i = 0; i < 8; i++) {
145 /* disable output for all but the one we want to scan */
146 ctrl_outw((dc_static | *y++), PDCR);
147 ctrl_outw((ec_static | *y++), PECR);
148 udelay(5);
149
150 /* Get scanline row */
151 ctrl_outb(*t++, PDDR);
152 ctrl_outb(*t++, PEDR);
153 udelay(50);
154
155 /* Read data */
156 *s++ = ctrl_inb(PCDR);
157 *s++ = ctrl_inb(PFDR);
158 }
159 /* Scan no lines */
160 ctrl_outb(0xff, PDDR);
161 ctrl_outb(0xff, PEDR);
162
163 /* Enable all scanlines */
164 ctrl_outw((dc_static | (0x5555 & 0xcc0c)),PDCR);
165 ctrl_outw((ec_static | (0x5555 & 0xf0cf)),PECR);
166
167 /* Ignore extra keys and events */
168 *s++ = ctrl_inb(PGDR);
169 *s++ = ctrl_inb(PHDR);
170}
171
172static void jornadakbd680_poll(struct input_polled_dev *dev)
173{
174 struct jornadakbd *jornadakbd = dev->private;
175
176 jornada_scan_keyb(jornadakbd->new_scan);
177 jornada_parse_kbd(jornadakbd);
178 memcpy(jornadakbd->old_scan, jornadakbd->new_scan, JORNADA_SCAN_SIZE);
179}
180
181static int __devinit jornada680kbd_probe(struct platform_device *pdev)
182{
183 struct jornadakbd *jornadakbd;
184 struct input_polled_dev *poll_dev;
185 struct input_dev *input_dev;
186 int i, error;
187
188 jornadakbd = kzalloc(sizeof(struct jornadakbd), GFP_KERNEL);
189 if (!jornadakbd)
190 return -ENOMEM;
191
192 poll_dev = input_allocate_polled_device();
193 if (!poll_dev) {
194 error = -ENOMEM;
195 goto failed;
196 }
197
198 platform_set_drvdata(pdev, jornadakbd);
199
200 jornadakbd->poll_dev = poll_dev;
201
202 memcpy(jornadakbd->keymap, jornada_scancodes,
203 sizeof(jornadakbd->keymap));
204
205 poll_dev->private = jornadakbd;
206 poll_dev->poll = jornadakbd680_poll;
207 poll_dev->poll_interval = 50; /* msec */
208
209 input_dev = poll_dev->input;
210 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REP);
211 input_dev->name = "HP Jornada 680 keyboard";
212 input_dev->phys = "jornadakbd/input0";
213 input_dev->keycode = jornadakbd->keymap;
214 input_dev->keycodesize = sizeof(unsigned short);
215 input_dev->keycodemax = ARRAY_SIZE(jornada_scancodes);
216 input_dev->dev.parent = &pdev->dev;
217 input_dev->id.bustype = BUS_HOST;
218
219 for (i = 0; i < 128; i++)
220 if (jornadakbd->keymap[i])
221 __set_bit(jornadakbd->keymap[i], input_dev->keybit);
222 __clear_bit(KEY_RESERVED, input_dev->keybit);
223
224 input_set_capability(input_dev, EV_MSC, MSC_SCAN);
225
226 error = input_register_polled_device(jornadakbd->poll_dev);
227 if (error)
228 goto failed;
229
230 return 0;
231
232 failed:
233 printk(KERN_ERR "Jornadakbd: failed to register driver, error: %d\n",
234 error);
235 platform_set_drvdata(pdev, NULL);
236 input_free_polled_device(poll_dev);
237 kfree(jornadakbd);
238 return error;
239
240}
241
242static int __devexit jornada680kbd_remove(struct platform_device *pdev)
243{
244 struct jornadakbd *jornadakbd = platform_get_drvdata(pdev);
245
246 platform_set_drvdata(pdev, NULL);
247 input_unregister_polled_device(jornadakbd->poll_dev);
248 input_free_polled_device(jornadakbd->poll_dev);
249 kfree(jornadakbd);
250
251 return 0;
252}
253
254static struct platform_driver jornada680kbd_driver = {
255 .driver = {
256 .name = "jornada680_kbd",
257 },
258 .probe = jornada680kbd_probe,
259 .remove = __devexit_p(jornada680kbd_remove),
260};
261
262static int __init jornada680kbd_init(void)
263{
264 return platform_driver_register(&jornada680kbd_driver);
265}
266
267static void __exit jornada680kbd_exit(void)
268{
269 platform_driver_unregister(&jornada680kbd_driver);
270}
271
272module_init(jornada680kbd_init);
273module_exit(jornada680kbd_exit);
274
275MODULE_AUTHOR("Kristoffer Ericson <kristoffer.ericson@gmail.com>");
276MODULE_DESCRIPTION("HP Jornada 620/660/680/690 Keyboard Driver");
277MODULE_LICENSE("GPLv2");
diff --git a/drivers/input/keyboard/jornada720_kbd.c b/drivers/input/keyboard/jornada720_kbd.c
new file mode 100644
index 000000000000..e6696b3c9416
--- /dev/null
+++ b/drivers/input/keyboard/jornada720_kbd.c
@@ -0,0 +1,185 @@
1/*
2 * drivers/input/keyboard/jornada720_kbd.c
3 *
4 * HP Jornada 720 keyboard platform driver
5 *
6 * Copyright (C) 2006/2007 Kristoffer Ericson <Kristoffer.Ericson@Gmail.com>
7 *
8 * Copyright (C) 2006 jornada 720 kbd driver by
9 Filip Zyzniewsk <Filip.Zyzniewski@tefnet.plX
10 * based on (C) 2004 jornada 720 kbd driver by
11 Alex Lange <chicken@handhelds.org>
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
16 *
17 */
18#include <linux/device.h>
19#include <linux/errno.h>
20#include <linux/init.h>
21#include <linux/interrupt.h>
22#include <linux/init.h>
23#include <linux/input.h>
24#include <linux/kernel.h>
25#include <linux/module.h>
26#include <linux/platform_device.h>
27
28#include <asm/arch/jornada720.h>
29#include <asm/hardware.h>
30
31MODULE_AUTHOR("Kristoffer Ericson <Kristoffer.Ericson@gmail.com>");
32MODULE_DESCRIPTION("HP Jornada 710/720/728 keyboard driver");
33MODULE_LICENSE("GPLv2");
34
35static unsigned short jornada_std_keymap[128] = { /* ROW */
36 0, KEY_ESC, KEY_F1, KEY_F2, KEY_F3, KEY_F4, KEY_F5, KEY_F6, KEY_F7, /* #1 */
37 KEY_F8, KEY_F9, KEY_F10, KEY_F11, KEY_VOLUMEUP, KEY_VOLUMEDOWN, KEY_MUTE, /* -> */
38 0, KEY_1, KEY_2, KEY_3, KEY_4, KEY_5, KEY_6, KEY_7, KEY_8, KEY_9, /* #2 */
39 KEY_0, KEY_MINUS, KEY_EQUAL,0, 0, 0, /* -> */
40 0, KEY_Q, KEY_W, KEY_E, KEY_R, KEY_T, KEY_Y, KEY_U, KEY_I, KEY_O, /* #3 */
41 KEY_P, KEY_BACKSLASH, KEY_BACKSPACE, 0, 0, 0, /* -> */
42 0, KEY_A, KEY_S, KEY_D, KEY_F, KEY_G, KEY_H, KEY_J, KEY_K, KEY_L, /* #4 */
43 KEY_SEMICOLON, KEY_LEFTBRACE, KEY_RIGHTBRACE, 0, 0, 0, /* -> */
44 0, KEY_Z, KEY_X, KEY_C, KEY_V, KEY_B, KEY_N, KEY_M, KEY_COMMA, /* #5 */
45 KEY_DOT, KEY_KPMINUS, KEY_APOSTROPHE, KEY_ENTER, 0, 0,0, /* -> */
46 0, KEY_TAB, 0, KEY_LEFTSHIFT, 0, KEY_APOSTROPHE, 0, 0, 0, 0, /* #6 */
47 KEY_UP, 0, KEY_RIGHTSHIFT, 0, 0, 0,0, 0, 0, 0, 0, KEY_LEFTALT, KEY_GRAVE, /* -> */
48 0, 0, KEY_LEFT, KEY_DOWN, KEY_RIGHT, 0, 0, 0, 0,0, KEY_KPASTERISK, /* -> */
49 KEY_LEFTCTRL, 0, KEY_SPACE, 0, 0, 0, KEY_SLASH, KEY_DELETE, 0, 0, /* -> */
50 0, 0, 0, KEY_POWER, /* -> */
51};
52
53struct jornadakbd {
54 unsigned short keymap[ARRAY_SIZE(jornada_std_keymap)];
55 struct input_dev *input;
56};
57
58static irqreturn_t jornada720_kbd_interrupt(int irq, void *dev_id)
59{
60 struct platform_device *pdev = dev_id;
61 struct jornadakbd *jornadakbd = platform_get_drvdata(pdev);
62 struct input_dev *input = jornadakbd->input;
63 u8 count, kbd_data, scan_code;
64
65 /* startup ssp with spinlock */
66 jornada_ssp_start();
67
68 if (jornada_ssp_inout(GETSCANKEYCODE) != TXDUMMY) {
69 printk(KERN_DEBUG
70 "jornada720_kbd: "
71 "GetKeycode command failed with ETIMEDOUT, "
72 "flushed bus\n");
73 } else {
74 /* How many keycodes are waiting for us? */
75 count = jornada_ssp_byte(TXDUMMY);
76
77 /* Lets drag them out one at a time */
78 while (count--) {
79 /* Exchange TxDummy for location (keymap[kbddata]) */
80 kbd_data = jornada_ssp_byte(TXDUMMY);
81 scan_code = kbd_data & 0x7f;
82
83 input_event(input, EV_MSC, MSC_SCAN, scan_code);
84 input_report_key(input, jornadakbd->keymap[scan_code],
85 !(kbd_data & 0x80));
86 input_sync(input);
87 }
88 }
89
90 /* release spinlock and turn off ssp */
91 jornada_ssp_end();
92
93 return IRQ_HANDLED;
94};
95
96static int __devinit jornada720_kbd_probe(struct platform_device *pdev)
97{
98 struct jornadakbd *jornadakbd;
99 struct input_dev *input_dev;
100 int i, err;
101
102 jornadakbd = kzalloc(sizeof(struct jornadakbd), GFP_KERNEL);
103 input_dev = input_allocate_device();
104 if (!jornadakbd || !input_dev) {
105 err = -ENOMEM;
106 goto fail1;
107 }
108
109 platform_set_drvdata(pdev, jornadakbd);
110
111 memcpy(jornadakbd->keymap, jornada_std_keymap,
112 sizeof(jornada_std_keymap));
113 jornadakbd->input = input_dev;
114
115 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REP);
116 input_dev->name = "HP Jornada 720 keyboard";
117 input_dev->phys = "jornadakbd/input0";
118 input_dev->keycode = jornadakbd->keymap;
119 input_dev->keycodesize = sizeof(unsigned short);
120 input_dev->keycodemax = ARRAY_SIZE(jornada_std_keymap);
121 input_dev->id.bustype = BUS_HOST;
122 input_dev->dev.parent = &pdev->dev;
123
124 for (i = 0; i < ARRAY_SIZE(jornadakbd->keymap); i++)
125 __set_bit(jornadakbd->keymap[i], input_dev->keybit);
126 __clear_bit(KEY_RESERVED, input_dev->keybit);
127
128 input_set_capability(input_dev, EV_MSC, MSC_SCAN);
129
130 err = request_irq(IRQ_GPIO0,
131 jornada720_kbd_interrupt,
132 IRQF_DISABLED | IRQF_TRIGGER_FALLING,
133 "jornadakbd", pdev);
134 if (err) {
135 printk(KERN_INFO "jornadakbd720_kbd: Unable to grab IRQ\n");
136 goto fail1;
137 }
138
139 err = input_register_device(jornadakbd->input);
140 if (err)
141 goto fail2;
142
143 return 0;
144
145 fail2: /* IRQ, DEVICE, MEMORY */
146 free_irq(IRQ_GPIO0, pdev);
147 fail1: /* DEVICE, MEMORY */
148 platform_set_drvdata(pdev, NULL);
149 input_free_device(input_dev);
150 kfree(jornadakbd);
151 return err;
152};
153
154static int __devexit jornada720_kbd_remove(struct platform_device *pdev)
155{
156 struct jornadakbd *jornadakbd = platform_get_drvdata(pdev);
157
158 free_irq(IRQ_GPIO0, pdev);
159 platform_set_drvdata(pdev, NULL);
160 input_unregister_device(jornadakbd->input);
161 kfree(jornadakbd);
162
163 return 0;
164}
165
166static struct platform_driver jornada720_kbd_driver = {
167 .driver = {
168 .name = "jornada720_kbd",
169 },
170 .probe = jornada720_kbd_probe,
171 .remove = __devexit_p(jornada720_kbd_remove),
172};
173
174static int __init jornada720_kbd_init(void)
175{
176 return platform_driver_register(&jornada720_kbd_driver);
177}
178
179static void __exit jornada720_kbd_exit(void)
180{
181 platform_driver_unregister(&jornada720_kbd_driver);
182}
183
184module_init(jornada720_kbd_init);
185module_exit(jornada720_kbd_exit);
diff --git a/drivers/input/keyboard/maple_keyb.c b/drivers/input/keyboard/maple_keyb.c
new file mode 100644
index 000000000000..2b404284c28a
--- /dev/null
+++ b/drivers/input/keyboard/maple_keyb.c
@@ -0,0 +1,252 @@
1/*
2 * SEGA Dreamcast keyboard driver
3 * Based on drivers/usb/usbkbd.c
4 * Copyright YAEGASHI Takeshi, 2001
5 * Porting to 2.6 Copyright Adrian McMenamin, 2007
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 as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see the file COPYING, or write
19 * to the Free Software Foundation, Inc.,
20 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23#include <linux/kernel.h>
24#include <linux/slab.h>
25#include <linux/input.h>
26#include <linux/module.h>
27#include <linux/init.h>
28#include <linux/timer.h>
29#include <linux/maple.h>
30#include <asm/mach/maple.h>
31
32/* Very simple mutex to ensure proper cleanup */
33static DEFINE_MUTEX(maple_keyb_mutex);
34
35#define NR_SCANCODES 256
36
37MODULE_AUTHOR("YAEGASHI Takeshi, Adrian McMenamin");
38MODULE_DESCRIPTION("SEGA Dreamcast keyboard driver");
39MODULE_LICENSE("GPL");
40
41struct dc_kbd {
42 struct input_dev *dev;
43 unsigned short keycode[NR_SCANCODES];
44 unsigned char new[8];
45 unsigned char old[8];
46};
47
48static const unsigned short dc_kbd_keycode[NR_SCANCODES] = {
49 KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_A, KEY_B, KEY_C, KEY_D,
50 KEY_E, KEY_F, KEY_G, KEY_H, KEY_I, KEY_J, KEY_K, KEY_L,
51 KEY_M, KEY_N, KEY_O, KEY_P, KEY_Q, KEY_R, KEY_S, KEY_T,
52 KEY_U, KEY_V, KEY_W, KEY_X, KEY_Y, KEY_Z, KEY_1, KEY_2,
53 KEY_3, KEY_4, KEY_5, KEY_6, KEY_7, KEY_8, KEY_9, KEY_0,
54 KEY_ENTER, KEY_ESC, KEY_BACKSPACE, KEY_TAB, KEY_SPACE, KEY_MINUS, KEY_EQUAL, KEY_LEFTBRACE,
55 KEY_RIGHTBRACE, KEY_BACKSLASH, KEY_BACKSLASH, KEY_SEMICOLON, KEY_APOSTROPHE, KEY_GRAVE, KEY_COMMA,
56 KEY_DOT, KEY_SLASH, KEY_CAPSLOCK, KEY_F1, KEY_F2, KEY_F3, KEY_F4, KEY_F5, KEY_F6,
57 KEY_F7, KEY_F8, KEY_F9, KEY_F10, KEY_F11, KEY_F12, KEY_SYSRQ,
58 KEY_SCROLLLOCK, KEY_PAUSE, KEY_INSERT, KEY_HOME, KEY_PAGEUP, KEY_DELETE,
59 KEY_END, KEY_PAGEDOWN, KEY_RIGHT, KEY_LEFT, KEY_DOWN, KEY_UP,
60 KEY_NUMLOCK, KEY_KPSLASH, KEY_KPASTERISK, KEY_KPMINUS, KEY_KPPLUS, KEY_KPENTER, KEY_KP1, KEY_KP2,
61 KEY_KP3, KEY_KP4, KEY_KP5, KEY_KP6, KEY_KP7, KEY_KP8, KEY_KP9, KEY_KP0, KEY_KPDOT,
62 KEY_102ND, KEY_COMPOSE, KEY_POWER, KEY_KPEQUAL, KEY_F13, KEY_F14, KEY_F15,
63 KEY_F16, KEY_F17, KEY_F18, KEY_F19, KEY_F20,
64 KEY_F21, KEY_F22, KEY_F23, KEY_F24, KEY_OPEN, KEY_HELP, KEY_PROPS, KEY_FRONT,
65 KEY_STOP, KEY_AGAIN, KEY_UNDO, KEY_CUT, KEY_COPY, KEY_PASTE, KEY_FIND, KEY_MUTE,
66 KEY_VOLUMEUP, KEY_VOLUMEDOWN, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_KPCOMMA, KEY_RESERVED, KEY_RO, KEY_KATAKANAHIRAGANA , KEY_YEN,
67 KEY_HENKAN, KEY_MUHENKAN, KEY_KPJPCOMMA, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED,
68 KEY_HANGEUL, KEY_HANJA, KEY_KATAKANA, KEY_HIRAGANA, KEY_ZENKAKUHANKAKU, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED,
69 KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED,
70 KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED,
71 KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED,
72 KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED,
73 KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED,
74 KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED,
75 KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED,
76 KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED,
77 KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED,
78 KEY_LEFTCTRL, KEY_LEFTSHIFT, KEY_LEFTALT, KEY_LEFTMETA, KEY_RIGHTCTRL, KEY_RIGHTSHIFT, KEY_RIGHTALT, KEY_RIGHTMETA,
79 KEY_PLAYPAUSE, KEY_STOPCD, KEY_PREVIOUSSONG, KEY_NEXTSONG, KEY_EJECTCD, KEY_VOLUMEUP, KEY_VOLUMEDOWN, KEY_MUTE,
80 KEY_WWW, KEY_BACK, KEY_FORWARD, KEY_STOP, KEY_FIND, KEY_SCROLLUP, KEY_SCROLLDOWN, KEY_EDIT, KEY_SLEEP,
81 KEY_SCREENLOCK, KEY_REFRESH, KEY_CALC, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED, KEY_RESERVED
82};
83
84static void dc_scan_kbd(struct dc_kbd *kbd)
85{
86 struct input_dev *dev = kbd->dev;
87 void *ptr;
88 int code, keycode;
89 int i;
90
91 for (i = 0; i < 8; i++) {
92 code = i + 224;
93 keycode = kbd->keycode[code];
94 input_event(dev, EV_MSC, MSC_SCAN, code);
95 input_report_key(dev, keycode, (kbd->new[0] >> i) & 1);
96 }
97
98 for (i = 2; i < 8; i++) {
99 ptr = memchr(kbd->new + 2, kbd->old[i], 6);
100 code = kbd->old[i];
101 if (code > 3 && ptr == NULL) {
102 keycode = kbd->keycode[code];
103 if (keycode) {
104 input_event(dev, EV_MSC, MSC_SCAN, code);
105 input_report_key(dev, keycode, 0);
106 } else
107 printk(KERN_DEBUG "maple_keyb: "
108 "Unknown key (scancode %#x) released.",
109 code);
110 }
111 ptr = memchr(kbd->old + 2, kbd->new[i], 6);
112 code = kbd->new[i];
113 if (code > 3 && ptr) {
114 keycode = kbd->keycode[code];
115 if (keycode) {
116 input_event(dev, EV_MSC, MSC_SCAN, code);
117 input_report_key(dev, keycode, 1);
118 } else
119 printk(KERN_DEBUG "maple_keyb: "
120 "Unknown key (scancode %#x) pressed.",
121 code);
122 }
123 }
124 input_sync(dev);
125 memcpy(kbd->old, kbd->new, 8);
126}
127
128static void dc_kbd_callback(struct mapleq *mq)
129{
130 struct maple_device *mapledev = mq->dev;
131 struct dc_kbd *kbd = mapledev->private_data;
132 unsigned long *buf = mq->recvbuf;
133
134 /*
135 * We should always be getting the lock because the only
136 * time it may be locked if driver is in cleanup phase.
137 */
138 if (likely(mutex_trylock(&maple_keyb_mutex))) {
139
140 if (buf[1] == mapledev->function) {
141 memcpy(kbd->new, buf + 2, 8);
142 dc_scan_kbd(kbd);
143 }
144
145 mutex_unlock(&maple_keyb_mutex);
146 }
147}
148
149static int dc_kbd_connect(struct maple_device *mdev)
150{
151 int i, error;
152 struct dc_kbd *kbd;
153 struct input_dev *dev;
154
155 if (!(mdev->function & MAPLE_FUNC_KEYBOARD))
156 return -EINVAL;
157
158 kbd = kzalloc(sizeof(struct dc_kbd), GFP_KERNEL);
159 dev = input_allocate_device();
160 if (!kbd || !dev) {
161 error = -ENOMEM;
162 goto fail;
163 }
164
165 mdev->private_data = kbd;
166
167 kbd->dev = dev;
168 memcpy(kbd->keycode, dc_kbd_keycode, sizeof(kbd->keycode));
169
170 dev->name = mdev->product_name;
171 dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REP);
172 dev->keycode = kbd->keycode;
173 dev->keycodesize = sizeof (unsigned short);
174 dev->keycodemax = ARRAY_SIZE(kbd->keycode);
175 dev->id.bustype = BUS_HOST;
176 dev->dev.parent = &mdev->dev;
177
178 for (i = 0; i < NR_SCANCODES; i++)
179 __set_bit(dc_kbd_keycode[i], dev->keybit);
180 __clear_bit(KEY_RESERVED, dev->keybit);
181
182 input_set_capability(dev, EV_MSC, MSC_SCAN);
183 input_set_drvdata(dev, kbd);
184
185 error = input_register_device(dev);
186 if (error)
187 goto fail;
188
189 /* Maple polling is locked to VBLANK - which may be just 50/s */
190 maple_getcond_callback(mdev, dc_kbd_callback, HZ/50, MAPLE_FUNC_KEYBOARD);
191 return 0;
192
193 fail:
194 input_free_device(dev);
195 kfree(kbd);
196 mdev->private_data = NULL;
197 return error;
198}
199
200static void dc_kbd_disconnect(struct maple_device *mdev)
201{
202 struct dc_kbd *kbd;
203
204 mutex_lock(&maple_keyb_mutex);
205
206 kbd = mdev->private_data;
207 mdev->private_data = NULL;
208 input_unregister_device(kbd->dev);
209 kfree(kbd);
210
211 mutex_unlock(&maple_keyb_mutex);
212}
213
214/* allow the keyboard to be used */
215static int probe_maple_kbd(struct device *dev)
216{
217 struct maple_device *mdev = to_maple_dev(dev);
218 struct maple_driver *mdrv = to_maple_driver(dev->driver);
219 int error;
220
221 error = dc_kbd_connect(mdev);
222 if (error)
223 return error;
224
225 mdev->driver = mdrv;
226 mdev->registered = 1;
227
228 return 0;
229}
230
231static struct maple_driver dc_kbd_driver = {
232 .function = MAPLE_FUNC_KEYBOARD,
233 .connect = dc_kbd_connect,
234 .disconnect = dc_kbd_disconnect,
235 .drv = {
236 .name = "Dreamcast_keyboard",
237 .probe = probe_maple_kbd,
238 },
239};
240
241static int __init dc_kbd_init(void)
242{
243 return maple_driver_register(&dc_kbd_driver.drv);
244}
245
246static void __exit dc_kbd_exit(void)
247{
248 driver_unregister(&dc_kbd_driver.drv);
249}
250
251module_init(dc_kbd_init);
252module_exit(dc_kbd_exit);
diff --git a/drivers/input/keyboard/omap-keypad.c b/drivers/input/keyboard/omap-keypad.c
index 3a228634f101..76f1969552c5 100644
--- a/drivers/input/keyboard/omap-keypad.c
+++ b/drivers/input/keyboard/omap-keypad.c
@@ -233,7 +233,7 @@ static void omap_kp_tasklet(unsigned long data)
233 omap_writew(0, OMAP_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT); 233 omap_writew(0, OMAP_MPUIO_BASE + OMAP_MPUIO_KBD_MASKIT);
234 kp_cur_group = -1; 234 kp_cur_group = -1;
235 } 235 }
236 } 236 }
237} 237}
238 238
239static ssize_t omap_kp_enable_show(struct device *dev, 239static ssize_t omap_kp_enable_show(struct device *dev,
@@ -318,7 +318,7 @@ static int __init omap_kp_probe(struct platform_device *pdev)
318 keymap = pdata->keymap; 318 keymap = pdata->keymap;
319 319
320 if (pdata->rep) 320 if (pdata->rep)
321 set_bit(EV_REP, input_dev->evbit); 321 __set_bit(EV_REP, input_dev->evbit);
322 322
323 if (pdata->delay) 323 if (pdata->delay)
324 omap_kp->delay = pdata->delay; 324 omap_kp->delay = pdata->delay;
@@ -365,9 +365,9 @@ static int __init omap_kp_probe(struct platform_device *pdev)
365 goto err2; 365 goto err2;
366 366
367 /* setup input device */ 367 /* setup input device */
368 set_bit(EV_KEY, input_dev->evbit); 368 __set_bit(EV_KEY, input_dev->evbit);
369 for (i = 0; keymap[i] != 0; i++) 369 for (i = 0; keymap[i] != 0; i++)
370 set_bit(keymap[i] & KEY_MAX, input_dev->keybit); 370 __set_bit(keymap[i] & KEY_MAX, input_dev->keybit);
371 input_dev->name = "omap-keypad"; 371 input_dev->name = "omap-keypad";
372 input_dev->phys = "omap-keypad/input0"; 372 input_dev->phys = "omap-keypad/input0";
373 input_dev->dev.parent = &pdev->dev; 373 input_dev->dev.parent = &pdev->dev;
@@ -377,10 +377,6 @@ static int __init omap_kp_probe(struct platform_device *pdev)
377 input_dev->id.product = 0x0001; 377 input_dev->id.product = 0x0001;
378 input_dev->id.version = 0x0100; 378 input_dev->id.version = 0x0100;
379 379
380 input_dev->keycode = keymap;
381 input_dev->keycodesize = sizeof(unsigned int);
382 input_dev->keycodemax = pdata->keymapsize;
383
384 ret = input_register_device(omap_kp->input); 380 ret = input_register_device(omap_kp->input);
385 if (ret < 0) { 381 if (ret < 0) {
386 printk(KERN_ERR "Unable to register omap-keypad input device\n"); 382 printk(KERN_ERR "Unable to register omap-keypad input device\n");
@@ -403,15 +399,15 @@ static int __init omap_kp_probe(struct platform_device *pdev)
403 } else { 399 } else {
404 for (irq_idx = 0; irq_idx < omap_kp->rows; irq_idx++) { 400 for (irq_idx = 0; irq_idx < omap_kp->rows; irq_idx++) {
405 if (request_irq(OMAP_GPIO_IRQ(row_gpios[irq_idx]), 401 if (request_irq(OMAP_GPIO_IRQ(row_gpios[irq_idx]),
406 omap_kp_interrupt, 402 omap_kp_interrupt,
407 IRQF_TRIGGER_FALLING, 403 IRQF_TRIGGER_FALLING,
408 "omap-keypad", omap_kp) < 0) 404 "omap-keypad", omap_kp) < 0)
409 goto err5; 405 goto err5;
410 } 406 }
411 } 407 }
412 return 0; 408 return 0;
413err5: 409err5:
414 for (i = irq_idx-1; i >=0; i--) 410 for (i = irq_idx - 1; i >=0; i--)
415 free_irq(row_gpios[i], 0); 411 free_irq(row_gpios[i], 0);
416err4: 412err4:
417 input_unregister_device(omap_kp->input); 413 input_unregister_device(omap_kp->input);
@@ -440,9 +436,9 @@ static int omap_kp_remove(struct platform_device *pdev)
440 if (cpu_is_omap24xx()) { 436 if (cpu_is_omap24xx()) {
441 int i; 437 int i;
442 for (i = 0; i < omap_kp->cols; i++) 438 for (i = 0; i < omap_kp->cols; i++)
443 omap_free_gpio(col_gpios[i]); 439 omap_free_gpio(col_gpios[i]);
444 for (i = 0; i < omap_kp->rows; i++) { 440 for (i = 0; i < omap_kp->rows; i++) {
445 omap_free_gpio(row_gpios[i]); 441 omap_free_gpio(row_gpios[i]);
446 free_irq(OMAP_GPIO_IRQ(row_gpios[i]), 0); 442 free_irq(OMAP_GPIO_IRQ(row_gpios[i]), 0);
447 } 443 }
448 } else { 444 } else {
diff --git a/drivers/input/mouse/alps.c b/drivers/input/mouse/alps.c
index 2c5f11a4f6b4..64d70a9b714c 100644
--- a/drivers/input/mouse/alps.c
+++ b/drivers/input/mouse/alps.c
@@ -48,11 +48,13 @@ static const struct alps_model_info alps_model_data[] = {
48 { { 0x63, 0x02, 0x50 }, 0xef, 0xef, ALPS_FW_BK_1 }, /* NEC Versa L320 */ 48 { { 0x63, 0x02, 0x50 }, 0xef, 0xef, ALPS_FW_BK_1 }, /* NEC Versa L320 */
49 { { 0x63, 0x02, 0x64 }, 0xf8, 0xf8, 0 }, 49 { { 0x63, 0x02, 0x64 }, 0xf8, 0xf8, 0 },
50 { { 0x63, 0x03, 0xc8 }, 0xf8, 0xf8, ALPS_PASS }, /* Dell Latitude D800 */ 50 { { 0x63, 0x03, 0xc8 }, 0xf8, 0xf8, ALPS_PASS }, /* Dell Latitude D800 */
51 { { 0x73, 0x00, 0x0a }, 0xf8, 0xf8, ALPS_DUALPOINT }, /* ThinkPad R61 8918-5QG */
51 { { 0x73, 0x02, 0x0a }, 0xf8, 0xf8, 0 }, 52 { { 0x73, 0x02, 0x0a }, 0xf8, 0xf8, 0 },
52 { { 0x73, 0x02, 0x14 }, 0xf8, 0xf8, ALPS_FW_BK_2 }, /* Ahtec Laptop */ 53 { { 0x73, 0x02, 0x14 }, 0xf8, 0xf8, ALPS_FW_BK_2 }, /* Ahtec Laptop */
53 { { 0x20, 0x02, 0x0e }, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* XXX */ 54 { { 0x20, 0x02, 0x0e }, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, /* XXX */
54 { { 0x22, 0x02, 0x0a }, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT }, 55 { { 0x22, 0x02, 0x0a }, 0xf8, 0xf8, ALPS_PASS | ALPS_DUALPOINT },
55 { { 0x22, 0x02, 0x14 }, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT }, /* Dell Latitude D600 */ 56 { { 0x22, 0x02, 0x14 }, 0xff, 0xff, ALPS_PASS | ALPS_DUALPOINT }, /* Dell Latitude D600 */
57 { { 0x73, 0x02, 0x50 }, 0xcf, 0xff, ALPS_FW_BK_1 } /* Dell Vostro 1400 */
56}; 58};
57 59
58/* 60/*
diff --git a/drivers/input/mouse/lifebook.c b/drivers/input/mouse/lifebook.c
index 608674d0be8b..d7de4c53b3d8 100644
--- a/drivers/input/mouse/lifebook.c
+++ b/drivers/input/mouse/lifebook.c
@@ -97,6 +97,14 @@ static const struct dmi_system_id lifebook_dmi_table[] = {
97 .callback = lifebook_set_6byte_proto, 97 .callback = lifebook_set_6byte_proto,
98 }, 98 },
99 { 99 {
100 .ident = "CF-72",
101 .matches = {
102 DMI_MATCH(DMI_PRODUCT_NAME, "CF-72"),
103 },
104 .callback = lifebook_set_serio_phys,
105 .driver_data = "isa0060/serio3",
106 },
107 {
100 .ident = "Lifebook B142", 108 .ident = "Lifebook B142",
101 .matches = { 109 .matches = {
102 DMI_MATCH(DMI_PRODUCT_NAME, "LifeBook B142"), 110 DMI_MATCH(DMI_PRODUCT_NAME, "LifeBook B142"),
@@ -282,7 +290,7 @@ static int lifebook_create_relative_device(struct psmouse *psmouse)
282int lifebook_init(struct psmouse *psmouse) 290int lifebook_init(struct psmouse *psmouse)
283{ 291{
284 struct input_dev *dev1 = psmouse->dev; 292 struct input_dev *dev1 = psmouse->dev;
285 int max_coord = lifebook_use_6byte_proto ? 1024 : 4096; 293 int max_coord = lifebook_use_6byte_proto ? 4096 : 1024;
286 294
287 if (lifebook_absolute_mode(psmouse)) 295 if (lifebook_absolute_mode(psmouse))
288 return -1; 296 return -1;
diff --git a/drivers/input/mouse/psmouse-base.c b/drivers/input/mouse/psmouse-base.c
index b9f0fb2530e2..073525756532 100644
--- a/drivers/input/mouse/psmouse-base.c
+++ b/drivers/input/mouse/psmouse-base.c
@@ -648,9 +648,10 @@ static int psmouse_extensions(struct psmouse *psmouse,
648 648
649/* 649/*
650 * Reset to defaults in case the device got confused by extended 650 * Reset to defaults in case the device got confused by extended
651 * protocol probes. Note that we do full reset becuase some mice 651 * protocol probes. Note that we follow up with full reset because
652 * put themselves to sleep when see PSMOUSE_RESET_DIS. 652 * some mice put themselves to sleep when they see PSMOUSE_RESET_DIS.
653 */ 653 */
654 ps2_command(&psmouse->ps2dev, NULL, PSMOUSE_CMD_RESET_DIS);
654 psmouse_reset(psmouse); 655 psmouse_reset(psmouse);
655 656
656 if (max_proto >= PSMOUSE_IMEX && im_explorer_detect(psmouse, set_properties) == 0) 657 if (max_proto >= PSMOUSE_IMEX && im_explorer_detect(psmouse, set_properties) == 0)
diff --git a/drivers/input/mousedev.c b/drivers/input/mousedev.c
index 9173916b8be5..cc36edbb912f 100644
--- a/drivers/input/mousedev.c
+++ b/drivers/input/mousedev.c
@@ -61,9 +61,11 @@ struct mousedev {
61 int open; 61 int open;
62 int minor; 62 int minor;
63 char name[16]; 63 char name[16];
64 struct input_handle handle;
64 wait_queue_head_t wait; 65 wait_queue_head_t wait;
65 struct list_head client_list; 66 struct list_head client_list;
66 struct input_handle handle; 67 spinlock_t client_lock; /* protects client_list */
68 struct mutex mutex;
67 struct device dev; 69 struct device dev;
68 70
69 struct list_head mixdev_node; 71 struct list_head mixdev_node;
@@ -113,108 +115,137 @@ static unsigned char mousedev_imex_seq[] = { 0xf3, 200, 0xf3, 200, 0xf3, 80 };
113static struct input_handler mousedev_handler; 115static struct input_handler mousedev_handler;
114 116
115static struct mousedev *mousedev_table[MOUSEDEV_MINORS]; 117static struct mousedev *mousedev_table[MOUSEDEV_MINORS];
118static DEFINE_MUTEX(mousedev_table_mutex);
116static struct mousedev *mousedev_mix; 119static struct mousedev *mousedev_mix;
117static LIST_HEAD(mousedev_mix_list); 120static LIST_HEAD(mousedev_mix_list);
118 121
122static void mixdev_open_devices(void);
123static void mixdev_close_devices(void);
124
119#define fx(i) (mousedev->old_x[(mousedev->pkt_count - (i)) & 03]) 125#define fx(i) (mousedev->old_x[(mousedev->pkt_count - (i)) & 03])
120#define fy(i) (mousedev->old_y[(mousedev->pkt_count - (i)) & 03]) 126#define fy(i) (mousedev->old_y[(mousedev->pkt_count - (i)) & 03])
121 127
122static void mousedev_touchpad_event(struct input_dev *dev, struct mousedev *mousedev, unsigned int code, int value) 128static void mousedev_touchpad_event(struct input_dev *dev,
129 struct mousedev *mousedev,
130 unsigned int code, int value)
123{ 131{
124 int size, tmp; 132 int size, tmp;
125 enum { FRACTION_DENOM = 128 }; 133 enum { FRACTION_DENOM = 128 };
126 134
127 switch (code) { 135 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 136
141 case ABS_Y: 137 case ABS_X:
142 fy(0) = value; 138 fx(0) = value;
143 if (mousedev->touch && mousedev->pkt_count >= 2) { 139 if (mousedev->touch && mousedev->pkt_count >= 2) {
144 /* use X size to keep the same scale */ 140 size = dev->absmax[ABS_X] - dev->absmin[ABS_X];
145 size = dev->absmax[ABS_X] - dev->absmin[ABS_X]; 141 if (size == 0)
146 if (size == 0) 142 size = 256 * 2;
147 size = 256 * 2; 143 tmp = ((value - fx(2)) * 256 * FRACTION_DENOM) / size;
148 tmp = -((value - fy(2)) * (256 * FRACTION_DENOM)) / size; 144 tmp += mousedev->frac_dx;
149 tmp += mousedev->frac_dy; 145 mousedev->packet.dx = tmp / FRACTION_DENOM;
150 mousedev->packet.dy = tmp / FRACTION_DENOM; 146 mousedev->frac_dx =
151 mousedev->frac_dy = tmp - mousedev->packet.dy * FRACTION_DENOM; 147 tmp - mousedev->packet.dx * FRACTION_DENOM;
152 } 148 }
153 break; 149 break;
150
151 case ABS_Y:
152 fy(0) = value;
153 if (mousedev->touch && mousedev->pkt_count >= 2) {
154 /* use X size to keep the same scale */
155 size = dev->absmax[ABS_X] - dev->absmin[ABS_X];
156 if (size == 0)
157 size = 256 * 2;
158 tmp = -((value - fy(2)) * 256 * FRACTION_DENOM) / size;
159 tmp += mousedev->frac_dy;
160 mousedev->packet.dy = tmp / FRACTION_DENOM;
161 mousedev->frac_dy = tmp -
162 mousedev->packet.dy * FRACTION_DENOM;
163 }
164 break;
154 } 165 }
155} 166}
156 167
157static void mousedev_abs_event(struct input_dev *dev, struct mousedev *mousedev, unsigned int code, int value) 168static void mousedev_abs_event(struct input_dev *dev, struct mousedev *mousedev,
169 unsigned int code, int value)
158{ 170{
159 int size; 171 int size;
160 172
161 switch (code) { 173 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
174 case ABS_Y: 175 case ABS_X:
175 size = dev->absmax[ABS_Y] - dev->absmin[ABS_Y]; 176 size = dev->absmax[ABS_X] - dev->absmin[ABS_X];
176 if (size == 0) 177 if (size == 0)
177 size = yres ? : 1; 178 size = xres ? : 1;
178 if (value > dev->absmax[ABS_Y]) 179 if (value > dev->absmax[ABS_X])
179 value = dev->absmax[ABS_Y]; 180 value = dev->absmax[ABS_X];
180 if (value < dev->absmin[ABS_Y]) 181 if (value < dev->absmin[ABS_X])
181 value = dev->absmin[ABS_Y]; 182 value = dev->absmin[ABS_X];
182 mousedev->packet.y = yres - ((value - dev->absmin[ABS_Y]) * yres) / size; 183 mousedev->packet.x =
183 mousedev->packet.abs_event = 1; 184 ((value - dev->absmin[ABS_X]) * xres) / size;
184 break; 185 mousedev->packet.abs_event = 1;
186 break;
187
188 case ABS_Y:
189 size = dev->absmax[ABS_Y] - dev->absmin[ABS_Y];
190 if (size == 0)
191 size = yres ? : 1;
192 if (value > dev->absmax[ABS_Y])
193 value = dev->absmax[ABS_Y];
194 if (value < dev->absmin[ABS_Y])
195 value = dev->absmin[ABS_Y];
196 mousedev->packet.y = yres -
197 ((value - dev->absmin[ABS_Y]) * yres) / size;
198 mousedev->packet.abs_event = 1;
199 break;
185 } 200 }
186} 201}
187 202
188static void mousedev_rel_event(struct mousedev *mousedev, unsigned int code, int value) 203static void mousedev_rel_event(struct mousedev *mousedev,
204 unsigned int code, int value)
189{ 205{
190 switch (code) { 206 switch (code) {
191 case REL_X: mousedev->packet.dx += value; break; 207 case REL_X:
192 case REL_Y: mousedev->packet.dy -= value; break; 208 mousedev->packet.dx += value;
193 case REL_WHEEL: mousedev->packet.dz -= value; break; 209 break;
210
211 case REL_Y:
212 mousedev->packet.dy -= value;
213 break;
214
215 case REL_WHEEL:
216 mousedev->packet.dz -= value;
217 break;
194 } 218 }
195} 219}
196 220
197static void mousedev_key_event(struct mousedev *mousedev, unsigned int code, int value) 221static void mousedev_key_event(struct mousedev *mousedev,
222 unsigned int code, int value)
198{ 223{
199 int index; 224 int index;
200 225
201 switch (code) { 226 switch (code) {
202 case BTN_TOUCH: 227
203 case BTN_0: 228 case BTN_TOUCH:
204 case BTN_LEFT: index = 0; break; 229 case BTN_0:
205 case BTN_STYLUS: 230 case BTN_LEFT: index = 0; break;
206 case BTN_1: 231
207 case BTN_RIGHT: index = 1; break; 232 case BTN_STYLUS:
208 case BTN_2: 233 case BTN_1:
209 case BTN_FORWARD: 234 case BTN_RIGHT: index = 1; break;
210 case BTN_STYLUS2: 235
211 case BTN_MIDDLE: index = 2; break; 236 case BTN_2:
212 case BTN_3: 237 case BTN_FORWARD:
213 case BTN_BACK: 238 case BTN_STYLUS2:
214 case BTN_SIDE: index = 3; break; 239 case BTN_MIDDLE: index = 2; break;
215 case BTN_4: 240
216 case BTN_EXTRA: index = 4; break; 241 case BTN_3:
217 default: return; 242 case BTN_BACK:
243 case BTN_SIDE: index = 3; break;
244
245 case BTN_4:
246 case BTN_EXTRA: index = 4; break;
247
248 default: return;
218 } 249 }
219 250
220 if (value) { 251 if (value) {
@@ -226,19 +257,22 @@ static void mousedev_key_event(struct mousedev *mousedev, unsigned int code, int
226 } 257 }
227} 258}
228 259
229static void mousedev_notify_readers(struct mousedev *mousedev, struct mousedev_hw_data *packet) 260static void mousedev_notify_readers(struct mousedev *mousedev,
261 struct mousedev_hw_data *packet)
230{ 262{
231 struct mousedev_client *client; 263 struct mousedev_client *client;
232 struct mousedev_motion *p; 264 struct mousedev_motion *p;
233 unsigned long flags; 265 unsigned int new_head;
234 int wake_readers = 0; 266 int wake_readers = 0;
235 267
236 list_for_each_entry(client, &mousedev->client_list, node) { 268 list_for_each_entry_rcu(client, &mousedev->client_list, node) {
237 spin_lock_irqsave(&client->packet_lock, flags); 269
270 /* Just acquire the lock, interrupts already disabled */
271 spin_lock(&client->packet_lock);
238 272
239 p = &client->packets[client->head]; 273 p = &client->packets[client->head];
240 if (client->ready && p->buttons != mousedev->packet.buttons) { 274 if (client->ready && p->buttons != mousedev->packet.buttons) {
241 unsigned int new_head = (client->head + 1) % PACKET_QUEUE_LEN; 275 new_head = (client->head + 1) % PACKET_QUEUE_LEN;
242 if (new_head != client->tail) { 276 if (new_head != client->tail) {
243 p = &client->packets[client->head = new_head]; 277 p = &client->packets[client->head = new_head];
244 memset(p, 0, sizeof(struct mousedev_motion)); 278 memset(p, 0, sizeof(struct mousedev_motion));
@@ -253,19 +287,22 @@ static void mousedev_notify_readers(struct mousedev *mousedev, struct mousedev_h
253 } 287 }
254 288
255 client->pos_x += packet->dx; 289 client->pos_x += packet->dx;
256 client->pos_x = client->pos_x < 0 ? 0 : (client->pos_x >= xres ? xres : client->pos_x); 290 client->pos_x = client->pos_x < 0 ?
291 0 : (client->pos_x >= xres ? xres : client->pos_x);
257 client->pos_y += packet->dy; 292 client->pos_y += packet->dy;
258 client->pos_y = client->pos_y < 0 ? 0 : (client->pos_y >= yres ? yres : client->pos_y); 293 client->pos_y = client->pos_y < 0 ?
294 0 : (client->pos_y >= yres ? yres : client->pos_y);
259 295
260 p->dx += packet->dx; 296 p->dx += packet->dx;
261 p->dy += packet->dy; 297 p->dy += packet->dy;
262 p->dz += packet->dz; 298 p->dz += packet->dz;
263 p->buttons = mousedev->packet.buttons; 299 p->buttons = mousedev->packet.buttons;
264 300
265 if (p->dx || p->dy || p->dz || p->buttons != client->last_buttons) 301 if (p->dx || p->dy || p->dz ||
302 p->buttons != client->last_buttons)
266 client->ready = 1; 303 client->ready = 1;
267 304
268 spin_unlock_irqrestore(&client->packet_lock, flags); 305 spin_unlock(&client->packet_lock);
269 306
270 if (client->ready) { 307 if (client->ready) {
271 kill_fasync(&client->fasync, SIGIO, POLL_IN); 308 kill_fasync(&client->fasync, SIGIO, POLL_IN);
@@ -281,7 +318,8 @@ static void mousedev_touchpad_touch(struct mousedev *mousedev, int value)
281{ 318{
282 if (!value) { 319 if (!value) {
283 if (mousedev->touch && 320 if (mousedev->touch &&
284 time_before(jiffies, mousedev->touch + msecs_to_jiffies(tap_time))) { 321 time_before(jiffies,
322 mousedev->touch + msecs_to_jiffies(tap_time))) {
285 /* 323 /*
286 * Toggle left button to emulate tap. 324 * Toggle left button to emulate tap.
287 * We rely on the fact that mousedev_mix always has 0 325 * We rely on the fact that mousedev_mix always has 0
@@ -290,7 +328,8 @@ static void mousedev_touchpad_touch(struct mousedev *mousedev, int value)
290 set_bit(0, &mousedev->packet.buttons); 328 set_bit(0, &mousedev->packet.buttons);
291 set_bit(0, &mousedev_mix->packet.buttons); 329 set_bit(0, &mousedev_mix->packet.buttons);
292 mousedev_notify_readers(mousedev, &mousedev_mix->packet); 330 mousedev_notify_readers(mousedev, &mousedev_mix->packet);
293 mousedev_notify_readers(mousedev_mix, &mousedev_mix->packet); 331 mousedev_notify_readers(mousedev_mix,
332 &mousedev_mix->packet);
294 clear_bit(0, &mousedev->packet.buttons); 333 clear_bit(0, &mousedev->packet.buttons);
295 clear_bit(0, &mousedev_mix->packet.buttons); 334 clear_bit(0, &mousedev_mix->packet.buttons);
296 } 335 }
@@ -302,54 +341,61 @@ static void mousedev_touchpad_touch(struct mousedev *mousedev, int value)
302 mousedev->touch = jiffies; 341 mousedev->touch = jiffies;
303} 342}
304 343
305static void mousedev_event(struct input_handle *handle, unsigned int type, unsigned int code, int value) 344static void mousedev_event(struct input_handle *handle,
345 unsigned int type, unsigned int code, int value)
306{ 346{
307 struct mousedev *mousedev = handle->private; 347 struct mousedev *mousedev = handle->private;
308 348
309 switch (type) { 349 switch (type) {
310 case EV_ABS:
311 /* Ignore joysticks */
312 if (test_bit(BTN_TRIGGER, handle->dev->keybit))
313 return;
314 350
315 if (test_bit(BTN_TOOL_FINGER, handle->dev->keybit)) 351 case EV_ABS:
316 mousedev_touchpad_event(handle->dev, mousedev, code, value); 352 /* Ignore joysticks */
317 else 353 if (test_bit(BTN_TRIGGER, handle->dev->keybit))
318 mousedev_abs_event(handle->dev, mousedev, code, value); 354 return;
319 355
320 break; 356 if (test_bit(BTN_TOOL_FINGER, handle->dev->keybit))
357 mousedev_touchpad_event(handle->dev,
358 mousedev, code, value);
359 else
360 mousedev_abs_event(handle->dev, mousedev, code, value);
321 361
322 case EV_REL: 362 break;
323 mousedev_rel_event(mousedev, code, value);
324 break;
325 363
326 case EV_KEY: 364 case EV_REL:
327 if (value != 2) { 365 mousedev_rel_event(mousedev, code, value);
328 if (code == BTN_TOUCH && test_bit(BTN_TOOL_FINGER, handle->dev->keybit)) 366 break;
329 mousedev_touchpad_touch(mousedev, value);
330 else
331 mousedev_key_event(mousedev, code, value);
332 }
333 break;
334 367
335 case EV_SYN: 368 case EV_KEY:
336 if (code == SYN_REPORT) { 369 if (value != 2) {
337 if (mousedev->touch) { 370 if (code == BTN_TOUCH &&
338 mousedev->pkt_count++; 371 test_bit(BTN_TOOL_FINGER, handle->dev->keybit))
339 /* Input system eats duplicate events, but we need all of them 372 mousedev_touchpad_touch(mousedev, value);
340 * to do correct averaging so apply present one forward 373 else
341 */ 374 mousedev_key_event(mousedev, code, value);
342 fx(0) = fx(1); 375 }
343 fy(0) = fy(1); 376 break;
344 } 377
345 378 case EV_SYN:
346 mousedev_notify_readers(mousedev, &mousedev->packet); 379 if (code == SYN_REPORT) {
347 mousedev_notify_readers(mousedev_mix, &mousedev->packet); 380 if (mousedev->touch) {
348 381 mousedev->pkt_count++;
349 mousedev->packet.dx = mousedev->packet.dy = mousedev->packet.dz = 0; 382 /*
350 mousedev->packet.abs_event = 0; 383 * Input system eats duplicate events,
384 * but we need all of them to do correct
385 * averaging so apply present one forward
386 */
387 fx(0) = fx(1);
388 fy(0) = fy(1);
351 } 389 }
352 break; 390
391 mousedev_notify_readers(mousedev, &mousedev->packet);
392 mousedev_notify_readers(mousedev_mix, &mousedev->packet);
393
394 mousedev->packet.dx = mousedev->packet.dy =
395 mousedev->packet.dz = 0;
396 mousedev->packet.abs_event = 0;
397 }
398 break;
353 } 399 }
354} 400}
355 401
@@ -367,41 +413,48 @@ static void mousedev_free(struct device *dev)
367{ 413{
368 struct mousedev *mousedev = container_of(dev, struct mousedev, dev); 414 struct mousedev *mousedev = container_of(dev, struct mousedev, dev);
369 415
370 mousedev_table[mousedev->minor] = NULL;
371 kfree(mousedev); 416 kfree(mousedev);
372} 417}
373 418
374static int mixdev_add_device(struct mousedev *mousedev) 419static int mousedev_open_device(struct mousedev *mousedev)
375{ 420{
376 int error; 421 int retval;
377 422
378 if (mousedev_mix->open) { 423 retval = mutex_lock_interruptible(&mousedev->mutex);
379 error = input_open_device(&mousedev->handle); 424 if (retval)
380 if (error) 425 return retval;
381 return error;
382 426
383 mousedev->open++; 427 if (mousedev->minor == MOUSEDEV_MIX)
384 mousedev->mixdev_open = 1; 428 mixdev_open_devices();
429 else if (!mousedev->exist)
430 retval = -ENODEV;
431 else if (!mousedev->open++) {
432 retval = input_open_device(&mousedev->handle);
433 if (retval)
434 mousedev->open--;
385 } 435 }
386 436
387 get_device(&mousedev->dev); 437 mutex_unlock(&mousedev->mutex);
388 list_add_tail(&mousedev->mixdev_node, &mousedev_mix_list); 438 return retval;
389
390 return 0;
391} 439}
392 440
393static void mixdev_remove_device(struct mousedev *mousedev) 441static void mousedev_close_device(struct mousedev *mousedev)
394{ 442{
395 if (mousedev->mixdev_open) { 443 mutex_lock(&mousedev->mutex);
396 mousedev->mixdev_open = 0;
397 if (!--mousedev->open && mousedev->exist)
398 input_close_device(&mousedev->handle);
399 }
400 444
401 list_del_init(&mousedev->mixdev_node); 445 if (mousedev->minor == MOUSEDEV_MIX)
402 put_device(&mousedev->dev); 446 mixdev_close_devices();
447 else if (mousedev->exist && !--mousedev->open)
448 input_close_device(&mousedev->handle);
449
450 mutex_unlock(&mousedev->mutex);
403} 451}
404 452
453/*
454 * Open all available devices so they can all be multiplexed in one.
455 * stream. Note that this function is called with mousedev_mix->mutex
456 * held.
457 */
405static void mixdev_open_devices(void) 458static void mixdev_open_devices(void)
406{ 459{
407 struct mousedev *mousedev; 460 struct mousedev *mousedev;
@@ -411,16 +464,19 @@ static void mixdev_open_devices(void)
411 464
412 list_for_each_entry(mousedev, &mousedev_mix_list, mixdev_node) { 465 list_for_each_entry(mousedev, &mousedev_mix_list, mixdev_node) {
413 if (!mousedev->mixdev_open) { 466 if (!mousedev->mixdev_open) {
414 if (!mousedev->open && mousedev->exist) 467 if (mousedev_open_device(mousedev))
415 if (input_open_device(&mousedev->handle)) 468 continue;
416 continue;
417 469
418 mousedev->open++;
419 mousedev->mixdev_open = 1; 470 mousedev->mixdev_open = 1;
420 } 471 }
421 } 472 }
422} 473}
423 474
475/*
476 * Close all devices that were opened as part of multiplexed
477 * device. Note that this function is called with mousedev_mix->mutex
478 * held.
479 */
424static void mixdev_close_devices(void) 480static void mixdev_close_devices(void)
425{ 481{
426 struct mousedev *mousedev; 482 struct mousedev *mousedev;
@@ -431,33 +487,50 @@ static void mixdev_close_devices(void)
431 list_for_each_entry(mousedev, &mousedev_mix_list, mixdev_node) { 487 list_for_each_entry(mousedev, &mousedev_mix_list, mixdev_node) {
432 if (mousedev->mixdev_open) { 488 if (mousedev->mixdev_open) {
433 mousedev->mixdev_open = 0; 489 mousedev->mixdev_open = 0;
434 if (!--mousedev->open && mousedev->exist) 490 mousedev_close_device(mousedev);
435 input_close_device(&mousedev->handle);
436 } 491 }
437 } 492 }
438} 493}
439 494
495
496static void mousedev_attach_client(struct mousedev *mousedev,
497 struct mousedev_client *client)
498{
499 spin_lock(&mousedev->client_lock);
500 list_add_tail_rcu(&client->node, &mousedev->client_list);
501 spin_unlock(&mousedev->client_lock);
502 /*
503 * We don't use synchronize_rcu() here because read-side
504 * critical section is protected by a spinlock (dev->event_lock)
505 * instead of rcu_read_lock().
506 */
507 synchronize_sched();
508}
509
510static void mousedev_detach_client(struct mousedev *mousedev,
511 struct mousedev_client *client)
512{
513 spin_lock(&mousedev->client_lock);
514 list_del_rcu(&client->node);
515 spin_unlock(&mousedev->client_lock);
516 synchronize_sched();
517}
518
440static int mousedev_release(struct inode *inode, struct file *file) 519static int mousedev_release(struct inode *inode, struct file *file)
441{ 520{
442 struct mousedev_client *client = file->private_data; 521 struct mousedev_client *client = file->private_data;
443 struct mousedev *mousedev = client->mousedev; 522 struct mousedev *mousedev = client->mousedev;
444 523
445 mousedev_fasync(-1, file, 0); 524 mousedev_fasync(-1, file, 0);
446 525 mousedev_detach_client(mousedev, client);
447 list_del(&client->node);
448 kfree(client); 526 kfree(client);
449 527
450 if (mousedev->minor == MOUSEDEV_MIX) 528 mousedev_close_device(mousedev);
451 mixdev_close_devices();
452 else if (!--mousedev->open && mousedev->exist)
453 input_close_device(&mousedev->handle);
454
455 put_device(&mousedev->dev); 529 put_device(&mousedev->dev);
456 530
457 return 0; 531 return 0;
458} 532}
459 533
460
461static int mousedev_open(struct inode *inode, struct file *file) 534static int mousedev_open(struct inode *inode, struct file *file)
462{ 535{
463 struct mousedev_client *client; 536 struct mousedev_client *client;
@@ -475,12 +548,17 @@ static int mousedev_open(struct inode *inode, struct file *file)
475 if (i >= MOUSEDEV_MINORS) 548 if (i >= MOUSEDEV_MINORS)
476 return -ENODEV; 549 return -ENODEV;
477 550
551 error = mutex_lock_interruptible(&mousedev_table_mutex);
552 if (error)
553 return error;
478 mousedev = mousedev_table[i]; 554 mousedev = mousedev_table[i];
555 if (mousedev)
556 get_device(&mousedev->dev);
557 mutex_unlock(&mousedev_table_mutex);
558
479 if (!mousedev) 559 if (!mousedev)
480 return -ENODEV; 560 return -ENODEV;
481 561
482 get_device(&mousedev->dev);
483
484 client = kzalloc(sizeof(struct mousedev_client), GFP_KERNEL); 562 client = kzalloc(sizeof(struct mousedev_client), GFP_KERNEL);
485 if (!client) { 563 if (!client) {
486 error = -ENOMEM; 564 error = -ENOMEM;
@@ -491,21 +569,17 @@ static int mousedev_open(struct inode *inode, struct file *file)
491 client->pos_x = xres / 2; 569 client->pos_x = xres / 2;
492 client->pos_y = yres / 2; 570 client->pos_y = yres / 2;
493 client->mousedev = mousedev; 571 client->mousedev = mousedev;
494 list_add_tail(&client->node, &mousedev->client_list); 572 mousedev_attach_client(mousedev, client);
495 573
496 if (mousedev->minor == MOUSEDEV_MIX) 574 error = mousedev_open_device(mousedev);
497 mixdev_open_devices(); 575 if (error)
498 else if (!mousedev->open++ && mousedev->exist) { 576 goto err_free_client;
499 error = input_open_device(&mousedev->handle);
500 if (error)
501 goto err_free_client;
502 }
503 577
504 file->private_data = client; 578 file->private_data = client;
505 return 0; 579 return 0;
506 580
507 err_free_client: 581 err_free_client:
508 list_del(&client->node); 582 mousedev_detach_client(mousedev, client);
509 kfree(client); 583 kfree(client);
510 err_put_mousedev: 584 err_put_mousedev:
511 put_device(&mousedev->dev); 585 put_device(&mousedev->dev);
@@ -517,41 +591,41 @@ static inline int mousedev_limit_delta(int delta, int limit)
517 return delta > limit ? limit : (delta < -limit ? -limit : delta); 591 return delta > limit ? limit : (delta < -limit ? -limit : delta);
518} 592}
519 593
520static void mousedev_packet(struct mousedev_client *client, signed char *ps2_data) 594static void mousedev_packet(struct mousedev_client *client,
595 signed char *ps2_data)
521{ 596{
522 struct mousedev_motion *p; 597 struct mousedev_motion *p = &client->packets[client->tail];
523 unsigned long flags;
524
525 spin_lock_irqsave(&client->packet_lock, flags);
526 p = &client->packets[client->tail];
527 598
528 ps2_data[0] = 0x08 | ((p->dx < 0) << 4) | ((p->dy < 0) << 5) | (p->buttons & 0x07); 599 ps2_data[0] = 0x08 |
600 ((p->dx < 0) << 4) | ((p->dy < 0) << 5) | (p->buttons & 0x07);
529 ps2_data[1] = mousedev_limit_delta(p->dx, 127); 601 ps2_data[1] = mousedev_limit_delta(p->dx, 127);
530 ps2_data[2] = mousedev_limit_delta(p->dy, 127); 602 ps2_data[2] = mousedev_limit_delta(p->dy, 127);
531 p->dx -= ps2_data[1]; 603 p->dx -= ps2_data[1];
532 p->dy -= ps2_data[2]; 604 p->dy -= ps2_data[2];
533 605
534 switch (client->mode) { 606 switch (client->mode) {
535 case MOUSEDEV_EMUL_EXPS: 607 case MOUSEDEV_EMUL_EXPS:
536 ps2_data[3] = mousedev_limit_delta(p->dz, 7); 608 ps2_data[3] = mousedev_limit_delta(p->dz, 7);
537 p->dz -= ps2_data[3]; 609 p->dz -= ps2_data[3];
538 ps2_data[3] = (ps2_data[3] & 0x0f) | ((p->buttons & 0x18) << 1); 610 ps2_data[3] = (ps2_data[3] & 0x0f) | ((p->buttons & 0x18) << 1);
539 client->bufsiz = 4; 611 client->bufsiz = 4;
540 break; 612 break;
541 613
542 case MOUSEDEV_EMUL_IMPS: 614 case MOUSEDEV_EMUL_IMPS:
543 ps2_data[0] |= ((p->buttons & 0x10) >> 3) | ((p->buttons & 0x08) >> 1); 615 ps2_data[0] |=
544 ps2_data[3] = mousedev_limit_delta(p->dz, 127); 616 ((p->buttons & 0x10) >> 3) | ((p->buttons & 0x08) >> 1);
545 p->dz -= ps2_data[3]; 617 ps2_data[3] = mousedev_limit_delta(p->dz, 127);
546 client->bufsiz = 4; 618 p->dz -= ps2_data[3];
547 break; 619 client->bufsiz = 4;
548 620 break;
549 case MOUSEDEV_EMUL_PS2: 621
550 default: 622 case MOUSEDEV_EMUL_PS2:
551 ps2_data[0] |= ((p->buttons & 0x10) >> 3) | ((p->buttons & 0x08) >> 1); 623 default:
552 p->dz = 0; 624 ps2_data[0] |=
553 client->bufsiz = 3; 625 ((p->buttons & 0x10) >> 3) | ((p->buttons & 0x08) >> 1);
554 break; 626 p->dz = 0;
627 client->bufsiz = 3;
628 break;
555 } 629 }
556 630
557 if (!p->dx && !p->dy && !p->dz) { 631 if (!p->dx && !p->dy && !p->dz) {
@@ -561,12 +635,56 @@ static void mousedev_packet(struct mousedev_client *client, signed char *ps2_dat
561 } else 635 } else
562 client->tail = (client->tail + 1) % PACKET_QUEUE_LEN; 636 client->tail = (client->tail + 1) % PACKET_QUEUE_LEN;
563 } 637 }
564
565 spin_unlock_irqrestore(&client->packet_lock, flags);
566} 638}
567 639
640static void mousedev_generate_response(struct mousedev_client *client,
641 int command)
642{
643 client->ps2[0] = 0xfa; /* ACK */
644
645 switch (command) {
568 646
569static ssize_t mousedev_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos) 647 case 0xeb: /* Poll */
648 mousedev_packet(client, &client->ps2[1]);
649 client->bufsiz++; /* account for leading ACK */
650 break;
651
652 case 0xf2: /* Get ID */
653 switch (client->mode) {
654 case MOUSEDEV_EMUL_PS2:
655 client->ps2[1] = 0;
656 break;
657 case MOUSEDEV_EMUL_IMPS:
658 client->ps2[1] = 3;
659 break;
660 case MOUSEDEV_EMUL_EXPS:
661 client->ps2[1] = 4;
662 break;
663 }
664 client->bufsiz = 2;
665 break;
666
667 case 0xe9: /* Get info */
668 client->ps2[1] = 0x60; client->ps2[2] = 3; client->ps2[3] = 200;
669 client->bufsiz = 4;
670 break;
671
672 case 0xff: /* Reset */
673 client->impsseq = client->imexseq = 0;
674 client->mode = MOUSEDEV_EMUL_PS2;
675 client->ps2[1] = 0xaa; client->ps2[2] = 0x00;
676 client->bufsiz = 3;
677 break;
678
679 default:
680 client->bufsiz = 1;
681 break;
682 }
683 client->buffer = client->bufsiz;
684}
685
686static ssize_t mousedev_write(struct file *file, const char __user *buffer,
687 size_t count, loff_t *ppos)
570{ 688{
571 struct mousedev_client *client = file->private_data; 689 struct mousedev_client *client = file->private_data;
572 unsigned char c; 690 unsigned char c;
@@ -577,6 +695,8 @@ static ssize_t mousedev_write(struct file *file, const char __user *buffer, size
577 if (get_user(c, buffer + i)) 695 if (get_user(c, buffer + i))
578 return -EFAULT; 696 return -EFAULT;
579 697
698 spin_lock_irq(&client->packet_lock);
699
580 if (c == mousedev_imex_seq[client->imexseq]) { 700 if (c == mousedev_imex_seq[client->imexseq]) {
581 if (++client->imexseq == MOUSEDEV_SEQ_LEN) { 701 if (++client->imexseq == MOUSEDEV_SEQ_LEN) {
582 client->imexseq = 0; 702 client->imexseq = 0;
@@ -593,68 +713,39 @@ static ssize_t mousedev_write(struct file *file, const char __user *buffer, size
593 } else 713 } else
594 client->impsseq = 0; 714 client->impsseq = 0;
595 715
596 client->ps2[0] = 0xfa; 716 mousedev_generate_response(client, c);
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 717
631 client->buffer = client->bufsiz; 718 spin_unlock_irq(&client->packet_lock);
632 } 719 }
633 720
634 kill_fasync(&client->fasync, SIGIO, POLL_IN); 721 kill_fasync(&client->fasync, SIGIO, POLL_IN);
635
636 wake_up_interruptible(&client->mousedev->wait); 722 wake_up_interruptible(&client->mousedev->wait);
637 723
638 return count; 724 return count;
639} 725}
640 726
641static ssize_t mousedev_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos) 727static ssize_t mousedev_read(struct file *file, char __user *buffer,
728 size_t count, loff_t *ppos)
642{ 729{
643 struct mousedev_client *client = file->private_data; 730 struct mousedev_client *client = file->private_data;
731 struct mousedev *mousedev = client->mousedev;
732 signed char data[sizeof(client->ps2)];
644 int retval = 0; 733 int retval = 0;
645 734
646 if (!client->ready && !client->buffer && (file->f_flags & O_NONBLOCK)) 735 if (!client->ready && !client->buffer && mousedev->exist &&
736 (file->f_flags & O_NONBLOCK))
647 return -EAGAIN; 737 return -EAGAIN;
648 738
649 retval = wait_event_interruptible(client->mousedev->wait, 739 retval = wait_event_interruptible(mousedev->wait,
650 !client->mousedev->exist || client->ready || client->buffer); 740 !mousedev->exist || client->ready || client->buffer);
651
652 if (retval) 741 if (retval)
653 return retval; 742 return retval;
654 743
655 if (!client->mousedev->exist) 744 if (!mousedev->exist)
656 return -ENODEV; 745 return -ENODEV;
657 746
747 spin_lock_irq(&client->packet_lock);
748
658 if (!client->buffer && client->ready) { 749 if (!client->buffer && client->ready) {
659 mousedev_packet(client, client->ps2); 750 mousedev_packet(client, client->ps2);
660 client->buffer = client->bufsiz; 751 client->buffer = client->bufsiz;
@@ -663,9 +754,12 @@ static ssize_t mousedev_read(struct file *file, char __user *buffer, size_t coun
663 if (count > client->buffer) 754 if (count > client->buffer)
664 count = client->buffer; 755 count = client->buffer;
665 756
757 memcpy(data, client->ps2 + client->bufsiz - client->buffer, count);
666 client->buffer -= count; 758 client->buffer -= count;
667 759
668 if (copy_to_user(buffer, client->ps2 + client->bufsiz - client->buffer - count, count)) 760 spin_unlock_irq(&client->packet_lock);
761
762 if (copy_to_user(buffer, data, count))
669 return -EFAULT; 763 return -EFAULT;
670 764
671 return count; 765 return count;
@@ -692,6 +786,60 @@ static const struct file_operations mousedev_fops = {
692 .fasync = mousedev_fasync, 786 .fasync = mousedev_fasync,
693}; 787};
694 788
789static int mousedev_install_chrdev(struct mousedev *mousedev)
790{
791 mousedev_table[mousedev->minor] = mousedev;
792 return 0;
793}
794
795static void mousedev_remove_chrdev(struct mousedev *mousedev)
796{
797 mutex_lock(&mousedev_table_mutex);
798 mousedev_table[mousedev->minor] = NULL;
799 mutex_unlock(&mousedev_table_mutex);
800}
801
802/*
803 * Mark device non-existent. This disables writes, ioctls and
804 * prevents new users from opening the device. Already posted
805 * blocking reads will stay, however new ones will fail.
806 */
807static void mousedev_mark_dead(struct mousedev *mousedev)
808{
809 mutex_lock(&mousedev->mutex);
810 mousedev->exist = 0;
811 mutex_unlock(&mousedev->mutex);
812}
813
814/*
815 * Wake up users waiting for IO so they can disconnect from
816 * dead device.
817 */
818static void mousedev_hangup(struct mousedev *mousedev)
819{
820 struct mousedev_client *client;
821
822 spin_lock(&mousedev->client_lock);
823 list_for_each_entry(client, &mousedev->client_list, node)
824 kill_fasync(&client->fasync, SIGIO, POLL_HUP);
825 spin_unlock(&mousedev->client_lock);
826
827 wake_up_interruptible(&mousedev->wait);
828}
829
830static void mousedev_cleanup(struct mousedev *mousedev)
831{
832 struct input_handle *handle = &mousedev->handle;
833
834 mousedev_mark_dead(mousedev);
835 mousedev_hangup(mousedev);
836 mousedev_remove_chrdev(mousedev);
837
838 /* mousedev is marked dead so no one else accesses mousedev->open */
839 if (mousedev->open)
840 input_close_device(handle);
841}
842
695static struct mousedev *mousedev_create(struct input_dev *dev, 843static struct mousedev *mousedev_create(struct input_dev *dev,
696 struct input_handler *handler, 844 struct input_handler *handler,
697 int minor) 845 int minor)
@@ -707,6 +855,10 @@ static struct mousedev *mousedev_create(struct input_dev *dev,
707 855
708 INIT_LIST_HEAD(&mousedev->client_list); 856 INIT_LIST_HEAD(&mousedev->client_list);
709 INIT_LIST_HEAD(&mousedev->mixdev_node); 857 INIT_LIST_HEAD(&mousedev->mixdev_node);
858 spin_lock_init(&mousedev->client_lock);
859 mutex_init(&mousedev->mutex);
860 lockdep_set_subclass(&mousedev->mutex,
861 minor == MOUSEDEV_MIX ? MOUSEDEV_MIX : 0);
710 init_waitqueue_head(&mousedev->wait); 862 init_waitqueue_head(&mousedev->wait);
711 863
712 if (minor == MOUSEDEV_MIX) 864 if (minor == MOUSEDEV_MIX)
@@ -731,14 +883,27 @@ static struct mousedev *mousedev_create(struct input_dev *dev,
731 mousedev->dev.release = mousedev_free; 883 mousedev->dev.release = mousedev_free;
732 device_initialize(&mousedev->dev); 884 device_initialize(&mousedev->dev);
733 885
734 mousedev_table[minor] = mousedev; 886 if (minor != MOUSEDEV_MIX) {
887 error = input_register_handle(&mousedev->handle);
888 if (error)
889 goto err_free_mousedev;
890 }
891
892 error = mousedev_install_chrdev(mousedev);
893 if (error)
894 goto err_unregister_handle;
735 895
736 error = device_add(&mousedev->dev); 896 error = device_add(&mousedev->dev);
737 if (error) 897 if (error)
738 goto err_free_mousedev; 898 goto err_cleanup_mousedev;
739 899
740 return mousedev; 900 return mousedev;
741 901
902 err_cleanup_mousedev:
903 mousedev_cleanup(mousedev);
904 err_unregister_handle:
905 if (minor != MOUSEDEV_MIX)
906 input_unregister_handle(&mousedev->handle);
742 err_free_mousedev: 907 err_free_mousedev:
743 put_device(&mousedev->dev); 908 put_device(&mousedev->dev);
744 err_out: 909 err_out:
@@ -747,29 +912,64 @@ static struct mousedev *mousedev_create(struct input_dev *dev,
747 912
748static void mousedev_destroy(struct mousedev *mousedev) 913static void mousedev_destroy(struct mousedev *mousedev)
749{ 914{
750 struct mousedev_client *client;
751
752 device_del(&mousedev->dev); 915 device_del(&mousedev->dev);
753 mousedev->exist = 0; 916 mousedev_cleanup(mousedev);
917 if (mousedev->minor != MOUSEDEV_MIX)
918 input_unregister_handle(&mousedev->handle);
919 put_device(&mousedev->dev);
920}
754 921
755 if (mousedev->open) { 922static int mixdev_add_device(struct mousedev *mousedev)
756 input_close_device(&mousedev->handle); 923{
757 list_for_each_entry(client, &mousedev->client_list, node) 924 int retval;
758 kill_fasync(&client->fasync, SIGIO, POLL_HUP); 925
759 wake_up_interruptible(&mousedev->wait); 926 retval = mutex_lock_interruptible(&mousedev_mix->mutex);
927 if (retval)
928 return retval;
929
930 if (mousedev_mix->open) {
931 retval = mousedev_open_device(mousedev);
932 if (retval)
933 goto out;
934
935 mousedev->mixdev_open = 1;
760 } 936 }
761 937
938 get_device(&mousedev->dev);
939 list_add_tail(&mousedev->mixdev_node, &mousedev_mix_list);
940
941 out:
942 mutex_unlock(&mousedev_mix->mutex);
943 return retval;
944}
945
946static void mixdev_remove_device(struct mousedev *mousedev)
947{
948 mutex_lock(&mousedev_mix->mutex);
949
950 if (mousedev->mixdev_open) {
951 mousedev->mixdev_open = 0;
952 mousedev_close_device(mousedev);
953 }
954
955 list_del_init(&mousedev->mixdev_node);
956 mutex_unlock(&mousedev_mix->mutex);
957
762 put_device(&mousedev->dev); 958 put_device(&mousedev->dev);
763} 959}
764 960
765static int mousedev_connect(struct input_handler *handler, struct input_dev *dev, 961static int mousedev_connect(struct input_handler *handler,
962 struct input_dev *dev,
766 const struct input_device_id *id) 963 const struct input_device_id *id)
767{ 964{
768 struct mousedev *mousedev; 965 struct mousedev *mousedev;
769 int minor; 966 int minor;
770 int error; 967 int error;
771 968
772 for (minor = 0; minor < MOUSEDEV_MINORS && mousedev_table[minor]; minor++); 969 for (minor = 0; minor < MOUSEDEV_MINORS; minor++)
970 if (!mousedev_table[minor])
971 break;
972
773 if (minor == MOUSEDEV_MINORS) { 973 if (minor == MOUSEDEV_MINORS) {
774 printk(KERN_ERR "mousedev: no more free mousedev devices\n"); 974 printk(KERN_ERR "mousedev: no more free mousedev devices\n");
775 return -ENFILE; 975 return -ENFILE;
@@ -779,21 +979,13 @@ static int mousedev_connect(struct input_handler *handler, struct input_dev *dev
779 if (IS_ERR(mousedev)) 979 if (IS_ERR(mousedev))
780 return PTR_ERR(mousedev); 980 return PTR_ERR(mousedev);
781 981
782 error = input_register_handle(&mousedev->handle);
783 if (error)
784 goto err_delete_mousedev;
785
786 error = mixdev_add_device(mousedev); 982 error = mixdev_add_device(mousedev);
787 if (error) 983 if (error) {
788 goto err_unregister_handle; 984 mousedev_destroy(mousedev);
985 return error;
986 }
789 987
790 return 0; 988 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} 989}
798 990
799static void mousedev_disconnect(struct input_handle *handle) 991static void mousedev_disconnect(struct input_handle *handle)
@@ -801,33 +993,42 @@ static void mousedev_disconnect(struct input_handle *handle)
801 struct mousedev *mousedev = handle->private; 993 struct mousedev *mousedev = handle->private;
802 994
803 mixdev_remove_device(mousedev); 995 mixdev_remove_device(mousedev);
804 input_unregister_handle(handle);
805 mousedev_destroy(mousedev); 996 mousedev_destroy(mousedev);
806} 997}
807 998
808static const struct input_device_id mousedev_ids[] = { 999static const struct input_device_id mousedev_ids[] = {
809 { 1000 {
810 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_RELBIT, 1001 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
1002 INPUT_DEVICE_ID_MATCH_KEYBIT |
1003 INPUT_DEVICE_ID_MATCH_RELBIT,
811 .evbit = { BIT(EV_KEY) | BIT(EV_REL) }, 1004 .evbit = { BIT(EV_KEY) | BIT(EV_REL) },
812 .keybit = { [LONG(BTN_LEFT)] = BIT(BTN_LEFT) }, 1005 .keybit = { [LONG(BTN_LEFT)] = BIT(BTN_LEFT) },
813 .relbit = { BIT(REL_X) | BIT(REL_Y) }, 1006 .relbit = { BIT(REL_X) | BIT(REL_Y) },
814 }, /* A mouse like device, at least one button, two relative axes */ 1007 }, /* A mouse like device, at least one button,
1008 two relative axes */
815 { 1009 {
816 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_RELBIT, 1010 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
1011 INPUT_DEVICE_ID_MATCH_RELBIT,
817 .evbit = { BIT(EV_KEY) | BIT(EV_REL) }, 1012 .evbit = { BIT(EV_KEY) | BIT(EV_REL) },
818 .relbit = { BIT(REL_WHEEL) }, 1013 .relbit = { BIT(REL_WHEEL) },
819 }, /* A separate scrollwheel */ 1014 }, /* A separate scrollwheel */
820 { 1015 {
821 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_ABSBIT, 1016 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
1017 INPUT_DEVICE_ID_MATCH_KEYBIT |
1018 INPUT_DEVICE_ID_MATCH_ABSBIT,
822 .evbit = { BIT(EV_KEY) | BIT(EV_ABS) }, 1019 .evbit = { BIT(EV_KEY) | BIT(EV_ABS) },
823 .keybit = { [LONG(BTN_TOUCH)] = BIT(BTN_TOUCH) }, 1020 .keybit = { [LONG(BTN_TOUCH)] = BIT(BTN_TOUCH) },
824 .absbit = { BIT(ABS_X) | BIT(ABS_Y) }, 1021 .absbit = { BIT(ABS_X) | BIT(ABS_Y) },
825 }, /* A tablet like device, at least touch detection, two absolute axes */ 1022 }, /* A tablet like device, at least touch detection,
1023 two absolute axes */
826 { 1024 {
827 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT | INPUT_DEVICE_ID_MATCH_ABSBIT, 1025 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
1026 INPUT_DEVICE_ID_MATCH_KEYBIT |
1027 INPUT_DEVICE_ID_MATCH_ABSBIT,
828 .evbit = { BIT(EV_KEY) | BIT(EV_ABS) }, 1028 .evbit = { BIT(EV_KEY) | BIT(EV_ABS) },
829 .keybit = { [LONG(BTN_TOOL_FINGER)] = BIT(BTN_TOOL_FINGER) }, 1029 .keybit = { [LONG(BTN_TOOL_FINGER)] = BIT(BTN_TOOL_FINGER) },
830 .absbit = { BIT(ABS_X) | BIT(ABS_Y) | BIT(ABS_PRESSURE) | BIT(ABS_TOOL_WIDTH) }, 1030 .absbit = { BIT(ABS_X) | BIT(ABS_Y) | BIT(ABS_PRESSURE) |
1031 BIT(ABS_TOOL_WIDTH) },
831 }, /* A touchpad */ 1032 }, /* A touchpad */
832 1033
833 { }, /* Terminating entry */ 1034 { }, /* Terminating entry */
diff --git a/drivers/input/serio/i8042.c b/drivers/input/serio/i8042.c
index c2eea2767e10..11dafc0ee994 100644
--- a/drivers/input/serio/i8042.c
+++ b/drivers/input/serio/i8042.c
@@ -385,6 +385,8 @@ static int i8042_enable_kbd_port(void)
385 i8042_ctr |= I8042_CTR_KBDINT; 385 i8042_ctr |= I8042_CTR_KBDINT;
386 386
387 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) { 387 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
388 i8042_ctr &= ~I8042_CTR_KBDINT;
389 i8042_ctr |= I8042_CTR_KBDDIS;
388 printk(KERN_ERR "i8042.c: Failed to enable KBD port.\n"); 390 printk(KERN_ERR "i8042.c: Failed to enable KBD port.\n");
389 return -EIO; 391 return -EIO;
390 } 392 }
@@ -402,6 +404,8 @@ static int i8042_enable_aux_port(void)
402 i8042_ctr |= I8042_CTR_AUXINT; 404 i8042_ctr |= I8042_CTR_AUXINT;
403 405
404 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) { 406 if (i8042_command(&i8042_ctr, I8042_CMD_CTL_WCTR)) {
407 i8042_ctr &= ~I8042_CTR_AUXINT;
408 i8042_ctr |= I8042_CTR_AUXDIS;
405 printk(KERN_ERR "i8042.c: Failed to enable AUX port.\n"); 409 printk(KERN_ERR "i8042.c: Failed to enable AUX port.\n");
406 return -EIO; 410 return -EIO;
407 } 411 }
diff --git a/drivers/input/touchscreen/Kconfig b/drivers/input/touchscreen/Kconfig
index f929fcdbae2e..e3e0baa1a158 100644
--- a/drivers/input/touchscreen/Kconfig
+++ b/drivers/input/touchscreen/Kconfig
@@ -126,6 +126,16 @@ config TOUCHSCREEN_HP600
126 To compile this driver as a module, choose M here: the 126 To compile this driver as a module, choose M here: the
127 module will be called hp680_ts_input. 127 module will be called hp680_ts_input.
128 128
129config TOUCHSCREEN_HP7XX
130 tristate "HP Jornada 710/720/728 touchscreen"
131 depends on SA1100_JORNADA720_SSP
132 help
133 Say Y here if you have a HP Jornada 710/720/728 and want
134 to support the built-in touchscreen.
135
136 To compile this driver as a module, choose M here: the
137 module will be called jornada720_ts.
138
129config TOUCHSCREEN_PENMOUNT 139config TOUCHSCREEN_PENMOUNT
130 tristate "Penmount serial touchscreen" 140 tristate "Penmount serial touchscreen"
131 select SERIO 141 select SERIO
@@ -191,6 +201,7 @@ config TOUCHSCREEN_USB_COMPOSITE
191 - Gunze AHL61 201 - Gunze AHL61
192 - DMC TSC-10/25 202 - DMC TSC-10/25
193 - IRTOUCHSYSTEMS/UNITOP 203 - IRTOUCHSYSTEMS/UNITOP
204 - IdealTEK URTC1000
194 205
195 Have a look at <http://linux.chapter7.ch/touchkit/> for 206 Have a look at <http://linux.chapter7.ch/touchkit/> for
196 a usage description and the required user-space stuff. 207 a usage description and the required user-space stuff.
@@ -238,4 +249,14 @@ config TOUCHSCREEN_USB_IRTOUCH
238 bool "IRTOUCHSYSTEMS/UNITOP device support" if EMBEDDED 249 bool "IRTOUCHSYSTEMS/UNITOP device support" if EMBEDDED
239 depends on TOUCHSCREEN_USB_COMPOSITE 250 depends on TOUCHSCREEN_USB_COMPOSITE
240 251
252config TOUCHSCREEN_USB_IDEALTEK
253 default y
254 bool "IdealTEK URTC1000 device support" if EMBEDDED
255 depends on TOUCHSCREEN_USB_COMPOSITE
256
257config TOUCHSCREEN_USB_GENERAL_TOUCH
258 default y
259 bool "GeneralTouch Touchscreen device support" if EMBEDDED
260 depends on TOUCHSCREEN_USB_COMPOSITE
261
241endif 262endif
diff --git a/drivers/input/touchscreen/Makefile b/drivers/input/touchscreen/Makefile
index 5de8933c4993..35d4097df35a 100644
--- a/drivers/input/touchscreen/Makefile
+++ b/drivers/input/touchscreen/Makefile
@@ -13,6 +13,7 @@ obj-$(CONFIG_TOUCHSCREEN_FUJITSU) += fujitsu_ts.o
13obj-$(CONFIG_TOUCHSCREEN_MTOUCH) += mtouch.o 13obj-$(CONFIG_TOUCHSCREEN_MTOUCH) += mtouch.o
14obj-$(CONFIG_TOUCHSCREEN_MK712) += mk712.o 14obj-$(CONFIG_TOUCHSCREEN_MK712) += mk712.o
15obj-$(CONFIG_TOUCHSCREEN_HP600) += hp680_ts_input.o 15obj-$(CONFIG_TOUCHSCREEN_HP600) += hp680_ts_input.o
16obj-$(CONFIG_TOUCHSCREEN_HP7XX) += jornada720_ts.o
16obj-$(CONFIG_TOUCHSCREEN_USB_COMPOSITE) += usbtouchscreen.o 17obj-$(CONFIG_TOUCHSCREEN_USB_COMPOSITE) += usbtouchscreen.o
17obj-$(CONFIG_TOUCHSCREEN_PENMOUNT) += penmount.o 18obj-$(CONFIG_TOUCHSCREEN_PENMOUNT) += penmount.o
18obj-$(CONFIG_TOUCHSCREEN_TOUCHRIGHT) += touchright.o 19obj-$(CONFIG_TOUCHSCREEN_TOUCHRIGHT) += touchright.o
diff --git a/drivers/input/touchscreen/jornada720_ts.c b/drivers/input/touchscreen/jornada720_ts.c
new file mode 100644
index 000000000000..42a1c9a1940e
--- /dev/null
+++ b/drivers/input/touchscreen/jornada720_ts.c
@@ -0,0 +1,182 @@
1/*
2 * drivers/input/touchscreen/jornada720_ts.c
3 *
4 * Copyright (C) 2007 Kristoffer Ericson <Kristoffer.Ericson@gmail.com>
5 *
6 * Copyright (C) 2006 Filip Zyzniewski <filip.zyzniewski@tefnet.pl>
7 * based on HP Jornada 56x touchscreen driver by Alex Lange <chicken@handhelds.org>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 * HP Jornada 710/720/729 Touchscreen Driver
14 */
15
16#include <linux/platform_device.h>
17#include <linux/init.h>
18#include <linux/input.h>
19#include <linux/interrupt.h>
20#include <linux/module.h>
21
22#include <asm/hardware.h>
23#include <asm/arch/jornada720.h>
24
25MODULE_AUTHOR("Kristoffer Ericson <kristoffer.ericson@gmail.com>");
26MODULE_DESCRIPTION("HP Jornada 710/720/728 touchscreen driver");
27MODULE_LICENSE("GPLv2");
28
29struct jornada_ts {
30 struct input_dev *dev;
31 int x_data[4]; /* X sample values */
32 int y_data[4]; /* Y sample values */
33};
34
35static void jornada720_ts_collect_data(struct jornada_ts *jornada_ts)
36{
37
38 /* 3 low word X samples */
39 jornada_ts->x_data[0] = jornada_ssp_byte(TXDUMMY);
40 jornada_ts->x_data[1] = jornada_ssp_byte(TXDUMMY);
41 jornada_ts->x_data[2] = jornada_ssp_byte(TXDUMMY);
42
43 /* 3 low word Y samples */
44 jornada_ts->y_data[0] = jornada_ssp_byte(TXDUMMY);
45 jornada_ts->y_data[1] = jornada_ssp_byte(TXDUMMY);
46 jornada_ts->y_data[2] = jornada_ssp_byte(TXDUMMY);
47
48 /* combined x samples bits */
49 jornada_ts->x_data[3] = jornada_ssp_byte(TXDUMMY);
50
51 /* combined y samples bits */
52 jornada_ts->y_data[3] = jornada_ssp_byte(TXDUMMY);
53}
54
55static int jornada720_ts_average(int coords[4])
56{
57 int coord, high_bits = coords[3];
58
59 coord = coords[0] | ((high_bits & 0x03) << 8);
60 coord += coords[1] | ((high_bits & 0x0c) << 6);
61 coord += coords[2] | ((high_bits & 0x30) << 4);
62
63 return coord / 3;
64}
65
66static irqreturn_t jornada720_ts_interrupt(int irq, void *dev_id)
67{
68 struct platform_device *pdev = dev_id;
69 struct jornada_ts *jornada_ts = platform_get_drvdata(pdev);
70 struct input_dev *input = jornada_ts->dev;
71 int x, y;
72
73 /* If GPIO_GPIO9 is set to high then report pen up */
74 if (GPLR & GPIO_GPIO(9)) {
75 input_report_key(input, BTN_TOUCH, 0);
76 input_sync(input);
77 } else {
78 jornada_ssp_start();
79
80 /* proper reply to request is always TXDUMMY */
81 if (jornada_ssp_inout(GETTOUCHSAMPLES) == TXDUMMY) {
82 jornada720_ts_collect_data(jornada_ts);
83
84 x = jornada720_ts_average(jornada_ts->x_data);
85 y = jornada720_ts_average(jornada_ts->y_data);
86
87 input_report_key(input, BTN_TOUCH, 1);
88 input_report_abs(input, ABS_X, x);
89 input_report_abs(input, ABS_Y, y);
90 input_sync(input);
91 }
92
93 jornada_ssp_end();
94 }
95
96 return IRQ_HANDLED;
97}
98
99static int __devinit jornada720_ts_probe(struct platform_device *pdev)
100{
101 struct jornada_ts *jornada_ts;
102 struct input_dev *input_dev;
103 int error;
104
105 jornada_ts = kzalloc(sizeof(struct jornada_ts), GFP_KERNEL);
106 input_dev = input_allocate_device();
107
108 if (!jornada_ts || !input_dev) {
109 error = -ENOMEM;
110 goto fail1;
111 }
112
113 platform_set_drvdata(pdev, jornada_ts);
114
115 jornada_ts->dev = input_dev;
116
117 input_dev->name = "HP Jornada 7xx Touchscreen";
118 input_dev->phys = "jornadats/input0";
119 input_dev->id.bustype = BUS_HOST;
120 input_dev->dev.parent = &pdev->dev;
121
122 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
123 input_dev->keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH);
124 input_set_abs_params(input_dev, ABS_X, 270, 3900, 0, 0);
125 input_set_abs_params(input_dev, ABS_Y, 180, 3700, 0, 0);
126
127 error = request_irq(IRQ_GPIO9,
128 jornada720_ts_interrupt,
129 IRQF_DISABLED | IRQF_TRIGGER_RISING,
130 "HP7XX Touchscreen driver", pdev);
131 if (error) {
132 printk(KERN_INFO "HP7XX TS : Unable to acquire irq!\n");
133 goto fail1;
134 }
135
136 error = input_register_device(jornada_ts->dev);
137 if (error)
138 goto fail2;
139
140 return 0;
141
142 fail2:
143 free_irq(IRQ_GPIO9, pdev);
144 fail1:
145 platform_set_drvdata(pdev, NULL);
146 input_free_device(input_dev);
147 kfree(jornada_ts);
148 return error;
149}
150
151static int __devexit jornada720_ts_remove(struct platform_device *pdev)
152{
153 struct jornada_ts *jornada_ts = platform_get_drvdata(pdev);
154
155 free_irq(IRQ_GPIO9, pdev);
156 platform_set_drvdata(pdev, NULL);
157 input_unregister_device(jornada_ts->dev);
158 kfree(jornada_ts);
159
160 return 0;
161}
162
163static struct platform_driver jornada720_ts_driver = {
164 .probe = jornada720_ts_probe,
165 .remove = __devexit_p(jornada720_ts_remove),
166 .driver = {
167 .name = "jornada_ts",
168 },
169};
170
171static int __init jornada720_ts_init(void)
172{
173 return platform_driver_register(&jornada720_ts_driver);
174}
175
176static void __exit jornada720_ts_exit(void)
177{
178 platform_driver_unregister(&jornada720_ts_driver);
179}
180
181module_init(jornada720_ts_init);
182module_exit(jornada720_ts_exit);
diff --git a/drivers/input/touchscreen/ucb1400_ts.c b/drivers/input/touchscreen/ucb1400_ts.c
index 36f944019158..86aed64ec0fb 100644
--- a/drivers/input/touchscreen/ucb1400_ts.c
+++ b/drivers/input/touchscreen/ucb1400_ts.c
@@ -130,8 +130,7 @@ static unsigned int ucb1400_adc_read(struct ucb1400 *ucb, u16 adc_channel)
130 if (val & UCB_ADC_DAT_VALID) 130 if (val & UCB_ADC_DAT_VALID)
131 break; 131 break;
132 /* yield to other processes */ 132 /* yield to other processes */
133 set_current_state(TASK_INTERRUPTIBLE); 133 schedule_timeout_uninterruptible(1);
134 schedule_timeout(1);
135 } 134 }
136 135
137 return UCB_ADC_DAT_VALUE(val); 136 return UCB_ADC_DAT_VALUE(val);
diff --git a/drivers/input/touchscreen/usbtouchscreen.c b/drivers/input/touchscreen/usbtouchscreen.c
index 741f6c6f1e50..9fb3d5c30999 100644
--- a/drivers/input/touchscreen/usbtouchscreen.c
+++ b/drivers/input/touchscreen/usbtouchscreen.c
@@ -10,6 +10,7 @@
10 * - Gunze AHL61 10 * - Gunze AHL61
11 * - DMC TSC-10/25 11 * - DMC TSC-10/25
12 * - IRTOUCHSYSTEMS/UNITOP 12 * - IRTOUCHSYSTEMS/UNITOP
13 * - IdealTEK URTC1000
13 * 14 *
14 * Copyright (C) 2004-2006 by Daniel Ritz <daniel.ritz@gmx.ch> 15 * Copyright (C) 2004-2006 by Daniel Ritz <daniel.ritz@gmx.ch>
15 * Copyright (C) by Todd E. Johnson (mtouchusb.c) 16 * Copyright (C) by Todd E. Johnson (mtouchusb.c)
@@ -92,7 +93,7 @@ struct usbtouch_usb {
92}; 93};
93 94
94 95
95#if defined(CONFIG_TOUCHSCREEN_USB_EGALAX) || defined(CONFIG_TOUCHSCREEN_USB_ETURBO) 96#if defined(CONFIG_TOUCHSCREEN_USB_EGALAX) || defined(CONFIG_TOUCHSCREEN_USB_ETURBO) || defined(CONFIG_TOUCHSCREEN_USB_IDEALTEK)
96#define MULTI_PACKET 97#define MULTI_PACKET
97#endif 98#endif
98 99
@@ -112,6 +113,8 @@ enum {
112 DEVTYPE_GUNZE, 113 DEVTYPE_GUNZE,
113 DEVTYPE_DMC_TSC10, 114 DEVTYPE_DMC_TSC10,
114 DEVTYPE_IRTOUCH, 115 DEVTYPE_IRTOUCH,
116 DEVTYPE_IDEALTEK,
117 DEVTYPE_GENERAL_TOUCH,
115}; 118};
116 119
117static struct usb_device_id usbtouch_devices[] = { 120static struct usb_device_id usbtouch_devices[] = {
@@ -157,6 +160,14 @@ static struct usb_device_id usbtouch_devices[] = {
157 {USB_DEVICE(0x6615, 0x0001), .driver_info = DEVTYPE_IRTOUCH}, 160 {USB_DEVICE(0x6615, 0x0001), .driver_info = DEVTYPE_IRTOUCH},
158#endif 161#endif
159 162
163#ifdef CONFIG_TOUCHSCREEN_USB_IDEALTEK
164 {USB_DEVICE(0x1391, 0x1000), .driver_info = DEVTYPE_IDEALTEK},
165#endif
166
167#ifdef CONFIG_TOUCHSCREEN_USB_GENERAL_TOUCH
168 {USB_DEVICE(0x0dfc, 0x0001), .driver_info = DEVTYPE_GENERAL_TOUCH},
169#endif
170
160 {} 171 {}
161}; 172};
162 173
@@ -396,7 +407,8 @@ static int dmc_tsc10_init(struct usbtouch_usb *usbtouch)
396 TSC10_RATE_150, 0, buf, 2, USB_CTRL_SET_TIMEOUT); 407 TSC10_RATE_150, 0, buf, 2, USB_CTRL_SET_TIMEOUT);
397 if (ret < 0) 408 if (ret < 0)
398 return ret; 409 return ret;
399 if (buf[0] != 0x06 || buf[1] != 0x00) 410 if ((buf[0] != 0x06 || buf[1] != 0x00) &&
411 (buf[0] != 0x15 || buf[1] != 0x01))
400 return -ENODEV; 412 return -ENODEV;
401 413
402 /* start sending data */ 414 /* start sending data */
@@ -438,6 +450,57 @@ static int irtouch_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
438 450
439 451
440/***************************************************************************** 452/*****************************************************************************
453 * IdealTEK URTC1000 Part
454 */
455#ifdef CONFIG_TOUCHSCREEN_USB_IDEALTEK
456static int idealtek_get_pkt_len(unsigned char *buf, int len)
457{
458 if (buf[0] & 0x80)
459 return 5;
460 if (buf[0] == 0x01)
461 return len;
462 return 0;
463}
464
465static int idealtek_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
466{
467 switch (pkt[0] & 0x98) {
468 case 0x88:
469 /* touch data in IdealTEK mode */
470 dev->x = (pkt[1] << 5) | (pkt[2] >> 2);
471 dev->y = (pkt[3] << 5) | (pkt[4] >> 2);
472 dev->touch = (pkt[0] & 0x40) ? 1 : 0;
473 return 1;
474
475 case 0x98:
476 /* touch data in MT emulation mode */
477 dev->x = (pkt[2] << 5) | (pkt[1] >> 2);
478 dev->y = (pkt[4] << 5) | (pkt[3] >> 2);
479 dev->touch = (pkt[0] & 0x40) ? 1 : 0;
480 return 1;
481
482 default:
483 return 0;
484 }
485}
486#endif
487
488/*****************************************************************************
489 * General Touch Part
490 */
491#ifdef CONFIG_TOUCHSCREEN_USB_GENERAL_TOUCH
492static int general_touch_read_data(struct usbtouch_usb *dev, unsigned char *pkt)
493{
494 dev->x = ((pkt[2] & 0x0F) << 8) | pkt[1] ;
495 dev->y = ((pkt[4] & 0x0F) << 8) | pkt[3] ;
496 dev->press = pkt[5] & 0xff;
497 dev->touch = pkt[0] & 0x01;
498
499 return 1;
500}
501#endif
502
503/*****************************************************************************
441 * the different device descriptors 504 * the different device descriptors
442 */ 505 */
443static struct usbtouch_device_info usbtouch_dev_info[] = { 506static struct usbtouch_device_info usbtouch_dev_info[] = {
@@ -537,6 +600,32 @@ static struct usbtouch_device_info usbtouch_dev_info[] = {
537 .read_data = irtouch_read_data, 600 .read_data = irtouch_read_data,
538 }, 601 },
539#endif 602#endif
603
604#ifdef CONFIG_TOUCHSCREEN_USB_IDEALTEK
605 [DEVTYPE_IDEALTEK] = {
606 .min_xc = 0x0,
607 .max_xc = 0x0fff,
608 .min_yc = 0x0,
609 .max_yc = 0x0fff,
610 .rept_size = 8,
611 .flags = USBTOUCH_FLG_BUFFER,
612 .process_pkt = usbtouch_process_multi,
613 .get_pkt_len = idealtek_get_pkt_len,
614 .read_data = idealtek_read_data,
615 },
616#endif
617
618#ifdef CONFIG_TOUCHSCREEN_USB_GENERAL_TOUCH
619 [DEVTYPE_GENERAL_TOUCH] = {
620 .min_xc = 0x0,
621 .max_xc = 0x0500,
622 .min_yc = 0x0,
623 .max_yc = 0x0500,
624 .rept_size = 7,
625 .read_data = general_touch_read_data,
626 }
627#endif
628
540}; 629};
541 630
542 631
diff --git a/drivers/input/tsdev.c b/drivers/input/tsdev.c
index d2f882e98e5e..120233493758 100644
--- a/drivers/input/tsdev.c
+++ b/drivers/input/tsdev.c
@@ -112,6 +112,8 @@ struct tsdev {
112 struct input_handle handle; 112 struct input_handle handle;
113 wait_queue_head_t wait; 113 wait_queue_head_t wait;
114 struct list_head client_list; 114 struct list_head client_list;
115 spinlock_t client_lock; /* protects client_list */
116 struct mutex mutex;
115 struct device dev; 117 struct device dev;
116 118
117 int x, y, pressure; 119 int x, y, pressure;
@@ -122,8 +124,9 @@ struct tsdev_client {
122 struct fasync_struct *fasync; 124 struct fasync_struct *fasync;
123 struct list_head node; 125 struct list_head node;
124 struct tsdev *tsdev; 126 struct tsdev *tsdev;
127 struct ts_event buffer[TSDEV_BUFFER_SIZE];
125 int head, tail; 128 int head, tail;
126 struct ts_event event[TSDEV_BUFFER_SIZE]; 129 spinlock_t buffer_lock; /* protects access to buffer, head and tail */
127 int raw; 130 int raw;
128}; 131};
129 132
@@ -137,6 +140,7 @@ struct tsdev_client {
137#define TS_SET_CAL _IOW(IOC_H3600_TS_MAGIC, 11, struct ts_calibration) 140#define TS_SET_CAL _IOW(IOC_H3600_TS_MAGIC, 11, struct ts_calibration)
138 141
139static struct tsdev *tsdev_table[TSDEV_MINORS/2]; 142static struct tsdev *tsdev_table[TSDEV_MINORS/2];
143static DEFINE_MUTEX(tsdev_table_mutex);
140 144
141static int tsdev_fasync(int fd, struct file *file, int on) 145static int tsdev_fasync(int fd, struct file *file, int on)
142{ 146{
@@ -144,9 +148,94 @@ static int tsdev_fasync(int fd, struct file *file, int on)
144 int retval; 148 int retval;
145 149
146 retval = fasync_helper(fd, file, on, &client->fasync); 150 retval = fasync_helper(fd, file, on, &client->fasync);
151
147 return retval < 0 ? retval : 0; 152 return retval < 0 ? retval : 0;
148} 153}
149 154
155static void tsdev_free(struct device *dev)
156{
157 struct tsdev *tsdev = container_of(dev, struct tsdev, dev);
158
159 kfree(tsdev);
160}
161
162static void tsdev_attach_client(struct tsdev *tsdev, struct tsdev_client *client)
163{
164 spin_lock(&tsdev->client_lock);
165 list_add_tail_rcu(&client->node, &tsdev->client_list);
166 spin_unlock(&tsdev->client_lock);
167 synchronize_sched();
168}
169
170static void tsdev_detach_client(struct tsdev *tsdev, struct tsdev_client *client)
171{
172 spin_lock(&tsdev->client_lock);
173 list_del_rcu(&client->node);
174 spin_unlock(&tsdev->client_lock);
175 synchronize_sched();
176}
177
178static int tsdev_open_device(struct tsdev *tsdev)
179{
180 int retval;
181
182 retval = mutex_lock_interruptible(&tsdev->mutex);
183 if (retval)
184 return retval;
185
186 if (!tsdev->exist)
187 retval = -ENODEV;
188 else if (!tsdev->open++) {
189 retval = input_open_device(&tsdev->handle);
190 if (retval)
191 tsdev->open--;
192 }
193
194 mutex_unlock(&tsdev->mutex);
195 return retval;
196}
197
198static void tsdev_close_device(struct tsdev *tsdev)
199{
200 mutex_lock(&tsdev->mutex);
201
202 if (tsdev->exist && !--tsdev->open)
203 input_close_device(&tsdev->handle);
204
205 mutex_unlock(&tsdev->mutex);
206}
207
208/*
209 * Wake up users waiting for IO so they can disconnect from
210 * dead device.
211 */
212static void tsdev_hangup(struct tsdev *tsdev)
213{
214 struct tsdev_client *client;
215
216 spin_lock(&tsdev->client_lock);
217 list_for_each_entry(client, &tsdev->client_list, node)
218 kill_fasync(&client->fasync, SIGIO, POLL_HUP);
219 spin_unlock(&tsdev->client_lock);
220
221 wake_up_interruptible(&tsdev->wait);
222}
223
224static int tsdev_release(struct inode *inode, struct file *file)
225{
226 struct tsdev_client *client = file->private_data;
227 struct tsdev *tsdev = client->tsdev;
228
229 tsdev_fasync(-1, file, 0);
230 tsdev_detach_client(tsdev, client);
231 kfree(client);
232
233 tsdev_close_device(tsdev);
234 put_device(&tsdev->dev);
235
236 return 0;
237}
238
150static int tsdev_open(struct inode *inode, struct file *file) 239static int tsdev_open(struct inode *inode, struct file *file)
151{ 240{
152 int i = iminor(inode) - TSDEV_MINOR_BASE; 241 int i = iminor(inode) - TSDEV_MINOR_BASE;
@@ -161,11 +250,16 @@ static int tsdev_open(struct inode *inode, struct file *file)
161 if (i >= TSDEV_MINORS) 250 if (i >= TSDEV_MINORS)
162 return -ENODEV; 251 return -ENODEV;
163 252
253 error = mutex_lock_interruptible(&tsdev_table_mutex);
254 if (error)
255 return error;
164 tsdev = tsdev_table[i & TSDEV_MINOR_MASK]; 256 tsdev = tsdev_table[i & TSDEV_MINOR_MASK];
165 if (!tsdev || !tsdev->exist) 257 if (tsdev)
166 return -ENODEV; 258 get_device(&tsdev->dev);
259 mutex_unlock(&tsdev_table_mutex);
167 260
168 get_device(&tsdev->dev); 261 if (!tsdev)
262 return -ENODEV;
169 263
170 client = kzalloc(sizeof(struct tsdev_client), GFP_KERNEL); 264 client = kzalloc(sizeof(struct tsdev_client), GFP_KERNEL);
171 if (!client) { 265 if (!client) {
@@ -173,51 +267,42 @@ static int tsdev_open(struct inode *inode, struct file *file)
173 goto err_put_tsdev; 267 goto err_put_tsdev;
174 } 268 }
175 269
270 spin_lock_init(&client->buffer_lock);
176 client->tsdev = tsdev; 271 client->tsdev = tsdev;
177 client->raw = (i >= TSDEV_MINORS / 2) ? 1 : 0; 272 client->raw = i >= TSDEV_MINORS / 2;
178 list_add_tail(&client->node, &tsdev->client_list); 273 tsdev_attach_client(tsdev, client);
179 274
180 if (!tsdev->open++ && tsdev->exist) { 275 error = tsdev_open_device(tsdev);
181 error = input_open_device(&tsdev->handle); 276 if (error)
182 if (error) 277 goto err_free_client;
183 goto err_free_client;
184 }
185 278
186 file->private_data = client; 279 file->private_data = client;
187 return 0; 280 return 0;
188 281
189 err_free_client: 282 err_free_client:
190 list_del(&client->node); 283 tsdev_detach_client(tsdev, client);
191 kfree(client); 284 kfree(client);
192 err_put_tsdev: 285 err_put_tsdev:
193 put_device(&tsdev->dev); 286 put_device(&tsdev->dev);
194 return error; 287 return error;
195} 288}
196 289
197static void tsdev_free(struct device *dev) 290static int tsdev_fetch_next_event(struct tsdev_client *client,
198{ 291 struct ts_event *event)
199 struct tsdev *tsdev = container_of(dev, struct tsdev, dev);
200
201 tsdev_table[tsdev->minor] = NULL;
202 kfree(tsdev);
203}
204
205static int tsdev_release(struct inode *inode, struct file *file)
206{ 292{
207 struct tsdev_client *client = file->private_data; 293 int have_event;
208 struct tsdev *tsdev = client->tsdev;
209 294
210 tsdev_fasync(-1, file, 0); 295 spin_lock_irq(&client->buffer_lock);
211
212 list_del(&client->node);
213 kfree(client);
214 296
215 if (!--tsdev->open && tsdev->exist) 297 have_event = client->head != client->tail;
216 input_close_device(&tsdev->handle); 298 if (have_event) {
299 *event = client->buffer[client->tail++];
300 client->tail &= TSDEV_BUFFER_SIZE - 1;
301 }
217 302
218 put_device(&tsdev->dev); 303 spin_unlock_irq(&client->buffer_lock);
219 304
220 return 0; 305 return have_event;
221} 306}
222 307
223static ssize_t tsdev_read(struct file *file, char __user *buffer, size_t count, 308static ssize_t tsdev_read(struct file *file, char __user *buffer, size_t count,
@@ -225,9 +310,11 @@ static ssize_t tsdev_read(struct file *file, char __user *buffer, size_t count,
225{ 310{
226 struct tsdev_client *client = file->private_data; 311 struct tsdev_client *client = file->private_data;
227 struct tsdev *tsdev = client->tsdev; 312 struct tsdev *tsdev = client->tsdev;
228 int retval = 0; 313 struct ts_event event;
314 int retval;
229 315
230 if (client->head == client->tail && tsdev->exist && (file->f_flags & O_NONBLOCK)) 316 if (client->head == client->tail && tsdev->exist &&
317 (file->f_flags & O_NONBLOCK))
231 return -EAGAIN; 318 return -EAGAIN;
232 319
233 retval = wait_event_interruptible(tsdev->wait, 320 retval = wait_event_interruptible(tsdev->wait,
@@ -238,13 +325,14 @@ static ssize_t tsdev_read(struct file *file, char __user *buffer, size_t count,
238 if (!tsdev->exist) 325 if (!tsdev->exist)
239 return -ENODEV; 326 return -ENODEV;
240 327
241 while (client->head != client->tail && 328 while (retval + sizeof(struct ts_event) <= count &&
242 retval + sizeof (struct ts_event) <= count) { 329 tsdev_fetch_next_event(client, &event)) {
243 if (copy_to_user (buffer + retval, client->event + client->tail, 330
244 sizeof (struct ts_event))) 331 if (copy_to_user(buffer + retval, &event,
332 sizeof(struct ts_event)))
245 return -EFAULT; 333 return -EFAULT;
246 client->tail = (client->tail + 1) & (TSDEV_BUFFER_SIZE - 1); 334
247 retval += sizeof (struct ts_event); 335 retval += sizeof(struct ts_event);
248 } 336 }
249 337
250 return retval; 338 return retval;
@@ -261,14 +349,23 @@ static unsigned int tsdev_poll(struct file *file, poll_table *wait)
261 (tsdev->exist ? 0 : (POLLHUP | POLLERR)); 349 (tsdev->exist ? 0 : (POLLHUP | POLLERR));
262} 350}
263 351
264static int tsdev_ioctl(struct inode *inode, struct file *file, 352static long tsdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
265 unsigned int cmd, unsigned long arg)
266{ 353{
267 struct tsdev_client *client = file->private_data; 354 struct tsdev_client *client = file->private_data;
268 struct tsdev *tsdev = client->tsdev; 355 struct tsdev *tsdev = client->tsdev;
269 int retval = 0; 356 int retval = 0;
270 357
358 retval = mutex_lock_interruptible(&tsdev->mutex);
359 if (retval)
360 return retval;
361
362 if (!tsdev->exist) {
363 retval = -ENODEV;
364 goto out;
365 }
366
271 switch (cmd) { 367 switch (cmd) {
368
272 case TS_GET_CAL: 369 case TS_GET_CAL:
273 if (copy_to_user((void __user *)arg, &tsdev->cal, 370 if (copy_to_user((void __user *)arg, &tsdev->cal,
274 sizeof (struct ts_calibration))) 371 sizeof (struct ts_calibration)))
@@ -277,7 +374,7 @@ static int tsdev_ioctl(struct inode *inode, struct file *file,
277 374
278 case TS_SET_CAL: 375 case TS_SET_CAL:
279 if (copy_from_user(&tsdev->cal, (void __user *)arg, 376 if (copy_from_user(&tsdev->cal, (void __user *)arg,
280 sizeof (struct ts_calibration))) 377 sizeof(struct ts_calibration)))
281 retval = -EFAULT; 378 retval = -EFAULT;
282 break; 379 break;
283 380
@@ -286,29 +383,79 @@ static int tsdev_ioctl(struct inode *inode, struct file *file,
286 break; 383 break;
287 } 384 }
288 385
386 out:
387 mutex_unlock(&tsdev->mutex);
289 return retval; 388 return retval;
290} 389}
291 390
292static const struct file_operations tsdev_fops = { 391static const struct file_operations tsdev_fops = {
293 .owner = THIS_MODULE, 392 .owner = THIS_MODULE,
294 .open = tsdev_open, 393 .open = tsdev_open,
295 .release = tsdev_release, 394 .release = tsdev_release,
296 .read = tsdev_read, 395 .read = tsdev_read,
297 .poll = tsdev_poll, 396 .poll = tsdev_poll,
298 .fasync = tsdev_fasync, 397 .fasync = tsdev_fasync,
299 .ioctl = tsdev_ioctl, 398 .unlocked_ioctl = tsdev_ioctl,
300}; 399};
301 400
401static void tsdev_pass_event(struct tsdev *tsdev, struct tsdev_client *client,
402 int x, int y, int pressure, int millisecs)
403{
404 struct ts_event *event;
405 int tmp;
406
407 /* Interrupts are already disabled, just acquire the lock */
408 spin_lock(&client->buffer_lock);
409
410 event = &client->buffer[client->head++];
411 client->head &= TSDEV_BUFFER_SIZE - 1;
412
413 /* Calibration */
414 if (!client->raw) {
415 x = ((x * tsdev->cal.xscale) >> 8) + tsdev->cal.xtrans;
416 y = ((y * tsdev->cal.yscale) >> 8) + tsdev->cal.ytrans;
417 if (tsdev->cal.xyswap) {
418 tmp = x; x = y; y = tmp;
419 }
420 }
421
422 event->millisecs = millisecs;
423 event->x = x;
424 event->y = y;
425 event->pressure = pressure;
426
427 spin_unlock(&client->buffer_lock);
428
429 kill_fasync(&client->fasync, SIGIO, POLL_IN);
430}
431
432static void tsdev_distribute_event(struct tsdev *tsdev)
433{
434 struct tsdev_client *client;
435 struct timeval time;
436 int millisecs;
437
438 do_gettimeofday(&time);
439 millisecs = time.tv_usec / 1000;
440
441 list_for_each_entry_rcu(client, &tsdev->client_list, node)
442 tsdev_pass_event(tsdev, client,
443 tsdev->x, tsdev->y,
444 tsdev->pressure, millisecs);
445}
446
302static void tsdev_event(struct input_handle *handle, unsigned int type, 447static void tsdev_event(struct input_handle *handle, unsigned int type,
303 unsigned int code, int value) 448 unsigned int code, int value)
304{ 449{
305 struct tsdev *tsdev = handle->private; 450 struct tsdev *tsdev = handle->private;
306 struct tsdev_client *client; 451 struct input_dev *dev = handle->dev;
307 struct timeval time; 452 int wake_up_readers = 0;
308 453
309 switch (type) { 454 switch (type) {
455
310 case EV_ABS: 456 case EV_ABS:
311 switch (code) { 457 switch (code) {
458
312 case ABS_X: 459 case ABS_X:
313 tsdev->x = value; 460 tsdev->x = value;
314 break; 461 break;
@@ -318,9 +465,9 @@ static void tsdev_event(struct input_handle *handle, unsigned int type,
318 break; 465 break;
319 466
320 case ABS_PRESSURE: 467 case ABS_PRESSURE:
321 if (value > handle->dev->absmax[ABS_PRESSURE]) 468 if (value > dev->absmax[ABS_PRESSURE])
322 value = handle->dev->absmax[ABS_PRESSURE]; 469 value = dev->absmax[ABS_PRESSURE];
323 value -= handle->dev->absmin[ABS_PRESSURE]; 470 value -= dev->absmin[ABS_PRESSURE];
324 if (value < 0) 471 if (value < 0)
325 value = 0; 472 value = 0;
326 tsdev->pressure = value; 473 tsdev->pressure = value;
@@ -330,6 +477,7 @@ static void tsdev_event(struct input_handle *handle, unsigned int type,
330 477
331 case EV_REL: 478 case EV_REL:
332 switch (code) { 479 switch (code) {
480
333 case REL_X: 481 case REL_X:
334 tsdev->x += value; 482 tsdev->x += value;
335 if (tsdev->x < 0) 483 if (tsdev->x < 0)
@@ -351,6 +499,7 @@ static void tsdev_event(struct input_handle *handle, unsigned int type,
351 case EV_KEY: 499 case EV_KEY:
352 if (code == BTN_TOUCH || code == BTN_MOUSE) { 500 if (code == BTN_TOUCH || code == BTN_MOUSE) {
353 switch (value) { 501 switch (value) {
502
354 case 0: 503 case 0:
355 tsdev->pressure = 0; 504 tsdev->pressure = 0;
356 break; 505 break;
@@ -362,49 +511,71 @@ static void tsdev_event(struct input_handle *handle, unsigned int type,
362 } 511 }
363 } 512 }
364 break; 513 break;
514
515 case EV_SYN:
516 if (code == SYN_REPORT) {
517 tsdev_distribute_event(tsdev);
518 wake_up_readers = 1;
519 }
520 break;
365 } 521 }
366 522
367 if (type != EV_SYN || code != SYN_REPORT) 523 if (wake_up_readers)
368 return; 524 wake_up_interruptible(&tsdev->wait);
525}
526
527static int tsdev_install_chrdev(struct tsdev *tsdev)
528{
529 tsdev_table[tsdev->minor] = tsdev;
530 return 0;
531}
369 532
370 list_for_each_entry(client, &tsdev->client_list, node) { 533static void tsdev_remove_chrdev(struct tsdev *tsdev)
371 int x, y, tmp; 534{
535 mutex_lock(&tsdev_table_mutex);
536 tsdev_table[tsdev->minor] = NULL;
537 mutex_unlock(&tsdev_table_mutex);
538}
372 539
373 do_gettimeofday(&time); 540/*
374 client->event[client->head].millisecs = time.tv_usec / 1000; 541 * Mark device non-existant. This disables writes, ioctls and
375 client->event[client->head].pressure = tsdev->pressure; 542 * prevents new users from opening the device. Already posted
543 * blocking reads will stay, however new ones will fail.
544 */
545static void tsdev_mark_dead(struct tsdev *tsdev)
546{
547 mutex_lock(&tsdev->mutex);
548 tsdev->exist = 0;
549 mutex_unlock(&tsdev->mutex);
550}
376 551
377 x = tsdev->x; 552static void tsdev_cleanup(struct tsdev *tsdev)
378 y = tsdev->y; 553{
554 struct input_handle *handle = &tsdev->handle;
379 555
380 /* Calibration */ 556 tsdev_mark_dead(tsdev);
381 if (!client->raw) { 557 tsdev_hangup(tsdev);
382 x = ((x * tsdev->cal.xscale) >> 8) + tsdev->cal.xtrans; 558 tsdev_remove_chrdev(tsdev);
383 y = ((y * tsdev->cal.yscale) >> 8) + tsdev->cal.ytrans;
384 if (tsdev->cal.xyswap) {
385 tmp = x; x = y; y = tmp;
386 }
387 }
388 559
389 client->event[client->head].x = x; 560 /* tsdev is marked dead so noone else accesses tsdev->open */
390 client->event[client->head].y = y; 561 if (tsdev->open)
391 client->head = (client->head + 1) & (TSDEV_BUFFER_SIZE - 1); 562 input_close_device(handle);
392 kill_fasync(&client->fasync, SIGIO, POLL_IN);
393 }
394 wake_up_interruptible(&tsdev->wait);
395} 563}
396 564
397static int tsdev_connect(struct input_handler *handler, struct input_dev *dev, 565static int tsdev_connect(struct input_handler *handler, struct input_dev *dev,
398 const struct input_device_id *id) 566 const struct input_device_id *id)
399{ 567{
400 struct tsdev *tsdev; 568 struct tsdev *tsdev;
401 int minor, delta; 569 int delta;
570 int minor;
402 int error; 571 int error;
403 572
404 for (minor = 0; minor < TSDEV_MINORS / 2 && tsdev_table[minor]; minor++); 573 for (minor = 0; minor < TSDEV_MINORS / 2; minor++)
405 if (minor >= TSDEV_MINORS / 2) { 574 if (!tsdev_table[minor])
406 printk(KERN_ERR 575 break;
407 "tsdev: You have way too many touchscreens\n"); 576
577 if (minor == TSDEV_MINORS) {
578 printk(KERN_ERR "tsdev: no more free tsdev devices\n");
408 return -ENFILE; 579 return -ENFILE;
409 } 580 }
410 581
@@ -413,15 +584,18 @@ static int tsdev_connect(struct input_handler *handler, struct input_dev *dev,
413 return -ENOMEM; 584 return -ENOMEM;
414 585
415 INIT_LIST_HEAD(&tsdev->client_list); 586 INIT_LIST_HEAD(&tsdev->client_list);
587 spin_lock_init(&tsdev->client_lock);
588 mutex_init(&tsdev->mutex);
416 init_waitqueue_head(&tsdev->wait); 589 init_waitqueue_head(&tsdev->wait);
417 590
591 snprintf(tsdev->name, sizeof(tsdev->name), "ts%d", minor);
418 tsdev->exist = 1; 592 tsdev->exist = 1;
419 tsdev->minor = minor; 593 tsdev->minor = minor;
594
420 tsdev->handle.dev = dev; 595 tsdev->handle.dev = dev;
421 tsdev->handle.name = tsdev->name; 596 tsdev->handle.name = tsdev->name;
422 tsdev->handle.handler = handler; 597 tsdev->handle.handler = handler;
423 tsdev->handle.private = tsdev; 598 tsdev->handle.private = tsdev;
424 snprintf(tsdev->name, sizeof(tsdev->name), "ts%d", minor);
425 599
426 /* Precompute the rough calibration matrix */ 600 /* Precompute the rough calibration matrix */
427 delta = dev->absmax [ABS_X] - dev->absmin [ABS_X] + 1; 601 delta = dev->absmax [ABS_X] - dev->absmin [ABS_X] + 1;
@@ -436,28 +610,31 @@ static int tsdev_connect(struct input_handler *handler, struct input_dev *dev,
436 tsdev->cal.yscale = (yres << 8) / delta; 610 tsdev->cal.yscale = (yres << 8) / delta;
437 tsdev->cal.ytrans = - ((dev->absmin [ABS_Y] * tsdev->cal.yscale) >> 8); 611 tsdev->cal.ytrans = - ((dev->absmin [ABS_Y] * tsdev->cal.yscale) >> 8);
438 612
439 snprintf(tsdev->dev.bus_id, sizeof(tsdev->dev.bus_id), 613 strlcpy(tsdev->dev.bus_id, tsdev->name, sizeof(tsdev->dev.bus_id));
440 "ts%d", minor); 614 tsdev->dev.devt = MKDEV(INPUT_MAJOR, TSDEV_MINOR_BASE + minor);
441 tsdev->dev.class = &input_class; 615 tsdev->dev.class = &input_class;
442 tsdev->dev.parent = &dev->dev; 616 tsdev->dev.parent = &dev->dev;
443 tsdev->dev.devt = MKDEV(INPUT_MAJOR, TSDEV_MINOR_BASE + minor);
444 tsdev->dev.release = tsdev_free; 617 tsdev->dev.release = tsdev_free;
445 device_initialize(&tsdev->dev); 618 device_initialize(&tsdev->dev);
446 619
447 tsdev_table[minor] = tsdev; 620 error = input_register_handle(&tsdev->handle);
448
449 error = device_add(&tsdev->dev);
450 if (error) 621 if (error)
451 goto err_free_tsdev; 622 goto err_free_tsdev;
452 623
453 error = input_register_handle(&tsdev->handle); 624 error = tsdev_install_chrdev(tsdev);
454 if (error) 625 if (error)
455 goto err_delete_tsdev; 626 goto err_unregister_handle;
627
628 error = device_add(&tsdev->dev);
629 if (error)
630 goto err_cleanup_tsdev;
456 631
457 return 0; 632 return 0;
458 633
459 err_delete_tsdev: 634 err_cleanup_tsdev:
460 device_del(&tsdev->dev); 635 tsdev_cleanup(tsdev);
636 err_unregister_handle:
637 input_unregister_handle(&tsdev->handle);
461 err_free_tsdev: 638 err_free_tsdev:
462 put_device(&tsdev->dev); 639 put_device(&tsdev->dev);
463 return error; 640 return error;
@@ -466,20 +643,10 @@ static int tsdev_connect(struct input_handler *handler, struct input_dev *dev,
466static void tsdev_disconnect(struct input_handle *handle) 643static void tsdev_disconnect(struct input_handle *handle)
467{ 644{
468 struct tsdev *tsdev = handle->private; 645 struct tsdev *tsdev = handle->private;
469 struct tsdev_client *client;
470 646
471 input_unregister_handle(handle);
472 device_del(&tsdev->dev); 647 device_del(&tsdev->dev);
473 648 tsdev_cleanup(tsdev);
474 tsdev->exist = 0; 649 input_unregister_handle(handle);
475
476 if (tsdev->open) {
477 input_close_device(handle);
478 list_for_each_entry(client, &tsdev->client_list, node)
479 kill_fasync(&client->fasync, SIGIO, POLL_HUP);
480 wake_up_interruptible(&tsdev->wait);
481 }
482
483 put_device(&tsdev->dev); 650 put_device(&tsdev->dev);
484} 651}
485 652
@@ -510,13 +677,13 @@ static const struct input_device_id tsdev_ids[] = {
510MODULE_DEVICE_TABLE(input, tsdev_ids); 677MODULE_DEVICE_TABLE(input, tsdev_ids);
511 678
512static struct input_handler tsdev_handler = { 679static struct input_handler tsdev_handler = {
513 .event = tsdev_event, 680 .event = tsdev_event,
514 .connect = tsdev_connect, 681 .connect = tsdev_connect,
515 .disconnect = tsdev_disconnect, 682 .disconnect = tsdev_disconnect,
516 .fops = &tsdev_fops, 683 .fops = &tsdev_fops,
517 .minor = TSDEV_MINOR_BASE, 684 .minor = TSDEV_MINOR_BASE,
518 .name = "tsdev", 685 .name = "tsdev",
519 .id_table = tsdev_ids, 686 .id_table = tsdev_ids,
520}; 687};
521 688
522static int __init tsdev_init(void) 689static int __init tsdev_init(void)