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