1*8465def4SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2*8465def4SGreg Kroah-Hartman /*
3*8465def4SGreg Kroah-Hartman * Greybus bundles
4*8465def4SGreg Kroah-Hartman *
5*8465def4SGreg Kroah-Hartman * Copyright 2014-2015 Google Inc.
6*8465def4SGreg Kroah-Hartman * Copyright 2014-2015 Linaro Ltd.
7*8465def4SGreg Kroah-Hartman */
8*8465def4SGreg Kroah-Hartman
9*8465def4SGreg Kroah-Hartman #include <linux/greybus.h>
10*8465def4SGreg Kroah-Hartman #include "greybus_trace.h"
11*8465def4SGreg Kroah-Hartman
bundle_class_show(struct device * dev,struct device_attribute * attr,char * buf)12*8465def4SGreg Kroah-Hartman static ssize_t bundle_class_show(struct device *dev,
13*8465def4SGreg Kroah-Hartman struct device_attribute *attr, char *buf)
14*8465def4SGreg Kroah-Hartman {
15*8465def4SGreg Kroah-Hartman struct gb_bundle *bundle = to_gb_bundle(dev);
16*8465def4SGreg Kroah-Hartman
17*8465def4SGreg Kroah-Hartman return sprintf(buf, "0x%02x\n", bundle->class);
18*8465def4SGreg Kroah-Hartman }
19*8465def4SGreg Kroah-Hartman static DEVICE_ATTR_RO(bundle_class);
20*8465def4SGreg Kroah-Hartman
bundle_id_show(struct device * dev,struct device_attribute * attr,char * buf)21*8465def4SGreg Kroah-Hartman static ssize_t bundle_id_show(struct device *dev,
22*8465def4SGreg Kroah-Hartman struct device_attribute *attr, char *buf)
23*8465def4SGreg Kroah-Hartman {
24*8465def4SGreg Kroah-Hartman struct gb_bundle *bundle = to_gb_bundle(dev);
25*8465def4SGreg Kroah-Hartman
26*8465def4SGreg Kroah-Hartman return sprintf(buf, "%u\n", bundle->id);
27*8465def4SGreg Kroah-Hartman }
28*8465def4SGreg Kroah-Hartman static DEVICE_ATTR_RO(bundle_id);
29*8465def4SGreg Kroah-Hartman
state_show(struct device * dev,struct device_attribute * attr,char * buf)30*8465def4SGreg Kroah-Hartman static ssize_t state_show(struct device *dev, struct device_attribute *attr,
31*8465def4SGreg Kroah-Hartman char *buf)
32*8465def4SGreg Kroah-Hartman {
33*8465def4SGreg Kroah-Hartman struct gb_bundle *bundle = to_gb_bundle(dev);
34*8465def4SGreg Kroah-Hartman
35*8465def4SGreg Kroah-Hartman if (!bundle->state)
36*8465def4SGreg Kroah-Hartman return sprintf(buf, "\n");
37*8465def4SGreg Kroah-Hartman
38*8465def4SGreg Kroah-Hartman return sprintf(buf, "%s\n", bundle->state);
39*8465def4SGreg Kroah-Hartman }
40*8465def4SGreg Kroah-Hartman
state_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)41*8465def4SGreg Kroah-Hartman static ssize_t state_store(struct device *dev, struct device_attribute *attr,
42*8465def4SGreg Kroah-Hartman const char *buf, size_t size)
43*8465def4SGreg Kroah-Hartman {
44*8465def4SGreg Kroah-Hartman struct gb_bundle *bundle = to_gb_bundle(dev);
45*8465def4SGreg Kroah-Hartman
46*8465def4SGreg Kroah-Hartman kfree(bundle->state);
47*8465def4SGreg Kroah-Hartman bundle->state = kstrdup(buf, GFP_KERNEL);
48*8465def4SGreg Kroah-Hartman if (!bundle->state)
49*8465def4SGreg Kroah-Hartman return -ENOMEM;
50*8465def4SGreg Kroah-Hartman
51*8465def4SGreg Kroah-Hartman /* Tell userspace that the file contents changed */
52*8465def4SGreg Kroah-Hartman sysfs_notify(&bundle->dev.kobj, NULL, "state");
53*8465def4SGreg Kroah-Hartman
54*8465def4SGreg Kroah-Hartman return size;
55*8465def4SGreg Kroah-Hartman }
56*8465def4SGreg Kroah-Hartman static DEVICE_ATTR_RW(state);
57*8465def4SGreg Kroah-Hartman
58*8465def4SGreg Kroah-Hartman static struct attribute *bundle_attrs[] = {
59*8465def4SGreg Kroah-Hartman &dev_attr_bundle_class.attr,
60*8465def4SGreg Kroah-Hartman &dev_attr_bundle_id.attr,
61*8465def4SGreg Kroah-Hartman &dev_attr_state.attr,
62*8465def4SGreg Kroah-Hartman NULL,
63*8465def4SGreg Kroah-Hartman };
64*8465def4SGreg Kroah-Hartman
65*8465def4SGreg Kroah-Hartman ATTRIBUTE_GROUPS(bundle);
66*8465def4SGreg Kroah-Hartman
gb_bundle_find(struct gb_interface * intf,u8 bundle_id)67*8465def4SGreg Kroah-Hartman static struct gb_bundle *gb_bundle_find(struct gb_interface *intf,
68*8465def4SGreg Kroah-Hartman u8 bundle_id)
69*8465def4SGreg Kroah-Hartman {
70*8465def4SGreg Kroah-Hartman struct gb_bundle *bundle;
71*8465def4SGreg Kroah-Hartman
72*8465def4SGreg Kroah-Hartman list_for_each_entry(bundle, &intf->bundles, links) {
73*8465def4SGreg Kroah-Hartman if (bundle->id == bundle_id)
74*8465def4SGreg Kroah-Hartman return bundle;
75*8465def4SGreg Kroah-Hartman }
76*8465def4SGreg Kroah-Hartman
77*8465def4SGreg Kroah-Hartman return NULL;
78*8465def4SGreg Kroah-Hartman }
79*8465def4SGreg Kroah-Hartman
gb_bundle_release(struct device * dev)80*8465def4SGreg Kroah-Hartman static void gb_bundle_release(struct device *dev)
81*8465def4SGreg Kroah-Hartman {
82*8465def4SGreg Kroah-Hartman struct gb_bundle *bundle = to_gb_bundle(dev);
83*8465def4SGreg Kroah-Hartman
84*8465def4SGreg Kroah-Hartman trace_gb_bundle_release(bundle);
85*8465def4SGreg Kroah-Hartman
86*8465def4SGreg Kroah-Hartman kfree(bundle->state);
87*8465def4SGreg Kroah-Hartman kfree(bundle->cport_desc);
88*8465def4SGreg Kroah-Hartman kfree(bundle);
89*8465def4SGreg Kroah-Hartman }
90*8465def4SGreg Kroah-Hartman
91*8465def4SGreg Kroah-Hartman #ifdef CONFIG_PM
gb_bundle_disable_all_connections(struct gb_bundle * bundle)92*8465def4SGreg Kroah-Hartman static void gb_bundle_disable_all_connections(struct gb_bundle *bundle)
93*8465def4SGreg Kroah-Hartman {
94*8465def4SGreg Kroah-Hartman struct gb_connection *connection;
95*8465def4SGreg Kroah-Hartman
96*8465def4SGreg Kroah-Hartman list_for_each_entry(connection, &bundle->connections, bundle_links)
97*8465def4SGreg Kroah-Hartman gb_connection_disable(connection);
98*8465def4SGreg Kroah-Hartman }
99*8465def4SGreg Kroah-Hartman
gb_bundle_enable_all_connections(struct gb_bundle * bundle)100*8465def4SGreg Kroah-Hartman static void gb_bundle_enable_all_connections(struct gb_bundle *bundle)
101*8465def4SGreg Kroah-Hartman {
102*8465def4SGreg Kroah-Hartman struct gb_connection *connection;
103*8465def4SGreg Kroah-Hartman
104*8465def4SGreg Kroah-Hartman list_for_each_entry(connection, &bundle->connections, bundle_links)
105*8465def4SGreg Kroah-Hartman gb_connection_enable(connection);
106*8465def4SGreg Kroah-Hartman }
107*8465def4SGreg Kroah-Hartman
gb_bundle_suspend(struct device * dev)108*8465def4SGreg Kroah-Hartman static int gb_bundle_suspend(struct device *dev)
109*8465def4SGreg Kroah-Hartman {
110*8465def4SGreg Kroah-Hartman struct gb_bundle *bundle = to_gb_bundle(dev);
111*8465def4SGreg Kroah-Hartman const struct dev_pm_ops *pm = dev->driver->pm;
112*8465def4SGreg Kroah-Hartman int ret;
113*8465def4SGreg Kroah-Hartman
114*8465def4SGreg Kroah-Hartman if (pm && pm->runtime_suspend) {
115*8465def4SGreg Kroah-Hartman ret = pm->runtime_suspend(&bundle->dev);
116*8465def4SGreg Kroah-Hartman if (ret)
117*8465def4SGreg Kroah-Hartman return ret;
118*8465def4SGreg Kroah-Hartman } else {
119*8465def4SGreg Kroah-Hartman gb_bundle_disable_all_connections(bundle);
120*8465def4SGreg Kroah-Hartman }
121*8465def4SGreg Kroah-Hartman
122*8465def4SGreg Kroah-Hartman ret = gb_control_bundle_suspend(bundle->intf->control, bundle->id);
123*8465def4SGreg Kroah-Hartman if (ret) {
124*8465def4SGreg Kroah-Hartman if (pm && pm->runtime_resume)
125*8465def4SGreg Kroah-Hartman ret = pm->runtime_resume(dev);
126*8465def4SGreg Kroah-Hartman else
127*8465def4SGreg Kroah-Hartman gb_bundle_enable_all_connections(bundle);
128*8465def4SGreg Kroah-Hartman
129*8465def4SGreg Kroah-Hartman return ret;
130*8465def4SGreg Kroah-Hartman }
131*8465def4SGreg Kroah-Hartman
132*8465def4SGreg Kroah-Hartman return 0;
133*8465def4SGreg Kroah-Hartman }
134*8465def4SGreg Kroah-Hartman
gb_bundle_resume(struct device * dev)135*8465def4SGreg Kroah-Hartman static int gb_bundle_resume(struct device *dev)
136*8465def4SGreg Kroah-Hartman {
137*8465def4SGreg Kroah-Hartman struct gb_bundle *bundle = to_gb_bundle(dev);
138*8465def4SGreg Kroah-Hartman const struct dev_pm_ops *pm = dev->driver->pm;
139*8465def4SGreg Kroah-Hartman int ret;
140*8465def4SGreg Kroah-Hartman
141*8465def4SGreg Kroah-Hartman ret = gb_control_bundle_resume(bundle->intf->control, bundle->id);
142*8465def4SGreg Kroah-Hartman if (ret)
143*8465def4SGreg Kroah-Hartman return ret;
144*8465def4SGreg Kroah-Hartman
145*8465def4SGreg Kroah-Hartman if (pm && pm->runtime_resume) {
146*8465def4SGreg Kroah-Hartman ret = pm->runtime_resume(dev);
147*8465def4SGreg Kroah-Hartman if (ret)
148*8465def4SGreg Kroah-Hartman return ret;
149*8465def4SGreg Kroah-Hartman } else {
150*8465def4SGreg Kroah-Hartman gb_bundle_enable_all_connections(bundle);
151*8465def4SGreg Kroah-Hartman }
152*8465def4SGreg Kroah-Hartman
153*8465def4SGreg Kroah-Hartman return 0;
154*8465def4SGreg Kroah-Hartman }
155*8465def4SGreg Kroah-Hartman
gb_bundle_idle(struct device * dev)156*8465def4SGreg Kroah-Hartman static int gb_bundle_idle(struct device *dev)
157*8465def4SGreg Kroah-Hartman {
158*8465def4SGreg Kroah-Hartman pm_runtime_mark_last_busy(dev);
159*8465def4SGreg Kroah-Hartman pm_request_autosuspend(dev);
160*8465def4SGreg Kroah-Hartman
161*8465def4SGreg Kroah-Hartman return 0;
162*8465def4SGreg Kroah-Hartman }
163*8465def4SGreg Kroah-Hartman #endif
164*8465def4SGreg Kroah-Hartman
165*8465def4SGreg Kroah-Hartman static const struct dev_pm_ops gb_bundle_pm_ops = {
166*8465def4SGreg Kroah-Hartman SET_RUNTIME_PM_OPS(gb_bundle_suspend, gb_bundle_resume, gb_bundle_idle)
167*8465def4SGreg Kroah-Hartman };
168*8465def4SGreg Kroah-Hartman
169*8465def4SGreg Kroah-Hartman struct device_type greybus_bundle_type = {
170*8465def4SGreg Kroah-Hartman .name = "greybus_bundle",
171*8465def4SGreg Kroah-Hartman .release = gb_bundle_release,
172*8465def4SGreg Kroah-Hartman .pm = &gb_bundle_pm_ops,
173*8465def4SGreg Kroah-Hartman };
174*8465def4SGreg Kroah-Hartman
175*8465def4SGreg Kroah-Hartman /*
176*8465def4SGreg Kroah-Hartman * Create a gb_bundle structure to represent a discovered
177*8465def4SGreg Kroah-Hartman * bundle. Returns a pointer to the new bundle or a null
178*8465def4SGreg Kroah-Hartman * pointer if a failure occurs due to memory exhaustion.
179*8465def4SGreg Kroah-Hartman */
gb_bundle_create(struct gb_interface * intf,u8 bundle_id,u8 class)180*8465def4SGreg Kroah-Hartman struct gb_bundle *gb_bundle_create(struct gb_interface *intf, u8 bundle_id,
181*8465def4SGreg Kroah-Hartman u8 class)
182*8465def4SGreg Kroah-Hartman {
183*8465def4SGreg Kroah-Hartman struct gb_bundle *bundle;
184*8465def4SGreg Kroah-Hartman
185*8465def4SGreg Kroah-Hartman if (bundle_id == BUNDLE_ID_NONE) {
186*8465def4SGreg Kroah-Hartman dev_err(&intf->dev, "can't use bundle id %u\n", bundle_id);
187*8465def4SGreg Kroah-Hartman return NULL;
188*8465def4SGreg Kroah-Hartman }
189*8465def4SGreg Kroah-Hartman
190*8465def4SGreg Kroah-Hartman /*
191*8465def4SGreg Kroah-Hartman * Reject any attempt to reuse a bundle id. We initialize
192*8465def4SGreg Kroah-Hartman * these serially, so there's no need to worry about keeping
193*8465def4SGreg Kroah-Hartman * the interface bundle list locked here.
194*8465def4SGreg Kroah-Hartman */
195*8465def4SGreg Kroah-Hartman if (gb_bundle_find(intf, bundle_id)) {
196*8465def4SGreg Kroah-Hartman dev_err(&intf->dev, "duplicate bundle id %u\n", bundle_id);
197*8465def4SGreg Kroah-Hartman return NULL;
198*8465def4SGreg Kroah-Hartman }
199*8465def4SGreg Kroah-Hartman
200*8465def4SGreg Kroah-Hartman bundle = kzalloc(sizeof(*bundle), GFP_KERNEL);
201*8465def4SGreg Kroah-Hartman if (!bundle)
202*8465def4SGreg Kroah-Hartman return NULL;
203*8465def4SGreg Kroah-Hartman
204*8465def4SGreg Kroah-Hartman bundle->intf = intf;
205*8465def4SGreg Kroah-Hartman bundle->id = bundle_id;
206*8465def4SGreg Kroah-Hartman bundle->class = class;
207*8465def4SGreg Kroah-Hartman INIT_LIST_HEAD(&bundle->connections);
208*8465def4SGreg Kroah-Hartman
209*8465def4SGreg Kroah-Hartman bundle->dev.parent = &intf->dev;
210*8465def4SGreg Kroah-Hartman bundle->dev.bus = &greybus_bus_type;
211*8465def4SGreg Kroah-Hartman bundle->dev.type = &greybus_bundle_type;
212*8465def4SGreg Kroah-Hartman bundle->dev.groups = bundle_groups;
213*8465def4SGreg Kroah-Hartman bundle->dev.dma_mask = intf->dev.dma_mask;
214*8465def4SGreg Kroah-Hartman device_initialize(&bundle->dev);
215*8465def4SGreg Kroah-Hartman dev_set_name(&bundle->dev, "%s.%d", dev_name(&intf->dev), bundle_id);
216*8465def4SGreg Kroah-Hartman
217*8465def4SGreg Kroah-Hartman list_add(&bundle->links, &intf->bundles);
218*8465def4SGreg Kroah-Hartman
219*8465def4SGreg Kroah-Hartman trace_gb_bundle_create(bundle);
220*8465def4SGreg Kroah-Hartman
221*8465def4SGreg Kroah-Hartman return bundle;
222*8465def4SGreg Kroah-Hartman }
223*8465def4SGreg Kroah-Hartman
gb_bundle_add(struct gb_bundle * bundle)224*8465def4SGreg Kroah-Hartman int gb_bundle_add(struct gb_bundle *bundle)
225*8465def4SGreg Kroah-Hartman {
226*8465def4SGreg Kroah-Hartman int ret;
227*8465def4SGreg Kroah-Hartman
228*8465def4SGreg Kroah-Hartman ret = device_add(&bundle->dev);
229*8465def4SGreg Kroah-Hartman if (ret) {
230*8465def4SGreg Kroah-Hartman dev_err(&bundle->dev, "failed to register bundle: %d\n", ret);
231*8465def4SGreg Kroah-Hartman return ret;
232*8465def4SGreg Kroah-Hartman }
233*8465def4SGreg Kroah-Hartman
234*8465def4SGreg Kroah-Hartman trace_gb_bundle_add(bundle);
235*8465def4SGreg Kroah-Hartman
236*8465def4SGreg Kroah-Hartman return 0;
237*8465def4SGreg Kroah-Hartman }
238*8465def4SGreg Kroah-Hartman
239*8465def4SGreg Kroah-Hartman /*
240*8465def4SGreg Kroah-Hartman * Tear down a previously set up bundle.
241*8465def4SGreg Kroah-Hartman */
gb_bundle_destroy(struct gb_bundle * bundle)242*8465def4SGreg Kroah-Hartman void gb_bundle_destroy(struct gb_bundle *bundle)
243*8465def4SGreg Kroah-Hartman {
244*8465def4SGreg Kroah-Hartman trace_gb_bundle_destroy(bundle);
245*8465def4SGreg Kroah-Hartman
246*8465def4SGreg Kroah-Hartman if (device_is_registered(&bundle->dev))
247*8465def4SGreg Kroah-Hartman device_del(&bundle->dev);
248*8465def4SGreg Kroah-Hartman
249*8465def4SGreg Kroah-Hartman list_del(&bundle->links);
250*8465def4SGreg Kroah-Hartman
251*8465def4SGreg Kroah-Hartman put_device(&bundle->dev);
252*8465def4SGreg Kroah-Hartman }
253