beb1f18050b15097bb02d5696b037a8a8f32e5a6
[lunaix-os.git] / lunaix-os / kernel / device / device.c
1 #include <klibc/stdio.h>
2 #include <lunaix/device.h>
3 #include <lunaix/fs/twifs.h>
4 #include <lunaix/mm/valloc.h>
5 #include <lunaix/spike.h>
6
7 static DEFINE_LLIST(root_list);
8
9 static volatile dev_t devid = 0;
10
11 struct device*
12 device_add(struct device* parent,
13            void* underlay,
14            char* name_fmt,
15            uint32_t type,
16            va_list args)
17 {
18     struct device* dev = vzalloc(sizeof(struct device));
19
20     if (parent) {
21         assert((parent->dev_type & DEV_MSKIF) == DEV_IFCAT);
22         llist_append(&parent->children, &dev->siblings);
23     } else {
24         llist_append(&root_list, &dev->siblings);
25     }
26
27     size_t strlen =
28       __ksprintf_internal(dev->name_val, name_fmt, DEVICE_NAME_SIZE, args);
29
30     dev->dev_id = devid++;
31     dev->name = HSTR(dev->name_val, strlen);
32     dev->parent = parent;
33     dev->underlay = underlay;
34     dev->dev_type = type;
35
36     hstr_rehash(&dev->name, HSTR_FULL_HASH);
37     llist_init_head(&dev->children);
38
39     return dev;
40 }
41
42 struct device*
43 device_addseq(struct device* parent, void* underlay, char* name_fmt, ...)
44 {
45     va_list args;
46     va_start(args, name_fmt);
47
48     struct device* dev =
49       device_add(parent, underlay, name_fmt, DEV_IFSEQ, args);
50
51     va_end(args);
52     return dev;
53 }
54
55 struct device*
56 device_addvol(struct device* parent, void* underlay, char* name_fmt, ...)
57 {
58     va_list args;
59     va_start(args, name_fmt);
60
61     struct device* dev =
62       device_add(parent, underlay, name_fmt, DEV_IFVOL, args);
63
64     va_end(args);
65     return dev;
66 }
67
68 struct device*
69 device_addcat(struct device* parent, char* name_fmt, ...)
70 {
71     va_list args;
72     va_start(args, name_fmt);
73
74     struct device* dev = device_add(parent, NULL, name_fmt, DEV_IFCAT, args);
75
76     va_end(args);
77     return dev;
78 }
79
80 struct device*
81 device_getbyid(struct llist_header* devlist, dev_t id)
82 {
83     devlist = devlist ? devlist : &root_list;
84     struct device *pos, *n;
85     llist_for_each(pos, n, devlist, siblings)
86     {
87         if (pos->dev_id == id) {
88             return pos;
89         }
90     }
91
92     return NULL;
93 }
94
95 struct device*
96 device_getbyhname(struct device* root_dev, struct hstr* name)
97 {
98     struct llist_header* devlist = root_dev ? &root_dev->children : &root_list;
99     struct device *pos, *n;
100     llist_for_each(pos, n, devlist, siblings)
101     {
102         if (HSTR_EQ(&pos->name, name)) {
103             return pos;
104         }
105     }
106
107     return NULL;
108 }
109
110 struct device*
111 device_getbyname(struct device* root_dev, const char* name, size_t len)
112 {
113     struct hstr hname = HSTR(name, len);
114     hstr_rehash(&hname, HSTR_FULL_HASH);
115
116     return device_getbyhname(root_dev, &hname);
117 }
118
119 void
120 device_remove(struct device* dev)
121 {
122     llist_delete(&dev->siblings);
123     vfree(dev);
124 }
125
126 struct device*
127 device_getbyoffset(struct device* root_dev, int offset)
128 {
129     struct llist_header* devlist = root_dev ? &root_dev->children : &root_list;
130     struct device *pos, *n;
131     int off = 0;
132     llist_for_each(pos, n, devlist, siblings)
133     {
134         if (off++ >= offset) {
135             return pos;
136         }
137     }
138     return NULL;
139 }