-
Notifications
You must be signed in to change notification settings - Fork 0
/
minivosc.c
893 lines (728 loc) · 25 KB
/
minivosc.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
/*
* Minimal virtual oscillator (minivosc) soundcard
*
* Based on Loopback soundcard (aloop-kernel.c):
* Original code:
* Copyright (c) by Jaroslav Kysela <perex@perex.cz>
*
* More accurate positioning and full-duplex support:
* Copyright (c) Ahmet İnan <ainan at mathematik.uni-freiburg.de>
*
* Major (almost complete) rewrite:
* Copyright (c) by Takashi Iwai <tiwai@suse.de>
*
* with snippets from Ben Collins: Writing an ALSA driver
* http://ben-collins.blogspot.com/2010/04/writing-alsa-driver.html
*
* minivosc specific parts:
* Copyright (c) by Smilen Dimitrov <sd at imi.aau.dk>
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
static int debug = 1;
/* Use our own dbg macro http://www.n1ywb.com/projects/darts/darts-usb/darts-usb.c*/
#undef dbg
#define dbg(format, arg...) do { if (debug) printk(KERN_DEBUG __FILE__ ": " format "\n" , ## arg); } while (0)
#define dbg2(format, arg...) do { if (debug) printk( ": " format "\n" , ## arg); } while (0)
/* Here is our user defined breakpoint to */
/* initiate communication with remote (k)gdb */
/* don't use if not actually using kgdb */
#define BREAKPOINT() asm(" int $3");
// copy from aloop-kernel.c:
#include <linux/init.h>
#include <linux/jiffies.h>
#include <linux/slab.h>
#include <linux/time.h>
#include <linux/wait.h>
#include <linux/moduleparam.h>
#include <linux/platform_device.h>
#include <sound/core.h>
#include <sound/control.h>
#include <sound/pcm.h>
#include <sound/initval.h>
MODULE_AUTHOR("sdaau");
MODULE_DESCRIPTION("maxivosc soundcard");
MODULE_LICENSE("GPL");
MODULE_SUPPORTED_DEVICE("{{ALSA,maxivosc soundcard}}");
static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
static int enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0};
static struct platform_device *devices[SNDRV_CARDS];
#define byte_pos(x) ((x) / HZ)
#define frac_pos(x) ((x) * HZ)
#define MAX_BUFFER (32 * 48 *16)
static struct snd_pcm_hardware minivosc_pcm_hw =
{
.info = (SNDRV_PCM_INFO_MMAP |
SNDRV_PCM_INFO_INTERLEAVED |
SNDRV_PCM_INFO_BLOCK_TRANSFER |
SNDRV_PCM_INFO_MMAP_VALID),
.formats = SNDRV_PCM_FMTBIT_U8,
.rates = SNDRV_PCM_RATE_8000,
.rate_min = 8000,
.rate_max = 8000,
.channels_min = 16,
.channels_max = 16,
.buffer_bytes_max = 32768,//MAX_BUFFER, //(32 * 48 * 16) = 1536 * 16,
.period_bytes_min = 4096,
.period_bytes_max = 32768,
.periods_min = 1,
.periods_max = 32,
};
struct minivosc_device
{
struct snd_card *card;
struct snd_pcm *pcm;
const struct minivosc_pcm_ops *timer_ops;
/*
* we have only one substream, so all data in this struct
*/
/* copied from struct loopback: */
struct mutex cable_lock;
/* copied from struct loopback_cable: */
/* PCM parameters */
unsigned int pcm_period_size;
unsigned int pcm_bps; /* bytes per second */
/* flags */
unsigned int valid;
unsigned int running;
unsigned int period_update_pending :1;
/* timer stuff */
unsigned int irq_pos; /* fractional IRQ position */
unsigned int period_size_frac;
unsigned long last_jiffies;
struct timer_list timer;
/* copied from struct loopback_pcm: */
struct snd_pcm_substream *substream;
unsigned int pcm_buffer_size;
unsigned int buf_pos; /* position in buffer */
unsigned int silent_size;
/* added for waveform: */
unsigned int wvf_pos; /* position in waveform array */
unsigned int wvf_lift; /* lift of waveform array */
};
// waveform
static char wvfdat[21 * 16];
static char wvfdat2[]={ 20, 22, 24, 25, 24, 22, 21,
19, 17, 15, 14, 15, 17, 19,
20, 127, 22, 19, 17, 15, 19};
static unsigned int wvfsz=sizeof(wvfdat);//*sizeof(float) is included already
// * functions for driver/kernel module initialization
static void minivosc_unregister_all(void);
static int __init alsa_card_minivosc_init(void);
static void __exit alsa_card_minivosc_exit(void);
// * declare functions for this struct describing the driver (to be defined later):
static int __devinit minivosc_probe(struct platform_device *devptr);
static int __devexit minivosc_remove(struct platform_device *devptr);
// * here declaration of functions that will need to be in _ops, before they are defined
static int minivosc_hw_params(struct snd_pcm_substream *ss,
struct snd_pcm_hw_params *hw_params);
static int minivosc_hw_free(struct snd_pcm_substream *ss);
static int minivosc_pcm_open(struct snd_pcm_substream *ss);
static int minivosc_pcm_close(struct snd_pcm_substream *ss);
static int minivosc_pcm_prepare(struct snd_pcm_substream *ss);
static int minivosc_pcm_trigger(struct snd_pcm_substream *ss,
int cmd);
static snd_pcm_uframes_t minivosc_pcm_pointer(struct snd_pcm_substream *ss);
static int minivosc_pcm_dev_free(struct snd_device *device);
static int minivosc_pcm_free(struct minivosc_device *chip);
// * declare timer functions - copied from aloop-kernel.c
static void minivosc_timer_start(struct minivosc_device *mydev);
static void minivosc_timer_stop(struct minivosc_device *mydev);
static void minivosc_pos_update(struct minivosc_device *mydev);
static void minivosc_timer_function(unsigned long data);
static void minivosc_xfer_buf(struct minivosc_device *mydev, unsigned int count);
static void minivosc_fill_capture_buf(struct minivosc_device *mydev, unsigned int bytes);
// note snd_pcm_ops can usually be separate _playback_ops and _capture_ops
static struct snd_pcm_ops minivosc_pcm_ops =
{
.open = minivosc_pcm_open,
.close = minivosc_pcm_close,
.ioctl = snd_pcm_lib_ioctl,
.hw_params = minivosc_hw_params,
.hw_free = minivosc_hw_free,
.prepare = minivosc_pcm_prepare,
.trigger = minivosc_pcm_trigger,
.pointer = minivosc_pcm_pointer,
};
// specifies what func is called @ snd_card_free
// used in snd_device_new
static struct snd_device_ops dev_ops =
{
.dev_free = minivosc_pcm_dev_free,
};
#define SND_MINIVOSC_DRIVER "snd_maxivosc"
// * we need a struct describing the driver:
static struct platform_driver minivosc_driver =
{
.probe = minivosc_probe,
.remove = __devexit_p(minivosc_remove),
//~ #ifdef CONFIG_PM
//~ .suspend = minivosc_suspend,
//~ .resume = minivosc_resume,
//~ #endif
.driver = {
.name = SND_MINIVOSC_DRIVER,
.owner = THIS_MODULE
},
};
/*
*
* Probe/remove functions
*
*/
static int __devinit minivosc_probe(struct platform_device *devptr)
{
struct snd_card *card;
struct minivosc_device *mydev;
int ret;
int nr_subdevs; // how many capture substreams we want
struct snd_pcm *pcm;
int dev = devptr->id; // from aloop-kernel.c
dbg("%s: probe", __func__);
// no need to kzalloc minivosc_device separately, if the sizeof is included here
ret = snd_card_create(index[dev], id[dev],
THIS_MODULE, sizeof(struct minivosc_device), &card);
if (ret < 0)
goto __nodev;
mydev = card->private_data;
mydev->card = card;
// MUST have mutex_init here - else crash on mutex_lock!!
mutex_init(&mydev->cable_lock);
dbg2("-- mydev %p", mydev);
sprintf(card->driver, "my_driver-%s", SND_MINIVOSC_DRIVER);
sprintf(card->shortname, "MySoundCard Audio %s", SND_MINIVOSC_DRIVER);
sprintf(card->longname, "%s", card->shortname);
snd_card_set_dev(card, &devptr->dev); // present in dummy, not in aloop though
ret = snd_device_new(card, SNDRV_DEV_LOWLEVEL, mydev, &dev_ops);
if (ret < 0)
goto __nodev;
nr_subdevs = 1; // how many capture substreams we want
// * we want 0 playback, and 1 capture substreams (4th and 5th arg) ..
ret = snd_pcm_new(card, card->driver, 0, 0, nr_subdevs, &pcm);
if (ret < 0)
goto __nodev;
snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &minivosc_pcm_ops); // in both aloop-kernel.c and dummy.c, after snd_pcm_new...
pcm->private_data = mydev; //here it should be dev/card struct (the one containing struct snd_card *card) - this DOES NOT end up in substream->private_data
pcm->info_flags = 0;
strcpy(pcm->name, card->shortname);
/*
trid to add this - but it crashes here:
//mydev->substream->private_data = mydev;
Well, first time real substream comes in, is in _open - so
that has to be handled there.. That is: at this point, mydev->substream is null,
and we first have a chance to set it ... in _open!
*/
ret = snd_pcm_lib_preallocate_pages_for_all(pcm,
SNDRV_DMA_TYPE_CONTINUOUS,
snd_dma_continuous_data(GFP_KERNEL),
MAX_BUFFER, MAX_BUFFER); // in both aloop-kernel.c and dummy.c, after snd_pcm_set_ops...
if (ret < 0)
goto __nodev;
// * will use the snd_card_register form from aloop-kernel.c/dummy.c here..
ret = snd_card_register(card);
if (ret == 0) // or... (!ret)
{
platform_set_drvdata(devptr, card);
return 0; // success
}
__nodev: // as in aloop/dummy...
dbg("__nodev reached!!");
snd_card_free(card); // this will autocall .dev_free (= minivosc_pcm_dev_free)
return ret;
}
// from dummy/aloop:
static int __devexit minivosc_remove(struct platform_device *devptr)
{
dbg("%s", __func__);
snd_card_free(platform_get_drvdata(devptr));
platform_set_drvdata(devptr, NULL);
return 0;
}
/*
*
* hw alloc/free functions
*
*/
static int minivosc_hw_params(struct snd_pcm_substream *ss,
struct snd_pcm_hw_params *hw_params)
{
dbg("%s", __func__);
return snd_pcm_lib_malloc_pages(ss,
params_buffer_bytes(hw_params));
}
static int minivosc_hw_free(struct snd_pcm_substream *ss)
{
dbg("%s", __func__);
return snd_pcm_lib_free_pages(ss);
}
/*
*
* PCM functions
*
*/
static int minivosc_pcm_open(struct snd_pcm_substream *ss)
{
struct minivosc_device *mydev = ss->private_data;
//BREAKPOINT();
dbg("%s", __func__);
// copied from aloop-kernel.c:
mutex_lock(&mydev->cable_lock);
ss->runtime->hw = minivosc_pcm_hw;
mydev->substream = ss; //save (system given) substream *ss, in our structure field
ss->runtime->private_data = mydev;
mydev->wvf_pos = 0; //init
mydev->wvf_lift = 0; //init
// SETUP THE TIMER HERE:
setup_timer(&mydev->timer, minivosc_timer_function,
(unsigned long)mydev);
mutex_unlock(&mydev->cable_lock);
return 0;
}
static int minivosc_pcm_close(struct snd_pcm_substream *ss)
{
struct minivosc_device *mydev = ss->private_data;
dbg("%s", __func__);
// copied from aloop-kernel.c:
// * even though mutexes are retrieved from ss->private_data,
// * which will be set to null,
// * lock the mutex here anyway:
mutex_lock(&mydev->cable_lock);
// * not much else to do here, but set to null:
ss->private_data = NULL;
mutex_unlock(&mydev->cable_lock);
return 0;
}
static int minivosc_pcm_prepare(struct snd_pcm_substream *ss)
{
// copied from aloop-kernel.c
// for one, we could get mydev from ss->private_data...
// here we try it via ss->runtime->private_data instead.
// turns out, this type of call via runtime->private_data
// ends up with mydev as null pointer causing SIGSEGV
// .. UNLESS runtime->private_data is assigned in _open?
struct snd_pcm_runtime *runtime = ss->runtime;
struct minivosc_device *mydev = runtime->private_data;
unsigned int bps;
dbg("%s", __func__);
bps = runtime->rate * runtime->channels; // params requested by user app (arecord, audacity)
bps *= snd_pcm_format_width(runtime->format);
bps /= 8;
if (bps <= 0)
return -EINVAL;
mydev->buf_pos = 0;
mydev->pcm_buffer_size = frames_to_bytes(runtime, runtime->buffer_size);
dbg2(" bps: %u; runtime->buffer_size: %lu; mydev->pcm_buffer_size: %u", bps, runtime->buffer_size, mydev->pcm_buffer_size);
if (ss->stream == SNDRV_PCM_STREAM_CAPTURE) {
/* clear capture buffer */
mydev->silent_size = mydev->pcm_buffer_size;
//memset(runtime->dma_area, 0, mydev->pcm_buffer_size);
// we're in char land here, so let's mark prepare buffer with value 45 (signature)
// this turns out to set everything permanently throughout - not just first buffer,
// even though it runs only at start?
memset(runtime->dma_area, 45, mydev->pcm_buffer_size);
}
if (!mydev->running) {
mydev->irq_pos = 0;
mydev->period_update_pending = 0;
}
mutex_lock(&mydev->cable_lock);
if (!(mydev->valid & ~(1 << ss->stream))) {
mydev->pcm_bps = bps;
mydev->pcm_period_size =
frames_to_bytes(runtime, runtime->period_size);
mydev->period_size_frac = frac_pos(mydev->pcm_period_size);
}
mydev->valid |= 1 << ss->stream;
mutex_unlock(&mydev->cable_lock);
dbg2(" pcm_period_size: %u; period_size_frac: %u", mydev->pcm_period_size, mydev->period_size_frac);
return 0;
}
static int minivosc_pcm_trigger(struct snd_pcm_substream *ss,
int cmd)
{
int ret = 0;
//copied from aloop-kernel.c
//here we do not get mydev from
// ss->runtime->private_data; but from:
struct minivosc_device *mydev = ss->private_data;
dbg("%s - trig %d", __func__, cmd);
switch (cmd)
{
case SNDRV_PCM_TRIGGER_START:
// Start the hardware capture
// from aloop-kernel.c:
if (!mydev->running) {
mydev->last_jiffies = jiffies;
// SET OFF THE TIMER HERE:
minivosc_timer_start(mydev);
}
mydev->running |= (1 << ss->stream);
break;
case SNDRV_PCM_TRIGGER_STOP:
// Stop the hardware capture
// from aloop-kernel.c:
mydev->running &= ~(1 << ss->stream);
if (!mydev->running)
// STOP THE TIMER HERE:
minivosc_timer_stop(mydev);
break;
default:
ret = -EINVAL;
}
return ret;
}
static snd_pcm_uframes_t minivosc_pcm_pointer(struct snd_pcm_substream *ss)
{
//copied from aloop-kernel.c
struct snd_pcm_runtime *runtime = ss->runtime;
struct minivosc_device *mydev= runtime->private_data;
dbg2("+minivosc_pointer ");
minivosc_pos_update(mydev);
dbg2("+ bytes_to_frames(: %lu, mydev->buf_pos: %d", bytes_to_frames(runtime, mydev->buf_pos),mydev->buf_pos);
return bytes_to_frames(runtime, mydev->buf_pos);
}
/*
*
* Timer functions
*
*/
static void minivosc_timer_start(struct minivosc_device *mydev)
{
unsigned long tick;
dbg2("minivosc_timer_start: mydev->period_size_frac: %u; mydev->irq_pos: %u jiffies: %lu pcm_bps %u", mydev->period_size_frac, mydev->irq_pos, jiffies, mydev->pcm_bps);
tick = mydev->period_size_frac - mydev->irq_pos;
tick = (tick + mydev->pcm_bps - 1) / mydev->pcm_bps;
mydev->timer.expires = jiffies + tick;
add_timer(&mydev->timer);
}
static void minivosc_timer_stop(struct minivosc_device *mydev)
{
dbg2("minivosc_timer_stop");
del_timer(&mydev->timer);
}
static void minivosc_pos_update(struct minivosc_device *mydev)
{
unsigned int last_pos, count;
unsigned long delta;
if (!mydev->running)
return;
dbg2("*minivosc_pos_update: running ");
delta = jiffies - mydev->last_jiffies;
dbg2("* : jiffies %lu, ->last_jiffies %lu, delta %lu", jiffies, mydev->last_jiffies, delta);
if (!delta)
return;
mydev->last_jiffies += delta;
last_pos = byte_pos(mydev->irq_pos);
mydev->irq_pos += delta * mydev->pcm_bps;
count = byte_pos(mydev->irq_pos) - last_pos;
dbg2("* : last_pos %d, c->irq_pos %d, count %d", last_pos, mydev->irq_pos, count);
if (!count)
return;
// FILL BUFFER HERE
minivosc_xfer_buf(mydev, count);
if (mydev->irq_pos >= mydev->period_size_frac)
{
dbg2("* : mydev->irq_pos >= mydev->period_size_frac %d", mydev->period_size_frac);
mydev->irq_pos %= mydev->period_size_frac;
mydev->period_update_pending = 1;
}
}
static void minivosc_timer_function(unsigned long data)
{
struct minivosc_device *mydev = (struct minivosc_device *)data;
if (!mydev->running)
return;
dbg2("minivosc_timer_function: running ");
minivosc_pos_update(mydev);
// SET OFF THE TIMER HERE:
minivosc_timer_start(mydev);
if (mydev->period_update_pending)
{
mydev->period_update_pending = 0;
if (mydev->running)
{
dbg2(" : calling snd_pcm_period_elapsed");
snd_pcm_period_elapsed(mydev->substream);
}
}
}
#define CABLE_PLAYBACK (1 << SNDRV_PCM_STREAM_PLAYBACK)
#define CABLE_CAPTURE (1 << SNDRV_PCM_STREAM_CAPTURE)
#define CABLE_BOTH (CABLE_PLAYBACK | CABLE_CAPTURE)
// choose which copy (fill) algorithm to use -
// (un)comment as needed, though use only one at a time!
#define COPYALG_V1
//~ #define COPYALG_V2
//~ #define COPYALG_V3
#define BUFFERMARKS // do we want 'buffer mark' samples or not
static void minivosc_xfer_buf(struct minivosc_device *mydev, unsigned int count)
{
dbg2(">minivosc_xfer_buf: count: %d ", count );
switch (mydev->running) {
case CABLE_CAPTURE:
minivosc_fill_capture_buf(mydev, count);
break;
}
if (mydev->running) {
// activate this buf_pos calculation, either if V3 is defined,
// or if no COPYALG is defined (which also handles lone BUFFERMARKS)
#if defined(COPYALG_V3) || !(defined(COPYALG_V1) || defined(COPYALG_V2) || defined(COPYALG_V3))
// here the (auto)increase of buf_pos is handled
mydev->buf_pos += count;
mydev->buf_pos %= mydev->pcm_buffer_size;
dbg2("> : mydev->buf_pos: %d ", mydev->buf_pos); // */
#endif
}
}
static void minivosc_fill_capture_buf(struct minivosc_device *mydev, unsigned int bytes)
{
char *dst = mydev->substream->runtime->dma_area;
unsigned int dst_off = mydev->buf_pos; // buf_pos is in bytes, not in samples !
float wrdat; // was char - value to fill silent_size with
unsigned int dpos = 0; //added
#if defined(COPYALG_V1) || defined(COPYALG_V2)
int i = 0; //added
int mylift = 0; //added
#endif
#if defined(COPYALG_V1)
unsigned int remain = 0; //added
unsigned int remain2 = 0; //added
unsigned int wvftocopy = 0; //added
#endif
#if defined(COPYALG_V2)
int j = 0; //added
#endif
#if defined(COPYALG_V3)
#endif
dbg2("_ minivosc_fill_capture_buf ss %d bs %d bytes %d buf_pos %d sizeof %d jiffies %lu", mydev->silent_size, mydev->pcm_buffer_size, bytes, dst_off, sizeof(*dst), jiffies);
#if defined(COPYALG_V1)
// loop v1.. fill waveform until end of 'bytes'..
// using memcpy for copying/filling
//*
while (dpos < bytes-1)
{
mylift = mydev->wvf_lift*10 - 10;
// create modified - 'lifted' - values of waveform:
for (i=0; i<wvfsz;) {
wvfdat[i] = wvfdat2[i%21]+mylift; i++;
wvfdat[i] = wvfdat2[i%21]+mylift; i++;
wvfdat[i] = wvfdat2[i%21]+mylift; i++;
wvfdat[i] = wvfdat2[i%21]+mylift; i++;
wvfdat[i] = wvfdat2[i%21]+mylift; i++;
wvfdat[i] = wvfdat2[i%21]+mylift; i++;
wvfdat[i] = wvfdat2[i%21]+mylift; i++;
wvfdat[i] = wvfdat2[i%21]+mylift; i++;
wvfdat[i] = wvfdat2[i%21]+mylift; i++;
wvfdat[i] = wvfdat2[i%21]+mylift; i++;
wvfdat[i] = wvfdat2[i%21]+mylift; i++;
wvfdat[i] = wvfdat2[i%21]+mylift; i++;
wvfdat[i] = wvfdat2[i%21]+mylift; i++;
wvfdat[i] = wvfdat2[i%21]+mylift; i++;
wvfdat[i] = wvfdat2[i%21]+mylift; i++;
}
remain = bytes - dpos;
remain2 = mydev->pcm_buffer_size - mydev->buf_pos;
if (remain < wvfsz) wvftocopy = remain; //not wvfsz - remain!
if (remain2 < wvfsz) wvftocopy = remain2; //also see if "big" PCM buffer wraps!
else wvftocopy = wvfsz;
if (mydev->wvf_pos > 0) wvftocopy -= mydev->wvf_pos;
dbg2("::: buf_pos %d; dpos %d; wvf_pos %d; wvftocopy %d; remain %d; remain2 %d; wvfsz %d; wvf_lift %d", mydev->buf_pos, dpos, mydev->wvf_pos, wvftocopy, remain, remain2, wvfsz, mydev->wvf_lift);
memcpy(dst + mydev->buf_pos, &wvfdat[mydev->wvf_pos], wvftocopy);
dpos += wvftocopy;
mydev->buf_pos += wvftocopy; //added if there isn't (auto)increase of buf_pos in xfer_buf
mydev->wvf_pos += wvftocopy;
if (mydev->wvf_pos >= wvfsz) { // we should wrap waveform here..
mydev->wvf_pos -= wvfsz;
// also handle lift here..
mydev->wvf_lift++;
if (mydev->wvf_lift >=4) mydev->wvf_lift = 0;
}
//added if there isn't (auto)increase of buf_pos in xfer_buf
// there may be some misalignments here still, though...
if (mydev->buf_pos >= mydev->pcm_buffer_size) {
mydev->buf_pos = 0;
break; //we don;t really need this.. but maybe here...?
}
if (dpos >= bytes-1) break;
} // end loop v1 */
#endif //defined(COPYALG_V1)
#if defined(COPYALG_V2)
// hmm... for this loop, v2, I was getting prepare signature (45), if
// mydev->buf_pos autoincrements (wraps) in minivosc_xfer_buf ;
// however, for more correct, we calculate 'buf_pos' here instead..
// using direct assignment of elements for copying/filling
//*
for (j=0; j<bytes; j++) {
mylift = mydev->wvf_lift*10 - 10;
for (i=0; i<sizeof(wvfdat); i++) {
wvfdat[i] = wvfdat2[i]+mylift;
}
dst[mydev->buf_pos] = wvfdat[mydev->wvf_pos];
dpos++; mydev->buf_pos++;
mydev->wvf_pos++;
if (mydev->wvf_pos >= wvfsz) { // we should wrap waveform here..
mydev->wvf_pos = 0;
// also handle lift here..
mydev->wvf_lift++;
if (mydev->wvf_lift >=4) mydev->wvf_lift = 0;
}
if (mydev->buf_pos >= mydev->pcm_buffer_size) {
mydev->buf_pos = 0;
//break; //we don;t really need this
}
if (dpos >= bytes) break;
} // end loop v2 */
#endif //defined(COPYALG_V2)
#if defined(COPYALG_V3)
// as in copy_play_buf in aloop-kernel.c, where we had:
//~ char *src = play->substream->runtime->dma_area;
//~ char *dst = capt->substream->runtime->dma_area;
// 'buf_pos' here is calculated in _xfer_buf, and
// the waveform wrapping is not correct
// using memcpy for copying/filling
for (;;) {
unsigned int size = bytes;
if (mydev->wvf_pos + size > wvfsz)
size = wvfsz - mydev->wvf_pos;
if (dst_off + size > mydev->pcm_buffer_size)
size = mydev->pcm_buffer_size - dst_off;
memcpy(dst + dst_off, wvfdat + mydev->wvf_pos, size);
if (size < mydev->silent_size)
mydev->silent_size -= size;
else
mydev->silent_size = 0;
bytes -= size;
if (!bytes)
break;
mydev->wvf_pos = (mydev->wvf_pos + size) % wvfsz;
dst_off = (dst_off + size) % mydev->pcm_buffer_size;
}
#endif //defined(COPYALG_V3)
#if defined(BUFFERMARKS)
//* //set buffer marks
//-------------
//these two shouldn't change in repeated calls of this func:
memset(dst+1, 160, 1); // mark start of pcm buffer
memset(dst + mydev->pcm_buffer_size - 2, 140, 1); // mark end of pcm buffer
memset(dst + dst_off, 120, 1); // mark start of this fill_capture_buf.
if (dst_off==0) memset(dst + dst_off, 250, 1); // different mark if offset is zero
// note - if marking end at dst + dst_off + bytes, it gets overwritten by next run
memset(dst + dst_off + bytes - 2, 90, 1); // mark end fill_capture_buf.
// end set buffer marks */
#endif //defined(BUFFERMARKS)
if (mydev->silent_size >= mydev->pcm_buffer_size)
return;
// NOTE: usually, the code returns by now -
// - it doesn't even execute past this point!
// from here on, apparently silent_size should be handled..
if (mydev->silent_size + bytes > mydev->pcm_buffer_size)
bytes = mydev->pcm_buffer_size - mydev->silent_size;
wrdat = -0.2; // value to copy, instead of 0 for silence (if needed)
for (;;) {
unsigned int size = bytes;
dpos = 0; //added
dbg2("_ clearrr.. %d", bytes);
if (dst_off + size > mydev->pcm_buffer_size)
size = mydev->pcm_buffer_size - dst_off;
//memset(dst + dst_off, 255, size); //0, size);
while (dpos < size)
{
memcpy(dst + dst_off + dpos, &wrdat, sizeof(wrdat));
dpos += sizeof(wrdat);
if (dpos >= size) break;
}
mydev->silent_size += size;
bytes -= size;
if (!bytes)
break;
dst_off = 0;
}
}
/*
*
* snd_device_ops free functions
*
*/
// these should eventually get called by snd_card_free (via .dev_free)
// however, since we do no special allocations, we need not free anything
static int minivosc_pcm_free(struct minivosc_device *chip)
{
dbg("%s", __func__);
return 0;
}
static int minivosc_pcm_dev_free(struct snd_device *device)
{
dbg("%s", __func__);
return minivosc_pcm_free(device->device_data);
}
/*
*
* functions for driver/kernel module initialization
* (_init, _exit)
* copied from aloop-kernel.c (same in dummy.c)
*
*/
static void minivosc_unregister_all(void)
{
int i;
dbg("%s", __func__);
for (i = 0; i < ARRAY_SIZE(devices); ++i)
platform_device_unregister(devices[i]);
platform_driver_unregister(&minivosc_driver);
}
static int __init alsa_card_minivosc_init(void)
{
int i, err, cards;
dbg("%s", __func__);
err = platform_driver_register(&minivosc_driver);
if (err < 0)
return err;
cards = 0;
for (i = 0; i < SNDRV_CARDS; i++)
{
struct platform_device *device;
if (!enable[i])
continue;
device = platform_device_register_simple(SND_MINIVOSC_DRIVER,
i, NULL, 0);
if (IS_ERR(device))
continue;
if (!platform_get_drvdata(device))
{
platform_device_unregister(device);
continue;
}
devices[i] = device;
cards++;
}
if (!cards)
{
#ifdef MODULE
printk(KERN_ERR "minivosc-alsa: No enabled, not found or device busy\n");
#endif
minivosc_unregister_all();
return -ENODEV;
}
return 0;
}
static void __exit alsa_card_minivosc_exit(void)
{
dbg("%s", __func__);
minivosc_unregister_all();
}
module_init(alsa_card_minivosc_init)
module_exit(alsa_card_minivosc_exit)