feat: add support for process to conduct Intel x87 and MMX related task.
[lunaix-os.git] / lunaix-os / kernel / device / device.c
1 #include <klibc/stdio.h>
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
10 static DEFINE_LLIST(root_list);
11
12 static volatile dev_t devid = 0;
13
14 struct device*
15 device_add(struct device* parent,
16            void* underlay,
17            char* name_fmt,
18            uint32_t type,
19            va_list args)
20 {
21     struct device* dev = vzalloc(sizeof(struct device));
22
23     if (parent) {
24         assert((parent->dev_type & DEV_MSKIF) == DEV_IFCAT);
25         llist_append(&parent->children, &dev->siblings);
26     } else {
27         llist_append(&root_list, &dev->siblings);
28     }
29
30     size_t strlen =
31       __ksprintf_internal(dev->name_val, name_fmt, DEVICE_NAME_SIZE, args);
32
33     dev->magic = DEV_STRUCT_MAGIC;
34     dev->dev_id = devid++;
35     dev->name = HSTR(dev->name_val, strlen);
36     dev->parent = parent;
37     dev->underlay = underlay;
38     dev->dev_type = type;
39
40     hstr_rehash(&dev->name, HSTR_FULL_HASH);
41     llist_init_head(&dev->children);
42
43     return dev;
44 }
45
46 struct device*
47 device_addseq(struct device* parent, void* underlay, char* name_fmt, ...)
48 {
49     va_list args;
50     va_start(args, name_fmt);
51
52     struct device* dev =
53       device_add(parent, underlay, name_fmt, DEV_IFSEQ, args);
54
55     va_end(args);
56     return dev;
57 }
58
59 struct device*
60 device_addvol(struct device* parent, void* underlay, char* name_fmt, ...)
61 {
62     va_list args;
63     va_start(args, name_fmt);
64
65     struct device* dev =
66       device_add(parent, underlay, name_fmt, DEV_IFVOL, args);
67
68     va_end(args);
69     return dev;
70 }
71
72 struct device*
73 device_addcat(struct device* parent, char* name_fmt, ...)
74 {
75     va_list args;
76     va_start(args, name_fmt);
77
78     struct device* dev = device_add(parent, NULL, name_fmt, DEV_IFCAT, args);
79
80     va_end(args);
81     return dev;
82 }
83
84 struct device*
85 device_getbyid(struct llist_header* devlist, dev_t id)
86 {
87     devlist = devlist ? devlist : &root_list;
88     struct device *pos, *n;
89     llist_for_each(pos, n, devlist, siblings)
90     {
91         if (pos->dev_id == id) {
92             return pos;
93         }
94     }
95
96     return NULL;
97 }
98
99 struct device*
100 device_getbyhname(struct device* root_dev, struct hstr* name)
101 {
102     struct llist_header* devlist = root_dev ? &root_dev->children : &root_list;
103     struct device *pos, *n;
104     llist_for_each(pos, n, devlist, siblings)
105     {
106         if (HSTR_EQ(&pos->name, name)) {
107             return pos;
108         }
109     }
110
111     return NULL;
112 }
113
114 struct device*
115 device_getbyname(struct device* root_dev, const char* name, size_t len)
116 {
117     struct hstr hname = HSTR(name, len);
118     hstr_rehash(&hname, HSTR_FULL_HASH);
119
120     return device_getbyhname(root_dev, &hname);
121 }
122
123 void
124 device_remove(struct device* dev)
125 {
126     llist_delete(&dev->siblings);
127     vfree(dev);
128 }
129
130 struct device*
131 device_getbyoffset(struct device* root_dev, int offset)
132 {
133     struct llist_header* devlist = root_dev ? &root_dev->children : &root_list;
134     struct device *pos, *n;
135     int off = 0;
136     llist_for_each(pos, n, devlist, siblings)
137     {
138         if (off++ >= offset) {
139             return pos;
140         }
141     }
142     return NULL;
143 }
144
145 __DEFINE_LXSYSCALL3(int, ioctl, int, fd, int, req, va_list, args)
146 {
147     int errno;
148     struct v_fd* fd_s;
149     if ((errno = vfs_getfd(fd, &fd_s))) {
150         goto done;
151     }
152
153     struct device* dev = (struct device*)fd_s->file->inode->data;
154     if (dev->magic != DEV_STRUCT_MAGIC) {
155         errno = ENODEV;
156         goto done;
157     }
158
159     if (!dev->exec_cmd) {
160         errno = EINVAL;
161         goto done;
162     }
163
164     errno = dev->exec_cmd(dev, req, args);
165
166 done:
167     return DO_STATUS_OR_RETURN(errno);
168 }