Linux Audio

Check our new training course

Buildroot integration, development and maintenance

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