Add basic support for mini2440 board to barebox.
[barebox-mini2440.git] / lib / parameter.c
blob0aa4193431f9111907c438296c1a0503df10af5d
1 /*
2 * parameter.c - device parameters
4 * Copyright (c) 2007 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
6 * See file CREDITS for list of people who contributed to this
7 * project.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2
11 * as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 /**
24 * @file
25 * @brief Handling device specific parameters
27 #include <common.h>
28 #include <param.h>
29 #include <errno.h>
30 #include <net.h>
31 #include <malloc.h>
32 #include <driver.h>
34 struct param_d *get_param_by_name(struct device_d *dev, const char *name)
36 struct param_d *p;
38 list_for_each_entry(p, &dev->parameters, list) {
39 if (!strcmp(p->name, name))
40 return p;
43 return NULL;
46 /**
47 * dev_get_param - get the value of a parameter
48 * @param dev The device
49 * @param name The name of the parameter
50 * @return The value
52 const char *dev_get_param(struct device_d *dev, const char *name)
54 struct param_d *param = get_param_by_name(dev, name);
56 if (!param) {
57 errno = -EINVAL;
58 return NULL;
61 return param->get(dev, param);
64 #ifdef CONFIG_NET
65 IPaddr_t dev_get_param_ip(struct device_d *dev, char *name)
67 IPaddr_t ip;
69 if (string_to_ip(dev_get_param(dev, name), &ip))
70 return 0;
72 return ip;
75 int dev_set_param_ip(struct device_d *dev, char *name, IPaddr_t ip)
77 char ipstr[sizeof("xxx.xxx.xxx.xxx")];
79 ip_to_string(ip, ipstr);
81 return dev_set_param(dev, name, ipstr);
83 #endif
85 /**
86 * dev_set_param - set a parameter of a device to a new value
87 * @param dev The device
88 * @param name The name of the parameter
89 * @param value The new value of the parameter
91 int dev_set_param(struct device_d *dev, const char *name, const char *val)
93 struct param_d *param;
95 if (!dev) {
96 errno = -ENODEV;
97 return -ENODEV;
100 param = get_param_by_name(dev, name);
102 if (!param) {
103 errno = -EINVAL;
104 return -EINVAL;
107 if (param->flags & PARAM_FLAG_RO) {
108 errno = -EACCES;
109 return -EACCES;
112 errno = param->set(dev, param, val);
113 return errno;
117 * dev_param_set_generic - generic setter function for a parameter
118 * @param dev The device
119 * @param p the parameter
120 * @param val The new value
122 * If used the value of a parameter is a string allocated with
123 * malloc and freed with free. If val is NULL the value is freed. This is
124 * used during deregistration of the parameter to free the alloctated
125 * memory.
127 int dev_param_set_generic(struct device_d *dev, struct param_d *p,
128 const char *val)
130 if (p->value)
131 free(p->value);
132 if (!val) {
133 p->value = NULL;
134 return 0;
136 p->value = strdup(val);
137 return 0;
140 static char *param_get_generic(struct device_d *dev, struct param_d *p)
142 return p->value;
145 static struct param_d *__dev_add_param(struct device_d *dev, char *name,
146 int (*set)(struct device_d *dev, struct param_d *p, const char *val),
147 char *(*get)(struct device_d *dev, struct param_d *p),
148 unsigned long flags)
150 struct param_d *param;
152 param = xzalloc(sizeof(*param));
154 if (set)
155 param->set = set;
156 else
157 param->set = dev_param_set_generic;
158 if (get)
159 param->get = get;
160 else
161 param->get = param_get_generic;
163 param->name = strdup(name);
164 param->flags = flags;
165 list_add_tail(&param->list, &dev->parameters);
167 return param;
171 * dev_add_param - add a parameter to a device
172 * @param dev The device
173 * @param name The name of the parameter
174 * @param set setter function for the parameter
175 * @param get getter function for the parameter
176 * @param flags
178 * This function adds a new parameter to a device. The get/set functions can
179 * be zero in which case the generic functions are used. The generic functions
180 * expect the parameter value to be a string which can be freed with free(). Do
181 * not use static arrays when using the generic functions.
183 int dev_add_param(struct device_d *dev, char *name,
184 int (*set)(struct device_d *dev, struct param_d *p, const char *val),
185 char *(*get)(struct device_d *dev, struct param_d *param),
186 unsigned long flags)
188 struct param_d *param;
190 param = __dev_add_param(dev, name, set, get, flags);
192 return param ? 0 : -EINVAL;
196 * dev_add_param_fixed - add a readonly parameter to a device
197 * @param dev The device
198 * @param name The name of the parameter
199 * @param value The value of the parameter
201 int dev_add_param_fixed(struct device_d *dev, char *name, char *value)
203 struct param_d *param;
205 param = __dev_add_param(dev, name, NULL, NULL, PARAM_FLAG_RO);
206 if (!param)
207 return -EINVAL;
209 param->value = strdup(value);
211 return 0;
215 * dev_remove_parameters - remove all parameters from a device and free their
216 * memory
217 * @param dev The device
219 void dev_remove_parameters(struct device_d *dev)
221 struct param_d *p, *n;
223 list_for_each_entry_safe(p, n, &dev->parameters, list) {
224 p->set(dev, p, NULL);
225 list_del(&p->list);
226 free(p);
230 /** @page dev_params Device parameters
232 @section params_devices Devices can have several parameters.
234 In case of a network device this may be the IP address, networking mask or
235 similar and users need access to these parameters. In barebox this is solved
236 with device paramters. Device parameters are always strings, although there
237 are functions to interpret them as something else. 'hush' users can access
238 parameters as a local variable which have a dot (.) in them. So setting the
239 IP address of the first ethernet device is a matter of typing
240 'eth0.ip=192.168.0.7' on the console and can then be read back with
241 'echo $eth0.ip'. The @ref devinfo_command command shows a summary about all
242 devices currently present. If called with a device id as parameter it shows the
243 parameters available for a device.
245 See the individual functions for parameter programming.