I'm trying to write simple kernel module that uses a device connected to the system via serial line, but I'm having trouble actually accessing the UART. I think I know how to communicate with the device with kernel_read
and kernel_write
:
struct file* serial_file;
char buf[32];
lsoff_t off = 0;
serial_file = filp_open("/dev/ttyS0", O_RDWR, 0);
kernel_write(serial_file, "test
", 5, &off);
kernel_read(serial_file, buf, 32, &off);
The file seems to be open (IS_ERR(serial_file)
is 0), but reads and writes return EIO - probably because of the wrong baudrate. Since there is no kernel_ioctl, there's no obvious way to fix this.
Answer to the problem above would be nice, but since this is more of a hack (from my understanding filesystem shouldn't really be accessed from kernel in this fashion), even nicer would be explanation how to do this properly? I'd guess it would involve calling functions from tty_struct
directly, but how to get it from file
(or from path, or from whatever it should be generated from)?
question from:
https://stackoverflow.com/questions/65910334/how-to-access-uart-from-linux-kernel 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…