Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / greybus / bundle.c
blob84660729538b95cb8624080490025a68cba4fa54
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Greybus bundles
5 * Copyright 2014-2015 Google Inc.
6 * Copyright 2014-2015 Linaro Ltd.
7 */
9 #include <linux/greybus.h>
10 #include "greybus_trace.h"
12 static ssize_t bundle_class_show(struct device *dev,
13 struct device_attribute *attr, char *buf)
15 struct gb_bundle *bundle = to_gb_bundle(dev);
17 return sprintf(buf, "0x%02x\n", bundle->class);
19 static DEVICE_ATTR_RO(bundle_class);
21 static ssize_t bundle_id_show(struct device *dev,
22 struct device_attribute *attr, char *buf)
24 struct gb_bundle *bundle = to_gb_bundle(dev);
26 return sprintf(buf, "%u\n", bundle->id);
28 static DEVICE_ATTR_RO(bundle_id);
30 static ssize_t state_show(struct device *dev, struct device_attribute *attr,
31 char *buf)
33 struct gb_bundle *bundle = to_gb_bundle(dev);
35 if (!bundle->state)
36 return sprintf(buf, "\n");
38 return sprintf(buf, "%s\n", bundle->state);
41 static ssize_t state_store(struct device *dev, struct device_attribute *attr,
42 const char *buf, size_t size)
44 struct gb_bundle *bundle = to_gb_bundle(dev);
46 kfree(bundle->state);
47 bundle->state = kstrdup(buf, GFP_KERNEL);
48 if (!bundle->state)
49 return -ENOMEM;
51 /* Tell userspace that the file contents changed */
52 sysfs_notify(&bundle->dev.kobj, NULL, "state");
54 return size;
56 static DEVICE_ATTR_RW(state);
58 static struct attribute *bundle_attrs[] = {
59 &dev_attr_bundle_class.attr,
60 &dev_attr_bundle_id.attr,
61 &dev_attr_state.attr,
62 NULL,
65 ATTRIBUTE_GROUPS(bundle);
67 static struct gb_bundle *gb_bundle_find(struct gb_interface *intf,
68 u8 bundle_id)
70 struct gb_bundle *bundle;
72 list_for_each_entry(bundle, &intf->bundles, links) {
73 if (bundle->id == bundle_id)
74 return bundle;
77 return NULL;
80 static void gb_bundle_release(struct device *dev)
82 struct gb_bundle *bundle = to_gb_bundle(dev);
84 trace_gb_bundle_release(bundle);
86 kfree(bundle->state);
87 kfree(bundle->cport_desc);
88 kfree(bundle);
91 #ifdef CONFIG_PM
92 static void gb_bundle_disable_all_connections(struct gb_bundle *bundle)
94 struct gb_connection *connection;
96 list_for_each_entry(connection, &bundle->connections, bundle_links)
97 gb_connection_disable(connection);
100 static void gb_bundle_enable_all_connections(struct gb_bundle *bundle)
102 struct gb_connection *connection;
104 list_for_each_entry(connection, &bundle->connections, bundle_links)
105 gb_connection_enable(connection);
108 static int gb_bundle_suspend(struct device *dev)
110 struct gb_bundle *bundle = to_gb_bundle(dev);
111 const struct dev_pm_ops *pm = dev->driver->pm;
112 int ret;
114 if (pm && pm->runtime_suspend) {
115 ret = pm->runtime_suspend(&bundle->dev);
116 if (ret)
117 return ret;
118 } else {
119 gb_bundle_disable_all_connections(bundle);
122 ret = gb_control_bundle_suspend(bundle->intf->control, bundle->id);
123 if (ret) {
124 if (pm && pm->runtime_resume)
125 ret = pm->runtime_resume(dev);
126 else
127 gb_bundle_enable_all_connections(bundle);
129 return ret;
132 return 0;
135 static int gb_bundle_resume(struct device *dev)
137 struct gb_bundle *bundle = to_gb_bundle(dev);
138 const struct dev_pm_ops *pm = dev->driver->pm;
139 int ret;
141 ret = gb_control_bundle_resume(bundle->intf->control, bundle->id);
142 if (ret)
143 return ret;
145 if (pm && pm->runtime_resume) {
146 ret = pm->runtime_resume(dev);
147 if (ret)
148 return ret;
149 } else {
150 gb_bundle_enable_all_connections(bundle);
153 return 0;
156 static int gb_bundle_idle(struct device *dev)
158 pm_runtime_mark_last_busy(dev);
159 pm_request_autosuspend(dev);
161 return 0;
163 #endif
165 static const struct dev_pm_ops gb_bundle_pm_ops = {
166 SET_RUNTIME_PM_OPS(gb_bundle_suspend, gb_bundle_resume, gb_bundle_idle)
169 struct device_type greybus_bundle_type = {
170 .name = "greybus_bundle",
171 .release = gb_bundle_release,
172 .pm = &gb_bundle_pm_ops,
176 * Create a gb_bundle structure to represent a discovered
177 * bundle. Returns a pointer to the new bundle or a null
178 * pointer if a failure occurs due to memory exhaustion.
180 struct gb_bundle *gb_bundle_create(struct gb_interface *intf, u8 bundle_id,
181 u8 class)
183 struct gb_bundle *bundle;
185 if (bundle_id == BUNDLE_ID_NONE) {
186 dev_err(&intf->dev, "can't use bundle id %u\n", bundle_id);
187 return NULL;
191 * Reject any attempt to reuse a bundle id. We initialize
192 * these serially, so there's no need to worry about keeping
193 * the interface bundle list locked here.
195 if (gb_bundle_find(intf, bundle_id)) {
196 dev_err(&intf->dev, "duplicate bundle id %u\n", bundle_id);
197 return NULL;
200 bundle = kzalloc(sizeof(*bundle), GFP_KERNEL);
201 if (!bundle)
202 return NULL;
204 bundle->intf = intf;
205 bundle->id = bundle_id;
206 bundle->class = class;
207 INIT_LIST_HEAD(&bundle->connections);
209 bundle->dev.parent = &intf->dev;
210 bundle->dev.bus = &greybus_bus_type;
211 bundle->dev.type = &greybus_bundle_type;
212 bundle->dev.groups = bundle_groups;
213 bundle->dev.dma_mask = intf->dev.dma_mask;
214 device_initialize(&bundle->dev);
215 dev_set_name(&bundle->dev, "%s.%d", dev_name(&intf->dev), bundle_id);
217 list_add(&bundle->links, &intf->bundles);
219 trace_gb_bundle_create(bundle);
221 return bundle;
224 int gb_bundle_add(struct gb_bundle *bundle)
226 int ret;
228 ret = device_add(&bundle->dev);
229 if (ret) {
230 dev_err(&bundle->dev, "failed to register bundle: %d\n", ret);
231 return ret;
234 trace_gb_bundle_add(bundle);
236 return 0;
240 * Tear down a previously set up bundle.
242 void gb_bundle_destroy(struct gb_bundle *bundle)
244 trace_gb_bundle_destroy(bundle);
246 if (device_is_registered(&bundle->dev))
247 device_del(&bundle->dev);
249 list_del(&bundle->links);
251 put_device(&bundle->dev);