parent
ee18e4dfa5
commit
f530fb60c9
@ -0,0 +1,74 @@ |
||||
|
||||
static char device_buffer[1]; |
||||
static int major; |
||||
static uint8_t buff_empty=1; |
||||
static uint8_t buff_start; |
||||
static uint8_t buff_end; |
||||
|
||||
|
||||
static uint8_t buffer[BUFFER_SIZE]; |
||||
|
||||
static uint8_t readBuffer(void){ |
||||
uint8_t ret;
|
||||
if(buff_empty){ |
||||
return 0; |
||||
} |
||||
ret=buffer[buff_end]; |
||||
buff_end++; |
||||
if(buff_end==BUFFER_SIZE){ |
||||
buff_end=0; |
||||
} |
||||
if(buff_end==buff_start){ |
||||
buff_empty=1; |
||||
} |
||||
return ret; |
||||
} |
||||
|
||||
static uint8_t writeToBuffer(uint8_t data){ |
||||
if(buff_start==buff_end && !buff_empty){ |
||||
/*overrun*/ |
||||
printk("<1>%s: Buffer overrun!\n",MOD_NAME); |
||||
return 1; |
||||
} |
||||
buffer[buff_start]=data; |
||||
buff_start++; |
||||
if(buff_start==BUFFER_SIZE){ |
||||
buff_start=0; |
||||
} |
||||
buff_empty=0; |
||||
return 0; |
||||
} |
||||
|
||||
static ssize_t device_read(struct file *filp, char __user *buffer, size_t length, loff_t *offset){ |
||||
uint8_t val; |
||||
if(!buff_empty){ |
||||
val = readBuffer(); |
||||
DEBUG_OUT("<1>%s: device_read invoked writing: mem[%x]=%02x\n",MOD_NAME,buffer,val); |
||||
if( copy_to_user(buffer,&val,1)<0 ){ |
||||
printk("<1>%s: Device read error!\n",MOD_NAME); |
||||
return 0; |
||||
} |
||||
return 1; |
||||
} |
||||
return 0; |
||||
} |
||||
|
||||
static ssize_t device_write(struct file *filp, const char __user *buff, size_t len, loff_t *off){ |
||||
int ret;
|
||||
DEBUG_OUT("<1>%s: device_write invoked\n",MOD_NAME); |
||||
if(len!=1){ |
||||
return -EINVAL; |
||||
} |
||||
ret = copy_from_user(device_buffer, buff, len); |
||||
if( ret<0 ){ |
||||
printk("<1>%s: Device write error!\n",MOD_NAME); |
||||
return 0; |
||||
} |
||||
return len; |
||||
} |
||||
|
||||
struct file_operations fops = { |
||||
read: device_read, |
||||
write: device_write, |
||||
}; |
||||
|
||||
@ -0,0 +1,8 @@ |
||||
|
||||
#ifndef _COMMON_H |
||||
#define _COMMON_H |
||||
|
||||
#define MOD_NAME "usb_gpio" |
||||
enum pin_commands {CMD_IN, CMD_OUTH, CMD_OUTL, CMD_H, CMD_L, CMD_READ}; |
||||
|
||||
#endif //_COMMON_H
|
||||
@ -1,20 +1,49 @@ |
||||
|
||||
static int get_value(struct gpio_chip *gc, unsigned offset){ |
||||
int ret; |
||||
|
||||
DEBUG_OUT("<1>%s: GET_VALUE of pin%d ",MOD_NAME,offset); |
||||
return 0; |
||||
|
||||
ret=writeToBuffer(offset); |
||||
ret|=writeToBuffer(CMD_READ); |
||||
|
||||
return -ret; |
||||
} |
||||
|
||||
static void set_value(struct gpio_chip *chip, unsigned offset, int value){ |
||||
DEBUG_OUT("<1>%s: SET_VALUE of pin%d to %d",MOD_NAME,offset,value); |
||||
|
||||
writeToBuffer(offset); |
||||
if(value){ |
||||
writeToBuffer(CMD_H); |
||||
}else{ |
||||
writeToBuffer(CMD_L); |
||||
} |
||||
} |
||||
|
||||
static int set_in(struct gpio_chip *chip, unsigned offset){ |
||||
int ret=0; |
||||
|
||||
DEBUG_OUT("<1>%s: SET_INPUT of pin%d ",MOD_NAME,offset); |
||||
return 0; |
||||
|
||||
ret=writeToBuffer(offset); |
||||
ret|=writeToBuffer(CMD_IN); |
||||
|
||||
return -ret; |
||||
} |
||||
|
||||
static int set_out(struct gpio_chip *chip, unsigned offset, int value){ |
||||
int ret; |
||||
|
||||
DEBUG_OUT("<1>%s: SET_OUTPUT of pin%d to %d",MOD_NAME,offset,value); |
||||
return 0; |
||||
|
||||
ret=writeToBuffer(offset); |
||||
if(value){ |
||||
ret|=writeToBuffer(CMD_OUTH); |
||||
}else{ |
||||
ret|=writeToBuffer(CMD_OUTL); |
||||
} |
||||
|
||||
return -ret; |
||||
} |
||||
|
||||
|
||||
Loading…
Reference in new issue