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 "ui/console.h"
31 #include "hw/usb/desc.h"
33 /* Interface requests */
34 #define WACOM_GET_REPORT 0x2101
35 #define WACOM_SET_REPORT 0x2109
37 /* HID interface requests */
38 #define HID_GET_REPORT 0xa101
39 #define HID_GET_IDLE 0xa102
40 #define HID_GET_PROTOCOL 0xa103
41 #define HID_SET_IDLE 0x210a
42 #define HID_SET_PROTOCOL 0x210b
44 typedef struct USBWacomState
{
47 QEMUPutMouseEntry
*eh_entry
;
48 int dx
, dy
, dz
, buttons_state
;
65 static const USBDescStrings desc_strings
= {
66 [STR_MANUFACTURER
] = "QEMU",
67 [STR_PRODUCT
] = "Wacom PenPartner",
68 [STR_SERIALNUMBER
] = "1",
71 static const USBDescIface desc_iface_wacom
= {
72 .bInterfaceNumber
= 0,
74 .bInterfaceClass
= USB_CLASS_HID
,
75 .bInterfaceSubClass
= 0x01, /* boot */
76 .bInterfaceProtocol
= 0x02,
78 .descs
= (USBDescOther
[]) {
82 0x09, /* u8 bLength */
83 0x21, /* u8 bDescriptorType */
84 0x01, 0x10, /* u16 HID_class */
85 0x00, /* u8 country_code */
86 0x01, /* u8 num_descriptors */
87 0x22, /* u8 type: Report */
88 0x6e, 0, /* u16 len */
92 .eps
= (USBDescEndpoint
[]) {
94 .bEndpointAddress
= USB_DIR_IN
| 0x01,
95 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
102 static const USBDescDevice desc_device_wacom
= {
104 .bMaxPacketSize0
= 8,
105 .bNumConfigurations
= 1,
106 .confs
= (USBDescConfig
[]) {
109 .bConfigurationValue
= 1,
110 .bmAttributes
= USB_CFG_ATT_ONE
,
113 .ifs
= &desc_iface_wacom
,
118 static const USBDesc desc_wacom
= {
123 .iManufacturer
= STR_MANUFACTURER
,
124 .iProduct
= STR_PRODUCT
,
125 .iSerialNumber
= STR_SERIALNUMBER
,
127 .full
= &desc_device_wacom
,
131 static void usb_mouse_event(void *opaque
,
132 int dx1
, int dy1
, int dz1
, int buttons_state
)
134 USBWacomState
*s
= opaque
;
139 s
->buttons_state
= buttons_state
;
141 usb_wakeup(s
->intr
, 0);
144 static void usb_wacom_event(void *opaque
,
145 int x
, int y
, int dz
, int buttons_state
)
147 USBWacomState
*s
= opaque
;
149 /* scale to Penpartner resolution */
150 s
->x
= (x
* 5040 / 0x7FFF);
151 s
->y
= (y
* 3780 / 0x7FFF);
153 s
->buttons_state
= buttons_state
;
155 usb_wakeup(s
->intr
, 0);
158 static inline int int_clamp(int val
, int vmin
, int vmax
)
168 static int usb_mouse_poll(USBWacomState
*s
, uint8_t *buf
, int len
)
170 int dx
, dy
, dz
, b
, l
;
172 if (!s
->mouse_grabbed
) {
173 s
->eh_entry
= qemu_add_mouse_event_handler(usb_mouse_event
, s
, 0,
174 "QEMU PenPartner tablet");
175 qemu_activate_mouse_event_handler(s
->eh_entry
);
176 s
->mouse_grabbed
= 1;
179 dx
= int_clamp(s
->dx
, -128, 127);
180 dy
= int_clamp(s
->dy
, -128, 127);
181 dz
= int_clamp(s
->dz
, -128, 127);
188 if (s
->buttons_state
& MOUSE_EVENT_LBUTTON
)
190 if (s
->buttons_state
& MOUSE_EVENT_RBUTTON
)
192 if (s
->buttons_state
& MOUSE_EVENT_MBUTTON
)
206 static int usb_wacom_poll(USBWacomState
*s
, uint8_t *buf
, int len
)
210 if (!s
->mouse_grabbed
) {
211 s
->eh_entry
= qemu_add_mouse_event_handler(usb_wacom_event
, s
, 1,
212 "QEMU PenPartner tablet");
213 qemu_activate_mouse_event_handler(s
->eh_entry
);
214 s
->mouse_grabbed
= 1;
218 if (s
->buttons_state
& MOUSE_EVENT_LBUTTON
)
220 if (s
->buttons_state
& MOUSE_EVENT_RBUTTON
)
222 if (s
->buttons_state
& MOUSE_EVENT_MBUTTON
)
223 b
|= 0x20; /* eraser */
229 buf
[5] = 0x00 | (b
& 0xf0);
230 buf
[1] = s
->x
& 0xff;
232 buf
[3] = s
->y
& 0xff;
237 buf
[6] = (unsigned char) -127;
243 static void usb_wacom_handle_reset(USBDevice
*dev
)
245 USBWacomState
*s
= (USBWacomState
*) dev
;
252 s
->buttons_state
= 0;
253 s
->mode
= WACOM_MODE_HID
;
256 static void usb_wacom_handle_control(USBDevice
*dev
, USBPacket
*p
,
257 int request
, int value
, int index
, int length
, uint8_t *data
)
259 USBWacomState
*s
= (USBWacomState
*) dev
;
262 ret
= usb_desc_handle_control(dev
, p
, request
, value
, index
, length
, data
);
268 case WACOM_SET_REPORT
:
269 if (s
->mouse_grabbed
) {
270 qemu_remove_mouse_event_handler(s
->eh_entry
);
271 s
->mouse_grabbed
= 0;
275 case WACOM_GET_REPORT
:
278 p
->actual_length
= 2;
280 /* USB HID requests */
282 if (s
->mode
== WACOM_MODE_HID
)
283 p
->actual_length
= usb_mouse_poll(s
, data
, length
);
284 else if (s
->mode
== WACOM_MODE_WACOM
)
285 p
->actual_length
= usb_wacom_poll(s
, data
, length
);
289 p
->actual_length
= 1;
292 s
->idle
= (uint8_t) (value
>> 8);
295 p
->status
= USB_RET_STALL
;
300 static void usb_wacom_handle_data(USBDevice
*dev
, USBPacket
*p
)
302 USBWacomState
*s
= (USBWacomState
*) dev
;
303 uint8_t buf
[p
->iov
.size
];
308 if (p
->ep
->nr
== 1) {
309 if (!(s
->changed
|| s
->idle
)) {
310 p
->status
= USB_RET_NAK
;
314 if (s
->mode
== WACOM_MODE_HID
)
315 len
= usb_mouse_poll(s
, buf
, p
->iov
.size
);
316 else if (s
->mode
== WACOM_MODE_WACOM
)
317 len
= usb_wacom_poll(s
, buf
, p
->iov
.size
);
318 usb_packet_copy(p
, buf
, len
);
324 p
->status
= USB_RET_STALL
;
328 static void usb_wacom_handle_destroy(USBDevice
*dev
)
330 USBWacomState
*s
= (USBWacomState
*) dev
;
332 if (s
->mouse_grabbed
) {
333 qemu_remove_mouse_event_handler(s
->eh_entry
);
334 s
->mouse_grabbed
= 0;
338 static void usb_wacom_realize(USBDevice
*dev
, Error
**errp
)
340 USBWacomState
*s
= DO_UPCAST(USBWacomState
, dev
, dev
);
341 usb_desc_create_serial(dev
);
343 s
->intr
= usb_ep_get(dev
, USB_TOKEN_IN
, 1);
347 static const VMStateDescription vmstate_usb_wacom
= {
352 static void usb_wacom_class_init(ObjectClass
*klass
, void *data
)
354 DeviceClass
*dc
= DEVICE_CLASS(klass
);
355 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
357 uc
->product_desc
= "QEMU PenPartner Tablet";
358 uc
->usb_desc
= &desc_wacom
;
359 uc
->realize
= usb_wacom_realize
;
360 uc
->handle_reset
= usb_wacom_handle_reset
;
361 uc
->handle_control
= usb_wacom_handle_control
;
362 uc
->handle_data
= usb_wacom_handle_data
;
363 uc
->handle_destroy
= usb_wacom_handle_destroy
;
364 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
365 dc
->desc
= "QEMU PenPartner Tablet";
366 dc
->vmsd
= &vmstate_usb_wacom
;
369 static const TypeInfo wacom_info
= {
370 .name
= "usb-wacom-tablet",
371 .parent
= TYPE_USB_DEVICE
,
372 .instance_size
= sizeof(USBWacomState
),
373 .class_init
= usb_wacom_class_init
,
376 static void usb_wacom_register_types(void)
378 type_register_static(&wacom_info
);
379 usb_legacy_register("usb-wacom-tablet", "wacom-tablet", NULL
);
382 type_init(usb_wacom_register_types
)