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_addsys(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_addseq(struct device* parent, void* underlay, char* name_fmt, ...)
64 va_start(args, name_fmt);
67 device_add(parent, underlay, name_fmt, DEV_IFSEQ, args);
74 device_addvol(struct device* parent, void* underlay, char* name_fmt, ...)
77 va_start(args, name_fmt);
80 device_add(parent, underlay, name_fmt, DEV_IFVOL, args);
87 device_addcat(struct device* parent, char* name_fmt, ...)
90 va_start(args, name_fmt);
92 struct device* dev = device_add(parent, NULL, name_fmt, DEV_IFCAT, args);
99 device_getbyid(struct llist_header* devlist, dev_t id)
101 devlist = devlist ? devlist : &root_list;
102 struct device *pos, *n;
103 llist_for_each(pos, n, devlist, siblings)
105 if (pos->dev_id == id) {
114 device_getbyhname(struct device* root_dev, struct hstr* name)
116 struct llist_header* devlist = root_dev ? &root_dev->children : &root_list;
117 struct device *pos, *n;
118 llist_for_each(pos, n, devlist, siblings)
120 if (HSTR_EQ(&pos->name, name)) {
129 device_getbyname(struct device* root_dev, const char* name, size_t len)
131 struct hstr hname = HSTR(name, len);
132 hstr_rehash(&hname, HSTR_FULL_HASH);
134 return device_getbyhname(root_dev, &hname);
138 device_remove(struct device* dev)
140 llist_delete(&dev->siblings);
145 device_getbyoffset(struct device* root_dev, int offset)
147 struct llist_header* devlist = root_dev ? &root_dev->children : &root_list;
148 struct device *pos, *n;
150 llist_for_each(pos, n, devlist, siblings)
152 if (off++ >= offset) {
159 __DEFINE_LXSYSCALL3(int, ioctl, int, fd, int, req, va_list, args)
163 if ((errno = vfs_getfd(fd, &fd_s))) {
167 struct device* dev = (struct device*)fd_s->file->inode->data;
168 if (dev->magic != DEV_STRUCT_MAGIC) {
173 if (!dev->ops.exec_cmd) {
178 errno = dev->ops.exec_cmd(dev, req, args);
181 return DO_STATUS_OR_RETURN(errno);