当前位置首页 > Ubuntu知识

Ubuntu系统添加驱动

阅读次数:243 次  来源:admin  发布时间:

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)

ize_t my_read(struct file *file, char __user *user, size_t t, loff_t *f)

ize_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";//设备名

truct 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)

{

rintk("failed to register_chrdev.\n")

return -1

}

else

{

rintk("the lgsDrive has been registered!\n")

rintk("id: %d\n", ret)

device_num = ret

return 0

}

}

//注销

void cleanup_module()

{

unregister_chrdev(device_num, devName)

rintk("unregister successful.\n")

}

//打开

int my_open(struct inode *inode, struct file *file)

{

rintk("open lgsDrive OK!\n")

try_module_get(THIS_MODULE)

return 0

}

//关闭

int my_release(struct inode *inode, struct file *file)

{

rintk("Device released!\n")

module_put(THIS_MODULE)

return 0

}

//读设备里的信息

ize_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)

}

//向设备里写信息

ize_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

上一篇:Ubuntu在laptop-mode(用电池的模式)下USB鼠标失效问题
下一篇:Debian安装/设置笔记