1 /* Synchronous part of GSM Layer 1: API to Layer2+ */
3 /* (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org>
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.
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.
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.
30 #include <byteorder.h>
32 #include <osmocom/core/msgb.h>
33 #include <osmocom/gsm/protocol/gsm_04_08.h>
34 #include <comm/sercomm.h>
36 #include <layer1/sync.h>
37 #include <layer1/async.h>
38 #include <layer1/mframe_sched.h>
39 #include <layer1/prim.h>
40 #include <layer1/tpu_window.h>
42 #include <abb/twl3025.h>
43 #include <rf/trf6151.h>
45 #include <l1ctl_proto.h>
47 /* the size we will allocate struct msgb* for HDLC */
49 #define L3_MSG_SIZE (sizeof(struct l1ctl_hdr)+sizeof(struct l1ctl_info_dl)+sizeof(struct l1ctl_traffic_ind) + L3_MSG_HEAD)
51 void l1_queue_for_l2(struct msgb *msg)
53 /* forward via serial for now */
54 sercomm_sendmsg(SC_DLCI_L1A_L23, msg);
63 static uint32_t chan_nr2mf_task_mask(uint8_t chan_nr, uint8_t neigh_mode)
65 uint8_t cbits = chan_nr >> 3;
66 uint8_t tn = chan_nr & 0x7;
68 enum mframe_task master_task = 0;
69 uint32_t neigh_task = 0;
70 enum mf_type multiframe;
74 master_task = (tn & 1) ? MF_TASK_TCH_F_ODD : MF_TASK_TCH_F_EVEN;
75 multiframe = (tn & 1) ? MF26ODD : MF26EVEN;
76 } else if ((cbits & 0x1e) == 0x02) {
77 lch_idx = cbits & 0x1;
78 master_task = MF_TASK_TCH_H_0 + lch_idx;
79 } else if ((cbits & 0x1c) == 0x04) {
80 lch_idx = cbits & 0x3;
81 master_task = MF_TASK_SDCCH4_0 + lch_idx;
83 } else if ((cbits & 0x18) == 0x08) {
84 lch_idx = cbits & 0x7;
85 master_task = MF_TASK_SDCCH8_0 + lch_idx;
88 } else if (cbits == 0x10) {
89 /* FIXME: when to do extended BCCH? */
90 master_task = MF_TASK_BCCH_NORM;
91 } else if (cbits == 0x11 || cbits == 0x12) {
92 /* FIXME: how to decide CCCH norm/extd? */
93 master_task = MF_TASK_BCCH_CCCH;
100 neigh_task = (1 << MF_TASK_NEIGH_PM51);
103 neigh_task = (1 << MF_TASK_NEIGH_PM26E);
106 neigh_task = (1 << MF_TASK_NEIGH_PM26O);
111 return (1 << master_task) | neigh_task;
114 static int chan_nr2dchan_type(uint8_t chan_nr)
116 uint8_t cbits = chan_nr >> 3;
119 return GSM_DCHAN_TCH_F;
120 } else if ((cbits & 0x1e) == 0x02) {
121 return GSM_DCHAN_TCH_H;
122 } else if ((cbits & 0x1c) == 0x04) {
123 return GSM_DCHAN_SDCCH_4;
124 } else if ((cbits & 0x18) == 0x08) {
125 return GSM_DCHAN_SDCCH_8;
127 return GSM_DCHAN_UNKNOWN;
130 static int chan_nr_is_tch(uint8_t chan_nr)
132 return ((chan_nr >> 3) == 0x01 || /* TCH/F */
133 ((chan_nr >> 3) & 0x1e) == 0x02); /* TCH/H */
136 static void audio_set_enabled(int enabled)
138 twl3025_unit_enable(TWL3025_UNIT_VUL, enabled);
139 twl3025_unit_enable(TWL3025_UNIT_VDL, enabled);
142 struct msgb *l1ctl_msgb_alloc(uint8_t msg_type)
145 struct l1ctl_hdr *l1h;
147 msg = msgb_alloc_headroom(L3_MSG_SIZE, L3_MSG_HEAD, "l1ctl");
150 puts("OOPS. Out of buffers...\n");
155 l1h = (struct l1ctl_hdr *) msgb_put(msg, sizeof(*l1h));
156 l1h->msg_type = msg_type;
159 msg->l1h = (uint8_t *)l1h;
164 struct msgb *l1_create_l2_msg(int msg_type, uint32_t fn, uint16_t snr,
167 struct l1ctl_info_dl *dl;
168 struct msgb *msg = l1ctl_msgb_alloc(msg_type);
170 dl = (struct l1ctl_info_dl *) msgb_put(msg, sizeof(*dl));
171 dl->frame_nr = htonl(fn);
173 dl->band_arfcn = htons(arfcn);
178 /* receive a L1CTL_FBSB_REQ from L23 */
179 static void l1ctl_rx_fbsb_req(struct msgb *msg)
181 struct l1ctl_hdr *l1h = (struct l1ctl_hdr *) msg->data;
182 struct l1ctl_fbsb_req *sync_req = (struct l1ctl_fbsb_req *) l1h->data;
184 if (sizeof(*sync_req) > msg->len) {
185 printf("Short sync msg. %u\n", msg->len);
189 printd("L1CTL_FBSB_REQ (arfcn=%u, flags=0x%x)\n",
190 ntohs(sync_req->band_arfcn), sync_req->flags);
192 /* reset scheduler and hardware */
195 /* pre-set the CCCH mode */
196 l1s.serving_cell.ccch_mode = sync_req->ccch_mode;
198 printd("Starting FCCH Recognition\n");
199 l1s_fbsb_req(1, sync_req);
202 /* receive a L1CTL_DM_EST_REQ from L23 */
203 static void l1ctl_rx_dm_est_req(struct msgb *msg)
205 struct l1ctl_hdr *l1h = (struct l1ctl_hdr *) msg->data;
206 struct l1ctl_info_ul *ul = (struct l1ctl_info_ul *) l1h->data;
207 struct l1ctl_dm_est_req *est_req = (struct l1ctl_dm_est_req *) ul->payload;
209 printd("L1CTL_DM_EST_REQ (arfcn=%u, chan_nr=0x%02x, tsc=%u)\n",
210 ntohs(est_req->h0.band_arfcn), ul->chan_nr, est_req->tsc);
212 /* disable neighbour cell measurement */
213 mframe_disable(MF_TASK_NEIGH_PM51);
215 /* configure dedicated channel state */
216 l1s.dedicated.type = chan_nr2dchan_type(ul->chan_nr);
217 l1s.dedicated.tsc = est_req->tsc;
218 l1s.dedicated.tn = ul->chan_nr & 0x7;
219 l1s.dedicated.h = est_req->h;
223 l1s.dedicated.h1.hsn = est_req->h1.hsn;
224 l1s.dedicated.h1.maio = est_req->h1.maio;
225 l1s.dedicated.h1.n = est_req->h1.n;
226 for (i=0; i<est_req->h1.n; i++)
227 l1s.dedicated.h1.ma[i] = ntohs(est_req->h1.ma[i]);
229 l1s.dedicated.h0.arfcn = ntohs(est_req->h0.band_arfcn);
233 if (chan_nr_is_tch(ul->chan_nr)) {
235 l1a_tch_mode_set(est_req->tch_mode);
238 l1s.tch_sync = 1; /* can be set without locking */
241 audio_set_enabled(est_req->tch_mode != GSM48_CMODE_SIGN);
244 /* figure out which MF tasks to enable */
245 l1a_mftask_set(chan_nr2mf_task_mask(ul->chan_nr, NEIGH_MODE_PM));
248 /* receive a L1CTL_DM_FREQ_REQ from L23 */
249 static void l1ctl_rx_dm_freq_req(struct msgb *msg)
251 struct l1ctl_hdr *l1h = (struct l1ctl_hdr *) msg->data;
252 struct l1ctl_info_ul *ul = (struct l1ctl_info_ul *) l1h->data;
253 struct l1ctl_dm_freq_req *freq_req =
254 (struct l1ctl_dm_freq_req *) ul->payload;
256 printd("L1CTL_DM_FREQ_REQ (arfcn=%u, tsc=%u)\n",
257 ntohs(freq_req->h0.band_arfcn), freq_req->tsc);
259 /* configure dedicated channel state */
260 l1s.dedicated.st_tsc = freq_req->tsc;
261 l1s.dedicated.st_h = freq_req->h;
265 l1s.dedicated.st_h1.hsn = freq_req->h1.hsn;
266 l1s.dedicated.st_h1.maio = freq_req->h1.maio;
267 l1s.dedicated.st_h1.n = freq_req->h1.n;
268 for (i=0; i<freq_req->h1.n; i++)
269 l1s.dedicated.st_h1.ma[i] = ntohs(freq_req->h1.ma[i]);
271 l1s.dedicated.st_h0.arfcn = ntohs(freq_req->h0.band_arfcn);
274 l1a_freq_req(ntohs(freq_req->fn));
277 /* receive a L1CTL_CRYPTO_REQ from L23 */
278 static void l1ctl_rx_crypto_req(struct msgb *msg)
280 struct l1ctl_hdr *l1h = (struct l1ctl_hdr *) msg->data;
281 struct l1ctl_info_ul *ul = (struct l1ctl_info_ul *) l1h->data;
282 struct l1ctl_crypto_req *cr = (struct l1ctl_crypto_req *) ul->payload;
283 uint8_t key_len = msg->len - sizeof(*l1h) - sizeof(*ul) - sizeof(*cr);
285 printd("L1CTL_CRYPTO_REQ (algo=A5/%u, len=%u)\n", cr->algo, key_len);
287 if (cr->algo && key_len != 8) {
288 printd("L1CTL_CRYPTO_REQ -> Invalid key\n");
292 dsp_load_ciph_param(cr->algo, cr->key);
295 /* receive a L1CTL_DM_REL_REQ from L23 */
296 static void l1ctl_rx_dm_rel_req(struct msgb *msg)
298 struct l1ctl_hdr *l1h = (struct l1ctl_hdr *) msg->data;
299 struct l1ctl_info_ul *ul = (struct l1ctl_info_ul *) l1h->data;
301 printd("L1CTL_DM_REL_REQ\n");
303 l1s.dedicated.type = GSM_DCHAN_NONE;
304 l1a_txq_msgb_flush(&l1s.tx_queue[L1S_CHAN_MAIN]);
305 l1a_txq_msgb_flush(&l1s.tx_queue[L1S_CHAN_SACCH]);
306 l1a_meas_msgb_set(NULL);
307 dsp_load_ciph_param(0, NULL);
308 l1a_tch_mode_set(GSM48_CMODE_SIGN);
309 audio_set_enabled(0);
313 /* receive a L1CTL_PARAM_REQ from L23 */
314 static void l1ctl_rx_param_req(struct msgb *msg)
316 struct l1ctl_hdr *l1h = (struct l1ctl_hdr *) msg->data;
317 struct l1ctl_info_ul *ul = (struct l1ctl_info_ul *) l1h->data;
318 struct l1ctl_par_req *par_req = (struct l1ctl_par_req *) ul->payload;
320 printd("L1CTL_PARAM_REQ (ta=%d, tx_power=%d)\n", par_req->ta,
323 l1s.ta = par_req->ta;
324 l1s.tx_power = par_req->tx_power;
327 /* receive a L1CTL_RACH_REQ from L23 */
328 static void l1ctl_rx_rach_req(struct msgb *msg)
330 struct l1ctl_hdr *l1h = (struct l1ctl_hdr *) msg->data;
331 struct l1ctl_info_ul *ul = (struct l1ctl_info_ul *) l1h->data;
332 struct l1ctl_rach_req *rach_req = (struct l1ctl_rach_req *) ul->payload;
334 printd("L1CTL_RACH_REQ (ra=0x%02x, offset=%d combined=%d)\n",
335 rach_req->ra, ntohs(rach_req->offset), rach_req->combined);
337 l1a_rach_req(ntohs(rach_req->offset), rach_req->combined,
341 /* receive a L1CTL_DATA_REQ from L23 */
342 static void l1ctl_rx_data_req(struct msgb *msg)
344 struct l1ctl_hdr *l1h = (struct l1ctl_hdr *) msg->data;
345 struct l1ctl_info_ul *ul = (struct l1ctl_info_ul *) l1h->data;
346 struct l1ctl_data_ind *data_ind = (struct l1ctl_data_ind *) ul->payload;
347 struct llist_head *tx_queue;
349 printd("L1CTL_DATA_REQ (link_id=0x%02x)\n", ul->link_id);
351 msg->l3h = data_ind->data;
352 if (ul->link_id & 0x40) {
353 struct gsm48_hdr *gh = (struct gsm48_hdr *)(data_ind->data + 5);
354 if (gh->proto_discr == GSM48_PDISC_RR
355 && gh->msg_type == GSM48_MT_RR_MEAS_REP) {
356 printd("updating measurement report\n");
357 l1a_meas_msgb_set(msg);
360 tx_queue = &l1s.tx_queue[L1S_CHAN_SACCH];
362 tx_queue = &l1s.tx_queue[L1S_CHAN_MAIN];
364 printd("ul=%p, ul->payload=%p, data_ind=%p, data_ind->data=%p l3h=%p\n",
365 ul, ul->payload, data_ind, data_ind->data, msg->l3h);
367 l1a_txq_msgb_enq(tx_queue, msg);
370 /* receive a L1CTL_PM_REQ from L23 */
371 static void l1ctl_rx_pm_req(struct msgb *msg)
373 struct l1ctl_hdr *l1h = (struct l1ctl_hdr *) msg->data;
374 struct l1ctl_pm_req *pm_req = (struct l1ctl_pm_req *) l1h->data;
376 switch (pm_req->type) {
379 l1s.pm.range.arfcn_start =
380 ntohs(pm_req->range.band_arfcn_from);
381 l1s.pm.range.arfcn_next =
382 ntohs(pm_req->range.band_arfcn_from);
383 l1s.pm.range.arfcn_end =
384 ntohs(pm_req->range.band_arfcn_to);
385 printf("L1CTL_PM_REQ start=%u end=%u\n",
386 l1s.pm.range.arfcn_start, l1s.pm.range.arfcn_end);
390 l1s_pm_test(1, l1s.pm.range.arfcn_next);
393 /* Transmit a L1CTL_RESET_IND or L1CTL_RESET_CONF */
394 void l1ctl_tx_reset(uint8_t msg_type, uint8_t reset_type)
396 struct msgb *msg = l1ctl_msgb_alloc(msg_type);
397 struct l1ctl_reset *reset_resp;
398 reset_resp = (struct l1ctl_reset *)
399 msgb_put(msg, sizeof(*reset_resp));
400 reset_resp->type = reset_type;
402 l1_queue_for_l2(msg);
405 /* receive a L1CTL_RESET_REQ from L23 */
406 static void l1ctl_rx_reset_req(struct msgb *msg)
408 struct l1ctl_hdr *l1h = (struct l1ctl_hdr *) msg->data;
409 struct l1ctl_reset *reset_req =
410 (struct l1ctl_reset *) l1h->data;
412 switch (reset_req->type) {
413 case L1CTL_RES_T_FULL:
414 printf("L1CTL_RESET_REQ: FULL!\n");
417 audio_set_enabled(0);
418 l1ctl_tx_reset(L1CTL_RESET_CONF, reset_req->type);
420 case L1CTL_RES_T_SCHED:
421 printf("L1CTL_RESET_REQ: SCHED!\n");
422 l1ctl_tx_reset(L1CTL_RESET_CONF, reset_req->type);
423 sched_gsmtime_reset();
426 printf("unknown L1CTL_RESET_REQ type\n");
431 /* Transmit a L1CTL_CCCH_MODE_CONF */
432 static void l1ctl_tx_ccch_mode_conf(uint8_t ccch_mode)
434 struct msgb *msg = l1ctl_msgb_alloc(L1CTL_CCCH_MODE_CONF);
435 struct l1ctl_ccch_mode_conf *mode_conf;
436 mode_conf = (struct l1ctl_ccch_mode_conf *)
437 msgb_put(msg, sizeof(*mode_conf));
438 mode_conf->ccch_mode = ccch_mode;
440 l1_queue_for_l2(msg);
443 /* receive a L1CTL_CCCH_MODE_REQ from L23 */
444 static void l1ctl_rx_ccch_mode_req(struct msgb *msg)
446 struct l1ctl_hdr *l1h = (struct l1ctl_hdr *) msg->data;
447 struct l1ctl_ccch_mode_req *ccch_mode_req =
448 (struct l1ctl_ccch_mode_req *) l1h->data;
449 uint8_t ccch_mode = ccch_mode_req->ccch_mode;
451 /* pre-set the CCCH mode */
452 l1s.serving_cell.ccch_mode = ccch_mode;
455 mframe_disable(MF_TASK_CCCH_COMB);
456 mframe_disable(MF_TASK_CCCH);
458 if (ccch_mode == CCCH_MODE_COMBINED)
459 mframe_enable(MF_TASK_CCCH_COMB);
460 else if (ccch_mode == CCCH_MODE_NON_COMBINED)
461 mframe_enable(MF_TASK_CCCH);
463 l1ctl_tx_ccch_mode_conf(ccch_mode);
466 /* Transmit a L1CTL_TCH_MODE_CONF */
467 static void l1ctl_tx_tch_mode_conf(uint8_t tch_mode)
469 struct msgb *msg = l1ctl_msgb_alloc(L1CTL_TCH_MODE_CONF);
470 struct l1ctl_tch_mode_conf *mode_conf;
471 mode_conf = (struct l1ctl_tch_mode_conf *)
472 msgb_put(msg, sizeof(*mode_conf));
473 mode_conf->tch_mode = tch_mode;
475 l1_queue_for_l2(msg);
478 /* receive a L1CTL_TCH_MODE_REQ from L23 */
479 static void l1ctl_rx_tch_mode_req(struct msgb *msg)
481 struct l1ctl_hdr *l1h = (struct l1ctl_hdr *) msg->data;
482 struct l1ctl_tch_mode_req *tch_mode_req =
483 (struct l1ctl_tch_mode_req *) l1h->data;
484 uint8_t tch_mode = tch_mode_req->tch_mode;
486 printd("L1CTL_TCH_MODE_REQ (mode=0x%02x)\n", tch_mode);
487 tch_mode = l1a_tch_mode_set(tch_mode);
489 audio_set_enabled(tch_mode != GSM48_CMODE_SIGN);
491 l1s.tch_sync = 1; /* Needed for audio to work */
493 l1ctl_tx_tch_mode_conf(tch_mode);
496 /* receive a L1CTL_NEIGH_PM_REQ from L23 */
497 static void l1ctl_rx_neigh_pm_req(struct msgb *msg)
499 struct l1ctl_hdr *l1h = (struct l1ctl_hdr *) msg->data;
500 struct l1ctl_neigh_pm_req *pm_req =
501 (struct l1ctl_neigh_pm_req *) l1h->data;
504 /* reset list in order to prevent race condition */
505 l1s.neigh_pm.n = 0; /* atomic */
506 l1s.neigh_pm.second = 0;
507 /* now reset pointer and fill list */
508 l1s.neigh_pm.pos = 0;
509 l1s.neigh_pm.running = 0;
510 for (i = 0; i < pm_req->n; i++)
511 l1s.neigh_pm.band_arfcn[i] = ntohs(pm_req->band_arfcn[i]);
512 printf("L1CTL_NEIGH_PM_REQ new list with %u entries\n", pm_req->n);
513 l1s.neigh_pm.n = pm_req->n; /* atomic */
515 /* on BCCH enable PM on frame 51 */
516 if (l1s.dedicated.type == GSM_DCHAN_NONE)
517 mframe_enable(MF_TASK_NEIGH_PM51);
520 /* callback from SERCOMM when L2 sends a message to L1 */
521 static void l1a_l23_rx_cb(uint8_t dlci, struct msgb *msg)
523 struct l1ctl_hdr *l1h = (struct l1ctl_hdr *) msg->data;
528 printf("l1a_l23_rx_cb (%u): ", msg->len);
529 for (i = 0; i < msg->len; i++)
530 printf("%02x ", msg->data[i]);
535 msg->l1h = msg->data;
537 if (sizeof(*l1h) > msg->len) {
538 printf("l1a_l23_cb: Short message. %u\n", msg->len);
542 switch (l1h->msg_type) {
544 l1ctl_rx_fbsb_req(msg);
546 case L1CTL_DM_EST_REQ:
547 l1ctl_rx_dm_est_req(msg);
549 case L1CTL_DM_REL_REQ:
550 l1ctl_rx_dm_rel_req(msg);
552 case L1CTL_PARAM_REQ:
553 l1ctl_rx_param_req(msg);
555 case L1CTL_DM_FREQ_REQ:
556 l1ctl_rx_dm_freq_req(msg);
558 case L1CTL_CRYPTO_REQ:
559 l1ctl_rx_crypto_req(msg);
562 l1ctl_rx_rach_req(msg);
565 l1ctl_rx_data_req(msg);
566 /* we have to keep the msgb, not free it! */
569 l1ctl_rx_pm_req(msg);
571 case L1CTL_RESET_REQ:
572 l1ctl_rx_reset_req(msg);
574 case L1CTL_CCCH_MODE_REQ:
575 l1ctl_rx_ccch_mode_req(msg);
577 case L1CTL_TCH_MODE_REQ:
578 l1ctl_rx_tch_mode_req(msg);
580 case L1CTL_NEIGH_PM_REQ:
581 l1ctl_rx_neigh_pm_req(msg);
591 void l1a_l23api_init(void)
593 sercomm_register_rx_cb(SC_DLCI_L1A_L23, l1a_l23_rx_cb);