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