]>
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" |
d6454270 | 32 | #include "migration/vmstate.h" |
0b8fa32f | 33 | #include "qemu/module.h" |
463581a8 | 34 | #include "desc.h" |
f6d2a316 AZ |
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; | |
8beba930 | 49 | USBEndpoint *intr; |
f6d2a316 AZ |
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; | |
2ca2078e FR |
58 | uint8_t idle; |
59 | int changed; | |
f6d2a316 AZ |
60 | } USBWacomState; |
61 | ||
924e567e GA |
62 | #define TYPE_USB_WACOM "usb-wacom-tablet" |
63 | #define USB_WACOM(obj) OBJECT_CHECK(USBWacomState, (obj), TYPE_USB_WACOM) | |
64 | ||
037a5203 GH |
65 | enum { |
66 | STR_MANUFACTURER = 1, | |
67 | STR_PRODUCT, | |
68 | STR_SERIALNUMBER, | |
69 | }; | |
f6d2a316 | 70 | |
037a5203 | 71 | static const USBDescStrings desc_strings = { |
93bfef4c | 72 | [STR_MANUFACTURER] = "QEMU", |
037a5203 GH |
73 | [STR_PRODUCT] = "Wacom PenPartner", |
74 | [STR_SERIALNUMBER] = "1", | |
75 | }; | |
f6d2a316 | 76 | |
037a5203 GH |
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 | }; | |
f6d2a316 | 107 | |
037a5203 GH |
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, | |
bd93976a | 116 | .bmAttributes = USB_CFG_ATT_ONE, |
037a5203 | 117 | .bMaxPower = 40, |
add75088 | 118 | .nif = 1, |
037a5203 GH |
119 | .ifs = &desc_iface_wacom, |
120 | }, | |
121 | }, | |
f6d2a316 AZ |
122 | }; |
123 | ||
037a5203 GH |
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, | |
f6d2a316 AZ |
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; | |
2ca2078e | 146 | s->changed = 1; |
8550a02d | 147 | usb_wakeup(s->intr, 0); |
f6d2a316 AZ |
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 | ||
2ca2078e FR |
155 | /* scale to Penpartner resolution */ |
156 | s->x = (x * 5040 / 0x7FFF); | |
157 | s->y = (y * 3780 / 0x7FFF); | |
f6d2a316 AZ |
158 | s->dz += dz; |
159 | s->buttons_state = buttons_state; | |
2ca2078e | 160 | s->changed = 1; |
8550a02d | 161 | usb_wakeup(s->intr, 0); |
f6d2a316 AZ |
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"); | |
b2d4d832 | 181 | qemu_activate_mouse_event_handler(s->eh_entry); |
f6d2a316 AZ |
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"); | |
b2d4d832 | 219 | qemu_activate_mouse_event_handler(s->eh_entry); |
f6d2a316 AZ |
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) | |
2ca2078e | 227 | b |= 0x40; |
f6d2a316 | 228 | if (s->buttons_state & MOUSE_EVENT_MBUTTON) |
2ca2078e | 229 | b |= 0x20; /* eraser */ |
f6d2a316 AZ |
230 | |
231 | if (len < 7) | |
232 | return 0; | |
233 | ||
234 | buf[0] = s->mode; | |
2ca2078e FR |
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) { | |
f6d2a316 AZ |
241 | buf[6] = 0; |
242 | } else { | |
f6d2a316 AZ |
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 | ||
9a77a0f5 | 262 | static void usb_wacom_handle_control(USBDevice *dev, USBPacket *p, |
007fd62f | 263 | int request, int value, int index, int length, uint8_t *data) |
f6d2a316 AZ |
264 | { |
265 | USBWacomState *s = (USBWacomState *) dev; | |
037a5203 GH |
266 | int ret; |
267 | ||
007fd62f | 268 | ret = usb_desc_handle_control(dev, p, request, value, index, length, data); |
037a5203 | 269 | if (ret >= 0) { |
9a77a0f5 | 270 | return; |
037a5203 | 271 | } |
f6d2a316 AZ |
272 | |
273 | switch (request) { | |
f6d2a316 | 274 | case WACOM_SET_REPORT: |
b2d4d832 GH |
275 | if (s->mouse_grabbed) { |
276 | qemu_remove_mouse_event_handler(s->eh_entry); | |
277 | s->mouse_grabbed = 0; | |
278 | } | |
f6d2a316 | 279 | s->mode = data[0]; |
f6d2a316 AZ |
280 | break; |
281 | case WACOM_GET_REPORT: | |
282 | data[0] = 0; | |
283 | data[1] = s->mode; | |
9a77a0f5 | 284 | p->actual_length = 2; |
f6d2a316 AZ |
285 | break; |
286 | /* USB HID requests */ | |
287 | case HID_GET_REPORT: | |
288 | if (s->mode == WACOM_MODE_HID) | |
9a77a0f5 | 289 | p->actual_length = usb_mouse_poll(s, data, length); |
f6d2a316 | 290 | else if (s->mode == WACOM_MODE_WACOM) |
9a77a0f5 | 291 | p->actual_length = usb_wacom_poll(s, data, length); |
f6d2a316 | 292 | break; |
2ca2078e | 293 | case HID_GET_IDLE: |
2ca2078e | 294 | data[0] = s->idle; |
9a77a0f5 | 295 | p->actual_length = 1; |
2ca2078e | 296 | break; |
f6d2a316 | 297 | case HID_SET_IDLE: |
2ca2078e | 298 | s->idle = (uint8_t) (value >> 8); |
f6d2a316 AZ |
299 | break; |
300 | default: | |
9a77a0f5 | 301 | p->status = USB_RET_STALL; |
f6d2a316 AZ |
302 | break; |
303 | } | |
f6d2a316 AZ |
304 | } |
305 | ||
9a77a0f5 | 306 | static void usb_wacom_handle_data(USBDevice *dev, USBPacket *p) |
f6d2a316 AZ |
307 | { |
308 | USBWacomState *s = (USBWacomState *) dev; | |
4f4321c1 | 309 | uint8_t buf[p->iov.size]; |
9a77a0f5 | 310 | int len = 0; |
f6d2a316 AZ |
311 | |
312 | switch (p->pid) { | |
313 | case USB_TOKEN_IN: | |
079d0b7f | 314 | if (p->ep->nr == 1) { |
9a77a0f5 HG |
315 | if (!(s->changed || s->idle)) { |
316 | p->status = USB_RET_NAK; | |
317 | return; | |
318 | } | |
2ca2078e | 319 | s->changed = 0; |
f6d2a316 | 320 | if (s->mode == WACOM_MODE_HID) |
9a77a0f5 | 321 | len = usb_mouse_poll(s, buf, p->iov.size); |
f6d2a316 | 322 | else if (s->mode == WACOM_MODE_WACOM) |
9a77a0f5 HG |
323 | len = usb_wacom_poll(s, buf, p->iov.size); |
324 | usb_packet_copy(p, buf, len); | |
f6d2a316 AZ |
325 | break; |
326 | } | |
327 | /* Fall through. */ | |
328 | case USB_TOKEN_OUT: | |
329 | default: | |
9a77a0f5 | 330 | p->status = USB_RET_STALL; |
f6d2a316 | 331 | } |
f6d2a316 AZ |
332 | } |
333 | ||
c4fe9700 | 334 | static void usb_wacom_unrealize(USBDevice *dev, Error **errp) |
f6d2a316 AZ |
335 | { |
336 | USBWacomState *s = (USBWacomState *) dev; | |
337 | ||
b2d4d832 GH |
338 | if (s->mouse_grabbed) { |
339 | qemu_remove_mouse_event_handler(s->eh_entry); | |
340 | s->mouse_grabbed = 0; | |
341 | } | |
f6d2a316 AZ |
342 | } |
343 | ||
27107d41 | 344 | static void usb_wacom_realize(USBDevice *dev, Error **errp) |
f6d2a316 | 345 | { |
924e567e | 346 | USBWacomState *s = USB_WACOM(dev); |
9d55d1ad | 347 | usb_desc_create_serial(dev); |
a980a065 | 348 | usb_desc_init(dev); |
8beba930 | 349 | s->intr = usb_ep_get(dev, USB_TOKEN_IN, 1); |
2ca2078e | 350 | s->changed = 1; |
806b6024 | 351 | } |
f6d2a316 | 352 | |
ccce9fd2 GH |
353 | static const VMStateDescription vmstate_usb_wacom = { |
354 | .name = "usb-wacom", | |
355 | .unmigratable = 1, | |
356 | }; | |
357 | ||
39bffca2 | 358 | static void usb_wacom_class_init(ObjectClass *klass, void *data) |
62aed765 | 359 | { |
39bffca2 AL |
360 | DeviceClass *dc = DEVICE_CLASS(klass); |
361 | USBDeviceClass *uc = USB_DEVICE_CLASS(klass); | |
62aed765 AL |
362 | |
363 | uc->product_desc = "QEMU PenPartner Tablet"; | |
364 | uc->usb_desc = &desc_wacom; | |
27107d41 | 365 | uc->realize = usb_wacom_realize; |
62aed765 AL |
366 | uc->handle_reset = usb_wacom_handle_reset; |
367 | uc->handle_control = usb_wacom_handle_control; | |
368 | uc->handle_data = usb_wacom_handle_data; | |
c4fe9700 | 369 | uc->unrealize = usb_wacom_unrealize; |
125ee0ed | 370 | set_bit(DEVICE_CATEGORY_INPUT, dc->categories); |
39bffca2 AL |
371 | dc->desc = "QEMU PenPartner Tablet"; |
372 | dc->vmsd = &vmstate_usb_wacom; | |
62aed765 AL |
373 | } |
374 | ||
8c43a6f0 | 375 | static const TypeInfo wacom_info = { |
924e567e | 376 | .name = TYPE_USB_WACOM, |
39bffca2 AL |
377 | .parent = TYPE_USB_DEVICE, |
378 | .instance_size = sizeof(USBWacomState), | |
379 | .class_init = usb_wacom_class_init, | |
806b6024 | 380 | }; |
f6d2a316 | 381 | |
83f7d43a | 382 | static void usb_wacom_register_types(void) |
806b6024 | 383 | { |
39bffca2 | 384 | type_register_static(&wacom_info); |
924e567e | 385 | usb_legacy_register(TYPE_USB_WACOM, "wacom-tablet", NULL); |
f6d2a316 | 386 | } |
83f7d43a AF |
387 | |
388 | type_init(usb_wacom_register_types) |