資源簡介
ubuntu9.10下(內(nèi)核為linux-2.6.31),動態(tài)可加載模塊(LKM)和字符設(shè)備驅(qū)動實驗的源碼及實驗報告
代碼片段和文件信息
#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實現(xiàn)一對一聊天
評論
共有 條評論