Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Greybus bundles
0004  *
0005  * Copyright 2014-2015 Google Inc.
0006  * Copyright 2014-2015 Linaro Ltd.
0007  */
0008 
0009 #include <linux/greybus.h>
0010 #include "greybus_trace.h"
0011 
0012 static ssize_t bundle_class_show(struct device *dev,
0013                  struct device_attribute *attr, char *buf)
0014 {
0015     struct gb_bundle *bundle = to_gb_bundle(dev);
0016 
0017     return sprintf(buf, "0x%02x\n", bundle->class);
0018 }
0019 static DEVICE_ATTR_RO(bundle_class);
0020 
0021 static ssize_t bundle_id_show(struct device *dev,
0022                   struct device_attribute *attr, char *buf)
0023 {
0024     struct gb_bundle *bundle = to_gb_bundle(dev);
0025 
0026     return sprintf(buf, "%u\n", bundle->id);
0027 }
0028 static DEVICE_ATTR_RO(bundle_id);
0029 
0030 static ssize_t state_show(struct device *dev, struct device_attribute *attr,
0031               char *buf)
0032 {
0033     struct gb_bundle *bundle = to_gb_bundle(dev);
0034 
0035     if (!bundle->state)
0036         return sprintf(buf, "\n");
0037 
0038     return sprintf(buf, "%s\n", bundle->state);
0039 }
0040 
0041 static ssize_t state_store(struct device *dev, struct device_attribute *attr,
0042                const char *buf, size_t size)
0043 {
0044     struct gb_bundle *bundle = to_gb_bundle(dev);
0045 
0046     kfree(bundle->state);
0047     bundle->state = kstrdup(buf, GFP_KERNEL);
0048     if (!bundle->state)
0049         return -ENOMEM;
0050 
0051     /* Tell userspace that the file contents changed */
0052     sysfs_notify(&bundle->dev.kobj, NULL, "state");
0053 
0054     return size;
0055 }
0056 static DEVICE_ATTR_RW(state);
0057 
0058 static struct attribute *bundle_attrs[] = {
0059     &dev_attr_bundle_class.attr,
0060     &dev_attr_bundle_id.attr,
0061     &dev_attr_state.attr,
0062     NULL,
0063 };
0064 
0065 ATTRIBUTE_GROUPS(bundle);
0066 
0067 static struct gb_bundle *gb_bundle_find(struct gb_interface *intf,
0068                     u8 bundle_id)
0069 {
0070     struct gb_bundle *bundle;
0071 
0072     list_for_each_entry(bundle, &intf->bundles, links) {
0073         if (bundle->id == bundle_id)
0074             return bundle;
0075     }
0076 
0077     return NULL;
0078 }
0079 
0080 static void gb_bundle_release(struct device *dev)
0081 {
0082     struct gb_bundle *bundle = to_gb_bundle(dev);
0083 
0084     trace_gb_bundle_release(bundle);
0085 
0086     kfree(bundle->state);
0087     kfree(bundle->cport_desc);
0088     kfree(bundle);
0089 }
0090 
0091 #ifdef CONFIG_PM
0092 static void gb_bundle_disable_all_connections(struct gb_bundle *bundle)
0093 {
0094     struct gb_connection *connection;
0095 
0096     list_for_each_entry(connection, &bundle->connections, bundle_links)
0097         gb_connection_disable(connection);
0098 }
0099 
0100 static void gb_bundle_enable_all_connections(struct gb_bundle *bundle)
0101 {
0102     struct gb_connection *connection;
0103 
0104     list_for_each_entry(connection, &bundle->connections, bundle_links)
0105         gb_connection_enable(connection);
0106 }
0107 
0108 static int gb_bundle_suspend(struct device *dev)
0109 {
0110     struct gb_bundle *bundle = to_gb_bundle(dev);
0111     const struct dev_pm_ops *pm = dev->driver->pm;
0112     int ret;
0113 
0114     if (pm && pm->runtime_suspend) {
0115         ret = pm->runtime_suspend(&bundle->dev);
0116         if (ret)
0117             return ret;
0118     } else {
0119         gb_bundle_disable_all_connections(bundle);
0120     }
0121 
0122     ret = gb_control_bundle_suspend(bundle->intf->control, bundle->id);
0123     if (ret) {
0124         if (pm && pm->runtime_resume)
0125             ret = pm->runtime_resume(dev);
0126         else
0127             gb_bundle_enable_all_connections(bundle);
0128 
0129         return ret;
0130     }
0131 
0132     return 0;
0133 }
0134 
0135 static int gb_bundle_resume(struct device *dev)
0136 {
0137     struct gb_bundle *bundle = to_gb_bundle(dev);
0138     const struct dev_pm_ops *pm = dev->driver->pm;
0139     int ret;
0140 
0141     ret = gb_control_bundle_resume(bundle->intf->control, bundle->id);
0142     if (ret)
0143         return ret;
0144 
0145     if (pm && pm->runtime_resume) {
0146         ret = pm->runtime_resume(dev);
0147         if (ret)
0148             return ret;
0149     } else {
0150         gb_bundle_enable_all_connections(bundle);
0151     }
0152 
0153     return 0;
0154 }
0155 
0156 static int gb_bundle_idle(struct device *dev)
0157 {
0158     pm_runtime_mark_last_busy(dev);
0159     pm_request_autosuspend(dev);
0160 
0161     return 0;
0162 }
0163 #endif
0164 
0165 static const struct dev_pm_ops gb_bundle_pm_ops = {
0166     SET_RUNTIME_PM_OPS(gb_bundle_suspend, gb_bundle_resume, gb_bundle_idle)
0167 };
0168 
0169 struct device_type greybus_bundle_type = {
0170     .name =     "greybus_bundle",
0171     .release =  gb_bundle_release,
0172     .pm =       &gb_bundle_pm_ops,
0173 };
0174 
0175 /*
0176  * Create a gb_bundle structure to represent a discovered
0177  * bundle.  Returns a pointer to the new bundle or a null
0178  * pointer if a failure occurs due to memory exhaustion.
0179  */
0180 struct gb_bundle *gb_bundle_create(struct gb_interface *intf, u8 bundle_id,
0181                    u8 class)
0182 {
0183     struct gb_bundle *bundle;
0184 
0185     if (bundle_id == BUNDLE_ID_NONE) {
0186         dev_err(&intf->dev, "can't use bundle id %u\n", bundle_id);
0187         return NULL;
0188     }
0189 
0190     /*
0191      * Reject any attempt to reuse a bundle id.  We initialize
0192      * these serially, so there's no need to worry about keeping
0193      * the interface bundle list locked here.
0194      */
0195     if (gb_bundle_find(intf, bundle_id)) {
0196         dev_err(&intf->dev, "duplicate bundle id %u\n", bundle_id);
0197         return NULL;
0198     }
0199 
0200     bundle = kzalloc(sizeof(*bundle), GFP_KERNEL);
0201     if (!bundle)
0202         return NULL;
0203 
0204     bundle->intf = intf;
0205     bundle->id = bundle_id;
0206     bundle->class = class;
0207     INIT_LIST_HEAD(&bundle->connections);
0208 
0209     bundle->dev.parent = &intf->dev;
0210     bundle->dev.bus = &greybus_bus_type;
0211     bundle->dev.type = &greybus_bundle_type;
0212     bundle->dev.groups = bundle_groups;
0213     bundle->dev.dma_mask = intf->dev.dma_mask;
0214     device_initialize(&bundle->dev);
0215     dev_set_name(&bundle->dev, "%s.%d", dev_name(&intf->dev), bundle_id);
0216 
0217     list_add(&bundle->links, &intf->bundles);
0218 
0219     trace_gb_bundle_create(bundle);
0220 
0221     return bundle;
0222 }
0223 
0224 int gb_bundle_add(struct gb_bundle *bundle)
0225 {
0226     int ret;
0227 
0228     ret = device_add(&bundle->dev);
0229     if (ret) {
0230         dev_err(&bundle->dev, "failed to register bundle: %d\n", ret);
0231         return ret;
0232     }
0233 
0234     trace_gb_bundle_add(bundle);
0235 
0236     return 0;
0237 }
0238 
0239 /*
0240  * Tear down a previously set up bundle.
0241  */
0242 void gb_bundle_destroy(struct gb_bundle *bundle)
0243 {
0244     trace_gb_bundle_destroy(bundle);
0245 
0246     if (device_is_registered(&bundle->dev))
0247         device_del(&bundle->dev);
0248 
0249     list_del(&bundle->links);
0250 
0251     put_device(&bundle->dev);
0252 }