2 * Wacom PenPartner USB tablet emulation.
4 * Copyright (c) 2006 Openedhand Ltd.
5 * Author: Andrzej Zaborowski <balrog@zabor.org>
7 * Based on hw/usb-hid.c:
8 * Copyright (c) 2005 Fabrice Bellard
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:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
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
28 #include "qemu/osdep.h"
30 #include "ui/console.h"
32 #include "hw/usb/desc.h"
34 /* Interface requests */
35 #define WACOM_GET_REPORT 0x2101
36 #define WACOM_SET_REPORT 0x2109
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
45 typedef struct USBWacomState
{
48 QEMUPutMouseEntry
*eh_entry
;
49 int dx
, dy
, dz
, buttons_state
;
60 #define TYPE_USB_WACOM "usb-wacom-tablet"
61 #define USB_WACOM(obj) OBJECT_CHECK(USBWacomState, (obj), TYPE_USB_WACOM)
69 static const USBDescStrings desc_strings
= {
70 [STR_MANUFACTURER
] = "QEMU",
71 [STR_PRODUCT
] = "Wacom PenPartner",
72 [STR_SERIALNUMBER
] = "1",
75 static const USBDescIface desc_iface_wacom
= {
76 .bInterfaceNumber
= 0,
78 .bInterfaceClass
= USB_CLASS_HID
,
79 .bInterfaceSubClass
= 0x01, /* boot */
80 .bInterfaceProtocol
= 0x02,
82 .descs
= (USBDescOther
[]) {
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 */
96 .eps
= (USBDescEndpoint
[]) {
98 .bEndpointAddress
= USB_DIR_IN
| 0x01,
99 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
106 static const USBDescDevice desc_device_wacom
= {
108 .bMaxPacketSize0
= 8,
109 .bNumConfigurations
= 1,
110 .confs
= (USBDescConfig
[]) {
113 .bConfigurationValue
= 1,
114 .bmAttributes
= USB_CFG_ATT_ONE
,
117 .ifs
= &desc_iface_wacom
,
122 static const USBDesc desc_wacom
= {
127 .iManufacturer
= STR_MANUFACTURER
,
128 .iProduct
= STR_PRODUCT
,
129 .iSerialNumber
= STR_SERIALNUMBER
,
131 .full
= &desc_device_wacom
,
135 static void usb_mouse_event(void *opaque
,
136 int dx1
, int dy1
, int dz1
, int buttons_state
)
138 USBWacomState
*s
= opaque
;
143 s
->buttons_state
= buttons_state
;
145 usb_wakeup(s
->intr
, 0);
148 static void usb_wacom_event(void *opaque
,
149 int x
, int y
, int dz
, int buttons_state
)
151 USBWacomState
*s
= opaque
;
153 /* scale to Penpartner resolution */
154 s
->x
= (x
* 5040 / 0x7FFF);
155 s
->y
= (y
* 3780 / 0x7FFF);
157 s
->buttons_state
= buttons_state
;
159 usb_wakeup(s
->intr
, 0);
162 static inline int int_clamp(int val
, int vmin
, int vmax
)
172 static int usb_mouse_poll(USBWacomState
*s
, uint8_t *buf
, int len
)
174 int dx
, dy
, dz
, b
, l
;
176 if (!s
->mouse_grabbed
) {
177 s
->eh_entry
= qemu_add_mouse_event_handler(usb_mouse_event
, s
, 0,
178 "QEMU PenPartner tablet");
179 qemu_activate_mouse_event_handler(s
->eh_entry
);
180 s
->mouse_grabbed
= 1;
183 dx
= int_clamp(s
->dx
, -128, 127);
184 dy
= int_clamp(s
->dy
, -128, 127);
185 dz
= int_clamp(s
->dz
, -128, 127);
192 if (s
->buttons_state
& MOUSE_EVENT_LBUTTON
)
194 if (s
->buttons_state
& MOUSE_EVENT_RBUTTON
)
196 if (s
->buttons_state
& MOUSE_EVENT_MBUTTON
)
210 static int usb_wacom_poll(USBWacomState
*s
, uint8_t *buf
, int len
)
214 if (!s
->mouse_grabbed
) {
215 s
->eh_entry
= qemu_add_mouse_event_handler(usb_wacom_event
, s
, 1,
216 "QEMU PenPartner tablet");
217 qemu_activate_mouse_event_handler(s
->eh_entry
);
218 s
->mouse_grabbed
= 1;
222 if (s
->buttons_state
& MOUSE_EVENT_LBUTTON
)
224 if (s
->buttons_state
& MOUSE_EVENT_RBUTTON
)
226 if (s
->buttons_state
& MOUSE_EVENT_MBUTTON
)
227 b
|= 0x20; /* eraser */
233 buf
[5] = 0x00 | (b
& 0xf0);
234 buf
[1] = s
->x
& 0xff;
236 buf
[3] = s
->y
& 0xff;
241 buf
[6] = (unsigned char) -127;
247 static void usb_wacom_handle_reset(USBDevice
*dev
)
249 USBWacomState
*s
= (USBWacomState
*) dev
;
256 s
->buttons_state
= 0;
257 s
->mode
= WACOM_MODE_HID
;
260 static void usb_wacom_handle_control(USBDevice
*dev
, USBPacket
*p
,
261 int request
, int value
, int index
, int length
, uint8_t *data
)
263 USBWacomState
*s
= (USBWacomState
*) dev
;
266 ret
= usb_desc_handle_control(dev
, p
, request
, value
, index
, length
, data
);
272 case WACOM_SET_REPORT
:
273 if (s
->mouse_grabbed
) {
274 qemu_remove_mouse_event_handler(s
->eh_entry
);
275 s
->mouse_grabbed
= 0;
279 case WACOM_GET_REPORT
:
282 p
->actual_length
= 2;
284 /* USB HID requests */
286 if (s
->mode
== WACOM_MODE_HID
)
287 p
->actual_length
= usb_mouse_poll(s
, data
, length
);
288 else if (s
->mode
== WACOM_MODE_WACOM
)
289 p
->actual_length
= usb_wacom_poll(s
, data
, length
);
293 p
->actual_length
= 1;
296 s
->idle
= (uint8_t) (value
>> 8);
299 p
->status
= USB_RET_STALL
;
304 static void usb_wacom_handle_data(USBDevice
*dev
, USBPacket
*p
)
306 USBWacomState
*s
= (USBWacomState
*) dev
;
307 uint8_t buf
[p
->iov
.size
];
312 if (p
->ep
->nr
== 1) {
313 if (!(s
->changed
|| s
->idle
)) {
314 p
->status
= USB_RET_NAK
;
318 if (s
->mode
== WACOM_MODE_HID
)
319 len
= usb_mouse_poll(s
, buf
, p
->iov
.size
);
320 else if (s
->mode
== WACOM_MODE_WACOM
)
321 len
= usb_wacom_poll(s
, buf
, p
->iov
.size
);
322 usb_packet_copy(p
, buf
, len
);
328 p
->status
= USB_RET_STALL
;
332 static void usb_wacom_unrealize(USBDevice
*dev
, Error
**errp
)
334 USBWacomState
*s
= (USBWacomState
*) dev
;
336 if (s
->mouse_grabbed
) {
337 qemu_remove_mouse_event_handler(s
->eh_entry
);
338 s
->mouse_grabbed
= 0;
342 static void usb_wacom_realize(USBDevice
*dev
, Error
**errp
)
344 USBWacomState
*s
= USB_WACOM(dev
);
345 usb_desc_create_serial(dev
);
347 s
->intr
= usb_ep_get(dev
, USB_TOKEN_IN
, 1);
351 static const VMStateDescription vmstate_usb_wacom
= {
356 static void usb_wacom_class_init(ObjectClass
*klass
, void *data
)
358 DeviceClass
*dc
= DEVICE_CLASS(klass
);
359 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
361 uc
->product_desc
= "QEMU PenPartner Tablet";
362 uc
->usb_desc
= &desc_wacom
;
363 uc
->realize
= usb_wacom_realize
;
364 uc
->handle_reset
= usb_wacom_handle_reset
;
365 uc
->handle_control
= usb_wacom_handle_control
;
366 uc
->handle_data
= usb_wacom_handle_data
;
367 uc
->unrealize
= usb_wacom_unrealize
;
368 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
369 dc
->desc
= "QEMU PenPartner Tablet";
370 dc
->vmsd
= &vmstate_usb_wacom
;
373 static const TypeInfo wacom_info
= {
374 .name
= TYPE_USB_WACOM
,
375 .parent
= TYPE_USB_DEVICE
,
376 .instance_size
= sizeof(USBWacomState
),
377 .class_init
= usb_wacom_class_init
,
380 static void usb_wacom_register_types(void)
382 type_register_static(&wacom_info
);
383 usb_legacy_register(TYPE_USB_WACOM
, "wacom-tablet", NULL
);
386 type_init(usb_wacom_register_types
)