2 * QEMU Wacom Penpartner serial tablet emulation
4 * some protocol details:
5 * http://linuxwacom.sourceforge.net/wiki/index.php/Serial_Protocol_IV
7 * Copyright (c) 2016 Anatoli Huseu1
8 * Copyright (c) 2016,17 Gerd Hoffmann
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:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
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
33 #include "qemu/osdep.h"
34 #include "qemu-common.h"
35 #include "chardev/char-serial.h"
36 #include "ui/console.h"
41 #define WC_OUTPUT_BUF_MAX_LEN 512
42 #define WC_COMMAND_MAX_LEN 60
44 #define WC_L7(n) ((n) & 127)
45 #define WC_M7(n) (((n) >> 7) & 127)
46 #define WC_H2(n) ((n) >> 14)
48 #define WC_L4(n) ((n) & 15)
49 #define WC_H4(n) (((n) >> 4) & 15)
51 /* Model string and config string */
52 #define WC_MODEL_STRING_LENGTH 18
53 uint8_t WC_MODEL_STRING[WC_MODEL_STRING_LENGTH + 1] = "~#CT-0045R,V1.3-5,";
55 #define WC_CONFIG_STRING_LENGTH 8
56 uint8_t WC_CONFIG_STRING[WC_CONFIG_STRING_LENGTH + 1] = "96,N,8,0";
58 #define WC_FULL_CONFIG_STRING_LENGTH 61
59 uint8_t WC_FULL_CONFIG_STRING[WC_FULL_CONFIG_STRING_LENGTH + 1] = {
60 0x5c, 0x39, 0x36, 0x2c, 0x4e, 0x2c, 0x38, 0x2c,
61 0x31, 0x28, 0x01, 0x24, 0x57, 0x41, 0x43, 0x30,
62 0x30, 0x34, 0x35, 0x5c, 0x5c, 0x50, 0x45, 0x4e, 0x5c,
63 0x57, 0x41, 0x43, 0x30, 0x30, 0x30, 0x30, 0x5c,
64 0x54, 0x61, 0x62, 0x6c, 0x65, 0x74, 0x0d, 0x0a,
65 0x43, 0x54, 0x2d, 0x30, 0x30, 0x34, 0x35, 0x52,
66 0x2c, 0x56, 0x31, 0x2e, 0x33, 0x2d, 0x35, 0x0d,
67 0x0a, 0x45, 0x37, 0x29
70 /* This structure is used to save private info for Wacom Tablet. */
73 QemuInputHandlerState *hs;
75 /* Query string from serial */
79 /* Command to be sent to serial port */
80 uint8_t outbuf[WC_OUTPUT_BUF_MAX_LEN];
85 int axis[INPUT_AXIS__MAX];
86 bool btns[INPUT_BUTTON__MAX];
90 #define TYPE_CHARDEV_WCTABLET "chardev-wctablet"
91 #define WCTABLET_CHARDEV(obj) \
92 OBJECT_CHECK(TabletChardev, (obj), TYPE_CHARDEV_WCTABLET)
95 static void wctablet_chr_accept_input(Chardev *chr);
97 static void wctablet_shift_input(TabletChardev *tablet, int count)
99 tablet->query_index -= count;
100 memmove(tablet->query, tablet->query + count, tablet->query_index);
101 tablet->query[tablet->query_index] = 0;
104 static void wctablet_queue_output(TabletChardev *tablet, uint8_t *buf, int count)
106 if (tablet->outlen + count > sizeof(tablet->outbuf)) {
110 memcpy(tablet->outbuf + tablet->outlen, buf, count);
111 tablet->outlen += count;
112 wctablet_chr_accept_input(CHARDEV(tablet));
115 static void wctablet_reset(TabletChardev *tablet)
118 tablet->query_index = 0;
121 tablet->send_events = false;
124 static void wctablet_queue_event(TabletChardev *tablet)
126 uint8_t codes[8] = { 0xe0, 0, 0, 0, 0, 0, 0 };
128 if (tablet->line_speed != 9600) {
132 int newX = tablet->axis[INPUT_AXIS_X] * 0.1537;
133 int nexY = tablet->axis[INPUT_AXIS_Y] * 0.1152;
135 codes[0] = codes[0] | WC_H2(newX);
136 codes[1] = codes[1] | WC_M7(newX);
137 codes[2] = codes[2] | WC_L7(newX);
139 codes[3] = codes[3] | WC_H2(nexY);
140 codes[4] = codes[4] | WC_M7(nexY);
141 codes[5] = codes[5] | WC_L7(nexY);
143 if (tablet->btns[INPUT_BUTTON_LEFT]) {
147 wctablet_queue_output(tablet, codes, 7);
150 static void wctablet_input_event(DeviceState *dev, QemuConsole *src,
153 TabletChardev *tablet = (TabletChardev *)dev;
154 InputMoveEvent *move;
158 case INPUT_EVENT_KIND_ABS:
159 move = evt->u.abs.data;
160 tablet->axis[move->axis] = move->value;
163 case INPUT_EVENT_KIND_BTN:
164 btn = evt->u.btn.data;
165 tablet->btns[btn->button] = btn->down;
174 static void wctablet_input_sync(DeviceState *dev)
176 TabletChardev *tablet = (TabletChardev *)dev;
178 if (tablet->send_events) {
179 wctablet_queue_event(tablet);
183 static QemuInputHandler wctablet_handler = {
184 .name = "QEMU Wacome Pen Tablet",
185 .mask = INPUT_EVENT_MASK_BTN | INPUT_EVENT_MASK_ABS,
186 .event = wctablet_input_event,
187 .sync = wctablet_input_sync,
190 static void wctablet_chr_accept_input(Chardev *chr)
192 TabletChardev *tablet = WCTABLET_CHARDEV(chr);
195 canWrite = qemu_chr_be_can_write(chr);
197 if (len > tablet->outlen) {
198 len = tablet->outlen;
202 qemu_chr_be_write(chr, tablet->outbuf, len);
203 tablet->outlen -= len;
204 if (tablet->outlen) {
205 memmove(tablet->outbuf, tablet->outbuf + len, tablet->outlen);
210 static int wctablet_chr_write(struct Chardev *chr,
211 const uint8_t *buf, int len)
213 TabletChardev *tablet = WCTABLET_CHARDEV(chr);
214 unsigned int i, clen;
217 if (tablet->line_speed != 9600) {
220 for (i = 0; i < len && tablet->query_index < sizeof(tablet->query) - 1; i++) {
221 tablet->query[tablet->query_index++] = buf[i];
223 tablet->query[tablet->query_index] = 0;
225 while (tablet->query_index > 0 && (tablet->query[0] == '@' ||
226 tablet->query[0] == '\r' ||
227 tablet->query[0] == '\n')) {
228 wctablet_shift_input(tablet, 1);
230 if (!tablet->query_index) {
234 if (strncmp((char *)tablet->query, "~#", 2) == 0) {
235 /* init / detect sequence */
237 wctablet_shift_input(tablet, 2);
238 wctablet_queue_output(tablet, WC_MODEL_STRING,
239 WC_MODEL_STRING_LENGTH);
244 pos = strchr((char *)tablet->query, '\r');
246 pos = strchr((char *)tablet->query, '\n');
251 clen = pos - (char *)tablet->query;
253 /* process commands */
254 if (strncmp((char *)tablet->query, "RE", 2) == 0 &&
257 wctablet_shift_input(tablet, 3);
258 wctablet_queue_output(tablet, WC_CONFIG_STRING,
259 WC_CONFIG_STRING_LENGTH);
261 } else if (strncmp((char *)tablet->query, "ST", 2) == 0 &&
264 wctablet_shift_input(tablet, 3);
265 tablet->send_events = true;
266 wctablet_queue_event(tablet);
268 } else if (strncmp((char *)tablet->query, "SP", 2) == 0 &&
271 wctablet_shift_input(tablet, 3);
272 tablet->send_events = false;
274 } else if (strncmp((char *)tablet->query, "TS", 2) == 0 &&
276 unsigned int input = tablet->query[2];
279 ((input & 0x80) == 0) ? 0x7e : 0x7f,
280 (((WC_H4(input) & 0x7) ^ 0x5) << 4) | (WC_L4(input) ^ 0x7),
286 trace_wct_cmd_ts(input);
287 wctablet_shift_input(tablet, 4);
288 wctablet_queue_output(tablet, codes, 7);
291 tablet->query[clen] = 0; /* terminate line for printing */
292 trace_wct_cmd_other((char *)tablet->query);
293 wctablet_shift_input(tablet, clen + 1);
300 static int wctablet_chr_ioctl(Chardev *chr, int cmd, void *arg)
302 TabletChardev *tablet = WCTABLET_CHARDEV(chr);
303 QEMUSerialSetParams *ssp;
306 case CHR_IOCTL_SERIAL_SET_PARAMS:
308 if (tablet->line_speed != ssp->speed) {
309 trace_wct_speed(ssp->speed);
310 wctablet_reset(tablet);
311 tablet->line_speed = ssp->speed;
320 static void wctablet_chr_finalize(Object *obj)
322 TabletChardev *tablet = WCTABLET_CHARDEV(obj);
324 qemu_input_handler_unregister(tablet->hs);
328 static void wctablet_chr_open(Chardev *chr,
329 ChardevBackend *backend,
333 TabletChardev *tablet = WCTABLET_CHARDEV(chr);
337 /* init state machine */
338 memcpy(tablet->outbuf, WC_FULL_CONFIG_STRING, WC_FULL_CONFIG_STRING_LENGTH);
339 tablet->outlen = WC_FULL_CONFIG_STRING_LENGTH;
340 tablet->query_index = 0;
342 tablet->hs = qemu_input_handler_register((DeviceState *)tablet,
346 static void wctablet_chr_class_init(ObjectClass *oc, void *data)
348 ChardevClass *cc = CHARDEV_CLASS(oc);
350 cc->open = wctablet_chr_open;
351 cc->chr_write = wctablet_chr_write;
352 cc->chr_ioctl = wctablet_chr_ioctl;
353 cc->chr_accept_input = wctablet_chr_accept_input;
356 static const TypeInfo wctablet_type_info = {
357 .name = TYPE_CHARDEV_WCTABLET,
358 .parent = TYPE_CHARDEV,
359 .instance_size = sizeof(TabletChardev),
360 .instance_finalize = wctablet_chr_finalize,
361 .class_init = wctablet_chr_class_init,
364 static void register_types(void)
366 type_register_static(&wctablet_type_info);
369 type_init(register_types);