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