9
0
Fork 0
barebox/lib/parameter.c

121 lines
2.4 KiB
C
Raw Normal View History

2007-07-05 16:02:19 +00:00
/*
* parameter.c - device parameters
*
* Copyright (c) 2007 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
*
* See file CREDITS for list of people who contributed to this
* project.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
2007-07-05 16:02:12 +00:00
#include <common.h>
#include <param.h>
#include <errno.h>
#include <net.h>
#include <malloc.h>
#include <driver.h>
struct param_d *get_param_by_name(struct device_d *dev, const char *name)
{
2007-10-11 18:56:18 +00:00
struct param_d *param = dev->param;
2007-07-05 16:02:12 +00:00
2007-10-11 18:56:18 +00:00
while (param) {
if (!strcmp(param->name, name))
return param;
param = param->next;
}
2007-07-05 16:02:12 +00:00
2007-10-11 18:56:18 +00:00
return NULL;
2007-07-05 16:02:12 +00:00
}
const char *dev_get_param(struct device_d *dev, const char *name)
{
2007-10-11 18:56:18 +00:00
struct param_d *param = get_param_by_name(dev, name);
2007-07-05 16:02:12 +00:00
if (!param) {
errno = -EINVAL;
return NULL;
}
2007-10-11 18:56:18 +00:00
if (param->get)
return param->get(dev, param);
2007-07-05 16:02:12 +00:00
2007-10-11 18:56:18 +00:00
return param->value;
2007-07-05 16:02:12 +00:00
}
#ifdef CONFIG_NET
2007-07-05 16:02:12 +00:00
IPaddr_t dev_get_param_ip(struct device_d *dev, char *name)
{
return string_to_ip(dev_get_param(dev, name));
}
int dev_set_param_ip(struct device_d *dev, char *name, IPaddr_t ip)
{
char ipstr[sizeof("xxx.xxx.xxx.xxx")];
2007-07-05 16:02:12 +00:00
ip_to_string(ip, ipstr);
return dev_set_param(dev, name, ipstr);
}
#endif
2007-07-05 16:02:12 +00:00
int dev_set_param(struct device_d *dev, const char *name, const char *val)
{
2007-10-11 18:56:18 +00:00
struct param_d *param;
2007-07-05 16:02:12 +00:00
if (!dev) {
errno = -ENODEV;
return -ENODEV;
}
param = get_param_by_name(dev, name);
if (!param) {
errno = -EINVAL;
return -EINVAL;
}
if (param->flags & PARAM_FLAG_RO) {
errno = -EACCES;
return -EACCES;
}
2007-10-11 18:56:18 +00:00
if (param->set)
return param->set(dev, param, val);
2007-07-05 16:02:12 +00:00
if (param->value)
free(param->value);
2007-10-11 18:56:18 +00:00
param->value = strdup(val);
2007-07-05 16:02:12 +00:00
return 0;
}
int dev_add_param(struct device_d *dev, struct param_d *newparam)
{
2007-10-11 18:56:18 +00:00
struct param_d *param = dev->param;
2007-07-05 16:02:12 +00:00
newparam->next = NULL;
2007-07-05 16:02:12 +00:00
2007-10-11 18:56:18 +00:00
if (param) {
while (param->next)
param = param->next;
param->next = newparam;
} else {
dev->param = newparam;
}
2007-07-05 16:02:12 +00:00
2007-10-11 18:56:18 +00:00
return 0;
2007-07-05 16:02:12 +00:00
}