资源简介
设计和实现一个虚拟命名管道(FIFO)的字符设备。写一个模块化的字符设备驱动程序
通过一个简单的设备驱动的实现过程。学会Linux中设备驱动程序的编写

代码片段和文件信息
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
MODULE_AUTHOR(“Me“);
MODULE_LICENSE(“GPL“);
#define MYCDEV_MAJOR 231 /*给定的主设备号*/
#define MYCDEV_SIZE 1000
static int mycdev_open(struct inode *inode struct file *fp)
{
return 0;
}
static int mycdev_release(struct inode *inode struct file *fp)
{
return 0;
}
static ssize_t mycdev_read(struct file *fp char __user *buf size_t size loff_t *pos)
{
unsigned long p = *pos;
unsigned int count = size;
//char kernel_buf[MYCDEV_SIZE]=“This is mycdev!“;
char kernel_buf[MYCDEV_SIZE];
memset(kernel_buf0MYCDEV_SIZE);
memcpy(kernel_buf “This is mycdev!“ MYCDEV_SIZE);
if(p >= MYCDEV_SIZE)
return -1;
if(count > MYCDEV_SIZE)
count = MYCDEV_SIZE - p;
if (copy_to_user(buf kernel_buf count) != 0) {
printk(“read error!\n“);
return -1;
}
/*
for (i = 0; i < count; i++) {
__put_user(i buf);//write ‘i‘ from kernel space to user space‘s buf;
buf++;
}
*/
printk(“reader: %d bytes was read...\n“ count);
return count;
}
static ssize_t mycdev_write(struct file *fp const char __user *buf size_t size loff_t *pos)
{
return size;
}
/* 填充 mycdev的 file operation 结构*/
static const struct file_operations mycdev_fops =
{
.owner = THIS_MODULE
.read = mycdev_read
.write = mycdev_write
.open = mycdev_open
.release = mycdev_release
};
/*模块初始化函数*/
static int __init mycdev_init(void)
{
int ret;
printk(“mycdev module is staring..\n“);
ret=register_chrdev(MYCDEV_MAJOR“my_cdev“&mycdev_fops); /*注册驱动程序*/
if(ret<0){
printk(“register failed..\n“);
return 0;
}else{
printk(“register success..\n“);
}
return 0;
}
/*模块卸载函数*/
static void __exit mycdev_exit(void)
{
printk(“mycdev module is leaving..\n“);
unregister_chrdev(MYCDEV_MAJOR“my_cdev“); /*注销驱动程序*/
}
module_init(mycdev_init);
module_exit(mycdev_exit);
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2018-03-07 16:12 1400002100 杨永博 实验五 设备驱动\
目录 0 2018-03-07 16:12 1400002100 杨永博 实验五 设备驱动\设备驱动\
文件 102400 2018-03-07 16:12 1400002100 杨永博 实验五 设备驱动\设备驱动\实验五 设备驱动.doc
目录 0 2018-03-07 15:48 1400002100 杨永博 实验五 设备驱动\设备驱动\源代码\
文件 91 2017-04-27 14:45 1400002100 杨永博 实验五 设备驱动\设备驱动\源代码\.mycdev.ko.cmd
文件 222 2017-04-27 14:45 1400002100 杨永博 实验五 设备驱动\设备驱动\源代码\.mycdev.ko.unsigned.cmd
文件 23661 2017-04-27 14:45 1400002100 杨永博 实验五 设备驱动\设备驱动\源代码\.mycdev.mod.o.cmd
文件 28337 2017-04-27 14:45 1400002100 杨永博 实验五 设备驱动\设备驱动\源代码\.mycdev.o.cmd
目录 0 2018-03-07 15:48 1400002100 杨永博 实验五 设备驱动\设备驱动\源代码\.tmp_versions\
文件 47 2017-04-27 14:45 1400002100 杨永博 实验五 设备驱动\设备驱动\源代码\.tmp_versions\mycdev.mod
文件 222 2017-04-27 14:45 1400002100 杨永博 实验五 设备驱动\设备驱动\源代码\Makefile
文件 0 2017-04-27 14:45 1400002100 杨永博 实验五 设备驱动\设备驱动\源代码\Module.symvers
文件 31 2017-04-27 14:45 1400002100 杨永博 实验五 设备驱动\设备驱动\源代码\modules.order
文件 2359 2017-04-27 14:45 1400002100 杨永博 实验五 设备驱动\设备驱动\源代码\mycdev.c
文件 175369 2017-04-27 14:45 1400002100 杨永博 实验五 设备驱动\设备驱动\源代码\mycdev.ko
文件 175369 2017-04-27 14:45 1400002100 杨永博 实验五 设备驱动\设备驱动\源代码\mycdev.ko.unsigned
文件 937 2017-04-27 14:45 1400002100 杨永博 实验五 设备驱动\设备驱动\源代码\mycdev.mod.c
文件 73068 2017-04-27 14:45 1400002100 杨永博 实验五 设备驱动\设备驱动\源代码\mycdev.mod.o
文件 103488 2017-04-27 14:45 1400002100 杨永博 实验五 设备驱动\设备驱动\源代码\mycdev.o
文件 5195 2017-04-27 14:45 1400002100 杨永博 实验五 设备驱动\设备驱动\源代码\test
文件 493 2017-04-27 14:45 1400002100 杨永博 实验五 设备驱动\设备驱动\源代码\test.c
文件 155 2017-04-27 14:41 1400002100 杨永博 实验五 设备驱动\运行步骤.txt
- 上一篇:Linux内存分配与回收
- 下一篇:基于51单片机pwm控制的呼吸灯程序
相关资源
- 升腾Win终端系统升级方法新版.doc
- uboot到linux logo显示不间断 补丁
- Uninstall_Cortana_WINCLIENT.CN.rar
- UNIX/LINUX编程实践教程的源码
- Linux任务管理器
- linux应用层的华容道游戏源代码
- STM32基于rt_thread操作系统的SDHC卡文件
- ubuntu9.10 可加载内核模块和字符设备驱
- 操作系统 LRU算法 实验报告 及 程序代
- [免费]车载CE6.0操作系统
- 分页系统模拟实验 操作系统 课程设
- MP3文件ID3v2ID3v2APEv2标签读取
- 模拟段页式虚拟存储管理中地址转换
- 操作系统实验——虚存管理实验
- 广工操作系统实验
- linux下的发包工具sendip
- 尚观培训linux许巍关于c 的笔记和讲义
- 尚观培训linux董亮老师关于数据结构的
- linux 线程池源码 c 版
- linux C 电梯程序练习
- 广东工业大学操作系统实验四文件系
- linux下用多进程同步方法解决生产者
- Bochs入门教程[操作系统第一步]
- 操作系统课程设计完整版
- 磁盘调度算法的模拟实现及对比
- 模拟一个文件管理系统
- 二级文件系统(操作系统)
- uCOS编译环境建立 BC45 TASM
- Linux 操作系统实验(全)
- Linux From Scratch 中文手册
评论
共有 条评论