]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/io/server/src/hw_device.cc
update
[l4.git] / l4 / pkg / io / server / src / hw_device.cc
1 /*
2  * (c) 2010 Alexander Warg <warg@os.inf.tu-dresden.de>
3  *     economic rights: Technische Universität Dresden (Germany)
4  *
5  * This file is part of TUD:OS and distributed under the terms of the
6  * GNU General Public License 2.
7  * Please see the COPYING-GPL-2 file for details.
8  */
9
10 #include <libc-symbols.h>
11 #include <fnmatch.h>
12
13 #include "hw_device.h"
14 #include "cfg.h"
15
16 namespace Hw {
17
18
19 void
20 Device::init()
21 {
22 #if 0
23   printf("Hw::Device::plug(this=%p, name='%s', hid='%s')\n",
24          this, name(), hid());
25 #endif
26   discover_resources();
27   request_resources();
28   _sta = Active;
29 }
30
31 void
32 Device::discover_secondary_bus()
33 {
34 #if 0
35   printf("Hw::Device::discover_secondary_bus(this=%p, name='%s', hid='%s')\n",
36          this, name(), hid());
37 #endif
38   discover_devices();
39
40   for (iterator c = begin(0); c != end(); ++c)
41     (*c)->init();
42
43   for (iterator c = begin(0); c != end(); ++c)
44     if ((*c)->status())
45       (*c)->discover_secondary_bus();
46 }
47
48 void
49 Device::plugin()
50 {
51   init();
52   discover_secondary_bus();
53   allocate_pending_resources();
54   setup_resources();
55 }
56
57 void
58 Device_factory::dump()
59 {
60   for (Name_map::const_iterator i = nm().begin(); i != nm().end(); ++i)
61     printf("HW: '%s'\n", (*i).first.c_str());
62 }
63
64 Device *
65 Device_factory::create(cxx::String const &name)
66 {
67   Name_map::const_iterator i = nm().find(std::string(name.start(), name.end()));
68   if (i != nm().end())
69     return i->second->create();
70
71   return 0;
72 }
73
74 bool
75 Device::resource_allocated(Resource const *r) const
76 {
77   return r->parent();
78 }
79
80 void
81 Device::discover_devices()
82 {
83   if (discover_bus_if())
84     discover_bus_if()->scan_bus();
85 }
86
87 void
88 Device::discover_resources()
89 {
90   for (Discover_res_list::const_iterator d = _resource_discovery_chain.begin();
91        d != _resource_discovery_chain.end(); ++d)
92     (*d)->discover_resources(this);
93 }
94
95 void
96 Device::setup_resources()
97 {
98   for (Discover_res_list::const_iterator d = _resource_discovery_chain.begin();
99        d != _resource_discovery_chain.end(); ++d)
100     (*d)->setup_resources(this);
101
102   // take care for children
103   for (Hw::Device::iterator i = begin(0); i != end(); ++i)
104     i->setup_resources();
105 }
106
107 Device *
108 Device::get_child_dev_adr(l4_uint32_t adr, bool create)
109 {
110   for (Device *c = children(); c; c = c->next())
111     if (c->adr() == adr)
112       return c;
113
114   if (!create)
115     return 0;
116
117   Device *c = new Device(adr);
118   _dt.add_child(c, this);
119   return c;
120 }
121
122 Device *
123 Device::get_child_dev_uid(l4_umword_t uid, l4_uint32_t adr, bool create)
124 {
125   for (Device *c = children(); c; c = c->next())
126     if (c->uid() == uid)
127       return c;
128
129   if (!create)
130     return 0;
131
132   Device *c = new Device(uid, adr);
133   _dt.add_child(c, this);
134   return c;
135 }
136
137 int
138 Device::set_property(cxx::String const &prop, Prop_val const &val)
139 {
140   if (prop == "hid")
141     {
142       if (val.type != Prop_val::String)
143         return -E_inval;
144
145       cxx::String const v = val.get_string();
146       _hid = std::string(v.start(), v.start() + v.len());
147       return E_ok;
148     }
149   else if (prop == "name")
150     {
151       if (val.type != Prop_val::String)
152         return -E_inval;
153
154       cxx::String const v = val.get_string();
155       _name = std::string(v.start(), v.start() + v.len());
156       return E_ok;
157     }
158   else if (prop == "adr")
159     {
160       if (val.type != Prop_val::Int)
161         return -E_inval;
162
163       _adr = val.val.integer;
164       return E_ok;
165     }
166   else
167     return -E_no_prop;
168 }
169
170 bool
171 Device::match_cid(cxx::String const &cid) const
172 {
173     {
174       char cid_cstr[cid.len() + 1];
175       __builtin_memcpy(cid_cstr, cid.start(), cid.len());
176       cid_cstr[cid.len()]  = 0;
177       if (!fnmatch(cid_cstr, hid(), 0))
178         return true;
179     }
180
181   for (Cid_list::const_iterator i = _cid.begin(); i != _cid.end(); ++i)
182     if (cid == (*i).c_str())
183       return true;
184
185   for (Feature_list::const_iterator i = _features.begin();
186        i != _features.end(); ++i)
187     if ((*i)->match_cid(cid))
188       return true;
189
190   return false;
191 }
192
193 void
194 Device::dump(int indent) const
195 {
196   printf("%*.sHw::Device[%s]\n", indent, " ", name());
197   if (Io_config::cfg->verbose() > 2)
198     {
199       for (Feature_list::const_iterator i = _features.begin();
200            i != _features.end(); ++i)
201         (*i)->dump(indent + 2);
202     }
203 }
204
205 namespace {
206   static Device_factory_t<Device> __hw_pf_factory("Device");
207 }
208 }