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