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