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