p16v.c 32.3 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
/*
 *  Copyright (c) by James Courtier-Dutton <James@superbug.demon.co.uk>
 *  Driver p16v chips
 *  Version: 0.22
 *
 *  FEATURES currently supported:
 *    Output fixed at S32_LE, 2 channel to hw:0,0
 *    Rates: 44.1, 48, 96, 192.
 *
 *  Changelog:
 *  0.8
 *    Use separate card based buffer for periods table.
 *  0.9
 *    Use 2 channel output streams instead of 8 channel.
 *       (8 channel output streams might be good for ASIO type output)
 *    Corrected speaker output, so Front -> Front etc.
 *  0.10
 *    Fixed missed interrupts.
 *  0.11
 *    Add Sound card model number and names.
 *    Add Analog volume controls.
 *  0.12
 *    Corrected playback interrupts. Now interrupt per period, instead of half period.
 *  0.13
 *    Use single trigger for multichannel.
 *  0.14
 *    Mic capture now works at fixed: S32_LE, 96000Hz, Stereo.
 *  0.15
 *    Force buffer_size / period_size == INTEGER.
 *  0.16
 *    Update p16v.c to work with changed alsa api.
 *  0.17
 *    Update p16v.c to work with changed alsa api. Removed boot_devs.
 *  0.18
 *    Merging with snd-emu10k1 driver.
 *  0.19
 *    One stereo channel at 24bit now works.
 *  0.20
 *    Added better register defines.
 *  0.21
 *    Integrated with snd-emu10k1 driver.
 *  0.22
 *    Removed #if 0 ... #endif
 *
 *
 *  BUGS:
 *    Some stability problems when unloading the snd-p16v kernel module.
 *    --
 *
 *  TODO:
 *    SPDIF out.
 *    Find out how to change capture sample rates. E.g. To record SPDIF at 48000Hz.
 *    Currently capture fixed at 48000Hz.
 *
 *    --
 *  GENERAL INFO:
 *    Model: SB0240
 *    P16V Chip: CA0151-DBS
 *    Audigy 2 Chip: CA0102-IAT
 *    AC97 Codec: STAC 9721
 *    ADC: Philips 1361T (Stereo 24bit)
 *    DAC: CS4382-K (8-channel, 24bit, 192Khz)
 *
 *  This code was initally based on code from ALSA's emu10k1x.c which is:
 *  Copyright (c) by Francisco Moraes <fmoraes@nc.rr.com>
 *
 *   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., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 */
#include <sound/driver.h>
#include <linux/delay.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/pci.h>
#include <linux/slab.h>
#include <linux/moduleparam.h>
#include <sound/core.h>
#include <sound/initval.h>
#include <sound/pcm.h>
#include <sound/ac97_codec.h>
#include <sound/info.h>
#include <sound/emu10k1.h>
#include "p16v.h"

#define SET_CHANNEL 0  /* Testing channel outputs 0=Front, 1=Center/LFE, 2=Unknown, 3=Rear */
#define PCM_FRONT_CHANNEL 0
#define PCM_REAR_CHANNEL 1
#define PCM_CENTER_LFE_CHANNEL 2
#define PCM_UNKNOWN_CHANNEL 3
#define CONTROL_FRONT_CHANNEL 0
#define CONTROL_REAR_CHANNEL 3
#define CONTROL_CENTER_LFE_CHANNEL 1
#define CONTROL_UNKNOWN_CHANNEL 2

/* Card IDs:
 * Class 0401: 1102:0004 (rev 04) Subsystem: 1102:2002 -> Audigy2 ZS 7.1 Model:SB0350
 * Class 0401: 1102:0004 (rev 04) Subsystem: 1102:1007 -> Audigy2 6.1    Model:SB0240
 * Class 0401: 1102:0004 (rev 04) Subsystem: 1102:1002 -> Audigy2 Platinum  Model:SB msb0240230009266
 * Class 0401: 1102:0004 (rev 04) Subsystem: 1102:2007 -> Audigy4 Pro Model:SB0380 M1SB0380472001901E
 *
 */

 /* hardware definition */
static snd_pcm_hardware_t snd_p16v_playback_hw = {
	.info =			(SNDRV_PCM_INFO_MMAP | 
				 SNDRV_PCM_INFO_INTERLEAVED |
				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
				 SNDRV_PCM_INFO_MMAP_VALID),
	.formats =		SNDRV_PCM_FMTBIT_S32_LE, /* Only supports 24-bit samples padded to 32 bits. */
122 123
	.rates =		SNDRV_PCM_RATE_192000 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_44100, 
	.rate_min =		44100,
L
Linus Torvalds 已提交
124 125 126 127 128 129 130 131 132 133 134
	.rate_max =		192000,
	.channels_min =		8, 
	.channels_max =		8,
	.buffer_bytes_max =	(32*1024),
	.period_bytes_min =	64,
	.period_bytes_max =	(16*1024),
	.periods_min =		2,
	.periods_max =		8,
	.fifo_size =		0,
};

135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
static snd_pcm_hardware_t snd_p16v_capture_hw = {
       .info =                 (SNDRV_PCM_INFO_MMAP |
                                SNDRV_PCM_INFO_INTERLEAVED |
                                SNDRV_PCM_INFO_BLOCK_TRANSFER |
                                SNDRV_PCM_INFO_MMAP_VALID),
       .formats =              SNDRV_PCM_FMTBIT_S32_LE,
       .rates =                SNDRV_PCM_RATE_48000,
       .rate_min =             48000,
       .rate_max =             48000,
       .channels_min =         2,
       .channels_max =         2,
       .buffer_bytes_max =     (32*1024),
       .period_bytes_min =     64,
       .period_bytes_max =     (16*1024),
       .periods_min =          2,
       .periods_max =          2,
       .fifo_size =            0,
};


L
Linus Torvalds 已提交
155 156
static void snd_p16v_pcm_free_substream(snd_pcm_runtime_t *runtime)
{
157
	emu10k1_pcm_t *epcm = runtime->private_data;
L
Linus Torvalds 已提交
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
  
	if (epcm) {
        	//snd_printk("epcm free: %p\n", epcm);
		kfree(epcm);
	}
}

/* open_playback callback */
static int snd_p16v_pcm_open_playback_channel(snd_pcm_substream_t *substream, int channel_id)
{
	emu10k1_t *emu = snd_pcm_substream_chip(substream);
        emu10k1_voice_t *channel = &(emu->p16v_voices[channel_id]);
	emu10k1_pcm_t *epcm;
	snd_pcm_runtime_t *runtime = substream->runtime;
	int err;

	epcm = kcalloc(1, sizeof(*epcm), GFP_KERNEL);
        //snd_printk("epcm kcalloc: %p\n", epcm);

	if (epcm == NULL)
		return -ENOMEM;
	epcm->emu = emu;
	epcm->substream = substream;
        //snd_printk("epcm device=%d, channel_id=%d\n", substream->pcm->device, channel_id);
  
	runtime->private_data = epcm;
	runtime->private_free = snd_p16v_pcm_free_substream;
  
	runtime->hw = snd_p16v_playback_hw;

        channel->emu = emu;
        channel->number = channel_id;

        channel->use=1;
	//snd_printk("p16v: open channel_id=%d, channel=%p, use=0x%x\n", channel_id, channel, channel->use);
        //printk("open:channel_id=%d, chip=%p, channel=%p\n",channel_id, chip, channel);
        //channel->interrupt = snd_p16v_pcm_channel_interrupt;
        channel->epcm=epcm;
	if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
                return err;

	return 0;
}
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
/* open_capture callback */
static int snd_p16v_pcm_open_capture_channel(snd_pcm_substream_t *substream, int channel_id)
{
	emu10k1_t *emu = snd_pcm_substream_chip(substream);
	emu10k1_voice_t *channel = &(emu->p16v_capture_voice);
	emu10k1_pcm_t *epcm;
	snd_pcm_runtime_t *runtime = substream->runtime;
	int err;

	epcm = kcalloc(1, sizeof(*epcm), GFP_KERNEL);
	//snd_printk("epcm kcalloc: %p\n", epcm);

	if (epcm == NULL)
		return -ENOMEM;
	epcm->emu = emu;
	epcm->substream = substream;
	//snd_printk("epcm device=%d, channel_id=%d\n", substream->pcm->device, channel_id);

	runtime->private_data = epcm;
	runtime->private_free = snd_p16v_pcm_free_substream;
  
	runtime->hw = snd_p16v_capture_hw;

	channel->emu = emu;
	channel->number = channel_id;

	channel->use=1;
	//snd_printk("p16v: open channel_id=%d, channel=%p, use=0x%x\n", channel_id, channel, channel->use);
	//printk("open:channel_id=%d, chip=%p, channel=%p\n",channel_id, chip, channel);
	//channel->interrupt = snd_p16v_pcm_channel_interrupt;
	channel->epcm=epcm;
	if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
		return err;

	return 0;
}

L
Linus Torvalds 已提交
238 239 240 241 242 243

/* close callback */
static int snd_p16v_pcm_close_playback(snd_pcm_substream_t *substream)
{
	emu10k1_t *emu = snd_pcm_substream_chip(substream);
	//snd_pcm_runtime_t *runtime = substream->runtime;
244 245 246 247 248 249 250 251 252 253 254 255 256 257
	//emu10k1_pcm_t *epcm = runtime->private_data;
	emu->p16v_voices[substream->pcm->device - emu->p16v_device_offset].use=0;
	/* FIXME: maybe zero others */
	return 0;
}

/* close callback */
static int snd_p16v_pcm_close_capture(snd_pcm_substream_t *substream)
{
	emu10k1_t *emu = snd_pcm_substream_chip(substream);
	//snd_pcm_runtime_t *runtime = substream->runtime;
	//emu10k1_pcm_t *epcm = runtime->private_data;
	emu->p16v_capture_voice.use=0;
	/* FIXME: maybe zero others */
L
Linus Torvalds 已提交
258 259 260 261 262 263 264 265
	return 0;
}

static int snd_p16v_pcm_open_playback_front(snd_pcm_substream_t *substream)
{
	return snd_p16v_pcm_open_playback_channel(substream, PCM_FRONT_CHANNEL);
}

266 267 268 269 270 271
static int snd_p16v_pcm_open_capture(snd_pcm_substream_t *substream)
{
	// Only using channel 0 for now, but the card has 2 channels.
	return snd_p16v_pcm_open_capture_channel(substream, 0);
}

L
Linus Torvalds 已提交
272 273 274 275 276 277 278 279 280 281
/* hw_params callback */
static int snd_p16v_pcm_hw_params_playback(snd_pcm_substream_t *substream,
				      snd_pcm_hw_params_t * hw_params)
{
	int result;
	result = snd_pcm_lib_malloc_pages(substream,
					params_buffer_bytes(hw_params));
	return result;
}

282 283 284 285 286 287 288 289 290 291 292
/* hw_params callback */
static int snd_p16v_pcm_hw_params_capture(snd_pcm_substream_t *substream,
				      snd_pcm_hw_params_t * hw_params)
{
	int result;
	result = snd_pcm_lib_malloc_pages(substream,
					params_buffer_bytes(hw_params));
	return result;
}


L
Linus Torvalds 已提交
293 294 295 296 297 298 299 300
/* hw_free callback */
static int snd_p16v_pcm_hw_free_playback(snd_pcm_substream_t *substream)
{
	int result;
	result = snd_pcm_lib_free_pages(substream);
	return result;
}

301 302 303 304 305 306 307 308 309
/* hw_free callback */
static int snd_p16v_pcm_hw_free_capture(snd_pcm_substream_t *substream)
{
	int result;
	result = snd_pcm_lib_free_pages(substream);
	return result;
}


L
Linus Torvalds 已提交
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
/* prepare playback callback */
static int snd_p16v_pcm_prepare_playback(snd_pcm_substream_t *substream)
{
	emu10k1_t *emu = snd_pcm_substream_chip(substream);
	snd_pcm_runtime_t *runtime = substream->runtime;
	int channel = substream->pcm->device - emu->p16v_device_offset;
	u32 *table_base = (u32 *)(emu->p16v_buffer.area+(8*16*channel));
	u32 period_size_bytes = frames_to_bytes(runtime, runtime->period_size);
	int i;
	u32 tmp;
	
        //snd_printk("prepare:channel_number=%d, rate=%d, format=0x%x, channels=%d, buffer_size=%ld, period_size=%ld, periods=%u, frames_to_bytes=%d\n",channel, runtime->rate, runtime->format, runtime->channels, runtime->buffer_size, runtime->period_size, runtime->periods, frames_to_bytes(runtime, 1));
        //snd_printk("dma_addr=%x, dma_area=%p, table_base=%p\n",runtime->dma_addr, runtime->dma_area, table_base);
	//snd_printk("dma_addr=%x, dma_area=%p, dma_bytes(size)=%x\n",emu->p16v_buffer.addr, emu->p16v_buffer.area, emu->p16v_buffer.bytes);
	tmp = snd_emu10k1_ptr_read(emu, A_SPDIF_SAMPLERATE, channel);
        switch (runtime->rate) {
	case 44100:
327
	  snd_emu10k1_ptr_write(emu, A_SPDIF_SAMPLERATE, channel, (tmp & ~0xe0e0) | 0x8080);
L
Linus Torvalds 已提交
328 329
	  break;
	case 96000:
330
	  snd_emu10k1_ptr_write(emu, A_SPDIF_SAMPLERATE, channel, (tmp & ~0xe0e0) | 0x4040);
L
Linus Torvalds 已提交
331 332
	  break;
	case 192000:
333
	  snd_emu10k1_ptr_write(emu, A_SPDIF_SAMPLERATE, channel, (tmp & ~0xe0e0) | 0x2020);
L
Linus Torvalds 已提交
334
	  break;
335
	case 48000:
L
Linus Torvalds 已提交
336
	default:
337
	  snd_emu10k1_ptr_write(emu, A_SPDIF_SAMPLERATE, channel, (tmp & ~0xe0e0) | 0x0000);
L
Linus Torvalds 已提交
338 339 340
	  break;
	}
	/* FIXME: Check emu->buffer.size before actually writing to it. */
341
	for(i=0; i < runtime->periods; i++) {
L
Linus Torvalds 已提交
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
		table_base[i*2]=runtime->dma_addr+(i*period_size_bytes);
		table_base[(i*2)+1]=period_size_bytes<<16;
	}
 
	snd_emu10k1_ptr20_write(emu, PLAYBACK_LIST_ADDR, channel, emu->p16v_buffer.addr+(8*16*channel));
	snd_emu10k1_ptr20_write(emu, PLAYBACK_LIST_SIZE, channel, (runtime->periods - 1) << 19);
	snd_emu10k1_ptr20_write(emu, PLAYBACK_LIST_PTR, channel, 0);
	snd_emu10k1_ptr20_write(emu, PLAYBACK_DMA_ADDR, channel, runtime->dma_addr);
	snd_emu10k1_ptr20_write(emu, PLAYBACK_PERIOD_SIZE, channel, frames_to_bytes(runtime, runtime->period_size)<<16); // buffer size in bytes
	snd_emu10k1_ptr20_write(emu, PLAYBACK_POINTER, channel, 0);
	snd_emu10k1_ptr20_write(emu, 0x07, channel, 0x0);
	snd_emu10k1_ptr20_write(emu, 0x08, channel, 0);

	return 0;
}

358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
/* prepare capture callback */
static int snd_p16v_pcm_prepare_capture(snd_pcm_substream_t *substream)
{
	emu10k1_t *emu = snd_pcm_substream_chip(substream);
	snd_pcm_runtime_t *runtime = substream->runtime;
	int channel = substream->pcm->device - emu->p16v_device_offset;
	//printk("prepare:channel_number=%d, rate=%d, format=0x%x, channels=%d, buffer_size=%ld, period_size=%ld, frames_to_bytes=%d\n",channel, runtime->rate, runtime->format, runtime->channels, runtime->buffer_size, runtime->period_size,  frames_to_bytes(runtime, 1));
	snd_emu10k1_ptr20_write(emu, 0x13, channel, 0);
	snd_emu10k1_ptr20_write(emu, CAPTURE_DMA_ADDR, channel, runtime->dma_addr);
	snd_emu10k1_ptr20_write(emu, CAPTURE_BUFFER_SIZE, channel, frames_to_bytes(runtime, runtime->buffer_size)<<16); // buffer size in bytes
	snd_emu10k1_ptr20_write(emu, CAPTURE_POINTER, channel, 0);
	//snd_emu10k1_ptr20_write(emu, CAPTURE_SOURCE, 0x0, 0x333300e4); /* Select MIC or Line in */
	//snd_emu10k1_ptr20_write(emu, EXTENDED_INT_MASK, 0, snd_emu10k1_ptr20_read(emu, EXTENDED_INT_MASK, 0) | (0x110000<<channel));

	return 0;
}

L
Linus Torvalds 已提交
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
static void snd_p16v_intr_enable(emu10k1_t *emu, unsigned int intrenb)
{
	unsigned long flags;
	unsigned int enable;

	spin_lock_irqsave(&emu->emu_lock, flags);
	enable = inl(emu->port + INTE2) | intrenb;
	outl(enable, emu->port + INTE2);
	spin_unlock_irqrestore(&emu->emu_lock, flags);
}

static void snd_p16v_intr_disable(emu10k1_t *emu, unsigned int intrenb)
{
	unsigned long flags;
	unsigned int disable;

	spin_lock_irqsave(&emu->emu_lock, flags);
	disable = inl(emu->port + INTE2) & (~intrenb);
	outl(disable, emu->port + INTE2);
	spin_unlock_irqrestore(&emu->emu_lock, flags);
}

/* trigger_playback callback */
static int snd_p16v_pcm_trigger_playback(snd_pcm_substream_t *substream,
				    int cmd)
{
	emu10k1_t *emu = snd_pcm_substream_chip(substream);
	snd_pcm_runtime_t *runtime;
	emu10k1_pcm_t *epcm;
	int channel;
	int result = 0;
	struct list_head *pos;
        snd_pcm_substream_t *s;
	u32 basic = 0;
	u32 inte = 0;
	int running=0;

	switch (cmd) {
	case SNDRV_PCM_TRIGGER_START:
		running=1;
		break;
	case SNDRV_PCM_TRIGGER_STOP:
	default:
		running=0;
		break;
	}
        snd_pcm_group_for_each(pos, substream) {
                s = snd_pcm_group_substream_entry(pos);
		runtime = s->runtime;
		epcm = runtime->private_data;
		channel = substream->pcm->device-emu->p16v_device_offset;
		//snd_printk("p16v channel=%d\n",channel);
		epcm->running = running;
		basic |= (0x1<<channel);
		inte |= (INTE2_PLAYBACK_CH_0_LOOP<<channel);
                snd_pcm_trigger_done(s, substream);
        }
	//snd_printk("basic=0x%x, inte=0x%x\n",basic, inte);

	switch (cmd) {
	case SNDRV_PCM_TRIGGER_START:
		snd_p16v_intr_enable(emu, inte);
		snd_emu10k1_ptr20_write(emu, BASIC_INTERRUPT, 0, snd_emu10k1_ptr20_read(emu, BASIC_INTERRUPT, 0)| (basic));
		break;
	case SNDRV_PCM_TRIGGER_STOP:
		snd_emu10k1_ptr20_write(emu, BASIC_INTERRUPT, 0, snd_emu10k1_ptr20_read(emu, BASIC_INTERRUPT, 0) & ~(basic));
		snd_p16v_intr_disable(emu, inte);
		break;
	default:
		result = -EINVAL;
		break;
	}
	return result;
}

450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
/* trigger_capture callback */
static int snd_p16v_pcm_trigger_capture(snd_pcm_substream_t *substream,
                                   int cmd)
{
	emu10k1_t *emu = snd_pcm_substream_chip(substream);
	snd_pcm_runtime_t *runtime = substream->runtime;
	emu10k1_pcm_t *epcm = runtime->private_data;
	int channel = 0;
	int result = 0;
	u32 inte = INTE2_CAPTURE_CH_0_LOOP | INTE2_CAPTURE_CH_0_HALF_LOOP;

	switch (cmd) {
	case SNDRV_PCM_TRIGGER_START:
		snd_p16v_intr_enable(emu, inte);
		snd_emu10k1_ptr20_write(emu, BASIC_INTERRUPT, 0, snd_emu10k1_ptr20_read(emu, BASIC_INTERRUPT, 0)|(0x100<<channel));
		epcm->running = 1;
		break;
	case SNDRV_PCM_TRIGGER_STOP:
		snd_emu10k1_ptr20_write(emu, BASIC_INTERRUPT, 0, snd_emu10k1_ptr20_read(emu, BASIC_INTERRUPT, 0) & ~(0x100<<channel));
		snd_p16v_intr_disable(emu, inte);
		//snd_emu10k1_ptr20_write(emu, EXTENDED_INT_MASK, 0, snd_emu10k1_ptr20_read(emu, EXTENDED_INT_MASK, 0) & ~(0x110000<<channel));
		epcm->running = 0;
		break;
	default:
		result = -EINVAL;
		break;
	}
	return result;
}

L
Linus Torvalds 已提交
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
/* pointer_playback callback */
static snd_pcm_uframes_t
snd_p16v_pcm_pointer_playback(snd_pcm_substream_t *substream)
{
	emu10k1_t *emu = snd_pcm_substream_chip(substream);
	snd_pcm_runtime_t *runtime = substream->runtime;
	emu10k1_pcm_t *epcm = runtime->private_data;
	snd_pcm_uframes_t ptr, ptr1, ptr2,ptr3,ptr4 = 0;
	int channel = substream->pcm->device - emu->p16v_device_offset;
	if (!epcm->running)
		return 0;

	ptr3 = snd_emu10k1_ptr20_read(emu, PLAYBACK_LIST_PTR, channel);
	ptr1 = snd_emu10k1_ptr20_read(emu, PLAYBACK_POINTER, channel);
	ptr4 = snd_emu10k1_ptr20_read(emu, PLAYBACK_LIST_PTR, channel);
	if (ptr3 != ptr4) ptr1 = snd_emu10k1_ptr20_read(emu, PLAYBACK_POINTER, channel);
	ptr2 = bytes_to_frames(runtime, ptr1);
	ptr2+= (ptr4 >> 3) * runtime->period_size;
	ptr=ptr2;
        if (ptr >= runtime->buffer_size)
		ptr -= runtime->buffer_size;

	return ptr;
}

505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
/* pointer_capture callback */
static snd_pcm_uframes_t
snd_p16v_pcm_pointer_capture(snd_pcm_substream_t *substream)
{
	emu10k1_t *emu = snd_pcm_substream_chip(substream);
	snd_pcm_runtime_t *runtime = substream->runtime;
	emu10k1_pcm_t *epcm = runtime->private_data;
	snd_pcm_uframes_t ptr, ptr1, ptr2 = 0;
	int channel = 0;

	if (!epcm->running)
		return 0;

	ptr1 = snd_emu10k1_ptr20_read(emu, CAPTURE_POINTER, channel);
	ptr2 = bytes_to_frames(runtime, ptr1);
	ptr=ptr2;
	if (ptr >= runtime->buffer_size) {
		ptr -= runtime->buffer_size;
		printk("buffer capture limited!\n");
	}
	//printk("ptr1 = 0x%lx, ptr2=0x%lx, ptr=0x%lx, buffer_size = 0x%x, period_size = 0x%x, bits=%d, rate=%d\n", ptr1, ptr2, ptr, (int)runtime->buffer_size, (int)runtime->period_size, (int)runtime->frame_bits, (int)runtime->rate);

	return ptr;
}

L
Linus Torvalds 已提交
530 531 532 533 534 535 536 537 538 539 540 541
/* operators */
static snd_pcm_ops_t snd_p16v_playback_front_ops = {
	.open =        snd_p16v_pcm_open_playback_front,
	.close =       snd_p16v_pcm_close_playback,
	.ioctl =       snd_pcm_lib_ioctl,
	.hw_params =   snd_p16v_pcm_hw_params_playback,
	.hw_free =     snd_p16v_pcm_hw_free_playback,
	.prepare =     snd_p16v_pcm_prepare_playback,
	.trigger =     snd_p16v_pcm_trigger_playback,
	.pointer =     snd_p16v_pcm_pointer_playback,
};

542 543 544 545 546 547 548 549 550 551 552 553
static snd_pcm_ops_t snd_p16v_capture_ops = {
	.open =        snd_p16v_pcm_open_capture,
	.close =       snd_p16v_pcm_close_capture,
	.ioctl =       snd_pcm_lib_ioctl,
	.hw_params =   snd_p16v_pcm_hw_params_capture,
	.hw_free =     snd_p16v_pcm_hw_free_capture,
	.prepare =     snd_p16v_pcm_prepare_capture,
	.trigger =     snd_p16v_pcm_trigger_capture,
	.pointer =     snd_p16v_pcm_pointer_capture,
};


L
Linus Torvalds 已提交
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
int snd_p16v_free(emu10k1_t *chip)
{
	// release the data
	if (chip->p16v_buffer.area) {
		snd_dma_free_pages(&chip->p16v_buffer);
		//snd_printk("period lables free: %p\n", &chip->p16v_buffer);
	}
	return 0;
}

static void snd_p16v_pcm_free(snd_pcm_t *pcm)
{
	emu10k1_t *emu = pcm->private_data;
	//snd_printk("snd_p16v_pcm_free pcm: called\n");
	snd_pcm_lib_preallocate_free_for_all(pcm);
	emu->pcm = NULL;
}

int snd_p16v_pcm(emu10k1_t *emu, int device, snd_pcm_t **rpcm)
{
	snd_pcm_t *pcm;
	snd_pcm_substream_t *substream;
	int err;
577
        int capture=1;
L
Linus Torvalds 已提交
578 579 580 581 582
  
	//snd_printk("snd_p16v_pcm called. device=%d\n", device);
	emu->p16v_device_offset = device;
	if (rpcm)
		*rpcm = NULL;
583

L
Linus Torvalds 已提交
584 585 586 587 588
	if ((err = snd_pcm_new(emu->card, "p16v", device, 1, capture, &pcm)) < 0)
		return err;
  
	pcm->private_data = emu;
	pcm->private_free = snd_p16v_pcm_free;
589 590
	// Single playback 8 channel device.
	// Single capture 2 channel device.
L
Linus Torvalds 已提交
591
	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_p16v_playback_front_ops);
592
	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_p16v_capture_ops);
L
Linus Torvalds 已提交
593 594 595 596 597 598 599 600 601 602 603 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 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 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 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867

	pcm->info_flags = 0;
	pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
	strcpy(pcm->name, "p16v");
	emu->pcm = pcm;

	for(substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; 
	    substream; 
	    substream = substream->next) {
		if ((err = snd_pcm_lib_preallocate_pages(substream, 
							 SNDRV_DMA_TYPE_DEV, 
							 snd_dma_pci_data(emu->pci), 
							 64*1024, 64*1024)) < 0) /* FIXME: 32*1024 for sound buffer, between 32and64 for Periods table. */
			return err;
		//snd_printk("preallocate playback substream: err=%d\n", err);
	}

	for (substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream; 
	      substream; 
	      substream = substream->next) {
 		if ((err = snd_pcm_lib_preallocate_pages(substream, 
	                                           SNDRV_DMA_TYPE_DEV, 
	                                           snd_dma_pci_data(emu->pci), 
	                                           64*1024, 64*1024)) < 0)
			return err;
		//snd_printk("preallocate capture substream: err=%d\n", err);
	}
  
	if (rpcm)
		*rpcm = pcm;
  
	return 0;
}

static int snd_p16v_volume_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
{
        uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
        uinfo->count = 2;
        uinfo->value.integer.min = 0;
        uinfo->value.integer.max = 255;
        return 0;
}

static int snd_p16v_volume_get(snd_kcontrol_t * kcontrol,
                                       snd_ctl_elem_value_t * ucontrol, int reg, int high_low)
{
        emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
        u32 value;

        value = snd_emu10k1_ptr20_read(emu, reg, high_low);
	if (high_low == 1) {
        	ucontrol->value.integer.value[0] = 0xff - ((value >> 24) & 0xff); /* Left */
        	ucontrol->value.integer.value[1] = 0xff - ((value >> 16) & 0xff); /* Right */
	} else {
        	ucontrol->value.integer.value[0] = 0xff - ((value >> 8) & 0xff); /* Left */
        	ucontrol->value.integer.value[1] = 0xff - ((value >> 0) & 0xff); /* Right */
	}
        return 0;
}

static int snd_p16v_volume_get_spdif_front(snd_kcontrol_t * kcontrol,
                                       snd_ctl_elem_value_t * ucontrol)
{
	int high_low = 0;
	int reg = PLAYBACK_VOLUME_MIXER7;
        return snd_p16v_volume_get(kcontrol, ucontrol, reg, high_low);
}

static int snd_p16v_volume_get_spdif_center_lfe(snd_kcontrol_t * kcontrol,
                                       snd_ctl_elem_value_t * ucontrol)
{
	int high_low = 1;
	int reg = PLAYBACK_VOLUME_MIXER7;
        return snd_p16v_volume_get(kcontrol, ucontrol, reg, high_low);
}
static int snd_p16v_volume_get_spdif_unknown(snd_kcontrol_t * kcontrol,
                                       snd_ctl_elem_value_t * ucontrol)
{
	int high_low = 0;
	int reg = PLAYBACK_VOLUME_MIXER8;
        return snd_p16v_volume_get(kcontrol, ucontrol, reg, high_low);
}
static int snd_p16v_volume_get_spdif_rear(snd_kcontrol_t * kcontrol,
                                       snd_ctl_elem_value_t * ucontrol)
{
	int high_low = 1;
	int reg = PLAYBACK_VOLUME_MIXER8;
        return snd_p16v_volume_get(kcontrol, ucontrol, reg, high_low);
}

static int snd_p16v_volume_get_analog_front(snd_kcontrol_t * kcontrol,
                                       snd_ctl_elem_value_t * ucontrol)
{
	int high_low = 0;
	int reg = PLAYBACK_VOLUME_MIXER9;
        return snd_p16v_volume_get(kcontrol, ucontrol, reg, high_low);
}

static int snd_p16v_volume_get_analog_center_lfe(snd_kcontrol_t * kcontrol,
                                       snd_ctl_elem_value_t * ucontrol)
{
	int high_low = 1;
	int reg = PLAYBACK_VOLUME_MIXER9;
        return snd_p16v_volume_get(kcontrol, ucontrol, reg, high_low);
}
static int snd_p16v_volume_get_analog_rear(snd_kcontrol_t * kcontrol,
                                       snd_ctl_elem_value_t * ucontrol)
{
	int high_low = 1;
	int reg = PLAYBACK_VOLUME_MIXER10;
        return snd_p16v_volume_get(kcontrol, ucontrol, reg, high_low);
}

static int snd_p16v_volume_get_analog_unknown(snd_kcontrol_t * kcontrol,
                                       snd_ctl_elem_value_t * ucontrol)
{
	int high_low = 0;
	int reg = PLAYBACK_VOLUME_MIXER10;
        return snd_p16v_volume_get(kcontrol, ucontrol, reg, high_low);
}

static int snd_p16v_volume_put(snd_kcontrol_t * kcontrol,
                                       snd_ctl_elem_value_t * ucontrol, int reg, int high_low)
{
        emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
        u32 value;
        value = snd_emu10k1_ptr20_read(emu, reg, 0);
        //value = value & 0xffff;
	if (high_low == 1) {
		value &= 0xffff;
	        value = value | ((0xff - ucontrol->value.integer.value[0]) << 24) | ((0xff - ucontrol->value.integer.value[1]) << 16);
	} else {
		value &= 0xffff0000;
        	value = value | ((0xff - ucontrol->value.integer.value[0]) << 8) | ((0xff - ucontrol->value.integer.value[1]) );
	}
        	snd_emu10k1_ptr20_write(emu, reg, 0, value);
        return 1;
}

static int snd_p16v_volume_put_spdif_front(snd_kcontrol_t * kcontrol,
                                       snd_ctl_elem_value_t * ucontrol)
{
	int high_low = 0;
	int reg = PLAYBACK_VOLUME_MIXER7;
        return snd_p16v_volume_put(kcontrol, ucontrol, reg, high_low);
}

static int snd_p16v_volume_put_spdif_center_lfe(snd_kcontrol_t * kcontrol,
                                       snd_ctl_elem_value_t * ucontrol)
{
	int high_low = 1;
	int reg = PLAYBACK_VOLUME_MIXER7;
        return snd_p16v_volume_put(kcontrol, ucontrol, reg, high_low);
}

static int snd_p16v_volume_put_spdif_unknown(snd_kcontrol_t * kcontrol,
                                       snd_ctl_elem_value_t * ucontrol)
{
	int high_low = 0;
	int reg = PLAYBACK_VOLUME_MIXER8;
        return snd_p16v_volume_put(kcontrol, ucontrol, reg, high_low);
}

static int snd_p16v_volume_put_spdif_rear(snd_kcontrol_t * kcontrol,
                                       snd_ctl_elem_value_t * ucontrol)
{
	int high_low = 1;
	int reg = PLAYBACK_VOLUME_MIXER8;
        return snd_p16v_volume_put(kcontrol, ucontrol, reg, high_low);
}

static int snd_p16v_volume_put_analog_front(snd_kcontrol_t * kcontrol,
                                       snd_ctl_elem_value_t * ucontrol)
{
	int high_low = 0;
	int reg = PLAYBACK_VOLUME_MIXER9;
        return snd_p16v_volume_put(kcontrol, ucontrol, reg, high_low);
}

static int snd_p16v_volume_put_analog_center_lfe(snd_kcontrol_t * kcontrol,
                                       snd_ctl_elem_value_t * ucontrol)
{
	int high_low = 1;
	int reg = PLAYBACK_VOLUME_MIXER9;
        return snd_p16v_volume_put(kcontrol, ucontrol, reg, high_low);
}

static int snd_p16v_volume_put_analog_rear(snd_kcontrol_t * kcontrol,
                                       snd_ctl_elem_value_t * ucontrol)
{
	int high_low = 1;
	int reg = PLAYBACK_VOLUME_MIXER10;
        return snd_p16v_volume_put(kcontrol, ucontrol, reg, high_low);
}

static int snd_p16v_volume_put_analog_unknown(snd_kcontrol_t * kcontrol,
                                       snd_ctl_elem_value_t * ucontrol)
{
	int high_low = 0;
	int reg = PLAYBACK_VOLUME_MIXER10;
        return snd_p16v_volume_put(kcontrol, ucontrol, reg, high_low);
}

static snd_kcontrol_new_t snd_p16v_volume_control_analog_front =
{
        .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
        .name =         "HD Analog Front Volume",
        .info =         snd_p16v_volume_info,
        .get =          snd_p16v_volume_get_analog_front,
        .put =          snd_p16v_volume_put_analog_front
};

static snd_kcontrol_new_t snd_p16v_volume_control_analog_center_lfe =
{
        .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
        .name =         "HD Analog Center/LFE Volume",
        .info =         snd_p16v_volume_info,
        .get =          snd_p16v_volume_get_analog_center_lfe,
        .put =          snd_p16v_volume_put_analog_center_lfe
};

static snd_kcontrol_new_t snd_p16v_volume_control_analog_unknown =
{
        .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
        .name =         "HD Analog Unknown Volume",
        .info =         snd_p16v_volume_info,
        .get =          snd_p16v_volume_get_analog_unknown,
        .put =          snd_p16v_volume_put_analog_unknown
};

static snd_kcontrol_new_t snd_p16v_volume_control_analog_rear =
{
        .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
        .name =         "HD Analog Rear Volume",
        .info =         snd_p16v_volume_info,
        .get =          snd_p16v_volume_get_analog_rear,
        .put =          snd_p16v_volume_put_analog_rear
};

static snd_kcontrol_new_t snd_p16v_volume_control_spdif_front =
{
        .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
        .name =         "HD SPDIF Front Volume",
        .info =         snd_p16v_volume_info,
        .get =          snd_p16v_volume_get_spdif_front,
        .put =          snd_p16v_volume_put_spdif_front
};

static snd_kcontrol_new_t snd_p16v_volume_control_spdif_center_lfe =
{
        .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
        .name =         "HD SPDIF Center/LFE Volume",
        .info =         snd_p16v_volume_info,
        .get =          snd_p16v_volume_get_spdif_center_lfe,
        .put =          snd_p16v_volume_put_spdif_center_lfe
};

static snd_kcontrol_new_t snd_p16v_volume_control_spdif_unknown =
{
        .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
        .name =         "HD SPDIF Unknown Volume",
        .info =         snd_p16v_volume_info,
        .get =          snd_p16v_volume_get_spdif_unknown,
        .put =          snd_p16v_volume_put_spdif_unknown
};

static snd_kcontrol_new_t snd_p16v_volume_control_spdif_rear =
{
        .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
        .name =         "HD SPDIF Rear Volume",
        .info =         snd_p16v_volume_info,
        .get =          snd_p16v_volume_get_spdif_rear,
        .put =          snd_p16v_volume_put_spdif_rear
};

868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917
static int snd_p16v_capture_source_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
{
	static char *texts[8] = { "SPDIF", "I2S", "SRC48", "SRCMulti_SPDIF", "SRCMulti_I2S", "CDIF", "FX", "AC97" };

	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
	uinfo->count = 1;
	uinfo->value.enumerated.items = 8;
	if (uinfo->value.enumerated.item > 7)
                uinfo->value.enumerated.item = 7;
	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
	return 0;
}

static int snd_p16v_capture_source_get(snd_kcontrol_t * kcontrol,
					snd_ctl_elem_value_t * ucontrol)
{
	emu10k1_t *emu = snd_kcontrol_chip(kcontrol);

	ucontrol->value.enumerated.item[0] = emu->p16v_capture_source;
	return 0;
}

static int snd_p16v_capture_source_put(snd_kcontrol_t * kcontrol,
					snd_ctl_elem_value_t * ucontrol)
{
	emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
	unsigned int val;
	int change = 0;
	u32 mask;
	u32 source;

	val = ucontrol->value.enumerated.item[0] ;
	change = (emu->p16v_capture_source != val);
	if (change) {
		emu->p16v_capture_source = val;
		source = (val << 28) | (val << 24) | (val << 20) | (val << 16);
		mask = snd_emu10k1_ptr20_read(emu, BASIC_INTERRUPT, 0) & 0xffff;
		snd_emu10k1_ptr20_write(emu, BASIC_INTERRUPT, 0, source | mask);
	}
        return change;
}

static snd_kcontrol_new_t snd_p16v_capture_source __devinitdata =
{
	.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
	.name =		"HD Capture source",
	.info =		snd_p16v_capture_source_info,
	.get =		snd_p16v_capture_source_get,
	.put =		snd_p16v_capture_source_put
};
L
Linus Torvalds 已提交
918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954
int snd_p16v_mixer(emu10k1_t *emu)
{
        int err;
        snd_kcontrol_t *kctl;
        snd_card_t *card = emu->card;
        if ((kctl = snd_ctl_new1(&snd_p16v_volume_control_analog_front, emu)) == NULL)
                return -ENOMEM;
        if ((err = snd_ctl_add(card, kctl)))
                return err;
        if ((kctl = snd_ctl_new1(&snd_p16v_volume_control_analog_rear, emu)) == NULL)
                return -ENOMEM;
        if ((err = snd_ctl_add(card, kctl)))
                return err;
        if ((kctl = snd_ctl_new1(&snd_p16v_volume_control_analog_center_lfe, emu)) == NULL)
                return -ENOMEM;
        if ((err = snd_ctl_add(card, kctl)))
                return err;
        if ((kctl = snd_ctl_new1(&snd_p16v_volume_control_analog_unknown, emu)) == NULL)
                return -ENOMEM;
        if ((err = snd_ctl_add(card, kctl)))
                return err;
        if ((kctl = snd_ctl_new1(&snd_p16v_volume_control_spdif_front, emu)) == NULL)
                return -ENOMEM;
        if ((err = snd_ctl_add(card, kctl)))
                return err;
        if ((kctl = snd_ctl_new1(&snd_p16v_volume_control_spdif_rear, emu)) == NULL)
                return -ENOMEM;
        if ((err = snd_ctl_add(card, kctl)))
                return err;
        if ((kctl = snd_ctl_new1(&snd_p16v_volume_control_spdif_center_lfe, emu)) == NULL)
                return -ENOMEM;
        if ((err = snd_ctl_add(card, kctl)))
                return err;
        if ((kctl = snd_ctl_new1(&snd_p16v_volume_control_spdif_unknown, emu)) == NULL)
                return -ENOMEM;
        if ((err = snd_ctl_add(card, kctl)))
                return err;
955 956 957 958
        if ((kctl = snd_ctl_new1(&snd_p16v_capture_source, emu)) == NULL)
                return -ENOMEM;
        if ((err = snd_ctl_add(card, kctl)))
                return err;
L
Linus Torvalds 已提交
959 960 961
        return 0;
}