Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 1 | #include "board.h" |
| 2 | #include "card_emu.h" |
Harald Welte | bd71768 | 2016-02-28 19:30:05 +0100 | [diff] [blame] | 3 | #include "iso7816_fidi.h" |
Harald Welte | 54cb3d0 | 2016-02-29 14:12:40 +0100 | [diff] [blame] | 4 | #include "utils.h" |
Harald Welte | acae412 | 2016-03-02 10:27:58 +0100 | [diff] [blame] | 5 | #include "linuxlist.h" |
Harald Welte | ebb80ed | 2016-03-02 13:56:59 +0100 | [diff] [blame] | 6 | #include "req_ctx.h" |
| 7 | #include "cardemu_prot.h" |
Harald Welte | bd71768 | 2016-02-28 19:30:05 +0100 | [diff] [blame] | 8 | |
| 9 | #define TRACE_ENTRY() TRACE_DEBUG("%s entering\n", __func__) |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 10 | |
| 11 | static const Pin pins_cardsim[] = PINS_CARDSIM; |
| 12 | |
| 13 | /* UART pins */ |
| 14 | static const Pin pins_usim1[] = {PINS_USIM1}; |
| 15 | static const Pin pin_usim1_rst = PIN_USIM1_nRST; |
| 16 | static const Pin pin_usim1_vcc = PIN_USIM1_VCC; |
| 17 | |
| 18 | #ifdef CARDEMU_SECOND_UART |
| 19 | static const Pin pins_usim2[] = {PINS_USIM1}; |
| 20 | static const Pin pin_usim2_rst = PIN_USIM2_nRST; |
| 21 | static const Pin pin_usim2_vcc = PIN_USIM2_VCC; |
| 22 | #endif |
| 23 | |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame^] | 24 | struct cardem_inst { |
| 25 | struct card_handle *ch; |
| 26 | struct llist_head usb_out_queue; |
| 27 | struct ringbuf rb; |
| 28 | struct Usart_info usart_info; |
| 29 | int usb_pending_old; |
| 30 | uint8_t ep_out; |
| 31 | uint8_t ep_in; |
| 32 | uint8_t ep_int; |
| 33 | }; |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 34 | |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame^] | 35 | static struct cardem_inst cardem_inst[] = { |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 36 | { |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame^] | 37 | .usart_info = { |
| 38 | .base = USART1, |
| 39 | .id = ID_USART1, |
| 40 | .state = USART_RCV |
| 41 | }, |
| 42 | .ep_out = PHONE_DATAOUT, |
| 43 | .ep_in = PHONE_DATAIN, |
| 44 | .ep_int = PHONE_INT, |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 45 | }, |
| 46 | #ifdef CARDEMU_SECOND_UART |
| 47 | { |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame^] | 48 | .usart_info = { |
| 49 | .base = USART0, |
| 50 | .id = ID_USART0, |
| 51 | .state = USART_RCV |
| 52 | }, |
| 53 | .ep_out = CARDEM_USIM2_DATAOUT, |
| 54 | .ep_in = CARDEM_USIM2_DATAIN, |
| 55 | .ep_int = CARDEM_USIM2_INT, |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 56 | }, |
| 57 | #endif |
| 58 | }; |
| 59 | |
| 60 | static Usart *get_usart_by_chan(uint8_t uart_chan) |
| 61 | { |
| 62 | switch (uart_chan) { |
| 63 | case 0: |
| 64 | return USART1; |
| 65 | #ifdef CARDEMU_SECOND_UART |
| 66 | case 1: |
| 67 | return USART0; |
| 68 | #endif |
| 69 | } |
| 70 | return NULL; |
| 71 | } |
| 72 | |
| 73 | /*********************************************************************** |
| 74 | * Call-Backs from card_emu.c |
| 75 | ***********************************************************************/ |
| 76 | |
| 77 | /* call-back from card_emu.c to enable/disable transmit and/or receive */ |
| 78 | void card_emu_uart_enable(uint8_t uart_chan, uint8_t rxtx) |
| 79 | { |
| 80 | Usart *usart = get_usart_by_chan(uart_chan); |
| 81 | switch (rxtx) { |
| 82 | case ENABLE_TX: |
Harald Welte | 9dbc46e | 2016-02-29 10:05:10 +0100 | [diff] [blame] | 83 | USART_DisableIt(usart, ~US_IER_TXRDY); |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 84 | USART_SetReceiverEnabled(usart, 0); |
Harald Welte | 8a416b1 | 2016-03-01 00:42:04 +0100 | [diff] [blame] | 85 | usart->US_CR = US_CR_RSTSTA | US_CR_RSTIT | US_CR_RSTNACK; |
| 86 | USART_EnableIt(usart, US_IER_TXRDY); |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 87 | USART_SetTransmitterEnabled(usart, 1); |
| 88 | break; |
| 89 | case ENABLE_RX: |
Harald Welte | 9dbc46e | 2016-02-29 10:05:10 +0100 | [diff] [blame] | 90 | USART_DisableIt(usart, ~US_IER_RXRDY); |
Harald Welte | 8a416b1 | 2016-03-01 00:42:04 +0100 | [diff] [blame] | 91 | USART_SetTransmitterEnabled(usart, 0); |
| 92 | usart->US_CR = US_CR_RSTSTA | US_CR_RSTIT | US_CR_RSTNACK; |
| 93 | USART_EnableIt(usart, US_IER_RXRDY); |
| 94 | USART_SetReceiverEnabled(usart, 1); |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 95 | break; |
| 96 | case 0: |
| 97 | default: |
| 98 | USART_SetTransmitterEnabled(usart, 0); |
| 99 | USART_SetReceiverEnabled(usart, 0); |
Harald Welte | 9dbc46e | 2016-02-29 10:05:10 +0100 | [diff] [blame] | 100 | USART_DisableIt(usart, 0xFFFFFFFF); |
Harald Welte | 8a416b1 | 2016-03-01 00:42:04 +0100 | [diff] [blame] | 101 | usart->US_CR = US_CR_RSTSTA | US_CR_RSTIT | US_CR_RSTNACK; |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 102 | break; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /* call-back from card_emu.c to transmit a byte */ |
| 107 | int card_emu_uart_tx(uint8_t uart_chan, uint8_t byte) |
| 108 | { |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame^] | 109 | Usart *usart = get_usart_by_chan(uart_chan); |
| 110 | #if 0 |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 111 | Usart_info *ui = &usart_info[uart_chan]; |
| 112 | ISO7816_SendChar(byte, ui); |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame^] | 113 | #else |
| 114 | int i = 0; |
| 115 | while ((usart->US_CSR & (US_CSR_TXRDY)) == 0) { |
| 116 | if (!(i%1000000)) { |
| 117 | printf("s: %x %02X", usart->US_CSR, usart->US_RHR & 0xFF); |
| 118 | usart->US_CR = US_CR_RSTTX; |
| 119 | usart->US_CR = US_CR_RSTRX; |
| 120 | } |
| 121 | } |
| 122 | usart->US_THR = byte; |
| 123 | TRACE_ERROR("Sx%02x\r\n", byte); |
| 124 | #endif |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 125 | return 1; |
| 126 | } |
| 127 | |
| 128 | |
| 129 | /* FIXME: integrate this with actual irq handler */ |
Harald Welte | c0bd7f0 | 2016-02-29 10:13:33 +0100 | [diff] [blame] | 130 | void usart_irq_rx(uint8_t uart) |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 131 | { |
Harald Welte | c0bd7f0 | 2016-02-29 10:13:33 +0100 | [diff] [blame] | 132 | Usart *usart = get_usart_by_chan(uart); |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame^] | 133 | struct cardem_inst *ci = &cardem_inst[0]; |
Harald Welte | c0bd7f0 | 2016-02-29 10:13:33 +0100 | [diff] [blame] | 134 | uint32_t csr; |
| 135 | uint8_t byte = 0; |
| 136 | |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 137 | #ifdef CARDEMU_SECOND_UART |
Harald Welte | c0bd7f0 | 2016-02-29 10:13:33 +0100 | [diff] [blame] | 138 | if (uart == 1) |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame^] | 139 | ci = &cardem_inst[1]; |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 140 | #endif |
Harald Welte | c0bd7f0 | 2016-02-29 10:13:33 +0100 | [diff] [blame] | 141 | csr = usart->US_CSR; |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 142 | |
Harald Welte | c0bd7f0 | 2016-02-29 10:13:33 +0100 | [diff] [blame] | 143 | if (csr & US_CSR_RXRDY) { |
| 144 | byte = (usart->US_RHR) & 0xFF; |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame^] | 145 | rbuf_write(&ci->rb, byte); |
Harald Welte | c0bd7f0 | 2016-02-29 10:13:33 +0100 | [diff] [blame] | 146 | } |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 147 | |
Harald Welte | 12d4bdf | 2016-03-02 10:31:03 +0100 | [diff] [blame] | 148 | if (csr & US_CSR_TXRDY) { |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame^] | 149 | if (card_emu_tx_byte(ci->ch) == 0) |
Harald Welte | 12d4bdf | 2016-03-02 10:31:03 +0100 | [diff] [blame] | 150 | USART_DisableIt(usart, US_IER_TXRDY); |
| 151 | } |
| 152 | |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 153 | if (csr & (US_CSR_OVRE|US_CSR_FRAME|US_CSR_PARE| |
| 154 | US_CSR_TIMEOUT|US_CSR_NACK|(1<<10))) { |
Harald Welte | c0bd7f0 | 2016-02-29 10:13:33 +0100 | [diff] [blame] | 155 | usart->US_CR = US_CR_RSTSTA | US_CR_RSTIT | US_CR_RSTNACK; |
| 156 | TRACE_ERROR("e 0x%x st: 0x%x\n", byte, csr); |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
Harald Welte | bd71768 | 2016-02-28 19:30:05 +0100 | [diff] [blame] | 160 | /* call-back from card_emu.c to change UART baud rate */ |
| 161 | int card_emu_uart_update_fidi(uint8_t uart_chan, unsigned int fidi) |
| 162 | { |
| 163 | int rc; |
Harald Welte | bd71768 | 2016-02-28 19:30:05 +0100 | [diff] [blame] | 164 | Usart *usart = get_usart_by_chan(uart_chan); |
| 165 | |
Harald Welte | 99f62a6 | 2016-02-29 10:08:49 +0100 | [diff] [blame] | 166 | usart->US_CR |= US_CR_RXDIS | US_CR_RSTRX; |
| 167 | usart->US_FIDI = fidi & 0x3ff; |
| 168 | usart->US_CR |= US_CR_RXEN | US_CR_STTTO; |
| 169 | return 0; |
Harald Welte | bd71768 | 2016-02-28 19:30:05 +0100 | [diff] [blame] | 170 | } |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 171 | |
| 172 | /*********************************************************************** |
| 173 | * Core USB / mainloop integration |
| 174 | ***********************************************************************/ |
| 175 | |
Harald Welte | c0bd7f0 | 2016-02-29 10:13:33 +0100 | [diff] [blame] | 176 | static void usim1_rst_irqhandler(const Pin *pPin) |
| 177 | { |
| 178 | int active = PIO_Get(&pin_usim1_rst) ? 0 : 1; |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame^] | 179 | card_emu_io_statechg(cardem_inst[0].ch, CARD_IO_RST, active); |
Harald Welte | c0bd7f0 | 2016-02-29 10:13:33 +0100 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | static void usim1_vcc_irqhandler(const Pin *pPin) |
| 183 | { |
| 184 | int active = PIO_Get(&pin_usim1_vcc) ? 1 : 0; |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame^] | 185 | card_emu_io_statechg(cardem_inst[0].ch, CARD_IO_VCC, active); |
Harald Welte | c0bd7f0 | 2016-02-29 10:13:33 +0100 | [diff] [blame] | 186 | /* FIXME do this for real */ |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame^] | 187 | card_emu_io_statechg(cardem_inst[0].ch, CARD_IO_CLK, active); |
Harald Welte | c0bd7f0 | 2016-02-29 10:13:33 +0100 | [diff] [blame] | 188 | } |
| 189 | |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame^] | 190 | #ifdef CARDEMU_SECOND_UART |
| 191 | static void usim2_rst_irqhandler(const Pin *pPin) |
| 192 | { |
| 193 | int active = PIO_Get(&pin_usim2_rst) ? 0 : 1; |
| 194 | card_emu_io_statechg(cardem_inst[1].ch, CARD_IO_RST, active); |
| 195 | } |
| 196 | |
| 197 | static void usim2_vcc_irqhandler(const Pin *pPin) |
| 198 | { |
| 199 | int active = PIO_Get(&pin_usim2_vcc) ? 1 : 0; |
| 200 | card_emu_io_statechg(cardem_inst[1].ch, CARD_IO_VCC, active); |
| 201 | /* FIXME do this for real */ |
| 202 | card_emu_io_statechg(cardem_inst[1].ch, CARD_IO_CLK, active); |
| 203 | } |
| 204 | #endif |
| 205 | |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 206 | /* executed once at system boot for each config */ |
| 207 | void mode_cardemu_configure(void) |
| 208 | { |
Harald Welte | bd71768 | 2016-02-28 19:30:05 +0100 | [diff] [blame] | 209 | TRACE_ENTRY(); |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | /* called if config is activated */ |
| 213 | void mode_cardemu_init(void) |
| 214 | { |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame^] | 215 | int i; |
Harald Welte | bd71768 | 2016-02-28 19:30:05 +0100 | [diff] [blame] | 216 | |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame^] | 217 | TRACE_ENTRY(); |
Harald Welte | c0bd7f0 | 2016-02-29 10:13:33 +0100 | [diff] [blame] | 218 | |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 219 | PIO_Configure(pins_cardsim, PIO_LISTSIZE(pins_cardsim)); |
| 220 | |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame^] | 221 | INIT_LLIST_HEAD(&cardem_inst[0].usb_out_queue); |
| 222 | rbuf_reset(&cardem_inst[0].rb); |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 223 | PIO_Configure(pins_usim1, PIO_LISTSIZE(pins_usim1)); |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame^] | 224 | ISO7816_Init(&cardem_inst[0].usart_info, CLK_SLAVE); |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 225 | NVIC_EnableIRQ(USART1_IRQn); |
Harald Welte | c0bd7f0 | 2016-02-29 10:13:33 +0100 | [diff] [blame] | 226 | PIO_ConfigureIt(&pin_usim1_rst, usim1_rst_irqhandler); |
| 227 | PIO_EnableIt(&pin_usim1_rst); |
| 228 | PIO_ConfigureIt(&pin_usim1_vcc, usim1_vcc_irqhandler); |
| 229 | PIO_EnableIt(&pin_usim1_vcc); |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame^] | 230 | cardem_inst[0].ch = card_emu_init(0, 2, 0); |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 231 | |
| 232 | #ifdef CARDEMU_SECOND_UART |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame^] | 233 | INIT_LLIST_HEAD(&cardem_inst[1].usb_out_queue); |
| 234 | rbuf_reset(&cardem_inst[1].rb); |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 235 | PIO_Configure(pins_usim2, PIO_LISTSIZE(pins_usim2)); |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame^] | 236 | ISO7816_Init(&cardem_inst[1].usart_info, CLK_SLAVE); |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 237 | NVIC_EnableIRQ(USART0_IRQn); |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame^] | 238 | PIO_ConfigureIt(&pin_usim2_rst, usim2_rst_irqhandler); |
| 239 | PIO_EnableIt(&pin_usim2_rst); |
| 240 | PIO_ConfigureIt(&pin_usim2_vcc, usim2_vcc_irqhandler); |
| 241 | PIO_EnableIt(&pin_usim2_vcc); |
| 242 | cardem_inst[1].ch = card_emu_init(1, 0, 1); |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 243 | #endif |
| 244 | } |
| 245 | |
| 246 | /* called if config is deactivated */ |
| 247 | void mode_cardemu_exit(void) |
| 248 | { |
Harald Welte | bd71768 | 2016-02-28 19:30:05 +0100 | [diff] [blame] | 249 | TRACE_ENTRY(); |
| 250 | |
Harald Welte | c0bd7f0 | 2016-02-29 10:13:33 +0100 | [diff] [blame] | 251 | PIO_DisableIt(&pin_usim1_rst); |
| 252 | PIO_DisableIt(&pin_usim1_vcc); |
| 253 | |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 254 | NVIC_DisableIRQ(USART1_IRQn); |
| 255 | USART_SetTransmitterEnabled(USART1, 0); |
| 256 | USART_SetReceiverEnabled(USART1, 0); |
| 257 | |
| 258 | #ifdef CARDEMU_SECOND_UART |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame^] | 259 | PIO_DisableIt(&pin_usim2_rst); |
| 260 | PIO_DisableIt(&pin_usim2_vcc); |
| 261 | |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 262 | NVIC_DisableIRQ(USART0_IRQn); |
| 263 | USART_SetTransmitterEnabled(USART0, 0); |
| 264 | USART_SetReceiverEnabled(USART0, 0); |
| 265 | #endif |
| 266 | } |
| 267 | |
Harald Welte | acae412 | 2016-03-02 10:27:58 +0100 | [diff] [blame] | 268 | static int llist_count(struct llist_head *head) |
| 269 | { |
| 270 | struct llist_head *list; |
| 271 | int i = 0; |
| 272 | |
| 273 | llist_for_each(list, head) |
| 274 | i++; |
| 275 | |
| 276 | return i; |
| 277 | } |
| 278 | |
Harald Welte | ebb80ed | 2016-03-02 13:56:59 +0100 | [diff] [blame] | 279 | /* handle a single USB command as received from the USB host */ |
| 280 | static void dispatch_usb_command(struct req_ctx *rctx, struct card_handle *ch) |
| 281 | { |
| 282 | struct cardemu_usb_msg_hdr *hdr; |
Harald Welte | 06b27f6 | 2016-03-02 14:26:38 +0100 | [diff] [blame] | 283 | struct cardemu_usb_msg_set_atr *atr; |
Harald Welte | ebb80ed | 2016-03-02 13:56:59 +0100 | [diff] [blame] | 284 | struct llist_head *queue; |
| 285 | |
| 286 | hdr = (struct cardemu_usb_msg_hdr *) rctx->data; |
| 287 | switch (hdr->msg_type) { |
| 288 | case CEMU_USB_MSGT_DT_TX_DATA: |
| 289 | queue = card_emu_get_uart_tx_queue(ch); |
| 290 | req_ctx_set_state(rctx, RCTX_S_UART_TX_PENDING); |
| 291 | llist_add_tail(&rctx->list, queue); |
| 292 | card_emu_have_new_uart_tx(ch); |
| 293 | break; |
| 294 | case CEMU_USB_MSGT_DT_SET_ATR: |
Harald Welte | 06b27f6 | 2016-03-02 14:26:38 +0100 | [diff] [blame] | 295 | atr = (struct cardemu_usb_msg_set_atr *) hdr; |
| 296 | card_emu_set_atr(ch, atr->atr, hdr->data_len); |
| 297 | llist_del(&rctx->list); |
| 298 | req_ctx_put(rctx); |
| 299 | break; |
Harald Welte | ebb80ed | 2016-03-02 13:56:59 +0100 | [diff] [blame] | 300 | case CEMU_USB_MSGT_DT_GET_STATS: |
| 301 | case CEMU_USB_MSGT_DT_GET_STATUS: |
| 302 | default: |
| 303 | /* FIXME */ |
| 304 | break; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | /* iterate over the queue of incoming USB commands and dispatch/execute |
| 309 | * them */ |
| 310 | static void process_any_usb_commands(struct llist_head *main_q, struct card_handle *ch) |
| 311 | { |
Harald Welte | 06b27f6 | 2016-03-02 14:26:38 +0100 | [diff] [blame] | 312 | struct req_ctx *rctx, *tmp; |
Harald Welte | ebb80ed | 2016-03-02 13:56:59 +0100 | [diff] [blame] | 313 | |
Harald Welte | 06b27f6 | 2016-03-02 14:26:38 +0100 | [diff] [blame] | 314 | llist_for_each_entry_safe(rctx, tmp, main_q, list) |
Harald Welte | ebb80ed | 2016-03-02 13:56:59 +0100 | [diff] [blame] | 315 | dispatch_usb_command(rctx, ch); |
| 316 | } |
| 317 | |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 318 | /* main loop function, called repeatedly */ |
| 319 | void mode_cardemu_run(void) |
| 320 | { |
Harald Welte | acae412 | 2016-03-02 10:27:58 +0100 | [diff] [blame] | 321 | struct llist_head *queue; |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame^] | 322 | unsigned int i; |
Harald Welte | acae412 | 2016-03-02 10:27:58 +0100 | [diff] [blame] | 323 | |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame^] | 324 | for (i = 0; i < ARRAY_SIZE(cardem_inst); i++) { |
| 325 | struct cardem_inst *ci = &cardem_inst[i]; |
| 326 | |
Harald Welte | 54cb3d0 | 2016-02-29 14:12:40 +0100 | [diff] [blame] | 327 | /* drain the ring buffer from UART into card_emu */ |
Harald Welte | c0bd7f0 | 2016-02-29 10:13:33 +0100 | [diff] [blame] | 328 | while (1) { |
| 329 | __disable_irq(); |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame^] | 330 | if (rbuf_is_empty(&ci->rb)) { |
Harald Welte | c0bd7f0 | 2016-02-29 10:13:33 +0100 | [diff] [blame] | 331 | __enable_irq(); |
| 332 | break; |
| 333 | } |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame^] | 334 | uint8_t byte = rbuf_read(&ci->rb); |
Harald Welte | c0bd7f0 | 2016-02-29 10:13:33 +0100 | [diff] [blame] | 335 | __enable_irq(); |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame^] | 336 | card_emu_process_rx_byte(ci->ch, byte); |
Harald Welte | acae412 | 2016-03-02 10:27:58 +0100 | [diff] [blame] | 337 | TRACE_ERROR("Rx%02x\r\n", byte); |
Harald Welte | c0bd7f0 | 2016-02-29 10:13:33 +0100 | [diff] [blame] | 338 | } |
Harald Welte | acae412 | 2016-03-02 10:27:58 +0100 | [diff] [blame] | 339 | |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame^] | 340 | queue = card_emu_get_usb_tx_queue(ci->ch); |
Harald Welte | acae412 | 2016-03-02 10:27:58 +0100 | [diff] [blame] | 341 | int usb_pending = llist_count(queue); |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame^] | 342 | if (usb_pending != ci->usb_pending_old) { |
Harald Welte | acae412 | 2016-03-02 10:27:58 +0100 | [diff] [blame] | 343 | // printf("usb_pending=%d\r\n", usb_pending); |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame^] | 344 | ci->usb_pending_old = usb_pending; |
Harald Welte | acae412 | 2016-03-02 10:27:58 +0100 | [diff] [blame] | 345 | } |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame^] | 346 | usb_refill_to_host(queue, ci->ep_in); |
Harald Welte | acae412 | 2016-03-02 10:27:58 +0100 | [diff] [blame] | 347 | |
Harald Welte | ebb80ed | 2016-03-02 13:56:59 +0100 | [diff] [blame] | 348 | /* ensure we can handle incoming USB messages from the |
| 349 | * host */ |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame^] | 350 | queue = &ci->usb_out_queue; |
| 351 | usb_refill_from_host(queue, ci->ep_out); |
| 352 | process_any_usb_commands(queue, ci->ch); |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 353 | } |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 354 | |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 355 | } |