1 #include <klibc/stdio.h>
2 #include <lunaix/device.h>
3 #include <lunaix/fs/twifs.h>
4 #include <lunaix/mm/valloc.h>
6 struct llist_header dev_list;
8 static struct twifs_node* dev_root;
11 __dev_read(struct v_file* file, void* buffer, size_t len, size_t fpos);
14 __dev_write(struct v_file* file, void* buffer, size_t len, size_t fpos);
19 dev_root = twifs_toplevel_node("dev", 3, 0);
21 llist_init_head(&dev_list);
25 __device_add(struct device* parent,
31 struct device* dev = vzalloc(sizeof(struct device));
34 __sprintf_internal(dev->name_val, name_fmt, DEVICE_NAME_SIZE, args);
36 dev->name = HSTR(dev->name_val, strlen);
38 dev->underlay = underlay;
40 hstr_rehash(&dev->name, HSTR_FULL_HASH);
41 llist_append(&dev_list, &dev->dev_list);
43 struct twifs_node* dev_node =
44 twifs_file_node(dev_root, dev->name_val, strlen, type);
46 dev_node->ops.read = __dev_read;
47 dev_node->ops.write = __dev_write;
49 dev->fs_node = dev_node;
55 device_addseq(struct device* parent, void* underlay, char* name_fmt, ...)
58 va_start(args, name_fmt);
61 __device_add(parent, underlay, name_fmt, VFS_IFSEQDEV, args);
68 device_addvol(struct device* parent, void* underlay, char* name_fmt, ...)
71 va_start(args, name_fmt);
74 __device_add(parent, underlay, name_fmt, VFS_IFVOLDEV, args);
81 __dev_read(struct v_file* file, void* buffer, size_t len, size_t fpos)
83 struct twifs_node* dev_node = (struct twifs_node*)file->inode->data;
84 struct device* dev = (struct device*)dev_node->data;
89 return dev->read(dev, buffer, fpos, len);
93 __dev_write(struct v_file* file, void* buffer, size_t len, size_t fpos)
95 struct twifs_node* dev_node = (struct twifs_node*)file->inode->data;
96 struct device* dev = (struct device*)dev_node->data;
101 return dev->write(dev, buffer, fpos, len);
105 device_remove(struct device* dev)
107 llist_delete(&dev->dev_list);
108 twifs_rm_node((struct twifs_node*)dev->fs_node);