Merge tag 'devicetree-for-linus' of git://git.secretlab.ca/git/linux
[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 char *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         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1427         uinfo->value.enumerated.items = chip->num_digital_modes;
1428         uinfo->count = 1;
1429         if (uinfo->value.enumerated.item >= chip->num_digital_modes)
1430                 uinfo->value.enumerated.item = chip->num_digital_modes - 1;
1431         strcpy(uinfo->value.enumerated.name, names[
1432                         chip->digital_mode_list[uinfo->value.enumerated.item]]);
1433         return 0;
1434 }
1435
1436 static int snd_echo_digital_mode_get(struct snd_kcontrol *kcontrol,
1437                                      struct snd_ctl_elem_value *ucontrol)
1438 {
1439         struct echoaudio *chip;
1440         int i, mode;
1441
1442         chip = snd_kcontrol_chip(kcontrol);
1443         mode = chip->digital_mode;
1444         for (i = chip->num_digital_modes - 1; i >= 0; i--)
1445                 if (mode == chip->digital_mode_list[i]) {
1446                         ucontrol->value.enumerated.item[0] = i;
1447                         break;
1448                 }
1449         return 0;
1450 }
1451
1452 static int snd_echo_digital_mode_put(struct snd_kcontrol *kcontrol,
1453                                      struct snd_ctl_elem_value *ucontrol)
1454 {
1455         struct echoaudio *chip;
1456         int changed;
1457         unsigned short emode, dmode;
1458
1459         changed = 0;
1460         chip = snd_kcontrol_chip(kcontrol);
1461
1462         emode = ucontrol->value.enumerated.item[0];
1463         if (emode >= chip->num_digital_modes)
1464                 return -EINVAL;
1465         dmode = chip->digital_mode_list[emode];
1466
1467         if (dmode != chip->digital_mode) {
1468                 /* mode_mutex is required to make this operation atomic wrt
1469                 pcm_digital_*_open() and set_input_clock() functions. */
1470                 mutex_lock(&chip->mode_mutex);
1471
1472                 /* Do not allow the user to change the digital mode when a pcm
1473                 device is open because it also changes the number of channels
1474                 and the allowed sample rates */
1475                 if (atomic_read(&chip->opencount)) {
1476                         changed = -EAGAIN;
1477                 } else {
1478                         changed = set_digital_mode(chip, dmode);
1479                         /* If we had to change the clock source, report it */
1480                         if (changed > 0 && chip->clock_src_ctl) {
1481                                 snd_ctl_notify(chip->card,
1482                                                SNDRV_CTL_EVENT_MASK_VALUE,
1483                                                &chip->clock_src_ctl->id);
1484                                 DE_ACT(("SDM() =%d\n", changed));
1485                         }
1486                         if (changed >= 0)
1487                                 changed = 1;    /* No errors */
1488                 }
1489                 mutex_unlock(&chip->mode_mutex);
1490         }
1491         return changed;
1492 }
1493
1494 static struct snd_kcontrol_new snd_echo_digital_mode_switch = {
1495         .name = "Digital mode Switch",
1496         .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1497         .info = snd_echo_digital_mode_info,
1498         .get = snd_echo_digital_mode_get,
1499         .put = snd_echo_digital_mode_put,
1500 };
1501
1502 #endif /* ECHOCARD_HAS_DIGITAL_MODE_SWITCH */
1503
1504
1505
1506 #ifdef ECHOCARD_HAS_DIGITAL_IO
1507
1508 /******************* S/PDIF mode switch *******************/
1509 static int snd_echo_spdif_mode_info(struct snd_kcontrol *kcontrol,
1510                                     struct snd_ctl_elem_info *uinfo)
1511 {
1512         static char *names[2] = {"Consumer", "Professional"};
1513
1514         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1515         uinfo->value.enumerated.items = 2;
1516         uinfo->count = 1;
1517         if (uinfo->value.enumerated.item)
1518                 uinfo->value.enumerated.item = 1;
1519         strcpy(uinfo->value.enumerated.name,
1520                names[uinfo->value.enumerated.item]);
1521         return 0;
1522 }
1523
1524 static int snd_echo_spdif_mode_get(struct snd_kcontrol *kcontrol,
1525                                    struct snd_ctl_elem_value *ucontrol)
1526 {
1527         struct echoaudio *chip;
1528
1529         chip = snd_kcontrol_chip(kcontrol);
1530         ucontrol->value.enumerated.item[0] = !!chip->professional_spdif;
1531         return 0;
1532 }
1533
1534 static int snd_echo_spdif_mode_put(struct snd_kcontrol *kcontrol,
1535                                    struct snd_ctl_elem_value *ucontrol)
1536 {
1537         struct echoaudio *chip;
1538         int mode;
1539
1540         chip = snd_kcontrol_chip(kcontrol);
1541         mode = !!ucontrol->value.enumerated.item[0];
1542         if (mode != chip->professional_spdif) {
1543                 spin_lock_irq(&chip->lock);
1544                 set_professional_spdif(chip, mode);
1545                 spin_unlock_irq(&chip->lock);
1546                 return 1;
1547         }
1548         return 0;
1549 }
1550
1551 static struct snd_kcontrol_new snd_echo_spdif_mode_switch = {
1552         .name = "S/PDIF mode Switch",
1553         .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1554         .info = snd_echo_spdif_mode_info,
1555         .get = snd_echo_spdif_mode_get,
1556         .put = snd_echo_spdif_mode_put,
1557 };
1558
1559 #endif /* ECHOCARD_HAS_DIGITAL_IO */
1560
1561
1562
1563 #ifdef ECHOCARD_HAS_EXTERNAL_CLOCK
1564
1565 /******************* Select input clock source *******************/
1566 static int snd_echo_clock_source_info(struct snd_kcontrol *kcontrol,
1567                                       struct snd_ctl_elem_info *uinfo)
1568 {
1569         static char *names[8] = {
1570                 "Internal", "Word", "Super", "S/PDIF", "ADAT", "ESync",
1571                 "ESync96", "MTC"
1572         };
1573         struct echoaudio *chip;
1574
1575         chip = snd_kcontrol_chip(kcontrol);
1576         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1577         uinfo->value.enumerated.items = chip->num_clock_sources;
1578         uinfo->count = 1;
1579         if (uinfo->value.enumerated.item >= chip->num_clock_sources)
1580                 uinfo->value.enumerated.item = chip->num_clock_sources - 1;
1581         strcpy(uinfo->value.enumerated.name, names[
1582                         chip->clock_source_list[uinfo->value.enumerated.item]]);
1583         return 0;
1584 }
1585
1586 static int snd_echo_clock_source_get(struct snd_kcontrol *kcontrol,
1587                                      struct snd_ctl_elem_value *ucontrol)
1588 {
1589         struct echoaudio *chip;
1590         int i, clock;
1591
1592         chip = snd_kcontrol_chip(kcontrol);
1593         clock = chip->input_clock;
1594
1595         for (i = 0; i < chip->num_clock_sources; i++)
1596                 if (clock == chip->clock_source_list[i])
1597                         ucontrol->value.enumerated.item[0] = i;
1598
1599         return 0;
1600 }
1601
1602 static int snd_echo_clock_source_put(struct snd_kcontrol *kcontrol,
1603                                      struct snd_ctl_elem_value *ucontrol)
1604 {
1605         struct echoaudio *chip;
1606         int changed;
1607         unsigned int eclock, dclock;
1608
1609         changed = 0;
1610         chip = snd_kcontrol_chip(kcontrol);
1611         eclock = ucontrol->value.enumerated.item[0];
1612         if (eclock >= chip->input_clock_types)
1613                 return -EINVAL;
1614         dclock = chip->clock_source_list[eclock];
1615         if (chip->input_clock != dclock) {
1616                 mutex_lock(&chip->mode_mutex);
1617                 spin_lock_irq(&chip->lock);
1618                 if ((changed = set_input_clock(chip, dclock)) == 0)
1619                         changed = 1;    /* no errors */
1620                 spin_unlock_irq(&chip->lock);
1621                 mutex_unlock(&chip->mode_mutex);
1622         }
1623
1624         if (changed < 0)
1625                 DE_ACT(("seticlk val%d err 0x%x\n", dclock, changed));
1626
1627         return changed;
1628 }
1629
1630 static struct snd_kcontrol_new snd_echo_clock_source_switch = {
1631         .name = "Sample Clock Source",
1632         .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1633         .info = snd_echo_clock_source_info,
1634         .get = snd_echo_clock_source_get,
1635         .put = snd_echo_clock_source_put,
1636 };
1637
1638 #endif /* ECHOCARD_HAS_EXTERNAL_CLOCK */
1639
1640
1641
1642 #ifdef ECHOCARD_HAS_PHANTOM_POWER
1643
1644 /******************* Phantom power switch *******************/
1645 #define snd_echo_phantom_power_info     snd_ctl_boolean_mono_info
1646
1647 static int snd_echo_phantom_power_get(struct snd_kcontrol *kcontrol,
1648                                       struct snd_ctl_elem_value *ucontrol)
1649 {
1650         struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1651
1652         ucontrol->value.integer.value[0] = chip->phantom_power;
1653         return 0;
1654 }
1655
1656 static int snd_echo_phantom_power_put(struct snd_kcontrol *kcontrol,
1657                                       struct snd_ctl_elem_value *ucontrol)
1658 {
1659         struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1660         int power, changed = 0;
1661
1662         power = !!ucontrol->value.integer.value[0];
1663         if (chip->phantom_power != power) {
1664                 spin_lock_irq(&chip->lock);
1665                 changed = set_phantom_power(chip, power);
1666                 spin_unlock_irq(&chip->lock);
1667                 if (changed == 0)
1668                         changed = 1;    /* no errors */
1669         }
1670         return changed;
1671 }
1672
1673 static struct snd_kcontrol_new snd_echo_phantom_power_switch = {
1674         .name = "Phantom power Switch",
1675         .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1676         .info = snd_echo_phantom_power_info,
1677         .get = snd_echo_phantom_power_get,
1678         .put = snd_echo_phantom_power_put,
1679 };
1680
1681 #endif /* ECHOCARD_HAS_PHANTOM_POWER */
1682
1683
1684
1685 #ifdef ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE
1686
1687 /******************* Digital input automute switch *******************/
1688 #define snd_echo_automute_info          snd_ctl_boolean_mono_info
1689
1690 static int snd_echo_automute_get(struct snd_kcontrol *kcontrol,
1691                                  struct snd_ctl_elem_value *ucontrol)
1692 {
1693         struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1694
1695         ucontrol->value.integer.value[0] = chip->digital_in_automute;
1696         return 0;
1697 }
1698
1699 static int snd_echo_automute_put(struct snd_kcontrol *kcontrol,
1700                                  struct snd_ctl_elem_value *ucontrol)
1701 {
1702         struct echoaudio *chip = snd_kcontrol_chip(kcontrol);
1703         int automute, changed = 0;
1704
1705         automute = !!ucontrol->value.integer.value[0];
1706         if (chip->digital_in_automute != automute) {
1707                 spin_lock_irq(&chip->lock);
1708                 changed = set_input_auto_mute(chip, automute);
1709                 spin_unlock_irq(&chip->lock);
1710                 if (changed == 0)
1711                         changed = 1;    /* no errors */
1712         }
1713         return changed;
1714 }
1715
1716 static struct snd_kcontrol_new snd_echo_automute_switch = {
1717         .name = "Digital Capture Switch (automute)",
1718         .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1719         .info = snd_echo_automute_info,
1720         .get = snd_echo_automute_get,
1721         .put = snd_echo_automute_put,
1722 };
1723
1724 #endif /* ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE */
1725
1726
1727
1728 /******************* VU-meters switch *******************/
1729 #define snd_echo_vumeters_switch_info           snd_ctl_boolean_mono_info
1730
1731 static int snd_echo_vumeters_switch_put(struct snd_kcontrol *kcontrol,
1732                                         struct snd_ctl_elem_value *ucontrol)
1733 {
1734         struct echoaudio *chip;
1735
1736         chip = snd_kcontrol_chip(kcontrol);
1737         spin_lock_irq(&chip->lock);
1738         set_meters_on(chip, ucontrol->value.integer.value[0]);
1739         spin_unlock_irq(&chip->lock);
1740         return 1;
1741 }
1742
1743 static struct snd_kcontrol_new snd_echo_vumeters_switch = {
1744         .name = "VU-meters Switch",
1745         .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1746         .access = SNDRV_CTL_ELEM_ACCESS_WRITE,
1747         .info = snd_echo_vumeters_switch_info,
1748         .put = snd_echo_vumeters_switch_put,
1749 };
1750
1751
1752
1753 /***** Read VU-meters (input, output, analog and digital together) *****/
1754 static int snd_echo_vumeters_info(struct snd_kcontrol *kcontrol,
1755                                   struct snd_ctl_elem_info *uinfo)
1756 {
1757         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1758         uinfo->count = 96;
1759         uinfo->value.integer.min = ECHOGAIN_MINOUT;
1760         uinfo->value.integer.max = 0;
1761 #ifdef ECHOCARD_HAS_VMIXER
1762         uinfo->dimen.d[0] = 3;  /* Out, In, Virt */
1763 #else
1764         uinfo->dimen.d[0] = 2;  /* Out, In */
1765 #endif
1766         uinfo->dimen.d[1] = 16; /* 16 channels */
1767         uinfo->dimen.d[2] = 2;  /* 0=level, 1=peak */
1768         return 0;
1769 }
1770
1771 static int snd_echo_vumeters_get(struct snd_kcontrol *kcontrol,
1772                                  struct snd_ctl_elem_value *ucontrol)
1773 {
1774         struct echoaudio *chip;
1775
1776         chip = snd_kcontrol_chip(kcontrol);
1777         get_audio_meters(chip, ucontrol->value.integer.value);
1778         return 0;
1779 }
1780
1781 static struct snd_kcontrol_new snd_echo_vumeters = {
1782         .name = "VU-meters",
1783         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1784         .access = SNDRV_CTL_ELEM_ACCESS_READ |
1785                   SNDRV_CTL_ELEM_ACCESS_VOLATILE |
1786                   SNDRV_CTL_ELEM_ACCESS_TLV_READ,
1787         .info = snd_echo_vumeters_info,
1788         .get = snd_echo_vumeters_get,
1789         .tlv = {.p = db_scale_output_gain},
1790 };
1791
1792
1793
1794 /*** Channels info - it exports informations about the number of channels ***/
1795 static int snd_echo_channels_info_info(struct snd_kcontrol *kcontrol,
1796                                        struct snd_ctl_elem_info *uinfo)
1797 {
1798         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1799         uinfo->count = 6;
1800         uinfo->value.integer.min = 0;
1801         uinfo->value.integer.max = 1 << ECHO_CLOCK_NUMBER;
1802         return 0;
1803 }
1804
1805 static int snd_echo_channels_info_get(struct snd_kcontrol *kcontrol,
1806                                       struct snd_ctl_elem_value *ucontrol)
1807 {
1808         struct echoaudio *chip;
1809         int detected, clocks, bit, src;
1810
1811         chip = snd_kcontrol_chip(kcontrol);
1812         ucontrol->value.integer.value[0] = num_busses_in(chip);
1813         ucontrol->value.integer.value[1] = num_analog_busses_in(chip);
1814         ucontrol->value.integer.value[2] = num_busses_out(chip);
1815         ucontrol->value.integer.value[3] = num_analog_busses_out(chip);
1816         ucontrol->value.integer.value[4] = num_pipes_out(chip);
1817
1818         /* Compute the bitmask of the currently valid input clocks */
1819         detected = detect_input_clocks(chip);
1820         clocks = 0;
1821         src = chip->num_clock_sources - 1;
1822         for (bit = ECHO_CLOCK_NUMBER - 1; bit >= 0; bit--)
1823                 if (detected & (1 << bit))
1824                         for (; src >= 0; src--)
1825                                 if (bit == chip->clock_source_list[src]) {
1826                                         clocks |= 1 << src;
1827                                         break;
1828                                 }
1829         ucontrol->value.integer.value[5] = clocks;
1830
1831         return 0;
1832 }
1833
1834 static struct snd_kcontrol_new snd_echo_channels_info = {
1835         .name = "Channels info",
1836         .iface = SNDRV_CTL_ELEM_IFACE_HWDEP,
1837         .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
1838         .info = snd_echo_channels_info_info,
1839         .get = snd_echo_channels_info_get,
1840 };
1841
1842
1843
1844
1845 /******************************************************************************
1846         IRQ Handler
1847 ******************************************************************************/
1848
1849 static irqreturn_t snd_echo_interrupt(int irq, void *dev_id)
1850 {
1851         struct echoaudio *chip = dev_id;
1852         struct snd_pcm_substream *substream;
1853         int period, ss, st;
1854
1855         spin_lock(&chip->lock);
1856         st = service_irq(chip);
1857         if (st < 0) {
1858                 spin_unlock(&chip->lock);
1859                 return IRQ_NONE;
1860         }
1861         /* The hardware doesn't tell us which substream caused the irq,
1862         thus we have to check all running substreams. */
1863         for (ss = 0; ss < DSP_MAXPIPES; ss++) {
1864                 substream = chip->substream[ss];
1865                 if (substream && ((struct audiopipe *)substream->runtime->
1866                                 private_data)->state == PIPE_STATE_STARTED) {
1867                         period = pcm_pointer(substream) /
1868                                 substream->runtime->period_size;
1869                         if (period != chip->last_period[ss]) {
1870                                 chip->last_period[ss] = period;
1871                                 spin_unlock(&chip->lock);
1872                                 snd_pcm_period_elapsed(substream);
1873                                 spin_lock(&chip->lock);
1874                         }
1875                 }
1876         }
1877         spin_unlock(&chip->lock);
1878
1879 #ifdef ECHOCARD_HAS_MIDI
1880         if (st > 0 && chip->midi_in) {
1881                 snd_rawmidi_receive(chip->midi_in, chip->midi_buffer, st);
1882                 DE_MID(("rawmidi_iread=%d\n", st));
1883         }
1884 #endif
1885         return IRQ_HANDLED;
1886 }
1887
1888
1889
1890
1891 /******************************************************************************
1892         Module construction / destruction
1893 ******************************************************************************/
1894
1895 static int snd_echo_free(struct echoaudio *chip)
1896 {
1897         DE_INIT(("Stop DSP...\n"));
1898         if (chip->comm_page)
1899                 rest_in_peace(chip);
1900         DE_INIT(("Stopped.\n"));
1901
1902         if (chip->irq >= 0)
1903                 free_irq(chip->irq, chip);
1904
1905         if (chip->comm_page)
1906                 snd_dma_free_pages(&chip->commpage_dma_buf);
1907
1908         if (chip->dsp_registers)
1909                 iounmap(chip->dsp_registers);
1910
1911         if (chip->iores)
1912                 release_and_free_resource(chip->iores);
1913
1914         DE_INIT(("MMIO freed.\n"));
1915
1916         pci_disable_device(chip->pci);
1917
1918         /* release chip data */
1919         free_firmware_cache(chip);
1920         kfree(chip);
1921         DE_INIT(("Chip freed.\n"));
1922         return 0;
1923 }
1924
1925
1926
1927 static int snd_echo_dev_free(struct snd_device *device)
1928 {
1929         struct echoaudio *chip = device->device_data;
1930
1931         DE_INIT(("snd_echo_dev_free()...\n"));
1932         return snd_echo_free(chip);
1933 }
1934
1935
1936
1937 /* <--snd_echo_probe() */
1938 static int snd_echo_create(struct snd_card *card,
1939                            struct pci_dev *pci,
1940                            struct echoaudio **rchip)
1941 {
1942         struct echoaudio *chip;
1943         int err;
1944         size_t sz;
1945         static struct snd_device_ops ops = {
1946                 .dev_free = snd_echo_dev_free,
1947         };
1948
1949         *rchip = NULL;
1950
1951         pci_write_config_byte(pci, PCI_LATENCY_TIMER, 0xC0);
1952
1953         if ((err = pci_enable_device(pci)) < 0)
1954                 return err;
1955         pci_set_master(pci);
1956
1957         /* Allocate chip if needed */
1958         if (!*rchip) {
1959                 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1960                 if (!chip) {
1961                         pci_disable_device(pci);
1962                         return -ENOMEM;
1963                 }
1964                 DE_INIT(("chip=%p\n", chip));
1965                 spin_lock_init(&chip->lock);
1966                 chip->card = card;
1967                 chip->pci = pci;
1968                 chip->irq = -1;
1969                 atomic_set(&chip->opencount, 0);
1970                 mutex_init(&chip->mode_mutex);
1971                 chip->can_set_rate = 1;
1972         } else {
1973                 /* If this was called from the resume function, chip is
1974                  * already allocated and it contains current card settings.
1975                  */
1976                 chip = *rchip;
1977         }
1978
1979         /* PCI resource allocation */
1980         chip->dsp_registers_phys = pci_resource_start(pci, 0);
1981         sz = pci_resource_len(pci, 0);
1982         if (sz > PAGE_SIZE)
1983                 sz = PAGE_SIZE;         /* We map only the required part */
1984
1985         if ((chip->iores = request_mem_region(chip->dsp_registers_phys, sz,
1986                                               ECHOCARD_NAME)) == NULL) {
1987                 dev_err(chip->card->dev, "cannot get memory region\n");
1988                 snd_echo_free(chip);
1989                 return -EBUSY;
1990         }
1991         chip->dsp_registers = (volatile u32 __iomem *)
1992                 ioremap_nocache(chip->dsp_registers_phys, sz);
1993
1994         if (request_irq(pci->irq, snd_echo_interrupt, IRQF_SHARED,
1995                         KBUILD_MODNAME, chip)) {
1996                 dev_err(chip->card->dev, "cannot grab irq\n");
1997                 snd_echo_free(chip);
1998                 return -EBUSY;
1999         }
2000         chip->irq = pci->irq;
2001         DE_INIT(("pci=%p irq=%d subdev=%04x Init hardware...\n",
2002                  chip->pci, chip->irq, chip->pci->subsystem_device));
2003
2004         /* Create the DSP comm page - this is the area of memory used for most
2005         of the communication with the DSP, which accesses it via bus mastering */
2006         if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
2007                                 sizeof(struct comm_page),
2008                                 &chip->commpage_dma_buf) < 0) {
2009                 dev_err(chip->card->dev, "cannot allocate the comm page\n");
2010                 snd_echo_free(chip);
2011                 return -ENOMEM;
2012         }
2013         chip->comm_page_phys = chip->commpage_dma_buf.addr;
2014         chip->comm_page = (struct comm_page *)chip->commpage_dma_buf.area;
2015
2016         err = init_hw(chip, chip->pci->device, chip->pci->subsystem_device);
2017         if (err >= 0)
2018                 err = set_mixer_defaults(chip);
2019         if (err < 0) {
2020                 DE_INIT(("init_hw err=%d\n", err));
2021                 snd_echo_free(chip);
2022                 return err;
2023         }
2024         DE_INIT(("Card init OK\n"));
2025
2026         if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
2027                 snd_echo_free(chip);
2028                 return err;
2029         }
2030         *rchip = chip;
2031         /* Init done ! */
2032         return 0;
2033 }
2034
2035
2036
2037 /* constructor */
2038 static int snd_echo_probe(struct pci_dev *pci,
2039                           const struct pci_device_id *pci_id)
2040 {
2041         static int dev;
2042         struct snd_card *card;
2043         struct echoaudio *chip;
2044         char *dsp;
2045         int i, err;
2046
2047         if (dev >= SNDRV_CARDS)
2048                 return -ENODEV;
2049         if (!enable[dev]) {
2050                 dev++;
2051                 return -ENOENT;
2052         }
2053
2054         DE_INIT(("Echoaudio driver starting...\n"));
2055         i = 0;
2056         err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
2057                            0, &card);
2058         if (err < 0)
2059                 return err;
2060
2061         chip = NULL;    /* Tells snd_echo_create to allocate chip */
2062         if ((err = snd_echo_create(card, pci, &chip)) < 0) {
2063                 snd_card_free(card);
2064                 return err;
2065         }
2066
2067         strcpy(card->driver, "Echo_" ECHOCARD_NAME);
2068         strcpy(card->shortname, chip->card_name);
2069
2070         dsp = "56301";
2071         if (pci_id->device == 0x3410)
2072                 dsp = "56361";
2073
2074         sprintf(card->longname, "%s rev.%d (DSP%s) at 0x%lx irq %i",
2075                 card->shortname, pci_id->subdevice & 0x000f, dsp,
2076                 chip->dsp_registers_phys, chip->irq);
2077
2078         if ((err = snd_echo_new_pcm(chip)) < 0) {
2079                 dev_err(chip->card->dev, "new pcm error %d\n", err);
2080                 snd_card_free(card);
2081                 return err;
2082         }
2083
2084 #ifdef ECHOCARD_HAS_MIDI
2085         if (chip->has_midi) {   /* Some Mia's do not have midi */
2086                 if ((err = snd_echo_midi_create(card, chip)) < 0) {
2087                         dev_err(chip->card->dev, "new midi error %d\n", err);
2088                         snd_card_free(card);
2089                         return err;
2090                 }
2091         }
2092 #endif
2093
2094 #ifdef ECHOCARD_HAS_VMIXER
2095         snd_echo_vmixer.count = num_pipes_out(chip) * num_busses_out(chip);
2096         if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vmixer, chip))) < 0)
2097                 goto ctl_error;
2098 #ifdef ECHOCARD_HAS_LINE_OUT_GAIN
2099         err = snd_ctl_add(chip->card,
2100                           snd_ctl_new1(&snd_echo_line_output_gain, chip));
2101         if (err < 0)
2102                 goto ctl_error;
2103 #endif
2104 #else /* ECHOCARD_HAS_VMIXER */
2105         err = snd_ctl_add(chip->card,
2106                           snd_ctl_new1(&snd_echo_pcm_output_gain, chip));
2107         if (err < 0)
2108                 goto ctl_error;
2109 #endif /* ECHOCARD_HAS_VMIXER */
2110
2111 #ifdef ECHOCARD_HAS_INPUT_GAIN
2112         if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_line_input_gain, chip))) < 0)
2113                 goto ctl_error;
2114 #endif
2115
2116 #ifdef ECHOCARD_HAS_INPUT_NOMINAL_LEVEL
2117         if (!chip->hasnt_input_nominal_level)
2118                 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_intput_nominal_level, chip))) < 0)
2119                         goto ctl_error;
2120 #endif
2121
2122 #ifdef ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL
2123         if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_output_nominal_level, chip))) < 0)
2124                 goto ctl_error;
2125 #endif
2126
2127         if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vumeters_switch, chip))) < 0)
2128                 goto ctl_error;
2129
2130         if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_vumeters, chip))) < 0)
2131                 goto ctl_error;
2132
2133 #ifdef ECHOCARD_HAS_MONITOR
2134         snd_echo_monitor_mixer.count = num_busses_in(chip) * num_busses_out(chip);
2135         if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_monitor_mixer, chip))) < 0)
2136                 goto ctl_error;
2137 #endif
2138
2139 #ifdef ECHOCARD_HAS_DIGITAL_IN_AUTOMUTE
2140         if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_automute_switch, chip))) < 0)
2141                 goto ctl_error;
2142 #endif
2143
2144         if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_channels_info, chip))) < 0)
2145                 goto ctl_error;
2146
2147 #ifdef ECHOCARD_HAS_DIGITAL_MODE_SWITCH
2148         /* Creates a list of available digital modes */
2149         chip->num_digital_modes = 0;
2150         for (i = 0; i < 6; i++)
2151                 if (chip->digital_modes & (1 << i))
2152                         chip->digital_mode_list[chip->num_digital_modes++] = i;
2153
2154         if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_digital_mode_switch, chip))) < 0)
2155                 goto ctl_error;
2156 #endif /* ECHOCARD_HAS_DIGITAL_MODE_SWITCH */
2157
2158 #ifdef ECHOCARD_HAS_EXTERNAL_CLOCK
2159         /* Creates a list of available clock sources */
2160         chip->num_clock_sources = 0;
2161         for (i = 0; i < 10; i++)
2162                 if (chip->input_clock_types & (1 << i))
2163                         chip->clock_source_list[chip->num_clock_sources++] = i;
2164
2165         if (chip->num_clock_sources > 1) {
2166                 chip->clock_src_ctl = snd_ctl_new1(&snd_echo_clock_source_switch, chip);
2167                 if ((err = snd_ctl_add(chip->card, chip->clock_src_ctl)) < 0)
2168                         goto ctl_error;
2169         }
2170 #endif /* ECHOCARD_HAS_EXTERNAL_CLOCK */
2171
2172 #ifdef ECHOCARD_HAS_DIGITAL_IO
2173         if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_spdif_mode_switch, chip))) < 0)
2174                 goto ctl_error;
2175 #endif
2176
2177 #ifdef ECHOCARD_HAS_PHANTOM_POWER
2178         if (chip->has_phantom_power)
2179                 if ((err = snd_ctl_add(chip->card, snd_ctl_new1(&snd_echo_phantom_power_switch, chip))) < 0)
2180                         goto ctl_error;
2181 #endif
2182
2183         err = snd_card_register(card);
2184         if (err < 0)
2185                 goto ctl_error;
2186         dev_info(card->dev, "Card registered: %s\n", card->longname);
2187
2188         pci_set_drvdata(pci, chip);
2189         dev++;
2190         return 0;
2191
2192 ctl_error:
2193         dev_err(card->dev, "new control error %d\n", err);
2194         snd_card_free(card);
2195         return err;
2196 }
2197
2198
2199
2200 #if defined(CONFIG_PM_SLEEP)
2201
2202 static int snd_echo_suspend(struct device *dev)
2203 {
2204         struct pci_dev *pci = to_pci_dev(dev);
2205         struct echoaudio *chip = dev_get_drvdata(dev);
2206
2207         DE_INIT(("suspend start\n"));
2208         snd_pcm_suspend_all(chip->analog_pcm);
2209         snd_pcm_suspend_all(chip->digital_pcm);
2210
2211 #ifdef ECHOCARD_HAS_MIDI
2212         /* This call can sleep */
2213         if (chip->midi_out)
2214                 snd_echo_midi_output_trigger(chip->midi_out, 0);
2215 #endif
2216         spin_lock_irq(&chip->lock);
2217         if (wait_handshake(chip)) {
2218                 spin_unlock_irq(&chip->lock);
2219                 return -EIO;
2220         }
2221         clear_handshake(chip);
2222         if (send_vector(chip, DSP_VC_GO_COMATOSE) < 0) {
2223                 spin_unlock_irq(&chip->lock);
2224                 return -EIO;
2225         }
2226         spin_unlock_irq(&chip->lock);
2227
2228         chip->dsp_code = NULL;
2229         free_irq(chip->irq, chip);
2230         chip->irq = -1;
2231         pci_save_state(pci);
2232         pci_disable_device(pci);
2233
2234         DE_INIT(("suspend done\n"));
2235         return 0;
2236 }
2237
2238
2239
2240 static int snd_echo_resume(struct device *dev)
2241 {
2242         struct pci_dev *pci = to_pci_dev(dev);
2243         struct echoaudio *chip = dev_get_drvdata(dev);
2244         struct comm_page *commpage, *commpage_bak;
2245         u32 pipe_alloc_mask;
2246         int err;
2247
2248         DE_INIT(("resume start\n"));
2249         pci_restore_state(pci);
2250         commpage_bak = kmalloc(sizeof(struct echoaudio), GFP_KERNEL);
2251         if (commpage_bak == NULL)
2252                 return -ENOMEM;
2253         commpage = chip->comm_page;
2254         memcpy(commpage_bak, commpage, sizeof(struct comm_page));
2255
2256         err = init_hw(chip, chip->pci->device, chip->pci->subsystem_device);
2257         if (err < 0) {
2258                 kfree(commpage_bak);
2259                 DE_INIT(("resume init_hw err=%d\n", err));
2260                 snd_echo_free(chip);
2261                 return err;
2262         }
2263         DE_INIT(("resume init OK\n"));
2264
2265         /* Temporarily set chip->pipe_alloc_mask=0 otherwise
2266          * restore_dsp_settings() fails.
2267          */
2268         pipe_alloc_mask = chip->pipe_alloc_mask;
2269         chip->pipe_alloc_mask = 0;
2270         err = restore_dsp_rettings(chip);
2271         chip->pipe_alloc_mask = pipe_alloc_mask;
2272         if (err < 0) {
2273                 kfree(commpage_bak);
2274                 return err;
2275         }
2276         DE_INIT(("resume restore OK\n"));
2277
2278         memcpy(&commpage->audio_format, &commpage_bak->audio_format,
2279                 sizeof(commpage->audio_format));
2280         memcpy(&commpage->sglist_addr, &commpage_bak->sglist_addr,
2281                 sizeof(commpage->sglist_addr));
2282         memcpy(&commpage->midi_output, &commpage_bak->midi_output,
2283                 sizeof(commpage->midi_output));
2284         kfree(commpage_bak);
2285
2286         if (request_irq(pci->irq, snd_echo_interrupt, IRQF_SHARED,
2287                         KBUILD_MODNAME, chip)) {
2288                 dev_err(chip->card->dev, "cannot grab irq\n");
2289                 snd_echo_free(chip);
2290                 return -EBUSY;
2291         }
2292         chip->irq = pci->irq;
2293         DE_INIT(("resume irq=%d\n", chip->irq));
2294
2295 #ifdef ECHOCARD_HAS_MIDI
2296         if (chip->midi_input_enabled)
2297                 enable_midi_input(chip, TRUE);
2298         if (chip->midi_out)
2299                 snd_echo_midi_output_trigger(chip->midi_out, 1);
2300 #endif
2301
2302         DE_INIT(("resume done\n"));
2303         return 0;
2304 }
2305
2306 static SIMPLE_DEV_PM_OPS(snd_echo_pm, snd_echo_suspend, snd_echo_resume);
2307 #define SND_ECHO_PM_OPS &snd_echo_pm
2308 #else
2309 #define SND_ECHO_PM_OPS NULL
2310 #endif /* CONFIG_PM_SLEEP */
2311
2312
2313 static void snd_echo_remove(struct pci_dev *pci)
2314 {
2315         struct echoaudio *chip;
2316
2317         chip = pci_get_drvdata(pci);
2318         if (chip)
2319                 snd_card_free(chip->card);
2320 }
2321
2322
2323
2324 /******************************************************************************
2325         Everything starts and ends here
2326 ******************************************************************************/
2327
2328 /* pci_driver definition */
2329 static struct pci_driver echo_driver = {
2330         .name = KBUILD_MODNAME,
2331         .id_table = snd_echo_ids,
2332         .probe = snd_echo_probe,
2333         .remove = snd_echo_remove,
2334         .driver = {
2335                 .pm = SND_ECHO_PM_OPS,
2336         },
2337 };
2338
2339 module_pci_driver(echo_driver);