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