greybus: connection: remove WARN_ON from destroy
[cascardo/linux.git] / drivers / staging / greybus / bundle.c
1 /*
2  * Greybus bundles
3  *
4  * Copyright 2014-2015 Google Inc.
5  * Copyright 2014-2015 Linaro Ltd.
6  *
7  * Released under the GPLv2 only.
8  */
9
10 #include "greybus.h"
11
12 static ssize_t bundle_class_show(struct device *dev,
13                                  struct device_attribute *attr, char *buf)
14 {
15         struct gb_bundle *bundle = to_gb_bundle(dev);
16
17         return sprintf(buf, "0x%02x\n", bundle->class);
18 }
19 static DEVICE_ATTR_RO(bundle_class);
20
21 static ssize_t bundle_id_show(struct device *dev,
22                               struct device_attribute *attr, char *buf)
23 {
24         struct gb_bundle *bundle = to_gb_bundle(dev);
25
26         return sprintf(buf, "%u\n", bundle->id);
27 }
28 static DEVICE_ATTR_RO(bundle_id);
29
30 static ssize_t state_show(struct device *dev, struct device_attribute *attr,
31                           char *buf)
32 {
33         struct gb_bundle *bundle = to_gb_bundle(dev);
34
35         if (bundle->state == NULL)
36                 return sprintf(buf, "\n");
37
38         return sprintf(buf, "%s\n", bundle->state);
39 }
40
41 static ssize_t state_store(struct device *dev, struct device_attribute *attr,
42                            const char *buf, size_t size)
43 {
44         struct gb_bundle *bundle = to_gb_bundle(dev);
45
46         kfree(bundle->state);
47         bundle->state = kstrdup(buf, GFP_KERNEL);
48         if (!bundle->state)
49                 return -ENOMEM;
50
51         /* Tell userspace that the file contents changed */
52         sysfs_notify(&bundle->dev.kobj, NULL, "state");
53
54         return size;
55 }
56 static DEVICE_ATTR_RW(state);
57
58
59 static struct attribute *bundle_attrs[] = {
60         &dev_attr_bundle_class.attr,
61         &dev_attr_bundle_id.attr,
62         &dev_attr_state.attr,
63         NULL,
64 };
65
66 ATTRIBUTE_GROUPS(bundle);
67
68 static struct gb_bundle *gb_bundle_find(struct gb_interface *intf,
69                                                         u8 bundle_id)
70 {
71         struct gb_bundle *bundle;
72
73         list_for_each_entry(bundle, &intf->bundles, links) {
74                 if (bundle->id == bundle_id)
75                         return bundle;
76         }
77
78         return NULL;
79 }
80
81 static void gb_bundle_release(struct device *dev)
82 {
83         struct gb_bundle *bundle = to_gb_bundle(dev);
84         struct gb_connection *connection;
85         struct gb_connection *tmp;
86
87         list_for_each_entry_safe(connection, tmp, &bundle->connections,
88                                                                 bundle_links) {
89                 gb_connection_destroy(connection);
90         }
91
92         kfree(bundle->state);
93         kfree(bundle);
94 }
95
96 struct device_type greybus_bundle_type = {
97         .name =         "greybus_bundle",
98         .release =      gb_bundle_release,
99 };
100
101 /*
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.
105  */
106 struct gb_bundle *gb_bundle_create(struct gb_interface *intf, u8 bundle_id,
107                                    u8 class)
108 {
109         struct gb_bundle *bundle;
110
111         /*
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.
115          */
116         if (gb_bundle_find(intf, bundle_id)) {
117                 dev_err(&intf->dev, "duplicate bundle id %u\n", bundle_id);
118                 return NULL;
119         }
120
121         bundle = kzalloc(sizeof(*bundle), GFP_KERNEL);
122         if (!bundle)
123                 return NULL;
124
125         bundle->intf = intf;
126         bundle->id = bundle_id;
127         bundle->class = class;
128         INIT_LIST_HEAD(&bundle->connections);
129
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);
136
137         list_add(&bundle->links, &intf->bundles);
138
139         return bundle;
140 }
141
142 int gb_bundle_add(struct gb_bundle *bundle)
143 {
144         int ret;
145
146         ret = device_add(&bundle->dev);
147         if (ret) {
148                 dev_err(&bundle->dev, "failed to register bundle: %d\n", ret);
149                 return ret;
150         }
151
152         return 0;
153 }
154
155 /*
156  * Tear down a previously set up bundle.
157  */
158 void gb_bundle_destroy(struct gb_bundle *bundle)
159 {
160         if (device_is_registered(&bundle->dev))
161                 device_del(&bundle->dev);
162
163         list_del(&bundle->links);
164
165         put_device(&bundle->dev);
166 }