blob: 907cf1a467128bf44256da495d1d9e76d2f11498 [file] [log] [blame]
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001/*
2 * ALSA driver for Echoaudio soundcards.
3 * Copyright (C) 2003-2004 Giuliano Pochini <pochini@shiny.it>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
Paul Gortmakerda155d52011-07-15 12:38:28 -040019#include <linux/module.h>
20
Giuliano Pochinidd7b2542006-06-28 13:53:41 +020021MODULE_AUTHOR("Giuliano Pochini <pochini@shiny.it>");
22MODULE_LICENSE("GPL v2");
23MODULE_DESCRIPTION("Echoaudio " ECHOCARD_NAME " soundcards driver");
24MODULE_SUPPORTED_DEVICE("{{Echoaudio," ECHOCARD_NAME "}}");
25MODULE_DEVICE_TABLE(pci, snd_echo_ids);
26
27static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
28static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
Rusty Russella67ff6a2011-12-15 13:49:36 +103029static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
Giuliano Pochinidd7b2542006-06-28 13:53:41 +020030
31module_param_array(index, int, NULL, 0444);
32MODULE_PARM_DESC(index, "Index value for " ECHOCARD_NAME " soundcard.");
33module_param_array(id, charp, NULL, 0444);
34MODULE_PARM_DESC(id, "ID string for " ECHOCARD_NAME " soundcard.");
35module_param_array(enable, bool, NULL, 0444);
36MODULE_PARM_DESC(enable, "Enable " ECHOCARD_NAME " soundcard.");
37
38static unsigned int channels_list[10] = {1, 2, 4, 6, 8, 10, 12, 14, 16, 999999};
Takashi Iwai0cb29ea2007-01-29 15:33:49 +010039static const DECLARE_TLV_DB_SCALE(db_scale_output_gain, -12800, 100, 1);
Giuliano Pochinidd7b2542006-06-28 13:53:41 +020040
Giuliano Pochini19b50062010-02-14 18:15:34 +010041
42
Giuliano Pochinidd7b2542006-06-28 13:53:41 +020043static int get_firmware(const struct firmware **fw_entry,
Giuliano Pochini19b50062010-02-14 18:15:34 +010044 struct echoaudio *chip, const short fw_index)
Giuliano Pochinidd7b2542006-06-28 13:53:41 +020045{
46 int err;
47 char name[30];
Giuliano Pochini19b50062010-02-14 18:15:34 +010048
Takashi Iwaic7561cd2012-08-14 18:12:04 +020049#ifdef CONFIG_PM_SLEEP
Giuliano Pochini4f8ada42010-02-14 18:15:51 +010050 if (chip->fw_cache[fw_index]) {
Sudip Mukherjeeb5b4a412014-11-03 16:04:13 +053051 dev_dbg(chip->card->dev,
52 "firmware requested: %s is cached\n",
53 card_fw[fw_index].data);
Giuliano Pochini4f8ada42010-02-14 18:15:51 +010054 *fw_entry = chip->fw_cache[fw_index];
55 return 0;
56 }
57#endif
58
Sudip Mukherjeeb5b4a412014-11-03 16:04:13 +053059 dev_dbg(chip->card->dev,
60 "firmware requested: %s\n", card_fw[fw_index].data);
Giuliano Pochini4f8ada42010-02-14 18:15:51 +010061 snprintf(name, sizeof(name), "ea/%s", card_fw[fw_index].data);
Takashi Iwaiafe5da32018-05-24 11:20:06 +020062 err = request_firmware(fw_entry, name, &chip->pci->dev);
Giuliano Pochini4f8ada42010-02-14 18:15:51 +010063 if (err < 0)
Takashi Iwaiece7a362014-02-25 16:43:02 +010064 dev_err(chip->card->dev,
65 "get_firmware(): Firmware not available (%d)\n", err);
Takashi Iwaic7561cd2012-08-14 18:12:04 +020066#ifdef CONFIG_PM_SLEEP
Giuliano Pochini4f8ada42010-02-14 18:15:51 +010067 else
68 chip->fw_cache[fw_index] = *fw_entry;
69#endif
Giuliano Pochinidd7b2542006-06-28 13:53:41 +020070 return err;
71}
72
Giuliano Pochini19b50062010-02-14 18:15:34 +010073
74
Sudip Mukherjeee3690862014-11-03 16:04:12 +053075static void free_firmware(const struct firmware *fw_entry,
76 struct echoaudio *chip)
Giuliano Pochinidd7b2542006-06-28 13:53:41 +020077{
Takashi Iwaic7561cd2012-08-14 18:12:04 +020078#ifdef CONFIG_PM_SLEEP
Sudip Mukherjeeb5b4a412014-11-03 16:04:13 +053079 dev_dbg(chip->card->dev, "firmware not released (kept in cache)\n");
Giuliano Pochini4f8ada42010-02-14 18:15:51 +010080#else
Giuliano Pochinidd7b2542006-06-28 13:53:41 +020081 release_firmware(fw_entry);
Giuliano Pochini4f8ada42010-02-14 18:15:51 +010082#endif
83}
84
85
86
87static void free_firmware_cache(struct echoaudio *chip)
88{
Takashi Iwaic7561cd2012-08-14 18:12:04 +020089#ifdef CONFIG_PM_SLEEP
Giuliano Pochini4f8ada42010-02-14 18:15:51 +010090 int i;
91
92 for (i = 0; i < 8 ; i++)
93 if (chip->fw_cache[i]) {
94 release_firmware(chip->fw_cache[i]);
Sudip Mukherjeeb5b4a412014-11-03 16:04:13 +053095 dev_dbg(chip->card->dev, "release_firmware(%d)\n", i);
Giuliano Pochini4f8ada42010-02-14 18:15:51 +010096 }
97
Giuliano Pochini4f8ada42010-02-14 18:15:51 +010098#endif
Giuliano Pochinidd7b2542006-06-28 13:53:41 +020099}
100
101
102
103/******************************************************************************
104 PCM interface
105******************************************************************************/
106
107static void audiopipe_free(struct snd_pcm_runtime *runtime)
108{
109 struct audiopipe *pipe = runtime->private_data;
110
111 if (pipe->sgpage.area)
112 snd_dma_free_pages(&pipe->sgpage);
113 kfree(pipe);
114}
115
116
117
118static int hw_rule_capture_format_by_channels(struct snd_pcm_hw_params *params,
119 struct snd_pcm_hw_rule *rule)
120{
121 struct snd_interval *c = hw_param_interval(params,
122 SNDRV_PCM_HW_PARAM_CHANNELS);
123 struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
124 struct snd_mask fmt;
125
126 snd_mask_any(&fmt);
127
128#ifndef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
129 /* >=2 channels cannot be S32_BE */
130 if (c->min == 2) {
131 fmt.bits[0] &= ~SNDRV_PCM_FMTBIT_S32_BE;
132 return snd_mask_refine(f, &fmt);
133 }
134#endif
135 /* > 2 channels cannot be U8 and S32_BE */
136 if (c->min > 2) {
137 fmt.bits[0] &= ~(SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S32_BE);
138 return snd_mask_refine(f, &fmt);
139 }
140 /* Mono is ok with any format */
141 return 0;
142}
143
144
145
146static int hw_rule_capture_channels_by_format(struct snd_pcm_hw_params *params,
147 struct snd_pcm_hw_rule *rule)
148{
149 struct snd_interval *c = hw_param_interval(params,
150 SNDRV_PCM_HW_PARAM_CHANNELS);
151 struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
152 struct snd_interval ch;
153
154 snd_interval_any(&ch);
155
156 /* S32_BE is mono (and stereo) only */
157 if (f->bits[0] == SNDRV_PCM_FMTBIT_S32_BE) {
158 ch.min = 1;
159#ifdef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
160 ch.max = 2;
161#else
162 ch.max = 1;
163#endif
164 ch.integer = 1;
165 return snd_interval_refine(c, &ch);
166 }
167 /* U8 can be only mono or stereo */
168 if (f->bits[0] == SNDRV_PCM_FMTBIT_U8) {
169 ch.min = 1;
170 ch.max = 2;
171 ch.integer = 1;
172 return snd_interval_refine(c, &ch);
173 }
174 /* S16_LE, S24_3LE and S32_LE support any number of channels. */
175 return 0;
176}
177
178
179
180static int hw_rule_playback_format_by_channels(struct snd_pcm_hw_params *params,
181 struct snd_pcm_hw_rule *rule)
182{
183 struct snd_interval *c = hw_param_interval(params,
184 SNDRV_PCM_HW_PARAM_CHANNELS);
185 struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
186 struct snd_mask fmt;
187 u64 fmask;
188 snd_mask_any(&fmt);
189
190 fmask = fmt.bits[0] + ((u64)fmt.bits[1] << 32);
191
192 /* >2 channels must be S16_LE, S24_3LE or S32_LE */
193 if (c->min > 2) {
194 fmask &= SNDRV_PCM_FMTBIT_S16_LE |
195 SNDRV_PCM_FMTBIT_S24_3LE |
196 SNDRV_PCM_FMTBIT_S32_LE;
197 /* 1 channel must be S32_BE or S32_LE */
198 } else if (c->max == 1)
199 fmask &= SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE;
200#ifndef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
201 /* 2 channels cannot be S32_BE */
202 else if (c->min == 2 && c->max == 2)
203 fmask &= ~SNDRV_PCM_FMTBIT_S32_BE;
204#endif
205 else
206 return 0;
207
208 fmt.bits[0] &= (u32)fmask;
209 fmt.bits[1] &= (u32)(fmask >> 32);
210 return snd_mask_refine(f, &fmt);
211}
212
213
214
215static int hw_rule_playback_channels_by_format(struct snd_pcm_hw_params *params,
216 struct snd_pcm_hw_rule *rule)
217{
218 struct snd_interval *c = hw_param_interval(params,
219 SNDRV_PCM_HW_PARAM_CHANNELS);
220 struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
221 struct snd_interval ch;
222 u64 fmask;
223
224 snd_interval_any(&ch);
225 ch.integer = 1;
226 fmask = f->bits[0] + ((u64)f->bits[1] << 32);
227
228 /* S32_BE is mono (and stereo) only */
229 if (fmask == SNDRV_PCM_FMTBIT_S32_BE) {
230 ch.min = 1;
231#ifdef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
232 ch.max = 2;
233#else
234 ch.max = 1;
235#endif
236 /* U8 is stereo only */
237 } else if (fmask == SNDRV_PCM_FMTBIT_U8)
238 ch.min = ch.max = 2;
239 /* S16_LE and S24_3LE must be at least stereo */
240 else if (!(fmask & ~(SNDRV_PCM_FMTBIT_S16_LE |
241 SNDRV_PCM_FMTBIT_S24_3LE)))
242 ch.min = 2;
243 else
244 return 0;
245
246 return snd_interval_refine(c, &ch);
247}
248
249
250
251/* Since the sample rate is a global setting, do allow the user to change the
252sample rate only if there is only one pcm device open. */
253static int hw_rule_sample_rate(struct snd_pcm_hw_params *params,
254 struct snd_pcm_hw_rule *rule)
255{
256 struct snd_interval *rate = hw_param_interval(params,
257 SNDRV_PCM_HW_PARAM_RATE);
258 struct echoaudio *chip = rule->private;
259 struct snd_interval fixed;
260
261 if (!chip->can_set_rate) {
262 snd_interval_any(&fixed);
263 fixed.min = fixed.max = chip->sample_rate;
264 return snd_interval_refine(rate, &fixed);
265 }
266 return 0;
267}
268
269
270static int pcm_open(struct snd_pcm_substream *substream,
271 signed char max_channels)
272{
273 struct echoaudio *chip;
274 struct snd_pcm_runtime *runtime;
275 struct audiopipe *pipe;
276 int err, i;
277
278 if (max_channels <= 0)
279 return -EAGAIN;
280
281 chip = snd_pcm_substream_chip(substream);
282 runtime = substream->runtime;
283
Panagiotis Issaris59feddb2006-07-25 15:28:03 +0200284 pipe = kzalloc(sizeof(struct audiopipe), GFP_KERNEL);
285 if (!pipe)
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200286 return -ENOMEM;
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200287 pipe->index = -1; /* Not configured yet */
288
289 /* Set up hw capabilities and contraints */
290 memcpy(&pipe->hw, &pcm_hardware_skel, sizeof(struct snd_pcm_hardware));
Sudip Mukherjeeb5b4a412014-11-03 16:04:13 +0530291 dev_dbg(chip->card->dev, "max_channels=%d\n", max_channels);
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200292 pipe->constr.list = channels_list;
293 pipe->constr.mask = 0;
294 for (i = 0; channels_list[i] <= max_channels; i++);
295 pipe->constr.count = i;
296 if (pipe->hw.channels_max > max_channels)
297 pipe->hw.channels_max = max_channels;
298 if (chip->digital_mode == DIGITAL_MODE_ADAT) {
299 pipe->hw.rate_max = 48000;
300 pipe->hw.rates &= SNDRV_PCM_RATE_8000_48000;
301 }
302
303 runtime->hw = pipe->hw;
304 runtime->private_data = pipe;
305 runtime->private_free = audiopipe_free;
306 snd_pcm_set_sync(substream);
307
308 /* Only mono and any even number of channels are allowed */
309 if ((err = snd_pcm_hw_constraint_list(runtime, 0,
310 SNDRV_PCM_HW_PARAM_CHANNELS,
311 &pipe->constr)) < 0)
312 return err;
313
314 /* All periods should have the same size */
315 if ((err = snd_pcm_hw_constraint_integer(runtime,
316 SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
317 return err;
318
319 /* The hw accesses memory in chunks 32 frames long and they should be
320 32-bytes-aligned. It's not a requirement, but it seems that IRQs are
321 generated with a resolution of 32 frames. Thus we need the following */
322 if ((err = snd_pcm_hw_constraint_step(runtime, 0,
323 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
324 32)) < 0)
325 return err;
326 if ((err = snd_pcm_hw_constraint_step(runtime, 0,
327 SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
328 32)) < 0)
329 return err;
330
331 if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
332 SNDRV_PCM_HW_PARAM_RATE,
333 hw_rule_sample_rate, chip,
334 SNDRV_PCM_HW_PARAM_RATE, -1)) < 0)
335 return err;
336
337 /* Finally allocate a page for the scatter-gather list */
338 if ((err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
339 snd_dma_pci_data(chip->pci),
340 PAGE_SIZE, &pipe->sgpage)) < 0) {
Sudip Mukherjeeb5b4a412014-11-03 16:04:13 +0530341 dev_err(chip->card->dev, "s-g list allocation failed\n");
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200342 return err;
343 }
344
345 return 0;
346}
347
348
349
350static int pcm_analog_in_open(struct snd_pcm_substream *substream)
351{
352 struct echoaudio *chip = snd_pcm_substream_chip(substream);
353 int err;
354
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200355 if ((err = pcm_open(substream, num_analog_busses_in(chip) -
356 substream->number)) < 0)
357 return err;
358 if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
359 SNDRV_PCM_HW_PARAM_CHANNELS,
360 hw_rule_capture_channels_by_format, NULL,
361 SNDRV_PCM_HW_PARAM_FORMAT, -1)) < 0)
362 return err;
363 if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
364 SNDRV_PCM_HW_PARAM_FORMAT,
365 hw_rule_capture_format_by_channels, NULL,
366 SNDRV_PCM_HW_PARAM_CHANNELS, -1)) < 0)
367 return err;
368 atomic_inc(&chip->opencount);
369 if (atomic_read(&chip->opencount) > 1 && chip->rate_set)
370 chip->can_set_rate=0;
Sudip Mukherjeeb5b4a412014-11-03 16:04:13 +0530371 dev_dbg(chip->card->dev, "pcm_analog_in_open cs=%d oc=%d r=%d\n",
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200372 chip->can_set_rate, atomic_read(&chip->opencount),
Sudip Mukherjeeb5b4a412014-11-03 16:04:13 +0530373 chip->sample_rate);
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200374 return 0;
375}
376
377
378
379static int pcm_analog_out_open(struct snd_pcm_substream *substream)
380{
381 struct echoaudio *chip = snd_pcm_substream_chip(substream);
382 int max_channels, err;
383
384#ifdef ECHOCARD_HAS_VMIXER
385 max_channels = num_pipes_out(chip);
386#else
387 max_channels = num_analog_busses_out(chip);
388#endif
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200389 if ((err = pcm_open(substream, max_channels - substream->number)) < 0)
390 return err;
391 if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
392 SNDRV_PCM_HW_PARAM_CHANNELS,
393 hw_rule_playback_channels_by_format,
394 NULL,
395 SNDRV_PCM_HW_PARAM_FORMAT, -1)) < 0)
396 return err;
397 if ((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)) < 0)
402 return err;
403 atomic_inc(&chip->opencount);
404 if (atomic_read(&chip->opencount) > 1 && chip->rate_set)
405 chip->can_set_rate=0;
Sudip Mukherjeeb5b4a412014-11-03 16:04:13 +0530406 dev_dbg(chip->card->dev, "pcm_analog_out_open cs=%d oc=%d r=%d\n",
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200407 chip->can_set_rate, atomic_read(&chip->opencount),
Sudip Mukherjeeb5b4a412014-11-03 16:04:13 +0530408 chip->sample_rate);
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200409 return 0;
410}
411
412
413
414#ifdef ECHOCARD_HAS_DIGITAL_IO
415
416static int pcm_digital_in_open(struct snd_pcm_substream *substream)
417{
418 struct echoaudio *chip = snd_pcm_substream_chip(substream);
419 int err, max_channels;
420
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200421 max_channels = num_digital_busses_in(chip) - substream->number;
Takashi Iwaibefceea2007-12-03 17:08:40 +0100422 mutex_lock(&chip->mode_mutex);
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200423 if (chip->digital_mode == DIGITAL_MODE_ADAT)
424 err = pcm_open(substream, max_channels);
425 else /* If the card has ADAT, subtract the 6 channels
426 * that S/PDIF doesn't have
427 */
428 err = pcm_open(substream, max_channels - ECHOCARD_HAS_ADAT);
429
430 if (err < 0)
431 goto din_exit;
432
433 if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
434 SNDRV_PCM_HW_PARAM_CHANNELS,
435 hw_rule_capture_channels_by_format, NULL,
436 SNDRV_PCM_HW_PARAM_FORMAT, -1)) < 0)
437 goto din_exit;
438 if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
439 SNDRV_PCM_HW_PARAM_FORMAT,
440 hw_rule_capture_format_by_channels, NULL,
441 SNDRV_PCM_HW_PARAM_CHANNELS, -1)) < 0)
442 goto din_exit;
443
444 atomic_inc(&chip->opencount);
445 if (atomic_read(&chip->opencount) > 1 && chip->rate_set)
446 chip->can_set_rate=0;
447
448din_exit:
Takashi Iwaibefceea2007-12-03 17:08:40 +0100449 mutex_unlock(&chip->mode_mutex);
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200450 return err;
451}
452
453
454
455#ifndef ECHOCARD_HAS_VMIXER /* See the note in snd_echo_new_pcm() */
456
457static int pcm_digital_out_open(struct snd_pcm_substream *substream)
458{
459 struct echoaudio *chip = snd_pcm_substream_chip(substream);
460 int err, max_channels;
461
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200462 max_channels = num_digital_busses_out(chip) - substream->number;
Takashi Iwaibefceea2007-12-03 17:08:40 +0100463 mutex_lock(&chip->mode_mutex);
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200464 if (chip->digital_mode == DIGITAL_MODE_ADAT)
465 err = pcm_open(substream, max_channels);
466 else /* If the card has ADAT, subtract the 6 channels
467 * that S/PDIF doesn't have
468 */
469 err = pcm_open(substream, max_channels - ECHOCARD_HAS_ADAT);
470
471 if (err < 0)
472 goto dout_exit;
473
474 if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
475 SNDRV_PCM_HW_PARAM_CHANNELS,
476 hw_rule_playback_channels_by_format,
477 NULL, SNDRV_PCM_HW_PARAM_FORMAT,
478 -1)) < 0)
479 goto dout_exit;
480 if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
481 SNDRV_PCM_HW_PARAM_FORMAT,
482 hw_rule_playback_format_by_channels,
483 NULL, SNDRV_PCM_HW_PARAM_CHANNELS,
484 -1)) < 0)
485 goto dout_exit;
486 atomic_inc(&chip->opencount);
487 if (atomic_read(&chip->opencount) > 1 && chip->rate_set)
488 chip->can_set_rate=0;
489dout_exit:
Takashi Iwaibefceea2007-12-03 17:08:40 +0100490 mutex_unlock(&chip->mode_mutex);
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200491 return err;
492}
493
494#endif /* !ECHOCARD_HAS_VMIXER */
495
496#endif /* ECHOCARD_HAS_DIGITAL_IO */
497
498
499
500static int pcm_close(struct snd_pcm_substream *substream)
501{
502 struct echoaudio *chip = snd_pcm_substream_chip(substream);
503 int oc;
504
505 /* Nothing to do here. Audio is already off and pipe will be
506 * freed by its callback
507 */
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200508
509 atomic_dec(&chip->opencount);
510 oc = atomic_read(&chip->opencount);
Sudip Mukherjeeb5b4a412014-11-03 16:04:13 +0530511 dev_dbg(chip->card->dev, "pcm_close oc=%d cs=%d rs=%d\n", oc,
512 chip->can_set_rate, chip->rate_set);
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200513 if (oc < 2)
514 chip->can_set_rate = 1;
515 if (oc == 0)
516 chip->rate_set = 0;
Sudip Mukherjeeb5b4a412014-11-03 16:04:13 +0530517 dev_dbg(chip->card->dev, "pcm_close2 oc=%d cs=%d rs=%d\n", oc,
518 chip->can_set_rate, chip->rate_set);
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200519
520 return 0;
521}
522
523
524
525/* Channel allocation and scatter-gather list setup */
526static 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;
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200532 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) {
Sudip Mukherjeeb5b4a412014-11-03 16:04:13 +0530542 dev_dbg(chip->card->dev, "hwp_ie free(%d)\n", pipe->index);
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200543 err = free_pipes(chip, pipe);
Takashi Iwaida3cec32008-08-08 17:12:14 +0200544 snd_BUG_ON(err);
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200545 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);
Sudip Mukherjeeb5b4a412014-11-03 16:04:13 +0530551 dev_err(chip->card->dev, "allocate_pipes(%d) err=%d\n",
552 pipe_index, err);
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200553 return err;
554 }
555 spin_unlock_irq(&chip->lock);
Sudip Mukherjeeb5b4a412014-11-03 16:04:13 +0530556 dev_dbg(chip->card->dev, "allocate_pipes()=%d\n", pipe_index);
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200557
Sudip Mukherjeeb5b4a412014-11-03 16:04:13 +0530558 dev_dbg(chip->card->dev,
559 "pcm_hw_params (bufsize=%dB periods=%d persize=%dB)\n",
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200560 params_buffer_bytes(hw_params), params_periods(hw_params),
Sudip Mukherjeeb5b4a412014-11-03 16:04:13 +0530561 params_period_bytes(hw_params));
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200562 err = snd_pcm_lib_malloc_pages(substream,
563 params_buffer_bytes(hw_params));
564 if (err < 0) {
Takashi Iwaiece7a362014-02-25 16:43:02 +0100565 dev_err(chip->card->dev, "malloc_pages err=%d\n", err);
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200566 spin_lock_irq(&chip->lock);
567 free_pipes(chip, pipe);
568 spin_unlock_irq(&chip->lock);
569 pipe->index = -1;
570 return err;
571 }
572
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200573 sglist_init(chip, pipe);
574 edge = PAGE_SIZE;
575 for (offs = page = per = 0; offs < params_buffer_bytes(hw_params);
576 per++) {
577 rest = params_period_bytes(hw_params);
578 if (offs + rest > params_buffer_bytes(hw_params))
579 rest = params_buffer_bytes(hw_params) - offs;
580 while (rest) {
Takashi Iwai77a23f22008-08-21 13:00:13 +0200581 dma_addr_t addr;
582 addr = snd_pcm_sgbuf_get_addr(substream, offs);
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200583 if (rest <= edge - offs) {
Takashi Iwai77a23f22008-08-21 13:00:13 +0200584 sglist_add_mapping(chip, pipe, addr, rest);
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200585 sglist_add_irq(chip, pipe);
586 offs += rest;
587 rest = 0;
588 } else {
Takashi Iwai77a23f22008-08-21 13:00:13 +0200589 sglist_add_mapping(chip, pipe, addr,
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200590 edge - offs);
591 rest -= edge - offs;
592 offs = edge;
593 }
594 if (offs == edge) {
595 edge += PAGE_SIZE;
596 page++;
597 }
598 }
599 }
600
601 /* Close the ring buffer */
602 sglist_wrap(chip, pipe);
603
604 /* This stuff is used by the irq handler, so it must be
605 * initialized before chip->substream
606 */
607 chip->last_period[pipe_index] = 0;
608 pipe->last_counter = 0;
609 pipe->position = 0;
610 smp_wmb();
611 chip->substream[pipe_index] = substream;
612 chip->rate_set = 1;
613 spin_lock_irq(&chip->lock);
614 set_sample_rate(chip, hw_params->rate_num / hw_params->rate_den);
615 spin_unlock_irq(&chip->lock);
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200616 return 0;
617}
618
619
620
621static int pcm_analog_in_hw_params(struct snd_pcm_substream *substream,
622 struct snd_pcm_hw_params *hw_params)
623{
624 struct echoaudio *chip = snd_pcm_substream_chip(substream);
625
626 return init_engine(substream, hw_params, px_analog_in(chip) +
627 substream->number, params_channels(hw_params));
628}
629
630
631
632static int pcm_analog_out_hw_params(struct snd_pcm_substream *substream,
633 struct snd_pcm_hw_params *hw_params)
634{
635 return init_engine(substream, hw_params, substream->number,
636 params_channels(hw_params));
637}
638
639
640
641#ifdef ECHOCARD_HAS_DIGITAL_IO
642
643static int pcm_digital_in_hw_params(struct snd_pcm_substream *substream,
644 struct snd_pcm_hw_params *hw_params)
645{
646 struct echoaudio *chip = snd_pcm_substream_chip(substream);
647
648 return init_engine(substream, hw_params, px_digital_in(chip) +
649 substream->number, params_channels(hw_params));
650}
651
652
653
654#ifndef ECHOCARD_HAS_VMIXER /* See the note in snd_echo_new_pcm() */
655static int pcm_digital_out_hw_params(struct snd_pcm_substream *substream,
656 struct snd_pcm_hw_params *hw_params)
657{
658 struct echoaudio *chip = snd_pcm_substream_chip(substream);
659
660 return init_engine(substream, hw_params, px_digital_out(chip) +
661 substream->number, params_channels(hw_params));
662}
663#endif /* !ECHOCARD_HAS_VMIXER */
664
665#endif /* ECHOCARD_HAS_DIGITAL_IO */
666
667
668
669static int pcm_hw_free(struct snd_pcm_substream *substream)
670{
671 struct echoaudio *chip;
672 struct audiopipe *pipe;
673
674 chip = snd_pcm_substream_chip(substream);
675 pipe = (struct audiopipe *) substream->runtime->private_data;
676
677 spin_lock_irq(&chip->lock);
678 if (pipe->index >= 0) {
Sudip Mukherjeeb5b4a412014-11-03 16:04:13 +0530679 dev_dbg(chip->card->dev, "pcm_hw_free(%d)\n", pipe->index);
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200680 free_pipes(chip, pipe);
681 chip->substream[pipe->index] = NULL;
682 pipe->index = -1;
683 }
684 spin_unlock_irq(&chip->lock);
685
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200686 snd_pcm_lib_free_pages(substream);
687 return 0;
688}
689
690
691
692static int pcm_prepare(struct snd_pcm_substream *substream)
693{
694 struct echoaudio *chip = snd_pcm_substream_chip(substream);
695 struct snd_pcm_runtime *runtime = substream->runtime;
696 struct audioformat format;
697 int pipe_index = ((struct audiopipe *)runtime->private_data)->index;
698
Sudip Mukherjeeb5b4a412014-11-03 16:04:13 +0530699 dev_dbg(chip->card->dev, "Prepare rate=%d format=%d channels=%d\n",
700 runtime->rate, runtime->format, runtime->channels);
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200701 format.interleave = runtime->channels;
702 format.data_are_bigendian = 0;
703 format.mono_to_stereo = 0;
704 switch (runtime->format) {
705 case SNDRV_PCM_FORMAT_U8:
706 format.bits_per_sample = 8;
707 break;
708 case SNDRV_PCM_FORMAT_S16_LE:
709 format.bits_per_sample = 16;
710 break;
711 case SNDRV_PCM_FORMAT_S24_3LE:
712 format.bits_per_sample = 24;
713 break;
714 case SNDRV_PCM_FORMAT_S32_BE:
715 format.data_are_bigendian = 1;
Gustavo A. R. Silvaef007522018-08-04 15:13:26 -0500716 /* fall through */
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200717 case SNDRV_PCM_FORMAT_S32_LE:
718 format.bits_per_sample = 32;
719 break;
720 default:
Sudip Mukherjeeb5b4a412014-11-03 16:04:13 +0530721 dev_err(chip->card->dev,
722 "Prepare error: unsupported format %d\n",
723 runtime->format);
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200724 return -EINVAL;
725 }
726
Takashi Iwaida3cec32008-08-08 17:12:14 +0200727 if (snd_BUG_ON(pipe_index >= px_num(chip)))
728 return -EINVAL;
729 if (snd_BUG_ON(!is_pipe_allocated(chip, pipe_index)))
730 return -EINVAL;
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200731 set_audio_format(chip, pipe_index, &format);
732 return 0;
733}
734
735
736
737static int pcm_trigger(struct snd_pcm_substream *substream, int cmd)
738{
739 struct echoaudio *chip = snd_pcm_substream_chip(substream);
Colin Ian King0bc66fd2018-03-12 15:01:00 +0000740 struct audiopipe *pipe;
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200741 int i, err;
742 u32 channelmask = 0;
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200743 struct snd_pcm_substream *s;
744
Takashi Iwaief991b92007-02-22 12:52:53 +0100745 snd_pcm_group_for_each_entry(s, substream) {
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200746 for (i = 0; i < DSP_MAXPIPES; i++) {
747 if (s == chip->substream[i]) {
748 channelmask |= 1 << i;
749 snd_pcm_trigger_done(s, substream);
750 }
751 }
752 }
753
754 spin_lock(&chip->lock);
755 switch (cmd) {
Giuliano Pochini47b5d022010-02-14 18:16:10 +0100756 case SNDRV_PCM_TRIGGER_RESUME:
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200757 case SNDRV_PCM_TRIGGER_START:
758 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200759 for (i = 0; i < DSP_MAXPIPES; i++) {
760 if (channelmask & (1 << i)) {
761 pipe = chip->substream[i]->runtime->private_data;
762 switch (pipe->state) {
763 case PIPE_STATE_STOPPED:
764 chip->last_period[i] = 0;
765 pipe->last_counter = 0;
766 pipe->position = 0;
767 *pipe->dma_counter = 0;
Gustavo A. R. Silvaef007522018-08-04 15:13:26 -0500768 /* fall through */
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200769 case PIPE_STATE_PAUSED:
770 pipe->state = PIPE_STATE_STARTED;
771 break;
772 case PIPE_STATE_STARTED:
773 break;
774 }
775 }
776 }
777 err = start_transport(chip, channelmask,
778 chip->pipe_cyclic_mask);
779 break;
Giuliano Pochini47b5d022010-02-14 18:16:10 +0100780 case SNDRV_PCM_TRIGGER_SUSPEND:
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200781 case SNDRV_PCM_TRIGGER_STOP:
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200782 for (i = 0; i < DSP_MAXPIPES; i++) {
783 if (channelmask & (1 << i)) {
784 pipe = chip->substream[i]->runtime->private_data;
785 pipe->state = PIPE_STATE_STOPPED;
786 }
787 }
788 err = stop_transport(chip, channelmask);
789 break;
790 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200791 for (i = 0; i < DSP_MAXPIPES; i++) {
792 if (channelmask & (1 << i)) {
793 pipe = chip->substream[i]->runtime->private_data;
794 pipe->state = PIPE_STATE_PAUSED;
795 }
796 }
797 err = pause_transport(chip, channelmask);
798 break;
799 default:
800 err = -EINVAL;
801 }
802 spin_unlock(&chip->lock);
803 return err;
804}
805
806
807
808static snd_pcm_uframes_t pcm_pointer(struct snd_pcm_substream *substream)
809{
810 struct snd_pcm_runtime *runtime = substream->runtime;
811 struct audiopipe *pipe = runtime->private_data;
812 size_t cnt, bufsize, pos;
813
814 cnt = le32_to_cpu(*pipe->dma_counter);
815 pipe->position += cnt - pipe->last_counter;
816 pipe->last_counter = cnt;
817 bufsize = substream->runtime->buffer_size;
818 pos = bytes_to_frames(substream->runtime, pipe->position);
819
820 while (pos >= bufsize) {
821 pipe->position -= frames_to_bytes(substream->runtime, bufsize);
822 pos -= bufsize;
823 }
824 return pos;
825}
826
827
828
829/* pcm *_ops structures */
Arvind Yadavafa04882017-08-10 17:17:34 +0530830static const struct snd_pcm_ops analog_playback_ops = {
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200831 .open = pcm_analog_out_open,
832 .close = pcm_close,
833 .ioctl = snd_pcm_lib_ioctl,
834 .hw_params = pcm_analog_out_hw_params,
835 .hw_free = pcm_hw_free,
836 .prepare = pcm_prepare,
837 .trigger = pcm_trigger,
838 .pointer = pcm_pointer,
839 .page = snd_pcm_sgbuf_ops_page,
840};
Arvind Yadavafa04882017-08-10 17:17:34 +0530841static const struct snd_pcm_ops analog_capture_ops = {
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200842 .open = pcm_analog_in_open,
843 .close = pcm_close,
844 .ioctl = snd_pcm_lib_ioctl,
845 .hw_params = pcm_analog_in_hw_params,
846 .hw_free = pcm_hw_free,
847 .prepare = pcm_prepare,
848 .trigger = pcm_trigger,
849 .pointer = pcm_pointer,
850 .page = snd_pcm_sgbuf_ops_page,
851};
852#ifdef ECHOCARD_HAS_DIGITAL_IO
853#ifndef ECHOCARD_HAS_VMIXER
Arvind Yadavafa04882017-08-10 17:17:34 +0530854static const struct snd_pcm_ops digital_playback_ops = {
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200855 .open = pcm_digital_out_open,
856 .close = pcm_close,
857 .ioctl = snd_pcm_lib_ioctl,
858 .hw_params = pcm_digital_out_hw_params,
859 .hw_free = pcm_hw_free,
860 .prepare = pcm_prepare,
861 .trigger = pcm_trigger,
862 .pointer = pcm_pointer,
863 .page = snd_pcm_sgbuf_ops_page,
864};
865#endif /* !ECHOCARD_HAS_VMIXER */
Arvind Yadavafa04882017-08-10 17:17:34 +0530866static const struct snd_pcm_ops digital_capture_ops = {
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200867 .open = pcm_digital_in_open,
868 .close = pcm_close,
869 .ioctl = snd_pcm_lib_ioctl,
870 .hw_params = pcm_digital_in_hw_params,
871 .hw_free = pcm_hw_free,
872 .prepare = pcm_prepare,
873 .trigger = pcm_trigger,
874 .pointer = pcm_pointer,
875 .page = snd_pcm_sgbuf_ops_page,
876};
877#endif /* ECHOCARD_HAS_DIGITAL_IO */
878
879
880
881/* Preallocate memory only for the first substream because it's the most
882 * used one
883 */
884static int snd_echo_preallocate_pages(struct snd_pcm *pcm, struct device *dev)
885{
886 struct snd_pcm_substream *ss;
887 int stream, err;
888
889 for (stream = 0; stream < 2; stream++)
890 for (ss = pcm->streams[stream].substream; ss; ss = ss->next) {
891 err = snd_pcm_lib_preallocate_pages(ss, SNDRV_DMA_TYPE_DEV_SG,
892 dev,
893 ss->number ? 0 : 128<<10,
894 256<<10);
895 if (err < 0)
896 return err;
897 }
898 return 0;
899}
900
901
902
903/*<--snd_echo_probe() */
Bill Pembertone23e7a12012-12-06 12:35:10 -0500904static int snd_echo_new_pcm(struct echoaudio *chip)
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200905{
906 struct snd_pcm *pcm;
907 int err;
908
909#ifdef ECHOCARD_HAS_VMIXER
910 /* This card has a Vmixer, that is there is no direct mapping from PCM
911 streams to physical outputs. The user can mix the streams as he wishes
912 via control interface and it's possible to send any stream to any
913 output, thus it makes no sense to keep analog and digital outputs
914 separated */
915
916 /* PCM#0 Virtual outputs and analog inputs */
917 if ((err = snd_pcm_new(chip->card, "PCM", 0, num_pipes_out(chip),
918 num_analog_busses_in(chip), &pcm)) < 0)
919 return err;
920 pcm->private_data = chip;
921 chip->analog_pcm = pcm;
922 strcpy(pcm->name, chip->card->shortname);
923 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &analog_playback_ops);
924 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &analog_capture_ops);
925 if ((err = snd_echo_preallocate_pages(pcm, snd_dma_pci_data(chip->pci))) < 0)
926 return err;
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200927
928#ifdef ECHOCARD_HAS_DIGITAL_IO
929 /* PCM#1 Digital inputs, no outputs */
930 if ((err = snd_pcm_new(chip->card, "Digital PCM", 1, 0,
931 num_digital_busses_in(chip), &pcm)) < 0)
932 return err;
933 pcm->private_data = chip;
934 chip->digital_pcm = pcm;
935 strcpy(pcm->name, chip->card->shortname);
936 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &digital_capture_ops);
937 if ((err = snd_echo_preallocate_pages(pcm, snd_dma_pci_data(chip->pci))) < 0)
938 return err;
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200939#endif /* ECHOCARD_HAS_DIGITAL_IO */
940
941#else /* ECHOCARD_HAS_VMIXER */
942
943 /* The card can manage substreams formed by analog and digital channels
944 at the same time, but I prefer to keep analog and digital channels
945 separated, because that mixed thing is confusing and useless. So we
946 register two PCM devices: */
947
948 /* PCM#0 Analog i/o */
949 if ((err = snd_pcm_new(chip->card, "Analog PCM", 0,
950 num_analog_busses_out(chip),
951 num_analog_busses_in(chip), &pcm)) < 0)
952 return err;
953 pcm->private_data = chip;
954 chip->analog_pcm = pcm;
955 strcpy(pcm->name, chip->card->shortname);
956 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &analog_playback_ops);
957 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &analog_capture_ops);
958 if ((err = snd_echo_preallocate_pages(pcm, snd_dma_pci_data(chip->pci))) < 0)
959 return err;
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200960
961#ifdef ECHOCARD_HAS_DIGITAL_IO
962 /* PCM#1 Digital i/o */
963 if ((err = snd_pcm_new(chip->card, "Digital PCM", 1,
964 num_digital_busses_out(chip),
965 num_digital_busses_in(chip), &pcm)) < 0)
966 return err;
967 pcm->private_data = chip;
968 chip->digital_pcm = pcm;
969 strcpy(pcm->name, chip->card->shortname);
970 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &digital_playback_ops);
971 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &digital_capture_ops);
972 if ((err = snd_echo_preallocate_pages(pcm, snd_dma_pci_data(chip->pci))) < 0)
973 return err;
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200974#endif /* ECHOCARD_HAS_DIGITAL_IO */
975
976#endif /* ECHOCARD_HAS_VMIXER */
977
978 return 0;
979}
980
981
982
983
984/******************************************************************************
985 Control interface
986******************************************************************************/
987
Giuliano Pochini392bf2f2009-09-30 08:26:45 +0200988#if !defined(ECHOCARD_HAS_VMIXER) || defined(ECHOCARD_HAS_LINE_OUT_GAIN)
Giuliano Pochini9f5d7902009-03-15 21:33:34 +0100989
Giuliano Pochinidd7b2542006-06-28 13:53:41 +0200990/******************* PCM output volume *******************/
991static int snd_echo_output_gain_info(struct snd_kcontrol *kcontrol,
992 struct snd_ctl_elem_info *uinfo)
993{
994 struct echoaudio *chip;
995
996 chip = snd_kcontrol_chip(kcontrol);
997 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
998 uinfo->count = num_busses_out(chip);
999 uinfo->value.integer.min = ECHOGAIN_MINOUT;
1000 uinfo->value.integer.max = ECHOGAIN_MAXOUT;
1001 return 0;
1002}
1003
1004static int snd_echo_output_gain_get(struct snd_kcontrol *kcontrol,
1005 struct snd_ctl_elem_value *ucontrol)
1006{
1007 struct echoaudio *chip;
1008 int c;
1009
1010 chip = snd_kcontrol_chip(kcontrol);
1011 for (c = 0; c < num_busses_out(chip); c++)
1012 ucontrol->value.integer.value[c] = chip->output_gain[c];
1013 return 0;
1014}
1015
1016static int snd_echo_output_gain_put(struct snd_kcontrol *kcontrol,
1017 struct snd_ctl_elem_value *ucontrol)
1018{
1019 struct echoaudio *chip;
1020 int c, changed, gain;
1021
1022 changed = 0;
1023 chip = snd_kcontrol_chip(kcontrol);
1024 spin_lock_irq(&chip->lock);
1025 for (c = 0; c < num_busses_out(chip); c++) {
1026 gain = ucontrol->value.integer.value[c];
1027 /* Ignore out of range values */
1028 if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT)
1029 continue;
1030 if (chip->output_gain[c] != gain) {
1031 set_output_gain(chip, c, gain);
1032 changed = 1;
1033 }
1034 }
1035 if (changed)
1036 update_output_line_level(chip);
1037 spin_unlock_irq(&chip->lock);
1038 return changed;
1039}
1040
Giuliano Pochini392bf2f2009-09-30 08:26:45 +02001041#ifdef ECHOCARD_HAS_LINE_OUT_GAIN
1042/* On the Mia this one controls the line-out volume */
Bhumika Goyalf3b827e2017-02-20 00:18:09 +05301043static const struct snd_kcontrol_new snd_echo_line_output_gain = {
Giuliano Pochini392bf2f2009-09-30 08:26:45 +02001044 .name = "Line Playback Volume",
1045 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1046 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1047 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1048 .info = snd_echo_output_gain_info,
1049 .get = snd_echo_output_gain_get,
1050 .put = snd_echo_output_gain_put,
1051 .tlv = {.p = db_scale_output_gain},
1052};
1053#else
Bhumika Goyalf3b827e2017-02-20 00:18:09 +05301054static const struct snd_kcontrol_new snd_echo_pcm_output_gain = {
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001055 .name = "PCM Playback Volume",
1056 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Giuliano Pochini048b9452006-11-24 13:03:58 +01001057 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001058 .info = snd_echo_output_gain_info,
1059 .get = snd_echo_output_gain_get,
1060 .put = snd_echo_output_gain_put,
Giuliano Pochini048b9452006-11-24 13:03:58 +01001061 .tlv = {.p = db_scale_output_gain},
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001062};
1063#endif
1064
Giuliano Pochini392bf2f2009-09-30 08:26:45 +02001065#endif /* !ECHOCARD_HAS_VMIXER || ECHOCARD_HAS_LINE_OUT_GAIN */
1066
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001067
1068
1069#ifdef ECHOCARD_HAS_INPUT_GAIN
1070
1071/******************* Analog input volume *******************/
1072static int snd_echo_input_gain_info(struct snd_kcontrol *kcontrol,
1073 struct snd_ctl_elem_info *uinfo)
1074{
1075 struct echoaudio *chip;
1076
1077 chip = snd_kcontrol_chip(kcontrol);
1078 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1079 uinfo->count = num_analog_busses_in(chip);
1080 uinfo->value.integer.min = ECHOGAIN_MININP;
1081 uinfo->value.integer.max = ECHOGAIN_MAXINP;
1082 return 0;
1083}
1084
1085static int snd_echo_input_gain_get(struct snd_kcontrol *kcontrol,
1086 struct snd_ctl_elem_value *ucontrol)
1087{
1088 struct echoaudio *chip;
1089 int c;
1090
1091 chip = snd_kcontrol_chip(kcontrol);
1092 for (c = 0; c < num_analog_busses_in(chip); c++)
1093 ucontrol->value.integer.value[c] = chip->input_gain[c];
1094 return 0;
1095}
1096
1097static int snd_echo_input_gain_put(struct snd_kcontrol *kcontrol,
1098 struct snd_ctl_elem_value *ucontrol)
1099{
1100 struct echoaudio *chip;
1101 int c, gain, changed;
1102
1103 changed = 0;
1104 chip = snd_kcontrol_chip(kcontrol);
1105 spin_lock_irq(&chip->lock);
1106 for (c = 0; c < num_analog_busses_in(chip); c++) {
1107 gain = ucontrol->value.integer.value[c];
1108 /* Ignore out of range values */
1109 if (gain < ECHOGAIN_MININP || gain > ECHOGAIN_MAXINP)
1110 continue;
1111 if (chip->input_gain[c] != gain) {
1112 set_input_gain(chip, c, gain);
1113 changed = 1;
1114 }
1115 }
1116 if (changed)
1117 update_input_line_level(chip);
1118 spin_unlock_irq(&chip->lock);
1119 return changed;
1120}
1121
Takashi Iwai0cb29ea2007-01-29 15:33:49 +01001122static const DECLARE_TLV_DB_SCALE(db_scale_input_gain, -2500, 50, 0);
Giuliano Pochini048b9452006-11-24 13:03:58 +01001123
Bhumika Goyalf3b827e2017-02-20 00:18:09 +05301124static const struct snd_kcontrol_new snd_echo_line_input_gain = {
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001125 .name = "Line Capture Volume",
1126 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Giuliano Pochini048b9452006-11-24 13:03:58 +01001127 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001128 .info = snd_echo_input_gain_info,
1129 .get = snd_echo_input_gain_get,
1130 .put = snd_echo_input_gain_put,
Giuliano Pochini048b9452006-11-24 13:03:58 +01001131 .tlv = {.p = db_scale_input_gain},
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001132};
1133
1134#endif /* ECHOCARD_HAS_INPUT_GAIN */
1135
1136
1137
1138#ifdef ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL
1139
1140/************ Analog output nominal level (+4dBu / -10dBV) ***************/
1141static int snd_echo_output_nominal_info (struct snd_kcontrol *kcontrol,
1142 struct snd_ctl_elem_info *uinfo)
1143{
1144 struct echoaudio *chip;
1145
1146 chip = snd_kcontrol_chip(kcontrol);
1147 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1148 uinfo->count = num_analog_busses_out(chip);
1149 uinfo->value.integer.min = 0;
1150 uinfo->value.integer.max = 1;
1151 return 0;
1152}
1153
1154static int snd_echo_output_nominal_get(struct snd_kcontrol *kcontrol,
1155 struct snd_ctl_elem_value *ucontrol)
1156{
1157 struct echoaudio *chip;
1158 int c;
1159
1160 chip = snd_kcontrol_chip(kcontrol);
1161 for (c = 0; c < num_analog_busses_out(chip); c++)
1162 ucontrol->value.integer.value[c] = chip->nominal_level[c];
1163 return 0;
1164}
1165
1166static int snd_echo_output_nominal_put(struct snd_kcontrol *kcontrol,
1167 struct snd_ctl_elem_value *ucontrol)
1168{
1169 struct echoaudio *chip;
1170 int c, changed;
1171
1172 changed = 0;
1173 chip = snd_kcontrol_chip(kcontrol);
1174 spin_lock_irq(&chip->lock);
1175 for (c = 0; c < num_analog_busses_out(chip); c++) {
1176 if (chip->nominal_level[c] != ucontrol->value.integer.value[c]) {
1177 set_nominal_level(chip, c,
1178 ucontrol->value.integer.value[c]);
1179 changed = 1;
1180 }
1181 }
1182 if (changed)
1183 update_output_line_level(chip);
1184 spin_unlock_irq(&chip->lock);
1185 return changed;
1186}
1187
Bhumika Goyalf3b827e2017-02-20 00:18:09 +05301188static const struct snd_kcontrol_new snd_echo_output_nominal_level = {
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001189 .name = "Line Playback Switch (-10dBV)",
1190 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1191 .info = snd_echo_output_nominal_info,
1192 .get = snd_echo_output_nominal_get,
1193 .put = snd_echo_output_nominal_put,
1194};
1195
1196#endif /* ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL */
1197
1198
1199
1200#ifdef ECHOCARD_HAS_INPUT_NOMINAL_LEVEL
1201
1202/*************** Analog input nominal level (+4dBu / -10dBV) ***************/
1203static int snd_echo_input_nominal_info(struct snd_kcontrol *kcontrol,
1204 struct snd_ctl_elem_info *uinfo)
1205{
1206 struct echoaudio *chip;
1207
1208 chip = snd_kcontrol_chip(kcontrol);
1209 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1210 uinfo->count = num_analog_busses_in(chip);
1211 uinfo->value.integer.min = 0;
1212 uinfo->value.integer.max = 1;
1213 return 0;
1214}
1215
1216static int snd_echo_input_nominal_get(struct snd_kcontrol *kcontrol,
1217 struct snd_ctl_elem_value *ucontrol)
1218{
1219 struct echoaudio *chip;
1220 int c;
1221
1222 chip = snd_kcontrol_chip(kcontrol);
1223 for (c = 0; c < num_analog_busses_in(chip); c++)
1224 ucontrol->value.integer.value[c] =
1225 chip->nominal_level[bx_analog_in(chip) + c];
1226 return 0;
1227}
1228
1229static int snd_echo_input_nominal_put(struct snd_kcontrol *kcontrol,
1230 struct snd_ctl_elem_value *ucontrol)
1231{
1232 struct echoaudio *chip;
1233 int c, changed;
1234
1235 changed = 0;
1236 chip = snd_kcontrol_chip(kcontrol);
1237 spin_lock_irq(&chip->lock);
1238 for (c = 0; c < num_analog_busses_in(chip); c++) {
1239 if (chip->nominal_level[bx_analog_in(chip) + c] !=
1240 ucontrol->value.integer.value[c]) {
1241 set_nominal_level(chip, bx_analog_in(chip) + c,
1242 ucontrol->value.integer.value[c]);
1243 changed = 1;
1244 }
1245 }
1246 if (changed)
1247 update_output_line_level(chip); /* "Output" is not a mistake
1248 * here.
1249 */
1250 spin_unlock_irq(&chip->lock);
1251 return changed;
1252}
1253
Bhumika Goyalf3b827e2017-02-20 00:18:09 +05301254static const struct snd_kcontrol_new snd_echo_intput_nominal_level = {
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001255 .name = "Line Capture Switch (-10dBV)",
1256 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1257 .info = snd_echo_input_nominal_info,
1258 .get = snd_echo_input_nominal_get,
1259 .put = snd_echo_input_nominal_put,
1260};
1261
1262#endif /* ECHOCARD_HAS_INPUT_NOMINAL_LEVEL */
1263
1264
1265
1266#ifdef ECHOCARD_HAS_MONITOR
1267
1268/******************* Monitor mixer *******************/
1269static int snd_echo_mixer_info(struct snd_kcontrol *kcontrol,
1270 struct snd_ctl_elem_info *uinfo)
1271{
1272 struct echoaudio *chip;
1273
1274 chip = snd_kcontrol_chip(kcontrol);
1275 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Takashi Sakamoto51db4522017-09-26 09:11:49 +09001276 uinfo->count = 1;
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001277 uinfo->value.integer.min = ECHOGAIN_MINOUT;
1278 uinfo->value.integer.max = ECHOGAIN_MAXOUT;
1279 uinfo->dimen.d[0] = num_busses_out(chip);
1280 uinfo->dimen.d[1] = num_busses_in(chip);
1281 return 0;
1282}
1283
1284static int snd_echo_mixer_get(struct snd_kcontrol *kcontrol,
1285 struct snd_ctl_elem_value *ucontrol)
1286{
Dan Carpenter77008b72015-03-23 12:41:31 +03001287 struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1288 unsigned int out = ucontrol->id.index / num_busses_in(chip);
1289 unsigned int in = ucontrol->id.index % num_busses_in(chip);
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001290
Dan Carpenter77008b72015-03-23 12:41:31 +03001291 if (out >= ECHO_MAXAUDIOOUTPUTS || in >= ECHO_MAXAUDIOINPUTS)
1292 return -EINVAL;
1293
1294 ucontrol->value.integer.value[0] = chip->monitor_gain[out][in];
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001295 return 0;
1296}
1297
1298static int snd_echo_mixer_put(struct snd_kcontrol *kcontrol,
1299 struct snd_ctl_elem_value *ucontrol)
1300{
1301 struct echoaudio *chip;
1302 int changed, gain;
Dan Carpenter77008b72015-03-23 12:41:31 +03001303 unsigned int out, in;
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001304
1305 changed = 0;
1306 chip = snd_kcontrol_chip(kcontrol);
1307 out = ucontrol->id.index / num_busses_in(chip);
1308 in = ucontrol->id.index % num_busses_in(chip);
Dan Carpenter77008b72015-03-23 12:41:31 +03001309 if (out >= ECHO_MAXAUDIOOUTPUTS || in >= ECHO_MAXAUDIOINPUTS)
1310 return -EINVAL;
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001311 gain = ucontrol->value.integer.value[0];
1312 if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT)
1313 return -EINVAL;
1314 if (chip->monitor_gain[out][in] != gain) {
1315 spin_lock_irq(&chip->lock);
1316 set_monitor_gain(chip, out, in, gain);
1317 update_output_line_level(chip);
1318 spin_unlock_irq(&chip->lock);
1319 changed = 1;
1320 }
1321 return changed;
1322}
1323
Bill Pembertone23e7a12012-12-06 12:35:10 -05001324static struct snd_kcontrol_new snd_echo_monitor_mixer = {
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001325 .name = "Monitor Mixer Volume",
1326 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Giuliano Pochini048b9452006-11-24 13:03:58 +01001327 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001328 .info = snd_echo_mixer_info,
1329 .get = snd_echo_mixer_get,
1330 .put = snd_echo_mixer_put,
Giuliano Pochini048b9452006-11-24 13:03:58 +01001331 .tlv = {.p = db_scale_output_gain},
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001332};
1333
1334#endif /* ECHOCARD_HAS_MONITOR */
1335
1336
1337
1338#ifdef ECHOCARD_HAS_VMIXER
1339
1340/******************* Vmixer *******************/
1341static int snd_echo_vmixer_info(struct snd_kcontrol *kcontrol,
1342 struct snd_ctl_elem_info *uinfo)
1343{
1344 struct echoaudio *chip;
1345
1346 chip = snd_kcontrol_chip(kcontrol);
1347 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Takashi Sakamoto51db4522017-09-26 09:11:49 +09001348 uinfo->count = 1;
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001349 uinfo->value.integer.min = ECHOGAIN_MINOUT;
1350 uinfo->value.integer.max = ECHOGAIN_MAXOUT;
1351 uinfo->dimen.d[0] = num_busses_out(chip);
1352 uinfo->dimen.d[1] = num_pipes_out(chip);
1353 return 0;
1354}
1355
1356static int snd_echo_vmixer_get(struct snd_kcontrol *kcontrol,
1357 struct snd_ctl_elem_value *ucontrol)
1358{
1359 struct echoaudio *chip;
1360
1361 chip = snd_kcontrol_chip(kcontrol);
1362 ucontrol->value.integer.value[0] =
1363 chip->vmixer_gain[ucontrol->id.index / num_pipes_out(chip)]
1364 [ucontrol->id.index % num_pipes_out(chip)];
1365 return 0;
1366}
1367
1368static int snd_echo_vmixer_put(struct snd_kcontrol *kcontrol,
1369 struct snd_ctl_elem_value *ucontrol)
1370{
1371 struct echoaudio *chip;
1372 int gain, changed;
1373 short vch, out;
1374
1375 changed = 0;
1376 chip = snd_kcontrol_chip(kcontrol);
1377 out = ucontrol->id.index / num_pipes_out(chip);
1378 vch = ucontrol->id.index % num_pipes_out(chip);
1379 gain = ucontrol->value.integer.value[0];
1380 if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT)
1381 return -EINVAL;
1382 if (chip->vmixer_gain[out][vch] != ucontrol->value.integer.value[0]) {
1383 spin_lock_irq(&chip->lock);
1384 set_vmixer_gain(chip, out, vch, ucontrol->value.integer.value[0]);
1385 update_vmixer_level(chip);
1386 spin_unlock_irq(&chip->lock);
1387 changed = 1;
1388 }
1389 return changed;
1390}
1391
Bill Pembertone23e7a12012-12-06 12:35:10 -05001392static struct snd_kcontrol_new snd_echo_vmixer = {
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001393 .name = "VMixer Volume",
1394 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Giuliano Pochini048b9452006-11-24 13:03:58 +01001395 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001396 .info = snd_echo_vmixer_info,
1397 .get = snd_echo_vmixer_get,
1398 .put = snd_echo_vmixer_put,
Giuliano Pochini048b9452006-11-24 13:03:58 +01001399 .tlv = {.p = db_scale_output_gain},
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001400};
1401
1402#endif /* ECHOCARD_HAS_VMIXER */
1403
1404
1405
1406#ifdef ECHOCARD_HAS_DIGITAL_MODE_SWITCH
1407
1408/******************* Digital mode switch *******************/
1409static int snd_echo_digital_mode_info(struct snd_kcontrol *kcontrol,
1410 struct snd_ctl_elem_info *uinfo)
1411{
Takashi Iwaic69a4f32014-10-20 18:16:38 +02001412 static const char * const names[4] = {
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001413 "S/PDIF Coaxial", "S/PDIF Optical", "ADAT Optical",
1414 "S/PDIF Cdrom"
1415 };
1416 struct echoaudio *chip;
1417
1418 chip = snd_kcontrol_chip(kcontrol);
Takashi Iwaic69a4f32014-10-20 18:16:38 +02001419 return snd_ctl_enum_info(uinfo, 1, chip->num_digital_modes, names);
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001420}
1421
1422static int snd_echo_digital_mode_get(struct snd_kcontrol *kcontrol,
1423 struct snd_ctl_elem_value *ucontrol)
1424{
1425 struct echoaudio *chip;
1426 int i, mode;
1427
1428 chip = snd_kcontrol_chip(kcontrol);
1429 mode = chip->digital_mode;
1430 for (i = chip->num_digital_modes - 1; i >= 0; i--)
1431 if (mode == chip->digital_mode_list[i]) {
1432 ucontrol->value.enumerated.item[0] = i;
1433 break;
1434 }
1435 return 0;
1436}
1437
1438static int snd_echo_digital_mode_put(struct snd_kcontrol *kcontrol,
1439 struct snd_ctl_elem_value *ucontrol)
1440{
1441 struct echoaudio *chip;
1442 int changed;
1443 unsigned short emode, dmode;
1444
1445 changed = 0;
1446 chip = snd_kcontrol_chip(kcontrol);
1447
1448 emode = ucontrol->value.enumerated.item[0];
1449 if (emode >= chip->num_digital_modes)
1450 return -EINVAL;
1451 dmode = chip->digital_mode_list[emode];
1452
1453 if (dmode != chip->digital_mode) {
1454 /* mode_mutex is required to make this operation atomic wrt
1455 pcm_digital_*_open() and set_input_clock() functions. */
Takashi Iwaibefceea2007-12-03 17:08:40 +01001456 mutex_lock(&chip->mode_mutex);
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001457
1458 /* Do not allow the user to change the digital mode when a pcm
1459 device is open because it also changes the number of channels
1460 and the allowed sample rates */
1461 if (atomic_read(&chip->opencount)) {
1462 changed = -EAGAIN;
1463 } else {
1464 changed = set_digital_mode(chip, dmode);
1465 /* If we had to change the clock source, report it */
1466 if (changed > 0 && chip->clock_src_ctl) {
1467 snd_ctl_notify(chip->card,
1468 SNDRV_CTL_EVENT_MASK_VALUE,
1469 &chip->clock_src_ctl->id);
Sudip Mukherjeeb5b4a412014-11-03 16:04:13 +05301470 dev_dbg(chip->card->dev,
1471 "SDM() =%d\n", changed);
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001472 }
1473 if (changed >= 0)
1474 changed = 1; /* No errors */
1475 }
Takashi Iwaibefceea2007-12-03 17:08:40 +01001476 mutex_unlock(&chip->mode_mutex);
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001477 }
1478 return changed;
1479}
1480
Bhumika Goyalf3b827e2017-02-20 00:18:09 +05301481static const struct snd_kcontrol_new snd_echo_digital_mode_switch = {
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001482 .name = "Digital mode Switch",
1483 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1484 .info = snd_echo_digital_mode_info,
1485 .get = snd_echo_digital_mode_get,
1486 .put = snd_echo_digital_mode_put,
1487};
1488
1489#endif /* ECHOCARD_HAS_DIGITAL_MODE_SWITCH */
1490
1491
1492
1493#ifdef ECHOCARD_HAS_DIGITAL_IO
1494
1495/******************* S/PDIF mode switch *******************/
1496static int snd_echo_spdif_mode_info(struct snd_kcontrol *kcontrol,
1497 struct snd_ctl_elem_info *uinfo)
1498{
Takashi Iwaic69a4f32014-10-20 18:16:38 +02001499 static const char * const names[2] = {"Consumer", "Professional"};
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001500
Takashi Iwaic69a4f32014-10-20 18:16:38 +02001501 return snd_ctl_enum_info(uinfo, 1, 2, names);
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001502}
1503
1504static int snd_echo_spdif_mode_get(struct snd_kcontrol *kcontrol,
1505 struct snd_ctl_elem_value *ucontrol)
1506{
1507 struct echoaudio *chip;
1508
1509 chip = snd_kcontrol_chip(kcontrol);
1510 ucontrol->value.enumerated.item[0] = !!chip->professional_spdif;
1511 return 0;
1512}
1513
1514static int snd_echo_spdif_mode_put(struct snd_kcontrol *kcontrol,
1515 struct snd_ctl_elem_value *ucontrol)
1516{
1517 struct echoaudio *chip;
1518 int mode;
1519
1520 chip = snd_kcontrol_chip(kcontrol);
1521 mode = !!ucontrol->value.enumerated.item[0];
1522 if (mode != chip->professional_spdif) {
1523 spin_lock_irq(&chip->lock);
1524 set_professional_spdif(chip, mode);
1525 spin_unlock_irq(&chip->lock);
1526 return 1;
1527 }
1528 return 0;
1529}
1530
Bhumika Goyalf3b827e2017-02-20 00:18:09 +05301531static const struct snd_kcontrol_new snd_echo_spdif_mode_switch = {
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001532 .name = "S/PDIF mode Switch",
1533 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1534 .info = snd_echo_spdif_mode_info,
1535 .get = snd_echo_spdif_mode_get,
1536 .put = snd_echo_spdif_mode_put,
1537};
1538
1539#endif /* ECHOCARD_HAS_DIGITAL_IO */
1540
1541
1542
1543#ifdef ECHOCARD_HAS_EXTERNAL_CLOCK
1544
1545/******************* Select input clock source *******************/
1546static int snd_echo_clock_source_info(struct snd_kcontrol *kcontrol,
1547 struct snd_ctl_elem_info *uinfo)
1548{
Takashi Iwaic69a4f32014-10-20 18:16:38 +02001549 static const char * const names[8] = {
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001550 "Internal", "Word", "Super", "S/PDIF", "ADAT", "ESync",
1551 "ESync96", "MTC"
1552 };
1553 struct echoaudio *chip;
1554
1555 chip = snd_kcontrol_chip(kcontrol);
Takashi Iwaic69a4f32014-10-20 18:16:38 +02001556 return snd_ctl_enum_info(uinfo, 1, chip->num_clock_sources, names);
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001557}
1558
1559static int snd_echo_clock_source_get(struct snd_kcontrol *kcontrol,
1560 struct snd_ctl_elem_value *ucontrol)
1561{
1562 struct echoaudio *chip;
1563 int i, clock;
1564
1565 chip = snd_kcontrol_chip(kcontrol);
1566 clock = chip->input_clock;
1567
1568 for (i = 0; i < chip->num_clock_sources; i++)
1569 if (clock == chip->clock_source_list[i])
1570 ucontrol->value.enumerated.item[0] = i;
1571
1572 return 0;
1573}
1574
1575static int snd_echo_clock_source_put(struct snd_kcontrol *kcontrol,
1576 struct snd_ctl_elem_value *ucontrol)
1577{
1578 struct echoaudio *chip;
1579 int changed;
1580 unsigned int eclock, dclock;
1581
1582 changed = 0;
1583 chip = snd_kcontrol_chip(kcontrol);
1584 eclock = ucontrol->value.enumerated.item[0];
1585 if (eclock >= chip->input_clock_types)
1586 return -EINVAL;
1587 dclock = chip->clock_source_list[eclock];
1588 if (chip->input_clock != dclock) {
Takashi Iwaibefceea2007-12-03 17:08:40 +01001589 mutex_lock(&chip->mode_mutex);
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001590 spin_lock_irq(&chip->lock);
1591 if ((changed = set_input_clock(chip, dclock)) == 0)
1592 changed = 1; /* no errors */
1593 spin_unlock_irq(&chip->lock);
Takashi Iwaibefceea2007-12-03 17:08:40 +01001594 mutex_unlock(&chip->mode_mutex);
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001595 }
1596
1597 if (changed < 0)
Sudip Mukherjeeb5b4a412014-11-03 16:04:13 +05301598 dev_dbg(chip->card->dev,
1599 "seticlk val%d err 0x%x\n", dclock, changed);
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001600
1601 return changed;
1602}
1603
Bhumika Goyalf3b827e2017-02-20 00:18:09 +05301604static const struct snd_kcontrol_new snd_echo_clock_source_switch = {
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001605 .name = "Sample Clock Source",
1606 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1607 .info = snd_echo_clock_source_info,
1608 .get = snd_echo_clock_source_get,
1609 .put = snd_echo_clock_source_put,
1610};
1611
1612#endif /* ECHOCARD_HAS_EXTERNAL_CLOCK */
1613
1614
1615
1616#ifdef ECHOCARD_HAS_PHANTOM_POWER
1617
1618/******************* Phantom power switch *******************/
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001619#define snd_echo_phantom_power_info snd_ctl_boolean_mono_info
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001620
1621static int snd_echo_phantom_power_get(struct snd_kcontrol *kcontrol,
1622 struct snd_ctl_elem_value *ucontrol)
1623{
1624 struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1625
1626 ucontrol->value.integer.value[0] = chip->phantom_power;
1627 return 0;
1628}
1629
1630static int snd_echo_phantom_power_put(struct snd_kcontrol *kcontrol,
1631 struct snd_ctl_elem_value *ucontrol)
1632{
1633 struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1634 int power, changed = 0;
1635
1636 power = !!ucontrol->value.integer.value[0];
1637 if (chip->phantom_power != power) {
1638 spin_lock_irq(&chip->lock);
1639 changed = set_phantom_power(chip, power);
1640 spin_unlock_irq(&chip->lock);
1641 if (changed == 0)
1642 changed = 1; /* no errors */
1643 }
1644 return changed;
1645}
1646
Bhumika Goyalf3b827e2017-02-20 00:18:09 +05301647static const struct snd_kcontrol_new snd_echo_phantom_power_switch = {
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001648 .name = "Phantom power Switch",
1649 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1650 .info = snd_echo_phantom_power_info,
1651 .get = snd_echo_phantom_power_get,
1652 .put = snd_echo_phantom_power_put,
1653};
1654
1655#endif /* ECHOCARD_HAS_PHANTOM_POWER */
1656
1657
1658
1659#ifdef ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE
1660
1661/******************* Digital input automute switch *******************/
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001662#define snd_echo_automute_info snd_ctl_boolean_mono_info
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001663
1664static int snd_echo_automute_get(struct snd_kcontrol *kcontrol,
1665 struct snd_ctl_elem_value *ucontrol)
1666{
1667 struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1668
1669 ucontrol->value.integer.value[0] = chip->digital_in_automute;
1670 return 0;
1671}
1672
1673static int snd_echo_automute_put(struct snd_kcontrol *kcontrol,
1674 struct snd_ctl_elem_value *ucontrol)
1675{
1676 struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1677 int automute, changed = 0;
1678
1679 automute = !!ucontrol->value.integer.value[0];
1680 if (chip->digital_in_automute != automute) {
1681 spin_lock_irq(&chip->lock);
1682 changed = set_input_auto_mute(chip, automute);
1683 spin_unlock_irq(&chip->lock);
1684 if (changed == 0)
1685 changed = 1; /* no errors */
1686 }
1687 return changed;
1688}
1689
Bhumika Goyalf3b827e2017-02-20 00:18:09 +05301690static const struct snd_kcontrol_new snd_echo_automute_switch = {
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001691 .name = "Digital Capture Switch (automute)",
1692 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1693 .info = snd_echo_automute_info,
1694 .get = snd_echo_automute_get,
1695 .put = snd_echo_automute_put,
1696};
1697
1698#endif /* ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE */
1699
1700
1701
1702/******************* VU-meters switch *******************/
Takashi Iwaia5ce8892007-07-23 15:42:26 +02001703#define snd_echo_vumeters_switch_info snd_ctl_boolean_mono_info
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001704
1705static int snd_echo_vumeters_switch_put(struct snd_kcontrol *kcontrol,
1706 struct snd_ctl_elem_value *ucontrol)
1707{
1708 struct echoaudio *chip;
1709
1710 chip = snd_kcontrol_chip(kcontrol);
1711 spin_lock_irq(&chip->lock);
1712 set_meters_on(chip, ucontrol->value.integer.value[0]);
1713 spin_unlock_irq(&chip->lock);
1714 return 1;
1715}
1716
Bhumika Goyalf3b827e2017-02-20 00:18:09 +05301717static const struct snd_kcontrol_new snd_echo_vumeters_switch = {
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001718 .name = "VU-meters Switch",
1719 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1720 .access = SNDRV_CTL_ELEM_ACCESS_WRITE,
1721 .info = snd_echo_vumeters_switch_info,
1722 .put = snd_echo_vumeters_switch_put,
1723};
1724
1725
1726
1727/***** Read VU-meters (input, output, analog and digital together) *****/
1728static int snd_echo_vumeters_info(struct snd_kcontrol *kcontrol,
1729 struct snd_ctl_elem_info *uinfo)
1730{
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001731 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Takashi Sakamoto51db4522017-09-26 09:11:49 +09001732 uinfo->count = 96;
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001733 uinfo->value.integer.min = ECHOGAIN_MINOUT;
1734 uinfo->value.integer.max = 0;
1735#ifdef ECHOCARD_HAS_VMIXER
1736 uinfo->dimen.d[0] = 3; /* Out, In, Virt */
1737#else
1738 uinfo->dimen.d[0] = 2; /* Out, In */
1739#endif
1740 uinfo->dimen.d[1] = 16; /* 16 channels */
1741 uinfo->dimen.d[2] = 2; /* 0=level, 1=peak */
1742 return 0;
1743}
1744
1745static int snd_echo_vumeters_get(struct snd_kcontrol *kcontrol,
1746 struct snd_ctl_elem_value *ucontrol)
1747{
1748 struct echoaudio *chip;
1749
1750 chip = snd_kcontrol_chip(kcontrol);
1751 get_audio_meters(chip, ucontrol->value.integer.value);
1752 return 0;
1753}
1754
Bhumika Goyalf3b827e2017-02-20 00:18:09 +05301755static const struct snd_kcontrol_new snd_echo_vumeters = {
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001756 .name = "VU-meters",
1757 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
Giuliano Pochini048b9452006-11-24 13:03:58 +01001758 .access = SNDRV_CTL_ELEM_ACCESS_READ |
1759 SNDRV_CTL_ELEM_ACCESS_VOLATILE |
1760 SNDRV_CTL_ELEM_ACCESS_TLV_READ,
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001761 .info = snd_echo_vumeters_info,
1762 .get = snd_echo_vumeters_get,
Giuliano Pochini048b9452006-11-24 13:03:58 +01001763 .tlv = {.p = db_scale_output_gain},
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001764};
1765
1766
1767
1768/*** Channels info - it exports informations about the number of channels ***/
1769static int snd_echo_channels_info_info(struct snd_kcontrol *kcontrol,
1770 struct snd_ctl_elem_info *uinfo)
1771{
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001772 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1773 uinfo->count = 6;
1774 uinfo->value.integer.min = 0;
1775 uinfo->value.integer.max = 1 << ECHO_CLOCK_NUMBER;
1776 return 0;
1777}
1778
1779static int snd_echo_channels_info_get(struct snd_kcontrol *kcontrol,
1780 struct snd_ctl_elem_value *ucontrol)
1781{
1782 struct echoaudio *chip;
1783 int detected, clocks, bit, src;
1784
1785 chip = snd_kcontrol_chip(kcontrol);
1786 ucontrol->value.integer.value[0] = num_busses_in(chip);
1787 ucontrol->value.integer.value[1] = num_analog_busses_in(chip);
1788 ucontrol->value.integer.value[2] = num_busses_out(chip);
1789 ucontrol->value.integer.value[3] = num_analog_busses_out(chip);
1790 ucontrol->value.integer.value[4] = num_pipes_out(chip);
1791
1792 /* Compute the bitmask of the currently valid input clocks */
1793 detected = detect_input_clocks(chip);
1794 clocks = 0;
1795 src = chip->num_clock_sources - 1;
1796 for (bit = ECHO_CLOCK_NUMBER - 1; bit >= 0; bit--)
1797 if (detected & (1 << bit))
1798 for (; src >= 0; src--)
1799 if (bit == chip->clock_source_list[src]) {
1800 clocks |= 1 << src;
1801 break;
1802 }
1803 ucontrol->value.integer.value[5] = clocks;
1804
1805 return 0;
1806}
1807
Bhumika Goyalf3b827e2017-02-20 00:18:09 +05301808static const struct snd_kcontrol_new snd_echo_channels_info = {
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001809 .name = "Channels info",
1810 .iface = SNDRV_CTL_ELEM_IFACE_HWDEP,
1811 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
1812 .info = snd_echo_channels_info_info,
1813 .get = snd_echo_channels_info_get,
1814};
1815
1816
1817
1818
1819/******************************************************************************
1820 IRQ Handler
1821******************************************************************************/
1822
David Howells7d12e782006-10-05 14:55:46 +01001823static irqreturn_t snd_echo_interrupt(int irq, void *dev_id)
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001824{
1825 struct echoaudio *chip = dev_id;
1826 struct snd_pcm_substream *substream;
1827 int period, ss, st;
1828
1829 spin_lock(&chip->lock);
1830 st = service_irq(chip);
1831 if (st < 0) {
1832 spin_unlock(&chip->lock);
1833 return IRQ_NONE;
1834 }
1835 /* The hardware doesn't tell us which substream caused the irq,
1836 thus we have to check all running substreams. */
1837 for (ss = 0; ss < DSP_MAXPIPES; ss++) {
Giuliano Pochinib721e682010-02-17 00:57:44 +01001838 substream = chip->substream[ss];
1839 if (substream && ((struct audiopipe *)substream->runtime->
1840 private_data)->state == PIPE_STATE_STARTED) {
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001841 period = pcm_pointer(substream) /
1842 substream->runtime->period_size;
1843 if (period != chip->last_period[ss]) {
1844 chip->last_period[ss] = period;
1845 spin_unlock(&chip->lock);
1846 snd_pcm_period_elapsed(substream);
1847 spin_lock(&chip->lock);
1848 }
1849 }
1850 }
1851 spin_unlock(&chip->lock);
1852
1853#ifdef ECHOCARD_HAS_MIDI
1854 if (st > 0 && chip->midi_in) {
1855 snd_rawmidi_receive(chip->midi_in, chip->midi_buffer, st);
Sudip Mukherjeeb5b4a412014-11-03 16:04:13 +05301856 dev_dbg(chip->card->dev, "rawmidi_iread=%d\n", st);
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001857 }
1858#endif
1859 return IRQ_HANDLED;
1860}
1861
1862
1863
1864
1865/******************************************************************************
1866 Module construction / destruction
1867******************************************************************************/
1868
1869static int snd_echo_free(struct echoaudio *chip)
1870{
Takashi Iwaiebf029d2008-04-22 17:28:11 +02001871 if (chip->comm_page)
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001872 rest_in_peace(chip);
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001873
1874 if (chip->irq >= 0)
Takashi Iwai437a5a42006-11-21 12:14:23 +01001875 free_irq(chip->irq, chip);
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001876
Takashi Iwaiebf029d2008-04-22 17:28:11 +02001877 if (chip->comm_page)
1878 snd_dma_free_pages(&chip->commpage_dma_buf);
1879
Markus Elfringff6defa2015-01-03 22:55:54 +01001880 iounmap(chip->dsp_registers);
Markus Elfring057a4a52014-12-02 18:34:45 +01001881 release_and_free_resource(chip->iores);
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001882 pci_disable_device(chip->pci);
1883
1884 /* release chip data */
Giuliano Pochini4f8ada42010-02-14 18:15:51 +01001885 free_firmware_cache(chip);
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001886 kfree(chip);
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001887 return 0;
1888}
1889
1890
1891
1892static int snd_echo_dev_free(struct snd_device *device)
1893{
1894 struct echoaudio *chip = device->device_data;
1895
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001896 return snd_echo_free(chip);
1897}
1898
1899
1900
1901/* <--snd_echo_probe() */
Bill Pembertone23e7a12012-12-06 12:35:10 -05001902static int snd_echo_create(struct snd_card *card,
1903 struct pci_dev *pci,
1904 struct echoaudio **rchip)
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001905{
1906 struct echoaudio *chip;
1907 int err;
1908 size_t sz;
1909 static struct snd_device_ops ops = {
1910 .dev_free = snd_echo_dev_free,
1911 };
1912
1913 *rchip = NULL;
1914
1915 pci_write_config_byte(pci, PCI_LATENCY_TIMER, 0xC0);
1916
1917 if ((err = pci_enable_device(pci)) < 0)
1918 return err;
1919 pci_set_master(pci);
1920
Giuliano Pochini47b5d022010-02-14 18:16:10 +01001921 /* Allocate chip if needed */
1922 if (!*rchip) {
1923 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1924 if (!chip) {
1925 pci_disable_device(pci);
1926 return -ENOMEM;
1927 }
Sudip Mukherjeeb5b4a412014-11-03 16:04:13 +05301928 dev_dbg(card->dev, "chip=%p\n", chip);
Giuliano Pochini47b5d022010-02-14 18:16:10 +01001929 spin_lock_init(&chip->lock);
1930 chip->card = card;
1931 chip->pci = pci;
1932 chip->irq = -1;
1933 atomic_set(&chip->opencount, 0);
1934 mutex_init(&chip->mode_mutex);
1935 chip->can_set_rate = 1;
1936 } else {
1937 /* If this was called from the resume function, chip is
1938 * already allocated and it contains current card settings.
1939 */
1940 chip = *rchip;
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001941 }
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001942
1943 /* PCI resource allocation */
1944 chip->dsp_registers_phys = pci_resource_start(pci, 0);
1945 sz = pci_resource_len(pci, 0);
1946 if (sz > PAGE_SIZE)
1947 sz = PAGE_SIZE; /* We map only the required part */
1948
1949 if ((chip->iores = request_mem_region(chip->dsp_registers_phys, sz,
1950 ECHOCARD_NAME)) == NULL) {
Takashi Iwaiece7a362014-02-25 16:43:02 +01001951 dev_err(chip->card->dev, "cannot get memory region\n");
Dan Carpenter4f50b412014-03-05 14:07:15 +03001952 snd_echo_free(chip);
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001953 return -EBUSY;
1954 }
1955 chip->dsp_registers = (volatile u32 __iomem *)
1956 ioremap_nocache(chip->dsp_registers_phys, sz);
1957
Takashi Iwai437a5a42006-11-21 12:14:23 +01001958 if (request_irq(pci->irq, snd_echo_interrupt, IRQF_SHARED,
Takashi Iwai934c2b62011-06-10 16:36:37 +02001959 KBUILD_MODNAME, chip)) {
Takashi Iwaiece7a362014-02-25 16:43:02 +01001960 dev_err(chip->card->dev, "cannot grab irq\n");
Dan Carpenter4f50b412014-03-05 14:07:15 +03001961 snd_echo_free(chip);
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001962 return -EBUSY;
1963 }
1964 chip->irq = pci->irq;
Sudip Mukherjeeb5b4a412014-11-03 16:04:13 +05301965 dev_dbg(card->dev, "pci=%p irq=%d subdev=%04x Init hardware...\n",
1966 chip->pci, chip->irq, chip->pci->subsystem_device);
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001967
1968 /* Create the DSP comm page - this is the area of memory used for most
1969 of the communication with the DSP, which accesses it via bus mastering */
1970 if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
1971 sizeof(struct comm_page),
1972 &chip->commpage_dma_buf) < 0) {
Takashi Iwaiece7a362014-02-25 16:43:02 +01001973 dev_err(chip->card->dev, "cannot allocate the comm page\n");
Dan Carpenter4f50b412014-03-05 14:07:15 +03001974 snd_echo_free(chip);
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001975 return -ENOMEM;
1976 }
1977 chip->comm_page_phys = chip->commpage_dma_buf.addr;
1978 chip->comm_page = (struct comm_page *)chip->commpage_dma_buf.area;
1979
1980 err = init_hw(chip, chip->pci->device, chip->pci->subsystem_device);
Giuliano Pochini47b5d022010-02-14 18:16:10 +01001981 if (err >= 0)
1982 err = set_mixer_defaults(chip);
1983 if (err < 0) {
Sudip Mukherjeeb5b4a412014-11-03 16:04:13 +05301984 dev_err(card->dev, "init_hw err=%d\n", err);
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001985 snd_echo_free(chip);
1986 return err;
1987 }
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001988
1989 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
1990 snd_echo_free(chip);
1991 return err;
1992 }
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02001993 *rchip = chip;
1994 /* Init done ! */
1995 return 0;
1996}
1997
1998
1999
2000/* constructor */
Bill Pembertone23e7a12012-12-06 12:35:10 -05002001static int snd_echo_probe(struct pci_dev *pci,
2002 const struct pci_device_id *pci_id)
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02002003{
2004 static int dev;
2005 struct snd_card *card;
2006 struct echoaudio *chip;
2007 char *dsp;
2008 int i, err;
2009
2010 if (dev >= SNDRV_CARDS)
2011 return -ENODEV;
2012 if (!enable[dev]) {
2013 dev++;
2014 return -ENOENT;
2015 }
2016
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02002017 i = 0;
Takashi Iwai60c57722014-01-29 14:20:19 +01002018 err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
2019 0, &card);
Takashi Iwaie58de7b2008-12-28 16:44:30 +01002020 if (err < 0)
2021 return err;
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02002022
Giuliano Pochini47b5d022010-02-14 18:16:10 +01002023 chip = NULL; /* Tells snd_echo_create to allocate chip */
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02002024 if ((err = snd_echo_create(card, pci, &chip)) < 0) {
2025 snd_card_free(card);
2026 return err;
2027 }
2028
2029 strcpy(card->driver, "Echo_" ECHOCARD_NAME);
2030 strcpy(card->shortname, chip->card_name);
2031
2032 dsp = "56301";
2033 if (pci_id->device == 0x3410)
2034 dsp = "56361";
2035
2036 sprintf(card->longname, "%s rev.%d (DSP%s) at 0x%lx irq %i",
2037 card->shortname, pci_id->subdevice & 0x000f, dsp,
2038 chip->dsp_registers_phys, chip->irq);
2039
2040 if ((err = snd_echo_new_pcm(chip)) < 0) {
Takashi Iwaiece7a362014-02-25 16:43:02 +01002041 dev_err(chip->card->dev, "new pcm error %d\n", err);
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02002042 snd_card_free(card);
2043 return err;
2044 }
2045
2046#ifdef ECHOCARD_HAS_MIDI
2047 if (chip->has_midi) { /* Some Mia's do not have midi */
2048 if ((err = snd_echo_midi_create(card, chip)) < 0) {
Takashi Iwaiece7a362014-02-25 16:43:02 +01002049 dev_err(chip->card->dev, "new midi error %d\n", err);
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02002050 snd_card_free(card);
2051 return err;
2052 }
2053 }
2054#endif
2055
2056#ifdef ECHOCARD_HAS_VMIXER
2057 snd_echo_vmixer.count = num_pipes_out(chip) * num_busses_out(chip);
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02002058 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vmixer, chip))) < 0)
2059 goto ctl_error;
Giuliano Pochini392bf2f2009-09-30 08:26:45 +02002060#ifdef ECHOCARD_HAS_LINE_OUT_GAIN
2061 err = snd_ctl_add(chip->card,
2062 snd_ctl_new1(&snd_echo_line_output_gain, chip));
2063 if (err < 0)
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02002064 goto ctl_error;
2065#endif
Giuliano Pochini392bf2f2009-09-30 08:26:45 +02002066#else /* ECHOCARD_HAS_VMIXER */
2067 err = snd_ctl_add(chip->card,
2068 snd_ctl_new1(&snd_echo_pcm_output_gain, chip));
2069 if (err < 0)
2070 goto ctl_error;
2071#endif /* ECHOCARD_HAS_VMIXER */
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02002072
2073#ifdef ECHOCARD_HAS_INPUT_GAIN
2074 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_line_input_gain, chip))) < 0)
2075 goto ctl_error;
2076#endif
2077
2078#ifdef ECHOCARD_HAS_INPUT_NOMINAL_LEVEL
2079 if (!chip->hasnt_input_nominal_level)
2080 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_intput_nominal_level, chip))) < 0)
2081 goto ctl_error;
2082#endif
2083
2084#ifdef ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL
2085 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_output_nominal_level, chip))) < 0)
2086 goto ctl_error;
2087#endif
2088
2089 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vumeters_switch, chip))) < 0)
2090 goto ctl_error;
2091
2092 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vumeters, chip))) < 0)
2093 goto ctl_error;
2094
2095#ifdef ECHOCARD_HAS_MONITOR
2096 snd_echo_monitor_mixer.count = num_busses_in(chip) * num_busses_out(chip);
2097 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_monitor_mixer, chip))) < 0)
2098 goto ctl_error;
2099#endif
2100
2101#ifdef ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE
2102 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_automute_switch, chip))) < 0)
2103 goto ctl_error;
2104#endif
2105
2106 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_channels_info, chip))) < 0)
2107 goto ctl_error;
2108
2109#ifdef ECHOCARD_HAS_DIGITAL_MODE_SWITCH
2110 /* Creates a list of available digital modes */
2111 chip->num_digital_modes = 0;
2112 for (i = 0; i < 6; i++)
2113 if (chip->digital_modes & (1 << i))
2114 chip->digital_mode_list[chip->num_digital_modes++] = i;
2115
2116 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_digital_mode_switch, chip))) < 0)
2117 goto ctl_error;
2118#endif /* ECHOCARD_HAS_DIGITAL_MODE_SWITCH */
2119
2120#ifdef ECHOCARD_HAS_EXTERNAL_CLOCK
2121 /* Creates a list of available clock sources */
2122 chip->num_clock_sources = 0;
2123 for (i = 0; i < 10; i++)
2124 if (chip->input_clock_types & (1 << i))
2125 chip->clock_source_list[chip->num_clock_sources++] = i;
2126
2127 if (chip->num_clock_sources > 1) {
2128 chip->clock_src_ctl = snd_ctl_new1(&snd_echo_clock_source_switch, chip);
2129 if ((err = snd_ctl_add(chip->card, chip->clock_src_ctl)) < 0)
2130 goto ctl_error;
2131 }
2132#endif /* ECHOCARD_HAS_EXTERNAL_CLOCK */
2133
2134#ifdef ECHOCARD_HAS_DIGITAL_IO
2135 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_spdif_mode_switch, chip))) < 0)
2136 goto ctl_error;
2137#endif
2138
2139#ifdef ECHOCARD_HAS_PHANTOM_POWER
2140 if (chip->has_phantom_power)
2141 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_phantom_power_switch, chip))) < 0)
2142 goto ctl_error;
2143#endif
2144
Julia Lawalla0fd4342010-04-02 14:47:59 +02002145 err = snd_card_register(card);
2146 if (err < 0)
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02002147 goto ctl_error;
Takashi Iwaiece7a362014-02-25 16:43:02 +01002148 dev_info(card->dev, "Card registered: %s\n", card->longname);
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02002149
2150 pci_set_drvdata(pci, chip);
2151 dev++;
2152 return 0;
2153
2154ctl_error:
Takashi Iwaiece7a362014-02-25 16:43:02 +01002155 dev_err(card->dev, "new control error %d\n", err);
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02002156 snd_card_free(card);
2157 return err;
2158}
2159
2160
2161
Takashi Iwaic7561cd2012-08-14 18:12:04 +02002162#if defined(CONFIG_PM_SLEEP)
Giuliano Pochini47b5d022010-02-14 18:16:10 +01002163
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002164static int snd_echo_suspend(struct device *dev)
Giuliano Pochini47b5d022010-02-14 18:16:10 +01002165{
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002166 struct echoaudio *chip = dev_get_drvdata(dev);
Giuliano Pochini47b5d022010-02-14 18:16:10 +01002167
Giuliano Pochini47b5d022010-02-14 18:16:10 +01002168 snd_pcm_suspend_all(chip->analog_pcm);
2169 snd_pcm_suspend_all(chip->digital_pcm);
2170
2171#ifdef ECHOCARD_HAS_MIDI
2172 /* This call can sleep */
2173 if (chip->midi_out)
2174 snd_echo_midi_output_trigger(chip->midi_out, 0);
2175#endif
2176 spin_lock_irq(&chip->lock);
2177 if (wait_handshake(chip)) {
2178 spin_unlock_irq(&chip->lock);
2179 return -EIO;
2180 }
2181 clear_handshake(chip);
2182 if (send_vector(chip, DSP_VC_GO_COMATOSE) < 0) {
2183 spin_unlock_irq(&chip->lock);
2184 return -EIO;
2185 }
2186 spin_unlock_irq(&chip->lock);
2187
2188 chip->dsp_code = NULL;
2189 free_irq(chip->irq, chip);
2190 chip->irq = -1;
Giuliano Pochini47b5d022010-02-14 18:16:10 +01002191 return 0;
2192}
2193
2194
2195
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002196static int snd_echo_resume(struct device *dev)
Giuliano Pochini47b5d022010-02-14 18:16:10 +01002197{
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002198 struct pci_dev *pci = to_pci_dev(dev);
2199 struct echoaudio *chip = dev_get_drvdata(dev);
Giuliano Pochini47b5d022010-02-14 18:16:10 +01002200 struct comm_page *commpage, *commpage_bak;
2201 u32 pipe_alloc_mask;
2202 int err;
2203
Christophe JAILLET9c6795a2016-06-27 21:06:51 +02002204 commpage_bak = kmalloc(sizeof(*commpage), GFP_KERNEL);
Kulikov Vasiliy0b6d0922010-07-16 20:15:43 +04002205 if (commpage_bak == NULL)
2206 return -ENOMEM;
Giuliano Pochini47b5d022010-02-14 18:16:10 +01002207 commpage = chip->comm_page;
Christophe JAILLET9c6795a2016-06-27 21:06:51 +02002208 memcpy(commpage_bak, commpage, sizeof(*commpage));
Giuliano Pochini47b5d022010-02-14 18:16:10 +01002209
2210 err = init_hw(chip, chip->pci->device, chip->pci->subsystem_device);
2211 if (err < 0) {
2212 kfree(commpage_bak);
Sudip Mukherjeeb5b4a412014-11-03 16:04:13 +05302213 dev_err(dev, "resume init_hw err=%d\n", err);
Giuliano Pochini47b5d022010-02-14 18:16:10 +01002214 snd_echo_free(chip);
2215 return err;
2216 }
Giuliano Pochini47b5d022010-02-14 18:16:10 +01002217
2218 /* Temporarily set chip->pipe_alloc_mask=0 otherwise
2219 * restore_dsp_settings() fails.
2220 */
2221 pipe_alloc_mask = chip->pipe_alloc_mask;
2222 chip->pipe_alloc_mask = 0;
2223 err = restore_dsp_rettings(chip);
2224 chip->pipe_alloc_mask = pipe_alloc_mask;
2225 if (err < 0) {
2226 kfree(commpage_bak);
2227 return err;
2228 }
Giuliano Pochini47b5d022010-02-14 18:16:10 +01002229
2230 memcpy(&commpage->audio_format, &commpage_bak->audio_format,
2231 sizeof(commpage->audio_format));
2232 memcpy(&commpage->sglist_addr, &commpage_bak->sglist_addr,
2233 sizeof(commpage->sglist_addr));
2234 memcpy(&commpage->midi_output, &commpage_bak->midi_output,
2235 sizeof(commpage->midi_output));
2236 kfree(commpage_bak);
2237
2238 if (request_irq(pci->irq, snd_echo_interrupt, IRQF_SHARED,
Takashi Iwai934c2b62011-06-10 16:36:37 +02002239 KBUILD_MODNAME, chip)) {
Takashi Iwaiece7a362014-02-25 16:43:02 +01002240 dev_err(chip->card->dev, "cannot grab irq\n");
Dan Carpenter4f50b412014-03-05 14:07:15 +03002241 snd_echo_free(chip);
Giuliano Pochini47b5d022010-02-14 18:16:10 +01002242 return -EBUSY;
2243 }
2244 chip->irq = pci->irq;
Sudip Mukherjeeb5b4a412014-11-03 16:04:13 +05302245 dev_dbg(dev, "resume irq=%d\n", chip->irq);
Giuliano Pochini47b5d022010-02-14 18:16:10 +01002246
2247#ifdef ECHOCARD_HAS_MIDI
2248 if (chip->midi_input_enabled)
Mark Brown3f6175e2015-08-10 13:02:53 +01002249 enable_midi_input(chip, true);
Giuliano Pochini47b5d022010-02-14 18:16:10 +01002250 if (chip->midi_out)
2251 snd_echo_midi_output_trigger(chip->midi_out, 1);
2252#endif
2253
Giuliano Pochini47b5d022010-02-14 18:16:10 +01002254 return 0;
2255}
2256
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002257static SIMPLE_DEV_PM_OPS(snd_echo_pm, snd_echo_suspend, snd_echo_resume);
2258#define SND_ECHO_PM_OPS &snd_echo_pm
2259#else
2260#define SND_ECHO_PM_OPS NULL
Takashi Iwaic7561cd2012-08-14 18:12:04 +02002261#endif /* CONFIG_PM_SLEEP */
Giuliano Pochini47b5d022010-02-14 18:16:10 +01002262
2263
Bill Pembertone23e7a12012-12-06 12:35:10 -05002264static void snd_echo_remove(struct pci_dev *pci)
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02002265{
2266 struct echoaudio *chip;
2267
2268 chip = pci_get_drvdata(pci);
2269 if (chip)
2270 snd_card_free(chip->card);
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02002271}
2272
2273
2274
2275/******************************************************************************
2276 Everything starts and ends here
2277******************************************************************************/
2278
2279/* pci_driver definition */
Takashi Iwaie9f66d92012-04-24 12:25:00 +02002280static struct pci_driver echo_driver = {
Takashi Iwai3733e422011-06-10 16:20:20 +02002281 .name = KBUILD_MODNAME,
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02002282 .id_table = snd_echo_ids,
2283 .probe = snd_echo_probe,
Bill Pembertone23e7a12012-12-06 12:35:10 -05002284 .remove = snd_echo_remove,
Takashi Iwai68cb2b52012-07-02 15:20:37 +02002285 .driver = {
2286 .pm = SND_ECHO_PM_OPS,
2287 },
Giuliano Pochinidd7b2542006-06-28 13:53:41 +02002288};
2289
Takashi Iwaie9f66d92012-04-24 12:25:00 +02002290module_pci_driver(echo_driver);