martinezjavier / ldd3

Linux Device Drivers 3 examples updated to work in recent kernels
http://examples.oreilly.com/9780596005900/
Other
2.38k stars 914 forks source link

Modify the code to support Linux 6.x #86

Open veidongray opened 1 year ago

veidongray commented 1 year ago

In tiny_tty.c: alloc_tty_driver() and put_tty_driver() has been deleted in Linux 5.15. See https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?id=56ec5880a28eae0f508e88e9e80d2e82a471c9be and https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?id=9f90a4ddef4e4d3aa4229f6b117d4e57231457b3

const struct tty_operations::set_ktermios(struct tty_struct *, struct ktermios *) ==> const struct tty_operations::set_ktermios(struct tty_struct *, const struct ktermios *) See https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/diff/include/linux/tty_driver.h?id=a8c11c1520347be74b02312d10ef686b01b525f1

In tiny_serial.c: struct uart_ops::set_termios(struct uart_port *, struct ktermios *, const struct ktermios *) ==> struct uart_ops::set_termios(struct uart_port *, struct ktermios *, const struct ktermios *) See https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/diff/include/linux/serial_core.h?id=bec5b814d46c2a704c3c8148752e62a33e9fa6dc

cweickhmann commented 6 months ago

It seems, the code has to be changed again around kernel version 6.6 as the tty_operations.write pointer type has changed (as well as some arguments):

#if (LINUX_VERSION_CODE < KERNEL_VERSION(6, 6, 0))
static int tiny_write(struct tty_struct *tty,
              const unsigned char *buffer, int count)
#else
static ssize_t tiny_write(struct tty_struct *tty, const u8 *buffer, size_t count)
#endif

Within the body of the function, change int i to ssize_t i.

This makes it compile. However, both on a 6.8.0 as well as on a 6.5.0 kernel, I get a device or resource busy error (-16, EBUSY) when trying to invoke tty_register_driver in tiny_init, i.e. upon insmod.

Playing with the major number does not help (apparently, on Ubuntu /dev/hidraw0 is on major 240). I've even tried implementing dynamic major,minor allocation to no avail.

Has anyone else observed this issue?

cweickhmann commented 6 months ago

There's news here. I found a couple of commas instead of line-end semicolons in lines 522-525 of the original code. Not sure, but this may have caused the issue with tty_register_driver.

522 tiny_tty_driver->major = TINY_TTY_MAJOR,
523 tiny_tty_driver->type = TTY_DRIVER_TYPE_SERIAL,
524 tiny_tty_driver->subtype = SERIAL_TYPE_NORMAL,
525 tiny_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV,

Also, looking at one of the shorter modules in the source tree goldfish.c, I figured, that a bunch of those settings may not be necessary or even not working anymore.

// tiny_tty_driver->owner = THIS_MODULE; // not needed?
// tiny_tty_driver->major = TINY_TTY_MAJOR; // not needed?
// tiny_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV; // not needed? Flags are set at `tty_alloc_driver` now?
// tiny_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL; // not needed? Goldfish doesn't set it.

I'll fiddle a bit more and post my version of the driver once it's working...