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