Merge tag 'v3.3.7' into 3.3/master
[zen-stable.git] / drivers / input / joydev.c
blob83341633473db99e3f0c21e50917cc0837622924
1 /*
2 * Joystick device driver for the input driver suite.
4 * Copyright (c) 1999-2002 Vojtech Pavlik
5 * Copyright (c) 1999 Colin Van Dyke
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.
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <asm/io.h>
16 #include <asm/system.h>
17 #include <linux/delay.h>
18 #include <linux/errno.h>
19 #include <linux/joystick.h>
20 #include <linux/input.h>
21 #include <linux/kernel.h>
22 #include <linux/major.h>
23 #include <linux/sched.h>
24 #include <linux/slab.h>
25 #include <linux/mm.h>
26 #include <linux/miscdevice.h>
27 #include <linux/module.h>
28 #include <linux/poll.h>
29 #include <linux/init.h>
30 #include <linux/device.h>
31 #include <linux/module.h>
32 #include <linux/moduleparam.h>
34 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
35 MODULE_DESCRIPTION("Joystick device interfaces");
36 MODULE_SUPPORTED_DEVICE("input/js");
37 MODULE_LICENSE("GPL");
38 #define JOYDEV_MINOR_BASE 0
39 #define JOYDEV_MINORS 16
40 #define JOYDEV_BUFFER_SIZE 64
41 #define MAX_REMAP_SIZE 10
43 static int remap_array[MAX_REMAP_SIZE];
44 static int remap_count = 0;
45 static int free_buttons[MAX_REMAP_SIZE];
47 struct joydev {
48 int open;
49 int minor;
50 struct input_handle handle;
51 wait_queue_head_t wait;
52 struct list_head client_list;
53 spinlock_t client_lock; /* protects client_list */
54 struct mutex mutex;
55 struct device dev;
56 bool exist;
58 struct js_corr corr[ABS_CNT];
59 struct JS_DATA_SAVE_TYPE glue;
60 int nabs;
61 int nkey;
62 __u16 keymap[KEY_MAX - BTN_MISC + 1];
63 __u16 keypam[KEY_MAX - BTN_MISC + 1];
64 __u8 absmap[ABS_CNT];
65 __u8 abspam[ABS_CNT];
66 __s16 abs[ABS_CNT];
69 struct joydev_client {
70 struct js_event buffer[JOYDEV_BUFFER_SIZE];
71 int head;
72 int tail;
73 int startup;
74 spinlock_t buffer_lock; /* protects access to buffer, head and tail */
75 struct fasync_struct *fasync;
76 struct joydev *joydev;
77 struct list_head node;
80 static struct joydev *joydev_table[JOYDEV_MINORS];
81 static DEFINE_MUTEX(joydev_table_mutex);
83 module_param_array(remap_array, int, &remap_count, 0 );
84 MODULE_PARM_DESC( remap_array, "remap axis to buttons\n" );
86 static int joydev_correct(int value, struct js_corr *corr)
88 switch (corr->type) {
90 case JS_CORR_NONE:
91 break;
93 case JS_CORR_BROKEN:
94 value = value > corr->coef[0] ? (value < corr->coef[1] ? 0 :
95 ((corr->coef[3] * (value - corr->coef[1])) >> 14)) :
96 ((corr->coef[2] * (value - corr->coef[0])) >> 14);
97 break;
99 default:
100 return 0;
103 return value < -32767 ? -32767 : (value > 32767 ? 32767 : value);
106 static void joydev_pass_event(struct joydev_client *client,
107 struct js_event *event)
109 struct joydev *joydev = client->joydev;
112 * IRQs already disabled, just acquire the lock
114 spin_lock(&client->buffer_lock);
116 client->buffer[client->head] = *event;
118 if (client->startup == joydev->nabs + joydev->nkey) {
119 client->head++;
120 client->head &= JOYDEV_BUFFER_SIZE - 1;
121 if (client->tail == client->head)
122 client->startup = 0;
125 spin_unlock(&client->buffer_lock);
127 kill_fasync(&client->fasync, SIGIO, POLL_IN);
130 static void joydev_event(struct input_handle *handle,
131 unsigned int type, unsigned int code, int value)
133 struct joydev *joydev = handle->private;
134 struct joydev_client *client;
135 struct js_event event;
136 int i;
138 if( remap_count > 0 && remap_count < MAX_REMAP_SIZE ){
139 for( i = 0; i < remap_count; i++ )
140 if( code == remap_array[i] ){
141 type = EV_KEY;
142 code = free_buttons[i];
143 if( value == 255 )
144 value = 1;
148 switch (type) {
150 case EV_KEY:
151 if (code < BTN_MISC || value == 2)
152 return;
153 event.type = JS_EVENT_BUTTON;
154 event.number = joydev->keymap[code - BTN_MISC];
155 event.value = value;
156 break;
158 case EV_ABS:
159 event.type = JS_EVENT_AXIS;
160 event.number = joydev->absmap[code];
161 event.value = joydev_correct(value,
162 &joydev->corr[event.number]);
163 if (event.value == joydev->abs[event.number])
164 return;
165 joydev->abs[event.number] = event.value;
166 break;
168 default:
169 return;
172 event.time = jiffies_to_msecs(jiffies);
174 rcu_read_lock();
175 list_for_each_entry_rcu(client, &joydev->client_list, node)
176 joydev_pass_event(client, &event);
177 rcu_read_unlock();
179 wake_up_interruptible(&joydev->wait);
182 static int joydev_fasync(int fd, struct file *file, int on)
184 struct joydev_client *client = file->private_data;
186 return fasync_helper(fd, file, on, &client->fasync);
189 static void joydev_free(struct device *dev)
191 struct joydev *joydev = container_of(dev, struct joydev, dev);
193 input_put_device(joydev->handle.dev);
194 kfree(joydev);
197 static void joydev_attach_client(struct joydev *joydev,
198 struct joydev_client *client)
200 spin_lock(&joydev->client_lock);
201 list_add_tail_rcu(&client->node, &joydev->client_list);
202 spin_unlock(&joydev->client_lock);
205 static void joydev_detach_client(struct joydev *joydev,
206 struct joydev_client *client)
208 spin_lock(&joydev->client_lock);
209 list_del_rcu(&client->node);
210 spin_unlock(&joydev->client_lock);
211 synchronize_rcu();
214 static int joydev_open_device(struct joydev *joydev)
216 int retval;
218 retval = mutex_lock_interruptible(&joydev->mutex);
219 if (retval)
220 return retval;
222 if (!joydev->exist)
223 retval = -ENODEV;
224 else if (!joydev->open++) {
225 retval = input_open_device(&joydev->handle);
226 if (retval)
227 joydev->open--;
230 mutex_unlock(&joydev->mutex);
231 return retval;
234 static void joydev_close_device(struct joydev *joydev)
236 mutex_lock(&joydev->mutex);
238 if (joydev->exist && !--joydev->open)
239 input_close_device(&joydev->handle);
241 mutex_unlock(&joydev->mutex);
245 * Wake up users waiting for IO so they can disconnect from
246 * dead device.
248 static void joydev_hangup(struct joydev *joydev)
250 struct joydev_client *client;
252 spin_lock(&joydev->client_lock);
253 list_for_each_entry(client, &joydev->client_list, node)
254 kill_fasync(&client->fasync, SIGIO, POLL_HUP);
255 spin_unlock(&joydev->client_lock);
257 wake_up_interruptible(&joydev->wait);
260 static int joydev_release(struct inode *inode, struct file *file)
262 struct joydev_client *client = file->private_data;
263 struct joydev *joydev = client->joydev;
265 joydev_detach_client(joydev, client);
266 kfree(client);
268 joydev_close_device(joydev);
269 put_device(&joydev->dev);
271 return 0;
274 static int joydev_open(struct inode *inode, struct file *file)
276 struct joydev_client *client;
277 struct joydev *joydev;
278 int i = iminor(inode) - JOYDEV_MINOR_BASE;
279 int error;
281 if (i >= JOYDEV_MINORS)
282 return -ENODEV;
284 error = mutex_lock_interruptible(&joydev_table_mutex);
285 if (error)
286 return error;
287 joydev = joydev_table[i];
288 if (joydev)
289 get_device(&joydev->dev);
290 mutex_unlock(&joydev_table_mutex);
292 if (!joydev)
293 return -ENODEV;
295 client = kzalloc(sizeof(struct joydev_client), GFP_KERNEL);
296 if (!client) {
297 error = -ENOMEM;
298 goto err_put_joydev;
301 spin_lock_init(&client->buffer_lock);
302 client->joydev = joydev;
303 joydev_attach_client(joydev, client);
305 error = joydev_open_device(joydev);
306 if (error)
307 goto err_free_client;
309 file->private_data = client;
310 nonseekable_open(inode, file);
312 return 0;
314 err_free_client:
315 joydev_detach_client(joydev, client);
316 kfree(client);
317 err_put_joydev:
318 put_device(&joydev->dev);
319 return error;
322 static int joydev_generate_startup_event(struct joydev_client *client,
323 struct input_dev *input,
324 struct js_event *event)
326 struct joydev *joydev = client->joydev;
327 int have_event;
329 spin_lock_irq(&client->buffer_lock);
331 have_event = client->startup < joydev->nabs + joydev->nkey;
333 if (have_event) {
335 event->time = jiffies_to_msecs(jiffies);
336 if (client->startup < joydev->nkey) {
337 event->type = JS_EVENT_BUTTON | JS_EVENT_INIT;
338 event->number = client->startup;
339 event->value = !!test_bit(joydev->keypam[event->number],
340 input->key);
341 } else {
342 event->type = JS_EVENT_AXIS | JS_EVENT_INIT;
343 event->number = client->startup - joydev->nkey;
344 event->value = joydev->abs[event->number];
346 client->startup++;
349 spin_unlock_irq(&client->buffer_lock);
351 return have_event;
354 static int joydev_fetch_next_event(struct joydev_client *client,
355 struct js_event *event)
357 int have_event;
359 spin_lock_irq(&client->buffer_lock);
361 have_event = client->head != client->tail;
362 if (have_event) {
363 *event = client->buffer[client->tail++];
364 client->tail &= JOYDEV_BUFFER_SIZE - 1;
367 spin_unlock_irq(&client->buffer_lock);
369 return have_event;
373 * Old joystick interface
375 static ssize_t joydev_0x_read(struct joydev_client *client,
376 struct input_dev *input,
377 char __user *buf)
379 struct joydev *joydev = client->joydev;
380 struct JS_DATA_TYPE data;
381 int i;
383 spin_lock_irq(&input->event_lock);
386 * Get device state
388 for (data.buttons = i = 0; i < 32 && i < joydev->nkey; i++)
389 data.buttons |=
390 test_bit(joydev->keypam[i], input->key) ? (1 << i) : 0;
391 data.x = (joydev->abs[0] / 256 + 128) >> joydev->glue.JS_CORR.x;
392 data.y = (joydev->abs[1] / 256 + 128) >> joydev->glue.JS_CORR.y;
395 * Reset reader's event queue
397 spin_lock(&client->buffer_lock);
398 client->startup = 0;
399 client->tail = client->head;
400 spin_unlock(&client->buffer_lock);
402 spin_unlock_irq(&input->event_lock);
404 if (copy_to_user(buf, &data, sizeof(struct JS_DATA_TYPE)))
405 return -EFAULT;
407 return sizeof(struct JS_DATA_TYPE);
410 static inline int joydev_data_pending(struct joydev_client *client)
412 struct joydev *joydev = client->joydev;
414 return client->startup < joydev->nabs + joydev->nkey ||
415 client->head != client->tail;
418 static ssize_t joydev_read(struct file *file, char __user *buf,
419 size_t count, loff_t *ppos)
421 struct joydev_client *client = file->private_data;
422 struct joydev *joydev = client->joydev;
423 struct input_dev *input = joydev->handle.dev;
424 struct js_event event;
425 int retval;
427 if (!joydev->exist)
428 return -ENODEV;
430 if (count < sizeof(struct js_event))
431 return -EINVAL;
433 if (count == sizeof(struct JS_DATA_TYPE))
434 return joydev_0x_read(client, input, buf);
436 if (!joydev_data_pending(client) && (file->f_flags & O_NONBLOCK))
437 return -EAGAIN;
439 retval = wait_event_interruptible(joydev->wait,
440 !joydev->exist || joydev_data_pending(client));
441 if (retval)
442 return retval;
444 if (!joydev->exist)
445 return -ENODEV;
447 while (retval + sizeof(struct js_event) <= count &&
448 joydev_generate_startup_event(client, input, &event)) {
450 if (copy_to_user(buf + retval, &event, sizeof(struct js_event)))
451 return -EFAULT;
453 retval += sizeof(struct js_event);
456 while (retval + sizeof(struct js_event) <= count &&
457 joydev_fetch_next_event(client, &event)) {
459 if (copy_to_user(buf + retval, &event, sizeof(struct js_event)))
460 return -EFAULT;
462 retval += sizeof(struct js_event);
465 return retval;
468 /* No kernel lock - fine */
469 static unsigned int joydev_poll(struct file *file, poll_table *wait)
471 struct joydev_client *client = file->private_data;
472 struct joydev *joydev = client->joydev;
474 poll_wait(file, &joydev->wait, wait);
475 return (joydev_data_pending(client) ? (POLLIN | POLLRDNORM) : 0) |
476 (joydev->exist ? 0 : (POLLHUP | POLLERR));
479 static int joydev_handle_JSIOCSAXMAP(struct joydev *joydev,
480 void __user *argp, size_t len)
482 __u8 *abspam;
483 int i;
484 int retval = 0;
486 len = min(len, sizeof(joydev->abspam));
488 /* Validate the map. */
489 abspam = kmalloc(len, GFP_KERNEL);
490 if (!abspam)
491 return -ENOMEM;
493 if (copy_from_user(abspam, argp, len)) {
494 retval = -EFAULT;
495 goto out;
498 for (i = 0; i < joydev->nabs; i++) {
499 if (abspam[i] > ABS_MAX) {
500 retval = -EINVAL;
501 goto out;
505 memcpy(joydev->abspam, abspam, len);
507 for (i = 0; i < joydev->nabs; i++)
508 joydev->absmap[joydev->abspam[i]] = i;
510 out:
511 kfree(abspam);
512 return retval;
515 static int joydev_handle_JSIOCSBTNMAP(struct joydev *joydev,
516 void __user *argp, size_t len)
518 __u16 *keypam;
519 int i;
520 int retval = 0;
522 len = min(len, sizeof(joydev->keypam));
524 /* Validate the map. */
525 keypam = kmalloc(len, GFP_KERNEL);
526 if (!keypam)
527 return -ENOMEM;
529 if (copy_from_user(keypam, argp, len)) {
530 retval = -EFAULT;
531 goto out;
534 for (i = 0; i < joydev->nkey; i++) {
535 if (keypam[i] > KEY_MAX || keypam[i] < BTN_MISC) {
536 retval = -EINVAL;
537 goto out;
541 memcpy(joydev->keypam, keypam, len);
543 for (i = 0; i < joydev->nkey; i++)
544 joydev->keymap[keypam[i] - BTN_MISC] = i;
546 out:
547 kfree(keypam);
548 return retval;
552 static int joydev_ioctl_common(struct joydev *joydev,
553 unsigned int cmd, void __user *argp)
555 struct input_dev *dev = joydev->handle.dev;
556 size_t len;
557 int i;
558 const char *name;
560 /* Process fixed-sized commands. */
561 switch (cmd) {
563 case JS_SET_CAL:
564 return copy_from_user(&joydev->glue.JS_CORR, argp,
565 sizeof(joydev->glue.JS_CORR)) ? -EFAULT : 0;
567 case JS_GET_CAL:
568 return copy_to_user(argp, &joydev->glue.JS_CORR,
569 sizeof(joydev->glue.JS_CORR)) ? -EFAULT : 0;
571 case JS_SET_TIMEOUT:
572 return get_user(joydev->glue.JS_TIMEOUT, (s32 __user *) argp);
574 case JS_GET_TIMEOUT:
575 return put_user(joydev->glue.JS_TIMEOUT, (s32 __user *) argp);
577 case JSIOCGVERSION:
578 return put_user(JS_VERSION, (__u32 __user *) argp);
580 case JSIOCGAXES:
581 return put_user(joydev->nabs, (__u8 __user *) argp);
583 case JSIOCGBUTTONS:
584 return put_user(joydev->nkey, (__u8 __user *) argp);
586 case JSIOCSCORR:
587 if (copy_from_user(joydev->corr, argp,
588 sizeof(joydev->corr[0]) * joydev->nabs))
589 return -EFAULT;
591 for (i = 0; i < joydev->nabs; i++) {
592 int val = input_abs_get_val(dev, joydev->abspam[i]);
593 joydev->abs[i] = joydev_correct(val, &joydev->corr[i]);
595 return 0;
597 case JSIOCGCORR:
598 return copy_to_user(argp, joydev->corr,
599 sizeof(joydev->corr[0]) * joydev->nabs) ? -EFAULT : 0;
604 * Process variable-sized commands (the axis and button map commands
605 * are considered variable-sized to decouple them from the values of
606 * ABS_MAX and KEY_MAX).
608 switch (cmd & ~IOCSIZE_MASK) {
610 case (JSIOCSAXMAP & ~IOCSIZE_MASK):
611 return joydev_handle_JSIOCSAXMAP(joydev, argp, _IOC_SIZE(cmd));
613 case (JSIOCGAXMAP & ~IOCSIZE_MASK):
614 len = min_t(size_t, _IOC_SIZE(cmd), sizeof(joydev->abspam));
615 return copy_to_user(argp, joydev->abspam, len) ? -EFAULT : len;
617 case (JSIOCSBTNMAP & ~IOCSIZE_MASK):
618 return joydev_handle_JSIOCSBTNMAP(joydev, argp, _IOC_SIZE(cmd));
620 case (JSIOCGBTNMAP & ~IOCSIZE_MASK):
621 len = min_t(size_t, _IOC_SIZE(cmd), sizeof(joydev->keypam));
622 return copy_to_user(argp, joydev->keypam, len) ? -EFAULT : len;
624 case JSIOCGNAME(0):
625 name = dev->name;
626 if (!name)
627 return 0;
629 len = min_t(size_t, _IOC_SIZE(cmd), strlen(name) + 1);
630 return copy_to_user(argp, name, len) ? -EFAULT : len;
633 return -EINVAL;
636 #ifdef CONFIG_COMPAT
637 static long joydev_compat_ioctl(struct file *file,
638 unsigned int cmd, unsigned long arg)
640 struct joydev_client *client = file->private_data;
641 struct joydev *joydev = client->joydev;
642 void __user *argp = (void __user *)arg;
643 s32 tmp32;
644 struct JS_DATA_SAVE_TYPE_32 ds32;
645 int retval;
647 retval = mutex_lock_interruptible(&joydev->mutex);
648 if (retval)
649 return retval;
651 if (!joydev->exist) {
652 retval = -ENODEV;
653 goto out;
656 switch (cmd) {
658 case JS_SET_TIMELIMIT:
659 retval = get_user(tmp32, (s32 __user *) arg);
660 if (retval == 0)
661 joydev->glue.JS_TIMELIMIT = tmp32;
662 break;
664 case JS_GET_TIMELIMIT:
665 tmp32 = joydev->glue.JS_TIMELIMIT;
666 retval = put_user(tmp32, (s32 __user *) arg);
667 break;
669 case JS_SET_ALL:
670 retval = copy_from_user(&ds32, argp,
671 sizeof(ds32)) ? -EFAULT : 0;
672 if (retval == 0) {
673 joydev->glue.JS_TIMEOUT = ds32.JS_TIMEOUT;
674 joydev->glue.BUSY = ds32.BUSY;
675 joydev->glue.JS_EXPIRETIME = ds32.JS_EXPIRETIME;
676 joydev->glue.JS_TIMELIMIT = ds32.JS_TIMELIMIT;
677 joydev->glue.JS_SAVE = ds32.JS_SAVE;
678 joydev->glue.JS_CORR = ds32.JS_CORR;
680 break;
682 case JS_GET_ALL:
683 ds32.JS_TIMEOUT = joydev->glue.JS_TIMEOUT;
684 ds32.BUSY = joydev->glue.BUSY;
685 ds32.JS_EXPIRETIME = joydev->glue.JS_EXPIRETIME;
686 ds32.JS_TIMELIMIT = joydev->glue.JS_TIMELIMIT;
687 ds32.JS_SAVE = joydev->glue.JS_SAVE;
688 ds32.JS_CORR = joydev->glue.JS_CORR;
690 retval = copy_to_user(argp, &ds32, sizeof(ds32)) ? -EFAULT : 0;
691 break;
693 default:
694 retval = joydev_ioctl_common(joydev, cmd, argp);
695 break;
698 out:
699 mutex_unlock(&joydev->mutex);
700 return retval;
702 #endif /* CONFIG_COMPAT */
704 static long joydev_ioctl(struct file *file,
705 unsigned int cmd, unsigned long arg)
707 struct joydev_client *client = file->private_data;
708 struct joydev *joydev = client->joydev;
709 void __user *argp = (void __user *)arg;
710 int retval;
712 retval = mutex_lock_interruptible(&joydev->mutex);
713 if (retval)
714 return retval;
716 if (!joydev->exist) {
717 retval = -ENODEV;
718 goto out;
721 switch (cmd) {
723 case JS_SET_TIMELIMIT:
724 retval = get_user(joydev->glue.JS_TIMELIMIT,
725 (long __user *) arg);
726 break;
728 case JS_GET_TIMELIMIT:
729 retval = put_user(joydev->glue.JS_TIMELIMIT,
730 (long __user *) arg);
731 break;
733 case JS_SET_ALL:
734 retval = copy_from_user(&joydev->glue, argp,
735 sizeof(joydev->glue)) ? -EFAULT: 0;
736 break;
738 case JS_GET_ALL:
739 retval = copy_to_user(argp, &joydev->glue,
740 sizeof(joydev->glue)) ? -EFAULT : 0;
741 break;
743 default:
744 retval = joydev_ioctl_common(joydev, cmd, argp);
745 break;
747 out:
748 mutex_unlock(&joydev->mutex);
749 return retval;
752 static const struct file_operations joydev_fops = {
753 .owner = THIS_MODULE,
754 .read = joydev_read,
755 .poll = joydev_poll,
756 .open = joydev_open,
757 .release = joydev_release,
758 .unlocked_ioctl = joydev_ioctl,
759 #ifdef CONFIG_COMPAT
760 .compat_ioctl = joydev_compat_ioctl,
761 #endif
762 .fasync = joydev_fasync,
763 .llseek = no_llseek,
766 static int joydev_install_chrdev(struct joydev *joydev)
768 joydev_table[joydev->minor] = joydev;
769 return 0;
772 static void joydev_remove_chrdev(struct joydev *joydev)
774 mutex_lock(&joydev_table_mutex);
775 joydev_table[joydev->minor] = NULL;
776 mutex_unlock(&joydev_table_mutex);
780 * Mark device non-existent. This disables writes, ioctls and
781 * prevents new users from opening the device. Already posted
782 * blocking reads will stay, however new ones will fail.
784 static void joydev_mark_dead(struct joydev *joydev)
786 mutex_lock(&joydev->mutex);
787 joydev->exist = false;
788 mutex_unlock(&joydev->mutex);
791 static void joydev_cleanup(struct joydev *joydev)
793 struct input_handle *handle = &joydev->handle;
795 joydev_mark_dead(joydev);
796 joydev_hangup(joydev);
797 joydev_remove_chrdev(joydev);
799 /* joydev is marked dead so no one else accesses joydev->open */
800 if (joydev->open)
801 input_close_device(handle);
805 static bool joydev_match(struct input_handler *handler, struct input_dev *dev)
807 /* Avoid touchpads and touchscreens */
808 if (test_bit(EV_KEY, dev->evbit) && test_bit(BTN_TOUCH, dev->keybit))
809 return false;
811 /* Avoid tablets, digitisers and similar devices */
812 if (test_bit(EV_KEY, dev->evbit) && test_bit(BTN_DIGI, dev->keybit))
813 return false;
815 return true;
818 static int joydev_connect(struct input_handler *handler, struct input_dev *dev,
819 const struct input_device_id *id)
821 struct joydev *joydev;
822 int i, j = 0, t, minor;
823 int error;
825 for (minor = 0; minor < JOYDEV_MINORS; minor++)
826 if (!joydev_table[minor])
827 break;
829 if (minor == JOYDEV_MINORS) {
830 pr_err("no more free joydev devices\n");
831 return -ENFILE;
834 joydev = kzalloc(sizeof(struct joydev), GFP_KERNEL);
835 if (!joydev)
836 return -ENOMEM;
838 INIT_LIST_HEAD(&joydev->client_list);
839 spin_lock_init(&joydev->client_lock);
840 mutex_init(&joydev->mutex);
841 init_waitqueue_head(&joydev->wait);
843 dev_set_name(&joydev->dev, "js%d", minor);
844 joydev->exist = true;
845 joydev->minor = minor;
847 joydev->handle.dev = input_get_device(dev);
848 joydev->handle.name = dev_name(&joydev->dev);
849 joydev->handle.handler = handler;
850 joydev->handle.private = joydev;
852 for (i = 0; i < ABS_CNT; i++)
853 if (test_bit(i, dev->absbit)) {
854 joydev->absmap[i] = joydev->nabs;
855 joydev->abspam[joydev->nabs] = i;
856 joydev->nabs++;
859 for (i = BTN_JOYSTICK - BTN_MISC; i < KEY_MAX - BTN_MISC + 1; i++)
860 if (test_bit(i + BTN_MISC, dev->keybit)) {
861 joydev->keymap[i] = joydev->nkey;
862 joydev->keypam[joydev->nkey] = i + BTN_MISC;
863 joydev->nkey++;
864 j = i;
866 if( remap_count > 0 && remap_count < MAX_REMAP_SIZE ){
867 printk( "[joydev] axis remapping enabled\n" );
868 for( i = 0; i < remap_count; i++ ){
869 joydev->keymap[j + i + 1] = joydev->nkey;
870 joydev->keypam[joydev->nkey] = i + j + 1 + BTN_MISC;
871 free_buttons[i] = j + i + 1 + BTN_MISC;
872 joydev->nkey++;
875 for (i = 0; i < BTN_JOYSTICK - BTN_MISC; i++)
876 if (test_bit(i + BTN_MISC, dev->keybit)) {
877 joydev->keymap[i] = joydev->nkey;
878 joydev->keypam[joydev->nkey] = i + BTN_MISC;
879 joydev->nkey++;
882 for (i = 0; i < joydev->nabs; i++) {
883 j = joydev->abspam[i];
884 if (input_abs_get_max(dev, j) == input_abs_get_min(dev, j)) {
885 joydev->corr[i].type = JS_CORR_NONE;
886 joydev->abs[i] = input_abs_get_val(dev, j);
887 continue;
889 joydev->corr[i].type = JS_CORR_BROKEN;
890 joydev->corr[i].prec = input_abs_get_fuzz(dev, j);
892 t = (input_abs_get_max(dev, j) + input_abs_get_min(dev, j)) / 2;
893 joydev->corr[i].coef[0] = t - input_abs_get_flat(dev, j);
894 joydev->corr[i].coef[1] = t + input_abs_get_flat(dev, j);
896 t = (input_abs_get_max(dev, j) - input_abs_get_min(dev, j)) / 2
897 - 2 * input_abs_get_flat(dev, j);
898 if (t) {
899 joydev->corr[i].coef[2] = (1 << 29) / t;
900 joydev->corr[i].coef[3] = (1 << 29) / t;
902 joydev->abs[i] =
903 joydev_correct(input_abs_get_val(dev, j),
904 joydev->corr + i);
908 joydev->dev.devt = MKDEV(INPUT_MAJOR, JOYDEV_MINOR_BASE + minor);
909 joydev->dev.class = &input_class;
910 joydev->dev.parent = &dev->dev;
911 joydev->dev.release = joydev_free;
912 device_initialize(&joydev->dev);
914 error = input_register_handle(&joydev->handle);
915 if (error)
916 goto err_free_joydev;
918 error = joydev_install_chrdev(joydev);
919 if (error)
920 goto err_unregister_handle;
922 error = device_add(&joydev->dev);
923 if (error)
924 goto err_cleanup_joydev;
926 return 0;
928 err_cleanup_joydev:
929 joydev_cleanup(joydev);
930 err_unregister_handle:
931 input_unregister_handle(&joydev->handle);
932 err_free_joydev:
933 put_device(&joydev->dev);
934 return error;
937 static void joydev_disconnect(struct input_handle *handle)
939 struct joydev *joydev = handle->private;
941 device_del(&joydev->dev);
942 joydev_cleanup(joydev);
943 input_unregister_handle(handle);
944 put_device(&joydev->dev);
947 static const struct input_device_id joydev_ids[] = {
949 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
950 INPUT_DEVICE_ID_MATCH_ABSBIT,
951 .evbit = { BIT_MASK(EV_ABS) },
952 .absbit = { BIT_MASK(ABS_X) },
955 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
956 INPUT_DEVICE_ID_MATCH_ABSBIT,
957 .evbit = { BIT_MASK(EV_ABS) },
958 .absbit = { BIT_MASK(ABS_WHEEL) },
961 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
962 INPUT_DEVICE_ID_MATCH_ABSBIT,
963 .evbit = { BIT_MASK(EV_ABS) },
964 .absbit = { BIT_MASK(ABS_THROTTLE) },
967 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
968 INPUT_DEVICE_ID_MATCH_KEYBIT,
969 .evbit = { BIT_MASK(EV_KEY) },
970 .keybit = {[BIT_WORD(BTN_JOYSTICK)] = BIT_MASK(BTN_JOYSTICK) },
973 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
974 INPUT_DEVICE_ID_MATCH_KEYBIT,
975 .evbit = { BIT_MASK(EV_KEY) },
976 .keybit = { [BIT_WORD(BTN_GAMEPAD)] = BIT_MASK(BTN_GAMEPAD) },
979 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
980 INPUT_DEVICE_ID_MATCH_KEYBIT,
981 .evbit = { BIT_MASK(EV_KEY) },
982 .keybit = { [BIT_WORD(BTN_TRIGGER_HAPPY)] = BIT_MASK(BTN_TRIGGER_HAPPY) },
984 { } /* Terminating entry */
987 MODULE_DEVICE_TABLE(input, joydev_ids);
989 static struct input_handler joydev_handler = {
990 .event = joydev_event,
991 .match = joydev_match,
992 .connect = joydev_connect,
993 .disconnect = joydev_disconnect,
994 .fops = &joydev_fops,
995 .minor = JOYDEV_MINOR_BASE,
996 .name = "joydev",
997 .id_table = joydev_ids,
1000 static int __init joydev_init(void)
1002 return input_register_handler(&joydev_handler);
1005 static void __exit joydev_exit(void)
1007 input_unregister_handler(&joydev_handler);
1010 module_init(joydev_init);
1011 module_exit(joydev_exit);