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