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