正文

一个linux设备驱动例程2006-11-16 13:41:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/embed/20366.html

分享到:

/*
* 这个文件是一个内核模块。
* 内核模块的编译,加载和卸载在前面已经介绍了。

* 这个模块的功能是,创建一个字符设备。
* 这个设备是一块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\n"; /* 设备 */

static int
init_module
(){
printk ("Hi, This' A Simple Device File!\n");
asdf_major = register_chrdev (0, "A Simple Device File", &asdf_fops); /* 申请字符设备的主设备号 */
if (asdf_major < 0) return asdf_major; /* 申请失败就直接返回错误编号 */
printk ("The major is:%d\n", asdf_major); /* 显示申请到的主设备号 */
return 0; /* 模块正常初始化 */
}

static void
cleanup_module
(){
unregister_chrdev(asdf_major, "A Simple Device File"); /* 注销以后,设备就不存在了 */
printk("A Simple Device has been removed,Bye!\n");
}

/*
* 编译这个模块然后加载它,
* 如果正常,会显示你的设备的主设备号。
* 现在你的设备就建立好了,我们可以测试一下。
* 假设你的模块申请到的主设备号是254,
* 运行"mknod abc c 254 0",就建立了我们的设备文件abc。
* 可以把它当成一个4096字节的内存块来测试一下,
* 比如"cat abc", "cp abc image", "cp image abc",
* 或写几个应用程序用它来进行通讯。

* 介绍一下两个需要注意的事,
* 一是printk()的显示只有在非图形模式的终端下才能看到,
* 二是加载过的模块最好在不用以后卸载掉。

* 如果对Linux环境的系统调用很陌生,建议先看APUE这本书。
*/

static int
asdf_open /* open回调 */
(
struct inode *inode,
struct file *filp
){
printk("^_^ : open %s\n ",current->comm);
/*
* 应用程序的运行环境由内核提供,内核的运行环境由硬件提供。
* 这里的current是一个指向当前进程的指针,
* 现在没必要了解current的细节。
* 在这里,当前进程正打开这个设备,
* 返回0表示打开成功,内核会给它一个文件描述符。
* 这里的comm是当前进程在Shell下的command字符串。
*/
return 0;
}

static int
asdf_release /* close回调 */
(
struct inode *inode,
struct file *filp
){
printk("^_^ : close\n ");
return 0;
}

static ssize_t
asdf_read /* read回调 */
(
struct file *filp,
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_to_user(buf, asdf_body+*f_pos, count)) return -EFAULT; /* 把数据写到应用程序空间 */
*f_pos = pos; /* 改变文件的读写位置 */
return count; /* 返回读到的字节数 */
}

static ssize_t
asdf_write /* write回调,和read一一对应 */
(
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;
}

static loff_t
asdf_lseek /* 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\n",pos);
return -EINVAL;
}
return file->f_pos = pos;
}

 

一些测试过程

运行gcc -I/usr/src/linux-2.4.20-8/include -c asdf.c
insmod asdf.o 加载
more /proc/devices可以看到新生成的设备号为254
mknod abc c 254 0 建立设备文件
cat abc
dmesg | tail -10察看
ls -l>abc
more abc可以看到内容
如果不行,重新启动,重新来一次

 

mknod /dev/test c 254 0 建立设备文件
cat /dev/test
dmesg | tail -10察看
ls -l>/dev/test
more /dev/test可以看到内容


对于测试文件test.c
gcc -o test -g test.c
./test
可以看到对于/dev/test的操作结果

卸载设备rmmod asdf

测试程序test.c

//test.c
#include<stdio.h>
#include<fcntl.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
int main() {
 int fd,i;
        char buffer[10];
 fd=open("/dev/test", O_RDWR);
 if(fd < 0)
 {      
                printf("can not open file\n");  
  exit(fd);
 }
 //your code here
 read(fd, buffer, 10);
        for(i=0;i<10;i++)
            printf("hehe%d\n",buffer[i]);
 close(fd);
 
 return 0;
}

阅读(3953) | 评论(6)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

loading...
您需要登录后才能评论,请 登录 或者 注册