blob: ddf882ed1dfb51f91a17c99f445aa0b1d68d4432 [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 */
53static void pcm_debug_name(struct snd_pcm_substream *substream,
54 char *name, size_t len)
55{
56 snprintf(name, len, "pcmC%dD%d%c:%d",
57 substream->pcm->card->number,
58 substream->pcm->device,
59 substream->stream ? 'c' : 'p',
60 substream->number);
61}
62#define DEBUG_NAME(substream, name) char name[16]; pcm_debug_name(substream, name, sizeof(name))
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +130063#else
Eliot Blennerhassetta6477132011-04-05 20:55:42 +120064#define pcm_debug_name(s, n, l) do { } while (0)
65#define DEBUG_NAME(name, substream) do { } while (0)
Eliot Blennerhassettb0096a62011-04-05 20:55:46 +120066#endif
Eliot Blennerhassetta6477132011-04-05 20:55:42 +120067
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +130068#if defined CONFIG_SND_DEBUG_VERBOSE
69/**
70 * snd_printddd - very verbose debug printk
71 * @format: format string
72 *
73 * Works like snd_printk() for debugging purposes.
74 * Ignored when CONFIG_SND_DEBUG_VERBOSE is not set.
75 * Must set snd module debug parameter to 3 to enable at runtime.
76 */
77#define snd_printddd(format, args...) \
78 __snd_printk(3, __FILE__, __LINE__, format, ##args)
79#else
Eliot Blennerhassettb0096a62011-04-05 20:55:46 +120080#define snd_printddd(format, args...) do { } while (0)
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +130081#endif
82
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020083static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* index 0-MAX */
84static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
85static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
86static int enable_hpi_hwdep = 1;
87
88module_param_array(index, int, NULL, S_IRUGO);
89MODULE_PARM_DESC(index, "ALSA index value for AudioScience soundcard.");
90
91module_param_array(id, charp, NULL, S_IRUGO);
92MODULE_PARM_DESC(id, "ALSA ID string for AudioScience soundcard.");
93
94module_param_array(enable, bool, NULL, S_IRUGO);
95MODULE_PARM_DESC(enable, "ALSA enable AudioScience soundcard.");
96
97module_param(enable_hpi_hwdep, bool, S_IRUGO|S_IWUSR);
98MODULE_PARM_DESC(enable_hpi_hwdep,
99 "ALSA enable HPI hwdep for AudioScience soundcard ");
100
101/* identify driver */
102#ifdef KERNEL_ALSA_BUILD
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300103static char *build_info = "Built using headers from kernel source";
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200104module_param(build_info, charp, S_IRUGO);
105MODULE_PARM_DESC(build_info, "built using headers from kernel source");
106#else
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300107static char *build_info = "Built within ALSA source";
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200108module_param(build_info, charp, S_IRUGO);
109MODULE_PARM_DESC(build_info, "built within ALSA source");
110#endif
111
112/* set to 1 to dump every control from adapter to log */
113static const int mixer_dump;
114
115#define DEFAULT_SAMPLERATE 44100
116static int adapter_fs = DEFAULT_SAMPLERATE;
117
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200118/* defaults */
119#define PERIODS_MIN 2
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300120#define PERIOD_BYTES_MIN 2048
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200121#define BUFFER_BYTES_MAX (512 * 1024)
122
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200123#define MAX_CLOCKSOURCES (HPI_SAMPLECLOCK_SOURCE_LAST + 1 + 7)
124
125struct clk_source {
126 int source;
127 int index;
128 char *name;
129};
130
131struct clk_cache {
132 int count;
133 int has_local;
134 struct clk_source s[MAX_CLOCKSOURCES];
135};
136
137/* Per card data */
138struct snd_card_asihpi {
139 struct snd_card *card;
140 struct pci_dev *pci;
141 u16 adapter_index;
142 u32 serial_number;
143 u16 type;
144 u16 version;
145 u16 num_outstreams;
146 u16 num_instreams;
147
148 u32 h_mixer;
149 struct clk_cache cc;
150
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200151 u16 can_dma;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200152 u16 support_grouping;
153 u16 support_mrx;
154 u16 update_interval_frames;
155 u16 in_max_chans;
156 u16 out_max_chans;
157};
158
159/* Per stream data */
160struct snd_card_asihpi_pcm {
161 struct timer_list timer;
162 unsigned int respawn_timer;
163 unsigned int hpi_buffer_attached;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300164 unsigned int buffer_bytes;
165 unsigned int period_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200166 unsigned int bytes_per_sec;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300167 unsigned int pcm_buf_host_rw_ofs; /* Host R/W pos */
168 unsigned int pcm_buf_dma_ofs; /* DMA R/W offset in buffer */
169 unsigned int pcm_buf_elapsed_dma_ofs; /* DMA R/W offset in buffer */
Eliot Blennerhassett0b7ce9e2011-04-05 20:55:43 +1200170 unsigned int drained_count;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200171 struct snd_pcm_substream *substream;
172 u32 h_stream;
173 struct hpi_format format;
174};
175
176/* universal stream verbs work with out or in stream handles */
177
178/* Functions to allow driver to give a buffer to HPI for busmastering */
179
180static u16 hpi_stream_host_buffer_attach(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200181 u32 h_stream, /* handle to outstream. */
182 u32 size_in_bytes, /* size in bytes of bus mastering buffer */
183 u32 pci_address
184)
185{
186 struct hpi_message hm;
187 struct hpi_response hr;
188 unsigned int obj = hpi_handle_object(h_stream);
189
190 if (!h_stream)
191 return HPI_ERROR_INVALID_OBJ;
192 hpi_init_message_response(&hm, &hr, obj,
193 obj == HPI_OBJ_OSTREAM ?
194 HPI_OSTREAM_HOSTBUFFER_ALLOC :
195 HPI_ISTREAM_HOSTBUFFER_ALLOC);
196
197 hpi_handle_to_indexes(h_stream, &hm.adapter_index,
198 &hm.obj_index);
199
200 hm.u.d.u.buffer.buffer_size = size_in_bytes;
201 hm.u.d.u.buffer.pci_address = pci_address;
202 hm.u.d.u.buffer.command = HPI_BUFFER_CMD_INTERNAL_GRANTADAPTER;
203 hpi_send_recv(&hm, &hr);
204 return hr.error;
205}
206
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300207static u16 hpi_stream_host_buffer_detach(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200208{
209 struct hpi_message hm;
210 struct hpi_response hr;
211 unsigned int obj = hpi_handle_object(h_stream);
212
213 if (!h_stream)
214 return HPI_ERROR_INVALID_OBJ;
215
216 hpi_init_message_response(&hm, &hr, obj,
217 obj == HPI_OBJ_OSTREAM ?
218 HPI_OSTREAM_HOSTBUFFER_FREE :
219 HPI_ISTREAM_HOSTBUFFER_FREE);
220
221 hpi_handle_to_indexes(h_stream, &hm.adapter_index,
222 &hm.obj_index);
223 hm.u.d.u.buffer.command = HPI_BUFFER_CMD_INTERNAL_REVOKEADAPTER;
224 hpi_send_recv(&hm, &hr);
225 return hr.error;
226}
227
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300228static inline u16 hpi_stream_start(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200229{
230 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300231 return hpi_outstream_start(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200232 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300233 return hpi_instream_start(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200234}
235
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300236static inline u16 hpi_stream_stop(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200237{
238 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300239 return hpi_outstream_stop(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200240 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300241 return hpi_instream_stop(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200242}
243
244static inline u16 hpi_stream_get_info_ex(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200245 u32 h_stream,
246 u16 *pw_state,
247 u32 *pbuffer_size,
248 u32 *pdata_in_buffer,
249 u32 *psample_count,
250 u32 *pauxiliary_data
251)
252{
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300253 u16 e;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200254 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300255 e = hpi_outstream_get_info_ex(h_stream, pw_state,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200256 pbuffer_size, pdata_in_buffer,
257 psample_count, pauxiliary_data);
258 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300259 e = hpi_instream_get_info_ex(h_stream, pw_state,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200260 pbuffer_size, pdata_in_buffer,
261 psample_count, pauxiliary_data);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300262 return e;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200263}
264
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300265static inline u16 hpi_stream_group_add(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200266 u32 h_master,
267 u32 h_stream)
268{
269 if (hpi_handle_object(h_master) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300270 return hpi_outstream_group_add(h_master, h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200271 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300272 return hpi_instream_group_add(h_master, h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200273}
274
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300275static inline u16 hpi_stream_group_reset(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200276{
277 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300278 return hpi_outstream_group_reset(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200279 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300280 return hpi_instream_group_reset(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200281}
282
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300283static inline u16 hpi_stream_group_get_map(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200284 u32 h_stream, u32 *mo, u32 *mi)
285{
286 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300287 return hpi_outstream_group_get_map(h_stream, mo, mi);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200288 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300289 return hpi_instream_group_get_map(h_stream, mo, mi);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200290}
291
292static u16 handle_error(u16 err, int line, char *filename)
293{
294 if (err)
295 printk(KERN_WARNING
296 "in file %s, line %d: HPI error %d\n",
297 filename, line, err);
298 return err;
299}
300
301#define hpi_handle_error(x) handle_error(x, __LINE__, __FILE__)
302
303/***************************** GENERAL PCM ****************/
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200304
305static void print_hwparams(struct snd_pcm_substream *substream,
306 struct snd_pcm_hw_params *p)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200307{
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200308 DEBUG_NAME(substream, name);
309 snd_printd("%s HWPARAMS\n", name);
310 snd_printd(" samplerate %d Hz\n", params_rate(p));
311 snd_printd(" channels %d\n", params_channels(p));
312 snd_printd(" format %d\n", params_format(p));
313 snd_printd(" subformat %d\n", params_subformat(p));
314 snd_printd(" buffer %d B\n", params_buffer_bytes(p));
315 snd_printd(" period %d B\n", params_period_bytes(p));
316 snd_printd(" access %d\n", params_access(p));
317 snd_printd(" period_size %d\n", params_period_size(p));
318 snd_printd(" periods %d\n", params_periods(p));
319 snd_printd(" buffer_size %d\n", params_buffer_size(p));
320 snd_printd(" %d B/s\n", params_rate(p) *
321 params_channels(p) *
322 snd_pcm_format_width(params_format(p)) / 8);
323
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200324}
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200325
326static snd_pcm_format_t hpi_to_alsa_formats[] = {
327 -1, /* INVALID */
328 SNDRV_PCM_FORMAT_U8, /* HPI_FORMAT_PCM8_UNSIGNED 1 */
329 SNDRV_PCM_FORMAT_S16, /* HPI_FORMAT_PCM16_SIGNED 2 */
330 -1, /* HPI_FORMAT_MPEG_L1 3 */
331 SNDRV_PCM_FORMAT_MPEG, /* HPI_FORMAT_MPEG_L2 4 */
332 SNDRV_PCM_FORMAT_MPEG, /* HPI_FORMAT_MPEG_L3 5 */
333 -1, /* HPI_FORMAT_DOLBY_AC2 6 */
334 -1, /* HPI_FORMAT_DOLBY_AC3 7 */
335 SNDRV_PCM_FORMAT_S16_BE,/* HPI_FORMAT_PCM16_BIGENDIAN 8 */
336 -1, /* HPI_FORMAT_AA_TAGIT1_HITS 9 */
337 -1, /* HPI_FORMAT_AA_TAGIT1_INSERTS 10 */
338 SNDRV_PCM_FORMAT_S32, /* HPI_FORMAT_PCM32_SIGNED 11 */
339 -1, /* HPI_FORMAT_RAW_BITSTREAM 12 */
340 -1, /* HPI_FORMAT_AA_TAGIT1_HITS_EX1 13 */
341 SNDRV_PCM_FORMAT_FLOAT, /* HPI_FORMAT_PCM32_FLOAT 14 */
342#if 1
343 /* ALSA can't handle 3 byte sample size together with power-of-2
344 * constraint on buffer_bytes, so disable this format
345 */
346 -1
347#else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300348 /* SNDRV_PCM_FORMAT_S24_3LE */ /* HPI_FORMAT_PCM24_SIGNED 15 */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200349#endif
350};
351
352
353static int snd_card_asihpi_format_alsa2hpi(snd_pcm_format_t alsa_format,
354 u16 *hpi_format)
355{
356 u16 format;
357
358 for (format = HPI_FORMAT_PCM8_UNSIGNED;
359 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
360 if (hpi_to_alsa_formats[format] == alsa_format) {
361 *hpi_format = format;
362 return 0;
363 }
364 }
365
366 snd_printd(KERN_WARNING "failed match for alsa format %d\n",
367 alsa_format);
368 *hpi_format = 0;
369 return -EINVAL;
370}
371
372static void snd_card_asihpi_pcm_samplerates(struct snd_card_asihpi *asihpi,
373 struct snd_pcm_hardware *pcmhw)
374{
375 u16 err;
376 u32 h_control;
377 u32 sample_rate;
378 int idx;
379 unsigned int rate_min = 200000;
380 unsigned int rate_max = 0;
381 unsigned int rates = 0;
382
383 if (asihpi->support_mrx) {
384 rates |= SNDRV_PCM_RATE_CONTINUOUS;
385 rates |= SNDRV_PCM_RATE_8000_96000;
386 rate_min = 8000;
387 rate_max = 100000;
388 } else {
389 /* on cards without SRC,
390 valid rates are determined by sampleclock */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300391 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200392 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
393 HPI_CONTROL_SAMPLECLOCK, &h_control);
394 if (err) {
395 snd_printk(KERN_ERR
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300396 "No local sampleclock, err %d\n", err);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200397 }
398
Eliot Blennerhassett7bf76c32011-03-25 15:25:46 +1300399 for (idx = -1; idx < 100; idx++) {
400 if (idx == -1) {
401 if (hpi_sample_clock_get_sample_rate(h_control,
402 &sample_rate))
403 continue;
404 } else if (hpi_sample_clock_query_local_rate(h_control,
405 idx, &sample_rate)) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200406 break;
407 }
408
409 rate_min = min(rate_min, sample_rate);
410 rate_max = max(rate_max, sample_rate);
411
412 switch (sample_rate) {
413 case 5512:
414 rates |= SNDRV_PCM_RATE_5512;
415 break;
416 case 8000:
417 rates |= SNDRV_PCM_RATE_8000;
418 break;
419 case 11025:
420 rates |= SNDRV_PCM_RATE_11025;
421 break;
422 case 16000:
423 rates |= SNDRV_PCM_RATE_16000;
424 break;
425 case 22050:
426 rates |= SNDRV_PCM_RATE_22050;
427 break;
428 case 32000:
429 rates |= SNDRV_PCM_RATE_32000;
430 break;
431 case 44100:
432 rates |= SNDRV_PCM_RATE_44100;
433 break;
434 case 48000:
435 rates |= SNDRV_PCM_RATE_48000;
436 break;
437 case 64000:
438 rates |= SNDRV_PCM_RATE_64000;
439 break;
440 case 88200:
441 rates |= SNDRV_PCM_RATE_88200;
442 break;
443 case 96000:
444 rates |= SNDRV_PCM_RATE_96000;
445 break;
446 case 176400:
447 rates |= SNDRV_PCM_RATE_176400;
448 break;
449 case 192000:
450 rates |= SNDRV_PCM_RATE_192000;
451 break;
452 default: /* some other rate */
453 rates |= SNDRV_PCM_RATE_KNOT;
454 }
455 }
456 }
457
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200458 pcmhw->rates = rates;
459 pcmhw->rate_min = rate_min;
460 pcmhw->rate_max = rate_max;
461}
462
463static int snd_card_asihpi_pcm_hw_params(struct snd_pcm_substream *substream,
464 struct snd_pcm_hw_params *params)
465{
466 struct snd_pcm_runtime *runtime = substream->runtime;
467 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
468 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
469 int err;
470 u16 format;
Kulikov Vasiliy315e8f72010-07-15 22:48:19 +0400471 int width;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200472 unsigned int bytes_per_sec;
473
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200474 print_hwparams(substream, params);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200475 err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
476 if (err < 0)
477 return err;
478 err = snd_card_asihpi_format_alsa2hpi(params_format(params), &format);
479 if (err)
480 return err;
481
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200482 hpi_handle_error(hpi_format_create(&dpcm->format,
483 params_channels(params),
484 format, params_rate(params), 0, 0));
485
486 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300487 if (hpi_instream_reset(dpcm->h_stream) != 0)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200488 return -EINVAL;
489
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300490 if (hpi_instream_set_format(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200491 dpcm->h_stream, &dpcm->format) != 0)
492 return -EINVAL;
493 }
494
495 dpcm->hpi_buffer_attached = 0;
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200496 if (card->can_dma) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300497 err = hpi_stream_host_buffer_attach(dpcm->h_stream,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200498 params_buffer_bytes(params), runtime->dma_addr);
499 if (err == 0) {
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300500 snd_printdd(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200501 "stream_host_buffer_attach succeeded %u %lu\n",
502 params_buffer_bytes(params),
503 (unsigned long)runtime->dma_addr);
504 } else {
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300505 snd_printd("stream_host_buffer_attach error %d\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200506 err);
507 return -ENOMEM;
508 }
509
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300510 err = hpi_stream_get_info_ex(dpcm->h_stream, NULL,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200511 &dpcm->hpi_buffer_attached,
512 NULL, NULL, NULL);
513
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300514 snd_printdd("stream_host_buffer_attach status 0x%x\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200515 dpcm->hpi_buffer_attached);
516 }
517 bytes_per_sec = params_rate(params) * params_channels(params);
Kulikov Vasiliy315e8f72010-07-15 22:48:19 +0400518 width = snd_pcm_format_width(params_format(params));
519 bytes_per_sec *= width;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200520 bytes_per_sec /= 8;
Kulikov Vasiliy315e8f72010-07-15 22:48:19 +0400521 if (width < 0 || bytes_per_sec == 0)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200522 return -EINVAL;
523
524 dpcm->bytes_per_sec = bytes_per_sec;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300525 dpcm->buffer_bytes = params_buffer_bytes(params);
526 dpcm->period_bytes = params_period_bytes(params);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200527
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200528 return 0;
529}
530
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300531static int
532snd_card_asihpi_hw_free(struct snd_pcm_substream *substream)
533{
534 struct snd_pcm_runtime *runtime = substream->runtime;
535 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
536 if (dpcm->hpi_buffer_attached)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300537 hpi_stream_host_buffer_detach(dpcm->h_stream);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300538
539 snd_pcm_lib_free_pages(substream);
540 return 0;
541}
542
543static void snd_card_asihpi_runtime_free(struct snd_pcm_runtime *runtime)
544{
545 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
546 kfree(dpcm);
547}
548
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200549static void snd_card_asihpi_pcm_timer_start(struct snd_pcm_substream *
550 substream)
551{
552 struct snd_pcm_runtime *runtime = substream->runtime;
553 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
554 int expiry;
555
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300556 expiry = HZ / 200;
557 /*? (dpcm->period_bytes * HZ / dpcm->bytes_per_sec); */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300558 expiry = max(expiry, 1); /* don't let it be zero! */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200559 dpcm->timer.expires = jiffies + expiry;
560 dpcm->respawn_timer = 1;
561 add_timer(&dpcm->timer);
562}
563
564static void snd_card_asihpi_pcm_timer_stop(struct snd_pcm_substream *substream)
565{
566 struct snd_pcm_runtime *runtime = substream->runtime;
567 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
568
569 dpcm->respawn_timer = 0;
570 del_timer(&dpcm->timer);
571}
572
573static int snd_card_asihpi_trigger(struct snd_pcm_substream *substream,
574 int cmd)
575{
576 struct snd_card_asihpi_pcm *dpcm = substream->runtime->private_data;
577 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
578 struct snd_pcm_substream *s;
579 u16 e;
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200580 DEBUG_NAME(substream, name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200581
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200582 snd_printdd("%s trigger\n", name);
583
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200584 switch (cmd) {
585 case SNDRV_PCM_TRIGGER_START:
586 snd_pcm_group_for_each_entry(s, substream) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300587 struct snd_pcm_runtime *runtime = s->runtime;
588 struct snd_card_asihpi_pcm *ds = runtime->private_data;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200589
590 if (snd_pcm_substream_chip(s) != card)
591 continue;
592
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300593 /* don't link Cap and Play */
594 if (substream->stream != s->stream)
595 continue;
596
Eliot Blennerhassett0b7ce9e2011-04-05 20:55:43 +1200597 ds->drained_count = 0;
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200598 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200599 /* How do I know how much valid data is present
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300600 * in buffer? Must be at least one period!
601 * Guessing 2 periods, but if
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200602 * buffer is bigger it may contain even more
603 * data??
604 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300605 unsigned int preload = ds->period_bytes * 1;
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300606 snd_printddd("%d preload x%x\n", s->number, preload);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200607 hpi_handle_error(hpi_outstream_write_buf(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300608 ds->h_stream,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300609 &runtime->dma_area[0],
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200610 preload,
611 &ds->format));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300612 ds->pcm_buf_host_rw_ofs = preload;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200613 }
614
615 if (card->support_grouping) {
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200616 snd_printdd("%d group\n", s->number);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300617 e = hpi_stream_group_add(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200618 dpcm->h_stream,
619 ds->h_stream);
620 if (!e) {
621 snd_pcm_trigger_done(s, substream);
622 } else {
623 hpi_handle_error(e);
624 break;
625 }
626 } else
627 break;
628 }
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300629 snd_printdd("start\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200630 /* start the master stream */
631 snd_card_asihpi_pcm_timer_start(substream);
Eliot Blennerhassettc4ed97d2011-02-10 17:26:20 +1300632 if ((substream->stream == SNDRV_PCM_STREAM_CAPTURE) ||
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200633 !card->can_dma)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300634 hpi_handle_error(hpi_stream_start(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200635 break;
636
637 case SNDRV_PCM_TRIGGER_STOP:
638 snd_card_asihpi_pcm_timer_stop(substream);
639 snd_pcm_group_for_each_entry(s, substream) {
640 if (snd_pcm_substream_chip(s) != card)
641 continue;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300642 /* don't link Cap and Play */
643 if (substream->stream != s->stream)
644 continue;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200645
646 /*? workaround linked streams don't
647 transition to SETUP 20070706*/
648 s->runtime->status->state = SNDRV_PCM_STATE_SETUP;
649
650 if (card->support_grouping) {
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200651 snd_printdd("%d group\n", s->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200652 snd_pcm_trigger_done(s, substream);
653 } else
654 break;
655 }
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300656 snd_printdd("stop\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200657
658 /* _prepare and _hwparams reset the stream */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300659 hpi_handle_error(hpi_stream_stop(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200660 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
661 hpi_handle_error(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300662 hpi_outstream_reset(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200663
664 if (card->support_grouping)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300665 hpi_handle_error(hpi_stream_group_reset(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200666 break;
667
668 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300669 snd_printdd("pause release\n");
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300670 hpi_handle_error(hpi_stream_start(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200671 snd_card_asihpi_pcm_timer_start(substream);
672 break;
673 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300674 snd_printdd("pause\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200675 snd_card_asihpi_pcm_timer_stop(substream);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300676 hpi_handle_error(hpi_stream_stop(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200677 break;
678 default:
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300679 snd_printd(KERN_ERR "\tINVALID\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200680 return -EINVAL;
681 }
682
683 return 0;
684}
685
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200686/*algorithm outline
687 Without linking degenerates to getting single stream pos etc
688 Without mmap 2nd loop degenerates to snd_pcm_period_elapsed
689*/
690/*
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300691pcm_buf_dma_ofs=get_buf_pos(s);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200692for_each_linked_stream(s) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300693 pcm_buf_dma_ofs=get_buf_pos(s);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300694 min_buf_pos = modulo_min(min_buf_pos, pcm_buf_dma_ofs, buffer_bytes)
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300695 new_data = min(new_data, calc_new_data(pcm_buf_dma_ofs,irq_pos)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200696}
697timer.expires = jiffies + predict_next_period_ready(min_buf_pos);
698for_each_linked_stream(s) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300699 s->pcm_buf_dma_ofs = min_buf_pos;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300700 if (new_data > period_bytes) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200701 if (mmap) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300702 irq_pos = (irq_pos + period_bytes) % buffer_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200703 if (playback) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300704 write(period_bytes);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200705 } else {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300706 read(period_bytes);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200707 }
708 }
709 snd_pcm_period_elapsed(s);
710 }
711}
712*/
713
714/** Minimum of 2 modulo values. Works correctly when the difference between
715* the values is less than half the modulus
716*/
717static inline unsigned int modulo_min(unsigned int a, unsigned int b,
718 unsigned long int modulus)
719{
720 unsigned int result;
721 if (((a-b) % modulus) < (modulus/2))
722 result = b;
723 else
724 result = a;
725
726 return result;
727}
728
729/** Timer function, equivalent to interrupt service routine for cards
730*/
731static void snd_card_asihpi_timer_function(unsigned long data)
732{
733 struct snd_card_asihpi_pcm *dpcm = (struct snd_card_asihpi_pcm *)data;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300734 struct snd_pcm_substream *substream = dpcm->substream;
735 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200736 struct snd_pcm_runtime *runtime;
737 struct snd_pcm_substream *s;
738 unsigned int newdata = 0;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300739 unsigned int pcm_buf_dma_ofs, min_buf_pos = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200740 unsigned int remdata, xfercount, next_jiffies;
741 int first = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300742 int loops = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200743 u16 state;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300744 u32 buffer_size, bytes_avail, samples_played, on_card_bytes;
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200745 DEBUG_NAME(substream, name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200746
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200747 snd_printdd("%s snd_card_asihpi_timer_function\n", name);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300748
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200749 /* find minimum newdata and buffer pos in group */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300750 snd_pcm_group_for_each_entry(s, substream) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200751 struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
752 runtime = s->runtime;
753
754 if (snd_pcm_substream_chip(s) != card)
755 continue;
756
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300757 /* don't link Cap and Play */
758 if (substream->stream != s->stream)
759 continue;
760
761 hpi_handle_error(hpi_stream_get_info_ex(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200762 ds->h_stream, &state,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300763 &buffer_size, &bytes_avail,
764 &samples_played, &on_card_bytes));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200765
766 /* number of bytes in on-card buffer */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300767 runtime->delay = on_card_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200768
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200769 if (!card->can_dma)
770 on_card_bytes = bytes_avail;
771
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300772 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300773 pcm_buf_dma_ofs = ds->pcm_buf_host_rw_ofs - bytes_avail;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300774 if (state == HPI_STATE_STOPPED) {
775 if ((bytes_avail == 0) &&
776 (on_card_bytes < ds->pcm_buf_host_rw_ofs)) {
777 hpi_handle_error(hpi_stream_start(ds->h_stream));
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300778 snd_printdd("P%d start\n", s->number);
Eliot Blennerhassett0b7ce9e2011-04-05 20:55:43 +1200779 ds->drained_count = 0;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300780 }
781 } else if (state == HPI_STATE_DRAINED) {
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300782 snd_printd(KERN_WARNING "P%d drained\n",
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300783 s->number);
Eliot Blennerhassett0b7ce9e2011-04-05 20:55:43 +1200784 ds->drained_count++;
785 if (ds->drained_count > 2) {
786 snd_pcm_stop(s, SNDRV_PCM_STATE_XRUN);
787 continue;
788 }
789 } else {
790 ds->drained_count = 0;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300791 }
792 } else
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300793 pcm_buf_dma_ofs = bytes_avail + ds->pcm_buf_host_rw_ofs;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200794
795 if (first) {
796 /* can't statically init min when wrap is involved */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300797 min_buf_pos = pcm_buf_dma_ofs;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300798 newdata = (pcm_buf_dma_ofs - ds->pcm_buf_elapsed_dma_ofs) % ds->buffer_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200799 first = 0;
800 } else {
801 min_buf_pos =
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300802 modulo_min(min_buf_pos, pcm_buf_dma_ofs, UINT_MAX+1L);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200803 newdata = min(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300804 (pcm_buf_dma_ofs - ds->pcm_buf_elapsed_dma_ofs) % ds->buffer_bytes,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200805 newdata);
806 }
807
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200808 snd_printdd("hw_ptr 0x%04lX, appl_ptr 0x%04lX\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200809 (unsigned long)frames_to_bytes(runtime,
810 runtime->status->hw_ptr),
811 (unsigned long)frames_to_bytes(runtime,
812 runtime->control->appl_ptr));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300813
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200814 snd_printdd("%d S=%d, "
815 "rw=0x%04X, dma=0x%04X, left=0x%04X, "
816 "aux=0x%04X space=0x%04X\n",
817 s->number, state,
818 ds->pcm_buf_host_rw_ofs, pcm_buf_dma_ofs,
819 (int)bytes_avail,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300820 (int)on_card_bytes, buffer_size-bytes_avail);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300821 loops++;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200822 }
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300823 pcm_buf_dma_ofs = min_buf_pos;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200824
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300825 remdata = newdata % dpcm->period_bytes;
826 xfercount = newdata - remdata; /* a multiple of period_bytes */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300827 /* come back when on_card_bytes has decreased enough to allow
828 write to happen, or when data has been consumed to make another
829 period
830 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300831 if (xfercount && (on_card_bytes > dpcm->period_bytes))
832 next_jiffies = ((on_card_bytes - dpcm->period_bytes) * HZ / dpcm->bytes_per_sec);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300833 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300834 next_jiffies = ((dpcm->period_bytes - remdata) * HZ / dpcm->bytes_per_sec);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300835
836 next_jiffies = max(next_jiffies, 1U);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200837 dpcm->timer.expires = jiffies + next_jiffies;
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200838 snd_printdd("jif %d buf pos 0x%04X newdata 0x%04X xfer 0x%04X\n",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300839 next_jiffies, pcm_buf_dma_ofs, newdata, xfercount);
840
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300841 snd_pcm_group_for_each_entry(s, substream) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200842 struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200843
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300844 /* don't link Cap and Play */
845 if (substream->stream != s->stream)
846 continue;
847
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300848 ds->pcm_buf_dma_ofs = pcm_buf_dma_ofs;
849
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200850 if (xfercount &&
851 /* Limit use of on card fifo for playback */
852 ((on_card_bytes <= ds->period_bytes) ||
853 (s->stream == SNDRV_PCM_STREAM_CAPTURE)))
854
855 {
856
857 unsigned int buf_ofs = ds->pcm_buf_host_rw_ofs % ds->buffer_bytes;
858 unsigned int xfer1, xfer2;
859 char *pd = &s->runtime->dma_area[buf_ofs];
860
Eliot Blennerhassettb0096a62011-04-05 20:55:46 +1200861 if (card->can_dma) { /* buffer wrap is handled at lower level */
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200862 xfer1 = xfercount;
863 xfer2 = 0;
864 } else {
865 xfer1 = min(xfercount, ds->buffer_bytes - buf_ofs);
866 xfer2 = xfercount - xfer1;
867 }
868
869 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
870 snd_printddd("P%d write1 0x%04X 0x%04X\n",
871 s->number, xfer1, buf_ofs);
872 hpi_handle_error(
873 hpi_outstream_write_buf(
874 ds->h_stream, pd, xfer1,
875 &ds->format));
876
877 if (xfer2) {
878 pd = s->runtime->dma_area;
879
880 snd_printddd("P%d write2 0x%04X 0x%04X\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200881 s->number,
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200882 xfercount - xfer1, buf_ofs);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200883 hpi_handle_error(
884 hpi_outstream_write_buf(
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200885 ds->h_stream, pd,
886 xfercount - xfer1,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200887 &ds->format));
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200888 }
889 } else {
890 snd_printddd("C%d read1 0x%04x\n",
891 s->number, xfer1);
892 hpi_handle_error(
893 hpi_instream_read_buf(
894 ds->h_stream,
895 pd, xfer1));
896 if (xfer2) {
897 pd = s->runtime->dma_area;
898 snd_printddd("C%d read2 0x%04x\n",
899 s->number, xfer2);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200900 hpi_handle_error(
901 hpi_instream_read_buf(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300902 ds->h_stream,
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200903 pd, xfer2));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200904 }
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200905 }
906 ds->pcm_buf_host_rw_ofs = ds->pcm_buf_host_rw_ofs + xfercount;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300907 ds->pcm_buf_elapsed_dma_ofs = pcm_buf_dma_ofs;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200908 snd_pcm_period_elapsed(s);
909 }
910 }
911
912 if (dpcm->respawn_timer)
913 add_timer(&dpcm->timer);
914}
915
916/***************************** PLAYBACK OPS ****************/
917static int snd_card_asihpi_playback_ioctl(struct snd_pcm_substream *substream,
918 unsigned int cmd, void *arg)
919{
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200920 snd_printddd(KERN_INFO "P%d ioctl %d\n", substream->number, cmd);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200921 return snd_pcm_lib_ioctl(substream, cmd, arg);
922}
923
924static int snd_card_asihpi_playback_prepare(struct snd_pcm_substream *
925 substream)
926{
927 struct snd_pcm_runtime *runtime = substream->runtime;
928 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
929
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200930 snd_printdd("P%d prepare\n", substream->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200931
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300932 hpi_handle_error(hpi_outstream_reset(dpcm->h_stream));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300933 dpcm->pcm_buf_host_rw_ofs = 0;
934 dpcm->pcm_buf_dma_ofs = 0;
935 dpcm->pcm_buf_elapsed_dma_ofs = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200936 return 0;
937}
938
939static snd_pcm_uframes_t
940snd_card_asihpi_playback_pointer(struct snd_pcm_substream *substream)
941{
942 struct snd_pcm_runtime *runtime = substream->runtime;
943 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
944 snd_pcm_uframes_t ptr;
945
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300946 ptr = bytes_to_frames(runtime, dpcm->pcm_buf_dma_ofs % dpcm->buffer_bytes);
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200947 snd_printddd("P%d pointer = 0x%04lx\n", substream->number, (unsigned long)ptr);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200948 return ptr;
949}
950
951static void snd_card_asihpi_playback_format(struct snd_card_asihpi *asihpi,
952 u32 h_stream,
953 struct snd_pcm_hardware *pcmhw)
954{
955 struct hpi_format hpi_format;
956 u16 format;
957 u16 err;
958 u32 h_control;
959 u32 sample_rate = 48000;
960
961 /* on cards without SRC, must query at valid rate,
962 * maybe set by external sync
963 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300964 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200965 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
966 HPI_CONTROL_SAMPLECLOCK, &h_control);
967
968 if (!err)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300969 err = hpi_sample_clock_get_sample_rate(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200970 &sample_rate);
971
972 for (format = HPI_FORMAT_PCM8_UNSIGNED;
973 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
974 err = hpi_format_create(&hpi_format,
975 2, format, sample_rate, 128000, 0);
976 if (!err)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300977 err = hpi_outstream_query_format(h_stream,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200978 &hpi_format);
979 if (!err && (hpi_to_alsa_formats[format] != -1))
980 pcmhw->formats |=
981 (1ULL << hpi_to_alsa_formats[format]);
982 }
983}
984
985static struct snd_pcm_hardware snd_card_asihpi_playback = {
986 .channels_min = 1,
987 .channels_max = 2,
988 .buffer_bytes_max = BUFFER_BYTES_MAX,
989 .period_bytes_min = PERIOD_BYTES_MIN,
990 .period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN,
991 .periods_min = PERIODS_MIN,
992 .periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN,
993 .fifo_size = 0,
994};
995
996static int snd_card_asihpi_playback_open(struct snd_pcm_substream *substream)
997{
998 struct snd_pcm_runtime *runtime = substream->runtime;
999 struct snd_card_asihpi_pcm *dpcm;
1000 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
1001 int err;
1002
1003 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
1004 if (dpcm == NULL)
1005 return -ENOMEM;
1006
1007 err =
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001008 hpi_outstream_open(card->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001009 substream->number, &dpcm->h_stream);
1010 hpi_handle_error(err);
1011 if (err)
1012 kfree(dpcm);
1013 if (err == HPI_ERROR_OBJ_ALREADY_OPEN)
1014 return -EBUSY;
1015 if (err)
1016 return -EIO;
1017
1018 /*? also check ASI5000 samplerate source
1019 If external, only support external rate.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001020 If internal and other stream playing, can't switch
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001021 */
1022
1023 init_timer(&dpcm->timer);
1024 dpcm->timer.data = (unsigned long) dpcm;
1025 dpcm->timer.function = snd_card_asihpi_timer_function;
1026 dpcm->substream = substream;
1027 runtime->private_data = dpcm;
1028 runtime->private_free = snd_card_asihpi_runtime_free;
1029
1030 snd_card_asihpi_playback.channels_max = card->out_max_chans;
1031 /*?snd_card_asihpi_playback.period_bytes_min =
1032 card->out_max_chans * 4096; */
1033
1034 snd_card_asihpi_playback_format(card, dpcm->h_stream,
1035 &snd_card_asihpi_playback);
1036
1037 snd_card_asihpi_pcm_samplerates(card, &snd_card_asihpi_playback);
1038
1039 snd_card_asihpi_playback.info = SNDRV_PCM_INFO_INTERLEAVED |
1040 SNDRV_PCM_INFO_DOUBLE |
1041 SNDRV_PCM_INFO_BATCH |
1042 SNDRV_PCM_INFO_BLOCK_TRANSFER |
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12001043 SNDRV_PCM_INFO_PAUSE |
1044 SNDRV_PCM_INFO_MMAP |
1045 SNDRV_PCM_INFO_MMAP_VALID;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001046
1047 if (card->support_grouping)
1048 snd_card_asihpi_playback.info |= SNDRV_PCM_INFO_SYNC_START;
1049
1050 /* struct is copied, so can create initializer dynamically */
1051 runtime->hw = snd_card_asihpi_playback;
1052
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12001053 if (card->can_dma)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001054 err = snd_pcm_hw_constraint_pow2(runtime, 0,
1055 SNDRV_PCM_HW_PARAM_BUFFER_BYTES);
1056 if (err < 0)
1057 return err;
1058
1059 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1060 card->update_interval_frames);
Eliot Blennerhassett26aebef2011-03-25 15:25:47 +13001061
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001062 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001063 card->update_interval_frames * 2, UINT_MAX);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001064
1065 snd_pcm_set_sync(substream);
1066
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001067 snd_printdd("playback open\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001068
1069 return 0;
1070}
1071
1072static int snd_card_asihpi_playback_close(struct snd_pcm_substream *substream)
1073{
1074 struct snd_pcm_runtime *runtime = substream->runtime;
1075 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1076
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001077 hpi_handle_error(hpi_outstream_close(dpcm->h_stream));
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001078 snd_printdd("playback close\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001079
1080 return 0;
1081}
1082
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001083static struct snd_pcm_ops snd_card_asihpi_playback_mmap_ops = {
1084 .open = snd_card_asihpi_playback_open,
1085 .close = snd_card_asihpi_playback_close,
1086 .ioctl = snd_card_asihpi_playback_ioctl,
1087 .hw_params = snd_card_asihpi_pcm_hw_params,
1088 .hw_free = snd_card_asihpi_hw_free,
1089 .prepare = snd_card_asihpi_playback_prepare,
1090 .trigger = snd_card_asihpi_trigger,
1091 .pointer = snd_card_asihpi_playback_pointer,
1092};
1093
1094/***************************** CAPTURE OPS ****************/
1095static snd_pcm_uframes_t
1096snd_card_asihpi_capture_pointer(struct snd_pcm_substream *substream)
1097{
1098 struct snd_pcm_runtime *runtime = substream->runtime;
1099 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1100
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001101 snd_printddd("capture pointer %d=%d\n",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001102 substream->number, dpcm->pcm_buf_dma_ofs);
1103 /* NOTE Unlike playback can't use actual samples_played
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001104 for the capture position, because those samples aren't yet in
1105 the local buffer available for reading.
1106 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001107 return bytes_to_frames(runtime, dpcm->pcm_buf_dma_ofs % dpcm->buffer_bytes);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001108}
1109
1110static int snd_card_asihpi_capture_ioctl(struct snd_pcm_substream *substream,
1111 unsigned int cmd, void *arg)
1112{
1113 return snd_pcm_lib_ioctl(substream, cmd, arg);
1114}
1115
1116static int snd_card_asihpi_capture_prepare(struct snd_pcm_substream *substream)
1117{
1118 struct snd_pcm_runtime *runtime = substream->runtime;
1119 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1120
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001121 hpi_handle_error(hpi_instream_reset(dpcm->h_stream));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001122 dpcm->pcm_buf_host_rw_ofs = 0;
1123 dpcm->pcm_buf_dma_ofs = 0;
1124 dpcm->pcm_buf_elapsed_dma_ofs = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001125
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001126 snd_printdd("Capture Prepare %d\n", substream->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001127 return 0;
1128}
1129
1130
1131
1132static void snd_card_asihpi_capture_format(struct snd_card_asihpi *asihpi,
1133 u32 h_stream,
1134 struct snd_pcm_hardware *pcmhw)
1135{
1136 struct hpi_format hpi_format;
1137 u16 format;
1138 u16 err;
1139 u32 h_control;
1140 u32 sample_rate = 48000;
1141
1142 /* on cards without SRC, must query at valid rate,
1143 maybe set by external sync */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001144 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001145 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
1146 HPI_CONTROL_SAMPLECLOCK, &h_control);
1147
1148 if (!err)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001149 err = hpi_sample_clock_get_sample_rate(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001150 &sample_rate);
1151
1152 for (format = HPI_FORMAT_PCM8_UNSIGNED;
1153 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
1154
1155 err = hpi_format_create(&hpi_format, 2, format,
1156 sample_rate, 128000, 0);
1157 if (!err)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001158 err = hpi_instream_query_format(h_stream,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001159 &hpi_format);
1160 if (!err)
1161 pcmhw->formats |=
1162 (1ULL << hpi_to_alsa_formats[format]);
1163 }
1164}
1165
1166
1167static struct snd_pcm_hardware snd_card_asihpi_capture = {
1168 .channels_min = 1,
1169 .channels_max = 2,
1170 .buffer_bytes_max = BUFFER_BYTES_MAX,
1171 .period_bytes_min = PERIOD_BYTES_MIN,
1172 .period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN,
1173 .periods_min = PERIODS_MIN,
1174 .periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN,
1175 .fifo_size = 0,
1176};
1177
1178static int snd_card_asihpi_capture_open(struct snd_pcm_substream *substream)
1179{
1180 struct snd_pcm_runtime *runtime = substream->runtime;
1181 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
1182 struct snd_card_asihpi_pcm *dpcm;
1183 int err;
1184
1185 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
1186 if (dpcm == NULL)
1187 return -ENOMEM;
1188
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001189 snd_printdd("capture open adapter %d stream %d\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001190 card->adapter_index, substream->number);
1191
1192 err = hpi_handle_error(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001193 hpi_instream_open(card->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001194 substream->number, &dpcm->h_stream));
1195 if (err)
1196 kfree(dpcm);
1197 if (err == HPI_ERROR_OBJ_ALREADY_OPEN)
1198 return -EBUSY;
1199 if (err)
1200 return -EIO;
1201
1202
1203 init_timer(&dpcm->timer);
1204 dpcm->timer.data = (unsigned long) dpcm;
1205 dpcm->timer.function = snd_card_asihpi_timer_function;
1206 dpcm->substream = substream;
1207 runtime->private_data = dpcm;
1208 runtime->private_free = snd_card_asihpi_runtime_free;
1209
1210 snd_card_asihpi_capture.channels_max = card->in_max_chans;
1211 snd_card_asihpi_capture_format(card, dpcm->h_stream,
1212 &snd_card_asihpi_capture);
1213 snd_card_asihpi_pcm_samplerates(card, &snd_card_asihpi_capture);
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12001214 snd_card_asihpi_capture.info = SNDRV_PCM_INFO_INTERLEAVED |
1215 SNDRV_PCM_INFO_MMAP |
1216 SNDRV_PCM_INFO_MMAP_VALID;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001217
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001218 if (card->support_grouping)
1219 snd_card_asihpi_capture.info |= SNDRV_PCM_INFO_SYNC_START;
1220
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001221 runtime->hw = snd_card_asihpi_capture;
1222
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12001223 if (card->can_dma)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001224 err = snd_pcm_hw_constraint_pow2(runtime, 0,
1225 SNDRV_PCM_HW_PARAM_BUFFER_BYTES);
1226 if (err < 0)
1227 return err;
1228
1229 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1230 card->update_interval_frames);
1231 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1232 card->update_interval_frames * 2, UINT_MAX);
1233
1234 snd_pcm_set_sync(substream);
1235
1236 return 0;
1237}
1238
1239static int snd_card_asihpi_capture_close(struct snd_pcm_substream *substream)
1240{
1241 struct snd_card_asihpi_pcm *dpcm = substream->runtime->private_data;
1242
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001243 hpi_handle_error(hpi_instream_close(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001244 return 0;
1245}
1246
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001247static struct snd_pcm_ops snd_card_asihpi_capture_mmap_ops = {
1248 .open = snd_card_asihpi_capture_open,
1249 .close = snd_card_asihpi_capture_close,
1250 .ioctl = snd_card_asihpi_capture_ioctl,
1251 .hw_params = snd_card_asihpi_pcm_hw_params,
1252 .hw_free = snd_card_asihpi_hw_free,
1253 .prepare = snd_card_asihpi_capture_prepare,
1254 .trigger = snd_card_asihpi_trigger,
1255 .pointer = snd_card_asihpi_capture_pointer,
1256};
1257
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001258static int __devinit snd_card_asihpi_pcm_new(struct snd_card_asihpi *asihpi,
1259 int device, int substreams)
1260{
1261 struct snd_pcm *pcm;
1262 int err;
1263
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001264 err = snd_pcm_new(asihpi->card, "Asihpi PCM", device,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001265 asihpi->num_outstreams, asihpi->num_instreams,
1266 &pcm);
1267 if (err < 0)
1268 return err;
1269 /* pointer to ops struct is stored, dont change ops afterwards! */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001270 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1271 &snd_card_asihpi_playback_mmap_ops);
1272 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1273 &snd_card_asihpi_capture_mmap_ops);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001274
1275 pcm->private_data = asihpi;
1276 pcm->info_flags = 0;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001277 strcpy(pcm->name, "Asihpi PCM");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001278
1279 /*? do we want to emulate MMAP for non-BBM cards?
1280 Jack doesn't work with ALSAs MMAP emulation - WHY NOT? */
1281 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1282 snd_dma_pci_data(asihpi->pci),
1283 64*1024, BUFFER_BYTES_MAX);
1284
1285 return 0;
1286}
1287
1288/***************************** MIXER CONTROLS ****************/
1289struct hpi_control {
1290 u32 h_control;
1291 u16 control_type;
1292 u16 src_node_type;
1293 u16 src_node_index;
1294 u16 dst_node_type;
1295 u16 dst_node_index;
1296 u16 band;
1297 char name[44]; /* copied to snd_ctl_elem_id.name[44]; */
1298};
1299
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001300static const char * const asihpi_tuner_band_names[] = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001301 "invalid",
1302 "AM",
1303 "FM mono",
1304 "TV NTSC-M",
1305 "FM stereo",
1306 "AUX",
1307 "TV PAL BG",
1308 "TV PAL I",
1309 "TV PAL DK",
1310 "TV SECAM",
1311};
1312
1313compile_time_assert(
1314 (ARRAY_SIZE(asihpi_tuner_band_names) ==
1315 (HPI_TUNER_BAND_LAST+1)),
1316 assert_tuner_band_names_size);
1317
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001318static const char * const asihpi_src_names[] = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001319 "no source",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001320 "PCM",
1321 "Line",
1322 "Digital",
1323 "Tuner",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001324 "RF",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001325 "Clock",
1326 "Bitstream",
1327 "Microphone",
1328 "Cobranet",
1329 "Analog",
1330 "Adapter",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001331};
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001332
1333compile_time_assert(
1334 (ARRAY_SIZE(asihpi_src_names) ==
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12001335 (HPI_SOURCENODE_LAST_INDEX-HPI_SOURCENODE_NONE+1)),
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001336 assert_src_names_size);
1337
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001338static const char * const asihpi_dst_names[] = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001339 "no destination",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001340 "PCM",
1341 "Line",
1342 "Digital",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001343 "RF",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001344 "Speaker",
1345 "Cobranet Out",
1346 "Analog"
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001347};
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001348
1349compile_time_assert(
1350 (ARRAY_SIZE(asihpi_dst_names) ==
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12001351 (HPI_DESTNODE_LAST_INDEX-HPI_DESTNODE_NONE+1)),
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001352 assert_dst_names_size);
1353
1354static inline int ctl_add(struct snd_card *card, struct snd_kcontrol_new *ctl,
1355 struct snd_card_asihpi *asihpi)
1356{
1357 int err;
1358
1359 err = snd_ctl_add(card, snd_ctl_new1(ctl, asihpi));
1360 if (err < 0)
1361 return err;
1362 else if (mixer_dump)
1363 snd_printk(KERN_INFO "added %s(%d)\n", ctl->name, ctl->index);
1364
1365 return 0;
1366}
1367
1368/* Convert HPI control name and location into ALSA control name */
1369static void asihpi_ctl_init(struct snd_kcontrol_new *snd_control,
1370 struct hpi_control *hpi_ctl,
1371 char *name)
1372{
Eliot Blennerhassett550ac6b2011-04-05 20:55:41 +12001373 char *dir;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001374 memset(snd_control, 0, sizeof(*snd_control));
1375 snd_control->name = hpi_ctl->name;
1376 snd_control->private_value = hpi_ctl->h_control;
1377 snd_control->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1378 snd_control->index = 0;
1379
Eliot Blennerhassett550ac6b2011-04-05 20:55:41 +12001380 if (hpi_ctl->src_node_type + HPI_SOURCENODE_NONE == HPI_SOURCENODE_CLOCK_SOURCE)
1381 dir = ""; /* clock is neither capture nor playback */
1382 else if (hpi_ctl->dst_node_type + HPI_DESTNODE_NONE == HPI_DESTNODE_ISTREAM)
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001383 dir = "Capture "; /* On or towards a PCM capture destination*/
1384 else if ((hpi_ctl->src_node_type + HPI_SOURCENODE_NONE != HPI_SOURCENODE_OSTREAM) &&
1385 (!hpi_ctl->dst_node_type))
1386 dir = "Capture "; /* On a source node that is not PCM playback */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001387 else if (hpi_ctl->src_node_type &&
1388 (hpi_ctl->src_node_type + HPI_SOURCENODE_NONE != HPI_SOURCENODE_OSTREAM) &&
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001389 (hpi_ctl->dst_node_type))
1390 dir = "Monitor Playback "; /* Between an input and an output */
1391 else
1392 dir = "Playback "; /* PCM Playback source, or output node */
1393
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001394 if (hpi_ctl->src_node_type && hpi_ctl->dst_node_type)
Eliot Blennerhassett550ac6b2011-04-05 20:55:41 +12001395 sprintf(hpi_ctl->name, "%s %d %s %d %s%s",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001396 asihpi_src_names[hpi_ctl->src_node_type],
1397 hpi_ctl->src_node_index,
1398 asihpi_dst_names[hpi_ctl->dst_node_type],
1399 hpi_ctl->dst_node_index,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001400 dir, name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001401 else if (hpi_ctl->dst_node_type) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001402 sprintf(hpi_ctl->name, "%s %d %s%s",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001403 asihpi_dst_names[hpi_ctl->dst_node_type],
1404 hpi_ctl->dst_node_index,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001405 dir, name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001406 } else {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001407 sprintf(hpi_ctl->name, "%s %d %s%s",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001408 asihpi_src_names[hpi_ctl->src_node_type],
1409 hpi_ctl->src_node_index,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001410 dir, name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001411 }
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001412 /* printk(KERN_INFO "Adding %s %d to %d ", hpi_ctl->name,
1413 hpi_ctl->wSrcNodeType, hpi_ctl->wDstNodeType); */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001414}
1415
1416/*------------------------------------------------------------
1417 Volume controls
1418 ------------------------------------------------------------*/
1419#define VOL_STEP_mB 1
1420static int snd_asihpi_volume_info(struct snd_kcontrol *kcontrol,
1421 struct snd_ctl_elem_info *uinfo)
1422{
1423 u32 h_control = kcontrol->private_value;
1424 u16 err;
1425 /* native gains are in millibels */
1426 short min_gain_mB;
1427 short max_gain_mB;
1428 short step_gain_mB;
1429
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001430 err = hpi_volume_query_range(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001431 &min_gain_mB, &max_gain_mB, &step_gain_mB);
1432 if (err) {
1433 max_gain_mB = 0;
1434 min_gain_mB = -10000;
1435 step_gain_mB = VOL_STEP_mB;
1436 }
1437
1438 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1439 uinfo->count = 2;
1440 uinfo->value.integer.min = min_gain_mB / VOL_STEP_mB;
1441 uinfo->value.integer.max = max_gain_mB / VOL_STEP_mB;
1442 uinfo->value.integer.step = step_gain_mB / VOL_STEP_mB;
1443 return 0;
1444}
1445
1446static int snd_asihpi_volume_get(struct snd_kcontrol *kcontrol,
1447 struct snd_ctl_elem_value *ucontrol)
1448{
1449 u32 h_control = kcontrol->private_value;
1450 short an_gain_mB[HPI_MAX_CHANNELS];
1451
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001452 hpi_handle_error(hpi_volume_get_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001453 ucontrol->value.integer.value[0] = an_gain_mB[0] / VOL_STEP_mB;
1454 ucontrol->value.integer.value[1] = an_gain_mB[1] / VOL_STEP_mB;
1455
1456 return 0;
1457}
1458
1459static int snd_asihpi_volume_put(struct snd_kcontrol *kcontrol,
1460 struct snd_ctl_elem_value *ucontrol)
1461{
1462 int change;
1463 u32 h_control = kcontrol->private_value;
1464 short an_gain_mB[HPI_MAX_CHANNELS];
1465
1466 an_gain_mB[0] =
1467 (ucontrol->value.integer.value[0]) * VOL_STEP_mB;
1468 an_gain_mB[1] =
1469 (ucontrol->value.integer.value[1]) * VOL_STEP_mB;
1470 /* change = asihpi->mixer_volume[addr][0] != left ||
1471 asihpi->mixer_volume[addr][1] != right;
1472 */
1473 change = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001474 hpi_handle_error(hpi_volume_set_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001475 return change;
1476}
1477
1478static const DECLARE_TLV_DB_SCALE(db_scale_100, -10000, VOL_STEP_mB, 0);
1479
1480static int __devinit snd_asihpi_volume_add(struct snd_card_asihpi *asihpi,
1481 struct hpi_control *hpi_ctl)
1482{
1483 struct snd_card *card = asihpi->card;
1484 struct snd_kcontrol_new snd_control;
1485
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001486 asihpi_ctl_init(&snd_control, hpi_ctl, "Volume");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001487 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1488 SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1489 snd_control.info = snd_asihpi_volume_info;
1490 snd_control.get = snd_asihpi_volume_get;
1491 snd_control.put = snd_asihpi_volume_put;
1492 snd_control.tlv.p = db_scale_100;
1493
1494 return ctl_add(card, &snd_control, asihpi);
1495}
1496
1497/*------------------------------------------------------------
1498 Level controls
1499 ------------------------------------------------------------*/
1500static int snd_asihpi_level_info(struct snd_kcontrol *kcontrol,
1501 struct snd_ctl_elem_info *uinfo)
1502{
1503 u32 h_control = kcontrol->private_value;
1504 u16 err;
1505 short min_gain_mB;
1506 short max_gain_mB;
1507 short step_gain_mB;
1508
1509 err =
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001510 hpi_level_query_range(h_control, &min_gain_mB,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001511 &max_gain_mB, &step_gain_mB);
1512 if (err) {
1513 max_gain_mB = 2400;
1514 min_gain_mB = -1000;
1515 step_gain_mB = 100;
1516 }
1517
1518 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1519 uinfo->count = 2;
1520 uinfo->value.integer.min = min_gain_mB / HPI_UNITS_PER_dB;
1521 uinfo->value.integer.max = max_gain_mB / HPI_UNITS_PER_dB;
1522 uinfo->value.integer.step = step_gain_mB / HPI_UNITS_PER_dB;
1523 return 0;
1524}
1525
1526static int snd_asihpi_level_get(struct snd_kcontrol *kcontrol,
1527 struct snd_ctl_elem_value *ucontrol)
1528{
1529 u32 h_control = kcontrol->private_value;
1530 short an_gain_mB[HPI_MAX_CHANNELS];
1531
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001532 hpi_handle_error(hpi_level_get_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001533 ucontrol->value.integer.value[0] =
1534 an_gain_mB[0] / HPI_UNITS_PER_dB;
1535 ucontrol->value.integer.value[1] =
1536 an_gain_mB[1] / HPI_UNITS_PER_dB;
1537
1538 return 0;
1539}
1540
1541static int snd_asihpi_level_put(struct snd_kcontrol *kcontrol,
1542 struct snd_ctl_elem_value *ucontrol)
1543{
1544 int change;
1545 u32 h_control = kcontrol->private_value;
1546 short an_gain_mB[HPI_MAX_CHANNELS];
1547
1548 an_gain_mB[0] =
1549 (ucontrol->value.integer.value[0]) * HPI_UNITS_PER_dB;
1550 an_gain_mB[1] =
1551 (ucontrol->value.integer.value[1]) * HPI_UNITS_PER_dB;
1552 /* change = asihpi->mixer_level[addr][0] != left ||
1553 asihpi->mixer_level[addr][1] != right;
1554 */
1555 change = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001556 hpi_handle_error(hpi_level_set_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001557 return change;
1558}
1559
1560static const DECLARE_TLV_DB_SCALE(db_scale_level, -1000, 100, 0);
1561
1562static int __devinit snd_asihpi_level_add(struct snd_card_asihpi *asihpi,
1563 struct hpi_control *hpi_ctl)
1564{
1565 struct snd_card *card = asihpi->card;
1566 struct snd_kcontrol_new snd_control;
1567
1568 /* can't use 'volume' cos some nodes have volume as well */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001569 asihpi_ctl_init(&snd_control, hpi_ctl, "Level");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001570 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1571 SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1572 snd_control.info = snd_asihpi_level_info;
1573 snd_control.get = snd_asihpi_level_get;
1574 snd_control.put = snd_asihpi_level_put;
1575 snd_control.tlv.p = db_scale_level;
1576
1577 return ctl_add(card, &snd_control, asihpi);
1578}
1579
1580/*------------------------------------------------------------
1581 AESEBU controls
1582 ------------------------------------------------------------*/
1583
1584/* AESEBU format */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001585static const char * const asihpi_aesebu_format_names[] = {
1586 "N/A", "S/PDIF", "AES/EBU" };
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001587
1588static int snd_asihpi_aesebu_format_info(struct snd_kcontrol *kcontrol,
1589 struct snd_ctl_elem_info *uinfo)
1590{
1591 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1592 uinfo->count = 1;
1593 uinfo->value.enumerated.items = 3;
1594
1595 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1596 uinfo->value.enumerated.item =
1597 uinfo->value.enumerated.items - 1;
1598
1599 strcpy(uinfo->value.enumerated.name,
1600 asihpi_aesebu_format_names[uinfo->value.enumerated.item]);
1601
1602 return 0;
1603}
1604
1605static int snd_asihpi_aesebu_format_get(struct snd_kcontrol *kcontrol,
1606 struct snd_ctl_elem_value *ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001607 u16 (*func)(u32, u16 *))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001608{
1609 u32 h_control = kcontrol->private_value;
1610 u16 source, err;
1611
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001612 err = func(h_control, &source);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001613
1614 /* default to N/A */
1615 ucontrol->value.enumerated.item[0] = 0;
1616 /* return success but set the control to N/A */
1617 if (err)
1618 return 0;
1619 if (source == HPI_AESEBU_FORMAT_SPDIF)
1620 ucontrol->value.enumerated.item[0] = 1;
1621 if (source == HPI_AESEBU_FORMAT_AESEBU)
1622 ucontrol->value.enumerated.item[0] = 2;
1623
1624 return 0;
1625}
1626
1627static int snd_asihpi_aesebu_format_put(struct snd_kcontrol *kcontrol,
1628 struct snd_ctl_elem_value *ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001629 u16 (*func)(u32, u16))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001630{
1631 u32 h_control = kcontrol->private_value;
1632
1633 /* default to S/PDIF */
1634 u16 source = HPI_AESEBU_FORMAT_SPDIF;
1635
1636 if (ucontrol->value.enumerated.item[0] == 1)
1637 source = HPI_AESEBU_FORMAT_SPDIF;
1638 if (ucontrol->value.enumerated.item[0] == 2)
1639 source = HPI_AESEBU_FORMAT_AESEBU;
1640
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001641 if (func(h_control, source) != 0)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001642 return -EINVAL;
1643
1644 return 1;
1645}
1646
1647static int snd_asihpi_aesebu_rx_format_get(struct snd_kcontrol *kcontrol,
1648 struct snd_ctl_elem_value *ucontrol) {
1649 return snd_asihpi_aesebu_format_get(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001650 hpi_aesebu_receiver_get_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001651}
1652
1653static int snd_asihpi_aesebu_rx_format_put(struct snd_kcontrol *kcontrol,
1654 struct snd_ctl_elem_value *ucontrol) {
1655 return snd_asihpi_aesebu_format_put(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001656 hpi_aesebu_receiver_set_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001657}
1658
1659static int snd_asihpi_aesebu_rxstatus_info(struct snd_kcontrol *kcontrol,
1660 struct snd_ctl_elem_info *uinfo)
1661{
1662 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1663 uinfo->count = 1;
1664
1665 uinfo->value.integer.min = 0;
1666 uinfo->value.integer.max = 0X1F;
1667 uinfo->value.integer.step = 1;
1668
1669 return 0;
1670}
1671
1672static int snd_asihpi_aesebu_rxstatus_get(struct snd_kcontrol *kcontrol,
1673 struct snd_ctl_elem_value *ucontrol) {
1674
1675 u32 h_control = kcontrol->private_value;
1676 u16 status;
1677
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001678 hpi_handle_error(hpi_aesebu_receiver_get_error_status(
1679 h_control, &status));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001680 ucontrol->value.integer.value[0] = status;
1681 return 0;
1682}
1683
1684static int __devinit snd_asihpi_aesebu_rx_add(struct snd_card_asihpi *asihpi,
1685 struct hpi_control *hpi_ctl)
1686{
1687 struct snd_card *card = asihpi->card;
1688 struct snd_kcontrol_new snd_control;
1689
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001690 asihpi_ctl_init(&snd_control, hpi_ctl, "Format");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001691 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1692 snd_control.info = snd_asihpi_aesebu_format_info;
1693 snd_control.get = snd_asihpi_aesebu_rx_format_get;
1694 snd_control.put = snd_asihpi_aesebu_rx_format_put;
1695
1696
1697 if (ctl_add(card, &snd_control, asihpi) < 0)
1698 return -EINVAL;
1699
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001700 asihpi_ctl_init(&snd_control, hpi_ctl, "Status");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001701 snd_control.access =
1702 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
1703 snd_control.info = snd_asihpi_aesebu_rxstatus_info;
1704 snd_control.get = snd_asihpi_aesebu_rxstatus_get;
1705
1706 return ctl_add(card, &snd_control, asihpi);
1707}
1708
1709static int snd_asihpi_aesebu_tx_format_get(struct snd_kcontrol *kcontrol,
1710 struct snd_ctl_elem_value *ucontrol) {
1711 return snd_asihpi_aesebu_format_get(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001712 hpi_aesebu_transmitter_get_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001713}
1714
1715static int snd_asihpi_aesebu_tx_format_put(struct snd_kcontrol *kcontrol,
1716 struct snd_ctl_elem_value *ucontrol) {
1717 return snd_asihpi_aesebu_format_put(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001718 hpi_aesebu_transmitter_set_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001719}
1720
1721
1722static int __devinit snd_asihpi_aesebu_tx_add(struct snd_card_asihpi *asihpi,
1723 struct hpi_control *hpi_ctl)
1724{
1725 struct snd_card *card = asihpi->card;
1726 struct snd_kcontrol_new snd_control;
1727
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001728 asihpi_ctl_init(&snd_control, hpi_ctl, "Format");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001729 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1730 snd_control.info = snd_asihpi_aesebu_format_info;
1731 snd_control.get = snd_asihpi_aesebu_tx_format_get;
1732 snd_control.put = snd_asihpi_aesebu_tx_format_put;
1733
1734 return ctl_add(card, &snd_control, asihpi);
1735}
1736
1737/*------------------------------------------------------------
1738 Tuner controls
1739 ------------------------------------------------------------*/
1740
1741/* Gain */
1742
1743static int snd_asihpi_tuner_gain_info(struct snd_kcontrol *kcontrol,
1744 struct snd_ctl_elem_info *uinfo)
1745{
1746 u32 h_control = kcontrol->private_value;
1747 u16 err;
1748 short idx;
1749 u16 gain_range[3];
1750
1751 for (idx = 0; idx < 3; idx++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001752 err = hpi_tuner_query_gain(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001753 idx, &gain_range[idx]);
1754 if (err != 0)
1755 return err;
1756 }
1757
1758 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1759 uinfo->count = 1;
1760 uinfo->value.integer.min = ((int)gain_range[0]) / HPI_UNITS_PER_dB;
1761 uinfo->value.integer.max = ((int)gain_range[1]) / HPI_UNITS_PER_dB;
1762 uinfo->value.integer.step = ((int) gain_range[2]) / HPI_UNITS_PER_dB;
1763 return 0;
1764}
1765
1766static int snd_asihpi_tuner_gain_get(struct snd_kcontrol *kcontrol,
1767 struct snd_ctl_elem_value *ucontrol)
1768{
1769 /*
1770 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1771 */
1772 u32 h_control = kcontrol->private_value;
1773 short gain;
1774
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001775 hpi_handle_error(hpi_tuner_get_gain(h_control, &gain));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001776 ucontrol->value.integer.value[0] = gain / HPI_UNITS_PER_dB;
1777
1778 return 0;
1779}
1780
1781static int snd_asihpi_tuner_gain_put(struct snd_kcontrol *kcontrol,
1782 struct snd_ctl_elem_value *ucontrol)
1783{
1784 /*
1785 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1786 */
1787 u32 h_control = kcontrol->private_value;
1788 short gain;
1789
1790 gain = (ucontrol->value.integer.value[0]) * HPI_UNITS_PER_dB;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001791 hpi_handle_error(hpi_tuner_set_gain(h_control, gain));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001792
1793 return 1;
1794}
1795
1796/* Band */
1797
1798static int asihpi_tuner_band_query(struct snd_kcontrol *kcontrol,
1799 u16 *band_list, u32 len) {
1800 u32 h_control = kcontrol->private_value;
1801 u16 err = 0;
1802 u32 i;
1803
1804 for (i = 0; i < len; i++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001805 err = hpi_tuner_query_band(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001806 h_control, i, &band_list[i]);
1807 if (err != 0)
1808 break;
1809 }
1810
1811 if (err && (err != HPI_ERROR_INVALID_OBJ_INDEX))
1812 return -EIO;
1813
1814 return i;
1815}
1816
1817static int snd_asihpi_tuner_band_info(struct snd_kcontrol *kcontrol,
1818 struct snd_ctl_elem_info *uinfo)
1819{
1820 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1821 int num_bands = 0;
1822
1823 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1824 HPI_TUNER_BAND_LAST);
1825
1826 if (num_bands < 0)
1827 return num_bands;
1828
1829 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1830 uinfo->count = 1;
1831 uinfo->value.enumerated.items = num_bands;
1832
1833 if (num_bands > 0) {
1834 if (uinfo->value.enumerated.item >=
1835 uinfo->value.enumerated.items)
1836 uinfo->value.enumerated.item =
1837 uinfo->value.enumerated.items - 1;
1838
1839 strcpy(uinfo->value.enumerated.name,
1840 asihpi_tuner_band_names[
1841 tuner_bands[uinfo->value.enumerated.item]]);
1842
1843 }
1844 return 0;
1845}
1846
1847static int snd_asihpi_tuner_band_get(struct snd_kcontrol *kcontrol,
1848 struct snd_ctl_elem_value *ucontrol)
1849{
1850 u32 h_control = kcontrol->private_value;
1851 /*
1852 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1853 */
1854 u16 band, idx;
1855 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1856 u32 num_bands = 0;
1857
1858 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1859 HPI_TUNER_BAND_LAST);
1860
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001861 hpi_handle_error(hpi_tuner_get_band(h_control, &band));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001862
1863 ucontrol->value.enumerated.item[0] = -1;
1864 for (idx = 0; idx < HPI_TUNER_BAND_LAST; idx++)
1865 if (tuner_bands[idx] == band) {
1866 ucontrol->value.enumerated.item[0] = idx;
1867 break;
1868 }
1869
1870 return 0;
1871}
1872
1873static int snd_asihpi_tuner_band_put(struct snd_kcontrol *kcontrol,
1874 struct snd_ctl_elem_value *ucontrol)
1875{
1876 /*
1877 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1878 */
1879 u32 h_control = kcontrol->private_value;
1880 u16 band;
1881 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1882 u32 num_bands = 0;
1883
1884 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1885 HPI_TUNER_BAND_LAST);
1886
1887 band = tuner_bands[ucontrol->value.enumerated.item[0]];
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001888 hpi_handle_error(hpi_tuner_set_band(h_control, band));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001889
1890 return 1;
1891}
1892
1893/* Freq */
1894
1895static int snd_asihpi_tuner_freq_info(struct snd_kcontrol *kcontrol,
1896 struct snd_ctl_elem_info *uinfo)
1897{
1898 u32 h_control = kcontrol->private_value;
1899 u16 err;
1900 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1901 u16 num_bands = 0, band_iter, idx;
1902 u32 freq_range[3], temp_freq_range[3];
1903
1904 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1905 HPI_TUNER_BAND_LAST);
1906
1907 freq_range[0] = INT_MAX;
1908 freq_range[1] = 0;
1909 freq_range[2] = INT_MAX;
1910
1911 for (band_iter = 0; band_iter < num_bands; band_iter++) {
1912 for (idx = 0; idx < 3; idx++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001913 err = hpi_tuner_query_frequency(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001914 idx, tuner_bands[band_iter],
1915 &temp_freq_range[idx]);
1916 if (err != 0)
1917 return err;
1918 }
1919
1920 /* skip band with bogus stepping */
1921 if (temp_freq_range[2] <= 0)
1922 continue;
1923
1924 if (temp_freq_range[0] < freq_range[0])
1925 freq_range[0] = temp_freq_range[0];
1926 if (temp_freq_range[1] > freq_range[1])
1927 freq_range[1] = temp_freq_range[1];
1928 if (temp_freq_range[2] < freq_range[2])
1929 freq_range[2] = temp_freq_range[2];
1930 }
1931
1932 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1933 uinfo->count = 1;
1934 uinfo->value.integer.min = ((int)freq_range[0]);
1935 uinfo->value.integer.max = ((int)freq_range[1]);
1936 uinfo->value.integer.step = ((int)freq_range[2]);
1937 return 0;
1938}
1939
1940static int snd_asihpi_tuner_freq_get(struct snd_kcontrol *kcontrol,
1941 struct snd_ctl_elem_value *ucontrol)
1942{
1943 u32 h_control = kcontrol->private_value;
1944 u32 freq;
1945
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001946 hpi_handle_error(hpi_tuner_get_frequency(h_control, &freq));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001947 ucontrol->value.integer.value[0] = freq;
1948
1949 return 0;
1950}
1951
1952static int snd_asihpi_tuner_freq_put(struct snd_kcontrol *kcontrol,
1953 struct snd_ctl_elem_value *ucontrol)
1954{
1955 u32 h_control = kcontrol->private_value;
1956 u32 freq;
1957
1958 freq = ucontrol->value.integer.value[0];
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001959 hpi_handle_error(hpi_tuner_set_frequency(h_control, freq));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001960
1961 return 1;
1962}
1963
1964/* Tuner control group initializer */
1965static int __devinit snd_asihpi_tuner_add(struct snd_card_asihpi *asihpi,
1966 struct hpi_control *hpi_ctl)
1967{
1968 struct snd_card *card = asihpi->card;
1969 struct snd_kcontrol_new snd_control;
1970
1971 snd_control.private_value = hpi_ctl->h_control;
1972 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1973
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001974 if (!hpi_tuner_get_gain(hpi_ctl->h_control, NULL)) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001975 asihpi_ctl_init(&snd_control, hpi_ctl, "Gain");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001976 snd_control.info = snd_asihpi_tuner_gain_info;
1977 snd_control.get = snd_asihpi_tuner_gain_get;
1978 snd_control.put = snd_asihpi_tuner_gain_put;
1979
1980 if (ctl_add(card, &snd_control, asihpi) < 0)
1981 return -EINVAL;
1982 }
1983
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001984 asihpi_ctl_init(&snd_control, hpi_ctl, "Band");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001985 snd_control.info = snd_asihpi_tuner_band_info;
1986 snd_control.get = snd_asihpi_tuner_band_get;
1987 snd_control.put = snd_asihpi_tuner_band_put;
1988
1989 if (ctl_add(card, &snd_control, asihpi) < 0)
1990 return -EINVAL;
1991
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001992 asihpi_ctl_init(&snd_control, hpi_ctl, "Freq");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001993 snd_control.info = snd_asihpi_tuner_freq_info;
1994 snd_control.get = snd_asihpi_tuner_freq_get;
1995 snd_control.put = snd_asihpi_tuner_freq_put;
1996
1997 return ctl_add(card, &snd_control, asihpi);
1998}
1999
2000/*------------------------------------------------------------
2001 Meter controls
2002 ------------------------------------------------------------*/
2003static int snd_asihpi_meter_info(struct snd_kcontrol *kcontrol,
2004 struct snd_ctl_elem_info *uinfo)
2005{
2006 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2007 uinfo->count = HPI_MAX_CHANNELS;
2008 uinfo->value.integer.min = 0;
2009 uinfo->value.integer.max = 0x7FFFFFFF;
2010 return 0;
2011}
2012
2013/* linear values for 10dB steps */
2014static int log2lin[] = {
2015 0x7FFFFFFF, /* 0dB */
2016 679093956,
2017 214748365,
2018 67909396,
2019 21474837,
2020 6790940,
2021 2147484, /* -60dB */
2022 679094,
2023 214748, /* -80 */
2024 67909,
2025 21475, /* -100 */
2026 6791,
2027 2147,
2028 679,
2029 214,
2030 68,
2031 21,
2032 7,
2033 2
2034};
2035
2036static int snd_asihpi_meter_get(struct snd_kcontrol *kcontrol,
2037 struct snd_ctl_elem_value *ucontrol)
2038{
2039 u32 h_control = kcontrol->private_value;
2040 short an_gain_mB[HPI_MAX_CHANNELS], i;
2041 u16 err;
2042
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002043 err = hpi_meter_get_peak(h_control, an_gain_mB);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002044
2045 for (i = 0; i < HPI_MAX_CHANNELS; i++) {
2046 if (err) {
2047 ucontrol->value.integer.value[i] = 0;
2048 } else if (an_gain_mB[i] >= 0) {
2049 ucontrol->value.integer.value[i] =
2050 an_gain_mB[i] << 16;
2051 } else {
2052 /* -ve is log value in millibels < -60dB,
2053 * convert to (roughly!) linear,
2054 */
2055 ucontrol->value.integer.value[i] =
2056 log2lin[an_gain_mB[i] / -1000];
2057 }
2058 }
2059 return 0;
2060}
2061
2062static int __devinit snd_asihpi_meter_add(struct snd_card_asihpi *asihpi,
2063 struct hpi_control *hpi_ctl, int subidx)
2064{
2065 struct snd_card *card = asihpi->card;
2066 struct snd_kcontrol_new snd_control;
2067
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002068 asihpi_ctl_init(&snd_control, hpi_ctl, "Meter");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002069 snd_control.access =
2070 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
2071 snd_control.info = snd_asihpi_meter_info;
2072 snd_control.get = snd_asihpi_meter_get;
2073
2074 snd_control.index = subidx;
2075
2076 return ctl_add(card, &snd_control, asihpi);
2077}
2078
2079/*------------------------------------------------------------
2080 Multiplexer controls
2081 ------------------------------------------------------------*/
2082static int snd_card_asihpi_mux_count_sources(struct snd_kcontrol *snd_control)
2083{
2084 u32 h_control = snd_control->private_value;
2085 struct hpi_control hpi_ctl;
2086 int s, err;
2087 for (s = 0; s < 32; s++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002088 err = hpi_multiplexer_query_source(h_control, s,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002089 &hpi_ctl.
2090 src_node_type,
2091 &hpi_ctl.
2092 src_node_index);
2093 if (err)
2094 break;
2095 }
2096 return s;
2097}
2098
2099static int snd_asihpi_mux_info(struct snd_kcontrol *kcontrol,
2100 struct snd_ctl_elem_info *uinfo)
2101{
2102 int err;
2103 u16 src_node_type, src_node_index;
2104 u32 h_control = kcontrol->private_value;
2105
2106 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2107 uinfo->count = 1;
2108 uinfo->value.enumerated.items =
2109 snd_card_asihpi_mux_count_sources(kcontrol);
2110
2111 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2112 uinfo->value.enumerated.item =
2113 uinfo->value.enumerated.items - 1;
2114
2115 err =
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002116 hpi_multiplexer_query_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002117 uinfo->value.enumerated.item,
2118 &src_node_type, &src_node_index);
2119
2120 sprintf(uinfo->value.enumerated.name, "%s %d",
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12002121 asihpi_src_names[src_node_type - HPI_SOURCENODE_NONE],
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002122 src_node_index);
2123 return 0;
2124}
2125
2126static int snd_asihpi_mux_get(struct snd_kcontrol *kcontrol,
2127 struct snd_ctl_elem_value *ucontrol)
2128{
2129 u32 h_control = kcontrol->private_value;
2130 u16 source_type, source_index;
2131 u16 src_node_type, src_node_index;
2132 int s;
2133
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002134 hpi_handle_error(hpi_multiplexer_get_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002135 &source_type, &source_index));
2136 /* Should cache this search result! */
2137 for (s = 0; s < 256; s++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002138 if (hpi_multiplexer_query_source(h_control, s,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002139 &src_node_type, &src_node_index))
2140 break;
2141
2142 if ((source_type == src_node_type)
2143 && (source_index == src_node_index)) {
2144 ucontrol->value.enumerated.item[0] = s;
2145 return 0;
2146 }
2147 }
2148 snd_printd(KERN_WARNING
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002149 "Control %x failed to match mux source %hu %hu\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002150 h_control, source_type, source_index);
2151 ucontrol->value.enumerated.item[0] = 0;
2152 return 0;
2153}
2154
2155static int snd_asihpi_mux_put(struct snd_kcontrol *kcontrol,
2156 struct snd_ctl_elem_value *ucontrol)
2157{
2158 int change;
2159 u32 h_control = kcontrol->private_value;
2160 u16 source_type, source_index;
2161 u16 e;
2162
2163 change = 1;
2164
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002165 e = hpi_multiplexer_query_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002166 ucontrol->value.enumerated.item[0],
2167 &source_type, &source_index);
2168 if (!e)
2169 hpi_handle_error(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002170 hpi_multiplexer_set_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002171 source_type, source_index));
2172 return change;
2173}
2174
2175
2176static int __devinit snd_asihpi_mux_add(struct snd_card_asihpi *asihpi,
2177 struct hpi_control *hpi_ctl)
2178{
2179 struct snd_card *card = asihpi->card;
2180 struct snd_kcontrol_new snd_control;
2181
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002182 asihpi_ctl_init(&snd_control, hpi_ctl, "Route");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002183 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2184 snd_control.info = snd_asihpi_mux_info;
2185 snd_control.get = snd_asihpi_mux_get;
2186 snd_control.put = snd_asihpi_mux_put;
2187
2188 return ctl_add(card, &snd_control, asihpi);
2189
2190}
2191
2192/*------------------------------------------------------------
2193 Channel mode controls
2194 ------------------------------------------------------------*/
2195static int snd_asihpi_cmode_info(struct snd_kcontrol *kcontrol,
2196 struct snd_ctl_elem_info *uinfo)
2197{
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002198 static const char * const mode_names[HPI_CHANNEL_MODE_LAST + 1] = {
2199 "invalid",
2200 "Normal", "Swap",
2201 "From Left", "From Right",
2202 "To Left", "To Right"
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002203 };
2204
2205 u32 h_control = kcontrol->private_value;
2206 u16 mode;
2207 int i;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002208 u16 mode_map[6];
2209 int valid_modes = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002210
2211 /* HPI channel mode values can be from 1 to 6
2212 Some adapters only support a contiguous subset
2213 */
2214 for (i = 0; i < HPI_CHANNEL_MODE_LAST; i++)
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002215 if (!hpi_channel_mode_query_mode(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002216 h_control, i, &mode)) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002217 mode_map[valid_modes] = mode;
2218 valid_modes++;
2219 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002220
2221 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2222 uinfo->count = 1;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002223 uinfo->value.enumerated.items = valid_modes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002224
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002225 if (uinfo->value.enumerated.item >= valid_modes)
2226 uinfo->value.enumerated.item = valid_modes - 1;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002227
2228 strcpy(uinfo->value.enumerated.name,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002229 mode_names[mode_map[uinfo->value.enumerated.item]]);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002230
2231 return 0;
2232}
2233
2234static int snd_asihpi_cmode_get(struct snd_kcontrol *kcontrol,
2235 struct snd_ctl_elem_value *ucontrol)
2236{
2237 u32 h_control = kcontrol->private_value;
2238 u16 mode;
2239
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002240 if (hpi_channel_mode_get(h_control, &mode))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002241 mode = 1;
2242
2243 ucontrol->value.enumerated.item[0] = mode - 1;
2244
2245 return 0;
2246}
2247
2248static int snd_asihpi_cmode_put(struct snd_kcontrol *kcontrol,
2249 struct snd_ctl_elem_value *ucontrol)
2250{
2251 int change;
2252 u32 h_control = kcontrol->private_value;
2253
2254 change = 1;
2255
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002256 hpi_handle_error(hpi_channel_mode_set(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002257 ucontrol->value.enumerated.item[0] + 1));
2258 return change;
2259}
2260
2261
2262static int __devinit snd_asihpi_cmode_add(struct snd_card_asihpi *asihpi,
2263 struct hpi_control *hpi_ctl)
2264{
2265 struct snd_card *card = asihpi->card;
2266 struct snd_kcontrol_new snd_control;
2267
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002268 asihpi_ctl_init(&snd_control, hpi_ctl, "Mode");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002269 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2270 snd_control.info = snd_asihpi_cmode_info;
2271 snd_control.get = snd_asihpi_cmode_get;
2272 snd_control.put = snd_asihpi_cmode_put;
2273
2274 return ctl_add(card, &snd_control, asihpi);
2275}
2276
2277/*------------------------------------------------------------
2278 Sampleclock source controls
2279 ------------------------------------------------------------*/
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002280static char *sampleclock_sources[MAX_CLOCKSOURCES] = {
2281 "N/A", "Local PLL", "Digital Sync", "Word External", "Word Header",
2282 "SMPTE", "Digital1", "Auto", "Network", "Invalid",
2283 "Prev Module",
2284 "Digital2", "Digital3", "Digital4", "Digital5",
2285 "Digital6", "Digital7", "Digital8"};
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002286
2287static int snd_asihpi_clksrc_info(struct snd_kcontrol *kcontrol,
2288 struct snd_ctl_elem_info *uinfo)
2289{
2290 struct snd_card_asihpi *asihpi =
2291 (struct snd_card_asihpi *)(kcontrol->private_data);
2292 struct clk_cache *clkcache = &asihpi->cc;
2293 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2294 uinfo->count = 1;
2295 uinfo->value.enumerated.items = clkcache->count;
2296
2297 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2298 uinfo->value.enumerated.item =
2299 uinfo->value.enumerated.items - 1;
2300
2301 strcpy(uinfo->value.enumerated.name,
2302 clkcache->s[uinfo->value.enumerated.item].name);
2303 return 0;
2304}
2305
2306static int snd_asihpi_clksrc_get(struct snd_kcontrol *kcontrol,
2307 struct snd_ctl_elem_value *ucontrol)
2308{
2309 struct snd_card_asihpi *asihpi =
2310 (struct snd_card_asihpi *)(kcontrol->private_data);
2311 struct clk_cache *clkcache = &asihpi->cc;
2312 u32 h_control = kcontrol->private_value;
2313 u16 source, srcindex = 0;
2314 int i;
2315
2316 ucontrol->value.enumerated.item[0] = 0;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002317 if (hpi_sample_clock_get_source(h_control, &source))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002318 source = 0;
2319
2320 if (source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002321 if (hpi_sample_clock_get_source_index(h_control, &srcindex))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002322 srcindex = 0;
2323
2324 for (i = 0; i < clkcache->count; i++)
2325 if ((clkcache->s[i].source == source) &&
2326 (clkcache->s[i].index == srcindex))
2327 break;
2328
2329 ucontrol->value.enumerated.item[0] = i;
2330
2331 return 0;
2332}
2333
2334static int snd_asihpi_clksrc_put(struct snd_kcontrol *kcontrol,
2335 struct snd_ctl_elem_value *ucontrol)
2336{
2337 struct snd_card_asihpi *asihpi =
2338 (struct snd_card_asihpi *)(kcontrol->private_data);
2339 struct clk_cache *clkcache = &asihpi->cc;
2340 int change, item;
2341 u32 h_control = kcontrol->private_value;
2342
2343 change = 1;
2344 item = ucontrol->value.enumerated.item[0];
2345 if (item >= clkcache->count)
2346 item = clkcache->count-1;
2347
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002348 hpi_handle_error(hpi_sample_clock_set_source(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002349 h_control, clkcache->s[item].source));
2350
2351 if (clkcache->s[item].source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002352 hpi_handle_error(hpi_sample_clock_set_source_index(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002353 h_control, clkcache->s[item].index));
2354 return change;
2355}
2356
2357/*------------------------------------------------------------
2358 Clkrate controls
2359 ------------------------------------------------------------*/
2360/* Need to change this to enumerated control with list of rates */
2361static int snd_asihpi_clklocal_info(struct snd_kcontrol *kcontrol,
2362 struct snd_ctl_elem_info *uinfo)
2363{
2364 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2365 uinfo->count = 1;
2366 uinfo->value.integer.min = 8000;
2367 uinfo->value.integer.max = 192000;
2368 uinfo->value.integer.step = 100;
2369
2370 return 0;
2371}
2372
2373static int snd_asihpi_clklocal_get(struct snd_kcontrol *kcontrol,
2374 struct snd_ctl_elem_value *ucontrol)
2375{
2376 u32 h_control = kcontrol->private_value;
2377 u32 rate;
2378 u16 e;
2379
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002380 e = hpi_sample_clock_get_local_rate(h_control, &rate);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002381 if (!e)
2382 ucontrol->value.integer.value[0] = rate;
2383 else
2384 ucontrol->value.integer.value[0] = 0;
2385 return 0;
2386}
2387
2388static int snd_asihpi_clklocal_put(struct snd_kcontrol *kcontrol,
2389 struct snd_ctl_elem_value *ucontrol)
2390{
2391 int change;
2392 u32 h_control = kcontrol->private_value;
2393
2394 /* change = asihpi->mixer_clkrate[addr][0] != left ||
2395 asihpi->mixer_clkrate[addr][1] != right;
2396 */
2397 change = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002398 hpi_handle_error(hpi_sample_clock_set_local_rate(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002399 ucontrol->value.integer.value[0]));
2400 return change;
2401}
2402
2403static int snd_asihpi_clkrate_info(struct snd_kcontrol *kcontrol,
2404 struct snd_ctl_elem_info *uinfo)
2405{
2406 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2407 uinfo->count = 1;
2408 uinfo->value.integer.min = 8000;
2409 uinfo->value.integer.max = 192000;
2410 uinfo->value.integer.step = 100;
2411
2412 return 0;
2413}
2414
2415static int snd_asihpi_clkrate_get(struct snd_kcontrol *kcontrol,
2416 struct snd_ctl_elem_value *ucontrol)
2417{
2418 u32 h_control = kcontrol->private_value;
2419 u32 rate;
2420 u16 e;
2421
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002422 e = hpi_sample_clock_get_sample_rate(h_control, &rate);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002423 if (!e)
2424 ucontrol->value.integer.value[0] = rate;
2425 else
2426 ucontrol->value.integer.value[0] = 0;
2427 return 0;
2428}
2429
2430static int __devinit snd_asihpi_sampleclock_add(struct snd_card_asihpi *asihpi,
2431 struct hpi_control *hpi_ctl)
2432{
2433 struct snd_card *card = asihpi->card;
2434 struct snd_kcontrol_new snd_control;
2435
2436 struct clk_cache *clkcache = &asihpi->cc;
2437 u32 hSC = hpi_ctl->h_control;
2438 int has_aes_in = 0;
2439 int i, j;
2440 u16 source;
2441
2442 snd_control.private_value = hpi_ctl->h_control;
2443
2444 clkcache->has_local = 0;
2445
2446 for (i = 0; i <= HPI_SAMPLECLOCK_SOURCE_LAST; i++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002447 if (hpi_sample_clock_query_source(hSC,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002448 i, &source))
2449 break;
2450 clkcache->s[i].source = source;
2451 clkcache->s[i].index = 0;
2452 clkcache->s[i].name = sampleclock_sources[source];
2453 if (source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
2454 has_aes_in = 1;
2455 if (source == HPI_SAMPLECLOCK_SOURCE_LOCAL)
2456 clkcache->has_local = 1;
2457 }
2458 if (has_aes_in)
2459 /* already will have picked up index 0 above */
2460 for (j = 1; j < 8; j++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002461 if (hpi_sample_clock_query_source_index(hSC,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002462 j, HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT,
2463 &source))
2464 break;
2465 clkcache->s[i].source =
2466 HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT;
2467 clkcache->s[i].index = j;
2468 clkcache->s[i].name = sampleclock_sources[
2469 j+HPI_SAMPLECLOCK_SOURCE_LAST];
2470 i++;
2471 }
2472 clkcache->count = i;
2473
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002474 asihpi_ctl_init(&snd_control, hpi_ctl, "Source");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002475 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE ;
2476 snd_control.info = snd_asihpi_clksrc_info;
2477 snd_control.get = snd_asihpi_clksrc_get;
2478 snd_control.put = snd_asihpi_clksrc_put;
2479 if (ctl_add(card, &snd_control, asihpi) < 0)
2480 return -EINVAL;
2481
2482
2483 if (clkcache->has_local) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002484 asihpi_ctl_init(&snd_control, hpi_ctl, "Localrate");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002485 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE ;
2486 snd_control.info = snd_asihpi_clklocal_info;
2487 snd_control.get = snd_asihpi_clklocal_get;
2488 snd_control.put = snd_asihpi_clklocal_put;
2489
2490
2491 if (ctl_add(card, &snd_control, asihpi) < 0)
2492 return -EINVAL;
2493 }
2494
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002495 asihpi_ctl_init(&snd_control, hpi_ctl, "Rate");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002496 snd_control.access =
2497 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
2498 snd_control.info = snd_asihpi_clkrate_info;
2499 snd_control.get = snd_asihpi_clkrate_get;
2500
2501 return ctl_add(card, &snd_control, asihpi);
2502}
2503/*------------------------------------------------------------
2504 Mixer
2505 ------------------------------------------------------------*/
2506
2507static int __devinit snd_card_asihpi_mixer_new(struct snd_card_asihpi *asihpi)
2508{
2509 struct snd_card *card = asihpi->card;
2510 unsigned int idx = 0;
2511 unsigned int subindex = 0;
2512 int err;
2513 struct hpi_control hpi_ctl, prev_ctl;
2514
2515 if (snd_BUG_ON(!asihpi))
2516 return -EINVAL;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002517 strcpy(card->mixername, "Asihpi Mixer");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002518
2519 err =
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002520 hpi_mixer_open(asihpi->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002521 &asihpi->h_mixer);
2522 hpi_handle_error(err);
2523 if (err)
2524 return -err;
2525
Takashi Iwai21896bc2010-06-02 12:08:37 +02002526 memset(&prev_ctl, 0, sizeof(prev_ctl));
2527 prev_ctl.control_type = -1;
2528
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002529 for (idx = 0; idx < 2000; idx++) {
2530 err = hpi_mixer_get_control_by_index(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002531 asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002532 idx,
2533 &hpi_ctl.src_node_type,
2534 &hpi_ctl.src_node_index,
2535 &hpi_ctl.dst_node_type,
2536 &hpi_ctl.dst_node_index,
2537 &hpi_ctl.control_type,
2538 &hpi_ctl.h_control);
2539 if (err) {
2540 if (err == HPI_ERROR_CONTROL_DISABLED) {
2541 if (mixer_dump)
2542 snd_printk(KERN_INFO
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002543 "Disabled HPI Control(%d)\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002544 idx);
2545 continue;
2546 } else
2547 break;
2548
2549 }
2550
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12002551 hpi_ctl.src_node_type -= HPI_SOURCENODE_NONE;
2552 hpi_ctl.dst_node_type -= HPI_DESTNODE_NONE;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002553
2554 /* ASI50xx in SSX mode has multiple meters on the same node.
2555 Use subindex to create distinct ALSA controls
2556 for any duplicated controls.
2557 */
2558 if ((hpi_ctl.control_type == prev_ctl.control_type) &&
2559 (hpi_ctl.src_node_type == prev_ctl.src_node_type) &&
2560 (hpi_ctl.src_node_index == prev_ctl.src_node_index) &&
2561 (hpi_ctl.dst_node_type == prev_ctl.dst_node_type) &&
2562 (hpi_ctl.dst_node_index == prev_ctl.dst_node_index))
2563 subindex++;
2564 else
2565 subindex = 0;
2566
2567 prev_ctl = hpi_ctl;
2568
2569 switch (hpi_ctl.control_type) {
2570 case HPI_CONTROL_VOLUME:
2571 err = snd_asihpi_volume_add(asihpi, &hpi_ctl);
2572 break;
2573 case HPI_CONTROL_LEVEL:
2574 err = snd_asihpi_level_add(asihpi, &hpi_ctl);
2575 break;
2576 case HPI_CONTROL_MULTIPLEXER:
2577 err = snd_asihpi_mux_add(asihpi, &hpi_ctl);
2578 break;
2579 case HPI_CONTROL_CHANNEL_MODE:
2580 err = snd_asihpi_cmode_add(asihpi, &hpi_ctl);
2581 break;
2582 case HPI_CONTROL_METER:
2583 err = snd_asihpi_meter_add(asihpi, &hpi_ctl, subindex);
2584 break;
2585 case HPI_CONTROL_SAMPLECLOCK:
2586 err = snd_asihpi_sampleclock_add(
2587 asihpi, &hpi_ctl);
2588 break;
2589 case HPI_CONTROL_CONNECTION: /* ignore these */
2590 continue;
2591 case HPI_CONTROL_TUNER:
2592 err = snd_asihpi_tuner_add(asihpi, &hpi_ctl);
2593 break;
2594 case HPI_CONTROL_AESEBU_TRANSMITTER:
2595 err = snd_asihpi_aesebu_tx_add(asihpi, &hpi_ctl);
2596 break;
2597 case HPI_CONTROL_AESEBU_RECEIVER:
2598 err = snd_asihpi_aesebu_rx_add(asihpi, &hpi_ctl);
2599 break;
2600 case HPI_CONTROL_VOX:
2601 case HPI_CONTROL_BITSTREAM:
2602 case HPI_CONTROL_MICROPHONE:
2603 case HPI_CONTROL_PARAMETRIC_EQ:
2604 case HPI_CONTROL_COMPANDER:
2605 default:
2606 if (mixer_dump)
2607 snd_printk(KERN_INFO
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002608 "Untranslated HPI Control"
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002609 "(%d) %d %d %d %d %d\n",
2610 idx,
2611 hpi_ctl.control_type,
2612 hpi_ctl.src_node_type,
2613 hpi_ctl.src_node_index,
2614 hpi_ctl.dst_node_type,
2615 hpi_ctl.dst_node_index);
2616 continue;
2617 };
2618 if (err < 0)
2619 return err;
2620 }
2621 if (HPI_ERROR_INVALID_OBJ_INDEX != err)
2622 hpi_handle_error(err);
2623
2624 snd_printk(KERN_INFO "%d mixer controls found\n", idx);
2625
2626 return 0;
2627}
2628
2629/*------------------------------------------------------------
2630 /proc interface
2631 ------------------------------------------------------------*/
2632
2633static void
2634snd_asihpi_proc_read(struct snd_info_entry *entry,
2635 struct snd_info_buffer *buffer)
2636{
2637 struct snd_card_asihpi *asihpi = entry->private_data;
2638 u16 version;
2639 u32 h_control;
2640 u32 rate = 0;
2641 u16 source = 0;
2642 int err;
2643
2644 snd_iprintf(buffer, "ASIHPI driver proc file\n");
2645 snd_iprintf(buffer,
2646 "adapter ID=%4X\n_index=%d\n"
2647 "num_outstreams=%d\n_num_instreams=%d\n",
2648 asihpi->type, asihpi->adapter_index,
2649 asihpi->num_outstreams, asihpi->num_instreams);
2650
2651 version = asihpi->version;
2652 snd_iprintf(buffer,
2653 "serial#=%d\n_hw version %c%d\nDSP code version %03d\n",
2654 asihpi->serial_number, ((version >> 3) & 0xf) + 'A',
2655 version & 0x7,
2656 ((version >> 13) * 100) + ((version >> 7) & 0x3f));
2657
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002658 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002659 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
2660 HPI_CONTROL_SAMPLECLOCK, &h_control);
2661
2662 if (!err) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002663 err = hpi_sample_clock_get_sample_rate(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002664 h_control, &rate);
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002665 err += hpi_sample_clock_get_source(h_control, &source);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002666
2667 if (!err)
2668 snd_iprintf(buffer, "sample_clock=%d_hz, source %s\n",
2669 rate, sampleclock_sources[source]);
2670 }
2671
2672}
2673
2674
2675static void __devinit snd_asihpi_proc_init(struct snd_card_asihpi *asihpi)
2676{
2677 struct snd_info_entry *entry;
2678
2679 if (!snd_card_proc_new(asihpi->card, "info", &entry))
2680 snd_info_set_text_ops(entry, asihpi, snd_asihpi_proc_read);
2681}
2682
2683/*------------------------------------------------------------
2684 HWDEP
2685 ------------------------------------------------------------*/
2686
2687static int snd_asihpi_hpi_open(struct snd_hwdep *hw, struct file *file)
2688{
2689 if (enable_hpi_hwdep)
2690 return 0;
2691 else
2692 return -ENODEV;
2693
2694}
2695
2696static int snd_asihpi_hpi_release(struct snd_hwdep *hw, struct file *file)
2697{
2698 if (enable_hpi_hwdep)
2699 return asihpi_hpi_release(file);
2700 else
2701 return -ENODEV;
2702}
2703
2704static int snd_asihpi_hpi_ioctl(struct snd_hwdep *hw, struct file *file,
2705 unsigned int cmd, unsigned long arg)
2706{
2707 if (enable_hpi_hwdep)
2708 return asihpi_hpi_ioctl(file, cmd, arg);
2709 else
2710 return -ENODEV;
2711}
2712
2713
2714/* results in /dev/snd/hwC#D0 file for each card with index #
2715 also /proc/asound/hwdep will contain '#-00: asihpi (HPI) for each card'
2716*/
2717static int __devinit snd_asihpi_hpi_new(struct snd_card_asihpi *asihpi,
2718 int device, struct snd_hwdep **rhwdep)
2719{
2720 struct snd_hwdep *hw;
2721 int err;
2722
2723 if (rhwdep)
2724 *rhwdep = NULL;
2725 err = snd_hwdep_new(asihpi->card, "HPI", device, &hw);
2726 if (err < 0)
2727 return err;
2728 strcpy(hw->name, "asihpi (HPI)");
2729 hw->iface = SNDRV_HWDEP_IFACE_LAST;
2730 hw->ops.open = snd_asihpi_hpi_open;
2731 hw->ops.ioctl = snd_asihpi_hpi_ioctl;
2732 hw->ops.release = snd_asihpi_hpi_release;
2733 hw->private_data = asihpi;
2734 if (rhwdep)
2735 *rhwdep = hw;
2736 return 0;
2737}
2738
2739/*------------------------------------------------------------
2740 CARD
2741 ------------------------------------------------------------*/
2742static int __devinit snd_asihpi_probe(struct pci_dev *pci_dev,
2743 const struct pci_device_id *pci_id)
2744{
2745 int err;
2746
2747 u16 version;
2748 int pcm_substreams;
2749
2750 struct hpi_adapter *hpi_card;
2751 struct snd_card *card;
2752 struct snd_card_asihpi *asihpi;
2753
2754 u32 h_control;
2755 u32 h_stream;
2756
2757 static int dev;
2758 if (dev >= SNDRV_CARDS)
2759 return -ENODEV;
2760
2761 /* Should this be enable[hpi_card->index] ? */
2762 if (!enable[dev]) {
2763 dev++;
2764 return -ENOENT;
2765 }
2766
2767 err = asihpi_adapter_probe(pci_dev, pci_id);
2768 if (err < 0)
2769 return err;
2770
2771 hpi_card = pci_get_drvdata(pci_dev);
2772 /* first try to give the card the same index as its hardware index */
2773 err = snd_card_create(hpi_card->index,
2774 id[hpi_card->index], THIS_MODULE,
2775 sizeof(struct snd_card_asihpi),
2776 &card);
2777 if (err < 0) {
2778 /* if that fails, try the default index==next available */
2779 err =
2780 snd_card_create(index[dev], id[dev],
2781 THIS_MODULE,
2782 sizeof(struct snd_card_asihpi),
2783 &card);
2784 if (err < 0)
2785 return err;
2786 snd_printk(KERN_WARNING
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002787 "**** WARNING **** Adapter index %d->ALSA index %d\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002788 hpi_card->index, card->number);
2789 }
2790
Eliot Blennerhassett12253672011-02-10 17:26:10 +13002791 snd_card_set_dev(card, &pci_dev->dev);
2792
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002793 asihpi = (struct snd_card_asihpi *) card->private_data;
2794 asihpi->card = card;
Eliot Blennerhassett12253672011-02-10 17:26:10 +13002795 asihpi->pci = pci_dev;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002796 asihpi->adapter_index = hpi_card->index;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002797 hpi_handle_error(hpi_adapter_get_info(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002798 asihpi->adapter_index,
2799 &asihpi->num_outstreams,
2800 &asihpi->num_instreams,
2801 &asihpi->version,
2802 &asihpi->serial_number, &asihpi->type));
2803
2804 version = asihpi->version;
2805 snd_printk(KERN_INFO "adapter ID=%4X index=%d num_outstreams=%d "
2806 "num_instreams=%d S/N=%d\n"
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002807 "Hw Version %c%d DSP code version %03d\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002808 asihpi->type, asihpi->adapter_index,
2809 asihpi->num_outstreams,
2810 asihpi->num_instreams, asihpi->serial_number,
2811 ((version >> 3) & 0xf) + 'A',
2812 version & 0x7,
2813 ((version >> 13) * 100) + ((version >> 7) & 0x3f));
2814
2815 pcm_substreams = asihpi->num_outstreams;
2816 if (pcm_substreams < asihpi->num_instreams)
2817 pcm_substreams = asihpi->num_instreams;
2818
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002819 err = hpi_adapter_get_property(asihpi->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002820 HPI_ADAPTER_PROPERTY_CAPS1,
2821 NULL, &asihpi->support_grouping);
2822 if (err)
2823 asihpi->support_grouping = 0;
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_CAPS2,
2827 &asihpi->support_mrx, NULL);
2828 if (err)
2829 asihpi->support_mrx = 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_INTERVAL,
2833 NULL, &asihpi->update_interval_frames);
2834 if (err)
2835 asihpi->update_interval_frames = 512;
2836
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12002837 if (!asihpi->can_dma)
Eliot Blennerhassett26aebef2011-03-25 15:25:47 +13002838 asihpi->update_interval_frames *= 2;
2839
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002840 hpi_handle_error(hpi_instream_open(asihpi->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002841 0, &h_stream));
2842
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002843 err = hpi_instream_host_buffer_free(h_stream);
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12002844 asihpi->can_dma = (!err);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002845
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002846 hpi_handle_error(hpi_instream_close(h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002847
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002848 err = hpi_adapter_get_property(asihpi->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002849 HPI_ADAPTER_PROPERTY_CURCHANNELS,
2850 &asihpi->in_max_chans, &asihpi->out_max_chans);
2851 if (err) {
2852 asihpi->in_max_chans = 2;
2853 asihpi->out_max_chans = 2;
2854 }
2855
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12002856 snd_printk(KERN_INFO "has dma:%d, grouping:%d, mrx:%d\n",
2857 asihpi->can_dma,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002858 asihpi->support_grouping,
2859 asihpi->support_mrx
2860 );
2861
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002862 err = snd_card_asihpi_pcm_new(asihpi, 0, pcm_substreams);
2863 if (err < 0) {
2864 snd_printk(KERN_ERR "pcm_new failed\n");
2865 goto __nodev;
2866 }
2867 err = snd_card_asihpi_mixer_new(asihpi);
2868 if (err < 0) {
2869 snd_printk(KERN_ERR "mixer_new failed\n");
2870 goto __nodev;
2871 }
2872
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002873 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002874 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
2875 HPI_CONTROL_SAMPLECLOCK, &h_control);
2876
2877 if (!err)
2878 err = hpi_sample_clock_set_local_rate(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002879 h_control, adapter_fs);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002880
2881 snd_asihpi_proc_init(asihpi);
2882
2883 /* always create, can be enabled or disabled dynamically
2884 by enable_hwdep module param*/
2885 snd_asihpi_hpi_new(asihpi, 0, NULL);
2886
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12002887 strcpy(card->driver, "ASIHPI");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002888
2889 sprintf(card->shortname, "AudioScience ASI%4X", asihpi->type);
2890 sprintf(card->longname, "%s %i",
2891 card->shortname, asihpi->adapter_index);
2892 err = snd_card_register(card);
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13002893
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002894 if (!err) {
2895 hpi_card->snd_card_asihpi = card;
2896 dev++;
2897 return 0;
2898 }
2899__nodev:
2900 snd_card_free(card);
2901 snd_printk(KERN_ERR "snd_asihpi_probe error %d\n", err);
2902 return err;
2903
2904}
2905
2906static void __devexit snd_asihpi_remove(struct pci_dev *pci_dev)
2907{
2908 struct hpi_adapter *hpi_card = pci_get_drvdata(pci_dev);
2909
2910 snd_card_free(hpi_card->snd_card_asihpi);
2911 hpi_card->snd_card_asihpi = NULL;
2912 asihpi_adapter_remove(pci_dev);
2913}
2914
2915static DEFINE_PCI_DEVICE_TABLE(asihpi_pci_tbl) = {
2916 {HPI_PCI_VENDOR_ID_TI, HPI_PCI_DEV_ID_DSP6205,
2917 HPI_PCI_VENDOR_ID_AUDIOSCIENCE, PCI_ANY_ID, 0, 0,
2918 (kernel_ulong_t)HPI_6205},
2919 {HPI_PCI_VENDOR_ID_TI, HPI_PCI_DEV_ID_PCI2040,
2920 HPI_PCI_VENDOR_ID_AUDIOSCIENCE, PCI_ANY_ID, 0, 0,
2921 (kernel_ulong_t)HPI_6000},
2922 {0,}
2923};
2924MODULE_DEVICE_TABLE(pci, asihpi_pci_tbl);
2925
2926static struct pci_driver driver = {
Takashi Iwai3733e422011-06-10 16:20:20 +02002927 .name = KBUILD_MODNAME,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002928 .id_table = asihpi_pci_tbl,
2929 .probe = snd_asihpi_probe,
2930 .remove = __devexit_p(snd_asihpi_remove),
2931#ifdef CONFIG_PM
2932/* .suspend = snd_asihpi_suspend,
2933 .resume = snd_asihpi_resume, */
2934#endif
2935};
2936
2937static int __init snd_asihpi_init(void)
2938{
2939 asihpi_init();
2940 return pci_register_driver(&driver);
2941}
2942
2943static void __exit snd_asihpi_exit(void)
2944{
2945
2946 pci_unregister_driver(&driver);
2947 asihpi_exit();
2948}
2949
2950module_init(snd_asihpi_init)
2951module_exit(snd_asihpi_exit)
2952