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