2002-11-03 00:24:07 +00:00
|
|
|
/*
|
2004-01-02 14:00:00 +00:00
|
|
|
* (C) Copyright 2001-2004
|
2002-11-03 00:24:07 +00:00
|
|
|
* Wolfgang Denk, DENX Software Engineering, wd@denx.de.
|
|
|
|
*
|
|
|
|
* See file CREDITS for list of people who contributed to this
|
|
|
|
* project.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of
|
|
|
|
* the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
|
|
|
|
* MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <command.h>
|
2007-07-05 16:01:24 +00:00
|
|
|
#include <driver.h>
|
|
|
|
#include <init.h>
|
2002-11-03 00:24:07 +00:00
|
|
|
#include <net.h>
|
2010-08-26 16:33:28 +00:00
|
|
|
#include <miidev.h>
|
2007-07-05 16:01:48 +00:00
|
|
|
#include <errno.h>
|
2008-08-20 11:21:55 +00:00
|
|
|
#include <malloc.h>
|
2007-07-05 16:01:25 +00:00
|
|
|
|
2007-07-05 16:01:22 +00:00
|
|
|
static struct eth_device *eth_current;
|
|
|
|
|
2009-06-10 17:44:14 +00:00
|
|
|
static LIST_HEAD(netdev_list);
|
|
|
|
|
2007-07-05 16:01:22 +00:00
|
|
|
void eth_set_current(struct eth_device *eth)
|
|
|
|
{
|
2010-05-27 09:37:46 +00:00
|
|
|
if (eth_current && eth_current->active) {
|
|
|
|
eth_current->halt(eth_current);
|
|
|
|
eth_current->active = 0;
|
|
|
|
}
|
|
|
|
|
2007-07-05 16:01:22 +00:00
|
|
|
eth_current = eth;
|
2010-05-27 09:37:46 +00:00
|
|
|
net_update_env();
|
2007-07-05 16:01:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct eth_device * eth_get_current(void)
|
|
|
|
{
|
|
|
|
return eth_current;
|
|
|
|
}
|
|
|
|
|
2009-06-11 13:32:06 +00:00
|
|
|
struct eth_device *eth_get_byname(char *ethname)
|
2009-06-10 17:44:14 +00:00
|
|
|
{
|
|
|
|
struct eth_device *edev;
|
2009-06-11 13:32:06 +00:00
|
|
|
char name[MAX_DRIVER_NAME];
|
2009-06-10 17:44:14 +00:00
|
|
|
|
|
|
|
list_for_each_entry(edev, &netdev_list, list) {
|
2009-06-11 13:32:06 +00:00
|
|
|
sprintf(name, "%s%d", edev->dev.name, edev->dev.id);
|
|
|
|
if (!strcmp(ethname, name))
|
2009-06-10 17:44:14 +00:00
|
|
|
return edev;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-07-05 16:01:43 +00:00
|
|
|
int eth_send(void *packet, int length)
|
2007-07-05 16:01:22 +00:00
|
|
|
{
|
2010-05-27 09:37:46 +00:00
|
|
|
int ret;
|
|
|
|
|
2007-07-05 16:01:22 +00:00
|
|
|
if (!eth_current)
|
2009-02-27 15:36:22 +00:00
|
|
|
return -ENODEV;
|
2007-07-05 16:01:22 +00:00
|
|
|
|
2010-05-27 09:37:46 +00:00
|
|
|
if (!eth_current->active) {
|
|
|
|
ret = eth_current->open(eth_current);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
eth_current->active = 1;
|
|
|
|
}
|
|
|
|
|
2010-12-18 13:37:39 +00:00
|
|
|
led_trigger_network(LED_TRIGGER_NET_TX);
|
|
|
|
|
2007-07-05 16:01:22 +00:00
|
|
|
return eth_current->send(eth_current, packet, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
int eth_rx(void)
|
|
|
|
{
|
2010-05-27 09:37:46 +00:00
|
|
|
int ret;
|
|
|
|
|
2007-07-05 16:01:22 +00:00
|
|
|
if (!eth_current)
|
2009-02-27 15:36:22 +00:00
|
|
|
return -ENODEV;
|
2007-07-05 16:01:22 +00:00
|
|
|
|
2010-05-27 09:37:46 +00:00
|
|
|
if (!eth_current->active) {
|
|
|
|
ret = eth_current->open(eth_current);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
eth_current->active = 1;
|
|
|
|
}
|
|
|
|
|
2007-07-05 16:01:22 +00:00
|
|
|
return eth_current->recv(eth_current);
|
|
|
|
}
|
|
|
|
|
2008-08-20 11:21:55 +00:00
|
|
|
static int eth_set_ethaddr(struct device_d *dev, struct param_d *param, const char *val)
|
|
|
|
{
|
2008-09-30 08:48:40 +00:00
|
|
|
struct eth_device *edev = dev->type_data;
|
2008-08-20 11:21:55 +00:00
|
|
|
char ethaddr[sizeof("xx:xx:xx:xx:xx:xx")];
|
|
|
|
|
2010-06-03 08:13:29 +00:00
|
|
|
if (!val)
|
|
|
|
return dev_param_set_generic(dev, param, NULL);
|
|
|
|
|
2008-08-20 11:21:55 +00:00
|
|
|
if (string_to_ethaddr(val, ethaddr) < 0)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2010-06-03 08:13:29 +00:00
|
|
|
dev_param_set_generic(dev, param, val);
|
2008-08-20 11:21:55 +00:00
|
|
|
|
2008-09-30 08:48:40 +00:00
|
|
|
edev->set_ethaddr(edev, ethaddr);
|
|
|
|
|
2010-05-27 09:37:46 +00:00
|
|
|
if (edev == eth_current)
|
|
|
|
net_update_env();
|
|
|
|
|
2008-08-20 11:21:55 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int eth_set_ipaddr(struct device_d *dev, struct param_d *param, const char *val)
|
|
|
|
{
|
2010-05-27 09:37:46 +00:00
|
|
|
struct eth_device *edev = dev->type_data;
|
2008-08-20 11:21:55 +00:00
|
|
|
IPaddr_t ip;
|
|
|
|
|
2010-06-03 08:13:29 +00:00
|
|
|
if (!val)
|
|
|
|
return dev_param_set_generic(dev, param, NULL);
|
|
|
|
|
2008-08-20 11:21:55 +00:00
|
|
|
if (string_to_ip(val, &ip))
|
|
|
|
return -EINVAL;
|
|
|
|
|
2010-06-03 08:13:29 +00:00
|
|
|
dev_param_set_generic(dev, param, val);
|
2008-08-20 11:21:55 +00:00
|
|
|
|
2010-05-27 09:37:46 +00:00
|
|
|
if (edev == eth_current)
|
|
|
|
net_update_env();
|
|
|
|
|
2008-08-20 11:21:55 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-07-05 16:01:31 +00:00
|
|
|
int eth_register(struct eth_device *edev)
|
2005-10-28 20:30:33 +00:00
|
|
|
{
|
2009-06-11 13:32:06 +00:00
|
|
|
struct device_d *dev = &edev->dev;
|
2007-07-05 16:01:31 +00:00
|
|
|
unsigned char ethaddr_str[20];
|
|
|
|
unsigned char ethaddr[6];
|
|
|
|
|
2007-10-18 09:17:16 +00:00
|
|
|
if (!edev->get_ethaddr) {
|
2011-02-24 14:53:36 +00:00
|
|
|
dev_err(dev, "no get_mac_address found for current eth device\n");
|
2007-07-05 16:01:22 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-06-11 13:32:06 +00:00
|
|
|
strcpy(edev->dev.name, "eth");
|
2011-02-03 12:07:57 +00:00
|
|
|
edev->dev.id = -1;
|
2009-06-11 13:32:06 +00:00
|
|
|
register_device(&edev->dev);
|
|
|
|
|
2009-02-27 00:09:26 +00:00
|
|
|
dev->type_data = edev;
|
2010-06-03 08:13:29 +00:00
|
|
|
dev_add_param(dev, "ipaddr", eth_set_ipaddr, NULL, 0);
|
|
|
|
dev_add_param(dev, "ethaddr", eth_set_ethaddr, NULL, 0);
|
|
|
|
dev_add_param(dev, "gateway", eth_set_ipaddr, NULL, 0);
|
|
|
|
dev_add_param(dev, "netmask", eth_set_ipaddr, NULL, 0);
|
|
|
|
dev_add_param(dev, "serverip", eth_set_ipaddr, NULL, 0);
|
2007-07-05 16:01:25 +00:00
|
|
|
|
2008-09-30 08:48:40 +00:00
|
|
|
edev->init(edev);
|
|
|
|
|
2009-06-10 17:44:14 +00:00
|
|
|
list_add_tail(&edev->list, &netdev_list);
|
|
|
|
|
2007-10-18 09:17:16 +00:00
|
|
|
if (edev->get_ethaddr(edev, ethaddr) == 0) {
|
|
|
|
ethaddr_to_string(ethaddr, ethaddr_str);
|
2011-04-11 10:34:32 +00:00
|
|
|
if (is_valid_ether_addr(ethaddr)) {
|
|
|
|
dev_info(dev, "got MAC address from EEPROM: %s\n", ethaddr_str);
|
|
|
|
dev_set_param(dev, "ethaddr", ethaddr_str);
|
|
|
|
}
|
2007-07-05 16:01:22 +00:00
|
|
|
}
|
|
|
|
|
2010-12-15 08:04:36 +00:00
|
|
|
if (!eth_current) {
|
2009-02-27 00:09:26 +00:00
|
|
|
eth_current = edev;
|
2010-12-15 08:04:36 +00:00
|
|
|
net_update_env();
|
|
|
|
}
|
2007-07-05 16:01:24 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-04-06 16:54:56 +00:00
|
|
|
void eth_unregister(struct eth_device *edev)
|
|
|
|
{
|
2010-06-03 08:13:29 +00:00
|
|
|
dev_remove_parameters(&edev->dev);
|
2009-06-10 17:44:14 +00:00
|
|
|
|
|
|
|
list_del(&edev->list);
|
2009-04-06 16:54:56 +00:00
|
|
|
}
|
|
|
|
|
2010-12-18 13:37:39 +00:00
|
|
|
void led_trigger_network(enum led_trigger trigger)
|
|
|
|
{
|
|
|
|
led_trigger(trigger, TRIGGER_FLASH);
|
|
|
|
led_trigger(LED_TRIGGER_NET_TXRX, TRIGGER_FLASH);
|
|
|
|
}
|