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