blob: 59513add5de64ea2eaa5e1a25de6a930526a2865 [file] [log] [blame]
Kévin Redon9a12d682018-07-08 13:21:16 +02001/* Code to switch between local (physical) and remote (emulated) SIM
2 *
3 * (C) 2015-2017 by Harald Welte <hwelte@hmw-consulting.de>
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 Welte5c583d32017-05-09 06:46:47 +020019#include "board.h"
20#include "trace.h"
Harald Weltec47fc5f2017-05-11 16:51:57 +020021#include "led.h"
Harald Welte5c583d32017-05-09 06:46:47 +020022#include "sim_switch.h"
23
24#ifdef PIN_SIM_SWITCH1
25static const Pin pin_conn_usim1 = {PIO_PA20, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT};
26#endif
27#ifdef PIN_SIM_SWITCH2
28static const Pin pin_conn_usim2 = {PIO_PA28, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT};
29#endif
30
31static int initialized = 0;
32
33int sim_switch_use_physical(unsigned int nr, int physical)
34{
35 const Pin *pin;
Harald Weltec47fc5f2017-05-11 16:51:57 +020036 enum led led;
Harald Welte5c583d32017-05-09 06:46:47 +020037
38 if (!initialized) {
39 TRACE_ERROR("Somebody forgot to call sim_switch_init()\r\n");
40 sim_switch_init();
41 }
42
43 TRACE_INFO("Modem %d: %s SIM\n\r", nr,
44 physical ? "physical" : "virtual");
45
46 switch (nr) {
47#ifdef PIN_SIM_SWITCH1
Harald Welte44622df2017-05-11 00:04:50 +020048 case 0:
Harald Welte5c583d32017-05-09 06:46:47 +020049 pin = &pin_conn_usim1;
Harald Weltec47fc5f2017-05-11 16:51:57 +020050 led = LED_USIM1;
Harald Welte5c583d32017-05-09 06:46:47 +020051 break;
52#endif
53#ifdef PIN_SIM_SWITCH2
Harald Welte44622df2017-05-11 00:04:50 +020054 case 1:
Harald Welte5c583d32017-05-09 06:46:47 +020055 pin = &pin_conn_usim2;
Harald Weltec47fc5f2017-05-11 16:51:57 +020056 led = LED_USIM2;
Harald Welte5c583d32017-05-09 06:46:47 +020057 break;
58#endif
59 default:
60 TRACE_ERROR("Invalid SIM%u\n\r", nr);
61 return -1;
62 }
63
Harald Welte0709d2d2017-05-11 00:06:00 +020064 if (physical) {
65 TRACE_INFO("%u: Use local/physical SIM\r\n", nr);
Harald Welte5c583d32017-05-09 06:46:47 +020066 PIO_Clear(pin);
Harald Weltec47fc5f2017-05-11 16:51:57 +020067 led_blink(led, BLINK_ALWAYS_ON);
Harald Welte0709d2d2017-05-11 00:06:00 +020068 } else {
69 TRACE_INFO("%u: Use remote/emulated SIM\r\n", nr);
Harald Welte5c583d32017-05-09 06:46:47 +020070 PIO_Set(pin);
Harald Weltec47fc5f2017-05-11 16:51:57 +020071 led_blink(led, BLINK_ALWAYS_OFF);
Harald Welte0709d2d2017-05-11 00:06:00 +020072 }
Harald Welte5c583d32017-05-09 06:46:47 +020073
74 return 0;
75}
76
77int sim_switch_init(void)
78{
79 int num_switch = 0;
80#ifdef PIN_SIM_SWITCH1
81 PIO_Configure(&pin_conn_usim1, 1);
82 num_switch++;
83#endif
84#ifdef PIN_SIM_SWITCH2
85 PIO_Configure(&pin_conn_usim2, 1);
86 num_switch++;
87#endif
Harald Welteeb50c9f2017-11-03 20:50:47 +010088 initialized = 1;
Harald Welte5c583d32017-05-09 06:46:47 +020089 return num_switch;
90}