]> Projects (at) Tadryanom (dot) Me - AdrOS.git/commitdiff
refactor: add ioctl/mmap callbacks to fs_node_t, decouple ioctl dispatch
authorTulio A M Mendes <[email protected]>
Thu, 12 Feb 2026 07:11:59 +0000 (04:11 -0300)
committerTulio A M Mendes <[email protected]>
Fri, 13 Feb 2026 02:44:55 +0000 (23:44 -0300)
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.

include/fs.h
src/kernel/pty.c
src/kernel/syscall.c
src/kernel/tty.c

index 15ddd82e24f00d7e4adbc9798461785b346dc5a9..56d962ece1a170554d376067fbd5f91a955fbc1a 100644 (file)
@@ -36,6 +36,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 {
index 5bd3c4a88c115cb7e9f72043be5cf0bc56c2ecd4..0e9fb1c29c610c172ac78e5ed2f28c86570c3f91 100644 (file)
@@ -87,6 +87,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];
@@ -109,6 +110,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 --- */
@@ -508,6 +510,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); }
index bb27741ce3a623038eb23c89c26235675849b4c6..69e54969b3e5e536053d48819753f28f8e68a5f1 100644 (file)
@@ -1355,10 +1355,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;
 }
 
index 73825666b17a643709a0b99de7f5ca2a1d3af504..237a3c64505153c2512b4dc292e775cee10f02ac 100644 (file)
@@ -404,6 +404,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;
@@ -421,6 +426,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 */
@@ -430,6 +436,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);
 }