- 2
- 0
- 约 9页
- 2016-05-24 发布于重庆
- 举报
实验71模块驱动架构实验
实验 7-1 模块驱动架构实验
【实验目的】
熟悉模块驱动架构.
掌握模块驱动的makefile文件的编写。
掌握模块驱动的加载和卸载。
【实验步骤】
利用vi编辑器,编写一个xsbase.c驱动架构代码;
#include linux/module.h
#include linux/fs.h
#include linux/init.h
#include asm/uaccess.h
MODULE_LICENSE(GPL);
static char *data;
#define xsbase_major 65
#define DEVICE_NAME xsbase
static int xsbase_open(struct inode *inode, struct file *file)
{
printk(KERN_EMERG This xsbase dev is opened!\n);
return 0;
}
static int xsbase_release(struct inode *inode, struct file *file)
{
printk(KERN_EMERG The xsbase dev is released!\n);
return 0;
}
static ssize_t xsbase_write( struct file *file, const char *buffer, size_t count, loff_t *f_pos)
{
if(count 0)
return -EINVAL;
kfree(data);
data = (char *)kmalloc(sizeof(char)*(count+1), GFP_KERNEL);
if(!data)
return -ENOMEM;
copy_from_user(data, buffer, count+1);
return count;
}
static ssize_t xsbase_read(struct file *file, char *buf, size_t count, loff_t *f_pos)
{
int len;
if(count 0)
return -EINVAL;
len = strlen(data);
if(len count)
count = len;
copy_to_user(buf, data, count+1);
return count;
}
static struct file_operations chr_fops = {
read: xsbase_read,
write: xsbase_write,
open: xsbase_open,
release: xsbase_release,
};
int __init xsbase_init(void)
{
int res;
res = register_chrdev(65, DEVICE_NAME, chr_fops);
if(res 0)
{
printk(================cant get major name!\n);
return res;
}
else
printk(KERN_EMERG The xsbase dev is init!\n);
return 0;
}
static void __exit xsbase_exit(void)
{
printk(KERN_EMERG The xsbase dev is exited ! \n);
unregister_chrdev(65,DEVICE_NAME);
}
module_init(xsbase_init);
module_exit(xsbase_exit);
MODULE_AUTHOR(Ben.li, ben.li@);
MODULE_DESCRIPTION(This is a driver struct demo);
利用vi编辑器,编写一个用于编译xsbase.c驱动架构Makefile文件;其中KERNELDIR ? = /root/work/linux-2.6.9为目标平台Linux内核的所在的路径
# Makefile for the XSBASE. #
CFLAGS +=$(DEBFLAGS) -Wall
ifneq ($(KERNELRELEASE),)
obj-m :=xsbase.o
else
KERNELDIR ?=
您可能关注的文档
最近下载
- 2025年事业单位工勤技能-河南-河南防疫员三级(高级工)历年参考题典型考点含答案解析.docx VIP
- 设计心理学-专业学习教材.pdf VIP
- 七年级数学上册第3章代数式单元测试题.docx VIP
- 杭州职业技术大学2025-2026学年《概率论与数理统计2》第一学期期末试题(B).docx VIP
- 杭州电子科技大学信息工程学院2025-2026学年《概率论与数理统计2》第一学期期末试题(B).docx VIP
- 急性上呼吸道梗阻.ppt VIP
- 《汽车发动机构造与维修》教案-.docx VIP
- 美国大学英语写作(第九版)课后习题答案.docx VIP
- 静脉血液标本采集指南(WST661-2020)解读PPT课件.pptx VIP
- 《仪器分析技术》课程标准.pdf VIP
原创力文档

文档评论(0)