Linux:3.10
WiFi芯片:RTL8723
接口:SDIO
本文从硬件结构到软件实现探究Linux中WiFi驱动的框架。如下图:
硬件角度:
Linux有非常好的模块化机制,所以这几部分作为各自独立的模块进行注册,下面从代码示例的方式看下。
该模块完成CPU对WiFi模组电源、引脚的初始化、控制等功能。由于Linux采用设备树(Device Tree)方式管理硬件设置,所以第一步就是解析dts文件中的设置项并进行赋值、初始化操作,如:
static struct of_device_id wlan_platdata_of_match[] = {
{ .compatible = "wlan-platdata" },
{ }
};
MODULE_DEVICE_TABLE(of, wlan_platdata_of_match);
static int wlan_platdata_parse_dt(struct device *dev, struct wifi_moudle *data)
{
struct device_node *node = dev->of_node;
ret = of_property_read_string(node, "wifi_chip_type", &strings);
ret = of_property_read_u32(node, "sdio_vref", &value);
of_find_property(node, "keep_wifi_power_on", NULL)
of_find_property(node, "vref_ctrl_enable", NULL)
of_find_property(node, "power_ctrl_by_pmu", NULL);
of_get_named_gpio_flags(node, "WIFI,poweren_gpio", 0, &flags);
of_get_named_gpio_flags(node, "WIFI,reset_gpio", 0, &flags);
of_get_named_gpio_flags(node, "WIFI,host_wake_irq", 0, &flags);
return 0;
}
static struct platform_driver wlan_driver = {
.probe = wlan_probe,
.remove = wlan_remove,
.suspend = wlan_suspend,
.resume = wlan_resume,
.driver = {
.name = "wlan-platdata",
.owner = THIS_MODULE,
.of_match_table = of_match_ptr(wlan_platdata_of_match),
},
};
static int wlan_probe(struct platform_device *pdev)
{
......
wlan_platdata_parse_dt(&pdev->dev, pdata);
......
}
static int __init wlan_init(void)
{
LOG("Enter %s\n", __func__);
return platform_driver_register(&wlan_driver);
}
module_init(wlan_init);
module_exit(wlan_exit);
CPU集成有SDIO控制器,所以Host端就是对CPU上SDIO控制器的驱动实现,然后把函数指针通过mmc_add_host()赋予Core层。这部分代码存在于drivers\mmc\host\目录。操作接口比如:
static const struct mmc_host_ops dw_mci_ops = {
.request = dw_mci_request,
.pre_req = dw_mci_pre_req,
.post_req = dw_mci_post_req,
.set_ios = dw_mci_set_ios,
.get_ro = dw_mci_get_ro,
.get_cd = dw_mci_get_cd,
.set_sdio_status = dw_mci_set_sdio_status,
.hw_reset = dw_mci_hw_reset,
.enable_sdio_irq = dw_mci_enable_sdio_irq,
.execute_tuning = dw_mci_execute_tuning,
.post_tmo = dw_mci_post_tmo,
#ifdef CONFIG_MMC_SWITCH_VOLTAGE
.start_signal_voltage_switch
= dw_mci_start_signal_voltage_switch,
.card_busy = dw_mci_card_busy,
#endif
};
添加Host:
struct mmc_host *mmc;
mmc = mmc_alloc_host(sizeof(struct dw_mci_slot), host->dev);
mmc->ops = &dw_mci_ops;
mmc_add_host(mmc);
WiFi模组自身集成有SDIO控制器,所以这部分完成对WiFi模组上SDIO控制器的驱动实现。针对RTL8723,SDIO Client的代码实现在其驱动源码里。如下:
rtl8723:
static struct sdio_drv_priv sdio_drvpriv = {
.r871xs_drv.probe = rtw_drv_init,
.r871xs_drv.remove = rtw_dev_remove,
.r871xs_drv.name = (char*)DRV_NAME,
.r871xs_drv.id_table = sdio_ids,
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,29))
.r871xs_drv.drv = {
.pm = &rtw_sdio_pm_ops,
}
#endif
};
static int rtw_drv_entry(void)
{
sdio_drvpriv.drv_registered = _TRUE;
// sdio_register_driver: kernel\drivers\mmc\core\sdio_bus.c
ret = sdio_register_driver(&sdio_drvpriv.r871xs_drv);
return ret;
}
int rtl8723_wifi_init_module(void)
{
return rtw_drv_entry();
}
late_initcall(rtl8723_wifi_init_module);
通过sys文件系统,用户可以读写驱动信息。
static ssize_t wifi_chip_read(struct class *cls, struct class_attribute *attr, char *_buf)
{
ssize_t count = sprintf(_buf, "%s", "RTL8723BS");
printk("Current WiFi chip is RTL8723BS.\n");
return count;
}
static ssize_t wifi_power_write(struct class *cls, struct class_attribute *attr, const char *_buf, size_t _count)
{
int poweren = 0;
poweren = simple_strtol(_buf, NULL, 10);
if(poweren > 0) {
wifi_power(1);
} else {
wifi_power(0);
}
return _count;
}
static struct class *wifi_class = NULL;
// 生成class_attr_chip
static CLASS_ATTR(chip, 0664, wifi_chip_read, NULL);
// 生成class_attr_power
static CLASS_ATTR(power, 0660, NULL, wifi_power_write);
int wifi_sysif_init(void)
{
int ret;
wifi_class = class_create(THIS_MODULE, "rtlwifi");
ret = class_create_file(wifi_class, &class_attr_chip);
ret = class_create_file(wifi_class, &class_attr_power);
return 0;
}
void wifi_sysif_exit(void)
{
// need to remove the sys files and class
class_remove_file(wifi_class, &class_attr_chip);
class_remove_file(wifi_class, &class_attr_power);
wifi_class = NULL;
}
module_init(wifi_sysif_init);
module_exit(wifi_sysif_exit);
该模块注册后,将出现/sys/class/rtlwifi目录,且目录下含有/sys/class/rtlwifi/chip、/sys/class/rtlwifi/power两个文件,cat chip文件将会得到RTL8723BS,而读写power文件能够获取、设置WiFi模组供电状态。