Paul Mackerras | 2e686bc | 2005-10-06 13:22:17 +1000 | [diff] [blame] | 1 | #include <linux/string.h> |
| 2 | #include <linux/kernel.h> |
Stephen Rothwell | f85ff30 | 2007-05-01 16:40:36 +1000 | [diff] [blame] | 3 | #include <linux/of.h> |
Paul Mackerras | 2e686bc | 2005-10-06 13:22:17 +1000 | [diff] [blame] | 4 | #include <linux/init.h> |
| 5 | #include <linux/module.h> |
| 6 | #include <linux/mod_devicetable.h> |
Paul Mackerras | 734d652 | 2005-10-31 13:57:01 +1100 | [diff] [blame] | 7 | #include <linux/slab.h> |
| 8 | |
Paul Mackerras | 2e686bc | 2005-10-06 13:22:17 +1000 | [diff] [blame] | 9 | #include <asm/errno.h> |
| 10 | #include <asm/of_device.h> |
| 11 | |
Sylvain Munaut | de41189 | 2007-05-07 01:38:46 +1000 | [diff] [blame] | 12 | ssize_t of_device_get_modalias(struct of_device *ofdev, |
| 13 | char *str, ssize_t len) |
Sylvain Munaut | eb0cb8a | 2007-02-12 23:13:25 +0100 | [diff] [blame] | 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 */ |
Stephen Rothwell | b3a6d2a | 2007-04-13 17:14:22 +1000 | [diff] [blame] | 24 | compat = of_get_property(ofdev->node, "compatible", &cplen); |
Sylvain Munaut | eb0cb8a | 2007-02-12 23:13:25 +0100 | [diff] [blame] | 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 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame^] | 60 | int of_device_uevent(struct device *dev, struct kobj_uevent_env *env) |
Sylvain Munaut | eb0cb8a | 2007-02-12 23:13:25 +0100 | [diff] [blame] | 61 | { |
| 62 | struct of_device *ofdev; |
| 63 | const char *compat; |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame^] | 64 | int seen = 0, cplen, sl; |
Sylvain Munaut | eb0cb8a | 2007-02-12 23:13:25 +0100 | [diff] [blame] | 65 | |
| 66 | if (!dev) |
| 67 | return -ENODEV; |
| 68 | |
| 69 | ofdev = to_of_device(dev); |
| 70 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame^] | 71 | if (add_uevent_var(env, "OF_NAME=%s", ofdev->node->name)) |
Sylvain Munaut | eb0cb8a | 2007-02-12 23:13:25 +0100 | [diff] [blame] | 72 | return -ENOMEM; |
| 73 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame^] | 74 | if (add_uevent_var(env, "OF_TYPE=%s", ofdev->node->type)) |
Sylvain Munaut | eb0cb8a | 2007-02-12 23:13:25 +0100 | [diff] [blame] | 75 | return -ENOMEM; |
| 76 | |
| 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. */ |
| 80 | |
Stephen Rothwell | b3a6d2a | 2007-04-13 17:14:22 +1000 | [diff] [blame] | 81 | compat = of_get_property(ofdev->node, "compatible", &cplen); |
Sylvain Munaut | eb0cb8a | 2007-02-12 23:13:25 +0100 | [diff] [blame] | 82 | while (compat && *compat && cplen > 0) { |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame^] | 83 | if (add_uevent_var(env, "OF_COMPATIBLE_%d=%s", seen, compat)) |
Sylvain Munaut | eb0cb8a | 2007-02-12 23:13:25 +0100 | [diff] [blame] | 84 | return -ENOMEM; |
| 85 | |
| 86 | sl = strlen (compat) + 1; |
| 87 | compat += sl; |
| 88 | cplen -= sl; |
| 89 | seen++; |
| 90 | } |
| 91 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame^] | 92 | if (add_uevent_var(env, "OF_COMPATIBLE_N=%d", seen)) |
Sylvain Munaut | eb0cb8a | 2007-02-12 23:13:25 +0100 | [diff] [blame] | 93 | return -ENOMEM; |
| 94 | |
| 95 | /* modalias is trickier, we add it in 2 steps */ |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame^] | 96 | if (add_uevent_var(env, "MODALIAS=")) |
Sylvain Munaut | eb0cb8a | 2007-02-12 23:13:25 +0100 | [diff] [blame] | 97 | return -ENOMEM; |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame^] | 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)) |
Sylvain Munaut | eb0cb8a | 2007-02-12 23:13:25 +0100 | [diff] [blame] | 101 | return -ENOMEM; |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame^] | 102 | env->buflen += sl; |
Sylvain Munaut | eb0cb8a | 2007-02-12 23:13:25 +0100 | [diff] [blame] | 103 | |
| 104 | return 0; |
| 105 | } |
Sylvain Munaut | eb0cb8a | 2007-02-12 23:13:25 +0100 | [diff] [blame] | 106 | EXPORT_SYMBOL(of_device_uevent); |
Sylvain Munaut | de41189 | 2007-05-07 01:38:46 +1000 | [diff] [blame] | 107 | EXPORT_SYMBOL(of_device_get_modalias); |