blob: 7a32b5766dfb6205c796992ed0364b0062ef05fd [file] [log] [blame]
Kévin Redon9a12d682018-07-08 13:21:16 +02001/* card presence utilities
2 *
3 * (C) 2016-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 Welte6d1128e2017-05-05 20:23:10 +020019#include <osmocom/core/timer.h>
20#include "board.h"
21#include "utils.h"
22#include "card_pres.h"
23
24#define NUM_CARDPRES 2
25
26#define TIMER_INTERVAL_MS 500
27
28static const Pin pin_cardpres[NUM_CARDPRES] = { PIN_DET_USIM1_PRES, PIN_DET_USIM2_PRES };
29static int last_state[NUM_CARDPRES] = { -1, -1 };
30static struct osmo_timer_list cardpres_timer;
31
32/* Determine if a SIM card is present in the given slot */
33int is_card_present(int port)
34{
35 const Pin *pin;
36 int present;
37
Harald Welteb52b8862017-11-03 20:33:10 +010038 if (port < 0 || port >= NUM_CARDPRES)
Harald Welte6d1128e2017-05-05 20:23:10 +020039 return -1;
Harald Welteb52b8862017-11-03 20:33:10 +010040 pin = &pin_cardpres[port];
Harald Welte6d1128e2017-05-05 20:23:10 +020041
42 /* Card present signals are low-active, as we have a switch
43 * against GND and an internal-pull-up in the SAM3 */
44 present = PIO_Get(pin) ? 0 : 1;
45
46 return present;
47}
48
49static void cardpres_tmr_cb(void *data)
50{
51 unsigned int i;
52
Harald Welteb52b8862017-11-03 20:33:10 +010053 for (i = 0; i < ARRAY_SIZE(pin_cardpres); i++) {
Harald Welte6d1128e2017-05-05 20:23:10 +020054 int state = is_card_present(i);
Harald Welteb52b8862017-11-03 20:33:10 +010055 if (state != last_state[i]) {
Harald Welte4e837d42017-11-03 20:35:55 +010056 TRACE_INFO("%u: Card Detect Status %d -> %d\r\n", i, last_state[i], state);
Harald Welte6d1128e2017-05-05 20:23:10 +020057 /* FIXME: report to USB host */
Harald Welteb52b8862017-11-03 20:33:10 +010058 last_state[i] = state;
Harald Welte6d1128e2017-05-05 20:23:10 +020059 }
60 }
61
62 osmo_timer_schedule(&cardpres_timer, 0, TIMER_INTERVAL_MS*1000);
63}
64
65int card_present_init(void)
66{
67 unsigned int i;
68
69 PIO_Configure(pin_cardpres, ARRAY_SIZE(pin_cardpres));
70
71 /* start timer */
72 cardpres_timer.cb = cardpres_tmr_cb;
73 osmo_timer_schedule(&cardpres_timer, 0, TIMER_INTERVAL_MS*1000);
74
75 return 2;
76}