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