block: drop empty .bdrv_close handlers
[qemu/ar7.git] / chardev / char-fe.c
blobb1f228e8b573dc465d2f3d09fb9c8445304fcaf9
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, counter = 10;
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;
92 if (!counter--) {
93 break;
97 if (qemu_chr_replay(s) && replay_mode == REPLAY_MODE_RECORD) {
98 replay_char_read_all_save_buf(buf, offset);
100 return offset;
103 int qemu_chr_fe_ioctl(CharBackend *be, int cmd, void *arg)
105 Chardev *s = be->chr;
106 int res;
108 if (!s || !CHARDEV_GET_CLASS(s)->chr_ioctl || qemu_chr_replay(s)) {
109 res = -ENOTSUP;
110 } else {
111 res = CHARDEV_GET_CLASS(s)->chr_ioctl(s, cmd, arg);
114 return res;
117 int qemu_chr_fe_get_msgfd(CharBackend *be)
119 Chardev *s = be->chr;
120 int fd;
121 int res = (qemu_chr_fe_get_msgfds(be, &fd, 1) == 1) ? fd : -1;
122 if (s && qemu_chr_replay(s)) {
123 error_report("Replay: get msgfd is not supported "
124 "for serial devices yet");
125 exit(1);
127 return res;
130 int qemu_chr_fe_get_msgfds(CharBackend *be, int *fds, int len)
132 Chardev *s = be->chr;
134 if (!s) {
135 return -1;
138 return CHARDEV_GET_CLASS(s)->get_msgfds ?
139 CHARDEV_GET_CLASS(s)->get_msgfds(s, fds, len) : -1;
142 int qemu_chr_fe_set_msgfds(CharBackend *be, int *fds, int num)
144 Chardev *s = be->chr;
146 if (!s) {
147 return -1;
150 return CHARDEV_GET_CLASS(s)->set_msgfds ?
151 CHARDEV_GET_CLASS(s)->set_msgfds(s, fds, num) : -1;
154 void qemu_chr_fe_accept_input(CharBackend *be)
156 Chardev *s = be->chr;
158 if (!s) {
159 return;
162 if (CHARDEV_GET_CLASS(s)->chr_accept_input) {
163 CHARDEV_GET_CLASS(s)->chr_accept_input(s);
165 qemu_notify_event();
168 void qemu_chr_fe_printf(CharBackend *be, const char *fmt, ...)
170 char buf[CHR_READ_BUF_LEN];
171 va_list ap;
172 va_start(ap, fmt);
173 vsnprintf(buf, sizeof(buf), fmt, ap);
174 /* XXX this blocks entire thread. Rewrite to use
175 * qemu_chr_fe_write and background I/O callbacks */
176 qemu_chr_fe_write_all(be, (uint8_t *)buf, strlen(buf));
177 va_end(ap);
180 Chardev *qemu_chr_fe_get_driver(CharBackend *be)
182 /* this is unsafe for the users that support chardev hotswap */
183 assert(be->chr_be_change == NULL);
184 return be->chr;
187 bool qemu_chr_fe_backend_connected(CharBackend *be)
189 return !!be->chr;
192 bool qemu_chr_fe_backend_open(CharBackend *be)
194 return be->chr && be->chr->be_open;
197 bool qemu_chr_fe_init(CharBackend *b, Chardev *s, Error **errp)
199 int tag = 0;
201 if (s) {
202 if (CHARDEV_IS_MUX(s)) {
203 MuxChardev *d = MUX_CHARDEV(s);
205 if (d->mux_cnt >= MAX_MUX) {
206 goto unavailable;
209 d->backends[d->mux_cnt] = b;
210 tag = d->mux_cnt++;
211 } else if (s->be) {
212 goto unavailable;
213 } else {
214 s->be = b;
218 b->fe_open = false;
219 b->tag = tag;
220 b->chr = s;
221 return true;
223 unavailable:
224 error_setg(errp, QERR_DEVICE_IN_USE, s->label);
225 return false;
228 void qemu_chr_fe_deinit(CharBackend *b, bool del)
230 assert(b);
232 if (b->chr) {
233 qemu_chr_fe_set_handlers(b, NULL, NULL, NULL, NULL, NULL, NULL, true);
234 if (b->chr->be == b) {
235 b->chr->be = NULL;
237 if (CHARDEV_IS_MUX(b->chr)) {
238 MuxChardev *d = MUX_CHARDEV(b->chr);
239 d->backends[b->tag] = NULL;
241 if (del) {
242 object_unparent(OBJECT(b->chr));
244 b->chr = NULL;
248 void qemu_chr_fe_set_handlers(CharBackend *b,
249 IOCanReadHandler *fd_can_read,
250 IOReadHandler *fd_read,
251 IOEventHandler *fd_event,
252 BackendChangeHandler *be_change,
253 void *opaque,
254 GMainContext *context,
255 bool set_open)
257 Chardev *s;
258 int fe_open;
260 s = b->chr;
261 if (!s) {
262 return;
265 if (!opaque && !fd_can_read && !fd_read && !fd_event) {
266 fe_open = 0;
267 remove_fd_in_watch(s);
268 } else {
269 fe_open = 1;
271 b->chr_can_read = fd_can_read;
272 b->chr_read = fd_read;
273 b->chr_event = fd_event;
274 b->chr_be_change = be_change;
275 b->opaque = opaque;
277 qemu_chr_be_update_read_handlers(s, context);
279 if (set_open) {
280 qemu_chr_fe_set_open(b, fe_open);
283 if (fe_open) {
284 qemu_chr_fe_take_focus(b);
285 /* We're connecting to an already opened device, so let's make sure we
286 also get the open event */
287 if (s->be_open) {
288 qemu_chr_be_event(s, CHR_EVENT_OPENED);
292 if (CHARDEV_IS_MUX(s)) {
293 mux_chr_set_handlers(s, context);
297 void qemu_chr_fe_take_focus(CharBackend *b)
299 if (!b->chr) {
300 return;
303 if (CHARDEV_IS_MUX(b->chr)) {
304 mux_set_focus(b->chr, b->tag);
308 int qemu_chr_fe_wait_connected(CharBackend *be, Error **errp)
310 if (!be->chr) {
311 error_setg(errp, "missing associated backend");
312 return -1;
315 return qemu_chr_wait_connected(be->chr, errp);
318 void qemu_chr_fe_set_echo(CharBackend *be, bool echo)
320 Chardev *chr = be->chr;
322 if (chr && CHARDEV_GET_CLASS(chr)->chr_set_echo) {
323 CHARDEV_GET_CLASS(chr)->chr_set_echo(chr, echo);
327 void qemu_chr_fe_set_open(CharBackend *be, int fe_open)
329 Chardev *chr = be->chr;
331 if (!chr) {
332 return;
335 if (be->fe_open == fe_open) {
336 return;
338 be->fe_open = fe_open;
339 if (CHARDEV_GET_CLASS(chr)->chr_set_fe_open) {
340 CHARDEV_GET_CLASS(chr)->chr_set_fe_open(chr, fe_open);
344 guint qemu_chr_fe_add_watch(CharBackend *be, GIOCondition cond,
345 GIOFunc func, void *user_data)
347 Chardev *s = be->chr;
348 GSource *src;
349 guint tag;
351 if (!s || CHARDEV_GET_CLASS(s)->chr_add_watch == NULL) {
352 return 0;
355 src = CHARDEV_GET_CLASS(s)->chr_add_watch(s, cond);
356 if (!src) {
357 return 0;
360 g_source_set_callback(src, (GSourceFunc)func, user_data, NULL);
361 tag = g_source_attach(src, s->gcontext);
362 g_source_unref(src);
364 return tag;
367 void qemu_chr_fe_disconnect(CharBackend *be)
369 Chardev *chr = be->chr;
371 if (chr && CHARDEV_GET_CLASS(chr)->chr_disconnect) {
372 CHARDEV_GET_CLASS(chr)->chr_disconnect(chr);