tests/tcg/mips: Add tests for R5900 three-operand MULTU
[qemu/ar7.git] / chardev / char-fe.c
bloba8931f7afd2e4141521c35269f2864be73c3dc01
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
24 #include "qemu/osdep.h"
25 #include "qemu/error-report.h"
26 #include "qapi/error.h"
27 #include "qapi/qmp/qerror.h"
28 #include "sysemu/replay.h"
30 #include "chardev/char-fe.h"
31 #include "chardev/char-io.h"
32 #include "chardev/char-mux.h"
34 int qemu_chr_fe_write(CharBackend *be, const uint8_t *buf, int len)
36 Chardev *s = be->chr;
38 if (!s) {
39 return 0;
42 return qemu_chr_write(s, buf, len, false);
45 int qemu_chr_fe_write_all(CharBackend *be, const uint8_t *buf, int len)
47 Chardev *s = be->chr;
49 if (!s) {
50 return 0;
53 return qemu_chr_write(s, buf, len, true);
56 int qemu_chr_fe_read_all(CharBackend *be, uint8_t *buf, int len)
58 Chardev *s = be->chr;
59 int offset = 0;
60 int res;
62 if (!s || !CHARDEV_GET_CLASS(s)->chr_sync_read) {
63 return 0;
66 if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_PLAY) {
67 return replay_char_read_all_load(buf);
70 while (offset < len) {
71 retry:
72 res = CHARDEV_GET_CLASS(s)->chr_sync_read(s, buf + offset,
73 len - offset);
74 if (res == -1 && errno == EAGAIN) {
75 g_usleep(100);
76 goto retry;
79 if (res == 0) {
80 break;
83 if (res < 0) {
84 if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_RECORD) {
85 replay_char_read_all_save_error(res);
87 return res;
90 offset += res;
93 if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_RECORD) {
94 replay_char_read_all_save_buf(buf, offset);
96 return offset;
99 int qemu_chr_fe_ioctl(CharBackend *be, int cmd, void *arg)
101 Chardev *s = be->chr;
102 int res;
104 if (!s || !CHARDEV_GET_CLASS(s)->chr_ioctl || qemu_chr_replay(s)) {
105 res = -ENOTSUP;
106 } else {
107 res = CHARDEV_GET_CLASS(s)->chr_ioctl(s, cmd, arg);
110 return res;
113 int qemu_chr_fe_get_msgfd(CharBackend *be)
115 Chardev *s = be->chr;
116 int fd;
117 int res = (qemu_chr_fe_get_msgfds(be, &fd, 1) == 1) ? fd : -1;
118 if (s && qemu_chr_replay(s)) {
119 error_report("Replay: get msgfd is not supported "
120 "for serial devices yet");
121 exit(1);
123 return res;
126 int qemu_chr_fe_get_msgfds(CharBackend *be, int *fds, int len)
128 Chardev *s = be->chr;
130 if (!s) {
131 return -1;
134 return CHARDEV_GET_CLASS(s)->get_msgfds ?
135 CHARDEV_GET_CLASS(s)->get_msgfds(s, fds, len) : -1;
138 int qemu_chr_fe_set_msgfds(CharBackend *be, int *fds, int num)
140 Chardev *s = be->chr;
142 if (!s) {
143 return -1;
146 return CHARDEV_GET_CLASS(s)->set_msgfds ?
147 CHARDEV_GET_CLASS(s)->set_msgfds(s, fds, num) : -1;
150 void qemu_chr_fe_accept_input(CharBackend *be)
152 Chardev *s = be->chr;
154 if (!s) {
155 return;
158 if (CHARDEV_GET_CLASS(s)->chr_accept_input) {
159 CHARDEV_GET_CLASS(s)->chr_accept_input(s);
161 qemu_notify_event();
164 void qemu_chr_fe_printf(CharBackend *be, const char *fmt, ...)
166 char buf[CHR_READ_BUF_LEN];
167 va_list ap;
168 va_start(ap, fmt);
169 vsnprintf(buf, sizeof(buf), fmt, ap);
170 /* XXX this blocks entire thread. Rewrite to use
171 * qemu_chr_fe_write and background I/O callbacks */
172 qemu_chr_fe_write_all(be, (uint8_t *)buf, strlen(buf));
173 va_end(ap);
176 Chardev *qemu_chr_fe_get_driver(CharBackend *be)
178 /* this is unsafe for the users that support chardev hotswap */
179 assert(be->chr_be_change == NULL);
180 return be->chr;
183 bool qemu_chr_fe_backend_connected(CharBackend *be)
185 return !!be->chr;
188 bool qemu_chr_fe_backend_open(CharBackend *be)
190 return be->chr && be->chr->be_open;
193 bool qemu_chr_fe_init(CharBackend *b, Chardev *s, Error **errp)
195 int tag = 0;
197 if (s) {
198 if (CHARDEV_IS_MUX(s)) {
199 MuxChardev *d = MUX_CHARDEV(s);
201 if (d->mux_cnt >= MAX_MUX) {
202 goto unavailable;
205 d->backends[d->mux_cnt] = b;
206 tag = d->mux_cnt++;
207 } else if (s->be) {
208 goto unavailable;
209 } else {
210 s->be = b;
214 b->fe_open = false;
215 b->tag = tag;
216 b->chr = s;
217 return true;
219 unavailable:
220 error_setg(errp, QERR_DEVICE_IN_USE, s->label);
221 return false;
224 void qemu_chr_fe_deinit(CharBackend *b, bool del)
226 assert(b);
228 if (b->chr) {
229 qemu_chr_fe_set_handlers(b, NULL, NULL, NULL, NULL, NULL, NULL, true);
230 if (b->chr->be == b) {
231 b->chr->be = NULL;
233 if (CHARDEV_IS_MUX(b->chr)) {
234 MuxChardev *d = MUX_CHARDEV(b->chr);
235 d->backends[b->tag] = NULL;
237 if (del) {
238 Object *obj = OBJECT(b->chr);
239 if (obj->parent) {
240 object_unparent(obj);
241 } else {
242 object_unref(obj);
245 b->chr = NULL;
249 void qemu_chr_fe_set_handlers(CharBackend *b,
250 IOCanReadHandler *fd_can_read,
251 IOReadHandler *fd_read,
252 IOEventHandler *fd_event,
253 BackendChangeHandler *be_change,
254 void *opaque,
255 GMainContext *context,
256 bool set_open)
258 Chardev *s;
259 int fe_open;
261 s = b->chr;
262 if (!s) {
263 return;
266 if (!opaque && !fd_can_read && !fd_read && !fd_event) {
267 fe_open = 0;
268 remove_fd_in_watch(s);
269 } else {
270 fe_open = 1;
272 b->chr_can_read = fd_can_read;
273 b->chr_read = fd_read;
274 b->chr_event = fd_event;
275 b->chr_be_change = be_change;
276 b->opaque = opaque;
278 qemu_chr_be_update_read_handlers(s, context);
280 if (set_open) {
281 qemu_chr_fe_set_open(b, fe_open);
284 if (fe_open) {
285 qemu_chr_fe_take_focus(b);
286 /* We're connecting to an already opened device, so let's make sure we
287 also get the open event */
288 if (s->be_open) {
289 qemu_chr_be_event(s, CHR_EVENT_OPENED);
293 if (CHARDEV_IS_MUX(s)) {
294 mux_chr_set_handlers(s, context);
298 void qemu_chr_fe_take_focus(CharBackend *b)
300 if (!b->chr) {
301 return;
304 if (CHARDEV_IS_MUX(b->chr)) {
305 mux_set_focus(b->chr, b->tag);
309 int qemu_chr_fe_wait_connected(CharBackend *be, Error **errp)
311 if (!be->chr) {
312 error_setg(errp, "missing associated backend");
313 return -1;
316 return qemu_chr_wait_connected(be->chr, errp);
319 void qemu_chr_fe_set_echo(CharBackend *be, bool echo)
321 Chardev *chr = be->chr;
323 if (chr && CHARDEV_GET_CLASS(chr)->chr_set_echo) {
324 CHARDEV_GET_CLASS(chr)->chr_set_echo(chr, echo);
328 void qemu_chr_fe_set_open(CharBackend *be, int fe_open)
330 Chardev *chr = be->chr;
332 if (!chr) {
333 return;
336 if (be->fe_open == fe_open) {
337 return;
339 be->fe_open = fe_open;
340 if (CHARDEV_GET_CLASS(chr)->chr_set_fe_open) {
341 CHARDEV_GET_CLASS(chr)->chr_set_fe_open(chr, fe_open);
345 guint qemu_chr_fe_add_watch(CharBackend *be, GIOCondition cond,
346 GIOFunc func, void *user_data)
348 Chardev *s = be->chr;
349 GSource *src;
350 guint tag;
352 if (!s || CHARDEV_GET_CLASS(s)->chr_add_watch == NULL) {
353 return 0;
356 src = CHARDEV_GET_CLASS(s)->chr_add_watch(s, cond);
357 if (!src) {
358 return 0;
361 g_source_set_callback(src, (GSourceFunc)func, user_data, NULL);
362 tag = g_source_attach(src, s->gcontext);
363 g_source_unref(src);
365 return tag;
368 void qemu_chr_fe_disconnect(CharBackend *be)
370 Chardev *chr = be->chr;
372 if (chr && CHARDEV_GET_CLASS(chr)->chr_disconnect) {
373 CHARDEV_GET_CLASS(chr)->chr_disconnect(chr);