aboutsummaryrefslogtreecommitdiffstats
path: root/examples/baremetal/cy7c68013a/nes-gamepads/hiddesc.c
blob: 74601f249254425eaa7d8e80b8c7cdbcb3cca9da (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
/****************************************************************************
**
** Copyright (C) 2019 Denis Shienkov <denis.shienkov@gmail.com>
** Contact: https://www.qt.io/licensing/
**
** This file is part of the examples of Qbs.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**   * Redistributions of source code must retain the above copyright
**     notice, this list of conditions and the following disclaimer.
**   * Redistributions in binary form must reproduce the above copyright
**     notice, this list of conditions and the following disclaimer in
**     the documentation and/or other materials provided with the
**     distribution.
**   * Neither the name of The Qt Company Ltd nor the names of its
**     contributors may be used to endorse or promote products derived
**     from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "hid.h"

enum usb_bcd_version {
    USB_SPEC_BCD_VERSION = 0x0200,
    USB_DEVICE_BCD_VERSION = 0x1234,
    USB_HID_BCD_VERSION = 0x0111
};

enum usb_ids {
    USB_DEVICE_VID = 0xFFFF,
    USB_DEVICE_PID = 0xFFFF
};

enum usb_lang_id {
    USB_LANG_ID = 0x0409
};

enum usb_descriptor_string_index {
    USB_DESC_LANGID_STRING_INDEX = 0,
    USB_DESC_MFG_STRING_INDEX,
    USB_DESC_PRODUCT_STRING_INDEX,
    USB_DESC_SERIAL_STRING_INDEX
};

enum usb_descr_length {
    // Standard length.
    USB_DESC_DEVICE_LEN = 18,
    USB_DESC_DEVICE_QUAL_LEN = 10,
    USB_DESC_CONF_LEN = 9,
    USB_DESC_OTHER_SPEED_CONF_LEN = 9,
    USB_DESC_INTERFACE_LEN = 9,
    USB_DESC_HID_LEN = 9,
    USB_DESCR_HID_REP_LEN = 56,
    USB_DESC_ENDPOINT_LEN = 7,

    // Total length.
    USB_DESC_DEVICE_TOTAL_LEN = USB_DESC_DEVICE_LEN,
    USB_DESC_DEVICE_QUAL_TOTAL_LEN = USB_DESC_DEVICE_QUAL_LEN,

    USB_DESC_CONF_TOTAL_LEN = USB_DESC_CONF_LEN
        + USB_DESC_INTERFACE_LEN
        + USB_DESC_HID_LEN + USB_DESC_ENDPOINT_LEN,

    USB_DESC_OTHER_SPEED_CONF_TOTAL_LEN = USB_DESC_OTHER_SPEED_CONF_LEN
        + USB_DESC_INTERFACE_LEN,
};

enum usb_descr_attributes {
    // Attributes (b7 - buspwr, b6 - selfpwr, b5 - rwu).
    USB_DESC_ATTRIBUTES = 0xA0,
    // 100 mA (div 2).
    USB_DESC_POWER_CONSUMPTION = 50
};

enum usb_descr_numbers {
    USB_DESC_CONFIG_COUNT = 1
};

static const BYTE CODE
g_hid_report_desc[USB_DESCR_HID_REP_LEN] = {
    // Pad #1 configuration.
    0x05, 0x01, // Usage page (Generic Desktop).
    0x09, 0x05, // Usage (Game Pad).
    0xa1, 0x01, // Collection (Application).
    0xa1, 0x00, //   Collection (Physical).
    0x85, HID_REPORT_ID_GAMEPAD1, // Report id (1).
    0x05, 0x09, //     Usage page (Button).
    0x19, 0x01, //     Usage minimum (Button 1).
    0x29, 0x08, //     Usage maximum (Button 8).
    0x15, 0x00, //     Logical minimum (0).
    0x25, 0x01, //     Logical maximum (1).
    0x95, HID_REPORT_BITS_COUNT, // Report count (8).
    0x75, 0x01, //     Report size (1).
    0x81, 0x02, //     Input (Data,Var,Abs).
    0xc0,       //   End collection.
    0xc0,       // End collection.
    // Pad #2 configuration.
    0x05, 0x01, // Usage page (Generic Desktop).
    0x09, 0x05, // Usage (Game Pad).
    0xa1, 0x01, // Collection (Application).
    0xa1, 0x00, //   Collection (Physical).
    0x85, HID_REPORT_ID_GAMEPAD2, // Report id (2).
    0x05, 0x09, //     Usage page (Button).
    0x19, 0x01, //     Usage minimum (Button 1).
    0x29, 0x08, //     Usage maximum (Button 8).
    0x15, 0x00, //     Logical minimum (0).
    0x25, 0x01, //     Logical maximum (1).
    0x95, HID_REPORT_BITS_COUNT, // Report count (8).
    0x75, 0x01, //     Report size (1).
    0x81, 0x02, //     Input (Data,Var,Abs).
    0xc0,       //   End collection
    0xc0        // End collection.
};

static const BYTE CODE
g_device_desc[USB_DESC_DEVICE_TOTAL_LEN] = {
    USB_DESC_DEVICE_LEN, // Descriptor length.
    USB_DESC_DEVICE, // Descriptor type.
    usb_word_lsb_get(USB_SPEC_BCD_VERSION), // USB BCD version, lo.
    usb_word_msb_get(USB_SPEC_BCD_VERSION), // USB BCD version, hi.
    0x00, // Device class.
    0x00, // Device sub-class.
    0x00, // Device protocol.
    MAX_EP0_SIZE, // Maximum packet size (ep0 size).
    usb_word_lsb_get(USB_DEVICE_VID), // Vendor ID, lo.
    usb_word_msb_get(USB_DEVICE_VID), // Vendor ID, hi.
    usb_word_lsb_get(USB_DEVICE_PID), // Product ID, lo.
    usb_word_msb_get(USB_DEVICE_PID), // Product ID, hi.
    usb_word_lsb_get(USB_DEVICE_BCD_VERSION), // Device BCD version, lo.
    usb_word_msb_get(USB_DEVICE_BCD_VERSION), // Device BCD version, hi.
    USB_DESC_MFG_STRING_INDEX,
    USB_DESC_PRODUCT_STRING_INDEX,
    USB_DESC_SERIAL_STRING_INDEX,
    USB_DESC_CONFIG_COUNT // Configurations count.
};

static const BYTE CODE
g_device_qual_desc[USB_DESC_DEVICE_QUAL_TOTAL_LEN] = {
    USB_DESC_DEVICE_QUAL_LEN, // Descriptor length.
    USB_DESC_DEVICE_QUAL, // Descriptor type.
    usb_word_lsb_get(USB_SPEC_BCD_VERSION), // USB BCD version, lo.
    usb_word_msb_get(USB_SPEC_BCD_VERSION), // USB BCD version, hi.
    0x00, // Device class.
    0x00, // Device sub-class.
    0x00, // Device protocol.
    MAX_EP0_SIZE, // Maximum packet size (ep0 size).
    USB_DESC_CONFIG_COUNT, // Configurations count.
    0x00 // Reserved.
};

static const BYTE CODE
g_config_desc[USB_DESC_CONF_TOTAL_LEN] = {
    USB_DESC_CONF_LEN, // Descriptor length.
    USB_DESC_CONF, // Descriptor type.
    usb_word_lsb_get(USB_DESC_CONF_TOTAL_LEN), // Total length, lo.
    usb_word_msb_get(USB_DESC_CONF_TOTAL_LEN), // Total length, hi.
    0x01, // Interfaces count.
    HID_CONFIG_NUMBER, // Configuration number.
    0x00, // Configuration string index.
    USB_DESC_ATTRIBUTES, // Attributes.
    USB_DESC_POWER_CONSUMPTION, // Power consumption.

    // Interface descriptor.
    USB_DESC_INTERFACE_LEN, // Descriptor length.
    USB_DESC_INTERFACE, // Descriptor type.
    HID_IFACE_NUMBER, // Zero-based index of this interfacce.
    HID_ALT_IFACE_NUMBER, // Alternate setting.
    0x01, // End points count (ep1 in).
    0x03, // Class code (HID).
    0x00, // Subclass code (boot).
    0x00, // Protocol code (none).
    0x00, // Interface string descriptor index.

    // HID descriptor.
    USB_DESC_HID_LEN, // Descriptor length.
    USB_DESC_HID, // Descriptor type.
    usb_word_lsb_get(USB_HID_BCD_VERSION), // HID class BCD version, lo.
    usb_word_msb_get(USB_HID_BCD_VERSION), // HID class BCD version, hi.
    0x00, // Country code (HW country code).
    0x01, // Number of HID class descriptors to follow.
    USB_DESC_REPORT, // Repord descriptor type (HID).
    usb_word_lsb_get(USB_DESCR_HID_REP_LEN), // Report descriptor total length, lo.
    usb_word_msb_get(USB_DESCR_HID_REP_LEN), // Report descriptor total length, hi.

    // End point descriptor.
    USB_DESC_ENDPOINT_LEN, // Descriptor length.
    USB_DESC_ENDPOINT, // Descriptor type.
    HID_EP_IN, // End point address (ep1 in).
    0x03, // End point type (interrupt).
    usb_word_lsb_get(MAX_EP1_SIZE), // Maximum packet size, lo (ep1 size).
    usb_word_msb_get(MAX_EP1_SIZE), // Maximum packet size, hi (ep1 size).
    0x01 // Polling interval (1 ms).
};

static const BYTE CODE
g_other_config_desc[USB_DESC_OTHER_SPEED_CONF_TOTAL_LEN] = {
    USB_DESC_OTHER_SPEED_CONF_LEN, // Descriptor length.
    USB_DESC_OTHER_SPEED_CONF, // Descriptor type.
    usb_word_lsb_get(USB_DESC_OTHER_SPEED_CONF_TOTAL_LEN), // Total length, lo.
    usb_word_msb_get(USB_DESC_OTHER_SPEED_CONF_TOTAL_LEN), // Total length, hi.
    0x01, // Interfaces number.
    0x01, // Configuration number.
    0x00, // Configuration string index.
    USB_DESC_ATTRIBUTES, // Attributes.
    USB_DESC_POWER_CONSUMPTION, // Power consumption.

    // Interface descriptor.
    USB_DESC_INTERFACE_LEN, // Descriptor length.
    USB_DESC_INTERFACE, // Descriptor type.
    0x00, // Zero-based index of this interfacce.
    0x00, // Alternate setting.
    0x00, // End points number (only ep0).
    0x00, // Class code.
    0x00, // Subclass code.USB_DESC_OTHER_SPEED_CONFIGURATION
    0x00, // Protocol code.
    0x00 // Interface string descriptor index.
};

static const BYTE CODE
g_lang_id_desc[] = {
    0x04, // Descriptor length.
    USB_DESC_STRING, // Descriptor type.
    usb_word_lsb_get(USB_LANG_ID), // Language id, lo.
    usb_word_msb_get(USB_LANG_ID) // Language id, hi.
};

static const BYTE CODE
g_manuf_str_desc[] = {
    0x18, // Descriptor length.
    USB_DESC_STRING, // Descriptor type.
    // Unicode string:
    'Q', 0, 'B', 0, 'S', 0, ' ', 0,
    'e', 0, 'x', 0, 'a', 0, 'm', 0, 'p', 0, 'l', 0, 'e', 0
};

static const BYTE CODE
g_product_str_desc[] = {
    0x1A, // Descriptor length.
    USB_DESC_STRING, // Descriptor type.
    // Unicode string:
    'N', 0, 'E', 0, 'S', 0, ' ', 0,
    'G', 0, 'a', 0, 'm', 0, 'e', 0,
    'P', 0, 'a', 0, 'd', 0, 's', 0
};

static const BYTE CODE
g_serialno_str_desc[] = {
    0x1A, // Descriptor length.
    USB_DESC_STRING, // Descriptor type.
    // Unicode string:
    '0', 0 ,'1', 0, '0', 0, '2', 0, '0', 0, '3', 0,
    '0', 0, '4', 0, '0', 0, '5', 0, '0', 0, '6', 0
};

static const BYTE CODE *ep0_string_desc_get(void)
{
    switch (SETUPDAT[2]) {
    case USB_DESC_LANGID_STRING_INDEX:
        return g_lang_id_desc;
    case USB_DESC_MFG_STRING_INDEX:
        return g_manuf_str_desc;
    case USB_DESC_PRODUCT_STRING_INDEX:
        return g_product_str_desc;
    case USB_DESC_SERIAL_STRING_INDEX:
        return g_serialno_str_desc;
    default:
        break;
    }

    return NULL;
}

static const BYTE CODE *ep0_config_desc_get(BOOL other)
{
    if (!other) {
        if (SETUPDAT[2] == 0) {
            return usb_is_high_speed() ? g_config_desc
                                       : g_other_config_desc;
        }
    } else {
        return usb_is_high_speed() ? g_other_config_desc
                                   : g_config_desc;
    }

    return NULL;
}

const BYTE CODE *hid_ep0_std_desc_get(void)
{
    switch (SETUPDAT[3]) {
    case USB_DESC_DEVICE:
        return g_device_desc;
    case USB_DESC_CONF:
        return ep0_config_desc_get(FALSE);
    case USB_DESC_STRING:
        return ep0_string_desc_get();
    case USB_DESC_DEVICE_QUAL:
        return g_device_qual_desc;
    case USB_DESC_OTHER_SPEED_CONF:
        return ep0_config_desc_get(TRUE);
    case USB_DESC_HID:
        return &g_config_desc[USB_DESC_CONF_LEN
                + USB_DESC_INTERFACE_LEN];
    default:
        break;
    }

    return NULL;
}

const BYTE CODE *hid_ep0_report_desc_get(WORD *length)
{
    *length = sizeof(g_hid_report_desc);
    return g_hid_report_desc;
}