我有一个智能卡IC模块,我想为它创建一个Linux设备驱动程序。此模块使用SPI作为控制线,并有一条中断线来指示卡是否就绪。我知道如何在Linux内核中创建SPI设备,以及如何在中断发生时读取内核中的数据。但我不知道如何将数据传输到用户空间(可能需要为它创建设备节点),以及如何给用户空间一个中断来通知它。有人有什么建议吗?
发布于 2019-05-17 14:25:49
可以这样做的一种方法是创建一个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);
...
}希望这能把事情说清楚。有关更详细的阅读,我建议您阅读this
https://stackoverflow.com/questions/56164078
复制相似问题