1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Bus for USB Type-C Alternate Modes
4 *
5 * Copyright (C) 2018 Intel Corporation
6 * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
7 */
8
9 #include <linux/usb/pd_vdo.h>
10
11 #include "bus.h"
12 #include "class.h"
13 #include "mux.h"
14 #include "retimer.h"
15
16 static inline int
typec_altmode_set_retimer(struct altmode * alt,unsigned long conf,void * data)17 typec_altmode_set_retimer(struct altmode *alt, unsigned long conf, void *data)
18 {
19 struct typec_retimer_state state;
20
21 if (!alt->retimer)
22 return 0;
23
24 state.alt = &alt->adev;
25 state.mode = conf;
26 state.data = data;
27
28 return typec_retimer_set(alt->retimer, &state);
29 }
30
31 static inline int
typec_altmode_set_mux(struct altmode * alt,unsigned long conf,void * data)32 typec_altmode_set_mux(struct altmode *alt, unsigned long conf, void *data)
33 {
34 struct typec_mux_state state;
35
36 if (!alt->mux)
37 return 0;
38
39 state.alt = &alt->adev;
40 state.mode = conf;
41 state.data = data;
42
43 return typec_mux_set(alt->mux, &state);
44 }
45
46 /* Wrapper to set various Type-C port switches together. */
47 static inline int
typec_altmode_set_switches(struct altmode * alt,unsigned long conf,void * data)48 typec_altmode_set_switches(struct altmode *alt, unsigned long conf, void *data)
49 {
50 int ret;
51
52 ret = typec_altmode_set_retimer(alt, conf, data);
53 if (ret)
54 return ret;
55
56 return typec_altmode_set_mux(alt, conf, data);
57 }
58
typec_altmode_set_state(struct typec_altmode * adev,unsigned long conf,void * data)59 static int typec_altmode_set_state(struct typec_altmode *adev,
60 unsigned long conf, void *data)
61 {
62 bool is_port = is_typec_port(adev->dev.parent);
63 struct altmode *port_altmode;
64
65 port_altmode = is_port ? to_altmode(adev) : to_altmode(adev)->partner;
66
67 return typec_altmode_set_switches(port_altmode, conf, data);
68 }
69
70 /* -------------------------------------------------------------------------- */
71 /* Common API */
72
73 /**
74 * typec_altmode_notify - Communication between the OS and alternate mode driver
75 * @adev: Handle to the alternate mode
76 * @conf: Alternate mode specific configuration value
77 * @data: Alternate mode specific data
78 *
79 * The primary purpose for this function is to allow the alternate mode drivers
80 * to tell which pin configuration has been negotiated with the partner. That
81 * information will then be used for example to configure the muxes.
82 * Communication to the other direction is also possible, and low level device
83 * drivers can also send notifications to the alternate mode drivers. The actual
84 * communication will be specific for every SVID.
85 */
typec_altmode_notify(struct typec_altmode * adev,unsigned long conf,void * data)86 int typec_altmode_notify(struct typec_altmode *adev,
87 unsigned long conf, void *data)
88 {
89 bool is_port;
90 struct altmode *altmode;
91 struct altmode *partner;
92 int ret;
93
94 if (!adev)
95 return 0;
96
97 altmode = to_altmode(adev);
98
99 if (!altmode->partner)
100 return -ENODEV;
101
102 is_port = is_typec_port(adev->dev.parent);
103 partner = altmode->partner;
104
105 ret = typec_altmode_set_switches(is_port ? altmode : partner, conf, data);
106 if (ret)
107 return ret;
108
109 if (partner->adev.ops && partner->adev.ops->notify)
110 return partner->adev.ops->notify(&partner->adev, conf, data);
111
112 return 0;
113 }
114 EXPORT_SYMBOL_GPL(typec_altmode_notify);
115
116 /**
117 * typec_altmode_enter - Enter Mode
118 * @adev: The alternate mode
119 * @vdo: VDO for the Enter Mode command
120 *
121 * The alternate mode drivers use this function to enter mode. The port drivers
122 * use this to inform the alternate mode drivers that the partner has initiated
123 * Enter Mode command. If the alternate mode does not require VDO, @vdo must be
124 * NULL.
125 */
typec_altmode_enter(struct typec_altmode * adev,u32 * vdo)126 int typec_altmode_enter(struct typec_altmode *adev, u32 *vdo)
127 {
128 struct altmode *partner = to_altmode(adev)->partner;
129 struct typec_altmode *pdev = &partner->adev;
130 int ret;
131
132 if (!adev || adev->active)
133 return 0;
134
135 if (!pdev->ops || !pdev->ops->enter)
136 return -EOPNOTSUPP;
137
138 if (is_typec_port(pdev->dev.parent) && !pdev->active)
139 return -EPERM;
140
141 /* Moving to USB Safe State */
142 ret = typec_altmode_set_state(adev, TYPEC_STATE_SAFE, NULL);
143 if (ret)
144 return ret;
145
146 /* Enter Mode */
147 return pdev->ops->enter(pdev, vdo);
148 }
149 EXPORT_SYMBOL_GPL(typec_altmode_enter);
150
151 /**
152 * typec_altmode_exit - Exit Mode
153 * @adev: The alternate mode
154 *
155 * The partner of @adev has initiated Exit Mode command.
156 */
typec_altmode_exit(struct typec_altmode * adev)157 int typec_altmode_exit(struct typec_altmode *adev)
158 {
159 struct altmode *partner = to_altmode(adev)->partner;
160 struct typec_altmode *pdev = &partner->adev;
161 int ret;
162
163 if (!adev || !adev->active)
164 return 0;
165
166 if (!pdev->ops || !pdev->ops->exit)
167 return -EOPNOTSUPP;
168
169 /* Moving to USB Safe State */
170 ret = typec_altmode_set_state(adev, TYPEC_STATE_SAFE, NULL);
171 if (ret)
172 return ret;
173
174 /* Exit Mode command */
175 return pdev->ops->exit(pdev);
176 }
177 EXPORT_SYMBOL_GPL(typec_altmode_exit);
178
179 /**
180 * typec_altmode_attention - Attention command
181 * @adev: The alternate mode
182 * @vdo: VDO for the Attention command
183 *
184 * Notifies the partner of @adev about Attention command.
185 */
typec_altmode_attention(struct typec_altmode * adev,u32 vdo)186 void typec_altmode_attention(struct typec_altmode *adev, u32 vdo)
187 {
188 struct typec_altmode *pdev = &to_altmode(adev)->partner->adev;
189
190 if (pdev->ops && pdev->ops->attention)
191 pdev->ops->attention(pdev, vdo);
192 }
193 EXPORT_SYMBOL_GPL(typec_altmode_attention);
194
195 /**
196 * typec_altmode_vdm - Send Vendor Defined Messages (VDM) to the partner
197 * @adev: Alternate mode handle
198 * @header: VDM Header
199 * @vdo: Array of Vendor Defined Data Objects
200 * @count: Number of Data Objects
201 *
202 * The alternate mode drivers use this function for SVID specific communication
203 * with the partner. The port drivers use it to deliver the Structured VDMs
204 * received from the partners to the alternate mode drivers.
205 */
typec_altmode_vdm(struct typec_altmode * adev,const u32 header,const u32 * vdo,int count)206 int typec_altmode_vdm(struct typec_altmode *adev,
207 const u32 header, const u32 *vdo, int count)
208 {
209 struct typec_altmode *pdev;
210 struct altmode *altmode;
211
212 if (!adev)
213 return 0;
214
215 altmode = to_altmode(adev);
216
217 if (!altmode->partner)
218 return -ENODEV;
219
220 pdev = &altmode->partner->adev;
221
222 if (!pdev->ops || !pdev->ops->vdm)
223 return -EOPNOTSUPP;
224
225 return pdev->ops->vdm(pdev, header, vdo, count);
226 }
227 EXPORT_SYMBOL_GPL(typec_altmode_vdm);
228
229 const struct typec_altmode *
typec_altmode_get_partner(struct typec_altmode * adev)230 typec_altmode_get_partner(struct typec_altmode *adev)
231 {
232 if (!adev || !to_altmode(adev)->partner)
233 return NULL;
234
235 return &to_altmode(adev)->partner->adev;
236 }
237 EXPORT_SYMBOL_GPL(typec_altmode_get_partner);
238
239 /* -------------------------------------------------------------------------- */
240 /* API for the alternate mode drivers */
241
242 /**
243 * typec_altmode_get_plug - Find cable plug alternate mode
244 * @adev: Handle to partner alternate mode
245 * @index: Cable plug index
246 *
247 * Increment reference count for cable plug alternate mode device. Returns
248 * handle to the cable plug alternate mode, or NULL if none is found.
249 */
typec_altmode_get_plug(struct typec_altmode * adev,enum typec_plug_index index)250 struct typec_altmode *typec_altmode_get_plug(struct typec_altmode *adev,
251 enum typec_plug_index index)
252 {
253 struct altmode *port = to_altmode(adev)->partner;
254
255 if (port->plug[index]) {
256 get_device(&port->plug[index]->adev.dev);
257 return &port->plug[index]->adev;
258 }
259
260 return NULL;
261 }
262 EXPORT_SYMBOL_GPL(typec_altmode_get_plug);
263
264 /**
265 * typec_altmode_put_plug - Decrement cable plug alternate mode reference count
266 * @plug: Handle to the cable plug alternate mode
267 */
typec_altmode_put_plug(struct typec_altmode * plug)268 void typec_altmode_put_plug(struct typec_altmode *plug)
269 {
270 if (plug)
271 put_device(&plug->dev);
272 }
273 EXPORT_SYMBOL_GPL(typec_altmode_put_plug);
274
__typec_altmode_register_driver(struct typec_altmode_driver * drv,struct module * module)275 int __typec_altmode_register_driver(struct typec_altmode_driver *drv,
276 struct module *module)
277 {
278 if (!drv->probe)
279 return -EINVAL;
280
281 drv->driver.owner = module;
282 drv->driver.bus = &typec_bus;
283
284 return driver_register(&drv->driver);
285 }
286 EXPORT_SYMBOL_GPL(__typec_altmode_register_driver);
287
typec_altmode_unregister_driver(struct typec_altmode_driver * drv)288 void typec_altmode_unregister_driver(struct typec_altmode_driver *drv)
289 {
290 driver_unregister(&drv->driver);
291 }
292 EXPORT_SYMBOL_GPL(typec_altmode_unregister_driver);
293
294 /* -------------------------------------------------------------------------- */
295 /* API for the port drivers */
296
297 /**
298 * typec_match_altmode - Match SVID and mode to an array of alternate modes
299 * @altmodes: Array of alternate modes
300 * @n: Number of elements in the array, or -1 for NULL terminated arrays
301 * @svid: Standard or Vendor ID to match with
302 * @mode: Mode to match with
303 *
304 * Return pointer to an alternate mode with SVID matching @svid, or NULL when no
305 * match is found.
306 */
typec_match_altmode(struct typec_altmode ** altmodes,size_t n,u16 svid,u8 mode)307 struct typec_altmode *typec_match_altmode(struct typec_altmode **altmodes,
308 size_t n, u16 svid, u8 mode)
309 {
310 int i;
311
312 for (i = 0; i < n; i++) {
313 if (!altmodes[i])
314 break;
315 if (altmodes[i]->svid == svid && altmodes[i]->mode == mode)
316 return altmodes[i];
317 }
318
319 return NULL;
320 }
321 EXPORT_SYMBOL_GPL(typec_match_altmode);
322
323 /* -------------------------------------------------------------------------- */
324
325 static ssize_t
description_show(struct device * dev,struct device_attribute * attr,char * buf)326 description_show(struct device *dev, struct device_attribute *attr, char *buf)
327 {
328 struct typec_altmode *alt = to_typec_altmode(dev);
329
330 return sprintf(buf, "%s\n", alt->desc ? alt->desc : "");
331 }
332 static DEVICE_ATTR_RO(description);
333
334 static struct attribute *typec_attrs[] = {
335 &dev_attr_description.attr,
336 NULL
337 };
338 ATTRIBUTE_GROUPS(typec);
339
typec_match(struct device * dev,struct device_driver * driver)340 static int typec_match(struct device *dev, struct device_driver *driver)
341 {
342 struct typec_altmode_driver *drv = to_altmode_driver(driver);
343 struct typec_altmode *altmode = to_typec_altmode(dev);
344 const struct typec_device_id *id;
345
346 for (id = drv->id_table; id->svid; id++)
347 if (id->svid == altmode->svid &&
348 (id->mode == TYPEC_ANY_MODE || id->mode == altmode->mode))
349 return 1;
350 return 0;
351 }
352
typec_uevent(const struct device * dev,struct kobj_uevent_env * env)353 static int typec_uevent(const struct device *dev, struct kobj_uevent_env *env)
354 {
355 const struct typec_altmode *altmode = to_typec_altmode(dev);
356
357 if (add_uevent_var(env, "SVID=%04X", altmode->svid))
358 return -ENOMEM;
359
360 if (add_uevent_var(env, "MODE=%u", altmode->mode))
361 return -ENOMEM;
362
363 return add_uevent_var(env, "MODALIAS=typec:id%04Xm%02X",
364 altmode->svid, altmode->mode);
365 }
366
typec_altmode_create_links(struct altmode * alt)367 static int typec_altmode_create_links(struct altmode *alt)
368 {
369 struct device *port_dev = &alt->partner->adev.dev;
370 struct device *dev = &alt->adev.dev;
371 int err;
372
373 err = sysfs_create_link(&dev->kobj, &port_dev->kobj, "port");
374 if (err)
375 return err;
376
377 err = sysfs_create_link(&port_dev->kobj, &dev->kobj, "partner");
378 if (err)
379 sysfs_remove_link(&dev->kobj, "port");
380
381 return err;
382 }
383
typec_altmode_remove_links(struct altmode * alt)384 static void typec_altmode_remove_links(struct altmode *alt)
385 {
386 sysfs_remove_link(&alt->partner->adev.dev.kobj, "partner");
387 sysfs_remove_link(&alt->adev.dev.kobj, "port");
388 }
389
typec_probe(struct device * dev)390 static int typec_probe(struct device *dev)
391 {
392 struct typec_altmode_driver *drv = to_altmode_driver(dev->driver);
393 struct typec_altmode *adev = to_typec_altmode(dev);
394 struct altmode *altmode = to_altmode(adev);
395 int ret;
396
397 /* Fail if the port does not support the alternate mode */
398 if (!altmode->partner)
399 return -ENODEV;
400
401 ret = typec_altmode_create_links(altmode);
402 if (ret) {
403 dev_warn(dev, "failed to create symlinks\n");
404 return ret;
405 }
406
407 ret = drv->probe(adev);
408 if (ret)
409 typec_altmode_remove_links(altmode);
410
411 return ret;
412 }
413
typec_remove(struct device * dev)414 static void typec_remove(struct device *dev)
415 {
416 struct typec_altmode_driver *drv = to_altmode_driver(dev->driver);
417 struct typec_altmode *adev = to_typec_altmode(dev);
418 struct altmode *altmode = to_altmode(adev);
419
420 typec_altmode_remove_links(altmode);
421
422 if (drv->remove)
423 drv->remove(to_typec_altmode(dev));
424
425 if (adev->active) {
426 WARN_ON(typec_altmode_set_state(adev, TYPEC_STATE_SAFE, NULL));
427 typec_altmode_update_active(adev, false);
428 }
429
430 adev->desc = NULL;
431 adev->ops = NULL;
432 }
433
434 struct bus_type typec_bus = {
435 .name = "typec",
436 .dev_groups = typec_groups,
437 .match = typec_match,
438 .uevent = typec_uevent,
439 .probe = typec_probe,
440 .remove = typec_remove,
441 };
442