1 #include <linux/string.h>
2 #include <linux/kernel.h>
4 #include <linux/init.h>
5 #include <linux/module.h>
6 #include <linux/mod_devicetable.h>
7 #include <linux/slab.h>
10 #include <asm/of_device.h>
12 ssize_t of_device_get_modalias(struct of_device *ofdev,
13 char *str, ssize_t len)
17 ssize_t tsize, csize, repend;
20 csize = snprintf(str, len, "of:N%sT%s",
21 ofdev->node->name, ofdev->node->type);
23 /* Get compatible property if any */
24 compat = of_get_property(ofdev->node, "compatible", &cplen);
28 /* Find true end (we tolerate multiple \0 at the end */
29 for (i=(cplen-1); i>=0 && !compat[i]; i--)
35 /* Check space (need cplen+1 chars including final \0) */
36 tsize = csize + cplen;
39 if (csize>=len) /* @ the limit, all is already filled */
42 if (tsize>=len) { /* limit compat list */
47 /* Copy and do char replacement */
48 memcpy(&str[csize+1], compat, cplen);
49 for (i=csize; i<repend; i++) {
60 int of_device_uevent(struct device *dev, struct kobj_uevent_env *env)
62 struct of_device *ofdev;
64 int seen = 0, cplen, sl;
69 ofdev = to_of_device(dev);
71 if (add_uevent_var(env, "OF_NAME=%s", ofdev->node->name))
74 if (add_uevent_var(env, "OF_TYPE=%s", ofdev->node->type))
77 /* Since the compatible field can contain pretty much anything
78 * it's not really legal to split it out with commas. We split it
79 * up using a number of environment variables instead. */
81 compat = of_get_property(ofdev->node, "compatible", &cplen);
82 while (compat && *compat && cplen > 0) {
83 if (add_uevent_var(env, "OF_COMPATIBLE_%d=%s", seen, compat))
86 sl = strlen (compat) + 1;
92 if (add_uevent_var(env, "OF_COMPATIBLE_N=%d", seen))
95 /* modalias is trickier, we add it in 2 steps */
96 if (add_uevent_var(env, "MODALIAS="))
98 sl = of_device_get_modalias(ofdev, &env->buf[env->buflen-1],
99 sizeof(env->buf) - env->buflen);
100 if (sl >= (sizeof(env->buf) - env->buflen))
106 EXPORT_SYMBOL(of_device_uevent);
107 EXPORT_SYMBOL(of_device_get_modalias);