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