Linux内核驱动之HelloWord本地驱动

###################################################################

linux内核驱动模块之hello

###################################################################
/* hello.c */
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <asm/uaccess.h>

MODULE_LICENSE("HELLO");
MODULE_AUTHOR("WENSION");

#define DEV_NAME "Hello"

static ssize_t HelloRead(struct file*,char *,size_t,loff_t*);
static ssize_t HelloWrite(struct file*,const char *,size_t,loff_t*);

static int char_major = 0;
static int Char_Data  = 0;        //"Test_Char"设备的全局变量

struct file_operations hello_fops =
{
    .read = HelloRead,
    .write= HelloWrite
};


static int hello_init(void)
{
//    printk(KERN_ALERT "Hello, world\n");
    int ret;
    
    ret = register_chrdev(char_major,DEV_NAME,&hello_fops);
    
    //注册设备驱动
    if(ret<0)
    {
        printk(KERN_ALERT "HelloDev Reg Faile!\n");
    }
    else
    {
        printk(KERN_ALERT "HelloDev Reg Success!\n");
        char_major = ret;
        printk(KERN_ALERT "Major = %d\n",char_major);
    }
    
    return ret;
}
static void hello_exit(void)
{
    printk(KERN_ALERT "Goodbye, Hello world\n");
    unregister_chrdev(char_major,DEV_NAME);        //注销设备驱动
    return;
}

/*设备驱动读函数*/
static ssize_t HelloRead(struct file *filp,char *buf,size_t len,loff_t *off)
{
    printk(KERN_ALERT "Hello Read Data\n");
    if(copy_to_user(buf,&Char_Data,sizeof(int)))
    {
        return -EFAULT;
    }
    return sizeof(int);
}

/*设备驱动写函数*/
static ssize_t HelloWrite(struct file *filp,const char *buf,size_t len,loff_t *off)
{
    printk(KERN_ALERT "Hello Write Data\n");
    if(copy_from_user(&Char_Data,buf,sizeof(int)))
    {
        return -EFAULT;
    }
    return sizeof(int);
}

module_init(hello_init);

module_exit(hello_exit);


###################################################################

Makefile

###################################################################
obj-m += hello.o
    CURRENT_PATH:=$(shell pwd)
    LINUX_KERNEL_PATH:=/lib/modules/$(shell uname -r)/build
all:
    $(MAKE) -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) modules
clean:
    rm -rf *.cmd *.o *.mod.c *.ko *.symvers *.order


###################################################################

加载内核驱动

###################################################################
sudo insmod hello.ko

查看内核分配的主设备号:

cat /proc/devices | grep Hello
250 Hello

创建设备节点:

sudo mknod -m 666 /dev/hello c 250 0


###################################################################

驱动应用程序

###################################################################
/*test_app.c*/
#include<sys/types.h>
#include<sys/stat.h>
#include<stdio.h>
#include<fcntl.h>
#include<unistd.h>

#define DEV_NAME "/dev/hello"

int main()
{
    int fd,num;
    //打开设备文件
    fd = open(DEV_NAME,O_RDWR,S_IRUSR | S_IWUSR);
    if(fd<0)
    {
        printf("Open Hello Device Fail!\n");
        return -1;
    }
    
    //读取当前设备
    read(fd,&num,sizeof(int));
    printf("The Hello Device is:%d\n",num);
    
    printf("Please input a number written to chardev: ");
    scanf("%d",&num);
    
    //写入数值到当前设备
    write(fd,&num,sizeof(int));
    
    //读取写入数值
    read(fd,&num,sizeof(int));
    printf("The Hello Device is:%d\n",num);
    
    close(fd);
    return 0;
}

查看模块加载信信息:

dmesg | tail -n 10

cat /var/log/syslog | grep Hello

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值