Linux 字符设备驱动(三)---字符设备驱动开发完整流程

前言

    根据第二节《Linux 字符设备驱动(二)---字符设备驱动开发流程及主要函数定义》中,字符设备驱动开发思维导图,字符设备驱动完整代码如下。

源代码:

1、驱动层代码gpio_chrdev.c如下:

#include <linux/module.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/cdev.h>
#include <linux/ioctl.h>
#include <linux/kernel.h>
#include <linux/device.h>
#include <linux/string.h>
#include <linux/slab.h>
#include <asm/uaccess.h>
#include "gpio.h"


/*  */
struct gpio_cdev
{
	struct cdev cdev;
	int count;
	struct class *gpio_class;
	struct device *gpio_device;
};

/* 用户进程利用在对设备文件进行诸如read/write操作的时候,系统调用通过设备文件的主设备号找到相应的设备驱动程序,然后读取这个数据结构相应的函数指针,接着把控制权交给该函数,这是Linux的设备驱动程序工作的基本原理。 
*/


/* 打开 */
int gpio_open(struct inode * inode , struct file * filp )
{
	printk("[%s] %s %d : gpio_open\n", __FILE__, __func__, __LINE__);
	return 0;
}

/* 关闭 */
int gpio_release(struct inode *inode, struct file *filp)
{
	printk("[%s] %s %d : gpio_release\n", __FILE__, __func__, __LINE__);
	return 0;
}

/*读设备*/
ssize_t gpio_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos)
{
	//包含头文件#include <linux/string.h> ,可使用内核字符串处理函数
	// char kernel_buf[64] = {"this is kernel space data"};
	char kernel_buf[64] = {0};
	
	memset(kernel_buf, 0, sizeof(kernel_buf));
	strncpy(kernel_buf, "this is kernel space data", sizeof(kernel_buf));
	kernel_buf[63] = '\0';
	
	if (copy_to_user(buf, kernel_buf, strlen(kernel_buf)))
	{
		//copy_to_user失败返回未写入的字节数
		printk("[%s] %s %d : gpio_get failed!\n", __FILE__, __func__, __LINE__);
		return -EFAULT;
	}
	else
	{
		//copy_to_user成功返回0
		printk("[%s] %s %d : kernel_buf = %s\n", __FILE__, __func__, __LINE__, kernel_buf);	
	}
	
	return 0;
}


/*写设备*/
ssize_t gpio_write(struct file *filp, const char __user *buf, size_t count, loff_t *f_pos)
{
    //copy_from_user(void *to, const void __user *from, unsigned long count);
	char kernel_buf[64] = {0};
	
	if (copy_from_user(kernel_buf, buf, sizeof(kernel_buf)))
	{
		printk("[%s] %s %d : gpio_set failed!\n", __FILE__, __func__, __LINE__);
		return -EFAULT;
	}
	printk("[%s] %s %d : kernel_buf = %s\n", __FILE__, __func__, __LINE__, kernel_buf);
	
	return 0;

}


/*ioctl函数*/
// static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
static int gpio_ioctl(struct inode *inode,struct file *filp,unsigned int cmd,unsigned long arg) 
{
	static gpio_opt gpio;
	int n = 0;
	
	if (copy_from_user(&gpio, (gpio_opt *)arg, sizeof(gpio_opt)))
		return -EFAULT;
	
	switch(cmd){
		case IOCTL_GPIO_GET:
			gpio.gpio_status = 2;
			n = copy_to_user((gpio_opt *)arg, &gpio, sizeof(gpio_opt));
			if (n == 0)
			{
				printk("[%s] %s %d : gpio.gpio_num = %d gpio.gpio_status = %d\n", __FILE__, __func__, __LINE__, gpio.gpio_num, gpio.gpio_status);				
			}
			else
			{
				printk("[%s] %s %d : gpio_get failed!\n", __FILE__, __func__, __LINE__);
				return -EFAULT;
			}
			break;
		case IOCTL_GPIO_SET:
			if((gpio.gpio_num == 111) && (gpio.gpio_status == 1))//匹配条件也可修改
			{
				//可添加set操作
				printk("[%s] %s %d : gpio.gpio_num = %d gpio.gpio_status = %d\n", __FILE__, __func__, __LINE__, gpio.gpio_num, gpio.gpio_status);
			}
			else
			{
				printk("[%s] %s %d : gpio_set failed!\n", __FILE__, __func__, __LINE__);
				
			}
			break;
		default:
			return -ENOTTY; /*不能支持的命令*/
   }
   return 0;
}


/* 用来修改文件当前的读写位置  */
loff_t gpio_llseek(struct file *filp, loff_t *f_pos, int n)
{
	
}


/*  */
static struct file_operations gpio_fops = {
	.owner = THIS_MODULE,
	.open = gpio_open,
	.read = gpio_read,
	.write = gpio_write,
	.release = gpio_release,
	/* 32位系统32位的内核 */
	.unlocked_ioctl = gpio_ioctl,
	/* 32位系统64位的内核 */
	// .compat_ioctl = gpio_ioctl,
	.llseek = gpio_llseek,

};


static struct gpio_cdev *gpio_dev = NULL;
dev_t gpio_devno;
static int gpio_major = 111;
static int gpio_minor = 0;

/* 模块初始化 */
static int __init gpio_init(void)
{
	int ret = 0;

	printk("[%s] %s %d : gpio_init \n", __FILE__, __func__, __LINE__);
	/* 动态分配cdev */
	gpio_dev = kmalloc(sizeof(struct gpio_cdev), GFP_KERNEL);
	if(gpio_dev == NULL)
	{
		printk("[%s] %s %d : kmalloc failed!\n", __FILE__, __func__, __LINE__);
		return -ENOMEM;
	}
	memset(gpio_dev, 0, sizeof(struct gpio_cdev));
	
	/* 构建设备号 */
	gpio_devno = MKDEV(gpio_major, 0);

	/* 如果gpio_major不为0则静态分配设备号,否则动态分配设备号 */
	if(gpio_major)
	{
		ret = register_chrdev_region(gpio_devno, 1, "gpio_dev");
		if(ret < 0)
		{
			printk("[%s] %s %d : register_chrdev_region failed!\n", __FILE__, __func__, __LINE__);
			goto err_register_chrdev_region;
		}
		
	}
	else
	{
		ret = alloc_chrdev_region(gpio_devno, 0, 1, "gpio_dev");
		if(ret < 0)
		{
			printk("[%s] %s %d : alloc_chrdev_region failed!\n", __FILE__, __func__, __LINE__);
			goto err_alloc_chrdev_region;
		}
	}
	
	/* 从设备号中提取主设备号和次设备号 */
	gpio_major = MAJOR(gpio_devno);
	gpio_minor = MINOR(gpio_devno);

	/* 初始化cdev,建立cdev与file_operations之间的联系 */
	cdev_init(&(gpio_dev->cdev), &gpio_fops);
	gpio_dev->cdev.owner = THIS_MODULE;

	#if 0
	/* 自定义的gpio_dev结构体用cdev_alloc分配地址不合适 */
	cdev = cdev_alloc();
	cdev.owner = THIS_MODULE;
	cdev.ops = &xxx_fops;
	memset(cdev, 0, sizeof(struct cdev));
	#endif

	/* 向内核注册cdev结构体 */
	ret = cdev_add(&(gpio_dev->cdev), gpio_devno, 1);
	if(ret < 0)
	{
		printk("[%s] %s %d : cdev_add failed!\n", __FILE__, __func__, __LINE__);
		goto err_cdev_add;
	}

	/* 自动创建设备节点 */
	
	/* 创建设备类 */
	gpio_dev->gpio_class = class_create(THIS_MODULE, "gpio_class");
	if(IS_ERR(gpio_dev->gpio_class))
	{
		ret = PTR_ERR(gpio_dev->gpio_class);
		printk("[%s] %s %d : class_create failed! ret = %d\n", __FILE__, __func__, __LINE__, ret);
		goto err_class_create;
	}
	
	/* 创建设备节点 */
	gpio_dev->gpio_device = device_create(gpio_dev->gpio_class, NULL, gpio_devno, "gpio_dev", 0);
	if(IS_ERR(gpio_dev->gpio_device))
	{
		ret = PTR_ERR(gpio_dev->gpio_device);
		printk("[%s] %s %d : device_create failed! ret = %d\n", __FILE__, __func__, __LINE__, ret);
		goto err_device_create;
	}
	
#if 0
	/*
	static inline int register_chrdev(unsigned int major, const char *name,
				  const struct file_operations *fops);
	完成了设备号申请,初始化,设备注册
	*/
	register_chrdev(gpio_major, "gpio_dev",  &gpio_fops);

	gpio_dev->gpio_class = class_create(THIS_MODULE, "gpio_class");
	if(IS_ERR(gpio_dev->gpio_class))
	{
		unregister_chrdev(gpio_major,"gpio_dev");
		return -1;
	}
	gpio_dev->gpio_device = device_create(gpio_dev->gpio_class, NULL, gpio_devno, "gpio_dev", 0);
	if(IS_ERR(gpio_dev->gpio_device))
	{
		class_destroy(gpio_dev->gpio_class);
		unregister_chrdev(gpio_major,"gpio_dev");
		return -1;
	}
	
#endif


err_device_create:
	device_destroy(gpio_dev->gpio_class, gpio_devno);

err_class_create:
	class_destroy(gpio_dev->gpio_class);
	
err_cdev_add:
	cdev_del(&(gpio_dev->cdev));
	
err_alloc_chrdev_region:
	kfree(gpio_dev);
	
err_register_chrdev_region:
	unregister_chrdev_region(MKDEV(gpio_devno, 0), 1);
	return -1;
	
	return 0;
}

/* 模块卸载 */
void __exit gpio_exit(void)
{
	device_destroy(gpio_dev->gpio_class, gpio_devno);
	class_destroy(gpio_dev->gpio_class);
	cdev_del(&(gpio_dev->cdev));
	kfree(gpio_dev);
	unregister_chrdev_region(MKDEV(gpio_devno, 0), 1);
	
	printk(KERN_ALERT "ptdrv gpio exit\n");
}


module_init(gpio_init);
module_exit(gpio_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("IS_ME");
MODULE_DESCRIPTION("gpio driver");


 2、应用层代码gpio_cmd.c如下:

#include <stdio.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include "gpio.h"


int gpio_get(int fd, int *status)
{
	/* 读 */
	int ret = 0;
	gpio_opt gpio;
	memset(&gpio, 0, sizeof(gpio));
	
	gpio.gpio_num = 111;
	ret = ioctl(fd, IOCTL_GPIO_GET, &gpio);
	if(ret < 0)
	{
		printf("[%s] %s %d : ioctl: %s \n", __FILE__, __func__, __LINE__, strerror(errno));
		return -1;
	}
	*status = gpio.gpio_status;
	
	printf("[%s] %s %d : get gpio_status = %d \n", __FILE__, __func__, __LINE__, gpio.gpio_status);	
	
	return 0;
	
}

int gpio_set(int fd)
{
	/* 写 */
	int ret = 0;
	gpio_opt gpio;
	memset(&gpio, 0, sizeof(gpio));
	
	gpio.gpio_num = 111;
	gpio.gpio_status = 1;
	ret = ioctl(fd, IOCTL_GPIO_SET, &gpio);
	if(ret < 0)
	{
		printf("[%s] %s %d : ioctl: %s \n", __FILE__, __func__, __LINE__, strerror(errno));
		close(fd);
		return -1;
	}
	
	return 0;
	
}

int main(int argc, char *argv[])
{
	int fd = -1;
	int ret = 0;
	int status = 0;
	char buf[64] = {0};
	
	if(argc >= 1)
	{
		printf("gpio_cmd start!\n");
		
	}
	
	fd = open("dev/gpio_dev", O_RDWR);
	if(fd < 0)
	{
		printf("[%s] %s %d : open %s failed!\n", __FILE__, __func__, __LINE__, "dev/gpio_dev");
		return -1;
	}
	memset(buf, 0, sizeof(buf));
	sprintf(buf, "%s", "this is userspace data");
	
	ret = write(fd, buf, strlen(buf));
	if(ret < 0)
	{
		printf("[%s] %s %d : write %s failed!\n", __FILE__, __func__, __LINE__, "dev/gpio_dev");
		close(fd);
		return -1;
	}
	else
	{
		printf("[%s] %s %d : write size = %d \n", __FILE__, __func__, __LINE__, ret);
	}
	
	memset(buf, 0, sizeof(buf));
	ret = read(fd, buf, strlen(buf));
	if(ret < 0)
	{
		printf("[%s] %s %d : read %s failed!\n", __FILE__, __func__, __LINE__, "dev/gpio_dev");
		close(fd);
		return -1;
	}
	else
	{
		printf("[%s] %s %d : write buf = %s \n", __FILE__, __func__, __LINE__, buf);
	}
	
	ret = gpio_set(fd);
	if(ret < 0)
	{
		printf("[%s] %s %d : gpio_set failed!\n", __FILE__, __func__, __LINE__);
		close(fd);
		return -1;
	}
	ret = gpio_get(fd, &status);
	if(ret < 0)
	{
		printf("[%s] %s %d : gpio_get failed!\n", __FILE__, __func__, __LINE__);
		close(fd);
		return -1;
	}
	else
	{
		printf("[%s] %s %d : get gpio_status = %d \n", __FILE__, __func__, __LINE__, status);	
		
	}
	close(fd);
	return 0;
}

3、头文件gpio.h



typedef struct gpio_opt
{
	int gpio_num;
	int gpio_status;
	
}gpio_opt;

static const unsigned char GPIO_MAGIC = 'G';

#define IOCTL_GPIO_GET							_IOR(GPIO_MAGIC, 1, gpio_opt)
#define IOCTL_GPIO_SET							_IOW(GPIO_MAGIC, 2, gpio_opt)

 4、Makefile


obj-m:=gpio_chrdev.o
KDIR:=/lib/modules/$(shell uname -r)/build
PWD:=$(shell pwd)

module:
	$(MAKE) -C $(KDIR) M=$(PWD) modules

cmd:
	gcc -g gpio_cmd.c gpio.h -o gpio_cmd

clean:
	rm -rf  .*.o.cmd *.mod.o.cmd *.o *.mod.c  .*.ko.cmd *.mod.o *.ko .tmp_versions *.order *symvers *Module.markers
	

猜你喜欢

转载自blog.csdn.net/the_wan/article/details/108435862