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
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,
33 static struct eth_device
*eth_current
;
35 static LIST_HEAD(netdev_list
);
37 void eth_set_current(struct eth_device
*eth
)
42 struct eth_device
* eth_get_current(void)
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
))
65 return eth_current
->open(eth_current
);
73 eth_current
->halt(eth_current
);
75 eth_current
->state
= ETH_STATE_PASSIVE
;
78 int eth_send(void *packet
, int length
)
83 return eth_current
->send(eth_current
, packet
, length
);
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)
103 param
->value
= strdup(val
);
105 edev
->set_ethaddr(edev
, ethaddr
);
110 static int eth_set_ipaddr(struct device_d
*dev
, struct param_d
*param
, const char *val
)
114 if (string_to_ip(val
, &ip
))
118 param
->value
= strdup(val
);
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");
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
= ð_set_ipaddr
;
140 edev
->param_ethaddr
.name
= "ethaddr";
141 edev
->param_ethaddr
.set
= ð_set_ethaddr
;
142 edev
->param_gateway
.name
= "gateway";
143 edev
->param_gateway
.set
= ð_set_ipaddr
;
144 edev
->param_netmask
.name
= "netmask";
145 edev
->param_netmask
.set
= ð_set_ipaddr
;
146 edev
->param_serverip
.name
= "serverip";
147 edev
->param_serverip
.set
= ð_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
);
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
);
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
)
186 list_del(&edev
->list
);