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