linux设备驱动程序注册过程详解

Linux的驱动程序注册过程,大致分为两个步骤:
  • 模块初始化
  • 驱动程序注册
下面以内核提供的示例代码pci-skeleton.c,详细说明一个pci设备驱动程序的注册过程。其他设备的驱动代码注册过程基本相同,大家可自行查看。使用的内核代码版本是2.6.38。

1. 模块初始化

1.1 驱动程序入口

所有的设备驱动程序都会有如下两行代码:
1922 module_init(netdrv_init_module);
1923 module_exit(netdrv_cleanup_module);
module_init/module_exit是两个宏。module_init是该驱动程序的入口,加载驱动模块时,驱动程序就从netdrv_init_module函数开始执行。而当该驱动程序对应的设备被删除了,则会执行netdrv_cleanup_module这个函数。

1.2 模块初始化

当驱动程序开始执行时,首先会执行该驱动程序的初始化函数netdrv_init_module,代码如下:
1906 static int __init netdrv_init_module(void)
1907 {
1908 /* when a module, this is printed whether or not devices are found in probe */
1909 #ifdef MODULE
1910     printk(version);
1911 #endif
1912     return pci_register_driver(&netdrv_pci_driver);
1913 }
可以看到,初始化函数很简单,只执行了一个pci_register_driver函数就返回了。
其实模块的初始化过程就是这么简单,这也是linux驱动程序的ISO标准流程:module_init-->xx_init_module-->xx_register_driver。相信你看着这里时,还是一头雾水。别着急,我们慢慢来。

2. 驱动程序注册

什么是驱动模块的注册?上面讲到的初始化函数中调用的pci_register_driver函数就是注册驱动程序啦。在介绍注册函数之前,必须要详细说明下linux的总线设备驱动模型,否则下面的内容很难描述清楚。

2.1 linux总线设备驱动模型

关于总线设备驱动模型,很多书上都有详细的讲解,但是都很抽象,很难理解(至少我是这样认为的)。下面我尽量用最简单的方法来说明相关内容。
linux内核中分别用struct bus_type,struct device和struct device_driver来描述总线、设备和驱动。
总线:
 50 struct bus_type {
 51     const char      *name;
 52     struct bus_attribute    *bus_attrs;
 53     struct device_attribute *dev_attrs;
 54     struct driver_attribute *drv_attrs;
 55
 56     int (*match)(struct device *dev, struct device_driver *drv);
 。。。
 68 };
设备:
406 struct device {
407     struct device       *parent;
408
409     struct device_private   *p;
410
411     struct kobject kobj;
412     const char      *init_name; /* initial name of the device */
413     struct device_type  *type;
414
415     struct mutex        mutex;  /* mutex to synchronize calls to
416                      * its driver.
417                      */
418
419     struct bus_type *bus;       /* type of bus device is on */
420     struct device_driver *driver;   /* which driver has allocated this
421                        device */
。。。
456
457     void    (*release)(struct device *dev);
458 };
驱动:
122 struct device_driver {
123     const char      *name;
124     struct bus_type     *bus;
125
126     struct module       *owner;
127     const char      *mod_name;  /* used for built-in modules */
128
129     bool suppress_bind_a
  • 7
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Linux设备驱动程序开发详解第2版》是一本针对Linux系统下设备驱动程序开发的重要参考书籍。下面给出对该书的详细回答。 《Linux设备驱动程序开发详解第2版》这本书的作者是Robert Love,它是一本权威的Linux设备驱动程序开发指南。该书首先介绍了设备驱动程序的基本概念和原理,包括设备文件、设备号、设备结构体等。然后,它详细讲解了Linux内核提供的设备驱动框架,包括字符设备驱动、块设备驱动和网络设备驱动。书中介绍了开发设备驱动程序的各种基本技术和工具,如模块管理、内存管理和中断处理等。 该书还通过大量的示例代码和实践案例,帮助读者深入理解设备驱动程序的开发过程。例如,它详细描述了如何编写一个字符设备驱动程序,并介绍了如何使用ioctl系统调用和proc文件系统来与设备进行通信。此外,书中还介绍了如何使用Linux内核提供的调试工具来调试设备驱动程序,以及如何编写可移植的驱动程序。 《Linux设备驱动程序开发详解第2版》在第一版的基础上进行了更新和扩充。它新增了对新版本Linux内核的支持,并添加了更多实例和案例。此外,该书还介绍了设备模型和总线驱动程序的概念,让读者了解如何开发可移植的设备驱动程序。 总之,《Linux设备驱动程序开发详解第2版》是一本非常有价值的书籍,对于想要深入了解和掌握Linux设备驱动程序开发的人来说,是一本必读的参考书。它全面而详细地介绍了设备驱动程序的开发原理、技术和工具,通过丰富的实例和案例,帮助读者更好地理解和掌握设备驱动程序开发的要点。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值