向内核新加文件:例如 demo1.c
1. 创建并编辑新的文件
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/module.h>
#include <linux/kdev_t.h>
#include <linux/cdev.h>
#define MAJOR_NUM 255
#define MINOR_NUM 0
#define DEV_NAME "demo1"
static int open(struct inode * node, struct file * file)
{
printk("demo open...\n");
return 0;
}
static ssize_t read(struct file * file, char __user * buf, size_t len, loff_t * offset)
{
printk("demo read...\n");
return 0;
}
static ssize_t write(struct file * file, const char __user * buf, size_t len, loff_t * offset)
{
printk("demo write...\n");
return 0;
}
static int close(struct inode * node, struct file * file)
{
printk("demo close...\n");
return 0;
}
static dev_t dev_num;
static struct file_operations fops =
{
.owner = THIS_MODULE,
.open = open,
.read = read,
.write = write,
.release = close
};
static struct cdev dev;
static int __init demo_init(void)
{
dev_num = MKDEV(MAJOR_NUM, MINOR_NUM);
cdev_init(&dev, &fops);
cdev_add(&dev, dev_num, 1);
register_chrdev_region(dev_num, 1, DEV_NAME);
printk("demo_init ##########################\n");
return 0;
}
static void __exit demo_exit(void)
{
unregister_chrdev_region(dev_num, 1);
cdev_del(&dev);
printk("demo_exit ##########################\n");
}
module_init(demo_init);
module_exit(demo_exit);
2. 修改同层目录下的Makefile,增加一句
obj-$(CONFIG_XXX) += xxx.o
3. 修改同层目录下的Kconfig 增加一个新的配置
4. make menuconfig
5. make uImage
此时生成内核,按照我之前的编译文章实现板子启动Linux
6.手动创建设备节点:
mknod /dev/demo1 c 255 0