Harald Welte | 53079bb | 2016-03-20 14:58:35 +0100 | [diff] [blame] | 1 | //#define TRACE_LEVEL 6 |
Harald Welte | 7abdb51 | 2016-03-03 17:48:32 +0100 | [diff] [blame] | 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 | b41598b | 2017-02-04 12:15:58 +0100 | [diff] [blame] | 9 | #include "osmocom/core/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 | |
Harald Welte | fd9c041 | 2016-03-20 18:15:57 +0100 | [diff] [blame] | 14 | #define TRACE_ENTRY() TRACE_DEBUG("%s entering\r\n", __func__) |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 15 | |
Harald Welte | 6d1128e | 2017-05-05 20:23:10 +0200 | [diff] [blame] | 16 | #ifdef PINS_CARDSIM |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 17 | static const Pin pins_cardsim[] = PINS_CARDSIM; |
Harald Welte | 6d1128e | 2017-05-05 20:23:10 +0200 | [diff] [blame] | 18 | #endif |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 19 | |
| 20 | /* UART pins */ |
| 21 | static const Pin pins_usim1[] = {PINS_USIM1}; |
| 22 | static const Pin pin_usim1_rst = PIN_USIM1_nRST; |
| 23 | static const Pin pin_usim1_vcc = PIN_USIM1_VCC; |
| 24 | |
| 25 | #ifdef CARDEMU_SECOND_UART |
Harald Welte | 6dcacf3 | 2016-03-19 14:01:31 +0100 | [diff] [blame] | 26 | static const Pin pins_usim2[] = {PINS_USIM2}; |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 27 | static const Pin pin_usim2_rst = PIN_USIM2_nRST; |
| 28 | static const Pin pin_usim2_vcc = PIN_USIM2_VCC; |
| 29 | #endif |
| 30 | |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame] | 31 | struct cardem_inst { |
Harald Welte | dde112e | 2016-03-20 16:42:11 +0100 | [diff] [blame] | 32 | uint32_t num; |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame] | 33 | struct card_handle *ch; |
| 34 | struct llist_head usb_out_queue; |
| 35 | struct ringbuf rb; |
| 36 | struct Usart_info usart_info; |
| 37 | int usb_pending_old; |
| 38 | uint8_t ep_out; |
| 39 | uint8_t ep_in; |
| 40 | uint8_t ep_int; |
Harald Welte | 0eaa992 | 2016-03-04 03:03:49 +0100 | [diff] [blame] | 41 | const Pin pin_insert; |
Harald Welte | 07872b6 | 2016-03-20 11:45:36 +0100 | [diff] [blame] | 42 | uint32_t vcc_uv; |
| 43 | uint32_t vcc_uv_last; |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame] | 44 | }; |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 45 | |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame] | 46 | static struct cardem_inst cardem_inst[] = { |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 47 | { |
Harald Welte | dde112e | 2016-03-20 16:42:11 +0100 | [diff] [blame] | 48 | .num = 0, |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame] | 49 | .usart_info = { |
| 50 | .base = USART1, |
| 51 | .id = ID_USART1, |
| 52 | .state = USART_RCV |
| 53 | }, |
| 54 | .ep_out = PHONE_DATAOUT, |
| 55 | .ep_in = PHONE_DATAIN, |
| 56 | .ep_int = PHONE_INT, |
Harald Welte | 6d1128e | 2017-05-05 20:23:10 +0200 | [diff] [blame] | 57 | #ifdef PIN_SET_USIM1_PRES |
Harald Welte | 0eaa992 | 2016-03-04 03:03:49 +0100 | [diff] [blame] | 58 | .pin_insert = PIN_SET_USIM1_PRES, |
Harald Welte | 6d1128e | 2017-05-05 20:23:10 +0200 | [diff] [blame] | 59 | #endif |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 60 | }, |
| 61 | #ifdef CARDEMU_SECOND_UART |
| 62 | { |
Harald Welte | dde112e | 2016-03-20 16:42:11 +0100 | [diff] [blame] | 63 | .num = 1, |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame] | 64 | .usart_info = { |
| 65 | .base = USART0, |
| 66 | .id = ID_USART0, |
| 67 | .state = USART_RCV |
| 68 | }, |
| 69 | .ep_out = CARDEM_USIM2_DATAOUT, |
| 70 | .ep_in = CARDEM_USIM2_DATAIN, |
| 71 | .ep_int = CARDEM_USIM2_INT, |
Harald Welte | 6d1128e | 2017-05-05 20:23:10 +0200 | [diff] [blame] | 72 | #ifdef PIN_SET_USIM2_PRES |
Harald Welte | 0eaa992 | 2016-03-04 03:03:49 +0100 | [diff] [blame] | 73 | .pin_insert = PIN_SET_USIM2_PRES, |
Harald Welte | 6d1128e | 2017-05-05 20:23:10 +0200 | [diff] [blame] | 74 | #endif |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 75 | }, |
| 76 | #endif |
| 77 | }; |
| 78 | |
| 79 | static Usart *get_usart_by_chan(uint8_t uart_chan) |
| 80 | { |
| 81 | switch (uart_chan) { |
| 82 | case 0: |
| 83 | return USART1; |
| 84 | #ifdef CARDEMU_SECOND_UART |
| 85 | case 1: |
| 86 | return USART0; |
| 87 | #endif |
| 88 | } |
| 89 | return NULL; |
| 90 | } |
| 91 | |
| 92 | /*********************************************************************** |
| 93 | * Call-Backs from card_emu.c |
| 94 | ***********************************************************************/ |
| 95 | |
Harald Welte | c8beefb | 2016-03-20 14:40:47 +0100 | [diff] [blame] | 96 | static void wait_tx_idle(Usart *usart) |
| 97 | { |
| 98 | int i = 1; |
| 99 | |
| 100 | /* wait until last char has been fully transmitted */ |
| 101 | while ((usart->US_CSR & (US_CSR_TXEMPTY)) == 0) { |
| 102 | if (!(i%1000000)) { |
| 103 | TRACE_ERROR("s: %x \r\n", usart->US_CSR); |
| 104 | } |
| 105 | i++; |
| 106 | } |
| 107 | } |
| 108 | |
Harald Welte | c58bba0 | 2016-03-20 14:57:53 +0100 | [diff] [blame] | 109 | void card_emu_uart_wait_tx_idle(uint8_t uart_chan) |
| 110 | { |
| 111 | Usart *usart = get_usart_by_chan(uart_chan); |
| 112 | wait_tx_idle(usart); |
| 113 | } |
| 114 | |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 115 | /* call-back from card_emu.c to enable/disable transmit and/or receive */ |
| 116 | void card_emu_uart_enable(uint8_t uart_chan, uint8_t rxtx) |
| 117 | { |
| 118 | Usart *usart = get_usart_by_chan(uart_chan); |
| 119 | switch (rxtx) { |
| 120 | case ENABLE_TX: |
Harald Welte | 9dbc46e | 2016-02-29 10:05:10 +0100 | [diff] [blame] | 121 | USART_DisableIt(usart, ~US_IER_TXRDY); |
Harald Welte | c8beefb | 2016-03-20 14:40:47 +0100 | [diff] [blame] | 122 | /* as irritating as it is, we actually want to keep the |
| 123 | * receiver enabled during transmit */ |
| 124 | USART_SetReceiverEnabled(usart, 1); |
Harald Welte | 8a416b1 | 2016-03-01 00:42:04 +0100 | [diff] [blame] | 125 | usart->US_CR = US_CR_RSTSTA | US_CR_RSTIT | US_CR_RSTNACK; |
| 126 | USART_EnableIt(usart, US_IER_TXRDY); |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 127 | USART_SetTransmitterEnabled(usart, 1); |
| 128 | break; |
| 129 | case ENABLE_RX: |
Harald Welte | 9dbc46e | 2016-02-29 10:05:10 +0100 | [diff] [blame] | 130 | USART_DisableIt(usart, ~US_IER_RXRDY); |
Harald Welte | c8beefb | 2016-03-20 14:40:47 +0100 | [diff] [blame] | 131 | /* as irritating as it is, we actually want to keep the |
| 132 | * transmitter enabled during receive */ |
| 133 | USART_SetTransmitterEnabled(usart, 1); |
| 134 | wait_tx_idle(usart); |
Harald Welte | 8a416b1 | 2016-03-01 00:42:04 +0100 | [diff] [blame] | 135 | usart->US_CR = US_CR_RSTSTA | US_CR_RSTIT | US_CR_RSTNACK; |
| 136 | USART_EnableIt(usart, US_IER_RXRDY); |
| 137 | USART_SetReceiverEnabled(usart, 1); |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 138 | break; |
| 139 | case 0: |
| 140 | default: |
| 141 | USART_SetTransmitterEnabled(usart, 0); |
| 142 | USART_SetReceiverEnabled(usart, 0); |
Harald Welte | 9dbc46e | 2016-02-29 10:05:10 +0100 | [diff] [blame] | 143 | USART_DisableIt(usart, 0xFFFFFFFF); |
Harald Welte | 8a416b1 | 2016-03-01 00:42:04 +0100 | [diff] [blame] | 144 | usart->US_CR = US_CR_RSTSTA | US_CR_RSTIT | US_CR_RSTNACK; |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 145 | break; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | /* call-back from card_emu.c to transmit a byte */ |
| 150 | int card_emu_uart_tx(uint8_t uart_chan, uint8_t byte) |
| 151 | { |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame] | 152 | Usart *usart = get_usart_by_chan(uart_chan); |
| 153 | #if 0 |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 154 | Usart_info *ui = &usart_info[uart_chan]; |
| 155 | ISO7816_SendChar(byte, ui); |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame] | 156 | #else |
Harald Welte | 53079bb | 2016-03-20 14:58:35 +0100 | [diff] [blame] | 157 | int i = 1; |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame] | 158 | while ((usart->US_CSR & (US_CSR_TXRDY)) == 0) { |
| 159 | if (!(i%1000000)) { |
Harald Welte | dde112e | 2016-03-20 16:42:11 +0100 | [diff] [blame] | 160 | TRACE_ERROR("%u: s: %x %02X\r\n", |
| 161 | uart_chan, usart->US_CSR, |
| 162 | usart->US_RHR & 0xFF); |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame] | 163 | usart->US_CR = US_CR_RSTTX; |
| 164 | usart->US_CR = US_CR_RSTRX; |
| 165 | } |
Harald Welte | 53079bb | 2016-03-20 14:58:35 +0100 | [diff] [blame] | 166 | i++; |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame] | 167 | } |
| 168 | usart->US_THR = byte; |
Harald Welte | 53079bb | 2016-03-20 14:58:35 +0100 | [diff] [blame] | 169 | //TRACE_ERROR("Sx%02x\r\n", byte); |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame] | 170 | #endif |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 171 | return 1; |
| 172 | } |
| 173 | |
| 174 | |
| 175 | /* FIXME: integrate this with actual irq handler */ |
Harald Welte | 3bafe43 | 2016-03-20 16:43:12 +0100 | [diff] [blame] | 176 | static void usart_irq_rx(uint8_t inst_num) |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 177 | { |
Harald Welte | 3bafe43 | 2016-03-20 16:43:12 +0100 | [diff] [blame] | 178 | Usart *usart = get_usart_by_chan(inst_num); |
| 179 | struct cardem_inst *ci = &cardem_inst[inst_num]; |
Harald Welte | c0bd7f0 | 2016-02-29 10:13:33 +0100 | [diff] [blame] | 180 | uint32_t csr; |
| 181 | uint8_t byte = 0; |
| 182 | |
Harald Welte | da15ca0 | 2016-03-17 21:14:04 +0100 | [diff] [blame] | 183 | csr = usart->US_CSR & usart->US_IMR; |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 184 | |
Harald Welte | c0bd7f0 | 2016-02-29 10:13:33 +0100 | [diff] [blame] | 185 | if (csr & US_CSR_RXRDY) { |
| 186 | byte = (usart->US_RHR) & 0xFF; |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame] | 187 | rbuf_write(&ci->rb, byte); |
Harald Welte | c0bd7f0 | 2016-02-29 10:13:33 +0100 | [diff] [blame] | 188 | } |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 189 | |
Harald Welte | 12d4bdf | 2016-03-02 10:31:03 +0100 | [diff] [blame] | 190 | if (csr & US_CSR_TXRDY) { |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame] | 191 | if (card_emu_tx_byte(ci->ch) == 0) |
Harald Welte | 12d4bdf | 2016-03-02 10:31:03 +0100 | [diff] [blame] | 192 | USART_DisableIt(usart, US_IER_TXRDY); |
| 193 | } |
| 194 | |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 195 | if (csr & (US_CSR_OVRE|US_CSR_FRAME|US_CSR_PARE| |
| 196 | US_CSR_TIMEOUT|US_CSR_NACK|(1<<10))) { |
Harald Welte | c0bd7f0 | 2016-02-29 10:13:33 +0100 | [diff] [blame] | 197 | usart->US_CR = US_CR_RSTSTA | US_CR_RSTIT | US_CR_RSTNACK; |
Harald Welte | dde112e | 2016-03-20 16:42:11 +0100 | [diff] [blame] | 198 | TRACE_ERROR("%u e 0x%x st: 0x%x\n", ci->num, byte, csr); |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | |
Harald Welte | 3bafe43 | 2016-03-20 16:43:12 +0100 | [diff] [blame] | 202 | void mode_cardemu_usart0_irq(void) |
| 203 | { |
| 204 | /* USART0 == Instance 1 == USIM 2 */ |
| 205 | usart_irq_rx(1); |
| 206 | } |
| 207 | |
| 208 | void mode_cardemu_usart1_irq(void) |
| 209 | { |
| 210 | /* USART1 == Instance 0 == USIM 1 */ |
| 211 | usart_irq_rx(0); |
| 212 | } |
| 213 | |
Harald Welte | bd71768 | 2016-02-28 19:30:05 +0100 | [diff] [blame] | 214 | /* call-back from card_emu.c to change UART baud rate */ |
| 215 | int card_emu_uart_update_fidi(uint8_t uart_chan, unsigned int fidi) |
| 216 | { |
| 217 | int rc; |
Harald Welte | bd71768 | 2016-02-28 19:30:05 +0100 | [diff] [blame] | 218 | Usart *usart = get_usart_by_chan(uart_chan); |
| 219 | |
Harald Welte | 99f62a6 | 2016-02-29 10:08:49 +0100 | [diff] [blame] | 220 | usart->US_CR |= US_CR_RXDIS | US_CR_RSTRX; |
| 221 | usart->US_FIDI = fidi & 0x3ff; |
| 222 | usart->US_CR |= US_CR_RXEN | US_CR_STTTO; |
| 223 | return 0; |
Harald Welte | bd71768 | 2016-02-28 19:30:05 +0100 | [diff] [blame] | 224 | } |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 225 | |
| 226 | /*********************************************************************** |
Harald Welte | 419eb8a | 2016-03-20 10:04:03 +0100 | [diff] [blame] | 227 | * ADC for VCC voltage detection |
| 228 | ***********************************************************************/ |
| 229 | |
Harald Welte | 07872b6 | 2016-03-20 11:45:36 +0100 | [diff] [blame] | 230 | #ifdef DETECT_VCC_BY_ADC |
| 231 | |
| 232 | static int adc_triggered = 0; |
Harald Welte | 28772eb | 2016-03-20 15:44:52 +0100 | [diff] [blame] | 233 | static int adc_sam3s_reva_errata = 0; |
Harald Welte | 07872b6 | 2016-03-20 11:45:36 +0100 | [diff] [blame] | 234 | |
Harald Welte | 419eb8a | 2016-03-20 10:04:03 +0100 | [diff] [blame] | 235 | static int card_vcc_adc_init(void) |
| 236 | { |
Harald Welte | 622b6be | 2016-03-20 16:43:59 +0100 | [diff] [blame] | 237 | uint32_t chip_arch = CHIPID->CHIPID_CIDR & CHIPID_CIDR_ARCH_Msk; |
| 238 | uint32_t chip_ver = CHIPID->CHIPID_CIDR & CHIPID_CIDR_VERSION_Msk; |
Harald Welte | 28772eb | 2016-03-20 15:44:52 +0100 | [diff] [blame] | 239 | |
Harald Welte | 07872b6 | 2016-03-20 11:45:36 +0100 | [diff] [blame] | 240 | PMC_EnablePeripheral(ID_ADC); |
| 241 | |
Harald Welte | 419eb8a | 2016-03-20 10:04:03 +0100 | [diff] [blame] | 242 | ADC->ADC_CR |= ADC_CR_SWRST; |
Harald Welte | 28772eb | 2016-03-20 15:44:52 +0100 | [diff] [blame] | 243 | if (chip_ver == 0 && |
| 244 | (chip_arch == CHIPID_CIDR_ARCH_SAM3SxA || |
| 245 | chip_arch == CHIPID_CIDR_ARCH_SAM3SxB || |
| 246 | chip_arch == CHIPID_CIDR_ARCH_SAM3SxC)) { |
| 247 | TRACE_INFO("Enabling Rev.A ADC Errata work-around\r\n"); |
| 248 | adc_sam3s_reva_errata = 1; |
| 249 | } |
| 250 | |
| 251 | if (adc_sam3s_reva_errata) { |
| 252 | /* Errata Work-Around to clear EOCx flags */ |
Harald Welte | 07872b6 | 2016-03-20 11:45:36 +0100 | [diff] [blame] | 253 | volatile uint32_t foo; |
| 254 | int i; |
| 255 | for (i = 0; i < 16; i++) |
| 256 | foo = ADC->ADC_CDR[i]; |
| 257 | } |
| 258 | |
| 259 | /* Initialize ADC for AD7 / AD6, fADC=48/24=2MHz */ |
Harald Welte | 419eb8a | 2016-03-20 10:04:03 +0100 | [diff] [blame] | 260 | ADC->ADC_MR = ADC_MR_TRGEN_DIS | ADC_MR_LOWRES_BITS_12 | |
| 261 | ADC_MR_SLEEP_NORMAL | ADC_MR_FWUP_OFF | |
Harald Welte | 07872b6 | 2016-03-20 11:45:36 +0100 | [diff] [blame] | 262 | ADC_MR_FREERUN_OFF | ADC_MR_PRESCAL(23) | |
| 263 | ADC_MR_STARTUP_SUT8 | ADC_MR_SETTLING(3) | |
| 264 | ADC_MR_ANACH_NONE | ADC_MR_TRACKTIM(4) | |
Harald Welte | 419eb8a | 2016-03-20 10:04:03 +0100 | [diff] [blame] | 265 | ADC_MR_TRANSFER(1) | ADC_MR_USEQ_NUM_ORDER; |
| 266 | /* enable AD6 + AD7 channels */ |
Harald Welte | 07872b6 | 2016-03-20 11:45:36 +0100 | [diff] [blame] | 267 | ADC->ADC_CHER = ADC_CHER_CH7; |
| 268 | ADC->ADC_IER = ADC_IER_EOC7; |
| 269 | #ifdef CARDEMU_SECOND_UART |
| 270 | ADC->ADC_CHER |= ADC_CHER_CH6; |
| 271 | ADC->ADC_IER |= ADC_IER_EOC6; |
| 272 | #endif |
| 273 | NVIC_EnableIRQ(ADC_IRQn); |
Harald Welte | 419eb8a | 2016-03-20 10:04:03 +0100 | [diff] [blame] | 274 | ADC->ADC_CR |= ADC_CR_START; |
Harald Welte | 07872b6 | 2016-03-20 11:45:36 +0100 | [diff] [blame] | 275 | |
| 276 | return 0; |
Harald Welte | 419eb8a | 2016-03-20 10:04:03 +0100 | [diff] [blame] | 277 | } |
| 278 | |
Harald Welte | 07872b6 | 2016-03-20 11:45:36 +0100 | [diff] [blame] | 279 | #define UV_PER_LSB ((3300 * 1000) / 4096) |
| 280 | #define VCC_UV_THRESH_1V8 1500000 |
| 281 | #define VCC_UV_THRESH_3V 2800000 |
| 282 | |
| 283 | static void process_vcc_adc(struct cardem_inst *ci) |
Harald Welte | 419eb8a | 2016-03-20 10:04:03 +0100 | [diff] [blame] | 284 | { |
Harald Welte | 07872b6 | 2016-03-20 11:45:36 +0100 | [diff] [blame] | 285 | if (ci->vcc_uv >= VCC_UV_THRESH_3V && |
| 286 | ci->vcc_uv_last < VCC_UV_THRESH_3V) { |
| 287 | card_emu_io_statechg(ci->ch, CARD_IO_VCC, 1); |
| 288 | /* FIXME do this for real */ |
| 289 | card_emu_io_statechg(ci->ch, CARD_IO_CLK, 1); |
| 290 | } else if (ci->vcc_uv < VCC_UV_THRESH_3V && |
| 291 | ci->vcc_uv_last >= VCC_UV_THRESH_3V) { |
| 292 | /* FIXME do this for real */ |
| 293 | card_emu_io_statechg(ci->ch, CARD_IO_CLK, 0); |
| 294 | card_emu_io_statechg(ci->ch, CARD_IO_VCC, 0); |
Harald Welte | 419eb8a | 2016-03-20 10:04:03 +0100 | [diff] [blame] | 295 | } |
Harald Welte | 07872b6 | 2016-03-20 11:45:36 +0100 | [diff] [blame] | 296 | ci->vcc_uv_last = ci->vcc_uv; |
| 297 | } |
| 298 | |
| 299 | static uint32_t adc2uv(uint16_t adc) |
| 300 | { |
| 301 | uint32_t uv = (uint32_t) adc * UV_PER_LSB; |
Harald Welte | 622b6be | 2016-03-20 16:43:59 +0100 | [diff] [blame] | 302 | return uv; |
Harald Welte | 07872b6 | 2016-03-20 11:45:36 +0100 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | void ADC_IrqHandler(void) |
| 306 | { |
| 307 | #ifdef CARDEMU_SECOND_UART |
| 308 | if (ADC->ADC_ISR & ADC_ISR_EOC6) { |
| 309 | uint16_t val = ADC->ADC_CDR[6] & 0xFFF; |
| 310 | cardem_inst[1].vcc_uv = adc2uv(val); |
| 311 | process_vcc_adc(&cardem_inst[1]); |
Harald Welte | 28772eb | 2016-03-20 15:44:52 +0100 | [diff] [blame] | 312 | if (adc_sam3s_reva_errata) { |
| 313 | /* Errata: START doesn't start a conversion |
| 314 | * sequence, but only a single conversion */ |
| 315 | ADC->ADC_CR |= ADC_CR_START; |
| 316 | } |
Harald Welte | 07872b6 | 2016-03-20 11:45:36 +0100 | [diff] [blame] | 317 | } |
| 318 | #endif |
Harald Welte | 419eb8a | 2016-03-20 10:04:03 +0100 | [diff] [blame] | 319 | |
| 320 | if (ADC->ADC_ISR & ADC_ISR_EOC7) { |
Harald Welte | 07872b6 | 2016-03-20 11:45:36 +0100 | [diff] [blame] | 321 | uint16_t val = ADC->ADC_CDR[7] & 0xFFF; |
| 322 | cardem_inst[0].vcc_uv = adc2uv(val); |
| 323 | process_vcc_adc(&cardem_inst[0]); |
| 324 | ADC->ADC_CR |= ADC_CR_START; |
Harald Welte | 419eb8a | 2016-03-20 10:04:03 +0100 | [diff] [blame] | 325 | } |
| 326 | } |
Harald Welte | 07872b6 | 2016-03-20 11:45:36 +0100 | [diff] [blame] | 327 | #endif /* DETECT_VCC_BY_ADC */ |
Harald Welte | 419eb8a | 2016-03-20 10:04:03 +0100 | [diff] [blame] | 328 | |
| 329 | /*********************************************************************** |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 330 | * Core USB / mainloop integration |
| 331 | ***********************************************************************/ |
| 332 | |
Harald Welte | c0bd7f0 | 2016-02-29 10:13:33 +0100 | [diff] [blame] | 333 | static void usim1_rst_irqhandler(const Pin *pPin) |
| 334 | { |
| 335 | int active = PIO_Get(&pin_usim1_rst) ? 0 : 1; |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame] | 336 | card_emu_io_statechg(cardem_inst[0].ch, CARD_IO_RST, active); |
Harald Welte | c0bd7f0 | 2016-02-29 10:13:33 +0100 | [diff] [blame] | 337 | } |
| 338 | |
Harald Welte | 07872b6 | 2016-03-20 11:45:36 +0100 | [diff] [blame] | 339 | #ifndef DETECT_VCC_BY_ADC |
Harald Welte | c0bd7f0 | 2016-02-29 10:13:33 +0100 | [diff] [blame] | 340 | static void usim1_vcc_irqhandler(const Pin *pPin) |
| 341 | { |
| 342 | int active = PIO_Get(&pin_usim1_vcc) ? 1 : 0; |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame] | 343 | card_emu_io_statechg(cardem_inst[0].ch, CARD_IO_VCC, active); |
Harald Welte | c0bd7f0 | 2016-02-29 10:13:33 +0100 | [diff] [blame] | 344 | /* FIXME do this for real */ |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame] | 345 | card_emu_io_statechg(cardem_inst[0].ch, CARD_IO_CLK, active); |
Harald Welte | c0bd7f0 | 2016-02-29 10:13:33 +0100 | [diff] [blame] | 346 | } |
Harald Welte | 07872b6 | 2016-03-20 11:45:36 +0100 | [diff] [blame] | 347 | #endif /* !DETECT_VCC_BY_ADC */ |
Harald Welte | c0bd7f0 | 2016-02-29 10:13:33 +0100 | [diff] [blame] | 348 | |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame] | 349 | #ifdef CARDEMU_SECOND_UART |
| 350 | static void usim2_rst_irqhandler(const Pin *pPin) |
| 351 | { |
| 352 | int active = PIO_Get(&pin_usim2_rst) ? 0 : 1; |
| 353 | card_emu_io_statechg(cardem_inst[1].ch, CARD_IO_RST, active); |
| 354 | } |
| 355 | |
Harald Welte | 07872b6 | 2016-03-20 11:45:36 +0100 | [diff] [blame] | 356 | #ifndef DETECT_VCC_BY_ADC |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame] | 357 | static void usim2_vcc_irqhandler(const Pin *pPin) |
| 358 | { |
| 359 | int active = PIO_Get(&pin_usim2_vcc) ? 1 : 0; |
| 360 | card_emu_io_statechg(cardem_inst[1].ch, CARD_IO_VCC, active); |
| 361 | /* FIXME do this for real */ |
| 362 | card_emu_io_statechg(cardem_inst[1].ch, CARD_IO_CLK, active); |
| 363 | } |
Harald Welte | 07872b6 | 2016-03-20 11:45:36 +0100 | [diff] [blame] | 364 | #endif /* !DETECT_VCC_BY_ADC */ |
| 365 | #endif /* CARDEMU_SECOND_UART */ |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame] | 366 | |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 367 | /* executed once at system boot for each config */ |
| 368 | void mode_cardemu_configure(void) |
| 369 | { |
Harald Welte | bd71768 | 2016-02-28 19:30:05 +0100 | [diff] [blame] | 370 | TRACE_ENTRY(); |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | /* called if config is activated */ |
| 374 | void mode_cardemu_init(void) |
| 375 | { |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame] | 376 | int i; |
Harald Welte | bd71768 | 2016-02-28 19:30:05 +0100 | [diff] [blame] | 377 | |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame] | 378 | TRACE_ENTRY(); |
Harald Welte | c0bd7f0 | 2016-02-29 10:13:33 +0100 | [diff] [blame] | 379 | |
Harald Welte | 6d1128e | 2017-05-05 20:23:10 +0200 | [diff] [blame] | 380 | #ifdef PINS_CARDSIM |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 381 | PIO_Configure(pins_cardsim, PIO_LISTSIZE(pins_cardsim)); |
Harald Welte | 6d1128e | 2017-05-05 20:23:10 +0200 | [diff] [blame] | 382 | #endif |
Harald Welte | 07872b6 | 2016-03-20 11:45:36 +0100 | [diff] [blame] | 383 | #ifdef DETECT_VCC_BY_ADC |
| 384 | card_vcc_adc_init(); |
| 385 | #endif /* DETECT_VCC_BY_ADC */ |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 386 | |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame] | 387 | INIT_LLIST_HEAD(&cardem_inst[0].usb_out_queue); |
| 388 | rbuf_reset(&cardem_inst[0].rb); |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 389 | PIO_Configure(pins_usim1, PIO_LISTSIZE(pins_usim1)); |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame] | 390 | ISO7816_Init(&cardem_inst[0].usart_info, CLK_SLAVE); |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 391 | NVIC_EnableIRQ(USART1_IRQn); |
Harald Welte | c0bd7f0 | 2016-02-29 10:13:33 +0100 | [diff] [blame] | 392 | PIO_ConfigureIt(&pin_usim1_rst, usim1_rst_irqhandler); |
| 393 | PIO_EnableIt(&pin_usim1_rst); |
Harald Welte | 07872b6 | 2016-03-20 11:45:36 +0100 | [diff] [blame] | 394 | #ifndef DETECT_VCC_BY_ADC |
Harald Welte | c0bd7f0 | 2016-02-29 10:13:33 +0100 | [diff] [blame] | 395 | PIO_ConfigureIt(&pin_usim1_vcc, usim1_vcc_irqhandler); |
| 396 | PIO_EnableIt(&pin_usim1_vcc); |
Harald Welte | 07872b6 | 2016-03-20 11:45:36 +0100 | [diff] [blame] | 397 | #endif /* DETECT_VCC_BY_ADC */ |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame] | 398 | cardem_inst[0].ch = card_emu_init(0, 2, 0); |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 399 | |
| 400 | #ifdef CARDEMU_SECOND_UART |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame] | 401 | INIT_LLIST_HEAD(&cardem_inst[1].usb_out_queue); |
| 402 | rbuf_reset(&cardem_inst[1].rb); |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 403 | PIO_Configure(pins_usim2, PIO_LISTSIZE(pins_usim2)); |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame] | 404 | ISO7816_Init(&cardem_inst[1].usart_info, CLK_SLAVE); |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 405 | NVIC_EnableIRQ(USART0_IRQn); |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame] | 406 | PIO_ConfigureIt(&pin_usim2_rst, usim2_rst_irqhandler); |
| 407 | PIO_EnableIt(&pin_usim2_rst); |
Harald Welte | 07872b6 | 2016-03-20 11:45:36 +0100 | [diff] [blame] | 408 | #ifndef DETECT_VCC_BY_ADC |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame] | 409 | PIO_ConfigureIt(&pin_usim2_vcc, usim2_vcc_irqhandler); |
| 410 | PIO_EnableIt(&pin_usim2_vcc); |
Harald Welte | 07872b6 | 2016-03-20 11:45:36 +0100 | [diff] [blame] | 411 | #endif /* DETECT_VCC_BY_ADC */ |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame] | 412 | cardem_inst[1].ch = card_emu_init(1, 0, 1); |
Harald Welte | 07872b6 | 2016-03-20 11:45:36 +0100 | [diff] [blame] | 413 | #endif /* CARDEMU_SECOND_UART */ |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | /* called if config is deactivated */ |
| 417 | void mode_cardemu_exit(void) |
| 418 | { |
Harald Welte | bd71768 | 2016-02-28 19:30:05 +0100 | [diff] [blame] | 419 | TRACE_ENTRY(); |
| 420 | |
Harald Welte | 7abdb51 | 2016-03-03 17:48:32 +0100 | [diff] [blame] | 421 | /* FIXME: stop tc_fdt */ |
| 422 | /* FIXME: release all rctx, unlink them from any queue */ |
| 423 | |
Harald Welte | c0bd7f0 | 2016-02-29 10:13:33 +0100 | [diff] [blame] | 424 | PIO_DisableIt(&pin_usim1_rst); |
| 425 | PIO_DisableIt(&pin_usim1_vcc); |
| 426 | |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 427 | NVIC_DisableIRQ(USART1_IRQn); |
| 428 | USART_SetTransmitterEnabled(USART1, 0); |
| 429 | USART_SetReceiverEnabled(USART1, 0); |
| 430 | |
| 431 | #ifdef CARDEMU_SECOND_UART |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame] | 432 | PIO_DisableIt(&pin_usim2_rst); |
| 433 | PIO_DisableIt(&pin_usim2_vcc); |
| 434 | |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 435 | NVIC_DisableIRQ(USART0_IRQn); |
| 436 | USART_SetTransmitterEnabled(USART0, 0); |
| 437 | USART_SetReceiverEnabled(USART0, 0); |
| 438 | #endif |
| 439 | } |
| 440 | |
Harald Welte | ebb80ed | 2016-03-02 13:56:59 +0100 | [diff] [blame] | 441 | /* handle a single USB command as received from the USB host */ |
Harald Welte | 0eaa992 | 2016-03-04 03:03:49 +0100 | [diff] [blame] | 442 | 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] | 443 | { |
| 444 | struct cardemu_usb_msg_hdr *hdr; |
Harald Welte | 06b27f6 | 2016-03-02 14:26:38 +0100 | [diff] [blame] | 445 | struct cardemu_usb_msg_set_atr *atr; |
Harald Welte | 0eaa992 | 2016-03-04 03:03:49 +0100 | [diff] [blame] | 446 | struct cardemu_usb_msg_cardinsert *cardins; |
Harald Welte | ebb80ed | 2016-03-02 13:56:59 +0100 | [diff] [blame] | 447 | struct llist_head *queue; |
| 448 | |
| 449 | hdr = (struct cardemu_usb_msg_hdr *) rctx->data; |
| 450 | switch (hdr->msg_type) { |
| 451 | case CEMU_USB_MSGT_DT_TX_DATA: |
Harald Welte | 0eaa992 | 2016-03-04 03:03:49 +0100 | [diff] [blame] | 452 | queue = card_emu_get_uart_tx_queue(ci->ch); |
Harald Welte | ebb80ed | 2016-03-02 13:56:59 +0100 | [diff] [blame] | 453 | req_ctx_set_state(rctx, RCTX_S_UART_TX_PENDING); |
| 454 | llist_add_tail(&rctx->list, queue); |
Harald Welte | 0eaa992 | 2016-03-04 03:03:49 +0100 | [diff] [blame] | 455 | card_emu_have_new_uart_tx(ci->ch); |
Harald Welte | ebb80ed | 2016-03-02 13:56:59 +0100 | [diff] [blame] | 456 | break; |
| 457 | case CEMU_USB_MSGT_DT_SET_ATR: |
Harald Welte | 06b27f6 | 2016-03-02 14:26:38 +0100 | [diff] [blame] | 458 | atr = (struct cardemu_usb_msg_set_atr *) hdr; |
Harald Welte | d295b92 | 2016-03-18 21:01:36 +0100 | [diff] [blame] | 459 | card_emu_set_atr(ci->ch, atr->atr, atr->atr_len); |
Harald Welte | 06b27f6 | 2016-03-02 14:26:38 +0100 | [diff] [blame] | 460 | req_ctx_put(rctx); |
| 461 | break; |
Harald Welte | 0eaa992 | 2016-03-04 03:03:49 +0100 | [diff] [blame] | 462 | case CEMU_USB_MSGT_DT_CARDINSERT: |
| 463 | cardins = (struct cardemu_usb_msg_cardinsert *) hdr; |
Harald Welte | dde112e | 2016-03-20 16:42:11 +0100 | [diff] [blame] | 464 | TRACE_INFO("%u: set card_insert to %s\r\n", ci->num, |
| 465 | cardins->card_insert ? "INSERTED" : "REMOVED"); |
Harald Welte | 0eaa992 | 2016-03-04 03:03:49 +0100 | [diff] [blame] | 466 | if (cardins->card_insert) |
| 467 | PIO_Set(&ci->pin_insert); |
| 468 | else |
| 469 | PIO_Clear(&ci->pin_insert); |
Harald Welte | 5820ea9 | 2016-03-16 22:12:00 +0100 | [diff] [blame] | 470 | req_ctx_put(rctx); |
Harald Welte | 0eaa992 | 2016-03-04 03:03:49 +0100 | [diff] [blame] | 471 | break; |
Harald Welte | ebb80ed | 2016-03-02 13:56:59 +0100 | [diff] [blame] | 472 | case CEMU_USB_MSGT_DT_GET_STATUS: |
Harald Welte | ff16065 | 2016-03-19 21:59:06 +0100 | [diff] [blame] | 473 | card_emu_report_status(ci->ch); |
| 474 | break; |
| 475 | case CEMU_USB_MSGT_DT_GET_STATS: |
Harald Welte | ebb80ed | 2016-03-02 13:56:59 +0100 | [diff] [blame] | 476 | default: |
| 477 | /* FIXME */ |
Harald Welte | 5820ea9 | 2016-03-16 22:12:00 +0100 | [diff] [blame] | 478 | req_ctx_put(rctx); |
Harald Welte | ebb80ed | 2016-03-02 13:56:59 +0100 | [diff] [blame] | 479 | break; |
| 480 | } |
| 481 | } |
| 482 | |
Harald Welte | b26d003 | 2016-03-19 13:33:02 +0100 | [diff] [blame] | 483 | static void dispatch_received_rctx(struct req_ctx *rctx, struct cardem_inst *ci) |
| 484 | { |
| 485 | struct req_ctx *segm; |
| 486 | struct cardemu_usb_msg_hdr *mh; |
| 487 | int i = 0; |
| 488 | |
| 489 | /* check if we have multiple concatenated commands in |
| 490 | * one message. USB endpoints are streams that don't |
| 491 | * preserve the message boundaries */ |
| 492 | mh = (struct cardemu_usb_msg_hdr *) rctx->data; |
Harald Welte | b26d003 | 2016-03-19 13:33:02 +0100 | [diff] [blame] | 493 | if (mh->msg_len == rctx->tot_len) { |
| 494 | /* fast path: only one message in buffer */ |
| 495 | dispatch_usb_command(rctx, ci); |
| 496 | return; |
| 497 | } |
| 498 | |
| 499 | /* slow path: iterate over list of messages, allocating one new |
| 500 | * reqe_ctx per segment */ |
| 501 | for (mh = (struct cardemu_usb_msg_hdr *) rctx->data; |
| 502 | (uint8_t *)mh < rctx->data + rctx->tot_len; |
| 503 | mh = (struct cardemu_usb_msg_hdr * ) ((uint8_t *)mh + mh->msg_len)) { |
Harald Welte | b26d003 | 2016-03-19 13:33:02 +0100 | [diff] [blame] | 504 | segm = req_ctx_find_get(0, RCTX_S_FREE, RCTX_S_MAIN_PROCESSING); |
| 505 | if (!segm) { |
Harald Welte | dde112e | 2016-03-20 16:42:11 +0100 | [diff] [blame] | 506 | TRACE_ERROR("%u: ENOMEM during rctx segmentation\r\n", |
| 507 | ci->num); |
Harald Welte | b26d003 | 2016-03-19 13:33:02 +0100 | [diff] [blame] | 508 | break; |
| 509 | } |
| 510 | segm->idx = 0; |
| 511 | segm->tot_len = mh->msg_len; |
| 512 | memcpy(segm->data, mh, segm->tot_len); |
| 513 | dispatch_usb_command(segm, ci); |
| 514 | i++; |
| 515 | } |
| 516 | |
| 517 | /* release the master req_ctx, as all segments have been |
| 518 | * processed now */ |
| 519 | req_ctx_put(rctx); |
| 520 | } |
| 521 | |
Harald Welte | ebb80ed | 2016-03-02 13:56:59 +0100 | [diff] [blame] | 522 | /* iterate over the queue of incoming USB commands and dispatch/execute |
| 523 | * them */ |
Harald Welte | 9f240b6 | 2016-03-18 10:32:56 +0100 | [diff] [blame] | 524 | static void process_any_usb_commands(struct llist_head *main_q, |
| 525 | struct cardem_inst *ci) |
Harald Welte | ebb80ed | 2016-03-02 13:56:59 +0100 | [diff] [blame] | 526 | { |
Harald Welte | 9f240b6 | 2016-03-18 10:32:56 +0100 | [diff] [blame] | 527 | struct llist_head *lh; |
| 528 | struct req_ctx *rctx; |
| 529 | int i; |
Harald Welte | ebb80ed | 2016-03-02 13:56:59 +0100 | [diff] [blame] | 530 | |
Harald Welte | 9f240b6 | 2016-03-18 10:32:56 +0100 | [diff] [blame] | 531 | /* limit the number of iterations to 10, to ensure we don't get |
| 532 | * stuck here without returning to main loop processing */ |
| 533 | for (i = 0; i < 10; i++) { |
| 534 | /* de-queue the list head in an irq-safe way */ |
| 535 | lh = llist_head_dequeue_irqsafe(main_q); |
| 536 | if (!lh) |
| 537 | break; |
| 538 | rctx = llist_entry(lh, struct req_ctx, list); |
Harald Welte | b26d003 | 2016-03-19 13:33:02 +0100 | [diff] [blame] | 539 | dispatch_received_rctx(rctx, ci); |
Harald Welte | 5820ea9 | 2016-03-16 22:12:00 +0100 | [diff] [blame] | 540 | } |
Harald Welte | ebb80ed | 2016-03-02 13:56:59 +0100 | [diff] [blame] | 541 | } |
| 542 | |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 543 | /* main loop function, called repeatedly */ |
| 544 | void mode_cardemu_run(void) |
| 545 | { |
Harald Welte | acae412 | 2016-03-02 10:27:58 +0100 | [diff] [blame] | 546 | struct llist_head *queue; |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame] | 547 | unsigned int i; |
Harald Welte | acae412 | 2016-03-02 10:27:58 +0100 | [diff] [blame] | 548 | |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame] | 549 | for (i = 0; i < ARRAY_SIZE(cardem_inst); i++) { |
| 550 | struct cardem_inst *ci = &cardem_inst[i]; |
| 551 | |
Harald Welte | 54cb3d0 | 2016-02-29 14:12:40 +0100 | [diff] [blame] | 552 | /* drain the ring buffer from UART into card_emu */ |
Harald Welte | c0bd7f0 | 2016-02-29 10:13:33 +0100 | [diff] [blame] | 553 | while (1) { |
| 554 | __disable_irq(); |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame] | 555 | if (rbuf_is_empty(&ci->rb)) { |
Harald Welte | c0bd7f0 | 2016-02-29 10:13:33 +0100 | [diff] [blame] | 556 | __enable_irq(); |
| 557 | break; |
| 558 | } |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame] | 559 | uint8_t byte = rbuf_read(&ci->rb); |
Harald Welte | c0bd7f0 | 2016-02-29 10:13:33 +0100 | [diff] [blame] | 560 | __enable_irq(); |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame] | 561 | card_emu_process_rx_byte(ci->ch, byte); |
Harald Welte | dde112e | 2016-03-20 16:42:11 +0100 | [diff] [blame] | 562 | //TRACE_ERROR("%uRx%02x\r\n", i, byte); |
Harald Welte | c0bd7f0 | 2016-02-29 10:13:33 +0100 | [diff] [blame] | 563 | } |
Harald Welte | acae412 | 2016-03-02 10:27:58 +0100 | [diff] [blame] | 564 | |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame] | 565 | queue = card_emu_get_usb_tx_queue(ci->ch); |
Harald Welte | acae412 | 2016-03-02 10:27:58 +0100 | [diff] [blame] | 566 | int usb_pending = llist_count(queue); |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame] | 567 | if (usb_pending != ci->usb_pending_old) { |
Harald Welte | dde112e | 2016-03-20 16:42:11 +0100 | [diff] [blame] | 568 | TRACE_DEBUG("%u usb_pending=%d\r\n", |
| 569 | i, usb_pending); |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame] | 570 | ci->usb_pending_old = usb_pending; |
Harald Welte | acae412 | 2016-03-02 10:27:58 +0100 | [diff] [blame] | 571 | } |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame] | 572 | usb_refill_to_host(queue, ci->ep_in); |
Harald Welte | acae412 | 2016-03-02 10:27:58 +0100 | [diff] [blame] | 573 | |
Harald Welte | ebb80ed | 2016-03-02 13:56:59 +0100 | [diff] [blame] | 574 | /* ensure we can handle incoming USB messages from the |
| 575 | * host */ |
Harald Welte | 13e8202 | 2016-03-02 15:17:53 +0100 | [diff] [blame] | 576 | queue = &ci->usb_out_queue; |
| 577 | usb_refill_from_host(queue, ci->ep_out); |
Harald Welte | 0eaa992 | 2016-03-04 03:03:49 +0100 | [diff] [blame] | 578 | process_any_usb_commands(queue, ci); |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 579 | } |
Harald Welte | 2a6d3af | 2016-02-28 19:29:14 +0100 | [diff] [blame] | 580 | } |