feat: kprintf now goes into dedicated pseudo-dev rather than flooding the framebuffer
[lunaix-os.git] / lunaix-os / kernel / device / device.c
1
2 #include <lunaix/device.h>
3 #include <lunaix/fs.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>
10
11 #include <klibc/strfmt.h>
12 #include <klibc/string.h>
13
14 static DEFINE_LLIST(root_list);
15
16 static volatile u32_t devid = 0;
17
18 struct devclass default_devclass = {};
19
20 void
21 device_setname_vargs(struct device* dev, char* fmt, va_list args)
22 {
23     size_t strlen =
24       ksnprintfv(dev->name_val, fmt, DEVICE_NAME_SIZE, args);
25
26     dev->name = HSTR(dev->name_val, strlen);
27
28     hstr_rehash(&dev->name, HSTR_FULL_HASH);
29 }
30
31 void
32 device_register(struct device* dev, struct devclass* class, char* fmt, ...)
33 {
34     va_list args;
35     va_start(args, fmt);
36
37     if (fmt) {
38         device_setname_vargs(dev, fmt, args);
39     }
40
41     if (class) {
42         dev->ident = (struct devident){ .fn_grp = class->fn_grp,
43                                         .unique = DEV_UNIQUE(class->device,
44                                                              class->variant) };
45     }
46
47     dev->dev_uid = devid++;
48
49     struct device* parent = dev->parent;
50     if (parent) {
51         assert((parent->dev_type & DEV_MSKIF) == DEV_IFCAT);
52         llist_append(&parent->children, &dev->siblings);
53     } else {
54         llist_append(&root_list, &dev->siblings);
55     }
56
57     va_end(args);
58 }
59
60 void
61 device_create(struct device* dev,
62               struct device* parent,
63               u32_t type,
64               void* underlay)
65 {
66     dev->magic = DEV_STRUCT_MAGIC;
67     dev->underlay = underlay;
68     dev->dev_type = type;
69     dev->parent = parent;
70
71     llist_init_head(&dev->children);
72     mutex_init(&dev->lock);
73 }
74
75 struct device*
76 device_alloc(struct device* parent, u32_t type, void* underlay)
77 {
78     struct device* dev = vzalloc(sizeof(struct device));
79
80     if (!dev) {
81         return NULL;
82     }
83
84     device_create(dev, parent, type, underlay);
85
86     return dev;
87 }
88
89 void
90 device_setname(struct device* dev, char* fmt, ...)
91 {
92     va_list args;
93     va_start(args, fmt);
94
95     device_setname_vargs(dev, fmt, args);
96
97     va_end(args);
98 }
99
100 struct device*
101 device_addcat(struct device* parent, char* name_fmt, ...)
102 {
103     va_list args;
104     va_start(args, name_fmt);
105
106     struct device* dev = device_alloc(parent, DEV_IFCAT, NULL);
107
108     device_setname_vargs(dev, name_fmt, args);
109     device_register(dev, NULL, NULL);
110
111     va_end(args);
112     return dev;
113 }
114
115 struct device*
116 device_getbyid(struct llist_header* devlist, u32_t id)
117 {
118     devlist = devlist ? devlist : &root_list;
119     struct device *pos, *n;
120     llist_for_each(pos, n, devlist, siblings)
121     {
122         if (pos->dev_uid == id) {
123             return pos;
124         }
125     }
126
127     return NULL;
128 }
129
130 struct device*
131 device_getbyhname(struct device* root_dev, struct hstr* name)
132 {
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)
136     {
137         if (HSTR_EQ(&pos->name, name)) {
138             return pos;
139         }
140     }
141
142     return NULL;
143 }
144
145 struct device*
146 device_getbyname(struct device* root_dev, const char* name, size_t len)
147 {
148     struct hstr hname = HSTR(name, len);
149     hstr_rehash(&hname, HSTR_FULL_HASH);
150
151     return device_getbyhname(root_dev, &hname);
152 }
153
154 void
155 device_remove(struct device* dev)
156 {
157     llist_delete(&dev->siblings);
158     vfree(dev);
159 }
160
161 struct device*
162 device_getbyoffset(struct device* root_dev, int offset)
163 {
164     struct llist_header* devlist = root_dev ? &root_dev->children : &root_list;
165     struct device *pos, *n;
166     int off = 0;
167     llist_for_each(pos, n, devlist, siblings)
168     {
169         if (off++ >= offset) {
170             return pos;
171         }
172     }
173     return NULL;
174 }
175
176 void
177 device_populate_info(struct device* dev, struct dev_info* devinfo)
178 {
179     devinfo->dev_id.group = dev->ident.fn_grp;
180     devinfo->dev_id.unique = dev->ident.unique;
181
182     if (!devinfo->dev_name.buf) {
183         return;
184     }
185
186     struct device_def* def = devdef_byident(&dev->ident);
187     size_t buflen = devinfo->dev_name.buf_len;
188
189     strncpy(devinfo->dev_name.buf, def->name, buflen);
190     devinfo->dev_name.buf[buflen - 1] = 0;
191 }
192
193 struct device*
194 device_cast(void* obj)
195 {
196     struct device* dev = (struct device*)obj;
197     if (dev && dev->magic == DEV_STRUCT_MAGIC) {
198         return dev;
199     }
200
201     return NULL;
202 }
203
204 __DEFINE_LXSYSCALL3(int, ioctl, int, fd, int, req, va_list, args)
205 {
206     int errno = -1;
207     struct v_fd* fd_s;
208     if ((errno &= vfs_getfd(fd, &fd_s))) {
209         goto done;
210     }
211
212     struct device* dev = (struct device*)fd_s->file->inode->data;
213     if (dev->magic != DEV_STRUCT_MAGIC) {
214         errno &= ENODEV;
215         goto done;
216     }
217
218     if (req == DEVIOIDENT) {
219         struct dev_info* devinfo = va_arg(args, struct dev_info*);
220         device_populate_info(dev, devinfo);
221         errno = 0;
222     }
223
224     if (!dev->ops.exec_cmd) {
225         errno &= ENOTSUP;
226         goto done;
227     }
228
229     errno &= dev->ops.exec_cmd(dev, req, args);
230
231 done:
232     return DO_STATUS_OR_RETURN(errno);
233 }