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 <klibc/strfmt.h>
12 #include <klibc/string.h>
14 static DEFINE_LLIST(root_list);
16 static volatile u32_t devid = 0;
18 struct devclass default_devclass = {};
21 device_setname_vargs(struct device_meta* dev, char* fmt, va_list args)
23 size_t strlen = ksnprintfv(dev->name_val, fmt, DEVICE_NAME_SIZE, args);
25 dev->name = HSTR(dev->name_val, strlen);
27 hstr_rehash(&dev->name, HSTR_FULL_HASH);
31 device_register_generic(struct device_meta* devm, struct devclass* class, char* fmt, ...)
37 device_setname_vargs(devm, fmt, args);
40 if (class && valid_device_subtype_ref(devm, DEV_STRUCT)) {
41 struct device* dev = to_dev(devm);
42 dev->ident = (struct devident){ .fn_grp = class->fn_grp,
43 .unique = DEV_UNIQUE(class->device,
47 devm->dev_uid = devid++;
49 struct device_meta* parent = devm->parent;
51 assert(valid_device_subtype_ref(parent, DEV_CAT));
52 llist_append(&parent->children, &devm->siblings);
54 llist_append(&root_list, &devm->siblings);
61 device_init_meta(struct device_meta* dmeta, struct device_meta* parent, unsigned int subtype)
63 dmeta->magic = DEV_STRUCT_MAGIC_MASK | subtype;
64 dmeta->parent = parent;
66 llist_init_head(&dmeta->children);
70 device_create(struct device* dev,
71 struct device_meta* parent,
75 dev->magic = DEV_STRUCT_MAGIC;
76 dev->underlay = underlay;
79 device_init_meta(dev_meta(dev), parent, DEV_STRUCT);
80 mutex_init(&dev->lock);
81 iopoll_init_evt_q(&dev->pollers);
85 device_alloc(struct device_meta* parent, u32_t type, void* underlay)
87 struct device* dev = vzalloc(sizeof(struct device));
93 device_create(dev, parent, type, underlay);
99 device_alloc_alias(struct device_meta* parent, struct device_meta* aliased)
101 struct device_alias* dev = vzalloc(sizeof(struct device_alias));
107 device_init_meta(dev_meta(dev), parent, DEV_ALIAS);
108 dev->alias = aliased;
114 device_alloc_cat(struct device_meta* parent)
116 struct device_cat* dev = vzalloc(sizeof(struct device_cat));
122 device_init_meta(dev_meta(dev), parent, DEV_CAT);
129 device_setname(struct device_meta* dev, char* fmt, ...)
134 device_setname_vargs(dev, fmt, args);
140 device_addcat(struct device_meta* parent, char* name_fmt, ...)
143 va_start(args, name_fmt);
145 struct device_cat* dev = device_alloc_cat(parent);
147 device_setname_vargs(dev_meta(dev), name_fmt, args);
148 device_register_generic(dev_meta(dev), NULL, NULL);
155 device_addalias(struct device_meta* parent, struct device_meta* aliased, char* name_fmt, ...)
158 va_start(args, name_fmt);
160 struct device_alias* dev = device_alloc_alias(parent, aliased);
162 device_setname_vargs(dev_meta(dev), name_fmt, args);
163 device_register_generic(dev_meta(dev), NULL, NULL);
170 device_getbyid(struct llist_header* devlist, u32_t id)
172 devlist = devlist ? devlist : &root_list;
173 struct device_meta *pos, *n;
174 llist_for_each(pos, n, devlist, siblings)
176 if (pos->dev_uid == id) {
185 device_getbyhname(struct device_meta* root_dev, struct hstr* name)
187 struct llist_header* devlist = root_dev ? &root_dev->children : &root_list;
188 struct device_meta *pos, *n;
189 llist_for_each(pos, n, devlist, siblings)
191 if (HSTR_EQ(&pos->name, name)) {
200 device_getbyname(struct device_meta* root_dev, const char* name, size_t len)
202 struct hstr hname = HSTR(name, len);
203 hstr_rehash(&hname, HSTR_FULL_HASH);
205 return device_getbyhname(root_dev, &hname);
209 device_remove(struct device_meta* dev)
211 llist_delete(&dev->siblings);
216 device_getbyoffset(struct device_meta* root_dev, int offset)
218 struct llist_header* devlist = root_dev ? &root_dev->children : &root_list;
219 struct device_meta *pos, *n;
221 llist_for_each(pos, n, devlist, siblings)
223 if (off++ >= offset) {
231 device_populate_info(struct device* dev, struct dev_info* devinfo)
233 devinfo->dev_id.group = dev->ident.fn_grp;
234 devinfo->dev_id.unique = dev->ident.unique;
236 if (!devinfo->dev_name.buf) {
240 struct device_def* def = devdef_byident(&dev->ident);
241 size_t buflen = devinfo->dev_name.buf_len;
243 strncpy(devinfo->dev_name.buf, def->name, buflen);
244 devinfo->dev_name.buf[buflen - 1] = 0;
248 resolve_device_meta(void* maybe_dev) {
249 if (!valid_device_ref(maybe_dev)) {
253 struct device_meta* dm = (struct device_meta*)maybe_dev;
254 unsigned int subtype = dm->magic ^ DEV_STRUCT_MAGIC_MASK;
263 struct device_meta* aliased_dm = dm;
265 while(valid_device_subtype_ref(aliased_dm, DEV_ALIAS)) {
266 aliased_dm = to_aliasdev(aliased_dm)->alias;
277 resolve_device(void* maybe_dev) {
278 struct device_meta* dm = resolve_device_meta(maybe_dev);
280 if (!valid_device_subtype_ref(dm, DEV_STRUCT)) {
288 device_alert_poller(struct device* dev, int poll_evt)
290 dev->poll_evflags = poll_evt;
291 iopoll_wake_pollers(&dev->pollers);
294 __DEFINE_LXSYSCALL3(int, ioctl, int, fd, int, req, va_list, args)
298 if ((errno &= vfs_getfd(fd, &fd_s))) {
302 struct device* dev = (struct device*)fd_s->file->inode->data;
303 if (valid_device_subtype_ref(dev, DEV_STRUCT)) {
308 if (req == DEVIOIDENT) {
309 struct dev_info* devinfo = va_arg(args, struct dev_info*);
310 device_populate_info(dev, devinfo);
314 if (!dev->ops.exec_cmd) {
319 errno &= dev->ops.exec_cmd(dev, req, args);
322 return DO_STATUS_OR_RETURN(errno);