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