Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1721721
  • 博文数量: 199
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 6186
  • 用 户 组: 普通用户
  • 注册时间: 2012-10-30 11:01
个人简介

Linuxer.

文章存档

2015年(4)

2014年(28)

2013年(167)

分类: LINUX

2013-09-23 17:41:37

1.proc文件读写代码:
  1. static int kdc_proc_write(struct file *file, const char __user *buffer,unsigned long count, void *data)
  2. {
  3.     char config_data[100]={0x00};
  4.     //unsigned long long target_bytes;
  5.     int ret = 0x00;

  6.     printk("kdc_proc_write called\n");
  7.     if (count <= 1)
  8.     {
  9.        printk("count <=1\n");
  10.        return -EBADMSG; /* runt */
  11.     }
  12.     if (count > sizeof(config_data))
  13.     {
  14.         printk("count > sizeof(config_data)\n");
  15.         return -EFBIG; /* too long */
  16.     }

  17.     if (copy_from_user(config_data, buffer, count))
  18.         return -EFAULT;
  19.      
  20.     int write_bytes =0;
  21.     uart_send(config_data,count,0,&write_bytes);

  22.     return ret;
  23. }

  24. static int kdc_proc_read(char *page, char **start, off_t off,int count, int *eof, void *data)
  25. {
  26.     int len;
  27.     printk("kdc_proc_read called\n");
  28.     unsigned char read_data[100]={0x00};
  29.     int receive_bytes=0;
  30.     uart_recv(read_data,20,0,&receive_bytes);
  31.     len = sprintf(page,"%s\n",read_data);

  32.     *eof = 1;
  33.     return len;
  34. }

2.问题如下:
近期在用proc文件调试串口时,发现一系列问题,向proc文件写入数据时,系统会不停的执行proc文件写操作,而读proc文件的时候,我明明读一次,但是系统会进入两次写proc文件的函数。

3.解决方法
后来发现proc文件写时,返回值不能返回0,否则系统认为写不成功,会一直循环写下去。而读proc文件时,返回值为0才表示读成功,其余值均表示不成功,也会循环读下去。
所以代码修改为如下:

  1. static int kdc_proc_write(struct file *file, const char __user *buffer,unsigned long count, void *data)
  2. {
  3.     char config_data[100]={0x00};
  4.     int ret = 0x00;

  5.     printk("kdc_proc_write called\n");
  6.     if (count <= 1)
  7.     {
  8.        printk("count <=1\n");
  9.        return -EBADMSG; /* runt */
  10.     }

  11.     if (count > sizeof(config_data))
  12.     {
  13.         printk("count > sizeof(config_data)\n");
  14.         return -EFBIG; /* too long */
  15.     }

  16.     if (copy_from_user(config_data, buffer, count))
  17.         return -EFAULT;
  18.    
  19.     int write_bytes =0;
  20.     uart_send(config_data,count,0,&write_bytes);
  21.     return write_bytes;
  22. }


  23. static int kdc_proc_read(char *page, char **start, off_t off,int count, int *eof, void *data)
  24. {
  25.     int len;
  26.     printk("kdc_proc_read called\n");
  27.     unsigned char read_data[100]={0x00};
  28.     int receive_bytes=0;
  29.     uart_recv(read_data,20,0,&receive_bytes);

  30.     len = sprintf(page,"%s\n",read_data);
  31.     *eof = 1;
  32.     return 0;
  33. }

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