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