2 * Wacom PenPartner USB tablet emulation.
4 * Copyright (c) 2006 Openedhand Ltd.
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
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 {
46 QEMUPutMouseEntry *eh_entry;
47 int dx, dy, dz, buttons_state;
64 static const USBDescStrings desc_strings = {
65 [STR_MANUFACTURER] = "QEMU " QEMU_VERSION,
66 [STR_PRODUCT] = "Wacom PenPartner",
67 [STR_SERIALNUMBER] = "1",
70 static const USBDescIface desc_iface_wacom = {
71 .bInterfaceNumber = 0,
73 .bInterfaceClass = USB_CLASS_HID,
74 .bInterfaceSubClass = 0x01, /* boot */
75 .bInterfaceProtocol = 0x02,
77 .descs = (USBDescOther[]) {
81 0x09, /* u8 bLength */
82 0x21, /* u8 bDescriptorType */
83 0x01, 0x10, /* u16 HID_class */
84 0x00, /* u8 country_code */
85 0x01, /* u8 num_descriptors */
86 0x22, /* u8 type: Report */
87 0x6e, 0, /* u16 len */
91 .eps = (USBDescEndpoint[]) {
93 .bEndpointAddress = USB_DIR_IN | 0x01,
94 .bmAttributes = USB_ENDPOINT_XFER_INT,
101 static const USBDescDevice desc_device_wacom = {
103 .bMaxPacketSize0 = 8,
104 .bNumConfigurations = 1,
105 .confs = (USBDescConfig[]) {
108 .bConfigurationValue = 1,
109 .bmAttributes = 0x80,
112 .ifs = &desc_iface_wacom,
117 static const USBDesc desc_wacom = {
122 .iManufacturer = STR_MANUFACTURER,
123 .iProduct = STR_PRODUCT,
124 .iSerialNumber = STR_SERIALNUMBER,
126 .full = &desc_device_wacom,
130 static void usb_mouse_event(void *opaque,
131 int dx1, int dy1, int dz1, int buttons_state)
133 USBWacomState *s = opaque;
138 s->buttons_state = buttons_state;
142 static void usb_wacom_event(void *opaque,
143 int x, int y, int dz, int buttons_state)
145 USBWacomState *s = opaque;
147 /* scale to Penpartner resolution */
148 s->x = (x * 5040 / 0x7FFF);
149 s->y = (y * 3780 / 0x7FFF);
151 s->buttons_state = buttons_state;
155 static inline int int_clamp(int val, int vmin, int vmax)
165 static int usb_mouse_poll(USBWacomState *s, uint8_t *buf, int len)
167 int dx, dy, dz, b, l;
169 if (!s->mouse_grabbed) {
170 s->eh_entry = qemu_add_mouse_event_handler(usb_mouse_event, s, 0,
171 "QEMU PenPartner tablet");
172 qemu_activate_mouse_event_handler(s->eh_entry);
173 s->mouse_grabbed = 1;
176 dx = int_clamp(s->dx, -128, 127);
177 dy = int_clamp(s->dy, -128, 127);
178 dz = int_clamp(s->dz, -128, 127);
185 if (s->buttons_state & MOUSE_EVENT_LBUTTON)
187 if (s->buttons_state & MOUSE_EVENT_RBUTTON)
189 if (s->buttons_state & MOUSE_EVENT_MBUTTON)
203 static int usb_wacom_poll(USBWacomState *s, uint8_t *buf, int len)
207 if (!s->mouse_grabbed) {
208 s->eh_entry = qemu_add_mouse_event_handler(usb_wacom_event, s, 1,
209 "QEMU PenPartner tablet");
210 qemu_activate_mouse_event_handler(s->eh_entry);
211 s->mouse_grabbed = 1;
215 if (s->buttons_state & MOUSE_EVENT_LBUTTON)
217 if (s->buttons_state & MOUSE_EVENT_RBUTTON)
219 if (s->buttons_state & MOUSE_EVENT_MBUTTON)
220 b |= 0x20; /* eraser */
226 buf[5] = 0x00 | (b & 0xf0);
227 buf[1] = s->x & 0xff;
229 buf[3] = s->y & 0xff;
234 buf[6] = (unsigned char) -127;
240 static void usb_wacom_handle_reset(USBDevice *dev)
242 USBWacomState *s = (USBWacomState *) dev;
249 s->buttons_state = 0;
250 s->mode = WACOM_MODE_HID;
253 static int usb_wacom_handle_control(USBDevice *dev, USBPacket *p,
254 int request, int value, int index, int length, uint8_t *data)
256 USBWacomState *s = (USBWacomState *) dev;
259 ret = usb_desc_handle_control(dev, p, request, value, index, length, data);
266 case DeviceRequest | USB_REQ_GET_INTERFACE:
270 case DeviceOutRequest | USB_REQ_SET_INTERFACE:
273 case WACOM_SET_REPORT:
274 if (s->mouse_grabbed) {
275 qemu_remove_mouse_event_handler(s->eh_entry);
276 s->mouse_grabbed = 0;
281 case WACOM_GET_REPORT:
286 /* USB HID requests */
288 if (s->mode == WACOM_MODE_HID)
289 ret = usb_mouse_poll(s, data, length);
290 else if (s->mode == WACOM_MODE_WACOM)
291 ret = usb_wacom_poll(s, data, length);
298 s->idle = (uint8_t) (value >> 8);
308 static int usb_wacom_handle_data(USBDevice *dev, USBPacket *p)
310 USBWacomState *s = (USBWacomState *) dev;
311 uint8_t buf[p->iov.size];
317 if (!(s->changed || s->idle))
320 if (s->mode == WACOM_MODE_HID)
321 ret = usb_mouse_poll(s, buf, p->iov.size);
322 else if (s->mode == WACOM_MODE_WACOM)
323 ret = usb_wacom_poll(s, buf, p->iov.size);
324 usb_packet_copy(p, buf, ret);
336 static void usb_wacom_handle_destroy(USBDevice *dev)
338 USBWacomState *s = (USBWacomState *) dev;
340 if (s->mouse_grabbed) {
341 qemu_remove_mouse_event_handler(s->eh_entry);
342 s->mouse_grabbed = 0;
346 static int usb_wacom_initfn(USBDevice *dev)
348 USBWacomState *s = DO_UPCAST(USBWacomState, dev, dev);
354 static const VMStateDescription vmstate_usb_wacom = {
359 static struct USBDeviceInfo wacom_info = {
360 .product_desc = "QEMU PenPartner Tablet",
361 .qdev.name = "usb-wacom-tablet",
362 .qdev.desc = "QEMU PenPartner Tablet",
363 .usbdevice_name = "wacom-tablet",
364 .usb_desc = &desc_wacom,
365 .qdev.size = sizeof(USBWacomState),
366 .qdev.vmsd = &vmstate_usb_wacom,
367 .init = usb_wacom_initfn,
368 .handle_packet = usb_generic_handle_packet,
369 .handle_reset = usb_wacom_handle_reset,
370 .handle_control = usb_wacom_handle_control,
371 .handle_data = usb_wacom_handle_data,
372 .handle_destroy = usb_wacom_handle_destroy,
375 static void usb_wacom_register_devices(void)
377 usb_qdev_register(&wacom_info);
379 device_init(usb_wacom_register_devices)