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