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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
|
// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright 2025 Google LLC.
*/
#include <linux/types.h>
#include <linux/list_sort.h>
#include <linux/slab.h>
#include <linux/mutex.h>
#include <linux/workqueue.h>
#include <linux/usb/typec_altmode.h>
#include "class.h"
/**
* struct mode_state - State tracking for a specific Type-C alternate mode
* @svid: Standard or Vendor ID of the Alternate Mode
* @priority: Mode priority
* @error: Outcome of the last attempt to enter the mode
* @list: List head to link this mode state into a prioritized list
*/
struct mode_state {
u16 svid;
u8 priority;
int error;
struct list_head list;
};
/**
* struct mode_selection - Manages the selection and state of Alternate Modes
* @mode_list: Prioritized list of available Alternate Modes
* @lock: Mutex to protect mode_list
* @work: Work structure
* @partner: Handle to the Type-C partner device
* @active_svid: svid of currently active mode
* @timeout: Timeout for a mode entry attempt, ms
* @delay: Delay between mode entry/exit attempts, ms
*/
struct mode_selection {
struct list_head mode_list;
/* Protects the mode_list*/
struct mutex lock;
struct delayed_work work;
struct typec_partner *partner;
u16 active_svid;
unsigned int timeout;
unsigned int delay;
};
/**
* struct mode_order - Mode activation tracking
* @svid: Standard or Vendor ID of the Alternate Mode
* @enter: Flag indicating if the driver is currently attempting to enter or
* exit the mode
* @result: Outcome of the attempt to activate the mode
*/
struct mode_order {
u16 svid;
int enter;
int result;
};
static int activate_altmode(struct device *dev, void *data)
{
if (is_typec_partner_altmode(dev)) {
struct typec_altmode *alt = to_typec_altmode(dev);
struct mode_order *order = (struct mode_order *)data;
if (order->svid == alt->svid) {
if (alt->ops && alt->ops->activate)
order->result = alt->ops->activate(alt, order->enter);
else
order->result = -EOPNOTSUPP;
return 1;
}
}
return 0;
}
static int mode_selection_activate(struct mode_selection *sel,
const u16 svid, const int enter)
__must_hold(&sel->lock)
{
struct mode_order order = {.svid = svid, .enter = enter, .result = -ENODEV};
/*
* The port driver may acquire its internal mutex during alternate mode
* activation. Since this is the same mutex that may be held during the
* execution of typec_altmode_state_update(), it is crucial to release
* sel->mutex before activation to avoid potential deadlock.
* Note that sel->mode_list must remain invariant throughout this unlocked
* interval.
*/
mutex_unlock(&sel->lock);
device_for_each_child(&sel->partner->dev, &order, activate_altmode);
mutex_lock(&sel->lock);
return order.result;
}
static void mode_list_clean(struct mode_selection *sel)
{
struct mode_state *ms, *tmp;
list_for_each_entry_safe(ms, tmp, &sel->mode_list, list) {
list_del(&ms->list);
kfree(ms);
}
}
/**
* mode_selection_work_fn() - Alternate mode activation task
* @work: work structure
*
* - If the Alternate Mode currently prioritized at the top of the list is already
* active, the entire selection process is considered finished.
* - If a different Alternate Mode is currently active, the system must exit that
* active mode first before attempting any new entry.
*
* The function then checks the result of the attempt to entre the current mode,
* stored in the `ms->error` field:
* - if the attempt FAILED, the mode is deactivated and removed from the list.
* - `ms->error` value of 0 signifies that the mode has not yet been activated.
*
* Once successfully activated, the task is scheduled for subsequent entry after
* a timeout period. The alternate mode driver is expected to call back with the
* actual mode entry result via `typec_altmode_state_update()`.
*/
static void mode_selection_work_fn(struct work_struct *work)
{
struct mode_selection *sel = container_of(work,
struct mode_selection, work.work);
struct mode_state *ms;
unsigned int delay = sel->delay;
int result;
guard(mutex)(&sel->lock);
ms = list_first_entry_or_null(&sel->mode_list, struct mode_state, list);
if (!ms)
return;
if (sel->active_svid == ms->svid) {
dev_dbg(&sel->partner->dev, "%x altmode is active\n", ms->svid);
mode_list_clean(sel);
} else if (sel->active_svid != 0) {
result = mode_selection_activate(sel, sel->active_svid, 0);
if (result)
mode_list_clean(sel);
else
sel->active_svid = 0;
} else if (ms->error) {
dev_err(&sel->partner->dev, "%x: entry error %pe\n",
ms->svid, ERR_PTR(ms->error));
mode_selection_activate(sel, ms->svid, 0);
list_del(&ms->list);
kfree(ms);
} else {
result = mode_selection_activate(sel, ms->svid, 1);
if (result) {
dev_err(&sel->partner->dev, "%x: activation error %pe\n",
ms->svid, ERR_PTR(result));
list_del(&ms->list);
kfree(ms);
} else {
delay = sel->timeout;
ms->error = -ETIMEDOUT;
}
}
if (!list_empty(&sel->mode_list))
schedule_delayed_work(&sel->work, msecs_to_jiffies(delay));
}
void typec_altmode_state_update(struct typec_partner *partner, const u16 svid,
const int error)
{
struct mode_selection *sel = partner->sel;
struct mode_state *ms;
if (sel) {
mutex_lock(&sel->lock);
ms = list_first_entry_or_null(&sel->mode_list, struct mode_state, list);
if (ms && ms->svid == svid) {
ms->error = error;
if (cancel_delayed_work(&sel->work))
schedule_delayed_work(&sel->work, 0);
}
if (!error)
sel->active_svid = svid;
else
sel->active_svid = 0;
mutex_unlock(&sel->lock);
}
}
EXPORT_SYMBOL_GPL(typec_altmode_state_update);
static int compare_priorities(void *priv,
const struct list_head *a, const struct list_head *b)
{
const struct mode_state *msa = container_of(a, struct mode_state, list);
const struct mode_state *msb = container_of(b, struct mode_state, list);
if (msa->priority < msb->priority)
return -1;
return 1;
}
static int altmode_add_to_list(struct device *dev, void *data)
{
if (is_typec_partner_altmode(dev)) {
struct list_head *list = (struct list_head *)data;
struct typec_altmode *altmode = to_typec_altmode(dev);
const struct typec_altmode *pdev = typec_altmode_get_partner(altmode);
struct mode_state *ms;
if (pdev && altmode->ops && altmode->ops->activate) {
ms = kzalloc_obj(*ms);
if (!ms)
return -ENOMEM;
ms->svid = pdev->svid;
ms->priority = pdev->priority;
INIT_LIST_HEAD(&ms->list);
list_add_tail(&ms->list, list);
}
}
return 0;
}
int typec_mode_selection_start(struct typec_partner *partner,
const unsigned int delay, const unsigned int timeout)
{
struct mode_selection *sel;
int ret;
if (partner->usb_mode == USB_MODE_USB4)
return -EBUSY;
if (partner->sel)
return -EALREADY;
sel = kzalloc_obj(*sel);
if (!sel)
return -ENOMEM;
INIT_LIST_HEAD(&sel->mode_list);
ret = device_for_each_child(&partner->dev, &sel->mode_list,
altmode_add_to_list);
if (ret || list_empty(&sel->mode_list)) {
mode_list_clean(sel);
kfree(sel);
return ret;
}
list_sort(NULL, &sel->mode_list, compare_priorities);
sel->partner = partner;
sel->delay = delay;
sel->timeout = timeout;
mutex_init(&sel->lock);
INIT_DELAYED_WORK(&sel->work, mode_selection_work_fn);
schedule_delayed_work(&sel->work, msecs_to_jiffies(delay));
partner->sel = sel;
return 0;
}
EXPORT_SYMBOL_GPL(typec_mode_selection_start);
void typec_mode_selection_delete(struct typec_partner *partner)
{
struct mode_selection *sel = partner->sel;
if (sel) {
partner->sel = NULL;
cancel_delayed_work_sync(&sel->work);
mode_list_clean(sel);
mutex_destroy(&sel->lock);
kfree(sel);
}
}
EXPORT_SYMBOL_GPL(typec_mode_selection_delete);
|