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