4 * Copyright 2014-2015 Google Inc.
5 * Copyright 2014-2015 Linaro Ltd.
7 * Released under the GPLv2 only.
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,
33 struct gb_bundle *bundle = to_gb_bundle(dev);
35 if (bundle->state == NULL)
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);
47 bundle->state = kstrdup(buf, GFP_KERNEL);
51 /* Tell userspace that the file contents changed */
52 sysfs_notify(&bundle->dev.kobj, NULL, "state");
56 static DEVICE_ATTR_RW(state);
59 static struct attribute *bundle_attrs[] = {
60 &dev_attr_bundle_class.attr,
61 &dev_attr_bundle_id.attr,
66 ATTRIBUTE_GROUPS(bundle);
68 static struct gb_bundle *gb_bundle_find(struct gb_interface *intf,
71 struct gb_bundle *bundle;
73 list_for_each_entry(bundle, &intf->bundles, links) {
74 if (bundle->id == bundle_id)
81 static void gb_bundle_release(struct device *dev)
83 struct gb_bundle *bundle = to_gb_bundle(dev);
84 struct gb_connection *connection;
85 struct gb_connection *tmp;
87 list_for_each_entry_safe(connection, tmp, &bundle->connections,
89 gb_connection_destroy(connection);
96 struct device_type greybus_bundle_type = {
97 .name = "greybus_bundle",
98 .release = gb_bundle_release,
102 * Create a gb_bundle structure to represent a discovered
103 * bundle. Returns a pointer to the new bundle or a null
104 * pointer if a failure occurs due to memory exhaustion.
106 struct gb_bundle *gb_bundle_create(struct gb_interface *intf, u8 bundle_id,
109 struct gb_bundle *bundle;
112 * Reject any attempt to reuse a bundle id. We initialize
113 * these serially, so there's no need to worry about keeping
114 * the interface bundle list locked here.
116 if (gb_bundle_find(intf, bundle_id)) {
117 dev_err(&intf->dev, "duplicate bundle id %u\n", bundle_id);
121 bundle = kzalloc(sizeof(*bundle), GFP_KERNEL);
126 bundle->id = bundle_id;
127 bundle->class = class;
128 INIT_LIST_HEAD(&bundle->connections);
130 bundle->dev.parent = &intf->dev;
131 bundle->dev.bus = &greybus_bus_type;
132 bundle->dev.type = &greybus_bundle_type;
133 bundle->dev.groups = bundle_groups;
134 device_initialize(&bundle->dev);
135 dev_set_name(&bundle->dev, "%s.%d", dev_name(&intf->dev), bundle_id);
137 list_add(&bundle->links, &intf->bundles);
142 int gb_bundle_add(struct gb_bundle *bundle)
146 ret = device_add(&bundle->dev);
148 dev_err(&bundle->dev, "failed to register bundle: %d\n", ret);
156 * Tear down a previously set up bundle.
158 void gb_bundle_destroy(struct gb_bundle *bundle)
160 if (device_is_registered(&bundle->dev))
161 device_del(&bundle->dev);
163 list_del(&bundle->links);
165 put_device(&bundle->dev);