Linux-USB Archive on lore.kernel.org
 help / color / Atom feed
* [RFC 2/4] Allow cdc_ncm to set MAC address in hardware
@ 2019-08-20 22:21 Charles.Hyde
  2019-08-21  9:39 ` Oliver Neukum
  0 siblings, 1 reply; 2+ messages in thread
From: Charles.Hyde @ 2019-08-20 22:21 UTC (permalink / raw)
  To: linux-usb, linux-acpi; +Cc: gregkh, Mario.Limonciello, oliver, netdev, nic_swsd

This patch adds support for pushing a MAC address out to USB based
ethernet controllers driven by cdc_ncm.  With this change, ifconfig can
now set the device's MAC address.  For example, the Dell Universal Dock
D6000 is driven by cdc_ncm.  The D6000 can now have its MAC address set
by ifconfig, as it can be done in Windows.  This was tested with a D6000
using ifconfig.

Signed-off-by: Charles Hyde <charles.hyde@dellteam.com>
Cc: Mario Limonciello <mario.limonciello@dell.com>
Cc: Oliver Neukum <oliver@neukum.org>
Cc: netdev@vger.kernel.org
Cc: linux-usb@vger.kernel.org
---
 drivers/net/usb/cdc_ncm.c  | 20 +++++++++++++++++++-
 drivers/net/usb/usbnet.c   | 37 ++++++++++++++++++++++++++++---------
 include/linux/usb/usbnet.h |  1 +
 3 files changed, 48 insertions(+), 10 deletions(-)

diff --git a/drivers/net/usb/cdc_ncm.c b/drivers/net/usb/cdc_ncm.c
index 50c05d0f44cb..f77c8672f972 100644
--- a/drivers/net/usb/cdc_ncm.c
+++ b/drivers/net/usb/cdc_ncm.c
@@ -750,6 +750,24 @@ int cdc_ncm_change_mtu(struct net_device *net, int new_mtu)
 }
 EXPORT_SYMBOL_GPL(cdc_ncm_change_mtu);
 
+/* Provide method to push MAC address to the USB device's ethernet controller.
+ */
+int cdc_ncm_set_mac_addr(struct net_device *net, void *p)
+{
+	struct usbnet *dev = netdev_priv(net);
+	struct sockaddr *addr = p;
+
+	memcpy(dev->net->dev_addr, addr->sa_data, ETH_ALEN);
+	/*
+	 * Try to push the MAC address out to the device.  Ignore any errors,
+	 * to be compatible with prior versions of this source.
+	 */
+	usbnet_set_ethernet_addr(dev);
+
+	return eth_mac_addr(net, p);
+}
+EXPORT_SYMBOL_GPL(cdc_ncm_set_mac_addr);
+
 static const struct net_device_ops cdc_ncm_netdev_ops = {
 	.ndo_open	     = usbnet_open,
 	.ndo_stop	     = usbnet_stop,
@@ -757,7 +775,7 @@ static const struct net_device_ops cdc_ncm_netdev_ops = {
 	.ndo_tx_timeout	     = usbnet_tx_timeout,
 	.ndo_get_stats64     = usbnet_get_stats64,
 	.ndo_change_mtu	     = cdc_ncm_change_mtu,
-	.ndo_set_mac_address = eth_mac_addr,
+	.ndo_set_mac_address = cdc_ncm_set_mac_addr,
 	.ndo_validate_addr   = eth_validate_addr,
 };
 
diff --git a/drivers/net/usb/usbnet.c b/drivers/net/usb/usbnet.c
index 72514c46b478..72bdac34b0ee 100644
--- a/drivers/net/usb/usbnet.c
+++ b/drivers/net/usb/usbnet.c
@@ -149,20 +149,39 @@ int usbnet_get_ethernet_addr(struct usbnet *dev, int iMACAddress)
 	int 		tmp = -1, ret;
 	unsigned char	buf [13];
 
-	ret = usb_string(dev->udev, iMACAddress, buf, sizeof buf);
-	if (ret == 12)
-		tmp = hex2bin(dev->net->dev_addr, buf, 6);
-	if (tmp < 0) {
-		dev_dbg(&dev->udev->dev,
-			"bad MAC string %d fetch, %d\n", iMACAddress, tmp);
-		if (ret >= 0)
-			ret = -EINVAL;
-		return ret;
+	ret = usb_get_address(dev->udev, buf);
+	if (ret == 6)
+		memcpy(dev->net->dev_addr, buf, 6);
+	else if (ret < 0) {
+		ret = usb_string(dev->udev, iMACAddress, buf, sizeof buf);
+		if (ret == 12)
+			tmp = hex2bin(dev->net->dev_addr, buf, 6);
+		if (tmp < 0) {
+			dev_dbg(&dev->udev->dev,
+				"bad MAC string %d fetch, %d\n", iMACAddress,
+				tmp);
+			if (ret >= 0)
+				ret = -EINVAL;
+			return ret;
+		}
 	}
 	return 0;
 }
 EXPORT_SYMBOL_GPL(usbnet_get_ethernet_addr);
 
+int usbnet_set_ethernet_addr(struct usbnet *dev)
+{
+	int ret;
+
+	ret = usb_set_address(dev->udev, dev->net->dev_addr);
+	if (ret < 0) {
+		dev_dbg(&dev->udev->dev,
+			"bad MAC address put, %d\n", ret);
+	}
+	return ret;
+}
+EXPORT_SYMBOL_GPL(usbnet_set_ethernet_addr);
+
 static void intr_complete (struct urb *urb)
 {
 	struct usbnet	*dev = urb->context;
diff --git a/include/linux/usb/usbnet.h b/include/linux/usb/usbnet.h
index d8860f2d0976..f2b2c5ab5493 100644
--- a/include/linux/usb/usbnet.h
+++ b/include/linux/usb/usbnet.h
@@ -258,6 +258,7 @@ extern int usbnet_change_mtu(struct net_device *net, int new_mtu);
 
 extern int usbnet_get_endpoints(struct usbnet *, struct usb_interface *);
 extern int usbnet_get_ethernet_addr(struct usbnet *, int);
+extern int usbnet_set_ethernet_addr(struct usbnet *);
 extern void usbnet_defer_kevent(struct usbnet *, int);
 extern void usbnet_skb_return(struct usbnet *, struct sk_buff *);
 extern void usbnet_unlink_rx_urbs(struct usbnet *);
-- 
2.20.1

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [RFC 2/4] Allow cdc_ncm to set MAC address in hardware
  2019-08-20 22:21 [RFC 2/4] Allow cdc_ncm to set MAC address in hardware Charles.Hyde
@ 2019-08-21  9:39 ` Oliver Neukum
  0 siblings, 0 replies; 2+ messages in thread
From: Oliver Neukum @ 2019-08-21  9:39 UTC (permalink / raw)
  To: Charles.Hyde, linux-acpi, linux-usb
  Cc: Mario.Limonciello, gregkh, nic_swsd, netdev

Am Dienstag, den 20.08.2019, 22:21 +0000 schrieb
Charles.Hyde@dellteam.com:
> This patch adds support for pushing a MAC address out to USB based
> ethernet controllers driven by cdc_ncm.  With this change, ifconfig can
> now set the device's MAC address.  For example, the Dell Universal Dock
> D6000 is driven by cdc_ncm.  The D6000 can now have its MAC address set
> by ifconfig, as it can be done in Windows.  This was tested with a D6000
> using ifconfig.

On a design note, it looks like you broke S4 with the driver.
Suspend To Disk will cut power and hence reset the MAC to default.
You need to reset it to the user's setting in reset_resume().
Please add that to usbnet.

> 
> Signed-off-by: Charles Hyde <charles.hyde@dellteam.com>
> Cc: Mario Limonciello <mario.limonciello@dell.com>
> Cc: Oliver Neukum <oliver@neukum.org>
> Cc: netdev@vger.kernel.org
> Cc: linux-usb@vger.kernel.org
> ---
>  drivers/net/usb/cdc_ncm.c  | 20 +++++++++++++++++++-
>  drivers/net/usb/usbnet.c   | 37 ++++++++++++++++++++++++++++---------
>  include/linux/usb/usbnet.h |  1 +
>  3 files changed, 48 insertions(+), 10 deletions(-)
> 
> diff --git a/drivers/net/usb/cdc_ncm.c b/drivers/net/usb/cdc_ncm.c
> index 50c05d0f44cb..f77c8672f972 100644
> --- a/drivers/net/usb/cdc_ncm.c
> +++ b/drivers/net/usb/cdc_ncm.c
> @@ -750,6 +750,24 @@ int cdc_ncm_change_mtu(struct net_device *net, int new_mtu)
>  }
>  EXPORT_SYMBOL_GPL(cdc_ncm_change_mtu);
>  
> +/* Provide method to push MAC address to the USB device's ethernet controller.
> + */
> +int cdc_ncm_set_mac_addr(struct net_device *net, void *p)
> +{
> +	struct usbnet *dev = netdev_priv(net);
> +	struct sockaddr *addr = p;
> +
> +	memcpy(dev->net->dev_addr, addr->sa_data, ETH_ALEN);
> +	/*
> +	 * Try to push the MAC address out to the device.  Ignore any errors,
> +	 * to be compatible with prior versions of this source.
> +	 */
> +	usbnet_set_ethernet_addr(dev);
> +
> +	return eth_mac_addr(net, p);
> +}
> +EXPORT_SYMBOL_GPL(cdc_ncm_set_mac_addr);
> +
>  static const struct net_device_ops cdc_ncm_netdev_ops = {
>  	.ndo_open	     = usbnet_open,
>  	.ndo_stop	     = usbnet_stop,
> @@ -757,7 +775,7 @@ static const struct net_device_ops cdc_ncm_netdev_ops = {
>  	.ndo_tx_timeout	     = usbnet_tx_timeout,
>  	.ndo_get_stats64     = usbnet_get_stats64,
>  	.ndo_change_mtu	     = cdc_ncm_change_mtu,
> -	.ndo_set_mac_address = eth_mac_addr,
> +	.ndo_set_mac_address = cdc_ncm_set_mac_addr,

Why can't this fully go into usbnet?

>  	.ndo_validate_addr   = eth_validate_addr,
>  };
>  
> diff --git a/drivers/net/usb/usbnet.c b/drivers/net/usb/usbnet.c
> index 72514c46b478..72bdac34b0ee 100644
> --- a/drivers/net/usb/usbnet.c
> +++ b/drivers/net/usb/usbnet.c
> @@ -149,20 +149,39 @@ int usbnet_get_ethernet_addr(struct usbnet *dev, int iMACAddress)
>  	int 		tmp = -1, ret;
>  	unsigned char	buf [13];
>  
> -	ret = usb_string(dev->udev, iMACAddress, buf, sizeof buf);
> -	if (ret == 12)
> -		tmp = hex2bin(dev->net->dev_addr, buf, 6);
> -	if (tmp < 0) {
> -		dev_dbg(&dev->udev->dev,
> -			"bad MAC string %d fetch, %d\n", iMACAddress, tmp);
> -		if (ret >= 0)
> -			ret = -EINVAL;
> -		return ret;
> +	ret = usb_get_address(dev->udev, buf);
> +	if (ret == 6)

If you mean ETH_ALEN, you should use it.

> +		memcpy(dev->net->dev_addr, buf, 6);
> +	else if (ret < 0) {
> +		ret = usb_string(dev->udev, iMACAddress, buf, sizeof buf);
> +		if (ret == 12)
> +			tmp = hex2bin(dev->net->dev_addr, buf, 6);
> +		if (tmp < 0) {
> +			dev_dbg(&dev->udev->dev,
> +				"bad MAC string %d fetch, %d\n", iMACAddress,
> +				tmp);
> +			if (ret >= 0)
> +				ret = -EINVAL;
> +			return ret;

Again, you cannot ignore the possibility of getting fewer or more than
6 bytes.

> +		}
>  	}
>  	return 0;
>  }
>  EXPORT_SYMBOL_GPL(usbnet_get_ethernet_addr);
>  
> +int usbnet_set_ethernet_addr(struct usbnet *dev)
> +{
> +	int ret;
> +
> +	ret = usb_set_address(dev->udev, dev->net->dev_addr);
> +	if (ret < 0) {
> +		dev_dbg(&dev->udev->dev,
> +			"bad MAC address put, %d\n", ret);
> +	}
> +	return ret;
> +}
> +EXPORT_SYMBOL_GPL(usbnet_set_ethernet_addr);

What is the purpose of this wrapper?

	Regards
		Oliver


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, back to index

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-08-20 22:21 [RFC 2/4] Allow cdc_ncm to set MAC address in hardware Charles.Hyde
2019-08-21  9:39 ` Oliver Neukum

Linux-USB Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-usb/0 linux-usb/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-usb linux-usb/ https://lore.kernel.org/linux-usb \
		linux-usb@vger.kernel.org
	public-inbox-index linux-usb

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-usb


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git