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
32 /* Interface requests */
33 #define WACOM_GET_REPORT 0x2101
34 #define WACOM_SET_REPORT 0x2109
36 /* HID interface requests */
37 #define HID_GET_REPORT 0xa101
38 #define HID_GET_IDLE 0xa102
39 #define HID_GET_PROTOCOL 0xa103
40 #define HID_SET_IDLE 0x210a
41 #define HID_SET_PROTOCOL 0x210b
43 typedef struct USBWacomState
{
45 QEMUPutMouseEntry
*eh_entry
;
46 int dx
, dy
, dz
, buttons_state
;
55 static const uint8_t qemu_wacom_dev_descriptor
[] = {
56 0x12, /* u8 bLength; */
57 0x01, /* u8 bDescriptorType; Device */
58 0x10, 0x10, /* u16 bcdUSB; v1.10 */
60 0x00, /* u8 bDeviceClass; */
61 0x00, /* u8 bDeviceSubClass; */
62 0x00, /* u8 bDeviceProtocol; [ low/full speeds only ] */
63 0x08, /* u8 bMaxPacketSize0; 8 Bytes */
65 0x6a, 0x05, /* u16 idVendor; */
66 0x00, 0x00, /* u16 idProduct; */
67 0x10, 0x42, /* u16 bcdDevice */
69 0x01, /* u8 iManufacturer; */
70 0x02, /* u8 iProduct; */
71 0x00, /* u8 iSerialNumber; */
72 0x01, /* u8 bNumConfigurations; */
75 static const uint8_t qemu_wacom_config_descriptor
[] = {
76 /* one configuration */
77 0x09, /* u8 bLength; */
78 0x02, /* u8 bDescriptorType; Configuration */
79 0x22, 0x00, /* u16 wTotalLength; */
80 0x01, /* u8 bNumInterfaces; (1) */
81 0x01, /* u8 bConfigurationValue; */
82 0x00, /* u8 iConfiguration; */
83 0x80, /* u8 bmAttributes;
88 40, /* u8 MaxPower; */
91 0x09, /* u8 if_bLength; */
92 0x04, /* u8 if_bDescriptorType; Interface */
93 0x00, /* u8 if_bInterfaceNumber; */
94 0x00, /* u8 if_bAlternateSetting; */
95 0x01, /* u8 if_bNumEndpoints; */
96 0x03, /* u8 if_bInterfaceClass; HID */
97 0x01, /* u8 if_bInterfaceSubClass; Boot */
98 0x02, /* u8 if_bInterfaceProtocol; [usb1.1 or single tt] */
99 0x00, /* u8 if_iInterface; */
102 0x09, /* u8 bLength; */
103 0x21, /* u8 bDescriptorType; */
104 0x01, 0x10, /* u16 HID_class */
105 0x00, /* u8 country_code */
106 0x01, /* u8 num_descriptors */
107 0x22, /* u8 type; Report */
108 0x6e, 0x00, /* u16 len */
110 /* one endpoint (status change endpoint) */
111 0x07, /* u8 ep_bLength; */
112 0x05, /* u8 ep_bDescriptorType; Endpoint */
113 0x81, /* u8 ep_bEndpointAddress; IN Endpoint 1 */
114 0x03, /* u8 ep_bmAttributes; Interrupt */
115 0x08, 0x00, /* u16 ep_wMaxPacketSize; */
116 0x0a, /* u8 ep_bInterval; */
119 static void usb_mouse_event(void *opaque
,
120 int dx1
, int dy1
, int dz1
, int buttons_state
)
122 USBWacomState
*s
= opaque
;
127 s
->buttons_state
= buttons_state
;
130 static void usb_wacom_event(void *opaque
,
131 int x
, int y
, int dz
, int buttons_state
)
133 USBWacomState
*s
= opaque
;
138 s
->buttons_state
= buttons_state
;
141 static inline int int_clamp(int val
, int vmin
, int vmax
)
151 static int usb_mouse_poll(USBWacomState
*s
, uint8_t *buf
, int len
)
153 int dx
, dy
, dz
, b
, l
;
155 if (!s
->mouse_grabbed
) {
156 s
->eh_entry
= qemu_add_mouse_event_handler(usb_mouse_event
, s
, 0,
157 "QEMU PenPartner tablet");
158 s
->mouse_grabbed
= 1;
161 dx
= int_clamp(s
->dx
, -128, 127);
162 dy
= int_clamp(s
->dy
, -128, 127);
163 dz
= int_clamp(s
->dz
, -128, 127);
170 if (s
->buttons_state
& MOUSE_EVENT_LBUTTON
)
172 if (s
->buttons_state
& MOUSE_EVENT_RBUTTON
)
174 if (s
->buttons_state
& MOUSE_EVENT_MBUTTON
)
188 static int usb_wacom_poll(USBWacomState
*s
, uint8_t *buf
, int len
)
192 if (!s
->mouse_grabbed
) {
193 s
->eh_entry
= qemu_add_mouse_event_handler(usb_wacom_event
, s
, 1,
194 "QEMU PenPartner tablet");
195 s
->mouse_grabbed
= 1;
199 if (s
->buttons_state
& MOUSE_EVENT_LBUTTON
)
201 if (s
->buttons_state
& MOUSE_EVENT_RBUTTON
)
203 if (s
->buttons_state
& MOUSE_EVENT_MBUTTON
)
212 buf
[1] = s
->x
& 0xff;
214 buf
[3] = s
->y
& 0xff;
222 buf
[6] = (unsigned char) -127;
228 static void usb_wacom_handle_reset(USBDevice
*dev
)
230 USBWacomState
*s
= (USBWacomState
*) dev
;
237 s
->buttons_state
= 0;
238 s
->mode
= WACOM_MODE_HID
;
241 static int usb_wacom_handle_control(USBDevice
*dev
, int request
, int value
,
242 int index
, int length
, uint8_t *data
)
244 USBWacomState
*s
= (USBWacomState
*) dev
;
248 case DeviceRequest
| USB_REQ_GET_STATUS
:
249 data
[0] = (1 << USB_DEVICE_SELF_POWERED
) |
250 (dev
->remote_wakeup
<< USB_DEVICE_REMOTE_WAKEUP
);
254 case DeviceOutRequest
| USB_REQ_CLEAR_FEATURE
:
255 if (value
== USB_DEVICE_REMOTE_WAKEUP
) {
256 dev
->remote_wakeup
= 0;
262 case DeviceOutRequest
| USB_REQ_SET_FEATURE
:
263 if (value
== USB_DEVICE_REMOTE_WAKEUP
) {
264 dev
->remote_wakeup
= 1;
270 case DeviceOutRequest
| USB_REQ_SET_ADDRESS
:
274 case DeviceRequest
| USB_REQ_GET_DESCRIPTOR
:
275 switch (value
>> 8) {
277 memcpy(data
, qemu_wacom_dev_descriptor
,
278 sizeof(qemu_wacom_dev_descriptor
));
279 ret
= sizeof(qemu_wacom_dev_descriptor
);
282 memcpy(data
, qemu_wacom_config_descriptor
,
283 sizeof(qemu_wacom_config_descriptor
));
284 ret
= sizeof(qemu_wacom_config_descriptor
);
287 switch (value
& 0xff) {
298 ret
= set_usb_string(data
, "1");
301 ret
= set_usb_string(data
, "Wacom PenPartner");
304 /* vendor description */
305 ret
= set_usb_string(data
, "QEMU " QEMU_VERSION
);
308 ret
= set_usb_string(data
, "Wacom Tablet");
311 ret
= set_usb_string(data
, "Endpoint1 Interrupt Pipe");
321 case DeviceRequest
| USB_REQ_GET_CONFIGURATION
:
325 case DeviceOutRequest
| USB_REQ_SET_CONFIGURATION
:
328 case DeviceRequest
| USB_REQ_GET_INTERFACE
:
332 case DeviceOutRequest
| USB_REQ_SET_INTERFACE
:
335 case WACOM_SET_REPORT
:
336 qemu_remove_mouse_event_handler(s
->eh_entry
);
337 s
->mouse_grabbed
= 0;
341 case WACOM_GET_REPORT
:
346 /* USB HID requests */
348 if (s
->mode
== WACOM_MODE_HID
)
349 ret
= usb_mouse_poll(s
, data
, length
);
350 else if (s
->mode
== WACOM_MODE_WACOM
)
351 ret
= usb_wacom_poll(s
, data
, length
);
364 static int usb_wacom_handle_data(USBDevice
*dev
, USBPacket
*p
)
366 USBWacomState
*s
= (USBWacomState
*) dev
;
372 if (s
->mode
== WACOM_MODE_HID
)
373 ret
= usb_mouse_poll(s
, p
->data
, p
->len
);
374 else if (s
->mode
== WACOM_MODE_WACOM
)
375 ret
= usb_wacom_poll(s
, p
->data
, p
->len
);
387 static void usb_wacom_handle_destroy(USBDevice
*dev
)
389 USBWacomState
*s
= (USBWacomState
*) dev
;
391 qemu_remove_mouse_event_handler(s
->eh_entry
);
395 USBDevice
*usb_wacom_init(void)
399 s
= qemu_mallocz(sizeof(USBWacomState
));
402 s
->dev
.speed
= USB_SPEED_FULL
;
403 s
->dev
.handle_packet
= usb_generic_handle_packet
;
405 s
->dev
.handle_reset
= usb_wacom_handle_reset
;
406 s
->dev
.handle_control
= usb_wacom_handle_control
;
407 s
->dev
.handle_data
= usb_wacom_handle_data
;
408 s
->dev
.handle_destroy
= usb_wacom_handle_destroy
;
410 pstrcpy(s
->dev
.devname
, sizeof(s
->dev
.devname
),
411 "QEMU PenPartner Tablet");
413 return (USBDevice
*) s
;