driver: add dev_name inline
[barebox-mini2440.git] / lib / parameter.c
blob1f683b2684ba54476bd0a2f71f9f6fb8acc68e22
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 *param = dev->param;
38 while (param) {
39 if (!strcmp(param->name, name))
40 return param;
41 param = param->next;
44 return NULL;
47 const char *dev_get_param(struct device_d *dev, const char *name)
49 struct param_d *param = get_param_by_name(dev, name);
51 if (!param) {
52 errno = -EINVAL;
53 return NULL;
56 if (param->get)
57 return param->get(dev, param);
59 return param->value;
62 #ifdef CONFIG_NET
63 IPaddr_t dev_get_param_ip(struct device_d *dev, char *name)
65 IPaddr_t ip;
67 if (string_to_ip(dev_get_param(dev, name), &ip))
68 return 0;
70 return ip;
73 int dev_set_param_ip(struct device_d *dev, char *name, IPaddr_t ip)
75 char ipstr[sizeof("xxx.xxx.xxx.xxx")];
77 ip_to_string(ip, ipstr);
79 return dev_set_param(dev, name, ipstr);
81 #endif
83 int dev_set_param(struct device_d *dev, const char *name, const char *val)
85 struct param_d *param;
87 if (!dev) {
88 errno = -ENODEV;
89 return -ENODEV;
92 param = get_param_by_name(dev, name);
94 if (!param) {
95 errno = -EINVAL;
96 return -EINVAL;
99 if (param->flags & PARAM_FLAG_RO) {
100 errno = -EACCES;
101 return -EACCES;
104 if (param->set) {
105 errno = param->set(dev, param, val);
106 return errno;
109 if (param->value)
110 free(param->value);
112 param->value = strdup(val);
113 return 0;
116 int dev_add_param(struct device_d *dev, struct param_d *newparam)
118 struct param_d *param = dev->param;
120 newparam->next = NULL;
122 if (param) {
123 while (param->next)
124 param = param->next;
125 param->next = newparam;
126 } else {
127 dev->param = newparam;
130 return 0;
133 /** @page dev_params Device parameters
135 @section params_devices Devices can have several parameters.
137 In case of a network device this may be the IP address, networking mask or
138 similar and users need access to these parameters. In U-Boot this is solved
139 with device paramters. Device parameters are always strings, although there
140 are functions to interpret them as something else. 'hush' users can access
141 parameters as a local variable which have a dot (.) in them. So setting the
142 IP address of the first ethernet device is a matter of typing
143 'eth0.ip=192.168.0.7' on the console and can then be read back with
144 'echo $eth0.ip'. The @ref devinfo_command command shows a summary about all
145 devices currently present. If called with a device id as parameter it shows the
146 parameters available for a device.
148 @section params_programming Device parameters programming API
150 @code
151 struct param_d {
152 char* (*get)(struct device_d *, struct param_d *param);
153 int (*set)(struct device_d *, struct param_d *param, const char *val);
154 ulong flags;
155 char *name;
156 struct param_d *next;
157 char *value;
159 @endcode
161 @code
162 int dev_add_param(struct device_d *dev, struct param_d *newparam);
163 @endcode
165 This function adds a new parameter to a device. At least the name field in
166 the new parameter struct has to be initialized. The 'get' and 'set' fields
167 can be set to NULL in which case the framework handles them. It is also
168 allowed to implement only one of the get/set functions. Care must be taken
169 with the initial value of the parameter. If the framework handles the set
170 function it will try to free the value of the parameter. If this is a
171 static array bad things will happen. A parameter can have the flag
172 PARAM_FLAG_RO which means that the parameter is readonly. It is perfectly ok
173 then to point the value to a static array.
175 @code
176 const char *dev_get_param(struct device_d *dev, const char *name);
177 @endcode
179 This function returns a pointer to the value of the parameter specified
180 with dev and name.
181 If the framework handles the get/set functions the parameter value strings
182 are alloceted with malloc and freed with free when another value is set for
183 this parameter. Drivers implementing set/get themselves are allowed to
184 return values in static arrays. This means that the pointers returned from
185 dev_get_param() are only valid until the next call to dev_get_param. If this
186 is not long enough strdup() or similar must be used.
188 @code
189 int dev_set_param(struct device_d *dev, const char *name, const char *val);
190 @endcode
192 Set the value of a parameter.