ALSA: Echoaudio - Add firmware cache #1
[pandora-kernel.git] / sound / pci / echoaudio / echoaudio.c
1 /*
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
19 MODULE_AUTHOR("Giuliano Pochini <pochini@shiny.it>");
20 MODULE_LICENSE("GPL v2");
21 MODULE_DESCRIPTION("Echoaudio " ECHOCARD_NAME " soundcards driver");
22 MODULE_SUPPORTED_DEVICE("{{Echoaudio," ECHOCARD_NAME "}}");
23 MODULE_DEVICE_TABLE(pci, snd_echo_ids);
24
25 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
26 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
27 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
28
29 module_param_array(index, int, NULL, 0444);
30 MODULE_PARM_DESC(index, "Index value for " ECHOCARD_NAME " soundcard.");
31 module_param_array(id, charp, NULL, 0444);
32 MODULE_PARM_DESC(id, "ID string for " ECHOCARD_NAME " soundcard.");
33 module_param_array(enable, bool, NULL, 0444);
34 MODULE_PARM_DESC(enable, "Enable " ECHOCARD_NAME " soundcard.");
35
36 static unsigned int channels_list[10] = {1, 2, 4, 6, 8, 10, 12, 14, 16, 999999};
37 static const DECLARE_TLV_DB_SCALE(db_scale_output_gain, -12800, 100, 1);
38
39
40
41 static int get_firmware(const struct firmware **fw_entry,
42                         struct echoaudio *chip, const short fw_index)
43 {
44         int err;
45         char name[30];
46         const struct firmware *frm = &card_fw[fw_index];
47
48         DE_ACT(("firmware requested: %s\n", frm->data));
49         snprintf(name, sizeof(name), "ea/%s", frm->data);
50         if ((err = request_firmware(fw_entry, name, pci_device(chip))) < 0)
51                 snd_printk(KERN_ERR "get_firmware(): Firmware not available (%d)\n", err);
52         return err;
53 }
54
55
56
57 static void free_firmware(const struct firmware *fw_entry)
58 {
59         release_firmware(fw_entry);
60         DE_ACT(("firmware released\n"));
61 }
62
63
64
65 /******************************************************************************
66         PCM interface
67 ******************************************************************************/
68
69 static void audiopipe_free(struct snd_pcm_runtime *runtime)
70 {
71         struct audiopipe *pipe = runtime->private_data;
72
73         if (pipe->sgpage.area)
74                 snd_dma_free_pages(&pipe->sgpage);
75         kfree(pipe);
76 }
77
78
79
80 static int hw_rule_capture_format_by_channels(struct snd_pcm_hw_params *params,
81                                               struct snd_pcm_hw_rule *rule)
82 {
83         struct snd_interval *c = hw_param_interval(params,
84                                                    SNDRV_PCM_HW_PARAM_CHANNELS);
85         struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
86         struct snd_mask fmt;
87
88         snd_mask_any(&fmt);
89
90 #ifndef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
91         /* >=2 channels cannot be S32_BE */
92         if (c->min == 2) {
93                 fmt.bits[0] &= ~SNDRV_PCM_FMTBIT_S32_BE;
94                 return snd_mask_refine(f, &fmt);
95         }
96 #endif
97         /* > 2 channels cannot be U8 and S32_BE */
98         if (c->min > 2) {
99                 fmt.bits[0] &= ~(SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S32_BE);
100                 return snd_mask_refine(f, &fmt);
101         }
102         /* Mono is ok with any format */
103         return 0;
104 }
105
106
107
108 static int hw_rule_capture_channels_by_format(struct snd_pcm_hw_params *params,
109                                               struct snd_pcm_hw_rule *rule)
110 {
111         struct snd_interval *c = hw_param_interval(params,
112                                                    SNDRV_PCM_HW_PARAM_CHANNELS);
113         struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
114         struct snd_interval ch;
115
116         snd_interval_any(&ch);
117
118         /* S32_BE is mono (and stereo) only */
119         if (f->bits[0] == SNDRV_PCM_FMTBIT_S32_BE) {
120                 ch.min = 1;
121 #ifdef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
122                 ch.max = 2;
123 #else
124                 ch.max = 1;
125 #endif
126                 ch.integer = 1;
127                 return snd_interval_refine(c, &ch);
128         }
129         /* U8 can be only mono or stereo */
130         if (f->bits[0] == SNDRV_PCM_FMTBIT_U8) {
131                 ch.min = 1;
132                 ch.max = 2;
133                 ch.integer = 1;
134                 return snd_interval_refine(c, &ch);
135         }
136         /* S16_LE, S24_3LE and S32_LE support any number of channels. */
137         return 0;
138 }
139
140
141
142 static int hw_rule_playback_format_by_channels(struct snd_pcm_hw_params *params,
143                                                struct snd_pcm_hw_rule *rule)
144 {
145         struct snd_interval *c = hw_param_interval(params,
146                                                    SNDRV_PCM_HW_PARAM_CHANNELS);
147         struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
148         struct snd_mask fmt;
149         u64 fmask;
150         snd_mask_any(&fmt);
151
152         fmask = fmt.bits[0] + ((u64)fmt.bits[1] << 32);
153
154         /* >2 channels must be S16_LE, S24_3LE or S32_LE */
155         if (c->min > 2) {
156                 fmask &= SNDRV_PCM_FMTBIT_S16_LE |
157                          SNDRV_PCM_FMTBIT_S24_3LE |
158                          SNDRV_PCM_FMTBIT_S32_LE;
159         /* 1 channel must be S32_BE or S32_LE */
160         } else if (c->max == 1)
161                 fmask &= SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE;
162 #ifndef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
163         /* 2 channels cannot be S32_BE */
164         else if (c->min == 2 && c->max == 2)
165                 fmask &= ~SNDRV_PCM_FMTBIT_S32_BE;
166 #endif
167         else
168                 return 0;
169
170         fmt.bits[0] &= (u32)fmask;
171         fmt.bits[1] &= (u32)(fmask >> 32);
172         return snd_mask_refine(f, &fmt);
173 }
174
175
176
177 static int hw_rule_playback_channels_by_format(struct snd_pcm_hw_params *params,
178                                                struct snd_pcm_hw_rule *rule)
179 {
180         struct snd_interval *c = hw_param_interval(params,
181                                                    SNDRV_PCM_HW_PARAM_CHANNELS);
182         struct snd_mask *f = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
183         struct snd_interval ch;
184         u64 fmask;
185
186         snd_interval_any(&ch);
187         ch.integer = 1;
188         fmask = f->bits[0] + ((u64)f->bits[1] << 32);
189
190         /* S32_BE is mono (and stereo) only */
191         if (fmask == SNDRV_PCM_FMTBIT_S32_BE) {
192                 ch.min = 1;
193 #ifdef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
194                 ch.max = 2;
195 #else
196                 ch.max = 1;
197 #endif
198         /* U8 is stereo only */
199         } else if (fmask == SNDRV_PCM_FMTBIT_U8)
200                 ch.min = ch.max = 2;
201         /* S16_LE and S24_3LE must be at least stereo */
202         else if (!(fmask & ~(SNDRV_PCM_FMTBIT_S16_LE |
203                                SNDRV_PCM_FMTBIT_S24_3LE)))
204                 ch.min = 2;
205         else
206                 return 0;
207
208         return snd_interval_refine(c, &ch);
209 }
210
211
212
213 /* Since the sample rate is a global setting, do allow the user to change the
214 sample rate only if there is only one pcm device open. */
215 static int hw_rule_sample_rate(struct snd_pcm_hw_params *params,
216                                struct snd_pcm_hw_rule *rule)
217 {
218         struct snd_interval *rate = hw_param_interval(params,
219                                                       SNDRV_PCM_HW_PARAM_RATE);
220         struct echoaudio *chip = rule->private;
221         struct snd_interval fixed;
222
223         if (!chip->can_set_rate) {
224                 snd_interval_any(&fixed);
225                 fixed.min = fixed.max = chip->sample_rate;
226                 return snd_interval_refine(rate, &fixed);
227         }
228         return 0;
229 }
230
231
232 static int pcm_open(struct snd_pcm_substream *substream,
233                     signed char max_channels)
234 {
235         struct echoaudio *chip;
236         struct snd_pcm_runtime *runtime;
237         struct audiopipe *pipe;
238         int err, i;
239
240         if (max_channels <= 0)
241                 return -EAGAIN;
242
243         chip = snd_pcm_substream_chip(substream);
244         runtime = substream->runtime;
245
246         pipe = kzalloc(sizeof(struct audiopipe), GFP_KERNEL);
247         if (!pipe)
248                 return -ENOMEM;
249         pipe->index = -1;               /* Not configured yet */
250
251         /* Set up hw capabilities and contraints */
252         memcpy(&pipe->hw, &pcm_hardware_skel, sizeof(struct snd_pcm_hardware));
253         DE_HWP(("max_channels=%d\n", max_channels));
254         pipe->constr.list = channels_list;
255         pipe->constr.mask = 0;
256         for (i = 0; channels_list[i] <= max_channels; i++);
257         pipe->constr.count = i;
258         if (pipe->hw.channels_max > max_channels)
259                 pipe->hw.channels_max = max_channels;
260         if (chip->digital_mode == DIGITAL_MODE_ADAT) {
261                 pipe->hw.rate_max = 48000;
262                 pipe->hw.rates &= SNDRV_PCM_RATE_8000_48000;
263         }
264
265         runtime->hw = pipe->hw;
266         runtime->private_data = pipe;
267         runtime->private_free = audiopipe_free;
268         snd_pcm_set_sync(substream);
269
270         /* Only mono and any even number of channels are allowed */
271         if ((err = snd_pcm_hw_constraint_list(runtime, 0,
272                                               SNDRV_PCM_HW_PARAM_CHANNELS,
273                                               &pipe->constr)) < 0)
274                 return err;
275
276         /* All periods should have the same size */
277         if ((err = snd_pcm_hw_constraint_integer(runtime,
278                                                  SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
279                 return err;
280
281         /* The hw accesses memory in chunks 32 frames long and they should be
282         32-bytes-aligned. It's not a requirement, but it seems that IRQs are
283         generated with a resolution of 32 frames. Thus we need the following */
284         if ((err = snd_pcm_hw_constraint_step(runtime, 0,
285                                               SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
286                                               32)) < 0)
287                 return err;
288         if ((err = snd_pcm_hw_constraint_step(runtime, 0,
289                                               SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
290                                               32)) < 0)
291                 return err;
292
293         if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
294                                        SNDRV_PCM_HW_PARAM_RATE,
295                                         hw_rule_sample_rate, chip,
296                                        SNDRV_PCM_HW_PARAM_RATE, -1)) < 0)
297                 return err;
298
299         /* Finally allocate a page for the scatter-gather list */
300         if ((err = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV,
301                                        snd_dma_pci_data(chip->pci),
302                                        PAGE_SIZE, &pipe->sgpage)) < 0) {
303                 DE_HWP(("s-g list allocation failed\n"));
304                 return err;
305         }
306
307         return 0;
308 }
309
310
311
312 static int pcm_analog_in_open(struct snd_pcm_substream *substream)
313 {
314         struct echoaudio *chip = snd_pcm_substream_chip(substream);
315         int err;
316
317         DE_ACT(("pcm_analog_in_open\n"));
318         if ((err = pcm_open(substream, num_analog_busses_in(chip) -
319                             substream->number)) < 0)
320                 return err;
321         if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
322                                        SNDRV_PCM_HW_PARAM_CHANNELS,
323                                        hw_rule_capture_channels_by_format, NULL,
324                                        SNDRV_PCM_HW_PARAM_FORMAT, -1)) < 0)
325                 return err;
326         if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
327                                        SNDRV_PCM_HW_PARAM_FORMAT,
328                                        hw_rule_capture_format_by_channels, NULL,
329                                        SNDRV_PCM_HW_PARAM_CHANNELS, -1)) < 0)
330                 return err;
331         atomic_inc(&chip->opencount);
332         if (atomic_read(&chip->opencount) > 1 && chip->rate_set)
333                 chip->can_set_rate=0;
334         DE_HWP(("pcm_analog_in_open  cs=%d  oc=%d  r=%d\n",
335                 chip->can_set_rate, atomic_read(&chip->opencount),
336                 chip->sample_rate));
337         return 0;
338 }
339
340
341
342 static int pcm_analog_out_open(struct snd_pcm_substream *substream)
343 {
344         struct echoaudio *chip = snd_pcm_substream_chip(substream);
345         int max_channels, err;
346
347 #ifdef ECHOCARD_HAS_VMIXER
348         max_channels = num_pipes_out(chip);
349 #else
350         max_channels = num_analog_busses_out(chip);
351 #endif
352         DE_ACT(("pcm_analog_out_open\n"));
353         if ((err = pcm_open(substream, max_channels - substream->number)) < 0)
354                 return err;
355         if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
356                                        SNDRV_PCM_HW_PARAM_CHANNELS,
357                                        hw_rule_playback_channels_by_format,
358                                        NULL,
359                                        SNDRV_PCM_HW_PARAM_FORMAT, -1)) < 0)
360                 return err;
361         if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
362                                        SNDRV_PCM_HW_PARAM_FORMAT,
363                                        hw_rule_playback_format_by_channels,
364                                        NULL,
365                                        SNDRV_PCM_HW_PARAM_CHANNELS, -1)) < 0)
366                 return err;
367         atomic_inc(&chip->opencount);
368         if (atomic_read(&chip->opencount) > 1 && chip->rate_set)
369                 chip->can_set_rate=0;
370         DE_HWP(("pcm_analog_out_open  cs=%d  oc=%d  r=%d\n",
371                 chip->can_set_rate, atomic_read(&chip->opencount),
372                 chip->sample_rate));
373         return 0;
374 }
375
376
377
378 #ifdef ECHOCARD_HAS_DIGITAL_IO
379
380 static int pcm_digital_in_open(struct snd_pcm_substream *substream)
381 {
382         struct echoaudio *chip = snd_pcm_substream_chip(substream);
383         int err, max_channels;
384
385         DE_ACT(("pcm_digital_in_open\n"));
386         max_channels = num_digital_busses_in(chip) - substream->number;
387         mutex_lock(&chip->mode_mutex);
388         if (chip->digital_mode == DIGITAL_MODE_ADAT)
389                 err = pcm_open(substream, max_channels);
390         else    /* If the card has ADAT, subtract the 6 channels
391                  * that S/PDIF doesn't have
392                  */
393                 err = pcm_open(substream, max_channels - ECHOCARD_HAS_ADAT);
394
395         if (err < 0)
396                 goto din_exit;
397
398         if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
399                                        SNDRV_PCM_HW_PARAM_CHANNELS,
400                                        hw_rule_capture_channels_by_format, NULL,
401                                        SNDRV_PCM_HW_PARAM_FORMAT, -1)) < 0)
402                 goto din_exit;
403         if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
404                                        SNDRV_PCM_HW_PARAM_FORMAT,
405                                        hw_rule_capture_format_by_channels, NULL,
406                                        SNDRV_PCM_HW_PARAM_CHANNELS, -1)) < 0)
407                 goto din_exit;
408
409         atomic_inc(&chip->opencount);
410         if (atomic_read(&chip->opencount) > 1 && chip->rate_set)
411                 chip->can_set_rate=0;
412
413 din_exit:
414         mutex_unlock(&chip->mode_mutex);
415         return err;
416 }
417
418
419
420 #ifndef ECHOCARD_HAS_VMIXER     /* See the note in snd_echo_new_pcm() */
421
422 static int pcm_digital_out_open(struct snd_pcm_substream *substream)
423 {
424         struct echoaudio *chip = snd_pcm_substream_chip(substream);
425         int err, max_channels;
426
427         DE_ACT(("pcm_digital_out_open\n"));
428         max_channels = num_digital_busses_out(chip) - substream->number;
429         mutex_lock(&chip->mode_mutex);
430         if (chip->digital_mode == DIGITAL_MODE_ADAT)
431                 err = pcm_open(substream, max_channels);
432         else    /* If the card has ADAT, subtract the 6 channels
433                  * that S/PDIF doesn't have
434                  */
435                 err = pcm_open(substream, max_channels - ECHOCARD_HAS_ADAT);
436
437         if (err < 0)
438                 goto dout_exit;
439
440         if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
441                                        SNDRV_PCM_HW_PARAM_CHANNELS,
442                                        hw_rule_playback_channels_by_format,
443                                        NULL, SNDRV_PCM_HW_PARAM_FORMAT,
444                                        -1)) < 0)
445                 goto dout_exit;
446         if ((err = snd_pcm_hw_rule_add(substream->runtime, 0,
447                                        SNDRV_PCM_HW_PARAM_FORMAT,
448                                        hw_rule_playback_format_by_channels,
449                                        NULL, SNDRV_PCM_HW_PARAM_CHANNELS,
450                                        -1)) < 0)
451                 goto dout_exit;
452         atomic_inc(&chip->opencount);
453         if (atomic_read(&chip->opencount) > 1 && chip->rate_set)
454                 chip->can_set_rate=0;
455 dout_exit:
456         mutex_unlock(&chip->mode_mutex);
457         return err;
458 }
459
460 #endif /* !ECHOCARD_HAS_VMIXER */
461
462 #endif /* ECHOCARD_HAS_DIGITAL_IO */
463
464
465
466 static int pcm_close(struct snd_pcm_substream *substream)
467 {
468         struct echoaudio *chip = snd_pcm_substream_chip(substream);
469         int oc;
470
471         /* Nothing to do here. Audio is already off and pipe will be
472          * freed by its callback
473          */
474         DE_ACT(("pcm_close\n"));
475
476         atomic_dec(&chip->opencount);
477         oc = atomic_read(&chip->opencount);
478         DE_ACT(("pcm_close  oc=%d  cs=%d  rs=%d\n", oc,
479                 chip->can_set_rate, chip->rate_set));
480         if (oc < 2)
481                 chip->can_set_rate = 1;
482         if (oc == 0)
483                 chip->rate_set = 0;
484         DE_ACT(("pcm_close2 oc=%d  cs=%d  rs=%d\n", oc,
485                 chip->can_set_rate,chip->rate_set));
486
487         return 0;
488 }
489
490
491
492 /* Channel allocation and scatter-gather list setup */
493 static int init_engine(struct snd_pcm_substream *substream,
494                        struct snd_pcm_hw_params *hw_params,
495                        int pipe_index, int interleave)
496 {
497         struct echoaudio *chip;
498         int err, per, rest, page, edge, offs;
499         struct audiopipe *pipe;
500
501         chip = snd_pcm_substream_chip(substream);
502         pipe = (struct audiopipe *) substream->runtime->private_data;
503
504         /* Sets up che hardware. If it's already initialized, reset and
505          * redo with the new parameters
506          */
507         spin_lock_irq(&chip->lock);
508         if (pipe->index >= 0) {
509                 DE_HWP(("hwp_ie free(%d)\n", pipe->index));
510                 err = free_pipes(chip, pipe);
511                 snd_BUG_ON(err);
512                 chip->substream[pipe->index] = NULL;
513         }
514
515         err = allocate_pipes(chip, pipe, pipe_index, interleave);
516         if (err < 0) {
517                 spin_unlock_irq(&chip->lock);
518                 DE_ACT((KERN_NOTICE "allocate_pipes(%d) err=%d\n",
519                         pipe_index, err));
520                 return err;
521         }
522         spin_unlock_irq(&chip->lock);
523         DE_ACT((KERN_NOTICE "allocate_pipes()=%d\n", pipe_index));
524
525         DE_HWP(("pcm_hw_params (bufsize=%dB periods=%d persize=%dB)\n",
526                 params_buffer_bytes(hw_params), params_periods(hw_params),
527                 params_period_bytes(hw_params)));
528         err = snd_pcm_lib_malloc_pages(substream,
529                                        params_buffer_bytes(hw_params));
530         if (err < 0) {
531                 snd_printk(KERN_ERR "malloc_pages err=%d\n", err);
532                 spin_lock_irq(&chip->lock);
533                 free_pipes(chip, pipe);
534                 spin_unlock_irq(&chip->lock);
535                 pipe->index = -1;
536                 return err;
537         }
538
539         sglist_init(chip, pipe);
540         edge = PAGE_SIZE;
541         for (offs = page = per = 0; offs < params_buffer_bytes(hw_params);
542              per++) {
543                 rest = params_period_bytes(hw_params);
544                 if (offs + rest > params_buffer_bytes(hw_params))
545                         rest = params_buffer_bytes(hw_params) - offs;
546                 while (rest) {
547                         dma_addr_t addr;
548                         addr = snd_pcm_sgbuf_get_addr(substream, offs);
549                         if (rest <= edge - offs) {
550                                 sglist_add_mapping(chip, pipe, addr, rest);
551                                 sglist_add_irq(chip, pipe);
552                                 offs += rest;
553                                 rest = 0;
554                         } else {
555                                 sglist_add_mapping(chip, pipe, addr,
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
588 static 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
599 static 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
610 static 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() */
622 static 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
636 static 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
660 static 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
693         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;
697         set_audio_format(chip, pipe_index, &format);
698         return 0;
699 }
700
701
702
703 static 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;
710         struct snd_pcm_substream *s;
711
712         snd_pcm_group_for_each_entry(s, substream) {
713                 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
775 static 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 */
797 static 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 };
808 static 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
821 static 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 */
833 static 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  */
851 static 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() */
871 static 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 #if !defined(ECHOCARD_HAS_VMIXER) || defined(ECHOCARD_HAS_LINE_OUT_GAIN)
960
961 /******************* PCM output volume *******************/
962 static int snd_echo_output_gain_info(struct snd_kcontrol *kcontrol,
963                                      struct snd_ctl_elem_info *uinfo)
964 {
965         struct echoaudio *chip;
966
967         chip = snd_kcontrol_chip(kcontrol);
968         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
969         uinfo->count = num_busses_out(chip);
970         uinfo->value.integer.min = ECHOGAIN_MINOUT;
971         uinfo->value.integer.max = ECHOGAIN_MAXOUT;
972         return 0;
973 }
974
975 static int snd_echo_output_gain_get(struct snd_kcontrol *kcontrol,
976                                     struct snd_ctl_elem_value *ucontrol)
977 {
978         struct echoaudio *chip;
979         int c;
980
981         chip = snd_kcontrol_chip(kcontrol);
982         for (c = 0; c < num_busses_out(chip); c++)
983                 ucontrol->value.integer.value[c] = chip->output_gain[c];
984         return 0;
985 }
986
987 static int snd_echo_output_gain_put(struct snd_kcontrol *kcontrol,
988                                     struct snd_ctl_elem_value *ucontrol)
989 {
990         struct echoaudio *chip;
991         int c, changed, gain;
992
993         changed = 0;
994         chip = snd_kcontrol_chip(kcontrol);
995         spin_lock_irq(&chip->lock);
996         for (c = 0; c < num_busses_out(chip); c++) {
997                 gain = ucontrol->value.integer.value[c];
998                 /* Ignore out of range values */
999                 if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT)
1000                         continue;
1001                 if (chip->output_gain[c] != gain) {
1002                         set_output_gain(chip, c, gain);
1003                         changed = 1;
1004                 }
1005         }
1006         if (changed)
1007                 update_output_line_level(chip);
1008         spin_unlock_irq(&chip->lock);
1009         return changed;
1010 }
1011
1012 #ifdef ECHOCARD_HAS_LINE_OUT_GAIN
1013 /* On the Mia this one controls the line-out volume */
1014 static struct snd_kcontrol_new snd_echo_line_output_gain __devinitdata = {
1015         .name = "Line Playback Volume",
1016         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1017         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1018                   SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1019         .info = snd_echo_output_gain_info,
1020         .get = snd_echo_output_gain_get,
1021         .put = snd_echo_output_gain_put,
1022         .tlv = {.p = db_scale_output_gain},
1023 };
1024 #else
1025 static struct snd_kcontrol_new snd_echo_pcm_output_gain __devinitdata = {
1026         .name = "PCM Playback Volume",
1027         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1028         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1029         .info = snd_echo_output_gain_info,
1030         .get = snd_echo_output_gain_get,
1031         .put = snd_echo_output_gain_put,
1032         .tlv = {.p = db_scale_output_gain},
1033 };
1034 #endif
1035
1036 #endif /* !ECHOCARD_HAS_VMIXER || ECHOCARD_HAS_LINE_OUT_GAIN */
1037
1038
1039
1040 #ifdef ECHOCARD_HAS_INPUT_GAIN
1041
1042 /******************* Analog input volume *******************/
1043 static int snd_echo_input_gain_info(struct snd_kcontrol *kcontrol,
1044                                     struct snd_ctl_elem_info *uinfo)
1045 {
1046         struct echoaudio *chip;
1047
1048         chip = snd_kcontrol_chip(kcontrol);
1049         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1050         uinfo->count = num_analog_busses_in(chip);
1051         uinfo->value.integer.min = ECHOGAIN_MININP;
1052         uinfo->value.integer.max = ECHOGAIN_MAXINP;
1053         return 0;
1054 }
1055
1056 static int snd_echo_input_gain_get(struct snd_kcontrol *kcontrol,
1057                                    struct snd_ctl_elem_value *ucontrol)
1058 {
1059         struct echoaudio *chip;
1060         int c;
1061
1062         chip = snd_kcontrol_chip(kcontrol);
1063         for (c = 0; c < num_analog_busses_in(chip); c++)
1064                 ucontrol->value.integer.value[c] = chip->input_gain[c];
1065         return 0;
1066 }
1067
1068 static int snd_echo_input_gain_put(struct snd_kcontrol *kcontrol,
1069                                    struct snd_ctl_elem_value *ucontrol)
1070 {
1071         struct echoaudio *chip;
1072         int c, gain, changed;
1073
1074         changed = 0;
1075         chip = snd_kcontrol_chip(kcontrol);
1076         spin_lock_irq(&chip->lock);
1077         for (c = 0; c < num_analog_busses_in(chip); c++) {
1078                 gain = ucontrol->value.integer.value[c];
1079                 /* Ignore out of range values */
1080                 if (gain < ECHOGAIN_MININP || gain > ECHOGAIN_MAXINP)
1081                         continue;
1082                 if (chip->input_gain[c] != gain) {
1083                         set_input_gain(chip, c, gain);
1084                         changed = 1;
1085                 }
1086         }
1087         if (changed)
1088                 update_input_line_level(chip);
1089         spin_unlock_irq(&chip->lock);
1090         return changed;
1091 }
1092
1093 static const DECLARE_TLV_DB_SCALE(db_scale_input_gain, -2500, 50, 0);
1094
1095 static struct snd_kcontrol_new snd_echo_line_input_gain __devinitdata = {
1096         .name = "Line Capture Volume",
1097         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1098         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1099         .info = snd_echo_input_gain_info,
1100         .get = snd_echo_input_gain_get,
1101         .put = snd_echo_input_gain_put,
1102         .tlv = {.p = db_scale_input_gain},
1103 };
1104
1105 #endif /* ECHOCARD_HAS_INPUT_GAIN */
1106
1107
1108
1109 #ifdef ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL
1110
1111 /************ Analog output nominal level (+4dBu / -10dBV) ***************/
1112 static int snd_echo_output_nominal_info (struct snd_kcontrol *kcontrol,
1113                                          struct snd_ctl_elem_info *uinfo)
1114 {
1115         struct echoaudio *chip;
1116
1117         chip = snd_kcontrol_chip(kcontrol);
1118         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1119         uinfo->count = num_analog_busses_out(chip);
1120         uinfo->value.integer.min = 0;
1121         uinfo->value.integer.max = 1;
1122         return 0;
1123 }
1124
1125 static int snd_echo_output_nominal_get(struct snd_kcontrol *kcontrol,
1126                                        struct snd_ctl_elem_value *ucontrol)
1127 {
1128         struct echoaudio *chip;
1129         int c;
1130
1131         chip = snd_kcontrol_chip(kcontrol);
1132         for (c = 0; c < num_analog_busses_out(chip); c++)
1133                 ucontrol->value.integer.value[c] = chip->nominal_level[c];
1134         return 0;
1135 }
1136
1137 static int snd_echo_output_nominal_put(struct snd_kcontrol *kcontrol,
1138                                        struct snd_ctl_elem_value *ucontrol)
1139 {
1140         struct echoaudio *chip;
1141         int c, changed;
1142
1143         changed = 0;
1144         chip = snd_kcontrol_chip(kcontrol);
1145         spin_lock_irq(&chip->lock);
1146         for (c = 0; c < num_analog_busses_out(chip); c++) {
1147                 if (chip->nominal_level[c] != ucontrol->value.integer.value[c]) {
1148                         set_nominal_level(chip, c,
1149                                           ucontrol->value.integer.value[c]);
1150                         changed = 1;
1151                 }
1152         }
1153         if (changed)
1154                 update_output_line_level(chip);
1155         spin_unlock_irq(&chip->lock);
1156         return changed;
1157 }
1158
1159 static struct snd_kcontrol_new snd_echo_output_nominal_level __devinitdata = {
1160         .name = "Line Playback Switch (-10dBV)",
1161         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1162         .info = snd_echo_output_nominal_info,
1163         .get = snd_echo_output_nominal_get,
1164         .put = snd_echo_output_nominal_put,
1165 };
1166
1167 #endif /* ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL */
1168
1169
1170
1171 #ifdef ECHOCARD_HAS_INPUT_NOMINAL_LEVEL
1172
1173 /*************** Analog input nominal level (+4dBu / -10dBV) ***************/
1174 static int snd_echo_input_nominal_info(struct snd_kcontrol *kcontrol,
1175                                        struct snd_ctl_elem_info *uinfo)
1176 {
1177         struct echoaudio *chip;
1178
1179         chip = snd_kcontrol_chip(kcontrol);
1180         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1181         uinfo->count = num_analog_busses_in(chip);
1182         uinfo->value.integer.min = 0;
1183         uinfo->value.integer.max = 1;
1184         return 0;
1185 }
1186
1187 static int snd_echo_input_nominal_get(struct snd_kcontrol *kcontrol,
1188                                       struct snd_ctl_elem_value *ucontrol)
1189 {
1190         struct echoaudio *chip;
1191         int c;
1192
1193         chip = snd_kcontrol_chip(kcontrol);
1194         for (c = 0; c < num_analog_busses_in(chip); c++)
1195                 ucontrol->value.integer.value[c] =
1196                         chip->nominal_level[bx_analog_in(chip) + c];
1197         return 0;
1198 }
1199
1200 static int snd_echo_input_nominal_put(struct snd_kcontrol *kcontrol,
1201                                       struct snd_ctl_elem_value *ucontrol)
1202 {
1203         struct echoaudio *chip;
1204         int c, changed;
1205
1206         changed = 0;
1207         chip = snd_kcontrol_chip(kcontrol);
1208         spin_lock_irq(&chip->lock);
1209         for (c = 0; c < num_analog_busses_in(chip); c++) {
1210                 if (chip->nominal_level[bx_analog_in(chip) + c] !=
1211                     ucontrol->value.integer.value[c]) {
1212                         set_nominal_level(chip, bx_analog_in(chip) + c,
1213                                           ucontrol->value.integer.value[c]);
1214                         changed = 1;
1215                 }
1216         }
1217         if (changed)
1218                 update_output_line_level(chip); /* "Output" is not a mistake
1219                                                  * here.
1220                                                  */
1221         spin_unlock_irq(&chip->lock);
1222         return changed;
1223 }
1224
1225 static struct snd_kcontrol_new snd_echo_intput_nominal_level __devinitdata = {
1226         .name = "Line Capture Switch (-10dBV)",
1227         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1228         .info = snd_echo_input_nominal_info,
1229         .get = snd_echo_input_nominal_get,
1230         .put = snd_echo_input_nominal_put,
1231 };
1232
1233 #endif /* ECHOCARD_HAS_INPUT_NOMINAL_LEVEL */
1234
1235
1236
1237 #ifdef ECHOCARD_HAS_MONITOR
1238
1239 /******************* Monitor mixer *******************/
1240 static int snd_echo_mixer_info(struct snd_kcontrol *kcontrol,
1241                                struct snd_ctl_elem_info *uinfo)
1242 {
1243         struct echoaudio *chip;
1244
1245         chip = snd_kcontrol_chip(kcontrol);
1246         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1247         uinfo->count = 1;
1248         uinfo->value.integer.min = ECHOGAIN_MINOUT;
1249         uinfo->value.integer.max = ECHOGAIN_MAXOUT;
1250         uinfo->dimen.d[0] = num_busses_out(chip);
1251         uinfo->dimen.d[1] = num_busses_in(chip);
1252         return 0;
1253 }
1254
1255 static int snd_echo_mixer_get(struct snd_kcontrol *kcontrol,
1256                               struct snd_ctl_elem_value *ucontrol)
1257 {
1258         struct echoaudio *chip;
1259
1260         chip = snd_kcontrol_chip(kcontrol);
1261         ucontrol->value.integer.value[0] =
1262                 chip->monitor_gain[ucontrol->id.index / num_busses_in(chip)]
1263                         [ucontrol->id.index % num_busses_in(chip)];
1264         return 0;
1265 }
1266
1267 static int snd_echo_mixer_put(struct snd_kcontrol *kcontrol,
1268                               struct snd_ctl_elem_value *ucontrol)
1269 {
1270         struct echoaudio *chip;
1271         int changed,  gain;
1272         short out, in;
1273
1274         changed = 0;
1275         chip = snd_kcontrol_chip(kcontrol);
1276         out = ucontrol->id.index / num_busses_in(chip);
1277         in = ucontrol->id.index % num_busses_in(chip);
1278         gain = ucontrol->value.integer.value[0];
1279         if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT)
1280                 return -EINVAL;
1281         if (chip->monitor_gain[out][in] != gain) {
1282                 spin_lock_irq(&chip->lock);
1283                 set_monitor_gain(chip, out, in, gain);
1284                 update_output_line_level(chip);
1285                 spin_unlock_irq(&chip->lock);
1286                 changed = 1;
1287         }
1288         return changed;
1289 }
1290
1291 static struct snd_kcontrol_new snd_echo_monitor_mixer __devinitdata = {
1292         .name = "Monitor Mixer Volume",
1293         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1294         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1295         .info = snd_echo_mixer_info,
1296         .get = snd_echo_mixer_get,
1297         .put = snd_echo_mixer_put,
1298         .tlv = {.p = db_scale_output_gain},
1299 };
1300
1301 #endif /* ECHOCARD_HAS_MONITOR */
1302
1303
1304
1305 #ifdef ECHOCARD_HAS_VMIXER
1306
1307 /******************* Vmixer *******************/
1308 static int snd_echo_vmixer_info(struct snd_kcontrol *kcontrol,
1309                                 struct snd_ctl_elem_info *uinfo)
1310 {
1311         struct echoaudio *chip;
1312
1313         chip = snd_kcontrol_chip(kcontrol);
1314         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1315         uinfo->count = 1;
1316         uinfo->value.integer.min = ECHOGAIN_MINOUT;
1317         uinfo->value.integer.max = ECHOGAIN_MAXOUT;
1318         uinfo->dimen.d[0] = num_busses_out(chip);
1319         uinfo->dimen.d[1] = num_pipes_out(chip);
1320         return 0;
1321 }
1322
1323 static int snd_echo_vmixer_get(struct snd_kcontrol *kcontrol,
1324                                struct snd_ctl_elem_value *ucontrol)
1325 {
1326         struct echoaudio *chip;
1327
1328         chip = snd_kcontrol_chip(kcontrol);
1329         ucontrol->value.integer.value[0] =
1330                 chip->vmixer_gain[ucontrol->id.index / num_pipes_out(chip)]
1331                         [ucontrol->id.index % num_pipes_out(chip)];
1332         return 0;
1333 }
1334
1335 static int snd_echo_vmixer_put(struct snd_kcontrol *kcontrol,
1336                                struct snd_ctl_elem_value *ucontrol)
1337 {
1338         struct echoaudio *chip;
1339         int gain, changed;
1340         short vch, out;
1341
1342         changed = 0;
1343         chip = snd_kcontrol_chip(kcontrol);
1344         out = ucontrol->id.index / num_pipes_out(chip);
1345         vch = ucontrol->id.index % num_pipes_out(chip);
1346         gain = ucontrol->value.integer.value[0];
1347         if (gain < ECHOGAIN_MINOUT || gain > ECHOGAIN_MAXOUT)
1348                 return -EINVAL;
1349         if (chip->vmixer_gain[out][vch] != ucontrol->value.integer.value[0]) {
1350                 spin_lock_irq(&chip->lock);
1351                 set_vmixer_gain(chip, out, vch, ucontrol->value.integer.value[0]);
1352                 update_vmixer_level(chip);
1353                 spin_unlock_irq(&chip->lock);
1354                 changed = 1;
1355         }
1356         return changed;
1357 }
1358
1359 static struct snd_kcontrol_new snd_echo_vmixer __devinitdata = {
1360         .name = "VMixer Volume",
1361         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1362         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1363         .info = snd_echo_vmixer_info,
1364         .get = snd_echo_vmixer_get,
1365         .put = snd_echo_vmixer_put,
1366         .tlv = {.p = db_scale_output_gain},
1367 };
1368
1369 #endif /* ECHOCARD_HAS_VMIXER */
1370
1371
1372
1373 #ifdef ECHOCARD_HAS_DIGITAL_MODE_SWITCH
1374
1375 /******************* Digital mode switch *******************/
1376 static int snd_echo_digital_mode_info(struct snd_kcontrol *kcontrol,
1377                                       struct snd_ctl_elem_info *uinfo)
1378 {
1379         static char *names[4] = {
1380                 "S/PDIF Coaxial", "S/PDIF Optical", "ADAT Optical",
1381                 "S/PDIF Cdrom"
1382         };
1383         struct echoaudio *chip;
1384
1385         chip = snd_kcontrol_chip(kcontrol);
1386         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1387         uinfo->value.enumerated.items = chip->num_digital_modes;
1388         uinfo->count = 1;
1389         if (uinfo->value.enumerated.item >= chip->num_digital_modes)
1390                 uinfo->value.enumerated.item = chip->num_digital_modes - 1;
1391         strcpy(uinfo->value.enumerated.name, names[
1392                         chip->digital_mode_list[uinfo->value.enumerated.item]]);
1393         return 0;
1394 }
1395
1396 static int snd_echo_digital_mode_get(struct snd_kcontrol *kcontrol,
1397                                      struct snd_ctl_elem_value *ucontrol)
1398 {
1399         struct echoaudio *chip;
1400         int i, mode;
1401
1402         chip = snd_kcontrol_chip(kcontrol);
1403         mode = chip->digital_mode;
1404         for (i = chip->num_digital_modes - 1; i >= 0; i--)
1405                 if (mode == chip->digital_mode_list[i]) {
1406                         ucontrol->value.enumerated.item[0] = i;
1407                         break;
1408                 }
1409         return 0;
1410 }
1411
1412 static int snd_echo_digital_mode_put(struct snd_kcontrol *kcontrol,
1413                                      struct snd_ctl_elem_value *ucontrol)
1414 {
1415         struct echoaudio *chip;
1416         int changed;
1417         unsigned short emode, dmode;
1418
1419         changed = 0;
1420         chip = snd_kcontrol_chip(kcontrol);
1421
1422         emode = ucontrol->value.enumerated.item[0];
1423         if (emode >= chip->num_digital_modes)
1424                 return -EINVAL;
1425         dmode = chip->digital_mode_list[emode];
1426
1427         if (dmode != chip->digital_mode) {
1428                 /* mode_mutex is required to make this operation atomic wrt
1429                 pcm_digital_*_open() and set_input_clock() functions. */
1430                 mutex_lock(&chip->mode_mutex);
1431
1432                 /* Do not allow the user to change the digital mode when a pcm
1433                 device is open because it also changes the number of channels
1434                 and the allowed sample rates */
1435                 if (atomic_read(&chip->opencount)) {
1436                         changed = -EAGAIN;
1437                 } else {
1438                         changed = set_digital_mode(chip, dmode);
1439                         /* If we had to change the clock source, report it */
1440                         if (changed > 0 && chip->clock_src_ctl) {
1441                                 snd_ctl_notify(chip->card,
1442                                                SNDRV_CTL_EVENT_MASK_VALUE,
1443                                                &chip->clock_src_ctl->id);
1444                                 DE_ACT(("SDM() =%d\n", changed));
1445                         }
1446                         if (changed >= 0)
1447                                 changed = 1;    /* No errors */
1448                 }
1449                 mutex_unlock(&chip->mode_mutex);
1450         }
1451         return changed;
1452 }
1453
1454 static struct snd_kcontrol_new snd_echo_digital_mode_switch __devinitdata = {
1455         .name = "Digital mode Switch",
1456         .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1457         .info = snd_echo_digital_mode_info,
1458         .get = snd_echo_digital_mode_get,
1459         .put = snd_echo_digital_mode_put,
1460 };
1461
1462 #endif /* ECHOCARD_HAS_DIGITAL_MODE_SWITCH */
1463
1464
1465
1466 #ifdef ECHOCARD_HAS_DIGITAL_IO
1467
1468 /******************* S/PDIF mode switch *******************/
1469 static int snd_echo_spdif_mode_info(struct snd_kcontrol *kcontrol,
1470                                     struct snd_ctl_elem_info *uinfo)
1471 {
1472         static char *names[2] = {"Consumer", "Professional"};
1473
1474         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1475         uinfo->value.enumerated.items = 2;
1476         uinfo->count = 1;
1477         if (uinfo->value.enumerated.item)
1478                 uinfo->value.enumerated.item = 1;
1479         strcpy(uinfo->value.enumerated.name,
1480                names[uinfo->value.enumerated.item]);
1481         return 0;
1482 }
1483
1484 static int snd_echo_spdif_mode_get(struct snd_kcontrol *kcontrol,
1485                                    struct snd_ctl_elem_value *ucontrol)
1486 {
1487         struct echoaudio *chip;
1488
1489         chip = snd_kcontrol_chip(kcontrol);
1490         ucontrol->value.enumerated.item[0] = !!chip->professional_spdif;
1491         return 0;
1492 }
1493
1494 static int snd_echo_spdif_mode_put(struct snd_kcontrol *kcontrol,
1495                                    struct snd_ctl_elem_value *ucontrol)
1496 {
1497         struct echoaudio *chip;
1498         int mode;
1499
1500         chip = snd_kcontrol_chip(kcontrol);
1501         mode = !!ucontrol->value.enumerated.item[0];
1502         if (mode != chip->professional_spdif) {
1503                 spin_lock_irq(&chip->lock);
1504                 set_professional_spdif(chip, mode);
1505                 spin_unlock_irq(&chip->lock);
1506                 return 1;
1507         }
1508         return 0;
1509 }
1510
1511 static struct snd_kcontrol_new snd_echo_spdif_mode_switch __devinitdata = {
1512         .name = "S/PDIF mode Switch",
1513         .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1514         .info = snd_echo_spdif_mode_info,
1515         .get = snd_echo_spdif_mode_get,
1516         .put = snd_echo_spdif_mode_put,
1517 };
1518
1519 #endif /* ECHOCARD_HAS_DIGITAL_IO */
1520
1521
1522
1523 #ifdef ECHOCARD_HAS_EXTERNAL_CLOCK
1524
1525 /******************* Select input clock source *******************/
1526 static int snd_echo_clock_source_info(struct snd_kcontrol *kcontrol,
1527                                       struct snd_ctl_elem_info *uinfo)
1528 {
1529         static char *names[8] = {
1530                 "Internal", "Word", "Super", "S/PDIF", "ADAT", "ESync",
1531                 "ESync96", "MTC"
1532         };
1533         struct echoaudio *chip;
1534
1535         chip = snd_kcontrol_chip(kcontrol);
1536         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1537         uinfo->value.enumerated.items = chip->num_clock_sources;
1538         uinfo->count = 1;
1539         if (uinfo->value.enumerated.item >= chip->num_clock_sources)
1540                 uinfo->value.enumerated.item = chip->num_clock_sources - 1;
1541         strcpy(uinfo->value.enumerated.name, names[
1542                         chip->clock_source_list[uinfo->value.enumerated.item]]);
1543         return 0;
1544 }
1545
1546 static int snd_echo_clock_source_get(struct snd_kcontrol *kcontrol,
1547                                      struct snd_ctl_elem_value *ucontrol)
1548 {
1549         struct echoaudio *chip;
1550         int i, clock;
1551
1552         chip = snd_kcontrol_chip(kcontrol);
1553         clock = chip->input_clock;
1554
1555         for (i = 0; i < chip->num_clock_sources; i++)
1556                 if (clock == chip->clock_source_list[i])
1557                         ucontrol->value.enumerated.item[0] = i;
1558
1559         return 0;
1560 }
1561
1562 static int snd_echo_clock_source_put(struct snd_kcontrol *kcontrol,
1563                                      struct snd_ctl_elem_value *ucontrol)
1564 {
1565         struct echoaudio *chip;
1566         int changed;
1567         unsigned int eclock, dclock;
1568
1569         changed = 0;
1570         chip = snd_kcontrol_chip(kcontrol);
1571         eclock = ucontrol->value.enumerated.item[0];
1572         if (eclock >= chip->input_clock_types)
1573                 return -EINVAL;
1574         dclock = chip->clock_source_list[eclock];
1575         if (chip->input_clock != dclock) {
1576                 mutex_lock(&chip->mode_mutex);
1577                 spin_lock_irq(&chip->lock);
1578                 if ((changed = set_input_clock(chip, dclock)) == 0)
1579                         changed = 1;    /* no errors */
1580                 spin_unlock_irq(&chip->lock);
1581                 mutex_unlock(&chip->mode_mutex);
1582         }
1583
1584         if (changed < 0)
1585                 DE_ACT(("seticlk val%d err 0x%x\n", dclock, changed));
1586
1587         return changed;
1588 }
1589
1590 static struct snd_kcontrol_new snd_echo_clock_source_switch __devinitdata = {
1591         .name = "Sample Clock Source",
1592         .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1593         .info = snd_echo_clock_source_info,
1594         .get = snd_echo_clock_source_get,
1595         .put = snd_echo_clock_source_put,
1596 };
1597
1598 #endif /* ECHOCARD_HAS_EXTERNAL_CLOCK */
1599
1600
1601
1602 #ifdef ECHOCARD_HAS_PHANTOM_POWER
1603
1604 /******************* Phantom power switch *******************/
1605 #define snd_echo_phantom_power_info     snd_ctl_boolean_mono_info
1606
1607 static int snd_echo_phantom_power_get(struct snd_kcontrol *kcontrol,
1608                                       struct snd_ctl_elem_value *ucontrol)
1609 {
1610         struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1611
1612         ucontrol->value.integer.value[0] = chip->phantom_power;
1613         return 0;
1614 }
1615
1616 static int snd_echo_phantom_power_put(struct snd_kcontrol *kcontrol,
1617                                       struct snd_ctl_elem_value *ucontrol)
1618 {
1619         struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1620         int power, changed = 0;
1621
1622         power = !!ucontrol->value.integer.value[0];
1623         if (chip->phantom_power != power) {
1624                 spin_lock_irq(&chip->lock);
1625                 changed = set_phantom_power(chip, power);
1626                 spin_unlock_irq(&chip->lock);
1627                 if (changed == 0)
1628                         changed = 1;    /* no errors */
1629         }
1630         return changed;
1631 }
1632
1633 static struct snd_kcontrol_new snd_echo_phantom_power_switch __devinitdata = {
1634         .name = "Phantom power Switch",
1635         .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1636         .info = snd_echo_phantom_power_info,
1637         .get = snd_echo_phantom_power_get,
1638         .put = snd_echo_phantom_power_put,
1639 };
1640
1641 #endif /* ECHOCARD_HAS_PHANTOM_POWER */
1642
1643
1644
1645 #ifdef ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE
1646
1647 /******************* Digital input automute switch *******************/
1648 #define snd_echo_automute_info          snd_ctl_boolean_mono_info
1649
1650 static int snd_echo_automute_get(struct snd_kcontrol *kcontrol,
1651                                  struct snd_ctl_elem_value *ucontrol)
1652 {
1653         struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1654
1655         ucontrol->value.integer.value[0] = chip->digital_in_automute;
1656         return 0;
1657 }
1658
1659 static int snd_echo_automute_put(struct snd_kcontrol *kcontrol,
1660                                  struct snd_ctl_elem_value *ucontrol)
1661 {
1662         struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1663         int automute, changed = 0;
1664
1665         automute = !!ucontrol->value.integer.value[0];
1666         if (chip->digital_in_automute != automute) {
1667                 spin_lock_irq(&chip->lock);
1668                 changed = set_input_auto_mute(chip, automute);
1669                 spin_unlock_irq(&chip->lock);
1670                 if (changed == 0)
1671                         changed = 1;    /* no errors */
1672         }
1673         return changed;
1674 }
1675
1676 static struct snd_kcontrol_new snd_echo_automute_switch __devinitdata = {
1677         .name = "Digital Capture Switch (automute)",
1678         .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1679         .info = snd_echo_automute_info,
1680         .get = snd_echo_automute_get,
1681         .put = snd_echo_automute_put,
1682 };
1683
1684 #endif /* ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE */
1685
1686
1687
1688 /******************* VU-meters switch *******************/
1689 #define snd_echo_vumeters_switch_info           snd_ctl_boolean_mono_info
1690
1691 static int snd_echo_vumeters_switch_put(struct snd_kcontrol *kcontrol,
1692                                         struct snd_ctl_elem_value *ucontrol)
1693 {
1694         struct echoaudio *chip;
1695
1696         chip = snd_kcontrol_chip(kcontrol);
1697         spin_lock_irq(&chip->lock);
1698         set_meters_on(chip, ucontrol->value.integer.value[0]);
1699         spin_unlock_irq(&chip->lock);
1700         return 1;
1701 }
1702
1703 static struct snd_kcontrol_new snd_echo_vumeters_switch __devinitdata = {
1704         .name = "VU-meters Switch",
1705         .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1706         .access = SNDRV_CTL_ELEM_ACCESS_WRITE,
1707         .info = snd_echo_vumeters_switch_info,
1708         .put = snd_echo_vumeters_switch_put,
1709 };
1710
1711
1712
1713 /***** Read VU-meters (input, output, analog and digital together) *****/
1714 static int snd_echo_vumeters_info(struct snd_kcontrol *kcontrol,
1715                                   struct snd_ctl_elem_info *uinfo)
1716 {
1717         struct echoaudio *chip;
1718
1719         chip = snd_kcontrol_chip(kcontrol);
1720         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1721         uinfo->count = 96;
1722         uinfo->value.integer.min = ECHOGAIN_MINOUT;
1723         uinfo->value.integer.max = 0;
1724 #ifdef ECHOCARD_HAS_VMIXER
1725         uinfo->dimen.d[0] = 3;  /* Out, In, Virt */
1726 #else
1727         uinfo->dimen.d[0] = 2;  /* Out, In */
1728 #endif
1729         uinfo->dimen.d[1] = 16; /* 16 channels */
1730         uinfo->dimen.d[2] = 2;  /* 0=level, 1=peak */
1731         return 0;
1732 }
1733
1734 static int snd_echo_vumeters_get(struct snd_kcontrol *kcontrol,
1735                                  struct snd_ctl_elem_value *ucontrol)
1736 {
1737         struct echoaudio *chip;
1738
1739         chip = snd_kcontrol_chip(kcontrol);
1740         get_audio_meters(chip, ucontrol->value.integer.value);
1741         return 0;
1742 }
1743
1744 static struct snd_kcontrol_new snd_echo_vumeters __devinitdata = {
1745         .name = "VU-meters",
1746         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1747         .access = SNDRV_CTL_ELEM_ACCESS_READ |
1748                   SNDRV_CTL_ELEM_ACCESS_VOLATILE |
1749                   SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1750         .info = snd_echo_vumeters_info,
1751         .get = snd_echo_vumeters_get,
1752         .tlv = {.p = db_scale_output_gain},
1753 };
1754
1755
1756
1757 /*** Channels info - it exports informations about the number of channels ***/
1758 static int snd_echo_channels_info_info(struct snd_kcontrol *kcontrol,
1759                                        struct snd_ctl_elem_info *uinfo)
1760 {
1761         struct echoaudio *chip;
1762
1763         chip = snd_kcontrol_chip(kcontrol);
1764         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1765         uinfo->count = 6;
1766         uinfo->value.integer.min = 0;
1767         uinfo->value.integer.max = 1 << ECHO_CLOCK_NUMBER;
1768         return 0;
1769 }
1770
1771 static int snd_echo_channels_info_get(struct snd_kcontrol *kcontrol,
1772                                       struct snd_ctl_elem_value *ucontrol)
1773 {
1774         struct echoaudio *chip;
1775         int detected, clocks, bit, src;
1776
1777         chip = snd_kcontrol_chip(kcontrol);
1778         ucontrol->value.integer.value[0] = num_busses_in(chip);
1779         ucontrol->value.integer.value[1] = num_analog_busses_in(chip);
1780         ucontrol->value.integer.value[2] = num_busses_out(chip);
1781         ucontrol->value.integer.value[3] = num_analog_busses_out(chip);
1782         ucontrol->value.integer.value[4] = num_pipes_out(chip);
1783
1784         /* Compute the bitmask of the currently valid input clocks */
1785         detected = detect_input_clocks(chip);
1786         clocks = 0;
1787         src = chip->num_clock_sources - 1;
1788         for (bit = ECHO_CLOCK_NUMBER - 1; bit >= 0; bit--)
1789                 if (detected & (1 << bit))
1790                         for (; src >= 0; src--)
1791                                 if (bit == chip->clock_source_list[src]) {
1792                                         clocks |= 1 << src;
1793                                         break;
1794                                 }
1795         ucontrol->value.integer.value[5] = clocks;
1796
1797         return 0;
1798 }
1799
1800 static struct snd_kcontrol_new snd_echo_channels_info __devinitdata = {
1801         .name = "Channels info",
1802         .iface = SNDRV_CTL_ELEM_IFACE_HWDEP,
1803         .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
1804         .info = snd_echo_channels_info_info,
1805         .get = snd_echo_channels_info_get,
1806 };
1807
1808
1809
1810
1811 /******************************************************************************
1812         IRQ Handler
1813 ******************************************************************************/
1814
1815 static irqreturn_t snd_echo_interrupt(int irq, void *dev_id)
1816 {
1817         struct echoaudio *chip = dev_id;
1818         struct snd_pcm_substream *substream;
1819         int period, ss, st;
1820
1821         spin_lock(&chip->lock);
1822         st = service_irq(chip);
1823         if (st < 0) {
1824                 spin_unlock(&chip->lock);
1825                 return IRQ_NONE;
1826         }
1827         /* The hardware doesn't tell us which substream caused the irq,
1828         thus we have to check all running substreams. */
1829         for (ss = 0; ss < DSP_MAXPIPES; ss++) {
1830                 if ((substream = chip->substream[ss])) {
1831                         period = pcm_pointer(substream) /
1832                                 substream->runtime->period_size;
1833                         if (period != chip->last_period[ss]) {
1834                                 chip->last_period[ss] = period;
1835                                 spin_unlock(&chip->lock);
1836                                 snd_pcm_period_elapsed(substream);
1837                                 spin_lock(&chip->lock);
1838                         }
1839                 }
1840         }
1841         spin_unlock(&chip->lock);
1842
1843 #ifdef ECHOCARD_HAS_MIDI
1844         if (st > 0 && chip->midi_in) {
1845                 snd_rawmidi_receive(chip->midi_in, chip->midi_buffer, st);
1846                 DE_MID(("rawmidi_iread=%d\n", st));
1847         }
1848 #endif
1849         return IRQ_HANDLED;
1850 }
1851
1852
1853
1854
1855 /******************************************************************************
1856         Module construction / destruction
1857 ******************************************************************************/
1858
1859 static int snd_echo_free(struct echoaudio *chip)
1860 {
1861         DE_INIT(("Stop DSP...\n"));
1862         if (chip->comm_page)
1863                 rest_in_peace(chip);
1864         DE_INIT(("Stopped.\n"));
1865
1866         if (chip->irq >= 0)
1867                 free_irq(chip->irq, chip);
1868
1869         if (chip->comm_page)
1870                 snd_dma_free_pages(&chip->commpage_dma_buf);
1871
1872         if (chip->dsp_registers)
1873                 iounmap(chip->dsp_registers);
1874
1875         if (chip->iores)
1876                 release_and_free_resource(chip->iores);
1877
1878         DE_INIT(("MMIO freed.\n"));
1879
1880         pci_disable_device(chip->pci);
1881
1882         /* release chip data */
1883         kfree(chip);
1884         DE_INIT(("Chip freed.\n"));
1885         return 0;
1886 }
1887
1888
1889
1890 static int snd_echo_dev_free(struct snd_device *device)
1891 {
1892         struct echoaudio *chip = device->device_data;
1893
1894         DE_INIT(("snd_echo_dev_free()...\n"));
1895         return snd_echo_free(chip);
1896 }
1897
1898
1899
1900 /* <--snd_echo_probe() */
1901 static __devinit int snd_echo_create(struct snd_card *card,
1902                                      struct pci_dev *pci,
1903                                      struct echoaudio **rchip)
1904 {
1905         struct echoaudio *chip;
1906         int err;
1907         size_t sz;
1908         static struct snd_device_ops ops = {
1909                 .dev_free = snd_echo_dev_free,
1910         };
1911
1912         *rchip = NULL;
1913
1914         pci_write_config_byte(pci, PCI_LATENCY_TIMER, 0xC0);
1915
1916         if ((err = pci_enable_device(pci)) < 0)
1917                 return err;
1918         pci_set_master(pci);
1919
1920         /* allocate a chip-specific data */
1921         chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1922         if (!chip) {
1923                 pci_disable_device(pci);
1924                 return -ENOMEM;
1925         }
1926         DE_INIT(("chip=%p\n", chip));
1927
1928         spin_lock_init(&chip->lock);
1929         chip->card = card;
1930         chip->pci = pci;
1931         chip->irq = -1;
1932
1933         /* PCI resource allocation */
1934         chip->dsp_registers_phys = pci_resource_start(pci, 0);
1935         sz = pci_resource_len(pci, 0);
1936         if (sz > PAGE_SIZE)
1937                 sz = PAGE_SIZE;         /* We map only the required part */
1938
1939         if ((chip->iores = request_mem_region(chip->dsp_registers_phys, sz,
1940                                               ECHOCARD_NAME)) == NULL) {
1941                 snd_echo_free(chip);
1942                 snd_printk(KERN_ERR "cannot get memory region\n");
1943                 return -EBUSY;
1944         }
1945         chip->dsp_registers = (volatile u32 __iomem *)
1946                 ioremap_nocache(chip->dsp_registers_phys, sz);
1947
1948         if (request_irq(pci->irq, snd_echo_interrupt, IRQF_SHARED,
1949                         ECHOCARD_NAME, chip)) {
1950                 snd_echo_free(chip);
1951                 snd_printk(KERN_ERR "cannot grab irq\n");
1952                 return -EBUSY;
1953         }
1954         chip->irq = pci->irq;
1955         DE_INIT(("pci=%p irq=%d subdev=%04x Init hardware...\n",
1956                  chip->pci, chip->irq, chip->pci->subsystem_device));
1957
1958         /* Create the DSP comm page - this is the area of memory used for most
1959         of the communication with the DSP, which accesses it via bus mastering */
1960         if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
1961                                 sizeof(struct comm_page),
1962                                 &chip->commpage_dma_buf) < 0) {
1963                 snd_echo_free(chip);
1964                 snd_printk(KERN_ERR "cannot allocate the comm page\n");
1965                 return -ENOMEM;
1966         }
1967         chip->comm_page_phys = chip->commpage_dma_buf.addr;
1968         chip->comm_page = (struct comm_page *)chip->commpage_dma_buf.area;
1969
1970         err = init_hw(chip, chip->pci->device, chip->pci->subsystem_device);
1971         if (err) {
1972                 DE_INIT(("init_hw err=%d\n", err));
1973                 snd_echo_free(chip);
1974                 return err;
1975         }
1976         DE_INIT(("Card init OK\n"));
1977
1978         if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
1979                 snd_echo_free(chip);
1980                 return err;
1981         }
1982         atomic_set(&chip->opencount, 0);
1983         mutex_init(&chip->mode_mutex);
1984         chip->can_set_rate = 1;
1985         *rchip = chip;
1986         /* Init done ! */
1987         return 0;
1988 }
1989
1990
1991
1992 /* constructor */
1993 static int __devinit snd_echo_probe(struct pci_dev *pci,
1994                                     const struct pci_device_id *pci_id)
1995 {
1996         static int dev;
1997         struct snd_card *card;
1998         struct echoaudio *chip;
1999         char *dsp;
2000         int i, err;
2001
2002         if (dev >= SNDRV_CARDS)
2003                 return -ENODEV;
2004         if (!enable[dev]) {
2005                 dev++;
2006                 return -ENOENT;
2007         }
2008
2009         DE_INIT(("Echoaudio driver starting...\n"));
2010         i = 0;
2011         err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
2012         if (err < 0)
2013                 return err;
2014
2015         snd_card_set_dev(card, &pci->dev);
2016
2017         if ((err = snd_echo_create(card, pci, &chip)) < 0) {
2018                 snd_card_free(card);
2019                 return err;
2020         }
2021
2022         strcpy(card->driver, "Echo_" ECHOCARD_NAME);
2023         strcpy(card->shortname, chip->card_name);
2024
2025         dsp = "56301";
2026         if (pci_id->device == 0x3410)
2027                 dsp = "56361";
2028
2029         sprintf(card->longname, "%s rev.%d (DSP%s) at 0x%lx irq %i",
2030                 card->shortname, pci_id->subdevice & 0x000f, dsp,
2031                 chip->dsp_registers_phys, chip->irq);
2032
2033         if ((err = snd_echo_new_pcm(chip)) < 0) {
2034                 snd_printk(KERN_ERR "new pcm error %d\n", err);
2035                 snd_card_free(card);
2036                 return err;
2037         }
2038
2039 #ifdef ECHOCARD_HAS_MIDI
2040         if (chip->has_midi) {   /* Some Mia's do not have midi */
2041                 if ((err = snd_echo_midi_create(card, chip)) < 0) {
2042                         snd_printk(KERN_ERR "new midi error %d\n", err);
2043                         snd_card_free(card);
2044                         return err;
2045                 }
2046         }
2047 #endif
2048
2049 #ifdef ECHOCARD_HAS_VMIXER
2050         snd_echo_vmixer.count = num_pipes_out(chip) * num_busses_out(chip);
2051         if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vmixer, chip))) < 0)
2052                 goto ctl_error;
2053 #ifdef ECHOCARD_HAS_LINE_OUT_GAIN
2054         err = snd_ctl_add(chip->card,
2055                           snd_ctl_new1(&snd_echo_line_output_gain, chip));
2056         if (err < 0)
2057                 goto ctl_error;
2058 #endif
2059 #else /* ECHOCARD_HAS_VMIXER */
2060         err = snd_ctl_add(chip->card,
2061                           snd_ctl_new1(&snd_echo_pcm_output_gain, chip));
2062         if (err < 0)
2063                 goto ctl_error;
2064 #endif /* ECHOCARD_HAS_VMIXER */
2065
2066 #ifdef ECHOCARD_HAS_INPUT_GAIN
2067         if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_line_input_gain, chip))) < 0)
2068                 goto ctl_error;
2069 #endif
2070
2071 #ifdef ECHOCARD_HAS_INPUT_NOMINAL_LEVEL
2072         if (!chip->hasnt_input_nominal_level)
2073                 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_intput_nominal_level, chip))) < 0)
2074                         goto ctl_error;
2075 #endif
2076
2077 #ifdef ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL
2078         if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_output_nominal_level, chip))) < 0)
2079                 goto ctl_error;
2080 #endif
2081
2082         if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vumeters_switch, chip))) < 0)
2083                 goto ctl_error;
2084
2085         if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vumeters, chip))) < 0)
2086                 goto ctl_error;
2087
2088 #ifdef ECHOCARD_HAS_MONITOR
2089         snd_echo_monitor_mixer.count = num_busses_in(chip) * num_busses_out(chip);
2090         if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_monitor_mixer, chip))) < 0)
2091                 goto ctl_error;
2092 #endif
2093
2094 #ifdef ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE
2095         if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_automute_switch, chip))) < 0)
2096                 goto ctl_error;
2097 #endif
2098
2099         if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_channels_info, chip))) < 0)
2100                 goto ctl_error;
2101
2102 #ifdef ECHOCARD_HAS_DIGITAL_MODE_SWITCH
2103         /* Creates a list of available digital modes */
2104         chip->num_digital_modes = 0;
2105         for (i = 0; i < 6; i++)
2106                 if (chip->digital_modes & (1 << i))
2107                         chip->digital_mode_list[chip->num_digital_modes++] = i;
2108
2109         if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_digital_mode_switch, chip))) < 0)
2110                 goto ctl_error;
2111 #endif /* ECHOCARD_HAS_DIGITAL_MODE_SWITCH */
2112
2113 #ifdef ECHOCARD_HAS_EXTERNAL_CLOCK
2114         /* Creates a list of available clock sources */
2115         chip->num_clock_sources = 0;
2116         for (i = 0; i < 10; i++)
2117                 if (chip->input_clock_types & (1 << i))
2118                         chip->clock_source_list[chip->num_clock_sources++] = i;
2119
2120         if (chip->num_clock_sources > 1) {
2121                 chip->clock_src_ctl = snd_ctl_new1(&snd_echo_clock_source_switch, chip);
2122                 if ((err = snd_ctl_add(chip->card, chip->clock_src_ctl)) < 0)
2123                         goto ctl_error;
2124         }
2125 #endif /* ECHOCARD_HAS_EXTERNAL_CLOCK */
2126
2127 #ifdef ECHOCARD_HAS_DIGITAL_IO
2128         if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_spdif_mode_switch, chip))) < 0)
2129                 goto ctl_error;
2130 #endif
2131
2132 #ifdef ECHOCARD_HAS_PHANTOM_POWER
2133         if (chip->has_phantom_power)
2134                 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_phantom_power_switch, chip))) < 0)
2135                         goto ctl_error;
2136 #endif
2137
2138         if ((err = snd_card_register(card)) < 0) {
2139                 snd_card_free(card);
2140                 goto ctl_error;
2141         }
2142         snd_printk(KERN_INFO "Card registered: %s\n", card->longname);
2143
2144         pci_set_drvdata(pci, chip);
2145         dev++;
2146         return 0;
2147
2148 ctl_error:
2149         snd_printk(KERN_ERR "new control error %d\n", err);
2150         snd_card_free(card);
2151         return err;
2152 }
2153
2154
2155
2156 static void __devexit snd_echo_remove(struct pci_dev *pci)
2157 {
2158         struct echoaudio *chip;
2159
2160         chip = pci_get_drvdata(pci);
2161         if (chip)
2162                 snd_card_free(chip->card);
2163         pci_set_drvdata(pci, NULL);
2164 }
2165
2166
2167
2168 /******************************************************************************
2169         Everything starts and ends here
2170 ******************************************************************************/
2171
2172 /* pci_driver definition */
2173 static struct pci_driver driver = {
2174         .name = "Echoaudio " ECHOCARD_NAME,
2175         .id_table = snd_echo_ids,
2176         .probe = snd_echo_probe,
2177         .remove = __devexit_p(snd_echo_remove),
2178 };
2179
2180
2181
2182 /* initialization of the module */
2183 static int __init alsa_card_echo_init(void)
2184 {
2185         return pci_register_driver(&driver);
2186 }
2187
2188
2189
2190 /* clean up the module */
2191 static void __exit alsa_card_echo_exit(void)
2192 {
2193         pci_unregister_driver(&driver);
2194 }
2195
2196
2197 module_init(alsa_card_echo_init)
2198 module_exit(alsa_card_echo_exit)