COMMENT: Fixing file name on include/asm-arm7/device.h file's descriptions.
[tinyx.git] / kernel / device.c
blob8cd35ba5c5715a7bc8b550a426fb39971182467a
1 /* kernel/device.c
3 * Copyright (C) 2007 David Cohen <dacohen@gmail.com>
4 * Copyright (C) 2007 Felipe Balbi <me@felipebalbi.com>
6 * This file is part of Tinyx Nanokernel Project.
8 * Tinyx is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * Tinyx is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Tinyx. If not, see <http://www.gnu.org/licenses/>.
22 #include <tinyx/device.h>
23 #include <tinyx/list.h>
24 #include <string.h>
26 DECLARE_LIST_HEAD(bus_list);
27 DECLARE_LIST_HEAD(driver_list);
28 DECLARE_LIST_HEAD(device_list);
30 static void *match_device_driver(struct device *dev, struct device_driver *drv)
32 struct list_head *list;
33 struct list_head *entry;
34 void *item;
35 int ret;
37 if (!dev)
38 list = &device_list;
39 else
40 list = &driver_list;
42 list_for_each(list, entry) {
43 if (!dev) {
44 item = list_entry(entry, struct device, queue);
45 ret = strcmp(((struct device *)item)->name, drv->name);
46 } else {
47 item = list_entry(entry, struct device_driver, queue);
48 ret = strcmp(dev->name,
49 ((struct device_driver *)item)->name);
51 if (!ret)
52 break;
53 else
54 item = NULL;
57 return item;
60 int register_bus_type(struct bus_type *bus)
62 int ret = 0;
64 if (bus == NULL)
65 return -ENODEV;
67 if (bus->probe)
68 ret = bus->probe(bus);
69 if (!ret)
70 list_add_tail(&bus_list, &bus->queue);
72 return ret;
75 int register_device_driver(struct device_driver *drv)
77 int ret = 0;
78 struct device *dev;
80 if (drv == NULL)
81 return -ENODEV;
83 dev = match_device_driver(NULL, drv);
84 if (dev) {
85 if (dev->bus->match)
86 ret = dev->bus->match(dev->bus, dev, drv);
87 if (!ret && drv->probe)
88 ret = drv->probe(dev);
91 if (!ret)
92 list_add_tail(&driver_list, &drv->queue);
94 return ret;
97 int register_device(struct device *dev, struct bus_type *bus)
99 int ret = 0;
100 struct device_driver *drv;
102 if (dev == NULL)
103 return -ENODEV;
105 dev->bus = bus;
106 drv = match_device_driver(dev, NULL);
107 if (drv) {
108 if (bus->match)
109 ret = bus->match(bus, dev, drv);
110 if (!ret && drv->probe)
111 ret = drv->probe(dev);
114 if (!ret)
115 list_add_tail(&device_list, &dev->queue);
116 else
117 dev->bus = NULL;
119 return ret;