2017-2018-1 20155202实验四 外设驱动程序设计
2017-2018-1 20155202实验四 外设驱动程序设计
一、学习笔记:
本章内容:
- Linux设备驱动的基本概念
- Linux设备驱动程序的基本功能
- linux设备驱动的运作过程
- 常见设备驱动接口函数
- 掌握LCD设备驱动程序编写步骤
- 掌握键盘设备驱动程序编写步骤
设备驱动简介
- 设备驱动程序是内核的一部分。
- OS通过各种驱动程序来操作硬件设备,设备驱动程序是内核的一部分,硬件驱动程序是OS最基本的组成部分。
- Linux将最基本的核心代码编译在内核当中,其他代码编译到内核或者内核的模块文件,需要时再加载。常见的内核模块驱动程序比如声卡和网卡,linux基础驱动包括CPU,PCI总线,TCP/IP协议,APM(高级电源管理)等。
- 加载驱动就是加载内核模块。
- lsmod列出当前系统中加载的模块
设备驱动程序与外界的接口
设备驱动程序必须为内核或者其子系统提供一个标准接口。
设备驱动编程
- 设备驱动程序以模块的方式动态加载到内核中。在驱动开发时没有main()函数,模块在调用insmod命令时被加载,在该函数中完成设备的注册。调用rmmod命令时被卸载。设备完成注册加载后,用户的应用程序可以对该设备进行一定的操作,如open()、read()、write()等。
- 字符设备的注册
- 在内核中使用struct cdev结构来描述字符设备,我们在驱动设备中将已分配到的设备号以及设备操作接口(struct file_operations结构)赋予struct cdev结构变量。
- 使用cdev_alloc()函数向系统申请分配struct cdev结构,再用cdev_init()函数初始化已分配到的结构与file_operations结构关联。
- 调用cdev_add()函数将设备号与struct cdev结构进行关联并向内核正式报告新设备的注册,新设备可以使用了。
- 设备驱动结构函数
- 打开设备的函数接口open()
- 释放设备的函数接口realease()
读写设备read() write()函数

根据书第十一章最后关于字符设备的test实验中代码,将test_drv.c,test.c,Makefile,test_drv_load,test_drv_unload文件敲入,保存在文件夹内。[注]:敲入书中代码时要多检查,避免错误。
这是test_drv.c的源代码:
#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/types.h>
#include <linux/errno.h>
#include <linux/cdev.h>
#include <asm/uaccess.h>
#define TEST_DEVICE_NAME "test_dev"
#define BUFF_SZ 1024
static struct cdev test_dev;
unsigned int major =0;
static char *data = NULL;
/*º¯ÊýÉùÃ÷*/
static ssize_t test_read(struct file *file, char *buf, size_t count, loff_t *f_pos);
static ssize_t test_write(struct file *file,const char *buffer, size_t count,loff_t *f_pos);
static int test_open(struct inode *inode, struct file *file);
static int test_release(struct inode *inode,struct file *file);
static ssize_t test_read(struct file *file, char *buf, size_t count, loff_t *f_pos)
{
int len;
if (count < 0 )
{
return -EINVAL;
}
len = strlen(data);
count = (len > count)?count:len;
if (copy_to_user(buf, data, count))
{
return -EFAULT;
}
return count;
}
static ssize_t test_write(struct file *file, const char *buffer, size_t count, loff_t *f_pos)
{
if(count < 0)
{
return -EINVAL;
}
memset(data, 0, BUFF_SZ);
count = (BUFF_SZ > count)?count:BUFF_SZ;
if (copy_from_user(data, buffer, count))
{
return -EFAULT;
}
return count;
}
static int test_open(struct inode *inode, struct file *file)
{
printk("This is open operation\n");
data = (char*)kmalloc(sizeof(char) * BUFF_SZ, GFP_KERNEL);
if (!data)
{
return -ENOMEM;
}
memset(data, 0, BUFF_SZ);
return 0;
}
static int test_release(struct inode *inode,struct file *file)
{
printk("This is release operation\n");
if (data)
{
kfree(data);
data = NULL;
}
return 0;
}
static void test_setup_cdev(struct cdev *dev, int minor,
struct file_operations *fops)
{
int err, devno = MKDEV(major, minor);
cdev_init(dev, fops);
dev->owner = THIS_MODULE;
dev->ops = fops;
err = cdev_add (dev, devno, 1);
if (err)
{
printk (KERN_NOTICE "Error %d adding test %d", err, minor);
}
}
static struct file_operations test_fops =
{
.owner = THIS_MODULE,
.read = test_read,
.write = test_write,
.open = test_open,
.release = test_release,
};
int init_module(void)
{
int result;
dev_t dev = MKDEV(major, 0);
if (major)
{
result = register_chrdev_region(dev, 1, TEST_DEVICE_NAME);
}
else
{
result = alloc_chrdev_region(&dev, 0, 1, TEST_DEVICE_NAME);
major = MAJOR(dev);
}
if (result < 0)
{
printk(KERN_WARNING "Test device: unable to get major %d\n", major);
return result;
}
test_setup_cdev(&test_dev, 0, &test_fops);
printk("The major of the test device is %d\n", major);
return 0;
}
void cleanup_module(void)
{
cdev_del(&test_dev);
unregister_chrdev_region(MKDEV(major, 0), 1);
printk("Test device uninstalled\n");
}test.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#define TEST_DEVICE_FILENAME "/dev/test_dev"
#define BUFF_SZ 1024
int main()
{
int fd, nwrite, nread;
char buff[BUFF_SZ];
fd = open(TEST_DEVICE_FILENAME, O_RDWR);
if (fd < 0)
{
perror("open");
exit(1);
}
do
{
printf("Input some words to kernel(enter 'quit' to exit):");
memset(buff, 0, BUFF_SZ);
if (fgets(buff, BUFF_SZ, stdin) == NULL)
{
perror("fgets");
break;
}
buff[strlen(buff) - 1] = '\0';
if (write(fd, buff, strlen(buff)) < 0)
{
perror("write");
break;
}
if (read(fd, buff, BUFF_SZ) < 0)
{
perror("read");
break;
}
else
{
printf("The read string is from kernel:%s\n", buff);
}
} while(strncmp(buff, "quit", 4));
close(fd);
exit(0);
}Makefile:
ifeq ($(KERNELRELEASE),)
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
modules_install:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
clean:
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions
.PHONY: modules modules_install clean
else
obj-m := test_drv.o
endif
test_drv_load脚本内容如下:
#!/bin/sh
module="test_drv"
device="test_dev"
mode="664"
group="david"
# remove stale nodes
rm -f /dev/${device}
# invoke insmod with all arguments we got
# and use a pathname, as newer modutils don't look in . by default
/sbin/insmod -f ./$module.ko $* || exit 1
major=`cat /proc/devices | awk "\\$2==\"$device\" {print \\$1}"`
mknod /dev/${device} c $major 0
# give appropriate group/permissions
chgrp $group /dev/${device}
chmod $mode /dev/${device}test_drv_unload脚本内容:
#!/bin/sh
module="test_drv"
device="test_dev"
# invoke rmmod with all arguments we got
/sbin/rmmod $module $* || exit 1
# remove nodes
rm -f /dev/${device}
exit 0具体实验内容:
- 先给脚本文件增加可执行权限:chmod +x ./test_drv_load
- 再以管理员身份运行加载脚本:sudo ./test_drv_load
- 加载成功
编译运行test.c
- 编译:gcc -o test test.c
- 运行:./test
根据提示输入学号信息
- 卸载模块
- 先给脚本文件增加可执行权限:chmod +x ./test_drv_unload
再以管理员身份运行加载脚本:sudo ./test_drv_unload
实验过程中遇到的问题
问题一:自己的虚拟机上不能编译运行代码。
解决:发现是自己的虚拟机内核与实验所需的不同,用实验一用到的老师的虚拟机,可以运行。
问题二:遇到missing separator stop的错误。
解决:把行首的空格改为了tab。
相关推荐
quguang 2020-02-12
waitui00 2019-12-01
wozijisunfly 2010-01-09
greenpepper 2019-06-27
mikean 2007-09-12
88550494 2014-05-15
猩猩少侠 2015-07-16
aotou 2012-01-17
dreamliner 2011-09-21
菜鸟实验室 2017-11-03
wangrui0 2011-02-08
lqp 2010-02-20
拒绝基本的小笔记 2018-01-29
急救室 2017-12-03
稀土 2017-12-03
任性的设计师 2017-12-03
住范儿 2017-12-02


