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
)
39 if (eth_current
&& eth_current
->active
) {
40 eth_current
->halt(eth_current
);
41 eth_current
->active
= 0;
48 struct eth_device
* eth_get_current(void)
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
))
66 int eth_send(void *packet
, int length
)
73 if (!eth_current
->active
) {
74 ret
= eth_current
->open(eth_current
);
77 eth_current
->active
= 1;
80 return eth_current
->send(eth_current
, packet
, length
);
90 if (!eth_current
->active
) {
91 ret
= eth_current
->open(eth_current
);
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")];
106 return dev_param_set_generic(dev
, param
, NULL
);
108 if (string_to_ethaddr(val
, ethaddr
) < 0)
111 dev_param_set_generic(dev
, param
, val
);
113 edev
->set_ethaddr(edev
, ethaddr
);
115 if (edev
== eth_current
)
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
;
127 return dev_param_set_generic(dev
, param
, NULL
);
129 if (string_to_ip(val
, &ip
))
132 dev_param_set_generic(dev
, param
, val
);
134 if (edev
== eth_current
)
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");
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);
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",ðaddr_str
);
168 dev_set_param(dev
, "ethaddr", ethaddr_str
);
177 void eth_unregister(struct eth_device
*edev
)
179 dev_remove_parameters(&edev
->dev
);
181 list_del(&edev
->list
);