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
29 #include "qemu/osdep.h"
30 #include "ui/console.h"
32 #include "migration/vmstate.h"
33 #include "qemu/module.h"
36 /* Interface requests */
37 #define WACOM_GET_REPORT 0x2101
38 #define WACOM_SET_REPORT 0x2109
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
47 typedef struct USBWacomState
{
50 QEMUPutMouseEntry
*eh_entry
;
51 int dx
, dy
, dz
, buttons_state
;
62 #define TYPE_USB_WACOM "usb-wacom-tablet"
63 #define USB_WACOM(obj) OBJECT_CHECK(USBWacomState, (obj), TYPE_USB_WACOM)
71 static const USBDescStrings desc_strings
= {
72 [STR_MANUFACTURER
] = "QEMU",
73 [STR_PRODUCT
] = "Wacom PenPartner",
74 [STR_SERIALNUMBER
] = "1",
77 static const USBDescIface desc_iface_wacom
= {
78 .bInterfaceNumber
= 0,
80 .bInterfaceClass
= USB_CLASS_HID
,
81 .bInterfaceSubClass
= 0x01, /* boot */
82 .bInterfaceProtocol
= 0x02,
84 .descs
= (USBDescOther
[]) {
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 */
98 .eps
= (USBDescEndpoint
[]) {
100 .bEndpointAddress
= USB_DIR_IN
| 0x01,
101 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
108 static const USBDescDevice desc_device_wacom
= {
110 .bMaxPacketSize0
= 8,
111 .bNumConfigurations
= 1,
112 .confs
= (USBDescConfig
[]) {
115 .bConfigurationValue
= 1,
116 .bmAttributes
= USB_CFG_ATT_ONE
,
119 .ifs
= &desc_iface_wacom
,
124 static const USBDesc desc_wacom
= {
129 .iManufacturer
= STR_MANUFACTURER
,
130 .iProduct
= STR_PRODUCT
,
131 .iSerialNumber
= STR_SERIALNUMBER
,
133 .full
= &desc_device_wacom
,
137 static void usb_mouse_event(void *opaque
,
138 int dx1
, int dy1
, int dz1
, int buttons_state
)
140 USBWacomState
*s
= opaque
;
145 s
->buttons_state
= buttons_state
;
147 usb_wakeup(s
->intr
, 0);
150 static void usb_wacom_event(void *opaque
,
151 int x
, int y
, int dz
, int buttons_state
)
153 USBWacomState
*s
= opaque
;
155 /* scale to Penpartner resolution */
156 s
->x
= (x
* 5040 / 0x7FFF);
157 s
->y
= (y
* 3780 / 0x7FFF);
159 s
->buttons_state
= buttons_state
;
161 usb_wakeup(s
->intr
, 0);
164 static inline int int_clamp(int val
, int vmin
, int vmax
)
174 static int usb_mouse_poll(USBWacomState
*s
, uint8_t *buf
, int len
)
176 int dx
, dy
, dz
, b
, l
;
178 if (!s
->mouse_grabbed
) {
179 s
->eh_entry
= qemu_add_mouse_event_handler(usb_mouse_event
, s
, 0,
180 "QEMU PenPartner tablet");
181 qemu_activate_mouse_event_handler(s
->eh_entry
);
182 s
->mouse_grabbed
= 1;
185 dx
= int_clamp(s
->dx
, -128, 127);
186 dy
= int_clamp(s
->dy
, -128, 127);
187 dz
= int_clamp(s
->dz
, -128, 127);
194 if (s
->buttons_state
& MOUSE_EVENT_LBUTTON
)
196 if (s
->buttons_state
& MOUSE_EVENT_RBUTTON
)
198 if (s
->buttons_state
& MOUSE_EVENT_MBUTTON
)
212 static int usb_wacom_poll(USBWacomState
*s
, uint8_t *buf
, int len
)
216 if (!s
->mouse_grabbed
) {
217 s
->eh_entry
= qemu_add_mouse_event_handler(usb_wacom_event
, s
, 1,
218 "QEMU PenPartner tablet");
219 qemu_activate_mouse_event_handler(s
->eh_entry
);
220 s
->mouse_grabbed
= 1;
224 if (s
->buttons_state
& MOUSE_EVENT_LBUTTON
)
226 if (s
->buttons_state
& MOUSE_EVENT_RBUTTON
)
228 if (s
->buttons_state
& MOUSE_EVENT_MBUTTON
)
229 b
|= 0x20; /* eraser */
235 buf
[5] = 0x00 | (b
& 0xf0);
236 buf
[1] = s
->x
& 0xff;
238 buf
[3] = s
->y
& 0xff;
243 buf
[6] = (unsigned char) -127;
249 static void usb_wacom_handle_reset(USBDevice
*dev
)
251 USBWacomState
*s
= (USBWacomState
*) dev
;
258 s
->buttons_state
= 0;
259 s
->mode
= WACOM_MODE_HID
;
262 static void usb_wacom_handle_control(USBDevice
*dev
, USBPacket
*p
,
263 int request
, int value
, int index
, int length
, uint8_t *data
)
265 USBWacomState
*s
= (USBWacomState
*) dev
;
268 ret
= usb_desc_handle_control(dev
, p
, request
, value
, index
, length
, data
);
274 case WACOM_SET_REPORT
:
275 if (s
->mouse_grabbed
) {
276 qemu_remove_mouse_event_handler(s
->eh_entry
);
277 s
->mouse_grabbed
= 0;
281 case WACOM_GET_REPORT
:
284 p
->actual_length
= 2;
286 /* USB HID requests */
288 if (s
->mode
== WACOM_MODE_HID
)
289 p
->actual_length
= usb_mouse_poll(s
, data
, length
);
290 else if (s
->mode
== WACOM_MODE_WACOM
)
291 p
->actual_length
= usb_wacom_poll(s
, data
, length
);
295 p
->actual_length
= 1;
298 s
->idle
= (uint8_t) (value
>> 8);
301 p
->status
= USB_RET_STALL
;
306 static void usb_wacom_handle_data(USBDevice
*dev
, USBPacket
*p
)
308 USBWacomState
*s
= (USBWacomState
*) dev
;
309 uint8_t buf
[p
->iov
.size
];
314 if (p
->ep
->nr
== 1) {
315 if (!(s
->changed
|| s
->idle
)) {
316 p
->status
= USB_RET_NAK
;
320 if (s
->mode
== WACOM_MODE_HID
)
321 len
= usb_mouse_poll(s
, buf
, p
->iov
.size
);
322 else if (s
->mode
== WACOM_MODE_WACOM
)
323 len
= usb_wacom_poll(s
, buf
, p
->iov
.size
);
324 usb_packet_copy(p
, buf
, len
);
330 p
->status
= USB_RET_STALL
;
334 static void usb_wacom_unrealize(USBDevice
*dev
, Error
**errp
)
336 USBWacomState
*s
= (USBWacomState
*) dev
;
338 if (s
->mouse_grabbed
) {
339 qemu_remove_mouse_event_handler(s
->eh_entry
);
340 s
->mouse_grabbed
= 0;
344 static void usb_wacom_realize(USBDevice
*dev
, Error
**errp
)
346 USBWacomState
*s
= USB_WACOM(dev
);
347 usb_desc_create_serial(dev
);
349 s
->intr
= usb_ep_get(dev
, USB_TOKEN_IN
, 1);
353 static const VMStateDescription vmstate_usb_wacom
= {
358 static void usb_wacom_class_init(ObjectClass
*klass
, void *data
)
360 DeviceClass
*dc
= DEVICE_CLASS(klass
);
361 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
363 uc
->product_desc
= "QEMU PenPartner Tablet";
364 uc
->usb_desc
= &desc_wacom
;
365 uc
->realize
= usb_wacom_realize
;
366 uc
->handle_reset
= usb_wacom_handle_reset
;
367 uc
->handle_control
= usb_wacom_handle_control
;
368 uc
->handle_data
= usb_wacom_handle_data
;
369 uc
->unrealize
= usb_wacom_unrealize
;
370 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
371 dc
->desc
= "QEMU PenPartner Tablet";
372 dc
->vmsd
= &vmstate_usb_wacom
;
375 static const TypeInfo wacom_info
= {
376 .name
= TYPE_USB_WACOM
,
377 .parent
= TYPE_USB_DEVICE
,
378 .instance_size
= sizeof(USBWacomState
),
379 .class_init
= usb_wacom_class_init
,
382 static void usb_wacom_register_types(void)
384 type_register_static(&wacom_info
);
385 usb_legacy_register(TYPE_USB_WACOM
, "wacom-tablet", NULL
);
388 type_init(usb_wacom_register_types
)