Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \file sercomm.h |
| 2 | * Osmocom Sercomm HDLC (de)multiplex. |
| 3 | */ |
| 4 | |
Vadim Yanitskiy | 395db20 | 2022-09-28 23:51:42 +0700 | [diff] [blame] | 5 | #pragma once |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 6 | |
| 7 | #include <osmocom/core/msgb.h> |
| 8 | |
Harald Welte | 7711713 | 2017-05-15 17:33:02 +0200 | [diff] [blame] | 9 | /*! \defgroup sercomm Seriall Communications (HDLC) |
| 10 | * @{ |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 11 | * \file sercomm.h */ |
Harald Welte | 7711713 | 2017-05-15 17:33:02 +0200 | [diff] [blame] | 12 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 13 | /*! A low sercomm_dlci means high priority. A high DLCI means low priority */ |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 14 | enum sercomm_dlci { |
| 15 | SC_DLCI_HIGHEST = 0, |
| 16 | SC_DLCI_DEBUG = 4, |
| 17 | SC_DLCI_L1A_L23 = 5, |
| 18 | SC_DLCI_LOADER = 9, |
| 19 | SC_DLCI_CONSOLE = 10, |
| 20 | SC_DLCI_ECHO = 128, |
| 21 | _SC_DLCI_MAX |
| 22 | }; |
| 23 | |
Harald Welte | 1358836 | 2017-04-30 23:57:55 +0200 | [diff] [blame] | 24 | struct osmo_sercomm_inst; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 25 | /*! call-back function for per-DLC receive handler |
Harald Welte | ea3d3ba | 2017-05-02 21:24:48 +0200 | [diff] [blame] | 26 | * \param[in] sercomm instance on which msg was received |
| 27 | * \param[in] dlci DLC Identifier of received msg |
| 28 | * \param[in] msg received message that needs to be processed */ |
Harald Welte | 1358836 | 2017-04-30 23:57:55 +0200 | [diff] [blame] | 29 | typedef void (*dlci_cb_t)(struct osmo_sercomm_inst *sercomm, uint8_t dlci, struct msgb *msg); |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 30 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 31 | /*! one instance of a sercomm multiplex/demultiplex */ |
Harald Welte | 1358836 | 2017-04-30 23:57:55 +0200 | [diff] [blame] | 32 | struct osmo_sercomm_inst { |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 33 | /*! Has this instance been initialized? */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 34 | int initialized; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 35 | /*! UART Identifier */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 36 | int uart_id; |
| 37 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 38 | /*! transmit side */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 39 | struct { |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 40 | /*! per-DLC queue of pending transmit msgbs */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 41 | struct llist_head dlci_queues[_SC_DLCI_MAX]; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 42 | /*! msgb currently being transmitted */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 43 | struct msgb *msg; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 44 | /*! transmit state */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 45 | int state; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 46 | /*! next to-be-transmitted char in msg */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 47 | uint8_t *next_char; |
| 48 | } tx; |
| 49 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 50 | /*! receive side */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 51 | struct { |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 52 | /*! per-DLC handler call-back functions */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 53 | dlci_cb_t dlci_handler[_SC_DLCI_MAX]; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 54 | /*! msgb allocation size for rx msgs */ |
Harald Welte | f6adcd7 | 2017-05-01 00:19:13 +0200 | [diff] [blame] | 55 | unsigned int msg_size; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 56 | /*! currently received msgb */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 57 | struct msgb *msg; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 58 | /*! receive state */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 59 | int state; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 60 | /*! DLCI of currently received msgb */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 61 | uint8_t dlci; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 62 | /*! CTRL of currently received msgb */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 63 | uint8_t ctrl; |
| 64 | } rx; |
| 65 | }; |
| 66 | |
| 67 | |
Harald Welte | 1358836 | 2017-04-30 23:57:55 +0200 | [diff] [blame] | 68 | void osmo_sercomm_init(struct osmo_sercomm_inst *sercomm); |
| 69 | int osmo_sercomm_initialized(struct osmo_sercomm_inst *sercomm); |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 70 | |
| 71 | /* User Interface: Tx */ |
Harald Welte | 1358836 | 2017-04-30 23:57:55 +0200 | [diff] [blame] | 72 | void osmo_sercomm_sendmsg(struct osmo_sercomm_inst *sercomm, uint8_t dlci, struct msgb *msg); |
Harald Welte | 1358836 | 2017-04-30 23:57:55 +0200 | [diff] [blame] | 73 | unsigned int osmo_sercomm_tx_queue_depth(struct osmo_sercomm_inst *sercomm, uint8_t dlci); |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 74 | |
| 75 | /* User Interface: Rx */ |
Harald Welte | 1358836 | 2017-04-30 23:57:55 +0200 | [diff] [blame] | 76 | int osmo_sercomm_register_rx_cb(struct osmo_sercomm_inst *sercomm, uint8_t dlci, dlci_cb_t cb); |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 77 | |
Harald Welte | 799bef5 | 2017-05-15 17:16:48 +0200 | [diff] [blame] | 78 | int osmo_sercomm_change_speed(struct osmo_sercomm_inst *sercomm, uint32_t bdrt); |
| 79 | |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 80 | /* Driver Interface */ |
| 81 | |
Harald Welte | 1358836 | 2017-04-30 23:57:55 +0200 | [diff] [blame] | 82 | int osmo_sercomm_drv_pull(struct osmo_sercomm_inst *sercomm, uint8_t *ch); |
Harald Welte | 1358836 | 2017-04-30 23:57:55 +0200 | [diff] [blame] | 83 | int osmo_sercomm_drv_rx_char(struct osmo_sercomm_inst *sercomm, uint8_t ch); |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 84 | |
Harald Welte | 799bef5 | 2017-05-15 17:16:48 +0200 | [diff] [blame] | 85 | extern void sercomm_drv_lock(unsigned long *flags); |
| 86 | extern void sercomm_drv_unlock(unsigned long *flags); |
Harald Welte | 7711713 | 2017-05-15 17:33:02 +0200 | [diff] [blame] | 87 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 88 | /*! low-level driver routine to request start of transmission |
Harald Welte | 7711713 | 2017-05-15 17:33:02 +0200 | [diff] [blame] | 89 | * The Sercomm code calls this function to inform the low-level driver |
| 90 | * that some data is pending for transmission, and the low-level driver |
| 91 | * should (if not active already) start enabling tx_empty interrupts |
| 92 | * and pull drivers out of sercomm using osmo_sercomm_drv_pull() until |
| 93 | * the latter returns 0. |
| 94 | * \param[in] sercomm Osmocom sercomm instance for which to change |
| 95 | */ |
Harald Welte | 799bef5 | 2017-05-15 17:16:48 +0200 | [diff] [blame] | 96 | extern void sercomm_drv_start_tx(struct osmo_sercomm_inst *sercomm); |
Harald Welte | 7711713 | 2017-05-15 17:33:02 +0200 | [diff] [blame] | 97 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 98 | /*! low-level driver routine to execute baud-rate change |
Harald Welte | 7711713 | 2017-05-15 17:33:02 +0200 | [diff] [blame] | 99 | * \param[in] sercomm Osmocom sercomm instance for which to change |
| 100 | * \param[in] bdrt New Baud-Rate (integer) |
| 101 | * \returns 0 on success; negative in case of error |
| 102 | */ |
Harald Welte | 799bef5 | 2017-05-15 17:16:48 +0200 | [diff] [blame] | 103 | extern int sercomm_drv_baudrate_chg(struct osmo_sercomm_inst *sercomm, uint32_t bdrt); |
| 104 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 105 | /*! Sercomm msgb allocator function */ |
Harald Welte | 1358836 | 2017-04-30 23:57:55 +0200 | [diff] [blame] | 106 | static inline struct msgb *osmo_sercomm_alloc_msgb(unsigned int len) |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 107 | { |
| 108 | return msgb_alloc_headroom(len+4, 4, "sercomm_tx"); |
| 109 | } |
| 110 | |
Harald Welte | 7711713 | 2017-05-15 17:33:02 +0200 | [diff] [blame] | 111 | /*! @} */ |