9
0
Fork 0
barebox/net/eth.c

166 lines
3.6 KiB
C
Raw Normal View History

2002-11-03 00:24:07 +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>
#include <miiphy.h>
#include <malloc.h>
2007-07-05 16:01:31 +00:00
#include <asm-generic/errno.h>
typedef enum eth_cookies {
PARAM_IP,
PARAM_MAC,
PARAM_GW,
PARAM_NM,
} eth_cookies_t;
2007-07-05 16:01:31 +00:00
static char *eth_get(struct device_d* dev, struct param_d *param)
{
2007-07-05 16:01:31 +00:00
struct eth_device *edev = dev->priv;
char *buf;
switch (param->cookie) {
case PARAM_MAC:
buf = malloc(18);
enet_addr_to_string(edev->enetaddr, buf);
break;
default:
buf = strdup((char *)param->val);
}
2007-07-05 16:01:31 +00:00
return buf;
}
2007-07-05 16:01:31 +00:00
static int eth_set(struct device_d* dev, struct param_d *param, char *newval)
{
2007-07-05 16:01:31 +00:00
struct eth_device *edev = dev->priv;
int ret;
switch (param->cookie) {
case PARAM_MAC:
if (string_to_enet_addr(newval, edev->enetaddr))
return -EINVAL;
if ((ret = edev->set_mac_address(edev, newval)))
return ret;
/* Fall through */
default:
if (param->val)
free(param->val);
param->val = strdup(newval);
}
return 0;
}
static struct param_d eth_params[] = {
{ .name = "ip", .cookie = PARAM_IP, .set = eth_set, .get = eth_get},
{ .name = "mac", .cookie = PARAM_MAC, .set = eth_set, .get = eth_get},
{ .name = "gateway", .cookie = PARAM_GW, .set = eth_set, .get = eth_get},
{ .name = "netmask", .cookie = PARAM_NM, .set = eth_set, .get = eth_get},
};
2002-11-03 00:24:07 +00:00
static struct eth_device *eth_current;
void eth_set_current(struct eth_device *eth)
{
eth_current = eth;
}
struct eth_device * eth_get_current(void)
{
return eth_current;
}
2007-07-05 16:01:24 +00:00
int eth_init(void)
{
if (!eth_current)
return 0;
2007-07-05 16:01:24 +00:00
eth_current->open(eth_current);
return 1;
}
void eth_halt(void)
{
if (!eth_current)
return;
eth_current->halt(eth_current);
eth_current->state = ETH_STATE_PASSIVE;
}
int eth_send(volatile void *packet, int length)
{
if (!eth_current)
return -1;
return eth_current->send(eth_current, packet, length);
}
int eth_rx(void)
{
if (!eth_current)
return -1;
return eth_current->recv(eth_current);
}
2007-07-05 16:01:31 +00:00
int eth_register(struct eth_device *edev)
2005-10-28 20:30:33 +00:00
{
2007-07-05 16:01:31 +00:00
struct device_d *dev = edev->dev;
unsigned char ethaddr_str[20];
unsigned char ethaddr[6];
int i;
2007-07-05 16:01:31 +00:00
printf("%s\n",__FUNCTION__);
if (!edev->get_mac_address) {
printf("no get_mac_address found for current eth device\n");
return -1;
}
for (i = 0; i < 4; i++)
dev_add_parameter(dev, &eth_params[i]);
2007-07-05 16:01:31 +00:00
if (edev->get_mac_address(edev, ethaddr) == 0) {
sprintf (ethaddr_str, "%02X:%02X:%02X:%02X:%02X:%02X",
ethaddr[0], ethaddr[1], ethaddr[2], ethaddr[3], ethaddr[4], ethaddr[5]);
2007-07-05 16:01:31 +00:00
printf("got MAC address from EEPROM: %s\n",ethaddr_str);
dev_set_param(dev, "mac", ethaddr);
memcpy(edev->enetaddr, ethaddr, 6);
}
2007-07-05 16:01:31 +00:00
eth_current = edev;
2007-07-05 16:01:24 +00:00
return 0;
}