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