Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * USB Type-C Connector Class
0004  *
0005  * Copyright (C) 2017, Intel Corporation
0006  * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
0007  */
0008 
0009 #include <linux/module.h>
0010 #include <linux/mutex.h>
0011 #include <linux/property.h>
0012 #include <linux/slab.h>
0013 #include <linux/usb/pd_vdo.h>
0014 #include <linux/usb/typec_mux.h>
0015 #include <linux/usb/typec_retimer.h>
0016 
0017 #include "bus.h"
0018 #include "class.h"
0019 #include "pd.h"
0020 
0021 static DEFINE_IDA(typec_index_ida);
0022 
0023 struct class typec_class = {
0024     .name = "typec",
0025     .owner = THIS_MODULE,
0026 };
0027 
0028 /* ------------------------------------------------------------------------- */
0029 /* Common attributes */
0030 
0031 static const char * const typec_accessory_modes[] = {
0032     [TYPEC_ACCESSORY_NONE]      = "none",
0033     [TYPEC_ACCESSORY_AUDIO]     = "analog_audio",
0034     [TYPEC_ACCESSORY_DEBUG]     = "debug",
0035 };
0036 
0037 /* Product types defined in USB PD Specification R3.0 V2.0 */
0038 static const char * const product_type_ufp[8] = {
0039     [IDH_PTYPE_NOT_UFP]     = "not_ufp",
0040     [IDH_PTYPE_HUB]         = "hub",
0041     [IDH_PTYPE_PERIPH]      = "peripheral",
0042     [IDH_PTYPE_PSD]         = "psd",
0043     [IDH_PTYPE_AMA]         = "ama",
0044 };
0045 
0046 static const char * const product_type_dfp[8] = {
0047     [IDH_PTYPE_NOT_DFP]     = "not_dfp",
0048     [IDH_PTYPE_DFP_HUB]     = "hub",
0049     [IDH_PTYPE_DFP_HOST]        = "host",
0050     [IDH_PTYPE_DFP_PB]      = "power_brick",
0051 };
0052 
0053 static const char * const product_type_cable[8] = {
0054     [IDH_PTYPE_NOT_CABLE]       = "not_cable",
0055     [IDH_PTYPE_PCABLE]      = "passive",
0056     [IDH_PTYPE_ACABLE]      = "active",
0057     [IDH_PTYPE_VPD]         = "vpd",
0058 };
0059 
0060 static struct usb_pd_identity *get_pd_identity(struct device *dev)
0061 {
0062     if (is_typec_partner(dev)) {
0063         struct typec_partner *partner = to_typec_partner(dev);
0064 
0065         return partner->identity;
0066     } else if (is_typec_cable(dev)) {
0067         struct typec_cable *cable = to_typec_cable(dev);
0068 
0069         return cable->identity;
0070     }
0071     return NULL;
0072 }
0073 
0074 static const char *get_pd_product_type(struct device *dev)
0075 {
0076     struct typec_port *port = to_typec_port(dev->parent);
0077     struct usb_pd_identity *id = get_pd_identity(dev);
0078     const char *ptype = NULL;
0079 
0080     if (is_typec_partner(dev)) {
0081         if (!id)
0082             return NULL;
0083 
0084         if (port->data_role == TYPEC_HOST)
0085             ptype = product_type_ufp[PD_IDH_PTYPE(id->id_header)];
0086         else
0087             ptype = product_type_dfp[PD_IDH_DFP_PTYPE(id->id_header)];
0088     } else if (is_typec_cable(dev)) {
0089         if (id)
0090             ptype = product_type_cable[PD_IDH_PTYPE(id->id_header)];
0091         else
0092             ptype = to_typec_cable(dev)->active ?
0093                 product_type_cable[IDH_PTYPE_ACABLE] :
0094                 product_type_cable[IDH_PTYPE_PCABLE];
0095     }
0096 
0097     return ptype;
0098 }
0099 
0100 static ssize_t id_header_show(struct device *dev, struct device_attribute *attr,
0101                   char *buf)
0102 {
0103     struct usb_pd_identity *id = get_pd_identity(dev);
0104 
0105     return sprintf(buf, "0x%08x\n", id->id_header);
0106 }
0107 static DEVICE_ATTR_RO(id_header);
0108 
0109 static ssize_t cert_stat_show(struct device *dev, struct device_attribute *attr,
0110                   char *buf)
0111 {
0112     struct usb_pd_identity *id = get_pd_identity(dev);
0113 
0114     return sprintf(buf, "0x%08x\n", id->cert_stat);
0115 }
0116 static DEVICE_ATTR_RO(cert_stat);
0117 
0118 static ssize_t product_show(struct device *dev, struct device_attribute *attr,
0119                 char *buf)
0120 {
0121     struct usb_pd_identity *id = get_pd_identity(dev);
0122 
0123     return sprintf(buf, "0x%08x\n", id->product);
0124 }
0125 static DEVICE_ATTR_RO(product);
0126 
0127 static ssize_t product_type_vdo1_show(struct device *dev, struct device_attribute *attr,
0128                       char *buf)
0129 {
0130     struct usb_pd_identity *id = get_pd_identity(dev);
0131 
0132     return sysfs_emit(buf, "0x%08x\n", id->vdo[0]);
0133 }
0134 static DEVICE_ATTR_RO(product_type_vdo1);
0135 
0136 static ssize_t product_type_vdo2_show(struct device *dev, struct device_attribute *attr,
0137                       char *buf)
0138 {
0139     struct usb_pd_identity *id = get_pd_identity(dev);
0140 
0141     return sysfs_emit(buf, "0x%08x\n", id->vdo[1]);
0142 }
0143 static DEVICE_ATTR_RO(product_type_vdo2);
0144 
0145 static ssize_t product_type_vdo3_show(struct device *dev, struct device_attribute *attr,
0146                       char *buf)
0147 {
0148     struct usb_pd_identity *id = get_pd_identity(dev);
0149 
0150     return sysfs_emit(buf, "0x%08x\n", id->vdo[2]);
0151 }
0152 static DEVICE_ATTR_RO(product_type_vdo3);
0153 
0154 static struct attribute *usb_pd_id_attrs[] = {
0155     &dev_attr_id_header.attr,
0156     &dev_attr_cert_stat.attr,
0157     &dev_attr_product.attr,
0158     &dev_attr_product_type_vdo1.attr,
0159     &dev_attr_product_type_vdo2.attr,
0160     &dev_attr_product_type_vdo3.attr,
0161     NULL
0162 };
0163 
0164 static const struct attribute_group usb_pd_id_group = {
0165     .name = "identity",
0166     .attrs = usb_pd_id_attrs,
0167 };
0168 
0169 static const struct attribute_group *usb_pd_id_groups[] = {
0170     &usb_pd_id_group,
0171     NULL,
0172 };
0173 
0174 static void typec_product_type_notify(struct device *dev)
0175 {
0176     char *envp[2] = { };
0177     const char *ptype;
0178 
0179     ptype = get_pd_product_type(dev);
0180     if (!ptype)
0181         return;
0182 
0183     sysfs_notify(&dev->kobj, NULL, "type");
0184 
0185     envp[0] = kasprintf(GFP_KERNEL, "PRODUCT_TYPE=%s", ptype);
0186     if (!envp[0])
0187         return;
0188 
0189     kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
0190     kfree(envp[0]);
0191 }
0192 
0193 static void typec_report_identity(struct device *dev)
0194 {
0195     sysfs_notify(&dev->kobj, "identity", "id_header");
0196     sysfs_notify(&dev->kobj, "identity", "cert_stat");
0197     sysfs_notify(&dev->kobj, "identity", "product");
0198     sysfs_notify(&dev->kobj, "identity", "product_type_vdo1");
0199     sysfs_notify(&dev->kobj, "identity", "product_type_vdo2");
0200     sysfs_notify(&dev->kobj, "identity", "product_type_vdo3");
0201     typec_product_type_notify(dev);
0202 }
0203 
0204 static ssize_t
0205 type_show(struct device *dev, struct device_attribute *attr, char *buf)
0206 {
0207     const char *ptype;
0208 
0209     ptype = get_pd_product_type(dev);
0210     if (!ptype)
0211         return 0;
0212 
0213     return sysfs_emit(buf, "%s\n", ptype);
0214 }
0215 static DEVICE_ATTR_RO(type);
0216 
0217 static ssize_t usb_power_delivery_revision_show(struct device *dev,
0218                         struct device_attribute *attr,
0219                         char *buf);
0220 static DEVICE_ATTR_RO(usb_power_delivery_revision);
0221 
0222 /* ------------------------------------------------------------------------- */
0223 /* Alternate Modes */
0224 
0225 static int altmode_match(struct device *dev, void *data)
0226 {
0227     struct typec_altmode *adev = to_typec_altmode(dev);
0228     struct typec_device_id *id = data;
0229 
0230     if (!is_typec_altmode(dev))
0231         return 0;
0232 
0233     return ((adev->svid == id->svid) && (adev->mode == id->mode));
0234 }
0235 
0236 static void typec_altmode_set_partner(struct altmode *altmode)
0237 {
0238     struct typec_altmode *adev = &altmode->adev;
0239     struct typec_device_id id = { adev->svid, adev->mode, };
0240     struct typec_port *port = typec_altmode2port(adev);
0241     struct altmode *partner;
0242     struct device *dev;
0243 
0244     dev = device_find_child(&port->dev, &id, altmode_match);
0245     if (!dev)
0246         return;
0247 
0248     /* Bind the port alt mode to the partner/plug alt mode. */
0249     partner = to_altmode(to_typec_altmode(dev));
0250     altmode->partner = partner;
0251 
0252     /* Bind the partner/plug alt mode to the port alt mode. */
0253     if (is_typec_plug(adev->dev.parent)) {
0254         struct typec_plug *plug = to_typec_plug(adev->dev.parent);
0255 
0256         partner->plug[plug->index] = altmode;
0257     } else {
0258         partner->partner = altmode;
0259     }
0260 }
0261 
0262 static void typec_altmode_put_partner(struct altmode *altmode)
0263 {
0264     struct altmode *partner = altmode->partner;
0265     struct typec_altmode *adev;
0266 
0267     if (!partner)
0268         return;
0269 
0270     adev = &partner->adev;
0271 
0272     if (is_typec_plug(adev->dev.parent)) {
0273         struct typec_plug *plug = to_typec_plug(adev->dev.parent);
0274 
0275         partner->plug[plug->index] = NULL;
0276     } else {
0277         partner->partner = NULL;
0278     }
0279     put_device(&adev->dev);
0280 }
0281 
0282 /**
0283  * typec_altmode_update_active - Report Enter/Exit mode
0284  * @adev: Handle to the alternate mode
0285  * @active: True when the mode has been entered
0286  *
0287  * If a partner or cable plug executes Enter/Exit Mode command successfully, the
0288  * drivers use this routine to report the updated state of the mode.
0289  */
0290 void typec_altmode_update_active(struct typec_altmode *adev, bool active)
0291 {
0292     char dir[6];
0293 
0294     if (adev->active == active)
0295         return;
0296 
0297     if (!is_typec_port(adev->dev.parent) && adev->dev.driver) {
0298         if (!active)
0299             module_put(adev->dev.driver->owner);
0300         else
0301             WARN_ON(!try_module_get(adev->dev.driver->owner));
0302     }
0303 
0304     adev->active = active;
0305     snprintf(dir, sizeof(dir), "mode%d", adev->mode);
0306     sysfs_notify(&adev->dev.kobj, dir, "active");
0307     sysfs_notify(&adev->dev.kobj, NULL, "active");
0308     kobject_uevent(&adev->dev.kobj, KOBJ_CHANGE);
0309 }
0310 EXPORT_SYMBOL_GPL(typec_altmode_update_active);
0311 
0312 /**
0313  * typec_altmode2port - Alternate Mode to USB Type-C port
0314  * @alt: The Alternate Mode
0315  *
0316  * Returns handle to the port that a cable plug or partner with @alt is
0317  * connected to.
0318  */
0319 struct typec_port *typec_altmode2port(struct typec_altmode *alt)
0320 {
0321     if (is_typec_plug(alt->dev.parent))
0322         return to_typec_port(alt->dev.parent->parent->parent);
0323     if (is_typec_partner(alt->dev.parent))
0324         return to_typec_port(alt->dev.parent->parent);
0325     if (is_typec_port(alt->dev.parent))
0326         return to_typec_port(alt->dev.parent);
0327 
0328     return NULL;
0329 }
0330 EXPORT_SYMBOL_GPL(typec_altmode2port);
0331 
0332 static ssize_t
0333 vdo_show(struct device *dev, struct device_attribute *attr, char *buf)
0334 {
0335     struct typec_altmode *alt = to_typec_altmode(dev);
0336 
0337     return sprintf(buf, "0x%08x\n", alt->vdo);
0338 }
0339 static DEVICE_ATTR_RO(vdo);
0340 
0341 static ssize_t
0342 description_show(struct device *dev, struct device_attribute *attr, char *buf)
0343 {
0344     struct typec_altmode *alt = to_typec_altmode(dev);
0345 
0346     return sprintf(buf, "%s\n", alt->desc ? alt->desc : "");
0347 }
0348 static DEVICE_ATTR_RO(description);
0349 
0350 static ssize_t
0351 active_show(struct device *dev, struct device_attribute *attr, char *buf)
0352 {
0353     struct typec_altmode *alt = to_typec_altmode(dev);
0354 
0355     return sprintf(buf, "%s\n", alt->active ? "yes" : "no");
0356 }
0357 
0358 static ssize_t active_store(struct device *dev, struct device_attribute *attr,
0359                 const char *buf, size_t size)
0360 {
0361     struct typec_altmode *adev = to_typec_altmode(dev);
0362     struct altmode *altmode = to_altmode(adev);
0363     bool enter;
0364     int ret;
0365 
0366     ret = kstrtobool(buf, &enter);
0367     if (ret)
0368         return ret;
0369 
0370     if (adev->active == enter)
0371         return size;
0372 
0373     if (is_typec_port(adev->dev.parent)) {
0374         typec_altmode_update_active(adev, enter);
0375 
0376         /* Make sure that the partner exits the mode before disabling */
0377         if (altmode->partner && !enter && altmode->partner->adev.active)
0378             typec_altmode_exit(&altmode->partner->adev);
0379     } else if (altmode->partner) {
0380         if (enter && !altmode->partner->adev.active) {
0381             dev_warn(dev, "port has the mode disabled\n");
0382             return -EPERM;
0383         }
0384     }
0385 
0386     /* Note: If there is no driver, the mode will not be entered */
0387     if (adev->ops && adev->ops->activate) {
0388         ret = adev->ops->activate(adev, enter);
0389         if (ret)
0390             return ret;
0391     }
0392 
0393     return size;
0394 }
0395 static DEVICE_ATTR_RW(active);
0396 
0397 static ssize_t
0398 supported_roles_show(struct device *dev, struct device_attribute *attr,
0399              char *buf)
0400 {
0401     struct altmode *alt = to_altmode(to_typec_altmode(dev));
0402     ssize_t ret;
0403 
0404     switch (alt->roles) {
0405     case TYPEC_PORT_SRC:
0406         ret = sprintf(buf, "source\n");
0407         break;
0408     case TYPEC_PORT_SNK:
0409         ret = sprintf(buf, "sink\n");
0410         break;
0411     case TYPEC_PORT_DRP:
0412     default:
0413         ret = sprintf(buf, "source sink\n");
0414         break;
0415     }
0416     return ret;
0417 }
0418 static DEVICE_ATTR_RO(supported_roles);
0419 
0420 static ssize_t
0421 mode_show(struct device *dev, struct device_attribute *attr, char *buf)
0422 {
0423     struct typec_altmode *adev = to_typec_altmode(dev);
0424 
0425     return sprintf(buf, "%u\n", adev->mode);
0426 }
0427 static DEVICE_ATTR_RO(mode);
0428 
0429 static ssize_t
0430 svid_show(struct device *dev, struct device_attribute *attr, char *buf)
0431 {
0432     struct typec_altmode *adev = to_typec_altmode(dev);
0433 
0434     return sprintf(buf, "%04x\n", adev->svid);
0435 }
0436 static DEVICE_ATTR_RO(svid);
0437 
0438 static struct attribute *typec_altmode_attrs[] = {
0439     &dev_attr_active.attr,
0440     &dev_attr_mode.attr,
0441     &dev_attr_svid.attr,
0442     &dev_attr_vdo.attr,
0443     NULL
0444 };
0445 
0446 static umode_t typec_altmode_attr_is_visible(struct kobject *kobj,
0447                          struct attribute *attr, int n)
0448 {
0449     struct typec_altmode *adev = to_typec_altmode(kobj_to_dev(kobj));
0450 
0451     if (attr == &dev_attr_active.attr)
0452         if (!adev->ops || !adev->ops->activate)
0453             return 0444;
0454 
0455     return attr->mode;
0456 }
0457 
0458 static const struct attribute_group typec_altmode_group = {
0459     .is_visible = typec_altmode_attr_is_visible,
0460     .attrs = typec_altmode_attrs,
0461 };
0462 
0463 static const struct attribute_group *typec_altmode_groups[] = {
0464     &typec_altmode_group,
0465     NULL
0466 };
0467 
0468 static int altmode_id_get(struct device *dev)
0469 {
0470     struct ida *ids;
0471 
0472     if (is_typec_partner(dev))
0473         ids = &to_typec_partner(dev)->mode_ids;
0474     else if (is_typec_plug(dev))
0475         ids = &to_typec_plug(dev)->mode_ids;
0476     else
0477         ids = &to_typec_port(dev)->mode_ids;
0478 
0479     return ida_simple_get(ids, 0, 0, GFP_KERNEL);
0480 }
0481 
0482 static void altmode_id_remove(struct device *dev, int id)
0483 {
0484     struct ida *ids;
0485 
0486     if (is_typec_partner(dev))
0487         ids = &to_typec_partner(dev)->mode_ids;
0488     else if (is_typec_plug(dev))
0489         ids = &to_typec_plug(dev)->mode_ids;
0490     else
0491         ids = &to_typec_port(dev)->mode_ids;
0492 
0493     ida_simple_remove(ids, id);
0494 }
0495 
0496 static void typec_altmode_release(struct device *dev)
0497 {
0498     struct altmode *alt = to_altmode(to_typec_altmode(dev));
0499 
0500     typec_altmode_put_partner(alt);
0501 
0502     altmode_id_remove(alt->adev.dev.parent, alt->id);
0503     kfree(alt);
0504 }
0505 
0506 const struct device_type typec_altmode_dev_type = {
0507     .name = "typec_alternate_mode",
0508     .groups = typec_altmode_groups,
0509     .release = typec_altmode_release,
0510 };
0511 
0512 static struct typec_altmode *
0513 typec_register_altmode(struct device *parent,
0514                const struct typec_altmode_desc *desc)
0515 {
0516     unsigned int id = altmode_id_get(parent);
0517     bool is_port = is_typec_port(parent);
0518     struct altmode *alt;
0519     int ret;
0520 
0521     alt = kzalloc(sizeof(*alt), GFP_KERNEL);
0522     if (!alt) {
0523         altmode_id_remove(parent, id);
0524         return ERR_PTR(-ENOMEM);
0525     }
0526 
0527     alt->adev.svid = desc->svid;
0528     alt->adev.mode = desc->mode;
0529     alt->adev.vdo = desc->vdo;
0530     alt->roles = desc->roles;
0531     alt->id = id;
0532 
0533     alt->attrs[0] = &dev_attr_vdo.attr;
0534     alt->attrs[1] = &dev_attr_description.attr;
0535     alt->attrs[2] = &dev_attr_active.attr;
0536 
0537     if (is_port) {
0538         alt->attrs[3] = &dev_attr_supported_roles.attr;
0539         alt->adev.active = true; /* Enabled by default */
0540     }
0541 
0542     sprintf(alt->group_name, "mode%d", desc->mode);
0543     alt->group.name = alt->group_name;
0544     alt->group.attrs = alt->attrs;
0545     alt->groups[0] = &alt->group;
0546 
0547     alt->adev.dev.parent = parent;
0548     alt->adev.dev.groups = alt->groups;
0549     alt->adev.dev.type = &typec_altmode_dev_type;
0550     dev_set_name(&alt->adev.dev, "%s.%u", dev_name(parent), id);
0551 
0552     /* Link partners and plugs with the ports */
0553     if (!is_port)
0554         typec_altmode_set_partner(alt);
0555 
0556     /* The partners are bind to drivers */
0557     if (is_typec_partner(parent))
0558         alt->adev.dev.bus = &typec_bus;
0559 
0560     /* Plug alt modes need a class to generate udev events. */
0561     if (is_typec_plug(parent))
0562         alt->adev.dev.class = &typec_class;
0563 
0564     ret = device_register(&alt->adev.dev);
0565     if (ret) {
0566         dev_err(parent, "failed to register alternate mode (%d)\n",
0567             ret);
0568         put_device(&alt->adev.dev);
0569         return ERR_PTR(ret);
0570     }
0571 
0572     return &alt->adev;
0573 }
0574 
0575 /**
0576  * typec_unregister_altmode - Unregister Alternate Mode
0577  * @adev: The alternate mode to be unregistered
0578  *
0579  * Unregister device created with typec_partner_register_altmode(),
0580  * typec_plug_register_altmode() or typec_port_register_altmode().
0581  */
0582 void typec_unregister_altmode(struct typec_altmode *adev)
0583 {
0584     if (IS_ERR_OR_NULL(adev))
0585         return;
0586     typec_mux_put(to_altmode(adev)->mux);
0587     device_unregister(&adev->dev);
0588 }
0589 EXPORT_SYMBOL_GPL(typec_unregister_altmode);
0590 
0591 /* ------------------------------------------------------------------------- */
0592 /* Type-C Partners */
0593 
0594 static ssize_t accessory_mode_show(struct device *dev,
0595                    struct device_attribute *attr,
0596                    char *buf)
0597 {
0598     struct typec_partner *p = to_typec_partner(dev);
0599 
0600     return sprintf(buf, "%s\n", typec_accessory_modes[p->accessory]);
0601 }
0602 static DEVICE_ATTR_RO(accessory_mode);
0603 
0604 static ssize_t supports_usb_power_delivery_show(struct device *dev,
0605                         struct device_attribute *attr,
0606                         char *buf)
0607 {
0608     struct typec_partner *p = to_typec_partner(dev);
0609 
0610     return sprintf(buf, "%s\n", p->usb_pd ? "yes" : "no");
0611 }
0612 static DEVICE_ATTR_RO(supports_usb_power_delivery);
0613 
0614 static ssize_t number_of_alternate_modes_show(struct device *dev, struct device_attribute *attr,
0615                           char *buf)
0616 {
0617     struct typec_partner *partner;
0618     struct typec_plug *plug;
0619     int num_altmodes;
0620 
0621     if (is_typec_partner(dev)) {
0622         partner = to_typec_partner(dev);
0623         num_altmodes = partner->num_altmodes;
0624     } else if (is_typec_plug(dev)) {
0625         plug = to_typec_plug(dev);
0626         num_altmodes = plug->num_altmodes;
0627     } else {
0628         return 0;
0629     }
0630 
0631     return sysfs_emit(buf, "%d\n", num_altmodes);
0632 }
0633 static DEVICE_ATTR_RO(number_of_alternate_modes);
0634 
0635 static struct attribute *typec_partner_attrs[] = {
0636     &dev_attr_accessory_mode.attr,
0637     &dev_attr_supports_usb_power_delivery.attr,
0638     &dev_attr_number_of_alternate_modes.attr,
0639     &dev_attr_type.attr,
0640     &dev_attr_usb_power_delivery_revision.attr,
0641     NULL
0642 };
0643 
0644 static umode_t typec_partner_attr_is_visible(struct kobject *kobj, struct attribute *attr, int n)
0645 {
0646     struct typec_partner *partner = to_typec_partner(kobj_to_dev(kobj));
0647 
0648     if (attr == &dev_attr_number_of_alternate_modes.attr) {
0649         if (partner->num_altmodes < 0)
0650             return 0;
0651     }
0652 
0653     if (attr == &dev_attr_type.attr)
0654         if (!get_pd_product_type(kobj_to_dev(kobj)))
0655             return 0;
0656 
0657     return attr->mode;
0658 }
0659 
0660 static const struct attribute_group typec_partner_group = {
0661     .is_visible = typec_partner_attr_is_visible,
0662     .attrs = typec_partner_attrs
0663 };
0664 
0665 static const struct attribute_group *typec_partner_groups[] = {
0666     &typec_partner_group,
0667     NULL
0668 };
0669 
0670 static void typec_partner_release(struct device *dev)
0671 {
0672     struct typec_partner *partner = to_typec_partner(dev);
0673 
0674     ida_destroy(&partner->mode_ids);
0675     kfree(partner);
0676 }
0677 
0678 const struct device_type typec_partner_dev_type = {
0679     .name = "typec_partner",
0680     .groups = typec_partner_groups,
0681     .release = typec_partner_release,
0682 };
0683 
0684 /**
0685  * typec_partner_set_identity - Report result from Discover Identity command
0686  * @partner: The partner updated identity values
0687  *
0688  * This routine is used to report that the result of Discover Identity USB power
0689  * delivery command has become available.
0690  */
0691 int typec_partner_set_identity(struct typec_partner *partner)
0692 {
0693     if (!partner->identity)
0694         return -EINVAL;
0695 
0696     typec_report_identity(&partner->dev);
0697     return 0;
0698 }
0699 EXPORT_SYMBOL_GPL(typec_partner_set_identity);
0700 
0701 /**
0702  * typec_partner_set_pd_revision - Set the PD revision supported by the partner
0703  * @partner: The partner to be updated.
0704  * @pd_revision:  USB Power Delivery Specification Revision supported by partner
0705  *
0706  * This routine is used to report that the PD revision of the port partner has
0707  * become available.
0708  */
0709 void typec_partner_set_pd_revision(struct typec_partner *partner, u16 pd_revision)
0710 {
0711     if (partner->pd_revision == pd_revision)
0712         return;
0713 
0714     partner->pd_revision = pd_revision;
0715     sysfs_notify(&partner->dev.kobj, NULL, "usb_power_delivery_revision");
0716     if (pd_revision != 0 && !partner->usb_pd) {
0717         partner->usb_pd = 1;
0718         sysfs_notify(&partner->dev.kobj, NULL,
0719                  "supports_usb_power_delivery");
0720     }
0721     kobject_uevent(&partner->dev.kobj, KOBJ_CHANGE);
0722 }
0723 EXPORT_SYMBOL_GPL(typec_partner_set_pd_revision);
0724 
0725 /**
0726  * typec_partner_set_usb_power_delivery - Declare USB Power Delivery Contract.
0727  * @partner: The partner device.
0728  * @pd: The USB PD instance.
0729  *
0730  * This routine can be used to declare USB Power Delivery Contract with @partner
0731  * by linking @partner to @pd which contains the objects that were used during the
0732  * negotiation of the contract.
0733  *
0734  * If @pd is NULL, the link is removed and the contract with @partner has ended.
0735  */
0736 int typec_partner_set_usb_power_delivery(struct typec_partner *partner,
0737                      struct usb_power_delivery *pd)
0738 {
0739     int ret;
0740 
0741     if (IS_ERR_OR_NULL(partner) || partner->pd == pd)
0742         return 0;
0743 
0744     if (pd) {
0745         ret = usb_power_delivery_link_device(pd, &partner->dev);
0746         if (ret)
0747             return ret;
0748     } else {
0749         usb_power_delivery_unlink_device(partner->pd, &partner->dev);
0750     }
0751 
0752     partner->pd = pd;
0753 
0754     return 0;
0755 }
0756 EXPORT_SYMBOL_GPL(typec_partner_set_usb_power_delivery);
0757 
0758 /**
0759  * typec_partner_set_num_altmodes - Set the number of available partner altmodes
0760  * @partner: The partner to be updated.
0761  * @num_altmodes: The number of altmodes we want to specify as available.
0762  *
0763  * This routine is used to report the number of alternate modes supported by the
0764  * partner. This value is *not* enforced in alternate mode registration routines.
0765  *
0766  * @partner.num_altmodes is set to -1 on partner registration, denoting that
0767  * a valid value has not been set for it yet.
0768  *
0769  * Returns 0 on success or negative error number on failure.
0770  */
0771 int typec_partner_set_num_altmodes(struct typec_partner *partner, int num_altmodes)
0772 {
0773     int ret;
0774 
0775     if (num_altmodes < 0)
0776         return -EINVAL;
0777 
0778     partner->num_altmodes = num_altmodes;
0779     ret = sysfs_update_group(&partner->dev.kobj, &typec_partner_group);
0780     if (ret < 0)
0781         return ret;
0782 
0783     sysfs_notify(&partner->dev.kobj, NULL, "number_of_alternate_modes");
0784     kobject_uevent(&partner->dev.kobj, KOBJ_CHANGE);
0785 
0786     return 0;
0787 }
0788 EXPORT_SYMBOL_GPL(typec_partner_set_num_altmodes);
0789 
0790 /**
0791  * typec_partner_register_altmode - Register USB Type-C Partner Alternate Mode
0792  * @partner: USB Type-C Partner that supports the alternate mode
0793  * @desc: Description of the alternate mode
0794  *
0795  * This routine is used to register each alternate mode individually that
0796  * @partner has listed in response to Discover SVIDs command. The modes for a
0797  * SVID listed in response to Discover Modes command need to be listed in an
0798  * array in @desc.
0799  *
0800  * Returns handle to the alternate mode on success or ERR_PTR on failure.
0801  */
0802 struct typec_altmode *
0803 typec_partner_register_altmode(struct typec_partner *partner,
0804                    const struct typec_altmode_desc *desc)
0805 {
0806     return typec_register_altmode(&partner->dev, desc);
0807 }
0808 EXPORT_SYMBOL_GPL(typec_partner_register_altmode);
0809 
0810 /**
0811  * typec_partner_set_svdm_version - Set negotiated Structured VDM (SVDM) Version
0812  * @partner: USB Type-C Partner that supports SVDM
0813  * @svdm_version: Negotiated SVDM Version
0814  *
0815  * This routine is used to save the negotiated SVDM Version.
0816  */
0817 void typec_partner_set_svdm_version(struct typec_partner *partner,
0818                    enum usb_pd_svdm_ver svdm_version)
0819 {
0820     partner->svdm_version = svdm_version;
0821 }
0822 EXPORT_SYMBOL_GPL(typec_partner_set_svdm_version);
0823 
0824 /**
0825  * typec_register_partner - Register a USB Type-C Partner
0826  * @port: The USB Type-C Port the partner is connected to
0827  * @desc: Description of the partner
0828  *
0829  * Registers a device for USB Type-C Partner described in @desc.
0830  *
0831  * Returns handle to the partner on success or ERR_PTR on failure.
0832  */
0833 struct typec_partner *typec_register_partner(struct typec_port *port,
0834                          struct typec_partner_desc *desc)
0835 {
0836     struct typec_partner *partner;
0837     int ret;
0838 
0839     partner = kzalloc(sizeof(*partner), GFP_KERNEL);
0840     if (!partner)
0841         return ERR_PTR(-ENOMEM);
0842 
0843     ida_init(&partner->mode_ids);
0844     partner->usb_pd = desc->usb_pd;
0845     partner->accessory = desc->accessory;
0846     partner->num_altmodes = -1;
0847     partner->pd_revision = desc->pd_revision;
0848     partner->svdm_version = port->cap->svdm_version;
0849 
0850     if (desc->identity) {
0851         /*
0852          * Creating directory for the identity only if the driver is
0853          * able to provide data to it.
0854          */
0855         partner->dev.groups = usb_pd_id_groups;
0856         partner->identity = desc->identity;
0857     }
0858 
0859     partner->dev.class = &typec_class;
0860     partner->dev.parent = &port->dev;
0861     partner->dev.type = &typec_partner_dev_type;
0862     dev_set_name(&partner->dev, "%s-partner", dev_name(&port->dev));
0863 
0864     ret = device_register(&partner->dev);
0865     if (ret) {
0866         dev_err(&port->dev, "failed to register partner (%d)\n", ret);
0867         put_device(&partner->dev);
0868         return ERR_PTR(ret);
0869     }
0870 
0871     return partner;
0872 }
0873 EXPORT_SYMBOL_GPL(typec_register_partner);
0874 
0875 /**
0876  * typec_unregister_partner - Unregister a USB Type-C Partner
0877  * @partner: The partner to be unregistered
0878  *
0879  * Unregister device created with typec_register_partner().
0880  */
0881 void typec_unregister_partner(struct typec_partner *partner)
0882 {
0883     if (!IS_ERR_OR_NULL(partner))
0884         device_unregister(&partner->dev);
0885 }
0886 EXPORT_SYMBOL_GPL(typec_unregister_partner);
0887 
0888 /* ------------------------------------------------------------------------- */
0889 /* Type-C Cable Plugs */
0890 
0891 static void typec_plug_release(struct device *dev)
0892 {
0893     struct typec_plug *plug = to_typec_plug(dev);
0894 
0895     ida_destroy(&plug->mode_ids);
0896     kfree(plug);
0897 }
0898 
0899 static struct attribute *typec_plug_attrs[] = {
0900     &dev_attr_number_of_alternate_modes.attr,
0901     NULL
0902 };
0903 
0904 static umode_t typec_plug_attr_is_visible(struct kobject *kobj, struct attribute *attr, int n)
0905 {
0906     struct typec_plug *plug = to_typec_plug(kobj_to_dev(kobj));
0907 
0908     if (attr == &dev_attr_number_of_alternate_modes.attr) {
0909         if (plug->num_altmodes < 0)
0910             return 0;
0911     }
0912 
0913     return attr->mode;
0914 }
0915 
0916 static const struct attribute_group typec_plug_group = {
0917     .is_visible = typec_plug_attr_is_visible,
0918     .attrs = typec_plug_attrs
0919 };
0920 
0921 static const struct attribute_group *typec_plug_groups[] = {
0922     &typec_plug_group,
0923     NULL
0924 };
0925 
0926 const struct device_type typec_plug_dev_type = {
0927     .name = "typec_plug",
0928     .groups = typec_plug_groups,
0929     .release = typec_plug_release,
0930 };
0931 
0932 /**
0933  * typec_plug_set_num_altmodes - Set the number of available plug altmodes
0934  * @plug: The plug to be updated.
0935  * @num_altmodes: The number of altmodes we want to specify as available.
0936  *
0937  * This routine is used to report the number of alternate modes supported by the
0938  * plug. This value is *not* enforced in alternate mode registration routines.
0939  *
0940  * @plug.num_altmodes is set to -1 on plug registration, denoting that
0941  * a valid value has not been set for it yet.
0942  *
0943  * Returns 0 on success or negative error number on failure.
0944  */
0945 int typec_plug_set_num_altmodes(struct typec_plug *plug, int num_altmodes)
0946 {
0947     int ret;
0948 
0949     if (num_altmodes < 0)
0950         return -EINVAL;
0951 
0952     plug->num_altmodes = num_altmodes;
0953     ret = sysfs_update_group(&plug->dev.kobj, &typec_plug_group);
0954     if (ret < 0)
0955         return ret;
0956 
0957     sysfs_notify(&plug->dev.kobj, NULL, "number_of_alternate_modes");
0958     kobject_uevent(&plug->dev.kobj, KOBJ_CHANGE);
0959 
0960     return 0;
0961 }
0962 EXPORT_SYMBOL_GPL(typec_plug_set_num_altmodes);
0963 
0964 /**
0965  * typec_plug_register_altmode - Register USB Type-C Cable Plug Alternate Mode
0966  * @plug: USB Type-C Cable Plug that supports the alternate mode
0967  * @desc: Description of the alternate mode
0968  *
0969  * This routine is used to register each alternate mode individually that @plug
0970  * has listed in response to Discover SVIDs command. The modes for a SVID that
0971  * the plug lists in response to Discover Modes command need to be listed in an
0972  * array in @desc.
0973  *
0974  * Returns handle to the alternate mode on success or ERR_PTR on failure.
0975  */
0976 struct typec_altmode *
0977 typec_plug_register_altmode(struct typec_plug *plug,
0978                 const struct typec_altmode_desc *desc)
0979 {
0980     return typec_register_altmode(&plug->dev, desc);
0981 }
0982 EXPORT_SYMBOL_GPL(typec_plug_register_altmode);
0983 
0984 /**
0985  * typec_register_plug - Register a USB Type-C Cable Plug
0986  * @cable: USB Type-C Cable with the plug
0987  * @desc: Description of the cable plug
0988  *
0989  * Registers a device for USB Type-C Cable Plug described in @desc. A USB Type-C
0990  * Cable Plug represents a plug with electronics in it that can response to USB
0991  * Power Delivery SOP Prime or SOP Double Prime packages.
0992  *
0993  * Returns handle to the cable plug on success or ERR_PTR on failure.
0994  */
0995 struct typec_plug *typec_register_plug(struct typec_cable *cable,
0996                        struct typec_plug_desc *desc)
0997 {
0998     struct typec_plug *plug;
0999     char name[8];
1000     int ret;
1001 
1002     plug = kzalloc(sizeof(*plug), GFP_KERNEL);
1003     if (!plug)
1004         return ERR_PTR(-ENOMEM);
1005 
1006     sprintf(name, "plug%d", desc->index);
1007 
1008     ida_init(&plug->mode_ids);
1009     plug->num_altmodes = -1;
1010     plug->index = desc->index;
1011     plug->dev.class = &typec_class;
1012     plug->dev.parent = &cable->dev;
1013     plug->dev.type = &typec_plug_dev_type;
1014     dev_set_name(&plug->dev, "%s-%s", dev_name(cable->dev.parent), name);
1015 
1016     ret = device_register(&plug->dev);
1017     if (ret) {
1018         dev_err(&cable->dev, "failed to register plug (%d)\n", ret);
1019         put_device(&plug->dev);
1020         return ERR_PTR(ret);
1021     }
1022 
1023     return plug;
1024 }
1025 EXPORT_SYMBOL_GPL(typec_register_plug);
1026 
1027 /**
1028  * typec_unregister_plug - Unregister a USB Type-C Cable Plug
1029  * @plug: The cable plug to be unregistered
1030  *
1031  * Unregister device created with typec_register_plug().
1032  */
1033 void typec_unregister_plug(struct typec_plug *plug)
1034 {
1035     if (!IS_ERR_OR_NULL(plug))
1036         device_unregister(&plug->dev);
1037 }
1038 EXPORT_SYMBOL_GPL(typec_unregister_plug);
1039 
1040 /* Type-C Cables */
1041 
1042 static const char * const typec_plug_types[] = {
1043     [USB_PLUG_NONE]     = "unknown",
1044     [USB_PLUG_TYPE_A]   = "type-a",
1045     [USB_PLUG_TYPE_B]   = "type-b",
1046     [USB_PLUG_TYPE_C]   = "type-c",
1047     [USB_PLUG_CAPTIVE]  = "captive",
1048 };
1049 
1050 static ssize_t plug_type_show(struct device *dev,
1051                   struct device_attribute *attr, char *buf)
1052 {
1053     struct typec_cable *cable = to_typec_cable(dev);
1054 
1055     return sprintf(buf, "%s\n", typec_plug_types[cable->type]);
1056 }
1057 static DEVICE_ATTR_RO(plug_type);
1058 
1059 static struct attribute *typec_cable_attrs[] = {
1060     &dev_attr_type.attr,
1061     &dev_attr_plug_type.attr,
1062     &dev_attr_usb_power_delivery_revision.attr,
1063     NULL
1064 };
1065 ATTRIBUTE_GROUPS(typec_cable);
1066 
1067 static void typec_cable_release(struct device *dev)
1068 {
1069     struct typec_cable *cable = to_typec_cable(dev);
1070 
1071     kfree(cable);
1072 }
1073 
1074 const struct device_type typec_cable_dev_type = {
1075     .name = "typec_cable",
1076     .groups = typec_cable_groups,
1077     .release = typec_cable_release,
1078 };
1079 
1080 static int cable_match(struct device *dev, void *data)
1081 {
1082     return is_typec_cable(dev);
1083 }
1084 
1085 /**
1086  * typec_cable_get - Get a reference to the USB Type-C cable
1087  * @port: The USB Type-C Port the cable is connected to
1088  *
1089  * The caller must decrement the reference count with typec_cable_put() after
1090  * use.
1091  */
1092 struct typec_cable *typec_cable_get(struct typec_port *port)
1093 {
1094     struct device *dev;
1095 
1096     dev = device_find_child(&port->dev, NULL, cable_match);
1097     if (!dev)
1098         return NULL;
1099 
1100     return to_typec_cable(dev);
1101 }
1102 EXPORT_SYMBOL_GPL(typec_cable_get);
1103 
1104 /**
1105  * typec_cable_put - Decrement the reference count on USB Type-C cable
1106  * @cable: The USB Type-C cable
1107  */
1108 void typec_cable_put(struct typec_cable *cable)
1109 {
1110     put_device(&cable->dev);
1111 }
1112 EXPORT_SYMBOL_GPL(typec_cable_put);
1113 
1114 /**
1115  * typec_cable_is_active - Check is the USB Type-C cable active or passive
1116  * @cable: The USB Type-C Cable
1117  *
1118  * Return 1 if the cable is active or 0 if it's passive.
1119  */
1120 int typec_cable_is_active(struct typec_cable *cable)
1121 {
1122     return cable->active;
1123 }
1124 EXPORT_SYMBOL_GPL(typec_cable_is_active);
1125 
1126 /**
1127  * typec_cable_set_identity - Report result from Discover Identity command
1128  * @cable: The cable updated identity values
1129  *
1130  * This routine is used to report that the result of Discover Identity USB power
1131  * delivery command has become available.
1132  */
1133 int typec_cable_set_identity(struct typec_cable *cable)
1134 {
1135     if (!cable->identity)
1136         return -EINVAL;
1137 
1138     typec_report_identity(&cable->dev);
1139     return 0;
1140 }
1141 EXPORT_SYMBOL_GPL(typec_cable_set_identity);
1142 
1143 /**
1144  * typec_register_cable - Register a USB Type-C Cable
1145  * @port: The USB Type-C Port the cable is connected to
1146  * @desc: Description of the cable
1147  *
1148  * Registers a device for USB Type-C Cable described in @desc. The cable will be
1149  * parent for the optional cable plug devises.
1150  *
1151  * Returns handle to the cable on success or ERR_PTR on failure.
1152  */
1153 struct typec_cable *typec_register_cable(struct typec_port *port,
1154                      struct typec_cable_desc *desc)
1155 {
1156     struct typec_cable *cable;
1157     int ret;
1158 
1159     cable = kzalloc(sizeof(*cable), GFP_KERNEL);
1160     if (!cable)
1161         return ERR_PTR(-ENOMEM);
1162 
1163     cable->type = desc->type;
1164     cable->active = desc->active;
1165     cable->pd_revision = desc->pd_revision;
1166 
1167     if (desc->identity) {
1168         /*
1169          * Creating directory for the identity only if the driver is
1170          * able to provide data to it.
1171          */
1172         cable->dev.groups = usb_pd_id_groups;
1173         cable->identity = desc->identity;
1174     }
1175 
1176     cable->dev.class = &typec_class;
1177     cable->dev.parent = &port->dev;
1178     cable->dev.type = &typec_cable_dev_type;
1179     dev_set_name(&cable->dev, "%s-cable", dev_name(&port->dev));
1180 
1181     ret = device_register(&cable->dev);
1182     if (ret) {
1183         dev_err(&port->dev, "failed to register cable (%d)\n", ret);
1184         put_device(&cable->dev);
1185         return ERR_PTR(ret);
1186     }
1187 
1188     return cable;
1189 }
1190 EXPORT_SYMBOL_GPL(typec_register_cable);
1191 
1192 /**
1193  * typec_unregister_cable - Unregister a USB Type-C Cable
1194  * @cable: The cable to be unregistered
1195  *
1196  * Unregister device created with typec_register_cable().
1197  */
1198 void typec_unregister_cable(struct typec_cable *cable)
1199 {
1200     if (!IS_ERR_OR_NULL(cable))
1201         device_unregister(&cable->dev);
1202 }
1203 EXPORT_SYMBOL_GPL(typec_unregister_cable);
1204 
1205 /* ------------------------------------------------------------------------- */
1206 /* USB Type-C ports */
1207 
1208 /**
1209  * typec_port_set_usb_power_delivery - Assign USB PD for port.
1210  * @port: USB Type-C port.
1211  * @pd: USB PD instance.
1212  *
1213  * This routine can be used to set the USB Power Delivery Capabilities for @port
1214  * that it will advertise to the partner.
1215  *
1216  * If @pd is NULL, the assignment is removed.
1217  */
1218 int typec_port_set_usb_power_delivery(struct typec_port *port, struct usb_power_delivery *pd)
1219 {
1220     int ret;
1221 
1222     if (IS_ERR_OR_NULL(port) || port->pd == pd)
1223         return 0;
1224 
1225     if (pd) {
1226         ret = usb_power_delivery_link_device(pd, &port->dev);
1227         if (ret)
1228             return ret;
1229     } else {
1230         usb_power_delivery_unlink_device(port->pd, &port->dev);
1231     }
1232 
1233     port->pd = pd;
1234 
1235     return 0;
1236 }
1237 EXPORT_SYMBOL_GPL(typec_port_set_usb_power_delivery);
1238 
1239 static ssize_t select_usb_power_delivery_store(struct device *dev,
1240                            struct device_attribute *attr,
1241                            const char *buf, size_t size)
1242 {
1243     struct typec_port *port = to_typec_port(dev);
1244     struct usb_power_delivery *pd;
1245 
1246     if (!port->ops || !port->ops->pd_set)
1247         return -EOPNOTSUPP;
1248 
1249     pd = usb_power_delivery_find(buf);
1250     if (!pd)
1251         return -EINVAL;
1252 
1253     return port->ops->pd_set(port, pd);
1254 }
1255 
1256 static ssize_t select_usb_power_delivery_show(struct device *dev,
1257                           struct device_attribute *attr, char *buf)
1258 {
1259     struct typec_port *port = to_typec_port(dev);
1260     struct usb_power_delivery **pds;
1261     struct usb_power_delivery *pd;
1262     int ret = 0;
1263 
1264     if (!port->ops || !port->ops->pd_get)
1265         return -EOPNOTSUPP;
1266 
1267     pds = port->ops->pd_get(port);
1268     if (!pds)
1269         return 0;
1270 
1271     for (pd = pds[0]; pd; pd++) {
1272         if (pd == port->pd)
1273             ret += sysfs_emit(buf + ret, "[%s] ", dev_name(&pd->dev));
1274         else
1275             ret += sysfs_emit(buf + ret, "%s ", dev_name(&pd->dev));
1276     }
1277 
1278     buf[ret - 1] = '\n';
1279 
1280     return ret;
1281 }
1282 static DEVICE_ATTR_RW(select_usb_power_delivery);
1283 
1284 static struct attribute *port_attrs[] = {
1285     &dev_attr_select_usb_power_delivery.attr,
1286     NULL
1287 };
1288 
1289 static umode_t port_attr_is_visible(struct kobject *kobj, struct attribute *attr, int n)
1290 {
1291     struct typec_port *port = to_typec_port(kobj_to_dev(kobj));
1292 
1293     if (!port->pd || !port->ops || !port->ops->pd_get)
1294         return 0;
1295     if (!port->ops->pd_set)
1296         return 0444;
1297 
1298     return attr->mode;
1299 }
1300 
1301 static const struct attribute_group pd_group = {
1302     .is_visible = port_attr_is_visible,
1303     .attrs = port_attrs,
1304 };
1305 
1306 static const char * const typec_orientations[] = {
1307     [TYPEC_ORIENTATION_NONE]    = "unknown",
1308     [TYPEC_ORIENTATION_NORMAL]  = "normal",
1309     [TYPEC_ORIENTATION_REVERSE] = "reverse",
1310 };
1311 
1312 static const char * const typec_roles[] = {
1313     [TYPEC_SINK]    = "sink",
1314     [TYPEC_SOURCE]  = "source",
1315 };
1316 
1317 static const char * const typec_data_roles[] = {
1318     [TYPEC_DEVICE]  = "device",
1319     [TYPEC_HOST]    = "host",
1320 };
1321 
1322 static const char * const typec_port_power_roles[] = {
1323     [TYPEC_PORT_SRC] = "source",
1324     [TYPEC_PORT_SNK] = "sink",
1325     [TYPEC_PORT_DRP] = "dual",
1326 };
1327 
1328 static const char * const typec_port_data_roles[] = {
1329     [TYPEC_PORT_DFP] = "host",
1330     [TYPEC_PORT_UFP] = "device",
1331     [TYPEC_PORT_DRD] = "dual",
1332 };
1333 
1334 static const char * const typec_port_types_drp[] = {
1335     [TYPEC_PORT_SRC] = "dual [source] sink",
1336     [TYPEC_PORT_SNK] = "dual source [sink]",
1337     [TYPEC_PORT_DRP] = "[dual] source sink",
1338 };
1339 
1340 static ssize_t
1341 preferred_role_store(struct device *dev, struct device_attribute *attr,
1342              const char *buf, size_t size)
1343 {
1344     struct typec_port *port = to_typec_port(dev);
1345     int role;
1346     int ret;
1347 
1348     if (port->cap->type != TYPEC_PORT_DRP) {
1349         dev_dbg(dev, "Preferred role only supported with DRP ports\n");
1350         return -EOPNOTSUPP;
1351     }
1352 
1353     if (!port->ops || !port->ops->try_role) {
1354         dev_dbg(dev, "Setting preferred role not supported\n");
1355         return -EOPNOTSUPP;
1356     }
1357 
1358     role = sysfs_match_string(typec_roles, buf);
1359     if (role < 0) {
1360         if (sysfs_streq(buf, "none"))
1361             role = TYPEC_NO_PREFERRED_ROLE;
1362         else
1363             return -EINVAL;
1364     }
1365 
1366     ret = port->ops->try_role(port, role);
1367     if (ret)
1368         return ret;
1369 
1370     port->prefer_role = role;
1371     return size;
1372 }
1373 
1374 static ssize_t
1375 preferred_role_show(struct device *dev, struct device_attribute *attr,
1376             char *buf)
1377 {
1378     struct typec_port *port = to_typec_port(dev);
1379 
1380     if (port->cap->type != TYPEC_PORT_DRP)
1381         return 0;
1382 
1383     if (port->prefer_role < 0)
1384         return 0;
1385 
1386     return sprintf(buf, "%s\n", typec_roles[port->prefer_role]);
1387 }
1388 static DEVICE_ATTR_RW(preferred_role);
1389 
1390 static ssize_t data_role_store(struct device *dev,
1391                    struct device_attribute *attr,
1392                    const char *buf, size_t size)
1393 {
1394     struct typec_port *port = to_typec_port(dev);
1395     int ret;
1396 
1397     if (!port->ops || !port->ops->dr_set) {
1398         dev_dbg(dev, "data role swapping not supported\n");
1399         return -EOPNOTSUPP;
1400     }
1401 
1402     ret = sysfs_match_string(typec_data_roles, buf);
1403     if (ret < 0)
1404         return ret;
1405 
1406     mutex_lock(&port->port_type_lock);
1407     if (port->cap->data != TYPEC_PORT_DRD) {
1408         ret = -EOPNOTSUPP;
1409         goto unlock_and_ret;
1410     }
1411 
1412     ret = port->ops->dr_set(port, ret);
1413     if (ret)
1414         goto unlock_and_ret;
1415 
1416     ret = size;
1417 unlock_and_ret:
1418     mutex_unlock(&port->port_type_lock);
1419     return ret;
1420 }
1421 
1422 static ssize_t data_role_show(struct device *dev,
1423                   struct device_attribute *attr, char *buf)
1424 {
1425     struct typec_port *port = to_typec_port(dev);
1426 
1427     if (port->cap->data == TYPEC_PORT_DRD)
1428         return sprintf(buf, "%s\n", port->data_role == TYPEC_HOST ?
1429                    "[host] device" : "host [device]");
1430 
1431     return sprintf(buf, "[%s]\n", typec_data_roles[port->data_role]);
1432 }
1433 static DEVICE_ATTR_RW(data_role);
1434 
1435 static ssize_t power_role_store(struct device *dev,
1436                 struct device_attribute *attr,
1437                 const char *buf, size_t size)
1438 {
1439     struct typec_port *port = to_typec_port(dev);
1440     int ret;
1441 
1442     if (!port->ops || !port->ops->pr_set) {
1443         dev_dbg(dev, "power role swapping not supported\n");
1444         return -EOPNOTSUPP;
1445     }
1446 
1447     if (port->pwr_opmode != TYPEC_PWR_MODE_PD) {
1448         dev_dbg(dev, "partner unable to swap power role\n");
1449         return -EIO;
1450     }
1451 
1452     ret = sysfs_match_string(typec_roles, buf);
1453     if (ret < 0)
1454         return ret;
1455 
1456     mutex_lock(&port->port_type_lock);
1457     if (port->port_type != TYPEC_PORT_DRP) {
1458         dev_dbg(dev, "port type fixed at \"%s\"",
1459                  typec_port_power_roles[port->port_type]);
1460         ret = -EOPNOTSUPP;
1461         goto unlock_and_ret;
1462     }
1463 
1464     ret = port->ops->pr_set(port, ret);
1465     if (ret)
1466         goto unlock_and_ret;
1467 
1468     ret = size;
1469 unlock_and_ret:
1470     mutex_unlock(&port->port_type_lock);
1471     return ret;
1472 }
1473 
1474 static ssize_t power_role_show(struct device *dev,
1475                    struct device_attribute *attr, char *buf)
1476 {
1477     struct typec_port *port = to_typec_port(dev);
1478 
1479     if (port->cap->type == TYPEC_PORT_DRP)
1480         return sprintf(buf, "%s\n", port->pwr_role == TYPEC_SOURCE ?
1481                    "[source] sink" : "source [sink]");
1482 
1483     return sprintf(buf, "[%s]\n", typec_roles[port->pwr_role]);
1484 }
1485 static DEVICE_ATTR_RW(power_role);
1486 
1487 static ssize_t
1488 port_type_store(struct device *dev, struct device_attribute *attr,
1489             const char *buf, size_t size)
1490 {
1491     struct typec_port *port = to_typec_port(dev);
1492     int ret;
1493     enum typec_port_type type;
1494 
1495     if (port->cap->type != TYPEC_PORT_DRP ||
1496         !port->ops || !port->ops->port_type_set) {
1497         dev_dbg(dev, "changing port type not supported\n");
1498         return -EOPNOTSUPP;
1499     }
1500 
1501     ret = sysfs_match_string(typec_port_power_roles, buf);
1502     if (ret < 0)
1503         return ret;
1504 
1505     type = ret;
1506     mutex_lock(&port->port_type_lock);
1507 
1508     if (port->port_type == type) {
1509         ret = size;
1510         goto unlock_and_ret;
1511     }
1512 
1513     ret = port->ops->port_type_set(port, type);
1514     if (ret)
1515         goto unlock_and_ret;
1516 
1517     port->port_type = type;
1518     ret = size;
1519 
1520 unlock_and_ret:
1521     mutex_unlock(&port->port_type_lock);
1522     return ret;
1523 }
1524 
1525 static ssize_t
1526 port_type_show(struct device *dev, struct device_attribute *attr,
1527         char *buf)
1528 {
1529     struct typec_port *port = to_typec_port(dev);
1530 
1531     if (port->cap->type == TYPEC_PORT_DRP)
1532         return sprintf(buf, "%s\n",
1533                    typec_port_types_drp[port->port_type]);
1534 
1535     return sprintf(buf, "[%s]\n", typec_port_power_roles[port->cap->type]);
1536 }
1537 static DEVICE_ATTR_RW(port_type);
1538 
1539 static const char * const typec_pwr_opmodes[] = {
1540     [TYPEC_PWR_MODE_USB]    = "default",
1541     [TYPEC_PWR_MODE_1_5A]   = "1.5A",
1542     [TYPEC_PWR_MODE_3_0A]   = "3.0A",
1543     [TYPEC_PWR_MODE_PD] = "usb_power_delivery",
1544 };
1545 
1546 static ssize_t power_operation_mode_show(struct device *dev,
1547                      struct device_attribute *attr,
1548                      char *buf)
1549 {
1550     struct typec_port *port = to_typec_port(dev);
1551 
1552     return sprintf(buf, "%s\n", typec_pwr_opmodes[port->pwr_opmode]);
1553 }
1554 static DEVICE_ATTR_RO(power_operation_mode);
1555 
1556 static ssize_t vconn_source_store(struct device *dev,
1557                   struct device_attribute *attr,
1558                   const char *buf, size_t size)
1559 {
1560     struct typec_port *port = to_typec_port(dev);
1561     bool source;
1562     int ret;
1563 
1564     if (!port->cap->pd_revision) {
1565         dev_dbg(dev, "VCONN swap depends on USB Power Delivery\n");
1566         return -EOPNOTSUPP;
1567     }
1568 
1569     if (!port->ops || !port->ops->vconn_set) {
1570         dev_dbg(dev, "VCONN swapping not supported\n");
1571         return -EOPNOTSUPP;
1572     }
1573 
1574     ret = kstrtobool(buf, &source);
1575     if (ret)
1576         return ret;
1577 
1578     ret = port->ops->vconn_set(port, (enum typec_role)source);
1579     if (ret)
1580         return ret;
1581 
1582     return size;
1583 }
1584 
1585 static ssize_t vconn_source_show(struct device *dev,
1586                  struct device_attribute *attr, char *buf)
1587 {
1588     struct typec_port *port = to_typec_port(dev);
1589 
1590     return sprintf(buf, "%s\n",
1591                port->vconn_role == TYPEC_SOURCE ? "yes" : "no");
1592 }
1593 static DEVICE_ATTR_RW(vconn_source);
1594 
1595 static ssize_t supported_accessory_modes_show(struct device *dev,
1596                           struct device_attribute *attr,
1597                           char *buf)
1598 {
1599     struct typec_port *port = to_typec_port(dev);
1600     ssize_t ret = 0;
1601     int i;
1602 
1603     for (i = 0; i < ARRAY_SIZE(port->cap->accessory); i++) {
1604         if (port->cap->accessory[i])
1605             ret += sprintf(buf + ret, "%s ",
1606                    typec_accessory_modes[port->cap->accessory[i]]);
1607     }
1608 
1609     if (!ret)
1610         return sprintf(buf, "none\n");
1611 
1612     buf[ret - 1] = '\n';
1613 
1614     return ret;
1615 }
1616 static DEVICE_ATTR_RO(supported_accessory_modes);
1617 
1618 static ssize_t usb_typec_revision_show(struct device *dev,
1619                        struct device_attribute *attr,
1620                        char *buf)
1621 {
1622     struct typec_port *port = to_typec_port(dev);
1623     u16 rev = port->cap->revision;
1624 
1625     return sprintf(buf, "%d.%d\n", (rev >> 8) & 0xff, (rev >> 4) & 0xf);
1626 }
1627 static DEVICE_ATTR_RO(usb_typec_revision);
1628 
1629 static ssize_t usb_power_delivery_revision_show(struct device *dev,
1630                         struct device_attribute *attr,
1631                         char *buf)
1632 {
1633     u16 rev = 0;
1634 
1635     if (is_typec_partner(dev)) {
1636         struct typec_partner *partner = to_typec_partner(dev);
1637 
1638         rev = partner->pd_revision;
1639     } else if (is_typec_cable(dev)) {
1640         struct typec_cable *cable = to_typec_cable(dev);
1641 
1642         rev = cable->pd_revision;
1643     } else if (is_typec_port(dev)) {
1644         struct typec_port *p = to_typec_port(dev);
1645 
1646         rev = p->cap->pd_revision;
1647     }
1648     return sysfs_emit(buf, "%d.%d\n", (rev >> 8) & 0xff, (rev >> 4) & 0xf);
1649 }
1650 
1651 static ssize_t orientation_show(struct device *dev,
1652                    struct device_attribute *attr,
1653                    char *buf)
1654 {
1655     struct typec_port *port = to_typec_port(dev);
1656 
1657     return sprintf(buf, "%s\n", typec_orientations[port->orientation]);
1658 }
1659 static DEVICE_ATTR_RO(orientation);
1660 
1661 static struct attribute *typec_attrs[] = {
1662     &dev_attr_data_role.attr,
1663     &dev_attr_power_operation_mode.attr,
1664     &dev_attr_power_role.attr,
1665     &dev_attr_preferred_role.attr,
1666     &dev_attr_supported_accessory_modes.attr,
1667     &dev_attr_usb_power_delivery_revision.attr,
1668     &dev_attr_usb_typec_revision.attr,
1669     &dev_attr_vconn_source.attr,
1670     &dev_attr_port_type.attr,
1671     &dev_attr_orientation.attr,
1672     NULL,
1673 };
1674 
1675 static umode_t typec_attr_is_visible(struct kobject *kobj,
1676                      struct attribute *attr, int n)
1677 {
1678     struct typec_port *port = to_typec_port(kobj_to_dev(kobj));
1679 
1680     if (attr == &dev_attr_data_role.attr) {
1681         if (port->cap->data != TYPEC_PORT_DRD ||
1682             !port->ops || !port->ops->dr_set)
1683             return 0444;
1684     } else if (attr == &dev_attr_power_role.attr) {
1685         if (port->cap->type != TYPEC_PORT_DRP ||
1686             !port->ops || !port->ops->pr_set)
1687             return 0444;
1688     } else if (attr == &dev_attr_vconn_source.attr) {
1689         if (!port->cap->pd_revision ||
1690             !port->ops || !port->ops->vconn_set)
1691             return 0444;
1692     } else if (attr == &dev_attr_preferred_role.attr) {
1693         if (port->cap->type != TYPEC_PORT_DRP ||
1694             !port->ops || !port->ops->try_role)
1695             return 0444;
1696     } else if (attr == &dev_attr_port_type.attr) {
1697         if (!port->ops || !port->ops->port_type_set)
1698             return 0;
1699         if (port->cap->type != TYPEC_PORT_DRP)
1700             return 0444;
1701     } else if (attr == &dev_attr_orientation.attr) {
1702         if (port->cap->orientation_aware)
1703             return 0444;
1704         return 0;
1705     }
1706 
1707     return attr->mode;
1708 }
1709 
1710 static const struct attribute_group typec_group = {
1711     .is_visible = typec_attr_is_visible,
1712     .attrs = typec_attrs,
1713 };
1714 
1715 static const struct attribute_group *typec_groups[] = {
1716     &typec_group,
1717     &pd_group,
1718     NULL
1719 };
1720 
1721 static int typec_uevent(struct device *dev, struct kobj_uevent_env *env)
1722 {
1723     int ret;
1724 
1725     ret = add_uevent_var(env, "TYPEC_PORT=%s", dev_name(dev));
1726     if (ret)
1727         dev_err(dev, "failed to add uevent TYPEC_PORT\n");
1728 
1729     return ret;
1730 }
1731 
1732 static void typec_release(struct device *dev)
1733 {
1734     struct typec_port *port = to_typec_port(dev);
1735 
1736     ida_simple_remove(&typec_index_ida, port->id);
1737     ida_destroy(&port->mode_ids);
1738     typec_switch_put(port->sw);
1739     typec_mux_put(port->mux);
1740     typec_retimer_put(port->retimer);
1741     kfree(port->cap);
1742     kfree(port);
1743 }
1744 
1745 const struct device_type typec_port_dev_type = {
1746     .name = "typec_port",
1747     .groups = typec_groups,
1748     .uevent = typec_uevent,
1749     .release = typec_release,
1750 };
1751 
1752 /* --------------------------------------- */
1753 /* Driver callbacks to report role updates */
1754 
1755 static int partner_match(struct device *dev, void *data)
1756 {
1757     return is_typec_partner(dev);
1758 }
1759 
1760 /**
1761  * typec_set_data_role - Report data role change
1762  * @port: The USB Type-C Port where the role was changed
1763  * @role: The new data role
1764  *
1765  * This routine is used by the port drivers to report data role changes.
1766  */
1767 void typec_set_data_role(struct typec_port *port, enum typec_data_role role)
1768 {
1769     struct device *partner_dev;
1770 
1771     if (port->data_role == role)
1772         return;
1773 
1774     port->data_role = role;
1775     sysfs_notify(&port->dev.kobj, NULL, "data_role");
1776     kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1777 
1778     partner_dev = device_find_child(&port->dev, NULL, partner_match);
1779     if (!partner_dev)
1780         return;
1781 
1782     if (to_typec_partner(partner_dev)->identity)
1783         typec_product_type_notify(partner_dev);
1784 
1785     put_device(partner_dev);
1786 }
1787 EXPORT_SYMBOL_GPL(typec_set_data_role);
1788 
1789 /**
1790  * typec_set_pwr_role - Report power role change
1791  * @port: The USB Type-C Port where the role was changed
1792  * @role: The new data role
1793  *
1794  * This routine is used by the port drivers to report power role changes.
1795  */
1796 void typec_set_pwr_role(struct typec_port *port, enum typec_role role)
1797 {
1798     if (port->pwr_role == role)
1799         return;
1800 
1801     port->pwr_role = role;
1802     sysfs_notify(&port->dev.kobj, NULL, "power_role");
1803     kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1804 }
1805 EXPORT_SYMBOL_GPL(typec_set_pwr_role);
1806 
1807 /**
1808  * typec_set_vconn_role - Report VCONN source change
1809  * @port: The USB Type-C Port which VCONN role changed
1810  * @role: Source when @port is sourcing VCONN, or Sink when it's not
1811  *
1812  * This routine is used by the port drivers to report if the VCONN source is
1813  * changes.
1814  */
1815 void typec_set_vconn_role(struct typec_port *port, enum typec_role role)
1816 {
1817     if (port->vconn_role == role)
1818         return;
1819 
1820     port->vconn_role = role;
1821     sysfs_notify(&port->dev.kobj, NULL, "vconn_source");
1822     kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1823 }
1824 EXPORT_SYMBOL_GPL(typec_set_vconn_role);
1825 
1826 /**
1827  * typec_set_pwr_opmode - Report changed power operation mode
1828  * @port: The USB Type-C Port where the mode was changed
1829  * @opmode: New power operation mode
1830  *
1831  * This routine is used by the port drivers to report changed power operation
1832  * mode in @port. The modes are USB (default), 1.5A, 3.0A as defined in USB
1833  * Type-C specification, and "USB Power Delivery" when the power levels are
1834  * negotiated with methods defined in USB Power Delivery specification.
1835  */
1836 void typec_set_pwr_opmode(struct typec_port *port,
1837               enum typec_pwr_opmode opmode)
1838 {
1839     struct device *partner_dev;
1840 
1841     if (port->pwr_opmode == opmode)
1842         return;
1843 
1844     port->pwr_opmode = opmode;
1845     sysfs_notify(&port->dev.kobj, NULL, "power_operation_mode");
1846     kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1847 
1848     partner_dev = device_find_child(&port->dev, NULL, partner_match);
1849     if (partner_dev) {
1850         struct typec_partner *partner = to_typec_partner(partner_dev);
1851 
1852         if (opmode == TYPEC_PWR_MODE_PD && !partner->usb_pd) {
1853             partner->usb_pd = 1;
1854             sysfs_notify(&partner_dev->kobj, NULL,
1855                      "supports_usb_power_delivery");
1856             kobject_uevent(&partner_dev->kobj, KOBJ_CHANGE);
1857         }
1858         put_device(partner_dev);
1859     }
1860 }
1861 EXPORT_SYMBOL_GPL(typec_set_pwr_opmode);
1862 
1863 /**
1864  * typec_find_pwr_opmode - Get the typec power operation mode capability
1865  * @name: power operation mode string
1866  *
1867  * This routine is used to find the typec_pwr_opmode by its string @name.
1868  *
1869  * Returns typec_pwr_opmode if success, otherwise negative error code.
1870  */
1871 int typec_find_pwr_opmode(const char *name)
1872 {
1873     return match_string(typec_pwr_opmodes,
1874                 ARRAY_SIZE(typec_pwr_opmodes), name);
1875 }
1876 EXPORT_SYMBOL_GPL(typec_find_pwr_opmode);
1877 
1878 /**
1879  * typec_find_orientation - Convert orientation string to enum typec_orientation
1880  * @name: Orientation string
1881  *
1882  * This routine is used to find the typec_orientation by its string name @name.
1883  *
1884  * Returns the orientation value on success, otherwise negative error code.
1885  */
1886 int typec_find_orientation(const char *name)
1887 {
1888     return match_string(typec_orientations, ARRAY_SIZE(typec_orientations),
1889                 name);
1890 }
1891 EXPORT_SYMBOL_GPL(typec_find_orientation);
1892 
1893 /**
1894  * typec_find_port_power_role - Get the typec port power capability
1895  * @name: port power capability string
1896  *
1897  * This routine is used to find the typec_port_type by its string name.
1898  *
1899  * Returns typec_port_type if success, otherwise negative error code.
1900  */
1901 int typec_find_port_power_role(const char *name)
1902 {
1903     return match_string(typec_port_power_roles,
1904                 ARRAY_SIZE(typec_port_power_roles), name);
1905 }
1906 EXPORT_SYMBOL_GPL(typec_find_port_power_role);
1907 
1908 /**
1909  * typec_find_power_role - Find the typec one specific power role
1910  * @name: power role string
1911  *
1912  * This routine is used to find the typec_role by its string name.
1913  *
1914  * Returns typec_role if success, otherwise negative error code.
1915  */
1916 int typec_find_power_role(const char *name)
1917 {
1918     return match_string(typec_roles, ARRAY_SIZE(typec_roles), name);
1919 }
1920 EXPORT_SYMBOL_GPL(typec_find_power_role);
1921 
1922 /**
1923  * typec_find_port_data_role - Get the typec port data capability
1924  * @name: port data capability string
1925  *
1926  * This routine is used to find the typec_port_data by its string name.
1927  *
1928  * Returns typec_port_data if success, otherwise negative error code.
1929  */
1930 int typec_find_port_data_role(const char *name)
1931 {
1932     return match_string(typec_port_data_roles,
1933                 ARRAY_SIZE(typec_port_data_roles), name);
1934 }
1935 EXPORT_SYMBOL_GPL(typec_find_port_data_role);
1936 
1937 /* ------------------------------------------ */
1938 /* API for Multiplexer/DeMultiplexer Switches */
1939 
1940 /**
1941  * typec_set_orientation - Set USB Type-C cable plug orientation
1942  * @port: USB Type-C Port
1943  * @orientation: USB Type-C cable plug orientation
1944  *
1945  * Set cable plug orientation for @port.
1946  */
1947 int typec_set_orientation(struct typec_port *port,
1948               enum typec_orientation orientation)
1949 {
1950     int ret;
1951 
1952     ret = typec_switch_set(port->sw, orientation);
1953     if (ret)
1954         return ret;
1955 
1956     port->orientation = orientation;
1957     sysfs_notify(&port->dev.kobj, NULL, "orientation");
1958     kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1959 
1960     return 0;
1961 }
1962 EXPORT_SYMBOL_GPL(typec_set_orientation);
1963 
1964 /**
1965  * typec_get_orientation - Get USB Type-C cable plug orientation
1966  * @port: USB Type-C Port
1967  *
1968  * Get current cable plug orientation for @port.
1969  */
1970 enum typec_orientation typec_get_orientation(struct typec_port *port)
1971 {
1972     return port->orientation;
1973 }
1974 EXPORT_SYMBOL_GPL(typec_get_orientation);
1975 
1976 /**
1977  * typec_set_mode - Set mode of operation for USB Type-C connector
1978  * @port: USB Type-C connector
1979  * @mode: Accessory Mode, USB Operation or Safe State
1980  *
1981  * Configure @port for Accessory Mode @mode. This function will configure the
1982  * muxes needed for @mode.
1983  */
1984 int typec_set_mode(struct typec_port *port, int mode)
1985 {
1986     struct typec_mux_state state = { };
1987 
1988     state.mode = mode;
1989 
1990     return typec_mux_set(port->mux, &state);
1991 }
1992 EXPORT_SYMBOL_GPL(typec_set_mode);
1993 
1994 /* --------------------------------------- */
1995 
1996 /**
1997  * typec_get_negotiated_svdm_version - Get negotiated SVDM Version
1998  * @port: USB Type-C Port.
1999  *
2000  * Get the negotiated SVDM Version. The Version is set to the port default
2001  * value stored in typec_capability on partner registration, and updated after
2002  * a successful Discover Identity if the negotiated value is less than the
2003  * default value.
2004  *
2005  * Returns usb_pd_svdm_ver if the partner has been registered otherwise -ENODEV.
2006  */
2007 int typec_get_negotiated_svdm_version(struct typec_port *port)
2008 {
2009     enum usb_pd_svdm_ver svdm_version;
2010     struct device *partner_dev;
2011 
2012     partner_dev = device_find_child(&port->dev, NULL, partner_match);
2013     if (!partner_dev)
2014         return -ENODEV;
2015 
2016     svdm_version = to_typec_partner(partner_dev)->svdm_version;
2017     put_device(partner_dev);
2018 
2019     return svdm_version;
2020 }
2021 EXPORT_SYMBOL_GPL(typec_get_negotiated_svdm_version);
2022 
2023 /**
2024  * typec_get_drvdata - Return private driver data pointer
2025  * @port: USB Type-C port
2026  */
2027 void *typec_get_drvdata(struct typec_port *port)
2028 {
2029     return dev_get_drvdata(&port->dev);
2030 }
2031 EXPORT_SYMBOL_GPL(typec_get_drvdata);
2032 
2033 int typec_get_fw_cap(struct typec_capability *cap,
2034              struct fwnode_handle *fwnode)
2035 {
2036     const char *cap_str;
2037     int ret;
2038 
2039     cap->fwnode = fwnode;
2040 
2041     ret = fwnode_property_read_string(fwnode, "power-role", &cap_str);
2042     if (ret < 0)
2043         return ret;
2044 
2045     ret = typec_find_port_power_role(cap_str);
2046     if (ret < 0)
2047         return ret;
2048     cap->type = ret;
2049 
2050     /* USB data support is optional */
2051     ret = fwnode_property_read_string(fwnode, "data-role", &cap_str);
2052     if (ret == 0) {
2053         ret = typec_find_port_data_role(cap_str);
2054         if (ret < 0)
2055             return ret;
2056         cap->data = ret;
2057     }
2058 
2059     /* Get the preferred power role for a DRP */
2060     if (cap->type == TYPEC_PORT_DRP) {
2061         cap->prefer_role = TYPEC_NO_PREFERRED_ROLE;
2062 
2063         ret = fwnode_property_read_string(fwnode, "try-power-role", &cap_str);
2064         if (ret == 0) {
2065             ret = typec_find_power_role(cap_str);
2066             if (ret < 0)
2067                 return ret;
2068             cap->prefer_role = ret;
2069         }
2070     }
2071 
2072     return 0;
2073 }
2074 EXPORT_SYMBOL_GPL(typec_get_fw_cap);
2075 
2076 /**
2077  * typec_port_register_altmode - Register USB Type-C Port Alternate Mode
2078  * @port: USB Type-C Port that supports the alternate mode
2079  * @desc: Description of the alternate mode
2080  *
2081  * This routine is used to register an alternate mode that @port is capable of
2082  * supporting.
2083  *
2084  * Returns handle to the alternate mode on success or ERR_PTR on failure.
2085  */
2086 struct typec_altmode *
2087 typec_port_register_altmode(struct typec_port *port,
2088                 const struct typec_altmode_desc *desc)
2089 {
2090     struct typec_altmode *adev;
2091     struct typec_mux *mux;
2092 
2093     mux = typec_mux_get(&port->dev, desc);
2094     if (IS_ERR(mux))
2095         return ERR_CAST(mux);
2096 
2097     adev = typec_register_altmode(&port->dev, desc);
2098     if (IS_ERR(adev))
2099         typec_mux_put(mux);
2100     else
2101         to_altmode(adev)->mux = mux;
2102 
2103     return adev;
2104 }
2105 EXPORT_SYMBOL_GPL(typec_port_register_altmode);
2106 
2107 void typec_port_register_altmodes(struct typec_port *port,
2108     const struct typec_altmode_ops *ops, void *drvdata,
2109     struct typec_altmode **altmodes, size_t n)
2110 {
2111     struct fwnode_handle *altmodes_node, *child;
2112     struct typec_altmode_desc desc;
2113     struct typec_altmode *alt;
2114     size_t index = 0;
2115     u32 svid, vdo;
2116     int ret;
2117 
2118     altmodes_node = device_get_named_child_node(&port->dev, "altmodes");
2119     if (!altmodes_node)
2120         return; /* No altmodes specified */
2121 
2122     fwnode_for_each_child_node(altmodes_node, child) {
2123         ret = fwnode_property_read_u32(child, "svid", &svid);
2124         if (ret) {
2125             dev_err(&port->dev, "Error reading svid for altmode %s\n",
2126                 fwnode_get_name(child));
2127             continue;
2128         }
2129 
2130         ret = fwnode_property_read_u32(child, "vdo", &vdo);
2131         if (ret) {
2132             dev_err(&port->dev, "Error reading vdo for altmode %s\n",
2133                 fwnode_get_name(child));
2134             continue;
2135         }
2136 
2137         if (index >= n) {
2138             dev_err(&port->dev, "Error not enough space for altmode %s\n",
2139                 fwnode_get_name(child));
2140             continue;
2141         }
2142 
2143         desc.svid = svid;
2144         desc.vdo = vdo;
2145         desc.mode = index + 1;
2146         alt = typec_port_register_altmode(port, &desc);
2147         if (IS_ERR(alt)) {
2148             dev_err(&port->dev, "Error registering altmode %s\n",
2149                 fwnode_get_name(child));
2150             continue;
2151         }
2152 
2153         alt->ops = ops;
2154         typec_altmode_set_drvdata(alt, drvdata);
2155         altmodes[index] = alt;
2156         index++;
2157     }
2158 }
2159 EXPORT_SYMBOL_GPL(typec_port_register_altmodes);
2160 
2161 /**
2162  * typec_register_port - Register a USB Type-C Port
2163  * @parent: Parent device
2164  * @cap: Description of the port
2165  *
2166  * Registers a device for USB Type-C Port described in @cap.
2167  *
2168  * Returns handle to the port on success or ERR_PTR on failure.
2169  */
2170 struct typec_port *typec_register_port(struct device *parent,
2171                        const struct typec_capability *cap)
2172 {
2173     struct typec_port *port;
2174     int ret;
2175     int id;
2176 
2177     port = kzalloc(sizeof(*port), GFP_KERNEL);
2178     if (!port)
2179         return ERR_PTR(-ENOMEM);
2180 
2181     id = ida_simple_get(&typec_index_ida, 0, 0, GFP_KERNEL);
2182     if (id < 0) {
2183         kfree(port);
2184         return ERR_PTR(id);
2185     }
2186 
2187     switch (cap->type) {
2188     case TYPEC_PORT_SRC:
2189         port->pwr_role = TYPEC_SOURCE;
2190         port->vconn_role = TYPEC_SOURCE;
2191         break;
2192     case TYPEC_PORT_SNK:
2193         port->pwr_role = TYPEC_SINK;
2194         port->vconn_role = TYPEC_SINK;
2195         break;
2196     case TYPEC_PORT_DRP:
2197         if (cap->prefer_role != TYPEC_NO_PREFERRED_ROLE)
2198             port->pwr_role = cap->prefer_role;
2199         else
2200             port->pwr_role = TYPEC_SINK;
2201         break;
2202     }
2203 
2204     switch (cap->data) {
2205     case TYPEC_PORT_DFP:
2206         port->data_role = TYPEC_HOST;
2207         break;
2208     case TYPEC_PORT_UFP:
2209         port->data_role = TYPEC_DEVICE;
2210         break;
2211     case TYPEC_PORT_DRD:
2212         if (cap->prefer_role == TYPEC_SOURCE)
2213             port->data_role = TYPEC_HOST;
2214         else
2215             port->data_role = TYPEC_DEVICE;
2216         break;
2217     }
2218 
2219     ida_init(&port->mode_ids);
2220     mutex_init(&port->port_type_lock);
2221 
2222     port->id = id;
2223     port->ops = cap->ops;
2224     port->port_type = cap->type;
2225     port->prefer_role = cap->prefer_role;
2226 
2227     device_initialize(&port->dev);
2228     port->dev.class = &typec_class;
2229     port->dev.parent = parent;
2230     port->dev.fwnode = cap->fwnode;
2231     port->dev.type = &typec_port_dev_type;
2232     dev_set_name(&port->dev, "port%d", id);
2233     dev_set_drvdata(&port->dev, cap->driver_data);
2234 
2235     port->cap = kmemdup(cap, sizeof(*cap), GFP_KERNEL);
2236     if (!port->cap) {
2237         put_device(&port->dev);
2238         return ERR_PTR(-ENOMEM);
2239     }
2240 
2241     port->sw = typec_switch_get(&port->dev);
2242     if (IS_ERR(port->sw)) {
2243         ret = PTR_ERR(port->sw);
2244         put_device(&port->dev);
2245         return ERR_PTR(ret);
2246     }
2247 
2248     port->mux = typec_mux_get(&port->dev, NULL);
2249     if (IS_ERR(port->mux)) {
2250         ret = PTR_ERR(port->mux);
2251         put_device(&port->dev);
2252         return ERR_PTR(ret);
2253     }
2254 
2255     port->retimer = typec_retimer_get(&port->dev);
2256     if (IS_ERR(port->retimer)) {
2257         ret = PTR_ERR(port->retimer);
2258         put_device(&port->dev);
2259         return ERR_PTR(ret);
2260     }
2261 
2262     ret = device_add(&port->dev);
2263     if (ret) {
2264         dev_err(parent, "failed to register port (%d)\n", ret);
2265         put_device(&port->dev);
2266         return ERR_PTR(ret);
2267     }
2268 
2269     ret = typec_port_set_usb_power_delivery(port, cap->pd);
2270     if (ret) {
2271         dev_err(&port->dev, "failed to link pd\n");
2272         device_unregister(&port->dev);
2273         return ERR_PTR(ret);
2274     }
2275 
2276     ret = typec_link_ports(port);
2277     if (ret)
2278         dev_warn(&port->dev, "failed to create symlinks (%d)\n", ret);
2279 
2280     return port;
2281 }
2282 EXPORT_SYMBOL_GPL(typec_register_port);
2283 
2284 /**
2285  * typec_unregister_port - Unregister a USB Type-C Port
2286  * @port: The port to be unregistered
2287  *
2288  * Unregister device created with typec_register_port().
2289  */
2290 void typec_unregister_port(struct typec_port *port)
2291 {
2292     if (!IS_ERR_OR_NULL(port)) {
2293         typec_unlink_ports(port);
2294         typec_port_set_usb_power_delivery(port, NULL);
2295         device_unregister(&port->dev);
2296     }
2297 }
2298 EXPORT_SYMBOL_GPL(typec_unregister_port);
2299 
2300 static int __init typec_init(void)
2301 {
2302     int ret;
2303 
2304     ret = bus_register(&typec_bus);
2305     if (ret)
2306         return ret;
2307 
2308     ret = class_register(&typec_mux_class);
2309     if (ret)
2310         goto err_unregister_bus;
2311 
2312     ret = class_register(&retimer_class);
2313     if (ret)
2314         goto err_unregister_mux_class;
2315 
2316     ret = class_register(&typec_class);
2317     if (ret)
2318         goto err_unregister_retimer_class;
2319 
2320     ret = usb_power_delivery_init();
2321     if (ret)
2322         goto err_unregister_class;
2323 
2324     return 0;
2325 
2326 err_unregister_class:
2327     class_unregister(&typec_class);
2328 
2329 err_unregister_retimer_class:
2330     class_unregister(&retimer_class);
2331 
2332 err_unregister_mux_class:
2333     class_unregister(&typec_mux_class);
2334 
2335 err_unregister_bus:
2336     bus_unregister(&typec_bus);
2337 
2338     return ret;
2339 }
2340 subsys_initcall(typec_init);
2341 
2342 static void __exit typec_exit(void)
2343 {
2344     usb_power_delivery_exit();
2345     class_unregister(&typec_class);
2346     ida_destroy(&typec_index_ida);
2347     bus_unregister(&typec_bus);
2348     class_unregister(&typec_mux_class);
2349     class_unregister(&retimer_class);
2350 }
2351 module_exit(typec_exit);
2352 
2353 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
2354 MODULE_LICENSE("GPL v2");
2355 MODULE_DESCRIPTION("USB Type-C Connector Class");