1
2
3
4
5
6
7
8
9
10
11
12
13
14#include <linux/attribute_container.h>
15#include <linux/device.h>
16#include <linux/kernel.h>
17#include <linux/slab.h>
18#include <linux/list.h>
19#include <linux/module.h>
20#include <linux/mutex.h>
21
22#include "base.h"
23
24
25
26struct internal_container {
27 struct klist_node node;
28 struct attribute_container *cont;
29 struct device classdev;
30};
31
32static void internal_container_klist_get(struct klist_node *n)
33{
34 struct internal_container *ic =
35 container_of(n, struct internal_container, node);
36 get_device(&ic->classdev);
37}
38
39static void internal_container_klist_put(struct klist_node *n)
40{
41 struct internal_container *ic =
42 container_of(n, struct internal_container, node);
43 put_device(&ic->classdev);
44}
45
46
47
48
49
50
51
52
53
54struct attribute_container *
55attribute_container_classdev_to_container(struct device *classdev)
56{
57 struct internal_container *ic =
58 container_of(classdev, struct internal_container, classdev);
59 return ic->cont;
60}
61EXPORT_SYMBOL_GPL(attribute_container_classdev_to_container);
62
63static LIST_HEAD(attribute_container_list);
64
65static DEFINE_MUTEX(attribute_container_mutex);
66
67
68
69
70
71
72
73int
74attribute_container_register(struct attribute_container *cont)
75{
76 INIT_LIST_HEAD(&cont->node);
77 klist_init(&cont->containers, internal_container_klist_get,
78 internal_container_klist_put);
79
80 mutex_lock(&attribute_container_mutex);
81 list_add_tail(&cont->node, &attribute_container_list);
82 mutex_unlock(&attribute_container_mutex);
83
84 return 0;
85}
86EXPORT_SYMBOL_GPL(attribute_container_register);
87
88
89
90
91
92
93int
94attribute_container_unregister(struct attribute_container *cont)
95{
96 int retval = -EBUSY;
97
98 mutex_lock(&attribute_container_mutex);
99 spin_lock(&cont->containers.k_lock);
100 if (!list_empty(&cont->containers.k_list))
101 goto out;
102 retval = 0;
103 list_del(&cont->node);
104 out:
105 spin_unlock(&cont->containers.k_lock);
106 mutex_unlock(&attribute_container_mutex);
107 return retval;
108
109}
110EXPORT_SYMBOL_GPL(attribute_container_unregister);
111
112
113static void attribute_container_release(struct device *classdev)
114{
115 struct internal_container *ic
116 = container_of(classdev, struct internal_container, classdev);
117 struct device *dev = classdev->parent;
118
119 kfree(ic);
120 put_device(dev);
121}
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141void
142attribute_container_add_device(struct device *dev,
143 int (*fn)(struct attribute_container *,
144 struct device *,
145 struct device *))
146{
147 struct attribute_container *cont;
148
149 mutex_lock(&attribute_container_mutex);
150 list_for_each_entry(cont, &attribute_container_list, node) {
151 struct internal_container *ic;
152
153 if (attribute_container_no_classdevs(cont))
154 continue;
155
156 if (!cont->match(cont, dev))
157 continue;
158
159 ic = kzalloc(sizeof(*ic), GFP_KERNEL);
160 if (!ic) {
161 dev_err(dev, "failed to allocate class container\n");
162 continue;
163 }
164
165 ic->cont = cont;
166 device_initialize(&ic->classdev);
167 ic->classdev.parent = get_device(dev);
168 ic->classdev.class = cont->class;
169 cont->class->dev_release = attribute_container_release;
170 dev_set_name(&ic->classdev, "%s", dev_name(dev));
171 if (fn)
172 fn(cont, dev, &ic->classdev);
173 else
174 attribute_container_add_class_device(&ic->classdev);
175 klist_add_tail(&ic->node, &cont->containers);
176 }
177 mutex_unlock(&attribute_container_mutex);
178}
179
180
181
182
183#define klist_for_each_entry(pos, head, member, iter) \
184 for (klist_iter_init(head, iter); (pos = ({ \
185 struct klist_node *n = klist_next(iter); \
186 n ? container_of(n, typeof(*pos), member) : \
187 ({ klist_iter_exit(iter) ; NULL; }); \
188 })) != NULL;)
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206void
207attribute_container_remove_device(struct device *dev,
208 void (*fn)(struct attribute_container *,
209 struct device *,
210 struct device *))
211{
212 struct attribute_container *cont;
213
214 mutex_lock(&attribute_container_mutex);
215 list_for_each_entry(cont, &attribute_container_list, node) {
216 struct internal_container *ic;
217 struct klist_iter iter;
218
219 if (attribute_container_no_classdevs(cont))
220 continue;
221
222 if (!cont->match(cont, dev))
223 continue;
224
225 klist_for_each_entry(ic, &cont->containers, node, &iter) {
226 if (dev != ic->classdev.parent)
227 continue;
228 klist_del(&ic->node);
229 if (fn)
230 fn(cont, dev, &ic->classdev);
231 else {
232 attribute_container_remove_attrs(&ic->classdev);
233 device_unregister(&ic->classdev);
234 }
235 }
236 }
237 mutex_unlock(&attribute_container_mutex);
238}
239
240
241
242
243
244
245
246
247
248
249
250void
251attribute_container_device_trigger(struct device *dev,
252 int (*fn)(struct attribute_container *,
253 struct device *,
254 struct device *))
255{
256 struct attribute_container *cont;
257
258 mutex_lock(&attribute_container_mutex);
259 list_for_each_entry(cont, &attribute_container_list, node) {
260 struct internal_container *ic;
261 struct klist_iter iter;
262
263 if (!cont->match(cont, dev))
264 continue;
265
266 if (attribute_container_no_classdevs(cont)) {
267 fn(cont, dev, NULL);
268 continue;
269 }
270
271 klist_for_each_entry(ic, &cont->containers, node, &iter) {
272 if (dev == ic->classdev.parent)
273 fn(cont, dev, &ic->classdev);
274 }
275 }
276 mutex_unlock(&attribute_container_mutex);
277}
278
279
280
281
282
283
284
285
286
287
288
289
290
291void
292attribute_container_trigger(struct device *dev,
293 int (*fn)(struct attribute_container *,
294 struct device *))
295{
296 struct attribute_container *cont;
297
298 mutex_lock(&attribute_container_mutex);
299 list_for_each_entry(cont, &attribute_container_list, node) {
300 if (cont->match(cont, dev))
301 fn(cont, dev);
302 }
303 mutex_unlock(&attribute_container_mutex);
304}
305
306
307
308
309
310
311
312
313
314int
315attribute_container_add_attrs(struct device *classdev)
316{
317 struct attribute_container *cont =
318 attribute_container_classdev_to_container(classdev);
319 struct device_attribute **attrs = cont->attrs;
320 int i, error;
321
322 BUG_ON(attrs && cont->grp);
323
324 if (!attrs && !cont->grp)
325 return 0;
326
327 if (cont->grp)
328 return sysfs_create_group(&classdev->kobj, cont->grp);
329
330 for (i = 0; attrs[i]; i++) {
331 sysfs_attr_init(&attrs[i]->attr);
332 error = device_create_file(classdev, attrs[i]);
333 if (error)
334 return error;
335 }
336
337 return 0;
338}
339
340
341
342
343
344
345
346
347
348
349int
350attribute_container_add_class_device(struct device *classdev)
351{
352 int error = device_add(classdev);
353
354 if (error)
355 return error;
356 return attribute_container_add_attrs(classdev);
357}
358
359
360
361
362
363
364
365int
366attribute_container_add_class_device_adapter(struct attribute_container *cont,
367 struct device *dev,
368 struct device *classdev)
369{
370 return attribute_container_add_class_device(classdev);
371}
372
373
374
375
376
377
378
379void
380attribute_container_remove_attrs(struct device *classdev)
381{
382 struct attribute_container *cont =
383 attribute_container_classdev_to_container(classdev);
384 struct device_attribute **attrs = cont->attrs;
385 int i;
386
387 if (!attrs && !cont->grp)
388 return;
389
390 if (cont->grp) {
391 sysfs_remove_group(&classdev->kobj, cont->grp);
392 return ;
393 }
394
395 for (i = 0; attrs[i]; i++)
396 device_remove_file(classdev, attrs[i]);
397}
398
399
400
401
402
403
404
405
406
407void
408attribute_container_class_device_del(struct device *classdev)
409{
410 attribute_container_remove_attrs(classdev);
411 device_del(classdev);
412}
413
414
415
416
417
418
419
420
421
422
423struct device *
424attribute_container_find_class_device(struct attribute_container *cont,
425 struct device *dev)
426{
427 struct device *cdev = NULL;
428 struct internal_container *ic;
429 struct klist_iter iter;
430
431 klist_for_each_entry(ic, &cont->containers, node, &iter) {
432 if (ic->classdev.parent == dev) {
433 cdev = &ic->classdev;
434
435 klist_iter_exit(&iter);
436 break;
437 }
438 }
439
440 return cdev;
441}
442EXPORT_SYMBOL_GPL(attribute_container_find_class_device);
443