ALSA: asihpi: Fix unlocked snd_pcm_stop() call
[pandora-kernel.git] / sound / pci / asihpi / asihpi.c
1 /*
2  *  Asihpi soundcard
3  *  Copyright (c) by AudioScience Inc <alsa@audioscience.com>
4  *
5  *   This program is free software; you can redistribute it and/or modify
6  *   it under the terms of version 2 of the GNU General Public License as
7  *   published by the Free Software Foundation;
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  *  The following is not a condition of use, merely a request:
20  *  If you modify this program, particularly if you fix errors, AudioScience Inc
21  *  would appreciate it if you grant us the right to use those modifications
22  *  for any purpose including commercial applications.
23  */
24
25 #include "hpi_internal.h"
26 #include "hpimsginit.h"
27 #include "hpioctl.h"
28
29 #include <linux/pci.h>
30 #include <linux/init.h>
31 #include <linux/jiffies.h>
32 #include <linux/slab.h>
33 #include <linux/time.h>
34 #include <linux/wait.h>
35 #include <linux/module.h>
36 #include <sound/core.h>
37 #include <sound/control.h>
38 #include <sound/pcm.h>
39 #include <sound/pcm_params.h>
40 #include <sound/info.h>
41 #include <sound/initval.h>
42 #include <sound/tlv.h>
43 #include <sound/hwdep.h>
44
45 MODULE_LICENSE("GPL");
46 MODULE_AUTHOR("AudioScience inc. <support@audioscience.com>");
47 MODULE_DESCRIPTION("AudioScience ALSA ASI5000 ASI6000 ASI87xx ASI89xx");
48
49 #if defined CONFIG_SND_DEBUG_VERBOSE
50 /**
51  * snd_printddd - very verbose debug printk
52  * @format: format string
53  *
54  * Works like snd_printk() for debugging purposes.
55  * Ignored when CONFIG_SND_DEBUG_VERBOSE is not set.
56  * Must set snd module debug parameter to 3 to enable at runtime.
57  */
58 #define snd_printddd(format, args...) \
59         __snd_printk(3, __FILE__, __LINE__, format, ##args)
60 #else
61 #define snd_printddd(format, args...) do { } while (0)
62 #endif
63
64 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;      /* index 0-MAX */
65 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;       /* ID for this card */
66 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
67 static int enable_hpi_hwdep = 1;
68
69 module_param_array(index, int, NULL, S_IRUGO);
70 MODULE_PARM_DESC(index, "ALSA index value for AudioScience soundcard.");
71
72 module_param_array(id, charp, NULL, S_IRUGO);
73 MODULE_PARM_DESC(id, "ALSA ID string for AudioScience soundcard.");
74
75 module_param_array(enable, bool, NULL, S_IRUGO);
76 MODULE_PARM_DESC(enable, "ALSA enable AudioScience soundcard.");
77
78 module_param(enable_hpi_hwdep, bool, S_IRUGO|S_IWUSR);
79 MODULE_PARM_DESC(enable_hpi_hwdep,
80                 "ALSA enable HPI hwdep for AudioScience soundcard ");
81
82 /* identify driver */
83 #ifdef KERNEL_ALSA_BUILD
84 static char *build_info = "Built using headers from kernel source";
85 module_param(build_info, charp, S_IRUGO);
86 MODULE_PARM_DESC(build_info, "built using headers from kernel source");
87 #else
88 static char *build_info = "Built within ALSA source";
89 module_param(build_info, charp, S_IRUGO);
90 MODULE_PARM_DESC(build_info, "built within ALSA source");
91 #endif
92
93 /* set to 1 to dump every control from adapter to log */
94 static const int mixer_dump;
95
96 #define DEFAULT_SAMPLERATE 44100
97 static int adapter_fs = DEFAULT_SAMPLERATE;
98
99 /* defaults */
100 #define PERIODS_MIN 2
101 #define PERIOD_BYTES_MIN  2048
102 #define BUFFER_BYTES_MAX (512 * 1024)
103
104 #define MAX_CLOCKSOURCES (HPI_SAMPLECLOCK_SOURCE_LAST + 1 + 7)
105
106 struct clk_source {
107         int source;
108         int index;
109         char *name;
110 };
111
112 struct clk_cache {
113         int count;
114         int has_local;
115         struct clk_source s[MAX_CLOCKSOURCES];
116 };
117
118 /* Per card data */
119 struct snd_card_asihpi {
120         struct snd_card *card;
121         struct pci_dev *pci;
122         u16 adapter_index;
123         u32 serial_number;
124         u16 type;
125         u16 version;
126         u16 num_outstreams;
127         u16 num_instreams;
128
129         u32 h_mixer;
130         struct clk_cache cc;
131
132         u16 can_dma;
133         u16 support_grouping;
134         u16 support_mrx;
135         u16 update_interval_frames;
136         u16 in_max_chans;
137         u16 out_max_chans;
138 };
139
140 /* Per stream data */
141 struct snd_card_asihpi_pcm {
142         struct timer_list timer;
143         unsigned int respawn_timer;
144         unsigned int hpi_buffer_attached;
145         unsigned int buffer_bytes;
146         unsigned int period_bytes;
147         unsigned int bytes_per_sec;
148         unsigned int pcm_buf_host_rw_ofs; /* Host R/W pos */
149         unsigned int pcm_buf_dma_ofs;   /* DMA R/W offset in buffer */
150         unsigned int pcm_buf_elapsed_dma_ofs;   /* DMA R/W offset in buffer */
151         unsigned int drained_count;
152         struct snd_pcm_substream *substream;
153         u32 h_stream;
154         struct hpi_format format;
155 };
156
157 /* universal stream verbs work with out or in stream handles */
158
159 /* Functions to allow driver to give a buffer to HPI for busmastering */
160
161 static u16 hpi_stream_host_buffer_attach(
162         u32 h_stream,   /* handle to outstream. */
163         u32 size_in_bytes, /* size in bytes of bus mastering buffer */
164         u32 pci_address
165 )
166 {
167         struct hpi_message hm;
168         struct hpi_response hr;
169         unsigned int obj = hpi_handle_object(h_stream);
170
171         if (!h_stream)
172                 return HPI_ERROR_INVALID_OBJ;
173         hpi_init_message_response(&hm, &hr, obj,
174                         obj == HPI_OBJ_OSTREAM ?
175                                 HPI_OSTREAM_HOSTBUFFER_ALLOC :
176                                 HPI_ISTREAM_HOSTBUFFER_ALLOC);
177
178         hpi_handle_to_indexes(h_stream, &hm.adapter_index,
179                                 &hm.obj_index);
180
181         hm.u.d.u.buffer.buffer_size = size_in_bytes;
182         hm.u.d.u.buffer.pci_address = pci_address;
183         hm.u.d.u.buffer.command = HPI_BUFFER_CMD_INTERNAL_GRANTADAPTER;
184         hpi_send_recv(&hm, &hr);
185         return hr.error;
186 }
187
188 static u16 hpi_stream_host_buffer_detach(u32  h_stream)
189 {
190         struct hpi_message hm;
191         struct hpi_response hr;
192         unsigned int obj = hpi_handle_object(h_stream);
193
194         if (!h_stream)
195                 return HPI_ERROR_INVALID_OBJ;
196
197         hpi_init_message_response(&hm, &hr,  obj,
198                         obj == HPI_OBJ_OSTREAM ?
199                                 HPI_OSTREAM_HOSTBUFFER_FREE :
200                                 HPI_ISTREAM_HOSTBUFFER_FREE);
201
202         hpi_handle_to_indexes(h_stream, &hm.adapter_index,
203                                 &hm.obj_index);
204         hm.u.d.u.buffer.command = HPI_BUFFER_CMD_INTERNAL_REVOKEADAPTER;
205         hpi_send_recv(&hm, &hr);
206         return hr.error;
207 }
208
209 static inline u16 hpi_stream_start(u32 h_stream)
210 {
211         if (hpi_handle_object(h_stream) ==  HPI_OBJ_OSTREAM)
212                 return hpi_outstream_start(h_stream);
213         else
214                 return hpi_instream_start(h_stream);
215 }
216
217 static inline u16 hpi_stream_stop(u32 h_stream)
218 {
219         if (hpi_handle_object(h_stream) ==  HPI_OBJ_OSTREAM)
220                 return hpi_outstream_stop(h_stream);
221         else
222                 return hpi_instream_stop(h_stream);
223 }
224
225 static inline u16 hpi_stream_get_info_ex(
226     u32 h_stream,
227     u16        *pw_state,
228     u32        *pbuffer_size,
229     u32        *pdata_in_buffer,
230     u32        *psample_count,
231     u32        *pauxiliary_data
232 )
233 {
234         u16 e;
235         if (hpi_handle_object(h_stream)  ==  HPI_OBJ_OSTREAM)
236                 e = hpi_outstream_get_info_ex(h_stream, pw_state,
237                                         pbuffer_size, pdata_in_buffer,
238                                         psample_count, pauxiliary_data);
239         else
240                 e = hpi_instream_get_info_ex(h_stream, pw_state,
241                                         pbuffer_size, pdata_in_buffer,
242                                         psample_count, pauxiliary_data);
243         return e;
244 }
245
246 static inline u16 hpi_stream_group_add(
247                                         u32 h_master,
248                                         u32 h_stream)
249 {
250         if (hpi_handle_object(h_master) ==  HPI_OBJ_OSTREAM)
251                 return hpi_outstream_group_add(h_master, h_stream);
252         else
253                 return hpi_instream_group_add(h_master, h_stream);
254 }
255
256 static inline u16 hpi_stream_group_reset(u32 h_stream)
257 {
258         if (hpi_handle_object(h_stream) ==  HPI_OBJ_OSTREAM)
259                 return hpi_outstream_group_reset(h_stream);
260         else
261                 return hpi_instream_group_reset(h_stream);
262 }
263
264 static inline u16 hpi_stream_group_get_map(
265                                 u32 h_stream, u32 *mo, u32 *mi)
266 {
267         if (hpi_handle_object(h_stream) ==  HPI_OBJ_OSTREAM)
268                 return hpi_outstream_group_get_map(h_stream, mo, mi);
269         else
270                 return hpi_instream_group_get_map(h_stream, mo, mi);
271 }
272
273 static u16 handle_error(u16 err, int line, char *filename)
274 {
275         if (err)
276                 printk(KERN_WARNING
277                         "in file %s, line %d: HPI error %d\n",
278                         filename, line, err);
279         return err;
280 }
281
282 #define hpi_handle_error(x)  handle_error(x, __LINE__, __FILE__)
283
284 /***************************** GENERAL PCM ****************/
285
286 static void print_hwparams(struct snd_pcm_substream *substream,
287                                 struct snd_pcm_hw_params *p)
288 {
289         char name[16];
290         snd_pcm_debug_name(substream, name, sizeof(name));
291         snd_printd("%s HWPARAMS\n", name);
292         snd_printd(" samplerate %d Hz\n", params_rate(p));
293         snd_printd(" channels %d\n", params_channels(p));
294         snd_printd(" format %d\n", params_format(p));
295         snd_printd(" subformat %d\n", params_subformat(p));
296         snd_printd(" buffer %d B\n", params_buffer_bytes(p));
297         snd_printd(" period %d B\n", params_period_bytes(p));
298         snd_printd(" access %d\n", params_access(p));
299         snd_printd(" period_size %d\n", params_period_size(p));
300         snd_printd(" periods %d\n", params_periods(p));
301         snd_printd(" buffer_size %d\n", params_buffer_size(p));
302         snd_printd(" %d B/s\n", params_rate(p) *
303                 params_channels(p) *
304                 snd_pcm_format_width(params_format(p)) / 8);
305
306 }
307
308 static snd_pcm_format_t hpi_to_alsa_formats[] = {
309         -1,                     /* INVALID */
310         SNDRV_PCM_FORMAT_U8,    /* HPI_FORMAT_PCM8_UNSIGNED        1 */
311         SNDRV_PCM_FORMAT_S16,   /* HPI_FORMAT_PCM16_SIGNED         2 */
312         -1,                     /* HPI_FORMAT_MPEG_L1              3 */
313         SNDRV_PCM_FORMAT_MPEG,  /* HPI_FORMAT_MPEG_L2              4 */
314         SNDRV_PCM_FORMAT_MPEG,  /* HPI_FORMAT_MPEG_L3              5 */
315         -1,                     /* HPI_FORMAT_DOLBY_AC2            6 */
316         -1,                     /* HPI_FORMAT_DOLBY_AC3            7 */
317         SNDRV_PCM_FORMAT_S16_BE,/* HPI_FORMAT_PCM16_BIGENDIAN      8 */
318         -1,                     /* HPI_FORMAT_AA_TAGIT1_HITS       9 */
319         -1,                     /* HPI_FORMAT_AA_TAGIT1_INSERTS   10 */
320         SNDRV_PCM_FORMAT_S32,   /* HPI_FORMAT_PCM32_SIGNED        11 */
321         -1,                     /* HPI_FORMAT_RAW_BITSTREAM       12 */
322         -1,                     /* HPI_FORMAT_AA_TAGIT1_HITS_EX1  13 */
323         SNDRV_PCM_FORMAT_FLOAT, /* HPI_FORMAT_PCM32_FLOAT         14 */
324 #if 1
325         /* ALSA can't handle 3 byte sample size together with power-of-2
326          *  constraint on buffer_bytes, so disable this format
327          */
328         -1
329 #else
330         /* SNDRV_PCM_FORMAT_S24_3LE */ /* HPI_FORMAT_PCM24_SIGNED 15 */
331 #endif
332 };
333
334
335 static int snd_card_asihpi_format_alsa2hpi(snd_pcm_format_t alsa_format,
336                                            u16 *hpi_format)
337 {
338         u16 format;
339
340         for (format = HPI_FORMAT_PCM8_UNSIGNED;
341              format <= HPI_FORMAT_PCM24_SIGNED; format++) {
342                 if (hpi_to_alsa_formats[format] == alsa_format) {
343                         *hpi_format = format;
344                         return 0;
345                 }
346         }
347
348         snd_printd(KERN_WARNING "failed match for alsa format %d\n",
349                    alsa_format);
350         *hpi_format = 0;
351         return -EINVAL;
352 }
353
354 static void snd_card_asihpi_pcm_samplerates(struct snd_card_asihpi *asihpi,
355                                          struct snd_pcm_hardware *pcmhw)
356 {
357         u16 err;
358         u32 h_control;
359         u32 sample_rate;
360         int idx;
361         unsigned int rate_min = 200000;
362         unsigned int rate_max = 0;
363         unsigned int rates = 0;
364
365         if (asihpi->support_mrx) {
366                 rates |= SNDRV_PCM_RATE_CONTINUOUS;
367                 rates |= SNDRV_PCM_RATE_8000_96000;
368                 rate_min = 8000;
369                 rate_max = 100000;
370         } else {
371                 /* on cards without SRC,
372                    valid rates are determined by sampleclock */
373                 err = hpi_mixer_get_control(asihpi->h_mixer,
374                                           HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
375                                           HPI_CONTROL_SAMPLECLOCK, &h_control);
376                 if (err) {
377                         snd_printk(KERN_ERR
378                                 "No local sampleclock, err %d\n", err);
379                 }
380
381                 for (idx = -1; idx < 100; idx++) {
382                         if (idx == -1) {
383                                 if (hpi_sample_clock_get_sample_rate(h_control,
384                                                                 &sample_rate))
385                                         continue;
386                         } else if (hpi_sample_clock_query_local_rate(h_control,
387                                                         idx, &sample_rate)) {
388                                 break;
389                         }
390
391                         rate_min = min(rate_min, sample_rate);
392                         rate_max = max(rate_max, sample_rate);
393
394                         switch (sample_rate) {
395                         case 5512:
396                                 rates |= SNDRV_PCM_RATE_5512;
397                                 break;
398                         case 8000:
399                                 rates |= SNDRV_PCM_RATE_8000;
400                                 break;
401                         case 11025:
402                                 rates |= SNDRV_PCM_RATE_11025;
403                                 break;
404                         case 16000:
405                                 rates |= SNDRV_PCM_RATE_16000;
406                                 break;
407                         case 22050:
408                                 rates |= SNDRV_PCM_RATE_22050;
409                                 break;
410                         case 32000:
411                                 rates |= SNDRV_PCM_RATE_32000;
412                                 break;
413                         case 44100:
414                                 rates |= SNDRV_PCM_RATE_44100;
415                                 break;
416                         case 48000:
417                                 rates |= SNDRV_PCM_RATE_48000;
418                                 break;
419                         case 64000:
420                                 rates |= SNDRV_PCM_RATE_64000;
421                                 break;
422                         case 88200:
423                                 rates |= SNDRV_PCM_RATE_88200;
424                                 break;
425                         case 96000:
426                                 rates |= SNDRV_PCM_RATE_96000;
427                                 break;
428                         case 176400:
429                                 rates |= SNDRV_PCM_RATE_176400;
430                                 break;
431                         case 192000:
432                                 rates |= SNDRV_PCM_RATE_192000;
433                                 break;
434                         default: /* some other rate */
435                                 rates |= SNDRV_PCM_RATE_KNOT;
436                         }
437                 }
438         }
439
440         pcmhw->rates = rates;
441         pcmhw->rate_min = rate_min;
442         pcmhw->rate_max = rate_max;
443 }
444
445 static int snd_card_asihpi_pcm_hw_params(struct snd_pcm_substream *substream,
446                                          struct snd_pcm_hw_params *params)
447 {
448         struct snd_pcm_runtime *runtime = substream->runtime;
449         struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
450         struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
451         int err;
452         u16 format;
453         int width;
454         unsigned int bytes_per_sec;
455
456         print_hwparams(substream, params);
457         err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
458         if (err < 0)
459                 return err;
460         err = snd_card_asihpi_format_alsa2hpi(params_format(params), &format);
461         if (err)
462                 return err;
463
464         hpi_handle_error(hpi_format_create(&dpcm->format,
465                         params_channels(params),
466                         format, params_rate(params), 0, 0));
467
468         if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
469                 if (hpi_instream_reset(dpcm->h_stream) != 0)
470                         return -EINVAL;
471
472                 if (hpi_instream_set_format(
473                         dpcm->h_stream, &dpcm->format) != 0)
474                         return -EINVAL;
475         }
476
477         dpcm->hpi_buffer_attached = 0;
478         if (card->can_dma) {
479                 err = hpi_stream_host_buffer_attach(dpcm->h_stream,
480                         params_buffer_bytes(params),  runtime->dma_addr);
481                 if (err == 0) {
482                         snd_printdd(
483                                 "stream_host_buffer_attach succeeded %u %lu\n",
484                                 params_buffer_bytes(params),
485                                 (unsigned long)runtime->dma_addr);
486                 } else {
487                         snd_printd("stream_host_buffer_attach error %d\n",
488                                         err);
489                         return -ENOMEM;
490                 }
491
492                 err = hpi_stream_get_info_ex(dpcm->h_stream, NULL,
493                                                 &dpcm->hpi_buffer_attached,
494                                                 NULL, NULL, NULL);
495
496                 snd_printdd("stream_host_buffer_attach status 0x%x\n",
497                                 dpcm->hpi_buffer_attached);
498         }
499         bytes_per_sec = params_rate(params) * params_channels(params);
500         width = snd_pcm_format_width(params_format(params));
501         bytes_per_sec *= width;
502         bytes_per_sec /= 8;
503         if (width < 0 || bytes_per_sec == 0)
504                 return -EINVAL;
505
506         dpcm->bytes_per_sec = bytes_per_sec;
507         dpcm->buffer_bytes = params_buffer_bytes(params);
508         dpcm->period_bytes = params_period_bytes(params);
509
510         return 0;
511 }
512
513 static int
514 snd_card_asihpi_hw_free(struct snd_pcm_substream *substream)
515 {
516         struct snd_pcm_runtime *runtime = substream->runtime;
517         struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
518         if (dpcm->hpi_buffer_attached)
519                 hpi_stream_host_buffer_detach(dpcm->h_stream);
520
521         snd_pcm_lib_free_pages(substream);
522         return 0;
523 }
524
525 static void snd_card_asihpi_runtime_free(struct snd_pcm_runtime *runtime)
526 {
527         struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
528         kfree(dpcm);
529 }
530
531 static void snd_card_asihpi_pcm_timer_start(struct snd_pcm_substream *
532                                             substream)
533 {
534         struct snd_pcm_runtime *runtime = substream->runtime;
535         struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
536         int expiry;
537
538         expiry = HZ / 200;
539         /*? (dpcm->period_bytes * HZ / dpcm->bytes_per_sec); */
540         expiry = max(expiry, 1); /* don't let it be zero! */
541         dpcm->timer.expires = jiffies + expiry;
542         dpcm->respawn_timer = 1;
543         add_timer(&dpcm->timer);
544 }
545
546 static void snd_card_asihpi_pcm_timer_stop(struct snd_pcm_substream *substream)
547 {
548         struct snd_pcm_runtime *runtime = substream->runtime;
549         struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
550
551         dpcm->respawn_timer = 0;
552         del_timer(&dpcm->timer);
553 }
554
555 static int snd_card_asihpi_trigger(struct snd_pcm_substream *substream,
556                                            int cmd)
557 {
558         struct snd_card_asihpi_pcm *dpcm = substream->runtime->private_data;
559         struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
560         struct snd_pcm_substream *s;
561         u16 e;
562         char name[16];
563
564         snd_pcm_debug_name(substream, name, sizeof(name));
565         snd_printdd("%s trigger\n", name);
566
567         switch (cmd) {
568         case SNDRV_PCM_TRIGGER_START:
569                 snd_pcm_group_for_each_entry(s, substream) {
570                         struct snd_pcm_runtime *runtime = s->runtime;
571                         struct snd_card_asihpi_pcm *ds = runtime->private_data;
572
573                         if (snd_pcm_substream_chip(s) != card)
574                                 continue;
575
576                         /* don't link Cap and Play */
577                         if (substream->stream != s->stream)
578                                 continue;
579
580                         ds->drained_count = 0;
581                         if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
582                                 /* How do I know how much valid data is present
583                                 * in buffer? Must be at least one period!
584                                 * Guessing 2 periods, but if
585                                 * buffer is bigger it may contain even more
586                                 * data??
587                                 */
588                                 unsigned int preload = ds->period_bytes * 1;
589                                 snd_printddd("%d preload x%x\n", s->number, preload);
590                                 hpi_handle_error(hpi_outstream_write_buf(
591                                                 ds->h_stream,
592                                                 &runtime->dma_area[0],
593                                                 preload,
594                                                 &ds->format));
595                                 ds->pcm_buf_host_rw_ofs = preload;
596                         }
597
598                         if (card->support_grouping) {
599                                 snd_printdd("%d group\n", s->number);
600                                 e = hpi_stream_group_add(
601                                         dpcm->h_stream,
602                                         ds->h_stream);
603                                 if (!e) {
604                                         snd_pcm_trigger_done(s, substream);
605                                 } else {
606                                         hpi_handle_error(e);
607                                         break;
608                                 }
609                         } else
610                                 break;
611                 }
612                 snd_printdd("start\n");
613                 /* start the master stream */
614                 snd_card_asihpi_pcm_timer_start(substream);
615                 if ((substream->stream == SNDRV_PCM_STREAM_CAPTURE) ||
616                         !card->can_dma)
617                         hpi_handle_error(hpi_stream_start(dpcm->h_stream));
618                 break;
619
620         case SNDRV_PCM_TRIGGER_STOP:
621                 snd_card_asihpi_pcm_timer_stop(substream);
622                 snd_pcm_group_for_each_entry(s, substream) {
623                         if (snd_pcm_substream_chip(s) != card)
624                                 continue;
625                         /* don't link Cap and Play */
626                         if (substream->stream != s->stream)
627                                 continue;
628
629                         /*? workaround linked streams don't
630                         transition to SETUP 20070706*/
631                         s->runtime->status->state = SNDRV_PCM_STATE_SETUP;
632
633                         if (card->support_grouping) {
634                                 snd_printdd("%d group\n", s->number);
635                                 snd_pcm_trigger_done(s, substream);
636                         } else
637                                 break;
638                 }
639                 snd_printdd("stop\n");
640
641                 /* _prepare and _hwparams reset the stream */
642                 hpi_handle_error(hpi_stream_stop(dpcm->h_stream));
643                 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
644                         hpi_handle_error(
645                                 hpi_outstream_reset(dpcm->h_stream));
646
647                 if (card->support_grouping)
648                         hpi_handle_error(hpi_stream_group_reset(dpcm->h_stream));
649                 break;
650
651         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
652                 snd_printdd("pause release\n");
653                 hpi_handle_error(hpi_stream_start(dpcm->h_stream));
654                 snd_card_asihpi_pcm_timer_start(substream);
655                 break;
656         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
657                 snd_printdd("pause\n");
658                 snd_card_asihpi_pcm_timer_stop(substream);
659                 hpi_handle_error(hpi_stream_stop(dpcm->h_stream));
660                 break;
661         default:
662                 snd_printd(KERN_ERR "\tINVALID\n");
663                 return -EINVAL;
664         }
665
666         return 0;
667 }
668
669 /*algorithm outline
670  Without linking degenerates to getting single stream pos etc
671  Without mmap 2nd loop degenerates to snd_pcm_period_elapsed
672 */
673 /*
674 pcm_buf_dma_ofs=get_buf_pos(s);
675 for_each_linked_stream(s) {
676         pcm_buf_dma_ofs=get_buf_pos(s);
677         min_buf_pos = modulo_min(min_buf_pos, pcm_buf_dma_ofs, buffer_bytes)
678         new_data = min(new_data, calc_new_data(pcm_buf_dma_ofs,irq_pos)
679 }
680 timer.expires = jiffies + predict_next_period_ready(min_buf_pos);
681 for_each_linked_stream(s) {
682         s->pcm_buf_dma_ofs = min_buf_pos;
683         if (new_data > period_bytes) {
684                 if (mmap) {
685                         irq_pos = (irq_pos + period_bytes) % buffer_bytes;
686                         if (playback) {
687                                 write(period_bytes);
688                         } else {
689                                 read(period_bytes);
690                         }
691                 }
692                 snd_pcm_period_elapsed(s);
693         }
694 }
695 */
696
697 /** Minimum of 2 modulo values.  Works correctly when the difference between
698 * the values is less than half the modulus
699 */
700 static inline unsigned int modulo_min(unsigned int a, unsigned int b,
701                                         unsigned long int modulus)
702 {
703         unsigned int result;
704         if (((a-b) % modulus) < (modulus/2))
705                 result = b;
706         else
707                 result = a;
708
709         return result;
710 }
711
712 /** Timer function, equivalent to interrupt service routine for cards
713 */
714 static void snd_card_asihpi_timer_function(unsigned long data)
715 {
716         struct snd_card_asihpi_pcm *dpcm = (struct snd_card_asihpi_pcm *)data;
717         struct snd_pcm_substream *substream = dpcm->substream;
718         struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
719         struct snd_pcm_runtime *runtime;
720         struct snd_pcm_substream *s;
721         unsigned int newdata = 0;
722         unsigned int pcm_buf_dma_ofs, min_buf_pos = 0;
723         unsigned int remdata, xfercount, next_jiffies;
724         int first = 1;
725         int loops = 0;
726         u16 state;
727         u32 buffer_size, bytes_avail, samples_played, on_card_bytes;
728         char name[16];
729
730         snd_pcm_debug_name(substream, name, sizeof(name));
731
732         snd_printdd("%s snd_card_asihpi_timer_function\n", name);
733
734         /* find minimum newdata and buffer pos in group */
735         snd_pcm_group_for_each_entry(s, substream) {
736                 struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
737                 runtime = s->runtime;
738
739                 if (snd_pcm_substream_chip(s) != card)
740                         continue;
741
742                 /* don't link Cap and Play */
743                 if (substream->stream != s->stream)
744                         continue;
745
746                 hpi_handle_error(hpi_stream_get_info_ex(
747                                         ds->h_stream, &state,
748                                         &buffer_size, &bytes_avail,
749                                         &samples_played, &on_card_bytes));
750
751                 /* number of bytes in on-card buffer */
752                 runtime->delay = on_card_bytes;
753
754                 if (!card->can_dma)
755                         on_card_bytes = bytes_avail;
756
757                 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
758                         pcm_buf_dma_ofs = ds->pcm_buf_host_rw_ofs - bytes_avail;
759                         if (state == HPI_STATE_STOPPED) {
760                                 if ((bytes_avail == 0) &&
761                                     (on_card_bytes < ds->pcm_buf_host_rw_ofs)) {
762                                         hpi_handle_error(hpi_stream_start(ds->h_stream));
763                                         snd_printdd("P%d start\n", s->number);
764                                         ds->drained_count = 0;
765                                 }
766                         } else if (state == HPI_STATE_DRAINED) {
767                                 snd_printd(KERN_WARNING "P%d drained\n",
768                                                 s->number);
769                                 ds->drained_count++;
770                                 if (ds->drained_count > 2) {
771                                         unsigned long flags;
772                                         snd_pcm_stream_lock_irqsave(s, flags);
773                                         snd_pcm_stop(s, SNDRV_PCM_STATE_XRUN);
774                                         snd_pcm_stream_unlock_irqrestore(s, flags);
775                                         continue;
776                                 }
777                         } else {
778                                 ds->drained_count = 0;
779                         }
780                 } else
781                         pcm_buf_dma_ofs = bytes_avail + ds->pcm_buf_host_rw_ofs;
782
783                 if (first) {
784                         /* can't statically init min when wrap is involved */
785                         min_buf_pos = pcm_buf_dma_ofs;
786                         newdata = (pcm_buf_dma_ofs - ds->pcm_buf_elapsed_dma_ofs) % ds->buffer_bytes;
787                         first = 0;
788                 } else {
789                         min_buf_pos =
790                                 modulo_min(min_buf_pos, pcm_buf_dma_ofs, UINT_MAX+1L);
791                         newdata = min(
792                                 (pcm_buf_dma_ofs - ds->pcm_buf_elapsed_dma_ofs) % ds->buffer_bytes,
793                                 newdata);
794                 }
795
796                 snd_printdd("hw_ptr 0x%04lX, appl_ptr 0x%04lX\n",
797                         (unsigned long)frames_to_bytes(runtime,
798                                                 runtime->status->hw_ptr),
799                         (unsigned long)frames_to_bytes(runtime,
800                                                 runtime->control->appl_ptr));
801
802                 snd_printdd("%d S=%d, "
803                         "rw=0x%04X, dma=0x%04X, left=0x%04X, "
804                         "aux=0x%04X space=0x%04X\n",
805                         s->number, state,
806                         ds->pcm_buf_host_rw_ofs, pcm_buf_dma_ofs,
807                         (int)bytes_avail,
808                         (int)on_card_bytes, buffer_size-bytes_avail);
809                 loops++;
810         }
811         pcm_buf_dma_ofs = min_buf_pos;
812
813         remdata = newdata % dpcm->period_bytes;
814         xfercount = newdata - remdata; /* a multiple of period_bytes */
815         /* come back when on_card_bytes has decreased enough to allow
816            write to happen, or when data has been consumed to make another
817            period
818         */
819         if (xfercount && (on_card_bytes  > dpcm->period_bytes))
820                 next_jiffies = ((on_card_bytes - dpcm->period_bytes) * HZ / dpcm->bytes_per_sec);
821         else
822                 next_jiffies = ((dpcm->period_bytes - remdata) * HZ / dpcm->bytes_per_sec);
823
824         next_jiffies = max(next_jiffies, 1U);
825         dpcm->timer.expires = jiffies + next_jiffies;
826         snd_printdd("jif %d buf pos 0x%04X newdata 0x%04X xfer 0x%04X\n",
827                         next_jiffies, pcm_buf_dma_ofs, newdata, xfercount);
828
829         snd_pcm_group_for_each_entry(s, substream) {
830                 struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
831
832                 /* don't link Cap and Play */
833                 if (substream->stream != s->stream)
834                         continue;
835
836                 ds->pcm_buf_dma_ofs = pcm_buf_dma_ofs;
837
838                 if (xfercount &&
839                         /* Limit use of on card fifo for playback */
840                         ((on_card_bytes <= ds->period_bytes) ||
841                         (s->stream == SNDRV_PCM_STREAM_CAPTURE)))
842
843                 {
844
845                         unsigned int buf_ofs = ds->pcm_buf_host_rw_ofs % ds->buffer_bytes;
846                         unsigned int xfer1, xfer2;
847                         char *pd = &s->runtime->dma_area[buf_ofs];
848
849                         if (card->can_dma) { /* buffer wrap is handled at lower level */
850                                 xfer1 = xfercount;
851                                 xfer2 = 0;
852                         } else {
853                                 xfer1 = min(xfercount, ds->buffer_bytes - buf_ofs);
854                                 xfer2 = xfercount - xfer1;
855                         }
856
857                         if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
858                                 snd_printddd("P%d write1 0x%04X 0x%04X\n",
859                                         s->number, xfer1, buf_ofs);
860                                 hpi_handle_error(
861                                         hpi_outstream_write_buf(
862                                                 ds->h_stream, pd, xfer1,
863                                                 &ds->format));
864
865                                 if (xfer2) {
866                                         pd = s->runtime->dma_area;
867
868                                         snd_printddd("P%d write2 0x%04X 0x%04X\n",
869                                                         s->number,
870                                                         xfercount - xfer1, buf_ofs);
871                                         hpi_handle_error(
872                                                 hpi_outstream_write_buf(
873                                                         ds->h_stream, pd,
874                                                         xfercount - xfer1,
875                                                         &ds->format));
876                                 }
877                         } else {
878                                 snd_printddd("C%d read1 0x%04x\n",
879                                         s->number, xfer1);
880                                 hpi_handle_error(
881                                         hpi_instream_read_buf(
882                                                 ds->h_stream,
883                                                 pd, xfer1));
884                                 if (xfer2) {
885                                         pd = s->runtime->dma_area;
886                                         snd_printddd("C%d read2 0x%04x\n",
887                                                 s->number, xfer2);
888                                         hpi_handle_error(
889                                                 hpi_instream_read_buf(
890                                                         ds->h_stream,
891                                                         pd, xfer2));
892                                 }
893                         }
894                         ds->pcm_buf_host_rw_ofs = ds->pcm_buf_host_rw_ofs + xfercount;
895                         ds->pcm_buf_elapsed_dma_ofs = pcm_buf_dma_ofs;
896                         snd_pcm_period_elapsed(s);
897                 }
898         }
899
900         if (dpcm->respawn_timer)
901                 add_timer(&dpcm->timer);
902 }
903
904 /***************************** PLAYBACK OPS ****************/
905 static int snd_card_asihpi_playback_ioctl(struct snd_pcm_substream *substream,
906                                           unsigned int cmd, void *arg)
907 {
908         snd_printddd(KERN_INFO "P%d ioctl %d\n", substream->number, cmd);
909         return snd_pcm_lib_ioctl(substream, cmd, arg);
910 }
911
912 static int snd_card_asihpi_playback_prepare(struct snd_pcm_substream *
913                                             substream)
914 {
915         struct snd_pcm_runtime *runtime = substream->runtime;
916         struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
917
918         snd_printdd("P%d prepare\n", substream->number);
919
920         hpi_handle_error(hpi_outstream_reset(dpcm->h_stream));
921         dpcm->pcm_buf_host_rw_ofs = 0;
922         dpcm->pcm_buf_dma_ofs = 0;
923         dpcm->pcm_buf_elapsed_dma_ofs = 0;
924         return 0;
925 }
926
927 static snd_pcm_uframes_t
928 snd_card_asihpi_playback_pointer(struct snd_pcm_substream *substream)
929 {
930         struct snd_pcm_runtime *runtime = substream->runtime;
931         struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
932         snd_pcm_uframes_t ptr;
933
934         ptr = bytes_to_frames(runtime, dpcm->pcm_buf_dma_ofs  % dpcm->buffer_bytes);
935         snd_printddd("P%d pointer = 0x%04lx\n", substream->number, (unsigned long)ptr);
936         return ptr;
937 }
938
939 static void snd_card_asihpi_playback_format(struct snd_card_asihpi *asihpi,
940                                                 u32 h_stream,
941                                                 struct snd_pcm_hardware *pcmhw)
942 {
943         struct hpi_format hpi_format;
944         u16 format;
945         u16 err;
946         u32 h_control;
947         u32 sample_rate = 48000;
948
949         /* on cards without SRC, must query at valid rate,
950         * maybe set by external sync
951         */
952         err = hpi_mixer_get_control(asihpi->h_mixer,
953                                   HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
954                                   HPI_CONTROL_SAMPLECLOCK, &h_control);
955
956         if (!err)
957                 err = hpi_sample_clock_get_sample_rate(h_control,
958                                 &sample_rate);
959
960         for (format = HPI_FORMAT_PCM8_UNSIGNED;
961              format <= HPI_FORMAT_PCM24_SIGNED; format++) {
962                 err = hpi_format_create(&hpi_format,
963                                         2, format, sample_rate, 128000, 0);
964                 if (!err)
965                         err = hpi_outstream_query_format(h_stream,
966                                                         &hpi_format);
967                 if (!err && (hpi_to_alsa_formats[format] != -1))
968                         pcmhw->formats |=
969                                 (1ULL << hpi_to_alsa_formats[format]);
970         }
971 }
972
973 static struct snd_pcm_hardware snd_card_asihpi_playback = {
974         .channels_min = 1,
975         .channels_max = 2,
976         .buffer_bytes_max = BUFFER_BYTES_MAX,
977         .period_bytes_min = PERIOD_BYTES_MIN,
978         .period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN,
979         .periods_min = PERIODS_MIN,
980         .periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN,
981         .fifo_size = 0,
982 };
983
984 static int snd_card_asihpi_playback_open(struct snd_pcm_substream *substream)
985 {
986         struct snd_pcm_runtime *runtime = substream->runtime;
987         struct snd_card_asihpi_pcm *dpcm;
988         struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
989         int err;
990
991         dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
992         if (dpcm == NULL)
993                 return -ENOMEM;
994
995         err =
996             hpi_outstream_open(card->adapter_index,
997                               substream->number, &dpcm->h_stream);
998         hpi_handle_error(err);
999         if (err)
1000                 kfree(dpcm);
1001         if (err == HPI_ERROR_OBJ_ALREADY_OPEN)
1002                 return -EBUSY;
1003         if (err)
1004                 return -EIO;
1005
1006         /*? also check ASI5000 samplerate source
1007             If external, only support external rate.
1008             If internal and other stream playing, can't switch
1009         */
1010
1011         init_timer(&dpcm->timer);
1012         dpcm->timer.data = (unsigned long) dpcm;
1013         dpcm->timer.function = snd_card_asihpi_timer_function;
1014         dpcm->substream = substream;
1015         runtime->private_data = dpcm;
1016         runtime->private_free = snd_card_asihpi_runtime_free;
1017
1018         snd_card_asihpi_playback.channels_max = card->out_max_chans;
1019         /*?snd_card_asihpi_playback.period_bytes_min =
1020         card->out_max_chans * 4096; */
1021
1022         snd_card_asihpi_playback_format(card, dpcm->h_stream,
1023                                         &snd_card_asihpi_playback);
1024
1025         snd_card_asihpi_pcm_samplerates(card,  &snd_card_asihpi_playback);
1026
1027         snd_card_asihpi_playback.info = SNDRV_PCM_INFO_INTERLEAVED |
1028                                         SNDRV_PCM_INFO_DOUBLE |
1029                                         SNDRV_PCM_INFO_BATCH |
1030                                         SNDRV_PCM_INFO_BLOCK_TRANSFER |
1031                                         SNDRV_PCM_INFO_PAUSE |
1032                                         SNDRV_PCM_INFO_MMAP |
1033                                         SNDRV_PCM_INFO_MMAP_VALID;
1034
1035         if (card->support_grouping)
1036                 snd_card_asihpi_playback.info |= SNDRV_PCM_INFO_SYNC_START;
1037
1038         /* struct is copied, so can create initializer dynamically */
1039         runtime->hw = snd_card_asihpi_playback;
1040
1041         if (card->can_dma)
1042                 err = snd_pcm_hw_constraint_pow2(runtime, 0,
1043                                         SNDRV_PCM_HW_PARAM_BUFFER_BYTES);
1044         if (err < 0)
1045                 return err;
1046
1047         snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1048                 card->update_interval_frames);
1049
1050         snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1051                 card->update_interval_frames * 2, UINT_MAX);
1052
1053         snd_pcm_set_sync(substream);
1054
1055         snd_printdd("playback open\n");
1056
1057         return 0;
1058 }
1059
1060 static int snd_card_asihpi_playback_close(struct snd_pcm_substream *substream)
1061 {
1062         struct snd_pcm_runtime *runtime = substream->runtime;
1063         struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1064
1065         hpi_handle_error(hpi_outstream_close(dpcm->h_stream));
1066         snd_printdd("playback close\n");
1067
1068         return 0;
1069 }
1070
1071 static struct snd_pcm_ops snd_card_asihpi_playback_mmap_ops = {
1072         .open = snd_card_asihpi_playback_open,
1073         .close = snd_card_asihpi_playback_close,
1074         .ioctl = snd_card_asihpi_playback_ioctl,
1075         .hw_params = snd_card_asihpi_pcm_hw_params,
1076         .hw_free = snd_card_asihpi_hw_free,
1077         .prepare = snd_card_asihpi_playback_prepare,
1078         .trigger = snd_card_asihpi_trigger,
1079         .pointer = snd_card_asihpi_playback_pointer,
1080 };
1081
1082 /***************************** CAPTURE OPS ****************/
1083 static snd_pcm_uframes_t
1084 snd_card_asihpi_capture_pointer(struct snd_pcm_substream *substream)
1085 {
1086         struct snd_pcm_runtime *runtime = substream->runtime;
1087         struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1088
1089         snd_printddd("capture pointer %d=%d\n",
1090                         substream->number, dpcm->pcm_buf_dma_ofs);
1091         /* NOTE Unlike playback can't use actual samples_played
1092                 for the capture position, because those samples aren't yet in
1093                 the local buffer available for reading.
1094         */
1095         return bytes_to_frames(runtime, dpcm->pcm_buf_dma_ofs % dpcm->buffer_bytes);
1096 }
1097
1098 static int snd_card_asihpi_capture_ioctl(struct snd_pcm_substream *substream,
1099                                          unsigned int cmd, void *arg)
1100 {
1101         return snd_pcm_lib_ioctl(substream, cmd, arg);
1102 }
1103
1104 static int snd_card_asihpi_capture_prepare(struct snd_pcm_substream *substream)
1105 {
1106         struct snd_pcm_runtime *runtime = substream->runtime;
1107         struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1108
1109         hpi_handle_error(hpi_instream_reset(dpcm->h_stream));
1110         dpcm->pcm_buf_host_rw_ofs = 0;
1111         dpcm->pcm_buf_dma_ofs = 0;
1112         dpcm->pcm_buf_elapsed_dma_ofs = 0;
1113
1114         snd_printdd("Capture Prepare %d\n", substream->number);
1115         return 0;
1116 }
1117
1118
1119
1120 static void snd_card_asihpi_capture_format(struct snd_card_asihpi *asihpi,
1121                                         u32 h_stream,
1122                                          struct snd_pcm_hardware *pcmhw)
1123 {
1124   struct hpi_format hpi_format;
1125         u16 format;
1126         u16 err;
1127         u32 h_control;
1128         u32 sample_rate = 48000;
1129
1130         /* on cards without SRC, must query at valid rate,
1131                 maybe set by external sync */
1132         err = hpi_mixer_get_control(asihpi->h_mixer,
1133                                   HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
1134                                   HPI_CONTROL_SAMPLECLOCK, &h_control);
1135
1136         if (!err)
1137                 err = hpi_sample_clock_get_sample_rate(h_control,
1138                         &sample_rate);
1139
1140         for (format = HPI_FORMAT_PCM8_UNSIGNED;
1141                 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
1142
1143                 err = hpi_format_create(&hpi_format, 2, format,
1144                                 sample_rate, 128000, 0);
1145                 if (!err)
1146                         err = hpi_instream_query_format(h_stream,
1147                                             &hpi_format);
1148                 if (!err)
1149                         pcmhw->formats |=
1150                                 (1ULL << hpi_to_alsa_formats[format]);
1151         }
1152 }
1153
1154
1155 static struct snd_pcm_hardware snd_card_asihpi_capture = {
1156         .channels_min = 1,
1157         .channels_max = 2,
1158         .buffer_bytes_max = BUFFER_BYTES_MAX,
1159         .period_bytes_min = PERIOD_BYTES_MIN,
1160         .period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN,
1161         .periods_min = PERIODS_MIN,
1162         .periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN,
1163         .fifo_size = 0,
1164 };
1165
1166 static int snd_card_asihpi_capture_open(struct snd_pcm_substream *substream)
1167 {
1168         struct snd_pcm_runtime *runtime = substream->runtime;
1169         struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
1170         struct snd_card_asihpi_pcm *dpcm;
1171         int err;
1172
1173         dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
1174         if (dpcm == NULL)
1175                 return -ENOMEM;
1176
1177         snd_printdd("capture open adapter %d stream %d\n",
1178                    card->adapter_index, substream->number);
1179
1180         err = hpi_handle_error(
1181             hpi_instream_open(card->adapter_index,
1182                              substream->number, &dpcm->h_stream));
1183         if (err)
1184                 kfree(dpcm);
1185         if (err == HPI_ERROR_OBJ_ALREADY_OPEN)
1186                 return -EBUSY;
1187         if (err)
1188                 return -EIO;
1189
1190
1191         init_timer(&dpcm->timer);
1192         dpcm->timer.data = (unsigned long) dpcm;
1193         dpcm->timer.function = snd_card_asihpi_timer_function;
1194         dpcm->substream = substream;
1195         runtime->private_data = dpcm;
1196         runtime->private_free = snd_card_asihpi_runtime_free;
1197
1198         snd_card_asihpi_capture.channels_max = card->in_max_chans;
1199         snd_card_asihpi_capture_format(card, dpcm->h_stream,
1200                                        &snd_card_asihpi_capture);
1201         snd_card_asihpi_pcm_samplerates(card,  &snd_card_asihpi_capture);
1202         snd_card_asihpi_capture.info = SNDRV_PCM_INFO_INTERLEAVED |
1203                                         SNDRV_PCM_INFO_MMAP |
1204                                         SNDRV_PCM_INFO_MMAP_VALID;
1205
1206         if (card->support_grouping)
1207                 snd_card_asihpi_capture.info |= SNDRV_PCM_INFO_SYNC_START;
1208
1209         runtime->hw = snd_card_asihpi_capture;
1210
1211         if (card->can_dma)
1212                 err = snd_pcm_hw_constraint_pow2(runtime, 0,
1213                                         SNDRV_PCM_HW_PARAM_BUFFER_BYTES);
1214         if (err < 0)
1215                 return err;
1216
1217         snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1218                 card->update_interval_frames);
1219         snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1220                 card->update_interval_frames * 2, UINT_MAX);
1221
1222         snd_pcm_set_sync(substream);
1223
1224         return 0;
1225 }
1226
1227 static int snd_card_asihpi_capture_close(struct snd_pcm_substream *substream)
1228 {
1229         struct snd_card_asihpi_pcm *dpcm = substream->runtime->private_data;
1230
1231         hpi_handle_error(hpi_instream_close(dpcm->h_stream));
1232         return 0;
1233 }
1234
1235 static struct snd_pcm_ops snd_card_asihpi_capture_mmap_ops = {
1236         .open = snd_card_asihpi_capture_open,
1237         .close = snd_card_asihpi_capture_close,
1238         .ioctl = snd_card_asihpi_capture_ioctl,
1239         .hw_params = snd_card_asihpi_pcm_hw_params,
1240         .hw_free = snd_card_asihpi_hw_free,
1241         .prepare = snd_card_asihpi_capture_prepare,
1242         .trigger = snd_card_asihpi_trigger,
1243         .pointer = snd_card_asihpi_capture_pointer,
1244 };
1245
1246 static int __devinit snd_card_asihpi_pcm_new(struct snd_card_asihpi *asihpi,
1247                                       int device, int substreams)
1248 {
1249         struct snd_pcm *pcm;
1250         int err;
1251
1252         err = snd_pcm_new(asihpi->card, "Asihpi PCM", device,
1253                          asihpi->num_outstreams, asihpi->num_instreams,
1254                          &pcm);
1255         if (err < 0)
1256                 return err;
1257         /* pointer to ops struct is stored, dont change ops afterwards! */
1258                 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1259                                 &snd_card_asihpi_playback_mmap_ops);
1260                 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1261                                 &snd_card_asihpi_capture_mmap_ops);
1262
1263         pcm->private_data = asihpi;
1264         pcm->info_flags = 0;
1265         strcpy(pcm->name, "Asihpi PCM");
1266
1267         /*? do we want to emulate MMAP for non-BBM cards?
1268         Jack doesn't work with ALSAs MMAP emulation - WHY NOT? */
1269         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1270                                                 snd_dma_pci_data(asihpi->pci),
1271                                                 64*1024, BUFFER_BYTES_MAX);
1272
1273         return 0;
1274 }
1275
1276 /***************************** MIXER CONTROLS ****************/
1277 struct hpi_control {
1278         u32 h_control;
1279         u16 control_type;
1280         u16 src_node_type;
1281         u16 src_node_index;
1282         u16 dst_node_type;
1283         u16 dst_node_index;
1284         u16 band;
1285         char name[44]; /* copied to snd_ctl_elem_id.name[44]; */
1286 };
1287
1288 static const char * const asihpi_tuner_band_names[] = {
1289         "invalid",
1290         "AM",
1291         "FM mono",
1292         "TV NTSC-M",
1293         "FM stereo",
1294         "AUX",
1295         "TV PAL BG",
1296         "TV PAL I",
1297         "TV PAL DK",
1298         "TV SECAM",
1299 };
1300
1301 compile_time_assert(
1302         (ARRAY_SIZE(asihpi_tuner_band_names) ==
1303                 (HPI_TUNER_BAND_LAST+1)),
1304         assert_tuner_band_names_size);
1305
1306 static const char * const asihpi_src_names[] = {
1307         "no source",
1308         "PCM",
1309         "Line",
1310         "Digital",
1311         "Tuner",
1312         "RF",
1313         "Clock",
1314         "Bitstream",
1315         "Mic",
1316         "Net",
1317         "Analog",
1318         "Adapter",
1319         "RTP",
1320         "GPI",
1321 };
1322
1323 compile_time_assert(
1324         (ARRAY_SIZE(asihpi_src_names) ==
1325                 (HPI_SOURCENODE_LAST_INDEX-HPI_SOURCENODE_NONE+1)),
1326         assert_src_names_size);
1327
1328 static const char * const asihpi_dst_names[] = {
1329         "no destination",
1330         "PCM",
1331         "Line",
1332         "Digital",
1333         "RF",
1334         "Speaker",
1335         "Net",
1336         "Analog",
1337         "RTP",
1338         "GPO",
1339 };
1340
1341 compile_time_assert(
1342         (ARRAY_SIZE(asihpi_dst_names) ==
1343                 (HPI_DESTNODE_LAST_INDEX-HPI_DESTNODE_NONE+1)),
1344         assert_dst_names_size);
1345
1346 static inline int ctl_add(struct snd_card *card, struct snd_kcontrol_new *ctl,
1347                                 struct snd_card_asihpi *asihpi)
1348 {
1349         int err;
1350
1351         err = snd_ctl_add(card, snd_ctl_new1(ctl, asihpi));
1352         if (err < 0)
1353                 return err;
1354         else if (mixer_dump)
1355                 snd_printk(KERN_INFO "added %s(%d)\n", ctl->name, ctl->index);
1356
1357         return 0;
1358 }
1359
1360 /* Convert HPI control name and location into ALSA control name */
1361 static void asihpi_ctl_init(struct snd_kcontrol_new *snd_control,
1362                                 struct hpi_control *hpi_ctl,
1363                                 char *name)
1364 {
1365         char *dir;
1366         memset(snd_control, 0, sizeof(*snd_control));
1367         snd_control->name = hpi_ctl->name;
1368         snd_control->private_value = hpi_ctl->h_control;
1369         snd_control->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1370         snd_control->index = 0;
1371
1372         if (hpi_ctl->src_node_type + HPI_SOURCENODE_NONE == HPI_SOURCENODE_CLOCK_SOURCE)
1373                 dir = ""; /* clock is neither capture nor playback */
1374         else if (hpi_ctl->dst_node_type + HPI_DESTNODE_NONE == HPI_DESTNODE_ISTREAM)
1375                 dir = "Capture ";  /* On or towards a PCM capture destination*/
1376         else if ((hpi_ctl->src_node_type + HPI_SOURCENODE_NONE != HPI_SOURCENODE_OSTREAM) &&
1377                 (!hpi_ctl->dst_node_type))
1378                 dir = "Capture "; /* On a source node that is not PCM playback */
1379         else if (hpi_ctl->src_node_type &&
1380                 (hpi_ctl->src_node_type + HPI_SOURCENODE_NONE != HPI_SOURCENODE_OSTREAM) &&
1381                 (hpi_ctl->dst_node_type))
1382                 dir = "Monitor Playback "; /* Between an input and an output */
1383         else
1384                 dir = "Playback "; /* PCM Playback source, or  output node */
1385
1386         if (hpi_ctl->src_node_type && hpi_ctl->dst_node_type)
1387                 sprintf(hpi_ctl->name, "%s %d %s %d %s%s",
1388                         asihpi_src_names[hpi_ctl->src_node_type],
1389                         hpi_ctl->src_node_index,
1390                         asihpi_dst_names[hpi_ctl->dst_node_type],
1391                         hpi_ctl->dst_node_index,
1392                         dir, name);
1393         else if (hpi_ctl->dst_node_type) {
1394                 sprintf(hpi_ctl->name, "%s %d %s%s",
1395                 asihpi_dst_names[hpi_ctl->dst_node_type],
1396                 hpi_ctl->dst_node_index,
1397                 dir, name);
1398         } else {
1399                 sprintf(hpi_ctl->name, "%s %d %s%s",
1400                 asihpi_src_names[hpi_ctl->src_node_type],
1401                 hpi_ctl->src_node_index,
1402                 dir, name);
1403         }
1404         /* printk(KERN_INFO "Adding %s %d to %d ",  hpi_ctl->name,
1405                 hpi_ctl->wSrcNodeType, hpi_ctl->wDstNodeType); */
1406 }
1407
1408 /*------------------------------------------------------------
1409    Volume controls
1410  ------------------------------------------------------------*/
1411 #define VOL_STEP_mB 1
1412 static int snd_asihpi_volume_info(struct snd_kcontrol *kcontrol,
1413                                   struct snd_ctl_elem_info *uinfo)
1414 {
1415         u32 h_control = kcontrol->private_value;
1416         u16 err;
1417         /* native gains are in millibels */
1418         short min_gain_mB;
1419         short max_gain_mB;
1420         short step_gain_mB;
1421
1422         err = hpi_volume_query_range(h_control,
1423                         &min_gain_mB, &max_gain_mB, &step_gain_mB);
1424         if (err) {
1425                 max_gain_mB = 0;
1426                 min_gain_mB = -10000;
1427                 step_gain_mB = VOL_STEP_mB;
1428         }
1429
1430         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1431         uinfo->count = 2;
1432         uinfo->value.integer.min = min_gain_mB / VOL_STEP_mB;
1433         uinfo->value.integer.max = max_gain_mB / VOL_STEP_mB;
1434         uinfo->value.integer.step = step_gain_mB / VOL_STEP_mB;
1435         return 0;
1436 }
1437
1438 static int snd_asihpi_volume_get(struct snd_kcontrol *kcontrol,
1439                                  struct snd_ctl_elem_value *ucontrol)
1440 {
1441         u32 h_control = kcontrol->private_value;
1442         short an_gain_mB[HPI_MAX_CHANNELS];
1443
1444         hpi_handle_error(hpi_volume_get_gain(h_control, an_gain_mB));
1445         ucontrol->value.integer.value[0] = an_gain_mB[0] / VOL_STEP_mB;
1446         ucontrol->value.integer.value[1] = an_gain_mB[1] / VOL_STEP_mB;
1447
1448         return 0;
1449 }
1450
1451 static int snd_asihpi_volume_put(struct snd_kcontrol *kcontrol,
1452                                  struct snd_ctl_elem_value *ucontrol)
1453 {
1454         int change;
1455         u32 h_control = kcontrol->private_value;
1456         short an_gain_mB[HPI_MAX_CHANNELS];
1457
1458         an_gain_mB[0] =
1459             (ucontrol->value.integer.value[0]) * VOL_STEP_mB;
1460         an_gain_mB[1] =
1461             (ucontrol->value.integer.value[1]) * VOL_STEP_mB;
1462         /*  change = asihpi->mixer_volume[addr][0] != left ||
1463            asihpi->mixer_volume[addr][1] != right;
1464          */
1465         change = 1;
1466         hpi_handle_error(hpi_volume_set_gain(h_control, an_gain_mB));
1467         return change;
1468 }
1469
1470 static const DECLARE_TLV_DB_SCALE(db_scale_100, -10000, VOL_STEP_mB, 0);
1471
1472 #define snd_asihpi_volume_mute_info     snd_ctl_boolean_mono_info
1473
1474 static int snd_asihpi_volume_mute_get(struct snd_kcontrol *kcontrol,
1475                                  struct snd_ctl_elem_value *ucontrol)
1476 {
1477         u32 h_control = kcontrol->private_value;
1478         u32 mute;
1479
1480         hpi_handle_error(hpi_volume_get_mute(h_control, &mute));
1481         ucontrol->value.integer.value[0] = mute ? 0 : 1;
1482
1483         return 0;
1484 }
1485
1486 static int snd_asihpi_volume_mute_put(struct snd_kcontrol *kcontrol,
1487                                  struct snd_ctl_elem_value *ucontrol)
1488 {
1489         u32 h_control = kcontrol->private_value;
1490         int change = 1;
1491         /* HPI currently only supports all or none muting of multichannel volume
1492         ALSA Switch element has opposite sense to HPI mute: on==unmuted, off=muted
1493         */
1494         int mute =  ucontrol->value.integer.value[0] ? 0 : HPI_BITMASK_ALL_CHANNELS;
1495         hpi_handle_error(hpi_volume_set_mute(h_control, mute));
1496         return change;
1497 }
1498
1499 static int __devinit snd_asihpi_volume_add(struct snd_card_asihpi *asihpi,
1500                                         struct hpi_control *hpi_ctl)
1501 {
1502         struct snd_card *card = asihpi->card;
1503         struct snd_kcontrol_new snd_control;
1504         int err;
1505         u32 mute;
1506
1507         asihpi_ctl_init(&snd_control, hpi_ctl, "Volume");
1508         snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1509                                 SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1510         snd_control.info = snd_asihpi_volume_info;
1511         snd_control.get = snd_asihpi_volume_get;
1512         snd_control.put = snd_asihpi_volume_put;
1513         snd_control.tlv.p = db_scale_100;
1514
1515         err = ctl_add(card, &snd_control, asihpi);
1516         if (err)
1517                 return err;
1518
1519         if (hpi_volume_get_mute(hpi_ctl->h_control, &mute) == 0) {
1520                 asihpi_ctl_init(&snd_control, hpi_ctl, "Switch");
1521                 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1522                 snd_control.info = snd_asihpi_volume_mute_info;
1523                 snd_control.get = snd_asihpi_volume_mute_get;
1524                 snd_control.put = snd_asihpi_volume_mute_put;
1525                 err = ctl_add(card, &snd_control, asihpi);
1526         }
1527         return err;
1528 }
1529
1530 /*------------------------------------------------------------
1531    Level controls
1532  ------------------------------------------------------------*/
1533 static int snd_asihpi_level_info(struct snd_kcontrol *kcontrol,
1534                                  struct snd_ctl_elem_info *uinfo)
1535 {
1536         u32 h_control = kcontrol->private_value;
1537         u16 err;
1538         short min_gain_mB;
1539         short max_gain_mB;
1540         short step_gain_mB;
1541
1542         err =
1543             hpi_level_query_range(h_control, &min_gain_mB,
1544                                &max_gain_mB, &step_gain_mB);
1545         if (err) {
1546                 max_gain_mB = 2400;
1547                 min_gain_mB = -1000;
1548                 step_gain_mB = 100;
1549         }
1550
1551         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1552         uinfo->count = 2;
1553         uinfo->value.integer.min = min_gain_mB / HPI_UNITS_PER_dB;
1554         uinfo->value.integer.max = max_gain_mB / HPI_UNITS_PER_dB;
1555         uinfo->value.integer.step = step_gain_mB / HPI_UNITS_PER_dB;
1556         return 0;
1557 }
1558
1559 static int snd_asihpi_level_get(struct snd_kcontrol *kcontrol,
1560                                 struct snd_ctl_elem_value *ucontrol)
1561 {
1562         u32 h_control = kcontrol->private_value;
1563         short an_gain_mB[HPI_MAX_CHANNELS];
1564
1565         hpi_handle_error(hpi_level_get_gain(h_control, an_gain_mB));
1566         ucontrol->value.integer.value[0] =
1567             an_gain_mB[0] / HPI_UNITS_PER_dB;
1568         ucontrol->value.integer.value[1] =
1569             an_gain_mB[1] / HPI_UNITS_PER_dB;
1570
1571         return 0;
1572 }
1573
1574 static int snd_asihpi_level_put(struct snd_kcontrol *kcontrol,
1575                                 struct snd_ctl_elem_value *ucontrol)
1576 {
1577         int change;
1578         u32 h_control = kcontrol->private_value;
1579         short an_gain_mB[HPI_MAX_CHANNELS];
1580
1581         an_gain_mB[0] =
1582             (ucontrol->value.integer.value[0]) * HPI_UNITS_PER_dB;
1583         an_gain_mB[1] =
1584             (ucontrol->value.integer.value[1]) * HPI_UNITS_PER_dB;
1585         /*  change = asihpi->mixer_level[addr][0] != left ||
1586            asihpi->mixer_level[addr][1] != right;
1587          */
1588         change = 1;
1589         hpi_handle_error(hpi_level_set_gain(h_control, an_gain_mB));
1590         return change;
1591 }
1592
1593 static const DECLARE_TLV_DB_SCALE(db_scale_level, -1000, 100, 0);
1594
1595 static int __devinit snd_asihpi_level_add(struct snd_card_asihpi *asihpi,
1596                                         struct hpi_control *hpi_ctl)
1597 {
1598         struct snd_card *card = asihpi->card;
1599         struct snd_kcontrol_new snd_control;
1600
1601         /* can't use 'volume' cos some nodes have volume as well */
1602         asihpi_ctl_init(&snd_control, hpi_ctl, "Level");
1603         snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1604                                 SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1605         snd_control.info = snd_asihpi_level_info;
1606         snd_control.get = snd_asihpi_level_get;
1607         snd_control.put = snd_asihpi_level_put;
1608         snd_control.tlv.p = db_scale_level;
1609
1610         return ctl_add(card, &snd_control, asihpi);
1611 }
1612
1613 /*------------------------------------------------------------
1614    AESEBU controls
1615  ------------------------------------------------------------*/
1616
1617 /* AESEBU format */
1618 static const char * const asihpi_aesebu_format_names[] = {
1619         "N/A", "S/PDIF", "AES/EBU" };
1620
1621 static int snd_asihpi_aesebu_format_info(struct snd_kcontrol *kcontrol,
1622                                   struct snd_ctl_elem_info *uinfo)
1623 {
1624         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1625         uinfo->count = 1;
1626         uinfo->value.enumerated.items = 3;
1627
1628         if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1629                 uinfo->value.enumerated.item =
1630                         uinfo->value.enumerated.items - 1;
1631
1632         strcpy(uinfo->value.enumerated.name,
1633                 asihpi_aesebu_format_names[uinfo->value.enumerated.item]);
1634
1635         return 0;
1636 }
1637
1638 static int snd_asihpi_aesebu_format_get(struct snd_kcontrol *kcontrol,
1639                         struct snd_ctl_elem_value *ucontrol,
1640                         u16 (*func)(u32, u16 *))
1641 {
1642         u32 h_control = kcontrol->private_value;
1643         u16 source, err;
1644
1645         err = func(h_control, &source);
1646
1647         /* default to N/A */
1648         ucontrol->value.enumerated.item[0] = 0;
1649         /* return success but set the control to N/A */
1650         if (err)
1651                 return 0;
1652         if (source == HPI_AESEBU_FORMAT_SPDIF)
1653                 ucontrol->value.enumerated.item[0] = 1;
1654         if (source == HPI_AESEBU_FORMAT_AESEBU)
1655                 ucontrol->value.enumerated.item[0] = 2;
1656
1657         return 0;
1658 }
1659
1660 static int snd_asihpi_aesebu_format_put(struct snd_kcontrol *kcontrol,
1661                         struct snd_ctl_elem_value *ucontrol,
1662                          u16 (*func)(u32, u16))
1663 {
1664         u32 h_control = kcontrol->private_value;
1665
1666         /* default to S/PDIF */
1667         u16 source = HPI_AESEBU_FORMAT_SPDIF;
1668
1669         if (ucontrol->value.enumerated.item[0] == 1)
1670                 source = HPI_AESEBU_FORMAT_SPDIF;
1671         if (ucontrol->value.enumerated.item[0] == 2)
1672                 source = HPI_AESEBU_FORMAT_AESEBU;
1673
1674         if (func(h_control, source) != 0)
1675                 return -EINVAL;
1676
1677         return 1;
1678 }
1679
1680 static int snd_asihpi_aesebu_rx_format_get(struct snd_kcontrol *kcontrol,
1681                                  struct snd_ctl_elem_value *ucontrol) {
1682         return snd_asihpi_aesebu_format_get(kcontrol, ucontrol,
1683                                         hpi_aesebu_receiver_get_format);
1684 }
1685
1686 static int snd_asihpi_aesebu_rx_format_put(struct snd_kcontrol *kcontrol,
1687                                  struct snd_ctl_elem_value *ucontrol) {
1688         return snd_asihpi_aesebu_format_put(kcontrol, ucontrol,
1689                                         hpi_aesebu_receiver_set_format);
1690 }
1691
1692 static int snd_asihpi_aesebu_rxstatus_info(struct snd_kcontrol *kcontrol,
1693                                   struct snd_ctl_elem_info *uinfo)
1694 {
1695         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1696         uinfo->count = 1;
1697
1698         uinfo->value.integer.min = 0;
1699         uinfo->value.integer.max = 0X1F;
1700         uinfo->value.integer.step = 1;
1701
1702         return 0;
1703 }
1704
1705 static int snd_asihpi_aesebu_rxstatus_get(struct snd_kcontrol *kcontrol,
1706                                  struct snd_ctl_elem_value *ucontrol) {
1707
1708         u32 h_control = kcontrol->private_value;
1709         u16 status;
1710
1711         hpi_handle_error(hpi_aesebu_receiver_get_error_status(
1712                                          h_control, &status));
1713         ucontrol->value.integer.value[0] = status;
1714         return 0;
1715 }
1716
1717 static int __devinit snd_asihpi_aesebu_rx_add(struct snd_card_asihpi *asihpi,
1718                                         struct hpi_control *hpi_ctl)
1719 {
1720         struct snd_card *card = asihpi->card;
1721         struct snd_kcontrol_new snd_control;
1722
1723         asihpi_ctl_init(&snd_control, hpi_ctl, "Format");
1724         snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1725         snd_control.info = snd_asihpi_aesebu_format_info;
1726         snd_control.get = snd_asihpi_aesebu_rx_format_get;
1727         snd_control.put = snd_asihpi_aesebu_rx_format_put;
1728
1729
1730         if (ctl_add(card, &snd_control, asihpi) < 0)
1731                 return -EINVAL;
1732
1733         asihpi_ctl_init(&snd_control, hpi_ctl, "Status");
1734         snd_control.access =
1735             SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
1736         snd_control.info = snd_asihpi_aesebu_rxstatus_info;
1737         snd_control.get = snd_asihpi_aesebu_rxstatus_get;
1738
1739         return ctl_add(card, &snd_control, asihpi);
1740 }
1741
1742 static int snd_asihpi_aesebu_tx_format_get(struct snd_kcontrol *kcontrol,
1743                                  struct snd_ctl_elem_value *ucontrol) {
1744         return snd_asihpi_aesebu_format_get(kcontrol, ucontrol,
1745                                         hpi_aesebu_transmitter_get_format);
1746 }
1747
1748 static int snd_asihpi_aesebu_tx_format_put(struct snd_kcontrol *kcontrol,
1749                                  struct snd_ctl_elem_value *ucontrol) {
1750         return snd_asihpi_aesebu_format_put(kcontrol, ucontrol,
1751                                         hpi_aesebu_transmitter_set_format);
1752 }
1753
1754
1755 static int __devinit snd_asihpi_aesebu_tx_add(struct snd_card_asihpi *asihpi,
1756                                         struct hpi_control *hpi_ctl)
1757 {
1758         struct snd_card *card = asihpi->card;
1759         struct snd_kcontrol_new snd_control;
1760
1761         asihpi_ctl_init(&snd_control, hpi_ctl, "Format");
1762         snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1763         snd_control.info = snd_asihpi_aesebu_format_info;
1764         snd_control.get = snd_asihpi_aesebu_tx_format_get;
1765         snd_control.put = snd_asihpi_aesebu_tx_format_put;
1766
1767         return ctl_add(card, &snd_control, asihpi);
1768 }
1769
1770 /*------------------------------------------------------------
1771    Tuner controls
1772  ------------------------------------------------------------*/
1773
1774 /* Gain */
1775
1776 static int snd_asihpi_tuner_gain_info(struct snd_kcontrol *kcontrol,
1777                                   struct snd_ctl_elem_info *uinfo)
1778 {
1779         u32 h_control = kcontrol->private_value;
1780         u16 err;
1781         short idx;
1782         u16 gain_range[3];
1783
1784         for (idx = 0; idx < 3; idx++) {
1785                 err = hpi_tuner_query_gain(h_control,
1786                                           idx, &gain_range[idx]);
1787                 if (err != 0)
1788                         return err;
1789         }
1790
1791         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1792         uinfo->count = 1;
1793         uinfo->value.integer.min = ((int)gain_range[0]) / HPI_UNITS_PER_dB;
1794         uinfo->value.integer.max = ((int)gain_range[1]) / HPI_UNITS_PER_dB;
1795         uinfo->value.integer.step = ((int) gain_range[2]) / HPI_UNITS_PER_dB;
1796         return 0;
1797 }
1798
1799 static int snd_asihpi_tuner_gain_get(struct snd_kcontrol *kcontrol,
1800                                  struct snd_ctl_elem_value *ucontrol)
1801 {
1802         /*
1803         struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1804         */
1805         u32 h_control = kcontrol->private_value;
1806         short gain;
1807
1808         hpi_handle_error(hpi_tuner_get_gain(h_control, &gain));
1809         ucontrol->value.integer.value[0] = gain / HPI_UNITS_PER_dB;
1810
1811         return 0;
1812 }
1813
1814 static int snd_asihpi_tuner_gain_put(struct snd_kcontrol *kcontrol,
1815                                  struct snd_ctl_elem_value *ucontrol)
1816 {
1817         /*
1818         struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1819         */
1820         u32 h_control = kcontrol->private_value;
1821         short gain;
1822
1823         gain = (ucontrol->value.integer.value[0]) * HPI_UNITS_PER_dB;
1824         hpi_handle_error(hpi_tuner_set_gain(h_control, gain));
1825
1826         return 1;
1827 }
1828
1829 /* Band  */
1830
1831 static int asihpi_tuner_band_query(struct snd_kcontrol *kcontrol,
1832                                         u16 *band_list, u32 len) {
1833         u32 h_control = kcontrol->private_value;
1834         u16 err = 0;
1835         u32 i;
1836
1837         for (i = 0; i < len; i++) {
1838                 err = hpi_tuner_query_band(
1839                                 h_control, i, &band_list[i]);
1840                 if (err != 0)
1841                         break;
1842         }
1843
1844         if (err && (err != HPI_ERROR_INVALID_OBJ_INDEX))
1845                 return -EIO;
1846
1847         return i;
1848 }
1849
1850 static int snd_asihpi_tuner_band_info(struct snd_kcontrol *kcontrol,
1851                                   struct snd_ctl_elem_info *uinfo)
1852 {
1853         u16 tuner_bands[HPI_TUNER_BAND_LAST];
1854         int num_bands = 0;
1855
1856         num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1857                                 HPI_TUNER_BAND_LAST);
1858
1859         if (num_bands < 0)
1860                 return num_bands;
1861
1862         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1863         uinfo->count = 1;
1864         uinfo->value.enumerated.items = num_bands;
1865
1866         if (num_bands > 0) {
1867                 if (uinfo->value.enumerated.item >=
1868                                         uinfo->value.enumerated.items)
1869                         uinfo->value.enumerated.item =
1870                                 uinfo->value.enumerated.items - 1;
1871
1872                 strcpy(uinfo->value.enumerated.name,
1873                         asihpi_tuner_band_names[
1874                                 tuner_bands[uinfo->value.enumerated.item]]);
1875
1876         }
1877         return 0;
1878 }
1879
1880 static int snd_asihpi_tuner_band_get(struct snd_kcontrol *kcontrol,
1881                                  struct snd_ctl_elem_value *ucontrol)
1882 {
1883         u32 h_control = kcontrol->private_value;
1884         /*
1885         struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1886         */
1887         u16 band, idx;
1888         u16 tuner_bands[HPI_TUNER_BAND_LAST];
1889         u32 num_bands = 0;
1890
1891         num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1892                                 HPI_TUNER_BAND_LAST);
1893
1894         hpi_handle_error(hpi_tuner_get_band(h_control, &band));
1895
1896         ucontrol->value.enumerated.item[0] = -1;
1897         for (idx = 0; idx < HPI_TUNER_BAND_LAST; idx++)
1898                 if (tuner_bands[idx] == band) {
1899                         ucontrol->value.enumerated.item[0] = idx;
1900                         break;
1901                 }
1902
1903         return 0;
1904 }
1905
1906 static int snd_asihpi_tuner_band_put(struct snd_kcontrol *kcontrol,
1907                                  struct snd_ctl_elem_value *ucontrol)
1908 {
1909         /*
1910         struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1911         */
1912         u32 h_control = kcontrol->private_value;
1913         u16 band;
1914         u16 tuner_bands[HPI_TUNER_BAND_LAST];
1915         u32 num_bands = 0;
1916
1917         num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1918                         HPI_TUNER_BAND_LAST);
1919
1920         band = tuner_bands[ucontrol->value.enumerated.item[0]];
1921         hpi_handle_error(hpi_tuner_set_band(h_control, band));
1922
1923         return 1;
1924 }
1925
1926 /* Freq */
1927
1928 static int snd_asihpi_tuner_freq_info(struct snd_kcontrol *kcontrol,
1929                                   struct snd_ctl_elem_info *uinfo)
1930 {
1931         u32 h_control = kcontrol->private_value;
1932         u16 err;
1933         u16 tuner_bands[HPI_TUNER_BAND_LAST];
1934         u16 num_bands = 0, band_iter, idx;
1935         u32 freq_range[3], temp_freq_range[3];
1936
1937         num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1938                         HPI_TUNER_BAND_LAST);
1939
1940         freq_range[0] = INT_MAX;
1941         freq_range[1] = 0;
1942         freq_range[2] = INT_MAX;
1943
1944         for (band_iter = 0; band_iter < num_bands; band_iter++) {
1945                 for (idx = 0; idx < 3; idx++) {
1946                         err = hpi_tuner_query_frequency(h_control,
1947                                 idx, tuner_bands[band_iter],
1948                                 &temp_freq_range[idx]);
1949                         if (err != 0)
1950                                 return err;
1951                 }
1952
1953                 /* skip band with bogus stepping */
1954                 if (temp_freq_range[2] <= 0)
1955                         continue;
1956
1957                 if (temp_freq_range[0] < freq_range[0])
1958                         freq_range[0] = temp_freq_range[0];
1959                 if (temp_freq_range[1] > freq_range[1])
1960                         freq_range[1] = temp_freq_range[1];
1961                 if (temp_freq_range[2] < freq_range[2])
1962                         freq_range[2] = temp_freq_range[2];
1963         }
1964
1965         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1966         uinfo->count = 1;
1967         uinfo->value.integer.min = ((int)freq_range[0]);
1968         uinfo->value.integer.max = ((int)freq_range[1]);
1969         uinfo->value.integer.step = ((int)freq_range[2]);
1970         return 0;
1971 }
1972
1973 static int snd_asihpi_tuner_freq_get(struct snd_kcontrol *kcontrol,
1974                                  struct snd_ctl_elem_value *ucontrol)
1975 {
1976         u32 h_control = kcontrol->private_value;
1977         u32 freq;
1978
1979         hpi_handle_error(hpi_tuner_get_frequency(h_control, &freq));
1980         ucontrol->value.integer.value[0] = freq;
1981
1982         return 0;
1983 }
1984
1985 static int snd_asihpi_tuner_freq_put(struct snd_kcontrol *kcontrol,
1986                                  struct snd_ctl_elem_value *ucontrol)
1987 {
1988         u32 h_control = kcontrol->private_value;
1989         u32 freq;
1990
1991         freq = ucontrol->value.integer.value[0];
1992         hpi_handle_error(hpi_tuner_set_frequency(h_control, freq));
1993
1994         return 1;
1995 }
1996
1997 /* Tuner control group initializer  */
1998 static int __devinit snd_asihpi_tuner_add(struct snd_card_asihpi *asihpi,
1999                                         struct hpi_control *hpi_ctl)
2000 {
2001         struct snd_card *card = asihpi->card;
2002         struct snd_kcontrol_new snd_control;
2003
2004         snd_control.private_value = hpi_ctl->h_control;
2005         snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2006
2007         if (!hpi_tuner_get_gain(hpi_ctl->h_control, NULL)) {
2008                 asihpi_ctl_init(&snd_control, hpi_ctl, "Gain");
2009                 snd_control.info = snd_asihpi_tuner_gain_info;
2010                 snd_control.get = snd_asihpi_tuner_gain_get;
2011                 snd_control.put = snd_asihpi_tuner_gain_put;
2012
2013                 if (ctl_add(card, &snd_control, asihpi) < 0)
2014                         return -EINVAL;
2015         }
2016
2017         asihpi_ctl_init(&snd_control, hpi_ctl, "Band");
2018         snd_control.info = snd_asihpi_tuner_band_info;
2019         snd_control.get = snd_asihpi_tuner_band_get;
2020         snd_control.put = snd_asihpi_tuner_band_put;
2021
2022         if (ctl_add(card, &snd_control, asihpi) < 0)
2023                 return -EINVAL;
2024
2025         asihpi_ctl_init(&snd_control, hpi_ctl, "Freq");
2026         snd_control.info = snd_asihpi_tuner_freq_info;
2027         snd_control.get = snd_asihpi_tuner_freq_get;
2028         snd_control.put = snd_asihpi_tuner_freq_put;
2029
2030         return ctl_add(card, &snd_control, asihpi);
2031 }
2032
2033 /*------------------------------------------------------------
2034    Meter controls
2035  ------------------------------------------------------------*/
2036 static int snd_asihpi_meter_info(struct snd_kcontrol *kcontrol,
2037                                  struct snd_ctl_elem_info *uinfo)
2038 {
2039         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2040         uinfo->count = HPI_MAX_CHANNELS;
2041         uinfo->value.integer.min = 0;
2042         uinfo->value.integer.max = 0x7FFFFFFF;
2043         return 0;
2044 }
2045
2046 /* linear values for 10dB steps */
2047 static int log2lin[] = {
2048         0x7FFFFFFF, /* 0dB */
2049         679093956,
2050         214748365,
2051          67909396,
2052          21474837,
2053           6790940,
2054           2147484, /* -60dB */
2055            679094,
2056            214748, /* -80 */
2057             67909,
2058             21475, /* -100 */
2059              6791,
2060              2147,
2061               679,
2062               214,
2063                68,
2064                21,
2065                 7,
2066                 2
2067 };
2068
2069 static int snd_asihpi_meter_get(struct snd_kcontrol *kcontrol,
2070                                 struct snd_ctl_elem_value *ucontrol)
2071 {
2072         u32 h_control = kcontrol->private_value;
2073         short an_gain_mB[HPI_MAX_CHANNELS], i;
2074         u16 err;
2075
2076         err = hpi_meter_get_peak(h_control, an_gain_mB);
2077
2078         for (i = 0; i < HPI_MAX_CHANNELS; i++) {
2079                 if (err) {
2080                         ucontrol->value.integer.value[i] = 0;
2081                 } else if (an_gain_mB[i] >= 0) {
2082                         ucontrol->value.integer.value[i] =
2083                                 an_gain_mB[i] << 16;
2084                 } else {
2085                         /* -ve is log value in millibels < -60dB,
2086                         * convert to (roughly!) linear,
2087                         */
2088                         ucontrol->value.integer.value[i] =
2089                                         log2lin[an_gain_mB[i] / -1000];
2090                 }
2091         }
2092         return 0;
2093 }
2094
2095 static int __devinit snd_asihpi_meter_add(struct snd_card_asihpi *asihpi,
2096                                         struct hpi_control *hpi_ctl, int subidx)
2097 {
2098         struct snd_card *card = asihpi->card;
2099         struct snd_kcontrol_new snd_control;
2100
2101         asihpi_ctl_init(&snd_control, hpi_ctl, "Meter");
2102         snd_control.access =
2103             SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
2104         snd_control.info = snd_asihpi_meter_info;
2105         snd_control.get = snd_asihpi_meter_get;
2106
2107         snd_control.index = subidx;
2108
2109         return ctl_add(card, &snd_control, asihpi);
2110 }
2111
2112 /*------------------------------------------------------------
2113    Multiplexer controls
2114  ------------------------------------------------------------*/
2115 static int snd_card_asihpi_mux_count_sources(struct snd_kcontrol *snd_control)
2116 {
2117         u32 h_control = snd_control->private_value;
2118         struct hpi_control hpi_ctl;
2119         int s, err;
2120         for (s = 0; s < 32; s++) {
2121                 err = hpi_multiplexer_query_source(h_control, s,
2122                                                   &hpi_ctl.
2123                                                   src_node_type,
2124                                                   &hpi_ctl.
2125                                                   src_node_index);
2126                 if (err)
2127                         break;
2128         }
2129         return s;
2130 }
2131
2132 static int snd_asihpi_mux_info(struct snd_kcontrol *kcontrol,
2133                                struct snd_ctl_elem_info *uinfo)
2134 {
2135         int err;
2136         u16 src_node_type, src_node_index;
2137         u32 h_control = kcontrol->private_value;
2138
2139         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2140         uinfo->count = 1;
2141         uinfo->value.enumerated.items =
2142             snd_card_asihpi_mux_count_sources(kcontrol);
2143
2144         if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2145                 uinfo->value.enumerated.item =
2146                     uinfo->value.enumerated.items - 1;
2147
2148         err =
2149             hpi_multiplexer_query_source(h_control,
2150                                         uinfo->value.enumerated.item,
2151                                         &src_node_type, &src_node_index);
2152
2153         sprintf(uinfo->value.enumerated.name, "%s %d",
2154                 asihpi_src_names[src_node_type - HPI_SOURCENODE_NONE],
2155                 src_node_index);
2156         return 0;
2157 }
2158
2159 static int snd_asihpi_mux_get(struct snd_kcontrol *kcontrol,
2160                               struct snd_ctl_elem_value *ucontrol)
2161 {
2162         u32 h_control = kcontrol->private_value;
2163         u16 source_type, source_index;
2164         u16 src_node_type, src_node_index;
2165         int s;
2166
2167         hpi_handle_error(hpi_multiplexer_get_source(h_control,
2168                                 &source_type, &source_index));
2169         /* Should cache this search result! */
2170         for (s = 0; s < 256; s++) {
2171                 if (hpi_multiplexer_query_source(h_control, s,
2172                                             &src_node_type, &src_node_index))
2173                         break;
2174
2175                 if ((source_type == src_node_type)
2176                     && (source_index == src_node_index)) {
2177                         ucontrol->value.enumerated.item[0] = s;
2178                         return 0;
2179                 }
2180         }
2181         snd_printd(KERN_WARNING
2182                 "Control %x failed to match mux source %hu %hu\n",
2183                 h_control, source_type, source_index);
2184         ucontrol->value.enumerated.item[0] = 0;
2185         return 0;
2186 }
2187
2188 static int snd_asihpi_mux_put(struct snd_kcontrol *kcontrol,
2189                               struct snd_ctl_elem_value *ucontrol)
2190 {
2191         int change;
2192         u32 h_control = kcontrol->private_value;
2193         u16 source_type, source_index;
2194         u16 e;
2195
2196         change = 1;
2197
2198         e = hpi_multiplexer_query_source(h_control,
2199                                     ucontrol->value.enumerated.item[0],
2200                                     &source_type, &source_index);
2201         if (!e)
2202                 hpi_handle_error(
2203                         hpi_multiplexer_set_source(h_control,
2204                                                 source_type, source_index));
2205         return change;
2206 }
2207
2208
2209 static int  __devinit snd_asihpi_mux_add(struct snd_card_asihpi *asihpi,
2210                                         struct hpi_control *hpi_ctl)
2211 {
2212         struct snd_card *card = asihpi->card;
2213         struct snd_kcontrol_new snd_control;
2214
2215         asihpi_ctl_init(&snd_control, hpi_ctl, "Route");
2216         snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2217         snd_control.info = snd_asihpi_mux_info;
2218         snd_control.get = snd_asihpi_mux_get;
2219         snd_control.put = snd_asihpi_mux_put;
2220
2221         return ctl_add(card, &snd_control, asihpi);
2222
2223 }
2224
2225 /*------------------------------------------------------------
2226    Channel mode controls
2227  ------------------------------------------------------------*/
2228 static int snd_asihpi_cmode_info(struct snd_kcontrol *kcontrol,
2229                                  struct snd_ctl_elem_info *uinfo)
2230 {
2231         static const char * const mode_names[HPI_CHANNEL_MODE_LAST + 1] = {
2232                 "invalid",
2233                 "Normal", "Swap",
2234                 "From Left", "From Right",
2235                 "To Left", "To Right"
2236         };
2237
2238         u32 h_control = kcontrol->private_value;
2239         u16 mode;
2240         int i;
2241         u16 mode_map[6];
2242         int valid_modes = 0;
2243
2244         /* HPI channel mode values can be from 1 to 6
2245         Some adapters only support a contiguous subset
2246         */
2247         for (i = 0; i < HPI_CHANNEL_MODE_LAST; i++)
2248                 if (!hpi_channel_mode_query_mode(
2249                         h_control, i, &mode)) {
2250                         mode_map[valid_modes] = mode;
2251                         valid_modes++;
2252                         }
2253
2254         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2255         uinfo->count = 1;
2256         uinfo->value.enumerated.items = valid_modes;
2257
2258         if (uinfo->value.enumerated.item >= valid_modes)
2259                 uinfo->value.enumerated.item = valid_modes - 1;
2260
2261         strcpy(uinfo->value.enumerated.name,
2262                mode_names[mode_map[uinfo->value.enumerated.item]]);
2263
2264         return 0;
2265 }
2266
2267 static int snd_asihpi_cmode_get(struct snd_kcontrol *kcontrol,
2268                                 struct snd_ctl_elem_value *ucontrol)
2269 {
2270         u32 h_control = kcontrol->private_value;
2271         u16 mode;
2272
2273         if (hpi_channel_mode_get(h_control, &mode))
2274                 mode = 1;
2275
2276         ucontrol->value.enumerated.item[0] = mode - 1;
2277
2278         return 0;
2279 }
2280
2281 static int snd_asihpi_cmode_put(struct snd_kcontrol *kcontrol,
2282                                 struct snd_ctl_elem_value *ucontrol)
2283 {
2284         int change;
2285         u32 h_control = kcontrol->private_value;
2286
2287         change = 1;
2288
2289         hpi_handle_error(hpi_channel_mode_set(h_control,
2290                            ucontrol->value.enumerated.item[0] + 1));
2291         return change;
2292 }
2293
2294
2295 static int __devinit snd_asihpi_cmode_add(struct snd_card_asihpi *asihpi,
2296                                         struct hpi_control *hpi_ctl)
2297 {
2298         struct snd_card *card = asihpi->card;
2299         struct snd_kcontrol_new snd_control;
2300
2301         asihpi_ctl_init(&snd_control, hpi_ctl, "Mode");
2302         snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2303         snd_control.info = snd_asihpi_cmode_info;
2304         snd_control.get = snd_asihpi_cmode_get;
2305         snd_control.put = snd_asihpi_cmode_put;
2306
2307         return ctl_add(card, &snd_control, asihpi);
2308 }
2309
2310 /*------------------------------------------------------------
2311    Sampleclock source  controls
2312  ------------------------------------------------------------*/
2313 static char *sampleclock_sources[MAX_CLOCKSOURCES] = {
2314         "N/A", "Local PLL", "Digital Sync", "Word External", "Word Header",
2315         "SMPTE", "Digital1", "Auto", "Network", "Invalid",
2316         "Prev Module",
2317         "Digital2", "Digital3", "Digital4", "Digital5",
2318         "Digital6", "Digital7", "Digital8"};
2319
2320 static int snd_asihpi_clksrc_info(struct snd_kcontrol *kcontrol,
2321                                   struct snd_ctl_elem_info *uinfo)
2322 {
2323         struct snd_card_asihpi *asihpi =
2324                         (struct snd_card_asihpi *)(kcontrol->private_data);
2325         struct clk_cache *clkcache = &asihpi->cc;
2326         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2327         uinfo->count = 1;
2328         uinfo->value.enumerated.items = clkcache->count;
2329
2330         if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2331                 uinfo->value.enumerated.item =
2332                                 uinfo->value.enumerated.items - 1;
2333
2334         strcpy(uinfo->value.enumerated.name,
2335                clkcache->s[uinfo->value.enumerated.item].name);
2336         return 0;
2337 }
2338
2339 static int snd_asihpi_clksrc_get(struct snd_kcontrol *kcontrol,
2340                                  struct snd_ctl_elem_value *ucontrol)
2341 {
2342         struct snd_card_asihpi *asihpi =
2343                         (struct snd_card_asihpi *)(kcontrol->private_data);
2344         struct clk_cache *clkcache = &asihpi->cc;
2345         u32 h_control = kcontrol->private_value;
2346         u16 source, srcindex = 0;
2347         int i;
2348
2349         ucontrol->value.enumerated.item[0] = 0;
2350         if (hpi_sample_clock_get_source(h_control, &source))
2351                 source = 0;
2352
2353         if (source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
2354                 if (hpi_sample_clock_get_source_index(h_control, &srcindex))
2355                         srcindex = 0;
2356
2357         for (i = 0; i < clkcache->count; i++)
2358                 if ((clkcache->s[i].source == source) &&
2359                         (clkcache->s[i].index == srcindex))
2360                         break;
2361
2362         ucontrol->value.enumerated.item[0] = i;
2363
2364         return 0;
2365 }
2366
2367 static int snd_asihpi_clksrc_put(struct snd_kcontrol *kcontrol,
2368                                  struct snd_ctl_elem_value *ucontrol)
2369 {
2370         struct snd_card_asihpi *asihpi =
2371                         (struct snd_card_asihpi *)(kcontrol->private_data);
2372         struct clk_cache *clkcache = &asihpi->cc;
2373         int change, item;
2374         u32 h_control = kcontrol->private_value;
2375
2376         change = 1;
2377         item = ucontrol->value.enumerated.item[0];
2378         if (item >= clkcache->count)
2379                 item = clkcache->count-1;
2380
2381         hpi_handle_error(hpi_sample_clock_set_source(
2382                                 h_control, clkcache->s[item].source));
2383
2384         if (clkcache->s[item].source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
2385                 hpi_handle_error(hpi_sample_clock_set_source_index(
2386                                 h_control, clkcache->s[item].index));
2387         return change;
2388 }
2389
2390 /*------------------------------------------------------------
2391    Clkrate controls
2392  ------------------------------------------------------------*/
2393 /* Need to change this to enumerated control with list of rates */
2394 static int snd_asihpi_clklocal_info(struct snd_kcontrol *kcontrol,
2395                                    struct snd_ctl_elem_info *uinfo)
2396 {
2397         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2398         uinfo->count = 1;
2399         uinfo->value.integer.min = 8000;
2400         uinfo->value.integer.max = 192000;
2401         uinfo->value.integer.step = 100;
2402
2403         return 0;
2404 }
2405
2406 static int snd_asihpi_clklocal_get(struct snd_kcontrol *kcontrol,
2407                                   struct snd_ctl_elem_value *ucontrol)
2408 {
2409         u32 h_control = kcontrol->private_value;
2410         u32 rate;
2411         u16 e;
2412
2413         e = hpi_sample_clock_get_local_rate(h_control, &rate);
2414         if (!e)
2415                 ucontrol->value.integer.value[0] = rate;
2416         else
2417                 ucontrol->value.integer.value[0] = 0;
2418         return 0;
2419 }
2420
2421 static int snd_asihpi_clklocal_put(struct snd_kcontrol *kcontrol,
2422                                   struct snd_ctl_elem_value *ucontrol)
2423 {
2424         int change;
2425         u32 h_control = kcontrol->private_value;
2426
2427         /*  change = asihpi->mixer_clkrate[addr][0] != left ||
2428            asihpi->mixer_clkrate[addr][1] != right;
2429          */
2430         change = 1;
2431         hpi_handle_error(hpi_sample_clock_set_local_rate(h_control,
2432                                       ucontrol->value.integer.value[0]));
2433         return change;
2434 }
2435
2436 static int snd_asihpi_clkrate_info(struct snd_kcontrol *kcontrol,
2437                                    struct snd_ctl_elem_info *uinfo)
2438 {
2439         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2440         uinfo->count = 1;
2441         uinfo->value.integer.min = 8000;
2442         uinfo->value.integer.max = 192000;
2443         uinfo->value.integer.step = 100;
2444
2445         return 0;
2446 }
2447
2448 static int snd_asihpi_clkrate_get(struct snd_kcontrol *kcontrol,
2449                                   struct snd_ctl_elem_value *ucontrol)
2450 {
2451         u32 h_control = kcontrol->private_value;
2452         u32 rate;
2453         u16 e;
2454
2455         e = hpi_sample_clock_get_sample_rate(h_control, &rate);
2456         if (!e)
2457                 ucontrol->value.integer.value[0] = rate;
2458         else
2459                 ucontrol->value.integer.value[0] = 0;
2460         return 0;
2461 }
2462
2463 static int __devinit snd_asihpi_sampleclock_add(struct snd_card_asihpi *asihpi,
2464                                         struct hpi_control *hpi_ctl)
2465 {
2466         struct snd_card *card = asihpi->card;
2467         struct snd_kcontrol_new snd_control;
2468
2469         struct clk_cache *clkcache = &asihpi->cc;
2470         u32 hSC =  hpi_ctl->h_control;
2471         int has_aes_in = 0;
2472         int i, j;
2473         u16 source;
2474
2475         snd_control.private_value = hpi_ctl->h_control;
2476
2477         clkcache->has_local = 0;
2478
2479         for (i = 0; i <= HPI_SAMPLECLOCK_SOURCE_LAST; i++) {
2480                 if  (hpi_sample_clock_query_source(hSC,
2481                                 i, &source))
2482                         break;
2483                 clkcache->s[i].source = source;
2484                 clkcache->s[i].index = 0;
2485                 clkcache->s[i].name = sampleclock_sources[source];
2486                 if (source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
2487                         has_aes_in = 1;
2488                 if (source == HPI_SAMPLECLOCK_SOURCE_LOCAL)
2489                         clkcache->has_local = 1;
2490         }
2491         if (has_aes_in)
2492                 /* already will have picked up index 0 above */
2493                 for (j = 1; j < 8; j++) {
2494                         if (hpi_sample_clock_query_source_index(hSC,
2495                                 j, HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT,
2496                                 &source))
2497                                 break;
2498                         clkcache->s[i].source =
2499                                 HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT;
2500                         clkcache->s[i].index = j;
2501                         clkcache->s[i].name = sampleclock_sources[
2502                                         j+HPI_SAMPLECLOCK_SOURCE_LAST];
2503                         i++;
2504                 }
2505         clkcache->count = i;
2506
2507         asihpi_ctl_init(&snd_control, hpi_ctl, "Source");
2508         snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE ;
2509         snd_control.info = snd_asihpi_clksrc_info;
2510         snd_control.get = snd_asihpi_clksrc_get;
2511         snd_control.put = snd_asihpi_clksrc_put;
2512         if (ctl_add(card, &snd_control, asihpi) < 0)
2513                 return -EINVAL;
2514
2515
2516         if (clkcache->has_local) {
2517                 asihpi_ctl_init(&snd_control, hpi_ctl, "Localrate");
2518                 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE ;
2519                 snd_control.info = snd_asihpi_clklocal_info;
2520                 snd_control.get = snd_asihpi_clklocal_get;
2521                 snd_control.put = snd_asihpi_clklocal_put;
2522
2523
2524                 if (ctl_add(card, &snd_control, asihpi) < 0)
2525                         return -EINVAL;
2526         }
2527
2528         asihpi_ctl_init(&snd_control, hpi_ctl, "Rate");
2529         snd_control.access =
2530             SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
2531         snd_control.info = snd_asihpi_clkrate_info;
2532         snd_control.get = snd_asihpi_clkrate_get;
2533
2534         return ctl_add(card, &snd_control, asihpi);
2535 }
2536 /*------------------------------------------------------------
2537    Mixer
2538  ------------------------------------------------------------*/
2539
2540 static int __devinit snd_card_asihpi_mixer_new(struct snd_card_asihpi *asihpi)
2541 {
2542         struct snd_card *card = asihpi->card;
2543         unsigned int idx = 0;
2544         unsigned int subindex = 0;
2545         int err;
2546         struct hpi_control hpi_ctl, prev_ctl;
2547
2548         if (snd_BUG_ON(!asihpi))
2549                 return -EINVAL;
2550         strcpy(card->mixername, "Asihpi Mixer");
2551
2552         err =
2553             hpi_mixer_open(asihpi->adapter_index,
2554                           &asihpi->h_mixer);
2555         hpi_handle_error(err);
2556         if (err)
2557                 return -err;
2558
2559         memset(&prev_ctl, 0, sizeof(prev_ctl));
2560         prev_ctl.control_type = -1;
2561
2562         for (idx = 0; idx < 2000; idx++) {
2563                 err = hpi_mixer_get_control_by_index(
2564                                 asihpi->h_mixer,
2565                                 idx,
2566                                 &hpi_ctl.src_node_type,
2567                                 &hpi_ctl.src_node_index,
2568                                 &hpi_ctl.dst_node_type,
2569                                 &hpi_ctl.dst_node_index,
2570                                 &hpi_ctl.control_type,
2571                                 &hpi_ctl.h_control);
2572                 if (err) {
2573                         if (err == HPI_ERROR_CONTROL_DISABLED) {
2574                                 if (mixer_dump)
2575                                         snd_printk(KERN_INFO
2576                                                    "Disabled HPI Control(%d)\n",
2577                                                    idx);
2578                                 continue;
2579                         } else
2580                                 break;
2581
2582                 }
2583
2584                 hpi_ctl.src_node_type -= HPI_SOURCENODE_NONE;
2585                 hpi_ctl.dst_node_type -= HPI_DESTNODE_NONE;
2586
2587                 /* ASI50xx in SSX mode has multiple meters on the same node.
2588                    Use subindex to create distinct ALSA controls
2589                    for any duplicated controls.
2590                 */
2591                 if ((hpi_ctl.control_type == prev_ctl.control_type) &&
2592                     (hpi_ctl.src_node_type == prev_ctl.src_node_type) &&
2593                     (hpi_ctl.src_node_index == prev_ctl.src_node_index) &&
2594                     (hpi_ctl.dst_node_type == prev_ctl.dst_node_type) &&
2595                     (hpi_ctl.dst_node_index == prev_ctl.dst_node_index))
2596                         subindex++;
2597                 else
2598                         subindex = 0;
2599
2600                 prev_ctl = hpi_ctl;
2601
2602                 switch (hpi_ctl.control_type) {
2603                 case HPI_CONTROL_VOLUME:
2604                         err = snd_asihpi_volume_add(asihpi, &hpi_ctl);
2605                         break;
2606                 case HPI_CONTROL_LEVEL:
2607                         err = snd_asihpi_level_add(asihpi, &hpi_ctl);
2608                         break;
2609                 case HPI_CONTROL_MULTIPLEXER:
2610                         err = snd_asihpi_mux_add(asihpi, &hpi_ctl);
2611                         break;
2612                 case HPI_CONTROL_CHANNEL_MODE:
2613                         err = snd_asihpi_cmode_add(asihpi, &hpi_ctl);
2614                         break;
2615                 case HPI_CONTROL_METER:
2616                         err = snd_asihpi_meter_add(asihpi, &hpi_ctl, subindex);
2617                         break;
2618                 case HPI_CONTROL_SAMPLECLOCK:
2619                         err = snd_asihpi_sampleclock_add(
2620                                                 asihpi, &hpi_ctl);
2621                         break;
2622                 case HPI_CONTROL_CONNECTION:    /* ignore these */
2623                         continue;
2624                 case HPI_CONTROL_TUNER:
2625                         err = snd_asihpi_tuner_add(asihpi, &hpi_ctl);
2626                         break;
2627                 case HPI_CONTROL_AESEBU_TRANSMITTER:
2628                         err = snd_asihpi_aesebu_tx_add(asihpi, &hpi_ctl);
2629                         break;
2630                 case HPI_CONTROL_AESEBU_RECEIVER:
2631                         err = snd_asihpi_aesebu_rx_add(asihpi, &hpi_ctl);
2632                         break;
2633                 case HPI_CONTROL_VOX:
2634                 case HPI_CONTROL_BITSTREAM:
2635                 case HPI_CONTROL_MICROPHONE:
2636                 case HPI_CONTROL_PARAMETRIC_EQ:
2637                 case HPI_CONTROL_COMPANDER:
2638                 default:
2639                         if (mixer_dump)
2640                                 snd_printk(KERN_INFO
2641                                         "Untranslated HPI Control"
2642                                         "(%d) %d %d %d %d %d\n",
2643                                         idx,
2644                                         hpi_ctl.control_type,
2645                                         hpi_ctl.src_node_type,
2646                                         hpi_ctl.src_node_index,
2647                                         hpi_ctl.dst_node_type,
2648                                         hpi_ctl.dst_node_index);
2649                         continue;
2650                 };
2651                 if (err < 0)
2652                         return err;
2653         }
2654         if (HPI_ERROR_INVALID_OBJ_INDEX != err)
2655                 hpi_handle_error(err);
2656
2657         snd_printk(KERN_INFO "%d mixer controls found\n", idx);
2658
2659         return 0;
2660 }
2661
2662 /*------------------------------------------------------------
2663    /proc interface
2664  ------------------------------------------------------------*/
2665
2666 static void
2667 snd_asihpi_proc_read(struct snd_info_entry *entry,
2668                         struct snd_info_buffer *buffer)
2669 {
2670         struct snd_card_asihpi *asihpi = entry->private_data;
2671         u16 version;
2672         u32 h_control;
2673         u32 rate = 0;
2674         u16 source = 0;
2675         int err;
2676
2677         snd_iprintf(buffer, "ASIHPI driver proc file\n");
2678         snd_iprintf(buffer,
2679                 "adapter ID=%4X\n_index=%d\n"
2680                 "num_outstreams=%d\n_num_instreams=%d\n",
2681                 asihpi->type, asihpi->adapter_index,
2682                 asihpi->num_outstreams, asihpi->num_instreams);
2683
2684         version = asihpi->version;
2685         snd_iprintf(buffer,
2686                 "serial#=%d\n_hw version %c%d\nDSP code version %03d\n",
2687                 asihpi->serial_number, ((version >> 3) & 0xf) + 'A',
2688                 version & 0x7,
2689                 ((version >> 13) * 100) + ((version >> 7) & 0x3f));
2690
2691         err = hpi_mixer_get_control(asihpi->h_mixer,
2692                                   HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
2693                                   HPI_CONTROL_SAMPLECLOCK, &h_control);
2694
2695         if (!err) {
2696                 err = hpi_sample_clock_get_sample_rate(
2697                                         h_control, &rate);
2698                 err += hpi_sample_clock_get_source(h_control, &source);
2699
2700                 if (!err)
2701                         snd_iprintf(buffer, "sample_clock=%d_hz, source %s\n",
2702                         rate, sampleclock_sources[source]);
2703         }
2704
2705 }
2706
2707
2708 static void __devinit snd_asihpi_proc_init(struct snd_card_asihpi *asihpi)
2709 {
2710         struct snd_info_entry *entry;
2711
2712         if (!snd_card_proc_new(asihpi->card, "info", &entry))
2713                 snd_info_set_text_ops(entry, asihpi, snd_asihpi_proc_read);
2714 }
2715
2716 /*------------------------------------------------------------
2717    HWDEP
2718  ------------------------------------------------------------*/
2719
2720 static int snd_asihpi_hpi_open(struct snd_hwdep *hw, struct file *file)
2721 {
2722         if (enable_hpi_hwdep)
2723                 return 0;
2724         else
2725                 return -ENODEV;
2726
2727 }
2728
2729 static int snd_asihpi_hpi_release(struct snd_hwdep *hw, struct file *file)
2730 {
2731         if (enable_hpi_hwdep)
2732                 return asihpi_hpi_release(file);
2733         else
2734                 return -ENODEV;
2735 }
2736
2737 static int snd_asihpi_hpi_ioctl(struct snd_hwdep *hw, struct file *file,
2738                                 unsigned int cmd, unsigned long arg)
2739 {
2740         if (enable_hpi_hwdep)
2741                 return asihpi_hpi_ioctl(file, cmd, arg);
2742         else
2743                 return -ENODEV;
2744 }
2745
2746
2747 /* results in /dev/snd/hwC#D0 file for each card with index #
2748    also /proc/asound/hwdep will contain '#-00: asihpi (HPI) for each card'
2749 */
2750 static int __devinit snd_asihpi_hpi_new(struct snd_card_asihpi *asihpi,
2751         int device, struct snd_hwdep **rhwdep)
2752 {
2753         struct snd_hwdep *hw;
2754         int err;
2755
2756         if (rhwdep)
2757                 *rhwdep = NULL;
2758         err = snd_hwdep_new(asihpi->card, "HPI", device, &hw);
2759         if (err < 0)
2760                 return err;
2761         strcpy(hw->name, "asihpi (HPI)");
2762         hw->iface = SNDRV_HWDEP_IFACE_LAST;
2763         hw->ops.open = snd_asihpi_hpi_open;
2764         hw->ops.ioctl = snd_asihpi_hpi_ioctl;
2765         hw->ops.release = snd_asihpi_hpi_release;
2766         hw->private_data = asihpi;
2767         if (rhwdep)
2768                 *rhwdep = hw;
2769         return 0;
2770 }
2771
2772 /*------------------------------------------------------------
2773    CARD
2774  ------------------------------------------------------------*/
2775 static int __devinit snd_asihpi_probe(struct pci_dev *pci_dev,
2776                                        const struct pci_device_id *pci_id)
2777 {
2778         int err;
2779
2780         u16 version;
2781         int pcm_substreams;
2782
2783         struct hpi_adapter *hpi_card;
2784         struct snd_card *card;
2785         struct snd_card_asihpi *asihpi;
2786
2787         u32 h_control;
2788         u32 h_stream;
2789
2790         static int dev;
2791         if (dev >= SNDRV_CARDS)
2792                 return -ENODEV;
2793
2794         /* Should this be enable[hpi_card->index] ? */
2795         if (!enable[dev]) {
2796                 dev++;
2797                 return -ENOENT;
2798         }
2799
2800         err = asihpi_adapter_probe(pci_dev, pci_id);
2801         if (err < 0)
2802                 return err;
2803
2804         hpi_card = pci_get_drvdata(pci_dev);
2805         /* first try to give the card the same index as its hardware index */
2806         err = snd_card_create(hpi_card->index,
2807                               id[hpi_card->index], THIS_MODULE,
2808                               sizeof(struct snd_card_asihpi),
2809                               &card);
2810         if (err < 0) {
2811                 /* if that fails, try the default index==next available */
2812                 err =
2813                     snd_card_create(index[dev], id[dev],
2814                                     THIS_MODULE,
2815                                     sizeof(struct snd_card_asihpi),
2816                                     &card);
2817                 if (err < 0)
2818                         return err;
2819                 snd_printk(KERN_WARNING
2820                         "**** WARNING **** Adapter index %d->ALSA index %d\n",
2821                         hpi_card->index, card->number);
2822         }
2823
2824         snd_card_set_dev(card, &pci_dev->dev);
2825
2826         asihpi = (struct snd_card_asihpi *) card->private_data;
2827         asihpi->card = card;
2828         asihpi->pci = pci_dev;
2829         asihpi->adapter_index = hpi_card->index;
2830         hpi_handle_error(hpi_adapter_get_info(
2831                                  asihpi->adapter_index,
2832                                  &asihpi->num_outstreams,
2833                                  &asihpi->num_instreams,
2834                                  &asihpi->version,
2835                                  &asihpi->serial_number, &asihpi->type));
2836
2837         version = asihpi->version;
2838         snd_printk(KERN_INFO "adapter ID=%4X index=%d num_outstreams=%d "
2839                         "num_instreams=%d S/N=%d\n"
2840                         "Hw Version %c%d DSP code version %03d\n",
2841                         asihpi->type, asihpi->adapter_index,
2842                         asihpi->num_outstreams,
2843                         asihpi->num_instreams, asihpi->serial_number,
2844                         ((version >> 3) & 0xf) + 'A',
2845                         version & 0x7,
2846                         ((version >> 13) * 100) + ((version >> 7) & 0x3f));
2847
2848         pcm_substreams = asihpi->num_outstreams;
2849         if (pcm_substreams < asihpi->num_instreams)
2850                 pcm_substreams = asihpi->num_instreams;
2851
2852         err = hpi_adapter_get_property(asihpi->adapter_index,
2853                 HPI_ADAPTER_PROPERTY_CAPS1,
2854                 NULL, &asihpi->support_grouping);
2855         if (err)
2856                 asihpi->support_grouping = 0;
2857
2858         err = hpi_adapter_get_property(asihpi->adapter_index,
2859                 HPI_ADAPTER_PROPERTY_CAPS2,
2860                 &asihpi->support_mrx, NULL);
2861         if (err)
2862                 asihpi->support_mrx = 0;
2863
2864         err = hpi_adapter_get_property(asihpi->adapter_index,
2865                 HPI_ADAPTER_PROPERTY_INTERVAL,
2866                 NULL, &asihpi->update_interval_frames);
2867         if (err)
2868                 asihpi->update_interval_frames = 512;
2869
2870         if (!asihpi->can_dma)
2871                 asihpi->update_interval_frames *= 2;
2872
2873         hpi_handle_error(hpi_instream_open(asihpi->adapter_index,
2874                              0, &h_stream));
2875
2876         err = hpi_instream_host_buffer_free(h_stream);
2877         asihpi->can_dma = (!err);
2878
2879         hpi_handle_error(hpi_instream_close(h_stream));
2880
2881         err = hpi_adapter_get_property(asihpi->adapter_index,
2882                 HPI_ADAPTER_PROPERTY_CURCHANNELS,
2883                 &asihpi->in_max_chans, &asihpi->out_max_chans);
2884         if (err) {
2885                 asihpi->in_max_chans = 2;
2886                 asihpi->out_max_chans = 2;
2887         }
2888
2889         snd_printk(KERN_INFO "has dma:%d, grouping:%d, mrx:%d\n",
2890                         asihpi->can_dma,
2891                         asihpi->support_grouping,
2892                         asihpi->support_mrx
2893               );
2894
2895         err = snd_card_asihpi_pcm_new(asihpi, 0, pcm_substreams);
2896         if (err < 0) {
2897                 snd_printk(KERN_ERR "pcm_new failed\n");
2898                 goto __nodev;
2899         }
2900         err = snd_card_asihpi_mixer_new(asihpi);
2901         if (err < 0) {
2902                 snd_printk(KERN_ERR "mixer_new failed\n");
2903                 goto __nodev;
2904         }
2905
2906         err = hpi_mixer_get_control(asihpi->h_mixer,
2907                                   HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
2908                                   HPI_CONTROL_SAMPLECLOCK, &h_control);
2909
2910         if (!err)
2911                 err = hpi_sample_clock_set_local_rate(
2912                         h_control, adapter_fs);
2913
2914         snd_asihpi_proc_init(asihpi);
2915
2916         /* always create, can be enabled or disabled dynamically
2917             by enable_hwdep  module param*/
2918         snd_asihpi_hpi_new(asihpi, 0, NULL);
2919
2920         strcpy(card->driver, "ASIHPI");
2921
2922         sprintf(card->shortname, "AudioScience ASI%4X", asihpi->type);
2923         sprintf(card->longname, "%s %i",
2924                         card->shortname, asihpi->adapter_index);
2925         err = snd_card_register(card);
2926
2927         if (!err) {
2928                 hpi_card->snd_card_asihpi = card;
2929                 dev++;
2930                 return 0;
2931         }
2932 __nodev:
2933         snd_card_free(card);
2934         snd_printk(KERN_ERR "snd_asihpi_probe error %d\n", err);
2935         return err;
2936
2937 }
2938
2939 static void __devexit snd_asihpi_remove(struct pci_dev *pci_dev)
2940 {
2941         struct hpi_adapter *hpi_card = pci_get_drvdata(pci_dev);
2942
2943         snd_card_free(hpi_card->snd_card_asihpi);
2944         hpi_card->snd_card_asihpi = NULL;
2945         asihpi_adapter_remove(pci_dev);
2946 }
2947
2948 static DEFINE_PCI_DEVICE_TABLE(asihpi_pci_tbl) = {
2949         {HPI_PCI_VENDOR_ID_TI, HPI_PCI_DEV_ID_DSP6205,
2950                 HPI_PCI_VENDOR_ID_AUDIOSCIENCE, PCI_ANY_ID, 0, 0,
2951                 (kernel_ulong_t)HPI_6205},
2952         {HPI_PCI_VENDOR_ID_TI, HPI_PCI_DEV_ID_PCI2040,
2953                 HPI_PCI_VENDOR_ID_AUDIOSCIENCE, PCI_ANY_ID, 0, 0,
2954                 (kernel_ulong_t)HPI_6000},
2955         {0,}
2956 };
2957 MODULE_DEVICE_TABLE(pci, asihpi_pci_tbl);
2958
2959 static struct pci_driver driver = {
2960         .name = KBUILD_MODNAME,
2961         .id_table = asihpi_pci_tbl,
2962         .probe = snd_asihpi_probe,
2963         .remove = __devexit_p(snd_asihpi_remove),
2964 #ifdef CONFIG_PM
2965 /*      .suspend = snd_asihpi_suspend,
2966         .resume = snd_asihpi_resume, */
2967 #endif
2968 };
2969
2970 static int __init snd_asihpi_init(void)
2971 {
2972         asihpi_init();
2973         return pci_register_driver(&driver);
2974 }
2975
2976 static void __exit snd_asihpi_exit(void)
2977 {
2978
2979         pci_unregister_driver(&driver);
2980         asihpi_exit();
2981 }
2982
2983 module_init(snd_asihpi_init)
2984 module_exit(snd_asihpi_exit)
2985