]> git.proxmox.com Git - mirror_qemu.git/blame - hw/usb/dev-wacom.c
migration: remove not used field xfer_limit
[mirror_qemu.git] / hw / usb / dev-wacom.c
CommitLineData
f6d2a316
AZ
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 */
e532b2e0 28#include "qemu/osdep.h"
f1ae32a1 29#include "hw/hw.h"
28ecbaee 30#include "ui/console.h"
f1ae32a1 31#include "hw/usb.h"
463581a8 32#include "desc.h"
f6d2a316
AZ
33
34/* Interface requests */
35#define WACOM_GET_REPORT 0x2101
36#define WACOM_SET_REPORT 0x2109
37
38/* HID interface requests */
39#define HID_GET_REPORT 0xa101
40#define HID_GET_IDLE 0xa102
41#define HID_GET_PROTOCOL 0xa103
42#define HID_SET_IDLE 0x210a
43#define HID_SET_PROTOCOL 0x210b
44
45typedef struct USBWacomState {
46 USBDevice dev;
8beba930 47 USBEndpoint *intr;
f6d2a316
AZ
48 QEMUPutMouseEntry *eh_entry;
49 int dx, dy, dz, buttons_state;
50 int x, y;
51 int mouse_grabbed;
52 enum {
53 WACOM_MODE_HID = 1,
54 WACOM_MODE_WACOM = 2,
55 } mode;
2ca2078e
FR
56 uint8_t idle;
57 int changed;
f6d2a316
AZ
58} USBWacomState;
59
924e567e
GA
60#define TYPE_USB_WACOM "usb-wacom-tablet"
61#define USB_WACOM(obj) OBJECT_CHECK(USBWacomState, (obj), TYPE_USB_WACOM)
62
037a5203
GH
63enum {
64 STR_MANUFACTURER = 1,
65 STR_PRODUCT,
66 STR_SERIALNUMBER,
67};
f6d2a316 68
037a5203 69static const USBDescStrings desc_strings = {
93bfef4c 70 [STR_MANUFACTURER] = "QEMU",
037a5203
GH
71 [STR_PRODUCT] = "Wacom PenPartner",
72 [STR_SERIALNUMBER] = "1",
73};
f6d2a316 74
037a5203
GH
75static const USBDescIface desc_iface_wacom = {
76 .bInterfaceNumber = 0,
77 .bNumEndpoints = 1,
78 .bInterfaceClass = USB_CLASS_HID,
79 .bInterfaceSubClass = 0x01, /* boot */
80 .bInterfaceProtocol = 0x02,
81 .ndesc = 1,
82 .descs = (USBDescOther[]) {
83 {
84 /* HID descriptor */
85 .data = (uint8_t[]) {
86 0x09, /* u8 bLength */
87 0x21, /* u8 bDescriptorType */
88 0x01, 0x10, /* u16 HID_class */
89 0x00, /* u8 country_code */
90 0x01, /* u8 num_descriptors */
91 0x22, /* u8 type: Report */
92 0x6e, 0, /* u16 len */
93 },
94 },
95 },
96 .eps = (USBDescEndpoint[]) {
97 {
98 .bEndpointAddress = USB_DIR_IN | 0x01,
99 .bmAttributes = USB_ENDPOINT_XFER_INT,
100 .wMaxPacketSize = 8,
101 .bInterval = 0x0a,
102 },
103 },
104};
f6d2a316 105
037a5203
GH
106static const USBDescDevice desc_device_wacom = {
107 .bcdUSB = 0x0110,
108 .bMaxPacketSize0 = 8,
109 .bNumConfigurations = 1,
110 .confs = (USBDescConfig[]) {
111 {
112 .bNumInterfaces = 1,
113 .bConfigurationValue = 1,
bd93976a 114 .bmAttributes = USB_CFG_ATT_ONE,
037a5203 115 .bMaxPower = 40,
add75088 116 .nif = 1,
037a5203
GH
117 .ifs = &desc_iface_wacom,
118 },
119 },
f6d2a316
AZ
120};
121
037a5203
GH
122static const USBDesc desc_wacom = {
123 .id = {
124 .idVendor = 0x056a,
125 .idProduct = 0x0000,
126 .bcdDevice = 0x4210,
127 .iManufacturer = STR_MANUFACTURER,
128 .iProduct = STR_PRODUCT,
129 .iSerialNumber = STR_SERIALNUMBER,
130 },
131 .full = &desc_device_wacom,
132 .str = desc_strings,
f6d2a316
AZ
133};
134
135static void usb_mouse_event(void *opaque,
136 int dx1, int dy1, int dz1, int buttons_state)
137{
138 USBWacomState *s = opaque;
139
140 s->dx += dx1;
141 s->dy += dy1;
142 s->dz += dz1;
143 s->buttons_state = buttons_state;
2ca2078e 144 s->changed = 1;
8550a02d 145 usb_wakeup(s->intr, 0);
f6d2a316
AZ
146}
147
148static void usb_wacom_event(void *opaque,
149 int x, int y, int dz, int buttons_state)
150{
151 USBWacomState *s = opaque;
152
2ca2078e
FR
153 /* scale to Penpartner resolution */
154 s->x = (x * 5040 / 0x7FFF);
155 s->y = (y * 3780 / 0x7FFF);
f6d2a316
AZ
156 s->dz += dz;
157 s->buttons_state = buttons_state;
2ca2078e 158 s->changed = 1;
8550a02d 159 usb_wakeup(s->intr, 0);
f6d2a316
AZ
160}
161
162static inline int int_clamp(int val, int vmin, int vmax)
163{
164 if (val < vmin)
165 return vmin;
166 else if (val > vmax)
167 return vmax;
168 else
169 return val;
170}
171
172static int usb_mouse_poll(USBWacomState *s, uint8_t *buf, int len)
173{
174 int dx, dy, dz, b, l;
175
176 if (!s->mouse_grabbed) {
177 s->eh_entry = qemu_add_mouse_event_handler(usb_mouse_event, s, 0,
178 "QEMU PenPartner tablet");
b2d4d832 179 qemu_activate_mouse_event_handler(s->eh_entry);
f6d2a316
AZ
180 s->mouse_grabbed = 1;
181 }
182
183 dx = int_clamp(s->dx, -128, 127);
184 dy = int_clamp(s->dy, -128, 127);
185 dz = int_clamp(s->dz, -128, 127);
186
187 s->dx -= dx;
188 s->dy -= dy;
189 s->dz -= dz;
190
191 b = 0;
192 if (s->buttons_state & MOUSE_EVENT_LBUTTON)
193 b |= 0x01;
194 if (s->buttons_state & MOUSE_EVENT_RBUTTON)
195 b |= 0x02;
196 if (s->buttons_state & MOUSE_EVENT_MBUTTON)
197 b |= 0x04;
198
199 buf[0] = b;
200 buf[1] = dx;
201 buf[2] = dy;
202 l = 3;
203 if (len >= 4) {
204 buf[3] = dz;
205 l = 4;
206 }
207 return l;
208}
209
210static int usb_wacom_poll(USBWacomState *s, uint8_t *buf, int len)
211{
212 int b;
213
214 if (!s->mouse_grabbed) {
215 s->eh_entry = qemu_add_mouse_event_handler(usb_wacom_event, s, 1,
216 "QEMU PenPartner tablet");
b2d4d832 217 qemu_activate_mouse_event_handler(s->eh_entry);
f6d2a316
AZ
218 s->mouse_grabbed = 1;
219 }
220
221 b = 0;
222 if (s->buttons_state & MOUSE_EVENT_LBUTTON)
223 b |= 0x01;
224 if (s->buttons_state & MOUSE_EVENT_RBUTTON)
2ca2078e 225 b |= 0x40;
f6d2a316 226 if (s->buttons_state & MOUSE_EVENT_MBUTTON)
2ca2078e 227 b |= 0x20; /* eraser */
f6d2a316
AZ
228
229 if (len < 7)
230 return 0;
231
232 buf[0] = s->mode;
2ca2078e
FR
233 buf[5] = 0x00 | (b & 0xf0);
234 buf[1] = s->x & 0xff;
235 buf[2] = s->x >> 8;
236 buf[3] = s->y & 0xff;
237 buf[4] = s->y >> 8;
238 if (b & 0x3f) {
f6d2a316
AZ
239 buf[6] = 0;
240 } else {
f6d2a316
AZ
241 buf[6] = (unsigned char) -127;
242 }
243
244 return 7;
245}
246
247static void usb_wacom_handle_reset(USBDevice *dev)
248{
249 USBWacomState *s = (USBWacomState *) dev;
250
251 s->dx = 0;
252 s->dy = 0;
253 s->dz = 0;
254 s->x = 0;
255 s->y = 0;
256 s->buttons_state = 0;
257 s->mode = WACOM_MODE_HID;
258}
259
9a77a0f5 260static void usb_wacom_handle_control(USBDevice *dev, USBPacket *p,
007fd62f 261 int request, int value, int index, int length, uint8_t *data)
f6d2a316
AZ
262{
263 USBWacomState *s = (USBWacomState *) dev;
037a5203
GH
264 int ret;
265
007fd62f 266 ret = usb_desc_handle_control(dev, p, request, value, index, length, data);
037a5203 267 if (ret >= 0) {
9a77a0f5 268 return;
037a5203 269 }
f6d2a316
AZ
270
271 switch (request) {
f6d2a316 272 case WACOM_SET_REPORT:
b2d4d832
GH
273 if (s->mouse_grabbed) {
274 qemu_remove_mouse_event_handler(s->eh_entry);
275 s->mouse_grabbed = 0;
276 }
f6d2a316 277 s->mode = data[0];
f6d2a316
AZ
278 break;
279 case WACOM_GET_REPORT:
280 data[0] = 0;
281 data[1] = s->mode;
9a77a0f5 282 p->actual_length = 2;
f6d2a316
AZ
283 break;
284 /* USB HID requests */
285 case HID_GET_REPORT:
286 if (s->mode == WACOM_MODE_HID)
9a77a0f5 287 p->actual_length = usb_mouse_poll(s, data, length);
f6d2a316 288 else if (s->mode == WACOM_MODE_WACOM)
9a77a0f5 289 p->actual_length = usb_wacom_poll(s, data, length);
f6d2a316 290 break;
2ca2078e 291 case HID_GET_IDLE:
2ca2078e 292 data[0] = s->idle;
9a77a0f5 293 p->actual_length = 1;
2ca2078e 294 break;
f6d2a316 295 case HID_SET_IDLE:
2ca2078e 296 s->idle = (uint8_t) (value >> 8);
f6d2a316
AZ
297 break;
298 default:
9a77a0f5 299 p->status = USB_RET_STALL;
f6d2a316
AZ
300 break;
301 }
f6d2a316
AZ
302}
303
9a77a0f5 304static void usb_wacom_handle_data(USBDevice *dev, USBPacket *p)
f6d2a316
AZ
305{
306 USBWacomState *s = (USBWacomState *) dev;
4f4321c1 307 uint8_t buf[p->iov.size];
9a77a0f5 308 int len = 0;
f6d2a316
AZ
309
310 switch (p->pid) {
311 case USB_TOKEN_IN:
079d0b7f 312 if (p->ep->nr == 1) {
9a77a0f5
HG
313 if (!(s->changed || s->idle)) {
314 p->status = USB_RET_NAK;
315 return;
316 }
2ca2078e 317 s->changed = 0;
f6d2a316 318 if (s->mode == WACOM_MODE_HID)
9a77a0f5 319 len = usb_mouse_poll(s, buf, p->iov.size);
f6d2a316 320 else if (s->mode == WACOM_MODE_WACOM)
9a77a0f5
HG
321 len = usb_wacom_poll(s, buf, p->iov.size);
322 usb_packet_copy(p, buf, len);
f6d2a316
AZ
323 break;
324 }
325 /* Fall through. */
326 case USB_TOKEN_OUT:
327 default:
9a77a0f5 328 p->status = USB_RET_STALL;
f6d2a316 329 }
f6d2a316
AZ
330}
331
c4fe9700 332static void usb_wacom_unrealize(USBDevice *dev, Error **errp)
f6d2a316
AZ
333{
334 USBWacomState *s = (USBWacomState *) dev;
335
b2d4d832
GH
336 if (s->mouse_grabbed) {
337 qemu_remove_mouse_event_handler(s->eh_entry);
338 s->mouse_grabbed = 0;
339 }
f6d2a316
AZ
340}
341
27107d41 342static void usb_wacom_realize(USBDevice *dev, Error **errp)
f6d2a316 343{
924e567e 344 USBWacomState *s = USB_WACOM(dev);
9d55d1ad 345 usb_desc_create_serial(dev);
a980a065 346 usb_desc_init(dev);
8beba930 347 s->intr = usb_ep_get(dev, USB_TOKEN_IN, 1);
2ca2078e 348 s->changed = 1;
806b6024 349}
f6d2a316 350
ccce9fd2
GH
351static const VMStateDescription vmstate_usb_wacom = {
352 .name = "usb-wacom",
353 .unmigratable = 1,
354};
355
39bffca2 356static void usb_wacom_class_init(ObjectClass *klass, void *data)
62aed765 357{
39bffca2
AL
358 DeviceClass *dc = DEVICE_CLASS(klass);
359 USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
62aed765
AL
360
361 uc->product_desc = "QEMU PenPartner Tablet";
362 uc->usb_desc = &desc_wacom;
27107d41 363 uc->realize = usb_wacom_realize;
62aed765
AL
364 uc->handle_reset = usb_wacom_handle_reset;
365 uc->handle_control = usb_wacom_handle_control;
366 uc->handle_data = usb_wacom_handle_data;
c4fe9700 367 uc->unrealize = usb_wacom_unrealize;
125ee0ed 368 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
39bffca2
AL
369 dc->desc = "QEMU PenPartner Tablet";
370 dc->vmsd = &vmstate_usb_wacom;
62aed765
AL
371}
372
8c43a6f0 373static const TypeInfo wacom_info = {
924e567e 374 .name = TYPE_USB_WACOM,
39bffca2
AL
375 .parent = TYPE_USB_DEVICE,
376 .instance_size = sizeof(USBWacomState),
377 .class_init = usb_wacom_class_init,
806b6024 378};
f6d2a316 379
83f7d43a 380static void usb_wacom_register_types(void)
806b6024 381{
39bffca2 382 type_register_static(&wacom_info);
924e567e 383 usb_legacy_register(TYPE_USB_WACOM, "wacom-tablet", NULL);
f6d2a316 384}
83f7d43a
AF
385
386type_init(usb_wacom_register_types)