-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDescr.h
190 lines (151 loc) · 4.78 KB
/
Descr.h
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
//------------------------------------------------------------------------------
// Copyright (c) 2024 John D. Haughton
//
// 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.
//------------------------------------------------------------------------------
// \brief USB descriptors
#pragma once
#include <cstdint>
#include "STB/List.h"
namespace USB {
// Device and interface class codes
enum Class : uint8_t
{
CLASS_INFO_IN_IFC = 0x00,
CLASS_AUDIO = 0x01,
CLASS_COMMS = 0x02,
CLASS_HID = 0x03,
CLASS_MASS_STORAGE = 0x08,
CLASS_VENDOR_SPEC = 0xFF
};
// Got to use something!
static const uint16_t DUMMY_VENDOR_ID = 0xC0DE;
// Descriptor type codes
enum DescrType : uint8_t
{
TYPE_DEVICE = 0x01,
TYPE_CONFIG = 0x02,
TYPE_STRING = 0x03,
TYPE_INTERFACE = 0x04,
TYPE_ENDPOINT = 0x05,
TYPE_CS_INTERFACE = 0x24,
TYPE_CS_ENDPOINT = 0x25
};
//! Base class for list element descriptors
class Descr : public STB::List<Descr>::Elem
{
public:
using List = STB::List<Descr>;
Descr() = default;
Descr(const uint8_t& length_)
: raw(&length_)
{
}
Descr(List& list_, const uint8_t& length_)
: raw(&length_)
{
list_.push_back(this);
}
//! Set option implementation pointer
void setImpl(void* impl_) { impl = impl_; }
//! Returns pointer to first byte in descriptor
const uint8_t* getRaw() const { return raw; }
//! Returns length of descriptor as a reference
const uint8_t& getLength() const { return raw[0]; }
//! Returns type of descriptor
DescrType getType() const { return DescrType(raw[1]); }
void* getImpl() const { return impl; }
private:
const uint8_t* raw{};
void* impl{nullptr};
};
struct DeviceDescr
{
DeviceDescr() = default;
uint8_t length{0x12};
uint8_t type{TYPE_DEVICE};
uint16_t usb_bcd{0x200}; // USB 2.0
uint8_t clas{CLASS_INFO_IN_IFC};
uint8_t sub_class{0};
uint8_t protocol{0};
uint8_t max_packet_size0{64};
uint16_t vendor_id{DUMMY_VENDOR_ID};
uint16_t product_id{0x0001};
uint16_t device_bcd{0x0100}; // 1.0
uint8_t vendor_idx{0};
uint8_t product_idx{0};
uint8_t serial_num_idx{0};
uint8_t num_configs{0};
} __attribute__((__packed__));
struct ConfigDescr
{
ConfigDescr() = default;
uint8_t length{9};
uint8_t type{TYPE_CONFIG};
uint16_t total_length{0};
uint8_t num_interfaces{0};
uint8_t value{1};
uint8_t name_idx{0};
uint8_t attributes{0b10100000};
uint8_t max_power{50};
} __attribute__((__packed__));
struct InterfaceDescr : public Descr
{
InterfaceDescr() = default;
InterfaceDescr(List& list_, uint8_t class_, uint8_t sub_class_, uint8_t protocol_ = 0)
: Descr(list_, length)
, clas(class_)
, sub_class(sub_class_)
, protocol(protocol_)
{
}
uint8_t length{9};
uint8_t type{TYPE_INTERFACE};
uint8_t number{0};
uint8_t alternate_setting{0};
uint8_t num_endpoints{0};
uint8_t clas{CLASS_VENDOR_SPEC};
uint8_t sub_class{0};
uint8_t protocol{0};
uint8_t name_idx{0};
} __attribute__((__packed__));
struct EndPointDescr : public Descr
{
// Direction
static const uint8_t OUT = 0b00000000;
static const uint8_t IN = 0b10000000;
// Type
static const uint8_t CONTROL = 0b00;
static const uint8_t ISOCHrONOUS = 0b00;
static const uint8_t BULK = 0b10;
static const uint8_t INTERRUPt = 0b11;
EndPointDescr(List& list_, uint8_t dir_, uint8_t type_)
: Descr(list_, length)
, addr(dir_)
, attr(uint8_t(type_))
{
}
uint8_t length{7};
uint8_t type{TYPE_ENDPOINT};
uint8_t addr{0};
uint8_t attr{0};
uint16_t max_packet_size{64};
uint8_t interval{0};
} __attribute__((__packed__));
} // namespace USB