blob: 776761fe5577cfd59c4064aa0afa16d2adf0a07f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (c) by James Courtier-Dutton <James@superbug.demon.co.uk>
3 * Driver p16v chips
4 * Version: 0.22
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 *
45 *
46 * BUGS:
47 * Some stability problems when unloading the snd-p16v kernel module.
48 * --
49 *
50 * TODO:
51 * SPDIF out.
52 * Find out how to change capture sample rates. E.g. To record SPDIF at 48000Hz.
53 * Currently capture fixed at 48000Hz.
54 *
55 * --
56 * GENERAL INFO:
57 * Model: SB0240
58 * P16V Chip: CA0151-DBS
59 * Audigy 2 Chip: CA0102-IAT
60 * AC97 Codec: STAC 9721
61 * ADC: Philips 1361T (Stereo 24bit)
62 * DAC: CS4382-K (8-channel, 24bit, 192Khz)
63 *
64 * This code was initally based on code from ALSA's emu10k1x.c which is:
65 * Copyright (c) by Francisco Moraes <fmoraes@nc.rr.com>
66 *
67 * This program is free software; you can redistribute it and/or modify
68 * it under the terms of the GNU General Public License as published by
69 * the Free Software Foundation; either version 2 of the License, or
70 * (at your option) any later version.
71 *
72 * This program is distributed in the hope that it will be useful,
73 * but WITHOUT ANY WARRANTY; without even the implied warranty of
74 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
75 * GNU General Public License for more details.
76 *
77 * You should have received a copy of the GNU General Public License
78 * along with this program; if not, write to the Free Software
79 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
80 *
81 */
82#include <sound/driver.h>
83#include <linux/delay.h>
84#include <linux/init.h>
85#include <linux/interrupt.h>
86#include <linux/pci.h>
87#include <linux/slab.h>
88#include <linux/moduleparam.h>
89#include <sound/core.h>
90#include <sound/initval.h>
91#include <sound/pcm.h>
92#include <sound/ac97_codec.h>
93#include <sound/info.h>
94#include <sound/emu10k1.h>
95#include "p16v.h"
96
97#define SET_CHANNEL 0 /* Testing channel outputs 0=Front, 1=Center/LFE, 2=Unknown, 3=Rear */
98#define PCM_FRONT_CHANNEL 0
99#define PCM_REAR_CHANNEL 1
100#define PCM_CENTER_LFE_CHANNEL 2
101#define PCM_UNKNOWN_CHANNEL 3
102#define CONTROL_FRONT_CHANNEL 0
103#define CONTROL_REAR_CHANNEL 3
104#define CONTROL_CENTER_LFE_CHANNEL 1
105#define CONTROL_UNKNOWN_CHANNEL 2
106
107/* Card IDs:
108 * Class 0401: 1102:0004 (rev 04) Subsystem: 1102:2002 -> Audigy2 ZS 7.1 Model:SB0350
109 * Class 0401: 1102:0004 (rev 04) Subsystem: 1102:1007 -> Audigy2 6.1 Model:SB0240
110 * Class 0401: 1102:0004 (rev 04) Subsystem: 1102:1002 -> Audigy2 Platinum Model:SB msb0240230009266
111 * Class 0401: 1102:0004 (rev 04) Subsystem: 1102:2007 -> Audigy4 Pro Model:SB0380 M1SB0380472001901E
112 *
113 */
114
115 /* hardware definition */
116static snd_pcm_hardware_t snd_p16v_playback_hw = {
117 .info = (SNDRV_PCM_INFO_MMAP |
118 SNDRV_PCM_INFO_INTERLEAVED |
119 SNDRV_PCM_INFO_BLOCK_TRANSFER |
120 SNDRV_PCM_INFO_MMAP_VALID),
121 .formats = SNDRV_PCM_FMTBIT_S32_LE, /* Only supports 24-bit samples padded to 32 bits. */
James Courtier-Dutton001f7582005-04-09 23:38:25 +0200122 .rates = SNDRV_PCM_RATE_192000 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_44100,
123 .rate_min = 44100,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 .rate_max = 192000,
125 .channels_min = 8,
126 .channels_max = 8,
James Courtier-Dutton310bacd2005-04-10 00:00:24 +0200127 .buffer_bytes_max = ((65536 - 64) * 8),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 .period_bytes_min = 64,
James Courtier-Dutton310bacd2005-04-10 00:00:24 +0200129 .period_bytes_max = (65536 - 64),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 .periods_min = 2,
131 .periods_max = 8,
132 .fifo_size = 0,
133};
134
James Courtier-Dutton6e4abc42005-03-26 19:35:29 +0100135static snd_pcm_hardware_t snd_p16v_capture_hw = {
136 .info = (SNDRV_PCM_INFO_MMAP |
137 SNDRV_PCM_INFO_INTERLEAVED |
138 SNDRV_PCM_INFO_BLOCK_TRANSFER |
139 SNDRV_PCM_INFO_MMAP_VALID),
140 .formats = SNDRV_PCM_FMTBIT_S32_LE,
141 .rates = SNDRV_PCM_RATE_48000,
142 .rate_min = 48000,
143 .rate_max = 48000,
144 .channels_min = 2,
145 .channels_max = 2,
146 .buffer_bytes_max = (32*1024),
147 .period_bytes_min = 64,
148 .period_bytes_max = (16*1024),
149 .periods_min = 2,
150 .periods_max = 2,
151 .fifo_size = 0,
152};
153
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155static void snd_p16v_pcm_free_substream(snd_pcm_runtime_t *runtime)
156{
James Courtier-Dutton6e4abc42005-03-26 19:35:29 +0100157 emu10k1_pcm_t *epcm = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159 if (epcm) {
160 //snd_printk("epcm free: %p\n", epcm);
161 kfree(epcm);
162 }
163}
164
165/* open_playback callback */
166static int snd_p16v_pcm_open_playback_channel(snd_pcm_substream_t *substream, int channel_id)
167{
168 emu10k1_t *emu = snd_pcm_substream_chip(substream);
169 emu10k1_voice_t *channel = &(emu->p16v_voices[channel_id]);
170 emu10k1_pcm_t *epcm;
171 snd_pcm_runtime_t *runtime = substream->runtime;
172 int err;
173
174 epcm = kcalloc(1, sizeof(*epcm), GFP_KERNEL);
175 //snd_printk("epcm kcalloc: %p\n", epcm);
176
177 if (epcm == NULL)
178 return -ENOMEM;
179 epcm->emu = emu;
180 epcm->substream = substream;
181 //snd_printk("epcm device=%d, channel_id=%d\n", substream->pcm->device, channel_id);
182
183 runtime->private_data = epcm;
184 runtime->private_free = snd_p16v_pcm_free_substream;
185
186 runtime->hw = snd_p16v_playback_hw;
187
188 channel->emu = emu;
189 channel->number = channel_id;
190
191 channel->use=1;
192 //snd_printk("p16v: open channel_id=%d, channel=%p, use=0x%x\n", channel_id, channel, channel->use);
193 //printk("open:channel_id=%d, chip=%p, channel=%p\n",channel_id, chip, channel);
194 //channel->interrupt = snd_p16v_pcm_channel_interrupt;
195 channel->epcm=epcm;
196 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
197 return err;
198
199 return 0;
200}
James Courtier-Dutton6e4abc42005-03-26 19:35:29 +0100201/* open_capture callback */
202static int snd_p16v_pcm_open_capture_channel(snd_pcm_substream_t *substream, int channel_id)
203{
204 emu10k1_t *emu = snd_pcm_substream_chip(substream);
205 emu10k1_voice_t *channel = &(emu->p16v_capture_voice);
206 emu10k1_pcm_t *epcm;
207 snd_pcm_runtime_t *runtime = substream->runtime;
208 int err;
209
210 epcm = kcalloc(1, sizeof(*epcm), GFP_KERNEL);
211 //snd_printk("epcm kcalloc: %p\n", epcm);
212
213 if (epcm == NULL)
214 return -ENOMEM;
215 epcm->emu = emu;
216 epcm->substream = substream;
217 //snd_printk("epcm device=%d, channel_id=%d\n", substream->pcm->device, channel_id);
218
219 runtime->private_data = epcm;
220 runtime->private_free = snd_p16v_pcm_free_substream;
221
222 runtime->hw = snd_p16v_capture_hw;
223
224 channel->emu = emu;
225 channel->number = channel_id;
226
227 channel->use=1;
228 //snd_printk("p16v: open channel_id=%d, channel=%p, use=0x%x\n", channel_id, channel, channel->use);
229 //printk("open:channel_id=%d, chip=%p, channel=%p\n",channel_id, chip, channel);
230 //channel->interrupt = snd_p16v_pcm_channel_interrupt;
231 channel->epcm=epcm;
232 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
233 return err;
234
235 return 0;
236}
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239/* close callback */
240static int snd_p16v_pcm_close_playback(snd_pcm_substream_t *substream)
241{
242 emu10k1_t *emu = snd_pcm_substream_chip(substream);
243 //snd_pcm_runtime_t *runtime = substream->runtime;
James Courtier-Dutton6e4abc42005-03-26 19:35:29 +0100244 //emu10k1_pcm_t *epcm = runtime->private_data;
245 emu->p16v_voices[substream->pcm->device - emu->p16v_device_offset].use=0;
246 /* FIXME: maybe zero others */
247 return 0;
248}
249
250/* close callback */
251static int snd_p16v_pcm_close_capture(snd_pcm_substream_t *substream)
252{
253 emu10k1_t *emu = snd_pcm_substream_chip(substream);
254 //snd_pcm_runtime_t *runtime = substream->runtime;
255 //emu10k1_pcm_t *epcm = runtime->private_data;
256 emu->p16v_capture_voice.use=0;
257 /* FIXME: maybe zero others */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 return 0;
259}
260
261static int snd_p16v_pcm_open_playback_front(snd_pcm_substream_t *substream)
262{
263 return snd_p16v_pcm_open_playback_channel(substream, PCM_FRONT_CHANNEL);
264}
265
James Courtier-Dutton6e4abc42005-03-26 19:35:29 +0100266static int snd_p16v_pcm_open_capture(snd_pcm_substream_t *substream)
267{
268 // Only using channel 0 for now, but the card has 2 channels.
269 return snd_p16v_pcm_open_capture_channel(substream, 0);
270}
271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272/* hw_params callback */
273static int snd_p16v_pcm_hw_params_playback(snd_pcm_substream_t *substream,
274 snd_pcm_hw_params_t * hw_params)
275{
276 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 result = snd_pcm_lib_malloc_pages(substream,
278 params_buffer_bytes(hw_params));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 return result;
280}
281
James Courtier-Dutton6e4abc42005-03-26 19:35:29 +0100282/* hw_params callback */
283static int snd_p16v_pcm_hw_params_capture(snd_pcm_substream_t *substream,
284 snd_pcm_hw_params_t * hw_params)
285{
286 int result;
287 result = snd_pcm_lib_malloc_pages(substream,
288 params_buffer_bytes(hw_params));
289 return result;
290}
291
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293/* hw_free callback */
294static int snd_p16v_pcm_hw_free_playback(snd_pcm_substream_t *substream)
295{
296 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 result = snd_pcm_lib_free_pages(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 return result;
299}
300
James Courtier-Dutton6e4abc42005-03-26 19:35:29 +0100301/* hw_free callback */
302static int snd_p16v_pcm_hw_free_capture(snd_pcm_substream_t *substream)
303{
304 int result;
305 result = snd_pcm_lib_free_pages(substream);
306 return result;
307}
308
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310/* prepare playback callback */
311static int snd_p16v_pcm_prepare_playback(snd_pcm_substream_t *substream)
312{
313 emu10k1_t *emu = snd_pcm_substream_chip(substream);
314 snd_pcm_runtime_t *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 int channel = substream->pcm->device - emu->p16v_device_offset;
316 u32 *table_base = (u32 *)(emu->p16v_buffer.area+(8*16*channel));
317 u32 period_size_bytes = frames_to_bytes(runtime, runtime->period_size);
318 int i;
319 u32 tmp;
320
321 //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));
322 //snd_printk("dma_addr=%x, dma_area=%p, table_base=%p\n",runtime->dma_addr, runtime->dma_area, table_base);
323 //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);
324 tmp = snd_emu10k1_ptr_read(emu, A_SPDIF_SAMPLERATE, channel);
325 switch (runtime->rate) {
326 case 44100:
James Courtier-Dutton001f7582005-04-09 23:38:25 +0200327 snd_emu10k1_ptr_write(emu, A_SPDIF_SAMPLERATE, channel, (tmp & ~0xe0e0) | 0x8080);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 break;
329 case 96000:
James Courtier-Dutton001f7582005-04-09 23:38:25 +0200330 snd_emu10k1_ptr_write(emu, A_SPDIF_SAMPLERATE, channel, (tmp & ~0xe0e0) | 0x4040);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 break;
332 case 192000:
James Courtier-Dutton001f7582005-04-09 23:38:25 +0200333 snd_emu10k1_ptr_write(emu, A_SPDIF_SAMPLERATE, channel, (tmp & ~0xe0e0) | 0x2020);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 break;
James Courtier-Dutton001f7582005-04-09 23:38:25 +0200335 case 48000:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 default:
James Courtier-Dutton001f7582005-04-09 23:38:25 +0200337 snd_emu10k1_ptr_write(emu, A_SPDIF_SAMPLERATE, channel, (tmp & ~0xe0e0) | 0x0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 break;
339 }
340 /* FIXME: Check emu->buffer.size before actually writing to it. */
James Courtier-Dutton6e4abc42005-03-26 19:35:29 +0100341 for(i=0; i < runtime->periods; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 table_base[i*2]=runtime->dma_addr+(i*period_size_bytes);
343 table_base[(i*2)+1]=period_size_bytes<<16;
344 }
345
346 snd_emu10k1_ptr20_write(emu, PLAYBACK_LIST_ADDR, channel, emu->p16v_buffer.addr+(8*16*channel));
347 snd_emu10k1_ptr20_write(emu, PLAYBACK_LIST_SIZE, channel, (runtime->periods - 1) << 19);
348 snd_emu10k1_ptr20_write(emu, PLAYBACK_LIST_PTR, channel, 0);
349 snd_emu10k1_ptr20_write(emu, PLAYBACK_DMA_ADDR, channel, runtime->dma_addr);
James Courtier-Dutton310bacd2005-04-10 00:00:24 +0200350 //snd_emu10k1_ptr20_write(emu, PLAYBACK_PERIOD_SIZE, channel, frames_to_bytes(runtime, runtime->period_size)<<16); // buffer size in bytes
351 snd_emu10k1_ptr20_write(emu, PLAYBACK_PERIOD_SIZE, channel, 0); // buffer size in bytes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 snd_emu10k1_ptr20_write(emu, PLAYBACK_POINTER, channel, 0);
353 snd_emu10k1_ptr20_write(emu, 0x07, channel, 0x0);
354 snd_emu10k1_ptr20_write(emu, 0x08, channel, 0);
355
356 return 0;
357}
358
James Courtier-Dutton6e4abc42005-03-26 19:35:29 +0100359/* prepare capture callback */
360static int snd_p16v_pcm_prepare_capture(snd_pcm_substream_t *substream)
361{
362 emu10k1_t *emu = snd_pcm_substream_chip(substream);
363 snd_pcm_runtime_t *runtime = substream->runtime;
364 int channel = substream->pcm->device - emu->p16v_device_offset;
365 //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));
366 snd_emu10k1_ptr20_write(emu, 0x13, channel, 0);
367 snd_emu10k1_ptr20_write(emu, CAPTURE_DMA_ADDR, channel, runtime->dma_addr);
368 snd_emu10k1_ptr20_write(emu, CAPTURE_BUFFER_SIZE, channel, frames_to_bytes(runtime, runtime->buffer_size)<<16); // buffer size in bytes
369 snd_emu10k1_ptr20_write(emu, CAPTURE_POINTER, channel, 0);
370 //snd_emu10k1_ptr20_write(emu, CAPTURE_SOURCE, 0x0, 0x333300e4); /* Select MIC or Line in */
371 //snd_emu10k1_ptr20_write(emu, EXTENDED_INT_MASK, 0, snd_emu10k1_ptr20_read(emu, EXTENDED_INT_MASK, 0) | (0x110000<<channel));
372
373 return 0;
374}
375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376static void snd_p16v_intr_enable(emu10k1_t *emu, unsigned int intrenb)
377{
378 unsigned long flags;
379 unsigned int enable;
380
381 spin_lock_irqsave(&emu->emu_lock, flags);
382 enable = inl(emu->port + INTE2) | intrenb;
383 outl(enable, emu->port + INTE2);
384 spin_unlock_irqrestore(&emu->emu_lock, flags);
385}
386
387static void snd_p16v_intr_disable(emu10k1_t *emu, unsigned int intrenb)
388{
389 unsigned long flags;
390 unsigned int disable;
391
392 spin_lock_irqsave(&emu->emu_lock, flags);
393 disable = inl(emu->port + INTE2) & (~intrenb);
394 outl(disable, emu->port + INTE2);
395 spin_unlock_irqrestore(&emu->emu_lock, flags);
396}
397
398/* trigger_playback callback */
399static int snd_p16v_pcm_trigger_playback(snd_pcm_substream_t *substream,
400 int cmd)
401{
402 emu10k1_t *emu = snd_pcm_substream_chip(substream);
403 snd_pcm_runtime_t *runtime;
404 emu10k1_pcm_t *epcm;
405 int channel;
406 int result = 0;
407 struct list_head *pos;
408 snd_pcm_substream_t *s;
409 u32 basic = 0;
410 u32 inte = 0;
411 int running=0;
412
413 switch (cmd) {
414 case SNDRV_PCM_TRIGGER_START:
415 running=1;
416 break;
417 case SNDRV_PCM_TRIGGER_STOP:
418 default:
419 running=0;
420 break;
421 }
422 snd_pcm_group_for_each(pos, substream) {
423 s = snd_pcm_group_substream_entry(pos);
424 runtime = s->runtime;
425 epcm = runtime->private_data;
426 channel = substream->pcm->device-emu->p16v_device_offset;
427 //snd_printk("p16v channel=%d\n",channel);
428 epcm->running = running;
429 basic |= (0x1<<channel);
430 inte |= (INTE2_PLAYBACK_CH_0_LOOP<<channel);
431 snd_pcm_trigger_done(s, substream);
432 }
433 //snd_printk("basic=0x%x, inte=0x%x\n",basic, inte);
434
435 switch (cmd) {
436 case SNDRV_PCM_TRIGGER_START:
437 snd_p16v_intr_enable(emu, inte);
438 snd_emu10k1_ptr20_write(emu, BASIC_INTERRUPT, 0, snd_emu10k1_ptr20_read(emu, BASIC_INTERRUPT, 0)| (basic));
439 break;
440 case SNDRV_PCM_TRIGGER_STOP:
441 snd_emu10k1_ptr20_write(emu, BASIC_INTERRUPT, 0, snd_emu10k1_ptr20_read(emu, BASIC_INTERRUPT, 0) & ~(basic));
442 snd_p16v_intr_disable(emu, inte);
443 break;
444 default:
445 result = -EINVAL;
446 break;
447 }
448 return result;
449}
450
James Courtier-Dutton6e4abc42005-03-26 19:35:29 +0100451/* trigger_capture callback */
452static int snd_p16v_pcm_trigger_capture(snd_pcm_substream_t *substream,
453 int cmd)
454{
455 emu10k1_t *emu = snd_pcm_substream_chip(substream);
456 snd_pcm_runtime_t *runtime = substream->runtime;
457 emu10k1_pcm_t *epcm = runtime->private_data;
458 int channel = 0;
459 int result = 0;
460 u32 inte = INTE2_CAPTURE_CH_0_LOOP | INTE2_CAPTURE_CH_0_HALF_LOOP;
461
462 switch (cmd) {
463 case SNDRV_PCM_TRIGGER_START:
464 snd_p16v_intr_enable(emu, inte);
465 snd_emu10k1_ptr20_write(emu, BASIC_INTERRUPT, 0, snd_emu10k1_ptr20_read(emu, BASIC_INTERRUPT, 0)|(0x100<<channel));
466 epcm->running = 1;
467 break;
468 case SNDRV_PCM_TRIGGER_STOP:
469 snd_emu10k1_ptr20_write(emu, BASIC_INTERRUPT, 0, snd_emu10k1_ptr20_read(emu, BASIC_INTERRUPT, 0) & ~(0x100<<channel));
470 snd_p16v_intr_disable(emu, inte);
471 //snd_emu10k1_ptr20_write(emu, EXTENDED_INT_MASK, 0, snd_emu10k1_ptr20_read(emu, EXTENDED_INT_MASK, 0) & ~(0x110000<<channel));
472 epcm->running = 0;
473 break;
474 default:
475 result = -EINVAL;
476 break;
477 }
478 return result;
479}
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481/* pointer_playback callback */
482static snd_pcm_uframes_t
483snd_p16v_pcm_pointer_playback(snd_pcm_substream_t *substream)
484{
485 emu10k1_t *emu = snd_pcm_substream_chip(substream);
486 snd_pcm_runtime_t *runtime = substream->runtime;
487 emu10k1_pcm_t *epcm = runtime->private_data;
488 snd_pcm_uframes_t ptr, ptr1, ptr2,ptr3,ptr4 = 0;
489 int channel = substream->pcm->device - emu->p16v_device_offset;
490 if (!epcm->running)
491 return 0;
492
493 ptr3 = snd_emu10k1_ptr20_read(emu, PLAYBACK_LIST_PTR, channel);
494 ptr1 = snd_emu10k1_ptr20_read(emu, PLAYBACK_POINTER, channel);
495 ptr4 = snd_emu10k1_ptr20_read(emu, PLAYBACK_LIST_PTR, channel);
496 if (ptr3 != ptr4) ptr1 = snd_emu10k1_ptr20_read(emu, PLAYBACK_POINTER, channel);
497 ptr2 = bytes_to_frames(runtime, ptr1);
498 ptr2+= (ptr4 >> 3) * runtime->period_size;
499 ptr=ptr2;
500 if (ptr >= runtime->buffer_size)
501 ptr -= runtime->buffer_size;
502
503 return ptr;
504}
505
James Courtier-Dutton6e4abc42005-03-26 19:35:29 +0100506/* pointer_capture callback */
507static snd_pcm_uframes_t
508snd_p16v_pcm_pointer_capture(snd_pcm_substream_t *substream)
509{
510 emu10k1_t *emu = snd_pcm_substream_chip(substream);
511 snd_pcm_runtime_t *runtime = substream->runtime;
512 emu10k1_pcm_t *epcm = runtime->private_data;
513 snd_pcm_uframes_t ptr, ptr1, ptr2 = 0;
514 int channel = 0;
515
516 if (!epcm->running)
517 return 0;
518
519 ptr1 = snd_emu10k1_ptr20_read(emu, CAPTURE_POINTER, channel);
520 ptr2 = bytes_to_frames(runtime, ptr1);
521 ptr=ptr2;
522 if (ptr >= runtime->buffer_size) {
523 ptr -= runtime->buffer_size;
524 printk("buffer capture limited!\n");
525 }
526 //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);
527
528 return ptr;
529}
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531/* operators */
532static snd_pcm_ops_t snd_p16v_playback_front_ops = {
533 .open = snd_p16v_pcm_open_playback_front,
534 .close = snd_p16v_pcm_close_playback,
535 .ioctl = snd_pcm_lib_ioctl,
536 .hw_params = snd_p16v_pcm_hw_params_playback,
537 .hw_free = snd_p16v_pcm_hw_free_playback,
538 .prepare = snd_p16v_pcm_prepare_playback,
539 .trigger = snd_p16v_pcm_trigger_playback,
540 .pointer = snd_p16v_pcm_pointer_playback,
541};
542
James Courtier-Dutton6e4abc42005-03-26 19:35:29 +0100543static snd_pcm_ops_t snd_p16v_capture_ops = {
544 .open = snd_p16v_pcm_open_capture,
545 .close = snd_p16v_pcm_close_capture,
546 .ioctl = snd_pcm_lib_ioctl,
547 .hw_params = snd_p16v_pcm_hw_params_capture,
548 .hw_free = snd_p16v_pcm_hw_free_capture,
549 .prepare = snd_p16v_pcm_prepare_capture,
550 .trigger = snd_p16v_pcm_trigger_capture,
551 .pointer = snd_p16v_pcm_pointer_capture,
552};
553
554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555int snd_p16v_free(emu10k1_t *chip)
556{
557 // release the data
558 if (chip->p16v_buffer.area) {
559 snd_dma_free_pages(&chip->p16v_buffer);
560 //snd_printk("period lables free: %p\n", &chip->p16v_buffer);
561 }
562 return 0;
563}
564
565static void snd_p16v_pcm_free(snd_pcm_t *pcm)
566{
567 emu10k1_t *emu = pcm->private_data;
568 //snd_printk("snd_p16v_pcm_free pcm: called\n");
569 snd_pcm_lib_preallocate_free_for_all(pcm);
570 emu->pcm = NULL;
571}
572
573int snd_p16v_pcm(emu10k1_t *emu, int device, snd_pcm_t **rpcm)
574{
575 snd_pcm_t *pcm;
576 snd_pcm_substream_t *substream;
577 int err;
James Courtier-Dutton6e4abc42005-03-26 19:35:29 +0100578 int capture=1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 //snd_printk("snd_p16v_pcm called. device=%d\n", device);
581 emu->p16v_device_offset = device;
582 if (rpcm)
583 *rpcm = NULL;
James Courtier-Dutton6e4abc42005-03-26 19:35:29 +0100584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 if ((err = snd_pcm_new(emu->card, "p16v", device, 1, capture, &pcm)) < 0)
586 return err;
587
588 pcm->private_data = emu;
589 pcm->private_free = snd_p16v_pcm_free;
James Courtier-Dutton6e4abc42005-03-26 19:35:29 +0100590 // Single playback 8 channel device.
591 // Single capture 2 channel device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_p16v_playback_front_ops);
James Courtier-Dutton6e4abc42005-03-26 19:35:29 +0100593 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_p16v_capture_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 pcm->info_flags = 0;
596 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
597 strcpy(pcm->name, "p16v");
598 emu->pcm = pcm;
599
600 for(substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
601 substream;
602 substream = substream->next) {
603 if ((err = snd_pcm_lib_preallocate_pages(substream,
604 SNDRV_DMA_TYPE_DEV,
605 snd_dma_pci_data(emu->pci),
James Courtier-Dutton310bacd2005-04-10 00:00:24 +0200606 ((65536 - 64) * 8), ((65536 - 64) * 8))) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 return err;
608 //snd_printk("preallocate playback substream: err=%d\n", err);
609 }
610
611 for (substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
612 substream;
613 substream = substream->next) {
614 if ((err = snd_pcm_lib_preallocate_pages(substream,
615 SNDRV_DMA_TYPE_DEV,
616 snd_dma_pci_data(emu->pci),
617 64*1024, 64*1024)) < 0)
618 return err;
619 //snd_printk("preallocate capture substream: err=%d\n", err);
620 }
621
622 if (rpcm)
623 *rpcm = pcm;
624
625 return 0;
626}
627
628static int snd_p16v_volume_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
629{
630 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
631 uinfo->count = 2;
632 uinfo->value.integer.min = 0;
633 uinfo->value.integer.max = 255;
634 return 0;
635}
636
637static int snd_p16v_volume_get(snd_kcontrol_t * kcontrol,
638 snd_ctl_elem_value_t * ucontrol, int reg, int high_low)
639{
640 emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
641 u32 value;
642
643 value = snd_emu10k1_ptr20_read(emu, reg, high_low);
644 if (high_low == 1) {
645 ucontrol->value.integer.value[0] = 0xff - ((value >> 24) & 0xff); /* Left */
646 ucontrol->value.integer.value[1] = 0xff - ((value >> 16) & 0xff); /* Right */
647 } else {
648 ucontrol->value.integer.value[0] = 0xff - ((value >> 8) & 0xff); /* Left */
649 ucontrol->value.integer.value[1] = 0xff - ((value >> 0) & 0xff); /* Right */
650 }
651 return 0;
652}
653
654static int snd_p16v_volume_get_spdif_front(snd_kcontrol_t * kcontrol,
655 snd_ctl_elem_value_t * ucontrol)
656{
657 int high_low = 0;
658 int reg = PLAYBACK_VOLUME_MIXER7;
659 return snd_p16v_volume_get(kcontrol, ucontrol, reg, high_low);
660}
661
662static int snd_p16v_volume_get_spdif_center_lfe(snd_kcontrol_t * kcontrol,
663 snd_ctl_elem_value_t * ucontrol)
664{
665 int high_low = 1;
666 int reg = PLAYBACK_VOLUME_MIXER7;
667 return snd_p16v_volume_get(kcontrol, ucontrol, reg, high_low);
668}
669static int snd_p16v_volume_get_spdif_unknown(snd_kcontrol_t * kcontrol,
670 snd_ctl_elem_value_t * ucontrol)
671{
672 int high_low = 0;
673 int reg = PLAYBACK_VOLUME_MIXER8;
674 return snd_p16v_volume_get(kcontrol, ucontrol, reg, high_low);
675}
676static int snd_p16v_volume_get_spdif_rear(snd_kcontrol_t * kcontrol,
677 snd_ctl_elem_value_t * ucontrol)
678{
679 int high_low = 1;
680 int reg = PLAYBACK_VOLUME_MIXER8;
681 return snd_p16v_volume_get(kcontrol, ucontrol, reg, high_low);
682}
683
684static int snd_p16v_volume_get_analog_front(snd_kcontrol_t * kcontrol,
685 snd_ctl_elem_value_t * ucontrol)
686{
687 int high_low = 0;
688 int reg = PLAYBACK_VOLUME_MIXER9;
689 return snd_p16v_volume_get(kcontrol, ucontrol, reg, high_low);
690}
691
692static int snd_p16v_volume_get_analog_center_lfe(snd_kcontrol_t * kcontrol,
693 snd_ctl_elem_value_t * ucontrol)
694{
695 int high_low = 1;
696 int reg = PLAYBACK_VOLUME_MIXER9;
697 return snd_p16v_volume_get(kcontrol, ucontrol, reg, high_low);
698}
699static int snd_p16v_volume_get_analog_rear(snd_kcontrol_t * kcontrol,
700 snd_ctl_elem_value_t * ucontrol)
701{
702 int high_low = 1;
703 int reg = PLAYBACK_VOLUME_MIXER10;
704 return snd_p16v_volume_get(kcontrol, ucontrol, reg, high_low);
705}
706
707static int snd_p16v_volume_get_analog_unknown(snd_kcontrol_t * kcontrol,
708 snd_ctl_elem_value_t * ucontrol)
709{
710 int high_low = 0;
711 int reg = PLAYBACK_VOLUME_MIXER10;
712 return snd_p16v_volume_get(kcontrol, ucontrol, reg, high_low);
713}
714
715static int snd_p16v_volume_put(snd_kcontrol_t * kcontrol,
716 snd_ctl_elem_value_t * ucontrol, int reg, int high_low)
717{
718 emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
719 u32 value;
720 value = snd_emu10k1_ptr20_read(emu, reg, 0);
721 //value = value & 0xffff;
722 if (high_low == 1) {
723 value &= 0xffff;
724 value = value | ((0xff - ucontrol->value.integer.value[0]) << 24) | ((0xff - ucontrol->value.integer.value[1]) << 16);
725 } else {
726 value &= 0xffff0000;
727 value = value | ((0xff - ucontrol->value.integer.value[0]) << 8) | ((0xff - ucontrol->value.integer.value[1]) );
728 }
729 snd_emu10k1_ptr20_write(emu, reg, 0, value);
730 return 1;
731}
732
733static int snd_p16v_volume_put_spdif_front(snd_kcontrol_t * kcontrol,
734 snd_ctl_elem_value_t * ucontrol)
735{
736 int high_low = 0;
737 int reg = PLAYBACK_VOLUME_MIXER7;
738 return snd_p16v_volume_put(kcontrol, ucontrol, reg, high_low);
739}
740
741static int snd_p16v_volume_put_spdif_center_lfe(snd_kcontrol_t * kcontrol,
742 snd_ctl_elem_value_t * ucontrol)
743{
744 int high_low = 1;
745 int reg = PLAYBACK_VOLUME_MIXER7;
746 return snd_p16v_volume_put(kcontrol, ucontrol, reg, high_low);
747}
748
749static int snd_p16v_volume_put_spdif_unknown(snd_kcontrol_t * kcontrol,
750 snd_ctl_elem_value_t * ucontrol)
751{
752 int high_low = 0;
753 int reg = PLAYBACK_VOLUME_MIXER8;
754 return snd_p16v_volume_put(kcontrol, ucontrol, reg, high_low);
755}
756
757static int snd_p16v_volume_put_spdif_rear(snd_kcontrol_t * kcontrol,
758 snd_ctl_elem_value_t * ucontrol)
759{
760 int high_low = 1;
761 int reg = PLAYBACK_VOLUME_MIXER8;
762 return snd_p16v_volume_put(kcontrol, ucontrol, reg, high_low);
763}
764
765static int snd_p16v_volume_put_analog_front(snd_kcontrol_t * kcontrol,
766 snd_ctl_elem_value_t * ucontrol)
767{
768 int high_low = 0;
769 int reg = PLAYBACK_VOLUME_MIXER9;
770 return snd_p16v_volume_put(kcontrol, ucontrol, reg, high_low);
771}
772
773static int snd_p16v_volume_put_analog_center_lfe(snd_kcontrol_t * kcontrol,
774 snd_ctl_elem_value_t * ucontrol)
775{
776 int high_low = 1;
777 int reg = PLAYBACK_VOLUME_MIXER9;
778 return snd_p16v_volume_put(kcontrol, ucontrol, reg, high_low);
779}
780
781static int snd_p16v_volume_put_analog_rear(snd_kcontrol_t * kcontrol,
782 snd_ctl_elem_value_t * ucontrol)
783{
784 int high_low = 1;
785 int reg = PLAYBACK_VOLUME_MIXER10;
786 return snd_p16v_volume_put(kcontrol, ucontrol, reg, high_low);
787}
788
789static int snd_p16v_volume_put_analog_unknown(snd_kcontrol_t * kcontrol,
790 snd_ctl_elem_value_t * ucontrol)
791{
792 int high_low = 0;
793 int reg = PLAYBACK_VOLUME_MIXER10;
794 return snd_p16v_volume_put(kcontrol, ucontrol, reg, high_low);
795}
796
797static snd_kcontrol_new_t snd_p16v_volume_control_analog_front =
798{
799 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
800 .name = "HD Analog Front Volume",
801 .info = snd_p16v_volume_info,
802 .get = snd_p16v_volume_get_analog_front,
803 .put = snd_p16v_volume_put_analog_front
804};
805
806static snd_kcontrol_new_t snd_p16v_volume_control_analog_center_lfe =
807{
808 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
809 .name = "HD Analog Center/LFE Volume",
810 .info = snd_p16v_volume_info,
811 .get = snd_p16v_volume_get_analog_center_lfe,
812 .put = snd_p16v_volume_put_analog_center_lfe
813};
814
815static snd_kcontrol_new_t snd_p16v_volume_control_analog_unknown =
816{
817 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
818 .name = "HD Analog Unknown Volume",
819 .info = snd_p16v_volume_info,
820 .get = snd_p16v_volume_get_analog_unknown,
821 .put = snd_p16v_volume_put_analog_unknown
822};
823
824static snd_kcontrol_new_t snd_p16v_volume_control_analog_rear =
825{
826 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
827 .name = "HD Analog Rear Volume",
828 .info = snd_p16v_volume_info,
829 .get = snd_p16v_volume_get_analog_rear,
830 .put = snd_p16v_volume_put_analog_rear
831};
832
833static snd_kcontrol_new_t snd_p16v_volume_control_spdif_front =
834{
835 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
836 .name = "HD SPDIF Front Volume",
837 .info = snd_p16v_volume_info,
838 .get = snd_p16v_volume_get_spdif_front,
839 .put = snd_p16v_volume_put_spdif_front
840};
841
842static snd_kcontrol_new_t snd_p16v_volume_control_spdif_center_lfe =
843{
844 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
845 .name = "HD SPDIF Center/LFE Volume",
846 .info = snd_p16v_volume_info,
847 .get = snd_p16v_volume_get_spdif_center_lfe,
848 .put = snd_p16v_volume_put_spdif_center_lfe
849};
850
851static snd_kcontrol_new_t snd_p16v_volume_control_spdif_unknown =
852{
853 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
854 .name = "HD SPDIF Unknown Volume",
855 .info = snd_p16v_volume_info,
856 .get = snd_p16v_volume_get_spdif_unknown,
857 .put = snd_p16v_volume_put_spdif_unknown
858};
859
860static snd_kcontrol_new_t snd_p16v_volume_control_spdif_rear =
861{
862 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
863 .name = "HD SPDIF Rear Volume",
864 .info = snd_p16v_volume_info,
865 .get = snd_p16v_volume_get_spdif_rear,
866 .put = snd_p16v_volume_put_spdif_rear
867};
868
James Courtier-Dutton6e4abc42005-03-26 19:35:29 +0100869static int snd_p16v_capture_source_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
870{
871 static char *texts[8] = { "SPDIF", "I2S", "SRC48", "SRCMulti_SPDIF", "SRCMulti_I2S", "CDIF", "FX", "AC97" };
872
873 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
874 uinfo->count = 1;
875 uinfo->value.enumerated.items = 8;
876 if (uinfo->value.enumerated.item > 7)
877 uinfo->value.enumerated.item = 7;
878 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
879 return 0;
880}
881
882static int snd_p16v_capture_source_get(snd_kcontrol_t * kcontrol,
883 snd_ctl_elem_value_t * ucontrol)
884{
885 emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
886
887 ucontrol->value.enumerated.item[0] = emu->p16v_capture_source;
888 return 0;
889}
890
891static int snd_p16v_capture_source_put(snd_kcontrol_t * kcontrol,
892 snd_ctl_elem_value_t * ucontrol)
893{
894 emu10k1_t *emu = snd_kcontrol_chip(kcontrol);
895 unsigned int val;
896 int change = 0;
897 u32 mask;
898 u32 source;
899
900 val = ucontrol->value.enumerated.item[0] ;
901 change = (emu->p16v_capture_source != val);
902 if (change) {
903 emu->p16v_capture_source = val;
904 source = (val << 28) | (val << 24) | (val << 20) | (val << 16);
905 mask = snd_emu10k1_ptr20_read(emu, BASIC_INTERRUPT, 0) & 0xffff;
906 snd_emu10k1_ptr20_write(emu, BASIC_INTERRUPT, 0, source | mask);
907 }
908 return change;
909}
910
911static snd_kcontrol_new_t snd_p16v_capture_source __devinitdata =
912{
913 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
914 .name = "HD Capture source",
915 .info = snd_p16v_capture_source_info,
916 .get = snd_p16v_capture_source_get,
917 .put = snd_p16v_capture_source_put
918};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919int snd_p16v_mixer(emu10k1_t *emu)
920{
921 int err;
922 snd_kcontrol_t *kctl;
923 snd_card_t *card = emu->card;
924 if ((kctl = snd_ctl_new1(&snd_p16v_volume_control_analog_front, emu)) == NULL)
925 return -ENOMEM;
926 if ((err = snd_ctl_add(card, kctl)))
927 return err;
928 if ((kctl = snd_ctl_new1(&snd_p16v_volume_control_analog_rear, emu)) == NULL)
929 return -ENOMEM;
930 if ((err = snd_ctl_add(card, kctl)))
931 return err;
932 if ((kctl = snd_ctl_new1(&snd_p16v_volume_control_analog_center_lfe, emu)) == NULL)
933 return -ENOMEM;
934 if ((err = snd_ctl_add(card, kctl)))
935 return err;
936 if ((kctl = snd_ctl_new1(&snd_p16v_volume_control_analog_unknown, emu)) == NULL)
937 return -ENOMEM;
938 if ((err = snd_ctl_add(card, kctl)))
939 return err;
940 if ((kctl = snd_ctl_new1(&snd_p16v_volume_control_spdif_front, emu)) == NULL)
941 return -ENOMEM;
942 if ((err = snd_ctl_add(card, kctl)))
943 return err;
944 if ((kctl = snd_ctl_new1(&snd_p16v_volume_control_spdif_rear, emu)) == NULL)
945 return -ENOMEM;
946 if ((err = snd_ctl_add(card, kctl)))
947 return err;
948 if ((kctl = snd_ctl_new1(&snd_p16v_volume_control_spdif_center_lfe, emu)) == NULL)
949 return -ENOMEM;
950 if ((err = snd_ctl_add(card, kctl)))
951 return err;
952 if ((kctl = snd_ctl_new1(&snd_p16v_volume_control_spdif_unknown, emu)) == NULL)
953 return -ENOMEM;
954 if ((err = snd_ctl_add(card, kctl)))
955 return err;
James Courtier-Dutton6e4abc42005-03-26 19:35:29 +0100956 if ((kctl = snd_ctl_new1(&snd_p16v_capture_source, emu)) == NULL)
957 return -ENOMEM;
958 if ((err = snd_ctl_add(card, kctl)))
959 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 return 0;
961}
962