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