[Xenial,snapdragon,1/1] UBUNTU: SAUCE: smsc75xx: read MAC address from file if it exists

Message ID 20180628090102.16636-2-jesse.sung@canonical.com
State New
Headers show
Series
  • [Xenial,snapdragon,1/1] UBUNTU: SAUCE: smsc75xx: read MAC address from file if it exists
Related show

Commit Message

Wen-chien Jesse Sung June 28, 2018, 9:01 a.m.
BugLink: https://launchpad.net/bugs/1779035

When trying to determine which MAC address to use, find if a valid one
is available in the file first.

Signed-off-by: Wen-chien Jesse Sung <jesse.sung@canonical.com>
---
 drivers/net/usb/smsc75xx.c | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)

Comments

Hui Wang July 4, 2018, 3:54 a.m. | #1
Looks fine to me, this change is safe to other platforms which also use 
this driver since they don't have the file in that folder.

Acked-by: Hui Wang <hui.wang@canonical.com>


On 2018年06月28日 17:01, Wen-chien Jesse Sung wrote:
> BugLink: https://launchpad.net/bugs/1779035
>
> When trying to determine which MAC address to use, find if a valid one
> is available in the file first.
>
> Signed-off-by: Wen-chien Jesse Sung <jesse.sung@canonical.com>
> ---
>   drivers/net/usb/smsc75xx.c | 32 ++++++++++++++++++++++++++++++++
>   1 file changed, 32 insertions(+)
>
> diff --git a/drivers/net/usb/smsc75xx.c b/drivers/net/usb/smsc75xx.c
> index 7337e6c0e126..b4352d4f58b1 100644
> --- a/drivers/net/usb/smsc75xx.c
> +++ b/drivers/net/usb/smsc75xx.c
> @@ -29,6 +29,7 @@
>   #include <linux/crc32.h>
>   #include <linux/usb/usbnet.h>
>   #include <linux/slab.h>
> +#include <linux/firmware.h>
>   #include "smsc75xx.h"
>   
>   #define SMSC_CHIPNAME			"smsc75xx"
> @@ -759,8 +760,39 @@ static int smsc75xx_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
>   	return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
>   }
>   
> +#define MAC_BASENAME	"smsc75xx/ethmacaddr"
> +
> +static int smsc75xx_get_hw_mac(struct usbnet *dev)
> +{
> +	const struct firmware *fp = NULL;
> +	int ret;
> +	u8 tmp[32];
> +
> +	snprintf(tmp, sizeof(tmp), "%s-%s", MAC_BASENAME, dev->udev->devpath);
> +	ret = request_firmware(&fp, tmp, &dev->udev->dev);
> +	if (!ret) {
> +		memset(tmp, 0, sizeof(tmp));
> +		memcpy(tmp, fp->data, sizeof(tmp) - 1);
> +		sscanf(tmp, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
> +			&dev->net->dev_addr[0],
> +			&dev->net->dev_addr[1],
> +			&dev->net->dev_addr[2],
> +			&dev->net->dev_addr[3],
> +			&dev->net->dev_addr[4],
> +			&dev->net->dev_addr[5]);
> +
> +		release_firmware(fp);
> +	}
> +	return ret;
> +}
> +
>   static void smsc75xx_init_mac_address(struct usbnet *dev)
>   {
> +	if (!smsc75xx_get_hw_mac(dev) && is_valid_ether_addr(dev->net->dev_addr)) {
> +		netif_dbg(dev, ifup, dev->net, "MAC address read from file\n");
> +		return;
> +	}
> +
>   	/* try reading mac address from EEPROM */
>   	if (smsc75xx_read_eeprom(dev, EEPROM_MAC_OFFSET, ETH_ALEN,
>   			dev->net->dev_addr) == 0) {
Paolo Pisati July 6, 2018, 9:21 a.m. | #2
On Thu, Jun 28, 2018 at 05:01:02PM +0800, Wen-chien Jesse Sung wrote:
> BugLink: https://launchpad.net/bugs/1779035
> 
> When trying to determine which MAC address to use, find if a valid one
> is available in the file first.

Acked-by: Paolo Pisati <paolo.pisati@canonical.com>

Patch

diff --git a/drivers/net/usb/smsc75xx.c b/drivers/net/usb/smsc75xx.c
index 7337e6c0e126..b4352d4f58b1 100644
--- a/drivers/net/usb/smsc75xx.c
+++ b/drivers/net/usb/smsc75xx.c
@@ -29,6 +29,7 @@ 
 #include <linux/crc32.h>
 #include <linux/usb/usbnet.h>
 #include <linux/slab.h>
+#include <linux/firmware.h>
 #include "smsc75xx.h"
 
 #define SMSC_CHIPNAME			"smsc75xx"
@@ -759,8 +760,39 @@  static int smsc75xx_ioctl(struct net_device *netdev, struct ifreq *rq, int cmd)
 	return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
 }
 
+#define MAC_BASENAME	"smsc75xx/ethmacaddr"
+
+static int smsc75xx_get_hw_mac(struct usbnet *dev)
+{
+	const struct firmware *fp = NULL;
+	int ret;
+	u8 tmp[32];
+
+	snprintf(tmp, sizeof(tmp), "%s-%s", MAC_BASENAME, dev->udev->devpath);
+	ret = request_firmware(&fp, tmp, &dev->udev->dev);
+	if (!ret) {
+		memset(tmp, 0, sizeof(tmp));
+		memcpy(tmp, fp->data, sizeof(tmp) - 1);
+		sscanf(tmp, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
+			&dev->net->dev_addr[0],
+			&dev->net->dev_addr[1],
+			&dev->net->dev_addr[2],
+			&dev->net->dev_addr[3],
+			&dev->net->dev_addr[4],
+			&dev->net->dev_addr[5]);
+
+		release_firmware(fp);
+	}
+	return ret;
+}
+
 static void smsc75xx_init_mac_address(struct usbnet *dev)
 {
+	if (!smsc75xx_get_hw_mac(dev) && is_valid_ether_addr(dev->net->dev_addr)) {
+		netif_dbg(dev, ifup, dev->net, "MAC address read from file\n");
+		return;
+	}
+
 	/* try reading mac address from EEPROM */
 	if (smsc75xx_read_eeprom(dev, EEPROM_MAC_OFFSET, ETH_ALEN,
 			dev->net->dev_addr) == 0) {