forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathasihpi.c
2980 lines (2524 loc) · 80.9 KB
/
asihpi.c
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: GPL-2.0-only
/*
* Asihpi soundcard
* Copyright (c) by AudioScience Inc <[email protected]>
*
* The following is not a condition of use, merely a request:
* If you modify this program, particularly if you fix errors, AudioScience Inc
* would appreciate it if you grant us the right to use those modifications
* for any purpose including commercial applications.
*/
#include "hpi_internal.h"
#include "hpi_version.h"
#include "hpimsginit.h"
#include "hpioctl.h"
#include "hpicmn.h"
#include <linux/pci.h>
#include <linux/init.h>
#include <linux/jiffies.h>
#include <linux/slab.h>
#include <linux/time.h>
#include <linux/wait.h>
#include <linux/module.h>
#include <sound/core.h>
#include <sound/control.h>
#include <sound/pcm.h>
#include <sound/pcm_params.h>
#include <sound/info.h>
#include <sound/initval.h>
#include <sound/tlv.h>
#include <sound/hwdep.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("AudioScience inc. <[email protected]>");
MODULE_DESCRIPTION("AudioScience ALSA ASI5xxx ASI6xxx ASI87xx ASI89xx "
HPI_VER_STRING);
#if defined CONFIG_SND_DEBUG_VERBOSE
/**
* snd_printddd - very verbose debug printk
* @format: format string
*
* Works like snd_printk() for debugging purposes.
* Ignored when CONFIG_SND_DEBUG_VERBOSE is not set.
* Must set snd module debug parameter to 3 to enable at runtime.
*/
#define snd_printddd(format, args...) \
__snd_printk(3, __FILE__, __LINE__, format, ##args)
#else
#define snd_printddd(format, args...) do { } while (0)
#endif
static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* index 0-MAX */
static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
static bool enable_hpi_hwdep = 1;
module_param_array(index, int, NULL, 0444);
MODULE_PARM_DESC(index, "ALSA index value for AudioScience soundcard.");
module_param_array(id, charp, NULL, 0444);
MODULE_PARM_DESC(id, "ALSA ID string for AudioScience soundcard.");
module_param_array(enable, bool, NULL, 0444);
MODULE_PARM_DESC(enable, "ALSA enable AudioScience soundcard.");
module_param(enable_hpi_hwdep, bool, 0644);
MODULE_PARM_DESC(enable_hpi_hwdep,
"ALSA enable HPI hwdep for AudioScience soundcard ");
/* identify driver */
#ifdef KERNEL_ALSA_BUILD
static char *build_info = "Built using headers from kernel source";
module_param(build_info, charp, 0444);
MODULE_PARM_DESC(build_info, "Built using headers from kernel source");
#else
static char *build_info = "Built within ALSA source";
module_param(build_info, charp, 0444);
MODULE_PARM_DESC(build_info, "Built within ALSA source");
#endif
/* set to 1 to dump every control from adapter to log */
static const int mixer_dump;
#define DEFAULT_SAMPLERATE 44100
static int adapter_fs = DEFAULT_SAMPLERATE;
/* defaults */
#define PERIODS_MIN 2
#define PERIOD_BYTES_MIN 2048
#define BUFFER_BYTES_MAX (512 * 1024)
#define MAX_CLOCKSOURCES (HPI_SAMPLECLOCK_SOURCE_LAST + 1 + 7)
struct clk_source {
int source;
int index;
const char *name;
};
struct clk_cache {
int count;
int has_local;
struct clk_source s[MAX_CLOCKSOURCES];
};
/* Per card data */
struct snd_card_asihpi {
struct snd_card *card;
struct pci_dev *pci;
struct hpi_adapter *hpi;
/* In low latency mode there is only one stream, a pointer to its
* private data is stored here on trigger and cleared on stop.
* The interrupt handler uses it as a parameter when calling
* snd_card_asihpi_timer_function().
*/
struct snd_card_asihpi_pcm *llmode_streampriv;
void (*pcm_start)(struct snd_pcm_substream *substream);
void (*pcm_stop)(struct snd_pcm_substream *substream);
u32 h_mixer;
struct clk_cache cc;
u16 can_dma;
u16 support_grouping;
u16 support_mrx;
u16 update_interval_frames;
u16 in_max_chans;
u16 out_max_chans;
u16 in_min_chans;
u16 out_min_chans;
};
/* Per stream data */
struct snd_card_asihpi_pcm {
struct timer_list timer;
unsigned int respawn_timer;
unsigned int hpi_buffer_attached;
unsigned int buffer_bytes;
unsigned int period_bytes;
unsigned int bytes_per_sec;
unsigned int pcm_buf_host_rw_ofs; /* Host R/W pos */
unsigned int pcm_buf_dma_ofs; /* DMA R/W offset in buffer */
unsigned int pcm_buf_elapsed_dma_ofs; /* DMA R/W offset in buffer */
unsigned int drained_count;
struct snd_pcm_substream *substream;
u32 h_stream;
struct hpi_format format;
};
/* universal stream verbs work with out or in stream handles */
/* Functions to allow driver to give a buffer to HPI for busmastering */
static u16 hpi_stream_host_buffer_attach(
u32 h_stream, /* handle to outstream. */
u32 size_in_bytes, /* size in bytes of bus mastering buffer */
u32 pci_address
)
{
struct hpi_message hm;
struct hpi_response hr;
unsigned int obj = hpi_handle_object(h_stream);
if (!h_stream)
return HPI_ERROR_INVALID_OBJ;
hpi_init_message_response(&hm, &hr, obj,
obj == HPI_OBJ_OSTREAM ?
HPI_OSTREAM_HOSTBUFFER_ALLOC :
HPI_ISTREAM_HOSTBUFFER_ALLOC);
hpi_handle_to_indexes(h_stream, &hm.adapter_index,
&hm.obj_index);
hm.u.d.u.buffer.buffer_size = size_in_bytes;
hm.u.d.u.buffer.pci_address = pci_address;
hm.u.d.u.buffer.command = HPI_BUFFER_CMD_INTERNAL_GRANTADAPTER;
hpi_send_recv(&hm, &hr);
return hr.error;
}
static u16 hpi_stream_host_buffer_detach(u32 h_stream)
{
struct hpi_message hm;
struct hpi_response hr;
unsigned int obj = hpi_handle_object(h_stream);
if (!h_stream)
return HPI_ERROR_INVALID_OBJ;
hpi_init_message_response(&hm, &hr, obj,
obj == HPI_OBJ_OSTREAM ?
HPI_OSTREAM_HOSTBUFFER_FREE :
HPI_ISTREAM_HOSTBUFFER_FREE);
hpi_handle_to_indexes(h_stream, &hm.adapter_index,
&hm.obj_index);
hm.u.d.u.buffer.command = HPI_BUFFER_CMD_INTERNAL_REVOKEADAPTER;
hpi_send_recv(&hm, &hr);
return hr.error;
}
static inline u16 hpi_stream_start(u32 h_stream)
{
if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
return hpi_outstream_start(h_stream);
else
return hpi_instream_start(h_stream);
}
static inline u16 hpi_stream_stop(u32 h_stream)
{
if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
return hpi_outstream_stop(h_stream);
else
return hpi_instream_stop(h_stream);
}
static inline u16 hpi_stream_get_info_ex(
u32 h_stream,
u16 *pw_state,
u32 *pbuffer_size,
u32 *pdata_in_buffer,
u32 *psample_count,
u32 *pauxiliary_data
)
{
u16 e;
if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
e = hpi_outstream_get_info_ex(h_stream, pw_state,
pbuffer_size, pdata_in_buffer,
psample_count, pauxiliary_data);
else
e = hpi_instream_get_info_ex(h_stream, pw_state,
pbuffer_size, pdata_in_buffer,
psample_count, pauxiliary_data);
return e;
}
static inline u16 hpi_stream_group_add(
u32 h_master,
u32 h_stream)
{
if (hpi_handle_object(h_master) == HPI_OBJ_OSTREAM)
return hpi_outstream_group_add(h_master, h_stream);
else
return hpi_instream_group_add(h_master, h_stream);
}
static inline u16 hpi_stream_group_reset(u32 h_stream)
{
if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
return hpi_outstream_group_reset(h_stream);
else
return hpi_instream_group_reset(h_stream);
}
static u16 handle_error(u16 err, int line, char *filename)
{
if (err)
printk(KERN_WARNING
"in file %s, line %d: HPI error %d\n",
filename, line, err);
return err;
}
#define hpi_handle_error(x) handle_error(x, __LINE__, __FILE__)
/***************************** GENERAL PCM ****************/
static void print_hwparams(struct snd_pcm_substream *substream,
struct snd_pcm_hw_params *p)
{
char name[16];
snd_pcm_debug_name(substream, name, sizeof(name));
snd_printdd("%s HWPARAMS\n", name);
snd_printdd(" samplerate=%dHz channels=%d format=%d subformat=%d\n",
params_rate(p), params_channels(p),
params_format(p), params_subformat(p));
snd_printdd(" buffer=%dB period=%dB period_size=%dB periods=%d\n",
params_buffer_bytes(p), params_period_bytes(p),
params_period_size(p), params_periods(p));
snd_printdd(" buffer_size=%d access=%d data_rate=%dB/s\n",
params_buffer_size(p), params_access(p),
params_rate(p) * params_channels(p) *
snd_pcm_format_width(params_format(p)) / 8);
}
#define INVALID_FORMAT (__force snd_pcm_format_t)(-1)
static const snd_pcm_format_t hpi_to_alsa_formats[] = {
INVALID_FORMAT, /* INVALID */
SNDRV_PCM_FORMAT_U8, /* HPI_FORMAT_PCM8_UNSIGNED 1 */
SNDRV_PCM_FORMAT_S16, /* HPI_FORMAT_PCM16_SIGNED 2 */
INVALID_FORMAT, /* HPI_FORMAT_MPEG_L1 3 */
SNDRV_PCM_FORMAT_MPEG, /* HPI_FORMAT_MPEG_L2 4 */
SNDRV_PCM_FORMAT_MPEG, /* HPI_FORMAT_MPEG_L3 5 */
INVALID_FORMAT, /* HPI_FORMAT_DOLBY_AC2 6 */
INVALID_FORMAT, /* HPI_FORMAT_DOLBY_AC3 7 */
SNDRV_PCM_FORMAT_S16_BE,/* HPI_FORMAT_PCM16_BIGENDIAN 8 */
INVALID_FORMAT, /* HPI_FORMAT_AA_TAGIT1_HITS 9 */
INVALID_FORMAT, /* HPI_FORMAT_AA_TAGIT1_INSERTS 10 */
SNDRV_PCM_FORMAT_S32, /* HPI_FORMAT_PCM32_SIGNED 11 */
INVALID_FORMAT, /* HPI_FORMAT_RAW_BITSTREAM 12 */
INVALID_FORMAT, /* HPI_FORMAT_AA_TAGIT1_HITS_EX1 13 */
SNDRV_PCM_FORMAT_FLOAT, /* HPI_FORMAT_PCM32_FLOAT 14 */
#if 1
/* ALSA can't handle 3 byte sample size together with power-of-2
* constraint on buffer_bytes, so disable this format
*/
INVALID_FORMAT
#else
/* SNDRV_PCM_FORMAT_S24_3LE */ /* HPI_FORMAT_PCM24_SIGNED 15 */
#endif
};
static int snd_card_asihpi_format_alsa2hpi(snd_pcm_format_t alsa_format,
u16 *hpi_format)
{
u16 format;
for (format = HPI_FORMAT_PCM8_UNSIGNED;
format <= HPI_FORMAT_PCM24_SIGNED; format++) {
if (hpi_to_alsa_formats[format] == alsa_format) {
*hpi_format = format;
return 0;
}
}
snd_printd(KERN_WARNING "failed match for alsa format %d\n",
alsa_format);
*hpi_format = 0;
return -EINVAL;
}
static void snd_card_asihpi_pcm_samplerates(struct snd_card_asihpi *asihpi,
struct snd_pcm_hardware *pcmhw)
{
u16 err;
u32 h_control;
u32 sample_rate;
int idx;
unsigned int rate_min = 200000;
unsigned int rate_max = 0;
unsigned int rates = 0;
if (asihpi->support_mrx) {
rates |= SNDRV_PCM_RATE_CONTINUOUS;
rates |= SNDRV_PCM_RATE_8000_96000;
rate_min = 8000;
rate_max = 100000;
} else {
/* on cards without SRC,
valid rates are determined by sampleclock */
err = hpi_mixer_get_control(asihpi->h_mixer,
HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
HPI_CONTROL_SAMPLECLOCK, &h_control);
if (err) {
dev_err(&asihpi->pci->dev,
"No local sampleclock, err %d\n", err);
}
for (idx = -1; idx < 100; idx++) {
if (idx == -1) {
if (hpi_sample_clock_get_sample_rate(h_control,
&sample_rate))
continue;
} else if (hpi_sample_clock_query_local_rate(h_control,
idx, &sample_rate)) {
break;
}
rate_min = min(rate_min, sample_rate);
rate_max = max(rate_max, sample_rate);
switch (sample_rate) {
case 5512:
rates |= SNDRV_PCM_RATE_5512;
break;
case 8000:
rates |= SNDRV_PCM_RATE_8000;
break;
case 11025:
rates |= SNDRV_PCM_RATE_11025;
break;
case 16000:
rates |= SNDRV_PCM_RATE_16000;
break;
case 22050:
rates |= SNDRV_PCM_RATE_22050;
break;
case 32000:
rates |= SNDRV_PCM_RATE_32000;
break;
case 44100:
rates |= SNDRV_PCM_RATE_44100;
break;
case 48000:
rates |= SNDRV_PCM_RATE_48000;
break;
case 64000:
rates |= SNDRV_PCM_RATE_64000;
break;
case 88200:
rates |= SNDRV_PCM_RATE_88200;
break;
case 96000:
rates |= SNDRV_PCM_RATE_96000;
break;
case 176400:
rates |= SNDRV_PCM_RATE_176400;
break;
case 192000:
rates |= SNDRV_PCM_RATE_192000;
break;
default: /* some other rate */
rates |= SNDRV_PCM_RATE_KNOT;
}
}
}
pcmhw->rates = rates;
pcmhw->rate_min = rate_min;
pcmhw->rate_max = rate_max;
}
static int snd_card_asihpi_pcm_hw_params(struct snd_pcm_substream *substream,
struct snd_pcm_hw_params *params)
{
struct snd_pcm_runtime *runtime = substream->runtime;
struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
int err;
u16 format;
int width;
unsigned int bytes_per_sec;
print_hwparams(substream, params);
err = snd_card_asihpi_format_alsa2hpi(params_format(params), &format);
if (err)
return err;
hpi_handle_error(hpi_format_create(&dpcm->format,
params_channels(params),
format, params_rate(params), 0, 0));
if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
if (hpi_instream_reset(dpcm->h_stream) != 0)
return -EINVAL;
if (hpi_instream_set_format(
dpcm->h_stream, &dpcm->format) != 0)
return -EINVAL;
}
dpcm->hpi_buffer_attached = 0;
if (card->can_dma) {
err = hpi_stream_host_buffer_attach(dpcm->h_stream,
params_buffer_bytes(params), runtime->dma_addr);
if (err == 0) {
snd_printdd(
"stream_host_buffer_attach success %u %lu\n",
params_buffer_bytes(params),
(unsigned long)runtime->dma_addr);
} else {
snd_printd("stream_host_buffer_attach error %d\n",
err);
return -ENOMEM;
}
err = hpi_stream_get_info_ex(dpcm->h_stream, NULL,
&dpcm->hpi_buffer_attached, NULL, NULL, NULL);
}
bytes_per_sec = params_rate(params) * params_channels(params);
width = snd_pcm_format_width(params_format(params));
bytes_per_sec *= width;
bytes_per_sec /= 8;
if (width < 0 || bytes_per_sec == 0)
return -EINVAL;
dpcm->bytes_per_sec = bytes_per_sec;
dpcm->buffer_bytes = params_buffer_bytes(params);
dpcm->period_bytes = params_period_bytes(params);
return 0;
}
static int
snd_card_asihpi_hw_free(struct snd_pcm_substream *substream)
{
struct snd_pcm_runtime *runtime = substream->runtime;
struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
if (dpcm->hpi_buffer_attached)
hpi_stream_host_buffer_detach(dpcm->h_stream);
return 0;
}
static void snd_card_asihpi_runtime_free(struct snd_pcm_runtime *runtime)
{
struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
kfree(dpcm);
}
static void snd_card_asihpi_pcm_timer_start(struct snd_pcm_substream *
substream)
{
struct snd_pcm_runtime *runtime = substream->runtime;
struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
int expiry;
expiry = HZ / 200;
expiry = max(expiry, 1); /* don't let it be zero! */
mod_timer(&dpcm->timer, jiffies + expiry);
dpcm->respawn_timer = 1;
}
static void snd_card_asihpi_pcm_timer_stop(struct snd_pcm_substream *substream)
{
struct snd_pcm_runtime *runtime = substream->runtime;
struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
dpcm->respawn_timer = 0;
del_timer(&dpcm->timer);
}
static void snd_card_asihpi_pcm_int_start(struct snd_pcm_substream *substream)
{
struct snd_card_asihpi_pcm *dpcm;
struct snd_card_asihpi *card;
dpcm = (struct snd_card_asihpi_pcm *)substream->runtime->private_data;
card = snd_pcm_substream_chip(substream);
WARN_ON(in_interrupt());
card->llmode_streampriv = dpcm;
hpi_handle_error(hpi_adapter_set_property(card->hpi->adapter->index,
HPI_ADAPTER_PROPERTY_IRQ_RATE,
card->update_interval_frames, 0));
}
static void snd_card_asihpi_pcm_int_stop(struct snd_pcm_substream *substream)
{
struct snd_card_asihpi *card;
card = snd_pcm_substream_chip(substream);
hpi_handle_error(hpi_adapter_set_property(card->hpi->adapter->index,
HPI_ADAPTER_PROPERTY_IRQ_RATE, 0, 0));
card->llmode_streampriv = NULL;
}
static int snd_card_asihpi_trigger(struct snd_pcm_substream *substream,
int cmd)
{
struct snd_card_asihpi_pcm *dpcm = substream->runtime->private_data;
struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
struct snd_pcm_substream *s;
u16 e;
char name[16];
snd_pcm_debug_name(substream, name, sizeof(name));
switch (cmd) {
case SNDRV_PCM_TRIGGER_START:
snd_printdd("%s trigger start\n", name);
snd_pcm_group_for_each_entry(s, substream) {
struct snd_pcm_runtime *runtime = s->runtime;
struct snd_card_asihpi_pcm *ds = runtime->private_data;
if (snd_pcm_substream_chip(s) != card)
continue;
/* don't link Cap and Play */
if (substream->stream != s->stream)
continue;
ds->drained_count = 0;
if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
/* How do I know how much valid data is present
* in buffer? Must be at least one period!
* Guessing 2 periods, but if
* buffer is bigger it may contain even more
* data??
*/
unsigned int preload = ds->period_bytes * 1;
snd_printddd("%d preload %d\n", s->number, preload);
hpi_handle_error(hpi_outstream_write_buf(
ds->h_stream,
&runtime->dma_area[0],
preload,
&ds->format));
ds->pcm_buf_host_rw_ofs = preload;
}
if (card->support_grouping) {
snd_printdd("%d group\n", s->number);
e = hpi_stream_group_add(
dpcm->h_stream,
ds->h_stream);
if (!e) {
snd_pcm_trigger_done(s, substream);
} else {
hpi_handle_error(e);
break;
}
} else
break;
}
/* start the master stream */
card->pcm_start(substream);
if ((substream->stream == SNDRV_PCM_STREAM_CAPTURE) ||
!card->can_dma)
hpi_handle_error(hpi_stream_start(dpcm->h_stream));
break;
case SNDRV_PCM_TRIGGER_STOP:
snd_printdd("%s trigger stop\n", name);
card->pcm_stop(substream);
snd_pcm_group_for_each_entry(s, substream) {
if (snd_pcm_substream_chip(s) != card)
continue;
/* don't link Cap and Play */
if (substream->stream != s->stream)
continue;
/*? workaround linked streams don't
transition to SETUP 20070706*/
s->runtime->status->state = SNDRV_PCM_STATE_SETUP;
if (card->support_grouping) {
snd_printdd("%d group\n", s->number);
snd_pcm_trigger_done(s, substream);
} else
break;
}
/* _prepare and _hwparams reset the stream */
hpi_handle_error(hpi_stream_stop(dpcm->h_stream));
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
hpi_handle_error(
hpi_outstream_reset(dpcm->h_stream));
if (card->support_grouping)
hpi_handle_error(hpi_stream_group_reset(dpcm->h_stream));
break;
case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
snd_printdd("%s trigger pause release\n", name);
card->pcm_start(substream);
hpi_handle_error(hpi_stream_start(dpcm->h_stream));
break;
case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
snd_printdd("%s trigger pause push\n", name);
card->pcm_stop(substream);
hpi_handle_error(hpi_stream_stop(dpcm->h_stream));
break;
default:
snd_printd(KERN_ERR "\tINVALID\n");
return -EINVAL;
}
return 0;
}
/*algorithm outline
Without linking degenerates to getting single stream pos etc
Without mmap 2nd loop degenerates to snd_pcm_period_elapsed
*/
/*
pcm_buf_dma_ofs=get_buf_pos(s);
for_each_linked_stream(s) {
pcm_buf_dma_ofs=get_buf_pos(s);
min_buf_pos = modulo_min(min_buf_pos, pcm_buf_dma_ofs, buffer_bytes)
new_data = min(new_data, calc_new_data(pcm_buf_dma_ofs,irq_pos)
}
timer.expires = jiffies + predict_next_period_ready(min_buf_pos);
for_each_linked_stream(s) {
s->pcm_buf_dma_ofs = min_buf_pos;
if (new_data > period_bytes) {
if (mmap) {
irq_pos = (irq_pos + period_bytes) % buffer_bytes;
if (playback) {
write(period_bytes);
} else {
read(period_bytes);
}
}
snd_pcm_period_elapsed(s);
}
}
*/
/** Minimum of 2 modulo values. Works correctly when the difference between
* the values is less than half the modulus
*/
static inline unsigned int modulo_min(unsigned int a, unsigned int b,
unsigned long int modulus)
{
unsigned int result;
if (((a-b) % modulus) < (modulus/2))
result = b;
else
result = a;
return result;
}
/** Timer function, equivalent to interrupt service routine for cards
*/
static void snd_card_asihpi_timer_function(struct timer_list *t)
{
struct snd_card_asihpi_pcm *dpcm = from_timer(dpcm, t, timer);
struct snd_pcm_substream *substream = dpcm->substream;
struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
struct snd_pcm_runtime *runtime;
struct snd_pcm_substream *s;
unsigned int newdata = 0;
unsigned int pcm_buf_dma_ofs, min_buf_pos = 0;
unsigned int remdata, xfercount, next_jiffies;
int first = 1;
int loops = 0;
u16 state;
u32 buffer_size, bytes_avail, samples_played, on_card_bytes;
char name[16];
snd_pcm_debug_name(substream, name, sizeof(name));
/* find minimum newdata and buffer pos in group */
snd_pcm_group_for_each_entry(s, substream) {
struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
runtime = s->runtime;
if (snd_pcm_substream_chip(s) != card)
continue;
/* don't link Cap and Play */
if (substream->stream != s->stream)
continue;
hpi_handle_error(hpi_stream_get_info_ex(
ds->h_stream, &state,
&buffer_size, &bytes_avail,
&samples_played, &on_card_bytes));
/* number of bytes in on-card buffer */
runtime->delay = on_card_bytes;
if (!card->can_dma)
on_card_bytes = bytes_avail;
if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
pcm_buf_dma_ofs = ds->pcm_buf_host_rw_ofs - bytes_avail;
if (state == HPI_STATE_STOPPED) {
if (bytes_avail == 0) {
hpi_handle_error(hpi_stream_start(ds->h_stream));
snd_printdd("P%d start\n", s->number);
ds->drained_count = 0;
}
} else if (state == HPI_STATE_DRAINED) {
snd_printd(KERN_WARNING "P%d drained\n",
s->number);
ds->drained_count++;
if (ds->drained_count > 20) {
snd_pcm_stop_xrun(s);
continue;
}
} else {
ds->drained_count = 0;
}
} else
pcm_buf_dma_ofs = bytes_avail + ds->pcm_buf_host_rw_ofs;
if (first) {
/* can't statically init min when wrap is involved */
min_buf_pos = pcm_buf_dma_ofs;
newdata = (pcm_buf_dma_ofs - ds->pcm_buf_elapsed_dma_ofs) % ds->buffer_bytes;
first = 0;
} else {
min_buf_pos =
modulo_min(min_buf_pos, pcm_buf_dma_ofs, UINT_MAX+1L);
newdata = min(
(pcm_buf_dma_ofs - ds->pcm_buf_elapsed_dma_ofs) % ds->buffer_bytes,
newdata);
}
snd_printddd(
"timer1, %s, %d, S=%d, elap=%d, rw=%d, dsp=%d, left=%d, aux=%d, space=%d, hw_ptr=%ld, appl_ptr=%ld\n",
name, s->number, state,
ds->pcm_buf_elapsed_dma_ofs,
ds->pcm_buf_host_rw_ofs,
pcm_buf_dma_ofs,
(int)bytes_avail,
(int)on_card_bytes,
buffer_size-bytes_avail,
(unsigned long)frames_to_bytes(runtime,
runtime->status->hw_ptr),
(unsigned long)frames_to_bytes(runtime,
runtime->control->appl_ptr)
);
loops++;
}
pcm_buf_dma_ofs = min_buf_pos;
remdata = newdata % dpcm->period_bytes;
xfercount = newdata - remdata; /* a multiple of period_bytes */
/* come back when on_card_bytes has decreased enough to allow
write to happen, or when data has been consumed to make another
period
*/
if (xfercount && (on_card_bytes > dpcm->period_bytes))
next_jiffies = ((on_card_bytes - dpcm->period_bytes) * HZ / dpcm->bytes_per_sec);
else
next_jiffies = ((dpcm->period_bytes - remdata) * HZ / dpcm->bytes_per_sec);
next_jiffies = max(next_jiffies, 1U);
dpcm->timer.expires = jiffies + next_jiffies;
snd_printddd("timer2, jif=%d, buf_pos=%d, newdata=%d, xfer=%d\n",
next_jiffies, pcm_buf_dma_ofs, newdata, xfercount);
snd_pcm_group_for_each_entry(s, substream) {
struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
/* don't link Cap and Play */
if (substream->stream != s->stream)
continue;
/* Store dma offset for use by pointer callback */
ds->pcm_buf_dma_ofs = pcm_buf_dma_ofs;
if (xfercount &&
/* Limit use of on card fifo for playback */
((on_card_bytes <= ds->period_bytes) ||
(s->stream == SNDRV_PCM_STREAM_CAPTURE)))
{
unsigned int buf_ofs = ds->pcm_buf_host_rw_ofs % ds->buffer_bytes;
unsigned int xfer1, xfer2;
char *pd = &s->runtime->dma_area[buf_ofs];
if (card->can_dma) { /* buffer wrap is handled at lower level */
xfer1 = xfercount;
xfer2 = 0;
} else {
xfer1 = min(xfercount, ds->buffer_bytes - buf_ofs);
xfer2 = xfercount - xfer1;
}
if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
snd_printddd("write1, P=%d, xfer=%d, buf_ofs=%d\n",
s->number, xfer1, buf_ofs);
hpi_handle_error(
hpi_outstream_write_buf(
ds->h_stream, pd, xfer1,
&ds->format));
if (xfer2) {
pd = s->runtime->dma_area;
snd_printddd("write2, P=%d, xfer=%d, buf_ofs=%d\n",
s->number,
xfercount - xfer1, buf_ofs);
hpi_handle_error(
hpi_outstream_write_buf(
ds->h_stream, pd,
xfercount - xfer1,
&ds->format));
}
} else {
snd_printddd("read1, C=%d, xfer=%d\n",
s->number, xfer1);
hpi_handle_error(
hpi_instream_read_buf(
ds->h_stream,
pd, xfer1));
if (xfer2) {
pd = s->runtime->dma_area;
snd_printddd("read2, C=%d, xfer=%d\n",
s->number, xfer2);
hpi_handle_error(
hpi_instream_read_buf(
ds->h_stream,
pd, xfer2));
}
}
/* ? host_rw_ofs always ahead of elapsed_dma_ofs by preload size? */
ds->pcm_buf_host_rw_ofs += xfercount;
ds->pcm_buf_elapsed_dma_ofs += xfercount;
snd_pcm_period_elapsed(s);
}
}
if (!card->hpi->interrupt_mode && dpcm->respawn_timer)
add_timer(&dpcm->timer);
}
static void snd_card_asihpi_isr(struct hpi_adapter *a)
{
struct snd_card_asihpi *asihpi;
WARN_ON(!a || !a->snd_card || !a->snd_card->private_data);
asihpi = (struct snd_card_asihpi *)a->snd_card->private_data;
if (asihpi->llmode_streampriv)
snd_card_asihpi_timer_function(
&asihpi->llmode_streampriv->timer);
}
/***************************** PLAYBACK OPS ****************/
static int snd_card_asihpi_playback_prepare(struct snd_pcm_substream *
substream)
{
struct snd_pcm_runtime *runtime = substream->runtime;
struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
snd_printdd("P%d prepare\n", substream->number);
hpi_handle_error(hpi_outstream_reset(dpcm->h_stream));
dpcm->pcm_buf_host_rw_ofs = 0;
dpcm->pcm_buf_dma_ofs = 0;
dpcm->pcm_buf_elapsed_dma_ofs = 0;
return 0;
}
static snd_pcm_uframes_t
snd_card_asihpi_playback_pointer(struct snd_pcm_substream *substream)
{
struct snd_pcm_runtime *runtime = substream->runtime;
struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
snd_pcm_uframes_t ptr;
char name[16];
snd_pcm_debug_name(substream, name, sizeof(name));
ptr = bytes_to_frames(runtime, dpcm->pcm_buf_dma_ofs % dpcm->buffer_bytes);
snd_printddd("%s, pointer=%ld\n", name, (unsigned long)ptr);
return ptr;
}
static u64 snd_card_asihpi_playback_formats(struct snd_card_asihpi *asihpi,
u32 h_stream)
{
struct hpi_format hpi_format;
u16 format;
u16 err;
u32 h_control;
u32 sample_rate = 48000;
u64 formats = 0;
/* on cards without SRC, must query at valid rate,
* maybe set by external sync
*/
err = hpi_mixer_get_control(asihpi->h_mixer,
HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
HPI_CONTROL_SAMPLECLOCK, &h_control);
if (!err)
err = hpi_sample_clock_get_sample_rate(h_control,
&sample_rate);
for (format = HPI_FORMAT_PCM8_UNSIGNED;
format <= HPI_FORMAT_PCM24_SIGNED; format++) {
err = hpi_format_create(&hpi_format, asihpi->out_max_chans,
format, sample_rate, 128000, 0);
if (!err)
err = hpi_outstream_query_format(h_stream, &hpi_format);
if (!err && (hpi_to_alsa_formats[format] != INVALID_FORMAT))
formats |= pcm_format_to_bits(hpi_to_alsa_formats[format]);
}
return formats;
}
static int snd_card_asihpi_playback_open(struct snd_pcm_substream *substream)
{
struct snd_pcm_runtime *runtime = substream->runtime;
struct snd_card_asihpi_pcm *dpcm;
struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
struct snd_pcm_hardware snd_card_asihpi_playback;
int err;
dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
if (dpcm == NULL)
return -ENOMEM;
err = hpi_outstream_open(card->hpi->adapter->index,
substream->number, &dpcm->h_stream);
hpi_handle_error(err);
if (err)
kfree(dpcm);
if (err == HPI_ERROR_OBJ_ALREADY_OPEN)
return -EBUSY;
if (err)
return -EIO;