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 #include <usr/lunaix/device.h>
13 #include <klibc/stdio.h>
14 #include <klibc/string.h>
16 static DEFINE_LLIST(root_list);
18 static volatile dev_t devid = 0;
21 device_prepare(struct device* dev)
23 dev->magic = DEV_STRUCT_MAGIC;
24 dev->dev_id = devid++;
26 llist_init_head(&dev->children);
30 device_setname_vargs(struct device* dev, char* fmt, va_list args)
33 __ksprintf_internal(dev->name_val, fmt, DEVICE_NAME_SIZE, args);
35 dev->name = HSTR(dev->name_val, strlen);
37 hstr_rehash(&dev->name, HSTR_FULL_HASH);
41 device_setname(struct device* dev, char* fmt, ...)
46 device_setname_vargs(dev, fmt, args);
52 device_add_vargs(struct device* parent,
58 struct device* dev = vzalloc(sizeof(struct device));
63 assert((parent->dev_type & DEV_MSKIF) == DEV_IFCAT);
64 llist_append(&parent->children, &dev->siblings);
66 llist_append(&root_list, &dev->siblings);
70 device_setname_vargs(dev, name_fmt, args);
74 dev->underlay = underlay;
81 device_add(struct device* parent,
88 va_start(args, name_fmt);
91 device_add_vargs(parent, underlay, name_fmt, type, args);
98 device_addsys(struct device* parent, void* underlay, char* name_fmt, ...)
101 va_start(args, name_fmt);
104 device_add_vargs(parent, underlay, name_fmt, DEV_IFSEQ, args);
111 device_addseq(struct device* parent, void* underlay, char* name_fmt, ...)
114 va_start(args, name_fmt);
117 device_add_vargs(parent, underlay, name_fmt, DEV_IFSEQ, args);
124 device_addvol(struct device* parent, void* underlay, char* name_fmt, ...)
127 va_start(args, name_fmt);
130 device_add_vargs(parent, underlay, name_fmt, DEV_IFVOL, args);
137 device_addcat(struct device* parent, char* name_fmt, ...)
140 va_start(args, name_fmt);
143 device_add_vargs(parent, NULL, name_fmt, DEV_IFCAT, args);
150 device_getbyid(struct llist_header* devlist, dev_t id)
152 devlist = devlist ? devlist : &root_list;
153 struct device *pos, *n;
154 llist_for_each(pos, n, devlist, siblings)
156 if (pos->dev_id == id) {
165 device_getbyhname(struct device* root_dev, struct hstr* name)
167 struct llist_header* devlist = root_dev ? &root_dev->children : &root_list;
168 struct device *pos, *n;
169 llist_for_each(pos, n, devlist, siblings)
171 if (HSTR_EQ(&pos->name, name)) {
180 device_getbyname(struct device* root_dev, const char* name, size_t len)
182 struct hstr hname = HSTR(name, len);
183 hstr_rehash(&hname, HSTR_FULL_HASH);
185 return device_getbyhname(root_dev, &hname);
189 device_remove(struct device* dev)
191 llist_delete(&dev->siblings);
196 device_getbyoffset(struct device* root_dev, int offset)
198 struct llist_header* devlist = root_dev ? &root_dev->children : &root_list;
199 struct device *pos, *n;
201 llist_for_each(pos, n, devlist, siblings)
203 if (off++ >= offset) {
211 device_populate_info(struct device* dev, struct dev_info* devinfo)
213 devinfo->dev_id.meta = dev->class.meta;
214 devinfo->dev_id.device = dev->class.device;
215 devinfo->dev_id.variant = dev->class.variant;
217 if (!devinfo->dev_name.buf) {
221 struct device_def* def = devdef_byclass(&dev->class);
222 size_t buflen = devinfo->dev_name.buf_len;
224 strncpy(devinfo->dev_name.buf, def->name, buflen);
225 devinfo->dev_name.buf[buflen - 1] = 0;
228 __DEFINE_LXSYSCALL3(int, ioctl, int, fd, int, req, va_list, args)
232 if ((errno &= vfs_getfd(fd, &fd_s))) {
236 struct device* dev = (struct device*)fd_s->file->inode->data;
237 if (dev->magic != DEV_STRUCT_MAGIC) {
242 if (req == DEVIOIDENT) {
243 struct dev_info* devinfo = va_arg(args, struct dev_info*);
244 device_populate_info(dev, devinfo);
248 if (!dev->ops.exec_cmd) {
253 errno &= dev->ops.exec_cmd(dev, req, args);
256 return DO_STATUS_OR_RETURN(errno);