blob: 2d9af99439c80df3fcbd20587e9507a691bf41c1 [file] [log] [blame]
Harald Welte53079bb2016-03-20 14:58:35 +01001//#define TRACE_LEVEL 6
Harald Welte7abdb512016-03-03 17:48:32 +01002
Harald Welte2a6d3af2016-02-28 19:29:14 +01003#include "board.h"
Harald Welte16055642016-03-03 11:02:45 +01004#include "simtrace.h"
5#include "ringbuffer.h"
Harald Welte2a6d3af2016-02-28 19:29:14 +01006#include "card_emu.h"
Harald Weltebd717682016-02-28 19:30:05 +01007#include "iso7816_fidi.h"
Harald Welte54cb3d02016-02-29 14:12:40 +01008#include "utils.h"
Harald Welte9d90d282018-06-29 22:25:42 +02009#include <osmocom/core/linuxlist.h>
10#include <osmocom/core/msgb.h>
Harald Welte9f240b62016-03-18 10:32:56 +010011#include "llist_irqsafe.h"
Harald Welte8e7fca32017-05-07 16:14:33 +020012#include "usb_buf.h"
Harald Welte25a9a802017-05-08 13:30:09 +020013#include "simtrace_prot.h"
Harald Welte5c583d32017-05-09 06:46:47 +020014#include "sim_switch.h"
Harald Weltebd717682016-02-28 19:30:05 +010015
Harald Weltefd9c0412016-03-20 18:15:57 +010016#define TRACE_ENTRY() TRACE_DEBUG("%s entering\r\n", __func__)
Harald Welte2a6d3af2016-02-28 19:29:14 +010017
Harald Welte6d1128e2017-05-05 20:23:10 +020018#ifdef PINS_CARDSIM
Harald Welte2a6d3af2016-02-28 19:29:14 +010019static const Pin pins_cardsim[] = PINS_CARDSIM;
Harald Welte6d1128e2017-05-05 20:23:10 +020020#endif
Harald Welte2a6d3af2016-02-28 19:29:14 +010021
22/* UART pins */
23static const Pin pins_usim1[] = {PINS_USIM1};
24static const Pin pin_usim1_rst = PIN_USIM1_nRST;
25static const Pin pin_usim1_vcc = PIN_USIM1_VCC;
26
27#ifdef CARDEMU_SECOND_UART
Harald Welte6dcacf32016-03-19 14:01:31 +010028static const Pin pins_usim2[] = {PINS_USIM2};
Harald Welte2a6d3af2016-02-28 19:29:14 +010029static const Pin pin_usim2_rst = PIN_USIM2_nRST;
30static const Pin pin_usim2_vcc = PIN_USIM2_VCC;
31#endif
32
Harald Welte13e82022016-03-02 15:17:53 +010033struct cardem_inst {
Harald Weltedde112e2016-03-20 16:42:11 +010034 uint32_t num;
Harald Welte13e82022016-03-02 15:17:53 +010035 struct card_handle *ch;
36 struct llist_head usb_out_queue;
37 struct ringbuf rb;
38 struct Usart_info usart_info;
39 int usb_pending_old;
40 uint8_t ep_out;
41 uint8_t ep_in;
42 uint8_t ep_int;
Harald Welte0eaa9922016-03-04 03:03:49 +010043 const Pin pin_insert;
Harald Welte07872b62016-03-20 11:45:36 +010044 uint32_t vcc_uv;
45 uint32_t vcc_uv_last;
Harald Welte13e82022016-03-02 15:17:53 +010046};
Harald Welte2a6d3af2016-02-28 19:29:14 +010047
Harald Welte8e7fca32017-05-07 16:14:33 +020048struct cardem_inst cardem_inst[] = {
Harald Welte2a6d3af2016-02-28 19:29:14 +010049 {
Harald Weltedde112e2016-03-20 16:42:11 +010050 .num = 0,
Harald Welte13e82022016-03-02 15:17:53 +010051 .usart_info = {
52 .base = USART1,
53 .id = ID_USART1,
54 .state = USART_RCV
55 },
56 .ep_out = PHONE_DATAOUT,
57 .ep_in = PHONE_DATAIN,
58 .ep_int = PHONE_INT,
Harald Welte6d1128e2017-05-05 20:23:10 +020059#ifdef PIN_SET_USIM1_PRES
Harald Welte0eaa9922016-03-04 03:03:49 +010060 .pin_insert = PIN_SET_USIM1_PRES,
Harald Welte6d1128e2017-05-05 20:23:10 +020061#endif
Harald Welte2a6d3af2016-02-28 19:29:14 +010062 },
63#ifdef CARDEMU_SECOND_UART
64 {
Harald Weltedde112e2016-03-20 16:42:11 +010065 .num = 1,
Harald Welte13e82022016-03-02 15:17:53 +010066 .usart_info = {
67 .base = USART0,
68 .id = ID_USART0,
69 .state = USART_RCV
70 },
71 .ep_out = CARDEM_USIM2_DATAOUT,
72 .ep_in = CARDEM_USIM2_DATAIN,
73 .ep_int = CARDEM_USIM2_INT,
Harald Welte6d1128e2017-05-05 20:23:10 +020074#ifdef PIN_SET_USIM2_PRES
Harald Welte0eaa9922016-03-04 03:03:49 +010075 .pin_insert = PIN_SET_USIM2_PRES,
Harald Welte6d1128e2017-05-05 20:23:10 +020076#endif
Harald Welte2a6d3af2016-02-28 19:29:14 +010077 },
78#endif
79};
80
81static Usart *get_usart_by_chan(uint8_t uart_chan)
82{
83 switch (uart_chan) {
84 case 0:
85 return USART1;
86#ifdef CARDEMU_SECOND_UART
87 case 1:
88 return USART0;
89#endif
90 }
91 return NULL;
92}
93
94/***********************************************************************
95 * Call-Backs from card_emu.c
96 ***********************************************************************/
97
Harald Weltec8beefb2016-03-20 14:40:47 +010098static void wait_tx_idle(Usart *usart)
99{
100 int i = 1;
101
102 /* wait until last char has been fully transmitted */
103 while ((usart->US_CSR & (US_CSR_TXEMPTY)) == 0) {
104 if (!(i%1000000)) {
105 TRACE_ERROR("s: %x \r\n", usart->US_CSR);
106 }
107 i++;
108 }
109}
110
Harald Weltec58bba02016-03-20 14:57:53 +0100111void card_emu_uart_wait_tx_idle(uint8_t uart_chan)
112{
113 Usart *usart = get_usart_by_chan(uart_chan);
114 wait_tx_idle(usart);
115}
116
Harald Welte2a6d3af2016-02-28 19:29:14 +0100117/* call-back from card_emu.c to enable/disable transmit and/or receive */
118void card_emu_uart_enable(uint8_t uart_chan, uint8_t rxtx)
119{
120 Usart *usart = get_usart_by_chan(uart_chan);
121 switch (rxtx) {
122 case ENABLE_TX:
Harald Welte9dbc46e2016-02-29 10:05:10 +0100123 USART_DisableIt(usart, ~US_IER_TXRDY);
Harald Weltec8beefb2016-03-20 14:40:47 +0100124 /* as irritating as it is, we actually want to keep the
125 * receiver enabled during transmit */
126 USART_SetReceiverEnabled(usart, 1);
Harald Welte8a416b12016-03-01 00:42:04 +0100127 usart->US_CR = US_CR_RSTSTA | US_CR_RSTIT | US_CR_RSTNACK;
128 USART_EnableIt(usart, US_IER_TXRDY);
Harald Welte2a6d3af2016-02-28 19:29:14 +0100129 USART_SetTransmitterEnabled(usart, 1);
130 break;
131 case ENABLE_RX:
Harald Welte9dbc46e2016-02-29 10:05:10 +0100132 USART_DisableIt(usart, ~US_IER_RXRDY);
Harald Weltec8beefb2016-03-20 14:40:47 +0100133 /* as irritating as it is, we actually want to keep the
134 * transmitter enabled during receive */
135 USART_SetTransmitterEnabled(usart, 1);
136 wait_tx_idle(usart);
Harald Welte8a416b12016-03-01 00:42:04 +0100137 usart->US_CR = US_CR_RSTSTA | US_CR_RSTIT | US_CR_RSTNACK;
138 USART_EnableIt(usart, US_IER_RXRDY);
139 USART_SetReceiverEnabled(usart, 1);
Harald Welte2a6d3af2016-02-28 19:29:14 +0100140 break;
141 case 0:
142 default:
143 USART_SetTransmitterEnabled(usart, 0);
144 USART_SetReceiverEnabled(usart, 0);
Harald Welte9dbc46e2016-02-29 10:05:10 +0100145 USART_DisableIt(usart, 0xFFFFFFFF);
Harald Welte8a416b12016-03-01 00:42:04 +0100146 usart->US_CR = US_CR_RSTSTA | US_CR_RSTIT | US_CR_RSTNACK;
Harald Welte2a6d3af2016-02-28 19:29:14 +0100147 break;
148 }
149}
150
151/* call-back from card_emu.c to transmit a byte */
152int card_emu_uart_tx(uint8_t uart_chan, uint8_t byte)
153{
Harald Welte13e82022016-03-02 15:17:53 +0100154 Usart *usart = get_usart_by_chan(uart_chan);
155#if 0
Harald Welte2a6d3af2016-02-28 19:29:14 +0100156 Usart_info *ui = &usart_info[uart_chan];
157 ISO7816_SendChar(byte, ui);
Harald Welte13e82022016-03-02 15:17:53 +0100158#else
Harald Welte53079bb2016-03-20 14:58:35 +0100159 int i = 1;
Harald Welte13e82022016-03-02 15:17:53 +0100160 while ((usart->US_CSR & (US_CSR_TXRDY)) == 0) {
161 if (!(i%1000000)) {
Harald Weltedde112e2016-03-20 16:42:11 +0100162 TRACE_ERROR("%u: s: %x %02X\r\n",
163 uart_chan, usart->US_CSR,
164 usart->US_RHR & 0xFF);
Harald Welte13e82022016-03-02 15:17:53 +0100165 usart->US_CR = US_CR_RSTTX;
166 usart->US_CR = US_CR_RSTRX;
167 }
Harald Welte53079bb2016-03-20 14:58:35 +0100168 i++;
Harald Welte13e82022016-03-02 15:17:53 +0100169 }
170 usart->US_THR = byte;
Harald Welte53079bb2016-03-20 14:58:35 +0100171 //TRACE_ERROR("Sx%02x\r\n", byte);
Harald Welte13e82022016-03-02 15:17:53 +0100172#endif
Harald Welte2a6d3af2016-02-28 19:29:14 +0100173 return 1;
174}
175
176
177/* FIXME: integrate this with actual irq handler */
Harald Welte3bafe432016-03-20 16:43:12 +0100178static void usart_irq_rx(uint8_t inst_num)
Harald Welte2a6d3af2016-02-28 19:29:14 +0100179{
Harald Welte3bafe432016-03-20 16:43:12 +0100180 Usart *usart = get_usart_by_chan(inst_num);
181 struct cardem_inst *ci = &cardem_inst[inst_num];
Harald Weltec0bd7f02016-02-29 10:13:33 +0100182 uint32_t csr;
183 uint8_t byte = 0;
184
Harald Welteda15ca02016-03-17 21:14:04 +0100185 csr = usart->US_CSR & usart->US_IMR;
Harald Welte2a6d3af2016-02-28 19:29:14 +0100186
Harald Weltec0bd7f02016-02-29 10:13:33 +0100187 if (csr & US_CSR_RXRDY) {
188 byte = (usart->US_RHR) & 0xFF;
Harald Welte05cc7f62018-07-04 04:39:40 +0200189 if (rbuf_write(&ci->rb, byte) < 0)
190 TRACE_ERROR("rbuf overrun\r\n");
Harald Weltec0bd7f02016-02-29 10:13:33 +0100191 }
Harald Welte2a6d3af2016-02-28 19:29:14 +0100192
Harald Welte12d4bdf2016-03-02 10:31:03 +0100193 if (csr & US_CSR_TXRDY) {
Harald Welte13e82022016-03-02 15:17:53 +0100194 if (card_emu_tx_byte(ci->ch) == 0)
Harald Welte12d4bdf2016-03-02 10:31:03 +0100195 USART_DisableIt(usart, US_IER_TXRDY);
196 }
197
Harald Welte2a6d3af2016-02-28 19:29:14 +0100198 if (csr & (US_CSR_OVRE|US_CSR_FRAME|US_CSR_PARE|
199 US_CSR_TIMEOUT|US_CSR_NACK|(1<<10))) {
Harald Weltec0bd7f02016-02-29 10:13:33 +0100200 usart->US_CR = US_CR_RSTSTA | US_CR_RSTIT | US_CR_RSTNACK;
Harald Weltedde112e2016-03-20 16:42:11 +0100201 TRACE_ERROR("%u e 0x%x st: 0x%x\n", ci->num, byte, csr);
Harald Welte2a6d3af2016-02-28 19:29:14 +0100202 }
203}
204
Harald Welte3bafe432016-03-20 16:43:12 +0100205void mode_cardemu_usart0_irq(void)
206{
207 /* USART0 == Instance 1 == USIM 2 */
208 usart_irq_rx(1);
209}
210
211void mode_cardemu_usart1_irq(void)
212{
213 /* USART1 == Instance 0 == USIM 1 */
214 usart_irq_rx(0);
215}
216
Harald Weltebd717682016-02-28 19:30:05 +0100217/* call-back from card_emu.c to change UART baud rate */
218int card_emu_uart_update_fidi(uint8_t uart_chan, unsigned int fidi)
219{
220 int rc;
Harald Weltebd717682016-02-28 19:30:05 +0100221 Usart *usart = get_usart_by_chan(uart_chan);
222
Harald Welte99f62a62016-02-29 10:08:49 +0100223 usart->US_CR |= US_CR_RXDIS | US_CR_RSTRX;
224 usart->US_FIDI = fidi & 0x3ff;
225 usart->US_CR |= US_CR_RXEN | US_CR_STTTO;
226 return 0;
Harald Weltebd717682016-02-28 19:30:05 +0100227}
Harald Welte2a6d3af2016-02-28 19:29:14 +0100228
229/***********************************************************************
Harald Welte419eb8a2016-03-20 10:04:03 +0100230 * ADC for VCC voltage detection
231 ***********************************************************************/
232
Harald Welte07872b62016-03-20 11:45:36 +0100233#ifdef DETECT_VCC_BY_ADC
234
235static int adc_triggered = 0;
Harald Welte28772eb2016-03-20 15:44:52 +0100236static int adc_sam3s_reva_errata = 0;
Harald Welte07872b62016-03-20 11:45:36 +0100237
Harald Welte419eb8a2016-03-20 10:04:03 +0100238static int card_vcc_adc_init(void)
239{
Harald Welte622b6be2016-03-20 16:43:59 +0100240 uint32_t chip_arch = CHIPID->CHIPID_CIDR & CHIPID_CIDR_ARCH_Msk;
241 uint32_t chip_ver = CHIPID->CHIPID_CIDR & CHIPID_CIDR_VERSION_Msk;
Harald Welte28772eb2016-03-20 15:44:52 +0100242
Harald Welte07872b62016-03-20 11:45:36 +0100243 PMC_EnablePeripheral(ID_ADC);
244
Harald Welte419eb8a2016-03-20 10:04:03 +0100245 ADC->ADC_CR |= ADC_CR_SWRST;
Harald Welte28772eb2016-03-20 15:44:52 +0100246 if (chip_ver == 0 &&
247 (chip_arch == CHIPID_CIDR_ARCH_SAM3SxA ||
248 chip_arch == CHIPID_CIDR_ARCH_SAM3SxB ||
249 chip_arch == CHIPID_CIDR_ARCH_SAM3SxC)) {
250 TRACE_INFO("Enabling Rev.A ADC Errata work-around\r\n");
251 adc_sam3s_reva_errata = 1;
252 }
253
254 if (adc_sam3s_reva_errata) {
255 /* Errata Work-Around to clear EOCx flags */
Harald Welte07872b62016-03-20 11:45:36 +0100256 volatile uint32_t foo;
257 int i;
258 for (i = 0; i < 16; i++)
259 foo = ADC->ADC_CDR[i];
260 }
261
262 /* Initialize ADC for AD7 / AD6, fADC=48/24=2MHz */
Harald Welte419eb8a2016-03-20 10:04:03 +0100263 ADC->ADC_MR = ADC_MR_TRGEN_DIS | ADC_MR_LOWRES_BITS_12 |
264 ADC_MR_SLEEP_NORMAL | ADC_MR_FWUP_OFF |
Harald Welte07872b62016-03-20 11:45:36 +0100265 ADC_MR_FREERUN_OFF | ADC_MR_PRESCAL(23) |
266 ADC_MR_STARTUP_SUT8 | ADC_MR_SETTLING(3) |
267 ADC_MR_ANACH_NONE | ADC_MR_TRACKTIM(4) |
Harald Welte419eb8a2016-03-20 10:04:03 +0100268 ADC_MR_TRANSFER(1) | ADC_MR_USEQ_NUM_ORDER;
269 /* enable AD6 + AD7 channels */
Harald Welte07872b62016-03-20 11:45:36 +0100270 ADC->ADC_CHER = ADC_CHER_CH7;
271 ADC->ADC_IER = ADC_IER_EOC7;
272#ifdef CARDEMU_SECOND_UART
273 ADC->ADC_CHER |= ADC_CHER_CH6;
274 ADC->ADC_IER |= ADC_IER_EOC6;
275#endif
276 NVIC_EnableIRQ(ADC_IRQn);
Harald Welte419eb8a2016-03-20 10:04:03 +0100277 ADC->ADC_CR |= ADC_CR_START;
Harald Welte07872b62016-03-20 11:45:36 +0100278
279 return 0;
Harald Welte419eb8a2016-03-20 10:04:03 +0100280}
281
Harald Welte07872b62016-03-20 11:45:36 +0100282#define UV_PER_LSB ((3300 * 1000) / 4096)
283#define VCC_UV_THRESH_1V8 1500000
284#define VCC_UV_THRESH_3V 2800000
285
286static void process_vcc_adc(struct cardem_inst *ci)
Harald Welte419eb8a2016-03-20 10:04:03 +0100287{
Harald Welte07872b62016-03-20 11:45:36 +0100288 if (ci->vcc_uv >= VCC_UV_THRESH_3V &&
289 ci->vcc_uv_last < VCC_UV_THRESH_3V) {
290 card_emu_io_statechg(ci->ch, CARD_IO_VCC, 1);
291 /* FIXME do this for real */
292 card_emu_io_statechg(ci->ch, CARD_IO_CLK, 1);
293 } else if (ci->vcc_uv < VCC_UV_THRESH_3V &&
294 ci->vcc_uv_last >= VCC_UV_THRESH_3V) {
295 /* FIXME do this for real */
296 card_emu_io_statechg(ci->ch, CARD_IO_CLK, 0);
297 card_emu_io_statechg(ci->ch, CARD_IO_VCC, 0);
Harald Welte419eb8a2016-03-20 10:04:03 +0100298 }
Harald Welte07872b62016-03-20 11:45:36 +0100299 ci->vcc_uv_last = ci->vcc_uv;
300}
301
302static uint32_t adc2uv(uint16_t adc)
303{
304 uint32_t uv = (uint32_t) adc * UV_PER_LSB;
Harald Welte622b6be2016-03-20 16:43:59 +0100305 return uv;
Harald Welte07872b62016-03-20 11:45:36 +0100306}
307
308void ADC_IrqHandler(void)
309{
310#ifdef CARDEMU_SECOND_UART
311 if (ADC->ADC_ISR & ADC_ISR_EOC6) {
312 uint16_t val = ADC->ADC_CDR[6] & 0xFFF;
313 cardem_inst[1].vcc_uv = adc2uv(val);
314 process_vcc_adc(&cardem_inst[1]);
Harald Welte28772eb2016-03-20 15:44:52 +0100315 if (adc_sam3s_reva_errata) {
316 /* Errata: START doesn't start a conversion
317 * sequence, but only a single conversion */
318 ADC->ADC_CR |= ADC_CR_START;
319 }
Harald Welte07872b62016-03-20 11:45:36 +0100320 }
321#endif
Harald Welte419eb8a2016-03-20 10:04:03 +0100322
323 if (ADC->ADC_ISR & ADC_ISR_EOC7) {
Harald Welte07872b62016-03-20 11:45:36 +0100324 uint16_t val = ADC->ADC_CDR[7] & 0xFFF;
325 cardem_inst[0].vcc_uv = adc2uv(val);
326 process_vcc_adc(&cardem_inst[0]);
327 ADC->ADC_CR |= ADC_CR_START;
Harald Welte419eb8a2016-03-20 10:04:03 +0100328 }
329}
Harald Welte07872b62016-03-20 11:45:36 +0100330#endif /* DETECT_VCC_BY_ADC */
Harald Welte419eb8a2016-03-20 10:04:03 +0100331
332/***********************************************************************
Harald Welte2a6d3af2016-02-28 19:29:14 +0100333 * Core USB / mainloop integration
334 ***********************************************************************/
335
Harald Weltec0bd7f02016-02-29 10:13:33 +0100336static void usim1_rst_irqhandler(const Pin *pPin)
337{
338 int active = PIO_Get(&pin_usim1_rst) ? 0 : 1;
Harald Welte13e82022016-03-02 15:17:53 +0100339 card_emu_io_statechg(cardem_inst[0].ch, CARD_IO_RST, active);
Harald Weltec0bd7f02016-02-29 10:13:33 +0100340}
341
Harald Welte07872b62016-03-20 11:45:36 +0100342#ifndef DETECT_VCC_BY_ADC
Harald Weltec0bd7f02016-02-29 10:13:33 +0100343static void usim1_vcc_irqhandler(const Pin *pPin)
344{
345 int active = PIO_Get(&pin_usim1_vcc) ? 1 : 0;
Harald Welte13e82022016-03-02 15:17:53 +0100346 card_emu_io_statechg(cardem_inst[0].ch, CARD_IO_VCC, active);
Harald Weltec0bd7f02016-02-29 10:13:33 +0100347 /* FIXME do this for real */
Harald Welte13e82022016-03-02 15:17:53 +0100348 card_emu_io_statechg(cardem_inst[0].ch, CARD_IO_CLK, active);
Harald Weltec0bd7f02016-02-29 10:13:33 +0100349}
Harald Welte07872b62016-03-20 11:45:36 +0100350#endif /* !DETECT_VCC_BY_ADC */
Harald Weltec0bd7f02016-02-29 10:13:33 +0100351
Harald Welte13e82022016-03-02 15:17:53 +0100352#ifdef CARDEMU_SECOND_UART
353static void usim2_rst_irqhandler(const Pin *pPin)
354{
355 int active = PIO_Get(&pin_usim2_rst) ? 0 : 1;
356 card_emu_io_statechg(cardem_inst[1].ch, CARD_IO_RST, active);
357}
358
Harald Welte07872b62016-03-20 11:45:36 +0100359#ifndef DETECT_VCC_BY_ADC
Harald Welte13e82022016-03-02 15:17:53 +0100360static void usim2_vcc_irqhandler(const Pin *pPin)
361{
362 int active = PIO_Get(&pin_usim2_vcc) ? 1 : 0;
363 card_emu_io_statechg(cardem_inst[1].ch, CARD_IO_VCC, active);
364 /* FIXME do this for real */
365 card_emu_io_statechg(cardem_inst[1].ch, CARD_IO_CLK, active);
366}
Harald Welte07872b62016-03-20 11:45:36 +0100367#endif /* !DETECT_VCC_BY_ADC */
368#endif /* CARDEMU_SECOND_UART */
Harald Welte13e82022016-03-02 15:17:53 +0100369
Harald Welte2a6d3af2016-02-28 19:29:14 +0100370/* executed once at system boot for each config */
371void mode_cardemu_configure(void)
372{
Harald Weltebd717682016-02-28 19:30:05 +0100373 TRACE_ENTRY();
Harald Welte2a6d3af2016-02-28 19:29:14 +0100374}
375
376/* called if config is activated */
377void mode_cardemu_init(void)
378{
Harald Welte13e82022016-03-02 15:17:53 +0100379 int i;
Harald Weltebd717682016-02-28 19:30:05 +0100380
Harald Welte13e82022016-03-02 15:17:53 +0100381 TRACE_ENTRY();
Harald Weltec0bd7f02016-02-29 10:13:33 +0100382
Harald Welte6d1128e2017-05-05 20:23:10 +0200383#ifdef PINS_CARDSIM
Harald Welte2a6d3af2016-02-28 19:29:14 +0100384 PIO_Configure(pins_cardsim, PIO_LISTSIZE(pins_cardsim));
Harald Welte6d1128e2017-05-05 20:23:10 +0200385#endif
Harald Welte07872b62016-03-20 11:45:36 +0100386#ifdef DETECT_VCC_BY_ADC
387 card_vcc_adc_init();
388#endif /* DETECT_VCC_BY_ADC */
Harald Welte2a6d3af2016-02-28 19:29:14 +0100389
Harald Welte13e82022016-03-02 15:17:53 +0100390 INIT_LLIST_HEAD(&cardem_inst[0].usb_out_queue);
391 rbuf_reset(&cardem_inst[0].rb);
Harald Welte2a6d3af2016-02-28 19:29:14 +0100392 PIO_Configure(pins_usim1, PIO_LISTSIZE(pins_usim1));
Harald Welte13e82022016-03-02 15:17:53 +0100393 ISO7816_Init(&cardem_inst[0].usart_info, CLK_SLAVE);
Harald Welte2a6d3af2016-02-28 19:29:14 +0100394 NVIC_EnableIRQ(USART1_IRQn);
Harald Weltec0bd7f02016-02-29 10:13:33 +0100395 PIO_ConfigureIt(&pin_usim1_rst, usim1_rst_irqhandler);
396 PIO_EnableIt(&pin_usim1_rst);
Harald Welte07872b62016-03-20 11:45:36 +0100397#ifndef DETECT_VCC_BY_ADC
Harald Weltec0bd7f02016-02-29 10:13:33 +0100398 PIO_ConfigureIt(&pin_usim1_vcc, usim1_vcc_irqhandler);
399 PIO_EnableIt(&pin_usim1_vcc);
Harald Welte07872b62016-03-20 11:45:36 +0100400#endif /* DETECT_VCC_BY_ADC */
Harald Welte8e7fca32017-05-07 16:14:33 +0200401 cardem_inst[0].ch = card_emu_init(0, 2, 0, PHONE_DATAIN, PHONE_INT);
Harald Weltec47fc5f2017-05-11 16:51:57 +0200402 sim_switch_use_physical(0, 1);
Harald Welte2a6d3af2016-02-28 19:29:14 +0100403
404#ifdef CARDEMU_SECOND_UART
Harald Welte13e82022016-03-02 15:17:53 +0100405 INIT_LLIST_HEAD(&cardem_inst[1].usb_out_queue);
406 rbuf_reset(&cardem_inst[1].rb);
Harald Welte2a6d3af2016-02-28 19:29:14 +0100407 PIO_Configure(pins_usim2, PIO_LISTSIZE(pins_usim2));
Harald Welte13e82022016-03-02 15:17:53 +0100408 ISO7816_Init(&cardem_inst[1].usart_info, CLK_SLAVE);
Harald Welte2a6d3af2016-02-28 19:29:14 +0100409 NVIC_EnableIRQ(USART0_IRQn);
Harald Welte13e82022016-03-02 15:17:53 +0100410 PIO_ConfigureIt(&pin_usim2_rst, usim2_rst_irqhandler);
411 PIO_EnableIt(&pin_usim2_rst);
Harald Welte07872b62016-03-20 11:45:36 +0100412#ifndef DETECT_VCC_BY_ADC
Harald Welte13e82022016-03-02 15:17:53 +0100413 PIO_ConfigureIt(&pin_usim2_vcc, usim2_vcc_irqhandler);
414 PIO_EnableIt(&pin_usim2_vcc);
Harald Welte07872b62016-03-20 11:45:36 +0100415#endif /* DETECT_VCC_BY_ADC */
Harald Welte8e7fca32017-05-07 16:14:33 +0200416 cardem_inst[1].ch = card_emu_init(1, 0, 1, CARDEM_USIM2_DATAIN, CARDEM_USIM2_INT);
Harald Weltec47fc5f2017-05-11 16:51:57 +0200417 sim_switch_use_physical(1, 1);
Harald Welte07872b62016-03-20 11:45:36 +0100418#endif /* CARDEMU_SECOND_UART */
Harald Weltec47fc5f2017-05-11 16:51:57 +0200419
Harald Welte2a6d3af2016-02-28 19:29:14 +0100420}
421
422/* called if config is deactivated */
423void mode_cardemu_exit(void)
424{
Harald Weltebd717682016-02-28 19:30:05 +0100425 TRACE_ENTRY();
426
Harald Welte7abdb512016-03-03 17:48:32 +0100427 /* FIXME: stop tc_fdt */
Harald Welte8e7fca32017-05-07 16:14:33 +0200428 /* FIXME: release all msg, unlink them from any queue */
Harald Welte7abdb512016-03-03 17:48:32 +0100429
Harald Weltec0bd7f02016-02-29 10:13:33 +0100430 PIO_DisableIt(&pin_usim1_rst);
431 PIO_DisableIt(&pin_usim1_vcc);
432
Harald Welte2a6d3af2016-02-28 19:29:14 +0100433 NVIC_DisableIRQ(USART1_IRQn);
434 USART_SetTransmitterEnabled(USART1, 0);
435 USART_SetReceiverEnabled(USART1, 0);
436
437#ifdef CARDEMU_SECOND_UART
Harald Welte13e82022016-03-02 15:17:53 +0100438 PIO_DisableIt(&pin_usim2_rst);
439 PIO_DisableIt(&pin_usim2_vcc);
440
Harald Welte2a6d3af2016-02-28 19:29:14 +0100441 NVIC_DisableIRQ(USART0_IRQn);
442 USART_SetTransmitterEnabled(USART0, 0);
443 USART_SetReceiverEnabled(USART0, 0);
444#endif
445}
446
Harald Welteebb80ed2016-03-02 13:56:59 +0100447/* handle a single USB command as received from the USB host */
Harald Welte25a9a802017-05-08 13:30:09 +0200448static void dispatch_usb_command_generic(struct msgb *msg, struct cardem_inst *ci)
Harald Welteebb80ed2016-03-02 13:56:59 +0100449{
Harald Welte25a9a802017-05-08 13:30:09 +0200450 struct simtrace_msg_hdr *hdr;
451
452 hdr = (struct simtrace_msg_hdr *) msg->l1h;
453 switch (hdr->msg_type) {
454 case SIMTRACE_CMD_BD_BOARD_INFO:
455 break;
456 default:
457 break;
458 }
459 usb_buf_free(msg);
460}
461
462/* handle a single USB command as received from the USB host */
463static void dispatch_usb_command_cardem(struct msgb *msg, struct cardem_inst *ci)
464{
465 struct simtrace_msg_hdr *hdr;
Harald Welte06b27f62016-03-02 14:26:38 +0100466 struct cardemu_usb_msg_set_atr *atr;
Harald Welte0eaa9922016-03-04 03:03:49 +0100467 struct cardemu_usb_msg_cardinsert *cardins;
Harald Welteebb80ed2016-03-02 13:56:59 +0100468 struct llist_head *queue;
469
Harald Welte25a9a802017-05-08 13:30:09 +0200470 hdr = (struct simtrace_msg_hdr *) msg->l1h;
Harald Welteebb80ed2016-03-02 13:56:59 +0100471 switch (hdr->msg_type) {
Harald Welte25a9a802017-05-08 13:30:09 +0200472 case SIMTRACE_MSGT_DT_CEMU_TX_DATA:
Harald Welte0eaa9922016-03-04 03:03:49 +0100473 queue = card_emu_get_uart_tx_queue(ci->ch);
Harald Welte8e7fca32017-05-07 16:14:33 +0200474 llist_add_tail(&msg->list, queue);
Harald Welte0eaa9922016-03-04 03:03:49 +0100475 card_emu_have_new_uart_tx(ci->ch);
Harald Welteebb80ed2016-03-02 13:56:59 +0100476 break;
Harald Welte25a9a802017-05-08 13:30:09 +0200477 case SIMTRACE_MSGT_DT_CEMU_SET_ATR:
478 atr = (struct cardemu_usb_msg_set_atr *) msg->l2h;
Harald Welted295b922016-03-18 21:01:36 +0100479 card_emu_set_atr(ci->ch, atr->atr, atr->atr_len);
Harald Welte8e7fca32017-05-07 16:14:33 +0200480 usb_buf_free(msg);
Harald Welte06b27f62016-03-02 14:26:38 +0100481 break;
Harald Welte25a9a802017-05-08 13:30:09 +0200482 case SIMTRACE_MSGT_DT_CEMU_CARDINSERT:
483 cardins = (struct cardemu_usb_msg_cardinsert *) msg->l2h;
Harald Welte965d5c92017-11-03 20:47:12 +0100484 if (!ci->pin_insert.pio) {
485 TRACE_INFO("%u: skipping unsupported card_insert to %s\r\n",
486 ci->num, cardins->card_insert ? "INSERTED" : "REMOVED");
487 break;
488 }
Harald Weltedde112e2016-03-20 16:42:11 +0100489 TRACE_INFO("%u: set card_insert to %s\r\n", ci->num,
490 cardins->card_insert ? "INSERTED" : "REMOVED");
Harald Welte0eaa9922016-03-04 03:03:49 +0100491 if (cardins->card_insert)
492 PIO_Set(&ci->pin_insert);
493 else
494 PIO_Clear(&ci->pin_insert);
Harald Welte8e7fca32017-05-07 16:14:33 +0200495 usb_buf_free(msg);
Harald Welte0eaa9922016-03-04 03:03:49 +0100496 break;
Harald Welte25a9a802017-05-08 13:30:09 +0200497 case SIMTRACE_MSGT_BD_CEMU_STATUS:
Harald Welteff160652016-03-19 21:59:06 +0100498 card_emu_report_status(ci->ch);
Harald Welte25a9a802017-05-08 13:30:09 +0200499 usb_buf_free(msg);
Harald Welteff160652016-03-19 21:59:06 +0100500 break;
Harald Welte25a9a802017-05-08 13:30:09 +0200501 case SIMTRACE_MSGT_BD_CEMU_STATS:
Harald Welteebb80ed2016-03-02 13:56:59 +0100502 default:
Harald Welte25a9a802017-05-08 13:30:09 +0200503 /* FIXME: Send Error */
504 usb_buf_free(msg);
505 break;
506 }
507}
508
Harald Weltefc87c242017-11-28 19:17:27 +0100509#ifdef PINS_PERST
510#include "wwan_perst.h"
511#endif
512
Harald Welte2e9254a2017-05-09 06:30:04 +0200513static int usb_command_modem_reset(struct msgb *msg, struct cardem_inst *ci)
514{
Harald Welteb91f6ad2017-05-10 22:51:30 +0200515 struct st_modem_reset *mr = (struct st_modem_reset *) msg->l2h;
Harald Welte2e9254a2017-05-09 06:30:04 +0200516
517 if (msgb_l2len(msg) < sizeof(*mr))
518 return -1;
519
520 switch (mr->asserted) {
Harald Weltefc87c242017-11-28 19:17:27 +0100521#ifdef PINS_PERST
Harald Welte2e9254a2017-05-09 06:30:04 +0200522 case 0:
523 wwan_perst_set(ci->num, 0);
524 break;
525 case 1:
526 wwan_perst_set(ci->num, 1);
527 break;
528 case 2:
529 wwan_perst_do_reset_pulse(ci->num, mr->pulse_duration_msec);
530 break;
Harald Weltefc87c242017-11-28 19:17:27 +0100531#endif
Harald Welte2e9254a2017-05-09 06:30:04 +0200532 default:
533 return -1;
534 }
535
536 return 0;
537}
538
Harald Welte5c583d32017-05-09 06:46:47 +0200539static int usb_command_sim_select(struct msgb *msg, struct cardem_inst *ci)
540{
Harald Welteb91f6ad2017-05-10 22:51:30 +0200541 struct st_modem_sim_select *mss = (struct st_modem_sim_select *) msg->l2h;
Harald Welte5c583d32017-05-09 06:46:47 +0200542
543 if (msgb_l2len(msg) < sizeof(*mss))
544 return -1;
545
546 if (mss->remote_sim)
547 sim_switch_use_physical(ci->num, 0);
548 else
549 sim_switch_use_physical(ci->num, 1);
550
551 return 0;
552}
553
Harald Welte25a9a802017-05-08 13:30:09 +0200554/* handle a single USB command as received from the USB host */
555static void dispatch_usb_command_modem(struct msgb *msg, struct cardem_inst *ci)
556{
557 struct simtrace_msg_hdr *hdr;
558
559 hdr = (struct simtrace_msg_hdr *) msg->l1h;
560 switch (hdr->msg_type) {
561 case SIMTRACE_MSGT_DT_MODEM_RESET:
Harald Welte2e9254a2017-05-09 06:30:04 +0200562 usb_command_modem_reset(msg, ci);
Harald Welte25a9a802017-05-08 13:30:09 +0200563 break;
564 case SIMTRACE_MSGT_DT_MODEM_SIM_SELECT:
Harald Welte5c583d32017-05-09 06:46:47 +0200565 usb_command_sim_select(msg, ci);
Harald Welte25a9a802017-05-08 13:30:09 +0200566 break;
567 case SIMTRACE_MSGT_BD_MODEM_STATUS:
568 break;
569 default:
570 break;
571 }
572 usb_buf_free(msg);
573}
574
575/* handle a single USB command as received from the USB host */
576static void dispatch_usb_command(struct msgb *msg, struct cardem_inst *ci)
577{
Harald Welteb91f6ad2017-05-10 22:51:30 +0200578 struct simtrace_msg_hdr *sh = (struct simtrace_msg_hdr *) msg->l1h;
Harald Welte25a9a802017-05-08 13:30:09 +0200579
580 if (msgb_length(msg) < sizeof(*sh)) {
581 /* FIXME: Error */
582 usb_buf_free(msg);
583 return;
584 }
585
586 switch (sh->msg_class) {
587 case SIMTRACE_MSGC_GENERIC:
588 dispatch_usb_command_generic(msg, ci);
589 break;
590 case SIMTRACE_MSGC_CARDEM:
591 dispatch_usb_command_cardem(msg, ci);
592 break;
593 case SIMTRACE_MSGC_MODEM:
Harald Welte23c00b62017-05-11 01:11:43 +0200594 /* FIXME: Find out why this fails if used for !=
595 * MSGC_MODEM ?!? */
596 msg->l2h = msg->l1h + sizeof(*sh);
Harald Welte25a9a802017-05-08 13:30:09 +0200597 dispatch_usb_command_modem(msg, ci);
598 break;
599 default:
600 /* FIXME: Send Error */
Harald Welte8e7fca32017-05-07 16:14:33 +0200601 usb_buf_free(msg);
Harald Welteebb80ed2016-03-02 13:56:59 +0100602 break;
603 }
604}
605
Harald Welte8e7fca32017-05-07 16:14:33 +0200606static void dispatch_received_msg(struct msgb *msg, struct cardem_inst *ci)
Harald Welteb26d0032016-03-19 13:33:02 +0100607{
Harald Welte8e7fca32017-05-07 16:14:33 +0200608 struct msgb *segm;
Harald Welte25a9a802017-05-08 13:30:09 +0200609 struct simtrace_msg_hdr *mh;
Harald Welteb26d0032016-03-19 13:33:02 +0100610
611 /* check if we have multiple concatenated commands in
612 * one message. USB endpoints are streams that don't
613 * preserve the message boundaries */
Harald Welte25a9a802017-05-08 13:30:09 +0200614 mh = (struct simtrace_msg_hdr *) msg->data;
Harald Welte8e7fca32017-05-07 16:14:33 +0200615 if (mh->msg_len == msgb_length(msg)) {
Harald Welteb26d0032016-03-19 13:33:02 +0100616 /* fast path: only one message in buffer */
Harald Welte8e7fca32017-05-07 16:14:33 +0200617 dispatch_usb_command(msg, ci);
Harald Welteb26d0032016-03-19 13:33:02 +0100618 return;
619 }
620
621 /* slow path: iterate over list of messages, allocating one new
622 * reqe_ctx per segment */
Harald Welte8e7fca32017-05-07 16:14:33 +0200623 while (1) {
Harald Welte25a9a802017-05-08 13:30:09 +0200624 mh = (struct simtrace_msg_hdr *) msg->data;
Harald Welte8e7fca32017-05-07 16:14:33 +0200625
626 segm = usb_buf_alloc(ci->ep_out);
Harald Welteb26d0032016-03-19 13:33:02 +0100627 if (!segm) {
Harald Welte8e7fca32017-05-07 16:14:33 +0200628 TRACE_ERROR("%u: ENOMEM during msg segmentation\r\n",
Harald Weltedde112e2016-03-20 16:42:11 +0100629 ci->num);
Harald Welteb26d0032016-03-19 13:33:02 +0100630 break;
631 }
Harald Welte8e7fca32017-05-07 16:14:33 +0200632
633 if (mh->msg_len > msgb_length(msg)) {
634 TRACE_ERROR("%u: Unexpected large message (%u bytes)\n",
635 ci->num, mh->msg_len);
636 usb_buf_free(segm);
637 } else {
638 uint8_t *cur = msgb_put(segm, mh->msg_len);
639 segm->l1h = segm->head;
640 memcpy(cur, mh, mh->msg_len);
641 dispatch_usb_command(segm, ci);
642 }
643 /* pull this message */
644 msgb_pull(msg, mh->msg_len);
645 /* abort if we're done */
646 if (msgb_length(msg) <= 0)
647 break;
Harald Welteb26d0032016-03-19 13:33:02 +0100648 }
649
Harald Welte8e7fca32017-05-07 16:14:33 +0200650 usb_buf_free(msg);
Harald Welteb26d0032016-03-19 13:33:02 +0100651}
652
Harald Welteebb80ed2016-03-02 13:56:59 +0100653/* iterate over the queue of incoming USB commands and dispatch/execute
654 * them */
Harald Welte9f240b62016-03-18 10:32:56 +0100655static void process_any_usb_commands(struct llist_head *main_q,
656 struct cardem_inst *ci)
Harald Welteebb80ed2016-03-02 13:56:59 +0100657{
Harald Welte9f240b62016-03-18 10:32:56 +0100658 struct llist_head *lh;
Harald Welte8e7fca32017-05-07 16:14:33 +0200659 struct msgb *msg;
Harald Welte9f240b62016-03-18 10:32:56 +0100660 int i;
Harald Welteebb80ed2016-03-02 13:56:59 +0100661
Harald Welte9f240b62016-03-18 10:32:56 +0100662 /* limit the number of iterations to 10, to ensure we don't get
663 * stuck here without returning to main loop processing */
664 for (i = 0; i < 10; i++) {
665 /* de-queue the list head in an irq-safe way */
666 lh = llist_head_dequeue_irqsafe(main_q);
667 if (!lh)
668 break;
Harald Welte8e7fca32017-05-07 16:14:33 +0200669 msg = llist_entry(lh, struct msgb, list);
670 dispatch_received_msg(msg, ci);
Harald Welte5820ea92016-03-16 22:12:00 +0100671 }
Harald Welteebb80ed2016-03-02 13:56:59 +0100672}
673
Harald Welte2a6d3af2016-02-28 19:29:14 +0100674/* main loop function, called repeatedly */
675void mode_cardemu_run(void)
676{
Harald Welteacae4122016-03-02 10:27:58 +0100677 struct llist_head *queue;
Harald Welte13e82022016-03-02 15:17:53 +0100678 unsigned int i;
Harald Welteacae4122016-03-02 10:27:58 +0100679
Harald Welte13e82022016-03-02 15:17:53 +0100680 for (i = 0; i < ARRAY_SIZE(cardem_inst); i++) {
681 struct cardem_inst *ci = &cardem_inst[i];
682
Harald Welte54cb3d02016-02-29 14:12:40 +0100683 /* drain the ring buffer from UART into card_emu */
Harald Weltec0bd7f02016-02-29 10:13:33 +0100684 while (1) {
685 __disable_irq();
Harald Welte13e82022016-03-02 15:17:53 +0100686 if (rbuf_is_empty(&ci->rb)) {
Harald Weltec0bd7f02016-02-29 10:13:33 +0100687 __enable_irq();
688 break;
689 }
Harald Welte13e82022016-03-02 15:17:53 +0100690 uint8_t byte = rbuf_read(&ci->rb);
Harald Weltec0bd7f02016-02-29 10:13:33 +0100691 __enable_irq();
Harald Welte13e82022016-03-02 15:17:53 +0100692 card_emu_process_rx_byte(ci->ch, byte);
Harald Weltedde112e2016-03-20 16:42:11 +0100693 //TRACE_ERROR("%uRx%02x\r\n", i, byte);
Harald Weltec0bd7f02016-02-29 10:13:33 +0100694 }
Harald Welteacae4122016-03-02 10:27:58 +0100695
Harald Welte8e7fca32017-05-07 16:14:33 +0200696 /* first try to send any pending messages on IRQ */
697 usb_refill_to_host(ci->ep_int);
698
699 /* then try to send any pending messages on IN */
700 usb_refill_to_host(ci->ep_in);
Harald Welteacae4122016-03-02 10:27:58 +0100701
Harald Welteebb80ed2016-03-02 13:56:59 +0100702 /* ensure we can handle incoming USB messages from the
703 * host */
Harald Welte8e7fca32017-05-07 16:14:33 +0200704 usb_refill_from_host(ci->ep_out);
705 queue = usb_get_queue(ci->ep_out);
Harald Welte0eaa9922016-03-04 03:03:49 +0100706 process_any_usb_commands(queue, ci);
Harald Welte2a6d3af2016-02-28 19:29:14 +0100707 }
Harald Welte2a6d3af2016-02-28 19:29:14 +0100708}