driver: add dev_name inline
[barebox-mini2440.git] / net / eth.c
blob7570198a179b0daf5d82ce0b741722627b3af1f9
1 /*
2 * (C) Copyright 2001-2004
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
6 * project.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
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,
21 * MA 02111-1307 USA
24 #include <common.h>
25 #include <command.h>
26 #include <driver.h>
27 #include <init.h>
28 #include <net.h>
29 #include <miiphy.h>
30 #include <errno.h>
31 #include <malloc.h>
33 static struct eth_device *eth_current;
35 static LIST_HEAD(netdev_list);
37 void eth_set_current(struct eth_device *eth)
39 eth_current = eth;
42 struct eth_device * eth_get_current(void)
44 return eth_current;
47 struct eth_device *eth_get_byname(char *ethname)
49 struct eth_device *edev;
50 char name[MAX_DRIVER_NAME];
52 list_for_each_entry(edev, &netdev_list, list) {
53 sprintf(name, "%s%d", edev->dev.name, edev->dev.id);
54 if (!strcmp(ethname, name))
55 return edev;
57 return NULL;
60 int eth_open(void)
62 if (!eth_current)
63 return -ENODEV;
65 return eth_current->open(eth_current);
68 void eth_halt(void)
70 if (!eth_current)
71 return;
73 eth_current->halt(eth_current);
75 eth_current->state = ETH_STATE_PASSIVE;
78 int eth_send(void *packet, int length)
80 if (!eth_current)
81 return -ENODEV;
83 return eth_current->send(eth_current, packet, length);
86 int eth_rx(void)
88 if (!eth_current)
89 return -ENODEV;
91 return eth_current->recv(eth_current);
94 static int eth_set_ethaddr(struct device_d *dev, struct param_d *param, const char *val)
96 struct eth_device *edev = dev->type_data;
97 char ethaddr[sizeof("xx:xx:xx:xx:xx:xx")];
99 if (string_to_ethaddr(val, ethaddr) < 0)
100 return -EINVAL;
102 free(param->value);
103 param->value = strdup(val);
105 edev->set_ethaddr(edev, ethaddr);
107 return 0;
110 static int eth_set_ipaddr(struct device_d *dev, struct param_d *param, const char *val)
112 IPaddr_t ip;
114 if (string_to_ip(val, &ip))
115 return -EINVAL;
117 free(param->value);
118 param->value = strdup(val);
120 return 0;
123 int eth_register(struct eth_device *edev)
125 struct device_d *dev = &edev->dev;
126 unsigned char ethaddr_str[20];
127 unsigned char ethaddr[6];
129 if (!edev->get_ethaddr) {
130 printf("no get_mac_address found for current eth device\n");
131 return -1;
134 strcpy(edev->dev.name, "eth");
135 register_device(&edev->dev);
137 dev->type_data = edev;
138 edev->param_ip.name = "ipaddr";
139 edev->param_ip.set = &eth_set_ipaddr;
140 edev->param_ethaddr.name = "ethaddr";
141 edev->param_ethaddr.set = &eth_set_ethaddr;
142 edev->param_gateway.name = "gateway";
143 edev->param_gateway.set = &eth_set_ipaddr;
144 edev->param_netmask.name = "netmask";
145 edev->param_netmask.set = &eth_set_ipaddr;
146 edev->param_serverip.name = "serverip";
147 edev->param_serverip.set = &eth_set_ipaddr;
148 dev_add_param(dev, &edev->param_ip);
149 dev_add_param(dev, &edev->param_ethaddr);
150 dev_add_param(dev, &edev->param_gateway);
151 dev_add_param(dev, &edev->param_netmask);
152 dev_add_param(dev, &edev->param_serverip);
154 edev->init(edev);
156 list_add_tail(&edev->list, &netdev_list);
158 if (edev->get_ethaddr(edev, ethaddr) == 0) {
159 ethaddr_to_string(ethaddr, ethaddr_str);
160 printf("got MAC address from EEPROM: %s\n",ethaddr_str);
161 dev_set_param(dev, "ethaddr", ethaddr_str);
164 if (!eth_current)
165 eth_current = edev;
167 return 0;
170 void eth_unregister(struct eth_device *edev)
172 if (edev->param_ip.value)
173 free(edev->param_ip.value);
174 if (edev->param_ethaddr.value)
175 free(edev->param_ethaddr.value);
176 if (edev->param_gateway.value)
177 free(edev->param_gateway.value);
178 if (edev->param_netmask.value)
179 free(edev->param_netmask.value);
180 if (edev->param_serverip.value)
181 free(edev->param_serverip.value);
183 if (eth_current == edev)
184 eth_current = NULL;
186 list_del(&edev->list);