staging/easycap: don't cast NULL pointer
[wandboard.git] / arch / sparc / kernel / prom_common.c
blobed25834328f4674f19dd7196fcc19153a337fd14
1 /* prom_common.c: OF device tree support common code.
3 * Paul Mackerras August 1996.
4 * Copyright (C) 1996-2005 Paul Mackerras.
6 * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
7 * {engebret|bergner}@us.ibm.com
9 * Adapted for sparc by David S. Miller davem@davemloft.net
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/errno.h>
20 #include <linux/mutex.h>
21 #include <linux/slab.h>
22 #include <linux/of.h>
23 #include <linux/of_pdt.h>
24 #include <asm/prom.h>
25 #include <asm/oplib.h>
26 #include <asm/leon.h>
28 #include "prom.h"
30 struct device_node *of_console_device;
31 EXPORT_SYMBOL(of_console_device);
33 char *of_console_path;
34 EXPORT_SYMBOL(of_console_path);
36 char *of_console_options;
37 EXPORT_SYMBOL(of_console_options);
39 int of_getintprop_default(struct device_node *np, const char *name, int def)
41 struct property *prop;
42 int len;
44 prop = of_find_property(np, name, &len);
45 if (!prop || len != 4)
46 return def;
48 return *(int *) prop->value;
50 EXPORT_SYMBOL(of_getintprop_default);
52 DEFINE_MUTEX(of_set_property_mutex);
53 EXPORT_SYMBOL(of_set_property_mutex);
55 int of_set_property(struct device_node *dp, const char *name, void *val, int len)
57 struct property **prevp;
58 void *new_val;
59 int err;
61 new_val = kmalloc(len, GFP_KERNEL);
62 if (!new_val)
63 return -ENOMEM;
65 memcpy(new_val, val, len);
67 err = -ENODEV;
69 mutex_lock(&of_set_property_mutex);
70 write_lock(&devtree_lock);
71 prevp = &dp->properties;
72 while (*prevp) {
73 struct property *prop = *prevp;
75 if (!strcasecmp(prop->name, name)) {
76 void *old_val = prop->value;
77 int ret;
79 ret = prom_setprop(dp->phandle, name, val, len);
81 err = -EINVAL;
82 if (ret >= 0) {
83 prop->value = new_val;
84 prop->length = len;
86 if (OF_IS_DYNAMIC(prop))
87 kfree(old_val);
89 OF_MARK_DYNAMIC(prop);
91 err = 0;
93 break;
95 prevp = &(*prevp)->next;
97 write_unlock(&devtree_lock);
98 mutex_unlock(&of_set_property_mutex);
100 /* XXX Upate procfs if necessary... */
102 return err;
104 EXPORT_SYMBOL(of_set_property);
106 int of_find_in_proplist(const char *list, const char *match, int len)
108 while (len > 0) {
109 int l;
111 if (!strcmp(list, match))
112 return 1;
113 l = strlen(list) + 1;
114 list += l;
115 len -= l;
117 return 0;
119 EXPORT_SYMBOL(of_find_in_proplist);
122 * SPARC32 and SPARC64's prom_nextprop() do things differently
123 * here, despite sharing the same interface. SPARC32 doesn't fill in 'buf',
124 * returning NULL on an error. SPARC64 fills in 'buf', but sets it to an
125 * empty string upon error.
127 static int __init handle_nextprop_quirks(char *buf, const char *name)
129 if (!name || strlen(name) == 0)
130 return -1;
132 #ifdef CONFIG_SPARC32
133 strcpy(buf, name);
134 #endif
135 return 0;
138 static int __init prom_common_nextprop(phandle node, char *prev, char *buf)
140 const char *name;
142 buf[0] = '\0';
143 name = prom_nextprop(node, prev, buf);
144 return handle_nextprop_quirks(buf, name);
147 unsigned int prom_early_allocated __initdata;
149 static struct of_pdt_ops prom_sparc_ops __initdata = {
150 .nextprop = prom_common_nextprop,
151 .getproplen = prom_getproplen,
152 .getproperty = prom_getproperty,
153 .getchild = prom_getchild,
154 .getsibling = prom_getsibling,
157 void __init prom_build_devicetree(void)
159 of_pdt_build_devicetree(prom_root_node, &prom_sparc_ops);
160 of_console_init();
162 pr_info("PROM: Built device tree with %u bytes of memory.\n",
163 prom_early_allocated);