This source file includes following definitions.
- typec_altmode_set_mux
- typec_altmode_set_state
- typec_altmode_notify
- typec_altmode_enter
- typec_altmode_exit
- typec_altmode_attention
- typec_altmode_vdm
- typec_altmode_get_partner
- typec_altmode_get_plug
- typec_altmode_put_plug
- __typec_altmode_register_driver
- typec_altmode_unregister_driver
- typec_match_altmode
- description_show
- typec_match
- typec_uevent
- typec_altmode_create_links
- typec_altmode_remove_links
- typec_probe
- typec_remove
1
2
3
4
5
6
7
8
9 #include <linux/usb/pd_vdo.h>
10
11 #include "bus.h"
12
13 static inline int typec_altmode_set_mux(struct altmode *alt, u8 state)
14 {
15 return alt->mux ? alt->mux->set(alt->mux, state) : 0;
16 }
17
18 static int typec_altmode_set_state(struct typec_altmode *adev, int state)
19 {
20 bool is_port = is_typec_port(adev->dev.parent);
21 struct altmode *port_altmode;
22 int ret;
23
24 port_altmode = is_port ? to_altmode(adev) : to_altmode(adev)->partner;
25
26 ret = typec_altmode_set_mux(port_altmode, state);
27 if (ret)
28 return ret;
29
30 blocking_notifier_call_chain(&port_altmode->nh, state, NULL);
31
32 return 0;
33 }
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51 int typec_altmode_notify(struct typec_altmode *adev,
52 unsigned long conf, void *data)
53 {
54 bool is_port;
55 struct altmode *altmode;
56 struct altmode *partner;
57 int ret;
58
59 if (!adev)
60 return 0;
61
62 altmode = to_altmode(adev);
63
64 if (!altmode->partner)
65 return -ENODEV;
66
67 is_port = is_typec_port(adev->dev.parent);
68 partner = altmode->partner;
69
70 ret = typec_altmode_set_mux(is_port ? altmode : partner, (u8)conf);
71 if (ret)
72 return ret;
73
74 blocking_notifier_call_chain(is_port ? &altmode->nh : &partner->nh,
75 conf, data);
76
77 if (partner->adev.ops && partner->adev.ops->notify)
78 return partner->adev.ops->notify(&partner->adev, conf, data);
79
80 return 0;
81 }
82 EXPORT_SYMBOL_GPL(typec_altmode_notify);
83
84
85
86
87
88
89
90
91
92 int typec_altmode_enter(struct typec_altmode *adev)
93 {
94 struct altmode *partner = to_altmode(adev)->partner;
95 struct typec_altmode *pdev = &partner->adev;
96 int ret;
97
98 if (!adev || adev->active)
99 return 0;
100
101 if (!pdev->ops || !pdev->ops->enter)
102 return -EOPNOTSUPP;
103
104
105 ret = typec_altmode_set_state(adev, TYPEC_STATE_SAFE);
106 if (ret)
107 return ret;
108
109
110 return pdev->ops->enter(pdev);
111 }
112 EXPORT_SYMBOL_GPL(typec_altmode_enter);
113
114
115
116
117
118
119
120 int typec_altmode_exit(struct typec_altmode *adev)
121 {
122 struct altmode *partner = to_altmode(adev)->partner;
123 struct typec_altmode *pdev = &partner->adev;
124 int ret;
125
126 if (!adev || !adev->active)
127 return 0;
128
129 if (!pdev->ops || !pdev->ops->enter)
130 return -EOPNOTSUPP;
131
132
133 ret = typec_altmode_set_state(adev, TYPEC_STATE_SAFE);
134 if (ret)
135 return ret;
136
137
138 return pdev->ops->exit(pdev);
139 }
140 EXPORT_SYMBOL_GPL(typec_altmode_exit);
141
142
143
144
145
146
147
148
149 void typec_altmode_attention(struct typec_altmode *adev, u32 vdo)
150 {
151 struct typec_altmode *pdev = &to_altmode(adev)->partner->adev;
152
153 if (pdev->ops && pdev->ops->attention)
154 pdev->ops->attention(pdev, vdo);
155 }
156 EXPORT_SYMBOL_GPL(typec_altmode_attention);
157
158
159
160
161
162
163
164
165
166
167
168
169 int typec_altmode_vdm(struct typec_altmode *adev,
170 const u32 header, const u32 *vdo, int count)
171 {
172 struct typec_altmode *pdev;
173 struct altmode *altmode;
174
175 if (!adev)
176 return 0;
177
178 altmode = to_altmode(adev);
179
180 if (!altmode->partner)
181 return -ENODEV;
182
183 pdev = &altmode->partner->adev;
184
185 if (!pdev->ops || !pdev->ops->vdm)
186 return -EOPNOTSUPP;
187
188 return pdev->ops->vdm(pdev, header, vdo, count);
189 }
190 EXPORT_SYMBOL_GPL(typec_altmode_vdm);
191
192 const struct typec_altmode *
193 typec_altmode_get_partner(struct typec_altmode *adev)
194 {
195 if (!adev || !to_altmode(adev)->partner)
196 return NULL;
197
198 return &to_altmode(adev)->partner->adev;
199 }
200 EXPORT_SYMBOL_GPL(typec_altmode_get_partner);
201
202
203
204
205
206
207
208
209
210
211
212
213 struct typec_altmode *typec_altmode_get_plug(struct typec_altmode *adev,
214 enum typec_plug_index index)
215 {
216 struct altmode *port = to_altmode(adev)->partner;
217
218 if (port->plug[index]) {
219 get_device(&port->plug[index]->adev.dev);
220 return &port->plug[index]->adev;
221 }
222
223 return NULL;
224 }
225 EXPORT_SYMBOL_GPL(typec_altmode_get_plug);
226
227
228
229
230
231 void typec_altmode_put_plug(struct typec_altmode *plug)
232 {
233 if (plug)
234 put_device(&plug->dev);
235 }
236 EXPORT_SYMBOL_GPL(typec_altmode_put_plug);
237
238 int __typec_altmode_register_driver(struct typec_altmode_driver *drv,
239 struct module *module)
240 {
241 if (!drv->probe)
242 return -EINVAL;
243
244 drv->driver.owner = module;
245 drv->driver.bus = &typec_bus;
246
247 return driver_register(&drv->driver);
248 }
249 EXPORT_SYMBOL_GPL(__typec_altmode_register_driver);
250
251 void typec_altmode_unregister_driver(struct typec_altmode_driver *drv)
252 {
253 driver_unregister(&drv->driver);
254 }
255 EXPORT_SYMBOL_GPL(typec_altmode_unregister_driver);
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270 struct typec_altmode *typec_match_altmode(struct typec_altmode **altmodes,
271 size_t n, u16 svid, u8 mode)
272 {
273 int i;
274
275 for (i = 0; i < n; i++) {
276 if (!altmodes[i])
277 break;
278 if (altmodes[i]->svid == svid && altmodes[i]->mode == mode)
279 return altmodes[i];
280 }
281
282 return NULL;
283 }
284 EXPORT_SYMBOL_GPL(typec_match_altmode);
285
286
287
288 static ssize_t
289 description_show(struct device *dev, struct device_attribute *attr, char *buf)
290 {
291 struct typec_altmode *alt = to_typec_altmode(dev);
292
293 return sprintf(buf, "%s\n", alt->desc ? alt->desc : "");
294 }
295 static DEVICE_ATTR_RO(description);
296
297 static struct attribute *typec_attrs[] = {
298 &dev_attr_description.attr,
299 NULL
300 };
301 ATTRIBUTE_GROUPS(typec);
302
303 static int typec_match(struct device *dev, struct device_driver *driver)
304 {
305 struct typec_altmode_driver *drv = to_altmode_driver(driver);
306 struct typec_altmode *altmode = to_typec_altmode(dev);
307 const struct typec_device_id *id;
308
309 for (id = drv->id_table; id->svid; id++)
310 if (id->svid == altmode->svid &&
311 (id->mode == TYPEC_ANY_MODE || id->mode == altmode->mode))
312 return 1;
313 return 0;
314 }
315
316 static int typec_uevent(struct device *dev, struct kobj_uevent_env *env)
317 {
318 struct typec_altmode *altmode = to_typec_altmode(dev);
319
320 if (add_uevent_var(env, "SVID=%04X", altmode->svid))
321 return -ENOMEM;
322
323 if (add_uevent_var(env, "MODE=%u", altmode->mode))
324 return -ENOMEM;
325
326 return add_uevent_var(env, "MODALIAS=typec:id%04Xm%02X",
327 altmode->svid, altmode->mode);
328 }
329
330 static int typec_altmode_create_links(struct altmode *alt)
331 {
332 struct device *port_dev = &alt->partner->adev.dev;
333 struct device *dev = &alt->adev.dev;
334 int err;
335
336 err = sysfs_create_link(&dev->kobj, &port_dev->kobj, "port");
337 if (err)
338 return err;
339
340 err = sysfs_create_link(&port_dev->kobj, &dev->kobj, "partner");
341 if (err)
342 sysfs_remove_link(&dev->kobj, "port");
343
344 return err;
345 }
346
347 static void typec_altmode_remove_links(struct altmode *alt)
348 {
349 sysfs_remove_link(&alt->partner->adev.dev.kobj, "partner");
350 sysfs_remove_link(&alt->adev.dev.kobj, "port");
351 }
352
353 static int typec_probe(struct device *dev)
354 {
355 struct typec_altmode_driver *drv = to_altmode_driver(dev->driver);
356 struct typec_altmode *adev = to_typec_altmode(dev);
357 struct altmode *altmode = to_altmode(adev);
358 int ret;
359
360
361 if (!altmode->partner)
362 return -ENODEV;
363
364 ret = typec_altmode_create_links(altmode);
365 if (ret) {
366 dev_warn(dev, "failed to create symlinks\n");
367 return ret;
368 }
369
370 ret = drv->probe(adev);
371 if (ret)
372 typec_altmode_remove_links(altmode);
373
374 return ret;
375 }
376
377 static int typec_remove(struct device *dev)
378 {
379 struct typec_altmode_driver *drv = to_altmode_driver(dev->driver);
380 struct typec_altmode *adev = to_typec_altmode(dev);
381 struct altmode *altmode = to_altmode(adev);
382
383 typec_altmode_remove_links(altmode);
384
385 if (drv->remove)
386 drv->remove(to_typec_altmode(dev));
387
388 if (adev->active) {
389 WARN_ON(typec_altmode_set_state(adev, TYPEC_STATE_SAFE));
390 typec_altmode_update_active(adev, false);
391 }
392
393 adev->desc = NULL;
394 adev->ops = NULL;
395
396 return 0;
397 }
398
399 struct bus_type typec_bus = {
400 .name = "typec",
401 .dev_groups = typec_groups,
402 .match = typec_match,
403 .uevent = typec_uevent,
404 .probe = typec_probe,
405 .remove = typec_remove,
406 };