Add basic support for mini2440 board to barebox.
[barebox-mini2440.git] / net / eth.c
blobcfb0fefe54035d81b31edaf7f3fa26860d5c083a
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 <miidev.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 if (eth_current && eth_current->active) {
40 eth_current->halt(eth_current);
41 eth_current->active = 0;
44 eth_current = eth;
45 net_update_env();
48 struct eth_device * eth_get_current(void)
50 return eth_current;
53 struct eth_device *eth_get_byname(char *ethname)
55 struct eth_device *edev;
56 char name[MAX_DRIVER_NAME];
58 list_for_each_entry(edev, &netdev_list, list) {
59 sprintf(name, "%s%d", edev->dev.name, edev->dev.id);
60 if (!strcmp(ethname, name))
61 return edev;
63 return NULL;
66 int eth_send(void *packet, int length)
68 int ret;
70 if (!eth_current)
71 return -ENODEV;
73 if (!eth_current->active) {
74 ret = eth_current->open(eth_current);
75 if (ret)
76 return ret;
77 eth_current->active = 1;
80 return eth_current->send(eth_current, packet, length);
83 int eth_rx(void)
85 int ret;
87 if (!eth_current)
88 return -ENODEV;
90 if (!eth_current->active) {
91 ret = eth_current->open(eth_current);
92 if (ret)
93 return ret;
94 eth_current->active = 1;
97 return eth_current->recv(eth_current);
100 static int eth_set_ethaddr(struct device_d *dev, struct param_d *param, const char *val)
102 struct eth_device *edev = dev->type_data;
103 char ethaddr[sizeof("xx:xx:xx:xx:xx:xx")];
105 if (!val)
106 return dev_param_set_generic(dev, param, NULL);
108 if (string_to_ethaddr(val, ethaddr) < 0)
109 return -EINVAL;
111 dev_param_set_generic(dev, param, val);
113 edev->set_ethaddr(edev, ethaddr);
115 if (edev == eth_current)
116 net_update_env();
118 return 0;
121 static int eth_set_ipaddr(struct device_d *dev, struct param_d *param, const char *val)
123 struct eth_device *edev = dev->type_data;
124 IPaddr_t ip;
126 if (!val)
127 return dev_param_set_generic(dev, param, NULL);
129 if (string_to_ip(val, &ip))
130 return -EINVAL;
132 dev_param_set_generic(dev, param, val);
134 if (edev == eth_current)
135 net_update_env();
137 return 0;
140 int eth_register(struct eth_device *edev)
142 struct device_d *dev = &edev->dev;
143 unsigned char ethaddr_str[20];
144 unsigned char ethaddr[6];
146 if (!edev->get_ethaddr) {
147 printf("no get_mac_address found for current eth device\n");
148 return -1;
151 strcpy(edev->dev.name, "eth");
152 register_device(&edev->dev);
154 dev->type_data = edev;
155 dev_add_param(dev, "ipaddr", eth_set_ipaddr, NULL, 0);
156 dev_add_param(dev, "ethaddr", eth_set_ethaddr, NULL, 0);
157 dev_add_param(dev, "gateway", eth_set_ipaddr, NULL, 0);
158 dev_add_param(dev, "netmask", eth_set_ipaddr, NULL, 0);
159 dev_add_param(dev, "serverip", eth_set_ipaddr, NULL, 0);
161 edev->init(edev);
163 list_add_tail(&edev->list, &netdev_list);
165 if (edev->get_ethaddr(edev, ethaddr) == 0) {
166 ethaddr_to_string(ethaddr, ethaddr_str);
167 printf("got MAC address from EEPROM: %s\n",&ethaddr_str);
168 dev_set_param(dev, "ethaddr", ethaddr_str);
171 if (!eth_current)
172 eth_current = edev;
174 return 0;
177 void eth_unregister(struct eth_device *edev)
179 dev_remove_parameters(&edev->dev);
181 list_del(&edev->list);