Linux Audio

Check our new training course

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