Harald Welte | a40c8e5 | 2019-09-27 19:22:34 +0200 | [diff] [blame] | 1 | #pragma once |
| 2 | #include <stdint.h> |
| 3 | #include <stdbool.h> |
| 4 | #include <osmocom/core/linuxlist.h> |
Harald Welte | 7b64fc0 | 2019-10-09 20:49:35 +0200 | [diff] [blame] | 5 | #include <osmocom/core/timer.h> |
Harald Welte | a40c8e5 | 2019-09-27 19:22:34 +0200 | [diff] [blame] | 6 | |
| 7 | #include <osmocom/core/select.h> |
| 8 | #include "utils_ringbuffer.h" |
| 9 | |
Harald Welte | 4aab33f | 2019-09-28 23:19:31 +0200 | [diff] [blame] | 10 | struct usart_async_descriptor; |
| 11 | |
Harald Welte | a40c8e5 | 2019-09-27 19:22:34 +0200 | [diff] [blame] | 12 | enum card_uart_event { |
| 13 | /* a single byte was received, it's present at the (uint8_t *) data location */ |
| 14 | CUART_E_RX_SINGLE, |
| 15 | /* an entire block of data was received */ |
| 16 | CUART_E_RX_COMPLETE, |
| 17 | CUART_E_RX_TIMEOUT, |
| 18 | /* an entire block of data was written, as instructed in prior card_uart_tx() call */ |
| 19 | CUART_E_TX_COMPLETE, |
| 20 | }; |
| 21 | |
| 22 | extern const struct value_string card_uart_event_vals[]; |
| 23 | |
| 24 | enum card_uart_ctl { |
Harald Welte | ec815ad | 2019-10-10 10:07:50 +0200 | [diff] [blame^] | 25 | CUART_CTL_RX, /* enable/disable receiver */ |
| 26 | CUART_CTL_POWER, /* enable/disable ICC power */ |
| 27 | CUART_CTL_CLOCK, /* enable/disable ICC clock */ |
| 28 | CUART_CTL_RST, /* enable/disable ICC reset */ |
| 29 | CUART_CTL_WTIME, /* set the waiting time (in etu) */ |
Harald Welte | a40c8e5 | 2019-09-27 19:22:34 +0200 | [diff] [blame] | 30 | }; |
| 31 | |
| 32 | struct card_uart; |
| 33 | |
| 34 | struct card_uart_ops { |
| 35 | int (*open)(struct card_uart *cuart, const char *device_name); |
| 36 | int (*close)(struct card_uart *cuart); |
Harald Welte | 02dd911 | 2019-10-01 08:55:29 +0200 | [diff] [blame] | 37 | 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] | 38 | int (*async_rx)(struct card_uart *cuart, uint8_t *data, size_t len); |
| 39 | |
Harald Welte | 9700d39 | 2019-10-09 20:17:28 +0200 | [diff] [blame] | 40 | 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] | 41 | }; |
| 42 | |
| 43 | /* Card UART driver */ |
| 44 | struct card_uart_driver { |
| 45 | /* global list of card UART drivers */ |
| 46 | struct llist_head list; |
| 47 | /* human-readable name of driver */ |
| 48 | const char *name; |
| 49 | /* operations implementing the driver */ |
| 50 | const struct card_uart_ops *ops; |
| 51 | }; |
| 52 | |
| 53 | struct card_uart { |
| 54 | /* member in global list of UARTs */ |
| 55 | struct llist_head list; |
| 56 | /* driver serving this UART */ |
| 57 | const struct card_uart_driver *driver; |
| 58 | /* event-handler function */ |
| 59 | void (*handle_event)(struct card_uart *cuart, enum card_uart_event evt, void *data); |
| 60 | /* opaque pointer for user */ |
| 61 | void *priv; |
| 62 | |
| 63 | /* is the transmitter currently busy (true) or not (false)? */ |
| 64 | bool tx_busy; |
| 65 | /* is the receiver currently enabled or not? */ |
| 66 | bool rx_enabled; |
Harald Welte | 6603d95 | 2019-10-09 20:50:13 +0200 | [diff] [blame] | 67 | /* should the receiver automatically be nabled after TX completion? */ |
| 68 | bool rx_after_tx_compl; |
Harald Welte | a40c8e5 | 2019-09-27 19:22:34 +0200 | [diff] [blame] | 69 | |
| 70 | /*! after how many bytes should we notify the user? If this is '1', we will |
| 71 | * issue CUART_E_RX_SINGLE; if it is > 1, we will issue CUART_E_RX_COMPLETE */ |
| 72 | uint32_t rx_threshold; |
| 73 | |
Harald Welte | 7b64fc0 | 2019-10-09 20:49:35 +0200 | [diff] [blame] | 74 | uint32_t wtime_etu; |
| 75 | struct osmo_timer_list wtime_tmr; |
| 76 | |
Harald Welte | a40c8e5 | 2019-09-27 19:22:34 +0200 | [diff] [blame] | 77 | /* driver-specific private data */ |
| 78 | union { |
| 79 | struct { |
| 80 | /* ringbuffer on receive side */ |
| 81 | uint8_t rx_buf[256]; |
| 82 | struct ringbuffer rx_ringbuf; |
| 83 | |
| 84 | /* pointer to (user-allocated) transmit buffer and length */ |
| 85 | const uint8_t *tx_buf; |
| 86 | size_t tx_buf_len; |
| 87 | /* index: offset of next to be transmitted byte in tx_buf */ |
| 88 | size_t tx_index; |
Harald Welte | 18ec322 | 2019-09-28 21:41:59 +0200 | [diff] [blame] | 89 | /* number of bytes we have received echoed back during transmit */ |
| 90 | uint32_t rx_count_during_tx; |
Harald Welte | a40c8e5 | 2019-09-27 19:22:34 +0200 | [diff] [blame] | 91 | |
| 92 | struct osmo_fd ofd; |
| 93 | unsigned int baudrate; |
| 94 | } tty; |
Harald Welte | 4aab33f | 2019-09-28 23:19:31 +0200 | [diff] [blame] | 95 | struct { |
| 96 | struct usart_async_descriptor *usa_pd; |
| 97 | uint8_t slot_nr; |
| 98 | } asf4; |
Harald Welte | a40c8e5 | 2019-09-27 19:22:34 +0200 | [diff] [blame] | 99 | } u; |
| 100 | }; |
| 101 | |
| 102 | /*! Open the Card UART */ |
| 103 | int card_uart_open(struct card_uart *cuart, const char *driver_name, const char *device_name); |
| 104 | |
| 105 | /*! Close the Card UART */ |
| 106 | int card_uart_close(struct card_uart *cuart); |
| 107 | |
| 108 | /*! Schedule (asynchronous) transmit data via UART; optionally enable Rx after completion */ |
Harald Welte | 6603d95 | 2019-10-09 20:50:13 +0200 | [diff] [blame] | 109 | 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] | 110 | |
| 111 | /*! Schedule (asynchronous) receive data via UART (after CUART_E_RX_COMPLETE) */ |
| 112 | int card_uart_rx(struct card_uart *cuart, uint8_t *data, size_t len); |
| 113 | |
Harald Welte | 9700d39 | 2019-10-09 20:17:28 +0200 | [diff] [blame] | 114 | 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] | 115 | |
| 116 | /*! Set the Rx notification threshold in number of bytes received */ |
| 117 | void card_uart_set_rx_threshold(struct card_uart *cuart, size_t rx_threshold); |
| 118 | |
Harald Welte | 7b64fc0 | 2019-10-09 20:49:35 +0200 | [diff] [blame] | 119 | /* (re)start the software WTIME timer */ |
| 120 | void card_uart_wtime_restart(struct card_uart *cuart); |
| 121 | |
Harald Welte | a40c8e5 | 2019-09-27 19:22:34 +0200 | [diff] [blame] | 122 | void card_uart_notification(struct card_uart *cuart, enum card_uart_event evt, void *data); |
| 123 | |
| 124 | int card_uart_driver_register(struct card_uart_driver *drv); |