Harald Welte | a67be5f | 2020-09-03 10:04:36 +0200 | [diff] [blame] | 1 | /* Card UART handling |
| 2 | * |
| 3 | * (C) 2019-2020 by Harald Welte <laforge@gnumonks.org> |
| 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 | */ |
| 19 | |
Harald Welte | a40c8e5 | 2019-09-27 19:22:34 +0200 | [diff] [blame] | 20 | #include <errno.h> |
| 21 | #include <string.h> |
| 22 | #include <osmocom/core/linuxlist.h> |
| 23 | #include <osmocom/core/utils.h> |
Harald Welte | 7b64fc0 | 2019-10-09 20:49:35 +0200 | [diff] [blame] | 24 | #include <osmocom/core/timer.h> |
Harald Welte | a40c8e5 | 2019-09-27 19:22:34 +0200 | [diff] [blame] | 25 | |
| 26 | #include "cuart.h" |
| 27 | |
| 28 | static LLIST_HEAD(g_cuart_drivers); |
| 29 | |
| 30 | const struct value_string card_uart_event_vals[] = { |
| 31 | OSMO_VALUE_STRING(CUART_E_RX_SINGLE), |
| 32 | OSMO_VALUE_STRING(CUART_E_RX_COMPLETE), |
| 33 | OSMO_VALUE_STRING(CUART_E_RX_TIMEOUT), |
| 34 | OSMO_VALUE_STRING(CUART_E_TX_COMPLETE), |
| 35 | { 0, NULL } |
| 36 | }; |
| 37 | |
| 38 | static struct card_uart_driver *cuart_drv_by_name(const char *driver_name) |
| 39 | { |
| 40 | struct card_uart_driver *drv; |
| 41 | llist_for_each_entry(drv, &g_cuart_drivers, list) { |
| 42 | if (!strcmp(drv->name, driver_name)) |
| 43 | return drv; |
| 44 | } |
| 45 | return NULL; |
| 46 | } |
| 47 | |
Harald Welte | 7b64fc0 | 2019-10-09 20:49:35 +0200 | [diff] [blame] | 48 | /* obtain the current ETU in us */ |
| 49 | static int get_etu_in_us(struct card_uart *cuart) |
| 50 | { |
Eric Wild | dbff421 | 2019-11-27 19:02:31 +0100 | [diff] [blame] | 51 | OSMO_ASSERT(cuart); |
| 52 | OSMO_ASSERT(cuart->driver); |
| 53 | OSMO_ASSERT(cuart->driver->ops); |
| 54 | OSMO_ASSERT(cuart->driver->ops->ctrl); |
| 55 | |
| 56 | return 1e6 / cuart->driver->ops->ctrl(cuart, CUART_CTL_GET_BAUDRATE, 0); |
Harald Welte | 7b64fc0 | 2019-10-09 20:49:35 +0200 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | /* software waiting-time timer has expired */ |
| 60 | static void card_uart_wtime_cb(void *data) |
| 61 | { |
| 62 | struct card_uart *cuart = (struct card_uart *) data; |
| 63 | card_uart_notification(cuart, CUART_E_RX_TIMEOUT, NULL); |
| 64 | /* should we automatically disable the receiver? */ |
| 65 | } |
| 66 | |
| 67 | void card_uart_wtime_restart(struct card_uart *cuart) |
| 68 | { |
| 69 | int secs, usecs; |
| 70 | |
Eric Wild | 2f5cdd1 | 2019-11-27 18:40:44 +0100 | [diff] [blame] | 71 | if(!cuart->current_wtime_byte) |
| 72 | return; |
| 73 | |
Eric Wild | 5fcfc49 | 2020-04-03 22:27:18 +0200 | [diff] [blame] | 74 | int etu_in_us = get_etu_in_us(cuart) + 1; |
| 75 | cuart->wtime_etu = cuart->wtime_etu ? cuart->wtime_etu : 1; |
| 76 | |
Eric Wild | 3ef02c0 | 2020-09-11 00:58:59 +0200 | [diff] [blame^] | 77 | /* timemout is wtime * ETU * expected number of bytes */ |
| 78 | usecs = etu_in_us * cuart->wtime_etu * cuart->current_wtime_byte; |
Eric Wild | 5fcfc49 | 2020-04-03 22:27:18 +0200 | [diff] [blame] | 79 | |
| 80 | /* limit lower wait time to reasonable value */ |
| 81 | usecs = usecs < 300000 ? 300000 : usecs; |
| 82 | |
Harald Welte | 7b64fc0 | 2019-10-09 20:49:35 +0200 | [diff] [blame] | 83 | if (usecs > 1000000) { |
| 84 | secs = usecs / 1000000; |
| 85 | usecs = usecs % 1000000; |
| 86 | } else |
| 87 | secs = 0; |
| 88 | osmo_timer_schedule(&cuart->wtime_tmr, secs, usecs); |
| 89 | } |
| 90 | |
Harald Welte | a40c8e5 | 2019-09-27 19:22:34 +0200 | [diff] [blame] | 91 | int card_uart_open(struct card_uart *cuart, const char *driver_name, const char *device_name) |
| 92 | { |
| 93 | struct card_uart_driver *drv = cuart_drv_by_name(driver_name); |
| 94 | int rc; |
| 95 | |
| 96 | if (!drv) |
| 97 | return -ENODEV; |
| 98 | |
Harald Welte | 7b64fc0 | 2019-10-09 20:49:35 +0200 | [diff] [blame] | 99 | cuart->wtime_etu = 9600; /* ISO 7816-3 Section 8.1 */ |
Harald Welte | a40c8e5 | 2019-09-27 19:22:34 +0200 | [diff] [blame] | 100 | cuart->rx_enabled = true; |
| 101 | cuart->rx_threshold = 1; |
Harald Welte | 7b64fc0 | 2019-10-09 20:49:35 +0200 | [diff] [blame] | 102 | osmo_timer_setup(&cuart->wtime_tmr, card_uart_wtime_cb, cuart); |
Harald Welte | a40c8e5 | 2019-09-27 19:22:34 +0200 | [diff] [blame] | 103 | |
| 104 | rc = drv->ops->open(cuart, device_name); |
| 105 | if (rc < 0) |
| 106 | return rc; |
| 107 | |
| 108 | cuart->driver = drv; |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | int card_uart_close(struct card_uart *cuart) |
| 113 | { |
| 114 | OSMO_ASSERT(cuart); |
| 115 | OSMO_ASSERT(cuart->driver); |
| 116 | OSMO_ASSERT(cuart->driver->ops); |
| 117 | OSMO_ASSERT(cuart->driver->ops->close); |
| 118 | return cuart->driver->ops->close(cuart); |
| 119 | } |
| 120 | |
Harald Welte | 9700d39 | 2019-10-09 20:17:28 +0200 | [diff] [blame] | 121 | int card_uart_ctrl(struct card_uart *cuart, enum card_uart_ctl ctl, int arg) |
Harald Welte | a40c8e5 | 2019-09-27 19:22:34 +0200 | [diff] [blame] | 122 | { |
| 123 | int rc; |
| 124 | OSMO_ASSERT(cuart); |
| 125 | OSMO_ASSERT(cuart->driver); |
| 126 | OSMO_ASSERT(cuart->driver->ops); |
| 127 | OSMO_ASSERT(cuart->driver->ops->ctrl); |
Harald Welte | 7b64fc0 | 2019-10-09 20:49:35 +0200 | [diff] [blame] | 128 | |
Harald Welte | 9700d39 | 2019-10-09 20:17:28 +0200 | [diff] [blame] | 129 | rc = cuart->driver->ops->ctrl(cuart, ctl, arg); |
Harald Welte | a40c8e5 | 2019-09-27 19:22:34 +0200 | [diff] [blame] | 130 | if (rc < 0) |
| 131 | return rc; |
| 132 | |
| 133 | switch (ctl) { |
Harald Welte | 7b64fc0 | 2019-10-09 20:49:35 +0200 | [diff] [blame] | 134 | case CUART_CTL_WTIME: |
| 135 | cuart->wtime_etu = arg; |
| 136 | break; |
Harald Welte | a40c8e5 | 2019-09-27 19:22:34 +0200 | [diff] [blame] | 137 | case CUART_CTL_RX: |
Harald Welte | 9700d39 | 2019-10-09 20:17:28 +0200 | [diff] [blame] | 138 | cuart->rx_enabled = arg ? true : false; |
Harald Welte | 7b64fc0 | 2019-10-09 20:49:35 +0200 | [diff] [blame] | 139 | if (!cuart->rx_enabled) |
| 140 | osmo_timer_del(&cuart->wtime_tmr); |
Eric Wild | 2f5cdd1 | 2019-11-27 18:40:44 +0100 | [diff] [blame] | 141 | // else |
| 142 | // card_uart_wtime_restart(cuart); |
| 143 | break; |
| 144 | case CUART_CTL_RX_TIMER_HINT: |
| 145 | cuart->current_wtime_byte = arg; |
| 146 | if(arg) |
| 147 | card_uart_wtime_restart(cuart); |
| 148 | else |
| 149 | osmo_timer_del(&cuart->wtime_tmr); |
Harald Welte | a40c8e5 | 2019-09-27 19:22:34 +0200 | [diff] [blame] | 150 | break; |
| 151 | default: |
| 152 | break; |
| 153 | } |
| 154 | |
| 155 | return rc; |
| 156 | } |
| 157 | |
Harald Welte | 6603d95 | 2019-10-09 20:50:13 +0200 | [diff] [blame] | 158 | int card_uart_tx(struct card_uart *cuart, const uint8_t *data, size_t len, bool rx_after_complete) |
Harald Welte | a40c8e5 | 2019-09-27 19:22:34 +0200 | [diff] [blame] | 159 | { |
| 160 | OSMO_ASSERT(cuart); |
| 161 | OSMO_ASSERT(cuart->driver); |
| 162 | OSMO_ASSERT(cuart->driver->ops); |
| 163 | OSMO_ASSERT(cuart->driver->ops->async_tx); |
| 164 | |
| 165 | OSMO_ASSERT(!cuart->tx_busy); |
| 166 | cuart->tx_busy = true; |
Harald Welte | 6603d95 | 2019-10-09 20:50:13 +0200 | [diff] [blame] | 167 | cuart->rx_after_tx_compl = rx_after_complete; |
Harald Welte | a40c8e5 | 2019-09-27 19:22:34 +0200 | [diff] [blame] | 168 | /* disable receiver to avoid receiving what we transmit */ |
| 169 | card_uart_ctrl(cuart, CUART_CTL_RX, false); |
| 170 | |
Harald Welte | 02dd911 | 2019-10-01 08:55:29 +0200 | [diff] [blame] | 171 | return cuart->driver->ops->async_tx(cuart, data, len); |
Harald Welte | a40c8e5 | 2019-09-27 19:22:34 +0200 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | int card_uart_rx(struct card_uart *cuart, uint8_t *data, size_t len) |
| 175 | { |
| 176 | OSMO_ASSERT(cuart); |
| 177 | OSMO_ASSERT(cuart->driver); |
| 178 | OSMO_ASSERT(cuart->driver->ops); |
| 179 | OSMO_ASSERT(cuart->driver->ops->async_rx); |
| 180 | return cuart->driver->ops->async_rx(cuart, data, len); |
| 181 | } |
| 182 | |
| 183 | void card_uart_set_rx_threshold(struct card_uart *cuart, size_t rx_threshold) |
| 184 | { |
| 185 | cuart->rx_threshold = rx_threshold; |
| 186 | } |
| 187 | |
| 188 | void card_uart_notification(struct card_uart *cuart, enum card_uart_event evt, void *data) |
| 189 | { |
| 190 | OSMO_ASSERT(cuart); |
| 191 | OSMO_ASSERT(cuart->handle_event); |
| 192 | |
| 193 | switch (evt) { |
| 194 | case CUART_E_TX_COMPLETE: |
| 195 | cuart->tx_busy = false; |
| 196 | /* re-enable receiver if we're done with transmit */ |
Harald Welte | 6603d95 | 2019-10-09 20:50:13 +0200 | [diff] [blame] | 197 | if (cuart->rx_after_tx_compl) |
| 198 | card_uart_ctrl(cuart, CUART_CTL_RX, true); |
Harald Welte | a40c8e5 | 2019-09-27 19:22:34 +0200 | [diff] [blame] | 199 | break; |
Eric Wild | 2f5cdd1 | 2019-11-27 18:40:44 +0100 | [diff] [blame] | 200 | // case CUART_E_RX_COMPLETE: |
| 201 | // osmo_timer_del(&cuart->wtime_tmr); |
| 202 | // break; |
| 203 | // case CUART_E_RX_SINGLE: |
| 204 | // card_uart_wtime_restart(cuart); |
| 205 | // break; |
Harald Welte | a40c8e5 | 2019-09-27 19:22:34 +0200 | [diff] [blame] | 206 | default: |
| 207 | break; |
| 208 | } |
| 209 | |
| 210 | cuart->handle_event(cuart, evt, data); |
| 211 | } |
| 212 | |
| 213 | int card_uart_driver_register(struct card_uart_driver *drv) |
| 214 | { |
| 215 | OSMO_ASSERT(!cuart_drv_by_name(drv->name)); |
| 216 | OSMO_ASSERT(drv->name); |
| 217 | OSMO_ASSERT(drv->ops); |
| 218 | llist_add_tail(&drv->list, &g_cuart_drivers); |
| 219 | return 0; |
| 220 | } |