[POWERPC] ibmebus: Prevent bus_id collisions
[linux-2.6] / arch / powerpc / kernel / of_device.c
1 #include <linux/string.h>
2 #include <linux/kernel.h>
3 #include <linux/of.h>
4 #include <linux/init.h>
5 #include <linux/module.h>
6 #include <linux/mod_devicetable.h>
7 #include <linux/slab.h>
8
9 #include <asm/errno.h>
10 #include <asm/of_device.h>
11
12 ssize_t of_device_get_modalias(struct of_device *ofdev,
13                                 char *str, ssize_t len)
14 {
15         const char *compat;
16         int cplen, i;
17         ssize_t tsize, csize, repend;
18
19         /* Name & Type */
20         csize = snprintf(str, len, "of:N%sT%s",
21                                 ofdev->node->name, ofdev->node->type);
22
23         /* Get compatible property if any */
24         compat = of_get_property(ofdev->node, "compatible", &cplen);
25         if (!compat)
26                 return csize;
27
28         /* Find true end (we tolerate multiple \0 at the end */
29         for (i=(cplen-1); i>=0 && !compat[i]; i--)
30                 cplen--;
31         if (!cplen)
32                 return csize;
33         cplen++;
34
35         /* Check space (need cplen+1 chars including final \0) */
36         tsize = csize + cplen;
37         repend = tsize;
38
39         if (csize>=len)         /* @ the limit, all is already filled */
40                 return tsize;
41
42         if (tsize>=len) {               /* limit compat list */
43                 cplen = len-csize-1;
44                 repend = len;
45         }
46
47         /* Copy and do char replacement */
48         memcpy(&str[csize+1], compat, cplen);
49         for (i=csize; i<repend; i++) {
50                 char c = str[i];
51                 if (c=='\0')
52                         str[i] = 'C';
53                 else if (c==' ')
54                         str[i] = '_';
55         }
56
57         return tsize;
58 }
59
60 int of_device_uevent(struct device *dev,
61                 char **envp, int num_envp, char *buffer, int buffer_size)
62 {
63         struct of_device *ofdev;
64         const char *compat;
65         int i = 0, length = 0, seen = 0, cplen, sl;
66
67         if (!dev)
68                 return -ENODEV;
69
70         ofdev = to_of_device(dev);
71
72         if (add_uevent_var(envp, num_envp, &i,
73                            buffer, buffer_size, &length,
74                            "OF_NAME=%s", ofdev->node->name))
75                 return -ENOMEM;
76
77         if (add_uevent_var(envp, num_envp, &i,
78                            buffer, buffer_size, &length,
79                            "OF_TYPE=%s", ofdev->node->type))
80                 return -ENOMEM;
81
82         /* Since the compatible field can contain pretty much anything
83          * it's not really legal to split it out with commas. We split it
84          * up using a number of environment variables instead. */
85
86         compat = of_get_property(ofdev->node, "compatible", &cplen);
87         while (compat && *compat && cplen > 0) {
88                 if (add_uevent_var(envp, num_envp, &i,
89                                    buffer, buffer_size, &length,
90                                    "OF_COMPATIBLE_%d=%s", seen, compat))
91                         return -ENOMEM;
92
93                 sl = strlen (compat) + 1;
94                 compat += sl;
95                 cplen -= sl;
96                 seen++;
97         }
98
99         if (add_uevent_var(envp, num_envp, &i,
100                            buffer, buffer_size, &length,
101                            "OF_COMPATIBLE_N=%d", seen))
102                 return -ENOMEM;
103
104         /* modalias is trickier, we add it in 2 steps */
105         if (add_uevent_var(envp, num_envp, &i,
106                            buffer, buffer_size, &length,
107                            "MODALIAS="))
108                 return -ENOMEM;
109
110         sl = of_device_get_modalias(ofdev, &buffer[length-1],
111                                         buffer_size-length);
112         if (sl >= (buffer_size-length))
113                 return -ENOMEM;
114
115         length += sl;
116
117         envp[i] = NULL;
118
119         return 0;
120 }
121 EXPORT_SYMBOL(of_device_uevent);
122 EXPORT_SYMBOL(of_device_get_modalias);