1 /* 2 * Wacom PenPartner USB tablet emulation. 3 * 4 * Copyright (c) 2006 Openedhand Ltd. 5 * Author: Andrzej Zaborowski <balrog@zabor.org> 6 * 7 * Based on hw/usb-hid.c: 8 * Copyright (c) 2005 Fabrice Bellard 9 * 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: 16 * 17 * The above copyright notice and this permission notice shall be included in 18 * all copies or substantial portions of the Software. 19 * 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 26 * THE SOFTWARE. 27 */ 28 29 #include "qemu/osdep.h" 30 #include "hw/hw.h" 31 #include "ui/console.h" 32 #include "hw/usb.h" 33 #include "migration/vmstate.h" 34 #include "qemu/module.h" 35 #include "desc.h" 36 37 /* Interface requests */ 38 #define WACOM_GET_REPORT 0x2101 39 #define WACOM_SET_REPORT 0x2109 40 41 /* HID interface requests */ 42 #define HID_GET_REPORT 0xa101 43 #define HID_GET_IDLE 0xa102 44 #define HID_GET_PROTOCOL 0xa103 45 #define HID_SET_IDLE 0x210a 46 #define HID_SET_PROTOCOL 0x210b 47 48 typedef struct USBWacomState { 49 USBDevice dev; 50 USBEndpoint *intr; 51 QEMUPutMouseEntry *eh_entry; 52 int dx, dy, dz, buttons_state; 53 int x, y; 54 int mouse_grabbed; 55 enum { 56 WACOM_MODE_HID = 1, 57 WACOM_MODE_WACOM = 2, 58 } mode; 59 uint8_t idle; 60 int changed; 61 } USBWacomState; 62 63 #define TYPE_USB_WACOM "usb-wacom-tablet" 64 #define USB_WACOM(obj) OBJECT_CHECK(USBWacomState, (obj), TYPE_USB_WACOM) 65 66 enum { 67 STR_MANUFACTURER = 1, 68 STR_PRODUCT, 69 STR_SERIALNUMBER, 70 }; 71 72 static const USBDescStrings desc_strings = { 73 [STR_MANUFACTURER] = "QEMU", 74 [STR_PRODUCT] = "Wacom PenPartner", 75 [STR_SERIALNUMBER] = "1", 76 }; 77 78 static const USBDescIface desc_iface_wacom = { 79 .bInterfaceNumber = 0, 80 .bNumEndpoints = 1, 81 .bInterfaceClass = USB_CLASS_HID, 82 .bInterfaceSubClass = 0x01, /* boot */ 83 .bInterfaceProtocol = 0x02, 84 .ndesc = 1, 85 .descs = (USBDescOther[]) { 86 { 87 /* HID descriptor */ 88 .data = (uint8_t[]) { 89 0x09, /* u8 bLength */ 90 0x21, /* u8 bDescriptorType */ 91 0x01, 0x10, /* u16 HID_class */ 92 0x00, /* u8 country_code */ 93 0x01, /* u8 num_descriptors */ 94 0x22, /* u8 type: Report */ 95 0x6e, 0, /* u16 len */ 96 }, 97 }, 98 }, 99 .eps = (USBDescEndpoint[]) { 100 { 101 .bEndpointAddress = USB_DIR_IN | 0x01, 102 .bmAttributes = USB_ENDPOINT_XFER_INT, 103 .wMaxPacketSize = 8, 104 .bInterval = 0x0a, 105 }, 106 }, 107 }; 108 109 static const USBDescDevice desc_device_wacom = { 110 .bcdUSB = 0x0110, 111 .bMaxPacketSize0 = 8, 112 .bNumConfigurations = 1, 113 .confs = (USBDescConfig[]) { 114 { 115 .bNumInterfaces = 1, 116 .bConfigurationValue = 1, 117 .bmAttributes = USB_CFG_ATT_ONE, 118 .bMaxPower = 40, 119 .nif = 1, 120 .ifs = &desc_iface_wacom, 121 }, 122 }, 123 }; 124 125 static const USBDesc desc_wacom = { 126 .id = { 127 .idVendor = 0x056a, 128 .idProduct = 0x0000, 129 .bcdDevice = 0x4210, 130 .iManufacturer = STR_MANUFACTURER, 131 .iProduct = STR_PRODUCT, 132 .iSerialNumber = STR_SERIALNUMBER, 133 }, 134 .full = &desc_device_wacom, 135 .str = desc_strings, 136 }; 137 138 static void usb_mouse_event(void *opaque, 139 int dx1, int dy1, int dz1, int buttons_state) 140 { 141 USBWacomState *s = opaque; 142 143 s->dx += dx1; 144 s->dy += dy1; 145 s->dz += dz1; 146 s->buttons_state = buttons_state; 147 s->changed = 1; 148 usb_wakeup(s->intr, 0); 149 } 150 151 static void usb_wacom_event(void *opaque, 152 int x, int y, int dz, int buttons_state) 153 { 154 USBWacomState *s = opaque; 155 156 /* scale to Penpartner resolution */ 157 s->x = (x * 5040 / 0x7FFF); 158 s->y = (y * 3780 / 0x7FFF); 159 s->dz += dz; 160 s->buttons_state = buttons_state; 161 s->changed = 1; 162 usb_wakeup(s->intr, 0); 163 } 164 165 static inline int int_clamp(int val, int vmin, int vmax) 166 { 167 if (val < vmin) 168 return vmin; 169 else if (val > vmax) 170 return vmax; 171 else 172 return val; 173 } 174 175 static int usb_mouse_poll(USBWacomState *s, uint8_t *buf, int len) 176 { 177 int dx, dy, dz, b, l; 178 179 if (!s->mouse_grabbed) { 180 s->eh_entry = qemu_add_mouse_event_handler(usb_mouse_event, s, 0, 181 "QEMU PenPartner tablet"); 182 qemu_activate_mouse_event_handler(s->eh_entry); 183 s->mouse_grabbed = 1; 184 } 185 186 dx = int_clamp(s->dx, -128, 127); 187 dy = int_clamp(s->dy, -128, 127); 188 dz = int_clamp(s->dz, -128, 127); 189 190 s->dx -= dx; 191 s->dy -= dy; 192 s->dz -= dz; 193 194 b = 0; 195 if (s->buttons_state & MOUSE_EVENT_LBUTTON) 196 b |= 0x01; 197 if (s->buttons_state & MOUSE_EVENT_RBUTTON) 198 b |= 0x02; 199 if (s->buttons_state & MOUSE_EVENT_MBUTTON) 200 b |= 0x04; 201 202 buf[0] = b; 203 buf[1] = dx; 204 buf[2] = dy; 205 l = 3; 206 if (len >= 4) { 207 buf[3] = dz; 208 l = 4; 209 } 210 return l; 211 } 212 213 static int usb_wacom_poll(USBWacomState *s, uint8_t *buf, int len) 214 { 215 int b; 216 217 if (!s->mouse_grabbed) { 218 s->eh_entry = qemu_add_mouse_event_handler(usb_wacom_event, s, 1, 219 "QEMU PenPartner tablet"); 220 qemu_activate_mouse_event_handler(s->eh_entry); 221 s->mouse_grabbed = 1; 222 } 223 224 b = 0; 225 if (s->buttons_state & MOUSE_EVENT_LBUTTON) 226 b |= 0x01; 227 if (s->buttons_state & MOUSE_EVENT_RBUTTON) 228 b |= 0x40; 229 if (s->buttons_state & MOUSE_EVENT_MBUTTON) 230 b |= 0x20; /* eraser */ 231 232 if (len < 7) 233 return 0; 234 235 buf[0] = s->mode; 236 buf[5] = 0x00 | (b & 0xf0); 237 buf[1] = s->x & 0xff; 238 buf[2] = s->x >> 8; 239 buf[3] = s->y & 0xff; 240 buf[4] = s->y >> 8; 241 if (b & 0x3f) { 242 buf[6] = 0; 243 } else { 244 buf[6] = (unsigned char) -127; 245 } 246 247 return 7; 248 } 249 250 static void usb_wacom_handle_reset(USBDevice *dev) 251 { 252 USBWacomState *s = (USBWacomState *) dev; 253 254 s->dx = 0; 255 s->dy = 0; 256 s->dz = 0; 257 s->x = 0; 258 s->y = 0; 259 s->buttons_state = 0; 260 s->mode = WACOM_MODE_HID; 261 } 262 263 static void usb_wacom_handle_control(USBDevice *dev, USBPacket *p, 264 int request, int value, int index, int length, uint8_t *data) 265 { 266 USBWacomState *s = (USBWacomState *) dev; 267 int ret; 268 269 ret = usb_desc_handle_control(dev, p, request, value, index, length, data); 270 if (ret >= 0) { 271 return; 272 } 273 274 switch (request) { 275 case WACOM_SET_REPORT: 276 if (s->mouse_grabbed) { 277 qemu_remove_mouse_event_handler(s->eh_entry); 278 s->mouse_grabbed = 0; 279 } 280 s->mode = data[0]; 281 break; 282 case WACOM_GET_REPORT: 283 data[0] = 0; 284 data[1] = s->mode; 285 p->actual_length = 2; 286 break; 287 /* USB HID requests */ 288 case HID_GET_REPORT: 289 if (s->mode == WACOM_MODE_HID) 290 p->actual_length = usb_mouse_poll(s, data, length); 291 else if (s->mode == WACOM_MODE_WACOM) 292 p->actual_length = usb_wacom_poll(s, data, length); 293 break; 294 case HID_GET_IDLE: 295 data[0] = s->idle; 296 p->actual_length = 1; 297 break; 298 case HID_SET_IDLE: 299 s->idle = (uint8_t) (value >> 8); 300 break; 301 default: 302 p->status = USB_RET_STALL; 303 break; 304 } 305 } 306 307 static void usb_wacom_handle_data(USBDevice *dev, USBPacket *p) 308 { 309 USBWacomState *s = (USBWacomState *) dev; 310 uint8_t buf[p->iov.size]; 311 int len = 0; 312 313 switch (p->pid) { 314 case USB_TOKEN_IN: 315 if (p->ep->nr == 1) { 316 if (!(s->changed || s->idle)) { 317 p->status = USB_RET_NAK; 318 return; 319 } 320 s->changed = 0; 321 if (s->mode == WACOM_MODE_HID) 322 len = usb_mouse_poll(s, buf, p->iov.size); 323 else if (s->mode == WACOM_MODE_WACOM) 324 len = usb_wacom_poll(s, buf, p->iov.size); 325 usb_packet_copy(p, buf, len); 326 break; 327 } 328 /* Fall through. */ 329 case USB_TOKEN_OUT: 330 default: 331 p->status = USB_RET_STALL; 332 } 333 } 334 335 static void usb_wacom_unrealize(USBDevice *dev, Error **errp) 336 { 337 USBWacomState *s = (USBWacomState *) dev; 338 339 if (s->mouse_grabbed) { 340 qemu_remove_mouse_event_handler(s->eh_entry); 341 s->mouse_grabbed = 0; 342 } 343 } 344 345 static void usb_wacom_realize(USBDevice *dev, Error **errp) 346 { 347 USBWacomState *s = USB_WACOM(dev); 348 usb_desc_create_serial(dev); 349 usb_desc_init(dev); 350 s->intr = usb_ep_get(dev, USB_TOKEN_IN, 1); 351 s->changed = 1; 352 } 353 354 static const VMStateDescription vmstate_usb_wacom = { 355 .name = "usb-wacom", 356 .unmigratable = 1, 357 }; 358 359 static void usb_wacom_class_init(ObjectClass *klass, void *data) 360 { 361 DeviceClass *dc = DEVICE_CLASS(klass); 362 USBDeviceClass *uc = USB_DEVICE_CLASS(klass); 363 364 uc->product_desc = "QEMU PenPartner Tablet"; 365 uc->usb_desc = &desc_wacom; 366 uc->realize = usb_wacom_realize; 367 uc->handle_reset = usb_wacom_handle_reset; 368 uc->handle_control = usb_wacom_handle_control; 369 uc->handle_data = usb_wacom_handle_data; 370 uc->unrealize = usb_wacom_unrealize; 371 set_bit(DEVICE_CATEGORY_INPUT, dc->categories); 372 dc->desc = "QEMU PenPartner Tablet"; 373 dc->vmsd = &vmstate_usb_wacom; 374 } 375 376 static const TypeInfo wacom_info = { 377 .name = TYPE_USB_WACOM, 378 .parent = TYPE_USB_DEVICE, 379 .instance_size = sizeof(USBWacomState), 380 .class_init = usb_wacom_class_init, 381 }; 382 383 static void usb_wacom_register_types(void) 384 { 385 type_register_static(&wacom_info); 386 usb_legacy_register(TYPE_USB_WACOM, "wacom-tablet", NULL); 387 } 388 389 type_init(usb_wacom_register_types) 390