ensure RF_ARFCN is part of l1ctl messages sent to host
[osmocom-bb.git] / src / target / firmware / layer1 / l23_api.c
1 /* Synchronous part of GSM Layer 1: API to Layer2+ */
2
3 /* (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.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 #define DEBUG
24
25 #include <stdint.h>
26 #include <stdio.h>
27 #include <debug.h>
28
29 #include <comm/msgb.h>
30 #include <comm/sercomm.h>
31
32 #include <layer1/sync.h>
33 #include <layer1/async.h>
34 #include <layer1/mframe_sched.h>
35 #include <layer1/tpu_window.h>
36
37 #include <rf/trf6151.h>
38
39 #include <l1a_l23_interface.h>
40
41 /* the size we will allocate struct msgb* for HDLC */
42 #define L3_MSG_SIZE (sizeof(struct l1ctl_data_ind) + 4)
43 #define L3_MSG_HEAD 4
44
45 void l1_queue_for_l2(struct msgb *msg)
46 {
47         /* forward via serial for now */
48         sercomm_sendmsg(SC_DLCI_L1A_L23, msg);
49 }
50
51 static enum mframe_task chan_nr2mf_task(uint8_t chan_nr)
52 {
53         uint8_t cbits = chan_nr >> 3;
54         uint8_t lch_idx;
55
56         if (cbits == 0x01) {
57                 lch_idx = 0;
58                 /* FIXME: TCH/F */
59         } else if ((cbits & 0x1e) == 0x02) {
60                 lch_idx = cbits & 0x1;
61                 /* FIXME: TCH/H */
62         } else if ((cbits & 0x1c) == 0x04) {
63                 lch_idx = cbits & 0x3;
64                 return MF_TASK_SDCCH4_0 + lch_idx;
65         } else if ((cbits & 0x18) == 0x08) {
66                 lch_idx = cbits & 0x7;
67                 return MF_TASK_SDCCH8_0 + lch_idx;
68 #if 0
69         } else if (cbits == 0x10) {
70                 /* FIXME: when to do extended BCCH? */
71                 return MF_TASK_BCCH_NORM;
72         } else if (cbits == 0x11 || cbits == 0x12) {
73                 /* FIXME: how to decide CCCH norm/extd? */
74                 return MF_TASK_BCCH_CCCH;
75 #endif
76         }
77         return 0;
78 }
79
80 struct msgb *l1_create_l2_msg(int msg_type, uint32_t fn, uint16_t snr,
81                               uint16_t arfcn)
82 {
83         struct l1ctl_info_dl *dl;
84         struct msgb *msg;
85
86         msg = msgb_alloc_headroom(L3_MSG_SIZE, L3_MSG_HEAD, "l1_burst");
87         if (!msg) {
88                 while (1) {
89                         puts("OOPS. Out of buffers...\n");
90                 }
91
92                 return NULL;
93         }
94
95         dl = (struct l1ctl_info_dl *) msgb_put(msg, sizeof(*dl));
96         dl->msg_type = msg_type;
97         /* FIXME: we may want to compute T1/T2/T3 in L23 */
98         gsm_fn2gsmtime(&dl->time, fn);
99         dl->snr[0] = snr;
100         dl->band_arfcn = arfcn;
101
102         return msg;
103 }
104
105 /* callbakc from SERCOMM when L2 sends a message to L1 */
106 static void l1a_l23_rx_cb(uint8_t dlci, struct msgb *msg)
107 {
108         struct l1ctl_info_ul *ul = (struct l1ctl_info_ul *) msg->data;
109         struct l1ctl_sync_new_ccch_req *sync_req;
110         struct l1ctl_rach_req *rach_req;
111         struct l1ctl_dm_est_req *est_req;
112         struct l1ctl_data_ind *data_ind;
113         struct llist_head *tx_queue;
114
115         {
116                 int i;
117                 puts("l1a_l23_rx_cb: ");
118                 for (i = 0; i < msg->len; i++)
119                         printf("%02x ", msg->data[i]);
120                 puts("\n");
121         }
122
123         if (sizeof(*ul) > msg->len) {
124                 printf("l1a_l23_cb: Short message. %u\n", msg->len);
125                 goto exit_msgbfree;
126         }
127
128         switch (ul->msg_type) {
129         case L1CTL_NEW_CCCH_REQ:
130                 if (sizeof(*ul) + sizeof(*sync_req) > msg->len) {
131                         printf("Short sync msg. %u\n", msg->len);
132                         break;
133                 }
134
135                 sync_req = (struct l1ctl_sync_new_ccch_req *) (&msg->data[0] + sizeof(*ul));
136                 printd("L1CTL_DM_EST_REQ (arfcn=%u)\n", sync_req->band_arfcn);
137
138                 /* reset scheduler and hardware */
139                 tdma_sched_reset();
140                 l1s_dsp_abort();
141
142                 /* tune to specified frequency */
143                 trf6151_rx_window(0, sync_req->band_arfcn, 40, 0);
144                 tpu_end_scenario();
145
146                 printd("Starting FCCH Recognition\n");
147                 l1s_fb_test(1, 0);
148                 break;
149         case L1CTL_DM_EST_REQ:
150                 est_req = (struct l1ctl_dm_est_req *) ul->payload;
151                 printd("L1CTL_DM_EST_REQ (arfcn=%u, chan_nr=0x%02x)\n",
152                         est_req->band_arfcn, ul->chan_nr);
153                 if (est_req->band_arfcn != l1s.serving_cell.arfcn) {
154                         /* FIXME: ARFCN */
155                         puts("We don't support ARFCN switches yet\n");
156                         break;
157                 }
158                 if (ul->chan_nr & 0x7) {
159                         /* FIXME: Timeslot */
160                         puts("We don't support non-0 TS yet\n");
161                         break;
162                 }
163                 if (est_req->h0.h) {
164                         puts("We don't support frequency hopping yet\n");
165                         break;
166                 }
167                 /* FIXME: set TSC of ded chan according to est_req.h0.tsc */
168                 /* figure out which MF tasks to enable */
169                 l1s.mf_tasks = (1 << chan_nr2mf_task(ul->chan_nr));
170                 break;
171         case L1CTL_RACH_REQ:
172                 rach_req = (struct l1ctl_rach_req *) ul->payload;
173                 printd("L1CTL_RACH_REQ (ra=0x%02x)\n", rach_req->ra);
174                 l1a_rach_req(27, rach_req->ra);
175                 break;
176         case L1CTL_DATA_REQ:
177                 data_ind = (struct l1ctl_data_ind *) ul->payload;
178                 printd("L1CTL_DATA_REQ (link_id=0x%02x)\n", ul->link_id);
179                 printd("sizeof(struct l1ctl_info_ul)=%u\n", sizeof(struct l1ctl_info_ul));
180                 if (ul->link_id & 0x40)
181                         tx_queue = &l1s.tx_queue[L1S_CHAN_SACCH];
182                 else
183                         tx_queue = &l1s.tx_queue[L1S_CHAN_MAIN];
184                 msg->l3h = data_ind->data;
185                 printd("ul=%p, ul->payload=%p, data_ind=%p, data_ind->data=%p l3h=%p\n",
186                         ul, ul->payload, data_ind, data_ind->data, msg->l3h);
187                 l1a_txq_msgb_enq(tx_queue, msg);
188                 /* we have to keep the msgb, not free it! */
189                 goto exit_nofree;
190         }
191
192 exit_msgbfree:
193         msgb_free(msg);
194 exit_nofree:
195         return;
196 }
197
198 void l1a_l23api_init(void)
199 {
200         sercomm_register_rx_cb(SC_DLCI_L1A_L23, l1a_l23_rx_cb);
201 }