]> git.proxmox.com Git - mirror_qemu.git/blob - include/sysemu/char.h
char: move mux to its own file
[mirror_qemu.git] / include / sysemu / char.h
1 #ifndef QEMU_CHAR_H
2 #define QEMU_CHAR_H
3
4 #include "qemu-common.h"
5 #include "qemu/queue.h"
6 #include "qemu/option.h"
7 #include "qemu/config-file.h"
8 #include "block/aio.h"
9 #include "qapi/qmp/qobject.h"
10 #include "qapi/qmp/qstring.h"
11 #include "qemu/main-loop.h"
12 #include "qemu/bitmap.h"
13 #include "qom/object.h"
14
15 /* character device */
16
17 typedef enum {
18 CHR_EVENT_BREAK, /* serial break char */
19 CHR_EVENT_OPENED, /* new connection established */
20 CHR_EVENT_MUX_IN, /* mux-focus was set to this terminal */
21 CHR_EVENT_MUX_OUT, /* mux-focus will move on */
22 CHR_EVENT_CLOSED /* connection closed */
23 } QEMUChrEvent;
24
25
26 #define CHR_IOCTL_SERIAL_SET_PARAMS 1
27 typedef struct {
28 int speed;
29 int parity;
30 int data_bits;
31 int stop_bits;
32 } QEMUSerialSetParams;
33
34 #define CHR_IOCTL_SERIAL_SET_BREAK 2
35
36 #define CHR_IOCTL_PP_READ_DATA 3
37 #define CHR_IOCTL_PP_WRITE_DATA 4
38 #define CHR_IOCTL_PP_READ_CONTROL 5
39 #define CHR_IOCTL_PP_WRITE_CONTROL 6
40 #define CHR_IOCTL_PP_READ_STATUS 7
41 #define CHR_IOCTL_PP_EPP_READ_ADDR 8
42 #define CHR_IOCTL_PP_EPP_READ 9
43 #define CHR_IOCTL_PP_EPP_WRITE_ADDR 10
44 #define CHR_IOCTL_PP_EPP_WRITE 11
45 #define CHR_IOCTL_PP_DATA_DIR 12
46
47 struct ParallelIOArg {
48 void *buffer;
49 int count;
50 };
51
52 #define CHR_IOCTL_SERIAL_SET_TIOCM 13
53 #define CHR_IOCTL_SERIAL_GET_TIOCM 14
54
55 #define CHR_TIOCM_CTS 0x020
56 #define CHR_TIOCM_CAR 0x040
57 #define CHR_TIOCM_DSR 0x100
58 #define CHR_TIOCM_RI 0x080
59 #define CHR_TIOCM_DTR 0x002
60 #define CHR_TIOCM_RTS 0x004
61
62 typedef void IOEventHandler(void *opaque, int event);
63
64 typedef enum {
65 /* Whether the chardev peer is able to close and
66 * reopen the data channel, thus requiring support
67 * for qemu_chr_wait_connected() to wait for a
68 * valid connection */
69 QEMU_CHAR_FEATURE_RECONNECTABLE,
70 /* Whether it is possible to send/recv file descriptors
71 * over the data channel */
72 QEMU_CHAR_FEATURE_FD_PASS,
73 /* Whether replay or record mode is enabled */
74 QEMU_CHAR_FEATURE_REPLAY,
75
76 QEMU_CHAR_FEATURE_LAST,
77 } ChardevFeature;
78
79 /* This is the backend as seen by frontend, the actual backend is
80 * Chardev */
81 typedef struct CharBackend {
82 Chardev *chr;
83 IOEventHandler *chr_event;
84 IOCanReadHandler *chr_can_read;
85 IOReadHandler *chr_read;
86 void *opaque;
87 int tag;
88 int fe_open;
89 } CharBackend;
90
91 struct Chardev {
92 Object parent_obj;
93
94 QemuMutex chr_write_lock;
95 CharBackend *be;
96 char *label;
97 char *filename;
98 int logfd;
99 int be_open;
100 guint fd_in_tag;
101 DECLARE_BITMAP(features, QEMU_CHAR_FEATURE_LAST);
102 QTAILQ_ENTRY(Chardev) next;
103 };
104
105 /**
106 * @qemu_chr_new_from_opts:
107 *
108 * Create a new character backend from a QemuOpts list.
109 *
110 * @opts see qemu-config.c for a list of valid options
111 *
112 * Returns: a new character backend
113 */
114 Chardev *qemu_chr_new_from_opts(QemuOpts *opts,
115 Error **errp);
116
117 /**
118 * @qemu_chr_parse_common:
119 *
120 * Parse the common options available to all character backends.
121 *
122 * @opts the options that still need parsing
123 * @backend a new backend
124 */
125 void qemu_chr_parse_common(QemuOpts *opts, ChardevCommon *backend);
126
127 /**
128 * @qemu_chr_new:
129 *
130 * Create a new character backend from a URI.
131 *
132 * @label the name of the backend
133 * @filename the URI
134 *
135 * Returns: a new character backend
136 */
137 Chardev *qemu_chr_new(const char *label, const char *filename);
138
139
140 /**
141 * @qemu_chr_fe_disconnect:
142 *
143 * Close a fd accpeted by character backend.
144 * Without associated Chardev, do nothing.
145 */
146 void qemu_chr_fe_disconnect(CharBackend *be);
147
148 /**
149 * @qemu_chr_cleanup:
150 *
151 * Delete all chardevs (when leaving qemu)
152 */
153 void qemu_chr_cleanup(void);
154
155 /**
156 * @qemu_chr_fe_wait_connected:
157 *
158 * Wait for characted backend to be connected, return < 0 on error or
159 * if no assicated Chardev.
160 */
161 int qemu_chr_fe_wait_connected(CharBackend *be, Error **errp);
162
163 /**
164 * @qemu_chr_new_noreplay:
165 *
166 * Create a new character backend from a URI.
167 * Character device communications are not written
168 * into the replay log.
169 *
170 * @label the name of the backend
171 * @filename the URI
172 *
173 * Returns: a new character backend
174 */
175 Chardev *qemu_chr_new_noreplay(const char *label, const char *filename);
176
177 /**
178 * @qemu_chr_delete:
179 *
180 * Destroy a character backend and remove it from the list of
181 * identified character backends.
182 */
183 void qemu_chr_delete(Chardev *chr);
184
185 /**
186 * @qemu_chr_fe_set_echo:
187 *
188 * Ask the backend to override its normal echo setting. This only really
189 * applies to the stdio backend and is used by the QMP server such that you
190 * can see what you type if you try to type QMP commands.
191 * Without associated Chardev, do nothing.
192 *
193 * @echo true to enable echo, false to disable echo
194 */
195 void qemu_chr_fe_set_echo(CharBackend *be, bool echo);
196
197 /**
198 * @qemu_chr_fe_set_open:
199 *
200 * Set character frontend open status. This is an indication that the
201 * front end is ready (or not) to begin doing I/O.
202 * Without associated Chardev, do nothing.
203 */
204 void qemu_chr_fe_set_open(CharBackend *be, int fe_open);
205
206 /**
207 * @qemu_chr_fe_printf:
208 *
209 * Write to a character backend using a printf style interface. This
210 * function is thread-safe. It does nothing without associated
211 * Chardev.
212 *
213 * @fmt see #printf
214 */
215 void qemu_chr_fe_printf(CharBackend *be, const char *fmt, ...)
216 GCC_FMT_ATTR(2, 3);
217
218 /**
219 * @qemu_chr_fe_add_watch:
220 *
221 * If the backend is connected, create and add a #GSource that fires
222 * when the given condition (typically G_IO_OUT|G_IO_HUP or G_IO_HUP)
223 * is active; return the #GSource's tag. If it is disconnected,
224 * or without associated Chardev, return 0.
225 *
226 * @cond the condition to poll for
227 * @func the function to call when the condition happens
228 * @user_data the opaque pointer to pass to @func
229 *
230 * Returns: the source tag
231 */
232 guint qemu_chr_fe_add_watch(CharBackend *be, GIOCondition cond,
233 GIOFunc func, void *user_data);
234
235 /**
236 * @qemu_chr_fe_write:
237 *
238 * Write data to a character backend from the front end. This function
239 * will send data from the front end to the back end. This function
240 * is thread-safe.
241 *
242 * @buf the data
243 * @len the number of bytes to send
244 *
245 * Returns: the number of bytes consumed (0 if no assicated Chardev)
246 */
247 int qemu_chr_fe_write(CharBackend *be, const uint8_t *buf, int len);
248
249 /**
250 * @qemu_chr_fe_write_all:
251 *
252 * Write data to a character backend from the front end. This function will
253 * send data from the front end to the back end. Unlike @qemu_chr_fe_write,
254 * this function will block if the back end cannot consume all of the data
255 * attempted to be written. This function is thread-safe.
256 *
257 * @buf the data
258 * @len the number of bytes to send
259 *
260 * Returns: the number of bytes consumed (0 if no assicated Chardev)
261 */
262 int qemu_chr_fe_write_all(CharBackend *be, const uint8_t *buf, int len);
263
264 /**
265 * @qemu_chr_fe_read_all:
266 *
267 * Read data to a buffer from the back end.
268 *
269 * @buf the data buffer
270 * @len the number of bytes to read
271 *
272 * Returns: the number of bytes read (0 if no assicated Chardev)
273 */
274 int qemu_chr_fe_read_all(CharBackend *be, uint8_t *buf, int len);
275
276 /**
277 * @qemu_chr_fe_ioctl:
278 *
279 * Issue a device specific ioctl to a backend. This function is thread-safe.
280 *
281 * @cmd see CHR_IOCTL_*
282 * @arg the data associated with @cmd
283 *
284 * Returns: if @cmd is not supported by the backend or there is no
285 * associated Chardev, -ENOTSUP, otherwise the return
286 * value depends on the semantics of @cmd
287 */
288 int qemu_chr_fe_ioctl(CharBackend *be, int cmd, void *arg);
289
290 /**
291 * @qemu_chr_fe_get_msgfd:
292 *
293 * For backends capable of fd passing, return the latest file descriptor passed
294 * by a client.
295 *
296 * Returns: -1 if fd passing isn't supported or there is no pending file
297 * descriptor. If a file descriptor is returned, subsequent calls to
298 * this function will return -1 until a client sends a new file
299 * descriptor.
300 */
301 int qemu_chr_fe_get_msgfd(CharBackend *be);
302
303 /**
304 * @qemu_chr_fe_get_msgfds:
305 *
306 * For backends capable of fd passing, return the number of file received
307 * descriptors and fills the fds array up to num elements
308 *
309 * Returns: -1 if fd passing isn't supported or there are no pending file
310 * descriptors. If file descriptors are returned, subsequent calls to
311 * this function will return -1 until a client sends a new set of file
312 * descriptors.
313 */
314 int qemu_chr_fe_get_msgfds(CharBackend *be, int *fds, int num);
315
316 /**
317 * @qemu_chr_fe_set_msgfds:
318 *
319 * For backends capable of fd passing, set an array of fds to be passed with
320 * the next send operation.
321 * A subsequent call to this function before calling a write function will
322 * result in overwriting the fd array with the new value without being send.
323 * Upon writing the message the fd array is freed.
324 *
325 * Returns: -1 if fd passing isn't supported or no associated Chardev.
326 */
327 int qemu_chr_fe_set_msgfds(CharBackend *be, int *fds, int num);
328
329 /**
330 * @qemu_chr_be_can_write:
331 *
332 * Determine how much data the front end can currently accept. This function
333 * returns the number of bytes the front end can accept. If it returns 0, the
334 * front end cannot receive data at the moment. The function must be polled
335 * to determine when data can be received.
336 *
337 * Returns: the number of bytes the front end can receive via @qemu_chr_be_write
338 */
339 int qemu_chr_be_can_write(Chardev *s);
340
341 /**
342 * @qemu_chr_be_write:
343 *
344 * Write data from the back end to the front end. Before issuing this call,
345 * the caller should call @qemu_chr_be_can_write to determine how much data
346 * the front end can currently accept.
347 *
348 * @buf a buffer to receive data from the front end
349 * @len the number of bytes to receive from the front end
350 */
351 void qemu_chr_be_write(Chardev *s, uint8_t *buf, int len);
352
353 /**
354 * @qemu_chr_be_write_impl:
355 *
356 * Implementation of back end writing. Used by replay module.
357 *
358 * @buf a buffer to receive data from the front end
359 * @len the number of bytes to receive from the front end
360 */
361 void qemu_chr_be_write_impl(Chardev *s, uint8_t *buf, int len);
362
363 /**
364 * @qemu_chr_be_event:
365 *
366 * Send an event from the back end to the front end.
367 *
368 * @event the event to send
369 */
370 void qemu_chr_be_event(Chardev *s, int event);
371
372 /**
373 * @qemu_chr_fe_init:
374 *
375 * Initializes a front end for the given CharBackend and
376 * Chardev. Call qemu_chr_fe_deinit() to remove the association and
377 * release the driver.
378 *
379 * Returns: false on error.
380 */
381 bool qemu_chr_fe_init(CharBackend *b, Chardev *s, Error **errp);
382
383 /**
384 * @qemu_chr_fe_get_driver:
385 *
386 * Returns the driver associated with a CharBackend or NULL if no
387 * associated Chardev.
388 */
389 Chardev *qemu_chr_fe_get_driver(CharBackend *be);
390
391 /**
392 * @qemu_chr_fe_deinit:
393 *
394 * Dissociate the CharBackend from the Chardev.
395 *
396 * Safe to call without associated Chardev.
397 */
398 void qemu_chr_fe_deinit(CharBackend *b);
399
400 /**
401 * @qemu_chr_fe_set_handlers:
402 * @b: a CharBackend
403 * @fd_can_read: callback to get the amount of data the frontend may
404 * receive
405 * @fd_read: callback to receive data from char
406 * @fd_event: event callback
407 * @opaque: an opaque pointer for the callbacks
408 * @context: a main loop context or NULL for the default
409 * @set_open: whether to call qemu_chr_fe_set_open() implicitely when
410 * any of the handler is non-NULL
411 *
412 * Set the front end char handlers. The front end takes the focus if
413 * any of the handler is non-NULL.
414 *
415 * Without associated Chardev, nothing is changed.
416 */
417 void qemu_chr_fe_set_handlers(CharBackend *b,
418 IOCanReadHandler *fd_can_read,
419 IOReadHandler *fd_read,
420 IOEventHandler *fd_event,
421 void *opaque,
422 GMainContext *context,
423 bool set_open);
424
425 /**
426 * @qemu_chr_fe_take_focus:
427 *
428 * Take the focus (if the front end is muxed).
429 *
430 * Without associated Chardev, nothing is changed.
431 */
432 void qemu_chr_fe_take_focus(CharBackend *b);
433
434 void qemu_chr_be_generic_open(Chardev *s);
435 void qemu_chr_fe_accept_input(CharBackend *be);
436 int qemu_chr_add_client(Chardev *s, int fd);
437 Chardev *qemu_chr_find(const char *name);
438
439 bool qemu_chr_has_feature(Chardev *chr,
440 ChardevFeature feature);
441 void qemu_chr_set_feature(Chardev *chr,
442 ChardevFeature feature);
443 QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename);
444 int qemu_chr_write_all(Chardev *s, const uint8_t *buf, int len);
445
446 #define TYPE_CHARDEV "chardev"
447 #define CHARDEV(obj) OBJECT_CHECK(Chardev, (obj), TYPE_CHARDEV)
448 #define CHARDEV_CLASS(klass) \
449 OBJECT_CLASS_CHECK(ChardevClass, (klass), TYPE_CHARDEV)
450 #define CHARDEV_GET_CLASS(obj) \
451 OBJECT_GET_CLASS(ChardevClass, (obj), TYPE_CHARDEV)
452
453 #define TYPE_CHARDEV_NULL "chardev-null"
454 #define TYPE_CHARDEV_MUX "chardev-mux"
455 #define TYPE_CHARDEV_RINGBUF "chardev-ringbuf"
456 #define TYPE_CHARDEV_PTY "chardev-pty"
457 #define TYPE_CHARDEV_CONSOLE "chardev-console"
458 #define TYPE_CHARDEV_STDIO "chardev-stdio"
459 #define TYPE_CHARDEV_PIPE "chardev-pipe"
460 #define TYPE_CHARDEV_MEMORY "chardev-memory"
461 #define TYPE_CHARDEV_PARALLEL "chardev-parallel"
462 #define TYPE_CHARDEV_FILE "chardev-file"
463 #define TYPE_CHARDEV_SERIAL "chardev-serial"
464 #define TYPE_CHARDEV_SOCKET "chardev-socket"
465 #define TYPE_CHARDEV_UDP "chardev-udp"
466
467 #define CHARDEV_IS_RINGBUF(chr) \
468 object_dynamic_cast(OBJECT(chr), TYPE_CHARDEV_RINGBUF)
469 #define CHARDEV_IS_PTY(chr) \
470 object_dynamic_cast(OBJECT(chr), TYPE_CHARDEV_PTY)
471
472 typedef struct ChardevClass {
473 ObjectClass parent_class;
474
475 bool internal; /* TODO: eventually use TYPE_USER_CREATABLE */
476 void (*parse)(QemuOpts *opts, ChardevBackend *backend, Error **errp);
477
478 void (*open)(Chardev *chr, ChardevBackend *backend,
479 bool *be_opened, Error **errp);
480
481 int (*chr_write)(Chardev *s, const uint8_t *buf, int len);
482 int (*chr_sync_read)(Chardev *s, const uint8_t *buf, int len);
483 GSource *(*chr_add_watch)(Chardev *s, GIOCondition cond);
484 void (*chr_update_read_handler)(Chardev *s, GMainContext *context);
485 int (*chr_ioctl)(Chardev *s, int cmd, void *arg);
486 int (*get_msgfds)(Chardev *s, int* fds, int num);
487 int (*set_msgfds)(Chardev *s, int *fds, int num);
488 int (*chr_add_client)(Chardev *chr, int fd);
489 int (*chr_wait_connected)(Chardev *chr, Error **errp);
490 void (*chr_disconnect)(Chardev *chr);
491 void (*chr_accept_input)(Chardev *chr);
492 void (*chr_set_echo)(Chardev *chr, bool echo);
493 void (*chr_set_fe_open)(Chardev *chr, int fe_open);
494 } ChardevClass;
495
496 Chardev *qemu_chardev_new(const char *id, const char *typename,
497 ChardevBackend *backend, Error **errp);
498
499 extern int term_escape_char;
500
501 /* console.c */
502 void qemu_chr_parse_vc(QemuOpts *opts, ChardevBackend *backend, Error **errp);
503
504 #endif