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 llist_init_head(&dev->capabilities);
81 mutex_init(&dev->lock);
82 iopoll_init_evt_q(&dev->pollers);
86 device_alloc(struct device_meta* parent, u32_t type, void* underlay)
88 struct device* dev = vzalloc(sizeof(struct device));
94 device_create(dev, parent, type, underlay);
100 device_alloc_alias(struct device_meta* parent, struct device_meta* aliased)
102 struct device_alias* dev = vzalloc(sizeof(struct device_alias));
108 device_init_meta(dev_meta(dev), parent, DEV_ALIAS);
109 dev->alias = aliased;
115 device_alloc_cat(struct device_meta* parent)
117 struct device_cat* dev = vzalloc(sizeof(struct device_cat));
123 device_init_meta(dev_meta(dev), parent, DEV_CAT);
130 device_setname(struct device_meta* dev, char* fmt, ...)
135 device_setname_vargs(dev, fmt, args);
141 device_addcat(struct device_meta* parent, char* name_fmt, ...)
144 va_start(args, name_fmt);
146 struct device_cat* dev = device_alloc_cat(parent);
148 device_setname_vargs(dev_meta(dev), name_fmt, args);
149 device_register_generic(dev_meta(dev), NULL, NULL);
156 device_addalias(struct device_meta* parent, struct device_meta* aliased, char* name_fmt, ...)
159 va_start(args, name_fmt);
161 struct device_alias* dev = device_alloc_alias(parent, aliased);
163 device_setname_vargs(dev_meta(dev), name_fmt, args);
164 device_register_generic(dev_meta(dev), NULL, NULL);
171 device_getbyid(struct llist_header* devlist, u32_t id)
173 devlist = devlist ? devlist : &root_list;
174 struct device_meta *pos, *n;
175 llist_for_each(pos, n, devlist, siblings)
177 if (pos->dev_uid == id) {
186 device_getbyhname(struct device_meta* root_dev, struct hstr* name)
188 struct llist_header* devlist = root_dev ? &root_dev->children : &root_list;
189 struct device_meta *pos, *n;
190 llist_for_each(pos, n, devlist, siblings)
192 if (HSTR_EQ(&pos->name, name)) {
201 device_getbyname(struct device_meta* root_dev, const char* name, size_t len)
203 struct hstr hname = HSTR(name, len);
204 hstr_rehash(&hname, HSTR_FULL_HASH);
206 return device_getbyhname(root_dev, &hname);
210 device_remove(struct device_meta* dev)
212 llist_delete(&dev->siblings);
217 device_getbyoffset(struct device_meta* root_dev, int offset)
219 struct llist_header* devlist = root_dev ? &root_dev->children : &root_list;
220 struct device_meta *pos, *n;
222 llist_for_each(pos, n, devlist, siblings)
224 if (off++ >= offset) {
232 device_populate_info(struct device* dev, struct dev_info* devinfo)
234 devinfo->dev_id.group = dev->ident.fn_grp;
235 devinfo->dev_id.unique = dev->ident.unique;
237 if (!devinfo->dev_name.buf) {
241 struct device_def* def = devdef_byident(&dev->ident);
242 size_t buflen = devinfo->dev_name.buf_len;
244 strncpy(devinfo->dev_name.buf, def->name, buflen);
245 devinfo->dev_name.buf[buflen - 1] = 0;
249 resolve_device_meta(void* maybe_dev) {
250 if (!valid_device_ref(maybe_dev)) {
254 struct device_meta* dm = (struct device_meta*)maybe_dev;
255 unsigned int subtype = dm->magic ^ DEV_STRUCT_MAGIC_MASK;
264 struct device_meta* aliased_dm = dm;
266 while(valid_device_subtype_ref(aliased_dm, DEV_ALIAS)) {
267 aliased_dm = to_aliasdev(aliased_dm)->alias;
278 resolve_device(void* maybe_dev) {
279 struct device_meta* dm = resolve_device_meta(maybe_dev);
281 if (!valid_device_subtype_ref(dm, DEV_STRUCT)) {
289 device_alert_poller(struct device* dev, int poll_evt)
291 dev->poll_evflags = poll_evt;
292 iopoll_wake_pollers(&dev->pollers);
295 __DEFINE_LXSYSCALL3(int, ioctl, int, fd, int, req, sc_va_list, _args)
301 convert_valist(&args, _args);
303 if ((errno &= vfs_getfd(fd, &fd_s))) {
307 struct device* dev = resolve_device(fd_s->file->inode->data);
308 if (!valid_device_subtype_ref(dev, DEV_STRUCT)) {
313 if (req == DEVIOIDENT) {
314 struct dev_info* devinfo = va_arg(args, struct dev_info*);
315 device_populate_info(dev, devinfo);
319 if (!dev->ops.exec_cmd) {
324 errno = dev->ops.exec_cmd(dev, req, args);
327 return DO_STATUS_OR_RETURN(errno);