免费注册 查看新帖 |

Chinaunix

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

ioctl学习随笔 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-10-21 16:56 |只看该作者 |倒序浏览
1.用户空间的ioctl:
  int ioctl(int fd,int cmd,...);  /* ... 表示一个可选的参数,而不是一个可变参数 */
2.驱动程序空间的ioctl:
  int (*ioctl)(struct inode *inode,struct file *filp,unsigned int cmd,unsigned long arg);
3.注册ioctl:
struct file_operations f_ops={
  read: ..,
  write: ..,
  ...
  ioctl:scull_ioctl,
};
4.ioctl的命令号要唯一,用四个宏来生成命令号:
  type :一个magic数,比如'k'
  nr   :  序号,0,1,2,...
  size :数据项的长度

  _IO(type,nr);
  _IOR(type,nr,size);
  _IOW(type,nr,size);
  _IORW(type,nr,size);
5.scull_ioctl:
int scull_ioctl(struct inode *inode,struct file *filp,unsigned int cmd,unsigned long arg){
  switch(cmd){
    case SCULL_XXX:  /* #define SCULL_XXX _IO('k',0)  ,#define SCULL_XXX _IOR('k',0,0) and so on */
       ...
       break;
    case SCULL_YYY:
       ...
       break;
    ...
  }
  return 0;
}
scull.c
-----------------------
#include
#include
#include
#include
#include "scull.h"
MODULE_LICENSE("GPL");
int scull_open(struct inode *inode,struct file *filp);
int scull_release(struct inode *inode,struct file *filp);
int scull_read(struct file *filp,char *buf,size_t count,loff_t *f_pos);
int scull_write(struct file *filp,const char *buf,size_t count,loff_t *f_pos);
int scull_ioctl(struct inode *inode,struct file *filp,unsigned int cmd,unsigned long arg);
struct file_operations scull_fops={
    read:    scull_read,
    write:   scull_write,
    open:    scull_open,
    release: scull_release,
    ioctl:   scull_ioctl,
};
int init_module(void){
    int result;
   
    printk(" scull initilized.\n");
    result=register_chrdev(scull_major,"scull",&scull_fops);
    if(result scull: can't get major %d\n",scull_major);
        return result;
    }
    if(scull_major==0) scull_major=result;
    printk(" major=%d\n",scull_major);
    return 0;
}
void cleanup_module(void){
  int result;
  printk(" when unload,major=%d\n",scull_major);
    result=unregister_chrdev(scull_major,"scull");
    if(result==-EINVAL)
      printk(" cleanup failed.");
    else
      printk(" scull removed.\n");
}
int scull_open(struct inode *inode,struct file *filp){
    printk(" scull opened.\n");
    MOD_INC_USE_COUNT;
    printk(" jiffies in open is : %ld",jiffies);
    return 0;
}
int scull_release(struct inode *inode,struct file *filp){
    printk(" scull closed.\n");
    MOD_DEC_USE_COUNT;
    return 0;
}
int scull_read(struct file *filp,char *buf,size_t count,loff_t *f_pos){
    printk(" scull be called 'read'.\n");
    printk(" jiffies in read is :%ld",jiffies);
    return 0;
}
int scull_write(struct file *filp,const char *buf,size_t count,loff_t *f_pos){
    printk(" scull be called 'write'.\n");
    return 0;
}
int scull_ioctl(struct inode *inode,struct file *filp,unsigned int cmd,unsigned long arg){
  switch(cmd){
  case SCULL_IOCTL1:
    printk("you called scull_ioctl1 method.\n");
    break;
  case SCULL_IOCTL2:
    printk("you called scull_ioctl2 method.\n");
    break;
  }
  return 0;
}
test_scull.c
-----------------------
#include
#include
#include
#include
#include
#include "scull.h"
int main(){
  int fd;
  char buf[128];
  fd=open("/dev/scull",O_RDWR);
  read(fd,buf,1);
  write(fd,buf,1);
  ioctl(fd,SCULL_IOCTL1);
  ioctl(fd,SCULL_IOCTL2);
  close(fd);
  return 0;
}
  


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

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP