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 %p, size=%d\n",
277 		 dma->vaddr, nr_pages << PAGE_SHIFT);
278 
279 	memset(dma->vaddr, 0, nr_pages << PAGE_SHIFT);
280 	dma->nr_pages = nr_pages;
281 
282 	dma->sglist = vzalloc(array_size(sizeof(*dma->sglist), dma->nr_pages));
283 	if (NULL == dma->sglist)
284 		goto vzalloc_err;
285 
286 	sg_init_table(dma->sglist, dma->nr_pages);
287 	for (i = 0; i < dma->nr_pages; i++) {
288 		pg = vmalloc_to_page(dma->vaddr + i * PAGE_SIZE);
289 		if (NULL == pg)
290 			goto vmalloc_to_page_err;
291 		sg_set_page(&dma->sglist[i], pg, PAGE_SIZE, 0);
292 	}
293 	return 0;
294 
295 vmalloc_to_page_err:
296 	vfree(dma->sglist);
297 	dma->sglist = NULL;
298 vzalloc_err:
299 	vfree(dma->vaddr);
300 	dma->vaddr = NULL;
301 	return -ENOMEM;
302 }
303 
304 static int saa7134_alsa_dma_map(struct saa7134_dev *dev)
305 {
306 	struct saa7134_dmasound *dma = &dev->dmasound;
307 
308 	dma->sglen = dma_map_sg(&dev->pci->dev, dma->sglist,
309 			dma->nr_pages, PCI_DMA_FROMDEVICE);
310 
311 	if (0 == dma->sglen) {
312 		pr_warn("%s: saa7134_alsa_map_sg failed\n", __func__);
313 		return -ENOMEM;
314 	}
315 	return 0;
316 }
317 
318 static int saa7134_alsa_dma_unmap(struct saa7134_dev *dev)
319 {
320 	struct saa7134_dmasound *dma = &dev->dmasound;
321 
322 	if (!dma->sglen)
323 		return 0;
324 
325 	dma_unmap_sg(&dev->pci->dev, dma->sglist, dma->sglen, PCI_DMA_FROMDEVICE);
326 	dma->sglen = 0;
327 	return 0;
328 }
329 
330 static int saa7134_alsa_dma_free(struct saa7134_dmasound *dma)
331 {
332 	vfree(dma->sglist);
333 	dma->sglist = NULL;
334 	vfree(dma->vaddr);
335 	dma->vaddr = NULL;
336 	return 0;
337 }
338 
339 /*
340  * DMA buffer initialization
341  *
342  *   Uses V4L functions to initialize the DMA. Shouldn't be necessary in
343  *  ALSA, but I was unable to use ALSA's own DMA, and had to force the
344  *  usage of V4L's
345  *
346  *   - Copied verbatim from saa7134-oss.
347  *
348  */
349 
350 static int dsp_buffer_init(struct saa7134_dev *dev)
351 {
352 	int err;
353 
354 	BUG_ON(!dev->dmasound.bufsize);
355 
356 	err = saa7134_alsa_dma_init(dev,
357 			       (dev->dmasound.bufsize + PAGE_SIZE) >> PAGE_SHIFT);
358 	if (0 != err)
359 		return err;
360 	return 0;
361 }
362 
363 /*
364  * DMA buffer release
365  *
366  *   Called after closing the device, during snd_card_saa7134_capture_close
367  *
368  */
369 
370 static int dsp_buffer_free(struct saa7134_dev *dev)
371 {
372 	BUG_ON(!dev->dmasound.blksize);
373 
374 	saa7134_alsa_dma_free(&dev->dmasound);
375 
376 	dev->dmasound.blocks  = 0;
377 	dev->dmasound.blksize = 0;
378 	dev->dmasound.bufsize = 0;
379 
380 	return 0;
381 }
382 
383 /*
384  * Setting the capture source and updating the ALSA controls
385  */
386 static int snd_saa7134_capsrc_set(struct snd_kcontrol *kcontrol,
387 				  int left, int right, bool force_notify)
388 {
389 	snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
390 	int change = 0, addr = kcontrol->private_value;
391 	int active, old_addr;
392 	u32 anabar, xbarin;
393 	int analog_io, rate;
394 	struct saa7134_dev *dev;
395 
396 	dev = chip->dev;
397 
398 	spin_lock_irq(&chip->mixer_lock);
399 
400 	active = left != 0 || right != 0;
401 	old_addr = chip->capture_source_addr;
402 
403 	/* The active capture source cannot be deactivated */
404 	if (active) {
405 		change = old_addr != addr ||
406 			 chip->capture_source[0] != left ||
407 			 chip->capture_source[1] != right;
408 
409 		chip->capture_source[0] = left;
410 		chip->capture_source[1] = right;
411 		chip->capture_source_addr = addr;
412 		dev->dmasound.input = addr;
413 	}
414 	spin_unlock_irq(&chip->mixer_lock);
415 
416 	if (change) {
417 		switch (dev->pci->device) {
418 
419 		case PCI_DEVICE_ID_PHILIPS_SAA7134:
420 			switch (addr) {
421 			case MIXER_ADDR_TVTUNER:
422 				saa_andorb(SAA7134_AUDIO_FORMAT_CTRL,
423 					   0xc0, 0xc0);
424 				saa_andorb(SAA7134_SIF_SAMPLE_FREQ,
425 					   0x03, 0x00);
426 				break;
427 			case MIXER_ADDR_LINE1:
428 			case MIXER_ADDR_LINE2:
429 				analog_io = (MIXER_ADDR_LINE1 == addr) ?
430 					     0x00 : 0x08;
431 				rate = (32000 == dev->dmasound.rate) ?
432 					0x01 : 0x03;
433 				saa_andorb(SAA7134_ANALOG_IO_SELECT,
434 					   0x08, analog_io);
435 				saa_andorb(SAA7134_AUDIO_FORMAT_CTRL,
436 					   0xc0, 0x80);
437 				saa_andorb(SAA7134_SIF_SAMPLE_FREQ,
438 					   0x03, rate);
439 				break;
440 			}
441 
442 			break;
443 		case PCI_DEVICE_ID_PHILIPS_SAA7133:
444 		case PCI_DEVICE_ID_PHILIPS_SAA7135:
445 			xbarin = 0x03; /* adc */
446 			anabar = 0;
447 			switch (addr) {
448 			case MIXER_ADDR_TVTUNER:
449 				xbarin = 0; /* Demodulator */
450 				anabar = 2; /* DACs */
451 				break;
452 			case MIXER_ADDR_LINE1:
453 				anabar = 0;  /* aux1, aux1 */
454 				break;
455 			case MIXER_ADDR_LINE2:
456 				anabar = 9;  /* aux2, aux2 */
457 				break;
458 			}
459 
460 			/* output xbar always main channel */
461 			saa_dsp_writel(dev, SAA7133_DIGITAL_OUTPUT_SEL1,
462 				       0xbbbb10);
463 
464 			if (left || right) {
465 				/* We've got data, turn the input on */
466 				saa_dsp_writel(dev, SAA7133_DIGITAL_INPUT_XBAR1,
467 					       xbarin);
468 				saa_writel(SAA7133_ANALOG_IO_SELECT, anabar);
469 			} else {
470 				saa_dsp_writel(dev, SAA7133_DIGITAL_INPUT_XBAR1,
471 					       0);
472 				saa_writel(SAA7133_ANALOG_IO_SELECT, 0);
473 			}
474 			break;
475 		}
476 	}
477 
478 	if (change) {
479 		if (force_notify)
480 			snd_ctl_notify(chip->card,
481 				       SNDRV_CTL_EVENT_MASK_VALUE,
482 				       &chip->capture_ctl[addr]->id);
483 
484 		if (old_addr != MIXER_ADDR_UNSELECTED && old_addr != addr)
485 			snd_ctl_notify(chip->card,
486 				       SNDRV_CTL_EVENT_MASK_VALUE,
487 				       &chip->capture_ctl[old_addr]->id);
488 	}
489 
490 	return change;
491 }
492 
493 /*
494  * ALSA PCM preparation
495  *
496  *   - One of the ALSA capture callbacks.
497  *
498  *   Called right after the capture device is opened, this function configures
499  *  the buffer using the previously defined functions, allocates the memory,
500  *  sets up the hardware registers, and then starts the DMA. When this function
501  *  returns, the audio should be flowing.
502  *
503  */
504 
505 static int snd_card_saa7134_capture_prepare(struct snd_pcm_substream * substream)
506 {
507 	struct snd_pcm_runtime *runtime = substream->runtime;
508 	int bswap, sign;
509 	u32 fmt, control;
510 	snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
511 	struct saa7134_dev *dev;
512 	snd_card_saa7134_pcm_t *pcm = runtime->private_data;
513 
514 	pcm->dev->dmasound.substream = substream;
515 
516 	dev = saa7134->dev;
517 
518 	if (snd_pcm_format_width(runtime->format) == 8)
519 		fmt = 0x00;
520 	else
521 		fmt = 0x01;
522 
523 	if (snd_pcm_format_signed(runtime->format))
524 		sign = 1;
525 	else
526 		sign = 0;
527 
528 	if (snd_pcm_format_big_endian(runtime->format))
529 		bswap = 1;
530 	else
531 		bswap = 0;
532 
533 	switch (dev->pci->device) {
534 	  case PCI_DEVICE_ID_PHILIPS_SAA7134:
535 		if (1 == runtime->channels)
536 			fmt |= (1 << 3);
537 		if (2 == runtime->channels)
538 			fmt |= (3 << 3);
539 		if (sign)
540 			fmt |= 0x04;
541 
542 		fmt |= (MIXER_ADDR_TVTUNER == dev->dmasound.input) ? 0xc0 : 0x80;
543 		saa_writeb(SAA7134_NUM_SAMPLES0, ((dev->dmasound.blksize - 1) & 0x0000ff));
544 		saa_writeb(SAA7134_NUM_SAMPLES1, ((dev->dmasound.blksize - 1) & 0x00ff00) >>  8);
545 		saa_writeb(SAA7134_NUM_SAMPLES2, ((dev->dmasound.blksize - 1) & 0xff0000) >> 16);
546 		saa_writeb(SAA7134_AUDIO_FORMAT_CTRL, fmt);
547 
548 		break;
549 	  case PCI_DEVICE_ID_PHILIPS_SAA7133:
550 	  case PCI_DEVICE_ID_PHILIPS_SAA7135:
551 		if (1 == runtime->channels)
552 			fmt |= (1 << 4);
553 		if (2 == runtime->channels)
554 			fmt |= (2 << 4);
555 		if (!sign)
556 			fmt |= 0x04;
557 		saa_writel(SAA7133_NUM_SAMPLES, dev->dmasound.blksize -1);
558 		saa_writel(SAA7133_AUDIO_CHANNEL, 0x543210 | (fmt << 24));
559 		break;
560 	}
561 
562 	pr_debug("rec_start: afmt=%d ch=%d  =>  fmt=0x%x swap=%c\n",
563 		runtime->format, runtime->channels, fmt,
564 		bswap ? 'b' : '-');
565 	/* dma: setup channel 6 (= AUDIO) */
566 	control = SAA7134_RS_CONTROL_BURST_16 |
567 		SAA7134_RS_CONTROL_ME |
568 		(dev->dmasound.pt.dma >> 12);
569 	if (bswap)
570 		control |= SAA7134_RS_CONTROL_BSWAP;
571 
572 	saa_writel(SAA7134_RS_BA1(6),0);
573 	saa_writel(SAA7134_RS_BA2(6),dev->dmasound.blksize);
574 	saa_writel(SAA7134_RS_PITCH(6),0);
575 	saa_writel(SAA7134_RS_CONTROL(6),control);
576 
577 	dev->dmasound.rate = runtime->rate;
578 
579 	/* Setup and update the card/ALSA controls */
580 	snd_saa7134_capsrc_set(saa7134->capture_ctl[dev->dmasound.input], 1, 1,
581 			       true);
582 
583 	return 0;
584 
585 }
586 
587 /*
588  * ALSA pointer fetching
589  *
590  *   - One of the ALSA capture callbacks.
591  *
592  *   Called whenever a period elapses, it must return the current hardware
593  *  position of the buffer.
594  *   Also resets the read counter used to prevent overruns
595  *
596  */
597 
598 static snd_pcm_uframes_t
599 snd_card_saa7134_capture_pointer(struct snd_pcm_substream * substream)
600 {
601 	struct snd_pcm_runtime *runtime = substream->runtime;
602 	snd_card_saa7134_pcm_t *pcm = runtime->private_data;
603 	struct saa7134_dev *dev=pcm->dev;
604 
605 	if (dev->dmasound.read_count) {
606 		dev->dmasound.read_count  -= snd_pcm_lib_period_bytes(substream);
607 		dev->dmasound.read_offset += snd_pcm_lib_period_bytes(substream);
608 		if (dev->dmasound.read_offset == dev->dmasound.bufsize)
609 			dev->dmasound.read_offset = 0;
610 	}
611 
612 	return bytes_to_frames(runtime, dev->dmasound.read_offset);
613 }
614 
615 /*
616  * ALSA hardware capabilities definition
617  *
618  *  Report only 32kHz for ALSA:
619  *
620  *  - SAA7133/35 uses DDEP (DemDec Easy Programming mode), which works in 32kHz
621  *    only
622  *  - SAA7134 for TV mode uses DemDec mode (32kHz)
623  *  - Radio works in 32kHz only
624  *  - When recording 48kHz from Line1/Line2, switching of capture source to TV
625  *    means
626  *    switching to 32kHz without any frequency translation
627  */
628 
629 static const struct snd_pcm_hardware snd_card_saa7134_capture =
630 {
631 	.info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
632 				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
633 				 SNDRV_PCM_INFO_MMAP_VALID),
634 	.formats =		SNDRV_PCM_FMTBIT_S16_LE | \
635 				SNDRV_PCM_FMTBIT_S16_BE | \
636 				SNDRV_PCM_FMTBIT_S8 | \
637 				SNDRV_PCM_FMTBIT_U8 | \
638 				SNDRV_PCM_FMTBIT_U16_LE | \
639 				SNDRV_PCM_FMTBIT_U16_BE,
640 	.rates =		SNDRV_PCM_RATE_32000,
641 	.rate_min =		32000,
642 	.rate_max =		32000,
643 	.channels_min =		1,
644 	.channels_max =		2,
645 	.buffer_bytes_max =	(256*1024),
646 	.period_bytes_min =	64,
647 	.period_bytes_max =	(256*1024),
648 	.periods_min =		4,
649 	.periods_max =		1024,
650 };
651 
652 static void snd_card_saa7134_runtime_free(struct snd_pcm_runtime *runtime)
653 {
654 	snd_card_saa7134_pcm_t *pcm = runtime->private_data;
655 
656 	kfree(pcm);
657 }
658 
659 
660 /*
661  * ALSA hardware params
662  *
663  *   - One of the ALSA capture callbacks.
664  *
665  *   Called on initialization, right before the PCM preparation
666  *
667  */
668 
669 static int snd_card_saa7134_hw_params(struct snd_pcm_substream * substream,
670 				      struct snd_pcm_hw_params * hw_params)
671 {
672 	snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
673 	struct saa7134_dev *dev;
674 	unsigned int period_size, periods;
675 	int err;
676 
677 	period_size = params_period_bytes(hw_params);
678 	periods = params_periods(hw_params);
679 
680 	if (period_size < 0x100 || period_size > 0x10000)
681 		return -EINVAL;
682 	if (periods < 4)
683 		return -EINVAL;
684 	if (period_size * periods > 1024 * 1024)
685 		return -EINVAL;
686 
687 	dev = saa7134->dev;
688 
689 	if (dev->dmasound.blocks == periods &&
690 	    dev->dmasound.blksize == period_size)
691 		return 0;
692 
693 	/* release the old buffer */
694 	if (substream->runtime->dma_area) {
695 		saa7134_pgtable_free(dev->pci, &dev->dmasound.pt);
696 		saa7134_alsa_dma_unmap(dev);
697 		dsp_buffer_free(dev);
698 		substream->runtime->dma_area = NULL;
699 	}
700 	dev->dmasound.blocks  = periods;
701 	dev->dmasound.blksize = period_size;
702 	dev->dmasound.bufsize = period_size * periods;
703 
704 	err = dsp_buffer_init(dev);
705 	if (0 != err) {
706 		dev->dmasound.blocks  = 0;
707 		dev->dmasound.blksize = 0;
708 		dev->dmasound.bufsize = 0;
709 		return err;
710 	}
711 
712 	err = saa7134_alsa_dma_map(dev);
713 	if (err) {
714 		dsp_buffer_free(dev);
715 		return err;
716 	}
717 	err = saa7134_pgtable_alloc(dev->pci, &dev->dmasound.pt);
718 	if (err) {
719 		saa7134_alsa_dma_unmap(dev);
720 		dsp_buffer_free(dev);
721 		return err;
722 	}
723 	err = saa7134_pgtable_build(dev->pci, &dev->dmasound.pt,
724 				dev->dmasound.sglist, dev->dmasound.sglen, 0);
725 	if (err) {
726 		saa7134_pgtable_free(dev->pci, &dev->dmasound.pt);
727 		saa7134_alsa_dma_unmap(dev);
728 		dsp_buffer_free(dev);
729 		return err;
730 	}
731 
732 	/* I should be able to use runtime->dma_addr in the control
733 	   byte, but it doesn't work. So I allocate the DMA using the
734 	   V4L functions, and force ALSA to use that as the DMA area */
735 
736 	substream->runtime->dma_area = dev->dmasound.vaddr;
737 	substream->runtime->dma_bytes = dev->dmasound.bufsize;
738 	substream->runtime->dma_addr = 0;
739 
740 	return 0;
741 
742 }
743 
744 /*
745  * ALSA hardware release
746  *
747  *   - One of the ALSA capture callbacks.
748  *
749  *   Called after closing the device, but before snd_card_saa7134_capture_close
750  *   It stops the DMA audio and releases the buffers.
751  *
752  */
753 
754 static int snd_card_saa7134_hw_free(struct snd_pcm_substream * substream)
755 {
756 	snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
757 	struct saa7134_dev *dev;
758 
759 	dev = saa7134->dev;
760 
761 	if (substream->runtime->dma_area) {
762 		saa7134_pgtable_free(dev->pci, &dev->dmasound.pt);
763 		saa7134_alsa_dma_unmap(dev);
764 		dsp_buffer_free(dev);
765 		substream->runtime->dma_area = NULL;
766 	}
767 
768 	return 0;
769 }
770 
771 /*
772  * ALSA capture finish
773  *
774  *   - One of the ALSA capture callbacks.
775  *
776  *   Called after closing the device.
777  *
778  */
779 
780 static int snd_card_saa7134_capture_close(struct snd_pcm_substream * substream)
781 {
782 	snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
783 	struct saa7134_dev *dev = saa7134->dev;
784 
785 	if (saa7134->mute_was_on) {
786 		dev->ctl_mute = 1;
787 		saa7134_tvaudio_setmute(dev);
788 	}
789 	return 0;
790 }
791 
792 /*
793  * ALSA capture start
794  *
795  *   - One of the ALSA capture callbacks.
796  *
797  *   Called when opening the device. It creates and populates the PCM
798  *  structure
799  *
800  */
801 
802 static int snd_card_saa7134_capture_open(struct snd_pcm_substream * substream)
803 {
804 	struct snd_pcm_runtime *runtime = substream->runtime;
805 	snd_card_saa7134_pcm_t *pcm;
806 	snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
807 	struct saa7134_dev *dev;
808 	int amux, err;
809 
810 	if (!saa7134) {
811 		pr_err("BUG: saa7134 can't find device struct. Can't proceed with open\n");
812 		return -ENODEV;
813 	}
814 	dev = saa7134->dev;
815 	mutex_lock(&dev->dmasound.lock);
816 
817 	dev->dmasound.read_count  = 0;
818 	dev->dmasound.read_offset = 0;
819 
820 	amux = dev->input->amux;
821 	if ((amux < 1) || (amux > 3))
822 		amux = 1;
823 	dev->dmasound.input  =  amux - 1;
824 
825 	mutex_unlock(&dev->dmasound.lock);
826 
827 	pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
828 	if (pcm == NULL)
829 		return -ENOMEM;
830 
831 	pcm->dev=saa7134->dev;
832 
833 	spin_lock_init(&pcm->lock);
834 
835 	pcm->substream = substream;
836 	runtime->private_data = pcm;
837 	runtime->private_free = snd_card_saa7134_runtime_free;
838 	runtime->hw = snd_card_saa7134_capture;
839 
840 	if (dev->ctl_mute != 0) {
841 		saa7134->mute_was_on = 1;
842 		dev->ctl_mute = 0;
843 		saa7134_tvaudio_setmute(dev);
844 	}
845 
846 	err = snd_pcm_hw_constraint_integer(runtime,
847 						SNDRV_PCM_HW_PARAM_PERIODS);
848 	if (err < 0)
849 		return err;
850 
851 	err = snd_pcm_hw_constraint_step(runtime, 0,
852 						SNDRV_PCM_HW_PARAM_PERIODS, 2);
853 	if (err < 0)
854 		return err;
855 
856 	return 0;
857 }
858 
859 /*
860  * page callback (needed for mmap)
861  */
862 
863 static struct page *snd_card_saa7134_page(struct snd_pcm_substream *substream,
864 					unsigned long offset)
865 {
866 	void *pageptr = substream->runtime->dma_area + offset;
867 	return vmalloc_to_page(pageptr);
868 }
869 
870 /*
871  * ALSA capture callbacks definition
872  */
873 
874 static const struct snd_pcm_ops snd_card_saa7134_capture_ops = {
875 	.open =			snd_card_saa7134_capture_open,
876 	.close =		snd_card_saa7134_capture_close,
877 	.ioctl =		snd_pcm_lib_ioctl,
878 	.hw_params =		snd_card_saa7134_hw_params,
879 	.hw_free =		snd_card_saa7134_hw_free,
880 	.prepare =		snd_card_saa7134_capture_prepare,
881 	.trigger =		snd_card_saa7134_capture_trigger,
882 	.pointer =		snd_card_saa7134_capture_pointer,
883 	.page =			snd_card_saa7134_page,
884 };
885 
886 /*
887  * ALSA PCM setup
888  *
889  *   Called when initializing the board. Sets up the name and hooks up
890  *  the callbacks
891  *
892  */
893 
894 static int snd_card_saa7134_pcm(snd_card_saa7134_t *saa7134, int device)
895 {
896 	struct snd_pcm *pcm;
897 	int err;
898 
899 	if ((err = snd_pcm_new(saa7134->card, "SAA7134 PCM", device, 0, 1, &pcm)) < 0)
900 		return err;
901 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_card_saa7134_capture_ops);
902 	pcm->private_data = saa7134;
903 	pcm->info_flags = 0;
904 	strscpy(pcm->name, "SAA7134 PCM", sizeof(pcm->name));
905 	return 0;
906 }
907 
908 #define SAA713x_VOLUME(xname, xindex, addr) \
909 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
910   .info = snd_saa7134_volume_info, \
911   .get = snd_saa7134_volume_get, .put = snd_saa7134_volume_put, \
912   .private_value = addr }
913 
914 static int snd_saa7134_volume_info(struct snd_kcontrol * kcontrol,
915 				   struct snd_ctl_elem_info * uinfo)
916 {
917 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
918 	uinfo->count = 2;
919 	uinfo->value.integer.min = 0;
920 	uinfo->value.integer.max = 20;
921 	return 0;
922 }
923 
924 static int snd_saa7134_volume_get(struct snd_kcontrol * kcontrol,
925 				  struct snd_ctl_elem_value * ucontrol)
926 {
927 	snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
928 	int addr = kcontrol->private_value;
929 
930 	ucontrol->value.integer.value[0] = chip->mixer_volume[addr][0];
931 	ucontrol->value.integer.value[1] = chip->mixer_volume[addr][1];
932 	return 0;
933 }
934 
935 static int snd_saa7134_volume_put(struct snd_kcontrol * kcontrol,
936 				  struct snd_ctl_elem_value * ucontrol)
937 {
938 	snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
939 	struct saa7134_dev *dev = chip->dev;
940 
941 	int change, addr = kcontrol->private_value;
942 	int left, right;
943 
944 	left = ucontrol->value.integer.value[0];
945 	if (left < 0)
946 		left = 0;
947 	if (left > 20)
948 		left = 20;
949 	right = ucontrol->value.integer.value[1];
950 	if (right < 0)
951 		right = 0;
952 	if (right > 20)
953 		right = 20;
954 	spin_lock_irq(&chip->mixer_lock);
955 	change = 0;
956 	if (chip->mixer_volume[addr][0] != left) {
957 		change = 1;
958 		right = left;
959 	}
960 	if (chip->mixer_volume[addr][1] != right) {
961 		change = 1;
962 		left = right;
963 	}
964 	if (change) {
965 		switch (dev->pci->device) {
966 			case PCI_DEVICE_ID_PHILIPS_SAA7134:
967 				switch (addr) {
968 					case MIXER_ADDR_TVTUNER:
969 						left = 20;
970 						break;
971 					case MIXER_ADDR_LINE1:
972 						saa_andorb(SAA7134_ANALOG_IO_SELECT,  0x10,
973 							   (left > 10) ? 0x00 : 0x10);
974 						break;
975 					case MIXER_ADDR_LINE2:
976 						saa_andorb(SAA7134_ANALOG_IO_SELECT,  0x20,
977 							   (left > 10) ? 0x00 : 0x20);
978 						break;
979 				}
980 				break;
981 			case PCI_DEVICE_ID_PHILIPS_SAA7133:
982 			case PCI_DEVICE_ID_PHILIPS_SAA7135:
983 				switch (addr) {
984 					case MIXER_ADDR_TVTUNER:
985 						left = 20;
986 						break;
987 					case MIXER_ADDR_LINE1:
988 						saa_andorb(0x0594,  0x10,
989 							   (left > 10) ? 0x00 : 0x10);
990 						break;
991 					case MIXER_ADDR_LINE2:
992 						saa_andorb(0x0594,  0x20,
993 							   (left > 10) ? 0x00 : 0x20);
994 						break;
995 				}
996 				break;
997 		}
998 		chip->mixer_volume[addr][0] = left;
999 		chip->mixer_volume[addr][1] = right;
1000 	}
1001 	spin_unlock_irq(&chip->mixer_lock);
1002 	return change;
1003 }
1004 
1005 #define SAA713x_CAPSRC(xname, xindex, addr) \
1006 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1007   .info = snd_saa7134_capsrc_info, \
1008   .get = snd_saa7134_capsrc_get, .put = snd_saa7134_capsrc_put, \
1009   .private_value = addr }
1010 
1011 static int snd_saa7134_capsrc_info(struct snd_kcontrol * kcontrol,
1012 				   struct snd_ctl_elem_info * uinfo)
1013 {
1014 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1015 	uinfo->count = 2;
1016 	uinfo->value.integer.min = 0;
1017 	uinfo->value.integer.max = 1;
1018 	return 0;
1019 }
1020 
1021 static int snd_saa7134_capsrc_get(struct snd_kcontrol * kcontrol,
1022 				  struct snd_ctl_elem_value * ucontrol)
1023 {
1024 	snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
1025 	int addr = kcontrol->private_value;
1026 
1027 	spin_lock_irq(&chip->mixer_lock);
1028 	if (chip->capture_source_addr == addr) {
1029 		ucontrol->value.integer.value[0] = chip->capture_source[0];
1030 		ucontrol->value.integer.value[1] = chip->capture_source[1];
1031 	} else {
1032 		ucontrol->value.integer.value[0] = 0;
1033 		ucontrol->value.integer.value[1] = 0;
1034 	}
1035 	spin_unlock_irq(&chip->mixer_lock);
1036 
1037 	return 0;
1038 }
1039 
1040 static int snd_saa7134_capsrc_put(struct snd_kcontrol * kcontrol,
1041 				  struct snd_ctl_elem_value * ucontrol)
1042 {
1043 	int left, right;
1044 	left = ucontrol->value.integer.value[0] & 1;
1045 	right = ucontrol->value.integer.value[1] & 1;
1046 
1047 	return snd_saa7134_capsrc_set(kcontrol, left, right, false);
1048 }
1049 
1050 static struct snd_kcontrol_new snd_saa7134_volume_controls[] = {
1051 SAA713x_VOLUME("Video Volume", 0, MIXER_ADDR_TVTUNER),
1052 SAA713x_VOLUME("Line Volume", 1, MIXER_ADDR_LINE1),
1053 SAA713x_VOLUME("Line Volume", 2, MIXER_ADDR_LINE2),
1054 };
1055 
1056 static struct snd_kcontrol_new snd_saa7134_capture_controls[] = {
1057 SAA713x_CAPSRC("Video Capture Switch", 0, MIXER_ADDR_TVTUNER),
1058 SAA713x_CAPSRC("Line Capture Switch", 1, MIXER_ADDR_LINE1),
1059 SAA713x_CAPSRC("Line Capture Switch", 2, MIXER_ADDR_LINE2),
1060 };
1061 
1062 /*
1063  * ALSA mixer setup
1064  *
1065  *   Called when initializing the board. Sets up the name and hooks up
1066  *  the callbacks
1067  *
1068  */
1069 
1070 static int snd_card_saa7134_new_mixer(snd_card_saa7134_t * chip)
1071 {
1072 	struct snd_card *card = chip->card;
1073 	struct snd_kcontrol *kcontrol;
1074 	unsigned int idx;
1075 	int err, addr;
1076 
1077 	strscpy(card->mixername, "SAA7134 Mixer", sizeof(card->mixername));
1078 
1079 	for (idx = 0; idx < ARRAY_SIZE(snd_saa7134_volume_controls); idx++) {
1080 		kcontrol = snd_ctl_new1(&snd_saa7134_volume_controls[idx],
1081 					chip);
1082 		err = snd_ctl_add(card, kcontrol);
1083 		if (err < 0)
1084 			return err;
1085 	}
1086 
1087 	for (idx = 0; idx < ARRAY_SIZE(snd_saa7134_capture_controls); idx++) {
1088 		kcontrol = snd_ctl_new1(&snd_saa7134_capture_controls[idx],
1089 					chip);
1090 		addr = snd_saa7134_capture_controls[idx].private_value;
1091 		chip->capture_ctl[addr] = kcontrol;
1092 		err = snd_ctl_add(card, kcontrol);
1093 		if (err < 0)
1094 			return err;
1095 	}
1096 
1097 	chip->capture_source_addr = MIXER_ADDR_UNSELECTED;
1098 	return 0;
1099 }
1100 
1101 static void snd_saa7134_free(struct snd_card * card)
1102 {
1103 	snd_card_saa7134_t *chip = card->private_data;
1104 
1105 	if (chip->dev->dmasound.priv_data == NULL)
1106 		return;
1107 
1108 	if (chip->irq >= 0)
1109 		free_irq(chip->irq, &chip->dev->dmasound);
1110 
1111 	chip->dev->dmasound.priv_data = NULL;
1112 
1113 }
1114 
1115 /*
1116  * ALSA initialization
1117  *
1118  *   Called by the init routine, once for each saa7134 device present,
1119  *  it creates the basic structures and registers the ALSA devices
1120  *
1121  */
1122 
1123 static int alsa_card_saa7134_create(struct saa7134_dev *dev, int devnum)
1124 {
1125 
1126 	struct snd_card *card;
1127 	snd_card_saa7134_t *chip;
1128 	int err;
1129 
1130 
1131 	if (devnum >= SNDRV_CARDS)
1132 		return -ENODEV;
1133 	if (!enable[devnum])
1134 		return -ENODEV;
1135 
1136 	err = snd_card_new(&dev->pci->dev, index[devnum], id[devnum],
1137 			   THIS_MODULE, sizeof(snd_card_saa7134_t), &card);
1138 	if (err < 0)
1139 		return err;
1140 
1141 	strscpy(card->driver, "SAA7134", sizeof(card->driver));
1142 
1143 	/* Card "creation" */
1144 
1145 	card->private_free = snd_saa7134_free;
1146 	chip = card->private_data;
1147 
1148 	spin_lock_init(&chip->lock);
1149 	spin_lock_init(&chip->mixer_lock);
1150 
1151 	chip->dev = dev;
1152 
1153 	chip->card = card;
1154 
1155 	chip->pci = dev->pci;
1156 	chip->iobase = pci_resource_start(dev->pci, 0);
1157 
1158 
1159 	err = request_irq(dev->pci->irq, saa7134_alsa_irq,
1160 				IRQF_SHARED, dev->name,
1161 				(void*) &dev->dmasound);
1162 
1163 	if (err < 0) {
1164 		pr_err("%s: can't get IRQ %d for ALSA\n",
1165 			dev->name, dev->pci->irq);
1166 		goto __nodev;
1167 	}
1168 
1169 	chip->irq = dev->pci->irq;
1170 
1171 	mutex_init(&dev->dmasound.lock);
1172 
1173 	if ((err = snd_card_saa7134_new_mixer(chip)) < 0)
1174 		goto __nodev;
1175 
1176 	if ((err = snd_card_saa7134_pcm(chip, 0)) < 0)
1177 		goto __nodev;
1178 
1179 	/* End of "creation" */
1180 
1181 	strscpy(card->shortname, "SAA7134", sizeof(card->shortname));
1182 	sprintf(card->longname, "%s at 0x%lx irq %d",
1183 		chip->dev->name, chip->iobase, chip->irq);
1184 
1185 	pr_info("%s/alsa: %s registered as card %d\n",
1186 		dev->name, card->longname, index[devnum]);
1187 
1188 	if ((err = snd_card_register(card)) == 0) {
1189 		snd_saa7134_cards[devnum] = card;
1190 		return 0;
1191 	}
1192 
1193 __nodev:
1194 	snd_card_free(card);
1195 	return err;
1196 }
1197 
1198 
1199 static int alsa_device_init(struct saa7134_dev *dev)
1200 {
1201 	dev->dmasound.priv_data = dev;
1202 	alsa_card_saa7134_create(dev,dev->nr);
1203 	return 1;
1204 }
1205 
1206 static int alsa_device_exit(struct saa7134_dev *dev)
1207 {
1208 	if (!snd_saa7134_cards[dev->nr])
1209 		return 1;
1210 
1211 	snd_card_free(snd_saa7134_cards[dev->nr]);
1212 	snd_saa7134_cards[dev->nr] = NULL;
1213 	return 1;
1214 }
1215 
1216 /*
1217  * Module initializer
1218  *
1219  * Loops through present saa7134 cards, and assigns an ALSA device
1220  * to each one
1221  *
1222  */
1223 
1224 static int saa7134_alsa_init(void)
1225 {
1226 	struct saa7134_dev *dev = NULL;
1227 	struct list_head *list;
1228 
1229 	saa7134_dmasound_init = alsa_device_init;
1230 	saa7134_dmasound_exit = alsa_device_exit;
1231 
1232 	pr_info("saa7134 ALSA driver for DMA sound loaded\n");
1233 
1234 	list_for_each(list,&saa7134_devlist) {
1235 		dev = list_entry(list, struct saa7134_dev, devlist);
1236 		if (dev->pci->device == PCI_DEVICE_ID_PHILIPS_SAA7130)
1237 			pr_info("%s/alsa: %s doesn't support digital audio\n",
1238 				dev->name, saa7134_boards[dev->board].name);
1239 		else
1240 			alsa_device_init(dev);
1241 	}
1242 
1243 	if (dev == NULL)
1244 		pr_info("saa7134 ALSA: no saa7134 cards found\n");
1245 
1246 	return 0;
1247 
1248 }
1249 
1250 /*
1251  * Module destructor
1252  */
1253 
1254 static void saa7134_alsa_exit(void)
1255 {
1256 	int idx;
1257 
1258 	for (idx = 0; idx < SNDRV_CARDS; idx++) {
1259 		if (snd_saa7134_cards[idx])
1260 			snd_card_free(snd_saa7134_cards[idx]);
1261 	}
1262 
1263 	saa7134_dmasound_init = NULL;
1264 	saa7134_dmasound_exit = NULL;
1265 	pr_info("saa7134 ALSA driver for DMA sound unloaded\n");
1266 
1267 	return;
1268 }
1269 
1270 /* We initialize this late, to make sure the sound system is up and running */
1271 late_initcall(saa7134_alsa_init);
1272 module_exit(saa7134_alsa_exit);
1273 MODULE_LICENSE("GPL");
1274 MODULE_AUTHOR("Ricardo Cerqueira");
1275