Realtek 网卡 支持RK vendor mac地址写入

简介: Realtek 网卡 支持RK vendor mac地址写入

首先先放上代码

Index: kernel-5.10/drivers/net/ethernet/realtek/r8169_main.c
===================================================================
--- kernel-5.10/drivers/net/ethernet/realtek/r8169_main.c    (版本 3026)
+++ kernel-5.10/drivers/net/ethernet/realtek/r8169_main.c    (版本 3027)
@@ -29,7 +29,7 @@
 #include <linux/prefetch.h>
 #include <linux/ipv6.h>
 #include <net/ip6_checksum.h>
-
+#include <linux/soc/rockchip/rk_vendor_storage.h>
 #include "r8169.h"
 #include "r8169_firmware.h"

@@ -88,7 +88,7 @@
 #define JUMBO_6K    (6 * SZ_1K - VLAN_ETH_HLEN - ETH_FCS_LEN)
 #define JUMBO_7K    (7 * SZ_1K - VLAN_ETH_HLEN - ETH_FCS_LEN)
 #define JUMBO_9K    (9 * SZ_1K - VLAN_ETH_HLEN - ETH_FCS_LEN)
-
+u8 eth1_mac_addr[6] = {0,0,0,0,0,0};
 static const struct {
     const char *name;
     const char *fw_name;
@@ -5312,7 +5312,8 @@
     struct net_device *dev = tp->dev;
     u8 *mac_addr = dev->dev_addr;
     int rc;
-
+    int ret;
+    struct sockaddr sa;
     rc = eth_platform_get_mac_address(tp_to_dev(tp), mac_addr);
     if (!rc)
         goto done;
@@ -5321,11 +5322,26 @@
     if (is_valid_ether_addr(mac_addr))
         goto done;

-    rtl_read_mac_from_reg(tp, mac_addr, MAC0);
-    if (is_valid_ether_addr(mac_addr))
-        goto done;
+    // rtl_read_mac_from_reg(tp, mac_addr, MAC0);
+    // if (is_valid_ether_addr(mac_addr))
+        // goto done;

-    eth_hw_addr_random(dev);
+    // eth_hw_addr_random(dev);
+     ret = rk_vendor_read(LAN_MAC_ID, eth1_mac_addr, 6);
+     if (ret != 6 || is_zero_ether_addr(eth1_mac_addr)) {
+        printk("longjiang %s: rk_vendor_read eth mac address failed (%d)",
+                    __func__, ret);
+        random_ether_addr(eth1_mac_addr);
+        printk("longjiang %s: generate random eth mac address: %02x:%02x:%02x:%02x:%02x:%02x",
+                    __func__, eth1_mac_addr[0], eth1_mac_addr[1], eth1_mac_addr[2],
+                    eth1_mac_addr[3], eth1_mac_addr[4], eth1_mac_addr[5]);
+        }
+    eth1_mac_addr[0] &= 0xfc;
+    ether_addr_copy(dev->dev_addr,eth1_mac_addr);
+
+    ether_addr_copy(sa.sa_data, dev->dev_addr);
+
+    ret = rtl_set_mac_address(dev, &sa);     
     dev_warn(tp_to_dev(tp), "can't read MAC address, setting random one\n");
 done:
     rtl_rar_set(tp, mac_addr);
@@ -5533,4 +5549,14 @@
 #endif
 };

-module_pci_driver(rtl8169_pci_driver);
+static int __init pci_rtl8169_init(void)
+{
+       return pci_register_driver(&rtl8169_pci_driver);
+}
+static void __exit pci_rtl8169_exit(void)
+{
+       pci_unregister_driver(&rtl8169_pci_driver);
+}
+late_initcall(pci_rtl8169_init);
+module_exit(pci_rtl8169_exit);
+//module_pci_driver(rtl8169_pci_driver);

需要注意的点有
1.不要使用module_pci_driver加载驱动,需要使用late_initcall延迟加载
2.pci_unregister_driver pci_register_driver 用于加载pci驱动 定义在kernel-5.10\include\linux\pci.h 如果是USB网卡 需要使用usb_register usb_deregister 他们定义在kernel-5.10\include\linux\usb.h

usb驱动修改如下:



---a/drivers/soc/rockchip/sdmmc_vendor_storage.c
+++ b/drivers/soc/rockchip/sdmmc_vendor_storage.c
@@ -592,15 +592,15 @@ static struct miscdevice vender_storage_dev = {

 static int vendor_init_thread(void *arg)
 {
-       int ret, try_count = 5;
+       int ret, try_count = 50;

        do {
                ret = emmc_vendor_storage_init();
                if (!ret) {
                        break;
                }
-               /* sleep 500ms wait emmc initialize completed */
-               msleep(500);
+               /* sleep 50ms wait emmc initialize completed */
+               msleep(50);
        } while (try_count--);

        if (!ret) {



diff --git a/drivers/net/usb/r8152.c b/drivers/net/usb/r8152.c
index 26cc7c1..2fd5e09 100755
--- a/drivers/net/usb/r8152.c
+++ b/drivers/net/usb/r8152.c
@@ -28,6 +28,7 @@
 #include <linux/suspend.h>

 #include "compatibility.h"
+u8 eth1_mac_addr[6] = {0,0,0,0,0,0};

 /* Version Information */
 #define DRIVER_VERSION "v2.12.0 (2019/04/29)"
@@ -1243,6 +1244,7 @@ static int set_ethernet_addr(struct r8152 *tp)
     if (ret < 0) {
         netif_err(tp, probe, dev, "Get ether addr fail\n");
     } else if (!is_valid_ether_addr(sa.sa_data)) {
+    #if 0
         netif_err(tp, probe, dev, "Invalid ether addr %pM\n",
               sa.sa_data);
         eth_hw_addr_random(dev);
@@ -1250,6 +1252,23 @@ static int set_ethernet_addr(struct r8152 *tp)
         ret = rtl8152_set_mac_address(dev, &sa);
         netif_info(tp, probe, dev, "Random ether addr %pM\n",
                sa.sa_data);
+    #else
+     ret = rk_vendor_read(BT_MAC_ID, eth1_mac_addr, 6);
+     if (ret != 6 || is_zero_ether_addr(eth1_mac_addr)) {
+        dev_err(dev, "%s: rk_vendor_read eth mac address failed (%d)",
+                    __func__, ret);
+        random_ether_addr(eth1_mac_addr);
+        dev_err(dev, "%s: generate random eth mac address: %02x:%02x:%02x:%02x:%02x:%02x",
+                    __func__, eth1_mac_addr[0], eth1_mac_addr[1], eth1_mac_addr[2],
+                    eth1_mac_addr[3], eth1_mac_addr[4], eth1_mac_addr[5]);
+        }
+    eth1_mac_addr[0] &= 0xfc;
+    ether_addr_copy(dev->dev_addr,eth1_mac_addr);
+    ether_addr_copy(sa.sa_data, dev->dev_addr);
+    ret = rtl8152_set_mac_address(dev, &sa);
+        netif_info(tp, probe, dev, "eth1 addr %pM\n",
+               sa.sa_data);
+        
+    #endif
     } else {
         if (tp->version == RTL_VER_01)
             ether_addr_copy(dev->dev_addr, sa.sa_data);




????1: ??vendor read ? vendor storage????,????BT_MAC, ???????:

hcq@ubuntu:~/RK3399_ANDROID7.1-Industry-SDK_V1.0/kernel$ git diff -p  drivers/net/usb/r8152.c 
diff --git a/drivers/net/usb/r8152.c b/drivers/net/usb/r8152.c
index 26cc7c1..8483f70 100755
--- a/drivers/net/usb/r8152.c
+++ b/drivers/net/usb/r8152.c
@@ -13004,7 +13004,20 @@ static struct usb_driver rtl8152_driver = {
 #endif
 };

-module_usb_driver(rtl8152_driver);
+//module_usb_driver(rtl8152_driver);
+
+static int __init usb_rtl8152_init(void)
+{
+       return usb_register(&rtl8152_driver);
+}
+
+static void __exit usb_rtl8152_exit(void)
+{
+       usb_deregister(&rtl8152_driver);
+}
+
+late_initcall(usb_rtl8152_init);
+module_exit(usb_rtl8152_exit);

 MODULE_AUTHOR(DRIVER_AUTHOR);
 MODULE_DESCRIPTION(DRIVER_DESC);
目录
相关文章
|
Linux
Linux下修改网卡MAC地址
Linux下修改网卡MAC地址
231 0
|
Ubuntu Linux Perl
linux实用技巧:使用脚本获取活动网卡的mac地址
linux实用技巧:使用脚本获取活动网卡的mac地址
linux实用技巧:使用脚本获取活动网卡的mac地址
|
Windows
Windows 技术篇-网卡物理(MAC)地址查看方法
Windows 技术篇-网卡物理(MAC)地址查看方法
264 0
Windows 技术篇-网卡物理(MAC)地址查看方法
|
Linux
linux更改网卡的mac地址
更改网卡的mac地址一块网卡的mac地址在出厂时已经写入,有时候是写入可以擦写的rom中,但是要有相当的专业技术或专业的设备。对于一些绑定了mac地址,更换了设备后又不想更换mac的,我们可以修改OS读取的MAC来达到我们的目的。
920 0
|
Python
Python 获取 网卡 MAC 地址
/*********************************************************************** * Python 获取 网卡 MAC 地址 * 说明: * 记录一下Python如何获取网卡MAC地址,主要用于数据唯一性保存。
1292 0