blob: 4ef5ff9026aec1bd86b784b7e15dc8c3ce7202de [file] [log] [blame]
Harald Welte8470bf22008-12-25 23:28:35 +00001/* GSM Channel allocation routines
2 *
3 * (C) 2008 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 <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <errno.h>
27
28#include <openbsc/gsm_data.h>
29#include <openbsc/chan_alloc.h>
Harald Welte4b634542008-12-27 01:55:51 +000030#include <openbsc/abis_nm.h>
Harald Welte8470bf22008-12-25 23:28:35 +000031
32struct gsm_bts_trx_ts *ts_c0_alloc(struct gsm_bts *bts,
33 enum gsm_phys_chan_config pchan)
34{
35 struct gsm_bts_trx *trx = &bts->trx[0];
36 struct gsm_bts_trx_ts *ts = &trx->ts[0];
37
38 if (pchan != GSM_PCHAN_CCCH &&
39 pchan != GSM_PCHAN_CCCH_SDCCH4)
40 return NULL;
41
42 if (ts->pchan != GSM_PCHAN_NONE)
43 return NULL;
44
45 ts->pchan = pchan;
46
47 return ts;
48}
49
Harald Welte4b634542008-12-27 01:55:51 +000050static const enum abis_nm_chan_comb chcomb4pchan[] = {
51 [GSM_PCHAN_CCCH] = NM_CHANC_mainBCCH,
52 [GSM_PCHAN_CCCH_SDCCH4] = NM_CHANC_BCCCHComb,
53 [GSM_PCHAN_TCH_F] = NM_CHANC_TCHFull,
54 [GSM_PCHAN_TCH_H] = NM_CHANC_TCHHalf,
55 [GSM_PCHAN_SDCCH8_SACCH8C] = NM_CHANC_SDCCH,
56 /* FIXME: bounds check */
57};
Harald Welte8470bf22008-12-25 23:28:35 +000058
59/* Allocate a logical channel (TS) */
60struct gsm_bts_trx_ts *ts_alloc(struct gsm_bts *bts,
61 enum gsm_phys_chan_config pchan)
62{
63 int i, j;
64 for (i = 0; i < bts->num_trx; i++) {
65 struct gsm_bts_trx *trx = &bts->trx[i];
66 for (j = 0; j < 8; j++) {
67 struct gsm_bts_trx_ts *ts = &trx->ts[j];
68 if (ts->pchan == GSM_PCHAN_NONE) {
69 ts->pchan = pchan;
Harald Welte4b634542008-12-27 01:55:51 +000070 /* set channel attribute on OML */
71 abis_nm_set_channel_attr(ts, chcomb4pchan[pchan]);
Harald Welte8470bf22008-12-25 23:28:35 +000072 return ts;
73 }
74 }
75 }
76 return NULL;
77}
78
79/* Free a physical channel (TS) */
80void ts_free(struct gsm_bts_trx_ts *ts)
81{
82 ts->pchan = GSM_PCHAN_NONE;
83}
84
85static const u_int8_t subslots_per_pchan[] = {
86 [GSM_PCHAN_NONE] = 0,
87 [GSM_PCHAN_CCCH] = 0,
88 [GSM_PCHAN_CCCH_SDCCH4] = 4,
89 [GSM_PCHAN_TCH_F] = 1,
90 [GSM_PCHAN_TCH_H] = 2,
91 [GSM_PCHAN_SDCCH8_SACCH8C] = 8.
92};
93
94static struct gsm_lchan *
95_lc_find(struct gsm_bts *bts, enum gsm_phys_chan_config pchan)
96{
97 struct gsm_bts_trx *trx;
98 struct gsm_bts_trx_ts *ts;
99 int i, j, ss;
100 for (i = 0; i < bts->num_trx; i++) {
101 trx = &bts->trx[i];
102 for (j = 0; j < 8; j++) {
103 ts = &trx->ts[j];
104 if (ts->pchan != pchan)
105 continue;
106 /* check if all sub-slots are allocated yet */
107 for (ss = 0; ss < subslots_per_pchan[pchan]; ss++) {
108 struct gsm_lchan *lc = &ts->lchan[ss];
109 if (lc->type == GSM_LCHAN_NONE)
110 return lc;
111 }
112 }
113 }
114 /* we cannot allocate more of these */
115 if (pchan == GSM_PCHAN_CCCH_SDCCH4)
116 return NULL;
117
118 /* if we've reached here, we need to allocate a new physical
119 * channel for the logical channel type requested */
120 ts = ts_alloc(bts, pchan);
121 if (!ts) {
122 /* no more radio resources */
123 return NULL;
124 }
125 return &ts->lchan[0];
126}
127
128/* Allocate a logical channel */
129struct gsm_lchan *lchan_alloc(struct gsm_bts *bts, enum gsm_chan_t type)
130{
131 struct gsm_lchan *lchan = NULL;
132
133 switch (type) {
134 case GSM_LCHAN_SDCCH:
135 lchan = _lc_find(bts, GSM_PCHAN_CCCH_SDCCH4);
136 if (lchan == NULL)
137 lchan = _lc_find(bts, GSM_PCHAN_SDCCH8_SACCH8C);
138 break;
139 case GSM_LCHAN_TCH_F:
140 lchan = _lc_find(bts, GSM_PCHAN_TCH_F);
141 break;
142 case GSM_LCHAN_TCH_H:
143 lchan =_lc_find(bts, GSM_PCHAN_TCH_H);
144 break;
145 default:
146 fprintf(stderr, "Unknown gsm_chan_t %u\n", type);
147 }
148
149 if (lchan)
150 lchan->type = type;
151
152 return lchan;
153}
154
155/* Free a logical channel */
156void lchan_free(struct gsm_lchan *lchan)
157{
158 lchan->type = GSM_LCHAN_NONE;
159 /* FIXME: ts_free() the timeslot, if we're the last logical
160 * channel using it */
161}