aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/hid/hid-magicmouse.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/hid/hid-magicmouse.c')
-rw-r--r--drivers/hid/hid-magicmouse.c96
1 files changed, 73 insertions, 23 deletions
diff --git a/drivers/hid/hid-magicmouse.c b/drivers/hid/hid-magicmouse.c
index f10d56a15f2..319b0e57ee4 100644
--- a/drivers/hid/hid-magicmouse.c
+++ b/drivers/hid/hid-magicmouse.c
@@ -30,6 +30,21 @@ static bool emulate_scroll_wheel = true;
30module_param(emulate_scroll_wheel, bool, 0644); 30module_param(emulate_scroll_wheel, bool, 0644);
31MODULE_PARM_DESC(emulate_scroll_wheel, "Emulate a scroll wheel"); 31MODULE_PARM_DESC(emulate_scroll_wheel, "Emulate a scroll wheel");
32 32
33static unsigned int scroll_speed = 32;
34static int param_set_scroll_speed(const char *val, struct kernel_param *kp) {
35 unsigned long speed;
36 if (!val || strict_strtoul(val, 0, &speed) || speed > 63)
37 return -EINVAL;
38 scroll_speed = speed;
39 return 0;
40}
41module_param_call(scroll_speed, param_set_scroll_speed, param_get_uint, &scroll_speed, 0644);
42MODULE_PARM_DESC(scroll_speed, "Scroll speed, value from 0 (slow) to 63 (fast)");
43
44static bool scroll_acceleration = false;
45module_param(scroll_acceleration, bool, 0644);
46MODULE_PARM_DESC(scroll_acceleration, "Accelerate sequential scroll events");
47
33static bool report_touches = true; 48static bool report_touches = true;
34module_param(report_touches, bool, 0644); 49module_param(report_touches, bool, 0644);
35MODULE_PARM_DESC(report_touches, "Emit touch records (otherwise, only use them for emulation)"); 50MODULE_PARM_DESC(report_touches, "Emit touch records (otherwise, only use them for emulation)");
@@ -50,6 +65,8 @@ MODULE_PARM_DESC(report_undeciphered, "Report undeciphered multi-touch state fie
50#define TOUCH_STATE_START 0x30 65#define TOUCH_STATE_START 0x30
51#define TOUCH_STATE_DRAG 0x40 66#define TOUCH_STATE_DRAG 0x40
52 67
68#define SCROLL_ACCEL_DEFAULT 7
69
53/** 70/**
54 * struct magicmouse_sc - Tracks Magic Mouse-specific data. 71 * struct magicmouse_sc - Tracks Magic Mouse-specific data.
55 * @input: Input device through which we report events. 72 * @input: Input device through which we report events.
@@ -78,8 +95,10 @@ struct magicmouse_sc {
78 struct { 95 struct {
79 short x; 96 short x;
80 short y; 97 short y;
98 short scroll_x;
81 short scroll_y; 99 short scroll_y;
82 u8 size; 100 u8 size;
101 u8 down;
83 } touches[16]; 102 } touches[16];
84 int tracking_ids[16]; 103 int tracking_ids[16];
85}; 104};
@@ -141,7 +160,7 @@ static void magicmouse_emit_buttons(struct magicmouse_sc *msc, int state)
141 input_report_key(msc->input, BTN_RIGHT, state & 2); 160 input_report_key(msc->input, BTN_RIGHT, state & 2);
142 161
143 if (state != last_state) 162 if (state != last_state)
144 msc->scroll_accel = 0; 163 msc->scroll_accel = SCROLL_ACCEL_DEFAULT;
145} 164}
146 165
147static void magicmouse_emit_touch(struct magicmouse_sc *msc, int raw_id, u8 *tdata) 166static void magicmouse_emit_touch(struct magicmouse_sc *msc, int raw_id, u8 *tdata)
@@ -152,6 +171,7 @@ static void magicmouse_emit_touch(struct magicmouse_sc *msc, int raw_id, u8 *tda
152 int id = (misc >> 6) & 15; 171 int id = (misc >> 6) & 15;
153 int x = x_y << 12 >> 20; 172 int x = x_y << 12 >> 20;
154 int y = -(x_y >> 20); 173 int y = -(x_y >> 20);
174 int down = (tdata[7] & TOUCH_STATE_MASK) != TOUCH_STATE_NONE;
155 175
156 /* Store tracking ID and other fields. */ 176 /* Store tracking ID and other fields. */
157 msc->tracking_ids[raw_id] = id; 177 msc->tracking_ids[raw_id] = id;
@@ -160,42 +180,54 @@ static void magicmouse_emit_touch(struct magicmouse_sc *msc, int raw_id, u8 *tda
160 msc->touches[id].size = misc & 63; 180 msc->touches[id].size = misc & 63;
161 181
162 /* If requested, emulate a scroll wheel by detecting small 182 /* If requested, emulate a scroll wheel by detecting small
163 * vertical touch motions along the middle of the mouse. 183 * vertical touch motions.
164 */ 184 */
165 if (emulate_scroll_wheel && 185 if (emulate_scroll_wheel) {
166 middle_button_start < x && x < middle_button_stop) {
167 static const int accel_profile[] = {
168 256, 228, 192, 160, 128, 96, 64, 32,
169 };
170 unsigned long now = jiffies; 186 unsigned long now = jiffies;
171 int step = msc->touches[id].scroll_y - y; 187 int step_x = msc->touches[id].scroll_x - x;
172 188 int step_y = msc->touches[id].scroll_y - y;
173 /* Reset acceleration after half a second. */
174 if (time_after(now, msc->scroll_jiffies + HZ / 2))
175 msc->scroll_accel = 0;
176 189
177 /* Calculate and apply the scroll motion. */ 190 /* Calculate and apply the scroll motion. */
178 switch (tdata[7] & TOUCH_STATE_MASK) { 191 switch (tdata[7] & TOUCH_STATE_MASK) {
179 case TOUCH_STATE_START: 192 case TOUCH_STATE_START:
193 msc->touches[id].scroll_x = x;
180 msc->touches[id].scroll_y = y; 194 msc->touches[id].scroll_y = y;
181 msc->scroll_accel = min_t(int, msc->scroll_accel + 1, 195
182 ARRAY_SIZE(accel_profile) - 1); 196 /* Reset acceleration after half a second. */
197 if (scroll_acceleration && time_before(now,
198 msc->scroll_jiffies + HZ / 2))
199 msc->scroll_accel = max_t(int,
200 msc->scroll_accel - 1, 1);
201 else
202 msc->scroll_accel = SCROLL_ACCEL_DEFAULT;
203
183 break; 204 break;
184 case TOUCH_STATE_DRAG: 205 case TOUCH_STATE_DRAG:
185 step = step / accel_profile[msc->scroll_accel]; 206 step_x /= (64 - (int)scroll_speed) * msc->scroll_accel;
186 if (step != 0) { 207 if (step_x != 0) {
187 msc->touches[id].scroll_y = y; 208 msc->touches[id].scroll_x -= step_x *
209 (64 - scroll_speed) * msc->scroll_accel;
188 msc->scroll_jiffies = now; 210 msc->scroll_jiffies = now;
189 input_report_rel(input, REL_WHEEL, step); 211 input_report_rel(input, REL_HWHEEL, -step_x);
212 }
213
214 step_y /= (64 - (int)scroll_speed) * msc->scroll_accel;
215 if (step_y != 0) {
216 msc->touches[id].scroll_y -= step_y *
217 (64 - scroll_speed) * msc->scroll_accel;
218 msc->scroll_jiffies = now;
219 input_report_rel(input, REL_WHEEL, step_y);
190 } 220 }
191 break; 221 break;
192 } 222 }
193 } 223 }
194 224
195 /* Generate the input events for this touch. */ 225 /* Generate the input events for this touch. */
196 if (report_touches) { 226 if (report_touches && down) {
197 int orientation = (misc >> 10) - 32; 227 int orientation = (misc >> 10) - 32;
198 228
229 msc->touches[id].down = 1;
230
199 input_report_abs(input, ABS_MT_TRACKING_ID, id); 231 input_report_abs(input, ABS_MT_TRACKING_ID, id);
200 input_report_abs(input, ABS_MT_TOUCH_MAJOR, tdata[3]); 232 input_report_abs(input, ABS_MT_TOUCH_MAJOR, tdata[3]);
201 input_report_abs(input, ABS_MT_TOUCH_MINOR, tdata[4]); 233 input_report_abs(input, ABS_MT_TOUCH_MINOR, tdata[4]);
@@ -215,7 +247,7 @@ static int magicmouse_raw_event(struct hid_device *hdev,
215{ 247{
216 struct magicmouse_sc *msc = hid_get_drvdata(hdev); 248 struct magicmouse_sc *msc = hid_get_drvdata(hdev);
217 struct input_dev *input = msc->input; 249 struct input_dev *input = msc->input;
218 int x, y, ts, ii, clicks; 250 int x, y, ts, ii, clicks, last_up;
219 251
220 switch (data[0]) { 252 switch (data[0]) {
221 case 0x10: 253 case 0x10:
@@ -235,12 +267,26 @@ static int magicmouse_raw_event(struct hid_device *hdev,
235 msc->ntouches = (size - 6) / 8; 267 msc->ntouches = (size - 6) / 8;
236 for (ii = 0; ii < msc->ntouches; ii++) 268 for (ii = 0; ii < msc->ntouches; ii++)
237 magicmouse_emit_touch(msc, ii, data + ii * 8 + 6); 269 magicmouse_emit_touch(msc, ii, data + ii * 8 + 6);
270
271 if (report_touches) {
272 last_up = 1;
273 for (ii = 0; ii < ARRAY_SIZE(msc->touches); ii++) {
274 if (msc->touches[ii].down) {
275 last_up = 0;
276 msc->touches[ii].down = 0;
277 }
278 }
279 if (last_up) {
280 input_mt_sync(input);
281 }
282 }
283
238 /* When emulating three-button mode, it is important 284 /* When emulating three-button mode, it is important
239 * to have the current touch information before 285 * to have the current touch information before
240 * generating a click event. 286 * generating a click event.
241 */ 287 */
242 x = (signed char)data[1]; 288 x = (int)(((data[3] & 0x0c) << 28) | (data[1] << 22)) >> 22;
243 y = (signed char)data[2]; 289 y = (int)(((data[3] & 0x30) << 26) | (data[2] << 22)) >> 22;
244 clicks = data[3]; 290 clicks = data[3];
245 break; 291 break;
246 case 0x20: /* Theoretically battery status (0-100), but I have 292 case 0x20: /* Theoretically battery status (0-100), but I have
@@ -301,8 +347,10 @@ static void magicmouse_setup_input(struct input_dev *input, struct hid_device *h
301 __set_bit(EV_REL, input->evbit); 347 __set_bit(EV_REL, input->evbit);
302 __set_bit(REL_X, input->relbit); 348 __set_bit(REL_X, input->relbit);
303 __set_bit(REL_Y, input->relbit); 349 __set_bit(REL_Y, input->relbit);
304 if (emulate_scroll_wheel) 350 if (emulate_scroll_wheel) {
305 __set_bit(REL_WHEEL, input->relbit); 351 __set_bit(REL_WHEEL, input->relbit);
352 __set_bit(REL_HWHEEL, input->relbit);
353 }
306 354
307 if (report_touches) { 355 if (report_touches) {
308 __set_bit(EV_ABS, input->evbit); 356 __set_bit(EV_ABS, input->evbit);
@@ -345,6 +393,8 @@ static int magicmouse_probe(struct hid_device *hdev,
345 return -ENOMEM; 393 return -ENOMEM;
346 } 394 }
347 395
396 msc->scroll_accel = SCROLL_ACCEL_DEFAULT;
397
348 msc->quirks = id->driver_data; 398 msc->quirks = id->driver_data;
349 hid_set_drvdata(hdev, msc); 399 hid_set_drvdata(hdev, msc);
350 400