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 "hw/usb/hid.h"
33 #include "migration/vmstate.h"
34 #include "qemu/module.h"
36 #include "qom/object.h"
38 /* Interface requests */
39 #define WACOM_GET_REPORT 0x2101
40 #define WACOM_SET_REPORT 0x2109
42 struct USBWacomState
{
45 QEMUPutMouseEntry
*eh_entry
;
46 int dx
, dy
, dz
, buttons_state
;
57 #define TYPE_USB_WACOM "usb-wacom-tablet"
58 OBJECT_DECLARE_SIMPLE_TYPE(USBWacomState
, USB_WACOM
)
66 static const USBDescStrings desc_strings
= {
67 [STR_MANUFACTURER
] = "QEMU",
68 [STR_PRODUCT
] = "Wacom PenPartner",
69 [STR_SERIALNUMBER
] = "1",
72 static const USBDescIface desc_iface_wacom
= {
73 .bInterfaceNumber
= 0,
75 .bInterfaceClass
= USB_CLASS_HID
,
76 .bInterfaceSubClass
= 0x01, /* boot */
77 .bInterfaceProtocol
= 0x02,
79 .descs
= (USBDescOther
[]) {
83 0x09, /* u8 bLength */
84 USB_DT_HID
, /* u8 bDescriptorType */
85 0x01, 0x10, /* u16 HID_class */
86 0x00, /* u8 country_code */
87 0x01, /* u8 num_descriptors */
88 USB_DT_REPORT
, /* u8 type: Report */
89 0x6e, 0, /* u16 len */
93 .eps
= (USBDescEndpoint
[]) {
95 .bEndpointAddress
= USB_DIR_IN
| 0x01,
96 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
103 static const USBDescDevice desc_device_wacom
= {
105 .bMaxPacketSize0
= 8,
106 .bNumConfigurations
= 1,
107 .confs
= (USBDescConfig
[]) {
110 .bConfigurationValue
= 1,
111 .bmAttributes
= USB_CFG_ATT_ONE
,
114 .ifs
= &desc_iface_wacom
,
119 static const USBDesc desc_wacom
= {
124 .iManufacturer
= STR_MANUFACTURER
,
125 .iProduct
= STR_PRODUCT
,
126 .iSerialNumber
= STR_SERIALNUMBER
,
128 .full
= &desc_device_wacom
,
132 static void usb_mouse_event(void *opaque
,
133 int dx1
, int dy1
, int dz1
, int buttons_state
)
135 USBWacomState
*s
= opaque
;
140 s
->buttons_state
= buttons_state
;
142 usb_wakeup(s
->intr
, 0);
145 static void usb_wacom_event(void *opaque
,
146 int x
, int y
, int dz
, int buttons_state
)
148 USBWacomState
*s
= opaque
;
150 /* scale to Penpartner resolution */
151 s
->x
= (x
* 5040 / 0x7FFF);
152 s
->y
= (y
* 3780 / 0x7FFF);
154 s
->buttons_state
= buttons_state
;
156 usb_wakeup(s
->intr
, 0);
159 static inline int int_clamp(int val
, int vmin
, int vmax
)
169 static int usb_mouse_poll(USBWacomState
*s
, uint8_t *buf
, int len
)
171 int dx
, dy
, dz
, b
, l
;
173 if (!s
->mouse_grabbed
) {
174 s
->eh_entry
= qemu_add_mouse_event_handler(usb_mouse_event
, s
, 0,
175 "QEMU PenPartner tablet");
176 qemu_activate_mouse_event_handler(s
->eh_entry
);
177 s
->mouse_grabbed
= 1;
180 dx
= int_clamp(s
->dx
, -128, 127);
181 dy
= int_clamp(s
->dy
, -128, 127);
182 dz
= int_clamp(s
->dz
, -128, 127);
189 if (s
->buttons_state
& MOUSE_EVENT_LBUTTON
)
191 if (s
->buttons_state
& MOUSE_EVENT_RBUTTON
)
193 if (s
->buttons_state
& MOUSE_EVENT_MBUTTON
)
207 static int usb_wacom_poll(USBWacomState
*s
, uint8_t *buf
, int len
)
211 if (!s
->mouse_grabbed
) {
212 s
->eh_entry
= qemu_add_mouse_event_handler(usb_wacom_event
, s
, 1,
213 "QEMU PenPartner tablet");
214 qemu_activate_mouse_event_handler(s
->eh_entry
);
215 s
->mouse_grabbed
= 1;
219 if (s
->buttons_state
& MOUSE_EVENT_LBUTTON
)
221 if (s
->buttons_state
& MOUSE_EVENT_RBUTTON
)
223 if (s
->buttons_state
& MOUSE_EVENT_MBUTTON
)
224 b
|= 0x20; /* eraser */
230 buf
[5] = 0x00 | (b
& 0xf0);
231 buf
[1] = s
->x
& 0xff;
233 buf
[3] = s
->y
& 0xff;
238 buf
[6] = (unsigned char) -127;
244 static void usb_wacom_handle_reset(USBDevice
*dev
)
246 USBWacomState
*s
= (USBWacomState
*) dev
;
253 s
->buttons_state
= 0;
254 s
->mode
= WACOM_MODE_HID
;
257 static void usb_wacom_handle_control(USBDevice
*dev
, USBPacket
*p
,
258 int request
, int value
, int index
, int length
, uint8_t *data
)
260 USBWacomState
*s
= (USBWacomState
*) dev
;
263 ret
= usb_desc_handle_control(dev
, p
, request
, value
, index
, length
, data
);
269 case WACOM_SET_REPORT
:
270 if (s
->mouse_grabbed
) {
271 qemu_remove_mouse_event_handler(s
->eh_entry
);
272 s
->mouse_grabbed
= 0;
276 case WACOM_GET_REPORT
:
279 p
->actual_length
= 2;
281 /* USB HID requests */
283 if (s
->mode
== WACOM_MODE_HID
)
284 p
->actual_length
= usb_mouse_poll(s
, data
, length
);
285 else if (s
->mode
== WACOM_MODE_WACOM
)
286 p
->actual_length
= usb_wacom_poll(s
, data
, length
);
290 p
->actual_length
= 1;
293 s
->idle
= (uint8_t) (value
>> 8);
296 p
->status
= USB_RET_STALL
;
301 static void usb_wacom_handle_data(USBDevice
*dev
, USBPacket
*p
)
303 USBWacomState
*s
= (USBWacomState
*) dev
;
304 uint8_t buf
[p
->iov
.size
];
309 if (p
->ep
->nr
== 1) {
310 if (!(s
->changed
|| s
->idle
)) {
311 p
->status
= USB_RET_NAK
;
315 if (s
->mode
== WACOM_MODE_HID
)
316 len
= usb_mouse_poll(s
, buf
, p
->iov
.size
);
317 else if (s
->mode
== WACOM_MODE_WACOM
)
318 len
= usb_wacom_poll(s
, buf
, p
->iov
.size
);
319 usb_packet_copy(p
, buf
, len
);
325 p
->status
= USB_RET_STALL
;
329 static void usb_wacom_unrealize(USBDevice
*dev
)
331 USBWacomState
*s
= (USBWacomState
*) dev
;
333 if (s
->mouse_grabbed
) {
334 qemu_remove_mouse_event_handler(s
->eh_entry
);
335 s
->mouse_grabbed
= 0;
339 static void usb_wacom_realize(USBDevice
*dev
, Error
**errp
)
341 USBWacomState
*s
= USB_WACOM(dev
);
342 usb_desc_create_serial(dev
);
344 s
->intr
= usb_ep_get(dev
, USB_TOKEN_IN
, 1);
348 static const VMStateDescription vmstate_usb_wacom
= {
353 static void usb_wacom_class_init(ObjectClass
*klass
, void *data
)
355 DeviceClass
*dc
= DEVICE_CLASS(klass
);
356 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
358 uc
->product_desc
= "QEMU PenPartner Tablet";
359 uc
->usb_desc
= &desc_wacom
;
360 uc
->realize
= usb_wacom_realize
;
361 uc
->handle_reset
= usb_wacom_handle_reset
;
362 uc
->handle_control
= usb_wacom_handle_control
;
363 uc
->handle_data
= usb_wacom_handle_data
;
364 uc
->unrealize
= usb_wacom_unrealize
;
365 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
366 dc
->desc
= "QEMU PenPartner Tablet";
367 dc
->vmsd
= &vmstate_usb_wacom
;
370 static const TypeInfo wacom_info
= {
371 .name
= TYPE_USB_WACOM
,
372 .parent
= TYPE_USB_DEVICE
,
373 .instance_size
= sizeof(USBWacomState
),
374 .class_init
= usb_wacom_class_init
,
377 static void usb_wacom_register_types(void)
379 type_register_static(&wacom_info
);
380 usb_legacy_register(TYPE_USB_WACOM
, "wacom-tablet", NULL
);
383 type_init(usb_wacom_register_types
)