Brian Silverman | f91524f | 2017-09-23 13:15:55 -0400 | [diff] [blame] | 1 | #include "motors/usb/usb.h" |
| 2 | |
| 3 | #include <string.h> |
| 4 | |
| 5 | #include "motors/util.h" |
| 6 | |
| 7 | namespace frc971 { |
| 8 | namespace teensy { |
| 9 | namespace { |
| 10 | |
| 11 | // The mask of interrupts we care about. |
| 12 | constexpr uint32_t usb_enabled_interrupts() { |
| 13 | // Deliberately not turning the sleep interrupt on here because we just |
| 14 | // want to ignore that anyways. |
| 15 | return USB_INTEN_TOKDNEEN | USB_INTEN_SOFTOKEN | USB_INTEN_ERROREN | |
| 16 | USB_INTEN_USBRSTEN; |
| 17 | } |
| 18 | |
| 19 | // The names of all the standard setup requests which come in on endpoint 0. |
| 20 | namespace standard_setup_requests { |
| 21 | constexpr int kGetStatus = 0; |
| 22 | constexpr int kClearFeature = 1; |
| 23 | constexpr int kSetFeature = 3; |
| 24 | constexpr int kSetAddress = 5; |
| 25 | constexpr int kGetDescriptor = 6; |
| 26 | constexpr int kSetDescriptor = 7; |
| 27 | constexpr int kGetConfiguration = 8; |
| 28 | constexpr int kSetConfiguration = 9; |
| 29 | constexpr int kGetInterface = 10; |
| 30 | constexpr int kSetInterface = 11; |
| 31 | constexpr int kSynchFrame = 12; |
| 32 | } // namespace standard_setup_requests |
| 33 | |
| 34 | // The names of the standard feature selectors. |
| 35 | namespace standard_feature_selectors { |
| 36 | constexpr int kDeviceRemoteWakeup = 1; |
| 37 | constexpr int kEndpointHalt = 0; |
| 38 | constexpr int kTestMode = 2; |
| 39 | } // namespace standard_feature_selectors |
| 40 | |
| 41 | // The names of all the PIDs (Packet IDs) from the USB standard. Note that this |
| 42 | // USB hardware doesn't expose most of them, especially in device mode. |
| 43 | enum class UsbPid { |
| 44 | kOut = 0x1, |
| 45 | kIn = 0x9, |
| 46 | kSof = 0x5, |
| 47 | kSetup = 0xD, |
| 48 | kData0 = 0x3, |
| 49 | kData1 = 0xB, |
| 50 | kData2 = 0x7, |
| 51 | kMData = 0xF, |
| 52 | kAck = 0x2, |
| 53 | kNak = 0xA, |
| 54 | kStall = 0xE, |
| 55 | kNYet = 0x6, |
| 56 | kPre = 0xC, |
| 57 | kErr = 0xC, |
| 58 | kSplit = 0x8, |
| 59 | kPing = 0x4, |
| 60 | kReserved = 0x0, |
| 61 | }; |
| 62 | |
| 63 | // The device class for using IADs. |
| 64 | constexpr uint8_t iad_device_class() { return 0xEF; } |
| 65 | // The device subclass for using IADs. |
| 66 | constexpr uint8_t iad_device_subclass() { return 0x02; } |
| 67 | // The device protocol for using IADs. |
| 68 | constexpr uint8_t iad_device_protocol() { return 0x01; } |
| 69 | |
| 70 | // The total number of endpoints supported by this hardware. |
| 71 | constexpr int number_endpoints() { return 16; } |
| 72 | |
| 73 | __attribute__((aligned(512))) BdtEntry |
| 74 | usb0_buffer_descriptor_table[number_endpoints() * 2 /* rx/tx */ * |
| 75 | 2 /* even/odd */]; |
| 76 | |
| 77 | // Returns the specified BDT entry. |
| 78 | BdtEntry *MutableBdtEntry(int endpoint, Direction direction, EvenOdd odd) { |
| 79 | return &usb0_buffer_descriptor_table[static_cast<uint32_t>(endpoint << 2) | |
| 80 | static_cast<uint32_t>(direction) | |
| 81 | static_cast<uint32_t>(odd)]; |
| 82 | } |
| 83 | |
| 84 | // Returns the BDT entry corresponding to a USBx_STAT value. |
| 85 | BdtEntry *MutableBdtEntryFromStat(uint8_t stat) { |
| 86 | return &usb0_buffer_descriptor_table[static_cast<uint32_t>(stat) >> 2]; |
| 87 | } |
| 88 | |
| 89 | // A pointer to the object we're going to ask to handle interrupts. |
| 90 | UsbDevice *volatile global_usb0_device = nullptr; |
| 91 | |
| 92 | } // namespace |
| 93 | |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame^] | 94 | constexpr int UsbDevice::kEndpoint0MaxSize; |
| 95 | |
Brian Silverman | f91524f | 2017-09-23 13:15:55 -0400 | [diff] [blame] | 96 | UsbDevice::UsbDevice(int index, uint16_t vendor_id, uint16_t product_id) |
| 97 | : index_(index) { |
| 98 | // TODO(Brian): Pass index_ into all the register access macros. Also sort out |
| 99 | // how to deal with it for the interrupts. |
| 100 | assert(index == 0); |
| 101 | |
| 102 | assert(global_usb0_device == nullptr); |
| 103 | global_usb0_device = this; |
| 104 | |
| 105 | // Endpoint 0 isn't a normal endpoint, so it doesn't show up in here. |
| 106 | endpoint_mapping_.push_back(nullptr); |
| 107 | |
| 108 | // Set up the "String Descriptor Zero, Specifying Languages Supported by the |
| 109 | // Device" (aka english_us_code() only). |
| 110 | strings_.emplace_back(4, '\0'); |
| 111 | strings_.back()[0] = 4; |
| 112 | strings_.back()[1] = static_cast<uint8_t>(UsbDescriptorType::kString); |
| 113 | strings_.back()[2] = english_us_code() & 0xFF; |
| 114 | strings_.back()[3] = (english_us_code() >> 8) & 0xFF; |
| 115 | |
| 116 | device_descriptor_ = |
| 117 | device_descriptor_list_.CreateDescriptor(18, UsbDescriptorType::kDevice); |
| 118 | device_descriptor_->AddUint16(0x0200); // bcdUSB |
| 119 | device_descriptor_->AddByte(iad_device_class()); // bDeviceClass |
| 120 | device_descriptor_->AddByte(iad_device_subclass()); // bDeviceSubClass |
| 121 | device_descriptor_->AddByte(iad_device_protocol()); // bDeviceProtocol |
| 122 | device_descriptor_->AddByte(kEndpoint0MaxSize); // bMaxPacketSize0 |
| 123 | device_descriptor_->AddUint16(vendor_id); // idVendor |
| 124 | device_descriptor_->AddUint16(product_id); // idProduct |
| 125 | device_descriptor_->AddUint16(0); // bcdDevice |
| 126 | // We might overwrite these string descriptor indices later if we get strings |
| 127 | // to put there. |
| 128 | device_descriptor_->AddByte(0); // iManufacturer |
| 129 | device_descriptor_->AddByte(0); // iProduct |
| 130 | device_descriptor_->AddByte(0); // iSerialNumber |
| 131 | device_descriptor_->AddByte(1); // bNumConfigurations |
| 132 | |
| 133 | config_descriptor_ = config_descriptor_list_.CreateDescriptor( |
| 134 | 9, UsbDescriptorType::kConfiguration); |
| 135 | } |
| 136 | |
| 137 | UsbDevice::~UsbDevice() { |
| 138 | NVIC_DISABLE_IRQ(IRQ_USBOTG); |
| 139 | dma_memory_barrier(); |
| 140 | assert(global_usb0_device == this); |
| 141 | global_usb0_device = nullptr; |
| 142 | } |
| 143 | |
| 144 | void UsbDevice::Initialize() { |
| 145 | assert(!is_set_up_); |
| 146 | |
| 147 | for (UsbFunction *function : functions_) { |
| 148 | function->Initialize(); |
| 149 | } |
| 150 | |
| 151 | config_descriptor_->AddUint16( |
| 152 | config_descriptor_list_.CurrentSize()); // wTotalLength |
| 153 | config_descriptor_->AddByte(interface_mapping_.size()); // bNumInterfaces |
| 154 | config_descriptor_->AddByte(1); // bConfigurationValue |
| 155 | // Doesn't seem to be much point naming our one and only configuration. |
| 156 | config_descriptor_->AddByte(0); // iConfiguration |
| 157 | config_descriptor_->AddByte((1 << 7) /* Reserved */ | |
| 158 | (1 << 6) /* Self-powered */); // bmAttribute |
| 159 | config_descriptor_->AddByte(2 /* 4mA */); // bMaxPower |
| 160 | |
| 161 | device_descriptor_.reset(); |
| 162 | config_descriptor_.reset(); |
| 163 | device_descriptor_list_.CheckFinished(); |
| 164 | config_descriptor_list_.CheckFinished(); |
| 165 | is_set_up_ = true; |
| 166 | |
| 167 | // Make sure all the buffer descriptors are clear. |
| 168 | for (int i = 0; i < number_endpoints(); ++i) { |
| 169 | for (Direction direction : {Direction::kTx, Direction::kRx}) { |
| 170 | for (EvenOdd odd : {EvenOdd::kOdd, EvenOdd::kEven}) { |
| 171 | MutableBdtEntry(i, direction, odd)->buffer_descriptor = 0; |
| 172 | MutableBdtEntry(i, direction, odd)->address = nullptr; |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | dma_memory_barrier(); |
| 177 | |
| 178 | // The other startup code handles getting the incoming 48MHz clock running. |
| 179 | SIM_SCGC4 |= SIM_SCGC4_USBOTG; |
| 180 | MPU_RGDAAC0 |= 0x03000000; |
| 181 | |
| 182 | // Reset it. |
| 183 | USB0_USBTRC0 = USB_USBTRC_USBRESET; |
| 184 | // TRM says to wait "two USB clock cycles", so assume that's at 48MHz and then |
| 185 | // round up, being pessimistic in assuming each read from the peripheral is |
| 186 | // only a single core clock. This wildly overapproximates how long we need to |
| 187 | // wait, but whatever. |
| 188 | for (int i = 0; i < ((F_CPU / 48000000) + 1) * 2; ++i) { |
| 189 | while ((USB0_USBTRC0 & USB_USBTRC_USBRESET) != 0) { |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | USB0_BDTPAGE1 = |
| 194 | reinterpret_cast<uintptr_t>(&usb0_buffer_descriptor_table[0]) >> 8; |
| 195 | USB0_BDTPAGE2 = |
| 196 | reinterpret_cast<uintptr_t>(&usb0_buffer_descriptor_table[0]) >> 16; |
| 197 | USB0_BDTPAGE3 = |
| 198 | reinterpret_cast<uintptr_t>(&usb0_buffer_descriptor_table[0]) >> 24; |
| 199 | |
| 200 | // The Quick Reference User Guide says to clear all the interrupts. |
| 201 | ClearInterrupts(); |
| 202 | USB0_OTGISTAT = USB_OTGISTAT_ONEMSEC | USB_OTGISTAT_LINE_STATE_CHG; |
| 203 | |
| 204 | // Now enable the module. |
| 205 | USB0_CTL = USB_CTL_USBENSOFEN; |
| 206 | |
| 207 | // Un-suspend the transceiver and disable weak pulldowns. |
| 208 | USB0_USBCTRL = 0; |
| 209 | // And enable the D+ pullup which indicates we're a full-speed device. |
| 210 | USB0_CONTROL = USB_CONTROL_DPPULLUPNONOTG; |
| 211 | |
| 212 | // Enable the reset interrupt (which is the first one we care about). |
| 213 | USB0_INTEN = USB_INTEN_USBRSTEN; |
| 214 | |
| 215 | dma_memory_barrier(); |
| 216 | NVIC_ENABLE_IRQ(IRQ_USBOTG); |
| 217 | } |
| 218 | |
| 219 | void usb_isr(void) { |
| 220 | UsbDevice *const usb0_device = global_usb0_device; |
| 221 | if (usb0_device == nullptr) { |
| 222 | NVIC_DISABLE_IRQ(IRQ_USBOTG); |
| 223 | } else { |
| 224 | usb0_device->HandleInterrupt(); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | void UsbDevice::ClearInterrupts() { |
| 229 | USB0_ISTAT = USB_ISTAT_ATTACH | USB_ISTAT_RESUME | USB_ISTAT_SLEEP | |
| 230 | USB_ISTAT_TOKDNE | USB_ISTAT_SOFTOK | USB_ISTAT_ERROR | |
| 231 | USB_ISTAT_USBRST; |
| 232 | USB0_ERRSTAT = USB_ERRSTAT_BTSERR | USB_ERRSTAT_DMAERR | USB_ERRSTAT_BTOERR | |
| 233 | USB_ERRSTAT_DFN8 | USB_ERRSTAT_CRC16 | USB_ERRSTAT_CRC5EOF | |
| 234 | USB_ERRSTAT_PIDERR; |
| 235 | } |
| 236 | |
| 237 | void UsbDevice::HandleInterrupt() { |
| 238 | while (true) { |
| 239 | const uint32_t status = USB0_ISTAT; |
| 240 | if ((status & usb_enabled_interrupts()) == 0) { |
| 241 | return; |
| 242 | } |
| 243 | |
| 244 | // If we just got a start-of-frame token, then ask all the functions what to |
| 245 | // do. |
| 246 | if (status & USB_ISTAT_SOFTOK) { |
| 247 | // TODO(Brian): Actually ask the functions, maybe only if we're |
| 248 | // configured. |
| 249 | USB0_ISTAT = USB_ISTAT_SOFTOK; |
| 250 | } |
| 251 | |
| 252 | // If we just finished processing a token. |
| 253 | if (status & USB_ISTAT_TOKDNE) { |
| 254 | const uint8_t stat = USB0_STAT; |
Brian Silverman | 69f96c2 | 2017-11-01 02:54:02 -0400 | [diff] [blame] | 255 | USB0_ISTAT = USB_ISTAT_TOKDNE; |
Brian Silverman | f91524f | 2017-09-23 13:15:55 -0400 | [diff] [blame] | 256 | const int endpoint = G_USB_STAT_ENDP(stat); |
| 257 | |
| 258 | if (endpoint == 0) { |
| 259 | HandleEndpoint0Token(stat); |
| 260 | } else { |
| 261 | BdtEntry *const bdt_entry = MutableBdtEntryFromStat(stat); |
| 262 | const UsbPid pid = G_USB_BD_PID(bdt_entry->buffer_descriptor); |
| 263 | UsbFunction *const function = endpoint_mapping_[endpoint]; |
| 264 | if (function == nullptr) { |
| 265 | // Should never happen, so stall if we do get here somehow. |
| 266 | StallEndpoint(endpoint); |
| 267 | } else { |
| 268 | switch (pid) { |
| 269 | case UsbPid::kOut: |
| 270 | function->HandleOutFinished(endpoint, bdt_entry); |
| 271 | break; |
| 272 | |
| 273 | case UsbPid::kIn: |
| 274 | function->HandleInFinished( |
| 275 | endpoint, bdt_entry, |
| 276 | (stat & M_USB_STAT_ODD) ? EvenOdd::kOdd : EvenOdd::kEven); |
| 277 | break; |
| 278 | |
| 279 | case UsbPid::kSetup: |
| 280 | default: |
| 281 | // Should never happen, so stall if we do get here somehow. |
| 282 | StallEndpoint(endpoint); |
| 283 | break; |
| 284 | } |
| 285 | } |
| 286 | } |
Brian Silverman | f91524f | 2017-09-23 13:15:55 -0400 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | if (status & USB_ISTAT_USBRST) { |
| 290 | // Use DATA0 for all endpoints. |
| 291 | USB0_CTL = USB_CTL_ODDRST; |
| 292 | endpoint0_tx_odd_ = EvenOdd::kEven; |
| 293 | endpoint0_tx_toggle_ = Data01::kData0; |
| 294 | |
| 295 | for (UsbFunction *function : functions_) { |
| 296 | function->HandleReset(); |
| 297 | } |
| 298 | |
| 299 | MutableBdtEntry(0, Direction::kRx, EvenOdd::kEven)->buffer_descriptor = |
| 300 | M_USB_BD_OWN | M_USB_BD_DTS | V_USB_BD_BC(kEndpoint0MaxSize); |
| 301 | MutableBdtEntry(0, Direction::kRx, EvenOdd::kEven)->address = |
| 302 | &endpoint0_receive_buffer_[0][0]; |
| 303 | |
| 304 | MutableBdtEntry(0, Direction::kRx, EvenOdd::kOdd)->buffer_descriptor = |
| 305 | M_USB_BD_OWN | M_USB_BD_DTS | V_USB_BD_BC(kEndpoint0MaxSize); |
| 306 | MutableBdtEntry(0, Direction::kRx, EvenOdd::kOdd)->address = |
| 307 | &endpoint0_receive_buffer_[1][0]; |
| 308 | |
| 309 | MutableBdtEntry(0, Direction::kTx, EvenOdd::kEven)->buffer_descriptor = 0; |
| 310 | MutableBdtEntry(0, Direction::kTx, EvenOdd::kOdd)->buffer_descriptor = 0; |
| 311 | |
| 312 | USB0_ENDPT0 = USB_ENDPT_EPRXEN | USB_ENDPT_EPTXEN | USB_ENDPT_EPHSHK; |
| 313 | |
| 314 | ClearInterrupts(); |
| 315 | |
| 316 | // Set the address to 0 for enumeration. |
| 317 | USB0_ADDR = 0; |
| 318 | new_address_ = 0; |
| 319 | |
| 320 | endpoint0_data_ = nullptr; |
| 321 | endpoint0_data_left_ = 0; |
| 322 | |
| 323 | USB0_INTEN = usb_enabled_interrupts(); |
| 324 | USB0_ERREN = USB_ERREN_BTSERREN | USB_ERREN_DMAERREN | |
| 325 | USB_ERREN_BTOERREN | USB_ERREN_DFN8EN | USB_ERREN_CRC16EN | |
| 326 | USB_ERREN_CRC5EOFEN | USB_ERREN_PIDERREN; |
| 327 | |
| 328 | // Start the peripheral going. |
| 329 | dma_memory_barrier(); |
| 330 | USB0_CTL = USB_CTL_USBENSOFEN; |
| 331 | |
| 332 | continue; |
| 333 | } |
| 334 | |
| 335 | // TODO(Brian): Handle errors more intelligently. |
| 336 | if (status & USB_ISTAT_ERROR) { |
| 337 | const uint8_t error = USB0_ERRSTAT; |
| 338 | USB0_ERRSTAT = error; |
| 339 | USB0_ISTAT = USB_ISTAT_ERROR; |
| 340 | } |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | void UsbDevice::HandleEndpoint0Token(const uint8_t stat) { |
| 345 | BdtEntry *const bdt_entry = MutableBdtEntryFromStat(stat); |
| 346 | const UsbPid pid = G_USB_BD_PID(bdt_entry->buffer_descriptor); |
| 347 | switch (pid) { |
| 348 | case UsbPid::kSetup: |
| 349 | // Unstall it if it was previously stalled. |
| 350 | USB0_ENDPT0 = USB_ENDPT_EPRXEN | USB_ENDPT_EPTXEN | USB_ENDPT_EPHSHK; |
| 351 | |
| 352 | SetupPacket setup_packet; |
| 353 | memcpy(&setup_packet, bdt_entry->address, sizeof(setup_packet)); |
| 354 | |
| 355 | // Give the buffer back now. |
| 356 | dma_memory_barrier(); |
| 357 | // Next IN and OUT packet for this endpoint (data stage/status stage) |
| 358 | // should both be DATA1. |
Brian Silverman | f91524f | 2017-09-23 13:15:55 -0400 | [diff] [blame] | 359 | MutableBdtEntryFromStat(stat ^ M_USB_STAT_ODD)->buffer_descriptor = |
| 360 | M_USB_BD_OWN | M_USB_BD_DTS | V_USB_BD_BC(kEndpoint0MaxSize) | |
| 361 | M_USB_BD_DATA1; |
| 362 | endpoint0_tx_toggle_ = Data01::kData1; |
| 363 | |
Brian Silverman | 69f96c2 | 2017-11-01 02:54:02 -0400 | [diff] [blame] | 364 | // Give this buffer back. It should be DATA0 because it'll be the second |
| 365 | // received packet. |
| 366 | bdt_entry->buffer_descriptor = |
| 367 | M_USB_BD_OWN | M_USB_BD_DTS | V_USB_BD_BC(kEndpoint0MaxSize); |
| 368 | |
Brian Silverman | f91524f | 2017-09-23 13:15:55 -0400 | [diff] [blame] | 369 | // TODO(Brian): Tell the functions a new setup packet is starting. |
| 370 | // CdcTty: next_endpoint0_out_ = NextEndpoint0Out::kNone; |
| 371 | |
| 372 | // Forget about any pending transactions on this endpoint. There shouldn't |
| 373 | // be any, so if we think there are something's out of sync and we should |
| 374 | // just drop it. Important to do this before clearing TXD_SUSPEND in |
| 375 | // USBx_CTL. Standard says "If a Setup transaction is received by an |
| 376 | // endpoint before a previously initiated control transfer is completed, |
| 377 | // the device must abort the current transfer/operation". |
| 378 | endpoint0_data_ = nullptr; |
| 379 | endpoint0_data_left_ = 0; |
| 380 | MutableBdtEntry(0, Direction::kTx, EvenOdd::kEven)->buffer_descriptor = 0; |
| 381 | MutableBdtEntry(0, Direction::kTx, EvenOdd::kOdd)->buffer_descriptor = 0; |
| 382 | |
| 383 | HandleEndpoint0SetupPacket(setup_packet); |
| 384 | |
| 385 | break; |
| 386 | |
| 387 | case UsbPid::kOut: |
| 388 | for (UsbFunction *function : functions_) { |
| 389 | switch (function->HandleEndpoint0OutPacket( |
| 390 | bdt_entry->address, G_USB_BD_BC(bdt_entry->buffer_descriptor))) { |
| 391 | case SetupResponse::kIgnored: |
| 392 | break; |
| 393 | case SetupResponse::kHandled: |
| 394 | dma_memory_barrier(); |
| 395 | bdt_entry->buffer_descriptor = |
| 396 | M_USB_BD_OWN | M_USB_BD_DTS | V_USB_BD_BC(kEndpoint0MaxSize); |
| 397 | return; |
| 398 | case SetupResponse::kStall: |
| 399 | bdt_entry->buffer_descriptor = |
| 400 | M_USB_BD_OWN | M_USB_BD_DTS | V_USB_BD_BC(kEndpoint0MaxSize); |
| 401 | StallEndpoint0(); |
| 402 | return; |
| 403 | } |
| 404 | } |
| 405 | bdt_entry->buffer_descriptor = |
| 406 | M_USB_BD_OWN | M_USB_BD_DTS | V_USB_BD_BC(kEndpoint0MaxSize); |
| 407 | StallEndpoint0(); |
| 408 | return; |
| 409 | |
| 410 | case UsbPid::kIn: |
| 411 | // The functions are allowed to queue data in {endpoint0_data_, |
| 412 | // endpoint0_data_left_}, so this case deals with sending their data too. |
| 413 | |
| 414 | // An IN transaction completed, so set up for the next one if appropriate. |
| 415 | if (!BufferEndpoint0TxPacket()) { |
| 416 | // After we're done, any further requests from the host should result in |
| 417 | // stalls (until the next setup token). |
| 418 | // TODO(Brian): Keep track of which direction it is and how much we've |
| 419 | // finished so we actually know when to stall it, both here and for |
| 420 | // kOut tokens. |
| 421 | //StallEndpoint0(); |
| 422 | } |
| 423 | |
| 424 | // If we have a new address, there is nothing left in the setup request |
| 425 | // besides a single IN packet forming the status stage, so we know the |
| 426 | // changes must be done now. |
| 427 | if (new_address_ != 0) { |
| 428 | USB0_ADDR = new_address_; |
| 429 | new_address_ = 0; |
| 430 | } |
| 431 | |
| 432 | break; |
| 433 | |
| 434 | default: |
| 435 | // Should never happen, but give the buffer back anyways if necessary. |
| 436 | if (!(bdt_entry->buffer_descriptor & M_USB_BD_OWN)) { |
| 437 | bdt_entry->buffer_descriptor = |
| 438 | M_USB_BD_OWN | M_USB_BD_DTS | V_USB_BD_BC(kEndpoint0MaxSize); |
| 439 | } |
| 440 | break; |
| 441 | } |
| 442 | |
| 443 | // Clear the TXD_SUSPEND flag. |
| 444 | dma_memory_barrier(); |
| 445 | USB0_CTL = USB_CTL_USBENSOFEN; |
| 446 | } |
| 447 | |
| 448 | void UsbDevice::HandleEndpoint0SetupPacket(const SetupPacket &setup_packet) { |
| 449 | const bool in = setup_packet.request_type & M_SETUP_REQUEST_TYPE_IN; |
| 450 | const uint8_t recipient = |
| 451 | G_SETUP_REQUEST_TYPE_RECIPIENT(setup_packet.request_type); |
| 452 | switch (G_SETUP_REQUEST_TYPE_TYPE(setup_packet.request_type)) { |
| 453 | case SetupRequestType::kStandard: |
| 454 | switch (setup_packet.request) { |
| 455 | case standard_setup_requests::kSetAddress: |
| 456 | if (in || recipient != standard_setup_recipients::kDevice || |
| 457 | setup_packet.index != 0 || setup_packet.length != 0) { |
| 458 | break; |
| 459 | } |
| 460 | new_address_ = setup_packet.value; |
| 461 | SendEmptyEndpoint0Packet(); |
| 462 | return; |
| 463 | |
| 464 | case standard_setup_requests::kSetConfiguration: |
| 465 | if (in || recipient != standard_setup_recipients::kDevice || |
| 466 | setup_packet.index != 0 || setup_packet.length != 0) { |
| 467 | break; |
| 468 | } |
| 469 | configuration_ = setup_packet.value; |
| 470 | |
| 471 | // No need to mess with endpoint0_tx_toggle_ because we reset it with |
| 472 | // each setup packet anyways. |
| 473 | |
| 474 | for (int endpoint = 0; |
| 475 | endpoint < static_cast<int>(endpoint_mapping_.size()); |
| 476 | ++endpoint) { |
| 477 | if (endpoint_mapping_[endpoint]) { |
| 478 | endpoint_mapping_[endpoint]->HandleConfigured(endpoint); |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | SendEmptyEndpoint0Packet(); |
| 483 | return; |
| 484 | |
| 485 | case standard_setup_requests::kClearFeature: |
| 486 | if (in || setup_packet.length != 0) { |
| 487 | break; |
| 488 | } |
| 489 | if (recipient == standard_setup_recipients::kEndpoint && |
| 490 | setup_packet.value == standard_feature_selectors::kEndpointHalt) { |
| 491 | const int endpoint = |
| 492 | G_SETUP_REQUEST_INDEX_ENDPOINT(setup_packet.index); |
| 493 | // Our endpoint 0 doesn't support the halt feature because that's |
| 494 | // weird and not recommended by the standard. |
| 495 | if (endpoint == 0) { |
| 496 | break; |
| 497 | } |
| 498 | if (endpoint >= number_endpoints()) { |
| 499 | break; |
| 500 | } |
| 501 | USB0_ENDPTn(endpoint) &= ~USB_ENDPT_EPSTALL; |
| 502 | if (endpoint_mapping_[endpoint] != nullptr) { |
| 503 | endpoint_mapping_[endpoint]->HandleConfigured(endpoint); |
| 504 | } |
| 505 | SendEmptyEndpoint0Packet(); |
| 506 | return; |
| 507 | } |
| 508 | // We should never get kDeviceRemoteWakeup because we don't advertise |
| 509 | // support for it in our configuration descriptors. |
| 510 | // We should never get kTestMode because we're not high-speed. |
| 511 | break; |
| 512 | |
| 513 | case standard_setup_requests::kSetFeature: |
| 514 | if (in || setup_packet.length != 0) { |
| 515 | break; |
| 516 | } |
| 517 | if (recipient == standard_setup_recipients::kEndpoint && |
| 518 | setup_packet.value == standard_feature_selectors::kEndpointHalt) { |
| 519 | const int endpoint = |
| 520 | G_SETUP_REQUEST_INDEX_ENDPOINT(setup_packet.index); |
| 521 | // Our endpoint 0 doesn't support the halt feature because that's |
| 522 | // weird and not recommended by the standard. |
| 523 | if (endpoint == 0) { |
| 524 | break; |
| 525 | } |
| 526 | if (endpoint >= number_endpoints()) { |
| 527 | break; |
| 528 | } |
| 529 | StallEndpoint(endpoint); |
| 530 | // TODO(Brian): Tell the appropriate function it's now stalled. |
| 531 | SendEmptyEndpoint0Packet(); |
| 532 | return; |
| 533 | } |
| 534 | // We should never get kDeviceRemoteWakeup because we don't advertise |
| 535 | // support for it in our configuration descriptors. |
| 536 | // We should never get kTestMode because we're not high-speed. |
| 537 | break; |
| 538 | |
| 539 | case standard_setup_requests::kGetConfiguration: |
| 540 | if (!in || recipient != standard_setup_recipients::kDevice || |
| 541 | setup_packet.index != 0 || setup_packet.length != 1) { |
| 542 | break; |
| 543 | } |
| 544 | endpoint0_transmit_buffer_[0] = configuration_; |
| 545 | QueueEndpoint0Data(endpoint0_transmit_buffer_, 1); |
| 546 | return; |
| 547 | |
| 548 | case standard_setup_requests::kGetInterface: |
| 549 | if (!in || recipient != standard_setup_recipients::kInterface || |
| 550 | setup_packet.value != 0 || setup_packet.length != 1) { |
| 551 | break; |
| 552 | } |
| 553 | // Standard says it's unspecified in the default state and must |
| 554 | // respond with an error in the address state, so just do an error for |
| 555 | // both of them. |
| 556 | if (configuration_ == 0) { |
| 557 | break; |
| 558 | } |
| 559 | // TODO(Brian): Ask the appropriate function what alternate setting |
| 560 | // the interface has, and stall if there isn't one. |
| 561 | endpoint0_transmit_buffer_[0] = 0; |
| 562 | QueueEndpoint0Data(endpoint0_transmit_buffer_, 1); |
| 563 | return; |
| 564 | |
| 565 | case standard_setup_requests::kSetInterface: |
| 566 | if (in || recipient != standard_setup_recipients::kInterface || |
| 567 | setup_packet.length != 0) { |
| 568 | break; |
| 569 | } |
| 570 | // Standard says it's unspecified in the default state and must |
| 571 | // respond with an error in the address state, so just do an error for |
| 572 | // both of them. |
| 573 | if (configuration_ == 0) { |
| 574 | break; |
| 575 | } |
| 576 | |
| 577 | // TODO(Brian): Pass to the appropriate function instead. |
| 578 | if (setup_packet.value != 0) { |
| 579 | break; |
| 580 | } |
| 581 | SendEmptyEndpoint0Packet(); |
| 582 | return; |
| 583 | |
| 584 | case standard_setup_requests::kGetStatus: |
| 585 | if (!in || setup_packet.value != 0 || setup_packet.length != 2) { |
| 586 | break; |
| 587 | } |
| 588 | if (recipient == standard_setup_recipients::kDevice) { |
| 589 | if (setup_packet.index != 0) { |
| 590 | break; |
| 591 | } |
| 592 | // Say that we're currently self powered. |
| 593 | endpoint0_transmit_buffer_[0] = 1; |
| 594 | endpoint0_transmit_buffer_[1] = 0; |
| 595 | QueueEndpoint0Data(endpoint0_transmit_buffer_, 2); |
| 596 | return; |
| 597 | } |
| 598 | if ((recipient == standard_setup_recipients::kInterface && |
| 599 | setup_packet.index == 0) || |
| 600 | (recipient == standard_setup_recipients::kEndpoint && |
| 601 | G_SETUP_REQUEST_INDEX_ENDPOINT(setup_packet.index) == 0)) { |
| 602 | endpoint0_transmit_buffer_[0] = 0; |
| 603 | endpoint0_transmit_buffer_[1] = 0; |
| 604 | QueueEndpoint0Data(endpoint0_transmit_buffer_, 2); |
| 605 | return; |
| 606 | } |
| 607 | // Standard says it's unspecified in the default state and must |
| 608 | // respond with an error in the address state, so just do an error |
| 609 | // for both of them. |
| 610 | if (configuration_ == 0) { |
| 611 | break; |
| 612 | } |
| 613 | |
| 614 | if (recipient == standard_setup_recipients::kInterface) { |
| 615 | // TODO(Brian): Check if it's actually an interface we have? |
| 616 | endpoint0_transmit_buffer_[0] = 0; |
| 617 | endpoint0_transmit_buffer_[1] = 0; |
| 618 | QueueEndpoint0Data(endpoint0_transmit_buffer_, 2); |
| 619 | return; |
| 620 | } |
| 621 | |
| 622 | if (recipient == standard_setup_recipients::kEndpoint) { |
| 623 | const int endpoint = |
| 624 | G_SETUP_REQUEST_INDEX_ENDPOINT(setup_packet.index); |
| 625 | // TODO(Brian): Check if it's actually an endpoint we have? |
| 626 | if (USB0_ENDPTn(endpoint) & USB_ENDPT_EPSTALL) { |
| 627 | endpoint0_transmit_buffer_[0] = 1; |
| 628 | } else { |
| 629 | endpoint0_transmit_buffer_[0] = 0; |
| 630 | } |
| 631 | endpoint0_transmit_buffer_[1] = 0; |
| 632 | QueueEndpoint0Data(endpoint0_transmit_buffer_, 2); |
| 633 | return; |
| 634 | } |
| 635 | break; |
| 636 | |
| 637 | case standard_setup_requests::kSetDescriptor: |
| 638 | // Not implementing anything for this. |
| 639 | break; |
| 640 | |
| 641 | case standard_setup_requests::kSynchFrame: |
| 642 | // We don't implement any classes which use this. |
| 643 | break; |
| 644 | |
| 645 | case standard_setup_requests::kGetDescriptor: |
Brian Silverman | d930f28 | 2017-11-04 23:09:12 -0400 | [diff] [blame] | 646 | if (!in) { |
Brian Silverman | f91524f | 2017-09-23 13:15:55 -0400 | [diff] [blame] | 647 | break; |
| 648 | } |
Brian Silverman | d930f28 | 2017-11-04 23:09:12 -0400 | [diff] [blame] | 649 | |
Brian Silverman | f91524f | 2017-09-23 13:15:55 -0400 | [diff] [blame] | 650 | const uint8_t descriptor_type_byte = (setup_packet.value >> 8) & 0xFF; |
Brian Silverman | d930f28 | 2017-11-04 23:09:12 -0400 | [diff] [blame] | 651 | if (G_DESCRIPTOR_TYPE_TYPE(descriptor_type_byte) != |
| 652 | standard_descriptor_type_types::kStandard) { |
| 653 | for (UsbFunction *function : functions_) { |
| 654 | switch (function->HandleGetDescriptor(setup_packet)) { |
| 655 | case SetupResponse::kIgnored: |
| 656 | continue; |
| 657 | case SetupResponse::kHandled: |
| 658 | return; |
| 659 | case SetupResponse::kStall: |
| 660 | break; |
| 661 | } |
| 662 | break; |
| 663 | } |
| 664 | break; |
| 665 | } |
| 666 | |
| 667 | if (recipient != standard_setup_recipients::kDevice) { |
| 668 | break; |
| 669 | } |
Brian Silverman | f91524f | 2017-09-23 13:15:55 -0400 | [diff] [blame] | 670 | if (descriptor_type_byte < kUsbDescriptorTypeMin || |
| 671 | descriptor_type_byte > kUsbDescriptorTypeMax) { |
| 672 | break; |
| 673 | } |
| 674 | const UsbDescriptorType descriptor_type = |
| 675 | static_cast<UsbDescriptorType>(descriptor_type_byte); |
| 676 | const uint8_t descriptor_index = setup_packet.value & 0xFF; |
| 677 | switch (descriptor_type) { |
| 678 | case UsbDescriptorType::kDevice: |
| 679 | if (setup_packet.index != 0 || descriptor_index != 0) { |
| 680 | break; |
| 681 | } |
| 682 | QueueEndpoint0Data( |
| 683 | device_descriptor_list_.data_.data(), |
| 684 | ::std::min<int>(setup_packet.length, |
| 685 | device_descriptor_list_.data_.size())); |
| 686 | return; |
| 687 | |
| 688 | case UsbDescriptorType::kConfiguration: |
| 689 | if (setup_packet.index != 0 || descriptor_index != 0) { |
| 690 | break; |
| 691 | } |
| 692 | QueueEndpoint0Data( |
| 693 | config_descriptor_list_.data_.data(), |
| 694 | ::std::min<int>(setup_packet.length, |
| 695 | config_descriptor_list_.data_.size())); |
| 696 | return; |
| 697 | |
| 698 | case UsbDescriptorType::kString: |
Brian Silverman | d930f28 | 2017-11-04 23:09:12 -0400 | [diff] [blame] | 699 | if (descriptor_index != 0 && |
| 700 | setup_packet.index != english_us_code()) { |
Brian Silverman | f91524f | 2017-09-23 13:15:55 -0400 | [diff] [blame] | 701 | break; |
| 702 | } |
| 703 | if (descriptor_index >= strings_.size()) { |
| 704 | break; |
| 705 | } |
| 706 | QueueEndpoint0Data( |
| 707 | strings_[descriptor_index].data(), |
| 708 | ::std::min<int>(setup_packet.length, |
| 709 | strings_[descriptor_index].size())); |
| 710 | return; |
| 711 | |
| 712 | default: |
| 713 | // TODO(Brian): Handle other types of descriptor too. |
| 714 | break; |
| 715 | } |
| 716 | } |
| 717 | break; |
| 718 | |
| 719 | default: |
| 720 | for (UsbFunction *function : functions_) { |
| 721 | switch (function->HandleEndpoint0SetupPacket(setup_packet)) { |
| 722 | case SetupResponse::kIgnored: |
| 723 | continue; |
| 724 | case SetupResponse::kHandled: |
| 725 | return; |
| 726 | case SetupResponse::kStall: |
| 727 | break; |
| 728 | } |
| 729 | break; |
| 730 | } |
| 731 | break; |
| 732 | } |
| 733 | |
| 734 | StallEndpoint0(); |
| 735 | } |
| 736 | |
| 737 | // We're supposed to continue returning stalls until the next kSetup packet. |
| 738 | // Code might continue putting stuff in the TX buffers, but the hardware won't |
| 739 | // actually send it as long as the EPSTALL bit is set. |
| 740 | void UsbDevice::StallEndpoint0() { |
| 741 | USB0_ENDPT0 = USB_ENDPT_EPSTALL | USB_ENDPT_EPRXEN | USB_ENDPT_EPTXEN | |
| 742 | USB_ENDPT_EPHSHK; |
| 743 | } |
| 744 | |
| 745 | bool UsbDevice::BufferEndpoint0TxPacket() { |
| 746 | if (endpoint0_data_ == nullptr) { |
| 747 | return false; |
| 748 | } |
| 749 | |
| 750 | const int to_transmit = ::std::min(endpoint0_data_left_, kEndpoint0MaxSize); |
| 751 | BdtEntry *const tx_bdt_entry = |
| 752 | MutableBdtEntry(0, Direction::kTx, endpoint0_tx_odd_); |
| 753 | // const_cast is safe because the hardware is only going to read from |
| 754 | // this, not write. |
| 755 | tx_bdt_entry->address = |
| 756 | const_cast<void *>(static_cast<const void *>(endpoint0_data_)); |
| 757 | dma_memory_barrier(); |
| 758 | tx_bdt_entry->buffer_descriptor = |
| 759 | V_USB_BD_BC(to_transmit) | static_cast<uint32_t>(endpoint0_tx_toggle_) | |
| 760 | M_USB_BD_OWN | M_USB_BD_DTS; |
| 761 | |
| 762 | endpoint0_tx_odd_ = EvenOddInverse(endpoint0_tx_odd_); |
| 763 | endpoint0_tx_toggle_ = Data01Inverse(endpoint0_tx_toggle_); |
| 764 | |
| 765 | endpoint0_data_ += to_transmit; |
| 766 | endpoint0_data_left_ -= to_transmit; |
| 767 | if (to_transmit < kEndpoint0MaxSize) { |
| 768 | endpoint0_data_ = nullptr; |
| 769 | } |
| 770 | |
| 771 | return true; |
| 772 | } |
| 773 | |
| 774 | void UsbDevice::SendEmptyEndpoint0Packet() { |
| 775 | // Really doesn't matter what we put here as long as it's not nullptr. |
| 776 | endpoint0_data_ = reinterpret_cast<char *>(this); |
| 777 | endpoint0_data_left_ = 0; |
| 778 | BufferEndpoint0TxPacket(); |
| 779 | } |
| 780 | |
| 781 | void UsbDevice::QueueEndpoint0Data(const char *data, int size) { |
| 782 | endpoint0_data_ = data; |
| 783 | endpoint0_data_left_ = size; |
| 784 | // There are 2 TX buffers, so fill them both up. |
| 785 | BufferEndpoint0TxPacket(); |
| 786 | BufferEndpoint0TxPacket(); |
| 787 | } |
| 788 | |
| 789 | void UsbDevice::StallEndpoint(int endpoint) { |
| 790 | for (Direction direction : {Direction::kTx, Direction::kRx}) { |
| 791 | for (EvenOdd odd : {EvenOdd::kOdd, EvenOdd::kEven}) { |
| 792 | MutableBdtEntry(endpoint, direction, odd)->buffer_descriptor = 0; |
| 793 | dma_memory_barrier(); |
| 794 | MutableBdtEntry(endpoint, direction, odd)->address = nullptr; |
| 795 | } |
| 796 | } |
| 797 | USB0_ENDPTn(endpoint) |= USB_ENDPT_EPSTALL; |
| 798 | } |
| 799 | |
| 800 | void UsbDevice::ConfigureEndpointFor(int endpoint, bool rx, bool tx, |
| 801 | bool handshake) { |
| 802 | uint8_t control = 0; |
| 803 | if (rx) { |
| 804 | control |= USB_ENDPT_EPRXEN; |
| 805 | } |
| 806 | if (tx) { |
| 807 | control |= USB_ENDPT_EPTXEN; |
| 808 | } |
| 809 | if (handshake) { |
| 810 | control |= USB_ENDPT_EPHSHK; |
| 811 | } |
| 812 | USB0_ENDPTn(endpoint) = control; |
| 813 | } |
| 814 | |
| 815 | int UsbFunction::AddEndpoint() { |
| 816 | const int r = device_->endpoint_mapping_.size(); |
| 817 | assert(r < number_endpoints()); |
| 818 | device_->endpoint_mapping_.push_back(this); |
| 819 | return r; |
| 820 | } |
| 821 | |
| 822 | int UsbFunction::AddInterface() { |
| 823 | const int r = device_->interface_mapping_.size(); |
| 824 | // bInterfaceNumber is only one byte. |
| 825 | assert(r < 255); |
| 826 | device_->interface_mapping_.push_back(this); |
| 827 | return r; |
| 828 | } |
| 829 | |
| 830 | void UsbDevice::SetBdtEntry(int endpoint, Direction direction, EvenOdd odd, |
| 831 | BdtEntry bdt_entry) { |
| 832 | *MutableBdtEntry(endpoint, direction, odd) = bdt_entry; |
| 833 | } |
| 834 | |
| 835 | } // namespace teensy |
| 836 | } // namespace frc971 |