1 #include <klibc/stdio.h>
2 #include <lunaix/device.h>
4 #include <lunaix/fs/twifs.h>
5 #include <lunaix/ioctl.h>
6 #include <lunaix/mm/valloc.h>
7 #include <lunaix/spike.h>
8 #include <lunaix/syscall.h>
10 static DEFINE_LLIST(root_list);
12 static volatile dev_t devid = 0;
15 device_add(struct device* parent,
21 struct device* dev = vzalloc(sizeof(struct device));
24 assert((parent->dev_type & DEV_MSKIF) == DEV_IFCAT);
25 llist_append(&parent->children, &dev->siblings);
27 llist_append(&root_list, &dev->siblings);
31 __ksprintf_internal(dev->name_val, name_fmt, DEVICE_NAME_SIZE, args);
33 dev->magic = DEV_STRUCT_MAGIC;
34 dev->dev_id = devid++;
35 dev->name = HSTR(dev->name_val, strlen);
37 dev->underlay = underlay;
40 hstr_rehash(&dev->name, HSTR_FULL_HASH);
41 llist_init_head(&dev->children);
47 device_addseq(struct device* parent, void* underlay, char* name_fmt, ...)
50 va_start(args, name_fmt);
53 device_add(parent, underlay, name_fmt, DEV_IFSEQ, args);
60 device_addvol(struct device* parent, void* underlay, char* name_fmt, ...)
63 va_start(args, name_fmt);
66 device_add(parent, underlay, name_fmt, DEV_IFVOL, args);
73 device_addcat(struct device* parent, char* name_fmt, ...)
76 va_start(args, name_fmt);
78 struct device* dev = device_add(parent, NULL, name_fmt, DEV_IFCAT, args);
85 device_getbyid(struct llist_header* devlist, dev_t id)
87 devlist = devlist ? devlist : &root_list;
88 struct device *pos, *n;
89 llist_for_each(pos, n, devlist, siblings)
91 if (pos->dev_id == id) {
100 device_getbyhname(struct device* root_dev, struct hstr* name)
102 struct llist_header* devlist = root_dev ? &root_dev->children : &root_list;
103 struct device *pos, *n;
104 llist_for_each(pos, n, devlist, siblings)
106 if (HSTR_EQ(&pos->name, name)) {
115 device_getbyname(struct device* root_dev, const char* name, size_t len)
117 struct hstr hname = HSTR(name, len);
118 hstr_rehash(&hname, HSTR_FULL_HASH);
120 return device_getbyhname(root_dev, &hname);
124 device_remove(struct device* dev)
126 llist_delete(&dev->siblings);
131 device_getbyoffset(struct device* root_dev, int offset)
133 struct llist_header* devlist = root_dev ? &root_dev->children : &root_list;
134 struct device *pos, *n;
136 llist_for_each(pos, n, devlist, siblings)
138 if (off++ >= offset) {
145 __DEFINE_LXSYSCALL3(int, ioctl, int, fd, int, req, va_list, args)
149 if ((errno = vfs_getfd(fd, &fd_s))) {
153 struct device* dev = (struct device*)fd_s->file->inode->data;
154 if (dev->magic != DEV_STRUCT_MAGIC) {
159 if (!dev->exec_cmd) {
164 errno = dev->exec_cmd(dev, req, args);
167 return DO_STATUS_OR_RETURN(errno);