Kévin Redon | 9a12d68 | 2018-07-08 13:21:16 +0200 | [diff] [blame] | 1 | /* 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 Welte | 6d1128e | 2017-05-05 20:23:10 +0200 | [diff] [blame] | 19 | #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 | |
| 28 | static const Pin pin_cardpres[NUM_CARDPRES] = { PIN_DET_USIM1_PRES, PIN_DET_USIM2_PRES }; |
| 29 | static int last_state[NUM_CARDPRES] = { -1, -1 }; |
| 30 | static struct osmo_timer_list cardpres_timer; |
| 31 | |
| 32 | /* Determine if a SIM card is present in the given slot */ |
| 33 | int is_card_present(int port) |
| 34 | { |
| 35 | const Pin *pin; |
| 36 | int present; |
| 37 | |
Harald Welte | b52b886 | 2017-11-03 20:33:10 +0100 | [diff] [blame] | 38 | if (port < 0 || port >= NUM_CARDPRES) |
Harald Welte | 6d1128e | 2017-05-05 20:23:10 +0200 | [diff] [blame] | 39 | return -1; |
Harald Welte | b52b886 | 2017-11-03 20:33:10 +0100 | [diff] [blame] | 40 | pin = &pin_cardpres[port]; |
Harald Welte | 6d1128e | 2017-05-05 20:23:10 +0200 | [diff] [blame] | 41 | |
| 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 | |
| 49 | static void cardpres_tmr_cb(void *data) |
| 50 | { |
| 51 | unsigned int i; |
| 52 | |
Harald Welte | b52b886 | 2017-11-03 20:33:10 +0100 | [diff] [blame] | 53 | for (i = 0; i < ARRAY_SIZE(pin_cardpres); i++) { |
Harald Welte | 6d1128e | 2017-05-05 20:23:10 +0200 | [diff] [blame] | 54 | int state = is_card_present(i); |
Harald Welte | b52b886 | 2017-11-03 20:33:10 +0100 | [diff] [blame] | 55 | if (state != last_state[i]) { |
Harald Welte | 4e837d4 | 2017-11-03 20:35:55 +0100 | [diff] [blame] | 56 | TRACE_INFO("%u: Card Detect Status %d -> %d\r\n", i, last_state[i], state); |
Harald Welte | 6d1128e | 2017-05-05 20:23:10 +0200 | [diff] [blame] | 57 | /* FIXME: report to USB host */ |
Harald Welte | b52b886 | 2017-11-03 20:33:10 +0100 | [diff] [blame] | 58 | last_state[i] = state; |
Harald Welte | 6d1128e | 2017-05-05 20:23:10 +0200 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | |
| 62 | osmo_timer_schedule(&cardpres_timer, 0, TIMER_INTERVAL_MS*1000); |
| 63 | } |
| 64 | |
| 65 | int 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 | } |