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