Statistics
| Branch: | Revision:

root / hw / usb-hid.c @ 0d878eec

History | View | Annotate | Download (32 kB)

1
/*
2
 * QEMU USB HID devices
3
 *
4
 * Copyright (c) 2005 Fabrice Bellard
5
 * Copyright (c) 2007 OpenMoko, Inc.  (andrew@openedhand.com)
6
 *
7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8
 * of this software and associated documentation files (the "Software"), to deal
9
 * in the Software without restriction, including without limitation the rights
10
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
 * copies of the Software, and to permit persons to whom the Software is
12
 * furnished to do so, subject to the following conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be included in
15
 * all copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
 * THE SOFTWARE.
24
 */
25
#include "hw.h"
26
#include "console.h"
27
#include "usb.h"
28
#include "usb-desc.h"
29
#include "qemu-timer.h"
30

    
31
/* HID interface requests */
32
#define GET_REPORT   0xa101
33
#define GET_IDLE     0xa102
34
#define GET_PROTOCOL 0xa103
35
#define SET_REPORT   0x2109
36
#define SET_IDLE     0x210a
37
#define SET_PROTOCOL 0x210b
38

    
39
/* HID descriptor types */
40
#define USB_DT_HID    0x21
41
#define USB_DT_REPORT 0x22
42
#define USB_DT_PHY    0x23
43

    
44
#define HID_MOUSE     1
45
#define HID_TABLET    2
46
#define HID_KEYBOARD  3
47

    
48
typedef struct HIDPointerEvent {
49
    int32_t xdx, ydy; /* relative iff it's a mouse, otherwise absolute */
50
    int32_t dz, buttons_state;
51
} HIDPointerEvent;
52

    
53
#define QUEUE_LENGTH    16 /* should be enough for a triple-click */
54
#define QUEUE_MASK      (QUEUE_LENGTH-1u)
55
#define QUEUE_INCR(v)   ((v)++, (v) &= QUEUE_MASK)
56

    
57
typedef struct HIDMouseState {
58
    HIDPointerEvent queue[QUEUE_LENGTH];
59
    int mouse_grabbed;
60
    QEMUPutMouseEntry *eh_entry;
61
} HIDMouseState;
62

    
63
typedef struct HIDKeyboardState {
64
    uint32_t keycodes[QUEUE_LENGTH];
65
    uint16_t modifiers;
66
    uint8_t leds;
67
    uint8_t key[16];
68
    int32_t keys;
69
} HIDKeyboardState;
70

    
71
typedef struct HIDState {
72
    union {
73
        HIDMouseState ptr;
74
        HIDKeyboardState kbd;
75
    };
76
    uint32_t head; /* index into circular queue */
77
    uint32_t n;
78
    int kind;
79
} HIDState;
80

    
81
typedef struct USBHIDState {
82
    USBDevice dev;
83
    HIDState hid;
84
    int32_t protocol;
85
    uint8_t idle;
86
    int64_t next_idle_clock;
87
    int changed;
88
    void *datain_opaque;
89
    void (*datain)(void *);
90
} USBHIDState;
91

    
92
enum {
93
    STR_MANUFACTURER = 1,
94
    STR_PRODUCT_MOUSE,
95
    STR_PRODUCT_TABLET,
96
    STR_PRODUCT_KEYBOARD,
97
    STR_SERIALNUMBER,
98
    STR_CONFIG_MOUSE,
99
    STR_CONFIG_TABLET,
100
    STR_CONFIG_KEYBOARD,
101
};
102

    
103
static const USBDescStrings desc_strings = {
104
    [STR_MANUFACTURER]     = "QEMU " QEMU_VERSION,
105
    [STR_PRODUCT_MOUSE]    = "QEMU USB Mouse",
106
    [STR_PRODUCT_TABLET]   = "QEMU USB Tablet",
107
    [STR_PRODUCT_KEYBOARD] = "QEMU USB Keyboard",
108
    [STR_SERIALNUMBER]     = "42", /* == remote wakeup works */
109
    [STR_CONFIG_MOUSE]     = "HID Mouse",
110
    [STR_CONFIG_TABLET]    = "HID Tablet",
111
    [STR_CONFIG_KEYBOARD]  = "HID Keyboard",
112
};
113

    
114
static const USBDescIface desc_iface_mouse = {
115
    .bInterfaceNumber              = 0,
116
    .bNumEndpoints                 = 1,
117
    .bInterfaceClass               = USB_CLASS_HID,
118
    .bInterfaceSubClass            = 0x01, /* boot */
119
    .bInterfaceProtocol            = 0x02,
120
    .ndesc                         = 1,
121
    .descs = (USBDescOther[]) {
122
        {
123
            /* HID descriptor */
124
            .data = (uint8_t[]) {
125
                0x09,          /*  u8  bLength */
126
                USB_DT_HID,    /*  u8  bDescriptorType */
127
                0x01, 0x00,    /*  u16 HID_class */
128
                0x00,          /*  u8  country_code */
129
                0x01,          /*  u8  num_descriptors */
130
                USB_DT_REPORT, /*  u8  type: Report */
131
                52, 0,         /*  u16 len */
132
            },
133
        },
134
    },
135
    .eps = (USBDescEndpoint[]) {
136
        {
137
            .bEndpointAddress      = USB_DIR_IN | 0x01,
138
            .bmAttributes          = USB_ENDPOINT_XFER_INT,
139
            .wMaxPacketSize        = 4,
140
            .bInterval             = 0x0a,
141
        },
142
    },
143
};
144

    
145
static const USBDescIface desc_iface_tablet = {
146
    .bInterfaceNumber              = 0,
147
    .bNumEndpoints                 = 1,
148
    .bInterfaceClass               = USB_CLASS_HID,
149
    .bInterfaceProtocol            = 0x02,
150
    .ndesc                         = 1,
151
    .descs = (USBDescOther[]) {
152
        {
153
            /* HID descriptor */
154
            .data = (uint8_t[]) {
155
                0x09,          /*  u8  bLength */
156
                USB_DT_HID,    /*  u8  bDescriptorType */
157
                0x01, 0x00,    /*  u16 HID_class */
158
                0x00,          /*  u8  country_code */
159
                0x01,          /*  u8  num_descriptors */
160
                USB_DT_REPORT, /*  u8  type: Report */
161
                74, 0,         /*  u16 len */
162
            },
163
        },
164
    },
165
    .eps = (USBDescEndpoint[]) {
166
        {
167
            .bEndpointAddress      = USB_DIR_IN | 0x01,
168
            .bmAttributes          = USB_ENDPOINT_XFER_INT,
169
            .wMaxPacketSize        = 8,
170
            .bInterval             = 0x0a,
171
        },
172
    },
173
};
174

    
175
static const USBDescIface desc_iface_keyboard = {
176
    .bInterfaceNumber              = 0,
177
    .bNumEndpoints                 = 1,
178
    .bInterfaceClass               = USB_CLASS_HID,
179
    .bInterfaceSubClass            = 0x01, /* boot */
180
    .bInterfaceProtocol            = 0x01, /* keyboard */
181
    .ndesc                         = 1,
182
    .descs = (USBDescOther[]) {
183
        {
184
            /* HID descriptor */
185
            .data = (uint8_t[]) {
186
                0x09,          /*  u8  bLength */
187
                USB_DT_HID,    /*  u8  bDescriptorType */
188
                0x11, 0x01,    /*  u16 HID_class */
189
                0x00,          /*  u8  country_code */
190
                0x01,          /*  u8  num_descriptors */
191
                USB_DT_REPORT, /*  u8  type: Report */
192
                0x3f, 0,       /*  u16 len */
193
            },
194
        },
195
    },
196
    .eps = (USBDescEndpoint[]) {
197
        {
198
            .bEndpointAddress      = USB_DIR_IN | 0x01,
199
            .bmAttributes          = USB_ENDPOINT_XFER_INT,
200
            .wMaxPacketSize        = 8,
201
            .bInterval             = 0x0a,
202
        },
203
    },
204
};
205

    
206
static const USBDescDevice desc_device_mouse = {
207
    .bcdUSB                        = 0x0100,
208
    .bMaxPacketSize0               = 8,
209
    .bNumConfigurations            = 1,
210
    .confs = (USBDescConfig[]) {
211
        {
212
            .bNumInterfaces        = 1,
213
            .bConfigurationValue   = 1,
214
            .iConfiguration        = STR_CONFIG_MOUSE,
215
            .bmAttributes          = 0xa0,
216
            .bMaxPower             = 50,
217
            .nif = 1,
218
            .ifs = &desc_iface_mouse,
219
        },
220
    },
221
};
222

    
223
static const USBDescDevice desc_device_tablet = {
224
    .bcdUSB                        = 0x0100,
225
    .bMaxPacketSize0               = 8,
226
    .bNumConfigurations            = 1,
227
    .confs = (USBDescConfig[]) {
228
        {
229
            .bNumInterfaces        = 1,
230
            .bConfigurationValue   = 1,
231
            .iConfiguration        = STR_CONFIG_TABLET,
232
            .bmAttributes          = 0xa0,
233
            .bMaxPower             = 50,
234
            .nif = 1,
235
            .ifs = &desc_iface_tablet,
236
        },
237
    },
238
};
239

    
240
static const USBDescDevice desc_device_keyboard = {
241
    .bcdUSB                        = 0x0100,
242
    .bMaxPacketSize0               = 8,
243
    .bNumConfigurations            = 1,
244
    .confs = (USBDescConfig[]) {
245
        {
246
            .bNumInterfaces        = 1,
247
            .bConfigurationValue   = 1,
248
            .iConfiguration        = STR_CONFIG_KEYBOARD,
249
            .bmAttributes          = 0xa0,
250
            .bMaxPower             = 50,
251
            .nif = 1,
252
            .ifs = &desc_iface_keyboard,
253
        },
254
    },
255
};
256

    
257
static const USBDesc desc_mouse = {
258
    .id = {
259
        .idVendor          = 0x0627,
260
        .idProduct         = 0x0001,
261
        .bcdDevice         = 0,
262
        .iManufacturer     = STR_MANUFACTURER,
263
        .iProduct          = STR_PRODUCT_MOUSE,
264
        .iSerialNumber     = STR_SERIALNUMBER,
265
    },
266
    .full = &desc_device_mouse,
267
    .str  = desc_strings,
268
};
269

    
270
static const USBDesc desc_tablet = {
271
    .id = {
272
        .idVendor          = 0x0627,
273
        .idProduct         = 0x0001,
274
        .bcdDevice         = 0,
275
        .iManufacturer     = STR_MANUFACTURER,
276
        .iProduct          = STR_PRODUCT_TABLET,
277
        .iSerialNumber     = STR_SERIALNUMBER,
278
    },
279
    .full = &desc_device_tablet,
280
    .str  = desc_strings,
281
};
282

    
283
static const USBDesc desc_keyboard = {
284
    .id = {
285
        .idVendor          = 0x0627,
286
        .idProduct         = 0x0001,
287
        .bcdDevice         = 0,
288
        .iManufacturer     = STR_MANUFACTURER,
289
        .iProduct          = STR_PRODUCT_KEYBOARD,
290
        .iSerialNumber     = STR_SERIALNUMBER,
291
    },
292
    .full = &desc_device_keyboard,
293
    .str  = desc_strings,
294
};
295

    
296
static const uint8_t qemu_mouse_hid_report_descriptor[] = {
297
    0x05, 0x01,                /* Usage Page (Generic Desktop) */
298
    0x09, 0x02,                /* Usage (Mouse) */
299
    0xa1, 0x01,                /* Collection (Application) */
300
    0x09, 0x01,                /*   Usage (Pointer) */
301
    0xa1, 0x00,                /*   Collection (Physical) */
302
    0x05, 0x09,                /*     Usage Page (Button) */
303
    0x19, 0x01,                /*     Usage Minimum (1) */
304
    0x29, 0x03,                /*     Usage Maximum (3) */
305
    0x15, 0x00,                /*     Logical Minimum (0) */
306
    0x25, 0x01,                /*     Logical Maximum (1) */
307
    0x95, 0x03,                /*     Report Count (3) */
308
    0x75, 0x01,                /*     Report Size (1) */
309
    0x81, 0x02,                /*     Input (Data, Variable, Absolute) */
310
    0x95, 0x01,                /*     Report Count (1) */
311
    0x75, 0x05,                /*     Report Size (5) */
312
    0x81, 0x01,                /*     Input (Constant) */
313
    0x05, 0x01,                /*     Usage Page (Generic Desktop) */
314
    0x09, 0x30,                /*     Usage (X) */
315
    0x09, 0x31,                /*     Usage (Y) */
316
    0x09, 0x38,                /*     Usage (Wheel) */
317
    0x15, 0x81,                /*     Logical Minimum (-0x7f) */
318
    0x25, 0x7f,                /*     Logical Maximum (0x7f) */
319
    0x75, 0x08,                /*     Report Size (8) */
320
    0x95, 0x03,                /*     Report Count (3) */
321
    0x81, 0x06,                /*     Input (Data, Variable, Relative) */
322
    0xc0,                /*   End Collection */
323
    0xc0,                /* End Collection */
324
};
325

    
326
static const uint8_t qemu_tablet_hid_report_descriptor[] = {
327
    0x05, 0x01,                /* Usage Page (Generic Desktop) */
328
    0x09, 0x01,                /* Usage (Pointer) */
329
    0xa1, 0x01,                /* Collection (Application) */
330
    0x09, 0x01,                /*   Usage (Pointer) */
331
    0xa1, 0x00,                /*   Collection (Physical) */
332
    0x05, 0x09,                /*     Usage Page (Button) */
333
    0x19, 0x01,                /*     Usage Minimum (1) */
334
    0x29, 0x03,                /*     Usage Maximum (3) */
335
    0x15, 0x00,                /*     Logical Minimum (0) */
336
    0x25, 0x01,                /*     Logical Maximum (1) */
337
    0x95, 0x03,                /*     Report Count (3) */
338
    0x75, 0x01,                /*     Report Size (1) */
339
    0x81, 0x02,                /*     Input (Data, Variable, Absolute) */
340
    0x95, 0x01,                /*     Report Count (1) */
341
    0x75, 0x05,                /*     Report Size (5) */
342
    0x81, 0x01,                /*     Input (Constant) */
343
    0x05, 0x01,                /*     Usage Page (Generic Desktop) */
344
    0x09, 0x30,                /*     Usage (X) */
345
    0x09, 0x31,                /*     Usage (Y) */
346
    0x15, 0x00,                /*     Logical Minimum (0) */
347
    0x26, 0xff, 0x7f,        /*     Logical Maximum (0x7fff) */
348
    0x35, 0x00,                /*     Physical Minimum (0) */
349
    0x46, 0xff, 0x7f,        /*     Physical Maximum (0x7fff) */
350
    0x75, 0x10,                /*     Report Size (16) */
351
    0x95, 0x02,                /*     Report Count (2) */
352
    0x81, 0x02,                /*     Input (Data, Variable, Absolute) */
353
    0x05, 0x01,                /*     Usage Page (Generic Desktop) */
354
    0x09, 0x38,                /*     Usage (Wheel) */
355
    0x15, 0x81,                /*     Logical Minimum (-0x7f) */
356
    0x25, 0x7f,                /*     Logical Maximum (0x7f) */
357
    0x35, 0x00,                /*     Physical Minimum (same as logical) */
358
    0x45, 0x00,                /*     Physical Maximum (same as logical) */
359
    0x75, 0x08,                /*     Report Size (8) */
360
    0x95, 0x01,                /*     Report Count (1) */
361
    0x81, 0x06,                /*     Input (Data, Variable, Relative) */
362
    0xc0,                /*   End Collection */
363
    0xc0,                /* End Collection */
364
};
365

    
366
static const uint8_t qemu_keyboard_hid_report_descriptor[] = {
367
    0x05, 0x01,                /* Usage Page (Generic Desktop) */
368
    0x09, 0x06,                /* Usage (Keyboard) */
369
    0xa1, 0x01,                /* Collection (Application) */
370
    0x75, 0x01,                /*   Report Size (1) */
371
    0x95, 0x08,                /*   Report Count (8) */
372
    0x05, 0x07,                /*   Usage Page (Key Codes) */
373
    0x19, 0xe0,                /*   Usage Minimum (224) */
374
    0x29, 0xe7,                /*   Usage Maximum (231) */
375
    0x15, 0x00,                /*   Logical Minimum (0) */
376
    0x25, 0x01,                /*   Logical Maximum (1) */
377
    0x81, 0x02,                /*   Input (Data, Variable, Absolute) */
378
    0x95, 0x01,                /*   Report Count (1) */
379
    0x75, 0x08,                /*   Report Size (8) */
380
    0x81, 0x01,                /*   Input (Constant) */
381
    0x95, 0x05,                /*   Report Count (5) */
382
    0x75, 0x01,                /*   Report Size (1) */
383
    0x05, 0x08,                /*   Usage Page (LEDs) */
384
    0x19, 0x01,                /*   Usage Minimum (1) */
385
    0x29, 0x05,                /*   Usage Maximum (5) */
386
    0x91, 0x02,                /*   Output (Data, Variable, Absolute) */
387
    0x95, 0x01,                /*   Report Count (1) */
388
    0x75, 0x03,                /*   Report Size (3) */
389
    0x91, 0x01,                /*   Output (Constant) */
390
    0x95, 0x06,                /*   Report Count (6) */
391
    0x75, 0x08,                /*   Report Size (8) */
392
    0x15, 0x00,                /*   Logical Minimum (0) */
393
    0x25, 0xff,                /*   Logical Maximum (255) */
394
    0x05, 0x07,                /*   Usage Page (Key Codes) */
395
    0x19, 0x00,                /*   Usage Minimum (0) */
396
    0x29, 0xff,                /*   Usage Maximum (255) */
397
    0x81, 0x00,                /*   Input (Data, Array) */
398
    0xc0,                /* End Collection */
399
};
400

    
401
#define USB_HID_USAGE_ERROR_ROLLOVER        0x01
402
#define USB_HID_USAGE_POSTFAIL                0x02
403
#define USB_HID_USAGE_ERROR_UNDEFINED        0x03
404

    
405
/* Indices are QEMU keycodes, values are from HID Usage Table.  Indices
406
 * above 0x80 are for keys that come after 0xe0 or 0xe1+0x1d or 0xe1+0x9d.  */
407
static const uint8_t usb_hid_usage_keys[0x100] = {
408
    0x00, 0x29, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
409
    0x24, 0x25, 0x26, 0x27, 0x2d, 0x2e, 0x2a, 0x2b,
410
    0x14, 0x1a, 0x08, 0x15, 0x17, 0x1c, 0x18, 0x0c,
411
    0x12, 0x13, 0x2f, 0x30, 0x28, 0xe0, 0x04, 0x16,
412
    0x07, 0x09, 0x0a, 0x0b, 0x0d, 0x0e, 0x0f, 0x33,
413
    0x34, 0x35, 0xe1, 0x31, 0x1d, 0x1b, 0x06, 0x19,
414
    0x05, 0x11, 0x10, 0x36, 0x37, 0x38, 0xe5, 0x55,
415
    0xe2, 0x2c, 0x32, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e,
416
    0x3f, 0x40, 0x41, 0x42, 0x43, 0x53, 0x47, 0x5f,
417
    0x60, 0x61, 0x56, 0x5c, 0x5d, 0x5e, 0x57, 0x59,
418
    0x5a, 0x5b, 0x62, 0x63, 0x00, 0x00, 0x00, 0x44,
419
    0x45, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e,
420
    0xe8, 0xe9, 0x71, 0x72, 0x73, 0x00, 0x00, 0x00,
421
    0x00, 0x00, 0x00, 0x85, 0x00, 0x00, 0x00, 0x00,
422
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
423
    0x00, 0x00, 0x00, 0x00, 0x00, 0xe3, 0xe7, 0x65,
424

    
425
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
426
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
427
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
428
    0x00, 0x00, 0x00, 0x00, 0x58, 0xe4, 0x00, 0x00,
429
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
430
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
431
    0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x00, 0x46,
432
    0xe6, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
433
    0x00, 0x00, 0x00, 0x00, 0x00, 0x48, 0x00, 0x4a,
434
    0x52, 0x4b, 0x00, 0x50, 0x00, 0x4f, 0x00, 0x4d,
435
    0x51, 0x4e, 0x49, 0x4c, 0x00, 0x00, 0x00, 0x00,
436
    0x00, 0x00, 0x00, 0xe3, 0xe7, 0x65, 0x00, 0x00,
437
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
438
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
439
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
440
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
441
};
442

    
443
static void usb_hid_changed(USBHIDState *hs)
444
{
445
    hs->changed = 1;
446

    
447
    if (hs->datain)
448
        hs->datain(hs->datain_opaque);
449

    
450
    usb_wakeup(&hs->dev);
451
}
452

    
453
static void hid_pointer_event_clear(HIDPointerEvent *e, int buttons)
454
{
455
    e->xdx = e->ydy = e->dz = 0;
456
    e->buttons_state = buttons;
457
}
458

    
459
static void hid_pointer_event_combine(HIDPointerEvent *e, int xyrel,
460
                                      int x1, int y1, int z1) {
461
    if (xyrel) {
462
        e->xdx += x1;
463
        e->ydy += y1;
464
    } else {
465
        e->xdx = x1;
466
        e->ydy = y1;
467
        /* Windows drivers do not like the 0/0 position and ignore such
468
         * events. */
469
        if (!(x1 | y1)) {
470
            x1 = 1;
471
        }
472
    }
473
    e->dz += z1;
474
}
475

    
476
static void usb_pointer_event(void *opaque,
477
                              int x1, int y1, int z1, int buttons_state)
478
{
479
    USBHIDState *us = opaque;
480
    HIDState *hs = &us->hid;
481
    unsigned use_slot = (hs->head + hs->n - 1) & QUEUE_MASK;
482
    unsigned previous_slot = (use_slot - 1) & QUEUE_MASK;
483

    
484
    /* We combine events where feasible to keep the queue small.  We shouldn't
485
     * combine anything with the first event of a particular button state, as
486
     * that would change the location of the button state change.  When the
487
     * queue is empty, a second event is needed because we don't know if
488
     * the first event changed the button state.  */
489
    if (hs->n == QUEUE_LENGTH) {
490
        /* Queue full.  Discard old button state, combine motion normally.  */
491
        hs->ptr.queue[use_slot].buttons_state = buttons_state;
492
    } else if (hs->n < 2 ||
493
               hs->ptr.queue[use_slot].buttons_state != buttons_state ||
494
               hs->ptr.queue[previous_slot].buttons_state !=
495
               hs->ptr.queue[use_slot].buttons_state) {
496
        /* Cannot or should not combine, so add an empty item to the queue.  */
497
        QUEUE_INCR(use_slot);
498
        hs->n++;
499
        hid_pointer_event_clear(&hs->ptr.queue[use_slot], buttons_state);
500
    }
501
    hid_pointer_event_combine(&hs->ptr.queue[use_slot],
502
                              hs->kind == HID_MOUSE,
503
                              x1, y1, z1);
504
    usb_hid_changed(us);
505
}
506

    
507
static void usb_keyboard_event(void *opaque, int keycode)
508
{
509
    USBHIDState *us = opaque;
510
    HIDState *hs = &us->hid;
511
    int slot;
512

    
513
    if (hs->n == QUEUE_LENGTH) {
514
        fprintf(stderr, "usb-kbd: warning: key event queue full\n");
515
        return;
516
    }
517
    slot = (hs->head + hs->n) & QUEUE_MASK; hs->n++;
518
    hs->kbd.keycodes[slot] = keycode;
519
    usb_hid_changed(us);
520
}
521

    
522
static void hid_keyboard_process_keycode(HIDState *hs)
523
{
524
    uint8_t hid_code, key;
525
    int i, keycode, slot;
526

    
527
    if (hs->n == 0) {
528
        return;
529
    }
530
    slot = hs->head & QUEUE_MASK; QUEUE_INCR(hs->head); hs->n--;
531
    keycode = hs->kbd.keycodes[slot];
532

    
533
    key = keycode & 0x7f;
534
    hid_code = usb_hid_usage_keys[key | ((hs->kbd.modifiers >> 1) & (1 << 7))];
535
    hs->kbd.modifiers &= ~(1 << 8);
536

    
537
    switch (hid_code) {
538
    case 0x00:
539
        return;
540

    
541
    case 0xe0:
542
        if (hs->kbd.modifiers & (1 << 9)) {
543
            hs->kbd.modifiers ^= 3 << 8;
544
            return;
545
        }
546
    case 0xe1 ... 0xe7:
547
        if (keycode & (1 << 7)) {
548
            hs->kbd.modifiers &= ~(1 << (hid_code & 0x0f));
549
            return;
550
        }
551
    case 0xe8 ... 0xef:
552
        hs->kbd.modifiers |= 1 << (hid_code & 0x0f);
553
        return;
554
    }
555

    
556
    if (keycode & (1 << 7)) {
557
        for (i = hs->kbd.keys - 1; i >= 0; i--) {
558
            if (hs->kbd.key[i] == hid_code) {
559
                hs->kbd.key[i] = hs->kbd.key[-- hs->kbd.keys];
560
                hs->kbd.key[hs->kbd.keys] = 0x00;
561
                break;
562
            }
563
        }
564
        if (i < 0) {
565
            return;
566
        }
567
    } else {
568
        for (i = hs->kbd.keys - 1; i >= 0; i--) {
569
            if (hs->kbd.key[i] == hid_code) {
570
                break;
571
            }
572
        }
573
        if (i < 0) {
574
            if (hs->kbd.keys < sizeof(hs->kbd.key)) {
575
                hs->kbd.key[hs->kbd.keys++] = hid_code;
576
            }
577
        } else {
578
            return;
579
        }
580
    }
581
}
582

    
583
static inline int int_clamp(int val, int vmin, int vmax)
584
{
585
    if (val < vmin)
586
        return vmin;
587
    else if (val > vmax)
588
        return vmax;
589
    else
590
        return val;
591
}
592

    
593
static int hid_pointer_poll(HIDState *hs, uint8_t *buf, int len)
594
{
595
    int dx, dy, dz, b, l;
596
    int index;
597
    HIDPointerEvent *e;
598

    
599
    if (!hs->ptr.mouse_grabbed) {
600
        qemu_activate_mouse_event_handler(hs->ptr.eh_entry);
601
        hs->ptr.mouse_grabbed = 1;
602
    }
603

    
604
    /* When the buffer is empty, return the last event.  Relative
605
       movements will all be zero.  */
606
    index = (hs->n ? hs->head : hs->head - 1);
607
    e = &hs->ptr.queue[index & QUEUE_MASK];
608

    
609
    if (hs->kind == HID_MOUSE) {
610
        dx = int_clamp(e->xdx, -127, 127);
611
        dy = int_clamp(e->ydy, -127, 127);
612
        e->xdx -= dx;
613
        e->ydy -= dy;
614
    } else {
615
        dx = e->xdx;
616
        dy = e->ydy;
617
    }
618
    dz = int_clamp(e->dz, -127, 127);
619
    e->dz -= dz;
620

    
621
    b = 0;
622
    if (e->buttons_state & MOUSE_EVENT_LBUTTON)
623
        b |= 0x01;
624
    if (e->buttons_state & MOUSE_EVENT_RBUTTON)
625
        b |= 0x02;
626
    if (e->buttons_state & MOUSE_EVENT_MBUTTON)
627
        b |= 0x04;
628

    
629
    if (hs->n &&
630
        !e->dz &&
631
        (hs->kind == HID_TABLET || (!e->xdx && !e->ydy))) {
632
        /* that deals with this event */
633
        QUEUE_INCR(hs->head);
634
        hs->n--;
635
    }
636

    
637
    /* Appears we have to invert the wheel direction */
638
    dz = 0 - dz;
639
    l = 0;
640
    switch (hs->kind) {
641
    case HID_MOUSE:
642
        if (len > l)
643
            buf[l++] = b;
644
        if (len > l)
645
            buf[l++] = dx;
646
        if (len > l)
647
            buf[l++] = dy;
648
        if (len > l)
649
            buf[l++] = dz;
650
        break;
651

    
652
    case HID_TABLET:
653
        if (len > l)
654
            buf[l++] = b;
655
        if (len > l)
656
            buf[l++] = dx & 0xff;
657
        if (len > l)
658
            buf[l++] = dx >> 8;
659
        if (len > l)
660
            buf[l++] = dy & 0xff;
661
        if (len > l)
662
            buf[l++] = dy >> 8;
663
        if (len > l)
664
            buf[l++] = dz;
665
        break;
666

    
667
    default:
668
        abort();
669
    }
670

    
671
    return l;
672
}
673

    
674
static int hid_keyboard_poll(HIDState *hs, uint8_t *buf, int len)
675
{
676
    if (len < 2)
677
        return 0;
678

    
679
    hid_keyboard_process_keycode(hs);
680

    
681
    buf[0] = hs->kbd.modifiers & 0xff;
682
    buf[1] = 0;
683
    if (hs->kbd.keys > 6) {
684
        memset(buf + 2, USB_HID_USAGE_ERROR_ROLLOVER, MIN(8, len) - 2);
685
    } else {
686
        memcpy(buf + 2, hs->kbd.key, MIN(8, len) - 2);
687
    }
688

    
689
    return MIN(8, len);
690
}
691

    
692
static int hid_keyboard_write(HIDState *hs, uint8_t *buf, int len)
693
{
694
    if (len > 0) {
695
        int ledstate = 0;
696
        /* 0x01: Num Lock LED
697
         * 0x02: Caps Lock LED
698
         * 0x04: Scroll Lock LED
699
         * 0x08: Compose LED
700
         * 0x10: Kana LED */
701
        hs->kbd.leds = buf[0];
702
        if (hs->kbd.leds & 0x04) {
703
            ledstate |= QEMU_SCROLL_LOCK_LED;
704
        }
705
        if (hs->kbd.leds & 0x01) {
706
            ledstate |= QEMU_NUM_LOCK_LED;
707
        }
708
        if (hs->kbd.leds & 0x02) {
709
            ledstate |= QEMU_CAPS_LOCK_LED;
710
        }
711
        kbd_put_ledstate(ledstate);
712
    }
713
    return 0;
714
}
715

    
716
static void usb_mouse_handle_reset(USBDevice *dev)
717
{
718
    USBHIDState *us = DO_UPCAST(USBHIDState, dev, dev);
719

    
720
    memset(us->hid.ptr.queue, 0, sizeof(us->hid.ptr.queue));
721
    us->hid.head = 0;
722
    us->hid.n = 0;
723
    us->protocol = 1;
724
}
725

    
726
static void usb_keyboard_handle_reset(USBDevice *dev)
727
{
728
    USBHIDState *us = DO_UPCAST(USBHIDState, dev, dev);
729

    
730
    qemu_add_kbd_event_handler(usb_keyboard_event, us);
731
    memset(us->hid.kbd.keycodes, 0, sizeof(us->hid.kbd.keycodes));
732
    us->hid.head = 0;
733
    us->hid.n = 0;
734
    memset(us->hid.kbd.key, 0, sizeof(us->hid.kbd.key));
735
    us->hid.kbd.keys = 0;
736
    us->protocol = 1;
737
}
738

    
739
static void usb_hid_set_next_idle(USBHIDState *s, int64_t curtime)
740
{
741
    s->next_idle_clock = curtime + (get_ticks_per_sec() * s->idle * 4) / 1000;
742
}
743

    
744
static int usb_hid_handle_control(USBDevice *dev, USBPacket *p,
745
               int request, int value, int index, int length, uint8_t *data)
746
{
747
    USBHIDState *us = DO_UPCAST(USBHIDState, dev, dev);
748
    HIDState *hs = &us->hid;
749
    int ret;
750

    
751
    ret = usb_desc_handle_control(dev, p, request, value, index, length, data);
752
    if (ret >= 0) {
753
        return ret;
754
    }
755

    
756
    ret = 0;
757
    switch (request) {
758
    case DeviceRequest | USB_REQ_GET_INTERFACE:
759
        data[0] = 0;
760
        ret = 1;
761
        break;
762
    case DeviceOutRequest | USB_REQ_SET_INTERFACE:
763
        ret = 0;
764
        break;
765
        /* hid specific requests */
766
    case InterfaceRequest | USB_REQ_GET_DESCRIPTOR:
767
        switch (value >> 8) {
768
        case 0x22:
769
            if (hs->kind == HID_MOUSE) {
770
                memcpy(data, qemu_mouse_hid_report_descriptor,
771
                       sizeof(qemu_mouse_hid_report_descriptor));
772
                ret = sizeof(qemu_mouse_hid_report_descriptor);
773
            } else if (hs->kind == HID_TABLET) {
774
                memcpy(data, qemu_tablet_hid_report_descriptor,
775
                       sizeof(qemu_tablet_hid_report_descriptor));
776
                ret = sizeof(qemu_tablet_hid_report_descriptor);
777
            } else if (hs->kind == HID_KEYBOARD) {
778
                memcpy(data, qemu_keyboard_hid_report_descriptor,
779
                       sizeof(qemu_keyboard_hid_report_descriptor));
780
                ret = sizeof(qemu_keyboard_hid_report_descriptor);
781
            }
782
            break;
783
        default:
784
            goto fail;
785
        }
786
        break;
787
    case GET_REPORT:
788
        if (hs->kind == HID_MOUSE || hs->kind == HID_TABLET) {
789
            ret = hid_pointer_poll(hs, data, length);
790
        } else if (hs->kind == HID_KEYBOARD) {
791
            ret = hid_keyboard_poll(hs, data, length);
792
        }
793
        us->changed = hs->n > 0;
794
        break;
795
    case SET_REPORT:
796
        if (hs->kind == HID_KEYBOARD) {
797
            ret = hid_keyboard_write(hs, data, length);
798
        } else {
799
            goto fail;
800
        }
801
        break;
802
    case GET_PROTOCOL:
803
        if (hs->kind != HID_KEYBOARD && hs->kind != HID_MOUSE) {
804
            goto fail;
805
        }
806
        ret = 1;
807
        data[0] = us->protocol;
808
        break;
809
    case SET_PROTOCOL:
810
        if (hs->kind != HID_KEYBOARD && hs->kind != HID_MOUSE) {
811
            goto fail;
812
        }
813
        ret = 0;
814
        us->protocol = value;
815
        break;
816
    case GET_IDLE:
817
        ret = 1;
818
        data[0] = us->idle;
819
        break;
820
    case SET_IDLE:
821
        us->idle = (uint8_t) (value >> 8);
822
        usb_hid_set_next_idle(us, qemu_get_clock_ns(vm_clock));
823
        ret = 0;
824
        break;
825
    default:
826
    fail:
827
        ret = USB_RET_STALL;
828
        break;
829
    }
830
    return ret;
831
}
832

    
833
static int usb_hid_handle_data(USBDevice *dev, USBPacket *p)
834
{
835
    USBHIDState *us = DO_UPCAST(USBHIDState, dev, dev);
836
    HIDState *hs = &us->hid;
837
    uint8_t buf[p->iov.size];
838
    int ret = 0;
839

    
840
    switch (p->pid) {
841
    case USB_TOKEN_IN:
842
        if (p->devep == 1) {
843
            int64_t curtime = qemu_get_clock_ns(vm_clock);
844
            if (!us->changed &&
845
                (!us->idle || us->next_idle_clock - curtime > 0)) {
846
                return USB_RET_NAK;
847
            }
848
            usb_hid_set_next_idle(us, curtime);
849
            if (hs->kind == HID_MOUSE || hs->kind == HID_TABLET) {
850
                ret = hid_pointer_poll(hs, buf, p->iov.size);
851
            } else if (hs->kind == HID_KEYBOARD) {
852
                ret = hid_keyboard_poll(hs, buf, p->iov.size);
853
            }
854
            usb_packet_copy(p, buf, ret);
855
            us->changed = hs->n > 0;
856
        } else {
857
            goto fail;
858
        }
859
        break;
860
    case USB_TOKEN_OUT:
861
    default:
862
    fail:
863
        ret = USB_RET_STALL;
864
        break;
865
    }
866
    return ret;
867
}
868

    
869
static void usb_hid_handle_destroy(USBDevice *dev)
870
{
871
    USBHIDState *us = DO_UPCAST(USBHIDState, dev, dev);
872

    
873
    switch (us->hid.kind) {
874
    case HID_KEYBOARD:
875
        qemu_remove_kbd_event_handler();
876
        break;
877
    default:
878
        qemu_remove_mouse_event_handler(us->hid.ptr.eh_entry);
879
    }
880
}
881

    
882
static int usb_hid_initfn(USBDevice *dev, int kind)
883
{
884
    USBHIDState *us = DO_UPCAST(USBHIDState, dev, dev);
885
    HIDState *hs = &us->hid;
886

    
887
    usb_desc_init(dev);
888
    hs->kind = kind;
889

    
890
    if (hs->kind == HID_MOUSE) {
891
        hs->ptr.eh_entry = qemu_add_mouse_event_handler(usb_pointer_event, us,
892
                                                        0, "QEMU HID Mouse");
893
    } else if (hs->kind == HID_TABLET) {
894
        hs->ptr.eh_entry = qemu_add_mouse_event_handler(usb_pointer_event, us,
895
                                                        1, "QEMU HID Tablet");
896
    }
897

    
898
    /* Force poll routine to be run and grab input the first time.  */
899
    us->changed = 1;
900
    return 0;
901
}
902

    
903
static int usb_tablet_initfn(USBDevice *dev)
904
{
905
    return usb_hid_initfn(dev, HID_TABLET);
906
}
907

    
908
static int usb_mouse_initfn(USBDevice *dev)
909
{
910
    return usb_hid_initfn(dev, HID_MOUSE);
911
}
912

    
913
static int usb_keyboard_initfn(USBDevice *dev)
914
{
915
    return usb_hid_initfn(dev, HID_KEYBOARD);
916
}
917

    
918
void usb_hid_datain_cb(USBDevice *dev, void *opaque, void (*datain)(void *))
919
{
920
    USBHIDState *s = (USBHIDState *)dev;
921

    
922
    s->datain_opaque = opaque;
923
    s->datain = datain;
924
}
925

    
926
static int usb_hid_post_load(void *opaque, int version_id)
927
{
928
    USBHIDState *s = opaque;
929

    
930
    if (s->idle) {
931
        usb_hid_set_next_idle(s, qemu_get_clock_ns(vm_clock));
932
    }
933
    return 0;
934
}
935

    
936
static const VMStateDescription vmstate_usb_ptr_queue = {
937
    .name = "usb-ptr-queue",
938
    .version_id = 1,
939
    .minimum_version_id = 1,
940
    .fields = (VMStateField []) {
941
        VMSTATE_INT32(xdx, HIDPointerEvent),
942
        VMSTATE_INT32(ydy, HIDPointerEvent),
943
        VMSTATE_INT32(dz, HIDPointerEvent),
944
        VMSTATE_INT32(buttons_state, HIDPointerEvent),
945
        VMSTATE_END_OF_LIST()
946
    }
947
};
948
static const VMStateDescription vmstate_usb_ptr = {
949
    .name = "usb-ptr",
950
    .version_id = 1,
951
    .minimum_version_id = 1,
952
    .post_load = usb_hid_post_load,
953
    .fields = (VMStateField []) {
954
        VMSTATE_USB_DEVICE(dev, USBHIDState),
955
        VMSTATE_STRUCT_ARRAY(hid.ptr.queue, USBHIDState, QUEUE_LENGTH, 0,
956
                             vmstate_usb_ptr_queue, HIDPointerEvent),
957
        VMSTATE_UINT32(hid.head, USBHIDState),
958
        VMSTATE_UINT32(hid.n, USBHIDState),
959
        VMSTATE_INT32(protocol, USBHIDState),
960
        VMSTATE_UINT8(idle, USBHIDState),
961
        VMSTATE_END_OF_LIST()
962
    }
963
};
964

    
965
static const VMStateDescription vmstate_usb_kbd = {
966
    .name = "usb-kbd",
967
    .version_id = 1,
968
    .minimum_version_id = 1,
969
    .post_load = usb_hid_post_load,
970
    .fields = (VMStateField []) {
971
        VMSTATE_USB_DEVICE(dev, USBHIDState),
972
        VMSTATE_UINT32_ARRAY(hid.kbd.keycodes, USBHIDState, QUEUE_LENGTH),
973
        VMSTATE_UINT32(hid.head, USBHIDState),
974
        VMSTATE_UINT32(hid.n, USBHIDState),
975
        VMSTATE_UINT16(hid.kbd.modifiers, USBHIDState),
976
        VMSTATE_UINT8(hid.kbd.leds, USBHIDState),
977
        VMSTATE_UINT8_ARRAY(hid.kbd.key, USBHIDState, 16),
978
        VMSTATE_INT32(hid.kbd.keys, USBHIDState),
979
        VMSTATE_INT32(protocol, USBHIDState),
980
        VMSTATE_UINT8(idle, USBHIDState),
981
        VMSTATE_END_OF_LIST()
982
    }
983
};
984

    
985
static struct USBDeviceInfo hid_info[] = {
986
    {
987
        .product_desc   = "QEMU USB Tablet",
988
        .qdev.name      = "usb-tablet",
989
        .usbdevice_name = "tablet",
990
        .qdev.size      = sizeof(USBHIDState),
991
        .qdev.vmsd      = &vmstate_usb_ptr,
992
        .usb_desc       = &desc_tablet,
993
        .init           = usb_tablet_initfn,
994
        .handle_packet  = usb_generic_handle_packet,
995
        .handle_reset   = usb_mouse_handle_reset,
996
        .handle_control = usb_hid_handle_control,
997
        .handle_data    = usb_hid_handle_data,
998
        .handle_destroy = usb_hid_handle_destroy,
999
    },{
1000
        .product_desc   = "QEMU USB Mouse",
1001
        .qdev.name      = "usb-mouse",
1002
        .usbdevice_name = "mouse",
1003
        .qdev.size      = sizeof(USBHIDState),
1004
        .qdev.vmsd      = &vmstate_usb_ptr,
1005
        .usb_desc       = &desc_mouse,
1006
        .init           = usb_mouse_initfn,
1007
        .handle_packet  = usb_generic_handle_packet,
1008
        .handle_reset   = usb_mouse_handle_reset,
1009
        .handle_control = usb_hid_handle_control,
1010
        .handle_data    = usb_hid_handle_data,
1011
        .handle_destroy = usb_hid_handle_destroy,
1012
    },{
1013
        .product_desc   = "QEMU USB Keyboard",
1014
        .qdev.name      = "usb-kbd",
1015
        .usbdevice_name = "keyboard",
1016
        .qdev.size      = sizeof(USBHIDState),
1017
        .qdev.vmsd      = &vmstate_usb_kbd,
1018
        .usb_desc       = &desc_keyboard,
1019
        .init           = usb_keyboard_initfn,
1020
        .handle_packet  = usb_generic_handle_packet,
1021
        .handle_reset   = usb_keyboard_handle_reset,
1022
        .handle_control = usb_hid_handle_control,
1023
        .handle_data    = usb_hid_handle_data,
1024
        .handle_destroy = usb_hid_handle_destroy,
1025
    },{
1026
        /* end of list */
1027
    }
1028
};
1029

    
1030
static void usb_hid_register_devices(void)
1031
{
1032
    usb_qdev_register_many(hid_info);
1033
}
1034
device_init(usb_hid_register_devices)