首页 > 技术文章 > Ubuntu系统添加驱动

AaronSeeWorld 2020-08-02 10:24 原文

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

 

原文链接 https://blog.csdn.net/ARAFATms/article/details/79397800

推荐阅读