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>
9 #include <lunaix/syscall_utils.h>
11 static DEFINE_LLIST(root_list);
13 static volatile dev_t devid = 0;
16 device_add(struct device* parent,
22 struct device* dev = vzalloc(sizeof(struct device));
25 assert((parent->dev_type & DEV_MSKIF) == DEV_IFCAT);
26 llist_append(&parent->children, &dev->siblings);
28 llist_append(&root_list, &dev->siblings);
32 __ksprintf_internal(dev->name_val, name_fmt, DEVICE_NAME_SIZE, args);
34 dev->magic = DEV_STRUCT_MAGIC;
35 dev->dev_id = devid++;
36 dev->name = HSTR(dev->name_val, strlen);
38 dev->underlay = underlay;
41 hstr_rehash(&dev->name, HSTR_FULL_HASH);
42 llist_init_head(&dev->children);
48 device_addseq(struct device* parent, void* underlay, char* name_fmt, ...)
51 va_start(args, name_fmt);
54 device_add(parent, underlay, name_fmt, DEV_IFSEQ, args);
61 device_addvol(struct device* parent, void* underlay, char* name_fmt, ...)
64 va_start(args, name_fmt);
67 device_add(parent, underlay, name_fmt, DEV_IFVOL, args);
74 device_addcat(struct device* parent, char* name_fmt, ...)
77 va_start(args, name_fmt);
79 struct device* dev = device_add(parent, NULL, name_fmt, DEV_IFCAT, args);
86 device_getbyid(struct llist_header* devlist, dev_t id)
88 devlist = devlist ? devlist : &root_list;
89 struct device *pos, *n;
90 llist_for_each(pos, n, devlist, siblings)
92 if (pos->dev_id == id) {
101 device_getbyhname(struct device* root_dev, struct hstr* name)
103 struct llist_header* devlist = root_dev ? &root_dev->children : &root_list;
104 struct device *pos, *n;
105 llist_for_each(pos, n, devlist, siblings)
107 if (HSTR_EQ(&pos->name, name)) {
116 device_getbyname(struct device* root_dev, const char* name, size_t len)
118 struct hstr hname = HSTR(name, len);
119 hstr_rehash(&hname, HSTR_FULL_HASH);
121 return device_getbyhname(root_dev, &hname);
125 device_remove(struct device* dev)
127 llist_delete(&dev->siblings);
132 device_getbyoffset(struct device* root_dev, int offset)
134 struct llist_header* devlist = root_dev ? &root_dev->children : &root_list;
135 struct device *pos, *n;
137 llist_for_each(pos, n, devlist, siblings)
139 if (off++ >= offset) {
146 __DEFINE_LXSYSCALL3(int, ioctl, int, fd, int, req, va_list, args)
150 if ((errno = vfs_getfd(fd, &fd_s))) {
154 struct device* dev = (struct device*)fd_s->file->inode->data;
155 if (dev->magic != DEV_STRUCT_MAGIC) {
160 if (!dev->exec_cmd) {
165 errno = dev->exec_cmd(dev, req, args);
168 return DO_STATUS_OR_RETURN(errno);