blob: 575b4c14f8a139b8ca61ae295b617af563330e9e [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
Brian Silverman4aa83042018-01-05 12:47:31 -0800122 CreateIadDescriptor(
123 /*first_interface=*/status_interface_,
124 /*interface_count=*/2,
125 /*function_class=*/communications_class(),
126 /*function_subclass=*/communications_subclass::abstract_control_model(),
127 /*function_protocol=*/0, "UsbTty");
Brian Silvermaneda63f32017-10-08 18:57:33 -0400128
129 {
130 const auto interface_descriptor = CreateDescriptor(
131 interface_descriptor_length(), UsbDescriptorType::kInterface);
132 interface_descriptor->AddByte(status_interface_); // bInterfaceNumber
133 interface_descriptor->AddByte(0); // bAlternateSetting
134 interface_descriptor->AddByte(1); // bNumEndpoints
135 interface_descriptor->AddByte(communications_class()); // bInterfaceClass
136 interface_descriptor->AddByte(
137 communications_subclass::
138 abstract_control_model()); // bInterfaceSubClass
139 interface_descriptor->AddByte(0); // bInterfaceProtocol
140 interface_descriptor->AddByte(
141 device()->AddString("UsbTty.status")); // iInterface
142 }
143
144 {
145 const auto cdc_header =
146 CreateDescriptor(cdc_descriptor_subtype::header_length(),
147 UsbClassDescriptorType::kInterface);
148 cdc_header->AddByte(
149 cdc_descriptor_subtype::header()); // bDescriptorSubtype
150 cdc_header->AddUint16(0x0110); // bcdCDC
151 }
152
153 {
154 const auto call_management =
155 CreateDescriptor(cdc_descriptor_subtype::call_management_length(),
156 UsbClassDescriptorType::kInterface);
157 call_management->AddByte(
158 cdc_descriptor_subtype::call_management()); // bDescriptorSubtype
159 // We don't do call management.
160 call_management->AddByte(0); // bmCapabilities
161 call_management->AddByte(data_interface_); // bDataInterface
162 }
163
164 {
Brian Silvermand930f282017-11-04 23:09:12 -0400165 const auto abstract_control_management = CreateDescriptor(
166 cdc_descriptor_subtype::abstract_control_management_length(),
167 UsbClassDescriptorType::kInterface);
Brian Silvermaneda63f32017-10-08 18:57:33 -0400168 abstract_control_management->AddByte(
Brian Silvermand930f282017-11-04 23:09:12 -0400169 cdc_descriptor_subtype::
170 abstract_control_management()); // bDescriptorSubtype
Brian Silvermaneda63f32017-10-08 18:57:33 -0400171 // We support:
172 // line_coding and serial_state
173 // send_break
174 // We don't support:
175 // comm_feature
176 // network_notification
177 abstract_control_management->AddByte(6); // bmCapabilities
178 }
179
180 {
181 const auto cdc_union_descriptor =
182 CreateDescriptor(cdc_descriptor_subtype::union_length(1),
183 UsbClassDescriptorType::kInterface);
184 cdc_union_descriptor->AddByte(
185 cdc_descriptor_subtype::union_function()); // bDescriptorSubtype
186 cdc_union_descriptor->AddByte(status_interface_); // bMasterInterface
187 cdc_union_descriptor->AddByte(data_interface_); // bSlaveInterface
188 }
189
190 {
191 const auto endpoint_descriptor = CreateDescriptor(
192 endpoint_descriptor_length(), UsbDescriptorType::kEndpoint);
193 endpoint_descriptor->AddByte(status_endpoint_ |
194 m_endpoint_address_in()); // bEndpointAddress
195 endpoint_descriptor->AddByte(
196 m_endpoint_attributes_interrupt()); // bmAttributes
Brian Silvermand930f282017-11-04 23:09:12 -0400197 endpoint_descriptor->AddUint16(kStatusMaxPacketSize); // wMaxPacketSize
Brian Silvermaneda63f32017-10-08 18:57:33 -0400198 // Set it to the max because we have nothing to send, so no point using bus
199 // bandwidth asking.
200 endpoint_descriptor->AddByte(255); // bInterval
201 }
202
203 {
204 const auto interface_descriptor = CreateDescriptor(
205 interface_descriptor_length(), UsbDescriptorType::kInterface);
206 interface_descriptor->AddByte(data_interface_); // bInterfaceNumber
207 interface_descriptor->AddByte(0); // bAlternateSetting
208 interface_descriptor->AddByte(2); // bNumEndpoints
209 interface_descriptor->AddByte(data_interface_class()); // bInterfaceClass
210 interface_descriptor->AddByte(0); // bInterfaceSubClass
211 interface_descriptor->AddByte(0); // bInterfaceProtocol
212 interface_descriptor->AddByte(
213 device()->AddString("UsbTty.data")); // iInterface
214 }
215
216 // Kernel seems to think tx and rx belong in the other order, but it deals
217 // with either one. Everybody's examples seem to use this order, and the
218 // kernel deals with it, so going to go with this.
219 {
220 const auto endpoint_descriptor = CreateDescriptor(
221 endpoint_descriptor_length(), UsbDescriptorType::kEndpoint);
222 endpoint_descriptor->AddByte(data_rx_endpoint_); // bEndpointAddress
223 endpoint_descriptor->AddByte(m_endpoint_attributes_bulk()); // bmAttributes
Brian Silvermand930f282017-11-04 23:09:12 -0400224 endpoint_descriptor->AddUint16(kDataMaxPacketSize); // wMaxPacketSize
Brian Silvermaneda63f32017-10-08 18:57:33 -0400225 endpoint_descriptor->AddByte(1); // bInterval
226 }
227
228 {
229 const auto endpoint_descriptor = CreateDescriptor(
230 endpoint_descriptor_length(), UsbDescriptorType::kEndpoint);
231 endpoint_descriptor->AddByte(data_tx_endpoint_ |
232 m_endpoint_address_in()); // bEndpointAddress
233 endpoint_descriptor->AddByte(m_endpoint_attributes_bulk()); // bmAttributes
Brian Silvermand930f282017-11-04 23:09:12 -0400234 endpoint_descriptor->AddUint16(kDataMaxPacketSize); // wMaxPacketSize
Brian Silvermaneda63f32017-10-08 18:57:33 -0400235 endpoint_descriptor->AddByte(1); // bInterval
236 }
237}
238
239// We deliberately don't implement SendEncapsulatedCommand and
240// GetEncapsulatedResponse because there doesn't seem to be any reason for
241// anybody to send those to us, despite them being "required".
242UsbFunction::SetupResponse AcmTty::HandleEndpoint0SetupPacket(
243 const UsbDevice::SetupPacket &setup_packet) {
244 if (G_SETUP_REQUEST_TYPE_TYPE(setup_packet.request_type) !=
245 SetupRequestType::kClass) {
246 return SetupResponse::kIgnored;
247 }
248 if (G_SETUP_REQUEST_TYPE_RECIPIENT(setup_packet.request_type) !=
249 standard_setup_recipients::kInterface) {
250 return SetupResponse::kIgnored;
251 }
252 if (setup_packet.index != status_interface_) {
253 return SetupResponse::kIgnored;
254 }
255 const bool in = setup_packet.request_type & M_SETUP_REQUEST_TYPE_IN;
256 switch (setup_packet.request) {
257 case cdc_class_requests::set_line_coding():
258 if (in || setup_packet.value != 0 ||
259 setup_packet.length != sizeof(line_coding_)) {
260 return SetupResponse::kStall;
261 }
262 next_endpoint0_out_ = NextEndpoint0Out::kLineCoding;
263 return SetupResponse::kHandled;
264
265 case cdc_class_requests::get_line_coding():
266 if (!in || setup_packet.value != 0 ||
267 setup_packet.length != sizeof(line_coding_)) {
268 return SetupResponse::kStall;
269 }
270 line_coding_to_send_ = line_coding_;
271 device()->QueueEndpoint0Data(
272 reinterpret_cast<const char *>(&line_coding_to_send_),
273 setup_packet.length);
274 return SetupResponse::kHandled;
275
276 case cdc_class_requests::set_control_line_state():
277 if (in || setup_packet.length != 0) {
278 return SetupResponse::kStall;
279 }
280 control_line_state_ = setup_packet.value;
281 device()->SendEmptyEndpoint0Packet();
282 return SetupResponse::kHandled;
283
284 case cdc_class_requests::send_break():
285 if (in || setup_packet.length != 0) {
286 return SetupResponse::kStall;
287 }
288 // TODO(Brian): setup_packet.value is the length of the break in ms.
289 // 0xFFFF means keep sending break until receiving another one.
290 device()->SendEmptyEndpoint0Packet();
291 return SetupResponse::kHandled;
292 }
293 return SetupResponse::kStall;
294}
295
296UsbFunction::SetupResponse AcmTty::HandleEndpoint0OutPacket(void *data,
297 int data_length) {
298 switch (next_endpoint0_out_) {
299 case NextEndpoint0Out::kNone:
300 return SetupResponse::kIgnored;
301
302 case NextEndpoint0Out::kLineCoding:
Brian Silverman69f96c22017-11-01 02:54:02 -0400303 next_endpoint0_out_ = NextEndpoint0Out::kNone;
Brian Silvermaneda63f32017-10-08 18:57:33 -0400304 if (data_length != sizeof(line_coding_)) {
305 return SetupResponse::kStall;
306 }
307 memcpy(&line_coding_, data, data_length);
308 device()->SendEmptyEndpoint0Packet();
Brian Silverman69f96c22017-11-01 02:54:02 -0400309 // If we're supposed to reboot, then do it.
310 if (line_coding_.rate == UINT32_C(0x97101678)) {
311 // Delay for a bit so the empty IN packet gets back to the host.
312 delay(5);
313 __asm__ __volatile__("bkpt");
314 }
Brian Silvermaneda63f32017-10-08 18:57:33 -0400315 return SetupResponse::kHandled;
316
317 default:
318 return SetupResponse::kIgnored;
319 }
320}
321
322void AcmTty::HandleOutFinished(int endpoint, BdtEntry *bdt_entry) {
323 if (endpoint == data_rx_endpoint_) {
324 const size_t data_size = G_USB_BD_BC(bdt_entry->buffer_descriptor);
325 CHECK(rx_queue_.Write(static_cast<char *>(bdt_entry->address), data_size) ==
326 data_size);
327 dma_memory_barrier();
328
329 DisableInterrupts disable_interrupts;
330 // If we don't have space to handle it, don't return the entry so we'll
331 // ask the host to wait to transmit more data.
332 if (rx_queue_.space_available() >= kDataMaxPacketSize * 2) {
333 bdt_entry->buffer_descriptor = M_USB_BD_OWN | M_USB_BD_DTS |
334 V_USB_BD_BC(kDataMaxPacketSize) |
335 static_cast<uint32_t>(next_rx_toggle_);
336 next_rx_toggle_ = Data01Inverse(next_rx_toggle_);
337 } else {
338 if (first_rx_held_ == nullptr) {
339 first_rx_held_ = bdt_entry;
340 } else {
341 CHECK(second_rx_held_ == nullptr);
342 second_rx_held_ = bdt_entry;
343 }
344 }
345 }
346}
347
348void AcmTty::HandleInFinished(int endpoint, BdtEntry * /*bdt_entry*/,
349 EvenOdd odd) {
350 if (endpoint == data_tx_endpoint_) {
351 DisableInterrupts disable_interrupts;
352 CHECK(odd == BufferStateToEmpty(tx_state_));
353 tx_state_ = BufferStateAfterEmpty(tx_state_);
354 EnqueueTxData(disable_interrupts);
355 }
356}
357
358void AcmTty::HandleConfigured(int endpoint) {
359 // TODO(Brian): Handle data already in the buffers correctly.
360 if (endpoint == status_endpoint_) {
361 device()->ConfigureEndpointFor(status_endpoint_, false, true, true);
362 } else if (endpoint == data_tx_endpoint_) {
363 device()->ConfigureEndpointFor(data_tx_endpoint_, false, true, true);
Brian Silvermaneda63f32017-10-08 18:57:33 -0400364 DisableInterrupts disable_interrupts;
Brian Silvermand930f282017-11-04 23:09:12 -0400365 next_tx_toggle_ = Data01::kData0;
Brian Silvermaneda63f32017-10-08 18:57:33 -0400366 EnqueueTxData(disable_interrupts);
367 } else if (endpoint == data_rx_endpoint_) {
368 device()->ConfigureEndpointFor(data_rx_endpoint_, true, false, true);
369 next_rx_toggle_ = Data01::kData0;
370 device()->SetBdtEntry(
371 data_rx_endpoint_, Direction::kRx, EvenOdd::kEven,
Brian Silverman64d04272017-11-26 22:26:39 -0500372 {M_USB_BD_OWN | M_USB_BD_DTS | V_USB_BD_BC(rx_buffers_[0].size()),
373 rx_buffers_[0].data()});
Brian Silvermaneda63f32017-10-08 18:57:33 -0400374 device()->SetBdtEntry(
375 data_rx_endpoint_, Direction::kRx, EvenOdd::kOdd,
Brian Silverman64d04272017-11-26 22:26:39 -0500376 {M_USB_BD_OWN | M_USB_BD_DTS | V_USB_BD_BC(rx_buffers_[1].size()) |
Brian Silvermaneda63f32017-10-08 18:57:33 -0400377 M_USB_BD_DATA1,
Brian Silverman64d04272017-11-26 22:26:39 -0500378 rx_buffers_[1].data()});
Brian Silvermaneda63f32017-10-08 18:57:33 -0400379 }
380}
381
382size_t AcmTty::Read(void *buffer, size_t buffer_size) {
383 const size_t r = rx_queue_.Read(static_cast<char *>(buffer), buffer_size);
384
385 DisableInterrupts disable_interrupts;
386 if (rx_queue_.space_available() >= kDataMaxPacketSize * 2) {
387 if (first_rx_held_ != nullptr) {
388 first_rx_held_->buffer_descriptor =
389 M_USB_BD_OWN | M_USB_BD_DTS | V_USB_BD_BC(kDataMaxPacketSize) |
390 static_cast<uint32_t>(next_rx_toggle_);
391 next_rx_toggle_ = Data01Inverse(next_rx_toggle_);
392 }
393 if (second_rx_held_ != nullptr) {
394 second_rx_held_->buffer_descriptor =
395 M_USB_BD_OWN | M_USB_BD_DTS | V_USB_BD_BC(kDataMaxPacketSize) |
396 static_cast<uint32_t>(next_rx_toggle_);
397 next_rx_toggle_ = Data01Inverse(next_rx_toggle_);
398 }
399 first_rx_held_ = second_rx_held_ = nullptr;
400 }
401
402 return r;
403}
404
405size_t AcmTty::Write(const void *buffer, size_t buffer_size) {
406 const size_t r =
407 tx_queue_.Write(static_cast<const char *>(buffer), buffer_size);
408 DisableInterrupts disable_interrupts;
409 EnqueueTxData(disable_interrupts);
410 return r;
411}
412
413// TODO(Brian): Could this critical section be broken up per buffer we fill?
414void AcmTty::EnqueueTxData(const DisableInterrupts &) {
415 while (BufferStateHasEmpty(tx_state_) && !tx_queue_.empty()) {
416 const EvenOdd next_tx_odd = BufferStateToFill(tx_state_);
417 const size_t buffer_size =
418 tx_queue_.Read(tx_buffer_for(next_tx_odd), kDataMaxPacketSize);
419 CHECK(buffer_size > 0);
420 dma_memory_barrier();
421 device()->SetBdtEntry(
422 data_tx_endpoint_, Direction::kTx, next_tx_odd,
423 {M_USB_BD_OWN | M_USB_BD_DTS | V_USB_BD_BC(buffer_size) |
424 static_cast<uint32_t>(next_tx_toggle_),
425 tx_buffer_for(next_tx_odd)});
426 tx_state_ = BufferStateAfterFill(tx_state_);
427 next_tx_toggle_ = Data01Inverse(next_tx_toggle_);
428 }
429}
430
431} // namespace teensy
432} // namespace frc971