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