4 #include "qemu-common.h"
5 #include "qemu/option.h"
6 #include "qemu/main-loop.h"
7 #include "qemu/bitmap.h"
8 #include "qom/object.h"
18 /* character device */
21 CHR_EVENT_BREAK
, /* serial break char */
22 CHR_EVENT_OPENED
, /* new connection established */
23 CHR_EVENT_MUX_IN
, /* mux-focus was set to this terminal */
24 CHR_EVENT_MUX_OUT
, /* mux-focus will move on */
25 CHR_EVENT_CLOSED
/* connection closed */
28 #define CHR_READ_BUF_LEN 4096
30 #define CHR_IOCTL_SERIAL_SET_PARAMS 1
36 } QEMUSerialSetParams
;
38 #define CHR_IOCTL_SERIAL_SET_BREAK 2
40 #define CHR_IOCTL_PP_READ_DATA 3
41 #define CHR_IOCTL_PP_WRITE_DATA 4
42 #define CHR_IOCTL_PP_READ_CONTROL 5
43 #define CHR_IOCTL_PP_WRITE_CONTROL 6
44 #define CHR_IOCTL_PP_READ_STATUS 7
45 #define CHR_IOCTL_PP_EPP_READ_ADDR 8
46 #define CHR_IOCTL_PP_EPP_READ 9
47 #define CHR_IOCTL_PP_EPP_WRITE_ADDR 10
48 #define CHR_IOCTL_PP_EPP_WRITE 11
49 #define CHR_IOCTL_PP_DATA_DIR 12
51 struct ParallelIOArg
{
56 #define CHR_IOCTL_SERIAL_SET_TIOCM 13
57 #define CHR_IOCTL_SERIAL_GET_TIOCM 14
59 #define CHR_TIOCM_CTS 0x020
60 #define CHR_TIOCM_CAR 0x040
61 #define CHR_TIOCM_DSR 0x100
62 #define CHR_TIOCM_RI 0x080
63 #define CHR_TIOCM_DTR 0x002
64 #define CHR_TIOCM_RTS 0x004
66 typedef void IOEventHandler(void *opaque
, int event
);
69 /* Whether the chardev peer is able to close and
70 * reopen the data channel, thus requiring support
71 * for qemu_chr_wait_connected() to wait for a
73 QEMU_CHAR_FEATURE_RECONNECTABLE
,
74 /* Whether it is possible to send/recv file descriptors
75 * over the data channel */
76 QEMU_CHAR_FEATURE_FD_PASS
,
77 /* Whether replay or record mode is enabled */
78 QEMU_CHAR_FEATURE_REPLAY
,
80 QEMU_CHAR_FEATURE_LAST
,
83 /* This is the backend as seen by frontend, the actual backend is
85 typedef struct CharBackend
{
87 IOEventHandler
*chr_event
;
88 IOCanReadHandler
*chr_can_read
;
89 IOReadHandler
*chr_read
;
98 QemuMutex chr_write_lock
;
105 DECLARE_BITMAP(features
, QEMU_CHAR_FEATURE_LAST
);
106 QTAILQ_ENTRY(Chardev
) next
;
110 * @qemu_chr_new_from_opts:
112 * Create a new character backend from a QemuOpts list.
114 * @opts see qemu-config.c for a list of valid options
116 * Returns: a new character backend
118 Chardev
*qemu_chr_new_from_opts(QemuOpts
*opts
,
122 * @qemu_chr_parse_common:
124 * Parse the common options available to all character backends.
126 * @opts the options that still need parsing
127 * @backend a new backend
129 void qemu_chr_parse_common(QemuOpts
*opts
, ChardevCommon
*backend
);
134 * Create a new character backend from a URI.
136 * @label the name of the backend
139 * Returns: a new character backend
141 Chardev
*qemu_chr_new(const char *label
, const char *filename
);
145 * @qemu_chr_fe_disconnect:
147 * Close a fd accpeted by character backend.
148 * Without associated Chardev, do nothing.
150 void qemu_chr_fe_disconnect(CharBackend
*be
);
155 * Delete all chardevs (when leaving qemu)
157 void qemu_chr_cleanup(void);
160 * @qemu_chr_fe_wait_connected:
162 * Wait for characted backend to be connected, return < 0 on error or
163 * if no assicated Chardev.
165 int qemu_chr_fe_wait_connected(CharBackend
*be
, Error
**errp
);
168 * @qemu_chr_new_noreplay:
170 * Create a new character backend from a URI.
171 * Character device communications are not written
172 * into the replay log.
174 * @label the name of the backend
177 * Returns: a new character backend
179 Chardev
*qemu_chr_new_noreplay(const char *label
, const char *filename
);
184 * Destroy a character backend and remove it from the list of
185 * identified character backends.
187 void qemu_chr_delete(Chardev
*chr
);
190 * @qemu_chr_fe_set_echo:
192 * Ask the backend to override its normal echo setting. This only really
193 * applies to the stdio backend and is used by the QMP server such that you
194 * can see what you type if you try to type QMP commands.
195 * Without associated Chardev, do nothing.
197 * @echo true to enable echo, false to disable echo
199 void qemu_chr_fe_set_echo(CharBackend
*be
, bool echo
);
202 * @qemu_chr_fe_set_open:
204 * Set character frontend open status. This is an indication that the
205 * front end is ready (or not) to begin doing I/O.
206 * Without associated Chardev, do nothing.
208 void qemu_chr_fe_set_open(CharBackend
*be
, int fe_open
);
211 * @qemu_chr_fe_printf:
213 * Write to a character backend using a printf style interface. This
214 * function is thread-safe. It does nothing without associated
219 void qemu_chr_fe_printf(CharBackend
*be
, const char *fmt
, ...)
223 * @qemu_chr_fe_add_watch:
225 * If the backend is connected, create and add a #GSource that fires
226 * when the given condition (typically G_IO_OUT|G_IO_HUP or G_IO_HUP)
227 * is active; return the #GSource's tag. If it is disconnected,
228 * or without associated Chardev, return 0.
230 * @cond the condition to poll for
231 * @func the function to call when the condition happens
232 * @user_data the opaque pointer to pass to @func
234 * Returns: the source tag
236 guint
qemu_chr_fe_add_watch(CharBackend
*be
, GIOCondition cond
,
237 GIOFunc func
, void *user_data
);
240 * @qemu_chr_fe_write:
242 * Write data to a character backend from the front end. This function
243 * will send data from the front end to the back end. This function
247 * @len the number of bytes to send
249 * Returns: the number of bytes consumed (0 if no assicated Chardev)
251 int qemu_chr_fe_write(CharBackend
*be
, const uint8_t *buf
, int len
);
254 * @qemu_chr_fe_write_all:
256 * Write data to a character backend from the front end. This function will
257 * send data from the front end to the back end. Unlike @qemu_chr_fe_write,
258 * this function will block if the back end cannot consume all of the data
259 * attempted to be written. This function is thread-safe.
262 * @len the number of bytes to send
264 * Returns: the number of bytes consumed (0 if no assicated Chardev)
266 int qemu_chr_fe_write_all(CharBackend
*be
, const uint8_t *buf
, int len
);
269 * @qemu_chr_fe_read_all:
271 * Read data to a buffer from the back end.
273 * @buf the data buffer
274 * @len the number of bytes to read
276 * Returns: the number of bytes read (0 if no assicated Chardev)
278 int qemu_chr_fe_read_all(CharBackend
*be
, uint8_t *buf
, int len
);
281 * @qemu_chr_fe_ioctl:
283 * Issue a device specific ioctl to a backend. This function is thread-safe.
285 * @cmd see CHR_IOCTL_*
286 * @arg the data associated with @cmd
288 * Returns: if @cmd is not supported by the backend or there is no
289 * associated Chardev, -ENOTSUP, otherwise the return
290 * value depends on the semantics of @cmd
292 int qemu_chr_fe_ioctl(CharBackend
*be
, int cmd
, void *arg
);
295 * @qemu_chr_fe_get_msgfd:
297 * For backends capable of fd passing, return the latest file descriptor passed
300 * Returns: -1 if fd passing isn't supported or there is no pending file
301 * descriptor. If a file descriptor is returned, subsequent calls to
302 * this function will return -1 until a client sends a new file
305 int qemu_chr_fe_get_msgfd(CharBackend
*be
);
308 * @qemu_chr_fe_get_msgfds:
310 * For backends capable of fd passing, return the number of file received
311 * descriptors and fills the fds array up to num elements
313 * Returns: -1 if fd passing isn't supported or there are no pending file
314 * descriptors. If file descriptors are returned, subsequent calls to
315 * this function will return -1 until a client sends a new set of file
318 int qemu_chr_fe_get_msgfds(CharBackend
*be
, int *fds
, int num
);
321 * @qemu_chr_fe_set_msgfds:
323 * For backends capable of fd passing, set an array of fds to be passed with
324 * the next send operation.
325 * A subsequent call to this function before calling a write function will
326 * result in overwriting the fd array with the new value without being send.
327 * Upon writing the message the fd array is freed.
329 * Returns: -1 if fd passing isn't supported or no associated Chardev.
331 int qemu_chr_fe_set_msgfds(CharBackend
*be
, int *fds
, int num
);
334 * @qemu_chr_be_can_write:
336 * Determine how much data the front end can currently accept. This function
337 * returns the number of bytes the front end can accept. If it returns 0, the
338 * front end cannot receive data at the moment. The function must be polled
339 * to determine when data can be received.
341 * Returns: the number of bytes the front end can receive via @qemu_chr_be_write
343 int qemu_chr_be_can_write(Chardev
*s
);
346 * @qemu_chr_be_write:
348 * Write data from the back end to the front end. Before issuing this call,
349 * the caller should call @qemu_chr_be_can_write to determine how much data
350 * the front end can currently accept.
352 * @buf a buffer to receive data from the front end
353 * @len the number of bytes to receive from the front end
355 void qemu_chr_be_write(Chardev
*s
, uint8_t *buf
, int len
);
358 * @qemu_chr_be_write_impl:
360 * Implementation of back end writing. Used by replay module.
362 * @buf a buffer to receive data from the front end
363 * @len the number of bytes to receive from the front end
365 void qemu_chr_be_write_impl(Chardev
*s
, uint8_t *buf
, int len
);
368 * @qemu_chr_be_event:
370 * Send an event from the back end to the front end.
372 * @event the event to send
374 void qemu_chr_be_event(Chardev
*s
, int event
);
379 * Initializes a front end for the given CharBackend and
380 * Chardev. Call qemu_chr_fe_deinit() to remove the association and
381 * release the driver.
383 * Returns: false on error.
385 bool qemu_chr_fe_init(CharBackend
*b
, Chardev
*s
, Error
**errp
);
388 * @qemu_chr_fe_get_driver:
390 * Returns the driver associated with a CharBackend or NULL if no
391 * associated Chardev.
393 Chardev
*qemu_chr_fe_get_driver(CharBackend
*be
);
396 * @qemu_chr_fe_deinit:
398 * Dissociate the CharBackend from the Chardev.
400 * Safe to call without associated Chardev.
402 void qemu_chr_fe_deinit(CharBackend
*b
);
405 * @qemu_chr_fe_set_handlers:
407 * @fd_can_read: callback to get the amount of data the frontend may
409 * @fd_read: callback to receive data from char
410 * @fd_event: event callback
411 * @opaque: an opaque pointer for the callbacks
412 * @context: a main loop context or NULL for the default
413 * @set_open: whether to call qemu_chr_fe_set_open() implicitely when
414 * any of the handler is non-NULL
416 * Set the front end char handlers. The front end takes the focus if
417 * any of the handler is non-NULL.
419 * Without associated Chardev, nothing is changed.
421 void qemu_chr_fe_set_handlers(CharBackend
*b
,
422 IOCanReadHandler
*fd_can_read
,
423 IOReadHandler
*fd_read
,
424 IOEventHandler
*fd_event
,
426 GMainContext
*context
,
430 * @qemu_chr_fe_take_focus:
432 * Take the focus (if the front end is muxed).
434 * Without associated Chardev, nothing is changed.
436 void qemu_chr_fe_take_focus(CharBackend
*b
);
438 void qemu_chr_be_generic_open(Chardev
*s
);
439 void qemu_chr_fe_accept_input(CharBackend
*be
);
440 int qemu_chr_add_client(Chardev
*s
, int fd
);
441 Chardev
*qemu_chr_find(const char *name
);
443 bool qemu_chr_has_feature(Chardev
*chr
,
444 ChardevFeature feature
);
445 void qemu_chr_set_feature(Chardev
*chr
,
446 ChardevFeature feature
);
447 QemuOpts
*qemu_chr_parse_compat(const char *label
, const char *filename
);
448 int qemu_chr_write_all(Chardev
*s
, const uint8_t *buf
, int len
);
449 int qemu_chr_wait_connected(Chardev
*chr
, Error
**errp
);
451 #define TYPE_CHARDEV "chardev"
452 #define CHARDEV(obj) OBJECT_CHECK(Chardev, (obj), TYPE_CHARDEV)
453 #define CHARDEV_CLASS(klass) \
454 OBJECT_CLASS_CHECK(ChardevClass, (klass), TYPE_CHARDEV)
455 #define CHARDEV_GET_CLASS(obj) \
456 OBJECT_GET_CLASS(ChardevClass, (obj), TYPE_CHARDEV)
458 #define TYPE_CHARDEV_NULL "chardev-null"
459 #define TYPE_CHARDEV_MUX "chardev-mux"
460 #define TYPE_CHARDEV_RINGBUF "chardev-ringbuf"
461 #define TYPE_CHARDEV_PTY "chardev-pty"
462 #define TYPE_CHARDEV_CONSOLE "chardev-console"
463 #define TYPE_CHARDEV_STDIO "chardev-stdio"
464 #define TYPE_CHARDEV_PIPE "chardev-pipe"
465 #define TYPE_CHARDEV_MEMORY "chardev-memory"
466 #define TYPE_CHARDEV_PARALLEL "chardev-parallel"
467 #define TYPE_CHARDEV_FILE "chardev-file"
468 #define TYPE_CHARDEV_SERIAL "chardev-serial"
469 #define TYPE_CHARDEV_SOCKET "chardev-socket"
470 #define TYPE_CHARDEV_UDP "chardev-udp"
472 #define CHARDEV_IS_RINGBUF(chr) \
473 object_dynamic_cast(OBJECT(chr), TYPE_CHARDEV_RINGBUF)
474 #define CHARDEV_IS_PTY(chr) \
475 object_dynamic_cast(OBJECT(chr), TYPE_CHARDEV_PTY)
477 typedef struct ChardevClass
{
478 ObjectClass parent_class
;
480 bool internal
; /* TODO: eventually use TYPE_USER_CREATABLE */
481 void (*parse
)(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
);
483 void (*open
)(Chardev
*chr
, ChardevBackend
*backend
,
484 bool *be_opened
, Error
**errp
);
486 int (*chr_write
)(Chardev
*s
, const uint8_t *buf
, int len
);
487 int (*chr_sync_read
)(Chardev
*s
, const uint8_t *buf
, int len
);
488 GSource
*(*chr_add_watch
)(Chardev
*s
, GIOCondition cond
);
489 void (*chr_update_read_handler
)(Chardev
*s
, GMainContext
*context
);
490 int (*chr_ioctl
)(Chardev
*s
, int cmd
, void *arg
);
491 int (*get_msgfds
)(Chardev
*s
, int* fds
, int num
);
492 int (*set_msgfds
)(Chardev
*s
, int *fds
, int num
);
493 int (*chr_add_client
)(Chardev
*chr
, int fd
);
494 int (*chr_wait_connected
)(Chardev
*chr
, Error
**errp
);
495 void (*chr_disconnect
)(Chardev
*chr
);
496 void (*chr_accept_input
)(Chardev
*chr
);
497 void (*chr_set_echo
)(Chardev
*chr
, bool echo
);
498 void (*chr_set_fe_open
)(Chardev
*chr
, int fe_open
);
501 Chardev
*qemu_chardev_new(const char *id
, const char *typename
,
502 ChardevBackend
*backend
, Error
**errp
);
504 extern int term_escape_char
;
507 void qemu_chr_parse_vc(QemuOpts
*opts
, ChardevBackend
*backend
, Error
**errp
);