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* 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(struct device* dev, struct devclass* class, char* fmt, ...)
37 device_setname_vargs(dev, fmt, args);
41 dev->ident = (struct devident){ .fn_grp = class->fn_grp,
42 .unique = DEV_UNIQUE(class->device,
46 dev->dev_uid = devid++;
48 struct device* parent = dev->parent;
50 assert((parent->dev_type & DEV_MSKIF) == DEV_IFCAT);
51 llist_append(&parent->children, &dev->siblings);
53 llist_append(&root_list, &dev->siblings);
60 device_create(struct device* dev,
61 struct device* parent,
65 dev->magic = DEV_STRUCT_MAGIC;
66 dev->underlay = underlay;
70 llist_init_head(&dev->children);
71 mutex_init(&dev->lock);
72 iopoll_init_evt_q(&dev->pollers);
76 device_alloc(struct device* parent, u32_t type, void* underlay)
78 struct device* dev = vzalloc(sizeof(struct device));
84 device_create(dev, parent, type, underlay);
90 device_setname(struct device* dev, char* fmt, ...)
95 device_setname_vargs(dev, fmt, args);
101 device_addcat(struct device* parent, char* name_fmt, ...)
104 va_start(args, name_fmt);
106 struct device* dev = device_alloc(parent, DEV_IFCAT, NULL);
108 device_setname_vargs(dev, name_fmt, args);
109 device_register(dev, NULL, NULL);
116 device_getbyid(struct llist_header* devlist, u32_t id)
118 devlist = devlist ? devlist : &root_list;
119 struct device *pos, *n;
120 llist_for_each(pos, n, devlist, siblings)
122 if (pos->dev_uid == id) {
131 device_getbyhname(struct device* root_dev, struct hstr* name)
133 struct llist_header* devlist = root_dev ? &root_dev->children : &root_list;
134 struct device *pos, *n;
135 llist_for_each(pos, n, devlist, siblings)
137 if (HSTR_EQ(&pos->name, name)) {
146 device_getbyname(struct device* root_dev, const char* name, size_t len)
148 struct hstr hname = HSTR(name, len);
149 hstr_rehash(&hname, HSTR_FULL_HASH);
151 return device_getbyhname(root_dev, &hname);
155 device_remove(struct device* dev)
157 llist_delete(&dev->siblings);
162 device_getbyoffset(struct device* root_dev, int offset)
164 struct llist_header* devlist = root_dev ? &root_dev->children : &root_list;
165 struct device *pos, *n;
167 llist_for_each(pos, n, devlist, siblings)
169 if (off++ >= offset) {
177 device_populate_info(struct device* dev, struct dev_info* devinfo)
179 devinfo->dev_id.group = dev->ident.fn_grp;
180 devinfo->dev_id.unique = dev->ident.unique;
182 if (!devinfo->dev_name.buf) {
186 struct device_def* def = devdef_byident(&dev->ident);
187 size_t buflen = devinfo->dev_name.buf_len;
189 strncpy(devinfo->dev_name.buf, def->name, buflen);
190 devinfo->dev_name.buf[buflen - 1] = 0;
194 device_cast(void* obj)
196 struct device* dev = (struct device*)obj;
197 if (dev && dev->magic == DEV_STRUCT_MAGIC) {
205 device_alert_poller(struct device* dev, int poll_evt)
207 dev->poll_evflags = poll_evt;
208 iopoll_wake_pollers(&dev->pollers);
211 __DEFINE_LXSYSCALL3(int, ioctl, int, fd, int, req, va_list, args)
215 if ((errno &= vfs_getfd(fd, &fd_s))) {
219 struct device* dev = (struct device*)fd_s->file->inode->data;
220 if (dev->magic != DEV_STRUCT_MAGIC) {
225 if (req == DEVIOIDENT) {
226 struct dev_info* devinfo = va_arg(args, struct dev_info*);
227 device_populate_info(dev, devinfo);
231 if (!dev->ops.exec_cmd) {
236 errno &= dev->ops.exec_cmd(dev, req, args);
239 return DO_STATUS_OR_RETURN(errno);