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