Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 1 | #ifndef _SERCOMM_H |
| 2 | #define _SERCOMM_H |
| 3 | |
| 4 | #include <osmocom/core/msgb.h> |
| 5 | |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 6 | /* a low sercomm_dlci means high priority. A high DLCI means low priority */ |
| 7 | enum sercomm_dlci { |
| 8 | SC_DLCI_HIGHEST = 0, |
| 9 | SC_DLCI_DEBUG = 4, |
| 10 | SC_DLCI_L1A_L23 = 5, |
| 11 | SC_DLCI_LOADER = 9, |
| 12 | SC_DLCI_CONSOLE = 10, |
| 13 | SC_DLCI_ECHO = 128, |
| 14 | _SC_DLCI_MAX |
| 15 | }; |
| 16 | |
Harald Welte | 1358836 | 2017-04-30 23:57:55 +0200 | [diff] [blame] | 17 | struct osmo_sercomm_inst; |
Harald Welte | ea3d3ba | 2017-05-02 21:24:48 +0200 | [diff] [blame] | 18 | /*! \brief call-back function for per-DLC receive handler |
| 19 | * \param[in] sercomm instance on which msg was received |
| 20 | * \param[in] dlci DLC Identifier of received msg |
| 21 | * \param[in] msg received message that needs to be processed */ |
Harald Welte | 1358836 | 2017-04-30 23:57:55 +0200 | [diff] [blame] | 22 | 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] | 23 | |
Harald Welte | ea3d3ba | 2017-05-02 21:24:48 +0200 | [diff] [blame] | 24 | /*! \brief one instance of a sercomm multiplex/demultiplex */ |
Harald Welte | 1358836 | 2017-04-30 23:57:55 +0200 | [diff] [blame] | 25 | struct osmo_sercomm_inst { |
Harald Welte | ea3d3ba | 2017-05-02 21:24:48 +0200 | [diff] [blame] | 26 | /*! \brief Has this instance been initialized? */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 27 | int initialized; |
Harald Welte | ea3d3ba | 2017-05-02 21:24:48 +0200 | [diff] [blame] | 28 | /*! \brief UART Identifier */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 29 | int uart_id; |
| 30 | |
Harald Welte | ea3d3ba | 2017-05-02 21:24:48 +0200 | [diff] [blame] | 31 | /*! \brief transmit side */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 32 | struct { |
Harald Welte | ea3d3ba | 2017-05-02 21:24:48 +0200 | [diff] [blame] | 33 | /*! \brief per-DLC queue of pending transmit msgbs */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 34 | struct llist_head dlci_queues[_SC_DLCI_MAX]; |
Harald Welte | ea3d3ba | 2017-05-02 21:24:48 +0200 | [diff] [blame] | 35 | /*! \brief msgb currently being transmitted */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 36 | struct msgb *msg; |
Harald Welte | ea3d3ba | 2017-05-02 21:24:48 +0200 | [diff] [blame] | 37 | /*! \brief transmit state */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 38 | int state; |
Harald Welte | ea3d3ba | 2017-05-02 21:24:48 +0200 | [diff] [blame] | 39 | /*! \brief next to-be-transmitted char in msg */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 40 | uint8_t *next_char; |
| 41 | } tx; |
| 42 | |
Harald Welte | ea3d3ba | 2017-05-02 21:24:48 +0200 | [diff] [blame] | 43 | /*! \brief receive side */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 44 | struct { |
Harald Welte | ea3d3ba | 2017-05-02 21:24:48 +0200 | [diff] [blame] | 45 | /*! \brief per-DLC handler call-back functions */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 46 | dlci_cb_t dlci_handler[_SC_DLCI_MAX]; |
Harald Welte | ea3d3ba | 2017-05-02 21:24:48 +0200 | [diff] [blame] | 47 | /*! \brief msgb allocation size for rx msgs */ |
Harald Welte | f6adcd7 | 2017-05-01 00:19:13 +0200 | [diff] [blame] | 48 | unsigned int msg_size; |
Harald Welte | ea3d3ba | 2017-05-02 21:24:48 +0200 | [diff] [blame] | 49 | /*! \brief currently received msgb */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 50 | struct msgb *msg; |
Harald Welte | ea3d3ba | 2017-05-02 21:24:48 +0200 | [diff] [blame] | 51 | /*! \brief receive state */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 52 | int state; |
Harald Welte | ea3d3ba | 2017-05-02 21:24:48 +0200 | [diff] [blame] | 53 | /*! \brief DLCI of currently received msgb */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 54 | uint8_t dlci; |
Harald Welte | ea3d3ba | 2017-05-02 21:24:48 +0200 | [diff] [blame] | 55 | /*! \brief CTRL of currently received msgb */ |
Harald Welte | cc95f4b | 2017-04-30 21:39:33 +0200 | [diff] [blame] | 56 | uint8_t ctrl; |
| 57 | } rx; |
| 58 | }; |
| 59 | |
| 60 | |
Harald Welte | 1358836 | 2017-04-30 23:57:55 +0200 | [diff] [blame] | 61 | void osmo_sercomm_init(struct osmo_sercomm_inst *sercomm); |
| 62 | int osmo_sercomm_initialized(struct osmo_sercomm_inst *sercomm); |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 63 | |
| 64 | /* User Interface: Tx */ |
Harald Welte | 1358836 | 2017-04-30 23:57:55 +0200 | [diff] [blame] | 65 | 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] | 66 | 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] | 67 | |
| 68 | /* User Interface: Rx */ |
Harald Welte | 1358836 | 2017-04-30 23:57:55 +0200 | [diff] [blame] | 69 | 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] | 70 | |
Harald Welte | 799bef5 | 2017-05-15 17:16:48 +0200 | [diff] [blame^] | 71 | int osmo_sercomm_change_speed(struct osmo_sercomm_inst *sercomm, uint32_t bdrt); |
| 72 | |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 73 | /* Driver Interface */ |
| 74 | |
Harald Welte | 1358836 | 2017-04-30 23:57:55 +0200 | [diff] [blame] | 75 | 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] | 76 | 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] | 77 | |
Harald Welte | 799bef5 | 2017-05-15 17:16:48 +0200 | [diff] [blame^] | 78 | extern void sercomm_drv_lock(unsigned long *flags); |
| 79 | extern void sercomm_drv_unlock(unsigned long *flags); |
| 80 | extern void sercomm_drv_start_tx(struct osmo_sercomm_inst *sercomm); |
| 81 | extern int sercomm_drv_baudrate_chg(struct osmo_sercomm_inst *sercomm, uint32_t bdrt); |
| 82 | |
Harald Welte | 1358836 | 2017-04-30 23:57:55 +0200 | [diff] [blame] | 83 | static inline struct msgb *osmo_sercomm_alloc_msgb(unsigned int len) |
Harald Welte | c68af6a | 2017-04-30 21:21:52 +0200 | [diff] [blame] | 84 | { |
| 85 | return msgb_alloc_headroom(len+4, 4, "sercomm_tx"); |
| 86 | } |
| 87 | |
| 88 | #endif /* _SERCOMM_H */ |