Skip to content

Commit 9318c49

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 <[email protected]>
1 parent 6d3db70 commit 9318c49

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
@@ -62,6 +62,7 @@ MODULE_PARM_DESC(report_undeciphered, "Report undeciphered multi-touch state fie
6262
#define SPI_REPORT_ID 0x02
6363
#define SPI_RESET_REPORT_ID 0x60
6464
#define MTP_REPORT_ID 0x75
65+
#define SENSOR_DIMENSIONS_REPORT_ID 0xd9
6566
#define USB_BATTERY_TIMEOUT_MS 60000
6667

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

120+
/* These are fallback values, since the real values will be queried from the device. */
119121
#define J314_TP_DIMENSION_X (float)13000
120122
#define J314_TP_MIN_X -5900
121123
#define J314_TP_MAX_X 6500
@@ -139,6 +141,7 @@ struct magicmouse_input_ops {
139141
* struct magicmouse_sc - Tracks Magic Mouse-specific data.
140142
* @input: Input device through which we report events.
141143
* @quirks: Currently unused.
144+
* @query_dimensions: Whether to query and update dimensions on first open
142145
* @ntouches: Number of touches in most recent touch report.
143146
* @scroll_accel: Number of consecutive scroll motions.
144147
* @scroll_jiffies: Time of last scroll motion.
@@ -153,6 +156,7 @@ struct magicmouse_input_ops {
153156
struct magicmouse_sc {
154157
struct input_dev *input;
155158
unsigned long quirks;
159+
bool query_dimensions;
156160

157161
int ntouches;
158162
int scroll_accel;
@@ -176,6 +180,11 @@ struct magicmouse_sc {
176180
struct magicmouse_input_ops input_ops;
177181
};
178182

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

@@ -697,11 +758,6 @@ struct tp_mouse_report {
697758
u8 padding[4];
698759
};
699760

700-
static inline int le16_to_int(__le16 x)
701-
{
702-
return (signed short)le16_to_cpu(x);
703-
}
704-
705761
static void report_finger_data(struct input_dev *input, int slot,
706762
const struct input_mt_pos *pos,
707763
const struct tp_finger *f)
@@ -997,6 +1053,7 @@ static int magicmouse_setup_input_mtp(struct input_dev *input,
9971053
{
9981054
int error;
9991055
int mt_flags = 0;
1056+
struct magicmouse_sc *msc = hid_get_drvdata(hdev);
10001057

10011058
__set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
10021059
__clear_bit(BTN_0, input->keybit);
@@ -1062,6 +1119,18 @@ static int magicmouse_setup_input_mtp(struct input_dev *input,
10621119
if (error)
10631120
return error;
10641121

1122+
/*
1123+
* Override the default input->open function to send the MT
1124+
* enable every time the device is opened. This ensures it works
1125+
* even if we missed a reset event due to the device being closed.
1126+
* input->close is overridden for symmetry.
1127+
*
1128+
* This also takes care of the dimensions query.
1129+
*/
1130+
input->open = magicmouse_open;
1131+
input->close = magicmouse_close;
1132+
msc->query_dimensions = true;
1133+
10651134
return 0;
10661135
}
10671136

@@ -1072,15 +1141,6 @@ static int magicmouse_setup_input_spi(struct input_dev *input,
10721141
if (ret)
10731142
return ret;
10741143

1075-
/*
1076-
* Override the default input->open function to send the MT
1077-
* enable every time the device is opened. This ensures it works
1078-
* even if we missed a reset event due to the device being closed.
1079-
* input->close is overridden for symmetry.
1080-
*/
1081-
input->open = magicmouse_open;
1082-
input->close = magicmouse_close;
1083-
10841144
return 0;
10851145
}
10861146

0 commit comments

Comments
 (0)