Chinaunix首页 | 论坛 | 博客
  • 博客访问: 355391
  • 博文数量: 79
  • 博客积分: 1270
  • 博客等级: 中尉
  • 技术积分: 1370
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-12 08:48
个人简介

freedom~~~~~~~~~~

文章分类

全部博文(79)

文章存档

2014年(10)

2013年(2)

2012年(13)

2011年(54)

分类: LINUX

2011-07-28 15:38:29

我自己定义了一个需求
1、建立1个字符设备,其中有100字节的存储区
2、能通过open,read,write实现,往其中写入数据和读出数据
  1. #ifndef SCULL_MAJOR
  2. #define SCULL_MAJOR 0
  3. #endif

  4. #include<linux/module.h>
  5. #include<linux/init.h>
  6. #include<linux/kernel.h>

  7. #include<linux/types.h>
  8. #include<linux/fs.h>
  9. #include<linux/cdev.h>
  10. #include<asm/uaccess.h>

  11. MODULE_AUTHOR("AW");
  12. MODULE_LICENSE("GPL");

  13. typedef struct scull_dev
  14. {
  15.     int num;
  16.     unsigned char data[100];
  17.     unsigned char *first;
  18.     struct cdev cdev;
  19. }scull_dev;

  20. //定义操作的函数
  21. int scull_open(struct inode *inode,struct file *flip);
  22. ssize_t scull_read(struct file *filp,const __user *buf,size_t len,loff_t *offp);
  23. ssize_t scull_write(struct file *filp,const char __user *buf,size_t len,loff_t *offp);
  24. int scull_ioctl(struct inode *inode,struct file *filp,unsigned long num );

  25. int scull_open(struct inode *inode ,struct file *filp)
  26. {
  27.     printk("<1>scull is opend \n");
  28.     struct scull_dev *dev=container_of(inode->i_cdev,scull_dev,cdev);
  29.     filp->private_data=dev->data;
  30.     memset(dev->data,1,100);
  31.     return 0;
  32. }

  33. ssize_t scull_read(struct file *filp,const __user *buf,size_t len,loff_t *offp)
  34. {
  35.     printk("<0>------------------------read!\n");
  36.     copy_to_user(buf,filp->private_data,20);
  37.     return 0;
  38. }

  39. ssize_t scull_write(struct file *filp,const char __user *buf,size_t len,loff_t *offp)
  40. {
  41.     printk("<0>!!!!!!!!!!!!!!!!!!!!!!!write!\n");
  42.     copy_from_user(filp->private_data,buf,20);
  43.     return 0;    
  44. }

  45. int scull_ioctl(struct inode *inode ,struct file *filp,unsigned long num)
  46. {
  47.     return 0;
  48. }

  49. static struct file_operations my_fops=
  50. {
  51.     .owner=THIS_MODULE,
  52.     .open=scull_open,
  53.     .read=scull_read,
  54.     .write=scull_write,
  55.     .ioctl=scull_ioctl,
  56.     //.close=scull_close,
  57. };

  58. //int register_chrdev(unsigned int major,const char *name,struct file_oprations *fops)//2.4中的方法
  59. //在此函数中,注册设备并初始化
  60. int scull_init(void)
  61. {
  62.     int result;
  63.     int dev_num=0;
  64.     int scull_major=SCULL_MAJOR;
  65.     int scull_minor=0;
  66.     scull_dev *myscull=(scull_dev *)kmalloc(sizeof(scull_dev),GFP_KERNEL);
  67.     memset(myscull,0,sizeof(scull_dev));

  68.     //设定设备号
  69.     if(scull_major)
  70.     {
  71.         dev_num=MKDEV(scull_major,scull_minor);
  72.         printk("<0>the devnum is %d",dev_num);
  73.         result=register_chrdev_region(dev_num,1,"scull");
  74.     }
  75.     else
  76.     {
  77.         result=alloc_chrdev_region(&dev_num,scull_minor,1,"scull");
  78.         printk("the result is %d",result);
  79.     }
  80.     if(result<0)
  81.     {
  82.         printk("<1>scull:can't get major %d\n",scull_major);
  83.         return result;
  84.     }
  85.     scull_major=MAJOR(dev_num);
  86.     printk("<0>the MAJOR is %d",scull_major);

  87.     //初始化cdev设备scull,确定相应的操作和结构
  88.     cdev_init(&myscull->cdev,&my_fops);
  89.     myscull->cdev.owner=THIS_MODULE;
  90.     //向系统注册,绑定刚刚的设备号
  91.     result=cdev_add(&myscull->cdev,dev_num,1);
  92.     if(result <0)
  93.     {
  94.         printk("<0>it doesn't registered!\n");
  95.     }
  96.     printk("<1>the module scull is insert !\n");
  97.     return 0;
  98. }

  99. void scull_exit(void)
  100. {
  101.     printk("<1>leave this module!\n");
  102. }

  103. module_init(scull_init);
  104. module_exit(scull_exit);
下面是测试函数
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<fcntl.h>
  4. #include<string.h>

  5. int main()
  6. {
  7.     int fd=open("/dev/scull",O_RDWR);
  8.     if(fd==-1)
  9.     {
  10.         printf("the fd can't opened\n");
  11.         return -1;
  12.     }
  13.     printf("the fd is %d\n",fd);
  14.     unsigned char data[100];
  15.     memset(data,0,100);
  16.     read(fd,data,20);
  17.     int i;
  18.     for(i=0;i<20;i++)
  19.     {
  20.         printf("%d",data[i]);
  21.     }
  22.     
  23.     printf("\n");
  24.     unsigned char data1[21]={1,2,3,4,5,6,7,8,8,1,2,3,4,5,5,6,6,4};
  25.     write(fd,data1,20);

  26.     read(fd,data,20);
  27.     for(i=0;i<20;i++)
  28.     {
  29.         printf("%d",data[i]);
  30.     }
  31.     printf("\n");
  32. }

其中的模块的Makefile文件
  1. obj-m:=scull.o
  2. KERNELDIR:=/lib/modules/2.6.32-30-generic/build
  3. PWD:=$(shell pwd)
  4. modules:
  5. $(MAKE) -C $(KERNELDIR) M=$(PWD)
  6. clean:
能够正确得到结果



阅读(664) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~