1 #include <klibc/stdio.h>
2 #include <lunaix/device.h>
3 #include <lunaix/fs/twifs.h>
4 #include <lunaix/mm/valloc.h>
5 #include <lunaix/spike.h>
7 static DEFINE_LLIST(root_list);
9 static volatile dev_t devid = 0;
12 device_add(struct device* parent,
18 struct device* dev = vzalloc(sizeof(struct device));
21 assert((parent->dev_type & DEV_MSKIF) == DEV_IFCAT);
22 llist_append(&parent->children, &dev->siblings);
24 llist_append(&root_list, &dev->siblings);
28 __ksprintf_internal(dev->name_val, name_fmt, DEVICE_NAME_SIZE, args);
30 dev->dev_id = devid++;
31 dev->name = HSTR(dev->name_val, strlen);
33 dev->underlay = underlay;
36 hstr_rehash(&dev->name, HSTR_FULL_HASH);
37 llist_init_head(&dev->children);
43 device_addseq(struct device* parent, void* underlay, char* name_fmt, ...)
46 va_start(args, name_fmt);
49 device_add(parent, underlay, name_fmt, DEV_IFSEQ, args);
56 device_addvol(struct device* parent, void* underlay, char* name_fmt, ...)
59 va_start(args, name_fmt);
62 device_add(parent, underlay, name_fmt, DEV_IFVOL, args);
69 device_addcat(struct device* parent, char* name_fmt, ...)
72 va_start(args, name_fmt);
74 struct device* dev = device_add(parent, NULL, name_fmt, DEV_IFCAT, args);
81 device_getbyid(struct llist_header* devlist, dev_t id)
83 devlist = devlist ? devlist : &root_list;
84 struct device *pos, *n;
85 llist_for_each(pos, n, devlist, siblings)
87 if (pos->dev_id == id) {
96 device_getbyhname(struct llist_header* devlist, struct hstr* name)
98 devlist = devlist ? devlist : &root_list;
99 struct device *pos, *n;
100 llist_for_each(pos, n, devlist, siblings)
102 if (HSTR_EQ(&pos->name, name)) {
111 device_getbyname(struct llist_header* devlist, const char* name, size_t len)
113 struct hstr hname = HSTR(name, len);
114 hstr_rehash(&hname, HSTR_FULL_HASH);
116 return device_getbyhname(devlist, &hname);
120 device_remove(struct device* dev)
122 llist_delete(&dev->siblings);
127 device_getbyoffset(struct llist_header* devlist, int offset)
129 devlist = devlist ? devlist : &root_list;
130 struct device *pos, *n;
132 llist_for_each(pos, n, devlist, siblings)
134 if (off++ >= offset) {