blob: 402682e0c2f4028b58599ce5ecddab84fdf26f69 [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.
Michael Poole128537c2010-02-06 12:24:36 -050074 * @ntouches: Number of touches in most recent touch report.
75 * @scroll_accel: Number of consecutive scroll motions.
76 * @scroll_jiffies: Time of last scroll motion.
77 * @touches: Most recent data for a touch, indexed by tracking ID.
78 * @tracking_ids: Mapping of current touch input data to @touches.
79 */
80struct magicmouse_sc {
81 struct input_dev *input;
82 unsigned long quirks;
83
Michael Poole128537c2010-02-06 12:24:36 -050084 int ntouches;
85 int scroll_accel;
86 unsigned long scroll_jiffies;
87
88 struct {
89 short x;
90 short y;
Chase Douglasc0426682010-06-20 21:32:31 -040091 short scroll_x;
Michael Poole128537c2010-02-06 12:24:36 -050092 short scroll_y;
93 u8 size;
94 } touches[16];
95 int tracking_ids[16];
96};
97
98static int magicmouse_firm_touch(struct magicmouse_sc *msc)
99{
100 int touch = -1;
101 int ii;
102
103 /* If there is only one "firm" touch, set touch to its
104 * tracking ID.
105 */
106 for (ii = 0; ii < msc->ntouches; ii++) {
107 int idx = msc->tracking_ids[ii];
108 if (msc->touches[idx].size < 8) {
109 /* Ignore this touch. */
110 } else if (touch >= 0) {
111 touch = -1;
112 break;
113 } else {
114 touch = idx;
115 }
116 }
117
118 return touch;
119}
120
121static void magicmouse_emit_buttons(struct magicmouse_sc *msc, int state)
122{
Michael Poole71b38bd2010-02-11 00:32:57 -0500123 int last_state = test_bit(BTN_LEFT, msc->input->key) << 0 |
124 test_bit(BTN_RIGHT, msc->input->key) << 1 |
125 test_bit(BTN_MIDDLE, msc->input->key) << 2;
Michael Poole128537c2010-02-06 12:24:36 -0500126
127 if (emulate_3button) {
128 int id;
129
130 /* If some button was pressed before, keep it held
131 * down. Otherwise, if there's exactly one firm
132 * touch, use that to override the mouse's guess.
133 */
134 if (state == 0) {
135 /* The button was released. */
136 } else if (last_state != 0) {
137 state = last_state;
138 } else if ((id = magicmouse_firm_touch(msc)) >= 0) {
139 int x = msc->touches[id].x;
140 if (x < middle_button_start)
141 state = 1;
142 else if (x > middle_button_stop)
143 state = 2;
144 else
145 state = 4;
146 } /* else: we keep the mouse's guess */
147
148 input_report_key(msc->input, BTN_MIDDLE, state & 4);
149 }
150
151 input_report_key(msc->input, BTN_LEFT, state & 1);
152 input_report_key(msc->input, BTN_RIGHT, state & 2);
153
154 if (state != last_state)
Chase Douglas0b778e72010-06-20 21:32:30 -0400155 msc->scroll_accel = SCROLL_ACCEL_DEFAULT;
Michael Poole128537c2010-02-06 12:24:36 -0500156}
157
158static void magicmouse_emit_touch(struct magicmouse_sc *msc, int raw_id, u8 *tdata)
159{
160 struct input_dev *input = msc->input;
Chase Douglas6de048b2010-08-31 21:56:20 -0400161 int id = (tdata[6] << 2 | tdata[5] >> 6) & 0xf;
162 int x = (tdata[1] << 28 | tdata[0] << 20) >> 20;
163 int y = -((tdata[2] << 24 | tdata[1] << 16) >> 20);
164 int size = tdata[5] & 0x3f;
165 int orientation = (tdata[6] >> 2) - 32;
166 int touch_major = tdata[3];
167 int touch_minor = tdata[4];
168 int state = tdata[7] & TOUCH_STATE_MASK;
169 int down = state != TOUCH_STATE_NONE;
Michael Poole128537c2010-02-06 12:24:36 -0500170
171 /* Store tracking ID and other fields. */
172 msc->tracking_ids[raw_id] = id;
173 msc->touches[id].x = x;
174 msc->touches[id].y = y;
Chase Douglas6de048b2010-08-31 21:56:20 -0400175 msc->touches[id].size = size;
Michael Poole128537c2010-02-06 12:24:36 -0500176
177 /* If requested, emulate a scroll wheel by detecting small
Chase Douglasef566d32010-06-02 10:28:25 -0400178 * vertical touch motions.
Michael Poole128537c2010-02-06 12:24:36 -0500179 */
Chase Douglasef566d32010-06-02 10:28:25 -0400180 if (emulate_scroll_wheel) {
Michael Poole128537c2010-02-06 12:24:36 -0500181 unsigned long now = jiffies;
Chase Douglasc0426682010-06-20 21:32:31 -0400182 int step_x = msc->touches[id].scroll_x - x;
183 int step_y = msc->touches[id].scroll_y - y;
Michael Poole128537c2010-02-06 12:24:36 -0500184
Michael Poole128537c2010-02-06 12:24:36 -0500185 /* Calculate and apply the scroll motion. */
Chase Douglas6de048b2010-08-31 21:56:20 -0400186 switch (state) {
Michael Poole128537c2010-02-06 12:24:36 -0500187 case TOUCH_STATE_START:
Chase Douglasc0426682010-06-20 21:32:31 -0400188 msc->touches[id].scroll_x = x;
Michael Poole128537c2010-02-06 12:24:36 -0500189 msc->touches[id].scroll_y = y;
Chase Douglas0b778e72010-06-20 21:32:30 -0400190
191 /* Reset acceleration after half a second. */
192 if (scroll_acceleration && time_before(now,
193 msc->scroll_jiffies + HZ / 2))
194 msc->scroll_accel = max_t(int,
195 msc->scroll_accel - 1, 1);
196 else
197 msc->scroll_accel = SCROLL_ACCEL_DEFAULT;
198
Michael Poole128537c2010-02-06 12:24:36 -0500199 break;
200 case TOUCH_STATE_DRAG:
Chase Douglasc0426682010-06-20 21:32:31 -0400201 step_x /= (64 - (int)scroll_speed) * msc->scroll_accel;
202 if (step_x != 0) {
203 msc->touches[id].scroll_x -= step_x *
Chase Douglas0b778e72010-06-20 21:32:30 -0400204 (64 - scroll_speed) * msc->scroll_accel;
Michael Poole128537c2010-02-06 12:24:36 -0500205 msc->scroll_jiffies = now;
Chase Douglasc0426682010-06-20 21:32:31 -0400206 input_report_rel(input, REL_HWHEEL, -step_x);
207 }
208
209 step_y /= (64 - (int)scroll_speed) * msc->scroll_accel;
210 if (step_y != 0) {
211 msc->touches[id].scroll_y -= step_y *
212 (64 - scroll_speed) * msc->scroll_accel;
213 msc->scroll_jiffies = now;
214 input_report_rel(input, REL_WHEEL, step_y);
Michael Poole128537c2010-02-06 12:24:36 -0500215 }
216 break;
217 }
218 }
219
220 /* Generate the input events for this touch. */
Chase Douglase3612e82010-07-05 09:57:52 -0400221 if (report_touches && down) {
Michael Poole128537c2010-02-06 12:24:36 -0500222 input_report_abs(input, ABS_MT_TRACKING_ID, id);
Chase Douglas6de048b2010-08-31 21:56:20 -0400223 input_report_abs(input, ABS_MT_TOUCH_MAJOR, touch_major);
224 input_report_abs(input, ABS_MT_TOUCH_MINOR, touch_minor);
Michael Poole128537c2010-02-06 12:24:36 -0500225 input_report_abs(input, ABS_MT_ORIENTATION, orientation);
226 input_report_abs(input, ABS_MT_POSITION_X, x);
227 input_report_abs(input, ABS_MT_POSITION_Y, y);
228
Michael Poole71b38bd2010-02-11 00:32:57 -0500229 if (report_undeciphered)
Michael Poole128537c2010-02-06 12:24:36 -0500230 input_event(input, EV_MSC, MSC_RAW, tdata[7]);
Michael Poole128537c2010-02-06 12:24:36 -0500231
232 input_mt_sync(input);
233 }
Chase Douglas0228db72010-09-02 16:49:52 +0200234
235 if (down)
236 msc->ntouches++;
Michael Poole128537c2010-02-06 12:24:36 -0500237}
238
239static int magicmouse_raw_event(struct hid_device *hdev,
240 struct hid_report *report, u8 *data, int size)
241{
242 struct magicmouse_sc *msc = hid_get_drvdata(hdev);
243 struct input_dev *input = msc->input;
Chase Douglasc61b7ce2010-09-02 16:51:54 +0200244 int x, y, ii, clicks, npoints;
Michael Poole128537c2010-02-06 12:24:36 -0500245
246 switch (data[0]) {
247 case 0x10:
248 if (size != 6)
249 return 0;
250 x = (__s16)(data[2] | data[3] << 8);
251 y = (__s16)(data[4] | data[5] << 8);
252 clicks = data[1];
253 break;
254 case TOUCH_REPORT_ID:
255 /* Expect six bytes of prefix, and N*8 bytes of touch data. */
256 if (size < 6 || ((size - 6) % 8) != 0)
257 return 0;
Chase Douglas0228db72010-09-02 16:49:52 +0200258 npoints = (size - 6) / 8;
259 msc->ntouches = 0;
260 for (ii = 0; ii < npoints; ii++)
Michael Poole128537c2010-02-06 12:24:36 -0500261 magicmouse_emit_touch(msc, ii, data + ii * 8 + 6);
Chase Douglase3612e82010-07-05 09:57:52 -0400262
Chase Douglas0228db72010-09-02 16:49:52 +0200263 if (report_touches && msc->ntouches == 0)
264 input_mt_sync(input);
Chase Douglase3612e82010-07-05 09:57:52 -0400265
Michael Poole128537c2010-02-06 12:24:36 -0500266 /* When emulating three-button mode, it is important
267 * to have the current touch information before
268 * generating a click event.
269 */
Michael Poole7d876c02010-07-05 10:50:09 -0400270 x = (int)(((data[3] & 0x0c) << 28) | (data[1] << 22)) >> 22;
271 y = (int)(((data[3] & 0x30) << 26) | (data[2] << 22)) >> 22;
Michael Poole128537c2010-02-06 12:24:36 -0500272 clicks = data[3];
Chase Douglasc61b7ce2010-09-02 16:51:54 +0200273
274 /* The following bits provide a device specific timestamp. They
275 * are unused here.
276 *
277 * ts = data[3] >> 6 | data[4] << 2 | data[5] << 10;
278 */
Michael Poole128537c2010-02-06 12:24:36 -0500279 break;
280 case 0x20: /* Theoretically battery status (0-100), but I have
281 * never seen it -- maybe it is only upon request.
282 */
283 case 0x60: /* Unknown, maybe laser on/off. */
284 case 0x61: /* Laser reflection status change.
285 * data[1]: 0 = spotted, 1 = lost
286 */
287 default:
288 return 0;
289 }
290
291 magicmouse_emit_buttons(msc, clicks & 3);
292 input_report_rel(input, REL_X, x);
293 input_report_rel(input, REL_Y, y);
294 input_sync(input);
295 return 1;
296}
297
298static int magicmouse_input_open(struct input_dev *dev)
299{
300 struct hid_device *hid = input_get_drvdata(dev);
301
302 return hid->ll_driver->open(hid);
303}
304
305static void magicmouse_input_close(struct input_dev *dev)
306{
307 struct hid_device *hid = input_get_drvdata(dev);
308
309 hid->ll_driver->close(hid);
310}
311
312static void magicmouse_setup_input(struct input_dev *input, struct hid_device *hdev)
313{
314 input_set_drvdata(input, hdev);
315 input->event = hdev->ll_driver->hidinput_input_event;
316 input->open = magicmouse_input_open;
317 input->close = magicmouse_input_close;
318
319 input->name = hdev->name;
320 input->phys = hdev->phys;
321 input->uniq = hdev->uniq;
322 input->id.bustype = hdev->bus;
323 input->id.vendor = hdev->vendor;
324 input->id.product = hdev->product;
325 input->id.version = hdev->version;
326 input->dev.parent = hdev->dev.parent;
327
Michael Poole71b38bd2010-02-11 00:32:57 -0500328 __set_bit(EV_KEY, input->evbit);
329 __set_bit(BTN_LEFT, input->keybit);
330 __set_bit(BTN_RIGHT, input->keybit);
Michael Poole128537c2010-02-06 12:24:36 -0500331 if (emulate_3button)
Michael Poole71b38bd2010-02-11 00:32:57 -0500332 __set_bit(BTN_MIDDLE, input->keybit);
333 __set_bit(BTN_TOOL_FINGER, input->keybit);
Michael Poole128537c2010-02-06 12:24:36 -0500334
Michael Poole71b38bd2010-02-11 00:32:57 -0500335 __set_bit(EV_REL, input->evbit);
336 __set_bit(REL_X, input->relbit);
337 __set_bit(REL_Y, input->relbit);
Chase Douglasc0426682010-06-20 21:32:31 -0400338 if (emulate_scroll_wheel) {
Michael Poole71b38bd2010-02-11 00:32:57 -0500339 __set_bit(REL_WHEEL, input->relbit);
Chase Douglasc0426682010-06-20 21:32:31 -0400340 __set_bit(REL_HWHEEL, input->relbit);
341 }
Michael Poole128537c2010-02-06 12:24:36 -0500342
343 if (report_touches) {
Michael Poole71b38bd2010-02-11 00:32:57 -0500344 __set_bit(EV_ABS, input->evbit);
Michael Poole128537c2010-02-06 12:24:36 -0500345
Michael Poole71b38bd2010-02-11 00:32:57 -0500346 input_set_abs_params(input, ABS_MT_TRACKING_ID, 0, 15, 0, 0);
347 input_set_abs_params(input, ABS_MT_TOUCH_MAJOR, 0, 255, 4, 0);
348 input_set_abs_params(input, ABS_MT_TOUCH_MINOR, 0, 255, 4, 0);
349 input_set_abs_params(input, ABS_MT_ORIENTATION, -32, 31, 1, 0);
350 input_set_abs_params(input, ABS_MT_POSITION_X, -1100, 1358,
351 4, 0);
Michael Poole128537c2010-02-06 12:24:36 -0500352 /* Note: Touch Y position from the device is inverted relative
353 * to how pointer motion is reported (and relative to how USB
354 * HID recommends the coordinates work). This driver keeps
355 * the origin at the same position, and just uses the additive
356 * inverse of the reported Y.
357 */
Michael Poole71b38bd2010-02-11 00:32:57 -0500358 input_set_abs_params(input, ABS_MT_POSITION_Y, -1589, 2047,
359 4, 0);
Michael Poole128537c2010-02-06 12:24:36 -0500360 }
361
362 if (report_undeciphered) {
Michael Poole71b38bd2010-02-11 00:32:57 -0500363 __set_bit(EV_MSC, input->evbit);
364 __set_bit(MSC_RAW, input->mscbit);
Michael Poole128537c2010-02-06 12:24:36 -0500365 }
366}
367
368static int magicmouse_probe(struct hid_device *hdev,
369 const struct hid_device_id *id)
370{
Chase Douglas07735902010-08-31 21:56:19 -0400371 __u8 feature[] = { 0xd7, 0x01 };
Michael Poole128537c2010-02-06 12:24:36 -0500372 struct input_dev *input;
373 struct magicmouse_sc *msc;
374 struct hid_report *report;
375 int ret;
376
377 msc = kzalloc(sizeof(*msc), GFP_KERNEL);
378 if (msc == NULL) {
379 dev_err(&hdev->dev, "can't alloc magicmouse descriptor\n");
380 return -ENOMEM;
381 }
382
Chase Douglas0b778e72010-06-20 21:32:30 -0400383 msc->scroll_accel = SCROLL_ACCEL_DEFAULT;
384
Michael Poole128537c2010-02-06 12:24:36 -0500385 msc->quirks = id->driver_data;
386 hid_set_drvdata(hdev, msc);
387
388 ret = hid_parse(hdev);
389 if (ret) {
390 dev_err(&hdev->dev, "magicmouse hid parse failed\n");
391 goto err_free;
392 }
393
Jiri Kosina23d02112010-05-12 16:01:26 +0200394 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
Michael Poole128537c2010-02-06 12:24:36 -0500395 if (ret) {
396 dev_err(&hdev->dev, "magicmouse hw start failed\n");
397 goto err_free;
398 }
399
Jiri Kosina23d02112010-05-12 16:01:26 +0200400 /* we are handling the input ourselves */
401 hidinput_disconnect(hdev);
402
Michael Poole128537c2010-02-06 12:24:36 -0500403 report = hid_register_report(hdev, HID_INPUT_REPORT, TOUCH_REPORT_ID);
404 if (!report) {
405 dev_err(&hdev->dev, "unable to register touch report\n");
406 ret = -ENOMEM;
Michael Poole71b38bd2010-02-11 00:32:57 -0500407 goto err_stop_hw;
Michael Poole128537c2010-02-06 12:24:36 -0500408 }
409 report->size = 6;
410
Chase Douglas07735902010-08-31 21:56:19 -0400411 ret = hdev->hid_output_raw_report(hdev, feature, sizeof(feature),
Michael Poole128537c2010-02-06 12:24:36 -0500412 HID_FEATURE_REPORT);
Chase Douglas07735902010-08-31 21:56:19 -0400413 if (ret != sizeof(feature)) {
414 dev_err(&hdev->dev, "unable to request touch data (%d)\n",
Michael Poole128537c2010-02-06 12:24:36 -0500415 ret);
Michael Poole71b38bd2010-02-11 00:32:57 -0500416 goto err_stop_hw;
Michael Poole128537c2010-02-06 12:24:36 -0500417 }
418
419 input = input_allocate_device();
420 if (!input) {
421 dev_err(&hdev->dev, "can't alloc input device\n");
422 ret = -ENOMEM;
Michael Poole71b38bd2010-02-11 00:32:57 -0500423 goto err_stop_hw;
Michael Poole128537c2010-02-06 12:24:36 -0500424 }
425 magicmouse_setup_input(input, hdev);
426
427 ret = input_register_device(input);
428 if (ret) {
429 dev_err(&hdev->dev, "input device registration failed\n");
Michael Poole71b38bd2010-02-11 00:32:57 -0500430 goto err_input;
Michael Poole128537c2010-02-06 12:24:36 -0500431 }
432 msc->input = input;
433
434 return 0;
Michael Poole71b38bd2010-02-11 00:32:57 -0500435err_input:
Michael Poole128537c2010-02-06 12:24:36 -0500436 input_free_device(input);
Michael Poole71b38bd2010-02-11 00:32:57 -0500437err_stop_hw:
438 hid_hw_stop(hdev);
439err_free:
Michael Poole128537c2010-02-06 12:24:36 -0500440 kfree(msc);
441 return ret;
442}
443
444static void magicmouse_remove(struct hid_device *hdev)
445{
Michael Poole28918c22010-03-09 06:47:35 -0500446 struct magicmouse_sc *msc = hid_get_drvdata(hdev);
447
Michael Poole128537c2010-02-06 12:24:36 -0500448 hid_hw_stop(hdev);
Michael Poole28918c22010-03-09 06:47:35 -0500449 input_unregister_device(msc->input);
450 kfree(msc);
Michael Poole128537c2010-02-06 12:24:36 -0500451}
452
453static const struct hid_device_id magic_mice[] = {
454 { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGICMOUSE),
455 .driver_data = 0 },
456 { }
457};
458MODULE_DEVICE_TABLE(hid, magic_mice);
459
460static struct hid_driver magicmouse_driver = {
461 .name = "magicmouse",
462 .id_table = magic_mice,
463 .probe = magicmouse_probe,
464 .remove = magicmouse_remove,
465 .raw_event = magicmouse_raw_event,
466};
467
468static int __init magicmouse_init(void)
469{
470 int ret;
471
472 ret = hid_register_driver(&magicmouse_driver);
473 if (ret)
474 printk(KERN_ERR "can't register magicmouse driver\n");
475
476 return ret;
477}
478
479static void __exit magicmouse_exit(void)
480{
481 hid_unregister_driver(&magicmouse_driver);
482}
483
484module_init(magicmouse_init);
485module_exit(magicmouse_exit);
486MODULE_LICENSE("GPL");