xref: /qemu/chardev/chardev-internal.h (revision b66ed232383162dc52d6a1c996599541860d1f1a)
1 /*
2  * QEMU Character device internals
3  *
4  * Copyright (c) 2003-2008 Fabrice Bellard
5  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
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.
23  */
24 
25 #ifndef CHARDEV_INTERNAL_H
26 #define CHARDEV_INTERNAL_H
27 
28 #include "chardev/char.h"
29 #include "chardev/char-fe.h"
30 #include "qom/object.h"
31 
32 #define MAX_HUB 4
33 #define MAX_MUX 4
34 #define MUX_BUFFER_SIZE 32 /* Must be a power of 2.  */
35 #define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
36 
37 struct MuxChardev {
38     Chardev parent;
39     /* Linked frontends */
40     CharBackend *backends[MAX_MUX];
41     /* Linked backend */
42     CharBackend chr;
43     unsigned long mux_bitset;
44     int focus;
45     bool term_got_escape;
46     /* Intermediate input buffer catches escape sequences even if the
47        currently active device is not accepting any input - but only until it
48        is full as well. */
49     unsigned char buffer[MAX_MUX][MUX_BUFFER_SIZE];
50     unsigned int prod[MAX_MUX];
51     unsigned int cons[MAX_MUX];
52     int timestamps;
53 
54     /* Protected by the Chardev chr_write_lock.  */
55     bool linestart;
56     int64_t timestamps_start;
57 };
58 typedef struct MuxChardev MuxChardev;
59 typedef struct HubChardev HubChardev;
60 typedef struct HubCharBackend HubCharBackend;
61 
62 /*
63  * Back-pointer on a hub, actual backend and its index in
64  * `hub->backends` array
65  */
66 struct HubCharBackend {
67     HubChardev   *hub;
68     CharBackend  be;
69     unsigned int be_ind;
70 };
71 
72 struct HubChardev {
73     Chardev parent;
74     /* Linked backends */
75     HubCharBackend backends[MAX_HUB];
76     /*
77      * Number of backends attached to this hub. Once attached, a
78      * backend can't be detached, so the counter is only increasing.
79      * To safely remove a backend, hub has to be removed first.
80      */
81     unsigned int be_cnt;
82     /*
83      * Number of CHR_EVEN_OPENED events from all backends. Needed to
84      * send CHR_EVEN_CLOSED only when counter goes to zero.
85      */
86     unsigned int be_event_opened_cnt;
87     /*
88      * Counters of written bytes from a single frontend device
89      * to multiple backend devices.
90      */
91     unsigned int be_written[MAX_HUB];
92     unsigned int be_min_written;
93     /*
94      * Index of a backend device which got EAGAIN on last write,
95      * -1 is invalid index.
96      */
97     int be_eagain_ind;
98 };
99 typedef struct HubChardev HubChardev;
100 
101 DECLARE_INSTANCE_CHECKER(MuxChardev, MUX_CHARDEV,
102                          TYPE_CHARDEV_MUX)
103 DECLARE_INSTANCE_CHECKER(HubChardev, HUB_CHARDEV,
104                          TYPE_CHARDEV_HUB)
105 
106 #define CHARDEV_IS_MUX(chr)                                \
107     object_dynamic_cast(OBJECT(chr), TYPE_CHARDEV_MUX)
108 #define CHARDEV_IS_HUB(chr)                                \
109     object_dynamic_cast(OBJECT(chr), TYPE_CHARDEV_HUB)
110 
111 bool mux_chr_attach_frontend(MuxChardev *d, CharBackend *b,
112                              unsigned int *tag, Error **errp);
113 bool mux_chr_detach_frontend(MuxChardev *d, unsigned int tag);
114 void mux_set_focus(Chardev *chr, unsigned int focus);
115 void mux_chr_send_all_event(Chardev *chr, QEMUChrEvent event);
116 
117 Object *get_chardevs_root(void);
118 
119 #endif /* CHARDEV_INTERNAL_H */
120