1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Apple Motion Sensor driver (joystick emulation)
5 * Copyright (C) 2005 Stelian Pop (stelian@popies.net)
6 * Copyright (C) 2006 Michael Hanselmann (linux-kernel@hansmi.ch)
9 #include <linux/module.h>
11 #include <linux/types.h>
12 #include <linux/errno.h>
13 #include <linux/init.h>
14 #include <linux/delay.h>
19 module_param(joystick
, bool, S_IRUGO
);
20 MODULE_PARM_DESC(joystick
, "Enable the input class device on module load");
23 module_param(invert
, bool, S_IWUSR
| S_IRUGO
);
24 MODULE_PARM_DESC(invert
, "Invert input data on X and Y axis");
26 static DEFINE_MUTEX(ams_input_mutex
);
28 static void ams_idev_poll(struct input_dev
*idev
)
32 mutex_lock(&ams_info
.lock
);
34 ams_sensors(&x
, &y
, &z
);
40 input_report_abs(idev
, ABS_X
, invert
? -x
: x
);
41 input_report_abs(idev
, ABS_Y
, invert
? -y
: y
);
42 input_report_abs(idev
, ABS_Z
, z
);
46 mutex_unlock(&ams_info
.lock
);
49 /* Call with ams_info.lock held! */
50 static int ams_input_enable(void)
52 struct input_dev
*input
;
56 ams_sensors(&x
, &y
, &z
);
61 input
= input_allocate_device();
65 input
->name
= "Apple Motion Sensor";
66 input
->id
.bustype
= ams_info
.bustype
;
68 input
->dev
.parent
= &ams_info
.of_dev
->dev
;
70 input_set_abs_params(input
, ABS_X
, -50, 50, 3, 0);
71 input_set_abs_params(input
, ABS_Y
, -50, 50, 3, 0);
72 input_set_abs_params(input
, ABS_Z
, -50, 50, 3, 0);
73 input_set_capability(input
, EV_KEY
, BTN_TOUCH
);
75 error
= input_setup_polling(input
, ams_idev_poll
);
79 input_set_poll_interval(input
, 25);
81 error
= input_register_device(input
);
85 ams_info
.idev
= input
;
91 input_free_device(input
);
95 static void ams_input_disable(void)
98 input_unregister_device(ams_info
.idev
);
105 static ssize_t
ams_input_show_joystick(struct device
*dev
,
106 struct device_attribute
*attr
, char *buf
)
108 return sprintf(buf
, "%d\n", joystick
);
111 static ssize_t
ams_input_store_joystick(struct device
*dev
,
112 struct device_attribute
*attr
, const char *buf
, size_t count
)
114 unsigned long enable
;
118 ret
= kstrtoul(buf
, 0, &enable
);
124 mutex_lock(&ams_input_mutex
);
126 if (enable
!= joystick
) {
128 error
= ams_input_enable();
133 mutex_unlock(&ams_input_mutex
);
135 return error
? error
: count
;
138 static DEVICE_ATTR(joystick
, S_IRUGO
| S_IWUSR
,
139 ams_input_show_joystick
, ams_input_store_joystick
);
141 int ams_input_init(void)
146 return device_create_file(&ams_info
.of_dev
->dev
, &dev_attr_joystick
);
149 void ams_input_exit(void)
151 device_remove_file(&ams_info
.of_dev
->dev
, &dev_attr_joystick
);
153 mutex_lock(&ams_input_mutex
);
155 mutex_unlock(&ams_input_mutex
);