Commit 754081a7 authored by 吴智聪(John Wu)'s avatar 吴智聪(John Wu)

cleanup ir blaster driver code

parent d0be9aad
......@@ -72,7 +72,8 @@ static struct timer_list learning_key_timer;
static struct blaster_data_type *BlsKey;
static struct blaster_data_pack *bls_data_pack;
struct irrtc_device {
struct irrtc_device
{
int key;
};
static struct irrtc_device device;
......@@ -106,226 +107,229 @@ static void disable_learning(void)
static void set_timer1_div(struct blaster_data_pack *blsdat)
{
unsigned int div;
int count;
static int sbit;
if(wave_len)
{
div=wave_len;
if(div>MAX_COUNTER)
div=MAX_COUNTER;
wave_len-=div;
}
else
{
count=blsdat->bitstimes&BITS_COUNT_MASK;
if (count==bls_wave_count)
sbit=0;
if (keybit_get(blsdat->mbits, (count-bls_wave_count)))
{
if (keybit_get(blsdat->dbits, (count-bls_wave_count)))
{
div=blsdat->specbits[sbit];
sbit++;
} else
{
div=blsdat->bit2;
}
} else
{
if (keybit_get(blsdat->dbits, (count-bls_wave_count)))
{
div=blsdat->bit1;
} else
{
div=blsdat->bit0;
}
}
}
unsigned int div;
int count;
static int sbit;
if(wave_len)
{
div=wave_len;
if(div>MAX_COUNTER)
div=MAX_COUNTER;
wave_len-=div;
}
else
{
count=blsdat->bitstimes&BITS_COUNT_MASK;
if(count==bls_wave_count)
sbit=0;
if(keybit_get(blsdat->mbits, (count-bls_wave_count)))
{
if(keybit_get(blsdat->dbits, (count-bls_wave_count)))
{
div=blsdat->specbits[sbit];
sbit++;
}
else
{
div=blsdat->bit2;
}
}
else
{
if(keybit_get(blsdat->dbits, (count-bls_wave_count)))
{
div=blsdat->bit1;
}
else
{
div=blsdat->bit0;
}
}
}
TIMER1_TGCR &= ~(15<<8);
TIMER1_TGCR |= BLS_TIMER_PRESCALE<<8;
//div-=div>>5; // The ideal situation is that set the timer prescale to let the timer cycle is 1us
// but the nearest setting make the cycle 1.032us so do this adjustment with
// divider value
div-=INT_TIMER1_TIME_WASTE;
if(div>MAX_COUNTER)
{
wave_len=div-MAX_COUNTER;
div=MAX_COUNTER;
}
div--;
//div-=div>>5; // The ideal situation is that set the timer prescale to let the timer cycle is 1us
// but the nearest setting make the cycle 1.032us so do this adjustment with
// divider value
div-=INT_TIMER1_TIME_WASTE;
if(div>MAX_COUNTER)
{
wave_len=div-MAX_COUNTER;
div=MAX_COUNTER;
}
div--;
TIMER1_PRD34 = div; // set timer period
}
static void blaster_key(struct blaster_data_pack* blsdat)
{
uint16_t bitset2;
enable_irq(IRQ_TINT1_TINT34);
wave_len=0;
bls_status=BLS_START;
bls_wave_count=blsdat->bitstimes&BITS_COUNT_MASK;
if(bls_wave_count>BLASTER_MAX_CHANGE || bls_wave_count<=0)
{
bls_status=BLS_ERROR;
if(bls_data_pack)
{
kfree(bls_data_pack);
bls_data_pack=NULL;
}
return;
}
uint16_t bitset2;
enable_irq(IRQ_TINT1_TINT34);
wave_len=0;
bls_status=BLS_START;
bls_wave_count=blsdat->bitstimes&BITS_COUNT_MASK;
if(bls_wave_count>BLASTER_MAX_CHANGE || bls_wave_count<=0)
{
bls_status=BLS_ERROR;
if(bls_data_pack)
{
kfree(bls_data_pack);
bls_data_pack=NULL;
}
return;
}
GPIO23_DIR &= ~GIO_BLS; //gio 47 direction output
/*check if the io port status correct if not correct set it's logic to reverse of start level and hold for a momemt*/
bitset2=GPIO23_OUT_DATA;
if (((bitset2 & GIO_BLS)!=0)&&((bls_data_pack->bitstimes&BITS_COUNT_MASK)!=0))
{
/*check if the io port status correct if not correct set it's logic to reverse of start level and hold for a momemt*/
bitset2=GPIO23_OUT_DATA;
if(((bitset2 & GIO_BLS)!=0)&&((bls_data_pack->bitstimes&BITS_COUNT_MASK)!=0))
{
GPIO23_CLR_DATA |= GIO_BLS;
msleep(WAIT_HARDWARE_RESET);
}
else if (((bitset2 & GIO_BLS)==0)&&((bls_data_pack->bitstimes&BITS_COUNT_MASK)==0))
{
msleep(WAIT_HARDWARE_RESET);
}
else if(((bitset2 & GIO_BLS)==0)&&((bls_data_pack->bitstimes&BITS_COUNT_MASK)==0))
{
GPIO23_SET_DATA |= GIO_BLS;
msleep(WAIT_HARDWARE_RESET);
}
if(blsdat->bitstimes & FIRST_LEVEL_BIT_MASK)
msleep(WAIT_HARDWARE_RESET);
}
if(blsdat->bitstimes & FIRST_LEVEL_BIT_MASK)
GPIO23_SET_DATA |= GIO_BLS;
else
else
GPIO23_CLR_DATA |= GIO_BLS;
set_timer1_div(blsdat);
set_timer1_div(blsdat);
TIMER1_TCR &= ~(3<<22); //disable timer1 34
TIMER1_TCR |= (2<<22); //enable timer1 34 as continue mode
}
void timer_handle(unsigned long data)
{
int osd_key;
dbg("bitstimes=%d\n", BlsKey->bitstimes);
if (learning_status & WAIT_LEARN_COMPLETE)
{
lock_data_protect();
osd_key=get_osd_key();
unlock_data_protect();
if (!osd_key )
{
disable_irq(IRQ_TINT1_TINT34);
int osd_key;
dbg("bitstimes=%d\n", BlsKey->bitstimes);
if(learning_status & WAIT_LEARN_COMPLETE)
{
lock_data_protect();
osd_key=get_osd_key();
unlock_data_protect();
if(!osd_key )
{
disable_irq(IRQ_TINT1_TINT34);
TIMER1_TCR &= ~(3<<22); //disable timer1 34
report_key(LEARNING_COMPLETE_KEY);
//report_key(UP_KEY);
}
learning_status |= WAIT_RELEASE_REMOTE;
learning_status &= ~WAIT_LEARN_COMPLETE;
learning_key_timer.expires = jiffies + POLL_RELEASE_DELAY;
learning_key_timer.function = timer_handle;
add_timer(&learning_key_timer);
}
else if (learning_status & WAIT_RELEASE_REMOTE)
{
if (learning_status & KEY_WAVE_PRESENT)
{
learning_status &= ~KEY_WAVE_PRESENT;
learning_key_timer.expires = jiffies + POLL_RELEASE_DELAY;
learning_key_timer.function = timer_handle;
add_timer(&learning_key_timer);
}
else
{
report_key(LEARNING_COMPLETE_KEY);
//report_key(UP_KEY);
}
learning_status |= WAIT_RELEASE_REMOTE;
learning_status &= ~WAIT_LEARN_COMPLETE;
learning_key_timer.expires = jiffies + POLL_RELEASE_DELAY;
learning_key_timer.function = timer_handle;
add_timer(&learning_key_timer);
}
else if(learning_status & WAIT_RELEASE_REMOTE)
{
if(learning_status & KEY_WAVE_PRESENT)
{
learning_status &= ~KEY_WAVE_PRESENT;
learning_key_timer.expires = jiffies + POLL_RELEASE_DELAY;
learning_key_timer.function = timer_handle;
add_timer(&learning_key_timer);
}
else
{
disable_learning();
lock_data_protect();
osd_key=get_osd_key();
unlock_data_protect();
if (!osd_key )
{
report_key(RELEASE_REMOTE_KEY);
//report_key(UP_KEY);
}
}
}
lock_data_protect();
osd_key=get_osd_key();
unlock_data_protect();
if(!osd_key )
{
report_key(RELEASE_REMOTE_KEY);
//report_key(UP_KEY);
}
}
}
}
static int capture_key(struct blaster_data_type* blsdat)
{
static int times=0;
static int old_counter;
static int old_int_counter;
int counter;
int td;
if (!(learning_status & WAIT_RELEASE_REMOTE))
{
counter=TIMER1_TIM34;
if (old_int_counter==timer_int_counter)
td=counter - old_counter;
else
td=MAX_COUNTER - old_counter - INT_CAPTURE_TIME_WASTE + counter ;
old_counter=counter;
old_int_counter=timer_int_counter;
if (!(learning_status & WAIT_LEARN_COMPLETE))
{
times = 0;
learning_key_timer.function = timer_handle;
mod_timer(&learning_key_timer, jiffies + LEANRING_COMPLETE_DELAY);
learning_status |= WAIT_LEARN_COMPLETE;
}
if (0 == times++)
{
blsdat->bitstimes |= ((SET_GPIO01_IN_DATA & GIO_CAP) << 8);
return 0;
}
blsdat->bits[times-2]=td;
blsdat->bitstimes++;
if (times == BLASTER_MAX_CHANGE+1)
{
report_key(LEARNING_COMPLETE_KEY);
learning_key_timer.function = timer_handle;
mod_timer(&learning_key_timer, jiffies + POLL_RELEASE_DELAY);
learning_status |= WAIT_RELEASE_REMOTE;
learning_status &= ~WAIT_LEARN_COMPLETE;
}
}
else
learning_status |= KEY_WAVE_PRESENT;
static int old_counter;
static int old_int_counter;
int counter;
int td;
if(!(learning_status & WAIT_RELEASE_REMOTE))
{
counter=TIMER1_TIM34;
if(old_int_counter==timer_int_counter)
td=counter - old_counter;
else
td=MAX_COUNTER - old_counter - INT_CAPTURE_TIME_WASTE + counter ;
old_counter=counter;
old_int_counter=timer_int_counter;
if(!(learning_status & WAIT_LEARN_COMPLETE))
{
times = 0;
learning_key_timer.function = timer_handle;
mod_timer(&learning_key_timer, jiffies + LEANRING_COMPLETE_DELAY);
learning_status |= WAIT_LEARN_COMPLETE;
}
if(0 == times++)
{
blsdat->bitstimes |= ((SET_GPIO01_IN_DATA & GIO_CAP) << 8);
return 0;
}
blsdat->bits[times-2]=td;
blsdat->bitstimes++;
if(times == BLASTER_MAX_CHANGE+1)
{
report_key(LEARNING_COMPLETE_KEY);
learning_key_timer.function = timer_handle;
mod_timer(&learning_key_timer, jiffies + POLL_RELEASE_DELAY);
learning_status |= WAIT_RELEASE_REMOTE;
learning_status &= ~WAIT_LEARN_COMPLETE;
}
}
else
learning_status |= KEY_WAVE_PRESENT;
return(0);
}
static irqreturn_t handle_bls_timer1_irqs(int irq, void * dev_id, struct pt_regs * regs)
{
uint32_t bitset2;
if(int_type==CAPTURE)
{
timer_int_counter++;
return IRQ_HANDLED;
}
if(bls_wave_count==0)
return IRQ_HANDLED;
if (!wave_len)
{
bitset2=GPIO23_OUT_DATA;
if (bitset2 & GIO_BLS)
uint32_t bitset2;
if(int_type==CAPTURE)
{
timer_int_counter++;
return IRQ_HANDLED;
}
if(bls_wave_count==0)
return IRQ_HANDLED;
if(!wave_len)
{
bitset2=GPIO23_OUT_DATA;
if(bitset2 & GIO_BLS)
GPIO23_CLR_DATA |= GIO_BLS;
else
else
GPIO23_SET_DATA |= GIO_BLS;
bls_wave_count--;
}
if(bls_wave_count==0)
{
disable_irq(IRQ_TINT1_TINT34);
bls_wave_count--;
}
if(bls_wave_count==0)
{
disable_irq(IRQ_TINT1_TINT34);
TIMER1_TCR &= ~(3<<22); //disable timer1 34
GPIO23_DIR |= GIO_BLS; //gio 47 direction input
bls_status=BLS_COMPLETE;
if(bls_data_pack)
{
kfree(bls_data_pack);
bls_data_pack=NULL;
}
return IRQ_HANDLED;
}
bls_status=BLS_COMPLETE;
if(bls_data_pack)
{
kfree(bls_data_pack);
bls_data_pack=NULL;
}
return IRQ_HANDLED;
}
else
{
set_timer1_div(bls_data_pack);
}
return IRQ_HANDLED;
{
set_timer1_div(bls_data_pack);
}
return IRQ_HANDLED;
}
static irqreturn_t handle_capture_irqs(int irq, void * dev_id, struct pt_regs * regs)
......@@ -339,7 +343,7 @@ static irqreturn_t handle_capture_irqs(int irq, void * dev_id, struct pt_regs *
static int irrtc_open(struct inode * inode, struct file * file)
{
#ifdef ONLYOPENONE
if (opened)
if(opened)
return -EBUSY;
opened = 1;
#endif
......@@ -350,7 +354,7 @@ static int irrtc_open(struct inode * inode, struct file * file)
static int irrtc_release(struct inode * inode, struct file * file)
{
#ifdef ONLYOPENONE
if (!opened)
if(!opened)
return -ERESTARTSYS;
opened = 0;
......@@ -360,90 +364,90 @@ static int irrtc_release(struct inode * inode, struct file * file)
}
static int irrtc_ioctl(struct inode * inode, struct file * file,
unsigned int cmd, unsigned long arg)
unsigned int cmd, unsigned long arg)
{
int ret = 0;
int arg_size;
if (_IOC_TYPE(cmd) != NEUROS_IR_BLASTER_IOC_MAGIC)
{
ret = -EINVAL;
goto bail;
}
arg_size = _IOC_SIZE(cmd);
if (_IOC_DIR(cmd) & _IOC_READ)
ret = !access_ok(VERIFY_WRITE, (void *)arg, arg_size);
else if (_IOC_DIR(cmd) & _IOC_WRITE)
ret = !access_ok(VERIFY_READ, (void *)arg, arg_size);
if (ret) goto bail;
switch (cmd)
{
case RRB_BLASTER_KEY:
{
if (bls_status!=BLS_START)
{
int_type=BLASTER;
bls_data_pack=kmalloc(sizeof(struct blaster_data_pack), GFP_KERNEL);
if (bls_data_pack==NULL)
{
ret = -EINVAL;
break;
}
copy_from_user(bls_data_pack, (void*)arg, sizeof(struct blaster_data_pack));
blaster_key(bls_data_pack);
}
}
break;
case RRB_CAPTURE_KEY:
{
set_osd_key(0);
int_type=CAPTURE;
timer_int_counter=0;
int ret = 0;
int arg_size;
if(_IOC_TYPE(cmd) != NEUROS_IR_BLASTER_IOC_MAGIC)
{
ret = -EINVAL;
goto bail;
}
arg_size = _IOC_SIZE(cmd);
if(_IOC_DIR(cmd) & _IOC_READ)
ret = !access_ok(VERIFY_WRITE, (void *)arg, arg_size);
else if(_IOC_DIR(cmd) & _IOC_WRITE)
ret = !access_ok(VERIFY_READ, (void *)arg, arg_size);
if(ret) goto bail;
switch(cmd)
{
case RRB_BLASTER_KEY:
{
if(bls_status!=BLS_START)
{
int_type=BLASTER;
bls_data_pack=kmalloc(sizeof(struct blaster_data_pack), GFP_KERNEL);
if(bls_data_pack==NULL)
{
ret = -EINVAL;
break;
}
copy_from_user(bls_data_pack, (void*)arg, sizeof(struct blaster_data_pack));
blaster_key(bls_data_pack);
}
}
break;
case RRB_CAPTURE_KEY:
{
set_osd_key(0);
int_type=CAPTURE;
timer_int_counter=0;
TIMER1_TCR &= ~(3<<22); //disable timer1 34
TIMER1_TGCR &= ~(15<<8);
TIMER1_TGCR |= BLS_TIMER_PRESCALE<<8;
TIMER1_PRD34 = MAX_COUNTER; // set timer period
TIMER1_TCR |= (2<<22); //enable timer1 34 as continuous mode
enable_irq(IRQ_TINT1_TINT34);
BlsKey=kmalloc(sizeof(struct blaster_data_type), GFP_KERNEL);
if(BlsKey==NULL)
{
ret = -EINVAL;
break;
}
memset(BlsKey, 0, sizeof(struct blaster_data_type));
learning_status=0;
enable_irq(IRQ_TINT1_TINT34);
BlsKey=kmalloc(sizeof(struct blaster_data_type), GFP_KERNEL);
if(BlsKey==NULL)
{
ret = -EINVAL;
break;
}
memset(BlsKey, 0, sizeof(struct blaster_data_type));
learning_status=0;
enable_learning();
}
break;
case RRB_READ_LEARNING_DATA:
{
if(BlsKey)
{
ret |= copy_to_user((void*)arg, BlsKey, sizeof(struct blaster_data_type));
kfree(BlsKey);
BlsKey=NULL;
}
}
break;
case RRB_FACTORY_TEST:
{
set_factory_test(1);
}
break;
case RRB_GET_BLASTER_STATUS:
{
ret |= copy_to_user((void*)arg, &bls_status, sizeof(bls_status));
}
break;
default:
ret = -EINVAL;
break;
}
bail:
return ret;
}
break;
case RRB_READ_LEARNING_DATA:
{
if(BlsKey)
{
ret |= copy_to_user((void*)arg, BlsKey, sizeof(struct blaster_data_type));
kfree(BlsKey);
BlsKey=NULL;
}
}
break;
case RRB_FACTORY_TEST:
{
set_factory_test(1);
}
break;
case RRB_GET_BLASTER_STATUS:
{
ret |= copy_to_user((void*)arg, &bls_status, sizeof(bls_status));
}
break;
default:
ret = -EINVAL;
break;
}
bail:
return ret;
}
static struct file_operations irrtc_fops = {
......@@ -483,10 +487,10 @@ static int __init irrtc_init(void)
printk(KERN_INFO "\t" MOD_DESC "\n");
status = register_chrdev(NEUROS_IR_BLASTER_MAJOR, "ir_blaster", &irrtc_fops);
if (status != 0)
if(status != 0)
{
if (status == -EINVAL) printk(KERN_ERR "%s Couldn't register device: invalid major number %d.\n", pname, NEUROS_IR_BLASTER_MAJOR);
else if (status == -EBUSY) printk(KERN_ERR "%s Couldn't register device: major number %d already busy.\n", pname, NEUROS_IR_BLASTER_MAJOR);
if(status == -EINVAL) printk(KERN_ERR "%s Couldn't register device: invalid major number %d.\n", pname, NEUROS_IR_BLASTER_MAJOR);
else if(status == -EBUSY) printk(KERN_ERR "%s Couldn't register device: major number %d already busy.\n", pname, NEUROS_IR_BLASTER_MAJOR);
else printk(KERN_ERR "%s Couldn't register device: error %d.\n", pname, status);
status = -1;
goto out;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment