Device subsystem improvements.
[tinyx.git] / kernel / device.c
bloba7baeb1c01775b0507bce0f68576a864f679ba15
1 #include <tinyx/device.h>
2 #include <tinyx/list.h>
3 #include <tinyx/libc/string.h>
5 DECLARE_LIST_HEAD(bus_list);
6 DECLARE_LIST_HEAD(driver_list);
7 DECLARE_LIST_HEAD(device_list);
9 static void *match_device_driver(struct device *dev, struct device_driver *drv)
11 struct list_head *list;
12 struct list_head *entry;
13 void *item;
14 int ret;
16 if (!dev)
17 list = &device_list;
18 else
19 list = &driver_list;
21 list_for_each(list, entry) {
22 if (!dev) {
23 item = list_entry(entry, struct device, queue);
24 ret = strcmp(((struct device *)item)->name, drv->name);
25 } else {
26 item = list_entry(entry, struct device_driver, queue);
27 ret = strcmp(dev->name,
28 ((struct device_driver *)item)->name);
30 if (!ret)
31 break;
32 else
33 item = NULL;
36 return item;
39 int register_bus_type(struct bus_type *bus)
41 int ret = 0;
43 if (bus == NULL)
44 return -ENODEV;
46 if (bus->probe)
47 ret = bus->probe(bus);
48 if (!ret)
49 list_add_tail(&bus_list, &bus->queue);
51 return ret;
54 int register_device_driver(struct device_driver *drv)
56 int ret = 0;
57 struct device *dev;
59 if (drv == NULL)
60 return -ENODEV;
62 dev = match_device_driver(NULL, drv);
63 if (dev) {
64 if (dev->bus->match)
65 ret = dev->bus->match(dev->bus, dev, drv);
66 if (!ret && drv->probe)
67 ret = drv->probe(dev);
70 if (!ret)
71 list_add_tail(&driver_list, &drv->queue);
73 return ret;
76 int register_device(struct device *dev, struct bus_type *bus)
78 int ret = 0;
79 struct device_driver *drv;
81 if (dev == NULL)
82 return -ENODEV;
84 dev->bus = bus;
85 drv = match_device_driver(dev, NULL);
86 if (drv) {
87 if (bus->match)
88 ret = bus->match(bus, dev, drv);
89 if (!ret && drv->probe)
90 ret = drv->probe(dev);
93 if (!ret)
94 list_add_tail(&device_list, &dev->queue);
95 else
96 dev->bus = NULL;
98 return ret;