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