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_prepare(struct device* dev)
18 dev->magic = DEV_STRUCT_MAGIC;
19 dev->dev_id = devid++;
21 llist_init_head(&dev->children);
25 device_setname_vargs(struct device* dev, char* fmt, va_list args)
28 __ksprintf_internal(dev->name_val, fmt, DEVICE_NAME_SIZE, args);
30 dev->name = HSTR(dev->name_val, strlen);
32 hstr_rehash(&dev->name, HSTR_FULL_HASH);
36 device_setname(struct device* dev, char* fmt, ...)
41 device_setname_vargs(dev, fmt, args);
47 device_add_vargs(struct device* parent,
53 struct device* dev = vzalloc(sizeof(struct device));
58 assert((parent->dev_type & DEV_MSKIF) == DEV_IFCAT);
59 llist_append(&parent->children, &dev->siblings);
61 llist_append(&root_list, &dev->siblings);
65 device_setname_vargs(dev, name_fmt, args);
69 dev->underlay = underlay;
76 device_add(struct device* parent,
83 va_start(args, name_fmt);
86 device_add_vargs(parent, underlay, name_fmt, type, args);
93 device_addsys(struct device* parent, void* underlay, char* name_fmt, ...)
96 va_start(args, name_fmt);
99 device_add_vargs(parent, underlay, name_fmt, DEV_IFSEQ, args);
106 device_addseq(struct device* parent, void* underlay, char* name_fmt, ...)
109 va_start(args, name_fmt);
112 device_add_vargs(parent, underlay, name_fmt, DEV_IFSEQ, args);
119 device_addvol(struct device* parent, void* underlay, char* name_fmt, ...)
122 va_start(args, name_fmt);
125 device_add_vargs(parent, underlay, name_fmt, DEV_IFVOL, args);
132 device_addcat(struct device* parent, char* name_fmt, ...)
135 va_start(args, name_fmt);
138 device_add_vargs(parent, NULL, name_fmt, DEV_IFCAT, args);
145 device_getbyid(struct llist_header* devlist, dev_t id)
147 devlist = devlist ? devlist : &root_list;
148 struct device *pos, *n;
149 llist_for_each(pos, n, devlist, siblings)
151 if (pos->dev_id == id) {
160 device_getbyhname(struct device* root_dev, struct hstr* name)
162 struct llist_header* devlist = root_dev ? &root_dev->children : &root_list;
163 struct device *pos, *n;
164 llist_for_each(pos, n, devlist, siblings)
166 if (HSTR_EQ(&pos->name, name)) {
175 device_getbyname(struct device* root_dev, const char* name, size_t len)
177 struct hstr hname = HSTR(name, len);
178 hstr_rehash(&hname, HSTR_FULL_HASH);
180 return device_getbyhname(root_dev, &hname);
184 device_remove(struct device* dev)
186 llist_delete(&dev->siblings);
191 device_getbyoffset(struct device* root_dev, int offset)
193 struct llist_header* devlist = root_dev ? &root_dev->children : &root_list;
194 struct device *pos, *n;
196 llist_for_each(pos, n, devlist, siblings)
198 if (off++ >= offset) {
205 __DEFINE_LXSYSCALL3(int, ioctl, int, fd, int, req, va_list, args)
209 if ((errno = vfs_getfd(fd, &fd_s))) {
213 struct device* dev = (struct device*)fd_s->file->inode->data;
214 if (dev->magic != DEV_STRUCT_MAGIC) {
219 if (!dev->ops.exec_cmd) {
224 errno = dev->ops.exec_cmd(dev, req, args);
227 return DO_STATUS_OR_RETURN(errno);