rfid6-写成platform驱动

将前面rfid的misc挂到platform平台上,因为misc驱动都有了,所以很简单,代码复制过来就好了
设备侧就用上文的那个就好
/******************platfrom_dev.c***************************/  
#include <linux/module.h>  
#include <linux/device.h>  
#include <linux/platform_device.h>  
#include <linux/string.h>  
#include <linux/init.h>  
#include <linux/kernel.h>  
  
#define DEVICE_NAME "song_rfid"  
static struct platform_device *my_device;  
  
static int __init my_init(void)  
{  
    int ret=0;  
  
    my_device= platform_device_alloc(DEVICE_NAME,-1);//this name is matched for driver,song_rfid  
    ret=platform_device_add(my_device);  
  
    if (ret == 0) {  
        printk("Register %s\n",DEVICE_NAME);  
    } else {  
        printk("Register  error.\n");  
        platform_device_put(my_device);  
    }  
    return ret;  
}  
  
static void __exit my_exit(void)  
{  
    platform_device_unregister(my_device);  
    printk("Unregister %s\n",DEVICE_NAME);  
}  
  
module_init(my_init);  
module_exit(my_exit);  
  
MODULE_LICENSE("GPL");  
MODULE_VERSION("1.5"); 
驱动侧,就把misc驱动的init函数中实现的内容比如misc_register,射频初始化等放到platform的probe函数中即可
#include "rc522.h"
#define DEVICE_NAME "rfid"

#include <linux/module.h>  
#include <linux/device.h>  
#include <linux/platform_device.h>  
#include <linux/string.h>  
#include <linux/init.h>  
#include <linux/kernel.h>  
#include <linux/io.h>  

#define DRIVER_NAME "song_rfid"  

unsigned char  LastKeyA[6]={0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};//NO.2卡
unsigned char  NewKeyA[6]={0x19,0x84,0x07,0x15,0x76,0x14};//NO.2卡
unsigned char  NewKey[16]={0x19,0x84,0x07,0x15,0x76,0x14,
                0xff,0x07,0x80,0x69,
                0x19,0x84,0x07,0x15,0x76,0x14};

unsigned char  Read_Data[16]={0x00};
unsigned char  Write_First_Data[16];
unsigned char  Write_Consume_Data[16];
unsigned char  RevBuffer[30];
unsigned char  MLastSelectedSnr[4];
unsigned char  oprationcard;

extern signed char PcdReset(void);
extern signed char PcdRequest(unsigned char  req_code,unsigned char *pTagType);
extern void PcdAntennaOn(void);
extern void PcdAntennaOff(void);
extern signed char M500PcdConfigISOType(unsigned char  type);
extern signed char PcdAnticoll(unsigned char *pSnr);
extern signed char PcdSelect(unsigned char *pSnr);
extern signed char PcdAuthState(unsigned char  auth_mode,unsigned char  addr,unsigned char *pKey,unsigned char *pSnr);
extern signed char PcdWrite(unsigned char  addr,unsigned char *pData);
extern signed char PcdRead(unsigned char  addr,unsigned char *pData);
extern signed char PcdHalt(void);

 union flt_chr
{
  //float flt;
  int flt;
  unsigned char chr[4];
}fltchr;

void delay_5ms(int x)

{
unsigned long j=jiffies+x*10;
while(jiffies<j) {;}


}

unsigned long rfid_table [] = {
    S3C2410_GPF(0),//nss out 
    S3C2410_GPF(1),//sck out 
    S3C2410_GPF(2),//mosi out 
    S3C2410_GPF(3),//miso in
    S3C2410_GPF(4),//rst  out
};
unsigned int rfid_cfg_table [] = {
    S3C2410_GPIO_OUTPUT,
    S3C2410_GPIO_OUTPUT,
    S3C2410_GPIO_OUTPUT,
    S3C2410_GPIO_INPUT,
    S3C2410_GPIO_OUTPUT,
};

int open_flag;
void InitRc522(void)
{
  PcdReset();
  PcdAntennaOff();  
  PcdAntennaOn();
  M500PcdConfigISOType( 'A' );
}
void ctrlprocess(void)
{
 
  char  status;    
  int ii;
  printk("\n");
  printk("HZ=%d\n",HZ);
/**********************************************寻卡**************************************************************/
  status=PcdRequest(PICC_REQIDL,&RevBuffer[0]);//寻天线区内未进入休眠状态的卡,返回卡片类型 2字节
  if(status!=MI_OK)
  {    printk("XXXXXXXXXXXXXXXX no card type\n");
    return;
  }
   printk("*********** discard card typr\n");
 /**********************************************返回序列号**************************************************************/
 status=PcdAnticoll(&RevBuffer[2]);//防冲撞,返回卡的序列号 4字节
  if(status!=MI_OK)
  {
        printk("XXXXXXXXXXXXXXXX no card serial num\n");
    return;
  }    
    printk("***********dicover card serial num\n");


     memcpy(MLastSelectedSnr,&RevBuffer[2],4);
     for(ii=0;ii<4;ii++)
  {
    printk("%x",(MLastSelectedSnr[ii]>>4)&0x0f);
    printk("%x",MLastSelectedSnr[ii]&0x0f);
  }
 /**********************************************选卡**************************************************************/
     memcpy(MLastSelectedSnr,&RevBuffer[2],4);
    status=PcdSelect(MLastSelectedSnr);//选卡
  if(status!=MI_OK)
  {
   printk("XXXXXXXXXXXXXXXX selected no card\n");
    return;
  }
   printk("*********** selected card\n");
 /**********************************************验证密码,扇区1,扇区1的控制块,地址为7********************************/
        status=PcdAuthState(PICC_AUTHENT1A,7,NewKeyA,MLastSelectedSnr);
    if(status!=MI_OK)
    { printk("XXXXXXXXXXXXXXXX not right password \n");
      return;
    }
    printk("*********** right password\n");

    status=PcdRead(4,Read_Data);  //读卡   *********************************************************
    if(status!=MI_OK)
    {
    printk("XXXXXXXXXXXXXXXX the first reading card failed\n");
          return;
    }
    printk("*********** the first reading card success,as follows \n");
    for(ii=0;ii<4;ii++)
    {
          fltchr.chr[ii]=Read_Data[ii];
    printk("fltchr.chr[%d]=%x\n",ii,fltchr.chr[ii]);
    }
    printk("fltchr.flt=%d\n",fltchr.flt);
 /**********************************************写卡,扇区1,扇区1的控第一个数据块,地址为4********************************/
     //fltchr.flt=108;
    fltchr.flt+=6;
    
    printk("*********** write card with the data as follows \n");
    printk("fltchr.flt=%d\n",fltchr.flt);
       for(ii=0;ii<4;ii++)        
    {
      Write_First_Data[ii]=fltchr.chr[ii];
     printk("fltchr.chr[%d]=%x\n",ii,fltchr.chr[ii]);
    }
    for(ii=0;ii<4;ii++)    printk("Write_First_Data[%d]=%x\n",ii,Write_First_Data[ii]);    
    status=PcdWrite(4,&Write_First_Data[0]);    //写卡*********************************
    if(status!=MI_OK)
    {
    printk("XXXXXXXXXXXXXXXX write to card failed \n") ;     
    printk("status=%d\n",status);  
    return;
    }    
         printk("*********** write to card success with the data above\n") ;     

    status=PcdRead(4,Read_Data);  //读卡   *********************************************************
    if(status!=MI_OK)
    {
        printk("XXXXXXXXXXXXXXXX the first reading card failed\n");
          return;
    }
    printk("*********** the second reading card success,as follows\n");
    for(ii=0;ii<4;ii++)
    {
        fltchr.chr[ii]=Read_Data[ii];
         printk("fltchr.chr[%d]=%x\n",ii,fltchr.chr[ii]);
    }
        printk("fltchr.flt=%d\n",fltchr.flt);
        
}

static ssize_t write_rfid(struct file *filp, const char *buffer, size_t count, loff_t *ppos)
{            

    int ret;
    char *commad=0;
    if (count == 0) {
        return count;
    }
    commad=kmalloc(count+1,GFP_KERNEL);//need print it using %s, so plus 1 byte for '\0'
    ret = copy_from_user(commad, buffer, count);//if success,ret=0
    if (ret) {
        return ret;
    }
    commad[count]='\0';
    printk("from kernel commad=%s\n",commad);
    printk("from kenel ret=%d\n",ret);
    
//to test the command "set to 1" or "clear to 0" if correct 
    if(strncmp(commad,"SET_SPI_CS",count)==0) {SET_SPI_CS;printk("excuting %s succes !\n",commad);}    
    if(strncmp(commad,"CLR_SPI_CS",count)==0) {CLR_SPI_CS;printk("excuting %s succes !\n",commad);}

    if(strncmp(commad,"SET_SPI_CK",count)==0) {SET_SPI_CK;printk("excuting %s succes !\n",commad);}
    if(strncmp(commad,"CLR_SPI_CK",count)==0) {CLR_SPI_CK;printk("excuting %s succes !\n",commad);}

    if(strncmp(commad,"SET_SPI_MOSI",count)==0) {SET_SPI_MOSI;printk("excuting %s succes !\n",commad);}
    if(strncmp(commad,"CLR_SPI_MOSI",count)==0) {CLR_SPI_MOSI;printk("excuting %s succes !\n",commad);}

    if(strncmp(commad,"SET_RC522RST",count)==0) {SET_RC522RST;printk("excuting %s succes !\n",commad);}
    if(strncmp(commad,"CLR_RC522RST",count)==0) {CLR_RC522RST;printk("excuting %s succes !\n",commad);}
    kfree(commad);
    return ret ? ret : count;
    
}
static ssize_t read_rfid(struct file *filp, char *buffer, size_t count, loff_t *ppos)
{    printk("read_rfid1\n");    
    ctrlprocess();    
    printk("read_rfid2\n");    
    return 0;
}


static int release_rfid(struct inode *inode, struct file *filp)
{
    return 0;
}

static int open_rfid(struct inode *inode, struct file *filp)
{    
    open_flag=1;
    printk("open_flag=%d\n",open_flag);
    return 0;
}




static struct file_operations dev_fops = {
    .owner    =    THIS_MODULE,
    .open    =    open_rfid,
    .read    =    read_rfid,
    .write    =    write_rfid,
    .release=    release_rfid,
};

static struct miscdevice misc = {
    .minor = MISC_DYNAMIC_MINOR,
    .name = DEVICE_NAME,
    .fops = &dev_fops,
};
/**************************** 以上属于misc ************************/




/**************************** 以下属于platform ************************/

/*在平台驱动probe函数中实现misc功能型驱动注册,及其他咚咚初始化*/
static int  my_probe(struct platform_device *pdev)  
{  
int ret,i;
printk("driver find device : %s which can handle\n",DRIVER_NAME); 
for (i = 0; i < 5; i++) {
    s3c2410_gpio_cfgpin(rfid_table[i], rfid_cfg_table[i]);
    s3c2410_gpio_setpin(rfid_table[i], 0);
}

ret = misc_register(&misc);
    InitRc522()    ;
printk (DEVICE_NAME"\tinitialized\n");
open_flag=0;
return ret;

}  
/*在平台驱动remove函数中实现misc驱动的注销*/
static int  my_remove(struct platform_device *pdev)  
{  
misc_deregister(&misc);
printk("driver found device : %s unpluged\n",DRIVER_NAME);  
return 0;  
}  

static struct platform_driver my_driver = {  
.probe      = my_probe,  
.remove     = my_remove,  
.driver     = {  
    .owner  = THIS_MODULE,  
    .name   = DRIVER_NAME,//this name is matched for driver,song_rfid  
},  
};  
/*在驱动入口函数init中实现平台驱动的注册*/
static int __init my_init(void)  
{  
printk("Register my_driver.\n");  
return platform_driver_register(&my_driver);  
}  
/*在驱动注销函数中实现平台驱动的注销*/
static void __exit my_exit(void)  
{  
printk("Unregister my_driver.\n");  
platform_driver_unregister(&my_driver);  
} 

module_init(my_init);  
module_exit(my_exit);  
MODULE_LICENSE("GPL");  

[root@FriendlyARM plg]# insmod platform_dev.ko 
Register song_rfid
[root@FriendlyARM plg]# insmod mymodule.ko           //之后就会出现设备文件/dev/rfid 
Register my_driver.
driver find device : song_rfid which can handle
rfid    initialized
[root@FriendlyARM plg]# cat /dev/rfid 
open_flag=1
read_rfid1

HZ=200
rc522.c-PcdRequest: status = 0
*********** discard card typr
***********dicover card serial num
ce1ed4c7*********** selected card
*********** right password
*********** the first reading card success,as follows 
fltchr.chr[0]=17
fltchr.chr[1]=0
fltchr.chr[2]=d5
fltchr.chr[3]=42
fltchr.flt=1121255447
*********** write card with the data as follows 
fltchr.flt=1121255453
fltchr.chr[0]=1d
fltchr.chr[1]=0
fltchr.chr[2]=d5
fltchr.chr[3]=42
Write_First_Data[0]=1d
Write_First_Data[1]=0
Write_First_Data[2]=d5
Write_First_Data[3]=42
*********** write to card success with the data above
*********** the second reading card success,as follows
fltchr.chr[0]=1d
fltchr.chr[1]=0
fltchr.chr[2]=d5
fltchr.chr[3]=42
fltchr.flt=1121255453
read_rfid2
[root@FriendlyARM plg]# 

http://download.csdn.net/detail/songqqnew/3716591
posted on 2011-10-25 03:54  _song  阅读(294)  评论(0编辑  收藏  举报