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