·ChinaUnix首页 ·论坛 ·博客 
Linux首页 | Linux新闻 | Linux文档 | Linux论坛 | Linux下载 | Linux博客 | Linux搜索
新手入门 | 安装启动 | 管理员指南 | 开发手册 | 桌面应用 | 程序开发 | 数据库 | 网络技术| CentOS | Fedora | MySQL | Apache | Ubuntu | Gentoo| OSCON08
  Linux时代 >> 技术文档 >> 新手入门
 
入门文章:教你学会编写Linux设备驱动
来源: ChinaUnix博客  日期: 2008.04.11 14:58 (共有条评论) 我要评论
 
阅读此文的目的: 学会编写
Linux
设备驱动。
  阅读此文的方法: 阅读以下2个文件: hello.c,asdf.c。
  此文假设读者:
  已经能用C语言编写
Linux
应用程序,
  理解"字符设备文件, 块设备文件, 主设备号, 次设备号",
  会写简单的Shell脚本和Makefile。
  1. "hello.c"
  --------------------------------
  /*
  * 这是我们的第一个源文件,
  * 它是一个可以加载的内核模块,
  * 加载时显示"Hello,World!",
  * 卸载时显示"Bye!"。
  * 需要说明一点,写内核或内核模块不能用写应用程序时的系统调用或函数库,
  * 因为我们写的就是为应用程序提供系统调用的代码。
  * 内核有专用的函数库,如, , 等,
  * 现在还没必要了解得很详细,
  * 这里用到的printk的功能类似于printf。
  * "/usr/src/
Linux
"是你实际的内核源码目录的一个符号链接,
  * 如果没有现在就创建一个,因为下面和以后都会用到。
  * 编译它用"gcc -c -I/usr/src/
Linux
/include hello.c",
  * 如果正常会生成文件hello.o,
  * 加载它用"insmod hello.o",
  * 只有在文本终端下才能看到输出。
  * 卸载它用"rmmod hello"
  */
  /*
  * 小技巧: 在用户目录的.bashrc里加上一行:
  * alias mkmod='gcc -c -I/usr/src/
Linux
/include'
  * 然后重新登陆Shell,
  * 以后就可以用"mkmod hello.c"的方式来编译内核模块了。
  */
  /* 开始例行公事 */
  #ifndef __KERNEL__
  # define __KERNEL__
  #endif
  #ifndef MODULE
  # define MODULE
  #endif
  #include
  #include
  MODULE_LICENSE("GPL");
  #ifdef CONFIG_SMP
  #define __SMP__
  #endif
  /* 结束例行公事 */
  #include /* printk()在这个文件里 */
  static int
  init_module
  (){
  printk("Hello,World!\n");
  return 0; /* 如果初始工作失败,就返回非0 */
  }
  static void
  cleanup_module
  (){
  printk("Bye!\n");
  }
  ------------------------------------
  2. "asdf.c"
  ------------------------------------
 /*
  * 这个文件是一个内核模块。
  * 内核模块的编译,加载和卸载在前面已经介绍了。
  * 这个模块的功能是,创建一个字符设备。
  * 这个设备是一块4096字节的共享内存。
  * 内核分配的主设备号会在加载模块时显示。
  */
  /* 开始例行公事 */
  #ifndef __KERNEL__
  # define __KERNEL__
  #endif
  #ifndef MODULE
  # define MODULE
  #endif
  #include
  #include
  #ifdef CONFIG_SMP
  #define __SMP__
  #endif
  MODULE_LICENSE("GPL");
  /* 结束例行公事 */
  #include /* copy_to_user(), copy_from_user */
  #include /* struct file_operations, register_chrdev(), ... */
  #include /* printk()在这个文件里 */
  #include /* 和任务调度有关 */
  #include /* 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 环境的系统调用很陌生,建议先看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) || (posf_pos = pos;
  }


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/17657/showart_527114.html

[ 本帖最后由 Send_linux 于 2008-4-16 13:47 编辑 ]
  发表评论 查看评论(共有条评论) 我要提问
 
 


最新资讯更多>> 
· Wine 1.1.6 发布
· Wikipedia“变心”,力挺Ubuntu
· Shopex收购ecshop,discuz为了..
· 提前下载:OpenOffice.org 3.0..
· 微软Office面临严重挑战?
· 媒体观察:法国收获开源带来的启示
· 微软频繁接触开源 跨越“源”界限
· 挣脱Windows束缚 拥抱Ubuntu L..
· Puppy Linux领导者转向开发UniPup
· 发行版发布:Mandriva Linux 2009
论坛热点更多>> 
· 那些常常见到又叫不出名字的花
· xp 和LINUX 上网下载东西
· 关于秋日里的一场梦。。。。。。
· LILEI和HANMEIMEI的欲望人生..
· 这几道题会做就可直接找LINU..
· Oracle再祭收购大旗,这次红..
· RHCE考试费4200!NND
· 配置DNS主从自动更新同步,无..
· 夜店最火辣的Party girls
· 倒!同事一个比一个住的远
文档更新更多>> 
· kmess1.5.1在红旗6sp1上的安装方法
· OOo3.0 RC2 发布,中文版可用。
· IT从业人员必看的10大论坛
· SSH权威指南
· DNS相关定义介绍
· OpenVPN(zt)
· Linux shell I/O重定向详解
· linux下的烧写环境配置
· tcpdump详细用法
· 转:ARM的嵌入式Linux移植体验之基..
 
关于我们 | 联系方式 | 广告合作 | 诚聘英才 | 网站地图 | 友情链接 | 免费注册

Copyright © 2001-2008 ChinaUnix.net All Rights Reserved

感谢所有关心和支持过ChinaUnix的朋友们

京ICP证041476号