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