从零开始编写linux驱动时,无论采用linux的何种架构,基本上都可以归纳为以下驱动八要素。
1.1.1 建立Linux驱动框架
Linux内核在使用驱动时首先要装载驱动,在装载过程中进行一些初始化动作,如建立设备文件、分配内存等。在驱动程序中需提供相应函数来处理驱动初始化工作,该函数须使用module_init宏指定。Linux系统在退出时需卸载Linux驱动,在卸载过程中进行一些退出动作,如删除设备文件、释放内存等。在驱动程序中需提供相应函数来处理退出工作,该函数须使用module_exit宏指定。Linux驱动程序一般都要这两个宏指定这两个函数,所以包含这两个宏以及其所指定的两个函数的C程序可看作是Linux驱动的框架。
蜂鸣器驱动对应的框架程序如下:
static struct platform_driver x4412_beep_driver = {
.probe = x4412_beep_probe,
.remove = x4412_beep_remove,
.suspend = x4412_beep_suspend,
.resume = x4412_beep_resume,
.driver = {
.name = "x4412-beep",
},
};
static struct platform_device x4412_beep_device = {
.name = "x4412-beep",
.id