summaryrefslogtreecommitdiff
path: root/mac/fira_access.c
blob: 0d8742be8cbf002d6635f4ecc69319c3f0da0f91 (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
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
/*
 * This file is part of the UWB stack for linux.
 *
 * Copyright (c) 2020-2021 Qorvo US, Inc.
 *
 * This software is provided under the GNU General Public License, version 2
 * (GPLv2), as well as under a Qorvo commercial license.
 *
 * You may choose to use this software under the terms of the GPLv2 License,
 * version 2 ("GPLv2"), as published by the Free Software Foundation.
 * You should have received a copy of the GPLv2 along with this program.  If
 * not, see <http://www.gnu.org/licenses/>.
 *
 * This program is distributed under the GPLv2 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 GPLv2 for more
 * details.
 *
 * If you cannot meet the requirements of the GPLv2, you may not use this
 * software for any purpose without first obtaining a commercial license from
 * Qorvo. Please contact Qorvo to inquire about licensing terms.
 */

#include "fira_access.h"
#include "fira_session.h"
#include "fira_frame.h"
#include "fira_trace.h"

#include <asm/unaligned.h>
#include <linux/bitops.h>
#include <linux/string.h>
#include <linux/ieee802154.h>
#include <net/mcps802154_frame.h>

#include "warn_return.h"
#include "utils_fixed_point.h"

#define FIRA_FRAME_MAX_SIZE 127

#define FIRA_STS_FOM_THRESHOLD 153

static void fira_update_antennas_id(struct fira_session *session);
static struct mcps802154_access *
fira_access_controlee(struct fira_local *local, struct fira_session *session);

/**
 * phase_to_rad_fp() - compute the angle(AoA) from phase(PDOA) with fixed-point.
 * @pdoa_rad_q11: phase of arrival in fixed-point.
 * @spacing_mm_q11: spacing between antenna in mm in fixed-point.
 *
 * Return: Angle of Arrival in fixed-point too.
 */
static s16 phase_to_rad_fp(s16 pdoa_rad_q11, int spacing_mm_q11)
{
	/**
	 * Speed of light in air.
	 * static const long long speed_of_light_m_per_s = 299702547ull;
	 * static const long long freq_hz = 6.5e9;
	 * Constant to amplify/decrease value A and B, and so decrease
	 * error added by fixed-point.
	 *  Through N, A and B are closed to INT16_MAX.
	 * static const int N = 762;
	 * static const long long num = N * K * speed_of_light_m_per_s;
	 * static const double dem = freq_hz * 2.0 * M_PI;
	 * static const s16 A = num / dem;
	 * s16 B = N * spacing_mm_q11 / 1000;
	 *
	 * To be sure to have a optimize code, inline A declaration.
	 * A = 11452
	 *
	 * A will change with CHAN index.
	 * B is calculated using given antenna spacing that come from the mcps
	 * driver from the calibration table.
	 */
	static const s16 A = 11452;
	s16 B = (s16)(762 * spacing_mm_q11 / 1000);
	s16 x = div_fp(mult_fp(pdoa_rad_q11, A), B);
	/* Saturate between -1 to +1 for asyn. */
	x = x > K ? K : x < -K ? -K : x;
	return asin_fp(x);
}

/**
 * fira_access_setup_frame() - Fill an access frame from a FiRa slot.
 * @local: FiRa context.
 * @session: Session.
 * @frame: Access frame.
 * @sts_params: Where to store STS parameters.
 * @slot: Corresponding slot.
 * @frame_dtu: frame transmission or reception date.
 * @is_tx: true for TX.
 */
static void fira_access_setup_frame(struct fira_local *local,
				    struct fira_session *session,
				    struct mcps802154_access_frame *frame,
				    struct mcps802154_sts_params *sts_params,
				    const struct fira_slot *slot, u32 frame_dtu,
				    bool is_tx)
{
	const struct fira_session_params *params = &session->params;
	struct mcps802154_sts_params *sts_params_for_access = NULL;
	int rframe_config = session->params.rframe_config;

	bool is_rframe = slot->message_id <= FIRA_MESSAGE_ID_RFRAME_MAX;
	bool is_last_rframe = slot->message_id == FIRA_MESSAGE_ID_RANGING_FINAL;

	if (is_rframe) {
		memcpy(sts_params->v, session->crypto.sts_v, AES_BLOCK_SIZE);
		put_unaligned_be32(slot->index,
				   sts_params->v + AES_BLOCK_SIZE / 2);
		memcpy(sts_params->key,
		       session->crypto.derived_authentication_key,
		       AES_KEYSIZE_128);
		/* Constant for the moment. */
		sts_params->n_segs = 1;
		sts_params->seg_len = 64;
		sts_params->sp2_tx_gap_4chips = 0;
		sts_params->sp2_rx_gap_4chips[0] = 0;
		sts_params->sp2_rx_gap_4chips[1] = 0;
		sts_params->sp2_rx_gap_4chips[2] = 0;
		sts_params->sp2_rx_gap_4chips[3] = 0;
		sts_params_for_access = sts_params;
	}

	if (is_tx) {
		u8 flags = MCPS802154_TX_FRAME_TIMESTAMP_DTU;

		if (is_rframe) {
			struct fira_ranging_info *ranging_info;

			ranging_info =
				&local->ranging_info[slot->ranging_index];
			ranging_info->timestamps_rctu[slot->message_id] =
				mcps802154_tx_timestamp_dtu_to_rmarker_rctu(
					local->llhw, frame_dtu, slot->tx_ant);

			flags |= MCPS802154_TX_FRAME_RANGING;
			if (rframe_config == FIRA_RFRAME_CONFIG_SP3)
				flags |= MCPS802154_TX_FRAME_SP3;
			else
				flags |= MCPS802154_TX_FRAME_SP1;
			if (!is_last_rframe)
				flags |= MCPS802154_TX_FRAME_KEEP_RANGING_CLOCK;
		}
		*frame = (struct mcps802154_access_frame){
			.is_tx = true,
			.tx_frame_info = {
				.timestamp_dtu = frame_dtu,
				.flags = flags,
				.ant_id = slot->tx_ant,
			},
			.sts_params = sts_params_for_access,
		};
	} else {
		u8 flags = MCPS802154_RX_INFO_TIMESTAMP_DTU;
		u16 request = 0;

		if (is_rframe) {
			flags |= MCPS802154_RX_INFO_RANGING;
			if (rframe_config == FIRA_RFRAME_CONFIG_SP3)
				flags |= MCPS802154_RX_INFO_SP3;
			else
				flags |= MCPS802154_RX_INFO_SP1;
			if (!is_last_rframe)
				flags |= MCPS802154_RX_INFO_KEEP_RANGING_CLOCK;
			request = MCPS802154_RX_FRAME_INFO_TIMESTAMP_RCTU |
				  MCPS802154_RX_FRAME_INFO_RANGING_STS_FOM;
			if (params->aoa_result_req) {
				flags |= MCPS802154_RX_INFO_RANGING_PDOA;
				request |=
					MCPS802154_RX_FRAME_INFO_RANGING_PDOA |
					MCPS802154_RX_FRAME_INFO_RANGING_PDOA_FOM;
			}
		}
		*frame = (struct mcps802154_access_frame){
			.is_tx = false,
			.rx = {
				.info = {
					.timestamp_dtu = frame_dtu,
					.flags = flags,
					.ant_pair_id = slot->rx_ant_pair,
				},
				.frame_info_flags_request = request,
			},
			.sts_params = sts_params_for_access,
		};
	}
}

static bool fira_rx_sts_good(struct fira_local *local,
			     const struct mcps802154_rx_frame_info *info)
{
	if (!(info->flags & MCPS802154_RX_FRAME_INFO_RANGING_STS_FOM))
		return false;
	/* Only one segment for the moment. */
	if (info->ranging_sts_fom[0] < FIRA_STS_FOM_THRESHOLD)
		return false;
	return true;
}

static void fira_rx_frame_ranging(struct fira_local *local,
				  const struct fira_slot *slot,
				  struct sk_buff *skb,
				  const struct mcps802154_rx_frame_info *info)
{
	const struct fira_session *session = local->current_session;
	const struct fira_session_params *params = &session->params;
	struct fira_ranging_info *ranging_info =
		&local->ranging_info[slot->ranging_index];
	struct mcps802154_ie_get_context ie_get = {};

	bool pdoa_info_present;

	if (!fira_rx_sts_good(local, info)) {
		ranging_info->status = FIRA_STATUS_RANGING_RX_PHY_STS_FAILED;
		return;
	}

	if (!(info->flags & MCPS802154_RX_FRAME_INFO_TIMESTAMP_RCTU)) {
		ranging_info->status = FIRA_STATUS_RANGING_RX_PHY_TOA_FAILED;
		return;
	}
	ranging_info->timestamps_rctu[slot->message_id] = info->timestamp_rctu;

	pdoa_info_present = info->flags & MCPS802154_RX_FRAME_INFO_RANGING_PDOA;

	if (pdoa_info_present) {
		struct fira_local_aoa_info *local_aoa;
		bool pdoa_fom_info_present =
			info->flags & MCPS802154_RX_FRAME_INFO_RANGING_PDOA_FOM;
		s16 local_pdoa_q11 = info->ranging_pdoa_rad_q11;
		s16 local_aoa_q11 = phase_to_rad_fp(
			local_pdoa_q11, info->ranging_pdoa_spacing_mm_q11);
		if (params->rx_antenna_pair_azimuth == slot->rx_ant_pair) {
			local_aoa = &ranging_info->local_aoa_azimuth;
		} else if (params->rx_antenna_pair_elevation ==
			   slot->rx_ant_pair) {
			local_aoa = &ranging_info->local_aoa_elevation;
		} else {
			local_aoa = &ranging_info->local_aoa;
		}

		local_aoa->present = true;
		local_aoa->rx_ant_pair = slot->rx_ant_pair;
		local_aoa->pdoa_2pi = map_q11_to_2pi(local_pdoa_q11);
		local_aoa->aoa_2pi = map_q11_to_2pi(local_aoa_q11);
		if (pdoa_fom_info_present)
			local_aoa->aoa_fom = info->ranging_pdoa_fom;
	}

	if (skb) {
		if (fira_frame_header_check_decrypt(local, slot, skb, &ie_get,
						    NULL, NULL) ||
		    !fira_frame_rframe_payload_check(local, slot, skb,
						     &ie_get)) {
			ranging_info->status =
				FIRA_STATUS_RANGING_RX_MAC_IE_DEC_FAILED;
		}
	}
}

static void fira_rx_frame_control(struct fira_local *local,
				  const struct fira_slot *slot,
				  struct sk_buff *skb,
				  const struct mcps802154_rx_frame_info *info)
{
	struct fira_ranging_info *ranging_info =
		&local->ranging_info[slot->ranging_index];
	struct fira_session *session = local->current_session;
	struct fira_session *allow_resync_session = NULL;
	struct mcps802154_ie_get_context ie_get = {};
	u32 sts_index;
	unsigned int n_slots;
	int last_slot_index;
	int i;

	if (!(info->flags & MCPS802154_RX_FRAME_INFO_TIMESTAMP_DTU)) {
		ranging_info->status = FIRA_STATUS_RANGING_RX_PHY_DEC_FAILED;
		return;
	}
	if (fira_frame_header_check_decrypt(
		    local, slot, skb, &ie_get, &sts_index,
		    session->synchronised ? NULL : &allow_resync_session))
		goto failed;
	if (allow_resync_session) {
		session = local->current_session = allow_resync_session;
		fira_access_controlee(local, session);
	}

	if (!fira_frame_control_payload_check(local, skb, &ie_get, &n_slots)) {
		ranging_info->status = FIRA_STATUS_RANGING_RX_MAC_IE_DEC_FAILED;
		goto failed;
	}

	fira_session_resync(local, session, sts_index, info->timestamp_dtu);

	last_slot_index = 0;
	for (i = 1; i < n_slots; i++) {
		const struct fira_slot *slot = &local->slots[i];
		struct mcps802154_access_frame *frame = &local->frames[i];
		struct mcps802154_sts_params *sts_params =
			&local->sts_params[i];
		bool is_tx;
		u32 frame_dtu;

		is_tx = slot->tx_controlee_index != -1;
		frame_dtu = info->timestamp_dtu +
			    session->params.slot_duration_dtu * slot->index;
		last_slot_index = slot->index;

		fira_access_setup_frame(local, session, frame, sts_params, slot,
					frame_dtu, is_tx);
	}
	local->access.timestamp_dtu = info->timestamp_dtu;
	local->access.duration_dtu =
		session->params.slot_duration_dtu * (last_slot_index + 1);
	local->access.n_frames = n_slots;

	return;
failed:
	ranging_info->status = FIRA_STATUS_RANGING_RX_MAC_IE_DEC_FAILED;
	local->access.timestamp_dtu = info->timestamp_dtu;
	local->access.duration_dtu = session->params.slot_duration_dtu;
}

static void fira_rx_frame_measurement_report(
	struct fira_local *local, const struct fira_slot *slot,
	struct sk_buff *skb, const struct mcps802154_rx_frame_info *info)
{
	struct fira_ranging_info *ranging_info =
		&local->ranging_info[slot->ranging_index];
	struct mcps802154_ie_get_context ie_get = {};

	if (fira_frame_header_check_decrypt(local, slot, skb, &ie_get, NULL,
					    NULL))
		goto failed;

	if (!fira_frame_measurement_report_payload_check(local, slot, skb,
							 &ie_get))
		goto failed;

	return;
failed:
	ranging_info->status = FIRA_STATUS_RANGING_RX_MAC_IE_DEC_FAILED;
}

static void
fira_rx_frame_result_report(struct fira_local *local,
			    const struct fira_slot *slot, struct sk_buff *skb,
			    const struct mcps802154_rx_frame_info *info)
{
	struct fira_ranging_info *ranging_info =
		&local->ranging_info[slot->ranging_index];
	struct mcps802154_ie_get_context ie_get = {};

	if (fira_frame_header_check_decrypt(local, slot, skb, &ie_get, NULL,
					    NULL))
		goto failed;

	if (!fira_frame_result_report_payload_check(local, slot, skb, &ie_get))
		goto failed;

	return;
failed:
	ranging_info->status = FIRA_STATUS_RANGING_RX_MAC_IE_DEC_FAILED;
}

static bool fira_do_process_rx_frame(enum mcps802154_rx_error_type error,
				     struct fira_ranging_info *ranging_info)
{
	switch (error) {
	case MCPS802154_RX_ERROR_NONE:
		return true;
	case MCPS802154_RX_ERROR_SFD_TIMEOUT:
	case MCPS802154_RX_ERROR_TIMEOUT:
		ranging_info->status = FIRA_STATUS_RANGING_RX_TIMEOUT;
		break;
	case MCPS802154_RX_ERROR_FILTERED:
	case MCPS802154_RX_ERROR_BAD_CKSUM:
		ranging_info->status = FIRA_STATUS_RANGING_RX_MAC_DEC_FAILED;
		break;
	case MCPS802154_RX_ERROR_UNCORRECTABLE:
	case MCPS802154_RX_ERROR_OTHER:
		ranging_info->status = FIRA_STATUS_RANGING_RX_PHY_DEC_FAILED;
		break;
	}
	return false;
}

static void fira_rx_frame(struct mcps802154_access *access, int frame_idx,
			  struct sk_buff *skb,
			  const struct mcps802154_rx_frame_info *info,
			  enum mcps802154_rx_error_type error)
{
	struct fira_local *local = access_to_local(access);
	const struct fira_slot *slot = &local->slots[frame_idx];
	struct fira_session *session = local->current_session;
	struct fira_ranging_info *ranging_info =
		&local->ranging_info[slot->ranging_index];

	if (fira_do_process_rx_frame(error, ranging_info)) {
		switch (slot->message_id) {
		case FIRA_MESSAGE_ID_RANGING_INITIATION:
		case FIRA_MESSAGE_ID_RANGING_RESPONSE:
		case FIRA_MESSAGE_ID_RANGING_FINAL:
			fira_rx_frame_ranging(local, slot, skb, info);
			break;
		case FIRA_MESSAGE_ID_CONTROL:
			fira_rx_frame_control(local, slot, skb, info);
			break;
		case FIRA_MESSAGE_ID_MEASUREMENT_REPORT:
			fira_rx_frame_measurement_report(local, slot, skb,
							 info);
			break;
		case FIRA_MESSAGE_ID_RESULT_REPORT:
			fira_rx_frame_result_report(local, slot, skb, info);
			break;
		case FIRA_MESSAGE_ID_CONTROL_UPDATE:
			break;
		default:
			WARN_UNREACHABLE_DEFAULT();
		}
	}

	if (skb)
		kfree_skb(skb);

	trace_region_fira_rx_message_type(
		session, slot->message_id,
		local->ranging_info[slot->ranging_index].status);

	/* Controlee: Stop round on error.
	   Controller: Stop when all ranging fails. */
	if (local->ranging_info[slot->ranging_index].status)
		if (session->params.device_type == FIRA_DEVICE_TYPE_CONTROLEE ||
		    (slot->message_id <= FIRA_MESSAGE_ID_RFRAME_MAX &&
		     --local->n_ranging_valid == 0))
			access->n_frames = frame_idx + 1;
}

static struct sk_buff *fira_tx_get_frame(struct mcps802154_access *access,
					 int frame_idx)
{
	struct fira_local *local = access_to_local(access);
	const struct fira_slot *slot = &local->slots[frame_idx];
	struct sk_buff *skb;
	int rframe = local->current_session->params.rframe_config;

	trace_region_fira_tx_message_type(local->current_session,
					  slot->message_id);
	if (rframe == FIRA_RFRAME_CONFIG_SP3 &&
	    slot->message_id <= FIRA_MESSAGE_ID_RFRAME_MAX)
		return NULL;

	skb = mcps802154_frame_alloc(local->llhw, FIRA_FRAME_MAX_SIZE,
				     GFP_KERNEL);
	WARN_RETURN_ON(!skb, NULL);

	fira_frame_header_put(local, slot, skb);

	switch (slot->message_id) {
	case FIRA_MESSAGE_ID_RANGING_INITIATION:
	case FIRA_MESSAGE_ID_RANGING_RESPONSE:
		fira_frame_rframe_payload_put(local, skb);
		break;
	case FIRA_MESSAGE_ID_RANGING_FINAL:
		break;
	case FIRA_MESSAGE_ID_CONTROL:
		local->current_session->n_cm_sent++;
		fira_frame_control_payload_put(local, slot, skb);
		break;
	case FIRA_MESSAGE_ID_MEASUREMENT_REPORT:
		fira_frame_measurement_report_payload_put(local, slot, skb);
		break;
	case FIRA_MESSAGE_ID_RESULT_REPORT:
		fira_frame_result_report_payload_put(local, slot, skb);
		break;
	case FIRA_MESSAGE_ID_CONTROL_UPDATE:
		break;
	default: /* LCOV_EXCL_START */
		kfree_skb(skb);
		WARN_UNREACHABLE_DEFAULT();
		return NULL;
		/* LCOV_EXCL_STOP */
	}

	if (fira_frame_encrypt(local, slot, skb)) {
		kfree_skb(skb);
		return NULL;
	}

	return skb;
}

static void fira_tx_return(struct mcps802154_access *access, int frame_idx,
			   struct sk_buff *skb,
			   enum mcps802154_access_tx_return_reason reason)
{
	struct fira_local *local = access_to_local(access);
	int i;

	kfree_skb(skb);

	/* Error on TX. */
	if (reason == MCPS802154_ACCESS_TX_RETURN_REASON_CANCEL) {
		for (i = 0; i < local->n_ranging_info; i++) {
			local->ranging_info[i].status =
				FIRA_STATUS_RANGING_TX_FAILED;
		}
	}
}

static void fira_access_done(struct mcps802154_access *access)
{
	struct fira_local *local = access_to_local(access);
	struct fira_session *session = local->current_session;

	fira_session_access_done(local, session);

	local->current_session = NULL;
}

struct mcps802154_access_ops fira_access_ops = {
	.common = {
		.access_done = fira_access_done,
	},
	.rx_frame = fira_rx_frame,
	.tx_get_frame = fira_tx_get_frame,
	.tx_return = fira_tx_return,
};

static int fira_next_in_bitfield_u8(u8 bitfield, int prev)
{
	u32 padded_bitfield = 1 << 16 | bitfield << 8 | bitfield;
	u32 rotated_bitfield = padded_bitfield >> (prev + 1);

	return (ffs(rotated_bitfield) + prev) % 8;
}

static void fira_update_antennas_id(struct fira_session *session)
{
	const struct fira_session_params *p = &session->params;

	session->tx_ant = fira_next_in_bitfield_u8(p->tx_antenna_selection,
						   session->tx_ant);

	switch (p->rx_antenna_switch) {
	default:
	case FIRA_RX_ANTENNA_SWITCH_BETWEEN_ROUND:
		/* Switch pairs between round. */
		session->rx_ant_pair[0] = session->rx_ant_pair[1] =
			fira_next_in_bitfield_u8(p->rx_antenna_selection,
						 session->rx_ant_pair[0]);
		break;
	case FIRA_RX_ANTENNA_SWITCH_DURING_ROUND:
		session->rx_ant_pair[0] = p->rx_antenna_pair_azimuth;
		session->rx_ant_pair[1] = p->rx_antenna_pair_elevation;
		break;
	case FIRA_RX_ANTENNA_SWITCH_TWO_RANGING:
		/* Switch from one ranging to another. */
		if (session->rx_ant_pair[0] == p->rx_antenna_pair_azimuth)
			session->rx_ant_pair[0] = p->rx_antenna_pair_elevation;
		else
			session->rx_ant_pair[0] = p->rx_antenna_pair_azimuth;
		session->rx_ant_pair[1] = session->rx_ant_pair[0];
		break;
	}
}

static __le16 fira_access_set_short_address(struct fira_local *local,
					    struct fira_session *session,
					    struct mcps802154_access *access)
{
	__le16 src_short_addr = mcps802154_get_short_addr(local->llhw);

	if (session->params.short_addr != IEEE802154_ADDR_SHORT_BROADCAST &&
	    src_short_addr != session->params.short_addr) {
		access->hw_addr_filt = (struct ieee802154_hw_addr_filt){
			.short_addr = session->params.short_addr,
		};
		access->hw_addr_filt_changed = IEEE802154_AFILT_SADDR_CHANGED;
		return session->params.short_addr;
	} else {
		access->hw_addr_filt_changed = 0;
		return src_short_addr;
	}
}

static struct mcps802154_access *
fira_access_controller(struct fira_local *local, struct fira_session *session)
{
	struct mcps802154_access *access;
	struct mcps802154_access_frame *frame;
	struct mcps802154_sts_params *sts_params;
	struct fira_slot *s;
	struct fira_ranging_info *ri;
	struct fira_controlees_array *controlees_array =
		&session->params.current_controlees;
	int i;
	u32 frame_dtu;
	int index = 0;

	access = &local->access;
	local->src_short_addr =
		fira_access_set_short_address(local, session, access);
	local->dst_short_addr = (controlees_array->size == 1) ?
					controlees_array->data[0].short_addr :
					IEEE802154_ADDR_SHORT_BROADCAST;

	access->method = MCPS802154_ACCESS_METHOD_MULTI;
	access->ops = &fira_access_ops;
	access->timestamp_dtu = session->block_start_dtu +
				fira_session_get_round_slot(session) *
					session->params.slot_duration_dtu;
	access->frames = local->frames;

	fira_update_antennas_id(session);
	local->n_ranging_info = controlees_array->size;
	local->n_ranging_valid = controlees_array->size;
	ri = local->ranging_info;

	memset(ri, 0, local->n_ranging_info * sizeof(*ri));

	s = local->slots;

	s->index = index++;
	s->tx_controlee_index = -1;
	s->ranging_index = 0;
	s->tx_ant = session->tx_ant;
	s->message_id = FIRA_MESSAGE_ID_CONTROL;

	s++;
	s->index = index++;
	s->tx_controlee_index = -1;
	s->ranging_index = 0;
	s->tx_ant = session->tx_ant;
	s->message_id = FIRA_MESSAGE_ID_RANGING_INITIATION;

	s++;
	for (i = 0; i < local->n_ranging_info; i++) {
		ri->short_addr = controlees_array->data[i].short_addr;
		/* Requested in fira_report_aoa function. */
		ri++;
		s->index = index++;
		s->tx_controlee_index = i;
		s->ranging_index = i;
		s->rx_ant_pair = session->rx_ant_pair[0];
		s->message_id = FIRA_MESSAGE_ID_RANGING_RESPONSE;
		s++;
	}
	s->index = index++;
	s->tx_controlee_index = -1;
	s->ranging_index = 0;
	s->tx_ant = session->tx_ant;
	s->message_id = FIRA_MESSAGE_ID_RANGING_FINAL;

	s++;
	s->index = index++;
	s->tx_controlee_index = -1;
	s->ranging_index = 0;
	s->tx_ant = session->tx_ant;
	s->message_id = FIRA_MESSAGE_ID_MEASUREMENT_REPORT;

	s++;
	if (session->params.result_report_phase) {
		for (i = 0; i < local->n_ranging_info; i++) {
			s->index = index++;
			s->tx_controlee_index = i;
			s->ranging_index = i;
			s->rx_ant_pair = session->rx_ant_pair[0];
			s->message_id = FIRA_MESSAGE_ID_RESULT_REPORT;
			s++;
		}
	}
	access->n_frames = index;

	frame_dtu = access->timestamp_dtu;

	for (i = 0; i < access->n_frames; i++) {
		bool is_tx;
		int delta_dtu = 0;

		s = &local->slots[i];
		frame = &local->frames[i];
		sts_params = &local->sts_params[i];

		is_tx = s->tx_controlee_index == -1;

		if (i && is_tx)
			delta_dtu = 4 * local->llhw->rstu_dtu;

		fira_access_setup_frame(local, session, frame, sts_params, s,
					frame_dtu + delta_dtu, is_tx);

		frame_dtu += session->params.slot_duration_dtu;
	}
	access->duration_dtu = frame_dtu - access->timestamp_dtu;

	return access;
}

static struct mcps802154_access *
fira_access_controlee(struct fira_local *local, struct fira_session *session)
{
	struct mcps802154_access *access;
	struct mcps802154_access_frame *frame;
	struct fira_slot *s;
	struct fira_ranging_info *ri;

	access = &local->access;
	local->src_short_addr =
		fira_access_set_short_address(local, session, access);
	local->dst_short_addr = session->params.controller_short_addr;

	access->method = MCPS802154_ACCESS_METHOD_MULTI;
	access->ops = &fira_access_ops;
	access->timestamp_dtu = session->last_access_timestamp_dtu;
	access->duration_dtu = session->last_access_duration_dtu;
	access->frames = local->frames;
	access->n_frames = 1;
	fira_update_antennas_id(session);

	ri = local->ranging_info;
	memset(ri, 0, sizeof(*ri));
	ri->short_addr = session->params.controller_short_addr;
	local->n_ranging_info = 1;

	s = local->slots;
	s->index = 0;
	s->tx_controlee_index = -1;
	s->ranging_index = 0;
	s->rx_ant_pair = session->rx_ant_pair[0];
	s->message_id = FIRA_MESSAGE_ID_CONTROL;

	frame = local->frames;
	*frame = (struct mcps802154_access_frame){
		.is_tx = false,
		.rx = {
			.info = {
				.timestamp_dtu = access->timestamp_dtu,
				.timeout_dtu = access->duration_dtu ? access->duration_dtu : -1,
				.flags = 0,//MCPS802154_RX_INFO_TIMESTAMP_DTU,
				.ant_pair_id = s->rx_ant_pair,
			},
			.frame_info_flags_request
				= MCPS802154_RX_FRAME_INFO_TIMESTAMP_DTU,
		},
	};

	return access;
}

struct mcps802154_access *fira_get_access(struct mcps802154_region *region,
					  u32 next_timestamp_dtu,
					  int next_in_region_dtu,
					  int region_duration_dtu)
{
	struct fira_local *local = region_to_local(region);
	struct fira_session *session;

	session = fira_session_next(local, next_timestamp_dtu +
						   local->llhw->anticip_dtu);
	local->current_session = session;

	if (!session)
		return NULL;
	return fira_compute_access(local, session);
}

struct mcps802154_access *fira_compute_access(struct fira_local *local,
					      struct fira_session *session)
{
	if (session->params.device_type == FIRA_DEVICE_TYPE_CONTROLLER)
		return fira_access_controller(local, session);
	else
		return fira_access_controlee(local, session);
}

void fira_session_get_demand(struct fira_local *local,
			     struct fira_session *session,
			     struct mcps802154_region_demand *demand)
{
	demand->timestamp_dtu = session->block_start_dtu +
				fira_session_get_round_slot(session) *
					session->params.slot_duration_dtu;
	if (session->params.device_type == FIRA_DEVICE_TYPE_CONTROLLER)
		demand->duration_dtu =
			(4 + 2 * session->params.current_controlees.size) *
			session->params.slot_duration_dtu;
	else
		/* Min size for DSTWR. */
		demand->duration_dtu = 6 * session->params.slot_duration_dtu;
}