blob: 378656d7916400b0ae4b7dffffd7627647135fba [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 Blennerhassettf3d145a2011-04-05 20:55:44 +120045
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020046MODULE_LICENSE("GPL");
47MODULE_AUTHOR("AudioScience inc. <support@audioscience.com>");
48MODULE_DESCRIPTION("AudioScience ALSA ASI5000 ASI6000 ASI87xx ASI89xx");
49
Eliot Blennerhassettb0096a62011-04-05 20:55:46 +120050#if defined CONFIG_SND_DEBUG
Eliot Blennerhassetta6477132011-04-05 20:55:42 +120051/* copied from pcm_lib.c, hope later patch will make that version public
52and this copy can be removed */
Eliot Blennerhassett0a17e992011-07-22 15:52:44 +120053static inline void
54snd_pcm_debug_name(struct snd_pcm_substream *substream, char *buf, size_t size)
Eliot Blennerhassetta6477132011-04-05 20:55:42 +120055{
Eliot Blennerhassett0a17e992011-07-22 15:52:44 +120056 snprintf(buf, size, "pcmC%dD%d%c:%d",
Eliot Blennerhassetta6477132011-04-05 20:55:42 +120057 substream->pcm->card->number,
58 substream->pcm->device,
59 substream->stream ? 'c' : 'p',
60 substream->number);
61}
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +130062#else
Eliot Blennerhassett0a17e992011-07-22 15:52:44 +120063static inline void
64snd_pcm_debug_name(struct snd_pcm_substream *substream, char *buf, size_t size)
65{
66 *buf = 0;
67}
Eliot Blennerhassettb0096a62011-04-05 20:55:46 +120068#endif
Eliot Blennerhassetta6477132011-04-05 20:55:42 +120069
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +130070#if defined CONFIG_SND_DEBUG_VERBOSE
71/**
72 * snd_printddd - very verbose debug printk
73 * @format: format string
74 *
75 * Works like snd_printk() for debugging purposes.
76 * Ignored when CONFIG_SND_DEBUG_VERBOSE is not set.
77 * Must set snd module debug parameter to 3 to enable at runtime.
78 */
79#define snd_printddd(format, args...) \
80 __snd_printk(3, __FILE__, __LINE__, format, ##args)
81#else
Eliot Blennerhassettb0096a62011-04-05 20:55:46 +120082#define snd_printddd(format, args...) do { } while (0)
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +130083#endif
84
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020085static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* index 0-MAX */
86static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
87static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
88static int enable_hpi_hwdep = 1;
89
90module_param_array(index, int, NULL, S_IRUGO);
91MODULE_PARM_DESC(index, "ALSA index value for AudioScience soundcard.");
92
93module_param_array(id, charp, NULL, S_IRUGO);
94MODULE_PARM_DESC(id, "ALSA ID string for AudioScience soundcard.");
95
96module_param_array(enable, bool, NULL, S_IRUGO);
97MODULE_PARM_DESC(enable, "ALSA enable AudioScience soundcard.");
98
99module_param(enable_hpi_hwdep, bool, S_IRUGO|S_IWUSR);
100MODULE_PARM_DESC(enable_hpi_hwdep,
101 "ALSA enable HPI hwdep for AudioScience soundcard ");
102
103/* identify driver */
104#ifdef KERNEL_ALSA_BUILD
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300105static char *build_info = "Built using headers from kernel source";
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200106module_param(build_info, charp, S_IRUGO);
107MODULE_PARM_DESC(build_info, "built using headers from kernel source");
108#else
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300109static char *build_info = "Built within ALSA source";
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200110module_param(build_info, charp, S_IRUGO);
111MODULE_PARM_DESC(build_info, "built within ALSA source");
112#endif
113
114/* set to 1 to dump every control from adapter to log */
115static const int mixer_dump;
116
117#define DEFAULT_SAMPLERATE 44100
118static int adapter_fs = DEFAULT_SAMPLERATE;
119
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200120/* defaults */
121#define PERIODS_MIN 2
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300122#define PERIOD_BYTES_MIN 2048
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200123#define BUFFER_BYTES_MAX (512 * 1024)
124
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200125#define MAX_CLOCKSOURCES (HPI_SAMPLECLOCK_SOURCE_LAST + 1 + 7)
126
127struct clk_source {
128 int source;
129 int index;
130 char *name;
131};
132
133struct clk_cache {
134 int count;
135 int has_local;
136 struct clk_source s[MAX_CLOCKSOURCES];
137};
138
139/* Per card data */
140struct snd_card_asihpi {
141 struct snd_card *card;
142 struct pci_dev *pci;
143 u16 adapter_index;
144 u32 serial_number;
145 u16 type;
146 u16 version;
147 u16 num_outstreams;
148 u16 num_instreams;
149
150 u32 h_mixer;
151 struct clk_cache cc;
152
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200153 u16 can_dma;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200154 u16 support_grouping;
155 u16 support_mrx;
156 u16 update_interval_frames;
157 u16 in_max_chans;
158 u16 out_max_chans;
159};
160
161/* Per stream data */
162struct snd_card_asihpi_pcm {
163 struct timer_list timer;
164 unsigned int respawn_timer;
165 unsigned int hpi_buffer_attached;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300166 unsigned int buffer_bytes;
167 unsigned int period_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200168 unsigned int bytes_per_sec;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300169 unsigned int pcm_buf_host_rw_ofs; /* Host R/W pos */
170 unsigned int pcm_buf_dma_ofs; /* DMA R/W offset in buffer */
171 unsigned int pcm_buf_elapsed_dma_ofs; /* DMA R/W offset in buffer */
Eliot Blennerhassett0b7ce9e2011-04-05 20:55:43 +1200172 unsigned int drained_count;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200173 struct snd_pcm_substream *substream;
174 u32 h_stream;
175 struct hpi_format format;
176};
177
178/* universal stream verbs work with out or in stream handles */
179
180/* Functions to allow driver to give a buffer to HPI for busmastering */
181
182static u16 hpi_stream_host_buffer_attach(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200183 u32 h_stream, /* handle to outstream. */
184 u32 size_in_bytes, /* size in bytes of bus mastering buffer */
185 u32 pci_address
186)
187{
188 struct hpi_message hm;
189 struct hpi_response hr;
190 unsigned int obj = hpi_handle_object(h_stream);
191
192 if (!h_stream)
193 return HPI_ERROR_INVALID_OBJ;
194 hpi_init_message_response(&hm, &hr, obj,
195 obj == HPI_OBJ_OSTREAM ?
196 HPI_OSTREAM_HOSTBUFFER_ALLOC :
197 HPI_ISTREAM_HOSTBUFFER_ALLOC);
198
199 hpi_handle_to_indexes(h_stream, &hm.adapter_index,
200 &hm.obj_index);
201
202 hm.u.d.u.buffer.buffer_size = size_in_bytes;
203 hm.u.d.u.buffer.pci_address = pci_address;
204 hm.u.d.u.buffer.command = HPI_BUFFER_CMD_INTERNAL_GRANTADAPTER;
205 hpi_send_recv(&hm, &hr);
206 return hr.error;
207}
208
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300209static u16 hpi_stream_host_buffer_detach(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200210{
211 struct hpi_message hm;
212 struct hpi_response hr;
213 unsigned int obj = hpi_handle_object(h_stream);
214
215 if (!h_stream)
216 return HPI_ERROR_INVALID_OBJ;
217
218 hpi_init_message_response(&hm, &hr, obj,
219 obj == HPI_OBJ_OSTREAM ?
220 HPI_OSTREAM_HOSTBUFFER_FREE :
221 HPI_ISTREAM_HOSTBUFFER_FREE);
222
223 hpi_handle_to_indexes(h_stream, &hm.adapter_index,
224 &hm.obj_index);
225 hm.u.d.u.buffer.command = HPI_BUFFER_CMD_INTERNAL_REVOKEADAPTER;
226 hpi_send_recv(&hm, &hr);
227 return hr.error;
228}
229
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300230static inline u16 hpi_stream_start(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200231{
232 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300233 return hpi_outstream_start(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200234 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300235 return hpi_instream_start(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200236}
237
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300238static inline u16 hpi_stream_stop(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200239{
240 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300241 return hpi_outstream_stop(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200242 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300243 return hpi_instream_stop(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200244}
245
246static inline u16 hpi_stream_get_info_ex(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200247 u32 h_stream,
248 u16 *pw_state,
249 u32 *pbuffer_size,
250 u32 *pdata_in_buffer,
251 u32 *psample_count,
252 u32 *pauxiliary_data
253)
254{
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300255 u16 e;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200256 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300257 e = hpi_outstream_get_info_ex(h_stream, pw_state,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200258 pbuffer_size, pdata_in_buffer,
259 psample_count, pauxiliary_data);
260 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300261 e = hpi_instream_get_info_ex(h_stream, pw_state,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200262 pbuffer_size, pdata_in_buffer,
263 psample_count, pauxiliary_data);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300264 return e;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200265}
266
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300267static inline u16 hpi_stream_group_add(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200268 u32 h_master,
269 u32 h_stream)
270{
271 if (hpi_handle_object(h_master) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300272 return hpi_outstream_group_add(h_master, h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200273 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300274 return hpi_instream_group_add(h_master, h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200275}
276
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300277static inline u16 hpi_stream_group_reset(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200278{
279 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300280 return hpi_outstream_group_reset(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200281 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300282 return hpi_instream_group_reset(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200283}
284
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300285static inline u16 hpi_stream_group_get_map(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200286 u32 h_stream, u32 *mo, u32 *mi)
287{
288 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300289 return hpi_outstream_group_get_map(h_stream, mo, mi);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200290 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300291 return hpi_instream_group_get_map(h_stream, mo, mi);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200292}
293
294static u16 handle_error(u16 err, int line, char *filename)
295{
296 if (err)
297 printk(KERN_WARNING
298 "in file %s, line %d: HPI error %d\n",
299 filename, line, err);
300 return err;
301}
302
303#define hpi_handle_error(x) handle_error(x, __LINE__, __FILE__)
304
305/***************************** GENERAL PCM ****************/
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200306
307static void print_hwparams(struct snd_pcm_substream *substream,
308 struct snd_pcm_hw_params *p)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200309{
Eliot Blennerhassett0a17e992011-07-22 15:52:44 +1200310 char name[16];
311 snd_pcm_debug_name(substream, name, sizeof(name));
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200312 snd_printd("%s HWPARAMS\n", name);
313 snd_printd(" samplerate %d Hz\n", params_rate(p));
314 snd_printd(" channels %d\n", params_channels(p));
315 snd_printd(" format %d\n", params_format(p));
316 snd_printd(" subformat %d\n", params_subformat(p));
317 snd_printd(" buffer %d B\n", params_buffer_bytes(p));
318 snd_printd(" period %d B\n", params_period_bytes(p));
319 snd_printd(" access %d\n", params_access(p));
320 snd_printd(" period_size %d\n", params_period_size(p));
321 snd_printd(" periods %d\n", params_periods(p));
322 snd_printd(" buffer_size %d\n", params_buffer_size(p));
323 snd_printd(" %d B/s\n", params_rate(p) *
324 params_channels(p) *
325 snd_pcm_format_width(params_format(p)) / 8);
326
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200327}
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200328
329static snd_pcm_format_t hpi_to_alsa_formats[] = {
330 -1, /* INVALID */
331 SNDRV_PCM_FORMAT_U8, /* HPI_FORMAT_PCM8_UNSIGNED 1 */
332 SNDRV_PCM_FORMAT_S16, /* HPI_FORMAT_PCM16_SIGNED 2 */
333 -1, /* HPI_FORMAT_MPEG_L1 3 */
334 SNDRV_PCM_FORMAT_MPEG, /* HPI_FORMAT_MPEG_L2 4 */
335 SNDRV_PCM_FORMAT_MPEG, /* HPI_FORMAT_MPEG_L3 5 */
336 -1, /* HPI_FORMAT_DOLBY_AC2 6 */
337 -1, /* HPI_FORMAT_DOLBY_AC3 7 */
338 SNDRV_PCM_FORMAT_S16_BE,/* HPI_FORMAT_PCM16_BIGENDIAN 8 */
339 -1, /* HPI_FORMAT_AA_TAGIT1_HITS 9 */
340 -1, /* HPI_FORMAT_AA_TAGIT1_INSERTS 10 */
341 SNDRV_PCM_FORMAT_S32, /* HPI_FORMAT_PCM32_SIGNED 11 */
342 -1, /* HPI_FORMAT_RAW_BITSTREAM 12 */
343 -1, /* HPI_FORMAT_AA_TAGIT1_HITS_EX1 13 */
344 SNDRV_PCM_FORMAT_FLOAT, /* HPI_FORMAT_PCM32_FLOAT 14 */
345#if 1
346 /* ALSA can't handle 3 byte sample size together with power-of-2
347 * constraint on buffer_bytes, so disable this format
348 */
349 -1
350#else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300351 /* SNDRV_PCM_FORMAT_S24_3LE */ /* HPI_FORMAT_PCM24_SIGNED 15 */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200352#endif
353};
354
355
356static int snd_card_asihpi_format_alsa2hpi(snd_pcm_format_t alsa_format,
357 u16 *hpi_format)
358{
359 u16 format;
360
361 for (format = HPI_FORMAT_PCM8_UNSIGNED;
362 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
363 if (hpi_to_alsa_formats[format] == alsa_format) {
364 *hpi_format = format;
365 return 0;
366 }
367 }
368
369 snd_printd(KERN_WARNING "failed match for alsa format %d\n",
370 alsa_format);
371 *hpi_format = 0;
372 return -EINVAL;
373}
374
375static void snd_card_asihpi_pcm_samplerates(struct snd_card_asihpi *asihpi,
376 struct snd_pcm_hardware *pcmhw)
377{
378 u16 err;
379 u32 h_control;
380 u32 sample_rate;
381 int idx;
382 unsigned int rate_min = 200000;
383 unsigned int rate_max = 0;
384 unsigned int rates = 0;
385
386 if (asihpi->support_mrx) {
387 rates |= SNDRV_PCM_RATE_CONTINUOUS;
388 rates |= SNDRV_PCM_RATE_8000_96000;
389 rate_min = 8000;
390 rate_max = 100000;
391 } else {
392 /* on cards without SRC,
393 valid rates are determined by sampleclock */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300394 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200395 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
396 HPI_CONTROL_SAMPLECLOCK, &h_control);
397 if (err) {
398 snd_printk(KERN_ERR
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300399 "No local sampleclock, err %d\n", err);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200400 }
401
Eliot Blennerhassett7bf76c32011-03-25 15:25:46 +1300402 for (idx = -1; idx < 100; idx++) {
403 if (idx == -1) {
404 if (hpi_sample_clock_get_sample_rate(h_control,
405 &sample_rate))
406 continue;
407 } else if (hpi_sample_clock_query_local_rate(h_control,
408 idx, &sample_rate)) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200409 break;
410 }
411
412 rate_min = min(rate_min, sample_rate);
413 rate_max = max(rate_max, sample_rate);
414
415 switch (sample_rate) {
416 case 5512:
417 rates |= SNDRV_PCM_RATE_5512;
418 break;
419 case 8000:
420 rates |= SNDRV_PCM_RATE_8000;
421 break;
422 case 11025:
423 rates |= SNDRV_PCM_RATE_11025;
424 break;
425 case 16000:
426 rates |= SNDRV_PCM_RATE_16000;
427 break;
428 case 22050:
429 rates |= SNDRV_PCM_RATE_22050;
430 break;
431 case 32000:
432 rates |= SNDRV_PCM_RATE_32000;
433 break;
434 case 44100:
435 rates |= SNDRV_PCM_RATE_44100;
436 break;
437 case 48000:
438 rates |= SNDRV_PCM_RATE_48000;
439 break;
440 case 64000:
441 rates |= SNDRV_PCM_RATE_64000;
442 break;
443 case 88200:
444 rates |= SNDRV_PCM_RATE_88200;
445 break;
446 case 96000:
447 rates |= SNDRV_PCM_RATE_96000;
448 break;
449 case 176400:
450 rates |= SNDRV_PCM_RATE_176400;
451 break;
452 case 192000:
453 rates |= SNDRV_PCM_RATE_192000;
454 break;
455 default: /* some other rate */
456 rates |= SNDRV_PCM_RATE_KNOT;
457 }
458 }
459 }
460
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200461 pcmhw->rates = rates;
462 pcmhw->rate_min = rate_min;
463 pcmhw->rate_max = rate_max;
464}
465
466static int snd_card_asihpi_pcm_hw_params(struct snd_pcm_substream *substream,
467 struct snd_pcm_hw_params *params)
468{
469 struct snd_pcm_runtime *runtime = substream->runtime;
470 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
471 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
472 int err;
473 u16 format;
Kulikov Vasiliy315e8f72010-07-15 22:48:19 +0400474 int width;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200475 unsigned int bytes_per_sec;
476
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200477 print_hwparams(substream, params);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200478 err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
479 if (err < 0)
480 return err;
481 err = snd_card_asihpi_format_alsa2hpi(params_format(params), &format);
482 if (err)
483 return err;
484
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200485 hpi_handle_error(hpi_format_create(&dpcm->format,
486 params_channels(params),
487 format, params_rate(params), 0, 0));
488
489 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300490 if (hpi_instream_reset(dpcm->h_stream) != 0)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200491 return -EINVAL;
492
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300493 if (hpi_instream_set_format(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200494 dpcm->h_stream, &dpcm->format) != 0)
495 return -EINVAL;
496 }
497
498 dpcm->hpi_buffer_attached = 0;
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200499 if (card->can_dma) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300500 err = hpi_stream_host_buffer_attach(dpcm->h_stream,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200501 params_buffer_bytes(params), runtime->dma_addr);
502 if (err == 0) {
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300503 snd_printdd(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200504 "stream_host_buffer_attach succeeded %u %lu\n",
505 params_buffer_bytes(params),
506 (unsigned long)runtime->dma_addr);
507 } else {
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300508 snd_printd("stream_host_buffer_attach error %d\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200509 err);
510 return -ENOMEM;
511 }
512
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300513 err = hpi_stream_get_info_ex(dpcm->h_stream, NULL,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200514 &dpcm->hpi_buffer_attached,
515 NULL, NULL, NULL);
516
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300517 snd_printdd("stream_host_buffer_attach status 0x%x\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200518 dpcm->hpi_buffer_attached);
519 }
520 bytes_per_sec = params_rate(params) * params_channels(params);
Kulikov Vasiliy315e8f72010-07-15 22:48:19 +0400521 width = snd_pcm_format_width(params_format(params));
522 bytes_per_sec *= width;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200523 bytes_per_sec /= 8;
Kulikov Vasiliy315e8f72010-07-15 22:48:19 +0400524 if (width < 0 || bytes_per_sec == 0)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200525 return -EINVAL;
526
527 dpcm->bytes_per_sec = bytes_per_sec;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300528 dpcm->buffer_bytes = params_buffer_bytes(params);
529 dpcm->period_bytes = params_period_bytes(params);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200530
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200531 return 0;
532}
533
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300534static int
535snd_card_asihpi_hw_free(struct snd_pcm_substream *substream)
536{
537 struct snd_pcm_runtime *runtime = substream->runtime;
538 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
539 if (dpcm->hpi_buffer_attached)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300540 hpi_stream_host_buffer_detach(dpcm->h_stream);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300541
542 snd_pcm_lib_free_pages(substream);
543 return 0;
544}
545
546static void snd_card_asihpi_runtime_free(struct snd_pcm_runtime *runtime)
547{
548 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
549 kfree(dpcm);
550}
551
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200552static void snd_card_asihpi_pcm_timer_start(struct snd_pcm_substream *
553 substream)
554{
555 struct snd_pcm_runtime *runtime = substream->runtime;
556 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
557 int expiry;
558
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300559 expiry = HZ / 200;
560 /*? (dpcm->period_bytes * HZ / dpcm->bytes_per_sec); */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300561 expiry = max(expiry, 1); /* don't let it be zero! */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200562 dpcm->timer.expires = jiffies + expiry;
563 dpcm->respawn_timer = 1;
564 add_timer(&dpcm->timer);
565}
566
567static void snd_card_asihpi_pcm_timer_stop(struct snd_pcm_substream *substream)
568{
569 struct snd_pcm_runtime *runtime = substream->runtime;
570 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
571
572 dpcm->respawn_timer = 0;
573 del_timer(&dpcm->timer);
574}
575
576static int snd_card_asihpi_trigger(struct snd_pcm_substream *substream,
577 int cmd)
578{
579 struct snd_card_asihpi_pcm *dpcm = substream->runtime->private_data;
580 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
581 struct snd_pcm_substream *s;
582 u16 e;
Eliot Blennerhassett0a17e992011-07-22 15:52:44 +1200583 char name[16];
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200584
Eliot Blennerhassett0a17e992011-07-22 15:52:44 +1200585 snd_pcm_debug_name(substream, name, sizeof(name));
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200586 snd_printdd("%s trigger\n", name);
587
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200588 switch (cmd) {
589 case SNDRV_PCM_TRIGGER_START:
590 snd_pcm_group_for_each_entry(s, substream) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300591 struct snd_pcm_runtime *runtime = s->runtime;
592 struct snd_card_asihpi_pcm *ds = runtime->private_data;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200593
594 if (snd_pcm_substream_chip(s) != card)
595 continue;
596
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300597 /* don't link Cap and Play */
598 if (substream->stream != s->stream)
599 continue;
600
Eliot Blennerhassett0b7ce9e2011-04-05 20:55:43 +1200601 ds->drained_count = 0;
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200602 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200603 /* How do I know how much valid data is present
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300604 * in buffer? Must be at least one period!
605 * Guessing 2 periods, but if
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200606 * buffer is bigger it may contain even more
607 * data??
608 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300609 unsigned int preload = ds->period_bytes * 1;
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300610 snd_printddd("%d preload x%x\n", s->number, preload);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200611 hpi_handle_error(hpi_outstream_write_buf(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300612 ds->h_stream,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300613 &runtime->dma_area[0],
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200614 preload,
615 &ds->format));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300616 ds->pcm_buf_host_rw_ofs = preload;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200617 }
618
619 if (card->support_grouping) {
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200620 snd_printdd("%d group\n", s->number);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300621 e = hpi_stream_group_add(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200622 dpcm->h_stream,
623 ds->h_stream);
624 if (!e) {
625 snd_pcm_trigger_done(s, substream);
626 } else {
627 hpi_handle_error(e);
628 break;
629 }
630 } else
631 break;
632 }
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300633 snd_printdd("start\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200634 /* start the master stream */
635 snd_card_asihpi_pcm_timer_start(substream);
Eliot Blennerhassettc4ed97d2011-02-10 17:26:20 +1300636 if ((substream->stream == SNDRV_PCM_STREAM_CAPTURE) ||
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200637 !card->can_dma)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300638 hpi_handle_error(hpi_stream_start(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200639 break;
640
641 case SNDRV_PCM_TRIGGER_STOP:
642 snd_card_asihpi_pcm_timer_stop(substream);
643 snd_pcm_group_for_each_entry(s, substream) {
644 if (snd_pcm_substream_chip(s) != card)
645 continue;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300646 /* don't link Cap and Play */
647 if (substream->stream != s->stream)
648 continue;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200649
650 /*? workaround linked streams don't
651 transition to SETUP 20070706*/
652 s->runtime->status->state = SNDRV_PCM_STATE_SETUP;
653
654 if (card->support_grouping) {
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200655 snd_printdd("%d group\n", s->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200656 snd_pcm_trigger_done(s, substream);
657 } else
658 break;
659 }
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300660 snd_printdd("stop\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200661
662 /* _prepare and _hwparams reset the stream */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300663 hpi_handle_error(hpi_stream_stop(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200664 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
665 hpi_handle_error(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300666 hpi_outstream_reset(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200667
668 if (card->support_grouping)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300669 hpi_handle_error(hpi_stream_group_reset(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200670 break;
671
672 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300673 snd_printdd("pause release\n");
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300674 hpi_handle_error(hpi_stream_start(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200675 snd_card_asihpi_pcm_timer_start(substream);
676 break;
677 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300678 snd_printdd("pause\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200679 snd_card_asihpi_pcm_timer_stop(substream);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300680 hpi_handle_error(hpi_stream_stop(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200681 break;
682 default:
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300683 snd_printd(KERN_ERR "\tINVALID\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200684 return -EINVAL;
685 }
686
687 return 0;
688}
689
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200690/*algorithm outline
691 Without linking degenerates to getting single stream pos etc
692 Without mmap 2nd loop degenerates to snd_pcm_period_elapsed
693*/
694/*
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300695pcm_buf_dma_ofs=get_buf_pos(s);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200696for_each_linked_stream(s) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300697 pcm_buf_dma_ofs=get_buf_pos(s);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300698 min_buf_pos = modulo_min(min_buf_pos, pcm_buf_dma_ofs, buffer_bytes)
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300699 new_data = min(new_data, calc_new_data(pcm_buf_dma_ofs,irq_pos)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200700}
701timer.expires = jiffies + predict_next_period_ready(min_buf_pos);
702for_each_linked_stream(s) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300703 s->pcm_buf_dma_ofs = min_buf_pos;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300704 if (new_data > period_bytes) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200705 if (mmap) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300706 irq_pos = (irq_pos + period_bytes) % buffer_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200707 if (playback) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300708 write(period_bytes);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200709 } else {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300710 read(period_bytes);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200711 }
712 }
713 snd_pcm_period_elapsed(s);
714 }
715}
716*/
717
718/** Minimum of 2 modulo values. Works correctly when the difference between
719* the values is less than half the modulus
720*/
721static inline unsigned int modulo_min(unsigned int a, unsigned int b,
722 unsigned long int modulus)
723{
724 unsigned int result;
725 if (((a-b) % modulus) < (modulus/2))
726 result = b;
727 else
728 result = a;
729
730 return result;
731}
732
733/** Timer function, equivalent to interrupt service routine for cards
734*/
735static void snd_card_asihpi_timer_function(unsigned long data)
736{
737 struct snd_card_asihpi_pcm *dpcm = (struct snd_card_asihpi_pcm *)data;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300738 struct snd_pcm_substream *substream = dpcm->substream;
739 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200740 struct snd_pcm_runtime *runtime;
741 struct snd_pcm_substream *s;
742 unsigned int newdata = 0;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300743 unsigned int pcm_buf_dma_ofs, min_buf_pos = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200744 unsigned int remdata, xfercount, next_jiffies;
745 int first = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300746 int loops = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200747 u16 state;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300748 u32 buffer_size, bytes_avail, samples_played, on_card_bytes;
Eliot Blennerhassett0a17e992011-07-22 15:52:44 +1200749 char name[16];
750
751 snd_pcm_debug_name(substream, name, sizeof(name));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200752
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200753 snd_printdd("%s snd_card_asihpi_timer_function\n", name);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300754
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200755 /* find minimum newdata and buffer pos in group */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300756 snd_pcm_group_for_each_entry(s, substream) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200757 struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
758 runtime = s->runtime;
759
760 if (snd_pcm_substream_chip(s) != card)
761 continue;
762
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300763 /* don't link Cap and Play */
764 if (substream->stream != s->stream)
765 continue;
766
767 hpi_handle_error(hpi_stream_get_info_ex(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200768 ds->h_stream, &state,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300769 &buffer_size, &bytes_avail,
770 &samples_played, &on_card_bytes));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200771
772 /* number of bytes in on-card buffer */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300773 runtime->delay = on_card_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200774
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200775 if (!card->can_dma)
776 on_card_bytes = bytes_avail;
777
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300778 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300779 pcm_buf_dma_ofs = ds->pcm_buf_host_rw_ofs - bytes_avail;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300780 if (state == HPI_STATE_STOPPED) {
781 if ((bytes_avail == 0) &&
782 (on_card_bytes < ds->pcm_buf_host_rw_ofs)) {
783 hpi_handle_error(hpi_stream_start(ds->h_stream));
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300784 snd_printdd("P%d start\n", s->number);
Eliot Blennerhassett0b7ce9e2011-04-05 20:55:43 +1200785 ds->drained_count = 0;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300786 }
787 } else if (state == HPI_STATE_DRAINED) {
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300788 snd_printd(KERN_WARNING "P%d drained\n",
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300789 s->number);
Eliot Blennerhassett0b7ce9e2011-04-05 20:55:43 +1200790 ds->drained_count++;
791 if (ds->drained_count > 2) {
792 snd_pcm_stop(s, SNDRV_PCM_STATE_XRUN);
793 continue;
794 }
795 } else {
796 ds->drained_count = 0;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300797 }
798 } else
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300799 pcm_buf_dma_ofs = bytes_avail + ds->pcm_buf_host_rw_ofs;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200800
801 if (first) {
802 /* can't statically init min when wrap is involved */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300803 min_buf_pos = pcm_buf_dma_ofs;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300804 newdata = (pcm_buf_dma_ofs - ds->pcm_buf_elapsed_dma_ofs) % ds->buffer_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200805 first = 0;
806 } else {
807 min_buf_pos =
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300808 modulo_min(min_buf_pos, pcm_buf_dma_ofs, UINT_MAX+1L);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200809 newdata = min(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300810 (pcm_buf_dma_ofs - ds->pcm_buf_elapsed_dma_ofs) % ds->buffer_bytes,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200811 newdata);
812 }
813
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200814 snd_printdd("hw_ptr 0x%04lX, appl_ptr 0x%04lX\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200815 (unsigned long)frames_to_bytes(runtime,
816 runtime->status->hw_ptr),
817 (unsigned long)frames_to_bytes(runtime,
818 runtime->control->appl_ptr));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300819
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200820 snd_printdd("%d S=%d, "
821 "rw=0x%04X, dma=0x%04X, left=0x%04X, "
822 "aux=0x%04X space=0x%04X\n",
823 s->number, state,
824 ds->pcm_buf_host_rw_ofs, pcm_buf_dma_ofs,
825 (int)bytes_avail,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300826 (int)on_card_bytes, buffer_size-bytes_avail);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300827 loops++;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200828 }
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300829 pcm_buf_dma_ofs = min_buf_pos;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200830
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300831 remdata = newdata % dpcm->period_bytes;
832 xfercount = newdata - remdata; /* a multiple of period_bytes */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300833 /* come back when on_card_bytes has decreased enough to allow
834 write to happen, or when data has been consumed to make another
835 period
836 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300837 if (xfercount && (on_card_bytes > dpcm->period_bytes))
838 next_jiffies = ((on_card_bytes - dpcm->period_bytes) * HZ / dpcm->bytes_per_sec);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300839 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300840 next_jiffies = ((dpcm->period_bytes - remdata) * HZ / dpcm->bytes_per_sec);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300841
842 next_jiffies = max(next_jiffies, 1U);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200843 dpcm->timer.expires = jiffies + next_jiffies;
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200844 snd_printdd("jif %d buf pos 0x%04X newdata 0x%04X xfer 0x%04X\n",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300845 next_jiffies, pcm_buf_dma_ofs, newdata, xfercount);
846
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300847 snd_pcm_group_for_each_entry(s, substream) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200848 struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200849
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300850 /* don't link Cap and Play */
851 if (substream->stream != s->stream)
852 continue;
853
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300854 ds->pcm_buf_dma_ofs = pcm_buf_dma_ofs;
855
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200856 if (xfercount &&
857 /* Limit use of on card fifo for playback */
858 ((on_card_bytes <= ds->period_bytes) ||
859 (s->stream == SNDRV_PCM_STREAM_CAPTURE)))
860
861 {
862
863 unsigned int buf_ofs = ds->pcm_buf_host_rw_ofs % ds->buffer_bytes;
864 unsigned int xfer1, xfer2;
865 char *pd = &s->runtime->dma_area[buf_ofs];
866
Eliot Blennerhassettb0096a62011-04-05 20:55:46 +1200867 if (card->can_dma) { /* buffer wrap is handled at lower level */
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200868 xfer1 = xfercount;
869 xfer2 = 0;
870 } else {
871 xfer1 = min(xfercount, ds->buffer_bytes - buf_ofs);
872 xfer2 = xfercount - xfer1;
873 }
874
875 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
876 snd_printddd("P%d write1 0x%04X 0x%04X\n",
877 s->number, xfer1, buf_ofs);
878 hpi_handle_error(
879 hpi_outstream_write_buf(
880 ds->h_stream, pd, xfer1,
881 &ds->format));
882
883 if (xfer2) {
884 pd = s->runtime->dma_area;
885
886 snd_printddd("P%d write2 0x%04X 0x%04X\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200887 s->number,
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200888 xfercount - xfer1, buf_ofs);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200889 hpi_handle_error(
890 hpi_outstream_write_buf(
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200891 ds->h_stream, pd,
892 xfercount - xfer1,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200893 &ds->format));
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200894 }
895 } else {
896 snd_printddd("C%d read1 0x%04x\n",
897 s->number, xfer1);
898 hpi_handle_error(
899 hpi_instream_read_buf(
900 ds->h_stream,
901 pd, xfer1));
902 if (xfer2) {
903 pd = s->runtime->dma_area;
904 snd_printddd("C%d read2 0x%04x\n",
905 s->number, xfer2);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200906 hpi_handle_error(
907 hpi_instream_read_buf(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300908 ds->h_stream,
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200909 pd, xfer2));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200910 }
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200911 }
912 ds->pcm_buf_host_rw_ofs = ds->pcm_buf_host_rw_ofs + xfercount;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300913 ds->pcm_buf_elapsed_dma_ofs = pcm_buf_dma_ofs;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200914 snd_pcm_period_elapsed(s);
915 }
916 }
917
918 if (dpcm->respawn_timer)
919 add_timer(&dpcm->timer);
920}
921
922/***************************** PLAYBACK OPS ****************/
923static int snd_card_asihpi_playback_ioctl(struct snd_pcm_substream *substream,
924 unsigned int cmd, void *arg)
925{
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200926 snd_printddd(KERN_INFO "P%d ioctl %d\n", substream->number, cmd);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200927 return snd_pcm_lib_ioctl(substream, cmd, arg);
928}
929
930static int snd_card_asihpi_playback_prepare(struct snd_pcm_substream *
931 substream)
932{
933 struct snd_pcm_runtime *runtime = substream->runtime;
934 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
935
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200936 snd_printdd("P%d prepare\n", substream->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200937
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300938 hpi_handle_error(hpi_outstream_reset(dpcm->h_stream));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300939 dpcm->pcm_buf_host_rw_ofs = 0;
940 dpcm->pcm_buf_dma_ofs = 0;
941 dpcm->pcm_buf_elapsed_dma_ofs = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200942 return 0;
943}
944
945static snd_pcm_uframes_t
946snd_card_asihpi_playback_pointer(struct snd_pcm_substream *substream)
947{
948 struct snd_pcm_runtime *runtime = substream->runtime;
949 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
950 snd_pcm_uframes_t ptr;
951
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300952 ptr = bytes_to_frames(runtime, dpcm->pcm_buf_dma_ofs % dpcm->buffer_bytes);
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200953 snd_printddd("P%d pointer = 0x%04lx\n", substream->number, (unsigned long)ptr);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200954 return ptr;
955}
956
957static void snd_card_asihpi_playback_format(struct snd_card_asihpi *asihpi,
958 u32 h_stream,
959 struct snd_pcm_hardware *pcmhw)
960{
961 struct hpi_format hpi_format;
962 u16 format;
963 u16 err;
964 u32 h_control;
965 u32 sample_rate = 48000;
966
967 /* on cards without SRC, must query at valid rate,
968 * maybe set by external sync
969 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300970 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200971 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
972 HPI_CONTROL_SAMPLECLOCK, &h_control);
973
974 if (!err)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300975 err = hpi_sample_clock_get_sample_rate(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200976 &sample_rate);
977
978 for (format = HPI_FORMAT_PCM8_UNSIGNED;
979 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
980 err = hpi_format_create(&hpi_format,
981 2, format, sample_rate, 128000, 0);
982 if (!err)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300983 err = hpi_outstream_query_format(h_stream,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200984 &hpi_format);
985 if (!err && (hpi_to_alsa_formats[format] != -1))
986 pcmhw->formats |=
987 (1ULL << hpi_to_alsa_formats[format]);
988 }
989}
990
991static struct snd_pcm_hardware snd_card_asihpi_playback = {
992 .channels_min = 1,
993 .channels_max = 2,
994 .buffer_bytes_max = BUFFER_BYTES_MAX,
995 .period_bytes_min = PERIOD_BYTES_MIN,
996 .period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN,
997 .periods_min = PERIODS_MIN,
998 .periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN,
999 .fifo_size = 0,
1000};
1001
1002static int snd_card_asihpi_playback_open(struct snd_pcm_substream *substream)
1003{
1004 struct snd_pcm_runtime *runtime = substream->runtime;
1005 struct snd_card_asihpi_pcm *dpcm;
1006 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
1007 int err;
1008
1009 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
1010 if (dpcm == NULL)
1011 return -ENOMEM;
1012
1013 err =
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001014 hpi_outstream_open(card->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001015 substream->number, &dpcm->h_stream);
1016 hpi_handle_error(err);
1017 if (err)
1018 kfree(dpcm);
1019 if (err == HPI_ERROR_OBJ_ALREADY_OPEN)
1020 return -EBUSY;
1021 if (err)
1022 return -EIO;
1023
1024 /*? also check ASI5000 samplerate source
1025 If external, only support external rate.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001026 If internal and other stream playing, can't switch
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001027 */
1028
1029 init_timer(&dpcm->timer);
1030 dpcm->timer.data = (unsigned long) dpcm;
1031 dpcm->timer.function = snd_card_asihpi_timer_function;
1032 dpcm->substream = substream;
1033 runtime->private_data = dpcm;
1034 runtime->private_free = snd_card_asihpi_runtime_free;
1035
1036 snd_card_asihpi_playback.channels_max = card->out_max_chans;
1037 /*?snd_card_asihpi_playback.period_bytes_min =
1038 card->out_max_chans * 4096; */
1039
1040 snd_card_asihpi_playback_format(card, dpcm->h_stream,
1041 &snd_card_asihpi_playback);
1042
1043 snd_card_asihpi_pcm_samplerates(card, &snd_card_asihpi_playback);
1044
1045 snd_card_asihpi_playback.info = SNDRV_PCM_INFO_INTERLEAVED |
1046 SNDRV_PCM_INFO_DOUBLE |
1047 SNDRV_PCM_INFO_BATCH |
1048 SNDRV_PCM_INFO_BLOCK_TRANSFER |
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12001049 SNDRV_PCM_INFO_PAUSE |
1050 SNDRV_PCM_INFO_MMAP |
1051 SNDRV_PCM_INFO_MMAP_VALID;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001052
1053 if (card->support_grouping)
1054 snd_card_asihpi_playback.info |= SNDRV_PCM_INFO_SYNC_START;
1055
1056 /* struct is copied, so can create initializer dynamically */
1057 runtime->hw = snd_card_asihpi_playback;
1058
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12001059 if (card->can_dma)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001060 err = snd_pcm_hw_constraint_pow2(runtime, 0,
1061 SNDRV_PCM_HW_PARAM_BUFFER_BYTES);
1062 if (err < 0)
1063 return err;
1064
1065 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1066 card->update_interval_frames);
Eliot Blennerhassett26aebef2011-03-25 15:25:47 +13001067
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001068 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001069 card->update_interval_frames * 2, UINT_MAX);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001070
1071 snd_pcm_set_sync(substream);
1072
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001073 snd_printdd("playback open\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001074
1075 return 0;
1076}
1077
1078static int snd_card_asihpi_playback_close(struct snd_pcm_substream *substream)
1079{
1080 struct snd_pcm_runtime *runtime = substream->runtime;
1081 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1082
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001083 hpi_handle_error(hpi_outstream_close(dpcm->h_stream));
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001084 snd_printdd("playback close\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001085
1086 return 0;
1087}
1088
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001089static struct snd_pcm_ops snd_card_asihpi_playback_mmap_ops = {
1090 .open = snd_card_asihpi_playback_open,
1091 .close = snd_card_asihpi_playback_close,
1092 .ioctl = snd_card_asihpi_playback_ioctl,
1093 .hw_params = snd_card_asihpi_pcm_hw_params,
1094 .hw_free = snd_card_asihpi_hw_free,
1095 .prepare = snd_card_asihpi_playback_prepare,
1096 .trigger = snd_card_asihpi_trigger,
1097 .pointer = snd_card_asihpi_playback_pointer,
1098};
1099
1100/***************************** CAPTURE OPS ****************/
1101static snd_pcm_uframes_t
1102snd_card_asihpi_capture_pointer(struct snd_pcm_substream *substream)
1103{
1104 struct snd_pcm_runtime *runtime = substream->runtime;
1105 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1106
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001107 snd_printddd("capture pointer %d=%d\n",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001108 substream->number, dpcm->pcm_buf_dma_ofs);
1109 /* NOTE Unlike playback can't use actual samples_played
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001110 for the capture position, because those samples aren't yet in
1111 the local buffer available for reading.
1112 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001113 return bytes_to_frames(runtime, dpcm->pcm_buf_dma_ofs % dpcm->buffer_bytes);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001114}
1115
1116static int snd_card_asihpi_capture_ioctl(struct snd_pcm_substream *substream,
1117 unsigned int cmd, void *arg)
1118{
1119 return snd_pcm_lib_ioctl(substream, cmd, arg);
1120}
1121
1122static int snd_card_asihpi_capture_prepare(struct snd_pcm_substream *substream)
1123{
1124 struct snd_pcm_runtime *runtime = substream->runtime;
1125 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1126
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001127 hpi_handle_error(hpi_instream_reset(dpcm->h_stream));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001128 dpcm->pcm_buf_host_rw_ofs = 0;
1129 dpcm->pcm_buf_dma_ofs = 0;
1130 dpcm->pcm_buf_elapsed_dma_ofs = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001131
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001132 snd_printdd("Capture Prepare %d\n", substream->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001133 return 0;
1134}
1135
1136
1137
1138static void snd_card_asihpi_capture_format(struct snd_card_asihpi *asihpi,
1139 u32 h_stream,
1140 struct snd_pcm_hardware *pcmhw)
1141{
1142 struct hpi_format hpi_format;
1143 u16 format;
1144 u16 err;
1145 u32 h_control;
1146 u32 sample_rate = 48000;
1147
1148 /* on cards without SRC, must query at valid rate,
1149 maybe set by external sync */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001150 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001151 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
1152 HPI_CONTROL_SAMPLECLOCK, &h_control);
1153
1154 if (!err)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001155 err = hpi_sample_clock_get_sample_rate(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001156 &sample_rate);
1157
1158 for (format = HPI_FORMAT_PCM8_UNSIGNED;
1159 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
1160
1161 err = hpi_format_create(&hpi_format, 2, format,
1162 sample_rate, 128000, 0);
1163 if (!err)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001164 err = hpi_instream_query_format(h_stream,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001165 &hpi_format);
1166 if (!err)
1167 pcmhw->formats |=
1168 (1ULL << hpi_to_alsa_formats[format]);
1169 }
1170}
1171
1172
1173static struct snd_pcm_hardware snd_card_asihpi_capture = {
1174 .channels_min = 1,
1175 .channels_max = 2,
1176 .buffer_bytes_max = BUFFER_BYTES_MAX,
1177 .period_bytes_min = PERIOD_BYTES_MIN,
1178 .period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN,
1179 .periods_min = PERIODS_MIN,
1180 .periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN,
1181 .fifo_size = 0,
1182};
1183
1184static int snd_card_asihpi_capture_open(struct snd_pcm_substream *substream)
1185{
1186 struct snd_pcm_runtime *runtime = substream->runtime;
1187 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
1188 struct snd_card_asihpi_pcm *dpcm;
1189 int err;
1190
1191 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
1192 if (dpcm == NULL)
1193 return -ENOMEM;
1194
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001195 snd_printdd("capture open adapter %d stream %d\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001196 card->adapter_index, substream->number);
1197
1198 err = hpi_handle_error(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001199 hpi_instream_open(card->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001200 substream->number, &dpcm->h_stream));
1201 if (err)
1202 kfree(dpcm);
1203 if (err == HPI_ERROR_OBJ_ALREADY_OPEN)
1204 return -EBUSY;
1205 if (err)
1206 return -EIO;
1207
1208
1209 init_timer(&dpcm->timer);
1210 dpcm->timer.data = (unsigned long) dpcm;
1211 dpcm->timer.function = snd_card_asihpi_timer_function;
1212 dpcm->substream = substream;
1213 runtime->private_data = dpcm;
1214 runtime->private_free = snd_card_asihpi_runtime_free;
1215
1216 snd_card_asihpi_capture.channels_max = card->in_max_chans;
1217 snd_card_asihpi_capture_format(card, dpcm->h_stream,
1218 &snd_card_asihpi_capture);
1219 snd_card_asihpi_pcm_samplerates(card, &snd_card_asihpi_capture);
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12001220 snd_card_asihpi_capture.info = SNDRV_PCM_INFO_INTERLEAVED |
1221 SNDRV_PCM_INFO_MMAP |
1222 SNDRV_PCM_INFO_MMAP_VALID;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001223
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001224 if (card->support_grouping)
1225 snd_card_asihpi_capture.info |= SNDRV_PCM_INFO_SYNC_START;
1226
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001227 runtime->hw = snd_card_asihpi_capture;
1228
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12001229 if (card->can_dma)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001230 err = snd_pcm_hw_constraint_pow2(runtime, 0,
1231 SNDRV_PCM_HW_PARAM_BUFFER_BYTES);
1232 if (err < 0)
1233 return err;
1234
1235 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1236 card->update_interval_frames);
1237 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1238 card->update_interval_frames * 2, UINT_MAX);
1239
1240 snd_pcm_set_sync(substream);
1241
1242 return 0;
1243}
1244
1245static int snd_card_asihpi_capture_close(struct snd_pcm_substream *substream)
1246{
1247 struct snd_card_asihpi_pcm *dpcm = substream->runtime->private_data;
1248
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001249 hpi_handle_error(hpi_instream_close(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001250 return 0;
1251}
1252
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001253static struct snd_pcm_ops snd_card_asihpi_capture_mmap_ops = {
1254 .open = snd_card_asihpi_capture_open,
1255 .close = snd_card_asihpi_capture_close,
1256 .ioctl = snd_card_asihpi_capture_ioctl,
1257 .hw_params = snd_card_asihpi_pcm_hw_params,
1258 .hw_free = snd_card_asihpi_hw_free,
1259 .prepare = snd_card_asihpi_capture_prepare,
1260 .trigger = snd_card_asihpi_trigger,
1261 .pointer = snd_card_asihpi_capture_pointer,
1262};
1263
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001264static int __devinit snd_card_asihpi_pcm_new(struct snd_card_asihpi *asihpi,
1265 int device, int substreams)
1266{
1267 struct snd_pcm *pcm;
1268 int err;
1269
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001270 err = snd_pcm_new(asihpi->card, "Asihpi PCM", device,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001271 asihpi->num_outstreams, asihpi->num_instreams,
1272 &pcm);
1273 if (err < 0)
1274 return err;
1275 /* pointer to ops struct is stored, dont change ops afterwards! */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001276 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1277 &snd_card_asihpi_playback_mmap_ops);
1278 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1279 &snd_card_asihpi_capture_mmap_ops);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001280
1281 pcm->private_data = asihpi;
1282 pcm->info_flags = 0;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001283 strcpy(pcm->name, "Asihpi PCM");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001284
1285 /*? do we want to emulate MMAP for non-BBM cards?
1286 Jack doesn't work with ALSAs MMAP emulation - WHY NOT? */
1287 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1288 snd_dma_pci_data(asihpi->pci),
1289 64*1024, BUFFER_BYTES_MAX);
1290
1291 return 0;
1292}
1293
1294/***************************** MIXER CONTROLS ****************/
1295struct hpi_control {
1296 u32 h_control;
1297 u16 control_type;
1298 u16 src_node_type;
1299 u16 src_node_index;
1300 u16 dst_node_type;
1301 u16 dst_node_index;
1302 u16 band;
1303 char name[44]; /* copied to snd_ctl_elem_id.name[44]; */
1304};
1305
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001306static const char * const asihpi_tuner_band_names[] = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001307 "invalid",
1308 "AM",
1309 "FM mono",
1310 "TV NTSC-M",
1311 "FM stereo",
1312 "AUX",
1313 "TV PAL BG",
1314 "TV PAL I",
1315 "TV PAL DK",
1316 "TV SECAM",
1317};
1318
1319compile_time_assert(
1320 (ARRAY_SIZE(asihpi_tuner_band_names) ==
1321 (HPI_TUNER_BAND_LAST+1)),
1322 assert_tuner_band_names_size);
1323
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001324static const char * const asihpi_src_names[] = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001325 "no source",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001326 "PCM",
1327 "Line",
1328 "Digital",
1329 "Tuner",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001330 "RF",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001331 "Clock",
1332 "Bitstream",
1333 "Microphone",
1334 "Cobranet",
1335 "Analog",
1336 "Adapter",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001337};
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001338
1339compile_time_assert(
1340 (ARRAY_SIZE(asihpi_src_names) ==
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12001341 (HPI_SOURCENODE_LAST_INDEX-HPI_SOURCENODE_NONE+1)),
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001342 assert_src_names_size);
1343
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001344static const char * const asihpi_dst_names[] = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001345 "no destination",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001346 "PCM",
1347 "Line",
1348 "Digital",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001349 "RF",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001350 "Speaker",
1351 "Cobranet Out",
1352 "Analog"
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001353};
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001354
1355compile_time_assert(
1356 (ARRAY_SIZE(asihpi_dst_names) ==
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12001357 (HPI_DESTNODE_LAST_INDEX-HPI_DESTNODE_NONE+1)),
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001358 assert_dst_names_size);
1359
1360static inline int ctl_add(struct snd_card *card, struct snd_kcontrol_new *ctl,
1361 struct snd_card_asihpi *asihpi)
1362{
1363 int err;
1364
1365 err = snd_ctl_add(card, snd_ctl_new1(ctl, asihpi));
1366 if (err < 0)
1367 return err;
1368 else if (mixer_dump)
1369 snd_printk(KERN_INFO "added %s(%d)\n", ctl->name, ctl->index);
1370
1371 return 0;
1372}
1373
1374/* Convert HPI control name and location into ALSA control name */
1375static void asihpi_ctl_init(struct snd_kcontrol_new *snd_control,
1376 struct hpi_control *hpi_ctl,
1377 char *name)
1378{
Eliot Blennerhassett550ac6b2011-04-05 20:55:41 +12001379 char *dir;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001380 memset(snd_control, 0, sizeof(*snd_control));
1381 snd_control->name = hpi_ctl->name;
1382 snd_control->private_value = hpi_ctl->h_control;
1383 snd_control->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1384 snd_control->index = 0;
1385
Eliot Blennerhassett550ac6b2011-04-05 20:55:41 +12001386 if (hpi_ctl->src_node_type + HPI_SOURCENODE_NONE == HPI_SOURCENODE_CLOCK_SOURCE)
1387 dir = ""; /* clock is neither capture nor playback */
1388 else if (hpi_ctl->dst_node_type + HPI_DESTNODE_NONE == HPI_DESTNODE_ISTREAM)
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001389 dir = "Capture "; /* On or towards a PCM capture destination*/
1390 else if ((hpi_ctl->src_node_type + HPI_SOURCENODE_NONE != HPI_SOURCENODE_OSTREAM) &&
1391 (!hpi_ctl->dst_node_type))
1392 dir = "Capture "; /* On a source node that is not PCM playback */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001393 else if (hpi_ctl->src_node_type &&
1394 (hpi_ctl->src_node_type + HPI_SOURCENODE_NONE != HPI_SOURCENODE_OSTREAM) &&
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001395 (hpi_ctl->dst_node_type))
1396 dir = "Monitor Playback "; /* Between an input and an output */
1397 else
1398 dir = "Playback "; /* PCM Playback source, or output node */
1399
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001400 if (hpi_ctl->src_node_type && hpi_ctl->dst_node_type)
Eliot Blennerhassett550ac6b2011-04-05 20:55:41 +12001401 sprintf(hpi_ctl->name, "%s %d %s %d %s%s",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001402 asihpi_src_names[hpi_ctl->src_node_type],
1403 hpi_ctl->src_node_index,
1404 asihpi_dst_names[hpi_ctl->dst_node_type],
1405 hpi_ctl->dst_node_index,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001406 dir, name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001407 else if (hpi_ctl->dst_node_type) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001408 sprintf(hpi_ctl->name, "%s %d %s%s",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001409 asihpi_dst_names[hpi_ctl->dst_node_type],
1410 hpi_ctl->dst_node_index,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001411 dir, name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001412 } else {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001413 sprintf(hpi_ctl->name, "%s %d %s%s",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001414 asihpi_src_names[hpi_ctl->src_node_type],
1415 hpi_ctl->src_node_index,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001416 dir, name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001417 }
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001418 /* printk(KERN_INFO "Adding %s %d to %d ", hpi_ctl->name,
1419 hpi_ctl->wSrcNodeType, hpi_ctl->wDstNodeType); */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001420}
1421
1422/*------------------------------------------------------------
1423 Volume controls
1424 ------------------------------------------------------------*/
1425#define VOL_STEP_mB 1
1426static int snd_asihpi_volume_info(struct snd_kcontrol *kcontrol,
1427 struct snd_ctl_elem_info *uinfo)
1428{
1429 u32 h_control = kcontrol->private_value;
1430 u16 err;
1431 /* native gains are in millibels */
1432 short min_gain_mB;
1433 short max_gain_mB;
1434 short step_gain_mB;
1435
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001436 err = hpi_volume_query_range(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001437 &min_gain_mB, &max_gain_mB, &step_gain_mB);
1438 if (err) {
1439 max_gain_mB = 0;
1440 min_gain_mB = -10000;
1441 step_gain_mB = VOL_STEP_mB;
1442 }
1443
1444 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1445 uinfo->count = 2;
1446 uinfo->value.integer.min = min_gain_mB / VOL_STEP_mB;
1447 uinfo->value.integer.max = max_gain_mB / VOL_STEP_mB;
1448 uinfo->value.integer.step = step_gain_mB / VOL_STEP_mB;
1449 return 0;
1450}
1451
1452static int snd_asihpi_volume_get(struct snd_kcontrol *kcontrol,
1453 struct snd_ctl_elem_value *ucontrol)
1454{
1455 u32 h_control = kcontrol->private_value;
1456 short an_gain_mB[HPI_MAX_CHANNELS];
1457
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001458 hpi_handle_error(hpi_volume_get_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001459 ucontrol->value.integer.value[0] = an_gain_mB[0] / VOL_STEP_mB;
1460 ucontrol->value.integer.value[1] = an_gain_mB[1] / VOL_STEP_mB;
1461
1462 return 0;
1463}
1464
1465static int snd_asihpi_volume_put(struct snd_kcontrol *kcontrol,
1466 struct snd_ctl_elem_value *ucontrol)
1467{
1468 int change;
1469 u32 h_control = kcontrol->private_value;
1470 short an_gain_mB[HPI_MAX_CHANNELS];
1471
1472 an_gain_mB[0] =
1473 (ucontrol->value.integer.value[0]) * VOL_STEP_mB;
1474 an_gain_mB[1] =
1475 (ucontrol->value.integer.value[1]) * VOL_STEP_mB;
1476 /* change = asihpi->mixer_volume[addr][0] != left ||
1477 asihpi->mixer_volume[addr][1] != right;
1478 */
1479 change = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001480 hpi_handle_error(hpi_volume_set_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001481 return change;
1482}
1483
1484static const DECLARE_TLV_DB_SCALE(db_scale_100, -10000, VOL_STEP_mB, 0);
1485
1486static int __devinit snd_asihpi_volume_add(struct snd_card_asihpi *asihpi,
1487 struct hpi_control *hpi_ctl)
1488{
1489 struct snd_card *card = asihpi->card;
1490 struct snd_kcontrol_new snd_control;
1491
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001492 asihpi_ctl_init(&snd_control, hpi_ctl, "Volume");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001493 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1494 SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1495 snd_control.info = snd_asihpi_volume_info;
1496 snd_control.get = snd_asihpi_volume_get;
1497 snd_control.put = snd_asihpi_volume_put;
1498 snd_control.tlv.p = db_scale_100;
1499
1500 return ctl_add(card, &snd_control, asihpi);
1501}
1502
1503/*------------------------------------------------------------
1504 Level controls
1505 ------------------------------------------------------------*/
1506static int snd_asihpi_level_info(struct snd_kcontrol *kcontrol,
1507 struct snd_ctl_elem_info *uinfo)
1508{
1509 u32 h_control = kcontrol->private_value;
1510 u16 err;
1511 short min_gain_mB;
1512 short max_gain_mB;
1513 short step_gain_mB;
1514
1515 err =
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001516 hpi_level_query_range(h_control, &min_gain_mB,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001517 &max_gain_mB, &step_gain_mB);
1518 if (err) {
1519 max_gain_mB = 2400;
1520 min_gain_mB = -1000;
1521 step_gain_mB = 100;
1522 }
1523
1524 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1525 uinfo->count = 2;
1526 uinfo->value.integer.min = min_gain_mB / HPI_UNITS_PER_dB;
1527 uinfo->value.integer.max = max_gain_mB / HPI_UNITS_PER_dB;
1528 uinfo->value.integer.step = step_gain_mB / HPI_UNITS_PER_dB;
1529 return 0;
1530}
1531
1532static int snd_asihpi_level_get(struct snd_kcontrol *kcontrol,
1533 struct snd_ctl_elem_value *ucontrol)
1534{
1535 u32 h_control = kcontrol->private_value;
1536 short an_gain_mB[HPI_MAX_CHANNELS];
1537
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001538 hpi_handle_error(hpi_level_get_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001539 ucontrol->value.integer.value[0] =
1540 an_gain_mB[0] / HPI_UNITS_PER_dB;
1541 ucontrol->value.integer.value[1] =
1542 an_gain_mB[1] / HPI_UNITS_PER_dB;
1543
1544 return 0;
1545}
1546
1547static int snd_asihpi_level_put(struct snd_kcontrol *kcontrol,
1548 struct snd_ctl_elem_value *ucontrol)
1549{
1550 int change;
1551 u32 h_control = kcontrol->private_value;
1552 short an_gain_mB[HPI_MAX_CHANNELS];
1553
1554 an_gain_mB[0] =
1555 (ucontrol->value.integer.value[0]) * HPI_UNITS_PER_dB;
1556 an_gain_mB[1] =
1557 (ucontrol->value.integer.value[1]) * HPI_UNITS_PER_dB;
1558 /* change = asihpi->mixer_level[addr][0] != left ||
1559 asihpi->mixer_level[addr][1] != right;
1560 */
1561 change = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001562 hpi_handle_error(hpi_level_set_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001563 return change;
1564}
1565
1566static const DECLARE_TLV_DB_SCALE(db_scale_level, -1000, 100, 0);
1567
1568static int __devinit snd_asihpi_level_add(struct snd_card_asihpi *asihpi,
1569 struct hpi_control *hpi_ctl)
1570{
1571 struct snd_card *card = asihpi->card;
1572 struct snd_kcontrol_new snd_control;
1573
1574 /* can't use 'volume' cos some nodes have volume as well */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001575 asihpi_ctl_init(&snd_control, hpi_ctl, "Level");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001576 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1577 SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1578 snd_control.info = snd_asihpi_level_info;
1579 snd_control.get = snd_asihpi_level_get;
1580 snd_control.put = snd_asihpi_level_put;
1581 snd_control.tlv.p = db_scale_level;
1582
1583 return ctl_add(card, &snd_control, asihpi);
1584}
1585
1586/*------------------------------------------------------------
1587 AESEBU controls
1588 ------------------------------------------------------------*/
1589
1590/* AESEBU format */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001591static const char * const asihpi_aesebu_format_names[] = {
1592 "N/A", "S/PDIF", "AES/EBU" };
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001593
1594static int snd_asihpi_aesebu_format_info(struct snd_kcontrol *kcontrol,
1595 struct snd_ctl_elem_info *uinfo)
1596{
1597 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1598 uinfo->count = 1;
1599 uinfo->value.enumerated.items = 3;
1600
1601 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1602 uinfo->value.enumerated.item =
1603 uinfo->value.enumerated.items - 1;
1604
1605 strcpy(uinfo->value.enumerated.name,
1606 asihpi_aesebu_format_names[uinfo->value.enumerated.item]);
1607
1608 return 0;
1609}
1610
1611static int snd_asihpi_aesebu_format_get(struct snd_kcontrol *kcontrol,
1612 struct snd_ctl_elem_value *ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001613 u16 (*func)(u32, u16 *))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001614{
1615 u32 h_control = kcontrol->private_value;
1616 u16 source, err;
1617
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001618 err = func(h_control, &source);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001619
1620 /* default to N/A */
1621 ucontrol->value.enumerated.item[0] = 0;
1622 /* return success but set the control to N/A */
1623 if (err)
1624 return 0;
1625 if (source == HPI_AESEBU_FORMAT_SPDIF)
1626 ucontrol->value.enumerated.item[0] = 1;
1627 if (source == HPI_AESEBU_FORMAT_AESEBU)
1628 ucontrol->value.enumerated.item[0] = 2;
1629
1630 return 0;
1631}
1632
1633static int snd_asihpi_aesebu_format_put(struct snd_kcontrol *kcontrol,
1634 struct snd_ctl_elem_value *ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001635 u16 (*func)(u32, u16))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001636{
1637 u32 h_control = kcontrol->private_value;
1638
1639 /* default to S/PDIF */
1640 u16 source = HPI_AESEBU_FORMAT_SPDIF;
1641
1642 if (ucontrol->value.enumerated.item[0] == 1)
1643 source = HPI_AESEBU_FORMAT_SPDIF;
1644 if (ucontrol->value.enumerated.item[0] == 2)
1645 source = HPI_AESEBU_FORMAT_AESEBU;
1646
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001647 if (func(h_control, source) != 0)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001648 return -EINVAL;
1649
1650 return 1;
1651}
1652
1653static int snd_asihpi_aesebu_rx_format_get(struct snd_kcontrol *kcontrol,
1654 struct snd_ctl_elem_value *ucontrol) {
1655 return snd_asihpi_aesebu_format_get(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001656 hpi_aesebu_receiver_get_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001657}
1658
1659static int snd_asihpi_aesebu_rx_format_put(struct snd_kcontrol *kcontrol,
1660 struct snd_ctl_elem_value *ucontrol) {
1661 return snd_asihpi_aesebu_format_put(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001662 hpi_aesebu_receiver_set_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001663}
1664
1665static int snd_asihpi_aesebu_rxstatus_info(struct snd_kcontrol *kcontrol,
1666 struct snd_ctl_elem_info *uinfo)
1667{
1668 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1669 uinfo->count = 1;
1670
1671 uinfo->value.integer.min = 0;
1672 uinfo->value.integer.max = 0X1F;
1673 uinfo->value.integer.step = 1;
1674
1675 return 0;
1676}
1677
1678static int snd_asihpi_aesebu_rxstatus_get(struct snd_kcontrol *kcontrol,
1679 struct snd_ctl_elem_value *ucontrol) {
1680
1681 u32 h_control = kcontrol->private_value;
1682 u16 status;
1683
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001684 hpi_handle_error(hpi_aesebu_receiver_get_error_status(
1685 h_control, &status));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001686 ucontrol->value.integer.value[0] = status;
1687 return 0;
1688}
1689
1690static int __devinit snd_asihpi_aesebu_rx_add(struct snd_card_asihpi *asihpi,
1691 struct hpi_control *hpi_ctl)
1692{
1693 struct snd_card *card = asihpi->card;
1694 struct snd_kcontrol_new snd_control;
1695
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001696 asihpi_ctl_init(&snd_control, hpi_ctl, "Format");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001697 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1698 snd_control.info = snd_asihpi_aesebu_format_info;
1699 snd_control.get = snd_asihpi_aesebu_rx_format_get;
1700 snd_control.put = snd_asihpi_aesebu_rx_format_put;
1701
1702
1703 if (ctl_add(card, &snd_control, asihpi) < 0)
1704 return -EINVAL;
1705
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001706 asihpi_ctl_init(&snd_control, hpi_ctl, "Status");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001707 snd_control.access =
1708 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
1709 snd_control.info = snd_asihpi_aesebu_rxstatus_info;
1710 snd_control.get = snd_asihpi_aesebu_rxstatus_get;
1711
1712 return ctl_add(card, &snd_control, asihpi);
1713}
1714
1715static int snd_asihpi_aesebu_tx_format_get(struct snd_kcontrol *kcontrol,
1716 struct snd_ctl_elem_value *ucontrol) {
1717 return snd_asihpi_aesebu_format_get(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001718 hpi_aesebu_transmitter_get_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001719}
1720
1721static int snd_asihpi_aesebu_tx_format_put(struct snd_kcontrol *kcontrol,
1722 struct snd_ctl_elem_value *ucontrol) {
1723 return snd_asihpi_aesebu_format_put(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001724 hpi_aesebu_transmitter_set_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001725}
1726
1727
1728static int __devinit snd_asihpi_aesebu_tx_add(struct snd_card_asihpi *asihpi,
1729 struct hpi_control *hpi_ctl)
1730{
1731 struct snd_card *card = asihpi->card;
1732 struct snd_kcontrol_new snd_control;
1733
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001734 asihpi_ctl_init(&snd_control, hpi_ctl, "Format");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001735 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1736 snd_control.info = snd_asihpi_aesebu_format_info;
1737 snd_control.get = snd_asihpi_aesebu_tx_format_get;
1738 snd_control.put = snd_asihpi_aesebu_tx_format_put;
1739
1740 return ctl_add(card, &snd_control, asihpi);
1741}
1742
1743/*------------------------------------------------------------
1744 Tuner controls
1745 ------------------------------------------------------------*/
1746
1747/* Gain */
1748
1749static int snd_asihpi_tuner_gain_info(struct snd_kcontrol *kcontrol,
1750 struct snd_ctl_elem_info *uinfo)
1751{
1752 u32 h_control = kcontrol->private_value;
1753 u16 err;
1754 short idx;
1755 u16 gain_range[3];
1756
1757 for (idx = 0; idx < 3; idx++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001758 err = hpi_tuner_query_gain(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001759 idx, &gain_range[idx]);
1760 if (err != 0)
1761 return err;
1762 }
1763
1764 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1765 uinfo->count = 1;
1766 uinfo->value.integer.min = ((int)gain_range[0]) / HPI_UNITS_PER_dB;
1767 uinfo->value.integer.max = ((int)gain_range[1]) / HPI_UNITS_PER_dB;
1768 uinfo->value.integer.step = ((int) gain_range[2]) / HPI_UNITS_PER_dB;
1769 return 0;
1770}
1771
1772static int snd_asihpi_tuner_gain_get(struct snd_kcontrol *kcontrol,
1773 struct snd_ctl_elem_value *ucontrol)
1774{
1775 /*
1776 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1777 */
1778 u32 h_control = kcontrol->private_value;
1779 short gain;
1780
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001781 hpi_handle_error(hpi_tuner_get_gain(h_control, &gain));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001782 ucontrol->value.integer.value[0] = gain / HPI_UNITS_PER_dB;
1783
1784 return 0;
1785}
1786
1787static int snd_asihpi_tuner_gain_put(struct snd_kcontrol *kcontrol,
1788 struct snd_ctl_elem_value *ucontrol)
1789{
1790 /*
1791 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1792 */
1793 u32 h_control = kcontrol->private_value;
1794 short gain;
1795
1796 gain = (ucontrol->value.integer.value[0]) * HPI_UNITS_PER_dB;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001797 hpi_handle_error(hpi_tuner_set_gain(h_control, gain));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001798
1799 return 1;
1800}
1801
1802/* Band */
1803
1804static int asihpi_tuner_band_query(struct snd_kcontrol *kcontrol,
1805 u16 *band_list, u32 len) {
1806 u32 h_control = kcontrol->private_value;
1807 u16 err = 0;
1808 u32 i;
1809
1810 for (i = 0; i < len; i++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001811 err = hpi_tuner_query_band(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001812 h_control, i, &band_list[i]);
1813 if (err != 0)
1814 break;
1815 }
1816
1817 if (err && (err != HPI_ERROR_INVALID_OBJ_INDEX))
1818 return -EIO;
1819
1820 return i;
1821}
1822
1823static int snd_asihpi_tuner_band_info(struct snd_kcontrol *kcontrol,
1824 struct snd_ctl_elem_info *uinfo)
1825{
1826 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1827 int num_bands = 0;
1828
1829 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1830 HPI_TUNER_BAND_LAST);
1831
1832 if (num_bands < 0)
1833 return num_bands;
1834
1835 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1836 uinfo->count = 1;
1837 uinfo->value.enumerated.items = num_bands;
1838
1839 if (num_bands > 0) {
1840 if (uinfo->value.enumerated.item >=
1841 uinfo->value.enumerated.items)
1842 uinfo->value.enumerated.item =
1843 uinfo->value.enumerated.items - 1;
1844
1845 strcpy(uinfo->value.enumerated.name,
1846 asihpi_tuner_band_names[
1847 tuner_bands[uinfo->value.enumerated.item]]);
1848
1849 }
1850 return 0;
1851}
1852
1853static int snd_asihpi_tuner_band_get(struct snd_kcontrol *kcontrol,
1854 struct snd_ctl_elem_value *ucontrol)
1855{
1856 u32 h_control = kcontrol->private_value;
1857 /*
1858 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1859 */
1860 u16 band, idx;
1861 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1862 u32 num_bands = 0;
1863
1864 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1865 HPI_TUNER_BAND_LAST);
1866
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001867 hpi_handle_error(hpi_tuner_get_band(h_control, &band));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001868
1869 ucontrol->value.enumerated.item[0] = -1;
1870 for (idx = 0; idx < HPI_TUNER_BAND_LAST; idx++)
1871 if (tuner_bands[idx] == band) {
1872 ucontrol->value.enumerated.item[0] = idx;
1873 break;
1874 }
1875
1876 return 0;
1877}
1878
1879static int snd_asihpi_tuner_band_put(struct snd_kcontrol *kcontrol,
1880 struct snd_ctl_elem_value *ucontrol)
1881{
1882 /*
1883 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1884 */
1885 u32 h_control = kcontrol->private_value;
1886 u16 band;
1887 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1888 u32 num_bands = 0;
1889
1890 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1891 HPI_TUNER_BAND_LAST);
1892
1893 band = tuner_bands[ucontrol->value.enumerated.item[0]];
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001894 hpi_handle_error(hpi_tuner_set_band(h_control, band));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001895
1896 return 1;
1897}
1898
1899/* Freq */
1900
1901static int snd_asihpi_tuner_freq_info(struct snd_kcontrol *kcontrol,
1902 struct snd_ctl_elem_info *uinfo)
1903{
1904 u32 h_control = kcontrol->private_value;
1905 u16 err;
1906 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1907 u16 num_bands = 0, band_iter, idx;
1908 u32 freq_range[3], temp_freq_range[3];
1909
1910 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1911 HPI_TUNER_BAND_LAST);
1912
1913 freq_range[0] = INT_MAX;
1914 freq_range[1] = 0;
1915 freq_range[2] = INT_MAX;
1916
1917 for (band_iter = 0; band_iter < num_bands; band_iter++) {
1918 for (idx = 0; idx < 3; idx++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001919 err = hpi_tuner_query_frequency(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001920 idx, tuner_bands[band_iter],
1921 &temp_freq_range[idx]);
1922 if (err != 0)
1923 return err;
1924 }
1925
1926 /* skip band with bogus stepping */
1927 if (temp_freq_range[2] <= 0)
1928 continue;
1929
1930 if (temp_freq_range[0] < freq_range[0])
1931 freq_range[0] = temp_freq_range[0];
1932 if (temp_freq_range[1] > freq_range[1])
1933 freq_range[1] = temp_freq_range[1];
1934 if (temp_freq_range[2] < freq_range[2])
1935 freq_range[2] = temp_freq_range[2];
1936 }
1937
1938 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1939 uinfo->count = 1;
1940 uinfo->value.integer.min = ((int)freq_range[0]);
1941 uinfo->value.integer.max = ((int)freq_range[1]);
1942 uinfo->value.integer.step = ((int)freq_range[2]);
1943 return 0;
1944}
1945
1946static int snd_asihpi_tuner_freq_get(struct snd_kcontrol *kcontrol,
1947 struct snd_ctl_elem_value *ucontrol)
1948{
1949 u32 h_control = kcontrol->private_value;
1950 u32 freq;
1951
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001952 hpi_handle_error(hpi_tuner_get_frequency(h_control, &freq));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001953 ucontrol->value.integer.value[0] = freq;
1954
1955 return 0;
1956}
1957
1958static int snd_asihpi_tuner_freq_put(struct snd_kcontrol *kcontrol,
1959 struct snd_ctl_elem_value *ucontrol)
1960{
1961 u32 h_control = kcontrol->private_value;
1962 u32 freq;
1963
1964 freq = ucontrol->value.integer.value[0];
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001965 hpi_handle_error(hpi_tuner_set_frequency(h_control, freq));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001966
1967 return 1;
1968}
1969
1970/* Tuner control group initializer */
1971static int __devinit snd_asihpi_tuner_add(struct snd_card_asihpi *asihpi,
1972 struct hpi_control *hpi_ctl)
1973{
1974 struct snd_card *card = asihpi->card;
1975 struct snd_kcontrol_new snd_control;
1976
1977 snd_control.private_value = hpi_ctl->h_control;
1978 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1979
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001980 if (!hpi_tuner_get_gain(hpi_ctl->h_control, NULL)) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001981 asihpi_ctl_init(&snd_control, hpi_ctl, "Gain");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001982 snd_control.info = snd_asihpi_tuner_gain_info;
1983 snd_control.get = snd_asihpi_tuner_gain_get;
1984 snd_control.put = snd_asihpi_tuner_gain_put;
1985
1986 if (ctl_add(card, &snd_control, asihpi) < 0)
1987 return -EINVAL;
1988 }
1989
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001990 asihpi_ctl_init(&snd_control, hpi_ctl, "Band");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001991 snd_control.info = snd_asihpi_tuner_band_info;
1992 snd_control.get = snd_asihpi_tuner_band_get;
1993 snd_control.put = snd_asihpi_tuner_band_put;
1994
1995 if (ctl_add(card, &snd_control, asihpi) < 0)
1996 return -EINVAL;
1997
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001998 asihpi_ctl_init(&snd_control, hpi_ctl, "Freq");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001999 snd_control.info = snd_asihpi_tuner_freq_info;
2000 snd_control.get = snd_asihpi_tuner_freq_get;
2001 snd_control.put = snd_asihpi_tuner_freq_put;
2002
2003 return ctl_add(card, &snd_control, asihpi);
2004}
2005
2006/*------------------------------------------------------------
2007 Meter controls
2008 ------------------------------------------------------------*/
2009static int snd_asihpi_meter_info(struct snd_kcontrol *kcontrol,
2010 struct snd_ctl_elem_info *uinfo)
2011{
2012 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2013 uinfo->count = HPI_MAX_CHANNELS;
2014 uinfo->value.integer.min = 0;
2015 uinfo->value.integer.max = 0x7FFFFFFF;
2016 return 0;
2017}
2018
2019/* linear values for 10dB steps */
2020static int log2lin[] = {
2021 0x7FFFFFFF, /* 0dB */
2022 679093956,
2023 214748365,
2024 67909396,
2025 21474837,
2026 6790940,
2027 2147484, /* -60dB */
2028 679094,
2029 214748, /* -80 */
2030 67909,
2031 21475, /* -100 */
2032 6791,
2033 2147,
2034 679,
2035 214,
2036 68,
2037 21,
2038 7,
2039 2
2040};
2041
2042static int snd_asihpi_meter_get(struct snd_kcontrol *kcontrol,
2043 struct snd_ctl_elem_value *ucontrol)
2044{
2045 u32 h_control = kcontrol->private_value;
2046 short an_gain_mB[HPI_MAX_CHANNELS], i;
2047 u16 err;
2048
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002049 err = hpi_meter_get_peak(h_control, an_gain_mB);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002050
2051 for (i = 0; i < HPI_MAX_CHANNELS; i++) {
2052 if (err) {
2053 ucontrol->value.integer.value[i] = 0;
2054 } else if (an_gain_mB[i] >= 0) {
2055 ucontrol->value.integer.value[i] =
2056 an_gain_mB[i] << 16;
2057 } else {
2058 /* -ve is log value in millibels < -60dB,
2059 * convert to (roughly!) linear,
2060 */
2061 ucontrol->value.integer.value[i] =
2062 log2lin[an_gain_mB[i] / -1000];
2063 }
2064 }
2065 return 0;
2066}
2067
2068static int __devinit snd_asihpi_meter_add(struct snd_card_asihpi *asihpi,
2069 struct hpi_control *hpi_ctl, int subidx)
2070{
2071 struct snd_card *card = asihpi->card;
2072 struct snd_kcontrol_new snd_control;
2073
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002074 asihpi_ctl_init(&snd_control, hpi_ctl, "Meter");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002075 snd_control.access =
2076 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
2077 snd_control.info = snd_asihpi_meter_info;
2078 snd_control.get = snd_asihpi_meter_get;
2079
2080 snd_control.index = subidx;
2081
2082 return ctl_add(card, &snd_control, asihpi);
2083}
2084
2085/*------------------------------------------------------------
2086 Multiplexer controls
2087 ------------------------------------------------------------*/
2088static int snd_card_asihpi_mux_count_sources(struct snd_kcontrol *snd_control)
2089{
2090 u32 h_control = snd_control->private_value;
2091 struct hpi_control hpi_ctl;
2092 int s, err;
2093 for (s = 0; s < 32; s++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002094 err = hpi_multiplexer_query_source(h_control, s,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002095 &hpi_ctl.
2096 src_node_type,
2097 &hpi_ctl.
2098 src_node_index);
2099 if (err)
2100 break;
2101 }
2102 return s;
2103}
2104
2105static int snd_asihpi_mux_info(struct snd_kcontrol *kcontrol,
2106 struct snd_ctl_elem_info *uinfo)
2107{
2108 int err;
2109 u16 src_node_type, src_node_index;
2110 u32 h_control = kcontrol->private_value;
2111
2112 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2113 uinfo->count = 1;
2114 uinfo->value.enumerated.items =
2115 snd_card_asihpi_mux_count_sources(kcontrol);
2116
2117 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2118 uinfo->value.enumerated.item =
2119 uinfo->value.enumerated.items - 1;
2120
2121 err =
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002122 hpi_multiplexer_query_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002123 uinfo->value.enumerated.item,
2124 &src_node_type, &src_node_index);
2125
2126 sprintf(uinfo->value.enumerated.name, "%s %d",
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12002127 asihpi_src_names[src_node_type - HPI_SOURCENODE_NONE],
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002128 src_node_index);
2129 return 0;
2130}
2131
2132static int snd_asihpi_mux_get(struct snd_kcontrol *kcontrol,
2133 struct snd_ctl_elem_value *ucontrol)
2134{
2135 u32 h_control = kcontrol->private_value;
2136 u16 source_type, source_index;
2137 u16 src_node_type, src_node_index;
2138 int s;
2139
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002140 hpi_handle_error(hpi_multiplexer_get_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002141 &source_type, &source_index));
2142 /* Should cache this search result! */
2143 for (s = 0; s < 256; s++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002144 if (hpi_multiplexer_query_source(h_control, s,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002145 &src_node_type, &src_node_index))
2146 break;
2147
2148 if ((source_type == src_node_type)
2149 && (source_index == src_node_index)) {
2150 ucontrol->value.enumerated.item[0] = s;
2151 return 0;
2152 }
2153 }
2154 snd_printd(KERN_WARNING
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002155 "Control %x failed to match mux source %hu %hu\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002156 h_control, source_type, source_index);
2157 ucontrol->value.enumerated.item[0] = 0;
2158 return 0;
2159}
2160
2161static int snd_asihpi_mux_put(struct snd_kcontrol *kcontrol,
2162 struct snd_ctl_elem_value *ucontrol)
2163{
2164 int change;
2165 u32 h_control = kcontrol->private_value;
2166 u16 source_type, source_index;
2167 u16 e;
2168
2169 change = 1;
2170
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002171 e = hpi_multiplexer_query_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002172 ucontrol->value.enumerated.item[0],
2173 &source_type, &source_index);
2174 if (!e)
2175 hpi_handle_error(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002176 hpi_multiplexer_set_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002177 source_type, source_index));
2178 return change;
2179}
2180
2181
2182static int __devinit snd_asihpi_mux_add(struct snd_card_asihpi *asihpi,
2183 struct hpi_control *hpi_ctl)
2184{
2185 struct snd_card *card = asihpi->card;
2186 struct snd_kcontrol_new snd_control;
2187
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002188 asihpi_ctl_init(&snd_control, hpi_ctl, "Route");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002189 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2190 snd_control.info = snd_asihpi_mux_info;
2191 snd_control.get = snd_asihpi_mux_get;
2192 snd_control.put = snd_asihpi_mux_put;
2193
2194 return ctl_add(card, &snd_control, asihpi);
2195
2196}
2197
2198/*------------------------------------------------------------
2199 Channel mode controls
2200 ------------------------------------------------------------*/
2201static int snd_asihpi_cmode_info(struct snd_kcontrol *kcontrol,
2202 struct snd_ctl_elem_info *uinfo)
2203{
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002204 static const char * const mode_names[HPI_CHANNEL_MODE_LAST + 1] = {
2205 "invalid",
2206 "Normal", "Swap",
2207 "From Left", "From Right",
2208 "To Left", "To Right"
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002209 };
2210
2211 u32 h_control = kcontrol->private_value;
2212 u16 mode;
2213 int i;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002214 u16 mode_map[6];
2215 int valid_modes = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002216
2217 /* HPI channel mode values can be from 1 to 6
2218 Some adapters only support a contiguous subset
2219 */
2220 for (i = 0; i < HPI_CHANNEL_MODE_LAST; i++)
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002221 if (!hpi_channel_mode_query_mode(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002222 h_control, i, &mode)) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002223 mode_map[valid_modes] = mode;
2224 valid_modes++;
2225 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002226
2227 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2228 uinfo->count = 1;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002229 uinfo->value.enumerated.items = valid_modes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002230
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002231 if (uinfo->value.enumerated.item >= valid_modes)
2232 uinfo->value.enumerated.item = valid_modes - 1;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002233
2234 strcpy(uinfo->value.enumerated.name,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002235 mode_names[mode_map[uinfo->value.enumerated.item]]);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002236
2237 return 0;
2238}
2239
2240static int snd_asihpi_cmode_get(struct snd_kcontrol *kcontrol,
2241 struct snd_ctl_elem_value *ucontrol)
2242{
2243 u32 h_control = kcontrol->private_value;
2244 u16 mode;
2245
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002246 if (hpi_channel_mode_get(h_control, &mode))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002247 mode = 1;
2248
2249 ucontrol->value.enumerated.item[0] = mode - 1;
2250
2251 return 0;
2252}
2253
2254static int snd_asihpi_cmode_put(struct snd_kcontrol *kcontrol,
2255 struct snd_ctl_elem_value *ucontrol)
2256{
2257 int change;
2258 u32 h_control = kcontrol->private_value;
2259
2260 change = 1;
2261
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002262 hpi_handle_error(hpi_channel_mode_set(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002263 ucontrol->value.enumerated.item[0] + 1));
2264 return change;
2265}
2266
2267
2268static int __devinit snd_asihpi_cmode_add(struct snd_card_asihpi *asihpi,
2269 struct hpi_control *hpi_ctl)
2270{
2271 struct snd_card *card = asihpi->card;
2272 struct snd_kcontrol_new snd_control;
2273
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002274 asihpi_ctl_init(&snd_control, hpi_ctl, "Mode");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002275 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2276 snd_control.info = snd_asihpi_cmode_info;
2277 snd_control.get = snd_asihpi_cmode_get;
2278 snd_control.put = snd_asihpi_cmode_put;
2279
2280 return ctl_add(card, &snd_control, asihpi);
2281}
2282
2283/*------------------------------------------------------------
2284 Sampleclock source controls
2285 ------------------------------------------------------------*/
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002286static char *sampleclock_sources[MAX_CLOCKSOURCES] = {
2287 "N/A", "Local PLL", "Digital Sync", "Word External", "Word Header",
2288 "SMPTE", "Digital1", "Auto", "Network", "Invalid",
2289 "Prev Module",
2290 "Digital2", "Digital3", "Digital4", "Digital5",
2291 "Digital6", "Digital7", "Digital8"};
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002292
2293static int snd_asihpi_clksrc_info(struct snd_kcontrol *kcontrol,
2294 struct snd_ctl_elem_info *uinfo)
2295{
2296 struct snd_card_asihpi *asihpi =
2297 (struct snd_card_asihpi *)(kcontrol->private_data);
2298 struct clk_cache *clkcache = &asihpi->cc;
2299 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2300 uinfo->count = 1;
2301 uinfo->value.enumerated.items = clkcache->count;
2302
2303 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2304 uinfo->value.enumerated.item =
2305 uinfo->value.enumerated.items - 1;
2306
2307 strcpy(uinfo->value.enumerated.name,
2308 clkcache->s[uinfo->value.enumerated.item].name);
2309 return 0;
2310}
2311
2312static int snd_asihpi_clksrc_get(struct snd_kcontrol *kcontrol,
2313 struct snd_ctl_elem_value *ucontrol)
2314{
2315 struct snd_card_asihpi *asihpi =
2316 (struct snd_card_asihpi *)(kcontrol->private_data);
2317 struct clk_cache *clkcache = &asihpi->cc;
2318 u32 h_control = kcontrol->private_value;
2319 u16 source, srcindex = 0;
2320 int i;
2321
2322 ucontrol->value.enumerated.item[0] = 0;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002323 if (hpi_sample_clock_get_source(h_control, &source))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002324 source = 0;
2325
2326 if (source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002327 if (hpi_sample_clock_get_source_index(h_control, &srcindex))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002328 srcindex = 0;
2329
2330 for (i = 0; i < clkcache->count; i++)
2331 if ((clkcache->s[i].source == source) &&
2332 (clkcache->s[i].index == srcindex))
2333 break;
2334
2335 ucontrol->value.enumerated.item[0] = i;
2336
2337 return 0;
2338}
2339
2340static int snd_asihpi_clksrc_put(struct snd_kcontrol *kcontrol,
2341 struct snd_ctl_elem_value *ucontrol)
2342{
2343 struct snd_card_asihpi *asihpi =
2344 (struct snd_card_asihpi *)(kcontrol->private_data);
2345 struct clk_cache *clkcache = &asihpi->cc;
2346 int change, item;
2347 u32 h_control = kcontrol->private_value;
2348
2349 change = 1;
2350 item = ucontrol->value.enumerated.item[0];
2351 if (item >= clkcache->count)
2352 item = clkcache->count-1;
2353
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002354 hpi_handle_error(hpi_sample_clock_set_source(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002355 h_control, clkcache->s[item].source));
2356
2357 if (clkcache->s[item].source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002358 hpi_handle_error(hpi_sample_clock_set_source_index(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002359 h_control, clkcache->s[item].index));
2360 return change;
2361}
2362
2363/*------------------------------------------------------------
2364 Clkrate controls
2365 ------------------------------------------------------------*/
2366/* Need to change this to enumerated control with list of rates */
2367static int snd_asihpi_clklocal_info(struct snd_kcontrol *kcontrol,
2368 struct snd_ctl_elem_info *uinfo)
2369{
2370 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2371 uinfo->count = 1;
2372 uinfo->value.integer.min = 8000;
2373 uinfo->value.integer.max = 192000;
2374 uinfo->value.integer.step = 100;
2375
2376 return 0;
2377}
2378
2379static int snd_asihpi_clklocal_get(struct snd_kcontrol *kcontrol,
2380 struct snd_ctl_elem_value *ucontrol)
2381{
2382 u32 h_control = kcontrol->private_value;
2383 u32 rate;
2384 u16 e;
2385
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002386 e = hpi_sample_clock_get_local_rate(h_control, &rate);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002387 if (!e)
2388 ucontrol->value.integer.value[0] = rate;
2389 else
2390 ucontrol->value.integer.value[0] = 0;
2391 return 0;
2392}
2393
2394static int snd_asihpi_clklocal_put(struct snd_kcontrol *kcontrol,
2395 struct snd_ctl_elem_value *ucontrol)
2396{
2397 int change;
2398 u32 h_control = kcontrol->private_value;
2399
2400 /* change = asihpi->mixer_clkrate[addr][0] != left ||
2401 asihpi->mixer_clkrate[addr][1] != right;
2402 */
2403 change = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002404 hpi_handle_error(hpi_sample_clock_set_local_rate(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002405 ucontrol->value.integer.value[0]));
2406 return change;
2407}
2408
2409static int snd_asihpi_clkrate_info(struct snd_kcontrol *kcontrol,
2410 struct snd_ctl_elem_info *uinfo)
2411{
2412 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2413 uinfo->count = 1;
2414 uinfo->value.integer.min = 8000;
2415 uinfo->value.integer.max = 192000;
2416 uinfo->value.integer.step = 100;
2417
2418 return 0;
2419}
2420
2421static int snd_asihpi_clkrate_get(struct snd_kcontrol *kcontrol,
2422 struct snd_ctl_elem_value *ucontrol)
2423{
2424 u32 h_control = kcontrol->private_value;
2425 u32 rate;
2426 u16 e;
2427
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002428 e = hpi_sample_clock_get_sample_rate(h_control, &rate);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002429 if (!e)
2430 ucontrol->value.integer.value[0] = rate;
2431 else
2432 ucontrol->value.integer.value[0] = 0;
2433 return 0;
2434}
2435
2436static int __devinit snd_asihpi_sampleclock_add(struct snd_card_asihpi *asihpi,
2437 struct hpi_control *hpi_ctl)
2438{
2439 struct snd_card *card = asihpi->card;
2440 struct snd_kcontrol_new snd_control;
2441
2442 struct clk_cache *clkcache = &asihpi->cc;
2443 u32 hSC = hpi_ctl->h_control;
2444 int has_aes_in = 0;
2445 int i, j;
2446 u16 source;
2447
2448 snd_control.private_value = hpi_ctl->h_control;
2449
2450 clkcache->has_local = 0;
2451
2452 for (i = 0; i <= HPI_SAMPLECLOCK_SOURCE_LAST; i++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002453 if (hpi_sample_clock_query_source(hSC,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002454 i, &source))
2455 break;
2456 clkcache->s[i].source = source;
2457 clkcache->s[i].index = 0;
2458 clkcache->s[i].name = sampleclock_sources[source];
2459 if (source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
2460 has_aes_in = 1;
2461 if (source == HPI_SAMPLECLOCK_SOURCE_LOCAL)
2462 clkcache->has_local = 1;
2463 }
2464 if (has_aes_in)
2465 /* already will have picked up index 0 above */
2466 for (j = 1; j < 8; j++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002467 if (hpi_sample_clock_query_source_index(hSC,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002468 j, HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT,
2469 &source))
2470 break;
2471 clkcache->s[i].source =
2472 HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT;
2473 clkcache->s[i].index = j;
2474 clkcache->s[i].name = sampleclock_sources[
2475 j+HPI_SAMPLECLOCK_SOURCE_LAST];
2476 i++;
2477 }
2478 clkcache->count = i;
2479
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002480 asihpi_ctl_init(&snd_control, hpi_ctl, "Source");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002481 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE ;
2482 snd_control.info = snd_asihpi_clksrc_info;
2483 snd_control.get = snd_asihpi_clksrc_get;
2484 snd_control.put = snd_asihpi_clksrc_put;
2485 if (ctl_add(card, &snd_control, asihpi) < 0)
2486 return -EINVAL;
2487
2488
2489 if (clkcache->has_local) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002490 asihpi_ctl_init(&snd_control, hpi_ctl, "Localrate");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002491 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE ;
2492 snd_control.info = snd_asihpi_clklocal_info;
2493 snd_control.get = snd_asihpi_clklocal_get;
2494 snd_control.put = snd_asihpi_clklocal_put;
2495
2496
2497 if (ctl_add(card, &snd_control, asihpi) < 0)
2498 return -EINVAL;
2499 }
2500
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002501 asihpi_ctl_init(&snd_control, hpi_ctl, "Rate");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002502 snd_control.access =
2503 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
2504 snd_control.info = snd_asihpi_clkrate_info;
2505 snd_control.get = snd_asihpi_clkrate_get;
2506
2507 return ctl_add(card, &snd_control, asihpi);
2508}
2509/*------------------------------------------------------------
2510 Mixer
2511 ------------------------------------------------------------*/
2512
2513static int __devinit snd_card_asihpi_mixer_new(struct snd_card_asihpi *asihpi)
2514{
2515 struct snd_card *card = asihpi->card;
2516 unsigned int idx = 0;
2517 unsigned int subindex = 0;
2518 int err;
2519 struct hpi_control hpi_ctl, prev_ctl;
2520
2521 if (snd_BUG_ON(!asihpi))
2522 return -EINVAL;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002523 strcpy(card->mixername, "Asihpi Mixer");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002524
2525 err =
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002526 hpi_mixer_open(asihpi->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002527 &asihpi->h_mixer);
2528 hpi_handle_error(err);
2529 if (err)
2530 return -err;
2531
Takashi Iwai21896bc2010-06-02 12:08:37 +02002532 memset(&prev_ctl, 0, sizeof(prev_ctl));
2533 prev_ctl.control_type = -1;
2534
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002535 for (idx = 0; idx < 2000; idx++) {
2536 err = hpi_mixer_get_control_by_index(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002537 asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002538 idx,
2539 &hpi_ctl.src_node_type,
2540 &hpi_ctl.src_node_index,
2541 &hpi_ctl.dst_node_type,
2542 &hpi_ctl.dst_node_index,
2543 &hpi_ctl.control_type,
2544 &hpi_ctl.h_control);
2545 if (err) {
2546 if (err == HPI_ERROR_CONTROL_DISABLED) {
2547 if (mixer_dump)
2548 snd_printk(KERN_INFO
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002549 "Disabled HPI Control(%d)\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002550 idx);
2551 continue;
2552 } else
2553 break;
2554
2555 }
2556
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12002557 hpi_ctl.src_node_type -= HPI_SOURCENODE_NONE;
2558 hpi_ctl.dst_node_type -= HPI_DESTNODE_NONE;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002559
2560 /* ASI50xx in SSX mode has multiple meters on the same node.
2561 Use subindex to create distinct ALSA controls
2562 for any duplicated controls.
2563 */
2564 if ((hpi_ctl.control_type == prev_ctl.control_type) &&
2565 (hpi_ctl.src_node_type == prev_ctl.src_node_type) &&
2566 (hpi_ctl.src_node_index == prev_ctl.src_node_index) &&
2567 (hpi_ctl.dst_node_type == prev_ctl.dst_node_type) &&
2568 (hpi_ctl.dst_node_index == prev_ctl.dst_node_index))
2569 subindex++;
2570 else
2571 subindex = 0;
2572
2573 prev_ctl = hpi_ctl;
2574
2575 switch (hpi_ctl.control_type) {
2576 case HPI_CONTROL_VOLUME:
2577 err = snd_asihpi_volume_add(asihpi, &hpi_ctl);
2578 break;
2579 case HPI_CONTROL_LEVEL:
2580 err = snd_asihpi_level_add(asihpi, &hpi_ctl);
2581 break;
2582 case HPI_CONTROL_MULTIPLEXER:
2583 err = snd_asihpi_mux_add(asihpi, &hpi_ctl);
2584 break;
2585 case HPI_CONTROL_CHANNEL_MODE:
2586 err = snd_asihpi_cmode_add(asihpi, &hpi_ctl);
2587 break;
2588 case HPI_CONTROL_METER:
2589 err = snd_asihpi_meter_add(asihpi, &hpi_ctl, subindex);
2590 break;
2591 case HPI_CONTROL_SAMPLECLOCK:
2592 err = snd_asihpi_sampleclock_add(
2593 asihpi, &hpi_ctl);
2594 break;
2595 case HPI_CONTROL_CONNECTION: /* ignore these */
2596 continue;
2597 case HPI_CONTROL_TUNER:
2598 err = snd_asihpi_tuner_add(asihpi, &hpi_ctl);
2599 break;
2600 case HPI_CONTROL_AESEBU_TRANSMITTER:
2601 err = snd_asihpi_aesebu_tx_add(asihpi, &hpi_ctl);
2602 break;
2603 case HPI_CONTROL_AESEBU_RECEIVER:
2604 err = snd_asihpi_aesebu_rx_add(asihpi, &hpi_ctl);
2605 break;
2606 case HPI_CONTROL_VOX:
2607 case HPI_CONTROL_BITSTREAM:
2608 case HPI_CONTROL_MICROPHONE:
2609 case HPI_CONTROL_PARAMETRIC_EQ:
2610 case HPI_CONTROL_COMPANDER:
2611 default:
2612 if (mixer_dump)
2613 snd_printk(KERN_INFO
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002614 "Untranslated HPI Control"
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002615 "(%d) %d %d %d %d %d\n",
2616 idx,
2617 hpi_ctl.control_type,
2618 hpi_ctl.src_node_type,
2619 hpi_ctl.src_node_index,
2620 hpi_ctl.dst_node_type,
2621 hpi_ctl.dst_node_index);
2622 continue;
2623 };
2624 if (err < 0)
2625 return err;
2626 }
2627 if (HPI_ERROR_INVALID_OBJ_INDEX != err)
2628 hpi_handle_error(err);
2629
2630 snd_printk(KERN_INFO "%d mixer controls found\n", idx);
2631
2632 return 0;
2633}
2634
2635/*------------------------------------------------------------
2636 /proc interface
2637 ------------------------------------------------------------*/
2638
2639static void
2640snd_asihpi_proc_read(struct snd_info_entry *entry,
2641 struct snd_info_buffer *buffer)
2642{
2643 struct snd_card_asihpi *asihpi = entry->private_data;
2644 u16 version;
2645 u32 h_control;
2646 u32 rate = 0;
2647 u16 source = 0;
2648 int err;
2649
2650 snd_iprintf(buffer, "ASIHPI driver proc file\n");
2651 snd_iprintf(buffer,
2652 "adapter ID=%4X\n_index=%d\n"
2653 "num_outstreams=%d\n_num_instreams=%d\n",
2654 asihpi->type, asihpi->adapter_index,
2655 asihpi->num_outstreams, asihpi->num_instreams);
2656
2657 version = asihpi->version;
2658 snd_iprintf(buffer,
2659 "serial#=%d\n_hw version %c%d\nDSP code version %03d\n",
2660 asihpi->serial_number, ((version >> 3) & 0xf) + 'A',
2661 version & 0x7,
2662 ((version >> 13) * 100) + ((version >> 7) & 0x3f));
2663
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002664 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002665 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
2666 HPI_CONTROL_SAMPLECLOCK, &h_control);
2667
2668 if (!err) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002669 err = hpi_sample_clock_get_sample_rate(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002670 h_control, &rate);
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002671 err += hpi_sample_clock_get_source(h_control, &source);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002672
2673 if (!err)
2674 snd_iprintf(buffer, "sample_clock=%d_hz, source %s\n",
2675 rate, sampleclock_sources[source]);
2676 }
2677
2678}
2679
2680
2681static void __devinit snd_asihpi_proc_init(struct snd_card_asihpi *asihpi)
2682{
2683 struct snd_info_entry *entry;
2684
2685 if (!snd_card_proc_new(asihpi->card, "info", &entry))
2686 snd_info_set_text_ops(entry, asihpi, snd_asihpi_proc_read);
2687}
2688
2689/*------------------------------------------------------------
2690 HWDEP
2691 ------------------------------------------------------------*/
2692
2693static int snd_asihpi_hpi_open(struct snd_hwdep *hw, struct file *file)
2694{
2695 if (enable_hpi_hwdep)
2696 return 0;
2697 else
2698 return -ENODEV;
2699
2700}
2701
2702static int snd_asihpi_hpi_release(struct snd_hwdep *hw, struct file *file)
2703{
2704 if (enable_hpi_hwdep)
2705 return asihpi_hpi_release(file);
2706 else
2707 return -ENODEV;
2708}
2709
2710static int snd_asihpi_hpi_ioctl(struct snd_hwdep *hw, struct file *file,
2711 unsigned int cmd, unsigned long arg)
2712{
2713 if (enable_hpi_hwdep)
2714 return asihpi_hpi_ioctl(file, cmd, arg);
2715 else
2716 return -ENODEV;
2717}
2718
2719
2720/* results in /dev/snd/hwC#D0 file for each card with index #
2721 also /proc/asound/hwdep will contain '#-00: asihpi (HPI) for each card'
2722*/
2723static int __devinit snd_asihpi_hpi_new(struct snd_card_asihpi *asihpi,
2724 int device, struct snd_hwdep **rhwdep)
2725{
2726 struct snd_hwdep *hw;
2727 int err;
2728
2729 if (rhwdep)
2730 *rhwdep = NULL;
2731 err = snd_hwdep_new(asihpi->card, "HPI", device, &hw);
2732 if (err < 0)
2733 return err;
2734 strcpy(hw->name, "asihpi (HPI)");
2735 hw->iface = SNDRV_HWDEP_IFACE_LAST;
2736 hw->ops.open = snd_asihpi_hpi_open;
2737 hw->ops.ioctl = snd_asihpi_hpi_ioctl;
2738 hw->ops.release = snd_asihpi_hpi_release;
2739 hw->private_data = asihpi;
2740 if (rhwdep)
2741 *rhwdep = hw;
2742 return 0;
2743}
2744
2745/*------------------------------------------------------------
2746 CARD
2747 ------------------------------------------------------------*/
2748static int __devinit snd_asihpi_probe(struct pci_dev *pci_dev,
2749 const struct pci_device_id *pci_id)
2750{
2751 int err;
2752
2753 u16 version;
2754 int pcm_substreams;
2755
2756 struct hpi_adapter *hpi_card;
2757 struct snd_card *card;
2758 struct snd_card_asihpi *asihpi;
2759
2760 u32 h_control;
2761 u32 h_stream;
2762
2763 static int dev;
2764 if (dev >= SNDRV_CARDS)
2765 return -ENODEV;
2766
2767 /* Should this be enable[hpi_card->index] ? */
2768 if (!enable[dev]) {
2769 dev++;
2770 return -ENOENT;
2771 }
2772
2773 err = asihpi_adapter_probe(pci_dev, pci_id);
2774 if (err < 0)
2775 return err;
2776
2777 hpi_card = pci_get_drvdata(pci_dev);
2778 /* first try to give the card the same index as its hardware index */
2779 err = snd_card_create(hpi_card->index,
2780 id[hpi_card->index], THIS_MODULE,
2781 sizeof(struct snd_card_asihpi),
2782 &card);
2783 if (err < 0) {
2784 /* if that fails, try the default index==next available */
2785 err =
2786 snd_card_create(index[dev], id[dev],
2787 THIS_MODULE,
2788 sizeof(struct snd_card_asihpi),
2789 &card);
2790 if (err < 0)
2791 return err;
2792 snd_printk(KERN_WARNING
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002793 "**** WARNING **** Adapter index %d->ALSA index %d\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002794 hpi_card->index, card->number);
2795 }
2796
Eliot Blennerhassett12253672011-02-10 17:26:10 +13002797 snd_card_set_dev(card, &pci_dev->dev);
2798
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002799 asihpi = (struct snd_card_asihpi *) card->private_data;
2800 asihpi->card = card;
Eliot Blennerhassett12253672011-02-10 17:26:10 +13002801 asihpi->pci = pci_dev;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002802 asihpi->adapter_index = hpi_card->index;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002803 hpi_handle_error(hpi_adapter_get_info(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002804 asihpi->adapter_index,
2805 &asihpi->num_outstreams,
2806 &asihpi->num_instreams,
2807 &asihpi->version,
2808 &asihpi->serial_number, &asihpi->type));
2809
2810 version = asihpi->version;
2811 snd_printk(KERN_INFO "adapter ID=%4X index=%d num_outstreams=%d "
2812 "num_instreams=%d S/N=%d\n"
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002813 "Hw Version %c%d DSP code version %03d\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002814 asihpi->type, asihpi->adapter_index,
2815 asihpi->num_outstreams,
2816 asihpi->num_instreams, asihpi->serial_number,
2817 ((version >> 3) & 0xf) + 'A',
2818 version & 0x7,
2819 ((version >> 13) * 100) + ((version >> 7) & 0x3f));
2820
2821 pcm_substreams = asihpi->num_outstreams;
2822 if (pcm_substreams < asihpi->num_instreams)
2823 pcm_substreams = asihpi->num_instreams;
2824
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002825 err = hpi_adapter_get_property(asihpi->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002826 HPI_ADAPTER_PROPERTY_CAPS1,
2827 NULL, &asihpi->support_grouping);
2828 if (err)
2829 asihpi->support_grouping = 0;
2830
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002831 err = hpi_adapter_get_property(asihpi->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002832 HPI_ADAPTER_PROPERTY_CAPS2,
2833 &asihpi->support_mrx, NULL);
2834 if (err)
2835 asihpi->support_mrx = 0;
2836
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002837 err = hpi_adapter_get_property(asihpi->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002838 HPI_ADAPTER_PROPERTY_INTERVAL,
2839 NULL, &asihpi->update_interval_frames);
2840 if (err)
2841 asihpi->update_interval_frames = 512;
2842
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12002843 if (!asihpi->can_dma)
Eliot Blennerhassett26aebef2011-03-25 15:25:47 +13002844 asihpi->update_interval_frames *= 2;
2845
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002846 hpi_handle_error(hpi_instream_open(asihpi->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002847 0, &h_stream));
2848
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002849 err = hpi_instream_host_buffer_free(h_stream);
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12002850 asihpi->can_dma = (!err);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002851
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002852 hpi_handle_error(hpi_instream_close(h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002853
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002854 err = hpi_adapter_get_property(asihpi->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002855 HPI_ADAPTER_PROPERTY_CURCHANNELS,
2856 &asihpi->in_max_chans, &asihpi->out_max_chans);
2857 if (err) {
2858 asihpi->in_max_chans = 2;
2859 asihpi->out_max_chans = 2;
2860 }
2861
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12002862 snd_printk(KERN_INFO "has dma:%d, grouping:%d, mrx:%d\n",
2863 asihpi->can_dma,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002864 asihpi->support_grouping,
2865 asihpi->support_mrx
2866 );
2867
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002868 err = snd_card_asihpi_pcm_new(asihpi, 0, pcm_substreams);
2869 if (err < 0) {
2870 snd_printk(KERN_ERR "pcm_new failed\n");
2871 goto __nodev;
2872 }
2873 err = snd_card_asihpi_mixer_new(asihpi);
2874 if (err < 0) {
2875 snd_printk(KERN_ERR "mixer_new failed\n");
2876 goto __nodev;
2877 }
2878
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002879 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002880 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
2881 HPI_CONTROL_SAMPLECLOCK, &h_control);
2882
2883 if (!err)
2884 err = hpi_sample_clock_set_local_rate(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002885 h_control, adapter_fs);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002886
2887 snd_asihpi_proc_init(asihpi);
2888
2889 /* always create, can be enabled or disabled dynamically
2890 by enable_hwdep module param*/
2891 snd_asihpi_hpi_new(asihpi, 0, NULL);
2892
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12002893 strcpy(card->driver, "ASIHPI");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002894
2895 sprintf(card->shortname, "AudioScience ASI%4X", asihpi->type);
2896 sprintf(card->longname, "%s %i",
2897 card->shortname, asihpi->adapter_index);
2898 err = snd_card_register(card);
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13002899
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002900 if (!err) {
2901 hpi_card->snd_card_asihpi = card;
2902 dev++;
2903 return 0;
2904 }
2905__nodev:
2906 snd_card_free(card);
2907 snd_printk(KERN_ERR "snd_asihpi_probe error %d\n", err);
2908 return err;
2909
2910}
2911
2912static void __devexit snd_asihpi_remove(struct pci_dev *pci_dev)
2913{
2914 struct hpi_adapter *hpi_card = pci_get_drvdata(pci_dev);
2915
2916 snd_card_free(hpi_card->snd_card_asihpi);
2917 hpi_card->snd_card_asihpi = NULL;
2918 asihpi_adapter_remove(pci_dev);
2919}
2920
2921static DEFINE_PCI_DEVICE_TABLE(asihpi_pci_tbl) = {
2922 {HPI_PCI_VENDOR_ID_TI, HPI_PCI_DEV_ID_DSP6205,
2923 HPI_PCI_VENDOR_ID_AUDIOSCIENCE, PCI_ANY_ID, 0, 0,
2924 (kernel_ulong_t)HPI_6205},
2925 {HPI_PCI_VENDOR_ID_TI, HPI_PCI_DEV_ID_PCI2040,
2926 HPI_PCI_VENDOR_ID_AUDIOSCIENCE, PCI_ANY_ID, 0, 0,
2927 (kernel_ulong_t)HPI_6000},
2928 {0,}
2929};
2930MODULE_DEVICE_TABLE(pci, asihpi_pci_tbl);
2931
2932static struct pci_driver driver = {
Takashi Iwai3733e422011-06-10 16:20:20 +02002933 .name = KBUILD_MODNAME,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002934 .id_table = asihpi_pci_tbl,
2935 .probe = snd_asihpi_probe,
2936 .remove = __devexit_p(snd_asihpi_remove),
2937#ifdef CONFIG_PM
2938/* .suspend = snd_asihpi_suspend,
2939 .resume = snd_asihpi_resume, */
2940#endif
2941};
2942
2943static int __init snd_asihpi_init(void)
2944{
2945 asihpi_init();
2946 return pci_register_driver(&driver);
2947}
2948
2949static void __exit snd_asihpi_exit(void)
2950{
2951
2952 pci_unregister_driver(&driver);
2953 asihpi_exit();
2954}
2955
2956module_init(snd_asihpi_init)
2957module_exit(snd_asihpi_exit)
2958