cce51b19dbec4aaf9c3bbc9b94a0e8bddc18b018
[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 = ksnprintfv(dev->name_val, fmt, DEVICE_NAME_SIZE, args);
24
25     dev->name = HSTR(dev->name_val, strlen);
26
27     hstr_rehash(&dev->name, HSTR_FULL_HASH);
28 }
29
30 void
31 device_register(struct device* dev, struct devclass* class, char* fmt, ...)
32 {
33     va_list args;
34     va_start(args, fmt);
35
36     if (fmt) {
37         device_setname_vargs(dev, fmt, args);
38     }
39
40     if (class) {
41         dev->ident = (struct devident){ .fn_grp = class->fn_grp,
42                                         .unique = DEV_UNIQUE(class->device,
43                                                              class->variant) };
44     }
45
46     dev->dev_uid = devid++;
47
48     struct device* parent = dev->parent;
49     if (parent) {
50         assert((parent->dev_type & DEV_MSKIF) == DEV_IFCAT);
51         llist_append(&parent->children, &dev->siblings);
52     } else {
53         llist_append(&root_list, &dev->siblings);
54     }
55
56     va_end(args);
57 }
58
59 void
60 device_create(struct device* dev,
61               struct device* parent,
62               u32_t type,
63               void* underlay)
64 {
65     dev->magic = DEV_STRUCT_MAGIC;
66     dev->underlay = underlay;
67     dev->dev_type = type;
68     dev->parent = parent;
69
70     llist_init_head(&dev->children);
71     mutex_init(&dev->lock);
72     iopoll_init_evt_q(&dev->pollers);
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 void
205 device_alert_poller(struct device* dev, int poll_evt)
206 {
207     dev->poll_evflags = poll_evt;
208     iopoll_wake_pollers(&dev->pollers);
209 }
210
211 __DEFINE_LXSYSCALL3(int, ioctl, int, fd, int, req, va_list, args)
212 {
213     int errno = -1;
214     struct v_fd* fd_s;
215     if ((errno &= vfs_getfd(fd, &fd_s))) {
216         goto done;
217     }
218
219     struct device* dev = (struct device*)fd_s->file->inode->data;
220     if (dev->magic != DEV_STRUCT_MAGIC) {
221         errno &= ENODEV;
222         goto done;
223     }
224
225     if (req == DEVIOIDENT) {
226         struct dev_info* devinfo = va_arg(args, struct dev_info*);
227         device_populate_info(dev, devinfo);
228         errno = 0;
229     }
230
231     if (!dev->ops.exec_cmd) {
232         errno &= ENOTSUP;
233         goto done;
234     }
235
236     errno &= dev->ops.exec_cmd(dev, req, args);
237
238 done:
239     return DO_STATUS_OR_RETURN(errno);
240 }