1.proc文件读写代码:
-
static int kdc_proc_write(struct file *file, const char __user *buffer,unsigned long count, void *data)
-
{
-
char config_data[100]={0x00};
-
//unsigned long long target_bytes;
-
int ret = 0x00;
-
-
printk("kdc_proc_write called\n");
-
if (count <= 1)
-
{
-
printk("count <=1\n");
-
return -EBADMSG; /* runt */
-
}
-
if (count > sizeof(config_data))
-
{
-
printk("count > sizeof(config_data)\n");
-
return -EFBIG; /* too long */
-
}
-
-
if (copy_from_user(config_data, buffer, count))
-
return -EFAULT;
-
-
int write_bytes =0;
-
uart_send(config_data,count,0,&write_bytes);
-
-
return ret;
-
}
-
-
static int kdc_proc_read(char *page, char **start, off_t off,int count, int *eof, void *data)
-
{
-
int len;
-
printk("kdc_proc_read called\n");
-
unsigned char read_data[100]={0x00};
-
int receive_bytes=0;
-
uart_recv(read_data,20,0,&receive_bytes);
-
len = sprintf(page,"%s\n",read_data);
-
-
*eof = 1;
-
return len;
-
}
2.问题如下:
近期在用proc文件调试串口时,发现一系列问题,向proc文件写入数据时,系统会不停的执行proc文件写操作,而读proc文件的时候,我明明读一次,但是系统会进入两次写proc文件的函数。
3.解决方法
后来发现proc文件写时,返回值不能返回0,否则系统认为写不成功,会一直循环写下去。而读proc文件时,返回值为0才表示读成功,其余值均表示不成功,也会循环读下去。
所以代码修改为如下:
-
static int kdc_proc_write(struct file *file, const char __user *buffer,unsigned long count, void *data)
-
{
-
char config_data[100]={0x00};
-
int ret = 0x00;
-
-
printk("kdc_proc_write called\n");
-
if (count <= 1)
-
{
-
printk("count <=1\n");
-
return -EBADMSG; /* runt */
-
}
-
-
if (count > sizeof(config_data))
-
{
-
printk("count > sizeof(config_data)\n");
-
return -EFBIG; /* too long */
-
}
-
-
if (copy_from_user(config_data, buffer, count))
-
return -EFAULT;
-
-
int write_bytes =0;
-
uart_send(config_data,count,0,&write_bytes);
-
return write_bytes;
-
}
-
-
-
static int kdc_proc_read(char *page, char **start, off_t off,int count, int *eof, void *data)
-
{
-
int len;
-
printk("kdc_proc_read called\n");
-
unsigned char read_data[100]={0x00};
-
int receive_bytes=0;
-
uart_recv(read_data,20,0,&receive_bytes);
-
-
len = sprintf(page,"%s\n",read_data);
-
*eof = 1;
-
return 0;
-
}
阅读(1117) | 评论(0) | 转发(0) |