1/*
2 *  Copyright (c) by James Courtier-Dutton <James@superbug.demon.co.uk>
3 *  Driver p16v chips
4 *  Version: 0.25
5 *
6 *  FEATURES currently supported:
7 *    Output fixed at S32_LE, 2 channel to hw:0,0
8 *    Rates: 44.1, 48, 96, 192.
9 *
10 *  Changelog:
11 *  0.8
12 *    Use separate card based buffer for periods table.
13 *  0.9
14 *    Use 2 channel output streams instead of 8 channel.
15 *       (8 channel output streams might be good for ASIO type output)
16 *    Corrected speaker output, so Front -> Front etc.
17 *  0.10
18 *    Fixed missed interrupts.
19 *  0.11
20 *    Add Sound card model number and names.
21 *    Add Analog volume controls.
22 *  0.12
23 *    Corrected playback interrupts. Now interrupt per period, instead of half period.
24 *  0.13
25 *    Use single trigger for multichannel.
26 *  0.14
27 *    Mic capture now works at fixed: S32_LE, 96000Hz, Stereo.
28 *  0.15
29 *    Force buffer_size / period_size == INTEGER.
30 *  0.16
31 *    Update p16v.c to work with changed alsa api.
32 *  0.17
33 *    Update p16v.c to work with changed alsa api. Removed boot_devs.
34 *  0.18
35 *    Merging with snd-emu10k1 driver.
36 *  0.19
37 *    One stereo channel at 24bit now works.
38 *  0.20
39 *    Added better register defines.
40 *  0.21
41 *    Integrated with snd-emu10k1 driver.
42 *  0.22
43 *    Removed #if 0 ... #endif
44 *  0.23
45 *    Implement different capture rates.
46 *  0.24
47 *    Implement different capture source channels.
48 *    e.g. When HD Capture source is set to SPDIF,
49 *    setting HD Capture channel to 0 captures from CDROM digital input.
50 *    setting HD Capture channel to 1 captures from SPDIF in.
51 *  0.25
52 *    Include capture buffer sizes.
53 *
54 *  BUGS:
55 *    Some stability problems when unloading the snd-p16v kernel module.
56 *    --
57 *
58 *  TODO:
59 *    SPDIF out.
60 *    Find out how to change capture sample rates. E.g. To record SPDIF at 48000Hz.
61 *    Currently capture fixed at 48000Hz.
62 *
63 *    --
64 *  GENERAL INFO:
65 *    Model: SB0240
66 *    P16V Chip: CA0151-DBS
67 *    Audigy 2 Chip: CA0102-IAT
68 *    AC97 Codec: STAC 9721
69 *    ADC: Philips 1361T (Stereo 24bit)
70 *    DAC: CS4382-K (8-channel, 24bit, 192Khz)
71 *
72 *  This code was initally based on code from ALSA's emu10k1x.c which is:
73 *  Copyright (c) by Francisco Moraes <fmoraes@nc.rr.com>
74 *
75 *   This program is free software; you can redistribute it and/or modify
76 *   it under the terms of the GNU General Public License as published by
77 *   the Free Software Foundation; either version 2 of the License, or
78 *   (at your option) any later version.
79 *
80 *   This program is distributed in the hope that it will be useful,
81 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
82 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
83 *   GNU General Public License for more details.
84 *
85 *   You should have received a copy of the GNU General Public License
86 *   along with this program; if not, write to the Free Software
87 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
88 *
89 */
90#include <sound/driver.h>
91#include <linux/delay.h>
92#include <linux/init.h>
93#include <linux/interrupt.h>
94#include <linux/pci.h>
95#include <linux/slab.h>
96#include <linux/vmalloc.h>
97#include <linux/moduleparam.h>
98#include <sound/core.h>
99#include <sound/initval.h>
100#include <sound/pcm.h>
101#include <sound/ac97_codec.h>
102#include <sound/info.h>
103#include <sound/tlv.h>
104#include <sound/emu10k1.h>
105#include "p16v.h"
106
107#define SET_CHANNEL 0  /* Testing channel outputs 0=Front, 1=Center/LFE, 2=Unknown, 3=Rear */
108#define PCM_FRONT_CHANNEL 0
109#define PCM_REAR_CHANNEL 1
110#define PCM_CENTER_LFE_CHANNEL 2
111#define PCM_SIDE_CHANNEL 3
112#define CONTROL_FRONT_CHANNEL 0
113#define CONTROL_REAR_CHANNEL 3
114#define CONTROL_CENTER_LFE_CHANNEL 1
115#define CONTROL_SIDE_CHANNEL 2
116
117/* Card IDs:
118 * Class 0401: 1102:0004 (rev 04) Subsystem: 1102:2002 -> Audigy2 ZS 7.1 Model:SB0350
119 * Class 0401: 1102:0004 (rev 04) Subsystem: 1102:1007 -> Audigy2 6.1    Model:SB0240
120 * Class 0401: 1102:0004 (rev 04) Subsystem: 1102:1002 -> Audigy2 Platinum  Model:SB msb0240230009266
121 * Class 0401: 1102:0004 (rev 04) Subsystem: 1102:2007 -> Audigy4 Pro Model:SB0380 M1SB0380472001901E
122 *
123 */
124
125 /* hardware definition */
126static struct snd_pcm_hardware snd_p16v_playback_hw = {
127	.info =			(SNDRV_PCM_INFO_MMAP |
128				 SNDRV_PCM_INFO_INTERLEAVED |
129				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
130				 SNDRV_PCM_INFO_RESUME |
131				 SNDRV_PCM_INFO_MMAP_VALID),
132	.formats =		SNDRV_PCM_FMTBIT_S32_LE, /* Only supports 24-bit samples padded to 32 bits. */
133	.rates =		SNDRV_PCM_RATE_192000 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_44100,
134	.rate_min =		44100,
135	.rate_max =		192000,
136	.channels_min =		8,
137	.channels_max =		8,
138	.buffer_bytes_max =	((65536 - 64) * 8),
139	.period_bytes_min =	64,
140	.period_bytes_max =	(65536 - 64),
141	.periods_min =		2,
142	.periods_max =		8,
143	.fifo_size =		0,
144};
145
146static struct snd_pcm_hardware snd_p16v_capture_hw = {
147	.info =			(SNDRV_PCM_INFO_MMAP |
148				 SNDRV_PCM_INFO_INTERLEAVED |
149				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
150				 SNDRV_PCM_INFO_RESUME |
151				 SNDRV_PCM_INFO_MMAP_VALID),
152	.formats =		SNDRV_PCM_FMTBIT_S32_LE,
153	.rates =		SNDRV_PCM_RATE_192000 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_44100,
154	.rate_min =		44100,
155	.rate_max =		192000,
156	.channels_min =		2,
157	.channels_max =		2,
158	.buffer_bytes_max =	(65536 - 64),
159	.period_bytes_min =	64,
160	.period_bytes_max =	(65536 - 128) >> 1,  /* size has to be N*64 bytes */
161	.periods_min =		2,
162	.periods_max =		2,
163	.fifo_size =		0,
164};
165
166static void snd_p16v_pcm_free_substream(struct snd_pcm_runtime *runtime)
167{
168	struct snd_emu10k1_pcm *epcm = runtime->private_data;
169
170	if (epcm) {
171        	//snd_printk("epcm free: %p\n", epcm);
172		kfree(epcm);
173	}
174}
175
176/* open_playback callback */
177static int snd_p16v_pcm_open_playback_channel(struct snd_pcm_substream *substream, int channel_id)
178{
179	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
180        struct snd_emu10k1_voice *channel = &(emu->p16v_voices[channel_id]);
181	struct snd_emu10k1_pcm *epcm;
182	struct snd_pcm_runtime *runtime = substream->runtime;
183	int err;
184
185	epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
186        //snd_printk("epcm kcalloc: %p\n", epcm);
187
188	if (epcm == NULL)
189		return -ENOMEM;
190	epcm->emu = emu;
191	epcm->substream = substream;
192        //snd_printk("epcm device=%d, channel_id=%d\n", substream->pcm->device, channel_id);
193
194	runtime->private_data = epcm;
195	runtime->private_free = snd_p16v_pcm_free_substream;
196
197	runtime->hw = snd_p16v_playback_hw;
198
199        channel->emu = emu;
200        channel->number = channel_id;
201
202        channel->use=1;
203	//snd_printk("p16v: open channel_id=%d, channel=%p, use=0x%x\n", channel_id, channel, channel->use);
204        //printk("open:channel_id=%d, chip=%p, channel=%p\n",channel_id, chip, channel);
205        //channel->interrupt = snd_p16v_pcm_channel_interrupt;
206        channel->epcm=epcm;
207	if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
208                return err;
209
210	return 0;
211}
212/* open_capture callback */
213static int snd_p16v_pcm_open_capture_channel(struct snd_pcm_substream *substream, int channel_id)
214{
215	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
216	struct snd_emu10k1_voice *channel = &(emu->p16v_capture_voice);
217	struct snd_emu10k1_pcm *epcm;
218	struct snd_pcm_runtime *runtime = substream->runtime;
219	int err;
220
221	epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
222	//snd_printk("epcm kcalloc: %p\n", epcm);
223
224	if (epcm == NULL)
225		return -ENOMEM;
226	epcm->emu = emu;
227	epcm->substream = substream;
228	//snd_printk("epcm device=%d, channel_id=%d\n", substream->pcm->device, channel_id);
229
230	runtime->private_data = epcm;
231	runtime->private_free = snd_p16v_pcm_free_substream;
232
233	runtime->hw = snd_p16v_capture_hw;
234
235	channel->emu = emu;
236	channel->number = channel_id;
237
238	channel->use=1;
239	//snd_printk("p16v: open channel_id=%d, channel=%p, use=0x%x\n", channel_id, channel, channel->use);
240	//printk("open:channel_id=%d, chip=%p, channel=%p\n",channel_id, chip, channel);
241	//channel->interrupt = snd_p16v_pcm_channel_interrupt;
242	channel->epcm=epcm;
243	if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
244		return err;
245
246	return 0;
247}
248
249
250/* close callback */
251static int snd_p16v_pcm_close_playback(struct snd_pcm_substream *substream)
252{
253	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
254	//struct snd_pcm_runtime *runtime = substream->runtime;
255	//struct snd_emu10k1_pcm *epcm = runtime->private_data;
256	emu->p16v_voices[substream->pcm->device - emu->p16v_device_offset].use = 0;
257	return 0;
258}
259
260/* close callback */
261static int snd_p16v_pcm_close_capture(struct snd_pcm_substream *substream)
262{
263	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
264	//struct snd_pcm_runtime *runtime = substream->runtime;
265	//struct snd_emu10k1_pcm *epcm = runtime->private_data;
266	emu->p16v_capture_voice.use = 0;
267	return 0;
268}
269
270static int snd_p16v_pcm_open_playback_front(struct snd_pcm_substream *substream)
271{
272	return snd_p16v_pcm_open_playback_channel(substream, PCM_FRONT_CHANNEL);
273}
274
275static int snd_p16v_pcm_open_capture(struct snd_pcm_substream *substream)
276{
277	// Only using channel 0 for now, but the card has 2 channels.
278	return snd_p16v_pcm_open_capture_channel(substream, 0);
279}
280
281/* hw_params callback */
282static int snd_p16v_pcm_hw_params_playback(struct snd_pcm_substream *substream,
283				      struct snd_pcm_hw_params *hw_params)
284{
285	int result;
286	result = snd_pcm_lib_malloc_pages(substream,
287					params_buffer_bytes(hw_params));
288	return result;
289}
290
291/* hw_params callback */
292static int snd_p16v_pcm_hw_params_capture(struct snd_pcm_substream *substream,
293				      struct snd_pcm_hw_params *hw_params)
294{
295	int result;
296	result = snd_pcm_lib_malloc_pages(substream,
297					params_buffer_bytes(hw_params));
298	return result;
299}
300
301
302/* hw_free callback */
303static int snd_p16v_pcm_hw_free_playback(struct snd_pcm_substream *substream)
304{
305	int result;
306	result = snd_pcm_lib_free_pages(substream);
307	return result;
308}
309
310/* hw_free callback */
311static int snd_p16v_pcm_hw_free_capture(struct snd_pcm_substream *substream)
312{
313	int result;
314	result = snd_pcm_lib_free_pages(substream);
315	return result;
316}
317
318
319/* prepare playback callback */
320static int snd_p16v_pcm_prepare_playback(struct snd_pcm_substream *substream)
321{
322	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
323	struct snd_pcm_runtime *runtime = substream->runtime;
324	int channel = substream->pcm->device - emu->p16v_device_offset;
325	u32 *table_base = (u32 *)(emu->p16v_buffer.area+(8*16*channel));
326	u32 period_size_bytes = frames_to_bytes(runtime, runtime->period_size);
327	int i;
328	u32 tmp;
329
330        //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));
331        //snd_printk("dma_addr=%x, dma_area=%p, table_base=%p\n",runtime->dma_addr, runtime->dma_area, table_base);
332	//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);
333	tmp = snd_emu10k1_ptr_read(emu, A_SPDIF_SAMPLERATE, channel);
334        switch (runtime->rate) {
335	case 44100:
336	  snd_emu10k1_ptr_write(emu, A_SPDIF_SAMPLERATE, channel, (tmp & ~0xe0e0) | 0x8080);
337	  break;
338	case 96000:
339	  snd_emu10k1_ptr_write(emu, A_SPDIF_SAMPLERATE, channel, (tmp & ~0xe0e0) | 0x4040);
340	  break;
341	case 192000:
342	  snd_emu10k1_ptr_write(emu, A_SPDIF_SAMPLERATE, channel, (tmp & ~0xe0e0) | 0x2020);
343	  break;
344	case 48000:
345	default:
346	  snd_emu10k1_ptr_write(emu, A_SPDIF_SAMPLERATE, channel, (tmp & ~0xe0e0) | 0x0000);
347	  break;
348	}
349	for(i = 0; i < runtime->periods; i++) {
350		table_base[i*2]=runtime->dma_addr+(i*period_size_bytes);
351		table_base[(i*2)+1]=period_size_bytes<<16;
352	}
353
354	snd_emu10k1_ptr20_write(emu, PLAYBACK_LIST_ADDR, channel, emu->p16v_buffer.addr+(8*16*channel));
355	snd_emu10k1_ptr20_write(emu, PLAYBACK_LIST_SIZE, channel, (runtime->periods - 1) << 19);
356	snd_emu10k1_ptr20_write(emu, PLAYBACK_LIST_PTR, channel, 0);
357	snd_emu10k1_ptr20_write(emu, PLAYBACK_DMA_ADDR, channel, runtime->dma_addr);
358	//snd_emu10k1_ptr20_write(emu, PLAYBACK_PERIOD_SIZE, channel, frames_to_bytes(runtime, runtime->period_size)<<16); // buffer size in bytes
359	snd_emu10k1_ptr20_write(emu, PLAYBACK_PERIOD_SIZE, channel, 0); // buffer size in bytes
360	snd_emu10k1_ptr20_write(emu, PLAYBACK_POINTER, channel, 0);
361	snd_emu10k1_ptr20_write(emu, 0x07, channel, 0x0);
362	snd_emu10k1_ptr20_write(emu, 0x08, channel, 0);
363
364	return 0;
365}
366
367/* prepare capture callback */
368static int snd_p16v_pcm_prepare_capture(struct snd_pcm_substream *substream)
369{
370	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
371	struct snd_pcm_runtime *runtime = substream->runtime;
372	int channel = substream->pcm->device - emu->p16v_device_offset;
373	u32 tmp;
374	//printk("prepare capture: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));
375	tmp = snd_emu10k1_ptr_read(emu, A_SPDIF_SAMPLERATE, channel);
376        switch (runtime->rate) {
377	case 44100:
378	  snd_emu10k1_ptr_write(emu, A_SPDIF_SAMPLERATE, channel, (tmp & ~0x0e00) | 0x0800);
379	  break;
380	case 96000:
381	  snd_emu10k1_ptr_write(emu, A_SPDIF_SAMPLERATE, channel, (tmp & ~0x0e00) | 0x0400);
382	  break;
383	case 192000:
384	  snd_emu10k1_ptr_write(emu, A_SPDIF_SAMPLERATE, channel, (tmp & ~0x0e00) | 0x0200);
385	  break;
386	case 48000:
387	default:
388	  snd_emu10k1_ptr_write(emu, A_SPDIF_SAMPLERATE, channel, (tmp & ~0x0e00) | 0x0000);
389	  break;
390	}
391	snd_emu10k1_ptr20_write(emu, 0x13, channel, 0);
392	snd_emu10k1_ptr20_write(emu, CAPTURE_DMA_ADDR, channel, runtime->dma_addr);
393	snd_emu10k1_ptr20_write(emu, CAPTURE_BUFFER_SIZE, channel, frames_to_bytes(runtime, runtime->buffer_size) << 16); // buffer size in bytes
394	snd_emu10k1_ptr20_write(emu, CAPTURE_POINTER, channel, 0);
395	//snd_emu10k1_ptr20_write(emu, CAPTURE_SOURCE, 0x0, 0x333300e4); /* Select MIC or Line in */
396	//snd_emu10k1_ptr20_write(emu, EXTENDED_INT_MASK, 0, snd_emu10k1_ptr20_read(emu, EXTENDED_INT_MASK, 0) | (0x110000<<channel));
397
398	return 0;
399}
400
401static void snd_p16v_intr_enable(struct snd_emu10k1 *emu, unsigned int intrenb)
402{
403	unsigned long flags;
404	unsigned int enable;
405
406	spin_lock_irqsave(&emu->emu_lock, flags);
407	enable = inl(emu->port + INTE2) | intrenb;
408	outl(enable, emu->port + INTE2);
409	spin_unlock_irqrestore(&emu->emu_lock, flags);
410}
411
412static void snd_p16v_intr_disable(struct snd_emu10k1 *emu, unsigned int intrenb)
413{
414	unsigned long flags;
415	unsigned int disable;
416
417	spin_lock_irqsave(&emu->emu_lock, flags);
418	disable = inl(emu->port + INTE2) & (~intrenb);
419	outl(disable, emu->port + INTE2);
420	spin_unlock_irqrestore(&emu->emu_lock, flags);
421}
422
423/* trigger_playback callback */
424static int snd_p16v_pcm_trigger_playback(struct snd_pcm_substream *substream,
425				    int cmd)
426{
427	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
428	struct snd_pcm_runtime *runtime;
429	struct snd_emu10k1_pcm *epcm;
430	int channel;
431	int result = 0;
432        struct snd_pcm_substream *s;
433	u32 basic = 0;
434	u32 inte = 0;
435	int running = 0;
436
437	switch (cmd) {
438	case SNDRV_PCM_TRIGGER_START:
439		running=1;
440		break;
441	case SNDRV_PCM_TRIGGER_STOP:
442	default:
443		running = 0;
444		break;
445	}
446        snd_pcm_group_for_each_entry(s, substream) {
447		runtime = s->runtime;
448		epcm = runtime->private_data;
449		channel = substream->pcm->device-emu->p16v_device_offset;
450		//snd_printk("p16v channel=%d\n",channel);
451		epcm->running = running;
452		basic |= (0x1<<channel);
453		inte |= (INTE2_PLAYBACK_CH_0_LOOP<<channel);
454                snd_pcm_trigger_done(s, substream);
455        }
456	//snd_printk("basic=0x%x, inte=0x%x\n",basic, inte);
457
458	switch (cmd) {
459	case SNDRV_PCM_TRIGGER_START:
460		snd_p16v_intr_enable(emu, inte);
461		snd_emu10k1_ptr20_write(emu, BASIC_INTERRUPT, 0, snd_emu10k1_ptr20_read(emu, BASIC_INTERRUPT, 0)| (basic));
462		break;
463	case SNDRV_PCM_TRIGGER_STOP:
464		snd_emu10k1_ptr20_write(emu, BASIC_INTERRUPT, 0, snd_emu10k1_ptr20_read(emu, BASIC_INTERRUPT, 0) & ~(basic));
465		snd_p16v_intr_disable(emu, inte);
466		break;
467	default:
468		result = -EINVAL;
469		break;
470	}
471	return result;
472}
473
474/* trigger_capture callback */
475static int snd_p16v_pcm_trigger_capture(struct snd_pcm_substream *substream,
476                                   int cmd)
477{
478	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
479	struct snd_pcm_runtime *runtime = substream->runtime;
480	struct snd_emu10k1_pcm *epcm = runtime->private_data;
481	int channel = 0;
482	int result = 0;
483	u32 inte = INTE2_CAPTURE_CH_0_LOOP | INTE2_CAPTURE_CH_0_HALF_LOOP;
484
485	switch (cmd) {
486	case SNDRV_PCM_TRIGGER_START:
487		snd_p16v_intr_enable(emu, inte);
488		snd_emu10k1_ptr20_write(emu, BASIC_INTERRUPT, 0, snd_emu10k1_ptr20_read(emu, BASIC_INTERRUPT, 0)|(0x100<<channel));
489		epcm->running = 1;
490		break;
491	case SNDRV_PCM_TRIGGER_STOP:
492		snd_emu10k1_ptr20_write(emu, BASIC_INTERRUPT, 0, snd_emu10k1_ptr20_read(emu, BASIC_INTERRUPT, 0) & ~(0x100<<channel));
493		snd_p16v_intr_disable(emu, inte);
494		//snd_emu10k1_ptr20_write(emu, EXTENDED_INT_MASK, 0, snd_emu10k1_ptr20_read(emu, EXTENDED_INT_MASK, 0) & ~(0x110000<<channel));
495		epcm->running = 0;
496		break;
497	default:
498		result = -EINVAL;
499		break;
500	}
501	return result;
502}
503
504/* pointer_playback callback */
505static snd_pcm_uframes_t
506snd_p16v_pcm_pointer_playback(struct snd_pcm_substream *substream)
507{
508	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
509	struct snd_pcm_runtime *runtime = substream->runtime;
510	struct snd_emu10k1_pcm *epcm = runtime->private_data;
511	snd_pcm_uframes_t ptr, ptr1, ptr2,ptr3,ptr4 = 0;
512	int channel = substream->pcm->device - emu->p16v_device_offset;
513	if (!epcm->running)
514		return 0;
515
516	ptr3 = snd_emu10k1_ptr20_read(emu, PLAYBACK_LIST_PTR, channel);
517	ptr1 = snd_emu10k1_ptr20_read(emu, PLAYBACK_POINTER, channel);
518	ptr4 = snd_emu10k1_ptr20_read(emu, PLAYBACK_LIST_PTR, channel);
519	if (ptr3 != ptr4) ptr1 = snd_emu10k1_ptr20_read(emu, PLAYBACK_POINTER, channel);
520	ptr2 = bytes_to_frames(runtime, ptr1);
521	ptr2+= (ptr4 >> 3) * runtime->period_size;
522	ptr=ptr2;
523        if (ptr >= runtime->buffer_size)
524		ptr -= runtime->buffer_size;
525
526	return ptr;
527}
528
529/* pointer_capture callback */
530static snd_pcm_uframes_t
531snd_p16v_pcm_pointer_capture(struct snd_pcm_substream *substream)
532{
533	struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
534	struct snd_pcm_runtime *runtime = substream->runtime;
535	struct snd_emu10k1_pcm *epcm = runtime->private_data;
536	snd_pcm_uframes_t ptr, ptr1, ptr2 = 0;
537	int channel = 0;
538
539	if (!epcm->running)
540		return 0;
541
542	ptr1 = snd_emu10k1_ptr20_read(emu, CAPTURE_POINTER, channel);
543	ptr2 = bytes_to_frames(runtime, ptr1);
544	ptr=ptr2;
545	if (ptr >= runtime->buffer_size) {
546		ptr -= runtime->buffer_size;
547		printk(KERN_WARNING "buffer capture limited!\n");
548	}
549	//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);
550
551	return ptr;
552}
553
554/* operators */
555static struct snd_pcm_ops snd_p16v_playback_front_ops = {
556	.open =        snd_p16v_pcm_open_playback_front,
557	.close =       snd_p16v_pcm_close_playback,
558	.ioctl =       snd_pcm_lib_ioctl,
559	.hw_params =   snd_p16v_pcm_hw_params_playback,
560	.hw_free =     snd_p16v_pcm_hw_free_playback,
561	.prepare =     snd_p16v_pcm_prepare_playback,
562	.trigger =     snd_p16v_pcm_trigger_playback,
563	.pointer =     snd_p16v_pcm_pointer_playback,
564};
565
566static struct snd_pcm_ops snd_p16v_capture_ops = {
567	.open =        snd_p16v_pcm_open_capture,
568	.close =       snd_p16v_pcm_close_capture,
569	.ioctl =       snd_pcm_lib_ioctl,
570	.hw_params =   snd_p16v_pcm_hw_params_capture,
571	.hw_free =     snd_p16v_pcm_hw_free_capture,
572	.prepare =     snd_p16v_pcm_prepare_capture,
573	.trigger =     snd_p16v_pcm_trigger_capture,
574	.pointer =     snd_p16v_pcm_pointer_capture,
575};
576
577
578int snd_p16v_free(struct snd_emu10k1 *chip)
579{
580	// release the data
581	if (chip->p16v_buffer.area) {
582		snd_dma_free_pages(&chip->p16v_buffer);
583		//snd_printk("period lables free: %p\n", &chip->p16v_buffer);
584	}
585	return 0;
586}
587
588int __devinit snd_p16v_pcm(struct snd_emu10k1 *emu, int device, struct snd_pcm **rpcm)
589{
590	struct snd_pcm *pcm;
591	struct snd_pcm_substream *substream;
592	int err;
593        int capture=1;
594
595	//snd_printk("snd_p16v_pcm called. device=%d\n", device);
596	emu->p16v_device_offset = device;
597	if (rpcm)
598		*rpcm = NULL;
599
600	if ((err = snd_pcm_new(emu->card, "p16v", device, 1, capture, &pcm)) < 0)
601		return err;
602
603	pcm->private_data = emu;
604	// Single playback 8 channel device.
605	// Single capture 2 channel device.
606	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_p16v_playback_front_ops);
607	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_p16v_capture_ops);
608
609	pcm->info_flags = 0;
610	pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
611	strcpy(pcm->name, "p16v");
612	emu->pcm_p16v = pcm;
613
614	for(substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
615	    substream;
616	    substream = substream->next) {
617		if ((err = snd_pcm_lib_preallocate_pages(substream,
618							 SNDRV_DMA_TYPE_DEV,
619							 snd_dma_pci_data(emu->pci),
620							 ((65536 - 64) * 8), ((65536 - 64) * 8))) < 0)
621			return err;
622		//snd_printk("preallocate playback substream: err=%d\n", err);
623	}
624
625	for (substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
626	      substream;
627	      substream = substream->next) {
628 		if ((err = snd_pcm_lib_preallocate_pages(substream,
629	                                           SNDRV_DMA_TYPE_DEV,
630	                                           snd_dma_pci_data(emu->pci),
631	                                           65536 - 64, 65536 - 64)) < 0)
632			return err;
633		//snd_printk("preallocate capture substream: err=%d\n", err);
634	}
635
636	if (rpcm)
637		*rpcm = pcm;
638
639	return 0;
640}
641
642static int snd_p16v_volume_info(struct snd_kcontrol *kcontrol,
643				struct snd_ctl_elem_info *uinfo)
644{
645        uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
646        uinfo->count = 2;
647        uinfo->value.integer.min = 0;
648        uinfo->value.integer.max = 255;
649        return 0;
650}
651
652static int snd_p16v_volume_get(struct snd_kcontrol *kcontrol,
653			       struct snd_ctl_elem_value *ucontrol)
654{
655        struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
656	int high_low = (kcontrol->private_value >> 8) & 0xff;
657	int reg = kcontrol->private_value & 0xff;
658	u32 value;
659
660	value = snd_emu10k1_ptr20_read(emu, reg, high_low);
661	if (high_low) {
662		ucontrol->value.integer.value[0] = 0xff - ((value >> 24) & 0xff); /* Left */
663		ucontrol->value.integer.value[1] = 0xff - ((value >> 16) & 0xff); /* Right */
664	} else {
665		ucontrol->value.integer.value[0] = 0xff - ((value >> 8) & 0xff); /* Left */
666		ucontrol->value.integer.value[1] = 0xff - ((value >> 0) & 0xff); /* Right */
667	}
668	return 0;
669}
670
671static int snd_p16v_volume_put(struct snd_kcontrol *kcontrol,
672			       struct snd_ctl_elem_value *ucontrol)
673{
674        struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
675	int high_low = (kcontrol->private_value >> 8) & 0xff;
676	int reg = kcontrol->private_value & 0xff;
677        u32 value, oval;
678
679	oval = value = snd_emu10k1_ptr20_read(emu, reg, 0);
680	if (high_low == 1) {
681		value &= 0xffff;
682		value |= ((0xff - ucontrol->value.integer.value[0]) << 24) |
683			((0xff - ucontrol->value.integer.value[1]) << 16);
684	} else {
685		value &= 0xffff0000;
686		value |= ((0xff - ucontrol->value.integer.value[0]) << 8) |
687			((0xff - ucontrol->value.integer.value[1]) );
688	}
689	if (value != oval) {
690		snd_emu10k1_ptr20_write(emu, reg, 0, value);
691		return 1;
692	}
693	return 0;
694}
695
696static int snd_p16v_capture_source_info(struct snd_kcontrol *kcontrol,
697					struct snd_ctl_elem_info *uinfo)
698{
699	static char *texts[8] = {
700		"SPDIF", "I2S", "SRC48", "SRCMulti_SPDIF", "SRCMulti_I2S",
701		"CDIF", "FX", "AC97"
702	};
703
704	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
705	uinfo->count = 1;
706	uinfo->value.enumerated.items = 8;
707	if (uinfo->value.enumerated.item > 7)
708                uinfo->value.enumerated.item = 7;
709	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
710	return 0;
711}
712
713static int snd_p16v_capture_source_get(struct snd_kcontrol *kcontrol,
714					struct snd_ctl_elem_value *ucontrol)
715{
716	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
717
718	ucontrol->value.enumerated.item[0] = emu->p16v_capture_source;
719	return 0;
720}
721
722static int snd_p16v_capture_source_put(struct snd_kcontrol *kcontrol,
723					struct snd_ctl_elem_value *ucontrol)
724{
725	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
726	unsigned int val;
727	int change = 0;
728	u32 mask;
729	u32 source;
730
731	val = ucontrol->value.enumerated.item[0] ;
732	change = (emu->p16v_capture_source != val);
733	if (change) {
734		emu->p16v_capture_source = val;
735		source = (val << 28) | (val << 24) | (val << 20) | (val << 16);
736		mask = snd_emu10k1_ptr20_read(emu, BASIC_INTERRUPT, 0) & 0xffff;
737		snd_emu10k1_ptr20_write(emu, BASIC_INTERRUPT, 0, source | mask);
738	}
739        return change;
740}
741
742static int snd_p16v_capture_channel_info(struct snd_kcontrol *kcontrol,
743					 struct snd_ctl_elem_info *uinfo)
744{
745	static char *texts[4] = { "0", "1", "2", "3",  };
746
747	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
748	uinfo->count = 1;
749	uinfo->value.enumerated.items = 4;
750	if (uinfo->value.enumerated.item > 3)
751                uinfo->value.enumerated.item = 3;
752	strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
753	return 0;
754}
755
756static int snd_p16v_capture_channel_get(struct snd_kcontrol *kcontrol,
757					struct snd_ctl_elem_value *ucontrol)
758{
759	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
760
761	ucontrol->value.enumerated.item[0] = emu->p16v_capture_channel;
762	return 0;
763}
764
765static int snd_p16v_capture_channel_put(struct snd_kcontrol *kcontrol,
766					struct snd_ctl_elem_value *ucontrol)
767{
768	struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
769	unsigned int val;
770	int change = 0;
771	u32 tmp;
772
773	val = ucontrol->value.enumerated.item[0] ;
774	change = (emu->p16v_capture_channel != val);
775	if (change) {
776		emu->p16v_capture_channel = val;
777		tmp = snd_emu10k1_ptr20_read(emu, CAPTURE_P16V_SOURCE, 0) & 0xfffc;
778		snd_emu10k1_ptr20_write(emu, CAPTURE_P16V_SOURCE, 0, tmp | val);
779	}
780        return change;
781}
782static const DECLARE_TLV_DB_SCALE(snd_p16v_db_scale1, -5175, 25, 1);
783
784#define P16V_VOL(xname,xreg,xhl) { \
785	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
786        .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |             \
787                  SNDRV_CTL_ELEM_ACCESS_TLV_READ,               \
788	.info = snd_p16v_volume_info, \
789	.get = snd_p16v_volume_get, \
790	.put = snd_p16v_volume_put, \
791	.tlv = { .p = snd_p16v_db_scale1 },	\
792	.private_value = ((xreg) | ((xhl) << 8)) \
793}
794
795static struct snd_kcontrol_new p16v_mixer_controls[] __devinitdata = {
796	P16V_VOL("HD Analog Front Playback Volume", PLAYBACK_VOLUME_MIXER9, 0),
797	P16V_VOL("HD Analog Rear Playback Volume", PLAYBACK_VOLUME_MIXER10, 1),
798	P16V_VOL("HD Analog Center/LFE Playback Volume", PLAYBACK_VOLUME_MIXER9, 1),
799	P16V_VOL("HD Analog Side Playback Volume", PLAYBACK_VOLUME_MIXER10, 0),
800	P16V_VOL("HD SPDIF Front Playback Volume", PLAYBACK_VOLUME_MIXER7, 0),
801	P16V_VOL("HD SPDIF Rear Playback Volume", PLAYBACK_VOLUME_MIXER8, 1),
802	P16V_VOL("HD SPDIF Center/LFE Playback Volume", PLAYBACK_VOLUME_MIXER7, 1),
803	P16V_VOL("HD SPDIF Side Playback Volume", PLAYBACK_VOLUME_MIXER8, 0),
804	{
805		.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
806		.name =		"HD source Capture",
807		.info =		snd_p16v_capture_source_info,
808		.get =		snd_p16v_capture_source_get,
809		.put =		snd_p16v_capture_source_put
810	},
811	{
812		.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
813		.name =		"HD channel Capture",
814		.info =		snd_p16v_capture_channel_info,
815		.get =		snd_p16v_capture_channel_get,
816		.put =		snd_p16v_capture_channel_put
817	},
818};
819
820
821int __devinit snd_p16v_mixer(struct snd_emu10k1 *emu)
822{
823	int i, err;
824        struct snd_card *card = emu->card;
825
826	for (i = 0; i < ARRAY_SIZE(p16v_mixer_controls); i++) {
827		if ((err = snd_ctl_add(card, snd_ctl_new1(&p16v_mixer_controls[i],
828							  emu))) < 0)
829			return err;
830	}
831        return 0;
832}
833
834#ifdef CONFIG_PM
835
836#define NUM_CHS	1	/* up to 4, but only first channel is used */
837
838int __devinit snd_p16v_alloc_pm_buffer(struct snd_emu10k1 *emu)
839{
840	emu->p16v_saved = vmalloc(NUM_CHS * 4 * 0x80);
841	if (! emu->p16v_saved)
842		return -ENOMEM;
843	return 0;
844}
845
846void snd_p16v_free_pm_buffer(struct snd_emu10k1 *emu)
847{
848	vfree(emu->p16v_saved);
849}
850
851void snd_p16v_suspend(struct snd_emu10k1 *emu)
852{
853	int i, ch;
854	unsigned int *val;
855
856	val = emu->p16v_saved;
857	for (ch = 0; ch < NUM_CHS; ch++)
858		for (i = 0; i < 0x80; i++, val++)
859			*val = snd_emu10k1_ptr20_read(emu, i, ch);
860}
861
862void snd_p16v_resume(struct snd_emu10k1 *emu)
863{
864	int i, ch;
865	unsigned int *val;
866
867	val = emu->p16v_saved;
868	for (ch = 0; ch < NUM_CHS; ch++)
869		for (i = 0; i < 0x80; i++, val++)
870			snd_emu10k1_ptr20_write(emu, i, ch, *val);
871}
872#endif
873