msmouse.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * QEMU Microsoft serial mouse emulation
  3. *
  4. * Copyright (c) 2008 Lubomir Rintel
  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. #include "qemu/osdep.h"
  25. #include "qemu/module.h"
  26. #include "qemu/fifo8.h"
  27. #include "chardev/char.h"
  28. #include "chardev/char-serial.h"
  29. #include "ui/console.h"
  30. #include "ui/input.h"
  31. #include "qom/object.h"
  32. #define MSMOUSE_LO6(n) ((n) & 0x3f)
  33. #define MSMOUSE_HI2(n) (((n) & 0xc0) >> 6)
  34. #define MSMOUSE_PWR(cm) (cm & (CHR_TIOCM_RTS | CHR_TIOCM_DTR))
  35. /* Serial PnP for 6 bit devices/mice sends all ASCII chars - 0x20 */
  36. #define M(c) (c - 0x20)
  37. /* Serial fifo size. */
  38. #define MSMOUSE_BUF_SZ 64
  39. /* Mouse ID: Send "M3" cause we behave like a 3 button logitech mouse. */
  40. const uint8_t mouse_id[] = {'M', '3'};
  41. /*
  42. * PnP start "(", PnP version (1.0), vendor ID, product ID, '\\',
  43. * serial ID (omitted), '\\', MS class name, '\\', driver ID (omitted), '\\',
  44. * product description, checksum, ")"
  45. * Missing parts are inserted later.
  46. */
  47. const uint8_t pnp_data[] = {M('('), 1, '$', M('Q'), M('M'), M('U'),
  48. M('0'), M('0'), M('0'), M('1'),
  49. M('\\'), M('\\'),
  50. M('M'), M('O'), M('U'), M('S'), M('E'),
  51. M('\\'), M('\\')};
  52. struct MouseChardev {
  53. Chardev parent;
  54. QemuInputHandlerState *hs;
  55. int tiocm;
  56. int axis[INPUT_AXIS__MAX];
  57. bool btns[INPUT_BUTTON__MAX];
  58. bool btnc[INPUT_BUTTON__MAX];
  59. Fifo8 outbuf;
  60. };
  61. typedef struct MouseChardev MouseChardev;
  62. #define TYPE_CHARDEV_MSMOUSE "chardev-msmouse"
  63. DECLARE_INSTANCE_CHECKER(MouseChardev, MOUSE_CHARDEV,
  64. TYPE_CHARDEV_MSMOUSE)
  65. static void msmouse_chr_accept_input(Chardev *chr)
  66. {
  67. MouseChardev *mouse = MOUSE_CHARDEV(chr);
  68. uint32_t len, avail;
  69. len = qemu_chr_be_can_write(chr);
  70. avail = fifo8_num_used(&mouse->outbuf);
  71. while (len > 0 && avail > 0) {
  72. const uint8_t *buf;
  73. uint32_t size;
  74. buf = fifo8_pop_bufptr(&mouse->outbuf, MIN(len, avail), &size);
  75. qemu_chr_be_write(chr, buf, size);
  76. len = qemu_chr_be_can_write(chr);
  77. avail -= size;
  78. }
  79. }
  80. static void msmouse_queue_event(MouseChardev *mouse)
  81. {
  82. unsigned char bytes[4] = { 0x40, 0x00, 0x00, 0x00 };
  83. int dx, dy, count = 3;
  84. dx = mouse->axis[INPUT_AXIS_X];
  85. mouse->axis[INPUT_AXIS_X] = 0;
  86. dy = mouse->axis[INPUT_AXIS_Y];
  87. mouse->axis[INPUT_AXIS_Y] = 0;
  88. /* Movement deltas */
  89. bytes[0] |= (MSMOUSE_HI2(dy) << 2) | MSMOUSE_HI2(dx);
  90. bytes[1] |= MSMOUSE_LO6(dx);
  91. bytes[2] |= MSMOUSE_LO6(dy);
  92. /* Buttons */
  93. bytes[0] |= (mouse->btns[INPUT_BUTTON_LEFT] ? 0x20 : 0x00);
  94. bytes[0] |= (mouse->btns[INPUT_BUTTON_RIGHT] ? 0x10 : 0x00);
  95. if (mouse->btns[INPUT_BUTTON_MIDDLE] ||
  96. mouse->btnc[INPUT_BUTTON_MIDDLE]) {
  97. bytes[3] |= (mouse->btns[INPUT_BUTTON_MIDDLE] ? 0x20 : 0x00);
  98. mouse->btnc[INPUT_BUTTON_MIDDLE] = false;
  99. count++;
  100. }
  101. if (fifo8_num_free(&mouse->outbuf) >= count) {
  102. fifo8_push_all(&mouse->outbuf, bytes, count);
  103. } else {
  104. /* queue full -> drop event */
  105. }
  106. }
  107. static void msmouse_input_event(DeviceState *dev, QemuConsole *src,
  108. InputEvent *evt)
  109. {
  110. MouseChardev *mouse = MOUSE_CHARDEV(dev);
  111. InputMoveEvent *move;
  112. InputBtnEvent *btn;
  113. /* Ignore events if serial mouse powered down. */
  114. if (!MSMOUSE_PWR(mouse->tiocm)) {
  115. return;
  116. }
  117. switch (evt->type) {
  118. case INPUT_EVENT_KIND_REL:
  119. move = evt->u.rel.data;
  120. mouse->axis[move->axis] += move->value;
  121. break;
  122. case INPUT_EVENT_KIND_BTN:
  123. btn = evt->u.btn.data;
  124. mouse->btns[btn->button] = btn->down;
  125. mouse->btnc[btn->button] = true;
  126. break;
  127. default:
  128. /* keep gcc happy */
  129. break;
  130. }
  131. }
  132. static void msmouse_input_sync(DeviceState *dev)
  133. {
  134. MouseChardev *mouse = MOUSE_CHARDEV(dev);
  135. Chardev *chr = CHARDEV(dev);
  136. /* Ignore events if serial mouse powered down. */
  137. if (!MSMOUSE_PWR(mouse->tiocm)) {
  138. return;
  139. }
  140. msmouse_queue_event(mouse);
  141. msmouse_chr_accept_input(chr);
  142. }
  143. static int msmouse_chr_write(struct Chardev *s, const uint8_t *buf, int len)
  144. {
  145. /* Ignore writes to mouse port */
  146. return len;
  147. }
  148. static const QemuInputHandler msmouse_handler = {
  149. .name = "QEMU Microsoft Mouse",
  150. .mask = INPUT_EVENT_MASK_BTN | INPUT_EVENT_MASK_REL,
  151. .event = msmouse_input_event,
  152. .sync = msmouse_input_sync,
  153. };
  154. static int msmouse_ioctl(Chardev *chr, int cmd, void *arg)
  155. {
  156. MouseChardev *mouse = MOUSE_CHARDEV(chr);
  157. int c, i, j;
  158. uint8_t bytes[MSMOUSE_BUF_SZ / 2];
  159. int *targ = (int *)arg;
  160. const uint8_t hexchr[16] = {M('0'), M('1'), M('2'), M('3'), M('4'), M('5'),
  161. M('6'), M('7'), M('8'), M('9'), M('A'), M('B'),
  162. M('C'), M('D'), M('E'), M('F')};
  163. switch (cmd) {
  164. case CHR_IOCTL_SERIAL_SET_TIOCM:
  165. c = mouse->tiocm;
  166. mouse->tiocm = *(int *)arg;
  167. if (MSMOUSE_PWR(mouse->tiocm)) {
  168. if (!MSMOUSE_PWR(c)) {
  169. /*
  170. * Power on after reset: Send ID and PnP data
  171. * No need to check fifo space as it is empty at this point.
  172. */
  173. fifo8_push_all(&mouse->outbuf, mouse_id, sizeof(mouse_id));
  174. /* Add PnP data: */
  175. fifo8_push_all(&mouse->outbuf, pnp_data, sizeof(pnp_data));
  176. /*
  177. * Add device description from qemu handler name.
  178. * Make sure this all fits into the queue beforehand!
  179. */
  180. c = M(')');
  181. for (i = 0; msmouse_handler.name[i]; i++) {
  182. bytes[i] = M(msmouse_handler.name[i]);
  183. c += bytes[i];
  184. }
  185. /* Calc more of checksum */
  186. for (j = 0; j < sizeof(pnp_data); j++) {
  187. c += pnp_data[j];
  188. }
  189. c &= 0xff;
  190. bytes[i++] = hexchr[c >> 4];
  191. bytes[i++] = hexchr[c & 0x0f];
  192. bytes[i++] = M(')');
  193. fifo8_push_all(&mouse->outbuf, bytes, i);
  194. /* Start sending data to serial. */
  195. msmouse_chr_accept_input(chr);
  196. }
  197. break;
  198. }
  199. /*
  200. * Reset mouse buffers on power down.
  201. * Mouse won't send anything without power.
  202. */
  203. fifo8_reset(&mouse->outbuf);
  204. memset(mouse->axis, 0, sizeof(mouse->axis));
  205. memset(mouse->btns, false, sizeof(mouse->btns));
  206. memset(mouse->btnc, false, sizeof(mouse->btns));
  207. break;
  208. case CHR_IOCTL_SERIAL_GET_TIOCM:
  209. /* Remember line control status. */
  210. *targ = mouse->tiocm;
  211. break;
  212. default:
  213. return -ENOTSUP;
  214. }
  215. return 0;
  216. }
  217. static void char_msmouse_finalize(Object *obj)
  218. {
  219. MouseChardev *mouse = MOUSE_CHARDEV(obj);
  220. if (mouse->hs) {
  221. qemu_input_handler_unregister(mouse->hs);
  222. }
  223. fifo8_destroy(&mouse->outbuf);
  224. }
  225. static void msmouse_chr_open(Chardev *chr,
  226. ChardevBackend *backend,
  227. bool *be_opened,
  228. Error **errp)
  229. {
  230. MouseChardev *mouse = MOUSE_CHARDEV(chr);
  231. *be_opened = false;
  232. mouse->hs = qemu_input_handler_register((DeviceState *)mouse,
  233. &msmouse_handler);
  234. mouse->tiocm = 0;
  235. fifo8_create(&mouse->outbuf, MSMOUSE_BUF_SZ);
  236. }
  237. static void char_msmouse_class_init(ObjectClass *oc, void *data)
  238. {
  239. ChardevClass *cc = CHARDEV_CLASS(oc);
  240. cc->open = msmouse_chr_open;
  241. cc->chr_write = msmouse_chr_write;
  242. cc->chr_accept_input = msmouse_chr_accept_input;
  243. cc->chr_ioctl = msmouse_ioctl;
  244. }
  245. static const TypeInfo char_msmouse_type_info = {
  246. .name = TYPE_CHARDEV_MSMOUSE,
  247. .parent = TYPE_CHARDEV,
  248. .instance_size = sizeof(MouseChardev),
  249. .instance_finalize = char_msmouse_finalize,
  250. .class_init = char_msmouse_class_init,
  251. };
  252. static void register_types(void)
  253. {
  254. type_register_static(&char_msmouse_type_info);
  255. }
  256. type_init(register_types);