2 * Apple Motion Sensor driver (joystick emulation)
4 * Copyright (C) 2005 Stelian Pop (stelian@popies.net)
5 * Copyright (C) 2006 Michael Hanselmann (linux-kernel@hansmi.ch)
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.
13 #include <linux/module.h>
15 #include <linux/types.h>
16 #include <linux/errno.h>
17 #include <linux/init.h>
18 #include <linux/delay.h>
22 static unsigned int joystick;
23 module_param(joystick, bool, 0644);
24 MODULE_PARM_DESC(joystick, "Enable the input class device on module load");
26 static unsigned int invert;
27 module_param(invert, bool, 0644);
28 MODULE_PARM_DESC(invert, "Invert input data on X and Y axis");
30 static int ams_input_kthread(void *data)
34 while (!kthread_should_stop()) {
35 mutex_lock(&ams_info.lock);
37 ams_sensors(&x, &y, &z);
43 input_report_abs(ams_info.idev, ABS_X, invert ? -x : x);
44 input_report_abs(ams_info.idev, ABS_Y, invert ? -y : y);
45 input_report_abs(ams_info.idev, ABS_Z, z);
47 input_sync(ams_info.idev);
49 mutex_unlock(&ams_info.lock);
57 static int ams_input_open(struct input_dev *dev)
59 ams_info.kthread = kthread_run(ams_input_kthread, NULL, "kams");
60 return IS_ERR(ams_info.kthread) ? PTR_ERR(ams_info.kthread) : 0;
63 static void ams_input_close(struct input_dev *dev)
65 kthread_stop(ams_info.kthread);
68 /* Call with ams_info.lock held! */
69 static void ams_input_enable(void)
76 ams_sensors(&x, &y, &z);
81 ams_info.idev = input_allocate_device();
85 ams_info.idev->name = "Apple Motion Sensor";
86 ams_info.idev->id.bustype = ams_info.bustype;
87 ams_info.idev->id.vendor = 0;
88 ams_info.idev->open = ams_input_open;
89 ams_info.idev->close = ams_input_close;
90 ams_info.idev->cdev.dev = &ams_info.of_dev->dev;
92 input_set_abs_params(ams_info.idev, ABS_X, -50, 50, 3, 0);
93 input_set_abs_params(ams_info.idev, ABS_Y, -50, 50, 3, 0);
94 input_set_abs_params(ams_info.idev, ABS_Z, -50, 50, 3, 0);
96 set_bit(EV_ABS, ams_info.idev->evbit);
97 set_bit(EV_KEY, ams_info.idev->evbit);
98 set_bit(BTN_TOUCH, ams_info.idev->keybit);
100 if (input_register_device(ams_info.idev)) {
101 input_free_device(ams_info.idev);
102 ams_info.idev = NULL;
107 /* Call with ams_info.lock held! */
108 static void ams_input_disable(void)
111 input_unregister_device(ams_info.idev);
112 ams_info.idev = NULL;
116 static ssize_t ams_input_show_joystick(struct device *dev,
117 struct device_attribute *attr, char *buf)
119 return sprintf(buf, "%d\n", joystick);
122 static ssize_t ams_input_store_joystick(struct device *dev,
123 struct device_attribute *attr, const char *buf, size_t count)
125 if (sscanf(buf, "%d\n", &joystick) != 1)
128 mutex_lock(&ams_info.lock);
135 mutex_unlock(&ams_info.lock);
140 static DEVICE_ATTR(joystick, S_IRUGO | S_IWUSR,
141 ams_input_show_joystick, ams_input_store_joystick);
143 /* Call with ams_info.lock held! */
144 int ams_input_init(void)
148 result = device_create_file(&ams_info.of_dev->dev, &dev_attr_joystick);
150 if (!result && joystick)
155 /* Call with ams_info.lock held! */
156 void ams_input_exit()
159 device_remove_file(&ams_info.of_dev->dev, &dev_attr_joystick);