From patchwork Fri Jun 10 15:04:08 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 99905 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from theia.denx.de (theia.denx.de [85.214.87.163]) by ozlabs.org (Postfix) with ESMTP id BFEB0B701A for ; Sat, 11 Jun 2011 01:05:10 +1000 (EST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 7C9FF28331; Fri, 10 Jun 2011 17:04:55 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id JNRZD4BDhgjq; Fri, 10 Jun 2011 17:04:55 +0200 (CEST) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id CD21E282FB; Fri, 10 Jun 2011 17:04:49 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id DCA97282FB for ; Fri, 10 Jun 2011 17:04:47 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id C8D-LFbaRIV8 for ; Fri, 10 Jun 2011 17:04:46 +0200 (CEST) X-policyd-weight: NOT_IN_SBL_XBL_SPAMHAUS=-1.5 NOT_IN_SPAMCOP=-1.5 NOT_IN_BL_NJABL=-1.5 (only DNSBL check requested) Received: from smtp-out.google.com (smtp-out.google.com [216.239.44.51]) by theia.denx.de (Postfix) with ESMTPS id B102E282C4 for ; Fri, 10 Jun 2011 17:04:44 +0200 (CEST) Received: from kpbe11.cbf.corp.google.com (kpbe11.cbf.corp.google.com [172.25.105.75]) by smtp-out.google.com with ESMTP id p5AF4TSM024794; Fri, 10 Jun 2011 08:04:29 -0700 Received: from sglass.mtv.corp.google.com (sglass.mtv.corp.google.com [172.22.72.144]) by kpbe11.cbf.corp.google.com with ESMTP id p5AF4PXk015410; Fri, 10 Jun 2011 08:04:25 -0700 Received: by sglass.mtv.corp.google.com (Postfix, from userid 121222) id 27B73140310; Fri, 10 Jun 2011 08:04:25 -0700 (PDT) From: Simon Glass To: U-Boot Mailing List Date: Fri, 10 Jun 2011 08:04:08 -0700 Message-Id: <1307718251-15947-3-git-send-email-sjg@chromium.org> X-Mailer: git-send-email 1.7.3.1 In-Reply-To: <1307718251-15947-1-git-send-email-sjg@chromium.org> References: <1307718251-15947-1-git-send-email-sjg@chromium.org> X-System-Of-Record: true Cc: Andy Fleming Subject: [U-Boot] [PATCH v7 RESEND 2/5] Add Ethernet hardware MAC address framework to usbnet X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.9 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: u-boot-bounces@lists.denx.de Errors-To: u-boot-bounces@lists.denx.de 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. Changes for v2: - eth_set_hwaddr -> eth_write_hwaddr - tided up other users of eth_getenv_enetaddr_by_index() Changes for v5: - Changed NULL to "eth" in eth_getenv_enetaddr_by_index() API Signed-off-by: Simon Glass --- board/davinci/common/misc.c | 2 +- drivers/net/designware.c | 2 +- drivers/usb/eth/usb_ether.c | 9 +++++- include/net.h | 25 ++++++++++++++++- net/eth.c | 64 ++++++++++++++++++++++++++----------------- 5 files changed, 72 insertions(+), 30 deletions(-) diff --git a/board/davinci/common/misc.c b/board/davinci/common/misc.c index 2bfdf23..53d6aa1 100644 --- a/board/davinci/common/misc.c +++ b/board/davinci/common/misc.c @@ -101,7 +101,7 @@ void davinci_sync_env_enetaddr(uint8_t *rom_enetaddr) { uint8_t env_enetaddr[6]; - eth_getenv_enetaddr_by_index(0, env_enetaddr); + eth_getenv_enetaddr_by_index("eth", 0, env_enetaddr); if (!memcmp(env_enetaddr, "\0\0\0\0\0\0", 6)) { /* There is no MAC address in the environment, so we initialize * it from the value in the EEPROM. */ diff --git a/drivers/net/designware.c b/drivers/net/designware.c index 3f5eeb7..02ba393 100644 --- a/drivers/net/designware.c +++ b/drivers/net/designware.c @@ -500,7 +500,7 @@ int designware_initialize(u32 id, ulong base_addr, u32 phy_addr) dev->iobase = (int)base_addr; dev->priv = priv; - eth_getenv_enetaddr_by_index(id, &dev->enetaddr[0]); + eth_getenv_enetaddr_by_index("eth", id, &dev->enetaddr[0]); priv->dev = dev; priv->mac_regs_p = (struct eth_mac_regs *)base_addr; diff --git a/drivers/usb/eth/usb_ether.c b/drivers/usb/eth/usb_ether.c index 7b55da3..6565ea5 100644 --- a/drivers/usb/eth/usb_ether.c +++ b/drivers/usb/eth/usb_ether.c @@ -80,6 +80,7 @@ int is_eth_dev_on_usb_host(void) */ static void probe_valid_drivers(struct usb_device *dev) { + struct eth_device *eth; int j; for (j = 0; prob_dev[j].probe && prob_dev[j].get_info; j++) { @@ -88,9 +89,10 @@ static void probe_valid_drivers(struct usb_device *dev) /* * ok, it is a supported eth device. Get info and fill it in */ + eth = &usb_eth[usb_max_eth_dev].eth_dev; if (prob_dev[j].get_info(dev, &usb_eth[usb_max_eth_dev], - &usb_eth[usb_max_eth_dev].eth_dev)) { + eth)) { /* found proper driver */ /* register with networking stack */ usb_max_eth_dev++; @@ -100,7 +102,10 @@ static void probe_valid_drivers(struct usb_device *dev) * call since eth_current_changed (internally called) * relies on it */ - eth_register(&usb_eth[usb_max_eth_dev - 1].eth_dev); + eth_register(eth); + if (eth_write_hwaddr(eth, "usbeth", + usb_max_eth_dev - 1)) + puts("Warning: failed to set MAC address\n"); break; } } diff --git a/include/net.h b/include/net.h index 018a744..ce54825 100644 --- a/include/net.h +++ b/include/net.h @@ -128,7 +128,18 @@ extern int eth_get_dev_index (void); /* get the device index */ extern void eth_parse_enetaddr(const char *addr, uchar *enetaddr); extern int eth_getenv_enetaddr(char *name, uchar *enetaddr); extern int eth_setenv_enetaddr(char *name, const uchar *enetaddr); -extern int eth_getenv_enetaddr_by_index(int index, uchar *enetaddr); + +/* + * Get the hardware address for an ethernet interface . + * Args: + * base_name - base name for device (normally "eth") + * index - device index number (0 for first) + * enetaddr - returns 6 byte hardware address + * Returns: + * Return true if the address is valid. + */ +extern int eth_getenv_enetaddr_by_index(const char *base_name, int index, + uchar *enetaddr); extern int usb_eth_initialize(bd_t *bi); extern int eth_init(bd_t *bis); /* Initialize the device */ @@ -141,6 +152,18 @@ extern int eth_rx(void); /* Check for received packets */ extern void eth_halt(void); /* stop SCC */ extern char *eth_get_name(void); /* get name of current device */ +/* + * Set the hardware address for an ethernet interface based on 'eth%daddr' + * environment variable (or just 'ethaddr' if eth_number is 0). + * Args: + * base_name - base name for device (normally "eth") + * eth_number - value of %d (0 for first device of this type) + * Returns: + * 0 is success, non-zero is error status from driver. + */ +int eth_write_hwaddr(struct eth_device *dev, const char *base_name, + int eth_number); + #ifdef CONFIG_MCAST_TFTP int eth_mcast_join( IPaddr_t mcast_addr, u8 join); u32 ether_crc (size_t len, unsigned char const *p); diff --git a/net/eth.c b/net/eth.c index 6523834..b3ea565 100644 --- a/net/eth.c +++ b/net/eth.c @@ -54,10 +54,11 @@ int eth_setenv_enetaddr(char *name, const uchar *enetaddr) return setenv(name, buf); } -int eth_getenv_enetaddr_by_index(int index, uchar *enetaddr) +int eth_getenv_enetaddr_by_index(const char *base_name, int index, + uchar *enetaddr) { char enetvar[32]; - sprintf(enetvar, index ? "eth%daddr" : "ethaddr", index); + sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index); return eth_getenv_enetaddr(enetvar, enetaddr); } @@ -188,6 +189,38 @@ static void eth_current_changed(void) #endif } +int eth_write_hwaddr(struct eth_device *dev, const char *base_name, + int eth_number) +{ + unsigned char env_enetaddr[6]; + int ret = 0; + + if (!eth_getenv_enetaddr_by_index(base_name, eth_number, env_enetaddr)) + return -1; + + if (memcmp(env_enetaddr, "\0\0\0\0\0\0", 6)) { + if (memcmp(dev->enetaddr, "\0\0\0\0\0\0", 6) && + memcmp(dev->enetaddr, env_enetaddr, 6)) { + printf("\nWarning: %s MAC addresses don't match:\n", + dev->name); + printf("Address in SROM is %pM\n", + dev->enetaddr); + printf("Address in environment is %pM\n", + env_enetaddr); + } + + memcpy(dev->enetaddr, env_enetaddr, 6); + } + + if (dev->write_hwaddr && + !eth_mac_skip(eth_number) && + is_valid_ether_addr(dev->enetaddr)) { + ret = dev->write_hwaddr(dev); + } + + return ret; +} + int eth_register(struct eth_device *dev) { struct eth_device *d; @@ -208,7 +241,6 @@ int eth_register(struct eth_device *dev) int eth_initialize(bd_t *bis) { - unsigned char env_enetaddr[6]; int eth_number = 0; eth_devices = NULL; @@ -264,27 +296,8 @@ int eth_initialize(bd_t *bis) if (strchr(dev->name, ' ')) puts("\nWarning: eth device name has a space!\n"); - eth_getenv_enetaddr_by_index(eth_number, env_enetaddr); - - if (memcmp(env_enetaddr, "\0\0\0\0\0\0", 6)) { - if (memcmp(dev->enetaddr, "\0\0\0\0\0\0", 6) && - memcmp(dev->enetaddr, env_enetaddr, 6)) - { - printf ("\nWarning: %s MAC addresses don't match:\n", - dev->name); - printf ("Address in SROM is %pM\n", - dev->enetaddr); - printf ("Address in environment is %pM\n", - env_enetaddr); - } - - memcpy(dev->enetaddr, env_enetaddr, 6); - } - if (dev->write_hwaddr && - !eth_mac_skip(eth_number) && - is_valid_ether_addr(dev->enetaddr)) { - dev->write_hwaddr(dev); - } + if (eth_write_hwaddr(dev, NULL, eth_number)) + puts("Warning: failed to set MAC address\n"); eth_number++; dev = dev->next; @@ -359,7 +372,8 @@ int eth_init(bd_t *bis) do { uchar env_enetaddr[6]; - if (eth_getenv_enetaddr_by_index(eth_number, env_enetaddr)) + if (eth_getenv_enetaddr_by_index("eth", eth_number, + env_enetaddr)) memcpy(dev->enetaddr, env_enetaddr, 6); ++eth_number;