[media] saa7134: coding style cleanups
[linux-2.6-block.git] / drivers / media / pci / saa7134 / saa7134-alsa.c
CommitLineData
5f7591c0 1/*
bd15eba3 2 * SAA713x ALSA support for V4L
5f7591c0 3 *
bd15eba3
RC
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
5f7591c0 16 *
5f7591c0
NS
17 */
18
bd15eba3
RC
19#include <linux/init.h>
20#include <linux/slab.h>
21#include <linux/time.h>
22#include <linux/wait.h>
bd15eba3 23#include <linux/module.h>
5f7591c0 24#include <sound/core.h>
5f7591c0 25#include <sound/control.h>
bd15eba3 26#include <sound/pcm.h>
d5ee43af 27#include <sound/pcm_params.h>
5f7591c0 28#include <sound/initval.h>
871242b9 29#include <linux/interrupt.h>
5f7591c0
NS
30
31#include "saa7134.h"
32#include "saa7134-reg.h"
33
ff699e6b 34static unsigned int debug;
79dd0c69
RC
35module_param(debug, int, 0644);
36MODULE_PARM_DESC(debug,"enable debug messages [alsa]");
37
b2c15ea9
RC
38/*
39 * Configuration macros
40 */
41
bd15eba3 42/* defaults */
736dadaa 43#define MIXER_ADDR_UNSELECTED -1
b2c15ea9
RC
44#define MIXER_ADDR_TVTUNER 0
45#define MIXER_ADDR_LINE1 1
46#define MIXER_ADDR_LINE2 2
47#define MIXER_ADDR_LAST 2
5f7591c0 48
674434c6 49
bd15eba3
RC
50static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
51static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
ede22415 52static int enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 1};
bd15eba3 53
b54134be 54module_param_array(index, int, NULL, 0444);
ede22415 55module_param_array(enable, int, NULL, 0444);
b54134be 56MODULE_PARM_DESC(index, "Index value for SAA7134 capture interface(s).");
ede22415 57MODULE_PARM_DESC(enable, "Enable (or not) the SAA7134 capture interface(s).");
b54134be 58
79dd0c69 59#define dprintk(fmt, arg...) if (debug) \
f85c657f 60 printk(KERN_DEBUG "%s/alsa: " fmt, dev->name , ##arg)
674434c6
MCC
61
62
bd15eba3 63
b2c15ea9
RC
64/*
65 * Main chip structure
66 */
674434c6 67
bd15eba3 68typedef struct snd_card_saa7134 {
f7cbb7fc 69 struct snd_card *card;
bd15eba3
RC
70 spinlock_t mixer_lock;
71 int mixer_volume[MIXER_ADDR_LAST+1][2];
736dadaa
OJ
72 int capture_source_addr;
73 int capture_source[2];
74 struct snd_kcontrol *capture_ctl[MIXER_ADDR_LAST+1];
4ac97914 75 struct pci_dev *pci;
4aabf633 76 struct saa7134_dev *dev;
bd15eba3 77
4ac97914 78 unsigned long iobase;
aac0ca6a
RB
79 s16 irq;
80 u16 mute_was_on;
bd15eba3
RC
81
82 spinlock_t lock;
83} snd_card_saa7134_t;
84
07eef6ce 85
b2c15ea9
RC
86/*
87 * PCM structure
88 */
89
bd15eba3 90typedef struct snd_card_saa7134_pcm {
4aabf633 91 struct saa7134_dev *dev;
bd15eba3
RC
92
93 spinlock_t lock;
4aabf633 94
f7cbb7fc 95 struct snd_pcm_substream *substream;
bd15eba3 96} snd_card_saa7134_pcm_t;
5f7591c0 97
f7cbb7fc 98static struct snd_card *snd_saa7134_cards[SNDRV_CARDS];
5f7591c0 99
79dd0c69 100
b2c15ea9
RC
101/*
102 * saa7134 DMA audio stop
103 *
104 * Called when the capture device is released or the buffer overflows
105 *
c817e763 106 * - Copied verbatim from saa7134-oss's dsp_dma_stop.
b2c15ea9
RC
107 *
108 */
109
bd15eba3
RC
110static void saa7134_dma_stop(struct saa7134_dev *dev)
111{
b54134be
RC
112 dev->dmasound.dma_blk = -1;
113 dev->dmasound.dma_running = 0;
4ac97914 114 saa7134_set_dmabits(dev);
bd15eba3 115}
5f7591c0 116
b2c15ea9
RC
117/*
118 * saa7134 DMA audio start
119 *
120 * Called when preparing the capture device for use
121 *
c817e763 122 * - Copied verbatim from saa7134-oss's dsp_dma_start.
b2c15ea9
RC
123 *
124 */
125
bd15eba3
RC
126static void saa7134_dma_start(struct saa7134_dev *dev)
127{
b54134be
RC
128 dev->dmasound.dma_blk = 0;
129 dev->dmasound.dma_running = 1;
4ac97914 130 saa7134_set_dmabits(dev);
bd15eba3 131}
5f7591c0 132
b2c15ea9
RC
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
943a4902
AB
144static void saa7134_irq_alsa_done(struct saa7134_dev *dev,
145 unsigned long status)
bd15eba3
RC
146{
147 int next_blk, reg = 0;
5f7591c0 148
bd15eba3 149 spin_lock(&dev->slock);
b54134be 150 if (UNSET == dev->dmasound.dma_blk) {
bd15eba3
RC
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 */
b54134be 158 if (0 == (dev->dmasound.dma_blk & 0x01))
bd15eba3
RC
159 reg = SAA7134_RS_BA1(6);
160 } else {
161 /* even */
b54134be 162 if (1 == (dev->dmasound.dma_blk & 0x01))
bd15eba3
RC
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 }
5f7591c0 170
b54134be
RC
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);
d5ee43af 174 spin_unlock(&dev->slock);
e6355ad7 175 snd_pcm_stream_lock(dev->dmasound.substream);
b54134be 176 snd_pcm_stop(dev->dmasound.substream,SNDRV_PCM_STATE_XRUN);
e6355ad7 177 snd_pcm_stream_unlock(dev->dmasound.substream);
d5ee43af 178 return;
bd15eba3 179 }
5f7591c0 180
4ac97914 181 /* next block addr */
b54134be
RC
182 next_blk = (dev->dmasound.dma_blk + 2) % dev->dmasound.blocks;
183 saa_writel(reg,next_blk * dev->dmasound.blksize);
79dd0c69 184 if (debug > 2)
4ac97914
MCC
185 dprintk("irq: ok, %s, next_blk=%d, addr=%x, blocks=%u, size=%u, read=%u\n",
186 (status & 0x10000000) ? "even" : "odd ", next_blk,
b54134be 187 next_blk * dev->dmasound.blksize, dev->dmasound.blocks, dev->dmasound.blksize, dev->dmasound.read_count);
5f7591c0 188
4ac97914 189 /* update status & wake waiting readers */
b54134be
RC
190 dev->dmasound.dma_blk = (dev->dmasound.dma_blk + 1) % dev->dmasound.blocks;
191 dev->dmasound.read_count += dev->dmasound.blksize;
5f7591c0 192
b54134be 193 dev->dmasound.recording_on = reg;
5f7591c0 194
b54134be 195 if (dev->dmasound.read_count >= snd_pcm_lib_period_bytes(dev->dmasound.substream)) {
4ac97914 196 spin_unlock(&dev->slock);
b54134be 197 snd_pcm_period_elapsed(dev->dmasound.substream);
4ac97914 198 spin_lock(&dev->slock);
bd15eba3 199 }
4aabf633 200
bd15eba3 201 done:
4ac97914 202 spin_unlock(&dev->slock);
5f7591c0 203
bd15eba3 204}
5f7591c0 205
79dd0c69
RC
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
7d12e780 214static irqreturn_t saa7134_alsa_irq(int irq, void *dev_id)
79dd0c69 215{
afd1a0c9
MCC
216 struct saa7134_dmasound *dmasound = dev_id;
217 struct saa7134_dev *dev = dmasound->priv_data;
4aabf633 218
79dd0c69
RC
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;
843b1378
HL
228 saa_writel(SAA7134_IRQ_REPORT,
229 SAA7134_IRQ_REPORT_DONE_RA3);
79dd0c69
RC
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
240out:
241 return IRQ_RETVAL(handled);
242}
243
b2c15ea9
RC
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 */
5f7591c0 253
f7cbb7fc 254static int snd_card_saa7134_capture_trigger(struct snd_pcm_substream * substream,
bd15eba3 255 int cmd)
5f7591c0 256{
f7cbb7fc 257 struct snd_pcm_runtime *runtime = substream->runtime;
4aabf633
RC
258 snd_card_saa7134_pcm_t *pcm = runtime->private_data;
259 struct saa7134_dev *dev=pcm->dev;
b54134be
RC
260 int err = 0;
261
d5ee43af 262 spin_lock(&dev->slock);
c817e763 263 if (cmd == SNDRV_PCM_TRIGGER_START) {
b54134be
RC
264 /* start dma */
265 saa7134_dma_start(dev);
c817e763 266 } else if (cmd == SNDRV_PCM_TRIGGER_STOP) {
b54134be
RC
267 /* stop dma */
268 saa7134_dma_stop(dev);
c817e763
RC
269 } else {
270 err = -EINVAL;
271 }
d5ee43af 272 spin_unlock(&dev->slock);
b54134be 273
c817e763 274 return err;
5f7591c0
NS
275}
276
b2c15ea9
RC
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 *
c817e763
RC
284 * - Copied verbatim from saa7134-oss.
285 *
b2c15ea9
RC
286 */
287
bd15eba3 288static int dsp_buffer_init(struct saa7134_dev *dev)
5f7591c0 289{
4ac97914
MCC
290 int err;
291
4aabf633
RC
292 BUG_ON(!dev->dmasound.bufsize);
293
b54134be
RC
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);
4ac97914
MCC
297 if (0 != err)
298 return err;
299 return 0;
5f7591c0
NS
300}
301
d5ee43af
RC
302/*
303 * DMA buffer release
304 *
305 * Called after closing the device, during snd_card_saa7134_capture_close
306 *
307 */
308
309static int dsp_buffer_free(struct saa7134_dev *dev)
310{
ae24601b 311 BUG_ON(!dev->dmasound.blksize);
d5ee43af
RC
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
b930e1d8 319 return 0;
d5ee43af
RC
320}
321
736dadaa
OJ
322/*
323 * Setting the capture source and updating the ALSA controls
324 */
325static 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}
d5ee43af 431
b2c15ea9
RC
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 */
5f7591c0 443
f7cbb7fc 444static int snd_card_saa7134_capture_prepare(struct snd_pcm_substream * substream)
5f7591c0 445{
f7cbb7fc 446 struct snd_pcm_runtime *runtime = substream->runtime;
d5ee43af 447 int bswap, sign;
4ac97914 448 u32 fmt, control;
bd15eba3 449 snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
4ac97914 450 struct saa7134_dev *dev;
4aabf633 451 snd_card_saa7134_pcm_t *pcm = runtime->private_data;
bd15eba3 452
4aabf633 453 pcm->dev->dmasound.substream = substream;
bd15eba3 454
d5ee43af 455 dev = saa7134->dev;
bd15eba3 456
d5ee43af 457 if (snd_pcm_format_width(runtime->format) == 8)
bd15eba3 458 fmt = 0x00;
d5ee43af 459 else
bd15eba3 460 fmt = 0x01;
4ac97914 461
d5ee43af 462 if (snd_pcm_format_signed(runtime->format))
bd15eba3 463 sign = 1;
d5ee43af 464 else
bd15eba3 465 sign = 0;
bd15eba3 466
d5ee43af
RC
467 if (snd_pcm_format_big_endian(runtime->format))
468 bswap = 1;
469 else
470 bswap = 0;
4ac97914
MCC
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
b54134be
RC
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);
4ac97914
MCC
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;
b54134be 496 saa_writel(SAA7133_NUM_SAMPLES, dev->dmasound.blksize -1);
4ac97914 497 saa_writel(SAA7133_AUDIO_CHANNEL, 0x543210 | (fmt << 24));
4ac97914
MCC
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 |
b54134be 507 (dev->dmasound.pt.dma >> 12);
4ac97914
MCC
508 if (bswap)
509 control |= SAA7134_RS_CONTROL_BSWAP;
bd15eba3 510
4ac97914 511 saa_writel(SAA7134_RS_BA1(6),0);
b54134be 512 saa_writel(SAA7134_RS_BA2(6),dev->dmasound.blksize);
4ac97914
MCC
513 saa_writel(SAA7134_RS_PITCH(6),0);
514 saa_writel(SAA7134_RS_CONTROL(6),control);
bd15eba3 515
b54134be 516 dev->dmasound.rate = runtime->rate;
5f7591c0 517
736dadaa
OJ
518 /* Setup and update the card/ALSA controls */
519 snd_saa7134_capsrc_set(saa7134->capture_ctl[dev->dmasound.input], 1, 1,
520 true);
521
bd15eba3 522 return 0;
5f7591c0 523
bd15eba3 524}
5f7591c0 525
b2c15ea9
RC
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 */
5f7591c0 536
f7cbb7fc
TI
537static snd_pcm_uframes_t
538snd_card_saa7134_capture_pointer(struct snd_pcm_substream * substream)
5f7591c0 539{
f7cbb7fc 540 struct snd_pcm_runtime *runtime = substream->runtime;
4aabf633
RC
541 snd_card_saa7134_pcm_t *pcm = runtime->private_data;
542 struct saa7134_dev *dev=pcm->dev;
5f7591c0 543
b54134be
RC
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;
bd15eba3 549 }
5f7591c0 550
b54134be 551 return bytes_to_frames(runtime, dev->dmasound.read_offset);
bd15eba3 552}
5f7591c0 553
b2c15ea9
RC
554/*
555 * ALSA hardware capabilities definition
7b543602
OJ
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
b2c15ea9 566 */
5f7591c0 567
f7cbb7fc 568static struct snd_pcm_hardware snd_card_saa7134_capture =
5f7591c0 569{
4ac97914 570 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
f2421ca3
MCC
571 SNDRV_PCM_INFO_BLOCK_TRANSFER |
572 SNDRV_PCM_INFO_MMAP_VALID),
b54134be
RC
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,
7b543602 579 .rates = SNDRV_PCM_RATE_32000,
b54134be 580 .rate_min = 32000,
7b543602 581 .rate_max = 32000,
b54134be
RC
582 .channels_min = 1,
583 .channels_max = 2,
584 .buffer_bytes_max = (256*1024),
bd15eba3 585 .period_bytes_min = 64,
b54134be 586 .period_bytes_max = (256*1024),
174eb8e8 587 .periods_min = 4,
b54134be 588 .periods_max = 1024,
bd15eba3 589};
5f7591c0 590
f7cbb7fc 591static void snd_card_saa7134_runtime_free(struct snd_pcm_runtime *runtime)
bd15eba3 592{
4aabf633 593 snd_card_saa7134_pcm_t *pcm = runtime->private_data;
bd15eba3 594
4aabf633 595 kfree(pcm);
5f7591c0
NS
596}
597
bd15eba3 598
b2c15ea9
RC
599/*
600 * ALSA hardware params
601 *
602 * - One of the ALSA capture callbacks.
603 *
604 * Called on initialization, right before the PCM preparation
b2c15ea9
RC
605 *
606 */
607
f7cbb7fc
TI
608static int snd_card_saa7134_hw_params(struct snd_pcm_substream * substream,
609 struct snd_pcm_hw_params * hw_params)
5f7591c0 610{
d5ee43af
RC
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
5e246b85
TI
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;
d5ee43af
RC
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);
95268403 635 videobuf_dma_unmap(&dev->pci->dev, &dev->dmasound.dma);
d5ee43af
RC
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
e72936d2
HV
651 err = videobuf_dma_map(&dev->pci->dev, &dev->dmasound.dma);
652 if (err) {
d5ee43af
RC
653 dsp_buffer_free(dev);
654 return err;
655 }
e72936d2
HV
656 err = saa7134_pgtable_alloc(dev->pci, &dev->dmasound.pt);
657 if (err) {
95268403 658 videobuf_dma_unmap(&dev->pci->dev, &dev->dmasound.dma);
d5ee43af
RC
659 dsp_buffer_free(dev);
660 return err;
661 }
e72936d2
HV
662 err = saa7134_pgtable_build(dev->pci, &dev->dmasound.pt,
663 dev->dmasound.sglist, dev->dmasound.sglen, 0);
664 if (err) {
d5ee43af 665 saa7134_pgtable_free(dev->pci, &dev->dmasound.pt);
95268403 666 videobuf_dma_unmap(&dev->pci->dev, &dev->dmasound.dma);
d5ee43af
RC
667 dsp_buffer_free(dev);
668 return err;
669 }
670
671 /* I should be able to use runtime->dma_addr in the control
672 byte, but it doesn't work. So I allocate the DMA using the
673 V4L functions, and force ALSA to use that as the DMA area */
674
bb6dbe74 675 substream->runtime->dma_area = dev->dmasound.dma.vaddr;
32d111a9
AP
676 substream->runtime->dma_bytes = dev->dmasound.bufsize;
677 substream->runtime->dma_addr = 0;
d5ee43af 678
32d111a9 679 return 0;
bd15eba3 680
5f7591c0
NS
681}
682
b2c15ea9
RC
683/*
684 * ALSA hardware release
685 *
686 * - One of the ALSA capture callbacks.
687 *
688 * Called after closing the device, but before snd_card_saa7134_capture_close
d5ee43af 689 * It stops the DMA audio and releases the buffers.
b2c15ea9
RC
690 *
691 */
692
f7cbb7fc 693static int snd_card_saa7134_hw_free(struct snd_pcm_substream * substream)
5f7591c0 694{
d5ee43af
RC
695 snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
696 struct saa7134_dev *dev;
5f7591c0 697
d5ee43af 698 dev = saa7134->dev;
5f7591c0 699
d5ee43af
RC
700 if (substream->runtime->dma_area) {
701 saa7134_pgtable_free(dev->pci, &dev->dmasound.pt);
95268403 702 videobuf_dma_unmap(&dev->pci->dev, &dev->dmasound.dma);
d5ee43af
RC
703 dsp_buffer_free(dev);
704 substream->runtime->dma_area = NULL;
705 }
bd15eba3 706
4ac97914 707 return 0;
5f7591c0
NS
708}
709
b2c15ea9
RC
710/*
711 * ALSA capture finish
712 *
713 * - One of the ALSA capture callbacks.
714 *
d5ee43af 715 * Called after closing the device.
b2c15ea9
RC
716 *
717 */
bd15eba3 718
f7cbb7fc 719static int snd_card_saa7134_capture_close(struct snd_pcm_substream * substream)
5f7591c0 720{
dafcaaf8
RC
721 snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
722 struct saa7134_dev *dev = saa7134->dev;
723
aac0ca6a
RB
724 if (saa7134->mute_was_on) {
725 dev->ctl_mute = 1;
726 saa7134_tvaudio_setmute(dev);
727 }
4ac97914 728 return 0;
5f7591c0
NS
729}
730
b2c15ea9
RC
731/*
732 * ALSA capture start
733 *
734 * - One of the ALSA capture callbacks.
735 *
736 * Called when opening the device. It creates and populates the PCM
737 * structure
738 *
739 */
5f7591c0 740
f7cbb7fc 741static int snd_card_saa7134_capture_open(struct snd_pcm_substream * substream)
b2c15ea9 742{
f7cbb7fc 743 struct snd_pcm_runtime *runtime = substream->runtime;
4aabf633 744 snd_card_saa7134_pcm_t *pcm;
4ac97914 745 snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
83ee87a3 746 struct saa7134_dev *dev;
5cfbacd0 747 int amux, err;
5f7591c0 748
83ee87a3
MCC
749 if (!saa7134) {
750 printk(KERN_ERR "BUG: saa7134 can't find device struct."
751 " Can't proceed with open\n");
752 return -ENODEV;
753 }
754 dev = saa7134->dev;
3593cab5 755 mutex_lock(&dev->dmasound.lock);
5f7591c0 756
b54134be
RC
757 dev->dmasound.read_count = 0;
758 dev->dmasound.read_offset = 0;
bd15eba3 759
5cfbacd0
HH
760 amux = dev->input->amux;
761 if ((amux < 1) || (amux > 3))
762 amux = 1;
763 dev->dmasound.input = amux - 1;
764
3593cab5 765 mutex_unlock(&dev->dmasound.lock);
5f7591c0 766
4aabf633
RC
767 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
768 if (pcm == NULL)
bd15eba3 769 return -ENOMEM;
bd15eba3 770
4aabf633
RC
771 pcm->dev=saa7134->dev;
772
773 spin_lock_init(&pcm->lock);
bd15eba3 774
4aabf633
RC
775 pcm->substream = substream;
776 runtime->private_data = pcm;
bd15eba3
RC
777 runtime->private_free = snd_card_saa7134_runtime_free;
778 runtime->hw = snd_card_saa7134_capture;
779
aac0ca6a
RB
780 if (dev->ctl_mute != 0) {
781 saa7134->mute_was_on = 1;
782 dev->ctl_mute = 0;
783 saa7134_tvaudio_setmute(dev);
784 }
dafcaaf8 785
174eb8e8
HL
786 err = snd_pcm_hw_constraint_integer(runtime,
787 SNDRV_PCM_HW_PARAM_PERIODS);
788 if (err < 0)
789 return err;
790
791 err = snd_pcm_hw_constraint_step(runtime, 0,
792 SNDRV_PCM_HW_PARAM_PERIODS, 2);
793 if (err < 0)
4ac97914 794 return err;
5f7591c0
NS
795
796 return 0;
797}
798
32d111a9
AP
799/*
800 * page callback (needed for mmap)
801 */
802
803static struct page *snd_card_saa7134_page(struct snd_pcm_substream *substream,
804 unsigned long offset)
805{
806 void *pageptr = substream->runtime->dma_area + offset;
807 return vmalloc_to_page(pageptr);
808}
809
b2c15ea9
RC
810/*
811 * ALSA capture callbacks definition
812 */
5f7591c0 813
f7cbb7fc 814static struct snd_pcm_ops snd_card_saa7134_capture_ops = {
bd15eba3
RC
815 .open = snd_card_saa7134_capture_open,
816 .close = snd_card_saa7134_capture_close,
817 .ioctl = snd_pcm_lib_ioctl,
818 .hw_params = snd_card_saa7134_hw_params,
819 .hw_free = snd_card_saa7134_hw_free,
820 .prepare = snd_card_saa7134_capture_prepare,
821 .trigger = snd_card_saa7134_capture_trigger,
822 .pointer = snd_card_saa7134_capture_pointer,
32d111a9 823 .page = snd_card_saa7134_page,
5f7591c0
NS
824};
825
b2c15ea9
RC
826/*
827 * ALSA PCM setup
828 *
829 * Called when initializing the board. Sets up the name and hooks up
830 * the callbacks
831 *
832 */
833
834static int snd_card_saa7134_pcm(snd_card_saa7134_t *saa7134, int device)
bd15eba3 835{
f7cbb7fc 836 struct snd_pcm *pcm;
bd15eba3 837 int err;
5f7591c0 838
bd15eba3
RC
839 if ((err = snd_pcm_new(saa7134->card, "SAA7134 PCM", device, 0, 1, &pcm)) < 0)
840 return err;
841 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_card_saa7134_capture_ops);
842 pcm->private_data = saa7134;
843 pcm->info_flags = 0;
844 strcpy(pcm->name, "SAA7134 PCM");
bd15eba3
RC
845 return 0;
846}
5f7591c0 847
bd15eba3
RC
848#define SAA713x_VOLUME(xname, xindex, addr) \
849{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
850 .info = snd_saa7134_volume_info, \
851 .get = snd_saa7134_volume_get, .put = snd_saa7134_volume_put, \
852 .private_value = addr }
5f7591c0 853
f7cbb7fc
TI
854static int snd_saa7134_volume_info(struct snd_kcontrol * kcontrol,
855 struct snd_ctl_elem_info * uinfo)
bd15eba3
RC
856{
857 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
858 uinfo->count = 2;
859 uinfo->value.integer.min = 0;
860 uinfo->value.integer.max = 20;
861 return 0;
862}
5f7591c0 863
f7cbb7fc
TI
864static int snd_saa7134_volume_get(struct snd_kcontrol * kcontrol,
865 struct snd_ctl_elem_value * ucontrol)
bd15eba3
RC
866{
867 snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
bd15eba3
RC
868 int addr = kcontrol->private_value;
869
bd15eba3
RC
870 ucontrol->value.integer.value[0] = chip->mixer_volume[addr][0];
871 ucontrol->value.integer.value[1] = chip->mixer_volume[addr][1];
bd15eba3
RC
872 return 0;
873}
5f7591c0 874
f7cbb7fc
TI
875static int snd_saa7134_volume_put(struct snd_kcontrol * kcontrol,
876 struct snd_ctl_elem_value * ucontrol)
5f7591c0 877{
bd15eba3 878 snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
5cfbacd0
HH
879 struct saa7134_dev *dev = chip->dev;
880
bd15eba3
RC
881 int change, addr = kcontrol->private_value;
882 int left, right;
883
884 left = ucontrol->value.integer.value[0];
885 if (left < 0)
886 left = 0;
887 if (left > 20)
888 left = 20;
889 right = ucontrol->value.integer.value[1];
890 if (right < 0)
891 right = 0;
892 if (right > 20)
893 right = 20;
d5ee43af 894 spin_lock_irq(&chip->mixer_lock);
5cfbacd0
HH
895 change = 0;
896 if (chip->mixer_volume[addr][0] != left) {
897 change = 1;
898 right = left;
899 }
900 if (chip->mixer_volume[addr][1] != right) {
901 change = 1;
902 left = right;
903 }
904 if (change) {
905 switch (dev->pci->device) {
906 case PCI_DEVICE_ID_PHILIPS_SAA7134:
907 switch (addr) {
908 case MIXER_ADDR_TVTUNER:
909 left = 20;
910 break;
911 case MIXER_ADDR_LINE1:
912 saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x10,
913 (left > 10) ? 0x00 : 0x10);
914 break;
915 case MIXER_ADDR_LINE2:
916 saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x20,
917 (left > 10) ? 0x00 : 0x20);
918 break;
919 }
920 break;
921 case PCI_DEVICE_ID_PHILIPS_SAA7133:
922 case PCI_DEVICE_ID_PHILIPS_SAA7135:
923 switch (addr) {
924 case MIXER_ADDR_TVTUNER:
925 left = 20;
926 break;
927 case MIXER_ADDR_LINE1:
928 saa_andorb(0x0594, 0x10,
929 (left > 10) ? 0x00 : 0x10);
930 break;
931 case MIXER_ADDR_LINE2:
932 saa_andorb(0x0594, 0x20,
933 (left > 10) ? 0x00 : 0x20);
934 break;
935 }
936 break;
937 }
938 chip->mixer_volume[addr][0] = left;
939 chip->mixer_volume[addr][1] = right;
940 }
d5ee43af 941 spin_unlock_irq(&chip->mixer_lock);
bd15eba3
RC
942 return change;
943}
5f7591c0 944
bd15eba3
RC
945#define SAA713x_CAPSRC(xname, xindex, addr) \
946{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
947 .info = snd_saa7134_capsrc_info, \
948 .get = snd_saa7134_capsrc_get, .put = snd_saa7134_capsrc_put, \
949 .private_value = addr }
5f7591c0 950
f7cbb7fc
TI
951static int snd_saa7134_capsrc_info(struct snd_kcontrol * kcontrol,
952 struct snd_ctl_elem_info * uinfo)
bd15eba3
RC
953{
954 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
b2c15ea9 955 uinfo->count = 2;
bd15eba3
RC
956 uinfo->value.integer.min = 0;
957 uinfo->value.integer.max = 1;
958 return 0;
959}
5f7591c0 960
f7cbb7fc
TI
961static int snd_saa7134_capsrc_get(struct snd_kcontrol * kcontrol,
962 struct snd_ctl_elem_value * ucontrol)
bd15eba3
RC
963{
964 snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
bd15eba3
RC
965 int addr = kcontrol->private_value;
966
d5ee43af 967 spin_lock_irq(&chip->mixer_lock);
736dadaa
OJ
968 if (chip->capture_source_addr == addr) {
969 ucontrol->value.integer.value[0] = chip->capture_source[0];
970 ucontrol->value.integer.value[1] = chip->capture_source[1];
971 } else {
972 ucontrol->value.integer.value[0] = 0;
973 ucontrol->value.integer.value[1] = 0;
974 }
d5ee43af 975 spin_unlock_irq(&chip->mixer_lock);
4aabf633 976
5f7591c0
NS
977 return 0;
978}
979
f7cbb7fc
TI
980static int snd_saa7134_capsrc_put(struct snd_kcontrol * kcontrol,
981 struct snd_ctl_elem_value * ucontrol)
5f7591c0 982{
bd15eba3 983 int left, right;
bd15eba3
RC
984 left = ucontrol->value.integer.value[0] & 1;
985 right = ucontrol->value.integer.value[1] & 1;
bd15eba3 986
736dadaa 987 return snd_saa7134_capsrc_set(kcontrol, left, right, false);
5f7591c0
NS
988}
989
736dadaa 990static struct snd_kcontrol_new snd_saa7134_volume_controls[] = {
bd15eba3 991SAA713x_VOLUME("Video Volume", 0, MIXER_ADDR_TVTUNER),
bd15eba3 992SAA713x_VOLUME("Line Volume", 1, MIXER_ADDR_LINE1),
bd15eba3 993SAA713x_VOLUME("Line Volume", 2, MIXER_ADDR_LINE2),
736dadaa
OJ
994};
995
996static struct snd_kcontrol_new snd_saa7134_capture_controls[] = {
997SAA713x_CAPSRC("Video Capture Switch", 0, MIXER_ADDR_TVTUNER),
998SAA713x_CAPSRC("Line Capture Switch", 1, MIXER_ADDR_LINE1),
bd15eba3
RC
999SAA713x_CAPSRC("Line Capture Switch", 2, MIXER_ADDR_LINE2),
1000};
5f7591c0 1001
b2c15ea9
RC
1002/*
1003 * ALSA mixer setup
1004 *
1005 * Called when initializing the board. Sets up the name and hooks up
1006 * the callbacks
1007 *
1008 */
1009
1010static int snd_card_saa7134_new_mixer(snd_card_saa7134_t * chip)
bd15eba3 1011{
f7cbb7fc 1012 struct snd_card *card = chip->card;
736dadaa 1013 struct snd_kcontrol *kcontrol;
bd15eba3 1014 unsigned int idx;
736dadaa 1015 int err, addr;
bd15eba3 1016
bd15eba3 1017 strcpy(card->mixername, "SAA7134 Mixer");
5f7591c0 1018
736dadaa
OJ
1019 for (idx = 0; idx < ARRAY_SIZE(snd_saa7134_volume_controls); idx++) {
1020 kcontrol = snd_ctl_new1(&snd_saa7134_volume_controls[idx],
1021 chip);
1022 err = snd_ctl_add(card, kcontrol);
1023 if (err < 0)
bd15eba3
RC
1024 return err;
1025 }
736dadaa
OJ
1026
1027 for (idx = 0; idx < ARRAY_SIZE(snd_saa7134_capture_controls); idx++) {
1028 kcontrol = snd_ctl_new1(&snd_saa7134_capture_controls[idx],
1029 chip);
1030 addr = snd_saa7134_capture_controls[idx].private_value;
1031 chip->capture_ctl[addr] = kcontrol;
1032 err = snd_ctl_add(card, kcontrol);
1033 if (err < 0)
1034 return err;
1035 }
1036
1037 chip->capture_source_addr = MIXER_ADDR_UNSELECTED;
bd15eba3
RC
1038 return 0;
1039}
1040
f7cbb7fc 1041static void snd_saa7134_free(struct snd_card * card)
5f7591c0 1042{
d5ee43af 1043 snd_card_saa7134_t *chip = card->private_data;
c817e763 1044
4aabf633 1045 if (chip->dev->dmasound.priv_data == NULL)
d5ee43af 1046 return;
4aabf633 1047
f000fd80 1048 if (chip->irq >= 0)
4aabf633 1049 free_irq(chip->irq, &chip->dev->dmasound);
c817e763 1050
4aabf633
RC
1051 chip->dev->dmasound.priv_data = NULL;
1052
bd15eba3 1053}
5f7591c0 1054
b2c15ea9
RC
1055/*
1056 * ALSA initialization
1057 *
c817e763
RC
1058 * Called by the init routine, once for each saa7134 device present,
1059 * it creates the basic structures and registers the ALSA devices
b2c15ea9
RC
1060 *
1061 */
1062
943a4902 1063static int alsa_card_saa7134_create(struct saa7134_dev *dev, int devnum)
bd15eba3 1064{
79dd0c69 1065
f7cbb7fc 1066 struct snd_card *card;
b2c15ea9 1067 snd_card_saa7134_t *chip;
bd15eba3 1068 int err;
5f7591c0 1069
79dd0c69 1070
4aabf633 1071 if (devnum >= SNDRV_CARDS)
4ac97914 1072 return -ENODEV;
4aabf633 1073 if (!enable[devnum])
bd15eba3 1074 return -ENODEV;
b2c15ea9 1075
e7356888
TI
1076 err = snd_card_new(&dev->pci->dev, index[devnum], id[devnum],
1077 THIS_MODULE, sizeof(snd_card_saa7134_t), &card);
758021bf
TI
1078 if (err < 0)
1079 return err;
5f7591c0 1080
bd15eba3 1081 strcpy(card->driver, "SAA7134");
5f7591c0 1082
bd15eba3 1083 /* Card "creation" */
b2c15ea9 1084
c817e763 1085 card->private_free = snd_saa7134_free;
abf84383 1086 chip = card->private_data;
5f7591c0 1087
4ac97914 1088 spin_lock_init(&chip->lock);
b54134be 1089 spin_lock_init(&chip->mixer_lock);
5f7591c0 1090
4aabf633 1091 chip->dev = dev;
5f7591c0 1092
4ac97914 1093 chip->card = card;
b2c15ea9 1094
4aabf633 1095 chip->pci = dev->pci;
4aabf633
RC
1096 chip->iobase = pci_resource_start(dev->pci, 0);
1097
5f7591c0 1098
4aabf633 1099 err = request_irq(dev->pci->irq, saa7134_alsa_irq,
3e018fe4 1100 IRQF_SHARED, dev->name,
4aabf633 1101 (void*) &dev->dmasound);
79dd0c69
RC
1102
1103 if (err < 0) {
1104 printk(KERN_ERR "%s: can't get IRQ %d for ALSA\n",
4aabf633 1105 dev->name, dev->pci->irq);
b54134be 1106 goto __nodev;
79dd0c69
RC
1107 }
1108
d5ee43af 1109 chip->irq = dev->pci->irq;
4aabf633 1110
3593cab5 1111 mutex_init(&dev->dmasound.lock);
5f7591c0 1112
bd15eba3
RC
1113 if ((err = snd_card_saa7134_new_mixer(chip)) < 0)
1114 goto __nodev;
5f7591c0 1115
bd15eba3
RC
1116 if ((err = snd_card_saa7134_pcm(chip, 0)) < 0)
1117 goto __nodev;
b2c15ea9 1118
bd15eba3 1119 /* End of "creation" */
5f7591c0 1120
bd15eba3 1121 strcpy(card->shortname, "SAA7134");
4ac97914 1122 sprintf(card->longname, "%s at 0x%lx irq %d",
4aabf633 1123 chip->dev->name, chip->iobase, chip->irq);
5f7591c0 1124
f5b974cb
RC
1125 printk(KERN_INFO "%s/alsa: %s registered as card %d\n",dev->name,card->longname,index[devnum]);
1126
bd15eba3 1127 if ((err = snd_card_register(card)) == 0) {
4aabf633 1128 snd_saa7134_cards[devnum] = card;
bd15eba3
RC
1129 return 0;
1130 }
1131
1132__nodev:
1133 snd_card_free(card);
bd15eba3 1134 return err;
5f7591c0
NS
1135}
1136
f5b974cb
RC
1137
1138static int alsa_device_init(struct saa7134_dev *dev)
1139{
1140 dev->dmasound.priv_data = dev;
1141 alsa_card_saa7134_create(dev,dev->nr);
1142 return 1;
1143}
1144
1145static int alsa_device_exit(struct saa7134_dev *dev)
1146{
1147
1148 snd_card_free(snd_saa7134_cards[dev->nr]);
1149 snd_saa7134_cards[dev->nr] = NULL;
1150 return 1;
1151}
1152
79dd0c69
RC
1153/*
1154 * Module initializer
1155 *
1156 * Loops through present saa7134 cards, and assigns an ALSA device
1157 * to each one
1158 *
1159 */
1160
1161static int saa7134_alsa_init(void)
1162{
4aabf633 1163 struct saa7134_dev *dev = NULL;
c817e763 1164 struct list_head *list;
79dd0c69 1165
166fb6b4
AB
1166 saa7134_dmasound_init = alsa_device_init;
1167 saa7134_dmasound_exit = alsa_device_exit;
7bb95296 1168
afd1a0c9 1169 printk(KERN_INFO "saa7134 ALSA driver for DMA sound loaded\n");
c817e763
RC
1170
1171 list_for_each(list,&saa7134_devlist) {
4aabf633 1172 dev = list_entry(list, struct saa7134_dev, devlist);
40c41c8c
MCC
1173 if (dev->pci->device == PCI_DEVICE_ID_PHILIPS_SAA7130)
1174 printk(KERN_INFO "%s/alsa: %s doesn't support digital audio\n",
1175 dev->name, saa7134_boards[dev->board].name);
1176 else
1177 alsa_device_init(dev);
c817e763 1178 }
79dd0c69 1179
4aabf633 1180 if (dev == NULL)
79dd0c69
RC
1181 printk(KERN_INFO "saa7134 ALSA: no saa7134 cards found\n");
1182
1183 return 0;
674434c6 1184
79dd0c69
RC
1185}
1186
1187/*
1188 * Module destructor
1189 */
1190
943a4902 1191static void saa7134_alsa_exit(void)
5f7591c0 1192{
4ac97914 1193 int idx;
b54134be 1194
79dd0c69 1195 for (idx = 0; idx < SNDRV_CARDS; idx++) {
4ac97914 1196 snd_card_free(snd_saa7134_cards[idx]);
bd15eba3 1197 }
b54134be 1198
78457018
AB
1199 saa7134_dmasound_init = NULL;
1200 saa7134_dmasound_exit = NULL;
79dd0c69 1201 printk(KERN_INFO "saa7134 ALSA driver for DMA sound unloaded\n");
b54134be 1202
79dd0c69 1203 return;
5f7591c0 1204}
79dd0c69 1205
c7ac6b42
LT
1206/* We initialize this late, to make sure the sound system is up and running */
1207late_initcall(saa7134_alsa_init);
79dd0c69
RC
1208module_exit(saa7134_alsa_exit);
1209MODULE_LICENSE("GPL");
1210MODULE_AUTHOR("Ricardo Cerqueira");