linux-kernel - 智能卡 IC 模块的 Linux 设备驱动程序
问题描述
我有一个智能卡 IC 模块,我想为它创建一个 Linux 设备驱动程序。该模块使用 SPI 作为控制线,并有一个中断线来指示卡是否准备好。我知道如何在 Linux 内核中创建一个 SPI 设备,以及如何在中断发生时读取内核中的数据。但是我不知道如何将数据传输到用户空间(可能需要为其创建一个设备节点),以及如何给用户空间一个中断来通知它。有人有什么建议吗?
解决方案
解决此问题的一种方法是创建一个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);
...
}
希望这可以解决问题。如需更详细的阅读,我建议您阅读此内容
推荐阅读
- excel - Vba 抓取 Twitter 详细信息
- artifactory - 有没有办法只备份工件存储库的元数据而不是内容
- javascript - Javascript - 返回初始状态
- php - 无法使用 VPN 和 WSL2 连接 SQL Server 数据库
- sql-server - 如何始终以相同的顺序返回行
- bash - Bash 脚本将日志从一个 pod 分离到另一个
- excel - Adding a note to a cell based on another cell
- node.js - 在 Node.js 模板中加载内联脚本,然后将其发送到客户端
- haskell - 使用 QuickCheck 将 ST 与多态测试“模板”一起使用
- esp32 - 如何将 ESP32 设置为最大 BLE 功率?