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