Character device driver templates

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/Sophisticated_/article/details/88285396

In character device driver module loading function should implement application and device number cdevregistered in uninstall function should be to achieve the release of numbers and equipment cdevwrite-offs.

Engineers often accustomed to the device is defined as a device-dependent structure, comprising the cdev, private data signals and the amount of information equipment involved. Common device structure, such as load and unload the module function code form shown in Listing

//设备结构体
struct xxx_dev_t
{
 	struct cdev cdev;
 	...
} xxx_dev;

//设备驱动模块加载函数
static int _ _init xxx_init(void)
{
	...
	cdev_init(&xxx_dev.cdev, &xxx_fops); //初始化 cdev
	xxx_dev.cdev.owner = THIS_MODULE;
	//获取字符设备号
	if (xxx_major)
	{
		register_chrdev_region(xxx_dev_no, 1, DEV_NAME);
	}
	else
	{
		alloc_chrdev_region(&xxx_dev_no, 0, 1, DEV_NAME);
	}

	ret = cdev_add(&xxx_dev.cdev, xxx_dev_no, 1); //注册设备
	...
}

/*设备驱动模块卸载函数*/
static void _ _exit xxx_exit(void)
{
	unregister_chrdev_region(xxx_dev_no, 1); //释放占用的设备号
	cdev_del(&xxx_dev.cdev); //注销设备
	...
}

file_operationsThe member function structure is a character device driver and interface to the kernel, it is the ultimate space for users who implement Linux system calls. Most character device driver will be achieved read(), write()and ioctl()the form of these three functions of function, common character device driver shown in Listing

/* 读设备*/
ssize_t xxx_read(struct file *filp, char __user *buf, size_t count, loff_t*f_pos)
{
	...
	copy_to_user(buf, ..., ...);
	...
}
/* 写设备*/
ssize_t xxx_write(struct file *filp, const char __user *buf, size_t count, loff_t *f_pos)
{
	...
	copy_from_user(..., buf, ...);
	...
}
/* ioctl 函数 */
int xxx_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
{
	...
	switch (cmd)
	{
		case XXX_CMD1:
			...
			break;
		case XXX_CMD2:
			...
			break;
		default:
			/* 不能支持的命令 */
			return - ENOTTY;
	}
	return 0;
}

Device driver read function, the filp file structure pointer, buf is the address of the user memory space, the address space in the kernel is not directly readable, count the number of bytes to read, read f_pos position with respect to the beginning of the file offset

Write function device driver, the filp file structure pointer, buf is the address of the user memory space, the address space in the kernel is not directly readable, count the number of bytes to write, write f_pos position relative to the beginning of the file offset

Since the memory kernel space and user space is not directly visits, thus by means of the function copy_from_user()is completed the user space to kernel space copy function copy_to_user()copy completion kernel space to user space

cmd parameter I / O control function is pre-defined I / O control commands, and arg is a parameter corresponding to the command

In the character device driver, the need to define a file_operations instance, the device drivers and the specific function assigned to the members file_operations

struct file_operations xxx_fops =
{
	.owner = THIS_MODULE,
	.read = xxx_read,
	.write = xxx_write,
	.ioctl = xxx_ioctl,
	...
};

Xxx_fops above code on line 11 cdev_init(&xxx_dev.cdev, &xxx_fops)is establishing a connection with the statements of cdev

The following figure shows the relationship between the access procedure of the character device drivers, device configuration, device drivers and character character and a character device driver and user-space device
Here Insert Picture Description

Guess you like

Origin blog.csdn.net/Sophisticated_/article/details/88285396