1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
4 * Creative Labs, Inc.
5 * Routines for control of EMU10K1 chips / PCM routines
6 * Multichannel PCM support Copyright (c) Lee Revell <rlrevell@joe-job.com>
7 *
8 * BUGS:
9 * --
10 *
11 * TODO:
12 * --
13 */
14
15 #include <linux/pci.h>
16 #include <linux/delay.h>
17 #include <linux/slab.h>
18 #include <linux/time.h>
19 #include <linux/init.h>
20 #include <sound/core.h>
21 #include <sound/emu10k1.h>
22
snd_emu10k1_pcm_interrupt(struct snd_emu10k1 * emu,struct snd_emu10k1_voice * voice)23 static void snd_emu10k1_pcm_interrupt(struct snd_emu10k1 *emu,
24 struct snd_emu10k1_voice *voice)
25 {
26 struct snd_emu10k1_pcm *epcm;
27
28 epcm = voice->epcm;
29 if (!epcm)
30 return;
31 if (epcm->substream == NULL)
32 return;
33 #if 0
34 dev_dbg(emu->card->dev,
35 "IRQ: position = 0x%x, period = 0x%x, size = 0x%x\n",
36 epcm->substream->runtime->hw->pointer(emu, epcm->substream),
37 snd_pcm_lib_period_bytes(epcm->substream),
38 snd_pcm_lib_buffer_bytes(epcm->substream));
39 #endif
40 snd_pcm_period_elapsed(epcm->substream);
41 }
42
snd_emu10k1_pcm_ac97adc_interrupt(struct snd_emu10k1 * emu,unsigned int status)43 static void snd_emu10k1_pcm_ac97adc_interrupt(struct snd_emu10k1 *emu,
44 unsigned int status)
45 {
46 #if 0
47 if (status & IPR_ADCBUFHALFFULL) {
48 if (emu->pcm_capture_substream->runtime->mode == SNDRV_PCM_MODE_FRAME)
49 return;
50 }
51 #endif
52 snd_pcm_period_elapsed(emu->pcm_capture_substream);
53 }
54
snd_emu10k1_pcm_ac97mic_interrupt(struct snd_emu10k1 * emu,unsigned int status)55 static void snd_emu10k1_pcm_ac97mic_interrupt(struct snd_emu10k1 *emu,
56 unsigned int status)
57 {
58 #if 0
59 if (status & IPR_MICBUFHALFFULL) {
60 if (emu->pcm_capture_mic_substream->runtime->mode == SNDRV_PCM_MODE_FRAME)
61 return;
62 }
63 #endif
64 snd_pcm_period_elapsed(emu->pcm_capture_mic_substream);
65 }
66
snd_emu10k1_pcm_efx_interrupt(struct snd_emu10k1 * emu,unsigned int status)67 static void snd_emu10k1_pcm_efx_interrupt(struct snd_emu10k1 *emu,
68 unsigned int status)
69 {
70 #if 0
71 if (status & IPR_EFXBUFHALFFULL) {
72 if (emu->pcm_capture_efx_substream->runtime->mode == SNDRV_PCM_MODE_FRAME)
73 return;
74 }
75 #endif
76 snd_pcm_period_elapsed(emu->pcm_capture_efx_substream);
77 }
78
snd_emu10k1_efx_playback_pointer(struct snd_pcm_substream * substream)79 static snd_pcm_uframes_t snd_emu10k1_efx_playback_pointer(struct snd_pcm_substream *substream)
80 {
81 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
82 struct snd_pcm_runtime *runtime = substream->runtime;
83 struct snd_emu10k1_pcm *epcm = runtime->private_data;
84 unsigned int ptr;
85
86 if (!epcm->running)
87 return 0;
88 ptr = snd_emu10k1_ptr_read(emu, CCCA, epcm->voices[0]->number) & 0x00ffffff;
89 ptr += runtime->buffer_size;
90 ptr -= epcm->ccca_start_addr;
91 ptr %= runtime->buffer_size;
92
93 return ptr;
94 }
95
snd_emu10k1_pcm_channel_alloc(struct snd_emu10k1_pcm * epcm,int voices)96 static int snd_emu10k1_pcm_channel_alloc(struct snd_emu10k1_pcm * epcm, int voices)
97 {
98 int err, i;
99
100 if (epcm->voices[1] != NULL && voices < 2) {
101 snd_emu10k1_voice_free(epcm->emu, epcm->voices[1]);
102 epcm->voices[1] = NULL;
103 }
104 for (i = 0; i < voices; i++) {
105 if (epcm->voices[i] == NULL)
106 break;
107 }
108 if (i == voices)
109 return 0; /* already allocated */
110
111 for (i = 0; i < ARRAY_SIZE(epcm->voices); i++) {
112 if (epcm->voices[i]) {
113 snd_emu10k1_voice_free(epcm->emu, epcm->voices[i]);
114 epcm->voices[i] = NULL;
115 }
116 }
117 err = snd_emu10k1_voice_alloc(epcm->emu,
118 epcm->type == PLAYBACK_EMUVOICE ? EMU10K1_PCM : EMU10K1_EFX,
119 voices,
120 &epcm->voices[0]);
121
122 if (err < 0)
123 return err;
124 epcm->voices[0]->epcm = epcm;
125 if (voices > 1) {
126 for (i = 1; i < voices; i++) {
127 epcm->voices[i] = &epcm->emu->voices[(epcm->voices[0]->number + i) % NUM_G];
128 epcm->voices[i]->epcm = epcm;
129 }
130 }
131 if (epcm->extra == NULL) {
132 err = snd_emu10k1_voice_alloc(epcm->emu,
133 epcm->type == PLAYBACK_EMUVOICE ? EMU10K1_PCM : EMU10K1_EFX,
134 1,
135 &epcm->extra);
136 if (err < 0) {
137 /*
138 dev_dbg(emu->card->dev, "pcm_channel_alloc: "
139 "failed extra: voices=%d, frame=%d\n",
140 voices, frame);
141 */
142 for (i = 0; i < voices; i++) {
143 snd_emu10k1_voice_free(epcm->emu, epcm->voices[i]);
144 epcm->voices[i] = NULL;
145 }
146 return err;
147 }
148 epcm->extra->epcm = epcm;
149 epcm->extra->interrupt = snd_emu10k1_pcm_interrupt;
150 }
151 return 0;
152 }
153
154 static const unsigned int capture_period_sizes[31] = {
155 384, 448, 512, 640,
156 384*2, 448*2, 512*2, 640*2,
157 384*4, 448*4, 512*4, 640*4,
158 384*8, 448*8, 512*8, 640*8,
159 384*16, 448*16, 512*16, 640*16,
160 384*32, 448*32, 512*32, 640*32,
161 384*64, 448*64, 512*64, 640*64,
162 384*128,448*128,512*128
163 };
164
165 static const struct snd_pcm_hw_constraint_list hw_constraints_capture_period_sizes = {
166 .count = 31,
167 .list = capture_period_sizes,
168 .mask = 0
169 };
170
171 static const unsigned int capture_rates[8] = {
172 8000, 11025, 16000, 22050, 24000, 32000, 44100, 48000
173 };
174
175 static const struct snd_pcm_hw_constraint_list hw_constraints_capture_rates = {
176 .count = 8,
177 .list = capture_rates,
178 .mask = 0
179 };
180
snd_emu10k1_capture_rate_reg(unsigned int rate)181 static unsigned int snd_emu10k1_capture_rate_reg(unsigned int rate)
182 {
183 switch (rate) {
184 case 8000: return ADCCR_SAMPLERATE_8;
185 case 11025: return ADCCR_SAMPLERATE_11;
186 case 16000: return ADCCR_SAMPLERATE_16;
187 case 22050: return ADCCR_SAMPLERATE_22;
188 case 24000: return ADCCR_SAMPLERATE_24;
189 case 32000: return ADCCR_SAMPLERATE_32;
190 case 44100: return ADCCR_SAMPLERATE_44;
191 case 48000: return ADCCR_SAMPLERATE_48;
192 default:
193 snd_BUG();
194 return ADCCR_SAMPLERATE_8;
195 }
196 }
197
snd_emu10k1_audigy_capture_rate_reg(unsigned int rate)198 static unsigned int snd_emu10k1_audigy_capture_rate_reg(unsigned int rate)
199 {
200 switch (rate) {
201 case 8000: return A_ADCCR_SAMPLERATE_8;
202 case 11025: return A_ADCCR_SAMPLERATE_11;
203 case 12000: return A_ADCCR_SAMPLERATE_12; /* really supported? */
204 case 16000: return ADCCR_SAMPLERATE_16;
205 case 22050: return ADCCR_SAMPLERATE_22;
206 case 24000: return ADCCR_SAMPLERATE_24;
207 case 32000: return ADCCR_SAMPLERATE_32;
208 case 44100: return ADCCR_SAMPLERATE_44;
209 case 48000: return ADCCR_SAMPLERATE_48;
210 default:
211 snd_BUG();
212 return A_ADCCR_SAMPLERATE_8;
213 }
214 }
215
emu10k1_calc_pitch_target(unsigned int rate)216 static unsigned int emu10k1_calc_pitch_target(unsigned int rate)
217 {
218 unsigned int pitch_target;
219
220 pitch_target = (rate << 8) / 375;
221 pitch_target = (pitch_target >> 1) + (pitch_target & 1);
222 return pitch_target;
223 }
224
225 #define PITCH_48000 0x00004000
226 #define PITCH_96000 0x00008000
227 #define PITCH_85000 0x00007155
228 #define PITCH_80726 0x00006ba2
229 #define PITCH_67882 0x00005a82
230 #define PITCH_57081 0x00004c1c
231
emu10k1_select_interprom(unsigned int pitch_target)232 static unsigned int emu10k1_select_interprom(unsigned int pitch_target)
233 {
234 if (pitch_target == PITCH_48000)
235 return CCCA_INTERPROM_0;
236 else if (pitch_target < PITCH_48000)
237 return CCCA_INTERPROM_1;
238 else if (pitch_target >= PITCH_96000)
239 return CCCA_INTERPROM_0;
240 else if (pitch_target >= PITCH_85000)
241 return CCCA_INTERPROM_6;
242 else if (pitch_target >= PITCH_80726)
243 return CCCA_INTERPROM_5;
244 else if (pitch_target >= PITCH_67882)
245 return CCCA_INTERPROM_4;
246 else if (pitch_target >= PITCH_57081)
247 return CCCA_INTERPROM_3;
248 else
249 return CCCA_INTERPROM_2;
250 }
251
252 /*
253 * calculate cache invalidate size
254 *
255 * stereo: channel is stereo
256 * w_16: using 16bit samples
257 *
258 * returns: cache invalidate size in samples
259 */
emu10k1_ccis(int stereo,int w_16)260 static inline int emu10k1_ccis(int stereo, int w_16)
261 {
262 if (w_16) {
263 return stereo ? 24 : 26;
264 } else {
265 return stereo ? 24*2 : 26*2;
266 }
267 }
268
snd_emu10k1_pcm_init_voice(struct snd_emu10k1 * emu,int master,int extra,struct snd_emu10k1_voice * evoice,unsigned int start_addr,unsigned int end_addr,struct snd_emu10k1_pcm_mixer * mix)269 static void snd_emu10k1_pcm_init_voice(struct snd_emu10k1 *emu,
270 int master, int extra,
271 struct snd_emu10k1_voice *evoice,
272 unsigned int start_addr,
273 unsigned int end_addr,
274 struct snd_emu10k1_pcm_mixer *mix)
275 {
276 struct snd_pcm_substream *substream = evoice->epcm->substream;
277 struct snd_pcm_runtime *runtime = substream->runtime;
278 unsigned int silent_page, tmp;
279 int voice, stereo, w_16;
280 unsigned char send_amount[8];
281 unsigned char send_routing[8];
282 unsigned long flags;
283 unsigned int pitch_target;
284 unsigned int ccis;
285
286 voice = evoice->number;
287 stereo = runtime->channels == 2;
288 w_16 = snd_pcm_format_width(runtime->format) == 16;
289
290 if (!extra && stereo) {
291 start_addr >>= 1;
292 end_addr >>= 1;
293 }
294 if (w_16) {
295 start_addr >>= 1;
296 end_addr >>= 1;
297 }
298
299 spin_lock_irqsave(&emu->reg_lock, flags);
300
301 /* volume parameters */
302 if (extra) {
303 memset(send_routing, 0, sizeof(send_routing));
304 send_routing[0] = 0;
305 send_routing[1] = 1;
306 send_routing[2] = 2;
307 send_routing[3] = 3;
308 memset(send_amount, 0, sizeof(send_amount));
309 } else {
310 /* mono, left, right (master voice = left) */
311 tmp = stereo ? (master ? 1 : 2) : 0;
312 memcpy(send_routing, &mix->send_routing[tmp][0], 8);
313 memcpy(send_amount, &mix->send_volume[tmp][0], 8);
314 }
315
316 ccis = emu10k1_ccis(stereo, w_16);
317
318 if (master) {
319 evoice->epcm->ccca_start_addr = start_addr + ccis;
320 if (extra) {
321 start_addr += ccis;
322 end_addr += ccis + emu->delay_pcm_irq;
323 }
324 if (stereo && !extra) {
325 snd_emu10k1_ptr_write(emu, CPF, voice, CPF_STEREO_MASK);
326 snd_emu10k1_ptr_write(emu, CPF, (voice + 1), CPF_STEREO_MASK);
327 } else {
328 snd_emu10k1_ptr_write(emu, CPF, voice, 0);
329 }
330 }
331
332 /* setup routing */
333 if (emu->audigy) {
334 snd_emu10k1_ptr_write(emu, A_FXRT1, voice,
335 snd_emu10k1_compose_audigy_fxrt1(send_routing));
336 snd_emu10k1_ptr_write(emu, A_FXRT2, voice,
337 snd_emu10k1_compose_audigy_fxrt2(send_routing));
338 snd_emu10k1_ptr_write(emu, A_SENDAMOUNTS, voice,
339 ((unsigned int)send_amount[4] << 24) |
340 ((unsigned int)send_amount[5] << 16) |
341 ((unsigned int)send_amount[6] << 8) |
342 (unsigned int)send_amount[7]);
343 } else
344 snd_emu10k1_ptr_write(emu, FXRT, voice,
345 snd_emu10k1_compose_send_routing(send_routing));
346 /* Stop CA */
347 /* Assumption that PT is already 0 so no harm overwriting */
348 snd_emu10k1_ptr_write(emu, PTRX, voice, (send_amount[0] << 8) | send_amount[1]);
349 snd_emu10k1_ptr_write(emu, DSL, voice, end_addr | (send_amount[3] << 24));
350 snd_emu10k1_ptr_write(emu, PSST, voice,
351 (start_addr + (extra ? emu->delay_pcm_irq : 0)) |
352 (send_amount[2] << 24));
353 if (emu->card_capabilities->emu_model)
354 pitch_target = PITCH_48000; /* Disable interpolators on emu1010 card */
355 else
356 pitch_target = emu10k1_calc_pitch_target(runtime->rate);
357 if (extra)
358 snd_emu10k1_ptr_write(emu, CCCA, voice, start_addr |
359 emu10k1_select_interprom(pitch_target) |
360 (w_16 ? 0 : CCCA_8BITSELECT));
361 else
362 snd_emu10k1_ptr_write(emu, CCCA, voice, (start_addr + ccis) |
363 emu10k1_select_interprom(pitch_target) |
364 (w_16 ? 0 : CCCA_8BITSELECT));
365 /* Clear filter delay memory */
366 snd_emu10k1_ptr_write(emu, Z1, voice, 0);
367 snd_emu10k1_ptr_write(emu, Z2, voice, 0);
368 /* invalidate maps */
369 silent_page = ((unsigned int)emu->silent_page.addr << emu->address_mode) | (emu->address_mode ? MAP_PTI_MASK1 : MAP_PTI_MASK0);
370 snd_emu10k1_ptr_write(emu, MAPA, voice, silent_page);
371 snd_emu10k1_ptr_write(emu, MAPB, voice, silent_page);
372 /* modulation envelope */
373 snd_emu10k1_ptr_write(emu, CVCF, voice, 0xffff);
374 snd_emu10k1_ptr_write(emu, VTFT, voice, 0xffff);
375 snd_emu10k1_ptr_write(emu, ATKHLDM, voice, 0);
376 snd_emu10k1_ptr_write(emu, DCYSUSM, voice, 0x007f);
377 snd_emu10k1_ptr_write(emu, LFOVAL1, voice, 0x8000);
378 snd_emu10k1_ptr_write(emu, LFOVAL2, voice, 0x8000);
379 snd_emu10k1_ptr_write(emu, FMMOD, voice, 0);
380 snd_emu10k1_ptr_write(emu, TREMFRQ, voice, 0);
381 snd_emu10k1_ptr_write(emu, FM2FRQ2, voice, 0);
382 snd_emu10k1_ptr_write(emu, ENVVAL, voice, 0x8000);
383 /* volume envelope */
384 snd_emu10k1_ptr_write(emu, ATKHLDV, voice, 0x7f7f);
385 snd_emu10k1_ptr_write(emu, ENVVOL, voice, 0x0000);
386 /* filter envelope */
387 snd_emu10k1_ptr_write(emu, PEFE_FILTERAMOUNT, voice, 0x7f);
388 /* pitch envelope */
389 snd_emu10k1_ptr_write(emu, PEFE_PITCHAMOUNT, voice, 0);
390
391 spin_unlock_irqrestore(&emu->reg_lock, flags);
392 }
393
snd_emu10k1_playback_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * hw_params)394 static int snd_emu10k1_playback_hw_params(struct snd_pcm_substream *substream,
395 struct snd_pcm_hw_params *hw_params)
396 {
397 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
398 struct snd_pcm_runtime *runtime = substream->runtime;
399 struct snd_emu10k1_pcm *epcm = runtime->private_data;
400 size_t alloc_size;
401 int err;
402
403 err = snd_emu10k1_pcm_channel_alloc(epcm, params_channels(hw_params));
404 if (err < 0)
405 return err;
406
407 alloc_size = params_buffer_bytes(hw_params);
408 if (emu->iommu_workaround)
409 alloc_size += EMUPAGESIZE;
410 err = snd_pcm_lib_malloc_pages(substream, alloc_size);
411 if (err < 0)
412 return err;
413 if (emu->iommu_workaround && runtime->dma_bytes >= EMUPAGESIZE)
414 runtime->dma_bytes -= EMUPAGESIZE;
415 if (err > 0) { /* change */
416 int mapped;
417 if (epcm->memblk != NULL)
418 snd_emu10k1_free_pages(emu, epcm->memblk);
419 epcm->memblk = snd_emu10k1_alloc_pages(emu, substream);
420 epcm->start_addr = 0;
421 if (! epcm->memblk)
422 return -ENOMEM;
423 mapped = ((struct snd_emu10k1_memblk *)epcm->memblk)->mapped_page;
424 if (mapped < 0)
425 return -ENOMEM;
426 epcm->start_addr = mapped << PAGE_SHIFT;
427 }
428 return 0;
429 }
430
snd_emu10k1_playback_hw_free(struct snd_pcm_substream * substream)431 static int snd_emu10k1_playback_hw_free(struct snd_pcm_substream *substream)
432 {
433 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
434 struct snd_pcm_runtime *runtime = substream->runtime;
435 struct snd_emu10k1_pcm *epcm;
436
437 if (runtime->private_data == NULL)
438 return 0;
439 epcm = runtime->private_data;
440 if (epcm->extra) {
441 snd_emu10k1_voice_free(epcm->emu, epcm->extra);
442 epcm->extra = NULL;
443 }
444 if (epcm->voices[1]) {
445 snd_emu10k1_voice_free(epcm->emu, epcm->voices[1]);
446 epcm->voices[1] = NULL;
447 }
448 if (epcm->voices[0]) {
449 snd_emu10k1_voice_free(epcm->emu, epcm->voices[0]);
450 epcm->voices[0] = NULL;
451 }
452 if (epcm->memblk) {
453 snd_emu10k1_free_pages(emu, epcm->memblk);
454 epcm->memblk = NULL;
455 epcm->start_addr = 0;
456 }
457 snd_pcm_lib_free_pages(substream);
458 return 0;
459 }
460
snd_emu10k1_efx_playback_hw_free(struct snd_pcm_substream * substream)461 static int snd_emu10k1_efx_playback_hw_free(struct snd_pcm_substream *substream)
462 {
463 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
464 struct snd_pcm_runtime *runtime = substream->runtime;
465 struct snd_emu10k1_pcm *epcm;
466 int i;
467
468 if (runtime->private_data == NULL)
469 return 0;
470 epcm = runtime->private_data;
471 if (epcm->extra) {
472 snd_emu10k1_voice_free(epcm->emu, epcm->extra);
473 epcm->extra = NULL;
474 }
475 for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
476 if (epcm->voices[i]) {
477 snd_emu10k1_voice_free(epcm->emu, epcm->voices[i]);
478 epcm->voices[i] = NULL;
479 }
480 }
481 if (epcm->memblk) {
482 snd_emu10k1_free_pages(emu, epcm->memblk);
483 epcm->memblk = NULL;
484 epcm->start_addr = 0;
485 }
486 snd_pcm_lib_free_pages(substream);
487 return 0;
488 }
489
snd_emu10k1_playback_prepare(struct snd_pcm_substream * substream)490 static int snd_emu10k1_playback_prepare(struct snd_pcm_substream *substream)
491 {
492 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
493 struct snd_pcm_runtime *runtime = substream->runtime;
494 struct snd_emu10k1_pcm *epcm = runtime->private_data;
495 unsigned int start_addr, end_addr;
496
497 start_addr = epcm->start_addr;
498 end_addr = snd_pcm_lib_period_bytes(substream);
499 if (runtime->channels == 2) {
500 start_addr >>= 1;
501 end_addr >>= 1;
502 }
503 end_addr += start_addr;
504 snd_emu10k1_pcm_init_voice(emu, 1, 1, epcm->extra,
505 start_addr, end_addr, NULL);
506 start_addr = epcm->start_addr;
507 end_addr = epcm->start_addr + snd_pcm_lib_buffer_bytes(substream);
508 snd_emu10k1_pcm_init_voice(emu, 1, 0, epcm->voices[0],
509 start_addr, end_addr,
510 &emu->pcm_mixer[substream->number]);
511 if (epcm->voices[1])
512 snd_emu10k1_pcm_init_voice(emu, 0, 0, epcm->voices[1],
513 start_addr, end_addr,
514 &emu->pcm_mixer[substream->number]);
515 return 0;
516 }
517
snd_emu10k1_efx_playback_prepare(struct snd_pcm_substream * substream)518 static int snd_emu10k1_efx_playback_prepare(struct snd_pcm_substream *substream)
519 {
520 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
521 struct snd_pcm_runtime *runtime = substream->runtime;
522 struct snd_emu10k1_pcm *epcm = runtime->private_data;
523 unsigned int start_addr, end_addr;
524 unsigned int channel_size;
525 int i;
526
527 start_addr = epcm->start_addr;
528 end_addr = epcm->start_addr + snd_pcm_lib_buffer_bytes(substream);
529
530 /*
531 * the kX driver leaves some space between voices
532 */
533 channel_size = ( end_addr - start_addr ) / NUM_EFX_PLAYBACK;
534
535 snd_emu10k1_pcm_init_voice(emu, 1, 1, epcm->extra,
536 start_addr, start_addr + (channel_size / 2), NULL);
537
538 /* only difference with the master voice is we use it for the pointer */
539 snd_emu10k1_pcm_init_voice(emu, 1, 0, epcm->voices[0],
540 start_addr, start_addr + channel_size,
541 &emu->efx_pcm_mixer[0]);
542
543 start_addr += channel_size;
544 for (i = 1; i < NUM_EFX_PLAYBACK; i++) {
545 snd_emu10k1_pcm_init_voice(emu, 0, 0, epcm->voices[i],
546 start_addr, start_addr + channel_size,
547 &emu->efx_pcm_mixer[i]);
548 start_addr += channel_size;
549 }
550
551 return 0;
552 }
553
554 static const struct snd_pcm_hardware snd_emu10k1_efx_playback =
555 {
556 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_NONINTERLEAVED |
557 SNDRV_PCM_INFO_BLOCK_TRANSFER |
558 SNDRV_PCM_INFO_RESUME |
559 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE),
560 .formats = SNDRV_PCM_FMTBIT_S16_LE,
561 .rates = SNDRV_PCM_RATE_48000,
562 .rate_min = 48000,
563 .rate_max = 48000,
564 .channels_min = NUM_EFX_PLAYBACK,
565 .channels_max = NUM_EFX_PLAYBACK,
566 .buffer_bytes_max = (64*1024),
567 .period_bytes_min = 64,
568 .period_bytes_max = (64*1024),
569 .periods_min = 2,
570 .periods_max = 2,
571 .fifo_size = 0,
572 };
573
snd_emu10k1_capture_prepare(struct snd_pcm_substream * substream)574 static int snd_emu10k1_capture_prepare(struct snd_pcm_substream *substream)
575 {
576 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
577 struct snd_pcm_runtime *runtime = substream->runtime;
578 struct snd_emu10k1_pcm *epcm = runtime->private_data;
579 int idx;
580
581 /* zeroing the buffer size will stop capture */
582 snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, 0);
583 switch (epcm->type) {
584 case CAPTURE_AC97ADC:
585 snd_emu10k1_ptr_write(emu, ADCCR, 0, 0);
586 break;
587 case CAPTURE_EFX:
588 if (emu->audigy) {
589 snd_emu10k1_ptr_write(emu, A_FXWC1, 0, 0);
590 snd_emu10k1_ptr_write(emu, A_FXWC2, 0, 0);
591 } else
592 snd_emu10k1_ptr_write(emu, FXWC, 0, 0);
593 break;
594 default:
595 break;
596 }
597 snd_emu10k1_ptr_write(emu, epcm->capture_ba_reg, 0, runtime->dma_addr);
598 epcm->capture_bufsize = snd_pcm_lib_buffer_bytes(substream);
599 epcm->capture_bs_val = 0;
600 for (idx = 0; idx < 31; idx++) {
601 if (capture_period_sizes[idx] == epcm->capture_bufsize) {
602 epcm->capture_bs_val = idx + 1;
603 break;
604 }
605 }
606 if (epcm->capture_bs_val == 0) {
607 snd_BUG();
608 epcm->capture_bs_val++;
609 }
610 if (epcm->type == CAPTURE_AC97ADC) {
611 epcm->capture_cr_val = emu->audigy ? A_ADCCR_LCHANENABLE : ADCCR_LCHANENABLE;
612 if (runtime->channels > 1)
613 epcm->capture_cr_val |= emu->audigy ? A_ADCCR_RCHANENABLE : ADCCR_RCHANENABLE;
614 epcm->capture_cr_val |= emu->audigy ?
615 snd_emu10k1_audigy_capture_rate_reg(runtime->rate) :
616 snd_emu10k1_capture_rate_reg(runtime->rate);
617 }
618 return 0;
619 }
620
snd_emu10k1_playback_invalidate_cache(struct snd_emu10k1 * emu,int extra,struct snd_emu10k1_voice * evoice)621 static void snd_emu10k1_playback_invalidate_cache(struct snd_emu10k1 *emu, int extra, struct snd_emu10k1_voice *evoice)
622 {
623 struct snd_pcm_runtime *runtime;
624 unsigned int voice, stereo, i, ccis, cra = 64, cs, sample;
625
626 if (evoice == NULL)
627 return;
628 runtime = evoice->epcm->substream->runtime;
629 voice = evoice->number;
630 stereo = (!extra && runtime->channels == 2);
631 sample = snd_pcm_format_width(runtime->format) == 16 ? 0 : 0x80808080;
632 ccis = emu10k1_ccis(stereo, sample == 0);
633 /* set cs to 2 * number of cache registers beside the invalidated */
634 cs = (sample == 0) ? (32-ccis) : (64-ccis+1) >> 1;
635 if (cs > 16) cs = 16;
636 for (i = 0; i < cs; i++) {
637 snd_emu10k1_ptr_write(emu, CD0 + i, voice, sample);
638 if (stereo) {
639 snd_emu10k1_ptr_write(emu, CD0 + i, voice + 1, sample);
640 }
641 }
642 /* reset cache */
643 snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice, 0);
644 snd_emu10k1_ptr_write(emu, CCR_READADDRESS, voice, cra);
645 if (stereo) {
646 snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice + 1, 0);
647 snd_emu10k1_ptr_write(emu, CCR_READADDRESS, voice + 1, cra);
648 }
649 /* fill cache */
650 snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice, ccis);
651 if (stereo) {
652 snd_emu10k1_ptr_write(emu, CCR_CACHEINVALIDSIZE, voice+1, ccis);
653 }
654 }
655
snd_emu10k1_playback_prepare_voice(struct snd_emu10k1 * emu,struct snd_emu10k1_voice * evoice,int master,int extra,struct snd_emu10k1_pcm_mixer * mix)656 static void snd_emu10k1_playback_prepare_voice(struct snd_emu10k1 *emu, struct snd_emu10k1_voice *evoice,
657 int master, int extra,
658 struct snd_emu10k1_pcm_mixer *mix)
659 {
660 struct snd_pcm_substream *substream;
661 struct snd_pcm_runtime *runtime;
662 unsigned int attn, vattn;
663 unsigned int voice, tmp;
664
665 if (evoice == NULL) /* skip second voice for mono */
666 return;
667 substream = evoice->epcm->substream;
668 runtime = substream->runtime;
669 voice = evoice->number;
670
671 attn = extra ? 0 : 0x00ff;
672 tmp = runtime->channels == 2 ? (master ? 1 : 2) : 0;
673 vattn = mix != NULL ? (mix->attn[tmp] << 16) : 0;
674 snd_emu10k1_ptr_write(emu, IFATN, voice, attn);
675 snd_emu10k1_ptr_write(emu, VTFT, voice, vattn | 0xffff);
676 snd_emu10k1_ptr_write(emu, CVCF, voice, vattn | 0xffff);
677 snd_emu10k1_ptr_write(emu, DCYSUSV, voice, 0x7f7f);
678 snd_emu10k1_voice_clear_loop_stop(emu, voice);
679 }
680
snd_emu10k1_playback_trigger_voice(struct snd_emu10k1 * emu,struct snd_emu10k1_voice * evoice,int master,int extra)681 static void snd_emu10k1_playback_trigger_voice(struct snd_emu10k1 *emu, struct snd_emu10k1_voice *evoice, int master, int extra)
682 {
683 struct snd_pcm_substream *substream;
684 struct snd_pcm_runtime *runtime;
685 unsigned int voice, pitch, pitch_target;
686
687 if (evoice == NULL) /* skip second voice for mono */
688 return;
689 substream = evoice->epcm->substream;
690 runtime = substream->runtime;
691 voice = evoice->number;
692
693 pitch = snd_emu10k1_rate_to_pitch(runtime->rate) >> 8;
694 if (emu->card_capabilities->emu_model)
695 pitch_target = PITCH_48000; /* Disable interpolators on emu1010 card */
696 else
697 pitch_target = emu10k1_calc_pitch_target(runtime->rate);
698 snd_emu10k1_ptr_write(emu, PTRX_PITCHTARGET, voice, pitch_target);
699 if (master || evoice->epcm->type == PLAYBACK_EFX)
700 snd_emu10k1_ptr_write(emu, CPF_CURRENTPITCH, voice, pitch_target);
701 snd_emu10k1_ptr_write(emu, IP, voice, pitch);
702 if (extra)
703 snd_emu10k1_voice_intr_enable(emu, voice);
704 }
705
snd_emu10k1_playback_stop_voice(struct snd_emu10k1 * emu,struct snd_emu10k1_voice * evoice)706 static void snd_emu10k1_playback_stop_voice(struct snd_emu10k1 *emu, struct snd_emu10k1_voice *evoice)
707 {
708 unsigned int voice;
709
710 if (evoice == NULL)
711 return;
712 voice = evoice->number;
713 snd_emu10k1_voice_intr_disable(emu, voice);
714 snd_emu10k1_ptr_write(emu, PTRX_PITCHTARGET, voice, 0);
715 snd_emu10k1_ptr_write(emu, CPF_CURRENTPITCH, voice, 0);
716 snd_emu10k1_ptr_write(emu, IFATN, voice, 0xffff);
717 snd_emu10k1_ptr_write(emu, VTFT, voice, 0xffff);
718 snd_emu10k1_ptr_write(emu, CVCF, voice, 0xffff);
719 snd_emu10k1_ptr_write(emu, IP, voice, 0);
720 }
721
snd_emu10k1_playback_mangle_extra(struct snd_emu10k1 * emu,struct snd_emu10k1_pcm * epcm,struct snd_pcm_substream * substream,struct snd_pcm_runtime * runtime)722 static inline void snd_emu10k1_playback_mangle_extra(struct snd_emu10k1 *emu,
723 struct snd_emu10k1_pcm *epcm,
724 struct snd_pcm_substream *substream,
725 struct snd_pcm_runtime *runtime)
726 {
727 unsigned int ptr, period_pos;
728
729 /* try to sychronize the current position for the interrupt
730 source voice */
731 period_pos = runtime->status->hw_ptr - runtime->hw_ptr_interrupt;
732 period_pos %= runtime->period_size;
733 ptr = snd_emu10k1_ptr_read(emu, CCCA, epcm->extra->number);
734 ptr &= ~0x00ffffff;
735 ptr |= epcm->ccca_start_addr + period_pos;
736 snd_emu10k1_ptr_write(emu, CCCA, epcm->extra->number, ptr);
737 }
738
snd_emu10k1_playback_trigger(struct snd_pcm_substream * substream,int cmd)739 static int snd_emu10k1_playback_trigger(struct snd_pcm_substream *substream,
740 int cmd)
741 {
742 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
743 struct snd_pcm_runtime *runtime = substream->runtime;
744 struct snd_emu10k1_pcm *epcm = runtime->private_data;
745 struct snd_emu10k1_pcm_mixer *mix;
746 int result = 0;
747
748 /*
749 dev_dbg(emu->card->dev,
750 "trigger - emu10k1 = 0x%x, cmd = %i, pointer = %i\n",
751 (int)emu, cmd, substream->ops->pointer(substream))
752 */
753 spin_lock(&emu->reg_lock);
754 switch (cmd) {
755 case SNDRV_PCM_TRIGGER_START:
756 snd_emu10k1_playback_invalidate_cache(emu, 1, epcm->extra); /* do we need this? */
757 snd_emu10k1_playback_invalidate_cache(emu, 0, epcm->voices[0]);
758 fallthrough;
759 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
760 case SNDRV_PCM_TRIGGER_RESUME:
761 if (cmd == SNDRV_PCM_TRIGGER_PAUSE_RELEASE)
762 snd_emu10k1_playback_mangle_extra(emu, epcm, substream, runtime);
763 mix = &emu->pcm_mixer[substream->number];
764 snd_emu10k1_playback_prepare_voice(emu, epcm->voices[0], 1, 0, mix);
765 snd_emu10k1_playback_prepare_voice(emu, epcm->voices[1], 0, 0, mix);
766 snd_emu10k1_playback_prepare_voice(emu, epcm->extra, 1, 1, NULL);
767 snd_emu10k1_playback_trigger_voice(emu, epcm->voices[0], 1, 0);
768 snd_emu10k1_playback_trigger_voice(emu, epcm->voices[1], 0, 0);
769 snd_emu10k1_playback_trigger_voice(emu, epcm->extra, 1, 1);
770 epcm->running = 1;
771 break;
772 case SNDRV_PCM_TRIGGER_STOP:
773 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
774 case SNDRV_PCM_TRIGGER_SUSPEND:
775 epcm->running = 0;
776 snd_emu10k1_playback_stop_voice(emu, epcm->voices[0]);
777 snd_emu10k1_playback_stop_voice(emu, epcm->voices[1]);
778 snd_emu10k1_playback_stop_voice(emu, epcm->extra);
779 break;
780 default:
781 result = -EINVAL;
782 break;
783 }
784 spin_unlock(&emu->reg_lock);
785 return result;
786 }
787
snd_emu10k1_capture_trigger(struct snd_pcm_substream * substream,int cmd)788 static int snd_emu10k1_capture_trigger(struct snd_pcm_substream *substream,
789 int cmd)
790 {
791 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
792 struct snd_pcm_runtime *runtime = substream->runtime;
793 struct snd_emu10k1_pcm *epcm = runtime->private_data;
794 int result = 0;
795
796 spin_lock(&emu->reg_lock);
797 switch (cmd) {
798 case SNDRV_PCM_TRIGGER_START:
799 case SNDRV_PCM_TRIGGER_RESUME:
800 /* hmm this should cause full and half full interrupt to be raised? */
801 outl(epcm->capture_ipr, emu->port + IPR);
802 snd_emu10k1_intr_enable(emu, epcm->capture_inte);
803 /*
804 dev_dbg(emu->card->dev, "adccr = 0x%x, adcbs = 0x%x\n",
805 epcm->adccr, epcm->adcbs);
806 */
807 switch (epcm->type) {
808 case CAPTURE_AC97ADC:
809 snd_emu10k1_ptr_write(emu, ADCCR, 0, epcm->capture_cr_val);
810 break;
811 case CAPTURE_EFX:
812 if (emu->audigy) {
813 snd_emu10k1_ptr_write(emu, A_FXWC1, 0, epcm->capture_cr_val);
814 snd_emu10k1_ptr_write(emu, A_FXWC2, 0, epcm->capture_cr_val2);
815 dev_dbg(emu->card->dev,
816 "cr_val=0x%x, cr_val2=0x%x\n",
817 epcm->capture_cr_val,
818 epcm->capture_cr_val2);
819 } else
820 snd_emu10k1_ptr_write(emu, FXWC, 0, epcm->capture_cr_val);
821 break;
822 default:
823 break;
824 }
825 snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, epcm->capture_bs_val);
826 epcm->running = 1;
827 epcm->first_ptr = 1;
828 break;
829 case SNDRV_PCM_TRIGGER_STOP:
830 case SNDRV_PCM_TRIGGER_SUSPEND:
831 epcm->running = 0;
832 snd_emu10k1_intr_disable(emu, epcm->capture_inte);
833 outl(epcm->capture_ipr, emu->port + IPR);
834 snd_emu10k1_ptr_write(emu, epcm->capture_bs_reg, 0, 0);
835 switch (epcm->type) {
836 case CAPTURE_AC97ADC:
837 snd_emu10k1_ptr_write(emu, ADCCR, 0, 0);
838 break;
839 case CAPTURE_EFX:
840 if (emu->audigy) {
841 snd_emu10k1_ptr_write(emu, A_FXWC1, 0, 0);
842 snd_emu10k1_ptr_write(emu, A_FXWC2, 0, 0);
843 } else
844 snd_emu10k1_ptr_write(emu, FXWC, 0, 0);
845 break;
846 default:
847 break;
848 }
849 break;
850 default:
851 result = -EINVAL;
852 }
853 spin_unlock(&emu->reg_lock);
854 return result;
855 }
856
snd_emu10k1_playback_pointer(struct snd_pcm_substream * substream)857 static snd_pcm_uframes_t snd_emu10k1_playback_pointer(struct snd_pcm_substream *substream)
858 {
859 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
860 struct snd_pcm_runtime *runtime = substream->runtime;
861 struct snd_emu10k1_pcm *epcm = runtime->private_data;
862 unsigned int ptr;
863
864 if (!epcm->running)
865 return 0;
866 ptr = snd_emu10k1_ptr_read(emu, CCCA, epcm->voices[0]->number) & 0x00ffffff;
867 #if 0 /* Perex's code */
868 ptr += runtime->buffer_size;
869 ptr -= epcm->ccca_start_addr;
870 ptr %= runtime->buffer_size;
871 #else /* EMU10K1 Open Source code from Creative */
872 if (ptr < epcm->ccca_start_addr)
873 ptr += runtime->buffer_size - epcm->ccca_start_addr;
874 else {
875 ptr -= epcm->ccca_start_addr;
876 if (ptr >= runtime->buffer_size)
877 ptr -= runtime->buffer_size;
878 }
879 #endif
880 /*
881 dev_dbg(emu->card->dev,
882 "ptr = 0x%lx, buffer_size = 0x%lx, period_size = 0x%lx\n",
883 (long)ptr, (long)runtime->buffer_size,
884 (long)runtime->period_size);
885 */
886 return ptr;
887 }
888
889
snd_emu10k1_efx_playback_trigger(struct snd_pcm_substream * substream,int cmd)890 static int snd_emu10k1_efx_playback_trigger(struct snd_pcm_substream *substream,
891 int cmd)
892 {
893 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
894 struct snd_pcm_runtime *runtime = substream->runtime;
895 struct snd_emu10k1_pcm *epcm = runtime->private_data;
896 int i;
897 int result = 0;
898
899 spin_lock(&emu->reg_lock);
900 switch (cmd) {
901 case SNDRV_PCM_TRIGGER_START:
902 /* prepare voices */
903 for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
904 snd_emu10k1_playback_invalidate_cache(emu, 0, epcm->voices[i]);
905 }
906 snd_emu10k1_playback_invalidate_cache(emu, 1, epcm->extra);
907 fallthrough;
908 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
909 case SNDRV_PCM_TRIGGER_RESUME:
910 snd_emu10k1_playback_prepare_voice(emu, epcm->extra, 1, 1, NULL);
911 snd_emu10k1_playback_prepare_voice(emu, epcm->voices[0], 0, 0,
912 &emu->efx_pcm_mixer[0]);
913 for (i = 1; i < NUM_EFX_PLAYBACK; i++)
914 snd_emu10k1_playback_prepare_voice(emu, epcm->voices[i], 0, 0,
915 &emu->efx_pcm_mixer[i]);
916 snd_emu10k1_playback_trigger_voice(emu, epcm->voices[0], 0, 0);
917 snd_emu10k1_playback_trigger_voice(emu, epcm->extra, 1, 1);
918 for (i = 1; i < NUM_EFX_PLAYBACK; i++)
919 snd_emu10k1_playback_trigger_voice(emu, epcm->voices[i], 0, 0);
920 epcm->running = 1;
921 break;
922 case SNDRV_PCM_TRIGGER_SUSPEND:
923 case SNDRV_PCM_TRIGGER_STOP:
924 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
925 epcm->running = 0;
926 for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
927 snd_emu10k1_playback_stop_voice(emu, epcm->voices[i]);
928 }
929 snd_emu10k1_playback_stop_voice(emu, epcm->extra);
930 break;
931 default:
932 result = -EINVAL;
933 break;
934 }
935 spin_unlock(&emu->reg_lock);
936 return result;
937 }
938
939
snd_emu10k1_capture_pointer(struct snd_pcm_substream * substream)940 static snd_pcm_uframes_t snd_emu10k1_capture_pointer(struct snd_pcm_substream *substream)
941 {
942 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
943 struct snd_pcm_runtime *runtime = substream->runtime;
944 struct snd_emu10k1_pcm *epcm = runtime->private_data;
945 unsigned int ptr;
946
947 if (!epcm->running)
948 return 0;
949 if (epcm->first_ptr) {
950 udelay(50); /* hack, it takes awhile until capture is started */
951 epcm->first_ptr = 0;
952 }
953 ptr = snd_emu10k1_ptr_read(emu, epcm->capture_idx_reg, 0) & 0x0000ffff;
954 return bytes_to_frames(runtime, ptr);
955 }
956
957 /*
958 * Playback support device description
959 */
960
961 static const struct snd_pcm_hardware snd_emu10k1_playback =
962 {
963 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
964 SNDRV_PCM_INFO_BLOCK_TRANSFER |
965 SNDRV_PCM_INFO_RESUME |
966 SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE),
967 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
968 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_96000,
969 .rate_min = 4000,
970 .rate_max = 96000,
971 .channels_min = 1,
972 .channels_max = 2,
973 .buffer_bytes_max = (128*1024),
974 .period_bytes_min = 64,
975 .period_bytes_max = (128*1024),
976 .periods_min = 1,
977 .periods_max = 1024,
978 .fifo_size = 0,
979 };
980
981 /*
982 * Capture support device description
983 */
984
985 static const struct snd_pcm_hardware snd_emu10k1_capture =
986 {
987 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
988 SNDRV_PCM_INFO_BLOCK_TRANSFER |
989 SNDRV_PCM_INFO_RESUME |
990 SNDRV_PCM_INFO_MMAP_VALID),
991 .formats = SNDRV_PCM_FMTBIT_S16_LE,
992 .rates = SNDRV_PCM_RATE_8000_48000,
993 .rate_min = 8000,
994 .rate_max = 48000,
995 .channels_min = 1,
996 .channels_max = 2,
997 .buffer_bytes_max = (64*1024),
998 .period_bytes_min = 384,
999 .period_bytes_max = (64*1024),
1000 .periods_min = 2,
1001 .periods_max = 2,
1002 .fifo_size = 0,
1003 };
1004
1005 static const struct snd_pcm_hardware snd_emu10k1_capture_efx =
1006 {
1007 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1008 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1009 SNDRV_PCM_INFO_RESUME |
1010 SNDRV_PCM_INFO_MMAP_VALID),
1011 .formats = SNDRV_PCM_FMTBIT_S16_LE,
1012 .rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
1013 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
1014 SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000,
1015 .rate_min = 44100,
1016 .rate_max = 192000,
1017 .channels_min = 8,
1018 .channels_max = 8,
1019 .buffer_bytes_max = (64*1024),
1020 .period_bytes_min = 384,
1021 .period_bytes_max = (64*1024),
1022 .periods_min = 2,
1023 .periods_max = 2,
1024 .fifo_size = 0,
1025 };
1026
1027 /*
1028 *
1029 */
1030
snd_emu10k1_pcm_mixer_notify1(struct snd_emu10k1 * emu,struct snd_kcontrol * kctl,int idx,int activate)1031 static void snd_emu10k1_pcm_mixer_notify1(struct snd_emu10k1 *emu, struct snd_kcontrol *kctl, int idx, int activate)
1032 {
1033 struct snd_ctl_elem_id id;
1034
1035 if (! kctl)
1036 return;
1037 if (activate)
1038 kctl->vd[idx].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1039 else
1040 kctl->vd[idx].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
1041 snd_ctl_notify(emu->card, SNDRV_CTL_EVENT_MASK_VALUE |
1042 SNDRV_CTL_EVENT_MASK_INFO,
1043 snd_ctl_build_ioff(&id, kctl, idx));
1044 }
1045
snd_emu10k1_pcm_mixer_notify(struct snd_emu10k1 * emu,int idx,int activate)1046 static void snd_emu10k1_pcm_mixer_notify(struct snd_emu10k1 *emu, int idx, int activate)
1047 {
1048 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_send_routing, idx, activate);
1049 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_send_volume, idx, activate);
1050 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_attn, idx, activate);
1051 }
1052
snd_emu10k1_pcm_efx_mixer_notify(struct snd_emu10k1 * emu,int idx,int activate)1053 static void snd_emu10k1_pcm_efx_mixer_notify(struct snd_emu10k1 *emu, int idx, int activate)
1054 {
1055 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_send_routing, idx, activate);
1056 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_send_volume, idx, activate);
1057 snd_emu10k1_pcm_mixer_notify1(emu, emu->ctl_efx_attn, idx, activate);
1058 }
1059
snd_emu10k1_pcm_free_substream(struct snd_pcm_runtime * runtime)1060 static void snd_emu10k1_pcm_free_substream(struct snd_pcm_runtime *runtime)
1061 {
1062 kfree(runtime->private_data);
1063 }
1064
snd_emu10k1_efx_playback_close(struct snd_pcm_substream * substream)1065 static int snd_emu10k1_efx_playback_close(struct snd_pcm_substream *substream)
1066 {
1067 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1068 struct snd_emu10k1_pcm_mixer *mix;
1069 int i;
1070
1071 for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
1072 mix = &emu->efx_pcm_mixer[i];
1073 mix->epcm = NULL;
1074 snd_emu10k1_pcm_efx_mixer_notify(emu, i, 0);
1075 }
1076 return 0;
1077 }
1078
snd_emu10k1_efx_playback_open(struct snd_pcm_substream * substream)1079 static int snd_emu10k1_efx_playback_open(struct snd_pcm_substream *substream)
1080 {
1081 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1082 struct snd_emu10k1_pcm *epcm;
1083 struct snd_emu10k1_pcm_mixer *mix;
1084 struct snd_pcm_runtime *runtime = substream->runtime;
1085 int i;
1086
1087 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1088 if (epcm == NULL)
1089 return -ENOMEM;
1090 epcm->emu = emu;
1091 epcm->type = PLAYBACK_EFX;
1092 epcm->substream = substream;
1093
1094 emu->pcm_playback_efx_substream = substream;
1095
1096 runtime->private_data = epcm;
1097 runtime->private_free = snd_emu10k1_pcm_free_substream;
1098 runtime->hw = snd_emu10k1_efx_playback;
1099
1100 for (i = 0; i < NUM_EFX_PLAYBACK; i++) {
1101 mix = &emu->efx_pcm_mixer[i];
1102 mix->send_routing[0][0] = i;
1103 memset(&mix->send_volume, 0, sizeof(mix->send_volume));
1104 mix->send_volume[0][0] = 255;
1105 mix->attn[0] = 0xffff;
1106 mix->epcm = epcm;
1107 snd_emu10k1_pcm_efx_mixer_notify(emu, i, 1);
1108 }
1109 return 0;
1110 }
1111
snd_emu10k1_playback_open(struct snd_pcm_substream * substream)1112 static int snd_emu10k1_playback_open(struct snd_pcm_substream *substream)
1113 {
1114 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1115 struct snd_emu10k1_pcm *epcm;
1116 struct snd_emu10k1_pcm_mixer *mix;
1117 struct snd_pcm_runtime *runtime = substream->runtime;
1118 int i, err, sample_rate;
1119
1120 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1121 if (epcm == NULL)
1122 return -ENOMEM;
1123 epcm->emu = emu;
1124 epcm->type = PLAYBACK_EMUVOICE;
1125 epcm->substream = substream;
1126 runtime->private_data = epcm;
1127 runtime->private_free = snd_emu10k1_pcm_free_substream;
1128 runtime->hw = snd_emu10k1_playback;
1129 err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
1130 if (err < 0) {
1131 kfree(epcm);
1132 return err;
1133 }
1134 err = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 256, UINT_MAX);
1135 if (err < 0) {
1136 kfree(epcm);
1137 return err;
1138 }
1139 if (emu->card_capabilities->emu_model && emu->emu1010.internal_clock == 0)
1140 sample_rate = 44100;
1141 else
1142 sample_rate = 48000;
1143 err = snd_pcm_hw_rule_noresample(runtime, sample_rate);
1144 if (err < 0) {
1145 kfree(epcm);
1146 return err;
1147 }
1148 mix = &emu->pcm_mixer[substream->number];
1149 for (i = 0; i < 4; i++)
1150 mix->send_routing[0][i] = mix->send_routing[1][i] = mix->send_routing[2][i] = i;
1151 memset(&mix->send_volume, 0, sizeof(mix->send_volume));
1152 mix->send_volume[0][0] = mix->send_volume[0][1] =
1153 mix->send_volume[1][0] = mix->send_volume[2][1] = 255;
1154 mix->attn[0] = mix->attn[1] = mix->attn[2] = 0xffff;
1155 mix->epcm = epcm;
1156 snd_emu10k1_pcm_mixer_notify(emu, substream->number, 1);
1157 return 0;
1158 }
1159
snd_emu10k1_playback_close(struct snd_pcm_substream * substream)1160 static int snd_emu10k1_playback_close(struct snd_pcm_substream *substream)
1161 {
1162 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1163 struct snd_emu10k1_pcm_mixer *mix = &emu->pcm_mixer[substream->number];
1164
1165 mix->epcm = NULL;
1166 snd_emu10k1_pcm_mixer_notify(emu, substream->number, 0);
1167 return 0;
1168 }
1169
snd_emu10k1_capture_open(struct snd_pcm_substream * substream)1170 static int snd_emu10k1_capture_open(struct snd_pcm_substream *substream)
1171 {
1172 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1173 struct snd_pcm_runtime *runtime = substream->runtime;
1174 struct snd_emu10k1_pcm *epcm;
1175
1176 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1177 if (epcm == NULL)
1178 return -ENOMEM;
1179 epcm->emu = emu;
1180 epcm->type = CAPTURE_AC97ADC;
1181 epcm->substream = substream;
1182 epcm->capture_ipr = IPR_ADCBUFFULL|IPR_ADCBUFHALFFULL;
1183 epcm->capture_inte = INTE_ADCBUFENABLE;
1184 epcm->capture_ba_reg = ADCBA;
1185 epcm->capture_bs_reg = ADCBS;
1186 epcm->capture_idx_reg = emu->audigy ? A_ADCIDX : ADCIDX;
1187 runtime->private_data = epcm;
1188 runtime->private_free = snd_emu10k1_pcm_free_substream;
1189 runtime->hw = snd_emu10k1_capture;
1190 emu->capture_interrupt = snd_emu10k1_pcm_ac97adc_interrupt;
1191 emu->pcm_capture_substream = substream;
1192 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
1193 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_capture_rates);
1194 return 0;
1195 }
1196
snd_emu10k1_capture_close(struct snd_pcm_substream * substream)1197 static int snd_emu10k1_capture_close(struct snd_pcm_substream *substream)
1198 {
1199 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1200
1201 emu->capture_interrupt = NULL;
1202 emu->pcm_capture_substream = NULL;
1203 return 0;
1204 }
1205
snd_emu10k1_capture_mic_open(struct snd_pcm_substream * substream)1206 static int snd_emu10k1_capture_mic_open(struct snd_pcm_substream *substream)
1207 {
1208 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1209 struct snd_emu10k1_pcm *epcm;
1210 struct snd_pcm_runtime *runtime = substream->runtime;
1211
1212 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1213 if (epcm == NULL)
1214 return -ENOMEM;
1215 epcm->emu = emu;
1216 epcm->type = CAPTURE_AC97MIC;
1217 epcm->substream = substream;
1218 epcm->capture_ipr = IPR_MICBUFFULL|IPR_MICBUFHALFFULL;
1219 epcm->capture_inte = INTE_MICBUFENABLE;
1220 epcm->capture_ba_reg = MICBA;
1221 epcm->capture_bs_reg = MICBS;
1222 epcm->capture_idx_reg = emu->audigy ? A_MICIDX : MICIDX;
1223 substream->runtime->private_data = epcm;
1224 substream->runtime->private_free = snd_emu10k1_pcm_free_substream;
1225 runtime->hw = snd_emu10k1_capture;
1226 runtime->hw.rates = SNDRV_PCM_RATE_8000;
1227 runtime->hw.rate_min = runtime->hw.rate_max = 8000;
1228 runtime->hw.channels_min = 1;
1229 emu->capture_mic_interrupt = snd_emu10k1_pcm_ac97mic_interrupt;
1230 emu->pcm_capture_mic_substream = substream;
1231 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
1232 return 0;
1233 }
1234
snd_emu10k1_capture_mic_close(struct snd_pcm_substream * substream)1235 static int snd_emu10k1_capture_mic_close(struct snd_pcm_substream *substream)
1236 {
1237 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1238
1239 emu->capture_interrupt = NULL;
1240 emu->pcm_capture_mic_substream = NULL;
1241 return 0;
1242 }
1243
snd_emu10k1_capture_efx_open(struct snd_pcm_substream * substream)1244 static int snd_emu10k1_capture_efx_open(struct snd_pcm_substream *substream)
1245 {
1246 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1247 struct snd_emu10k1_pcm *epcm;
1248 struct snd_pcm_runtime *runtime = substream->runtime;
1249 int nefx = emu->audigy ? 64 : 32;
1250 int idx;
1251
1252 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
1253 if (epcm == NULL)
1254 return -ENOMEM;
1255 epcm->emu = emu;
1256 epcm->type = CAPTURE_EFX;
1257 epcm->substream = substream;
1258 epcm->capture_ipr = IPR_EFXBUFFULL|IPR_EFXBUFHALFFULL;
1259 epcm->capture_inte = INTE_EFXBUFENABLE;
1260 epcm->capture_ba_reg = FXBA;
1261 epcm->capture_bs_reg = FXBS;
1262 epcm->capture_idx_reg = FXIDX;
1263 substream->runtime->private_data = epcm;
1264 substream->runtime->private_free = snd_emu10k1_pcm_free_substream;
1265 runtime->hw = snd_emu10k1_capture_efx;
1266 runtime->hw.rates = SNDRV_PCM_RATE_48000;
1267 runtime->hw.rate_min = runtime->hw.rate_max = 48000;
1268 spin_lock_irq(&emu->reg_lock);
1269 if (emu->card_capabilities->emu_model) {
1270 /* Nb. of channels has been increased to 16 */
1271 /* TODO
1272 * SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE
1273 * SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
1274 * SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
1275 * SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000
1276 * rate_min = 44100,
1277 * rate_max = 192000,
1278 * channels_min = 16,
1279 * channels_max = 16,
1280 * Need to add mixer control to fix sample rate
1281 *
1282 * There are 32 mono channels of 16bits each.
1283 * 24bit Audio uses 2x channels over 16bit
1284 * 96kHz uses 2x channels over 48kHz
1285 * 192kHz uses 4x channels over 48kHz
1286 * So, for 48kHz 24bit, one has 16 channels
1287 * for 96kHz 24bit, one has 8 channels
1288 * for 192kHz 24bit, one has 4 channels
1289 *
1290 */
1291 #if 1
1292 switch (emu->emu1010.internal_clock) {
1293 case 0:
1294 /* For 44.1kHz */
1295 runtime->hw.rates = SNDRV_PCM_RATE_44100;
1296 runtime->hw.rate_min = runtime->hw.rate_max = 44100;
1297 runtime->hw.channels_min =
1298 runtime->hw.channels_max = 16;
1299 break;
1300 case 1:
1301 /* For 48kHz */
1302 runtime->hw.rates = SNDRV_PCM_RATE_48000;
1303 runtime->hw.rate_min = runtime->hw.rate_max = 48000;
1304 runtime->hw.channels_min =
1305 runtime->hw.channels_max = 16;
1306 break;
1307 }
1308 #endif
1309 #if 0
1310 /* For 96kHz */
1311 runtime->hw.rates = SNDRV_PCM_RATE_96000;
1312 runtime->hw.rate_min = runtime->hw.rate_max = 96000;
1313 runtime->hw.channels_min = runtime->hw.channels_max = 4;
1314 #endif
1315 #if 0
1316 /* For 192kHz */
1317 runtime->hw.rates = SNDRV_PCM_RATE_192000;
1318 runtime->hw.rate_min = runtime->hw.rate_max = 192000;
1319 runtime->hw.channels_min = runtime->hw.channels_max = 2;
1320 #endif
1321 runtime->hw.formats = SNDRV_PCM_FMTBIT_S32_LE;
1322 /* efx_voices_mask[0] is expected to be zero
1323 * efx_voices_mask[1] is expected to have 32bits set
1324 */
1325 } else {
1326 runtime->hw.channels_min = runtime->hw.channels_max = 0;
1327 for (idx = 0; idx < nefx; idx++) {
1328 if (emu->efx_voices_mask[idx/32] & (1 << (idx%32))) {
1329 runtime->hw.channels_min++;
1330 runtime->hw.channels_max++;
1331 }
1332 }
1333 }
1334 epcm->capture_cr_val = emu->efx_voices_mask[0];
1335 epcm->capture_cr_val2 = emu->efx_voices_mask[1];
1336 spin_unlock_irq(&emu->reg_lock);
1337 emu->capture_efx_interrupt = snd_emu10k1_pcm_efx_interrupt;
1338 emu->pcm_capture_efx_substream = substream;
1339 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, &hw_constraints_capture_period_sizes);
1340 return 0;
1341 }
1342
snd_emu10k1_capture_efx_close(struct snd_pcm_substream * substream)1343 static int snd_emu10k1_capture_efx_close(struct snd_pcm_substream *substream)
1344 {
1345 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1346
1347 emu->capture_interrupt = NULL;
1348 emu->pcm_capture_efx_substream = NULL;
1349 return 0;
1350 }
1351
1352 static const struct snd_pcm_ops snd_emu10k1_playback_ops = {
1353 .open = snd_emu10k1_playback_open,
1354 .close = snd_emu10k1_playback_close,
1355 .hw_params = snd_emu10k1_playback_hw_params,
1356 .hw_free = snd_emu10k1_playback_hw_free,
1357 .prepare = snd_emu10k1_playback_prepare,
1358 .trigger = snd_emu10k1_playback_trigger,
1359 .pointer = snd_emu10k1_playback_pointer,
1360 };
1361
1362 static const struct snd_pcm_ops snd_emu10k1_capture_ops = {
1363 .open = snd_emu10k1_capture_open,
1364 .close = snd_emu10k1_capture_close,
1365 .prepare = snd_emu10k1_capture_prepare,
1366 .trigger = snd_emu10k1_capture_trigger,
1367 .pointer = snd_emu10k1_capture_pointer,
1368 };
1369
1370 /* EFX playback */
1371 static const struct snd_pcm_ops snd_emu10k1_efx_playback_ops = {
1372 .open = snd_emu10k1_efx_playback_open,
1373 .close = snd_emu10k1_efx_playback_close,
1374 .hw_params = snd_emu10k1_playback_hw_params,
1375 .hw_free = snd_emu10k1_efx_playback_hw_free,
1376 .prepare = snd_emu10k1_efx_playback_prepare,
1377 .trigger = snd_emu10k1_efx_playback_trigger,
1378 .pointer = snd_emu10k1_efx_playback_pointer,
1379 };
1380
snd_emu10k1_pcm(struct snd_emu10k1 * emu,int device)1381 int snd_emu10k1_pcm(struct snd_emu10k1 *emu, int device)
1382 {
1383 struct snd_pcm *pcm;
1384 struct snd_pcm_substream *substream;
1385 int err;
1386
1387 err = snd_pcm_new(emu->card, "emu10k1", device, 32, 1, &pcm);
1388 if (err < 0)
1389 return err;
1390
1391 pcm->private_data = emu;
1392
1393 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_playback_ops);
1394 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_ops);
1395
1396 pcm->info_flags = 0;
1397 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
1398 strcpy(pcm->name, "ADC Capture/Standard PCM Playback");
1399 emu->pcm = pcm;
1400
1401 /* playback substream can't use managed buffers due to alignment */
1402 for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
1403 snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV_SG,
1404 &emu->pci->dev,
1405 64*1024, 64*1024);
1406
1407 for (substream = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream; substream; substream = substream->next)
1408 snd_pcm_set_managed_buffer(substream, SNDRV_DMA_TYPE_DEV,
1409 &emu->pci->dev, 64*1024, 64*1024);
1410
1411 return 0;
1412 }
1413
snd_emu10k1_pcm_multi(struct snd_emu10k1 * emu,int device)1414 int snd_emu10k1_pcm_multi(struct snd_emu10k1 *emu, int device)
1415 {
1416 struct snd_pcm *pcm;
1417 struct snd_pcm_substream *substream;
1418 int err;
1419
1420 err = snd_pcm_new(emu->card, "emu10k1", device, 1, 0, &pcm);
1421 if (err < 0)
1422 return err;
1423
1424 pcm->private_data = emu;
1425
1426 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_efx_playback_ops);
1427
1428 pcm->info_flags = 0;
1429 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
1430 strcpy(pcm->name, "Multichannel Playback");
1431 emu->pcm_multi = pcm;
1432
1433 for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
1434 snd_pcm_lib_preallocate_pages(substream, SNDRV_DMA_TYPE_DEV_SG,
1435 &emu->pci->dev,
1436 64*1024, 64*1024);
1437
1438 return 0;
1439 }
1440
1441
1442 static const struct snd_pcm_ops snd_emu10k1_capture_mic_ops = {
1443 .open = snd_emu10k1_capture_mic_open,
1444 .close = snd_emu10k1_capture_mic_close,
1445 .prepare = snd_emu10k1_capture_prepare,
1446 .trigger = snd_emu10k1_capture_trigger,
1447 .pointer = snd_emu10k1_capture_pointer,
1448 };
1449
snd_emu10k1_pcm_mic(struct snd_emu10k1 * emu,int device)1450 int snd_emu10k1_pcm_mic(struct snd_emu10k1 *emu, int device)
1451 {
1452 struct snd_pcm *pcm;
1453 int err;
1454
1455 err = snd_pcm_new(emu->card, "emu10k1 mic", device, 0, 1, &pcm);
1456 if (err < 0)
1457 return err;
1458
1459 pcm->private_data = emu;
1460
1461 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_mic_ops);
1462
1463 pcm->info_flags = 0;
1464 strcpy(pcm->name, "Mic Capture");
1465 emu->pcm_mic = pcm;
1466
1467 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, &emu->pci->dev,
1468 64*1024, 64*1024);
1469
1470 return 0;
1471 }
1472
snd_emu10k1_pcm_efx_voices_mask_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1473 static int snd_emu10k1_pcm_efx_voices_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1474 {
1475 struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1476 int nefx = emu->audigy ? 64 : 32;
1477 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1478 uinfo->count = nefx;
1479 uinfo->value.integer.min = 0;
1480 uinfo->value.integer.max = 1;
1481 return 0;
1482 }
1483
snd_emu10k1_pcm_efx_voices_mask_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1484 static int snd_emu10k1_pcm_efx_voices_mask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1485 {
1486 struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1487 int nefx = emu->audigy ? 64 : 32;
1488 int idx;
1489
1490 spin_lock_irq(&emu->reg_lock);
1491 for (idx = 0; idx < nefx; idx++)
1492 ucontrol->value.integer.value[idx] = (emu->efx_voices_mask[idx / 32] & (1 << (idx % 32))) ? 1 : 0;
1493 spin_unlock_irq(&emu->reg_lock);
1494 return 0;
1495 }
1496
snd_emu10k1_pcm_efx_voices_mask_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1497 static int snd_emu10k1_pcm_efx_voices_mask_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1498 {
1499 struct snd_emu10k1 *emu = snd_kcontrol_chip(kcontrol);
1500 unsigned int nval[2], bits;
1501 int nefx = emu->audigy ? 64 : 32;
1502 int nefxb = emu->audigy ? 7 : 6;
1503 int change, idx;
1504
1505 nval[0] = nval[1] = 0;
1506 for (idx = 0, bits = 0; idx < nefx; idx++)
1507 if (ucontrol->value.integer.value[idx]) {
1508 nval[idx / 32] |= 1 << (idx % 32);
1509 bits++;
1510 }
1511
1512 for (idx = 0; idx < nefxb; idx++)
1513 if (1 << idx == bits)
1514 break;
1515
1516 if (idx >= nefxb)
1517 return -EINVAL;
1518
1519 spin_lock_irq(&emu->reg_lock);
1520 change = (nval[0] != emu->efx_voices_mask[0]) ||
1521 (nval[1] != emu->efx_voices_mask[1]);
1522 emu->efx_voices_mask[0] = nval[0];
1523 emu->efx_voices_mask[1] = nval[1];
1524 spin_unlock_irq(&emu->reg_lock);
1525 return change;
1526 }
1527
1528 static const struct snd_kcontrol_new snd_emu10k1_pcm_efx_voices_mask = {
1529 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1530 .name = "Captured FX8010 Outputs",
1531 .info = snd_emu10k1_pcm_efx_voices_mask_info,
1532 .get = snd_emu10k1_pcm_efx_voices_mask_get,
1533 .put = snd_emu10k1_pcm_efx_voices_mask_put
1534 };
1535
1536 static const struct snd_pcm_ops snd_emu10k1_capture_efx_ops = {
1537 .open = snd_emu10k1_capture_efx_open,
1538 .close = snd_emu10k1_capture_efx_close,
1539 .prepare = snd_emu10k1_capture_prepare,
1540 .trigger = snd_emu10k1_capture_trigger,
1541 .pointer = snd_emu10k1_capture_pointer,
1542 };
1543
1544
1545 /* EFX playback */
1546
1547 #define INITIAL_TRAM_SHIFT 14
1548 #define INITIAL_TRAM_POS(size) ((((size) / 2) - INITIAL_TRAM_SHIFT) - 1)
1549
snd_emu10k1_fx8010_playback_irq(struct snd_emu10k1 * emu,void * private_data)1550 static void snd_emu10k1_fx8010_playback_irq(struct snd_emu10k1 *emu, void *private_data)
1551 {
1552 struct snd_pcm_substream *substream = private_data;
1553 snd_pcm_period_elapsed(substream);
1554 }
1555
snd_emu10k1_fx8010_playback_tram_poke1(unsigned short * dst_left,unsigned short * dst_right,unsigned short * src,unsigned int count,unsigned int tram_shift)1556 static void snd_emu10k1_fx8010_playback_tram_poke1(unsigned short *dst_left,
1557 unsigned short *dst_right,
1558 unsigned short *src,
1559 unsigned int count,
1560 unsigned int tram_shift)
1561 {
1562 /*
1563 dev_dbg(emu->card->dev,
1564 "tram_poke1: dst_left = 0x%p, dst_right = 0x%p, "
1565 "src = 0x%p, count = 0x%x\n",
1566 dst_left, dst_right, src, count);
1567 */
1568 if ((tram_shift & 1) == 0) {
1569 while (count--) {
1570 *dst_left-- = *src++;
1571 *dst_right-- = *src++;
1572 }
1573 } else {
1574 while (count--) {
1575 *dst_right-- = *src++;
1576 *dst_left-- = *src++;
1577 }
1578 }
1579 }
1580
fx8010_pb_trans_copy(struct snd_pcm_substream * substream,struct snd_pcm_indirect * rec,size_t bytes)1581 static void fx8010_pb_trans_copy(struct snd_pcm_substream *substream,
1582 struct snd_pcm_indirect *rec, size_t bytes)
1583 {
1584 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1585 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1586 unsigned int tram_size = pcm->buffer_size;
1587 unsigned short *src = (unsigned short *)(substream->runtime->dma_area + rec->sw_data);
1588 unsigned int frames = bytes >> 2, count;
1589 unsigned int tram_pos = pcm->tram_pos;
1590 unsigned int tram_shift = pcm->tram_shift;
1591
1592 while (frames > tram_pos) {
1593 count = tram_pos + 1;
1594 snd_emu10k1_fx8010_playback_tram_poke1((unsigned short *)emu->fx8010.etram_pages.area + tram_pos,
1595 (unsigned short *)emu->fx8010.etram_pages.area + tram_pos + tram_size / 2,
1596 src, count, tram_shift);
1597 src += count * 2;
1598 frames -= count;
1599 tram_pos = (tram_size / 2) - 1;
1600 tram_shift++;
1601 }
1602 snd_emu10k1_fx8010_playback_tram_poke1((unsigned short *)emu->fx8010.etram_pages.area + tram_pos,
1603 (unsigned short *)emu->fx8010.etram_pages.area + tram_pos + tram_size / 2,
1604 src, frames, tram_shift);
1605 tram_pos -= frames;
1606 pcm->tram_pos = tram_pos;
1607 pcm->tram_shift = tram_shift;
1608 }
1609
snd_emu10k1_fx8010_playback_transfer(struct snd_pcm_substream * substream)1610 static int snd_emu10k1_fx8010_playback_transfer(struct snd_pcm_substream *substream)
1611 {
1612 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1613 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1614
1615 return snd_pcm_indirect_playback_transfer(substream, &pcm->pcm_rec,
1616 fx8010_pb_trans_copy);
1617 }
1618
snd_emu10k1_fx8010_playback_hw_free(struct snd_pcm_substream * substream)1619 static int snd_emu10k1_fx8010_playback_hw_free(struct snd_pcm_substream *substream)
1620 {
1621 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1622 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1623 unsigned int i;
1624
1625 for (i = 0; i < pcm->channels; i++)
1626 snd_emu10k1_ptr_write(emu, TANKMEMADDRREGBASE + 0x80 + pcm->etram[i], 0, 0);
1627 return 0;
1628 }
1629
snd_emu10k1_fx8010_playback_prepare(struct snd_pcm_substream * substream)1630 static int snd_emu10k1_fx8010_playback_prepare(struct snd_pcm_substream *substream)
1631 {
1632 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1633 struct snd_pcm_runtime *runtime = substream->runtime;
1634 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1635 unsigned int i;
1636
1637 /*
1638 dev_dbg(emu->card->dev, "prepare: etram_pages = 0x%p, dma_area = 0x%x, "
1639 "buffer_size = 0x%x (0x%x)\n",
1640 emu->fx8010.etram_pages, runtime->dma_area,
1641 runtime->buffer_size, runtime->buffer_size << 2);
1642 */
1643 memset(&pcm->pcm_rec, 0, sizeof(pcm->pcm_rec));
1644 pcm->pcm_rec.hw_buffer_size = pcm->buffer_size * 2; /* byte size */
1645 pcm->pcm_rec.sw_buffer_size = snd_pcm_lib_buffer_bytes(substream);
1646 pcm->tram_pos = INITIAL_TRAM_POS(pcm->buffer_size);
1647 pcm->tram_shift = 0;
1648 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_running, 0, 0); /* reset */
1649 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 0); /* reset */
1650 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_size, 0, runtime->buffer_size);
1651 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_ptr, 0, 0); /* reset ptr number */
1652 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_count, 0, runtime->period_size);
1653 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_tmpcount, 0, runtime->period_size);
1654 for (i = 0; i < pcm->channels; i++)
1655 snd_emu10k1_ptr_write(emu, TANKMEMADDRREGBASE + 0x80 + pcm->etram[i], 0, (TANKMEMADDRREG_READ|TANKMEMADDRREG_ALIGN) + i * (runtime->buffer_size / pcm->channels));
1656 return 0;
1657 }
1658
snd_emu10k1_fx8010_playback_trigger(struct snd_pcm_substream * substream,int cmd)1659 static int snd_emu10k1_fx8010_playback_trigger(struct snd_pcm_substream *substream, int cmd)
1660 {
1661 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1662 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1663 int result = 0;
1664
1665 spin_lock(&emu->reg_lock);
1666 switch (cmd) {
1667 case SNDRV_PCM_TRIGGER_START:
1668 /* follow thru */
1669 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1670 case SNDRV_PCM_TRIGGER_RESUME:
1671 #ifdef EMU10K1_SET_AC3_IEC958
1672 {
1673 int i;
1674 for (i = 0; i < 3; i++) {
1675 unsigned int bits;
1676 bits = SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
1677 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC | SPCS_GENERATIONSTATUS |
1678 0x00001200 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT | SPCS_NOTAUDIODATA;
1679 snd_emu10k1_ptr_write(emu, SPCS0 + i, 0, bits);
1680 }
1681 }
1682 #endif
1683 result = snd_emu10k1_fx8010_register_irq_handler(emu, snd_emu10k1_fx8010_playback_irq, pcm->gpr_running, substream, &pcm->irq);
1684 if (result < 0)
1685 goto __err;
1686 snd_emu10k1_fx8010_playback_transfer(substream); /* roll the ball */
1687 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 1);
1688 break;
1689 case SNDRV_PCM_TRIGGER_STOP:
1690 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1691 case SNDRV_PCM_TRIGGER_SUSPEND:
1692 snd_emu10k1_fx8010_unregister_irq_handler(emu, &pcm->irq);
1693 snd_emu10k1_ptr_write(emu, emu->gpr_base + pcm->gpr_trigger, 0, 0);
1694 pcm->tram_pos = INITIAL_TRAM_POS(pcm->buffer_size);
1695 pcm->tram_shift = 0;
1696 break;
1697 default:
1698 result = -EINVAL;
1699 break;
1700 }
1701 __err:
1702 spin_unlock(&emu->reg_lock);
1703 return result;
1704 }
1705
snd_emu10k1_fx8010_playback_pointer(struct snd_pcm_substream * substream)1706 static snd_pcm_uframes_t snd_emu10k1_fx8010_playback_pointer(struct snd_pcm_substream *substream)
1707 {
1708 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1709 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1710 size_t ptr; /* byte pointer */
1711
1712 if (!snd_emu10k1_ptr_read(emu, emu->gpr_base + pcm->gpr_trigger, 0))
1713 return 0;
1714 ptr = snd_emu10k1_ptr_read(emu, emu->gpr_base + pcm->gpr_ptr, 0) << 2;
1715 return snd_pcm_indirect_playback_pointer(substream, &pcm->pcm_rec, ptr);
1716 }
1717
1718 static const struct snd_pcm_hardware snd_emu10k1_fx8010_playback =
1719 {
1720 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1721 SNDRV_PCM_INFO_RESUME |
1722 /* SNDRV_PCM_INFO_MMAP_VALID | */ SNDRV_PCM_INFO_PAUSE |
1723 SNDRV_PCM_INFO_SYNC_APPLPTR),
1724 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
1725 .rates = SNDRV_PCM_RATE_48000,
1726 .rate_min = 48000,
1727 .rate_max = 48000,
1728 .channels_min = 1,
1729 .channels_max = 1,
1730 .buffer_bytes_max = (128*1024),
1731 .period_bytes_min = 1024,
1732 .period_bytes_max = (128*1024),
1733 .periods_min = 2,
1734 .periods_max = 1024,
1735 .fifo_size = 0,
1736 };
1737
snd_emu10k1_fx8010_playback_open(struct snd_pcm_substream * substream)1738 static int snd_emu10k1_fx8010_playback_open(struct snd_pcm_substream *substream)
1739 {
1740 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1741 struct snd_pcm_runtime *runtime = substream->runtime;
1742 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1743
1744 runtime->hw = snd_emu10k1_fx8010_playback;
1745 runtime->hw.channels_min = runtime->hw.channels_max = pcm->channels;
1746 runtime->hw.period_bytes_max = (pcm->buffer_size * 2) / 2;
1747 spin_lock_irq(&emu->reg_lock);
1748 if (pcm->valid == 0) {
1749 spin_unlock_irq(&emu->reg_lock);
1750 return -ENODEV;
1751 }
1752 pcm->opened = 1;
1753 spin_unlock_irq(&emu->reg_lock);
1754 return 0;
1755 }
1756
snd_emu10k1_fx8010_playback_close(struct snd_pcm_substream * substream)1757 static int snd_emu10k1_fx8010_playback_close(struct snd_pcm_substream *substream)
1758 {
1759 struct snd_emu10k1 *emu = snd_pcm_substream_chip(substream);
1760 struct snd_emu10k1_fx8010_pcm *pcm = &emu->fx8010.pcm[substream->number];
1761
1762 spin_lock_irq(&emu->reg_lock);
1763 pcm->opened = 0;
1764 spin_unlock_irq(&emu->reg_lock);
1765 return 0;
1766 }
1767
1768 static const struct snd_pcm_ops snd_emu10k1_fx8010_playback_ops = {
1769 .open = snd_emu10k1_fx8010_playback_open,
1770 .close = snd_emu10k1_fx8010_playback_close,
1771 .hw_free = snd_emu10k1_fx8010_playback_hw_free,
1772 .prepare = snd_emu10k1_fx8010_playback_prepare,
1773 .trigger = snd_emu10k1_fx8010_playback_trigger,
1774 .pointer = snd_emu10k1_fx8010_playback_pointer,
1775 .ack = snd_emu10k1_fx8010_playback_transfer,
1776 };
1777
snd_emu10k1_pcm_efx(struct snd_emu10k1 * emu,int device)1778 int snd_emu10k1_pcm_efx(struct snd_emu10k1 *emu, int device)
1779 {
1780 struct snd_pcm *pcm;
1781 struct snd_kcontrol *kctl;
1782 int err;
1783
1784 err = snd_pcm_new(emu->card, "emu10k1 efx", device, 8, 1, &pcm);
1785 if (err < 0)
1786 return err;
1787
1788 pcm->private_data = emu;
1789
1790 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1_fx8010_playback_ops);
1791 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1_capture_efx_ops);
1792
1793 pcm->info_flags = 0;
1794 strcpy(pcm->name, "Multichannel Capture/PT Playback");
1795 emu->pcm_efx = pcm;
1796
1797 /* EFX capture - record the "FXBUS2" channels, by default we connect the EXTINs
1798 * to these
1799 */
1800
1801 /* emu->efx_voices_mask[0] = FXWC_DEFAULTROUTE_C | FXWC_DEFAULTROUTE_A; */
1802 if (emu->audigy) {
1803 emu->efx_voices_mask[0] = 0;
1804 if (emu->card_capabilities->emu_model)
1805 /* Pavel Hofman - 32 voices will be used for
1806 * capture (write mode) -
1807 * each bit = corresponding voice
1808 */
1809 emu->efx_voices_mask[1] = 0xffffffff;
1810 else
1811 emu->efx_voices_mask[1] = 0xffff;
1812 } else {
1813 emu->efx_voices_mask[0] = 0xffff0000;
1814 emu->efx_voices_mask[1] = 0;
1815 }
1816 /* For emu1010, the control has to set 32 upper bits (voices)
1817 * out of the 64 bits (voices) to true for the 16-channels capture
1818 * to work correctly. Correct A_FXWC2 initial value (0xffffffff)
1819 * is already defined but the snd_emu10k1_pcm_efx_voices_mask
1820 * control can override this register's value.
1821 */
1822 kctl = snd_ctl_new1(&snd_emu10k1_pcm_efx_voices_mask, emu);
1823 if (!kctl)
1824 return -ENOMEM;
1825 kctl->id.device = device;
1826 err = snd_ctl_add(emu->card, kctl);
1827 if (err < 0)
1828 return err;
1829
1830 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, &emu->pci->dev,
1831 64*1024, 64*1024);
1832
1833 return 0;
1834 }
1835