chardev-internal.h 4.0 KB

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