文章目录
- 字符设备驱动
- 字符设备 APP
- 生成 dev 节点的原理
- 配置内核自动创建设备节点
- 模块使用
字符设备驱动
#include "linux/device/class.h"
#include "linux/export.h"
#include "linux/uaccess.h"
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/cdev.h>
#define CHRDEVBASE_NAME "chrdevbase" /* 设备名 */
#define CHRDEVBASE_NUM 1 /* 设备数目 */
static char write_buf[100];
static char read_buf[100];
static char *string_test = "kernel data this tyustli test";
typedef struct {
dev_t dev_id; /* 设备号 */
struct cdev c_dev; /* cdev */
struct class *class; /* 类 */
struct device *device; /* 设备 */
int major; /* 主设备号 */
int minor; /* 次设备号 */
} new_chrdev_t;
new_chrdev_t new_chrdev;
static int chrdevbase_open(struct inode *inode, struct file *file)
{
printk("k: chrdevbase open\r\n");
return 0;
}
static ssize_t chrdevbase_read(struct file *file, char __user *buf,
size_t count, loff_t *ppos)
{
unsigned long ret = 0;
printk("k: chrdevbase read\r\n");
memcpy(read_buf, string_test, strlen(string_test));
ret = copy_to_user(buf, read_buf, count);
if (ret == 0) {
printk("k: read data success\r\n");
} else {
printk("k: read data failed ret = %ld\r\n", ret);
}
return ret;
}
static ssize_t chrdevbase_write(struct file *file, const char __user *buf,
size_t count, loff_t *ppos)
{
unsigned long ret = 0;
printk("k: chrdevbase write\r\n");
ret = copy_from_user(write_buf, buf, count);
if (ret == 0) {
printk("k: write data success write data is: %s\r\n", write_buf);
} else {
printk("k: write data failed ret = %ld\r\n", ret);
}
return count;
}
static int chrdevbase_release(struct inode *inode, struct file *file)
{
printk("k: chrdevbase release\r\n");
return 0;
}
static struct file_operations chrdevbase_fops = {
.owner = THIS_MODULE,
.open = chrdevbase_open,
.read = chrdevbase_read,
.write = chrdevbase_write,
.release = chrdevbase_release,
};
static int __init chrdevbase_init(void)
{
int err = 0;
err = alloc_chrdev_region(&new_chrdev.dev_id, 0, CHRDEVBASE_NUM,
CHRDEVBASE_NAME);
if (err < 0) {
printk("k: alloc chrdev region failed err = %d\r\n", err);
return -1;
}
/* get major and minor */
new_chrdev.major = MAJOR(new_chrdev.dev_id);
new_chrdev.minor = MINOR(new_chrdev.dev_id);
printk("k: newcheled major=%d,minor=%d\r\n", new_chrdev.major,
new_chrdev.minor);
new_chrdev.c_dev.owner = THIS_MODULE;
cdev_init(&new_chrdev.c_dev, &chrdevbase_fops);
err = cdev_add(&new_chrdev.c_dev, new_chrdev.dev_id, CHRDEVBASE_NUM);
if (err < 0) {
printk("k: cdev add failed err = %d\r\n", err);
goto out;
}
new_chrdev.class = class_create(CHRDEVBASE_NAME);
if (IS_ERR(new_chrdev.class)) {
printk("k: class create failed\r\n");
goto out_cdev;
}
new_chrdev.device = device_create(new_chrdev.class, NULL, new_chrdev.dev_id,
NULL, CHRDEVBASE_NAME);
if (IS_ERR(new_chrdev.device)) {
printk("k: device create failed\r\n");
goto out_class;
}
printk("k: base module init\r\n");
return 0;
out_class:
class_destroy(new_chrdev.class);
out_cdev:
cdev_del(&new_chrdev.c_dev);
out:
unregister_chrdev_region(new_chrdev.dev_id, CHRDEVBASE_NUM);
return err;
}
static void __exit chrdevbase_exit(void)
{
device_destroy(new_chrdev.class, new_chrdev.dev_id);
class_destroy(new_chrdev.class);
cdev_del(&new_chrdev.c_dev);
unregister_chrdev_region(new_chrdev.dev_id, CHRDEVBASE_NUM);
printk("k: base module exit!\r\n");
}
module_init(chrdevbase_init);
module_exit(chrdevbase_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("tyustli");
MODULE_INFO(intree, "Y"); /* loading out-of-tree module taints kernel */
字符设备 APP
#include "stdio.h"
#include "unistd.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "fcntl.h"
#include "stdlib.h"
#include "string.h"
static char usrdata[] = { "user data!" };
int main(int argc, char *argv[])
{
int fd, retvalue;
char *filename;
char readbuf[100], writebuf[100];
if (argc != 3) {
printf("u: error Usage!\r\n");
return -1;
}
filename = argv[1];
/* 打开驱动文件 */
fd = open(filename, O_RDWR);
if (fd < 0) {
printf("u: can't open file %s\r\n", filename);
return -1;
}
/* 从驱动文件读取数据 */
if (atoi(argv[2]) == 1) {
retvalue = read(fd, readbuf, 50);
if (retvalue < 0) {
printf("u: read file %s failed!\r\n", filename);
} else {
/* 读取成功,打印出读取成功的数据 */
printf("u: read data:%s\r\n", readbuf);
}
}
/* 向设备驱动写数据 */
if (atoi(argv[2]) == 2) {
memcpy(writebuf, usrdata, sizeof(usrdata));
retvalue = write(fd, writebuf, 50);
if (retvalue < 0) {
printf("u: write file %s failed!\r\n", filename);
}
}
/* 关闭设备 */
retvalue = close(fd);
if (retvalue < 0) {
printf("u: can't close file %s\r\n", filename);
return -1;
}
return 0;
}
生成 dev 节点的原理
dev 目录下的节点不是由驱动本身生成的,是由文件系统中的工具 mdev 生成的;
当系统启动后,加载完内核再去加载文件系统,执行文件系统中的脚本,脚本会执行 mdev -s
命令(即执行 mdev
程序),该命令会去遍历/sys/class
下的所有文件,寻找所有名为 dev
的文件,dev
文件保存了每个驱动的主设备号、次设备号,以及驱动名。
生成设备节点的依赖有两个
- /sys/class 有驱动的节点信息,即 dev 文件
- 执行 mdev -s创建 /dev 下的节点
ls /sys/class
这里文件是有的,这个时候执行
mdev -s
并查看 /dev/
目录
ls /dev
可以看到设备节点文件自动创建了
配置内核自动创建设备节点
配置 linux 内核使之自动创建设备节点文件
在内核中 make menuconfig
使能 CONFIG_UEVENT_HELPER
模块使用
linux 自动创建设备节点之后,就不用手动创建了,可以直接查看 /dev 目录下已经生成了设备节点文件
ls /dev
模块安装
modprobe my_module
模块使用
lib/modules/6.5.7+/my_app /dev/chrdevbase 1