blob: 3ef8b4d2484b69ce6374a0a15f476446e16771f1 [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];
Harald Welte75a983f2008-12-27 21:34:06 +000066 int from, to;
67
68 /* the following constraints are pure policy,
69 * no requirement to put this restriction in place */
70 switch (pchan) {
71 case GSM_PCHAN_CCCH:
72 case GSM_PCHAN_CCCH_SDCCH4:
73 from = 0; to = 0;
74 break;
75 case GSM_PCHAN_SDCCH8_SACCH8C:
76 from = 1; to = 1;
77 break;
78 case GSM_PCHAN_TCH_F:
79 case GSM_PCHAN_TCH_H:
80 from = 2; to = 7;
81 break;
82 }
83
84 for (j = from; j <= to; j++) {
Harald Welte8470bf22008-12-25 23:28:35 +000085 struct gsm_bts_trx_ts *ts = &trx->ts[j];
86 if (ts->pchan == GSM_PCHAN_NONE) {
87 ts->pchan = pchan;
Harald Welte4b634542008-12-27 01:55:51 +000088 /* set channel attribute on OML */
89 abis_nm_set_channel_attr(ts, chcomb4pchan[pchan]);
Harald Welte8470bf22008-12-25 23:28:35 +000090 return ts;
91 }
92 }
93 }
94 return NULL;
95}
96
97/* Free a physical channel (TS) */
98void ts_free(struct gsm_bts_trx_ts *ts)
99{
100 ts->pchan = GSM_PCHAN_NONE;
101}
102
103static const u_int8_t subslots_per_pchan[] = {
104 [GSM_PCHAN_NONE] = 0,
105 [GSM_PCHAN_CCCH] = 0,
106 [GSM_PCHAN_CCCH_SDCCH4] = 4,
107 [GSM_PCHAN_TCH_F] = 1,
108 [GSM_PCHAN_TCH_H] = 2,
109 [GSM_PCHAN_SDCCH8_SACCH8C] = 8.
110};
111
112static struct gsm_lchan *
113_lc_find(struct gsm_bts *bts, enum gsm_phys_chan_config pchan)
114{
115 struct gsm_bts_trx *trx;
116 struct gsm_bts_trx_ts *ts;
117 int i, j, ss;
118 for (i = 0; i < bts->num_trx; i++) {
119 trx = &bts->trx[i];
120 for (j = 0; j < 8; j++) {
121 ts = &trx->ts[j];
122 if (ts->pchan != pchan)
123 continue;
124 /* check if all sub-slots are allocated yet */
125 for (ss = 0; ss < subslots_per_pchan[pchan]; ss++) {
126 struct gsm_lchan *lc = &ts->lchan[ss];
127 if (lc->type == GSM_LCHAN_NONE)
128 return lc;
129 }
130 }
131 }
132 /* we cannot allocate more of these */
133 if (pchan == GSM_PCHAN_CCCH_SDCCH4)
134 return NULL;
135
136 /* if we've reached here, we need to allocate a new physical
137 * channel for the logical channel type requested */
138 ts = ts_alloc(bts, pchan);
139 if (!ts) {
140 /* no more radio resources */
141 return NULL;
142 }
143 return &ts->lchan[0];
144}
145
146/* Allocate a logical channel */
147struct gsm_lchan *lchan_alloc(struct gsm_bts *bts, enum gsm_chan_t type)
148{
149 struct gsm_lchan *lchan = NULL;
150
151 switch (type) {
152 case GSM_LCHAN_SDCCH:
153 lchan = _lc_find(bts, GSM_PCHAN_CCCH_SDCCH4);
154 if (lchan == NULL)
155 lchan = _lc_find(bts, GSM_PCHAN_SDCCH8_SACCH8C);
156 break;
157 case GSM_LCHAN_TCH_F:
158 lchan = _lc_find(bts, GSM_PCHAN_TCH_F);
159 break;
160 case GSM_LCHAN_TCH_H:
161 lchan =_lc_find(bts, GSM_PCHAN_TCH_H);
162 break;
163 default:
164 fprintf(stderr, "Unknown gsm_chan_t %u\n", type);
165 }
166
167 if (lchan)
168 lchan->type = type;
169
170 return lchan;
171}
172
173/* Free a logical channel */
174void lchan_free(struct gsm_lchan *lchan)
175{
176 lchan->type = GSM_LCHAN_NONE;
177 /* FIXME: ts_free() the timeslot, if we're the last logical
178 * channel using it */
179}