树莓派linux led字符设备驱动( platform)

一、platform 设备驱动

  Linux 系统要考虑到驱动的可重用性,因此提出了驱动的分离与分层这样的软件思路,在这个思路下诞生了platform 设备驱动,也叫做平台设备驱动。platform引入了总线(bus)、驱动(driver)和设备(device)模型。

1.1、platform 总线

  Linux系统内核使用bus_type结构体表示总线,此结构体定义在文件include/linux/device.h中。platform 总线是 bus_type 的一个具体实例,定义在文件 drivers/base/platform.c中

struct bus_type platform_bus_type = {
    
    
	.name = "platform",
	.dev_groups = platform_dev_groups,
	.match = platform_match,
	.uevent = platform_uevent,
	.pm = &platform_dev_pm_ops,
};

  platform_bus_type 就是 platform 平台总线,其中 platform_match 就是匹配函数。

1.2、platform 驱动

  platform_driver 结 构 体 表 示 platform 驱 动 , 此 结 构 体 定 义 在 文 件include/linux/platform_device.h 中

struct platform_driver {
    
    
	int (*probe)(struct platform_device *);
	int (*remove)(struct platform_device *);
	void (*shutdown)(struct platform_device *);
	int (*suspend)(struct platform_device *, pm_message_t state);
	int (*resume)(struct platform_device *);
	struct device_driver driver;
	const struct platform_device_id *id_table;
	bool prevent_deferred_probe;
};

  在编写 platform 驱动的时候,首先定义一个 platform_driver 结构体变量,然后实现结构体中的各个成员变量,重点是实现匹配方法以及 probe 函数。当驱动和设备匹配成功以后 probe函数就会执行,具体的驱动程序在 probe 函数里面编写,比如字符设备驱动等等。当我们定义并初始化好 platform_driver 结构体变量以后,需要在驱动入口函数里面调用platform_driver_register 函数向 Linux 内核注册一个 platform 驱动。
  platform 驱动框架如下所示:

/* 设备结构体 */
struct xxx_dev
{
    
    
	struct cdev cdev;
	/* 设备结构体其他具体内容 */
};

struct xxx_dev xxxdev; /* 定义个设备结构体变量 */

static int xxx_open(struct inode *inode, struct file *filp)
{
    
    
	/* 函数具体内容 */
	return 0;
}
static ssize_t xxx_write(struct file *filp, const char __user *buf,size_t cnt, loff_t *offt)
{
    
    
	/* 函数具体内容 */
	return 0;
}
/*
* 字符设备驱动操作集
*/
static struct file_operations xxx_fops = 
{
    
    
	.owner = THIS_MODULE,
	.open = xxx_open,
	.write = xxx_write,
};
/*
* platform 驱动的 probe 函数
* 驱动与设备匹配成功以后此函数就会执行
*/
static int xxx_probe(struct platform_device *dev)
{
    
    
 	......
	cdev_init(&xxxdev.cdev, &xxx_fops); /* 注册字符设备驱动 */
	/* 函数具体内容 */
	return 0;
}

static int xxx_remove(struct platform_device *dev)
{
    
    
	......
	cdev_del(&xxxdev.cdev);/* 删除 cdev */
	/* 函数具体内容 */
	return 0;
}

/* 匹配列表 */
static const struct of_device_id xxx_of_match[] = 
{
    
    
	{
    
     .compatible = "xxx-gpio" },
	{
    
     /* Sentinel */ }
};

/*
* platform 平台驱动结构体
*/
static struct platform_driver xxx_driver = 
{
    
    
	.driver = {
    
    
		.name = "xxx",
		.of_match_table = xxx_of_match,
	},
 	.probe = xxx_probe,
	.remove = xxx_remove,
};
/* 驱动模块加载 */
static int __init xxxdriver_init(void)
{
    
    
	return platform_driver_register(&xxx_driver);
}
/* 驱动模块卸载 */
static void __exit xxxdriver_exit(void)
{
    
    
	platform_driver_unregister(&xxx_driver);
}

module_init(xxxdriver_init);
module_exit(xxxdriver_exit);
MODULE_LICENSE("GPL");

1.3、platform 设备

  platform_device 这个结构体表示 platform 设备,如果内核支持设备树的话就不要再使用 platform_device 来描述设备了,而改用设备树去描述了。 platform_device 结构体定义在文件include/linux/platform_device.h 中

二、编写驱动

  因为 Linux 下的驱动分离与分层,引出了总线、设备和驱动这样的驱动框架。从而 Linux 内核提出来 platform 这个虚拟总线,相应的也有 platform 设备和 platform 驱动。其中总线是 Linux 内核提供的,在使用设备树的时候,设备的描述被放到了设备树中,因此platform 驱动编写只需要实现 platform_driver 即可。

2.1 修改设备树文件

ledtest{
    
    
	#address-cells = <1>;
	#size-cells = <1>;
	compatible = "ledtest";
	pinctrl-names = "default";
	linux,default-trigger = "input";
	gpios = <&gpio 26 0>;
	status = "okay";
};

2.2、LED 驱动修改

#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of_gpio.h>
#include <linux/semaphore.h>
#include <linux/timer.h>
#include <linux/irq.h>
#include <linux/wait.h>
#include <linux/poll.h>
#include <linux/fs.h>
#include <linux/fcntl.h>
#include <linux/platform_device.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>


#define LEDDEV_CNT		1				/* 设备号长度 	*/
#define LEDDEV_NAME		"ledtest"	/* 设备名字 	*/
#define LEDOFF 			0
#define LEDON 			1

/* leddev设备结构体 */
struct leddev_dev{
    
    
	dev_t devid;				/* 设备号	*/
	struct cdev cdev;			/* cdev		*/
	struct class *class;		/* 类 		*/
	struct device *device;		/* 设备		*/
	int major;					/* 主设备号	*/	
	struct device_node *node;	/* LED设备节点 */
	int led0;					/* LED灯GPIO标号 */
};

struct leddev_dev leddev; 		/* led设备 */

/*
 * @description		: LED打开/关闭
 * @param - sta 	: LEDON(0) 打开LED,LEDOFF(1) 关闭LED
 * @return 			: 无
 */
void led0_switch(u8 sta)
{
    
    
	if (sta == LEDON )
		gpio_set_value(leddev.led0, 0);
	else if (sta == LEDOFF)
		gpio_set_value(leddev.led0, 1);	
}

/*
 * @description		: 打开设备
 * @param - inode 	: 传递给驱动的inode
 * @param - filp 	: 设备文件,file结构体有个叫做private_data的成员变量
 * 					  一般在open的时候将private_data指向设备结构体。
 * @return 			: 0 成功;其他 失败
 */
static int led_open(struct inode *inode, struct file *filp)
{
    
    
	filp->private_data = &leddev; /* 设置私有数据  */
	return 0;
}

/*
 * @description		: 向设备写数据 
 * @param - filp 	: 设备文件,表示打开的文件描述符
 * @param - buf 	: 要写给设备写入的数据
 * @param - cnt 	: 要写入的数据长度
 * @param - offt 	: 相对于文件首地址的偏移
 * @return 			: 写入的字节数,如果为负值,表示写入失败
 */
static ssize_t led_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offt)
{
    
    
	int retvalue;
	unsigned char databuf[2];
	unsigned char ledstat;

	retvalue = copy_from_user(databuf, buf, cnt);
	if(retvalue < 0) {
    
    

		printk("kernel write failed!\r\n");
		return -EFAULT;
	}
	
	ledstat = databuf[0];
	if (ledstat == LEDON) {
    
    
		led0_switch(LEDON);
	} else if (ledstat == LEDOFF) {
    
    
		led0_switch(LEDOFF);
	}
	return 0;
}

/* 设备操作函数 */
static struct file_operations led_fops = {
    
    
	.owner = THIS_MODULE,
	.open = led_open,
	.write = led_write,
};

/*
 * @description		: flatform驱动的probe函数,当驱动与
 * 					  设备匹配以后此函数就会执行
 * @param - dev 	: platform设备
 * @return 			: 0,成功;其他负值,失败
 */
static int led_probe(struct platform_device *dev)
{
    
    	
	printk("led driver and device was matched!\r\n");
	/* 1、设置设备号 */
	if (leddev.major) {
    
    
		leddev.devid = MKDEV(leddev.major, 0);
		register_chrdev_region(leddev.devid, LEDDEV_CNT, LEDDEV_NAME);
	} else {
    
    
		alloc_chrdev_region(&leddev.devid, 0, LEDDEV_CNT, LEDDEV_NAME);
		leddev.major = MAJOR(leddev.devid);
	}

	/* 2、注册设备      */
	cdev_init(&leddev.cdev, &led_fops);
	cdev_add(&leddev.cdev, leddev.devid, LEDDEV_CNT);

	/* 3、创建类      */
	leddev.class = class_create(THIS_MODULE, LEDDEV_NAME);
	if (IS_ERR(leddev.class)) {
    
    
		return PTR_ERR(leddev.class);
	}

	/* 4、创建设备 */
	leddev.device = device_create(leddev.class, NULL, leddev.devid, NULL, LEDDEV_NAME);
	if (IS_ERR(leddev.device)) {
    
    
		return PTR_ERR(leddev.device);
	}

	/* 5、初始化IO */	
	leddev.node = of_find_node_by_path("/ledtest");
	if (leddev.node == NULL){
    
    
		printk("ledtest node nost find!\r\n");
		return -EINVAL;
	} 
	
	leddev.led0 = of_get_named_gpio(leddev.node, "gpios", 0);
	if (leddev.led0 < 0) {
    
    
		printk("can't get gpios\r\n");
		return -EINVAL;
	}

	gpio_request(leddev.led0, "led0");
	gpio_direction_output(leddev.led0, 1); /* led0 IO设置为输出,默认高电平	*/
	return 0;
}

/*
 * @description		: platform驱动的remove函数,移除platform驱动的时候此函数会执行
 * @param - dev 	: platform设备
 * @return 			: 0,成功;其他负值,失败
 */
static int led_remove(struct platform_device *dev)
{
    
    
	gpio_set_value(leddev.led0, 1); 	/* 卸载驱动的时候关闭LED */

	cdev_del(&leddev.cdev);				/*  删除cdev */
	unregister_chrdev_region(leddev.devid, LEDDEV_CNT); /* 注销设备号 */
	device_destroy(leddev.class, leddev.devid);
	class_destroy(leddev.class);
	return 0;
}

/* 匹配列表 */
static const struct of_device_id led_of_match[] = {
    
    
	{
    
     .compatible = "ledtest" },
	{
    
     /* Sentinel */ }
};

/* platform驱动结构体 */
static struct platform_driver led_driver = {
    
    
	.driver		= {
    
    
		.name	= "ledtest",			/* 驱动名字,用于和设备匹配 */
		.of_match_table	= led_of_match, /* 设备树匹配表 		 */
	},
	.probe		= led_probe,
	.remove		= led_remove,
};
		
/*
 * @description	: 驱动模块加载函数
 * @param 		: 无
 * @return 		: 无
 */
static int __init leddriver_init(void)
{
    
    
	return platform_driver_register(&led_driver);
}

/*
 * @description	: 驱动模块卸载函数
 * @param 		: 无
 * @return 		: 无
 */
static void __exit leddriver_exit(void)
{
    
    
	platform_driver_unregister(&led_driver);
}

module_init(leddriver_init);
module_exit(leddriver_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("zsx");




2.3、编写测试APP

  测试 APP 使用https://blog.csdn.net/xxxx123041/article/details/120421290中的 App.c 即可。

三、运行测试

  将编译出来的 led.ko文件拷贝到/lib/modules/5.10.17-v7l+/ 目录中,输入如下命令加载 led.ko 驱动模块:

sudo depmod //第一次加载驱动的时候需要运行此命令
sudo modprobe ledtest//加载驱动
dmesg //查看模块打印信息

  驱动模块加载完成以后到/sys/bus/platform/drivers/目录下查看驱动是否存在,我们在leddriver.c 中设置 led_driver (platform_driver 类型)的 name 字段为“ ledtest”,因此会在/sys/bus/platform/drivers/目录下存在名为“ledtest”这个文件
  同理,在/sys/bus/platform/devices/目录下也存在 led 的设备文件,也就是设备树中 ledtest这个节点

猜你喜欢

转载自blog.csdn.net/xxxx123041/article/details/120434778