Commit | Line | Data |
---|---|---|
dcb69dd0 SP |
1 | /* |
2 | * Apple Motion Sensor driver (joystick emulation) | |
3 | * | |
4 | * Copyright (C) 2005 Stelian Pop (stelian@popies.net) | |
5 | * Copyright (C) 2006 Michael Hanselmann (linux-kernel@hansmi.ch) | |
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 <linux/module.h> | |
14 | ||
15 | #include <linux/types.h> | |
16 | #include <linux/errno.h> | |
17 | #include <linux/init.h> | |
18 | #include <linux/delay.h> | |
19 | ||
20 | #include "ams.h" | |
21 | ||
90ab5ee9 | 22 | static bool joystick; |
0a020022 | 23 | module_param(joystick, bool, S_IRUGO); |
dcb69dd0 SP |
24 | MODULE_PARM_DESC(joystick, "Enable the input class device on module load"); |
25 | ||
90ab5ee9 | 26 | static bool invert; |
0a020022 | 27 | module_param(invert, bool, S_IWUSR | S_IRUGO); |
dcb69dd0 SP |
28 | MODULE_PARM_DESC(invert, "Invert input data on X and Y axis"); |
29 | ||
ee4cd32e DT |
30 | static DEFINE_MUTEX(ams_input_mutex); |
31 | ||
3fdbc340 | 32 | static void ams_idev_poll(struct input_polled_dev *dev) |
dcb69dd0 | 33 | { |
3fdbc340 | 34 | struct input_dev *idev = dev->input; |
dcb69dd0 SP |
35 | s8 x, y, z; |
36 | ||
3fdbc340 | 37 | mutex_lock(&ams_info.lock); |
dcb69dd0 | 38 | |
3fdbc340 | 39 | ams_sensors(&x, &y, &z); |
dcb69dd0 | 40 | |
3fdbc340 DT |
41 | x -= ams_info.xcalib; |
42 | y -= ams_info.ycalib; | |
43 | z -= ams_info.zcalib; | |
dcb69dd0 | 44 | |
3fdbc340 DT |
45 | input_report_abs(idev, ABS_X, invert ? -x : x); |
46 | input_report_abs(idev, ABS_Y, invert ? -y : y); | |
47 | input_report_abs(idev, ABS_Z, z); | |
dcb69dd0 | 48 | |
3fdbc340 | 49 | input_sync(idev); |
dcb69dd0 | 50 | |
3fdbc340 | 51 | mutex_unlock(&ams_info.lock); |
dcb69dd0 SP |
52 | } |
53 | ||
54 | /* Call with ams_info.lock held! */ | |
ee4cd32e | 55 | static int ams_input_enable(void) |
dcb69dd0 | 56 | { |
3fdbc340 | 57 | struct input_dev *input; |
dcb69dd0 | 58 | s8 x, y, z; |
ee4cd32e | 59 | int error; |
dcb69dd0 SP |
60 | |
61 | ams_sensors(&x, &y, &z); | |
62 | ams_info.xcalib = x; | |
63 | ams_info.ycalib = y; | |
64 | ams_info.zcalib = z; | |
65 | ||
3fdbc340 | 66 | ams_info.idev = input_allocate_polled_device(); |
dcb69dd0 | 67 | if (!ams_info.idev) |
ee4cd32e | 68 | return -ENOMEM; |
dcb69dd0 | 69 | |
3fdbc340 DT |
70 | ams_info.idev->poll = ams_idev_poll; |
71 | ams_info.idev->poll_interval = 25; | |
72 | ||
73 | input = ams_info.idev->input; | |
74 | input->name = "Apple Motion Sensor"; | |
75 | input->id.bustype = ams_info.bustype; | |
76 | input->id.vendor = 0; | |
77 | input->dev.parent = &ams_info.of_dev->dev; | |
dcb69dd0 | 78 | |
3fdbc340 DT |
79 | input_set_abs_params(input, ABS_X, -50, 50, 3, 0); |
80 | input_set_abs_params(input, ABS_Y, -50, 50, 3, 0); | |
81 | input_set_abs_params(input, ABS_Z, -50, 50, 3, 0); | |
dcb69dd0 | 82 | |
3fdbc340 DT |
83 | set_bit(EV_ABS, input->evbit); |
84 | set_bit(EV_KEY, input->evbit); | |
85 | set_bit(BTN_TOUCH, input->keybit); | |
dcb69dd0 | 86 | |
ee4cd32e DT |
87 | error = input_register_polled_device(ams_info.idev); |
88 | if (error) { | |
3fdbc340 | 89 | input_free_polled_device(ams_info.idev); |
dcb69dd0 | 90 | ams_info.idev = NULL; |
ee4cd32e | 91 | return error; |
dcb69dd0 | 92 | } |
ee4cd32e DT |
93 | |
94 | joystick = 1; | |
95 | ||
96 | return 0; | |
dcb69dd0 SP |
97 | } |
98 | ||
dcb69dd0 SP |
99 | static void ams_input_disable(void) |
100 | { | |
101 | if (ams_info.idev) { | |
3fdbc340 DT |
102 | input_unregister_polled_device(ams_info.idev); |
103 | input_free_polled_device(ams_info.idev); | |
dcb69dd0 SP |
104 | ams_info.idev = NULL; |
105 | } | |
ee4cd32e DT |
106 | |
107 | joystick = 0; | |
dcb69dd0 SP |
108 | } |
109 | ||
110 | static ssize_t ams_input_show_joystick(struct device *dev, | |
111 | struct device_attribute *attr, char *buf) | |
112 | { | |
113 | return sprintf(buf, "%d\n", joystick); | |
114 | } | |
115 | ||
116 | static ssize_t ams_input_store_joystick(struct device *dev, | |
117 | struct device_attribute *attr, const char *buf, size_t count) | |
118 | { | |
ee4cd32e DT |
119 | unsigned long enable; |
120 | int error = 0; | |
7033b64b | 121 | int ret; |
ee4cd32e | 122 | |
7033b64b JH |
123 | ret = kstrtoul(buf, 0, &enable); |
124 | if (ret) | |
125 | return ret; | |
126 | if (enable > 1) | |
dcb69dd0 SP |
127 | return -EINVAL; |
128 | ||
ee4cd32e | 129 | mutex_lock(&ams_input_mutex); |
dcb69dd0 | 130 | |
ee4cd32e DT |
131 | if (enable != joystick) { |
132 | if (enable) | |
133 | error = ams_input_enable(); | |
134 | else | |
135 | ams_input_disable(); | |
136 | } | |
dcb69dd0 | 137 | |
ee4cd32e | 138 | mutex_unlock(&ams_input_mutex); |
dcb69dd0 | 139 | |
ee4cd32e | 140 | return error ? error : count; |
dcb69dd0 SP |
141 | } |
142 | ||
143 | static DEVICE_ATTR(joystick, S_IRUGO | S_IWUSR, | |
144 | ams_input_show_joystick, ams_input_store_joystick); | |
145 | ||
dcb69dd0 SP |
146 | int ams_input_init(void) |
147 | { | |
ee4cd32e | 148 | if (joystick) |
dcb69dd0 | 149 | ams_input_enable(); |
ee4cd32e DT |
150 | |
151 | return device_create_file(&ams_info.of_dev->dev, &dev_attr_joystick); | |
dcb69dd0 SP |
152 | } |
153 | ||
d7bde2fe | 154 | void ams_input_exit(void) |
dcb69dd0 | 155 | { |
dcb69dd0 | 156 | device_remove_file(&ams_info.of_dev->dev, &dev_attr_joystick); |
ee4cd32e DT |
157 | |
158 | mutex_lock(&ams_input_mutex); | |
159 | ams_input_disable(); | |
160 | mutex_unlock(&ams_input_mutex); | |
dcb69dd0 | 161 | } |