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
30 /* Interface requests */
31 #define WACOM_GET_REPORT 0x2101
32 #define WACOM_SET_REPORT 0x2109
34 /* HID interface requests */
35 #define HID_GET_REPORT 0xa101
36 #define HID_GET_IDLE 0xa102
37 #define HID_GET_PROTOCOL 0xa103
38 #define HID_SET_IDLE 0x210a
39 #define HID_SET_PROTOCOL 0x210b
41 typedef struct USBWacomState
{
43 QEMUPutMouseEntry
*eh_entry
;
44 int dx
, dy
, dz
, buttons_state
;
53 static const uint8_t qemu_wacom_dev_descriptor
[] = {
54 0x12, /* u8 bLength; */
55 0x01, /* u8 bDescriptorType; Device */
56 0x10, 0x10, /* u16 bcdUSB; v1.10 */
58 0x00, /* u8 bDeviceClass; */
59 0x00, /* u8 bDeviceSubClass; */
60 0x00, /* u8 bDeviceProtocol; [ low/full speeds only ] */
61 0x08, /* u8 bMaxPacketSize0; 8 Bytes */
63 0x6a, 0x05, /* u16 idVendor; */
64 0x00, 0x00, /* u16 idProduct; */
65 0x10, 0x42, /* u16 bcdDevice */
67 0x01, /* u8 iManufacturer; */
68 0x02, /* u8 iProduct; */
69 0x00, /* u8 iSerialNumber; */
70 0x01, /* u8 bNumConfigurations; */
73 static const uint8_t qemu_wacom_config_descriptor
[] = {
74 /* one configuration */
75 0x09, /* u8 bLength; */
76 0x02, /* u8 bDescriptorType; Configuration */
77 0x22, 0x00, /* u16 wTotalLength; */
78 0x01, /* u8 bNumInterfaces; (1) */
79 0x01, /* u8 bConfigurationValue; */
80 0x00, /* u8 iConfiguration; */
81 0x80, /* u8 bmAttributes;
86 40, /* u8 MaxPower; */
89 0x09, /* u8 if_bLength; */
90 0x04, /* u8 if_bDescriptorType; Interface */
91 0x00, /* u8 if_bInterfaceNumber; */
92 0x00, /* u8 if_bAlternateSetting; */
93 0x01, /* u8 if_bNumEndpoints; */
94 0x03, /* u8 if_bInterfaceClass; HID */
95 0x01, /* u8 if_bInterfaceSubClass; Boot */
96 0x02, /* u8 if_bInterfaceProtocol; [usb1.1 or single tt] */
97 0x00, /* u8 if_iInterface; */
100 0x09, /* u8 bLength; */
101 0x21, /* u8 bDescriptorType; */
102 0x01, 0x10, /* u16 HID_class */
103 0x00, /* u8 country_code */
104 0x01, /* u8 num_descriptors */
105 0x22, /* u8 type; Report */
106 0x6e, 0x00, /* u16 len */
108 /* one endpoint (status change endpoint) */
109 0x07, /* u8 ep_bLength; */
110 0x05, /* u8 ep_bDescriptorType; Endpoint */
111 0x81, /* u8 ep_bEndpointAddress; IN Endpoint 1 */
112 0x03, /* u8 ep_bmAttributes; Interrupt */
113 0x08, 0x00, /* u16 ep_wMaxPacketSize; */
114 0x0a, /* u8 ep_bInterval; */
117 static void usb_mouse_event(void *opaque
,
118 int dx1
, int dy1
, int dz1
, int buttons_state
)
120 USBWacomState
*s
= opaque
;
125 s
->buttons_state
= buttons_state
;
128 static void usb_wacom_event(void *opaque
,
129 int x
, int y
, int dz
, int buttons_state
)
131 USBWacomState
*s
= opaque
;
136 s
->buttons_state
= buttons_state
;
139 static inline int int_clamp(int val
, int vmin
, int vmax
)
149 static int usb_mouse_poll(USBWacomState
*s
, uint8_t *buf
, int len
)
151 int dx
, dy
, dz
, b
, l
;
153 if (!s
->mouse_grabbed
) {
154 s
->eh_entry
= qemu_add_mouse_event_handler(usb_mouse_event
, s
, 0,
155 "QEMU PenPartner tablet");
156 s
->mouse_grabbed
= 1;
159 dx
= int_clamp(s
->dx
, -128, 127);
160 dy
= int_clamp(s
->dy
, -128, 127);
161 dz
= int_clamp(s
->dz
, -128, 127);
168 if (s
->buttons_state
& MOUSE_EVENT_LBUTTON
)
170 if (s
->buttons_state
& MOUSE_EVENT_RBUTTON
)
172 if (s
->buttons_state
& MOUSE_EVENT_MBUTTON
)
186 static int usb_wacom_poll(USBWacomState
*s
, uint8_t *buf
, int len
)
190 if (!s
->mouse_grabbed
) {
191 s
->eh_entry
= qemu_add_mouse_event_handler(usb_wacom_event
, s
, 1,
192 "QEMU PenPartner tablet");
193 s
->mouse_grabbed
= 1;
197 if (s
->buttons_state
& MOUSE_EVENT_LBUTTON
)
199 if (s
->buttons_state
& MOUSE_EVENT_RBUTTON
)
201 if (s
->buttons_state
& MOUSE_EVENT_MBUTTON
)
210 buf
[1] = s
->x
& 0xff;
212 buf
[3] = s
->y
& 0xff;
220 buf
[6] = (unsigned char) -127;
226 static void usb_wacom_handle_reset(USBDevice
*dev
)
228 USBWacomState
*s
= (USBWacomState
*) dev
;
235 s
->buttons_state
= 0;
236 s
->mode
= WACOM_MODE_HID
;
239 static int usb_wacom_handle_control(USBDevice
*dev
, int request
, int value
,
240 int index
, int length
, uint8_t *data
)
242 USBWacomState
*s
= (USBWacomState
*) dev
;
246 case DeviceRequest
| USB_REQ_GET_STATUS
:
247 data
[0] = (1 << USB_DEVICE_SELF_POWERED
) |
248 (dev
->remote_wakeup
<< USB_DEVICE_REMOTE_WAKEUP
);
252 case DeviceOutRequest
| USB_REQ_CLEAR_FEATURE
:
253 if (value
== USB_DEVICE_REMOTE_WAKEUP
) {
254 dev
->remote_wakeup
= 0;
260 case DeviceOutRequest
| USB_REQ_SET_FEATURE
:
261 if (value
== USB_DEVICE_REMOTE_WAKEUP
) {
262 dev
->remote_wakeup
= 1;
268 case DeviceOutRequest
| USB_REQ_SET_ADDRESS
:
272 case DeviceRequest
| USB_REQ_GET_DESCRIPTOR
:
273 switch (value
>> 8) {
275 memcpy(data
, qemu_wacom_dev_descriptor
,
276 sizeof(qemu_wacom_dev_descriptor
));
277 ret
= sizeof(qemu_wacom_dev_descriptor
);
280 memcpy(data
, qemu_wacom_config_descriptor
,
281 sizeof(qemu_wacom_config_descriptor
));
282 ret
= sizeof(qemu_wacom_config_descriptor
);
285 switch (value
& 0xff) {
296 ret
= set_usb_string(data
, "1");
299 ret
= set_usb_string(data
, "Wacom PenPartner");
302 /* vendor description */
303 ret
= set_usb_string(data
, "QEMU " QEMU_VERSION
);
306 ret
= set_usb_string(data
, "Wacom Tablet");
309 ret
= set_usb_string(data
, "Endpoint1 Interrupt Pipe");
319 case DeviceRequest
| USB_REQ_GET_CONFIGURATION
:
323 case DeviceOutRequest
| USB_REQ_SET_CONFIGURATION
:
326 case DeviceRequest
| USB_REQ_GET_INTERFACE
:
330 case DeviceOutRequest
| USB_REQ_SET_INTERFACE
:
333 case WACOM_SET_REPORT
:
334 qemu_remove_mouse_event_handler(s
->eh_entry
);
335 s
->mouse_grabbed
= 0;
339 case WACOM_GET_REPORT
:
344 /* USB HID requests */
346 if (s
->mode
== WACOM_MODE_HID
)
347 ret
= usb_mouse_poll(s
, data
, length
);
348 else if (s
->mode
== WACOM_MODE_WACOM
)
349 ret
= usb_wacom_poll(s
, data
, length
);
362 static int usb_wacom_handle_data(USBDevice
*dev
, USBPacket
*p
)
364 USBWacomState
*s
= (USBWacomState
*) dev
;
370 if (s
->mode
== WACOM_MODE_HID
)
371 ret
= usb_mouse_poll(s
, p
->data
, p
->len
);
372 else if (s
->mode
== WACOM_MODE_WACOM
)
373 ret
= usb_wacom_poll(s
, p
->data
, p
->len
);
385 static void usb_wacom_handle_destroy(USBDevice
*dev
)
387 USBWacomState
*s
= (USBWacomState
*) dev
;
389 qemu_remove_mouse_event_handler(s
->eh_entry
);
393 USBDevice
*usb_wacom_init(void)
397 s
= qemu_mallocz(sizeof(USBWacomState
));
400 s
->dev
.speed
= USB_SPEED_FULL
;
401 s
->dev
.handle_packet
= usb_generic_handle_packet
;
403 s
->dev
.handle_reset
= usb_wacom_handle_reset
;
404 s
->dev
.handle_control
= usb_wacom_handle_control
;
405 s
->dev
.handle_data
= usb_wacom_handle_data
;
406 s
->dev
.handle_destroy
= usb_wacom_handle_destroy
;
408 pstrcpy(s
->dev
.devname
, sizeof(s
->dev
.devname
),
409 "QEMU PenPartner Tablet");
411 return (USBDevice
*) s
;