Linux Audio

Check our new training course

Loading...
v3.5.6
  1/* prom_common.c: OF device tree support common code.
  2 *
  3 * Paul Mackerras	August 1996.
  4 * Copyright (C) 1996-2005 Paul Mackerras.
  5 *
  6 *  Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
  7 *    {engebret|bergner}@us.ibm.com
  8 *
  9 *  Adapted for sparc by David S. Miller davem@davemloft.net
 10 *
 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.
 15 */
 16
 17#include <linux/kernel.h>
 18#include <linux/export.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
 27#include "prom.h"
 28
 29struct device_node *of_console_device;
 30EXPORT_SYMBOL(of_console_device);
 31
 32char *of_console_path;
 33EXPORT_SYMBOL(of_console_path);
 34
 35char *of_console_options;
 36EXPORT_SYMBOL(of_console_options);
 37
 38int of_getintprop_default(struct device_node *np, const char *name, int def)
 39{
 40	struct property *prop;
 41	int len;
 42
 43	prop = of_find_property(np, name, &len);
 44	if (!prop || len != 4)
 45		return def;
 46
 47	return *(int *) prop->value;
 48}
 49EXPORT_SYMBOL(of_getintprop_default);
 50
 51DEFINE_MUTEX(of_set_property_mutex);
 52EXPORT_SYMBOL(of_set_property_mutex);
 53
 54int of_set_property(struct device_node *dp, const char *name, void *val, int len)
 55{
 56	struct property **prevp;
 57	void *new_val;
 58	int err;
 59
 60	new_val = kmemdup(val, len, GFP_KERNEL);
 61	if (!new_val)
 62		return -ENOMEM;
 
 
 63
 64	err = -ENODEV;
 65
 66	mutex_lock(&of_set_property_mutex);
 67	write_lock(&devtree_lock);
 68	prevp = &dp->properties;
 69	while (*prevp) {
 70		struct property *prop = *prevp;
 71
 72		if (!strcasecmp(prop->name, name)) {
 73			void *old_val = prop->value;
 74			int ret;
 75
 76			ret = prom_setprop(dp->phandle, name, val, len);
 77
 78			err = -EINVAL;
 79			if (ret >= 0) {
 80				prop->value = new_val;
 81				prop->length = len;
 82
 83				if (OF_IS_DYNAMIC(prop))
 84					kfree(old_val);
 85
 86				OF_MARK_DYNAMIC(prop);
 87
 88				err = 0;
 89			}
 90			break;
 91		}
 92		prevp = &(*prevp)->next;
 93	}
 94	write_unlock(&devtree_lock);
 95	mutex_unlock(&of_set_property_mutex);
 96
 97	/* XXX Upate procfs if necessary... */
 98
 99	return err;
100}
101EXPORT_SYMBOL(of_set_property);
102
103int of_find_in_proplist(const char *list, const char *match, int len)
104{
105	while (len > 0) {
106		int l;
107
108		if (!strcmp(list, match))
109			return 1;
110		l = strlen(list) + 1;
111		list += l;
112		len -= l;
113	}
114	return 0;
115}
116EXPORT_SYMBOL(of_find_in_proplist);
117
118/*
119 * SPARC32 and SPARC64's prom_nextprop() do things differently
120 * here, despite sharing the same interface.  SPARC32 doesn't fill in 'buf',
121 * returning NULL on an error.  SPARC64 fills in 'buf', but sets it to an
122 * empty string upon error.
123 */
124static int __init handle_nextprop_quirks(char *buf, const char *name)
125{
126	if (!name || strlen(name) == 0)
127		return -1;
128
129#ifdef CONFIG_SPARC32
130	strcpy(buf, name);
131#endif
132	return 0;
133}
134
135static int __init prom_common_nextprop(phandle node, char *prev, char *buf)
136{
137	const char *name;
138
139	buf[0] = '\0';
140	name = prom_nextprop(node, prev, buf);
141	return handle_nextprop_quirks(buf, name);
142}
143
144unsigned int prom_early_allocated __initdata;
145
146static struct of_pdt_ops prom_sparc_ops __initdata = {
147	.nextprop = prom_common_nextprop,
148	.getproplen = prom_getproplen,
149	.getproperty = prom_getproperty,
150	.getchild = prom_getchild,
151	.getsibling = prom_getsibling,
152};
153
154void __init prom_build_devicetree(void)
155{
156	of_pdt_build_devicetree(prom_root_node, &prom_sparc_ops);
157	of_console_init();
158
159	pr_info("PROM: Built device tree with %u bytes of memory.\n",
160			prom_early_allocated);
161}
v3.1
  1/* prom_common.c: OF device tree support common code.
  2 *
  3 * Paul Mackerras	August 1996.
  4 * Copyright (C) 1996-2005 Paul Mackerras.
  5 *
  6 *  Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
  7 *    {engebret|bergner}@us.ibm.com
  8 *
  9 *  Adapted for sparc by David S. Miller davem@davemloft.net
 10 *
 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.
 15 */
 16
 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>
 27
 28#include "prom.h"
 29
 30struct device_node *of_console_device;
 31EXPORT_SYMBOL(of_console_device);
 32
 33char *of_console_path;
 34EXPORT_SYMBOL(of_console_path);
 35
 36char *of_console_options;
 37EXPORT_SYMBOL(of_console_options);
 38
 39int of_getintprop_default(struct device_node *np, const char *name, int def)
 40{
 41	struct property *prop;
 42	int len;
 43
 44	prop = of_find_property(np, name, &len);
 45	if (!prop || len != 4)
 46		return def;
 47
 48	return *(int *) prop->value;
 49}
 50EXPORT_SYMBOL(of_getintprop_default);
 51
 52DEFINE_MUTEX(of_set_property_mutex);
 53EXPORT_SYMBOL(of_set_property_mutex);
 54
 55int of_set_property(struct device_node *dp, const char *name, void *val, int len)
 56{
 57	struct property **prevp;
 58	void *new_val;
 59	int err;
 60
 61	new_val = kmalloc(len, GFP_KERNEL);
 62	if (!new_val)
 63		return -ENOMEM;
 64
 65	memcpy(new_val, val, len);
 66
 67	err = -ENODEV;
 68
 69	mutex_lock(&of_set_property_mutex);
 70	write_lock(&devtree_lock);
 71	prevp = &dp->properties;
 72	while (*prevp) {
 73		struct property *prop = *prevp;
 74
 75		if (!strcasecmp(prop->name, name)) {
 76			void *old_val = prop->value;
 77			int ret;
 78
 79			ret = prom_setprop(dp->phandle, name, val, len);
 80
 81			err = -EINVAL;
 82			if (ret >= 0) {
 83				prop->value = new_val;
 84				prop->length = len;
 85
 86				if (OF_IS_DYNAMIC(prop))
 87					kfree(old_val);
 88
 89				OF_MARK_DYNAMIC(prop);
 90
 91				err = 0;
 92			}
 93			break;
 94		}
 95		prevp = &(*prevp)->next;
 96	}
 97	write_unlock(&devtree_lock);
 98	mutex_unlock(&of_set_property_mutex);
 99
100	/* XXX Upate procfs if necessary... */
101
102	return err;
103}
104EXPORT_SYMBOL(of_set_property);
105
106int of_find_in_proplist(const char *list, const char *match, int len)
107{
108	while (len > 0) {
109		int l;
110
111		if (!strcmp(list, match))
112			return 1;
113		l = strlen(list) + 1;
114		list += l;
115		len -= l;
116	}
117	return 0;
118}
119EXPORT_SYMBOL(of_find_in_proplist);
120
121/*
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.
126 */
127static int __init handle_nextprop_quirks(char *buf, const char *name)
128{
129	if (!name || strlen(name) == 0)
130		return -1;
131
132#ifdef CONFIG_SPARC32
133	strcpy(buf, name);
134#endif
135	return 0;
136}
137
138static int __init prom_common_nextprop(phandle node, char *prev, char *buf)
139{
140	const char *name;
141
142	buf[0] = '\0';
143	name = prom_nextprop(node, prev, buf);
144	return handle_nextprop_quirks(buf, name);
145}
146
147unsigned int prom_early_allocated __initdata;
148
149static 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,
155};
156
157void __init prom_build_devicetree(void)
158{
159	of_pdt_build_devicetree(prom_root_node, &prom_sparc_ops);
160	of_console_init();
161
162	pr_info("PROM: Built device tree with %u bytes of memory.\n",
163			prom_early_allocated);
164}