Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ properties:
oneOf:
- const: apple,dockchannel-hid
- items:
- const: apple,t6041-dockchannel-hid
- enum:
- apple,t6041-dockchannel-hid
- apple,t6050-dockchannel-hid
- const: apple,dockchannel-hid

reg:
Expand Down
2 changes: 1 addition & 1 deletion arch/arm64/boot/dts/apple/t6050-k114-k116.dtsi
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@
*/
&mtp_dockchannel {
mtp_hid: input@8000 {
compatible = "apple,dockchannel-hid";
compatible = "apple,t6050-dockchannel-hid", "apple,dockchannel-hid";
reg = <0x8000 0x1000>, <0xc000 0x1000>,
<0x0 0x1000>, <0x4000 0x1000>;
reg-names = "config", "data", "rmt-config", "rmt-data";
Expand Down
2 changes: 2 additions & 0 deletions drivers/hid/dockchannel-hid/dockchannel-hid-parser-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ static void *dchid_test_ring(struct kunit *test, u32 consumer, u32 producer)
dchid->ring_read = consumer;
dchid->ring_registered = true;
dchid->use_ring = true;
dchid->use_rx_ring = true;
dchid->ring_size = DCHID_RING_SIZE;
return dchid->ring + DCHID_RING_HEADER_SIZE;
}

Expand Down
35 changes: 35 additions & 0 deletions drivers/hid/dockchannel-hid/dockchannel-hid-protocol-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,46 @@ static void dchid_pm2_power_off_test(struct kunit *test)
KUNIT_EXPECT_MEMEQ(test, cmd, expected, sizeof(expected));
}

static void dchid_ring_refusal_keeps_fifo_test(struct kunit *test)
{
/* A node with an MMIO FIFO receive path of its own may fall back to it. */
KUNIT_EXPECT_TRUE(test, dchid_ring_refused(-ETIMEDOUT, true));
}

static void dchid_ring_refusal_fails_start_without_fallback_test(struct kunit *test)
{
/* The T6041 ring is its only receive path: a refusal must fail the start. */
KUNIT_EXPECT_FALSE(test, dchid_ring_refused(-ETIMEDOUT, false));
}

static void dchid_ring_success_is_not_a_refusal_test(struct kunit *test)
{
KUNIT_EXPECT_FALSE(test, dchid_ring_refused(0, true));
}

static void dchid_ring_failed_prepare_is_not_a_refusal_test(struct kunit *test)
{
/* CMD_REGISTER_RING was never sent, so the ring is not refused: stay unregistered and retry. */
KUNIT_EXPECT_FALSE(test, dchid_ring_stays_on_fifo(false, -ETIMEDOUT, true));
}

static void dchid_ring_refused_register_stays_on_fifo_test(struct kunit *test)
{
KUNIT_EXPECT_TRUE(test, dchid_ring_stays_on_fifo(true, -ETIMEDOUT, true));
KUNIT_EXPECT_FALSE(test, dchid_ring_stays_on_fifo(true, -ETIMEDOUT, false));
KUNIT_EXPECT_FALSE(test, dchid_ring_stays_on_fifo(true, 0, true));
}

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_ring_refusal_keeps_fifo_test),
KUNIT_CASE(dchid_ring_refusal_fails_start_without_fallback_test),
KUNIT_CASE(dchid_ring_success_is_not_a_refusal_test),
KUNIT_CASE(dchid_ring_failed_prepare_is_not_a_refusal_test),
KUNIT_CASE(dchid_ring_refused_register_stays_on_fifo_test),
{}
};

Expand Down
21 changes: 21 additions & 0 deletions drivers/hid/dockchannel-hid/dockchannel-hid-protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,25 @@ static inline void dchid_pm2_command(u8 iface, u8 state, bool has_changed,
cmd[4] = has_changed;
}

/*
* Whether a failed receive-ring command is to be absorbed. Only a node that
* has an MMIO FIFO receive path of its own (fifo_fallback) may fall back to
* it; the T6041 ring is its only receive path and its failure must fail the
* interface start.
*/
static inline bool dchid_ring_refused(int ret, bool fifo_fallback)
{
return ret < 0 && fifo_fallback;
}

/*
* Whether the ring stays unused for the session. Only a refused CMD_REGISTER_RING does:
* when the preparation command failed, CMD_REGISTER_RING was never sent, so nothing
* was refused and a later interface start may register the ring.
*/
static inline bool dchid_ring_stays_on_fifo(bool prepared, int ret, bool fifo_fallback)
{
return prepared && dchid_ring_refused(ret, fifo_fallback);
}

#endif /* _DOCKCHANNEL_HID_PROTOCOL_H */
74 changes: 57 additions & 17 deletions drivers/hid/dockchannel-hid/dockchannel-hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,13 @@ struct dockchannel_hid {
struct gpio_desc *afe_reset;

struct mutex ring_mutex;
/* The T8132 transport changes reset and GPIO handling as well as receives. */
bool use_ring;
/* Receive through a host-memory ring while keeping the local reset paths. */
bool use_rx_ring;
bool ring_registered;
int ring_error;
size_t ring_size;
void *ring;
dma_addr_t ring_dma;
u32 ring_read;
Expand Down Expand Up @@ -544,28 +548,52 @@ static int dchid_register_ring(struct dockchannel_hid *dchid)
} __packed msg = {
.cmd = CMD_REGISTER_RING,
.addr = dchid->ring_dma,
.size = DCHID_RING_SIZE,
.size = dchid->ring_size,
};
/* Observed in both macOS and standalone T8132 bringup before 0x91. */
u8 prepare[] = { CMD_HOST_STATE, HOST_STATE_AWAKE };
int ret = 0;
int ret = 0, cmd_ret;
bool fifo_fallback = !dchid->use_ring;
bool prepared;

if (!dchid->use_ring)
if (!dchid->use_rx_ring)
return 0;

mutex_lock(&dchid->ring_mutex);
ret = dchid->ring_error;
if (!dchid->ring_registered) {
ret = dchid_comm_cmd(dchid, prepare, sizeof(prepare));
if (ret < 0) {
mutex_unlock(&dchid->ring_mutex);
return ret;
cmd_ret = dchid_comm_cmd(dchid, prepare, sizeof(prepare));
prepared = cmd_ret >= 0;
if (prepared) {
/* The registration ACK itself arrives through the ring. */
dma_wmb();
WRITE_ONCE(dchid->ring_registered, true);
cmd_ret = dchid_comm_cmd(dchid, &msg, sizeof(msg));
dchid->ring_error = cmd_ret;
}
ret = cmd_ret;
if (dchid_ring_stays_on_fifo(prepared, cmd_ret, fifo_fallback)) {
/*
* Stay on the FIFO. Keep the ring marked registered, so a
* ring the firmware did accept still drains, and clear the
* sticky error so later interface starts do not fail.
*/
WRITE_ONCE(dchid->ring_registered, true);
dchid->ring_error = 0;
dev_warn(dchid->dev,
"receive ring refused (%d), staying on FIFO\n", cmd_ret);
ret = 0;
} else if (!prepared && fifo_fallback) {
/*
* The preparation command failed and the ring was never asked for.
* The FIFO still works, so the start goes on, but the ring stays
* unregistered and the next interface start tries again.
*/
dev_warn(dchid->dev,
"receive ring not registered, host state failed (%d); using the FIFO for now\n",
cmd_ret);
ret = 0;
}
/* The registration ACK itself arrives through the ring. */
dma_wmb();
WRITE_ONCE(dchid->ring_registered, true);
ret = dchid_comm_cmd(dchid, &msg, sizeof(msg));
dchid->ring_error = ret;
}
mutex_unlock(&dchid->ring_mutex);
return ret;
Expand Down Expand Up @@ -1450,7 +1478,7 @@ static int dchid_dispatch(struct dockchannel_hid *dchid, struct dchid_hdr *hdr,
return 0;
}

if (dchid->use_ring && hdr->channel == DCHID_CHANNEL_NOTIFY) {
if (dchid->use_rx_ring && hdr->channel == DCHID_CHANNEL_NOTIFY) {
/* A FIFO notification has no application subheader or report ID. */
return fifo && !hdr->length && hdr->iface == IFACE_COMM;
}
Expand Down Expand Up @@ -1496,7 +1524,7 @@ static void dchid_ring_copy(struct dockchannel_hid *dchid, void *dst,
u32 offset, size_t length)
{
size_t first = min_t(size_t, length,
DCHID_RING_SIZE - DCHID_RING_HEADER_SIZE - offset);
dchid->ring_size - DCHID_RING_HEADER_SIZE - offset);
void *data = dchid->ring + DCHID_RING_HEADER_SIZE;

memcpy(dst, data + offset, first);
Expand All @@ -1505,7 +1533,7 @@ static void dchid_ring_copy(struct dockchannel_hid *dchid, void *dst,

static int dchid_drain_ring(struct dockchannel_hid *dchid)
{
const u32 capacity = DCHID_RING_SIZE - DCHID_RING_HEADER_SIZE;
const u32 capacity = dchid->ring_size - DCHID_RING_HEADER_SIZE;
u32 *indices = dchid->ring;
u32 producer, consumer, available;
int size;
Expand Down Expand Up @@ -1662,6 +1690,18 @@ static int dockchannel_hid_probe(struct platform_device *pdev)
mutex_init(&dchid->ring_mutex);
/* The M4 Pro (J616s) MTP uses the same 26.6.2 firmware transport as T8132. */
dchid->use_ring = of_device_is_compatible(dev->of_node, "apple,t6041-dockchannel-hid");
/*
* The T6050 MTP keeps this platform's reset and GPIO paths, but its large
* raw touch reports need a bigger receive path: the T6050 MTP compatible
* selects the ring and its size, the 20 MiB introduced for it in #2015
* (the T6041 path keeps its 2 MiB). The ring needs the node's MTP DART
* streams 0 and 1 described (see #2014). Try that receive transport and
* fall back to the MMIO FIFO if the firmware refuses it.
*/
dchid->use_rx_ring = dchid->use_ring ||
of_device_is_compatible(dev->of_node, "apple,t6050-dockchannel-hid");
dchid->ring_size = of_device_is_compatible(dev->of_node, "apple,t6050-dockchannel-hid") ?
20 * SZ_1M : DCHID_RING_SIZE;

ret = dchid_bind_afe_smc(dchid);
if (ret)
Expand Down Expand Up @@ -1782,8 +1822,8 @@ static int dockchannel_hid_probe(struct platform_device *pdev)
if (ret)
dev_warn(dev, "Failed to set up wakeup: %d\n", ret);

if (dchid->use_ring) {
dchid->ring = dmam_alloc_coherent(dev, DCHID_RING_SIZE, &dchid->ring_dma,
if (dchid->use_rx_ring) {
dchid->ring = dmam_alloc_coherent(dev, dchid->ring_size, &dchid->ring_dma,
GFP_KERNEL);
if (!dchid->ring)
return -ENOMEM;
Expand Down
Loading