一、引言:
?? ? ? ?在 字符設(shè)備驅(qū)動的傳統(tǒng)寫法 中,介紹了字符設(shè)備的傳統(tǒng)寫法。從代碼中我們可以看到,使用的引腳,寫死在代碼中了,如果更改硬件資源,比如將GPIO3_4改成GPIO3_5,那就需要重新編譯這個驅(qū)動程序,如果該驅(qū)動程序是放在內(nèi)核里的,那么就需要重新編譯內(nèi)核。并且,在需要更改硬件資源的時候都需要去閱讀驅(qū)動源碼,對于沒有寫驅(qū)動能力的人來說,這也是挺痛苦的。在Linux內(nèi)核持續(xù)發(fā)展中,改進(jìn)了寫驅(qū)動程序的方法,使用總線設(shè)備驅(qū)動模型。
?? ? ? ?為了方便對比學(xué)習(xí),我們在上節(jié)傳統(tǒng)字符設(shè)備寫法基礎(chǔ)上進(jìn)行修改??偩€設(shè)備驅(qū)動模型將驅(qū)動程序分成了兩部分led_dev 和 led_drv ;dev部分指定硬件資源,drv分配設(shè)置fileoperations結(jié)構(gòu)體然后根據(jù)硬件資源來操作硬件 。
二、BUS - dev
?? ? ? ?現(xiàn)在我們來看下,內(nèi)核中又是如何指定硬件資源的?以面向?qū)ο蟮乃枷氲姆椒?,在?nèi)核里它定義一個dev的時候,也是去分配設(shè)置某個結(jié)構(gòu)體,這個結(jié)構(gòu)體就是平臺設(shè)備。
平臺設(shè)備使用struct platform_device來描述:
struct platform_device {
const char * name; //設(shè)備名稱,要與platform_driver的name一樣,
//這樣總線才能匹配成功
u32 id; //id號,插入總線下相同name的設(shè)備編號(一個驅(qū)動可以有多個設(shè)備),
//如果只有一個設(shè)備填-1
struct device dev; //內(nèi)嵌的具體的device結(jié)構(gòu)體,其中成員platform_data,是個void *類型,
//可以給平臺driver提供各種數(shù)據(jù)(比如:GPIO引腳等等)
u32 num_resources; //資源數(shù)量,
struct resource * resource; //資源結(jié)構(gòu)體,保存設(shè)備的信息
};
resource資源,就是用來記錄地址,地址等資源,供drv使用。
其中resource資源結(jié)構(gòu)體,如下:
struct resource {
resource_size_t start; //起始資源,如果是地址的話,必須是物理地址
resource_size_t end; //結(jié)束資源,如果是地址的話,必須是物理地址
const char *name; //資源名
unsigned long flags; //資源的標(biāo)志
//比如IORESOURCE_MEM,表示地址資源, IORESOURCE_IRQ表示中斷引腳... ...
struct resource *parent, *sibling, *child; //資源拓?fù)渲羔樃?、兄、?可以構(gòu)成鏈表
};
分析到這里,可以看出,對于 bus-dev 這邊,就是去定義一個一個的platform_device,然后去注冊到bus總線上。對于 bus-drv 那邊,也類似,定義了一個一個的platform_driver然后注冊到bus總線上。
????? 內(nèi)核里有那么多的platform_device,上百個都有可能,同時也存在那么多個platform_driver;問題來了,platform_driver該從哪個platform_device里獲得指定的硬件資源呢?或者說,對于指定的platform_device又是給內(nèi)核中那么多的platform_driver中的哪個提供硬件資源的描述呢?他們之間需要有個匹配,在BUS(我們平時使用的一般為platform_bus_type)里有個match函數(shù) ,就是用來匹配drv和dev。如果匹配,則調(diào)用drv->probe函數(shù);至于probe函數(shù)里做什么,由驅(qū)動開發(fā)者決定;總線設(shè)備驅(qū)動模型不過提供了這樣一種機制。它并不是驅(qū)動程序的核心,核心仍然是drv里的分配、設(shè)置、注冊file_operations結(jié)構(gòu)體。
????? 接下來看總線里的match函數(shù)是如何確定dev和drv是否匹配的??偩€下面掛載著一系列的dev和一系列的drv,設(shè)備、驅(qū)動匹配時就是通過match函數(shù)來兩兩比較的,一旦match成功,則調(diào)用drv里的probe函數(shù)。

機制講完,開始寫代碼。
2.1 編寫 led_dev
2.1.1 首先分配設(shè)置一個平臺 dev
static struct platform_device led_dev = {
.name = "myled", //對應(yīng)的platform_driver驅(qū)動的名字
.id = -1, //表示只有一個設(shè)備
.num_resources = ARRAY_SIZE(led_resource), //資源數(shù)量,ARRAY_SIZE()函數(shù):獲取數(shù)量
.resource = led_resource, //資源數(shù)組led_resource
.dev = {
.release = led_release, //釋放函數(shù),必須向內(nèi)核提供一個release函數(shù), 、
//否則卸載時,內(nèi)核找不到該函數(shù)會報錯
},
};
.name 設(shè)置平臺設(shè)備中的名字,用于與平臺驅(qū)動匹配;
.resource 資源,用于描述設(shè)備信息,具體類型通過resource中的flags標(biāo)識;資源類型主要有:

好像都沒有指明引腳的資源,那怎么辦?反正這個平臺資源是自己使用的,我們先假設(shè)其為MEM資源,在驅(qū)動解析使用時,并不把他當(dāng)成是MEM,內(nèi)存資源,直接當(dāng)成一個引腳
static struct resource led_resource[] = {
[0] = {
.start = S3C2440_GPF(5),
.end = S3C2440_GPF(5),
.flags = IORESOURCE_MEM,
},
};
2.1.2 在入口時注冊
platform_device_register(&led_dev);
2.1.3 出口時卸載
platform_device_unregister(&led_dev);
完整的led_dev.c如下:
#include <linux/module.h>
#include <linux/version.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/interrupt.h>
#include <linux/list.h>
#include <linux/timer.h>
#include <linux/init.h>
#include <linux/serial_core.h>
#include <linux/platform_device.h>
#define S3C2440_GPA(n) (0<<16 | n)
#define S3C2440_GPB(n) (1<<16 | n)
#define S3C2440_GPC(n) (2<<16 | n)
#define S3C2440_GPD(n) (3<<16 | n)
#define S3C2440_GPE(n) (4<<16 | n)
#define S3C2440_GPF(n) (5<<16 | n)
#define S3C2440_GPG(n) (6<<16 | n)
#define S3C2440_GPH(n) (7<<16 | n)
#define S3C2440_GPI(n) (8<<16 | n)
#define S3C2440_GPJ(n) (9<<16 | n)
/* 分配/設(shè)置/注冊一個platform_device */
static struct resource led_resource[] = {
[0] = {
.start = S3C2440_GPF(5),
.end = S3C2440_GPF(5),
.flags = IORESOURCE_MEM,
},
};
static void led_release(struct device * dev)
{
}
static struct platform_device led_dev = {
.name = "myled",
.id = -1,
.num_resources = ARRAY_SIZE(led_resource),
.resource = led_resource,
.dev = {
.release = led_release,
},
};
static int led_dev_init(void)
{
platform_device_register(&led_dev);
return 0;
}
static void led_dev_exit(void)
{
platform_device_unregister(&led_dev);
}
module_init(led_dev_init);
module_exit(led_dev_exit);
MODULE_LICENSE("GPL");
2.2 編寫 led_drv
2.2.1 首先還是先分配設(shè)置一個平臺 drv
struct platform_driver led_drv = {
.probe = led_probe,
.remove = led_remove,
.driver = {
.name = "myled",
}
};
?????? 經(jīng)過對平臺總線機制的分析,我們知道在平臺drv中也有個名字,就是用這個名字("myled")來和平臺dev中的名字做匹配的,一旦匹配,則調(diào)用平臺drv中的probe函數(shù)。接下來編寫probe函數(shù),其參數(shù)中有個 platform_device 平臺 dev ,在傳統(tǒng)的字符設(shè)備寫法中,在入口函數(shù)中直接注冊了字符設(shè)備;file_operations中的open、write直接使用了led_pin,led_pin是直接寫死在驅(qū)動代碼中的;現(xiàn)在,我們要使用平臺總線的方法來寫,這個引腳資源需要從對應(yīng)的平臺 dev 里來獲得這個資源,確定這個引腳。
static struct file_operations myled_oprs = {
.owner = THIS_MODULE,
.open = led_open,
.write = led_write,
.release = led_release,
};
static int led_probe(struct platform_device *pdev)
{
struct resource *res;
/* 根據(jù)platform_device的資源進(jìn)行ioremap
參數(shù) 0代表IORESOURCE_MEM這類資源中的第0個,
把他取出來后res->start,代表的就是引腳了*/
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
led_pin = res->start;
major = register_chrdev(0, "myled", &myled_oprs);
led_class = class_create(THIS_MODULE, "myled");
device_create(led_class, NULL, MKDEV(major, 0), NULL, "led"); /* /dev/led */
return 0;
}
在remove里面來unregister:
static int led_remove(struct platform_device *pdev)
{
unregister_chrdev(major, "myled");
device_destroy(led_class, MKDEV(major, 0));
class_destroy(led_class);
return 0;
}
接下來的就還是原本的那套,完整代碼如下:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/of_platform.h>
#include <linux/platform_device.h>
#define S3C2440_GPA(n) (0<<16 | n)
#define S3C2440_GPB(n) (1<<16 | n)
#define S3C2440_GPC(n) (2<<16 | n)
#define S3C2440_GPD(n) (3<<16 | n)
#define S3C2440_GPE(n) (4<<16 | n)
#define S3C2440_GPF(n) (5<<16 | n)
#define S3C2440_GPG(n) (6<<16 | n)
#define S3C2440_GPH(n) (7<<16 | n)
#define S3C2440_GPI(n) (8<<16 | n)
#define S3C2440_GPJ(n) (9<<16 | n)
static int led_pin;
static volatile unsigned int *gpio_con;
static volatile unsigned int *gpio_dat;
/* 123. 分配/設(shè)置/注冊file_operations
* 4. 入口
* 5. 出口
*/
static int major;
static struct class *led_class;
static unsigned int gpio_base[] = {
0x56000000, /* GPACON */
0x56000010, /* GPBCON */
0x56000020, /* GPCCON */
0x56000030, /* GPDCON */
0x56000040, /* GPECON */
0x56000050, /* GPFCON */
0x56000060, /* GPGCON */
0x56000070, /* GPHCON */
0, /* GPICON */
0x560000D0, /* GPJCON */
};
static int led_open (struct inode *node, struct file *filp)
{
/* 把LED引腳配置為輸出引腳 */
/* GPF5 - 0x56000050 */
int bank = led_pin >> 16;
int base = gpio_base[bank];
int pin = led_pin & 0xffff;
gpio_con = ioremap(base, 8);
if (gpio_con) {
printk("ioremap(0x%x) = 0x%x\n", base, gpio_con);
}
else {
return -EINVAL;
}
gpio_dat = gpio_con + 1;
*gpio_con &= ~(3<<(pin * 2));
*gpio_con |= (1<<(pin * 2));
return 0;
}
static ssize_t led_write (struct file *filp, const char __user *buf, size_t size, loff_t *off)
{
/* 根據(jù)APP傳入的值來設(shè)置LED引腳 */
unsigned char val;
int pin = led_pin & 0xffff;
copy_from_user(&val, buf, 1);
if (val)
{
/* 點燈 */
*gpio_dat &= ~(1<<pin);
}
else
{
/* 滅燈 */
*gpio_dat |= (1<<pin);
}
return 1; /* 已寫入1個數(shù)據(jù) */
}
static int led_release (struct inode *node, struct file *filp)
{
printk("iounmap(0x%x)\n", gpio_con);
iounmap(gpio_con);
return 0;
}
static struct file_operations myled_oprs = {
.owner = THIS_MODULE,
.open = led_open,
.write = led_write,
.release = led_release,
};
static int led_probe(struct platform_device *pdev)
{
struct resource *res;
/* 根據(jù)platform_device的資源進(jìn)行ioremap
參數(shù) 0代表IORESOURCE_MEM這類資源中的第0個,
把他取出來后res->start,代表的就是引腳了*/
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
led_pin = res->start;
major = register_chrdev(0, "myled", &myled_oprs);
led_class = class_create(THIS_MODULE, "myled");
device_create(led_class, NULL, MKDEV(major, 0), NULL, "led"); /* /dev/led */
return 0;
}
static int led_remove(struct platform_device *pdev)
{
unregister_chrdev(major, "myled");
device_destroy(led_class, MKDEV(major, 0));
class_destroy(led_class);
return 0;
}
struct platform_driver led_drv = {
.probe = led_probe,
.remove = led_remove,
.driver = {
.name = "myled",
}
};
static int myled_init(void)
{
platform_driver_register(&led_drv);
return 0;
}
static void myled_exit(void)
{
platform_driver_unregister(&led_drv);
}
module_init(myled_init);
module_exit(myled_exit);
MODULE_LICENSE("GPL");
2.3 makefile
KERN_DIR = /work/system/linux-4.19-rc3
all:
make -C $(KERN_DIR) M=`pwd` modules
clean:
make -C $(KERN_DIR) M=`pwd` modules clean
rm -rf modules.order
obj-m += led_drv.o
obj-m += led_dev.o
2.4 test.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
/* ledtest on
* ledtest off
*/
int main(int argc, char **argv)
{
int fd;
unsigned char val = 1;
fd = open("/dev/led", O_RDWR);
if (fd < 0)
{
printf("can't open!\n");
}
if (argc != 2)
{
printf("Usage :\n");
printf("%s <on|off>\n", argv[0]);
return 0;
}
if (strcmp(argv[1], "on") == 0)
{
val = 1;
}
else
{
val = 0;
}
write(fd, &val, 1);
return 0;
}
2.5 測試
2.5.1 加載led_drv

2.5.2 加載led_dev

2.5.3 運行測試代碼

接下來,如果想更變LED引腳,就不需要再修改led_drv.c了,直接在led_dev.c里更改相應(yīng)引腳就OK了。
三、寫在最后
賦個圖介紹下,注冊平臺dev、注冊平臺drv所觸發(fā)的match、probe過程是怎樣進(jìn)行的:
