参考书:<linux设备驱动开发详解>宋宝华
上一篇介绍了platform的基本构成与源码,本篇用实例来说明platform的实现.
参考书中介绍了一个名为globalfifo的驱动,以他为例,来说明作为platfrom设备如何实现驱动和设备的匹配.
将globalfifo驱动挂接到platform总线上,要完成两部分工作:
(1)将globalfifo移植为platform驱动
(2)在板文件中添加globalfifo这个platform设备
1.移植
为完成globalfifo移植到platform驱动的工作,需要在原始的字符设备globalfifo驱动中套一层platfrom_driver外壳. 这一步骤并没有改变globalfifo字符设备的本质,只是将其挂接到了platform总线上.
static int globalfifo_probe(struct platform_device *pdev){int ret;dev_t devno = MKDEV(globalfifo_major,0); //定义设备号,数据类型dev_t,32位数if (globalfifo_major) //静态申请ret = register_chrdev_region(devno,1,"globalfifo");//申请字符设备驱动,1表示申请的连续设备编号的个数,globalfifo是设备名称else{ //静态申请失败ret = alloc_chrdev_region(&devno,0,1,"globalfifo"); //0表示请求的第一个次设备号,1表示个数globalfifo_major = MAJOR(devno);}if (ret < 0)return ret;globalfifo_devp = devm_kzalloc(&pdev->dev,sizeof(*globalfifo_devp),GFP_KERNEL);if (!globalfifo_devp) {ret = -ENOMEM;goto fail_malloc;}globalfifo_setup_cdev(globalfifo_devp,0); //cdev初始化和添加mutex_init( &globalfifo_devp->mutex);init_waitqueue_head(&globalfifo_devp->r_wait); //初始化队列头init_waitqueue_head(&globalfifo_devp->w_wait);return 0;fail_malloc:unregister_chrdev_region(devno,1);return ret;}static int globalfifo_remove(struct platform_device *pdev){cdev_del(&globalfifo_devp->cdev);//删除cdev结构体unregister_chrdev_region(MKDEV(globalfifo_major,0),1);//注销设备区域return 0;}static struct platform_driver globalfifo_driver = {.driver = {.name = "globalfifo",.owner = THIS_MODULE,},.probe = globalfifo_probe,.remove = globalfifo_remove,};module_platform_driver(globalfifo_driver);
注册完globalfifo对应的platform_driver后,会发现/sys/bus/platform/drivers目录下多出一个名叫globalfifo的子目录.
板文件arch/arm/mach-<soc名>/mach-<板名>.c中添加如下代码:
static struct platform_device globalfifo_device = {.name = "globalfifo",.id = -1,};
如果一切顺利,会在/sys/devices/platform目录下看到一个名为globalfifo的子目录
/sys/devices/platfrom/globalfifo中会有一个driver文件,指向/sys/bus/platform/drivers/globalfifo的符号链接, 证明驱动和设备匹配上了.