1 // SPDX-License-Identifier: GPL-2.0
3 * Bus for USB Type-C Alternate Modes
5 * Copyright (C) 2018 Intel Corporation
6 * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
9 #include <linux/usb/pd_vdo.h>
16 typec_altmode_set_mux(struct altmode *alt, unsigned long conf, void *data)
18 struct typec_mux_state state;
23 state.alt = &alt->adev;
27 return alt->mux->set(alt->mux, &state);
30 static int typec_altmode_set_state(struct typec_altmode *adev,
31 unsigned long conf, void *data)
33 bool is_port = is_typec_port(adev->dev.parent);
34 struct altmode *port_altmode;
36 port_altmode = is_port ? to_altmode(adev) : to_altmode(adev)->partner;
38 return typec_altmode_set_mux(port_altmode, conf, data);
41 /* -------------------------------------------------------------------------- */
45 * typec_altmode_notify - Communication between the OS and alternate mode driver
46 * @adev: Handle to the alternate mode
47 * @conf: Alternate mode specific configuration value
48 * @data: Alternate mode specific data
50 * The primary purpose for this function is to allow the alternate mode drivers
51 * to tell which pin configuration has been negotiated with the partner. That
52 * information will then be used for example to configure the muxes.
53 * Communication to the other direction is also possible, and low level device
54 * drivers can also send notifications to the alternate mode drivers. The actual
55 * communication will be specific for every SVID.
57 int typec_altmode_notify(struct typec_altmode *adev,
58 unsigned long conf, void *data)
61 struct altmode *altmode;
62 struct altmode *partner;
68 altmode = to_altmode(adev);
70 if (!altmode->partner)
73 is_port = is_typec_port(adev->dev.parent);
74 partner = altmode->partner;
76 ret = typec_altmode_set_mux(is_port ? altmode : partner, conf, data);
80 if (partner->adev.ops && partner->adev.ops->notify)
81 return partner->adev.ops->notify(&partner->adev, conf, data);
85 EXPORT_SYMBOL_GPL(typec_altmode_notify);
88 * typec_altmode_enter - Enter Mode
89 * @adev: The alternate mode
90 * @vdo: VDO for the Enter Mode command
92 * The alternate mode drivers use this function to enter mode. The port drivers
93 * use this to inform the alternate mode drivers that the partner has initiated
94 * Enter Mode command. If the alternate mode does not require VDO, @vdo must be
97 int typec_altmode_enter(struct typec_altmode *adev, u32 *vdo)
99 struct altmode *partner = to_altmode(adev)->partner;
100 struct typec_altmode *pdev = &partner->adev;
103 if (!adev || adev->active)
106 if (!pdev->ops || !pdev->ops->enter)
109 if (is_typec_port(pdev->dev.parent) && !pdev->active)
112 /* Moving to USB Safe State */
113 ret = typec_altmode_set_state(adev, TYPEC_STATE_SAFE, NULL);
118 return pdev->ops->enter(pdev, vdo);
120 EXPORT_SYMBOL_GPL(typec_altmode_enter);
123 * typec_altmode_exit - Exit Mode
124 * @adev: The alternate mode
126 * The partner of @adev has initiated Exit Mode command.
128 int typec_altmode_exit(struct typec_altmode *adev)
130 struct altmode *partner = to_altmode(adev)->partner;
131 struct typec_altmode *pdev = &partner->adev;
134 if (!adev || !adev->active)
137 if (!pdev->ops || !pdev->ops->enter)
140 /* Moving to USB Safe State */
141 ret = typec_altmode_set_state(adev, TYPEC_STATE_SAFE, NULL);
145 /* Exit Mode command */
146 return pdev->ops->exit(pdev);
148 EXPORT_SYMBOL_GPL(typec_altmode_exit);
151 * typec_altmode_attention - Attention command
152 * @adev: The alternate mode
153 * @vdo: VDO for the Attention command
155 * Notifies the partner of @adev about Attention command.
157 void typec_altmode_attention(struct typec_altmode *adev, u32 vdo)
159 struct typec_altmode *pdev = &to_altmode(adev)->partner->adev;
161 if (pdev->ops && pdev->ops->attention)
162 pdev->ops->attention(pdev, vdo);
164 EXPORT_SYMBOL_GPL(typec_altmode_attention);
167 * typec_altmode_vdm - Send Vendor Defined Messages (VDM) to the partner
168 * @adev: Alternate mode handle
169 * @header: VDM Header
170 * @vdo: Array of Vendor Defined Data Objects
171 * @count: Number of Data Objects
173 * The alternate mode drivers use this function for SVID specific communication
174 * with the partner. The port drivers use it to deliver the Structured VDMs
175 * received from the partners to the alternate mode drivers.
177 int typec_altmode_vdm(struct typec_altmode *adev,
178 const u32 header, const u32 *vdo, int count)
180 struct typec_altmode *pdev;
181 struct altmode *altmode;
186 altmode = to_altmode(adev);
188 if (!altmode->partner)
191 pdev = &altmode->partner->adev;
193 if (!pdev->ops || !pdev->ops->vdm)
196 return pdev->ops->vdm(pdev, header, vdo, count);
198 EXPORT_SYMBOL_GPL(typec_altmode_vdm);
200 const struct typec_altmode *
201 typec_altmode_get_partner(struct typec_altmode *adev)
203 if (!adev || !to_altmode(adev)->partner)
206 return &to_altmode(adev)->partner->adev;
208 EXPORT_SYMBOL_GPL(typec_altmode_get_partner);
210 /* -------------------------------------------------------------------------- */
211 /* API for the alternate mode drivers */
214 * typec_altmode_get_plug - Find cable plug alternate mode
215 * @adev: Handle to partner alternate mode
216 * @index: Cable plug index
218 * Increment reference count for cable plug alternate mode device. Returns
219 * handle to the cable plug alternate mode, or NULL if none is found.
221 struct typec_altmode *typec_altmode_get_plug(struct typec_altmode *adev,
222 enum typec_plug_index index)
224 struct altmode *port = to_altmode(adev)->partner;
226 if (port->plug[index]) {
227 get_device(&port->plug[index]->adev.dev);
228 return &port->plug[index]->adev;
233 EXPORT_SYMBOL_GPL(typec_altmode_get_plug);
236 * typec_altmode_put_plug - Decrement cable plug alternate mode reference count
237 * @plug: Handle to the cable plug alternate mode
239 void typec_altmode_put_plug(struct typec_altmode *plug)
242 put_device(&plug->dev);
244 EXPORT_SYMBOL_GPL(typec_altmode_put_plug);
246 int __typec_altmode_register_driver(struct typec_altmode_driver *drv,
247 struct module *module)
252 drv->driver.owner = module;
253 drv->driver.bus = &typec_bus;
255 return driver_register(&drv->driver);
257 EXPORT_SYMBOL_GPL(__typec_altmode_register_driver);
259 void typec_altmode_unregister_driver(struct typec_altmode_driver *drv)
261 driver_unregister(&drv->driver);
263 EXPORT_SYMBOL_GPL(typec_altmode_unregister_driver);
265 /* -------------------------------------------------------------------------- */
266 /* API for the port drivers */
269 * typec_match_altmode - Match SVID and mode to an array of alternate modes
270 * @altmodes: Array of alternate modes
271 * @n: Number of elements in the array, or -1 for NULL terminated arrays
272 * @svid: Standard or Vendor ID to match with
273 * @mode: Mode to match with
275 * Return pointer to an alternate mode with SVID matching @svid, or NULL when no
278 struct typec_altmode *typec_match_altmode(struct typec_altmode **altmodes,
279 size_t n, u16 svid, u8 mode)
283 for (i = 0; i < n; i++) {
286 if (altmodes[i]->svid == svid && altmodes[i]->mode == mode)
292 EXPORT_SYMBOL_GPL(typec_match_altmode);
294 /* -------------------------------------------------------------------------- */
297 description_show(struct device *dev, struct device_attribute *attr, char *buf)
299 struct typec_altmode *alt = to_typec_altmode(dev);
301 return sprintf(buf, "%s\n", alt->desc ? alt->desc : "");
303 static DEVICE_ATTR_RO(description);
305 static struct attribute *typec_attrs[] = {
306 &dev_attr_description.attr,
309 ATTRIBUTE_GROUPS(typec);
311 static int typec_match(struct device *dev, struct device_driver *driver)
313 struct typec_altmode_driver *drv = to_altmode_driver(driver);
314 struct typec_altmode *altmode = to_typec_altmode(dev);
315 const struct typec_device_id *id;
317 for (id = drv->id_table; id->svid; id++)
318 if (id->svid == altmode->svid &&
319 (id->mode == TYPEC_ANY_MODE || id->mode == altmode->mode))
324 static int typec_uevent(struct device *dev, struct kobj_uevent_env *env)
326 struct typec_altmode *altmode = to_typec_altmode(dev);
328 if (add_uevent_var(env, "SVID=%04X", altmode->svid))
331 if (add_uevent_var(env, "MODE=%u", altmode->mode))
334 return add_uevent_var(env, "MODALIAS=typec:id%04Xm%02X",
335 altmode->svid, altmode->mode);
338 static int typec_altmode_create_links(struct altmode *alt)
340 struct device *port_dev = &alt->partner->adev.dev;
341 struct device *dev = &alt->adev.dev;
344 err = sysfs_create_link(&dev->kobj, &port_dev->kobj, "port");
348 err = sysfs_create_link(&port_dev->kobj, &dev->kobj, "partner");
350 sysfs_remove_link(&dev->kobj, "port");
355 static void typec_altmode_remove_links(struct altmode *alt)
357 sysfs_remove_link(&alt->partner->adev.dev.kobj, "partner");
358 sysfs_remove_link(&alt->adev.dev.kobj, "port");
361 static int typec_probe(struct device *dev)
363 struct typec_altmode_driver *drv = to_altmode_driver(dev->driver);
364 struct typec_altmode *adev = to_typec_altmode(dev);
365 struct altmode *altmode = to_altmode(adev);
368 /* Fail if the port does not support the alternate mode */
369 if (!altmode->partner)
372 ret = typec_altmode_create_links(altmode);
374 dev_warn(dev, "failed to create symlinks\n");
378 ret = drv->probe(adev);
380 typec_altmode_remove_links(altmode);
385 static int typec_remove(struct device *dev)
387 struct typec_altmode_driver *drv = to_altmode_driver(dev->driver);
388 struct typec_altmode *adev = to_typec_altmode(dev);
389 struct altmode *altmode = to_altmode(adev);
391 typec_altmode_remove_links(altmode);
394 drv->remove(to_typec_altmode(dev));
397 WARN_ON(typec_altmode_set_state(adev, TYPEC_STATE_SAFE, NULL));
398 typec_altmode_update_active(adev, false);
407 struct bus_type typec_bus = {
409 .dev_groups = typec_groups,
410 .match = typec_match,
411 .uevent = typec_uevent,
412 .probe = typec_probe,
413 .remove = typec_remove,