xref: /linux/drivers/usb/typec/class.c (revision f8941e6c4c712948663ec5d7bbb546f1a0f4e3f6)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * USB Type-C Connector Class
4  *
5  * Copyright (C) 2017, Intel Corporation
6  * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
7  */
8 
9 #include <linux/module.h>
10 #include <linux/mutex.h>
11 #include <linux/property.h>
12 #include <linux/slab.h>
13 #include <linux/usb/pd_vdo.h>
14 #include <linux/usb/typec_mux.h>
15 #include <linux/usb/typec_retimer.h>
16 
17 #include "bus.h"
18 #include "class.h"
19 #include "pd.h"
20 
21 static DEFINE_IDA(typec_index_ida);
22 
23 struct class typec_class = {
24 	.name = "typec",
25 };
26 
27 /* ------------------------------------------------------------------------- */
28 /* Common attributes */
29 
30 static const char * const typec_accessory_modes[] = {
31 	[TYPEC_ACCESSORY_NONE]		= "none",
32 	[TYPEC_ACCESSORY_AUDIO]		= "analog_audio",
33 	[TYPEC_ACCESSORY_DEBUG]		= "debug",
34 };
35 
36 /* Product types defined in USB PD Specification R3.0 V2.0 */
37 static const char * const product_type_ufp[8] = {
38 	[IDH_PTYPE_NOT_UFP]		= "not_ufp",
39 	[IDH_PTYPE_HUB]			= "hub",
40 	[IDH_PTYPE_PERIPH]		= "peripheral",
41 	[IDH_PTYPE_PSD]			= "psd",
42 	[IDH_PTYPE_AMA]			= "ama",
43 };
44 
45 static const char * const product_type_dfp[8] = {
46 	[IDH_PTYPE_NOT_DFP]		= "not_dfp",
47 	[IDH_PTYPE_DFP_HUB]		= "hub",
48 	[IDH_PTYPE_DFP_HOST]		= "host",
49 	[IDH_PTYPE_DFP_PB]		= "power_brick",
50 };
51 
52 static const char * const product_type_cable[8] = {
53 	[IDH_PTYPE_NOT_CABLE]		= "not_cable",
54 	[IDH_PTYPE_PCABLE]		= "passive",
55 	[IDH_PTYPE_ACABLE]		= "active",
56 	[IDH_PTYPE_VPD]			= "vpd",
57 };
58 
59 static struct usb_pd_identity *get_pd_identity(struct device *dev)
60 {
61 	if (is_typec_partner(dev)) {
62 		struct typec_partner *partner = to_typec_partner(dev);
63 
64 		return partner->identity;
65 	} else if (is_typec_cable(dev)) {
66 		struct typec_cable *cable = to_typec_cable(dev);
67 
68 		return cable->identity;
69 	}
70 	return NULL;
71 }
72 
73 static const char *get_pd_product_type(struct device *dev)
74 {
75 	struct typec_port *port = to_typec_port(dev->parent);
76 	struct usb_pd_identity *id = get_pd_identity(dev);
77 	const char *ptype = NULL;
78 
79 	if (is_typec_partner(dev)) {
80 		if (!id)
81 			return NULL;
82 
83 		if (port->data_role == TYPEC_HOST)
84 			ptype = product_type_ufp[PD_IDH_PTYPE(id->id_header)];
85 		else
86 			ptype = product_type_dfp[PD_IDH_DFP_PTYPE(id->id_header)];
87 	} else if (is_typec_cable(dev)) {
88 		if (id)
89 			ptype = product_type_cable[PD_IDH_PTYPE(id->id_header)];
90 		else
91 			ptype = to_typec_cable(dev)->active ?
92 				product_type_cable[IDH_PTYPE_ACABLE] :
93 				product_type_cable[IDH_PTYPE_PCABLE];
94 	}
95 
96 	return ptype;
97 }
98 
99 static ssize_t id_header_show(struct device *dev, struct device_attribute *attr,
100 			      char *buf)
101 {
102 	struct usb_pd_identity *id = get_pd_identity(dev);
103 
104 	return sprintf(buf, "0x%08x\n", id->id_header);
105 }
106 static DEVICE_ATTR_RO(id_header);
107 
108 static ssize_t cert_stat_show(struct device *dev, struct device_attribute *attr,
109 			      char *buf)
110 {
111 	struct usb_pd_identity *id = get_pd_identity(dev);
112 
113 	return sprintf(buf, "0x%08x\n", id->cert_stat);
114 }
115 static DEVICE_ATTR_RO(cert_stat);
116 
117 static ssize_t product_show(struct device *dev, struct device_attribute *attr,
118 			    char *buf)
119 {
120 	struct usb_pd_identity *id = get_pd_identity(dev);
121 
122 	return sprintf(buf, "0x%08x\n", id->product);
123 }
124 static DEVICE_ATTR_RO(product);
125 
126 static ssize_t product_type_vdo1_show(struct device *dev, struct device_attribute *attr,
127 				      char *buf)
128 {
129 	struct usb_pd_identity *id = get_pd_identity(dev);
130 
131 	return sysfs_emit(buf, "0x%08x\n", id->vdo[0]);
132 }
133 static DEVICE_ATTR_RO(product_type_vdo1);
134 
135 static ssize_t product_type_vdo2_show(struct device *dev, struct device_attribute *attr,
136 				      char *buf)
137 {
138 	struct usb_pd_identity *id = get_pd_identity(dev);
139 
140 	return sysfs_emit(buf, "0x%08x\n", id->vdo[1]);
141 }
142 static DEVICE_ATTR_RO(product_type_vdo2);
143 
144 static ssize_t product_type_vdo3_show(struct device *dev, struct device_attribute *attr,
145 				      char *buf)
146 {
147 	struct usb_pd_identity *id = get_pd_identity(dev);
148 
149 	return sysfs_emit(buf, "0x%08x\n", id->vdo[2]);
150 }
151 static DEVICE_ATTR_RO(product_type_vdo3);
152 
153 static struct attribute *usb_pd_id_attrs[] = {
154 	&dev_attr_id_header.attr,
155 	&dev_attr_cert_stat.attr,
156 	&dev_attr_product.attr,
157 	&dev_attr_product_type_vdo1.attr,
158 	&dev_attr_product_type_vdo2.attr,
159 	&dev_attr_product_type_vdo3.attr,
160 	NULL
161 };
162 
163 static const struct attribute_group usb_pd_id_group = {
164 	.name = "identity",
165 	.attrs = usb_pd_id_attrs,
166 };
167 
168 static const struct attribute_group *usb_pd_id_groups[] = {
169 	&usb_pd_id_group,
170 	NULL,
171 };
172 
173 static void typec_product_type_notify(struct device *dev)
174 {
175 	char *envp[2] = { };
176 	const char *ptype;
177 
178 	ptype = get_pd_product_type(dev);
179 	if (!ptype)
180 		return;
181 
182 	sysfs_notify(&dev->kobj, NULL, "type");
183 
184 	envp[0] = kasprintf(GFP_KERNEL, "PRODUCT_TYPE=%s", ptype);
185 	if (!envp[0])
186 		return;
187 
188 	kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
189 	kfree(envp[0]);
190 }
191 
192 static void typec_report_identity(struct device *dev)
193 {
194 	sysfs_notify(&dev->kobj, "identity", "id_header");
195 	sysfs_notify(&dev->kobj, "identity", "cert_stat");
196 	sysfs_notify(&dev->kobj, "identity", "product");
197 	sysfs_notify(&dev->kobj, "identity", "product_type_vdo1");
198 	sysfs_notify(&dev->kobj, "identity", "product_type_vdo2");
199 	sysfs_notify(&dev->kobj, "identity", "product_type_vdo3");
200 	typec_product_type_notify(dev);
201 }
202 
203 static ssize_t
204 type_show(struct device *dev, struct device_attribute *attr, char *buf)
205 {
206 	const char *ptype;
207 
208 	ptype = get_pd_product_type(dev);
209 	if (!ptype)
210 		return 0;
211 
212 	return sysfs_emit(buf, "%s\n", ptype);
213 }
214 static DEVICE_ATTR_RO(type);
215 
216 static ssize_t usb_power_delivery_revision_show(struct device *dev,
217 						struct device_attribute *attr,
218 						char *buf);
219 static DEVICE_ATTR_RO(usb_power_delivery_revision);
220 
221 /* ------------------------------------------------------------------------- */
222 /* Alternate Modes */
223 
224 static int altmode_match(struct device *dev, void *data)
225 {
226 	struct typec_altmode *adev = to_typec_altmode(dev);
227 	struct typec_device_id *id = data;
228 
229 	if (!is_typec_altmode(dev))
230 		return 0;
231 
232 	return ((adev->svid == id->svid) && (adev->mode == id->mode));
233 }
234 
235 static void typec_altmode_set_partner(struct altmode *altmode)
236 {
237 	struct typec_altmode *adev = &altmode->adev;
238 	struct typec_device_id id = { adev->svid, adev->mode, };
239 	struct typec_port *port = typec_altmode2port(adev);
240 	struct altmode *partner;
241 	struct device *dev;
242 
243 	dev = device_find_child(&port->dev, &id, altmode_match);
244 	if (!dev)
245 		return;
246 
247 	/* Bind the port alt mode to the partner/plug alt mode. */
248 	partner = to_altmode(to_typec_altmode(dev));
249 	altmode->partner = partner;
250 
251 	/* Bind the partner/plug alt mode to the port alt mode. */
252 	if (is_typec_plug(adev->dev.parent)) {
253 		struct typec_plug *plug = to_typec_plug(adev->dev.parent);
254 
255 		partner->plug[plug->index] = altmode;
256 	} else {
257 		partner->partner = altmode;
258 	}
259 }
260 
261 static void typec_altmode_put_partner(struct altmode *altmode)
262 {
263 	struct altmode *partner = altmode->partner;
264 	struct typec_altmode *adev;
265 
266 	if (!partner)
267 		return;
268 
269 	adev = &partner->adev;
270 
271 	if (is_typec_plug(adev->dev.parent)) {
272 		struct typec_plug *plug = to_typec_plug(adev->dev.parent);
273 
274 		partner->plug[plug->index] = NULL;
275 	} else {
276 		partner->partner = NULL;
277 	}
278 	put_device(&adev->dev);
279 }
280 
281 /**
282  * typec_altmode_update_active - Report Enter/Exit mode
283  * @adev: Handle to the alternate mode
284  * @active: True when the mode has been entered
285  *
286  * If a partner or cable plug executes Enter/Exit Mode command successfully, the
287  * drivers use this routine to report the updated state of the mode.
288  */
289 void typec_altmode_update_active(struct typec_altmode *adev, bool active)
290 {
291 	char dir[6];
292 
293 	if (adev->active == active)
294 		return;
295 
296 	if (!is_typec_port(adev->dev.parent) && adev->dev.driver) {
297 		if (!active)
298 			module_put(adev->dev.driver->owner);
299 		else
300 			WARN_ON(!try_module_get(adev->dev.driver->owner));
301 	}
302 
303 	adev->active = active;
304 	snprintf(dir, sizeof(dir), "mode%d", adev->mode);
305 	sysfs_notify(&adev->dev.kobj, dir, "active");
306 	sysfs_notify(&adev->dev.kobj, NULL, "active");
307 	kobject_uevent(&adev->dev.kobj, KOBJ_CHANGE);
308 }
309 EXPORT_SYMBOL_GPL(typec_altmode_update_active);
310 
311 /**
312  * typec_altmode2port - Alternate Mode to USB Type-C port
313  * @alt: The Alternate Mode
314  *
315  * Returns handle to the port that a cable plug or partner with @alt is
316  * connected to.
317  */
318 struct typec_port *typec_altmode2port(struct typec_altmode *alt)
319 {
320 	if (is_typec_plug(alt->dev.parent))
321 		return to_typec_port(alt->dev.parent->parent->parent);
322 	if (is_typec_partner(alt->dev.parent))
323 		return to_typec_port(alt->dev.parent->parent);
324 	if (is_typec_port(alt->dev.parent))
325 		return to_typec_port(alt->dev.parent);
326 
327 	return NULL;
328 }
329 EXPORT_SYMBOL_GPL(typec_altmode2port);
330 
331 static ssize_t
332 vdo_show(struct device *dev, struct device_attribute *attr, char *buf)
333 {
334 	struct typec_altmode *alt = to_typec_altmode(dev);
335 
336 	return sprintf(buf, "0x%08x\n", alt->vdo);
337 }
338 static DEVICE_ATTR_RO(vdo);
339 
340 static ssize_t
341 description_show(struct device *dev, struct device_attribute *attr, char *buf)
342 {
343 	struct typec_altmode *alt = to_typec_altmode(dev);
344 
345 	return sprintf(buf, "%s\n", alt->desc ? alt->desc : "");
346 }
347 static DEVICE_ATTR_RO(description);
348 
349 static ssize_t
350 active_show(struct device *dev, struct device_attribute *attr, char *buf)
351 {
352 	struct typec_altmode *alt = to_typec_altmode(dev);
353 
354 	return sprintf(buf, "%s\n", alt->active ? "yes" : "no");
355 }
356 
357 static ssize_t active_store(struct device *dev, struct device_attribute *attr,
358 			    const char *buf, size_t size)
359 {
360 	struct typec_altmode *adev = to_typec_altmode(dev);
361 	struct altmode *altmode = to_altmode(adev);
362 	bool enter;
363 	int ret;
364 
365 	ret = kstrtobool(buf, &enter);
366 	if (ret)
367 		return ret;
368 
369 	if (adev->active == enter)
370 		return size;
371 
372 	if (is_typec_port(adev->dev.parent)) {
373 		typec_altmode_update_active(adev, enter);
374 
375 		/* Make sure that the partner exits the mode before disabling */
376 		if (altmode->partner && !enter && altmode->partner->adev.active)
377 			typec_altmode_exit(&altmode->partner->adev);
378 	} else if (altmode->partner) {
379 		if (enter && !altmode->partner->adev.active) {
380 			dev_warn(dev, "port has the mode disabled\n");
381 			return -EPERM;
382 		}
383 	}
384 
385 	/* Note: If there is no driver, the mode will not be entered */
386 	if (adev->ops && adev->ops->activate) {
387 		ret = adev->ops->activate(adev, enter);
388 		if (ret)
389 			return ret;
390 	}
391 
392 	return size;
393 }
394 static DEVICE_ATTR_RW(active);
395 
396 static ssize_t
397 supported_roles_show(struct device *dev, struct device_attribute *attr,
398 		     char *buf)
399 {
400 	struct altmode *alt = to_altmode(to_typec_altmode(dev));
401 	ssize_t ret;
402 
403 	switch (alt->roles) {
404 	case TYPEC_PORT_SRC:
405 		ret = sprintf(buf, "source\n");
406 		break;
407 	case TYPEC_PORT_SNK:
408 		ret = sprintf(buf, "sink\n");
409 		break;
410 	case TYPEC_PORT_DRP:
411 	default:
412 		ret = sprintf(buf, "source sink\n");
413 		break;
414 	}
415 	return ret;
416 }
417 static DEVICE_ATTR_RO(supported_roles);
418 
419 static ssize_t
420 mode_show(struct device *dev, struct device_attribute *attr, char *buf)
421 {
422 	struct typec_altmode *adev = to_typec_altmode(dev);
423 
424 	return sprintf(buf, "%u\n", adev->mode);
425 }
426 static DEVICE_ATTR_RO(mode);
427 
428 static ssize_t
429 svid_show(struct device *dev, struct device_attribute *attr, char *buf)
430 {
431 	struct typec_altmode *adev = to_typec_altmode(dev);
432 
433 	return sprintf(buf, "%04x\n", adev->svid);
434 }
435 static DEVICE_ATTR_RO(svid);
436 
437 static struct attribute *typec_altmode_attrs[] = {
438 	&dev_attr_active.attr,
439 	&dev_attr_mode.attr,
440 	&dev_attr_svid.attr,
441 	&dev_attr_vdo.attr,
442 	NULL
443 };
444 
445 static umode_t typec_altmode_attr_is_visible(struct kobject *kobj,
446 					     struct attribute *attr, int n)
447 {
448 	struct typec_altmode *adev = to_typec_altmode(kobj_to_dev(kobj));
449 
450 	if (attr == &dev_attr_active.attr)
451 		if (!adev->ops || !adev->ops->activate)
452 			return 0444;
453 
454 	return attr->mode;
455 }
456 
457 static const struct attribute_group typec_altmode_group = {
458 	.is_visible = typec_altmode_attr_is_visible,
459 	.attrs = typec_altmode_attrs,
460 };
461 
462 static const struct attribute_group *typec_altmode_groups[] = {
463 	&typec_altmode_group,
464 	NULL
465 };
466 
467 static int altmode_id_get(struct device *dev)
468 {
469 	struct ida *ids;
470 
471 	if (is_typec_partner(dev))
472 		ids = &to_typec_partner(dev)->mode_ids;
473 	else if (is_typec_plug(dev))
474 		ids = &to_typec_plug(dev)->mode_ids;
475 	else
476 		ids = &to_typec_port(dev)->mode_ids;
477 
478 	return ida_simple_get(ids, 0, 0, GFP_KERNEL);
479 }
480 
481 static void altmode_id_remove(struct device *dev, int id)
482 {
483 	struct ida *ids;
484 
485 	if (is_typec_partner(dev))
486 		ids = &to_typec_partner(dev)->mode_ids;
487 	else if (is_typec_plug(dev))
488 		ids = &to_typec_plug(dev)->mode_ids;
489 	else
490 		ids = &to_typec_port(dev)->mode_ids;
491 
492 	ida_simple_remove(ids, id);
493 }
494 
495 static void typec_altmode_release(struct device *dev)
496 {
497 	struct altmode *alt = to_altmode(to_typec_altmode(dev));
498 
499 	typec_altmode_put_partner(alt);
500 
501 	altmode_id_remove(alt->adev.dev.parent, alt->id);
502 	kfree(alt);
503 }
504 
505 const struct device_type typec_altmode_dev_type = {
506 	.name = "typec_alternate_mode",
507 	.groups = typec_altmode_groups,
508 	.release = typec_altmode_release,
509 };
510 
511 static struct typec_altmode *
512 typec_register_altmode(struct device *parent,
513 		       const struct typec_altmode_desc *desc)
514 {
515 	unsigned int id = altmode_id_get(parent);
516 	bool is_port = is_typec_port(parent);
517 	struct altmode *alt;
518 	int ret;
519 
520 	alt = kzalloc(sizeof(*alt), GFP_KERNEL);
521 	if (!alt) {
522 		altmode_id_remove(parent, id);
523 		return ERR_PTR(-ENOMEM);
524 	}
525 
526 	alt->adev.svid = desc->svid;
527 	alt->adev.mode = desc->mode;
528 	alt->adev.vdo = desc->vdo;
529 	alt->roles = desc->roles;
530 	alt->id = id;
531 
532 	alt->attrs[0] = &dev_attr_vdo.attr;
533 	alt->attrs[1] = &dev_attr_description.attr;
534 	alt->attrs[2] = &dev_attr_active.attr;
535 
536 	if (is_port) {
537 		alt->attrs[3] = &dev_attr_supported_roles.attr;
538 		alt->adev.active = true; /* Enabled by default */
539 	}
540 
541 	sprintf(alt->group_name, "mode%d", desc->mode);
542 	alt->group.name = alt->group_name;
543 	alt->group.attrs = alt->attrs;
544 	alt->groups[0] = &alt->group;
545 
546 	alt->adev.dev.parent = parent;
547 	alt->adev.dev.groups = alt->groups;
548 	alt->adev.dev.type = &typec_altmode_dev_type;
549 	dev_set_name(&alt->adev.dev, "%s.%u", dev_name(parent), id);
550 
551 	/* Link partners and plugs with the ports */
552 	if (!is_port)
553 		typec_altmode_set_partner(alt);
554 
555 	/* The partners are bind to drivers */
556 	if (is_typec_partner(parent))
557 		alt->adev.dev.bus = &typec_bus;
558 
559 	/* Plug alt modes need a class to generate udev events. */
560 	if (is_typec_plug(parent))
561 		alt->adev.dev.class = &typec_class;
562 
563 	ret = device_register(&alt->adev.dev);
564 	if (ret) {
565 		dev_err(parent, "failed to register alternate mode (%d)\n",
566 			ret);
567 		put_device(&alt->adev.dev);
568 		return ERR_PTR(ret);
569 	}
570 
571 	return &alt->adev;
572 }
573 
574 /**
575  * typec_unregister_altmode - Unregister Alternate Mode
576  * @adev: The alternate mode to be unregistered
577  *
578  * Unregister device created with typec_partner_register_altmode(),
579  * typec_plug_register_altmode() or typec_port_register_altmode().
580  */
581 void typec_unregister_altmode(struct typec_altmode *adev)
582 {
583 	if (IS_ERR_OR_NULL(adev))
584 		return;
585 	typec_retimer_put(to_altmode(adev)->retimer);
586 	typec_mux_put(to_altmode(adev)->mux);
587 	device_unregister(&adev->dev);
588 }
589 EXPORT_SYMBOL_GPL(typec_unregister_altmode);
590 
591 /* ------------------------------------------------------------------------- */
592 /* Type-C Partners */
593 
594 static ssize_t accessory_mode_show(struct device *dev,
595 				   struct device_attribute *attr,
596 				   char *buf)
597 {
598 	struct typec_partner *p = to_typec_partner(dev);
599 
600 	return sprintf(buf, "%s\n", typec_accessory_modes[p->accessory]);
601 }
602 static DEVICE_ATTR_RO(accessory_mode);
603 
604 static ssize_t supports_usb_power_delivery_show(struct device *dev,
605 						struct device_attribute *attr,
606 						char *buf)
607 {
608 	struct typec_partner *p = to_typec_partner(dev);
609 
610 	return sprintf(buf, "%s\n", p->usb_pd ? "yes" : "no");
611 }
612 static DEVICE_ATTR_RO(supports_usb_power_delivery);
613 
614 static ssize_t number_of_alternate_modes_show(struct device *dev, struct device_attribute *attr,
615 					      char *buf)
616 {
617 	struct typec_partner *partner;
618 	struct typec_plug *plug;
619 	int num_altmodes;
620 
621 	if (is_typec_partner(dev)) {
622 		partner = to_typec_partner(dev);
623 		num_altmodes = partner->num_altmodes;
624 	} else if (is_typec_plug(dev)) {
625 		plug = to_typec_plug(dev);
626 		num_altmodes = plug->num_altmodes;
627 	} else {
628 		return 0;
629 	}
630 
631 	return sysfs_emit(buf, "%d\n", num_altmodes);
632 }
633 static DEVICE_ATTR_RO(number_of_alternate_modes);
634 
635 static struct attribute *typec_partner_attrs[] = {
636 	&dev_attr_accessory_mode.attr,
637 	&dev_attr_supports_usb_power_delivery.attr,
638 	&dev_attr_number_of_alternate_modes.attr,
639 	&dev_attr_type.attr,
640 	&dev_attr_usb_power_delivery_revision.attr,
641 	NULL
642 };
643 
644 static umode_t typec_partner_attr_is_visible(struct kobject *kobj, struct attribute *attr, int n)
645 {
646 	struct typec_partner *partner = to_typec_partner(kobj_to_dev(kobj));
647 
648 	if (attr == &dev_attr_number_of_alternate_modes.attr) {
649 		if (partner->num_altmodes < 0)
650 			return 0;
651 	}
652 
653 	if (attr == &dev_attr_type.attr)
654 		if (!get_pd_product_type(kobj_to_dev(kobj)))
655 			return 0;
656 
657 	return attr->mode;
658 }
659 
660 static const struct attribute_group typec_partner_group = {
661 	.is_visible = typec_partner_attr_is_visible,
662 	.attrs = typec_partner_attrs
663 };
664 
665 static const struct attribute_group *typec_partner_groups[] = {
666 	&typec_partner_group,
667 	NULL
668 };
669 
670 static void typec_partner_release(struct device *dev)
671 {
672 	struct typec_partner *partner = to_typec_partner(dev);
673 
674 	ida_destroy(&partner->mode_ids);
675 	kfree(partner);
676 }
677 
678 const struct device_type typec_partner_dev_type = {
679 	.name = "typec_partner",
680 	.groups = typec_partner_groups,
681 	.release = typec_partner_release,
682 };
683 
684 /**
685  * typec_partner_set_identity - Report result from Discover Identity command
686  * @partner: The partner updated identity values
687  *
688  * This routine is used to report that the result of Discover Identity USB power
689  * delivery command has become available.
690  */
691 int typec_partner_set_identity(struct typec_partner *partner)
692 {
693 	if (!partner->identity)
694 		return -EINVAL;
695 
696 	typec_report_identity(&partner->dev);
697 	return 0;
698 }
699 EXPORT_SYMBOL_GPL(typec_partner_set_identity);
700 
701 /**
702  * typec_partner_set_pd_revision - Set the PD revision supported by the partner
703  * @partner: The partner to be updated.
704  * @pd_revision:  USB Power Delivery Specification Revision supported by partner
705  *
706  * This routine is used to report that the PD revision of the port partner has
707  * become available.
708  */
709 void typec_partner_set_pd_revision(struct typec_partner *partner, u16 pd_revision)
710 {
711 	if (partner->pd_revision == pd_revision)
712 		return;
713 
714 	partner->pd_revision = pd_revision;
715 	sysfs_notify(&partner->dev.kobj, NULL, "usb_power_delivery_revision");
716 	if (pd_revision != 0 && !partner->usb_pd) {
717 		partner->usb_pd = 1;
718 		sysfs_notify(&partner->dev.kobj, NULL,
719 			     "supports_usb_power_delivery");
720 	}
721 	kobject_uevent(&partner->dev.kobj, KOBJ_CHANGE);
722 }
723 EXPORT_SYMBOL_GPL(typec_partner_set_pd_revision);
724 
725 /**
726  * typec_partner_set_usb_power_delivery - Declare USB Power Delivery Contract.
727  * @partner: The partner device.
728  * @pd: The USB PD instance.
729  *
730  * This routine can be used to declare USB Power Delivery Contract with @partner
731  * by linking @partner to @pd which contains the objects that were used during the
732  * negotiation of the contract.
733  *
734  * If @pd is NULL, the link is removed and the contract with @partner has ended.
735  */
736 int typec_partner_set_usb_power_delivery(struct typec_partner *partner,
737 					 struct usb_power_delivery *pd)
738 {
739 	int ret;
740 
741 	if (IS_ERR_OR_NULL(partner) || partner->pd == pd)
742 		return 0;
743 
744 	if (pd) {
745 		ret = usb_power_delivery_link_device(pd, &partner->dev);
746 		if (ret)
747 			return ret;
748 	} else {
749 		usb_power_delivery_unlink_device(partner->pd, &partner->dev);
750 	}
751 
752 	partner->pd = pd;
753 
754 	return 0;
755 }
756 EXPORT_SYMBOL_GPL(typec_partner_set_usb_power_delivery);
757 
758 /**
759  * typec_partner_set_num_altmodes - Set the number of available partner altmodes
760  * @partner: The partner to be updated.
761  * @num_altmodes: The number of altmodes we want to specify as available.
762  *
763  * This routine is used to report the number of alternate modes supported by the
764  * partner. This value is *not* enforced in alternate mode registration routines.
765  *
766  * @partner.num_altmodes is set to -1 on partner registration, denoting that
767  * a valid value has not been set for it yet.
768  *
769  * Returns 0 on success or negative error number on failure.
770  */
771 int typec_partner_set_num_altmodes(struct typec_partner *partner, int num_altmodes)
772 {
773 	int ret;
774 
775 	if (num_altmodes < 0)
776 		return -EINVAL;
777 
778 	partner->num_altmodes = num_altmodes;
779 	ret = sysfs_update_group(&partner->dev.kobj, &typec_partner_group);
780 	if (ret < 0)
781 		return ret;
782 
783 	sysfs_notify(&partner->dev.kobj, NULL, "number_of_alternate_modes");
784 	kobject_uevent(&partner->dev.kobj, KOBJ_CHANGE);
785 
786 	return 0;
787 }
788 EXPORT_SYMBOL_GPL(typec_partner_set_num_altmodes);
789 
790 /**
791  * typec_partner_register_altmode - Register USB Type-C Partner Alternate Mode
792  * @partner: USB Type-C Partner that supports the alternate mode
793  * @desc: Description of the alternate mode
794  *
795  * This routine is used to register each alternate mode individually that
796  * @partner has listed in response to Discover SVIDs command. The modes for a
797  * SVID listed in response to Discover Modes command need to be listed in an
798  * array in @desc.
799  *
800  * Returns handle to the alternate mode on success or ERR_PTR on failure.
801  */
802 struct typec_altmode *
803 typec_partner_register_altmode(struct typec_partner *partner,
804 			       const struct typec_altmode_desc *desc)
805 {
806 	return typec_register_altmode(&partner->dev, desc);
807 }
808 EXPORT_SYMBOL_GPL(typec_partner_register_altmode);
809 
810 /**
811  * typec_partner_set_svdm_version - Set negotiated Structured VDM (SVDM) Version
812  * @partner: USB Type-C Partner that supports SVDM
813  * @svdm_version: Negotiated SVDM Version
814  *
815  * This routine is used to save the negotiated SVDM Version.
816  */
817 void typec_partner_set_svdm_version(struct typec_partner *partner,
818 				   enum usb_pd_svdm_ver svdm_version)
819 {
820 	partner->svdm_version = svdm_version;
821 }
822 EXPORT_SYMBOL_GPL(typec_partner_set_svdm_version);
823 
824 /**
825  * typec_partner_usb_power_delivery_register - Register Type-C partner USB Power Delivery Support
826  * @partner: Type-C partner device.
827  * @desc: Description of the USB PD contract.
828  *
829  * This routine is a wrapper around usb_power_delivery_register(). It registers
830  * USB Power Delivery Capabilities for a Type-C partner device. Specifically,
831  * it sets the Type-C partner device as a parent for the resulting USB Power Delivery object.
832  *
833  * Returns handle to struct usb_power_delivery or ERR_PTR.
834  */
835 struct usb_power_delivery *
836 typec_partner_usb_power_delivery_register(struct typec_partner *partner,
837 					  struct usb_power_delivery_desc *desc)
838 {
839 	return usb_power_delivery_register(&partner->dev, desc);
840 }
841 EXPORT_SYMBOL_GPL(typec_partner_usb_power_delivery_register);
842 
843 /**
844  * typec_register_partner - Register a USB Type-C Partner
845  * @port: The USB Type-C Port the partner is connected to
846  * @desc: Description of the partner
847  *
848  * Registers a device for USB Type-C Partner described in @desc.
849  *
850  * Returns handle to the partner on success or ERR_PTR on failure.
851  */
852 struct typec_partner *typec_register_partner(struct typec_port *port,
853 					     struct typec_partner_desc *desc)
854 {
855 	struct typec_partner *partner;
856 	int ret;
857 
858 	partner = kzalloc(sizeof(*partner), GFP_KERNEL);
859 	if (!partner)
860 		return ERR_PTR(-ENOMEM);
861 
862 	ida_init(&partner->mode_ids);
863 	partner->usb_pd = desc->usb_pd;
864 	partner->accessory = desc->accessory;
865 	partner->num_altmodes = -1;
866 	partner->pd_revision = desc->pd_revision;
867 	partner->svdm_version = port->cap->svdm_version;
868 
869 	if (desc->identity) {
870 		/*
871 		 * Creating directory for the identity only if the driver is
872 		 * able to provide data to it.
873 		 */
874 		partner->dev.groups = usb_pd_id_groups;
875 		partner->identity = desc->identity;
876 	}
877 
878 	partner->dev.class = &typec_class;
879 	partner->dev.parent = &port->dev;
880 	partner->dev.type = &typec_partner_dev_type;
881 	dev_set_name(&partner->dev, "%s-partner", dev_name(&port->dev));
882 
883 	ret = device_register(&partner->dev);
884 	if (ret) {
885 		dev_err(&port->dev, "failed to register partner (%d)\n", ret);
886 		put_device(&partner->dev);
887 		return ERR_PTR(ret);
888 	}
889 
890 	return partner;
891 }
892 EXPORT_SYMBOL_GPL(typec_register_partner);
893 
894 /**
895  * typec_unregister_partner - Unregister a USB Type-C Partner
896  * @partner: The partner to be unregistered
897  *
898  * Unregister device created with typec_register_partner().
899  */
900 void typec_unregister_partner(struct typec_partner *partner)
901 {
902 	if (!IS_ERR_OR_NULL(partner))
903 		device_unregister(&partner->dev);
904 }
905 EXPORT_SYMBOL_GPL(typec_unregister_partner);
906 
907 /* ------------------------------------------------------------------------- */
908 /* Type-C Cable Plugs */
909 
910 static void typec_plug_release(struct device *dev)
911 {
912 	struct typec_plug *plug = to_typec_plug(dev);
913 
914 	ida_destroy(&plug->mode_ids);
915 	kfree(plug);
916 }
917 
918 static struct attribute *typec_plug_attrs[] = {
919 	&dev_attr_number_of_alternate_modes.attr,
920 	NULL
921 };
922 
923 static umode_t typec_plug_attr_is_visible(struct kobject *kobj, struct attribute *attr, int n)
924 {
925 	struct typec_plug *plug = to_typec_plug(kobj_to_dev(kobj));
926 
927 	if (attr == &dev_attr_number_of_alternate_modes.attr) {
928 		if (plug->num_altmodes < 0)
929 			return 0;
930 	}
931 
932 	return attr->mode;
933 }
934 
935 static const struct attribute_group typec_plug_group = {
936 	.is_visible = typec_plug_attr_is_visible,
937 	.attrs = typec_plug_attrs
938 };
939 
940 static const struct attribute_group *typec_plug_groups[] = {
941 	&typec_plug_group,
942 	NULL
943 };
944 
945 const struct device_type typec_plug_dev_type = {
946 	.name = "typec_plug",
947 	.groups = typec_plug_groups,
948 	.release = typec_plug_release,
949 };
950 
951 /**
952  * typec_plug_set_num_altmodes - Set the number of available plug altmodes
953  * @plug: The plug to be updated.
954  * @num_altmodes: The number of altmodes we want to specify as available.
955  *
956  * This routine is used to report the number of alternate modes supported by the
957  * plug. This value is *not* enforced in alternate mode registration routines.
958  *
959  * @plug.num_altmodes is set to -1 on plug registration, denoting that
960  * a valid value has not been set for it yet.
961  *
962  * Returns 0 on success or negative error number on failure.
963  */
964 int typec_plug_set_num_altmodes(struct typec_plug *plug, int num_altmodes)
965 {
966 	int ret;
967 
968 	if (num_altmodes < 0)
969 		return -EINVAL;
970 
971 	plug->num_altmodes = num_altmodes;
972 	ret = sysfs_update_group(&plug->dev.kobj, &typec_plug_group);
973 	if (ret < 0)
974 		return ret;
975 
976 	sysfs_notify(&plug->dev.kobj, NULL, "number_of_alternate_modes");
977 	kobject_uevent(&plug->dev.kobj, KOBJ_CHANGE);
978 
979 	return 0;
980 }
981 EXPORT_SYMBOL_GPL(typec_plug_set_num_altmodes);
982 
983 /**
984  * typec_plug_register_altmode - Register USB Type-C Cable Plug Alternate Mode
985  * @plug: USB Type-C Cable Plug that supports the alternate mode
986  * @desc: Description of the alternate mode
987  *
988  * This routine is used to register each alternate mode individually that @plug
989  * has listed in response to Discover SVIDs command. The modes for a SVID that
990  * the plug lists in response to Discover Modes command need to be listed in an
991  * array in @desc.
992  *
993  * Returns handle to the alternate mode on success or ERR_PTR on failure.
994  */
995 struct typec_altmode *
996 typec_plug_register_altmode(struct typec_plug *plug,
997 			    const struct typec_altmode_desc *desc)
998 {
999 	return typec_register_altmode(&plug->dev, desc);
1000 }
1001 EXPORT_SYMBOL_GPL(typec_plug_register_altmode);
1002 
1003 /**
1004  * typec_register_plug - Register a USB Type-C Cable Plug
1005  * @cable: USB Type-C Cable with the plug
1006  * @desc: Description of the cable plug
1007  *
1008  * Registers a device for USB Type-C Cable Plug described in @desc. A USB Type-C
1009  * Cable Plug represents a plug with electronics in it that can response to USB
1010  * Power Delivery SOP Prime or SOP Double Prime packages.
1011  *
1012  * Returns handle to the cable plug on success or ERR_PTR on failure.
1013  */
1014 struct typec_plug *typec_register_plug(struct typec_cable *cable,
1015 				       struct typec_plug_desc *desc)
1016 {
1017 	struct typec_plug *plug;
1018 	char name[8];
1019 	int ret;
1020 
1021 	plug = kzalloc(sizeof(*plug), GFP_KERNEL);
1022 	if (!plug)
1023 		return ERR_PTR(-ENOMEM);
1024 
1025 	sprintf(name, "plug%d", desc->index);
1026 
1027 	ida_init(&plug->mode_ids);
1028 	plug->num_altmodes = -1;
1029 	plug->index = desc->index;
1030 	plug->dev.class = &typec_class;
1031 	plug->dev.parent = &cable->dev;
1032 	plug->dev.type = &typec_plug_dev_type;
1033 	dev_set_name(&plug->dev, "%s-%s", dev_name(cable->dev.parent), name);
1034 
1035 	ret = device_register(&plug->dev);
1036 	if (ret) {
1037 		dev_err(&cable->dev, "failed to register plug (%d)\n", ret);
1038 		put_device(&plug->dev);
1039 		return ERR_PTR(ret);
1040 	}
1041 
1042 	return plug;
1043 }
1044 EXPORT_SYMBOL_GPL(typec_register_plug);
1045 
1046 /**
1047  * typec_unregister_plug - Unregister a USB Type-C Cable Plug
1048  * @plug: The cable plug to be unregistered
1049  *
1050  * Unregister device created with typec_register_plug().
1051  */
1052 void typec_unregister_plug(struct typec_plug *plug)
1053 {
1054 	if (!IS_ERR_OR_NULL(plug))
1055 		device_unregister(&plug->dev);
1056 }
1057 EXPORT_SYMBOL_GPL(typec_unregister_plug);
1058 
1059 /* Type-C Cables */
1060 
1061 static const char * const typec_plug_types[] = {
1062 	[USB_PLUG_NONE]		= "unknown",
1063 	[USB_PLUG_TYPE_A]	= "type-a",
1064 	[USB_PLUG_TYPE_B]	= "type-b",
1065 	[USB_PLUG_TYPE_C]	= "type-c",
1066 	[USB_PLUG_CAPTIVE]	= "captive",
1067 };
1068 
1069 static ssize_t plug_type_show(struct device *dev,
1070 			      struct device_attribute *attr, char *buf)
1071 {
1072 	struct typec_cable *cable = to_typec_cable(dev);
1073 
1074 	return sprintf(buf, "%s\n", typec_plug_types[cable->type]);
1075 }
1076 static DEVICE_ATTR_RO(plug_type);
1077 
1078 static struct attribute *typec_cable_attrs[] = {
1079 	&dev_attr_type.attr,
1080 	&dev_attr_plug_type.attr,
1081 	&dev_attr_usb_power_delivery_revision.attr,
1082 	NULL
1083 };
1084 ATTRIBUTE_GROUPS(typec_cable);
1085 
1086 static void typec_cable_release(struct device *dev)
1087 {
1088 	struct typec_cable *cable = to_typec_cable(dev);
1089 
1090 	kfree(cable);
1091 }
1092 
1093 const struct device_type typec_cable_dev_type = {
1094 	.name = "typec_cable",
1095 	.groups = typec_cable_groups,
1096 	.release = typec_cable_release,
1097 };
1098 
1099 static int cable_match(struct device *dev, void *data)
1100 {
1101 	return is_typec_cable(dev);
1102 }
1103 
1104 /**
1105  * typec_cable_get - Get a reference to the USB Type-C cable
1106  * @port: The USB Type-C Port the cable is connected to
1107  *
1108  * The caller must decrement the reference count with typec_cable_put() after
1109  * use.
1110  */
1111 struct typec_cable *typec_cable_get(struct typec_port *port)
1112 {
1113 	struct device *dev;
1114 
1115 	dev = device_find_child(&port->dev, NULL, cable_match);
1116 	if (!dev)
1117 		return NULL;
1118 
1119 	return to_typec_cable(dev);
1120 }
1121 EXPORT_SYMBOL_GPL(typec_cable_get);
1122 
1123 /**
1124  * typec_cable_put - Decrement the reference count on USB Type-C cable
1125  * @cable: The USB Type-C cable
1126  */
1127 void typec_cable_put(struct typec_cable *cable)
1128 {
1129 	put_device(&cable->dev);
1130 }
1131 EXPORT_SYMBOL_GPL(typec_cable_put);
1132 
1133 /**
1134  * typec_cable_is_active - Check is the USB Type-C cable active or passive
1135  * @cable: The USB Type-C Cable
1136  *
1137  * Return 1 if the cable is active or 0 if it's passive.
1138  */
1139 int typec_cable_is_active(struct typec_cable *cable)
1140 {
1141 	return cable->active;
1142 }
1143 EXPORT_SYMBOL_GPL(typec_cable_is_active);
1144 
1145 /**
1146  * typec_cable_set_identity - Report result from Discover Identity command
1147  * @cable: The cable updated identity values
1148  *
1149  * This routine is used to report that the result of Discover Identity USB power
1150  * delivery command has become available.
1151  */
1152 int typec_cable_set_identity(struct typec_cable *cable)
1153 {
1154 	if (!cable->identity)
1155 		return -EINVAL;
1156 
1157 	typec_report_identity(&cable->dev);
1158 	return 0;
1159 }
1160 EXPORT_SYMBOL_GPL(typec_cable_set_identity);
1161 
1162 /**
1163  * typec_register_cable - Register a USB Type-C Cable
1164  * @port: The USB Type-C Port the cable is connected to
1165  * @desc: Description of the cable
1166  *
1167  * Registers a device for USB Type-C Cable described in @desc. The cable will be
1168  * parent for the optional cable plug devises.
1169  *
1170  * Returns handle to the cable on success or ERR_PTR on failure.
1171  */
1172 struct typec_cable *typec_register_cable(struct typec_port *port,
1173 					 struct typec_cable_desc *desc)
1174 {
1175 	struct typec_cable *cable;
1176 	int ret;
1177 
1178 	cable = kzalloc(sizeof(*cable), GFP_KERNEL);
1179 	if (!cable)
1180 		return ERR_PTR(-ENOMEM);
1181 
1182 	cable->type = desc->type;
1183 	cable->active = desc->active;
1184 	cable->pd_revision = desc->pd_revision;
1185 
1186 	if (desc->identity) {
1187 		/*
1188 		 * Creating directory for the identity only if the driver is
1189 		 * able to provide data to it.
1190 		 */
1191 		cable->dev.groups = usb_pd_id_groups;
1192 		cable->identity = desc->identity;
1193 	}
1194 
1195 	cable->dev.class = &typec_class;
1196 	cable->dev.parent = &port->dev;
1197 	cable->dev.type = &typec_cable_dev_type;
1198 	dev_set_name(&cable->dev, "%s-cable", dev_name(&port->dev));
1199 
1200 	ret = device_register(&cable->dev);
1201 	if (ret) {
1202 		dev_err(&port->dev, "failed to register cable (%d)\n", ret);
1203 		put_device(&cable->dev);
1204 		return ERR_PTR(ret);
1205 	}
1206 
1207 	return cable;
1208 }
1209 EXPORT_SYMBOL_GPL(typec_register_cable);
1210 
1211 /**
1212  * typec_unregister_cable - Unregister a USB Type-C Cable
1213  * @cable: The cable to be unregistered
1214  *
1215  * Unregister device created with typec_register_cable().
1216  */
1217 void typec_unregister_cable(struct typec_cable *cable)
1218 {
1219 	if (!IS_ERR_OR_NULL(cable))
1220 		device_unregister(&cable->dev);
1221 }
1222 EXPORT_SYMBOL_GPL(typec_unregister_cable);
1223 
1224 /* ------------------------------------------------------------------------- */
1225 /* USB Type-C ports */
1226 
1227 /**
1228  * typec_port_set_usb_power_delivery - Assign USB PD for port.
1229  * @port: USB Type-C port.
1230  * @pd: USB PD instance.
1231  *
1232  * This routine can be used to set the USB Power Delivery Capabilities for @port
1233  * that it will advertise to the partner.
1234  *
1235  * If @pd is NULL, the assignment is removed.
1236  */
1237 int typec_port_set_usb_power_delivery(struct typec_port *port, struct usb_power_delivery *pd)
1238 {
1239 	int ret;
1240 
1241 	if (IS_ERR_OR_NULL(port) || port->pd == pd)
1242 		return 0;
1243 
1244 	if (pd) {
1245 		ret = usb_power_delivery_link_device(pd, &port->dev);
1246 		if (ret)
1247 			return ret;
1248 	} else {
1249 		usb_power_delivery_unlink_device(port->pd, &port->dev);
1250 	}
1251 
1252 	port->pd = pd;
1253 
1254 	return 0;
1255 }
1256 EXPORT_SYMBOL_GPL(typec_port_set_usb_power_delivery);
1257 
1258 static ssize_t select_usb_power_delivery_store(struct device *dev,
1259 					       struct device_attribute *attr,
1260 					       const char *buf, size_t size)
1261 {
1262 	struct typec_port *port = to_typec_port(dev);
1263 	struct usb_power_delivery *pd;
1264 
1265 	if (!port->ops || !port->ops->pd_set)
1266 		return -EOPNOTSUPP;
1267 
1268 	pd = usb_power_delivery_find(buf);
1269 	if (!pd)
1270 		return -EINVAL;
1271 
1272 	return port->ops->pd_set(port, pd);
1273 }
1274 
1275 static ssize_t select_usb_power_delivery_show(struct device *dev,
1276 					      struct device_attribute *attr, char *buf)
1277 {
1278 	struct typec_port *port = to_typec_port(dev);
1279 	struct usb_power_delivery **pds;
1280 	int i, ret = 0;
1281 
1282 	if (!port->ops || !port->ops->pd_get)
1283 		return -EOPNOTSUPP;
1284 
1285 	pds = port->ops->pd_get(port);
1286 	if (!pds)
1287 		return 0;
1288 
1289 	for (i = 0; pds[i]; i++) {
1290 		if (pds[i] == port->pd)
1291 			ret += sysfs_emit_at(buf, ret, "[%s] ", dev_name(&pds[i]->dev));
1292 		else
1293 			ret += sysfs_emit_at(buf, ret, "%s ", dev_name(&pds[i]->dev));
1294 	}
1295 
1296 	buf[ret - 1] = '\n';
1297 
1298 	return ret;
1299 }
1300 static DEVICE_ATTR_RW(select_usb_power_delivery);
1301 
1302 static struct attribute *port_attrs[] = {
1303 	&dev_attr_select_usb_power_delivery.attr,
1304 	NULL
1305 };
1306 
1307 static umode_t port_attr_is_visible(struct kobject *kobj, struct attribute *attr, int n)
1308 {
1309 	struct typec_port *port = to_typec_port(kobj_to_dev(kobj));
1310 
1311 	if (!port->pd || !port->ops || !port->ops->pd_get)
1312 		return 0;
1313 	if (!port->ops->pd_set)
1314 		return 0444;
1315 
1316 	return attr->mode;
1317 }
1318 
1319 static const struct attribute_group pd_group = {
1320 	.is_visible = port_attr_is_visible,
1321 	.attrs = port_attrs,
1322 };
1323 
1324 static const char * const typec_orientations[] = {
1325 	[TYPEC_ORIENTATION_NONE]	= "unknown",
1326 	[TYPEC_ORIENTATION_NORMAL]	= "normal",
1327 	[TYPEC_ORIENTATION_REVERSE]	= "reverse",
1328 };
1329 
1330 static const char * const typec_roles[] = {
1331 	[TYPEC_SINK]	= "sink",
1332 	[TYPEC_SOURCE]	= "source",
1333 };
1334 
1335 static const char * const typec_data_roles[] = {
1336 	[TYPEC_DEVICE]	= "device",
1337 	[TYPEC_HOST]	= "host",
1338 };
1339 
1340 static const char * const typec_port_power_roles[] = {
1341 	[TYPEC_PORT_SRC] = "source",
1342 	[TYPEC_PORT_SNK] = "sink",
1343 	[TYPEC_PORT_DRP] = "dual",
1344 };
1345 
1346 static const char * const typec_port_data_roles[] = {
1347 	[TYPEC_PORT_DFP] = "host",
1348 	[TYPEC_PORT_UFP] = "device",
1349 	[TYPEC_PORT_DRD] = "dual",
1350 };
1351 
1352 static const char * const typec_port_types_drp[] = {
1353 	[TYPEC_PORT_SRC] = "dual [source] sink",
1354 	[TYPEC_PORT_SNK] = "dual source [sink]",
1355 	[TYPEC_PORT_DRP] = "[dual] source sink",
1356 };
1357 
1358 static ssize_t
1359 preferred_role_store(struct device *dev, struct device_attribute *attr,
1360 		     const char *buf, size_t size)
1361 {
1362 	struct typec_port *port = to_typec_port(dev);
1363 	int role;
1364 	int ret;
1365 
1366 	if (port->cap->type != TYPEC_PORT_DRP) {
1367 		dev_dbg(dev, "Preferred role only supported with DRP ports\n");
1368 		return -EOPNOTSUPP;
1369 	}
1370 
1371 	if (!port->ops || !port->ops->try_role) {
1372 		dev_dbg(dev, "Setting preferred role not supported\n");
1373 		return -EOPNOTSUPP;
1374 	}
1375 
1376 	role = sysfs_match_string(typec_roles, buf);
1377 	if (role < 0) {
1378 		if (sysfs_streq(buf, "none"))
1379 			role = TYPEC_NO_PREFERRED_ROLE;
1380 		else
1381 			return -EINVAL;
1382 	}
1383 
1384 	ret = port->ops->try_role(port, role);
1385 	if (ret)
1386 		return ret;
1387 
1388 	port->prefer_role = role;
1389 	return size;
1390 }
1391 
1392 static ssize_t
1393 preferred_role_show(struct device *dev, struct device_attribute *attr,
1394 		    char *buf)
1395 {
1396 	struct typec_port *port = to_typec_port(dev);
1397 
1398 	if (port->cap->type != TYPEC_PORT_DRP)
1399 		return 0;
1400 
1401 	if (port->prefer_role < 0)
1402 		return 0;
1403 
1404 	return sprintf(buf, "%s\n", typec_roles[port->prefer_role]);
1405 }
1406 static DEVICE_ATTR_RW(preferred_role);
1407 
1408 static ssize_t data_role_store(struct device *dev,
1409 			       struct device_attribute *attr,
1410 			       const char *buf, size_t size)
1411 {
1412 	struct typec_port *port = to_typec_port(dev);
1413 	int ret;
1414 
1415 	if (!port->ops || !port->ops->dr_set) {
1416 		dev_dbg(dev, "data role swapping not supported\n");
1417 		return -EOPNOTSUPP;
1418 	}
1419 
1420 	ret = sysfs_match_string(typec_data_roles, buf);
1421 	if (ret < 0)
1422 		return ret;
1423 
1424 	mutex_lock(&port->port_type_lock);
1425 	if (port->cap->data != TYPEC_PORT_DRD) {
1426 		ret = -EOPNOTSUPP;
1427 		goto unlock_and_ret;
1428 	}
1429 
1430 	ret = port->ops->dr_set(port, ret);
1431 	if (ret)
1432 		goto unlock_and_ret;
1433 
1434 	ret = size;
1435 unlock_and_ret:
1436 	mutex_unlock(&port->port_type_lock);
1437 	return ret;
1438 }
1439 
1440 static ssize_t data_role_show(struct device *dev,
1441 			      struct device_attribute *attr, char *buf)
1442 {
1443 	struct typec_port *port = to_typec_port(dev);
1444 
1445 	if (port->cap->data == TYPEC_PORT_DRD)
1446 		return sprintf(buf, "%s\n", port->data_role == TYPEC_HOST ?
1447 			       "[host] device" : "host [device]");
1448 
1449 	return sprintf(buf, "[%s]\n", typec_data_roles[port->data_role]);
1450 }
1451 static DEVICE_ATTR_RW(data_role);
1452 
1453 static ssize_t power_role_store(struct device *dev,
1454 				struct device_attribute *attr,
1455 				const char *buf, size_t size)
1456 {
1457 	struct typec_port *port = to_typec_port(dev);
1458 	int ret;
1459 
1460 	if (!port->ops || !port->ops->pr_set) {
1461 		dev_dbg(dev, "power role swapping not supported\n");
1462 		return -EOPNOTSUPP;
1463 	}
1464 
1465 	if (port->pwr_opmode != TYPEC_PWR_MODE_PD) {
1466 		dev_dbg(dev, "partner unable to swap power role\n");
1467 		return -EIO;
1468 	}
1469 
1470 	ret = sysfs_match_string(typec_roles, buf);
1471 	if (ret < 0)
1472 		return ret;
1473 
1474 	mutex_lock(&port->port_type_lock);
1475 	if (port->port_type != TYPEC_PORT_DRP) {
1476 		dev_dbg(dev, "port type fixed at \"%s\"",
1477 			     typec_port_power_roles[port->port_type]);
1478 		ret = -EOPNOTSUPP;
1479 		goto unlock_and_ret;
1480 	}
1481 
1482 	ret = port->ops->pr_set(port, ret);
1483 	if (ret)
1484 		goto unlock_and_ret;
1485 
1486 	ret = size;
1487 unlock_and_ret:
1488 	mutex_unlock(&port->port_type_lock);
1489 	return ret;
1490 }
1491 
1492 static ssize_t power_role_show(struct device *dev,
1493 			       struct device_attribute *attr, char *buf)
1494 {
1495 	struct typec_port *port = to_typec_port(dev);
1496 
1497 	if (port->cap->type == TYPEC_PORT_DRP)
1498 		return sprintf(buf, "%s\n", port->pwr_role == TYPEC_SOURCE ?
1499 			       "[source] sink" : "source [sink]");
1500 
1501 	return sprintf(buf, "[%s]\n", typec_roles[port->pwr_role]);
1502 }
1503 static DEVICE_ATTR_RW(power_role);
1504 
1505 static ssize_t
1506 port_type_store(struct device *dev, struct device_attribute *attr,
1507 			const char *buf, size_t size)
1508 {
1509 	struct typec_port *port = to_typec_port(dev);
1510 	int ret;
1511 	enum typec_port_type type;
1512 
1513 	if (port->cap->type != TYPEC_PORT_DRP ||
1514 	    !port->ops || !port->ops->port_type_set) {
1515 		dev_dbg(dev, "changing port type not supported\n");
1516 		return -EOPNOTSUPP;
1517 	}
1518 
1519 	ret = sysfs_match_string(typec_port_power_roles, buf);
1520 	if (ret < 0)
1521 		return ret;
1522 
1523 	type = ret;
1524 	mutex_lock(&port->port_type_lock);
1525 
1526 	if (port->port_type == type) {
1527 		ret = size;
1528 		goto unlock_and_ret;
1529 	}
1530 
1531 	ret = port->ops->port_type_set(port, type);
1532 	if (ret)
1533 		goto unlock_and_ret;
1534 
1535 	port->port_type = type;
1536 	ret = size;
1537 
1538 unlock_and_ret:
1539 	mutex_unlock(&port->port_type_lock);
1540 	return ret;
1541 }
1542 
1543 static ssize_t
1544 port_type_show(struct device *dev, struct device_attribute *attr,
1545 		char *buf)
1546 {
1547 	struct typec_port *port = to_typec_port(dev);
1548 
1549 	if (port->cap->type == TYPEC_PORT_DRP)
1550 		return sprintf(buf, "%s\n",
1551 			       typec_port_types_drp[port->port_type]);
1552 
1553 	return sprintf(buf, "[%s]\n", typec_port_power_roles[port->cap->type]);
1554 }
1555 static DEVICE_ATTR_RW(port_type);
1556 
1557 static const char * const typec_pwr_opmodes[] = {
1558 	[TYPEC_PWR_MODE_USB]	= "default",
1559 	[TYPEC_PWR_MODE_1_5A]	= "1.5A",
1560 	[TYPEC_PWR_MODE_3_0A]	= "3.0A",
1561 	[TYPEC_PWR_MODE_PD]	= "usb_power_delivery",
1562 };
1563 
1564 static ssize_t power_operation_mode_show(struct device *dev,
1565 					 struct device_attribute *attr,
1566 					 char *buf)
1567 {
1568 	struct typec_port *port = to_typec_port(dev);
1569 
1570 	return sprintf(buf, "%s\n", typec_pwr_opmodes[port->pwr_opmode]);
1571 }
1572 static DEVICE_ATTR_RO(power_operation_mode);
1573 
1574 static ssize_t vconn_source_store(struct device *dev,
1575 				  struct device_attribute *attr,
1576 				  const char *buf, size_t size)
1577 {
1578 	struct typec_port *port = to_typec_port(dev);
1579 	bool source;
1580 	int ret;
1581 
1582 	if (!port->cap->pd_revision) {
1583 		dev_dbg(dev, "VCONN swap depends on USB Power Delivery\n");
1584 		return -EOPNOTSUPP;
1585 	}
1586 
1587 	if (!port->ops || !port->ops->vconn_set) {
1588 		dev_dbg(dev, "VCONN swapping not supported\n");
1589 		return -EOPNOTSUPP;
1590 	}
1591 
1592 	ret = kstrtobool(buf, &source);
1593 	if (ret)
1594 		return ret;
1595 
1596 	ret = port->ops->vconn_set(port, (enum typec_role)source);
1597 	if (ret)
1598 		return ret;
1599 
1600 	return size;
1601 }
1602 
1603 static ssize_t vconn_source_show(struct device *dev,
1604 				 struct device_attribute *attr, char *buf)
1605 {
1606 	struct typec_port *port = to_typec_port(dev);
1607 
1608 	return sprintf(buf, "%s\n",
1609 		       port->vconn_role == TYPEC_SOURCE ? "yes" : "no");
1610 }
1611 static DEVICE_ATTR_RW(vconn_source);
1612 
1613 static ssize_t supported_accessory_modes_show(struct device *dev,
1614 					      struct device_attribute *attr,
1615 					      char *buf)
1616 {
1617 	struct typec_port *port = to_typec_port(dev);
1618 	ssize_t ret = 0;
1619 	int i;
1620 
1621 	for (i = 0; i < ARRAY_SIZE(port->cap->accessory); i++) {
1622 		if (port->cap->accessory[i])
1623 			ret += sprintf(buf + ret, "%s ",
1624 			       typec_accessory_modes[port->cap->accessory[i]]);
1625 	}
1626 
1627 	if (!ret)
1628 		return sprintf(buf, "none\n");
1629 
1630 	buf[ret - 1] = '\n';
1631 
1632 	return ret;
1633 }
1634 static DEVICE_ATTR_RO(supported_accessory_modes);
1635 
1636 static ssize_t usb_typec_revision_show(struct device *dev,
1637 				       struct device_attribute *attr,
1638 				       char *buf)
1639 {
1640 	struct typec_port *port = to_typec_port(dev);
1641 	u16 rev = port->cap->revision;
1642 
1643 	return sprintf(buf, "%d.%d\n", (rev >> 8) & 0xff, (rev >> 4) & 0xf);
1644 }
1645 static DEVICE_ATTR_RO(usb_typec_revision);
1646 
1647 static ssize_t usb_power_delivery_revision_show(struct device *dev,
1648 						struct device_attribute *attr,
1649 						char *buf)
1650 {
1651 	u16 rev = 0;
1652 
1653 	if (is_typec_partner(dev)) {
1654 		struct typec_partner *partner = to_typec_partner(dev);
1655 
1656 		rev = partner->pd_revision;
1657 	} else if (is_typec_cable(dev)) {
1658 		struct typec_cable *cable = to_typec_cable(dev);
1659 
1660 		rev = cable->pd_revision;
1661 	} else if (is_typec_port(dev)) {
1662 		struct typec_port *p = to_typec_port(dev);
1663 
1664 		rev = p->cap->pd_revision;
1665 	}
1666 	return sysfs_emit(buf, "%d.%d\n", (rev >> 8) & 0xff, (rev >> 4) & 0xf);
1667 }
1668 
1669 static ssize_t orientation_show(struct device *dev,
1670 				   struct device_attribute *attr,
1671 				   char *buf)
1672 {
1673 	struct typec_port *port = to_typec_port(dev);
1674 
1675 	return sprintf(buf, "%s\n", typec_orientations[port->orientation]);
1676 }
1677 static DEVICE_ATTR_RO(orientation);
1678 
1679 static struct attribute *typec_attrs[] = {
1680 	&dev_attr_data_role.attr,
1681 	&dev_attr_power_operation_mode.attr,
1682 	&dev_attr_power_role.attr,
1683 	&dev_attr_preferred_role.attr,
1684 	&dev_attr_supported_accessory_modes.attr,
1685 	&dev_attr_usb_power_delivery_revision.attr,
1686 	&dev_attr_usb_typec_revision.attr,
1687 	&dev_attr_vconn_source.attr,
1688 	&dev_attr_port_type.attr,
1689 	&dev_attr_orientation.attr,
1690 	NULL,
1691 };
1692 
1693 static umode_t typec_attr_is_visible(struct kobject *kobj,
1694 				     struct attribute *attr, int n)
1695 {
1696 	struct typec_port *port = to_typec_port(kobj_to_dev(kobj));
1697 
1698 	if (attr == &dev_attr_data_role.attr) {
1699 		if (port->cap->data != TYPEC_PORT_DRD ||
1700 		    !port->ops || !port->ops->dr_set)
1701 			return 0444;
1702 	} else if (attr == &dev_attr_power_role.attr) {
1703 		if (port->cap->type != TYPEC_PORT_DRP ||
1704 		    !port->ops || !port->ops->pr_set)
1705 			return 0444;
1706 	} else if (attr == &dev_attr_vconn_source.attr) {
1707 		if (!port->cap->pd_revision ||
1708 		    !port->ops || !port->ops->vconn_set)
1709 			return 0444;
1710 	} else if (attr == &dev_attr_preferred_role.attr) {
1711 		if (port->cap->type != TYPEC_PORT_DRP ||
1712 		    !port->ops || !port->ops->try_role)
1713 			return 0444;
1714 	} else if (attr == &dev_attr_port_type.attr) {
1715 		if (!port->ops || !port->ops->port_type_set)
1716 			return 0;
1717 		if (port->cap->type != TYPEC_PORT_DRP)
1718 			return 0444;
1719 	} else if (attr == &dev_attr_orientation.attr) {
1720 		if (port->cap->orientation_aware)
1721 			return 0444;
1722 		return 0;
1723 	}
1724 
1725 	return attr->mode;
1726 }
1727 
1728 static const struct attribute_group typec_group = {
1729 	.is_visible = typec_attr_is_visible,
1730 	.attrs = typec_attrs,
1731 };
1732 
1733 static const struct attribute_group *typec_groups[] = {
1734 	&typec_group,
1735 	&pd_group,
1736 	NULL
1737 };
1738 
1739 static int typec_uevent(const struct device *dev, struct kobj_uevent_env *env)
1740 {
1741 	int ret;
1742 
1743 	ret = add_uevent_var(env, "TYPEC_PORT=%s", dev_name(dev));
1744 	if (ret)
1745 		dev_err(dev, "failed to add uevent TYPEC_PORT\n");
1746 
1747 	return ret;
1748 }
1749 
1750 static void typec_release(struct device *dev)
1751 {
1752 	struct typec_port *port = to_typec_port(dev);
1753 
1754 	ida_simple_remove(&typec_index_ida, port->id);
1755 	ida_destroy(&port->mode_ids);
1756 	typec_switch_put(port->sw);
1757 	typec_mux_put(port->mux);
1758 	typec_retimer_put(port->retimer);
1759 	kfree(port->cap);
1760 	kfree(port);
1761 }
1762 
1763 const struct device_type typec_port_dev_type = {
1764 	.name = "typec_port",
1765 	.groups = typec_groups,
1766 	.uevent = typec_uevent,
1767 	.release = typec_release,
1768 };
1769 
1770 /* --------------------------------------- */
1771 /* Driver callbacks to report role updates */
1772 
1773 static int partner_match(struct device *dev, void *data)
1774 {
1775 	return is_typec_partner(dev);
1776 }
1777 
1778 /**
1779  * typec_set_data_role - Report data role change
1780  * @port: The USB Type-C Port where the role was changed
1781  * @role: The new data role
1782  *
1783  * This routine is used by the port drivers to report data role changes.
1784  */
1785 void typec_set_data_role(struct typec_port *port, enum typec_data_role role)
1786 {
1787 	struct device *partner_dev;
1788 
1789 	if (port->data_role == role)
1790 		return;
1791 
1792 	port->data_role = role;
1793 	sysfs_notify(&port->dev.kobj, NULL, "data_role");
1794 	kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1795 
1796 	partner_dev = device_find_child(&port->dev, NULL, partner_match);
1797 	if (!partner_dev)
1798 		return;
1799 
1800 	if (to_typec_partner(partner_dev)->identity)
1801 		typec_product_type_notify(partner_dev);
1802 
1803 	put_device(partner_dev);
1804 }
1805 EXPORT_SYMBOL_GPL(typec_set_data_role);
1806 
1807 /**
1808  * typec_set_pwr_role - Report power role change
1809  * @port: The USB Type-C Port where the role was changed
1810  * @role: The new data role
1811  *
1812  * This routine is used by the port drivers to report power role changes.
1813  */
1814 void typec_set_pwr_role(struct typec_port *port, enum typec_role role)
1815 {
1816 	if (port->pwr_role == role)
1817 		return;
1818 
1819 	port->pwr_role = role;
1820 	sysfs_notify(&port->dev.kobj, NULL, "power_role");
1821 	kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1822 }
1823 EXPORT_SYMBOL_GPL(typec_set_pwr_role);
1824 
1825 /**
1826  * typec_set_vconn_role - Report VCONN source change
1827  * @port: The USB Type-C Port which VCONN role changed
1828  * @role: Source when @port is sourcing VCONN, or Sink when it's not
1829  *
1830  * This routine is used by the port drivers to report if the VCONN source is
1831  * changes.
1832  */
1833 void typec_set_vconn_role(struct typec_port *port, enum typec_role role)
1834 {
1835 	if (port->vconn_role == role)
1836 		return;
1837 
1838 	port->vconn_role = role;
1839 	sysfs_notify(&port->dev.kobj, NULL, "vconn_source");
1840 	kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1841 }
1842 EXPORT_SYMBOL_GPL(typec_set_vconn_role);
1843 
1844 /**
1845  * typec_set_pwr_opmode - Report changed power operation mode
1846  * @port: The USB Type-C Port where the mode was changed
1847  * @opmode: New power operation mode
1848  *
1849  * This routine is used by the port drivers to report changed power operation
1850  * mode in @port. The modes are USB (default), 1.5A, 3.0A as defined in USB
1851  * Type-C specification, and "USB Power Delivery" when the power levels are
1852  * negotiated with methods defined in USB Power Delivery specification.
1853  */
1854 void typec_set_pwr_opmode(struct typec_port *port,
1855 			  enum typec_pwr_opmode opmode)
1856 {
1857 	struct device *partner_dev;
1858 
1859 	if (port->pwr_opmode == opmode)
1860 		return;
1861 
1862 	port->pwr_opmode = opmode;
1863 	sysfs_notify(&port->dev.kobj, NULL, "power_operation_mode");
1864 	kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1865 
1866 	partner_dev = device_find_child(&port->dev, NULL, partner_match);
1867 	if (partner_dev) {
1868 		struct typec_partner *partner = to_typec_partner(partner_dev);
1869 
1870 		if (opmode == TYPEC_PWR_MODE_PD && !partner->usb_pd) {
1871 			partner->usb_pd = 1;
1872 			sysfs_notify(&partner_dev->kobj, NULL,
1873 				     "supports_usb_power_delivery");
1874 			kobject_uevent(&partner_dev->kobj, KOBJ_CHANGE);
1875 		}
1876 		put_device(partner_dev);
1877 	}
1878 }
1879 EXPORT_SYMBOL_GPL(typec_set_pwr_opmode);
1880 
1881 /**
1882  * typec_find_pwr_opmode - Get the typec power operation mode capability
1883  * @name: power operation mode string
1884  *
1885  * This routine is used to find the typec_pwr_opmode by its string @name.
1886  *
1887  * Returns typec_pwr_opmode if success, otherwise negative error code.
1888  */
1889 int typec_find_pwr_opmode(const char *name)
1890 {
1891 	return match_string(typec_pwr_opmodes,
1892 			    ARRAY_SIZE(typec_pwr_opmodes), name);
1893 }
1894 EXPORT_SYMBOL_GPL(typec_find_pwr_opmode);
1895 
1896 /**
1897  * typec_find_orientation - Convert orientation string to enum typec_orientation
1898  * @name: Orientation string
1899  *
1900  * This routine is used to find the typec_orientation by its string name @name.
1901  *
1902  * Returns the orientation value on success, otherwise negative error code.
1903  */
1904 int typec_find_orientation(const char *name)
1905 {
1906 	return match_string(typec_orientations, ARRAY_SIZE(typec_orientations),
1907 			    name);
1908 }
1909 EXPORT_SYMBOL_GPL(typec_find_orientation);
1910 
1911 /**
1912  * typec_find_port_power_role - Get the typec port power capability
1913  * @name: port power capability string
1914  *
1915  * This routine is used to find the typec_port_type by its string name.
1916  *
1917  * Returns typec_port_type if success, otherwise negative error code.
1918  */
1919 int typec_find_port_power_role(const char *name)
1920 {
1921 	return match_string(typec_port_power_roles,
1922 			    ARRAY_SIZE(typec_port_power_roles), name);
1923 }
1924 EXPORT_SYMBOL_GPL(typec_find_port_power_role);
1925 
1926 /**
1927  * typec_find_power_role - Find the typec one specific power role
1928  * @name: power role string
1929  *
1930  * This routine is used to find the typec_role by its string name.
1931  *
1932  * Returns typec_role if success, otherwise negative error code.
1933  */
1934 int typec_find_power_role(const char *name)
1935 {
1936 	return match_string(typec_roles, ARRAY_SIZE(typec_roles), name);
1937 }
1938 EXPORT_SYMBOL_GPL(typec_find_power_role);
1939 
1940 /**
1941  * typec_find_port_data_role - Get the typec port data capability
1942  * @name: port data capability string
1943  *
1944  * This routine is used to find the typec_port_data by its string name.
1945  *
1946  * Returns typec_port_data if success, otherwise negative error code.
1947  */
1948 int typec_find_port_data_role(const char *name)
1949 {
1950 	return match_string(typec_port_data_roles,
1951 			    ARRAY_SIZE(typec_port_data_roles), name);
1952 }
1953 EXPORT_SYMBOL_GPL(typec_find_port_data_role);
1954 
1955 /* ------------------------------------------ */
1956 /* API for Multiplexer/DeMultiplexer Switches */
1957 
1958 /**
1959  * typec_set_orientation - Set USB Type-C cable plug orientation
1960  * @port: USB Type-C Port
1961  * @orientation: USB Type-C cable plug orientation
1962  *
1963  * Set cable plug orientation for @port.
1964  */
1965 int typec_set_orientation(struct typec_port *port,
1966 			  enum typec_orientation orientation)
1967 {
1968 	int ret;
1969 
1970 	ret = typec_switch_set(port->sw, orientation);
1971 	if (ret)
1972 		return ret;
1973 
1974 	port->orientation = orientation;
1975 	sysfs_notify(&port->dev.kobj, NULL, "orientation");
1976 	kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1977 
1978 	return 0;
1979 }
1980 EXPORT_SYMBOL_GPL(typec_set_orientation);
1981 
1982 /**
1983  * typec_get_orientation - Get USB Type-C cable plug orientation
1984  * @port: USB Type-C Port
1985  *
1986  * Get current cable plug orientation for @port.
1987  */
1988 enum typec_orientation typec_get_orientation(struct typec_port *port)
1989 {
1990 	return port->orientation;
1991 }
1992 EXPORT_SYMBOL_GPL(typec_get_orientation);
1993 
1994 /**
1995  * typec_set_mode - Set mode of operation for USB Type-C connector
1996  * @port: USB Type-C connector
1997  * @mode: Accessory Mode, USB Operation or Safe State
1998  *
1999  * Configure @port for Accessory Mode @mode. This function will configure the
2000  * muxes needed for @mode.
2001  */
2002 int typec_set_mode(struct typec_port *port, int mode)
2003 {
2004 	struct typec_mux_state state = { };
2005 
2006 	state.mode = mode;
2007 
2008 	return typec_mux_set(port->mux, &state);
2009 }
2010 EXPORT_SYMBOL_GPL(typec_set_mode);
2011 
2012 /* --------------------------------------- */
2013 
2014 /**
2015  * typec_get_negotiated_svdm_version - Get negotiated SVDM Version
2016  * @port: USB Type-C Port.
2017  *
2018  * Get the negotiated SVDM Version. The Version is set to the port default
2019  * value stored in typec_capability on partner registration, and updated after
2020  * a successful Discover Identity if the negotiated value is less than the
2021  * default value.
2022  *
2023  * Returns usb_pd_svdm_ver if the partner has been registered otherwise -ENODEV.
2024  */
2025 int typec_get_negotiated_svdm_version(struct typec_port *port)
2026 {
2027 	enum usb_pd_svdm_ver svdm_version;
2028 	struct device *partner_dev;
2029 
2030 	partner_dev = device_find_child(&port->dev, NULL, partner_match);
2031 	if (!partner_dev)
2032 		return -ENODEV;
2033 
2034 	svdm_version = to_typec_partner(partner_dev)->svdm_version;
2035 	put_device(partner_dev);
2036 
2037 	return svdm_version;
2038 }
2039 EXPORT_SYMBOL_GPL(typec_get_negotiated_svdm_version);
2040 
2041 /**
2042  * typec_get_drvdata - Return private driver data pointer
2043  * @port: USB Type-C port
2044  */
2045 void *typec_get_drvdata(struct typec_port *port)
2046 {
2047 	return dev_get_drvdata(&port->dev);
2048 }
2049 EXPORT_SYMBOL_GPL(typec_get_drvdata);
2050 
2051 int typec_get_fw_cap(struct typec_capability *cap,
2052 		     struct fwnode_handle *fwnode)
2053 {
2054 	const char *cap_str;
2055 	int ret;
2056 
2057 	cap->fwnode = fwnode;
2058 
2059 	ret = fwnode_property_read_string(fwnode, "power-role", &cap_str);
2060 	if (ret < 0)
2061 		return ret;
2062 
2063 	ret = typec_find_port_power_role(cap_str);
2064 	if (ret < 0)
2065 		return ret;
2066 	cap->type = ret;
2067 
2068 	/* USB data support is optional */
2069 	ret = fwnode_property_read_string(fwnode, "data-role", &cap_str);
2070 	if (ret == 0) {
2071 		ret = typec_find_port_data_role(cap_str);
2072 		if (ret < 0)
2073 			return ret;
2074 		cap->data = ret;
2075 	}
2076 
2077 	/* Get the preferred power role for a DRP */
2078 	if (cap->type == TYPEC_PORT_DRP) {
2079 		cap->prefer_role = TYPEC_NO_PREFERRED_ROLE;
2080 
2081 		ret = fwnode_property_read_string(fwnode, "try-power-role", &cap_str);
2082 		if (ret == 0) {
2083 			ret = typec_find_power_role(cap_str);
2084 			if (ret < 0)
2085 				return ret;
2086 			cap->prefer_role = ret;
2087 		}
2088 	}
2089 
2090 	return 0;
2091 }
2092 EXPORT_SYMBOL_GPL(typec_get_fw_cap);
2093 
2094 /**
2095  * typec_port_register_altmode - Register USB Type-C Port Alternate Mode
2096  * @port: USB Type-C Port that supports the alternate mode
2097  * @desc: Description of the alternate mode
2098  *
2099  * This routine is used to register an alternate mode that @port is capable of
2100  * supporting.
2101  *
2102  * Returns handle to the alternate mode on success or ERR_PTR on failure.
2103  */
2104 struct typec_altmode *
2105 typec_port_register_altmode(struct typec_port *port,
2106 			    const struct typec_altmode_desc *desc)
2107 {
2108 	struct typec_altmode *adev;
2109 	struct typec_mux *mux;
2110 	struct typec_retimer *retimer;
2111 
2112 	mux = typec_mux_get(&port->dev);
2113 	if (IS_ERR(mux))
2114 		return ERR_CAST(mux);
2115 
2116 	retimer = typec_retimer_get(&port->dev);
2117 	if (IS_ERR(retimer)) {
2118 		typec_mux_put(mux);
2119 		return ERR_CAST(retimer);
2120 	}
2121 
2122 	adev = typec_register_altmode(&port->dev, desc);
2123 	if (IS_ERR(adev)) {
2124 		typec_retimer_put(retimer);
2125 		typec_mux_put(mux);
2126 	} else {
2127 		to_altmode(adev)->mux = mux;
2128 		to_altmode(adev)->retimer = retimer;
2129 	}
2130 
2131 	return adev;
2132 }
2133 EXPORT_SYMBOL_GPL(typec_port_register_altmode);
2134 
2135 void typec_port_register_altmodes(struct typec_port *port,
2136 	const struct typec_altmode_ops *ops, void *drvdata,
2137 	struct typec_altmode **altmodes, size_t n)
2138 {
2139 	struct fwnode_handle *altmodes_node, *child;
2140 	struct typec_altmode_desc desc;
2141 	struct typec_altmode *alt;
2142 	size_t index = 0;
2143 	u32 svid, vdo;
2144 	int ret;
2145 
2146 	altmodes_node = device_get_named_child_node(&port->dev, "altmodes");
2147 	if (!altmodes_node)
2148 		return; /* No altmodes specified */
2149 
2150 	fwnode_for_each_child_node(altmodes_node, child) {
2151 		ret = fwnode_property_read_u32(child, "svid", &svid);
2152 		if (ret) {
2153 			dev_err(&port->dev, "Error reading svid for altmode %s\n",
2154 				fwnode_get_name(child));
2155 			continue;
2156 		}
2157 
2158 		ret = fwnode_property_read_u32(child, "vdo", &vdo);
2159 		if (ret) {
2160 			dev_err(&port->dev, "Error reading vdo for altmode %s\n",
2161 				fwnode_get_name(child));
2162 			continue;
2163 		}
2164 
2165 		if (index >= n) {
2166 			dev_err(&port->dev, "Error not enough space for altmode %s\n",
2167 				fwnode_get_name(child));
2168 			continue;
2169 		}
2170 
2171 		desc.svid = svid;
2172 		desc.vdo = vdo;
2173 		desc.mode = index + 1;
2174 		alt = typec_port_register_altmode(port, &desc);
2175 		if (IS_ERR(alt)) {
2176 			dev_err(&port->dev, "Error registering altmode %s\n",
2177 				fwnode_get_name(child));
2178 			continue;
2179 		}
2180 
2181 		alt->ops = ops;
2182 		typec_altmode_set_drvdata(alt, drvdata);
2183 		altmodes[index] = alt;
2184 		index++;
2185 	}
2186 }
2187 EXPORT_SYMBOL_GPL(typec_port_register_altmodes);
2188 
2189 /**
2190  * typec_register_port - Register a USB Type-C Port
2191  * @parent: Parent device
2192  * @cap: Description of the port
2193  *
2194  * Registers a device for USB Type-C Port described in @cap.
2195  *
2196  * Returns handle to the port on success or ERR_PTR on failure.
2197  */
2198 struct typec_port *typec_register_port(struct device *parent,
2199 				       const struct typec_capability *cap)
2200 {
2201 	struct typec_port *port;
2202 	int ret;
2203 	int id;
2204 
2205 	port = kzalloc(sizeof(*port), GFP_KERNEL);
2206 	if (!port)
2207 		return ERR_PTR(-ENOMEM);
2208 
2209 	id = ida_simple_get(&typec_index_ida, 0, 0, GFP_KERNEL);
2210 	if (id < 0) {
2211 		kfree(port);
2212 		return ERR_PTR(id);
2213 	}
2214 
2215 	switch (cap->type) {
2216 	case TYPEC_PORT_SRC:
2217 		port->pwr_role = TYPEC_SOURCE;
2218 		port->vconn_role = TYPEC_SOURCE;
2219 		break;
2220 	case TYPEC_PORT_SNK:
2221 		port->pwr_role = TYPEC_SINK;
2222 		port->vconn_role = TYPEC_SINK;
2223 		break;
2224 	case TYPEC_PORT_DRP:
2225 		if (cap->prefer_role != TYPEC_NO_PREFERRED_ROLE)
2226 			port->pwr_role = cap->prefer_role;
2227 		else
2228 			port->pwr_role = TYPEC_SINK;
2229 		break;
2230 	}
2231 
2232 	switch (cap->data) {
2233 	case TYPEC_PORT_DFP:
2234 		port->data_role = TYPEC_HOST;
2235 		break;
2236 	case TYPEC_PORT_UFP:
2237 		port->data_role = TYPEC_DEVICE;
2238 		break;
2239 	case TYPEC_PORT_DRD:
2240 		if (cap->prefer_role == TYPEC_SOURCE)
2241 			port->data_role = TYPEC_HOST;
2242 		else
2243 			port->data_role = TYPEC_DEVICE;
2244 		break;
2245 	}
2246 
2247 	ida_init(&port->mode_ids);
2248 	mutex_init(&port->port_type_lock);
2249 
2250 	port->id = id;
2251 	port->ops = cap->ops;
2252 	port->port_type = cap->type;
2253 	port->prefer_role = cap->prefer_role;
2254 
2255 	device_initialize(&port->dev);
2256 	port->dev.class = &typec_class;
2257 	port->dev.parent = parent;
2258 	port->dev.fwnode = cap->fwnode;
2259 	port->dev.type = &typec_port_dev_type;
2260 	dev_set_name(&port->dev, "port%d", id);
2261 	dev_set_drvdata(&port->dev, cap->driver_data);
2262 
2263 	port->cap = kmemdup(cap, sizeof(*cap), GFP_KERNEL);
2264 	if (!port->cap) {
2265 		put_device(&port->dev);
2266 		return ERR_PTR(-ENOMEM);
2267 	}
2268 
2269 	port->sw = typec_switch_get(&port->dev);
2270 	if (IS_ERR(port->sw)) {
2271 		ret = PTR_ERR(port->sw);
2272 		put_device(&port->dev);
2273 		return ERR_PTR(ret);
2274 	}
2275 
2276 	port->mux = typec_mux_get(&port->dev);
2277 	if (IS_ERR(port->mux)) {
2278 		ret = PTR_ERR(port->mux);
2279 		put_device(&port->dev);
2280 		return ERR_PTR(ret);
2281 	}
2282 
2283 	port->retimer = typec_retimer_get(&port->dev);
2284 	if (IS_ERR(port->retimer)) {
2285 		ret = PTR_ERR(port->retimer);
2286 		put_device(&port->dev);
2287 		return ERR_PTR(ret);
2288 	}
2289 
2290 	port->pd = cap->pd;
2291 
2292 	ret = device_add(&port->dev);
2293 	if (ret) {
2294 		dev_err(parent, "failed to register port (%d)\n", ret);
2295 		put_device(&port->dev);
2296 		return ERR_PTR(ret);
2297 	}
2298 
2299 	ret = usb_power_delivery_link_device(port->pd, &port->dev);
2300 	if (ret) {
2301 		dev_err(&port->dev, "failed to link pd\n");
2302 		device_unregister(&port->dev);
2303 		return ERR_PTR(ret);
2304 	}
2305 
2306 	ret = typec_link_ports(port);
2307 	if (ret)
2308 		dev_warn(&port->dev, "failed to create symlinks (%d)\n", ret);
2309 
2310 	return port;
2311 }
2312 EXPORT_SYMBOL_GPL(typec_register_port);
2313 
2314 /**
2315  * typec_unregister_port - Unregister a USB Type-C Port
2316  * @port: The port to be unregistered
2317  *
2318  * Unregister device created with typec_register_port().
2319  */
2320 void typec_unregister_port(struct typec_port *port)
2321 {
2322 	if (!IS_ERR_OR_NULL(port)) {
2323 		typec_unlink_ports(port);
2324 		typec_port_set_usb_power_delivery(port, NULL);
2325 		device_unregister(&port->dev);
2326 	}
2327 }
2328 EXPORT_SYMBOL_GPL(typec_unregister_port);
2329 
2330 static int __init typec_init(void)
2331 {
2332 	int ret;
2333 
2334 	ret = bus_register(&typec_bus);
2335 	if (ret)
2336 		return ret;
2337 
2338 	ret = class_register(&typec_mux_class);
2339 	if (ret)
2340 		goto err_unregister_bus;
2341 
2342 	ret = class_register(&retimer_class);
2343 	if (ret)
2344 		goto err_unregister_mux_class;
2345 
2346 	ret = class_register(&typec_class);
2347 	if (ret)
2348 		goto err_unregister_retimer_class;
2349 
2350 	ret = usb_power_delivery_init();
2351 	if (ret)
2352 		goto err_unregister_class;
2353 
2354 	return 0;
2355 
2356 err_unregister_class:
2357 	class_unregister(&typec_class);
2358 
2359 err_unregister_retimer_class:
2360 	class_unregister(&retimer_class);
2361 
2362 err_unregister_mux_class:
2363 	class_unregister(&typec_mux_class);
2364 
2365 err_unregister_bus:
2366 	bus_unregister(&typec_bus);
2367 
2368 	return ret;
2369 }
2370 subsys_initcall(typec_init);
2371 
2372 static void __exit typec_exit(void)
2373 {
2374 	usb_power_delivery_exit();
2375 	class_unregister(&typec_class);
2376 	ida_destroy(&typec_index_ida);
2377 	bus_unregister(&typec_bus);
2378 	class_unregister(&typec_mux_class);
2379 	class_unregister(&retimer_class);
2380 }
2381 module_exit(typec_exit);
2382 
2383 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
2384 MODULE_LICENSE("GPL v2");
2385 MODULE_DESCRIPTION("USB Type-C Connector Class");
2386