3 * device driver for philips saa7134 based TV cards
6 * (c) 2001,02 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
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.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <linux/init.h>
24 #include <linux/list.h>
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/kernel.h>
28 #include <linux/slab.h>
29 #include <linux/soundcard.h>
31 #include "saa7134-reg.h"
34 /* ------------------------------------------------------------------ */
36 static unsigned int oss_debug = 0;
37 module_param(oss_debug, int, 0644);
38 MODULE_PARM_DESC(oss_debug,"enable debug messages [oss]");
40 static unsigned int oss_rate = 0;
41 module_param(oss_rate, int, 0444);
42 MODULE_PARM_DESC(oss_rate,"sample rate (valid are: 32000,48000)");
44 #define dprintk(fmt, arg...) if (oss_debug) \
45 printk(KERN_DEBUG "%s/oss: " fmt, dev->name , ## arg)
48 /* ------------------------------------------------------------------ */
50 static int dsp_buffer_conf(struct saa7134_dev *dev, int blksize, int blocks)
54 if (blksize > 0x10000)
59 if ((blksize * blocks) > 1024*1024)
60 blocks = 1024*1024 / blksize;
62 dev->dmasound.blocks = blocks;
63 dev->dmasound.blksize = blksize;
64 dev->dmasound.bufsize = blksize * blocks;
66 dprintk("buffer config: %d blocks / %d bytes, %d kB total\n",
67 blocks,blksize,blksize * blocks / 1024);
71 static int dsp_buffer_init(struct saa7134_dev *dev)
75 if (!dev->dmasound.bufsize)
77 videobuf_dma_init(&dev->dmasound.dma);
78 err = videobuf_dma_init_kernel(&dev->dmasound.dma, PCI_DMA_FROMDEVICE,
79 (dev->dmasound.bufsize + PAGE_SIZE) >> PAGE_SHIFT);
85 static int dsp_buffer_free(struct saa7134_dev *dev)
87 if (!dev->dmasound.blksize)
89 videobuf_dma_free(&dev->dmasound.dma);
90 dev->dmasound.blocks = 0;
91 dev->dmasound.blksize = 0;
92 dev->dmasound.bufsize = 0;
96 static void dsp_dma_start(struct saa7134_dev *dev)
98 dev->dmasound.dma_blk = 0;
99 dev->dmasound.dma_running = 1;
100 saa7134_set_dmabits(dev);
103 static void dsp_dma_stop(struct saa7134_dev *dev)
105 dev->dmasound.dma_blk = -1;
106 dev->dmasound.dma_running = 0;
107 saa7134_set_dmabits(dev);
110 static int dsp_rec_start(struct saa7134_dev *dev)
112 int err, bswap, sign;
117 if (0 != (err = videobuf_dma_pci_map(dev->pci,&dev->dmasound.dma)))
119 if (0 != (err = saa7134_pgtable_alloc(dev->pci,&dev->dmasound.pt)))
121 if (0 != (err = saa7134_pgtable_build(dev->pci,&dev->dmasound.pt,
122 dev->dmasound.dma.sglist,
123 dev->dmasound.dma.sglen,
128 switch (dev->dmasound.afmt) {
130 case AFMT_S8: fmt = 0x00; break;
134 case AFMT_S16_BE: fmt = 0x01; break;
140 switch (dev->dmasound.afmt) {
143 case AFMT_S16_BE: sign = 1; break;
144 default: sign = 0; break;
147 switch (dev->dmasound.afmt) {
149 case AFMT_S16_BE: bswap = 1; break;
150 default: bswap = 0; break;
153 switch (dev->pci->device) {
154 case PCI_DEVICE_ID_PHILIPS_SAA7134:
155 if (1 == dev->dmasound.channels)
157 if (2 == dev->dmasound.channels)
161 fmt |= (TV == dev->dmasound.input) ? 0xc0 : 0x80;
163 saa_writeb(SAA7134_NUM_SAMPLES0, ((dev->dmasound.blksize - 1) & 0x0000ff));
164 saa_writeb(SAA7134_NUM_SAMPLES1, ((dev->dmasound.blksize - 1) & 0x00ff00) >> 8);
165 saa_writeb(SAA7134_NUM_SAMPLES2, ((dev->dmasound.blksize - 1) & 0xff0000) >> 16);
166 saa_writeb(SAA7134_AUDIO_FORMAT_CTRL, fmt);
169 case PCI_DEVICE_ID_PHILIPS_SAA7133:
170 case PCI_DEVICE_ID_PHILIPS_SAA7135:
171 if (1 == dev->dmasound.channels)
173 if (2 == dev->dmasound.channels)
177 saa_writel(SAA7133_NUM_SAMPLES, dev->dmasound.blksize -4);
178 saa_writel(SAA7133_AUDIO_CHANNEL, 0x543210 | (fmt << 24));
181 dprintk("rec_start: afmt=%d ch=%d => fmt=0x%x swap=%c\n",
182 dev->dmasound.afmt, dev->dmasound.channels, fmt,
185 /* dma: setup channel 6 (= AUDIO) */
186 control = SAA7134_RS_CONTROL_BURST_16 |
187 SAA7134_RS_CONTROL_ME |
188 (dev->dmasound.pt.dma >> 12);
190 control |= SAA7134_RS_CONTROL_BSWAP;
191 saa_writel(SAA7134_RS_BA1(6),0);
192 saa_writel(SAA7134_RS_BA2(6),dev->dmasound.blksize);
193 saa_writel(SAA7134_RS_PITCH(6),0);
194 saa_writel(SAA7134_RS_CONTROL(6),control);
197 dev->dmasound.recording_on = 1;
198 spin_lock_irqsave(&dev->slock,flags);
200 spin_unlock_irqrestore(&dev->slock,flags);
204 saa7134_pgtable_free(dev->pci,&dev->dmasound.pt);
206 videobuf_dma_pci_unmap(dev->pci,&dev->dmasound.dma);
210 static int dsp_rec_stop(struct saa7134_dev *dev)
214 dprintk("rec_stop dma_blk=%d\n",dev->dmasound.dma_blk);
217 dev->dmasound.recording_on = 0;
218 spin_lock_irqsave(&dev->slock,flags);
220 spin_unlock_irqrestore(&dev->slock,flags);
223 saa7134_pgtable_free(dev->pci,&dev->dmasound.pt);
224 videobuf_dma_pci_unmap(dev->pci,&dev->dmasound.dma);
228 /* ------------------------------------------------------------------ */
230 static int dsp_open(struct inode *inode, struct file *file)
232 int minor = iminor(inode);
233 struct saa7134_dev *h,*dev = NULL;
234 struct list_head *list;
237 list_for_each(list,&saa7134_devlist) {
238 h = list_entry(list, struct saa7134_dev, devlist);
239 if (h->dmasound.minor_dsp == minor)
245 down(&dev->dmasound.lock);
247 if (dev->dmasound.users_dsp)
249 dev->dmasound.users_dsp++;
250 file->private_data = dev;
252 dev->dmasound.afmt = AFMT_U8;
253 dev->dmasound.channels = 1;
254 dev->dmasound.read_count = 0;
255 dev->dmasound.read_offset = 0;
256 dsp_buffer_conf(dev,PAGE_SIZE,64);
257 err = dsp_buffer_init(dev);
261 up(&dev->dmasound.lock);
265 dev->dmasound.users_dsp--;
267 up(&dev->dmasound.lock);
271 static int dsp_release(struct inode *inode, struct file *file)
273 struct saa7134_dev *dev = file->private_data;
275 down(&dev->dmasound.lock);
276 if (dev->dmasound.recording_on)
278 dsp_buffer_free(dev);
279 dev->dmasound.users_dsp--;
280 file->private_data = NULL;
281 up(&dev->dmasound.lock);
285 static ssize_t dsp_read(struct file *file, char __user *buffer,
286 size_t count, loff_t *ppos)
288 struct saa7134_dev *dev = file->private_data;
289 DECLARE_WAITQUEUE(wait, current);
294 add_wait_queue(&dev->dmasound.wq, &wait);
295 down(&dev->dmasound.lock);
297 /* wait for data if needed */
298 if (0 == dev->dmasound.read_count) {
299 if (!dev->dmasound.recording_on) {
300 err = dsp_rec_start(dev);
307 if (dev->dmasound.recording_on &&
308 !dev->dmasound.dma_running) {
309 /* recover from overruns */
310 spin_lock_irqsave(&dev->slock,flags);
312 spin_unlock_irqrestore(&dev->slock,flags);
314 if (file->f_flags & O_NONBLOCK) {
319 up(&dev->dmasound.lock);
320 set_current_state(TASK_INTERRUPTIBLE);
321 if (0 == dev->dmasound.read_count)
323 set_current_state(TASK_RUNNING);
324 down(&dev->dmasound.lock);
325 if (signal_pending(current)) {
332 /* copy data to userspace */
334 if (bytes > dev->dmasound.read_count)
335 bytes = dev->dmasound.read_count;
336 if (bytes > dev->dmasound.bufsize - dev->dmasound.read_offset)
337 bytes = dev->dmasound.bufsize - dev->dmasound.read_offset;
338 if (copy_to_user(buffer + ret,
339 dev->dmasound.dma.vmalloc + dev->dmasound.read_offset,
348 dev->dmasound.read_count -= bytes;
349 dev->dmasound.read_offset += bytes;
350 if (dev->dmasound.read_offset == dev->dmasound.bufsize)
351 dev->dmasound.read_offset = 0;
353 up(&dev->dmasound.lock);
354 remove_wait_queue(&dev->dmasound.wq, &wait);
358 static ssize_t dsp_write(struct file *file, const char __user *buffer,
359 size_t count, loff_t *ppos)
364 static int dsp_ioctl(struct inode *inode, struct file *file,
365 unsigned int cmd, unsigned long arg)
367 struct saa7134_dev *dev = file->private_data;
368 void __user *argp = (void __user *) arg;
369 int __user *p = argp;
373 saa7134_print_ioctl(dev->name,cmd);
376 return put_user(SOUND_VERSION, p);
377 case SNDCTL_DSP_GETCAPS:
380 case SNDCTL_DSP_SPEED:
381 if (get_user(val, p))
384 case SOUND_PCM_READ_RATE:
385 return put_user(dev->dmasound.rate, p);
387 case SNDCTL_DSP_STEREO:
388 if (get_user(val, p))
390 down(&dev->dmasound.lock);
391 dev->dmasound.channels = val ? 2 : 1;
392 if (dev->dmasound.recording_on) {
396 up(&dev->dmasound.lock);
397 return put_user(dev->dmasound.channels-1, p);
399 case SNDCTL_DSP_CHANNELS:
400 if (get_user(val, p))
402 if (val != 1 && val != 2)
404 down(&dev->dmasound.lock);
405 dev->dmasound.channels = val;
406 if (dev->dmasound.recording_on) {
410 up(&dev->dmasound.lock);
412 case SOUND_PCM_READ_CHANNELS:
413 return put_user(dev->dmasound.channels, p);
415 case SNDCTL_DSP_GETFMTS: /* Returns a mask */
416 return put_user(AFMT_U8 | AFMT_S8 |
417 AFMT_U16_LE | AFMT_U16_BE |
418 AFMT_S16_LE | AFMT_S16_BE, p);
420 case SNDCTL_DSP_SETFMT: /* Selects ONE fmt */
421 if (get_user(val, p))
433 down(&dev->dmasound.lock);
434 dev->dmasound.afmt = val;
435 if (dev->dmasound.recording_on) {
439 up(&dev->dmasound.lock);
440 return put_user(dev->dmasound.afmt, p);
445 case SOUND_PCM_READ_BITS:
446 switch (dev->dmasound.afmt) {
449 return put_user(8, p);
454 return put_user(16, p);
459 case SNDCTL_DSP_NONBLOCK:
460 file->f_flags |= O_NONBLOCK;
463 case SNDCTL_DSP_RESET:
464 down(&dev->dmasound.lock);
465 if (dev->dmasound.recording_on)
467 up(&dev->dmasound.lock);
469 case SNDCTL_DSP_GETBLKSIZE:
470 return put_user(dev->dmasound.blksize, p);
472 case SNDCTL_DSP_SETFRAGMENT:
473 if (get_user(val, p))
475 if (dev->dmasound.recording_on)
477 dsp_buffer_free(dev);
478 /* used to be arg >> 16 instead of val >> 16; fixed */
479 dsp_buffer_conf(dev,1 << (val & 0xffff), (val >> 16) & 0xffff);
480 dsp_buffer_init(dev);
483 case SNDCTL_DSP_SYNC:
487 case SNDCTL_DSP_GETISPACE:
490 info.fragsize = dev->dmasound.blksize;
491 info.fragstotal = dev->dmasound.blocks;
492 info.bytes = dev->dmasound.read_count;
493 info.fragments = info.bytes / info.fragsize;
494 if (copy_to_user(argp, &info, sizeof(info)))
503 static unsigned int dsp_poll(struct file *file, struct poll_table_struct *wait)
505 struct saa7134_dev *dev = file->private_data;
506 unsigned int mask = 0;
508 poll_wait(file, &dev->dmasound.wq, wait);
510 if (0 == dev->dmasound.read_count) {
511 down(&dev->dmasound.lock);
512 if (!dev->dmasound.recording_on)
514 up(&dev->dmasound.lock);
516 mask |= (POLLIN | POLLRDNORM);
520 struct file_operations saa7134_dsp_fops = {
521 .owner = THIS_MODULE,
523 .release = dsp_release,
531 /* ------------------------------------------------------------------ */
534 mixer_recsrc_7134(struct saa7134_dev *dev)
538 switch (dev->dmasound.input) {
540 saa_andorb(SAA7134_AUDIO_FORMAT_CTRL, 0xc0, 0xc0);
541 saa_andorb(SAA7134_SIF_SAMPLE_FREQ, 0x03, 0x00);
546 analog_io = (LINE1 == dev->dmasound.input) ? 0x00 : 0x08;
547 rate = (32000 == dev->dmasound.rate) ? 0x01 : 0x03;
548 saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x08, analog_io);
549 saa_andorb(SAA7134_AUDIO_FORMAT_CTRL, 0xc0, 0x80);
550 saa_andorb(SAA7134_SIF_SAMPLE_FREQ, 0x03, rate);
557 mixer_recsrc_7133(struct saa7134_dev *dev)
561 xbarin = 0x03; // adc
563 switch (dev->dmasound.input) {
565 xbarin = 0; // Demodulator
569 anabar = 0; // aux1, aux1
573 anabar = 9; // aux2, aux2
576 /* output xbar always main channel */
577 saa_dsp_writel(dev, 0x46c >> 2, 0xbbbb10);
578 saa_dsp_writel(dev, 0x464 >> 2, xbarin);
579 saa_writel(0x594 >> 2, anabar);
585 mixer_recsrc(struct saa7134_dev *dev, enum saa7134_audio_in src)
587 static const char *iname[] = { "Oops", "TV", "LINE1", "LINE2" };
589 dev->dmasound.count++;
590 dev->dmasound.input = src;
591 dprintk("mixer input = %s\n",iname[dev->dmasound.input]);
593 switch (dev->pci->device) {
594 case PCI_DEVICE_ID_PHILIPS_SAA7134:
595 mixer_recsrc_7134(dev);
597 case PCI_DEVICE_ID_PHILIPS_SAA7133:
598 case PCI_DEVICE_ID_PHILIPS_SAA7135:
599 mixer_recsrc_7133(dev);
606 mixer_level(struct saa7134_dev *dev, enum saa7134_audio_in src, int level)
608 switch (dev->pci->device) {
609 case PCI_DEVICE_ID_PHILIPS_SAA7134:
615 saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x10,
616 (100 == level) ? 0x00 : 0x10);
620 saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x20,
621 (100 == level) ? 0x00 : 0x20);
625 case PCI_DEVICE_ID_PHILIPS_SAA7133:
626 case PCI_DEVICE_ID_PHILIPS_SAA7135:
633 /* ------------------------------------------------------------------ */
635 static int mixer_open(struct inode *inode, struct file *file)
637 int minor = iminor(inode);
638 struct saa7134_dev *h,*dev = NULL;
639 struct list_head *list;
641 list_for_each(list,&saa7134_devlist) {
642 h = list_entry(list, struct saa7134_dev, devlist);
643 if (h->dmasound.minor_mixer == minor)
649 file->private_data = dev;
653 static int mixer_release(struct inode *inode, struct file *file)
655 file->private_data = NULL;
659 static int mixer_ioctl(struct inode *inode, struct file *file,
660 unsigned int cmd, unsigned long arg)
662 struct saa7134_dev *dev = file->private_data;
663 enum saa7134_audio_in input;
665 void __user *argp = (void __user *) arg;
666 int __user *p = argp;
669 saa7134_print_ioctl(dev->name,cmd);
672 return put_user(SOUND_VERSION, p);
673 case SOUND_MIXER_INFO:
676 memset(&info,0,sizeof(info));
677 strlcpy(info.id, "TV audio", sizeof(info.id));
678 strlcpy(info.name, dev->name, sizeof(info.name));
679 info.modify_counter = dev->dmasound.count;
680 if (copy_to_user(argp, &info, sizeof(info)))
684 case SOUND_OLD_MIXER_INFO:
686 _old_mixer_info info;
687 memset(&info,0,sizeof(info));
688 strlcpy(info.id, "TV audio", sizeof(info.id));
689 strlcpy(info.name, dev->name, sizeof(info.name));
690 if (copy_to_user(argp, &info, sizeof(info)))
694 case MIXER_READ(SOUND_MIXER_CAPS):
695 return put_user(SOUND_CAP_EXCL_INPUT, p);
696 case MIXER_READ(SOUND_MIXER_STEREODEVS):
697 return put_user(0, p);
698 case MIXER_READ(SOUND_MIXER_RECMASK):
699 case MIXER_READ(SOUND_MIXER_DEVMASK):
700 val = SOUND_MASK_LINE1 | SOUND_MASK_LINE2;
701 if (32000 == dev->dmasound.rate)
702 val |= SOUND_MASK_VIDEO;
703 return put_user(val, p);
705 case MIXER_WRITE(SOUND_MIXER_RECSRC):
706 if (get_user(val, p))
708 input = dev->dmasound.input;
709 if (32000 == dev->dmasound.rate &&
710 val & SOUND_MASK_VIDEO && dev->dmasound.input != TV)
712 if (val & SOUND_MASK_LINE1 && dev->dmasound.input != LINE1)
714 if (val & SOUND_MASK_LINE2 && dev->dmasound.input != LINE2)
716 if (input != dev->dmasound.input)
717 mixer_recsrc(dev,input);
719 case MIXER_READ(SOUND_MIXER_RECSRC):
720 switch (dev->dmasound.input) {
721 case TV: ret = SOUND_MASK_VIDEO; break;
722 case LINE1: ret = SOUND_MASK_LINE1; break;
723 case LINE2: ret = SOUND_MASK_LINE2; break;
726 return put_user(ret, p);
728 case MIXER_WRITE(SOUND_MIXER_VIDEO):
729 case MIXER_READ(SOUND_MIXER_VIDEO):
730 if (32000 != dev->dmasound.rate)
732 return put_user(100 | 100 << 8, p);
734 case MIXER_WRITE(SOUND_MIXER_LINE1):
735 if (get_user(val, p))
738 val = (val <= 50) ? 50 : 100;
739 dev->dmasound.line1 = val;
740 mixer_level(dev,LINE1,dev->dmasound.line1);
742 case MIXER_READ(SOUND_MIXER_LINE1):
743 return put_user(dev->dmasound.line1 | dev->dmasound.line1 << 8, p);
745 case MIXER_WRITE(SOUND_MIXER_LINE2):
746 if (get_user(val, p))
749 val = (val <= 50) ? 50 : 100;
750 dev->dmasound.line2 = val;
751 mixer_level(dev,LINE2,dev->dmasound.line2);
753 case MIXER_READ(SOUND_MIXER_LINE2):
754 return put_user(dev->dmasound.line2 | dev->dmasound.line2 << 8, p);
761 struct file_operations saa7134_mixer_fops = {
762 .owner = THIS_MODULE,
764 .release = mixer_release,
765 .ioctl = mixer_ioctl,
769 /* ------------------------------------------------------------------ */
771 int saa7134_oss_init1(struct saa7134_dev *dev)
774 init_MUTEX(&dev->dmasound.lock);
775 init_waitqueue_head(&dev->dmasound.wq);
777 switch (dev->pci->device) {
778 case PCI_DEVICE_ID_PHILIPS_SAA7133:
779 case PCI_DEVICE_ID_PHILIPS_SAA7135:
780 saa_writel(0x588 >> 2, 0x00000fff);
781 saa_writel(0x58c >> 2, 0x00543210);
782 saa_dsp_writel(dev, 0x46c >> 2, 0xbbbbbb);
787 dev->dmasound.rate = 32000;
789 dev->dmasound.rate = oss_rate;
790 dev->dmasound.rate = (dev->dmasound.rate > 40000) ? 48000 : 32000;
793 dev->dmasound.line1 = 50;
794 dev->dmasound.line2 = 50;
795 mixer_level(dev,LINE1,dev->dmasound.line1);
796 mixer_level(dev,LINE2,dev->dmasound.line2);
797 mixer_recsrc(dev, (dev->dmasound.rate == 32000) ? TV : LINE2);
802 int saa7134_oss_fini(struct saa7134_dev *dev)
808 void saa7134_irq_oss_done(struct saa7134_dev *dev, unsigned long status)
810 int next_blk, reg = 0;
812 spin_lock(&dev->slock);
813 if (UNSET == dev->dmasound.dma_blk) {
814 dprintk("irq: recording stopped\n");
817 if (0 != (status & 0x0f000000))
818 dprintk("irq: lost %ld\n", (status >> 24) & 0x0f);
819 if (0 == (status & 0x10000000)) {
821 if (0 == (dev->dmasound.dma_blk & 0x01))
822 reg = SAA7134_RS_BA1(6);
825 if (1 == (dev->dmasound.dma_blk & 0x01))
826 reg = SAA7134_RS_BA2(6);
829 dprintk("irq: field oops [%s]\n",
830 (status & 0x10000000) ? "even" : "odd");
833 if (dev->dmasound.read_count >= dev->dmasound.blksize * (dev->dmasound.blocks-2)) {
834 dprintk("irq: overrun [full=%d/%d]\n",dev->dmasound.read_count,
835 dev->dmasound.bufsize);
840 /* next block addr */
841 next_blk = (dev->dmasound.dma_blk + 2) % dev->dmasound.blocks;
842 saa_writel(reg,next_blk * dev->dmasound.blksize);
844 dprintk("irq: ok, %s, next_blk=%d, addr=%x\n",
845 (status & 0x10000000) ? "even" : "odd ", next_blk,
846 next_blk * dev->dmasound.blksize);
848 /* update status & wake waiting readers */
849 dev->dmasound.dma_blk = (dev->dmasound.dma_blk + 1) % dev->dmasound.blocks;
850 dev->dmasound.read_count += dev->dmasound.blksize;
851 wake_up(&dev->dmasound.wq);
854 spin_unlock(&dev->slock);
857 /* ----------------------------------------------------------- */