Harald Welte | a40c8e5 | 2019-09-27 19:22:34 +0200 | [diff] [blame] | 1 | #pragma once |
Harald Welte | a67be5f | 2020-09-03 10:04:36 +0200 | [diff] [blame] | 2 | /* Card UART handling |
| 3 | * |
| 4 | * (C) 2019-2020 by Harald Welte <laforge@gnumonks.org> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA |
| 19 | */ |
| 20 | |
Harald Welte | a40c8e5 | 2019-09-27 19:22:34 +0200 | [diff] [blame] | 21 | #include <stdint.h> |
| 22 | #include <stdbool.h> |
| 23 | #include <osmocom/core/linuxlist.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 <osmocom/core/select.h> |
| 27 | #include "utils_ringbuffer.h" |
| 28 | |
Harald Welte | 03d6ebb | 2019-09-28 23:19:31 +0200 | [diff] [blame] | 29 | struct usart_async_descriptor; |
| 30 | |
Harald Welte | a40c8e5 | 2019-09-27 19:22:34 +0200 | [diff] [blame] | 31 | enum card_uart_event { |
| 32 | /* a single byte was received, it's present at the (uint8_t *) data location */ |
| 33 | CUART_E_RX_SINGLE, |
| 34 | /* an entire block of data was received */ |
| 35 | CUART_E_RX_COMPLETE, |
| 36 | CUART_E_RX_TIMEOUT, |
| 37 | /* an entire block of data was written, as instructed in prior card_uart_tx() call */ |
| 38 | CUART_E_TX_COMPLETE, |
| 39 | }; |
| 40 | |
| 41 | extern const struct value_string card_uart_event_vals[]; |
| 42 | |
| 43 | enum card_uart_ctl { |
Harald Welte | d4328e2 | 2019-10-10 10:07:50 +0200 | [diff] [blame] | 44 | CUART_CTL_RX, /* enable/disable receiver */ |
Eric Wild | 2f5cdd1 | 2019-11-27 18:40:44 +0100 | [diff] [blame] | 45 | CUART_CTL_RX_TIMER_HINT, /* tell cuart approximate number of rx bytes */ |
Eric Wild | fd0bace | 2019-11-27 18:33:09 +0100 | [diff] [blame] | 46 | CUART_CTL_NO_RXTX, /* enable/disable receiver */ |
Eric Wild | 70f3691 | 2020-04-03 22:29:02 +0200 | [diff] [blame] | 47 | CUART_CTL_POWER_5V0, |
| 48 | CUART_CTL_POWER_3V0, |
| 49 | CUART_CTL_POWER_1V8, |
Harald Welte | d4328e2 | 2019-10-10 10:07:50 +0200 | [diff] [blame] | 50 | CUART_CTL_CLOCK, /* enable/disable ICC clock */ |
Eric Wild | 587d4fb | 2019-11-27 18:59:43 +0100 | [diff] [blame] | 51 | CUART_CTL_SET_CLOCK_FREQ, /* set ICC clock frequency (hz)*/ |
Harald Welte | d4328e2 | 2019-10-10 10:07:50 +0200 | [diff] [blame] | 52 | CUART_CTL_RST, /* enable/disable ICC reset */ |
| 53 | CUART_CTL_WTIME, /* set the waiting time (in etu) */ |
Eric Wild | 587d4fb | 2019-11-27 18:59:43 +0100 | [diff] [blame] | 54 | CUART_CTL_SET_FD, |
Eric Wild | 9970ca0 | 2019-11-27 19:01:42 +0100 | [diff] [blame] | 55 | CUART_CTL_GET_BAUDRATE, |
| 56 | CUART_CTL_GET_CLOCK_FREQ, |
Harald Welte | a40c8e5 | 2019-09-27 19:22:34 +0200 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | struct card_uart; |
| 60 | |
| 61 | struct card_uart_ops { |
| 62 | int (*open)(struct card_uart *cuart, const char *device_name); |
| 63 | int (*close)(struct card_uart *cuart); |
Harald Welte | 02dd911 | 2019-10-01 08:55:29 +0200 | [diff] [blame] | 64 | int (*async_tx)(struct card_uart *cuart, const uint8_t *data, size_t len); |
Harald Welte | a40c8e5 | 2019-09-27 19:22:34 +0200 | [diff] [blame] | 65 | int (*async_rx)(struct card_uart *cuart, uint8_t *data, size_t len); |
| 66 | |
Harald Welte | 9700d39 | 2019-10-09 20:17:28 +0200 | [diff] [blame] | 67 | int (*ctrl)(struct card_uart *cuart, enum card_uart_ctl ctl, int arg); |
Harald Welte | a40c8e5 | 2019-09-27 19:22:34 +0200 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | /* Card UART driver */ |
| 71 | struct card_uart_driver { |
| 72 | /* global list of card UART drivers */ |
| 73 | struct llist_head list; |
| 74 | /* human-readable name of driver */ |
| 75 | const char *name; |
| 76 | /* operations implementing the driver */ |
| 77 | const struct card_uart_ops *ops; |
| 78 | }; |
| 79 | |
| 80 | struct card_uart { |
| 81 | /* member in global list of UARTs */ |
| 82 | struct llist_head list; |
| 83 | /* driver serving this UART */ |
| 84 | const struct card_uart_driver *driver; |
| 85 | /* event-handler function */ |
| 86 | void (*handle_event)(struct card_uart *cuart, enum card_uart_event evt, void *data); |
| 87 | /* opaque pointer for user */ |
| 88 | void *priv; |
| 89 | |
| 90 | /* is the transmitter currently busy (true) or not (false)? */ |
| 91 | bool tx_busy; |
| 92 | /* is the receiver currently enabled or not? */ |
| 93 | bool rx_enabled; |
Harald Welte | 6603d95 | 2019-10-09 20:50:13 +0200 | [diff] [blame] | 94 | /* should the receiver automatically be nabled after TX completion? */ |
| 95 | bool rx_after_tx_compl; |
Harald Welte | a40c8e5 | 2019-09-27 19:22:34 +0200 | [diff] [blame] | 96 | |
| 97 | /*! after how many bytes should we notify the user? If this is '1', we will |
| 98 | * issue CUART_E_RX_SINGLE; if it is > 1, we will issue CUART_E_RX_COMPLETE */ |
| 99 | uint32_t rx_threshold; |
| 100 | |
Harald Welte | 7b64fc0 | 2019-10-09 20:49:35 +0200 | [diff] [blame] | 101 | uint32_t wtime_etu; |
| 102 | struct osmo_timer_list wtime_tmr; |
Eric Wild | 2f5cdd1 | 2019-11-27 18:40:44 +0100 | [diff] [blame] | 103 | /* expected number of bytes, for timeout */ |
| 104 | uint32_t current_wtime_byte; |
Harald Welte | 7b64fc0 | 2019-10-09 20:49:35 +0200 | [diff] [blame] | 105 | |
Harald Welte | a40c8e5 | 2019-09-27 19:22:34 +0200 | [diff] [blame] | 106 | /* driver-specific private data */ |
| 107 | union { |
| 108 | struct { |
| 109 | /* ringbuffer on receive side */ |
| 110 | uint8_t rx_buf[256]; |
| 111 | struct ringbuffer rx_ringbuf; |
| 112 | |
| 113 | /* pointer to (user-allocated) transmit buffer and length */ |
| 114 | const uint8_t *tx_buf; |
| 115 | size_t tx_buf_len; |
| 116 | /* index: offset of next to be transmitted byte in tx_buf */ |
| 117 | size_t tx_index; |
Harald Welte | 18ec322 | 2019-09-28 21:41:59 +0200 | [diff] [blame] | 118 | /* number of bytes we have received echoed back during transmit */ |
| 119 | uint32_t rx_count_during_tx; |
Harald Welte | a40c8e5 | 2019-09-27 19:22:34 +0200 | [diff] [blame] | 120 | |
| 121 | struct osmo_fd ofd; |
| 122 | unsigned int baudrate; |
| 123 | } tty; |
Harald Welte | 03d6ebb | 2019-09-28 23:19:31 +0200 | [diff] [blame] | 124 | struct { |
| 125 | struct usart_async_descriptor *usa_pd; |
| 126 | uint8_t slot_nr; |
Eric Wild | b39d832 | 2019-11-27 16:50:28 +0100 | [diff] [blame] | 127 | /* in us, required, no delay breaks _rx_ */ |
| 128 | uint32_t extrawait_after_rx; |
Eric Wild | 9970ca0 | 2019-11-27 19:01:42 +0100 | [diff] [blame] | 129 | uint32_t current_baudrate; |
Harald Welte | 03d6ebb | 2019-09-28 23:19:31 +0200 | [diff] [blame] | 130 | } asf4; |
Harald Welte | a40c8e5 | 2019-09-27 19:22:34 +0200 | [diff] [blame] | 131 | } u; |
| 132 | }; |
| 133 | |
| 134 | /*! Open the Card UART */ |
| 135 | int card_uart_open(struct card_uart *cuart, const char *driver_name, const char *device_name); |
| 136 | |
| 137 | /*! Close the Card UART */ |
| 138 | int card_uart_close(struct card_uart *cuart); |
| 139 | |
| 140 | /*! Schedule (asynchronous) transmit data via UART; optionally enable Rx after completion */ |
Harald Welte | 6603d95 | 2019-10-09 20:50:13 +0200 | [diff] [blame] | 141 | 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] | 142 | |
| 143 | /*! Schedule (asynchronous) receive data via UART (after CUART_E_RX_COMPLETE) */ |
| 144 | int card_uart_rx(struct card_uart *cuart, uint8_t *data, size_t len); |
| 145 | |
Harald Welte | 9700d39 | 2019-10-09 20:17:28 +0200 | [diff] [blame] | 146 | 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] | 147 | |
| 148 | /*! Set the Rx notification threshold in number of bytes received */ |
| 149 | void card_uart_set_rx_threshold(struct card_uart *cuart, size_t rx_threshold); |
| 150 | |
Harald Welte | 7b64fc0 | 2019-10-09 20:49:35 +0200 | [diff] [blame] | 151 | /* (re)start the software WTIME timer */ |
| 152 | void card_uart_wtime_restart(struct card_uart *cuart); |
| 153 | |
Harald Welte | a40c8e5 | 2019-09-27 19:22:34 +0200 | [diff] [blame] | 154 | void card_uart_notification(struct card_uart *cuart, enum card_uart_event evt, void *data); |
| 155 | |
| 156 | int card_uart_driver_register(struct card_uart_driver *drv); |