fix dependency check logic cause config always disabled
[lunaix-os.git] / lunaix-os / kernel / device / device.c
1 #include <lunaix/device.h>
2 #include <lunaix/fs.h>
3 #include <lunaix/fs/twifs.h>
4 #include <lunaix/ioctl.h>
5 #include <lunaix/mm/valloc.h>
6 #include <lunaix/spike.h>
7 #include <lunaix/syscall.h>
8 #include <lunaix/syscall_utils.h>
9 #include <lunaix/owloysius.h>
10
11 #include <klibc/strfmt.h>
12 #include <klibc/string.h>
13
14 static DEFINE_LLIST(root_list);
15
16 morph_t* device_mobj_root;
17
18 void
19 device_setname_vargs(struct device_meta* dev, char* fmt, va_list args)
20 {
21     ksnprintfv(dev->name_val, fmt, DEVICE_NAME_SIZE, args);
22     changeling_setname(dev_mobj(dev), dev->name_val);
23 }
24
25 void
26 device_register_generic(struct device_meta* devm, struct devclass* class, 
27                         char* fmt, ...)
28 {
29     va_list args;
30     morph_t* morphed, *parent;
31
32     morphed = &devm->mobj;
33     va_start(args, fmt);
34
35     if (fmt) {
36         device_setname_vargs(devm, fmt, args);
37     }
38
39     if (class && morph_type_of(morphed, device_morpher)) 
40     {
41         struct device* dev = to_dev(devm);
42         dev->ident = (struct devident) { 
43             .fn_grp = class->fn_grp,
44             .unique = DEV_UNIQUE(class->device, class->variant) 
45         };
46     }
47
48     parent = morphed->parent;
49     if (parent) {
50         changeling_attach(parent, morphed);
51     }
52
53     va_end(args);
54 }
55
56 void
57 device_create(struct device* dev, struct device_meta* parent,
58               u32_t type, void* underlay)
59 {
60     dev->underlay = underlay;
61     dev->dev_type = type;
62
63     llist_init_head(&dev->potentium);
64     mutex_init(&dev->lock);
65     iopoll_init_evt_q(&dev->pollers);
66 }
67
68 struct device*
69 device_alloc(struct device_meta* parent, u32_t type, void* underlay)
70 {
71     struct device* dev = vzalloc(sizeof(struct device));
72
73     if (!dev) {
74         return NULL;
75     }
76
77     device_create(dev, parent, type, underlay);
78     changeling_morph(dev_morph(parent), dev->mobj, NULL, device_morpher);
79
80     return dev;
81 }
82
83 struct device_alias*
84 device_alloc_alias(struct device_meta* parent, struct device_meta* aliased)
85 {
86     struct device_alias* dev = vzalloc(sizeof(struct device_alias));
87
88     if (!dev) {
89         return NULL;
90     }
91
92     dev->alias = aliased;
93     changeling_ref(dev_mobj(aliased));
94     changeling_morph(dev_morph(parent), dev->mobj, NULL, devalias_morpher);
95
96     return dev;
97 }
98
99 struct device_cat*
100 device_alloc_cat(struct device_meta* parent)
101 {
102     struct device_cat* dev = vzalloc(sizeof(struct device_cat));
103
104     if (!dev) {
105         return NULL;
106     }
107
108     changeling_morph(dev_morph(parent), dev->mobj, NULL, devcat_morpher);
109
110     return dev;
111 }
112
113 void
114 device_setname(struct device_meta* dev, char* fmt, ...)
115 {
116     va_list args;
117     va_start(args, fmt);
118
119     device_setname_vargs(dev, fmt, args);
120
121     va_end(args);
122 }
123
124 struct device_cat*
125 device_addcat(struct device_meta* parent, char* name_fmt, ...)
126 {
127     va_list args;
128     va_start(args, name_fmt);
129
130     struct device_cat* dev = device_alloc_cat(parent);
131
132     device_setname_vargs(dev_meta(dev), name_fmt, args);
133     device_register_generic(dev_meta(dev), NULL, NULL);
134
135     va_end(args);
136     return dev;
137 }
138
139 struct device_alias*
140 device_addalias(struct device_meta* parent, 
141                 struct device_meta* aliased, char* name_fmt, ...)
142 {
143     va_list args;
144     va_start(args, name_fmt);
145
146     struct device_alias* dev = device_alloc_alias(parent, aliased);
147
148     device_setname_vargs(dev_meta(dev), name_fmt, args);
149     device_register_generic(dev_meta(dev), NULL, NULL);
150
151     va_end(args);
152     return dev;
153 }
154
155 void
156 device_remove(struct device_meta* dev)
157 {
158     changeling_isolate(&dev->mobj);
159     vfree(dev);
160 }
161
162 void
163 device_populate_info(struct device* dev, struct dev_info* devinfo)
164 {
165     devinfo->dev_id.group = dev->ident.fn_grp;
166     devinfo->dev_id.unique = dev->ident.unique;
167
168     if (!devinfo->dev_name.buf) {
169         return;
170     }
171
172     struct device_def* def = devdef_byident(&dev->ident);
173     size_t buflen = devinfo->dev_name.buf_len;
174
175     strncpy(devinfo->dev_name.buf, def->name, buflen);
176     devinfo->dev_name.buf[buflen - 1] = 0;
177 }
178
179 morph_t*
180 resolve_device_morph(void* maybe_dev)
181 {
182     struct device_alias* da = NULL;
183     morph_t *morphed;
184     
185     morphed = morphed_ptr(maybe_dev);
186
187     if (!is_changeling(morphed)) {
188         return NULL;
189     }
190
191     if (morph_type_of(morphed, device_morpher)) 
192     {
193         return morphed;
194     }
195
196     if (morph_type_of(morphed, devcat_morpher)) 
197     {
198         return morphed;
199     }
200
201     while(morph_type_of(morphed, devalias_morpher)) 
202     {
203         da = changeling_reveal(morphed, devalias_morpher);
204         morphed = &da->alias->mobj;
205     }
206
207     return da ? morphed : NULL;
208 }
209
210 void
211 device_alert_poller(struct device* dev, int poll_evt)
212 {
213     dev->poll_evflags = poll_evt;
214     iopoll_wake_pollers(&dev->pollers);
215 }
216
217 void
218 device_chain_loader(struct device_def* def, devdef_ldfn fn)
219 {
220     struct device_ldfn_chain* node;
221
222     node = valloc(sizeof(*node));
223     node->load = fn;
224
225     if (!def->load_chain) {
226         node->chain = NULL;
227     }
228     else {
229         node->chain = def->load_chain;
230     }
231
232     def->load_chain = node;
233 }
234
235 void
236 device_chain_load_once(struct device_def* def)
237 {
238     struct device_ldfn_chain *node, *next;
239
240     if (def->load) {
241         def->load(def);
242     }
243
244     node = def->load_chain;
245     def->load_chain = NULL;
246     
247     while (node)
248     {
249         node->load(def);
250         next = node->chain;
251         vfree(node);
252
253         node = next;
254     }
255
256     if (def->flags.no_default_realm) {
257         return;
258     }
259
260     if (!def->create) {
261         return;
262     }
263
264     def->create(def, NULL);
265     
266 }
267
268 __DEFINE_LXSYSCALL3(int, ioctl, int, fd, int, req, sc_va_list, _args)
269 {
270     int errno = -1;
271     struct v_fd* fd_s;
272     va_list args;
273
274     convert_valist(&args, _args);
275
276     if ((errno &= vfs_getfd(fd, &fd_s))) {
277         goto done;
278     }
279
280     struct device* dev = resolve_device(fd_s->file->inode->data);
281     if (!dev) {
282         errno = ENODEV;
283         goto done;
284     }
285
286     if (req == DEVIOIDENT) {
287         struct dev_info* devinfo = va_arg(args, struct dev_info*);
288         device_populate_info(dev, devinfo);
289         errno = 0;
290     }
291
292     if (!dev->ops.exec_cmd) {
293         errno = ENOTSUP;
294         goto done;
295     }
296
297     errno = dev->ops.exec_cmd(dev, req, args);
298
299 done:
300     return DO_STATUS_OR_RETURN(errno);
301 }
302
303 static void
304 __device_subsys_init()
305 {
306     device_mobj_root = changeling_spawn(NULL, "devices");
307 }
308 owloysius_fetch_init(__device_subsys_init, on_sysconf);