linux 字符设备 平台总线,3. 字符设备驱动-总线设备驱动模型写法

一、引言:

在 字符设备驱动的传统写法 中,介绍了字符设备的传统写法。从代码中我们可以看到,使用的引脚,写死在代码中了,如果更改硬件资源,比如将GPIO3_4改成GPIO3_5,那就需要重新编译这个驱动程序,如果该驱动程序是放在内核里的,那么就需要重新编译内核。并且,在需要更改硬件资源的时候都需要去阅读驱动源码,对于没有写驱动能力的人来说,这也是挺痛苦的。在Linux内核持续发展中,改进了写驱动程序的方法,使用总线设备驱动模型。

为了方便对比学习,我们在上节传统字符设备写法基础上进行修改。总线设备驱动模型将驱动程序分成了两部分led_dev 和 led_drv ;dev部分指定硬件资源,drv分配设置fileoperations结构体然后根据硬件资源来操作硬件 。

二、BUS - dev

现在我们来看下,内核中又是如何指定硬件资源的?以面向对象的思想的方法,在内核里它定义一个dev的时候,也是去分配设置某个结构体,这个结构体就是平台设备。

平台设备使用struct platform_device来描述:

struct platform_device {

const char * name; //设备名称,要与platform_driver的name一样,

//这样总线才能匹配成功

u32 id; //id号,插入总线下相同name的设备编号(一个驱动可以有多个设备),

//如果只有一个设备填-1

struct device dev; //内嵌的具体的device结构体,其中成员platform_data,是个void *类型,

//可以给平台driver提供各种数据(比如:GPIO引脚等等)

u32 num_resources; //资源数量,

struct resource * resource; //资源结构体,保存设备的信息

};

resource资源,就是用来记录地址,地址等资源,供drv使用。

其中resource资源结构体,如下:

struct resource {

resource_size_t start; //起始资源,如果是地址的话,必须是物理地址

resource_size_t end; //结束资源,如果是地址的话,必须是物理地址

const char *name; //资源名

unsigned long flags; //资源的标志

//比如IORESOURCE_MEM,表示地址资源, IORESOURCE_IRQ表示中断引脚... ...

struct resource *parent, *sibling, *child; //资源拓扑指针父、兄、子,可以构成链表

};

分析到这里,可以看出,对于 bus-dev 这边,就是去定义一个一个的platform_device,然后去注册到bus总线上。对于 bus-drv 那边,也类似,定义了一个一个的platform_driver然后注册到bus总线上。

内核里有那么多的platform_device,上百个都有可能,同时也存在那么多个platform_driver;问题来了,platform_driver该从哪个platform_device里获得指定的硬件资源呢?或者说,对于指定的platform_device又是给内核中那么多的platform_driver中的哪个提供硬件资源的描述呢?他们之间需要有个匹配,在BUS(我们平时使用的一般为platform_bus_type)里有个match函数 ,就是用来匹配drv和dev。如果匹配,则调用drv->probe函数;至于probe函数里做什么,由驱动开发者决定;总线设备驱动模型不过提供了这样一种机制。它并不是驱动程序的核心,核心仍然是drv里的分配、设置、注册file_operations结构体。

接下来看总线里的match函数是如何确定dev和drv是否匹配的。总线下面挂载着一系列的dev和一系列的drv,设备、驱动匹配时就是通过match函数来两两比较的,一旦match成功,则调用drv里的probe函数。

6325f723509b

机制讲完,开始写代码。

2.1 编写 led_dev

2.1.1 首先分配设置一个平台 dev

static struct platform_device led_dev = {

.name = "myled", //对应的platform_driver驱动的名字

.id = -1, //表示只有一个设备

.num_resources = ARRAY_SIZE(led_resource), //资源数量,ARRAY_SIZE()函数:获取数量

.resource = led_resource, //资源数组led_resource

.dev = {

.release = led_release, //释放函数,必须向内核提供一个release函数, 、

//否则卸载时,内核找不到该函数会报错

},

};

.name 设置平台设备中的名字,用于与平台驱动匹配;

.resource 资源,用于描述设备信息,具体类型通过resource中的flags标识;资源类型主要有:

6325f723509b

好像都没有指明引脚的资源,那怎么办?反正这个平台资源是自己使用的,我们先假设其为MEM资源,在驱动解析使用时,并不把他当成是MEM,内存资源,直接当成一个引脚

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

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#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)

/* 分配/设置/注册一个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 首先还是先分配设置一个平台 drv

struct platform_driver led_drv = {

.probe = led_probe,

.remove = led_remove,

.driver = {

.name = "myled",

}

};

经过对平台总线机制的分析,我们知道在平台drv中也有个名字,就是用这个名字("myled")来和平台dev中的名字做匹配的,一旦匹配,则调用平台drv中的probe函数。接下来编写probe函数,其参数中有个 platform_device 平台 dev ,在传统的字符设备写法中,在入口函数中直接注册了字符设备;file_operations中的open、write直接使用了led_pin,led_pin是直接写死在驱动代码中的;现在,我们要使用平台总线的方法来写,这个引脚资源需要从对应的平台 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;

/* 根据platform_device的资源进行ioremap

参数 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

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#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. 分配/设置/注册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<

*gpio_con |= (1<

return 0;

}

static ssize_t led_write (struct file *filp, const char __user *buf, size_t size, loff_t *off)

{

/* 根据APP传入的值来设置LED引脚 */

unsigned char val;

int pin = led_pin & 0xffff;

copy_from_user(&val, buf, 1);

if (val)

{

/* 点灯 */

*gpio_dat &= ~(1<

}

else

{

/* 灭灯 */

*gpio_dat |= (1<

}

return 1; /* 已写入1个数据 */

}

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;

/* 根据platform_device的资源进行ioremap

参数 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

#include

#include

#include

/* 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 \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

6325f723509b

2.5.2 加载led_dev

6325f723509b

2.5.3 运行测试代码

6325f723509b

接下来,如果想更变LED引脚,就不需要再修改led_drv.c了,直接在led_dev.c里更改相应引脚就OK了。

三、写在最后

赋个图介绍下,注册平台dev、注册平台drv所触发的match、probe过程是怎样进行的:

6325f723509b

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值