Linux 从驱动程序内部使用驱动程序
我正在尝试通过 RS232 串口从我的 Linux 盒子连接到微控制器。
我已经编写了驱动程序并实现了 b/n pc 和微控制器协议,它使用内核中已经存在的 tty(/dev/ttyS0) 设备作为模块(例如通过调用 open、close 等)。但是,当我尝试编译时,它说找不到打开、写入、读取等的引用...
我如何从驱动程序中使用现有的设备驱动程序?我还需要添加其他内容吗?
如果没有,我如何从驱动程序中轻松使用串行端口?
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/cdev.h>
#include <linux/spinlock.h>
#include <linux/termios.h>
#include <linux/fcntl.h>
#include <linux/unistd.h>
I am trying to interface to a microcontroller from my linux box via RS232 serial.
I have written the driver and implemented a protocol b/n pc and microcontroller, which uses a tty(/dev/ttyS0) device already present in the kernel as a module(eg via calling open, close, etc..). However, when I try to compile, it says it cannot find reference to open, write, read etc...
How do I just use an existing device driver from within a driver? Is there something else I need to include?
If not, how can I use the serial port easily from within a driver?
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/cdev.h>
#include <linux/spinlock.h>
#include <linux/termios.h>
#include <linux/fcntl.h>
#include <linux/unistd.h>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通常你应该在用户空间中做这样的事情 - 在普通的用户空间程序中实现你的设备的协议。
可以,但绝对不建议在内核中执行这些操作。例如,ppp 驱动程序在串行驱动程序之上实现网络驱动程序。我不知道在这种情况下它是如何工作的,但我希望用户空间帮助程序打开设备,初始化其参数等,然后使用某些系统调用将文件描述符传递到内核中。
您不能从内核调用任意库函数 - 或者实际上根本不能调用任何库函数(除了实际作为内核一部分提供的库之外)。这包括内核系统调用。有可以调用的等效函数 - 例如 filp_open。
在大多数情况下,您不能只从内核调用正常的系统调用,因为它们期望指针指向用户空间数据,但在内核中,您的指针(通过 kalloc 等分配)通常会指向内核空间数据。两者不能随意混合。
Normally you should do such a thing in userspace - implement your device's protocol in a normal, userspace program.
It is possible, but definitely not recommended to do these things in the kernel. For example, the ppp driver implements a network driver on top of a serial driver. I don't know how it works in that case, but I'd expect that a userspace helper program opens the device, initialises its parameters etc, then passes the file descriptor into the kernel using some system call.
You cannot call arbitrary library functions from the kernel - or indeed, any library functions at all (except libraries which are actually shipped as part of the kernel). This includes kernel system calls. There are equivalent functions which it may be possible to call - for example, filp_open.
In most cases you can't just call the normal syscall from the kernel, as they expect pointers to point to userspace data, but in the kernel yours (allocated via kalloc etc) will normally point to kernel-space data. The two can't be freely mixed.