]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/io/server/src/vdevice.h
update
[l4.git] / l4 / pkg / io / server / src / vdevice.h
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 #pragma once
10
11 #include <l4/sys/types.h>
12 #include <l4/cxx/avl_set>
13 //#include <set>
14 #include <string>
15 #include <vector>
16 #include <l4/cxx/ipc_stream>
17 #include <l4/vbus/vbus_types.h>
18 #include <cstdio> // dbg
19
20 #include "device.h"
21
22 namespace Hw {
23 class Dev_feature;
24 }
25
26 class Adr_resource;
27
28 namespace Vi {
29
30 class Device;
31
32 class Dev_feature
33 {
34 public:
35   virtual ~Dev_feature() {}
36   virtual bool match_hw_feature(Hw::Dev_feature const *) const = 0;
37   virtual int dispatch(l4_umword_t obj, l4_uint32_t func, L4::Ipc_iostream &ios) = 0;
38   virtual Device *host() const = 0;
39   virtual void set_host(Device *d) = 0;
40 };
41
42
43 class Device : public Generic_device, public Device_tree_mixin<Device>
44 {
45 public:
46   typedef Device_tree_mixin<Device>::iterator iterator;
47   using Device_tree_mixin<Device>::begin;
48   using Device_tree_mixin<Device>::end;
49
50   // disptach helper for server object
51   int vdevice_dispatch(l4_umword_t obj, l4_uint32_t func, L4::Ipc_iostream &ios);
52
53   typedef std::vector<Dev_feature*> Feature_list;
54
55   Feature_list const *features() const { return &_features; }
56
57   void add_feature(Dev_feature *f)
58   {
59     f->set_host(this);
60     _features.push_back(f);
61   }
62
63   template< typename FT >
64   FT *find_feature()
65   {
66     for (Feature_list::const_iterator i = _features.begin();
67          i != _features.end(); ++i)
68       if (FT *r = dynamic_cast<FT*>(*i))
69         return r;
70
71     return 0;
72   }
73
74
75   virtual l4_uint32_t adr() const
76   { return l4_uint32_t(~0); }
77
78   virtual ~Device()
79   { __devs.erase(l4vbus_device_handle_t(this)); }
80
81   char const *name() const
82   { return _name.c_str(); }
83
84   bool name(cxx::String const &n)
85   {
86     _name = std::string(n.start(), n.end());
87     return true;
88   }
89
90   bool resource_allocated(Resource const *) const;
91
92   virtual void finalize_setup() {}
93
94   Device *parent() const { return _dt.parent(); }
95   Device *children() const { return _dt.children(); }
96   Device *next() const { return _dt.next(); }
97   int depth() const { return _dt.depth(); }
98
99 protected:
100   // helper functions
101   int get_by_hid(L4::Ipc_iostream &ios);
102   int vbus_get_device(L4::Ipc_iostream &ios);
103   Device *get_dev_by_id(l4vbus_device_handle_t id);
104
105
106   Device() : _name("(noname)")
107   { __devs.insert(l4vbus_device_handle_t(this)); }
108
109   Device *get_root()
110   {
111     Device *d;
112     for (d = this; d->parent(); d = d->parent())
113       ;
114     return d;
115   }
116
117   std::string _name;
118
119   typedef cxx::Avl_set<l4vbus_device_handle_t> Dev_set;
120   //typedef std::set<l4vbus_device_handle_t> Dev_set;
121   static Dev_set __devs;
122
123 private:
124   Feature_list _features;
125 };
126
127 }