[layer1] Added mf_off to shedule next RACH request.
[osmocom-bb.git] / src / target / firmware / layer1 / l23_api.c
index d37d18d..146242e 100644 (file)
@@ -24,6 +24,7 @@
 
 #include <stdint.h>
 #include <stdio.h>
+#include <string.h>
 
 #include <debug.h>
 #include <byteorder.h>
@@ -79,6 +80,22 @@ static enum mframe_task chan_nr2mf_task(uint8_t chan_nr)
        return 0;
 }
 
+static int  chan_nr2dchan_type(uint8_t chan_nr)
+{
+       uint8_t cbits = chan_nr >> 3;
+
+       if (cbits == 0x01) {
+               return GSM_DCHAN_TCH_F;
+       } else if ((cbits & 0x1e) == 0x02) {
+               return GSM_DCHAN_TCH_H;
+       } else if ((cbits & 0x1c) == 0x04) {
+               return GSM_DCHAN_SDCCH_4;
+       } else if ((cbits & 0x18) == 0x08) {
+               return GSM_DCHAN_SDCCH_8;
+       }
+       return GSM_DCHAN_UNKNOWN;
+}
+
 struct msgb *l1ctl_msgb_alloc(uint8_t msg_type)
 {
        struct msgb *msg;
@@ -96,7 +113,7 @@ struct msgb *l1ctl_msgb_alloc(uint8_t msg_type)
        l1h->msg_type = msg_type;
        l1h->flags = 0;
 
-       msg->l1h = l1h;
+       msg->l1h = (uint8_t *)l1h;
 
        return msg;
 }
@@ -115,8 +132,119 @@ struct msgb *l1_create_l2_msg(int msg_type, uint32_t fn, uint16_t snr,
        return msg;
 }
 
+/* receive a L1CTL_FBSB_REQ from L23 */
+static void l1ctl_rx_fbsb_req(struct msgb *msg)
+{
+       struct l1ctl_hdr *l1h = (struct l1ctl_hdr *) msg->data;
+       struct l1ctl_fbsb_req *sync_req = (struct l1ctl_fbsb_req *) l1h->data;
+
+       if (sizeof(*sync_req) > msg->len) {
+               printf("Short sync msg. %u\n", msg->len);
+               return;
+       }
+
+       printd("L1CTL_FBSB_REQ (arfcn=%u, flags=0x%x)\n",
+               ntohs(sync_req->band_arfcn), sync_req->flags);
+
+       /* reset scheduler and hardware */
+       l1s_reset();
+
+       /* pre-set the CCCH mode */
+       l1s.serving_cell.ccch_mode = sync_req->ccch_mode;
+
+       printd("Starting FCCH Recognition\n");
+       l1s_fbsb_req(1, sync_req);
+}
+
+/* receive a L1CTL_DM_EST_REQ from L23 */
+static void l1ctl_rx_dm_est_req(struct msgb *msg)
+{
+       struct l1ctl_hdr *l1h = (struct l1ctl_hdr *) msg->data;
+       struct l1ctl_info_ul *ul = (struct l1ctl_info_ul *) l1h->data;
+       struct l1ctl_dm_est_req *est_req = (struct l1ctl_dm_est_req *) ul->payload;
+
+       printd("L1CTL_DM_EST_REQ (arfcn=%u, chan_nr=0x%02x, tsc=%u)\n",
+               ntohs(est_req->h0.band_arfcn), ul->chan_nr, est_req->tsc);
+
+       /* Current limitations */
+       if ((ul->chan_nr & 0x7) > 4) {
+               /* FIXME: Timeslot */
+               puts("We don't support TS > 4 yet\n");
+               return;
+       }
+
+       if ((chan_nr2mf_task(ul->chan_nr) >= MF_TASK_SDCCH8_4) &&
+           (chan_nr2mf_task(ul->chan_nr) <= MF_TASK_SDCCH8_7)) {
+               /* FIXME: TX while RX prevents SDCCH8 [4..7] */
+               puts("We don't support SDCCH8 [4..7] yet\n");
+               return;
+       }
+
+       /* configure dedicated channel state */
+       l1s.dedicated.type = chan_nr2dchan_type(ul->chan_nr);
+       l1s.dedicated.tsc  = est_req->tsc;
+       l1s.dedicated.tn   = ul->chan_nr & 0x7;
+       l1s.dedicated.h    = est_req->h;
+
+       if (est_req->h) {
+               int i;
+               l1s.dedicated.h1.hsn  = est_req->h1.hsn;
+               l1s.dedicated.h1.maio = est_req->h1.maio;
+               l1s.dedicated.h1.n    = est_req->h1.n;
+               for (i=0; i<est_req->h1.n; i++)
+                       l1s.dedicated.h1.ma[i] = ntohs(est_req->h1.ma[i]);
+       } else {
+               l1s.dedicated.h0.arfcn = ntohs(est_req->h0.band_arfcn);
+       }
+
+       /* figure out which MF tasks to enable */
+       l1a_mftask_set(1 << chan_nr2mf_task(ul->chan_nr));
+}
+
+/* receive a L1CTL_DM_REL_REQ from L23 */
+static void l1ctl_rx_dm_rel_req(struct msgb *msg)
+{
+       struct l1ctl_hdr *l1h = (struct l1ctl_hdr *) msg->data;
+       struct l1ctl_info_ul *ul = (struct l1ctl_info_ul *) l1h->data;
+
+       l1a_mftask_set(0);
+}
+
+/* receive a L1CTL_RACH_REQ from L23 */
+static void l1ctl_rx_rach_req(struct msgb *msg)
+{
+       struct l1ctl_hdr *l1h = (struct l1ctl_hdr *) msg->data;
+       struct l1ctl_info_ul *ul = (struct l1ctl_info_ul *) l1h->data;
+       struct l1ctl_rach_req *rach_req = (struct l1ctl_rach_req *) ul->payload;
+
+       printd("L1CTL_RACH_REQ (ra=0x%02x, fn51=%d, mf_off=%d)\n", rach_req->ra, rach_req->fn51, rach_req->mf_off);
+
+       l1a_rach_req(rach_req->fn51, rach_req->mf_off, rach_req->ra);
+}
+
+/* receive a L1CTL_DATA_REQ from L23 */
+static void l1ctl_rx_data_req(struct msgb *msg)
+{
+       struct l1ctl_hdr *l1h = (struct l1ctl_hdr *) msg->data;
+       struct l1ctl_info_ul *ul = (struct l1ctl_info_ul *) l1h->data;
+       struct l1ctl_data_ind *data_ind = (struct l1ctl_data_ind *) ul->payload;
+       struct llist_head *tx_queue;
+
+       printd("L1CTL_DATA_REQ (link_id=0x%02x)\n", ul->link_id);
+
+       msg->l3h = data_ind->data;
+       tx_queue = (ul->link_id & 0x40) ?
+                       &l1s.tx_queue[L1S_CHAN_SACCH] :
+                       &l1s.tx_queue[L1S_CHAN_MAIN];
+
+       printd("ul=%p, ul->payload=%p, data_ind=%p, data_ind->data=%p l3h=%p\n",
+               ul, ul->payload, data_ind, data_ind->data, msg->l3h);
+
+       l1a_txq_msgb_enq(tx_queue, msg);
+}
+
 /* receive a L1CTL_PM_REQ from L23 */
-void l1ctl_rx_pm_req(struct msgb *msg)
+static void l1ctl_rx_pm_req(struct msgb *msg)
 {
        struct l1ctl_hdr *l1h = (struct l1ctl_hdr *) msg->data;
        struct l1ctl_pm_req *pm_req = (struct l1ctl_pm_req *) l1h->data;
@@ -138,16 +266,78 @@ void l1ctl_rx_pm_req(struct msgb *msg)
        l1s_pm_test(1, l1s.pm.range.arfcn_next);
 }
 
+/* Transmit a L1CTL_RESET_IND or L1CTL_RESET_CONF */
+void l1ctl_tx_reset(uint8_t msg_type, uint8_t reset_type)
+{
+       struct msgb *msg = l1ctl_msgb_alloc(msg_type);
+       struct l1ctl_reset *reset_resp;
+       reset_resp = (struct l1ctl_reset *)
+                               msgb_put(msg, sizeof(*reset_resp));
+       reset_resp->type = reset_type;
+
+       l1_queue_for_l2(msg);
+}
+
+/* receive a L1CTL_RESET_REQ from L23 */
+static void l1ctl_rx_reset_req(struct msgb *msg)
+{
+       struct l1ctl_hdr *l1h = (struct l1ctl_hdr *) msg->data;
+       struct l1ctl_reset *reset_req =
+                               (struct l1ctl_reset *) l1h->data;
+
+       switch (reset_req->type) {
+       case L1CTL_RES_T_FULL:
+               printf("L1CTL_RESET_REQ: FULL!\n");
+               l1s_reset();
+               l1s_reset_hw();
+               l1ctl_tx_reset(L1CTL_RESET_CONF, reset_req->type);
+               break;
+       default:
+               printf("unknown L1CTL_RESET_REQ type\n");
+               break;
+       }
+}
+
+/* Transmit a L1CTL_CCCH_MODE_CONF */
+static void l1ctl_tx_ccch_mode_conf(uint8_t ccch_mode)
+{
+       struct msgb *msg = l1ctl_msgb_alloc(L1CTL_CCCH_MODE_CONF);
+       struct l1ctl_ccch_mode_conf *mode_conf;
+       mode_conf = (struct l1ctl_ccch_mode_conf *)
+                               msgb_put(msg, sizeof(*mode_conf));
+       mode_conf->ccch_mode = ccch_mode;
+
+       l1_queue_for_l2(msg);
+}
+
+/* receive a L1CTL_CCCH_MODE_REQ from L23 */
+static void l1ctl_rx_ccch_mode_req(struct msgb *msg)
+{
+       struct l1ctl_hdr *l1h = (struct l1ctl_hdr *) msg->data;
+       struct l1ctl_ccch_mode_req *ccch_mode_req =
+               (struct l1ctl_ccch_mode_req *) l1h->data;
+       uint8_t ccch_mode = ccch_mode_req->ccch_mode;
+
+       /* pre-set the CCCH mode */
+       l1s.serving_cell.ccch_mode = ccch_mode;
+
+       /* Update task */
+       mframe_disable(MF_TASK_CCCH_COMB);
+       mframe_disable(MF_TASK_CCCH);
+
+       if (ccch_mode == CCCH_MODE_COMBINED)
+               mframe_enable(MF_TASK_CCCH_COMB);
+       else if (ccch_mode == CCCH_MODE_NON_COMBINED)
+               mframe_enable(MF_TASK_CCCH);
+
+       l1ctl_tx_ccch_mode_conf(ccch_mode);
+}
+
 /* callback from SERCOMM when L2 sends a message to L1 */
 static void l1a_l23_rx_cb(uint8_t dlci, struct msgb *msg)
 {
        struct l1ctl_hdr *l1h = (struct l1ctl_hdr *) msg->data;
-       struct l1ctl_info_ul *ul = (struct l1ctl_info_ul *) l1h->data;
-       struct l1ctl_fbsb_req *sync_req;
-       struct l1ctl_rach_req *rach_req;
-       struct l1ctl_dm_est_req *est_req;
-       struct l1ctl_data_ind *data_ind;
-       struct llist_head *tx_queue;
+
 #if 0
        {
                int i;
@@ -157,6 +347,7 @@ static void l1a_l23_rx_cb(uint8_t dlci, struct msgb *msg)
                puts("\n");
        }
 #endif
+
        msg->l1h = msg->data;
 
        if (sizeof(*l1h) > msg->len) {
@@ -166,68 +357,30 @@ static void l1a_l23_rx_cb(uint8_t dlci, struct msgb *msg)
 
        switch (l1h->msg_type) {
        case L1CTL_FBSB_REQ:
-               if (sizeof(*sync_req) > msg->len) {
-                       printf("Short sync msg. %u\n", msg->len);
-                       break;
-               }
-
-               sync_req = (struct l1ctl_fbsb_req *) l1h->data;
-               printd("L1CTL_FBSB_REQ (arfcn=%u, flags=0x%x)\n",
-                       ntohs(sync_req->band_arfcn), sync_req->flags);
-
-               /* reset scheduler and hardware */
-               l1s_reset();
-
-               /* tune to specified frequency */
-               trf6151_rx_window(0, ntohs(sync_req->band_arfcn), 40, 0);
-               tpu_end_scenario();
-
-               printd("Starting FCCH Recognition\n");
-               l1s_fbsb_req(1, sync_req);
+               l1ctl_rx_fbsb_req(msg);
                break;
        case L1CTL_DM_EST_REQ:
-               est_req = (struct l1ctl_dm_est_req *) ul->payload;
-               printd("L1CTL_DM_EST_REQ (arfcn=%u, chan_nr=0x%02x)\n",
-                       ntohs(est_req->band_arfcn), ul->chan_nr);
-               if (ntohs(est_req->band_arfcn) != l1s.serving_cell.arfcn) {
-                       /* FIXME: ARFCN */
-                       puts("We don't support ARFCN switches yet\n");
-                       break;
-               }
-               if (ul->chan_nr & 0x7) {
-                       /* FIXME: Timeslot */
-                       puts("We don't support non-0 TS yet\n");
-                       break;
-               }
-               if (est_req->h0.h) {
-                       puts("We don't support frequency hopping yet\n");
-                       break;
-               }
-               /* FIXME: set TSC of ded chan according to est_req.h0.tsc */
-               /* figure out which MF tasks to enable */
-               l1a_mftask_set(1 << chan_nr2mf_task(ul->chan_nr));
+               l1ctl_rx_dm_est_req(msg);
+               break;
+       case L1CTL_DM_REL_REQ:
+               l1ctl_rx_dm_rel_req(msg);
                break;
        case L1CTL_RACH_REQ:
-               rach_req = (struct l1ctl_rach_req *) ul->payload;
-               printd("L1CTL_RACH_REQ (ra=0x%02x)\n", rach_req->ra);
-               l1a_rach_req(27, rach_req->ra);
+               l1ctl_rx_rach_req(msg);
                break;
        case L1CTL_DATA_REQ:
-               data_ind = (struct l1ctl_data_ind *) ul->payload;
-               printd("L1CTL_DATA_REQ (link_id=0x%02x)\n", ul->link_id);
-               if (ul->link_id & 0x40)
-                       tx_queue = &l1s.tx_queue[L1S_CHAN_SACCH];
-               else
-                       tx_queue = &l1s.tx_queue[L1S_CHAN_MAIN];
-               msg->l3h = data_ind->data;
-               printd("ul=%p, ul->payload=%p, data_ind=%p, data_ind->data=%p l3h=%p\n",
-                       ul, ul->payload, data_ind, data_ind->data, msg->l3h);
-               l1a_txq_msgb_enq(tx_queue, msg);
+               l1ctl_rx_data_req(msg);
                /* we have to keep the msgb, not free it! */
                goto exit_nofree;
        case L1CTL_PM_REQ:
                l1ctl_rx_pm_req(msg);
                break;
+       case L1CTL_RESET_REQ:
+               l1ctl_rx_reset_req(msg);
+               break;
+       case L1CTL_CCCH_MODE_REQ:
+               l1ctl_rx_ccch_mode_req(msg);
+               break;
        }
 
 exit_msgbfree: