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