一、platform 驅動的工作過程
platform模型驅動編程,需要實現platform_device(設備)與platform_driver(驅動)在platform(虛擬總線)上的注冊、匹配,相互綁定,然后再做為一個普通的字符設備進行相應的應用,總之如果編寫的是基于字符設備的platform驅動,在遵循并實現platform總線上驅動與設備的特定接口的情況下,最核心的還是字符設備的核心結構:cdev、 file_operations(他包含的操作函數接口)、dev_t(設備號)、設備文件(/dev)等,因為用platform機制編寫的字符驅動,它的本質是字符驅動。
我們要記住,platform 驅動只是在字符設備驅動外套一層platform_driver 的外殼。
在一般情況下,2.6內核中已經初始化并掛載了一條platform總線在sysfs文件系統中。那么我們編寫platform模型驅動時,需要完成兩個工作:
a -- 實現platform驅動
b -- 實現platform設備
然而在實現這兩個工作的過程中還需要實現其他的很多小工作,在后面介紹。platform模型驅動的實現過程核心架構就很簡單,如下所示:
platform驅動模型三個對象:platform總線、platform設備、platform驅動。
platform總線對應的內核結構:struct bus_type-->它包含的最關鍵的函數:match() (要注意的是,這塊由內核完成,我們不參與)
platform設備對應的內核結構:struct platform_device-->注冊:platform_device_register(unregister)
platform驅動對應的內核結構:struct platform_driver-->注冊:platform_driver_register(unregister)
那具體platform驅動的工作過程是什么呢:
設備(或驅動)注冊的時候,都會引發總線調用自己的match函數來尋找目前platform總線是否掛載有與該設備(或驅動)名字匹配的驅動(或設備),如果存在則將雙方綁定;
如果先注冊設備,驅動還沒有注冊,那么設備在被注冊到總線上時,將不會匹配到與自己同名的驅動,然后在驅動注冊到總線上時,因為設備已注冊,那么總線會立即匹配與綁定這時的同名的設備與驅動,再調用驅動中的probe函數等;
如果是驅動先注冊,同設備驅動一樣先會匹配失敗,匹配失敗將導致它的probe函數暫不調用,而是要等到設備注冊成功并與自己匹配綁定后才會調用。
二、實現platform 驅動與設備的詳細過程
1、思考問題?
在分析platform 之前,可以先思考一下下面的問題:
a -- 為什么要用 platform 驅動?不用platform驅動可以嗎?
b -- 設備驅動中引入platform 概念有什么好處?
現在先不回答,看完下面的分析就明白了,后面會附上總結。
2、platform_device 結構體 VS platform_driver 結構體
這兩個結構體分別描述了設備和驅動,二者有什么關系呢?先看一下具體結構體對比

前面提到,實現platform模型的過程就是總線對設備和驅動的匹配過程 。打個比方,就好比相親,總線是紅娘,設備是男方,驅動是女方:
a -- 紅娘(總線)負責男方(設備)和女方(驅動)的撮合;
b -- 男方(女方)找到紅娘,說我來登記一下,看有沒有合適的姑娘(漢子)—— 設備或驅動的注冊;
c -- 紅娘這時候就需要看看有沒有八字(二者的name 字段)匹配的姑娘(漢子)——match 函數進行匹配,看name是否相同;
d -- 如果八字不合,就告訴男方(女方)沒有合適的對象,先等著,別急著亂做事 —— 設備和驅動會等待,直到匹配成功;
e -- 終于遇到八字匹配的了,那就結婚唄!接完婚,男方就向女方交代,我有多少存款,我的房子在哪,錢放在哪等等( struct resource *resource),女方說好啊,于是去房子里拿錢,去給男方買菜啦,給自己買衣服、化妝品、首飾啊等等(int (*probe)(struct platform_device *) 匹配成功后驅動執行的第一個函數),當然如果男的跟小三跑了(設備卸載),女方也不會繼續待下去的( int (*remove)(struct platform_device *))。
3、設備資源結構體
在struct platform_device 結構體中有一重要成員 struct resource *resource
[cpp] view plain copy
1. struct resource {
2. resource_size_t start; 資源起始地址
3. resource_size_t end; 資源結束地址
4. const char *name;
5. unsigned long flags; 區分是資源什么類型的
6. struct resource *parent, *sibling, *child;
7. };
8.
9. #define IORESOURCE_MEM 0x00000200
10. #define IORESOURCE_IRQ 0x00000400
flags 指資源類型,我們常用的是 IORESOURCE_MEM、IORESOURCE_IRQ 這兩種。start 和 end 的含義會隨著 flags而變更,如
a -- flags為IORESOURCE_MEM 時,start 、end 分別表示該platform_device占據的內存的開始地址和結束值;
b -- flags為 IORESOURCE_IRQ 時,start 、end 分別表示該platform_device使用的中斷號的開始地址和結束值;
下面看一個實例:
[cpp] view plain copy
1. static struct resource beep_resource[] =
2. {
3. [0] = {
4. .start = 0x114000a0,
5. .end = 0x114000a0+0x4,
6. .flags = IORESOURCE_MEM,
7. },
8.
9. [1] = {
10. .start = 0x139D0000,
11. .end = 0x139D0000+0x14,
12. .flags = IORESOURCE_MEM,
13. },
14. };
4、將字符設備添加到platform的driver中
前面我們提到platform 驅動只是在字符設備驅動外套一層platform_driver 的外殼,下面我們看一下添加的過程:
[cpp] view plain copy
1. static struct file_operations hello_ops=
2. {
3. .open = hello_open,
4. .release = hello_release,
5. .unlocked_ioctl = hello_ioctl,
6. };
7.
8. static int hello_remove(struct platform_device *pdev)
9. {
10. 注銷分配的各種資源
11. }
12.
13. static int hello_probe(struct platform_device *pdev)
14. {
15. 1.申請設備號
16. 2.cdev初始化注冊,&hello_ops
17. 3.從pdev讀出硬件資源
18. 4.對硬件資源初始化,ioremap,request_irq( )
19. }
20.
21. static int hello_init(void)
22. {
23. 只注冊 platform_driver
24. }
25.
26. static void hello_exit(void)
27. {
28. 只注銷 platform_driver
29. }
可以看到,模塊加載和卸載函數僅僅通過paltform_driver_register()、paltform_driver_unregister() 函數進行 platform_driver 的注冊和注銷,而原先注冊和注銷字符設備的工作已經被移交到 platform_driver 的 probe() 和 remove() 成員函數中。
5、platform是如何匹配device和driver
這時就該總線出場了,系統為platform總線定義了一個bus_type 的實例platform_bus_type,其定義如下:
[cpp] view plain copy
1. struct bus_type platform_bus_type = {
2. .name = "platform",
3. .dev_groups = platform_dev_groups,
4. .match = platform_match,
5. .uevent = platform_uevent,
6. .pm = &platform_dev_pm_ops,
7. };
其又是怎樣工作的呢?在platform.c (e:\linux-3.14-fs4412\drivers\base) 31577 2014/3/31 中可以看到
[cpp] view plain copy
1. __platform_driver_register()
2. {
3. drv->driver.bus = &platform_bus_type; 536行
4. }
在platform_bus_type 中調用 了platform_match:
[cpp] view plain copy
1. static int platform_match(struct device *dev, struct device_driver *drv)
2. {
3. struct platform_device *pdev = to_platform_device(dev);
4. struct platform_driver *pdrv = to_platform_driver(drv);
5.
6. 匹配設備樹信息,如果有設備樹,就調用 of_driver_match_device() 函數進行匹配
7. if (of_driver_match_device(dev, drv))
8. return 1;
9.
10.
11. 匹配id_table
12. if (pdrv->id_table)
13. return platform_match_id(pdrv->id_table, pdev) != NULL;
14.
15. 最基本匹配規則
16. return (strcmp(pdev->name, drv->name) == 0);
17. }
6、解決問題
現在可以回答這兩個問題了
a -- 為什么要用 platform 驅動?不用platform驅動可以嗎?
b -- 設備驅動中引入platform 概念有什么好處?
引入platform模型符合Linux 設備模型 —— 總線、設備、驅動,設備模型中配套的sysfs節點都可以用,方便我們的開發;當然你也可以選擇不用,不過就失去了一些platform帶來的便利;
設備驅動中引入platform 概念,隔離BSP和驅動。在BSP中定義platform設備和設備使用的資源、設備的具體匹配信息,而在驅動中,只需要通過API去獲取資源和數據,做到了板相關代碼和驅動代碼的分離,使得驅動具有更好的可擴展性和跨平臺性。
三、實例
這是一個蜂鳴器的驅動,其實前面已經有解析 Linux 字符設備驅動開發基礎(二)—— 編寫簡單 PWM 設備驅動, 下面來看一下,套上platform 外殼后的程序:
1、device.c
[cpp] view plain copy
1. #include <linux/module.h>
2. #include <linux/device.h>
3. #include <linux/platform_device.h>
4. #include <linux/ioport.h>
5.
6. static struct resource beep_resource[] =
7. {
8. [0] ={
9. .start = 0x114000a0,
10. .end = 0x114000a0 + 0x4,
11. .flags = IORESOURCE_MEM,
12. },
13.
14. [1] ={
15. .start = 0x139D0000,
16. .end = 0x139D0000 + 0x14,
17. .flags = IORESOURCE_MEM,
18. }
19. };
20.
21. static void hello_release(struct device *dev)
22. {
23. printk("hello_release\n");
24. return ;
25. }
26.
27.
28.
29. static struct platform_device hello_device=
30. {
31. .name = "bigbang",
32. .id = -1,
33. .dev.release = hello_release,
34. .num_resources = ARRAY_SIZE(beep_resource),
35. .resource = beep_resource,
36. };
37.
38. static int hello_init(void)
39. {
40. printk("hello_init");
41. return platform_device_register(&hello_device);
42. }
43.
44. static void hello_exit(void)
45. {
46. printk("hello_exit");
47. platform_device_unregister(&hello_device);
48. return;
49. }
50.
51. MODULE_LICENSE("GPL");
52. module_init(hello_init);
53. module_exit(hello_exit);
2、driver.c
[cpp] view plain copy
1. #include <linux/module.h>
2. #include <linux/fs.h>
3. #include <linux/cdev.h>
4. #include <linux/device.h>
5. #include <linux/platform_device.h>
6. #include <asm/io.h>
7.
8. static int major = 250;
9. static int minor=0;
10. static dev_t devno;
11. static struct class *cls;
12. static struct device *test_device;
13.
14. #define TCFG0 0x0000
15. #define TCFG1 0x0004
16. #define TCON 0x0008
17. #define TCNTB0 0x000C
18. #define TCMPB0 0x0010
19.
20. static unsigned int *gpd0con;
21. static void *timer_base;
22.
23. #define MAGIC_NUMBER 'k'
24. #define BEEP_ON _IO(MAGIC_NUMBER ,0)
25. #define BEEP_OFF _IO(MAGIC_NUMBER ,1)
26. #define BEEP_FREQ _IO(MAGIC_NUMBER ,2)
27.
28. static void fs4412_beep_init(void)
29. {
30. writel ((readl(gpd0con)&~(0xf<<0)) | (0x2<<0),gpd0con);
31. writel ((readl(timer_base +TCFG0 )&~(0xff<<0)) | (0xff <<0),timer_base +TCFG0);
32. writel ((readl(timer_base +TCFG1 )&~(0xf<<0)) | (0x2 <<0),timer_base +TCFG1 );
33.
34. writel (500, timer_base +TCNTB0 );
35. writel (250, timer_base +TCMPB0 );
36. writel ((readl(timer_base +TCON )&~(0xf<<0)) | (0x2 <<0),timer_base +TCON );
37. }
38.
39. void fs4412_beep_on(void)
40. {
41. writel ((readl(timer_base +TCON )&~(0xf<<0)) | (0x9 <<0),timer_base +TCON );
42. }
43.
44. void fs4412_beep_off(void)
45. {
46. writel ((readl(timer_base +TCON )&~(0xf<<0)) | (0x0 <<0),timer_base +TCON );
47. }
48.
49. static void beep_unmap(void)
50. {
51. iounmap(gpd0con);
52. iounmap(timer_base);
53. }
54.
55. static int beep_open (struct inode *inode, struct file *filep)
56. {
57. fs4412_beep_on();
58. return 0;
59. }
60.
61. static int beep_release(struct inode *inode, struct file *filep)
62. {
63. fs4412_beep_off();
64. return 0;
65. }
66.
67. #define BEPP_IN_FREQ 100000
68. static void beep_freq(unsigned long arg)
69. {
70. writel(BEPP_IN_FREQ/arg, timer_base +TCNTB0 );
71. writel(BEPP_IN_FREQ/(2*arg), timer_base +TCMPB0 );
72.
73. }
74.
75. static long beep_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
76. {
77. switch(cmd)
78. {
79. case BEEP_ON:
80. fs4412_beep_on();
81. break;
82. case BEEP_OFF:
83. fs4412_beep_off();
84. break;
85. case BEEP_FREQ:
86. beep_freq( arg );
87. break;
88. default :
89. return -EINVAL;
90. }
91. return 0;
92. }
93.
94. static struct file_operations beep_ops=
95. {
96. .open = beep_open,
97. .release = beep_release,
98. .unlocked_ioctl = beep_ioctl,
99. };
100.
101. static int beep_probe(struct platform_device *pdev)
102. {
103. int ret;
104. printk("match ok!");
105.
106. gpd0con = ioremap(pdev->resource[0].start,pdev->resource[0].end - pdev->resource[0].start);
107. timer_base = ioremap(pdev->resource[1].start, pdev->resource[1].end - pdev->resource[1].start);
108.
109. devno = MKDEV(major,minor);
110. ret = register_chrdev(major,"beep",&beep_ops);
111.
112. cls = class_create(THIS_MODULE, "myclass");
113. if(IS_ERR(cls))
114. {
115. unregister_chrdev(major,"beep");
116. return -EBUSY;
117. }
118.
119. test_device = device_create(cls,NULL,devno,NULL,"beep");//mknod /dev/hello
120. if(IS_ERR(test_device))
121. {
122. class_destroy(cls);
123. unregister_chrdev(major,"beep");
124. return -EBUSY;
125. }
126.
127. fs4412_beep_init();
128.
129. return 0;
130. }
131.
132. static int beep_remove(struct platform_device *pdev)
133. {
134. beep_unmap();
135. device_destroy(cls,devno);
136. class_destroy(cls);
137. unregister_chrdev(major,"beep");
138.
139. return 0;
140. }
141.
142.
143. static struct platform_driver beep_driver=
144. {
145. .driver.name = "bigbang",
146. .probe = beep_probe,
147. .remove = beep_remove,
148. };
149.
150.
151. static int beep_init(void)
152. {
153. printk("beep_init");
154.
155. return platform_driver_register(&beep_driver);
156. }
157.
158. static void beep_exit(void)
159. {
160. printk("beep_exit");
161. platform_driver_unregister(&beep_driver);
162.
163. return;
164. }
165.
166.
167. MODULE_LICENSE("GPL");
168. module_init(beep_init);
169. module_exit(beep_exit);
3、makefile
[cpp] view plain copy
1. ifneq ($(KERNELRELEASE),)
2. obj-m:=device.o driver.o
3. $(info "2nd")
4. else
5. #KDIR := /lib/modules/$(shell uname -r)/build
6. KDIR := /home/fs/linux/linux-3.14-fs4412
7. PWD:=$(shell pwd)
8. all:
9. $(info "1st")
10. make -C $(KDIR) M=$(PWD) modules
11. clean:
12. rm -f *.ko *.o *.symvers *.mod.c *.mod.o *.order
13. endif
4、test.c
[cpp] view plain copy
1. #include <sys/types.h>
2. #include <sys/stat.h>
3. #include <fcntl.h>
4. #include <stdio.h>
5.
6. main()
7. {
8. int fd,i,lednum;
9.
10. fd = open("/dev/beep",O_RDWR);
11. if(fd<0)
12. {
13. perror("open fail \n");
14. return ;
15. }
16.
17. sleep(10);
18. close(fd);
19. }
來源:玩轉單片機
免責聲明:本文系網絡轉載,版權歸原作者所有。本文所用視頻、圖片、文字如涉及作品版權問題,請第一時間告知,我們將根據您提供的證明材料確認版權并按國家標準支付稿酬或立即刪除內容!本文內容為原作者觀點,并不代表本公眾號贊同其觀點和對其真實性負責。
為您發布產品,請點擊“閱讀原文”