Linux Audio

Check our new training course

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