blob: 4886983d27a7d16f0ffcbcedc457a7d8af300f26 [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>
Kévin Redonb218cc32020-03-26 01:46:45 +01004 * (C) 2018-2019 by sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de>
Kévin Redon9a12d682018-07-08 13:21:16 +02005 *
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 Welte2a6d3af2016-02-28 19:29:14 +010020#include "board.h"
Kévin Redonac0843a2018-07-11 09:23:33 +020021#include "boardver_adc.h"
Harald Welte16055642016-03-03 11:02:45 +010022#include "simtrace.h"
23#include "ringbuffer.h"
Harald Welte2a6d3af2016-02-28 19:29:14 +010024#include "card_emu.h"
Harald Weltebd717682016-02-28 19:30:05 +010025#include "iso7816_fidi.h"
Harald Welte54cb3d02016-02-29 14:12:40 +010026#include "utils.h"
Harald Welte9d90d282018-06-29 22:25:42 +020027#include <osmocom/core/linuxlist.h>
28#include <osmocom/core/msgb.h>
Harald Welte9f240b62016-03-18 10:32:56 +010029#include "llist_irqsafe.h"
Harald Welte8e7fca32017-05-07 16:14:33 +020030#include "usb_buf.h"
Kévin Redona1012b12018-07-01 18:11:01 +020031#include "simtrace_usb.h"
Harald Welte25a9a802017-05-08 13:30:09 +020032#include "simtrace_prot.h"
Harald Welte5c583d32017-05-09 06:46:47 +020033#include "sim_switch.h"
Harald Weltebd717682016-02-28 19:30:05 +010034
Harald Weltefd9c0412016-03-20 18:15:57 +010035#define TRACE_ENTRY() TRACE_DEBUG("%s entering\r\n", __func__)
Harald Welte2a6d3af2016-02-28 19:29:14 +010036
Harald Welte6d1128e2017-05-05 20:23:10 +020037#ifdef PINS_CARDSIM
Harald Welte2a6d3af2016-02-28 19:29:14 +010038static const Pin pins_cardsim[] = PINS_CARDSIM;
Harald Welte6d1128e2017-05-05 20:23:10 +020039#endif
Harald Welte2a6d3af2016-02-28 19:29:14 +010040
41/* UART pins */
42static const Pin pins_usim1[] = {PINS_USIM1};
43static const Pin pin_usim1_rst = PIN_USIM1_nRST;
44static const Pin pin_usim1_vcc = PIN_USIM1_VCC;
45
46#ifdef CARDEMU_SECOND_UART
Kévin Redon7233cf82019-11-14 19:37:32 +010047static const Pin pins_usim2[] = {PINS_USIM2};
Harald Welte2a6d3af2016-02-28 19:29:14 +010048static const Pin pin_usim2_rst = PIN_USIM2_nRST;
49static const Pin pin_usim2_vcc = PIN_USIM2_VCC;
50#endif
51
Harald Welte13e82022016-03-02 15:17:53 +010052struct cardem_inst {
Harald Weltec3941092018-08-26 09:53:13 +020053 unsigned int num;
Harald Welte13e82022016-03-02 15:17:53 +010054 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 Welte0eaa9922016-03-04 03:03:49 +010062 const Pin pin_insert;
Harald Weltefa197ca2019-12-14 22:13:29 +010063#ifdef DETECT_VCC_BY_ADC
Harald Welte07872b62016-03-20 11:45:36 +010064 uint32_t vcc_uv;
Harald Weltefa197ca2019-12-14 22:13:29 +010065#endif
66 bool vcc_active;
67 bool vcc_active_last;
68 bool rst_active;
69 bool rst_active_last;
Harald Welte13e82022016-03-02 15:17:53 +010070};
Harald Welte2a6d3af2016-02-28 19:29:14 +010071
Harald Welte8e7fca32017-05-07 16:14:33 +020072struct cardem_inst cardem_inst[] = {
Harald Welte2a6d3af2016-02-28 19:29:14 +010073 {
Harald Weltedde112e2016-03-20 16:42:11 +010074 .num = 0,
Harald Welte13e82022016-03-02 15:17:53 +010075 .usart_info = {
76 .base = USART1,
77 .id = ID_USART1,
78 .state = USART_RCV
79 },
Kévin Redona1012b12018-07-01 18:11:01 +020080 .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 Welte6d1128e2017-05-05 20:23:10 +020083#ifdef PIN_SET_USIM1_PRES
Harald Welte0eaa9922016-03-04 03:03:49 +010084 .pin_insert = PIN_SET_USIM1_PRES,
Harald Welte6d1128e2017-05-05 20:23:10 +020085#endif
Harald Welte2a6d3af2016-02-28 19:29:14 +010086 },
87#ifdef CARDEMU_SECOND_UART
88 {
Harald Weltedde112e2016-03-20 16:42:11 +010089 .num = 1,
Harald Welte13e82022016-03-02 15:17:53 +010090 .usart_info = {
91 .base = USART0,
92 .id = ID_USART0,
93 .state = USART_RCV
94 },
Kévin Redona1012b12018-07-01 18:11:01 +020095 .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 Welte6d1128e2017-05-05 20:23:10 +020098#ifdef PIN_SET_USIM2_PRES
Harald Welte0eaa9922016-03-04 03:03:49 +010099 .pin_insert = PIN_SET_USIM2_PRES,
Harald Welte6d1128e2017-05-05 20:23:10 +0200100#endif
Harald Welte2a6d3af2016-02-28 19:29:14 +0100101 },
102#endif
103};
104
105static Usart *get_usart_by_chan(uint8_t uart_chan)
106{
Kévin Redonebe672e2018-07-29 00:18:12 +0200107 if (uart_chan < ARRAY_SIZE(cardem_inst)) {
108 return cardem_inst[uart_chan].usart_info.base;
109 } else {
110 return NULL;
Harald Welte2a6d3af2016-02-28 19:29:14 +0100111 }
Harald Welte2a6d3af2016-02-28 19:29:14 +0100112}
113
114/***********************************************************************
115 * Call-Backs from card_emu.c
116 ***********************************************************************/
117
Harald Weltec8beefb2016-03-20 14:40:47 +0100118static 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 Weltec3941092018-08-26 09:53:13 +0200125 TRACE_ERROR("s: %lx \r\n", usart->US_CSR);
Harald Weltec8beefb2016-03-20 14:40:47 +0100126 }
127 i++;
128 }
129}
130
Harald Weltec58bba02016-03-20 14:57:53 +0100131void 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 Welte2a6d3af2016-02-28 19:29:14 +0100137/* call-back from card_emu.c to enable/disable transmit and/or receive */
138void 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 Welte9dbc46e2016-02-29 10:05:10 +0100143 USART_DisableIt(usart, ~US_IER_TXRDY);
Harald Weltec8beefb2016-03-20 14:40:47 +0100144 /* as irritating as it is, we actually want to keep the
145 * receiver enabled during transmit */
146 USART_SetReceiverEnabled(usart, 1);
Harald Welte8a416b12016-03-01 00:42:04 +0100147 usart->US_CR = US_CR_RSTSTA | US_CR_RSTIT | US_CR_RSTNACK;
148 USART_EnableIt(usart, US_IER_TXRDY);
Harald Welte2a6d3af2016-02-28 19:29:14 +0100149 USART_SetTransmitterEnabled(usart, 1);
150 break;
151 case ENABLE_RX:
Harald Welte9dbc46e2016-02-29 10:05:10 +0100152 USART_DisableIt(usart, ~US_IER_RXRDY);
Harald Weltec8beefb2016-03-20 14:40:47 +0100153 /* 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 Welte8a416b12016-03-01 00:42:04 +0100157 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 Welte2a6d3af2016-02-28 19:29:14 +0100160 break;
161 case 0:
162 default:
163 USART_SetTransmitterEnabled(usart, 0);
164 USART_SetReceiverEnabled(usart, 0);
Harald Welte9dbc46e2016-02-29 10:05:10 +0100165 USART_DisableIt(usart, 0xFFFFFFFF);
Harald Welte8a416b12016-03-01 00:42:04 +0100166 usart->US_CR = US_CR_RSTSTA | US_CR_RSTIT | US_CR_RSTNACK;
Harald Welte2a6d3af2016-02-28 19:29:14 +0100167 break;
168 }
169}
170
171/* call-back from card_emu.c to transmit a byte */
172int card_emu_uart_tx(uint8_t uart_chan, uint8_t byte)
173{
Harald Welte13e82022016-03-02 15:17:53 +0100174 Usart *usart = get_usart_by_chan(uart_chan);
175#if 0
Harald Welte2a6d3af2016-02-28 19:29:14 +0100176 Usart_info *ui = &usart_info[uart_chan];
177 ISO7816_SendChar(byte, ui);
Harald Welte13e82022016-03-02 15:17:53 +0100178#else
Harald Welte53079bb2016-03-20 14:58:35 +0100179 int i = 1;
Harald Welte13e82022016-03-02 15:17:53 +0100180 while ((usart->US_CSR & (US_CSR_TXRDY)) == 0) {
181 if (!(i%1000000)) {
Harald Weltec3941092018-08-26 09:53:13 +0200182 TRACE_ERROR("%u: s: %lx %02lX\r\n",
Harald Weltedde112e2016-03-20 16:42:11 +0100183 uart_chan, usart->US_CSR,
184 usart->US_RHR & 0xFF);
Harald Welte13e82022016-03-02 15:17:53 +0100185 usart->US_CR = US_CR_RSTTX;
186 usart->US_CR = US_CR_RSTRX;
187 }
Harald Welte53079bb2016-03-20 14:58:35 +0100188 i++;
Harald Welte13e82022016-03-02 15:17:53 +0100189 }
190 usart->US_THR = byte;
Harald Welte53079bb2016-03-20 14:58:35 +0100191 //TRACE_ERROR("Sx%02x\r\n", byte);
Harald Welte13e82022016-03-02 15:17:53 +0100192#endif
Harald Welte2a6d3af2016-02-28 19:29:14 +0100193 return 1;
194}
195
196
Kévin Redonb218cc32020-03-26 01:46:45 +0100197/*! common handler if interrupt was received.
198 * \param[in] inst_num Instance number, range 0..1 (some boards only '0' permitted) */
Harald Welte3bafe432016-03-20 16:43:12 +0100199static void usart_irq_rx(uint8_t inst_num)
Harald Welte2a6d3af2016-02-28 19:29:14 +0100200{
Harald Welte3bafe432016-03-20 16:43:12 +0100201 Usart *usart = get_usart_by_chan(inst_num);
202 struct cardem_inst *ci = &cardem_inst[inst_num];
Harald Weltec0bd7f02016-02-29 10:13:33 +0100203 uint32_t csr;
204 uint8_t byte = 0;
205
Kévin Redonb218cc32020-03-26 01:46:45 +0100206 /* get one atomic snapshot of state/flags before they get changed */
Harald Welteda15ca02016-03-17 21:14:04 +0100207 csr = usart->US_CSR & usart->US_IMR;
Harald Welte2a6d3af2016-02-28 19:29:14 +0100208
Kévin Redonb218cc32020-03-26 01:46:45 +0100209 /* check if one byte has been completely received and is now in the holding register */
Harald Weltec0bd7f02016-02-29 10:13:33 +0100210 if (csr & US_CSR_RXRDY) {
Kévin Redonb218cc32020-03-26 01:46:45 +0100211 /* read the bye from the holding register */
Kévin Redon680bdab2018-07-18 14:00:57 +0200212 byte = (usart->US_RHR) & 0xFF;
Kévin Redonb218cc32020-03-26 01:46:45 +0100213 /* append it to the buffer */
Harald Welte05cc7f62018-07-04 04:39:40 +0200214 if (rbuf_write(&ci->rb, byte) < 0)
215 TRACE_ERROR("rbuf overrun\r\n");
Harald Weltec0bd7f02016-02-29 10:13:33 +0100216 }
Harald Welte2a6d3af2016-02-28 19:29:14 +0100217
Kévin Redonb218cc32020-03-26 01:46:45 +0100218 /* check if the transmitter is ready for the next byte */
Harald Welte12d4bdf2016-03-02 10:31:03 +0100219 if (csr & US_CSR_TXRDY) {
Kévin Redonb218cc32020-03-26 01:46:45 +0100220 /* transmit next byte and check if more bytes are to be transmitted */
221 if (card_emu_tx_byte(ci->ch) == 0) {
222 /* stop the TX ready interrupt of no more bytes to transmit */
Harald Welte12d4bdf2016-03-02 10:31:03 +0100223 USART_DisableIt(usart, US_IER_TXRDY);
Kévin Redonb218cc32020-03-26 01:46:45 +0100224 }
Harald Welte12d4bdf2016-03-02 10:31:03 +0100225 }
226
Kévin Redonb218cc32020-03-26 01:46:45 +0100227 /* check if any error flags are set */
228 if (csr & (US_CSR_OVRE|US_CSR_FRAME|US_CSR_PARE|US_CSR_TIMEOUT|US_CSR_NACK|(1<<10))) {
229 /* clear any error flags */
Harald Weltec0bd7f02016-02-29 10:13:33 +0100230 usart->US_CR = US_CR_RSTSTA | US_CR_RSTIT | US_CR_RSTNACK;
Kévin Redonb218cc32020-03-26 01:46:45 +0100231 TRACE_ERROR("%u USART error on 0x%x status: 0x%lx\n", ci->num, byte, csr);
Harald Welte2a6d3af2016-02-28 19:29:14 +0100232 }
233}
234
Kévin Redonb218cc32020-03-26 01:46:45 +0100235/*! ISR called for USART0 */
Harald Welte3bafe432016-03-20 16:43:12 +0100236void mode_cardemu_usart0_irq(void)
237{
238 /* USART0 == Instance 1 == USIM 2 */
239 usart_irq_rx(1);
240}
241
Kévin Redonb218cc32020-03-26 01:46:45 +0100242/*! ISR called for USART1 */
Harald Welte3bafe432016-03-20 16:43:12 +0100243void mode_cardemu_usart1_irq(void)
244{
245 /* USART1 == Instance 0 == USIM 1 */
246 usart_irq_rx(0);
247}
248
Harald Weltebd717682016-02-28 19:30:05 +0100249/* call-back from card_emu.c to change UART baud rate */
250int card_emu_uart_update_fidi(uint8_t uart_chan, unsigned int fidi)
251{
252 int rc;
Harald Weltebd717682016-02-28 19:30:05 +0100253 Usart *usart = get_usart_by_chan(uart_chan);
254
Harald Welte99f62a62016-02-29 10:08:49 +0100255 usart->US_CR |= US_CR_RXDIS | US_CR_RSTRX;
256 usart->US_FIDI = fidi & 0x3ff;
257 usart->US_CR |= US_CR_RXEN | US_CR_STTTO;
258 return 0;
Harald Weltebd717682016-02-28 19:30:05 +0100259}
Harald Welte2a6d3af2016-02-28 19:29:14 +0100260
Kévin Redonebe672e2018-07-29 00:18:12 +0200261/* call-back from card_emu.c to force a USART interrupt */
262void card_emu_uart_interrupt(uint8_t uart_chan)
263{
264 Usart *usart = get_usart_by_chan(uart_chan);
265 if (!usart) {
266 return;
267 }
268 if (USART0 == usart) {
269 NVIC_SetPendingIRQ(USART0_IRQn);
270 } else if (USART1 == usart) {
271 NVIC_SetPendingIRQ(USART1_IRQn);
272 }
273}
274
Harald Welte2a6d3af2016-02-28 19:29:14 +0100275/***********************************************************************
Harald Welte419eb8a2016-03-20 10:04:03 +0100276 * ADC for VCC voltage detection
277 ***********************************************************************/
278
Harald Welte07872b62016-03-20 11:45:36 +0100279#ifdef DETECT_VCC_BY_ADC
280
Harald Weltefa197ca2019-12-14 22:13:29 +0100281static volatile int adc_triggered = 0;
Harald Welte28772eb2016-03-20 15:44:52 +0100282static int adc_sam3s_reva_errata = 0;
Harald Welte07872b62016-03-20 11:45:36 +0100283
Harald Welte419eb8a2016-03-20 10:04:03 +0100284static int card_vcc_adc_init(void)
285{
Harald Welte622b6be2016-03-20 16:43:59 +0100286 uint32_t chip_arch = CHIPID->CHIPID_CIDR & CHIPID_CIDR_ARCH_Msk;
287 uint32_t chip_ver = CHIPID->CHIPID_CIDR & CHIPID_CIDR_VERSION_Msk;
Harald Welte28772eb2016-03-20 15:44:52 +0100288
Harald Welte07872b62016-03-20 11:45:36 +0100289 PMC_EnablePeripheral(ID_ADC);
290
Harald Welte419eb8a2016-03-20 10:04:03 +0100291 ADC->ADC_CR |= ADC_CR_SWRST;
Harald Welte28772eb2016-03-20 15:44:52 +0100292 if (chip_ver == 0 &&
293 (chip_arch == CHIPID_CIDR_ARCH_SAM3SxA ||
294 chip_arch == CHIPID_CIDR_ARCH_SAM3SxB ||
295 chip_arch == CHIPID_CIDR_ARCH_SAM3SxC)) {
296 TRACE_INFO("Enabling Rev.A ADC Errata work-around\r\n");
297 adc_sam3s_reva_errata = 1;
298 }
299
300 if (adc_sam3s_reva_errata) {
301 /* Errata Work-Around to clear EOCx flags */
Harald Welte07872b62016-03-20 11:45:36 +0100302 volatile uint32_t foo;
303 int i;
304 for (i = 0; i < 16; i++)
305 foo = ADC->ADC_CDR[i];
306 }
307
308 /* Initialize ADC for AD7 / AD6, fADC=48/24=2MHz */
Harald Welte419eb8a2016-03-20 10:04:03 +0100309 ADC->ADC_MR = ADC_MR_TRGEN_DIS | ADC_MR_LOWRES_BITS_12 |
310 ADC_MR_SLEEP_NORMAL | ADC_MR_FWUP_OFF |
Harald Welte07872b62016-03-20 11:45:36 +0100311 ADC_MR_FREERUN_OFF | ADC_MR_PRESCAL(23) |
312 ADC_MR_STARTUP_SUT8 | ADC_MR_SETTLING(3) |
313 ADC_MR_ANACH_NONE | ADC_MR_TRACKTIM(4) |
Harald Welte419eb8a2016-03-20 10:04:03 +0100314 ADC_MR_TRANSFER(1) | ADC_MR_USEQ_NUM_ORDER;
315 /* enable AD6 + AD7 channels */
Harald Welte07872b62016-03-20 11:45:36 +0100316 ADC->ADC_CHER = ADC_CHER_CH7;
317 ADC->ADC_IER = ADC_IER_EOC7;
318#ifdef CARDEMU_SECOND_UART
319 ADC->ADC_CHER |= ADC_CHER_CH6;
320 ADC->ADC_IER |= ADC_IER_EOC6;
321#endif
322 NVIC_EnableIRQ(ADC_IRQn);
Harald Welte419eb8a2016-03-20 10:04:03 +0100323 ADC->ADC_CR |= ADC_CR_START;
Harald Welte07872b62016-03-20 11:45:36 +0100324
325 return 0;
Harald Welte419eb8a2016-03-20 10:04:03 +0100326}
327
Harald Welte07872b62016-03-20 11:45:36 +0100328#define VCC_UV_THRESH_1V8 1500000
Kévin Redonb7f94dc2019-11-26 13:49:45 +0100329#define VCC_UV_THRESH_3V 2500000
Harald Welte07872b62016-03-20 11:45:36 +0100330
331static void process_vcc_adc(struct cardem_inst *ci)
Harald Welte419eb8a2016-03-20 10:04:03 +0100332{
Harald Weltefa197ca2019-12-14 22:13:29 +0100333 if (ci->vcc_uv >= VCC_UV_THRESH_3V)
334 ci->vcc_active = true;
335 else
336 ci->vcc_active = false;
Harald Welte07872b62016-03-20 11:45:36 +0100337}
338
Harald Welte07872b62016-03-20 11:45:36 +0100339void ADC_IrqHandler(void)
340{
341#ifdef CARDEMU_SECOND_UART
342 if (ADC->ADC_ISR & ADC_ISR_EOC6) {
343 uint16_t val = ADC->ADC_CDR[6] & 0xFFF;
344 cardem_inst[1].vcc_uv = adc2uv(val);
345 process_vcc_adc(&cardem_inst[1]);
Harald Welte28772eb2016-03-20 15:44:52 +0100346 if (adc_sam3s_reva_errata) {
347 /* Errata: START doesn't start a conversion
348 * sequence, but only a single conversion */
349 ADC->ADC_CR |= ADC_CR_START;
350 }
Harald Welte07872b62016-03-20 11:45:36 +0100351 }
352#endif
Harald Welte419eb8a2016-03-20 10:04:03 +0100353
354 if (ADC->ADC_ISR & ADC_ISR_EOC7) {
Harald Welte07872b62016-03-20 11:45:36 +0100355 uint16_t val = ADC->ADC_CDR[7] & 0xFFF;
356 cardem_inst[0].vcc_uv = adc2uv(val);
357 process_vcc_adc(&cardem_inst[0]);
358 ADC->ADC_CR |= ADC_CR_START;
Harald Weltefa197ca2019-12-14 22:13:29 +0100359 adc_triggered = 1;
Harald Welte419eb8a2016-03-20 10:04:03 +0100360 }
361}
Harald Welte07872b62016-03-20 11:45:36 +0100362#endif /* DETECT_VCC_BY_ADC */
Harald Welte419eb8a2016-03-20 10:04:03 +0100363
Harald Weltefa197ca2019-12-14 22:13:29 +0100364
365/* called from main loop; dispatches card I/O state changes to card_emu from main loop */
366static void process_io_statechg(struct cardem_inst *ci)
367{
368 if (ci->vcc_active != ci->vcc_active_last) {
369 card_emu_io_statechg(ci->ch, CARD_IO_VCC, ci->vcc_active);
370 /* FIXME do this for real */
371 card_emu_io_statechg(ci->ch, CARD_IO_CLK, ci->vcc_active);
372 ci->vcc_active_last = ci->vcc_active;
373 }
374
375 if (ci->rst_active != ci->rst_active_last) {
376 card_emu_io_statechg(ci->ch, CARD_IO_RST, ci->rst_active);
377 ci->rst_active_last = ci->rst_active;
378 }
379}
380
Harald Welte419eb8a2016-03-20 10:04:03 +0100381/***********************************************************************
Kévin Redon680bdab2018-07-18 14:00:57 +0200382 * Core USB / main loop integration
Harald Welte2a6d3af2016-02-28 19:29:14 +0100383 ***********************************************************************/
384
Harald Weltec0bd7f02016-02-29 10:13:33 +0100385static void usim1_rst_irqhandler(const Pin *pPin)
386{
Harald Weltefa197ca2019-12-14 22:13:29 +0100387 cardem_inst[0].rst_active = PIO_Get(&pin_usim1_rst) ? false : true;
Harald Weltec0bd7f02016-02-29 10:13:33 +0100388}
389
Harald Welte07872b62016-03-20 11:45:36 +0100390#ifndef DETECT_VCC_BY_ADC
Harald Weltec0bd7f02016-02-29 10:13:33 +0100391static void usim1_vcc_irqhandler(const Pin *pPin)
392{
Harald Weltefa197ca2019-12-14 22:13:29 +0100393 cardem_inst[0].vcc_active = PIO_Get(&pin_usim1_vcc) ? true : false;
Harald Weltec0bd7f02016-02-29 10:13:33 +0100394}
Harald Welte07872b62016-03-20 11:45:36 +0100395#endif /* !DETECT_VCC_BY_ADC */
Harald Weltec0bd7f02016-02-29 10:13:33 +0100396
Harald Welte13e82022016-03-02 15:17:53 +0100397#ifdef CARDEMU_SECOND_UART
398static void usim2_rst_irqhandler(const Pin *pPin)
399{
Harald Weltefa197ca2019-12-14 22:13:29 +0100400 cardem_inst[1].rst_active = PIO_Get(&pin_usim2_rst) ? false : true;
Harald Welte13e82022016-03-02 15:17:53 +0100401}
402
Harald Welte07872b62016-03-20 11:45:36 +0100403#ifndef DETECT_VCC_BY_ADC
Harald Welte13e82022016-03-02 15:17:53 +0100404static void usim2_vcc_irqhandler(const Pin *pPin)
405{
Harald Weltefa197ca2019-12-14 22:13:29 +0100406 cardem_inst[1].vcc_active = PIO_Get(&pin_usim2_vcc) ? true : false;
Harald Welte13e82022016-03-02 15:17:53 +0100407}
Harald Welte07872b62016-03-20 11:45:36 +0100408#endif /* !DETECT_VCC_BY_ADC */
409#endif /* CARDEMU_SECOND_UART */
Harald Welte13e82022016-03-02 15:17:53 +0100410
Harald Welte2a6d3af2016-02-28 19:29:14 +0100411/* executed once at system boot for each config */
412void mode_cardemu_configure(void)
413{
Harald Weltebd717682016-02-28 19:30:05 +0100414 TRACE_ENTRY();
Harald Welte2a6d3af2016-02-28 19:29:14 +0100415}
416
417/* called if config is activated */
418void mode_cardemu_init(void)
419{
Harald Welte13e82022016-03-02 15:17:53 +0100420 int i;
Harald Weltebd717682016-02-28 19:30:05 +0100421
Harald Welte13e82022016-03-02 15:17:53 +0100422 TRACE_ENTRY();
Harald Weltec0bd7f02016-02-29 10:13:33 +0100423
Harald Welte6d1128e2017-05-05 20:23:10 +0200424#ifdef PINS_CARDSIM
Harald Welte2a6d3af2016-02-28 19:29:14 +0100425 PIO_Configure(pins_cardsim, PIO_LISTSIZE(pins_cardsim));
Harald Welte6d1128e2017-05-05 20:23:10 +0200426#endif
Harald Welte07872b62016-03-20 11:45:36 +0100427#ifdef DETECT_VCC_BY_ADC
428 card_vcc_adc_init();
429#endif /* DETECT_VCC_BY_ADC */
Harald Welte2a6d3af2016-02-28 19:29:14 +0100430
Harald Welte13e82022016-03-02 15:17:53 +0100431 INIT_LLIST_HEAD(&cardem_inst[0].usb_out_queue);
432 rbuf_reset(&cardem_inst[0].rb);
Harald Welte2a6d3af2016-02-28 19:29:14 +0100433 PIO_Configure(pins_usim1, PIO_LISTSIZE(pins_usim1));
Kévin Redonb218cc32020-03-26 01:46:45 +0100434
435 /* configure USART as ISO-7816 slave (e.g. card) */
Harald Welte13e82022016-03-02 15:17:53 +0100436 ISO7816_Init(&cardem_inst[0].usart_info, CLK_SLAVE);
Harald Welte2a6d3af2016-02-28 19:29:14 +0100437 NVIC_EnableIRQ(USART1_IRQn);
Harald Weltec0bd7f02016-02-29 10:13:33 +0100438 PIO_ConfigureIt(&pin_usim1_rst, usim1_rst_irqhandler);
439 PIO_EnableIt(&pin_usim1_rst);
Kévin Redonb218cc32020-03-26 01:46:45 +0100440
441 /* obtain current RST state */
442 usim1_rst_irqhandler(&pin_usim1_rst);
Harald Welte07872b62016-03-20 11:45:36 +0100443#ifndef DETECT_VCC_BY_ADC
Harald Weltec0bd7f02016-02-29 10:13:33 +0100444 PIO_ConfigureIt(&pin_usim1_vcc, usim1_vcc_irqhandler);
445 PIO_EnableIt(&pin_usim1_vcc);
Kévin Redonb218cc32020-03-26 01:46:45 +0100446
447 /* obtain current VCC state */
448 usim1_vcc_irqhandler(&pin_usim1_vcc);
Harald Weltefa197ca2019-12-14 22:13:29 +0100449#else
450 do {} while (!adc_triggered); /* wait for first ADC reading */
Harald Welte07872b62016-03-20 11:45:36 +0100451#endif /* DETECT_VCC_BY_ADC */
Harald Weltefa197ca2019-12-14 22:13:29 +0100452
453 cardem_inst[0].ch = card_emu_init(0, 2, 0, SIMTRACE_CARDEM_USB_EP_USIM1_DATAIN,
454 SIMTRACE_CARDEM_USB_EP_USIM1_INT, cardem_inst[0].vcc_active,
455 cardem_inst[0].rst_active, cardem_inst[0].vcc_active);
Harald Weltec47fc5f2017-05-11 16:51:57 +0200456 sim_switch_use_physical(0, 1);
Harald Welte2a6d3af2016-02-28 19:29:14 +0100457
458#ifdef CARDEMU_SECOND_UART
Harald Welte13e82022016-03-02 15:17:53 +0100459 INIT_LLIST_HEAD(&cardem_inst[1].usb_out_queue);
460 rbuf_reset(&cardem_inst[1].rb);
Harald Welte2a6d3af2016-02-28 19:29:14 +0100461 PIO_Configure(pins_usim2, PIO_LISTSIZE(pins_usim2));
Harald Welte13e82022016-03-02 15:17:53 +0100462 ISO7816_Init(&cardem_inst[1].usart_info, CLK_SLAVE);
Kévin Redonb218cc32020-03-26 01:46:45 +0100463 /* TODO enable timeout */
Harald Welte2a6d3af2016-02-28 19:29:14 +0100464 NVIC_EnableIRQ(USART0_IRQn);
Harald Welte13e82022016-03-02 15:17:53 +0100465 PIO_ConfigureIt(&pin_usim2_rst, usim2_rst_irqhandler);
466 PIO_EnableIt(&pin_usim2_rst);
Harald Weltefa197ca2019-12-14 22:13:29 +0100467 usim2_rst_irqhandler(&pin_usim2_rst); /* obtain current RST state */
Harald Welte07872b62016-03-20 11:45:36 +0100468#ifndef DETECT_VCC_BY_ADC
Harald Welte13e82022016-03-02 15:17:53 +0100469 PIO_ConfigureIt(&pin_usim2_vcc, usim2_vcc_irqhandler);
470 PIO_EnableIt(&pin_usim2_vcc);
Harald Weltefa197ca2019-12-14 22:13:29 +0100471 usim2_vcc_irqhandler(&pin_usim2_vcc); /* obtain current VCC state */
472#else
473 do {} while (!adc_triggered); /* wait for first ADC reading */
Harald Welte07872b62016-03-20 11:45:36 +0100474#endif /* DETECT_VCC_BY_ADC */
Harald Weltefa197ca2019-12-14 22:13:29 +0100475
476 cardem_inst[1].ch = card_emu_init(1, 0, 1, SIMTRACE_CARDEM_USB_EP_USIM2_DATAIN,
477 SIMTRACE_CARDEM_USB_EP_USIM2_INT, cardem_inst[1].vcc_active,
478 cardem_inst[1].rst_active, cardem_inst[1].vcc_active);
Harald Weltec47fc5f2017-05-11 16:51:57 +0200479 sim_switch_use_physical(1, 1);
Kévin Redonb218cc32020-03-26 01:46:45 +0100480 /* TODO check RST and VCC */
Harald Welte07872b62016-03-20 11:45:36 +0100481#endif /* CARDEMU_SECOND_UART */
Harald Welte2a6d3af2016-02-28 19:29:14 +0100482}
483
484/* called if config is deactivated */
485void mode_cardemu_exit(void)
486{
Harald Weltebd717682016-02-28 19:30:05 +0100487 TRACE_ENTRY();
488
Harald Welte7abdb512016-03-03 17:48:32 +0100489 /* FIXME: stop tc_fdt */
Harald Welte8e7fca32017-05-07 16:14:33 +0200490 /* FIXME: release all msg, unlink them from any queue */
Harald Welte7abdb512016-03-03 17:48:32 +0100491
Harald Weltec0bd7f02016-02-29 10:13:33 +0100492 PIO_DisableIt(&pin_usim1_rst);
493 PIO_DisableIt(&pin_usim1_vcc);
494
Harald Welte2a6d3af2016-02-28 19:29:14 +0100495 NVIC_DisableIRQ(USART1_IRQn);
496 USART_SetTransmitterEnabled(USART1, 0);
497 USART_SetReceiverEnabled(USART1, 0);
498
499#ifdef CARDEMU_SECOND_UART
Harald Welte13e82022016-03-02 15:17:53 +0100500 PIO_DisableIt(&pin_usim2_rst);
501 PIO_DisableIt(&pin_usim2_vcc);
502
Harald Welte2a6d3af2016-02-28 19:29:14 +0100503 NVIC_DisableIRQ(USART0_IRQn);
504 USART_SetTransmitterEnabled(USART0, 0);
505 USART_SetReceiverEnabled(USART0, 0);
506#endif
507}
508
Harald Welteebb80ed2016-03-02 13:56:59 +0100509/* handle a single USB command as received from the USB host */
Harald Welte25a9a802017-05-08 13:30:09 +0200510static void dispatch_usb_command_generic(struct msgb *msg, struct cardem_inst *ci)
Harald Welteebb80ed2016-03-02 13:56:59 +0100511{
Harald Welte25a9a802017-05-08 13:30:09 +0200512 struct simtrace_msg_hdr *hdr;
513
514 hdr = (struct simtrace_msg_hdr *) msg->l1h;
515 switch (hdr->msg_type) {
516 case SIMTRACE_CMD_BD_BOARD_INFO:
517 break;
518 default:
519 break;
520 }
521 usb_buf_free(msg);
522}
523
524/* handle a single USB command as received from the USB host */
525static void dispatch_usb_command_cardem(struct msgb *msg, struct cardem_inst *ci)
526{
527 struct simtrace_msg_hdr *hdr;
Harald Welte06b27f62016-03-02 14:26:38 +0100528 struct cardemu_usb_msg_set_atr *atr;
Harald Welte0eaa9922016-03-04 03:03:49 +0100529 struct cardemu_usb_msg_cardinsert *cardins;
Harald Weltea5bbe782019-12-16 10:39:55 +0100530 struct cardemu_usb_msg_config *cfg;
Harald Welteebb80ed2016-03-02 13:56:59 +0100531 struct llist_head *queue;
532
Harald Welte25a9a802017-05-08 13:30:09 +0200533 hdr = (struct simtrace_msg_hdr *) msg->l1h;
Harald Welteebb80ed2016-03-02 13:56:59 +0100534 switch (hdr->msg_type) {
Harald Welte25a9a802017-05-08 13:30:09 +0200535 case SIMTRACE_MSGT_DT_CEMU_TX_DATA:
Harald Welte0eaa9922016-03-04 03:03:49 +0100536 queue = card_emu_get_uart_tx_queue(ci->ch);
Harald Welte8e7fca32017-05-07 16:14:33 +0200537 llist_add_tail(&msg->list, queue);
Harald Welte0eaa9922016-03-04 03:03:49 +0100538 card_emu_have_new_uart_tx(ci->ch);
Harald Welteebb80ed2016-03-02 13:56:59 +0100539 break;
Harald Welte25a9a802017-05-08 13:30:09 +0200540 case SIMTRACE_MSGT_DT_CEMU_SET_ATR:
541 atr = (struct cardemu_usb_msg_set_atr *) msg->l2h;
Harald Welted295b922016-03-18 21:01:36 +0100542 card_emu_set_atr(ci->ch, atr->atr, atr->atr_len);
Harald Welte8e7fca32017-05-07 16:14:33 +0200543 usb_buf_free(msg);
Harald Welte06b27f62016-03-02 14:26:38 +0100544 break;
Harald Welte25a9a802017-05-08 13:30:09 +0200545 case SIMTRACE_MSGT_DT_CEMU_CARDINSERT:
546 cardins = (struct cardemu_usb_msg_cardinsert *) msg->l2h;
Harald Welte965d5c92017-11-03 20:47:12 +0100547 if (!ci->pin_insert.pio) {
548 TRACE_INFO("%u: skipping unsupported card_insert to %s\r\n",
549 ci->num, cardins->card_insert ? "INSERTED" : "REMOVED");
Harald Welte56be0bf2019-12-14 23:23:36 +0100550 usb_buf_free(msg);
Harald Welte965d5c92017-11-03 20:47:12 +0100551 break;
552 }
Harald Weltedde112e2016-03-20 16:42:11 +0100553 TRACE_INFO("%u: set card_insert to %s\r\n", ci->num,
554 cardins->card_insert ? "INSERTED" : "REMOVED");
Harald Welte0eaa9922016-03-04 03:03:49 +0100555 if (cardins->card_insert)
556 PIO_Set(&ci->pin_insert);
557 else
558 PIO_Clear(&ci->pin_insert);
Harald Welte8e7fca32017-05-07 16:14:33 +0200559 usb_buf_free(msg);
Harald Welte0eaa9922016-03-04 03:03:49 +0100560 break;
Harald Welte25a9a802017-05-08 13:30:09 +0200561 case SIMTRACE_MSGT_BD_CEMU_STATUS:
Harald Welte140f0072019-12-16 10:23:32 +0100562 card_emu_report_status(ci->ch, false);
Harald Welte25a9a802017-05-08 13:30:09 +0200563 usb_buf_free(msg);
Harald Welteff160652016-03-19 21:59:06 +0100564 break;
Harald Weltea5bbe782019-12-16 10:39:55 +0100565 case SIMTRACE_MSGT_BD_CEMU_CONFIG:
566 cfg = (struct cardemu_usb_msg_config *) msg->l2h;
567 card_emu_set_config(ci->ch, cfg, msgb_l2len(msg));
568 break;
Harald Welte25a9a802017-05-08 13:30:09 +0200569 case SIMTRACE_MSGT_BD_CEMU_STATS:
Harald Welteebb80ed2016-03-02 13:56:59 +0100570 default:
Harald Welte25a9a802017-05-08 13:30:09 +0200571 /* FIXME: Send Error */
572 usb_buf_free(msg);
573 break;
574 }
575}
576
Harald Weltefc87c242017-11-28 19:17:27 +0100577#ifdef PINS_PERST
578#include "wwan_perst.h"
579#endif
580
Harald Welte2e9254a2017-05-09 06:30:04 +0200581static int usb_command_modem_reset(struct msgb *msg, struct cardem_inst *ci)
582{
Harald Welteb91f6ad2017-05-10 22:51:30 +0200583 struct st_modem_reset *mr = (struct st_modem_reset *) msg->l2h;
Harald Welte2e9254a2017-05-09 06:30:04 +0200584
585 if (msgb_l2len(msg) < sizeof(*mr))
586 return -1;
587
588 switch (mr->asserted) {
Harald Weltefc87c242017-11-28 19:17:27 +0100589#ifdef PINS_PERST
Harald Welte2e9254a2017-05-09 06:30:04 +0200590 case 0:
591 wwan_perst_set(ci->num, 0);
592 break;
593 case 1:
594 wwan_perst_set(ci->num, 1);
595 break;
596 case 2:
597 wwan_perst_do_reset_pulse(ci->num, mr->pulse_duration_msec);
598 break;
Harald Weltefc87c242017-11-28 19:17:27 +0100599#endif
Harald Welte2e9254a2017-05-09 06:30:04 +0200600 default:
601 return -1;
602 }
603
604 return 0;
605}
606
Harald Welte5c583d32017-05-09 06:46:47 +0200607static int usb_command_sim_select(struct msgb *msg, struct cardem_inst *ci)
608{
Harald Welteb91f6ad2017-05-10 22:51:30 +0200609 struct st_modem_sim_select *mss = (struct st_modem_sim_select *) msg->l2h;
Harald Welte5c583d32017-05-09 06:46:47 +0200610
611 if (msgb_l2len(msg) < sizeof(*mss))
612 return -1;
613
614 if (mss->remote_sim)
615 sim_switch_use_physical(ci->num, 0);
616 else
617 sim_switch_use_physical(ci->num, 1);
618
619 return 0;
620}
621
Harald Welte25a9a802017-05-08 13:30:09 +0200622/* handle a single USB command as received from the USB host */
623static void dispatch_usb_command_modem(struct msgb *msg, struct cardem_inst *ci)
624{
625 struct simtrace_msg_hdr *hdr;
626
627 hdr = (struct simtrace_msg_hdr *) msg->l1h;
628 switch (hdr->msg_type) {
629 case SIMTRACE_MSGT_DT_MODEM_RESET:
Harald Welte2e9254a2017-05-09 06:30:04 +0200630 usb_command_modem_reset(msg, ci);
Harald Welte25a9a802017-05-08 13:30:09 +0200631 break;
632 case SIMTRACE_MSGT_DT_MODEM_SIM_SELECT:
Harald Welte5c583d32017-05-09 06:46:47 +0200633 usb_command_sim_select(msg, ci);
Harald Welte25a9a802017-05-08 13:30:09 +0200634 break;
635 case SIMTRACE_MSGT_BD_MODEM_STATUS:
636 break;
637 default:
638 break;
639 }
640 usb_buf_free(msg);
641}
642
643/* handle a single USB command as received from the USB host */
644static void dispatch_usb_command(struct msgb *msg, struct cardem_inst *ci)
645{
Harald Welteb91f6ad2017-05-10 22:51:30 +0200646 struct simtrace_msg_hdr *sh = (struct simtrace_msg_hdr *) msg->l1h;
Harald Welte25a9a802017-05-08 13:30:09 +0200647
648 if (msgb_length(msg) < sizeof(*sh)) {
649 /* FIXME: Error */
650 usb_buf_free(msg);
651 return;
652 }
Kévin Redonb6053882018-07-18 14:06:13 +0200653 msg->l2h = msg->l1h + sizeof(*sh);
Harald Welte25a9a802017-05-08 13:30:09 +0200654
655 switch (sh->msg_class) {
656 case SIMTRACE_MSGC_GENERIC:
657 dispatch_usb_command_generic(msg, ci);
658 break;
659 case SIMTRACE_MSGC_CARDEM:
660 dispatch_usb_command_cardem(msg, ci);
661 break;
662 case SIMTRACE_MSGC_MODEM:
Harald Welte23c00b62017-05-11 01:11:43 +0200663 /* FIXME: Find out why this fails if used for !=
664 * MSGC_MODEM ?!? */
Harald Welte25a9a802017-05-08 13:30:09 +0200665 dispatch_usb_command_modem(msg, ci);
666 break;
667 default:
668 /* FIXME: Send Error */
Harald Welte8e7fca32017-05-07 16:14:33 +0200669 usb_buf_free(msg);
Harald Welteebb80ed2016-03-02 13:56:59 +0100670 break;
671 }
672}
673
Harald Welte8e7fca32017-05-07 16:14:33 +0200674static void dispatch_received_msg(struct msgb *msg, struct cardem_inst *ci)
Harald Welteb26d0032016-03-19 13:33:02 +0100675{
Harald Welte8e7fca32017-05-07 16:14:33 +0200676 struct msgb *segm;
Harald Welte25a9a802017-05-08 13:30:09 +0200677 struct simtrace_msg_hdr *mh;
Harald Welteb26d0032016-03-19 13:33:02 +0100678
679 /* check if we have multiple concatenated commands in
680 * one message. USB endpoints are streams that don't
681 * preserve the message boundaries */
Harald Welte25a9a802017-05-08 13:30:09 +0200682 mh = (struct simtrace_msg_hdr *) msg->data;
Harald Welte8e7fca32017-05-07 16:14:33 +0200683 if (mh->msg_len == msgb_length(msg)) {
Harald Welteb26d0032016-03-19 13:33:02 +0100684 /* fast path: only one message in buffer */
Harald Welte8e7fca32017-05-07 16:14:33 +0200685 dispatch_usb_command(msg, ci);
Harald Welteb26d0032016-03-19 13:33:02 +0100686 return;
687 }
688
689 /* slow path: iterate over list of messages, allocating one new
690 * reqe_ctx per segment */
Harald Welte8e7fca32017-05-07 16:14:33 +0200691 while (1) {
Harald Welte25a9a802017-05-08 13:30:09 +0200692 mh = (struct simtrace_msg_hdr *) msg->data;
Harald Welte8e7fca32017-05-07 16:14:33 +0200693
694 segm = usb_buf_alloc(ci->ep_out);
Harald Welteb26d0032016-03-19 13:33:02 +0100695 if (!segm) {
Harald Welte8e7fca32017-05-07 16:14:33 +0200696 TRACE_ERROR("%u: ENOMEM during msg segmentation\r\n",
Harald Weltedde112e2016-03-20 16:42:11 +0100697 ci->num);
Harald Welteb26d0032016-03-19 13:33:02 +0100698 break;
699 }
Harald Welte8e7fca32017-05-07 16:14:33 +0200700
701 if (mh->msg_len > msgb_length(msg)) {
Harald Welte823f4532020-03-01 15:58:12 +0100702 TRACE_ERROR("%u: Unexpected large message (%u bytes)\r\n",
Harald Welte8e7fca32017-05-07 16:14:33 +0200703 ci->num, mh->msg_len);
704 usb_buf_free(segm);
Harald Weltee6806e52020-03-01 15:56:07 +0100705 break;
Harald Welte8e7fca32017-05-07 16:14:33 +0200706 } else {
707 uint8_t *cur = msgb_put(segm, mh->msg_len);
708 segm->l1h = segm->head;
709 memcpy(cur, mh, mh->msg_len);
710 dispatch_usb_command(segm, ci);
711 }
712 /* pull this message */
713 msgb_pull(msg, mh->msg_len);
714 /* abort if we're done */
715 if (msgb_length(msg) <= 0)
716 break;
Harald Welteb26d0032016-03-19 13:33:02 +0100717 }
718
Harald Welte8e7fca32017-05-07 16:14:33 +0200719 usb_buf_free(msg);
Harald Welteb26d0032016-03-19 13:33:02 +0100720}
721
Harald Welteebb80ed2016-03-02 13:56:59 +0100722/* iterate over the queue of incoming USB commands and dispatch/execute
723 * them */
Harald Welte9f240b62016-03-18 10:32:56 +0100724static void process_any_usb_commands(struct llist_head *main_q,
725 struct cardem_inst *ci)
Harald Welteebb80ed2016-03-02 13:56:59 +0100726{
Harald Welte9f240b62016-03-18 10:32:56 +0100727 struct llist_head *lh;
Harald Welte8e7fca32017-05-07 16:14:33 +0200728 struct msgb *msg;
Harald Welte9f240b62016-03-18 10:32:56 +0100729 int i;
Harald Welteebb80ed2016-03-02 13:56:59 +0100730
Harald Welte9f240b62016-03-18 10:32:56 +0100731 /* limit the number of iterations to 10, to ensure we don't get
732 * stuck here without returning to main loop processing */
733 for (i = 0; i < 10; i++) {
734 /* de-queue the list head in an irq-safe way */
735 lh = llist_head_dequeue_irqsafe(main_q);
736 if (!lh)
737 break;
Harald Welte8e7fca32017-05-07 16:14:33 +0200738 msg = llist_entry(lh, struct msgb, list);
739 dispatch_received_msg(msg, ci);
Harald Welte5820ea92016-03-16 22:12:00 +0100740 }
Harald Welteebb80ed2016-03-02 13:56:59 +0100741}
742
Harald Welte2a6d3af2016-02-28 19:29:14 +0100743/* main loop function, called repeatedly */
744void mode_cardemu_run(void)
745{
Harald Welteacae4122016-03-02 10:27:58 +0100746 struct llist_head *queue;
Harald Welte13e82022016-03-02 15:17:53 +0100747 unsigned int i;
Harald Welteacae4122016-03-02 10:27:58 +0100748
Harald Welte13e82022016-03-02 15:17:53 +0100749 for (i = 0; i < ARRAY_SIZE(cardem_inst); i++) {
750 struct cardem_inst *ci = &cardem_inst[i];
751
Harald Welte54cb3d02016-02-29 14:12:40 +0100752 /* drain the ring buffer from UART into card_emu */
Harald Weltec0bd7f02016-02-29 10:13:33 +0100753 while (1) {
754 __disable_irq();
Harald Welte13e82022016-03-02 15:17:53 +0100755 if (rbuf_is_empty(&ci->rb)) {
Harald Weltec0bd7f02016-02-29 10:13:33 +0100756 __enable_irq();
757 break;
758 }
Harald Welte13e82022016-03-02 15:17:53 +0100759 uint8_t byte = rbuf_read(&ci->rb);
Harald Weltec0bd7f02016-02-29 10:13:33 +0100760 __enable_irq();
Harald Welte13e82022016-03-02 15:17:53 +0100761 card_emu_process_rx_byte(ci->ch, byte);
Harald Weltedde112e2016-03-20 16:42:11 +0100762 //TRACE_ERROR("%uRx%02x\r\n", i, byte);
Harald Weltec0bd7f02016-02-29 10:13:33 +0100763 }
Harald Welteacae4122016-03-02 10:27:58 +0100764
Harald Weltefa197ca2019-12-14 22:13:29 +0100765 process_io_statechg(ci);
766
Harald Welte8e7fca32017-05-07 16:14:33 +0200767 /* first try to send any pending messages on IRQ */
768 usb_refill_to_host(ci->ep_int);
769
770 /* then try to send any pending messages on IN */
771 usb_refill_to_host(ci->ep_in);
Harald Welteacae4122016-03-02 10:27:58 +0100772
Harald Welteebb80ed2016-03-02 13:56:59 +0100773 /* ensure we can handle incoming USB messages from the
774 * host */
Harald Welte8e7fca32017-05-07 16:14:33 +0200775 usb_refill_from_host(ci->ep_out);
776 queue = usb_get_queue(ci->ep_out);
Harald Welte0eaa9922016-03-04 03:03:49 +0100777 process_any_usb_commands(queue, ci);
Harald Welte2a6d3af2016-02-28 19:29:14 +0100778 }
Harald Welte2a6d3af2016-02-28 19:29:14 +0100779}