of: Move of_modalias() to module.c
[platform/kernel/linux-starfive.git] / drivers / of / module.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Linux kernel module helpers.
4  */
5
6 #include <linux/of.h>
7 #include <linux/slab.h>
8 #include <linux/string.h>
9
10 ssize_t of_modalias(const struct device_node *np, char *str, ssize_t len)
11 {
12         const char *compat;
13         char *c;
14         struct property *p;
15         ssize_t csize;
16         ssize_t tsize;
17
18         /* Name & Type */
19         /* %p eats all alphanum characters, so %c must be used here */
20         csize = snprintf(str, len, "of:N%pOFn%c%s", np, 'T',
21                          of_node_get_device_type(np));
22         tsize = csize;
23         len -= csize;
24         if (str)
25                 str += csize;
26
27         of_property_for_each_string(np, "compatible", p, compat) {
28                 csize = strlen(compat) + 1;
29                 tsize += csize;
30                 if (csize > len)
31                         continue;
32
33                 csize = snprintf(str, len, "C%s", compat);
34                 for (c = str; c; ) {
35                         c = strchr(c, ' ');
36                         if (c)
37                                 *c++ = '_';
38                 }
39                 len -= csize;
40                 str += csize;
41         }
42
43         return tsize;
44 }