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