2
0

wctablet.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. * QEMU Wacom Penpartner serial tablet emulation
  3. *
  4. * some protocol details:
  5. * http://linuxwacom.sourceforge.net/wiki/index.php/Serial_Protocol_IV
  6. *
  7. * Copyright (c) 2016 Anatoli Huseu1
  8. * Copyright (c) 2016,17 Gerd Hoffmann
  9. *
  10. * Permission is hereby granted, free of charge, to any person obtaining a copy
  11. * of this software and associated documentation files (the "Software"), to
  12. * deal in the Software without restriction, including without limitation
  13. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  14. * and/or sell copies of the Software, and to permit persons to whom the
  15. * Software is furnished to do so, subject to the following conditions:
  16. *
  17. * The above copyright notice and this permission notice shall be included in
  18. * all copies or substantial portions of the Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  23. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM
  25. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  26. * THE SOFTWARE.
  27. */
  28. #include "qemu/osdep.h"
  29. #include "qemu/module.h"
  30. #include "chardev/char-serial.h"
  31. #include "ui/console.h"
  32. #include "ui/input.h"
  33. #include "trace.h"
  34. #include "qom/object.h"
  35. #define WC_OUTPUT_BUF_MAX_LEN 512
  36. #define WC_COMMAND_MAX_LEN 60
  37. #define WC_L7(n) ((n) & 127)
  38. #define WC_M7(n) (((n) >> 7) & 127)
  39. #define WC_H2(n) ((n) >> 14)
  40. #define WC_L4(n) ((n) & 15)
  41. #define WC_H4(n) (((n) >> 4) & 15)
  42. /* Model string and config string */
  43. #define WC_MODEL_STRING_LENGTH 18
  44. uint8_t WC_MODEL_STRING[WC_MODEL_STRING_LENGTH + 1] = "~#CT-0045R,V1.3-5,";
  45. #define WC_CONFIG_STRING_LENGTH 8
  46. uint8_t WC_CONFIG_STRING[WC_CONFIG_STRING_LENGTH + 1] = "96,N,8,0";
  47. #define WC_FULL_CONFIG_STRING_LENGTH 61
  48. uint8_t WC_FULL_CONFIG_STRING[WC_FULL_CONFIG_STRING_LENGTH + 1] = {
  49. 0x5c, 0x39, 0x36, 0x2c, 0x4e, 0x2c, 0x38, 0x2c,
  50. 0x31, 0x28, 0x01, 0x24, 0x57, 0x41, 0x43, 0x30,
  51. 0x30, 0x34, 0x35, 0x5c, 0x5c, 0x50, 0x45, 0x4e, 0x5c,
  52. 0x57, 0x41, 0x43, 0x30, 0x30, 0x30, 0x30, 0x5c,
  53. 0x54, 0x61, 0x62, 0x6c, 0x65, 0x74, 0x0d, 0x0a,
  54. 0x43, 0x54, 0x2d, 0x30, 0x30, 0x34, 0x35, 0x52,
  55. 0x2c, 0x56, 0x31, 0x2e, 0x33, 0x2d, 0x35, 0x0d,
  56. 0x0a, 0x45, 0x37, 0x29
  57. };
  58. /* This structure is used to save private info for Wacom Tablet. */
  59. struct TabletChardev {
  60. Chardev parent;
  61. QemuInputHandlerState *hs;
  62. /* Query string from serial */
  63. uint8_t query[100];
  64. int query_index;
  65. /* Command to be sent to serial port */
  66. uint8_t outbuf[WC_OUTPUT_BUF_MAX_LEN];
  67. int outlen;
  68. int line_speed;
  69. bool send_events;
  70. int axis[INPUT_AXIS__MAX];
  71. bool btns[INPUT_BUTTON__MAX];
  72. };
  73. typedef struct TabletChardev TabletChardev;
  74. #define TYPE_CHARDEV_WCTABLET "chardev-wctablet"
  75. DECLARE_INSTANCE_CHECKER(TabletChardev, WCTABLET_CHARDEV,
  76. TYPE_CHARDEV_WCTABLET)
  77. static void wctablet_chr_accept_input(Chardev *chr);
  78. static void wctablet_shift_input(TabletChardev *tablet, int count)
  79. {
  80. tablet->query_index -= count;
  81. memmove(tablet->query, tablet->query + count, tablet->query_index);
  82. tablet->query[tablet->query_index] = 0;
  83. }
  84. static void wctablet_queue_output(TabletChardev *tablet, uint8_t *buf, int count)
  85. {
  86. if (tablet->outlen + count > sizeof(tablet->outbuf)) {
  87. return;
  88. }
  89. memcpy(tablet->outbuf + tablet->outlen, buf, count);
  90. tablet->outlen += count;
  91. wctablet_chr_accept_input(CHARDEV(tablet));
  92. }
  93. static void wctablet_reset(TabletChardev *tablet)
  94. {
  95. /* clear buffers */
  96. tablet->query_index = 0;
  97. tablet->outlen = 0;
  98. /* reset state */
  99. tablet->send_events = false;
  100. }
  101. static void wctablet_queue_event(TabletChardev *tablet)
  102. {
  103. uint8_t codes[8] = { 0xe0, 0, 0, 0, 0, 0, 0 };
  104. if (tablet->line_speed != 9600) {
  105. return;
  106. }
  107. int newX = tablet->axis[INPUT_AXIS_X] * 0.1537;
  108. int nexY = tablet->axis[INPUT_AXIS_Y] * 0.1152;
  109. codes[0] = codes[0] | WC_H2(newX);
  110. codes[1] = codes[1] | WC_M7(newX);
  111. codes[2] = codes[2] | WC_L7(newX);
  112. codes[3] = codes[3] | WC_H2(nexY);
  113. codes[4] = codes[4] | WC_M7(nexY);
  114. codes[5] = codes[5] | WC_L7(nexY);
  115. if (tablet->btns[INPUT_BUTTON_LEFT]) {
  116. codes[0] = 0xa0;
  117. }
  118. wctablet_queue_output(tablet, codes, 7);
  119. }
  120. static void wctablet_input_event(DeviceState *dev, QemuConsole *src,
  121. InputEvent *evt)
  122. {
  123. TabletChardev *tablet = (TabletChardev *)dev;
  124. InputMoveEvent *move;
  125. InputBtnEvent *btn;
  126. switch (evt->type) {
  127. case INPUT_EVENT_KIND_ABS:
  128. move = evt->u.abs.data;
  129. tablet->axis[move->axis] = move->value;
  130. break;
  131. case INPUT_EVENT_KIND_BTN:
  132. btn = evt->u.btn.data;
  133. tablet->btns[btn->button] = btn->down;
  134. break;
  135. default:
  136. /* keep gcc happy */
  137. break;
  138. }
  139. }
  140. static void wctablet_input_sync(DeviceState *dev)
  141. {
  142. TabletChardev *tablet = (TabletChardev *)dev;
  143. if (tablet->send_events) {
  144. wctablet_queue_event(tablet);
  145. }
  146. }
  147. static const QemuInputHandler wctablet_handler = {
  148. .name = "QEMU Wacom Pen Tablet",
  149. .mask = INPUT_EVENT_MASK_BTN | INPUT_EVENT_MASK_ABS,
  150. .event = wctablet_input_event,
  151. .sync = wctablet_input_sync,
  152. };
  153. static void wctablet_chr_accept_input(Chardev *chr)
  154. {
  155. TabletChardev *tablet = WCTABLET_CHARDEV(chr);
  156. int len, canWrite;
  157. canWrite = qemu_chr_be_can_write(chr);
  158. len = canWrite;
  159. if (len > tablet->outlen) {
  160. len = tablet->outlen;
  161. }
  162. if (len) {
  163. qemu_chr_be_write(chr, tablet->outbuf, len);
  164. tablet->outlen -= len;
  165. if (tablet->outlen) {
  166. memmove(tablet->outbuf, tablet->outbuf + len, tablet->outlen);
  167. }
  168. }
  169. }
  170. static int wctablet_chr_write(struct Chardev *chr,
  171. const uint8_t *buf, int len)
  172. {
  173. TabletChardev *tablet = WCTABLET_CHARDEV(chr);
  174. unsigned int i, clen;
  175. char *pos;
  176. if (tablet->line_speed != 9600) {
  177. return len;
  178. }
  179. for (i = 0; i < len && tablet->query_index < sizeof(tablet->query) - 1; i++) {
  180. tablet->query[tablet->query_index++] = buf[i];
  181. }
  182. tablet->query[tablet->query_index] = 0;
  183. while (tablet->query_index > 0 && (tablet->query[0] == '@' ||
  184. tablet->query[0] == '\r' ||
  185. tablet->query[0] == '\n')) {
  186. wctablet_shift_input(tablet, 1);
  187. }
  188. if (!tablet->query_index) {
  189. return len;
  190. }
  191. if (strncmp((char *)tablet->query, "~#", 2) == 0) {
  192. /* init / detect sequence */
  193. trace_wct_init();
  194. wctablet_shift_input(tablet, 2);
  195. wctablet_queue_output(tablet, WC_MODEL_STRING,
  196. WC_MODEL_STRING_LENGTH);
  197. return len;
  198. }
  199. /* detect line */
  200. pos = strchr((char *)tablet->query, '\r');
  201. if (!pos) {
  202. pos = strchr((char *)tablet->query, '\n');
  203. }
  204. if (!pos) {
  205. return len;
  206. }
  207. clen = pos - (char *)tablet->query;
  208. /* process commands */
  209. if (strncmp((char *)tablet->query, "RE", 2) == 0 &&
  210. clen == 2) {
  211. trace_wct_cmd_re();
  212. wctablet_shift_input(tablet, 3);
  213. wctablet_queue_output(tablet, WC_CONFIG_STRING,
  214. WC_CONFIG_STRING_LENGTH);
  215. } else if (strncmp((char *)tablet->query, "ST", 2) == 0 &&
  216. clen == 2) {
  217. trace_wct_cmd_st();
  218. wctablet_shift_input(tablet, 3);
  219. tablet->send_events = true;
  220. wctablet_queue_event(tablet);
  221. } else if (strncmp((char *)tablet->query, "SP", 2) == 0 &&
  222. clen == 2) {
  223. trace_wct_cmd_sp();
  224. wctablet_shift_input(tablet, 3);
  225. tablet->send_events = false;
  226. } else if (strncmp((char *)tablet->query, "TS", 2) == 0 &&
  227. clen == 3) {
  228. unsigned int input = tablet->query[2];
  229. uint8_t codes[7] = {
  230. 0xa3,
  231. ((input & 0x80) == 0) ? 0x7e : 0x7f,
  232. (((WC_H4(input) & 0x7) ^ 0x5) << 4) | (WC_L4(input) ^ 0x7),
  233. 0x03,
  234. 0x7f,
  235. 0x7f,
  236. 0x00,
  237. };
  238. trace_wct_cmd_ts(input);
  239. wctablet_shift_input(tablet, 4);
  240. wctablet_queue_output(tablet, codes, 7);
  241. } else {
  242. tablet->query[clen] = 0; /* terminate line for printing */
  243. trace_wct_cmd_other((char *)tablet->query);
  244. wctablet_shift_input(tablet, clen + 1);
  245. }
  246. return len;
  247. }
  248. static int wctablet_chr_ioctl(Chardev *chr, int cmd, void *arg)
  249. {
  250. TabletChardev *tablet = WCTABLET_CHARDEV(chr);
  251. QEMUSerialSetParams *ssp;
  252. switch (cmd) {
  253. case CHR_IOCTL_SERIAL_SET_PARAMS:
  254. ssp = arg;
  255. if (tablet->line_speed != ssp->speed) {
  256. trace_wct_speed(ssp->speed);
  257. wctablet_reset(tablet);
  258. tablet->line_speed = ssp->speed;
  259. }
  260. break;
  261. default:
  262. return -ENOTSUP;
  263. }
  264. return 0;
  265. }
  266. static void wctablet_chr_finalize(Object *obj)
  267. {
  268. TabletChardev *tablet = WCTABLET_CHARDEV(obj);
  269. if (tablet->hs) {
  270. qemu_input_handler_unregister(tablet->hs);
  271. }
  272. }
  273. static void wctablet_chr_open(Chardev *chr,
  274. ChardevBackend *backend,
  275. bool *be_opened,
  276. Error **errp)
  277. {
  278. TabletChardev *tablet = WCTABLET_CHARDEV(chr);
  279. *be_opened = true;
  280. /* init state machine */
  281. memcpy(tablet->outbuf, WC_FULL_CONFIG_STRING, WC_FULL_CONFIG_STRING_LENGTH);
  282. tablet->outlen = WC_FULL_CONFIG_STRING_LENGTH;
  283. tablet->query_index = 0;
  284. tablet->hs = qemu_input_handler_register((DeviceState *)tablet,
  285. &wctablet_handler);
  286. }
  287. static void wctablet_chr_class_init(ObjectClass *oc, void *data)
  288. {
  289. ChardevClass *cc = CHARDEV_CLASS(oc);
  290. cc->open = wctablet_chr_open;
  291. cc->chr_write = wctablet_chr_write;
  292. cc->chr_ioctl = wctablet_chr_ioctl;
  293. cc->chr_accept_input = wctablet_chr_accept_input;
  294. }
  295. static const TypeInfo wctablet_type_info = {
  296. .name = TYPE_CHARDEV_WCTABLET,
  297. .parent = TYPE_CHARDEV,
  298. .instance_size = sizeof(TabletChardev),
  299. .instance_finalize = wctablet_chr_finalize,
  300. .class_init = wctablet_chr_class_init,
  301. };
  302. static void register_types(void)
  303. {
  304. type_register_static(&wctablet_type_info);
  305. }
  306. type_init(register_types);