Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * USB Power Delivery sysfs entries
0004  *
0005  * Copyright (C) 2022, Intel Corporation
0006  * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
0007  */
0008 
0009 #include <linux/slab.h>
0010 #include <linux/usb/pd.h>
0011 
0012 #include "pd.h"
0013 
0014 static DEFINE_IDA(pd_ida);
0015 
0016 static struct class pd_class = {
0017     .name = "usb_power_delivery",
0018     .owner = THIS_MODULE,
0019 };
0020 
0021 #define to_pdo(o) container_of(o, struct pdo, dev)
0022 
0023 struct pdo {
0024     struct device dev;
0025     int object_position;
0026     u32 pdo;
0027 };
0028 
0029 static void pdo_release(struct device *dev)
0030 {
0031     kfree(to_pdo(dev));
0032 }
0033 
0034 /* -------------------------------------------------------------------------- */
0035 /* Fixed Supply */
0036 
0037 static ssize_t
0038 dual_role_power_show(struct device *dev, struct device_attribute *attr, char *buf)
0039 {
0040     return sysfs_emit(buf, "%u\n", !!(to_pdo(dev)->pdo & PDO_FIXED_DUAL_ROLE));
0041 }
0042 static DEVICE_ATTR_RO(dual_role_power);
0043 
0044 static ssize_t
0045 usb_suspend_supported_show(struct device *dev, struct device_attribute *attr, char *buf)
0046 {
0047     return sysfs_emit(buf, "%u\n", !!(to_pdo(dev)->pdo & PDO_FIXED_SUSPEND));
0048 }
0049 static DEVICE_ATTR_RO(usb_suspend_supported);
0050 
0051 static ssize_t
0052 unconstrained_power_show(struct device *dev, struct device_attribute *attr, char *buf)
0053 {
0054     return sysfs_emit(buf, "%u\n", !!(to_pdo(dev)->pdo & PDO_FIXED_EXTPOWER));
0055 }
0056 static DEVICE_ATTR_RO(unconstrained_power);
0057 
0058 static ssize_t
0059 usb_communication_capable_show(struct device *dev, struct device_attribute *attr, char *buf)
0060 {
0061     return sysfs_emit(buf, "%u\n", !!(to_pdo(dev)->pdo & PDO_FIXED_USB_COMM));
0062 }
0063 static DEVICE_ATTR_RO(usb_communication_capable);
0064 
0065 static ssize_t
0066 dual_role_data_show(struct device *dev, struct device_attribute *attr, char *buf)
0067 {
0068     return sysfs_emit(buf, "%u\n", !!(to_pdo(dev)->pdo & PDO_FIXED_DATA_SWAP));
0069 }
0070 static DEVICE_ATTR_RO(dual_role_data);
0071 
0072 static ssize_t
0073 unchunked_extended_messages_supported_show(struct device *dev,
0074                        struct device_attribute *attr, char *buf)
0075 {
0076     return sysfs_emit(buf, "%u\n", !!(to_pdo(dev)->pdo & PDO_FIXED_UNCHUNK_EXT));
0077 }
0078 static DEVICE_ATTR_RO(unchunked_extended_messages_supported);
0079 
0080 /*
0081  * REVISIT: Peak Current requires access also to the RDO.
0082 static ssize_t
0083 peak_current_show(struct device *dev, struct device_attribute *attr, char *buf)
0084 {
0085     ...
0086 }
0087 */
0088 
0089 static ssize_t
0090 fast_role_swap_current_show(struct device *dev, struct device_attribute *attr, char *buf)
0091 {
0092     return sysfs_emit(buf, "%u\n", to_pdo(dev)->pdo >> PDO_FIXED_FRS_CURR_SHIFT) & 3;
0093 }
0094 static DEVICE_ATTR_RO(fast_role_swap_current);
0095 
0096 static ssize_t voltage_show(struct device *dev, struct device_attribute *attr, char *buf)
0097 {
0098     return sysfs_emit(buf, "%umV\n", pdo_fixed_voltage(to_pdo(dev)->pdo));
0099 }
0100 static DEVICE_ATTR_RO(voltage);
0101 
0102 /* Shared with Variable supplies, both source and sink */
0103 static ssize_t current_show(struct device *dev, struct device_attribute *attr, char *buf)
0104 {
0105     return sysfs_emit(buf, "%umA\n", pdo_max_current(to_pdo(dev)->pdo));
0106 }
0107 
0108 /* Shared with Variable type supplies */
0109 static struct device_attribute maximum_current_attr = {
0110     .attr = {
0111         .name = "maximum_current",
0112         .mode = 0444,
0113     },
0114     .show = current_show,
0115 };
0116 
0117 static struct device_attribute operational_current_attr = {
0118     .attr = {
0119         .name = "operational_current",
0120         .mode = 0444,
0121     },
0122     .show = current_show,
0123 };
0124 
0125 static struct attribute *source_fixed_supply_attrs[] = {
0126     &dev_attr_dual_role_power.attr,
0127     &dev_attr_usb_suspend_supported.attr,
0128     &dev_attr_unconstrained_power.attr,
0129     &dev_attr_usb_communication_capable.attr,
0130     &dev_attr_dual_role_data.attr,
0131     &dev_attr_unchunked_extended_messages_supported.attr,
0132     /*&dev_attr_peak_current.attr,*/
0133     &dev_attr_voltage.attr,
0134     &maximum_current_attr.attr,
0135     NULL
0136 };
0137 
0138 static umode_t fixed_attr_is_visible(struct kobject *kobj, struct attribute *attr, int n)
0139 {
0140     if (to_pdo(kobj_to_dev(kobj))->object_position &&
0141         /*attr != &dev_attr_peak_current.attr &&*/
0142         attr != &dev_attr_voltage.attr &&
0143         attr != &maximum_current_attr.attr &&
0144         attr != &operational_current_attr.attr)
0145         return 0;
0146 
0147     return attr->mode;
0148 }
0149 
0150 static const struct attribute_group source_fixed_supply_group = {
0151     .is_visible = fixed_attr_is_visible,
0152     .attrs = source_fixed_supply_attrs,
0153 };
0154 __ATTRIBUTE_GROUPS(source_fixed_supply);
0155 
0156 static struct device_type source_fixed_supply_type = {
0157     .name = "pdo",
0158     .release = pdo_release,
0159     .groups = source_fixed_supply_groups,
0160 };
0161 
0162 static struct attribute *sink_fixed_supply_attrs[] = {
0163     &dev_attr_dual_role_power.attr,
0164     &dev_attr_usb_suspend_supported.attr,
0165     &dev_attr_unconstrained_power.attr,
0166     &dev_attr_usb_communication_capable.attr,
0167     &dev_attr_dual_role_data.attr,
0168     &dev_attr_unchunked_extended_messages_supported.attr,
0169     &dev_attr_fast_role_swap_current.attr,
0170     &dev_attr_voltage.attr,
0171     &operational_current_attr.attr,
0172     NULL
0173 };
0174 
0175 static const struct attribute_group sink_fixed_supply_group = {
0176     .is_visible = fixed_attr_is_visible,
0177     .attrs = sink_fixed_supply_attrs,
0178 };
0179 __ATTRIBUTE_GROUPS(sink_fixed_supply);
0180 
0181 static struct device_type sink_fixed_supply_type = {
0182     .name = "pdo",
0183     .release = pdo_release,
0184     .groups = sink_fixed_supply_groups,
0185 };
0186 
0187 /* -------------------------------------------------------------------------- */
0188 /* Variable Supply */
0189 
0190 static ssize_t
0191 maximum_voltage_show(struct device *dev, struct device_attribute *attr, char *buf)
0192 {
0193     return sysfs_emit(buf, "%umV\n", pdo_max_voltage(to_pdo(dev)->pdo));
0194 }
0195 static DEVICE_ATTR_RO(maximum_voltage);
0196 
0197 static ssize_t
0198 minimum_voltage_show(struct device *dev, struct device_attribute *attr, char *buf)
0199 {
0200     return sysfs_emit(buf, "%umV\n", pdo_min_voltage(to_pdo(dev)->pdo));
0201 }
0202 static DEVICE_ATTR_RO(minimum_voltage);
0203 
0204 static struct attribute *source_variable_supply_attrs[] = {
0205     &dev_attr_maximum_voltage.attr,
0206     &dev_attr_minimum_voltage.attr,
0207     &maximum_current_attr.attr,
0208     NULL
0209 };
0210 ATTRIBUTE_GROUPS(source_variable_supply);
0211 
0212 static struct device_type source_variable_supply_type = {
0213     .name = "pdo",
0214     .release = pdo_release,
0215     .groups = source_variable_supply_groups,
0216 };
0217 
0218 static struct attribute *sink_variable_supply_attrs[] = {
0219     &dev_attr_maximum_voltage.attr,
0220     &dev_attr_minimum_voltage.attr,
0221     &operational_current_attr.attr,
0222     NULL
0223 };
0224 ATTRIBUTE_GROUPS(sink_variable_supply);
0225 
0226 static struct device_type sink_variable_supply_type = {
0227     .name = "pdo",
0228     .release = pdo_release,
0229     .groups = sink_variable_supply_groups,
0230 };
0231 
0232 /* -------------------------------------------------------------------------- */
0233 /* Battery */
0234 
0235 static ssize_t
0236 maximum_power_show(struct device *dev, struct device_attribute *attr, char *buf)
0237 {
0238     return sysfs_emit(buf, "%umW\n", pdo_max_power(to_pdo(dev)->pdo));
0239 }
0240 static DEVICE_ATTR_RO(maximum_power);
0241 
0242 static ssize_t
0243 operational_power_show(struct device *dev, struct device_attribute *attr, char *buf)
0244 {
0245     return sysfs_emit(buf, "%umW\n", pdo_max_power(to_pdo(dev)->pdo));
0246 }
0247 static DEVICE_ATTR_RO(operational_power);
0248 
0249 static struct attribute *source_battery_attrs[] = {
0250     &dev_attr_maximum_voltage.attr,
0251     &dev_attr_minimum_voltage.attr,
0252     &dev_attr_maximum_power.attr,
0253     NULL
0254 };
0255 ATTRIBUTE_GROUPS(source_battery);
0256 
0257 static struct device_type source_battery_type = {
0258     .name = "pdo",
0259     .release = pdo_release,
0260     .groups = source_battery_groups,
0261 };
0262 
0263 static struct attribute *sink_battery_attrs[] = {
0264     &dev_attr_maximum_voltage.attr,
0265     &dev_attr_minimum_voltage.attr,
0266     &dev_attr_operational_power.attr,
0267     NULL
0268 };
0269 ATTRIBUTE_GROUPS(sink_battery);
0270 
0271 static struct device_type sink_battery_type = {
0272     .name = "pdo",
0273     .release = pdo_release,
0274     .groups = sink_battery_groups,
0275 };
0276 
0277 /* -------------------------------------------------------------------------- */
0278 /* Standard Power Range (SPR) Programmable Power Supply (PPS) */
0279 
0280 static ssize_t
0281 pps_power_limited_show(struct device *dev, struct device_attribute *attr, char *buf)
0282 {
0283     return sysfs_emit(buf, "%u\n", !!(to_pdo(dev)->pdo & BIT(27)));
0284 }
0285 static DEVICE_ATTR_RO(pps_power_limited);
0286 
0287 static ssize_t
0288 pps_max_voltage_show(struct device *dev, struct device_attribute *attr, char *buf)
0289 {
0290     return sysfs_emit(buf, "%umV\n", pdo_pps_apdo_max_voltage(to_pdo(dev)->pdo));
0291 }
0292 
0293 static ssize_t
0294 pps_min_voltage_show(struct device *dev, struct device_attribute *attr, char *buf)
0295 {
0296     return sysfs_emit(buf, "%umV\n", pdo_pps_apdo_min_voltage(to_pdo(dev)->pdo));
0297 }
0298 
0299 static ssize_t
0300 pps_max_current_show(struct device *dev, struct device_attribute *attr, char *buf)
0301 {
0302     return sysfs_emit(buf, "%umA\n", pdo_pps_apdo_max_current(to_pdo(dev)->pdo));
0303 }
0304 
0305 static struct device_attribute pps_max_voltage_attr = {
0306     .attr = {
0307         .name = "maximum_voltage",
0308         .mode = 0444,
0309     },
0310     .show = pps_max_voltage_show,
0311 };
0312 
0313 static struct device_attribute pps_min_voltage_attr = {
0314     .attr = {
0315         .name = "minimum_voltage",
0316         .mode = 0444,
0317     },
0318     .show = pps_min_voltage_show,
0319 };
0320 
0321 static struct device_attribute pps_max_current_attr = {
0322     .attr = {
0323         .name = "maximum_current",
0324         .mode = 0444,
0325     },
0326     .show = pps_max_current_show,
0327 };
0328 
0329 static struct attribute *source_pps_attrs[] = {
0330     &dev_attr_pps_power_limited.attr,
0331     &pps_max_voltage_attr.attr,
0332     &pps_min_voltage_attr.attr,
0333     &pps_max_current_attr.attr,
0334     NULL
0335 };
0336 ATTRIBUTE_GROUPS(source_pps);
0337 
0338 static struct device_type source_pps_type = {
0339     .name = "pdo",
0340     .release = pdo_release,
0341     .groups = source_pps_groups,
0342 };
0343 
0344 static struct attribute *sink_pps_attrs[] = {
0345     &pps_max_voltage_attr.attr,
0346     &pps_min_voltage_attr.attr,
0347     &pps_max_current_attr.attr,
0348     NULL
0349 };
0350 ATTRIBUTE_GROUPS(sink_pps);
0351 
0352 static struct device_type sink_pps_type = {
0353     .name = "pdo",
0354     .release = pdo_release,
0355     .groups = sink_pps_groups,
0356 };
0357 
0358 /* -------------------------------------------------------------------------- */
0359 
0360 static const char * const supply_name[] = {
0361     [PDO_TYPE_FIXED] = "fixed_supply",
0362     [PDO_TYPE_BATT]  = "battery",
0363     [PDO_TYPE_VAR]   = "variable_supply",
0364 };
0365 
0366 static const char * const apdo_supply_name[] = {
0367     [APDO_TYPE_PPS]  = "programmable_supply",
0368 };
0369 
0370 static struct device_type *source_type[] = {
0371     [PDO_TYPE_FIXED] = &source_fixed_supply_type,
0372     [PDO_TYPE_BATT]  = &source_battery_type,
0373     [PDO_TYPE_VAR]   = &source_variable_supply_type,
0374 };
0375 
0376 static struct device_type *source_apdo_type[] = {
0377     [APDO_TYPE_PPS]  = &source_pps_type,
0378 };
0379 
0380 static struct device_type *sink_type[] = {
0381     [PDO_TYPE_FIXED] = &sink_fixed_supply_type,
0382     [PDO_TYPE_BATT]  = &sink_battery_type,
0383     [PDO_TYPE_VAR]   = &sink_variable_supply_type,
0384 };
0385 
0386 static struct device_type *sink_apdo_type[] = {
0387     [APDO_TYPE_PPS]  = &sink_pps_type,
0388 };
0389 
0390 /* REVISIT: Export when EPR_*_Capabilities need to be supported. */
0391 static int add_pdo(struct usb_power_delivery_capabilities *cap, u32 pdo, int position)
0392 {
0393     struct device_type *type;
0394     const char *name;
0395     struct pdo *p;
0396     int ret;
0397 
0398     p = kzalloc(sizeof(*p), GFP_KERNEL);
0399     if (!p)
0400         return -ENOMEM;
0401 
0402     p->pdo = pdo;
0403     p->object_position = position;
0404 
0405     if (pdo_type(pdo) == PDO_TYPE_APDO) {
0406         /* FIXME: Only PPS supported for now! Skipping others. */
0407         if (pdo_apdo_type(pdo) > APDO_TYPE_PPS) {
0408             dev_warn(&cap->dev, "Unknown APDO type. PDO 0x%08x\n", pdo);
0409             kfree(p);
0410             return 0;
0411         }
0412 
0413         if (is_source(cap->role))
0414             type = source_apdo_type[pdo_apdo_type(pdo)];
0415         else
0416             type = sink_apdo_type[pdo_apdo_type(pdo)];
0417 
0418         name = apdo_supply_name[pdo_apdo_type(pdo)];
0419     } else {
0420         if (is_source(cap->role))
0421             type = source_type[pdo_type(pdo)];
0422         else
0423             type = sink_type[pdo_type(pdo)];
0424 
0425         name = supply_name[pdo_type(pdo)];
0426     }
0427 
0428     p->dev.parent = &cap->dev;
0429     p->dev.type = type;
0430     dev_set_name(&p->dev, "%u:%s", position + 1, name);
0431 
0432     ret = device_register(&p->dev);
0433     if (ret) {
0434         put_device(&p->dev);
0435         return ret;
0436     }
0437 
0438     return 0;
0439 }
0440 
0441 static int remove_pdo(struct device *dev, void *data)
0442 {
0443     device_unregister(dev);
0444     return 0;
0445 }
0446 
0447 /* -------------------------------------------------------------------------- */
0448 
0449 static const char * const cap_name[] = {
0450     [TYPEC_SINK]    = "sink-capabilities",
0451     [TYPEC_SOURCE]  = "source-capabilities",
0452 };
0453 
0454 static void pd_capabilities_release(struct device *dev)
0455 {
0456     kfree(to_usb_power_delivery_capabilities(dev));
0457 }
0458 
0459 static struct device_type pd_capabilities_type = {
0460     .name = "capabilities",
0461     .release = pd_capabilities_release,
0462 };
0463 
0464 /**
0465  * usb_power_delivery_register_capabilities - Register a set of capabilities.
0466  * @pd: The USB PD instance that the capabilities belong to.
0467  * @desc: Description of the Capablities Message.
0468  *
0469  * This function registers a Capabilities Message described in @desc. The
0470  * capabilities will have their own sub-directory under @pd in sysfs.
0471  *
0472  * The function returns pointer to struct usb_power_delivery_capabilities, or
0473  * ERR_PRT(errno).
0474  */
0475 struct usb_power_delivery_capabilities *
0476 usb_power_delivery_register_capabilities(struct usb_power_delivery *pd,
0477                      struct usb_power_delivery_capabilities_desc *desc)
0478 {
0479     struct usb_power_delivery_capabilities *cap;
0480     int ret;
0481     int i;
0482 
0483     cap = kzalloc(sizeof(*cap), GFP_KERNEL);
0484     if (!cap)
0485         return ERR_PTR(-ENOMEM);
0486 
0487     cap->pd = pd;
0488     cap->role = desc->role;
0489 
0490     cap->dev.parent = &pd->dev;
0491     cap->dev.type = &pd_capabilities_type;
0492     dev_set_name(&cap->dev, "%s", cap_name[cap->role]);
0493 
0494     ret = device_register(&cap->dev);
0495     if (ret) {
0496         put_device(&cap->dev);
0497         return ERR_PTR(ret);
0498     }
0499 
0500     for (i = 0; i < PDO_MAX_OBJECTS && desc->pdo[i]; i++) {
0501         ret = add_pdo(cap, desc->pdo[i], i);
0502         if (ret) {
0503             usb_power_delivery_unregister_capabilities(cap);
0504             return ERR_PTR(ret);
0505         }
0506     }
0507 
0508     return cap;
0509 }
0510 EXPORT_SYMBOL_GPL(usb_power_delivery_register_capabilities);
0511 
0512 /**
0513  * usb_power_delivery_unregister_capabilities - Unregister a set of capabilities
0514  * @cap: The capabilities
0515  */
0516 void usb_power_delivery_unregister_capabilities(struct usb_power_delivery_capabilities *cap)
0517 {
0518     if (!cap)
0519         return;
0520 
0521     device_for_each_child(&cap->dev, NULL, remove_pdo);
0522     device_unregister(&cap->dev);
0523 }
0524 EXPORT_SYMBOL_GPL(usb_power_delivery_unregister_capabilities);
0525 
0526 /* -------------------------------------------------------------------------- */
0527 
0528 static ssize_t revision_show(struct device *dev, struct device_attribute *attr, char *buf)
0529 {
0530     struct usb_power_delivery *pd = to_usb_power_delivery(dev);
0531 
0532     return sysfs_emit(buf, "%u.%u\n", (pd->revision >> 8) & 0xff, (pd->revision >> 4) & 0xf);
0533 }
0534 static DEVICE_ATTR_RO(revision);
0535 
0536 static ssize_t version_show(struct device *dev, struct device_attribute *attr, char *buf)
0537 {
0538     struct usb_power_delivery *pd = to_usb_power_delivery(dev);
0539 
0540     return sysfs_emit(buf, "%u.%u\n", (pd->version >> 8) & 0xff, (pd->version >> 4) & 0xf);
0541 }
0542 static DEVICE_ATTR_RO(version);
0543 
0544 static struct attribute *pd_attrs[] = {
0545     &dev_attr_revision.attr,
0546     &dev_attr_version.attr,
0547     NULL
0548 };
0549 
0550 static umode_t pd_attr_is_visible(struct kobject *kobj, struct attribute *attr, int n)
0551 {
0552     struct usb_power_delivery *pd = to_usb_power_delivery(kobj_to_dev(kobj));
0553 
0554     if (attr == &dev_attr_version.attr && !pd->version)
0555         return 0;
0556 
0557     return attr->mode;
0558 }
0559 
0560 static const struct attribute_group pd_group = {
0561     .is_visible = pd_attr_is_visible,
0562     .attrs = pd_attrs,
0563 };
0564 __ATTRIBUTE_GROUPS(pd);
0565 
0566 static void pd_release(struct device *dev)
0567 {
0568     struct usb_power_delivery *pd = to_usb_power_delivery(dev);
0569 
0570     ida_simple_remove(&pd_ida, pd->id);
0571     kfree(pd);
0572 }
0573 
0574 static struct device_type pd_type = {
0575     .name = "usb_power_delivery",
0576     .release = pd_release,
0577     .groups = pd_groups,
0578 };
0579 
0580 struct usb_power_delivery *usb_power_delivery_find(const char *name)
0581 {
0582     struct device *dev;
0583 
0584     dev = class_find_device_by_name(&pd_class, name);
0585 
0586     return dev ? to_usb_power_delivery(dev) : NULL;
0587 }
0588 
0589 /**
0590  * usb_power_delivery_register - Register USB Power Delivery Support.
0591  * @parent: Parent device.
0592  * @desc: Description of the USB PD contract.
0593  *
0594  * This routine can be used to register USB Power Delivery capabilities that a
0595  * device or devices can support. These capabilities represent all the
0596  * capabilities that can be negotiated with a partner, so not only the Power
0597  * Capabilities that are negotiated using the USB PD Capabilities Message.
0598  *
0599  * The USB Power Delivery Support object that this routine generates can be used
0600  * as the parent object for all the actual USB Power Delivery Messages and
0601  * objects that can be negotiated with the partner.
0602  *
0603  * Returns handle to struct usb_power_delivery or ERR_PTR.
0604  */
0605 struct usb_power_delivery *
0606 usb_power_delivery_register(struct device *parent, struct usb_power_delivery_desc *desc)
0607 {
0608     struct usb_power_delivery *pd;
0609     int ret;
0610 
0611     pd = kzalloc(sizeof(*pd), GFP_KERNEL);
0612     if (!pd)
0613         return ERR_PTR(-ENOMEM);
0614 
0615     ret = ida_simple_get(&pd_ida, 0, 0, GFP_KERNEL);
0616     if (ret < 0) {
0617         kfree(pd);
0618         return ERR_PTR(ret);
0619     }
0620 
0621     pd->id = ret;
0622     pd->revision = desc->revision;
0623     pd->version = desc->version;
0624 
0625     pd->dev.parent = parent;
0626     pd->dev.type = &pd_type;
0627     pd->dev.class = &pd_class;
0628     dev_set_name(&pd->dev, "pd%d", pd->id);
0629 
0630     ret = device_register(&pd->dev);
0631     if (ret) {
0632         put_device(&pd->dev);
0633         return ERR_PTR(ret);
0634     }
0635 
0636     return pd;
0637 }
0638 EXPORT_SYMBOL_GPL(usb_power_delivery_register);
0639 
0640 /**
0641  * usb_power_delivery_unregister - Unregister USB Power Delivery Support.
0642  * @pd: The USB PD contract.
0643  */
0644 void usb_power_delivery_unregister(struct usb_power_delivery *pd)
0645 {
0646     if (IS_ERR_OR_NULL(pd))
0647         return;
0648 
0649     device_unregister(&pd->dev);
0650 }
0651 EXPORT_SYMBOL_GPL(usb_power_delivery_unregister);
0652 
0653 /**
0654  * usb_power_delivery_link_device - Link device to its USB PD object.
0655  * @pd: The USB PD instance.
0656  * @dev: The device.
0657  *
0658  * This function can be used to create a symlink named "usb_power_delivery" for
0659  * @dev that points to @pd.
0660  */
0661 int usb_power_delivery_link_device(struct usb_power_delivery *pd, struct device *dev)
0662 {
0663     int ret;
0664 
0665     if (IS_ERR_OR_NULL(pd) || !dev)
0666         return 0;
0667 
0668     ret = sysfs_create_link(&dev->kobj, &pd->dev.kobj, "usb_power_delivery");
0669     if (ret)
0670         return ret;
0671 
0672     get_device(&pd->dev);
0673     get_device(dev);
0674 
0675     return 0;
0676 }
0677 EXPORT_SYMBOL_GPL(usb_power_delivery_link_device);
0678 
0679 /**
0680  * usb_power_delivery_unlink_device - Unlink device from its USB PD object.
0681  * @pd: The USB PD instance.
0682  * @dev: The device.
0683  *
0684  * Remove the symlink that was previously created with pd_link_device().
0685  */
0686 void usb_power_delivery_unlink_device(struct usb_power_delivery *pd, struct device *dev)
0687 {
0688     if (IS_ERR_OR_NULL(pd) || !dev)
0689         return;
0690 
0691     sysfs_remove_link(&dev->kobj, "usb_power_delivery");
0692     put_device(&pd->dev);
0693     put_device(dev);
0694 }
0695 EXPORT_SYMBOL_GPL(usb_power_delivery_unlink_device);
0696 
0697 /* -------------------------------------------------------------------------- */
0698 
0699 int __init usb_power_delivery_init(void)
0700 {
0701     return class_register(&pd_class);
0702 }
0703 
0704 void __exit usb_power_delivery_exit(void)
0705 {
0706     ida_destroy(&pd_ida);
0707     class_unregister(&pd_class);
0708 }