From: Tulio A M Mendes Date: Thu, 12 Feb 2026 07:11:59 +0000 (-0300) Subject: refactor: add ioctl/mmap callbacks to fs_node_t, decouple ioctl dispatch X-Git-Url: https://projects.tadryanom.me/?a=commitdiff_plain;h=b8667d1f8bcbf0084d1ae4f5f73f9302a1de94c9;p=AdrOS.git refactor: add ioctl/mmap callbacks to fs_node_t, decouple ioctl dispatch Added ioctl and mmap function pointers to fs_node_t for generic device dispatch. Refactored syscall_ioctl_impl to call node->ioctl instead of hardcoding TTY/PTY dispatch by inode number. - tty.c: added tty_devfs_ioctl wrapper, set on console/tty nodes - pty.c: added pty_slave_ioctl_fn wrapper, set on all slave nodes - syscall.c: ioctl now dispatches generically through node callback This prepares the VFS for /dev/fb0 and other devices that need ioctl and mmap support. --- diff --git a/include/fs.h b/include/fs.h index b6f3c9e..96fe567 100644 --- a/include/fs.h +++ b/include/fs.h @@ -27,6 +27,8 @@ typedef struct fs_node { void (*close)(struct fs_node* node); struct fs_node* (*finddir)(struct fs_node* node, const char* name); int (*readdir)(struct fs_node* node, uint32_t* inout_index, void* buf, uint32_t buf_len); + int (*ioctl)(struct fs_node* node, uint32_t cmd, void* arg); + uintptr_t (*mmap)(struct fs_node* node, uintptr_t addr, uint32_t length, uint32_t prot, uint32_t offset); } fs_node_t; struct vfs_dirent { diff --git a/src/kernel/pty.c b/src/kernel/pty.c index 52282f9..b8e661c 100644 --- a/src/kernel/pty.c +++ b/src/kernel/pty.c @@ -78,6 +78,7 @@ static uint32_t pty_master_read_fn(fs_node_t* node, uint32_t offset, uint32_t si static uint32_t pty_master_write_fn(fs_node_t* node, uint32_t offset, uint32_t size, const uint8_t* buffer); static uint32_t pty_slave_read_fn(fs_node_t* node, uint32_t offset, uint32_t size, uint8_t* buffer); static uint32_t pty_slave_write_fn(fs_node_t* node, uint32_t offset, uint32_t size, const uint8_t* buffer); +static int pty_slave_ioctl_fn(fs_node_t* node, uint32_t cmd, void* arg); static void pty_init_pair(int idx) { struct pty_pair* p = &g_ptys[idx]; @@ -100,6 +101,7 @@ static void pty_init_pair(int idx) { p->slave_node.inode = PTY_SLAVE_INO_BASE + (uint32_t)idx; p->slave_node.read = &pty_slave_read_fn; p->slave_node.write = &pty_slave_write_fn; + p->slave_node.ioctl = &pty_slave_ioctl_fn; } /* --- DevFS pts directory callbacks --- */ @@ -499,6 +501,12 @@ static uint32_t pty_slave_write_fn(fs_node_t* node, uint32_t offset, uint32_t si return (uint32_t)rc; } +static int pty_slave_ioctl_fn(fs_node_t* node, uint32_t cmd, void* arg) { + int idx = pty_ino_to_idx(node->inode); + if (idx < 0) return -ENODEV; + return pty_slave_ioctl_idx(idx, cmd, arg); +} + int pty_master_can_read(void) { return pty_master_can_read_idx(0); } int pty_master_can_write(void) { return pty_master_can_write_idx(0); } int pty_slave_can_read(void) { return pty_slave_can_read_idx(0); } diff --git a/src/kernel/syscall.c b/src/kernel/syscall.c index b876c4e..d73841c 100644 --- a/src/kernel/syscall.c +++ b/src/kernel/syscall.c @@ -1346,10 +1346,7 @@ static int syscall_ioctl_impl(int fd, uint32_t cmd, void* user_arg) { if (!f || !f->node) return -EBADF; fs_node_t* n = f->node; - if (n->flags != FS_CHARDEVICE) return -ENOTTY; - if (n->inode == 3) return tty_ioctl(cmd, user_arg); - if (pty_is_slave_ino(n->inode)) return pty_slave_ioctl_idx(pty_ino_to_idx(n->inode), cmd, user_arg); - if (pty_is_master_ino(n->inode)) return -ENOTTY; + if (n->ioctl) return n->ioctl(n, cmd, user_arg); return -ENOTTY; } diff --git a/src/kernel/tty.c b/src/kernel/tty.c index 6a344f2..5f74039 100644 --- a/src/kernel/tty.c +++ b/src/kernel/tty.c @@ -395,6 +395,11 @@ static uint32_t tty_devfs_write(fs_node_t* node, uint32_t offset, uint32_t size, return (uint32_t)rc; } +static int tty_devfs_ioctl(fs_node_t* node, uint32_t cmd, void* arg) { + (void)node; + return tty_ioctl(cmd, arg); +} + void tty_init(void) { spinlock_init(&tty_lock); line_len = 0; @@ -412,6 +417,7 @@ void tty_init(void) { g_dev_console_node.inode = 10; g_dev_console_node.read = &tty_devfs_read; g_dev_console_node.write = &tty_devfs_write; + g_dev_console_node.ioctl = &tty_devfs_ioctl; devfs_register_device(&g_dev_console_node); /* Register /dev/tty */ @@ -421,6 +427,7 @@ void tty_init(void) { g_dev_tty_node.inode = 3; g_dev_tty_node.read = &tty_devfs_read; g_dev_tty_node.write = &tty_devfs_write; + g_dev_tty_node.ioctl = &tty_devfs_ioctl; devfs_register_device(&g_dev_tty_node); }