1 /*
2  * Copyright (c) 2017-2019 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /**
8  *  User CPR Interval
9  */
10 #if !defined(CONFIG_BT_CTLR_USER_CPR_INTERVAL_MIN)
11 /* Bluetooth defined CPR Interval Minimum (7.5ms) */
12 #define CONN_INTERVAL_MIN(x) (6)
13 #else /* CONFIG_BT_CTLR_USER_CPR_INTERVAL_MIN */
14 /* Proprietary user defined CPR Interval Minimum */
15 #define CONN_INTERVAL_MIN(x) (MAX(ull_conn_interval_min_get(x), 1))
16 #endif /* CONFIG_BT_CTLR_USER_CPR_INTERVAL_MIN */
17 
18 /**
19  *  User deference of CPR Anchor Point Move
20  */
21 #if !defined(CONFIG_BT_CTLR_USER_CPR_ANCHOR_POINT_MOVE)
22 #define DEFER_APM_CHECK(x, y, z) (false)
23 #else
24 /* Proprietary handling of peripheral CPR Anchor Point Movement Response
25  *
26  * When returning TRUE the LLCP system changes to a
27  * USER_WAIT state and an EXTERNAL trigger must kick the LLCP system
28  * to continue to either accept (with possibly changed offsets) or reject CPR
29  *
30  * When returning FALSE the LLCP system will automatically
31  * continue and thus respond immediately
32  *
33  * Possibly modified LLCP internal status/error state will determine the type of 'response'
34  *   0U                             - Accept CPR (possibly with changed offsets)
35  *   BT_HCI_ERR_UNSUPP_LL_PARAM_VAL - Reject CPR
36  */
37 struct ll_conn;
38 extern bool ull_handle_cpr_anchor_point_move(struct ll_conn *conn, uint16_t *offsets,
39 					     uint8_t *status);
40 #define DEFER_APM_CHECK(x, y, z) (ull_handle_cpr_anchor_point_move(x, y, z))
41 #endif /* CONFIG_BT_CTLR_USER_CPR_ANCHOR_POINT_MOVE */
42 /* Macro to convert time in us to connection interval units */
43 #define RADIO_CONN_EVENTS(x, y) ((uint16_t)DIV_ROUND_UP(x, y))
44 
45 /* Macro to convert time in us to periodic advertising interval units */
46 #define RADIO_SYNC_EVENTS(x, y) ((uint16_t)DIV_ROUND_UP(x, y))
47 
ull_ref_get(struct ull_hdr * hdr)48 static inline uint8_t ull_ref_get(struct ull_hdr *hdr)
49 {
50 	return hdr->ref;
51 }
52 
ull_ref_inc(struct ull_hdr * hdr)53 static inline uint8_t ull_ref_inc(struct ull_hdr *hdr)
54 {
55 	return ++hdr->ref;
56 }
57 
ull_ref_dec(struct ull_hdr * hdr)58 static inline uint8_t ull_ref_dec(struct ull_hdr *hdr)
59 {
60 	return hdr->ref--;
61 }
62 
ull_hdr_init(struct ull_hdr * hdr)63 static inline void ull_hdr_init(struct ull_hdr *hdr)
64 {
65 	hdr->ref = 0U;
66 	hdr->disabled_cb = hdr->disabled_param = NULL;
67 }
68 
69 void *ll_rx_link_alloc(void);
70 void ll_rx_link_release(memq_link_t *link);
71 void *ll_rx_alloc(void);
72 void ll_rx_release(void *node_rx);
73 void *ll_pdu_rx_alloc_peek(uint8_t count);
74 void *ll_pdu_rx_alloc(void);
75 void ll_rx_put_sched(memq_link_t *link, void *rx);
76 void ll_rx_put(memq_link_t *link, void *rx);
77 void ll_rx_sched(void);
78 void ull_ticker_status_give(uint32_t status, void *param);
79 uint32_t ull_ticker_status_take(uint32_t ret, uint32_t volatile *ret_cb);
80 void *ull_disable_mark(void *param);
81 void *ull_disable_unmark(void *param);
82 void *ull_disable_mark_get(void);
83 int ull_ticker_stop_with_mark(uint8_t ticker_handle, void *param,
84 			      void *lll_disable);
85 void *ull_update_mark(void *param);
86 void *ull_update_unmark(void *param);
87 void *ull_update_mark_get(void);
88 int ull_disable(void *param);
89 void ull_drift_ticks_get(struct node_rx_event_done *done,
90 			 uint32_t *ticks_drift_plus,
91 			 uint32_t *ticks_drift_minus);
92 
93 /**
94  * @brief   RX FIFO macro frontend
95  * @details The RXFIFO data composite consists of an MFIFO with pointers to
96  *          data elements backed by a memory pool and memq link elements.
97  *          Link memq elements have a separate pool of (_count + _extra_links)
98  *          elements. Extra links may be used for initializing one or more
99  *          external memq instances. The following data structures are created
100  *          with RXFIFO_DEFINE():
101  *          - mfifo_<_name>:    FIFO with pointers to RX node elements.
102  *          - mem_<_name>:      Backing data pool of <_count> RX node elements
103  *                              of size <_size>.
104  *          - mem_link_<_name>: Pool of <_count + _extra_links> memq_link_t
105  *                              elements.
106  */
107 #define RXFIFO_DEFINE(_name, _size, _count, _extra_links) \
108 	MFIFO_DEFINE(_name, sizeof(void *), _count); \
109 	\
110 	static const struct { \
111 		uint16_t size; \
112 		uint8_t count; \
113 		uint8_t extra_links; \
114 	} mem_##_name = { .size = MROUND(_size), .count = _count, \
115 			  .extra_links = _extra_links }; \
116 	\
117 	static struct { \
118 		void *free; \
119 		uint8_t pool[MROUND(_size) * (_count)]; \
120 	} mem_pool_##_name; \
121 	\
122 	static struct { \
123 		void *free; \
124 		uint8_t pool[sizeof(memq_link_t) * (_count + _extra_links)]; \
125 	} mem_link_##_name
126 
127 /**
128  * @brief   Initializes MFIFO and pools
129  * @details This makes the MFIFO empty and will subsequently need
130  *          RXFIFO_ALLOC. Memory pools are initialized.
131  */
132 #define RXFIFO_INIT(_name) \
133 	MFIFO_INIT(_name); \
134 	mem_init(mem_pool_##_name.pool, mem_##_name.size, \
135 		 mem_##_name.count, &mem_pool_##_name.free); \
136 	\
137 	mem_init(mem_link_##_name.pool, sizeof(memq_link_t), \
138 		 (mem_##_name.count + mem_##_name.extra_links), \
139 		 &mem_link_##_name.free)
140 
141 /**
142  * @brief   Allocate FIFO elements with backing
143  * @details This function allocates up to <_count> number of MFIFO elements by
144  *          enqueuing pointers to memory elements with associated memq links.
145  */
146 #define RXFIFO_ALLOC(_name, _count) \
147 	ull_rxfifo_alloc(mfifo_##_name.s, mfifo_##_name.n, \
148 			 mfifo_fifo_##_name.f, &mfifo_fifo_##_name.l, \
149 			 mfifo_fifo_##_name.m, &mem_pool_##_name.free, \
150 			 &mem_link_##_name.free, _count)
151 
152 /**
153  * @brief Initialize and allocate MFIFO and pools
154  */
155 #define RXFIFO_INIT_ALLOC(_name) \
156 	RXFIFO_INIT(_name); \
157 	RXFIFO_ALLOC(_name, mem_##_name.count)
158 
159 /**
160  * @brief   Release RX node
161  * @details Enqueues an RX node back into the FIFO.
162  */
163 #define RXFIFO_RELEASE(_name, _link, _rx) \
164 	ull_rxfifo_release(mfifo_##_name.s, mfifo_##_name.n, \
165 			   mfifo_fifo_##_name.f, &mfifo_fifo_##_name.l, \
166 			   mfifo_fifo_##_name.m, _link, \
167 			   (struct node_rx_hdr *)_rx)
168 
169 void ull_rxfifo_alloc(uint8_t s, uint8_t n, uint8_t f, uint8_t *l, uint8_t *m,
170 		      void *mem_free, void *link_free, uint8_t max);
171 void *ull_rxfifo_release(uint8_t s, uint8_t n, uint8_t f, uint8_t *l, uint8_t *m,
172 			 memq_link_t *link, struct node_rx_hdr *rx);
173