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