首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >智能卡IC模块的Linux设备驱动程序

智能卡IC模块的Linux设备驱动程序
EN

Stack Overflow用户
提问于 2019-05-16 16:25:53
回答 1查看 92关注 0票数 0

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

EN

回答 1

Stack Overflow用户

发布于 2019-05-17 14:25:49

可以这样做的一种方法是创建一个devfs条目,然后让感兴趣的进程打开该设备,并使用fasync从设备驱动程序接收异步通知。

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

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

在驾驶员方面

代码语言:javascript
复制
/* 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);
       ...
}

在用户土地进程端

代码语言:javascript
复制
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);
       ...
}

希望这能把事情说清楚。有关更详细的阅读,我建议您阅读this

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56164078

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档