LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 652|回复: 0

字符设备驱动,往里面写怎么说没有剩余空间呢?

[复制链接]
发表于 2005-4-11 20:40:09 | 显示全部楼层 |阅读模式
cat >/dev/dpchr
ls
cat: write error: No space left on device

  1. #include <linux/kernel.h>
  2. #include <linux/fs.h>
  3. #include <linux/module.h>
  4. #include <asm/uaccess.h>
  5. #include <linux/cdev.h>


  6. #define DP_MAJOR 50
  7. #define DP_MINOR 0
  8. #define MYMEMSIZE 2000
  9. static int char_read(struct file *filp,char __user *buffer,size_t,loff_t *);
  10. static int char_open(struct inode *,struct file *);
  11. static int char_write(struct file *filp,const char __user *buffer,size_t ,loff_t*);
  12. static int char_release(struct inode *,struct file *);
  13. static char *dp_chrp;
  14. static char *arr;
  15. static int chropen;
  16. struct cdev *my_cdev;
  17. static int len;
  18. struct file_operations Fops = {
  19.         .read = char_read,
  20.         .write = char_write,
  21.         .open = char_open,
  22.         .release = char_release,        /* a.k.a. close */
  23. };
  24. static int __init char_init(void)
  25. {
  26.         printk(KERN_ALERT"Initing......\n");
  27.         dev_t dev;
  28.         dev=MKDEV(DP_MAJOR,DP_MINOR);
  29.         my_cdev = cdev_alloc( );
  30.         arr=kmalloc(1024,GFP_KERNEL);
  31.         if(arr==NULL){
  32.                 printk(KERN_ALERT"kmalloc error\n");
  33.         }
  34.         if(my_cdev==NULL){
  35.                 return -1;
  36.         }
  37.         if(register_chrdev_region(dev,10,"dpchr")<0){
  38.                 printk(KERN_ALERT"Register char dev error\n");
  39.                 return -1;
  40.         }
  41.         //dp_chrp=kmalloc(MYMEMSIZE,GFP_KERNEL);
  42.         dp_chrp=arr;
  43.         if(!dp_chrp){
  44.                 printk(KERN_ALERT"Kmalloc error\n");
  45.                 return -1;
  46.         }
  47.         chropen=0;
  48.         len=0;
  49.         my_cdev->ops = &Fops;
  50.         cdev_init(my_cdev,&Fops);
  51.         cdev_add(my_cdev,dev,1);

  52.         return 0;
  53. }

  54. static int char_open(struct inode *inode,struct file *file)
  55. {
  56.         if(chropen==0)
  57.                 chropen++;
  58.         else{
  59.                 printk(KERN_ALERT"Another process open the char device\n");
  60.                 return -1;
  61.         }
  62.         printk(KERN_ALERT"Device open!\n");
  63.         try_module_get(THIS_MODULE);
  64.         return 0;
  65. }

  66. static int char_release(struct inode *inode,struct file *file)
  67. {
  68.         chropen--;
  69.         module_put(THIS_MODULE);
  70.         printk(KERN_ALERT"Device closeing......\n");
  71.         return 0;
  72. }

  73. static int char_read(struct file *filp,char __user *buffer,size_t length,loff_t *offset)
  74. {
  75.         int i=len;
  76.         int j=0;
  77. //        while(i>0&&j<100){
  78.                 //put_user(arr[j++],buffer++);
  79.                 copy_to_user(arr,buffer,len);
  80. //        }
  81.         return i;
  82. }

  83. static int char_write(struct file *filp,const char __user  *buffer,size_t length,loff_t *offset)
  84. {

  85.         int i;
  86. //        for(i=0;i<length&&i<100;i++){
  87. //                get_user(arr[i],buffer+i);
  88. //        }
  89.         len =copy_from_user(arr,buffer,10);
  90.         //len=i;
  91.         return len;
  92. }
  93.        

  94. static void module_close()
  95. {
  96.         len=0;
  97.         printk(KERN_ALERT"Unloading..........\n");
  98.         unregister_chrdev_region(MKDEV(DP_MAJOR,DP_MINOR),10);
  99.         cdev_del(my_cdev);
  100. }

  101. module_init(char_init);
  102. module_exit(module_close);
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表