2 * $Id: saa7134-oss.c,v 1.13 2004/12/10 12:33:39 kraxel Exp $
4 * device driver for philips saa7134 based TV cards
7 * (c) 2001,02 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
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; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <linux/init.h>
25 #include <linux/list.h>
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/kernel.h>
29 #include <linux/slab.h>
30 #include <linux/soundcard.h>
32 #include "saa7134-reg.h"
35 /* ------------------------------------------------------------------ */
37 static unsigned int oss_debug
= 0;
38 module_param(oss_debug
, int, 0644);
39 MODULE_PARM_DESC(oss_debug
,"enable debug messages [oss]");
41 static unsigned int oss_rate
= 0;
42 module_param(oss_rate
, int, 0444);
43 MODULE_PARM_DESC(oss_rate
,"sample rate (valid are: 32000,48000)");
45 #define dprintk(fmt, arg...) if (oss_debug) \
46 printk(KERN_DEBUG "%s/oss: " fmt, dev->name , ## arg)
48 /* ------------------------------------------------------------------ */
50 static int dsp_buffer_conf(struct saa7134_dev
*dev
, int blksize
, int blocks
)
55 if (blksize
> 0x10000)
60 while ((blksize
* blocks
) & ~PAGE_MASK
)
62 if ((blksize
* blocks
) > 1024*1024)
63 blocks
= 1024*1024 / blksize
;
65 dev
->oss
.blocks
= blocks
;
66 dev
->oss
.blksize
= blksize
;
67 dev
->oss
.bufsize
= blksize
* blocks
;
69 dprintk("buffer config: %d blocks / %d bytes, %d kB total\n",
70 blocks
,blksize
,blksize
* blocks
/ 1024);
74 static int dsp_buffer_init(struct saa7134_dev
*dev
)
78 if (!dev
->oss
.bufsize
)
80 videobuf_dma_init(&dev
->oss
.dma
);
81 err
= videobuf_dma_init_kernel(&dev
->oss
.dma
, PCI_DMA_FROMDEVICE
,
82 dev
->oss
.bufsize
>> PAGE_SHIFT
);
88 static int dsp_buffer_free(struct saa7134_dev
*dev
)
90 if (!dev
->oss
.blksize
)
92 videobuf_dma_free(&dev
->oss
.dma
);
99 static void dsp_dma_start(struct saa7134_dev
*dev
)
101 dev
->oss
.dma_blk
= 0;
102 dev
->oss
.dma_running
= 1;
103 saa7134_set_dmabits(dev
);
106 static void dsp_dma_stop(struct saa7134_dev
*dev
)
108 dev
->oss
.dma_blk
= -1;
109 dev
->oss
.dma_running
= 0;
110 saa7134_set_dmabits(dev
);
113 static int dsp_rec_start(struct saa7134_dev
*dev
)
115 int err
, bswap
, sign
;
120 if (0 != (err
= videobuf_dma_pci_map(dev
->pci
,&dev
->oss
.dma
)))
122 if (0 != (err
= saa7134_pgtable_alloc(dev
->pci
,&dev
->oss
.pt
)))
124 if (0 != (err
= saa7134_pgtable_build(dev
->pci
,&dev
->oss
.pt
,
131 switch (dev
->oss
.afmt
) {
133 case AFMT_S8
: fmt
= 0x00; break;
137 case AFMT_S16_BE
: fmt
= 0x01; break;
143 switch (dev
->oss
.afmt
) {
146 case AFMT_S16_BE
: sign
= 1; break;
147 default: sign
= 0; break;
150 switch (dev
->oss
.afmt
) {
152 case AFMT_S16_BE
: bswap
= 1; break;
153 default: bswap
= 0; break;
156 switch (dev
->pci
->device
) {
157 case PCI_DEVICE_ID_PHILIPS_SAA7134
:
158 if (1 == dev
->oss
.channels
)
160 if (2 == dev
->oss
.channels
)
164 fmt
|= (TV
== dev
->oss
.input
) ? 0xc0 : 0x80;
166 saa_writeb(SAA7134_NUM_SAMPLES0
, (dev
->oss
.blksize
& 0x0000ff));
167 saa_writeb(SAA7134_NUM_SAMPLES1
, (dev
->oss
.blksize
& 0x00ff00) >> 8);
168 saa_writeb(SAA7134_NUM_SAMPLES2
, (dev
->oss
.blksize
& 0xff0000) >> 16);
169 saa_writeb(SAA7134_AUDIO_FORMAT_CTRL
, fmt
);
171 case PCI_DEVICE_ID_PHILIPS_SAA7133
:
172 case PCI_DEVICE_ID_PHILIPS_SAA7135
:
173 if (1 == dev
->oss
.channels
)
175 if (2 == dev
->oss
.channels
)
179 saa_writel(0x588 >> 2, dev
->oss
.blksize
-4);
180 saa_writel(0x58c >> 2, 0x543210 | (fmt
<< 24));
183 dprintk("rec_start: afmt=%d ch=%d => fmt=0x%x swap=%c\n",
184 dev
->oss
.afmt
, dev
->oss
.channels
, fmt
,
187 /* dma: setup channel 6 (= AUDIO) */
188 control
= SAA7134_RS_CONTROL_BURST_16
|
189 SAA7134_RS_CONTROL_ME
|
190 (dev
->oss
.pt
.dma
>> 12);
192 control
|= SAA7134_RS_CONTROL_BSWAP
;
193 saa_writel(SAA7134_RS_BA1(6),0);
194 saa_writel(SAA7134_RS_BA2(6),dev
->oss
.blksize
);
195 saa_writel(SAA7134_RS_PITCH(6),0);
196 saa_writel(SAA7134_RS_CONTROL(6),control
);
199 dev
->oss
.recording_on
= 1;
200 spin_lock_irqsave(&dev
->slock
,flags
);
202 spin_unlock_irqrestore(&dev
->slock
,flags
);
206 saa7134_pgtable_free(dev
->pci
,&dev
->oss
.pt
);
208 videobuf_dma_pci_unmap(dev
->pci
,&dev
->oss
.dma
);
212 static int dsp_rec_stop(struct saa7134_dev
*dev
)
216 dprintk("rec_stop dma_blk=%d\n",dev
->oss
.dma_blk
);
219 dev
->oss
.recording_on
= 0;
220 spin_lock_irqsave(&dev
->slock
,flags
);
222 spin_unlock_irqrestore(&dev
->slock
,flags
);
225 saa7134_pgtable_free(dev
->pci
,&dev
->oss
.pt
);
226 videobuf_dma_pci_unmap(dev
->pci
,&dev
->oss
.dma
);
230 /* ------------------------------------------------------------------ */
232 static int dsp_open(struct inode
*inode
, struct file
*file
)
234 int minor
= iminor(inode
);
235 struct saa7134_dev
*h
,*dev
= NULL
;
236 struct list_head
*list
;
239 list_for_each(list
,&saa7134_devlist
) {
240 h
= list_entry(list
, struct saa7134_dev
, devlist
);
241 if (h
->oss
.minor_dsp
== minor
)
247 down(&dev
->oss
.lock
);
249 if (dev
->oss
.users_dsp
)
251 dev
->oss
.users_dsp
++;
252 file
->private_data
= dev
;
254 dev
->oss
.afmt
= AFMT_U8
;
255 dev
->oss
.channels
= 1;
256 dev
->oss
.read_count
= 0;
257 dev
->oss
.read_offset
= 0;
258 dsp_buffer_conf(dev
,PAGE_SIZE
,64);
259 err
= dsp_buffer_init(dev
);
267 dev
->oss
.users_dsp
--;
273 static int dsp_release(struct inode
*inode
, struct file
*file
)
275 struct saa7134_dev
*dev
= file
->private_data
;
277 down(&dev
->oss
.lock
);
278 if (dev
->oss
.recording_on
)
280 dsp_buffer_free(dev
);
281 dev
->oss
.users_dsp
--;
282 file
->private_data
= NULL
;
287 static ssize_t
dsp_read(struct file
*file
, char __user
*buffer
,
288 size_t count
, loff_t
*ppos
)
290 struct saa7134_dev
*dev
= file
->private_data
;
291 DECLARE_WAITQUEUE(wait
, current
);
296 add_wait_queue(&dev
->oss
.wq
, &wait
);
297 down(&dev
->oss
.lock
);
299 /* wait for data if needed */
300 if (0 == dev
->oss
.read_count
) {
301 if (!dev
->oss
.recording_on
) {
302 err
= dsp_rec_start(dev
);
309 if (dev
->oss
.recording_on
&&
310 !dev
->oss
.dma_running
) {
311 /* recover from overruns */
312 spin_lock_irqsave(&dev
->slock
,flags
);
314 spin_unlock_irqrestore(&dev
->slock
,flags
);
316 if (file
->f_flags
& O_NONBLOCK
) {
322 set_current_state(TASK_INTERRUPTIBLE
);
323 if (0 == dev
->oss
.read_count
)
325 set_current_state(TASK_RUNNING
);
326 down(&dev
->oss
.lock
);
327 if (signal_pending(current
)) {
334 /* copy data to userspace */
336 if (bytes
> dev
->oss
.read_count
)
337 bytes
= dev
->oss
.read_count
;
338 if (bytes
> dev
->oss
.bufsize
- dev
->oss
.read_offset
)
339 bytes
= dev
->oss
.bufsize
- dev
->oss
.read_offset
;
340 if (copy_to_user(buffer
+ ret
,
341 dev
->oss
.dma
.vmalloc
+ dev
->oss
.read_offset
,
350 dev
->oss
.read_count
-= bytes
;
351 dev
->oss
.read_offset
+= bytes
;
352 if (dev
->oss
.read_offset
== dev
->oss
.bufsize
)
353 dev
->oss
.read_offset
= 0;
356 remove_wait_queue(&dev
->oss
.wq
, &wait
);
360 static ssize_t
dsp_write(struct file
*file
, const char __user
*buffer
,
361 size_t count
, loff_t
*ppos
)
366 static int dsp_ioctl(struct inode
*inode
, struct file
*file
,
367 unsigned int cmd
, unsigned long arg
)
369 struct saa7134_dev
*dev
= file
->private_data
;
370 void __user
*argp
= (void __user
*) arg
;
371 int __user
*p
= argp
;
375 saa7134_print_ioctl(dev
->name
,cmd
);
378 return put_user(SOUND_VERSION
, p
);
379 case SNDCTL_DSP_GETCAPS
:
382 case SNDCTL_DSP_SPEED
:
383 if (get_user(val
, p
))
386 case SOUND_PCM_READ_RATE
:
387 return put_user(dev
->oss
.rate
, p
);
389 case SNDCTL_DSP_STEREO
:
390 if (get_user(val
, p
))
392 down(&dev
->oss
.lock
);
393 dev
->oss
.channels
= val
? 2 : 1;
394 if (dev
->oss
.recording_on
) {
399 return put_user(dev
->oss
.channels
-1, p
);
401 case SNDCTL_DSP_CHANNELS
:
402 if (get_user(val
, p
))
404 if (val
!= 1 && val
!= 2)
406 down(&dev
->oss
.lock
);
407 dev
->oss
.channels
= val
;
408 if (dev
->oss
.recording_on
) {
414 case SOUND_PCM_READ_CHANNELS
:
415 return put_user(dev
->oss
.channels
, p
);
417 case SNDCTL_DSP_GETFMTS
: /* Returns a mask */
418 return put_user(AFMT_U8
| AFMT_S8
|
419 AFMT_U16_LE
| AFMT_U16_BE
|
420 AFMT_S16_LE
| AFMT_S16_BE
, p
);
422 case SNDCTL_DSP_SETFMT
: /* Selects ONE fmt */
423 if (get_user(val
, p
))
435 down(&dev
->oss
.lock
);
437 if (dev
->oss
.recording_on
) {
442 return put_user(dev
->oss
.afmt
, p
);
447 case SOUND_PCM_READ_BITS
:
448 switch (dev
->oss
.afmt
) {
451 return put_user(8, p
);
456 return put_user(16, p
);
461 case SNDCTL_DSP_NONBLOCK
:
462 file
->f_flags
|= O_NONBLOCK
;
465 case SNDCTL_DSP_RESET
:
466 down(&dev
->oss
.lock
);
467 if (dev
->oss
.recording_on
)
471 case SNDCTL_DSP_GETBLKSIZE
:
472 return put_user(dev
->oss
.blksize
, p
);
474 case SNDCTL_DSP_SETFRAGMENT
:
475 if (get_user(val
, p
))
477 if (dev
->oss
.recording_on
)
479 dsp_buffer_free(dev
);
480 /* used to be arg >> 16 instead of val >> 16; fixed */
481 dsp_buffer_conf(dev
,1 << (val
& 0xffff), (val
>> 16) & 0xffff);
482 dsp_buffer_init(dev
);
485 case SNDCTL_DSP_SYNC
:
489 case SNDCTL_DSP_GETISPACE
:
492 info
.fragsize
= dev
->oss
.blksize
;
493 info
.fragstotal
= dev
->oss
.blocks
;
494 info
.bytes
= dev
->oss
.read_count
;
495 info
.fragments
= info
.bytes
/ info
.fragsize
;
496 if (copy_to_user(argp
, &info
, sizeof(info
)))
505 static unsigned int dsp_poll(struct file
*file
, struct poll_table_struct
*wait
)
507 struct saa7134_dev
*dev
= file
->private_data
;
508 unsigned int mask
= 0;
510 poll_wait(file
, &dev
->oss
.wq
, wait
);
512 if (0 == dev
->oss
.read_count
) {
513 down(&dev
->oss
.lock
);
514 if (!dev
->oss
.recording_on
)
518 mask
|= (POLLIN
| POLLRDNORM
);
522 struct file_operations saa7134_dsp_fops
= {
523 .owner
= THIS_MODULE
,
525 .release
= dsp_release
,
533 /* ------------------------------------------------------------------ */
536 mixer_recsrc_7134(struct saa7134_dev
*dev
)
540 switch (dev
->oss
.input
) {
542 saa_andorb(SAA7134_AUDIO_FORMAT_CTRL
, 0xc0, 0xc0);
543 saa_andorb(SAA7134_SIF_SAMPLE_FREQ
, 0x03, 0x00);
548 analog_io
= (LINE1
== dev
->oss
.input
) ? 0x00 : 0x08;
549 rate
= (32000 == dev
->oss
.rate
) ? 0x01 : 0x03;
550 saa_andorb(SAA7134_ANALOG_IO_SELECT
, 0x08, analog_io
);
551 saa_andorb(SAA7134_AUDIO_FORMAT_CTRL
, 0xc0, 0x80);
552 saa_andorb(SAA7134_SIF_SAMPLE_FREQ
, 0x03, rate
);
559 mixer_recsrc_7133(struct saa7134_dev
*dev
)
561 u32 value
= 0xbbbbbb;
563 switch (dev
->oss
.input
) {
565 value
= 0xbbbb10; /* MAIN */
568 value
= 0xbbbb32; /* AUX1 */
572 value
= 0xbbbb54; /* AUX2 */
575 saa_dsp_writel(dev
, 0x46c >> 2, value
);
580 mixer_recsrc(struct saa7134_dev
*dev
, enum saa7134_audio_in src
)
582 static const char *iname
[] = { "Oops", "TV", "LINE1", "LINE2" };
585 dev
->oss
.input
= src
;
586 dprintk("mixer input = %s\n",iname
[dev
->oss
.input
]);
588 switch (dev
->pci
->device
) {
589 case PCI_DEVICE_ID_PHILIPS_SAA7134
:
590 mixer_recsrc_7134(dev
);
592 case PCI_DEVICE_ID_PHILIPS_SAA7133
:
593 case PCI_DEVICE_ID_PHILIPS_SAA7135
:
594 mixer_recsrc_7133(dev
);
601 mixer_level(struct saa7134_dev
*dev
, enum saa7134_audio_in src
, int level
)
603 switch (dev
->pci
->device
) {
604 case PCI_DEVICE_ID_PHILIPS_SAA7134
:
610 saa_andorb(SAA7134_ANALOG_IO_SELECT
, 0x10,
611 (100 == level
) ? 0x00 : 0x10);
615 saa_andorb(SAA7134_ANALOG_IO_SELECT
, 0x20,
616 (100 == level
) ? 0x00 : 0x20);
620 case PCI_DEVICE_ID_PHILIPS_SAA7133
:
621 case PCI_DEVICE_ID_PHILIPS_SAA7135
:
628 /* ------------------------------------------------------------------ */
630 static int mixer_open(struct inode
*inode
, struct file
*file
)
632 int minor
= iminor(inode
);
633 struct saa7134_dev
*h
,*dev
= NULL
;
634 struct list_head
*list
;
636 list_for_each(list
,&saa7134_devlist
) {
637 h
= list_entry(list
, struct saa7134_dev
, devlist
);
638 if (h
->oss
.minor_mixer
== minor
)
644 file
->private_data
= dev
;
648 static int mixer_release(struct inode
*inode
, struct file
*file
)
650 file
->private_data
= NULL
;
654 static int mixer_ioctl(struct inode
*inode
, struct file
*file
,
655 unsigned int cmd
, unsigned long arg
)
657 struct saa7134_dev
*dev
= file
->private_data
;
658 enum saa7134_audio_in input
;
660 void __user
*argp
= (void __user
*) arg
;
661 int __user
*p
= argp
;
664 saa7134_print_ioctl(dev
->name
,cmd
);
667 return put_user(SOUND_VERSION
, p
);
668 case SOUND_MIXER_INFO
:
671 memset(&info
,0,sizeof(info
));
672 strlcpy(info
.id
, "TV audio", sizeof(info
.id
));
673 strlcpy(info
.name
, dev
->name
, sizeof(info
.name
));
674 info
.modify_counter
= dev
->oss
.count
;
675 if (copy_to_user(argp
, &info
, sizeof(info
)))
679 case SOUND_OLD_MIXER_INFO
:
681 _old_mixer_info info
;
682 memset(&info
,0,sizeof(info
));
683 strlcpy(info
.id
, "TV audio", sizeof(info
.id
));
684 strlcpy(info
.name
, dev
->name
, sizeof(info
.name
));
685 if (copy_to_user(argp
, &info
, sizeof(info
)))
689 case MIXER_READ(SOUND_MIXER_CAPS
):
690 return put_user(SOUND_CAP_EXCL_INPUT
, p
);
691 case MIXER_READ(SOUND_MIXER_STEREODEVS
):
692 return put_user(0, p
);
693 case MIXER_READ(SOUND_MIXER_RECMASK
):
694 case MIXER_READ(SOUND_MIXER_DEVMASK
):
695 val
= SOUND_MASK_LINE1
| SOUND_MASK_LINE2
;
696 if (32000 == dev
->oss
.rate
)
697 val
|= SOUND_MASK_VIDEO
;
698 return put_user(val
, p
);
700 case MIXER_WRITE(SOUND_MIXER_RECSRC
):
701 if (get_user(val
, p
))
703 input
= dev
->oss
.input
;
704 if (32000 == dev
->oss
.rate
&&
705 val
& SOUND_MASK_VIDEO
&& dev
->oss
.input
!= TV
)
707 if (val
& SOUND_MASK_LINE1
&& dev
->oss
.input
!= LINE1
)
709 if (val
& SOUND_MASK_LINE2
&& dev
->oss
.input
!= LINE2
)
711 if (input
!= dev
->oss
.input
)
712 mixer_recsrc(dev
,input
);
714 case MIXER_READ(SOUND_MIXER_RECSRC
):
715 switch (dev
->oss
.input
) {
716 case TV
: ret
= SOUND_MASK_VIDEO
; break;
717 case LINE1
: ret
= SOUND_MASK_LINE1
; break;
718 case LINE2
: ret
= SOUND_MASK_LINE2
; break;
721 return put_user(ret
, p
);
723 case MIXER_WRITE(SOUND_MIXER_VIDEO
):
724 case MIXER_READ(SOUND_MIXER_VIDEO
):
725 if (32000 != dev
->oss
.rate
)
727 return put_user(100 | 100 << 8, p
);
729 case MIXER_WRITE(SOUND_MIXER_LINE1
):
730 if (get_user(val
, p
))
733 val
= (val
<= 50) ? 50 : 100;
734 dev
->oss
.line1
= val
;
735 mixer_level(dev
,LINE1
,dev
->oss
.line1
);
737 case MIXER_READ(SOUND_MIXER_LINE1
):
738 return put_user(dev
->oss
.line1
| dev
->oss
.line1
<< 8, p
);
740 case MIXER_WRITE(SOUND_MIXER_LINE2
):
741 if (get_user(val
, p
))
744 val
= (val
<= 50) ? 50 : 100;
745 dev
->oss
.line2
= val
;
746 mixer_level(dev
,LINE2
,dev
->oss
.line2
);
748 case MIXER_READ(SOUND_MIXER_LINE2
):
749 return put_user(dev
->oss
.line2
| dev
->oss
.line2
<< 8, p
);
756 struct file_operations saa7134_mixer_fops
= {
757 .owner
= THIS_MODULE
,
759 .release
= mixer_release
,
760 .ioctl
= mixer_ioctl
,
764 /* ------------------------------------------------------------------ */
766 int saa7134_oss_init1(struct saa7134_dev
*dev
)
769 init_MUTEX(&dev
->oss
.lock
);
770 init_waitqueue_head(&dev
->oss
.wq
);
772 switch (dev
->pci
->device
) {
773 case PCI_DEVICE_ID_PHILIPS_SAA7133
:
774 case PCI_DEVICE_ID_PHILIPS_SAA7135
:
775 saa_writel(0x588 >> 2, 0x00000fff);
776 saa_writel(0x58c >> 2, 0x00543210);
777 saa_dsp_writel(dev
, 0x46c >> 2, 0xbbbbbb);
782 dev
->oss
.rate
= 32000;
784 dev
->oss
.rate
= oss_rate
;
785 dev
->oss
.rate
= (dev
->oss
.rate
> 40000) ? 48000 : 32000;
790 mixer_level(dev
,LINE1
,dev
->oss
.line1
);
791 mixer_level(dev
,LINE2
,dev
->oss
.line2
);
792 mixer_recsrc(dev
, (dev
->oss
.rate
== 32000) ? TV
: LINE2
);
797 int saa7134_oss_fini(struct saa7134_dev
*dev
)
803 void saa7134_irq_oss_done(struct saa7134_dev
*dev
, unsigned long status
)
805 int next_blk
, reg
= 0;
807 spin_lock(&dev
->slock
);
808 if (UNSET
== dev
->oss
.dma_blk
) {
809 dprintk("irq: recording stopped\n");
812 if (0 != (status
& 0x0f000000))
813 dprintk("irq: lost %ld\n", (status
>> 24) & 0x0f);
814 if (0 == (status
& 0x10000000)) {
816 if (0 == (dev
->oss
.dma_blk
& 0x01))
817 reg
= SAA7134_RS_BA1(6);
820 if (0 == (dev
->oss
.dma_blk
& 0x00))
821 reg
= SAA7134_RS_BA2(6);
824 dprintk("irq: field oops [%s]\n",
825 (status
& 0x10000000) ? "even" : "odd");
828 if (dev
->oss
.read_count
>= dev
->oss
.blksize
* (dev
->oss
.blocks
-2)) {
829 dprintk("irq: overrun [full=%d/%d]\n",dev
->oss
.read_count
,
835 /* next block addr */
836 next_blk
= (dev
->oss
.dma_blk
+ 2) % dev
->oss
.blocks
;
837 saa_writel(reg
,next_blk
* dev
->oss
.blksize
);
839 dprintk("irq: ok, %s, next_blk=%d, addr=%x\n",
840 (status
& 0x10000000) ? "even" : "odd ", next_blk
,
841 next_blk
* dev
->oss
.blksize
);
843 /* update status & wake waiting readers */
844 dev
->oss
.dma_blk
= (dev
->oss
.dma_blk
+ 1) % dev
->oss
.blocks
;
845 dev
->oss
.read_count
+= dev
->oss
.blksize
;
846 wake_up(&dev
->oss
.wq
);
849 spin_unlock(&dev
->slock
);
852 /* ----------------------------------------------------------- */