文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>又一个简单示例

又一个简单示例

时间:2005-10-27  来源:xqkz

最近一直在学Linux的驱动开发,记录一些以备查阅吧。
下面这个例子来源于网上的一份教程:
下面这个例程在2.4.31上运行通过。
/*
* 这个文件是一个内核模块。
* 内核模块的编译,加载和卸载在前面已经介绍了。
* 这个模块的功能是,创建一个字符设备。
* 这个设备是一块4096字节的共享内存。
* 内核分配的主设备号会在加载模块时显示。
* 开始例行公事
*/
#ifndef __KERNEL__
#define __KERNEL__
#endif
#ifndef MODULE
# define MODULE
#endif

#include <linux/config.h>
#include <linux/module.h>
#ifdef CONFIG_SMP
#define __SMP__
#endif
MODULE_LICENSE("GPL");
/* 结束例行公事 */

#include <asm/uaccess.h>/* copy_to_user(), copy_from_user */
#include <linux/fs.h>/* struct file_operations, register_chrdev(), ... */
#include <linux/kernel.h>/* printk()在这个文件里 */
#include <linux/sched.h>/* 和任务调度有关 */
#include <linux/types.h>/* u8, u16, u32 ... */
/* 关于内核功能库,可以去网上搜索详细资料 */
/* 文件被操作时的回调功能 */
static int asdf_open (struct inode *inode, struct file *filp);
static int asdf_release (struct inode *inode, struct file *filp);
static ssize_t asdf_read (struct file *filp, char *buf, size_t count,loff_t *f_pos);
static ssize_t asdf_write (struct file *filp, const char *buf, size_t count,loff_t *f_pos);
static loff_t asdf_lseek (struct file * file, loff_t offset, int orig);
/* 申请主设备号时用的结构, 在linux/fs.h里定义 */
struct file_operations asdf_fops = {
open: asdf_open,
release: asdf_release,
read: asdf_read,
write: asdf_write,
llseek: asdf_lseek,
};
static int asdf_major; /* 用来保存申请到的主设备号 */
static u8 asdf_body[4096]="asdf_body This is a test! "; /* 设备 */
int init_module(void){
printk ("Hi, This' A Simple Device File! ");
asdf_major = register_chrdev (0, "A Simple Device File", &asdf_fops); /* 申请字符设备的主设备号 */
if (asdf_major < 0) return asdf_major; /* 申请失败就直接返回错误编号 */
printk ("The major is:%d ", asdf_major); /* 显示申请到的主设备号 */
return 0; /* 模块正常初始化 */
}
void cleanup_module(void){
unregister_chrdev(asdf_major, "A Simple Device File"); /* 注销以后,设备就不存在了 */
printk("A Simple Device has been removed,Bye! ");
}
//实现在asdf_fops结构体中使用的回调函数
static int asdf_open(struct inode *inode,struct file *filp){/*open回调*/
printk("^_^ : open %s ",current->comm);
return 0;
}
static int asdf_release(struct inode *inode,struct file *filp){ /* close回调 */
printk("^_^ : close ");
return 0;
}
static ssize_t asdf_read (struct file *filp,char *buf,size_t count,loff_t *f_pos){/* read回调 */
loff_t pos;
pos = *f_pos; /* 文件的读写位置 */
if ((pos==4096) || (count>4096)) return 0; /* 判断是否已经到设备尾,或写的长度超过设备大小 */
pos += count;
if (pos > 4096) {
count -= (pos - 4096);
pos = 4096;
}
if (copy_to_user(buf, asdf_body+*f_pos, count)) return -EFAULT; /* 把数据写到应用程序空间 */
*f_pos = pos; /* 改变文件的读写位置 */
return count; /* 返回读到的字节数 */
}
/* write回调,和read一一对应 */
static ssize_t asdf_write(struct file *filp,const char *buf,size_t count,loff_t *f_pos){
loff_t pos;
pos = *f_pos;
if ((pos==4096) || (count>4096)) return 0;
pos += count;
if (pos > 4096) {
count -= (pos - 4096);
pos = 4096;
}
if (copy_from_user(asdf_body+*f_pos, buf, count)) return -EFAULT;
*f_pos = pos;
return count;
}
/* lseek回调 */
static loff_t asdf_lseek(struct file * file,loff_t offset,int orig){
loff_t pos;
pos = file->f_pos;
switch (orig) {
case 0:
pos = offset;
break;
case 1:
pos += offset;
break;
case 2:
pos = 4096+offset;
break;
default:
return -EINVAL;
}
if ((pos>4096) || (pos<0)) {
printk("^_^ : lseek error %d ",(int)pos);
return -EINVAL;
}
return file->f_pos = pos;
}
使用Makefile文件如下:
CC=gcc
CFLAGS = -D__KERNEL__ -I/usr/src/linux-$(shell uname -r)/include -Wall
-Wstrict-prototypes -O2 -fomit-frame-pointer -pipe
-fno-strength-reduce -mcpu=i686 -DMODULE -DMODVERSIONS
-include /usr/src/linux-$(shell uname -r)/include/linux/modversions.h
-fno-strict-aliasing
test.o: test.c
gcc -c $(CFLAGS) $<
clean:
rm -f .*.cmd *.mod.c *.o *.ko -r .tmp*
加载模块:insmod test.o
卸载模块:rmmod test
加载模后可可通过/var/log/syslog查看所申请有主设备号。假设为254
他建设备文件:
mknod abc c 254 0
然后cat abc。
echo this is a test >abc
cat abc
查看输出结果。

相关阅读 更多 +
排行榜 更多 +
枪战特训2

枪战特训2

飞行射击 下载
打击者19452代最新版

打击者19452代最新版

飞行射击 下载
方块枪战战场安卓版

方块枪战战场安卓版

飞行射击 下载