blob: 7bb06f99687490757314e32f4d49cdd1bdf3d5a5 [file] [log] [blame]
Kévin Redon9a12d682018-07-08 13:21:16 +02001/* card emulation mode
2 *
3 * (C) 2015-2017 by Harald Welte <laforge@gnumonks.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
18 */
Harald Welte2a6d3af2016-02-28 19:29:14 +010019#include "board.h"
Kévin Redonac0843a2018-07-11 09:23:33 +020020#include "boardver_adc.h"
Harald Welte16055642016-03-03 11:02:45 +010021#include "simtrace.h"
22#include "ringbuffer.h"
Harald Welte2a6d3af2016-02-28 19:29:14 +010023#include "card_emu.h"
Harald Weltebd717682016-02-28 19:30:05 +010024#include "iso7816_fidi.h"
Harald Welte54cb3d02016-02-29 14:12:40 +010025#include "utils.h"
Harald Welte9d90d282018-06-29 22:25:42 +020026#include <osmocom/core/linuxlist.h>
27#include <osmocom/core/msgb.h>
Harald Welte9f240b62016-03-18 10:32:56 +010028#include "llist_irqsafe.h"
Harald Welte8e7fca32017-05-07 16:14:33 +020029#include "usb_buf.h"
Kévin Redona1012b12018-07-01 18:11:01 +020030#include "simtrace_usb.h"
Harald Welte25a9a802017-05-08 13:30:09 +020031#include "simtrace_prot.h"
Harald Welte5c583d32017-05-09 06:46:47 +020032#include "sim_switch.h"
Harald Weltebd717682016-02-28 19:30:05 +010033
Harald Weltefd9c0412016-03-20 18:15:57 +010034#define TRACE_ENTRY() TRACE_DEBUG("%s entering\r\n", __func__)
Harald Welte2a6d3af2016-02-28 19:29:14 +010035
Harald Welte6d1128e2017-05-05 20:23:10 +020036#ifdef PINS_CARDSIM
Harald Welte2a6d3af2016-02-28 19:29:14 +010037static const Pin pins_cardsim[] = PINS_CARDSIM;
Harald Welte6d1128e2017-05-05 20:23:10 +020038#endif
Harald Welte2a6d3af2016-02-28 19:29:14 +010039
40/* UART pins */
41static const Pin pins_usim1[] = {PINS_USIM1};
42static const Pin pin_usim1_rst = PIN_USIM1_nRST;
43static const Pin pin_usim1_vcc = PIN_USIM1_VCC;
44
45#ifdef CARDEMU_SECOND_UART
Harald Welte6dcacf32016-03-19 14:01:31 +010046static const Pin pins_usim2[] = {PINS_USIM2};
Harald Welte2a6d3af2016-02-28 19:29:14 +010047static const Pin pin_usim2_rst = PIN_USIM2_nRST;
48static const Pin pin_usim2_vcc = PIN_USIM2_VCC;
49#endif
50
Harald Welte13e82022016-03-02 15:17:53 +010051struct cardem_inst {
Harald Weltedde112e2016-03-20 16:42:11 +010052 uint32_t num;
Harald Welte13e82022016-03-02 15:17:53 +010053 struct card_handle *ch;
54 struct llist_head usb_out_queue;
55 struct ringbuf rb;
56 struct Usart_info usart_info;
57 int usb_pending_old;
58 uint8_t ep_out;
59 uint8_t ep_in;
60 uint8_t ep_int;
Harald Welte0eaa9922016-03-04 03:03:49 +010061 const Pin pin_insert;
Harald Welte07872b62016-03-20 11:45:36 +010062 uint32_t vcc_uv;
63 uint32_t vcc_uv_last;
Harald Welte13e82022016-03-02 15:17:53 +010064};
Harald Welte2a6d3af2016-02-28 19:29:14 +010065
Harald Welte8e7fca32017-05-07 16:14:33 +020066struct cardem_inst cardem_inst[] = {
Harald Welte2a6d3af2016-02-28 19:29:14 +010067 {
Harald Weltedde112e2016-03-20 16:42:11 +010068 .num = 0,
Harald Welte13e82022016-03-02 15:17:53 +010069 .usart_info = {
70 .base = USART1,
71 .id = ID_USART1,
72 .state = USART_RCV
73 },
Kévin Redona1012b12018-07-01 18:11:01 +020074 .ep_out = SIMTRACE_CARDEM_USB_EP_USIM1_DATAOUT,
75 .ep_in = SIMTRACE_CARDEM_USB_EP_USIM1_DATAIN,
76 .ep_int = SIMTRACE_CARDEM_USB_EP_USIM1_INT,
Harald Welte6d1128e2017-05-05 20:23:10 +020077#ifdef PIN_SET_USIM1_PRES
Harald Welte0eaa9922016-03-04 03:03:49 +010078 .pin_insert = PIN_SET_USIM1_PRES,
Harald Welte6d1128e2017-05-05 20:23:10 +020079#endif
Harald Welte2a6d3af2016-02-28 19:29:14 +010080 },
81#ifdef CARDEMU_SECOND_UART
82 {
Harald Weltedde112e2016-03-20 16:42:11 +010083 .num = 1,
Harald Welte13e82022016-03-02 15:17:53 +010084 .usart_info = {
85 .base = USART0,
86 .id = ID_USART0,
87 .state = USART_RCV
88 },
Kévin Redona1012b12018-07-01 18:11:01 +020089 .ep_out = SIMTRACE_CARDEM_USB_EP_USIM2_DATAOUT,
90 .ep_in = SIMTRACE_CARDEM_USB_EP_USIM2_DATAIN,
91 .ep_int = SIMTRACE_CARDEM_USB_EP_USIM2_INT,
Harald Welte6d1128e2017-05-05 20:23:10 +020092#ifdef PIN_SET_USIM2_PRES
Harald Welte0eaa9922016-03-04 03:03:49 +010093 .pin_insert = PIN_SET_USIM2_PRES,
Harald Welte6d1128e2017-05-05 20:23:10 +020094#endif
Harald Welte2a6d3af2016-02-28 19:29:14 +010095 },
96#endif
97};
98
99static Usart *get_usart_by_chan(uint8_t uart_chan)
100{
101 switch (uart_chan) {
102 case 0:
103 return USART1;
104#ifdef CARDEMU_SECOND_UART
105 case 1:
106 return USART0;
107#endif
108 }
109 return NULL;
110}
111
112/***********************************************************************
113 * Call-Backs from card_emu.c
114 ***********************************************************************/
115
Harald Weltec8beefb2016-03-20 14:40:47 +0100116static void wait_tx_idle(Usart *usart)
117{
118 int i = 1;
119
120 /* wait until last char has been fully transmitted */
121 while ((usart->US_CSR & (US_CSR_TXEMPTY)) == 0) {
122 if (!(i%1000000)) {
123 TRACE_ERROR("s: %x \r\n", usart->US_CSR);
124 }
125 i++;
126 }
127}
128
Harald Weltec58bba02016-03-20 14:57:53 +0100129void card_emu_uart_wait_tx_idle(uint8_t uart_chan)
130{
131 Usart *usart = get_usart_by_chan(uart_chan);
132 wait_tx_idle(usart);
133}
134
Harald Welte2a6d3af2016-02-28 19:29:14 +0100135/* call-back from card_emu.c to enable/disable transmit and/or receive */
136void card_emu_uart_enable(uint8_t uart_chan, uint8_t rxtx)
137{
138 Usart *usart = get_usart_by_chan(uart_chan);
139 switch (rxtx) {
140 case ENABLE_TX:
Harald Welte9dbc46e2016-02-29 10:05:10 +0100141 USART_DisableIt(usart, ~US_IER_TXRDY);
Harald Weltec8beefb2016-03-20 14:40:47 +0100142 /* as irritating as it is, we actually want to keep the
143 * receiver enabled during transmit */
144 USART_SetReceiverEnabled(usart, 1);
Harald Welte8a416b12016-03-01 00:42:04 +0100145 usart->US_CR = US_CR_RSTSTA | US_CR_RSTIT | US_CR_RSTNACK;
146 USART_EnableIt(usart, US_IER_TXRDY);
Harald Welte2a6d3af2016-02-28 19:29:14 +0100147 USART_SetTransmitterEnabled(usart, 1);
148 break;
149 case ENABLE_RX:
Harald Welte9dbc46e2016-02-29 10:05:10 +0100150 USART_DisableIt(usart, ~US_IER_RXRDY);
Harald Weltec8beefb2016-03-20 14:40:47 +0100151 /* as irritating as it is, we actually want to keep the
152 * transmitter enabled during receive */
153 USART_SetTransmitterEnabled(usart, 1);
154 wait_tx_idle(usart);
Harald Welte8a416b12016-03-01 00:42:04 +0100155 usart->US_CR = US_CR_RSTSTA | US_CR_RSTIT | US_CR_RSTNACK;
156 USART_EnableIt(usart, US_IER_RXRDY);
157 USART_SetReceiverEnabled(usart, 1);
Harald Welte2a6d3af2016-02-28 19:29:14 +0100158 break;
159 case 0:
160 default:
161 USART_SetTransmitterEnabled(usart, 0);
162 USART_SetReceiverEnabled(usart, 0);
Harald Welte9dbc46e2016-02-29 10:05:10 +0100163 USART_DisableIt(usart, 0xFFFFFFFF);
Harald Welte8a416b12016-03-01 00:42:04 +0100164 usart->US_CR = US_CR_RSTSTA | US_CR_RSTIT | US_CR_RSTNACK;
Harald Welte2a6d3af2016-02-28 19:29:14 +0100165 break;
166 }
167}
168
169/* call-back from card_emu.c to transmit a byte */
170int card_emu_uart_tx(uint8_t uart_chan, uint8_t byte)
171{
Harald Welte13e82022016-03-02 15:17:53 +0100172 Usart *usart = get_usart_by_chan(uart_chan);
173#if 0
Harald Welte2a6d3af2016-02-28 19:29:14 +0100174 Usart_info *ui = &usart_info[uart_chan];
175 ISO7816_SendChar(byte, ui);
Harald Welte13e82022016-03-02 15:17:53 +0100176#else
Harald Welte53079bb2016-03-20 14:58:35 +0100177 int i = 1;
Harald Welte13e82022016-03-02 15:17:53 +0100178 while ((usart->US_CSR & (US_CSR_TXRDY)) == 0) {
179 if (!(i%1000000)) {
Harald Weltedde112e2016-03-20 16:42:11 +0100180 TRACE_ERROR("%u: s: %x %02X\r\n",
181 uart_chan, usart->US_CSR,
182 usart->US_RHR & 0xFF);
Harald Welte13e82022016-03-02 15:17:53 +0100183 usart->US_CR = US_CR_RSTTX;
184 usart->US_CR = US_CR_RSTRX;
185 }
Harald Welte53079bb2016-03-20 14:58:35 +0100186 i++;
Harald Welte13e82022016-03-02 15:17:53 +0100187 }
188 usart->US_THR = byte;
Harald Welte53079bb2016-03-20 14:58:35 +0100189 //TRACE_ERROR("Sx%02x\r\n", byte);
Harald Welte13e82022016-03-02 15:17:53 +0100190#endif
Harald Welte2a6d3af2016-02-28 19:29:14 +0100191 return 1;
192}
193
194
195/* FIXME: integrate this with actual irq handler */
Harald Welte3bafe432016-03-20 16:43:12 +0100196static void usart_irq_rx(uint8_t inst_num)
Harald Welte2a6d3af2016-02-28 19:29:14 +0100197{
Harald Welte3bafe432016-03-20 16:43:12 +0100198 Usart *usart = get_usart_by_chan(inst_num);
199 struct cardem_inst *ci = &cardem_inst[inst_num];
Harald Weltec0bd7f02016-02-29 10:13:33 +0100200 uint32_t csr;
201 uint8_t byte = 0;
202
Harald Welteda15ca02016-03-17 21:14:04 +0100203 csr = usart->US_CSR & usart->US_IMR;
Harald Welte2a6d3af2016-02-28 19:29:14 +0100204
Harald Weltec0bd7f02016-02-29 10:13:33 +0100205 if (csr & US_CSR_RXRDY) {
Kévin Redon680bdab2018-07-18 14:00:57 +0200206 byte = (usart->US_RHR) & 0xFF;
Harald Welte05cc7f62018-07-04 04:39:40 +0200207 if (rbuf_write(&ci->rb, byte) < 0)
208 TRACE_ERROR("rbuf overrun\r\n");
Harald Weltec0bd7f02016-02-29 10:13:33 +0100209 }
Harald Welte2a6d3af2016-02-28 19:29:14 +0100210
Harald Welte12d4bdf2016-03-02 10:31:03 +0100211 if (csr & US_CSR_TXRDY) {
Harald Welte13e82022016-03-02 15:17:53 +0100212 if (card_emu_tx_byte(ci->ch) == 0)
Harald Welte12d4bdf2016-03-02 10:31:03 +0100213 USART_DisableIt(usart, US_IER_TXRDY);
214 }
215
Harald Welte2a6d3af2016-02-28 19:29:14 +0100216 if (csr & (US_CSR_OVRE|US_CSR_FRAME|US_CSR_PARE|
217 US_CSR_TIMEOUT|US_CSR_NACK|(1<<10))) {
Harald Weltec0bd7f02016-02-29 10:13:33 +0100218 usart->US_CR = US_CR_RSTSTA | US_CR_RSTIT | US_CR_RSTNACK;
Harald Weltedde112e2016-03-20 16:42:11 +0100219 TRACE_ERROR("%u e 0x%x st: 0x%x\n", ci->num, byte, csr);
Harald Welte2a6d3af2016-02-28 19:29:14 +0100220 }
221}
222
Harald Welte3bafe432016-03-20 16:43:12 +0100223void mode_cardemu_usart0_irq(void)
224{
225 /* USART0 == Instance 1 == USIM 2 */
226 usart_irq_rx(1);
227}
228
229void mode_cardemu_usart1_irq(void)
230{
231 /* USART1 == Instance 0 == USIM 1 */
232 usart_irq_rx(0);
233}
234
Harald Weltebd717682016-02-28 19:30:05 +0100235/* call-back from card_emu.c to change UART baud rate */
236int card_emu_uart_update_fidi(uint8_t uart_chan, unsigned int fidi)
237{
238 int rc;
Harald Weltebd717682016-02-28 19:30:05 +0100239 Usart *usart = get_usart_by_chan(uart_chan);
240
Harald Welte99f62a62016-02-29 10:08:49 +0100241 usart->US_CR |= US_CR_RXDIS | US_CR_RSTRX;
242 usart->US_FIDI = fidi & 0x3ff;
243 usart->US_CR |= US_CR_RXEN | US_CR_STTTO;
244 return 0;
Harald Weltebd717682016-02-28 19:30:05 +0100245}
Harald Welte2a6d3af2016-02-28 19:29:14 +0100246
247/***********************************************************************
Harald Welte419eb8a2016-03-20 10:04:03 +0100248 * ADC for VCC voltage detection
249 ***********************************************************************/
250
Harald Welte07872b62016-03-20 11:45:36 +0100251#ifdef DETECT_VCC_BY_ADC
252
253static int adc_triggered = 0;
Harald Welte28772eb2016-03-20 15:44:52 +0100254static int adc_sam3s_reva_errata = 0;
Harald Welte07872b62016-03-20 11:45:36 +0100255
Harald Welte419eb8a2016-03-20 10:04:03 +0100256static int card_vcc_adc_init(void)
257{
Harald Welte622b6be2016-03-20 16:43:59 +0100258 uint32_t chip_arch = CHIPID->CHIPID_CIDR & CHIPID_CIDR_ARCH_Msk;
259 uint32_t chip_ver = CHIPID->CHIPID_CIDR & CHIPID_CIDR_VERSION_Msk;
Harald Welte28772eb2016-03-20 15:44:52 +0100260
Harald Welte07872b62016-03-20 11:45:36 +0100261 PMC_EnablePeripheral(ID_ADC);
262
Harald Welte419eb8a2016-03-20 10:04:03 +0100263 ADC->ADC_CR |= ADC_CR_SWRST;
Harald Welte28772eb2016-03-20 15:44:52 +0100264 if (chip_ver == 0 &&
265 (chip_arch == CHIPID_CIDR_ARCH_SAM3SxA ||
266 chip_arch == CHIPID_CIDR_ARCH_SAM3SxB ||
267 chip_arch == CHIPID_CIDR_ARCH_SAM3SxC)) {
268 TRACE_INFO("Enabling Rev.A ADC Errata work-around\r\n");
269 adc_sam3s_reva_errata = 1;
270 }
271
272 if (adc_sam3s_reva_errata) {
273 /* Errata Work-Around to clear EOCx flags */
Harald Welte07872b62016-03-20 11:45:36 +0100274 volatile uint32_t foo;
275 int i;
276 for (i = 0; i < 16; i++)
277 foo = ADC->ADC_CDR[i];
278 }
279
280 /* Initialize ADC for AD7 / AD6, fADC=48/24=2MHz */
Harald Welte419eb8a2016-03-20 10:04:03 +0100281 ADC->ADC_MR = ADC_MR_TRGEN_DIS | ADC_MR_LOWRES_BITS_12 |
282 ADC_MR_SLEEP_NORMAL | ADC_MR_FWUP_OFF |
Harald Welte07872b62016-03-20 11:45:36 +0100283 ADC_MR_FREERUN_OFF | ADC_MR_PRESCAL(23) |
284 ADC_MR_STARTUP_SUT8 | ADC_MR_SETTLING(3) |
285 ADC_MR_ANACH_NONE | ADC_MR_TRACKTIM(4) |
Harald Welte419eb8a2016-03-20 10:04:03 +0100286 ADC_MR_TRANSFER(1) | ADC_MR_USEQ_NUM_ORDER;
287 /* enable AD6 + AD7 channels */
Harald Welte07872b62016-03-20 11:45:36 +0100288 ADC->ADC_CHER = ADC_CHER_CH7;
289 ADC->ADC_IER = ADC_IER_EOC7;
290#ifdef CARDEMU_SECOND_UART
291 ADC->ADC_CHER |= ADC_CHER_CH6;
292 ADC->ADC_IER |= ADC_IER_EOC6;
293#endif
294 NVIC_EnableIRQ(ADC_IRQn);
Harald Welte419eb8a2016-03-20 10:04:03 +0100295 ADC->ADC_CR |= ADC_CR_START;
Harald Welte07872b62016-03-20 11:45:36 +0100296
297 return 0;
Harald Welte419eb8a2016-03-20 10:04:03 +0100298}
299
Harald Welte07872b62016-03-20 11:45:36 +0100300#define VCC_UV_THRESH_1V8 1500000
301#define VCC_UV_THRESH_3V 2800000
302
303static void process_vcc_adc(struct cardem_inst *ci)
Harald Welte419eb8a2016-03-20 10:04:03 +0100304{
Harald Welte07872b62016-03-20 11:45:36 +0100305 if (ci->vcc_uv >= VCC_UV_THRESH_3V &&
306 ci->vcc_uv_last < VCC_UV_THRESH_3V) {
307 card_emu_io_statechg(ci->ch, CARD_IO_VCC, 1);
308 /* FIXME do this for real */
309 card_emu_io_statechg(ci->ch, CARD_IO_CLK, 1);
310 } else if (ci->vcc_uv < VCC_UV_THRESH_3V &&
311 ci->vcc_uv_last >= VCC_UV_THRESH_3V) {
312 /* FIXME do this for real */
313 card_emu_io_statechg(ci->ch, CARD_IO_CLK, 0);
314 card_emu_io_statechg(ci->ch, CARD_IO_VCC, 0);
Harald Welte419eb8a2016-03-20 10:04:03 +0100315 }
Harald Welte07872b62016-03-20 11:45:36 +0100316 ci->vcc_uv_last = ci->vcc_uv;
317}
318
Harald Welte07872b62016-03-20 11:45:36 +0100319void ADC_IrqHandler(void)
320{
321#ifdef CARDEMU_SECOND_UART
322 if (ADC->ADC_ISR & ADC_ISR_EOC6) {
323 uint16_t val = ADC->ADC_CDR[6] & 0xFFF;
324 cardem_inst[1].vcc_uv = adc2uv(val);
325 process_vcc_adc(&cardem_inst[1]);
Harald Welte28772eb2016-03-20 15:44:52 +0100326 if (adc_sam3s_reva_errata) {
327 /* Errata: START doesn't start a conversion
328 * sequence, but only a single conversion */
329 ADC->ADC_CR |= ADC_CR_START;
330 }
Harald Welte07872b62016-03-20 11:45:36 +0100331 }
332#endif
Harald Welte419eb8a2016-03-20 10:04:03 +0100333
334 if (ADC->ADC_ISR & ADC_ISR_EOC7) {
Harald Welte07872b62016-03-20 11:45:36 +0100335 uint16_t val = ADC->ADC_CDR[7] & 0xFFF;
336 cardem_inst[0].vcc_uv = adc2uv(val);
337 process_vcc_adc(&cardem_inst[0]);
338 ADC->ADC_CR |= ADC_CR_START;
Harald Welte419eb8a2016-03-20 10:04:03 +0100339 }
340}
Harald Welte07872b62016-03-20 11:45:36 +0100341#endif /* DETECT_VCC_BY_ADC */
Harald Welte419eb8a2016-03-20 10:04:03 +0100342
343/***********************************************************************
Kévin Redon680bdab2018-07-18 14:00:57 +0200344 * Core USB / main loop integration
Harald Welte2a6d3af2016-02-28 19:29:14 +0100345 ***********************************************************************/
346
Harald Weltec0bd7f02016-02-29 10:13:33 +0100347static void usim1_rst_irqhandler(const Pin *pPin)
348{
349 int active = PIO_Get(&pin_usim1_rst) ? 0 : 1;
Harald Welte13e82022016-03-02 15:17:53 +0100350 card_emu_io_statechg(cardem_inst[0].ch, CARD_IO_RST, active);
Harald Weltec0bd7f02016-02-29 10:13:33 +0100351}
352
Harald Welte07872b62016-03-20 11:45:36 +0100353#ifndef DETECT_VCC_BY_ADC
Harald Weltec0bd7f02016-02-29 10:13:33 +0100354static void usim1_vcc_irqhandler(const Pin *pPin)
355{
356 int active = PIO_Get(&pin_usim1_vcc) ? 1 : 0;
Harald Welte13e82022016-03-02 15:17:53 +0100357 card_emu_io_statechg(cardem_inst[0].ch, CARD_IO_VCC, active);
Harald Weltec0bd7f02016-02-29 10:13:33 +0100358 /* FIXME do this for real */
Harald Welte13e82022016-03-02 15:17:53 +0100359 card_emu_io_statechg(cardem_inst[0].ch, CARD_IO_CLK, active);
Harald Weltec0bd7f02016-02-29 10:13:33 +0100360}
Harald Welte07872b62016-03-20 11:45:36 +0100361#endif /* !DETECT_VCC_BY_ADC */
Harald Weltec0bd7f02016-02-29 10:13:33 +0100362
Harald Welte13e82022016-03-02 15:17:53 +0100363#ifdef CARDEMU_SECOND_UART
364static void usim2_rst_irqhandler(const Pin *pPin)
365{
366 int active = PIO_Get(&pin_usim2_rst) ? 0 : 1;
367 card_emu_io_statechg(cardem_inst[1].ch, CARD_IO_RST, active);
368}
369
Harald Welte07872b62016-03-20 11:45:36 +0100370#ifndef DETECT_VCC_BY_ADC
Harald Welte13e82022016-03-02 15:17:53 +0100371static void usim2_vcc_irqhandler(const Pin *pPin)
372{
373 int active = PIO_Get(&pin_usim2_vcc) ? 1 : 0;
374 card_emu_io_statechg(cardem_inst[1].ch, CARD_IO_VCC, active);
375 /* FIXME do this for real */
376 card_emu_io_statechg(cardem_inst[1].ch, CARD_IO_CLK, active);
377}
Harald Welte07872b62016-03-20 11:45:36 +0100378#endif /* !DETECT_VCC_BY_ADC */
379#endif /* CARDEMU_SECOND_UART */
Harald Welte13e82022016-03-02 15:17:53 +0100380
Harald Welte2a6d3af2016-02-28 19:29:14 +0100381/* executed once at system boot for each config */
382void mode_cardemu_configure(void)
383{
Harald Weltebd717682016-02-28 19:30:05 +0100384 TRACE_ENTRY();
Harald Welte2a6d3af2016-02-28 19:29:14 +0100385}
386
387/* called if config is activated */
388void mode_cardemu_init(void)
389{
Harald Welte13e82022016-03-02 15:17:53 +0100390 int i;
Harald Weltebd717682016-02-28 19:30:05 +0100391
Harald Welte13e82022016-03-02 15:17:53 +0100392 TRACE_ENTRY();
Harald Weltec0bd7f02016-02-29 10:13:33 +0100393
Harald Welte6d1128e2017-05-05 20:23:10 +0200394#ifdef PINS_CARDSIM
Harald Welte2a6d3af2016-02-28 19:29:14 +0100395 PIO_Configure(pins_cardsim, PIO_LISTSIZE(pins_cardsim));
Harald Welte6d1128e2017-05-05 20:23:10 +0200396#endif
Harald Welte07872b62016-03-20 11:45:36 +0100397#ifdef DETECT_VCC_BY_ADC
398 card_vcc_adc_init();
399#endif /* DETECT_VCC_BY_ADC */
Harald Welte2a6d3af2016-02-28 19:29:14 +0100400
Harald Welte13e82022016-03-02 15:17:53 +0100401 INIT_LLIST_HEAD(&cardem_inst[0].usb_out_queue);
402 rbuf_reset(&cardem_inst[0].rb);
Harald Welte2a6d3af2016-02-28 19:29:14 +0100403 PIO_Configure(pins_usim1, PIO_LISTSIZE(pins_usim1));
Harald Welte13e82022016-03-02 15:17:53 +0100404 ISO7816_Init(&cardem_inst[0].usart_info, CLK_SLAVE);
Harald Welte2a6d3af2016-02-28 19:29:14 +0100405 NVIC_EnableIRQ(USART1_IRQn);
Harald Weltec0bd7f02016-02-29 10:13:33 +0100406 PIO_ConfigureIt(&pin_usim1_rst, usim1_rst_irqhandler);
407 PIO_EnableIt(&pin_usim1_rst);
Harald Welte07872b62016-03-20 11:45:36 +0100408#ifndef DETECT_VCC_BY_ADC
Harald Weltec0bd7f02016-02-29 10:13:33 +0100409 PIO_ConfigureIt(&pin_usim1_vcc, usim1_vcc_irqhandler);
410 PIO_EnableIt(&pin_usim1_vcc);
Harald Welte07872b62016-03-20 11:45:36 +0100411#endif /* DETECT_VCC_BY_ADC */
Kévin Redona1012b12018-07-01 18:11:01 +0200412 cardem_inst[0].ch = card_emu_init(0, 2, 0, SIMTRACE_CARDEM_USB_EP_USIM1_DATAIN, SIMTRACE_CARDEM_USB_EP_USIM1_INT);
Harald Weltec47fc5f2017-05-11 16:51:57 +0200413 sim_switch_use_physical(0, 1);
Harald Welte2a6d3af2016-02-28 19:29:14 +0100414
415#ifdef CARDEMU_SECOND_UART
Harald Welte13e82022016-03-02 15:17:53 +0100416 INIT_LLIST_HEAD(&cardem_inst[1].usb_out_queue);
417 rbuf_reset(&cardem_inst[1].rb);
Harald Welte2a6d3af2016-02-28 19:29:14 +0100418 PIO_Configure(pins_usim2, PIO_LISTSIZE(pins_usim2));
Harald Welte13e82022016-03-02 15:17:53 +0100419 ISO7816_Init(&cardem_inst[1].usart_info, CLK_SLAVE);
Harald Welte2a6d3af2016-02-28 19:29:14 +0100420 NVIC_EnableIRQ(USART0_IRQn);
Harald Welte13e82022016-03-02 15:17:53 +0100421 PIO_ConfigureIt(&pin_usim2_rst, usim2_rst_irqhandler);
422 PIO_EnableIt(&pin_usim2_rst);
Harald Welte07872b62016-03-20 11:45:36 +0100423#ifndef DETECT_VCC_BY_ADC
Harald Welte13e82022016-03-02 15:17:53 +0100424 PIO_ConfigureIt(&pin_usim2_vcc, usim2_vcc_irqhandler);
425 PIO_EnableIt(&pin_usim2_vcc);
Harald Welte07872b62016-03-20 11:45:36 +0100426#endif /* DETECT_VCC_BY_ADC */
Kévin Redona1012b12018-07-01 18:11:01 +0200427 cardem_inst[1].ch = card_emu_init(1, 0, 1, SIMTRACE_CARDEM_USB_EP_USIM2_DATAIN, SIMTRACE_CARDEM_USB_EP_USIM2_INT);
Harald Weltec47fc5f2017-05-11 16:51:57 +0200428 sim_switch_use_physical(1, 1);
Harald Welte07872b62016-03-20 11:45:36 +0100429#endif /* CARDEMU_SECOND_UART */
Harald Weltec47fc5f2017-05-11 16:51:57 +0200430
Harald Welte2a6d3af2016-02-28 19:29:14 +0100431}
432
433/* called if config is deactivated */
434void mode_cardemu_exit(void)
435{
Harald Weltebd717682016-02-28 19:30:05 +0100436 TRACE_ENTRY();
437
Harald Welte7abdb512016-03-03 17:48:32 +0100438 /* FIXME: stop tc_fdt */
Harald Welte8e7fca32017-05-07 16:14:33 +0200439 /* FIXME: release all msg, unlink them from any queue */
Harald Welte7abdb512016-03-03 17:48:32 +0100440
Harald Weltec0bd7f02016-02-29 10:13:33 +0100441 PIO_DisableIt(&pin_usim1_rst);
442 PIO_DisableIt(&pin_usim1_vcc);
443
Harald Welte2a6d3af2016-02-28 19:29:14 +0100444 NVIC_DisableIRQ(USART1_IRQn);
445 USART_SetTransmitterEnabled(USART1, 0);
446 USART_SetReceiverEnabled(USART1, 0);
447
448#ifdef CARDEMU_SECOND_UART
Harald Welte13e82022016-03-02 15:17:53 +0100449 PIO_DisableIt(&pin_usim2_rst);
450 PIO_DisableIt(&pin_usim2_vcc);
451
Harald Welte2a6d3af2016-02-28 19:29:14 +0100452 NVIC_DisableIRQ(USART0_IRQn);
453 USART_SetTransmitterEnabled(USART0, 0);
454 USART_SetReceiverEnabled(USART0, 0);
455#endif
456}
457
Harald Welteebb80ed2016-03-02 13:56:59 +0100458/* handle a single USB command as received from the USB host */
Harald Welte25a9a802017-05-08 13:30:09 +0200459static void dispatch_usb_command_generic(struct msgb *msg, struct cardem_inst *ci)
Harald Welteebb80ed2016-03-02 13:56:59 +0100460{
Harald Welte25a9a802017-05-08 13:30:09 +0200461 struct simtrace_msg_hdr *hdr;
462
463 hdr = (struct simtrace_msg_hdr *) msg->l1h;
464 switch (hdr->msg_type) {
465 case SIMTRACE_CMD_BD_BOARD_INFO:
466 break;
467 default:
468 break;
469 }
470 usb_buf_free(msg);
471}
472
473/* handle a single USB command as received from the USB host */
474static void dispatch_usb_command_cardem(struct msgb *msg, struct cardem_inst *ci)
475{
476 struct simtrace_msg_hdr *hdr;
Harald Welte06b27f62016-03-02 14:26:38 +0100477 struct cardemu_usb_msg_set_atr *atr;
Harald Welte0eaa9922016-03-04 03:03:49 +0100478 struct cardemu_usb_msg_cardinsert *cardins;
Harald Welteebb80ed2016-03-02 13:56:59 +0100479 struct llist_head *queue;
480
Harald Welte25a9a802017-05-08 13:30:09 +0200481 hdr = (struct simtrace_msg_hdr *) msg->l1h;
Harald Welteebb80ed2016-03-02 13:56:59 +0100482 switch (hdr->msg_type) {
Harald Welte25a9a802017-05-08 13:30:09 +0200483 case SIMTRACE_MSGT_DT_CEMU_TX_DATA:
Harald Welte0eaa9922016-03-04 03:03:49 +0100484 queue = card_emu_get_uart_tx_queue(ci->ch);
Harald Welte8e7fca32017-05-07 16:14:33 +0200485 llist_add_tail(&msg->list, queue);
Harald Welte0eaa9922016-03-04 03:03:49 +0100486 card_emu_have_new_uart_tx(ci->ch);
Harald Welteebb80ed2016-03-02 13:56:59 +0100487 break;
Harald Welte25a9a802017-05-08 13:30:09 +0200488 case SIMTRACE_MSGT_DT_CEMU_SET_ATR:
489 atr = (struct cardemu_usb_msg_set_atr *) msg->l2h;
Harald Welted295b922016-03-18 21:01:36 +0100490 card_emu_set_atr(ci->ch, atr->atr, atr->atr_len);
Harald Welte8e7fca32017-05-07 16:14:33 +0200491 usb_buf_free(msg);
Harald Welte06b27f62016-03-02 14:26:38 +0100492 break;
Harald Welte25a9a802017-05-08 13:30:09 +0200493 case SIMTRACE_MSGT_DT_CEMU_CARDINSERT:
494 cardins = (struct cardemu_usb_msg_cardinsert *) msg->l2h;
Harald Welte965d5c92017-11-03 20:47:12 +0100495 if (!ci->pin_insert.pio) {
496 TRACE_INFO("%u: skipping unsupported card_insert to %s\r\n",
497 ci->num, cardins->card_insert ? "INSERTED" : "REMOVED");
498 break;
499 }
Harald Weltedde112e2016-03-20 16:42:11 +0100500 TRACE_INFO("%u: set card_insert to %s\r\n", ci->num,
501 cardins->card_insert ? "INSERTED" : "REMOVED");
Harald Welte0eaa9922016-03-04 03:03:49 +0100502 if (cardins->card_insert)
503 PIO_Set(&ci->pin_insert);
504 else
505 PIO_Clear(&ci->pin_insert);
Harald Welte8e7fca32017-05-07 16:14:33 +0200506 usb_buf_free(msg);
Harald Welte0eaa9922016-03-04 03:03:49 +0100507 break;
Harald Welte25a9a802017-05-08 13:30:09 +0200508 case SIMTRACE_MSGT_BD_CEMU_STATUS:
Harald Welteff160652016-03-19 21:59:06 +0100509 card_emu_report_status(ci->ch);
Harald Welte25a9a802017-05-08 13:30:09 +0200510 usb_buf_free(msg);
Harald Welteff160652016-03-19 21:59:06 +0100511 break;
Harald Welte25a9a802017-05-08 13:30:09 +0200512 case SIMTRACE_MSGT_BD_CEMU_STATS:
Harald Welteebb80ed2016-03-02 13:56:59 +0100513 default:
Harald Welte25a9a802017-05-08 13:30:09 +0200514 /* FIXME: Send Error */
515 usb_buf_free(msg);
516 break;
517 }
518}
519
Harald Weltefc87c242017-11-28 19:17:27 +0100520#ifdef PINS_PERST
521#include "wwan_perst.h"
522#endif
523
Harald Welte2e9254a2017-05-09 06:30:04 +0200524static int usb_command_modem_reset(struct msgb *msg, struct cardem_inst *ci)
525{
Harald Welteb91f6ad2017-05-10 22:51:30 +0200526 struct st_modem_reset *mr = (struct st_modem_reset *) msg->l2h;
Harald Welte2e9254a2017-05-09 06:30:04 +0200527
528 if (msgb_l2len(msg) < sizeof(*mr))
529 return -1;
530
531 switch (mr->asserted) {
Harald Weltefc87c242017-11-28 19:17:27 +0100532#ifdef PINS_PERST
Harald Welte2e9254a2017-05-09 06:30:04 +0200533 case 0:
534 wwan_perst_set(ci->num, 0);
535 break;
536 case 1:
537 wwan_perst_set(ci->num, 1);
538 break;
539 case 2:
540 wwan_perst_do_reset_pulse(ci->num, mr->pulse_duration_msec);
541 break;
Harald Weltefc87c242017-11-28 19:17:27 +0100542#endif
Harald Welte2e9254a2017-05-09 06:30:04 +0200543 default:
544 return -1;
545 }
546
547 return 0;
548}
549
Harald Welte5c583d32017-05-09 06:46:47 +0200550static int usb_command_sim_select(struct msgb *msg, struct cardem_inst *ci)
551{
Harald Welteb91f6ad2017-05-10 22:51:30 +0200552 struct st_modem_sim_select *mss = (struct st_modem_sim_select *) msg->l2h;
Harald Welte5c583d32017-05-09 06:46:47 +0200553
554 if (msgb_l2len(msg) < sizeof(*mss))
555 return -1;
556
557 if (mss->remote_sim)
558 sim_switch_use_physical(ci->num, 0);
559 else
560 sim_switch_use_physical(ci->num, 1);
561
562 return 0;
563}
564
Harald Welte25a9a802017-05-08 13:30:09 +0200565/* handle a single USB command as received from the USB host */
566static void dispatch_usb_command_modem(struct msgb *msg, struct cardem_inst *ci)
567{
568 struct simtrace_msg_hdr *hdr;
569
570 hdr = (struct simtrace_msg_hdr *) msg->l1h;
571 switch (hdr->msg_type) {
572 case SIMTRACE_MSGT_DT_MODEM_RESET:
Harald Welte2e9254a2017-05-09 06:30:04 +0200573 usb_command_modem_reset(msg, ci);
Harald Welte25a9a802017-05-08 13:30:09 +0200574 break;
575 case SIMTRACE_MSGT_DT_MODEM_SIM_SELECT:
Harald Welte5c583d32017-05-09 06:46:47 +0200576 usb_command_sim_select(msg, ci);
Harald Welte25a9a802017-05-08 13:30:09 +0200577 break;
578 case SIMTRACE_MSGT_BD_MODEM_STATUS:
579 break;
580 default:
581 break;
582 }
583 usb_buf_free(msg);
584}
585
586/* handle a single USB command as received from the USB host */
587static void dispatch_usb_command(struct msgb *msg, struct cardem_inst *ci)
588{
Harald Welteb91f6ad2017-05-10 22:51:30 +0200589 struct simtrace_msg_hdr *sh = (struct simtrace_msg_hdr *) msg->l1h;
Harald Welte25a9a802017-05-08 13:30:09 +0200590
591 if (msgb_length(msg) < sizeof(*sh)) {
592 /* FIXME: Error */
593 usb_buf_free(msg);
594 return;
595 }
596
597 switch (sh->msg_class) {
598 case SIMTRACE_MSGC_GENERIC:
599 dispatch_usb_command_generic(msg, ci);
600 break;
601 case SIMTRACE_MSGC_CARDEM:
602 dispatch_usb_command_cardem(msg, ci);
603 break;
604 case SIMTRACE_MSGC_MODEM:
Harald Welte23c00b62017-05-11 01:11:43 +0200605 /* FIXME: Find out why this fails if used for !=
606 * MSGC_MODEM ?!? */
607 msg->l2h = msg->l1h + sizeof(*sh);
Harald Welte25a9a802017-05-08 13:30:09 +0200608 dispatch_usb_command_modem(msg, ci);
609 break;
610 default:
611 /* FIXME: Send Error */
Harald Welte8e7fca32017-05-07 16:14:33 +0200612 usb_buf_free(msg);
Harald Welteebb80ed2016-03-02 13:56:59 +0100613 break;
614 }
615}
616
Harald Welte8e7fca32017-05-07 16:14:33 +0200617static void dispatch_received_msg(struct msgb *msg, struct cardem_inst *ci)
Harald Welteb26d0032016-03-19 13:33:02 +0100618{
Harald Welte8e7fca32017-05-07 16:14:33 +0200619 struct msgb *segm;
Harald Welte25a9a802017-05-08 13:30:09 +0200620 struct simtrace_msg_hdr *mh;
Harald Welteb26d0032016-03-19 13:33:02 +0100621
622 /* check if we have multiple concatenated commands in
623 * one message. USB endpoints are streams that don't
624 * preserve the message boundaries */
Harald Welte25a9a802017-05-08 13:30:09 +0200625 mh = (struct simtrace_msg_hdr *) msg->data;
Harald Welte8e7fca32017-05-07 16:14:33 +0200626 if (mh->msg_len == msgb_length(msg)) {
Harald Welteb26d0032016-03-19 13:33:02 +0100627 /* fast path: only one message in buffer */
Harald Welte8e7fca32017-05-07 16:14:33 +0200628 dispatch_usb_command(msg, ci);
Harald Welteb26d0032016-03-19 13:33:02 +0100629 return;
630 }
631
632 /* slow path: iterate over list of messages, allocating one new
633 * reqe_ctx per segment */
Harald Welte8e7fca32017-05-07 16:14:33 +0200634 while (1) {
Harald Welte25a9a802017-05-08 13:30:09 +0200635 mh = (struct simtrace_msg_hdr *) msg->data;
Harald Welte8e7fca32017-05-07 16:14:33 +0200636
637 segm = usb_buf_alloc(ci->ep_out);
Harald Welteb26d0032016-03-19 13:33:02 +0100638 if (!segm) {
Harald Welte8e7fca32017-05-07 16:14:33 +0200639 TRACE_ERROR("%u: ENOMEM during msg segmentation\r\n",
Harald Weltedde112e2016-03-20 16:42:11 +0100640 ci->num);
Harald Welteb26d0032016-03-19 13:33:02 +0100641 break;
642 }
Harald Welte8e7fca32017-05-07 16:14:33 +0200643
644 if (mh->msg_len > msgb_length(msg)) {
645 TRACE_ERROR("%u: Unexpected large message (%u bytes)\n",
646 ci->num, mh->msg_len);
647 usb_buf_free(segm);
648 } else {
649 uint8_t *cur = msgb_put(segm, mh->msg_len);
650 segm->l1h = segm->head;
651 memcpy(cur, mh, mh->msg_len);
652 dispatch_usb_command(segm, ci);
653 }
654 /* pull this message */
655 msgb_pull(msg, mh->msg_len);
656 /* abort if we're done */
657 if (msgb_length(msg) <= 0)
658 break;
Harald Welteb26d0032016-03-19 13:33:02 +0100659 }
660
Harald Welte8e7fca32017-05-07 16:14:33 +0200661 usb_buf_free(msg);
Harald Welteb26d0032016-03-19 13:33:02 +0100662}
663
Harald Welteebb80ed2016-03-02 13:56:59 +0100664/* iterate over the queue of incoming USB commands and dispatch/execute
665 * them */
Harald Welte9f240b62016-03-18 10:32:56 +0100666static void process_any_usb_commands(struct llist_head *main_q,
667 struct cardem_inst *ci)
Harald Welteebb80ed2016-03-02 13:56:59 +0100668{
Harald Welte9f240b62016-03-18 10:32:56 +0100669 struct llist_head *lh;
Harald Welte8e7fca32017-05-07 16:14:33 +0200670 struct msgb *msg;
Harald Welte9f240b62016-03-18 10:32:56 +0100671 int i;
Harald Welteebb80ed2016-03-02 13:56:59 +0100672
Harald Welte9f240b62016-03-18 10:32:56 +0100673 /* limit the number of iterations to 10, to ensure we don't get
674 * stuck here without returning to main loop processing */
675 for (i = 0; i < 10; i++) {
676 /* de-queue the list head in an irq-safe way */
677 lh = llist_head_dequeue_irqsafe(main_q);
678 if (!lh)
679 break;
Harald Welte8e7fca32017-05-07 16:14:33 +0200680 msg = llist_entry(lh, struct msgb, list);
681 dispatch_received_msg(msg, ci);
Harald Welte5820ea92016-03-16 22:12:00 +0100682 }
Harald Welteebb80ed2016-03-02 13:56:59 +0100683}
684
Harald Welte2a6d3af2016-02-28 19:29:14 +0100685/* main loop function, called repeatedly */
686void mode_cardemu_run(void)
687{
Harald Welteacae4122016-03-02 10:27:58 +0100688 struct llist_head *queue;
Harald Welte13e82022016-03-02 15:17:53 +0100689 unsigned int i;
Harald Welteacae4122016-03-02 10:27:58 +0100690
Harald Welte13e82022016-03-02 15:17:53 +0100691 for (i = 0; i < ARRAY_SIZE(cardem_inst); i++) {
692 struct cardem_inst *ci = &cardem_inst[i];
693
Harald Welte54cb3d02016-02-29 14:12:40 +0100694 /* drain the ring buffer from UART into card_emu */
Harald Weltec0bd7f02016-02-29 10:13:33 +0100695 while (1) {
696 __disable_irq();
Harald Welte13e82022016-03-02 15:17:53 +0100697 if (rbuf_is_empty(&ci->rb)) {
Harald Weltec0bd7f02016-02-29 10:13:33 +0100698 __enable_irq();
699 break;
700 }
Harald Welte13e82022016-03-02 15:17:53 +0100701 uint8_t byte = rbuf_read(&ci->rb);
Harald Weltec0bd7f02016-02-29 10:13:33 +0100702 __enable_irq();
Harald Welte13e82022016-03-02 15:17:53 +0100703 card_emu_process_rx_byte(ci->ch, byte);
Harald Weltedde112e2016-03-20 16:42:11 +0100704 //TRACE_ERROR("%uRx%02x\r\n", i, byte);
Harald Weltec0bd7f02016-02-29 10:13:33 +0100705 }
Harald Welteacae4122016-03-02 10:27:58 +0100706
Harald Welte8e7fca32017-05-07 16:14:33 +0200707 /* first try to send any pending messages on IRQ */
708 usb_refill_to_host(ci->ep_int);
709
710 /* then try to send any pending messages on IN */
711 usb_refill_to_host(ci->ep_in);
Harald Welteacae4122016-03-02 10:27:58 +0100712
Harald Welteebb80ed2016-03-02 13:56:59 +0100713 /* ensure we can handle incoming USB messages from the
714 * host */
Harald Welte8e7fca32017-05-07 16:14:33 +0200715 usb_refill_from_host(ci->ep_out);
716 queue = usb_get_queue(ci->ep_out);
Harald Welte0eaa9922016-03-04 03:03:49 +0100717 process_any_usb_commands(queue, ci);
Harald Welte2a6d3af2016-02-28 19:29:14 +0100718 }
Harald Welte2a6d3af2016-02-28 19:29:14 +0100719}