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)
47 /* ------------------------------------------------------------------ */
49 static int dsp_buffer_conf(struct saa7134_dev *dev, int blksize, int blocks)
53 if (blksize > 0x10000)
58 if ((blksize * blocks) > 1024*1024)
59 blocks = 1024*1024 / blksize;
61 dev->oss.blocks = blocks;
62 dev->oss.blksize = blksize;
63 dev->oss.bufsize = blksize * blocks;
65 dprintk("buffer config: %d blocks / %d bytes, %d kB total\n",
66 blocks,blksize,blksize * blocks / 1024);
70 static int dsp_buffer_init(struct saa7134_dev *dev)
74 if (!dev->oss.bufsize)
76 videobuf_dma_init(&dev->oss.dma);
77 err = videobuf_dma_init_kernel(&dev->oss.dma, PCI_DMA_FROMDEVICE,
78 (dev->oss.bufsize + PAGE_SIZE) >> PAGE_SHIFT);
84 static int dsp_buffer_free(struct saa7134_dev *dev)
86 if (!dev->oss.blksize)
88 videobuf_dma_free(&dev->oss.dma);
95 static void dsp_dma_start(struct saa7134_dev *dev)
98 dev->oss.dma_running = 1;
99 saa7134_set_dmabits(dev);
102 static void dsp_dma_stop(struct saa7134_dev *dev)
104 dev->oss.dma_blk = -1;
105 dev->oss.dma_running = 0;
106 saa7134_set_dmabits(dev);
109 static int dsp_rec_start(struct saa7134_dev *dev)
111 int err, bswap, sign;
116 if (0 != (err = videobuf_dma_pci_map(dev->pci,&dev->oss.dma)))
118 if (0 != (err = saa7134_pgtable_alloc(dev->pci,&dev->oss.pt)))
120 if (0 != (err = saa7134_pgtable_build(dev->pci,&dev->oss.pt,
127 switch (dev->oss.afmt) {
129 case AFMT_S8: fmt = 0x00; break;
133 case AFMT_S16_BE: fmt = 0x01; break;
139 switch (dev->oss.afmt) {
142 case AFMT_S16_BE: sign = 1; break;
143 default: sign = 0; break;
146 switch (dev->oss.afmt) {
148 case AFMT_S16_BE: bswap = 1; break;
149 default: bswap = 0; break;
152 switch (dev->pci->device) {
153 case PCI_DEVICE_ID_PHILIPS_SAA7134:
154 if (1 == dev->oss.channels)
156 if (2 == dev->oss.channels)
160 fmt |= (TV == dev->oss.input) ? 0xc0 : 0x80;
162 saa_writeb(SAA7134_NUM_SAMPLES0, ((dev->oss.blksize - 1) & 0x0000ff));
163 saa_writeb(SAA7134_NUM_SAMPLES1, ((dev->oss.blksize - 1) & 0x00ff00) >> 8);
164 saa_writeb(SAA7134_NUM_SAMPLES2, ((dev->oss.blksize - 1) & 0xff0000) >> 16);
165 saa_writeb(SAA7134_AUDIO_FORMAT_CTRL, fmt);
168 case PCI_DEVICE_ID_PHILIPS_SAA7133:
169 case PCI_DEVICE_ID_PHILIPS_SAA7135:
170 if (1 == dev->oss.channels)
172 if (2 == dev->oss.channels)
176 saa_writel(0x588 >> 2, dev->oss.blksize -4);
177 saa_writel(0x58c >> 2, 0x543210 | (fmt << 24));
180 dprintk("rec_start: afmt=%d ch=%d => fmt=0x%x swap=%c\n",
181 dev->oss.afmt, dev->oss.channels, fmt,
184 /* dma: setup channel 6 (= AUDIO) */
185 control = SAA7134_RS_CONTROL_BURST_16 |
186 SAA7134_RS_CONTROL_ME |
187 (dev->oss.pt.dma >> 12);
189 control |= SAA7134_RS_CONTROL_BSWAP;
190 saa_writel(SAA7134_RS_BA1(6),0);
191 saa_writel(SAA7134_RS_BA2(6),dev->oss.blksize);
192 saa_writel(SAA7134_RS_PITCH(6),0);
193 saa_writel(SAA7134_RS_CONTROL(6),control);
196 dev->oss.recording_on = 1;
197 spin_lock_irqsave(&dev->slock,flags);
199 spin_unlock_irqrestore(&dev->slock,flags);
203 saa7134_pgtable_free(dev->pci,&dev->oss.pt);
205 videobuf_dma_pci_unmap(dev->pci,&dev->oss.dma);
209 static int dsp_rec_stop(struct saa7134_dev *dev)
213 dprintk("rec_stop dma_blk=%d\n",dev->oss.dma_blk);
216 dev->oss.recording_on = 0;
217 spin_lock_irqsave(&dev->slock,flags);
219 spin_unlock_irqrestore(&dev->slock,flags);
222 saa7134_pgtable_free(dev->pci,&dev->oss.pt);
223 videobuf_dma_pci_unmap(dev->pci,&dev->oss.dma);
227 /* ------------------------------------------------------------------ */
229 static int dsp_open(struct inode *inode, struct file *file)
231 int minor = iminor(inode);
232 struct saa7134_dev *h,*dev = NULL;
233 struct list_head *list;
236 list_for_each(list,&saa7134_devlist) {
237 h = list_entry(list, struct saa7134_dev, devlist);
238 if (h->oss.minor_dsp == minor)
244 down(&dev->oss.lock);
246 if (dev->oss.users_dsp)
248 dev->oss.users_dsp++;
249 file->private_data = dev;
251 dev->oss.afmt = AFMT_U8;
252 dev->oss.channels = 1;
253 dev->oss.read_count = 0;
254 dev->oss.read_offset = 0;
255 dsp_buffer_conf(dev,PAGE_SIZE,64);
256 err = dsp_buffer_init(dev);
264 dev->oss.users_dsp--;
270 static int dsp_release(struct inode *inode, struct file *file)
272 struct saa7134_dev *dev = file->private_data;
274 down(&dev->oss.lock);
275 if (dev->oss.recording_on)
277 dsp_buffer_free(dev);
278 dev->oss.users_dsp--;
279 file->private_data = NULL;
284 static ssize_t dsp_read(struct file *file, char __user *buffer,
285 size_t count, loff_t *ppos)
287 struct saa7134_dev *dev = file->private_data;
288 DECLARE_WAITQUEUE(wait, current);
293 add_wait_queue(&dev->oss.wq, &wait);
294 down(&dev->oss.lock);
296 /* wait for data if needed */
297 if (0 == dev->oss.read_count) {
298 if (!dev->oss.recording_on) {
299 err = dsp_rec_start(dev);
306 if (dev->oss.recording_on &&
307 !dev->oss.dma_running) {
308 /* recover from overruns */
309 spin_lock_irqsave(&dev->slock,flags);
311 spin_unlock_irqrestore(&dev->slock,flags);
313 if (file->f_flags & O_NONBLOCK) {
319 set_current_state(TASK_INTERRUPTIBLE);
320 if (0 == dev->oss.read_count)
322 set_current_state(TASK_RUNNING);
323 down(&dev->oss.lock);
324 if (signal_pending(current)) {
331 /* copy data to userspace */
333 if (bytes > dev->oss.read_count)
334 bytes = dev->oss.read_count;
335 if (bytes > dev->oss.bufsize - dev->oss.read_offset)
336 bytes = dev->oss.bufsize - dev->oss.read_offset;
337 if (copy_to_user(buffer + ret,
338 dev->oss.dma.vmalloc + dev->oss.read_offset,
347 dev->oss.read_count -= bytes;
348 dev->oss.read_offset += bytes;
349 if (dev->oss.read_offset == dev->oss.bufsize)
350 dev->oss.read_offset = 0;
353 remove_wait_queue(&dev->oss.wq, &wait);
357 static ssize_t dsp_write(struct file *file, const char __user *buffer,
358 size_t count, loff_t *ppos)
363 static int dsp_ioctl(struct inode *inode, struct file *file,
364 unsigned int cmd, unsigned long arg)
366 struct saa7134_dev *dev = file->private_data;
367 void __user *argp = (void __user *) arg;
368 int __user *p = argp;
372 saa7134_print_ioctl(dev->name,cmd);
375 return put_user(SOUND_VERSION, p);
376 case SNDCTL_DSP_GETCAPS:
379 case SNDCTL_DSP_SPEED:
380 if (get_user(val, p))
383 case SOUND_PCM_READ_RATE:
384 return put_user(dev->oss.rate, p);
386 case SNDCTL_DSP_STEREO:
387 if (get_user(val, p))
389 down(&dev->oss.lock);
390 dev->oss.channels = val ? 2 : 1;
391 if (dev->oss.recording_on) {
396 return put_user(dev->oss.channels-1, p);
398 case SNDCTL_DSP_CHANNELS:
399 if (get_user(val, p))
401 if (val != 1 && val != 2)
403 down(&dev->oss.lock);
404 dev->oss.channels = val;
405 if (dev->oss.recording_on) {
411 case SOUND_PCM_READ_CHANNELS:
412 return put_user(dev->oss.channels, p);
414 case SNDCTL_DSP_GETFMTS: /* Returns a mask */
415 return put_user(AFMT_U8 | AFMT_S8 |
416 AFMT_U16_LE | AFMT_U16_BE |
417 AFMT_S16_LE | AFMT_S16_BE, p);
419 case SNDCTL_DSP_SETFMT: /* Selects ONE fmt */
420 if (get_user(val, p))
432 down(&dev->oss.lock);
434 if (dev->oss.recording_on) {
439 return put_user(dev->oss.afmt, p);
444 case SOUND_PCM_READ_BITS:
445 switch (dev->oss.afmt) {
448 return put_user(8, p);
453 return put_user(16, p);
458 case SNDCTL_DSP_NONBLOCK:
459 file->f_flags |= O_NONBLOCK;
462 case SNDCTL_DSP_RESET:
463 down(&dev->oss.lock);
464 if (dev->oss.recording_on)
468 case SNDCTL_DSP_GETBLKSIZE:
469 return put_user(dev->oss.blksize, p);
471 case SNDCTL_DSP_SETFRAGMENT:
472 if (get_user(val, p))
474 if (dev->oss.recording_on)
476 dsp_buffer_free(dev);
477 /* used to be arg >> 16 instead of val >> 16; fixed */
478 dsp_buffer_conf(dev,1 << (val & 0xffff), (val >> 16) & 0xffff);
479 dsp_buffer_init(dev);
482 case SNDCTL_DSP_SYNC:
486 case SNDCTL_DSP_GETISPACE:
489 info.fragsize = dev->oss.blksize;
490 info.fragstotal = dev->oss.blocks;
491 info.bytes = dev->oss.read_count;
492 info.fragments = info.bytes / info.fragsize;
493 if (copy_to_user(argp, &info, sizeof(info)))
502 static unsigned int dsp_poll(struct file *file, struct poll_table_struct *wait)
504 struct saa7134_dev *dev = file->private_data;
505 unsigned int mask = 0;
507 poll_wait(file, &dev->oss.wq, wait);
509 if (0 == dev->oss.read_count) {
510 down(&dev->oss.lock);
511 if (!dev->oss.recording_on)
515 mask |= (POLLIN | POLLRDNORM);
519 struct file_operations saa7134_dsp_fops = {
520 .owner = THIS_MODULE,
522 .release = dsp_release,
530 /* ------------------------------------------------------------------ */
533 mixer_recsrc_7134(struct saa7134_dev *dev)
537 switch (dev->oss.input) {
539 saa_andorb(SAA7134_AUDIO_FORMAT_CTRL, 0xc0, 0xc0);
540 saa_andorb(SAA7134_SIF_SAMPLE_FREQ, 0x03, 0x00);
545 analog_io = (LINE1 == dev->oss.input) ? 0x00 : 0x08;
546 rate = (32000 == dev->oss.rate) ? 0x01 : 0x03;
547 saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x08, analog_io);
548 saa_andorb(SAA7134_AUDIO_FORMAT_CTRL, 0xc0, 0x80);
549 saa_andorb(SAA7134_SIF_SAMPLE_FREQ, 0x03, rate);
556 mixer_recsrc_7133(struct saa7134_dev *dev)
560 xbarin = 0x03; // adc
562 switch (dev->oss.input) {
564 xbarin = 0; // Demodulator
568 anabar = 0; // aux1, aux1
572 anabar = 9; // aux2, aux2
575 /* output xbar always main channel */
576 saa_dsp_writel(dev, 0x46c >> 2, 0xbbbb10);
577 saa_dsp_writel(dev, 0x464 >> 2, xbarin);
578 saa_writel(0x594 >> 2, anabar);
584 mixer_recsrc(struct saa7134_dev *dev, enum saa7134_audio_in src)
586 static const char *iname[] = { "Oops", "TV", "LINE1", "LINE2" };
589 dev->oss.input = src;
590 dprintk("mixer input = %s\n",iname[dev->oss.input]);
592 switch (dev->pci->device) {
593 case PCI_DEVICE_ID_PHILIPS_SAA7134:
594 mixer_recsrc_7134(dev);
596 case PCI_DEVICE_ID_PHILIPS_SAA7133:
597 case PCI_DEVICE_ID_PHILIPS_SAA7135:
598 mixer_recsrc_7133(dev);
605 mixer_level(struct saa7134_dev *dev, enum saa7134_audio_in src, int level)
607 switch (dev->pci->device) {
608 case PCI_DEVICE_ID_PHILIPS_SAA7134:
614 saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x10,
615 (100 == level) ? 0x00 : 0x10);
619 saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x20,
620 (100 == level) ? 0x00 : 0x20);
624 case PCI_DEVICE_ID_PHILIPS_SAA7133:
625 case PCI_DEVICE_ID_PHILIPS_SAA7135:
632 /* ------------------------------------------------------------------ */
634 static int mixer_open(struct inode *inode, struct file *file)
636 int minor = iminor(inode);
637 struct saa7134_dev *h,*dev = NULL;
638 struct list_head *list;
640 list_for_each(list,&saa7134_devlist) {
641 h = list_entry(list, struct saa7134_dev, devlist);
642 if (h->oss.minor_mixer == minor)
648 file->private_data = dev;
652 static int mixer_release(struct inode *inode, struct file *file)
654 file->private_data = NULL;
658 static int mixer_ioctl(struct inode *inode, struct file *file,
659 unsigned int cmd, unsigned long arg)
661 struct saa7134_dev *dev = file->private_data;
662 enum saa7134_audio_in input;
664 void __user *argp = (void __user *) arg;
665 int __user *p = argp;
668 saa7134_print_ioctl(dev->name,cmd);
671 return put_user(SOUND_VERSION, p);
672 case SOUND_MIXER_INFO:
675 memset(&info,0,sizeof(info));
676 strlcpy(info.id, "TV audio", sizeof(info.id));
677 strlcpy(info.name, dev->name, sizeof(info.name));
678 info.modify_counter = dev->oss.count;
679 if (copy_to_user(argp, &info, sizeof(info)))
683 case SOUND_OLD_MIXER_INFO:
685 _old_mixer_info info;
686 memset(&info,0,sizeof(info));
687 strlcpy(info.id, "TV audio", sizeof(info.id));
688 strlcpy(info.name, dev->name, sizeof(info.name));
689 if (copy_to_user(argp, &info, sizeof(info)))
693 case MIXER_READ(SOUND_MIXER_CAPS):
694 return put_user(SOUND_CAP_EXCL_INPUT, p);
695 case MIXER_READ(SOUND_MIXER_STEREODEVS):
696 return put_user(0, p);
697 case MIXER_READ(SOUND_MIXER_RECMASK):
698 case MIXER_READ(SOUND_MIXER_DEVMASK):
699 val = SOUND_MASK_LINE1 | SOUND_MASK_LINE2;
700 if (32000 == dev->oss.rate)
701 val |= SOUND_MASK_VIDEO;
702 return put_user(val, p);
704 case MIXER_WRITE(SOUND_MIXER_RECSRC):
705 if (get_user(val, p))
707 input = dev->oss.input;
708 if (32000 == dev->oss.rate &&
709 val & SOUND_MASK_VIDEO && dev->oss.input != TV)
711 if (val & SOUND_MASK_LINE1 && dev->oss.input != LINE1)
713 if (val & SOUND_MASK_LINE2 && dev->oss.input != LINE2)
715 if (input != dev->oss.input)
716 mixer_recsrc(dev,input);
718 case MIXER_READ(SOUND_MIXER_RECSRC):
719 switch (dev->oss.input) {
720 case TV: ret = SOUND_MASK_VIDEO; break;
721 case LINE1: ret = SOUND_MASK_LINE1; break;
722 case LINE2: ret = SOUND_MASK_LINE2; break;
725 return put_user(ret, p);
727 case MIXER_WRITE(SOUND_MIXER_VIDEO):
728 case MIXER_READ(SOUND_MIXER_VIDEO):
729 if (32000 != dev->oss.rate)
731 return put_user(100 | 100 << 8, p);
733 case MIXER_WRITE(SOUND_MIXER_LINE1):
734 if (get_user(val, p))
737 val = (val <= 50) ? 50 : 100;
738 dev->oss.line1 = val;
739 mixer_level(dev,LINE1,dev->oss.line1);
741 case MIXER_READ(SOUND_MIXER_LINE1):
742 return put_user(dev->oss.line1 | dev->oss.line1 << 8, p);
744 case MIXER_WRITE(SOUND_MIXER_LINE2):
745 if (get_user(val, p))
748 val = (val <= 50) ? 50 : 100;
749 dev->oss.line2 = val;
750 mixer_level(dev,LINE2,dev->oss.line2);
752 case MIXER_READ(SOUND_MIXER_LINE2):
753 return put_user(dev->oss.line2 | dev->oss.line2 << 8, p);
760 struct file_operations saa7134_mixer_fops = {
761 .owner = THIS_MODULE,
763 .release = mixer_release,
764 .ioctl = mixer_ioctl,
768 /* ------------------------------------------------------------------ */
770 int saa7134_oss_init1(struct saa7134_dev *dev)
773 init_MUTEX(&dev->oss.lock);
774 init_waitqueue_head(&dev->oss.wq);
776 switch (dev->pci->device) {
777 case PCI_DEVICE_ID_PHILIPS_SAA7133:
778 case PCI_DEVICE_ID_PHILIPS_SAA7135:
779 saa_writel(0x588 >> 2, 0x00000fff);
780 saa_writel(0x58c >> 2, 0x00543210);
781 saa_dsp_writel(dev, 0x46c >> 2, 0xbbbbbb);
786 dev->oss.rate = 32000;
788 dev->oss.rate = oss_rate;
789 dev->oss.rate = (dev->oss.rate > 40000) ? 48000 : 32000;
794 mixer_level(dev,LINE1,dev->oss.line1);
795 mixer_level(dev,LINE2,dev->oss.line2);
796 mixer_recsrc(dev, (dev->oss.rate == 32000) ? TV : LINE2);
801 int saa7134_oss_fini(struct saa7134_dev *dev)
807 void saa7134_irq_oss_done(struct saa7134_dev *dev, unsigned long status)
809 int next_blk, reg = 0;
811 spin_lock(&dev->slock);
812 if (UNSET == dev->oss.dma_blk) {
813 dprintk("irq: recording stopped\n");
816 if (0 != (status & 0x0f000000))
817 dprintk("irq: lost %ld\n", (status >> 24) & 0x0f);
818 if (0 == (status & 0x10000000)) {
820 if (0 == (dev->oss.dma_blk & 0x01))
821 reg = SAA7134_RS_BA1(6);
824 if (1 == (dev->oss.dma_blk & 0x01))
825 reg = SAA7134_RS_BA2(6);
828 dprintk("irq: field oops [%s]\n",
829 (status & 0x10000000) ? "even" : "odd");
832 if (dev->oss.read_count >= dev->oss.blksize * (dev->oss.blocks-2)) {
833 dprintk("irq: overrun [full=%d/%d]\n",dev->oss.read_count,
839 /* next block addr */
840 next_blk = (dev->oss.dma_blk + 2) % dev->oss.blocks;
841 saa_writel(reg,next_blk * dev->oss.blksize);
843 dprintk("irq: ok, %s, next_blk=%d, addr=%x\n",
844 (status & 0x10000000) ? "even" : "odd ", next_blk,
845 next_blk * dev->oss.blksize);
847 /* update status & wake waiting readers */
848 dev->oss.dma_blk = (dev->oss.dma_blk + 1) % dev->oss.blocks;
849 dev->oss.read_count += dev->oss.blksize;
850 wake_up(&dev->oss.wq);
853 spin_unlock(&dev->slock);
856 /* ----------------------------------------------------------- */