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