目录

如何分析其他系统代码来适配设备

分析OpenWRT代码

下面以OpenWRT中的TP-Link WR841n v8的代码为例。

首先下载OpenWRT代码,然后进入target/linux/ar71xx/files/arch/mips/ath79目录。

这里一个mach-tl-wr841n-v8.c文件,就是针对该设备的代码。如下

/*
 *  TP-LINK TL-WR841N/ND v8/TL-MR3420 v2 board support
 *
 *  Copyright (C) 2012 Gabor Juhos <juhosg@openwrt.org>
 *
 *  This program is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License version 2 as published
 *  by the Free Software Foundation.
 */

#include <linux/gpio.h>
#include <linux/platform_device.h>

#include <asm/mach-ath79/ath79.h>
#include <asm/mach-ath79/ar71xx_regs.h>

#include "common.h"
#include "dev-eth.h"
#include "dev-gpio-buttons.h"
#include "dev-leds-gpio.h"
#include "dev-m25p80.h"
#include "dev-usb.h"
#include "dev-wmac.h"
#include "machtypes.h"

#define TL_WR841NV8_GPIO_LED_WLAN   13
#define TL_WR841NV8_GPIO_LED_QSS    15
#define TL_WR841NV8_GPIO_LED_WAN    18
#define TL_WR841NV8_GPIO_LED_LAN1   19
#define TL_WR841NV8_GPIO_LED_LAN2   20
#define TL_WR841NV8_GPIO_LED_LAN3   21
#define TL_WR841NV8_GPIO_LED_LAN4   12
#define TL_WR841NV8_GPIO_LED_SYSTEM 14

#define TL_WR841NV8_GPIO_BTN_RESET  17
#define TL_WR841NV8_GPIO_SW_RFKILL  16  /* WPS for MR3420 v2 */

#define TL_MR3420V2_GPIO_LED_3G 11
#define TL_MR3420V2_GPIO_USB_POWER  4

#define TL_WR941NDV5_GPIO_LED_WLAN  13
#define TL_WR941NDV5_GPIO_LED_QSS   15
#define TL_WR941NDV5_GPIO_LED_WAN   18
#define TL_WR941NDV5_GPIO_LED_LAN1  19
#define TL_WR941NDV5_GPIO_LED_LAN2  20
#define TL_WR941NDV5_GPIO_LED_LAN3  2
#define TL_WR941NDV5_GPIO_LED_LAN4  3
#define TL_WR941NDV5_GPIO_LED_SYSTEM    14

#define TL_WR841NV8_KEYS_POLL_INTERVAL  20  /* msecs */
#define TL_WR841NV8_KEYS_DEBOUNCE_INTERVAL (3 * TL_WR841NV8_KEYS_POLL_INTERVAL)

static const char *tl_wr841n_v8_part_probes[] = {
    "tp-link",
    NULL,
};

static struct flash_platform_data tl_wr841n_v8_flash_data = {
    .part_probes    = tl_wr841n_v8_part_probes,
};

static struct gpio_led tl_wr841n_v8_leds_gpio[] __initdata = {
    {
        .name       = "tp-link:green:lan1",
        .gpio       = TL_WR841NV8_GPIO_LED_LAN1,
        .active_low = 1,
    }, {
        .name       = "tp-link:green:lan2",
        .gpio       = TL_WR841NV8_GPIO_LED_LAN2,
        .active_low = 1,
    }, {
        .name       = "tp-link:green:lan3",
        .gpio       = TL_WR841NV8_GPIO_LED_LAN3,
        .active_low = 1,
    }, {
        .name       = "tp-link:green:lan4",
        .gpio       = TL_WR841NV8_GPIO_LED_LAN4,
        .active_low = 1,
    }, {
        .name       = "tp-link:green:qss",
        .gpio       = TL_WR841NV8_GPIO_LED_QSS,
        .active_low = 1,
    }, {
        .name       = "tp-link:green:system",
        .gpio       = TL_WR841NV8_GPIO_LED_SYSTEM,
        .active_low = 1,
    }, {
        .name       = "tp-link:green:wan",
        .gpio       = TL_WR841NV8_GPIO_LED_WAN,
        .active_low = 1,
    }, {
        .name       = "tp-link:green:wlan",
        .gpio       = TL_WR841NV8_GPIO_LED_WLAN,
        .active_low = 1,
    }, {
        /* the 3G LED is only present on the MR3420 v2 */
        .name       = "tp-link:green:3g",
        .gpio       = TL_MR3420V2_GPIO_LED_3G,
        .active_low = 1,
    },
};

static struct gpio_keys_button tl_wr841n_v8_gpio_keys[] __initdata = {
    {
        .desc       = "Reset button",
        .type       = EV_KEY,
        .code       = KEY_RESTART,
        .debounce_interval = TL_WR841NV8_KEYS_DEBOUNCE_INTERVAL,
        .gpio       = TL_WR841NV8_GPIO_BTN_RESET,
        .active_low = 1,
    }, {
        .desc       = "RFKILL switch",
        .type       = EV_SW,
        .code       = KEY_RFKILL,
        .debounce_interval = TL_WR841NV8_KEYS_DEBOUNCE_INTERVAL,
        .gpio       = TL_WR841NV8_GPIO_SW_RFKILL,
        .active_low = 0,
    }
};

static struct gpio_keys_button tl_mr3420v2_gpio_keys[] __initdata = {
    {
        .desc       = "Reset button",
        .type       = EV_KEY,
        .code       = KEY_RESTART,
        .debounce_interval = TL_WR841NV8_KEYS_DEBOUNCE_INTERVAL,
        .gpio       = TL_WR841NV8_GPIO_BTN_RESET,
        .active_low = 1,
    }, {
        .desc       = "WPS",
        .type       = EV_KEY,
        .code       = KEY_WPS_BUTTON,
        .debounce_interval = TL_WR841NV8_KEYS_DEBOUNCE_INTERVAL,
        .gpio       = TL_WR841NV8_GPIO_SW_RFKILL,
        .active_low = 0,
    }
};

static struct gpio_led tl_wr941nd_v5_leds_gpio[] __initdata = {
    {
        .name       = "tp-link:green:lan1",
        .gpio       = TL_WR941NDV5_GPIO_LED_LAN1,
        .active_low = 1,
    }, {
        .name       = "tp-link:green:lan2",
        .gpio       = TL_WR941NDV5_GPIO_LED_LAN2,
        .active_low = 1,
    }, {
        .name       = "tp-link:green:lan3",
        .gpio       = TL_WR941NDV5_GPIO_LED_LAN3,
        .active_low = 1,
    }, {
        .name       = "tp-link:green:lan4",
        .gpio       = TL_WR941NDV5_GPIO_LED_LAN4,
        .active_low = 1,
    }, {
        .name       = "tp-link:green:qss",
        .gpio       = TL_WR941NDV5_GPIO_LED_QSS,
        .active_low = 1,
    }, {
        .name       = "tp-link:green:system",
        .gpio       = TL_WR941NDV5_GPIO_LED_SYSTEM,
        .active_low = 1,
    }, {
        .name       = "tp-link:green:wan",
        .gpio       = TL_WR941NDV5_GPIO_LED_WAN,
        .active_low = 1,
    }, {
        .name       = "tp-link:green:wlan",
        .gpio       = TL_WR941NDV5_GPIO_LED_WLAN,
        .active_low = 1,
    },
};

static void __init tl_ap123_setup(void)
{
    u8 *mac = (u8 *) KSEG1ADDR(0x1f01fc00);
    u8 *ee = (u8 *) KSEG1ADDR(0x1fff1000);

    /* Disable JTAG, enabling GPIOs 0-3 */
    /* Configure OBS4 line, for GPIO 4*/
    ath79_gpio_function_setup(AR934X_GPIO_FUNC_JTAG_DISABLE,
                 AR934X_GPIO_FUNC_CLK_OBS4_EN);

    /* config gpio4 as normal gpio function */
    ath79_gpio_output_select(TL_MR3420V2_GPIO_USB_POWER,
                 AR934X_GPIO_OUT_GPIO);

    ath79_register_m25p80(&tl_wr841n_v8_flash_data);

    ath79_setup_ar934x_eth_cfg(AR934X_ETH_CFG_SW_PHY_SWAP);

    ath79_register_mdio(1, 0x0);

    ath79_init_mac(ath79_eth0_data.mac_addr, mac, -1);
    ath79_init_mac(ath79_eth1_data.mac_addr, mac, 0);

    /* GMAC0 is connected to the PHY0 of the internal switch */
    ath79_switch_data.phy4_mii_en = 1;
    ath79_switch_data.phy_poll_mask = BIT(0);
    ath79_eth0_data.phy_if_mode = PHY_INTERFACE_MODE_MII;
    ath79_eth0_data.phy_mask = BIT(0);
    ath79_eth0_data.mii_bus_dev = &ath79_mdio1_device.dev;
    ath79_register_eth(0);

    /* GMAC1 is connected to the internal switch */
    ath79_eth1_data.phy_if_mode = PHY_INTERFACE_MODE_GMII;
    ath79_register_eth(1);

    ath79_register_wmac(ee, mac);
}

static void __init tl_wr841n_v8_setup(void)
{
    tl_ap123_setup();

    ath79_register_leds_gpio(-1, ARRAY_SIZE(tl_wr841n_v8_leds_gpio) - 1,
                 tl_wr841n_v8_leds_gpio);

    ath79_register_gpio_keys_polled(1, TL_WR841NV8_KEYS_POLL_INTERVAL,
                    ARRAY_SIZE(tl_wr841n_v8_gpio_keys),
                    tl_wr841n_v8_gpio_keys);
}

MIPS_MACHINE(ATH79_MACH_TL_WR841N_V8, "TL-WR841N-v8", "TP-LINK TL-WR841N/ND v8",
         tl_wr841n_v8_setup);


static void __init tl_wr842n_v2_setup(void)
{
    tl_ap123_setup();

    ath79_register_leds_gpio(-1, ARRAY_SIZE(tl_wr841n_v8_leds_gpio),
                 tl_wr841n_v8_leds_gpio);

    ath79_register_gpio_keys_polled(1, TL_WR841NV8_KEYS_POLL_INTERVAL,
                    ARRAY_SIZE(tl_wr841n_v8_gpio_keys),
                    tl_wr841n_v8_gpio_keys);

    gpio_request_one(TL_MR3420V2_GPIO_USB_POWER,
             GPIOF_OUT_INIT_HIGH | GPIOF_EXPORT_DIR_FIXED,
             "USB power");

    ath79_register_usb();
}

static void __init tl_mr3420v2_setup(void)
{
    tl_ap123_setup();

    ath79_register_leds_gpio(-1, ARRAY_SIZE(tl_wr841n_v8_leds_gpio),
                tl_wr841n_v8_leds_gpio);

    ath79_register_gpio_keys_polled(1, TL_WR841NV8_KEYS_POLL_INTERVAL,
                ARRAY_SIZE(tl_mr3420v2_gpio_keys),
                tl_mr3420v2_gpio_keys);

    /* enable power for the USB port */
    gpio_request_one(TL_MR3420V2_GPIO_USB_POWER,
             GPIOF_OUT_INIT_HIGH | GPIOF_EXPORT_DIR_FIXED,
             "USB power");

    ath79_register_usb();
}

MIPS_MACHINE(ATH79_MACH_TL_MR3420_V2, "TL-MR3420-v2", "TP-LINK TL-MR3420 v2",
         tl_mr3420v2_setup);


static void __init tl_wr941nd_v5_setup(void)
{
    tl_ap123_setup();

    ath79_register_leds_gpio(-1, ARRAY_SIZE(tl_wr941nd_v5_leds_gpio),
                 tl_wr941nd_v5_leds_gpio);

    ath79_register_gpio_keys_polled(1, TL_WR841NV8_KEYS_POLL_INTERVAL,
                    ARRAY_SIZE(tl_wr841n_v8_gpio_keys),
                    tl_wr841n_v8_gpio_keys);
}

MIPS_MACHINE(ATH79_MACH_TL_WR941ND_V5, "TL-WR941ND-v5", "TP-LINK TL-WR941N/ND v5",
         tl_wr941nd_v5_setup);

这里需要提取的信息在几个部分:

最开始部分关于GPIO定义用的宏

也就是:

#define TL_WR841NV8_GPIO_LED_WLAN   13
#define TL_WR841NV8_GPIO_LED_QSS    15
#define TL_WR841NV8_GPIO_LED_WAN    18
#define TL_WR841NV8_GPIO_LED_LAN1   19
#define TL_WR841NV8_GPIO_LED_LAN2   20
#define TL_WR841NV8_GPIO_LED_LAN3   21
#define TL_WR841NV8_GPIO_LED_LAN4   12
#define TL_WR841NV8_GPIO_LED_SYSTEM 14

这里说明了设备的网口的lED GPIO是哪些。当然,这个是针对841nv8的,文件里还定义了WR941NDV5的,你也可以找一下。

还一段是:

#define TL_WR841NV8_GPIO_BTN_RESET  17
#define TL_WR841NV8_GPIO_SW_RFKILL  16  /* WPS for MR3420 v2 */

定义了Reset键的GPIO,另一个则是MR3420 v2中的WPS键。

它们其实都通过struct gpio_led来实际使用,如果没这些宏定义,你可以尝试找一下这里怎么设的GPIO。

然后需要找到ART和MAC地址的位置

    u8 *mac = (u8 *) KSEG1ADDR(0x1f01fc00);
    u8 *ee = (u8 *) KSEG1ADDR(0x1fff1000);

mac和ee这两个变量用在了ath79_init_mac()和ath79_register_wmac()中,分别表示该设备MAC地址在Flash中的位置,和ART在Flash中的位置。

因为ART要传给wlan的注册换函数,这里是ath79_register_wmac()。

发现更多其它设备

当然,在这个文件不只支持了wr841n v8,还有其它很型号的设备,它们都定义在MIPS_MACHINE()中。

在这里,他们的GPIO不同,所以,在调用ath79_register_leds_gpio()和ath79_register_gpio_keys_polled()时,使用了不同的struct gpio_led和struct gpio_keys_button对应的变量,这里有:tl_wr841n_v8_leds_gpio、tl_wr841n_v8_gpio_keys、tl_mr3420v2_gpio_keys、tl_wr941nd_v5_leds_gpio。

通过看变量中具体使用的不同的宏,就知道设备对应了哪个GPIO。如:

static void __init tl_wr941nd_v5_setup(void)
{
    tl_ap123_setup();

    ath79_register_leds_gpio(-1, ARRAY_SIZE(tl_wr941nd_v5_leds_gpio),
                 tl_wr941nd_v5_leds_gpio);

    ath79_register_gpio_keys_polled(1, TL_WR841NV8_KEYS_POLL_INTERVAL,
                    ARRAY_SIZE(tl_wr841n_v8_gpio_keys),
                    tl_wr841n_v8_gpio_keys);
}

MIPS_MACHINE(ATH79_MACH_TL_WR941ND_V5, "TL-WR941ND-v5", "TP-LINK TL-WR941N/ND v5",
         tl_wr941nd_v5_setup);

...

static struct gpio_keys_button tl_wr841n_v8_gpio_keys[] __initdata = {
    {
        .desc       = "Reset button",
        .type       = EV_KEY,
        .code       = KEY_RESTART,
        .debounce_interval = TL_WR841NV8_KEYS_DEBOUNCE_INTERVAL,
        .gpio       = TL_WR841NV8_GPIO_BTN_RESET,
        .active_low = 1,
    }, {
        .desc       = "RFKILL switch",
        .type       = EV_SW,
        .code       = KEY_RFKILL,
        .debounce_interval = TL_WR841NV8_KEYS_DEBOUNCE_INTERVAL,
        .gpio       = TL_WR841NV8_GPIO_SW_RFKILL,
        .active_low = 0,
    }
};

static struct gpio_led tl_wr941nd_v5_leds_gpio[] __initdata = {
    {
        .name       = "tp-link:green:lan1",
        .gpio       = TL_WR941NDV5_GPIO_LED_LAN1,
        .active_low = 1,
    }, {
        .name       = "tp-link:green:lan2",
        .gpio       = TL_WR941NDV5_GPIO_LED_LAN2,
        .active_low = 1,
    }, {
        .name       = "tp-link:green:lan3",
        .gpio       = TL_WR941NDV5_GPIO_LED_LAN3,
        .active_low = 1,
    }, {
        .name       = "tp-link:green:lan4",
        .gpio       = TL_WR941NDV5_GPIO_LED_LAN4,
        .active_low = 1,
    }, {
        .name       = "tp-link:green:qss",
        .gpio       = TL_WR941NDV5_GPIO_LED_QSS,
        .active_low = 1,
    }, {
        .name       = "tp-link:green:system",
        .gpio       = TL_WR941NDV5_GPIO_LED_SYSTEM,
        .active_low = 1,
    }, {
        .name       = "tp-link:green:wan",
        .gpio       = TL_WR941NDV5_GPIO_LED_WAN,
        .active_low = 1,
    }, {
        .name       = "tp-link:green:wlan",
        .gpio       = TL_WR941NDV5_GPIO_LED_WLAN,
        .active_low = 1,
    },
};

... 

#define TL_WR841NV8_GPIO_BTN_RESET  17
#define TL_WR841NV8_GPIO_SW_RFKILL  16  /* WPS for MR3420 v2 */

#define TL_WR941NDV5_GPIO_LED_WLAN  13
#define TL_WR941NDV5_GPIO_LED_QSS   15
#define TL_WR941NDV5_GPIO_LED_WAN   18
#define TL_WR941NDV5_GPIO_LED_LAN1  19
#define TL_WR941NDV5_GPIO_LED_LAN2  20
#define TL_WR941NDV5_GPIO_LED_LAN3  2
#define TL_WR941NDV5_GPIO_LED_LAN4  3
#define TL_WR941NDV5_GPIO_LED_SYSTEM    14

适配到SuperWRT

在上面的WR841n v8中,MAC的位置是0x1f01fc00,ART的们置是0x1fff1000。其中0x1f000000表示Flash开始的位置,所以在Flash中的位置分别为0x1fc00和0xff1000。

16M Flash的大小为0x1000000,当使用小于16M的Flash时,高位将失去作用。如0xff1000在8M Flash中,实际访问的是0x7f1000位置。

所以,对于2M Flash设备,我们可以按下面定义填写:

LED GPIO的定义:
  WLAN   13
  WAN    18
  LAN1   19
  LAN2   20
  LAN3   21
  LAN4   12
  SYSTEM 14
按键的定义:
  RESET  17
ART及MAC地址的位置:
  ART 1f1000
  MAC 1fc00

注:SuperWRT的boot从1.2.2版本开始,换大Flash后,支持自动扩展分区。虽然这里定义的2M,但你换了8M或16M的Flash后,系统会自动利用新增加的空间。

分区按SuperWRT分区格式,留出以上ART和MAC所在的位置即可。

所以,定制网页可以这里填写:

bootargs="console=ttyS0,115200 mtdparts=spi0.0:112k(uboot),4k(SBI),8k(custom),4k(CAL),1856k(firmware),32k(CAL2),32k(config)"

这里,由于firmware分区大小未满足SuperWRT要求,其实是不能使用2M Flash的。但仍然要这样定义分区,是为了可以平滑的使用更大Flash。因为没有改变原来分区的位置结构,所以,不用刷art及mac,可以直接使用原机的。

当然,你可以移动一个MAC和ART的位置,满足SuperWRT对于firmware分区大小的要求,以可以在2M Flash中使用SuperWRT。如下:

将0x1fc00的MAC地址内容(6字节大小)移动到0x20000位置,将0x1f1000的ART的内容(0x800大小)移动在0x20800位置。

然后分区如下:

bootargs="console=ttyS0,115200 mtdparts=spi0.0:112k(uboot),8k(SBI),8k(custom),4k(CAL),28k(config),1888k(firmware)"

填写网页时,更改一下下面的信息:

校准信息地址:0x20800
mac信息地址:0x20000