Skip to content

Commit 3fe2699

Browse files
marcanjannau
authored andcommitted
HID: magicmouse: Query device dimensions via HID report
For SPI/MTP trackpads, query the dimensions via HID report instead of hardcoding values. TODO: Does this work for the USB/BT devices? Maybe we can get rid of the hardcoded sizes everywhere? Signed-off-by: Hector Martin <marcan@marcan.st>
1 parent 2fa901b commit 3fe2699

1 file changed

Lines changed: 81 additions & 21 deletions

File tree

drivers/hid/hid-magicmouse.c

Lines changed: 81 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ MODULE_PARM_DESC(report_undeciphered, "Report undeciphered multi-touch state fie
6363
#define SPI_REPORT_ID 0x02
6464
#define SPI_RESET_REPORT_ID 0x60
6565
#define MTP_REPORT_ID 0x75
66+
#define SENSOR_DIMENSIONS_REPORT_ID 0xd9
6667
#define USB_BATTERY_TIMEOUT_MS 60000
6768

6869
#define MAX_CONTACTS 16
@@ -117,6 +118,7 @@ MODULE_PARM_DESC(report_undeciphered, "Report undeciphered multi-touch state fie
117118
#define TRACKPAD2_RES_Y \
118119
((TRACKPAD2_MAX_Y - TRACKPAD2_MIN_Y) / (TRACKPAD2_DIMENSION_Y / 100))
119120

121+
/* These are fallback values, since the real values will be queried from the device. */
120122
#define J314_TP_DIMENSION_X (float)13000
121123
#define J314_TP_MIN_X -5900
122124
#define J314_TP_MAX_X 6500
@@ -140,6 +142,7 @@ struct magicmouse_input_ops {
140142
* struct magicmouse_sc - Tracks Magic Mouse-specific data.
141143
* @input: Input device through which we report events.
142144
* @quirks: Currently unused.
145+
* @query_dimensions: Whether to query and update dimensions on first open
143146
* @ntouches: Number of touches in most recent touch report.
144147
* @scroll_accel: Number of consecutive scroll motions.
145148
* @scroll_jiffies: Time of last scroll motion.
@@ -154,6 +157,7 @@ struct magicmouse_input_ops {
154157
struct magicmouse_sc {
155158
struct input_dev *input;
156159
unsigned long quirks;
160+
bool query_dimensions;
157161

158162
int ntouches;
159163
int scroll_accel;
@@ -177,6 +181,11 @@ struct magicmouse_sc {
177181
struct magicmouse_input_ops input_ops;
178182
};
179183

184+
static inline int le16_to_int(__le16 x)
185+
{
186+
return (signed short)le16_to_cpu(x);
187+
}
188+
180189
static int magicmouse_enable_multitouch(struct hid_device *hdev)
181190
{
182191
const u8 *feature;
@@ -246,19 +255,71 @@ static int magicmouse_open(struct input_dev *dev)
246255
* This results in -EIO from the _raw low-level transport callback,
247256
* but there seems to be no other way of switching the mode.
248257
* Thus the super-ugly hacky success check below.
258+
*
259+
* MTP devices do not need this.
249260
*/
250-
ret = magicmouse_enable_multitouch(hdev);
251-
if (ret == -EIO && hdev->product == USB_DEVICE_ID_APPLE_MAGICMOUSE2) {
252-
schedule_delayed_work(&msc->work, msecs_to_jiffies(500));
253-
return 0;
261+
if (hdev->bus != BUS_HOST) {
262+
ret = magicmouse_enable_multitouch(hdev);
263+
if (ret == -EIO && hdev->product == USB_DEVICE_ID_APPLE_MAGICMOUSE2) {
264+
schedule_delayed_work(&msc->work, msecs_to_jiffies(500));
265+
return 0;
266+
}
267+
if (ret < 0)
268+
hid_err(hdev, "unable to request touch data (%d)\n", ret);
254269
}
255-
if (ret < 0)
256-
hid_err(hdev, "unable to request touch data (%d)\n", ret);
257-
258270
/*
259271
* MT enable is usually not required after the first time, so don't
260272
* consider it fatal.
261273
*/
274+
275+
/*
276+
* For Apple Silicon trackpads, we want to query the dimensions on
277+
* device open. This is because doing so requires the firmware, but
278+
* we don't want to force a firmware load until the device is opened
279+
* for the first time. So do that here and update the input properties
280+
* just in time before userspace queries them.
281+
*/
282+
if (msc->query_dimensions) {
283+
struct input_dev *input = msc->input;
284+
u8 buf[32];
285+
struct {
286+
__le32 width;
287+
__le32 height;
288+
__le16 min_x;
289+
__le16 min_y;
290+
__le16 max_x;
291+
__le16 max_y;
292+
} dim;
293+
uint32_t x_span, y_span;
294+
295+
ret = hid_hw_raw_request(hdev, SENSOR_DIMENSIONS_REPORT_ID, buf, sizeof(buf), HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
296+
if (ret < (int)(1 + sizeof(dim))) {
297+
hid_err(hdev, "unable to request dimensions (%d)\n", ret);
298+
return ret;
299+
}
300+
301+
memcpy(&dim, buf + 1, sizeof(dim));
302+
303+
/* finger position */
304+
input_set_abs_params(input, ABS_MT_POSITION_X,
305+
le16_to_int(dim.min_x), le16_to_int(dim.max_x), 0, 0);
306+
/* Y axis is inverted */
307+
input_set_abs_params(input, ABS_MT_POSITION_Y,
308+
-le16_to_int(dim.max_y), -le16_to_int(dim.min_y), 0, 0);
309+
x_span = le16_to_int(dim.max_x) - le16_to_int(dim.min_x);
310+
y_span = le16_to_int(dim.max_y) - le16_to_int(dim.min_y);
311+
312+
/* X/Y resolution */
313+
input_abs_set_res(input, ABS_MT_POSITION_X, 100 * x_span / le32_to_cpu(dim.width) );
314+
input_abs_set_res(input, ABS_MT_POSITION_Y, 100 * y_span / le32_to_cpu(dim.height) );
315+
316+
/* copy info, as input_mt_init_slots() does */
317+
dev->absinfo[ABS_X] = dev->absinfo[ABS_MT_POSITION_X];
318+
dev->absinfo[ABS_Y] = dev->absinfo[ABS_MT_POSITION_Y];
319+
320+
msc->query_dimensions = false;
321+
}
322+
262323
return 0;
263324
}
264325

@@ -708,11 +769,6 @@ struct tp_mouse_report {
708769
u8 padding[4];
709770
};
710771

711-
static inline int le16_to_int(__le16 x)
712-
{
713-
return (signed short)le16_to_cpu(x);
714-
}
715-
716772
static void report_finger_data(struct input_dev *input, int slot,
717773
const struct input_mt_pos *pos,
718774
const struct tp_finger *f)
@@ -1016,6 +1072,7 @@ static int magicmouse_setup_input_mtp(struct input_dev *input,
10161072
{
10171073
int error;
10181074
int mt_flags = 0;
1075+
struct magicmouse_sc *msc = hid_get_drvdata(hdev);
10191076

10201077
__set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
10211078
__clear_bit(BTN_0, input->keybit);
@@ -1081,6 +1138,18 @@ static int magicmouse_setup_input_mtp(struct input_dev *input,
10811138
if (error)
10821139
return error;
10831140

1141+
/*
1142+
* Override the default input->open function to send the MT
1143+
* enable every time the device is opened. This ensures it works
1144+
* even if we missed a reset event due to the device being closed.
1145+
* input->close is overridden for symmetry.
1146+
*
1147+
* This also takes care of the dimensions query.
1148+
*/
1149+
input->open = magicmouse_open;
1150+
input->close = magicmouse_close;
1151+
msc->query_dimensions = true;
1152+
10841153
return 0;
10851154
}
10861155

@@ -1091,15 +1160,6 @@ static int magicmouse_setup_input_spi(struct input_dev *input,
10911160
if (ret)
10921161
return ret;
10931162

1094-
/*
1095-
* Override the default input->open function to send the MT
1096-
* enable every time the device is opened. This ensures it works
1097-
* even if we missed a reset event due to the device being closed.
1098-
* input->close is overridden for symmetry.
1099-
*/
1100-
input->open = magicmouse_open;
1101-
input->close = magicmouse_close;
1102-
11031163
return 0;
11041164
}
11051165

0 commit comments

Comments
 (0)