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