Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 1 | /* 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 Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 30 | #include <openbsc/abis_nm.h> |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 31 | |
| 32 | struct 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 Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 50 | static 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 Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 58 | |
| 59 | /* Allocate a logical channel (TS) */ |
| 60 | struct 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 Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame^] | 66 | 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 Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 85 | struct gsm_bts_trx_ts *ts = &trx->ts[j]; |
| 86 | if (ts->pchan == GSM_PCHAN_NONE) { |
| 87 | ts->pchan = pchan; |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 88 | /* set channel attribute on OML */ |
| 89 | abis_nm_set_channel_attr(ts, chcomb4pchan[pchan]); |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 90 | return ts; |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | return NULL; |
| 95 | } |
| 96 | |
| 97 | /* Free a physical channel (TS) */ |
| 98 | void ts_free(struct gsm_bts_trx_ts *ts) |
| 99 | { |
| 100 | ts->pchan = GSM_PCHAN_NONE; |
| 101 | } |
| 102 | |
| 103 | static 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 | |
| 112 | static 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 */ |
| 147 | struct 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 */ |
| 174 | void 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 | } |