aboutsummaryrefslogtreecommitdiff
path: root/tmk_core/protocol/chibios/usb_report_handling.c
blob: 64074b21642fd7725100f6ee8b769627a4ffd68d (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
// Copyright 2023 Stefan Kerkmann (@KarlK90)
// SPDX-License-Identifier: GPL-3.0-or-later

#include <string.h>
#include <stdint.h>
#include <stdbool.h>

#include "usb_report_handling.h"
#include "usb_endpoints.h"
#include "usb_main.h"
#include "usb_types.h"
#include "usb_driver.h"
#include "report.h"

extern usb_endpoint_in_t     usb_endpoints_in[USB_ENDPOINT_IN_COUNT];
extern usb_endpoint_in_lut_t usb_endpoint_interface_lut[TOTAL_INTERFACES];

void usb_set_report(usb_fs_report_t **reports, const uint8_t *data, size_t length) {
    if (*reports == NULL) {
        return;
    }

    (*reports)->last_report = chVTGetSystemTimeX();
    (*reports)->length      = length;
    memcpy(&(*reports)->data, data, length);
}

void usb_get_report(usb_fs_report_t **reports, uint8_t report_id, usb_fs_report_t *report) {
    (void)report_id;
    if (*reports == NULL) {
        return;
    }

    report->length = (*reports)->length;
    memcpy(&report->data, &(*reports)->data, report->length);
}

void usb_reset_report(usb_fs_report_t **reports) {
    if (*reports == NULL) {
        return;
    }

    memset(&(*reports)->data, 0, (*reports)->length);
    (*reports)->idle_rate   = 0;
    (*reports)->last_report = 0;
}

void usb_shared_set_report(usb_fs_report_t **reports, const uint8_t *data, size_t length) {
    uint8_t report_id = data[0];

    if (report_id > REPORT_ID_COUNT || reports[report_id] == NULL) {
        return;
    }

    reports[report_id]->last_report = chVTGetSystemTimeX();
    reports[report_id]->length      = length;
    memcpy(&reports[report_id]->data, data, length);
}

void usb_shared_get_report(usb_fs_report_t **reports, uint8_t report_id, usb_fs_report_t *report) {
    if (report_id > REPORT_ID_COUNT || reports[report_id] == NULL) {
        return;
    }

    report->length = reports[report_id]->length;
    memcpy(&report->data, &reports[report_id]->data, report->length);
}

void usb_shared_reset_report(usb_fs_report_t **reports) {
    for (int i = 0; i <= REPORT_ID_COUNT; i++) {
        if (reports[i] == NULL) {
            continue;
        }
        memset(&reports[i]->data, 0, reports[i]->length);
        reports[i]->idle_rate   = 0;
        reports[i]->last_report = 0;
    }
}

bool usb_get_report_cb(USBDriver *driver) {
    usb_control_request_t *setup     = (usb_control_request_t *)driver->setup;
    uint8_t                interface = setup->wIndex;
    uint8_t                report_id = setup->wValue.lbyte;

    static usb_fs_report_t report;

    if (!IS_VALID_INTERFACE(interface) || !IS_VALID_REPORT_ID(report_id)) {
        return false;
    }

    usb_endpoint_in_lut_t ep = usb_endpoint_interface_lut[interface];

    if (!IS_VALID_USB_ENDPOINT_IN_LUT(ep)) {
        return false;
    }

    usb_report_storage_t *report_storage = usb_endpoints_in[ep].report_storage;

    if (report_storage == NULL) {
        return false;
    }

    report_storage->get_report(report_storage->reports, report_id, &report);

    usbSetupTransfer(driver, (uint8_t *)report.data, report.length, NULL);

    return true;
}

static bool run_idle_task = false;

void usb_set_idle_rate(usb_fs_report_t **reports, uint8_t report_id, uint8_t idle_rate) {
    (void)report_id;

    if (*reports == NULL) {
        return;
    }

    (*reports)->idle_rate = idle_rate * 4;

    run_idle_task |= idle_rate != 0;
}

uint8_t usb_get_idle_rate(usb_fs_report_t **reports, uint8_t report_id) {
    (void)report_id;

    if (*reports == NULL) {
        return 0;
    }

    return (*reports)->idle_rate / 4;
}

bool usb_idle_timer_elapsed(usb_fs_report_t **reports, uint8_t report_id) {
    (void)report_id;

    if (*reports == NULL) {
        return false;
    }

    osalSysLock();
    time_msecs_t idle_rate   = (*reports)->idle_rate;
    systime_t    last_report = (*reports)->last_report;
    osalSysUnlock();

    if (idle_rate == 0) {
        return false;
    }

    return chTimeI2MS(chVTTimeElapsedSinceX(last_report)) >= idle_rate;
}

void usb_shared_set_idle_rate(usb_fs_report_t **reports, uint8_t report_id, uint8_t idle_rate) {
    // USB spec demands that a report_id of 0 would set the idle rate for all
    // reports of that endpoint, but this can easily lead to resource
    // exhaustion, therefore we deliberalty break the spec at this point.
    if (report_id == 0 || report_id > REPORT_ID_COUNT || reports[report_id] == NULL) {
        return;
    }

    reports[report_id]->idle_rate = idle_rate * 4;

    run_idle_task |= idle_rate != 0;
}

uint8_t usb_shared_get_idle_rate(usb_fs_report_t **reports, uint8_t report_id) {
    if (report_id > REPORT_ID_COUNT || reports[report_id] == NULL) {
        return 0;
    }

    return reports[report_id]->idle_rate / 4;
}

bool usb_shared_idle_timer_elapsed(usb_fs_report_t **reports, uint8_t report_id) {
    if (report_id > REPORT_ID_COUNT || reports[report_id] == NULL) {
        return false;
    }

    osalSysLock();
    time_msecs_t idle_rate   = reports[report_id]->idle_rate;
    systime_t    last_report = reports[report_id]->last_report;
    osalSysUnlock();

    if (idle_rate == 0) {
        return false;
    }

    return chTimeI2MS(chVTTimeElapsedSinceX(last_report)) >= idle_rate;
}

void usb_idle_task(void) {
    if (!run_idle_task) {
        return;
    }

    static usb_fs_report_t report;
    bool                   non_zero_idle_rate_found = false;

    for (int ep = 0; ep < USB_ENDPOINT_IN_COUNT; ep++) {
        usb_report_storage_t *report_storage = usb_endpoints_in[ep].report_storage;

        if (report_storage == NULL) {
            continue;
        }

#if defined(SHARED_EP_ENABLE)
        if (ep == USB_ENDPOINT_IN_SHARED) {
            for (int report_id = 1; report_id <= REPORT_ID_COUNT; report_id++) {
                osalSysLock();
                non_zero_idle_rate_found |= report_storage->get_idle(report_storage->reports, report_id) != 0;
                osalSysUnlock();

                if (usb_endpoint_in_is_inactive(&usb_endpoints_in[ep]) && report_storage->idle_timer_elasped(report_storage->reports, report_id)) {
                    osalSysLock();
                    report_storage->get_report(report_storage->reports, report_id, &report);
                    osalSysUnlock();
                    send_report(ep, &report.data, report.length);
                }
            }
            continue;
        }
#endif

        osalSysLock();
        non_zero_idle_rate_found |= report_storage->get_idle(report_storage->reports, 0) != 0;
        osalSysUnlock();

        if (usb_endpoint_in_is_inactive(&usb_endpoints_in[ep]) && report_storage->idle_timer_elasped(report_storage->reports, 0)) {
            osalSysLock();
            report_storage->get_report(report_storage->reports, 0, &report);
            osalSysUnlock();
            send_report(ep, &report.data, report.length);
        }
    }

    run_idle_task = non_zero_idle_rate_found;
}

bool usb_get_idle_cb(USBDriver *driver) {
    usb_control_request_t *setup     = (usb_control_request_t *)driver->setup;
    uint8_t                interface = setup->wIndex;
    uint8_t                report_id = setup->wValue.lbyte;

    static uint8_t _Alignas(4) idle_rate;

    if (!IS_VALID_INTERFACE(interface) || !IS_VALID_REPORT_ID(report_id)) {
        return false;
    }

    usb_endpoint_in_lut_t ep = usb_endpoint_interface_lut[interface];

    if (!IS_VALID_USB_ENDPOINT_IN_LUT(ep)) {
        return false;
    }

    usb_report_storage_t *report_storage = usb_endpoints_in[ep].report_storage;

    if (report_storage == NULL) {
        return false;
    }

    idle_rate = report_storage->get_idle(report_storage->reports, report_id);

    usbSetupTransfer(driver, &idle_rate, 1, NULL);

    return true;
}

bool usb_set_idle_cb(USBDriver *driver) {
    usb_control_request_t *setup     = (usb_control_request_t *)driver->setup;
    uint8_t                interface = setup->wIndex;
    uint8_t                report_id = setup->wValue.lbyte;
    uint8_t                idle_rate = setup->wValue.hbyte;

    if (!IS_VALID_INTERFACE(interface) || !IS_VALID_REPORT_ID(report_id)) {
        return false;
    }

    usb_endpoint_in_lut_t ep = usb_endpoint_interface_lut[interface];

    if (!IS_VALID_USB_ENDPOINT_IN_LUT(ep)) {
        return false;
    }

    usb_report_storage_t *report_storage = usb_endpoints_in[ep].report_storage;

    if (report_storage == NULL) {
        return false;
    }

    report_storage->set_idle(report_storage->reports, report_id, idle_rate);

    usbSetupTransfer(driver, NULL, 0, NULL);

    return true;
}