blob: b74abf202e01eff01fbb24e6537cdb00f841293a [file] [log] [blame]
Michael Poole128537c2010-02-06 12:24:36 -05001/*
2 * Apple "Magic" Wireless Mouse driver
3 *
4 * Copyright (c) 2010 Michael Poole <mdpoole@troilus.org>
5 */
6
7/*
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
11 * any later version.
12 */
13
14#include <linux/device.h>
15#include <linux/hid.h>
16#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Michael Poole128537c2010-02-06 12:24:36 -050018#include <linux/usb.h>
19
20#include "hid-ids.h"
21
Michael Poole71b38bd2010-02-11 00:32:57 -050022static bool emulate_3button = true;
Michael Poole128537c2010-02-06 12:24:36 -050023module_param(emulate_3button, bool, 0644);
24MODULE_PARM_DESC(emulate_3button, "Emulate a middle button");
25
26static int middle_button_start = -350;
27static int middle_button_stop = +350;
28
Michael Poole71b38bd2010-02-11 00:32:57 -050029static bool emulate_scroll_wheel = true;
Michael Poole128537c2010-02-06 12:24:36 -050030module_param(emulate_scroll_wheel, bool, 0644);
31MODULE_PARM_DESC(emulate_scroll_wheel, "Emulate a scroll wheel");
32
Chase Douglas0b778e72010-06-20 21:32:30 -040033static 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
Chase Douglas9846f352010-06-02 10:28:27 -040044static bool scroll_acceleration = false;
45module_param(scroll_acceleration, bool, 0644);
46MODULE_PARM_DESC(scroll_acceleration, "Accelerate sequential scroll events");
47
Michael Poole71b38bd2010-02-11 00:32:57 -050048static bool report_touches = true;
Michael Poole128537c2010-02-06 12:24:36 -050049module_param(report_touches, bool, 0644);
50MODULE_PARM_DESC(report_touches, "Emit touch records (otherwise, only use them for emulation)");
51
Michael Poole71b38bd2010-02-11 00:32:57 -050052static bool report_undeciphered;
Michael Poole128537c2010-02-06 12:24:36 -050053module_param(report_undeciphered, bool, 0644);
54MODULE_PARM_DESC(report_undeciphered, "Report undeciphered multi-touch state field using a MSC_RAW event");
55
56#define TOUCH_REPORT_ID 0x29
57/* These definitions are not precise, but they're close enough. (Bits
58 * 0x03 seem to indicate the aspect ratio of the touch, bits 0x70 seem
59 * to be some kind of bit mask -- 0x20 may be a near-field reading,
60 * and 0x40 is actual contact, and 0x10 may be a start/stop or change
61 * indication.)
62 */
63#define TOUCH_STATE_MASK 0xf0
64#define TOUCH_STATE_NONE 0x00
65#define TOUCH_STATE_START 0x30
66#define TOUCH_STATE_DRAG 0x40
67
Chase Douglas0b778e72010-06-20 21:32:30 -040068#define SCROLL_ACCEL_DEFAULT 7
69
Michael Poole128537c2010-02-06 12:24:36 -050070/**
71 * struct magicmouse_sc - Tracks Magic Mouse-specific data.
72 * @input: Input device through which we report events.
73 * @quirks: Currently unused.
74 * @last_timestamp: Timestamp from most recent (18-bit) touch report
75 * (units of milliseconds over short windows, but seems to
76 * increase faster when there are no touches).
77 * @delta_time: 18-bit difference between the two most recent touch
78 * reports from the mouse.
79 * @ntouches: Number of touches in most recent touch report.
80 * @scroll_accel: Number of consecutive scroll motions.
81 * @scroll_jiffies: Time of last scroll motion.
82 * @touches: Most recent data for a touch, indexed by tracking ID.
83 * @tracking_ids: Mapping of current touch input data to @touches.
84 */
85struct magicmouse_sc {
86 struct input_dev *input;
87 unsigned long quirks;
88
89 int last_timestamp;
90 int delta_time;
91 int ntouches;
92 int scroll_accel;
93 unsigned long scroll_jiffies;
94
95 struct {
96 short x;
97 short y;
Chase Douglasc0426682010-06-20 21:32:31 -040098 short scroll_x;
Michael Poole128537c2010-02-06 12:24:36 -050099 short scroll_y;
100 u8 size;
101 } touches[16];
102 int tracking_ids[16];
103};
104
105static int magicmouse_firm_touch(struct magicmouse_sc *msc)
106{
107 int touch = -1;
108 int ii;
109
110 /* If there is only one "firm" touch, set touch to its
111 * tracking ID.
112 */
113 for (ii = 0; ii < msc->ntouches; ii++) {
114 int idx = msc->tracking_ids[ii];
115 if (msc->touches[idx].size < 8) {
116 /* Ignore this touch. */
117 } else if (touch >= 0) {
118 touch = -1;
119 break;
120 } else {
121 touch = idx;
122 }
123 }
124
125 return touch;
126}
127
128static void magicmouse_emit_buttons(struct magicmouse_sc *msc, int state)
129{
Michael Poole71b38bd2010-02-11 00:32:57 -0500130 int last_state = test_bit(BTN_LEFT, msc->input->key) << 0 |
131 test_bit(BTN_RIGHT, msc->input->key) << 1 |
132 test_bit(BTN_MIDDLE, msc->input->key) << 2;
Michael Poole128537c2010-02-06 12:24:36 -0500133
134 if (emulate_3button) {
135 int id;
136
137 /* If some button was pressed before, keep it held
138 * down. Otherwise, if there's exactly one firm
139 * touch, use that to override the mouse's guess.
140 */
141 if (state == 0) {
142 /* The button was released. */
143 } else if (last_state != 0) {
144 state = last_state;
145 } else if ((id = magicmouse_firm_touch(msc)) >= 0) {
146 int x = msc->touches[id].x;
147 if (x < middle_button_start)
148 state = 1;
149 else if (x > middle_button_stop)
150 state = 2;
151 else
152 state = 4;
153 } /* else: we keep the mouse's guess */
154
155 input_report_key(msc->input, BTN_MIDDLE, state & 4);
156 }
157
158 input_report_key(msc->input, BTN_LEFT, state & 1);
159 input_report_key(msc->input, BTN_RIGHT, state & 2);
160
161 if (state != last_state)
Chase Douglas0b778e72010-06-20 21:32:30 -0400162 msc->scroll_accel = SCROLL_ACCEL_DEFAULT;
Michael Poole128537c2010-02-06 12:24:36 -0500163}
164
165static void magicmouse_emit_touch(struct magicmouse_sc *msc, int raw_id, u8 *tdata)
166{
167 struct input_dev *input = msc->input;
168 __s32 x_y = tdata[0] << 8 | tdata[1] << 16 | tdata[2] << 24;
169 int misc = tdata[5] | tdata[6] << 8;
170 int id = (misc >> 6) & 15;
171 int x = x_y << 12 >> 20;
172 int y = -(x_y >> 20);
Chase Douglase3612e82010-07-05 09:57:52 -0400173 int down = (tdata[7] & TOUCH_STATE_MASK) != TOUCH_STATE_NONE;
Michael Poole128537c2010-02-06 12:24:36 -0500174
175 /* Store tracking ID and other fields. */
176 msc->tracking_ids[raw_id] = id;
177 msc->touches[id].x = x;
178 msc->touches[id].y = y;
179 msc->touches[id].size = misc & 63;
180
181 /* If requested, emulate a scroll wheel by detecting small
Chase Douglasef566d32010-06-02 10:28:25 -0400182 * vertical touch motions.
Michael Poole128537c2010-02-06 12:24:36 -0500183 */
Chase Douglasef566d32010-06-02 10:28:25 -0400184 if (emulate_scroll_wheel) {
Michael Poole128537c2010-02-06 12:24:36 -0500185 unsigned long now = jiffies;
Chase Douglasc0426682010-06-20 21:32:31 -0400186 int step_x = msc->touches[id].scroll_x - x;
187 int step_y = msc->touches[id].scroll_y - y;
Michael Poole128537c2010-02-06 12:24:36 -0500188
Michael Poole128537c2010-02-06 12:24:36 -0500189 /* Calculate and apply the scroll motion. */
190 switch (tdata[7] & TOUCH_STATE_MASK) {
191 case TOUCH_STATE_START:
Chase Douglasc0426682010-06-20 21:32:31 -0400192 msc->touches[id].scroll_x = x;
Michael Poole128537c2010-02-06 12:24:36 -0500193 msc->touches[id].scroll_y = y;
Chase Douglas0b778e72010-06-20 21:32:30 -0400194
195 /* Reset acceleration after half a second. */
196 if (scroll_acceleration && time_before(now,
197 msc->scroll_jiffies + HZ / 2))
198 msc->scroll_accel = max_t(int,
199 msc->scroll_accel - 1, 1);
200 else
201 msc->scroll_accel = SCROLL_ACCEL_DEFAULT;
202
Michael Poole128537c2010-02-06 12:24:36 -0500203 break;
204 case TOUCH_STATE_DRAG:
Chase Douglasc0426682010-06-20 21:32:31 -0400205 step_x /= (64 - (int)scroll_speed) * msc->scroll_accel;
206 if (step_x != 0) {
207 msc->touches[id].scroll_x -= step_x *
Chase Douglas0b778e72010-06-20 21:32:30 -0400208 (64 - scroll_speed) * msc->scroll_accel;
Michael Poole128537c2010-02-06 12:24:36 -0500209 msc->scroll_jiffies = now;
Chase Douglasc0426682010-06-20 21:32:31 -0400210 input_report_rel(input, REL_HWHEEL, -step_x);
211 }
212
213 step_y /= (64 - (int)scroll_speed) * msc->scroll_accel;
214 if (step_y != 0) {
215 msc->touches[id].scroll_y -= step_y *
216 (64 - scroll_speed) * msc->scroll_accel;
217 msc->scroll_jiffies = now;
218 input_report_rel(input, REL_WHEEL, step_y);
Michael Poole128537c2010-02-06 12:24:36 -0500219 }
220 break;
221 }
222 }
223
224 /* Generate the input events for this touch. */
Chase Douglase3612e82010-07-05 09:57:52 -0400225 if (report_touches && down) {
Michael Poole71b38bd2010-02-11 00:32:57 -0500226 int orientation = (misc >> 10) - 32;
Michael Poole128537c2010-02-06 12:24:36 -0500227
228 input_report_abs(input, ABS_MT_TRACKING_ID, id);
229 input_report_abs(input, ABS_MT_TOUCH_MAJOR, tdata[3]);
230 input_report_abs(input, ABS_MT_TOUCH_MINOR, tdata[4]);
231 input_report_abs(input, ABS_MT_ORIENTATION, orientation);
232 input_report_abs(input, ABS_MT_POSITION_X, x);
233 input_report_abs(input, ABS_MT_POSITION_Y, y);
234
Michael Poole71b38bd2010-02-11 00:32:57 -0500235 if (report_undeciphered)
Michael Poole128537c2010-02-06 12:24:36 -0500236 input_event(input, EV_MSC, MSC_RAW, tdata[7]);
Michael Poole128537c2010-02-06 12:24:36 -0500237
238 input_mt_sync(input);
239 }
Chase Douglas0228db72010-09-02 16:49:52 +0200240
241 if (down)
242 msc->ntouches++;
Michael Poole128537c2010-02-06 12:24:36 -0500243}
244
245static int magicmouse_raw_event(struct hid_device *hdev,
246 struct hid_report *report, u8 *data, int size)
247{
248 struct magicmouse_sc *msc = hid_get_drvdata(hdev);
249 struct input_dev *input = msc->input;
Chase Douglas0228db72010-09-02 16:49:52 +0200250 int x, y, ts, ii, clicks, npoints;
Michael Poole128537c2010-02-06 12:24:36 -0500251
252 switch (data[0]) {
253 case 0x10:
254 if (size != 6)
255 return 0;
256 x = (__s16)(data[2] | data[3] << 8);
257 y = (__s16)(data[4] | data[5] << 8);
258 clicks = data[1];
259 break;
260 case TOUCH_REPORT_ID:
261 /* Expect six bytes of prefix, and N*8 bytes of touch data. */
262 if (size < 6 || ((size - 6) % 8) != 0)
263 return 0;
264 ts = data[3] >> 6 | data[4] << 2 | data[5] << 10;
265 msc->delta_time = (ts - msc->last_timestamp) & 0x3ffff;
266 msc->last_timestamp = ts;
Chase Douglas0228db72010-09-02 16:49:52 +0200267 npoints = (size - 6) / 8;
268 msc->ntouches = 0;
269 for (ii = 0; ii < npoints; ii++)
Michael Poole128537c2010-02-06 12:24:36 -0500270 magicmouse_emit_touch(msc, ii, data + ii * 8 + 6);
Chase Douglase3612e82010-07-05 09:57:52 -0400271
Chase Douglas0228db72010-09-02 16:49:52 +0200272 if (report_touches && msc->ntouches == 0)
273 input_mt_sync(input);
Chase Douglase3612e82010-07-05 09:57:52 -0400274
Michael Poole128537c2010-02-06 12:24:36 -0500275 /* When emulating three-button mode, it is important
276 * to have the current touch information before
277 * generating a click event.
278 */
Michael Poole7d876c02010-07-05 10:50:09 -0400279 x = (int)(((data[3] & 0x0c) << 28) | (data[1] << 22)) >> 22;
280 y = (int)(((data[3] & 0x30) << 26) | (data[2] << 22)) >> 22;
Michael Poole128537c2010-02-06 12:24:36 -0500281 clicks = data[3];
282 break;
283 case 0x20: /* Theoretically battery status (0-100), but I have
284 * never seen it -- maybe it is only upon request.
285 */
286 case 0x60: /* Unknown, maybe laser on/off. */
287 case 0x61: /* Laser reflection status change.
288 * data[1]: 0 = spotted, 1 = lost
289 */
290 default:
291 return 0;
292 }
293
294 magicmouse_emit_buttons(msc, clicks & 3);
295 input_report_rel(input, REL_X, x);
296 input_report_rel(input, REL_Y, y);
297 input_sync(input);
298 return 1;
299}
300
301static int magicmouse_input_open(struct input_dev *dev)
302{
303 struct hid_device *hid = input_get_drvdata(dev);
304
305 return hid->ll_driver->open(hid);
306}
307
308static void magicmouse_input_close(struct input_dev *dev)
309{
310 struct hid_device *hid = input_get_drvdata(dev);
311
312 hid->ll_driver->close(hid);
313}
314
315static void magicmouse_setup_input(struct input_dev *input, struct hid_device *hdev)
316{
317 input_set_drvdata(input, hdev);
318 input->event = hdev->ll_driver->hidinput_input_event;
319 input->open = magicmouse_input_open;
320 input->close = magicmouse_input_close;
321
322 input->name = hdev->name;
323 input->phys = hdev->phys;
324 input->uniq = hdev->uniq;
325 input->id.bustype = hdev->bus;
326 input->id.vendor = hdev->vendor;
327 input->id.product = hdev->product;
328 input->id.version = hdev->version;
329 input->dev.parent = hdev->dev.parent;
330
Michael Poole71b38bd2010-02-11 00:32:57 -0500331 __set_bit(EV_KEY, input->evbit);
332 __set_bit(BTN_LEFT, input->keybit);
333 __set_bit(BTN_RIGHT, input->keybit);
Michael Poole128537c2010-02-06 12:24:36 -0500334 if (emulate_3button)
Michael Poole71b38bd2010-02-11 00:32:57 -0500335 __set_bit(BTN_MIDDLE, input->keybit);
336 __set_bit(BTN_TOOL_FINGER, input->keybit);
Michael Poole128537c2010-02-06 12:24:36 -0500337
Michael Poole71b38bd2010-02-11 00:32:57 -0500338 __set_bit(EV_REL, input->evbit);
339 __set_bit(REL_X, input->relbit);
340 __set_bit(REL_Y, input->relbit);
Chase Douglasc0426682010-06-20 21:32:31 -0400341 if (emulate_scroll_wheel) {
Michael Poole71b38bd2010-02-11 00:32:57 -0500342 __set_bit(REL_WHEEL, input->relbit);
Chase Douglasc0426682010-06-20 21:32:31 -0400343 __set_bit(REL_HWHEEL, input->relbit);
344 }
Michael Poole128537c2010-02-06 12:24:36 -0500345
346 if (report_touches) {
Michael Poole71b38bd2010-02-11 00:32:57 -0500347 __set_bit(EV_ABS, input->evbit);
Michael Poole128537c2010-02-06 12:24:36 -0500348
Michael Poole71b38bd2010-02-11 00:32:57 -0500349 input_set_abs_params(input, ABS_MT_TRACKING_ID, 0, 15, 0, 0);
350 input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0, 255, 4, 0);
351 input_set_abs_params(input, ABS_MT_TOUCH_MINOR, 0, 255, 4, 0);
352 input_set_abs_params(input, ABS_MT_ORIENTATION, -32, 31, 1, 0);
353 input_set_abs_params(input, ABS_MT_POSITION_X, -1100, 1358,
354 4, 0);
Michael Poole128537c2010-02-06 12:24:36 -0500355 /* Note: Touch Y position from the device is inverted relative
356 * to how pointer motion is reported (and relative to how USB
357 * HID recommends the coordinates work). This driver keeps
358 * the origin at the same position, and just uses the additive
359 * inverse of the reported Y.
360 */
Michael Poole71b38bd2010-02-11 00:32:57 -0500361 input_set_abs_params(input, ABS_MT_POSITION_Y, -1589, 2047,
362 4, 0);
Michael Poole128537c2010-02-06 12:24:36 -0500363 }
364
365 if (report_undeciphered) {
Michael Poole71b38bd2010-02-11 00:32:57 -0500366 __set_bit(EV_MSC, input->evbit);
367 __set_bit(MSC_RAW, input->mscbit);
Michael Poole128537c2010-02-06 12:24:36 -0500368 }
369}
370
371static int magicmouse_probe(struct hid_device *hdev,
372 const struct hid_device_id *id)
373{
374 __u8 feature_1[] = { 0xd7, 0x01 };
375 __u8 feature_2[] = { 0xf8, 0x01, 0x32 };
376 struct input_dev *input;
377 struct magicmouse_sc *msc;
378 struct hid_report *report;
379 int ret;
380
381 msc = kzalloc(sizeof(*msc), GFP_KERNEL);
382 if (msc == NULL) {
383 dev_err(&hdev->dev, "can't alloc magicmouse descriptor\n");
384 return -ENOMEM;
385 }
386
Chase Douglas0b778e72010-06-20 21:32:30 -0400387 msc->scroll_accel = SCROLL_ACCEL_DEFAULT;
388
Michael Poole128537c2010-02-06 12:24:36 -0500389 msc->quirks = id->driver_data;
390 hid_set_drvdata(hdev, msc);
391
392 ret = hid_parse(hdev);
393 if (ret) {
394 dev_err(&hdev->dev, "magicmouse hid parse failed\n");
395 goto err_free;
396 }
397
Jiri Kosina23d02112010-05-12 16:01:26 +0200398 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
Michael Poole128537c2010-02-06 12:24:36 -0500399 if (ret) {
400 dev_err(&hdev->dev, "magicmouse hw start failed\n");
401 goto err_free;
402 }
403
Jiri Kosina23d02112010-05-12 16:01:26 +0200404 /* we are handling the input ourselves */
405 hidinput_disconnect(hdev);
406
Michael Poole128537c2010-02-06 12:24:36 -0500407 report = hid_register_report(hdev, HID_INPUT_REPORT, TOUCH_REPORT_ID);
408 if (!report) {
409 dev_err(&hdev->dev, "unable to register touch report\n");
410 ret = -ENOMEM;
Michael Poole71b38bd2010-02-11 00:32:57 -0500411 goto err_stop_hw;
Michael Poole128537c2010-02-06 12:24:36 -0500412 }
413 report->size = 6;
414
415 ret = hdev->hid_output_raw_report(hdev, feature_1, sizeof(feature_1),
416 HID_FEATURE_REPORT);
417 if (ret != sizeof(feature_1)) {
418 dev_err(&hdev->dev, "unable to request touch data (1:%d)\n",
419 ret);
Michael Poole71b38bd2010-02-11 00:32:57 -0500420 goto err_stop_hw;
Michael Poole128537c2010-02-06 12:24:36 -0500421 }
422 ret = hdev->hid_output_raw_report(hdev, feature_2,
423 sizeof(feature_2), HID_FEATURE_REPORT);
424 if (ret != sizeof(feature_2)) {
425 dev_err(&hdev->dev, "unable to request touch data (2:%d)\n",
426 ret);
Michael Poole71b38bd2010-02-11 00:32:57 -0500427 goto err_stop_hw;
Michael Poole128537c2010-02-06 12:24:36 -0500428 }
429
430 input = input_allocate_device();
431 if (!input) {
432 dev_err(&hdev->dev, "can't alloc input device\n");
433 ret = -ENOMEM;
Michael Poole71b38bd2010-02-11 00:32:57 -0500434 goto err_stop_hw;
Michael Poole128537c2010-02-06 12:24:36 -0500435 }
436 magicmouse_setup_input(input, hdev);
437
438 ret = input_register_device(input);
439 if (ret) {
440 dev_err(&hdev->dev, "input device registration failed\n");
Michael Poole71b38bd2010-02-11 00:32:57 -0500441 goto err_input;
Michael Poole128537c2010-02-06 12:24:36 -0500442 }
443 msc->input = input;
444
445 return 0;
Michael Poole71b38bd2010-02-11 00:32:57 -0500446err_input:
Michael Poole128537c2010-02-06 12:24:36 -0500447 input_free_device(input);
Michael Poole71b38bd2010-02-11 00:32:57 -0500448err_stop_hw:
449 hid_hw_stop(hdev);
450err_free:
Michael Poole128537c2010-02-06 12:24:36 -0500451 kfree(msc);
452 return ret;
453}
454
455static void magicmouse_remove(struct hid_device *hdev)
456{
Michael Poole28918c22010-03-09 06:47:35 -0500457 struct magicmouse_sc *msc = hid_get_drvdata(hdev);
458
Michael Poole128537c2010-02-06 12:24:36 -0500459 hid_hw_stop(hdev);
Michael Poole28918c22010-03-09 06:47:35 -0500460 input_unregister_device(msc->input);
461 kfree(msc);
Michael Poole128537c2010-02-06 12:24:36 -0500462}
463
464static const struct hid_device_id magic_mice[] = {
465 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGICMOUSE),
466 .driver_data = 0 },
467 { }
468};
469MODULE_DEVICE_TABLE(hid, magic_mice);
470
471static struct hid_driver magicmouse_driver = {
472 .name = "magicmouse",
473 .id_table = magic_mice,
474 .probe = magicmouse_probe,
475 .remove = magicmouse_remove,
476 .raw_event = magicmouse_raw_event,
477};
478
479static int __init magicmouse_init(void)
480{
481 int ret;
482
483 ret = hid_register_driver(&magicmouse_driver);
484 if (ret)
485 printk(KERN_ERR "can't register magicmouse driver\n");
486
487 return ret;
488}
489
490static void __exit magicmouse_exit(void)
491{
492 hid_unregister_driver(&magicmouse_driver);
493}
494
495module_init(magicmouse_init);
496module_exit(magicmouse_exit);
497MODULE_LICENSE("GPL");