QMOD: Add code to determine SIM Card presence
diff --git a/firmware/libboard/qmod/include/board.h b/firmware/libboard/qmod/include/board.h
index 0f6343e..082d254 100644
--- a/firmware/libboard/qmod/include/board.h
+++ b/firmware/libboard/qmod/include/board.h
@@ -28,19 +28,20 @@
 #define PIN_USIM1_CLK_TC	{PIO_PA29, PIOA, ID_PIOA, PIO_PERIPH_B, PIO_DEFAULT}
 #define PINS_TC_USIM1		PIN_USIM1_IO_TC, PIN_USIM1_CLK_TC
 
-#define PIN_SET_USIM1_PRES	{PIO_PA12, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT}
 #define PIN_USIM1_nRST		{PIO_PA24, PIOA, ID_PIOA, PIO_INPUT, PIO_DEFAULT}
 #define PIN_USIM1_VCC		{PIO_PB3, PIOB, ID_PIOB, PIO_INPUT, PIO_DEFAULT}
 
-#define PIN_SET_USIM2_PRES	{PIO_PA14, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT}
 #define PIN_USIM2_nRST		{PIO_PA7, PIOA, ID_PIOA, PIO_INPUT, PIO_DEFAULT}
 #define PIN_USIM2_VCC		{PIO_PB2, PIOB, ID_PIOB, PIO_INPUT, PIO_DEFAULT}
 
-#define PINS_USIM1		PINS_TC_USIM1, PINS_ISO7816_USIM1, PIN_USIM1_nRST, PIN_SET_USIM1_PRES
-#define PINS_USIM2		PINS_TC_USIM2, PINS_ISO7816_USIM2, PIN_USIM2_nRST, PIN_SET_USIM2_PRES
+#define PINS_USIM1		PINS_TC_USIM1, PINS_ISO7816_USIM1, PIN_USIM1_nRST
+#define PINS_USIM2		PINS_TC_USIM2, PINS_ISO7816_USIM2, PIN_USIM2_nRST
 
-#define PINS_CARDSIM		{ PIN_SET_USIM1_PRES, PIN_SET_USIM2_PRES }
+/* from v3 and onwards only (!) */
+#define PIN_DET_USIM1_PRES	{PIO_PA12, PIOA, ID_PIOA, PIO_INPUT, PIO_PULLUP | PIO_DEGLITCH | PIO_IT_EDGE}
+#define PIN_DET_USIM2_PRES	{PIO_PA8, PIOA, ID_PIOA, PIO_INPUT, PIO_PULLUP | PIO_DEGLITCH | PIO_IT_EDGE}
 
+/* only in v2 and lower (!) */
 #define PIN_PRTPWR_OVERRIDE	{PIO_PA8, PIOA, ID_PIOA, PIO_OUTPUT_1, PIO_DEFAULT}
 
 /* inputs reading the WWAN LED level */
diff --git a/firmware/libboard/qmod/include/card_pres.h b/firmware/libboard/qmod/include/card_pres.h
new file mode 100644
index 0000000..fd7f065
--- /dev/null
+++ b/firmware/libboard/qmod/include/card_pres.h
@@ -0,0 +1,4 @@
+#pragma once
+
+int is_card_present(int port);
+int card_present_init(void);
diff --git a/firmware/libboard/qmod/source/board_qmod.c b/firmware/libboard/qmod/source/board_qmod.c
index 0d30ede..9f25a4b 100644
--- a/firmware/libboard/qmod/source/board_qmod.c
+++ b/firmware/libboard/qmod/source/board_qmod.c
@@ -8,6 +8,7 @@
 #include "wwan_led.h"
 #include "wwan_perst.h"
 #include "boardver_adc.h"
+#include "card_pres.h"
 #include "osmocom/core/timer.h"
 
 static const Pin pin_hubpwr_override = PIN_PRTPWR_OVERRIDE;
@@ -257,4 +258,6 @@
 
 	/* Obtain the circuit board version (currently just prints voltage */
 	get_board_version_adc();
+	/* Initialize checking for card insert/remove events */
+	card_present_init();
 }
diff --git a/firmware/libboard/qmod/source/card_pres.c b/firmware/libboard/qmod/source/card_pres.c
new file mode 100644
index 0000000..344ab09
--- /dev/null
+++ b/firmware/libboard/qmod/source/card_pres.c
@@ -0,0 +1,58 @@
+#include <osmocom/core/timer.h>
+#include "board.h"
+#include "utils.h"
+#include "card_pres.h"
+
+#define NUM_CARDPRES	2
+
+#define TIMER_INTERVAL_MS	500
+
+static const Pin pin_cardpres[NUM_CARDPRES] = { PIN_DET_USIM1_PRES, PIN_DET_USIM2_PRES };
+static int last_state[NUM_CARDPRES] = { -1, -1 };
+static struct osmo_timer_list cardpres_timer;
+
+/* Determine if a SIM card is present in the given slot */
+int is_card_present(int port)
+{
+	const Pin *pin;
+	int present;
+
+	if (port < 1 || port > NUM_CARDPRES)
+		return -1;
+	pin = &pin_cardpres[port-1];
+
+	/* Card present signals are low-active, as we have a switch
+	 * against GND and an internal-pull-up in the SAM3 */
+	present = PIO_Get(pin) ? 0 : 1;
+
+	return present;
+}
+
+static void cardpres_tmr_cb(void *data)
+{
+	unsigned int i;
+
+	for (i = 1; i <= ARRAY_SIZE(pin_cardpres); i++) {
+		int state = is_card_present(i);
+		if (state != last_state[i-1]) {
+			TRACE_INFO("Card Detect %d Status %d -> %d\r\n", i, last_state[i], state);
+			/* FIXME: report to USB host */
+			last_state[i-1] = state;
+		}
+	}
+
+	osmo_timer_schedule(&cardpres_timer, 0, TIMER_INTERVAL_MS*1000);
+}
+
+int card_present_init(void)
+{
+	unsigned int i;
+
+	PIO_Configure(pin_cardpres, ARRAY_SIZE(pin_cardpres));
+
+	/* start timer */
+	cardpres_timer.cb = cardpres_tmr_cb;
+	osmo_timer_schedule(&cardpres_timer, 0, TIMER_INTERVAL_MS*1000);
+
+	return 2;
+}
diff --git a/firmware/libcommon/source/mode_cardemu.c b/firmware/libcommon/source/mode_cardemu.c
index 20d3356..13a5b73 100644
--- a/firmware/libcommon/source/mode_cardemu.c
+++ b/firmware/libcommon/source/mode_cardemu.c
@@ -13,7 +13,9 @@
 
 #define TRACE_ENTRY()	TRACE_DEBUG("%s entering\r\n", __func__)
 
+#ifdef PINS_CARDSIM
 static const Pin pins_cardsim[] = PINS_CARDSIM;
+#endif
 
 /* UART pins */
 static const Pin pins_usim1[]	= {PINS_USIM1};
@@ -52,7 +54,9 @@
 		.ep_out = PHONE_DATAOUT,
 		.ep_in = PHONE_DATAIN,
 		.ep_int = PHONE_INT,
+#ifdef PIN_SET_USIM1_PRES
 		.pin_insert = PIN_SET_USIM1_PRES,
+#endif
 	},
 #ifdef CARDEMU_SECOND_UART
 	{
@@ -65,7 +69,9 @@
 		.ep_out = CARDEM_USIM2_DATAOUT,
 		.ep_in = CARDEM_USIM2_DATAIN,
 		.ep_int = CARDEM_USIM2_INT,
+#ifdef PIN_SET_USIM2_PRES
 		.pin_insert = PIN_SET_USIM2_PRES,
+#endif
 	},
 #endif
 };
@@ -371,7 +377,9 @@
 
 	TRACE_ENTRY();
 
+#ifdef PINS_CARDSIM
 	PIO_Configure(pins_cardsim, PIO_LISTSIZE(pins_cardsim));
+#endif
 #ifdef DETECT_VCC_BY_ADC
 	card_vcc_adc_init();
 #endif /* DETECT_VCC_BY_ADC */