]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/io/server/src/vgpio.cc
99cdd7e2742e35f0f95976e4234cdce6b5f09169
[l4.git] / l4 / pkg / io / server / src / vgpio.cc
1 /*
2  * (c) 2011 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
11 #include "gpio"
12 #include "hw_device.h"
13 #include "vdevice.h"
14 #include "vbus_factory.h"
15 #include "vbus.h"
16 #include "vicu.h"
17
18 #include <l4/vbus/vbus_gpio-ops.h>
19
20 #include <cerrno>
21
22
23 namespace Vi {
24 namespace {
25
26 class Gpio : public Device, public Dev_feature
27 {
28 public:
29   int dispatch(l4_umword_t, l4_uint32_t func, L4::Ipc::Iostream &ios);
30
31   explicit Gpio(Hw::Device *d)
32     : _hwd(dynamic_cast<Hw::Gpio_chip*>(d)), _mask(0)
33   {
34     add_feature(this);
35
36     for (unsigned i = 0; i < Max_pins; ++i)
37       _irqs[i] = -1;
38   }
39
40   int add_filter(cxx::String const &tag, cxx::String const &)
41   {
42     if (tag != "pins")
43       return -ENODEV;
44     return -EINVAL;
45   }
46
47   int add_filter(cxx::String const &tag, unsigned long long val)
48   {
49     if (tag != "pins")
50       return -ENODEV;
51     _mask |= (1UL << val);
52     return 0;
53   }
54
55   int add_filter(cxx::String const &tag, unsigned long long s, unsigned long long e)
56   {
57     if (tag != "pins")
58       return -ENODEV;
59     _mask |= ~(~0UL << (e - s + 1)) << s;
60     return 0;
61   }
62
63   char const *hid() const { return "GPIO"; }
64   void set_host(Device *d) { _host = d; }
65   Device *host() const { return _host; }
66
67   bool match_hw_feature(const Hw::Dev_feature*) const
68   { return false; }
69
70 private:
71   enum { Max_pins = 32 };
72   Device *_host;
73   Hw::Gpio_chip *_hwd;
74   l4_uint32_t _mask;
75
76   int _irqs[Max_pins];
77
78   int setup(L4::Ipc::Iostream &ios);
79   int config_pad(L4::Ipc::Iostream &ios);
80   int get(L4::Ipc::Iostream &ios);
81   int set(L4::Ipc::Iostream &ios);
82   int multi_setup(L4::Ipc::Iostream &ios);
83   int multi_config_pad(L4::Ipc::Iostream &ios);
84   int multi_get(L4::Ipc::Iostream &ios);
85   int multi_set(L4::Ipc::Iostream &ios);
86   int to_irq(L4::Ipc::Iostream &ios);
87
88   void check(unsigned pin)
89   {
90     if (pin > 31)
91       throw -ERANGE;
92
93     if (!((1UL << pin) & _mask))
94       throw -ERANGE;
95   }
96
97   void check_mask(unsigned mask)
98   {
99     if (mask & ~_mask)
100       throw -ERANGE;
101   }
102 };
103
104
105
106 int
107 Gpio::setup(L4::Ipc::Iostream &ios)
108 {
109   unsigned pin, mode;
110   int value;
111   ios >> pin >> mode >> value;
112   check(pin);
113   _hwd->setup(pin, mode, value);
114   return 0;
115 }
116
117 int
118 Gpio::config_pad(L4::Ipc::Iostream &ios)
119 {
120   unsigned pin, func, value;
121   ios >> pin >> func >> value;
122   check(pin);
123   _hwd->config_pad(pin, _mask, func, value);
124   return 0;
125 }
126
127 int
128 Gpio::get(L4::Ipc::Iostream &ios)
129 {
130   unsigned pin;
131   ios >> pin;
132   check(pin);
133   return _hwd->get(pin);
134 }
135
136 int
137 Gpio::set(L4::Ipc::Iostream &ios)
138 {
139   unsigned pin;
140   int value;
141   ios >> pin >> value;
142   check(pin);
143   _hwd->set(pin, value);
144   return 0;
145 }
146
147 int
148 Gpio::multi_setup(L4::Ipc::Iostream &ios)
149 {
150   unsigned mask, mode, outvalue;
151   ios >> mask >> mode >> outvalue;
152   check_mask(mask);
153   _hwd->multi_setup(mask, mode, outvalue);
154   return 0;
155 }
156
157 int
158 Gpio::multi_config_pad(L4::Ipc::Iostream &ios)
159 {
160   unsigned mask, func, value;
161   ios >> mask >> func >> value;
162   check_mask(mask);
163   _hwd->multi_config_pad(mask, func, value);
164   return 0;
165 }
166
167 int
168 Gpio::multi_get(L4::Ipc::Iostream &ios)
169 {
170   unsigned data = _hwd->multi_get();
171   ios << (unsigned)(data & _mask);
172   return 0;
173 }
174
175 int
176 Gpio::multi_set(L4::Ipc::Iostream &ios)
177 {
178   unsigned mask, data;
179   ios >> mask >> data;
180   check_mask(mask);
181   _hwd->multi_set(mask, data);
182   return 0;
183 }
184
185 int
186 Gpio::to_irq(L4::Ipc::Iostream &ios)
187 {
188   unsigned pin;
189   ios >> pin;
190   check(pin);
191
192   if (_irqs[pin] == -1)
193     {
194       // we have to allocate the IRQ...
195       // if it fails we mark the IRQ as unavailable (-L4_ENODEV)
196       _irqs[pin] = -L4_ENODEV;
197
198       int irqnum = _hwd->get_irq(pin);
199       if (irqnum < 0)
200         return irqnum;
201
202       if (System_bus *sb = dynamic_cast<System_bus *>(get_root()))
203         {
204           int virq = sb->sw_icu()->alloc_irq(Sw_icu::S_allow_set_mode,
205                                              Sw_icu::real_irq(irqnum));
206           if (virq < 0)
207             return virq;
208
209           _irqs[pin] = virq;
210           return virq;
211         }
212       return -L4_ENODEV;
213     }
214   return _irqs[pin];
215 }
216
217 int
218 Gpio::dispatch(l4_umword_t, l4_uint32_t func, L4::Ipc::Iostream &ios)
219 {
220   try
221     {
222       switch (func)
223         {
224         case L4VBUS_GPIO_OP_SETUP: return setup(ios);
225         case L4VBUS_GPIO_OP_CONFIG_PAD: return config_pad(ios);
226         case L4VBUS_GPIO_OP_GET: return get(ios);
227         case L4VBUS_GPIO_OP_SET: return set(ios);
228         case L4VBUS_GPIO_OP_MULTI_SETUP: return multi_setup(ios);
229         case L4VBUS_GPIO_OP_MULTI_CONFIG_PAD: return multi_config_pad(ios);
230         case L4VBUS_GPIO_OP_MULTI_GET: return multi_get(ios);
231         case L4VBUS_GPIO_OP_MULTI_SET: return multi_set(ios);
232         case L4VBUS_GPIO_OP_TO_IRQ: return to_irq(ios);
233         default: return -L4_ENOSYS;
234         }
235     }
236   catch (int err)
237     {
238       return err;
239     }
240 }
241
242
243 static Dev_factory_t<Gpio, Hw::Gpio_device> __gpio_factory;
244
245
246 }
247 }