3 * Copyright (c) by AudioScience Inc <alsa@audioscience.com>
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;
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.
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
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.
24 /* >0: print Hw params, timer vars. >1: print stream write/copy sizes */
25 #define REALLY_VERBOSE_LOGGING 0
27 #if REALLY_VERBOSE_LOGGING
28 #define VPRINTK1 snd_printd
33 #if REALLY_VERBOSE_LOGGING > 1
34 #define VPRINTK2 snd_printd
39 #ifndef ASI_STYLE_NAMES
40 /* not sure how ALSA style name should look */
41 #define ASI_STYLE_NAMES 1
44 #include "hpi_internal.h"
45 #include "hpimsginit.h"
48 #include <linux/pci.h>
49 #include <linux/init.h>
50 #include <linux/jiffies.h>
51 #include <linux/slab.h>
52 #include <linux/time.h>
53 #include <linux/wait.h>
54 #include <sound/core.h>
55 #include <sound/control.h>
56 #include <sound/pcm.h>
57 #include <sound/pcm_params.h>
58 #include <sound/info.h>
59 #include <sound/initval.h>
60 #include <sound/tlv.h>
61 #include <sound/hwdep.h>
64 MODULE_LICENSE("GPL");
65 MODULE_AUTHOR("AudioScience inc. <support@audioscience.com>");
66 MODULE_DESCRIPTION("AudioScience ALSA ASI5000 ASI6000 ASI87xx ASI89xx");
68 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* index 0-MAX */
69 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
70 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
71 static int enable_hpi_hwdep = 1;
73 module_param_array(index, int, NULL, S_IRUGO);
74 MODULE_PARM_DESC(index, "ALSA index value for AudioScience soundcard.");
76 module_param_array(id, charp, NULL, S_IRUGO);
77 MODULE_PARM_DESC(id, "ALSA ID string for AudioScience soundcard.");
79 module_param_array(enable, bool, NULL, S_IRUGO);
80 MODULE_PARM_DESC(enable, "ALSA enable AudioScience soundcard.");
82 module_param(enable_hpi_hwdep, bool, S_IRUGO|S_IWUSR);
83 MODULE_PARM_DESC(enable_hpi_hwdep,
84 "ALSA enable HPI hwdep for AudioScience soundcard ");
87 #ifdef KERNEL_ALSA_BUILD
88 static char *build_info = "built using headers from kernel source";
89 module_param(build_info, charp, S_IRUGO);
90 MODULE_PARM_DESC(build_info, "built using headers from kernel source");
92 static char *build_info = "built within ALSA source";
93 module_param(build_info, charp, S_IRUGO);
94 MODULE_PARM_DESC(build_info, "built within ALSA source");
97 /* set to 1 to dump every control from adapter to log */
98 static const int mixer_dump;
100 #define DEFAULT_SAMPLERATE 44100
101 static int adapter_fs = DEFAULT_SAMPLERATE;
103 static struct hpi_hsubsys *ss; /* handle to HPI audio subsystem */
106 #define PERIODS_MIN 2
107 #define PERIOD_BYTES_MIN 2304
108 #define BUFFER_BYTES_MAX (512 * 1024)
110 /*#define TIMER_MILLISECONDS 20
111 #define FORCE_TIMER_JIFFIES ((TIMER_MILLISECONDS * HZ + 999)/1000)
114 #define MAX_CLOCKSOURCES (HPI_SAMPLECLOCK_SOURCE_LAST + 1 + 7)
125 struct clk_source s[MAX_CLOCKSOURCES];
129 struct snd_card_asihpi {
130 struct snd_card *card;
143 u16 support_grouping;
145 u16 update_interval_frames;
150 /* Per stream data */
151 struct snd_card_asihpi_pcm {
152 struct timer_list timer;
153 unsigned int respawn_timer;
154 unsigned int hpi_buffer_attached;
155 unsigned int pcm_size;
156 unsigned int pcm_count;
157 unsigned int bytes_per_sec;
158 unsigned int pcm_irq_pos; /* IRQ position */
159 unsigned int pcm_buf_pos; /* position in buffer */
160 struct snd_pcm_substream *substream;
162 struct hpi_format format;
165 /* universal stream verbs work with out or in stream handles */
167 /* Functions to allow driver to give a buffer to HPI for busmastering */
169 static u16 hpi_stream_host_buffer_attach(
170 struct hpi_hsubsys *hS,
171 u32 h_stream, /* handle to outstream. */
172 u32 size_in_bytes, /* size in bytes of bus mastering buffer */
176 struct hpi_message hm;
177 struct hpi_response hr;
178 unsigned int obj = hpi_handle_object(h_stream);
181 return HPI_ERROR_INVALID_OBJ;
182 hpi_init_message_response(&hm, &hr, obj,
183 obj == HPI_OBJ_OSTREAM ?
184 HPI_OSTREAM_HOSTBUFFER_ALLOC :
185 HPI_ISTREAM_HOSTBUFFER_ALLOC);
187 hpi_handle_to_indexes(h_stream, &hm.adapter_index,
190 hm.u.d.u.buffer.buffer_size = size_in_bytes;
191 hm.u.d.u.buffer.pci_address = pci_address;
192 hm.u.d.u.buffer.command = HPI_BUFFER_CMD_INTERNAL_GRANTADAPTER;
193 hpi_send_recv(&hm, &hr);
197 static u16 hpi_stream_host_buffer_detach(
198 struct hpi_hsubsys *hS,
202 struct hpi_message hm;
203 struct hpi_response hr;
204 unsigned int obj = hpi_handle_object(h_stream);
207 return HPI_ERROR_INVALID_OBJ;
209 hpi_init_message_response(&hm, &hr, obj,
210 obj == HPI_OBJ_OSTREAM ?
211 HPI_OSTREAM_HOSTBUFFER_FREE :
212 HPI_ISTREAM_HOSTBUFFER_FREE);
214 hpi_handle_to_indexes(h_stream, &hm.adapter_index,
216 hm.u.d.u.buffer.command = HPI_BUFFER_CMD_INTERNAL_REVOKEADAPTER;
217 hpi_send_recv(&hm, &hr);
221 static inline u16 hpi_stream_start(struct hpi_hsubsys *hS, u32 h_stream)
223 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
224 return hpi_outstream_start(hS, h_stream);
226 return hpi_instream_start(hS, h_stream);
229 static inline u16 hpi_stream_stop(struct hpi_hsubsys *hS, u32 h_stream)
231 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
232 return hpi_outstream_stop(hS, h_stream);
234 return hpi_instream_stop(hS, h_stream);
237 static inline u16 hpi_stream_get_info_ex(
238 struct hpi_hsubsys *hS,
242 u32 *pdata_in_buffer,
247 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
248 return hpi_outstream_get_info_ex(hS, h_stream, pw_state,
249 pbuffer_size, pdata_in_buffer,
250 psample_count, pauxiliary_data);
252 return hpi_instream_get_info_ex(hS, h_stream, pw_state,
253 pbuffer_size, pdata_in_buffer,
254 psample_count, pauxiliary_data);
257 static inline u16 hpi_stream_group_add(struct hpi_hsubsys *hS,
261 if (hpi_handle_object(h_master) == HPI_OBJ_OSTREAM)
262 return hpi_outstream_group_add(hS, h_master, h_stream);
264 return hpi_instream_group_add(hS, h_master, h_stream);
267 static inline u16 hpi_stream_group_reset(struct hpi_hsubsys *hS,
270 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
271 return hpi_outstream_group_reset(hS, h_stream);
273 return hpi_instream_group_reset(hS, h_stream);
276 static inline u16 hpi_stream_group_get_map(struct hpi_hsubsys *hS,
277 u32 h_stream, u32 *mo, u32 *mi)
279 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
280 return hpi_outstream_group_get_map(hS, h_stream, mo, mi);
282 return hpi_instream_group_get_map(hS, h_stream, mo, mi);
285 static u16 handle_error(u16 err, int line, char *filename)
289 "in file %s, line %d: HPI error %d\n",
290 filename, line, err);
294 #define hpi_handle_error(x) handle_error(x, __LINE__, __FILE__)
296 /***************************** GENERAL PCM ****************/
297 #if REALLY_VERBOSE_LOGGING
298 static void print_hwparams(struct snd_pcm_hw_params *p)
300 snd_printd("HWPARAMS \n");
301 snd_printd("samplerate %d \n", params_rate(p));
302 snd_printd("channels %d \n", params_channels(p));
303 snd_printd("format %d \n", params_format(p));
304 snd_printd("subformat %d \n", params_subformat(p));
305 snd_printd("buffer bytes %d \n", params_buffer_bytes(p));
306 snd_printd("period bytes %d \n", params_period_bytes(p));
307 snd_printd("access %d \n", params_access(p));
308 snd_printd("period_size %d \n", params_period_size(p));
309 snd_printd("periods %d \n", params_periods(p));
310 snd_printd("buffer_size %d \n", params_buffer_size(p));
313 #define print_hwparams(x)
316 static snd_pcm_format_t hpi_to_alsa_formats[] = {
318 SNDRV_PCM_FORMAT_U8, /* HPI_FORMAT_PCM8_UNSIGNED 1 */
319 SNDRV_PCM_FORMAT_S16, /* HPI_FORMAT_PCM16_SIGNED 2 */
320 -1, /* HPI_FORMAT_MPEG_L1 3 */
321 SNDRV_PCM_FORMAT_MPEG, /* HPI_FORMAT_MPEG_L2 4 */
322 SNDRV_PCM_FORMAT_MPEG, /* HPI_FORMAT_MPEG_L3 5 */
323 -1, /* HPI_FORMAT_DOLBY_AC2 6 */
324 -1, /* HPI_FORMAT_DOLBY_AC3 7 */
325 SNDRV_PCM_FORMAT_S16_BE,/* HPI_FORMAT_PCM16_BIGENDIAN 8 */
326 -1, /* HPI_FORMAT_AA_TAGIT1_HITS 9 */
327 -1, /* HPI_FORMAT_AA_TAGIT1_INSERTS 10 */
328 SNDRV_PCM_FORMAT_S32, /* HPI_FORMAT_PCM32_SIGNED 11 */
329 -1, /* HPI_FORMAT_RAW_BITSTREAM 12 */
330 -1, /* HPI_FORMAT_AA_TAGIT1_HITS_EX1 13 */
331 SNDRV_PCM_FORMAT_FLOAT, /* HPI_FORMAT_PCM32_FLOAT 14 */
333 /* ALSA can't handle 3 byte sample size together with power-of-2
334 * constraint on buffer_bytes, so disable this format
338 /* SNDRV_PCM_FORMAT_S24_3LE */ /* { HPI_FORMAT_PCM24_SIGNED 15 */
343 static int snd_card_asihpi_format_alsa2hpi(snd_pcm_format_t alsa_format,
348 for (format = HPI_FORMAT_PCM8_UNSIGNED;
349 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
350 if (hpi_to_alsa_formats[format] == alsa_format) {
351 *hpi_format = format;
356 snd_printd(KERN_WARNING "failed match for alsa format %d\n",
362 static void snd_card_asihpi_pcm_samplerates(struct snd_card_asihpi *asihpi,
363 struct snd_pcm_hardware *pcmhw)
369 unsigned int rate_min = 200000;
370 unsigned int rate_max = 0;
371 unsigned int rates = 0;
373 if (asihpi->support_mrx) {
374 rates |= SNDRV_PCM_RATE_CONTINUOUS;
375 rates |= SNDRV_PCM_RATE_8000_96000;
379 /* on cards without SRC,
380 valid rates are determined by sampleclock */
381 err = hpi_mixer_get_control(ss, asihpi->h_mixer,
382 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
383 HPI_CONTROL_SAMPLECLOCK, &h_control);
386 "no local sampleclock, err %d\n", err);
389 for (idx = 0; idx < 100; idx++) {
390 if (hpi_sample_clock_query_local_rate(ss,
391 h_control, idx, &sample_rate)) {
394 "local rate query failed\n");
399 rate_min = min(rate_min, sample_rate);
400 rate_max = max(rate_max, sample_rate);
402 switch (sample_rate) {
404 rates |= SNDRV_PCM_RATE_5512;
407 rates |= SNDRV_PCM_RATE_8000;
410 rates |= SNDRV_PCM_RATE_11025;
413 rates |= SNDRV_PCM_RATE_16000;
416 rates |= SNDRV_PCM_RATE_22050;
419 rates |= SNDRV_PCM_RATE_32000;
422 rates |= SNDRV_PCM_RATE_44100;
425 rates |= SNDRV_PCM_RATE_48000;
428 rates |= SNDRV_PCM_RATE_64000;
431 rates |= SNDRV_PCM_RATE_88200;
434 rates |= SNDRV_PCM_RATE_96000;
437 rates |= SNDRV_PCM_RATE_176400;
440 rates |= SNDRV_PCM_RATE_192000;
442 default: /* some other rate */
443 rates |= SNDRV_PCM_RATE_KNOT;
448 /* printk(KERN_INFO "Supported rates %X %d %d\n",
449 rates, rate_min, rate_max); */
450 pcmhw->rates = rates;
451 pcmhw->rate_min = rate_min;
452 pcmhw->rate_max = rate_max;
455 static int snd_card_asihpi_pcm_hw_params(struct snd_pcm_substream *substream,
456 struct snd_pcm_hw_params *params)
458 struct snd_pcm_runtime *runtime = substream->runtime;
459 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
460 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
463 unsigned int bytes_per_sec;
465 print_hwparams(params);
466 err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
469 err = snd_card_asihpi_format_alsa2hpi(params_format(params), &format);
473 VPRINTK1(KERN_INFO "format %d, %d chans, %d_hz\n",
474 format, params_channels(params),
475 params_rate(params));
477 hpi_handle_error(hpi_format_create(&dpcm->format,
478 params_channels(params),
479 format, params_rate(params), 0, 0));
481 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
482 if (hpi_instream_reset(ss, dpcm->h_stream) != 0)
485 if (hpi_instream_set_format(ss,
486 dpcm->h_stream, &dpcm->format) != 0)
490 dpcm->hpi_buffer_attached = 0;
491 if (card->support_mmap) {
493 err = hpi_stream_host_buffer_attach(ss, dpcm->h_stream,
494 params_buffer_bytes(params), runtime->dma_addr);
497 "stream_host_buffer_attach succeeded %u %lu\n",
498 params_buffer_bytes(params),
499 (unsigned long)runtime->dma_addr);
502 "stream_host_buffer_attach error %d\n",
507 err = hpi_stream_get_info_ex(ss, dpcm->h_stream, NULL,
508 &dpcm->hpi_buffer_attached,
511 snd_printd(KERN_INFO "stream_host_buffer_attach status 0x%x\n",
512 dpcm->hpi_buffer_attached);
514 bytes_per_sec = params_rate(params) * params_channels(params);
515 bytes_per_sec *= snd_pcm_format_width(params_format(params));
517 if (bytes_per_sec <= 0)
520 dpcm->bytes_per_sec = bytes_per_sec;
521 dpcm->pcm_size = params_buffer_bytes(params);
522 dpcm->pcm_count = params_period_bytes(params);
523 snd_printd(KERN_INFO "pcm_size=%d, pcm_count=%d, bps=%d\n",
524 dpcm->pcm_size, dpcm->pcm_count, bytes_per_sec);
526 dpcm->pcm_irq_pos = 0;
527 dpcm->pcm_buf_pos = 0;
531 static void snd_card_asihpi_pcm_timer_start(struct snd_pcm_substream *
534 struct snd_pcm_runtime *runtime = substream->runtime;
535 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
538 expiry = (dpcm->pcm_count * HZ / dpcm->bytes_per_sec);
539 /* wait longer the first time, for samples to propagate */
540 expiry = max(expiry, 20);
541 dpcm->timer.expires = jiffies + expiry;
542 dpcm->respawn_timer = 1;
543 add_timer(&dpcm->timer);
546 static void snd_card_asihpi_pcm_timer_stop(struct snd_pcm_substream *substream)
548 struct snd_pcm_runtime *runtime = substream->runtime;
549 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
551 dpcm->respawn_timer = 0;
552 del_timer(&dpcm->timer);
555 static int snd_card_asihpi_trigger(struct snd_pcm_substream *substream,
558 struct snd_card_asihpi_pcm *dpcm = substream->runtime->private_data;
559 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
560 struct snd_pcm_substream *s;
563 snd_printd("trigger %dstream %d\n",
564 substream->stream, substream->number);
566 case SNDRV_PCM_TRIGGER_START:
567 snd_pcm_group_for_each_entry(s, substream) {
568 struct snd_card_asihpi_pcm *ds;
569 ds = s->runtime->private_data;
571 if (snd_pcm_substream_chip(s) != card)
574 if ((s->stream == SNDRV_PCM_STREAM_PLAYBACK) &&
575 (card->support_mmap)) {
576 /* How do I know how much valid data is present
577 * in buffer? Just guessing 2 periods, but if
578 * buffer is bigger it may contain even more
581 unsigned int preload = ds->pcm_count * 2;
582 VPRINTK2("preload %d\n", preload);
583 hpi_handle_error(hpi_outstream_write_buf(
585 &s->runtime->dma_area[0],
590 if (card->support_grouping) {
591 VPRINTK1("\t_group %dstream %d\n", s->stream,
593 e = hpi_stream_group_add(ss,
597 snd_pcm_trigger_done(s, substream);
605 snd_printd("start\n");
606 /* start the master stream */
607 snd_card_asihpi_pcm_timer_start(substream);
608 hpi_handle_error(hpi_stream_start(ss, dpcm->h_stream));
611 case SNDRV_PCM_TRIGGER_STOP:
612 snd_card_asihpi_pcm_timer_stop(substream);
613 snd_pcm_group_for_each_entry(s, substream) {
614 if (snd_pcm_substream_chip(s) != card)
617 /*? workaround linked streams don't
618 transition to SETUP 20070706*/
619 s->runtime->status->state = SNDRV_PCM_STATE_SETUP;
621 if (card->support_grouping) {
622 VPRINTK1("\t_group %dstream %d\n", s->stream,
624 snd_pcm_trigger_done(s, substream);
628 snd_printd("stop\n");
630 /* _prepare and _hwparams reset the stream */
631 hpi_handle_error(hpi_stream_stop(ss, dpcm->h_stream));
632 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
634 hpi_outstream_reset(ss, dpcm->h_stream));
636 if (card->support_grouping)
637 hpi_handle_error(hpi_stream_group_reset(ss,
641 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
642 snd_printd("pause release\n");
643 hpi_handle_error(hpi_stream_start(ss, dpcm->h_stream));
644 snd_card_asihpi_pcm_timer_start(substream);
646 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
647 snd_printd("pause\n");
648 snd_card_asihpi_pcm_timer_stop(substream);
649 hpi_handle_error(hpi_stream_stop(ss, dpcm->h_stream));
652 snd_printd("\tINVALID\n");
660 snd_card_asihpi_hw_free(struct snd_pcm_substream *substream)
662 struct snd_pcm_runtime *runtime = substream->runtime;
663 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
664 if (dpcm->hpi_buffer_attached)
665 hpi_stream_host_buffer_detach(ss, dpcm->h_stream);
667 snd_pcm_lib_free_pages(substream);
671 static void snd_card_asihpi_runtime_free(struct snd_pcm_runtime *runtime)
673 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
678 Without linking degenerates to getting single stream pos etc
679 Without mmap 2nd loop degenerates to snd_pcm_period_elapsed
682 buf_pos=get_buf_pos(s);
683 for_each_linked_stream(s) {
684 buf_pos=get_buf_pos(s);
685 min_buf_pos = modulo_min(min_buf_pos, buf_pos, pcm_size)
686 new_data = min(new_data, calc_new_data(buf_pos,irq_pos)
688 timer.expires = jiffies + predict_next_period_ready(min_buf_pos);
689 for_each_linked_stream(s) {
690 s->buf_pos = min_buf_pos;
691 if (new_data > pcm_count) {
693 irq_pos = (irq_pos + pcm_count) % pcm_size;
700 snd_pcm_period_elapsed(s);
705 /** Minimum of 2 modulo values. Works correctly when the difference between
706 * the values is less than half the modulus
708 static inline unsigned int modulo_min(unsigned int a, unsigned int b,
709 unsigned long int modulus)
712 if (((a-b) % modulus) < (modulus/2))
720 /** Timer function, equivalent to interrupt service routine for cards
722 static void snd_card_asihpi_timer_function(unsigned long data)
724 struct snd_card_asihpi_pcm *dpcm = (struct snd_card_asihpi_pcm *)data;
725 struct snd_card_asihpi *card = snd_pcm_substream_chip(dpcm->substream);
726 struct snd_pcm_runtime *runtime;
727 struct snd_pcm_substream *s;
728 unsigned int newdata = 0;
729 unsigned int buf_pos, min_buf_pos = 0;
730 unsigned int remdata, xfercount, next_jiffies;
733 u32 buffer_size, data_avail, samples_played, aux;
735 /* find minimum newdata and buffer pos in group */
736 snd_pcm_group_for_each_entry(s, dpcm->substream) {
737 struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
738 runtime = s->runtime;
740 if (snd_pcm_substream_chip(s) != card)
743 hpi_handle_error(hpi_stream_get_info_ex(ss,
744 ds->h_stream, &state,
745 &buffer_size, &data_avail,
746 &samples_played, &aux));
748 /* number of bytes in on-card buffer */
749 runtime->delay = aux;
751 if (state == HPI_STATE_DRAINED) {
752 snd_printd(KERN_WARNING "outstream %d drained\n",
754 snd_pcm_stop(s, SNDRV_PCM_STATE_XRUN);
758 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
759 buf_pos = frames_to_bytes(runtime, samples_played);
761 buf_pos = data_avail + ds->pcm_irq_pos;
765 /* can't statically init min when wrap is involved */
766 min_buf_pos = buf_pos;
767 newdata = (buf_pos - ds->pcm_irq_pos) % ds->pcm_size;
771 modulo_min(min_buf_pos, buf_pos, UINT_MAX+1L);
773 (buf_pos - ds->pcm_irq_pos) % ds->pcm_size,
777 VPRINTK1("PB timer hw_ptr x%04lX, appl_ptr x%04lX\n",
778 (unsigned long)frames_to_bytes(runtime,
779 runtime->status->hw_ptr),
780 (unsigned long)frames_to_bytes(runtime,
781 runtime->control->appl_ptr));
782 VPRINTK1("%d S=%d, irq=%04X, pos=x%04X, left=x%04X,"
783 " aux=x%04X space=x%04X\n", s->number,
784 state, ds->pcm_irq_pos, buf_pos, (int)data_avail,
785 (int)aux, buffer_size-data_avail);
788 remdata = newdata % dpcm->pcm_count;
789 xfercount = newdata - remdata; /* a multiple of pcm_count */
790 next_jiffies = ((dpcm->pcm_count-remdata) * HZ / dpcm->bytes_per_sec)+1;
791 next_jiffies = max(next_jiffies, 2U * HZ / 1000U);
792 dpcm->timer.expires = jiffies + next_jiffies;
793 VPRINTK1("jif %d buf pos x%04X newdata x%04X xc x%04X\n",
794 next_jiffies, min_buf_pos, newdata, xfercount);
796 snd_pcm_group_for_each_entry(s, dpcm->substream) {
797 struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
798 ds->pcm_buf_pos = min_buf_pos;
801 if (card->support_mmap) {
802 ds->pcm_irq_pos = ds->pcm_irq_pos + xfercount;
803 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
804 VPRINTK2("write OS%d x%04x\n",
808 hpi_outstream_write_buf(
815 VPRINTK2("read IS%d x%04x\n",
819 hpi_instream_read_buf(
823 } /* else R/W will be handled by read/write callbacks */
824 snd_pcm_period_elapsed(s);
828 if (dpcm->respawn_timer)
829 add_timer(&dpcm->timer);
832 /***************************** PLAYBACK OPS ****************/
833 static int snd_card_asihpi_playback_ioctl(struct snd_pcm_substream *substream,
834 unsigned int cmd, void *arg)
836 /* snd_printd(KERN_INFO "Playback ioctl %d\n", cmd); */
837 return snd_pcm_lib_ioctl(substream, cmd, arg);
840 static int snd_card_asihpi_playback_prepare(struct snd_pcm_substream *
843 struct snd_pcm_runtime *runtime = substream->runtime;
844 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
846 snd_printd(KERN_INFO "playback prepare %d\n", substream->number);
848 hpi_handle_error(hpi_outstream_reset(ss, dpcm->h_stream));
849 dpcm->pcm_irq_pos = 0;
850 dpcm->pcm_buf_pos = 0;
855 static snd_pcm_uframes_t
856 snd_card_asihpi_playback_pointer(struct snd_pcm_substream *substream)
858 struct snd_pcm_runtime *runtime = substream->runtime;
859 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
860 snd_pcm_uframes_t ptr;
865 if (!snd_pcm_stream_linked(substream)) {
866 /* NOTE, can use samples played for playback position here and
867 * in timer fn because it LAGS the actual read pointer, and is a
868 * better representation of actual playout position
870 err = hpi_outstream_get_info_ex(ss, dpcm->h_stream, NULL,
872 &samples_played, NULL);
873 hpi_handle_error(err);
875 dpcm->pcm_buf_pos = frames_to_bytes(runtime, samples_played);
877 /* else must return most conservative value found in timer func
878 * by looping over all streams
881 ptr = bytes_to_frames(runtime, dpcm->pcm_buf_pos % dpcm->pcm_size);
882 VPRINTK2("playback_pointer=%04ld\n", (unsigned long)ptr);
886 static void snd_card_asihpi_playback_format(struct snd_card_asihpi *asihpi,
888 struct snd_pcm_hardware *pcmhw)
890 struct hpi_format hpi_format;
894 u32 sample_rate = 48000;
896 /* on cards without SRC, must query at valid rate,
897 * maybe set by external sync
899 err = hpi_mixer_get_control(ss, asihpi->h_mixer,
900 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
901 HPI_CONTROL_SAMPLECLOCK, &h_control);
904 err = hpi_sample_clock_get_sample_rate(ss, h_control,
907 for (format = HPI_FORMAT_PCM8_UNSIGNED;
908 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
909 err = hpi_format_create(&hpi_format,
910 2, format, sample_rate, 128000, 0);
912 err = hpi_outstream_query_format(ss, h_stream,
914 if (!err && (hpi_to_alsa_formats[format] != -1))
916 (1ULL << hpi_to_alsa_formats[format]);
920 static struct snd_pcm_hardware snd_card_asihpi_playback = {
923 .buffer_bytes_max = BUFFER_BYTES_MAX,
924 .period_bytes_min = PERIOD_BYTES_MIN,
925 .period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN,
926 .periods_min = PERIODS_MIN,
927 .periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN,
931 static int snd_card_asihpi_playback_open(struct snd_pcm_substream *substream)
933 struct snd_pcm_runtime *runtime = substream->runtime;
934 struct snd_card_asihpi_pcm *dpcm;
935 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
938 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
943 hpi_outstream_open(ss, card->adapter_index,
944 substream->number, &dpcm->h_stream);
945 hpi_handle_error(err);
948 if (err == HPI_ERROR_OBJ_ALREADY_OPEN)
953 /*? also check ASI5000 samplerate source
954 If external, only support external rate.
955 If internal and other stream playing, cant switch
958 init_timer(&dpcm->timer);
959 dpcm->timer.data = (unsigned long) dpcm;
960 dpcm->timer.function = snd_card_asihpi_timer_function;
961 dpcm->substream = substream;
962 runtime->private_data = dpcm;
963 runtime->private_free = snd_card_asihpi_runtime_free;
965 snd_card_asihpi_playback.channels_max = card->out_max_chans;
966 /*?snd_card_asihpi_playback.period_bytes_min =
967 card->out_max_chans * 4096; */
969 snd_card_asihpi_playback_format(card, dpcm->h_stream,
970 &snd_card_asihpi_playback);
972 snd_card_asihpi_pcm_samplerates(card, &snd_card_asihpi_playback);
974 snd_card_asihpi_playback.info = SNDRV_PCM_INFO_INTERLEAVED |
975 SNDRV_PCM_INFO_DOUBLE |
976 SNDRV_PCM_INFO_BATCH |
977 SNDRV_PCM_INFO_BLOCK_TRANSFER |
978 SNDRV_PCM_INFO_PAUSE;
980 if (card->support_mmap)
981 snd_card_asihpi_playback.info |= SNDRV_PCM_INFO_MMAP |
982 SNDRV_PCM_INFO_MMAP_VALID;
984 if (card->support_grouping)
985 snd_card_asihpi_playback.info |= SNDRV_PCM_INFO_SYNC_START;
987 /* struct is copied, so can create initializer dynamically */
988 runtime->hw = snd_card_asihpi_playback;
990 if (card->support_mmap)
991 err = snd_pcm_hw_constraint_pow2(runtime, 0,
992 SNDRV_PCM_HW_PARAM_BUFFER_BYTES);
996 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
997 card->update_interval_frames);
998 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
999 card->update_interval_frames * 4, UINT_MAX);
1001 snd_pcm_set_sync(substream);
1003 snd_printd(KERN_INFO "playback open\n");
1008 static int snd_card_asihpi_playback_close(struct snd_pcm_substream *substream)
1010 struct snd_pcm_runtime *runtime = substream->runtime;
1011 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1013 hpi_handle_error(hpi_outstream_close(ss, dpcm->h_stream));
1014 snd_printd(KERN_INFO "playback close\n");
1019 static int snd_card_asihpi_playback_copy(struct snd_pcm_substream *substream,
1021 snd_pcm_uframes_t pos,
1023 snd_pcm_uframes_t count)
1025 struct snd_pcm_runtime *runtime = substream->runtime;
1026 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1029 len = frames_to_bytes(runtime, count);
1031 if (copy_from_user(runtime->dma_area, src, len))
1034 VPRINTK2(KERN_DEBUG "playback copy%d %u bytes\n",
1035 substream->number, len);
1037 hpi_handle_error(hpi_outstream_write_buf(ss, dpcm->h_stream,
1038 runtime->dma_area, len, &dpcm->format));
1043 static int snd_card_asihpi_playback_silence(struct snd_pcm_substream *
1044 substream, int channel,
1045 snd_pcm_uframes_t pos,
1046 snd_pcm_uframes_t count)
1049 struct snd_pcm_runtime *runtime = substream->runtime;
1050 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1052 len = frames_to_bytes(runtime, count);
1053 snd_printd(KERN_INFO "playback silence %u bytes\n", len);
1055 memset(runtime->dma_area, 0, len);
1056 hpi_handle_error(hpi_outstream_write_buf(ss, dpcm->h_stream,
1057 runtime->dma_area, len, &dpcm->format));
1061 static struct snd_pcm_ops snd_card_asihpi_playback_ops = {
1062 .open = snd_card_asihpi_playback_open,
1063 .close = snd_card_asihpi_playback_close,
1064 .ioctl = snd_card_asihpi_playback_ioctl,
1065 .hw_params = snd_card_asihpi_pcm_hw_params,
1066 .hw_free = snd_card_asihpi_hw_free,
1067 .prepare = snd_card_asihpi_playback_prepare,
1068 .trigger = snd_card_asihpi_trigger,
1069 .pointer = snd_card_asihpi_playback_pointer,
1070 .copy = snd_card_asihpi_playback_copy,
1071 .silence = snd_card_asihpi_playback_silence,
1074 static struct snd_pcm_ops snd_card_asihpi_playback_mmap_ops = {
1075 .open = snd_card_asihpi_playback_open,
1076 .close = snd_card_asihpi_playback_close,
1077 .ioctl = snd_card_asihpi_playback_ioctl,
1078 .hw_params = snd_card_asihpi_pcm_hw_params,
1079 .hw_free = snd_card_asihpi_hw_free,
1080 .prepare = snd_card_asihpi_playback_prepare,
1081 .trigger = snd_card_asihpi_trigger,
1082 .pointer = snd_card_asihpi_playback_pointer,
1085 /***************************** CAPTURE OPS ****************/
1086 static snd_pcm_uframes_t
1087 snd_card_asihpi_capture_pointer(struct snd_pcm_substream *substream)
1089 struct snd_pcm_runtime *runtime = substream->runtime;
1090 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1092 VPRINTK2("capture pointer %d=%d\n",
1093 substream->number, dpcm->pcm_buf_pos);
1094 /* NOTE Unlike playback can't use actual dwSamplesPlayed
1095 for the capture position, because those samples aren't yet in
1096 the local buffer available for reading.
1098 return bytes_to_frames(runtime, dpcm->pcm_buf_pos % dpcm->pcm_size);
1101 static int snd_card_asihpi_capture_ioctl(struct snd_pcm_substream *substream,
1102 unsigned int cmd, void *arg)
1104 return snd_pcm_lib_ioctl(substream, cmd, arg);
1107 static int snd_card_asihpi_capture_prepare(struct snd_pcm_substream *substream)
1109 struct snd_pcm_runtime *runtime = substream->runtime;
1110 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1112 hpi_handle_error(hpi_instream_reset(ss, dpcm->h_stream));
1113 dpcm->pcm_irq_pos = 0;
1114 dpcm->pcm_buf_pos = 0;
1116 snd_printd("capture prepare %d\n", substream->number);
1122 static void snd_card_asihpi_capture_format(struct snd_card_asihpi *asihpi,
1124 struct snd_pcm_hardware *pcmhw)
1126 struct hpi_format hpi_format;
1130 u32 sample_rate = 48000;
1132 /* on cards without SRC, must query at valid rate,
1133 maybe set by external sync */
1134 err = hpi_mixer_get_control(ss, asihpi->h_mixer,
1135 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
1136 HPI_CONTROL_SAMPLECLOCK, &h_control);
1139 err = hpi_sample_clock_get_sample_rate(ss, h_control,
1142 for (format = HPI_FORMAT_PCM8_UNSIGNED;
1143 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
1145 err = hpi_format_create(&hpi_format, 2, format,
1146 sample_rate, 128000, 0);
1148 err = hpi_instream_query_format(ss, h_stream,
1152 (1ULL << hpi_to_alsa_formats[format]);
1157 static struct snd_pcm_hardware snd_card_asihpi_capture = {
1160 .buffer_bytes_max = BUFFER_BYTES_MAX,
1161 .period_bytes_min = PERIOD_BYTES_MIN,
1162 .period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN,
1163 .periods_min = PERIODS_MIN,
1164 .periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN,
1168 static int snd_card_asihpi_capture_open(struct snd_pcm_substream *substream)
1170 struct snd_pcm_runtime *runtime = substream->runtime;
1171 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
1172 struct snd_card_asihpi_pcm *dpcm;
1175 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
1179 snd_printd("hpi_instream_open adapter %d stream %d\n",
1180 card->adapter_index, substream->number);
1182 err = hpi_handle_error(
1183 hpi_instream_open(ss, card->adapter_index,
1184 substream->number, &dpcm->h_stream));
1187 if (err == HPI_ERROR_OBJ_ALREADY_OPEN)
1193 init_timer(&dpcm->timer);
1194 dpcm->timer.data = (unsigned long) dpcm;
1195 dpcm->timer.function = snd_card_asihpi_timer_function;
1196 dpcm->substream = substream;
1197 runtime->private_data = dpcm;
1198 runtime->private_free = snd_card_asihpi_runtime_free;
1200 snd_card_asihpi_capture.channels_max = card->in_max_chans;
1201 snd_card_asihpi_capture_format(card, dpcm->h_stream,
1202 &snd_card_asihpi_capture);
1203 snd_card_asihpi_pcm_samplerates(card, &snd_card_asihpi_capture);
1204 snd_card_asihpi_capture.info = SNDRV_PCM_INFO_INTERLEAVED;
1206 if (card->support_mmap)
1207 snd_card_asihpi_capture.info |= SNDRV_PCM_INFO_MMAP |
1208 SNDRV_PCM_INFO_MMAP_VALID;
1210 runtime->hw = snd_card_asihpi_capture;
1212 if (card->support_mmap)
1213 err = snd_pcm_hw_constraint_pow2(runtime, 0,
1214 SNDRV_PCM_HW_PARAM_BUFFER_BYTES);
1218 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1219 card->update_interval_frames);
1220 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1221 card->update_interval_frames * 2, UINT_MAX);
1223 snd_pcm_set_sync(substream);
1228 static int snd_card_asihpi_capture_close(struct snd_pcm_substream *substream)
1230 struct snd_card_asihpi_pcm *dpcm = substream->runtime->private_data;
1232 hpi_handle_error(hpi_instream_close(ss, dpcm->h_stream));
1236 static int snd_card_asihpi_capture_copy(struct snd_pcm_substream *substream,
1237 int channel, snd_pcm_uframes_t pos,
1238 void __user *dst, snd_pcm_uframes_t count)
1240 struct snd_pcm_runtime *runtime = substream->runtime;
1241 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1244 data_size = frames_to_bytes(runtime, count);
1246 VPRINTK2("capture copy%d %d bytes\n", substream->number, data_size);
1247 hpi_handle_error(hpi_instream_read_buf(ss, dpcm->h_stream,
1248 runtime->dma_area, data_size));
1250 /* Used by capture_pointer */
1251 dpcm->pcm_irq_pos = dpcm->pcm_irq_pos + data_size;
1253 if (copy_to_user(dst, runtime->dma_area, data_size))
1259 static struct snd_pcm_ops snd_card_asihpi_capture_mmap_ops = {
1260 .open = snd_card_asihpi_capture_open,
1261 .close = snd_card_asihpi_capture_close,
1262 .ioctl = snd_card_asihpi_capture_ioctl,
1263 .hw_params = snd_card_asihpi_pcm_hw_params,
1264 .hw_free = snd_card_asihpi_hw_free,
1265 .prepare = snd_card_asihpi_capture_prepare,
1266 .trigger = snd_card_asihpi_trigger,
1267 .pointer = snd_card_asihpi_capture_pointer,
1270 static struct snd_pcm_ops snd_card_asihpi_capture_ops = {
1271 .open = snd_card_asihpi_capture_open,
1272 .close = snd_card_asihpi_capture_close,
1273 .ioctl = snd_card_asihpi_capture_ioctl,
1274 .hw_params = snd_card_asihpi_pcm_hw_params,
1275 .hw_free = snd_card_asihpi_hw_free,
1276 .prepare = snd_card_asihpi_capture_prepare,
1277 .trigger = snd_card_asihpi_trigger,
1278 .pointer = snd_card_asihpi_capture_pointer,
1279 .copy = snd_card_asihpi_capture_copy
1282 static int __devinit snd_card_asihpi_pcm_new(struct snd_card_asihpi *asihpi,
1283 int device, int substreams)
1285 struct snd_pcm *pcm;
1288 err = snd_pcm_new(asihpi->card, "asihpi PCM", device,
1289 asihpi->num_outstreams, asihpi->num_instreams,
1293 /* pointer to ops struct is stored, dont change ops afterwards! */
1294 if (asihpi->support_mmap) {
1295 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1296 &snd_card_asihpi_playback_mmap_ops);
1297 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1298 &snd_card_asihpi_capture_mmap_ops);
1300 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1301 &snd_card_asihpi_playback_ops);
1302 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1303 &snd_card_asihpi_capture_ops);
1306 pcm->private_data = asihpi;
1307 pcm->info_flags = 0;
1308 strcpy(pcm->name, "asihpi PCM");
1310 /*? do we want to emulate MMAP for non-BBM cards?
1311 Jack doesn't work with ALSAs MMAP emulation - WHY NOT? */
1312 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1313 snd_dma_pci_data(asihpi->pci),
1314 64*1024, BUFFER_BYTES_MAX);
1319 /***************************** MIXER CONTROLS ****************/
1320 struct hpi_control {
1328 char name[44]; /* copied to snd_ctl_elem_id.name[44]; */
1331 static char *asihpi_tuner_band_names[] =
1345 compile_time_assert(
1346 (ARRAY_SIZE(asihpi_tuner_band_names) ==
1347 (HPI_TUNER_BAND_LAST+1)),
1348 assert_tuner_band_names_size);
1351 static char *asihpi_src_names[] =
1367 static char *asihpi_src_names[] =
1384 compile_time_assert(
1385 (ARRAY_SIZE(asihpi_src_names) ==
1386 (HPI_SOURCENODE_LAST_INDEX-HPI_SOURCENODE_BASE+1)),
1387 assert_src_names_size);
1390 static char *asihpi_dst_names[] =
1402 static char *asihpi_dst_names[] =
1415 compile_time_assert(
1416 (ARRAY_SIZE(asihpi_dst_names) ==
1417 (HPI_DESTNODE_LAST_INDEX-HPI_DESTNODE_BASE+1)),
1418 assert_dst_names_size);
1420 static inline int ctl_add(struct snd_card *card, struct snd_kcontrol_new *ctl,
1421 struct snd_card_asihpi *asihpi)
1425 err = snd_ctl_add(card, snd_ctl_new1(ctl, asihpi));
1428 else if (mixer_dump)
1429 snd_printk(KERN_INFO "added %s(%d)\n", ctl->name, ctl->index);
1434 /* Convert HPI control name and location into ALSA control name */
1435 static void asihpi_ctl_init(struct snd_kcontrol_new *snd_control,
1436 struct hpi_control *hpi_ctl,
1439 memset(snd_control, 0, sizeof(*snd_control));
1440 snd_control->name = hpi_ctl->name;
1441 snd_control->private_value = hpi_ctl->h_control;
1442 snd_control->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1443 snd_control->index = 0;
1445 if (hpi_ctl->src_node_type && hpi_ctl->dst_node_type)
1446 sprintf(hpi_ctl->name, "%s%d to %s%d %s",
1447 asihpi_src_names[hpi_ctl->src_node_type],
1448 hpi_ctl->src_node_index,
1449 asihpi_dst_names[hpi_ctl->dst_node_type],
1450 hpi_ctl->dst_node_index,
1452 else if (hpi_ctl->dst_node_type) {
1453 sprintf(hpi_ctl->name, "%s%d %s",
1454 asihpi_dst_names[hpi_ctl->dst_node_type],
1455 hpi_ctl->dst_node_index,
1458 sprintf(hpi_ctl->name, "%s%d %s",
1459 asihpi_src_names[hpi_ctl->src_node_type],
1460 hpi_ctl->src_node_index,
1465 /*------------------------------------------------------------
1467 ------------------------------------------------------------*/
1468 #define VOL_STEP_mB 1
1469 static int snd_asihpi_volume_info(struct snd_kcontrol *kcontrol,
1470 struct snd_ctl_elem_info *uinfo)
1472 u32 h_control = kcontrol->private_value;
1474 /* native gains are in millibels */
1479 err = hpi_volume_query_range(ss, h_control,
1480 &min_gain_mB, &max_gain_mB, &step_gain_mB);
1483 min_gain_mB = -10000;
1484 step_gain_mB = VOL_STEP_mB;
1487 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1489 uinfo->value.integer.min = min_gain_mB / VOL_STEP_mB;
1490 uinfo->value.integer.max = max_gain_mB / VOL_STEP_mB;
1491 uinfo->value.integer.step = step_gain_mB / VOL_STEP_mB;
1495 static int snd_asihpi_volume_get(struct snd_kcontrol *kcontrol,
1496 struct snd_ctl_elem_value *ucontrol)
1498 u32 h_control = kcontrol->private_value;
1499 short an_gain_mB[HPI_MAX_CHANNELS];
1501 hpi_handle_error(hpi_volume_get_gain(ss, h_control, an_gain_mB));
1502 ucontrol->value.integer.value[0] = an_gain_mB[0] / VOL_STEP_mB;
1503 ucontrol->value.integer.value[1] = an_gain_mB[1] / VOL_STEP_mB;
1508 static int snd_asihpi_volume_put(struct snd_kcontrol *kcontrol,
1509 struct snd_ctl_elem_value *ucontrol)
1512 u32 h_control = kcontrol->private_value;
1513 short an_gain_mB[HPI_MAX_CHANNELS];
1516 (ucontrol->value.integer.value[0]) * VOL_STEP_mB;
1518 (ucontrol->value.integer.value[1]) * VOL_STEP_mB;
1519 /* change = asihpi->mixer_volume[addr][0] != left ||
1520 asihpi->mixer_volume[addr][1] != right;
1523 hpi_handle_error(hpi_volume_set_gain(ss, h_control, an_gain_mB));
1527 static const DECLARE_TLV_DB_SCALE(db_scale_100, -10000, VOL_STEP_mB, 0);
1529 static int __devinit snd_asihpi_volume_add(struct snd_card_asihpi *asihpi,
1530 struct hpi_control *hpi_ctl)
1532 struct snd_card *card = asihpi->card;
1533 struct snd_kcontrol_new snd_control;
1535 asihpi_ctl_init(&snd_control, hpi_ctl, "volume");
1536 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1537 SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1538 snd_control.info = snd_asihpi_volume_info;
1539 snd_control.get = snd_asihpi_volume_get;
1540 snd_control.put = snd_asihpi_volume_put;
1541 snd_control.tlv.p = db_scale_100;
1543 return ctl_add(card, &snd_control, asihpi);
1546 /*------------------------------------------------------------
1548 ------------------------------------------------------------*/
1549 static int snd_asihpi_level_info(struct snd_kcontrol *kcontrol,
1550 struct snd_ctl_elem_info *uinfo)
1552 u32 h_control = kcontrol->private_value;
1559 hpi_level_query_range(ss, h_control, &min_gain_mB,
1560 &max_gain_mB, &step_gain_mB);
1563 min_gain_mB = -1000;
1567 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1569 uinfo->value.integer.min = min_gain_mB / HPI_UNITS_PER_dB;
1570 uinfo->value.integer.max = max_gain_mB / HPI_UNITS_PER_dB;
1571 uinfo->value.integer.step = step_gain_mB / HPI_UNITS_PER_dB;
1575 static int snd_asihpi_level_get(struct snd_kcontrol *kcontrol,
1576 struct snd_ctl_elem_value *ucontrol)
1578 u32 h_control = kcontrol->private_value;
1579 short an_gain_mB[HPI_MAX_CHANNELS];
1581 hpi_handle_error(hpi_level_get_gain(ss, h_control, an_gain_mB));
1582 ucontrol->value.integer.value[0] =
1583 an_gain_mB[0] / HPI_UNITS_PER_dB;
1584 ucontrol->value.integer.value[1] =
1585 an_gain_mB[1] / HPI_UNITS_PER_dB;
1590 static int snd_asihpi_level_put(struct snd_kcontrol *kcontrol,
1591 struct snd_ctl_elem_value *ucontrol)
1594 u32 h_control = kcontrol->private_value;
1595 short an_gain_mB[HPI_MAX_CHANNELS];
1598 (ucontrol->value.integer.value[0]) * HPI_UNITS_PER_dB;
1600 (ucontrol->value.integer.value[1]) * HPI_UNITS_PER_dB;
1601 /* change = asihpi->mixer_level[addr][0] != left ||
1602 asihpi->mixer_level[addr][1] != right;
1605 hpi_handle_error(hpi_level_set_gain(ss, h_control, an_gain_mB));
1609 static const DECLARE_TLV_DB_SCALE(db_scale_level, -1000, 100, 0);
1611 static int __devinit snd_asihpi_level_add(struct snd_card_asihpi *asihpi,
1612 struct hpi_control *hpi_ctl)
1614 struct snd_card *card = asihpi->card;
1615 struct snd_kcontrol_new snd_control;
1617 /* can't use 'volume' cos some nodes have volume as well */
1618 asihpi_ctl_init(&snd_control, hpi_ctl, "level");
1619 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1620 SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1621 snd_control.info = snd_asihpi_level_info;
1622 snd_control.get = snd_asihpi_level_get;
1623 snd_control.put = snd_asihpi_level_put;
1624 snd_control.tlv.p = db_scale_level;
1626 return ctl_add(card, &snd_control, asihpi);
1629 /*------------------------------------------------------------
1631 ------------------------------------------------------------*/
1634 static char *asihpi_aesebu_format_names[] =
1641 static int snd_asihpi_aesebu_format_info(struct snd_kcontrol *kcontrol,
1642 struct snd_ctl_elem_info *uinfo)
1644 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1646 uinfo->value.enumerated.items = 3;
1648 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1649 uinfo->value.enumerated.item =
1650 uinfo->value.enumerated.items - 1;
1652 strcpy(uinfo->value.enumerated.name,
1653 asihpi_aesebu_format_names[uinfo->value.enumerated.item]);
1658 static int snd_asihpi_aesebu_format_get(struct snd_kcontrol *kcontrol,
1659 struct snd_ctl_elem_value *ucontrol,
1660 u16 (*func)(const struct hpi_hsubsys *, u32, u16 *))
1662 u32 h_control = kcontrol->private_value;
1665 err = func(ss, h_control, &source);
1667 /* default to N/A */
1668 ucontrol->value.enumerated.item[0] = 0;
1669 /* return success but set the control to N/A */
1672 if (source == HPI_AESEBU_FORMAT_SPDIF)
1673 ucontrol->value.enumerated.item[0] = 1;
1674 if (source == HPI_AESEBU_FORMAT_AESEBU)
1675 ucontrol->value.enumerated.item[0] = 2;
1680 static int snd_asihpi_aesebu_format_put(struct snd_kcontrol *kcontrol,
1681 struct snd_ctl_elem_value *ucontrol,
1682 u16 (*func)(const struct hpi_hsubsys *, u32, u16))
1684 u32 h_control = kcontrol->private_value;
1686 /* default to S/PDIF */
1687 u16 source = HPI_AESEBU_FORMAT_SPDIF;
1689 if (ucontrol->value.enumerated.item[0] == 1)
1690 source = HPI_AESEBU_FORMAT_SPDIF;
1691 if (ucontrol->value.enumerated.item[0] == 2)
1692 source = HPI_AESEBU_FORMAT_AESEBU;
1694 if (func(ss, h_control, source) != 0)
1700 static int snd_asihpi_aesebu_rx_format_get(struct snd_kcontrol *kcontrol,
1701 struct snd_ctl_elem_value *ucontrol) {
1702 return snd_asihpi_aesebu_format_get(kcontrol, ucontrol,
1703 HPI_AESEBU__receiver_get_format);
1706 static int snd_asihpi_aesebu_rx_format_put(struct snd_kcontrol *kcontrol,
1707 struct snd_ctl_elem_value *ucontrol) {
1708 return snd_asihpi_aesebu_format_put(kcontrol, ucontrol,
1709 HPI_AESEBU__receiver_set_format);
1712 static int snd_asihpi_aesebu_rxstatus_info(struct snd_kcontrol *kcontrol,
1713 struct snd_ctl_elem_info *uinfo)
1715 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1718 uinfo->value.integer.min = 0;
1719 uinfo->value.integer.max = 0X1F;
1720 uinfo->value.integer.step = 1;
1725 static int snd_asihpi_aesebu_rxstatus_get(struct snd_kcontrol *kcontrol,
1726 struct snd_ctl_elem_value *ucontrol) {
1728 u32 h_control = kcontrol->private_value;
1731 hpi_handle_error(HPI_AESEBU__receiver_get_error_status(
1732 ss, h_control, &status));
1733 ucontrol->value.integer.value[0] = status;
1737 static int __devinit snd_asihpi_aesebu_rx_add(struct snd_card_asihpi *asihpi,
1738 struct hpi_control *hpi_ctl)
1740 struct snd_card *card = asihpi->card;
1741 struct snd_kcontrol_new snd_control;
1743 asihpi_ctl_init(&snd_control, hpi_ctl, "format");
1744 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1745 snd_control.info = snd_asihpi_aesebu_format_info;
1746 snd_control.get = snd_asihpi_aesebu_rx_format_get;
1747 snd_control.put = snd_asihpi_aesebu_rx_format_put;
1750 if (ctl_add(card, &snd_control, asihpi) < 0)
1753 asihpi_ctl_init(&snd_control, hpi_ctl, "status");
1754 snd_control.access =
1755 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
1756 snd_control.info = snd_asihpi_aesebu_rxstatus_info;
1757 snd_control.get = snd_asihpi_aesebu_rxstatus_get;
1759 return ctl_add(card, &snd_control, asihpi);
1762 static int snd_asihpi_aesebu_tx_format_get(struct snd_kcontrol *kcontrol,
1763 struct snd_ctl_elem_value *ucontrol) {
1764 return snd_asihpi_aesebu_format_get(kcontrol, ucontrol,
1765 HPI_AESEBU__transmitter_get_format);
1768 static int snd_asihpi_aesebu_tx_format_put(struct snd_kcontrol *kcontrol,
1769 struct snd_ctl_elem_value *ucontrol) {
1770 return snd_asihpi_aesebu_format_put(kcontrol, ucontrol,
1771 HPI_AESEBU__transmitter_set_format);
1775 static int __devinit snd_asihpi_aesebu_tx_add(struct snd_card_asihpi *asihpi,
1776 struct hpi_control *hpi_ctl)
1778 struct snd_card *card = asihpi->card;
1779 struct snd_kcontrol_new snd_control;
1781 asihpi_ctl_init(&snd_control, hpi_ctl, "format");
1782 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1783 snd_control.info = snd_asihpi_aesebu_format_info;
1784 snd_control.get = snd_asihpi_aesebu_tx_format_get;
1785 snd_control.put = snd_asihpi_aesebu_tx_format_put;
1787 return ctl_add(card, &snd_control, asihpi);
1790 /*------------------------------------------------------------
1792 ------------------------------------------------------------*/
1796 static int snd_asihpi_tuner_gain_info(struct snd_kcontrol *kcontrol,
1797 struct snd_ctl_elem_info *uinfo)
1799 u32 h_control = kcontrol->private_value;
1804 for (idx = 0; idx < 3; idx++) {
1805 err = hpi_tuner_query_gain(ss, h_control,
1806 idx, &gain_range[idx]);
1811 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1813 uinfo->value.integer.min = ((int)gain_range[0]) / HPI_UNITS_PER_dB;
1814 uinfo->value.integer.max = ((int)gain_range[1]) / HPI_UNITS_PER_dB;
1815 uinfo->value.integer.step = ((int) gain_range[2]) / HPI_UNITS_PER_dB;
1819 static int snd_asihpi_tuner_gain_get(struct snd_kcontrol *kcontrol,
1820 struct snd_ctl_elem_value *ucontrol)
1823 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1825 u32 h_control = kcontrol->private_value;
1828 hpi_handle_error(hpi_tuner_get_gain(ss, h_control, &gain));
1829 ucontrol->value.integer.value[0] = gain / HPI_UNITS_PER_dB;
1834 static int snd_asihpi_tuner_gain_put(struct snd_kcontrol *kcontrol,
1835 struct snd_ctl_elem_value *ucontrol)
1838 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1840 u32 h_control = kcontrol->private_value;
1843 gain = (ucontrol->value.integer.value[0]) * HPI_UNITS_PER_dB;
1844 hpi_handle_error(hpi_tuner_set_gain(ss, h_control, gain));
1851 static int asihpi_tuner_band_query(struct snd_kcontrol *kcontrol,
1852 u16 *band_list, u32 len) {
1853 u32 h_control = kcontrol->private_value;
1857 for (i = 0; i < len; i++) {
1858 err = hpi_tuner_query_band(ss,
1859 h_control, i, &band_list[i]);
1864 if (err && (err != HPI_ERROR_INVALID_OBJ_INDEX))
1870 static int snd_asihpi_tuner_band_info(struct snd_kcontrol *kcontrol,
1871 struct snd_ctl_elem_info *uinfo)
1873 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1876 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1877 HPI_TUNER_BAND_LAST);
1882 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1884 uinfo->value.enumerated.items = num_bands;
1886 if (num_bands > 0) {
1887 if (uinfo->value.enumerated.item >=
1888 uinfo->value.enumerated.items)
1889 uinfo->value.enumerated.item =
1890 uinfo->value.enumerated.items - 1;
1892 strcpy(uinfo->value.enumerated.name,
1893 asihpi_tuner_band_names[
1894 tuner_bands[uinfo->value.enumerated.item]]);
1900 static int snd_asihpi_tuner_band_get(struct snd_kcontrol *kcontrol,
1901 struct snd_ctl_elem_value *ucontrol)
1903 u32 h_control = kcontrol->private_value;
1905 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1908 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1911 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1912 HPI_TUNER_BAND_LAST);
1914 hpi_handle_error(hpi_tuner_get_band(ss, h_control, &band));
1916 ucontrol->value.enumerated.item[0] = -1;
1917 for (idx = 0; idx < HPI_TUNER_BAND_LAST; idx++)
1918 if (tuner_bands[idx] == band) {
1919 ucontrol->value.enumerated.item[0] = idx;
1926 static int snd_asihpi_tuner_band_put(struct snd_kcontrol *kcontrol,
1927 struct snd_ctl_elem_value *ucontrol)
1930 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1932 u32 h_control = kcontrol->private_value;
1934 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1937 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1938 HPI_TUNER_BAND_LAST);
1940 band = tuner_bands[ucontrol->value.enumerated.item[0]];
1941 hpi_handle_error(hpi_tuner_set_band(ss, h_control, band));
1948 static int snd_asihpi_tuner_freq_info(struct snd_kcontrol *kcontrol,
1949 struct snd_ctl_elem_info *uinfo)
1951 u32 h_control = kcontrol->private_value;
1953 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1954 u16 num_bands = 0, band_iter, idx;
1955 u32 freq_range[3], temp_freq_range[3];
1957 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1958 HPI_TUNER_BAND_LAST);
1960 freq_range[0] = INT_MAX;
1962 freq_range[2] = INT_MAX;
1964 for (band_iter = 0; band_iter < num_bands; band_iter++) {
1965 for (idx = 0; idx < 3; idx++) {
1966 err = hpi_tuner_query_frequency(ss, h_control,
1967 idx, tuner_bands[band_iter],
1968 &temp_freq_range[idx]);
1973 /* skip band with bogus stepping */
1974 if (temp_freq_range[2] <= 0)
1977 if (temp_freq_range[0] < freq_range[0])
1978 freq_range[0] = temp_freq_range[0];
1979 if (temp_freq_range[1] > freq_range[1])
1980 freq_range[1] = temp_freq_range[1];
1981 if (temp_freq_range[2] < freq_range[2])
1982 freq_range[2] = temp_freq_range[2];
1985 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1987 uinfo->value.integer.min = ((int)freq_range[0]);
1988 uinfo->value.integer.max = ((int)freq_range[1]);
1989 uinfo->value.integer.step = ((int)freq_range[2]);
1993 static int snd_asihpi_tuner_freq_get(struct snd_kcontrol *kcontrol,
1994 struct snd_ctl_elem_value *ucontrol)
1996 u32 h_control = kcontrol->private_value;
1999 hpi_handle_error(hpi_tuner_get_frequency(ss, h_control, &freq));
2000 ucontrol->value.integer.value[0] = freq;
2005 static int snd_asihpi_tuner_freq_put(struct snd_kcontrol *kcontrol,
2006 struct snd_ctl_elem_value *ucontrol)
2008 u32 h_control = kcontrol->private_value;
2011 freq = ucontrol->value.integer.value[0];
2012 hpi_handle_error(hpi_tuner_set_frequency(ss, h_control, freq));
2017 /* Tuner control group initializer */
2018 static int __devinit snd_asihpi_tuner_add(struct snd_card_asihpi *asihpi,
2019 struct hpi_control *hpi_ctl)
2021 struct snd_card *card = asihpi->card;
2022 struct snd_kcontrol_new snd_control;
2024 snd_control.private_value = hpi_ctl->h_control;
2025 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2027 if (!hpi_tuner_get_gain(ss, hpi_ctl->h_control, NULL)) {
2028 asihpi_ctl_init(&snd_control, hpi_ctl, "gain");
2029 snd_control.info = snd_asihpi_tuner_gain_info;
2030 snd_control.get = snd_asihpi_tuner_gain_get;
2031 snd_control.put = snd_asihpi_tuner_gain_put;
2033 if (ctl_add(card, &snd_control, asihpi) < 0)
2037 asihpi_ctl_init(&snd_control, hpi_ctl, "band");
2038 snd_control.info = snd_asihpi_tuner_band_info;
2039 snd_control.get = snd_asihpi_tuner_band_get;
2040 snd_control.put = snd_asihpi_tuner_band_put;
2042 if (ctl_add(card, &snd_control, asihpi) < 0)
2045 asihpi_ctl_init(&snd_control, hpi_ctl, "freq");
2046 snd_control.info = snd_asihpi_tuner_freq_info;
2047 snd_control.get = snd_asihpi_tuner_freq_get;
2048 snd_control.put = snd_asihpi_tuner_freq_put;
2050 return ctl_add(card, &snd_control, asihpi);
2053 /*------------------------------------------------------------
2055 ------------------------------------------------------------*/
2056 static int snd_asihpi_meter_info(struct snd_kcontrol *kcontrol,
2057 struct snd_ctl_elem_info *uinfo)
2059 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2060 uinfo->count = HPI_MAX_CHANNELS;
2061 uinfo->value.integer.min = 0;
2062 uinfo->value.integer.max = 0x7FFFFFFF;
2066 /* linear values for 10dB steps */
2067 static int log2lin[] = {
2068 0x7FFFFFFF, /* 0dB */
2074 2147484, /* -60dB */
2089 static int snd_asihpi_meter_get(struct snd_kcontrol *kcontrol,
2090 struct snd_ctl_elem_value *ucontrol)
2092 u32 h_control = kcontrol->private_value;
2093 short an_gain_mB[HPI_MAX_CHANNELS], i;
2096 err = hpi_meter_get_peak(ss, h_control, an_gain_mB);
2098 for (i = 0; i < HPI_MAX_CHANNELS; i++) {
2100 ucontrol->value.integer.value[i] = 0;
2101 } else if (an_gain_mB[i] >= 0) {
2102 ucontrol->value.integer.value[i] =
2103 an_gain_mB[i] << 16;
2105 /* -ve is log value in millibels < -60dB,
2106 * convert to (roughly!) linear,
2108 ucontrol->value.integer.value[i] =
2109 log2lin[an_gain_mB[i] / -1000];
2115 static int __devinit snd_asihpi_meter_add(struct snd_card_asihpi *asihpi,
2116 struct hpi_control *hpi_ctl, int subidx)
2118 struct snd_card *card = asihpi->card;
2119 struct snd_kcontrol_new snd_control;
2121 asihpi_ctl_init(&snd_control, hpi_ctl, "meter");
2122 snd_control.access =
2123 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
2124 snd_control.info = snd_asihpi_meter_info;
2125 snd_control.get = snd_asihpi_meter_get;
2127 snd_control.index = subidx;
2129 return ctl_add(card, &snd_control, asihpi);
2132 /*------------------------------------------------------------
2133 Multiplexer controls
2134 ------------------------------------------------------------*/
2135 static int snd_card_asihpi_mux_count_sources(struct snd_kcontrol *snd_control)
2137 u32 h_control = snd_control->private_value;
2138 struct hpi_control hpi_ctl;
2140 for (s = 0; s < 32; s++) {
2141 err = hpi_multiplexer_query_source(ss, h_control, s,
2152 static int snd_asihpi_mux_info(struct snd_kcontrol *kcontrol,
2153 struct snd_ctl_elem_info *uinfo)
2156 u16 src_node_type, src_node_index;
2157 u32 h_control = kcontrol->private_value;
2159 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2161 uinfo->value.enumerated.items =
2162 snd_card_asihpi_mux_count_sources(kcontrol);
2164 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2165 uinfo->value.enumerated.item =
2166 uinfo->value.enumerated.items - 1;
2169 hpi_multiplexer_query_source(ss, h_control,
2170 uinfo->value.enumerated.item,
2171 &src_node_type, &src_node_index);
2173 sprintf(uinfo->value.enumerated.name, "%s %d",
2174 asihpi_src_names[src_node_type - HPI_SOURCENODE_BASE],
2179 static int snd_asihpi_mux_get(struct snd_kcontrol *kcontrol,
2180 struct snd_ctl_elem_value *ucontrol)
2182 u32 h_control = kcontrol->private_value;
2183 u16 source_type, source_index;
2184 u16 src_node_type, src_node_index;
2187 hpi_handle_error(hpi_multiplexer_get_source(ss, h_control,
2188 &source_type, &source_index));
2189 /* Should cache this search result! */
2190 for (s = 0; s < 256; s++) {
2191 if (hpi_multiplexer_query_source(ss, h_control, s,
2192 &src_node_type, &src_node_index))
2195 if ((source_type == src_node_type)
2196 && (source_index == src_node_index)) {
2197 ucontrol->value.enumerated.item[0] = s;
2201 snd_printd(KERN_WARNING
2202 "control %x failed to match mux source %hu %hu\n",
2203 h_control, source_type, source_index);
2204 ucontrol->value.enumerated.item[0] = 0;
2208 static int snd_asihpi_mux_put(struct snd_kcontrol *kcontrol,
2209 struct snd_ctl_elem_value *ucontrol)
2212 u32 h_control = kcontrol->private_value;
2213 u16 source_type, source_index;
2218 e = hpi_multiplexer_query_source(ss, h_control,
2219 ucontrol->value.enumerated.item[0],
2220 &source_type, &source_index);
2223 hpi_multiplexer_set_source(ss, h_control,
2224 source_type, source_index));
2229 static int __devinit snd_asihpi_mux_add(struct snd_card_asihpi *asihpi,
2230 struct hpi_control *hpi_ctl)
2232 struct snd_card *card = asihpi->card;
2233 struct snd_kcontrol_new snd_control;
2236 asihpi_ctl_init(&snd_control, hpi_ctl, "multiplexer");
2238 asihpi_ctl_init(&snd_control, hpi_ctl, "route");
2240 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2241 snd_control.info = snd_asihpi_mux_info;
2242 snd_control.get = snd_asihpi_mux_get;
2243 snd_control.put = snd_asihpi_mux_put;
2245 return ctl_add(card, &snd_control, asihpi);
2249 /*------------------------------------------------------------
2250 Channel mode controls
2251 ------------------------------------------------------------*/
2252 static int snd_asihpi_cmode_info(struct snd_kcontrol *kcontrol,
2253 struct snd_ctl_elem_info *uinfo)
2255 static char *mode_names[HPI_CHANNEL_MODE_LAST] = {
2257 "from_left", "from_right",
2258 "to_left", "to_right"
2261 u32 h_control = kcontrol->private_value;
2265 /* HPI channel mode values can be from 1 to 6
2266 Some adapters only support a contiguous subset
2268 for (i = 0; i < HPI_CHANNEL_MODE_LAST; i++)
2269 if (hpi_channel_mode_query_mode(
2270 ss, h_control, i, &mode))
2273 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2275 uinfo->value.enumerated.items = i;
2277 if (uinfo->value.enumerated.item >= i)
2278 uinfo->value.enumerated.item = i - 1;
2280 strcpy(uinfo->value.enumerated.name,
2281 mode_names[uinfo->value.enumerated.item]);
2286 static int snd_asihpi_cmode_get(struct snd_kcontrol *kcontrol,
2287 struct snd_ctl_elem_value *ucontrol)
2289 u32 h_control = kcontrol->private_value;
2292 if (hpi_channel_mode_get(ss, h_control, &mode))
2295 ucontrol->value.enumerated.item[0] = mode - 1;
2300 static int snd_asihpi_cmode_put(struct snd_kcontrol *kcontrol,
2301 struct snd_ctl_elem_value *ucontrol)
2304 u32 h_control = kcontrol->private_value;
2308 hpi_handle_error(hpi_channel_mode_set(ss, h_control,
2309 ucontrol->value.enumerated.item[0] + 1));
2314 static int __devinit snd_asihpi_cmode_add(struct snd_card_asihpi *asihpi,
2315 struct hpi_control *hpi_ctl)
2317 struct snd_card *card = asihpi->card;
2318 struct snd_kcontrol_new snd_control;
2320 asihpi_ctl_init(&snd_control, hpi_ctl, "channel mode");
2321 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2322 snd_control.info = snd_asihpi_cmode_info;
2323 snd_control.get = snd_asihpi_cmode_get;
2324 snd_control.put = snd_asihpi_cmode_put;
2326 return ctl_add(card, &snd_control, asihpi);
2329 /*------------------------------------------------------------
2330 Sampleclock source controls
2331 ------------------------------------------------------------*/
2333 static char *sampleclock_sources[MAX_CLOCKSOURCES] =
2334 { "N/A", "local PLL", "AES/EBU sync", "word external", "word header",
2335 "SMPTE", "AES/EBU in1", "auto", "network", "invalid",
2337 "AES/EBU in2", "AES/EBU in3", "AES/EBU in4", "AES/EBU in5",
2338 "AES/EBU in6", "AES/EBU in7", "AES/EBU in8"};
2342 static int snd_asihpi_clksrc_info(struct snd_kcontrol *kcontrol,
2343 struct snd_ctl_elem_info *uinfo)
2345 struct snd_card_asihpi *asihpi =
2346 (struct snd_card_asihpi *)(kcontrol->private_data);
2347 struct clk_cache *clkcache = &asihpi->cc;
2348 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2350 uinfo->value.enumerated.items = clkcache->count;
2352 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2353 uinfo->value.enumerated.item =
2354 uinfo->value.enumerated.items - 1;
2356 strcpy(uinfo->value.enumerated.name,
2357 clkcache->s[uinfo->value.enumerated.item].name);
2361 static int snd_asihpi_clksrc_get(struct snd_kcontrol *kcontrol,
2362 struct snd_ctl_elem_value *ucontrol)
2364 struct snd_card_asihpi *asihpi =
2365 (struct snd_card_asihpi *)(kcontrol->private_data);
2366 struct clk_cache *clkcache = &asihpi->cc;
2367 u32 h_control = kcontrol->private_value;
2368 u16 source, srcindex = 0;
2371 ucontrol->value.enumerated.item[0] = 0;
2372 if (hpi_sample_clock_get_source(ss, h_control, &source))
2375 if (source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
2376 if (hpi_sample_clock_get_source_index(ss, h_control, &srcindex))
2379 for (i = 0; i < clkcache->count; i++)
2380 if ((clkcache->s[i].source == source) &&
2381 (clkcache->s[i].index == srcindex))
2384 ucontrol->value.enumerated.item[0] = i;
2389 static int snd_asihpi_clksrc_put(struct snd_kcontrol *kcontrol,
2390 struct snd_ctl_elem_value *ucontrol)
2392 struct snd_card_asihpi *asihpi =
2393 (struct snd_card_asihpi *)(kcontrol->private_data);
2394 struct clk_cache *clkcache = &asihpi->cc;
2396 u32 h_control = kcontrol->private_value;
2399 item = ucontrol->value.enumerated.item[0];
2400 if (item >= clkcache->count)
2401 item = clkcache->count-1;
2403 hpi_handle_error(hpi_sample_clock_set_source(ss,
2404 h_control, clkcache->s[item].source));
2406 if (clkcache->s[item].source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
2407 hpi_handle_error(hpi_sample_clock_set_source_index(ss,
2408 h_control, clkcache->s[item].index));
2412 /*------------------------------------------------------------
2414 ------------------------------------------------------------*/
2415 /* Need to change this to enumerated control with list of rates */
2416 static int snd_asihpi_clklocal_info(struct snd_kcontrol *kcontrol,
2417 struct snd_ctl_elem_info *uinfo)
2419 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2421 uinfo->value.integer.min = 8000;
2422 uinfo->value.integer.max = 192000;
2423 uinfo->value.integer.step = 100;
2428 static int snd_asihpi_clklocal_get(struct snd_kcontrol *kcontrol,
2429 struct snd_ctl_elem_value *ucontrol)
2431 u32 h_control = kcontrol->private_value;
2435 e = hpi_sample_clock_get_local_rate(ss, h_control, &rate);
2437 ucontrol->value.integer.value[0] = rate;
2439 ucontrol->value.integer.value[0] = 0;
2443 static int snd_asihpi_clklocal_put(struct snd_kcontrol *kcontrol,
2444 struct snd_ctl_elem_value *ucontrol)
2447 u32 h_control = kcontrol->private_value;
2449 /* change = asihpi->mixer_clkrate[addr][0] != left ||
2450 asihpi->mixer_clkrate[addr][1] != right;
2453 hpi_handle_error(hpi_sample_clock_set_local_rate(ss, h_control,
2454 ucontrol->value.integer.value[0]));
2458 static int snd_asihpi_clkrate_info(struct snd_kcontrol *kcontrol,
2459 struct snd_ctl_elem_info *uinfo)
2461 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2463 uinfo->value.integer.min = 8000;
2464 uinfo->value.integer.max = 192000;
2465 uinfo->value.integer.step = 100;
2470 static int snd_asihpi_clkrate_get(struct snd_kcontrol *kcontrol,
2471 struct snd_ctl_elem_value *ucontrol)
2473 u32 h_control = kcontrol->private_value;
2477 e = hpi_sample_clock_get_sample_rate(ss, h_control, &rate);
2479 ucontrol->value.integer.value[0] = rate;
2481 ucontrol->value.integer.value[0] = 0;
2485 static int __devinit snd_asihpi_sampleclock_add(struct snd_card_asihpi *asihpi,
2486 struct hpi_control *hpi_ctl)
2488 struct snd_card *card = asihpi->card;
2489 struct snd_kcontrol_new snd_control;
2491 struct clk_cache *clkcache = &asihpi->cc;
2492 u32 hSC = hpi_ctl->h_control;
2497 snd_control.private_value = hpi_ctl->h_control;
2499 clkcache->has_local = 0;
2501 for (i = 0; i <= HPI_SAMPLECLOCK_SOURCE_LAST; i++) {
2502 if (hpi_sample_clock_query_source(ss, hSC,
2505 clkcache->s[i].source = source;
2506 clkcache->s[i].index = 0;
2507 clkcache->s[i].name = sampleclock_sources[source];
2508 if (source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
2510 if (source == HPI_SAMPLECLOCK_SOURCE_LOCAL)
2511 clkcache->has_local = 1;
2514 /* already will have picked up index 0 above */
2515 for (j = 1; j < 8; j++) {
2516 if (hpi_sample_clock_query_source_index(ss, hSC,
2517 j, HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT,
2520 clkcache->s[i].source =
2521 HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT;
2522 clkcache->s[i].index = j;
2523 clkcache->s[i].name = sampleclock_sources[
2524 j+HPI_SAMPLECLOCK_SOURCE_LAST];
2527 clkcache->count = i;
2529 asihpi_ctl_init(&snd_control, hpi_ctl, "source");
2530 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE ;
2531 snd_control.info = snd_asihpi_clksrc_info;
2532 snd_control.get = snd_asihpi_clksrc_get;
2533 snd_control.put = snd_asihpi_clksrc_put;
2534 if (ctl_add(card, &snd_control, asihpi) < 0)
2538 if (clkcache->has_local) {
2539 asihpi_ctl_init(&snd_control, hpi_ctl, "local_rate");
2540 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE ;
2541 snd_control.info = snd_asihpi_clklocal_info;
2542 snd_control.get = snd_asihpi_clklocal_get;
2543 snd_control.put = snd_asihpi_clklocal_put;
2546 if (ctl_add(card, &snd_control, asihpi) < 0)
2550 asihpi_ctl_init(&snd_control, hpi_ctl, "rate");
2551 snd_control.access =
2552 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
2553 snd_control.info = snd_asihpi_clkrate_info;
2554 snd_control.get = snd_asihpi_clkrate_get;
2556 return ctl_add(card, &snd_control, asihpi);
2558 /*------------------------------------------------------------
2560 ------------------------------------------------------------*/
2562 static int __devinit snd_card_asihpi_mixer_new(struct snd_card_asihpi *asihpi)
2564 struct snd_card *card = asihpi->card;
2565 unsigned int idx = 0;
2566 unsigned int subindex = 0;
2568 struct hpi_control hpi_ctl, prev_ctl;
2570 if (snd_BUG_ON(!asihpi))
2572 strcpy(card->mixername, "asihpi mixer");
2575 hpi_mixer_open(ss, asihpi->adapter_index,
2577 hpi_handle_error(err);
2581 memset(&prev_ctl, 0, sizeof(prev_ctl));
2582 prev_ctl.control_type = -1;
2584 for (idx = 0; idx < 2000; idx++) {
2585 err = hpi_mixer_get_control_by_index(
2586 ss, asihpi->h_mixer,
2588 &hpi_ctl.src_node_type,
2589 &hpi_ctl.src_node_index,
2590 &hpi_ctl.dst_node_type,
2591 &hpi_ctl.dst_node_index,
2592 &hpi_ctl.control_type,
2593 &hpi_ctl.h_control);
2595 if (err == HPI_ERROR_CONTROL_DISABLED) {
2597 snd_printk(KERN_INFO
2598 "disabled HPI control(%d)\n",
2606 hpi_ctl.src_node_type -= HPI_SOURCENODE_BASE;
2607 hpi_ctl.dst_node_type -= HPI_DESTNODE_BASE;
2609 /* ASI50xx in SSX mode has multiple meters on the same node.
2610 Use subindex to create distinct ALSA controls
2611 for any duplicated controls.
2613 if ((hpi_ctl.control_type == prev_ctl.control_type) &&
2614 (hpi_ctl.src_node_type == prev_ctl.src_node_type) &&
2615 (hpi_ctl.src_node_index == prev_ctl.src_node_index) &&
2616 (hpi_ctl.dst_node_type == prev_ctl.dst_node_type) &&
2617 (hpi_ctl.dst_node_index == prev_ctl.dst_node_index))
2624 switch (hpi_ctl.control_type) {
2625 case HPI_CONTROL_VOLUME:
2626 err = snd_asihpi_volume_add(asihpi, &hpi_ctl);
2628 case HPI_CONTROL_LEVEL:
2629 err = snd_asihpi_level_add(asihpi, &hpi_ctl);
2631 case HPI_CONTROL_MULTIPLEXER:
2632 err = snd_asihpi_mux_add(asihpi, &hpi_ctl);
2634 case HPI_CONTROL_CHANNEL_MODE:
2635 err = snd_asihpi_cmode_add(asihpi, &hpi_ctl);
2637 case HPI_CONTROL_METER:
2638 err = snd_asihpi_meter_add(asihpi, &hpi_ctl, subindex);
2640 case HPI_CONTROL_SAMPLECLOCK:
2641 err = snd_asihpi_sampleclock_add(
2644 case HPI_CONTROL_CONNECTION: /* ignore these */
2646 case HPI_CONTROL_TUNER:
2647 err = snd_asihpi_tuner_add(asihpi, &hpi_ctl);
2649 case HPI_CONTROL_AESEBU_TRANSMITTER:
2650 err = snd_asihpi_aesebu_tx_add(asihpi, &hpi_ctl);
2652 case HPI_CONTROL_AESEBU_RECEIVER:
2653 err = snd_asihpi_aesebu_rx_add(asihpi, &hpi_ctl);
2655 case HPI_CONTROL_VOX:
2656 case HPI_CONTROL_BITSTREAM:
2657 case HPI_CONTROL_MICROPHONE:
2658 case HPI_CONTROL_PARAMETRIC_EQ:
2659 case HPI_CONTROL_COMPANDER:
2662 snd_printk(KERN_INFO
2663 "untranslated HPI control"
2664 "(%d) %d %d %d %d %d\n",
2666 hpi_ctl.control_type,
2667 hpi_ctl.src_node_type,
2668 hpi_ctl.src_node_index,
2669 hpi_ctl.dst_node_type,
2670 hpi_ctl.dst_node_index);
2676 if (HPI_ERROR_INVALID_OBJ_INDEX != err)
2677 hpi_handle_error(err);
2679 snd_printk(KERN_INFO "%d mixer controls found\n", idx);
2684 /*------------------------------------------------------------
2686 ------------------------------------------------------------*/
2689 snd_asihpi_proc_read(struct snd_info_entry *entry,
2690 struct snd_info_buffer *buffer)
2692 struct snd_card_asihpi *asihpi = entry->private_data;
2699 snd_iprintf(buffer, "ASIHPI driver proc file\n");
2701 "adapter ID=%4X\n_index=%d\n"
2702 "num_outstreams=%d\n_num_instreams=%d\n",
2703 asihpi->type, asihpi->adapter_index,
2704 asihpi->num_outstreams, asihpi->num_instreams);
2706 version = asihpi->version;
2708 "serial#=%d\n_hw version %c%d\nDSP code version %03d\n",
2709 asihpi->serial_number, ((version >> 3) & 0xf) + 'A',
2711 ((version >> 13) * 100) + ((version >> 7) & 0x3f));
2713 err = hpi_mixer_get_control(ss, asihpi->h_mixer,
2714 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
2715 HPI_CONTROL_SAMPLECLOCK, &h_control);
2718 err = hpi_sample_clock_get_sample_rate(ss,
2720 err += hpi_sample_clock_get_source(ss, h_control, &source);
2723 snd_iprintf(buffer, "sample_clock=%d_hz, source %s\n",
2724 rate, sampleclock_sources[source]);
2730 static void __devinit snd_asihpi_proc_init(struct snd_card_asihpi *asihpi)
2732 struct snd_info_entry *entry;
2734 if (!snd_card_proc_new(asihpi->card, "info", &entry))
2735 snd_info_set_text_ops(entry, asihpi, snd_asihpi_proc_read);
2738 /*------------------------------------------------------------
2740 ------------------------------------------------------------*/
2742 static int snd_asihpi_hpi_open(struct snd_hwdep *hw, struct file *file)
2744 if (enable_hpi_hwdep)
2751 static int snd_asihpi_hpi_release(struct snd_hwdep *hw, struct file *file)
2753 if (enable_hpi_hwdep)
2754 return asihpi_hpi_release(file);
2759 static int snd_asihpi_hpi_ioctl(struct snd_hwdep *hw, struct file *file,
2760 unsigned int cmd, unsigned long arg)
2762 if (enable_hpi_hwdep)
2763 return asihpi_hpi_ioctl(file, cmd, arg);
2769 /* results in /dev/snd/hwC#D0 file for each card with index #
2770 also /proc/asound/hwdep will contain '#-00: asihpi (HPI) for each card'
2772 static int __devinit snd_asihpi_hpi_new(struct snd_card_asihpi *asihpi,
2773 int device, struct snd_hwdep **rhwdep)
2775 struct snd_hwdep *hw;
2780 err = snd_hwdep_new(asihpi->card, "HPI", device, &hw);
2783 strcpy(hw->name, "asihpi (HPI)");
2784 hw->iface = SNDRV_HWDEP_IFACE_LAST;
2785 hw->ops.open = snd_asihpi_hpi_open;
2786 hw->ops.ioctl = snd_asihpi_hpi_ioctl;
2787 hw->ops.release = snd_asihpi_hpi_release;
2788 hw->private_data = asihpi;
2794 /*------------------------------------------------------------
2796 ------------------------------------------------------------*/
2797 static int __devinit snd_asihpi_probe(struct pci_dev *pci_dev,
2798 const struct pci_device_id *pci_id)
2805 struct hpi_adapter *hpi_card;
2806 struct snd_card *card;
2807 struct snd_card_asihpi *asihpi;
2813 if (dev >= SNDRV_CARDS)
2816 /* Should this be enable[hpi_card->index] ? */
2822 err = asihpi_adapter_probe(pci_dev, pci_id);
2826 hpi_card = pci_get_drvdata(pci_dev);
2827 /* first try to give the card the same index as its hardware index */
2828 err = snd_card_create(hpi_card->index,
2829 id[hpi_card->index], THIS_MODULE,
2830 sizeof(struct snd_card_asihpi),
2833 /* if that fails, try the default index==next available */
2835 snd_card_create(index[dev], id[dev],
2837 sizeof(struct snd_card_asihpi),
2841 snd_printk(KERN_WARNING
2842 "**** WARNING **** adapter index %d->ALSA index %d\n",
2843 hpi_card->index, card->number);
2846 asihpi = (struct snd_card_asihpi *) card->private_data;
2847 asihpi->card = card;
2848 asihpi->pci = hpi_card->pci;
2849 asihpi->adapter_index = hpi_card->index;
2850 hpi_handle_error(hpi_adapter_get_info(ss,
2851 asihpi->adapter_index,
2852 &asihpi->num_outstreams,
2853 &asihpi->num_instreams,
2855 &asihpi->serial_number, &asihpi->type));
2857 version = asihpi->version;
2858 snd_printk(KERN_INFO "adapter ID=%4X index=%d num_outstreams=%d "
2859 "num_instreams=%d S/N=%d\n"
2860 "hw version %c%d DSP code version %03d\n",
2861 asihpi->type, asihpi->adapter_index,
2862 asihpi->num_outstreams,
2863 asihpi->num_instreams, asihpi->serial_number,
2864 ((version >> 3) & 0xf) + 'A',
2866 ((version >> 13) * 100) + ((version >> 7) & 0x3f));
2868 pcm_substreams = asihpi->num_outstreams;
2869 if (pcm_substreams < asihpi->num_instreams)
2870 pcm_substreams = asihpi->num_instreams;
2872 err = hpi_adapter_get_property(ss, asihpi->adapter_index,
2873 HPI_ADAPTER_PROPERTY_CAPS1,
2874 NULL, &asihpi->support_grouping);
2876 asihpi->support_grouping = 0;
2878 err = hpi_adapter_get_property(ss, asihpi->adapter_index,
2879 HPI_ADAPTER_PROPERTY_CAPS2,
2880 &asihpi->support_mrx, NULL);
2882 asihpi->support_mrx = 0;
2884 err = hpi_adapter_get_property(ss, asihpi->adapter_index,
2885 HPI_ADAPTER_PROPERTY_INTERVAL,
2886 NULL, &asihpi->update_interval_frames);
2888 asihpi->update_interval_frames = 512;
2890 hpi_handle_error(hpi_instream_open(ss, asihpi->adapter_index,
2893 err = hpi_instream_host_buffer_free(ss, h_stream);
2894 asihpi->support_mmap = (!err);
2896 hpi_handle_error(hpi_instream_close(ss, h_stream));
2898 err = hpi_adapter_get_property(ss, asihpi->adapter_index,
2899 HPI_ADAPTER_PROPERTY_CURCHANNELS,
2900 &asihpi->in_max_chans, &asihpi->out_max_chans);
2902 asihpi->in_max_chans = 2;
2903 asihpi->out_max_chans = 2;
2906 snd_printk(KERN_INFO "supports mmap:%d grouping:%d mrx:%d\n",
2907 asihpi->support_mmap,
2908 asihpi->support_grouping,
2913 err = snd_card_asihpi_pcm_new(asihpi, 0, pcm_substreams);
2915 snd_printk(KERN_ERR "pcm_new failed\n");
2918 err = snd_card_asihpi_mixer_new(asihpi);
2920 snd_printk(KERN_ERR "mixer_new failed\n");
2924 err = hpi_mixer_get_control(ss, asihpi->h_mixer,
2925 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
2926 HPI_CONTROL_SAMPLECLOCK, &h_control);
2929 err = hpi_sample_clock_set_local_rate(
2930 ss, h_control, adapter_fs);
2932 snd_asihpi_proc_init(asihpi);
2934 /* always create, can be enabled or disabled dynamically
2935 by enable_hwdep module param*/
2936 snd_asihpi_hpi_new(asihpi, 0, NULL);
2938 if (asihpi->support_mmap)
2939 strcpy(card->driver, "ASIHPI-MMAP");
2941 strcpy(card->driver, "ASIHPI");
2943 sprintf(card->shortname, "AudioScience ASI%4X", asihpi->type);
2944 sprintf(card->longname, "%s %i",
2945 card->shortname, asihpi->adapter_index);
2946 err = snd_card_register(card);
2948 hpi_card->snd_card_asihpi = card;
2953 snd_card_free(card);
2954 snd_printk(KERN_ERR "snd_asihpi_probe error %d\n", err);
2959 static void __devexit snd_asihpi_remove(struct pci_dev *pci_dev)
2961 struct hpi_adapter *hpi_card = pci_get_drvdata(pci_dev);
2963 snd_card_free(hpi_card->snd_card_asihpi);
2964 hpi_card->snd_card_asihpi = NULL;
2965 asihpi_adapter_remove(pci_dev);
2968 static DEFINE_PCI_DEVICE_TABLE(asihpi_pci_tbl) = {
2969 {HPI_PCI_VENDOR_ID_TI, HPI_PCI_DEV_ID_DSP6205,
2970 HPI_PCI_VENDOR_ID_AUDIOSCIENCE, PCI_ANY_ID, 0, 0,
2971 (kernel_ulong_t)HPI_6205},
2972 {HPI_PCI_VENDOR_ID_TI, HPI_PCI_DEV_ID_PCI2040,
2973 HPI_PCI_VENDOR_ID_AUDIOSCIENCE, PCI_ANY_ID, 0, 0,
2974 (kernel_ulong_t)HPI_6000},
2977 MODULE_DEVICE_TABLE(pci, asihpi_pci_tbl);
2979 static struct pci_driver driver = {
2981 .id_table = asihpi_pci_tbl,
2982 .probe = snd_asihpi_probe,
2983 .remove = __devexit_p(snd_asihpi_remove),
2985 /* .suspend = snd_asihpi_suspend,
2986 .resume = snd_asihpi_resume, */
2990 static int __init snd_asihpi_init(void)
2993 return pci_register_driver(&driver);
2996 static void __exit snd_asihpi_exit(void)
2999 pci_unregister_driver(&driver);
3003 module_init(snd_asihpi_init)
3004 module_exit(snd_asihpi_exit)