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