资源简介
ubuntu9.10下(内核为linux-2.6.31),动态可加载模块(LKM)和字符设备驱动实验的源码及实验报告
代码片段和文件信息
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include “char_driver.h“
int char_device_major = CHAR_DEVICE_MAJOR;
int char_device_minor = CHAR_DEVICE_MINOR;
/*pointer to the char_dev_t struct*/
struct char_dev_t *dev_mem;
static unsigned char device_inc=0;
enum {
MODE_PUBLICMODE_PROTECTEDMODE_PRIVATE
};
int mode = MODE_PROTECTED; //the mode of the device;
int valid = 0; //is the device valid to use
unsigned long password=1234567;
/* timer */
struct timer_list timer;
long delay = 30;
void on_time_out(void);
void start_timer(void );
void stop_timer(void);
module_param(password long S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
MODULE_PARM_DESC(password “password of the device“);
module_param(mode int S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
MODULE_PARM_DESC(mode “mode of the device“);
module_param(delay long S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
MODULE_PARM_DESC(delay “time of automatically logout“);
static int my_open(struct inode *inode struct file *filp)
{
struct char_dev_t *dev;
if(device_inc>0)
return -ERESTARTSYS;
device_inc++;
dev = container_of(inode->i_cdev struct char_dev_t cdev);
filp->private_data = dev;
return 0;
}
int my_release(struct inode *inode struct file *filp)
{
device_inc--;
return 0;
}
ssize_t my_read(struct file *filp char __user *buf size_t countloff_t *f_pos)
{
int result;
/*the position to read*/
loff_t pos = *f_pos;
struct char_dev_t *dev_data = filp->private_data;
if(mode !=MODE_PUBLIC && mode!=MODE_PROTECTED && !valid){ //judge the mode of the device
printk(KERN_INFO “Read operation to the device not permitted.\n“);
return -1;
}
/*analyze the positin*/
if (pos >= GLOBALMEM_SIZE)
{
result = 0;
goto out;
}
if (count > (GLOBALMEM_SIZE-pos))
{
count = GLOBALMEM_SIZE - pos;
}
/*copy data to user space*/
if (down_interruptible(&dev_mem->sem))
{
return -ERESTARTSYS;
}
if (copy_to_user(buf dev_data -> buffer + pos count))
{
count = -EFAULT;
goto out;
}
pos += count;
/* update the write/read position */
*f_pos = pos;
up(&dev_mem->sem);
printk(KERN_INFO “read %d byte(s) from kernel\n“ count);
return count;
out:
up(&dev_mem->sem);
return count;
}
ssize_t my_write(struct file *filp const char __user *buf size_t sizeloff_t *f_pos)
{
unsigned int count = size;
/*the position to write*/
loff_t pos = *f_pos;
struct char_dev_t *dev_data = filp->private_data;
if( mode != MODE_PUBLIC && !valid){
printk(KERN_INFO “Write operation to the device not permitted.\n“);
return -1;
}
if (pos >= GLOBALMEM_SIZE)
{
return -ENOMEM;
}
if (count > (GLOBALMEM_SIZE - pos))
{
count = GLOBALMEM_SIZE - pos;
}
/*copy data from user space to kernel*/
if (down
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 7080 2010-07-03 00:29 lab3\code\char_device_driver\char_driver.c
文件 1617 2010-07-02 17:01 lab3\code\char_device_driver\char_driver.h
文件 185 2010-07-02 03:42 lab3\code\char_device_driver\Makefile
文件 1177 2010-07-03 00:30 lab3\code\char_device_driver\test\test1.c
文件 966 2010-07-03 00:41 lab3\code\char_device_driver\test\test2.c
文件 1154 2010-07-03 00:35 lab3\code\char_device_driver\test\test3.c
文件 906 2010-07-03 00:46 lab3\code\char_device_driver\test\test4.c
文件 285 2010-07-02 18:38 lab3\code\module\Makefile
文件 4180 2010-07-02 21:41 lab3\code\module\my_module.c
文件 519370 2010-07-03 01:17 lab3\lab3-Experiment Report.pdf
目录 0 2010-07-03 00:51 lab3\code\char_device_driver\test
目录 0 2010-07-03 00:50 lab3\code\char_device_driver
目录 0 2010-07-02 22:57 lab3\code\module
目录 0 2010-07-02 23:25 lab3\code
目录 0 2010-07-03 01:19 lab3
----------- --------- ---------- ----- ----
536920 15
- 上一篇:51单片机控制舵机程序
- 下一篇:websocket实现一对一聊天
相关资源
- MP3文件ID3v2ID3v2APEv2标签读取
- 操作系统实验——虚存管理实验
- linux下的发包工具sendip
- 尚观培训linux许巍关于c 的笔记和讲义
- 尚观培训linux董亮老师关于数据结构的
- linux 线程池源码 c 版
- linux C 电梯程序练习
- linux下用多进程同步方法解决生产者
- Linux 操作系统实验(全)
- Linux From Scratch 中文手册
- linux 网络实验 ftp程序
- Linux命令大全离线版&在线版
- 操作系统共享内存实验
- dos 下运行Linux 命令--gnu_utils
- linux 0.12内核源代码
- linux简易shell C实现
- linux实验报告及心得体会
- 基于GTK的Linux环境下的简易任务管理器
- linux扫雷游戏代码
- CAN Linux驱动代码
- Linux系统教材
- intel 82579LM 网卡驱动Linux系统版 v1.9.
- SA1110处理器掌上电脑液晶显示器设计
- 基于Linux的串口服务器设计
- Windows下访问LINUX的利器-SSH
- Linux操作系统课件PPT
- 《操作系统原理及应用Linux》PPT
- Linux版的telnet安装包telnet、telnet-serv
- 快速掌握LINUX 学习笔记
- 基于Linux、QT的视频监控系统的设计与
评论
共有 条评论