blob: 87ce0dcfa0fdaa1105a48bff945d8aeff8ee2e75 [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 {
Brian Silvermand930f282017-11-04 23:09:12 -0400170 const auto abstract_control_management = CreateDescriptor(
171 cdc_descriptor_subtype::abstract_control_management_length(),
172 UsbClassDescriptorType::kInterface);
Brian Silvermaneda63f32017-10-08 18:57:33 -0400173 abstract_control_management->AddByte(
Brian Silvermand930f282017-11-04 23:09:12 -0400174 cdc_descriptor_subtype::
175 abstract_control_management()); // bDescriptorSubtype
Brian Silvermaneda63f32017-10-08 18:57:33 -0400176 // We support:
177 // line_coding and serial_state
178 // send_break
179 // We don't support:
180 // comm_feature
181 // network_notification
182 abstract_control_management->AddByte(6); // bmCapabilities
183 }
184
185 {
186 const auto cdc_union_descriptor =
187 CreateDescriptor(cdc_descriptor_subtype::union_length(1),
188 UsbClassDescriptorType::kInterface);
189 cdc_union_descriptor->AddByte(
190 cdc_descriptor_subtype::union_function()); // bDescriptorSubtype
191 cdc_union_descriptor->AddByte(status_interface_); // bMasterInterface
192 cdc_union_descriptor->AddByte(data_interface_); // bSlaveInterface
193 }
194
195 {
196 const auto endpoint_descriptor = CreateDescriptor(
197 endpoint_descriptor_length(), UsbDescriptorType::kEndpoint);
198 endpoint_descriptor->AddByte(status_endpoint_ |
199 m_endpoint_address_in()); // bEndpointAddress
200 endpoint_descriptor->AddByte(
201 m_endpoint_attributes_interrupt()); // bmAttributes
Brian Silvermand930f282017-11-04 23:09:12 -0400202 endpoint_descriptor->AddUint16(kStatusMaxPacketSize); // wMaxPacketSize
Brian Silvermaneda63f32017-10-08 18:57:33 -0400203 // Set it to the max because we have nothing to send, so no point using bus
204 // bandwidth asking.
205 endpoint_descriptor->AddByte(255); // bInterval
206 }
207
208 {
209 const auto interface_descriptor = CreateDescriptor(
210 interface_descriptor_length(), UsbDescriptorType::kInterface);
211 interface_descriptor->AddByte(data_interface_); // bInterfaceNumber
212 interface_descriptor->AddByte(0); // bAlternateSetting
213 interface_descriptor->AddByte(2); // bNumEndpoints
214 interface_descriptor->AddByte(data_interface_class()); // bInterfaceClass
215 interface_descriptor->AddByte(0); // bInterfaceSubClass
216 interface_descriptor->AddByte(0); // bInterfaceProtocol
217 interface_descriptor->AddByte(
218 device()->AddString("UsbTty.data")); // iInterface
219 }
220
221 // Kernel seems to think tx and rx belong in the other order, but it deals
222 // with either one. Everybody's examples seem to use this order, and the
223 // kernel deals with it, so going to go with this.
224 {
225 const auto endpoint_descriptor = CreateDescriptor(
226 endpoint_descriptor_length(), UsbDescriptorType::kEndpoint);
227 endpoint_descriptor->AddByte(data_rx_endpoint_); // bEndpointAddress
228 endpoint_descriptor->AddByte(m_endpoint_attributes_bulk()); // bmAttributes
Brian Silvermand930f282017-11-04 23:09:12 -0400229 endpoint_descriptor->AddUint16(kDataMaxPacketSize); // wMaxPacketSize
Brian Silvermaneda63f32017-10-08 18:57:33 -0400230 endpoint_descriptor->AddByte(1); // bInterval
231 }
232
233 {
234 const auto endpoint_descriptor = CreateDescriptor(
235 endpoint_descriptor_length(), UsbDescriptorType::kEndpoint);
236 endpoint_descriptor->AddByte(data_tx_endpoint_ |
237 m_endpoint_address_in()); // bEndpointAddress
238 endpoint_descriptor->AddByte(m_endpoint_attributes_bulk()); // bmAttributes
Brian Silvermand930f282017-11-04 23:09:12 -0400239 endpoint_descriptor->AddUint16(kDataMaxPacketSize); // wMaxPacketSize
Brian Silvermaneda63f32017-10-08 18:57:33 -0400240 endpoint_descriptor->AddByte(1); // bInterval
241 }
242}
243
244// We deliberately don't implement SendEncapsulatedCommand and
245// GetEncapsulatedResponse because there doesn't seem to be any reason for
246// anybody to send those to us, despite them being "required".
247UsbFunction::SetupResponse AcmTty::HandleEndpoint0SetupPacket(
248 const UsbDevice::SetupPacket &setup_packet) {
249 if (G_SETUP_REQUEST_TYPE_TYPE(setup_packet.request_type) !=
250 SetupRequestType::kClass) {
251 return SetupResponse::kIgnored;
252 }
253 if (G_SETUP_REQUEST_TYPE_RECIPIENT(setup_packet.request_type) !=
254 standard_setup_recipients::kInterface) {
255 return SetupResponse::kIgnored;
256 }
257 if (setup_packet.index != status_interface_) {
258 return SetupResponse::kIgnored;
259 }
260 const bool in = setup_packet.request_type & M_SETUP_REQUEST_TYPE_IN;
261 switch (setup_packet.request) {
262 case cdc_class_requests::set_line_coding():
263 if (in || setup_packet.value != 0 ||
264 setup_packet.length != sizeof(line_coding_)) {
265 return SetupResponse::kStall;
266 }
267 next_endpoint0_out_ = NextEndpoint0Out::kLineCoding;
268 return SetupResponse::kHandled;
269
270 case cdc_class_requests::get_line_coding():
271 if (!in || setup_packet.value != 0 ||
272 setup_packet.length != sizeof(line_coding_)) {
273 return SetupResponse::kStall;
274 }
275 line_coding_to_send_ = line_coding_;
276 device()->QueueEndpoint0Data(
277 reinterpret_cast<const char *>(&line_coding_to_send_),
278 setup_packet.length);
279 return SetupResponse::kHandled;
280
281 case cdc_class_requests::set_control_line_state():
282 if (in || setup_packet.length != 0) {
283 return SetupResponse::kStall;
284 }
285 control_line_state_ = setup_packet.value;
286 device()->SendEmptyEndpoint0Packet();
287 return SetupResponse::kHandled;
288
289 case cdc_class_requests::send_break():
290 if (in || setup_packet.length != 0) {
291 return SetupResponse::kStall;
292 }
293 // TODO(Brian): setup_packet.value is the length of the break in ms.
294 // 0xFFFF means keep sending break until receiving another one.
295 device()->SendEmptyEndpoint0Packet();
296 return SetupResponse::kHandled;
297 }
298 return SetupResponse::kStall;
299}
300
301UsbFunction::SetupResponse AcmTty::HandleEndpoint0OutPacket(void *data,
302 int data_length) {
303 switch (next_endpoint0_out_) {
304 case NextEndpoint0Out::kNone:
305 return SetupResponse::kIgnored;
306
307 case NextEndpoint0Out::kLineCoding:
Brian Silverman69f96c22017-11-01 02:54:02 -0400308 next_endpoint0_out_ = NextEndpoint0Out::kNone;
Brian Silvermaneda63f32017-10-08 18:57:33 -0400309 if (data_length != sizeof(line_coding_)) {
310 return SetupResponse::kStall;
311 }
312 memcpy(&line_coding_, data, data_length);
313 device()->SendEmptyEndpoint0Packet();
Brian Silverman69f96c22017-11-01 02:54:02 -0400314 // If we're supposed to reboot, then do it.
315 if (line_coding_.rate == UINT32_C(0x97101678)) {
316 // Delay for a bit so the empty IN packet gets back to the host.
317 delay(5);
318 __asm__ __volatile__("bkpt");
319 }
Brian Silvermaneda63f32017-10-08 18:57:33 -0400320 return SetupResponse::kHandled;
321
322 default:
323 return SetupResponse::kIgnored;
324 }
325}
326
327void AcmTty::HandleOutFinished(int endpoint, BdtEntry *bdt_entry) {
328 if (endpoint == data_rx_endpoint_) {
329 const size_t data_size = G_USB_BD_BC(bdt_entry->buffer_descriptor);
330 CHECK(rx_queue_.Write(static_cast<char *>(bdt_entry->address), data_size) ==
331 data_size);
332 dma_memory_barrier();
333
334 DisableInterrupts disable_interrupts;
335 // If we don't have space to handle it, don't return the entry so we'll
336 // ask the host to wait to transmit more data.
337 if (rx_queue_.space_available() >= kDataMaxPacketSize * 2) {
338 bdt_entry->buffer_descriptor = M_USB_BD_OWN | M_USB_BD_DTS |
339 V_USB_BD_BC(kDataMaxPacketSize) |
340 static_cast<uint32_t>(next_rx_toggle_);
341 next_rx_toggle_ = Data01Inverse(next_rx_toggle_);
342 } else {
343 if (first_rx_held_ == nullptr) {
344 first_rx_held_ = bdt_entry;
345 } else {
346 CHECK(second_rx_held_ == nullptr);
347 second_rx_held_ = bdt_entry;
348 }
349 }
350 }
351}
352
353void AcmTty::HandleInFinished(int endpoint, BdtEntry * /*bdt_entry*/,
354 EvenOdd odd) {
355 if (endpoint == data_tx_endpoint_) {
356 DisableInterrupts disable_interrupts;
357 CHECK(odd == BufferStateToEmpty(tx_state_));
358 tx_state_ = BufferStateAfterEmpty(tx_state_);
359 EnqueueTxData(disable_interrupts);
360 }
361}
362
363void AcmTty::HandleConfigured(int endpoint) {
364 // TODO(Brian): Handle data already in the buffers correctly.
365 if (endpoint == status_endpoint_) {
366 device()->ConfigureEndpointFor(status_endpoint_, false, true, true);
367 } else if (endpoint == data_tx_endpoint_) {
368 device()->ConfigureEndpointFor(data_tx_endpoint_, false, true, true);
Brian Silvermaneda63f32017-10-08 18:57:33 -0400369 DisableInterrupts disable_interrupts;
Brian Silvermand930f282017-11-04 23:09:12 -0400370 next_tx_toggle_ = Data01::kData0;
Brian Silvermaneda63f32017-10-08 18:57:33 -0400371 EnqueueTxData(disable_interrupts);
372 } else if (endpoint == data_rx_endpoint_) {
373 device()->ConfigureEndpointFor(data_rx_endpoint_, true, false, true);
374 next_rx_toggle_ = Data01::kData0;
375 device()->SetBdtEntry(
376 data_rx_endpoint_, Direction::kRx, EvenOdd::kEven,
Brian Silverman64d04272017-11-26 22:26:39 -0500377 {M_USB_BD_OWN | M_USB_BD_DTS | V_USB_BD_BC(rx_buffers_[0].size()),
378 rx_buffers_[0].data()});
Brian Silvermaneda63f32017-10-08 18:57:33 -0400379 device()->SetBdtEntry(
380 data_rx_endpoint_, Direction::kRx, EvenOdd::kOdd,
Brian Silverman64d04272017-11-26 22:26:39 -0500381 {M_USB_BD_OWN | M_USB_BD_DTS | V_USB_BD_BC(rx_buffers_[1].size()) |
Brian Silvermaneda63f32017-10-08 18:57:33 -0400382 M_USB_BD_DATA1,
Brian Silverman64d04272017-11-26 22:26:39 -0500383 rx_buffers_[1].data()});
Brian Silvermaneda63f32017-10-08 18:57:33 -0400384 }
385}
386
387size_t AcmTty::Read(void *buffer, size_t buffer_size) {
388 const size_t r = rx_queue_.Read(static_cast<char *>(buffer), buffer_size);
389
390 DisableInterrupts disable_interrupts;
391 if (rx_queue_.space_available() >= kDataMaxPacketSize * 2) {
392 if (first_rx_held_ != nullptr) {
393 first_rx_held_->buffer_descriptor =
394 M_USB_BD_OWN | M_USB_BD_DTS | V_USB_BD_BC(kDataMaxPacketSize) |
395 static_cast<uint32_t>(next_rx_toggle_);
396 next_rx_toggle_ = Data01Inverse(next_rx_toggle_);
397 }
398 if (second_rx_held_ != nullptr) {
399 second_rx_held_->buffer_descriptor =
400 M_USB_BD_OWN | M_USB_BD_DTS | V_USB_BD_BC(kDataMaxPacketSize) |
401 static_cast<uint32_t>(next_rx_toggle_);
402 next_rx_toggle_ = Data01Inverse(next_rx_toggle_);
403 }
404 first_rx_held_ = second_rx_held_ = nullptr;
405 }
406
407 return r;
408}
409
410size_t AcmTty::Write(const void *buffer, size_t buffer_size) {
411 const size_t r =
412 tx_queue_.Write(static_cast<const char *>(buffer), buffer_size);
413 DisableInterrupts disable_interrupts;
414 EnqueueTxData(disable_interrupts);
415 return r;
416}
417
418// TODO(Brian): Could this critical section be broken up per buffer we fill?
419void AcmTty::EnqueueTxData(const DisableInterrupts &) {
420 while (BufferStateHasEmpty(tx_state_) && !tx_queue_.empty()) {
421 const EvenOdd next_tx_odd = BufferStateToFill(tx_state_);
422 const size_t buffer_size =
423 tx_queue_.Read(tx_buffer_for(next_tx_odd), kDataMaxPacketSize);
424 CHECK(buffer_size > 0);
425 dma_memory_barrier();
426 device()->SetBdtEntry(
427 data_tx_endpoint_, Direction::kTx, next_tx_odd,
428 {M_USB_BD_OWN | M_USB_BD_DTS | V_USB_BD_BC(buffer_size) |
429 static_cast<uint32_t>(next_tx_toggle_),
430 tx_buffer_for(next_tx_odd)});
431 tx_state_ = BufferStateAfterFill(tx_state_);
432 next_tx_toggle_ = Data01Inverse(next_tx_toggle_);
433 }
434}
435
436} // namespace teensy
437} // namespace frc971