自定义字符设备的读取系统调用的回调函数(scull_read)在使用 $cat 读取时被无限次调用

The call-back function (scull_read) for read system call of a custum character device being called infinity times while reading using $cat

我编写了一个字符设备驱动程序来切换两个 GPIO 引脚。读取设备回调函数为scull_read()。设备打开、读取、写入和关闭的正常操作正常运行,如果我从 C 程序执行所有操作,但在使用 echo[=31= 从终端执行上述操作时]和cat,读写回调函数(scull_read()scull_write())被无限次调用。在此阶段,停止进程的终止信号 ^cscull_write() 不起作用。

测试代码已经可用,名称为:strong text

你能帮我解决这个问题吗?

这是一个githublink:https://github.com/guruprasad-92/Device-Driver.git

cat(大概是echo)会在循环中调用你的读取函数,一旦你的读取函数returns为0就终止,表明文件的末尾已经结束到达。 You can check the source here.

何时达到 "end of file" 由您决定。通常,这是通过在每次调用读取函数时更新文件偏移量来完成的。 (这是你调用的参数f_pos。)

因此,您需要在 scull_read 中执行类似 *f_pos += count 的操作。然后,当一个程序对同一个文件再次调用scull_read时,您可以检查*f_pos来判断是否还有数据要读取。如果没有剩余数据可读,scull_read 应该 return 0。

也许您会发现 this 有用。