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