meson: convert qapi-specific to meson
[qemu.git] / hw / usb / dev-wacom.c
1 /*
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 */
28
29 #include "qemu/osdep.h"
30 #include "ui/console.h"
31 #include "hw/usb.h"
32 #include "migration/vmstate.h"
33 #include "qemu/module.h"
34 #include "desc.h"
35
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
47 typedef struct USBWacomState {
48 USBDevice dev;
49 USBEndpoint *intr;
50 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;
58 uint8_t idle;
59 int changed;
60 } USBWacomState;
61
62 #define TYPE_USB_WACOM "usb-wacom-tablet"
63 #define USB_WACOM(obj) OBJECT_CHECK(USBWacomState, (obj), TYPE_USB_WACOM)
64
65 enum {
66 STR_MANUFACTURER = 1,
67 STR_PRODUCT,
68 STR_SERIALNUMBER,
69 };
70
71 static const USBDescStrings desc_strings = {
72 [STR_MANUFACTURER] = "QEMU",
73 [STR_PRODUCT] = "Wacom PenPartner",
74 [STR_SERIALNUMBER] = "1",
75 };
76
77 static 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 };
107
108 static const USBDescDevice desc_device_wacom = {
109 .bcdUSB = 0x0110,
110 .bMaxPacketSize0 = 8,
111 .bNumConfigurations = 1,
112 .confs = (USBDescConfig[]) {
113 {
114 .bNumInterfaces = 1,
115 .bConfigurationValue = 1,
116 .bmAttributes = USB_CFG_ATT_ONE,
117 .bMaxPower = 40,
118 .nif = 1,
119 .ifs = &desc_iface_wacom,
120 },
121 },
122 };
123
124 static 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,
135 };
136
137 static 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;
146 s->changed = 1;
147 usb_wakeup(s->intr, 0);
148 }
149
150 static void usb_wacom_event(void *opaque,
151 int x, int y, int dz, int buttons_state)
152 {
153 USBWacomState *s = opaque;
154
155 /* scale to Penpartner resolution */
156 s->x = (x * 5040 / 0x7FFF);
157 s->y = (y * 3780 / 0x7FFF);
158 s->dz += dz;
159 s->buttons_state = buttons_state;
160 s->changed = 1;
161 usb_wakeup(s->intr, 0);
162 }
163
164 static 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
174 static 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");
181 qemu_activate_mouse_event_handler(s->eh_entry);
182 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
212 static 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");
219 qemu_activate_mouse_event_handler(s->eh_entry);
220 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)
227 b |= 0x40;
228 if (s->buttons_state & MOUSE_EVENT_MBUTTON)
229 b |= 0x20; /* eraser */
230
231 if (len < 7)
232 return 0;
233
234 buf[0] = s->mode;
235 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) {
241 buf[6] = 0;
242 } else {
243 buf[6] = (unsigned char) -127;
244 }
245
246 return 7;
247 }
248
249 static 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
262 static void usb_wacom_handle_control(USBDevice *dev, USBPacket *p,
263 int request, int value, int index, int length, uint8_t *data)
264 {
265 USBWacomState *s = (USBWacomState *) dev;
266 int ret;
267
268 ret = usb_desc_handle_control(dev, p, request, value, index, length, data);
269 if (ret >= 0) {
270 return;
271 }
272
273 switch (request) {
274 case WACOM_SET_REPORT:
275 if (s->mouse_grabbed) {
276 qemu_remove_mouse_event_handler(s->eh_entry);
277 s->mouse_grabbed = 0;
278 }
279 s->mode = data[0];
280 break;
281 case WACOM_GET_REPORT:
282 data[0] = 0;
283 data[1] = s->mode;
284 p->actual_length = 2;
285 break;
286 /* USB HID requests */
287 case HID_GET_REPORT:
288 if (s->mode == WACOM_MODE_HID)
289 p->actual_length = usb_mouse_poll(s, data, length);
290 else if (s->mode == WACOM_MODE_WACOM)
291 p->actual_length = usb_wacom_poll(s, data, length);
292 break;
293 case HID_GET_IDLE:
294 data[0] = s->idle;
295 p->actual_length = 1;
296 break;
297 case HID_SET_IDLE:
298 s->idle = (uint8_t) (value >> 8);
299 break;
300 default:
301 p->status = USB_RET_STALL;
302 break;
303 }
304 }
305
306 static void usb_wacom_handle_data(USBDevice *dev, USBPacket *p)
307 {
308 USBWacomState *s = (USBWacomState *) dev;
309 uint8_t buf[p->iov.size];
310 int len = 0;
311
312 switch (p->pid) {
313 case USB_TOKEN_IN:
314 if (p->ep->nr == 1) {
315 if (!(s->changed || s->idle)) {
316 p->status = USB_RET_NAK;
317 return;
318 }
319 s->changed = 0;
320 if (s->mode == WACOM_MODE_HID)
321 len = usb_mouse_poll(s, buf, p->iov.size);
322 else if (s->mode == WACOM_MODE_WACOM)
323 len = usb_wacom_poll(s, buf, p->iov.size);
324 usb_packet_copy(p, buf, len);
325 break;
326 }
327 /* Fall through. */
328 case USB_TOKEN_OUT:
329 default:
330 p->status = USB_RET_STALL;
331 }
332 }
333
334 static void usb_wacom_unrealize(USBDevice *dev)
335 {
336 USBWacomState *s = (USBWacomState *) dev;
337
338 if (s->mouse_grabbed) {
339 qemu_remove_mouse_event_handler(s->eh_entry);
340 s->mouse_grabbed = 0;
341 }
342 }
343
344 static void usb_wacom_realize(USBDevice *dev, Error **errp)
345 {
346 USBWacomState *s = USB_WACOM(dev);
347 usb_desc_create_serial(dev);
348 usb_desc_init(dev);
349 s->intr = usb_ep_get(dev, USB_TOKEN_IN, 1);
350 s->changed = 1;
351 }
352
353 static const VMStateDescription vmstate_usb_wacom = {
354 .name = "usb-wacom",
355 .unmigratable = 1,
356 };
357
358 static void usb_wacom_class_init(ObjectClass *klass, void *data)
359 {
360 DeviceClass *dc = DEVICE_CLASS(klass);
361 USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
362
363 uc->product_desc = "QEMU PenPartner Tablet";
364 uc->usb_desc = &desc_wacom;
365 uc->realize = usb_wacom_realize;
366 uc->handle_reset = usb_wacom_handle_reset;
367 uc->handle_control = usb_wacom_handle_control;
368 uc->handle_data = usb_wacom_handle_data;
369 uc->unrealize = usb_wacom_unrealize;
370 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
371 dc->desc = "QEMU PenPartner Tablet";
372 dc->vmsd = &vmstate_usb_wacom;
373 }
374
375 static const TypeInfo wacom_info = {
376 .name = TYPE_USB_WACOM,
377 .parent = TYPE_USB_DEVICE,
378 .instance_size = sizeof(USBWacomState),
379 .class_init = usb_wacom_class_init,
380 };
381
382 static void usb_wacom_register_types(void)
383 {
384 type_register_static(&wacom_info);
385 usb_legacy_register(TYPE_USB_WACOM, "wacom-tablet", NULL);
386 }
387
388 type_init(usb_wacom_register_types)