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
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
25 * @brief Handling device specific parameters
34 struct param_d
*get_param_by_name(struct device_d
*dev
, const char *name
)
38 list_for_each_entry(p
, &dev
->parameters
, list
) {
39 if (!strcmp(p
->name
, name
))
47 * dev_get_param - get the value of a parameter
48 * @param dev The device
49 * @param name The name of the parameter
52 const char *dev_get_param(struct device_d
*dev
, const char *name
)
54 struct param_d
*param
= get_param_by_name(dev
, name
);
61 return param
->get(dev
, param
);
65 IPaddr_t
dev_get_param_ip(struct device_d
*dev
, char *name
)
69 if (string_to_ip(dev_get_param(dev
, name
), &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
);
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
;
100 param
= get_param_by_name(dev
, name
);
107 if (param
->flags
& PARAM_FLAG_RO
) {
112 errno
= param
->set(dev
, param
, val
);
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
127 int dev_param_set_generic(struct device_d
*dev
, struct param_d
*p
,
136 p
->value
= strdup(val
);
140 static char *param_get_generic(struct device_d
*dev
, struct param_d
*p
)
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
),
150 struct param_d
*param
;
152 param
= xzalloc(sizeof(*param
));
157 param
->set
= dev_param_set_generic
;
161 param
->get
= param_get_generic
;
163 param
->name
= strdup(name
);
164 param
->flags
= flags
;
165 list_add_tail(¶m
->list
, &dev
->parameters
);
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
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
),
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
);
209 param
->value
= strdup(value
);
215 * dev_remove_parameters - remove all parameters from a device and free their
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
);
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.