summaryrefslogtreecommitdiffstats
path: root/src/host/trxcon/src/trxcon_fsm.c
blob: 186c04bcc08ebfb098220347ffa9ea9dd894d2d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
/*
 * OsmocomBB <-> SDR connection bridge
 * The trxcon state machine (see 3GPP TS 44.004, section 5.1)
 *
 * (C) 2022 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
 * Author: Vadim Yanitskiy <vyanitskiy@sysmocom.de>
 *
 * All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */

#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>

#include <osmocom/core/fsm.h>
#include <osmocom/core/msgb.h>
#include <osmocom/core/talloc.h>
#include <osmocom/core/logging.h>
#include <osmocom/gsm/gsm0502.h>

#include <osmocom/bb/trxcon/trxcon.h>
#include <osmocom/bb/trxcon/trxcon_fsm.h>
#include <osmocom/bb/trxcon/phyif.h>
#include <osmocom/bb/trxcon/l1ctl.h>
#include <osmocom/bb/l1sched/l1sched.h>

#define S(x)	(1 << (x))

static void trxcon_allstate_action(struct osmo_fsm_inst *fi,
				   uint32_t event, void *data)
{
	struct trxcon_inst *trxcon = fi->priv;
	struct trxcon_phyif_cmd phycmd = { };

	switch (event) {
	case TRXCON_EV_PHYIF_FAILURE:
		trxcon->phyif = NULL;
		osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
		break;
	case TRXCON_EV_L2IF_FAILURE:
		trxcon->l2if = NULL;
		osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL);
		break;
	case TRXCON_EV_RESET_FULL_REQ:
		TALLOC_FREE(trxcon->fi_data);
		if (fi->state != TRXCON_ST_RESET)
			osmo_fsm_inst_state_chg(fi, TRXCON_ST_RESET, 0, 0);
		l1sched_reset(trxcon->sched, true);

		/* Reset the L1 parameters */
		trxcon->l1p.band_arfcn = 0xffff;
		trxcon->l1p.tx_power = 0;
		trxcon->l1p.ta = 0;

		phycmd.type = TRXCON_PHYIF_CMDT_RESET;
		trxcon_phyif_handle_cmd(trxcon->phyif, &phycmd);
		break;
	case TRXCON_EV_RESET_SCHED_REQ:
		l1sched_reset(trxcon->sched, false);
		break;
	case TRXCON_EV_SET_PHY_CONFIG_REQ:
	{
		const struct trxcon_param_set_phy_config_req *req = data;

		switch (req->type) {
		case TRXCON_PHY_CFGT_PCHAN_COMB:
			phycmd.type = TRXCON_PHYIF_CMDT_SETSLOT;
			phycmd.param.setslot.tn = req->pchan_comb.tn;
			phycmd.param.setslot.pchan = req->pchan_comb.pchan;
			trxcon_phyif_handle_cmd(trxcon->phyif, &phycmd);
			break;
		case TRXCON_PHY_CFGT_TX_PARAMS:
			if (trxcon->l1p.ta != req->tx_params.timing_advance) {
				phycmd.type = TRXCON_PHYIF_CMDT_SETTA;
				phycmd.param.setta.ta = req->tx_params.timing_advance;
				trxcon_phyif_handle_cmd(trxcon->phyif, &phycmd);
			}
			trxcon->l1p.tx_power = req->tx_params.tx_power;
			trxcon->l1p.ta = req->tx_params.timing_advance;
			break;
		}
		break;
	}
	case TRXCON_EV_UPDATE_SACCH_CACHE_REQ:
	{
		const struct trxcon_param_tx_data_req *req = data;

		if (req->link_id != L1SCHED_CH_LID_SACCH) {
			LOGPFSML(fi, LOGL_ERROR, "Unexpected link_id=0x%02x\n", req->link_id);
			break;
		}
		if (req->data_len != GSM_MACBLOCK_LEN) {
			LOGPFSML(fi, LOGL_ERROR, "Unexpected data length=%zu\n", req->data_len);
			break;
		}

		memcpy(&trxcon->sched->sacch_cache[0], req->data, req->data_len);
		break;
	}
	default:
		OSMO_ASSERT(0);
	}
}

static int trxcon_timer_cb(struct osmo_fsm_inst *fi)
{
	struct trxcon_inst *trxcon = fi->priv;

	switch (fi->state) {
	case TRXCON_ST_FBSB_SEARCH:
		l1ctl_tx_fbsb_fail(trxcon, trxcon->l1p.band_arfcn);
		osmo_fsm_inst_state_chg(fi, TRXCON_ST_RESET, 0, 0);
		return 0;
	default:
		OSMO_ASSERT(0);
	}
}

static void handle_full_power_scan_req(struct osmo_fsm_inst *fi,
				       const struct trxcon_param_full_power_scan_req *req)
{
	struct trxcon_inst *trxcon = fi->priv;
	enum gsm_band band_start, band_stop;

	if (trxcon->fi_data != NULL) {
		LOGPFSML(fi, LOGL_ERROR, "Full power scan is already in progress\n");
		return;
	}

	/* The start and stop ARFCNs must be in the same band */
	if (gsm_arfcn2band_rc(req->band_arfcn_start, &band_start) != 0 ||
	    gsm_arfcn2band_rc(req->band_arfcn_stop, &band_stop) != 0 ||
	    band_start != band_stop) {
		LOGPFSML(fi, LOGL_ERROR, "Full power scan request has invalid params\n");
		return;
	}

	trxcon->fi_data = talloc_memdup(fi, req, sizeof(*req));
	OSMO_ASSERT(trxcon->fi_data != NULL);

	/* trxcon_st_full_power_scan_onenter() sends the initial TRXCON_PHYIF_CMDT_MEASURE */
	osmo_fsm_inst_state_chg(fi, TRXCON_ST_FULL_POWER_SCAN, 0, 0); /* TODO: timeout */
}

static void trxcon_st_reset_action(struct osmo_fsm_inst *fi,
				   uint32_t event, void *data)
{
	struct trxcon_inst *trxcon = fi->priv;

	switch (event) {
	case TRXCON_EV_FBSB_SEARCH_REQ:
	{
		const struct trxcon_param_fbsb_search_req *req = data;
		unsigned long timeout_fns, timeout_ms;

		/* Some PHYs need additional time to tune (in TDMA FNs) */
		timeout_fns = req->timeout_fns + trxcon->phy_quirks.fbsb_extend_fns;
		timeout_ms = timeout_fns * GSM_TDMA_FN_DURATION_uS / 1000;
		osmo_fsm_inst_state_chg_ms(fi, TRXCON_ST_FBSB_SEARCH, timeout_ms, 0);

		l1sched_configure_ts(trxcon->sched, 0, req->pchan_config);

		/* Only if current ARFCN differs */
		if (trxcon->l1p.band_arfcn != req->band_arfcn) {
			const struct trxcon_phyif_cmd phycmd = {
				.type = TRXCON_PHYIF_CMDT_SETFREQ_H0,
				.param.setfreq_h0 = {
					.band_arfcn = req->band_arfcn,
				},
			};

			/* Update current ARFCN */
			trxcon->l1p.band_arfcn = req->band_arfcn;

			/* Tune transceiver to required ARFCN */
			trxcon_phyif_handle_cmd(trxcon->phyif, &phycmd);
		}

		const struct trxcon_phyif_cmd phycmd = { .type = TRXCON_PHYIF_CMDT_POWERON };
		trxcon_phyif_handle_cmd(trxcon->phyif, &phycmd);
		break;
	}
	case TRXCON_EV_FULL_POWER_SCAN_REQ:
		handle_full_power_scan_req(fi, (const struct trxcon_param_full_power_scan_req *)data);
		break;
	default:
		OSMO_ASSERT(0);
	}
}

static void trxcon_st_full_power_scan_onenter(struct osmo_fsm_inst *fi,
					      uint32_t prev_state)
{
	const struct trxcon_inst *trxcon = fi->priv;
	const struct trxcon_param_full_power_scan_req *req = trxcon->fi_data;

	/* req->band_arfcn_start holds the current ARFCN */
	const struct trxcon_phyif_cmd phycmd = {
		.type = TRXCON_PHYIF_CMDT_MEASURE,
		.param.measure = {
			.band_arfcn = req->band_arfcn_start,
		},
	};

	trxcon_phyif_handle_cmd(trxcon->phyif, &phycmd);
}

static void trxcon_st_full_power_scan_action(struct osmo_fsm_inst *fi,
					     uint32_t event, void *data)
{
	struct trxcon_inst *trxcon = fi->priv;

	switch (event) {
	case TRXCON_EV_FULL_POWER_SCAN_RES:
	{
		struct trxcon_param_full_power_scan_req *req = trxcon->fi_data;
		const struct trxcon_param_full_power_scan_res *res = data;

		if (req == NULL) {
			LOGPFSML(fi, LOGL_ERROR, "Rx unexpected power scan result\n");
			break;
		}

		/* req->band_arfcn_start holds the expected ARFCN */
		if (res->band_arfcn != req->band_arfcn_start) {
			LOGPFSML(fi, LOGL_ERROR, "Rx power scan result "
				 "with unexpected ARFCN %u (expected %u)\n",
				 res->band_arfcn & ~ARFCN_FLAG_MASK,
				 req->band_arfcn_start & ~ARFCN_FLAG_MASK);
			break;
		}

		if (res->band_arfcn < req->band_arfcn_stop) {
			l1ctl_tx_pm_conf(trxcon, res->band_arfcn, res->dbm, false);
			/* trxcon_st_full_power_scan_onenter() sends the next TRXCON_PHYIF_CMDT_MEASURE */
			req->band_arfcn_start = res->band_arfcn + 1;
			osmo_fsm_inst_state_chg(fi, TRXCON_ST_FULL_POWER_SCAN, 0, 0); /* TODO: timeout */
		} else {
			l1ctl_tx_pm_conf(trxcon, res->band_arfcn, res->dbm, true);
			LOGPFSML(fi, LOGL_INFO, "Full power scan completed\n");
			TALLOC_FREE(trxcon->fi_data);
		}
		break;
	}
	case TRXCON_EV_FULL_POWER_SCAN_REQ:
		handle_full_power_scan_req(fi, (const struct trxcon_param_full_power_scan_req *)data);
		break;
	default:
		OSMO_ASSERT(0);
	}
}

static void trxcon_st_fbsb_search_action(struct osmo_fsm_inst *fi,
					 uint32_t event, void *data)
{
	struct trxcon_inst *trxcon = fi->priv;

	switch (event) {
	case TRXCON_EV_FBSB_SEARCH_RES:
		osmo_fsm_inst_state_chg(fi, TRXCON_ST_BCCH_CCCH, 0, 0);
		l1ctl_tx_fbsb_conf(trxcon, trxcon->l1p.band_arfcn, trxcon->sched->bsic);
		break;
	default:
		OSMO_ASSERT(0);
	}
}

static void handle_tx_access_burst_req(struct osmo_fsm_inst *fi,
				       const struct trxcon_param_tx_access_burst_req *req)
{
	struct trxcon_inst *trxcon = fi->priv;
	enum l1sched_ts_prim_type prim_type;
	const struct l1sched_ts_prim *prim;

	const struct l1sched_ts_prim_rach rach = {
		.synch_seq = req->synch_seq,
		.offset = req->offset,
		.ra = req->ra,
	};

	prim_type = req->is_11bit ? L1SCHED_PRIM_RACH11 : L1SCHED_PRIM_RACH8;
	prim = l1sched_prim_push(trxcon->sched, prim_type,
				 req->chan_nr, req->link_id,
				 (const uint8_t *)&rach, sizeof(rach));
	if (prim == NULL)
		LOGPFSML(fi, LOGL_ERROR, "Failed to enqueue a prim\n");
}

static void handle_dch_est_req(struct osmo_fsm_inst *fi,
			       const struct trxcon_param_dch_est_req *req)
{
	struct trxcon_inst *trxcon = fi->priv;
	enum gsm_phys_chan_config config;
	struct l1sched_ts *ts;
	int rc;

	config = l1sched_chan_nr2pchan_config(req->chan_nr);
	if (config == GSM_PCHAN_NONE) {
		LOGPFSML(fi, LOGL_ERROR, "Failed to determine channel config\n");
		return;
	}

	if (req->hopping) {
		const struct trxcon_phyif_cmd phycmd = {
			.type = TRXCON_PHYIF_CMDT_SETFREQ_H1,
			.param.setfreq_h1 = {
				.hsn = req->h1.hsn,
				.maio = req->h1.maio,
				.ma = &req->h1.ma[0],
				.ma_len = req->h1.n,
			},
		};

		/* Apply the freq. hopping parameters */
		if (trxcon_phyif_handle_cmd(trxcon->phyif, &phycmd) != 0)
			return;

		/* Set current ARFCN to an invalid value */
		trxcon->l1p.band_arfcn = 0xffff;
	} else {
		const struct trxcon_phyif_cmd phycmd = {
			.type = TRXCON_PHYIF_CMDT_SETFREQ_H0,
			.param.setfreq_h0 = {
				.band_arfcn = req->h0.band_arfcn,
			},
		};

		/* Tune transceiver to required ARFCN */
		if (trxcon_phyif_handle_cmd(trxcon->phyif, &phycmd) != 0)
			return;

		/* Update current ARFCN */
		trxcon->l1p.band_arfcn = req->h0.band_arfcn;
	}

	/* Remove all active timeslots */
	l1sched_reset(trxcon->sched, false);

	rc = l1sched_configure_ts(trxcon->sched, req->chan_nr & 0x07, config);
	if (rc)
		return;
	ts = trxcon->sched->ts[req->chan_nr & 0x07];
	OSMO_ASSERT(ts != NULL);

	l1sched_deactivate_all_lchans(ts);

	/* Activate only requested lchans */
	rc = l1sched_set_lchans(ts, req->chan_nr, 1, req->tch_mode, req->tsc);
	if (rc) {
		LOGPFSML(fi, LOGL_ERROR, "Failed to activate requested lchans\n");
		return;
	}

	if (config == GSM_PCHAN_PDCH)
		osmo_fsm_inst_state_chg(fi, TRXCON_ST_PACKET_DATA, 0, 0);
	else
		osmo_fsm_inst_state_chg(fi, TRXCON_ST_DEDICATED, 0, 0);
}

static void trxcon_st_bcch_ccch_action(struct osmo_fsm_inst *fi,
				       uint32_t event, void *data)
{
	struct trxcon_inst *trxcon = fi->priv;

	switch (event) {
	case TRXCON_EV_TX_ACCESS_BURST_REQ:
		handle_tx_access_burst_req(fi, data);
		break;
	case TRXCON_EV_TX_ACCESS_BURST_CNF:
		l1ctl_tx_rach_conf(trxcon, (const struct trxcon_param_tx_access_burst_cnf *)data);
		break;
	case TRXCON_EV_SET_CCCH_MODE_REQ:
	{
		struct trxcon_param_set_ccch_tch_mode_req *req = data;
		enum gsm_phys_chan_config chan_config = req->mode;
		struct l1sched_ts *ts = trxcon->sched->ts[0];

		/* Make sure that TS0 is allocated and configured */
		if (ts == NULL || ts->mf_layout == NULL) {
			LOGPFSML(fi, LOGL_ERROR, "TS0 is not configured\n");
			return;
		}

		/* Do nothing if the current mode matches required */
		if (ts->mf_layout->chan_config != chan_config)
			l1sched_configure_ts(trxcon->sched, 0, chan_config);
		req->applied = true;
		break;
	}
	case TRXCON_EV_DCH_EST_REQ:
		handle_dch_est_req(fi, (const struct trxcon_param_dch_est_req *)data);
		break;
	case TRXCON_EV_RX_DATA_IND:
		l1ctl_tx_dt_ind(trxcon, (const struct trxcon_param_rx_data_ind *)data);
		break;
	default:
		OSMO_ASSERT(0);
	}
}

static void trxcon_st_dedicated_action(struct osmo_fsm_inst *fi,
				       uint32_t event, void *data)
{
	struct trxcon_inst *trxcon = fi->priv;

	switch (event) {
	case TRXCON_EV_TX_ACCESS_BURST_REQ:
		handle_tx_access_burst_req(fi, data);
		break;
	case TRXCON_EV_TX_ACCESS_BURST_CNF:
		l1ctl_tx_rach_conf(trxcon, (const struct trxcon_param_tx_access_burst_cnf *)data);
		break;
	case TRXCON_EV_DCH_EST_REQ:
		handle_dch_est_req(fi, (const struct trxcon_param_dch_est_req *)data);
		break;
	case TRXCON_EV_DCH_REL_REQ:
		l1sched_reset(trxcon->sched, false);
		/* TODO: switch to (not implemented) TRXCON_ST_DCH_TUNING? */
		break;
	case TRXCON_EV_SET_TCH_MODE_REQ:
	{
		struct trxcon_param_set_ccch_tch_mode_req *req = data;
		unsigned int tn;

		/* Iterate over timeslot list */
		for (tn = 0; tn < ARRAY_SIZE(trxcon->sched->ts); tn++) {
			struct l1sched_ts *ts = trxcon->sched->ts[tn];
			struct l1sched_lchan_state *lchan;

			/* Timeslot is not allocated */
			if (ts == NULL || ts->mf_layout == NULL)
				continue;

			/* Iterate over all allocated lchans */
			llist_for_each_entry(lchan, &ts->lchans, list) {
				/* Omit inactive channels */
				if (!lchan->active)
					continue;
				if (req->mode == GSM48_CMODE_SPEECH_AMR) {
					int rc = l1sched_lchan_set_amr_cfg(lchan,
									   req->amr.codecs_bitmask,
									   req->amr.start_codec);
					if (rc)
						continue;
				}
				lchan->tch_mode = req->mode;
				req->applied = true;
			}
		}
		break;
	}
	case TRXCON_EV_CRYPTO_REQ:
	{
		const struct trxcon_param_crypto_req *req = data;
		unsigned int tn = req->chan_nr & 0x07;
		struct l1sched_ts *ts;

		/* Make sure that required TS is allocated and configured */
		ts = trxcon->sched->ts[tn];
		if (ts == NULL || ts->mf_layout == NULL) {
			LOGPFSML(fi, LOGL_ERROR, "TS%u is not configured\n", tn);
			return;
		}

		if (l1sched_start_ciphering(ts, req->a5_algo, req->key, req->key_len) != 0) {
			LOGPFSML(fi, LOGL_ERROR, "Failed to configure ciphering\n");
			return;
		}
		break;
	}
	case TRXCON_EV_TX_DATA_REQ:
	{
		const struct trxcon_param_tx_data_req *req = data;
		struct l1sched_ts_prim *prim;

		prim = l1sched_prim_push(trxcon->sched, L1SCHED_PRIM_DATA,
					 req->chan_nr, req->link_id,
					 req->data, req->data_len);
		if (prim == NULL) {
			LOGPFSML(fi, LOGL_ERROR, "Failed to enqueue a prim\n");
			return;
		}
		break;
	}
	case TRXCON_EV_TX_DATA_CNF:
		l1ctl_tx_dt_conf(trxcon, (const struct trxcon_param_tx_data_cnf *)data);
		break;
	case TRXCON_EV_RX_DATA_IND:
		l1ctl_tx_dt_ind(trxcon, (const struct trxcon_param_rx_data_ind *)data);
		break;
	default:
		OSMO_ASSERT(0);
	}
}

static void trxcon_st_packet_data_action(struct osmo_fsm_inst *fi,
					 uint32_t event, void *data)
{
	struct trxcon_inst *trxcon = fi->priv;

	switch (event) {
	case TRXCON_EV_TX_ACCESS_BURST_REQ:
		handle_tx_access_burst_req(fi, data);
		break;
	case TRXCON_EV_TX_ACCESS_BURST_CNF:
		l1ctl_tx_rach_conf(trxcon, (const struct trxcon_param_tx_access_burst_cnf *)data);
		break;
	case TRXCON_EV_RX_DATA_IND:
	{
		const struct trxcon_param_rx_data_ind *ind = data;

		if (ind->link_id == 0x00)
			LOGPFSML(fi, LOGL_NOTICE, "Rx PDTCH/D message\n");
		else
			LOGPFSML(fi, LOGL_NOTICE, "Rx PTCCH/D message\n");
		break;
	}
	case TRXCON_EV_DCH_EST_REQ:
		handle_dch_est_req(fi, (const struct trxcon_param_dch_est_req *)data);
		break;
	case TRXCON_EV_DCH_REL_REQ:
		l1sched_reset(trxcon->sched, false);
		/* TODO: switch to (not implemented) TRXCON_ST_DCH_TUNING? */
		break;
	default:
		OSMO_ASSERT(0);
	}
}

static void trxcon_fsm_pre_term_cb(struct osmo_fsm_inst *fi,
				   enum osmo_fsm_term_cause cause)
{
	struct trxcon_inst *trxcon = fi->priv;

	if (trxcon == NULL)
		return;

	/* Shutdown the scheduler */
	if (trxcon->sched != NULL)
		l1sched_free(trxcon->sched);
	/* Close active connections */
	if (trxcon->l2if != NULL)
		trxcon_l1ctl_close(trxcon);
	if (trxcon->phyif != NULL)
		trxcon_phyif_close(trxcon->phyif);

	talloc_free(trxcon);
	fi->priv = NULL;
}

static const struct osmo_fsm_state trxcon_fsm_states[] = {
	[TRXCON_ST_RESET] = {
		.name = "RESET",
		.out_state_mask = S(TRXCON_ST_FBSB_SEARCH)
				| S(TRXCON_ST_FULL_POWER_SCAN),
		.in_event_mask  = S(TRXCON_EV_FBSB_SEARCH_REQ)
				| S(TRXCON_EV_FULL_POWER_SCAN_REQ),
		.action = &trxcon_st_reset_action,
	},
	[TRXCON_ST_FULL_POWER_SCAN] = {
		.name = "FULL_POWER_SCAN",
		.out_state_mask = S(TRXCON_ST_RESET)
				| S(TRXCON_ST_FULL_POWER_SCAN),
		.in_event_mask  = S(TRXCON_EV_FULL_POWER_SCAN_RES)
				| S(TRXCON_EV_FULL_POWER_SCAN_REQ),
		.onenter = &trxcon_st_full_power_scan_onenter,
		.action = &trxcon_st_full_power_scan_action,
	},
	[TRXCON_ST_FBSB_SEARCH] = {
		.name = "FBSB_SEARCH",
		.out_state_mask = S(TRXCON_ST_RESET)
				| S(TRXCON_ST_BCCH_CCCH),
		.in_event_mask  = S(TRXCON_EV_FBSB_SEARCH_RES),
		.action = &trxcon_st_fbsb_search_action,
	},
	[TRXCON_ST_BCCH_CCCH] = {
		.name = "BCCH_CCCH",
		.out_state_mask = S(TRXCON_ST_RESET)
				| S(TRXCON_ST_FBSB_SEARCH)
				| S(TRXCON_ST_DEDICATED)
				| S(TRXCON_ST_PACKET_DATA),
		.in_event_mask  = S(TRXCON_EV_RX_DATA_IND)
				| S(TRXCON_EV_SET_CCCH_MODE_REQ)
				| S(TRXCON_EV_TX_ACCESS_BURST_REQ)
				| S(TRXCON_EV_TX_ACCESS_BURST_CNF)
				| S(TRXCON_EV_DCH_EST_REQ),
		.action = &trxcon_st_bcch_ccch_action,
	},
	[TRXCON_ST_DEDICATED] = {
		.name = "DEDICATED",
		.out_state_mask = S(TRXCON_ST_RESET)
				| S(TRXCON_ST_FBSB_SEARCH)
				| S(TRXCON_ST_DEDICATED)
				| S(TRXCON_ST_PACKET_DATA),
		.in_event_mask  = S(TRXCON_EV_DCH_REL_REQ)
				| S(TRXCON_EV_DCH_EST_REQ)
				| S(TRXCON_EV_TX_ACCESS_BURST_REQ)
				| S(TRXCON_EV_TX_ACCESS_BURST_CNF)
				| S(TRXCON_EV_SET_TCH_MODE_REQ)
				| S(TRXCON_EV_TX_DATA_REQ)
				| S(TRXCON_EV_TX_DATA_CNF)
				| S(TRXCON_EV_RX_DATA_IND)
				| S(TRXCON_EV_CRYPTO_REQ),
		.action = &trxcon_st_dedicated_action,
	},
	[TRXCON_ST_PACKET_DATA] = {
		.name = "PACKET_DATA",
		.out_state_mask = S(TRXCON_ST_RESET)
				| S(TRXCON_ST_FBSB_SEARCH)
				| S(TRXCON_ST_DEDICATED)
				| S(TRXCON_ST_PACKET_DATA),
		.in_event_mask  = S(TRXCON_EV_DCH_REL_REQ)
				| S(TRXCON_EV_DCH_EST_REQ)
				| S(TRXCON_EV_TX_ACCESS_BURST_REQ)
				| S(TRXCON_EV_TX_ACCESS_BURST_CNF)
				| S(TRXCON_EV_RX_DATA_IND),
		.action = &trxcon_st_packet_data_action,
	},
};

static const struct value_string trxcon_fsm_event_names[] = {
	OSMO_VALUE_STRING(TRXCON_EV_PHYIF_FAILURE),
	OSMO_VALUE_STRING(TRXCON_EV_L2IF_FAILURE),
	OSMO_VALUE_STRING(TRXCON_EV_RESET_FULL_REQ),
	OSMO_VALUE_STRING(TRXCON_EV_RESET_SCHED_REQ),
	OSMO_VALUE_STRING(TRXCON_EV_FULL_POWER_SCAN_REQ),
	OSMO_VALUE_STRING(TRXCON_EV_FULL_POWER_SCAN_RES),
	OSMO_VALUE_STRING(TRXCON_EV_FBSB_SEARCH_REQ),
	OSMO_VALUE_STRING(TRXCON_EV_FBSB_SEARCH_RES),
	OSMO_VALUE_STRING(TRXCON_EV_SET_CCCH_MODE_REQ),
	OSMO_VALUE_STRING(TRXCON_EV_SET_TCH_MODE_REQ),
	OSMO_VALUE_STRING(TRXCON_EV_SET_PHY_CONFIG_REQ),
	OSMO_VALUE_STRING(TRXCON_EV_TX_ACCESS_BURST_REQ),
	OSMO_VALUE_STRING(TRXCON_EV_TX_ACCESS_BURST_CNF),
	OSMO_VALUE_STRING(TRXCON_EV_UPDATE_SACCH_CACHE_REQ),
	OSMO_VALUE_STRING(TRXCON_EV_DCH_EST_REQ),
	OSMO_VALUE_STRING(TRXCON_EV_DCH_REL_REQ),
	OSMO_VALUE_STRING(TRXCON_EV_TX_DATA_REQ),
	OSMO_VALUE_STRING(TRXCON_EV_TX_DATA_CNF),
	OSMO_VALUE_STRING(TRXCON_EV_RX_DATA_IND),
	OSMO_VALUE_STRING(TRXCON_EV_CRYPTO_REQ),
	{ 0, NULL }
};

struct osmo_fsm trxcon_fsm_def = {
	.name = "trxcon",
	.states = trxcon_fsm_states,
	.num_states = ARRAY_SIZE(trxcon_fsm_states),
	.log_subsys = DLGLOBAL,
	.event_names = trxcon_fsm_event_names,
	.allstate_event_mask = S(TRXCON_EV_PHYIF_FAILURE)
			     | S(TRXCON_EV_L2IF_FAILURE)
			     | S(TRXCON_EV_RESET_FULL_REQ)
			     | S(TRXCON_EV_RESET_SCHED_REQ)
			     | S(TRXCON_EV_SET_PHY_CONFIG_REQ)
			     | S(TRXCON_EV_UPDATE_SACCH_CACHE_REQ),
	.allstate_action = &trxcon_allstate_action,
	.timer_cb = &trxcon_timer_cb,
	.pre_term = &trxcon_fsm_pre_term_cb,
};

static __attribute__((constructor)) void on_dso_load(void)
{
	OSMO_ASSERT(osmo_fsm_register(&trxcon_fsm_def) == 0);
}