]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/l4re_vfs/include/impl/vcon_stream_impl.h
update
[l4.git] / l4 / pkg / l4re_vfs / include / impl / vcon_stream_impl.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  * As a special exception, you may use this file as part of a free software
10  * library without restriction.  Specifically, if other files instantiate
11  * templates or use macros or inline functions from this file, or you compile
12  * this file and link it with other files to produce an executable, this
13  * file does not by itself cause the resulting executable to be covered by
14  * the GNU General Public License.  This exception does not however
15  * invalidate any other reasons why the executable file might be covered by
16  * the GNU General Public License.
17  */
18
19 #include <l4/re/env>
20 #include <l4/sys/factory>
21
22 #include "vcon_stream.h"
23 #include "vfs_api.h"
24
25 #include <termios.h>
26 #include <unistd.h>
27 #include <sys/ioctl.h>
28
29 namespace L4Re { namespace Core {
30 Vcon_stream::Vcon_stream(L4::Cap<L4::Vcon> s) throw()
31 : Be_file(), _s(s), _irq(cap_alloc()->alloc<L4::Irq>())
32 {
33 #if 1
34   //printf("VCON: irq cap = %lx\n", _irq.cap());
35   int res = l4_error(L4Re::Env::env()->factory()->create_irq(_irq));
36   //printf("VCON: irq create res=%d\n", res);
37
38   if (res < 0)
39     return; // handle errors!!!
40
41   res = l4_error(L4::cap_reinterpret_cast<L4::Icu>(_s)->bind(0, _irq));
42   //printf("VCON: bound irq to con res=%d\n", res);
43 #endif
44 }
45
46 ssize_t
47 Vcon_stream::readv(const struct iovec *iovec, int iovcnt) throw()
48 {
49   (void) iovec;
50   (void) iovcnt;
51 #if 0
52   ssize_t bytes = 0;
53   for (; iovcnt > 0; --iovcnt, ++iovec)
54     {
55       if (iovec->iov_len == 0)
56         continue;
57
58       char *buf = (char *)iovec->iov_base;
59       size_t len = iovec->iov_len;
60
61       while (1)
62         {
63           int ret = _s->read(buf, len);
64
65           // BS: what is this ??
66           if (ret > (int)len)
67             ret = len;
68
69           if (ret < 0)
70             return ret;
71           else if (ret == 0)
72             {
73               if (bytes)
74                 return bytes;
75
76               _irq->detach();
77               _irq->attach(12, L4::Cap<L4::Thread>::Invalid);
78               ret = _s->read(buf, len);
79               if (ret < 0)
80                 return ret;
81               else if (ret == 0)
82                 {
83                   _irq->receive();
84                   continue;
85                 }
86             }
87
88           bytes += ret;
89           len   -= ret;
90           buf   += ret;
91
92           if (len == 0)
93             break;
94         }
95     }
96
97   return bytes;
98 #endif
99   return 0;
100 }
101
102 ssize_t
103 Vcon_stream::writev(const struct iovec *iovec, int iovcnt) throw()
104 {
105   l4_msg_regs_t store;
106   l4_msg_regs_t *mr = l4_utcb_mr();
107
108   Vfs_config::memcpy(&store, mr, sizeof(store));
109
110   ssize_t written = 0;
111   while (iovcnt)
112     {
113       size_t sl = iovec->iov_len;
114       char const *b = (char const *)iovec->iov_base;
115
116       for (; sl > L4_VCON_WRITE_SIZE
117            ; sl -= L4_VCON_WRITE_SIZE, b += L4_VCON_WRITE_SIZE)
118         _s->write(b, L4_VCON_WRITE_SIZE);
119
120       _s->write(b, sl);
121
122       written += iovec->iov_len;
123
124       ++iovec;
125       --iovcnt;
126     }
127   Vfs_config::memcpy(mr, &store, sizeof(store));
128   return written;
129 }
130
131 int
132 Vcon_stream::fstat64(struct stat64 *buf) const throw()
133 {
134   buf->st_size = 0;
135   buf->st_mode = 0666;
136   buf->st_dev = _s.cap();
137   buf->st_ino = 0;
138   return 0;
139 }
140
141 int
142 Vcon_stream::ioctl(unsigned long request, va_list args) throw()
143 {
144   switch (request) {
145     case TCGETS:
146         {
147           //vt100_tcgetattr(term, (struct termios *)argp);
148
149           struct termios *t = va_arg(args, struct termios *);
150
151           l4_vcon_attr_t l4a;
152           if (!l4_error(_s->get_attr(&l4a)))
153             {
154               t->c_iflag = l4a.i_flags;
155               t->c_oflag = l4a.o_flags; // output flags
156               t->c_cflag = 0; // control flags
157               t->c_lflag = l4a.l_flags; // local flags
158             }
159           else
160             t->c_iflag = t->c_oflag = t->c_cflag = t->c_lflag = 0;
161 #if 0
162           //t->c_lflag |= ECHO; // if term->echo
163           t->c_lflag |= ICANON; // if term->term_mode == VT100MODE_COOKED
164 #endif
165
166           t->c_cc[VEOF]   = CEOF;
167           t->c_cc[VEOL]   = _POSIX_VDISABLE;
168           t->c_cc[VEOL2]  = _POSIX_VDISABLE;
169           t->c_cc[VERASE] = CERASE;
170           t->c_cc[VWERASE]= CWERASE;
171           t->c_cc[VKILL]  = CKILL;
172           t->c_cc[VREPRINT]=CREPRINT;
173           t->c_cc[VINTR]  = CINTR;
174           t->c_cc[VQUIT]  = _POSIX_VDISABLE;
175           t->c_cc[VSUSP]  = CSUSP;
176           t->c_cc[VSTART] = CSTART;
177           t->c_cc[VSTOP] = CSTOP;
178           t->c_cc[VLNEXT] = CLNEXT;
179           t->c_cc[VDISCARD]=CDISCARD;
180           t->c_cc[VMIN] = CMIN;
181           t->c_cc[VTIME] = 0;
182
183         }
184
185       return 0;
186
187     case TCSETS:
188     case TCSETSW:
189     case TCSETSF:
190         {
191           //vt100_tcsetattr(term, (struct termios *)argp);
192           struct termios const *t = va_arg(args, struct termios const *);
193
194           // XXX: well, we're cheating, get this from the other side!
195
196           l4_vcon_attr_t l4a;
197           l4a.i_flags = t->c_iflag;
198           l4a.o_flags = t->c_oflag; // output flags
199           l4a.l_flags = t->c_lflag; // local flags
200           _s->set_attr(&l4a);
201         }
202       return 0;
203
204     default:
205       break;
206   };
207   return -EINVAL;
208 }
209
210 }}