From f3819dbd2dc76197ce028d6d6f66dedc16072b0b Mon Sep 17 00:00:00 2001
From: Ondrej Jirman <megi@xff.cz>
Date: Sun, 14 Aug 2022 16:23:28 +0200
Subject: [PATCH 352/463] usb: typec: tcpm: Unregister altmodes before
 registering new ones

Just in case there are already altmodes registered. Otherwise the
kernel will fail with:

[50554.467172] sysfs: cannot create duplicate filename '/bus/typec/devices/port0-partner.0'
[50554.467198] CPU: 2 PID: 897 Comm: 4-0022 Tainted: G         C        5.18.15-1-MANJARO-ARM #1
[50554.467208] Hardware name: Pine64 PinePhonePro (DT)
[50554.467214] Call trace:
[50554.467218]  dump_backtrace.part.0+0xcc/0xe0
[50554.467241]  show_stack+0x18/0x6c
[50554.467249]  dump_stack_lvl+0x64/0x80
[50554.467262]  dump_stack+0x18/0x34
[50554.467270]  sysfs_warn_dup+0x60/0x7c
[50554.467279]  sysfs_do_create_link_sd+0xf0/0x100
[50554.467286]  sysfs_create_link+0x24/0x44
[50554.467292]  bus_add_device+0x64/0x120
[50554.467304]  device_add+0x320/0x890
[50554.467312]  device_register+0x20/0x30
[50554.467318]  typec_register_altmode+0x1e4/0x35c
[50554.467330]  typec_partner_register_altmode+0x10/0x20
[50554.467339]  tcpm_pd_rx_handler+0x1804/0x18e0
[50554.467346]  kthread_worker_fn+0xa0/0x180
[50554.467357]  kthread+0x108/0x10c
[50554.467364]  ret_from_fork+0x10/0x20
[50554.467528] typec port0-partner: failed to register alternate mode (-17)

Signed-off-by: Ondrej Jirman <megi@xff.cz>
---
 drivers/usb/typec/tcpm/tcpm.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/drivers/usb/typec/tcpm/tcpm.c b/drivers/usb/typec/tcpm/tcpm.c
index b1c12ddd43f75..092ecba722caa 100644
--- a/drivers/usb/typec/tcpm/tcpm.c
+++ b/drivers/usb/typec/tcpm/tcpm.c
@@ -1752,6 +1752,9 @@ static void tcpm_register_partner_altmodes(struct tcpm_port *port)
 		return;
 
 	for (i = 0; i < modep->altmodes; i++) {
+		typec_unregister_altmode(port->partner_altmode[i]);
+		port->partner_altmode[i] = NULL;
+
 		altmode = typec_partner_register_altmode(port->partner,
 						&modep->altmode_desc[i]);
 		if (IS_ERR(altmode)) {
-- 
2.46.0

