]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/input/joydev.c
Input: matrix_keypad - make matrix keymap size dynamic
[net-next-2.6.git] / drivers / input / joydev.c
CommitLineData
1da177e4
LT
1/*
2 * Joystick device driver for the input driver suite.
3 *
4 * Copyright (c) 1999-2002 Vojtech Pavlik
5 * Copyright (c) 1999 Colin Van Dyke
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
13#include <asm/io.h>
14#include <asm/system.h>
15#include <linux/delay.h>
16#include <linux/errno.h>
17#include <linux/joystick.h>
18#include <linux/input.h>
19#include <linux/kernel.h>
20#include <linux/major.h>
21#include <linux/slab.h>
22#include <linux/mm.h>
23#include <linux/miscdevice.h>
24#include <linux/module.h>
25#include <linux/poll.h>
26#include <linux/init.h>
1da177e4 27#include <linux/device.h>
1da177e4
LT
28
29MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
30MODULE_DESCRIPTION("Joystick device interfaces");
31MODULE_SUPPORTED_DEVICE("input/js");
32MODULE_LICENSE("GPL");
33
34#define JOYDEV_MINOR_BASE 0
35#define JOYDEV_MINORS 16
36#define JOYDEV_BUFFER_SIZE 64
37
1da177e4
LT
38struct joydev {
39 int exist;
40 int open;
41 int minor;
1da177e4
LT
42 struct input_handle handle;
43 wait_queue_head_t wait;
d0ffb9be 44 struct list_head client_list;
b126207c
DT
45 spinlock_t client_lock; /* protects client_list */
46 struct mutex mutex;
9657d75c
DT
47 struct device dev;
48
1da177e4
LT
49 struct js_corr corr[ABS_MAX + 1];
50 struct JS_DATA_SAVE_TYPE glue;
51 int nabs;
52 int nkey;
53 __u16 keymap[KEY_MAX - BTN_MISC + 1];
54 __u16 keypam[KEY_MAX - BTN_MISC + 1];
55 __u8 absmap[ABS_MAX + 1];
56 __u8 abspam[ABS_MAX + 1];
57 __s16 abs[ABS_MAX + 1];
58};
59
d0ffb9be 60struct joydev_client {
1da177e4
LT
61 struct js_event buffer[JOYDEV_BUFFER_SIZE];
62 int head;
63 int tail;
64 int startup;
b126207c 65 spinlock_t buffer_lock; /* protects access to buffer, head and tail */
1da177e4
LT
66 struct fasync_struct *fasync;
67 struct joydev *joydev;
68 struct list_head node;
69};
70
71static struct joydev *joydev_table[JOYDEV_MINORS];
b126207c 72static DEFINE_MUTEX(joydev_table_mutex);
1da177e4
LT
73
74static int joydev_correct(int value, struct js_corr *corr)
75{
76 switch (corr->type) {
b126207c
DT
77
78 case JS_CORR_NONE:
79 break;
80
81 case JS_CORR_BROKEN:
82 value = value > corr->coef[0] ? (value < corr->coef[1] ? 0 :
83 ((corr->coef[3] * (value - corr->coef[1])) >> 14)) :
84 ((corr->coef[2] * (value - corr->coef[0])) >> 14);
85 break;
86
87 default:
88 return 0;
1da177e4
LT
89 }
90
1e0afb28 91 return value < -32767 ? -32767 : (value > 32767 ? 32767 : value);
1da177e4
LT
92}
93
b126207c
DT
94static void joydev_pass_event(struct joydev_client *client,
95 struct js_event *event)
96{
97 struct joydev *joydev = client->joydev;
98
99 /*
100 * IRQs already disabled, just acquire the lock
101 */
102 spin_lock(&client->buffer_lock);
103
104 client->buffer[client->head] = *event;
105
106 if (client->startup == joydev->nabs + joydev->nkey) {
107 client->head++;
108 client->head &= JOYDEV_BUFFER_SIZE - 1;
109 if (client->tail == client->head)
110 client->startup = 0;
111 }
112
113 spin_unlock(&client->buffer_lock);
114
115 kill_fasync(&client->fasync, SIGIO, POLL_IN);
116}
117
118static void joydev_event(struct input_handle *handle,
119 unsigned int type, unsigned int code, int value)
1da177e4
LT
120{
121 struct joydev *joydev = handle->private;
d0ffb9be 122 struct joydev_client *client;
1da177e4
LT
123 struct js_event event;
124
125 switch (type) {
126
b126207c
DT
127 case EV_KEY:
128 if (code < BTN_MISC || value == 2)
129 return;
130 event.type = JS_EVENT_BUTTON;
131 event.number = joydev->keymap[code - BTN_MISC];
132 event.value = value;
133 break;
1da177e4 134
b126207c
DT
135 case EV_ABS:
136 event.type = JS_EVENT_AXIS;
137 event.number = joydev->absmap[code];
138 event.value = joydev_correct(value,
139 &joydev->corr[event.number]);
140 if (event.value == joydev->abs[event.number])
1da177e4 141 return;
b126207c
DT
142 joydev->abs[event.number] = event.value;
143 break;
144
145 default:
146 return;
1da177e4
LT
147 }
148
6f5eacfc 149 event.time = jiffies_to_msecs(jiffies);
1da177e4 150
82ba56c2 151 rcu_read_lock();
b126207c
DT
152 list_for_each_entry_rcu(client, &joydev->client_list, node)
153 joydev_pass_event(client, &event);
82ba56c2 154 rcu_read_unlock();
1da177e4
LT
155
156 wake_up_interruptible(&joydev->wait);
157}
158
159static int joydev_fasync(int fd, struct file *file, int on)
160{
d0ffb9be 161 struct joydev_client *client = file->private_data;
1e0afb28 162
60aa4924 163 return fasync_helper(fd, file, on, &client->fasync);
1da177e4
LT
164}
165
9657d75c 166static void joydev_free(struct device *dev)
1da177e4 167{
9657d75c
DT
168 struct joydev *joydev = container_of(dev, struct joydev, dev);
169
a7097ff8 170 input_put_device(joydev->handle.dev);
1da177e4
LT
171 kfree(joydev);
172}
173
b126207c
DT
174static void joydev_attach_client(struct joydev *joydev,
175 struct joydev_client *client)
176{
177 spin_lock(&joydev->client_lock);
178 list_add_tail_rcu(&client->node, &joydev->client_list);
179 spin_unlock(&joydev->client_lock);
82ba56c2 180 synchronize_rcu();
b126207c
DT
181}
182
183static void joydev_detach_client(struct joydev *joydev,
184 struct joydev_client *client)
185{
186 spin_lock(&joydev->client_lock);
187 list_del_rcu(&client->node);
188 spin_unlock(&joydev->client_lock);
82ba56c2 189 synchronize_rcu();
b126207c
DT
190}
191
192static int joydev_open_device(struct joydev *joydev)
193{
194 int retval;
195
196 retval = mutex_lock_interruptible(&joydev->mutex);
197 if (retval)
198 return retval;
199
200 if (!joydev->exist)
201 retval = -ENODEV;
06445014 202 else if (!joydev->open++) {
b126207c 203 retval = input_open_device(&joydev->handle);
06445014
ON
204 if (retval)
205 joydev->open--;
206 }
b126207c
DT
207
208 mutex_unlock(&joydev->mutex);
209 return retval;
210}
211
212static void joydev_close_device(struct joydev *joydev)
213{
214 mutex_lock(&joydev->mutex);
215
216 if (joydev->exist && !--joydev->open)
217 input_close_device(&joydev->handle);
218
219 mutex_unlock(&joydev->mutex);
220}
221
222/*
223 * Wake up users waiting for IO so they can disconnect from
224 * dead device.
225 */
226static void joydev_hangup(struct joydev *joydev)
227{
228 struct joydev_client *client;
229
230 spin_lock(&joydev->client_lock);
231 list_for_each_entry(client, &joydev->client_list, node)
232 kill_fasync(&client->fasync, SIGIO, POLL_HUP);
233 spin_unlock(&joydev->client_lock);
234
235 wake_up_interruptible(&joydev->wait);
236}
237
d0ffb9be 238static int joydev_release(struct inode *inode, struct file *file)
1da177e4 239{
d0ffb9be
DT
240 struct joydev_client *client = file->private_data;
241 struct joydev *joydev = client->joydev;
1da177e4 242
b126207c 243 joydev_detach_client(joydev, client);
d0ffb9be 244 kfree(client);
1da177e4 245
b126207c 246 joydev_close_device(joydev);
9657d75c 247 put_device(&joydev->dev);
1da177e4 248
1da177e4
LT
249 return 0;
250}
251
252static int joydev_open(struct inode *inode, struct file *file)
253{
d0ffb9be
DT
254 struct joydev_client *client;
255 struct joydev *joydev;
1da177e4 256 int i = iminor(inode) - JOYDEV_MINOR_BASE;
d542ed82 257 int error;
1da177e4 258
d0ffb9be
DT
259 if (i >= JOYDEV_MINORS)
260 return -ENODEV;
1da177e4 261
b126207c
DT
262 error = mutex_lock_interruptible(&joydev_table_mutex);
263 if (error)
264 return error;
d0ffb9be 265 joydev = joydev_table[i];
b126207c
DT
266 if (joydev)
267 get_device(&joydev->dev);
268 mutex_unlock(&joydev_table_mutex);
1da177e4 269
b126207c
DT
270 if (!joydev)
271 return -ENODEV;
9657d75c 272
d0ffb9be 273 client = kzalloc(sizeof(struct joydev_client), GFP_KERNEL);
9657d75c
DT
274 if (!client) {
275 error = -ENOMEM;
276 goto err_put_joydev;
277 }
1da177e4 278
b126207c 279 spin_lock_init(&client->buffer_lock);
d0ffb9be 280 client->joydev = joydev;
b126207c 281 joydev_attach_client(joydev, client);
1da177e4 282
b126207c
DT
283 error = joydev_open_device(joydev);
284 if (error)
285 goto err_free_client;
1da177e4 286
d0ffb9be 287 file->private_data = client;
1da177e4 288 return 0;
9657d75c
DT
289
290 err_free_client:
b126207c 291 joydev_detach_client(joydev, client);
9657d75c
DT
292 kfree(client);
293 err_put_joydev:
294 put_device(&joydev->dev);
295 return error;
1da177e4
LT
296}
297
b126207c
DT
298static int joydev_generate_startup_event(struct joydev_client *client,
299 struct input_dev *input,
300 struct js_event *event)
1da177e4 301{
b126207c
DT
302 struct joydev *joydev = client->joydev;
303 int have_event;
304
305 spin_lock_irq(&client->buffer_lock);
306
307 have_event = client->startup < joydev->nabs + joydev->nkey;
308
309 if (have_event) {
310
311 event->time = jiffies_to_msecs(jiffies);
312 if (client->startup < joydev->nkey) {
313 event->type = JS_EVENT_BUTTON | JS_EVENT_INIT;
314 event->number = client->startup;
315 event->value = !!test_bit(joydev->keypam[event->number],
316 input->key);
317 } else {
318 event->type = JS_EVENT_AXIS | JS_EVENT_INIT;
319 event->number = client->startup - joydev->nkey;
320 event->value = joydev->abs[event->number];
321 }
322 client->startup++;
323 }
324
325 spin_unlock_irq(&client->buffer_lock);
326
327 return have_event;
328}
329
330static int joydev_fetch_next_event(struct joydev_client *client,
331 struct js_event *event)
332{
333 int have_event;
334
335 spin_lock_irq(&client->buffer_lock);
336
337 have_event = client->head != client->tail;
338 if (have_event) {
339 *event = client->buffer[client->tail++];
340 client->tail &= JOYDEV_BUFFER_SIZE - 1;
341 }
342
343 spin_unlock_irq(&client->buffer_lock);
344
345 return have_event;
346}
347
348/*
349 * Old joystick interface
350 */
351static ssize_t joydev_0x_read(struct joydev_client *client,
352 struct input_dev *input,
353 char __user *buf)
354{
355 struct joydev *joydev = client->joydev;
356 struct JS_DATA_TYPE data;
357 int i;
358
359 spin_lock_irq(&input->event_lock);
360
361 /*
362 * Get device state
363 */
364 for (data.buttons = i = 0; i < 32 && i < joydev->nkey; i++)
365 data.buttons |=
366 test_bit(joydev->keypam[i], input->key) ? (1 << i) : 0;
367 data.x = (joydev->abs[0] / 256 + 128) >> joydev->glue.JS_CORR.x;
368 data.y = (joydev->abs[1] / 256 + 128) >> joydev->glue.JS_CORR.y;
369
370 /*
371 * Reset reader's event queue
372 */
373 spin_lock(&client->buffer_lock);
374 client->startup = 0;
375 client->tail = client->head;
376 spin_unlock(&client->buffer_lock);
377
378 spin_unlock_irq(&input->event_lock);
379
380 if (copy_to_user(buf, &data, sizeof(struct JS_DATA_TYPE)))
381 return -EFAULT;
382
383 return sizeof(struct JS_DATA_TYPE);
1da177e4
LT
384}
385
b126207c
DT
386static inline int joydev_data_pending(struct joydev_client *client)
387{
388 struct joydev *joydev = client->joydev;
389
390 return client->startup < joydev->nabs + joydev->nkey ||
391 client->head != client->tail;
392}
393
394static ssize_t joydev_read(struct file *file, char __user *buf,
395 size_t count, loff_t *ppos)
1da177e4 396{
d0ffb9be
DT
397 struct joydev_client *client = file->private_data;
398 struct joydev *joydev = client->joydev;
1da177e4 399 struct input_dev *input = joydev->handle.dev;
b126207c
DT
400 struct js_event event;
401 int retval;
1da177e4 402
d0ffb9be 403 if (!joydev->exist)
1da177e4
LT
404 return -ENODEV;
405
406 if (count < sizeof(struct js_event))
407 return -EINVAL;
408
b126207c
DT
409 if (count == sizeof(struct JS_DATA_TYPE))
410 return joydev_0x_read(client, input, buf);
1da177e4 411
b126207c 412 if (!joydev_data_pending(client) && (file->f_flags & O_NONBLOCK))
1e0afb28 413 return -EAGAIN;
1da177e4 414
d0ffb9be 415 retval = wait_event_interruptible(joydev->wait,
b126207c 416 !joydev->exist || joydev_data_pending(client));
1da177e4
LT
417 if (retval)
418 return retval;
419
d0ffb9be 420 if (!joydev->exist)
1da177e4
LT
421 return -ENODEV;
422
b126207c
DT
423 while (retval + sizeof(struct js_event) <= count &&
424 joydev_generate_startup_event(client, input, &event)) {
1da177e4
LT
425
426 if (copy_to_user(buf + retval, &event, sizeof(struct js_event)))
427 return -EFAULT;
428
1da177e4
LT
429 retval += sizeof(struct js_event);
430 }
431
b126207c
DT
432 while (retval + sizeof(struct js_event) <= count &&
433 joydev_fetch_next_event(client, &event)) {
1da177e4 434
b126207c 435 if (copy_to_user(buf + retval, &event, sizeof(struct js_event)))
1da177e4
LT
436 return -EFAULT;
437
1da177e4
LT
438 retval += sizeof(struct js_event);
439 }
440
441 return retval;
442}
443
444/* No kernel lock - fine */
445static unsigned int joydev_poll(struct file *file, poll_table *wait)
446{
d0ffb9be
DT
447 struct joydev_client *client = file->private_data;
448 struct joydev *joydev = client->joydev;
1e0afb28 449
d0ffb9be 450 poll_wait(file, &joydev->wait, wait);
b126207c
DT
451 return (joydev_data_pending(client) ? (POLLIN | POLLRDNORM) : 0) |
452 (joydev->exist ? 0 : (POLLHUP | POLLERR));
1da177e4
LT
453}
454
b126207c
DT
455static int joydev_ioctl_common(struct joydev *joydev,
456 unsigned int cmd, void __user *argp)
1da177e4 457{
1da177e4 458 struct input_dev *dev = joydev->handle.dev;
1da177e4
LT
459 int i, j;
460
1da177e4
LT
461 switch (cmd) {
462
b126207c
DT
463 case JS_SET_CAL:
464 return copy_from_user(&joydev->glue.JS_CORR, argp,
024ac44c 465 sizeof(joydev->glue.JS_CORR)) ? -EFAULT : 0;
1e0afb28 466
b126207c
DT
467 case JS_GET_CAL:
468 return copy_to_user(argp, &joydev->glue.JS_CORR,
024ac44c 469 sizeof(joydev->glue.JS_CORR)) ? -EFAULT : 0;
1e0afb28 470
b126207c
DT
471 case JS_SET_TIMEOUT:
472 return get_user(joydev->glue.JS_TIMEOUT, (s32 __user *) argp);
1e0afb28 473
b126207c
DT
474 case JS_GET_TIMEOUT:
475 return put_user(joydev->glue.JS_TIMEOUT, (s32 __user *) argp);
1da177e4 476
b126207c
DT
477 case JSIOCGVERSION:
478 return put_user(JS_VERSION, (__u32 __user *) argp);
1e0afb28 479
b126207c
DT
480 case JSIOCGAXES:
481 return put_user(joydev->nabs, (__u8 __user *) argp);
1e0afb28 482
b126207c
DT
483 case JSIOCGBUTTONS:
484 return put_user(joydev->nkey, (__u8 __user *) argp);
1e0afb28 485
b126207c
DT
486 case JSIOCSCORR:
487 if (copy_from_user(joydev->corr, argp,
488 sizeof(joydev->corr[0]) * joydev->nabs))
489 return -EFAULT;
1e0afb28 490
b126207c
DT
491 for (i = 0; i < joydev->nabs; i++) {
492 j = joydev->abspam[i];
493 joydev->abs[i] = joydev_correct(dev->abs[j],
494 &joydev->corr[i]);
495 }
496 return 0;
1e0afb28 497
b126207c
DT
498 case JSIOCGCORR:
499 return copy_to_user(argp, joydev->corr,
500 sizeof(joydev->corr[0]) * joydev->nabs) ? -EFAULT : 0;
501
502 case JSIOCSAXMAP:
503 if (copy_from_user(joydev->abspam, argp,
504 sizeof(__u8) * (ABS_MAX + 1)))
505 return -EFAULT;
506
507 for (i = 0; i < joydev->nabs; i++) {
508 if (joydev->abspam[i] > ABS_MAX)
509 return -EINVAL;
510 joydev->absmap[joydev->abspam[i]] = i;
511 }
512 return 0;
513
514 case JSIOCGAXMAP:
515 return copy_to_user(argp, joydev->abspam,
516 sizeof(__u8) * (ABS_MAX + 1)) ? -EFAULT : 0;
517
518 case JSIOCSBTNMAP:
519 if (copy_from_user(joydev->keypam, argp,
520 sizeof(__u16) * (KEY_MAX - BTN_MISC + 1)))
521 return -EFAULT;
522
523 for (i = 0; i < joydev->nkey; i++) {
524 if (joydev->keypam[i] > KEY_MAX ||
525 joydev->keypam[i] < BTN_MISC)
526 return -EINVAL;
527 joydev->keymap[joydev->keypam[i] - BTN_MISC] = i;
528 }
529
530 return 0;
531
532 case JSIOCGBTNMAP:
533 return copy_to_user(argp, joydev->keypam,
534 sizeof(__u16) * (KEY_MAX - BTN_MISC + 1)) ? -EFAULT : 0;
535
536 default:
537 if ((cmd & ~IOCSIZE_MASK) == JSIOCGNAME(0)) {
538 int len;
f9366014 539 const char *name = dev->name;
3d5cb60e
TLSC
540
541 if (!name)
b126207c 542 return 0;
3d5cb60e 543 len = strlen(name) + 1;
b126207c
DT
544 if (len > _IOC_SIZE(cmd))
545 len = _IOC_SIZE(cmd);
3d5cb60e 546 if (copy_to_user(argp, name, len))
1da177e4 547 return -EFAULT;
b126207c
DT
548 return len;
549 }
1da177e4
LT
550 }
551 return -EINVAL;
552}
553
024ac44c 554#ifdef CONFIG_COMPAT
b126207c
DT
555static long joydev_compat_ioctl(struct file *file,
556 unsigned int cmd, unsigned long arg)
024ac44c 557{
d0ffb9be
DT
558 struct joydev_client *client = file->private_data;
559 struct joydev *joydev = client->joydev;
024ac44c
JF
560 void __user *argp = (void __user *)arg;
561 s32 tmp32;
562 struct JS_DATA_SAVE_TYPE_32 ds32;
b126207c 563 int retval;
024ac44c 564
b126207c
DT
565 retval = mutex_lock_interruptible(&joydev->mutex);
566 if (retval)
567 return retval;
568
569 if (!joydev->exist) {
570 retval = -ENODEV;
571 goto out;
572 }
573
574 switch (cmd) {
1e0afb28 575
024ac44c 576 case JS_SET_TIMELIMIT:
b126207c
DT
577 retval = get_user(tmp32, (s32 __user *) arg);
578 if (retval == 0)
024ac44c
JF
579 joydev->glue.JS_TIMELIMIT = tmp32;
580 break;
b126207c 581
024ac44c
JF
582 case JS_GET_TIMELIMIT:
583 tmp32 = joydev->glue.JS_TIMELIMIT;
b126207c 584 retval = put_user(tmp32, (s32 __user *) arg);
024ac44c
JF
585 break;
586
587 case JS_SET_ALL:
b126207c
DT
588 retval = copy_from_user(&ds32, argp,
589 sizeof(ds32)) ? -EFAULT : 0;
590 if (retval == 0) {
024ac44c
JF
591 joydev->glue.JS_TIMEOUT = ds32.JS_TIMEOUT;
592 joydev->glue.BUSY = ds32.BUSY;
593 joydev->glue.JS_EXPIRETIME = ds32.JS_EXPIRETIME;
594 joydev->glue.JS_TIMELIMIT = ds32.JS_TIMELIMIT;
595 joydev->glue.JS_SAVE = ds32.JS_SAVE;
596 joydev->glue.JS_CORR = ds32.JS_CORR;
597 }
598 break;
599
600 case JS_GET_ALL:
601 ds32.JS_TIMEOUT = joydev->glue.JS_TIMEOUT;
602 ds32.BUSY = joydev->glue.BUSY;
603 ds32.JS_EXPIRETIME = joydev->glue.JS_EXPIRETIME;
604 ds32.JS_TIMELIMIT = joydev->glue.JS_TIMELIMIT;
605 ds32.JS_SAVE = joydev->glue.JS_SAVE;
606 ds32.JS_CORR = joydev->glue.JS_CORR;
607
b126207c 608 retval = copy_to_user(argp, &ds32, sizeof(ds32)) ? -EFAULT : 0;
024ac44c
JF
609 break;
610
611 default:
b126207c
DT
612 retval = joydev_ioctl_common(joydev, cmd, argp);
613 break;
024ac44c 614 }
b126207c
DT
615
616 out:
617 mutex_unlock(&joydev->mutex);
618 return retval;
024ac44c
JF
619}
620#endif /* CONFIG_COMPAT */
621
b126207c
DT
622static long joydev_ioctl(struct file *file,
623 unsigned int cmd, unsigned long arg)
024ac44c 624{
d0ffb9be
DT
625 struct joydev_client *client = file->private_data;
626 struct joydev *joydev = client->joydev;
024ac44c 627 void __user *argp = (void __user *)arg;
b126207c 628 int retval;
024ac44c 629
b126207c
DT
630 retval = mutex_lock_interruptible(&joydev->mutex);
631 if (retval)
632 return retval;
024ac44c 633
b126207c
DT
634 if (!joydev->exist) {
635 retval = -ENODEV;
636 goto out;
024ac44c 637 }
b126207c
DT
638
639 switch (cmd) {
640
641 case JS_SET_TIMELIMIT:
642 retval = get_user(joydev->glue.JS_TIMELIMIT,
643 (long __user *) arg);
644 break;
645
646 case JS_GET_TIMELIMIT:
647 retval = put_user(joydev->glue.JS_TIMELIMIT,
648 (long __user *) arg);
649 break;
650
651 case JS_SET_ALL:
652 retval = copy_from_user(&joydev->glue, argp,
653 sizeof(joydev->glue)) ? -EFAULT: 0;
654 break;
655
656 case JS_GET_ALL:
657 retval = copy_to_user(argp, &joydev->glue,
658 sizeof(joydev->glue)) ? -EFAULT : 0;
659 break;
660
661 default:
662 retval = joydev_ioctl_common(joydev, cmd, argp);
663 break;
664 }
665 out:
666 mutex_unlock(&joydev->mutex);
667 return retval;
024ac44c
JF
668}
669
66e66118 670static const struct file_operations joydev_fops = {
b126207c
DT
671 .owner = THIS_MODULE,
672 .read = joydev_read,
673 .poll = joydev_poll,
674 .open = joydev_open,
675 .release = joydev_release,
676 .unlocked_ioctl = joydev_ioctl,
024ac44c 677#ifdef CONFIG_COMPAT
b126207c 678 .compat_ioctl = joydev_compat_ioctl,
024ac44c 679#endif
b126207c 680 .fasync = joydev_fasync,
1da177e4
LT
681};
682
b126207c
DT
683static int joydev_install_chrdev(struct joydev *joydev)
684{
685 joydev_table[joydev->minor] = joydev;
686 return 0;
687}
688
689static void joydev_remove_chrdev(struct joydev *joydev)
690{
691 mutex_lock(&joydev_table_mutex);
692 joydev_table[joydev->minor] = NULL;
693 mutex_unlock(&joydev_table_mutex);
694}
695
696/*
697 * Mark device non-existant. This disables writes, ioctls and
698 * prevents new users from opening the device. Already posted
699 * blocking reads will stay, however new ones will fail.
700 */
701static void joydev_mark_dead(struct joydev *joydev)
702{
703 mutex_lock(&joydev->mutex);
704 joydev->exist = 0;
705 mutex_unlock(&joydev->mutex);
706}
707
708static void joydev_cleanup(struct joydev *joydev)
709{
710 struct input_handle *handle = &joydev->handle;
711
712 joydev_mark_dead(joydev);
713 joydev_hangup(joydev);
714 joydev_remove_chrdev(joydev);
715
716 /* joydev is marked dead so noone else accesses joydev->open */
717 if (joydev->open)
718 input_close_device(handle);
719}
720
5b2a0826
DT
721static int joydev_connect(struct input_handler *handler, struct input_dev *dev,
722 const struct input_device_id *id)
1da177e4
LT
723{
724 struct joydev *joydev;
725 int i, j, t, minor;
5b2a0826 726 int error;
1da177e4 727
b126207c
DT
728 for (minor = 0; minor < JOYDEV_MINORS; minor++)
729 if (!joydev_table[minor])
730 break;
731
1da177e4
LT
732 if (minor == JOYDEV_MINORS) {
733 printk(KERN_ERR "joydev: no more free joydev devices\n");
5b2a0826 734 return -ENFILE;
1da177e4
LT
735 }
736
5b2a0826
DT
737 joydev = kzalloc(sizeof(struct joydev), GFP_KERNEL);
738 if (!joydev)
739 return -ENOMEM;
1da177e4 740
d0ffb9be 741 INIT_LIST_HEAD(&joydev->client_list);
b126207c
DT
742 spin_lock_init(&joydev->client_lock);
743 mutex_init(&joydev->mutex);
1da177e4
LT
744 init_waitqueue_head(&joydev->wait);
745
3d5cb60e 746 dev_set_name(&joydev->dev, "js%d", minor);
b126207c 747 joydev->exist = 1;
1da177e4 748 joydev->minor = minor;
b126207c 749
1da177e4 750 joydev->exist = 1;
a7097ff8 751 joydev->handle.dev = input_get_device(dev);
3d5cb60e 752 joydev->handle.name = dev_name(&joydev->dev);
1da177e4
LT
753 joydev->handle.handler = handler;
754 joydev->handle.private = joydev;
1da177e4
LT
755
756 for (i = 0; i < ABS_MAX + 1; i++)
757 if (test_bit(i, dev->absbit)) {
758 joydev->absmap[i] = joydev->nabs;
759 joydev->abspam[joydev->nabs] = i;
760 joydev->nabs++;
761 }
762
763 for (i = BTN_JOYSTICK - BTN_MISC; i < KEY_MAX - BTN_MISC + 1; i++)
764 if (test_bit(i + BTN_MISC, dev->keybit)) {
765 joydev->keymap[i] = joydev->nkey;
766 joydev->keypam[joydev->nkey] = i + BTN_MISC;
767 joydev->nkey++;
768 }
769
f24949e8 770 for (i = 0; i < BTN_JOYSTICK - BTN_MISC; i++)
1da177e4
LT
771 if (test_bit(i + BTN_MISC, dev->keybit)) {
772 joydev->keymap[i] = joydev->nkey;
773 joydev->keypam[joydev->nkey] = i + BTN_MISC;
774 joydev->nkey++;
775 }
776
777 for (i = 0; i < joydev->nabs; i++) {
778 j = joydev->abspam[i];
779 if (dev->absmax[j] == dev->absmin[j]) {
780 joydev->corr[i].type = JS_CORR_NONE;
781 joydev->abs[i] = dev->abs[j];
782 continue;
783 }
784 joydev->corr[i].type = JS_CORR_BROKEN;
785 joydev->corr[i].prec = dev->absfuzz[j];
b126207c
DT
786 joydev->corr[i].coef[0] =
787 (dev->absmax[j] + dev->absmin[j]) / 2 - dev->absflat[j];
788 joydev->corr[i].coef[1] =
789 (dev->absmax[j] + dev->absmin[j]) / 2 + dev->absflat[j];
790
791 t = (dev->absmax[j] - dev->absmin[j]) / 2 - 2 * dev->absflat[j];
792 if (t) {
793 joydev->corr[i].coef[2] = (1 << 29) / t;
794 joydev->corr[i].coef[3] = (1 << 29) / t;
795
796 joydev->abs[i] = joydev_correct(dev->abs[j],
797 joydev->corr + i);
798 }
1da177e4
LT
799 }
800
b126207c 801 joydev->dev.devt = MKDEV(INPUT_MAJOR, JOYDEV_MINOR_BASE + minor);
9657d75c
DT
802 joydev->dev.class = &input_class;
803 joydev->dev.parent = &dev->dev;
9657d75c
DT
804 joydev->dev.release = joydev_free;
805 device_initialize(&joydev->dev);
5b2a0826 806
b126207c 807 error = input_register_handle(&joydev->handle);
5b2a0826 808 if (error)
9657d75c 809 goto err_free_joydev;
5b2a0826 810
b126207c 811 error = joydev_install_chrdev(joydev);
5b2a0826 812 if (error)
b126207c
DT
813 goto err_unregister_handle;
814
815 error = device_add(&joydev->dev);
816 if (error)
817 goto err_cleanup_joydev;
5b2a0826
DT
818
819 return 0;
1da177e4 820
b126207c
DT
821 err_cleanup_joydev:
822 joydev_cleanup(joydev);
823 err_unregister_handle:
824 input_unregister_handle(&joydev->handle);
5b2a0826 825 err_free_joydev:
9657d75c 826 put_device(&joydev->dev);
5b2a0826 827 return error;
1da177e4
LT
828}
829
830static void joydev_disconnect(struct input_handle *handle)
831{
832 struct joydev *joydev = handle->private;
1da177e4 833
9657d75c 834 device_del(&joydev->dev);
b126207c
DT
835 joydev_cleanup(joydev);
836 input_unregister_handle(handle);
9657d75c 837 put_device(&joydev->dev);
1da177e4
LT
838}
839
66e66118 840static const struct input_device_id joydev_blacklist[] = {
1da177e4 841 {
b126207c
DT
842 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
843 INPUT_DEVICE_ID_MATCH_KEYBIT,
7b19ada2
JS
844 .evbit = { BIT_MASK(EV_KEY) },
845 .keybit = { [BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH) },
d07a9cba
TC
846 }, /* Avoid itouchpads and touchscreens */
847 {
848 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
849 INPUT_DEVICE_ID_MATCH_KEYBIT,
850 .evbit = { BIT_MASK(EV_KEY) },
851 .keybit = { [BIT_WORD(BTN_DIGI)] = BIT_MASK(BTN_DIGI) },
852 }, /* Avoid tablets, digitisers and similar devices */
1e0afb28 853 { } /* Terminating entry */
1da177e4
LT
854};
855
66e66118 856static const struct input_device_id joydev_ids[] = {
1da177e4 857 {
b126207c
DT
858 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
859 INPUT_DEVICE_ID_MATCH_ABSBIT,
7b19ada2
JS
860 .evbit = { BIT_MASK(EV_ABS) },
861 .absbit = { BIT_MASK(ABS_X) },
1da177e4
LT
862 },
863 {
b126207c
DT
864 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
865 INPUT_DEVICE_ID_MATCH_ABSBIT,
7b19ada2
JS
866 .evbit = { BIT_MASK(EV_ABS) },
867 .absbit = { BIT_MASK(ABS_WHEEL) },
1da177e4
LT
868 },
869 {
b126207c
DT
870 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
871 INPUT_DEVICE_ID_MATCH_ABSBIT,
7b19ada2
JS
872 .evbit = { BIT_MASK(EV_ABS) },
873 .absbit = { BIT_MASK(ABS_THROTTLE) },
1da177e4 874 },
1e0afb28 875 { } /* Terminating entry */
1da177e4
LT
876};
877
878MODULE_DEVICE_TABLE(input, joydev_ids);
879
880static struct input_handler joydev_handler = {
b126207c
DT
881 .event = joydev_event,
882 .connect = joydev_connect,
883 .disconnect = joydev_disconnect,
884 .fops = &joydev_fops,
885 .minor = JOYDEV_MINOR_BASE,
886 .name = "joydev",
887 .id_table = joydev_ids,
888 .blacklist = joydev_blacklist,
1da177e4
LT
889};
890
891static int __init joydev_init(void)
892{
4263cf0f 893 return input_register_handler(&joydev_handler);
1da177e4
LT
894}
895
896static void __exit joydev_exit(void)
897{
898 input_unregister_handler(&joydev_handler);
899}
900
901module_init(joydev_init);
902module_exit(joydev_exit);