1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#include <linux/init.h>
20#include <linux/slab.h>
21#include <linux/time.h>
22#include <linux/wait.h>
23#include <linux/module.h>
24#include <sound/core.h>
25#include <sound/control.h>
26#include <sound/pcm.h>
27#include <sound/pcm_params.h>
28#include <sound/initval.h>
29#include <linux/interrupt.h>
30#include <linux/vmalloc.h>
31
32#include "saa7134.h"
33#include "saa7134-reg.h"
34
35static unsigned int debug;
36module_param(debug, int, 0644);
37MODULE_PARM_DESC(debug,"enable debug messages [alsa]");
38
39
40
41
42
43
44#define MIXER_ADDR_UNSELECTED -1
45#define MIXER_ADDR_TVTUNER 0
46#define MIXER_ADDR_LINE1 1
47#define MIXER_ADDR_LINE2 2
48#define MIXER_ADDR_LAST 2
49
50
51static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
52static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
53static int enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 1};
54
55module_param_array(index, int, NULL, 0444);
56module_param_array(enable, int, NULL, 0444);
57MODULE_PARM_DESC(index, "Index value for SAA7134 capture interface(s).");
58MODULE_PARM_DESC(enable, "Enable (or not) the SAA7134 capture interface(s).");
59
60#define dprintk(fmt, arg...) if (debug) \
61 printk(KERN_DEBUG "%s/alsa: " fmt, dev->name , ##arg)
62
63
64
65
66
67
68
69typedef struct snd_card_saa7134 {
70 struct snd_card *card;
71 spinlock_t mixer_lock;
72 int mixer_volume[MIXER_ADDR_LAST+1][2];
73 int capture_source_addr;
74 int capture_source[2];
75 struct snd_kcontrol *capture_ctl[MIXER_ADDR_LAST+1];
76 struct pci_dev *pci;
77 struct saa7134_dev *dev;
78
79 unsigned long iobase;
80 s16 irq;
81 u16 mute_was_on;
82
83 spinlock_t lock;
84} snd_card_saa7134_t;
85
86
87
88
89
90
91typedef struct snd_card_saa7134_pcm {
92 struct saa7134_dev *dev;
93
94 spinlock_t lock;
95
96 struct snd_pcm_substream *substream;
97} snd_card_saa7134_pcm_t;
98
99static struct snd_card *snd_saa7134_cards[SNDRV_CARDS];
100
101
102
103
104
105
106
107
108
109
110
111static void saa7134_dma_stop(struct saa7134_dev *dev)
112{
113 dev->dmasound.dma_blk = -1;
114 dev->dmasound.dma_running = 0;
115 saa7134_set_dmabits(dev);
116}
117
118
119
120
121
122
123
124
125
126
127static void saa7134_dma_start(struct saa7134_dev *dev)
128{
129 dev->dmasound.dma_blk = 0;
130 dev->dmasound.dma_running = 1;
131 saa7134_set_dmabits(dev);
132}
133
134
135
136
137
138
139
140
141
142
143
144
145static void saa7134_irq_alsa_done(struct saa7134_dev *dev,
146 unsigned long status)
147{
148 int next_blk, reg = 0;
149
150 spin_lock(&dev->slock);
151 if (UNSET == dev->dmasound.dma_blk) {
152 dprintk("irq: recording stopped\n");
153 goto done;
154 }
155 if (0 != (status & 0x0f000000))
156 dprintk("irq: lost %ld\n", (status >> 24) & 0x0f);
157 if (0 == (status & 0x10000000)) {
158
159 if (0 == (dev->dmasound.dma_blk & 0x01))
160 reg = SAA7134_RS_BA1(6);
161 } else {
162
163 if (1 == (dev->dmasound.dma_blk & 0x01))
164 reg = SAA7134_RS_BA2(6);
165 }
166 if (0 == reg) {
167 dprintk("irq: field oops [%s]\n",
168 (status & 0x10000000) ? "even" : "odd");
169 goto done;
170 }
171
172 if (dev->dmasound.read_count >= dev->dmasound.blksize * (dev->dmasound.blocks-2)) {
173 dprintk("irq: overrun [full=%d/%d] - Blocks in %d\n",dev->dmasound.read_count,
174 dev->dmasound.bufsize, dev->dmasound.blocks);
175 spin_unlock(&dev->slock);
176 snd_pcm_stream_lock(dev->dmasound.substream);
177 snd_pcm_stop(dev->dmasound.substream,SNDRV_PCM_STATE_XRUN);
178 snd_pcm_stream_unlock(dev->dmasound.substream);
179 return;
180 }
181
182
183 next_blk = (dev->dmasound.dma_blk + 2) % dev->dmasound.blocks;
184 saa_writel(reg,next_blk * dev->dmasound.blksize);
185 if (debug > 2)
186 dprintk("irq: ok, %s, next_blk=%d, addr=%x, blocks=%u, size=%u, read=%u\n",
187 (status & 0x10000000) ? "even" : "odd ", next_blk,
188 next_blk * dev->dmasound.blksize, dev->dmasound.blocks, dev->dmasound.blksize, dev->dmasound.read_count);
189
190
191 dev->dmasound.dma_blk = (dev->dmasound.dma_blk + 1) % dev->dmasound.blocks;
192 dev->dmasound.read_count += dev->dmasound.blksize;
193
194 dev->dmasound.recording_on = reg;
195
196 if (dev->dmasound.read_count >= snd_pcm_lib_period_bytes(dev->dmasound.substream)) {
197 spin_unlock(&dev->slock);
198 snd_pcm_period_elapsed(dev->dmasound.substream);
199 spin_lock(&dev->slock);
200 }
201
202 done:
203 spin_unlock(&dev->slock);
204
205}
206
207
208
209
210
211
212
213
214
215static irqreturn_t saa7134_alsa_irq(int irq, void *dev_id)
216{
217 struct saa7134_dmasound *dmasound = dev_id;
218 struct saa7134_dev *dev = dmasound->priv_data;
219
220 unsigned long report, status;
221 int loop, handled = 0;
222
223 for (loop = 0; loop < 10; loop++) {
224 report = saa_readl(SAA7134_IRQ_REPORT);
225 status = saa_readl(SAA7134_IRQ_STATUS);
226
227 if (report & SAA7134_IRQ_REPORT_DONE_RA3) {
228 handled = 1;
229 saa_writel(SAA7134_IRQ_REPORT,
230 SAA7134_IRQ_REPORT_DONE_RA3);
231 saa7134_irq_alsa_done(dev, status);
232 } else {
233 goto out;
234 }
235 }
236
237 if (loop == 10) {
238 dprintk("error! looping IRQ!");
239 }
240
241out:
242 return IRQ_RETVAL(handled);
243}
244
245
246
247
248
249
250
251
252
253
254
255static int snd_card_saa7134_capture_trigger(struct snd_pcm_substream * substream,
256 int cmd)
257{
258 struct snd_pcm_runtime *runtime = substream->runtime;
259 snd_card_saa7134_pcm_t *pcm = runtime->private_data;
260 struct saa7134_dev *dev=pcm->dev;
261 int err = 0;
262
263 spin_lock(&dev->slock);
264 if (cmd == SNDRV_PCM_TRIGGER_START) {
265
266 saa7134_dma_start(dev);
267 } else if (cmd == SNDRV_PCM_TRIGGER_STOP) {
268
269 saa7134_dma_stop(dev);
270 } else {
271 err = -EINVAL;
272 }
273 spin_unlock(&dev->slock);
274
275 return err;
276}
277
278static int saa7134_alsa_dma_init(struct saa7134_dev *dev, int nr_pages)
279{
280 struct saa7134_dmasound *dma = &dev->dmasound;
281 struct page *pg;
282 int i;
283
284 dma->vaddr = vmalloc_32(nr_pages << PAGE_SHIFT);
285 if (NULL == dma->vaddr) {
286 dprintk("vmalloc_32(%d pages) failed\n", nr_pages);
287 return -ENOMEM;
288 }
289
290 dprintk("vmalloc is at addr 0x%08lx, size=%d\n",
291 (unsigned long)dma->vaddr,
292 nr_pages << PAGE_SHIFT);
293
294 memset(dma->vaddr, 0, nr_pages << PAGE_SHIFT);
295 dma->nr_pages = nr_pages;
296
297 dma->sglist = vzalloc(dma->nr_pages * sizeof(*dma->sglist));
298 if (NULL == dma->sglist)
299 goto vzalloc_err;
300
301 sg_init_table(dma->sglist, dma->nr_pages);
302 for (i = 0; i < dma->nr_pages; i++) {
303 pg = vmalloc_to_page(dma->vaddr + i * PAGE_SIZE);
304 if (NULL == pg)
305 goto vmalloc_to_page_err;
306 sg_set_page(&dma->sglist[i], pg, PAGE_SIZE, 0);
307 }
308 return 0;
309
310vmalloc_to_page_err:
311 vfree(dma->sglist);
312 dma->sglist = NULL;
313vzalloc_err:
314 vfree(dma->vaddr);
315 dma->vaddr = NULL;
316 return -ENOMEM;
317}
318
319static int saa7134_alsa_dma_map(struct saa7134_dev *dev)
320{
321 struct saa7134_dmasound *dma = &dev->dmasound;
322
323 dma->sglen = dma_map_sg(&dev->pci->dev, dma->sglist,
324 dma->nr_pages, PCI_DMA_FROMDEVICE);
325
326 if (0 == dma->sglen) {
327 pr_warn("%s: saa7134_alsa_map_sg failed\n", __func__);
328 return -ENOMEM;
329 }
330 return 0;
331}
332
333static int saa7134_alsa_dma_unmap(struct saa7134_dev *dev)
334{
335 struct saa7134_dmasound *dma = &dev->dmasound;
336
337 if (!dma->sglen)
338 return 0;
339
340 dma_unmap_sg(&dev->pci->dev, dma->sglist, dma->sglen, PCI_DMA_FROMDEVICE);
341 dma->sglen = 0;
342 return 0;
343}
344
345static int saa7134_alsa_dma_free(struct saa7134_dmasound *dma)
346{
347 vfree(dma->sglist);
348 dma->sglist = NULL;
349 vfree(dma->vaddr);
350 dma->vaddr = NULL;
351 return 0;
352}
353
354
355
356
357
358
359
360
361
362
363
364
365static int dsp_buffer_init(struct saa7134_dev *dev)
366{
367 int err;
368
369 BUG_ON(!dev->dmasound.bufsize);
370
371 err = saa7134_alsa_dma_init(dev,
372 (dev->dmasound.bufsize + PAGE_SIZE) >> PAGE_SHIFT);
373 if (0 != err)
374 return err;
375 return 0;
376}
377
378
379
380
381
382
383
384
385static int dsp_buffer_free(struct saa7134_dev *dev)
386{
387 BUG_ON(!dev->dmasound.blksize);
388
389 saa7134_alsa_dma_free(&dev->dmasound);
390
391 dev->dmasound.blocks = 0;
392 dev->dmasound.blksize = 0;
393 dev->dmasound.bufsize = 0;
394
395 return 0;
396}
397
398
399
400
401static int snd_saa7134_capsrc_set(struct snd_kcontrol *kcontrol,
402 int left, int right, bool force_notify)
403{
404 snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
405 int change = 0, addr = kcontrol->private_value;
406 int active, old_addr;
407 u32 anabar, xbarin;
408 int analog_io, rate;
409 struct saa7134_dev *dev;
410
411 dev = chip->dev;
412
413 spin_lock_irq(&chip->mixer_lock);
414
415 active = left != 0 || right != 0;
416 old_addr = chip->capture_source_addr;
417
418
419 if (active) {
420 change = old_addr != addr ||
421 chip->capture_source[0] != left ||
422 chip->capture_source[1] != right;
423
424 chip->capture_source[0] = left;
425 chip->capture_source[1] = right;
426 chip->capture_source_addr = addr;
427 dev->dmasound.input = addr;
428 }
429 spin_unlock_irq(&chip->mixer_lock);
430
431 if (change) {
432 switch (dev->pci->device) {
433
434 case PCI_DEVICE_ID_PHILIPS_SAA7134:
435 switch (addr) {
436 case MIXER_ADDR_TVTUNER:
437 saa_andorb(SAA7134_AUDIO_FORMAT_CTRL,
438 0xc0, 0xc0);
439 saa_andorb(SAA7134_SIF_SAMPLE_FREQ,
440 0x03, 0x00);
441 break;
442 case MIXER_ADDR_LINE1:
443 case MIXER_ADDR_LINE2:
444 analog_io = (MIXER_ADDR_LINE1 == addr) ?
445 0x00 : 0x08;
446 rate = (32000 == dev->dmasound.rate) ?
447 0x01 : 0x03;
448 saa_andorb(SAA7134_ANALOG_IO_SELECT,
449 0x08, analog_io);
450 saa_andorb(SAA7134_AUDIO_FORMAT_CTRL,
451 0xc0, 0x80);
452 saa_andorb(SAA7134_SIF_SAMPLE_FREQ,
453 0x03, rate);
454 break;
455 }
456
457 break;
458 case PCI_DEVICE_ID_PHILIPS_SAA7133:
459 case PCI_DEVICE_ID_PHILIPS_SAA7135:
460 xbarin = 0x03;
461 anabar = 0;
462 switch (addr) {
463 case MIXER_ADDR_TVTUNER:
464 xbarin = 0;
465 anabar = 2;
466 break;
467 case MIXER_ADDR_LINE1:
468 anabar = 0;
469 break;
470 case MIXER_ADDR_LINE2:
471 anabar = 9;
472 break;
473 }
474
475
476 saa_dsp_writel(dev, SAA7133_DIGITAL_OUTPUT_SEL1,
477 0xbbbb10);
478
479 if (left || right) {
480
481 saa_dsp_writel(dev, SAA7133_DIGITAL_INPUT_XBAR1,
482 xbarin);
483 saa_writel(SAA7133_ANALOG_IO_SELECT, anabar);
484 } else {
485 saa_dsp_writel(dev, SAA7133_DIGITAL_INPUT_XBAR1,
486 0);
487 saa_writel(SAA7133_ANALOG_IO_SELECT, 0);
488 }
489 break;
490 }
491 }
492
493 if (change) {
494 if (force_notify)
495 snd_ctl_notify(chip->card,
496 SNDRV_CTL_EVENT_MASK_VALUE,
497 &chip->capture_ctl[addr]->id);
498
499 if (old_addr != MIXER_ADDR_UNSELECTED && old_addr != addr)
500 snd_ctl_notify(chip->card,
501 SNDRV_CTL_EVENT_MASK_VALUE,
502 &chip->capture_ctl[old_addr]->id);
503 }
504
505 return change;
506}
507
508
509
510
511
512
513
514
515
516
517
518
519
520static int snd_card_saa7134_capture_prepare(struct snd_pcm_substream * substream)
521{
522 struct snd_pcm_runtime *runtime = substream->runtime;
523 int bswap, sign;
524 u32 fmt, control;
525 snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
526 struct saa7134_dev *dev;
527 snd_card_saa7134_pcm_t *pcm = runtime->private_data;
528
529 pcm->dev->dmasound.substream = substream;
530
531 dev = saa7134->dev;
532
533 if (snd_pcm_format_width(runtime->format) == 8)
534 fmt = 0x00;
535 else
536 fmt = 0x01;
537
538 if (snd_pcm_format_signed(runtime->format))
539 sign = 1;
540 else
541 sign = 0;
542
543 if (snd_pcm_format_big_endian(runtime->format))
544 bswap = 1;
545 else
546 bswap = 0;
547
548 switch (dev->pci->device) {
549 case PCI_DEVICE_ID_PHILIPS_SAA7134:
550 if (1 == runtime->channels)
551 fmt |= (1 << 3);
552 if (2 == runtime->channels)
553 fmt |= (3 << 3);
554 if (sign)
555 fmt |= 0x04;
556
557 fmt |= (MIXER_ADDR_TVTUNER == dev->dmasound.input) ? 0xc0 : 0x80;
558 saa_writeb(SAA7134_NUM_SAMPLES0, ((dev->dmasound.blksize - 1) & 0x0000ff));
559 saa_writeb(SAA7134_NUM_SAMPLES1, ((dev->dmasound.blksize - 1) & 0x00ff00) >> 8);
560 saa_writeb(SAA7134_NUM_SAMPLES2, ((dev->dmasound.blksize - 1) & 0xff0000) >> 16);
561 saa_writeb(SAA7134_AUDIO_FORMAT_CTRL, fmt);
562
563 break;
564 case PCI_DEVICE_ID_PHILIPS_SAA7133:
565 case PCI_DEVICE_ID_PHILIPS_SAA7135:
566 if (1 == runtime->channels)
567 fmt |= (1 << 4);
568 if (2 == runtime->channels)
569 fmt |= (2 << 4);
570 if (!sign)
571 fmt |= 0x04;
572 saa_writel(SAA7133_NUM_SAMPLES, dev->dmasound.blksize -1);
573 saa_writel(SAA7133_AUDIO_CHANNEL, 0x543210 | (fmt << 24));
574 break;
575 }
576
577 dprintk("rec_start: afmt=%d ch=%d => fmt=0x%x swap=%c\n",
578 runtime->format, runtime->channels, fmt,
579 bswap ? 'b' : '-');
580
581 control = SAA7134_RS_CONTROL_BURST_16 |
582 SAA7134_RS_CONTROL_ME |
583 (dev->dmasound.pt.dma >> 12);
584 if (bswap)
585 control |= SAA7134_RS_CONTROL_BSWAP;
586
587 saa_writel(SAA7134_RS_BA1(6),0);
588 saa_writel(SAA7134_RS_BA2(6),dev->dmasound.blksize);
589 saa_writel(SAA7134_RS_PITCH(6),0);
590 saa_writel(SAA7134_RS_CONTROL(6),control);
591
592 dev->dmasound.rate = runtime->rate;
593
594
595 snd_saa7134_capsrc_set(saa7134->capture_ctl[dev->dmasound.input], 1, 1,
596 true);
597
598 return 0;
599
600}
601
602
603
604
605
606
607
608
609
610
611
612
613static snd_pcm_uframes_t
614snd_card_saa7134_capture_pointer(struct snd_pcm_substream * substream)
615{
616 struct snd_pcm_runtime *runtime = substream->runtime;
617 snd_card_saa7134_pcm_t *pcm = runtime->private_data;
618 struct saa7134_dev *dev=pcm->dev;
619
620 if (dev->dmasound.read_count) {
621 dev->dmasound.read_count -= snd_pcm_lib_period_bytes(substream);
622 dev->dmasound.read_offset += snd_pcm_lib_period_bytes(substream);
623 if (dev->dmasound.read_offset == dev->dmasound.bufsize)
624 dev->dmasound.read_offset = 0;
625 }
626
627 return bytes_to_frames(runtime, dev->dmasound.read_offset);
628}
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644static struct snd_pcm_hardware snd_card_saa7134_capture =
645{
646 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
647 SNDRV_PCM_INFO_BLOCK_TRANSFER |
648 SNDRV_PCM_INFO_MMAP_VALID),
649 .formats = SNDRV_PCM_FMTBIT_S16_LE | \
650 SNDRV_PCM_FMTBIT_S16_BE | \
651 SNDRV_PCM_FMTBIT_S8 | \
652 SNDRV_PCM_FMTBIT_U8 | \
653 SNDRV_PCM_FMTBIT_U16_LE | \
654 SNDRV_PCM_FMTBIT_U16_BE,
655 .rates = SNDRV_PCM_RATE_32000,
656 .rate_min = 32000,
657 .rate_max = 32000,
658 .channels_min = 1,
659 .channels_max = 2,
660 .buffer_bytes_max = (256*1024),
661 .period_bytes_min = 64,
662 .period_bytes_max = (256*1024),
663 .periods_min = 4,
664 .periods_max = 1024,
665};
666
667static void snd_card_saa7134_runtime_free(struct snd_pcm_runtime *runtime)
668{
669 snd_card_saa7134_pcm_t *pcm = runtime->private_data;
670
671 kfree(pcm);
672}
673
674
675
676
677
678
679
680
681
682
683
684static int snd_card_saa7134_hw_params(struct snd_pcm_substream * substream,
685 struct snd_pcm_hw_params * hw_params)
686{
687 snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
688 struct saa7134_dev *dev;
689 unsigned int period_size, periods;
690 int err;
691
692 period_size = params_period_bytes(hw_params);
693 periods = params_periods(hw_params);
694
695 if (period_size < 0x100 || period_size > 0x10000)
696 return -EINVAL;
697 if (periods < 4)
698 return -EINVAL;
699 if (period_size * periods > 1024 * 1024)
700 return -EINVAL;
701
702 dev = saa7134->dev;
703
704 if (dev->dmasound.blocks == periods &&
705 dev->dmasound.blksize == period_size)
706 return 0;
707
708
709 if (substream->runtime->dma_area) {
710 saa7134_pgtable_free(dev->pci, &dev->dmasound.pt);
711 saa7134_alsa_dma_unmap(dev);
712 dsp_buffer_free(dev);
713 substream->runtime->dma_area = NULL;
714 }
715 dev->dmasound.blocks = periods;
716 dev->dmasound.blksize = period_size;
717 dev->dmasound.bufsize = period_size * periods;
718
719 err = dsp_buffer_init(dev);
720 if (0 != err) {
721 dev->dmasound.blocks = 0;
722 dev->dmasound.blksize = 0;
723 dev->dmasound.bufsize = 0;
724 return err;
725 }
726
727 err = saa7134_alsa_dma_map(dev);
728 if (err) {
729 dsp_buffer_free(dev);
730 return err;
731 }
732 err = saa7134_pgtable_alloc(dev->pci, &dev->dmasound.pt);
733 if (err) {
734 saa7134_alsa_dma_unmap(dev);
735 dsp_buffer_free(dev);
736 return err;
737 }
738 err = saa7134_pgtable_build(dev->pci, &dev->dmasound.pt,
739 dev->dmasound.sglist, dev->dmasound.sglen, 0);
740 if (err) {
741 saa7134_pgtable_free(dev->pci, &dev->dmasound.pt);
742 saa7134_alsa_dma_unmap(dev);
743 dsp_buffer_free(dev);
744 return err;
745 }
746
747
748
749
750
751 substream->runtime->dma_area = dev->dmasound.vaddr;
752 substream->runtime->dma_bytes = dev->dmasound.bufsize;
753 substream->runtime->dma_addr = 0;
754
755 return 0;
756
757}
758
759
760
761
762
763
764
765
766
767
768
769static int snd_card_saa7134_hw_free(struct snd_pcm_substream * substream)
770{
771 snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
772 struct saa7134_dev *dev;
773
774 dev = saa7134->dev;
775
776 if (substream->runtime->dma_area) {
777 saa7134_pgtable_free(dev->pci, &dev->dmasound.pt);
778 saa7134_alsa_dma_unmap(dev);
779 dsp_buffer_free(dev);
780 substream->runtime->dma_area = NULL;
781 }
782
783 return 0;
784}
785
786
787
788
789
790
791
792
793
794
795static int snd_card_saa7134_capture_close(struct snd_pcm_substream * substream)
796{
797 snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
798 struct saa7134_dev *dev = saa7134->dev;
799
800 if (saa7134->mute_was_on) {
801 dev->ctl_mute = 1;
802 saa7134_tvaudio_setmute(dev);
803 }
804 return 0;
805}
806
807
808
809
810
811
812
813
814
815
816
817static int snd_card_saa7134_capture_open(struct snd_pcm_substream * substream)
818{
819 struct snd_pcm_runtime *runtime = substream->runtime;
820 snd_card_saa7134_pcm_t *pcm;
821 snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
822 struct saa7134_dev *dev;
823 int amux, err;
824
825 if (!saa7134) {
826 printk(KERN_ERR "BUG: saa7134 can't find device struct."
827 " Can't proceed with open\n");
828 return -ENODEV;
829 }
830 dev = saa7134->dev;
831 mutex_lock(&dev->dmasound.lock);
832
833 dev->dmasound.read_count = 0;
834 dev->dmasound.read_offset = 0;
835
836 amux = dev->input->amux;
837 if ((amux < 1) || (amux > 3))
838 amux = 1;
839 dev->dmasound.input = amux - 1;
840
841 mutex_unlock(&dev->dmasound.lock);
842
843 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
844 if (pcm == NULL)
845 return -ENOMEM;
846
847 pcm->dev=saa7134->dev;
848
849 spin_lock_init(&pcm->lock);
850
851 pcm->substream = substream;
852 runtime->private_data = pcm;
853 runtime->private_free = snd_card_saa7134_runtime_free;
854 runtime->hw = snd_card_saa7134_capture;
855
856 if (dev->ctl_mute != 0) {
857 saa7134->mute_was_on = 1;
858 dev->ctl_mute = 0;
859 saa7134_tvaudio_setmute(dev);
860 }
861
862 err = snd_pcm_hw_constraint_integer(runtime,
863 SNDRV_PCM_HW_PARAM_PERIODS);
864 if (err < 0)
865 return err;
866
867 err = snd_pcm_hw_constraint_step(runtime, 0,
868 SNDRV_PCM_HW_PARAM_PERIODS, 2);
869 if (err < 0)
870 return err;
871
872 return 0;
873}
874
875
876
877
878
879static struct page *snd_card_saa7134_page(struct snd_pcm_substream *substream,
880 unsigned long offset)
881{
882 void *pageptr = substream->runtime->dma_area + offset;
883 return vmalloc_to_page(pageptr);
884}
885
886
887
888
889
890static struct snd_pcm_ops snd_card_saa7134_capture_ops = {
891 .open = snd_card_saa7134_capture_open,
892 .close = snd_card_saa7134_capture_close,
893 .ioctl = snd_pcm_lib_ioctl,
894 .hw_params = snd_card_saa7134_hw_params,
895 .hw_free = snd_card_saa7134_hw_free,
896 .prepare = snd_card_saa7134_capture_prepare,
897 .trigger = snd_card_saa7134_capture_trigger,
898 .pointer = snd_card_saa7134_capture_pointer,
899 .page = snd_card_saa7134_page,
900};
901
902
903
904
905
906
907
908
909
910static int snd_card_saa7134_pcm(snd_card_saa7134_t *saa7134, int device)
911{
912 struct snd_pcm *pcm;
913 int err;
914
915 if ((err = snd_pcm_new(saa7134->card, "SAA7134 PCM", device, 0, 1, &pcm)) < 0)
916 return err;
917 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_card_saa7134_capture_ops);
918 pcm->private_data = saa7134;
919 pcm->info_flags = 0;
920 strcpy(pcm->name, "SAA7134 PCM");
921 return 0;
922}
923
924#define SAA713x_VOLUME(xname, xindex, addr) \
925{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
926 .info = snd_saa7134_volume_info, \
927 .get = snd_saa7134_volume_get, .put = snd_saa7134_volume_put, \
928 .private_value = addr }
929
930static int snd_saa7134_volume_info(struct snd_kcontrol * kcontrol,
931 struct snd_ctl_elem_info * uinfo)
932{
933 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
934 uinfo->count = 2;
935 uinfo->value.integer.min = 0;
936 uinfo->value.integer.max = 20;
937 return 0;
938}
939
940static int snd_saa7134_volume_get(struct snd_kcontrol * kcontrol,
941 struct snd_ctl_elem_value * ucontrol)
942{
943 snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
944 int addr = kcontrol->private_value;
945
946 ucontrol->value.integer.value[0] = chip->mixer_volume[addr][0];
947 ucontrol->value.integer.value[1] = chip->mixer_volume[addr][1];
948 return 0;
949}
950
951static int snd_saa7134_volume_put(struct snd_kcontrol * kcontrol,
952 struct snd_ctl_elem_value * ucontrol)
953{
954 snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
955 struct saa7134_dev *dev = chip->dev;
956
957 int change, addr = kcontrol->private_value;
958 int left, right;
959
960 left = ucontrol->value.integer.value[0];
961 if (left < 0)
962 left = 0;
963 if (left > 20)
964 left = 20;
965 right = ucontrol->value.integer.value[1];
966 if (right < 0)
967 right = 0;
968 if (right > 20)
969 right = 20;
970 spin_lock_irq(&chip->mixer_lock);
971 change = 0;
972 if (chip->mixer_volume[addr][0] != left) {
973 change = 1;
974 right = left;
975 }
976 if (chip->mixer_volume[addr][1] != right) {
977 change = 1;
978 left = right;
979 }
980 if (change) {
981 switch (dev->pci->device) {
982 case PCI_DEVICE_ID_PHILIPS_SAA7134:
983 switch (addr) {
984 case MIXER_ADDR_TVTUNER:
985 left = 20;
986 break;
987 case MIXER_ADDR_LINE1:
988 saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x10,
989 (left > 10) ? 0x00 : 0x10);
990 break;
991 case MIXER_ADDR_LINE2:
992 saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x20,
993 (left > 10) ? 0x00 : 0x20);
994 break;
995 }
996 break;
997 case PCI_DEVICE_ID_PHILIPS_SAA7133:
998 case PCI_DEVICE_ID_PHILIPS_SAA7135:
999 switch (addr) {
1000 case MIXER_ADDR_TVTUNER:
1001 left = 20;
1002 break;
1003 case MIXER_ADDR_LINE1:
1004 saa_andorb(0x0594, 0x10,
1005 (left > 10) ? 0x00 : 0x10);
1006 break;
1007 case MIXER_ADDR_LINE2:
1008 saa_andorb(0x0594, 0x20,
1009 (left > 10) ? 0x00 : 0x20);
1010 break;
1011 }
1012 break;
1013 }
1014 chip->mixer_volume[addr][0] = left;
1015 chip->mixer_volume[addr][1] = right;
1016 }
1017 spin_unlock_irq(&chip->mixer_lock);
1018 return change;
1019}
1020
1021#define SAA713x_CAPSRC(xname, xindex, addr) \
1022{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1023 .info = snd_saa7134_capsrc_info, \
1024 .get = snd_saa7134_capsrc_get, .put = snd_saa7134_capsrc_put, \
1025 .private_value = addr }
1026
1027static int snd_saa7134_capsrc_info(struct snd_kcontrol * kcontrol,
1028 struct snd_ctl_elem_info * uinfo)
1029{
1030 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1031 uinfo->count = 2;
1032 uinfo->value.integer.min = 0;
1033 uinfo->value.integer.max = 1;
1034 return 0;
1035}
1036
1037static int snd_saa7134_capsrc_get(struct snd_kcontrol * kcontrol,
1038 struct snd_ctl_elem_value * ucontrol)
1039{
1040 snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
1041 int addr = kcontrol->private_value;
1042
1043 spin_lock_irq(&chip->mixer_lock);
1044 if (chip->capture_source_addr == addr) {
1045 ucontrol->value.integer.value[0] = chip->capture_source[0];
1046 ucontrol->value.integer.value[1] = chip->capture_source[1];
1047 } else {
1048 ucontrol->value.integer.value[0] = 0;
1049 ucontrol->value.integer.value[1] = 0;
1050 }
1051 spin_unlock_irq(&chip->mixer_lock);
1052
1053 return 0;
1054}
1055
1056static int snd_saa7134_capsrc_put(struct snd_kcontrol * kcontrol,
1057 struct snd_ctl_elem_value * ucontrol)
1058{
1059 int left, right;
1060 left = ucontrol->value.integer.value[0] & 1;
1061 right = ucontrol->value.integer.value[1] & 1;
1062
1063 return snd_saa7134_capsrc_set(kcontrol, left, right, false);
1064}
1065
1066static struct snd_kcontrol_new snd_saa7134_volume_controls[] = {
1067SAA713x_VOLUME("Video Volume", 0, MIXER_ADDR_TVTUNER),
1068SAA713x_VOLUME("Line Volume", 1, MIXER_ADDR_LINE1),
1069SAA713x_VOLUME("Line Volume", 2, MIXER_ADDR_LINE2),
1070};
1071
1072static struct snd_kcontrol_new snd_saa7134_capture_controls[] = {
1073SAA713x_CAPSRC("Video Capture Switch", 0, MIXER_ADDR_TVTUNER),
1074SAA713x_CAPSRC("Line Capture Switch", 1, MIXER_ADDR_LINE1),
1075SAA713x_CAPSRC("Line Capture Switch", 2, MIXER_ADDR_LINE2),
1076};
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086static int snd_card_saa7134_new_mixer(snd_card_saa7134_t * chip)
1087{
1088 struct snd_card *card = chip->card;
1089 struct snd_kcontrol *kcontrol;
1090 unsigned int idx;
1091 int err, addr;
1092
1093 strcpy(card->mixername, "SAA7134 Mixer");
1094
1095 for (idx = 0; idx < ARRAY_SIZE(snd_saa7134_volume_controls); idx++) {
1096 kcontrol = snd_ctl_new1(&snd_saa7134_volume_controls[idx],
1097 chip);
1098 err = snd_ctl_add(card, kcontrol);
1099 if (err < 0)
1100 return err;
1101 }
1102
1103 for (idx = 0; idx < ARRAY_SIZE(snd_saa7134_capture_controls); idx++) {
1104 kcontrol = snd_ctl_new1(&snd_saa7134_capture_controls[idx],
1105 chip);
1106 addr = snd_saa7134_capture_controls[idx].private_value;
1107 chip->capture_ctl[addr] = kcontrol;
1108 err = snd_ctl_add(card, kcontrol);
1109 if (err < 0)
1110 return err;
1111 }
1112
1113 chip->capture_source_addr = MIXER_ADDR_UNSELECTED;
1114 return 0;
1115}
1116
1117static void snd_saa7134_free(struct snd_card * card)
1118{
1119 snd_card_saa7134_t *chip = card->private_data;
1120
1121 if (chip->dev->dmasound.priv_data == NULL)
1122 return;
1123
1124 if (chip->irq >= 0)
1125 free_irq(chip->irq, &chip->dev->dmasound);
1126
1127 chip->dev->dmasound.priv_data = NULL;
1128
1129}
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139static int alsa_card_saa7134_create(struct saa7134_dev *dev, int devnum)
1140{
1141
1142 struct snd_card *card;
1143 snd_card_saa7134_t *chip;
1144 int err;
1145
1146
1147 if (devnum >= SNDRV_CARDS)
1148 return -ENODEV;
1149 if (!enable[devnum])
1150 return -ENODEV;
1151
1152 err = snd_card_new(&dev->pci->dev, index[devnum], id[devnum],
1153 THIS_MODULE, sizeof(snd_card_saa7134_t), &card);
1154 if (err < 0)
1155 return err;
1156
1157 strcpy(card->driver, "SAA7134");
1158
1159
1160
1161 card->private_free = snd_saa7134_free;
1162 chip = card->private_data;
1163
1164 spin_lock_init(&chip->lock);
1165 spin_lock_init(&chip->mixer_lock);
1166
1167 chip->dev = dev;
1168
1169 chip->card = card;
1170
1171 chip->pci = dev->pci;
1172 chip->iobase = pci_resource_start(dev->pci, 0);
1173
1174
1175 err = request_irq(dev->pci->irq, saa7134_alsa_irq,
1176 IRQF_SHARED, dev->name,
1177 (void*) &dev->dmasound);
1178
1179 if (err < 0) {
1180 printk(KERN_ERR "%s: can't get IRQ %d for ALSA\n",
1181 dev->name, dev->pci->irq);
1182 goto __nodev;
1183 }
1184
1185 chip->irq = dev->pci->irq;
1186
1187 mutex_init(&dev->dmasound.lock);
1188
1189 if ((err = snd_card_saa7134_new_mixer(chip)) < 0)
1190 goto __nodev;
1191
1192 if ((err = snd_card_saa7134_pcm(chip, 0)) < 0)
1193 goto __nodev;
1194
1195
1196
1197 strcpy(card->shortname, "SAA7134");
1198 sprintf(card->longname, "%s at 0x%lx irq %d",
1199 chip->dev->name, chip->iobase, chip->irq);
1200
1201 printk(KERN_INFO "%s/alsa: %s registered as card %d\n",dev->name,card->longname,index[devnum]);
1202
1203 if ((err = snd_card_register(card)) == 0) {
1204 snd_saa7134_cards[devnum] = card;
1205 return 0;
1206 }
1207
1208__nodev:
1209 snd_card_free(card);
1210 return err;
1211}
1212
1213
1214static int alsa_device_init(struct saa7134_dev *dev)
1215{
1216 dev->dmasound.priv_data = dev;
1217 alsa_card_saa7134_create(dev,dev->nr);
1218 return 1;
1219}
1220
1221static int alsa_device_exit(struct saa7134_dev *dev)
1222{
1223
1224 snd_card_free(snd_saa7134_cards[dev->nr]);
1225 snd_saa7134_cards[dev->nr] = NULL;
1226 return 1;
1227}
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237static int saa7134_alsa_init(void)
1238{
1239 struct saa7134_dev *dev = NULL;
1240 struct list_head *list;
1241
1242 saa7134_dmasound_init = alsa_device_init;
1243 saa7134_dmasound_exit = alsa_device_exit;
1244
1245 printk(KERN_INFO "saa7134 ALSA driver for DMA sound loaded\n");
1246
1247 list_for_each(list,&saa7134_devlist) {
1248 dev = list_entry(list, struct saa7134_dev, devlist);
1249 if (dev->pci->device == PCI_DEVICE_ID_PHILIPS_SAA7130)
1250 printk(KERN_INFO "%s/alsa: %s doesn't support digital audio\n",
1251 dev->name, saa7134_boards[dev->board].name);
1252 else
1253 alsa_device_init(dev);
1254 }
1255
1256 if (dev == NULL)
1257 printk(KERN_INFO "saa7134 ALSA: no saa7134 cards found\n");
1258
1259 return 0;
1260
1261}
1262
1263
1264
1265
1266
1267static void saa7134_alsa_exit(void)
1268{
1269 int idx;
1270
1271 for (idx = 0; idx < SNDRV_CARDS; idx++) {
1272 snd_card_free(snd_saa7134_cards[idx]);
1273 }
1274
1275 saa7134_dmasound_init = NULL;
1276 saa7134_dmasound_exit = NULL;
1277 printk(KERN_INFO "saa7134 ALSA driver for DMA sound unloaded\n");
1278
1279 return;
1280}
1281
1282
1283late_initcall(saa7134_alsa_init);
1284module_exit(saa7134_alsa_exit);
1285MODULE_LICENSE("GPL");
1286MODULE_AUTHOR("Ricardo Cerqueira");
1287