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