software node: remove property_entry_read_uNN_array functions
[linux-2.6-block.git] / drivers / base / swnode.c
CommitLineData
59abd836
HK
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Software nodes for the firmware node framework.
4 *
5 * Copyright (C) 2018, Intel Corporation
6 * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
7 */
8
9#include <linux/device.h>
10#include <linux/kernel.h>
11#include <linux/property.h>
12#include <linux/slab.h>
13
80488a6b 14struct swnode {
59abd836
HK
15 int id;
16 struct kobject kobj;
17 struct fwnode_handle fwnode;
80488a6b 18 const struct software_node *node;
59abd836
HK
19
20 /* hierarchy */
21 struct ida child_ids;
22 struct list_head entry;
23 struct list_head children;
80488a6b 24 struct swnode *parent;
59abd836 25
80488a6b 26 unsigned int allocated:1;
59abd836
HK
27};
28
29static DEFINE_IDA(swnode_root_ids);
30static struct kset *swnode_kset;
31
80488a6b 32#define kobj_to_swnode(_kobj_) container_of(_kobj_, struct swnode, kobj)
59abd836
HK
33
34static const struct fwnode_operations software_node_ops;
35
36bool is_software_node(const struct fwnode_handle *fwnode)
37{
38 return !IS_ERR_OR_NULL(fwnode) && fwnode->ops == &software_node_ops;
39}
80488a6b 40EXPORT_SYMBOL_GPL(is_software_node);
59abd836 41
80488a6b 42#define to_swnode(__fwnode) \
59abd836 43 ({ \
80488a6b 44 typeof(__fwnode) __to_swnode_fwnode = __fwnode; \
59abd836 45 \
80488a6b
HK
46 is_software_node(__to_swnode_fwnode) ? \
47 container_of(__to_swnode_fwnode, \
48 struct swnode, fwnode) : NULL; \
59abd836
HK
49 })
50
80488a6b
HK
51static struct swnode *
52software_node_to_swnode(const struct software_node *node)
53{
61636873 54 struct swnode *swnode = NULL;
80488a6b
HK
55 struct kobject *k;
56
57 if (!node)
58 return NULL;
59
60 spin_lock(&swnode_kset->list_lock);
61
62 list_for_each_entry(k, &swnode_kset->list, entry) {
63 swnode = kobj_to_swnode(k);
64 if (swnode->node == node)
65 break;
66 swnode = NULL;
67 }
68
69 spin_unlock(&swnode_kset->list_lock);
70
71 return swnode;
72}
73
56c9aa07 74const struct software_node *to_software_node(const struct fwnode_handle *fwnode)
80488a6b 75{
56c9aa07 76 const struct swnode *swnode = to_swnode(fwnode);
80488a6b
HK
77
78 return swnode ? swnode->node : NULL;
79}
80EXPORT_SYMBOL_GPL(to_software_node);
81
82struct fwnode_handle *software_node_fwnode(const struct software_node *node)
83{
84 struct swnode *swnode = software_node_to_swnode(node);
85
86 return swnode ? &swnode->fwnode : NULL;
87}
88EXPORT_SYMBOL_GPL(software_node_fwnode);
89
59abd836
HK
90/* -------------------------------------------------------------------------- */
91/* property_entry processing */
92
93static const struct property_entry *
94property_entry_get(const struct property_entry *prop, const char *name)
95{
96 if (!prop)
97 return NULL;
98
99 for (; prop->name; prop++)
100 if (!strcmp(name, prop->name))
101 return prop;
102
103 return NULL;
104}
105
106static const void *property_get_pointer(const struct property_entry *prop)
107{
1f74d70f 108 if (!prop->length)
59abd836 109 return NULL;
1f74d70f
DT
110
111 if (prop->is_array)
112 return prop->pointer;
113
114 return &prop->value;
59abd836
HK
115}
116
117static const void *property_entry_find(const struct property_entry *props,
118 const char *propname, size_t length)
119{
120 const struct property_entry *prop;
121 const void *pointer;
122
123 prop = property_entry_get(props, propname);
124 if (!prop)
125 return ERR_PTR(-EINVAL);
126 pointer = property_get_pointer(prop);
127 if (!pointer)
128 return ERR_PTR(-ENODATA);
129 if (length > prop->length)
130 return ERR_PTR(-EOVERFLOW);
131 return pointer;
132}
133
59abd836
HK
134static int
135property_entry_count_elems_of_size(const struct property_entry *props,
136 const char *propname, size_t length)
137{
138 const struct property_entry *prop;
139
140 prop = property_entry_get(props, propname);
141 if (!prop)
142 return -EINVAL;
143
144 return prop->length / length;
145}
146
147static int property_entry_read_int_array(const struct property_entry *props,
148 const char *name,
149 unsigned int elem_size, void *val,
150 size_t nval)
151{
5236f5fe
DT
152 const void *pointer;
153 size_t length;
154
59abd836
HK
155 if (!val)
156 return property_entry_count_elems_of_size(props, name,
157 elem_size);
59abd836 158
5236f5fe
DT
159 if (!is_power_of_2(elem_size) || elem_size > sizeof(u64))
160 return -ENXIO;
161
162 length = nval * elem_size;
163
164 pointer = property_entry_find(props, name, length);
165 if (IS_ERR(pointer))
166 return PTR_ERR(pointer);
167
168 memcpy(val, pointer, length);
169 return 0;
59abd836
HK
170}
171
172static int property_entry_read_string_array(const struct property_entry *props,
173 const char *propname,
174 const char **strings, size_t nval)
175{
176 const struct property_entry *prop;
177 const void *pointer;
178 size_t array_len, length;
179
180 /* Find out the array length. */
181 prop = property_entry_get(props, propname);
182 if (!prop)
183 return -EINVAL;
184
185 if (prop->is_array)
186 /* Find the length of an array. */
187 array_len = property_entry_count_elems_of_size(props, propname,
188 sizeof(const char *));
189 else
190 /* The array length for a non-array string property is 1. */
191 array_len = 1;
192
193 /* Return how many there are if strings is NULL. */
194 if (!strings)
195 return array_len;
196
197 array_len = min(nval, array_len);
198 length = array_len * sizeof(*strings);
199
200 pointer = property_entry_find(props, propname, length);
201 if (IS_ERR(pointer))
202 return PTR_ERR(pointer);
203
204 memcpy(strings, pointer, length);
205
206 return array_len;
207}
208
ed1cdf31
HK
209static void property_entry_free_data(const struct property_entry *p)
210{
211 const void *pointer = property_get_pointer(p);
1f74d70f 212 const char * const *src_str;
ed1cdf31
HK
213 size_t i, nval;
214
215 if (p->is_array) {
1f74d70f
DT
216 if (p->type == DEV_PROP_STRING && p->pointer) {
217 src_str = p->pointer;
ed1cdf31
HK
218 nval = p->length / sizeof(const char *);
219 for (i = 0; i < nval; i++)
1f74d70f 220 kfree(src_str[i]);
ed1cdf31
HK
221 }
222 kfree(pointer);
223 } else if (p->type == DEV_PROP_STRING) {
224 kfree(p->value.str);
225 }
226 kfree(p->name);
227}
228
75dd63c9
DT
229static const char * const *
230property_copy_string_array(const struct property_entry *src)
ed1cdf31
HK
231{
232 const char **d;
1f74d70f 233 const char * const *src_str = src->pointer;
ed1cdf31
HK
234 size_t nval = src->length / sizeof(*d);
235 int i;
236
237 d = kcalloc(nval, sizeof(*d), GFP_KERNEL);
238 if (!d)
75dd63c9 239 return NULL;
ed1cdf31
HK
240
241 for (i = 0; i < nval; i++) {
1f74d70f
DT
242 d[i] = kstrdup(src_str[i], GFP_KERNEL);
243 if (!d[i] && src_str[i]) {
ed1cdf31
HK
244 while (--i >= 0)
245 kfree(d[i]);
246 kfree(d);
75dd63c9 247 return NULL;
ed1cdf31
HK
248 }
249 }
250
75dd63c9 251 return d;
ed1cdf31
HK
252}
253
254static int property_entry_copy_data(struct property_entry *dst,
255 const struct property_entry *src)
256{
257 const void *pointer = property_get_pointer(src);
258 const void *new;
ed1cdf31
HK
259
260 if (src->is_array) {
261 if (!src->length)
262 return -ENODATA;
263
264 if (src->type == DEV_PROP_STRING) {
75dd63c9
DT
265 new = property_copy_string_array(src);
266 if (!new)
267 return -ENOMEM;
ed1cdf31
HK
268 } else {
269 new = kmemdup(pointer, src->length, GFP_KERNEL);
270 if (!new)
271 return -ENOMEM;
272 }
1f74d70f
DT
273
274 dst->is_array = true;
275 dst->pointer = new;
ed1cdf31
HK
276 } else if (src->type == DEV_PROP_STRING) {
277 new = kstrdup(src->value.str, GFP_KERNEL);
278 if (!new && src->value.str)
279 return -ENOMEM;
1f74d70f
DT
280
281 dst->value.str = new;
ed1cdf31 282 } else {
1f74d70f 283 dst->value = src->value;
ed1cdf31
HK
284 }
285
286 dst->length = src->length;
ed1cdf31 287 dst->type = src->type;
ed1cdf31
HK
288 dst->name = kstrdup(src->name, GFP_KERNEL);
289 if (!dst->name)
290 goto out_free_data;
291
292 return 0;
293
294out_free_data:
295 property_entry_free_data(dst);
296 return -ENOMEM;
297}
298
299/**
300 * property_entries_dup - duplicate array of properties
301 * @properties: array of properties to copy
302 *
303 * This function creates a deep copy of the given NULL-terminated array
304 * of property entries.
305 */
306struct property_entry *
307property_entries_dup(const struct property_entry *properties)
308{
309 struct property_entry *p;
310 int i, n = 0;
311 int ret;
312
a7996986
HK
313 if (!properties)
314 return NULL;
315
ed1cdf31
HK
316 while (properties[n].name)
317 n++;
318
319 p = kcalloc(n + 1, sizeof(*p), GFP_KERNEL);
320 if (!p)
321 return ERR_PTR(-ENOMEM);
322
323 for (i = 0; i < n; i++) {
324 ret = property_entry_copy_data(&p[i], &properties[i]);
325 if (ret) {
326 while (--i >= 0)
327 property_entry_free_data(&p[i]);
328 kfree(p);
329 return ERR_PTR(ret);
330 }
331 }
332
333 return p;
334}
335EXPORT_SYMBOL_GPL(property_entries_dup);
336
337/**
338 * property_entries_free - free previously allocated array of properties
339 * @properties: array of properties to destroy
340 *
341 * This function frees given NULL-terminated array of property entries,
342 * along with their data.
343 */
344void property_entries_free(const struct property_entry *properties)
345{
346 const struct property_entry *p;
347
348 if (!properties)
349 return;
350
351 for (p = properties; p->name; p++)
352 property_entry_free_data(p);
353
354 kfree(properties);
355}
356EXPORT_SYMBOL_GPL(property_entries_free);
357
59abd836
HK
358/* -------------------------------------------------------------------------- */
359/* fwnode operations */
360
361static struct fwnode_handle *software_node_get(struct fwnode_handle *fwnode)
362{
80488a6b 363 struct swnode *swnode = to_swnode(fwnode);
59abd836
HK
364
365 kobject_get(&swnode->kobj);
366
367 return &swnode->fwnode;
368}
369
370static void software_node_put(struct fwnode_handle *fwnode)
371{
80488a6b 372 struct swnode *swnode = to_swnode(fwnode);
59abd836
HK
373
374 kobject_put(&swnode->kobj);
375}
376
377static bool software_node_property_present(const struct fwnode_handle *fwnode,
378 const char *propname)
379{
80488a6b
HK
380 struct swnode *swnode = to_swnode(fwnode);
381
382 return !!property_entry_get(swnode->node->properties, propname);
59abd836
HK
383}
384
385static int software_node_read_int_array(const struct fwnode_handle *fwnode,
386 const char *propname,
387 unsigned int elem_size, void *val,
388 size_t nval)
389{
80488a6b 390 struct swnode *swnode = to_swnode(fwnode);
59abd836 391
80488a6b 392 return property_entry_read_int_array(swnode->node->properties, propname,
59abd836
HK
393 elem_size, val, nval);
394}
395
396static int software_node_read_string_array(const struct fwnode_handle *fwnode,
397 const char *propname,
398 const char **val, size_t nval)
399{
80488a6b 400 struct swnode *swnode = to_swnode(fwnode);
59abd836 401
80488a6b
HK
402 return property_entry_read_string_array(swnode->node->properties,
403 propname, val, nval);
59abd836
HK
404}
405
bc0500c1
SA
406static const char *
407software_node_get_name(const struct fwnode_handle *fwnode)
408{
409 const struct swnode *swnode = to_swnode(fwnode);
410
411 if (!swnode)
412 return "(null)";
413
414 return kobject_name(&swnode->kobj);
415}
416
e7e242bc
SA
417static const char *
418software_node_get_name_prefix(const struct fwnode_handle *fwnode)
419{
420 struct fwnode_handle *parent;
421 const char *prefix;
422
423 parent = fwnode_get_parent(fwnode);
424 if (!parent)
425 return "";
426
427 /* Figure out the prefix from the parents. */
428 while (is_software_node(parent))
429 parent = fwnode_get_next_parent(parent);
430
431 prefix = fwnode_get_name_prefix(parent);
432 fwnode_handle_put(parent);
433
434 /* Guess something if prefix was NULL. */
435 return prefix ?: "/";
436}
437
0e3edd94 438static struct fwnode_handle *
59abd836
HK
439software_node_get_parent(const struct fwnode_handle *fwnode)
440{
80488a6b 441 struct swnode *swnode = to_swnode(fwnode);
59abd836 442
51c100a6
SA
443 if (!swnode || !swnode->parent)
444 return NULL;
445
446 return fwnode_handle_get(&swnode->parent->fwnode);
59abd836
HK
447}
448
0e3edd94 449static struct fwnode_handle *
59abd836
HK
450software_node_get_next_child(const struct fwnode_handle *fwnode,
451 struct fwnode_handle *child)
452{
80488a6b
HK
453 struct swnode *p = to_swnode(fwnode);
454 struct swnode *c = to_swnode(child);
59abd836 455
1d8f062e 456 if (!p || list_empty(&p->children) ||
59abd836
HK
457 (c && list_is_last(&c->entry, &p->children)))
458 return NULL;
459
460 if (c)
461 c = list_next_entry(c, entry);
462 else
80488a6b 463 c = list_first_entry(&p->children, struct swnode, entry);
59abd836
HK
464 return &c->fwnode;
465}
466
34479820
HK
467static struct fwnode_handle *
468software_node_get_named_child_node(const struct fwnode_handle *fwnode,
469 const char *childname)
470{
80488a6b 471 struct swnode *swnode = to_swnode(fwnode);
80488a6b 472 struct swnode *child;
34479820
HK
473
474 if (!swnode || list_empty(&swnode->children))
475 return NULL;
476
477 list_for_each_entry(child, &swnode->children, entry) {
c959d0c2 478 if (!strcmp(childname, kobject_name(&child->kobj))) {
34479820
HK
479 kobject_get(&child->kobj);
480 return &child->fwnode;
481 }
482 }
483 return NULL;
484}
59abd836 485
b06184ac
HK
486static int
487software_node_get_reference_args(const struct fwnode_handle *fwnode,
488 const char *propname, const char *nargs_prop,
489 unsigned int nargs, unsigned int index,
490 struct fwnode_reference_args *args)
491{
492 struct swnode *swnode = to_swnode(fwnode);
493 const struct software_node_reference *ref;
494 const struct property_entry *prop;
495 struct fwnode_handle *refnode;
496 int i;
497
498 if (!swnode || !swnode->node->references)
499 return -ENOENT;
500
501 for (ref = swnode->node->references; ref->name; ref++)
502 if (!strcmp(ref->name, propname))
503 break;
504
505 if (!ref->name || index > (ref->nrefs - 1))
506 return -ENOENT;
507
508 refnode = software_node_fwnode(ref->refs[index].node);
509 if (!refnode)
510 return -ENOENT;
511
512 if (nargs_prop) {
513 prop = property_entry_get(swnode->node->properties, nargs_prop);
514 if (!prop)
515 return -EINVAL;
516
517 nargs = prop->value.u32_data;
518 }
519
520 if (nargs > NR_FWNODE_REFERENCE_ARGS)
521 return -EINVAL;
522
523 args->fwnode = software_node_get(refnode);
524 args->nargs = nargs;
525
526 for (i = 0; i < nargs; i++)
527 args->args[i] = ref->refs[index].args[i];
528
529 return 0;
530}
531
59abd836
HK
532static const struct fwnode_operations software_node_ops = {
533 .get = software_node_get,
534 .put = software_node_put,
535 .property_present = software_node_property_present,
536 .property_read_int_array = software_node_read_int_array,
537 .property_read_string_array = software_node_read_string_array,
bc0500c1 538 .get_name = software_node_get_name,
e7e242bc 539 .get_name_prefix = software_node_get_name_prefix,
59abd836
HK
540 .get_parent = software_node_get_parent,
541 .get_next_child_node = software_node_get_next_child,
34479820 542 .get_named_child_node = software_node_get_named_child_node,
b06184ac 543 .get_reference_args = software_node_get_reference_args
59abd836
HK
544};
545
546/* -------------------------------------------------------------------------- */
547
1666faed
HK
548/**
549 * software_node_find_by_name - Find software node by name
550 * @parent: Parent of the software node
551 * @name: Name of the software node
552 *
553 * The function will find a node that is child of @parent and that is named
554 * @name. If no node is found, the function returns NULL.
555 *
556 * NOTE: you will need to drop the reference with fwnode_handle_put() after use.
557 */
558const struct software_node *
559software_node_find_by_name(const struct software_node *parent, const char *name)
560{
016049a8 561 struct swnode *swnode = NULL;
1666faed
HK
562 struct kobject *k;
563
564 if (!name)
565 return NULL;
566
567 spin_lock(&swnode_kset->list_lock);
568
569 list_for_each_entry(k, &swnode_kset->list, entry) {
570 swnode = kobj_to_swnode(k);
571 if (parent == swnode->node->parent && swnode->node->name &&
572 !strcmp(name, swnode->node->name)) {
573 kobject_get(&swnode->kobj);
574 break;
575 }
576 swnode = NULL;
577 }
578
579 spin_unlock(&swnode_kset->list_lock);
580
581 return swnode ? swnode->node : NULL;
582}
583EXPORT_SYMBOL_GPL(software_node_find_by_name);
584
59abd836 585static int
80488a6b 586software_node_register_properties(struct software_node *node,
59abd836
HK
587 const struct property_entry *properties)
588{
589 struct property_entry *props;
590
591 props = property_entries_dup(properties);
592 if (IS_ERR(props))
593 return PTR_ERR(props);
594
80488a6b 595 node->properties = props;
59abd836
HK
596
597 return 0;
598}
599
600static void software_node_release(struct kobject *kobj)
601{
80488a6b 602 struct swnode *swnode = kobj_to_swnode(kobj);
59abd836 603
80488a6b
HK
604 if (swnode->allocated) {
605 property_entries_free(swnode->node->properties);
606 kfree(swnode->node);
607 }
59abd836 608 ida_destroy(&swnode->child_ids);
59abd836
HK
609 kfree(swnode);
610}
611
612static struct kobj_type software_node_type = {
613 .release = software_node_release,
614 .sysfs_ops = &kobj_sysfs_ops,
615};
616
80488a6b
HK
617static struct fwnode_handle *
618swnode_register(const struct software_node *node, struct swnode *parent,
619 unsigned int allocated)
59abd836 620{
80488a6b 621 struct swnode *swnode;
59abd836
HK
622 int ret;
623
59abd836 624 swnode = kzalloc(sizeof(*swnode), GFP_KERNEL);
80488a6b
HK
625 if (!swnode) {
626 ret = -ENOMEM;
627 goto out_err;
628 }
59abd836 629
80488a6b
HK
630 ret = ida_simple_get(parent ? &parent->child_ids : &swnode_root_ids,
631 0, 0, GFP_KERNEL);
59abd836
HK
632 if (ret < 0) {
633 kfree(swnode);
80488a6b 634 goto out_err;
59abd836
HK
635 }
636
637 swnode->id = ret;
80488a6b
HK
638 swnode->node = node;
639 swnode->parent = parent;
640 swnode->allocated = allocated;
59abd836
HK
641 swnode->kobj.kset = swnode_kset;
642 swnode->fwnode.ops = &software_node_ops;
643
644 ida_init(&swnode->child_ids);
645 INIT_LIST_HEAD(&swnode->entry);
646 INIT_LIST_HEAD(&swnode->children);
59abd836 647
80488a6b
HK
648 if (node->name)
649 ret = kobject_init_and_add(&swnode->kobj, &software_node_type,
650 parent ? &parent->kobj : NULL,
651 "%s", node->name);
652 else
653 ret = kobject_init_and_add(&swnode->kobj, &software_node_type,
654 parent ? &parent->kobj : NULL,
655 "node%d", swnode->id);
59abd836
HK
656 if (ret) {
657 kobject_put(&swnode->kobj);
658 return ERR_PTR(ret);
659 }
660
80488a6b
HK
661 if (parent)
662 list_add_tail(&swnode->entry, &parent->children);
663
664 kobject_uevent(&swnode->kobj, KOBJ_ADD);
665 return &swnode->fwnode;
666
667out_err:
668 if (allocated)
669 property_entries_free(node->properties);
670 return ERR_PTR(ret);
671}
672
673/**
674 * software_node_register_nodes - Register an array of software nodes
675 * @nodes: Zero terminated array of software nodes to be registered
676 *
677 * Register multiple software nodes at once.
678 */
679int software_node_register_nodes(const struct software_node *nodes)
680{
681 int ret;
682 int i;
683
684 for (i = 0; nodes[i].name; i++) {
685 ret = software_node_register(&nodes[i]);
686 if (ret) {
687 software_node_unregister_nodes(nodes);
688 return ret;
689 }
690 }
691
692 return 0;
693}
694EXPORT_SYMBOL_GPL(software_node_register_nodes);
695
696/**
697 * software_node_unregister_nodes - Unregister an array of software nodes
698 * @nodes: Zero terminated array of software nodes to be unregistered
699 *
700 * Unregister multiple software nodes at once.
701 */
702void software_node_unregister_nodes(const struct software_node *nodes)
703{
704 struct swnode *swnode;
705 int i;
706
707 for (i = 0; nodes[i].name; i++) {
708 swnode = software_node_to_swnode(&nodes[i]);
709 if (swnode)
710 fwnode_remove_software_node(&swnode->fwnode);
711 }
712}
713EXPORT_SYMBOL_GPL(software_node_unregister_nodes);
714
715/**
716 * software_node_register - Register static software node
717 * @node: The software node to be registered
718 */
719int software_node_register(const struct software_node *node)
720{
721 struct swnode *parent = software_node_to_swnode(node->parent);
722
723 if (software_node_to_swnode(node))
724 return -EEXIST;
725
726 return PTR_ERR_OR_ZERO(swnode_register(node, parent, 0));
727}
728EXPORT_SYMBOL_GPL(software_node_register);
729
730struct fwnode_handle *
731fwnode_create_software_node(const struct property_entry *properties,
732 const struct fwnode_handle *parent)
733{
734 struct software_node *node;
735 struct swnode *p = NULL;
736 int ret;
737
738 if (parent) {
739 if (IS_ERR(parent))
740 return ERR_CAST(parent);
741 if (!is_software_node(parent))
742 return ERR_PTR(-EINVAL);
743 p = to_swnode(parent);
744 }
745
746 node = kzalloc(sizeof(*node), GFP_KERNEL);
747 if (!node)
748 return ERR_PTR(-ENOMEM);
749
750 ret = software_node_register_properties(node, properties);
59abd836 751 if (ret) {
80488a6b 752 kfree(node);
59abd836
HK
753 return ERR_PTR(ret);
754 }
755
80488a6b 756 node->parent = p ? p->node : NULL;
3df85a1a 757
80488a6b 758 return swnode_register(node, p, 1);
59abd836
HK
759}
760EXPORT_SYMBOL_GPL(fwnode_create_software_node);
761
762void fwnode_remove_software_node(struct fwnode_handle *fwnode)
763{
80488a6b 764 struct swnode *swnode = to_swnode(fwnode);
59abd836
HK
765
766 if (!swnode)
767 return;
768
3df85a1a
HK
769 if (swnode->parent) {
770 ida_simple_remove(&swnode->parent->child_ids, swnode->id);
771 list_del(&swnode->entry);
772 } else {
773 ida_simple_remove(&swnode_root_ids, swnode->id);
774 }
775
59abd836
HK
776 kobject_put(&swnode->kobj);
777}
778EXPORT_SYMBOL_GPL(fwnode_remove_software_node);
779
780int software_node_notify(struct device *dev, unsigned long action)
781{
782 struct fwnode_handle *fwnode = dev_fwnode(dev);
80488a6b 783 struct swnode *swnode;
59abd836
HK
784 int ret;
785
786 if (!fwnode)
787 return 0;
788
789 if (!is_software_node(fwnode))
790 fwnode = fwnode->secondary;
791 if (!is_software_node(fwnode))
792 return 0;
793
80488a6b 794 swnode = to_swnode(fwnode);
59abd836
HK
795
796 switch (action) {
797 case KOBJ_ADD:
798 ret = sysfs_create_link(&dev->kobj, &swnode->kobj,
799 "software_node");
800 if (ret)
801 break;
802
803 ret = sysfs_create_link(&swnode->kobj, &dev->kobj,
804 dev_name(dev));
805 if (ret) {
806 sysfs_remove_link(&dev->kobj, "software_node");
807 break;
808 }
809 kobject_get(&swnode->kobj);
810 break;
811 case KOBJ_REMOVE:
812 sysfs_remove_link(&swnode->kobj, dev_name(dev));
813 sysfs_remove_link(&dev->kobj, "software_node");
814 kobject_put(&swnode->kobj);
815 break;
816 default:
817 break;
818 }
819
820 return 0;
821}
822
823static int __init software_node_init(void)
824{
825 swnode_kset = kset_create_and_add("software_nodes", NULL, kernel_kobj);
826 if (!swnode_kset)
827 return -ENOMEM;
828 return 0;
829}
830postcore_initcall(software_node_init);
831
832static void __exit software_node_exit(void)
833{
834 ida_destroy(&swnode_root_ids);
835 kset_unregister(swnode_kset);
836}
837__exitcall(software_node_exit);