免费注册 查看新帖 |

Chinaunix

广告
  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 957 | 回复: 0
打印 上一主题 下一主题

通过实例了解linux设备驱动程序的编写(转) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-02-20 15:50 |只看该作者 |倒序浏览

??Linux设备驱动程序设计实例
??
?? Linux系统中,设备驱动程序是操作系统内核的重要组成部分,在与硬件设备之间建立了标准的抽象接口。通过这个接口,用户可以像处理普通文件一样,对硬件设备进行打开(open)、关闭(close)、读写(read/write)等操作。通过分析和设计设备驱动程序,可以深入理解Linux系统和进行系统开发。本文通过一个简单的例子来说明设备驱动程序的设计。
??1、程序清单
??#ifndef __KERNEL__
??# define __KERNEL__   //按内核模块编译
??#endif
??#ifndef MODULE
??# define MODULE        //设备驱动程序模块编译
??#endif
??#define DEVICE_NAME "MyDev"  
??#define OPENSPK 1
??#define CLOSESPK 2
??
??//必要的头文件
??#include  //同kernel.h,最基本的内核模块头文件
??#include  //同module.h,最基本的内核模块头文件
??#include   //这里包含了进行正确性检查的宏
??#include    //文件系统所必需的头文件
??#include  //这里包含了内核空间与用户空间进行数据交换时的
?? 函数宏
??#include     //I/O访问
??int my_major=0;         //主设备号
??static int Device_Open=0;
??static char Message[]="This is from device driver";
??char *Message_Ptr;
??
??int my_open(struct inode *inode, struct file *file)
??{//每当应用程序用open打开设备时,此函数被调用
?? printk ("ndevice_open(%p,%p)n", inode, file);
?? if (Device_Open)
??  return -EBUSY;   //同时只能由一个应用程序打开
?? Device_Open++;
?? MOD_INC_USE_COUNT;  //设备打开期间禁止卸载
?? return 0;
??}
??
??static void my_release(struct inode *inode, struct file *file)
??{//每当应用程序用close关闭设备时,此函数被调用
?? printk ("ndevice_release(%p,%p)n", inode, file);
?? Device_Open --;
?? MOD_DEC_USE_COUNT;  //引用计数减1
??}
??
??ssize_t my_read (struct file *f,char *buf,int size,loff_t off)
??{//每当应用程序用read访问设备时,此函数被调用
??  int bytes_read=0;   
??#ifdef DEBUG
??  printk("nmy_read is called. User buffer is %p,size is dn",buf,size);
??#endif
??if (verify_area(VERIFY_WRITE,buf,size)==-EFAULT)
??  return -EFAULT;
??    Message_Ptr=Message;
??    while(size && *Message_Ptr)
??    {   
??        if(put_user(*(Message_Ptr++),buf++)) //写数据到用户空间
??            return -EINVAL;
??        size --;
??        bytes_read++;
??    }
??    return bytes_read;
??}
??
??ssize_t my_write (struct file *f,const char *buf, int size,loff_t off)
??{//每当应用程序用write访问设备时,此函数被调用
??  int i;
??    unsigned char uc;
??#ifdef DEBUG
?? printk("nmy_write is called. User buffer is %p,size is dn",buf,size);
??#endif
??if (verify_area(VERIFY_WRITE,buf,size)==-EFAULT)
??return -EFAULT;
??    printk("nData below is from user program:n");
??    for (i=0;i
??#include
??#include
??#define DEVICE_NAME MyDev
??#define OPENSPK 1
??#define CLOSESPK 2
??char buf[128];
??int main(){
?? int f=open(DEVICE_NAME,O_RDRW);
?? if (f==-1) return 1;
?? printf("nHit enter key to read device...");
?? read(f,buf,128); printf(buf);
?? printf("nHit enter key to write device ...");
?? write(f,"test",4);
?? printf("nHit enter key to open PC's speaker...");
?? ioctl(f,OPENSPK);
?? printf("nHit enter key to close PC's speaker...");
?? ioctl(f,CLOSESPK);
?? close(f);
??        }
??

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/92401/showart_1836316.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP