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