rtc-test: introduce more update tests
[qemu/ar7.git] / chardev / wctablet.c
blob6c13c2c58a82ce6fc018206fb05fc6e3ab258492
1 /*
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
26 * THE SOFTWARE.
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/time.h>
31 #include <time.h>
33 #include "qemu/osdep.h"
34 #include "qemu-common.h"
35 #include "chardev/char-serial.h"
36 #include "ui/console.h"
37 #include "ui/input.h"
38 #include "trace.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. */
71 typedef struct {
72 Chardev parent;
73 QemuInputHandlerState *hs;
75 /* Query string from serial */
76 uint8_t query[100];
77 int query_index;
79 /* Command to be sent to serial port */
80 uint8_t outbuf[WC_OUTPUT_BUF_MAX_LEN];
81 int outlen;
83 int line_speed;
84 bool send_events;
85 int axis[INPUT_AXIS__MAX];
86 bool btns[INPUT_BUTTON__MAX];
88 } TabletChardev;
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)) {
107 return;
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)
117 /* clear buffers */
118 tablet->query_index = 0;
119 tablet->outlen = 0;
120 /* reset state */
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) {
129 return;
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]) {
144 codes[0] = 0xa0;
147 wctablet_queue_output(tablet, codes, 7);
150 static void wctablet_input_event(DeviceState *dev, QemuConsole *src,
151 InputEvent *evt)
153 TabletChardev *tablet = (TabletChardev *)dev;
154 InputMoveEvent *move;
155 InputBtnEvent *btn;
157 switch (evt->type) {
158 case INPUT_EVENT_KIND_ABS:
159 move = evt->u.abs.data;
160 tablet->axis[move->axis] = move->value;
161 break;
163 case INPUT_EVENT_KIND_BTN:
164 btn = evt->u.btn.data;
165 tablet->btns[btn->button] = btn->down;
166 break;
168 default:
169 /* keep gcc happy */
170 break;
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);
193 int len, canWrite;
195 canWrite = qemu_chr_be_can_write(chr);
196 len = canWrite;
197 if (len > tablet->outlen) {
198 len = tablet->outlen;
201 if (len) {
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;
215 char *pos;
217 if (tablet->line_speed != 9600) {
218 return len;
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) {
231 return len;
234 if (strncmp((char *)tablet->query, "~#", 2) == 0) {
235 /* init / detect sequence */
236 trace_wct_init();
237 wctablet_shift_input(tablet, 2);
238 wctablet_queue_output(tablet, WC_MODEL_STRING,
239 WC_MODEL_STRING_LENGTH);
240 return len;
243 /* detect line */
244 pos = strchr((char *)tablet->query, '\r');
245 if (!pos) {
246 pos = strchr((char *)tablet->query, '\n');
248 if (!pos) {
249 return len;
251 clen = pos - (char *)tablet->query;
253 /* process commands */
254 if (strncmp((char *)tablet->query, "RE", 2) == 0 &&
255 clen == 2) {
256 trace_wct_cmd_re();
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 &&
262 clen == 2) {
263 trace_wct_cmd_st();
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 &&
269 clen == 2) {
270 trace_wct_cmd_sp();
271 wctablet_shift_input(tablet, 3);
272 tablet->send_events = false;
274 } else if (strncmp((char *)tablet->query, "TS", 2) == 0 &&
275 clen == 3) {
276 unsigned int input = tablet->query[2];
277 uint8_t codes[7] = {
278 0xa3,
279 ((input & 0x80) == 0) ? 0x7e : 0x7f,
280 (((WC_H4(input) & 0x7) ^ 0x5) << 4) | (WC_L4(input) ^ 0x7),
281 0x03,
282 0x7f,
283 0x7f,
284 0x00,
286 trace_wct_cmd_ts(input);
287 wctablet_shift_input(tablet, 4);
288 wctablet_queue_output(tablet, codes, 7);
290 } else {
291 tablet->query[clen] = 0; /* terminate line for printing */
292 trace_wct_cmd_other((char *)tablet->query);
293 wctablet_shift_input(tablet, clen + 1);
297 return len;
300 static int wctablet_chr_ioctl(Chardev *chr, int cmd, void *arg)
302 TabletChardev *tablet = WCTABLET_CHARDEV(chr);
303 QEMUSerialSetParams *ssp;
305 switch (cmd) {
306 case CHR_IOCTL_SERIAL_SET_PARAMS:
307 ssp = arg;
308 if (tablet->line_speed != ssp->speed) {
309 trace_wct_speed(ssp->speed);
310 wctablet_reset(tablet);
311 tablet->line_speed = ssp->speed;
313 break;
314 default:
315 return -ENOTSUP;
317 return 0;
320 static void wctablet_chr_finalize(Object *obj)
322 TabletChardev *tablet = WCTABLET_CHARDEV(obj);
324 qemu_input_handler_unregister(tablet->hs);
325 g_free(tablet);
328 static void wctablet_chr_open(Chardev *chr,
329 ChardevBackend *backend,
330 bool *be_opened,
331 Error **errp)
333 TabletChardev *tablet = WCTABLET_CHARDEV(chr);
335 *be_opened = true;
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,
343 &wctablet_handler);
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);