1
0
mirror of https://github.com/richardghirst/PiBits.git synced 2024-11-28 12:24:11 +01:00

Allow dev_write to work per-process.

This commit is contained in:
Robin Mallinson 2012-11-18 23:35:32 +00:00
parent ef87ef88a7
commit b1bb608d4c
2 changed files with 29 additions and 22 deletions

View File

@ -404,21 +404,27 @@ static ssize_t dev_read(struct file *filp,char *buf,size_t count,loff_t *f_pos)
static ssize_t dev_write(struct file *filp,const char *buf,size_t count,loff_t *f_pos) static ssize_t dev_write(struct file *filp,const char *buf,size_t count,loff_t *f_pos)
{ {
char* end_of_line=0; char* str = 0;
static int idx=0; struct process_data* const pdata = filp->private_data;
static char str[32]; if (0 == pdata) return 0;
static const int max_idx = sizeof(str) - 1;
if ((idx+count) > max_idx) count = max_idx-idx; // Read user data into str
if (copy_from_user(str, buf+idx, count)) { {
char* end_of_line=0;
static const int max_idx = sizeof(pdata->cmd_str) - 1;
int* const idx = &(pdata->cmd_idx);
str = pdata->cmd_str;
if ((*idx+count) > max_idx) count = max_idx-*idx;
if (copy_from_user(str, buf+*idx, count)) {
return -EFAULT; return -EFAULT;
} }
idx+=count; *idx+=count;
str[idx] = '\0'; str[*idx] = '\0';
end_of_line = strchr(str, '\n'); end_of_line = strchr(str, '\n');
if (NULL == end_of_line) if (NULL == end_of_line)
{ {
if (31 == idx) { if (31 == *idx) {
// Full buf without '\n' // Full buf without '\n'
printk(KERN_WARNING "ServoBlaster: Failed to parse command (%s)\n", str); printk(KERN_WARNING "ServoBlaster: Failed to parse command (%s)\n", str);
return -EINVAL; return -EINVAL;
@ -426,9 +432,10 @@ static ssize_t dev_write(struct file *filp,const char *buf,size_t count,loff_t *
return count; // Incomplete line. return count; // Incomplete line.
} }
// We've reached end of command, so terminate string at '\n' and reset idx. // End of command, so terminate string at '\n' and reset idx.
*end_of_line = '\0'; *end_of_line = '\0';
idx=0; *idx=0;
}
// Process the complete user string. // Process the complete user string.
{ {

Binary file not shown.