blob: 4c4a79c760a2135b46c0b98b80bc77e406c63cb9 [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 Douglas9846f352010-06-02 10:28:27 -040033static bool scroll_acceleration = false;
34module_param(scroll_acceleration, bool, 0644);
35MODULE_PARM_DESC(scroll_acceleration, "Accelerate sequential scroll events");
36
Michael Poole71b38bd2010-02-11 00:32:57 -050037static bool report_touches = true;
Michael Poole128537c2010-02-06 12:24:36 -050038module_param(report_touches, bool, 0644);
39MODULE_PARM_DESC(report_touches, "Emit touch records (otherwise, only use them for emulation)");
40
Michael Poole71b38bd2010-02-11 00:32:57 -050041static bool report_undeciphered;
Michael Poole128537c2010-02-06 12:24:36 -050042module_param(report_undeciphered, bool, 0644);
43MODULE_PARM_DESC(report_undeciphered, "Report undeciphered multi-touch state field using a MSC_RAW event");
44
45#define TOUCH_REPORT_ID 0x29
46/* These definitions are not precise, but they're close enough. (Bits
47 * 0x03 seem to indicate the aspect ratio of the touch, bits 0x70 seem
48 * to be some kind of bit mask -- 0x20 may be a near-field reading,
49 * and 0x40 is actual contact, and 0x10 may be a start/stop or change
50 * indication.)
51 */
52#define TOUCH_STATE_MASK 0xf0
53#define TOUCH_STATE_NONE 0x00
54#define TOUCH_STATE_START 0x30
55#define TOUCH_STATE_DRAG 0x40
56
57/**
58 * struct magicmouse_sc - Tracks Magic Mouse-specific data.
59 * @input: Input device through which we report events.
60 * @quirks: Currently unused.
61 * @last_timestamp: Timestamp from most recent (18-bit) touch report
62 * (units of milliseconds over short windows, but seems to
63 * increase faster when there are no touches).
64 * @delta_time: 18-bit difference between the two most recent touch
65 * reports from the mouse.
66 * @ntouches: Number of touches in most recent touch report.
67 * @scroll_accel: Number of consecutive scroll motions.
68 * @scroll_jiffies: Time of last scroll motion.
69 * @touches: Most recent data for a touch, indexed by tracking ID.
70 * @tracking_ids: Mapping of current touch input data to @touches.
71 */
72struct magicmouse_sc {
73 struct input_dev *input;
74 unsigned long quirks;
75
76 int last_timestamp;
77 int delta_time;
78 int ntouches;
79 int scroll_accel;
80 unsigned long scroll_jiffies;
81
82 struct {
83 short x;
84 short y;
85 short scroll_y;
86 u8 size;
87 } touches[16];
88 int tracking_ids[16];
89};
90
91static int magicmouse_firm_touch(struct magicmouse_sc *msc)
92{
93 int touch = -1;
94 int ii;
95
96 /* If there is only one "firm" touch, set touch to its
97 * tracking ID.
98 */
99 for (ii = 0; ii < msc->ntouches; ii++) {
100 int idx = msc->tracking_ids[ii];
101 if (msc->touches[idx].size < 8) {
102 /* Ignore this touch. */
103 } else if (touch >= 0) {
104 touch = -1;
105 break;
106 } else {
107 touch = idx;
108 }
109 }
110
111 return touch;
112}
113
114static void magicmouse_emit_buttons(struct magicmouse_sc *msc, int state)
115{
Michael Poole71b38bd2010-02-11 00:32:57 -0500116 int last_state = test_bit(BTN_LEFT, msc->input->key) << 0 |
117 test_bit(BTN_RIGHT, msc->input->key) << 1 |
118 test_bit(BTN_MIDDLE, msc->input->key) << 2;
Michael Poole128537c2010-02-06 12:24:36 -0500119
120 if (emulate_3button) {
121 int id;
122
123 /* If some button was pressed before, keep it held
124 * down. Otherwise, if there's exactly one firm
125 * touch, use that to override the mouse's guess.
126 */
127 if (state == 0) {
128 /* The button was released. */
129 } else if (last_state != 0) {
130 state = last_state;
131 } else if ((id = magicmouse_firm_touch(msc)) >= 0) {
132 int x = msc->touches[id].x;
133 if (x < middle_button_start)
134 state = 1;
135 else if (x > middle_button_stop)
136 state = 2;
137 else
138 state = 4;
139 } /* else: we keep the mouse's guess */
140
141 input_report_key(msc->input, BTN_MIDDLE, state & 4);
142 }
143
144 input_report_key(msc->input, BTN_LEFT, state & 1);
145 input_report_key(msc->input, BTN_RIGHT, state & 2);
146
147 if (state != last_state)
148 msc->scroll_accel = 0;
149}
150
151static void magicmouse_emit_touch(struct magicmouse_sc *msc, int raw_id, u8 *tdata)
152{
153 struct input_dev *input = msc->input;
154 __s32 x_y = tdata[0] << 8 | tdata[1] << 16 | tdata[2] << 24;
155 int misc = tdata[5] | tdata[6] << 8;
156 int id = (misc >> 6) & 15;
157 int x = x_y << 12 >> 20;
158 int y = -(x_y >> 20);
159
160 /* Store tracking ID and other fields. */
161 msc->tracking_ids[raw_id] = id;
162 msc->touches[id].x = x;
163 msc->touches[id].y = y;
164 msc->touches[id].size = misc & 63;
165
166 /* If requested, emulate a scroll wheel by detecting small
Chase Douglasef566d32010-06-02 10:28:25 -0400167 * vertical touch motions.
Michael Poole128537c2010-02-06 12:24:36 -0500168 */
Chase Douglasef566d32010-06-02 10:28:25 -0400169 if (emulate_scroll_wheel) {
Michael Poole128537c2010-02-06 12:24:36 -0500170 static const int accel_profile[] = {
171 256, 228, 192, 160, 128, 96, 64, 32,
172 };
173 unsigned long now = jiffies;
174 int step = msc->touches[id].scroll_y - y;
175
176 /* Reset acceleration after half a second. */
177 if (time_after(now, msc->scroll_jiffies + HZ / 2))
178 msc->scroll_accel = 0;
179
180 /* Calculate and apply the scroll motion. */
181 switch (tdata[7] & TOUCH_STATE_MASK) {
182 case TOUCH_STATE_START:
183 msc->touches[id].scroll_y = y;
Chase Douglas9846f352010-06-02 10:28:27 -0400184 if (scroll_acceleration)
185 msc->scroll_accel = min_t(int,
186 msc->scroll_accel + 1,
Michael Poole128537c2010-02-06 12:24:36 -0500187 ARRAY_SIZE(accel_profile) - 1);
188 break;
189 case TOUCH_STATE_DRAG:
190 step = step / accel_profile[msc->scroll_accel];
191 if (step != 0) {
192 msc->touches[id].scroll_y = y;
193 msc->scroll_jiffies = now;
194 input_report_rel(input, REL_WHEEL, step);
195 }
196 break;
197 }
198 }
199
200 /* Generate the input events for this touch. */
201 if (report_touches) {
Michael Poole71b38bd2010-02-11 00:32:57 -0500202 int orientation = (misc >> 10) - 32;
Michael Poole128537c2010-02-06 12:24:36 -0500203
204 input_report_abs(input, ABS_MT_TRACKING_ID, id);
205 input_report_abs(input, ABS_MT_TOUCH_MAJOR, tdata[3]);
206 input_report_abs(input, ABS_MT_TOUCH_MINOR, tdata[4]);
207 input_report_abs(input, ABS_MT_ORIENTATION, orientation);
208 input_report_abs(input, ABS_MT_POSITION_X, x);
209 input_report_abs(input, ABS_MT_POSITION_Y, y);
210
Michael Poole71b38bd2010-02-11 00:32:57 -0500211 if (report_undeciphered)
Michael Poole128537c2010-02-06 12:24:36 -0500212 input_event(input, EV_MSC, MSC_RAW, tdata[7]);
Michael Poole128537c2010-02-06 12:24:36 -0500213
214 input_mt_sync(input);
215 }
216}
217
218static int magicmouse_raw_event(struct hid_device *hdev,
219 struct hid_report *report, u8 *data, int size)
220{
221 struct magicmouse_sc *msc = hid_get_drvdata(hdev);
222 struct input_dev *input = msc->input;
223 int x, y, ts, ii, clicks;
224
225 switch (data[0]) {
226 case 0x10:
227 if (size != 6)
228 return 0;
229 x = (__s16)(data[2] | data[3] << 8);
230 y = (__s16)(data[4] | data[5] << 8);
231 clicks = data[1];
232 break;
233 case TOUCH_REPORT_ID:
234 /* Expect six bytes of prefix, and N*8 bytes of touch data. */
235 if (size < 6 || ((size - 6) % 8) != 0)
236 return 0;
237 ts = data[3] >> 6 | data[4] << 2 | data[5] << 10;
238 msc->delta_time = (ts - msc->last_timestamp) & 0x3ffff;
239 msc->last_timestamp = ts;
240 msc->ntouches = (size - 6) / 8;
241 for (ii = 0; ii < msc->ntouches; ii++)
242 magicmouse_emit_touch(msc, ii, data + ii * 8 + 6);
243 /* When emulating three-button mode, it is important
244 * to have the current touch information before
245 * generating a click event.
246 */
247 x = (signed char)data[1];
248 y = (signed char)data[2];
249 clicks = data[3];
250 break;
251 case 0x20: /* Theoretically battery status (0-100), but I have
252 * never seen it -- maybe it is only upon request.
253 */
254 case 0x60: /* Unknown, maybe laser on/off. */
255 case 0x61: /* Laser reflection status change.
256 * data[1]: 0 = spotted, 1 = lost
257 */
258 default:
259 return 0;
260 }
261
262 magicmouse_emit_buttons(msc, clicks & 3);
263 input_report_rel(input, REL_X, x);
264 input_report_rel(input, REL_Y, y);
265 input_sync(input);
266 return 1;
267}
268
269static int magicmouse_input_open(struct input_dev *dev)
270{
271 struct hid_device *hid = input_get_drvdata(dev);
272
273 return hid->ll_driver->open(hid);
274}
275
276static void magicmouse_input_close(struct input_dev *dev)
277{
278 struct hid_device *hid = input_get_drvdata(dev);
279
280 hid->ll_driver->close(hid);
281}
282
283static void magicmouse_setup_input(struct input_dev *input, struct hid_device *hdev)
284{
285 input_set_drvdata(input, hdev);
286 input->event = hdev->ll_driver->hidinput_input_event;
287 input->open = magicmouse_input_open;
288 input->close = magicmouse_input_close;
289
290 input->name = hdev->name;
291 input->phys = hdev->phys;
292 input->uniq = hdev->uniq;
293 input->id.bustype = hdev->bus;
294 input->id.vendor = hdev->vendor;
295 input->id.product = hdev->product;
296 input->id.version = hdev->version;
297 input->dev.parent = hdev->dev.parent;
298
Michael Poole71b38bd2010-02-11 00:32:57 -0500299 __set_bit(EV_KEY, input->evbit);
300 __set_bit(BTN_LEFT, input->keybit);
301 __set_bit(BTN_RIGHT, input->keybit);
Michael Poole128537c2010-02-06 12:24:36 -0500302 if (emulate_3button)
Michael Poole71b38bd2010-02-11 00:32:57 -0500303 __set_bit(BTN_MIDDLE, input->keybit);
304 __set_bit(BTN_TOOL_FINGER, input->keybit);
Michael Poole128537c2010-02-06 12:24:36 -0500305
Michael Poole71b38bd2010-02-11 00:32:57 -0500306 __set_bit(EV_REL, input->evbit);
307 __set_bit(REL_X, input->relbit);
308 __set_bit(REL_Y, input->relbit);
Michael Poole128537c2010-02-06 12:24:36 -0500309 if (emulate_scroll_wheel)
Michael Poole71b38bd2010-02-11 00:32:57 -0500310 __set_bit(REL_WHEEL, input->relbit);
Michael Poole128537c2010-02-06 12:24:36 -0500311
312 if (report_touches) {
Michael Poole71b38bd2010-02-11 00:32:57 -0500313 __set_bit(EV_ABS, input->evbit);
Michael Poole128537c2010-02-06 12:24:36 -0500314
Michael Poole71b38bd2010-02-11 00:32:57 -0500315 input_set_abs_params(input, ABS_MT_TRACKING_ID, 0, 15, 0, 0);
316 input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0, 255, 4, 0);
317 input_set_abs_params(input, ABS_MT_TOUCH_MINOR, 0, 255, 4, 0);
318 input_set_abs_params(input, ABS_MT_ORIENTATION, -32, 31, 1, 0);
319 input_set_abs_params(input, ABS_MT_POSITION_X, -1100, 1358,
320 4, 0);
Michael Poole128537c2010-02-06 12:24:36 -0500321 /* Note: Touch Y position from the device is inverted relative
322 * to how pointer motion is reported (and relative to how USB
323 * HID recommends the coordinates work). This driver keeps
324 * the origin at the same position, and just uses the additive
325 * inverse of the reported Y.
326 */
Michael Poole71b38bd2010-02-11 00:32:57 -0500327 input_set_abs_params(input, ABS_MT_POSITION_Y, -1589, 2047,
328 4, 0);
Michael Poole128537c2010-02-06 12:24:36 -0500329 }
330
331 if (report_undeciphered) {
Michael Poole71b38bd2010-02-11 00:32:57 -0500332 __set_bit(EV_MSC, input->evbit);
333 __set_bit(MSC_RAW, input->mscbit);
Michael Poole128537c2010-02-06 12:24:36 -0500334 }
335}
336
337static int magicmouse_probe(struct hid_device *hdev,
338 const struct hid_device_id *id)
339{
340 __u8 feature_1[] = { 0xd7, 0x01 };
341 __u8 feature_2[] = { 0xf8, 0x01, 0x32 };
342 struct input_dev *input;
343 struct magicmouse_sc *msc;
344 struct hid_report *report;
345 int ret;
346
347 msc = kzalloc(sizeof(*msc), GFP_KERNEL);
348 if (msc == NULL) {
349 dev_err(&hdev->dev, "can't alloc magicmouse descriptor\n");
350 return -ENOMEM;
351 }
352
353 msc->quirks = id->driver_data;
354 hid_set_drvdata(hdev, msc);
355
356 ret = hid_parse(hdev);
357 if (ret) {
358 dev_err(&hdev->dev, "magicmouse hid parse failed\n");
359 goto err_free;
360 }
361
Jiri Kosina23d02112010-05-12 16:01:26 +0200362 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
Michael Poole128537c2010-02-06 12:24:36 -0500363 if (ret) {
364 dev_err(&hdev->dev, "magicmouse hw start failed\n");
365 goto err_free;
366 }
367
Jiri Kosina23d02112010-05-12 16:01:26 +0200368 /* we are handling the input ourselves */
369 hidinput_disconnect(hdev);
370
Michael Poole128537c2010-02-06 12:24:36 -0500371 report = hid_register_report(hdev, HID_INPUT_REPORT, TOUCH_REPORT_ID);
372 if (!report) {
373 dev_err(&hdev->dev, "unable to register touch report\n");
374 ret = -ENOMEM;
Michael Poole71b38bd2010-02-11 00:32:57 -0500375 goto err_stop_hw;
Michael Poole128537c2010-02-06 12:24:36 -0500376 }
377 report->size = 6;
378
379 ret = hdev->hid_output_raw_report(hdev, feature_1, sizeof(feature_1),
380 HID_FEATURE_REPORT);
381 if (ret != sizeof(feature_1)) {
382 dev_err(&hdev->dev, "unable to request touch data (1:%d)\n",
383 ret);
Michael Poole71b38bd2010-02-11 00:32:57 -0500384 goto err_stop_hw;
Michael Poole128537c2010-02-06 12:24:36 -0500385 }
386 ret = hdev->hid_output_raw_report(hdev, feature_2,
387 sizeof(feature_2), HID_FEATURE_REPORT);
388 if (ret != sizeof(feature_2)) {
389 dev_err(&hdev->dev, "unable to request touch data (2:%d)\n",
390 ret);
Michael Poole71b38bd2010-02-11 00:32:57 -0500391 goto err_stop_hw;
Michael Poole128537c2010-02-06 12:24:36 -0500392 }
393
394 input = input_allocate_device();
395 if (!input) {
396 dev_err(&hdev->dev, "can't alloc input device\n");
397 ret = -ENOMEM;
Michael Poole71b38bd2010-02-11 00:32:57 -0500398 goto err_stop_hw;
Michael Poole128537c2010-02-06 12:24:36 -0500399 }
400 magicmouse_setup_input(input, hdev);
401
402 ret = input_register_device(input);
403 if (ret) {
404 dev_err(&hdev->dev, "input device registration failed\n");
Michael Poole71b38bd2010-02-11 00:32:57 -0500405 goto err_input;
Michael Poole128537c2010-02-06 12:24:36 -0500406 }
407 msc->input = input;
408
409 return 0;
Michael Poole71b38bd2010-02-11 00:32:57 -0500410err_input:
Michael Poole128537c2010-02-06 12:24:36 -0500411 input_free_device(input);
Michael Poole71b38bd2010-02-11 00:32:57 -0500412err_stop_hw:
413 hid_hw_stop(hdev);
414err_free:
Michael Poole128537c2010-02-06 12:24:36 -0500415 kfree(msc);
416 return ret;
417}
418
419static void magicmouse_remove(struct hid_device *hdev)
420{
Michael Poole28918c22010-03-09 06:47:35 -0500421 struct magicmouse_sc *msc = hid_get_drvdata(hdev);
422
Michael Poole128537c2010-02-06 12:24:36 -0500423 hid_hw_stop(hdev);
Michael Poole28918c22010-03-09 06:47:35 -0500424 input_unregister_device(msc->input);
425 kfree(msc);
Michael Poole128537c2010-02-06 12:24:36 -0500426}
427
428static const struct hid_device_id magic_mice[] = {
429 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGICMOUSE),
430 .driver_data = 0 },
431 { }
432};
433MODULE_DEVICE_TABLE(hid, magic_mice);
434
435static struct hid_driver magicmouse_driver = {
436 .name = "magicmouse",
437 .id_table = magic_mice,
438 .probe = magicmouse_probe,
439 .remove = magicmouse_remove,
440 .raw_event = magicmouse_raw_event,
441};
442
443static int __init magicmouse_init(void)
444{
445 int ret;
446
447 ret = hid_register_driver(&magicmouse_driver);
448 if (ret)
449 printk(KERN_ERR "can't register magicmouse driver\n");
450
451 return ret;
452}
453
454static void __exit magicmouse_exit(void)
455{
456 hid_unregister_driver(&magicmouse_driver);
457}
458
459module_init(magicmouse_init);
460module_exit(magicmouse_exit);
461MODULE_LICENSE("GPL");