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