首页 > 解决方案 > 智能卡 IC 模块的 Linux 设备驱动程序

问题描述

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

标签: linux-kernellinux-device-driver

解决方案


解决此问题的一种方法是创建一个devfs条目,然后让感兴趣的进程打开该设备并使用fasync.

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

我正在写一个小例子来说明这个特性。

在驾驶员一侧

/* 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);
       ...
}

希望这可以解决问题。如需更详细的阅读,我建议您阅读此内容


推荐阅读