blob: 0ac1f98d91a1f2196fb9cd4aa93b4c79c2d57aad [file] [log] [blame]
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001/*
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 */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +130024
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020025/* >0: print Hw params, timer vars. >1: print stream write/copy sizes */
26#define REALLY_VERBOSE_LOGGING 0
27
28#if REALLY_VERBOSE_LOGGING
29#define VPRINTK1 snd_printd
30#else
31#define VPRINTK1(...)
32#endif
33
34#if REALLY_VERBOSE_LOGGING > 1
35#define VPRINTK2 snd_printd
36#else
37#define VPRINTK2(...)
38#endif
39
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020040#include "hpi_internal.h"
41#include "hpimsginit.h"
42#include "hpioctl.h"
43
44#include <linux/pci.h>
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +130045#include <linux/version.h>
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020046#include <linux/init.h>
47#include <linux/jiffies.h>
48#include <linux/slab.h>
49#include <linux/time.h>
50#include <linux/wait.h>
51#include <sound/core.h>
52#include <sound/control.h>
53#include <sound/pcm.h>
54#include <sound/pcm_params.h>
55#include <sound/info.h>
56#include <sound/initval.h>
57#include <sound/tlv.h>
58#include <sound/hwdep.h>
59
60
61MODULE_LICENSE("GPL");
62MODULE_AUTHOR("AudioScience inc. <support@audioscience.com>");
63MODULE_DESCRIPTION("AudioScience ALSA ASI5000 ASI6000 ASI87xx ASI89xx");
64
65static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* index 0-MAX */
66static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
67static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
68static int enable_hpi_hwdep = 1;
69
70module_param_array(index, int, NULL, S_IRUGO);
71MODULE_PARM_DESC(index, "ALSA index value for AudioScience soundcard.");
72
73module_param_array(id, charp, NULL, S_IRUGO);
74MODULE_PARM_DESC(id, "ALSA ID string for AudioScience soundcard.");
75
76module_param_array(enable, bool, NULL, S_IRUGO);
77MODULE_PARM_DESC(enable, "ALSA enable AudioScience soundcard.");
78
79module_param(enable_hpi_hwdep, bool, S_IRUGO|S_IWUSR);
80MODULE_PARM_DESC(enable_hpi_hwdep,
81 "ALSA enable HPI hwdep for AudioScience soundcard ");
82
83/* identify driver */
84#ifdef KERNEL_ALSA_BUILD
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +130085static char *build_info = "Built using headers from kernel source";
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020086module_param(build_info, charp, S_IRUGO);
87MODULE_PARM_DESC(build_info, "built using headers from kernel source");
88#else
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +130089static char *build_info = "Built within ALSA source";
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020090module_param(build_info, charp, S_IRUGO);
91MODULE_PARM_DESC(build_info, "built within ALSA source");
92#endif
93
94/* set to 1 to dump every control from adapter to log */
95static const int mixer_dump;
96
97#define DEFAULT_SAMPLERATE 44100
98static int adapter_fs = DEFAULT_SAMPLERATE;
99
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200100/* defaults */
101#define PERIODS_MIN 2
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300102#define PERIOD_BYTES_MIN 2048
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200103#define BUFFER_BYTES_MAX (512 * 1024)
104
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300105/* convert stream to character */
106#define SCHR(s) ((s == SNDRV_PCM_STREAM_PLAYBACK) ? 'P' : 'C')
107
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200108/*#define TIMER_MILLISECONDS 20
109#define FORCE_TIMER_JIFFIES ((TIMER_MILLISECONDS * HZ + 999)/1000)
110*/
111
112#define MAX_CLOCKSOURCES (HPI_SAMPLECLOCK_SOURCE_LAST + 1 + 7)
113
114struct clk_source {
115 int source;
116 int index;
117 char *name;
118};
119
120struct clk_cache {
121 int count;
122 int has_local;
123 struct clk_source s[MAX_CLOCKSOURCES];
124};
125
126/* Per card data */
127struct snd_card_asihpi {
128 struct snd_card *card;
129 struct pci_dev *pci;
130 u16 adapter_index;
131 u32 serial_number;
132 u16 type;
133 u16 version;
134 u16 num_outstreams;
135 u16 num_instreams;
136
137 u32 h_mixer;
138 struct clk_cache cc;
139
140 u16 support_mmap;
141 u16 support_grouping;
142 u16 support_mrx;
143 u16 update_interval_frames;
144 u16 in_max_chans;
145 u16 out_max_chans;
146};
147
148/* Per stream data */
149struct snd_card_asihpi_pcm {
150 struct timer_list timer;
151 unsigned int respawn_timer;
152 unsigned int hpi_buffer_attached;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300153 unsigned int buffer_bytes;
154 unsigned int period_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200155 unsigned int bytes_per_sec;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300156 unsigned int pcm_buf_host_rw_ofs; /* Host R/W pos */
157 unsigned int pcm_buf_dma_ofs; /* DMA R/W offset in buffer */
158 unsigned int pcm_buf_elapsed_dma_ofs; /* DMA R/W offset in buffer */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200159 struct snd_pcm_substream *substream;
160 u32 h_stream;
161 struct hpi_format format;
162};
163
164/* universal stream verbs work with out or in stream handles */
165
166/* Functions to allow driver to give a buffer to HPI for busmastering */
167
168static u16 hpi_stream_host_buffer_attach(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200169 u32 h_stream, /* handle to outstream. */
170 u32 size_in_bytes, /* size in bytes of bus mastering buffer */
171 u32 pci_address
172)
173{
174 struct hpi_message hm;
175 struct hpi_response hr;
176 unsigned int obj = hpi_handle_object(h_stream);
177
178 if (!h_stream)
179 return HPI_ERROR_INVALID_OBJ;
180 hpi_init_message_response(&hm, &hr, obj,
181 obj == HPI_OBJ_OSTREAM ?
182 HPI_OSTREAM_HOSTBUFFER_ALLOC :
183 HPI_ISTREAM_HOSTBUFFER_ALLOC);
184
185 hpi_handle_to_indexes(h_stream, &hm.adapter_index,
186 &hm.obj_index);
187
188 hm.u.d.u.buffer.buffer_size = size_in_bytes;
189 hm.u.d.u.buffer.pci_address = pci_address;
190 hm.u.d.u.buffer.command = HPI_BUFFER_CMD_INTERNAL_GRANTADAPTER;
191 hpi_send_recv(&hm, &hr);
192 return hr.error;
193}
194
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300195static u16 hpi_stream_host_buffer_detach(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200196{
197 struct hpi_message hm;
198 struct hpi_response hr;
199 unsigned int obj = hpi_handle_object(h_stream);
200
201 if (!h_stream)
202 return HPI_ERROR_INVALID_OBJ;
203
204 hpi_init_message_response(&hm, &hr, obj,
205 obj == HPI_OBJ_OSTREAM ?
206 HPI_OSTREAM_HOSTBUFFER_FREE :
207 HPI_ISTREAM_HOSTBUFFER_FREE);
208
209 hpi_handle_to_indexes(h_stream, &hm.adapter_index,
210 &hm.obj_index);
211 hm.u.d.u.buffer.command = HPI_BUFFER_CMD_INTERNAL_REVOKEADAPTER;
212 hpi_send_recv(&hm, &hr);
213 return hr.error;
214}
215
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300216static inline u16 hpi_stream_start(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200217{
218 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300219 return hpi_outstream_start(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200220 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300221 return hpi_instream_start(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200222}
223
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300224static inline u16 hpi_stream_stop(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200225{
226 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300227 return hpi_outstream_stop(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200228 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300229 return hpi_instream_stop(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200230}
231
232static inline u16 hpi_stream_get_info_ex(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200233 u32 h_stream,
234 u16 *pw_state,
235 u32 *pbuffer_size,
236 u32 *pdata_in_buffer,
237 u32 *psample_count,
238 u32 *pauxiliary_data
239)
240{
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300241 u16 e;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200242 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300243 e = hpi_outstream_get_info_ex(h_stream, pw_state,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200244 pbuffer_size, pdata_in_buffer,
245 psample_count, pauxiliary_data);
246 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300247 e = hpi_instream_get_info_ex(h_stream, pw_state,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200248 pbuffer_size, pdata_in_buffer,
249 psample_count, pauxiliary_data);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300250 return e;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200251}
252
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300253static inline u16 hpi_stream_group_add(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200254 u32 h_master,
255 u32 h_stream)
256{
257 if (hpi_handle_object(h_master) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300258 return hpi_outstream_group_add(h_master, h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200259 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300260 return hpi_instream_group_add(h_master, h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200261}
262
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300263static inline u16 hpi_stream_group_reset(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200264{
265 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300266 return hpi_outstream_group_reset(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200267 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300268 return hpi_instream_group_reset(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200269}
270
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300271static inline u16 hpi_stream_group_get_map(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200272 u32 h_stream, u32 *mo, u32 *mi)
273{
274 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300275 return hpi_outstream_group_get_map(h_stream, mo, mi);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200276 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300277 return hpi_instream_group_get_map(h_stream, mo, mi);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200278}
279
280static u16 handle_error(u16 err, int line, char *filename)
281{
282 if (err)
283 printk(KERN_WARNING
284 "in file %s, line %d: HPI error %d\n",
285 filename, line, err);
286 return err;
287}
288
289#define hpi_handle_error(x) handle_error(x, __LINE__, __FILE__)
290
291/***************************** GENERAL PCM ****************/
292#if REALLY_VERBOSE_LOGGING
293static void print_hwparams(struct snd_pcm_hw_params *p)
294{
295 snd_printd("HWPARAMS \n");
296 snd_printd("samplerate %d \n", params_rate(p));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300297 snd_printd("Channels %d \n", params_channels(p));
298 snd_printd("Format %d \n", params_format(p));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200299 snd_printd("subformat %d \n", params_subformat(p));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300300 snd_printd("Buffer bytes %d \n", params_buffer_bytes(p));
301 snd_printd("Period bytes %d \n", params_period_bytes(p));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200302 snd_printd("access %d \n", params_access(p));
303 snd_printd("period_size %d \n", params_period_size(p));
304 snd_printd("periods %d \n", params_periods(p));
305 snd_printd("buffer_size %d \n", params_buffer_size(p));
306}
307#else
308#define print_hwparams(x)
309#endif
310
311static snd_pcm_format_t hpi_to_alsa_formats[] = {
312 -1, /* INVALID */
313 SNDRV_PCM_FORMAT_U8, /* HPI_FORMAT_PCM8_UNSIGNED 1 */
314 SNDRV_PCM_FORMAT_S16, /* HPI_FORMAT_PCM16_SIGNED 2 */
315 -1, /* HPI_FORMAT_MPEG_L1 3 */
316 SNDRV_PCM_FORMAT_MPEG, /* HPI_FORMAT_MPEG_L2 4 */
317 SNDRV_PCM_FORMAT_MPEG, /* HPI_FORMAT_MPEG_L3 5 */
318 -1, /* HPI_FORMAT_DOLBY_AC2 6 */
319 -1, /* HPI_FORMAT_DOLBY_AC3 7 */
320 SNDRV_PCM_FORMAT_S16_BE,/* HPI_FORMAT_PCM16_BIGENDIAN 8 */
321 -1, /* HPI_FORMAT_AA_TAGIT1_HITS 9 */
322 -1, /* HPI_FORMAT_AA_TAGIT1_INSERTS 10 */
323 SNDRV_PCM_FORMAT_S32, /* HPI_FORMAT_PCM32_SIGNED 11 */
324 -1, /* HPI_FORMAT_RAW_BITSTREAM 12 */
325 -1, /* HPI_FORMAT_AA_TAGIT1_HITS_EX1 13 */
326 SNDRV_PCM_FORMAT_FLOAT, /* HPI_FORMAT_PCM32_FLOAT 14 */
327#if 1
328 /* ALSA can't handle 3 byte sample size together with power-of-2
329 * constraint on buffer_bytes, so disable this format
330 */
331 -1
332#else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300333 /* SNDRV_PCM_FORMAT_S24_3LE */ /* HPI_FORMAT_PCM24_SIGNED 15 */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200334#endif
335};
336
337
338static int snd_card_asihpi_format_alsa2hpi(snd_pcm_format_t alsa_format,
339 u16 *hpi_format)
340{
341 u16 format;
342
343 for (format = HPI_FORMAT_PCM8_UNSIGNED;
344 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
345 if (hpi_to_alsa_formats[format] == alsa_format) {
346 *hpi_format = format;
347 return 0;
348 }
349 }
350
351 snd_printd(KERN_WARNING "failed match for alsa format %d\n",
352 alsa_format);
353 *hpi_format = 0;
354 return -EINVAL;
355}
356
357static void snd_card_asihpi_pcm_samplerates(struct snd_card_asihpi *asihpi,
358 struct snd_pcm_hardware *pcmhw)
359{
360 u16 err;
361 u32 h_control;
362 u32 sample_rate;
363 int idx;
364 unsigned int rate_min = 200000;
365 unsigned int rate_max = 0;
366 unsigned int rates = 0;
367
368 if (asihpi->support_mrx) {
369 rates |= SNDRV_PCM_RATE_CONTINUOUS;
370 rates |= SNDRV_PCM_RATE_8000_96000;
371 rate_min = 8000;
372 rate_max = 100000;
373 } else {
374 /* on cards without SRC,
375 valid rates are determined by sampleclock */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300376 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200377 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
378 HPI_CONTROL_SAMPLECLOCK, &h_control);
379 if (err) {
380 snd_printk(KERN_ERR
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300381 "No local sampleclock, err %d\n", err);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200382 }
383
384 for (idx = 0; idx < 100; idx++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300385 if (hpi_sample_clock_query_local_rate(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200386 h_control, idx, &sample_rate)) {
387 if (!idx)
388 snd_printk(KERN_ERR
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300389 "Local rate query failed\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200390
391 break;
392 }
393
394 rate_min = min(rate_min, sample_rate);
395 rate_max = max(rate_max, sample_rate);
396
397 switch (sample_rate) {
398 case 5512:
399 rates |= SNDRV_PCM_RATE_5512;
400 break;
401 case 8000:
402 rates |= SNDRV_PCM_RATE_8000;
403 break;
404 case 11025:
405 rates |= SNDRV_PCM_RATE_11025;
406 break;
407 case 16000:
408 rates |= SNDRV_PCM_RATE_16000;
409 break;
410 case 22050:
411 rates |= SNDRV_PCM_RATE_22050;
412 break;
413 case 32000:
414 rates |= SNDRV_PCM_RATE_32000;
415 break;
416 case 44100:
417 rates |= SNDRV_PCM_RATE_44100;
418 break;
419 case 48000:
420 rates |= SNDRV_PCM_RATE_48000;
421 break;
422 case 64000:
423 rates |= SNDRV_PCM_RATE_64000;
424 break;
425 case 88200:
426 rates |= SNDRV_PCM_RATE_88200;
427 break;
428 case 96000:
429 rates |= SNDRV_PCM_RATE_96000;
430 break;
431 case 176400:
432 rates |= SNDRV_PCM_RATE_176400;
433 break;
434 case 192000:
435 rates |= SNDRV_PCM_RATE_192000;
436 break;
437 default: /* some other rate */
438 rates |= SNDRV_PCM_RATE_KNOT;
439 }
440 }
441 }
442
443 /* printk(KERN_INFO "Supported rates %X %d %d\n",
444 rates, rate_min, rate_max); */
445 pcmhw->rates = rates;
446 pcmhw->rate_min = rate_min;
447 pcmhw->rate_max = rate_max;
448}
449
450static int snd_card_asihpi_pcm_hw_params(struct snd_pcm_substream *substream,
451 struct snd_pcm_hw_params *params)
452{
453 struct snd_pcm_runtime *runtime = substream->runtime;
454 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
455 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
456 int err;
457 u16 format;
Kulikov Vasiliy315e8f72010-07-15 22:48:19 +0400458 int width;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200459 unsigned int bytes_per_sec;
460
461 print_hwparams(params);
462 err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
463 if (err < 0)
464 return err;
465 err = snd_card_asihpi_format_alsa2hpi(params_format(params), &format);
466 if (err)
467 return err;
468
469 VPRINTK1(KERN_INFO "format %d, %d chans, %d_hz\n",
470 format, params_channels(params),
471 params_rate(params));
472
473 hpi_handle_error(hpi_format_create(&dpcm->format,
474 params_channels(params),
475 format, params_rate(params), 0, 0));
476
477 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300478 if (hpi_instream_reset(dpcm->h_stream) != 0)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200479 return -EINVAL;
480
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300481 if (hpi_instream_set_format(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200482 dpcm->h_stream, &dpcm->format) != 0)
483 return -EINVAL;
484 }
485
486 dpcm->hpi_buffer_attached = 0;
487 if (card->support_mmap) {
488
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300489 err = hpi_stream_host_buffer_attach(dpcm->h_stream,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200490 params_buffer_bytes(params), runtime->dma_addr);
491 if (err == 0) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300492 VPRINTK1(KERN_INFO
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200493 "stream_host_buffer_attach succeeded %u %lu\n",
494 params_buffer_bytes(params),
495 (unsigned long)runtime->dma_addr);
496 } else {
497 snd_printd(KERN_INFO
498 "stream_host_buffer_attach error %d\n",
499 err);
500 return -ENOMEM;
501 }
502
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300503 err = hpi_stream_get_info_ex(dpcm->h_stream, NULL,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200504 &dpcm->hpi_buffer_attached,
505 NULL, NULL, NULL);
506
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300507 VPRINTK1(KERN_INFO "stream_host_buffer_attach status 0x%x\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200508 dpcm->hpi_buffer_attached);
509 }
510 bytes_per_sec = params_rate(params) * params_channels(params);
Kulikov Vasiliy315e8f72010-07-15 22:48:19 +0400511 width = snd_pcm_format_width(params_format(params));
512 bytes_per_sec *= width;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200513 bytes_per_sec /= 8;
Kulikov Vasiliy315e8f72010-07-15 22:48:19 +0400514 if (width < 0 || bytes_per_sec == 0)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200515 return -EINVAL;
516
517 dpcm->bytes_per_sec = bytes_per_sec;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300518 dpcm->buffer_bytes = params_buffer_bytes(params);
519 dpcm->period_bytes = params_period_bytes(params);
520 VPRINTK1(KERN_INFO "buffer_bytes=%d, period_bytes=%d, bps=%d\n",
521 dpcm->buffer_bytes, dpcm->period_bytes, bytes_per_sec);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200522
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200523 return 0;
524}
525
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300526static int
527snd_card_asihpi_hw_free(struct snd_pcm_substream *substream)
528{
529 struct snd_pcm_runtime *runtime = substream->runtime;
530 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
531 if (dpcm->hpi_buffer_attached)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300532 hpi_stream_host_buffer_detach(dpcm->h_stream);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300533
534 snd_pcm_lib_free_pages(substream);
535 return 0;
536}
537
538static void snd_card_asihpi_runtime_free(struct snd_pcm_runtime *runtime)
539{
540 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
541 kfree(dpcm);
542}
543
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200544static void snd_card_asihpi_pcm_timer_start(struct snd_pcm_substream *
545 substream)
546{
547 struct snd_pcm_runtime *runtime = substream->runtime;
548 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
549 int expiry;
550
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300551 expiry = HZ / 200;
552 /*? (dpcm->period_bytes * HZ / dpcm->bytes_per_sec); */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300553 expiry = max(expiry, 1); /* don't let it be zero! */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200554 dpcm->timer.expires = jiffies + expiry;
555 dpcm->respawn_timer = 1;
556 add_timer(&dpcm->timer);
557}
558
559static void snd_card_asihpi_pcm_timer_stop(struct snd_pcm_substream *substream)
560{
561 struct snd_pcm_runtime *runtime = substream->runtime;
562 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
563
564 dpcm->respawn_timer = 0;
565 del_timer(&dpcm->timer);
566}
567
568static int snd_card_asihpi_trigger(struct snd_pcm_substream *substream,
569 int cmd)
570{
571 struct snd_card_asihpi_pcm *dpcm = substream->runtime->private_data;
572 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
573 struct snd_pcm_substream *s;
574 u16 e;
575
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300576 VPRINTK1(KERN_INFO "%c%d trigger\n",
577 SCHR(substream->stream), substream->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200578 switch (cmd) {
579 case SNDRV_PCM_TRIGGER_START:
580 snd_pcm_group_for_each_entry(s, substream) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300581 struct snd_pcm_runtime *runtime = s->runtime;
582 struct snd_card_asihpi_pcm *ds = runtime->private_data;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200583
584 if (snd_pcm_substream_chip(s) != card)
585 continue;
586
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300587 /* don't link Cap and Play */
588 if (substream->stream != s->stream)
589 continue;
590
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200591 if ((s->stream == SNDRV_PCM_STREAM_PLAYBACK) &&
592 (card->support_mmap)) {
593 /* How do I know how much valid data is present
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300594 * in buffer? Must be at least one period!
595 * Guessing 2 periods, but if
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200596 * buffer is bigger it may contain even more
597 * data??
598 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300599 unsigned int preload = ds->period_bytes * 1;
600 VPRINTK2(KERN_INFO "%d preload x%x\n", s->number, preload);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200601 hpi_handle_error(hpi_outstream_write_buf(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300602 ds->h_stream,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300603 &runtime->dma_area[0],
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200604 preload,
605 &ds->format));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300606 ds->pcm_buf_host_rw_ofs = preload;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200607 }
608
609 if (card->support_grouping) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300610 VPRINTK1(KERN_INFO "\t%c%d group\n",
611 SCHR(s->stream),
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200612 s->number);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300613 e = hpi_stream_group_add(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200614 dpcm->h_stream,
615 ds->h_stream);
616 if (!e) {
617 snd_pcm_trigger_done(s, substream);
618 } else {
619 hpi_handle_error(e);
620 break;
621 }
622 } else
623 break;
624 }
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300625 VPRINTK1(KERN_INFO "start\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200626 /* start the master stream */
627 snd_card_asihpi_pcm_timer_start(substream);
Eliot Blennerhassettc4ed97d2011-02-10 17:26:20 +1300628 if ((substream->stream == SNDRV_PCM_STREAM_CAPTURE) ||
629 !card->support_mmap)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300630 hpi_handle_error(hpi_stream_start(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200631 break;
632
633 case SNDRV_PCM_TRIGGER_STOP:
634 snd_card_asihpi_pcm_timer_stop(substream);
635 snd_pcm_group_for_each_entry(s, substream) {
636 if (snd_pcm_substream_chip(s) != card)
637 continue;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300638 /* don't link Cap and Play */
639 if (substream->stream != s->stream)
640 continue;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200641
642 /*? workaround linked streams don't
643 transition to SETUP 20070706*/
644 s->runtime->status->state = SNDRV_PCM_STATE_SETUP;
645
646 if (card->support_grouping) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300647 VPRINTK1(KERN_INFO "\t%c%d group\n",
648 SCHR(s->stream),
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200649 s->number);
650 snd_pcm_trigger_done(s, substream);
651 } else
652 break;
653 }
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300654 VPRINTK1(KERN_INFO "stop\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200655
656 /* _prepare and _hwparams reset the stream */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300657 hpi_handle_error(hpi_stream_stop(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200658 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
659 hpi_handle_error(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300660 hpi_outstream_reset(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200661
662 if (card->support_grouping)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300663 hpi_handle_error(hpi_stream_group_reset(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200664 break;
665
666 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300667 VPRINTK1(KERN_INFO "pause release\n");
668 hpi_handle_error(hpi_stream_start(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200669 snd_card_asihpi_pcm_timer_start(substream);
670 break;
671 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300672 VPRINTK1(KERN_INFO "pause\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200673 snd_card_asihpi_pcm_timer_stop(substream);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300674 hpi_handle_error(hpi_stream_stop(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200675 break;
676 default:
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300677 snd_printd(KERN_ERR "\tINVALID\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200678 return -EINVAL;
679 }
680
681 return 0;
682}
683
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200684/*algorithm outline
685 Without linking degenerates to getting single stream pos etc
686 Without mmap 2nd loop degenerates to snd_pcm_period_elapsed
687*/
688/*
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300689pcm_buf_dma_ofs=get_buf_pos(s);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200690for_each_linked_stream(s) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300691 pcm_buf_dma_ofs=get_buf_pos(s);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300692 min_buf_pos = modulo_min(min_buf_pos, pcm_buf_dma_ofs, buffer_bytes)
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300693 new_data = min(new_data, calc_new_data(pcm_buf_dma_ofs,irq_pos)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200694}
695timer.expires = jiffies + predict_next_period_ready(min_buf_pos);
696for_each_linked_stream(s) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300697 s->pcm_buf_dma_ofs = min_buf_pos;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300698 if (new_data > period_bytes) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200699 if (mmap) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300700 irq_pos = (irq_pos + period_bytes) % buffer_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200701 if (playback) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300702 write(period_bytes);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200703 } else {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300704 read(period_bytes);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200705 }
706 }
707 snd_pcm_period_elapsed(s);
708 }
709}
710*/
711
712/** Minimum of 2 modulo values. Works correctly when the difference between
713* the values is less than half the modulus
714*/
715static inline unsigned int modulo_min(unsigned int a, unsigned int b,
716 unsigned long int modulus)
717{
718 unsigned int result;
719 if (((a-b) % modulus) < (modulus/2))
720 result = b;
721 else
722 result = a;
723
724 return result;
725}
726
727/** Timer function, equivalent to interrupt service routine for cards
728*/
729static void snd_card_asihpi_timer_function(unsigned long data)
730{
731 struct snd_card_asihpi_pcm *dpcm = (struct snd_card_asihpi_pcm *)data;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300732 struct snd_pcm_substream *substream = dpcm->substream;
733 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200734 struct snd_pcm_runtime *runtime;
735 struct snd_pcm_substream *s;
736 unsigned int newdata = 0;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300737 unsigned int pcm_buf_dma_ofs, min_buf_pos = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200738 unsigned int remdata, xfercount, next_jiffies;
739 int first = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300740 int loops = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200741 u16 state;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300742 u32 buffer_size, bytes_avail, samples_played, on_card_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200743
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300744 VPRINTK1(KERN_INFO "%c%d snd_card_asihpi_timer_function\n",
745 SCHR(substream->stream), substream->number);
746
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200747 /* find minimum newdata and buffer pos in group */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300748 snd_pcm_group_for_each_entry(s, substream) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200749 struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
750 runtime = s->runtime;
751
752 if (snd_pcm_substream_chip(s) != card)
753 continue;
754
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300755 /* don't link Cap and Play */
756 if (substream->stream != s->stream)
757 continue;
758
759 hpi_handle_error(hpi_stream_get_info_ex(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200760 ds->h_stream, &state,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300761 &buffer_size, &bytes_avail,
762 &samples_played, &on_card_bytes));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200763
764 /* number of bytes in on-card buffer */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300765 runtime->delay = on_card_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200766
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300767 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300768 pcm_buf_dma_ofs = ds->pcm_buf_host_rw_ofs - bytes_avail;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300769 if (state == HPI_STATE_STOPPED) {
770 if ((bytes_avail == 0) &&
771 (on_card_bytes < ds->pcm_buf_host_rw_ofs)) {
772 hpi_handle_error(hpi_stream_start(ds->h_stream));
773 VPRINTK1(KERN_INFO "P%d start\n", s->number);
774 }
775 } else if (state == HPI_STATE_DRAINED) {
776 VPRINTK1(KERN_WARNING "P%d drained\n",
777 s->number);
778 /*snd_pcm_stop(s, SNDRV_PCM_STATE_XRUN);
779 continue; */
780 }
781 } else
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300782 pcm_buf_dma_ofs = bytes_avail + ds->pcm_buf_host_rw_ofs;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200783
784 if (first) {
785 /* can't statically init min when wrap is involved */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300786 min_buf_pos = pcm_buf_dma_ofs;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300787 newdata = (pcm_buf_dma_ofs - ds->pcm_buf_elapsed_dma_ofs) % ds->buffer_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200788 first = 0;
789 } else {
790 min_buf_pos =
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300791 modulo_min(min_buf_pos, pcm_buf_dma_ofs, UINT_MAX+1L);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200792 newdata = min(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300793 (pcm_buf_dma_ofs - ds->pcm_buf_elapsed_dma_ofs) % ds->buffer_bytes,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200794 newdata);
795 }
796
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300797 VPRINTK1(KERN_INFO "PB timer hw_ptr x%04lX, appl_ptr x%04lX\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200798 (unsigned long)frames_to_bytes(runtime,
799 runtime->status->hw_ptr),
800 (unsigned long)frames_to_bytes(runtime,
801 runtime->control->appl_ptr));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300802
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300803 VPRINTK1(KERN_INFO "%d %c%d S=%d, rw=%04X, dma=x%04X, left=x%04X,"
804 " aux=x%04X space=x%04X\n",
805 loops, SCHR(s->stream), s->number,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300806 state, ds->pcm_buf_host_rw_ofs, pcm_buf_dma_ofs, (int)bytes_avail,
807 (int)on_card_bytes, buffer_size-bytes_avail);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300808 loops++;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200809 }
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300810 pcm_buf_dma_ofs = min_buf_pos;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200811
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300812 remdata = newdata % dpcm->period_bytes;
813 xfercount = newdata - remdata; /* a multiple of period_bytes */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300814 /* come back when on_card_bytes has decreased enough to allow
815 write to happen, or when data has been consumed to make another
816 period
817 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300818 if (xfercount && (on_card_bytes > dpcm->period_bytes))
819 next_jiffies = ((on_card_bytes - dpcm->period_bytes) * HZ / dpcm->bytes_per_sec);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300820 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300821 next_jiffies = ((dpcm->period_bytes - remdata) * HZ / dpcm->bytes_per_sec);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300822
823 next_jiffies = max(next_jiffies, 1U);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200824 dpcm->timer.expires = jiffies + next_jiffies;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300825 VPRINTK1(KERN_INFO "jif %d buf pos x%04X newdata x%04X xfer x%04X\n",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300826 next_jiffies, pcm_buf_dma_ofs, newdata, xfercount);
827
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300828 snd_pcm_group_for_each_entry(s, substream) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200829 struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200830
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300831 /* don't link Cap and Play */
832 if (substream->stream != s->stream)
833 continue;
834
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300835 ds->pcm_buf_dma_ofs = pcm_buf_dma_ofs;
836
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300837 if (xfercount && (on_card_bytes <= ds->period_bytes)) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200838 if (card->support_mmap) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200839 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300840 VPRINTK2(KERN_INFO "P%d write x%04x\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200841 s->number,
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300842 ds->period_bytes);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200843 hpi_handle_error(
844 hpi_outstream_write_buf(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300845 ds->h_stream,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200846 &s->runtime->
847 dma_area[0],
848 xfercount,
849 &ds->format));
850 } else {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300851 VPRINTK2(KERN_INFO "C%d read x%04x\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200852 s->number,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300853 xfercount);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200854 hpi_handle_error(
855 hpi_instream_read_buf(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300856 ds->h_stream,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200857 NULL, xfercount));
858 }
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300859 ds->pcm_buf_host_rw_ofs = ds->pcm_buf_host_rw_ofs + xfercount;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200860 } /* else R/W will be handled by read/write callbacks */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300861 ds->pcm_buf_elapsed_dma_ofs = pcm_buf_dma_ofs;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200862 snd_pcm_period_elapsed(s);
863 }
864 }
865
866 if (dpcm->respawn_timer)
867 add_timer(&dpcm->timer);
868}
869
870/***************************** PLAYBACK OPS ****************/
871static int snd_card_asihpi_playback_ioctl(struct snd_pcm_substream *substream,
872 unsigned int cmd, void *arg)
873{
874 /* snd_printd(KERN_INFO "Playback ioctl %d\n", cmd); */
875 return snd_pcm_lib_ioctl(substream, cmd, arg);
876}
877
878static int snd_card_asihpi_playback_prepare(struct snd_pcm_substream *
879 substream)
880{
881 struct snd_pcm_runtime *runtime = substream->runtime;
882 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
883
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300884 VPRINTK1(KERN_INFO "playback prepare %d\n", substream->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200885
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300886 hpi_handle_error(hpi_outstream_reset(dpcm->h_stream));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300887 dpcm->pcm_buf_host_rw_ofs = 0;
888 dpcm->pcm_buf_dma_ofs = 0;
889 dpcm->pcm_buf_elapsed_dma_ofs = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200890 return 0;
891}
892
893static snd_pcm_uframes_t
894snd_card_asihpi_playback_pointer(struct snd_pcm_substream *substream)
895{
896 struct snd_pcm_runtime *runtime = substream->runtime;
897 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
898 snd_pcm_uframes_t ptr;
899
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300900 ptr = bytes_to_frames(runtime, dpcm->pcm_buf_dma_ofs % dpcm->buffer_bytes);
901 /* VPRINTK2(KERN_INFO "playback_pointer=x%04lx\n", (unsigned long)ptr); */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200902 return ptr;
903}
904
905static void snd_card_asihpi_playback_format(struct snd_card_asihpi *asihpi,
906 u32 h_stream,
907 struct snd_pcm_hardware *pcmhw)
908{
909 struct hpi_format hpi_format;
910 u16 format;
911 u16 err;
912 u32 h_control;
913 u32 sample_rate = 48000;
914
915 /* on cards without SRC, must query at valid rate,
916 * maybe set by external sync
917 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300918 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200919 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
920 HPI_CONTROL_SAMPLECLOCK, &h_control);
921
922 if (!err)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300923 err = hpi_sample_clock_get_sample_rate(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200924 &sample_rate);
925
926 for (format = HPI_FORMAT_PCM8_UNSIGNED;
927 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
928 err = hpi_format_create(&hpi_format,
929 2, format, sample_rate, 128000, 0);
930 if (!err)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300931 err = hpi_outstream_query_format(h_stream,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200932 &hpi_format);
933 if (!err && (hpi_to_alsa_formats[format] != -1))
934 pcmhw->formats |=
935 (1ULL << hpi_to_alsa_formats[format]);
936 }
937}
938
939static struct snd_pcm_hardware snd_card_asihpi_playback = {
940 .channels_min = 1,
941 .channels_max = 2,
942 .buffer_bytes_max = BUFFER_BYTES_MAX,
943 .period_bytes_min = PERIOD_BYTES_MIN,
944 .period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN,
945 .periods_min = PERIODS_MIN,
946 .periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN,
947 .fifo_size = 0,
948};
949
950static int snd_card_asihpi_playback_open(struct snd_pcm_substream *substream)
951{
952 struct snd_pcm_runtime *runtime = substream->runtime;
953 struct snd_card_asihpi_pcm *dpcm;
954 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
955 int err;
956
957 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
958 if (dpcm == NULL)
959 return -ENOMEM;
960
961 err =
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300962 hpi_outstream_open(card->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200963 substream->number, &dpcm->h_stream);
964 hpi_handle_error(err);
965 if (err)
966 kfree(dpcm);
967 if (err == HPI_ERROR_OBJ_ALREADY_OPEN)
968 return -EBUSY;
969 if (err)
970 return -EIO;
971
972 /*? also check ASI5000 samplerate source
973 If external, only support external rate.
974 If internal and other stream playing, cant switch
975 */
976
977 init_timer(&dpcm->timer);
978 dpcm->timer.data = (unsigned long) dpcm;
979 dpcm->timer.function = snd_card_asihpi_timer_function;
980 dpcm->substream = substream;
981 runtime->private_data = dpcm;
982 runtime->private_free = snd_card_asihpi_runtime_free;
983
984 snd_card_asihpi_playback.channels_max = card->out_max_chans;
985 /*?snd_card_asihpi_playback.period_bytes_min =
986 card->out_max_chans * 4096; */
987
988 snd_card_asihpi_playback_format(card, dpcm->h_stream,
989 &snd_card_asihpi_playback);
990
991 snd_card_asihpi_pcm_samplerates(card, &snd_card_asihpi_playback);
992
993 snd_card_asihpi_playback.info = SNDRV_PCM_INFO_INTERLEAVED |
994 SNDRV_PCM_INFO_DOUBLE |
995 SNDRV_PCM_INFO_BATCH |
996 SNDRV_PCM_INFO_BLOCK_TRANSFER |
997 SNDRV_PCM_INFO_PAUSE;
998
999 if (card->support_mmap)
1000 snd_card_asihpi_playback.info |= SNDRV_PCM_INFO_MMAP |
1001 SNDRV_PCM_INFO_MMAP_VALID;
1002
1003 if (card->support_grouping)
1004 snd_card_asihpi_playback.info |= SNDRV_PCM_INFO_SYNC_START;
1005
1006 /* struct is copied, so can create initializer dynamically */
1007 runtime->hw = snd_card_asihpi_playback;
1008
1009 if (card->support_mmap)
1010 err = snd_pcm_hw_constraint_pow2(runtime, 0,
1011 SNDRV_PCM_HW_PARAM_BUFFER_BYTES);
1012 if (err < 0)
1013 return err;
1014
1015 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1016 card->update_interval_frames);
1017 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001018 card->update_interval_frames * 2, UINT_MAX);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001019
1020 snd_pcm_set_sync(substream);
1021
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001022 VPRINTK1(KERN_INFO "playback open\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001023
1024 return 0;
1025}
1026
1027static int snd_card_asihpi_playback_close(struct snd_pcm_substream *substream)
1028{
1029 struct snd_pcm_runtime *runtime = substream->runtime;
1030 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1031
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001032 hpi_handle_error(hpi_outstream_close(dpcm->h_stream));
1033 VPRINTK1(KERN_INFO "playback close\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001034
1035 return 0;
1036}
1037
1038static int snd_card_asihpi_playback_copy(struct snd_pcm_substream *substream,
1039 int channel,
1040 snd_pcm_uframes_t pos,
1041 void __user *src,
1042 snd_pcm_uframes_t count)
1043{
1044 struct snd_pcm_runtime *runtime = substream->runtime;
1045 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1046 unsigned int len;
1047
1048 len = frames_to_bytes(runtime, count);
1049
1050 if (copy_from_user(runtime->dma_area, src, len))
1051 return -EFAULT;
1052
1053 VPRINTK2(KERN_DEBUG "playback copy%d %u bytes\n",
1054 substream->number, len);
1055
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001056 hpi_handle_error(hpi_outstream_write_buf(dpcm->h_stream,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001057 runtime->dma_area, len, &dpcm->format));
1058
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001059 dpcm->pcm_buf_host_rw_ofs = dpcm->pcm_buf_host_rw_ofs + len;
1060
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001061 return 0;
1062}
1063
1064static int snd_card_asihpi_playback_silence(struct snd_pcm_substream *
1065 substream, int channel,
1066 snd_pcm_uframes_t pos,
1067 snd_pcm_uframes_t count)
1068{
1069 unsigned int len;
1070 struct snd_pcm_runtime *runtime = substream->runtime;
1071 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1072
1073 len = frames_to_bytes(runtime, count);
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001074 VPRINTK1(KERN_INFO "playback silence %u bytes\n", len);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001075
1076 memset(runtime->dma_area, 0, len);
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001077 hpi_handle_error(hpi_outstream_write_buf(dpcm->h_stream,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001078 runtime->dma_area, len, &dpcm->format));
1079 return 0;
1080}
1081
1082static struct snd_pcm_ops snd_card_asihpi_playback_ops = {
1083 .open = snd_card_asihpi_playback_open,
1084 .close = snd_card_asihpi_playback_close,
1085 .ioctl = snd_card_asihpi_playback_ioctl,
1086 .hw_params = snd_card_asihpi_pcm_hw_params,
1087 .hw_free = snd_card_asihpi_hw_free,
1088 .prepare = snd_card_asihpi_playback_prepare,
1089 .trigger = snd_card_asihpi_trigger,
1090 .pointer = snd_card_asihpi_playback_pointer,
1091 .copy = snd_card_asihpi_playback_copy,
1092 .silence = snd_card_asihpi_playback_silence,
1093};
1094
1095static struct snd_pcm_ops snd_card_asihpi_playback_mmap_ops = {
1096 .open = snd_card_asihpi_playback_open,
1097 .close = snd_card_asihpi_playback_close,
1098 .ioctl = snd_card_asihpi_playback_ioctl,
1099 .hw_params = snd_card_asihpi_pcm_hw_params,
1100 .hw_free = snd_card_asihpi_hw_free,
1101 .prepare = snd_card_asihpi_playback_prepare,
1102 .trigger = snd_card_asihpi_trigger,
1103 .pointer = snd_card_asihpi_playback_pointer,
1104};
1105
1106/***************************** CAPTURE OPS ****************/
1107static snd_pcm_uframes_t
1108snd_card_asihpi_capture_pointer(struct snd_pcm_substream *substream)
1109{
1110 struct snd_pcm_runtime *runtime = substream->runtime;
1111 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1112
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001113 VPRINTK2(KERN_INFO "capture pointer %d=%d\n",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001114 substream->number, dpcm->pcm_buf_dma_ofs);
1115 /* NOTE Unlike playback can't use actual samples_played
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001116 for the capture position, because those samples aren't yet in
1117 the local buffer available for reading.
1118 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001119 return bytes_to_frames(runtime, dpcm->pcm_buf_dma_ofs % dpcm->buffer_bytes);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001120}
1121
1122static int snd_card_asihpi_capture_ioctl(struct snd_pcm_substream *substream,
1123 unsigned int cmd, void *arg)
1124{
1125 return snd_pcm_lib_ioctl(substream, cmd, arg);
1126}
1127
1128static int snd_card_asihpi_capture_prepare(struct snd_pcm_substream *substream)
1129{
1130 struct snd_pcm_runtime *runtime = substream->runtime;
1131 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1132
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001133 hpi_handle_error(hpi_instream_reset(dpcm->h_stream));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001134 dpcm->pcm_buf_host_rw_ofs = 0;
1135 dpcm->pcm_buf_dma_ofs = 0;
1136 dpcm->pcm_buf_elapsed_dma_ofs = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001137
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001138 VPRINTK1("Capture Prepare %d\n", substream->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001139 return 0;
1140}
1141
1142
1143
1144static void snd_card_asihpi_capture_format(struct snd_card_asihpi *asihpi,
1145 u32 h_stream,
1146 struct snd_pcm_hardware *pcmhw)
1147{
1148 struct hpi_format hpi_format;
1149 u16 format;
1150 u16 err;
1151 u32 h_control;
1152 u32 sample_rate = 48000;
1153
1154 /* on cards without SRC, must query at valid rate,
1155 maybe set by external sync */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001156 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001157 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
1158 HPI_CONTROL_SAMPLECLOCK, &h_control);
1159
1160 if (!err)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001161 err = hpi_sample_clock_get_sample_rate(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001162 &sample_rate);
1163
1164 for (format = HPI_FORMAT_PCM8_UNSIGNED;
1165 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
1166
1167 err = hpi_format_create(&hpi_format, 2, format,
1168 sample_rate, 128000, 0);
1169 if (!err)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001170 err = hpi_instream_query_format(h_stream,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001171 &hpi_format);
1172 if (!err)
1173 pcmhw->formats |=
1174 (1ULL << hpi_to_alsa_formats[format]);
1175 }
1176}
1177
1178
1179static struct snd_pcm_hardware snd_card_asihpi_capture = {
1180 .channels_min = 1,
1181 .channels_max = 2,
1182 .buffer_bytes_max = BUFFER_BYTES_MAX,
1183 .period_bytes_min = PERIOD_BYTES_MIN,
1184 .period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN,
1185 .periods_min = PERIODS_MIN,
1186 .periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN,
1187 .fifo_size = 0,
1188};
1189
1190static int snd_card_asihpi_capture_open(struct snd_pcm_substream *substream)
1191{
1192 struct snd_pcm_runtime *runtime = substream->runtime;
1193 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
1194 struct snd_card_asihpi_pcm *dpcm;
1195 int err;
1196
1197 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
1198 if (dpcm == NULL)
1199 return -ENOMEM;
1200
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001201 VPRINTK1("hpi_instream_open adapter %d stream %d\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001202 card->adapter_index, substream->number);
1203
1204 err = hpi_handle_error(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001205 hpi_instream_open(card->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001206 substream->number, &dpcm->h_stream));
1207 if (err)
1208 kfree(dpcm);
1209 if (err == HPI_ERROR_OBJ_ALREADY_OPEN)
1210 return -EBUSY;
1211 if (err)
1212 return -EIO;
1213
1214
1215 init_timer(&dpcm->timer);
1216 dpcm->timer.data = (unsigned long) dpcm;
1217 dpcm->timer.function = snd_card_asihpi_timer_function;
1218 dpcm->substream = substream;
1219 runtime->private_data = dpcm;
1220 runtime->private_free = snd_card_asihpi_runtime_free;
1221
1222 snd_card_asihpi_capture.channels_max = card->in_max_chans;
1223 snd_card_asihpi_capture_format(card, dpcm->h_stream,
1224 &snd_card_asihpi_capture);
1225 snd_card_asihpi_pcm_samplerates(card, &snd_card_asihpi_capture);
1226 snd_card_asihpi_capture.info = SNDRV_PCM_INFO_INTERLEAVED;
1227
1228 if (card->support_mmap)
1229 snd_card_asihpi_capture.info |= SNDRV_PCM_INFO_MMAP |
1230 SNDRV_PCM_INFO_MMAP_VALID;
1231
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001232 if (card->support_grouping)
1233 snd_card_asihpi_capture.info |= SNDRV_PCM_INFO_SYNC_START;
1234
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001235 runtime->hw = snd_card_asihpi_capture;
1236
1237 if (card->support_mmap)
1238 err = snd_pcm_hw_constraint_pow2(runtime, 0,
1239 SNDRV_PCM_HW_PARAM_BUFFER_BYTES);
1240 if (err < 0)
1241 return err;
1242
1243 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1244 card->update_interval_frames);
1245 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1246 card->update_interval_frames * 2, UINT_MAX);
1247
1248 snd_pcm_set_sync(substream);
1249
1250 return 0;
1251}
1252
1253static int snd_card_asihpi_capture_close(struct snd_pcm_substream *substream)
1254{
1255 struct snd_card_asihpi_pcm *dpcm = substream->runtime->private_data;
1256
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001257 hpi_handle_error(hpi_instream_close(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001258 return 0;
1259}
1260
1261static int snd_card_asihpi_capture_copy(struct snd_pcm_substream *substream,
1262 int channel, snd_pcm_uframes_t pos,
1263 void __user *dst, snd_pcm_uframes_t count)
1264{
1265 struct snd_pcm_runtime *runtime = substream->runtime;
1266 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001267 u32 len;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001268
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001269 len = frames_to_bytes(runtime, count);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001270
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001271 VPRINTK2(KERN_INFO "capture copy%d %d bytes\n", substream->number, len);
1272 hpi_handle_error(hpi_instream_read_buf(dpcm->h_stream,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001273 runtime->dma_area, len));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001274
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001275 dpcm->pcm_buf_host_rw_ofs = dpcm->pcm_buf_host_rw_ofs + len;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001276
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001277 if (copy_to_user(dst, runtime->dma_area, len))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001278 return -EFAULT;
1279
1280 return 0;
1281}
1282
1283static struct snd_pcm_ops snd_card_asihpi_capture_mmap_ops = {
1284 .open = snd_card_asihpi_capture_open,
1285 .close = snd_card_asihpi_capture_close,
1286 .ioctl = snd_card_asihpi_capture_ioctl,
1287 .hw_params = snd_card_asihpi_pcm_hw_params,
1288 .hw_free = snd_card_asihpi_hw_free,
1289 .prepare = snd_card_asihpi_capture_prepare,
1290 .trigger = snd_card_asihpi_trigger,
1291 .pointer = snd_card_asihpi_capture_pointer,
1292};
1293
1294static struct snd_pcm_ops snd_card_asihpi_capture_ops = {
1295 .open = snd_card_asihpi_capture_open,
1296 .close = snd_card_asihpi_capture_close,
1297 .ioctl = snd_card_asihpi_capture_ioctl,
1298 .hw_params = snd_card_asihpi_pcm_hw_params,
1299 .hw_free = snd_card_asihpi_hw_free,
1300 .prepare = snd_card_asihpi_capture_prepare,
1301 .trigger = snd_card_asihpi_trigger,
1302 .pointer = snd_card_asihpi_capture_pointer,
1303 .copy = snd_card_asihpi_capture_copy
1304};
1305
1306static int __devinit snd_card_asihpi_pcm_new(struct snd_card_asihpi *asihpi,
1307 int device, int substreams)
1308{
1309 struct snd_pcm *pcm;
1310 int err;
1311
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001312 err = snd_pcm_new(asihpi->card, "Asihpi PCM", device,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001313 asihpi->num_outstreams, asihpi->num_instreams,
1314 &pcm);
1315 if (err < 0)
1316 return err;
1317 /* pointer to ops struct is stored, dont change ops afterwards! */
1318 if (asihpi->support_mmap) {
1319 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1320 &snd_card_asihpi_playback_mmap_ops);
1321 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1322 &snd_card_asihpi_capture_mmap_ops);
1323 } else {
1324 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1325 &snd_card_asihpi_playback_ops);
1326 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1327 &snd_card_asihpi_capture_ops);
1328 }
1329
1330 pcm->private_data = asihpi;
1331 pcm->info_flags = 0;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001332 strcpy(pcm->name, "Asihpi PCM");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001333
1334 /*? do we want to emulate MMAP for non-BBM cards?
1335 Jack doesn't work with ALSAs MMAP emulation - WHY NOT? */
1336 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1337 snd_dma_pci_data(asihpi->pci),
1338 64*1024, BUFFER_BYTES_MAX);
1339
1340 return 0;
1341}
1342
1343/***************************** MIXER CONTROLS ****************/
1344struct hpi_control {
1345 u32 h_control;
1346 u16 control_type;
1347 u16 src_node_type;
1348 u16 src_node_index;
1349 u16 dst_node_type;
1350 u16 dst_node_index;
1351 u16 band;
1352 char name[44]; /* copied to snd_ctl_elem_id.name[44]; */
1353};
1354
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001355static const char * const asihpi_tuner_band_names[] = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001356 "invalid",
1357 "AM",
1358 "FM mono",
1359 "TV NTSC-M",
1360 "FM stereo",
1361 "AUX",
1362 "TV PAL BG",
1363 "TV PAL I",
1364 "TV PAL DK",
1365 "TV SECAM",
1366};
1367
1368compile_time_assert(
1369 (ARRAY_SIZE(asihpi_tuner_band_names) ==
1370 (HPI_TUNER_BAND_LAST+1)),
1371 assert_tuner_band_names_size);
1372
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001373static const char * const asihpi_src_names[] = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001374 "no source",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001375 "PCM",
1376 "Line",
1377 "Digital",
1378 "Tuner",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001379 "RF",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001380 "Clock",
1381 "Bitstream",
1382 "Microphone",
1383 "Cobranet",
1384 "Analog",
1385 "Adapter",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001386};
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001387
1388compile_time_assert(
1389 (ARRAY_SIZE(asihpi_src_names) ==
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12001390 (HPI_SOURCENODE_LAST_INDEX-HPI_SOURCENODE_NONE+1)),
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001391 assert_src_names_size);
1392
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001393static const char * const asihpi_dst_names[] = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001394 "no destination",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001395 "PCM",
1396 "Line",
1397 "Digital",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001398 "RF",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001399 "Speaker",
1400 "Cobranet Out",
1401 "Analog"
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001402};
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001403
1404compile_time_assert(
1405 (ARRAY_SIZE(asihpi_dst_names) ==
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12001406 (HPI_DESTNODE_LAST_INDEX-HPI_DESTNODE_NONE+1)),
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001407 assert_dst_names_size);
1408
1409static inline int ctl_add(struct snd_card *card, struct snd_kcontrol_new *ctl,
1410 struct snd_card_asihpi *asihpi)
1411{
1412 int err;
1413
1414 err = snd_ctl_add(card, snd_ctl_new1(ctl, asihpi));
1415 if (err < 0)
1416 return err;
1417 else if (mixer_dump)
1418 snd_printk(KERN_INFO "added %s(%d)\n", ctl->name, ctl->index);
1419
1420 return 0;
1421}
1422
1423/* Convert HPI control name and location into ALSA control name */
1424static void asihpi_ctl_init(struct snd_kcontrol_new *snd_control,
1425 struct hpi_control *hpi_ctl,
1426 char *name)
1427{
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001428 char *dir = "";
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001429 memset(snd_control, 0, sizeof(*snd_control));
1430 snd_control->name = hpi_ctl->name;
1431 snd_control->private_value = hpi_ctl->h_control;
1432 snd_control->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1433 snd_control->index = 0;
1434
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001435 if (hpi_ctl->dst_node_type + HPI_DESTNODE_NONE == HPI_DESTNODE_ISTREAM)
1436 dir = "Capture "; /* On or towards a PCM capture destination*/
1437 else if ((hpi_ctl->src_node_type + HPI_SOURCENODE_NONE != HPI_SOURCENODE_OSTREAM) &&
1438 (!hpi_ctl->dst_node_type))
1439 dir = "Capture "; /* On a source node that is not PCM playback */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001440 else if (hpi_ctl->src_node_type &&
1441 (hpi_ctl->src_node_type + HPI_SOURCENODE_NONE != HPI_SOURCENODE_OSTREAM) &&
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001442 (hpi_ctl->dst_node_type))
1443 dir = "Monitor Playback "; /* Between an input and an output */
1444 else
1445 dir = "Playback "; /* PCM Playback source, or output node */
1446
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001447 if (hpi_ctl->src_node_type && hpi_ctl->dst_node_type)
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001448 sprintf(hpi_ctl->name, "%s%d %s%d %s%s",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001449 asihpi_src_names[hpi_ctl->src_node_type],
1450 hpi_ctl->src_node_index,
1451 asihpi_dst_names[hpi_ctl->dst_node_type],
1452 hpi_ctl->dst_node_index,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001453 dir, name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001454 else if (hpi_ctl->dst_node_type) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001455 sprintf(hpi_ctl->name, "%s %d %s%s",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001456 asihpi_dst_names[hpi_ctl->dst_node_type],
1457 hpi_ctl->dst_node_index,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001458 dir, name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001459 } else {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001460 sprintf(hpi_ctl->name, "%s %d %s%s",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001461 asihpi_src_names[hpi_ctl->src_node_type],
1462 hpi_ctl->src_node_index,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001463 dir, name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001464 }
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001465 /* printk(KERN_INFO "Adding %s %d to %d ", hpi_ctl->name,
1466 hpi_ctl->wSrcNodeType, hpi_ctl->wDstNodeType); */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001467}
1468
1469/*------------------------------------------------------------
1470 Volume controls
1471 ------------------------------------------------------------*/
1472#define VOL_STEP_mB 1
1473static int snd_asihpi_volume_info(struct snd_kcontrol *kcontrol,
1474 struct snd_ctl_elem_info *uinfo)
1475{
1476 u32 h_control = kcontrol->private_value;
1477 u16 err;
1478 /* native gains are in millibels */
1479 short min_gain_mB;
1480 short max_gain_mB;
1481 short step_gain_mB;
1482
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001483 err = hpi_volume_query_range(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001484 &min_gain_mB, &max_gain_mB, &step_gain_mB);
1485 if (err) {
1486 max_gain_mB = 0;
1487 min_gain_mB = -10000;
1488 step_gain_mB = VOL_STEP_mB;
1489 }
1490
1491 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1492 uinfo->count = 2;
1493 uinfo->value.integer.min = min_gain_mB / VOL_STEP_mB;
1494 uinfo->value.integer.max = max_gain_mB / VOL_STEP_mB;
1495 uinfo->value.integer.step = step_gain_mB / VOL_STEP_mB;
1496 return 0;
1497}
1498
1499static int snd_asihpi_volume_get(struct snd_kcontrol *kcontrol,
1500 struct snd_ctl_elem_value *ucontrol)
1501{
1502 u32 h_control = kcontrol->private_value;
1503 short an_gain_mB[HPI_MAX_CHANNELS];
1504
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001505 hpi_handle_error(hpi_volume_get_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001506 ucontrol->value.integer.value[0] = an_gain_mB[0] / VOL_STEP_mB;
1507 ucontrol->value.integer.value[1] = an_gain_mB[1] / VOL_STEP_mB;
1508
1509 return 0;
1510}
1511
1512static int snd_asihpi_volume_put(struct snd_kcontrol *kcontrol,
1513 struct snd_ctl_elem_value *ucontrol)
1514{
1515 int change;
1516 u32 h_control = kcontrol->private_value;
1517 short an_gain_mB[HPI_MAX_CHANNELS];
1518
1519 an_gain_mB[0] =
1520 (ucontrol->value.integer.value[0]) * VOL_STEP_mB;
1521 an_gain_mB[1] =
1522 (ucontrol->value.integer.value[1]) * VOL_STEP_mB;
1523 /* change = asihpi->mixer_volume[addr][0] != left ||
1524 asihpi->mixer_volume[addr][1] != right;
1525 */
1526 change = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001527 hpi_handle_error(hpi_volume_set_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001528 return change;
1529}
1530
1531static const DECLARE_TLV_DB_SCALE(db_scale_100, -10000, VOL_STEP_mB, 0);
1532
1533static int __devinit snd_asihpi_volume_add(struct snd_card_asihpi *asihpi,
1534 struct hpi_control *hpi_ctl)
1535{
1536 struct snd_card *card = asihpi->card;
1537 struct snd_kcontrol_new snd_control;
1538
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001539 asihpi_ctl_init(&snd_control, hpi_ctl, "Volume");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001540 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1541 SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1542 snd_control.info = snd_asihpi_volume_info;
1543 snd_control.get = snd_asihpi_volume_get;
1544 snd_control.put = snd_asihpi_volume_put;
1545 snd_control.tlv.p = db_scale_100;
1546
1547 return ctl_add(card, &snd_control, asihpi);
1548}
1549
1550/*------------------------------------------------------------
1551 Level controls
1552 ------------------------------------------------------------*/
1553static int snd_asihpi_level_info(struct snd_kcontrol *kcontrol,
1554 struct snd_ctl_elem_info *uinfo)
1555{
1556 u32 h_control = kcontrol->private_value;
1557 u16 err;
1558 short min_gain_mB;
1559 short max_gain_mB;
1560 short step_gain_mB;
1561
1562 err =
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001563 hpi_level_query_range(h_control, &min_gain_mB,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001564 &max_gain_mB, &step_gain_mB);
1565 if (err) {
1566 max_gain_mB = 2400;
1567 min_gain_mB = -1000;
1568 step_gain_mB = 100;
1569 }
1570
1571 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1572 uinfo->count = 2;
1573 uinfo->value.integer.min = min_gain_mB / HPI_UNITS_PER_dB;
1574 uinfo->value.integer.max = max_gain_mB / HPI_UNITS_PER_dB;
1575 uinfo->value.integer.step = step_gain_mB / HPI_UNITS_PER_dB;
1576 return 0;
1577}
1578
1579static int snd_asihpi_level_get(struct snd_kcontrol *kcontrol,
1580 struct snd_ctl_elem_value *ucontrol)
1581{
1582 u32 h_control = kcontrol->private_value;
1583 short an_gain_mB[HPI_MAX_CHANNELS];
1584
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001585 hpi_handle_error(hpi_level_get_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001586 ucontrol->value.integer.value[0] =
1587 an_gain_mB[0] / HPI_UNITS_PER_dB;
1588 ucontrol->value.integer.value[1] =
1589 an_gain_mB[1] / HPI_UNITS_PER_dB;
1590
1591 return 0;
1592}
1593
1594static int snd_asihpi_level_put(struct snd_kcontrol *kcontrol,
1595 struct snd_ctl_elem_value *ucontrol)
1596{
1597 int change;
1598 u32 h_control = kcontrol->private_value;
1599 short an_gain_mB[HPI_MAX_CHANNELS];
1600
1601 an_gain_mB[0] =
1602 (ucontrol->value.integer.value[0]) * HPI_UNITS_PER_dB;
1603 an_gain_mB[1] =
1604 (ucontrol->value.integer.value[1]) * HPI_UNITS_PER_dB;
1605 /* change = asihpi->mixer_level[addr][0] != left ||
1606 asihpi->mixer_level[addr][1] != right;
1607 */
1608 change = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001609 hpi_handle_error(hpi_level_set_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001610 return change;
1611}
1612
1613static const DECLARE_TLV_DB_SCALE(db_scale_level, -1000, 100, 0);
1614
1615static int __devinit snd_asihpi_level_add(struct snd_card_asihpi *asihpi,
1616 struct hpi_control *hpi_ctl)
1617{
1618 struct snd_card *card = asihpi->card;
1619 struct snd_kcontrol_new snd_control;
1620
1621 /* can't use 'volume' cos some nodes have volume as well */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001622 asihpi_ctl_init(&snd_control, hpi_ctl, "Level");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001623 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1624 SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1625 snd_control.info = snd_asihpi_level_info;
1626 snd_control.get = snd_asihpi_level_get;
1627 snd_control.put = snd_asihpi_level_put;
1628 snd_control.tlv.p = db_scale_level;
1629
1630 return ctl_add(card, &snd_control, asihpi);
1631}
1632
1633/*------------------------------------------------------------
1634 AESEBU controls
1635 ------------------------------------------------------------*/
1636
1637/* AESEBU format */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001638static const char * const asihpi_aesebu_format_names[] = {
1639 "N/A", "S/PDIF", "AES/EBU" };
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001640
1641static int snd_asihpi_aesebu_format_info(struct snd_kcontrol *kcontrol,
1642 struct snd_ctl_elem_info *uinfo)
1643{
1644 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1645 uinfo->count = 1;
1646 uinfo->value.enumerated.items = 3;
1647
1648 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1649 uinfo->value.enumerated.item =
1650 uinfo->value.enumerated.items - 1;
1651
1652 strcpy(uinfo->value.enumerated.name,
1653 asihpi_aesebu_format_names[uinfo->value.enumerated.item]);
1654
1655 return 0;
1656}
1657
1658static int snd_asihpi_aesebu_format_get(struct snd_kcontrol *kcontrol,
1659 struct snd_ctl_elem_value *ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001660 u16 (*func)(u32, u16 *))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001661{
1662 u32 h_control = kcontrol->private_value;
1663 u16 source, err;
1664
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001665 err = func(h_control, &source);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001666
1667 /* default to N/A */
1668 ucontrol->value.enumerated.item[0] = 0;
1669 /* return success but set the control to N/A */
1670 if (err)
1671 return 0;
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;
1676
1677 return 0;
1678}
1679
1680static int snd_asihpi_aesebu_format_put(struct snd_kcontrol *kcontrol,
1681 struct snd_ctl_elem_value *ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001682 u16 (*func)(u32, u16))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001683{
1684 u32 h_control = kcontrol->private_value;
1685
1686 /* default to S/PDIF */
1687 u16 source = HPI_AESEBU_FORMAT_SPDIF;
1688
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;
1693
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001694 if (func(h_control, source) != 0)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001695 return -EINVAL;
1696
1697 return 1;
1698}
1699
1700static 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,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001703 hpi_aesebu_receiver_get_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001704}
1705
1706static 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,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001709 hpi_aesebu_receiver_set_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001710}
1711
1712static int snd_asihpi_aesebu_rxstatus_info(struct snd_kcontrol *kcontrol,
1713 struct snd_ctl_elem_info *uinfo)
1714{
1715 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1716 uinfo->count = 1;
1717
1718 uinfo->value.integer.min = 0;
1719 uinfo->value.integer.max = 0X1F;
1720 uinfo->value.integer.step = 1;
1721
1722 return 0;
1723}
1724
1725static int snd_asihpi_aesebu_rxstatus_get(struct snd_kcontrol *kcontrol,
1726 struct snd_ctl_elem_value *ucontrol) {
1727
1728 u32 h_control = kcontrol->private_value;
1729 u16 status;
1730
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001731 hpi_handle_error(hpi_aesebu_receiver_get_error_status(
1732 h_control, &status));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001733 ucontrol->value.integer.value[0] = status;
1734 return 0;
1735}
1736
1737static int __devinit snd_asihpi_aesebu_rx_add(struct snd_card_asihpi *asihpi,
1738 struct hpi_control *hpi_ctl)
1739{
1740 struct snd_card *card = asihpi->card;
1741 struct snd_kcontrol_new snd_control;
1742
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001743 asihpi_ctl_init(&snd_control, hpi_ctl, "Format");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001744 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;
1748
1749
1750 if (ctl_add(card, &snd_control, asihpi) < 0)
1751 return -EINVAL;
1752
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001753 asihpi_ctl_init(&snd_control, hpi_ctl, "Status");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001754 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;
1758
1759 return ctl_add(card, &snd_control, asihpi);
1760}
1761
1762static 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,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001765 hpi_aesebu_transmitter_get_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001766}
1767
1768static 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,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001771 hpi_aesebu_transmitter_set_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001772}
1773
1774
1775static int __devinit snd_asihpi_aesebu_tx_add(struct snd_card_asihpi *asihpi,
1776 struct hpi_control *hpi_ctl)
1777{
1778 struct snd_card *card = asihpi->card;
1779 struct snd_kcontrol_new snd_control;
1780
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001781 asihpi_ctl_init(&snd_control, hpi_ctl, "Format");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001782 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;
1786
1787 return ctl_add(card, &snd_control, asihpi);
1788}
1789
1790/*------------------------------------------------------------
1791 Tuner controls
1792 ------------------------------------------------------------*/
1793
1794/* Gain */
1795
1796static int snd_asihpi_tuner_gain_info(struct snd_kcontrol *kcontrol,
1797 struct snd_ctl_elem_info *uinfo)
1798{
1799 u32 h_control = kcontrol->private_value;
1800 u16 err;
1801 short idx;
1802 u16 gain_range[3];
1803
1804 for (idx = 0; idx < 3; idx++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001805 err = hpi_tuner_query_gain(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001806 idx, &gain_range[idx]);
1807 if (err != 0)
1808 return err;
1809 }
1810
1811 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1812 uinfo->count = 1;
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;
1816 return 0;
1817}
1818
1819static int snd_asihpi_tuner_gain_get(struct snd_kcontrol *kcontrol,
1820 struct snd_ctl_elem_value *ucontrol)
1821{
1822 /*
1823 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1824 */
1825 u32 h_control = kcontrol->private_value;
1826 short gain;
1827
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001828 hpi_handle_error(hpi_tuner_get_gain(h_control, &gain));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001829 ucontrol->value.integer.value[0] = gain / HPI_UNITS_PER_dB;
1830
1831 return 0;
1832}
1833
1834static int snd_asihpi_tuner_gain_put(struct snd_kcontrol *kcontrol,
1835 struct snd_ctl_elem_value *ucontrol)
1836{
1837 /*
1838 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1839 */
1840 u32 h_control = kcontrol->private_value;
1841 short gain;
1842
1843 gain = (ucontrol->value.integer.value[0]) * HPI_UNITS_PER_dB;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001844 hpi_handle_error(hpi_tuner_set_gain(h_control, gain));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001845
1846 return 1;
1847}
1848
1849/* Band */
1850
1851static int asihpi_tuner_band_query(struct snd_kcontrol *kcontrol,
1852 u16 *band_list, u32 len) {
1853 u32 h_control = kcontrol->private_value;
1854 u16 err = 0;
1855 u32 i;
1856
1857 for (i = 0; i < len; i++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001858 err = hpi_tuner_query_band(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001859 h_control, i, &band_list[i]);
1860 if (err != 0)
1861 break;
1862 }
1863
1864 if (err && (err != HPI_ERROR_INVALID_OBJ_INDEX))
1865 return -EIO;
1866
1867 return i;
1868}
1869
1870static int snd_asihpi_tuner_band_info(struct snd_kcontrol *kcontrol,
1871 struct snd_ctl_elem_info *uinfo)
1872{
1873 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1874 int num_bands = 0;
1875
1876 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1877 HPI_TUNER_BAND_LAST);
1878
1879 if (num_bands < 0)
1880 return num_bands;
1881
1882 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1883 uinfo->count = 1;
1884 uinfo->value.enumerated.items = num_bands;
1885
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;
1891
1892 strcpy(uinfo->value.enumerated.name,
1893 asihpi_tuner_band_names[
1894 tuner_bands[uinfo->value.enumerated.item]]);
1895
1896 }
1897 return 0;
1898}
1899
1900static int snd_asihpi_tuner_band_get(struct snd_kcontrol *kcontrol,
1901 struct snd_ctl_elem_value *ucontrol)
1902{
1903 u32 h_control = kcontrol->private_value;
1904 /*
1905 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1906 */
1907 u16 band, idx;
1908 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1909 u32 num_bands = 0;
1910
1911 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1912 HPI_TUNER_BAND_LAST);
1913
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001914 hpi_handle_error(hpi_tuner_get_band(h_control, &band));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001915
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;
1920 break;
1921 }
1922
1923 return 0;
1924}
1925
1926static int snd_asihpi_tuner_band_put(struct snd_kcontrol *kcontrol,
1927 struct snd_ctl_elem_value *ucontrol)
1928{
1929 /*
1930 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1931 */
1932 u32 h_control = kcontrol->private_value;
1933 u16 band;
1934 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1935 u32 num_bands = 0;
1936
1937 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1938 HPI_TUNER_BAND_LAST);
1939
1940 band = tuner_bands[ucontrol->value.enumerated.item[0]];
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001941 hpi_handle_error(hpi_tuner_set_band(h_control, band));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001942
1943 return 1;
1944}
1945
1946/* Freq */
1947
1948static int snd_asihpi_tuner_freq_info(struct snd_kcontrol *kcontrol,
1949 struct snd_ctl_elem_info *uinfo)
1950{
1951 u32 h_control = kcontrol->private_value;
1952 u16 err;
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];
1956
1957 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1958 HPI_TUNER_BAND_LAST);
1959
1960 freq_range[0] = INT_MAX;
1961 freq_range[1] = 0;
1962 freq_range[2] = INT_MAX;
1963
1964 for (band_iter = 0; band_iter < num_bands; band_iter++) {
1965 for (idx = 0; idx < 3; idx++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001966 err = hpi_tuner_query_frequency(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001967 idx, tuner_bands[band_iter],
1968 &temp_freq_range[idx]);
1969 if (err != 0)
1970 return err;
1971 }
1972
1973 /* skip band with bogus stepping */
1974 if (temp_freq_range[2] <= 0)
1975 continue;
1976
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];
1983 }
1984
1985 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1986 uinfo->count = 1;
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]);
1990 return 0;
1991}
1992
1993static int snd_asihpi_tuner_freq_get(struct snd_kcontrol *kcontrol,
1994 struct snd_ctl_elem_value *ucontrol)
1995{
1996 u32 h_control = kcontrol->private_value;
1997 u32 freq;
1998
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001999 hpi_handle_error(hpi_tuner_get_frequency(h_control, &freq));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002000 ucontrol->value.integer.value[0] = freq;
2001
2002 return 0;
2003}
2004
2005static int snd_asihpi_tuner_freq_put(struct snd_kcontrol *kcontrol,
2006 struct snd_ctl_elem_value *ucontrol)
2007{
2008 u32 h_control = kcontrol->private_value;
2009 u32 freq;
2010
2011 freq = ucontrol->value.integer.value[0];
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002012 hpi_handle_error(hpi_tuner_set_frequency(h_control, freq));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002013
2014 return 1;
2015}
2016
2017/* Tuner control group initializer */
2018static int __devinit snd_asihpi_tuner_add(struct snd_card_asihpi *asihpi,
2019 struct hpi_control *hpi_ctl)
2020{
2021 struct snd_card *card = asihpi->card;
2022 struct snd_kcontrol_new snd_control;
2023
2024 snd_control.private_value = hpi_ctl->h_control;
2025 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2026
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002027 if (!hpi_tuner_get_gain(hpi_ctl->h_control, NULL)) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002028 asihpi_ctl_init(&snd_control, hpi_ctl, "Gain");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002029 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;
2032
2033 if (ctl_add(card, &snd_control, asihpi) < 0)
2034 return -EINVAL;
2035 }
2036
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002037 asihpi_ctl_init(&snd_control, hpi_ctl, "Band");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002038 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;
2041
2042 if (ctl_add(card, &snd_control, asihpi) < 0)
2043 return -EINVAL;
2044
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002045 asihpi_ctl_init(&snd_control, hpi_ctl, "Freq");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002046 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;
2049
2050 return ctl_add(card, &snd_control, asihpi);
2051}
2052
2053/*------------------------------------------------------------
2054 Meter controls
2055 ------------------------------------------------------------*/
2056static int snd_asihpi_meter_info(struct snd_kcontrol *kcontrol,
2057 struct snd_ctl_elem_info *uinfo)
2058{
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;
2063 return 0;
2064}
2065
2066/* linear values for 10dB steps */
2067static int log2lin[] = {
2068 0x7FFFFFFF, /* 0dB */
2069 679093956,
2070 214748365,
2071 67909396,
2072 21474837,
2073 6790940,
2074 2147484, /* -60dB */
2075 679094,
2076 214748, /* -80 */
2077 67909,
2078 21475, /* -100 */
2079 6791,
2080 2147,
2081 679,
2082 214,
2083 68,
2084 21,
2085 7,
2086 2
2087};
2088
2089static int snd_asihpi_meter_get(struct snd_kcontrol *kcontrol,
2090 struct snd_ctl_elem_value *ucontrol)
2091{
2092 u32 h_control = kcontrol->private_value;
2093 short an_gain_mB[HPI_MAX_CHANNELS], i;
2094 u16 err;
2095
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002096 err = hpi_meter_get_peak(h_control, an_gain_mB);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002097
2098 for (i = 0; i < HPI_MAX_CHANNELS; i++) {
2099 if (err) {
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;
2104 } else {
2105 /* -ve is log value in millibels < -60dB,
2106 * convert to (roughly!) linear,
2107 */
2108 ucontrol->value.integer.value[i] =
2109 log2lin[an_gain_mB[i] / -1000];
2110 }
2111 }
2112 return 0;
2113}
2114
2115static int __devinit snd_asihpi_meter_add(struct snd_card_asihpi *asihpi,
2116 struct hpi_control *hpi_ctl, int subidx)
2117{
2118 struct snd_card *card = asihpi->card;
2119 struct snd_kcontrol_new snd_control;
2120
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002121 asihpi_ctl_init(&snd_control, hpi_ctl, "Meter");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002122 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;
2126
2127 snd_control.index = subidx;
2128
2129 return ctl_add(card, &snd_control, asihpi);
2130}
2131
2132/*------------------------------------------------------------
2133 Multiplexer controls
2134 ------------------------------------------------------------*/
2135static int snd_card_asihpi_mux_count_sources(struct snd_kcontrol *snd_control)
2136{
2137 u32 h_control = snd_control->private_value;
2138 struct hpi_control hpi_ctl;
2139 int s, err;
2140 for (s = 0; s < 32; s++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002141 err = hpi_multiplexer_query_source(h_control, s,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002142 &hpi_ctl.
2143 src_node_type,
2144 &hpi_ctl.
2145 src_node_index);
2146 if (err)
2147 break;
2148 }
2149 return s;
2150}
2151
2152static int snd_asihpi_mux_info(struct snd_kcontrol *kcontrol,
2153 struct snd_ctl_elem_info *uinfo)
2154{
2155 int err;
2156 u16 src_node_type, src_node_index;
2157 u32 h_control = kcontrol->private_value;
2158
2159 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2160 uinfo->count = 1;
2161 uinfo->value.enumerated.items =
2162 snd_card_asihpi_mux_count_sources(kcontrol);
2163
2164 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2165 uinfo->value.enumerated.item =
2166 uinfo->value.enumerated.items - 1;
2167
2168 err =
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002169 hpi_multiplexer_query_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002170 uinfo->value.enumerated.item,
2171 &src_node_type, &src_node_index);
2172
2173 sprintf(uinfo->value.enumerated.name, "%s %d",
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12002174 asihpi_src_names[src_node_type - HPI_SOURCENODE_NONE],
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002175 src_node_index);
2176 return 0;
2177}
2178
2179static int snd_asihpi_mux_get(struct snd_kcontrol *kcontrol,
2180 struct snd_ctl_elem_value *ucontrol)
2181{
2182 u32 h_control = kcontrol->private_value;
2183 u16 source_type, source_index;
2184 u16 src_node_type, src_node_index;
2185 int s;
2186
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002187 hpi_handle_error(hpi_multiplexer_get_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002188 &source_type, &source_index));
2189 /* Should cache this search result! */
2190 for (s = 0; s < 256; s++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002191 if (hpi_multiplexer_query_source(h_control, s,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002192 &src_node_type, &src_node_index))
2193 break;
2194
2195 if ((source_type == src_node_type)
2196 && (source_index == src_node_index)) {
2197 ucontrol->value.enumerated.item[0] = s;
2198 return 0;
2199 }
2200 }
2201 snd_printd(KERN_WARNING
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002202 "Control %x failed to match mux source %hu %hu\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002203 h_control, source_type, source_index);
2204 ucontrol->value.enumerated.item[0] = 0;
2205 return 0;
2206}
2207
2208static int snd_asihpi_mux_put(struct snd_kcontrol *kcontrol,
2209 struct snd_ctl_elem_value *ucontrol)
2210{
2211 int change;
2212 u32 h_control = kcontrol->private_value;
2213 u16 source_type, source_index;
2214 u16 e;
2215
2216 change = 1;
2217
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002218 e = hpi_multiplexer_query_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002219 ucontrol->value.enumerated.item[0],
2220 &source_type, &source_index);
2221 if (!e)
2222 hpi_handle_error(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002223 hpi_multiplexer_set_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002224 source_type, source_index));
2225 return change;
2226}
2227
2228
2229static int __devinit snd_asihpi_mux_add(struct snd_card_asihpi *asihpi,
2230 struct hpi_control *hpi_ctl)
2231{
2232 struct snd_card *card = asihpi->card;
2233 struct snd_kcontrol_new snd_control;
2234
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002235 asihpi_ctl_init(&snd_control, hpi_ctl, "Route");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002236 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2237 snd_control.info = snd_asihpi_mux_info;
2238 snd_control.get = snd_asihpi_mux_get;
2239 snd_control.put = snd_asihpi_mux_put;
2240
2241 return ctl_add(card, &snd_control, asihpi);
2242
2243}
2244
2245/*------------------------------------------------------------
2246 Channel mode controls
2247 ------------------------------------------------------------*/
2248static int snd_asihpi_cmode_info(struct snd_kcontrol *kcontrol,
2249 struct snd_ctl_elem_info *uinfo)
2250{
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002251 static const char * const mode_names[HPI_CHANNEL_MODE_LAST + 1] = {
2252 "invalid",
2253 "Normal", "Swap",
2254 "From Left", "From Right",
2255 "To Left", "To Right"
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002256 };
2257
2258 u32 h_control = kcontrol->private_value;
2259 u16 mode;
2260 int i;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002261 u16 mode_map[6];
2262 int valid_modes = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002263
2264 /* HPI channel mode values can be from 1 to 6
2265 Some adapters only support a contiguous subset
2266 */
2267 for (i = 0; i < HPI_CHANNEL_MODE_LAST; i++)
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002268 if (!hpi_channel_mode_query_mode(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002269 h_control, i, &mode)) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002270 mode_map[valid_modes] = mode;
2271 valid_modes++;
2272 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002273
2274 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2275 uinfo->count = 1;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002276 uinfo->value.enumerated.items = valid_modes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002277
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002278 if (uinfo->value.enumerated.item >= valid_modes)
2279 uinfo->value.enumerated.item = valid_modes - 1;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002280
2281 strcpy(uinfo->value.enumerated.name,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002282 mode_names[mode_map[uinfo->value.enumerated.item]]);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002283
2284 return 0;
2285}
2286
2287static int snd_asihpi_cmode_get(struct snd_kcontrol *kcontrol,
2288 struct snd_ctl_elem_value *ucontrol)
2289{
2290 u32 h_control = kcontrol->private_value;
2291 u16 mode;
2292
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002293 if (hpi_channel_mode_get(h_control, &mode))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002294 mode = 1;
2295
2296 ucontrol->value.enumerated.item[0] = mode - 1;
2297
2298 return 0;
2299}
2300
2301static int snd_asihpi_cmode_put(struct snd_kcontrol *kcontrol,
2302 struct snd_ctl_elem_value *ucontrol)
2303{
2304 int change;
2305 u32 h_control = kcontrol->private_value;
2306
2307 change = 1;
2308
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002309 hpi_handle_error(hpi_channel_mode_set(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002310 ucontrol->value.enumerated.item[0] + 1));
2311 return change;
2312}
2313
2314
2315static int __devinit snd_asihpi_cmode_add(struct snd_card_asihpi *asihpi,
2316 struct hpi_control *hpi_ctl)
2317{
2318 struct snd_card *card = asihpi->card;
2319 struct snd_kcontrol_new snd_control;
2320
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002321 asihpi_ctl_init(&snd_control, hpi_ctl, "Mode");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002322 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2323 snd_control.info = snd_asihpi_cmode_info;
2324 snd_control.get = snd_asihpi_cmode_get;
2325 snd_control.put = snd_asihpi_cmode_put;
2326
2327 return ctl_add(card, &snd_control, asihpi);
2328}
2329
2330/*------------------------------------------------------------
2331 Sampleclock source controls
2332 ------------------------------------------------------------*/
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002333static char *sampleclock_sources[MAX_CLOCKSOURCES] = {
2334 "N/A", "Local PLL", "Digital Sync", "Word External", "Word Header",
2335 "SMPTE", "Digital1", "Auto", "Network", "Invalid",
2336 "Prev Module",
2337 "Digital2", "Digital3", "Digital4", "Digital5",
2338 "Digital6", "Digital7", "Digital8"};
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002339
2340static int snd_asihpi_clksrc_info(struct snd_kcontrol *kcontrol,
2341 struct snd_ctl_elem_info *uinfo)
2342{
2343 struct snd_card_asihpi *asihpi =
2344 (struct snd_card_asihpi *)(kcontrol->private_data);
2345 struct clk_cache *clkcache = &asihpi->cc;
2346 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2347 uinfo->count = 1;
2348 uinfo->value.enumerated.items = clkcache->count;
2349
2350 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2351 uinfo->value.enumerated.item =
2352 uinfo->value.enumerated.items - 1;
2353
2354 strcpy(uinfo->value.enumerated.name,
2355 clkcache->s[uinfo->value.enumerated.item].name);
2356 return 0;
2357}
2358
2359static int snd_asihpi_clksrc_get(struct snd_kcontrol *kcontrol,
2360 struct snd_ctl_elem_value *ucontrol)
2361{
2362 struct snd_card_asihpi *asihpi =
2363 (struct snd_card_asihpi *)(kcontrol->private_data);
2364 struct clk_cache *clkcache = &asihpi->cc;
2365 u32 h_control = kcontrol->private_value;
2366 u16 source, srcindex = 0;
2367 int i;
2368
2369 ucontrol->value.enumerated.item[0] = 0;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002370 if (hpi_sample_clock_get_source(h_control, &source))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002371 source = 0;
2372
2373 if (source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002374 if (hpi_sample_clock_get_source_index(h_control, &srcindex))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002375 srcindex = 0;
2376
2377 for (i = 0; i < clkcache->count; i++)
2378 if ((clkcache->s[i].source == source) &&
2379 (clkcache->s[i].index == srcindex))
2380 break;
2381
2382 ucontrol->value.enumerated.item[0] = i;
2383
2384 return 0;
2385}
2386
2387static int snd_asihpi_clksrc_put(struct snd_kcontrol *kcontrol,
2388 struct snd_ctl_elem_value *ucontrol)
2389{
2390 struct snd_card_asihpi *asihpi =
2391 (struct snd_card_asihpi *)(kcontrol->private_data);
2392 struct clk_cache *clkcache = &asihpi->cc;
2393 int change, item;
2394 u32 h_control = kcontrol->private_value;
2395
2396 change = 1;
2397 item = ucontrol->value.enumerated.item[0];
2398 if (item >= clkcache->count)
2399 item = clkcache->count-1;
2400
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002401 hpi_handle_error(hpi_sample_clock_set_source(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002402 h_control, clkcache->s[item].source));
2403
2404 if (clkcache->s[item].source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002405 hpi_handle_error(hpi_sample_clock_set_source_index(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002406 h_control, clkcache->s[item].index));
2407 return change;
2408}
2409
2410/*------------------------------------------------------------
2411 Clkrate controls
2412 ------------------------------------------------------------*/
2413/* Need to change this to enumerated control with list of rates */
2414static int snd_asihpi_clklocal_info(struct snd_kcontrol *kcontrol,
2415 struct snd_ctl_elem_info *uinfo)
2416{
2417 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2418 uinfo->count = 1;
2419 uinfo->value.integer.min = 8000;
2420 uinfo->value.integer.max = 192000;
2421 uinfo->value.integer.step = 100;
2422
2423 return 0;
2424}
2425
2426static int snd_asihpi_clklocal_get(struct snd_kcontrol *kcontrol,
2427 struct snd_ctl_elem_value *ucontrol)
2428{
2429 u32 h_control = kcontrol->private_value;
2430 u32 rate;
2431 u16 e;
2432
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002433 e = hpi_sample_clock_get_local_rate(h_control, &rate);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002434 if (!e)
2435 ucontrol->value.integer.value[0] = rate;
2436 else
2437 ucontrol->value.integer.value[0] = 0;
2438 return 0;
2439}
2440
2441static int snd_asihpi_clklocal_put(struct snd_kcontrol *kcontrol,
2442 struct snd_ctl_elem_value *ucontrol)
2443{
2444 int change;
2445 u32 h_control = kcontrol->private_value;
2446
2447 /* change = asihpi->mixer_clkrate[addr][0] != left ||
2448 asihpi->mixer_clkrate[addr][1] != right;
2449 */
2450 change = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002451 hpi_handle_error(hpi_sample_clock_set_local_rate(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002452 ucontrol->value.integer.value[0]));
2453 return change;
2454}
2455
2456static int snd_asihpi_clkrate_info(struct snd_kcontrol *kcontrol,
2457 struct snd_ctl_elem_info *uinfo)
2458{
2459 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2460 uinfo->count = 1;
2461 uinfo->value.integer.min = 8000;
2462 uinfo->value.integer.max = 192000;
2463 uinfo->value.integer.step = 100;
2464
2465 return 0;
2466}
2467
2468static int snd_asihpi_clkrate_get(struct snd_kcontrol *kcontrol,
2469 struct snd_ctl_elem_value *ucontrol)
2470{
2471 u32 h_control = kcontrol->private_value;
2472 u32 rate;
2473 u16 e;
2474
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002475 e = hpi_sample_clock_get_sample_rate(h_control, &rate);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002476 if (!e)
2477 ucontrol->value.integer.value[0] = rate;
2478 else
2479 ucontrol->value.integer.value[0] = 0;
2480 return 0;
2481}
2482
2483static int __devinit snd_asihpi_sampleclock_add(struct snd_card_asihpi *asihpi,
2484 struct hpi_control *hpi_ctl)
2485{
2486 struct snd_card *card = asihpi->card;
2487 struct snd_kcontrol_new snd_control;
2488
2489 struct clk_cache *clkcache = &asihpi->cc;
2490 u32 hSC = hpi_ctl->h_control;
2491 int has_aes_in = 0;
2492 int i, j;
2493 u16 source;
2494
2495 snd_control.private_value = hpi_ctl->h_control;
2496
2497 clkcache->has_local = 0;
2498
2499 for (i = 0; i <= HPI_SAMPLECLOCK_SOURCE_LAST; i++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002500 if (hpi_sample_clock_query_source(hSC,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002501 i, &source))
2502 break;
2503 clkcache->s[i].source = source;
2504 clkcache->s[i].index = 0;
2505 clkcache->s[i].name = sampleclock_sources[source];
2506 if (source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
2507 has_aes_in = 1;
2508 if (source == HPI_SAMPLECLOCK_SOURCE_LOCAL)
2509 clkcache->has_local = 1;
2510 }
2511 if (has_aes_in)
2512 /* already will have picked up index 0 above */
2513 for (j = 1; j < 8; j++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002514 if (hpi_sample_clock_query_source_index(hSC,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002515 j, HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT,
2516 &source))
2517 break;
2518 clkcache->s[i].source =
2519 HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT;
2520 clkcache->s[i].index = j;
2521 clkcache->s[i].name = sampleclock_sources[
2522 j+HPI_SAMPLECLOCK_SOURCE_LAST];
2523 i++;
2524 }
2525 clkcache->count = i;
2526
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002527 asihpi_ctl_init(&snd_control, hpi_ctl, "Source");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002528 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE ;
2529 snd_control.info = snd_asihpi_clksrc_info;
2530 snd_control.get = snd_asihpi_clksrc_get;
2531 snd_control.put = snd_asihpi_clksrc_put;
2532 if (ctl_add(card, &snd_control, asihpi) < 0)
2533 return -EINVAL;
2534
2535
2536 if (clkcache->has_local) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002537 asihpi_ctl_init(&snd_control, hpi_ctl, "Localrate");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002538 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE ;
2539 snd_control.info = snd_asihpi_clklocal_info;
2540 snd_control.get = snd_asihpi_clklocal_get;
2541 snd_control.put = snd_asihpi_clklocal_put;
2542
2543
2544 if (ctl_add(card, &snd_control, asihpi) < 0)
2545 return -EINVAL;
2546 }
2547
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002548 asihpi_ctl_init(&snd_control, hpi_ctl, "Rate");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002549 snd_control.access =
2550 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
2551 snd_control.info = snd_asihpi_clkrate_info;
2552 snd_control.get = snd_asihpi_clkrate_get;
2553
2554 return ctl_add(card, &snd_control, asihpi);
2555}
2556/*------------------------------------------------------------
2557 Mixer
2558 ------------------------------------------------------------*/
2559
2560static int __devinit snd_card_asihpi_mixer_new(struct snd_card_asihpi *asihpi)
2561{
2562 struct snd_card *card = asihpi->card;
2563 unsigned int idx = 0;
2564 unsigned int subindex = 0;
2565 int err;
2566 struct hpi_control hpi_ctl, prev_ctl;
2567
2568 if (snd_BUG_ON(!asihpi))
2569 return -EINVAL;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002570 strcpy(card->mixername, "Asihpi Mixer");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002571
2572 err =
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002573 hpi_mixer_open(asihpi->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002574 &asihpi->h_mixer);
2575 hpi_handle_error(err);
2576 if (err)
2577 return -err;
2578
Takashi Iwai21896bc2010-06-02 12:08:37 +02002579 memset(&prev_ctl, 0, sizeof(prev_ctl));
2580 prev_ctl.control_type = -1;
2581
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002582 for (idx = 0; idx < 2000; idx++) {
2583 err = hpi_mixer_get_control_by_index(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002584 asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002585 idx,
2586 &hpi_ctl.src_node_type,
2587 &hpi_ctl.src_node_index,
2588 &hpi_ctl.dst_node_type,
2589 &hpi_ctl.dst_node_index,
2590 &hpi_ctl.control_type,
2591 &hpi_ctl.h_control);
2592 if (err) {
2593 if (err == HPI_ERROR_CONTROL_DISABLED) {
2594 if (mixer_dump)
2595 snd_printk(KERN_INFO
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002596 "Disabled HPI Control(%d)\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002597 idx);
2598 continue;
2599 } else
2600 break;
2601
2602 }
2603
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12002604 hpi_ctl.src_node_type -= HPI_SOURCENODE_NONE;
2605 hpi_ctl.dst_node_type -= HPI_DESTNODE_NONE;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002606
2607 /* ASI50xx in SSX mode has multiple meters on the same node.
2608 Use subindex to create distinct ALSA controls
2609 for any duplicated controls.
2610 */
2611 if ((hpi_ctl.control_type == prev_ctl.control_type) &&
2612 (hpi_ctl.src_node_type == prev_ctl.src_node_type) &&
2613 (hpi_ctl.src_node_index == prev_ctl.src_node_index) &&
2614 (hpi_ctl.dst_node_type == prev_ctl.dst_node_type) &&
2615 (hpi_ctl.dst_node_index == prev_ctl.dst_node_index))
2616 subindex++;
2617 else
2618 subindex = 0;
2619
2620 prev_ctl = hpi_ctl;
2621
2622 switch (hpi_ctl.control_type) {
2623 case HPI_CONTROL_VOLUME:
2624 err = snd_asihpi_volume_add(asihpi, &hpi_ctl);
2625 break;
2626 case HPI_CONTROL_LEVEL:
2627 err = snd_asihpi_level_add(asihpi, &hpi_ctl);
2628 break;
2629 case HPI_CONTROL_MULTIPLEXER:
2630 err = snd_asihpi_mux_add(asihpi, &hpi_ctl);
2631 break;
2632 case HPI_CONTROL_CHANNEL_MODE:
2633 err = snd_asihpi_cmode_add(asihpi, &hpi_ctl);
2634 break;
2635 case HPI_CONTROL_METER:
2636 err = snd_asihpi_meter_add(asihpi, &hpi_ctl, subindex);
2637 break;
2638 case HPI_CONTROL_SAMPLECLOCK:
2639 err = snd_asihpi_sampleclock_add(
2640 asihpi, &hpi_ctl);
2641 break;
2642 case HPI_CONTROL_CONNECTION: /* ignore these */
2643 continue;
2644 case HPI_CONTROL_TUNER:
2645 err = snd_asihpi_tuner_add(asihpi, &hpi_ctl);
2646 break;
2647 case HPI_CONTROL_AESEBU_TRANSMITTER:
2648 err = snd_asihpi_aesebu_tx_add(asihpi, &hpi_ctl);
2649 break;
2650 case HPI_CONTROL_AESEBU_RECEIVER:
2651 err = snd_asihpi_aesebu_rx_add(asihpi, &hpi_ctl);
2652 break;
2653 case HPI_CONTROL_VOX:
2654 case HPI_CONTROL_BITSTREAM:
2655 case HPI_CONTROL_MICROPHONE:
2656 case HPI_CONTROL_PARAMETRIC_EQ:
2657 case HPI_CONTROL_COMPANDER:
2658 default:
2659 if (mixer_dump)
2660 snd_printk(KERN_INFO
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002661 "Untranslated HPI Control"
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002662 "(%d) %d %d %d %d %d\n",
2663 idx,
2664 hpi_ctl.control_type,
2665 hpi_ctl.src_node_type,
2666 hpi_ctl.src_node_index,
2667 hpi_ctl.dst_node_type,
2668 hpi_ctl.dst_node_index);
2669 continue;
2670 };
2671 if (err < 0)
2672 return err;
2673 }
2674 if (HPI_ERROR_INVALID_OBJ_INDEX != err)
2675 hpi_handle_error(err);
2676
2677 snd_printk(KERN_INFO "%d mixer controls found\n", idx);
2678
2679 return 0;
2680}
2681
2682/*------------------------------------------------------------
2683 /proc interface
2684 ------------------------------------------------------------*/
2685
2686static void
2687snd_asihpi_proc_read(struct snd_info_entry *entry,
2688 struct snd_info_buffer *buffer)
2689{
2690 struct snd_card_asihpi *asihpi = entry->private_data;
2691 u16 version;
2692 u32 h_control;
2693 u32 rate = 0;
2694 u16 source = 0;
2695 int err;
2696
2697 snd_iprintf(buffer, "ASIHPI driver proc file\n");
2698 snd_iprintf(buffer,
2699 "adapter ID=%4X\n_index=%d\n"
2700 "num_outstreams=%d\n_num_instreams=%d\n",
2701 asihpi->type, asihpi->adapter_index,
2702 asihpi->num_outstreams, asihpi->num_instreams);
2703
2704 version = asihpi->version;
2705 snd_iprintf(buffer,
2706 "serial#=%d\n_hw version %c%d\nDSP code version %03d\n",
2707 asihpi->serial_number, ((version >> 3) & 0xf) + 'A',
2708 version & 0x7,
2709 ((version >> 13) * 100) + ((version >> 7) & 0x3f));
2710
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002711 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002712 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
2713 HPI_CONTROL_SAMPLECLOCK, &h_control);
2714
2715 if (!err) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002716 err = hpi_sample_clock_get_sample_rate(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002717 h_control, &rate);
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002718 err += hpi_sample_clock_get_source(h_control, &source);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002719
2720 if (!err)
2721 snd_iprintf(buffer, "sample_clock=%d_hz, source %s\n",
2722 rate, sampleclock_sources[source]);
2723 }
2724
2725}
2726
2727
2728static void __devinit snd_asihpi_proc_init(struct snd_card_asihpi *asihpi)
2729{
2730 struct snd_info_entry *entry;
2731
2732 if (!snd_card_proc_new(asihpi->card, "info", &entry))
2733 snd_info_set_text_ops(entry, asihpi, snd_asihpi_proc_read);
2734}
2735
2736/*------------------------------------------------------------
2737 HWDEP
2738 ------------------------------------------------------------*/
2739
2740static int snd_asihpi_hpi_open(struct snd_hwdep *hw, struct file *file)
2741{
2742 if (enable_hpi_hwdep)
2743 return 0;
2744 else
2745 return -ENODEV;
2746
2747}
2748
2749static int snd_asihpi_hpi_release(struct snd_hwdep *hw, struct file *file)
2750{
2751 if (enable_hpi_hwdep)
2752 return asihpi_hpi_release(file);
2753 else
2754 return -ENODEV;
2755}
2756
2757static int snd_asihpi_hpi_ioctl(struct snd_hwdep *hw, struct file *file,
2758 unsigned int cmd, unsigned long arg)
2759{
2760 if (enable_hpi_hwdep)
2761 return asihpi_hpi_ioctl(file, cmd, arg);
2762 else
2763 return -ENODEV;
2764}
2765
2766
2767/* results in /dev/snd/hwC#D0 file for each card with index #
2768 also /proc/asound/hwdep will contain '#-00: asihpi (HPI) for each card'
2769*/
2770static int __devinit snd_asihpi_hpi_new(struct snd_card_asihpi *asihpi,
2771 int device, struct snd_hwdep **rhwdep)
2772{
2773 struct snd_hwdep *hw;
2774 int err;
2775
2776 if (rhwdep)
2777 *rhwdep = NULL;
2778 err = snd_hwdep_new(asihpi->card, "HPI", device, &hw);
2779 if (err < 0)
2780 return err;
2781 strcpy(hw->name, "asihpi (HPI)");
2782 hw->iface = SNDRV_HWDEP_IFACE_LAST;
2783 hw->ops.open = snd_asihpi_hpi_open;
2784 hw->ops.ioctl = snd_asihpi_hpi_ioctl;
2785 hw->ops.release = snd_asihpi_hpi_release;
2786 hw->private_data = asihpi;
2787 if (rhwdep)
2788 *rhwdep = hw;
2789 return 0;
2790}
2791
2792/*------------------------------------------------------------
2793 CARD
2794 ------------------------------------------------------------*/
2795static int __devinit snd_asihpi_probe(struct pci_dev *pci_dev,
2796 const struct pci_device_id *pci_id)
2797{
2798 int err;
2799
2800 u16 version;
2801 int pcm_substreams;
2802
2803 struct hpi_adapter *hpi_card;
2804 struct snd_card *card;
2805 struct snd_card_asihpi *asihpi;
2806
2807 u32 h_control;
2808 u32 h_stream;
2809
2810 static int dev;
2811 if (dev >= SNDRV_CARDS)
2812 return -ENODEV;
2813
2814 /* Should this be enable[hpi_card->index] ? */
2815 if (!enable[dev]) {
2816 dev++;
2817 return -ENOENT;
2818 }
2819
2820 err = asihpi_adapter_probe(pci_dev, pci_id);
2821 if (err < 0)
2822 return err;
2823
2824 hpi_card = pci_get_drvdata(pci_dev);
2825 /* first try to give the card the same index as its hardware index */
2826 err = snd_card_create(hpi_card->index,
2827 id[hpi_card->index], THIS_MODULE,
2828 sizeof(struct snd_card_asihpi),
2829 &card);
2830 if (err < 0) {
2831 /* if that fails, try the default index==next available */
2832 err =
2833 snd_card_create(index[dev], id[dev],
2834 THIS_MODULE,
2835 sizeof(struct snd_card_asihpi),
2836 &card);
2837 if (err < 0)
2838 return err;
2839 snd_printk(KERN_WARNING
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002840 "**** WARNING **** Adapter index %d->ALSA index %d\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002841 hpi_card->index, card->number);
2842 }
2843
Eliot Blennerhassett12253672011-02-10 17:26:10 +13002844 snd_card_set_dev(card, &pci_dev->dev);
2845
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002846 asihpi = (struct snd_card_asihpi *) card->private_data;
2847 asihpi->card = card;
Eliot Blennerhassett12253672011-02-10 17:26:10 +13002848 asihpi->pci = pci_dev;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002849 asihpi->adapter_index = hpi_card->index;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002850 hpi_handle_error(hpi_adapter_get_info(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002851 asihpi->adapter_index,
2852 &asihpi->num_outstreams,
2853 &asihpi->num_instreams,
2854 &asihpi->version,
2855 &asihpi->serial_number, &asihpi->type));
2856
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"
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002860 "Hw Version %c%d DSP code version %03d\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002861 asihpi->type, asihpi->adapter_index,
2862 asihpi->num_outstreams,
2863 asihpi->num_instreams, asihpi->serial_number,
2864 ((version >> 3) & 0xf) + 'A',
2865 version & 0x7,
2866 ((version >> 13) * 100) + ((version >> 7) & 0x3f));
2867
2868 pcm_substreams = asihpi->num_outstreams;
2869 if (pcm_substreams < asihpi->num_instreams)
2870 pcm_substreams = asihpi->num_instreams;
2871
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002872 err = hpi_adapter_get_property(asihpi->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002873 HPI_ADAPTER_PROPERTY_CAPS1,
2874 NULL, &asihpi->support_grouping);
2875 if (err)
2876 asihpi->support_grouping = 0;
2877
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002878 err = hpi_adapter_get_property(asihpi->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002879 HPI_ADAPTER_PROPERTY_CAPS2,
2880 &asihpi->support_mrx, NULL);
2881 if (err)
2882 asihpi->support_mrx = 0;
2883
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002884 err = hpi_adapter_get_property(asihpi->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002885 HPI_ADAPTER_PROPERTY_INTERVAL,
2886 NULL, &asihpi->update_interval_frames);
2887 if (err)
2888 asihpi->update_interval_frames = 512;
2889
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002890 hpi_handle_error(hpi_instream_open(asihpi->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002891 0, &h_stream));
2892
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002893 err = hpi_instream_host_buffer_free(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002894 asihpi->support_mmap = (!err);
2895
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002896 hpi_handle_error(hpi_instream_close(h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002897
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002898 err = hpi_adapter_get_property(asihpi->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002899 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 "supports mmap:%d grouping:%d mrx:%d\n",
2907 asihpi->support_mmap,
2908 asihpi->support_grouping,
2909 asihpi->support_mrx
2910 );
2911
2912
2913 err = snd_card_asihpi_pcm_new(asihpi, 0, pcm_substreams);
2914 if (err < 0) {
2915 snd_printk(KERN_ERR "pcm_new failed\n");
2916 goto __nodev;
2917 }
2918 err = snd_card_asihpi_mixer_new(asihpi);
2919 if (err < 0) {
2920 snd_printk(KERN_ERR "mixer_new failed\n");
2921 goto __nodev;
2922 }
2923
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002924 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002925 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
2926 HPI_CONTROL_SAMPLECLOCK, &h_control);
2927
2928 if (!err)
2929 err = hpi_sample_clock_set_local_rate(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002930 h_control, adapter_fs);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002931
2932 snd_asihpi_proc_init(asihpi);
2933
2934 /* always create, can be enabled or disabled dynamically
2935 by enable_hwdep module param*/
2936 snd_asihpi_hpi_new(asihpi, 0, NULL);
2937
2938 if (asihpi->support_mmap)
2939 strcpy(card->driver, "ASIHPI-MMAP");
2940 else
2941 strcpy(card->driver, "ASIHPI");
2942
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);
2947 if (!err) {
2948 hpi_card->snd_card_asihpi = card;
2949 dev++;
2950 return 0;
2951 }
2952__nodev:
2953 snd_card_free(card);
2954 snd_printk(KERN_ERR "snd_asihpi_probe error %d\n", err);
2955 return err;
2956
2957}
2958
2959static void __devexit snd_asihpi_remove(struct pci_dev *pci_dev)
2960{
2961 struct hpi_adapter *hpi_card = pci_get_drvdata(pci_dev);
2962
2963 snd_card_free(hpi_card->snd_card_asihpi);
2964 hpi_card->snd_card_asihpi = NULL;
2965 asihpi_adapter_remove(pci_dev);
2966}
2967
2968static 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},
2975 {0,}
2976};
2977MODULE_DEVICE_TABLE(pci, asihpi_pci_tbl);
2978
2979static struct pci_driver driver = {
2980 .name = "asihpi",
2981 .id_table = asihpi_pci_tbl,
2982 .probe = snd_asihpi_probe,
2983 .remove = __devexit_p(snd_asihpi_remove),
2984#ifdef CONFIG_PM
2985/* .suspend = snd_asihpi_suspend,
2986 .resume = snd_asihpi_resume, */
2987#endif
2988};
2989
2990static int __init snd_asihpi_init(void)
2991{
2992 asihpi_init();
2993 return pci_register_driver(&driver);
2994}
2995
2996static void __exit snd_asihpi_exit(void)
2997{
2998
2999 pci_unregister_driver(&driver);
3000 asihpi_exit();
3001}
3002
3003module_init(snd_asihpi_init)
3004module_exit(snd_asihpi_exit)
3005