[PATCH] v4l: 725: fixed kernel oops when hotswapping pc cards
[linux-2.6] / drivers / media / video / saa7134 / saa7134-alsa.c
1 /*
2  *   SAA713x ALSA support for V4L
3  *   Ricardo Cerqueira <v4l@cerqueira.org>
4  *
5  *
6  *   Caveats:
7  *        - Volume doesn't work (it's always at max)
8  *
9  *   This program is free software; you can redistribute it and/or modify
10  *   it under the terms of the GNU General Public License as published by
11  *   the Free Software Foundation, version 2
12  *
13  *   This program is distributed in the hope that it will be useful,
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *   GNU General Public License for more details.
17  *
18  *   You should have received a copy of the GNU General Public License
19  *   along with this program; if not, write to the Free Software
20  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21  *
22  */
23
24 #include <sound/driver.h>
25 #include <linux/init.h>
26 #include <linux/slab.h>
27 #include <linux/time.h>
28 #include <linux/wait.h>
29 #include <linux/moduleparam.h>
30 #include <linux/module.h>
31 #include <sound/core.h>
32 #include <sound/control.h>
33 #include <sound/pcm.h>
34 #include <sound/rawmidi.h>
35 #include <sound/initval.h>
36
37 #include "saa7134.h"
38 #include "saa7134-reg.h"
39
40 static unsigned int alsa_debug  = 0;
41 module_param(alsa_debug, int, 0644);
42 MODULE_PARM_DESC(alsa_debug,"enable debug messages [alsa]");
43
44 /*
45  * Configuration macros
46  */
47
48 #define MAX_PCM_DEVICES         1
49 #define MAX_PCM_SUBSTREAMS      1
50
51 /* defaults */
52 #define MAX_BUFFER_SIZE         (256*1024)
53 #define USE_FORMATS             SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE
54 #define USE_RATE                SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_48000
55 #define USE_RATE_MIN            32000
56 #define USE_RATE_MAX            48000
57 #define USE_CHANNELS_MIN        1
58 #define USE_CHANNELS_MAX        2
59 #ifndef USE_PERIODS_MIN
60 #define USE_PERIODS_MIN         2
61 #endif
62 #ifndef USE_PERIODS_MAX
63 #define USE_PERIODS_MAX         1024
64 #endif
65
66 #define MIXER_ADDR_TVTUNER      0
67 #define MIXER_ADDR_LINE1        1
68 #define MIXER_ADDR_LINE2        2
69 #define MIXER_ADDR_LAST         2
70
71 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;      /* Index 0-MAX */
72 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;       /* ID for this card */
73 static int enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0};
74
75 #define dprintk(fmt, arg...)    if (alsa_debug) \
76         printk(KERN_DEBUG "%s/alsa: " fmt, dev->name , ## arg)
77
78 /*
79  * Main chip structure
80  */
81 typedef struct snd_card_saa7134 {
82         snd_card_t *card;
83         spinlock_t mixer_lock;
84         int mixer_volume[MIXER_ADDR_LAST+1][2];
85         int capture_source[MIXER_ADDR_LAST+1][2];
86         struct pci_dev *pci;
87         struct saa7134_dev *saadev;
88
89         unsigned long iobase;
90         int irq;
91
92         spinlock_t lock;
93 } snd_card_saa7134_t;
94
95 /*
96  * PCM structure
97  */
98
99 typedef struct snd_card_saa7134_pcm {
100         struct saa7134_dev *saadev;
101
102         spinlock_t lock;
103         unsigned int pcm_size;          /* buffer size */
104         unsigned int pcm_count;         /* bytes per period */
105         unsigned int pcm_bps;           /* bytes per second */
106         snd_pcm_substream_t *substream;
107 } snd_card_saa7134_pcm_t;
108
109 static snd_card_t *snd_saa7134_cards[SNDRV_CARDS] = SNDRV_DEFAULT_PTR;
110
111 /*
112  * saa7134 DMA audio stop
113  *
114  *   Called when the capture device is released or the buffer overflows
115  *
116  *   - Copied verbatim from saa7134-oss's dsp_dma_stop. Can be dropped
117  *     if we just share dsp_dma_stop and use it here
118  *
119  */
120
121 static void saa7134_dma_stop(struct saa7134_dev *dev)
122
123 {
124         dev->oss.dma_blk     = -1;
125         dev->oss.dma_running = 0;
126         saa7134_set_dmabits(dev);
127 }
128
129 /*
130  * saa7134 DMA audio start
131  *
132  *   Called when preparing the capture device for use
133  *
134  *   - Copied verbatim from saa7134-oss's dsp_dma_start. Can be dropped
135  *     if we just share dsp_dma_start and use it here
136  *
137  */
138
139 static void saa7134_dma_start(struct saa7134_dev *dev)
140 {
141         dev->oss.dma_blk     = 0;
142         dev->oss.dma_running = 1;
143         saa7134_set_dmabits(dev);
144 }
145
146 /*
147  * saa7134 audio DMA IRQ handler
148  *
149  *   Called whenever we get an SAA7134_IRQ_REPORT_DONE_RA3 interrupt
150  *   Handles shifting between the 2 buffers, manages the read counters,
151  *  and notifies ALSA when periods elapse
152  *
153  *   - Mostly copied from saa7134-oss's saa7134_irq_oss_done.
154  *
155  */
156
157 void saa7134_irq_alsa_done(struct saa7134_dev *dev, unsigned long status)
158 {
159         int next_blk, reg = 0;
160
161         spin_lock(&dev->slock);
162         if (UNSET == dev->oss.dma_blk) {
163                 dprintk("irq: recording stopped\n");
164                 goto done;
165         }
166         if (0 != (status & 0x0f000000))
167                 dprintk("irq: lost %ld\n", (status >> 24) & 0x0f);
168         if (0 == (status & 0x10000000)) {
169                 /* odd */
170                 if (0 == (dev->oss.dma_blk & 0x01))
171                         reg = SAA7134_RS_BA1(6);
172         } else {
173                 /* even */
174                 if (1 == (dev->oss.dma_blk & 0x01))
175                         reg = SAA7134_RS_BA2(6);
176         }
177         if (0 == reg) {
178                 dprintk("irq: field oops [%s]\n",
179                         (status & 0x10000000) ? "even" : "odd");
180                 goto done;
181         }
182
183         if (dev->oss.read_count >= dev->oss.blksize * (dev->oss.blocks-2)) {
184                 dprintk("irq: overrun [full=%d/%d] - Blocks in %d\n",dev->oss.read_count,
185                         dev->oss.bufsize, dev->oss.blocks);
186                 saa7134_dma_stop(dev);
187                 goto done;
188         }
189
190         /* next block addr */
191         next_blk = (dev->oss.dma_blk + 2) % dev->oss.blocks;
192         saa_writel(reg,next_blk * dev->oss.blksize);
193         if (alsa_debug > 2)
194                 dprintk("irq: ok, %s, next_blk=%d, addr=%x, blocks=%u, size=%u, read=%u\n",
195                         (status & 0x10000000) ? "even" : "odd ", next_blk,
196                         next_blk * dev->oss.blksize, dev->oss.blocks, dev->oss.blksize, dev->oss.read_count);
197
198
199         /* update status & wake waiting readers */
200         dev->oss.dma_blk = (dev->oss.dma_blk + 1) % dev->oss.blocks;
201         dev->oss.read_count += dev->oss.blksize;
202
203         dev->oss.recording_on = reg;
204
205         if (dev->oss.read_count >= snd_pcm_lib_period_bytes(dev->oss.substream)) {
206                 spin_unlock(&dev->slock);
207                 snd_pcm_period_elapsed(dev->oss.substream);
208                 spin_lock(&dev->slock);
209         }
210  done:
211         spin_unlock(&dev->slock);
212
213 }
214
215 /*
216  * ALSA capture trigger
217  *
218  *   - One of the ALSA capture callbacks.
219  *
220  *   Called whenever a capture is started or stopped. Must be defined,
221  *   but there's nothing we want to do here
222  *
223  */
224
225 static int snd_card_saa7134_capture_trigger(snd_pcm_substream_t * substream,
226                                           int cmd)
227 {
228         return 0;
229 }
230
231 /*
232  * DMA buffer config
233  *
234  *   Sets the values that will later be used as the size of the buffer,
235  *  size of the fragments, and total number of fragments.
236  *   Must be called during the preparation stage, before memory is
237  *  allocated
238  *
239  *   - Copied verbatim from saa7134-oss. Can be dropped
240  *     if we just share dsp_buffer_conf from OSS.
241  */
242
243 static int dsp_buffer_conf(struct saa7134_dev *dev, int blksize, int blocks)
244 {
245         if (blksize < 0x100)
246                 blksize = 0x100;
247         if (blksize > 0x10000)
248                 blksize = 0x10000;
249
250         if (blocks < 2)
251                 blocks = 2;
252         if ((blksize * blocks) > 1024*1024)
253                 blocks = 1024*1024 / blksize;
254
255         dev->oss.blocks  = blocks;
256         dev->oss.blksize = blksize;
257         dev->oss.bufsize = blksize * blocks;
258
259         dprintk("buffer config: %d blocks / %d bytes, %d kB total\n",
260                 blocks,blksize,blksize * blocks / 1024);
261         return 0;
262 }
263
264 /*
265  * DMA buffer initialization
266  *
267  *   Uses V4L functions to initialize the DMA. Shouldn't be necessary in
268  *  ALSA, but I was unable to use ALSA's own DMA, and had to force the
269  *  usage of V4L's
270  *
271  *   - Copied verbatim from saa7134-oss. Can be dropped
272  *     if we just share dsp_buffer_init from OSS.
273  */
274
275 static int dsp_buffer_init(struct saa7134_dev *dev)
276 {
277         int err;
278
279         if (!dev->oss.bufsize)
280                 BUG();
281         videobuf_dma_init(&dev->oss.dma);
282         err = videobuf_dma_init_kernel(&dev->oss.dma, PCI_DMA_FROMDEVICE,
283                                        (dev->oss.bufsize + PAGE_SIZE) >> PAGE_SHIFT);
284         if (0 != err)
285                 return err;
286         return 0;
287 }
288
289 /*
290  * ALSA PCM preparation
291  *
292  *   - One of the ALSA capture callbacks.
293  *
294  *   Called right after the capture device is opened, this function configures
295  *  the buffer using the previously defined functions, allocates the memory,
296  *  sets up the hardware registers, and then starts the DMA. When this function
297  *  returns, the audio should be flowing.
298  *
299  */
300
301 static int snd_card_saa7134_capture_prepare(snd_pcm_substream_t * substream)
302 {
303         snd_pcm_runtime_t *runtime = substream->runtime;
304         int err, bswap, sign;
305         u32 fmt, control;
306         unsigned long flags;
307         snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
308         struct saa7134_dev *dev;
309         snd_card_saa7134_pcm_t *saapcm = runtime->private_data;
310         unsigned int bps;
311         unsigned long size;
312         unsigned count;
313
314         size = snd_pcm_lib_buffer_bytes(substream);
315         count = snd_pcm_lib_period_bytes(substream);
316
317         saapcm->saadev->oss.substream = substream;
318         bps = runtime->rate * runtime->channels;
319         bps *= snd_pcm_format_width(runtime->format);
320         bps /= 8;
321         if (bps <= 0)
322                 return -EINVAL;
323         saapcm->pcm_bps = bps;
324         saapcm->pcm_size = snd_pcm_lib_buffer_bytes(substream);
325         saapcm->pcm_count = snd_pcm_lib_period_bytes(substream);
326
327
328         dev=saa7134->saadev;
329
330         dsp_buffer_conf(dev,saapcm->pcm_count,(saapcm->pcm_size/saapcm->pcm_count));
331
332         err = dsp_buffer_init(dev);
333         if (0 != err)
334                 goto fail2;
335
336         /* prepare buffer */
337         if (0 != (err = videobuf_dma_pci_map(dev->pci,&dev->oss.dma)))
338                 return err;
339         if (0 != (err = saa7134_pgtable_alloc(dev->pci,&dev->oss.pt)))
340                 goto fail1;
341         if (0 != (err = saa7134_pgtable_build(dev->pci,&dev->oss.pt,
342                                               dev->oss.dma.sglist,
343                                               dev->oss.dma.sglen,
344                                               0)))
345                 goto fail2;
346
347
348
349         switch (runtime->format) {
350           case SNDRV_PCM_FORMAT_U8:
351           case SNDRV_PCM_FORMAT_S8:
352                 fmt = 0x00;
353                 break;
354           case SNDRV_PCM_FORMAT_U16_LE:
355           case SNDRV_PCM_FORMAT_U16_BE:
356           case SNDRV_PCM_FORMAT_S16_LE:
357           case SNDRV_PCM_FORMAT_S16_BE:
358                 fmt = 0x01;
359                 break;
360           default:
361                 err = -EINVAL;
362                 return 1;
363         }
364
365         switch (runtime->format) {
366           case SNDRV_PCM_FORMAT_S8:
367           case SNDRV_PCM_FORMAT_S16_LE:
368           case SNDRV_PCM_FORMAT_S16_BE:
369                 sign = 1;
370                 break;
371           default:
372                 sign = 0;
373                 break;
374         }
375
376         switch (runtime->format) {
377           case SNDRV_PCM_FORMAT_U16_BE:
378           case SNDRV_PCM_FORMAT_S16_BE:
379                 bswap = 1; break;
380           default:
381                 bswap = 0; break;
382         }
383
384         switch (dev->pci->device) {
385           case PCI_DEVICE_ID_PHILIPS_SAA7134:
386                 if (1 == runtime->channels)
387                         fmt |= (1 << 3);
388                 if (2 == runtime->channels)
389                         fmt |= (3 << 3);
390                 if (sign)
391                         fmt |= 0x04;
392
393                 fmt |= (MIXER_ADDR_TVTUNER == dev->oss.input) ? 0xc0 : 0x80;
394                 saa_writeb(SAA7134_NUM_SAMPLES0, ((dev->oss.blksize - 1) & 0x0000ff));
395                 saa_writeb(SAA7134_NUM_SAMPLES1, ((dev->oss.blksize - 1) & 0x00ff00) >>  8);
396                 saa_writeb(SAA7134_NUM_SAMPLES2, ((dev->oss.blksize - 1) & 0xff0000) >> 16);
397                 saa_writeb(SAA7134_AUDIO_FORMAT_CTRL, fmt);
398
399                 break;
400           case PCI_DEVICE_ID_PHILIPS_SAA7133:
401           case PCI_DEVICE_ID_PHILIPS_SAA7135:
402                 if (1 == runtime->channels)
403                         fmt |= (1 << 4);
404                 if (2 == runtime->channels)
405                         fmt |= (2 << 4);
406                 if (!sign)
407                         fmt |= 0x04;
408                 saa_writel(SAA7133_NUM_SAMPLES, dev->oss.blksize -1);
409                 saa_writel(SAA7133_AUDIO_CHANNEL, 0x543210 | (fmt << 24));
410                 //saa_writel(SAA7133_AUDIO_CHANNEL, 0x543210);
411                 break;
412         }
413
414         dprintk("rec_start: afmt=%d ch=%d  =>  fmt=0x%x swap=%c\n",
415                 runtime->format, runtime->channels, fmt,
416                 bswap ? 'b' : '-');
417         /* dma: setup channel 6 (= AUDIO) */
418         control = SAA7134_RS_CONTROL_BURST_16 |
419                 SAA7134_RS_CONTROL_ME |
420                 (dev->oss.pt.dma >> 12);
421         if (bswap)
422                 control |= SAA7134_RS_CONTROL_BSWAP;
423
424         /* I should be able to use runtime->dma_addr in the control
425            byte, but it doesn't work. So I allocate the DMA using the
426            V4L functions, and force ALSA to use that as the DMA area */
427
428         runtime->dma_area = dev->oss.dma.vmalloc;
429
430         saa_writel(SAA7134_RS_BA1(6),0);
431         saa_writel(SAA7134_RS_BA2(6),dev->oss.blksize);
432         saa_writel(SAA7134_RS_PITCH(6),0);
433         saa_writel(SAA7134_RS_CONTROL(6),control);
434
435         dev->oss.rate = runtime->rate;
436         /* start dma */
437         spin_lock_irqsave(&dev->slock,flags);
438         saa7134_dma_start(dev);
439         spin_unlock_irqrestore(&dev->slock,flags);
440
441         return 0;
442  fail2:
443         saa7134_pgtable_free(dev->pci,&dev->oss.pt);
444  fail1:
445         videobuf_dma_pci_unmap(dev->pci,&dev->oss.dma);
446         return err;
447
448
449 }
450
451 /*
452  * ALSA pointer fetching
453  *
454  *   - One of the ALSA capture callbacks.
455  *
456  *   Called whenever a period elapses, it must return the current hardware
457  *  position of the buffer.
458  *   Also resets the read counter used to prevent overruns
459  *
460  */
461
462 static snd_pcm_uframes_t snd_card_saa7134_capture_pointer(snd_pcm_substream_t * substream)
463 {
464         snd_pcm_runtime_t *runtime = substream->runtime;
465         snd_card_saa7134_pcm_t *saapcm = runtime->private_data;
466         struct saa7134_dev *dev=saapcm->saadev;
467
468
469
470         if (dev->oss.read_count) {
471                 dev->oss.read_count  -= snd_pcm_lib_period_bytes(substream);
472                 dev->oss.read_offset += snd_pcm_lib_period_bytes(substream);
473                 if (dev->oss.read_offset == dev->oss.bufsize)
474                         dev->oss.read_offset = 0;
475         }
476
477         return bytes_to_frames(runtime, dev->oss.read_offset);
478 }
479
480 /*
481  * ALSA hardware capabilities definition
482  */
483
484 static snd_pcm_hardware_t snd_card_saa7134_capture =
485 {
486         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
487                                  SNDRV_PCM_INFO_BLOCK_TRANSFER |
488                                  SNDRV_PCM_INFO_MMAP_VALID),
489         .formats =              USE_FORMATS,
490         .rates =                USE_RATE,
491         .rate_min =             USE_RATE_MIN,
492         .rate_max =             USE_RATE_MAX,
493         .channels_min =         USE_CHANNELS_MIN,
494         .channels_max =         USE_CHANNELS_MAX,
495         .buffer_bytes_max =     MAX_BUFFER_SIZE,
496         .period_bytes_min =     64,
497         .period_bytes_max =     MAX_BUFFER_SIZE,
498         .periods_min =          USE_PERIODS_MIN,
499         .periods_max =          USE_PERIODS_MAX,
500         .fifo_size =            0x08070503,
501 };
502
503 static void snd_card_saa7134_runtime_free(snd_pcm_runtime_t *runtime)
504 {
505         snd_card_saa7134_pcm_t *saapcm = runtime->private_data;
506
507         kfree(saapcm);
508 }
509
510
511 /*
512  * ALSA hardware params
513  *
514  *   - One of the ALSA capture callbacks.
515  *
516  *   Called on initialization, right before the PCM preparation
517  *   Usually used in ALSA to allocate the DMA, but since we don't use the
518  *  ALSA DMA I'm almost sure this isn't necessary.
519  *
520  */
521
522 static int snd_card_saa7134_hw_params(snd_pcm_substream_t * substream,
523                                     snd_pcm_hw_params_t * hw_params)
524 {
525
526         return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
527
528
529 }
530
531 /*
532  * ALSA hardware release
533  *
534  *   - One of the ALSA capture callbacks.
535  *
536  *   Called after closing the device, but before snd_card_saa7134_capture_close
537  *   Usually used in ALSA to free the DMA, but since we don't use the
538  *  ALSA DMA I'm almost sure this isn't necessary.
539  *
540  */
541
542 static int snd_card_saa7134_hw_free(snd_pcm_substream_t * substream)
543 {
544         return snd_pcm_lib_free_pages(substream);
545 }
546
547 /*
548  * DMA buffer release
549  *
550  *   Called after closing the device, during snd_card_saa7134_capture_close
551  *
552  */
553
554 static int dsp_buffer_free(struct saa7134_dev *dev)
555 {
556         if (!dev->oss.blksize)
557                 BUG();
558
559         videobuf_dma_free(&dev->oss.dma);
560
561         dev->oss.blocks  = 0;
562         dev->oss.blksize = 0;
563         dev->oss.bufsize = 0;
564
565         return 0;
566 }
567
568 /*
569  * ALSA capture finish
570  *
571  *   - One of the ALSA capture callbacks.
572  *
573  *   Called after closing the device. It stops the DMA audio and releases
574  *  the buffers
575  *
576  */
577
578 static int snd_card_saa7134_capture_close(snd_pcm_substream_t * substream)
579 {
580         snd_card_saa7134_t *chip = snd_pcm_substream_chip(substream);
581         struct saa7134_dev *dev = chip->saadev;
582         unsigned long flags;
583
584         /* stop dma */
585         spin_lock_irqsave(&dev->slock,flags);
586         saa7134_dma_stop(dev);
587         spin_unlock_irqrestore(&dev->slock,flags);
588
589         /* unlock buffer */
590         saa7134_pgtable_free(dev->pci,&dev->oss.pt);
591         videobuf_dma_pci_unmap(dev->pci,&dev->oss.dma);
592
593         dsp_buffer_free(dev);
594         return 0;
595 }
596
597 /*
598  * ALSA capture start
599  *
600  *   - One of the ALSA capture callbacks.
601  *
602  *   Called when opening the device. It creates and populates the PCM
603  *  structure
604  *
605  */
606
607 static int snd_card_saa7134_capture_open(snd_pcm_substream_t * substream)
608 {
609         snd_pcm_runtime_t *runtime = substream->runtime;
610         snd_card_saa7134_pcm_t *saapcm;
611         snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
612         struct saa7134_dev *dev = saa7134->saadev;
613         int err;
614
615         down(&dev->oss.lock);
616
617         dev->oss.afmt        = SNDRV_PCM_FORMAT_U8;
618         dev->oss.channels    = 2;
619         dev->oss.read_count  = 0;
620         dev->oss.read_offset = 0;
621
622         up(&dev->oss.lock);
623
624         saapcm = kcalloc(1, sizeof(*saapcm), GFP_KERNEL);
625         if (saapcm == NULL)
626                 return -ENOMEM;
627         saapcm->saadev=saa7134->saadev;
628
629         spin_lock_init(&saapcm->lock);
630
631         saapcm->substream = substream;
632         runtime->private_data = saapcm;
633         runtime->private_free = snd_card_saa7134_runtime_free;
634         runtime->hw = snd_card_saa7134_capture;
635
636         if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
637                 return err;
638
639         return 0;
640 }
641
642 /*
643  * ALSA capture callbacks definition
644  */
645
646 static snd_pcm_ops_t snd_card_saa7134_capture_ops = {
647         .open =                 snd_card_saa7134_capture_open,
648         .close =                snd_card_saa7134_capture_close,
649         .ioctl =                snd_pcm_lib_ioctl,
650         .hw_params =            snd_card_saa7134_hw_params,
651         .hw_free =              snd_card_saa7134_hw_free,
652         .prepare =              snd_card_saa7134_capture_prepare,
653         .trigger =              snd_card_saa7134_capture_trigger,
654         .pointer =              snd_card_saa7134_capture_pointer,
655 };
656
657 /*
658  * ALSA PCM setup
659  *
660  *   Called when initializing the board. Sets up the name and hooks up
661  *  the callbacks
662  *
663  */
664
665 static int snd_card_saa7134_pcm(snd_card_saa7134_t *saa7134, int device)
666 {
667         snd_pcm_t *pcm;
668         int err;
669
670         if ((err = snd_pcm_new(saa7134->card, "SAA7134 PCM", device, 0, 1, &pcm)) < 0)
671                 return err;
672         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_card_saa7134_capture_ops);
673         pcm->private_data = saa7134;
674         pcm->info_flags = 0;
675         strcpy(pcm->name, "SAA7134 PCM");
676         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
677                                               snd_dma_pci_data(saa7134->pci),
678                                               128*1024, 256*1024);
679         return 0;
680 }
681
682 #define SAA713x_VOLUME(xname, xindex, addr) \
683 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
684   .info = snd_saa7134_volume_info, \
685   .get = snd_saa7134_volume_get, .put = snd_saa7134_volume_put, \
686   .private_value = addr }
687
688 static int snd_saa7134_volume_info(snd_kcontrol_t * kcontrol, snd_ctl_elem_info_t * uinfo)
689 {
690         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
691         uinfo->count = 2;
692         uinfo->value.integer.min = 0;
693         uinfo->value.integer.max = 20;
694         return 0;
695 }
696
697 static int snd_saa7134_volume_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
698 {
699         snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
700         unsigned long flags;
701         int addr = kcontrol->private_value;
702
703         spin_lock_irqsave(&chip->mixer_lock, flags);
704         ucontrol->value.integer.value[0] = chip->mixer_volume[addr][0];
705         ucontrol->value.integer.value[1] = chip->mixer_volume[addr][1];
706         spin_unlock_irqrestore(&chip->mixer_lock, flags);
707         return 0;
708 }
709
710 static int snd_saa7134_volume_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
711 {
712         snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
713         unsigned long flags;
714         int change, addr = kcontrol->private_value;
715         int left, right;
716
717         left = ucontrol->value.integer.value[0];
718         if (left < 0)
719                 left = 0;
720         if (left > 20)
721                 left = 20;
722         right = ucontrol->value.integer.value[1];
723         if (right < 0)
724                 right = 0;
725         if (right > 20)
726                 right = 20;
727         spin_lock_irqsave(&chip->mixer_lock, flags);
728         change = chip->mixer_volume[addr][0] != left ||
729                  chip->mixer_volume[addr][1] != right;
730         chip->mixer_volume[addr][0] = left;
731         chip->mixer_volume[addr][1] = right;
732         spin_unlock_irqrestore(&chip->mixer_lock, flags);
733         return change;
734 }
735
736 #define SAA713x_CAPSRC(xname, xindex, addr) \
737 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
738   .info = snd_saa7134_capsrc_info, \
739   .get = snd_saa7134_capsrc_get, .put = snd_saa7134_capsrc_put, \
740   .private_value = addr }
741
742 static int snd_saa7134_capsrc_info(snd_kcontrol_t * kcontrol, snd_ctl_elem_info_t * uinfo)
743 {
744         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
745         uinfo->count = 2;
746         uinfo->value.integer.min = 0;
747         uinfo->value.integer.max = 1;
748         return 0;
749 }
750
751 static int snd_saa7134_capsrc_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
752 {
753         snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
754         unsigned long flags;
755         int addr = kcontrol->private_value;
756
757         spin_lock_irqsave(&chip->mixer_lock, flags);
758         ucontrol->value.integer.value[0] = chip->capture_source[addr][0];
759         ucontrol->value.integer.value[1] = chip->capture_source[addr][1];
760         spin_unlock_irqrestore(&chip->mixer_lock, flags);
761         return 0;
762 }
763
764 static int snd_saa7134_capsrc_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
765 {
766         snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
767         unsigned long flags;
768         int change, addr = kcontrol->private_value;
769         int left, right;
770         u32 anabar, xbarin;
771         int analog_io, rate;
772         struct saa7134_dev *dev;
773
774         dev = chip->saadev;
775
776         left = ucontrol->value.integer.value[0] & 1;
777         right = ucontrol->value.integer.value[1] & 1;
778         spin_lock_irqsave(&chip->mixer_lock, flags);
779
780         change = chip->capture_source[addr][0] != left ||
781                  chip->capture_source[addr][1] != right;
782         chip->capture_source[addr][0] = left;
783         chip->capture_source[addr][1] = right;
784         dev->oss.input=addr;
785         spin_unlock_irqrestore(&chip->mixer_lock, flags);
786
787
788         if (change) {
789           switch (dev->pci->device) {
790
791            case PCI_DEVICE_ID_PHILIPS_SAA7134:
792                 switch (addr) {
793                         case MIXER_ADDR_TVTUNER:
794                                 saa_andorb(SAA7134_AUDIO_FORMAT_CTRL, 0xc0, 0xc0);
795                                 saa_andorb(SAA7134_SIF_SAMPLE_FREQ,   0x03, 0x00);
796                                 break;
797                         case MIXER_ADDR_LINE1:
798                         case MIXER_ADDR_LINE2:
799                                 analog_io = (MIXER_ADDR_LINE1 == addr) ? 0x00 : 0x08;
800                                 rate = (32000 == dev->oss.rate) ? 0x01 : 0x03;
801                                 saa_andorb(SAA7134_ANALOG_IO_SELECT,  0x08, analog_io);
802                                 saa_andorb(SAA7134_AUDIO_FORMAT_CTRL, 0xc0, 0x80);
803                                 saa_andorb(SAA7134_SIF_SAMPLE_FREQ,   0x03, rate);
804                                 break;
805                 }
806
807            case PCI_DEVICE_ID_PHILIPS_SAA7133:
808            case PCI_DEVICE_ID_PHILIPS_SAA7135:
809                 xbarin = 0x03; // adc
810                 anabar = 0;
811                 switch (addr) {
812                         case MIXER_ADDR_TVTUNER:
813                                 xbarin = 0; // Demodulator
814                                 anabar = 2; // DACs
815                                 break;
816                         case MIXER_ADDR_LINE1:
817                                 anabar = 0;  // aux1, aux1
818                                 break;
819                         case MIXER_ADDR_LINE2:
820                                 anabar = 9;  // aux2, aux2
821                                 break;
822                 }
823
824                 /* output xbar always main channel */
825                 saa_dsp_writel(dev, SAA7133_DIGITAL_OUTPUT_SEL1, 0xbbbb10);
826
827                 if (left || right) { // We've got data, turn the input on
828                   //saa_dsp_writel(dev, SAA7133_DIGITAL_OUTPUT_SEL2, 0x101010);
829                   saa_dsp_writel(dev, SAA7133_DIGITAL_INPUT_XBAR1, xbarin);
830                   saa_writel(SAA7133_ANALOG_IO_SELECT, anabar);
831                 } else {
832                   //saa_dsp_writel(dev, SAA7133_DIGITAL_OUTPUT_SEL2, 0x101010);
833                   saa_dsp_writel(dev, SAA7133_DIGITAL_INPUT_XBAR1, 0);
834                   saa_writel(SAA7133_ANALOG_IO_SELECT, 0);
835                 }
836           }
837         }
838
839         return change;
840 }
841
842 static snd_kcontrol_new_t snd_saa7134_controls[] = {
843 SAA713x_VOLUME("Video Volume", 0, MIXER_ADDR_TVTUNER),
844 SAA713x_CAPSRC("Video Capture Switch", 0, MIXER_ADDR_TVTUNER),
845 SAA713x_VOLUME("Line Volume", 1, MIXER_ADDR_LINE1),
846 SAA713x_CAPSRC("Line Capture Switch", 1, MIXER_ADDR_LINE1),
847 SAA713x_VOLUME("Line Volume", 2, MIXER_ADDR_LINE2),
848 SAA713x_CAPSRC("Line Capture Switch", 2, MIXER_ADDR_LINE2),
849 };
850
851 /*
852  * ALSA mixer setup
853  *
854  *   Called when initializing the board. Sets up the name and hooks up
855  *  the callbacks
856  *
857  */
858
859 static int snd_card_saa7134_new_mixer(snd_card_saa7134_t * chip)
860 {
861         snd_card_t *card = chip->card;
862         unsigned int idx;
863         int err;
864
865         snd_assert(chip != NULL, return -EINVAL);
866         strcpy(card->mixername, "SAA7134 Mixer");
867
868         for (idx = 0; idx < ARRAY_SIZE(snd_saa7134_controls); idx++) {
869                 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_saa7134_controls[idx], chip))) < 0)
870                         return err;
871         }
872         return 0;
873 }
874
875 static int snd_saa7134_free(snd_card_saa7134_t *chip)
876 {
877         return 0;
878 }
879
880 static int snd_saa7134_dev_free(snd_device_t *device)
881 {
882         snd_card_saa7134_t *chip = device->device_data;
883         return snd_saa7134_free(chip);
884 }
885
886 /*
887  * ALSA initialization
888  *
889  *   Called by saa7134-core, it creates the basic structures and registers
890  *  the ALSA devices
891  *
892  */
893
894 int alsa_card_saa7134_create(struct saa7134_dev *saadev)
895 {
896         static int dev;
897         snd_card_t *card;
898         snd_card_saa7134_t *chip;
899         int err;
900         static snd_device_ops_t ops = {
901                 .dev_free =     snd_saa7134_dev_free,
902         };
903
904         if (dev >= SNDRV_CARDS)
905                 return -ENODEV;
906         if (!enable[dev])
907                 return -ENODEV;
908
909         card = snd_card_new(index[dev], id[dev], THIS_MODULE,
910                             0);
911         if (card == NULL)
912                 return -ENOMEM;
913
914         strcpy(card->driver, "SAA7134");
915
916         /* Card "creation" */
917
918         chip = kcalloc(1, sizeof(*chip), GFP_KERNEL);
919         if (chip == NULL) {
920                 return -ENOMEM;
921         }
922
923         spin_lock_init(&chip->lock);
924
925         chip->saadev = saadev;
926
927         chip->card = card;
928
929         chip->pci = saadev->pci;
930         chip->irq = saadev->pci->irq;
931         chip->iobase = pci_resource_start(saadev->pci, 0);
932
933         if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
934                 snd_saa7134_free(chip);
935                 return err;
936         }
937
938         if ((err = snd_card_saa7134_new_mixer(chip)) < 0)
939                 goto __nodev;
940
941         if ((err = snd_card_saa7134_pcm(chip, 0)) < 0)
942                 goto __nodev;
943
944         spin_lock_init(&chip->mixer_lock);
945
946         snd_card_set_dev(card, &chip->pci->dev);
947
948         /* End of "creation" */
949
950         strcpy(card->shortname, "SAA7134");
951         sprintf(card->longname, "%s at 0x%lx irq %d",
952                 card->shortname, chip->iobase, chip->irq);
953
954         if ((err = snd_card_register(card)) == 0) {
955                 snd_saa7134_cards[dev] = card;
956                 return 0;
957         }
958
959 __nodev:
960         snd_card_free(card);
961         kfree(card);
962         return err;
963 }
964
965 void alsa_card_saa7134_exit(void)
966 {
967         int idx;
968          for (idx = 0; idx < SNDRV_CARDS; idx++) {
969                 snd_card_free(snd_saa7134_cards[idx]);
970         }
971 }