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