Commit 7616e7850804c7c69e0a22c179dfcba9e8f3f587
Committed by
Remy Bohmer
1 parent
291391bed5
Exists in
master
and in
54 other branches
Add Ethernet hardware MAC address framework to usbnet
Built-in Ethernet adapters support setting the mac address by means of a ethaddr environment variable for each interface (ethaddr, eth1addr, eth2addr). This adds similar support to the USB network side, using the names usbethaddr, usbeth1addr, etc. They are kept separate since we don't want a USB device taking the MAC address of a built-in device or vice versa. Signed-off-by: Simon Glass <sjg@chromium.org> Tested-by: Eric Bénard <eric@eukrea.com>
Showing 5 changed files with 72 additions and 30 deletions Side-by-side Diff
board/davinci/common/misc.c
... | ... | @@ -101,7 +101,7 @@ |
101 | 101 | { |
102 | 102 | uint8_t env_enetaddr[6]; |
103 | 103 | |
104 | - eth_getenv_enetaddr_by_index(0, env_enetaddr); | |
104 | + eth_getenv_enetaddr_by_index("eth", 0, env_enetaddr); | |
105 | 105 | if (!memcmp(env_enetaddr, "\0\0\0\0\0\0", 6)) { |
106 | 106 | /* There is no MAC address in the environment, so we initialize |
107 | 107 | * it from the value in the EEPROM. */ |
drivers/net/designware.c
... | ... | @@ -504,7 +504,7 @@ |
504 | 504 | dev->iobase = (int)base_addr; |
505 | 505 | dev->priv = priv; |
506 | 506 | |
507 | - eth_getenv_enetaddr_by_index(id, &dev->enetaddr[0]); | |
507 | + eth_getenv_enetaddr_by_index("eth", id, &dev->enetaddr[0]); | |
508 | 508 | |
509 | 509 | priv->dev = dev; |
510 | 510 | priv->mac_regs_p = (struct eth_mac_regs *)base_addr; |
drivers/usb/eth/usb_ether.c
... | ... | @@ -80,6 +80,7 @@ |
80 | 80 | */ |
81 | 81 | static void probe_valid_drivers(struct usb_device *dev) |
82 | 82 | { |
83 | + struct eth_device *eth; | |
83 | 84 | int j; |
84 | 85 | |
85 | 86 | for (j = 0; prob_dev[j].probe && prob_dev[j].get_info; j++) { |
86 | 87 | |
... | ... | @@ -88,9 +89,10 @@ |
88 | 89 | /* |
89 | 90 | * ok, it is a supported eth device. Get info and fill it in |
90 | 91 | */ |
92 | + eth = &usb_eth[usb_max_eth_dev].eth_dev; | |
91 | 93 | if (prob_dev[j].get_info(dev, |
92 | 94 | &usb_eth[usb_max_eth_dev], |
93 | - &usb_eth[usb_max_eth_dev].eth_dev)) { | |
95 | + eth)) { | |
94 | 96 | /* found proper driver */ |
95 | 97 | /* register with networking stack */ |
96 | 98 | usb_max_eth_dev++; |
... | ... | @@ -100,7 +102,10 @@ |
100 | 102 | * call since eth_current_changed (internally called) |
101 | 103 | * relies on it |
102 | 104 | */ |
103 | - eth_register(&usb_eth[usb_max_eth_dev - 1].eth_dev); | |
105 | + eth_register(eth); | |
106 | + if (eth_write_hwaddr(eth, "usbeth", | |
107 | + usb_max_eth_dev - 1)) | |
108 | + puts("Warning: failed to set MAC address\n"); | |
104 | 109 | break; |
105 | 110 | } |
106 | 111 | } |
include/net.h
... | ... | @@ -128,8 +128,19 @@ |
128 | 128 | extern void eth_parse_enetaddr(const char *addr, uchar *enetaddr); |
129 | 129 | extern int eth_getenv_enetaddr(char *name, uchar *enetaddr); |
130 | 130 | extern int eth_setenv_enetaddr(char *name, const uchar *enetaddr); |
131 | -extern int eth_getenv_enetaddr_by_index(int index, uchar *enetaddr); | |
132 | 131 | |
132 | +/* | |
133 | + * Get the hardware address for an ethernet interface . | |
134 | + * Args: | |
135 | + * base_name - base name for device (normally "eth") | |
136 | + * index - device index number (0 for first) | |
137 | + * enetaddr - returns 6 byte hardware address | |
138 | + * Returns: | |
139 | + * Return true if the address is valid. | |
140 | + */ | |
141 | +extern int eth_getenv_enetaddr_by_index(const char *base_name, int index, | |
142 | + uchar *enetaddr); | |
143 | + | |
133 | 144 | extern int usb_eth_initialize(bd_t *bi); |
134 | 145 | extern int eth_init(bd_t *bis); /* Initialize the device */ |
135 | 146 | extern int eth_send(volatile void *packet, int length); /* Send a packet */ |
... | ... | @@ -140,6 +151,18 @@ |
140 | 151 | extern int eth_rx(void); /* Check for received packets */ |
141 | 152 | extern void eth_halt(void); /* stop SCC */ |
142 | 153 | extern char *eth_get_name(void); /* get name of current device */ |
154 | + | |
155 | +/* | |
156 | + * Set the hardware address for an ethernet interface based on 'eth%daddr' | |
157 | + * environment variable (or just 'ethaddr' if eth_number is 0). | |
158 | + * Args: | |
159 | + * base_name - base name for device (normally "eth") | |
160 | + * eth_number - value of %d (0 for first device of this type) | |
161 | + * Returns: | |
162 | + * 0 is success, non-zero is error status from driver. | |
163 | + */ | |
164 | +int eth_write_hwaddr(struct eth_device *dev, const char *base_name, | |
165 | + int eth_number); | |
143 | 166 | |
144 | 167 | #ifdef CONFIG_MCAST_TFTP |
145 | 168 | int eth_mcast_join( IPaddr_t mcast_addr, u8 join); |
net/eth.c
... | ... | @@ -54,10 +54,11 @@ |
54 | 54 | return setenv(name, buf); |
55 | 55 | } |
56 | 56 | |
57 | -int eth_getenv_enetaddr_by_index(int index, uchar *enetaddr) | |
57 | +int eth_getenv_enetaddr_by_index(const char *base_name, int index, | |
58 | + uchar *enetaddr) | |
58 | 59 | { |
59 | 60 | char enetvar[32]; |
60 | - sprintf(enetvar, index ? "eth%daddr" : "ethaddr", index); | |
61 | + sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index); | |
61 | 62 | return eth_getenv_enetaddr(enetvar, enetaddr); |
62 | 63 | } |
63 | 64 | |
... | ... | @@ -188,6 +189,38 @@ |
188 | 189 | #endif |
189 | 190 | } |
190 | 191 | |
192 | +int eth_write_hwaddr(struct eth_device *dev, const char *base_name, | |
193 | + int eth_number) | |
194 | +{ | |
195 | + unsigned char env_enetaddr[6]; | |
196 | + int ret = 0; | |
197 | + | |
198 | + if (!eth_getenv_enetaddr_by_index(base_name, eth_number, env_enetaddr)) | |
199 | + return -1; | |
200 | + | |
201 | + if (memcmp(env_enetaddr, "\0\0\0\0\0\0", 6)) { | |
202 | + if (memcmp(dev->enetaddr, "\0\0\0\0\0\0", 6) && | |
203 | + memcmp(dev->enetaddr, env_enetaddr, 6)) { | |
204 | + printf("\nWarning: %s MAC addresses don't match:\n", | |
205 | + dev->name); | |
206 | + printf("Address in SROM is %pM\n", | |
207 | + dev->enetaddr); | |
208 | + printf("Address in environment is %pM\n", | |
209 | + env_enetaddr); | |
210 | + } | |
211 | + | |
212 | + memcpy(dev->enetaddr, env_enetaddr, 6); | |
213 | + } | |
214 | + | |
215 | + if (dev->write_hwaddr && | |
216 | + !eth_mac_skip(eth_number) && | |
217 | + is_valid_ether_addr(dev->enetaddr)) { | |
218 | + ret = dev->write_hwaddr(dev); | |
219 | + } | |
220 | + | |
221 | + return ret; | |
222 | +} | |
223 | + | |
191 | 224 | int eth_register(struct eth_device *dev) |
192 | 225 | { |
193 | 226 | struct eth_device *d; |
... | ... | @@ -208,7 +241,6 @@ |
208 | 241 | |
209 | 242 | int eth_initialize(bd_t *bis) |
210 | 243 | { |
211 | - unsigned char env_enetaddr[6]; | |
212 | 244 | int eth_number = 0; |
213 | 245 | |
214 | 246 | eth_devices = NULL; |
215 | 247 | |
... | ... | @@ -264,28 +296,9 @@ |
264 | 296 | if (strchr(dev->name, ' ')) |
265 | 297 | puts("\nWarning: eth device name has a space!\n"); |
266 | 298 | |
267 | - eth_getenv_enetaddr_by_index(eth_number, env_enetaddr); | |
299 | + if (eth_write_hwaddr(dev, NULL, eth_number)) | |
300 | + puts("Warning: failed to set MAC address\n"); | |
268 | 301 | |
269 | - if (memcmp(env_enetaddr, "\0\0\0\0\0\0", 6)) { | |
270 | - if (memcmp(dev->enetaddr, "\0\0\0\0\0\0", 6) && | |
271 | - memcmp(dev->enetaddr, env_enetaddr, 6)) | |
272 | - { | |
273 | - printf ("\nWarning: %s MAC addresses don't match:\n", | |
274 | - dev->name); | |
275 | - printf ("Address in SROM is %pM\n", | |
276 | - dev->enetaddr); | |
277 | - printf ("Address in environment is %pM\n", | |
278 | - env_enetaddr); | |
279 | - } | |
280 | - | |
281 | - memcpy(dev->enetaddr, env_enetaddr, 6); | |
282 | - } | |
283 | - if (dev->write_hwaddr && | |
284 | - !eth_mac_skip(eth_number) && | |
285 | - is_valid_ether_addr(dev->enetaddr)) { | |
286 | - dev->write_hwaddr(dev); | |
287 | - } | |
288 | - | |
289 | 302 | eth_number++; |
290 | 303 | dev = dev->next; |
291 | 304 | } while(dev != eth_devices); |
... | ... | @@ -359,7 +372,8 @@ |
359 | 372 | do { |
360 | 373 | uchar env_enetaddr[6]; |
361 | 374 | |
362 | - if (eth_getenv_enetaddr_by_index(eth_number, env_enetaddr)) | |
375 | + if (eth_getenv_enetaddr_by_index("eth", eth_number, | |
376 | + env_enetaddr)) | |
363 | 377 | memcpy(dev->enetaddr, env_enetaddr, 6); |
364 | 378 | |
365 | 379 | ++eth_number; |