Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions drivers/hid/dockchannel-hid/dockchannel-hid-protocol-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,65 @@ static void dchid_pm1_fallback_test(struct kunit *test)
false, false, true));
}

/* A TP26 data frame: header, one record of @id carrying the 32-bit @word. */
static size_t dchid_tp26_frame(u8 *buf, const u8 *id, u32 word, u16 len)
{
size_t pos = DCHID_TP26_HEADER_SIZE;

memset(buf, 0, DCHID_TP26_HEADER_SIZE);
buf[0] = 0x5a;
buf[1] = 0x01;
buf[2] = 2;
buf[pos] = 0x02;
memcpy(buf + pos + 1, id, 16);
buf[pos + 17] = 0x02;
put_unaligned_le16(len, buf + pos + 20);
put_unaligned_le32(word, buf + pos + DCHID_TP26_RECORD_HDR);
return pos + DCHID_TP26_RECORD_HDR + 4;
}

static void dchid_tp26_clicked_test(struct kunit *test)
{
static const u8 contacts_id[16] = {
0x91, 0x62, 0x0e, 0x00, 0x8a, 0x5d, 0x81, 0xd5,
0x7e, 0xde, 0x54, 0xa8, 0x63, 0x1f, 0xdf, 0x17,
};
u8 buf[64];
size_t n;

n = dchid_tp26_frame(buf, dchid_tp26_button_id, 1, 4);
KUNIT_EXPECT_TRUE(test, dchid_tp26_clicked(buf, n));

n = dchid_tp26_frame(buf, dchid_tp26_button_id, 0, 4);
KUNIT_EXPECT_FALSE(test, dchid_tp26_clicked(buf, n));

/* Another stream with the bit set is not a click */
n = dchid_tp26_frame(buf, contacts_id, 1, 4);
KUNIT_EXPECT_FALSE(test, dchid_tp26_clicked(buf, n));

/* A record that runs past the frame */
n = dchid_tp26_frame(buf, dchid_tp26_button_id, 1, 40);
KUNIT_EXPECT_FALSE(test, dchid_tp26_clicked(buf, n));

/* Not a data frame, or not TP26 at all */
n = dchid_tp26_frame(buf, dchid_tp26_button_id, 1, 4);
buf[2] = 1;
KUNIT_EXPECT_FALSE(test, dchid_tp26_clicked(buf, n));
buf[2] = 2;
buf[0] = 0x75;
KUNIT_EXPECT_FALSE(test, dchid_tp26_clicked(buf, n));

/* Too short for the header */
KUNIT_EXPECT_FALSE(test, dchid_tp26_clicked(buf, DCHID_TP26_HEADER_SIZE - 1));
}

static struct kunit_case dchid_protocol_test_cases[] = {
KUNIT_CASE(dchid_pm1_command_test),
KUNIT_CASE(dchid_pm2_will_change_test),
KUNIT_CASE(dchid_pm2_has_changed_test),
KUNIT_CASE(dchid_pm2_power_off_test),
KUNIT_CASE(dchid_pm1_fallback_test),
KUNIT_CASE(dchid_tp26_clicked_test),
{}
};

Expand Down
38 changes: 38 additions & 0 deletions drivers/hid/dockchannel-hid/dockchannel-hid-protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <linux/errno.h>
#include <linux/string.h>
#include <linux/types.h>
#include <linux/unaligned.h>

/*
* Interface power commands go to the comm interface as SET_REPORT feature
Expand Down Expand Up @@ -65,4 +66,41 @@ static inline void dchid_pm2_command(u8 iface, u8 state, bool has_changed,
cmd[4] = has_changed;
}

/*
* Newer trackpad firmware ("SensingAlgsTrackpad26C", hid-magicmouse's TP26
* frames) sends no report ID: a frame is 0x5a 0x01, a frame type (2 for data),
* a header to offset 31, then records of 0x02, a 16-byte stream id, 0x02, two
* type bytes, a little-endian 16-bit length and the payload. The button
* stream's payload is a 32-bit word with bit 0 set while the pad is clicked.
*/
#define DCHID_TP26_HEADER_SIZE 31
#define DCHID_TP26_RECORD_HDR 22

static const u8 dchid_tp26_button_id[16] = {
0x91, 0x62, 0x0e, 0x00, 0x8a, 0x5d, 0x81, 0xd5,
0xc2, 0x87, 0x93, 0x63, 0x28, 0x29, 0xc8, 0xbf,
};

/* Whether a TP26 data frame carries a button stream record that says clicked. */
static inline bool dchid_tp26_clicked(const u8 *data, size_t size)
{
size_t pos;

if (size < DCHID_TP26_HEADER_SIZE || data[0] != 0x5a || data[1] != 0x01 ||
data[2] != 2)
return false;
for (pos = DCHID_TP26_HEADER_SIZE; pos + DCHID_TP26_RECORD_HDR <= size;) {
u16 len = get_unaligned_le16(data + pos + 20);

if (data[pos] != 0x02 || data[pos + 17] != 0x02 ||
len > size - pos - DCHID_TP26_RECORD_HDR)
return false;
if (len >= 4 && !memcmp(data + pos + 1, dchid_tp26_button_id,
sizeof(dchid_tp26_button_id)))
return get_unaligned_le32(data + pos + DCHID_TP26_RECORD_HDR) & 1;
pos += DCHID_TP26_RECORD_HDR + len;
}
return false;
}

#endif /* _DOCKCHANNEL_HID_PROTOCOL_H */
13 changes: 8 additions & 5 deletions drivers/hid/dockchannel-hid/dockchannel-hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -1511,8 +1511,9 @@ static void dchid_handle_ack(struct dchid_iface *iface, struct dchid_hdr *hdr, v

/*
* Whether a report should wake the system: any keyboard report, and a
* multi-touch report only while the trackpad is clicked. Touches, the
* accelerometer and the other interfaces do not wake it.
* multi-touch report only while the trackpad is clicked, in the MTP report or
* in a TP26 frame's button stream. Touches, the accelerometer and the other
* interfaces do not wake it.
*/
static bool dchid_wake_report(struct dchid_iface *iface, void *data)
{
Expand All @@ -1523,9 +1524,11 @@ static bool dchid_wake_report(struct dchid_iface *iface, void *data)
return false;
if (!strcmp(iface->name, "keyboard"))
return shdr->length > 0;
return !strcmp(iface->name, "multi-touch") &&
shdr->length > MT_REPORT_BUTTON && payload[0] == MT_REPORT_ID &&
(payload[MT_REPORT_BUTTON] & 1);
if (strcmp(iface->name, "multi-touch"))
return false;
if (shdr->length > MT_REPORT_BUTTON && payload[0] == MT_REPORT_ID)
return payload[MT_REPORT_BUTTON] & 1;
return dchid_tp26_clicked(payload, shdr->length);
}

/* Return the complete frame size, or reject a lost framing boundary. */
Expand Down
Loading