Repository navigation
Expand file tree
/
Copy pathkpb.c
More file actions
1653 lines (1449 loc) · 45.5 KB
/
Copy pathkpb.c
File metadata and controls
1653 lines (1449 loc) · 45.5 KB
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
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: BSD-3-Clause
//
// Copyright(c) 2019 Intel Corporation. All rights reserved.
//
// Author: Marcin Rajwa <marcin.rajwa@linux.intel.com>
/*
* A key phrase buffer component.
*/
/**
* \file audio/kpb.c
* \brief Key phrase buffer component implementation
* \author Marcin Rajwa <marcin.rajwa@linux.intel.com>
*/
#include <sof/audio/buffer.h>
#include <sof/audio/component_ext.h>
#include <sof/audio/pipeline.h>
#include <sof/audio/kpb.h>
#include <sof/audio/ipc-config.h>
#include <sof/common.h>
#include <sof/debug/panic.h>
#include <sof/ipc/msg.h>
#include <sof/drivers/timer.h>
#include <sof/lib/alloc.h>
#include <sof/lib/clk.h>
#include <sof/lib/memory.h>
#include <sof/lib/notifier.h>
#include <sof/lib/pm_runtime.h>
#include <sof/lib/uuid.h>
#include <sof/list.h>
#include <sof/math/numbers.h>
#include <sof/platform.h>
#include <sof/schedule/edf_schedule.h>
#include <sof/schedule/schedule.h>
#include <sof/schedule/task.h>
#include <sof/string.h>
#include <sof/ut.h>
#include <ipc/topology.h>
#include <user/kpb.h>
#include <user/trace.h>
#include <errno.h>
#include <limits.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
static const struct comp_driver comp_kpb;
LOG_MODULE_REGISTER(kpb, CONFIG_SOF_LOG_LEVEL);
/* d8218443-5ff3-4a4c-b388-6cfe07b9562e */
DECLARE_SOF_RT_UUID("kpb", kpb_uuid, 0xd8218443, 0x5ff3, 0x4a4c,
0xb3, 0x88, 0x6c, 0xfe, 0x07, 0xb9, 0x56, 0x2e);
DECLARE_TR_CTX(kpb_tr, SOF_UUID(kpb_uuid), LOG_LEVEL_INFO);
/* e50057a5-8b27-4db4-bd79-9a639cee5f50 */
DECLARE_SOF_UUID("kpb-task", kpb_task_uuid, 0xe50057a5, 0x8b27, 0x4db4,
0xbd, 0x79, 0x9a, 0x63, 0x9c, 0xee, 0x5f, 0x50);
/* KPB private data, runtime data */
struct comp_data {
enum kpb_state state; /**< current state of KPB component */
uint32_t state_log; /**< keeps record of KPB recent states */
#ifndef __ZEPHYR__
struct k_spinlock lock; /**< locking mechanism for read pointer calculations */
k_spinlock_key_t key;
#else
struct k_mutex lock;
#endif
struct sof_kpb_config config; /**< component configuration data */
struct history_data hd; /** data related to history buffer */
struct task draining_task;
struct draining_data draining_task_data;
struct kpb_client clients[KPB_MAX_NO_OF_CLIENTS];
struct comp_buffer *sel_sink; /**< real time sink (channel selector )*/
struct comp_buffer *host_sink; /**< draining sink (client) */
uint32_t kpb_no_of_clients; /**< number of registered clients */
uint32_t source_period_bytes; /**< source number of period bytes */
uint32_t sink_period_bytes; /**< sink number of period bytes */
size_t host_buffer_size; /**< size of host buffer */
size_t host_period_size; /**< size of history period */
bool sync_draining_mode; /**< should we synchronize draining with
* host?
*/
enum comp_copy_type force_copy_type; /**< should we force copy_type on kpb sink? */
};
/*! KPB private functions */
static void kpb_event_handler(void *arg, enum notify_id type, void *event_data);
static int kpb_register_client(struct comp_data *kpb, struct kpb_client *cli);
static void kpb_init_draining(struct comp_dev *dev, struct kpb_client *cli);
static enum task_state kpb_draining_task(void *arg);
static int kpb_buffer_data(struct comp_dev *dev,
const struct comp_buffer *source, size_t size);
static size_t kpb_allocate_history_buffer(struct comp_data *kpb,
size_t hb_size_req);
static void kpb_clear_history_buffer(struct history_buffer *buff);
static void kpb_free_history_buffer(struct history_buffer *buff);
static inline bool kpb_is_sample_width_supported(uint32_t sampling_width);
static void kpb_copy_samples(struct comp_buffer *sink,
struct comp_buffer *source, size_t size,
size_t sample_width);
static void kpb_drain_samples(void *source, struct audio_stream *sink,
size_t size, size_t sample_width);
static void kpb_buffer_samples(const struct audio_stream *source,
int offset, void *sink, size_t size,
size_t sample_width);
static void kpb_reset_history_buffer(struct history_buffer *buff);
static inline bool validate_host_params(struct comp_dev *dev,
size_t host_period_size,
size_t host_buffer_size,
size_t hb_size_req);
static inline void kpb_change_state(struct comp_data *kpb,
enum kpb_state state);
static uint64_t kpb_task_deadline(void *data)
{
return SOF_TASK_DEADLINE_ALMOST_IDLE;
}
#ifdef __ZEPHYR__
static void kpb_lock(struct comp_data *kpb)
{
k_mutex_lock(&kpb->lock, K_FOREVER);
}
static void kpb_unlock(struct comp_data *kpb)
{
k_mutex_unlock(&kpb->lock);
}
static void kpb_lock_init(struct comp_data *kpb)
{
k_mutex_init(&kpb->lock);
}
#else /* __ZEPHYR__ */
static void kpb_lock(struct comp_data *kpb)
{
kpb->key = k_spin_lock(&kpb->lock);
}
static void kpb_unlock(struct comp_data *kpb)
{
k_spin_unlock(&kpb->lock, kpb->key);
}
static void kpb_lock_init(struct comp_data *kpb)
{
k_spinlock_init(&kpb->lock);
}
#endif /* __ZEPHYR__ */
/**
* \brief Create a key phrase buffer component.
* \param[in] config - generic ipc component pointer.
*
* \return: a pointer to newly created KPB component.
*/
static struct comp_dev *kpb_new(const struct comp_driver *drv,
struct comp_ipc_config *config,
void *spec)
{
struct ipc_config_process *ipc_process = spec;
struct task_ops ops = {
.run = kpb_draining_task,
.get_deadline = kpb_task_deadline,
};
struct comp_dev *dev;
struct comp_data *kpb;
int ret;
comp_cl_info(&comp_kpb, "kpb_new()");
/* make sure data size is not bigger than config space */
if (ipc_process->size > sizeof(struct sof_kpb_config)) {
comp_cl_err(&comp_kpb, "kpb_new(): data size %u too big",
ipc_process->size);
return NULL;
}
dev = comp_alloc(drv, sizeof(*dev));
if (!dev)
return NULL;
dev->ipc_config = *config;
kpb = rzalloc(SOF_MEM_ZONE_RUNTIME, 0, SOF_MEM_CAPS_RAM, sizeof(*kpb));
if (!kpb) {
rfree(dev);
return NULL;
}
comp_set_drvdata(dev, kpb);
ret = memcpy_s(&kpb->config, sizeof(kpb->config), ipc_process->data,
ipc_process->size);
assert(!ret);
if (!kpb_is_sample_width_supported(kpb->config.sampling_width)) {
comp_err(dev, "kpb_new(): requested sampling width not supported");
rfree(dev);
return NULL;
}
if (kpb->config.channels > KPB_MAX_SUPPORTED_CHANNELS) {
comp_err(dev, "kpb_new(): no of channels exceeded the limit");
rfree(dev);
return NULL;
}
if (kpb->config.sampling_freq != KPB_SAMPLNG_FREQUENCY) {
comp_err(dev, "kpb_new(): requested sampling frequency not supported");
rfree(dev);
return NULL;
}
kpb_lock_init(kpb);
/* Initialize draining task */
schedule_task_init_edf(&kpb->draining_task, /* task structure */
SOF_UUID(kpb_task_uuid), /* task uuid */
&ops, /* task ops */
&kpb->draining_task_data, /* task private data */
0, /* core on which we should run */
0); /* no flags */
/* Init basic component data */
kpb->hd.c_hb = NULL;
kpb->kpb_no_of_clients = 0;
kpb->state_log = 0;
#ifdef CONFIG_KPB_FORCE_COPY_TYPE_NORMAL
kpb->force_copy_type = COMP_COPY_NORMAL;
#else
kpb->force_copy_type = COMP_COPY_INVALID; /* do not change kpb sink copy type */
#endif
/* Kpb has been created successfully */
dev->state = COMP_STATE_READY;
kpb_change_state(kpb, KPB_STATE_CREATED);
return dev;
}
/**
* \brief Allocate history buffer.
* \param[in] kpb - KPB component data pointer.
*
* \return: none.
*/
static size_t kpb_allocate_history_buffer(struct comp_data *kpb,
size_t hb_size_req)
{
struct history_buffer *hb;
struct history_buffer *new_hb = NULL;
/*! Total allocation size */
size_t hb_size = hb_size_req;
/*! Current allocation size */
size_t ca_size = hb_size;
/*! Memory caps priorites for history buffer */
int hb_mcp[KPB_NO_OF_MEM_POOLS] = {SOF_MEM_CAPS_LP, SOF_MEM_CAPS_HP,
SOF_MEM_CAPS_RAM };
void *new_mem_block = NULL;
size_t temp_ca_size;
int i = 0;
size_t allocated_size = 0;
comp_cl_info(&comp_kpb, "kpb_allocate_history_buffer()");
/* Initialize history buffer */
kpb->hd.c_hb = rzalloc(SOF_MEM_ZONE_RUNTIME, 0, SOF_MEM_CAPS_RAM,
sizeof(struct history_buffer));
if (!kpb->hd.c_hb)
return 0;
kpb->hd.c_hb->next = kpb->hd.c_hb;
kpb->hd.c_hb->prev = kpb->hd.c_hb;
hb = kpb->hd.c_hb;
/* Allocate history buffer/s. KPB history buffer has a size of
* KPB_MAX_BUFFER_SIZE, since there is no single memory block
* that big, we need to allocate couple smaller blocks which
* linked together will form history buffer.
*/
while (hb_size > 0 && i < ARRAY_SIZE(hb_mcp)) {
/* Try to allocate ca_size (current allocation size). At first
* attempt it will be equal to hb_size (history buffer size).
*/
new_mem_block = rballoc(0, hb_mcp[i], ca_size);
if (new_mem_block) {
/* We managed to allocate a block of ca_size.
* Now we initialize it.
*/
comp_cl_info(&comp_kpb, "kpb new memory block: %d",
ca_size);
allocated_size += ca_size;
hb->start_addr = new_mem_block;
hb->end_addr = (char *)new_mem_block +
ca_size;
hb->w_ptr = new_mem_block;
hb->r_ptr = new_mem_block;
hb->state = KPB_BUFFER_FREE;
hb_size -= ca_size;
hb->next = kpb->hd.c_hb;
/* Do we need another buffer? */
if (hb_size > 0) {
/* Yes, we still need at least one more buffer.
* Let's first create new container for it.
*/
new_hb = rzalloc(SOF_MEM_ZONE_RUNTIME, 0,
SOF_MEM_CAPS_RAM,
sizeof(struct history_buffer));
if (!new_hb)
return 0;
hb->next = new_hb;
new_hb->next = kpb->hd.c_hb;
new_hb->state = KPB_BUFFER_OFF;
new_hb->prev = hb;
hb = new_hb;
kpb->hd.c_hb->prev = new_hb;
ca_size = hb_size;
i++;
}
} else {
/* We've failed to allocate ca_size of that hb_mcp
* let's try again with some smaller size.
* NOTE! If we decrement by some small value,
* the allocation will take significant time.
* However, bigger values like
* HEAP_HP_BUFFER_BLOCK_SIZE will result in lower
* accuracy of allocation.
*/
temp_ca_size = ca_size - KPB_ALLOCATION_STEP;
ca_size = (ca_size < temp_ca_size) ? 0 : temp_ca_size;
if (ca_size == 0) {
ca_size = hb_size;
i++;
}
continue;
}
}
comp_cl_info(&comp_kpb, "kpb_allocate_history_buffer(): allocated %d bytes",
allocated_size);
return allocated_size;
}
/**
* \brief Reclaim memory of a history buffer.
* \param[in] buff - pointer to current history buffer.
*
* \return none.
*/
static void kpb_free_history_buffer(struct history_buffer *buff)
{
struct history_buffer *_buff;
struct history_buffer *first_buff = buff;
comp_cl_info(&comp_kpb, "kpb_free_history_buffer()");
if (!buff)
return;
/* Free history buffer/s */
do {
/* First reclaim HB internal memory, then HB itself */
if (buff->start_addr)
rfree(buff->start_addr);
_buff = buff->next;
rfree(buff);
buff = _buff;
} while (buff && buff != first_buff);
}
/**
* \brief Reclaim memory of a key phrase buffer.
* \param[in] dev - component device pointer.
*
* \return none.
*/
static void kpb_free(struct comp_dev *dev)
{
struct comp_data *kpb = comp_get_drvdata(dev);
comp_info(dev, "kpb_free()");
/* Unregister KPB from notifications */
notifier_unregister(dev, NULL, NOTIFIER_ID_KPB_CLIENT_EVT);
/* Reclaim memory occupied by history buffer */
kpb_free_history_buffer(kpb->hd.c_hb);
kpb->hd.c_hb = NULL;
kpb->hd.buffer_size = 0;
/* remove scheduling */
schedule_task_free(&kpb->draining_task);
/* change state */
kpb_change_state(kpb, KPB_STATE_DISABLED);
/* Free KPB */
rfree(kpb);
rfree(dev);
}
/**
* \brief Trigger a change of KPB state.
* \param[in] dev - component device pointer.
* \param[in] cmd - command type.
* \return none.
*/
static int kpb_trigger(struct comp_dev *dev, int cmd)
{
comp_info(dev, "kpb_trigger()");
return comp_set_state(dev, cmd);
}
static int kbp_verify_params(struct comp_dev *dev,
struct sof_ipc_stream_params *params)
{
int ret;
comp_dbg(dev, "kbp_verify_params()");
ret = comp_verify_params(dev, 0, params);
if (ret < 0) {
comp_err(dev, "kpb_verify_params(): comp_verify_params() failed");
return ret;
}
return 0;
}
/**
* \brief KPB params.
* \param[in] dev - component device pointer.
* \param[in] params - pcm params.
* \return none.
*/
static int kpb_params(struct comp_dev *dev,
struct sof_ipc_stream_params *params)
{
struct comp_data *kpb = comp_get_drvdata(dev);
int err;
if (dev->state == COMP_STATE_PREPARE) {
comp_err(dev, "kpb_params(): kpb has been already configured.");
return PPL_STATUS_PATH_STOP;
}
err = kbp_verify_params(dev, params);
if (err < 0) {
comp_err(dev, "kpb_params(): pcm params verification failed");
return -EINVAL;
}
kpb->host_buffer_size = params->buffer.size;
kpb->host_period_size = params->host_period_bytes;
kpb->config.sampling_width = params->sample_container_bytes * 8;
return 0;
}
/**
* \brief Prepare key phrase buffer.
* \param[in] dev - kpb component device pointer.
*
* \return integer representing either:
* 0 -> success
* -EINVAL -> failure.
*/
static int kpb_prepare(struct comp_dev *dev)
{
struct comp_data *kpb = comp_get_drvdata(dev);
int ret = 0;
int i;
struct list_item *blist;
struct comp_buffer *sink;
size_t hb_size_req = KPB_MAX_BUFFER_SIZE(kpb->config.sampling_width);
comp_info(dev, "kpb_prepare()");
if (kpb->state == KPB_STATE_RESETTING ||
kpb->state == KPB_STATE_RESET_FINISHING) {
comp_cl_err(&comp_kpb, "kpb_prepare(): can not prepare KPB due to ongoing reset, state log %x",
kpb->state_log);
return -EBUSY;
}
ret = comp_set_state(dev, COMP_TRIGGER_PREPARE);
if (ret < 0)
return ret;
if (ret == COMP_STATUS_STATE_ALREADY_SET)
return PPL_STATUS_PATH_STOP;
if (!validate_host_params(dev, kpb->host_period_size,
kpb->host_buffer_size, hb_size_req)) {
return -EINVAL;
}
kpb_change_state(kpb, KPB_STATE_PREPARING);
/* Init private data */
kpb->kpb_no_of_clients = 0;
kpb->hd.buffered = 0;
kpb->sel_sink = NULL;
kpb->host_sink = NULL;
if (kpb->hd.c_hb && kpb->hd.buffer_size < hb_size_req) {
/* Host params has changed, we need to allocate new buffer */
kpb_free_history_buffer(kpb->hd.c_hb);
kpb->hd.c_hb = NULL;
}
if (!kpb->hd.c_hb) {
/* Allocate history buffer */
kpb->hd.buffer_size = kpb_allocate_history_buffer(kpb,
hb_size_req);
/* Have we allocated what we requested? */
if (kpb->hd.buffer_size < hb_size_req) {
comp_cl_err(&comp_kpb, "kpb_prepare(): failed to allocate space for KPB buffer");
kpb_free_history_buffer(kpb->hd.c_hb);
kpb->hd.c_hb = NULL;
kpb->hd.buffer_size = 0;
return -EINVAL;
}
}
/* Init history buffer */
kpb_reset_history_buffer(kpb->hd.c_hb);
kpb->hd.free = kpb->hd.buffer_size;
/* Initialize clients data */
for (i = 0; i < KPB_MAX_NO_OF_CLIENTS; i++) {
kpb->clients[i].state = KPB_CLIENT_UNREGISTERED;
kpb->clients[i].r_ptr = NULL;
}
/* Register KPB for notification */
ret = notifier_register(dev, NULL, NOTIFIER_ID_KPB_CLIENT_EVT,
kpb_event_handler, 0);
if (ret < 0) {
kpb_free_history_buffer(kpb->hd.c_hb);
kpb->hd.c_hb = NULL;
return -ENOMEM;
}
/* Search for KPB related sinks.
* NOTE! We assume here that channel selector component device
* is connected to the KPB sinks as well as host device.
*/
list_for_item(blist, &dev->bsink_list) {
sink = container_of(blist, struct comp_buffer, source_list);
if (!sink->sink) {
ret = -EINVAL;
break;
}
if (dev_comp_type(sink->sink) == SOF_COMP_SELECTOR) {
/* We found proper real time sink */
kpb->sel_sink = sink;
} else if (dev_comp_type(sink->sink) == SOF_COMP_HOST) {
/* We found proper host sink */
kpb->host_sink = sink;
}
}
if (!kpb->sel_sink || !kpb->host_sink) {
comp_info(dev, "kpb_prepare(): could not find sinks: sel_sink %p host_sink %p",
kpb->sel_sink, kpb->host_sink);
ret = -EIO;
}
/* Disallow sync_draining_mode for now */
kpb->sync_draining_mode = false;
kpb_change_state(kpb, KPB_STATE_RUN);
return ret;
}
/**
* \brief Used to pass standard and bespoke commands (with data) to component.
* \param[in,out] dev - Volume base component device.
* \param[in] cmd - Command type.
* \param[in,out] data - Control command data.
* \return Error code.
*/
static int kpb_cmd(struct comp_dev *dev, int cmd, void *data,
int max_data_size)
{
return 0;
}
/**
* \brief Resets KPB component.
* \param[in,out] dev KPB base component device.
* \return Error code.
*/
static int kpb_reset(struct comp_dev *dev)
{
struct comp_data *kpb = comp_get_drvdata(dev);
int ret = 0;
int i;
comp_cl_info(&comp_kpb, "kpb_reset(): resetting from state %d, state log %x",
kpb->state, kpb->state_log);
switch (kpb->state) {
case KPB_STATE_BUFFERING:
case KPB_STATE_DRAINING:
/* KPB is performing some task now,
* terminate it gently.
*/
kpb_change_state(kpb, KPB_STATE_RESETTING);
ret = -EBUSY;
break;
case KPB_STATE_DISABLED:
case KPB_STATE_CREATED:
/* Nothing to reset */
ret = comp_set_state(dev, COMP_TRIGGER_RESET);
break;
default:
kpb->hd.buffered = 0;
kpb->sel_sink = NULL;
kpb->host_sink = NULL;
kpb->host_buffer_size = 0;
kpb->host_period_size = 0;
for (i = 0; i < KPB_MAX_NO_OF_CLIENTS; i++) {
kpb->clients[i].state = KPB_CLIENT_UNREGISTERED;
kpb->clients[i].r_ptr = NULL;
}
if (kpb->hd.c_hb) {
/* Reset history buffer - zero its data, reset pointers
* and states.
*/
kpb_reset_history_buffer(kpb->hd.c_hb);
}
/* Unregister KPB from notifications */
notifier_unregister(dev, NULL, NOTIFIER_ID_KPB_CLIENT_EVT);
/* Finally KPB is ready after reset */
kpb_change_state(kpb, KPB_STATE_PREPARING);
ret = comp_set_state(dev, COMP_TRIGGER_RESET);
break;
}
return ret;
}
/**
* \brief Copy real time input stream into sink buffer,
* and in the same time buffers that input for
* later use by some of clients.
*
*\param[in] dev - kpb component device pointer.
*
* \return integer representing either:
* 0 - success
* -EINVAL - failure.
*/
static int kpb_copy(struct comp_dev *dev)
{
int ret = 0;
struct comp_data *kpb = comp_get_drvdata(dev);
struct comp_buffer *source = NULL;
struct comp_buffer *sink = NULL;
size_t copy_bytes = 0;
size_t sample_width = kpb->config.sampling_width;
struct draining_data *dd = &kpb->draining_task_data;
uint32_t avail_bytes;
comp_dbg(dev, "kpb_copy()");
/* Get source and sink buffers */
source = list_first_item(&dev->bsource_list, struct comp_buffer,
sink_list);
if (!source) {
comp_err(dev, "kpb_copy(): no source.");
ret = -EINVAL;
goto out;
}
source = buffer_acquire(source);
/* Validate source */
if (!source->stream.r_ptr) {
comp_err(dev, "kpb_copy(): invalid source pointers.");
ret = -EINVAL;
source = buffer_release(source);
goto out;
}
source = buffer_release(source);
switch (kpb->state) {
case KPB_STATE_RUN:
/* In normal RUN state we simply copy to our sink. */
sink = kpb->sel_sink;
ret = PPL_STATUS_PATH_STOP;
if (!sink) {
comp_err(dev, "kpb_copy(): no sink.");
ret = -EINVAL;
goto out;
}
sink = buffer_acquire(sink);
/* Validate sink */
if (!sink->stream.w_ptr) {
comp_err(dev, "kpb_copy(): invalid selector sink pointers.");
ret = -EINVAL;
sink = buffer_release(sink);
goto out;
}
sink = buffer_release(sink);
copy_bytes = audio_stream_get_copy_bytes(&source->stream, &sink->stream);
if (!copy_bytes) {
comp_err(dev, "kpb_copy(): nothing to copy sink->free %d source->avail %d",
audio_stream_get_free_bytes(&sink->stream),
audio_stream_get_avail_bytes(&source->stream));
ret = PPL_STATUS_PATH_STOP;
goto out;
}
kpb_copy_samples(sink, source, copy_bytes, sample_width);
/* Buffer source data internally in history buffer for future
* use by clients.
*/
if (audio_stream_get_avail_bytes(&source->stream) <= kpb->hd.buffer_size) {
ret = kpb_buffer_data(dev, source, copy_bytes);
if (ret) {
comp_err(dev, "kpb_copy(): internal buffering failed.");
goto out;
} else {
ret = PPL_STATUS_PATH_STOP;
}
/* Update buffered size. NOTE! We only record buffered
* data up to the size of history buffer.
*/
kpb->hd.buffered += MIN(kpb->hd.buffer_size -
kpb->hd.buffered,
copy_bytes);
} else {
comp_err(dev, "kpb_copy(): too much data to buffer.");
}
comp_update_buffer_produce(sink, copy_bytes);
comp_update_buffer_consume(source, copy_bytes);
break;
case KPB_STATE_HOST_COPY:
/* In host copy state we only copy to host buffer. */
sink = kpb->host_sink;
if (!sink) {
comp_err(dev, "kpb_copy(): no sink.");
ret = -EINVAL;
goto out;
}
sink = buffer_acquire(sink);
/* Validate sink */
if (!sink->stream.w_ptr) {
comp_err(dev, "kpb_copy(): invalid host sink pointers.");
ret = -EINVAL;
sink = buffer_release(sink);
goto out;
}
sink = buffer_release(sink);
copy_bytes = audio_stream_get_copy_bytes(&source->stream, &sink->stream);
if (!copy_bytes) {
comp_err(dev, "kpb_copy(): nothing to copy sink->free %d source->avail %d",
audio_stream_get_free_bytes(&sink->stream),
audio_stream_get_avail_bytes(&source->stream));
/* NOTE! We should stop further pipeline copy due to
* no data availability however due to HW bug
* (no HOST DMA IRQs) we need to call host copy
* anyway so it can update its pointers.
*/
goto out;
}
kpb_copy_samples(sink, source, copy_bytes, sample_width);
comp_update_buffer_produce(sink, copy_bytes);
comp_update_buffer_consume(source, copy_bytes);
break;
case KPB_STATE_INIT_DRAINING:
case KPB_STATE_DRAINING:
/* In draining and init draining we only buffer data in
* the internal history buffer.
*/
avail_bytes = audio_stream_get_avail_bytes(&source->stream);
copy_bytes = MIN(avail_bytes, kpb->hd.free);
ret = PPL_STATUS_PATH_STOP;
if (copy_bytes) {
buffer_stream_invalidate(source, copy_bytes);
ret = kpb_buffer_data(dev, source, copy_bytes);
dd->buffered_while_draining += copy_bytes;
kpb->hd.free -= copy_bytes;
if (ret) {
comp_err(dev, "kpb_copy(): internal buffering failed.");
goto out;
}
comp_update_buffer_consume(source, copy_bytes);
} else {
comp_warn(dev, "kpb_copy(): buffering skipped (no data to copy, avail %d, free %d",
audio_stream_get_avail_bytes(&source->stream),
kpb->hd.free);
}
break;
default:
comp_cl_err(&comp_kpb, "kpb_copy(): wrong state (state %d, state log %x)",
kpb->state, kpb->state_log);
ret = -EIO;
break;
}
out:
return ret;
}
/**
* \brief Buffer real time data stream in
* the internal buffer.
*
* \param[in] dev - KPB component data pointer.
* \param[in] source - pointer to the buffer source.
*
*/
static int kpb_buffer_data(struct comp_dev *dev,
const struct comp_buffer *source, size_t size)
{
int ret = 0;
size_t size_to_copy = size;
size_t space_avail;
struct comp_data *kpb = comp_get_drvdata(dev);
struct history_buffer *buff = kpb->hd.c_hb;
uint32_t offset = 0;
uint64_t timeout = 0;
uint64_t current_time;
enum kpb_state state_preserved = kpb->state;
size_t sample_width = kpb->config.sampling_width;
comp_dbg(dev, "kpb_buffer_data()");
/* We are allowed to buffer data in internal history buffer
* only in KPB_STATE_RUN, KPB_STATE_DRAINING or KPB_STATE_INIT_DRAINING
* states.
*/
if (kpb->state != KPB_STATE_RUN &&
kpb->state != KPB_STATE_DRAINING &&
kpb->state != KPB_STATE_INIT_DRAINING) {
comp_err(dev, "kpb_buffer_data(): wrong state! (current state %d, state log %x)",
kpb->state, kpb->state_log);
return PPL_STATUS_PATH_STOP;
}
kpb_change_state(kpb, KPB_STATE_BUFFERING);
timeout = k_cycle_get_64() + k_ms_to_cyc_ceil64(1);
/* Let's store audio stream data in internal history buffer */
while (size_to_copy) {
/* Reset was requested, it's time to stop buffering and finish
* KPB reset.
*/
if (kpb->state == KPB_STATE_RESETTING) {
kpb_change_state(kpb, KPB_STATE_RESET_FINISHING);
kpb_reset(dev);
return PPL_STATUS_PATH_STOP;
}
/* Are we stuck in buffering? */
current_time = k_cycle_get_64();
if (timeout < current_time) {
timeout = k_cyc_to_ms_near64(current_time - timeout);
if (timeout <= UINT_MAX)
comp_err(dev,
"kpb_buffer_data(): timeout of %u [ms] (current state %d, state log %x)",
(unsigned int)(timeout), kpb->state,
kpb->state_log);
else
comp_err(dev,
"kpb_buffer_data(): timeout > %u [ms] (current state %d, state log %x)",
UINT_MAX, kpb->state,
kpb->state_log);
return -ETIME;
}
/* Check how much space there is in current write buffer */
space_avail = (uintptr_t)buff->end_addr - (uintptr_t)buff->w_ptr;
if (size_to_copy > space_avail) {
/* We have more data to copy than available space
* in this buffer, copy what's available and continue
* with next buffer.
*/
kpb_buffer_samples(&source->stream, offset, buff->w_ptr,
space_avail, sample_width);
/* Update write pointer & requested copy size */
buff->w_ptr = (char *)buff->w_ptr + space_avail;
size_to_copy = size_to_copy - space_avail;
/* Update read pointer's offset before continuing
* with next buffer.
*/
offset += space_avail;
} else {
/* Requested size is smaller or equal to the space
* available in this buffer. In this scenario simply
* copy what was requested.
*/
kpb_buffer_samples(&source->stream, offset, buff->w_ptr,
size_to_copy, sample_width);
/* Update write pointer & requested copy size */
buff->w_ptr = (char *)buff->w_ptr + size_to_copy;
/* Reset requested copy size */
size_to_copy = 0;
}
/* Have we filled whole buffer? */
if (buff->w_ptr == buff->end_addr) {
/* Reset write pointer back to the beginning
* of the buffer.
*/
buff->w_ptr = buff->start_addr;
/* If we have more buffers use them */
if (buff->next && buff->next != buff) {
/* Mark current buffer FULL */
buff->state = KPB_BUFFER_FULL;
/* Use next buffer available on the list
* of buffers.
*/
buff = buff->next;
/* Update also component container,
* so next time we enter buffering function
* we will know right away what is the current
* write buffer
*/
kpb->hd.c_hb = buff;
}
/* Mark buffer as FREE */
buff->state = KPB_BUFFER_FREE;
}
}
kpb_change_state(kpb, state_preserved);
return ret;
}
/**
* \brief Main event dispatcher.
* \param[in] arg - KPB component internal data.
* \param[in] type - notification type
* \param[in] event_data - event specific data.
* \return none.
*/
static void kpb_event_handler(void *arg, enum notify_id type, void *event_data)
{
struct comp_dev *dev = arg;
struct comp_data *kpb = comp_get_drvdata(dev);
struct kpb_event_data *evd = event_data;
struct kpb_client *cli = evd->client_data;
comp_info(dev, "kpb_event_handler(): received event with ID: %d ",
evd->event_id);
switch (evd->event_id) {
case KPB_EVENT_REGISTER_CLIENT:
kpb_register_client(kpb, cli);
break;
case KPB_EVENT_UNREGISTER_CLIENT:
/*TODO*/
break;
case KPB_EVENT_BEGIN_DRAINING: