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