blob: 8ed57b3b44444ad0e07e65ae0929b449bbd8e41e [file] [log] [blame]
balrogf6d2a312007-06-10 19:21:04 +00001/*
2 * Wacom PenPartner USB tablet emulation.
3 *
4 * Copyright (c) 2006 Openedhand Ltd.
5 * Author: Andrzej Zaborowski <balrog@zabor.org>
6 *
7 * Based on hw/usb-hid.c:
8 * Copyright (c) 2005 Fabrice Bellard
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 deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * 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 */
Markus Armbruster0b8fa322019-05-23 16:35:07 +020028
Peter Maydelle532b2e2016-01-26 18:17:12 +000029#include "qemu/osdep.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010030#include "ui/console.h"
Gerd Hoffmannf1ae32a2012-03-07 14:55:18 +010031#include "hw/usb.h"
Markus Armbrusterd6454272019-08-12 07:23:45 +020032#include "migration/vmstate.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020033#include "qemu/module.h"
Michael S. Tsirkin463581a2018-05-03 22:50:48 +030034#include "desc.h"
balrogf6d2a312007-06-10 19:21:04 +000035
36/* Interface requests */
37#define WACOM_GET_REPORT 0x2101
38#define WACOM_SET_REPORT 0x2109
39
40/* HID interface requests */
41#define HID_GET_REPORT 0xa101
42#define HID_GET_IDLE 0xa102
43#define HID_GET_PROTOCOL 0xa103
44#define HID_SET_IDLE 0x210a
45#define HID_SET_PROTOCOL 0x210b
46
47typedef struct USBWacomState {
48 USBDevice dev;
Hans de Goede8beba932012-11-17 12:47:14 +010049 USBEndpoint *intr;
balrogf6d2a312007-06-10 19:21:04 +000050 QEMUPutMouseEntry *eh_entry;
51 int dx, dy, dz, buttons_state;
52 int x, y;
53 int mouse_grabbed;
54 enum {
55 WACOM_MODE_HID = 1,
56 WACOM_MODE_WACOM = 2,
57 } mode;
François Revol2ca20782009-08-25 11:14:10 +020058 uint8_t idle;
59 int changed;
balrogf6d2a312007-06-10 19:21:04 +000060} USBWacomState;
61
Gonglei924e5672015-05-06 20:55:34 +080062#define TYPE_USB_WACOM "usb-wacom-tablet"
63#define USB_WACOM(obj) OBJECT_CHECK(USBWacomState, (obj), TYPE_USB_WACOM)
64
Gerd Hoffmann037a5202010-11-25 16:12:06 +010065enum {
66 STR_MANUFACTURER = 1,
67 STR_PRODUCT,
68 STR_SERIALNUMBER,
balrogf6d2a312007-06-10 19:21:04 +000069};
70
Gerd Hoffmann037a5202010-11-25 16:12:06 +010071static const USBDescStrings desc_strings = {
Crístian Viana93bfef42012-05-30 00:35:51 -030072 [STR_MANUFACTURER] = "QEMU",
Gerd Hoffmann037a5202010-11-25 16:12:06 +010073 [STR_PRODUCT] = "Wacom PenPartner",
74 [STR_SERIALNUMBER] = "1",
75};
balrogf6d2a312007-06-10 19:21:04 +000076
Gerd Hoffmann037a5202010-11-25 16:12:06 +010077static const USBDescIface desc_iface_wacom = {
78 .bInterfaceNumber = 0,
79 .bNumEndpoints = 1,
80 .bInterfaceClass = USB_CLASS_HID,
81 .bInterfaceSubClass = 0x01, /* boot */
82 .bInterfaceProtocol = 0x02,
83 .ndesc = 1,
84 .descs = (USBDescOther[]) {
85 {
86 /* HID descriptor */
87 .data = (uint8_t[]) {
88 0x09, /* u8 bLength */
89 0x21, /* u8 bDescriptorType */
90 0x01, 0x10, /* u16 HID_class */
91 0x00, /* u8 country_code */
92 0x01, /* u8 num_descriptors */
93 0x22, /* u8 type: Report */
94 0x6e, 0, /* u16 len */
95 },
96 },
97 },
98 .eps = (USBDescEndpoint[]) {
99 {
100 .bEndpointAddress = USB_DIR_IN | 0x01,
101 .bmAttributes = USB_ENDPOINT_XFER_INT,
102 .wMaxPacketSize = 8,
103 .bInterval = 0x0a,
104 },
105 },
106};
balrogf6d2a312007-06-10 19:21:04 +0000107
Gerd Hoffmann037a5202010-11-25 16:12:06 +0100108static const USBDescDevice desc_device_wacom = {
109 .bcdUSB = 0x0110,
110 .bMaxPacketSize0 = 8,
111 .bNumConfigurations = 1,
112 .confs = (USBDescConfig[]) {
113 {
114 .bNumInterfaces = 1,
115 .bConfigurationValue = 1,
Pantelis Koukousoulasbd939762013-12-16 09:42:49 +0200116 .bmAttributes = USB_CFG_ATT_ONE,
Gerd Hoffmann037a5202010-11-25 16:12:06 +0100117 .bMaxPower = 40,
Brad Hardsadd75082011-04-03 15:33:19 +1000118 .nif = 1,
Gerd Hoffmann037a5202010-11-25 16:12:06 +0100119 .ifs = &desc_iface_wacom,
120 },
121 },
122};
balrogf6d2a312007-06-10 19:21:04 +0000123
Gerd Hoffmann037a5202010-11-25 16:12:06 +0100124static const USBDesc desc_wacom = {
125 .id = {
126 .idVendor = 0x056a,
127 .idProduct = 0x0000,
128 .bcdDevice = 0x4210,
129 .iManufacturer = STR_MANUFACTURER,
130 .iProduct = STR_PRODUCT,
131 .iSerialNumber = STR_SERIALNUMBER,
132 },
133 .full = &desc_device_wacom,
134 .str = desc_strings,
balrogf6d2a312007-06-10 19:21:04 +0000135};
136
137static void usb_mouse_event(void *opaque,
138 int dx1, int dy1, int dz1, int buttons_state)
139{
140 USBWacomState *s = opaque;
141
142 s->dx += dx1;
143 s->dy += dy1;
144 s->dz += dz1;
145 s->buttons_state = buttons_state;
François Revol2ca20782009-08-25 11:14:10 +0200146 s->changed = 1;
Gerd Hoffmann8550a022013-01-29 12:44:35 +0100147 usb_wakeup(s->intr, 0);
balrogf6d2a312007-06-10 19:21:04 +0000148}
149
150static void usb_wacom_event(void *opaque,
151 int x, int y, int dz, int buttons_state)
152{
153 USBWacomState *s = opaque;
154
François Revol2ca20782009-08-25 11:14:10 +0200155 /* scale to Penpartner resolution */
156 s->x = (x * 5040 / 0x7FFF);
157 s->y = (y * 3780 / 0x7FFF);
balrogf6d2a312007-06-10 19:21:04 +0000158 s->dz += dz;
159 s->buttons_state = buttons_state;
François Revol2ca20782009-08-25 11:14:10 +0200160 s->changed = 1;
Gerd Hoffmann8550a022013-01-29 12:44:35 +0100161 usb_wakeup(s->intr, 0);
balrogf6d2a312007-06-10 19:21:04 +0000162}
163
164static inline int int_clamp(int val, int vmin, int vmax)
165{
166 if (val < vmin)
167 return vmin;
168 else if (val > vmax)
169 return vmax;
170 else
171 return val;
172}
173
174static int usb_mouse_poll(USBWacomState *s, uint8_t *buf, int len)
175{
176 int dx, dy, dz, b, l;
177
178 if (!s->mouse_grabbed) {
179 s->eh_entry = qemu_add_mouse_event_handler(usb_mouse_event, s, 0,
180 "QEMU PenPartner tablet");
Gerd Hoffmannb2d4d832010-10-08 12:30:14 +0200181 qemu_activate_mouse_event_handler(s->eh_entry);
balrogf6d2a312007-06-10 19:21:04 +0000182 s->mouse_grabbed = 1;
183 }
184
185 dx = int_clamp(s->dx, -128, 127);
186 dy = int_clamp(s->dy, -128, 127);
187 dz = int_clamp(s->dz, -128, 127);
188
189 s->dx -= dx;
190 s->dy -= dy;
191 s->dz -= dz;
192
193 b = 0;
194 if (s->buttons_state & MOUSE_EVENT_LBUTTON)
195 b |= 0x01;
196 if (s->buttons_state & MOUSE_EVENT_RBUTTON)
197 b |= 0x02;
198 if (s->buttons_state & MOUSE_EVENT_MBUTTON)
199 b |= 0x04;
200
201 buf[0] = b;
202 buf[1] = dx;
203 buf[2] = dy;
204 l = 3;
205 if (len >= 4) {
206 buf[3] = dz;
207 l = 4;
208 }
209 return l;
210}
211
212static int usb_wacom_poll(USBWacomState *s, uint8_t *buf, int len)
213{
214 int b;
215
216 if (!s->mouse_grabbed) {
217 s->eh_entry = qemu_add_mouse_event_handler(usb_wacom_event, s, 1,
218 "QEMU PenPartner tablet");
Gerd Hoffmannb2d4d832010-10-08 12:30:14 +0200219 qemu_activate_mouse_event_handler(s->eh_entry);
balrogf6d2a312007-06-10 19:21:04 +0000220 s->mouse_grabbed = 1;
221 }
222
223 b = 0;
224 if (s->buttons_state & MOUSE_EVENT_LBUTTON)
225 b |= 0x01;
226 if (s->buttons_state & MOUSE_EVENT_RBUTTON)
François Revol2ca20782009-08-25 11:14:10 +0200227 b |= 0x40;
balrogf6d2a312007-06-10 19:21:04 +0000228 if (s->buttons_state & MOUSE_EVENT_MBUTTON)
François Revol2ca20782009-08-25 11:14:10 +0200229 b |= 0x20; /* eraser */
balrogf6d2a312007-06-10 19:21:04 +0000230
231 if (len < 7)
232 return 0;
233
234 buf[0] = s->mode;
François Revol2ca20782009-08-25 11:14:10 +0200235 buf[5] = 0x00 | (b & 0xf0);
236 buf[1] = s->x & 0xff;
237 buf[2] = s->x >> 8;
238 buf[3] = s->y & 0xff;
239 buf[4] = s->y >> 8;
240 if (b & 0x3f) {
balrogf6d2a312007-06-10 19:21:04 +0000241 buf[6] = 0;
242 } else {
balrogf6d2a312007-06-10 19:21:04 +0000243 buf[6] = (unsigned char) -127;
244 }
245
246 return 7;
247}
248
249static void usb_wacom_handle_reset(USBDevice *dev)
250{
251 USBWacomState *s = (USBWacomState *) dev;
252
253 s->dx = 0;
254 s->dy = 0;
255 s->dz = 0;
256 s->x = 0;
257 s->y = 0;
258 s->buttons_state = 0;
259 s->mode = WACOM_MODE_HID;
260}
261
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100262static void usb_wacom_handle_control(USBDevice *dev, USBPacket *p,
Hans de Goede007fd622011-02-02 16:33:13 +0100263 int request, int value, int index, int length, uint8_t *data)
balrogf6d2a312007-06-10 19:21:04 +0000264{
265 USBWacomState *s = (USBWacomState *) dev;
Gerd Hoffmann037a5202010-11-25 16:12:06 +0100266 int ret;
balrogf6d2a312007-06-10 19:21:04 +0000267
Hans de Goede007fd622011-02-02 16:33:13 +0100268 ret = usb_desc_handle_control(dev, p, request, value, index, length, data);
Gerd Hoffmann037a5202010-11-25 16:12:06 +0100269 if (ret >= 0) {
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100270 return;
Gerd Hoffmann037a5202010-11-25 16:12:06 +0100271 }
272
balrogf6d2a312007-06-10 19:21:04 +0000273 switch (request) {
balrogf6d2a312007-06-10 19:21:04 +0000274 case WACOM_SET_REPORT:
Gerd Hoffmannb2d4d832010-10-08 12:30:14 +0200275 if (s->mouse_grabbed) {
276 qemu_remove_mouse_event_handler(s->eh_entry);
277 s->mouse_grabbed = 0;
278 }
balrogf6d2a312007-06-10 19:21:04 +0000279 s->mode = data[0];
balrogf6d2a312007-06-10 19:21:04 +0000280 break;
281 case WACOM_GET_REPORT:
282 data[0] = 0;
283 data[1] = s->mode;
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100284 p->actual_length = 2;
balrogf6d2a312007-06-10 19:21:04 +0000285 break;
286 /* USB HID requests */
287 case HID_GET_REPORT:
288 if (s->mode == WACOM_MODE_HID)
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100289 p->actual_length = usb_mouse_poll(s, data, length);
balrogf6d2a312007-06-10 19:21:04 +0000290 else if (s->mode == WACOM_MODE_WACOM)
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100291 p->actual_length = usb_wacom_poll(s, data, length);
balrogf6d2a312007-06-10 19:21:04 +0000292 break;
François Revol2ca20782009-08-25 11:14:10 +0200293 case HID_GET_IDLE:
François Revol2ca20782009-08-25 11:14:10 +0200294 data[0] = s->idle;
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100295 p->actual_length = 1;
François Revol2ca20782009-08-25 11:14:10 +0200296 break;
balrogf6d2a312007-06-10 19:21:04 +0000297 case HID_SET_IDLE:
François Revol2ca20782009-08-25 11:14:10 +0200298 s->idle = (uint8_t) (value >> 8);
balrogf6d2a312007-06-10 19:21:04 +0000299 break;
300 default:
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100301 p->status = USB_RET_STALL;
balrogf6d2a312007-06-10 19:21:04 +0000302 break;
303 }
balrogf6d2a312007-06-10 19:21:04 +0000304}
305
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100306static void usb_wacom_handle_data(USBDevice *dev, USBPacket *p)
balrogf6d2a312007-06-10 19:21:04 +0000307{
308 USBWacomState *s = (USBWacomState *) dev;
Gerd Hoffmann4f4321c2011-07-12 15:22:25 +0200309 uint8_t buf[p->iov.size];
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100310 int len = 0;
balrogf6d2a312007-06-10 19:21:04 +0000311
312 switch (p->pid) {
313 case USB_TOKEN_IN:
Gerd Hoffmann079d0b72012-01-12 13:23:01 +0100314 if (p->ep->nr == 1) {
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100315 if (!(s->changed || s->idle)) {
316 p->status = USB_RET_NAK;
317 return;
318 }
François Revol2ca20782009-08-25 11:14:10 +0200319 s->changed = 0;
balrogf6d2a312007-06-10 19:21:04 +0000320 if (s->mode == WACOM_MODE_HID)
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100321 len = usb_mouse_poll(s, buf, p->iov.size);
balrogf6d2a312007-06-10 19:21:04 +0000322 else if (s->mode == WACOM_MODE_WACOM)
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100323 len = usb_wacom_poll(s, buf, p->iov.size);
324 usb_packet_copy(p, buf, len);
balrogf6d2a312007-06-10 19:21:04 +0000325 break;
326 }
327 /* Fall through. */
328 case USB_TOKEN_OUT:
329 default:
Hans de Goede9a77a0f2012-11-01 17:15:01 +0100330 p->status = USB_RET_STALL;
balrogf6d2a312007-06-10 19:21:04 +0000331 }
balrogf6d2a312007-06-10 19:21:04 +0000332}
333
Marc-André Lureauc4fe9702017-02-21 18:14:45 +0400334static void usb_wacom_unrealize(USBDevice *dev, Error **errp)
balrogf6d2a312007-06-10 19:21:04 +0000335{
336 USBWacomState *s = (USBWacomState *) dev;
337
Gerd Hoffmannb2d4d832010-10-08 12:30:14 +0200338 if (s->mouse_grabbed) {
339 qemu_remove_mouse_event_handler(s->eh_entry);
340 s->mouse_grabbed = 0;
341 }
balrogf6d2a312007-06-10 19:21:04 +0000342}
343
Gonglei27107d42014-09-19 14:48:37 +0800344static void usb_wacom_realize(USBDevice *dev, Error **errp)
Gerd Hoffmann806b6022009-08-31 14:23:59 +0200345{
Gonglei924e5672015-05-06 20:55:34 +0800346 USBWacomState *s = USB_WACOM(dev);
Gerd Hoffmann9d55d1a2012-04-20 12:33:30 +0200347 usb_desc_create_serial(dev);
Gerd Hoffmanna980a062010-11-26 20:20:41 +0100348 usb_desc_init(dev);
Hans de Goede8beba932012-11-17 12:47:14 +0100349 s->intr = usb_ep_get(dev, USB_TOKEN_IN, 1);
François Revol2ca20782009-08-25 11:14:10 +0200350 s->changed = 1;
Gerd Hoffmann806b6022009-08-31 14:23:59 +0200351}
352
Gerd Hoffmannccce9fd2011-07-20 10:00:51 +0200353static const VMStateDescription vmstate_usb_wacom = {
354 .name = "usb-wacom",
355 .unmigratable = 1,
356};
357
Anthony Liguori39bffca2011-12-07 21:34:16 -0600358static void usb_wacom_class_init(ObjectClass *klass, void *data)
Anthony Liguori62aed762011-12-15 14:53:10 -0600359{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600360 DeviceClass *dc = DEVICE_CLASS(klass);
361 USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
Anthony Liguori62aed762011-12-15 14:53:10 -0600362
363 uc->product_desc = "QEMU PenPartner Tablet";
364 uc->usb_desc = &desc_wacom;
Gonglei27107d42014-09-19 14:48:37 +0800365 uc->realize = usb_wacom_realize;
Anthony Liguori62aed762011-12-15 14:53:10 -0600366 uc->handle_reset = usb_wacom_handle_reset;
367 uc->handle_control = usb_wacom_handle_control;
368 uc->handle_data = usb_wacom_handle_data;
Marc-André Lureauc4fe9702017-02-21 18:14:45 +0400369 uc->unrealize = usb_wacom_unrealize;
Marcel Apfelbaum125ee0e2013-07-29 17:17:45 +0300370 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
Anthony Liguori39bffca2011-12-07 21:34:16 -0600371 dc->desc = "QEMU PenPartner Tablet";
372 dc->vmsd = &vmstate_usb_wacom;
Anthony Liguori62aed762011-12-15 14:53:10 -0600373}
374
Andreas Färber8c43a6f2013-01-10 16:19:07 +0100375static const TypeInfo wacom_info = {
Gonglei924e5672015-05-06 20:55:34 +0800376 .name = TYPE_USB_WACOM,
Anthony Liguori39bffca2011-12-07 21:34:16 -0600377 .parent = TYPE_USB_DEVICE,
378 .instance_size = sizeof(USBWacomState),
379 .class_init = usb_wacom_class_init,
Gerd Hoffmann806b6022009-08-31 14:23:59 +0200380};
381
Andreas Färber83f7d432012-02-09 15:20:55 +0100382static void usb_wacom_register_types(void)
Gerd Hoffmann806b6022009-08-31 14:23:59 +0200383{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600384 type_register_static(&wacom_info);
Gonglei924e5672015-05-06 20:55:34 +0800385 usb_legacy_register(TYPE_USB_WACOM, "wacom-tablet", NULL);
Gerd Hoffmann806b6022009-08-31 14:23:59 +0200386}
Andreas Färber83f7d432012-02-09 15:20:55 +0100387
388type_init(usb_wacom_register_types)