saa7134-oss.c 24.8 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6
/*
 *
 * device driver for philips saa7134 based TV cards
 * oss dsp interface
 *
 * (c) 2001,02 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
7 8
 *     2005 conversion to standalone module:
 *         Ricardo Cerqueira <v4l@cerqueira.org>
L
Linus Torvalds 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <linux/init.h>
#include <linux/list.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
30
#include <linux/interrupt.h>
L
Linus Torvalds 已提交
31
#include <linux/slab.h>
32
#include <linux/sound.h>
L
Linus Torvalds 已提交
33 34 35 36 37 38 39
#include <linux/soundcard.h>

#include "saa7134-reg.h"
#include "saa7134.h"

/* ------------------------------------------------------------------ */

40 41 42
static unsigned int debug  = 0;
module_param(debug, int, 0644);
MODULE_PARM_DESC(debug,"enable debug messages [oss]");
L
Linus Torvalds 已提交
43

44 45 46
static unsigned int rate  = 0;
module_param(rate, int, 0444);
MODULE_PARM_DESC(rate,"sample rate (valid are: 32000,48000)");
L
Linus Torvalds 已提交
47

48 49 50 51 52 53 54 55 56
static unsigned int dsp_nr[]   = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
MODULE_PARM_DESC(dsp_nr, "device numbers for SAA7134 capture interface(s).");
module_param_array(dsp_nr,   int, NULL, 0444);

static unsigned int mixer_nr[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
MODULE_PARM_DESC(mixer_nr, "mixer numbers for SAA7134 capture interface(s).");
module_param_array(mixer_nr, int, NULL, 0444);

#define dprintk(fmt, arg...)	if (debug) \
L
Linus Torvalds 已提交
57 58
	printk(KERN_DEBUG "%s/oss: " fmt, dev->name , ## arg)

59

L
Linus Torvalds 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72 73
/* ------------------------------------------------------------------ */

static int dsp_buffer_conf(struct saa7134_dev *dev, int blksize, int blocks)
{
	if (blksize < 0x100)
		blksize = 0x100;
	if (blksize > 0x10000)
		blksize = 0x10000;

	if (blocks < 2)
		blocks = 2;
	if ((blksize * blocks) > 1024*1024)
		blocks = 1024*1024 / blksize;

74 75 76
	dev->dmasound.blocks  = blocks;
	dev->dmasound.blksize = blksize;
	dev->dmasound.bufsize = blksize * blocks;
L
Linus Torvalds 已提交
77 78

	dprintk("buffer config: %d blocks / %d bytes, %d kB total\n",
79
		blocks,blksize,blksize * blocks / 1024);
L
Linus Torvalds 已提交
80 81 82 83 84 85 86
	return 0;
}

static int dsp_buffer_init(struct saa7134_dev *dev)
{
	int err;

87
	if (!dev->dmasound.bufsize)
L
Linus Torvalds 已提交
88
		BUG();
89 90 91
	videobuf_dma_init(&dev->dmasound.dma);
	err = videobuf_dma_init_kernel(&dev->dmasound.dma, PCI_DMA_FROMDEVICE,
				       (dev->dmasound.bufsize + PAGE_SIZE) >> PAGE_SHIFT);
L
Linus Torvalds 已提交
92 93 94 95 96 97 98
	if (0 != err)
		return err;
	return 0;
}

static int dsp_buffer_free(struct saa7134_dev *dev)
{
99
	if (!dev->dmasound.blksize)
L
Linus Torvalds 已提交
100
		BUG();
101 102 103 104
	videobuf_dma_free(&dev->dmasound.dma);
	dev->dmasound.blocks  = 0;
	dev->dmasound.blksize = 0;
	dev->dmasound.bufsize = 0;
L
Linus Torvalds 已提交
105 106 107 108 109
	return 0;
}

static void dsp_dma_start(struct saa7134_dev *dev)
{
110 111
	dev->dmasound.dma_blk     = 0;
	dev->dmasound.dma_running = 1;
L
Linus Torvalds 已提交
112 113 114 115 116
	saa7134_set_dmabits(dev);
}

static void dsp_dma_stop(struct saa7134_dev *dev)
{
117 118
	dev->dmasound.dma_blk     = -1;
	dev->dmasound.dma_running = 0;
L
Linus Torvalds 已提交
119 120 121 122 123 124 125 126 127 128
	saa7134_set_dmabits(dev);
}

static int dsp_rec_start(struct saa7134_dev *dev)
{
	int err, bswap, sign;
	u32 fmt, control;
	unsigned long flags;

	/* prepare buffer */
129
	if (0 != (err = videobuf_dma_pci_map(dev->pci,&dev->dmasound.dma)))
L
Linus Torvalds 已提交
130
		return err;
131
	if (0 != (err = saa7134_pgtable_alloc(dev->pci,&dev->dmasound.pt)))
L
Linus Torvalds 已提交
132
		goto fail1;
133 134 135
	if (0 != (err = saa7134_pgtable_build(dev->pci,&dev->dmasound.pt,
					      dev->dmasound.dma.sglist,
					      dev->dmasound.dma.sglen,
L
Linus Torvalds 已提交
136 137 138 139
					      0)))
		goto fail2;

	/* sample format */
140
	switch (dev->dmasound.afmt) {
L
Linus Torvalds 已提交
141 142 143 144 145 146 147 148 149 150 151
	case AFMT_U8:
	case AFMT_S8:     fmt = 0x00;  break;
	case AFMT_U16_LE:
	case AFMT_U16_BE:
	case AFMT_S16_LE:
	case AFMT_S16_BE: fmt = 0x01;  break;
	default:
		err = -EINVAL;
		goto fail2;
	}

152
	switch (dev->dmasound.afmt) {
L
Linus Torvalds 已提交
153 154 155 156 157 158
	case AFMT_S8:
	case AFMT_S16_LE:
	case AFMT_S16_BE: sign = 1; break;
	default:          sign = 0; break;
	}

159
	switch (dev->dmasound.afmt) {
L
Linus Torvalds 已提交
160 161 162 163 164 165 166
	case AFMT_U16_BE:
	case AFMT_S16_BE: bswap = 1; break;
	default:          bswap = 0; break;
	}

	switch (dev->pci->device) {
	case PCI_DEVICE_ID_PHILIPS_SAA7134:
167
		if (1 == dev->dmasound.channels)
L
Linus Torvalds 已提交
168
			fmt |= (1 << 3);
169
		if (2 == dev->dmasound.channels)
L
Linus Torvalds 已提交
170 171 172
			fmt |= (3 << 3);
		if (sign)
			fmt |= 0x04;
173
		fmt |= (TV == dev->dmasound.input) ? 0xc0 : 0x80;
L
Linus Torvalds 已提交
174

175 176 177
		saa_writeb(SAA7134_NUM_SAMPLES0, ((dev->dmasound.blksize - 1) & 0x0000ff));
		saa_writeb(SAA7134_NUM_SAMPLES1, ((dev->dmasound.blksize - 1) & 0x00ff00) >>  8);
		saa_writeb(SAA7134_NUM_SAMPLES2, ((dev->dmasound.blksize - 1) & 0xff0000) >> 16);
L
Linus Torvalds 已提交
178
		saa_writeb(SAA7134_AUDIO_FORMAT_CTRL, fmt);
179

L
Linus Torvalds 已提交
180 181 182
		break;
	case PCI_DEVICE_ID_PHILIPS_SAA7133:
	case PCI_DEVICE_ID_PHILIPS_SAA7135:
183
		if (1 == dev->dmasound.channels)
L
Linus Torvalds 已提交
184
			fmt |= (1 << 4);
185
		if (2 == dev->dmasound.channels)
L
Linus Torvalds 已提交
186 187 188
			fmt |= (2 << 4);
		if (!sign)
			fmt |= 0x04;
189
		saa_writel(SAA7133_NUM_SAMPLES, dev->dmasound.blksize -4);
190
		saa_writel(SAA7133_AUDIO_CHANNEL, 0x543210 | (fmt << 24));
L
Linus Torvalds 已提交
191 192 193
		break;
	}
	dprintk("rec_start: afmt=%d ch=%d  =>  fmt=0x%x swap=%c\n",
194
		dev->dmasound.afmt, dev->dmasound.channels, fmt,
L
Linus Torvalds 已提交
195 196 197 198 199
		bswap ? 'b' : '-');

	/* dma: setup channel 6 (= AUDIO) */
	control = SAA7134_RS_CONTROL_BURST_16 |
		SAA7134_RS_CONTROL_ME |
200
		(dev->dmasound.pt.dma >> 12);
L
Linus Torvalds 已提交
201 202 203
	if (bswap)
		control |= SAA7134_RS_CONTROL_BSWAP;
	saa_writel(SAA7134_RS_BA1(6),0);
204
	saa_writel(SAA7134_RS_BA2(6),dev->dmasound.blksize);
L
Linus Torvalds 已提交
205 206 207 208
	saa_writel(SAA7134_RS_PITCH(6),0);
	saa_writel(SAA7134_RS_CONTROL(6),control);

	/* start dma */
209
	dev->dmasound.recording_on = 1;
L
Linus Torvalds 已提交
210 211 212 213 214 215
	spin_lock_irqsave(&dev->slock,flags);
	dsp_dma_start(dev);
	spin_unlock_irqrestore(&dev->slock,flags);
	return 0;

 fail2:
216
	saa7134_pgtable_free(dev->pci,&dev->dmasound.pt);
L
Linus Torvalds 已提交
217
 fail1:
218
	videobuf_dma_pci_unmap(dev->pci,&dev->dmasound.dma);
L
Linus Torvalds 已提交
219 220 221 222 223 224 225
	return err;
}

static int dsp_rec_stop(struct saa7134_dev *dev)
{
	unsigned long flags;

226
	dprintk("rec_stop dma_blk=%d\n",dev->dmasound.dma_blk);
L
Linus Torvalds 已提交
227 228

	/* stop dma */
229
	dev->dmasound.recording_on = 0;
L
Linus Torvalds 已提交
230 231 232 233 234
	spin_lock_irqsave(&dev->slock,flags);
	dsp_dma_stop(dev);
	spin_unlock_irqrestore(&dev->slock,flags);

	/* unlock buffer */
235 236
	saa7134_pgtable_free(dev->pci,&dev->dmasound.pt);
	videobuf_dma_pci_unmap(dev->pci,&dev->dmasound.dma);
L
Linus Torvalds 已提交
237 238 239 240 241 242 243 244 245 246 247 248 249 250
	return 0;
}

/* ------------------------------------------------------------------ */

static int dsp_open(struct inode *inode, struct file *file)
{
	int minor = iminor(inode);
	struct saa7134_dev *h,*dev = NULL;
	struct list_head *list;
	int err;

	list_for_each(list,&saa7134_devlist) {
		h = list_entry(list, struct saa7134_dev, devlist);
251
		if (h->dmasound.minor_dsp == minor)
L
Linus Torvalds 已提交
252 253 254 255 256
			dev = h;
	}
	if (NULL == dev)
		return -ENODEV;

257
	down(&dev->dmasound.lock);
L
Linus Torvalds 已提交
258
	err = -EBUSY;
259
	if (dev->dmasound.users_dsp)
L
Linus Torvalds 已提交
260
		goto fail1;
261
	dev->dmasound.users_dsp++;
L
Linus Torvalds 已提交
262 263
	file->private_data = dev;

264 265 266 267
	dev->dmasound.afmt        = AFMT_U8;
	dev->dmasound.channels    = 1;
	dev->dmasound.read_count  = 0;
	dev->dmasound.read_offset = 0;
L
Linus Torvalds 已提交
268 269 270 271 272
	dsp_buffer_conf(dev,PAGE_SIZE,64);
	err = dsp_buffer_init(dev);
	if (0 != err)
		goto fail2;

273
	up(&dev->dmasound.lock);
L
Linus Torvalds 已提交
274 275 276
	return 0;

 fail2:
277
	dev->dmasound.users_dsp--;
L
Linus Torvalds 已提交
278
 fail1:
279
	up(&dev->dmasound.lock);
L
Linus Torvalds 已提交
280 281 282 283 284 285 286
	return err;
}

static int dsp_release(struct inode *inode, struct file *file)
{
	struct saa7134_dev *dev = file->private_data;

287 288
	down(&dev->dmasound.lock);
	if (dev->dmasound.recording_on)
L
Linus Torvalds 已提交
289 290
		dsp_rec_stop(dev);
	dsp_buffer_free(dev);
291
	dev->dmasound.users_dsp--;
L
Linus Torvalds 已提交
292
	file->private_data = NULL;
293
	up(&dev->dmasound.lock);
L
Linus Torvalds 已提交
294 295 296 297 298 299 300 301 302 303 304 305
	return 0;
}

static ssize_t dsp_read(struct file *file, char __user *buffer,
			size_t count, loff_t *ppos)
{
	struct saa7134_dev *dev = file->private_data;
	DECLARE_WAITQUEUE(wait, current);
	unsigned int bytes;
	unsigned long flags;
	int err,ret = 0;

306 307
	add_wait_queue(&dev->dmasound.wq, &wait);
	down(&dev->dmasound.lock);
L
Linus Torvalds 已提交
308 309
	while (count > 0) {
		/* wait for data if needed */
310 311
		if (0 == dev->dmasound.read_count) {
			if (!dev->dmasound.recording_on) {
L
Linus Torvalds 已提交
312 313 314 315 316 317 318
				err = dsp_rec_start(dev);
				if (err < 0) {
					if (0 == ret)
						ret = err;
					break;
				}
			}
319 320
			if (dev->dmasound.recording_on &&
			    !dev->dmasound.dma_running) {
L
Linus Torvalds 已提交
321 322 323 324 325 326 327 328 329 330
				/* recover from overruns */
				spin_lock_irqsave(&dev->slock,flags);
				dsp_dma_start(dev);
				spin_unlock_irqrestore(&dev->slock,flags);
			}
			if (file->f_flags & O_NONBLOCK) {
				if (0 == ret)
					ret = -EAGAIN;
				break;
			}
331
			up(&dev->dmasound.lock);
L
Linus Torvalds 已提交
332
			set_current_state(TASK_INTERRUPTIBLE);
333
			if (0 == dev->dmasound.read_count)
L
Linus Torvalds 已提交
334 335
				schedule();
			set_current_state(TASK_RUNNING);
336
			down(&dev->dmasound.lock);
L
Linus Torvalds 已提交
337 338 339 340 341 342 343 344 345
			if (signal_pending(current)) {
				if (0 == ret)
					ret = -EINTR;
				break;
			}
		}

		/* copy data to userspace */
		bytes = count;
346 347 348 349
		if (bytes > dev->dmasound.read_count)
			bytes = dev->dmasound.read_count;
		if (bytes > dev->dmasound.bufsize - dev->dmasound.read_offset)
			bytes = dev->dmasound.bufsize - dev->dmasound.read_offset;
L
Linus Torvalds 已提交
350
		if (copy_to_user(buffer + ret,
351
				 dev->dmasound.dma.vmalloc + dev->dmasound.read_offset,
L
Linus Torvalds 已提交
352 353 354 355 356 357 358 359
				 bytes)) {
			if (0 == ret)
				ret = -EFAULT;
			break;
		}

		ret   += bytes;
		count -= bytes;
360 361 362 363
		dev->dmasound.read_count  -= bytes;
		dev->dmasound.read_offset += bytes;
		if (dev->dmasound.read_offset == dev->dmasound.bufsize)
			dev->dmasound.read_offset = 0;
L
Linus Torvalds 已提交
364
	}
365 366
	up(&dev->dmasound.lock);
	remove_wait_queue(&dev->dmasound.wq, &wait);
L
Linus Torvalds 已提交
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
	return ret;
}

static ssize_t dsp_write(struct file *file, const char __user *buffer,
			 size_t count, loff_t *ppos)
{
	return -EINVAL;
}

static int dsp_ioctl(struct inode *inode, struct file *file,
		     unsigned int cmd, unsigned long arg)
{
	struct saa7134_dev *dev = file->private_data;
	void __user *argp = (void __user *) arg;
	int __user *p = argp;
	int val = 0;

384
	if (debug > 1)
L
Linus Torvalds 已提交
385
		saa7134_print_ioctl(dev->name,cmd);
386 387 388 389
	switch (cmd) {
	case OSS_GETVERSION:
		return put_user(SOUND_VERSION, p);
	case SNDCTL_DSP_GETCAPS:
L
Linus Torvalds 已提交
390 391
		return 0;

392
	case SNDCTL_DSP_SPEED:
L
Linus Torvalds 已提交
393 394 395
		if (get_user(val, p))
			return -EFAULT;
		/* fall through */
396
	case SOUND_PCM_READ_RATE:
397
		return put_user(dev->dmasound.rate, p);
L
Linus Torvalds 已提交
398

399
	case SNDCTL_DSP_STEREO:
L
Linus Torvalds 已提交
400 401
		if (get_user(val, p))
			return -EFAULT;
402 403 404
		down(&dev->dmasound.lock);
		dev->dmasound.channels = val ? 2 : 1;
		if (dev->dmasound.recording_on) {
L
Linus Torvalds 已提交
405 406 407
			dsp_rec_stop(dev);
			dsp_rec_start(dev);
		}
408 409
		up(&dev->dmasound.lock);
		return put_user(dev->dmasound.channels-1, p);
L
Linus Torvalds 已提交
410

411
	case SNDCTL_DSP_CHANNELS:
L
Linus Torvalds 已提交
412 413 414 415
		if (get_user(val, p))
			return -EFAULT;
		if (val != 1 && val != 2)
			return -EINVAL;
416 417 418
		down(&dev->dmasound.lock);
		dev->dmasound.channels = val;
		if (dev->dmasound.recording_on) {
L
Linus Torvalds 已提交
419 420 421
			dsp_rec_stop(dev);
			dsp_rec_start(dev);
		}
422
		up(&dev->dmasound.lock);
L
Linus Torvalds 已提交
423
		/* fall through */
424
	case SOUND_PCM_READ_CHANNELS:
425
		return put_user(dev->dmasound.channels, p);
L
Linus Torvalds 已提交
426

427
	case SNDCTL_DSP_GETFMTS: /* Returns a mask */
L
Linus Torvalds 已提交
428 429 430 431
		return put_user(AFMT_U8     | AFMT_S8     |
				AFMT_U16_LE | AFMT_U16_BE |
				AFMT_S16_LE | AFMT_S16_BE, p);

432
	case SNDCTL_DSP_SETFMT: /* Selects ONE fmt */
L
Linus Torvalds 已提交
433 434 435 436 437 438 439 440 441 442 443 444
		if (get_user(val, p))
			return -EFAULT;
		switch (val) {
		case AFMT_QUERY:
			/* nothing to do */
			break;
		case AFMT_U8:
		case AFMT_S8:
		case AFMT_U16_LE:
		case AFMT_U16_BE:
		case AFMT_S16_LE:
		case AFMT_S16_BE:
445 446 447
			down(&dev->dmasound.lock);
			dev->dmasound.afmt = val;
			if (dev->dmasound.recording_on) {
L
Linus Torvalds 已提交
448 449 450
				dsp_rec_stop(dev);
				dsp_rec_start(dev);
			}
451 452
			up(&dev->dmasound.lock);
			return put_user(dev->dmasound.afmt, p);
L
Linus Torvalds 已提交
453 454 455 456
		default:
			return -EINVAL;
		}

457
	case SOUND_PCM_READ_BITS:
458
		switch (dev->dmasound.afmt) {
L
Linus Torvalds 已提交
459 460 461 462 463 464 465 466 467 468 469 470
		case AFMT_U8:
		case AFMT_S8:
			return put_user(8, p);
		case AFMT_U16_LE:
		case AFMT_U16_BE:
		case AFMT_S16_LE:
		case AFMT_S16_BE:
			return put_user(16, p);
		default:
			return -EINVAL;
		}

471 472 473
	case SNDCTL_DSP_NONBLOCK:
		file->f_flags |= O_NONBLOCK;
		return 0;
L
Linus Torvalds 已提交
474

475
	case SNDCTL_DSP_RESET:
476 477
		down(&dev->dmasound.lock);
		if (dev->dmasound.recording_on)
L
Linus Torvalds 已提交
478
			dsp_rec_stop(dev);
479
		up(&dev->dmasound.lock);
L
Linus Torvalds 已提交
480
		return 0;
481
	case SNDCTL_DSP_GETBLKSIZE:
482
		return put_user(dev->dmasound.blksize, p);
L
Linus Torvalds 已提交
483

484
	case SNDCTL_DSP_SETFRAGMENT:
L
Linus Torvalds 已提交
485 486
		if (get_user(val, p))
			return -EFAULT;
487
		if (dev->dmasound.recording_on)
L
Linus Torvalds 已提交
488 489 490 491 492 493 494
			return -EBUSY;
		dsp_buffer_free(dev);
		/* used to be arg >> 16 instead of val >> 16; fixed */
		dsp_buffer_conf(dev,1 << (val & 0xffff), (val >> 16) & 0xffff);
		dsp_buffer_init(dev);
		return 0;

495
	case SNDCTL_DSP_SYNC:
L
Linus Torvalds 已提交
496 497 498 499 500 501
		/* NOP */
		return 0;

	case SNDCTL_DSP_GETISPACE:
	{
		audio_buf_info info;
502 503 504
		info.fragsize   = dev->dmasound.blksize;
		info.fragstotal = dev->dmasound.blocks;
		info.bytes      = dev->dmasound.read_count;
L
Linus Torvalds 已提交
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
		info.fragments  = info.bytes / info.fragsize;
		if (copy_to_user(argp, &info, sizeof(info)))
			return -EFAULT;
		return 0;
	}
	default:
		return -EINVAL;
	}
}

static unsigned int dsp_poll(struct file *file, struct poll_table_struct *wait)
{
	struct saa7134_dev *dev = file->private_data;
	unsigned int mask = 0;

520
	poll_wait(file, &dev->dmasound.wq, wait);
L
Linus Torvalds 已提交
521

522 523 524
	if (0 == dev->dmasound.read_count) {
		down(&dev->dmasound.lock);
		if (!dev->dmasound.recording_on)
L
Linus Torvalds 已提交
525
			dsp_rec_start(dev);
526
		up(&dev->dmasound.lock);
L
Linus Torvalds 已提交
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
	} else
		mask |= (POLLIN | POLLRDNORM);
	return mask;
}

struct file_operations saa7134_dsp_fops = {
	.owner   = THIS_MODULE,
	.open    = dsp_open,
	.release = dsp_release,
	.read    = dsp_read,
	.write   = dsp_write,
	.ioctl   = dsp_ioctl,
	.poll    = dsp_poll,
	.llseek  = no_llseek,
};

/* ------------------------------------------------------------------ */

static int
mixer_recsrc_7134(struct saa7134_dev *dev)
{
	int analog_io,rate;

550
	switch (dev->dmasound.input) {
L
Linus Torvalds 已提交
551 552 553 554 555 556 557
	case TV:
		saa_andorb(SAA7134_AUDIO_FORMAT_CTRL, 0xc0, 0xc0);
		saa_andorb(SAA7134_SIF_SAMPLE_FREQ,   0x03, 0x00);
		break;
	case LINE1:
	case LINE2:
	case LINE2_LEFT:
558 559
		analog_io = (LINE1 == dev->dmasound.input) ? 0x00 : 0x08;
		rate = (32000 == dev->dmasound.rate) ? 0x01 : 0x03;
L
Linus Torvalds 已提交
560 561 562 563 564 565 566 567 568 569 570
		saa_andorb(SAA7134_ANALOG_IO_SELECT,  0x08, analog_io);
		saa_andorb(SAA7134_AUDIO_FORMAT_CTRL, 0xc0, 0x80);
		saa_andorb(SAA7134_SIF_SAMPLE_FREQ,   0x03, rate);
		break;
	}
	return 0;
}

static int
mixer_recsrc_7133(struct saa7134_dev *dev)
{
571
	u32 anabar, xbarin;
L
Linus Torvalds 已提交
572

573 574
	xbarin = 0x03; // adc
    anabar = 0;
575
	switch (dev->dmasound.input) {
L
Linus Torvalds 已提交
576
	case TV:
577
		xbarin = 0; // Demodulator
578
	anabar = 2; // DACs
L
Linus Torvalds 已提交
579 580
		break;
	case LINE1:
581
		anabar = 0;  // aux1, aux1
L
Linus Torvalds 已提交
582 583 584
		break;
	case LINE2:
	case LINE2_LEFT:
585
		anabar = 9;  // aux2, aux2
L
Linus Torvalds 已提交
586 587
		break;
	}
588 589 590 591 592
    /* output xbar always main channel */
	saa_dsp_writel(dev, 0x46c >> 2, 0xbbbb10);
	saa_dsp_writel(dev, 0x464 >> 2, xbarin);
	saa_writel(0x594 >> 2, anabar);

L
Linus Torvalds 已提交
593 594 595 596 597 598 599 600
	return 0;
}

static int
mixer_recsrc(struct saa7134_dev *dev, enum saa7134_audio_in src)
{
	static const char *iname[] = { "Oops", "TV", "LINE1", "LINE2" };

601 602 603
	dev->dmasound.count++;
	dev->dmasound.input = src;
	dprintk("mixer input = %s\n",iname[dev->dmasound.input]);
L
Linus Torvalds 已提交
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654

	switch (dev->pci->device) {
	case PCI_DEVICE_ID_PHILIPS_SAA7134:
		mixer_recsrc_7134(dev);
		break;
	case PCI_DEVICE_ID_PHILIPS_SAA7133:
	case PCI_DEVICE_ID_PHILIPS_SAA7135:
		mixer_recsrc_7133(dev);
		break;
	}
	return 0;
}

static int
mixer_level(struct saa7134_dev *dev, enum saa7134_audio_in src, int level)
{
	switch (dev->pci->device) {
	case PCI_DEVICE_ID_PHILIPS_SAA7134:
		switch (src) {
		case TV:
			/* nothing */
			break;
		case LINE1:
			saa_andorb(SAA7134_ANALOG_IO_SELECT,  0x10,
				   (100 == level) ? 0x00 : 0x10);
			break;
		case LINE2:
		case LINE2_LEFT:
			saa_andorb(SAA7134_ANALOG_IO_SELECT,  0x20,
				   (100 == level) ? 0x00 : 0x20);
			break;
		}
		break;
	case PCI_DEVICE_ID_PHILIPS_SAA7133:
	case PCI_DEVICE_ID_PHILIPS_SAA7135:
		/* nothing */
		break;
	}
	return 0;
}

/* ------------------------------------------------------------------ */

static int mixer_open(struct inode *inode, struct file *file)
{
	int minor = iminor(inode);
	struct saa7134_dev *h,*dev = NULL;
	struct list_head *list;

	list_for_each(list,&saa7134_devlist) {
		h = list_entry(list, struct saa7134_dev, devlist);
655
		if (h->dmasound.minor_mixer == minor)
L
Linus Torvalds 已提交
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
			dev = h;
	}
	if (NULL == dev)
		return -ENODEV;

	file->private_data = dev;
	return 0;
}

static int mixer_release(struct inode *inode, struct file *file)
{
	file->private_data = NULL;
	return 0;
}

static int mixer_ioctl(struct inode *inode, struct file *file,
		     unsigned int cmd, unsigned long arg)
{
	struct saa7134_dev *dev = file->private_data;
	enum saa7134_audio_in input;
	int val,ret;
	void __user *argp = (void __user *) arg;
	int __user *p = argp;

680
	if (debug > 1)
L
Linus Torvalds 已提交
681
		saa7134_print_ioctl(dev->name,cmd);
682 683 684
	switch (cmd) {
	case OSS_GETVERSION:
		return put_user(SOUND_VERSION, p);
L
Linus Torvalds 已提交
685 686 687 688
	case SOUND_MIXER_INFO:
	{
		mixer_info info;
		memset(&info,0,sizeof(info));
689 690
		strlcpy(info.id,   "TV audio", sizeof(info.id));
		strlcpy(info.name, dev->name,  sizeof(info.name));
691
		info.modify_counter = dev->dmasound.count;
692 693
		if (copy_to_user(argp, &info, sizeof(info)))
			return -EFAULT;
L
Linus Torvalds 已提交
694 695 696 697 698 699
		return 0;
	}
	case SOUND_OLD_MIXER_INFO:
	{
		_old_mixer_info info;
		memset(&info,0,sizeof(info));
700 701 702 703
		strlcpy(info.id,   "TV audio", sizeof(info.id));
		strlcpy(info.name, dev->name,  sizeof(info.name));
		if (copy_to_user(argp, &info, sizeof(info)))
			return -EFAULT;
L
Linus Torvalds 已提交
704 705 706 707 708 709 710 711 712
		return 0;
	}
	case MIXER_READ(SOUND_MIXER_CAPS):
		return put_user(SOUND_CAP_EXCL_INPUT, p);
	case MIXER_READ(SOUND_MIXER_STEREODEVS):
		return put_user(0, p);
	case MIXER_READ(SOUND_MIXER_RECMASK):
	case MIXER_READ(SOUND_MIXER_DEVMASK):
		val = SOUND_MASK_LINE1 | SOUND_MASK_LINE2;
713
		if (32000 == dev->dmasound.rate)
L
Linus Torvalds 已提交
714 715 716 717 718 719
			val |= SOUND_MASK_VIDEO;
		return put_user(val, p);

	case MIXER_WRITE(SOUND_MIXER_RECSRC):
		if (get_user(val, p))
			return -EFAULT;
720 721 722
		input = dev->dmasound.input;
		if (32000 == dev->dmasound.rate  &&
		    val & SOUND_MASK_VIDEO  &&  dev->dmasound.input != TV)
L
Linus Torvalds 已提交
723
			input = TV;
724
		if (val & SOUND_MASK_LINE1  &&  dev->dmasound.input != LINE1)
L
Linus Torvalds 已提交
725
			input = LINE1;
726
		if (val & SOUND_MASK_LINE2  &&  dev->dmasound.input != LINE2)
L
Linus Torvalds 已提交
727
			input = LINE2;
728
		if (input != dev->dmasound.input)
L
Linus Torvalds 已提交
729 730 731
			mixer_recsrc(dev,input);
		/* fall throuth */
	case MIXER_READ(SOUND_MIXER_RECSRC):
732
		switch (dev->dmasound.input) {
L
Linus Torvalds 已提交
733 734 735 736 737 738 739 740 741
		case TV:    ret = SOUND_MASK_VIDEO; break;
		case LINE1: ret = SOUND_MASK_LINE1; break;
		case LINE2: ret = SOUND_MASK_LINE2; break;
		default:    ret = 0;
		}
		return put_user(ret, p);

	case MIXER_WRITE(SOUND_MIXER_VIDEO):
	case MIXER_READ(SOUND_MIXER_VIDEO):
742
		if (32000 != dev->dmasound.rate)
L
Linus Torvalds 已提交
743 744 745 746 747 748 749 750
			return -EINVAL;
		return put_user(100 | 100 << 8, p);

	case MIXER_WRITE(SOUND_MIXER_LINE1):
		if (get_user(val, p))
			return -EFAULT;
		val &= 0xff;
		val = (val <= 50) ? 50 : 100;
751 752
		dev->dmasound.line1 = val;
		mixer_level(dev,LINE1,dev->dmasound.line1);
L
Linus Torvalds 已提交
753 754
		/* fall throuth */
	case MIXER_READ(SOUND_MIXER_LINE1):
755
		return put_user(dev->dmasound.line1 | dev->dmasound.line1 << 8, p);
L
Linus Torvalds 已提交
756 757 758 759 760 761

	case MIXER_WRITE(SOUND_MIXER_LINE2):
		if (get_user(val, p))
			return -EFAULT;
		val &= 0xff;
		val = (val <= 50) ? 50 : 100;
762 763
		dev->dmasound.line2 = val;
		mixer_level(dev,LINE2,dev->dmasound.line2);
L
Linus Torvalds 已提交
764 765
		/* fall throuth */
	case MIXER_READ(SOUND_MIXER_LINE2):
766
		return put_user(dev->dmasound.line2 | dev->dmasound.line2 << 8, p);
L
Linus Torvalds 已提交
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782

	default:
		return -EINVAL;
	}
}

struct file_operations saa7134_mixer_fops = {
	.owner   = THIS_MODULE,
	.open    = mixer_open,
	.release = mixer_release,
	.ioctl   = mixer_ioctl,
	.llseek  = no_llseek,
};

/* ------------------------------------------------------------------ */

783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809
static irqreturn_t saa7134_oss_irq(int irq, void *dev_id, struct pt_regs *regs)
{
        struct saa7134_dmasound *dmasound = dev_id;
        struct saa7134_dev *dev = dmasound->priv_data;
        unsigned long report, status;
        int loop, handled = 0;

        for (loop = 0; loop < 10; loop++) {
                report = saa_readl(SAA7134_IRQ_REPORT);
                status = saa_readl(SAA7134_IRQ_STATUS);

                if (report & SAA7134_IRQ_REPORT_DONE_RA3) {
                        handled = 1;
                        saa_writel(SAA7134_IRQ_REPORT,report);
                        saa7134_irq_oss_done(dev, status);
                } else {
                        goto out;
                }
        }

        if (loop == 10) {
                dprintk("error! looping IRQ!");
        }
out:
        return IRQ_RETVAL(handled);
}

L
Linus Torvalds 已提交
810 811
int saa7134_oss_init1(struct saa7134_dev *dev)
{
812 813 814 815 816 817

        if ((request_irq(dev->pci->irq, saa7134_oss_irq,
                         SA_SHIRQ | SA_INTERRUPT, dev->name,
			(void*) &dev->dmasound)) < 0)
		return -1;

L
Linus Torvalds 已提交
818
	/* general */
819 820
	init_MUTEX(&dev->dmasound.lock);
	init_waitqueue_head(&dev->dmasound.wq);
L
Linus Torvalds 已提交
821 822 823 824 825 826 827 828 829 830 831

	switch (dev->pci->device) {
	case PCI_DEVICE_ID_PHILIPS_SAA7133:
	case PCI_DEVICE_ID_PHILIPS_SAA7135:
		saa_writel(0x588 >> 2, 0x00000fff);
		saa_writel(0x58c >> 2, 0x00543210);
		saa_dsp_writel(dev, 0x46c >> 2, 0xbbbbbb);
		break;
	}

	/* dsp */
832
	dev->dmasound.rate = 32000;
833 834
	if (rate)
		dev->dmasound.rate = rate;
835
	dev->dmasound.rate = (dev->dmasound.rate > 40000) ? 48000 : 32000;
L
Linus Torvalds 已提交
836 837

	/* mixer */
838 839 840 841 842
	dev->dmasound.line1 = 50;
	dev->dmasound.line2 = 50;
	mixer_level(dev,LINE1,dev->dmasound.line1);
	mixer_level(dev,LINE2,dev->dmasound.line2);
	mixer_recsrc(dev, (dev->dmasound.rate == 32000) ? TV : LINE2);
L
Linus Torvalds 已提交
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857

	return 0;
}

int saa7134_oss_fini(struct saa7134_dev *dev)
{
	/* nothing */
	return 0;
}

void saa7134_irq_oss_done(struct saa7134_dev *dev, unsigned long status)
{
	int next_blk, reg = 0;

	spin_lock(&dev->slock);
858
	if (UNSET == dev->dmasound.dma_blk) {
L
Linus Torvalds 已提交
859 860 861 862 863 864 865
		dprintk("irq: recording stopped\n");
		goto done;
	}
	if (0 != (status & 0x0f000000))
		dprintk("irq: lost %ld\n", (status >> 24) & 0x0f);
	if (0 == (status & 0x10000000)) {
		/* odd */
866
		if (0 == (dev->dmasound.dma_blk & 0x01))
L
Linus Torvalds 已提交
867 868 869
			reg = SAA7134_RS_BA1(6);
	} else {
		/* even */
870
		if (1 == (dev->dmasound.dma_blk & 0x01))
L
Linus Torvalds 已提交
871 872 873 874 875 876 877
			reg = SAA7134_RS_BA2(6);
	}
	if (0 == reg) {
		dprintk("irq: field oops [%s]\n",
			(status & 0x10000000) ? "even" : "odd");
		goto done;
	}
878 879 880
	if (dev->dmasound.read_count >= dev->dmasound.blksize * (dev->dmasound.blocks-2)) {
		dprintk("irq: overrun [full=%d/%d]\n",dev->dmasound.read_count,
			dev->dmasound.bufsize);
L
Linus Torvalds 已提交
881 882 883 884 885
		dsp_dma_stop(dev);
		goto done;
	}

	/* next block addr */
886 887
	next_blk = (dev->dmasound.dma_blk + 2) % dev->dmasound.blocks;
	saa_writel(reg,next_blk * dev->dmasound.blksize);
888
	if (debug > 2)
L
Linus Torvalds 已提交
889 890
		dprintk("irq: ok, %s, next_blk=%d, addr=%x\n",
			(status & 0x10000000) ? "even" : "odd ", next_blk,
891
			next_blk * dev->dmasound.blksize);
L
Linus Torvalds 已提交
892 893

	/* update status & wake waiting readers */
894 895 896
	dev->dmasound.dma_blk = (dev->dmasound.dma_blk + 1) % dev->dmasound.blocks;
	dev->dmasound.read_count += dev->dmasound.blksize;
	wake_up(&dev->dmasound.wq);
L
Linus Torvalds 已提交
897 898 899 900 901

 done:
	spin_unlock(&dev->slock);
}

902
static int saa7134_dsp_create(struct saa7134_dev *dev)
903 904 905
{
	int err;

906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921
	err = dev->dmasound.minor_dsp =
		register_sound_dsp(&saa7134_dsp_fops,
                			dsp_nr[dev->nr]);
	if (err < 0) {
		goto fail;
	}
	printk(KERN_INFO "%s: registered device dsp%d\n",
		dev->name,dev->dmasound.minor_dsp >> 4);

	err = dev->dmasound.minor_mixer =
		register_sound_mixer(&saa7134_mixer_fops,
					mixer_nr[dev->nr]);
	if (err < 0)
		goto fail;
	printk(KERN_INFO "%s: registered device mixer%d\n",
		dev->name,dev->dmasound.minor_mixer >> 4);
922 923 924 925 926 927 928 929 930 931

	return 0;

fail:
        unregister_sound_dsp(dev->dmasound.minor_dsp);
	return 0;


}

932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956
static int oss_device_init(struct saa7134_dev *dev)
{
	dev->dmasound.priv_data = dev;
	saa7134_oss_init1(dev);
	saa7134_dsp_create(dev);
	return 1;
}

static int oss_device_exit(struct saa7134_dev *dev)
{

	unregister_sound_mixer(dev->dmasound.minor_mixer);
	unregister_sound_dsp(dev->dmasound.minor_dsp);

	saa7134_oss_fini(dev);

	if (dev->pci->irq > 0) {
		synchronize_irq(dev->pci->irq);
		free_irq(dev->pci->irq,&dev->dmasound);
	}

	dev->dmasound.priv_data = NULL;
	return 1;
}

957 958 959 960 961 962 963 964 965 966
static int saa7134_oss_init(void)
{
        struct saa7134_dev *dev = NULL;
        struct list_head *list;

        printk(KERN_INFO "saa7134 OSS driver for DMA sound loaded\n");

        list_for_each(list,&saa7134_devlist) {
                dev = list_entry(list, struct saa7134_dev, devlist);
		if (dev->dmasound.priv_data == NULL) {
967
			oss_device_init(dev);
968 969 970 971 972 973 974 975 976
		} else {
                	printk(KERN_ERR "saa7134 OSS: DMA sound is being handled by ALSA, ignoring %s\n",dev->name);
			return -EBUSY;
		}
        }

        if (dev == NULL)
                printk(KERN_INFO "saa7134 OSS: no saa7134 cards found\n");

977 978 979
	dmasound_init = oss_device_init;
	dmasound_exit = oss_device_exit;

980 981 982 983
        return 0;

}

984
static void saa7134_oss_exit(void)
985 986 987 988 989 990 991 992 993 994 995
{
        struct saa7134_dev *dev = NULL;
        struct list_head *list;

        list_for_each(list,&saa7134_devlist) {
                dev = list_entry(list, struct saa7134_dev, devlist);

		/* Device isn't registered by OSS, probably ALSA's */
		if (!dev->dmasound.minor_dsp)
			continue;

996
		oss_device_exit(dev);
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008
        }

        printk(KERN_INFO "saa7134 OSS driver for DMA sound unloaded\n");

        return;
}

module_init(saa7134_oss_init);
module_exit(saa7134_oss_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");

L
Linus Torvalds 已提交
1009 1010 1011 1012 1013 1014
/* ----------------------------------------------------------- */
/*
 * Local variables:
 * c-basic-offset: 8
 * End:
 */