七、linux驱动注册
一、驅動注冊結構體
????????驅動注冊使用結構體platform_driver,該結構體在頭文件“vim include/linux/platform_device.h”中,和剛剛那個設備注冊的驅動在同一個頭文件。
- ? ? ? ? 驅動注冊platform_driver_register
- ????????驅動卸載函數platform_driver_unregister
???????也在這個頭文件中,這兩個函數的參數都只有結構體platform_driver
????????驅動常見的幾種狀態,初始化,移除,休眠,復位
????????就像PC一樣,有的驅動休眠之后無法使用,有的可以使用;有的系統喚醒之后,驅動需要重新啟動才能正常工作,也有直接就可以使用等等
?????????? probe函數
????????????????– platform_match函數匹配之后,驅動調用的初始化函數
????????? remove函數?
????????????????– 移除驅動函數
???????????suspend函數
????????????????– 懸掛(休眠)驅動函數
?????????? resume函數
????????????????– 休眠后恢復驅動
????????? device_driver數據結構的兩個參數
????????????????– name和注冊的設備name要一致
????????????????– owner一般賦值THIS_MODULE
二、驅動源碼
我們在前面最小驅動的代碼基礎上修改,主要實現platform_driver結構體中定義的函數,然后再通過platform_driver_register和platform_driver_unregister注冊和卸載:
#include <linux/init.h> #include <linux/module.h>/*驅動注冊的頭文件,包含驅動的結構體和注冊和卸載的函數*/ #include <linux/platform_device.h>#define DRIVER_NAME "hello_ctl"MODULE_LICENSE("Dual BSD/GPL"); MODULE_AUTHOR("TOPEET");static int hello_probe(struct platform_device *pdv){printk(KERN_EMERG "\tinitialized\n");return 0; }static int hello_remove(struct platform_device *pdv){return 0; }static void hello_shutdown(struct platform_device *pdv){; }static int hello_suspend(struct platform_device *pdv){return 0; }static int hello_resume(struct platform_device *pdv){return 0; }struct platform_driver hello_driver = {.probe = hello_probe,.remove = hello_remove,.shutdown = hello_shutdown,.suspend = hello_suspend,.resume = hello_resume,.driver = {.name = DRIVER_NAME,.owner = THIS_MODULE,} };static int hello_init(void) {int DriverState;printk(KERN_EMERG "HELLO WORLD enter!\n");DriverState = platform_driver_register(&hello_driver); // 注冊驅動printk(KERN_EMERG "\tDriverState is %d\n",DriverState);return 0; }static void hello_exit(void) {printk(KERN_EMERG "HELLO WORLD exit!\n");platform_driver_unregister(&hello_driver); // 注銷驅動 }module_init(hello_init);// 模塊入口 module_exit(hello_exit);?運行如下:當遇到驅動無法卸載時,請按照下面操作解決
?
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的七、linux驱动注册的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 六、linux虚拟平台设备注册
- 下一篇: 八、linux以模块方式注册设备