blob: 9f3529ad3fdab5b149cf7c835fc32e413e6c5109 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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/smp_lock.h>
28#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
31MODULE_DESCRIPTION("Joystick device interfaces");
32MODULE_SUPPORTED_DEVICE("input/js");
33MODULE_LICENSE("GPL");
34
35#define JOYDEV_MINOR_BASE 0
36#define JOYDEV_MINORS 16
37#define JOYDEV_BUFFER_SIZE 64
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039struct joydev {
40 int exist;
41 int open;
42 int minor;
43 char name[16];
44 struct input_handle handle;
45 wait_queue_head_t wait;
46 struct list_head list;
47 struct js_corr corr[ABS_MAX + 1];
48 struct JS_DATA_SAVE_TYPE glue;
49 int nabs;
50 int nkey;
51 __u16 keymap[KEY_MAX - BTN_MISC + 1];
52 __u16 keypam[KEY_MAX - BTN_MISC + 1];
53 __u8 absmap[ABS_MAX + 1];
54 __u8 abspam[ABS_MAX + 1];
55 __s16 abs[ABS_MAX + 1];
56};
57
58struct joydev_list {
59 struct js_event buffer[JOYDEV_BUFFER_SIZE];
60 int head;
61 int tail;
62 int startup;
63 struct fasync_struct *fasync;
64 struct joydev *joydev;
65 struct list_head node;
66};
67
68static struct joydev *joydev_table[JOYDEV_MINORS];
69
70static int joydev_correct(int value, struct js_corr *corr)
71{
72 switch (corr->type) {
73 case JS_CORR_NONE:
74 break;
75 case JS_CORR_BROKEN:
76 value = value > corr->coef[0] ? (value < corr->coef[1] ? 0 :
77 ((corr->coef[3] * (value - corr->coef[1])) >> 14)) :
78 ((corr->coef[2] * (value - corr->coef[0])) >> 14);
79 break;
80 default:
81 return 0;
82 }
83
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -040084 return value < -32767 ? -32767 : (value > 32767 ? 32767 : value);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
87static void joydev_event(struct input_handle *handle, unsigned int type, unsigned int code, int value)
88{
89 struct joydev *joydev = handle->private;
90 struct joydev_list *list;
91 struct js_event event;
92
93 switch (type) {
94
95 case EV_KEY:
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -040096 if (code < BTN_MISC || value == 2)
97 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 event.type = JS_EVENT_BUTTON;
99 event.number = joydev->keymap[code - BTN_MISC];
100 event.value = value;
101 break;
102
103 case EV_ABS:
104 event.type = JS_EVENT_AXIS;
105 event.number = joydev->absmap[code];
106 event.value = joydev_correct(value, joydev->corr + event.number);
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400107 if (event.value == joydev->abs[event.number])
108 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 joydev->abs[event.number] = event.value;
110 break;
111
112 default:
113 return;
114 }
115
Tobias Klauser6f5eacf2005-07-11 01:08:56 -0500116 event.time = jiffies_to_msecs(jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118 list_for_each_entry(list, &joydev->list, node) {
119
120 memcpy(list->buffer + list->head, &event, sizeof(struct js_event));
121
122 if (list->startup == joydev->nabs + joydev->nkey)
123 if (list->tail == (list->head = (list->head + 1) & (JOYDEV_BUFFER_SIZE - 1)))
124 list->startup = 0;
125
126 kill_fasync(&list->fasync, SIGIO, POLL_IN);
127 }
128
129 wake_up_interruptible(&joydev->wait);
130}
131
132static int joydev_fasync(int fd, struct file *file, int on)
133{
134 int retval;
135 struct joydev_list *list = file->private_data;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 retval = fasync_helper(fd, file, on, &list->fasync);
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 return retval < 0 ? retval : 0;
140}
141
142static void joydev_free(struct joydev *joydev)
143{
144 joydev_table[joydev->minor] = NULL;
145 kfree(joydev);
146}
147
148static int joydev_release(struct inode * inode, struct file * file)
149{
150 struct joydev_list *list = file->private_data;
151
152 joydev_fasync(-1, file, 0);
153
154 list_del(&list->node);
155
156 if (!--list->joydev->open) {
157 if (list->joydev->exist)
158 input_close_device(&list->joydev->handle);
159 else
160 joydev_free(list->joydev);
161 }
162
163 kfree(list);
164 return 0;
165}
166
167static int joydev_open(struct inode *inode, struct file *file)
168{
169 struct joydev_list *list;
170 int i = iminor(inode) - JOYDEV_MINOR_BASE;
171
172 if (i >= JOYDEV_MINORS || !joydev_table[i])
173 return -ENODEV;
174
Eric Sesterhennb39787a2006-03-14 00:09:16 -0500175 if (!(list = kzalloc(sizeof(struct joydev_list), GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178 list->joydev = joydev_table[i];
179 list_add_tail(&list->node, &joydev_table[i]->list);
180 file->private_data = list;
181
182 if (!list->joydev->open++)
183 if (list->joydev->exist)
184 input_open_device(&list->joydev->handle);
185
186 return 0;
187}
188
189static ssize_t joydev_write(struct file * file, const char __user * buffer, size_t count, loff_t *ppos)
190{
191 return -EINVAL;
192}
193
194static ssize_t joydev_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
195{
196 struct joydev_list *list = file->private_data;
197 struct joydev *joydev = list->joydev;
198 struct input_dev *input = joydev->handle.dev;
199 int retval = 0;
200
201 if (!list->joydev->exist)
202 return -ENODEV;
203
204 if (count < sizeof(struct js_event))
205 return -EINVAL;
206
207 if (count == sizeof(struct JS_DATA_TYPE)) {
208
209 struct JS_DATA_TYPE data;
210 int i;
211
212 for (data.buttons = i = 0; i < 32 && i < joydev->nkey; i++)
213 data.buttons |= test_bit(joydev->keypam[i], input->key) ? (1 << i) : 0;
214 data.x = (joydev->abs[0] / 256 + 128) >> joydev->glue.JS_CORR.x;
215 data.y = (joydev->abs[1] / 256 + 128) >> joydev->glue.JS_CORR.y;
216
217 if (copy_to_user(buf, &data, sizeof(struct JS_DATA_TYPE)))
218 return -EFAULT;
219
220 list->startup = 0;
221 list->tail = list->head;
222
223 return sizeof(struct JS_DATA_TYPE);
224 }
225
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400226 if (list->startup == joydev->nabs + joydev->nkey &&
227 list->head == list->tail && (file->f_flags & O_NONBLOCK))
228 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 retval = wait_event_interruptible(list->joydev->wait,
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400231 !list->joydev->exist ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 list->startup < joydev->nabs + joydev->nkey ||
233 list->head != list->tail);
234
235 if (retval)
236 return retval;
237
238 if (!list->joydev->exist)
239 return -ENODEV;
240
241 while (list->startup < joydev->nabs + joydev->nkey && retval + sizeof(struct js_event) <= count) {
242
243 struct js_event event;
244
Tobias Klauser6f5eacf2005-07-11 01:08:56 -0500245 event.time = jiffies_to_msecs(jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247 if (list->startup < joydev->nkey) {
248 event.type = JS_EVENT_BUTTON | JS_EVENT_INIT;
249 event.number = list->startup;
250 event.value = !!test_bit(joydev->keypam[event.number], input->key);
251 } else {
252 event.type = JS_EVENT_AXIS | JS_EVENT_INIT;
253 event.number = list->startup - joydev->nkey;
254 event.value = joydev->abs[event.number];
255 }
256
257 if (copy_to_user(buf + retval, &event, sizeof(struct js_event)))
258 return -EFAULT;
259
260 list->startup++;
261 retval += sizeof(struct js_event);
262 }
263
264 while (list->head != list->tail && retval + sizeof(struct js_event) <= count) {
265
266 if (copy_to_user(buf + retval, list->buffer + list->tail, sizeof(struct js_event)))
267 return -EFAULT;
268
269 list->tail = (list->tail + 1) & (JOYDEV_BUFFER_SIZE - 1);
270 retval += sizeof(struct js_event);
271 }
272
273 return retval;
274}
275
276/* No kernel lock - fine */
277static unsigned int joydev_poll(struct file *file, poll_table *wait)
278{
279 struct joydev_list *list = file->private_data;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 poll_wait(file, &list->joydev->wait, wait);
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400282 return ((list->head != list->tail || list->startup < list->joydev->nabs + list->joydev->nkey) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 (POLLIN | POLLRDNORM) : 0) | (list->joydev->exist ? 0 : (POLLHUP | POLLERR));
284}
285
Jeremy Fitzhardinge024ac442005-05-29 02:26:31 -0500286static int joydev_ioctl_common(struct joydev *joydev, unsigned int cmd, void __user *argp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 struct input_dev *dev = joydev->handle.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 int i, j;
290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 switch (cmd) {
292
293 case JS_SET_CAL:
294 return copy_from_user(&joydev->glue.JS_CORR, argp,
Jeremy Fitzhardinge024ac442005-05-29 02:26:31 -0500295 sizeof(joydev->glue.JS_CORR)) ? -EFAULT : 0;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 case JS_GET_CAL:
298 return copy_to_user(argp, &joydev->glue.JS_CORR,
Jeremy Fitzhardinge024ac442005-05-29 02:26:31 -0500299 sizeof(joydev->glue.JS_CORR)) ? -EFAULT : 0;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 case JS_SET_TIMEOUT:
Jeremy Fitzhardinge024ac442005-05-29 02:26:31 -0500302 return get_user(joydev->glue.JS_TIMEOUT, (s32 __user *) argp);
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 case JS_GET_TIMEOUT:
Jeremy Fitzhardinge024ac442005-05-29 02:26:31 -0500305 return put_user(joydev->glue.JS_TIMEOUT, (s32 __user *) argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307 case JSIOCGVERSION:
Jeremy Fitzhardinge024ac442005-05-29 02:26:31 -0500308 return put_user(JS_VERSION, (__u32 __user *) argp);
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 case JSIOCGAXES:
Jeremy Fitzhardinge024ac442005-05-29 02:26:31 -0500311 return put_user(joydev->nabs, (__u8 __user *) argp);
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 case JSIOCGBUTTONS:
Jeremy Fitzhardinge024ac442005-05-29 02:26:31 -0500314 return put_user(joydev->nkey, (__u8 __user *) argp);
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 case JSIOCSCORR:
317 if (copy_from_user(joydev->corr, argp,
Jeremy Fitzhardinge024ac442005-05-29 02:26:31 -0500318 sizeof(joydev->corr[0]) * joydev->nabs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 return -EFAULT;
320 for (i = 0; i < joydev->nabs; i++) {
321 j = joydev->abspam[i];
322 joydev->abs[i] = joydev_correct(dev->abs[j], joydev->corr + i);
323 }
324 return 0;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 case JSIOCGCORR:
327 return copy_to_user(argp, joydev->corr,
Jeremy Fitzhardinge024ac442005-05-29 02:26:31 -0500328 sizeof(joydev->corr[0]) * joydev->nabs) ? -EFAULT : 0;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 case JSIOCSAXMAP:
331 if (copy_from_user(joydev->abspam, argp, sizeof(__u8) * (ABS_MAX + 1)))
332 return -EFAULT;
333 for (i = 0; i < joydev->nabs; i++) {
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400334 if (joydev->abspam[i] > ABS_MAX)
335 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 joydev->absmap[joydev->abspam[i]] = i;
337 }
338 return 0;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 case JSIOCGAXMAP:
341 return copy_to_user(argp, joydev->abspam,
342 sizeof(__u8) * (ABS_MAX + 1)) ? -EFAULT : 0;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 case JSIOCSBTNMAP:
345 if (copy_from_user(joydev->keypam, argp, sizeof(__u16) * (KEY_MAX - BTN_MISC + 1)))
346 return -EFAULT;
347 for (i = 0; i < joydev->nkey; i++) {
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400348 if (joydev->keypam[i] > KEY_MAX || joydev->keypam[i] < BTN_MISC)
349 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 joydev->keymap[joydev->keypam[i] - BTN_MISC] = i;
351 }
352 return 0;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 case JSIOCGBTNMAP:
355 return copy_to_user(argp, joydev->keypam,
356 sizeof(__u16) * (KEY_MAX - BTN_MISC + 1)) ? -EFAULT : 0;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 default:
359 if ((cmd & ~(_IOC_SIZEMASK << _IOC_SIZESHIFT)) == JSIOCGNAME(0)) {
360 int len;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400361 if (!dev->name)
362 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 len = strlen(dev->name) + 1;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400364 if (len > _IOC_SIZE(cmd))
365 len = _IOC_SIZE(cmd);
366 if (copy_to_user(argp, dev->name, len))
367 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return len;
369 }
370 }
371 return -EINVAL;
372}
373
Jeremy Fitzhardinge024ac442005-05-29 02:26:31 -0500374#ifdef CONFIG_COMPAT
375static long joydev_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
376{
377 struct joydev_list *list = file->private_data;
378 struct joydev *joydev = list->joydev;
379 void __user *argp = (void __user *)arg;
380 s32 tmp32;
381 struct JS_DATA_SAVE_TYPE_32 ds32;
382 int err;
383
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400384 if (!joydev->exist)
385 return -ENODEV;
386
Jeremy Fitzhardinge024ac442005-05-29 02:26:31 -0500387 switch(cmd) {
388 case JS_SET_TIMELIMIT:
389 err = get_user(tmp32, (s32 __user *) arg);
390 if (err == 0)
391 joydev->glue.JS_TIMELIMIT = tmp32;
392 break;
393 case JS_GET_TIMELIMIT:
394 tmp32 = joydev->glue.JS_TIMELIMIT;
395 err = put_user(tmp32, (s32 __user *) arg);
396 break;
397
398 case JS_SET_ALL:
399 err = copy_from_user(&ds32, argp,
400 sizeof(ds32)) ? -EFAULT : 0;
401 if (err == 0) {
402 joydev->glue.JS_TIMEOUT = ds32.JS_TIMEOUT;
403 joydev->glue.BUSY = ds32.BUSY;
404 joydev->glue.JS_EXPIRETIME = ds32.JS_EXPIRETIME;
405 joydev->glue.JS_TIMELIMIT = ds32.JS_TIMELIMIT;
406 joydev->glue.JS_SAVE = ds32.JS_SAVE;
407 joydev->glue.JS_CORR = ds32.JS_CORR;
408 }
409 break;
410
411 case JS_GET_ALL:
412 ds32.JS_TIMEOUT = joydev->glue.JS_TIMEOUT;
413 ds32.BUSY = joydev->glue.BUSY;
414 ds32.JS_EXPIRETIME = joydev->glue.JS_EXPIRETIME;
415 ds32.JS_TIMELIMIT = joydev->glue.JS_TIMELIMIT;
416 ds32.JS_SAVE = joydev->glue.JS_SAVE;
417 ds32.JS_CORR = joydev->glue.JS_CORR;
418
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400419 err = copy_to_user(argp, &ds32, sizeof(ds32)) ? -EFAULT : 0;
Jeremy Fitzhardinge024ac442005-05-29 02:26:31 -0500420 break;
421
422 default:
423 err = joydev_ioctl_common(joydev, cmd, argp);
424 }
425 return err;
426}
427#endif /* CONFIG_COMPAT */
428
429static int joydev_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
430{
431 struct joydev_list *list = file->private_data;
432 struct joydev *joydev = list->joydev;
433 void __user *argp = (void __user *)arg;
434
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400435 if (!joydev->exist)
436 return -ENODEV;
Jeremy Fitzhardinge024ac442005-05-29 02:26:31 -0500437
438 switch(cmd) {
439 case JS_SET_TIMELIMIT:
440 return get_user(joydev->glue.JS_TIMELIMIT, (long __user *) arg);
441 case JS_GET_TIMELIMIT:
442 return put_user(joydev->glue.JS_TIMELIMIT, (long __user *) arg);
443 case JS_SET_ALL:
444 return copy_from_user(&joydev->glue, argp,
445 sizeof(joydev->glue)) ? -EFAULT : 0;
446 case JS_GET_ALL:
447 return copy_to_user(argp, &joydev->glue,
448 sizeof(joydev->glue)) ? -EFAULT : 0;
449 default:
450 return joydev_ioctl_common(joydev, cmd, argp);
451 }
452}
453
Dmitry Torokhov66e66112006-09-14 01:31:59 -0400454static const struct file_operations joydev_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 .owner = THIS_MODULE,
456 .read = joydev_read,
457 .write = joydev_write,
458 .poll = joydev_poll,
459 .open = joydev_open,
460 .release = joydev_release,
461 .ioctl = joydev_ioctl,
Jeremy Fitzhardinge024ac442005-05-29 02:26:31 -0500462#ifdef CONFIG_COMPAT
463 .compat_ioctl = joydev_compat_ioctl,
464#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 .fasync = joydev_fasync,
466};
467
Dmitry Torokhov66e66112006-09-14 01:31:59 -0400468static struct input_handle *joydev_connect(struct input_handler *handler, struct input_dev *dev,
469 const struct input_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
471 struct joydev *joydev;
Greg Kroah-Hartmanc9bcd582005-10-27 22:25:43 -0700472 struct class_device *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 int i, j, t, minor;
474
475 for (minor = 0; minor < JOYDEV_MINORS && joydev_table[minor]; minor++);
476 if (minor == JOYDEV_MINORS) {
477 printk(KERN_ERR "joydev: no more free joydev devices\n");
478 return NULL;
479 }
480
Eric Sesterhennb39787a2006-03-14 00:09:16 -0500481 if (!(joydev = kzalloc(sizeof(struct joydev), GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484 INIT_LIST_HEAD(&joydev->list);
485 init_waitqueue_head(&joydev->wait);
486
487 joydev->minor = minor;
488 joydev->exist = 1;
489 joydev->handle.dev = dev;
490 joydev->handle.name = joydev->name;
491 joydev->handle.handler = handler;
492 joydev->handle.private = joydev;
493 sprintf(joydev->name, "js%d", minor);
494
495 for (i = 0; i < ABS_MAX + 1; i++)
496 if (test_bit(i, dev->absbit)) {
497 joydev->absmap[i] = joydev->nabs;
498 joydev->abspam[joydev->nabs] = i;
499 joydev->nabs++;
500 }
501
502 for (i = BTN_JOYSTICK - BTN_MISC; i < KEY_MAX - BTN_MISC + 1; i++)
503 if (test_bit(i + BTN_MISC, dev->keybit)) {
504 joydev->keymap[i] = joydev->nkey;
505 joydev->keypam[joydev->nkey] = i + BTN_MISC;
506 joydev->nkey++;
507 }
508
Vojtech Pavlikf24949e2005-05-28 02:11:49 -0500509 for (i = 0; i < BTN_JOYSTICK - BTN_MISC; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 if (test_bit(i + BTN_MISC, dev->keybit)) {
511 joydev->keymap[i] = joydev->nkey;
512 joydev->keypam[joydev->nkey] = i + BTN_MISC;
513 joydev->nkey++;
514 }
515
516 for (i = 0; i < joydev->nabs; i++) {
517 j = joydev->abspam[i];
518 if (dev->absmax[j] == dev->absmin[j]) {
519 joydev->corr[i].type = JS_CORR_NONE;
520 joydev->abs[i] = dev->abs[j];
521 continue;
522 }
523 joydev->corr[i].type = JS_CORR_BROKEN;
524 joydev->corr[i].prec = dev->absfuzz[j];
525 joydev->corr[i].coef[0] = (dev->absmax[j] + dev->absmin[j]) / 2 - dev->absflat[j];
526 joydev->corr[i].coef[1] = (dev->absmax[j] + dev->absmin[j]) / 2 + dev->absflat[j];
527 if (!(t = ((dev->absmax[j] - dev->absmin[j]) / 2 - 2 * dev->absflat[j])))
528 continue;
529 joydev->corr[i].coef[2] = (1 << 29) / t;
530 joydev->corr[i].coef[3] = (1 << 29) / t;
531
532 joydev->abs[i] = joydev_correct(dev->abs[j], joydev->corr + i);
533 }
534
535 joydev_table[minor] = joydev;
536
Greg Kroah-Hartmanc9bcd582005-10-27 22:25:43 -0700537 cdev = class_device_create(&input_class, &dev->cdev,
gregkh@suse.de12356862005-03-15 14:26:30 -0800538 MKDEV(INPUT_MAJOR, JOYDEV_MINOR_BASE + minor),
Greg Kroah-Hartmanc9bcd582005-10-27 22:25:43 -0700539 dev->cdev.dev, joydev->name);
540
541 /* temporary symlink to keep userspace happy */
542 sysfs_create_link(&input_class.subsys.kset.kobj, &cdev->kobj,
543 joydev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 return &joydev->handle;
546}
547
548static void joydev_disconnect(struct input_handle *handle)
549{
550 struct joydev *joydev = handle->private;
551 struct joydev_list *list;
552
Greg Kroah-Hartmanc9bcd582005-10-27 22:25:43 -0700553 sysfs_remove_link(&input_class.subsys.kset.kobj, joydev->name);
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -0700554 class_device_destroy(&input_class, MKDEV(INPUT_MAJOR, JOYDEV_MINOR_BASE + joydev->minor));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 joydev->exist = 0;
556
557 if (joydev->open) {
558 input_close_device(handle);
559 wake_up_interruptible(&joydev->wait);
560 list_for_each_entry(list, &joydev->list, node)
561 kill_fasync(&list->fasync, SIGIO, POLL_HUP);
562 } else
563 joydev_free(joydev);
564}
565
Dmitry Torokhov66e66112006-09-14 01:31:59 -0400566static const struct input_device_id joydev_blacklist[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 {
568 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
569 .evbit = { BIT(EV_KEY) },
570 .keybit = { [LONG(BTN_TOUCH)] = BIT(BTN_TOUCH) },
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400571 }, /* Avoid itouchpads, touchscreens and tablets */
572 { } /* Terminating entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573};
574
Dmitry Torokhov66e66112006-09-14 01:31:59 -0400575static const struct input_device_id joydev_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 {
577 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_ABSBIT,
578 .evbit = { BIT(EV_ABS) },
579 .absbit = { BIT(ABS_X) },
580 },
581 {
582 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_ABSBIT,
583 .evbit = { BIT(EV_ABS) },
584 .absbit = { BIT(ABS_WHEEL) },
585 },
586 {
587 .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_ABSBIT,
588 .evbit = { BIT(EV_ABS) },
589 .absbit = { BIT(ABS_THROTTLE) },
590 },
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400591 { } /* Terminating entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592};
593
594MODULE_DEVICE_TABLE(input, joydev_ids);
595
596static struct input_handler joydev_handler = {
597 .event = joydev_event,
598 .connect = joydev_connect,
599 .disconnect = joydev_disconnect,
600 .fops = &joydev_fops,
601 .minor = JOYDEV_MINOR_BASE,
602 .name = "joydev",
603 .id_table = joydev_ids,
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400604 .blacklist = joydev_blacklist,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605};
606
607static int __init joydev_init(void)
608{
Dmitry Torokhov4263cf02006-09-14 01:32:39 -0400609 return input_register_handler(&joydev_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610}
611
612static void __exit joydev_exit(void)
613{
614 input_unregister_handler(&joydev_handler);
615}
616
617module_init(joydev_init);
618module_exit(joydev_exit);