/*wiegand 协议接收驱动代码*/
#include <linux/module.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/mm.h>
#include <linux/sched.h>
#include <linux/init.h>
#include <linux/cdev.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/time.h>
#include <linux/device.h>
#include <linux/semaphore.h>
#include <linux/kernel.h>
#include <linux/uaccess.h>
#include <linux/gpio.h>
#include <linux/slab.h>
#define WIEGAND_MAJOR 243
//delay 33ms to convert data
#define TIMER_DELAY HZ/8
#define DEVICE_NAME "wiegand"
bool TIMEER_FLAG = false;
bool RF_OPEN_FLAG = false;
static struct class *cdev_class;
//dev_t dev = 0;
#define D0_GPIO_NO 32*8+7-8 //gpio2_a7
#define D1_GPIO_NO 32*8+1-8 //gpio2_b0
struct wiegand_dev
{
char wiegand[26]; //Wiegand-26
int hid;
int pid;
int count; //Global Counter
struct cdev cdev;
struct semaphore sem;
struct completion receive_completion;
struct timer_list wiegand_timer;
struct work_struct pen_event_work;
struct workqueue_struct *ts_workqueue;
int gpio_d0;
int gpio_d1;
int d0_irq;
int d1_irq;
};
static struct wiegand_dev *rf_card;
//应该除了初始化外,只能有这一个地方能够将rf_card->count置为0,其余地方置为0皆不合法
static char convert_data(void)
{
int i,even,odd;
int cardno ;
//偶校验
even = 0;
for(i = 1; i < 13;i++)
{
if(rf_card->wiegand[i] == 1)
even = (~even) & 0x01;
}
if(even != rf_card->wiegand[0])
{
rf_card->count = 0;
goto error;
}
//奇校验
odd = 1;
for(i = 13; i< 25;i++)
{
if(rf_card->wiegand[i] == 1)
odd = (~odd)& 0x01;
}
if(odd != rf_card->wiegand[25])
{
rf_card->count = 0;
goto error;
}
//奇偶校验通过
rf_card->hid = 0;
for(i = 1 ;i<=8;i++)//hid转换
rf_card->hid = rf_card->hid << 1 |rf_card->wiegand[i];
rf_card->pid = 0;