blob: edcbe39d60338dddbf1ac04606d988e112c4ed51 [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#include "hpi_internal.h"
26#include "hpimsginit.h"
27#include "hpioctl.h"
28
29#include <linux/pci.h>
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +130030#include <linux/version.h>
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020031#include <linux/init.h>
32#include <linux/jiffies.h>
33#include <linux/slab.h>
34#include <linux/time.h>
35#include <linux/wait.h>
36#include <sound/core.h>
37#include <sound/control.h>
38#include <sound/pcm.h>
39#include <sound/pcm_params.h>
40#include <sound/info.h>
41#include <sound/initval.h>
42#include <sound/tlv.h>
43#include <sound/hwdep.h>
44
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020045MODULE_LICENSE("GPL");
46MODULE_AUTHOR("AudioScience inc. <support@audioscience.com>");
47MODULE_DESCRIPTION("AudioScience ALSA ASI5000 ASI6000 ASI87xx ASI89xx");
48
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +130049#if defined CONFIG_SND_DEBUG_VERBOSE
50/**
51 * snd_printddd - very verbose debug printk
52 * @format: format string
53 *
54 * Works like snd_printk() for debugging purposes.
55 * Ignored when CONFIG_SND_DEBUG_VERBOSE is not set.
56 * Must set snd module debug parameter to 3 to enable at runtime.
57 */
58#define snd_printddd(format, args...) \
59 __snd_printk(3, __FILE__, __LINE__, format, ##args)
60#else
61#define snd_printddd(format, args...) do { } while (0)
62#endif
63
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020064static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* index 0-MAX */
65static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
66static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
67static int enable_hpi_hwdep = 1;
68
69module_param_array(index, int, NULL, S_IRUGO);
70MODULE_PARM_DESC(index, "ALSA index value for AudioScience soundcard.");
71
72module_param_array(id, charp, NULL, S_IRUGO);
73MODULE_PARM_DESC(id, "ALSA ID string for AudioScience soundcard.");
74
75module_param_array(enable, bool, NULL, S_IRUGO);
76MODULE_PARM_DESC(enable, "ALSA enable AudioScience soundcard.");
77
78module_param(enable_hpi_hwdep, bool, S_IRUGO|S_IWUSR);
79MODULE_PARM_DESC(enable_hpi_hwdep,
80 "ALSA enable HPI hwdep for AudioScience soundcard ");
81
82/* identify driver */
83#ifdef KERNEL_ALSA_BUILD
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +130084static char *build_info = "Built using headers from kernel source";
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020085module_param(build_info, charp, S_IRUGO);
86MODULE_PARM_DESC(build_info, "built using headers from kernel source");
87#else
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +130088static char *build_info = "Built within ALSA source";
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020089module_param(build_info, charp, S_IRUGO);
90MODULE_PARM_DESC(build_info, "built within ALSA source");
91#endif
92
93/* set to 1 to dump every control from adapter to log */
94static const int mixer_dump;
95
96#define DEFAULT_SAMPLERATE 44100
97static int adapter_fs = DEFAULT_SAMPLERATE;
98
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020099/* defaults */
100#define PERIODS_MIN 2
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300101#define PERIOD_BYTES_MIN 2048
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200102#define BUFFER_BYTES_MAX (512 * 1024)
103
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300104/* convert stream to character */
105#define SCHR(s) ((s == SNDRV_PCM_STREAM_PLAYBACK) ? 'P' : 'C')
106
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200107/*#define TIMER_MILLISECONDS 20
108#define FORCE_TIMER_JIFFIES ((TIMER_MILLISECONDS * HZ + 999)/1000)
109*/
110
111#define MAX_CLOCKSOURCES (HPI_SAMPLECLOCK_SOURCE_LAST + 1 + 7)
112
113struct clk_source {
114 int source;
115 int index;
116 char *name;
117};
118
119struct clk_cache {
120 int count;
121 int has_local;
122 struct clk_source s[MAX_CLOCKSOURCES];
123};
124
125/* Per card data */
126struct snd_card_asihpi {
127 struct snd_card *card;
128 struct pci_dev *pci;
129 u16 adapter_index;
130 u32 serial_number;
131 u16 type;
132 u16 version;
133 u16 num_outstreams;
134 u16 num_instreams;
135
136 u32 h_mixer;
137 struct clk_cache cc;
138
139 u16 support_mmap;
140 u16 support_grouping;
141 u16 support_mrx;
142 u16 update_interval_frames;
143 u16 in_max_chans;
144 u16 out_max_chans;
145};
146
147/* Per stream data */
148struct snd_card_asihpi_pcm {
149 struct timer_list timer;
150 unsigned int respawn_timer;
151 unsigned int hpi_buffer_attached;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300152 unsigned int buffer_bytes;
153 unsigned int period_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200154 unsigned int bytes_per_sec;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300155 unsigned int pcm_buf_host_rw_ofs; /* Host R/W pos */
156 unsigned int pcm_buf_dma_ofs; /* DMA R/W offset in buffer */
157 unsigned int pcm_buf_elapsed_dma_ofs; /* DMA R/W offset in buffer */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200158 struct snd_pcm_substream *substream;
159 u32 h_stream;
160 struct hpi_format format;
161};
162
163/* universal stream verbs work with out or in stream handles */
164
165/* Functions to allow driver to give a buffer to HPI for busmastering */
166
167static u16 hpi_stream_host_buffer_attach(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200168 u32 h_stream, /* handle to outstream. */
169 u32 size_in_bytes, /* size in bytes of bus mastering buffer */
170 u32 pci_address
171)
172{
173 struct hpi_message hm;
174 struct hpi_response hr;
175 unsigned int obj = hpi_handle_object(h_stream);
176
177 if (!h_stream)
178 return HPI_ERROR_INVALID_OBJ;
179 hpi_init_message_response(&hm, &hr, obj,
180 obj == HPI_OBJ_OSTREAM ?
181 HPI_OSTREAM_HOSTBUFFER_ALLOC :
182 HPI_ISTREAM_HOSTBUFFER_ALLOC);
183
184 hpi_handle_to_indexes(h_stream, &hm.adapter_index,
185 &hm.obj_index);
186
187 hm.u.d.u.buffer.buffer_size = size_in_bytes;
188 hm.u.d.u.buffer.pci_address = pci_address;
189 hm.u.d.u.buffer.command = HPI_BUFFER_CMD_INTERNAL_GRANTADAPTER;
190 hpi_send_recv(&hm, &hr);
191 return hr.error;
192}
193
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300194static u16 hpi_stream_host_buffer_detach(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200195{
196 struct hpi_message hm;
197 struct hpi_response hr;
198 unsigned int obj = hpi_handle_object(h_stream);
199
200 if (!h_stream)
201 return HPI_ERROR_INVALID_OBJ;
202
203 hpi_init_message_response(&hm, &hr, obj,
204 obj == HPI_OBJ_OSTREAM ?
205 HPI_OSTREAM_HOSTBUFFER_FREE :
206 HPI_ISTREAM_HOSTBUFFER_FREE);
207
208 hpi_handle_to_indexes(h_stream, &hm.adapter_index,
209 &hm.obj_index);
210 hm.u.d.u.buffer.command = HPI_BUFFER_CMD_INTERNAL_REVOKEADAPTER;
211 hpi_send_recv(&hm, &hr);
212 return hr.error;
213}
214
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300215static inline u16 hpi_stream_start(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200216{
217 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300218 return hpi_outstream_start(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200219 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300220 return hpi_instream_start(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200221}
222
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300223static inline u16 hpi_stream_stop(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200224{
225 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300226 return hpi_outstream_stop(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200227 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300228 return hpi_instream_stop(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200229}
230
231static inline u16 hpi_stream_get_info_ex(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200232 u32 h_stream,
233 u16 *pw_state,
234 u32 *pbuffer_size,
235 u32 *pdata_in_buffer,
236 u32 *psample_count,
237 u32 *pauxiliary_data
238)
239{
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300240 u16 e;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200241 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300242 e = hpi_outstream_get_info_ex(h_stream, pw_state,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200243 pbuffer_size, pdata_in_buffer,
244 psample_count, pauxiliary_data);
245 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300246 e = hpi_instream_get_info_ex(h_stream, pw_state,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200247 pbuffer_size, pdata_in_buffer,
248 psample_count, pauxiliary_data);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300249 return e;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200250}
251
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300252static inline u16 hpi_stream_group_add(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200253 u32 h_master,
254 u32 h_stream)
255{
256 if (hpi_handle_object(h_master) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300257 return hpi_outstream_group_add(h_master, h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200258 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300259 return hpi_instream_group_add(h_master, h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200260}
261
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300262static inline u16 hpi_stream_group_reset(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200263{
264 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300265 return hpi_outstream_group_reset(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200266 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300267 return hpi_instream_group_reset(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200268}
269
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300270static inline u16 hpi_stream_group_get_map(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200271 u32 h_stream, u32 *mo, u32 *mi)
272{
273 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300274 return hpi_outstream_group_get_map(h_stream, mo, mi);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200275 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300276 return hpi_instream_group_get_map(h_stream, mo, mi);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200277}
278
279static u16 handle_error(u16 err, int line, char *filename)
280{
281 if (err)
282 printk(KERN_WARNING
283 "in file %s, line %d: HPI error %d\n",
284 filename, line, err);
285 return err;
286}
287
288#define hpi_handle_error(x) handle_error(x, __LINE__, __FILE__)
289
290/***************************** GENERAL PCM ****************/
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200291static void print_hwparams(struct snd_pcm_hw_params *p)
292{
293 snd_printd("HWPARAMS \n");
294 snd_printd("samplerate %d \n", params_rate(p));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300295 snd_printd("Channels %d \n", params_channels(p));
296 snd_printd("Format %d \n", params_format(p));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200297 snd_printd("subformat %d \n", params_subformat(p));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300298 snd_printd("Buffer bytes %d \n", params_buffer_bytes(p));
299 snd_printd("Period bytes %d \n", params_period_bytes(p));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200300 snd_printd("access %d \n", params_access(p));
301 snd_printd("period_size %d \n", params_period_size(p));
302 snd_printd("periods %d \n", params_periods(p));
303 snd_printd("buffer_size %d \n", params_buffer_size(p));
304}
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200305
306static snd_pcm_format_t hpi_to_alsa_formats[] = {
307 -1, /* INVALID */
308 SNDRV_PCM_FORMAT_U8, /* HPI_FORMAT_PCM8_UNSIGNED 1 */
309 SNDRV_PCM_FORMAT_S16, /* HPI_FORMAT_PCM16_SIGNED 2 */
310 -1, /* HPI_FORMAT_MPEG_L1 3 */
311 SNDRV_PCM_FORMAT_MPEG, /* HPI_FORMAT_MPEG_L2 4 */
312 SNDRV_PCM_FORMAT_MPEG, /* HPI_FORMAT_MPEG_L3 5 */
313 -1, /* HPI_FORMAT_DOLBY_AC2 6 */
314 -1, /* HPI_FORMAT_DOLBY_AC3 7 */
315 SNDRV_PCM_FORMAT_S16_BE,/* HPI_FORMAT_PCM16_BIGENDIAN 8 */
316 -1, /* HPI_FORMAT_AA_TAGIT1_HITS 9 */
317 -1, /* HPI_FORMAT_AA_TAGIT1_INSERTS 10 */
318 SNDRV_PCM_FORMAT_S32, /* HPI_FORMAT_PCM32_SIGNED 11 */
319 -1, /* HPI_FORMAT_RAW_BITSTREAM 12 */
320 -1, /* HPI_FORMAT_AA_TAGIT1_HITS_EX1 13 */
321 SNDRV_PCM_FORMAT_FLOAT, /* HPI_FORMAT_PCM32_FLOAT 14 */
322#if 1
323 /* ALSA can't handle 3 byte sample size together with power-of-2
324 * constraint on buffer_bytes, so disable this format
325 */
326 -1
327#else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300328 /* SNDRV_PCM_FORMAT_S24_3LE */ /* HPI_FORMAT_PCM24_SIGNED 15 */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200329#endif
330};
331
332
333static int snd_card_asihpi_format_alsa2hpi(snd_pcm_format_t alsa_format,
334 u16 *hpi_format)
335{
336 u16 format;
337
338 for (format = HPI_FORMAT_PCM8_UNSIGNED;
339 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
340 if (hpi_to_alsa_formats[format] == alsa_format) {
341 *hpi_format = format;
342 return 0;
343 }
344 }
345
346 snd_printd(KERN_WARNING "failed match for alsa format %d\n",
347 alsa_format);
348 *hpi_format = 0;
349 return -EINVAL;
350}
351
352static void snd_card_asihpi_pcm_samplerates(struct snd_card_asihpi *asihpi,
353 struct snd_pcm_hardware *pcmhw)
354{
355 u16 err;
356 u32 h_control;
357 u32 sample_rate;
358 int idx;
359 unsigned int rate_min = 200000;
360 unsigned int rate_max = 0;
361 unsigned int rates = 0;
362
363 if (asihpi->support_mrx) {
364 rates |= SNDRV_PCM_RATE_CONTINUOUS;
365 rates |= SNDRV_PCM_RATE_8000_96000;
366 rate_min = 8000;
367 rate_max = 100000;
368 } else {
369 /* on cards without SRC,
370 valid rates are determined by sampleclock */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300371 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200372 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
373 HPI_CONTROL_SAMPLECLOCK, &h_control);
374 if (err) {
375 snd_printk(KERN_ERR
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300376 "No local sampleclock, err %d\n", err);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200377 }
378
Eliot Blennerhassett7bf76c32011-03-25 15:25:46 +1300379 for (idx = -1; idx < 100; idx++) {
380 if (idx == -1) {
381 if (hpi_sample_clock_get_sample_rate(h_control,
382 &sample_rate))
383 continue;
384 } else if (hpi_sample_clock_query_local_rate(h_control,
385 idx, &sample_rate)) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200386 break;
387 }
388
389 rate_min = min(rate_min, sample_rate);
390 rate_max = max(rate_max, sample_rate);
391
392 switch (sample_rate) {
393 case 5512:
394 rates |= SNDRV_PCM_RATE_5512;
395 break;
396 case 8000:
397 rates |= SNDRV_PCM_RATE_8000;
398 break;
399 case 11025:
400 rates |= SNDRV_PCM_RATE_11025;
401 break;
402 case 16000:
403 rates |= SNDRV_PCM_RATE_16000;
404 break;
405 case 22050:
406 rates |= SNDRV_PCM_RATE_22050;
407 break;
408 case 32000:
409 rates |= SNDRV_PCM_RATE_32000;
410 break;
411 case 44100:
412 rates |= SNDRV_PCM_RATE_44100;
413 break;
414 case 48000:
415 rates |= SNDRV_PCM_RATE_48000;
416 break;
417 case 64000:
418 rates |= SNDRV_PCM_RATE_64000;
419 break;
420 case 88200:
421 rates |= SNDRV_PCM_RATE_88200;
422 break;
423 case 96000:
424 rates |= SNDRV_PCM_RATE_96000;
425 break;
426 case 176400:
427 rates |= SNDRV_PCM_RATE_176400;
428 break;
429 case 192000:
430 rates |= SNDRV_PCM_RATE_192000;
431 break;
432 default: /* some other rate */
433 rates |= SNDRV_PCM_RATE_KNOT;
434 }
435 }
436 }
437
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200438 pcmhw->rates = rates;
439 pcmhw->rate_min = rate_min;
440 pcmhw->rate_max = rate_max;
441}
442
443static int snd_card_asihpi_pcm_hw_params(struct snd_pcm_substream *substream,
444 struct snd_pcm_hw_params *params)
445{
446 struct snd_pcm_runtime *runtime = substream->runtime;
447 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
448 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
449 int err;
450 u16 format;
Kulikov Vasiliy315e8f72010-07-15 22:48:19 +0400451 int width;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200452 unsigned int bytes_per_sec;
453
454 print_hwparams(params);
455 err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
456 if (err < 0)
457 return err;
458 err = snd_card_asihpi_format_alsa2hpi(params_format(params), &format);
459 if (err)
460 return err;
461
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300462 snd_printdd("format %d, %d chans, %d_hz\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200463 format, params_channels(params),
464 params_rate(params));
465
466 hpi_handle_error(hpi_format_create(&dpcm->format,
467 params_channels(params),
468 format, params_rate(params), 0, 0));
469
470 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300471 if (hpi_instream_reset(dpcm->h_stream) != 0)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200472 return -EINVAL;
473
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300474 if (hpi_instream_set_format(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200475 dpcm->h_stream, &dpcm->format) != 0)
476 return -EINVAL;
477 }
478
479 dpcm->hpi_buffer_attached = 0;
480 if (card->support_mmap) {
481
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300482 err = hpi_stream_host_buffer_attach(dpcm->h_stream,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200483 params_buffer_bytes(params), runtime->dma_addr);
484 if (err == 0) {
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300485 snd_printdd(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200486 "stream_host_buffer_attach succeeded %u %lu\n",
487 params_buffer_bytes(params),
488 (unsigned long)runtime->dma_addr);
489 } else {
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300490 snd_printd("stream_host_buffer_attach error %d\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200491 err);
492 return -ENOMEM;
493 }
494
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300495 err = hpi_stream_get_info_ex(dpcm->h_stream, NULL,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200496 &dpcm->hpi_buffer_attached,
497 NULL, NULL, NULL);
498
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300499 snd_printdd("stream_host_buffer_attach status 0x%x\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200500 dpcm->hpi_buffer_attached);
501 }
502 bytes_per_sec = params_rate(params) * params_channels(params);
Kulikov Vasiliy315e8f72010-07-15 22:48:19 +0400503 width = snd_pcm_format_width(params_format(params));
504 bytes_per_sec *= width;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200505 bytes_per_sec /= 8;
Kulikov Vasiliy315e8f72010-07-15 22:48:19 +0400506 if (width < 0 || bytes_per_sec == 0)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200507 return -EINVAL;
508
509 dpcm->bytes_per_sec = bytes_per_sec;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300510 dpcm->buffer_bytes = params_buffer_bytes(params);
511 dpcm->period_bytes = params_period_bytes(params);
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300512 snd_printdd("buffer_bytes=%d, period_bytes=%d, bps=%d\n",
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300513 dpcm->buffer_bytes, dpcm->period_bytes, bytes_per_sec);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200514
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200515 return 0;
516}
517
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300518static int
519snd_card_asihpi_hw_free(struct snd_pcm_substream *substream)
520{
521 struct snd_pcm_runtime *runtime = substream->runtime;
522 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
523 if (dpcm->hpi_buffer_attached)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300524 hpi_stream_host_buffer_detach(dpcm->h_stream);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300525
526 snd_pcm_lib_free_pages(substream);
527 return 0;
528}
529
530static void snd_card_asihpi_runtime_free(struct snd_pcm_runtime *runtime)
531{
532 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
533 kfree(dpcm);
534}
535
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200536static void snd_card_asihpi_pcm_timer_start(struct snd_pcm_substream *
537 substream)
538{
539 struct snd_pcm_runtime *runtime = substream->runtime;
540 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
541 int expiry;
542
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300543 expiry = HZ / 200;
544 /*? (dpcm->period_bytes * HZ / dpcm->bytes_per_sec); */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300545 expiry = max(expiry, 1); /* don't let it be zero! */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200546 dpcm->timer.expires = jiffies + expiry;
547 dpcm->respawn_timer = 1;
548 add_timer(&dpcm->timer);
549}
550
551static void snd_card_asihpi_pcm_timer_stop(struct snd_pcm_substream *substream)
552{
553 struct snd_pcm_runtime *runtime = substream->runtime;
554 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
555
556 dpcm->respawn_timer = 0;
557 del_timer(&dpcm->timer);
558}
559
560static int snd_card_asihpi_trigger(struct snd_pcm_substream *substream,
561 int cmd)
562{
563 struct snd_card_asihpi_pcm *dpcm = substream->runtime->private_data;
564 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
565 struct snd_pcm_substream *s;
566 u16 e;
567
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300568 snd_printdd("%c%d trigger\n",
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300569 SCHR(substream->stream), substream->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200570 switch (cmd) {
571 case SNDRV_PCM_TRIGGER_START:
572 snd_pcm_group_for_each_entry(s, substream) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300573 struct snd_pcm_runtime *runtime = s->runtime;
574 struct snd_card_asihpi_pcm *ds = runtime->private_data;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200575
576 if (snd_pcm_substream_chip(s) != card)
577 continue;
578
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300579 /* don't link Cap and Play */
580 if (substream->stream != s->stream)
581 continue;
582
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200583 if ((s->stream == SNDRV_PCM_STREAM_PLAYBACK) &&
584 (card->support_mmap)) {
585 /* How do I know how much valid data is present
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300586 * in buffer? Must be at least one period!
587 * Guessing 2 periods, but if
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200588 * buffer is bigger it may contain even more
589 * data??
590 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300591 unsigned int preload = ds->period_bytes * 1;
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300592 snd_printddd("%d preload x%x\n", s->number, preload);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200593 hpi_handle_error(hpi_outstream_write_buf(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300594 ds->h_stream,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300595 &runtime->dma_area[0],
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200596 preload,
597 &ds->format));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300598 ds->pcm_buf_host_rw_ofs = preload;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200599 }
600
601 if (card->support_grouping) {
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300602 snd_printdd("\t%c%d group\n",
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300603 SCHR(s->stream),
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200604 s->number);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300605 e = hpi_stream_group_add(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200606 dpcm->h_stream,
607 ds->h_stream);
608 if (!e) {
609 snd_pcm_trigger_done(s, substream);
610 } else {
611 hpi_handle_error(e);
612 break;
613 }
614 } else
615 break;
616 }
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300617 snd_printdd("start\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200618 /* start the master stream */
619 snd_card_asihpi_pcm_timer_start(substream);
Eliot Blennerhassettc4ed97d2011-02-10 17:26:20 +1300620 if ((substream->stream == SNDRV_PCM_STREAM_CAPTURE) ||
621 !card->support_mmap)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300622 hpi_handle_error(hpi_stream_start(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200623 break;
624
625 case SNDRV_PCM_TRIGGER_STOP:
626 snd_card_asihpi_pcm_timer_stop(substream);
627 snd_pcm_group_for_each_entry(s, substream) {
628 if (snd_pcm_substream_chip(s) != card)
629 continue;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300630 /* don't link Cap and Play */
631 if (substream->stream != s->stream)
632 continue;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200633
634 /*? workaround linked streams don't
635 transition to SETUP 20070706*/
636 s->runtime->status->state = SNDRV_PCM_STATE_SETUP;
637
638 if (card->support_grouping) {
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300639 snd_printdd("\t%c%d group\n",
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300640 SCHR(s->stream),
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200641 s->number);
642 snd_pcm_trigger_done(s, substream);
643 } else
644 break;
645 }
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300646 snd_printdd("stop\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200647
648 /* _prepare and _hwparams reset the stream */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300649 hpi_handle_error(hpi_stream_stop(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200650 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
651 hpi_handle_error(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300652 hpi_outstream_reset(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200653
654 if (card->support_grouping)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300655 hpi_handle_error(hpi_stream_group_reset(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200656 break;
657
658 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300659 snd_printdd("pause release\n");
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300660 hpi_handle_error(hpi_stream_start(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200661 snd_card_asihpi_pcm_timer_start(substream);
662 break;
663 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300664 snd_printdd("pause\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200665 snd_card_asihpi_pcm_timer_stop(substream);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300666 hpi_handle_error(hpi_stream_stop(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200667 break;
668 default:
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300669 snd_printd(KERN_ERR "\tINVALID\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200670 return -EINVAL;
671 }
672
673 return 0;
674}
675
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200676/*algorithm outline
677 Without linking degenerates to getting single stream pos etc
678 Without mmap 2nd loop degenerates to snd_pcm_period_elapsed
679*/
680/*
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300681pcm_buf_dma_ofs=get_buf_pos(s);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200682for_each_linked_stream(s) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300683 pcm_buf_dma_ofs=get_buf_pos(s);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300684 min_buf_pos = modulo_min(min_buf_pos, pcm_buf_dma_ofs, buffer_bytes)
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300685 new_data = min(new_data, calc_new_data(pcm_buf_dma_ofs,irq_pos)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200686}
687timer.expires = jiffies + predict_next_period_ready(min_buf_pos);
688for_each_linked_stream(s) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300689 s->pcm_buf_dma_ofs = min_buf_pos;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300690 if (new_data > period_bytes) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200691 if (mmap) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300692 irq_pos = (irq_pos + period_bytes) % buffer_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200693 if (playback) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300694 write(period_bytes);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200695 } else {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300696 read(period_bytes);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200697 }
698 }
699 snd_pcm_period_elapsed(s);
700 }
701}
702*/
703
704/** Minimum of 2 modulo values. Works correctly when the difference between
705* the values is less than half the modulus
706*/
707static inline unsigned int modulo_min(unsigned int a, unsigned int b,
708 unsigned long int modulus)
709{
710 unsigned int result;
711 if (((a-b) % modulus) < (modulus/2))
712 result = b;
713 else
714 result = a;
715
716 return result;
717}
718
719/** Timer function, equivalent to interrupt service routine for cards
720*/
721static void snd_card_asihpi_timer_function(unsigned long data)
722{
723 struct snd_card_asihpi_pcm *dpcm = (struct snd_card_asihpi_pcm *)data;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300724 struct snd_pcm_substream *substream = dpcm->substream;
725 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200726 struct snd_pcm_runtime *runtime;
727 struct snd_pcm_substream *s;
728 unsigned int newdata = 0;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300729 unsigned int pcm_buf_dma_ofs, min_buf_pos = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200730 unsigned int remdata, xfercount, next_jiffies;
731 int first = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300732 int loops = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200733 u16 state;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300734 u32 buffer_size, bytes_avail, samples_played, on_card_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200735
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300736 snd_printdd("%c%d snd_card_asihpi_timer_function\n",
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300737 SCHR(substream->stream), substream->number);
738
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200739 /* find minimum newdata and buffer pos in group */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300740 snd_pcm_group_for_each_entry(s, substream) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200741 struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
742 runtime = s->runtime;
743
744 if (snd_pcm_substream_chip(s) != card)
745 continue;
746
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300747 /* don't link Cap and Play */
748 if (substream->stream != s->stream)
749 continue;
750
751 hpi_handle_error(hpi_stream_get_info_ex(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200752 ds->h_stream, &state,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300753 &buffer_size, &bytes_avail,
754 &samples_played, &on_card_bytes));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200755
756 /* number of bytes in on-card buffer */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300757 runtime->delay = on_card_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200758
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300759 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300760 pcm_buf_dma_ofs = ds->pcm_buf_host_rw_ofs - bytes_avail;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300761 if (state == HPI_STATE_STOPPED) {
762 if ((bytes_avail == 0) &&
763 (on_card_bytes < ds->pcm_buf_host_rw_ofs)) {
764 hpi_handle_error(hpi_stream_start(ds->h_stream));
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300765 snd_printdd("P%d start\n", s->number);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300766 }
767 } else if (state == HPI_STATE_DRAINED) {
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300768 snd_printd(KERN_WARNING "P%d drained\n",
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300769 s->number);
770 /*snd_pcm_stop(s, SNDRV_PCM_STATE_XRUN);
771 continue; */
772 }
773 } else
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300774 pcm_buf_dma_ofs = bytes_avail + ds->pcm_buf_host_rw_ofs;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200775
776 if (first) {
777 /* can't statically init min when wrap is involved */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300778 min_buf_pos = pcm_buf_dma_ofs;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300779 newdata = (pcm_buf_dma_ofs - ds->pcm_buf_elapsed_dma_ofs) % ds->buffer_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200780 first = 0;
781 } else {
782 min_buf_pos =
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300783 modulo_min(min_buf_pos, pcm_buf_dma_ofs, UINT_MAX+1L);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200784 newdata = min(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300785 (pcm_buf_dma_ofs - ds->pcm_buf_elapsed_dma_ofs) % ds->buffer_bytes,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200786 newdata);
787 }
788
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300789 snd_printdd("hw_ptr x%04lX, appl_ptr x%04lX\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200790 (unsigned long)frames_to_bytes(runtime,
791 runtime->status->hw_ptr),
792 (unsigned long)frames_to_bytes(runtime,
793 runtime->control->appl_ptr));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300794
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300795 snd_printdd("%d %c%d S=%d, rw=%04X, dma=x%04X, left=x%04X,"
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300796 " aux=x%04X space=x%04X\n",
797 loops, SCHR(s->stream), s->number,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300798 state, ds->pcm_buf_host_rw_ofs, pcm_buf_dma_ofs, (int)bytes_avail,
799 (int)on_card_bytes, buffer_size-bytes_avail);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300800 loops++;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200801 }
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300802 pcm_buf_dma_ofs = min_buf_pos;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200803
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300804 remdata = newdata % dpcm->period_bytes;
805 xfercount = newdata - remdata; /* a multiple of period_bytes */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300806 /* come back when on_card_bytes has decreased enough to allow
807 write to happen, or when data has been consumed to make another
808 period
809 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300810 if (xfercount && (on_card_bytes > dpcm->period_bytes))
811 next_jiffies = ((on_card_bytes - dpcm->period_bytes) * HZ / dpcm->bytes_per_sec);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300812 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300813 next_jiffies = ((dpcm->period_bytes - remdata) * HZ / dpcm->bytes_per_sec);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300814
815 next_jiffies = max(next_jiffies, 1U);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200816 dpcm->timer.expires = jiffies + next_jiffies;
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300817 snd_printdd("jif %d buf pos x%04X newdata x%04X xfer x%04X\n",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300818 next_jiffies, pcm_buf_dma_ofs, newdata, xfercount);
819
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300820 snd_pcm_group_for_each_entry(s, substream) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200821 struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200822
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300823 /* don't link Cap and Play */
824 if (substream->stream != s->stream)
825 continue;
826
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300827 ds->pcm_buf_dma_ofs = pcm_buf_dma_ofs;
828
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300829 if (xfercount && (on_card_bytes <= ds->period_bytes)) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200830 if (card->support_mmap) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200831 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300832 snd_printddd("P%d write x%04x\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200833 s->number,
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300834 ds->period_bytes);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200835 hpi_handle_error(
836 hpi_outstream_write_buf(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300837 ds->h_stream,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200838 &s->runtime->
839 dma_area[0],
840 xfercount,
841 &ds->format));
842 } else {
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300843 snd_printddd("C%d read x%04x\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200844 s->number,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300845 xfercount);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200846 hpi_handle_error(
847 hpi_instream_read_buf(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300848 ds->h_stream,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200849 NULL, xfercount));
850 }
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300851 ds->pcm_buf_host_rw_ofs = ds->pcm_buf_host_rw_ofs + xfercount;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200852 } /* else R/W will be handled by read/write callbacks */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300853 ds->pcm_buf_elapsed_dma_ofs = pcm_buf_dma_ofs;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200854 snd_pcm_period_elapsed(s);
855 }
856 }
857
858 if (dpcm->respawn_timer)
859 add_timer(&dpcm->timer);
860}
861
862/***************************** PLAYBACK OPS ****************/
863static int snd_card_asihpi_playback_ioctl(struct snd_pcm_substream *substream,
864 unsigned int cmd, void *arg)
865{
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300866 snd_printdd(KERN_INFO "Playback ioctl %d\n", cmd);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200867 return snd_pcm_lib_ioctl(substream, cmd, arg);
868}
869
870static int snd_card_asihpi_playback_prepare(struct snd_pcm_substream *
871 substream)
872{
873 struct snd_pcm_runtime *runtime = substream->runtime;
874 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
875
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300876 snd_printdd("playback prepare %d\n", substream->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200877
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300878 hpi_handle_error(hpi_outstream_reset(dpcm->h_stream));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300879 dpcm->pcm_buf_host_rw_ofs = 0;
880 dpcm->pcm_buf_dma_ofs = 0;
881 dpcm->pcm_buf_elapsed_dma_ofs = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200882 return 0;
883}
884
885static snd_pcm_uframes_t
886snd_card_asihpi_playback_pointer(struct snd_pcm_substream *substream)
887{
888 struct snd_pcm_runtime *runtime = substream->runtime;
889 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
890 snd_pcm_uframes_t ptr;
891
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300892 ptr = bytes_to_frames(runtime, dpcm->pcm_buf_dma_ofs % dpcm->buffer_bytes);
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300893 snd_printddd("playback_pointer=x%04lx\n", (unsigned long)ptr);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200894 return ptr;
895}
896
897static void snd_card_asihpi_playback_format(struct snd_card_asihpi *asihpi,
898 u32 h_stream,
899 struct snd_pcm_hardware *pcmhw)
900{
901 struct hpi_format hpi_format;
902 u16 format;
903 u16 err;
904 u32 h_control;
905 u32 sample_rate = 48000;
906
907 /* on cards without SRC, must query at valid rate,
908 * maybe set by external sync
909 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300910 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200911 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
912 HPI_CONTROL_SAMPLECLOCK, &h_control);
913
914 if (!err)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300915 err = hpi_sample_clock_get_sample_rate(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200916 &sample_rate);
917
918 for (format = HPI_FORMAT_PCM8_UNSIGNED;
919 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
920 err = hpi_format_create(&hpi_format,
921 2, format, sample_rate, 128000, 0);
922 if (!err)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300923 err = hpi_outstream_query_format(h_stream,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200924 &hpi_format);
925 if (!err && (hpi_to_alsa_formats[format] != -1))
926 pcmhw->formats |=
927 (1ULL << hpi_to_alsa_formats[format]);
928 }
929}
930
931static struct snd_pcm_hardware snd_card_asihpi_playback = {
932 .channels_min = 1,
933 .channels_max = 2,
934 .buffer_bytes_max = BUFFER_BYTES_MAX,
935 .period_bytes_min = PERIOD_BYTES_MIN,
936 .period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN,
937 .periods_min = PERIODS_MIN,
938 .periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN,
939 .fifo_size = 0,
940};
941
942static int snd_card_asihpi_playback_open(struct snd_pcm_substream *substream)
943{
944 struct snd_pcm_runtime *runtime = substream->runtime;
945 struct snd_card_asihpi_pcm *dpcm;
946 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
947 int err;
948
949 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
950 if (dpcm == NULL)
951 return -ENOMEM;
952
953 err =
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300954 hpi_outstream_open(card->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200955 substream->number, &dpcm->h_stream);
956 hpi_handle_error(err);
957 if (err)
958 kfree(dpcm);
959 if (err == HPI_ERROR_OBJ_ALREADY_OPEN)
960 return -EBUSY;
961 if (err)
962 return -EIO;
963
964 /*? also check ASI5000 samplerate source
965 If external, only support external rate.
966 If internal and other stream playing, cant switch
967 */
968
969 init_timer(&dpcm->timer);
970 dpcm->timer.data = (unsigned long) dpcm;
971 dpcm->timer.function = snd_card_asihpi_timer_function;
972 dpcm->substream = substream;
973 runtime->private_data = dpcm;
974 runtime->private_free = snd_card_asihpi_runtime_free;
975
976 snd_card_asihpi_playback.channels_max = card->out_max_chans;
977 /*?snd_card_asihpi_playback.period_bytes_min =
978 card->out_max_chans * 4096; */
979
980 snd_card_asihpi_playback_format(card, dpcm->h_stream,
981 &snd_card_asihpi_playback);
982
983 snd_card_asihpi_pcm_samplerates(card, &snd_card_asihpi_playback);
984
985 snd_card_asihpi_playback.info = SNDRV_PCM_INFO_INTERLEAVED |
986 SNDRV_PCM_INFO_DOUBLE |
987 SNDRV_PCM_INFO_BATCH |
988 SNDRV_PCM_INFO_BLOCK_TRANSFER |
989 SNDRV_PCM_INFO_PAUSE;
990
991 if (card->support_mmap)
992 snd_card_asihpi_playback.info |= SNDRV_PCM_INFO_MMAP |
993 SNDRV_PCM_INFO_MMAP_VALID;
994
995 if (card->support_grouping)
996 snd_card_asihpi_playback.info |= SNDRV_PCM_INFO_SYNC_START;
997
998 /* struct is copied, so can create initializer dynamically */
999 runtime->hw = snd_card_asihpi_playback;
1000
1001 if (card->support_mmap)
1002 err = snd_pcm_hw_constraint_pow2(runtime, 0,
1003 SNDRV_PCM_HW_PARAM_BUFFER_BYTES);
1004 if (err < 0)
1005 return err;
1006
1007 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1008 card->update_interval_frames);
Eliot Blennerhassett26aebef2011-03-25 15:25:47 +13001009
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001010 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001011 card->update_interval_frames * 2, UINT_MAX);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001012
1013 snd_pcm_set_sync(substream);
1014
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001015 snd_printdd("playback open\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001016
1017 return 0;
1018}
1019
1020static int snd_card_asihpi_playback_close(struct snd_pcm_substream *substream)
1021{
1022 struct snd_pcm_runtime *runtime = substream->runtime;
1023 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1024
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001025 hpi_handle_error(hpi_outstream_close(dpcm->h_stream));
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001026 snd_printdd("playback close\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001027
1028 return 0;
1029}
1030
1031static int snd_card_asihpi_playback_copy(struct snd_pcm_substream *substream,
1032 int channel,
1033 snd_pcm_uframes_t pos,
1034 void __user *src,
1035 snd_pcm_uframes_t count)
1036{
1037 struct snd_pcm_runtime *runtime = substream->runtime;
1038 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1039 unsigned int len;
1040
1041 len = frames_to_bytes(runtime, count);
1042
1043 if (copy_from_user(runtime->dma_area, src, len))
1044 return -EFAULT;
1045
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001046 snd_printddd("playback copy%d %u bytes\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001047 substream->number, len);
1048
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001049 hpi_handle_error(hpi_outstream_write_buf(dpcm->h_stream,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001050 runtime->dma_area, len, &dpcm->format));
1051
Eliot Blennerhassett26aebef2011-03-25 15:25:47 +13001052 dpcm->pcm_buf_host_rw_ofs += len;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001053
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001054 return 0;
1055}
1056
1057static int snd_card_asihpi_playback_silence(struct snd_pcm_substream *
1058 substream, int channel,
1059 snd_pcm_uframes_t pos,
1060 snd_pcm_uframes_t count)
1061{
Eliot Blennerhassett26aebef2011-03-25 15:25:47 +13001062 /* Usually writes silence to DMA buffer, which should be overwritten
1063 by real audio later. Our fifos cannot be overwritten, and are not
1064 free-running DMAs. Silence is output on fifo underflow.
1065 This callback is still required to allow the copy callback to be used.
1066 */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001067 return 0;
1068}
1069
1070static struct snd_pcm_ops snd_card_asihpi_playback_ops = {
1071 .open = snd_card_asihpi_playback_open,
1072 .close = snd_card_asihpi_playback_close,
1073 .ioctl = snd_card_asihpi_playback_ioctl,
1074 .hw_params = snd_card_asihpi_pcm_hw_params,
1075 .hw_free = snd_card_asihpi_hw_free,
1076 .prepare = snd_card_asihpi_playback_prepare,
1077 .trigger = snd_card_asihpi_trigger,
1078 .pointer = snd_card_asihpi_playback_pointer,
1079 .copy = snd_card_asihpi_playback_copy,
1080 .silence = snd_card_asihpi_playback_silence,
1081};
1082
1083static struct snd_pcm_ops snd_card_asihpi_playback_mmap_ops = {
1084 .open = snd_card_asihpi_playback_open,
1085 .close = snd_card_asihpi_playback_close,
1086 .ioctl = snd_card_asihpi_playback_ioctl,
1087 .hw_params = snd_card_asihpi_pcm_hw_params,
1088 .hw_free = snd_card_asihpi_hw_free,
1089 .prepare = snd_card_asihpi_playback_prepare,
1090 .trigger = snd_card_asihpi_trigger,
1091 .pointer = snd_card_asihpi_playback_pointer,
1092};
1093
1094/***************************** CAPTURE OPS ****************/
1095static snd_pcm_uframes_t
1096snd_card_asihpi_capture_pointer(struct snd_pcm_substream *substream)
1097{
1098 struct snd_pcm_runtime *runtime = substream->runtime;
1099 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1100
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001101 snd_printddd("capture pointer %d=%d\n",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001102 substream->number, dpcm->pcm_buf_dma_ofs);
1103 /* NOTE Unlike playback can't use actual samples_played
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001104 for the capture position, because those samples aren't yet in
1105 the local buffer available for reading.
1106 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001107 return bytes_to_frames(runtime, dpcm->pcm_buf_dma_ofs % dpcm->buffer_bytes);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001108}
1109
1110static int snd_card_asihpi_capture_ioctl(struct snd_pcm_substream *substream,
1111 unsigned int cmd, void *arg)
1112{
1113 return snd_pcm_lib_ioctl(substream, cmd, arg);
1114}
1115
1116static int snd_card_asihpi_capture_prepare(struct snd_pcm_substream *substream)
1117{
1118 struct snd_pcm_runtime *runtime = substream->runtime;
1119 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1120
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001121 hpi_handle_error(hpi_instream_reset(dpcm->h_stream));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001122 dpcm->pcm_buf_host_rw_ofs = 0;
1123 dpcm->pcm_buf_dma_ofs = 0;
1124 dpcm->pcm_buf_elapsed_dma_ofs = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001125
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001126 snd_printdd("Capture Prepare %d\n", substream->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001127 return 0;
1128}
1129
1130
1131
1132static void snd_card_asihpi_capture_format(struct snd_card_asihpi *asihpi,
1133 u32 h_stream,
1134 struct snd_pcm_hardware *pcmhw)
1135{
1136 struct hpi_format hpi_format;
1137 u16 format;
1138 u16 err;
1139 u32 h_control;
1140 u32 sample_rate = 48000;
1141
1142 /* on cards without SRC, must query at valid rate,
1143 maybe set by external sync */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001144 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001145 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
1146 HPI_CONTROL_SAMPLECLOCK, &h_control);
1147
1148 if (!err)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001149 err = hpi_sample_clock_get_sample_rate(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001150 &sample_rate);
1151
1152 for (format = HPI_FORMAT_PCM8_UNSIGNED;
1153 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
1154
1155 err = hpi_format_create(&hpi_format, 2, format,
1156 sample_rate, 128000, 0);
1157 if (!err)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001158 err = hpi_instream_query_format(h_stream,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001159 &hpi_format);
1160 if (!err)
1161 pcmhw->formats |=
1162 (1ULL << hpi_to_alsa_formats[format]);
1163 }
1164}
1165
1166
1167static struct snd_pcm_hardware snd_card_asihpi_capture = {
1168 .channels_min = 1,
1169 .channels_max = 2,
1170 .buffer_bytes_max = BUFFER_BYTES_MAX,
1171 .period_bytes_min = PERIOD_BYTES_MIN,
1172 .period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN,
1173 .periods_min = PERIODS_MIN,
1174 .periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN,
1175 .fifo_size = 0,
1176};
1177
1178static int snd_card_asihpi_capture_open(struct snd_pcm_substream *substream)
1179{
1180 struct snd_pcm_runtime *runtime = substream->runtime;
1181 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
1182 struct snd_card_asihpi_pcm *dpcm;
1183 int err;
1184
1185 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
1186 if (dpcm == NULL)
1187 return -ENOMEM;
1188
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001189 snd_printdd("capture open adapter %d stream %d\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001190 card->adapter_index, substream->number);
1191
1192 err = hpi_handle_error(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001193 hpi_instream_open(card->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001194 substream->number, &dpcm->h_stream));
1195 if (err)
1196 kfree(dpcm);
1197 if (err == HPI_ERROR_OBJ_ALREADY_OPEN)
1198 return -EBUSY;
1199 if (err)
1200 return -EIO;
1201
1202
1203 init_timer(&dpcm->timer);
1204 dpcm->timer.data = (unsigned long) dpcm;
1205 dpcm->timer.function = snd_card_asihpi_timer_function;
1206 dpcm->substream = substream;
1207 runtime->private_data = dpcm;
1208 runtime->private_free = snd_card_asihpi_runtime_free;
1209
1210 snd_card_asihpi_capture.channels_max = card->in_max_chans;
1211 snd_card_asihpi_capture_format(card, dpcm->h_stream,
1212 &snd_card_asihpi_capture);
1213 snd_card_asihpi_pcm_samplerates(card, &snd_card_asihpi_capture);
1214 snd_card_asihpi_capture.info = SNDRV_PCM_INFO_INTERLEAVED;
1215
1216 if (card->support_mmap)
1217 snd_card_asihpi_capture.info |= SNDRV_PCM_INFO_MMAP |
1218 SNDRV_PCM_INFO_MMAP_VALID;
1219
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001220 if (card->support_grouping)
1221 snd_card_asihpi_capture.info |= SNDRV_PCM_INFO_SYNC_START;
1222
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001223 runtime->hw = snd_card_asihpi_capture;
1224
1225 if (card->support_mmap)
1226 err = snd_pcm_hw_constraint_pow2(runtime, 0,
1227 SNDRV_PCM_HW_PARAM_BUFFER_BYTES);
1228 if (err < 0)
1229 return err;
1230
1231 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1232 card->update_interval_frames);
1233 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1234 card->update_interval_frames * 2, UINT_MAX);
1235
1236 snd_pcm_set_sync(substream);
1237
1238 return 0;
1239}
1240
1241static int snd_card_asihpi_capture_close(struct snd_pcm_substream *substream)
1242{
1243 struct snd_card_asihpi_pcm *dpcm = substream->runtime->private_data;
1244
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001245 hpi_handle_error(hpi_instream_close(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001246 return 0;
1247}
1248
1249static int snd_card_asihpi_capture_copy(struct snd_pcm_substream *substream,
1250 int channel, snd_pcm_uframes_t pos,
1251 void __user *dst, snd_pcm_uframes_t count)
1252{
1253 struct snd_pcm_runtime *runtime = substream->runtime;
1254 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001255 u32 len;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001256
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001257 len = frames_to_bytes(runtime, count);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001258
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001259 snd_printddd("capture copy%d %d bytes\n", substream->number, len);
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001260 hpi_handle_error(hpi_instream_read_buf(dpcm->h_stream,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001261 runtime->dma_area, len));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001262
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001263 dpcm->pcm_buf_host_rw_ofs = dpcm->pcm_buf_host_rw_ofs + len;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001264
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001265 if (copy_to_user(dst, runtime->dma_area, len))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001266 return -EFAULT;
1267
1268 return 0;
1269}
1270
1271static struct snd_pcm_ops snd_card_asihpi_capture_mmap_ops = {
1272 .open = snd_card_asihpi_capture_open,
1273 .close = snd_card_asihpi_capture_close,
1274 .ioctl = snd_card_asihpi_capture_ioctl,
1275 .hw_params = snd_card_asihpi_pcm_hw_params,
1276 .hw_free = snd_card_asihpi_hw_free,
1277 .prepare = snd_card_asihpi_capture_prepare,
1278 .trigger = snd_card_asihpi_trigger,
1279 .pointer = snd_card_asihpi_capture_pointer,
1280};
1281
1282static struct snd_pcm_ops snd_card_asihpi_capture_ops = {
1283 .open = snd_card_asihpi_capture_open,
1284 .close = snd_card_asihpi_capture_close,
1285 .ioctl = snd_card_asihpi_capture_ioctl,
1286 .hw_params = snd_card_asihpi_pcm_hw_params,
1287 .hw_free = snd_card_asihpi_hw_free,
1288 .prepare = snd_card_asihpi_capture_prepare,
1289 .trigger = snd_card_asihpi_trigger,
1290 .pointer = snd_card_asihpi_capture_pointer,
1291 .copy = snd_card_asihpi_capture_copy
1292};
1293
1294static int __devinit snd_card_asihpi_pcm_new(struct snd_card_asihpi *asihpi,
1295 int device, int substreams)
1296{
1297 struct snd_pcm *pcm;
1298 int err;
1299
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001300 err = snd_pcm_new(asihpi->card, "Asihpi PCM", device,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001301 asihpi->num_outstreams, asihpi->num_instreams,
1302 &pcm);
1303 if (err < 0)
1304 return err;
1305 /* pointer to ops struct is stored, dont change ops afterwards! */
1306 if (asihpi->support_mmap) {
1307 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1308 &snd_card_asihpi_playback_mmap_ops);
1309 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1310 &snd_card_asihpi_capture_mmap_ops);
1311 } else {
1312 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1313 &snd_card_asihpi_playback_ops);
1314 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1315 &snd_card_asihpi_capture_ops);
1316 }
1317
1318 pcm->private_data = asihpi;
1319 pcm->info_flags = 0;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001320 strcpy(pcm->name, "Asihpi PCM");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001321
1322 /*? do we want to emulate MMAP for non-BBM cards?
1323 Jack doesn't work with ALSAs MMAP emulation - WHY NOT? */
1324 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1325 snd_dma_pci_data(asihpi->pci),
1326 64*1024, BUFFER_BYTES_MAX);
1327
1328 return 0;
1329}
1330
1331/***************************** MIXER CONTROLS ****************/
1332struct hpi_control {
1333 u32 h_control;
1334 u16 control_type;
1335 u16 src_node_type;
1336 u16 src_node_index;
1337 u16 dst_node_type;
1338 u16 dst_node_index;
1339 u16 band;
1340 char name[44]; /* copied to snd_ctl_elem_id.name[44]; */
1341};
1342
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001343static const char * const asihpi_tuner_band_names[] = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001344 "invalid",
1345 "AM",
1346 "FM mono",
1347 "TV NTSC-M",
1348 "FM stereo",
1349 "AUX",
1350 "TV PAL BG",
1351 "TV PAL I",
1352 "TV PAL DK",
1353 "TV SECAM",
1354};
1355
1356compile_time_assert(
1357 (ARRAY_SIZE(asihpi_tuner_band_names) ==
1358 (HPI_TUNER_BAND_LAST+1)),
1359 assert_tuner_band_names_size);
1360
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001361static const char * const asihpi_src_names[] = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001362 "no source",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001363 "PCM",
1364 "Line",
1365 "Digital",
1366 "Tuner",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001367 "RF",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001368 "Clock",
1369 "Bitstream",
1370 "Microphone",
1371 "Cobranet",
1372 "Analog",
1373 "Adapter",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001374};
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001375
1376compile_time_assert(
1377 (ARRAY_SIZE(asihpi_src_names) ==
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12001378 (HPI_SOURCENODE_LAST_INDEX-HPI_SOURCENODE_NONE+1)),
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001379 assert_src_names_size);
1380
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001381static const char * const asihpi_dst_names[] = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001382 "no destination",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001383 "PCM",
1384 "Line",
1385 "Digital",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001386 "RF",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001387 "Speaker",
1388 "Cobranet Out",
1389 "Analog"
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001390};
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001391
1392compile_time_assert(
1393 (ARRAY_SIZE(asihpi_dst_names) ==
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12001394 (HPI_DESTNODE_LAST_INDEX-HPI_DESTNODE_NONE+1)),
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001395 assert_dst_names_size);
1396
1397static inline int ctl_add(struct snd_card *card, struct snd_kcontrol_new *ctl,
1398 struct snd_card_asihpi *asihpi)
1399{
1400 int err;
1401
1402 err = snd_ctl_add(card, snd_ctl_new1(ctl, asihpi));
1403 if (err < 0)
1404 return err;
1405 else if (mixer_dump)
1406 snd_printk(KERN_INFO "added %s(%d)\n", ctl->name, ctl->index);
1407
1408 return 0;
1409}
1410
1411/* Convert HPI control name and location into ALSA control name */
1412static void asihpi_ctl_init(struct snd_kcontrol_new *snd_control,
1413 struct hpi_control *hpi_ctl,
1414 char *name)
1415{
Eliot Blennerhassett550ac6b2011-04-05 20:55:41 +12001416 char *dir;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001417 memset(snd_control, 0, sizeof(*snd_control));
1418 snd_control->name = hpi_ctl->name;
1419 snd_control->private_value = hpi_ctl->h_control;
1420 snd_control->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1421 snd_control->index = 0;
1422
Eliot Blennerhassett550ac6b2011-04-05 20:55:41 +12001423 if (hpi_ctl->src_node_type + HPI_SOURCENODE_NONE == HPI_SOURCENODE_CLOCK_SOURCE)
1424 dir = ""; /* clock is neither capture nor playback */
1425 else if (hpi_ctl->dst_node_type + HPI_DESTNODE_NONE == HPI_DESTNODE_ISTREAM)
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001426 dir = "Capture "; /* On or towards a PCM capture destination*/
1427 else if ((hpi_ctl->src_node_type + HPI_SOURCENODE_NONE != HPI_SOURCENODE_OSTREAM) &&
1428 (!hpi_ctl->dst_node_type))
1429 dir = "Capture "; /* On a source node that is not PCM playback */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001430 else if (hpi_ctl->src_node_type &&
1431 (hpi_ctl->src_node_type + HPI_SOURCENODE_NONE != HPI_SOURCENODE_OSTREAM) &&
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001432 (hpi_ctl->dst_node_type))
1433 dir = "Monitor Playback "; /* Between an input and an output */
1434 else
1435 dir = "Playback "; /* PCM Playback source, or output node */
1436
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001437 if (hpi_ctl->src_node_type && hpi_ctl->dst_node_type)
Eliot Blennerhassett550ac6b2011-04-05 20:55:41 +12001438 sprintf(hpi_ctl->name, "%s %d %s %d %s%s",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001439 asihpi_src_names[hpi_ctl->src_node_type],
1440 hpi_ctl->src_node_index,
1441 asihpi_dst_names[hpi_ctl->dst_node_type],
1442 hpi_ctl->dst_node_index,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001443 dir, name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001444 else if (hpi_ctl->dst_node_type) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001445 sprintf(hpi_ctl->name, "%s %d %s%s",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001446 asihpi_dst_names[hpi_ctl->dst_node_type],
1447 hpi_ctl->dst_node_index,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001448 dir, name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001449 } else {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001450 sprintf(hpi_ctl->name, "%s %d %s%s",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001451 asihpi_src_names[hpi_ctl->src_node_type],
1452 hpi_ctl->src_node_index,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001453 dir, name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001454 }
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001455 /* printk(KERN_INFO "Adding %s %d to %d ", hpi_ctl->name,
1456 hpi_ctl->wSrcNodeType, hpi_ctl->wDstNodeType); */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001457}
1458
1459/*------------------------------------------------------------
1460 Volume controls
1461 ------------------------------------------------------------*/
1462#define VOL_STEP_mB 1
1463static int snd_asihpi_volume_info(struct snd_kcontrol *kcontrol,
1464 struct snd_ctl_elem_info *uinfo)
1465{
1466 u32 h_control = kcontrol->private_value;
1467 u16 err;
1468 /* native gains are in millibels */
1469 short min_gain_mB;
1470 short max_gain_mB;
1471 short step_gain_mB;
1472
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001473 err = hpi_volume_query_range(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001474 &min_gain_mB, &max_gain_mB, &step_gain_mB);
1475 if (err) {
1476 max_gain_mB = 0;
1477 min_gain_mB = -10000;
1478 step_gain_mB = VOL_STEP_mB;
1479 }
1480
1481 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1482 uinfo->count = 2;
1483 uinfo->value.integer.min = min_gain_mB / VOL_STEP_mB;
1484 uinfo->value.integer.max = max_gain_mB / VOL_STEP_mB;
1485 uinfo->value.integer.step = step_gain_mB / VOL_STEP_mB;
1486 return 0;
1487}
1488
1489static int snd_asihpi_volume_get(struct snd_kcontrol *kcontrol,
1490 struct snd_ctl_elem_value *ucontrol)
1491{
1492 u32 h_control = kcontrol->private_value;
1493 short an_gain_mB[HPI_MAX_CHANNELS];
1494
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001495 hpi_handle_error(hpi_volume_get_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001496 ucontrol->value.integer.value[0] = an_gain_mB[0] / VOL_STEP_mB;
1497 ucontrol->value.integer.value[1] = an_gain_mB[1] / VOL_STEP_mB;
1498
1499 return 0;
1500}
1501
1502static int snd_asihpi_volume_put(struct snd_kcontrol *kcontrol,
1503 struct snd_ctl_elem_value *ucontrol)
1504{
1505 int change;
1506 u32 h_control = kcontrol->private_value;
1507 short an_gain_mB[HPI_MAX_CHANNELS];
1508
1509 an_gain_mB[0] =
1510 (ucontrol->value.integer.value[0]) * VOL_STEP_mB;
1511 an_gain_mB[1] =
1512 (ucontrol->value.integer.value[1]) * VOL_STEP_mB;
1513 /* change = asihpi->mixer_volume[addr][0] != left ||
1514 asihpi->mixer_volume[addr][1] != right;
1515 */
1516 change = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001517 hpi_handle_error(hpi_volume_set_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001518 return change;
1519}
1520
1521static const DECLARE_TLV_DB_SCALE(db_scale_100, -10000, VOL_STEP_mB, 0);
1522
1523static int __devinit snd_asihpi_volume_add(struct snd_card_asihpi *asihpi,
1524 struct hpi_control *hpi_ctl)
1525{
1526 struct snd_card *card = asihpi->card;
1527 struct snd_kcontrol_new snd_control;
1528
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001529 asihpi_ctl_init(&snd_control, hpi_ctl, "Volume");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001530 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1531 SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1532 snd_control.info = snd_asihpi_volume_info;
1533 snd_control.get = snd_asihpi_volume_get;
1534 snd_control.put = snd_asihpi_volume_put;
1535 snd_control.tlv.p = db_scale_100;
1536
1537 return ctl_add(card, &snd_control, asihpi);
1538}
1539
1540/*------------------------------------------------------------
1541 Level controls
1542 ------------------------------------------------------------*/
1543static int snd_asihpi_level_info(struct snd_kcontrol *kcontrol,
1544 struct snd_ctl_elem_info *uinfo)
1545{
1546 u32 h_control = kcontrol->private_value;
1547 u16 err;
1548 short min_gain_mB;
1549 short max_gain_mB;
1550 short step_gain_mB;
1551
1552 err =
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001553 hpi_level_query_range(h_control, &min_gain_mB,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001554 &max_gain_mB, &step_gain_mB);
1555 if (err) {
1556 max_gain_mB = 2400;
1557 min_gain_mB = -1000;
1558 step_gain_mB = 100;
1559 }
1560
1561 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1562 uinfo->count = 2;
1563 uinfo->value.integer.min = min_gain_mB / HPI_UNITS_PER_dB;
1564 uinfo->value.integer.max = max_gain_mB / HPI_UNITS_PER_dB;
1565 uinfo->value.integer.step = step_gain_mB / HPI_UNITS_PER_dB;
1566 return 0;
1567}
1568
1569static int snd_asihpi_level_get(struct snd_kcontrol *kcontrol,
1570 struct snd_ctl_elem_value *ucontrol)
1571{
1572 u32 h_control = kcontrol->private_value;
1573 short an_gain_mB[HPI_MAX_CHANNELS];
1574
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001575 hpi_handle_error(hpi_level_get_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001576 ucontrol->value.integer.value[0] =
1577 an_gain_mB[0] / HPI_UNITS_PER_dB;
1578 ucontrol->value.integer.value[1] =
1579 an_gain_mB[1] / HPI_UNITS_PER_dB;
1580
1581 return 0;
1582}
1583
1584static int snd_asihpi_level_put(struct snd_kcontrol *kcontrol,
1585 struct snd_ctl_elem_value *ucontrol)
1586{
1587 int change;
1588 u32 h_control = kcontrol->private_value;
1589 short an_gain_mB[HPI_MAX_CHANNELS];
1590
1591 an_gain_mB[0] =
1592 (ucontrol->value.integer.value[0]) * HPI_UNITS_PER_dB;
1593 an_gain_mB[1] =
1594 (ucontrol->value.integer.value[1]) * HPI_UNITS_PER_dB;
1595 /* change = asihpi->mixer_level[addr][0] != left ||
1596 asihpi->mixer_level[addr][1] != right;
1597 */
1598 change = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001599 hpi_handle_error(hpi_level_set_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001600 return change;
1601}
1602
1603static const DECLARE_TLV_DB_SCALE(db_scale_level, -1000, 100, 0);
1604
1605static int __devinit snd_asihpi_level_add(struct snd_card_asihpi *asihpi,
1606 struct hpi_control *hpi_ctl)
1607{
1608 struct snd_card *card = asihpi->card;
1609 struct snd_kcontrol_new snd_control;
1610
1611 /* can't use 'volume' cos some nodes have volume as well */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001612 asihpi_ctl_init(&snd_control, hpi_ctl, "Level");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001613 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1614 SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1615 snd_control.info = snd_asihpi_level_info;
1616 snd_control.get = snd_asihpi_level_get;
1617 snd_control.put = snd_asihpi_level_put;
1618 snd_control.tlv.p = db_scale_level;
1619
1620 return ctl_add(card, &snd_control, asihpi);
1621}
1622
1623/*------------------------------------------------------------
1624 AESEBU controls
1625 ------------------------------------------------------------*/
1626
1627/* AESEBU format */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001628static const char * const asihpi_aesebu_format_names[] = {
1629 "N/A", "S/PDIF", "AES/EBU" };
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001630
1631static int snd_asihpi_aesebu_format_info(struct snd_kcontrol *kcontrol,
1632 struct snd_ctl_elem_info *uinfo)
1633{
1634 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1635 uinfo->count = 1;
1636 uinfo->value.enumerated.items = 3;
1637
1638 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1639 uinfo->value.enumerated.item =
1640 uinfo->value.enumerated.items - 1;
1641
1642 strcpy(uinfo->value.enumerated.name,
1643 asihpi_aesebu_format_names[uinfo->value.enumerated.item]);
1644
1645 return 0;
1646}
1647
1648static int snd_asihpi_aesebu_format_get(struct snd_kcontrol *kcontrol,
1649 struct snd_ctl_elem_value *ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001650 u16 (*func)(u32, u16 *))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001651{
1652 u32 h_control = kcontrol->private_value;
1653 u16 source, err;
1654
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001655 err = func(h_control, &source);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001656
1657 /* default to N/A */
1658 ucontrol->value.enumerated.item[0] = 0;
1659 /* return success but set the control to N/A */
1660 if (err)
1661 return 0;
1662 if (source == HPI_AESEBU_FORMAT_SPDIF)
1663 ucontrol->value.enumerated.item[0] = 1;
1664 if (source == HPI_AESEBU_FORMAT_AESEBU)
1665 ucontrol->value.enumerated.item[0] = 2;
1666
1667 return 0;
1668}
1669
1670static int snd_asihpi_aesebu_format_put(struct snd_kcontrol *kcontrol,
1671 struct snd_ctl_elem_value *ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001672 u16 (*func)(u32, u16))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001673{
1674 u32 h_control = kcontrol->private_value;
1675
1676 /* default to S/PDIF */
1677 u16 source = HPI_AESEBU_FORMAT_SPDIF;
1678
1679 if (ucontrol->value.enumerated.item[0] == 1)
1680 source = HPI_AESEBU_FORMAT_SPDIF;
1681 if (ucontrol->value.enumerated.item[0] == 2)
1682 source = HPI_AESEBU_FORMAT_AESEBU;
1683
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001684 if (func(h_control, source) != 0)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001685 return -EINVAL;
1686
1687 return 1;
1688}
1689
1690static int snd_asihpi_aesebu_rx_format_get(struct snd_kcontrol *kcontrol,
1691 struct snd_ctl_elem_value *ucontrol) {
1692 return snd_asihpi_aesebu_format_get(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001693 hpi_aesebu_receiver_get_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001694}
1695
1696static int snd_asihpi_aesebu_rx_format_put(struct snd_kcontrol *kcontrol,
1697 struct snd_ctl_elem_value *ucontrol) {
1698 return snd_asihpi_aesebu_format_put(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001699 hpi_aesebu_receiver_set_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001700}
1701
1702static int snd_asihpi_aesebu_rxstatus_info(struct snd_kcontrol *kcontrol,
1703 struct snd_ctl_elem_info *uinfo)
1704{
1705 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1706 uinfo->count = 1;
1707
1708 uinfo->value.integer.min = 0;
1709 uinfo->value.integer.max = 0X1F;
1710 uinfo->value.integer.step = 1;
1711
1712 return 0;
1713}
1714
1715static int snd_asihpi_aesebu_rxstatus_get(struct snd_kcontrol *kcontrol,
1716 struct snd_ctl_elem_value *ucontrol) {
1717
1718 u32 h_control = kcontrol->private_value;
1719 u16 status;
1720
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001721 hpi_handle_error(hpi_aesebu_receiver_get_error_status(
1722 h_control, &status));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001723 ucontrol->value.integer.value[0] = status;
1724 return 0;
1725}
1726
1727static int __devinit snd_asihpi_aesebu_rx_add(struct snd_card_asihpi *asihpi,
1728 struct hpi_control *hpi_ctl)
1729{
1730 struct snd_card *card = asihpi->card;
1731 struct snd_kcontrol_new snd_control;
1732
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001733 asihpi_ctl_init(&snd_control, hpi_ctl, "Format");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001734 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1735 snd_control.info = snd_asihpi_aesebu_format_info;
1736 snd_control.get = snd_asihpi_aesebu_rx_format_get;
1737 snd_control.put = snd_asihpi_aesebu_rx_format_put;
1738
1739
1740 if (ctl_add(card, &snd_control, asihpi) < 0)
1741 return -EINVAL;
1742
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001743 asihpi_ctl_init(&snd_control, hpi_ctl, "Status");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001744 snd_control.access =
1745 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
1746 snd_control.info = snd_asihpi_aesebu_rxstatus_info;
1747 snd_control.get = snd_asihpi_aesebu_rxstatus_get;
1748
1749 return ctl_add(card, &snd_control, asihpi);
1750}
1751
1752static int snd_asihpi_aesebu_tx_format_get(struct snd_kcontrol *kcontrol,
1753 struct snd_ctl_elem_value *ucontrol) {
1754 return snd_asihpi_aesebu_format_get(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001755 hpi_aesebu_transmitter_get_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001756}
1757
1758static int snd_asihpi_aesebu_tx_format_put(struct snd_kcontrol *kcontrol,
1759 struct snd_ctl_elem_value *ucontrol) {
1760 return snd_asihpi_aesebu_format_put(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001761 hpi_aesebu_transmitter_set_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001762}
1763
1764
1765static int __devinit snd_asihpi_aesebu_tx_add(struct snd_card_asihpi *asihpi,
1766 struct hpi_control *hpi_ctl)
1767{
1768 struct snd_card *card = asihpi->card;
1769 struct snd_kcontrol_new snd_control;
1770
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001771 asihpi_ctl_init(&snd_control, hpi_ctl, "Format");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001772 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1773 snd_control.info = snd_asihpi_aesebu_format_info;
1774 snd_control.get = snd_asihpi_aesebu_tx_format_get;
1775 snd_control.put = snd_asihpi_aesebu_tx_format_put;
1776
1777 return ctl_add(card, &snd_control, asihpi);
1778}
1779
1780/*------------------------------------------------------------
1781 Tuner controls
1782 ------------------------------------------------------------*/
1783
1784/* Gain */
1785
1786static int snd_asihpi_tuner_gain_info(struct snd_kcontrol *kcontrol,
1787 struct snd_ctl_elem_info *uinfo)
1788{
1789 u32 h_control = kcontrol->private_value;
1790 u16 err;
1791 short idx;
1792 u16 gain_range[3];
1793
1794 for (idx = 0; idx < 3; idx++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001795 err = hpi_tuner_query_gain(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001796 idx, &gain_range[idx]);
1797 if (err != 0)
1798 return err;
1799 }
1800
1801 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1802 uinfo->count = 1;
1803 uinfo->value.integer.min = ((int)gain_range[0]) / HPI_UNITS_PER_dB;
1804 uinfo->value.integer.max = ((int)gain_range[1]) / HPI_UNITS_PER_dB;
1805 uinfo->value.integer.step = ((int) gain_range[2]) / HPI_UNITS_PER_dB;
1806 return 0;
1807}
1808
1809static int snd_asihpi_tuner_gain_get(struct snd_kcontrol *kcontrol,
1810 struct snd_ctl_elem_value *ucontrol)
1811{
1812 /*
1813 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1814 */
1815 u32 h_control = kcontrol->private_value;
1816 short gain;
1817
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001818 hpi_handle_error(hpi_tuner_get_gain(h_control, &gain));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001819 ucontrol->value.integer.value[0] = gain / HPI_UNITS_PER_dB;
1820
1821 return 0;
1822}
1823
1824static int snd_asihpi_tuner_gain_put(struct snd_kcontrol *kcontrol,
1825 struct snd_ctl_elem_value *ucontrol)
1826{
1827 /*
1828 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1829 */
1830 u32 h_control = kcontrol->private_value;
1831 short gain;
1832
1833 gain = (ucontrol->value.integer.value[0]) * HPI_UNITS_PER_dB;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001834 hpi_handle_error(hpi_tuner_set_gain(h_control, gain));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001835
1836 return 1;
1837}
1838
1839/* Band */
1840
1841static int asihpi_tuner_band_query(struct snd_kcontrol *kcontrol,
1842 u16 *band_list, u32 len) {
1843 u32 h_control = kcontrol->private_value;
1844 u16 err = 0;
1845 u32 i;
1846
1847 for (i = 0; i < len; i++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001848 err = hpi_tuner_query_band(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001849 h_control, i, &band_list[i]);
1850 if (err != 0)
1851 break;
1852 }
1853
1854 if (err && (err != HPI_ERROR_INVALID_OBJ_INDEX))
1855 return -EIO;
1856
1857 return i;
1858}
1859
1860static int snd_asihpi_tuner_band_info(struct snd_kcontrol *kcontrol,
1861 struct snd_ctl_elem_info *uinfo)
1862{
1863 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1864 int num_bands = 0;
1865
1866 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1867 HPI_TUNER_BAND_LAST);
1868
1869 if (num_bands < 0)
1870 return num_bands;
1871
1872 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1873 uinfo->count = 1;
1874 uinfo->value.enumerated.items = num_bands;
1875
1876 if (num_bands > 0) {
1877 if (uinfo->value.enumerated.item >=
1878 uinfo->value.enumerated.items)
1879 uinfo->value.enumerated.item =
1880 uinfo->value.enumerated.items - 1;
1881
1882 strcpy(uinfo->value.enumerated.name,
1883 asihpi_tuner_band_names[
1884 tuner_bands[uinfo->value.enumerated.item]]);
1885
1886 }
1887 return 0;
1888}
1889
1890static int snd_asihpi_tuner_band_get(struct snd_kcontrol *kcontrol,
1891 struct snd_ctl_elem_value *ucontrol)
1892{
1893 u32 h_control = kcontrol->private_value;
1894 /*
1895 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1896 */
1897 u16 band, idx;
1898 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1899 u32 num_bands = 0;
1900
1901 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1902 HPI_TUNER_BAND_LAST);
1903
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001904 hpi_handle_error(hpi_tuner_get_band(h_control, &band));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001905
1906 ucontrol->value.enumerated.item[0] = -1;
1907 for (idx = 0; idx < HPI_TUNER_BAND_LAST; idx++)
1908 if (tuner_bands[idx] == band) {
1909 ucontrol->value.enumerated.item[0] = idx;
1910 break;
1911 }
1912
1913 return 0;
1914}
1915
1916static int snd_asihpi_tuner_band_put(struct snd_kcontrol *kcontrol,
1917 struct snd_ctl_elem_value *ucontrol)
1918{
1919 /*
1920 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1921 */
1922 u32 h_control = kcontrol->private_value;
1923 u16 band;
1924 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1925 u32 num_bands = 0;
1926
1927 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1928 HPI_TUNER_BAND_LAST);
1929
1930 band = tuner_bands[ucontrol->value.enumerated.item[0]];
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001931 hpi_handle_error(hpi_tuner_set_band(h_control, band));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001932
1933 return 1;
1934}
1935
1936/* Freq */
1937
1938static int snd_asihpi_tuner_freq_info(struct snd_kcontrol *kcontrol,
1939 struct snd_ctl_elem_info *uinfo)
1940{
1941 u32 h_control = kcontrol->private_value;
1942 u16 err;
1943 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1944 u16 num_bands = 0, band_iter, idx;
1945 u32 freq_range[3], temp_freq_range[3];
1946
1947 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1948 HPI_TUNER_BAND_LAST);
1949
1950 freq_range[0] = INT_MAX;
1951 freq_range[1] = 0;
1952 freq_range[2] = INT_MAX;
1953
1954 for (band_iter = 0; band_iter < num_bands; band_iter++) {
1955 for (idx = 0; idx < 3; idx++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001956 err = hpi_tuner_query_frequency(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001957 idx, tuner_bands[band_iter],
1958 &temp_freq_range[idx]);
1959 if (err != 0)
1960 return err;
1961 }
1962
1963 /* skip band with bogus stepping */
1964 if (temp_freq_range[2] <= 0)
1965 continue;
1966
1967 if (temp_freq_range[0] < freq_range[0])
1968 freq_range[0] = temp_freq_range[0];
1969 if (temp_freq_range[1] > freq_range[1])
1970 freq_range[1] = temp_freq_range[1];
1971 if (temp_freq_range[2] < freq_range[2])
1972 freq_range[2] = temp_freq_range[2];
1973 }
1974
1975 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1976 uinfo->count = 1;
1977 uinfo->value.integer.min = ((int)freq_range[0]);
1978 uinfo->value.integer.max = ((int)freq_range[1]);
1979 uinfo->value.integer.step = ((int)freq_range[2]);
1980 return 0;
1981}
1982
1983static int snd_asihpi_tuner_freq_get(struct snd_kcontrol *kcontrol,
1984 struct snd_ctl_elem_value *ucontrol)
1985{
1986 u32 h_control = kcontrol->private_value;
1987 u32 freq;
1988
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001989 hpi_handle_error(hpi_tuner_get_frequency(h_control, &freq));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001990 ucontrol->value.integer.value[0] = freq;
1991
1992 return 0;
1993}
1994
1995static int snd_asihpi_tuner_freq_put(struct snd_kcontrol *kcontrol,
1996 struct snd_ctl_elem_value *ucontrol)
1997{
1998 u32 h_control = kcontrol->private_value;
1999 u32 freq;
2000
2001 freq = ucontrol->value.integer.value[0];
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002002 hpi_handle_error(hpi_tuner_set_frequency(h_control, freq));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002003
2004 return 1;
2005}
2006
2007/* Tuner control group initializer */
2008static int __devinit snd_asihpi_tuner_add(struct snd_card_asihpi *asihpi,
2009 struct hpi_control *hpi_ctl)
2010{
2011 struct snd_card *card = asihpi->card;
2012 struct snd_kcontrol_new snd_control;
2013
2014 snd_control.private_value = hpi_ctl->h_control;
2015 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2016
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002017 if (!hpi_tuner_get_gain(hpi_ctl->h_control, NULL)) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002018 asihpi_ctl_init(&snd_control, hpi_ctl, "Gain");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002019 snd_control.info = snd_asihpi_tuner_gain_info;
2020 snd_control.get = snd_asihpi_tuner_gain_get;
2021 snd_control.put = snd_asihpi_tuner_gain_put;
2022
2023 if (ctl_add(card, &snd_control, asihpi) < 0)
2024 return -EINVAL;
2025 }
2026
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002027 asihpi_ctl_init(&snd_control, hpi_ctl, "Band");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002028 snd_control.info = snd_asihpi_tuner_band_info;
2029 snd_control.get = snd_asihpi_tuner_band_get;
2030 snd_control.put = snd_asihpi_tuner_band_put;
2031
2032 if (ctl_add(card, &snd_control, asihpi) < 0)
2033 return -EINVAL;
2034
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002035 asihpi_ctl_init(&snd_control, hpi_ctl, "Freq");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002036 snd_control.info = snd_asihpi_tuner_freq_info;
2037 snd_control.get = snd_asihpi_tuner_freq_get;
2038 snd_control.put = snd_asihpi_tuner_freq_put;
2039
2040 return ctl_add(card, &snd_control, asihpi);
2041}
2042
2043/*------------------------------------------------------------
2044 Meter controls
2045 ------------------------------------------------------------*/
2046static int snd_asihpi_meter_info(struct snd_kcontrol *kcontrol,
2047 struct snd_ctl_elem_info *uinfo)
2048{
2049 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2050 uinfo->count = HPI_MAX_CHANNELS;
2051 uinfo->value.integer.min = 0;
2052 uinfo->value.integer.max = 0x7FFFFFFF;
2053 return 0;
2054}
2055
2056/* linear values for 10dB steps */
2057static int log2lin[] = {
2058 0x7FFFFFFF, /* 0dB */
2059 679093956,
2060 214748365,
2061 67909396,
2062 21474837,
2063 6790940,
2064 2147484, /* -60dB */
2065 679094,
2066 214748, /* -80 */
2067 67909,
2068 21475, /* -100 */
2069 6791,
2070 2147,
2071 679,
2072 214,
2073 68,
2074 21,
2075 7,
2076 2
2077};
2078
2079static int snd_asihpi_meter_get(struct snd_kcontrol *kcontrol,
2080 struct snd_ctl_elem_value *ucontrol)
2081{
2082 u32 h_control = kcontrol->private_value;
2083 short an_gain_mB[HPI_MAX_CHANNELS], i;
2084 u16 err;
2085
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002086 err = hpi_meter_get_peak(h_control, an_gain_mB);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002087
2088 for (i = 0; i < HPI_MAX_CHANNELS; i++) {
2089 if (err) {
2090 ucontrol->value.integer.value[i] = 0;
2091 } else if (an_gain_mB[i] >= 0) {
2092 ucontrol->value.integer.value[i] =
2093 an_gain_mB[i] << 16;
2094 } else {
2095 /* -ve is log value in millibels < -60dB,
2096 * convert to (roughly!) linear,
2097 */
2098 ucontrol->value.integer.value[i] =
2099 log2lin[an_gain_mB[i] / -1000];
2100 }
2101 }
2102 return 0;
2103}
2104
2105static int __devinit snd_asihpi_meter_add(struct snd_card_asihpi *asihpi,
2106 struct hpi_control *hpi_ctl, int subidx)
2107{
2108 struct snd_card *card = asihpi->card;
2109 struct snd_kcontrol_new snd_control;
2110
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002111 asihpi_ctl_init(&snd_control, hpi_ctl, "Meter");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002112 snd_control.access =
2113 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
2114 snd_control.info = snd_asihpi_meter_info;
2115 snd_control.get = snd_asihpi_meter_get;
2116
2117 snd_control.index = subidx;
2118
2119 return ctl_add(card, &snd_control, asihpi);
2120}
2121
2122/*------------------------------------------------------------
2123 Multiplexer controls
2124 ------------------------------------------------------------*/
2125static int snd_card_asihpi_mux_count_sources(struct snd_kcontrol *snd_control)
2126{
2127 u32 h_control = snd_control->private_value;
2128 struct hpi_control hpi_ctl;
2129 int s, err;
2130 for (s = 0; s < 32; s++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002131 err = hpi_multiplexer_query_source(h_control, s,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002132 &hpi_ctl.
2133 src_node_type,
2134 &hpi_ctl.
2135 src_node_index);
2136 if (err)
2137 break;
2138 }
2139 return s;
2140}
2141
2142static int snd_asihpi_mux_info(struct snd_kcontrol *kcontrol,
2143 struct snd_ctl_elem_info *uinfo)
2144{
2145 int err;
2146 u16 src_node_type, src_node_index;
2147 u32 h_control = kcontrol->private_value;
2148
2149 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2150 uinfo->count = 1;
2151 uinfo->value.enumerated.items =
2152 snd_card_asihpi_mux_count_sources(kcontrol);
2153
2154 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2155 uinfo->value.enumerated.item =
2156 uinfo->value.enumerated.items - 1;
2157
2158 err =
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002159 hpi_multiplexer_query_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002160 uinfo->value.enumerated.item,
2161 &src_node_type, &src_node_index);
2162
2163 sprintf(uinfo->value.enumerated.name, "%s %d",
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12002164 asihpi_src_names[src_node_type - HPI_SOURCENODE_NONE],
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002165 src_node_index);
2166 return 0;
2167}
2168
2169static int snd_asihpi_mux_get(struct snd_kcontrol *kcontrol,
2170 struct snd_ctl_elem_value *ucontrol)
2171{
2172 u32 h_control = kcontrol->private_value;
2173 u16 source_type, source_index;
2174 u16 src_node_type, src_node_index;
2175 int s;
2176
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002177 hpi_handle_error(hpi_multiplexer_get_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002178 &source_type, &source_index));
2179 /* Should cache this search result! */
2180 for (s = 0; s < 256; s++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002181 if (hpi_multiplexer_query_source(h_control, s,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002182 &src_node_type, &src_node_index))
2183 break;
2184
2185 if ((source_type == src_node_type)
2186 && (source_index == src_node_index)) {
2187 ucontrol->value.enumerated.item[0] = s;
2188 return 0;
2189 }
2190 }
2191 snd_printd(KERN_WARNING
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002192 "Control %x failed to match mux source %hu %hu\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002193 h_control, source_type, source_index);
2194 ucontrol->value.enumerated.item[0] = 0;
2195 return 0;
2196}
2197
2198static int snd_asihpi_mux_put(struct snd_kcontrol *kcontrol,
2199 struct snd_ctl_elem_value *ucontrol)
2200{
2201 int change;
2202 u32 h_control = kcontrol->private_value;
2203 u16 source_type, source_index;
2204 u16 e;
2205
2206 change = 1;
2207
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002208 e = hpi_multiplexer_query_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002209 ucontrol->value.enumerated.item[0],
2210 &source_type, &source_index);
2211 if (!e)
2212 hpi_handle_error(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002213 hpi_multiplexer_set_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002214 source_type, source_index));
2215 return change;
2216}
2217
2218
2219static int __devinit snd_asihpi_mux_add(struct snd_card_asihpi *asihpi,
2220 struct hpi_control *hpi_ctl)
2221{
2222 struct snd_card *card = asihpi->card;
2223 struct snd_kcontrol_new snd_control;
2224
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002225 asihpi_ctl_init(&snd_control, hpi_ctl, "Route");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002226 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2227 snd_control.info = snd_asihpi_mux_info;
2228 snd_control.get = snd_asihpi_mux_get;
2229 snd_control.put = snd_asihpi_mux_put;
2230
2231 return ctl_add(card, &snd_control, asihpi);
2232
2233}
2234
2235/*------------------------------------------------------------
2236 Channel mode controls
2237 ------------------------------------------------------------*/
2238static int snd_asihpi_cmode_info(struct snd_kcontrol *kcontrol,
2239 struct snd_ctl_elem_info *uinfo)
2240{
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002241 static const char * const mode_names[HPI_CHANNEL_MODE_LAST + 1] = {
2242 "invalid",
2243 "Normal", "Swap",
2244 "From Left", "From Right",
2245 "To Left", "To Right"
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002246 };
2247
2248 u32 h_control = kcontrol->private_value;
2249 u16 mode;
2250 int i;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002251 u16 mode_map[6];
2252 int valid_modes = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002253
2254 /* HPI channel mode values can be from 1 to 6
2255 Some adapters only support a contiguous subset
2256 */
2257 for (i = 0; i < HPI_CHANNEL_MODE_LAST; i++)
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002258 if (!hpi_channel_mode_query_mode(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002259 h_control, i, &mode)) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002260 mode_map[valid_modes] = mode;
2261 valid_modes++;
2262 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002263
2264 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2265 uinfo->count = 1;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002266 uinfo->value.enumerated.items = valid_modes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002267
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002268 if (uinfo->value.enumerated.item >= valid_modes)
2269 uinfo->value.enumerated.item = valid_modes - 1;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002270
2271 strcpy(uinfo->value.enumerated.name,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002272 mode_names[mode_map[uinfo->value.enumerated.item]]);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002273
2274 return 0;
2275}
2276
2277static int snd_asihpi_cmode_get(struct snd_kcontrol *kcontrol,
2278 struct snd_ctl_elem_value *ucontrol)
2279{
2280 u32 h_control = kcontrol->private_value;
2281 u16 mode;
2282
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002283 if (hpi_channel_mode_get(h_control, &mode))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002284 mode = 1;
2285
2286 ucontrol->value.enumerated.item[0] = mode - 1;
2287
2288 return 0;
2289}
2290
2291static int snd_asihpi_cmode_put(struct snd_kcontrol *kcontrol,
2292 struct snd_ctl_elem_value *ucontrol)
2293{
2294 int change;
2295 u32 h_control = kcontrol->private_value;
2296
2297 change = 1;
2298
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002299 hpi_handle_error(hpi_channel_mode_set(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002300 ucontrol->value.enumerated.item[0] + 1));
2301 return change;
2302}
2303
2304
2305static int __devinit snd_asihpi_cmode_add(struct snd_card_asihpi *asihpi,
2306 struct hpi_control *hpi_ctl)
2307{
2308 struct snd_card *card = asihpi->card;
2309 struct snd_kcontrol_new snd_control;
2310
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002311 asihpi_ctl_init(&snd_control, hpi_ctl, "Mode");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002312 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2313 snd_control.info = snd_asihpi_cmode_info;
2314 snd_control.get = snd_asihpi_cmode_get;
2315 snd_control.put = snd_asihpi_cmode_put;
2316
2317 return ctl_add(card, &snd_control, asihpi);
2318}
2319
2320/*------------------------------------------------------------
2321 Sampleclock source controls
2322 ------------------------------------------------------------*/
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002323static char *sampleclock_sources[MAX_CLOCKSOURCES] = {
2324 "N/A", "Local PLL", "Digital Sync", "Word External", "Word Header",
2325 "SMPTE", "Digital1", "Auto", "Network", "Invalid",
2326 "Prev Module",
2327 "Digital2", "Digital3", "Digital4", "Digital5",
2328 "Digital6", "Digital7", "Digital8"};
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002329
2330static int snd_asihpi_clksrc_info(struct snd_kcontrol *kcontrol,
2331 struct snd_ctl_elem_info *uinfo)
2332{
2333 struct snd_card_asihpi *asihpi =
2334 (struct snd_card_asihpi *)(kcontrol->private_data);
2335 struct clk_cache *clkcache = &asihpi->cc;
2336 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2337 uinfo->count = 1;
2338 uinfo->value.enumerated.items = clkcache->count;
2339
2340 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2341 uinfo->value.enumerated.item =
2342 uinfo->value.enumerated.items - 1;
2343
2344 strcpy(uinfo->value.enumerated.name,
2345 clkcache->s[uinfo->value.enumerated.item].name);
2346 return 0;
2347}
2348
2349static int snd_asihpi_clksrc_get(struct snd_kcontrol *kcontrol,
2350 struct snd_ctl_elem_value *ucontrol)
2351{
2352 struct snd_card_asihpi *asihpi =
2353 (struct snd_card_asihpi *)(kcontrol->private_data);
2354 struct clk_cache *clkcache = &asihpi->cc;
2355 u32 h_control = kcontrol->private_value;
2356 u16 source, srcindex = 0;
2357 int i;
2358
2359 ucontrol->value.enumerated.item[0] = 0;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002360 if (hpi_sample_clock_get_source(h_control, &source))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002361 source = 0;
2362
2363 if (source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002364 if (hpi_sample_clock_get_source_index(h_control, &srcindex))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002365 srcindex = 0;
2366
2367 for (i = 0; i < clkcache->count; i++)
2368 if ((clkcache->s[i].source == source) &&
2369 (clkcache->s[i].index == srcindex))
2370 break;
2371
2372 ucontrol->value.enumerated.item[0] = i;
2373
2374 return 0;
2375}
2376
2377static int snd_asihpi_clksrc_put(struct snd_kcontrol *kcontrol,
2378 struct snd_ctl_elem_value *ucontrol)
2379{
2380 struct snd_card_asihpi *asihpi =
2381 (struct snd_card_asihpi *)(kcontrol->private_data);
2382 struct clk_cache *clkcache = &asihpi->cc;
2383 int change, item;
2384 u32 h_control = kcontrol->private_value;
2385
2386 change = 1;
2387 item = ucontrol->value.enumerated.item[0];
2388 if (item >= clkcache->count)
2389 item = clkcache->count-1;
2390
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002391 hpi_handle_error(hpi_sample_clock_set_source(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002392 h_control, clkcache->s[item].source));
2393
2394 if (clkcache->s[item].source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002395 hpi_handle_error(hpi_sample_clock_set_source_index(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002396 h_control, clkcache->s[item].index));
2397 return change;
2398}
2399
2400/*------------------------------------------------------------
2401 Clkrate controls
2402 ------------------------------------------------------------*/
2403/* Need to change this to enumerated control with list of rates */
2404static int snd_asihpi_clklocal_info(struct snd_kcontrol *kcontrol,
2405 struct snd_ctl_elem_info *uinfo)
2406{
2407 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2408 uinfo->count = 1;
2409 uinfo->value.integer.min = 8000;
2410 uinfo->value.integer.max = 192000;
2411 uinfo->value.integer.step = 100;
2412
2413 return 0;
2414}
2415
2416static int snd_asihpi_clklocal_get(struct snd_kcontrol *kcontrol,
2417 struct snd_ctl_elem_value *ucontrol)
2418{
2419 u32 h_control = kcontrol->private_value;
2420 u32 rate;
2421 u16 e;
2422
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002423 e = hpi_sample_clock_get_local_rate(h_control, &rate);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002424 if (!e)
2425 ucontrol->value.integer.value[0] = rate;
2426 else
2427 ucontrol->value.integer.value[0] = 0;
2428 return 0;
2429}
2430
2431static int snd_asihpi_clklocal_put(struct snd_kcontrol *kcontrol,
2432 struct snd_ctl_elem_value *ucontrol)
2433{
2434 int change;
2435 u32 h_control = kcontrol->private_value;
2436
2437 /* change = asihpi->mixer_clkrate[addr][0] != left ||
2438 asihpi->mixer_clkrate[addr][1] != right;
2439 */
2440 change = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002441 hpi_handle_error(hpi_sample_clock_set_local_rate(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002442 ucontrol->value.integer.value[0]));
2443 return change;
2444}
2445
2446static int snd_asihpi_clkrate_info(struct snd_kcontrol *kcontrol,
2447 struct snd_ctl_elem_info *uinfo)
2448{
2449 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2450 uinfo->count = 1;
2451 uinfo->value.integer.min = 8000;
2452 uinfo->value.integer.max = 192000;
2453 uinfo->value.integer.step = 100;
2454
2455 return 0;
2456}
2457
2458static int snd_asihpi_clkrate_get(struct snd_kcontrol *kcontrol,
2459 struct snd_ctl_elem_value *ucontrol)
2460{
2461 u32 h_control = kcontrol->private_value;
2462 u32 rate;
2463 u16 e;
2464
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002465 e = hpi_sample_clock_get_sample_rate(h_control, &rate);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002466 if (!e)
2467 ucontrol->value.integer.value[0] = rate;
2468 else
2469 ucontrol->value.integer.value[0] = 0;
2470 return 0;
2471}
2472
2473static int __devinit snd_asihpi_sampleclock_add(struct snd_card_asihpi *asihpi,
2474 struct hpi_control *hpi_ctl)
2475{
2476 struct snd_card *card = asihpi->card;
2477 struct snd_kcontrol_new snd_control;
2478
2479 struct clk_cache *clkcache = &asihpi->cc;
2480 u32 hSC = hpi_ctl->h_control;
2481 int has_aes_in = 0;
2482 int i, j;
2483 u16 source;
2484
2485 snd_control.private_value = hpi_ctl->h_control;
2486
2487 clkcache->has_local = 0;
2488
2489 for (i = 0; i <= HPI_SAMPLECLOCK_SOURCE_LAST; i++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002490 if (hpi_sample_clock_query_source(hSC,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002491 i, &source))
2492 break;
2493 clkcache->s[i].source = source;
2494 clkcache->s[i].index = 0;
2495 clkcache->s[i].name = sampleclock_sources[source];
2496 if (source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
2497 has_aes_in = 1;
2498 if (source == HPI_SAMPLECLOCK_SOURCE_LOCAL)
2499 clkcache->has_local = 1;
2500 }
2501 if (has_aes_in)
2502 /* already will have picked up index 0 above */
2503 for (j = 1; j < 8; j++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002504 if (hpi_sample_clock_query_source_index(hSC,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002505 j, HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT,
2506 &source))
2507 break;
2508 clkcache->s[i].source =
2509 HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT;
2510 clkcache->s[i].index = j;
2511 clkcache->s[i].name = sampleclock_sources[
2512 j+HPI_SAMPLECLOCK_SOURCE_LAST];
2513 i++;
2514 }
2515 clkcache->count = i;
2516
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002517 asihpi_ctl_init(&snd_control, hpi_ctl, "Source");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002518 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE ;
2519 snd_control.info = snd_asihpi_clksrc_info;
2520 snd_control.get = snd_asihpi_clksrc_get;
2521 snd_control.put = snd_asihpi_clksrc_put;
2522 if (ctl_add(card, &snd_control, asihpi) < 0)
2523 return -EINVAL;
2524
2525
2526 if (clkcache->has_local) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002527 asihpi_ctl_init(&snd_control, hpi_ctl, "Localrate");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002528 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE ;
2529 snd_control.info = snd_asihpi_clklocal_info;
2530 snd_control.get = snd_asihpi_clklocal_get;
2531 snd_control.put = snd_asihpi_clklocal_put;
2532
2533
2534 if (ctl_add(card, &snd_control, asihpi) < 0)
2535 return -EINVAL;
2536 }
2537
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002538 asihpi_ctl_init(&snd_control, hpi_ctl, "Rate");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002539 snd_control.access =
2540 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
2541 snd_control.info = snd_asihpi_clkrate_info;
2542 snd_control.get = snd_asihpi_clkrate_get;
2543
2544 return ctl_add(card, &snd_control, asihpi);
2545}
2546/*------------------------------------------------------------
2547 Mixer
2548 ------------------------------------------------------------*/
2549
2550static int __devinit snd_card_asihpi_mixer_new(struct snd_card_asihpi *asihpi)
2551{
2552 struct snd_card *card = asihpi->card;
2553 unsigned int idx = 0;
2554 unsigned int subindex = 0;
2555 int err;
2556 struct hpi_control hpi_ctl, prev_ctl;
2557
2558 if (snd_BUG_ON(!asihpi))
2559 return -EINVAL;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002560 strcpy(card->mixername, "Asihpi Mixer");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002561
2562 err =
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002563 hpi_mixer_open(asihpi->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002564 &asihpi->h_mixer);
2565 hpi_handle_error(err);
2566 if (err)
2567 return -err;
2568
Takashi Iwai21896bc2010-06-02 12:08:37 +02002569 memset(&prev_ctl, 0, sizeof(prev_ctl));
2570 prev_ctl.control_type = -1;
2571
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002572 for (idx = 0; idx < 2000; idx++) {
2573 err = hpi_mixer_get_control_by_index(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002574 asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002575 idx,
2576 &hpi_ctl.src_node_type,
2577 &hpi_ctl.src_node_index,
2578 &hpi_ctl.dst_node_type,
2579 &hpi_ctl.dst_node_index,
2580 &hpi_ctl.control_type,
2581 &hpi_ctl.h_control);
2582 if (err) {
2583 if (err == HPI_ERROR_CONTROL_DISABLED) {
2584 if (mixer_dump)
2585 snd_printk(KERN_INFO
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002586 "Disabled HPI Control(%d)\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002587 idx);
2588 continue;
2589 } else
2590 break;
2591
2592 }
2593
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12002594 hpi_ctl.src_node_type -= HPI_SOURCENODE_NONE;
2595 hpi_ctl.dst_node_type -= HPI_DESTNODE_NONE;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002596
2597 /* ASI50xx in SSX mode has multiple meters on the same node.
2598 Use subindex to create distinct ALSA controls
2599 for any duplicated controls.
2600 */
2601 if ((hpi_ctl.control_type == prev_ctl.control_type) &&
2602 (hpi_ctl.src_node_type == prev_ctl.src_node_type) &&
2603 (hpi_ctl.src_node_index == prev_ctl.src_node_index) &&
2604 (hpi_ctl.dst_node_type == prev_ctl.dst_node_type) &&
2605 (hpi_ctl.dst_node_index == prev_ctl.dst_node_index))
2606 subindex++;
2607 else
2608 subindex = 0;
2609
2610 prev_ctl = hpi_ctl;
2611
2612 switch (hpi_ctl.control_type) {
2613 case HPI_CONTROL_VOLUME:
2614 err = snd_asihpi_volume_add(asihpi, &hpi_ctl);
2615 break;
2616 case HPI_CONTROL_LEVEL:
2617 err = snd_asihpi_level_add(asihpi, &hpi_ctl);
2618 break;
2619 case HPI_CONTROL_MULTIPLEXER:
2620 err = snd_asihpi_mux_add(asihpi, &hpi_ctl);
2621 break;
2622 case HPI_CONTROL_CHANNEL_MODE:
2623 err = snd_asihpi_cmode_add(asihpi, &hpi_ctl);
2624 break;
2625 case HPI_CONTROL_METER:
2626 err = snd_asihpi_meter_add(asihpi, &hpi_ctl, subindex);
2627 break;
2628 case HPI_CONTROL_SAMPLECLOCK:
2629 err = snd_asihpi_sampleclock_add(
2630 asihpi, &hpi_ctl);
2631 break;
2632 case HPI_CONTROL_CONNECTION: /* ignore these */
2633 continue;
2634 case HPI_CONTROL_TUNER:
2635 err = snd_asihpi_tuner_add(asihpi, &hpi_ctl);
2636 break;
2637 case HPI_CONTROL_AESEBU_TRANSMITTER:
2638 err = snd_asihpi_aesebu_tx_add(asihpi, &hpi_ctl);
2639 break;
2640 case HPI_CONTROL_AESEBU_RECEIVER:
2641 err = snd_asihpi_aesebu_rx_add(asihpi, &hpi_ctl);
2642 break;
2643 case HPI_CONTROL_VOX:
2644 case HPI_CONTROL_BITSTREAM:
2645 case HPI_CONTROL_MICROPHONE:
2646 case HPI_CONTROL_PARAMETRIC_EQ:
2647 case HPI_CONTROL_COMPANDER:
2648 default:
2649 if (mixer_dump)
2650 snd_printk(KERN_INFO
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002651 "Untranslated HPI Control"
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002652 "(%d) %d %d %d %d %d\n",
2653 idx,
2654 hpi_ctl.control_type,
2655 hpi_ctl.src_node_type,
2656 hpi_ctl.src_node_index,
2657 hpi_ctl.dst_node_type,
2658 hpi_ctl.dst_node_index);
2659 continue;
2660 };
2661 if (err < 0)
2662 return err;
2663 }
2664 if (HPI_ERROR_INVALID_OBJ_INDEX != err)
2665 hpi_handle_error(err);
2666
2667 snd_printk(KERN_INFO "%d mixer controls found\n", idx);
2668
2669 return 0;
2670}
2671
2672/*------------------------------------------------------------
2673 /proc interface
2674 ------------------------------------------------------------*/
2675
2676static void
2677snd_asihpi_proc_read(struct snd_info_entry *entry,
2678 struct snd_info_buffer *buffer)
2679{
2680 struct snd_card_asihpi *asihpi = entry->private_data;
2681 u16 version;
2682 u32 h_control;
2683 u32 rate = 0;
2684 u16 source = 0;
2685 int err;
2686
2687 snd_iprintf(buffer, "ASIHPI driver proc file\n");
2688 snd_iprintf(buffer,
2689 "adapter ID=%4X\n_index=%d\n"
2690 "num_outstreams=%d\n_num_instreams=%d\n",
2691 asihpi->type, asihpi->adapter_index,
2692 asihpi->num_outstreams, asihpi->num_instreams);
2693
2694 version = asihpi->version;
2695 snd_iprintf(buffer,
2696 "serial#=%d\n_hw version %c%d\nDSP code version %03d\n",
2697 asihpi->serial_number, ((version >> 3) & 0xf) + 'A',
2698 version & 0x7,
2699 ((version >> 13) * 100) + ((version >> 7) & 0x3f));
2700
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002701 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002702 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
2703 HPI_CONTROL_SAMPLECLOCK, &h_control);
2704
2705 if (!err) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002706 err = hpi_sample_clock_get_sample_rate(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002707 h_control, &rate);
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002708 err += hpi_sample_clock_get_source(h_control, &source);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002709
2710 if (!err)
2711 snd_iprintf(buffer, "sample_clock=%d_hz, source %s\n",
2712 rate, sampleclock_sources[source]);
2713 }
2714
2715}
2716
2717
2718static void __devinit snd_asihpi_proc_init(struct snd_card_asihpi *asihpi)
2719{
2720 struct snd_info_entry *entry;
2721
2722 if (!snd_card_proc_new(asihpi->card, "info", &entry))
2723 snd_info_set_text_ops(entry, asihpi, snd_asihpi_proc_read);
2724}
2725
2726/*------------------------------------------------------------
2727 HWDEP
2728 ------------------------------------------------------------*/
2729
2730static int snd_asihpi_hpi_open(struct snd_hwdep *hw, struct file *file)
2731{
2732 if (enable_hpi_hwdep)
2733 return 0;
2734 else
2735 return -ENODEV;
2736
2737}
2738
2739static int snd_asihpi_hpi_release(struct snd_hwdep *hw, struct file *file)
2740{
2741 if (enable_hpi_hwdep)
2742 return asihpi_hpi_release(file);
2743 else
2744 return -ENODEV;
2745}
2746
2747static int snd_asihpi_hpi_ioctl(struct snd_hwdep *hw, struct file *file,
2748 unsigned int cmd, unsigned long arg)
2749{
2750 if (enable_hpi_hwdep)
2751 return asihpi_hpi_ioctl(file, cmd, arg);
2752 else
2753 return -ENODEV;
2754}
2755
2756
2757/* results in /dev/snd/hwC#D0 file for each card with index #
2758 also /proc/asound/hwdep will contain '#-00: asihpi (HPI) for each card'
2759*/
2760static int __devinit snd_asihpi_hpi_new(struct snd_card_asihpi *asihpi,
2761 int device, struct snd_hwdep **rhwdep)
2762{
2763 struct snd_hwdep *hw;
2764 int err;
2765
2766 if (rhwdep)
2767 *rhwdep = NULL;
2768 err = snd_hwdep_new(asihpi->card, "HPI", device, &hw);
2769 if (err < 0)
2770 return err;
2771 strcpy(hw->name, "asihpi (HPI)");
2772 hw->iface = SNDRV_HWDEP_IFACE_LAST;
2773 hw->ops.open = snd_asihpi_hpi_open;
2774 hw->ops.ioctl = snd_asihpi_hpi_ioctl;
2775 hw->ops.release = snd_asihpi_hpi_release;
2776 hw->private_data = asihpi;
2777 if (rhwdep)
2778 *rhwdep = hw;
2779 return 0;
2780}
2781
2782/*------------------------------------------------------------
2783 CARD
2784 ------------------------------------------------------------*/
2785static int __devinit snd_asihpi_probe(struct pci_dev *pci_dev,
2786 const struct pci_device_id *pci_id)
2787{
2788 int err;
2789
2790 u16 version;
2791 int pcm_substreams;
2792
2793 struct hpi_adapter *hpi_card;
2794 struct snd_card *card;
2795 struct snd_card_asihpi *asihpi;
2796
2797 u32 h_control;
2798 u32 h_stream;
2799
2800 static int dev;
2801 if (dev >= SNDRV_CARDS)
2802 return -ENODEV;
2803
2804 /* Should this be enable[hpi_card->index] ? */
2805 if (!enable[dev]) {
2806 dev++;
2807 return -ENOENT;
2808 }
2809
2810 err = asihpi_adapter_probe(pci_dev, pci_id);
2811 if (err < 0)
2812 return err;
2813
2814 hpi_card = pci_get_drvdata(pci_dev);
2815 /* first try to give the card the same index as its hardware index */
2816 err = snd_card_create(hpi_card->index,
2817 id[hpi_card->index], THIS_MODULE,
2818 sizeof(struct snd_card_asihpi),
2819 &card);
2820 if (err < 0) {
2821 /* if that fails, try the default index==next available */
2822 err =
2823 snd_card_create(index[dev], id[dev],
2824 THIS_MODULE,
2825 sizeof(struct snd_card_asihpi),
2826 &card);
2827 if (err < 0)
2828 return err;
2829 snd_printk(KERN_WARNING
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002830 "**** WARNING **** Adapter index %d->ALSA index %d\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002831 hpi_card->index, card->number);
2832 }
2833
Eliot Blennerhassett12253672011-02-10 17:26:10 +13002834 snd_card_set_dev(card, &pci_dev->dev);
2835
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002836 asihpi = (struct snd_card_asihpi *) card->private_data;
2837 asihpi->card = card;
Eliot Blennerhassett12253672011-02-10 17:26:10 +13002838 asihpi->pci = pci_dev;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002839 asihpi->adapter_index = hpi_card->index;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002840 hpi_handle_error(hpi_adapter_get_info(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002841 asihpi->adapter_index,
2842 &asihpi->num_outstreams,
2843 &asihpi->num_instreams,
2844 &asihpi->version,
2845 &asihpi->serial_number, &asihpi->type));
2846
2847 version = asihpi->version;
2848 snd_printk(KERN_INFO "adapter ID=%4X index=%d num_outstreams=%d "
2849 "num_instreams=%d S/N=%d\n"
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002850 "Hw Version %c%d DSP code version %03d\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002851 asihpi->type, asihpi->adapter_index,
2852 asihpi->num_outstreams,
2853 asihpi->num_instreams, asihpi->serial_number,
2854 ((version >> 3) & 0xf) + 'A',
2855 version & 0x7,
2856 ((version >> 13) * 100) + ((version >> 7) & 0x3f));
2857
2858 pcm_substreams = asihpi->num_outstreams;
2859 if (pcm_substreams < asihpi->num_instreams)
2860 pcm_substreams = asihpi->num_instreams;
2861
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002862 err = hpi_adapter_get_property(asihpi->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002863 HPI_ADAPTER_PROPERTY_CAPS1,
2864 NULL, &asihpi->support_grouping);
2865 if (err)
2866 asihpi->support_grouping = 0;
2867
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002868 err = hpi_adapter_get_property(asihpi->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002869 HPI_ADAPTER_PROPERTY_CAPS2,
2870 &asihpi->support_mrx, NULL);
2871 if (err)
2872 asihpi->support_mrx = 0;
2873
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002874 err = hpi_adapter_get_property(asihpi->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002875 HPI_ADAPTER_PROPERTY_INTERVAL,
2876 NULL, &asihpi->update_interval_frames);
2877 if (err)
2878 asihpi->update_interval_frames = 512;
2879
Eliot Blennerhassett26aebef2011-03-25 15:25:47 +13002880 if (!asihpi->support_mmap)
2881 asihpi->update_interval_frames *= 2;
2882
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002883 hpi_handle_error(hpi_instream_open(asihpi->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002884 0, &h_stream));
2885
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002886 err = hpi_instream_host_buffer_free(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002887 asihpi->support_mmap = (!err);
2888
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002889 hpi_handle_error(hpi_instream_close(h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002890
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002891 err = hpi_adapter_get_property(asihpi->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002892 HPI_ADAPTER_PROPERTY_CURCHANNELS,
2893 &asihpi->in_max_chans, &asihpi->out_max_chans);
2894 if (err) {
2895 asihpi->in_max_chans = 2;
2896 asihpi->out_max_chans = 2;
2897 }
2898
2899 snd_printk(KERN_INFO "supports mmap:%d grouping:%d mrx:%d\n",
2900 asihpi->support_mmap,
2901 asihpi->support_grouping,
2902 asihpi->support_mrx
2903 );
2904
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002905 err = snd_card_asihpi_pcm_new(asihpi, 0, pcm_substreams);
2906 if (err < 0) {
2907 snd_printk(KERN_ERR "pcm_new failed\n");
2908 goto __nodev;
2909 }
2910 err = snd_card_asihpi_mixer_new(asihpi);
2911 if (err < 0) {
2912 snd_printk(KERN_ERR "mixer_new failed\n");
2913 goto __nodev;
2914 }
2915
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002916 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002917 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
2918 HPI_CONTROL_SAMPLECLOCK, &h_control);
2919
2920 if (!err)
2921 err = hpi_sample_clock_set_local_rate(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002922 h_control, adapter_fs);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002923
2924 snd_asihpi_proc_init(asihpi);
2925
2926 /* always create, can be enabled or disabled dynamically
2927 by enable_hwdep module param*/
2928 snd_asihpi_hpi_new(asihpi, 0, NULL);
2929
2930 if (asihpi->support_mmap)
2931 strcpy(card->driver, "ASIHPI-MMAP");
2932 else
2933 strcpy(card->driver, "ASIHPI");
2934
2935 sprintf(card->shortname, "AudioScience ASI%4X", asihpi->type);
2936 sprintf(card->longname, "%s %i",
2937 card->shortname, asihpi->adapter_index);
2938 err = snd_card_register(card);
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13002939
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002940 if (!err) {
2941 hpi_card->snd_card_asihpi = card;
2942 dev++;
2943 return 0;
2944 }
2945__nodev:
2946 snd_card_free(card);
2947 snd_printk(KERN_ERR "snd_asihpi_probe error %d\n", err);
2948 return err;
2949
2950}
2951
2952static void __devexit snd_asihpi_remove(struct pci_dev *pci_dev)
2953{
2954 struct hpi_adapter *hpi_card = pci_get_drvdata(pci_dev);
2955
2956 snd_card_free(hpi_card->snd_card_asihpi);
2957 hpi_card->snd_card_asihpi = NULL;
2958 asihpi_adapter_remove(pci_dev);
2959}
2960
2961static DEFINE_PCI_DEVICE_TABLE(asihpi_pci_tbl) = {
2962 {HPI_PCI_VENDOR_ID_TI, HPI_PCI_DEV_ID_DSP6205,
2963 HPI_PCI_VENDOR_ID_AUDIOSCIENCE, PCI_ANY_ID, 0, 0,
2964 (kernel_ulong_t)HPI_6205},
2965 {HPI_PCI_VENDOR_ID_TI, HPI_PCI_DEV_ID_PCI2040,
2966 HPI_PCI_VENDOR_ID_AUDIOSCIENCE, PCI_ANY_ID, 0, 0,
2967 (kernel_ulong_t)HPI_6000},
2968 {0,}
2969};
2970MODULE_DEVICE_TABLE(pci, asihpi_pci_tbl);
2971
2972static struct pci_driver driver = {
2973 .name = "asihpi",
2974 .id_table = asihpi_pci_tbl,
2975 .probe = snd_asihpi_probe,
2976 .remove = __devexit_p(snd_asihpi_remove),
2977#ifdef CONFIG_PM
2978/* .suspend = snd_asihpi_suspend,
2979 .resume = snd_asihpi_resume, */
2980#endif
2981};
2982
2983static int __init snd_asihpi_init(void)
2984{
2985 asihpi_init();
2986 return pci_register_driver(&driver);
2987}
2988
2989static void __exit snd_asihpi_exit(void)
2990{
2991
2992 pci_unregister_driver(&driver);
2993 asihpi_exit();
2994}
2995
2996module_init(snd_asihpi_init)
2997module_exit(snd_asihpi_exit)
2998