]>
Commit | Line | Data |
---|---|---|
f6d2a316 AZ |
1 | /* |
2 | * Wacom PenPartner USB tablet emulation. | |
3 | * | |
4 | * Copyright (c) 2006 Openedhand Ltd. | |
5 | * Author: Andrzej Zaborowski <[email protected]> | |
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 | */ | |
0b8fa32f | 28 | |
e532b2e0 | 29 | #include "qemu/osdep.h" |
28ecbaee | 30 | #include "ui/console.h" |
f1ae32a1 | 31 | #include "hw/usb.h" |
84b6c236 | 32 | #include "hw/usb/hid.h" |
d6454270 | 33 | #include "migration/vmstate.h" |
0b8fa32f | 34 | #include "qemu/module.h" |
463581a8 | 35 | #include "desc.h" |
db1015e9 | 36 | #include "qom/object.h" |
f6d2a316 AZ |
37 | |
38 | /* Interface requests */ | |
6c10e08a AG |
39 | #define WACOM_GET_REPORT 0x2101 |
40 | #define WACOM_SET_REPORT 0x2109 | |
f6d2a316 | 41 | |
db1015e9 | 42 | struct USBWacomState { |
f6d2a316 | 43 | USBDevice dev; |
8beba930 | 44 | USBEndpoint *intr; |
f6d2a316 AZ |
45 | QEMUPutMouseEntry *eh_entry; |
46 | int dx, dy, dz, buttons_state; | |
47 | int x, y; | |
48 | int mouse_grabbed; | |
49 | enum { | |
50 | WACOM_MODE_HID = 1, | |
51 | WACOM_MODE_WACOM = 2, | |
52 | } mode; | |
2ca2078e FR |
53 | uint8_t idle; |
54 | int changed; | |
db1015e9 | 55 | }; |
f6d2a316 | 56 | |
924e567e | 57 | #define TYPE_USB_WACOM "usb-wacom-tablet" |
8063396b | 58 | OBJECT_DECLARE_SIMPLE_TYPE(USBWacomState, USB_WACOM) |
924e567e | 59 | |
037a5203 GH |
60 | enum { |
61 | STR_MANUFACTURER = 1, | |
62 | STR_PRODUCT, | |
63 | STR_SERIALNUMBER, | |
64 | }; | |
f6d2a316 | 65 | |
037a5203 | 66 | static const USBDescStrings desc_strings = { |
93bfef4c | 67 | [STR_MANUFACTURER] = "QEMU", |
037a5203 GH |
68 | [STR_PRODUCT] = "Wacom PenPartner", |
69 | [STR_SERIALNUMBER] = "1", | |
70 | }; | |
f6d2a316 | 71 | |
5280117b DB |
72 | static const uint8_t qemu_wacom_hid_report_descriptor[] = { |
73 | 0x05, 0x01, /* Usage Page (Desktop) */ | |
74 | 0x09, 0x02, /* Usage (Mouse) */ | |
75 | 0xa1, 0x01, /* Collection (Application) */ | |
76 | 0x85, 0x01, /* Report ID (1) */ | |
77 | 0x09, 0x01, /* Usage (Pointer) */ | |
78 | 0xa1, 0x00, /* Collection (Physical) */ | |
79 | 0x05, 0x09, /* Usage Page (Button) */ | |
80 | 0x19, 0x01, /* Usage Minimum (01h) */ | |
81 | 0x29, 0x03, /* Usage Maximum (03h) */ | |
82 | 0x15, 0x00, /* Logical Minimum (0) */ | |
83 | 0x25, 0x01, /* Logical Maximum (1) */ | |
84 | 0x95, 0x03, /* Report Count (3) */ | |
85 | 0x75, 0x01, /* Report Size (1) */ | |
86 | 0x81, 0x02, /* Input (Data, Variable, Absolute) */ | |
87 | 0x95, 0x01, /* Report Count (1) */ | |
88 | 0x75, 0x05, /* Report Size (5) */ | |
89 | 0x81, 0x01, /* Input (Constant) */ | |
90 | 0x05, 0x01, /* Usage Page (Desktop) */ | |
91 | 0x09, 0x30, /* Usage (X) */ | |
92 | 0x09, 0x31, /* Usage (Y) */ | |
93 | 0x09, 0x38, /* Usage (Wheel) */ | |
94 | 0x15, 0x81, /* Logical Minimum (-127) */ | |
95 | 0x25, 0x7f, /* Logical Maximum (127) */ | |
96 | 0x75, 0x08, /* Report Size (8) */ | |
97 | 0x95, 0x03, /* Report Count (3) */ | |
98 | 0x81, 0x06, /* Input (Data, Variable, Relative) */ | |
99 | 0x95, 0x03, /* Report Count (3) */ | |
100 | 0x81, 0x01, /* Input (Constant) */ | |
101 | 0xc0, /* End Collection */ | |
102 | 0xc0, /* End Collection */ | |
103 | 0x05, 0x0d, /* Usage Page (Digitizer) */ | |
104 | 0x09, 0x01, /* Usage (Digitizer) */ | |
105 | 0xa1, 0x01, /* Collection (Application) */ | |
106 | 0x85, 0x02, /* Report ID (2) */ | |
107 | 0xa1, 0x00, /* Collection (Physical) */ | |
108 | 0x06, 0x00, 0xff,/* Usage Page (ff00h), vendor-defined */ | |
109 | 0x09, 0x01, /* Usage (01h) */ | |
110 | 0x15, 0x00, /* Logical Minimum (0) */ | |
111 | 0x26, 0xff, 0x00,/* Logical Maximum (255) */ | |
112 | 0x75, 0x08, /* Report Size (8) */ | |
113 | 0x95, 0x07, /* Report Count (7) */ | |
114 | 0x81, 0x02, /* Input (Data, Variable, Absolute) */ | |
115 | 0xc0, /* End Collection */ | |
116 | 0x09, 0x01, /* Usage (01h) */ | |
117 | 0x85, 0x63, /* Report ID (99) */ | |
118 | 0x95, 0x07, /* Report Count (7) */ | |
119 | 0x81, 0x02, /* Input (Data, Variable, Absolute) */ | |
120 | 0x09, 0x01, /* Usage (01h) */ | |
121 | 0x85, 0x02, /* Report ID (2) */ | |
122 | 0x95, 0x01, /* Report Count (1) */ | |
123 | 0xb1, 0x02, /* Feature (Variable) */ | |
124 | 0x09, 0x01, /* Usage (01h) */ | |
125 | 0x85, 0x03, /* Report ID (3) */ | |
126 | 0x95, 0x01, /* Report Count (1) */ | |
127 | 0xb1, 0x02, /* Feature (Variable) */ | |
128 | 0xc0 /* End Collection */ | |
129 | }; | |
130 | ||
037a5203 GH |
131 | static const USBDescIface desc_iface_wacom = { |
132 | .bInterfaceNumber = 0, | |
133 | .bNumEndpoints = 1, | |
134 | .bInterfaceClass = USB_CLASS_HID, | |
135 | .bInterfaceSubClass = 0x01, /* boot */ | |
136 | .bInterfaceProtocol = 0x02, | |
137 | .ndesc = 1, | |
138 | .descs = (USBDescOther[]) { | |
139 | { | |
140 | /* HID descriptor */ | |
141 | .data = (uint8_t[]) { | |
142 | 0x09, /* u8 bLength */ | |
84b6c236 | 143 | USB_DT_HID, /* u8 bDescriptorType */ |
037a5203 GH |
144 | 0x01, 0x10, /* u16 HID_class */ |
145 | 0x00, /* u8 country_code */ | |
146 | 0x01, /* u8 num_descriptors */ | |
84b6c236 | 147 | USB_DT_REPORT, /* u8 type: Report */ |
5280117b | 148 | sizeof(qemu_wacom_hid_report_descriptor), 0, /* u16 len */ |
037a5203 GH |
149 | }, |
150 | }, | |
151 | }, | |
152 | .eps = (USBDescEndpoint[]) { | |
153 | { | |
154 | .bEndpointAddress = USB_DIR_IN | 0x01, | |
155 | .bmAttributes = USB_ENDPOINT_XFER_INT, | |
156 | .wMaxPacketSize = 8, | |
157 | .bInterval = 0x0a, | |
158 | }, | |
159 | }, | |
160 | }; | |
f6d2a316 | 161 | |
037a5203 GH |
162 | static const USBDescDevice desc_device_wacom = { |
163 | .bcdUSB = 0x0110, | |
164 | .bMaxPacketSize0 = 8, | |
165 | .bNumConfigurations = 1, | |
166 | .confs = (USBDescConfig[]) { | |
167 | { | |
168 | .bNumInterfaces = 1, | |
169 | .bConfigurationValue = 1, | |
bd93976a | 170 | .bmAttributes = USB_CFG_ATT_ONE, |
037a5203 | 171 | .bMaxPower = 40, |
add75088 | 172 | .nif = 1, |
037a5203 GH |
173 | .ifs = &desc_iface_wacom, |
174 | }, | |
175 | }, | |
f6d2a316 AZ |
176 | }; |
177 | ||
037a5203 GH |
178 | static const USBDesc desc_wacom = { |
179 | .id = { | |
180 | .idVendor = 0x056a, | |
181 | .idProduct = 0x0000, | |
182 | .bcdDevice = 0x4210, | |
183 | .iManufacturer = STR_MANUFACTURER, | |
184 | .iProduct = STR_PRODUCT, | |
185 | .iSerialNumber = STR_SERIALNUMBER, | |
186 | }, | |
187 | .full = &desc_device_wacom, | |
188 | .str = desc_strings, | |
f6d2a316 AZ |
189 | }; |
190 | ||
191 | static void usb_mouse_event(void *opaque, | |
192 | int dx1, int dy1, int dz1, int buttons_state) | |
193 | { | |
194 | USBWacomState *s = opaque; | |
195 | ||
196 | s->dx += dx1; | |
197 | s->dy += dy1; | |
198 | s->dz += dz1; | |
199 | s->buttons_state = buttons_state; | |
2ca2078e | 200 | s->changed = 1; |
8550a02d | 201 | usb_wakeup(s->intr, 0); |
f6d2a316 AZ |
202 | } |
203 | ||
204 | static void usb_wacom_event(void *opaque, | |
205 | int x, int y, int dz, int buttons_state) | |
206 | { | |
207 | USBWacomState *s = opaque; | |
208 | ||
2ca2078e FR |
209 | /* scale to Penpartner resolution */ |
210 | s->x = (x * 5040 / 0x7FFF); | |
211 | s->y = (y * 3780 / 0x7FFF); | |
f6d2a316 AZ |
212 | s->dz += dz; |
213 | s->buttons_state = buttons_state; | |
2ca2078e | 214 | s->changed = 1; |
8550a02d | 215 | usb_wakeup(s->intr, 0); |
f6d2a316 AZ |
216 | } |
217 | ||
218 | static inline int int_clamp(int val, int vmin, int vmax) | |
219 | { | |
220 | if (val < vmin) | |
221 | return vmin; | |
222 | else if (val > vmax) | |
223 | return vmax; | |
224 | else | |
225 | return val; | |
226 | } | |
227 | ||
228 | static int usb_mouse_poll(USBWacomState *s, uint8_t *buf, int len) | |
229 | { | |
230 | int dx, dy, dz, b, l; | |
231 | ||
232 | if (!s->mouse_grabbed) { | |
233 | s->eh_entry = qemu_add_mouse_event_handler(usb_mouse_event, s, 0, | |
234 | "QEMU PenPartner tablet"); | |
b2d4d832 | 235 | qemu_activate_mouse_event_handler(s->eh_entry); |
f6d2a316 AZ |
236 | s->mouse_grabbed = 1; |
237 | } | |
238 | ||
239 | dx = int_clamp(s->dx, -128, 127); | |
240 | dy = int_clamp(s->dy, -128, 127); | |
241 | dz = int_clamp(s->dz, -128, 127); | |
242 | ||
243 | s->dx -= dx; | |
244 | s->dy -= dy; | |
245 | s->dz -= dz; | |
246 | ||
247 | b = 0; | |
248 | if (s->buttons_state & MOUSE_EVENT_LBUTTON) | |
249 | b |= 0x01; | |
250 | if (s->buttons_state & MOUSE_EVENT_RBUTTON) | |
251 | b |= 0x02; | |
252 | if (s->buttons_state & MOUSE_EVENT_MBUTTON) | |
253 | b |= 0x04; | |
254 | ||
255 | buf[0] = b; | |
256 | buf[1] = dx; | |
257 | buf[2] = dy; | |
258 | l = 3; | |
259 | if (len >= 4) { | |
260 | buf[3] = dz; | |
261 | l = 4; | |
262 | } | |
263 | return l; | |
264 | } | |
265 | ||
266 | static int usb_wacom_poll(USBWacomState *s, uint8_t *buf, int len) | |
267 | { | |
268 | int b; | |
269 | ||
270 | if (!s->mouse_grabbed) { | |
271 | s->eh_entry = qemu_add_mouse_event_handler(usb_wacom_event, s, 1, | |
272 | "QEMU PenPartner tablet"); | |
b2d4d832 | 273 | qemu_activate_mouse_event_handler(s->eh_entry); |
f6d2a316 AZ |
274 | s->mouse_grabbed = 1; |
275 | } | |
276 | ||
277 | b = 0; | |
278 | if (s->buttons_state & MOUSE_EVENT_LBUTTON) | |
279 | b |= 0x01; | |
280 | if (s->buttons_state & MOUSE_EVENT_RBUTTON) | |
2ca2078e | 281 | b |= 0x40; |
f6d2a316 | 282 | if (s->buttons_state & MOUSE_EVENT_MBUTTON) |
2ca2078e | 283 | b |= 0x20; /* eraser */ |
f6d2a316 AZ |
284 | |
285 | if (len < 7) | |
286 | return 0; | |
287 | ||
288 | buf[0] = s->mode; | |
2ca2078e FR |
289 | buf[5] = 0x00 | (b & 0xf0); |
290 | buf[1] = s->x & 0xff; | |
291 | buf[2] = s->x >> 8; | |
292 | buf[3] = s->y & 0xff; | |
293 | buf[4] = s->y >> 8; | |
294 | if (b & 0x3f) { | |
f6d2a316 AZ |
295 | buf[6] = 0; |
296 | } else { | |
f6d2a316 AZ |
297 | buf[6] = (unsigned char) -127; |
298 | } | |
299 | ||
300 | return 7; | |
301 | } | |
302 | ||
303 | static void usb_wacom_handle_reset(USBDevice *dev) | |
304 | { | |
305 | USBWacomState *s = (USBWacomState *) dev; | |
306 | ||
307 | s->dx = 0; | |
308 | s->dy = 0; | |
309 | s->dz = 0; | |
310 | s->x = 0; | |
311 | s->y = 0; | |
312 | s->buttons_state = 0; | |
313 | s->mode = WACOM_MODE_HID; | |
314 | } | |
315 | ||
9a77a0f5 | 316 | static void usb_wacom_handle_control(USBDevice *dev, USBPacket *p, |
007fd62f | 317 | int request, int value, int index, int length, uint8_t *data) |
f6d2a316 AZ |
318 | { |
319 | USBWacomState *s = (USBWacomState *) dev; | |
037a5203 GH |
320 | int ret; |
321 | ||
007fd62f | 322 | ret = usb_desc_handle_control(dev, p, request, value, index, length, data); |
037a5203 | 323 | if (ret >= 0) { |
9a77a0f5 | 324 | return; |
037a5203 | 325 | } |
f6d2a316 AZ |
326 | |
327 | switch (request) { | |
5280117b DB |
328 | case InterfaceRequest | USB_REQ_GET_DESCRIPTOR: |
329 | switch (value >> 8) { | |
330 | case 0x22: | |
331 | memcpy(data, qemu_wacom_hid_report_descriptor, | |
332 | sizeof(qemu_wacom_hid_report_descriptor)); | |
333 | p->actual_length = sizeof(qemu_wacom_hid_report_descriptor); | |
334 | break; | |
335 | default: | |
336 | return; | |
337 | } | |
338 | break; | |
f6d2a316 | 339 | case WACOM_SET_REPORT: |
b2d4d832 GH |
340 | if (s->mouse_grabbed) { |
341 | qemu_remove_mouse_event_handler(s->eh_entry); | |
342 | s->mouse_grabbed = 0; | |
343 | } | |
f6d2a316 | 344 | s->mode = data[0]; |
f6d2a316 AZ |
345 | break; |
346 | case WACOM_GET_REPORT: | |
347 | data[0] = 0; | |
348 | data[1] = s->mode; | |
9a77a0f5 | 349 | p->actual_length = 2; |
f6d2a316 AZ |
350 | break; |
351 | /* USB HID requests */ | |
352 | case HID_GET_REPORT: | |
353 | if (s->mode == WACOM_MODE_HID) | |
9a77a0f5 | 354 | p->actual_length = usb_mouse_poll(s, data, length); |
f6d2a316 | 355 | else if (s->mode == WACOM_MODE_WACOM) |
9a77a0f5 | 356 | p->actual_length = usb_wacom_poll(s, data, length); |
f6d2a316 | 357 | break; |
2ca2078e | 358 | case HID_GET_IDLE: |
2ca2078e | 359 | data[0] = s->idle; |
9a77a0f5 | 360 | p->actual_length = 1; |
2ca2078e | 361 | break; |
f6d2a316 | 362 | case HID_SET_IDLE: |
2ca2078e | 363 | s->idle = (uint8_t) (value >> 8); |
f6d2a316 AZ |
364 | break; |
365 | default: | |
9a77a0f5 | 366 | p->status = USB_RET_STALL; |
f6d2a316 AZ |
367 | break; |
368 | } | |
f6d2a316 AZ |
369 | } |
370 | ||
9a77a0f5 | 371 | static void usb_wacom_handle_data(USBDevice *dev, USBPacket *p) |
f6d2a316 AZ |
372 | { |
373 | USBWacomState *s = (USBWacomState *) dev; | |
3f67e2e7 | 374 | g_autofree uint8_t *buf = g_malloc(p->iov.size); |
9a77a0f5 | 375 | int len = 0; |
f6d2a316 AZ |
376 | |
377 | switch (p->pid) { | |
378 | case USB_TOKEN_IN: | |
079d0b7f | 379 | if (p->ep->nr == 1) { |
9a77a0f5 HG |
380 | if (!(s->changed || s->idle)) { |
381 | p->status = USB_RET_NAK; | |
382 | return; | |
383 | } | |
2ca2078e | 384 | s->changed = 0; |
f6d2a316 | 385 | if (s->mode == WACOM_MODE_HID) |
9a77a0f5 | 386 | len = usb_mouse_poll(s, buf, p->iov.size); |
f6d2a316 | 387 | else if (s->mode == WACOM_MODE_WACOM) |
9a77a0f5 HG |
388 | len = usb_wacom_poll(s, buf, p->iov.size); |
389 | usb_packet_copy(p, buf, len); | |
f6d2a316 AZ |
390 | break; |
391 | } | |
392 | /* Fall through. */ | |
393 | case USB_TOKEN_OUT: | |
394 | default: | |
9a77a0f5 | 395 | p->status = USB_RET_STALL; |
f6d2a316 | 396 | } |
f6d2a316 AZ |
397 | } |
398 | ||
b69c3c21 | 399 | static void usb_wacom_unrealize(USBDevice *dev) |
f6d2a316 AZ |
400 | { |
401 | USBWacomState *s = (USBWacomState *) dev; | |
402 | ||
b2d4d832 GH |
403 | if (s->mouse_grabbed) { |
404 | qemu_remove_mouse_event_handler(s->eh_entry); | |
405 | s->mouse_grabbed = 0; | |
406 | } | |
f6d2a316 AZ |
407 | } |
408 | ||
27107d41 | 409 | static void usb_wacom_realize(USBDevice *dev, Error **errp) |
f6d2a316 | 410 | { |
924e567e | 411 | USBWacomState *s = USB_WACOM(dev); |
9d55d1ad | 412 | usb_desc_create_serial(dev); |
a980a065 | 413 | usb_desc_init(dev); |
8beba930 | 414 | s->intr = usb_ep_get(dev, USB_TOKEN_IN, 1); |
2ca2078e | 415 | s->changed = 1; |
806b6024 | 416 | } |
f6d2a316 | 417 | |
ccce9fd2 GH |
418 | static const VMStateDescription vmstate_usb_wacom = { |
419 | .name = "usb-wacom", | |
420 | .unmigratable = 1, | |
421 | }; | |
422 | ||
39bffca2 | 423 | static void usb_wacom_class_init(ObjectClass *klass, void *data) |
62aed765 | 424 | { |
39bffca2 AL |
425 | DeviceClass *dc = DEVICE_CLASS(klass); |
426 | USBDeviceClass *uc = USB_DEVICE_CLASS(klass); | |
62aed765 AL |
427 | |
428 | uc->product_desc = "QEMU PenPartner Tablet"; | |
429 | uc->usb_desc = &desc_wacom; | |
27107d41 | 430 | uc->realize = usb_wacom_realize; |
62aed765 AL |
431 | uc->handle_reset = usb_wacom_handle_reset; |
432 | uc->handle_control = usb_wacom_handle_control; | |
433 | uc->handle_data = usb_wacom_handle_data; | |
c4fe9700 | 434 | uc->unrealize = usb_wacom_unrealize; |
125ee0ed | 435 | set_bit(DEVICE_CATEGORY_INPUT, dc->categories); |
39bffca2 AL |
436 | dc->desc = "QEMU PenPartner Tablet"; |
437 | dc->vmsd = &vmstate_usb_wacom; | |
62aed765 AL |
438 | } |
439 | ||
8c43a6f0 | 440 | static const TypeInfo wacom_info = { |
924e567e | 441 | .name = TYPE_USB_WACOM, |
39bffca2 AL |
442 | .parent = TYPE_USB_DEVICE, |
443 | .instance_size = sizeof(USBWacomState), | |
444 | .class_init = usb_wacom_class_init, | |
806b6024 | 445 | }; |
f6d2a316 | 446 | |
83f7d43a | 447 | static void usb_wacom_register_types(void) |
806b6024 | 448 | { |
39bffca2 | 449 | type_register_static(&wacom_info); |
924e567e | 450 | usb_legacy_register(TYPE_USB_WACOM, "wacom-tablet", NULL); |
f6d2a316 | 451 | } |
83f7d43a AF |
452 | |
453 | type_init(usb_wacom_register_types) |