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