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