blob: 5289b78f882604faea79206632602f9b57c6d58c [file] [log] [blame]
Brian Silvermaneda63f32017-10-08 18:57:33 -04001#include "motors/usb/cdc.h"
2
3#include <string.h>
Brian Silverman69f96c22017-11-01 02:54:02 -04004#include <stdint.h>
5
6#include "motors/core/time.h"
Brian Silvermaneda63f32017-10-08 18:57:33 -04007
8#define CHECK(c) \
9 do { \
10 if (!(c)) { \
11 while (true) { \
12 for (int i = 0; i < 10000000; ++i) { \
13 GPIOC_PSOR = 1 << 5; \
14 } \
15 for (int i = 0; i < 10000000; ++i) { \
16 GPIOC_PCOR = 1 << 5; \
17 } \
18 } \
19 } \
20 } while (false)
21
22namespace frc971 {
23namespace teensy {
24namespace {
25
26// Aka the Communications Device Class code, the Communications Class code,
27// and the Communications Interface Class code.
28constexpr uint8_t communications_class() { return 0x02; }
29constexpr uint8_t data_interface_class() { return 0x0A; }
30
31namespace cdc_descriptor_subtype {
32constexpr uint8_t header() { return 0x00; }
33constexpr uint8_t header_length() { return 5; }
34constexpr uint8_t call_management() { return 0x01; }
35constexpr uint8_t call_management_length() { return 5; }
36constexpr uint8_t abstract_control_management() { return 0x02; }
37constexpr uint8_t abstract_control_management_length() { return 4; }
38constexpr uint8_t direct_line_management() { return 0x03; }
39constexpr uint8_t telephone_ringer() { return 0x04; }
40constexpr uint8_t telephone_call_etc() { return 0x05; }
41// Can't just call this "union" because that's a keyword...
42constexpr uint8_t union_function() { return 0x06; }
43constexpr uint8_t union_length(int number_subordinates) {
44 return 4 + number_subordinates;
45}
46constexpr uint8_t country_selection() { return 0x07; }
47constexpr uint8_t telephone_operational_modes() { return 0x08; }
48constexpr uint8_t usb_terminal() { return 0x09; }
49constexpr uint8_t network_channel() { return 0x0A; }
50constexpr uint8_t protocol_unit() { return 0x0B; }
51constexpr uint8_t extension_unit() { return 0x0C; }
52constexpr uint8_t multichannel_management() { return 0x0D; }
53constexpr uint8_t capi_control() { return 0x0E; }
54constexpr uint8_t ethernet_networking() { return 0x0F; }
55constexpr uint8_t atm_networking() { return 0x10; }
56constexpr uint8_t wireless_handset_control() { return 0x11; }
57constexpr uint8_t mobile_direct_line() { return 0x12; }
58constexpr uint8_t mdlm_detail() { return 0x13; }
59constexpr uint8_t device_management() { return 0x14; }
60constexpr uint8_t obex() { return 0x15; }
61constexpr uint8_t command_set() { return 0x16; }
62constexpr uint8_t command_set_detail() { return 0x17; }
63constexpr uint8_t telephone_control() { return 0x18; }
64constexpr uint8_t obex_service() { return 0x19; }
65constexpr uint8_t ncm() { return 0x1A; }
66} // namespace cdc_descriptor_subtype
67
68namespace communications_subclass {
69constexpr uint8_t direct_line_control_model() { return 0x01; }
70constexpr uint8_t abstract_control_model() { return 0x02; }
71constexpr uint8_t telephone_control_model() { return 0x03; }
72constexpr uint8_t multichannel_control_model() { return 0x04; }
73constexpr uint8_t capi_control_model() { return 0x05; }
74constexpr uint8_t ethernet_networking_control_model() { return 0x06; }
75constexpr uint8_t atm_networking_control_model() { return 0x07; }
76constexpr uint8_t wireless_handset_control_model() { return 0x08; }
77constexpr uint8_t device_management() { return 0x09; }
78constexpr uint8_t mobile_direct_line_model() { return 0x0A; }
79constexpr uint8_t obex() { return 0x0B; }
80constexpr uint8_t ethernet_emulation_model() { return 0x0C; }
81constexpr uint8_t network_control_model() { return 0x0D; }
82} // namespace communications_subclass
83
84namespace cdc_class_requests {
85constexpr uint8_t send_encapsulated_command() { return 0x00; }
86constexpr uint8_t get_encapsulated_response() { return 0x01; }
87constexpr uint8_t set_comm_feature() { return 0x02; }
88constexpr uint8_t get_comm_feature() { return 0x03; }
89constexpr uint8_t clear_comm_feature() { return 0x04; }
90constexpr uint8_t set_aux_line_state() { return 0x10; }
91constexpr uint8_t set_hook_state() { return 0x11; }
92constexpr uint8_t pulse_setup() { return 0x12; }
93constexpr uint8_t send_pulse() { return 0x13; }
94constexpr uint8_t set_pulse_time() { return 0x14; }
95constexpr uint8_t ring_aux_jack() { return 0x15; }
96constexpr uint8_t set_line_coding() { return 0x20; }
97constexpr uint8_t get_line_coding() { return 0x21; }
98constexpr uint8_t set_control_line_state() { return 0x22; }
99constexpr uint8_t send_break() { return 0x23; }
100constexpr uint8_t set_ringer_parms() { return 0x30; }
101constexpr uint8_t get_ringer_parms() { return 0x31; }
102constexpr uint8_t set_operation_parms() { return 0x32; }
103constexpr uint8_t get_operation_parms() { return 0x33; }
104constexpr uint8_t set_line_parms() { return 0x34; }
105constexpr uint8_t get_line_parms() { return 0x35; }
106constexpr uint8_t dial_digits() { return 0x36; }
107constexpr uint8_t set_unit_parameter() { return 0x37; }
108constexpr uint8_t get_unit_parameter() { return 0x38; }
109constexpr uint8_t clear_unit_parameter() { return 0x39; }
110constexpr uint8_t get_profile() { return 0x3A; }
111} // namespace cdc_class_requests
112
113} // namespace
114
115void AcmTty::Initialize() {
116 status_interface_ = AddInterface();
117 data_interface_ = AddInterface();
118 status_endpoint_ = AddEndpoint();
119 data_tx_endpoint_ = AddEndpoint();
120 data_rx_endpoint_ = AddEndpoint();
121
122 {
123 const auto iad_descriptor = CreateDescriptor(
124 iad_descriptor_length(), UsbDescriptorType::kInterfaceAssociation);
125 iad_descriptor->AddByte(status_interface_); // bFirstInterface
126 iad_descriptor->AddByte(2); // bInterfaceCount
127 iad_descriptor->AddByte(communications_class()); // bFunctionClass
128 iad_descriptor->AddByte(communications_subclass::
129 abstract_control_model()); // bFunctionSubClass
130 iad_descriptor->AddByte(0); // bFunctionProtocol
131 iad_descriptor->AddByte(device()->AddString("UsbTty")); // iFunction
132 }
133
134 {
135 const auto interface_descriptor = CreateDescriptor(
136 interface_descriptor_length(), UsbDescriptorType::kInterface);
137 interface_descriptor->AddByte(status_interface_); // bInterfaceNumber
138 interface_descriptor->AddByte(0); // bAlternateSetting
139 interface_descriptor->AddByte(1); // bNumEndpoints
140 interface_descriptor->AddByte(communications_class()); // bInterfaceClass
141 interface_descriptor->AddByte(
142 communications_subclass::
143 abstract_control_model()); // bInterfaceSubClass
144 interface_descriptor->AddByte(0); // bInterfaceProtocol
145 interface_descriptor->AddByte(
146 device()->AddString("UsbTty.status")); // iInterface
147 }
148
149 {
150 const auto cdc_header =
151 CreateDescriptor(cdc_descriptor_subtype::header_length(),
152 UsbClassDescriptorType::kInterface);
153 cdc_header->AddByte(
154 cdc_descriptor_subtype::header()); // bDescriptorSubtype
155 cdc_header->AddUint16(0x0110); // bcdCDC
156 }
157
158 {
159 const auto call_management =
160 CreateDescriptor(cdc_descriptor_subtype::call_management_length(),
161 UsbClassDescriptorType::kInterface);
162 call_management->AddByte(
163 cdc_descriptor_subtype::call_management()); // bDescriptorSubtype
164 // We don't do call management.
165 call_management->AddByte(0); // bmCapabilities
166 call_management->AddByte(data_interface_); // bDataInterface
167 }
168
169 {
170 const auto abstract_control_management =
171 CreateDescriptor(cdc_descriptor_subtype::abstract_control_management_length(),
172 UsbClassDescriptorType::kInterface);
173 abstract_control_management->AddByte(
174 cdc_descriptor_subtype::abstract_control_management()); // bDescriptorSubtype
175 // We support:
176 // line_coding and serial_state
177 // send_break
178 // We don't support:
179 // comm_feature
180 // network_notification
181 abstract_control_management->AddByte(6); // bmCapabilities
182 }
183
184 {
185 const auto cdc_union_descriptor =
186 CreateDescriptor(cdc_descriptor_subtype::union_length(1),
187 UsbClassDescriptorType::kInterface);
188 cdc_union_descriptor->AddByte(
189 cdc_descriptor_subtype::union_function()); // bDescriptorSubtype
190 cdc_union_descriptor->AddByte(status_interface_); // bMasterInterface
191 cdc_union_descriptor->AddByte(data_interface_); // bSlaveInterface
192 }
193
194 {
195 const auto endpoint_descriptor = CreateDescriptor(
196 endpoint_descriptor_length(), UsbDescriptorType::kEndpoint);
197 endpoint_descriptor->AddByte(status_endpoint_ |
198 m_endpoint_address_in()); // bEndpointAddress
199 endpoint_descriptor->AddByte(
200 m_endpoint_attributes_interrupt()); // bmAttributes
201 endpoint_descriptor->AddUint16(kStatusMaxPacketSize); // wMaxEndpointSize
202 // Set it to the max because we have nothing to send, so no point using bus
203 // bandwidth asking.
204 endpoint_descriptor->AddByte(255); // bInterval
205 }
206
207 {
208 const auto interface_descriptor = CreateDescriptor(
209 interface_descriptor_length(), UsbDescriptorType::kInterface);
210 interface_descriptor->AddByte(data_interface_); // bInterfaceNumber
211 interface_descriptor->AddByte(0); // bAlternateSetting
212 interface_descriptor->AddByte(2); // bNumEndpoints
213 interface_descriptor->AddByte(data_interface_class()); // bInterfaceClass
214 interface_descriptor->AddByte(0); // bInterfaceSubClass
215 interface_descriptor->AddByte(0); // bInterfaceProtocol
216 interface_descriptor->AddByte(
217 device()->AddString("UsbTty.data")); // iInterface
218 }
219
220 // Kernel seems to think tx and rx belong in the other order, but it deals
221 // with either one. Everybody's examples seem to use this order, and the
222 // kernel deals with it, so going to go with this.
223 {
224 const auto endpoint_descriptor = CreateDescriptor(
225 endpoint_descriptor_length(), UsbDescriptorType::kEndpoint);
226 endpoint_descriptor->AddByte(data_rx_endpoint_); // bEndpointAddress
227 endpoint_descriptor->AddByte(m_endpoint_attributes_bulk()); // bmAttributes
228 endpoint_descriptor->AddUint16(kDataMaxPacketSize); // wMaxEndpointSize
229 endpoint_descriptor->AddByte(1); // bInterval
230 }
231
232 {
233 const auto endpoint_descriptor = CreateDescriptor(
234 endpoint_descriptor_length(), UsbDescriptorType::kEndpoint);
235 endpoint_descriptor->AddByte(data_tx_endpoint_ |
236 m_endpoint_address_in()); // bEndpointAddress
237 endpoint_descriptor->AddByte(m_endpoint_attributes_bulk()); // bmAttributes
238 endpoint_descriptor->AddUint16(kDataMaxPacketSize); // wMaxEndpointSize
239 endpoint_descriptor->AddByte(1); // bInterval
240 }
241}
242
243// We deliberately don't implement SendEncapsulatedCommand and
244// GetEncapsulatedResponse because there doesn't seem to be any reason for
245// anybody to send those to us, despite them being "required".
246UsbFunction::SetupResponse AcmTty::HandleEndpoint0SetupPacket(
247 const UsbDevice::SetupPacket &setup_packet) {
248 if (G_SETUP_REQUEST_TYPE_TYPE(setup_packet.request_type) !=
249 SetupRequestType::kClass) {
250 return SetupResponse::kIgnored;
251 }
252 if (G_SETUP_REQUEST_TYPE_RECIPIENT(setup_packet.request_type) !=
253 standard_setup_recipients::kInterface) {
254 return SetupResponse::kIgnored;
255 }
256 if (setup_packet.index != status_interface_) {
257 return SetupResponse::kIgnored;
258 }
259 const bool in = setup_packet.request_type & M_SETUP_REQUEST_TYPE_IN;
260 switch (setup_packet.request) {
261 case cdc_class_requests::set_line_coding():
262 if (in || setup_packet.value != 0 ||
263 setup_packet.length != sizeof(line_coding_)) {
264 return SetupResponse::kStall;
265 }
266 next_endpoint0_out_ = NextEndpoint0Out::kLineCoding;
267 return SetupResponse::kHandled;
268
269 case cdc_class_requests::get_line_coding():
270 if (!in || setup_packet.value != 0 ||
271 setup_packet.length != sizeof(line_coding_)) {
272 return SetupResponse::kStall;
273 }
274 line_coding_to_send_ = line_coding_;
275 device()->QueueEndpoint0Data(
276 reinterpret_cast<const char *>(&line_coding_to_send_),
277 setup_packet.length);
278 return SetupResponse::kHandled;
279
280 case cdc_class_requests::set_control_line_state():
281 if (in || setup_packet.length != 0) {
282 return SetupResponse::kStall;
283 }
284 control_line_state_ = setup_packet.value;
285 device()->SendEmptyEndpoint0Packet();
286 return SetupResponse::kHandled;
287
288 case cdc_class_requests::send_break():
289 if (in || setup_packet.length != 0) {
290 return SetupResponse::kStall;
291 }
292 // TODO(Brian): setup_packet.value is the length of the break in ms.
293 // 0xFFFF means keep sending break until receiving another one.
294 device()->SendEmptyEndpoint0Packet();
295 return SetupResponse::kHandled;
296 }
297 return SetupResponse::kStall;
298}
299
300UsbFunction::SetupResponse AcmTty::HandleEndpoint0OutPacket(void *data,
301 int data_length) {
302 switch (next_endpoint0_out_) {
303 case NextEndpoint0Out::kNone:
304 return SetupResponse::kIgnored;
305
306 case NextEndpoint0Out::kLineCoding:
Brian Silverman69f96c22017-11-01 02:54:02 -0400307 next_endpoint0_out_ = NextEndpoint0Out::kNone;
Brian Silvermaneda63f32017-10-08 18:57:33 -0400308 if (data_length != sizeof(line_coding_)) {
309 return SetupResponse::kStall;
310 }
311 memcpy(&line_coding_, data, data_length);
312 device()->SendEmptyEndpoint0Packet();
Brian Silverman69f96c22017-11-01 02:54:02 -0400313 // If we're supposed to reboot, then do it.
314 if (line_coding_.rate == UINT32_C(0x97101678)) {
315 // Delay for a bit so the empty IN packet gets back to the host.
316 delay(5);
317 __asm__ __volatile__("bkpt");
318 }
Brian Silvermaneda63f32017-10-08 18:57:33 -0400319 return SetupResponse::kHandled;
320
321 default:
322 return SetupResponse::kIgnored;
323 }
324}
325
326void AcmTty::HandleOutFinished(int endpoint, BdtEntry *bdt_entry) {
327 if (endpoint == data_rx_endpoint_) {
328 const size_t data_size = G_USB_BD_BC(bdt_entry->buffer_descriptor);
329 CHECK(rx_queue_.Write(static_cast<char *>(bdt_entry->address), data_size) ==
330 data_size);
331 dma_memory_barrier();
332
333 DisableInterrupts disable_interrupts;
334 // If we don't have space to handle it, don't return the entry so we'll
335 // ask the host to wait to transmit more data.
336 if (rx_queue_.space_available() >= kDataMaxPacketSize * 2) {
337 bdt_entry->buffer_descriptor = M_USB_BD_OWN | M_USB_BD_DTS |
338 V_USB_BD_BC(kDataMaxPacketSize) |
339 static_cast<uint32_t>(next_rx_toggle_);
340 next_rx_toggle_ = Data01Inverse(next_rx_toggle_);
341 } else {
342 if (first_rx_held_ == nullptr) {
343 first_rx_held_ = bdt_entry;
344 } else {
345 CHECK(second_rx_held_ == nullptr);
346 second_rx_held_ = bdt_entry;
347 }
348 }
349 }
350}
351
352void AcmTty::HandleInFinished(int endpoint, BdtEntry * /*bdt_entry*/,
353 EvenOdd odd) {
354 if (endpoint == data_tx_endpoint_) {
355 DisableInterrupts disable_interrupts;
356 CHECK(odd == BufferStateToEmpty(tx_state_));
357 tx_state_ = BufferStateAfterEmpty(tx_state_);
358 EnqueueTxData(disable_interrupts);
359 }
360}
361
362void AcmTty::HandleConfigured(int endpoint) {
363 // TODO(Brian): Handle data already in the buffers correctly.
364 if (endpoint == status_endpoint_) {
365 device()->ConfigureEndpointFor(status_endpoint_, false, true, true);
366 } else if (endpoint == data_tx_endpoint_) {
367 device()->ConfigureEndpointFor(data_tx_endpoint_, false, true, true);
368 next_tx_toggle_ = Data01::kData0;
369 DisableInterrupts disable_interrupts;
370 EnqueueTxData(disable_interrupts);
371 } else if (endpoint == data_rx_endpoint_) {
372 device()->ConfigureEndpointFor(data_rx_endpoint_, true, false, true);
373 next_rx_toggle_ = Data01::kData0;
374 device()->SetBdtEntry(
375 data_rx_endpoint_, Direction::kRx, EvenOdd::kEven,
Brian Silverman64d04272017-11-26 22:26:39 -0500376 {M_USB_BD_OWN | M_USB_BD_DTS | V_USB_BD_BC(rx_buffers_[0].size()),
377 rx_buffers_[0].data()});
Brian Silvermaneda63f32017-10-08 18:57:33 -0400378 device()->SetBdtEntry(
379 data_rx_endpoint_, Direction::kRx, EvenOdd::kOdd,
Brian Silverman64d04272017-11-26 22:26:39 -0500380 {M_USB_BD_OWN | M_USB_BD_DTS | V_USB_BD_BC(rx_buffers_[1].size()) |
Brian Silvermaneda63f32017-10-08 18:57:33 -0400381 M_USB_BD_DATA1,
Brian Silverman64d04272017-11-26 22:26:39 -0500382 rx_buffers_[1].data()});
Brian Silvermaneda63f32017-10-08 18:57:33 -0400383 }
384}
385
386size_t AcmTty::Read(void *buffer, size_t buffer_size) {
387 const size_t r = rx_queue_.Read(static_cast<char *>(buffer), buffer_size);
388
389 DisableInterrupts disable_interrupts;
390 if (rx_queue_.space_available() >= kDataMaxPacketSize * 2) {
391 if (first_rx_held_ != nullptr) {
392 first_rx_held_->buffer_descriptor =
393 M_USB_BD_OWN | M_USB_BD_DTS | V_USB_BD_BC(kDataMaxPacketSize) |
394 static_cast<uint32_t>(next_rx_toggle_);
395 next_rx_toggle_ = Data01Inverse(next_rx_toggle_);
396 }
397 if (second_rx_held_ != nullptr) {
398 second_rx_held_->buffer_descriptor =
399 M_USB_BD_OWN | M_USB_BD_DTS | V_USB_BD_BC(kDataMaxPacketSize) |
400 static_cast<uint32_t>(next_rx_toggle_);
401 next_rx_toggle_ = Data01Inverse(next_rx_toggle_);
402 }
403 first_rx_held_ = second_rx_held_ = nullptr;
404 }
405
406 return r;
407}
408
409size_t AcmTty::Write(const void *buffer, size_t buffer_size) {
410 const size_t r =
411 tx_queue_.Write(static_cast<const char *>(buffer), buffer_size);
412 DisableInterrupts disable_interrupts;
413 EnqueueTxData(disable_interrupts);
414 return r;
415}
416
417// TODO(Brian): Could this critical section be broken up per buffer we fill?
418void AcmTty::EnqueueTxData(const DisableInterrupts &) {
419 while (BufferStateHasEmpty(tx_state_) && !tx_queue_.empty()) {
420 const EvenOdd next_tx_odd = BufferStateToFill(tx_state_);
421 const size_t buffer_size =
422 tx_queue_.Read(tx_buffer_for(next_tx_odd), kDataMaxPacketSize);
423 CHECK(buffer_size > 0);
424 dma_memory_barrier();
425 device()->SetBdtEntry(
426 data_tx_endpoint_, Direction::kTx, next_tx_odd,
427 {M_USB_BD_OWN | M_USB_BD_DTS | V_USB_BD_BC(buffer_size) |
428 static_cast<uint32_t>(next_tx_toggle_),
429 tx_buffer_for(next_tx_odd)});
430 tx_state_ = BufferStateAfterFill(tx_state_);
431 next_tx_toggle_ = Data01Inverse(next_tx_toggle_);
432 }
433}
434
435} // namespace teensy
436} // namespace frc971