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