fw/l1a: Add a message to safely count the length of a txqueue
[osmocom-bb.git] / src / target / firmware / layer1 / async.c
1 /* Asynchronous part of GSM Layer 1 */
2
3 /* (C) 2010 by Harald Welte <laforge@gnumonks.org>
4  *
5  * All Rights Reserved
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  */
22
23 #include <stdint.h>
24
25 #include <debug.h>
26 #include <arm.h>
27 #include <asm/system.h>
28
29 #include <osmocom/core/msgb.h>
30 #include <osmocom/gsm/protocol/gsm_04_08.h>
31
32 #include <layer1/sync.h>
33 #include <layer1/async.h>
34 #include <layer1/mframe_sched.h>
35 #include <layer1/sched_gsmtime.h>
36 #include <layer1/l23_api.h>
37 #include <calypso/l1_environment.h>
38
39 extern const struct tdma_sched_item rach_sched_set_ul[];
40
41 /* safely enable a message into the L1S TX queue */
42 void l1a_txq_msgb_enq(struct llist_head *queue, struct msgb *msg)
43 {
44         unsigned long flags;
45
46         local_firq_save(flags);
47         msgb_enqueue(queue, msg);
48         local_irq_restore(flags);
49 }
50
51 void l1a_meas_msgb_set(struct msgb *msg)
52 {
53         unsigned long flags;
54
55         local_firq_save(flags);
56         if (l1s.tx_meas)
57                 msgb_free(l1s.tx_meas);
58         l1s.tx_meas = msg;
59         local_irq_restore(flags);
60 }
61
62 /* safely count messages in the L1S TX queue */
63 int l1a_txq_msgb_count(struct llist_head *queue)
64 {
65         unsigned long flags;
66         int num = 0;
67         struct llist_head *le;
68
69         local_firq_save(flags);
70         llist_for_each(le, queue)
71                 num++;
72         local_irq_restore(flags);
73
74         return num;
75 }
76
77 /* safely flush all pending msgb */
78 void l1a_txq_msgb_flush(struct llist_head *queue)
79 {
80         struct msgb *msg;
81         unsigned long flags;
82
83         local_firq_save(flags);
84         while ((msg = msgb_dequeue(queue)))
85                 msgb_free(msg);
86         local_irq_restore(flags);
87 }
88
89 /* Enable a repeating multiframe task */
90 void l1a_mftask_enable(enum mframe_task task)
91 {
92         /* we don't need locking here as L1S only reads mframe.tasks */
93         mframe_enable(task);
94 }
95
96 /* Disable a repeating multiframe task */
97 void l1a_mftask_disable(enum mframe_task task)
98 {
99         /* we don't need locking here as L1S only reads mframe.tasks */
100         mframe_disable(task);
101 }
102
103 /* Set the mask for repeating multiframe tasks */
104 void l1a_mftask_set(uint32_t tasks)
105 {
106         /* we don't need locking here as L1S only reads mframe.tasks */
107         mframe_set(tasks);
108 }
109
110 /* Set TCH mode */
111 uint8_t l1a_tch_mode_set(uint8_t mode)
112 {
113         switch (mode) {
114         case GSM48_CMODE_SPEECH_V1:
115         case GSM48_CMODE_SPEECH_EFR:
116                 l1s.tch_mode = mode;
117                 break;
118         default:
119                 l1s.tch_mode = GSM48_CMODE_SIGN;
120         }
121
122         return l1s.tch_mode;
123 }
124
125 /* Initialize asynchronous part of Layer1 */
126 void l1a_init(void)
127 {
128         l1a_l23api_init();
129 }
130
131 /* Execute pending L1A completions */
132 void l1a_compl_execute(void)
133 {
134         unsigned long flags;
135         unsigned int scheduled;
136         unsigned int i;
137
138         /* get and reset the currently scheduled tasks */
139         local_firq_save(flags);
140         scheduled = l1s.scheduled_compl;
141         l1s.scheduled_compl = 0;
142         local_irq_restore(flags);
143
144         /* Iterate over list of scheduled completions, call their
145          * respective completion handler */
146         for (i = 0; i < 32; i++) {
147                 if (!(scheduled & (1 << i)))
148                         continue;
149                 /* call completion function */
150                 l1s.completion[i](i);
151         }
152 }