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