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
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ properties:
the J714s) that the controller's LOCK command takes to unlock the
commands that need it. These are its debug mode, for rearming the
debug link after a sleep ended it, and a disconnect and reattach of
its partner (DISC).
its partner (DISC). It marks a port that may carry a debug link, and
whose partner the OS may replug on its own after a sleep. A port
without it needs no key here for a replug the user asks for, the key
is then the board number of the machine's compatible, reversed.

apple,event-irq-in-sleep:
type: boolean
Expand Down
1 change: 1 addition & 0 deletions arch/arm64/configs/asahi.config
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ CONFIG_USB4=m
CONFIG_USB4_APPLE_SOC=m
CONFIG_USB_XHCI_PCI_ASMEDIA=y
CONFIG_TYPEC_SN201202X=m
CONFIG_TYPEC_TPS6598X_DEBUG_COMMANDS=y
CONFIG_RTC_DRV_MACSMC=y
CONFIG_RTC_DRV_APPLE_PMU=y
CONFIG_APPLE_ADMAC=m
Expand Down
12 changes: 10 additions & 2 deletions drivers/usb/typec/tipd/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ config TYPEC_SN201202X
config TYPEC_TPS6598X_DEBUG_COMMANDS
bool "Debugfs command access to TPS6598x/CD321x controllers"
depends on TYPEC_TPS6598X_CORE && DEBUG_FS
default y if ARCH_APPLE
help
Add a root-only debugfs file, usb/tipd/<device>/command, that sends
a few controller commands (LOCK, DBMa, VDMs, DVEn and DISC) to the
Expand All @@ -42,7 +43,13 @@ config TYPEC_TPS6598X_DEBUG_COMMANDS
device. The read-only register file, usb/tipd/<device>/regs, is
available with DEBUG_FS whether or not this is set.

Say N unless you use the controller's debug mode for development.
With kernel lockdown, in integrity or confidentiality mode, opening
the command file (and usb/tipd/<device>/replug) fails with EPERM.
The regs file can still be read.

Say Y on Apple silicon, so that a hung Mac on a USB-C cable can be
rebooted from this one. Say N elsewhere, unless you use the
controller's debug mode for development.

config TYPEC_TIPD_KUNIT_TEST
tristate "KUnit tests for CD321x" if !KUNIT_ALL_TESTS
Expand All @@ -67,7 +74,8 @@ config TYPEC_TIPD_KUNIT_TEST
nothing, and that other providers and errors keep the bounded
retries.
They also cover which DisplayPort hotplug event a cable update sends
to the display connector.
to the display connector, and the LOCK key derived from the board's
number.
These tests run without accessing a physical Type-C controller.

config TYPEC_SN201202X_KUNIT_TEST
Expand Down
3 changes: 2 additions & 1 deletion drivers/usb/typec/tipd/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ obj-$(CONFIG_TYPEC_SN201202X) += sn201202x.o
sn201202x-y := spmi.o

obj-$(CONFIG_TYPEC_TIPD_KUNIT_TEST) += cd321x-pm-test.o cd321x-tbt-test.o \
cd321x-irq-test.o cd321x-mux-test.o cd321x-dp-test.o
cd321x-irq-test.o cd321x-mux-test.o cd321x-dp-test.o \
tipd-key-test.o
obj-$(CONFIG_TYPEC_SN201202X_KUNIT_TEST) += sn201202x-pm-test.o
121 changes: 64 additions & 57 deletions drivers/usb/typec/tipd/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -3781,40 +3781,50 @@ static int cd321x_remove(struct tps6598x *tps)
}

/*
* An SN201202x takes "DISC" unlocked with the key its DT gives it (apple,debug-lock-key, the board
* number reversed).
* The LOCK key of the board with the machine compatible @compatible: the letter and the three
* digits of its board number, reversed. "apple,j414c" gives "414J", "apple,k114s" "411K".
*/
static int tipd_replug_key(struct tps6598x *tps, u8 key[4])
VISIBLE_IF_KUNIT int tipd_board_lock_key(const char *compatible, u8 key[4])
{
size_t len = str_has_prefix(compatible, "apple,");
const char *board = compatible + len;

/* Short-circuits at the end of the string. A digit after the three is a SoC's number. */
if (!len || !isalpha(board[0]) || !isdigit(board[1]) || !isdigit(board[2]) ||
!isdigit(board[3]) || isdigit(board[4]))
return -EOPNOTSUPP;
key[0] = board[3];
key[1] = board[2];
key[2] = board[1];
key[3] = toupper(board[0]);
return 0;
}
EXPORT_SYMBOL_IF_KUNIT(tipd_board_lock_key);

/* The LOCK key a controller's DT gives it, apple,debug-lock-key. */
static int tipd_dt_lock_key(struct tps6598x *tps, u8 key[4])
{
const char *s;

if (!device_is_compatible(tps->dev, "apple,sn201202x") ||
device_property_read_string(tps->dev, "apple,debug-lock-key", &s) || strlen(s) != 4)
if (device_property_read_string(tps->dev, "apple,debug-lock-key", &s) || strlen(s) != 4)
return -EOPNOTSUPP;
memcpy(key, s, 4);
return 0;
}

/*
* A CD321x takes the board number reversed as its LOCK key, as the J716c's does ("617J").
* It comes from the machine's compatible: "apple,j414c" gives "414J".
*/
static int cd321x_board_key(u8 key[4])
/* The LOCK key of an Apple controller: the one its DT gives it, or the board's. */
static int tipd_lock_key(struct tps6598x *tps, u8 key[4])
{
struct device_node *root = of_find_node_by_path("/");
const char *s;
int ret = -EOPNOTSUPP;
const char *machine;

if (root && !of_property_read_string(root, "compatible", &s) &&
!strncmp(s, "apple,j", 7) && isdigit(s[7]) && isdigit(s[8]) && isdigit(s[9])) {
key[0] = s[9];
key[1] = s[8];
key[2] = s[7];
key[3] = 'J';
ret = 0;
}
of_node_put(root);
return ret;
if (!device_is_compatible(tps->dev, "apple,sn201202x") &&
!device_is_compatible(tps->dev, "apple,cd321x"))
return -EOPNOTSUPP;
if (!tipd_dt_lock_key(tps, key))
return 0;
if (of_machine_read_compatible(&machine, 0))
return -EOPNOTSUPP;
return tipd_board_lock_key(machine, key);
}

/*
Expand All @@ -3826,7 +3836,7 @@ static int cd321x_board_key(u8 key[4])
* The renegotiation's DISC takes the controller unlocked with the board's LOCK key, the board
* number reversed. A port whose DT gives it (apple,debug-lock-key) is left as it was if the
* renegotiation fails. A port whose DT does not give it (the J716c's) takes the key from the
* board number (cd321x_board_key()), and a failure there fails the probe.
* board number (tipd_lock_key()), and a failure there fails the probe.
*/

static int __cd321x_reset_inherited_connection(struct tps6598x *tps, const u8 key[4],
Expand Down Expand Up @@ -3887,7 +3897,7 @@ static int __cd321x_reset_inherited_connection(struct tps6598x *tps, const u8 ke

static int cd321x_reset_inherited_connection(struct tps6598x *tps, u32 *status)
{
bool best_effort = true;
bool best_effort;
u32 fresh;
u8 key[4];
int ret;
Expand All @@ -3897,13 +3907,11 @@ static int cd321x_reset_inherited_connection(struct tps6598x *tps, u32 *status)
tps->dbma_at_probe || !(*status & TPS_STATUS_PLUG_PRESENT) ||
!TPS_STATUS_TO_TYPEC_DATAROLE(*status))
return 0;
if (tipd_replug_key(tps, key)) {
if (cd321x_board_key(key)) {
dev_warn(tps->dev,
"inherited connection not reinitialized: no apple,debug-lock-key\n");
return 0;
}
best_effort = false;
best_effort = !tipd_dt_lock_key(tps, key);
if (!best_effort && tipd_lock_key(tps, key)) {
dev_warn(tps->dev,
"inherited connection not reinitialized: no apple,debug-lock-key\n");
return 0;
}

ret = __cd321x_reset_inherited_connection(tps, key, status);
Expand Down Expand Up @@ -3945,7 +3953,7 @@ static int tipd_read_plug_state(struct tps6598x *tps)
return 0;
}

/* The ports tipd_port_replug() looks up by the USB role switch their data role drives. */
/* The ports that can be replugged; tipd_port_replug() looks one up by its USB role switch. */
static LIST_HEAD(tipd_ports);
static DEFINE_MUTEX(tipd_ports_lock);

Expand All @@ -3955,8 +3963,6 @@ static void tipd_port_list_add(struct tps6598x *tps)
{
INIT_LIST_HEAD(&tps->port_node);
INIT_WORK(&tps->replug_work, tipd_replug_work);
if (!tps->role_sw)
return;
mutex_lock(&tipd_ports_lock);
list_add_tail(&tps->port_node, &tipd_ports);
mutex_unlock(&tipd_ports_lock);
Expand All @@ -3972,11 +3978,14 @@ static void tipd_port_list_del(struct tps6598x *tps)
cancel_work_sync(&tps->replug_work);
}

/* A port with a connector (MagSafe's controller has none) and a known LOCK key. */
bool tipd_replug_supported(struct tps6598x *tps)
{
struct fwnode_handle *con = device_get_named_child_node(tps->dev, "connector");
u8 key[4];

return !tipd_replug_key(tps, key);
fwnode_handle_put(con);
return con && !tipd_lock_key(tps, key);
}

/*
Expand All @@ -3999,12 +4008,7 @@ static void tipd_replug_work(struct work_struct *work)
u8 key[4];

mutex_lock(&tps->lock);
if (tps->kept_attach_replug) {
memcpy(key, tps->kept_attach_key, sizeof(key));
ret = 0;
} else {
ret = tipd_replug_key(tps, key);
}
ret = tipd_lock_key(tps, key);
if (!ret)
ret = tps6598x_exec_cmd_gap(tps, "LOCK", sizeof(key), key, sizeof(result), &result,
1000, 0, CD321X_SSPS_READ_GAP_US);
Expand Down Expand Up @@ -4040,7 +4044,6 @@ static void tipd_replug_work(struct work_struct *work)
msleep(500);
}

tps->kept_attach_replug = false;
if (ret || lock_ret)
dev_warn(tps->dev, "port replug for its partner failed: %d (unlock result %u), locked again: %d (try %u)\n",
ret, result, lock_ret, tries);
Expand All @@ -4055,8 +4058,6 @@ static int tipd_replug_queue(struct tps6598x *tps, u8 seconds)
int ret;

lockdep_assert_held(&tipd_ports_lock);
if (!tipd_replug_supported(tps))
return -EOPNOTSUPP;
if (work_pending(&tps->replug_work))
return -EBUSY;
tps->replug_seconds = seconds;
Expand All @@ -4077,19 +4078,26 @@ static int tipd_replug_queue(struct tps6598x *tps, u8 seconds)
* DP alt mode hub whose SuperSpeed side does not come back after some wakes (dwc3-apple). A
* display on the port goes dark meanwhile. The replug runs on the port's own work, after this
* returns: 0 when it is queued, -EBUSY if one already is, -EOPNOTSUPP on a controller that is
* not known to take it (an SN201202x whose DT gives apple,debug-lock-key), -ENODEV if no port
* drives @sw.
* not known to take it unasked (an SN201202x whose DT gives apple,debug-lock-key), -ENODEV if no
* port drives @sw.
*/
int tipd_port_replug(struct usb_role_switch *sw, u8 seconds)
{
struct tps6598x *tps;
int ret = -ENODEV;
u8 key[4];

if (!sw)
return -ENODEV;
mutex_lock(&tipd_ports_lock);
list_for_each_entry(tps, &tipd_ports, port_node) {
if (tps->role_sw != sw)
continue;
ret = tipd_replug_queue(tps, seconds);
if (device_is_compatible(tps->dev, "apple,sn201202x") &&
!tipd_dt_lock_key(tps, key))
ret = tipd_replug_queue(tps, seconds);
else
ret = -EOPNOTSUPP;
break;
}
mutex_unlock(&tipd_ports_lock);
Expand Down Expand Up @@ -4123,7 +4131,8 @@ static void cd321x_replug_kept_attach(struct tps6598x *tps)
u32 status;
int ret;

if (!device_is_compatible(tps->dev, "apple,cd321x"))
if (!device_is_compatible(tps->dev, "apple,cd321x") || !tps->role_sw ||
!tipd_replug_supported(tps))
return;
scoped_guard(mutex, &tps->lock) {
if (!tps6598x_read_status(tps, &status) || (status & TPS_STATUS_PLUG_PRESENT))
Expand All @@ -4132,31 +4141,29 @@ static void cd321x_replug_kept_attach(struct tps6598x *tps)
(state[CD321X_ATTACH_STATE_BYTE] & kept) != kept)
return;
}
if (cd321x_board_key(tps->kept_attach_key))
return;

guard(mutex)(&tipd_ports_lock);
if (!tps->port_node.next || list_empty(&tps->port_node) || work_pending(&tps->replug_work))
return;
tps->kept_attach_replug = true;
tps->replug_seconds = CD321X_KEPT_ATTACH_REPLUG_S;
ret = queue_work(system_freezable_wq, &tps->replug_work) ? 0 : -EBUSY;
if (ret)
tps->kept_attach_replug = false;
dev_info(tps->dev, "no partner, but the controller kept an earlier attach (%#04x): replugging the port: %d\n",
state[CD321X_ATTACH_STATE_BYTE], ret);
}

/*
* The same replug for this port, asked for through debugfs ("replug"). -ENODEV while the port is
* not on tipd_ports: without a role switch, and from tipd_port_list_del() on, which cancels the
* work after taking the port off, so nothing is queued after it.
* The same replug for this port, asked for through debugfs ("replug"). It needs neither a USB role
* switch (the J700's ports have none, its host is fixed) nor a key in the DT. -ENODEV while the
* port is not on tipd_ports: from tipd_port_list_del() on, which cancels the work after taking the
* port off, so nothing is queued after it.
*/
int tipd_replug_request(struct tps6598x *tps, u8 seconds)
{
guard(mutex)(&tipd_ports_lock);
if (!tps->port_node.next || list_empty(&tps->port_node))
return -ENODEV;
if (!tipd_replug_supported(tps))
return -EOPNOTSUPP;
return tipd_replug_queue(tps, seconds);
}

Expand Down
7 changes: 4 additions & 3 deletions drivers/usb/typec/tipd/debugfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
* read-only register access: pread() with the register as offset
* (0x00..0x7f) and the length (1..64 bytes) as count.
* /sys/kernel/debug/usb/tipd/<device>/replug
* Only on a port tipd can replug its partner on (an SN201202x whose DT
* gives apple,debug-lock-key, with a USB role switch).
* Only on a port tipd can replug its partner on (an SN201202x or CD321x
* with a connector, whose LOCK key is known from apple,debug-lock-key
* or from the board number in the machine's compatible).
* write: the seconds (1..10) the partner is disconnected for. One replug,
* queued as the driver queues its own after a sleep (see
* tipd_port_replug()); -EBUSY while one is queued and not yet
Expand Down Expand Up @@ -316,7 +317,7 @@ void tipd_debugfs_register(struct tps6598x *tps)
&tipd_debugfs_command_fops);
debugfs_create_file("regs", 0400, dbg->dentry, tps,
&tipd_debugfs_regs_fops);
if (tipd_replug_supported(tps) && tps->role_sw)
if (tipd_replug_supported(tps))
debugfs_create_file("replug", 0200, dbg->dentry, tps,
&tipd_debugfs_replug_fops);
}
Expand Down
Loading
Loading