Merge master.kernel.org:/pub/scm/linux/kernel/git/torvalds/linux-2.6
[linux-2.6] / 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 static int joydev_correct(int value, struct js_corr *corr)
76 {
77         switch (corr->type) {
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;
90         }
91
92         return value < -32767 ? -32767 : (value > 32767 ? 32767 : value);
93 }
94
95 static 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
119 static void joydev_event(struct input_handle *handle,
120                          unsigned int type, unsigned int code, int value)
121 {
122         struct joydev *joydev = handle->private;
123         struct joydev_client *client;
124         struct js_event event;
125
126         switch (type) {
127
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;
135
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])
142                         return;
143                 joydev->abs[event.number] = event.value;
144                 break;
145
146         default:
147                 return;
148         }
149
150         event.time = jiffies_to_msecs(jiffies);
151
152         list_for_each_entry_rcu(client, &joydev->client_list, node)
153                 joydev_pass_event(client, &event);
154
155         wake_up_interruptible(&joydev->wait);
156 }
157
158 static int joydev_fasync(int fd, struct file *file, int on)
159 {
160         int retval;
161         struct joydev_client *client = file->private_data;
162
163         retval = fasync_helper(fd, file, on, &client->fasync);
164
165         return retval < 0 ? retval : 0;
166 }
167
168 static void joydev_free(struct device *dev)
169 {
170         struct joydev *joydev = container_of(dev, struct joydev, dev);
171
172         kfree(joydev);
173 }
174
175 static 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
189 static 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
198 static int joydev_open_device(struct joydev *joydev)
199 {
200         int retval;
201
202         retval = mutex_lock_interruptible(&joydev->mutex);
203         if (retval)
204                 return retval;
205
206         if (!joydev->exist)
207                 retval = -ENODEV;
208         else if (!joydev->open++) {
209                 retval = input_open_device(&joydev->handle);
210                 if (retval)
211                         joydev->open--;
212         }
213
214         mutex_unlock(&joydev->mutex);
215         return retval;
216 }
217
218 static 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  */
232 static 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
244 static int joydev_release(struct inode *inode, struct file *file)
245 {
246         struct joydev_client *client = file->private_data;
247         struct joydev *joydev = client->joydev;
248
249         joydev_fasync(-1, file, 0);
250         joydev_detach_client(joydev, client);
251         kfree(client);
252
253         joydev_close_device(joydev);
254         put_device(&joydev->dev);
255
256         return 0;
257 }
258
259 static int joydev_open(struct inode *inode, struct file *file)
260 {
261         struct joydev_client *client;
262         struct joydev *joydev;
263         int i = iminor(inode) - JOYDEV_MINOR_BASE;
264         int error;
265
266         if (i >= JOYDEV_MINORS)
267                 return -ENODEV;
268
269         error = mutex_lock_interruptible(&joydev_table_mutex);
270         if (error)
271                 return error;
272         joydev = joydev_table[i];
273         if (joydev)
274                 get_device(&joydev->dev);
275         mutex_unlock(&joydev_table_mutex);
276
277         if (!joydev)
278                 return -ENODEV;
279
280         client = kzalloc(sizeof(struct joydev_client), GFP_KERNEL);
281         if (!client) {
282                 error = -ENOMEM;
283                 goto err_put_joydev;
284         }
285
286         spin_lock_init(&client->buffer_lock);
287         client->joydev = joydev;
288         joydev_attach_client(joydev, client);
289
290         error = joydev_open_device(joydev);
291         if (error)
292                 goto err_free_client;
293
294         file->private_data = client;
295         return 0;
296
297  err_free_client:
298         joydev_detach_client(joydev, client);
299         kfree(client);
300  err_put_joydev:
301         put_device(&joydev->dev);
302         return error;
303 }
304
305 static int joydev_generate_startup_event(struct joydev_client *client,
306                                          struct input_dev *input,
307                                          struct js_event *event)
308 {
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
337 static 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  */
358 static ssize_t joydev_0x_read(struct joydev_client *client,
359                               struct input_dev *input,
360                               char __user *buf)
361 {
362         struct joydev *joydev = client->joydev;
363         struct JS_DATA_TYPE data;
364         int i;
365
366         spin_lock_irq(&input->event_lock);
367
368         /*
369          * Get device state
370          */
371         for (data.buttons = i = 0; i < 32 && i < joydev->nkey; i++)
372                 data.buttons |=
373                         test_bit(joydev->keypam[i], input->key) ? (1 << i) : 0;
374         data.x = (joydev->abs[0] / 256 + 128) >> joydev->glue.JS_CORR.x;
375         data.y = (joydev->abs[1] / 256 + 128) >> joydev->glue.JS_CORR.y;
376
377         /*
378          * Reset reader's event queue
379          */
380         spin_lock(&client->buffer_lock);
381         client->startup = 0;
382         client->tail = client->head;
383         spin_unlock(&client->buffer_lock);
384
385         spin_unlock_irq(&input->event_lock);
386
387         if (copy_to_user(buf, &data, sizeof(struct JS_DATA_TYPE)))
388                 return -EFAULT;
389
390         return sizeof(struct JS_DATA_TYPE);
391 }
392
393 static 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
401 static ssize_t joydev_read(struct file *file, char __user *buf,
402                            size_t count, loff_t *ppos)
403 {
404         struct joydev_client *client = file->private_data;
405         struct joydev *joydev = client->joydev;
406         struct input_dev *input = joydev->handle.dev;
407         struct js_event event;
408         int retval;
409
410         if (!joydev->exist)
411                 return -ENODEV;
412
413         if (count < sizeof(struct js_event))
414                 return -EINVAL;
415
416         if (count == sizeof(struct JS_DATA_TYPE))
417                 return joydev_0x_read(client, input, buf);
418
419         if (!joydev_data_pending(client) && (file->f_flags & O_NONBLOCK))
420                 return -EAGAIN;
421
422         retval = wait_event_interruptible(joydev->wait,
423                         !joydev->exist || joydev_data_pending(client));
424         if (retval)
425                 return retval;
426
427         if (!joydev->exist)
428                 return -ENODEV;
429
430         while (retval + sizeof(struct js_event) <= count &&
431                joydev_generate_startup_event(client, input, &event)) {
432
433                 if (copy_to_user(buf + retval, &event, sizeof(struct js_event)))
434                         return -EFAULT;
435
436                 retval += sizeof(struct js_event);
437         }
438
439         while (retval + sizeof(struct js_event) <= count &&
440                joydev_fetch_next_event(client, &event)) {
441
442                 if (copy_to_user(buf + retval, &event, sizeof(struct js_event)))
443                         return -EFAULT;
444
445                 retval += sizeof(struct js_event);
446         }
447
448         return retval;
449 }
450
451 /* No kernel lock - fine */
452 static unsigned int joydev_poll(struct file *file, poll_table *wait)
453 {
454         struct joydev_client *client = file->private_data;
455         struct joydev *joydev = client->joydev;
456
457         poll_wait(file, &joydev->wait, wait);
458         return (joydev_data_pending(client) ? (POLLIN | POLLRDNORM) : 0) |
459                 (joydev->exist ?  0 : (POLLHUP | POLLERR));
460 }
461
462 static int joydev_ioctl_common(struct joydev *joydev,
463                                 unsigned int cmd, void __user *argp)
464 {
465         struct input_dev *dev = joydev->handle.dev;
466         int i, j;
467
468         switch (cmd) {
469
470         case JS_SET_CAL:
471                 return copy_from_user(&joydev->glue.JS_CORR, argp,
472                                 sizeof(joydev->glue.JS_CORR)) ? -EFAULT : 0;
473
474         case JS_GET_CAL:
475                 return copy_to_user(argp, &joydev->glue.JS_CORR,
476                                 sizeof(joydev->glue.JS_CORR)) ? -EFAULT : 0;
477
478         case JS_SET_TIMEOUT:
479                 return get_user(joydev->glue.JS_TIMEOUT, (s32 __user *) argp);
480
481         case JS_GET_TIMEOUT:
482                 return put_user(joydev->glue.JS_TIMEOUT, (s32 __user *) argp);
483
484         case JSIOCGVERSION:
485                 return put_user(JS_VERSION, (__u32 __user *) argp);
486
487         case JSIOCGAXES:
488                 return put_user(joydev->nabs, (__u8 __user *) argp);
489
490         case JSIOCGBUTTONS:
491                 return put_user(joydev->nkey, (__u8 __user *) argp);
492
493         case JSIOCSCORR:
494                 if (copy_from_user(joydev->corr, argp,
495                               sizeof(joydev->corr[0]) * joydev->nabs))
496                     return -EFAULT;
497
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;
504
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))
552                                 return -EFAULT;
553                         return len;
554                 }
555         }
556         return -EINVAL;
557 }
558
559 #ifdef CONFIG_COMPAT
560 static long joydev_compat_ioctl(struct file *file,
561                                 unsigned int cmd, unsigned long arg)
562 {
563         struct joydev_client *client = file->private_data;
564         struct joydev *joydev = client->joydev;
565         void __user *argp = (void __user *)arg;
566         s32 tmp32;
567         struct JS_DATA_SAVE_TYPE_32 ds32;
568         int retval;
569
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) {
580
581         case JS_SET_TIMELIMIT:
582                 retval = get_user(tmp32, (s32 __user *) arg);
583                 if (retval == 0)
584                         joydev->glue.JS_TIMELIMIT = tmp32;
585                 break;
586
587         case JS_GET_TIMELIMIT:
588                 tmp32 = joydev->glue.JS_TIMELIMIT;
589                 retval = put_user(tmp32, (s32 __user *) arg);
590                 break;
591
592         case JS_SET_ALL:
593                 retval = copy_from_user(&ds32, argp,
594                                         sizeof(ds32)) ? -EFAULT : 0;
595                 if (retval == 0) {
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
613                 retval = copy_to_user(argp, &ds32, sizeof(ds32)) ? -EFAULT : 0;
614                 break;
615
616         default:
617                 retval = joydev_ioctl_common(joydev, cmd, argp);
618                 break;
619         }
620
621  out:
622         mutex_unlock(&joydev->mutex);
623         return retval;
624 }
625 #endif /* CONFIG_COMPAT */
626
627 static long joydev_ioctl(struct file *file,
628                          unsigned int cmd, unsigned long arg)
629 {
630         struct joydev_client *client = file->private_data;
631         struct joydev *joydev = client->joydev;
632         void __user *argp = (void __user *)arg;
633         int retval;
634
635         retval = mutex_lock_interruptible(&joydev->mutex);
636         if (retval)
637                 return retval;
638
639         if (!joydev->exist) {
640                 retval = -ENODEV;
641                 goto out;
642         }
643
644         switch (cmd) {
645
646         case JS_SET_TIMELIMIT:
647                 retval = get_user(joydev->glue.JS_TIMELIMIT,
648                                   (long __user *) arg);
649                 break;
650
651         case JS_GET_TIMELIMIT:
652                 retval = put_user(joydev->glue.JS_TIMELIMIT,
653                                   (long __user *) arg);
654                 break;
655
656         case JS_SET_ALL:
657                 retval = copy_from_user(&joydev->glue, argp,
658                                         sizeof(joydev->glue)) ? -EFAULT: 0;
659                 break;
660
661         case JS_GET_ALL:
662                 retval = copy_to_user(argp, &joydev->glue,
663                                       sizeof(joydev->glue)) ? -EFAULT : 0;
664                 break;
665
666         default:
667                 retval = joydev_ioctl_common(joydev, cmd, argp);
668                 break;
669         }
670  out:
671         mutex_unlock(&joydev->mutex);
672         return retval;
673 }
674
675 static const struct file_operations joydev_fops = {
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,
682 #ifdef CONFIG_COMPAT
683         .compat_ioctl   = joydev_compat_ioctl,
684 #endif
685         .fasync         = joydev_fasync,
686 };
687
688 static int joydev_install_chrdev(struct joydev *joydev)
689 {
690         joydev_table[joydev->minor] = joydev;
691         return 0;
692 }
693
694 static 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  */
706 static void joydev_mark_dead(struct joydev *joydev)
707 {
708         mutex_lock(&joydev->mutex);
709         joydev->exist = 0;
710         mutex_unlock(&joydev->mutex);
711 }
712
713 static 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
726 static int joydev_connect(struct input_handler *handler, struct input_dev *dev,
727                           const struct input_device_id *id)
728 {
729         struct joydev *joydev;
730         int i, j, t, minor;
731         int error;
732
733         for (minor = 0; minor < JOYDEV_MINORS; minor++)
734                 if (!joydev_table[minor])
735                         break;
736
737         if (minor == JOYDEV_MINORS) {
738                 printk(KERN_ERR "joydev: no more free joydev devices\n");
739                 return -ENFILE;
740         }
741
742         joydev = kzalloc(sizeof(struct joydev), GFP_KERNEL);
743         if (!joydev)
744                 return -ENOMEM;
745
746         INIT_LIST_HEAD(&joydev->client_list);
747         spin_lock_init(&joydev->client_lock);
748         mutex_init(&joydev->mutex);
749         init_waitqueue_head(&joydev->wait);
750
751         snprintf(joydev->name, sizeof(joydev->name), "js%d", minor);
752         joydev->exist = 1;
753         joydev->minor = minor;
754
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;
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
775         for (i = 0; i < BTN_JOYSTICK - BTN_MISC; i++)
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];
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                 }
804         }
805
806         strlcpy(joydev->dev.bus_id, joydev->name, sizeof(joydev->dev.bus_id));
807         joydev->dev.devt = MKDEV(INPUT_MAJOR, JOYDEV_MINOR_BASE + minor);
808         joydev->dev.class = &input_class;
809         joydev->dev.parent = &dev->dev;
810         joydev->dev.release = joydev_free;
811         device_initialize(&joydev->dev);
812
813         error = input_register_handle(&joydev->handle);
814         if (error)
815                 goto err_free_joydev;
816
817         error = joydev_install_chrdev(joydev);
818         if (error)
819                 goto err_unregister_handle;
820
821         error = device_add(&joydev->dev);
822         if (error)
823                 goto err_cleanup_joydev;
824
825         return 0;
826
827  err_cleanup_joydev:
828         joydev_cleanup(joydev);
829  err_unregister_handle:
830         input_unregister_handle(&joydev->handle);
831  err_free_joydev:
832         put_device(&joydev->dev);
833         return error;
834 }
835
836 static void joydev_disconnect(struct input_handle *handle)
837 {
838         struct joydev *joydev = handle->private;
839
840         device_del(&joydev->dev);
841         joydev_cleanup(joydev);
842         input_unregister_handle(handle);
843         put_device(&joydev->dev);
844 }
845
846 static const struct input_device_id joydev_blacklist[] = {
847         {
848                 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
849                                 INPUT_DEVICE_ID_MATCH_KEYBIT,
850                 .evbit = { BIT(EV_KEY) },
851                 .keybit = { [LONG(BTN_TOUCH)] = BIT(BTN_TOUCH) },
852         },      /* Avoid itouchpads, touchscreens and tablets */
853         { }     /* Terminating entry */
854 };
855
856 static const struct input_device_id joydev_ids[] = {
857         {
858                 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
859                                 INPUT_DEVICE_ID_MATCH_ABSBIT,
860                 .evbit = { BIT(EV_ABS) },
861                 .absbit = { BIT(ABS_X) },
862         },
863         {
864                 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
865                                 INPUT_DEVICE_ID_MATCH_ABSBIT,
866                 .evbit = { BIT(EV_ABS) },
867                 .absbit = { BIT(ABS_WHEEL) },
868         },
869         {
870                 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
871                                 INPUT_DEVICE_ID_MATCH_ABSBIT,
872                 .evbit = { BIT(EV_ABS) },
873                 .absbit = { BIT(ABS_THROTTLE) },
874         },
875         { }     /* Terminating entry */
876 };
877
878 MODULE_DEVICE_TABLE(input, joydev_ids);
879
880 static struct input_handler joydev_handler = {
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,
889 };
890
891 static int __init joydev_init(void)
892 {
893         return input_register_handler(&joydev_handler);
894 }
895
896 static void __exit joydev_exit(void)
897 {
898         input_unregister_handler(&joydev_handler);
899 }
900
901 module_init(joydev_init);
902 module_exit(joydev_exit);