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.
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>
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>
29 #include <linux/devfs_fs_kernel.h>
31 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
32 MODULE_DESCRIPTION("Joystick device interfaces");
33 MODULE_SUPPORTED_DEVICE("input/js");
34 MODULE_LICENSE("GPL");
36 #define JOYDEV_MINOR_BASE 0
37 #define JOYDEV_MINORS 16
38 #define JOYDEV_BUFFER_SIZE 64
40 #define MSECS(t) (1000 * ((t) / HZ) + 1000 * ((t) % HZ) / HZ)
47 struct input_handle handle
;
48 wait_queue_head_t wait
;
49 struct list_head list
;
50 struct js_corr corr
[ABS_MAX
];
51 struct JS_DATA_SAVE_TYPE glue
;
54 __u16 keymap
[KEY_MAX
- BTN_MISC
];
55 __u16 keypam
[KEY_MAX
- BTN_MISC
];
62 struct js_event buffer
[JOYDEV_BUFFER_SIZE
];
66 struct fasync_struct
*fasync
;
67 struct joydev
*joydev
;
68 struct list_head node
;
71 static struct joydev
*joydev_table
[JOYDEV_MINORS
];
73 static int joydev_correct(int value
, struct js_corr
*corr
)
79 value
= value
> corr
->coef
[0] ? (value
< corr
->coef
[1] ? 0 :
80 ((corr
->coef
[3] * (value
- corr
->coef
[1])) >> 14)) :
81 ((corr
->coef
[2] * (value
- corr
->coef
[0])) >> 14);
87 if (value
< -32767) return -32767;
88 if (value
> 32767) return 32767;
93 static void joydev_event(struct input_handle
*handle
, unsigned int type
, unsigned int code
, int value
)
95 struct joydev
*joydev
= handle
->private;
96 struct joydev_list
*list
;
97 struct js_event event
;
102 if (code
< BTN_MISC
|| value
== 2) return;
103 event
.type
= JS_EVENT_BUTTON
;
104 event
.number
= joydev
->keymap
[code
- BTN_MISC
];
109 event
.type
= JS_EVENT_AXIS
;
110 event
.number
= joydev
->absmap
[code
];
111 event
.value
= joydev_correct(value
, joydev
->corr
+ event
.number
);
112 if (event
.value
== joydev
->abs
[event
.number
]) return;
113 joydev
->abs
[event
.number
] = event
.value
;
120 event
.time
= MSECS(jiffies
);
122 list_for_each_entry(list
, &joydev
->list
, node
) {
124 memcpy(list
->buffer
+ list
->head
, &event
, sizeof(struct js_event
));
126 if (list
->startup
== joydev
->nabs
+ joydev
->nkey
)
127 if (list
->tail
== (list
->head
= (list
->head
+ 1) & (JOYDEV_BUFFER_SIZE
- 1)))
130 kill_fasync(&list
->fasync
, SIGIO
, POLL_IN
);
133 wake_up_interruptible(&joydev
->wait
);
136 static int joydev_fasync(int fd
, struct file
*file
, int on
)
139 struct joydev_list
*list
= file
->private_data
;
140 retval
= fasync_helper(fd
, file
, on
, &list
->fasync
);
141 return retval
< 0 ? retval
: 0;
144 static void joydev_free(struct joydev
*joydev
)
146 devfs_remove("input/js%d", joydev
->minor
);
147 joydev_table
[joydev
->minor
] = NULL
;
148 class_simple_device_remove(MKDEV(INPUT_MAJOR
, JOYDEV_MINOR_BASE
+ joydev
->minor
));
152 static int joydev_release(struct inode
* inode
, struct file
* file
)
154 struct joydev_list
*list
= file
->private_data
;
156 joydev_fasync(-1, file
, 0);
158 list_del(&list
->node
);
160 if (!--list
->joydev
->open
) {
161 if (list
->joydev
->exist
)
162 input_close_device(&list
->joydev
->handle
);
164 joydev_free(list
->joydev
);
171 static int joydev_open(struct inode
*inode
, struct file
*file
)
173 struct joydev_list
*list
;
174 int i
= iminor(inode
) - JOYDEV_MINOR_BASE
;
176 if (i
>= JOYDEV_MINORS
|| !joydev_table
[i
])
179 if (!(list
= kmalloc(sizeof(struct joydev_list
), GFP_KERNEL
)))
181 memset(list
, 0, sizeof(struct joydev_list
));
183 list
->joydev
= joydev_table
[i
];
184 list_add_tail(&list
->node
, &joydev_table
[i
]->list
);
185 file
->private_data
= list
;
187 if (!list
->joydev
->open
++)
188 if (list
->joydev
->exist
)
189 input_open_device(&list
->joydev
->handle
);
194 static ssize_t
joydev_write(struct file
* file
, const char __user
* buffer
, size_t count
, loff_t
*ppos
)
199 static ssize_t
joydev_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
201 struct joydev_list
*list
= file
->private_data
;
202 struct joydev
*joydev
= list
->joydev
;
203 struct input_dev
*input
= joydev
->handle
.dev
;
206 if (!list
->joydev
->exist
)
209 if (count
< sizeof(struct js_event
))
212 if (count
== sizeof(struct JS_DATA_TYPE
)) {
214 struct JS_DATA_TYPE data
;
217 for (data
.buttons
= i
= 0; i
< 32 && i
< joydev
->nkey
; i
++)
218 data
.buttons
|= test_bit(joydev
->keypam
[i
], input
->key
) ? (1 << i
) : 0;
219 data
.x
= (joydev
->abs
[0] / 256 + 128) >> joydev
->glue
.JS_CORR
.x
;
220 data
.y
= (joydev
->abs
[1] / 256 + 128) >> joydev
->glue
.JS_CORR
.y
;
222 if (copy_to_user(buf
, &data
, sizeof(struct JS_DATA_TYPE
)))
226 list
->tail
= list
->head
;
228 return sizeof(struct JS_DATA_TYPE
);
231 if (list
->startup
== joydev
->nabs
+ joydev
->nkey
232 && list
->head
== list
->tail
&& (file
->f_flags
& O_NONBLOCK
))
235 retval
= wait_event_interruptible(list
->joydev
->wait
,
236 !list
->joydev
->exist
||
237 list
->startup
< joydev
->nabs
+ joydev
->nkey
||
238 list
->head
!= list
->tail
);
243 if (!list
->joydev
->exist
)
246 while (list
->startup
< joydev
->nabs
+ joydev
->nkey
&& retval
+ sizeof(struct js_event
) <= count
) {
248 struct js_event event
;
250 event
.time
= MSECS(jiffies
);
252 if (list
->startup
< joydev
->nkey
) {
253 event
.type
= JS_EVENT_BUTTON
| JS_EVENT_INIT
;
254 event
.number
= list
->startup
;
255 event
.value
= !!test_bit(joydev
->keypam
[event
.number
], input
->key
);
257 event
.type
= JS_EVENT_AXIS
| JS_EVENT_INIT
;
258 event
.number
= list
->startup
- joydev
->nkey
;
259 event
.value
= joydev
->abs
[event
.number
];
262 if (copy_to_user(buf
+ retval
, &event
, sizeof(struct js_event
)))
266 retval
+= sizeof(struct js_event
);
269 while (list
->head
!= list
->tail
&& retval
+ sizeof(struct js_event
) <= count
) {
271 if (copy_to_user(buf
+ retval
, list
->buffer
+ list
->tail
, sizeof(struct js_event
)))
274 list
->tail
= (list
->tail
+ 1) & (JOYDEV_BUFFER_SIZE
- 1);
275 retval
+= sizeof(struct js_event
);
281 /* No kernel lock - fine */
282 static unsigned int joydev_poll(struct file
*file
, poll_table
*wait
)
284 struct joydev_list
*list
= file
->private_data
;
285 poll_wait(file
, &list
->joydev
->wait
, wait
);
286 if (list
->head
!= list
->tail
|| list
->startup
< list
->joydev
->nabs
+ list
->joydev
->nkey
)
287 return POLLIN
| POLLRDNORM
;
291 static int joydev_ioctl(struct inode
*inode
, struct file
*file
, unsigned int cmd
, unsigned long arg
)
293 struct joydev_list
*list
= file
->private_data
;
294 struct joydev
*joydev
= list
->joydev
;
295 struct input_dev
*dev
= joydev
->handle
.dev
;
296 void __user
*argp
= (void __user
*)arg
;
299 if (!joydev
->exist
) return -ENODEV
;
304 return copy_from_user(&joydev
->glue
.JS_CORR
, argp
,
305 sizeof(struct JS_DATA_TYPE
)) ? -EFAULT
: 0;
307 return copy_to_user(argp
, &joydev
->glue
.JS_CORR
,
308 sizeof(struct JS_DATA_TYPE
)) ? -EFAULT
: 0;
310 return get_user(joydev
->glue
.JS_TIMEOUT
, (int __user
*) arg
);
312 return put_user(joydev
->glue
.JS_TIMEOUT
, (int __user
*) arg
);
313 case JS_SET_TIMELIMIT
:
314 return get_user(joydev
->glue
.JS_TIMELIMIT
, (long __user
*) arg
);
315 case JS_GET_TIMELIMIT
:
316 return put_user(joydev
->glue
.JS_TIMELIMIT
, (long __user
*) arg
);
318 return copy_from_user(&joydev
->glue
, argp
,
319 sizeof(struct JS_DATA_SAVE_TYPE
)) ? -EFAULT
: 0;
321 return copy_to_user(argp
, &joydev
->glue
,
322 sizeof(struct JS_DATA_SAVE_TYPE
)) ? -EFAULT
: 0;
325 return put_user(JS_VERSION
, (__u32 __user
*) arg
);
327 return put_user(joydev
->nabs
, (__u8 __user
*) arg
);
329 return put_user(joydev
->nkey
, (__u8 __user
*) arg
);
331 if (copy_from_user(joydev
->corr
, argp
,
332 sizeof(struct js_corr
) * joydev
->nabs
))
334 for (i
= 0; i
< joydev
->nabs
; i
++) {
335 j
= joydev
->abspam
[i
];
336 joydev
->abs
[i
] = joydev_correct(dev
->abs
[j
], joydev
->corr
+ i
);
340 return copy_to_user(argp
, joydev
->corr
,
341 sizeof(struct js_corr
) * joydev
->nabs
) ? -EFAULT
: 0;
343 if (copy_from_user(joydev
->abspam
, argp
, sizeof(__u8
) * ABS_MAX
))
345 for (i
= 0; i
< joydev
->nabs
; i
++) {
346 if (joydev
->abspam
[i
] > ABS_MAX
) return -EINVAL
;
347 joydev
->absmap
[joydev
->abspam
[i
]] = i
;
351 return copy_to_user(argp
, joydev
->abspam
,
352 sizeof(__u8
) * ABS_MAX
) ? -EFAULT
: 0;
354 if (copy_from_user(joydev
->keypam
, argp
, sizeof(__u16
) * (KEY_MAX
- BTN_MISC
)))
356 for (i
= 0; i
< joydev
->nkey
; i
++) {
357 if (joydev
->keypam
[i
] > KEY_MAX
|| joydev
->keypam
[i
] < BTN_MISC
) return -EINVAL
;
358 joydev
->keymap
[joydev
->keypam
[i
] - BTN_MISC
] = i
;
362 return copy_to_user(argp
, joydev
->keypam
,
363 sizeof(__u16
) * (KEY_MAX
- BTN_MISC
)) ? -EFAULT
: 0;
365 if ((cmd
& ~(_IOC_SIZEMASK
<< _IOC_SIZESHIFT
)) == JSIOCGNAME(0)) {
367 if (!dev
->name
) return 0;
368 len
= strlen(dev
->name
) + 1;
369 if (len
> _IOC_SIZE(cmd
)) len
= _IOC_SIZE(cmd
);
370 if (copy_to_user(argp
, dev
->name
, len
)) return -EFAULT
;
377 static struct file_operations joydev_fops
= {
378 .owner
= THIS_MODULE
,
380 .write
= joydev_write
,
383 .release
= joydev_release
,
384 .ioctl
= joydev_ioctl
,
385 .fasync
= joydev_fasync
,
388 static struct input_handle
*joydev_connect(struct input_handler
*handler
, struct input_dev
*dev
, struct input_device_id
*id
)
390 struct joydev
*joydev
;
393 for (minor
= 0; minor
< JOYDEV_MINORS
&& joydev_table
[minor
]; minor
++);
394 if (minor
== JOYDEV_MINORS
) {
395 printk(KERN_ERR
"joydev: no more free joydev devices\n");
399 if (!(joydev
= kmalloc(sizeof(struct joydev
), GFP_KERNEL
)))
401 memset(joydev
, 0, sizeof(struct joydev
));
403 INIT_LIST_HEAD(&joydev
->list
);
404 init_waitqueue_head(&joydev
->wait
);
406 joydev
->minor
= minor
;
408 joydev
->handle
.dev
= dev
;
409 joydev
->handle
.name
= joydev
->name
;
410 joydev
->handle
.handler
= handler
;
411 joydev
->handle
.private = joydev
;
412 sprintf(joydev
->name
, "js%d", minor
);
414 for (i
= 0; i
< ABS_MAX
; i
++)
415 if (test_bit(i
, dev
->absbit
)) {
416 joydev
->absmap
[i
] = joydev
->nabs
;
417 joydev
->abspam
[joydev
->nabs
] = i
;
421 for (i
= BTN_JOYSTICK
- BTN_MISC
; i
< KEY_MAX
- BTN_MISC
; i
++)
422 if (test_bit(i
+ BTN_MISC
, dev
->keybit
)) {
423 joydev
->keymap
[i
] = joydev
->nkey
;
424 joydev
->keypam
[joydev
->nkey
] = i
+ BTN_MISC
;
428 for (i
= 0; i
< BTN_JOYSTICK
- BTN_MISC
; i
++)
429 if (test_bit(i
+ BTN_MISC
, dev
->keybit
)) {
430 joydev
->keymap
[i
] = joydev
->nkey
;
431 joydev
->keypam
[joydev
->nkey
] = i
+ BTN_MISC
;
435 for (i
= 0; i
< joydev
->nabs
; i
++) {
436 j
= joydev
->abspam
[i
];
437 if (dev
->absmax
[j
] == dev
->absmin
[j
]) {
438 joydev
->corr
[i
].type
= JS_CORR_NONE
;
439 joydev
->abs
[i
] = dev
->abs
[j
];
442 joydev
->corr
[i
].type
= JS_CORR_BROKEN
;
443 joydev
->corr
[i
].prec
= dev
->absfuzz
[j
];
444 joydev
->corr
[i
].coef
[0] = (dev
->absmax
[j
] + dev
->absmin
[j
]) / 2 - dev
->absflat
[j
];
445 joydev
->corr
[i
].coef
[1] = (dev
->absmax
[j
] + dev
->absmin
[j
]) / 2 + dev
->absflat
[j
];
446 if (!(t
= ((dev
->absmax
[j
] - dev
->absmin
[j
]) / 2 - 2 * dev
->absflat
[j
])))
448 joydev
->corr
[i
].coef
[2] = (1 << 29) / t
;
449 joydev
->corr
[i
].coef
[3] = (1 << 29) / t
;
451 joydev
->abs
[i
] = joydev_correct(dev
->abs
[j
], joydev
->corr
+ i
);
454 joydev_table
[minor
] = joydev
;
456 devfs_mk_cdev(MKDEV(INPUT_MAJOR
, JOYDEV_MINOR_BASE
+ minor
),
457 S_IFCHR
|S_IRUGO
|S_IWUSR
, "input/js%d", minor
);
458 class_simple_device_add(input_class
,
459 MKDEV(INPUT_MAJOR
, JOYDEV_MINOR_BASE
+ minor
),
460 dev
->dev
, "js%d", minor
);
462 return &joydev
->handle
;
465 static void joydev_disconnect(struct input_handle
*handle
)
467 struct joydev
*joydev
= handle
->private;
472 input_close_device(handle
);
477 static struct input_device_id joydev_blacklist
[] = {
479 .flags
= INPUT_DEVICE_ID_MATCH_EVBIT
| INPUT_DEVICE_ID_MATCH_KEYBIT
,
480 .evbit
= { BIT(EV_KEY
) },
481 .keybit
= { [LONG(BTN_TOUCH
)] = BIT(BTN_TOUCH
) },
482 }, /* Avoid itouchpads, touchscreens and tablets */
483 { }, /* Terminating entry */
486 static struct input_device_id joydev_ids
[] = {
488 .flags
= INPUT_DEVICE_ID_MATCH_EVBIT
| INPUT_DEVICE_ID_MATCH_ABSBIT
,
489 .evbit
= { BIT(EV_ABS
) },
490 .absbit
= { BIT(ABS_X
) },
493 .flags
= INPUT_DEVICE_ID_MATCH_EVBIT
| INPUT_DEVICE_ID_MATCH_ABSBIT
,
494 .evbit
= { BIT(EV_ABS
) },
495 .absbit
= { BIT(ABS_WHEEL
) },
498 .flags
= INPUT_DEVICE_ID_MATCH_EVBIT
| INPUT_DEVICE_ID_MATCH_ABSBIT
,
499 .evbit
= { BIT(EV_ABS
) },
500 .absbit
= { BIT(ABS_THROTTLE
) },
502 { }, /* Terminating entry */
505 MODULE_DEVICE_TABLE(input
, joydev_ids
);
507 static struct input_handler joydev_handler
= {
508 .event
= joydev_event
,
509 .connect
= joydev_connect
,
510 .disconnect
= joydev_disconnect
,
511 .fops
= &joydev_fops
,
512 .minor
= JOYDEV_MINOR_BASE
,
514 .id_table
= joydev_ids
,
515 .blacklist
= joydev_blacklist
,
518 static int __init
joydev_init(void)
520 input_register_handler(&joydev_handler
);
524 static void __exit
joydev_exit(void)
526 input_unregister_handler(&joydev_handler
);
529 module_init(joydev_init
);
530 module_exit(joydev_exit
);