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.
29 #include <comm/msgb.h>
30 #include <comm/sercomm.h>
32 #include <layer1/sync.h>
33 #include <layer1/async.h>
34 #include <layer1/mframe_sched.h>
35 #include <layer1/tpu_window.h>
37 #include <rf/trf6151.h>
39 #include <l1a_l23_interface.h>
41 /* the size we will allocate struct msgb* for HDLC */
42 #define L3_MSG_SIZE (sizeof(struct l1ctl_data_ind) + 4)
45 void l1_queue_for_l2(struct msgb *msg)
47 /* forward via serial for now */
48 sercomm_sendmsg(SC_DLCI_L1A_L23, msg);
51 static enum mframe_task chan_nr2mf_task(uint8_t chan_nr)
53 uint8_t cbits = chan_nr >> 3;
59 } else if ((cbits & 0x1e) == 0x02) {
60 lch_idx = cbits & 0x1;
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;
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;
80 struct msgb *l1_create_l2_msg(int msg_type, uint32_t fn, uint16_t snr,
83 struct l1ctl_info_dl *dl;
86 msg = msgb_alloc_headroom(L3_MSG_SIZE, L3_MSG_HEAD, "l1_burst");
89 puts("OOPS. Out of buffers...\n");
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);
100 dl->band_arfcn = arfcn;
105 /* callbakc from SERCOMM when L2 sends a message to L1 */
106 static void l1a_l23_rx_cb(uint8_t dlci, struct msgb *msg)
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;
117 puts("l1a_l23_rx_cb: ");
118 for (i = 0; i < msg->len; i++)
119 printf("%02x ", msg->data[i]);
123 if (sizeof(*ul) > msg->len) {
124 printf("l1a_l23_cb: Short message. %u\n", msg->len);
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);
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);
138 /* reset scheduler and hardware */
142 /* tune to specified frequency */
143 trf6151_rx_window(0, sync_req->band_arfcn, 40, 0);
146 printd("Starting FCCH Recognition\n");
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) {
155 puts("We don't support ARFCN switches yet\n");
158 if (ul->chan_nr & 0x7) {
159 /* FIXME: Timeslot */
160 puts("We don't support non-0 TS yet\n");
164 puts("We don't support frequency hopping yet\n");
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));
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);
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];
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! */
198 void l1a_l23api_init(void)
200 sercomm_register_rx_cb(SC_DLCI_L1A_L23, l1a_l23_rx_cb);