1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * ALSA driver for Echoaudio soundcards.
4 * Copyright (C) 2003-2004 Giuliano Pochini <pochini@shiny.it>
5 * Copyright (C) 2020 Mark Hills <mark@xwax.org>
6 */
7
8 #include <linux/module.h>
9 #include <linux/string.h>
10
11 MODULE_AUTHOR("Giuliano Pochini <pochini@shiny.it>");
12 MODULE_LICENSE("GPL v2");
13 MODULE_DESCRIPTION("Echoaudio " ECHOCARD_NAME " soundcards driver");
14 MODULE_DEVICE_TABLE(pci, snd_echo_ids);
15
16 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
17 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
18 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
19
20 module_param_array(index, int, NULL, 0444);
21 MODULE_PARM_DESC(index, "Index value for " ECHOCARD_NAME " soundcard.");
22 module_param_array(id, charp, NULL, 0444);
23 MODULE_PARM_DESC(id, "ID string for " ECHOCARD_NAME " soundcard.");
24 module_param_array(enable, bool, NULL, 0444);
25 MODULE_PARM_DESC(enable, "Enable " ECHOCARD_NAME " soundcard.");
26
27 static const unsigned int channels_list[10] = {1, 2, 4, 6, 8, 10, 12, 14, 16, 999999};
28 static const DECLARE_TLV_DB_SCALE(db_scale_output_gain, -12800, 100, 1);
29
30
31
get_firmware(const struct firmware ** fw_entry,struct echoaudio * chip,const short fw_index)32 static int get_firmware(const struct firmware **fw_entry,
33 struct echoaudio *chip, const short fw_index)
34 {
35 int err;
36 char name[30];
37
38 if (chip->fw_cache[fw_index]) {
39 dev_dbg(chip->card->dev,
40 "firmware requested: %s is cached\n",
41 card_fw[fw_index].data);
42 *fw_entry = chip->fw_cache[fw_index];
43 return 0;
44 }
45
46 dev_dbg(chip->card->dev,
47 "firmware requested: %s\n", card_fw[fw_index].data);
48 snprintf(name, sizeof(name), "ea/%s", card_fw[fw_index].data);
49 err = request_firmware(fw_entry, name, &chip->pci->dev);
50 if (err < 0)
51 dev_err(chip->card->dev,
52 "get_firmware(): Firmware not available (%d)\n", err);
53 else
54 chip->fw_cache[fw_index] = *fw_entry;
55 return err;
56 }
57
58
59
free_firmware(const struct firmware * fw_entry,struct echoaudio * chip)60 static void free_firmware(const struct firmware *fw_entry,
61 struct echoaudio *chip)
62 {
63 dev_dbg(chip->card->dev, "firmware not released (kept in cache)\n");
64 }
65
66
67
free_firmware_cache(struct echoaudio * chip)68 static void free_firmware_cache(struct echoaudio *chip)
69 {
70 int i;
71
72 for (i = 0; i < 8 ; i++)
73 if (chip->fw_cache[i]) {
74 release_firmware(chip->fw_cache[i]);
75 dev_dbg(chip->card->dev, "release_firmware(%d)\n", i);
76 }
77 }
78
79
80
81 /******************************************************************************
82 PCM interface
83 ******************************************************************************/
84
audiopipe_free(struct snd_pcm_runtime * runtime)85 static void audiopipe_free(struct snd_pcm_runtime *runtime)
86 {
87 struct audiopipe *pipe = runtime->private_data;
88
89 if (pipe->sgpage.area)
90 snd_dma_free_pages(&pipe->sgpage);
91 kfree(pipe);
92 }
93
94
95
hw_rule_capture_format_by_channels(struct snd_pcm_hw_params * params,struct snd_pcm_hw_rule * rule)96 static int hw_rule_capture_format_by_channels(struct snd_pcm_hw_params *params,
97 struct snd_pcm_hw_rule *rule)
98 {
99 struct snd_interval *c = hw_param_interval(params,
100 SNDRV_PCM_HW_PARAM_CHANNELS);
101 struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
102 struct snd_mask fmt;
103
104 snd_mask_any(&fmt);
105
106 #ifndef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
107 /* >=2 channels cannot be S32_BE */
108 if (c->min == 2) {
109 fmt.bits[0] &= ~SNDRV_PCM_FMTBIT_S32_BE;
110 return snd_mask_refine(f, &fmt);
111 }
112 #endif
113 /* > 2 channels cannot be U8 and S32_BE */
114 if (c->min > 2) {
115 fmt.bits[0] &= ~(SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S32_BE);
116 return snd_mask_refine(f, &fmt);
117 }
118 /* Mono is ok with any format */
119 return 0;
120 }
121
122
123
hw_rule_capture_channels_by_format(struct snd_pcm_hw_params * params,struct snd_pcm_hw_rule * rule)124 static int hw_rule_capture_channels_by_format(struct snd_pcm_hw_params *params,
125 struct snd_pcm_hw_rule *rule)
126 {
127 struct snd_interval *c = hw_param_interval(params,
128 SNDRV_PCM_HW_PARAM_CHANNELS);
129 struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
130 struct snd_interval ch;
131
132 snd_interval_any(&ch);
133
134 /* S32_BE is mono (and stereo) only */
135 if (f->bits[0] == SNDRV_PCM_FMTBIT_S32_BE) {
136 ch.min = 1;
137 #ifdef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
138 ch.max = 2;
139 #else
140 ch.max = 1;
141 #endif
142 ch.integer = 1;
143 return snd_interval_refine(c, &ch);
144 }
145 /* U8 can be only mono or stereo */
146 if (f->bits[0] == SNDRV_PCM_FMTBIT_U8) {
147 ch.min = 1;
148 ch.max = 2;
149 ch.integer = 1;
150 return snd_interval_refine(c, &ch);
151 }
152 /* S16_LE, S24_3LE and S32_LE support any number of channels. */
153 return 0;
154 }
155
156
157
hw_rule_playback_format_by_channels(struct snd_pcm_hw_params * params,struct snd_pcm_hw_rule * rule)158 static int hw_rule_playback_format_by_channels(struct snd_pcm_hw_params *params,
159 struct snd_pcm_hw_rule *rule)
160 {
161 struct snd_interval *c = hw_param_interval(params,
162 SNDRV_PCM_HW_PARAM_CHANNELS);
163 struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
164 struct snd_mask fmt;
165 u64 fmask;
166 snd_mask_any(&fmt);
167
168 fmask = fmt.bits[0] + ((u64)fmt.bits[1] << 32);
169
170 /* >2 channels must be S16_LE, S24_3LE or S32_LE */
171 if (c->min > 2) {
172 fmask &= SNDRV_PCM_FMTBIT_S16_LE |
173 SNDRV_PCM_FMTBIT_S24_3LE |
174 SNDRV_PCM_FMTBIT_S32_LE;
175 /* 1 channel must be S32_BE or S32_LE */
176 } else if (c->max == 1)
177 fmask &= SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE;
178 #ifndef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
179 /* 2 channels cannot be S32_BE */
180 else if (c->min == 2 && c->max == 2)
181 fmask &= ~SNDRV_PCM_FMTBIT_S32_BE;
182 #endif
183 else
184 return 0;
185
186 fmt.bits[0] &= (u32)fmask;
187 fmt.bits[1] &= (u32)(fmask >> 32);
188 return snd_mask_refine(f, &fmt);
189 }
190
191
192
hw_rule_playback_channels_by_format(struct snd_pcm_hw_params * params,struct snd_pcm_hw_rule * rule)193 static int hw_rule_playback_channels_by_format(struct snd_pcm_hw_params *params,
194 struct snd_pcm_hw_rule *rule)
195 {
196 struct snd_interval *c = hw_param_interval(params,
197 SNDRV_PCM_HW_PARAM_CHANNELS);
198 struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
199 struct snd_interval ch;
200 u64 fmask;
201
202 snd_interval_any(&ch);
203 ch.integer = 1;
204 fmask = f->bits[0] + ((u64)f->bits[1] << 32);
205
206 /* S32_BE is mono (and stereo) only */
207 if (fmask == SNDRV_PCM_FMTBIT_S32_BE) {
208 ch.min = 1;
209 #ifdef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
210 ch.max = 2;
211 #else
212 ch.max = 1;
213 #endif
214 /* U8 is stereo only */
215 } else if (fmask == SNDRV_PCM_FMTBIT_U8)
216 ch.min = ch.max = 2;
217 /* S16_LE and S24_3LE must be at least stereo */
218 else if (!(fmask & ~(SNDRV_PCM_FMTBIT_S16_LE |
219 SNDRV_PCM_FMTBIT_S24_3LE)))
220 ch.min = 2;
221 else
222 return 0;
223
224 return snd_interval_refine(c, &ch);
225 }
226
227
228
229 /* Since the sample rate is a global setting, do allow the user to change the
230 sample rate only if there is only one pcm device open. */
hw_rule_sample_rate(struct snd_pcm_hw_params * params,struct snd_pcm_hw_rule * rule)231 static int hw_rule_sample_rate(struct snd_pcm_hw_params *params,
232 struct snd_pcm_hw_rule *rule)
233 {
234 struct snd_interval *rate = hw_param_interval(params,
235 SNDRV_PCM_HW_PARAM_RATE);
236 struct echoaudio *chip = rule->private;
237 struct snd_interval fixed;
238 int err;
239
240 mutex_lock(&chip->mode_mutex);
241
242 if (chip->can_set_rate) {
243 err = 0;
244 } else {
245 snd_interval_any(&fixed);
246 fixed.min = fixed.max = chip->sample_rate;
247 err = snd_interval_refine(rate, &fixed);
248 }
249
250 mutex_unlock(&chip->mode_mutex);
251 return err;
252 }
253
254
pcm_open(struct snd_pcm_substream * substream,signed char max_channels)255 static int pcm_open(struct snd_pcm_substream *substream,
256 signed char max_channels)
257 {
258 struct echoaudio *chip;
259 struct snd_pcm_runtime *runtime;
260 struct audiopipe *pipe;
261 int err, i;
262
263 if (max_channels <= 0)
264 return -EAGAIN;
265
266 chip = snd_pcm_substream_chip(substream);
267 runtime = substream->runtime;
268
269 pipe = kzalloc(sizeof(struct audiopipe), GFP_KERNEL);
270 if (!pipe)
271 return -ENOMEM;
272 pipe->index = -1; /* Not configured yet */
273
274 /* Set up hw capabilities and contraints */
275 memcpy(&pipe->hw, &pcm_hardware_skel, sizeof(struct snd_pcm_hardware));
276 dev_dbg(chip->card->dev, "max_channels=%d\n", max_channels);
277 pipe->constr.list = channels_list;
278 pipe->constr.mask = 0;
279 for (i = 0; channels_list[i] <= max_channels; i++);
280 pipe->constr.count = i;
281 if (pipe->hw.channels_max > max_channels)
282 pipe->hw.channels_max = max_channels;
283 if (chip->digital_mode == DIGITAL_MODE_ADAT) {
284 pipe->hw.rate_max = 48000;
285 pipe->hw.rates &= SNDRV_PCM_RATE_8000_48000;
286 }
287
288 runtime->hw = pipe->hw;
289 runtime->private_data = pipe;
290 runtime->private_free = audiopipe_free;
291 snd_pcm_set_sync(substream);
292
293 /* Only mono and any even number of channels are allowed */
294 err = snd_pcm_hw_constraint_list(runtime, 0,
295 SNDRV_PCM_HW_PARAM_CHANNELS,
296 &pipe->constr);
297 if (err < 0)
298 return err;
299
300 /* All periods should have the same size */
301 err = snd_pcm_hw_constraint_integer(runtime,
302 SNDRV_PCM_HW_PARAM_PERIODS);
303 if (err < 0)
304 return err;
305
306 /* The hw accesses memory in chunks 32 frames long and they should be
307 32-bytes-aligned. It's not a requirement, but it seems that IRQs are
308 generated with a resolution of 32 frames. Thus we need the following */
309 err = snd_pcm_hw_constraint_step(runtime, 0,
310 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 32);
311 if (err < 0)
312 return err;
313 err = snd_pcm_hw_constraint_step(runtime, 0,
314 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 32);
315 if (err < 0)
316 return err;
317
318 err = snd_pcm_hw_rule_add(substream->runtime, 0,
319 SNDRV_PCM_HW_PARAM_RATE,
320 hw_rule_sample_rate, chip,
321 SNDRV_PCM_HW_PARAM_RATE, -1);
322 if (err < 0)
323 return err;
324
325 /* Allocate a page for the scatter-gather list */
326 err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
327 &chip->pci->dev,
328 PAGE_SIZE, &pipe->sgpage);
329 if (err < 0) {
330 dev_err(chip->card->dev, "s-g list allocation failed\n");
331 return err;
332 }
333
334 /*
335 * Sole ownership required to set the rate
336 */
337
338 dev_dbg(chip->card->dev, "pcm_open opencount=%d can_set_rate=%d, rate_set=%d",
339 chip->opencount, chip->can_set_rate, chip->rate_set);
340
341 chip->opencount++;
342 if (chip->opencount > 1 && chip->rate_set)
343 chip->can_set_rate = 0;
344
345 return 0;
346 }
347
348
349
pcm_analog_in_open(struct snd_pcm_substream * substream)350 static int pcm_analog_in_open(struct snd_pcm_substream *substream)
351 {
352 struct echoaudio *chip = snd_pcm_substream_chip(substream);
353 int err;
354
355 err = pcm_open(substream,
356 num_analog_busses_in(chip) - substream->number);
357 if (err < 0)
358 return err;
359 err = snd_pcm_hw_rule_add(substream->runtime, 0,
360 SNDRV_PCM_HW_PARAM_CHANNELS,
361 hw_rule_capture_channels_by_format, NULL,
362 SNDRV_PCM_HW_PARAM_FORMAT, -1);
363 if (err < 0)
364 return err;
365 err = snd_pcm_hw_rule_add(substream->runtime, 0,
366 SNDRV_PCM_HW_PARAM_FORMAT,
367 hw_rule_capture_format_by_channels, NULL,
368 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
369 if (err < 0)
370 return err;
371
372 return 0;
373 }
374
375
376
pcm_analog_out_open(struct snd_pcm_substream * substream)377 static int pcm_analog_out_open(struct snd_pcm_substream *substream)
378 {
379 struct echoaudio *chip = snd_pcm_substream_chip(substream);
380 int max_channels, err;
381
382 #ifdef ECHOCARD_HAS_VMIXER
383 max_channels = num_pipes_out(chip);
384 #else
385 max_channels = num_analog_busses_out(chip);
386 #endif
387 err = pcm_open(substream, max_channels - substream->number);
388 if (err < 0)
389 return err;
390 err = snd_pcm_hw_rule_add(substream->runtime, 0,
391 SNDRV_PCM_HW_PARAM_CHANNELS,
392 hw_rule_playback_channels_by_format,
393 NULL,
394 SNDRV_PCM_HW_PARAM_FORMAT, -1);
395 if (err < 0)
396 return err;
397 err = snd_pcm_hw_rule_add(substream->runtime, 0,
398 SNDRV_PCM_HW_PARAM_FORMAT,
399 hw_rule_playback_format_by_channels,
400 NULL,
401 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
402 if (err < 0)
403 return err;
404
405 return 0;
406 }
407
408
409
410 #ifdef ECHOCARD_HAS_DIGITAL_IO
411
pcm_digital_in_open(struct snd_pcm_substream * substream)412 static int pcm_digital_in_open(struct snd_pcm_substream *substream)
413 {
414 struct echoaudio *chip = snd_pcm_substream_chip(substream);
415 int err, max_channels;
416
417 max_channels = num_digital_busses_in(chip) - substream->number;
418 mutex_lock(&chip->mode_mutex);
419 if (chip->digital_mode == DIGITAL_MODE_ADAT)
420 err = pcm_open(substream, max_channels);
421 else /* If the card has ADAT, subtract the 6 channels
422 * that S/PDIF doesn't have
423 */
424 err = pcm_open(substream, max_channels - ECHOCARD_HAS_ADAT);
425
426 if (err < 0)
427 goto din_exit;
428
429 err = snd_pcm_hw_rule_add(substream->runtime, 0,
430 SNDRV_PCM_HW_PARAM_CHANNELS,
431 hw_rule_capture_channels_by_format, NULL,
432 SNDRV_PCM_HW_PARAM_FORMAT, -1);
433 if (err < 0)
434 goto din_exit;
435 err = snd_pcm_hw_rule_add(substream->runtime, 0,
436 SNDRV_PCM_HW_PARAM_FORMAT,
437 hw_rule_capture_format_by_channels, NULL,
438 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
439 if (err < 0)
440 goto din_exit;
441
442 din_exit:
443 mutex_unlock(&chip->mode_mutex);
444 return err;
445 }
446
447
448
449 #ifndef ECHOCARD_HAS_VMIXER /* See the note in snd_echo_new_pcm() */
450
pcm_digital_out_open(struct snd_pcm_substream * substream)451 static int pcm_digital_out_open(struct snd_pcm_substream *substream)
452 {
453 struct echoaudio *chip = snd_pcm_substream_chip(substream);
454 int err, max_channels;
455
456 max_channels = num_digital_busses_out(chip) - substream->number;
457 mutex_lock(&chip->mode_mutex);
458 if (chip->digital_mode == DIGITAL_MODE_ADAT)
459 err = pcm_open(substream, max_channels);
460 else /* If the card has ADAT, subtract the 6 channels
461 * that S/PDIF doesn't have
462 */
463 err = pcm_open(substream, max_channels - ECHOCARD_HAS_ADAT);
464
465 if (err < 0)
466 goto dout_exit;
467
468 err = snd_pcm_hw_rule_add(substream->runtime, 0,
469 SNDRV_PCM_HW_PARAM_CHANNELS,
470 hw_rule_playback_channels_by_format,
471 NULL, SNDRV_PCM_HW_PARAM_FORMAT,
472 -1);
473 if (err < 0)
474 goto dout_exit;
475 err = snd_pcm_hw_rule_add(substream->runtime, 0,
476 SNDRV_PCM_HW_PARAM_FORMAT,
477 hw_rule_playback_format_by_channels,
478 NULL, SNDRV_PCM_HW_PARAM_CHANNELS,
479 -1);
480 if (err < 0)
481 goto dout_exit;
482
483 dout_exit:
484 mutex_unlock(&chip->mode_mutex);
485 return err;
486 }
487
488 #endif /* !ECHOCARD_HAS_VMIXER */
489
490 #endif /* ECHOCARD_HAS_DIGITAL_IO */
491
492
493
pcm_close(struct snd_pcm_substream * substream)494 static int pcm_close(struct snd_pcm_substream *substream)
495 {
496 struct echoaudio *chip = snd_pcm_substream_chip(substream);
497
498 /* Nothing to do here. Audio is already off and pipe will be
499 * freed by its callback
500 */
501
502 mutex_lock(&chip->mode_mutex);
503
504 dev_dbg(chip->card->dev, "pcm_open opencount=%d can_set_rate=%d, rate_set=%d",
505 chip->opencount, chip->can_set_rate, chip->rate_set);
506
507 chip->opencount--;
508
509 switch (chip->opencount) {
510 case 1:
511 chip->can_set_rate = 1;
512 break;
513
514 case 0:
515 chip->rate_set = 0;
516 break;
517 }
518
519 mutex_unlock(&chip->mode_mutex);
520 return 0;
521 }
522
523
524
525 /* Channel allocation and scatter-gather list setup */
init_engine(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * hw_params,int pipe_index,int interleave)526 static int init_engine(struct snd_pcm_substream *substream,
527 struct snd_pcm_hw_params *hw_params,
528 int pipe_index, int interleave)
529 {
530 struct echoaudio *chip;
531 int err, per, rest, page, edge, offs;
532 struct audiopipe *pipe;
533
534 chip = snd_pcm_substream_chip(substream);
535 pipe = (struct audiopipe *) substream->runtime->private_data;
536
537 /* Sets up che hardware. If it's already initialized, reset and
538 * redo with the new parameters
539 */
540 spin_lock_irq(&chip->lock);
541 if (pipe->index >= 0) {
542 dev_dbg(chip->card->dev, "hwp_ie free(%d)\n", pipe->index);
543 err = free_pipes(chip, pipe);
544 snd_BUG_ON(err);
545 chip->substream[pipe->index] = NULL;
546 }
547
548 err = allocate_pipes(chip, pipe, pipe_index, interleave);
549 if (err < 0) {
550 spin_unlock_irq(&chip->lock);
551 dev_err(chip->card->dev, "allocate_pipes(%d) err=%d\n",
552 pipe_index, err);
553 return err;
554 }
555 spin_unlock_irq(&chip->lock);
556 dev_dbg(chip->card->dev, "allocate_pipes()=%d\n", pipe_index);
557
558 dev_dbg(chip->card->dev,
559 "pcm_hw_params (bufsize=%dB periods=%d persize=%dB)\n",
560 params_buffer_bytes(hw_params), params_periods(hw_params),
561 params_period_bytes(hw_params));
562
563 sglist_init(chip, pipe);
564 edge = PAGE_SIZE;
565 for (offs = page = per = 0; offs < params_buffer_bytes(hw_params);
566 per++) {
567 rest = params_period_bytes(hw_params);
568 if (offs + rest > params_buffer_bytes(hw_params))
569 rest = params_buffer_bytes(hw_params) - offs;
570 while (rest) {
571 dma_addr_t addr;
572 addr = snd_pcm_sgbuf_get_addr(substream, offs);
573 if (rest <= edge - offs) {
574 sglist_add_mapping(chip, pipe, addr, rest);
575 sglist_add_irq(chip, pipe);
576 offs += rest;
577 rest = 0;
578 } else {
579 sglist_add_mapping(chip, pipe, addr,
580 edge - offs);
581 rest -= edge - offs;
582 offs = edge;
583 }
584 if (offs == edge) {
585 edge += PAGE_SIZE;
586 page++;
587 }
588 }
589 }
590
591 /* Close the ring buffer */
592 sglist_wrap(chip, pipe);
593
594 /* This stuff is used by the irq handler, so it must be
595 * initialized before chip->substream
596 */
597 pipe->last_period = 0;
598 pipe->last_counter = 0;
599 pipe->position = 0;
600 smp_wmb();
601 chip->substream[pipe_index] = substream;
602 chip->rate_set = 1;
603 spin_lock_irq(&chip->lock);
604 set_sample_rate(chip, hw_params->rate_num / hw_params->rate_den);
605 spin_unlock_irq(&chip->lock);
606 return 0;
607 }
608
609
610
pcm_analog_in_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * hw_params)611 static int pcm_analog_in_hw_params(struct snd_pcm_substream *substream,
612 struct snd_pcm_hw_params *hw_params)
613 {
614 struct echoaudio *chip = snd_pcm_substream_chip(substream);
615
616 return init_engine(substream, hw_params, px_analog_in(chip) +
617 substream->number, params_channels(hw_params));
618 }
619
620
621
pcm_analog_out_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * hw_params)622 static int pcm_analog_out_hw_params(struct snd_pcm_substream *substream,
623 struct snd_pcm_hw_params *hw_params)
624 {
625 return init_engine(substream, hw_params, substream->number,
626 params_channels(hw_params));
627 }
628
629
630
631 #ifdef ECHOCARD_HAS_DIGITAL_IO
632
pcm_digital_in_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * hw_params)633 static int pcm_digital_in_hw_params(struct snd_pcm_substream *substream,
634 struct snd_pcm_hw_params *hw_params)
635 {
636 struct echoaudio *chip = snd_pcm_substream_chip(substream);
637
638 return init_engine(substream, hw_params, px_digital_in(chip) +
639 substream->number, params_channels(hw_params));
640 }
641
642
643
644 #ifndef ECHOCARD_HAS_VMIXER /* See the note in snd_echo_new_pcm() */
pcm_digital_out_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * hw_params)645 static int pcm_digital_out_hw_params(struct snd_pcm_substream *substream,
646 struct snd_pcm_hw_params *hw_params)
647 {
648 struct echoaudio *chip = snd_pcm_substream_chip(substream);
649
650 return init_engine(substream, hw_params, px_digital_out(chip) +
651 substream->number, params_channels(hw_params));
652 }
653 #endif /* !ECHOCARD_HAS_VMIXER */
654
655 #endif /* ECHOCARD_HAS_DIGITAL_IO */
656
657
658
pcm_hw_free(struct snd_pcm_substream * substream)659 static int pcm_hw_free(struct snd_pcm_substream *substream)
660 {
661 struct echoaudio *chip;
662 struct audiopipe *pipe;
663
664 chip = snd_pcm_substream_chip(substream);
665 pipe = (struct audiopipe *) substream->runtime->private_data;
666
667 spin_lock_irq(&chip->lock);
668 if (pipe->index >= 0) {
669 dev_dbg(chip->card->dev, "pcm_hw_free(%d)\n", pipe->index);
670 free_pipes(chip, pipe);
671 chip->substream[pipe->index] = NULL;
672 pipe->index = -1;
673 }
674 spin_unlock_irq(&chip->lock);
675
676 return 0;
677 }
678
679
680
pcm_prepare(struct snd_pcm_substream * substream)681 static int pcm_prepare(struct snd_pcm_substream *substream)
682 {
683 struct echoaudio *chip = snd_pcm_substream_chip(substream);
684 struct snd_pcm_runtime *runtime = substream->runtime;
685 struct audioformat format;
686 int pipe_index = ((struct audiopipe *)runtime->private_data)->index;
687
688 dev_dbg(chip->card->dev, "Prepare rate=%d format=%d channels=%d\n",
689 runtime->rate, runtime->format, runtime->channels);
690 format.interleave = runtime->channels;
691 format.data_are_bigendian = 0;
692 format.mono_to_stereo = 0;
693 switch (runtime->format) {
694 case SNDRV_PCM_FORMAT_U8:
695 format.bits_per_sample = 8;
696 break;
697 case SNDRV_PCM_FORMAT_S16_LE:
698 format.bits_per_sample = 16;
699 break;
700 case SNDRV_PCM_FORMAT_S24_3LE:
701 format.bits_per_sample = 24;
702 break;
703 case SNDRV_PCM_FORMAT_S32_BE:
704 format.data_are_bigendian = 1;
705 fallthrough;
706 case SNDRV_PCM_FORMAT_S32_LE:
707 format.bits_per_sample = 32;
708 break;
709 default:
710 dev_err(chip->card->dev,
711 "Prepare error: unsupported format %d\n",
712 runtime->format);
713 return -EINVAL;
714 }
715
716 if (snd_BUG_ON(pipe_index >= px_num(chip)))
717 return -EINVAL;
718
719 /*
720 * We passed checks we can do independently; now take
721 * exclusive control
722 */
723
724 spin_lock_irq(&chip->lock);
725
726 if (snd_BUG_ON(!is_pipe_allocated(chip, pipe_index))) {
727 spin_unlock_irq(&chip->lock);
728 return -EINVAL;
729 }
730
731 set_audio_format(chip, pipe_index, &format);
732 spin_unlock_irq(&chip->lock);
733
734 return 0;
735 }
736
737
738
pcm_trigger(struct snd_pcm_substream * substream,int cmd)739 static int pcm_trigger(struct snd_pcm_substream *substream, int cmd)
740 {
741 struct echoaudio *chip = snd_pcm_substream_chip(substream);
742 struct audiopipe *pipe;
743 int i, err;
744 u32 channelmask = 0;
745 struct snd_pcm_substream *s;
746
747 snd_pcm_group_for_each_entry(s, substream) {
748 for (i = 0; i < DSP_MAXPIPES; i++) {
749 if (s == chip->substream[i]) {
750 channelmask |= 1 << i;
751 snd_pcm_trigger_done(s, substream);
752 }
753 }
754 }
755
756 spin_lock(&chip->lock);
757 switch (cmd) {
758 case SNDRV_PCM_TRIGGER_RESUME:
759 case SNDRV_PCM_TRIGGER_START:
760 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
761 for (i = 0; i < DSP_MAXPIPES; i++) {
762 if (channelmask & (1 << i)) {
763 pipe = chip->substream[i]->runtime->private_data;
764 switch (pipe->state) {
765 case PIPE_STATE_STOPPED:
766 pipe->last_period = 0;
767 pipe->last_counter = 0;
768 pipe->position = 0;
769 *pipe->dma_counter = 0;
770 fallthrough;
771 case PIPE_STATE_PAUSED:
772 pipe->state = PIPE_STATE_STARTED;
773 break;
774 case PIPE_STATE_STARTED:
775 break;
776 }
777 }
778 }
779 err = start_transport(chip, channelmask,
780 chip->pipe_cyclic_mask);
781 break;
782 case SNDRV_PCM_TRIGGER_SUSPEND:
783 case SNDRV_PCM_TRIGGER_STOP:
784 for (i = 0; i < DSP_MAXPIPES; i++) {
785 if (channelmask & (1 << i)) {
786 pipe = chip->substream[i]->runtime->private_data;
787 pipe->state = PIPE_STATE_STOPPED;
788 }
789 }
790 err = stop_transport(chip, channelmask);
791 break;
792 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
793 for (i = 0; i < DSP_MAXPIPES; i++) {
794 if (channelmask & (1 << i)) {
795 pipe = chip->substream[i]->runtime->private_data;
796 pipe->state = PIPE_STATE_PAUSED;
797 }
798 }
799 err = pause_transport(chip, channelmask);
800 break;
801 default:
802 err = -EINVAL;
803 }
804 spin_unlock(&chip->lock);
805 return err;
806 }
807
808
809
pcm_pointer(struct snd_pcm_substream * substream)810 static snd_pcm_uframes_t pcm_pointer(struct snd_pcm_substream *substream)
811 {
812 struct snd_pcm_runtime *runtime = substream->runtime;
813 struct audiopipe *pipe = runtime->private_data;
814 u32 counter, step;
815
816 /*
817 * IRQ handling runs concurrently. Do not share tracking of
818 * counter with it, which would race or require locking
819 */
820
821 counter = le32_to_cpu(*pipe->dma_counter); /* presumed atomic */
822
823 step = counter - pipe->last_counter; /* handles wrapping */
824 pipe->last_counter = counter;
825
826 /* counter doesn't neccessarily wrap on a multiple of
827 * buffer_size, so can't derive the position; must
828 * accumulate */
829
830 pipe->position += step;
831 pipe->position %= frames_to_bytes(runtime, runtime->buffer_size); /* wrap */
832
833 return bytes_to_frames(runtime, pipe->position);
834 }
835
836
837
838 /* pcm *_ops structures */
839 static const struct snd_pcm_ops analog_playback_ops = {
840 .open = pcm_analog_out_open,
841 .close = pcm_close,
842 .hw_params = pcm_analog_out_hw_params,
843 .hw_free = pcm_hw_free,
844 .prepare = pcm_prepare,
845 .trigger = pcm_trigger,
846 .pointer = pcm_pointer,
847 };
848 static const struct snd_pcm_ops analog_capture_ops = {
849 .open = pcm_analog_in_open,
850 .close = pcm_close,
851 .hw_params = pcm_analog_in_hw_params,
852 .hw_free = pcm_hw_free,
853 .prepare = pcm_prepare,
854 .trigger = pcm_trigger,
855 .pointer = pcm_pointer,
856 };
857 #ifdef ECHOCARD_HAS_DIGITAL_IO
858 #ifndef ECHOCARD_HAS_VMIXER
859 static const struct snd_pcm_ops digital_playback_ops = {
860 .open = pcm_digital_out_open,
861 .close = pcm_close,
862 .hw_params = pcm_digital_out_hw_params,
863 .hw_free = pcm_hw_free,
864 .prepare = pcm_prepare,
865 .trigger = pcm_trigger,
866 .pointer = pcm_pointer,
867 };
868 #endif /* !ECHOCARD_HAS_VMIXER */
869 static const struct snd_pcm_ops digital_capture_ops = {
870 .open = pcm_digital_in_open,
871 .close = pcm_close,
872 .hw_params = pcm_digital_in_hw_params,
873 .hw_free = pcm_hw_free,
874 .prepare = pcm_prepare,
875 .trigger = pcm_trigger,
876 .pointer = pcm_pointer,
877 };
878 #endif /* ECHOCARD_HAS_DIGITAL_IO */
879
880
881
882 /* Preallocate memory only for the first substream because it's the most
883 * used one
884 */
snd_echo_preallocate_pages(struct snd_pcm * pcm,struct device * dev)885 static void snd_echo_preallocate_pages(struct snd_pcm *pcm, struct device *dev)
886 {
887 struct snd_pcm_substream *ss;
888 int stream;
889
890 for (stream = 0; stream < 2; stream++)
891 for (ss = pcm->streams[stream].substream; ss; ss = ss->next)
892 snd_pcm_set_managed_buffer(ss, SNDRV_DMA_TYPE_DEV_SG,
893 dev,
894 ss->number ? 0 : 128<<10,
895 256<<10);
896 }
897
898
899
900 /*<--snd_echo_probe() */
snd_echo_new_pcm(struct echoaudio * chip)901 static int snd_echo_new_pcm(struct echoaudio *chip)
902 {
903 struct snd_pcm *pcm;
904 int err;
905
906 #ifdef ECHOCARD_HAS_VMIXER
907 /* This card has a Vmixer, that is there is no direct mapping from PCM
908 streams to physical outputs. The user can mix the streams as he wishes
909 via control interface and it's possible to send any stream to any
910 output, thus it makes no sense to keep analog and digital outputs
911 separated */
912
913 /* PCM#0 Virtual outputs and analog inputs */
914 err = snd_pcm_new(chip->card, "PCM", 0, num_pipes_out(chip),
915 num_analog_busses_in(chip), &pcm);
916 if (err < 0)
917 return err;
918 pcm->private_data = chip;
919 chip->analog_pcm = pcm;
920 strscpy(pcm->name, chip->card->shortname);
921 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &analog_playback_ops);
922 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &analog_capture_ops);
923 snd_echo_preallocate_pages(pcm, &chip->pci->dev);
924
925 #ifdef ECHOCARD_HAS_DIGITAL_IO
926 /* PCM#1 Digital inputs, no outputs */
927 err = snd_pcm_new(chip->card, "Digital PCM", 1, 0,
928 num_digital_busses_in(chip), &pcm);
929 if (err < 0)
930 return err;
931 pcm->private_data = chip;
932 chip->digital_pcm = pcm;
933 strscpy(pcm->name, chip->card->shortname);
934 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &digital_capture_ops);
935 snd_echo_preallocate_pages(pcm, &chip->pci->dev);
936 #endif /* ECHOCARD_HAS_DIGITAL_IO */
937
938 #else /* ECHOCARD_HAS_VMIXER */
939
940 /* The card can manage substreams formed by analog and digital channels
941 at the same time, but I prefer to keep analog and digital channels
942 separated, because that mixed thing is confusing and useless. So we
943 register two PCM devices: */
944
945 /* PCM#0 Analog i/o */
946 err = snd_pcm_new(chip->card, "Analog PCM", 0,
947 num_analog_busses_out(chip),
948 num_analog_busses_in(chip), &pcm);
949 if (err < 0)
950 return err;
951 pcm->private_data = chip;
952 chip->analog_pcm = pcm;
953 strscpy(pcm->name, chip->card->shortname);
954 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &analog_playback_ops);
955 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &analog_capture_ops);
956 snd_echo_preallocate_pages(pcm, &chip->pci->dev);
957
958 #ifdef ECHOCARD_HAS_DIGITAL_IO
959 /* PCM#1 Digital i/o */
960 err = snd_pcm_new(chip->card, "Digital PCM", 1,
961 num_digital_busses_out(chip),
962 num_digital_busses_in(chip), &pcm);
963 if (err < 0)
964 return err;
965 pcm->private_data = chip;
966 chip->digital_pcm = pcm;
967 strscpy(pcm->name, chip->card->shortname);
968 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &digital_playback_ops);
969 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &digital_capture_ops);
970 snd_echo_preallocate_pages(pcm, &chip->pci->dev);
971 #endif /* ECHOCARD_HAS_DIGITAL_IO */
972
973 #endif /* ECHOCARD_HAS_VMIXER */
974
975 return 0;
976 }
977
978
979
980
981 /******************************************************************************
982 Control interface
983 ******************************************************************************/
984
985 #if !defined(ECHOCARD_HAS_VMIXER) || defined(ECHOCARD_HAS_LINE_OUT_GAIN)
986
987 /******************* PCM output volume *******************/
snd_echo_output_gain_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)988 static int snd_echo_output_gain_info(struct snd_kcontrol *kcontrol,
989 struct snd_ctl_elem_info *uinfo)
990 {
991 struct echoaudio *chip;
992
993 chip = snd_kcontrol_chip(kcontrol);
994 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
995 uinfo->count = num_busses_out(chip);
996 uinfo->value.integer.min = ECHOGAIN_MINOUT;
997 uinfo->value.integer.max = ECHOGAIN_MAXOUT;
998 return 0;
999 }
1000
snd_echo_output_gain_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1001 static int snd_echo_output_gain_get(struct snd_kcontrol *kcontrol,
1002 struct snd_ctl_elem_value *ucontrol)
1003 {
1004 struct echoaudio *chip;
1005 int c;
1006
1007 chip = snd_kcontrol_chip(kcontrol);
1008 for (c = 0; c < num_busses_out(chip); c++)
1009 ucontrol->value.integer.value[c] = chip->output_gain[c];
1010 return 0;
1011 }
1012
snd_echo_output_gain_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1013 static int snd_echo_output_gain_put(struct snd_kcontrol *kcontrol,
1014 struct snd_ctl_elem_value *ucontrol)
1015 {
1016 struct echoaudio *chip;
1017 int c, changed, gain;
1018
1019 changed = 0;
1020 chip = snd_kcontrol_chip(kcontrol);
1021 spin_lock_irq(&chip->lock);
1022 for (c = 0; c < num_busses_out(chip); c++) {
1023 gain = ucontrol->value.integer.value[c];
1024 /* Ignore out of range values */
1025 if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT)
1026 continue;
1027 if (chip->output_gain[c] != gain) {
1028 set_output_gain(chip, c, gain);
1029 changed = 1;
1030 }
1031 }
1032 if (changed)
1033 update_output_line_level(chip);
1034 spin_unlock_irq(&chip->lock);
1035 return changed;
1036 }
1037
1038 #ifdef ECHOCARD_HAS_LINE_OUT_GAIN
1039 /* On the Mia this one controls the line-out volume */
1040 static const struct snd_kcontrol_new snd_echo_line_output_gain = {
1041 .name = "Line Playback Volume",
1042 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1043 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1044 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1045 .info = snd_echo_output_gain_info,
1046 .get = snd_echo_output_gain_get,
1047 .put = snd_echo_output_gain_put,
1048 .tlv = {.p = db_scale_output_gain},
1049 };
1050 #else
1051 static const struct snd_kcontrol_new snd_echo_pcm_output_gain = {
1052 .name = "PCM Playback Volume",
1053 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1054 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1055 .info = snd_echo_output_gain_info,
1056 .get = snd_echo_output_gain_get,
1057 .put = snd_echo_output_gain_put,
1058 .tlv = {.p = db_scale_output_gain},
1059 };
1060 #endif
1061
1062 #endif /* !ECHOCARD_HAS_VMIXER || ECHOCARD_HAS_LINE_OUT_GAIN */
1063
1064
1065
1066 #ifdef ECHOCARD_HAS_INPUT_GAIN
1067
1068 /******************* Analog input volume *******************/
snd_echo_input_gain_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1069 static int snd_echo_input_gain_info(struct snd_kcontrol *kcontrol,
1070 struct snd_ctl_elem_info *uinfo)
1071 {
1072 struct echoaudio *chip;
1073
1074 chip = snd_kcontrol_chip(kcontrol);
1075 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1076 uinfo->count = num_analog_busses_in(chip);
1077 uinfo->value.integer.min = ECHOGAIN_MININP;
1078 uinfo->value.integer.max = ECHOGAIN_MAXINP;
1079 return 0;
1080 }
1081
snd_echo_input_gain_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1082 static int snd_echo_input_gain_get(struct snd_kcontrol *kcontrol,
1083 struct snd_ctl_elem_value *ucontrol)
1084 {
1085 struct echoaudio *chip;
1086 int c;
1087
1088 chip = snd_kcontrol_chip(kcontrol);
1089 for (c = 0; c < num_analog_busses_in(chip); c++)
1090 ucontrol->value.integer.value[c] = chip->input_gain[c];
1091 return 0;
1092 }
1093
snd_echo_input_gain_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1094 static int snd_echo_input_gain_put(struct snd_kcontrol *kcontrol,
1095 struct snd_ctl_elem_value *ucontrol)
1096 {
1097 struct echoaudio *chip;
1098 int c, gain, changed;
1099
1100 changed = 0;
1101 chip = snd_kcontrol_chip(kcontrol);
1102 spin_lock_irq(&chip->lock);
1103 for (c = 0; c < num_analog_busses_in(chip); c++) {
1104 gain = ucontrol->value.integer.value[c];
1105 /* Ignore out of range values */
1106 if (gain < ECHOGAIN_MININP || gain > ECHOGAIN_MAXINP)
1107 continue;
1108 if (chip->input_gain[c] != gain) {
1109 set_input_gain(chip, c, gain);
1110 changed = 1;
1111 }
1112 }
1113 if (changed)
1114 update_input_line_level(chip);
1115 spin_unlock_irq(&chip->lock);
1116 return changed;
1117 }
1118
1119 static const DECLARE_TLV_DB_SCALE(db_scale_input_gain, -2500, 50, 0);
1120
1121 static const struct snd_kcontrol_new snd_echo_line_input_gain = {
1122 .name = "Line Capture Volume",
1123 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1124 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1125 .info = snd_echo_input_gain_info,
1126 .get = snd_echo_input_gain_get,
1127 .put = snd_echo_input_gain_put,
1128 .tlv = {.p = db_scale_input_gain},
1129 };
1130
1131 #endif /* ECHOCARD_HAS_INPUT_GAIN */
1132
1133
1134
1135 #ifdef ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL
1136
1137 /************ Analog output nominal level (+4dBu / -10dBV) ***************/
snd_echo_output_nominal_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1138 static int snd_echo_output_nominal_info (struct snd_kcontrol *kcontrol,
1139 struct snd_ctl_elem_info *uinfo)
1140 {
1141 struct echoaudio *chip;
1142
1143 chip = snd_kcontrol_chip(kcontrol);
1144 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1145 uinfo->count = num_analog_busses_out(chip);
1146 uinfo->value.integer.min = 0;
1147 uinfo->value.integer.max = 1;
1148 return 0;
1149 }
1150
snd_echo_output_nominal_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1151 static int snd_echo_output_nominal_get(struct snd_kcontrol *kcontrol,
1152 struct snd_ctl_elem_value *ucontrol)
1153 {
1154 struct echoaudio *chip;
1155 int c;
1156
1157 chip = snd_kcontrol_chip(kcontrol);
1158 for (c = 0; c < num_analog_busses_out(chip); c++)
1159 ucontrol->value.integer.value[c] = chip->nominal_level[c];
1160 return 0;
1161 }
1162
snd_echo_output_nominal_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1163 static int snd_echo_output_nominal_put(struct snd_kcontrol *kcontrol,
1164 struct snd_ctl_elem_value *ucontrol)
1165 {
1166 struct echoaudio *chip;
1167 int c, changed;
1168
1169 changed = 0;
1170 chip = snd_kcontrol_chip(kcontrol);
1171 spin_lock_irq(&chip->lock);
1172 for (c = 0; c < num_analog_busses_out(chip); c++) {
1173 if (chip->nominal_level[c] != ucontrol->value.integer.value[c]) {
1174 set_nominal_level(chip, c,
1175 ucontrol->value.integer.value[c]);
1176 changed = 1;
1177 }
1178 }
1179 if (changed)
1180 update_output_line_level(chip);
1181 spin_unlock_irq(&chip->lock);
1182 return changed;
1183 }
1184
1185 static const struct snd_kcontrol_new snd_echo_output_nominal_level = {
1186 .name = "Line Playback Switch (-10dBV)",
1187 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1188 .info = snd_echo_output_nominal_info,
1189 .get = snd_echo_output_nominal_get,
1190 .put = snd_echo_output_nominal_put,
1191 };
1192
1193 #endif /* ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL */
1194
1195
1196
1197 #ifdef ECHOCARD_HAS_INPUT_NOMINAL_LEVEL
1198
1199 /*************** Analog input nominal level (+4dBu / -10dBV) ***************/
snd_echo_input_nominal_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1200 static int snd_echo_input_nominal_info(struct snd_kcontrol *kcontrol,
1201 struct snd_ctl_elem_info *uinfo)
1202 {
1203 struct echoaudio *chip;
1204
1205 chip = snd_kcontrol_chip(kcontrol);
1206 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1207 uinfo->count = num_analog_busses_in(chip);
1208 uinfo->value.integer.min = 0;
1209 uinfo->value.integer.max = 1;
1210 return 0;
1211 }
1212
snd_echo_input_nominal_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1213 static int snd_echo_input_nominal_get(struct snd_kcontrol *kcontrol,
1214 struct snd_ctl_elem_value *ucontrol)
1215 {
1216 struct echoaudio *chip;
1217 int c;
1218
1219 chip = snd_kcontrol_chip(kcontrol);
1220 for (c = 0; c < num_analog_busses_in(chip); c++)
1221 ucontrol->value.integer.value[c] =
1222 chip->nominal_level[bx_analog_in(chip) + c];
1223 return 0;
1224 }
1225
snd_echo_input_nominal_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1226 static int snd_echo_input_nominal_put(struct snd_kcontrol *kcontrol,
1227 struct snd_ctl_elem_value *ucontrol)
1228 {
1229 struct echoaudio *chip;
1230 int c, changed;
1231
1232 changed = 0;
1233 chip = snd_kcontrol_chip(kcontrol);
1234 spin_lock_irq(&chip->lock);
1235 for (c = 0; c < num_analog_busses_in(chip); c++) {
1236 if (chip->nominal_level[bx_analog_in(chip) + c] !=
1237 ucontrol->value.integer.value[c]) {
1238 set_nominal_level(chip, bx_analog_in(chip) + c,
1239 ucontrol->value.integer.value[c]);
1240 changed = 1;
1241 }
1242 }
1243 if (changed)
1244 update_output_line_level(chip); /* "Output" is not a mistake
1245 * here.
1246 */
1247 spin_unlock_irq(&chip->lock);
1248 return changed;
1249 }
1250
1251 static const struct snd_kcontrol_new snd_echo_intput_nominal_level = {
1252 .name = "Line Capture Switch (-10dBV)",
1253 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1254 .info = snd_echo_input_nominal_info,
1255 .get = snd_echo_input_nominal_get,
1256 .put = snd_echo_input_nominal_put,
1257 };
1258
1259 #endif /* ECHOCARD_HAS_INPUT_NOMINAL_LEVEL */
1260
1261
1262
1263 #ifdef ECHOCARD_HAS_MONITOR
1264
1265 /******************* Monitor mixer *******************/
snd_echo_mixer_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1266 static int snd_echo_mixer_info(struct snd_kcontrol *kcontrol,
1267 struct snd_ctl_elem_info *uinfo)
1268 {
1269 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1270 uinfo->count = 1;
1271 uinfo->value.integer.min = ECHOGAIN_MINOUT;
1272 uinfo->value.integer.max = ECHOGAIN_MAXOUT;
1273 return 0;
1274 }
1275
snd_echo_mixer_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1276 static int snd_echo_mixer_get(struct snd_kcontrol *kcontrol,
1277 struct snd_ctl_elem_value *ucontrol)
1278 {
1279 struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1280 unsigned int out = ucontrol->id.index / num_busses_in(chip);
1281 unsigned int in = ucontrol->id.index % num_busses_in(chip);
1282
1283 if (out >= ECHO_MAXAUDIOOUTPUTS || in >= ECHO_MAXAUDIOINPUTS)
1284 return -EINVAL;
1285
1286 ucontrol->value.integer.value[0] = chip->monitor_gain[out][in];
1287 return 0;
1288 }
1289
snd_echo_mixer_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1290 static int snd_echo_mixer_put(struct snd_kcontrol *kcontrol,
1291 struct snd_ctl_elem_value *ucontrol)
1292 {
1293 struct echoaudio *chip;
1294 int changed, gain;
1295 unsigned int out, in;
1296
1297 changed = 0;
1298 chip = snd_kcontrol_chip(kcontrol);
1299 out = ucontrol->id.index / num_busses_in(chip);
1300 in = ucontrol->id.index % num_busses_in(chip);
1301 if (out >= ECHO_MAXAUDIOOUTPUTS || in >= ECHO_MAXAUDIOINPUTS)
1302 return -EINVAL;
1303 gain = ucontrol->value.integer.value[0];
1304 if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT)
1305 return -EINVAL;
1306 if (chip->monitor_gain[out][in] != gain) {
1307 spin_lock_irq(&chip->lock);
1308 set_monitor_gain(chip, out, in, gain);
1309 update_output_line_level(chip);
1310 spin_unlock_irq(&chip->lock);
1311 changed = 1;
1312 }
1313 return changed;
1314 }
1315
1316 static struct snd_kcontrol_new snd_echo_monitor_mixer = {
1317 .name = "Monitor Mixer Volume",
1318 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1319 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1320 .info = snd_echo_mixer_info,
1321 .get = snd_echo_mixer_get,
1322 .put = snd_echo_mixer_put,
1323 .tlv = {.p = db_scale_output_gain},
1324 };
1325
1326 #endif /* ECHOCARD_HAS_MONITOR */
1327
1328
1329
1330 #ifdef ECHOCARD_HAS_VMIXER
1331
1332 /******************* Vmixer *******************/
snd_echo_vmixer_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1333 static int snd_echo_vmixer_info(struct snd_kcontrol *kcontrol,
1334 struct snd_ctl_elem_info *uinfo)
1335 {
1336 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1337 uinfo->count = 1;
1338 uinfo->value.integer.min = ECHOGAIN_MINOUT;
1339 uinfo->value.integer.max = ECHOGAIN_MAXOUT;
1340 return 0;
1341 }
1342
snd_echo_vmixer_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1343 static int snd_echo_vmixer_get(struct snd_kcontrol *kcontrol,
1344 struct snd_ctl_elem_value *ucontrol)
1345 {
1346 struct echoaudio *chip;
1347
1348 chip = snd_kcontrol_chip(kcontrol);
1349 ucontrol->value.integer.value[0] =
1350 chip->vmixer_gain[ucontrol->id.index / num_pipes_out(chip)]
1351 [ucontrol->id.index % num_pipes_out(chip)];
1352 return 0;
1353 }
1354
snd_echo_vmixer_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1355 static int snd_echo_vmixer_put(struct snd_kcontrol *kcontrol,
1356 struct snd_ctl_elem_value *ucontrol)
1357 {
1358 struct echoaudio *chip;
1359 int gain, changed;
1360 short vch, out;
1361
1362 changed = 0;
1363 chip = snd_kcontrol_chip(kcontrol);
1364 out = ucontrol->id.index / num_pipes_out(chip);
1365 vch = ucontrol->id.index % num_pipes_out(chip);
1366 gain = ucontrol->value.integer.value[0];
1367 if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT)
1368 return -EINVAL;
1369 if (chip->vmixer_gain[out][vch] != ucontrol->value.integer.value[0]) {
1370 spin_lock_irq(&chip->lock);
1371 set_vmixer_gain(chip, out, vch, ucontrol->value.integer.value[0]);
1372 update_vmixer_level(chip);
1373 spin_unlock_irq(&chip->lock);
1374 changed = 1;
1375 }
1376 return changed;
1377 }
1378
1379 static struct snd_kcontrol_new snd_echo_vmixer = {
1380 .name = "VMixer Volume",
1381 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1382 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1383 .info = snd_echo_vmixer_info,
1384 .get = snd_echo_vmixer_get,
1385 .put = snd_echo_vmixer_put,
1386 .tlv = {.p = db_scale_output_gain},
1387 };
1388
1389 #endif /* ECHOCARD_HAS_VMIXER */
1390
1391
1392
1393 #ifdef ECHOCARD_HAS_DIGITAL_MODE_SWITCH
1394
1395 /******************* Digital mode switch *******************/
snd_echo_digital_mode_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1396 static int snd_echo_digital_mode_info(struct snd_kcontrol *kcontrol,
1397 struct snd_ctl_elem_info *uinfo)
1398 {
1399 static const char * const names[4] = {
1400 "S/PDIF Coaxial", "S/PDIF Optical", "ADAT Optical",
1401 "S/PDIF Cdrom"
1402 };
1403 struct echoaudio *chip;
1404
1405 chip = snd_kcontrol_chip(kcontrol);
1406 return snd_ctl_enum_info(uinfo, 1, chip->num_digital_modes, names);
1407 }
1408
snd_echo_digital_mode_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1409 static int snd_echo_digital_mode_get(struct snd_kcontrol *kcontrol,
1410 struct snd_ctl_elem_value *ucontrol)
1411 {
1412 struct echoaudio *chip;
1413 int i, mode;
1414
1415 chip = snd_kcontrol_chip(kcontrol);
1416 mode = chip->digital_mode;
1417 for (i = chip->num_digital_modes - 1; i >= 0; i--)
1418 if (mode == chip->digital_mode_list[i]) {
1419 ucontrol->value.enumerated.item[0] = i;
1420 break;
1421 }
1422 return 0;
1423 }
1424
snd_echo_digital_mode_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1425 static int snd_echo_digital_mode_put(struct snd_kcontrol *kcontrol,
1426 struct snd_ctl_elem_value *ucontrol)
1427 {
1428 struct echoaudio *chip;
1429 int changed;
1430 unsigned short emode, dmode;
1431
1432 changed = 0;
1433 chip = snd_kcontrol_chip(kcontrol);
1434
1435 emode = ucontrol->value.enumerated.item[0];
1436 if (emode >= chip->num_digital_modes)
1437 return -EINVAL;
1438 dmode = chip->digital_mode_list[emode];
1439
1440 if (dmode != chip->digital_mode) {
1441 /* mode_mutex is required to make this operation atomic wrt
1442 pcm_digital_*_open() and set_input_clock() functions. */
1443 mutex_lock(&chip->mode_mutex);
1444
1445 /* Do not allow the user to change the digital mode when a pcm
1446 device is open because it also changes the number of channels
1447 and the allowed sample rates */
1448 if (chip->opencount) {
1449 changed = -EAGAIN;
1450 } else {
1451 changed = set_digital_mode(chip, dmode);
1452 /* If we had to change the clock source, report it */
1453 if (changed > 0 && chip->clock_src_ctl) {
1454 snd_ctl_notify(chip->card,
1455 SNDRV_CTL_EVENT_MASK_VALUE,
1456 &chip->clock_src_ctl->id);
1457 dev_dbg(chip->card->dev,
1458 "SDM() =%d\n", changed);
1459 }
1460 if (changed >= 0)
1461 changed = 1; /* No errors */
1462 }
1463 mutex_unlock(&chip->mode_mutex);
1464 }
1465 return changed;
1466 }
1467
1468 static const struct snd_kcontrol_new snd_echo_digital_mode_switch = {
1469 .name = "Digital mode Switch",
1470 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1471 .info = snd_echo_digital_mode_info,
1472 .get = snd_echo_digital_mode_get,
1473 .put = snd_echo_digital_mode_put,
1474 };
1475
1476 #endif /* ECHOCARD_HAS_DIGITAL_MODE_SWITCH */
1477
1478
1479
1480 #ifdef ECHOCARD_HAS_DIGITAL_IO
1481
1482 /******************* S/PDIF mode switch *******************/
snd_echo_spdif_mode_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1483 static int snd_echo_spdif_mode_info(struct snd_kcontrol *kcontrol,
1484 struct snd_ctl_elem_info *uinfo)
1485 {
1486 static const char * const names[2] = {"Consumer", "Professional"};
1487
1488 return snd_ctl_enum_info(uinfo, 1, 2, names);
1489 }
1490
snd_echo_spdif_mode_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1491 static int snd_echo_spdif_mode_get(struct snd_kcontrol *kcontrol,
1492 struct snd_ctl_elem_value *ucontrol)
1493 {
1494 struct echoaudio *chip;
1495
1496 chip = snd_kcontrol_chip(kcontrol);
1497 ucontrol->value.enumerated.item[0] = !!chip->professional_spdif;
1498 return 0;
1499 }
1500
snd_echo_spdif_mode_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1501 static int snd_echo_spdif_mode_put(struct snd_kcontrol *kcontrol,
1502 struct snd_ctl_elem_value *ucontrol)
1503 {
1504 struct echoaudio *chip;
1505 int mode;
1506
1507 chip = snd_kcontrol_chip(kcontrol);
1508 mode = !!ucontrol->value.enumerated.item[0];
1509 if (mode != chip->professional_spdif) {
1510 spin_lock_irq(&chip->lock);
1511 set_professional_spdif(chip, mode);
1512 spin_unlock_irq(&chip->lock);
1513 return 1;
1514 }
1515 return 0;
1516 }
1517
1518 static const struct snd_kcontrol_new snd_echo_spdif_mode_switch = {
1519 .name = "S/PDIF mode Switch",
1520 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1521 .info = snd_echo_spdif_mode_info,
1522 .get = snd_echo_spdif_mode_get,
1523 .put = snd_echo_spdif_mode_put,
1524 };
1525
1526 #endif /* ECHOCARD_HAS_DIGITAL_IO */
1527
1528
1529
1530 #ifdef ECHOCARD_HAS_EXTERNAL_CLOCK
1531
1532 /******************* Select input clock source *******************/
snd_echo_clock_source_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1533 static int snd_echo_clock_source_info(struct snd_kcontrol *kcontrol,
1534 struct snd_ctl_elem_info *uinfo)
1535 {
1536 static const char * const names[8] = {
1537 "Internal", "Word", "Super", "S/PDIF", "ADAT", "ESync",
1538 "ESync96", "MTC"
1539 };
1540 struct echoaudio *chip;
1541
1542 chip = snd_kcontrol_chip(kcontrol);
1543 return snd_ctl_enum_info(uinfo, 1, chip->num_clock_sources, names);
1544 }
1545
snd_echo_clock_source_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1546 static int snd_echo_clock_source_get(struct snd_kcontrol *kcontrol,
1547 struct snd_ctl_elem_value *ucontrol)
1548 {
1549 struct echoaudio *chip;
1550 int i, clock;
1551
1552 chip = snd_kcontrol_chip(kcontrol);
1553 clock = chip->input_clock;
1554
1555 for (i = 0; i < chip->num_clock_sources; i++)
1556 if (clock == chip->clock_source_list[i])
1557 ucontrol->value.enumerated.item[0] = i;
1558
1559 return 0;
1560 }
1561
snd_echo_clock_source_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1562 static int snd_echo_clock_source_put(struct snd_kcontrol *kcontrol,
1563 struct snd_ctl_elem_value *ucontrol)
1564 {
1565 struct echoaudio *chip;
1566 int changed;
1567 unsigned int eclock, dclock;
1568
1569 changed = 0;
1570 chip = snd_kcontrol_chip(kcontrol);
1571 eclock = ucontrol->value.enumerated.item[0];
1572 if (eclock >= chip->input_clock_types)
1573 return -EINVAL;
1574 dclock = chip->clock_source_list[eclock];
1575 if (chip->input_clock != dclock) {
1576 mutex_lock(&chip->mode_mutex);
1577 spin_lock_irq(&chip->lock);
1578 changed = set_input_clock(chip, dclock);
1579 if (!changed)
1580 changed = 1; /* no errors */
1581 spin_unlock_irq(&chip->lock);
1582 mutex_unlock(&chip->mode_mutex);
1583 }
1584
1585 if (changed < 0)
1586 dev_dbg(chip->card->dev,
1587 "seticlk val%d err 0x%x\n", dclock, changed);
1588
1589 return changed;
1590 }
1591
1592 static const struct snd_kcontrol_new snd_echo_clock_source_switch = {
1593 .name = "Sample Clock Source",
1594 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1595 .info = snd_echo_clock_source_info,
1596 .get = snd_echo_clock_source_get,
1597 .put = snd_echo_clock_source_put,
1598 };
1599
1600 #endif /* ECHOCARD_HAS_EXTERNAL_CLOCK */
1601
1602
1603
1604 #ifdef ECHOCARD_HAS_PHANTOM_POWER
1605
1606 /******************* Phantom power switch *******************/
1607 #define snd_echo_phantom_power_info snd_ctl_boolean_mono_info
1608
snd_echo_phantom_power_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1609 static int snd_echo_phantom_power_get(struct snd_kcontrol *kcontrol,
1610 struct snd_ctl_elem_value *ucontrol)
1611 {
1612 struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1613
1614 ucontrol->value.integer.value[0] = chip->phantom_power;
1615 return 0;
1616 }
1617
snd_echo_phantom_power_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1618 static int snd_echo_phantom_power_put(struct snd_kcontrol *kcontrol,
1619 struct snd_ctl_elem_value *ucontrol)
1620 {
1621 struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1622 int power, changed = 0;
1623
1624 power = !!ucontrol->value.integer.value[0];
1625 if (chip->phantom_power != power) {
1626 spin_lock_irq(&chip->lock);
1627 changed = set_phantom_power(chip, power);
1628 spin_unlock_irq(&chip->lock);
1629 if (changed == 0)
1630 changed = 1; /* no errors */
1631 }
1632 return changed;
1633 }
1634
1635 static const struct snd_kcontrol_new snd_echo_phantom_power_switch = {
1636 .name = "Phantom power Switch",
1637 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1638 .info = snd_echo_phantom_power_info,
1639 .get = snd_echo_phantom_power_get,
1640 .put = snd_echo_phantom_power_put,
1641 };
1642
1643 #endif /* ECHOCARD_HAS_PHANTOM_POWER */
1644
1645
1646
1647 #ifdef ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE
1648
1649 /******************* Digital input automute switch *******************/
1650 #define snd_echo_automute_info snd_ctl_boolean_mono_info
1651
snd_echo_automute_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1652 static int snd_echo_automute_get(struct snd_kcontrol *kcontrol,
1653 struct snd_ctl_elem_value *ucontrol)
1654 {
1655 struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1656
1657 ucontrol->value.integer.value[0] = chip->digital_in_automute;
1658 return 0;
1659 }
1660
snd_echo_automute_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1661 static int snd_echo_automute_put(struct snd_kcontrol *kcontrol,
1662 struct snd_ctl_elem_value *ucontrol)
1663 {
1664 struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1665 int automute, changed = 0;
1666
1667 automute = !!ucontrol->value.integer.value[0];
1668 if (chip->digital_in_automute != automute) {
1669 spin_lock_irq(&chip->lock);
1670 changed = set_input_auto_mute(chip, automute);
1671 spin_unlock_irq(&chip->lock);
1672 if (changed == 0)
1673 changed = 1; /* no errors */
1674 }
1675 return changed;
1676 }
1677
1678 static const struct snd_kcontrol_new snd_echo_automute_switch = {
1679 .name = "Digital Capture Switch (automute)",
1680 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1681 .info = snd_echo_automute_info,
1682 .get = snd_echo_automute_get,
1683 .put = snd_echo_automute_put,
1684 };
1685
1686 #endif /* ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE */
1687
1688
1689
1690 /******************* VU-meters switch *******************/
1691 #define snd_echo_vumeters_switch_info snd_ctl_boolean_mono_info
1692
snd_echo_vumeters_switch_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1693 static int snd_echo_vumeters_switch_put(struct snd_kcontrol *kcontrol,
1694 struct snd_ctl_elem_value *ucontrol)
1695 {
1696 struct echoaudio *chip;
1697
1698 chip = snd_kcontrol_chip(kcontrol);
1699 spin_lock_irq(&chip->lock);
1700 set_meters_on(chip, ucontrol->value.integer.value[0]);
1701 spin_unlock_irq(&chip->lock);
1702 return 1;
1703 }
1704
1705 static const struct snd_kcontrol_new snd_echo_vumeters_switch = {
1706 .name = "VU-meters Switch",
1707 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1708 .access = SNDRV_CTL_ELEM_ACCESS_WRITE,
1709 .info = snd_echo_vumeters_switch_info,
1710 .put = snd_echo_vumeters_switch_put,
1711 };
1712
1713
1714
1715 /***** Read VU-meters (input, output, analog and digital together) *****/
snd_echo_vumeters_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1716 static int snd_echo_vumeters_info(struct snd_kcontrol *kcontrol,
1717 struct snd_ctl_elem_info *uinfo)
1718 {
1719 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1720 uinfo->count = 96;
1721 uinfo->value.integer.min = ECHOGAIN_MINOUT;
1722 uinfo->value.integer.max = 0;
1723 return 0;
1724 }
1725
snd_echo_vumeters_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1726 static int snd_echo_vumeters_get(struct snd_kcontrol *kcontrol,
1727 struct snd_ctl_elem_value *ucontrol)
1728 {
1729 struct echoaudio *chip;
1730
1731 chip = snd_kcontrol_chip(kcontrol);
1732 get_audio_meters(chip, ucontrol->value.integer.value);
1733 return 0;
1734 }
1735
1736 static const struct snd_kcontrol_new snd_echo_vumeters = {
1737 .name = "VU-meters",
1738 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1739 .access = SNDRV_CTL_ELEM_ACCESS_READ |
1740 SNDRV_CTL_ELEM_ACCESS_VOLATILE |
1741 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1742 .info = snd_echo_vumeters_info,
1743 .get = snd_echo_vumeters_get,
1744 .tlv = {.p = db_scale_output_gain},
1745 };
1746
1747
1748
1749 /*** Channels info - it exports informations about the number of channels ***/
snd_echo_channels_info_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1750 static int snd_echo_channels_info_info(struct snd_kcontrol *kcontrol,
1751 struct snd_ctl_elem_info *uinfo)
1752 {
1753 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1754 uinfo->count = 6;
1755 uinfo->value.integer.min = 0;
1756 uinfo->value.integer.max = 1 << ECHO_CLOCK_NUMBER;
1757 return 0;
1758 }
1759
snd_echo_channels_info_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1760 static int snd_echo_channels_info_get(struct snd_kcontrol *kcontrol,
1761 struct snd_ctl_elem_value *ucontrol)
1762 {
1763 struct echoaudio *chip;
1764 int detected, clocks, bit, src;
1765
1766 chip = snd_kcontrol_chip(kcontrol);
1767 ucontrol->value.integer.value[0] = num_busses_in(chip);
1768 ucontrol->value.integer.value[1] = num_analog_busses_in(chip);
1769 ucontrol->value.integer.value[2] = num_busses_out(chip);
1770 ucontrol->value.integer.value[3] = num_analog_busses_out(chip);
1771 ucontrol->value.integer.value[4] = num_pipes_out(chip);
1772
1773 /* Compute the bitmask of the currently valid input clocks */
1774 detected = detect_input_clocks(chip);
1775 clocks = 0;
1776 src = chip->num_clock_sources - 1;
1777 for (bit = ECHO_CLOCK_NUMBER - 1; bit >= 0; bit--)
1778 if (detected & (1 << bit))
1779 for (; src >= 0; src--)
1780 if (bit == chip->clock_source_list[src]) {
1781 clocks |= 1 << src;
1782 break;
1783 }
1784 ucontrol->value.integer.value[5] = clocks;
1785
1786 return 0;
1787 }
1788
1789 static const struct snd_kcontrol_new snd_echo_channels_info = {
1790 .name = "Channels info",
1791 .iface = SNDRV_CTL_ELEM_IFACE_HWDEP,
1792 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
1793 .info = snd_echo_channels_info_info,
1794 .get = snd_echo_channels_info_get,
1795 };
1796
1797
1798
1799
1800 /******************************************************************************
1801 IRQ Handling
1802 ******************************************************************************/
1803 /* Check if a period has elapsed since last interrupt
1804 *
1805 * Don't make any updates to state; PCM core handles this with the
1806 * correct locks.
1807 *
1808 * \return true if a period has elapsed, otherwise false
1809 */
period_has_elapsed(struct snd_pcm_substream * substream)1810 static bool period_has_elapsed(struct snd_pcm_substream *substream)
1811 {
1812 struct snd_pcm_runtime *runtime = substream->runtime;
1813 struct audiopipe *pipe = runtime->private_data;
1814 u32 counter, step;
1815 size_t period_bytes;
1816
1817 if (pipe->state != PIPE_STATE_STARTED)
1818 return false;
1819
1820 period_bytes = frames_to_bytes(runtime, runtime->period_size);
1821
1822 counter = le32_to_cpu(*pipe->dma_counter); /* presumed atomic */
1823
1824 step = counter - pipe->last_period; /* handles wrapping */
1825 step -= step % period_bytes; /* acknowledge whole periods only */
1826
1827 if (step == 0)
1828 return false; /* haven't advanced a whole period yet */
1829
1830 pipe->last_period += step; /* used exclusively by us */
1831 return true;
1832 }
1833
snd_echo_interrupt(int irq,void * dev_id)1834 static irqreturn_t snd_echo_interrupt(int irq, void *dev_id)
1835 {
1836 struct echoaudio *chip = dev_id;
1837 int ss, st;
1838
1839 spin_lock(&chip->lock);
1840 st = service_irq(chip);
1841 if (st < 0) {
1842 spin_unlock(&chip->lock);
1843 return IRQ_NONE;
1844 }
1845 /* The hardware doesn't tell us which substream caused the irq,
1846 thus we have to check all running substreams. */
1847 for (ss = 0; ss < DSP_MAXPIPES; ss++) {
1848 struct snd_pcm_substream *substream;
1849
1850 substream = chip->substream[ss];
1851 if (substream && period_has_elapsed(substream)) {
1852 spin_unlock(&chip->lock);
1853 snd_pcm_period_elapsed(substream);
1854 spin_lock(&chip->lock);
1855 }
1856 }
1857 spin_unlock(&chip->lock);
1858
1859 #ifdef ECHOCARD_HAS_MIDI
1860 if (st > 0 && chip->midi_in) {
1861 snd_rawmidi_receive(chip->midi_in, chip->midi_buffer, st);
1862 dev_dbg(chip->card->dev, "rawmidi_iread=%d\n", st);
1863 }
1864 #endif
1865 return IRQ_HANDLED;
1866 }
1867
1868
1869
1870
1871 /******************************************************************************
1872 Module construction / destruction
1873 ******************************************************************************/
1874
snd_echo_free(struct snd_card * card)1875 static void snd_echo_free(struct snd_card *card)
1876 {
1877 struct echoaudio *chip = card->private_data;
1878
1879 if (chip->comm_page)
1880 rest_in_peace(chip);
1881
1882 if (chip->irq >= 0)
1883 free_irq(chip->irq, chip);
1884
1885 /* release chip data */
1886 free_firmware_cache(chip);
1887 }
1888
1889 /* <--snd_echo_probe() */
snd_echo_create(struct snd_card * card,struct pci_dev * pci)1890 static int snd_echo_create(struct snd_card *card,
1891 struct pci_dev *pci)
1892 {
1893 struct echoaudio *chip = card->private_data;
1894 int err;
1895 size_t sz;
1896
1897 pci_write_config_byte(pci, PCI_LATENCY_TIMER, 0xC0);
1898
1899 err = pcim_enable_device(pci);
1900 if (err < 0)
1901 return err;
1902 pci_set_master(pci);
1903
1904 /* Allocate chip if needed */
1905 spin_lock_init(&chip->lock);
1906 chip->card = card;
1907 chip->pci = pci;
1908 chip->irq = -1;
1909 chip->opencount = 0;
1910 mutex_init(&chip->mode_mutex);
1911 chip->can_set_rate = 1;
1912
1913 /* PCI resource allocation */
1914 err = pcim_request_all_regions(pci, ECHOCARD_NAME);
1915 if (err < 0)
1916 return err;
1917
1918 chip->dsp_registers_phys = pci_resource_start(pci, 0);
1919 sz = pci_resource_len(pci, 0);
1920 if (sz > PAGE_SIZE)
1921 sz = PAGE_SIZE; /* We map only the required part */
1922
1923 chip->dsp_registers = devm_ioremap(&pci->dev, chip->dsp_registers_phys, sz);
1924 if (!chip->dsp_registers) {
1925 dev_err(chip->card->dev, "ioremap failed\n");
1926 return -ENOMEM;
1927 }
1928
1929 if (request_irq(pci->irq, snd_echo_interrupt, IRQF_SHARED,
1930 KBUILD_MODNAME, chip)) {
1931 dev_err(chip->card->dev, "cannot grab irq\n");
1932 return -EBUSY;
1933 }
1934 chip->irq = pci->irq;
1935 card->sync_irq = chip->irq;
1936 dev_dbg(card->dev, "pci=%p irq=%d subdev=%04x Init hardware...\n",
1937 chip->pci, chip->irq, chip->pci->subsystem_device);
1938
1939 card->private_free = snd_echo_free;
1940
1941 /* Create the DSP comm page - this is the area of memory used for most
1942 of the communication with the DSP, which accesses it via bus mastering */
1943 chip->commpage_dma_buf =
1944 snd_devm_alloc_pages(&pci->dev, SNDRV_DMA_TYPE_DEV,
1945 sizeof(struct comm_page));
1946 if (!chip->commpage_dma_buf)
1947 return -ENOMEM;
1948 chip->comm_page_phys = chip->commpage_dma_buf->addr;
1949 chip->comm_page = (struct comm_page *)chip->commpage_dma_buf->area;
1950
1951 err = init_hw(chip, chip->pci->device, chip->pci->subsystem_device);
1952 if (err >= 0)
1953 err = set_mixer_defaults(chip);
1954 if (err < 0) {
1955 dev_err(card->dev, "init_hw err=%d\n", err);
1956 return err;
1957 }
1958
1959 return 0;
1960 }
1961
1962 /* constructor */
__snd_echo_probe(struct pci_dev * pci,const struct pci_device_id * pci_id)1963 static int __snd_echo_probe(struct pci_dev *pci,
1964 const struct pci_device_id *pci_id)
1965 {
1966 static int dev;
1967 struct snd_card *card;
1968 struct echoaudio *chip;
1969 char *dsp;
1970 int err;
1971
1972 if (dev >= SNDRV_CARDS)
1973 return -ENODEV;
1974 if (!enable[dev]) {
1975 dev++;
1976 return -ENOENT;
1977 }
1978
1979 err = snd_devm_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
1980 sizeof(*chip), &card);
1981 if (err < 0)
1982 return err;
1983 chip = card->private_data;
1984
1985 err = snd_echo_create(card, pci);
1986 if (err < 0)
1987 return err;
1988
1989 strscpy(card->driver, "Echo_" ECHOCARD_NAME);
1990 strscpy(card->shortname, chip->card_name);
1991
1992 dsp = "56301";
1993 if (pci_id->device == 0x3410)
1994 dsp = "56361";
1995
1996 sprintf(card->longname, "%s rev.%d (DSP%s) at 0x%lx irq %i",
1997 card->shortname, pci_id->subdevice & 0x000f, dsp,
1998 chip->dsp_registers_phys, chip->irq);
1999
2000 err = snd_echo_new_pcm(chip);
2001 if (err < 0) {
2002 dev_err(chip->card->dev, "new pcm error %d\n", err);
2003 return err;
2004 }
2005
2006 #ifdef ECHOCARD_HAS_MIDI
2007 if (chip->has_midi) { /* Some Mia's do not have midi */
2008 err = snd_echo_midi_create(card, chip);
2009 if (err < 0) {
2010 dev_err(chip->card->dev, "new midi error %d\n", err);
2011 return err;
2012 }
2013 }
2014 #endif
2015
2016 #ifdef ECHOCARD_HAS_VMIXER
2017 snd_echo_vmixer.count = num_pipes_out(chip) * num_busses_out(chip);
2018 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vmixer, chip));
2019 if (err < 0)
2020 return err;
2021 #ifdef ECHOCARD_HAS_LINE_OUT_GAIN
2022 err = snd_ctl_add(chip->card,
2023 snd_ctl_new1(&snd_echo_line_output_gain, chip));
2024 if (err < 0)
2025 return err;
2026 #endif
2027 #else /* ECHOCARD_HAS_VMIXER */
2028 err = snd_ctl_add(chip->card,
2029 snd_ctl_new1(&snd_echo_pcm_output_gain, chip));
2030 if (err < 0)
2031 return err;
2032 #endif /* ECHOCARD_HAS_VMIXER */
2033
2034 #ifdef ECHOCARD_HAS_INPUT_GAIN
2035 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_line_input_gain, chip));
2036 if (err < 0)
2037 return err;
2038 #endif
2039
2040 #ifdef ECHOCARD_HAS_INPUT_NOMINAL_LEVEL
2041 if (!chip->hasnt_input_nominal_level) {
2042 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_intput_nominal_level, chip));
2043 if (err < 0)
2044 return err;
2045 }
2046 #endif
2047
2048 #ifdef ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL
2049 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_output_nominal_level, chip));
2050 if (err < 0)
2051 return err;
2052 #endif
2053
2054 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vumeters_switch, chip));
2055 if (err < 0)
2056 return err;
2057
2058 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vumeters, chip));
2059 if (err < 0)
2060 return err;
2061
2062 #ifdef ECHOCARD_HAS_MONITOR
2063 snd_echo_monitor_mixer.count = num_busses_in(chip) * num_busses_out(chip);
2064 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_monitor_mixer, chip));
2065 if (err < 0)
2066 return err;
2067 #endif
2068
2069 #ifdef ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE
2070 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_automute_switch, chip));
2071 if (err < 0)
2072 return err;
2073 #endif
2074
2075 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_channels_info, chip));
2076 if (err < 0)
2077 return err;
2078
2079 #ifdef ECHOCARD_HAS_DIGITAL_MODE_SWITCH
2080 /* Creates a list of available digital modes */
2081 chip->num_digital_modes = 0;
2082 for (int i = 0; i < 6; i++)
2083 if (chip->digital_modes & (1 << i))
2084 chip->digital_mode_list[chip->num_digital_modes++] = i;
2085
2086 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_digital_mode_switch, chip));
2087 if (err < 0)
2088 return err;
2089 #endif /* ECHOCARD_HAS_DIGITAL_MODE_SWITCH */
2090
2091 #ifdef ECHOCARD_HAS_EXTERNAL_CLOCK
2092 /* Creates a list of available clock sources */
2093 chip->num_clock_sources = 0;
2094 for (int i = 0; i < 10; i++)
2095 if (chip->input_clock_types & (1 << i))
2096 chip->clock_source_list[chip->num_clock_sources++] = i;
2097
2098 if (chip->num_clock_sources > 1) {
2099 chip->clock_src_ctl = snd_ctl_new1(&snd_echo_clock_source_switch, chip);
2100 err = snd_ctl_add(chip->card, chip->clock_src_ctl);
2101 if (err < 0)
2102 return err;
2103 }
2104 #endif /* ECHOCARD_HAS_EXTERNAL_CLOCK */
2105
2106 #ifdef ECHOCARD_HAS_DIGITAL_IO
2107 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_spdif_mode_switch, chip));
2108 if (err < 0)
2109 return err;
2110 #endif
2111
2112 #ifdef ECHOCARD_HAS_PHANTOM_POWER
2113 if (chip->has_phantom_power) {
2114 err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_phantom_power_switch, chip));
2115 if (err < 0)
2116 return err;
2117 }
2118 #endif
2119
2120 err = snd_card_register(card);
2121 if (err < 0)
2122 return err;
2123 dev_info(card->dev, "Card registered: %s\n", card->longname);
2124
2125 pci_set_drvdata(pci, chip);
2126 dev++;
2127 return 0;
2128 }
2129
snd_echo_probe(struct pci_dev * pci,const struct pci_device_id * pci_id)2130 static int snd_echo_probe(struct pci_dev *pci,
2131 const struct pci_device_id *pci_id)
2132 {
2133 return snd_card_free_on_error(&pci->dev, __snd_echo_probe(pci, pci_id));
2134 }
2135
2136
snd_echo_suspend(struct device * dev)2137 static int snd_echo_suspend(struct device *dev)
2138 {
2139 struct echoaudio *chip = dev_get_drvdata(dev);
2140
2141 #ifdef ECHOCARD_HAS_MIDI
2142 /* This call can sleep */
2143 if (chip->midi_out)
2144 snd_echo_midi_output_trigger(chip->midi_out, 0);
2145 #endif
2146 spin_lock_irq(&chip->lock);
2147 if (wait_handshake(chip)) {
2148 spin_unlock_irq(&chip->lock);
2149 return -EIO;
2150 }
2151 clear_handshake(chip);
2152 if (send_vector(chip, DSP_VC_GO_COMATOSE) < 0) {
2153 spin_unlock_irq(&chip->lock);
2154 return -EIO;
2155 }
2156 spin_unlock_irq(&chip->lock);
2157
2158 chip->dsp_code = NULL;
2159 free_irq(chip->irq, chip);
2160 chip->irq = -1;
2161 chip->card->sync_irq = -1;
2162 return 0;
2163 }
2164
2165
2166
snd_echo_resume(struct device * dev)2167 static int snd_echo_resume(struct device *dev)
2168 {
2169 struct pci_dev *pci = to_pci_dev(dev);
2170 struct echoaudio *chip = dev_get_drvdata(dev);
2171 struct comm_page *commpage, *commpage_bak;
2172 u32 pipe_alloc_mask;
2173 int err;
2174
2175 commpage = chip->comm_page;
2176 commpage_bak = kmemdup(commpage, sizeof(*commpage), GFP_KERNEL);
2177 if (commpage_bak == NULL)
2178 return -ENOMEM;
2179
2180 err = init_hw(chip, chip->pci->device, chip->pci->subsystem_device);
2181 if (err < 0) {
2182 kfree(commpage_bak);
2183 dev_err(dev, "resume init_hw err=%d\n", err);
2184 return err;
2185 }
2186
2187 /* Temporarily set chip->pipe_alloc_mask=0 otherwise
2188 * restore_dsp_settings() fails.
2189 */
2190 pipe_alloc_mask = chip->pipe_alloc_mask;
2191 chip->pipe_alloc_mask = 0;
2192 err = restore_dsp_rettings(chip);
2193 chip->pipe_alloc_mask = pipe_alloc_mask;
2194 if (err < 0) {
2195 kfree(commpage_bak);
2196 return err;
2197 }
2198
2199 memcpy(&commpage->audio_format, &commpage_bak->audio_format,
2200 sizeof(commpage->audio_format));
2201 memcpy(&commpage->sglist_addr, &commpage_bak->sglist_addr,
2202 sizeof(commpage->sglist_addr));
2203 memcpy(&commpage->midi_output, &commpage_bak->midi_output,
2204 sizeof(commpage->midi_output));
2205 kfree(commpage_bak);
2206
2207 if (request_irq(pci->irq, snd_echo_interrupt, IRQF_SHARED,
2208 KBUILD_MODNAME, chip)) {
2209 dev_err(chip->card->dev, "cannot grab irq\n");
2210 return -EBUSY;
2211 }
2212 chip->irq = pci->irq;
2213 chip->card->sync_irq = chip->irq;
2214 dev_dbg(dev, "resume irq=%d\n", chip->irq);
2215
2216 #ifdef ECHOCARD_HAS_MIDI
2217 if (chip->midi_input_enabled)
2218 enable_midi_input(chip, true);
2219 if (chip->midi_out)
2220 snd_echo_midi_output_trigger(chip->midi_out, 1);
2221 #endif
2222
2223 return 0;
2224 }
2225
2226 static DEFINE_SIMPLE_DEV_PM_OPS(snd_echo_pm, snd_echo_suspend, snd_echo_resume);
2227
2228 /******************************************************************************
2229 Everything starts and ends here
2230 ******************************************************************************/
2231
2232 /* pci_driver definition */
2233 static struct pci_driver echo_driver = {
2234 .name = KBUILD_MODNAME,
2235 .id_table = snd_echo_ids,
2236 .probe = snd_echo_probe,
2237 .driver = {
2238 .pm = &snd_echo_pm,
2239 },
2240 };
2241
2242 module_pci_driver(echo_driver);
2243