Input: remove tsdev interface
[deliverable/linux.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;
42 char name[16];
43 struct input_handle handle;
44 wait_queue_head_t wait;
d0ffb9be 45 struct list_head client_list;
b126207c
DT
46 spinlock_t client_lock; /* protects client_list */
47 struct mutex mutex;
9657d75c
DT
48 struct device dev;
49
1da177e4
LT
50 struct js_corr corr[ABS_MAX + 1];
51 struct JS_DATA_SAVE_TYPE glue;
52 int nabs;
53 int nkey;
54 __u16 keymap[KEY_MAX - BTN_MISC + 1];
55 __u16 keypam[KEY_MAX - BTN_MISC + 1];
56 __u8 absmap[ABS_MAX + 1];
57 __u8 abspam[ABS_MAX + 1];
58 __s16 abs[ABS_MAX + 1];
59};
60
d0ffb9be 61struct joydev_client {
1da177e4
LT
62 struct js_event buffer[JOYDEV_BUFFER_SIZE];
63 int head;
64 int tail;
65 int startup;
b126207c 66 spinlock_t buffer_lock; /* protects access to buffer, head and tail */
1da177e4
LT
67 struct fasync_struct *fasync;
68 struct joydev *joydev;
69 struct list_head node;
70};
71
72static struct joydev *joydev_table[JOYDEV_MINORS];
b126207c 73static DEFINE_MUTEX(joydev_table_mutex);
1da177e4
LT
74
75static int joydev_correct(int value, struct js_corr *corr)
76{
77 switch (corr->type) {
b126207c
DT
78
79 case JS_CORR_NONE:
80 break;
81
82 case JS_CORR_BROKEN:
83 value = value > corr->coef[0] ? (value < corr->coef[1] ? 0 :
84 ((corr->coef[3] * (value - corr->coef[1])) >> 14)) :
85 ((corr->coef[2] * (value - corr->coef[0])) >> 14);
86 break;
87
88 default:
89 return 0;
1da177e4
LT
90 }
91
1e0afb28 92 return value < -32767 ? -32767 : (value > 32767 ? 32767 : value);
1da177e4
LT
93}
94
b126207c
DT
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)
1da177e4
LT
121{
122 struct joydev *joydev = handle->private;
d0ffb9be 123 struct joydev_client *client;
1da177e4
LT
124 struct js_event event;
125
126 switch (type) {
127
b126207c
DT
128 case EV_KEY:
129 if (code < BTN_MISC || value == 2)
130 return;
131 event.type = JS_EVENT_BUTTON;
132 event.number = joydev->keymap[code - BTN_MISC];
133 event.value = value;
134 break;
1da177e4 135
b126207c
DT
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])
1da177e4 142 return;
b126207c
DT
143 joydev->abs[event.number] = event.value;
144 break;
145
146 default:
147 return;
1da177e4
LT
148 }
149
6f5eacfc 150 event.time = jiffies_to_msecs(jiffies);
1da177e4 151
b126207c
DT
152 list_for_each_entry_rcu(client, &joydev->client_list, node)
153 joydev_pass_event(client, &event);
1da177e4
LT
154
155 wake_up_interruptible(&joydev->wait);
156}
157
158static int joydev_fasync(int fd, struct file *file, int on)
159{
160 int retval;
d0ffb9be 161 struct joydev_client *client = file->private_data;
1e0afb28 162
d0ffb9be 163 retval = fasync_helper(fd, file, on, &client->fasync);
1e0afb28 164
1da177e4
LT
165 return retval < 0 ? retval : 0;
166}
167
9657d75c 168static void joydev_free(struct device *dev)
1da177e4 169{
9657d75c
DT
170 struct joydev *joydev = container_of(dev, struct joydev, dev);
171
1da177e4
LT
172 kfree(joydev);
173}
174
b126207c
DT
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;
06445014 208 else if (!joydev->open++) {
b126207c 209 retval = input_open_device(&joydev->handle);
06445014
ON
210 if (retval)
211 joydev->open--;
212 }
b126207c
DT
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
d0ffb9be 244static int joydev_release(struct inode *inode, struct file *file)
1da177e4 245{
d0ffb9be
DT
246 struct joydev_client *client = file->private_data;
247 struct joydev *joydev = client->joydev;
1da177e4
LT
248
249 joydev_fasync(-1, file, 0);
b126207c 250 joydev_detach_client(joydev, client);
d0ffb9be 251 kfree(client);
1da177e4 252
b126207c 253 joydev_close_device(joydev);
9657d75c 254 put_device(&joydev->dev);
1da177e4 255
1da177e4
LT
256 return 0;
257}
258
259static int joydev_open(struct inode *inode, struct file *file)
260{
d0ffb9be
DT
261 struct joydev_client *client;
262 struct joydev *joydev;
1da177e4 263 int i = iminor(inode) - JOYDEV_MINOR_BASE;
d542ed82 264 int error;
1da177e4 265
d0ffb9be
DT
266 if (i >= JOYDEV_MINORS)
267 return -ENODEV;
1da177e4 268
b126207c
DT
269 error = mutex_lock_interruptible(&joydev_table_mutex);
270 if (error)
271 return error;
d0ffb9be 272 joydev = joydev_table[i];
b126207c
DT
273 if (joydev)
274 get_device(&joydev->dev);
275 mutex_unlock(&joydev_table_mutex);
1da177e4 276
b126207c
DT
277 if (!joydev)
278 return -ENODEV;
9657d75c 279
d0ffb9be 280 client = kzalloc(sizeof(struct joydev_client), GFP_KERNEL);
9657d75c
DT
281 if (!client) {
282 error = -ENOMEM;
283 goto err_put_joydev;
284 }
1da177e4 285
b126207c 286 spin_lock_init(&client->buffer_lock);
d0ffb9be 287 client->joydev = joydev;
b126207c 288 joydev_attach_client(joydev, client);
1da177e4 289
b126207c
DT
290 error = joydev_open_device(joydev);
291 if (error)
292 goto err_free_client;
1da177e4 293
d0ffb9be 294 file->private_data = client;
1da177e4 295 return 0;
9657d75c
DT
296
297 err_free_client:
b126207c 298 joydev_detach_client(joydev, client);
9657d75c
DT
299 kfree(client);
300 err_put_joydev:
301 put_device(&joydev->dev);
302 return error;
1da177e4
LT
303}
304
b126207c
DT
305static int joydev_generate_startup_event(struct joydev_client *client,
306 struct input_dev *input,
307 struct js_event *event)
1da177e4 308{
b126207c
DT
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);
1da177e4
LT
391}
392
b126207c
DT
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;
399}
400
401static ssize_t joydev_read(struct file *file, char __user *buf,
402 size_t count, loff_t *ppos)
1da177e4 403{
d0ffb9be
DT
404 struct joydev_client *client = file->private_data;
405 struct joydev *joydev = client->joydev;
1da177e4 406 struct input_dev *input = joydev->handle.dev;
b126207c
DT
407 struct js_event event;
408 int retval;
1da177e4 409
d0ffb9be 410 if (!joydev->exist)
1da177e4
LT
411 return -ENODEV;
412
413 if (count < sizeof(struct js_event))
414 return -EINVAL;
415
b126207c
DT
416 if (count == sizeof(struct JS_DATA_TYPE))
417 return joydev_0x_read(client, input, buf);
1da177e4 418
b126207c 419 if (!joydev_data_pending(client) && (file->f_flags & O_NONBLOCK))
1e0afb28 420 return -EAGAIN;
1da177e4 421
d0ffb9be 422 retval = wait_event_interruptible(joydev->wait,
b126207c 423 !joydev->exist || joydev_data_pending(client));
1da177e4
LT
424 if (retval)
425 return retval;
426
d0ffb9be 427 if (!joydev->exist)
1da177e4
LT
428 return -ENODEV;
429
b126207c
DT
430 while (retval + sizeof(struct js_event) <= count &&
431 joydev_generate_startup_event(client, input, &event)) {
1da177e4
LT
432
433 if (copy_to_user(buf + retval, &event, sizeof(struct js_event)))
434 return -EFAULT;
435
1da177e4
LT
436 retval += sizeof(struct js_event);
437 }
438
b126207c
DT
439 while (retval + sizeof(struct js_event) <= count &&
440 joydev_fetch_next_event(client, &event)) {
1da177e4 441
b126207c 442 if (copy_to_user(buf + retval, &event, sizeof(struct js_event)))
1da177e4
LT
443 return -EFAULT;
444
1da177e4
LT
445 retval += sizeof(struct js_event);
446 }
447
448 return retval;
449}
450
451/* No kernel lock - fine */
452static unsigned int joydev_poll(struct file *file, poll_table *wait)
453{
d0ffb9be
DT
454 struct joydev_client *client = file->private_data;
455 struct joydev *joydev = client->joydev;
1e0afb28 456
d0ffb9be 457 poll_wait(file, &joydev->wait, wait);
b126207c
DT
458 return (joydev_data_pending(client) ? (POLLIN | POLLRDNORM) : 0) |
459 (joydev->exist ? 0 : (POLLHUP | POLLERR));
1da177e4
LT
460}
461
b126207c
DT
462static int joydev_ioctl_common(struct joydev *joydev,
463 unsigned int cmd, void __user *argp)
1da177e4 464{
1da177e4 465 struct input_dev *dev = joydev->handle.dev;
1da177e4
LT
466 int i, j;
467
1da177e4
LT
468 switch (cmd) {
469
b126207c
DT
470 case JS_SET_CAL:
471 return copy_from_user(&joydev->glue.JS_CORR, argp,
024ac44c 472 sizeof(joydev->glue.JS_CORR)) ? -EFAULT : 0;
1e0afb28 473
b126207c
DT
474 case JS_GET_CAL:
475 return copy_to_user(argp, &joydev->glue.JS_CORR,
024ac44c 476 sizeof(joydev->glue.JS_CORR)) ? -EFAULT : 0;
1e0afb28 477
b126207c
DT
478 case JS_SET_TIMEOUT:
479 return get_user(joydev->glue.JS_TIMEOUT, (s32 __user *) argp);
1e0afb28 480
b126207c
DT
481 case JS_GET_TIMEOUT:
482 return put_user(joydev->glue.JS_TIMEOUT, (s32 __user *) argp);
1da177e4 483
b126207c
DT
484 case JSIOCGVERSION:
485 return put_user(JS_VERSION, (__u32 __user *) argp);
1e0afb28 486
b126207c
DT
487 case JSIOCGAXES:
488 return put_user(joydev->nabs, (__u8 __user *) argp);
1e0afb28 489
b126207c
DT
490 case JSIOCGBUTTONS:
491 return put_user(joydev->nkey, (__u8 __user *) argp);
1e0afb28 492
b126207c
DT
493 case JSIOCSCORR:
494 if (copy_from_user(joydev->corr, argp,
495 sizeof(joydev->corr[0]) * joydev->nabs))
496 return -EFAULT;
1e0afb28 497
b126207c
DT
498 for (i = 0; i < joydev->nabs; i++) {
499 j = joydev->abspam[i];
500 joydev->abs[i] = joydev_correct(dev->abs[j],
501 &joydev->corr[i]);
502 }
503 return 0;
1e0afb28 504
b126207c
DT
505 case JSIOCGCORR:
506 return copy_to_user(argp, joydev->corr,
507 sizeof(joydev->corr[0]) * joydev->nabs) ? -EFAULT : 0;
508
509 case JSIOCSAXMAP:
510 if (copy_from_user(joydev->abspam, argp,
511 sizeof(__u8) * (ABS_MAX + 1)))
512 return -EFAULT;
513
514 for (i = 0; i < joydev->nabs; i++) {
515 if (joydev->abspam[i] > ABS_MAX)
516 return -EINVAL;
517 joydev->absmap[joydev->abspam[i]] = i;
518 }
519 return 0;
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))
1da177e4 552 return -EFAULT;
b126207c
DT
553 return len;
554 }
1da177e4
LT
555 }
556 return -EINVAL;
557}
558
024ac44c 559#ifdef CONFIG_COMPAT
b126207c
DT
560static long joydev_compat_ioctl(struct file *file,
561 unsigned int cmd, unsigned long arg)
024ac44c 562{
d0ffb9be
DT
563 struct joydev_client *client = file->private_data;
564 struct joydev *joydev = client->joydev;
024ac44c
JF
565 void __user *argp = (void __user *)arg;
566 s32 tmp32;
567 struct JS_DATA_SAVE_TYPE_32 ds32;
b126207c 568 int retval;
024ac44c 569
b126207c
DT
570 retval = mutex_lock_interruptible(&joydev->mutex);
571 if (retval)
572 return retval;
573
574 if (!joydev->exist) {
575 retval = -ENODEV;
576 goto out;
577 }
578
579 switch (cmd) {
1e0afb28 580
024ac44c 581 case JS_SET_TIMELIMIT:
b126207c
DT
582 retval = get_user(tmp32, (s32 __user *) arg);
583 if (retval == 0)
024ac44c
JF
584 joydev->glue.JS_TIMELIMIT = tmp32;
585 break;
b126207c 586
024ac44c
JF
587 case JS_GET_TIMELIMIT:
588 tmp32 = joydev->glue.JS_TIMELIMIT;
b126207c 589 retval = put_user(tmp32, (s32 __user *) arg);
024ac44c
JF
590 break;
591
592 case JS_SET_ALL:
b126207c
DT
593 retval = copy_from_user(&ds32, argp,
594 sizeof(ds32)) ? -EFAULT : 0;
595 if (retval == 0) {
024ac44c
JF
596 joydev->glue.JS_TIMEOUT = ds32.JS_TIMEOUT;
597 joydev->glue.BUSY = ds32.BUSY;
598 joydev->glue.JS_EXPIRETIME = ds32.JS_EXPIRETIME;
599 joydev->glue.JS_TIMELIMIT = ds32.JS_TIMELIMIT;
600 joydev->glue.JS_SAVE = ds32.JS_SAVE;
601 joydev->glue.JS_CORR = ds32.JS_CORR;
602 }
603 break;
604
605 case JS_GET_ALL:
606 ds32.JS_TIMEOUT = joydev->glue.JS_TIMEOUT;
607 ds32.BUSY = joydev->glue.BUSY;
608 ds32.JS_EXPIRETIME = joydev->glue.JS_EXPIRETIME;
609 ds32.JS_TIMELIMIT = joydev->glue.JS_TIMELIMIT;
610 ds32.JS_SAVE = joydev->glue.JS_SAVE;
611 ds32.JS_CORR = joydev->glue.JS_CORR;
612
b126207c 613 retval = copy_to_user(argp, &ds32, sizeof(ds32)) ? -EFAULT : 0;
024ac44c
JF
614 break;
615
616 default:
b126207c
DT
617 retval = joydev_ioctl_common(joydev, cmd, argp);
618 break;
024ac44c 619 }
b126207c
DT
620
621 out:
622 mutex_unlock(&joydev->mutex);
623 return retval;
024ac44c
JF
624}
625#endif /* CONFIG_COMPAT */
626
b126207c
DT
627static long joydev_ioctl(struct file *file,
628 unsigned int cmd, unsigned long arg)
024ac44c 629{
d0ffb9be
DT
630 struct joydev_client *client = file->private_data;
631 struct joydev *joydev = client->joydev;
024ac44c 632 void __user *argp = (void __user *)arg;
b126207c 633 int retval;
024ac44c 634
b126207c
DT
635 retval = mutex_lock_interruptible(&joydev->mutex);
636 if (retval)
637 return retval;
024ac44c 638
b126207c
DT
639 if (!joydev->exist) {
640 retval = -ENODEV;
641 goto out;
024ac44c 642 }
b126207c
DT
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;
665
666 default:
667 retval = joydev_ioctl_common(joydev, cmd, argp);
668 break;
669 }
670 out:
671 mutex_unlock(&joydev->mutex);
672 return retval;
024ac44c
JF
673}
674
66e66118 675static const struct file_operations joydev_fops = {
b126207c
DT
676 .owner = THIS_MODULE,
677 .read = joydev_read,
678 .poll = joydev_poll,
679 .open = joydev_open,
680 .release = joydev_release,
681 .unlocked_ioctl = joydev_ioctl,
024ac44c 682#ifdef CONFIG_COMPAT
b126207c 683 .compat_ioctl = joydev_compat_ioctl,
024ac44c 684#endif
b126207c 685 .fasync = joydev_fasync,
1da177e4
LT
686};
687
b126207c
DT
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
5b2a0826
DT
726static int joydev_connect(struct input_handler *handler, struct input_dev *dev,
727 const struct input_device_id *id)
1da177e4
LT
728{
729 struct joydev *joydev;
730 int i, j, t, minor;
5b2a0826 731 int error;
1da177e4 732
b126207c
DT
733 for (minor = 0; minor < JOYDEV_MINORS; minor++)
734 if (!joydev_table[minor])
735 break;
736
1da177e4
LT
737 if (minor == JOYDEV_MINORS) {
738 printk(KERN_ERR "joydev: no more free joydev devices\n");
5b2a0826 739 return -ENFILE;
1da177e4
LT
740 }
741
5b2a0826
DT
742 joydev = kzalloc(sizeof(struct joydev), GFP_KERNEL);
743 if (!joydev)
744 return -ENOMEM;
1da177e4 745
d0ffb9be 746 INIT_LIST_HEAD(&joydev->client_list);
b126207c
DT
747 spin_lock_init(&joydev->client_lock);
748 mutex_init(&joydev->mutex);
1da177e4
LT
749 init_waitqueue_head(&joydev->wait);
750
b126207c
DT
751 snprintf(joydev->name, sizeof(joydev->name), "js%d", minor);
752 joydev->exist = 1;
1da177e4 753 joydev->minor = minor;
b126207c 754
1da177e4
LT
755 joydev->exist = 1;
756 joydev->handle.dev = dev;
757 joydev->handle.name = joydev->name;
758 joydev->handle.handler = handler;
759 joydev->handle.private = joydev;
1da177e4
LT
760
761 for (i = 0; i < ABS_MAX + 1; i++)
762 if (test_bit(i, dev->absbit)) {
763 joydev->absmap[i] = joydev->nabs;
764 joydev->abspam[joydev->nabs] = i;
765 joydev->nabs++;
766 }
767
768 for (i = BTN_JOYSTICK - BTN_MISC; i < KEY_MAX - BTN_MISC + 1; i++)
769 if (test_bit(i + BTN_MISC, dev->keybit)) {
770 joydev->keymap[i] = joydev->nkey;
771 joydev->keypam[joydev->nkey] = i + BTN_MISC;
772 joydev->nkey++;
773 }
774
f24949e8 775 for (i = 0; i < BTN_JOYSTICK - BTN_MISC; i++)
1da177e4
LT
776 if (test_bit(i + BTN_MISC, dev->keybit)) {
777 joydev->keymap[i] = joydev->nkey;
778 joydev->keypam[joydev->nkey] = i + BTN_MISC;
779 joydev->nkey++;
780 }
781
782 for (i = 0; i < joydev->nabs; i++) {
783 j = joydev->abspam[i];
784 if (dev->absmax[j] == dev->absmin[j]) {
785 joydev->corr[i].type = JS_CORR_NONE;
786 joydev->abs[i] = dev->abs[j];
787 continue;
788 }
789 joydev->corr[i].type = JS_CORR_BROKEN;
790 joydev->corr[i].prec = dev->absfuzz[j];
b126207c
DT
791 joydev->corr[i].coef[0] =
792 (dev->absmax[j] + dev->absmin[j]) / 2 - dev->absflat[j];
793 joydev->corr[i].coef[1] =
794 (dev->absmax[j] + dev->absmin[j]) / 2 + dev->absflat[j];
795
796 t = (dev->absmax[j] - dev->absmin[j]) / 2 - 2 * dev->absflat[j];
797 if (t) {
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 }
1da177e4
LT
804 }
805
b126207c
DT
806 strlcpy(joydev->dev.bus_id, joydev->name, sizeof(joydev->dev.bus_id));
807 joydev->dev.devt = MKDEV(INPUT_MAJOR, JOYDEV_MINOR_BASE + minor);
9657d75c
DT
808 joydev->dev.class = &input_class;
809 joydev->dev.parent = &dev->dev;
9657d75c
DT
810 joydev->dev.release = joydev_free;
811 device_initialize(&joydev->dev);
5b2a0826 812
b126207c 813 error = input_register_handle(&joydev->handle);
5b2a0826 814 if (error)
9657d75c 815 goto err_free_joydev;
5b2a0826 816
b126207c 817 error = joydev_install_chrdev(joydev);
5b2a0826 818 if (error)
b126207c
DT
819 goto err_unregister_handle;
820
821 error = device_add(&joydev->dev);
822 if (error)
823 goto err_cleanup_joydev;
5b2a0826
DT
824
825 return 0;
1da177e4 826
b126207c
DT
827 err_cleanup_joydev:
828 joydev_cleanup(joydev);
829 err_unregister_handle:
830 input_unregister_handle(&joydev->handle);
5b2a0826 831 err_free_joydev:
9657d75c 832 put_device(&joydev->dev);
5b2a0826 833 return error;
1da177e4
LT
834}
835
836static void joydev_disconnect(struct input_handle *handle)
837{
838 struct joydev *joydev = handle->private;
1da177e4 839
9657d75c 840 device_del(&joydev->dev);
b126207c
DT
841 joydev_cleanup(joydev);
842 input_unregister_handle(handle);
9657d75c 843 put_device(&joydev->dev);
1da177e4
LT
844}
845
66e66118 846static const struct input_device_id joydev_blacklist[] = {
1da177e4 847 {
b126207c
DT
848 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
849 INPUT_DEVICE_ID_MATCH_KEYBIT,
1da177e4
LT
850 .evbit = { BIT(EV_KEY) },
851 .keybit = { [LONG(BTN_TOUCH)] = BIT(BTN_TOUCH) },
1e0afb28
DT
852 }, /* Avoid itouchpads, touchscreens and tablets */
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,
1da177e4
LT
860 .evbit = { BIT(EV_ABS) },
861 .absbit = { BIT(ABS_X) },
862 },
863 {
b126207c
DT
864 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
865 INPUT_DEVICE_ID_MATCH_ABSBIT,
1da177e4
LT
866 .evbit = { BIT(EV_ABS) },
867 .absbit = { BIT(ABS_WHEEL) },
868 },
869 {
b126207c
DT
870 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
871 INPUT_DEVICE_ID_MATCH_ABSBIT,
1da177e4
LT
872 .evbit = { BIT(EV_ABS) },
873 .absbit = { BIT(ABS_THROTTLE) },
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);
This page took 0.242909 seconds and 5 git commands to generate.