13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17 #include <linux/errno.h>
18 #include <linux/joystick.h>
19 #include <linux/input.h>
20 #include <linux/kernel.h>
22 #include <linux/sched.h>
23 #include <linux/slab.h>
26 #include <linux/module.h>
27 #include <linux/poll.h>
29 #include <linux/device.h>
37 #define JOYDEV_MINOR_BASE 0
38 #define JOYDEV_MINORS 16
39 #define JOYDEV_BUFFER_SIZE 64
53 struct JS_DATA_SAVE_TYPE
glue;
74 static int joydev_correct(
int value,
struct js_corr *corr)
82 value = value > corr->
coef[0] ? (value < corr->
coef[1] ? 0 :
83 ((corr->
coef[3] * (value - corr->
coef[1])) >> 14)) :
84 ((corr->
coef[2] * (value - corr->
coef[0])) >> 14);
91 return value < -32767 ? -32767 : (value > 32767 ? 32767 :
value);
118 static void joydev_event(
struct input_handle *
handle,
119 unsigned int type,
unsigned int code,
int value)
121 struct joydev *joydev = handle->private;
138 event.value = joydev_correct(value,
142 joydev->
abs[
event.number] =
event.value;
153 joydev_pass_event(client, &event);
166 static void joydev_free(
struct device *
dev)
168 struct joydev *joydev =
container_of(dev,
struct joydev, dev);
170 input_put_device(joydev->
handle.dev);
174 static void joydev_attach_client(
struct joydev *joydev,
182 static void joydev_detach_client(
struct joydev *joydev,
186 list_del_rcu(&client->
node);
191 static int joydev_open_device(
struct joydev *joydev)
201 else if (!joydev->
open++) {
202 retval = input_open_device(&joydev->
handle);
211 static void joydev_close_device(
struct joydev *joydev)
216 input_close_device(&joydev->
handle);
225 static void joydev_hangup(
struct joydev *joydev)
232 spin_unlock(&joydev->client_lock);
240 struct joydev *joydev = client->
joydev;
242 joydev_detach_client(joydev, client);
245 joydev_close_device(joydev);
252 struct joydev *joydev =
263 joydev_attach_client(joydev, client);
265 error = joydev_open_device(joydev);
267 goto err_free_client;
275 joydev_detach_client(joydev, client);
280 static int joydev_generate_startup_event(
struct joydev_client *client,
281 struct input_dev *
input,
284 struct joydev *joydev = client->
joydev;
296 event->number = client->
startup;
302 event->value = joydev->
abs[
event->number];
312 static int joydev_fetch_next_event(
struct joydev_client *client,
319 have_event = client->
head != client->
tail;
334 struct input_dev *input,
337 struct joydev *joydev = client->
joydev;
341 spin_lock_irq(&input->event_lock);
346 for (
data.buttons = i = 0; i < 32 && i < joydev->nkey; i++)
349 data.x = (joydev->
abs[0] / 256 + 128) >> joydev->
glue.JS_CORR.x;
350 data.y = (joydev->
abs[1] / 256 + 128) >> joydev->
glue.JS_CORR.y;
360 spin_unlock_irq(&input->event_lock);
368 static inline int joydev_data_pending(
struct joydev_client *client)
370 struct joydev *joydev = client->
joydev;
376 static ssize_t joydev_read(
struct file *file,
char __user *buf,
377 size_t count, loff_t *ppos)
380 struct joydev *joydev = client->
joydev;
381 struct input_dev *input = joydev->
handle.dev;
388 if (count <
sizeof(
struct js_event))
392 return joydev_0x_read(client, input, buf);
398 !joydev->
exist || joydev_data_pending(client));
405 while (retval +
sizeof(
struct js_event) <= count &&
406 joydev_generate_startup_event(client, input, &event)) {
414 while (retval +
sizeof(
struct js_event) <= count &&
415 joydev_fetch_next_event(client, &event)) {
427 static unsigned int joydev_poll(
struct file *file,
poll_table *
wait)
430 struct joydev *joydev = client->
joydev;
432 poll_wait(file, &joydev->
wait, wait);
437 static int joydev_handle_JSIOCSAXMAP(
struct joydev *joydev,
438 void __user *
argp,
size_t len)
456 for (i = 0; i < joydev->
nabs; i++) {
465 for (i = 0; i < joydev->
nabs; i++)
473 static int joydev_handle_JSIOCSBTNMAP(
struct joydev *joydev,
474 void __user *argp,
size_t len)
492 for (i = 0; i < joydev->
nkey; i++) {
501 for (i = 0; i < joydev->
nkey; i++)
510 static int joydev_ioctl_common(
struct joydev *joydev,
511 unsigned int cmd,
void __user *argp)
513 struct input_dev *
dev = joydev->
handle.dev;
546 sizeof(joydev->
corr[0]) * joydev->
nabs))
549 for (i = 0; i < joydev->
nabs; i++) {
550 int val = input_abs_get_val(dev, joydev->
abspam[i]);
551 joydev->
abs[
i] = joydev_correct(val, &joydev->
corr[i]);
569 return joydev_handle_JSIOCSAXMAP(joydev, argp,
_IOC_SIZE(cmd));
576 return joydev_handle_JSIOCSBTNMAP(joydev, argp,
_IOC_SIZE(cmd));
595 static long joydev_compat_ioctl(
struct file *file,
596 unsigned int cmd,
unsigned long arg)
599 struct joydev *joydev = client->
joydev;
609 if (!joydev->
exist) {
619 joydev->
glue.JS_TIMELIMIT = tmp32;
623 tmp32 = joydev->
glue.JS_TIMELIMIT;
629 sizeof(ds32)) ? -
EFAULT : 0;
631 joydev->
glue.JS_TIMEOUT = ds32.JS_TIMEOUT;
632 joydev->
glue.BUSY = ds32.BUSY;
633 joydev->
glue.JS_EXPIRETIME = ds32.JS_EXPIRETIME;
634 joydev->
glue.JS_TIMELIMIT = ds32.JS_TIMELIMIT;
635 joydev->
glue.JS_SAVE = ds32.JS_SAVE;
636 joydev->
glue.JS_CORR = ds32.JS_CORR;
641 ds32.JS_TIMEOUT = joydev->
glue.JS_TIMEOUT;
642 ds32.BUSY = joydev->
glue.BUSY;
643 ds32.JS_EXPIRETIME = joydev->
glue.JS_EXPIRETIME;
644 ds32.JS_TIMELIMIT = joydev->
glue.JS_TIMELIMIT;
645 ds32.JS_SAVE = joydev->
glue.JS_SAVE;
646 ds32.JS_CORR = joydev->
glue.JS_CORR;
652 retval = joydev_ioctl_common(joydev, cmd, argp);
662 static long joydev_ioctl(
struct file *file,
663 unsigned int cmd,
unsigned long arg)
666 struct joydev *joydev = client->
joydev;
674 if (!joydev->
exist) {
683 (
long __user *) arg);
688 (
long __user *) arg);
702 retval = joydev_ioctl_common(joydev, cmd, argp);
715 .release = joydev_release,
716 .unlocked_ioctl = joydev_ioctl,
718 .compat_ioctl = joydev_compat_ioctl,
720 .fasync = joydev_fasync,
729 static void joydev_mark_dead(
struct joydev *joydev)
732 joydev->
exist =
false;
736 static void joydev_cleanup(
struct joydev *joydev)
738 struct input_handle *handle = &joydev->
handle;
740 joydev_mark_dead(joydev);
741 joydev_hangup(joydev);
747 input_close_device(handle);
751 static bool joydev_match(
struct input_handler *handler,
struct input_dev *dev)
764 static int joydev_connect(
struct input_handler *handler,
struct input_dev *dev,
767 struct joydev *joydev;
774 pr_err(
"failed to reserve new minor: %d\n", error);
778 joydev = kzalloc(
sizeof(
struct joydev),
GFP_KERNEL);
788 joydev->
exist =
true;
796 joydev->
handle.dev = input_get_device(dev);
797 joydev->
handle.name = dev_name(&joydev->
dev);
798 joydev->
handle.handler = handler;
799 joydev->
handle.private = joydev;
809 if (
test_bit(i + BTN_MISC, dev->keybit)) {
816 if (
test_bit(i + BTN_MISC, dev->keybit)) {
822 for (i = 0; i < joydev->
nabs; i++) {
824 if (input_abs_get_max(dev, j) == input_abs_get_min(dev, j)) {
826 joydev->
abs[
i] = input_abs_get_val(dev, j);
830 joydev->
corr[
i].prec = input_abs_get_fuzz(dev, j);
832 t = (input_abs_get_max(dev, j) + input_abs_get_min(dev, j)) / 2;
833 joydev->
corr[
i].coef[0] = t - input_abs_get_flat(dev, j);
834 joydev->
corr[
i].coef[1] = t + input_abs_get_flat(dev, j);
836 t = (input_abs_get_max(dev, j) - input_abs_get_min(dev, j)) / 2
837 - 2 * input_abs_get_flat(dev, j);
839 joydev->
corr[
i].coef[2] = (1 << 29) / t;
840 joydev->
corr[
i].coef[3] = (1 << 29) / t;
843 joydev_correct(input_abs_get_val(dev, j),
849 joydev->
dev.class = &input_class;
850 joydev->
dev.parent = &dev->dev;
851 joydev->
dev.release = joydev_free;
854 error = input_register_handle(&joydev->
handle);
856 goto err_free_joydev;
859 joydev->
cdev.kobj.parent = &joydev->
dev.kobj;
862 goto err_unregister_handle;
866 goto err_cleanup_joydev;
871 joydev_cleanup(joydev);
872 err_unregister_handle:
873 input_unregister_handle(&joydev->
handle);
877 input_free_minor(minor);
881 static void joydev_disconnect(
struct input_handle *handle)
883 struct joydev *joydev = handle->private;
886 joydev_cleanup(joydev);
887 input_free_minor(
MINOR(joydev->
dev.devt));
888 input_unregister_handle(handle);
934 static struct input_handler joydev_handler = {
935 .event = joydev_event,
936 .match = joydev_match,
937 .connect = joydev_connect,
938 .disconnect = joydev_disconnect,
939 .legacy_minors =
true,
942 .id_table = joydev_ids,
945 static int __init joydev_init(
void)
947 return input_register_handler(&joydev_handler);
950 static void __exit joydev_exit(
void)
952 input_unregister_handler(&joydev_handler);