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