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