1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
// SPDX-License-Identifier: GPL-2.0
/*
* UCSI Thunderbolt Alternate Mode Support
*
* Copyright 2026 Google LLC
*/
#include <linux/usb/typec_tbt.h>
#include <linux/usb/pd_vdo.h>
#include <linux/err.h>
#include <linux/dev_printk.h>
#include <linux/device/devres.h>
#include <linux/gfp_types.h>
#include <linux/types.h>
#include <linux/usb/typec_altmode.h>
#include <linux/workqueue.h>
#include "ucsi.h"
/**
* struct ucsi_tbt - Thunderbolt Alternate Mode private data structure
* @con: Pointer to UCSI connector structure
* @alt: Pointer to typec altmode structure
* @work: Work structure
* @cam: An offset into the list of alternate modes supported by the PPM
* @header: VDO header
*/
struct ucsi_tbt {
struct ucsi_connector *con;
struct typec_altmode *alt;
struct work_struct work;
int cam;
u32 header;
};
static void ucsi_thunderbolt_work(struct work_struct *work)
{
struct ucsi_tbt *tbt = container_of(work, struct ucsi_tbt, work);
if (typec_altmode_vdm(tbt->alt, tbt->header, NULL, 0))
dev_err(&tbt->alt->dev, "VDM 0x%x failed\n", tbt->header);
tbt->header = 0;
}
static int ucsi_thunderbolt_set_altmode(struct ucsi_tbt *tbt,
bool enter, u32 vdo)
{
int svdm_version;
int cmd;
int ret;
u64 command = UCSI_SET_NEW_CAM |
UCSI_CONNECTOR_NUMBER(tbt->con->num) |
UCSI_SET_NEW_CAM_SET_AM(tbt->cam) |
((u64)vdo << 32);
if (enter)
command |= (1 << 23);
ret = ucsi_send_command(tbt->con->ucsi, command, NULL, 0);
if (ret < 0)
return ret;
svdm_version = typec_altmode_get_svdm_version(tbt->alt);
if (svdm_version < 0)
return svdm_version;
if (enter)
cmd = CMD_ENTER_MODE;
else
cmd = CMD_EXIT_MODE;
tbt->header = VDO(USB_TYPEC_TBT_SID, 1, svdm_version, cmd);
tbt->header |= VDO_OPOS(TYPEC_TBT_MODE);
tbt->header |= VDO_CMDT(CMDT_RSP_ACK);
schedule_work(&tbt->work);
return 0;
}
static int ucsi_thunderbolt_enter(struct typec_altmode *alt, u32 *vdo)
{
struct ucsi_tbt *tbt = typec_altmode_get_drvdata(alt);
struct ucsi_connector *con = tbt->con;
u64 command;
u8 cur = 0;
int ret;
if (!ucsi_con_mutex_lock(con))
return -ENOTCONN;
command = UCSI_GET_CURRENT_CAM | UCSI_CONNECTOR_NUMBER(con->num);
ret = ucsi_send_command(con->ucsi, command, &cur, sizeof(cur));
if (ret < 0) {
if (con->ucsi->version > 0x0100)
goto err_unlock;
cur = 0xff;
}
if (cur != 0xff) {
if (cur >= UCSI_MAX_ALTMODES || con->port_altmode[cur] != alt)
ret = -EBUSY;
else
ret = 0;
goto err_unlock;
}
ret = ucsi_thunderbolt_set_altmode(tbt, true, *vdo);
ucsi_altmode_update_active(tbt->con);
err_unlock:
ucsi_con_mutex_unlock(con);
return ret;
}
static int ucsi_thunderbolt_exit(struct typec_altmode *alt)
{
struct ucsi_tbt *tbt = typec_altmode_get_drvdata(alt);
int ret;
if (!ucsi_con_mutex_lock(tbt->con))
return -ENOTCONN;
ret = ucsi_thunderbolt_set_altmode(tbt, false, 0);
ucsi_con_mutex_unlock(tbt->con);
return ret;
}
static int ucsi_thunderbolt_vdm(struct typec_altmode *alt,
u32 header, const u32 *data, int count)
{
struct ucsi_tbt *tbt = typec_altmode_get_drvdata(alt);
int cmd_type = PD_VDO_CMDT(header);
int cmd = PD_VDO_CMD(header);
int svdm_version;
if (!ucsi_con_mutex_lock(tbt->con))
return -ENOTCONN;
svdm_version = typec_altmode_get_svdm_version(alt);
if (svdm_version < 0) {
ucsi_con_mutex_unlock(tbt->con);
return svdm_version;
}
switch (cmd_type) {
case CMDT_INIT:
if (PD_VDO_SVDM_VER(header) < svdm_version) {
svdm_version = PD_VDO_SVDM_VER(header);
typec_partner_set_svdm_version(tbt->con->partner, svdm_version);
}
tbt->header = VDO(USB_TYPEC_TBT_SID, 1, svdm_version, cmd);
tbt->header |= VDO_OPOS(TYPEC_TBT_MODE);
tbt->header |= VDO_CMDT(CMDT_RSP_ACK);
schedule_work(&tbt->work);
break;
default:
break;
}
ucsi_con_mutex_unlock(tbt->con);
return 0;
}
static const struct typec_altmode_ops ucsi_thunderbolt_ops = {
.enter = ucsi_thunderbolt_enter,
.exit = ucsi_thunderbolt_exit,
.vdm = ucsi_thunderbolt_vdm,
};
struct typec_altmode *ucsi_register_thunderbolt(struct ucsi_connector *con,
bool override, int offset,
struct typec_altmode_desc *desc)
{
struct typec_altmode *alt;
struct ucsi_tbt *tbt;
alt = typec_port_register_altmode(con->port, desc);
if (IS_ERR(alt) || !override)
return alt;
tbt = devm_kzalloc(&alt->dev, sizeof(*tbt), GFP_KERNEL);
if (!tbt) {
typec_unregister_altmode(alt);
return ERR_PTR(-ENOMEM);
}
tbt->cam = offset;
tbt->con = con;
tbt->alt = alt;
INIT_WORK(&tbt->work, ucsi_thunderbolt_work);
typec_altmode_set_drvdata(alt, tbt);
typec_altmode_set_ops(alt, &ucsi_thunderbolt_ops);
return alt;
}
void ucsi_thunderbolt_remove_partner(struct typec_altmode *alt)
{
struct ucsi_tbt *tbt;
if (alt) {
tbt = typec_altmode_get_drvdata(alt);
if (tbt)
cancel_work_sync(&tbt->work);
}
}
|