ubuntu 添加字符设备驱动程序

本文介绍如何在Ubuntu系统中从零开始制作一个简单的字符设备驱动,包括编写C语言驱动代码、创建Makefile、编译模块、加载及卸载驱动模块等步骤,并提供了测试驱动程序的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ubuntu设备驱动是由装载模块的方式进行的

2.6.x核心模块在http://www.ibm.com/developerworks/cn/linux/l-module26/上可以下载

按照定义创建驱动文件

创建c文件 Drive.c

#include "linux/kernel.h"
#include "linux/module.h"
#include "linux/fs.h"
#include "linux/init.h"
#include "linux/types.h"
#include "linux/errno.h"
#include "linux/uaccess.h"
#include "linux/kdev_t.h"
 
#define MAX_SIZE 1024
 
int my_open(struct inode *inode, struct file *file);
int my_release(struct inode *inode, struct file *file);
ssize_t my_read(struct file *file, char __user *user, size_t t, loff_t *f);
ssize_t my_write(struct file *file, const char __user *user, size_t t, loff_t *f);
 
char message[MAX_SIZE] = "this is arafat(very shuai boy)'s Drive";  //打开设备时会显示的消息
int device_num;//设备号
char* devName = "arafatsDrive";//设备名
 
struct file_operations pStruct =
{ 
	open:my_open, 
	release:my_release, 
	read:my_read, 
	write:my_write, 
};
 
/* 注册 */
int init_module()
{
	int ret;
 
	ret = register_chrdev(0, devName, &pStruct);
	if (ret < 0)
	{
		printk("failed to register_chrdev.\n");
		return -1;
	}
	else
	{
		printk("the lgsDrive has been registered!\n");
		printk("id: %d\n", ret);
		device_num = ret;
 
		return 0;
	}
}
 
//注销
void cleanup_module()
{
	unregister_chrdev(device_num, devName);
	printk("unregister successful.\n");
}
 
 
//打开
int my_open(struct inode *inode, struct file *file)
{
	printk("open lgsDrive OK!\n");
	try_module_get(THIS_MODULE);
	return 0;
}
 
//关闭
int my_release(struct inode *inode, struct file *file)
{
	printk("Device released!\n");
	module_put(THIS_MODULE);
 
	return 0;
}
 
 
//读设备里的信息
ssize_t my_read(struct file *file, char __user *user, size_t t, loff_t *f)
{
	if(copy_to_user(user,message,sizeof(message)))
	{
		return -2;
	}
	return sizeof(message);
}
 
//向设备里写信息
ssize_t my_write(struct file *file, const char __user *user, size_t t, loff_t *f)
{
	if(copy_from_user(message,user,sizeof(message)))
	{
		return -3;
	}
	return sizeof(message);
}

相同目录下创建 Makefile

    obj-m := arafatsDrive.o    #这里是上面所创建的c文件名.o
    PWD := $(shell pwd)
    KERNELDIR := /usr/src/linux-4.9.84    #你要安装mod的内核版本 
all:
	make -C $(KERNELDIR) M=$(PWD) modules
 
 
.PHONY: clean
clean:
	rm -rf *.o *~ core *.ko *.mod.c modules.order Module.symvers

以下全部命令都得用root用户

make

make完了以后用ls命令查看 xxxx.ko是我们的驱动程序

用下面的命令装在我们的模块

insmod xxxxxx.ko

利用lsmode命令可以查看我们的设备

这时候使用dmesg命令。可以查看源代码Drive.c中,设备注册函数init_module()中的prink的输出

cat /proc/devices

查看自己设备的设备号

mknod /dev/Device c 243 0 

上面命令来加入自己的设备 Device为设备名字 c 为字符型设备 243为设备号

然后使用 ls /dev就可以看到自己的设备了


下面编写测试程序test.c

#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#define MAX_SIZE 1024
 
int main(void)
{
	int fd;
	char buf[MAX_SIZE];	//缓冲区
	char get[MAX_SIZE];	//要写入的信息
 
	char dir[50] = "/dev/arafatsDevice";    //设备名
 
	fd = open(dir, O_RDWR | O_NONBLOCK);
 
	if (fd != -1)
	{
		//读初始信息
		read(fd, buf, sizeof(buf));
		printf("%s\n", buf);
 
		//写信息
		printf("input :");
		gets(get);
		write(fd, get, sizeof(get));
 
		//读刚才写的信息
		read(fd, buf, sizeof(buf)); 
		printf("device Message: %s\n", buf);
		
		close(fd);
		return 0;
	}
	else
	{
		printf("Device open failed\n");
		return -1;
	}
}

测试成功 


利用以下命令卸载设备和模块

rm /dev/lgsDevice
rmmod lgsDrive

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值