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