用于智能卡IC模块的Linux设备驱动程序 [英] Linux device driver for a Smart Card IC module

查看:237
本文介绍了用于智能卡IC模块的Linux设备驱动程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个智能卡IC模块,并且我想为其创建Linux设备驱动程序.该模块使用SPI作为控制线,并具有一条中断线以指示卡是否已就绪.我知道如何在Linux内核中创建SPI设备,以及如何在发生中断时在内核中读取数据.但是我不知道如何将数据传输到用户空间(可能需要为其创建设备节点),以及如何中断用户空间以通知它.有人有什么建议吗?

I have a smart card IC module, and I want to create a Linux device driver for it. This module is using SPI as the controlling line and has an interrupt line to indicate whether a card is ready. I know how to create a SPI device in Linux kernel and how to read data in the kernel when the interruption happens. But I have no idea on how to transfer the data to the user space (maybe need to create a device node for it), and how to give the user space a interruption to notify it. Does anyone have some suggestion?

推荐答案

一种解决方法是创建devfs条目,然后让感兴趣的进程打开该设备,并使用以下命令从设备驱动程序接收异步通知fasync.

One way you can go about this is by creating a devfs entry and then having the interested process open that device and receive asynchronous notification from the device driver using fasync.

在用户空间中收到通知后,您可以通过您认为合适的任何方式通知其他感兴趣的进程.

Once you have the notification in user space you can notify other interested processes by any means you deem fit.

我正在编写一个精简的小示例来说明此功能.

I am writing a small trimmed down example illustrating this feature.

/* Appropriate headers */

static int myfasync(int fd, struct file *fp, int on);
static struct fasync_struct *fasyncQueue;

static struct file_operations fops =
{
     .open = charDriverOpen,
     .release = charDriverClose,
     .read = charDriverRead,
     .write = charDriverWrite,
     .unlocked_ioctl = charDriverCtrl,
     // This will be called when the FASYNC flag is set
     .fasync = myfasync,
};

static int __init charDriverEntry()
{
      // Appropriate init for the driver
      // Nothing specific needs to be done here with respect to
      // fasync feature.
 }

static int myfasync(int fd, struct file *fp, int on)
{
      // Register the process pointed to by fp to the list
      // of processes to be notified when any event occurs
      return fasync_helper(fd, fp, 1, &fasyncQueue);
}

// Now to the part where we want to notify the processes listed
// in fasyncQueue when something happens. Here in this example I had
// implemented the timer. Not getting in to the details of timer func
// here
static void send_signal_timerfn(unsigned long data)
{
       ...
       printk(KERN_INFO "timer expired \n");

       kill_fasync(&fasyncQueue, SIGIO, POLL_OUT);
       ...
}

在用户登陆过程侧

void my_notifier(int signo, siginfo_t *sigInfo, void *data)
{
        printf("Signal received from the driver expected %d got %d \n",SIGIO,signo);
}

int main()
{
       struct sigaction signalInfo;
       int flagInfo;

       signalInfo.sa_sigaction = my_notifier;
       signalInfo.sa_flags = SA_SIGINFO;
       sigemptyset(&signalInfo.sa_mask);

       sigaction(SIGIO, &signalInfo, NULL);


       int fp,i;
       fp = open("/dev/myCharDevice",O_RDWR);
       if (fp<0)
               printf("Failed to open\n");

       /*New we will own the device so that we can get the signal from the device*/
       // Own the process
       fcntl(fp, F_SETOWN, getpid());
       flagInfo = fcntl(fp, F_GETFL);
       // Set the FASYNC flag this triggers the fasync fops 
       fcntl(fp, F_SETFL, flagInfo|FASYNC);
       ...
}

希望这可以清除一切. 有关详细阅读,建议您阅读

Hope this clears things up. For more detailed reading I suggest you read this

这篇关于用于智能卡IC模块的Linux设备驱动程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆