summaryrefslogtreecommitdiff
path: root/hw/usb/hid-logitech-dj.c
blob: 77e7f20202fbc63e46fa5bc35703e823dbf8be76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
 * Logitech Unifying receiver emulation (HID++ protocol).
 *
 * Copyright (c) 2014 Peter Wu <peter@lekensteyn.nl>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
#include "hw/hw.h"
#include "ui/console.h"
#include "hw/usb.h"
#include "dump.h"
#include "hw/input/hid.h"
#include "hw/usb/hid-logitech-dj.h"

#define COMPILE_ASSERT(cond)    typedef char _compile_assert[1 - 2 * !(cond)]

/* Report IDs */
enum {
    HIDPP_SHORT   = 0x10,   /* 7 bytes */
    HIDPP_LONG    = 0x11,   /* 20 bytes */
    DJ_SHORT      = 0x20,   /* 15 bytes */
    DJ_LONG       = 0x21,   /* 32 bytes */
};

/* returns the expected length of the report or 0 if invalid */
static unsigned msg_report_length(HidppMsg *msg)
{
    COMPILE_ASSERT(sizeof(HidppMsgShort) == 7);
    COMPILE_ASSERT(sizeof(HidppMsgLong) == 20);
    COMPILE_ASSERT(sizeof(DjMsgShort) == 15);
    COMPILE_ASSERT(sizeof(DjMsgLong) == 32);

    switch (msg->report_id) {
        case HIDPP_SHORT:   return 7;
        case HIDPP_LONG:    return 20;
        case DJ_SHORT:      return 15;
        case DJ_LONG:       return 32;
        default:            return 0;
    }
}

static void hidpp_queue_error(USBLtunifyState *s, HidppMsg *msg, uint8_t err)
{
    HidppMsgShort *report;
    unsigned slot;

    if (s->error_queue.n < LQUEUE_SIZE(s->error_queue)) {
        slot = LQUEUE_WRAP(s->error_queue, s->error_queue.head + s->error_queue.n);
        s->error_queue.n++;
        report = &s->error_queue.reports[slot];
        report->report_id = HIDPP_SHORT;
        report->device_index = msg->device_index;
        report->sub_id = 0x8F;
        report->address = msg->hidpp_s.sub_id;
        report->value[0] = msg->hidpp_s.address;
        report->value[1] = err;
        report->value[2] = 0;
    }
}

static void hidpp_queue_output_report(USBLtunifyState *s, HidppMsg *msg)
{
    unsigned slot;

    assert(msg_report_length(msg) != 0);

    if (s->output_queue.n < LQUEUE_SIZE(s->output_queue)) {
        slot = LQUEUE_WRAP(s->output_queue, s->output_queue.head + s->output_queue.n);
        s->output_queue.n++;
        s->output_queue.reports[slot] = *msg;
    }
}

static bool hidpp_device_available(USBLtunifyState *s, uint8_t device_index)
{
    /* TODO: consider unpaired / powered off */
    return device_index >= 1 && device_index <= MAX_DEVICES &&
        s->devices[device_index - 1].powered_on;
}

static bool hidpp_process_receiver_report(USBLtunifyState *s, HidppMsg *msg)
{
    LHidDevice *hd;
    int i;
    uint8_t *parms = msg->dj_s.payload;

    assert(msg->device_index == 0xFF);

    if (msg->report_id == DJ_SHORT) {
        /* DJ reports */
        switch (msg->dj_s.report_type) {
        case 0x0E: /* Keyboard LEDs */
            /* ignored */
            break;
        case 0x80: /* Switch and Keep-Alive */
            for (i = 0; i < MAX_DEVICES; i++) {
                hd = &s->devices[i];
                if ((parms[0] >> i) & 1) {
                    hd->mode = LTUNIFY_MODE_DJ;
                } else {
                    hd->mode = LTUNIFY_MODE_HID;
                }
            }
            // not implemented: keep-alive timeout params[1];
            break;
        case 0x81: /* Get Paired Devices */
            memset(msg, 0, sizeof(*msg));
            msg->report_id = DJ_SHORT;
            msg->dj_s.report_type = 0x41; /* Device Paired notif */
            parms[0] = 1; /* more notifications will follow bit. */
            for (i = 0; i < MAX_DEVICES; i++) {
                hd = &s->devices[i];
                if (!hidpp_device_available(s, i + 1)) {
                    continue;
                }

                msg->device_index = i + 1;
                parms[1] = (uint8_t) hd->info.wireless_pid;
                parms[2] = (uint8_t) (hd->info.wireless_pid >> 8);
                parms[3] = (uint8_t) hd->info.report_types;
                parms[4] = (uint8_t) (hd->info.report_types >> 8);
                parms[5] = (uint8_t) (hd->info.report_types >> 16);
                parms[6] = (uint8_t) (hd->info.report_types >> 24);
                hidpp_queue_output_report(s, msg);
            }
            /* end of notifications list */
            msg->device_index = 0x00;
            parms[0] = 2;
            memset(parms + 1, 0, 6);
            hidpp_queue_output_report(s, msg);
            break;
        default:
            hidpp_queue_error(s, msg, HIDPP_ERR_INVALID_SUBID);
        }
    } else if (msg->report_id == HIDPP_SHORT || msg->report_id == HIDPP_LONG) {
        /* TODO: handle requests */
    } else {
        /* DJ_LONG is unhandled */
        return false;
    }

    /* report is accepted and possibly processed */
    return true;
}

static void hidpp_process_input_report(USBLtunifyState *s, HidppMsg msg)
{
    /* receiver reports are processed earlier, elsewhere */
    assert(msg.device_index != 0xFF);

    if (hidpp_device_available(s, msg.device_index)) {
        /* TODO: implement for devices */
        hidpp_queue_error(s, &msg, HIDPP_ERR_REQUEST_UNAVAILABLE);
    } else {
        hidpp_queue_error(s, &msg, HIDPP_ERR_RESOURCE_ERROR);
    }
}

/* process a received report */
static void hidpp_set_report(USBDevice *dev, USBPacket *p, uint8_t *data, size_t len)
{
    USBLtunifyState *s = DO_UPCAST(USBLtunifyState, dev, dev);
    HidppMsg *msg = (HidppMsg *) data;
    int report_len;

    if (len < sizeof(HidppMsgShort)) {
        goto fail;
    }

    report_len = msg_report_length(msg);
    if (report_len > 0 && len >= report_len) {
        if (msg->device_index == 0xFF) {
            /* receiver messages can be processed immediately */
            if (!hidpp_process_receiver_report(s, msg)) {
                goto fail;
            }
        } else if (s->input_queue.n < LQUEUE_SIZE(s->input_queue)) {
            unsigned slot = LQUEUE_WRAP(s->input_queue, s->input_queue.head + s->input_queue.n);
            s->input_queue.n++;
            memcpy((uint8_t *) &s->input_queue.reports[slot], data, report_len);
        } else {
            /* queue full, cannot accept this report */
            hidpp_queue_error(s, msg, HIDPP_ERR_BUSY);
        }
    } else {
fail:
        p->status = USB_RET_STALL;
    }
}

void usb_ltunify_handle_control_hidpp(USBDevice *dev, USBPacket *p,
               int request, int value, int index, int length, uint8_t *data)
{
    //USBLtunifyState *s = (USBLtunifyState *) dev;

    switch (request) {
    case HID_GET_REPORT:
        /* FIXME */
        break;
    case HID_SET_REPORT:
        hidpp_set_report(dev, p, data, length);
        break;
    default:
        p->status = USB_RET_STALL;
        break;
    }
}

static void hidpp_handle_hid(USBDevice *dev, USBPacket *p)
{
    USBLtunifyState *s = (USBLtunifyState *) dev;
    DjMsgShort msg = { 0 };
    uint8_t *data = (uint8_t *) &msg;
    uint8_t *hid_data = msg.payload;
    int i;

    msg.report_id = DJ_SHORT;

    /* TODO: be more fair, right now the first device always takes all events,
     * causing delays for other devices. */
    for (i = 0; i < MAX_DEVICES; i++) {
        LHidDevice *hd = &s->devices[i];

        if (!hd->info.device_type) {
            continue;
        }

        if (hd->hid->kind == HID_MOUSE) {
            /* start accepting pointer events if not already */
            hid_pointer_activate(hd->hid);
        }

        if (!hid_has_events(hd->hid)) {
            continue;
        }

        hid_set_next_idle(hd->hid);

        msg.device_index = i + 1;
        if (hd->hid->kind == HID_MOUSE) {
            msg.report_type = 0x02;
            hid_pointer_poll(hd->hid, hid_data, sizeof(msg.payload));
        } else if (hd->hid->kind == HID_KEYBOARD) {
            msg.report_type = 0x01;
            hid_keyboard_poll(hd->hid, hid_data, sizeof(msg.payload));
        }

        usb_dump_complete_data(s->usb_dump_state, p, data, sizeof(msg));
        usb_packet_copy(p, data, sizeof(msg));
        usb_dump_submit(s->usb_dump_state, p);
        return;
    }

    p->status = USB_RET_NAK;
}

void usb_ltunify_handle_datain_hidpp(USBDevice *dev, USBPacket *p)
{
    USBLtunifyState *s = (USBLtunifyState *) dev;
    int slot;
    int len = 0;
    HidppMsg *msg = NULL;

    assert(p->pid == USB_TOKEN_IN);
    assert(p->ep->nr == 3);

    /* process input reports */
    if (s->input_queue.n > 0) {
        slot = s->input_queue.head;
        LQUEUE_INCR(s->input_queue, s->input_queue.head);
        s->input_queue.n--;
        hidpp_process_input_report(s, s->input_queue.reports[slot]);
    }

    /* try to send a reply to a previously sent request if possible. */
    if (s->error_queue.n > 0) {
        slot = s->error_queue.head;
        LQUEUE_INCR(s->error_queue, s->error_queue.head);
        s->error_queue.n--;
        msg = (HidppMsg *) &s->error_queue.reports[slot];
    } else if (s->output_queue.n > 0) {
        slot = s->output_queue.head;
        LQUEUE_INCR(s->output_queue, s->output_queue.head);
        s->output_queue.n--;
        msg = &s->output_queue.reports[slot];
    }

    if (msg == NULL) {
        hidpp_handle_hid(dev, p);
        return;
    }

    if (msg != NULL) {
        len = msg_report_length(msg);
        assert(len > 0);
        usb_dump_complete_data(s->usb_dump_state, p, (uint8_t *) msg, len);
        usb_packet_copy(p, (uint8_t *) msg, len);
        usb_dump_submit(s->usb_dump_state, p);
    } else {
        p->status = USB_RET_NAK;
    }
}

static void hidpp_init_device(USBLtunifyState *s, int device_index, int devtype)
{
    LHidDevice *hd;

    assert(device_index >= 1 && device_index <= MAX_DEVICES);
    hd = &s->devices[device_index - 1];

    memset(hd, 0, sizeof(*hd));

    hd->info.device_type = devtype;
    switch (devtype) {
    case DEVTYPE_KEYBOARD:
        hd->hid = &s->hid[IFACE_KBD];
        hd->info.report_types = 0x1a; /* Keyboard, Consumer Control, System Control */
        hd->info.wireless_pid = 0x2010;
        hd->info.serial = 0x4b657973;
        memcpy(hd->info.name, "K800", 4);
        break;
    case DEVTYPE_MOUSE:
        hd->hid = &s->hid[IFACE_MSE];
        hd->info.report_types = 4; /* HID Collection: Mouse */
        hd->info.wireless_pid = 0x4013;
        hd->info.serial = 0x52617473;
        memcpy(hd->info.name, "M525", 4);
        break;
    case DEVTYPE_TOUCHPAD:
        /* TODO: perhaps "unpair" mouse and use this touchpad instead? */
        hd->hid = &s->hid[IFACE_MSE];
        hd->info.report_types = 0x0e; /* Keyboard, Mouse, Consumer Ctrl */
        hd->info.wireless_pid = 0x4026;
        hd->info.serial = 0x4f4f4f48;
        memcpy(hd->info.name, "T650", 4);
        break;
    }
    hd->info.protocol_version = 0x0200; /* HID++ 2.0 */
    hd->mode = LTUNIFY_MODE_HID;
    hd->powered_on = true;
}

void hidpp_reset(USBLtunifyState *s)
{
    int i;

    for (i = 0; i < MAX_DEVICES; i++) {
        LHidDevice *hd = &s->devices[i];
        if (hd->info.device_type) {
            hidpp_init_device(s, i + 1, hd->info.device_type);
        }
    }
}

void hidpp_init(USBLtunifyState *s)
{
    LHidReceiver *r = &s->receiver;
    r->info.serial = 0x4c4f5354;

    hidpp_init_device(s, 1, DEVTYPE_KEYBOARD);
    hidpp_init_device(s, 2, DEVTYPE_MOUSE);
}