blob: d5258aa738a19790abfd60adca5377b0e82199f9 [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 Blennerhassettb2e65c82011-03-25 15:25:48 +130050#if defined CONFIG_SND_DEBUG_VERBOSE
51/**
52 * snd_printddd - very verbose debug printk
53 * @format: format string
54 *
55 * Works like snd_printk() for debugging purposes.
56 * Ignored when CONFIG_SND_DEBUG_VERBOSE is not set.
57 * Must set snd module debug parameter to 3 to enable at runtime.
58 */
59#define snd_printddd(format, args...) \
60 __snd_printk(3, __FILE__, __LINE__, format, ##args)
Eliot Blennerhassetta6477132011-04-05 20:55:42 +120061
62/* copied from pcm_lib.c, hope later patch will make that version public
63and this copy can be removed */
64static void pcm_debug_name(struct snd_pcm_substream *substream,
65 char *name, size_t len)
66{
67 snprintf(name, len, "pcmC%dD%d%c:%d",
68 substream->pcm->card->number,
69 substream->pcm->device,
70 substream->stream ? 'c' : 'p',
71 substream->number);
72}
73#define DEBUG_NAME(substream, name) char name[16]; pcm_debug_name(substream, name, sizeof(name))
74
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +130075#else
Eliot Blennerhassetta6477132011-04-05 20:55:42 +120076#define snd_printddd(format, args...) do { } while (0)
77#define pcm_debug_name(s, n, l) do { } while (0)
78#define DEBUG_NAME(name, substream) do { } while (0)
79
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +130080#endif
81
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020082static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* index 0-MAX */
83static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
84static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
85static int enable_hpi_hwdep = 1;
86
87module_param_array(index, int, NULL, S_IRUGO);
88MODULE_PARM_DESC(index, "ALSA index value for AudioScience soundcard.");
89
90module_param_array(id, charp, NULL, S_IRUGO);
91MODULE_PARM_DESC(id, "ALSA ID string for AudioScience soundcard.");
92
93module_param_array(enable, bool, NULL, S_IRUGO);
94MODULE_PARM_DESC(enable, "ALSA enable AudioScience soundcard.");
95
96module_param(enable_hpi_hwdep, bool, S_IRUGO|S_IWUSR);
97MODULE_PARM_DESC(enable_hpi_hwdep,
98 "ALSA enable HPI hwdep for AudioScience soundcard ");
99
100/* identify driver */
101#ifdef KERNEL_ALSA_BUILD
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300102static char *build_info = "Built using headers from kernel source";
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200103module_param(build_info, charp, S_IRUGO);
104MODULE_PARM_DESC(build_info, "built using headers from kernel source");
105#else
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300106static char *build_info = "Built within ALSA source";
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200107module_param(build_info, charp, S_IRUGO);
108MODULE_PARM_DESC(build_info, "built within ALSA source");
109#endif
110
111/* set to 1 to dump every control from adapter to log */
112static const int mixer_dump;
113
114#define DEFAULT_SAMPLERATE 44100
115static int adapter_fs = DEFAULT_SAMPLERATE;
116
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200117/* defaults */
118#define PERIODS_MIN 2
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300119#define PERIOD_BYTES_MIN 2048
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200120#define BUFFER_BYTES_MAX (512 * 1024)
121
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200122#define MAX_CLOCKSOURCES (HPI_SAMPLECLOCK_SOURCE_LAST + 1 + 7)
123
124struct clk_source {
125 int source;
126 int index;
127 char *name;
128};
129
130struct clk_cache {
131 int count;
132 int has_local;
133 struct clk_source s[MAX_CLOCKSOURCES];
134};
135
136/* Per card data */
137struct snd_card_asihpi {
138 struct snd_card *card;
139 struct pci_dev *pci;
140 u16 adapter_index;
141 u32 serial_number;
142 u16 type;
143 u16 version;
144 u16 num_outstreams;
145 u16 num_instreams;
146
147 u32 h_mixer;
148 struct clk_cache cc;
149
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200150 u16 can_dma;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200151 u16 support_grouping;
152 u16 support_mrx;
153 u16 update_interval_frames;
154 u16 in_max_chans;
155 u16 out_max_chans;
156};
157
158/* Per stream data */
159struct snd_card_asihpi_pcm {
160 struct timer_list timer;
161 unsigned int respawn_timer;
162 unsigned int hpi_buffer_attached;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300163 unsigned int buffer_bytes;
164 unsigned int period_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200165 unsigned int bytes_per_sec;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300166 unsigned int pcm_buf_host_rw_ofs; /* Host R/W pos */
167 unsigned int pcm_buf_dma_ofs; /* DMA R/W offset in buffer */
168 unsigned int pcm_buf_elapsed_dma_ofs; /* DMA R/W offset in buffer */
Eliot Blennerhassett0b7ce9e2011-04-05 20:55:43 +1200169 unsigned int drained_count;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200170 struct snd_pcm_substream *substream;
171 u32 h_stream;
172 struct hpi_format format;
173};
174
175/* universal stream verbs work with out or in stream handles */
176
177/* Functions to allow driver to give a buffer to HPI for busmastering */
178
179static u16 hpi_stream_host_buffer_attach(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200180 u32 h_stream, /* handle to outstream. */
181 u32 size_in_bytes, /* size in bytes of bus mastering buffer */
182 u32 pci_address
183)
184{
185 struct hpi_message hm;
186 struct hpi_response hr;
187 unsigned int obj = hpi_handle_object(h_stream);
188
189 if (!h_stream)
190 return HPI_ERROR_INVALID_OBJ;
191 hpi_init_message_response(&hm, &hr, obj,
192 obj == HPI_OBJ_OSTREAM ?
193 HPI_OSTREAM_HOSTBUFFER_ALLOC :
194 HPI_ISTREAM_HOSTBUFFER_ALLOC);
195
196 hpi_handle_to_indexes(h_stream, &hm.adapter_index,
197 &hm.obj_index);
198
199 hm.u.d.u.buffer.buffer_size = size_in_bytes;
200 hm.u.d.u.buffer.pci_address = pci_address;
201 hm.u.d.u.buffer.command = HPI_BUFFER_CMD_INTERNAL_GRANTADAPTER;
202 hpi_send_recv(&hm, &hr);
203 return hr.error;
204}
205
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300206static u16 hpi_stream_host_buffer_detach(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200207{
208 struct hpi_message hm;
209 struct hpi_response hr;
210 unsigned int obj = hpi_handle_object(h_stream);
211
212 if (!h_stream)
213 return HPI_ERROR_INVALID_OBJ;
214
215 hpi_init_message_response(&hm, &hr, obj,
216 obj == HPI_OBJ_OSTREAM ?
217 HPI_OSTREAM_HOSTBUFFER_FREE :
218 HPI_ISTREAM_HOSTBUFFER_FREE);
219
220 hpi_handle_to_indexes(h_stream, &hm.adapter_index,
221 &hm.obj_index);
222 hm.u.d.u.buffer.command = HPI_BUFFER_CMD_INTERNAL_REVOKEADAPTER;
223 hpi_send_recv(&hm, &hr);
224 return hr.error;
225}
226
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300227static inline u16 hpi_stream_start(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200228{
229 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300230 return hpi_outstream_start(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200231 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300232 return hpi_instream_start(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200233}
234
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300235static inline u16 hpi_stream_stop(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200236{
237 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300238 return hpi_outstream_stop(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200239 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300240 return hpi_instream_stop(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200241}
242
243static inline u16 hpi_stream_get_info_ex(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200244 u32 h_stream,
245 u16 *pw_state,
246 u32 *pbuffer_size,
247 u32 *pdata_in_buffer,
248 u32 *psample_count,
249 u32 *pauxiliary_data
250)
251{
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300252 u16 e;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200253 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300254 e = hpi_outstream_get_info_ex(h_stream, pw_state,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200255 pbuffer_size, pdata_in_buffer,
256 psample_count, pauxiliary_data);
257 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300258 e = hpi_instream_get_info_ex(h_stream, pw_state,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200259 pbuffer_size, pdata_in_buffer,
260 psample_count, pauxiliary_data);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300261 return e;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200262}
263
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300264static inline u16 hpi_stream_group_add(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200265 u32 h_master,
266 u32 h_stream)
267{
268 if (hpi_handle_object(h_master) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300269 return hpi_outstream_group_add(h_master, h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200270 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300271 return hpi_instream_group_add(h_master, h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200272}
273
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300274static inline u16 hpi_stream_group_reset(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200275{
276 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300277 return hpi_outstream_group_reset(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200278 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300279 return hpi_instream_group_reset(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200280}
281
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300282static inline u16 hpi_stream_group_get_map(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200283 u32 h_stream, u32 *mo, u32 *mi)
284{
285 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300286 return hpi_outstream_group_get_map(h_stream, mo, mi);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200287 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300288 return hpi_instream_group_get_map(h_stream, mo, mi);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200289}
290
291static u16 handle_error(u16 err, int line, char *filename)
292{
293 if (err)
294 printk(KERN_WARNING
295 "in file %s, line %d: HPI error %d\n",
296 filename, line, err);
297 return err;
298}
299
300#define hpi_handle_error(x) handle_error(x, __LINE__, __FILE__)
301
302/***************************** GENERAL PCM ****************/
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200303
304static void print_hwparams(struct snd_pcm_substream *substream,
305 struct snd_pcm_hw_params *p)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200306{
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200307 DEBUG_NAME(substream, name);
308 snd_printd("%s HWPARAMS\n", name);
309 snd_printd(" samplerate %d Hz\n", params_rate(p));
310 snd_printd(" channels %d\n", params_channels(p));
311 snd_printd(" format %d\n", params_format(p));
312 snd_printd(" subformat %d\n", params_subformat(p));
313 snd_printd(" buffer %d B\n", params_buffer_bytes(p));
314 snd_printd(" period %d B\n", params_period_bytes(p));
315 snd_printd(" access %d\n", params_access(p));
316 snd_printd(" period_size %d\n", params_period_size(p));
317 snd_printd(" periods %d\n", params_periods(p));
318 snd_printd(" buffer_size %d\n", params_buffer_size(p));
319 snd_printd(" %d B/s\n", params_rate(p) *
320 params_channels(p) *
321 snd_pcm_format_width(params_format(p)) / 8);
322
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200323}
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200324
325static snd_pcm_format_t hpi_to_alsa_formats[] = {
326 -1, /* INVALID */
327 SNDRV_PCM_FORMAT_U8, /* HPI_FORMAT_PCM8_UNSIGNED 1 */
328 SNDRV_PCM_FORMAT_S16, /* HPI_FORMAT_PCM16_SIGNED 2 */
329 -1, /* HPI_FORMAT_MPEG_L1 3 */
330 SNDRV_PCM_FORMAT_MPEG, /* HPI_FORMAT_MPEG_L2 4 */
331 SNDRV_PCM_FORMAT_MPEG, /* HPI_FORMAT_MPEG_L3 5 */
332 -1, /* HPI_FORMAT_DOLBY_AC2 6 */
333 -1, /* HPI_FORMAT_DOLBY_AC3 7 */
334 SNDRV_PCM_FORMAT_S16_BE,/* HPI_FORMAT_PCM16_BIGENDIAN 8 */
335 -1, /* HPI_FORMAT_AA_TAGIT1_HITS 9 */
336 -1, /* HPI_FORMAT_AA_TAGIT1_INSERTS 10 */
337 SNDRV_PCM_FORMAT_S32, /* HPI_FORMAT_PCM32_SIGNED 11 */
338 -1, /* HPI_FORMAT_RAW_BITSTREAM 12 */
339 -1, /* HPI_FORMAT_AA_TAGIT1_HITS_EX1 13 */
340 SNDRV_PCM_FORMAT_FLOAT, /* HPI_FORMAT_PCM32_FLOAT 14 */
341#if 1
342 /* ALSA can't handle 3 byte sample size together with power-of-2
343 * constraint on buffer_bytes, so disable this format
344 */
345 -1
346#else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300347 /* SNDRV_PCM_FORMAT_S24_3LE */ /* HPI_FORMAT_PCM24_SIGNED 15 */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200348#endif
349};
350
351
352static int snd_card_asihpi_format_alsa2hpi(snd_pcm_format_t alsa_format,
353 u16 *hpi_format)
354{
355 u16 format;
356
357 for (format = HPI_FORMAT_PCM8_UNSIGNED;
358 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
359 if (hpi_to_alsa_formats[format] == alsa_format) {
360 *hpi_format = format;
361 return 0;
362 }
363 }
364
365 snd_printd(KERN_WARNING "failed match for alsa format %d\n",
366 alsa_format);
367 *hpi_format = 0;
368 return -EINVAL;
369}
370
371static void snd_card_asihpi_pcm_samplerates(struct snd_card_asihpi *asihpi,
372 struct snd_pcm_hardware *pcmhw)
373{
374 u16 err;
375 u32 h_control;
376 u32 sample_rate;
377 int idx;
378 unsigned int rate_min = 200000;
379 unsigned int rate_max = 0;
380 unsigned int rates = 0;
381
382 if (asihpi->support_mrx) {
383 rates |= SNDRV_PCM_RATE_CONTINUOUS;
384 rates |= SNDRV_PCM_RATE_8000_96000;
385 rate_min = 8000;
386 rate_max = 100000;
387 } else {
388 /* on cards without SRC,
389 valid rates are determined by sampleclock */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300390 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200391 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
392 HPI_CONTROL_SAMPLECLOCK, &h_control);
393 if (err) {
394 snd_printk(KERN_ERR
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300395 "No local sampleclock, err %d\n", err);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200396 }
397
Eliot Blennerhassett7bf76c32011-03-25 15:25:46 +1300398 for (idx = -1; idx < 100; idx++) {
399 if (idx == -1) {
400 if (hpi_sample_clock_get_sample_rate(h_control,
401 &sample_rate))
402 continue;
403 } else if (hpi_sample_clock_query_local_rate(h_control,
404 idx, &sample_rate)) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200405 break;
406 }
407
408 rate_min = min(rate_min, sample_rate);
409 rate_max = max(rate_max, sample_rate);
410
411 switch (sample_rate) {
412 case 5512:
413 rates |= SNDRV_PCM_RATE_5512;
414 break;
415 case 8000:
416 rates |= SNDRV_PCM_RATE_8000;
417 break;
418 case 11025:
419 rates |= SNDRV_PCM_RATE_11025;
420 break;
421 case 16000:
422 rates |= SNDRV_PCM_RATE_16000;
423 break;
424 case 22050:
425 rates |= SNDRV_PCM_RATE_22050;
426 break;
427 case 32000:
428 rates |= SNDRV_PCM_RATE_32000;
429 break;
430 case 44100:
431 rates |= SNDRV_PCM_RATE_44100;
432 break;
433 case 48000:
434 rates |= SNDRV_PCM_RATE_48000;
435 break;
436 case 64000:
437 rates |= SNDRV_PCM_RATE_64000;
438 break;
439 case 88200:
440 rates |= SNDRV_PCM_RATE_88200;
441 break;
442 case 96000:
443 rates |= SNDRV_PCM_RATE_96000;
444 break;
445 case 176400:
446 rates |= SNDRV_PCM_RATE_176400;
447 break;
448 case 192000:
449 rates |= SNDRV_PCM_RATE_192000;
450 break;
451 default: /* some other rate */
452 rates |= SNDRV_PCM_RATE_KNOT;
453 }
454 }
455 }
456
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200457 pcmhw->rates = rates;
458 pcmhw->rate_min = rate_min;
459 pcmhw->rate_max = rate_max;
460}
461
462static int snd_card_asihpi_pcm_hw_params(struct snd_pcm_substream *substream,
463 struct snd_pcm_hw_params *params)
464{
465 struct snd_pcm_runtime *runtime = substream->runtime;
466 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
467 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
468 int err;
469 u16 format;
Kulikov Vasiliy315e8f72010-07-15 22:48:19 +0400470 int width;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200471 unsigned int bytes_per_sec;
472
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200473 print_hwparams(substream, params);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200474 err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
475 if (err < 0)
476 return err;
477 err = snd_card_asihpi_format_alsa2hpi(params_format(params), &format);
478 if (err)
479 return err;
480
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200481 hpi_handle_error(hpi_format_create(&dpcm->format,
482 params_channels(params),
483 format, params_rate(params), 0, 0));
484
485 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300486 if (hpi_instream_reset(dpcm->h_stream) != 0)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200487 return -EINVAL;
488
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300489 if (hpi_instream_set_format(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200490 dpcm->h_stream, &dpcm->format) != 0)
491 return -EINVAL;
492 }
493
494 dpcm->hpi_buffer_attached = 0;
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200495 if (card->can_dma) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300496 err = hpi_stream_host_buffer_attach(dpcm->h_stream,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200497 params_buffer_bytes(params), runtime->dma_addr);
498 if (err == 0) {
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300499 snd_printdd(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200500 "stream_host_buffer_attach succeeded %u %lu\n",
501 params_buffer_bytes(params),
502 (unsigned long)runtime->dma_addr);
503 } else {
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300504 snd_printd("stream_host_buffer_attach error %d\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200505 err);
506 return -ENOMEM;
507 }
508
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300509 err = hpi_stream_get_info_ex(dpcm->h_stream, NULL,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200510 &dpcm->hpi_buffer_attached,
511 NULL, NULL, NULL);
512
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300513 snd_printdd("stream_host_buffer_attach status 0x%x\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200514 dpcm->hpi_buffer_attached);
515 }
516 bytes_per_sec = params_rate(params) * params_channels(params);
Kulikov Vasiliy315e8f72010-07-15 22:48:19 +0400517 width = snd_pcm_format_width(params_format(params));
518 bytes_per_sec *= width;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200519 bytes_per_sec /= 8;
Kulikov Vasiliy315e8f72010-07-15 22:48:19 +0400520 if (width < 0 || bytes_per_sec == 0)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200521 return -EINVAL;
522
523 dpcm->bytes_per_sec = bytes_per_sec;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300524 dpcm->buffer_bytes = params_buffer_bytes(params);
525 dpcm->period_bytes = params_period_bytes(params);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200526
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200527 return 0;
528}
529
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300530static int
531snd_card_asihpi_hw_free(struct snd_pcm_substream *substream)
532{
533 struct snd_pcm_runtime *runtime = substream->runtime;
534 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
535 if (dpcm->hpi_buffer_attached)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300536 hpi_stream_host_buffer_detach(dpcm->h_stream);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300537
538 snd_pcm_lib_free_pages(substream);
539 return 0;
540}
541
542static void snd_card_asihpi_runtime_free(struct snd_pcm_runtime *runtime)
543{
544 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
545 kfree(dpcm);
546}
547
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200548static void snd_card_asihpi_pcm_timer_start(struct snd_pcm_substream *
549 substream)
550{
551 struct snd_pcm_runtime *runtime = substream->runtime;
552 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
553 int expiry;
554
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300555 expiry = HZ / 200;
556 /*? (dpcm->period_bytes * HZ / dpcm->bytes_per_sec); */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300557 expiry = max(expiry, 1); /* don't let it be zero! */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200558 dpcm->timer.expires = jiffies + expiry;
559 dpcm->respawn_timer = 1;
560 add_timer(&dpcm->timer);
561}
562
563static void snd_card_asihpi_pcm_timer_stop(struct snd_pcm_substream *substream)
564{
565 struct snd_pcm_runtime *runtime = substream->runtime;
566 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
567
568 dpcm->respawn_timer = 0;
569 del_timer(&dpcm->timer);
570}
571
572static int snd_card_asihpi_trigger(struct snd_pcm_substream *substream,
573 int cmd)
574{
575 struct snd_card_asihpi_pcm *dpcm = substream->runtime->private_data;
576 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
577 struct snd_pcm_substream *s;
578 u16 e;
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200579 DEBUG_NAME(substream, name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200580
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200581 snd_printdd("%s trigger\n", name);
582
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200583 switch (cmd) {
584 case SNDRV_PCM_TRIGGER_START:
585 snd_pcm_group_for_each_entry(s, substream) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300586 struct snd_pcm_runtime *runtime = s->runtime;
587 struct snd_card_asihpi_pcm *ds = runtime->private_data;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200588
589 if (snd_pcm_substream_chip(s) != card)
590 continue;
591
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300592 /* don't link Cap and Play */
593 if (substream->stream != s->stream)
594 continue;
595
Eliot Blennerhassett0b7ce9e2011-04-05 20:55:43 +1200596 ds->drained_count = 0;
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200597 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200598 /* How do I know how much valid data is present
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300599 * in buffer? Must be at least one period!
600 * Guessing 2 periods, but if
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200601 * buffer is bigger it may contain even more
602 * data??
603 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300604 unsigned int preload = ds->period_bytes * 1;
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300605 snd_printddd("%d preload x%x\n", s->number, preload);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200606 hpi_handle_error(hpi_outstream_write_buf(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300607 ds->h_stream,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300608 &runtime->dma_area[0],
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200609 preload,
610 &ds->format));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300611 ds->pcm_buf_host_rw_ofs = preload;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200612 }
613
614 if (card->support_grouping) {
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200615 snd_printdd("%d group\n", s->number);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300616 e = hpi_stream_group_add(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200617 dpcm->h_stream,
618 ds->h_stream);
619 if (!e) {
620 snd_pcm_trigger_done(s, substream);
621 } else {
622 hpi_handle_error(e);
623 break;
624 }
625 } else
626 break;
627 }
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300628 snd_printdd("start\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200629 /* start the master stream */
630 snd_card_asihpi_pcm_timer_start(substream);
Eliot Blennerhassettc4ed97d2011-02-10 17:26:20 +1300631 if ((substream->stream == SNDRV_PCM_STREAM_CAPTURE) ||
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200632 !card->can_dma)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300633 hpi_handle_error(hpi_stream_start(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200634 break;
635
636 case SNDRV_PCM_TRIGGER_STOP:
637 snd_card_asihpi_pcm_timer_stop(substream);
638 snd_pcm_group_for_each_entry(s, substream) {
639 if (snd_pcm_substream_chip(s) != card)
640 continue;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300641 /* don't link Cap and Play */
642 if (substream->stream != s->stream)
643 continue;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200644
645 /*? workaround linked streams don't
646 transition to SETUP 20070706*/
647 s->runtime->status->state = SNDRV_PCM_STATE_SETUP;
648
649 if (card->support_grouping) {
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200650 snd_printdd("%d group\n", s->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200651 snd_pcm_trigger_done(s, substream);
652 } else
653 break;
654 }
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300655 snd_printdd("stop\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200656
657 /* _prepare and _hwparams reset the stream */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300658 hpi_handle_error(hpi_stream_stop(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200659 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
660 hpi_handle_error(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300661 hpi_outstream_reset(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200662
663 if (card->support_grouping)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300664 hpi_handle_error(hpi_stream_group_reset(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200665 break;
666
667 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300668 snd_printdd("pause release\n");
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300669 hpi_handle_error(hpi_stream_start(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200670 snd_card_asihpi_pcm_timer_start(substream);
671 break;
672 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300673 snd_printdd("pause\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200674 snd_card_asihpi_pcm_timer_stop(substream);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300675 hpi_handle_error(hpi_stream_stop(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200676 break;
677 default:
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300678 snd_printd(KERN_ERR "\tINVALID\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200679 return -EINVAL;
680 }
681
682 return 0;
683}
684
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200685/*algorithm outline
686 Without linking degenerates to getting single stream pos etc
687 Without mmap 2nd loop degenerates to snd_pcm_period_elapsed
688*/
689/*
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300690pcm_buf_dma_ofs=get_buf_pos(s);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200691for_each_linked_stream(s) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300692 pcm_buf_dma_ofs=get_buf_pos(s);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300693 min_buf_pos = modulo_min(min_buf_pos, pcm_buf_dma_ofs, buffer_bytes)
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300694 new_data = min(new_data, calc_new_data(pcm_buf_dma_ofs,irq_pos)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200695}
696timer.expires = jiffies + predict_next_period_ready(min_buf_pos);
697for_each_linked_stream(s) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300698 s->pcm_buf_dma_ofs = min_buf_pos;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300699 if (new_data > period_bytes) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200700 if (mmap) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300701 irq_pos = (irq_pos + period_bytes) % buffer_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200702 if (playback) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300703 write(period_bytes);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200704 } else {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300705 read(period_bytes);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200706 }
707 }
708 snd_pcm_period_elapsed(s);
709 }
710}
711*/
712
713/** Minimum of 2 modulo values. Works correctly when the difference between
714* the values is less than half the modulus
715*/
716static inline unsigned int modulo_min(unsigned int a, unsigned int b,
717 unsigned long int modulus)
718{
719 unsigned int result;
720 if (((a-b) % modulus) < (modulus/2))
721 result = b;
722 else
723 result = a;
724
725 return result;
726}
727
728/** Timer function, equivalent to interrupt service routine for cards
729*/
730static void snd_card_asihpi_timer_function(unsigned long data)
731{
732 struct snd_card_asihpi_pcm *dpcm = (struct snd_card_asihpi_pcm *)data;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300733 struct snd_pcm_substream *substream = dpcm->substream;
734 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200735 struct snd_pcm_runtime *runtime;
736 struct snd_pcm_substream *s;
737 unsigned int newdata = 0;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300738 unsigned int pcm_buf_dma_ofs, min_buf_pos = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200739 unsigned int remdata, xfercount, next_jiffies;
740 int first = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300741 int loops = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200742 u16 state;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300743 u32 buffer_size, bytes_avail, samples_played, on_card_bytes;
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200744 DEBUG_NAME(substream, name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200745
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200746 snd_printdd("%s snd_card_asihpi_timer_function\n", name);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300747
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200748 /* find minimum newdata and buffer pos in group */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300749 snd_pcm_group_for_each_entry(s, substream) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200750 struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
751 runtime = s->runtime;
752
753 if (snd_pcm_substream_chip(s) != card)
754 continue;
755
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300756 /* don't link Cap and Play */
757 if (substream->stream != s->stream)
758 continue;
759
760 hpi_handle_error(hpi_stream_get_info_ex(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200761 ds->h_stream, &state,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300762 &buffer_size, &bytes_avail,
763 &samples_played, &on_card_bytes));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200764
765 /* number of bytes in on-card buffer */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300766 runtime->delay = on_card_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200767
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200768 if (!card->can_dma)
769 on_card_bytes = bytes_avail;
770
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300771 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300772 pcm_buf_dma_ofs = ds->pcm_buf_host_rw_ofs - bytes_avail;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300773 if (state == HPI_STATE_STOPPED) {
774 if ((bytes_avail == 0) &&
775 (on_card_bytes < ds->pcm_buf_host_rw_ofs)) {
776 hpi_handle_error(hpi_stream_start(ds->h_stream));
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300777 snd_printdd("P%d start\n", s->number);
Eliot Blennerhassett0b7ce9e2011-04-05 20:55:43 +1200778 ds->drained_count = 0;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300779 }
780 } else if (state == HPI_STATE_DRAINED) {
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300781 snd_printd(KERN_WARNING "P%d drained\n",
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300782 s->number);
Eliot Blennerhassett0b7ce9e2011-04-05 20:55:43 +1200783 ds->drained_count++;
784 if (ds->drained_count > 2) {
785 snd_pcm_stop(s, SNDRV_PCM_STATE_XRUN);
786 continue;
787 }
788 } else {
789 ds->drained_count = 0;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300790 }
791 } else
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300792 pcm_buf_dma_ofs = bytes_avail + ds->pcm_buf_host_rw_ofs;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200793
794 if (first) {
795 /* can't statically init min when wrap is involved */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300796 min_buf_pos = pcm_buf_dma_ofs;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300797 newdata = (pcm_buf_dma_ofs - ds->pcm_buf_elapsed_dma_ofs) % ds->buffer_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200798 first = 0;
799 } else {
800 min_buf_pos =
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300801 modulo_min(min_buf_pos, pcm_buf_dma_ofs, UINT_MAX+1L);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200802 newdata = min(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300803 (pcm_buf_dma_ofs - ds->pcm_buf_elapsed_dma_ofs) % ds->buffer_bytes,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200804 newdata);
805 }
806
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200807 snd_printdd("hw_ptr 0x%04lX, appl_ptr 0x%04lX\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200808 (unsigned long)frames_to_bytes(runtime,
809 runtime->status->hw_ptr),
810 (unsigned long)frames_to_bytes(runtime,
811 runtime->control->appl_ptr));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300812
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200813 snd_printdd("%d S=%d, "
814 "rw=0x%04X, dma=0x%04X, left=0x%04X, "
815 "aux=0x%04X space=0x%04X\n",
816 s->number, state,
817 ds->pcm_buf_host_rw_ofs, pcm_buf_dma_ofs,
818 (int)bytes_avail,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300819 (int)on_card_bytes, buffer_size-bytes_avail);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300820 loops++;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200821 }
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300822 pcm_buf_dma_ofs = min_buf_pos;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200823
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300824 remdata = newdata % dpcm->period_bytes;
825 xfercount = newdata - remdata; /* a multiple of period_bytes */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300826 /* come back when on_card_bytes has decreased enough to allow
827 write to happen, or when data has been consumed to make another
828 period
829 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300830 if (xfercount && (on_card_bytes > dpcm->period_bytes))
831 next_jiffies = ((on_card_bytes - dpcm->period_bytes) * HZ / dpcm->bytes_per_sec);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300832 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300833 next_jiffies = ((dpcm->period_bytes - remdata) * HZ / dpcm->bytes_per_sec);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300834
835 next_jiffies = max(next_jiffies, 1U);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200836 dpcm->timer.expires = jiffies + next_jiffies;
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200837 snd_printdd("jif %d buf pos 0x%04X newdata 0x%04X xfer 0x%04X\n",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300838 next_jiffies, pcm_buf_dma_ofs, newdata, xfercount);
839
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300840 snd_pcm_group_for_each_entry(s, substream) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200841 struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200842
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300843 /* don't link Cap and Play */
844 if (substream->stream != s->stream)
845 continue;
846
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300847 ds->pcm_buf_dma_ofs = pcm_buf_dma_ofs;
848
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200849 if (xfercount &&
850 /* Limit use of on card fifo for playback */
851 ((on_card_bytes <= ds->period_bytes) ||
852 (s->stream == SNDRV_PCM_STREAM_CAPTURE)))
853
854 {
855
856 unsigned int buf_ofs = ds->pcm_buf_host_rw_ofs % ds->buffer_bytes;
857 unsigned int xfer1, xfer2;
858 char *pd = &s->runtime->dma_area[buf_ofs];
859
860 if (card->can_dma) {
861 xfer1 = xfercount;
862 xfer2 = 0;
863 } else {
864 xfer1 = min(xfercount, ds->buffer_bytes - buf_ofs);
865 xfer2 = xfercount - xfer1;
866 }
867
868 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
869 snd_printddd("P%d write1 0x%04X 0x%04X\n",
870 s->number, xfer1, buf_ofs);
871 hpi_handle_error(
872 hpi_outstream_write_buf(
873 ds->h_stream, pd, xfer1,
874 &ds->format));
875
876 if (xfer2) {
877 pd = s->runtime->dma_area;
878
879 snd_printddd("P%d write2 0x%04X 0x%04X\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200880 s->number,
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200881 xfercount - xfer1, buf_ofs);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200882 hpi_handle_error(
883 hpi_outstream_write_buf(
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200884 ds->h_stream, pd,
885 xfercount - xfer1,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200886 &ds->format));
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200887 }
888 } else {
889 snd_printddd("C%d read1 0x%04x\n",
890 s->number, xfer1);
891 hpi_handle_error(
892 hpi_instream_read_buf(
893 ds->h_stream,
894 pd, xfer1));
895 if (xfer2) {
896 pd = s->runtime->dma_area;
897 snd_printddd("C%d read2 0x%04x\n",
898 s->number, xfer2);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200899 hpi_handle_error(
900 hpi_instream_read_buf(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300901 ds->h_stream,
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200902 pd, xfer2));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200903 }
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200904 }
905 ds->pcm_buf_host_rw_ofs = ds->pcm_buf_host_rw_ofs + xfercount;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300906 ds->pcm_buf_elapsed_dma_ofs = pcm_buf_dma_ofs;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200907 snd_pcm_period_elapsed(s);
908 }
909 }
910
911 if (dpcm->respawn_timer)
912 add_timer(&dpcm->timer);
913}
914
915/***************************** PLAYBACK OPS ****************/
916static int snd_card_asihpi_playback_ioctl(struct snd_pcm_substream *substream,
917 unsigned int cmd, void *arg)
918{
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200919 snd_printddd(KERN_INFO "P%d ioctl %d\n", substream->number, cmd);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200920 return snd_pcm_lib_ioctl(substream, cmd, arg);
921}
922
923static int snd_card_asihpi_playback_prepare(struct snd_pcm_substream *
924 substream)
925{
926 struct snd_pcm_runtime *runtime = substream->runtime;
927 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
928
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200929 snd_printdd("P%d prepare\n", substream->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200930
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300931 hpi_handle_error(hpi_outstream_reset(dpcm->h_stream));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300932 dpcm->pcm_buf_host_rw_ofs = 0;
933 dpcm->pcm_buf_dma_ofs = 0;
934 dpcm->pcm_buf_elapsed_dma_ofs = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200935 return 0;
936}
937
938static snd_pcm_uframes_t
939snd_card_asihpi_playback_pointer(struct snd_pcm_substream *substream)
940{
941 struct snd_pcm_runtime *runtime = substream->runtime;
942 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
943 snd_pcm_uframes_t ptr;
944
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300945 ptr = bytes_to_frames(runtime, dpcm->pcm_buf_dma_ofs % dpcm->buffer_bytes);
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200946 snd_printddd("P%d pointer = 0x%04lx\n", substream->number, (unsigned long)ptr);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200947 return ptr;
948}
949
950static void snd_card_asihpi_playback_format(struct snd_card_asihpi *asihpi,
951 u32 h_stream,
952 struct snd_pcm_hardware *pcmhw)
953{
954 struct hpi_format hpi_format;
955 u16 format;
956 u16 err;
957 u32 h_control;
958 u32 sample_rate = 48000;
959
960 /* on cards without SRC, must query at valid rate,
961 * maybe set by external sync
962 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300963 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200964 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
965 HPI_CONTROL_SAMPLECLOCK, &h_control);
966
967 if (!err)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300968 err = hpi_sample_clock_get_sample_rate(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200969 &sample_rate);
970
971 for (format = HPI_FORMAT_PCM8_UNSIGNED;
972 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
973 err = hpi_format_create(&hpi_format,
974 2, format, sample_rate, 128000, 0);
975 if (!err)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300976 err = hpi_outstream_query_format(h_stream,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200977 &hpi_format);
978 if (!err && (hpi_to_alsa_formats[format] != -1))
979 pcmhw->formats |=
980 (1ULL << hpi_to_alsa_formats[format]);
981 }
982}
983
984static struct snd_pcm_hardware snd_card_asihpi_playback = {
985 .channels_min = 1,
986 .channels_max = 2,
987 .buffer_bytes_max = BUFFER_BYTES_MAX,
988 .period_bytes_min = PERIOD_BYTES_MIN,
989 .period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN,
990 .periods_min = PERIODS_MIN,
991 .periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN,
992 .fifo_size = 0,
993};
994
995static int snd_card_asihpi_playback_open(struct snd_pcm_substream *substream)
996{
997 struct snd_pcm_runtime *runtime = substream->runtime;
998 struct snd_card_asihpi_pcm *dpcm;
999 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
1000 int err;
1001
1002 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
1003 if (dpcm == NULL)
1004 return -ENOMEM;
1005
1006 err =
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001007 hpi_outstream_open(card->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001008 substream->number, &dpcm->h_stream);
1009 hpi_handle_error(err);
1010 if (err)
1011 kfree(dpcm);
1012 if (err == HPI_ERROR_OBJ_ALREADY_OPEN)
1013 return -EBUSY;
1014 if (err)
1015 return -EIO;
1016
1017 /*? also check ASI5000 samplerate source
1018 If external, only support external rate.
1019 If internal and other stream playing, cant switch
1020 */
1021
1022 init_timer(&dpcm->timer);
1023 dpcm->timer.data = (unsigned long) dpcm;
1024 dpcm->timer.function = snd_card_asihpi_timer_function;
1025 dpcm->substream = substream;
1026 runtime->private_data = dpcm;
1027 runtime->private_free = snd_card_asihpi_runtime_free;
1028
1029 snd_card_asihpi_playback.channels_max = card->out_max_chans;
1030 /*?snd_card_asihpi_playback.period_bytes_min =
1031 card->out_max_chans * 4096; */
1032
1033 snd_card_asihpi_playback_format(card, dpcm->h_stream,
1034 &snd_card_asihpi_playback);
1035
1036 snd_card_asihpi_pcm_samplerates(card, &snd_card_asihpi_playback);
1037
1038 snd_card_asihpi_playback.info = SNDRV_PCM_INFO_INTERLEAVED |
1039 SNDRV_PCM_INFO_DOUBLE |
1040 SNDRV_PCM_INFO_BATCH |
1041 SNDRV_PCM_INFO_BLOCK_TRANSFER |
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12001042 SNDRV_PCM_INFO_PAUSE |
1043 SNDRV_PCM_INFO_MMAP |
1044 SNDRV_PCM_INFO_MMAP_VALID;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001045
1046 if (card->support_grouping)
1047 snd_card_asihpi_playback.info |= SNDRV_PCM_INFO_SYNC_START;
1048
1049 /* struct is copied, so can create initializer dynamically */
1050 runtime->hw = snd_card_asihpi_playback;
1051
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12001052 if (card->can_dma)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001053 err = snd_pcm_hw_constraint_pow2(runtime, 0,
1054 SNDRV_PCM_HW_PARAM_BUFFER_BYTES);
1055 if (err < 0)
1056 return err;
1057
1058 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1059 card->update_interval_frames);
Eliot Blennerhassett26aebef2011-03-25 15:25:47 +13001060
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001061 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001062 card->update_interval_frames * 2, UINT_MAX);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001063
1064 snd_pcm_set_sync(substream);
1065
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001066 snd_printdd("playback open\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001067
1068 return 0;
1069}
1070
1071static int snd_card_asihpi_playback_close(struct snd_pcm_substream *substream)
1072{
1073 struct snd_pcm_runtime *runtime = substream->runtime;
1074 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1075
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001076 hpi_handle_error(hpi_outstream_close(dpcm->h_stream));
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001077 snd_printdd("playback close\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001078
1079 return 0;
1080}
1081
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001082static struct snd_pcm_ops snd_card_asihpi_playback_mmap_ops = {
1083 .open = snd_card_asihpi_playback_open,
1084 .close = snd_card_asihpi_playback_close,
1085 .ioctl = snd_card_asihpi_playback_ioctl,
1086 .hw_params = snd_card_asihpi_pcm_hw_params,
1087 .hw_free = snd_card_asihpi_hw_free,
1088 .prepare = snd_card_asihpi_playback_prepare,
1089 .trigger = snd_card_asihpi_trigger,
1090 .pointer = snd_card_asihpi_playback_pointer,
1091};
1092
1093/***************************** CAPTURE OPS ****************/
1094static snd_pcm_uframes_t
1095snd_card_asihpi_capture_pointer(struct snd_pcm_substream *substream)
1096{
1097 struct snd_pcm_runtime *runtime = substream->runtime;
1098 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1099
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001100 snd_printddd("capture pointer %d=%d\n",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001101 substream->number, dpcm->pcm_buf_dma_ofs);
1102 /* NOTE Unlike playback can't use actual samples_played
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001103 for the capture position, because those samples aren't yet in
1104 the local buffer available for reading.
1105 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001106 return bytes_to_frames(runtime, dpcm->pcm_buf_dma_ofs % dpcm->buffer_bytes);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001107}
1108
1109static int snd_card_asihpi_capture_ioctl(struct snd_pcm_substream *substream,
1110 unsigned int cmd, void *arg)
1111{
1112 return snd_pcm_lib_ioctl(substream, cmd, arg);
1113}
1114
1115static int snd_card_asihpi_capture_prepare(struct snd_pcm_substream *substream)
1116{
1117 struct snd_pcm_runtime *runtime = substream->runtime;
1118 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1119
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001120 hpi_handle_error(hpi_instream_reset(dpcm->h_stream));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001121 dpcm->pcm_buf_host_rw_ofs = 0;
1122 dpcm->pcm_buf_dma_ofs = 0;
1123 dpcm->pcm_buf_elapsed_dma_ofs = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001124
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001125 snd_printdd("Capture Prepare %d\n", substream->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001126 return 0;
1127}
1128
1129
1130
1131static void snd_card_asihpi_capture_format(struct snd_card_asihpi *asihpi,
1132 u32 h_stream,
1133 struct snd_pcm_hardware *pcmhw)
1134{
1135 struct hpi_format hpi_format;
1136 u16 format;
1137 u16 err;
1138 u32 h_control;
1139 u32 sample_rate = 48000;
1140
1141 /* on cards without SRC, must query at valid rate,
1142 maybe set by external sync */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001143 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001144 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
1145 HPI_CONTROL_SAMPLECLOCK, &h_control);
1146
1147 if (!err)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001148 err = hpi_sample_clock_get_sample_rate(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001149 &sample_rate);
1150
1151 for (format = HPI_FORMAT_PCM8_UNSIGNED;
1152 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
1153
1154 err = hpi_format_create(&hpi_format, 2, format,
1155 sample_rate, 128000, 0);
1156 if (!err)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001157 err = hpi_instream_query_format(h_stream,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001158 &hpi_format);
1159 if (!err)
1160 pcmhw->formats |=
1161 (1ULL << hpi_to_alsa_formats[format]);
1162 }
1163}
1164
1165
1166static struct snd_pcm_hardware snd_card_asihpi_capture = {
1167 .channels_min = 1,
1168 .channels_max = 2,
1169 .buffer_bytes_max = BUFFER_BYTES_MAX,
1170 .period_bytes_min = PERIOD_BYTES_MIN,
1171 .period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN,
1172 .periods_min = PERIODS_MIN,
1173 .periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN,
1174 .fifo_size = 0,
1175};
1176
1177static int snd_card_asihpi_capture_open(struct snd_pcm_substream *substream)
1178{
1179 struct snd_pcm_runtime *runtime = substream->runtime;
1180 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
1181 struct snd_card_asihpi_pcm *dpcm;
1182 int err;
1183
1184 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
1185 if (dpcm == NULL)
1186 return -ENOMEM;
1187
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001188 snd_printdd("capture open adapter %d stream %d\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001189 card->adapter_index, substream->number);
1190
1191 err = hpi_handle_error(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001192 hpi_instream_open(card->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001193 substream->number, &dpcm->h_stream));
1194 if (err)
1195 kfree(dpcm);
1196 if (err == HPI_ERROR_OBJ_ALREADY_OPEN)
1197 return -EBUSY;
1198 if (err)
1199 return -EIO;
1200
1201
1202 init_timer(&dpcm->timer);
1203 dpcm->timer.data = (unsigned long) dpcm;
1204 dpcm->timer.function = snd_card_asihpi_timer_function;
1205 dpcm->substream = substream;
1206 runtime->private_data = dpcm;
1207 runtime->private_free = snd_card_asihpi_runtime_free;
1208
1209 snd_card_asihpi_capture.channels_max = card->in_max_chans;
1210 snd_card_asihpi_capture_format(card, dpcm->h_stream,
1211 &snd_card_asihpi_capture);
1212 snd_card_asihpi_pcm_samplerates(card, &snd_card_asihpi_capture);
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12001213 snd_card_asihpi_capture.info = SNDRV_PCM_INFO_INTERLEAVED |
1214 SNDRV_PCM_INFO_MMAP |
1215 SNDRV_PCM_INFO_MMAP_VALID;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001216
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001217 if (card->support_grouping)
1218 snd_card_asihpi_capture.info |= SNDRV_PCM_INFO_SYNC_START;
1219
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001220 runtime->hw = snd_card_asihpi_capture;
1221
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12001222 if (card->can_dma)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001223 err = snd_pcm_hw_constraint_pow2(runtime, 0,
1224 SNDRV_PCM_HW_PARAM_BUFFER_BYTES);
1225 if (err < 0)
1226 return err;
1227
1228 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1229 card->update_interval_frames);
1230 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1231 card->update_interval_frames * 2, UINT_MAX);
1232
1233 snd_pcm_set_sync(substream);
1234
1235 return 0;
1236}
1237
1238static int snd_card_asihpi_capture_close(struct snd_pcm_substream *substream)
1239{
1240 struct snd_card_asihpi_pcm *dpcm = substream->runtime->private_data;
1241
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001242 hpi_handle_error(hpi_instream_close(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001243 return 0;
1244}
1245
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001246static struct snd_pcm_ops snd_card_asihpi_capture_mmap_ops = {
1247 .open = snd_card_asihpi_capture_open,
1248 .close = snd_card_asihpi_capture_close,
1249 .ioctl = snd_card_asihpi_capture_ioctl,
1250 .hw_params = snd_card_asihpi_pcm_hw_params,
1251 .hw_free = snd_card_asihpi_hw_free,
1252 .prepare = snd_card_asihpi_capture_prepare,
1253 .trigger = snd_card_asihpi_trigger,
1254 .pointer = snd_card_asihpi_capture_pointer,
1255};
1256
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001257static int __devinit snd_card_asihpi_pcm_new(struct snd_card_asihpi *asihpi,
1258 int device, int substreams)
1259{
1260 struct snd_pcm *pcm;
1261 int err;
1262
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001263 err = snd_pcm_new(asihpi->card, "Asihpi PCM", device,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001264 asihpi->num_outstreams, asihpi->num_instreams,
1265 &pcm);
1266 if (err < 0)
1267 return err;
1268 /* pointer to ops struct is stored, dont change ops afterwards! */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001269 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1270 &snd_card_asihpi_playback_mmap_ops);
1271 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1272 &snd_card_asihpi_capture_mmap_ops);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001273
1274 pcm->private_data = asihpi;
1275 pcm->info_flags = 0;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001276 strcpy(pcm->name, "Asihpi PCM");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001277
1278 /*? do we want to emulate MMAP for non-BBM cards?
1279 Jack doesn't work with ALSAs MMAP emulation - WHY NOT? */
1280 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1281 snd_dma_pci_data(asihpi->pci),
1282 64*1024, BUFFER_BYTES_MAX);
1283
1284 return 0;
1285}
1286
1287/***************************** MIXER CONTROLS ****************/
1288struct hpi_control {
1289 u32 h_control;
1290 u16 control_type;
1291 u16 src_node_type;
1292 u16 src_node_index;
1293 u16 dst_node_type;
1294 u16 dst_node_index;
1295 u16 band;
1296 char name[44]; /* copied to snd_ctl_elem_id.name[44]; */
1297};
1298
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001299static const char * const asihpi_tuner_band_names[] = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001300 "invalid",
1301 "AM",
1302 "FM mono",
1303 "TV NTSC-M",
1304 "FM stereo",
1305 "AUX",
1306 "TV PAL BG",
1307 "TV PAL I",
1308 "TV PAL DK",
1309 "TV SECAM",
1310};
1311
1312compile_time_assert(
1313 (ARRAY_SIZE(asihpi_tuner_band_names) ==
1314 (HPI_TUNER_BAND_LAST+1)),
1315 assert_tuner_band_names_size);
1316
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001317static const char * const asihpi_src_names[] = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001318 "no source",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001319 "PCM",
1320 "Line",
1321 "Digital",
1322 "Tuner",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001323 "RF",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001324 "Clock",
1325 "Bitstream",
1326 "Microphone",
1327 "Cobranet",
1328 "Analog",
1329 "Adapter",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001330};
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001331
1332compile_time_assert(
1333 (ARRAY_SIZE(asihpi_src_names) ==
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12001334 (HPI_SOURCENODE_LAST_INDEX-HPI_SOURCENODE_NONE+1)),
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001335 assert_src_names_size);
1336
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001337static const char * const asihpi_dst_names[] = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001338 "no destination",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001339 "PCM",
1340 "Line",
1341 "Digital",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001342 "RF",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001343 "Speaker",
1344 "Cobranet Out",
1345 "Analog"
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001346};
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001347
1348compile_time_assert(
1349 (ARRAY_SIZE(asihpi_dst_names) ==
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12001350 (HPI_DESTNODE_LAST_INDEX-HPI_DESTNODE_NONE+1)),
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001351 assert_dst_names_size);
1352
1353static inline int ctl_add(struct snd_card *card, struct snd_kcontrol_new *ctl,
1354 struct snd_card_asihpi *asihpi)
1355{
1356 int err;
1357
1358 err = snd_ctl_add(card, snd_ctl_new1(ctl, asihpi));
1359 if (err < 0)
1360 return err;
1361 else if (mixer_dump)
1362 snd_printk(KERN_INFO "added %s(%d)\n", ctl->name, ctl->index);
1363
1364 return 0;
1365}
1366
1367/* Convert HPI control name and location into ALSA control name */
1368static void asihpi_ctl_init(struct snd_kcontrol_new *snd_control,
1369 struct hpi_control *hpi_ctl,
1370 char *name)
1371{
Eliot Blennerhassett550ac6b2011-04-05 20:55:41 +12001372 char *dir;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001373 memset(snd_control, 0, sizeof(*snd_control));
1374 snd_control->name = hpi_ctl->name;
1375 snd_control->private_value = hpi_ctl->h_control;
1376 snd_control->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1377 snd_control->index = 0;
1378
Eliot Blennerhassett550ac6b2011-04-05 20:55:41 +12001379 if (hpi_ctl->src_node_type + HPI_SOURCENODE_NONE == HPI_SOURCENODE_CLOCK_SOURCE)
1380 dir = ""; /* clock is neither capture nor playback */
1381 else if (hpi_ctl->dst_node_type + HPI_DESTNODE_NONE == HPI_DESTNODE_ISTREAM)
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001382 dir = "Capture "; /* On or towards a PCM capture destination*/
1383 else if ((hpi_ctl->src_node_type + HPI_SOURCENODE_NONE != HPI_SOURCENODE_OSTREAM) &&
1384 (!hpi_ctl->dst_node_type))
1385 dir = "Capture "; /* On a source node that is not PCM playback */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001386 else if (hpi_ctl->src_node_type &&
1387 (hpi_ctl->src_node_type + HPI_SOURCENODE_NONE != HPI_SOURCENODE_OSTREAM) &&
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001388 (hpi_ctl->dst_node_type))
1389 dir = "Monitor Playback "; /* Between an input and an output */
1390 else
1391 dir = "Playback "; /* PCM Playback source, or output node */
1392
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001393 if (hpi_ctl->src_node_type && hpi_ctl->dst_node_type)
Eliot Blennerhassett550ac6b2011-04-05 20:55:41 +12001394 sprintf(hpi_ctl->name, "%s %d %s %d %s%s",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001395 asihpi_src_names[hpi_ctl->src_node_type],
1396 hpi_ctl->src_node_index,
1397 asihpi_dst_names[hpi_ctl->dst_node_type],
1398 hpi_ctl->dst_node_index,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001399 dir, name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001400 else if (hpi_ctl->dst_node_type) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001401 sprintf(hpi_ctl->name, "%s %d %s%s",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001402 asihpi_dst_names[hpi_ctl->dst_node_type],
1403 hpi_ctl->dst_node_index,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001404 dir, name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001405 } else {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001406 sprintf(hpi_ctl->name, "%s %d %s%s",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001407 asihpi_src_names[hpi_ctl->src_node_type],
1408 hpi_ctl->src_node_index,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001409 dir, name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001410 }
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001411 /* printk(KERN_INFO "Adding %s %d to %d ", hpi_ctl->name,
1412 hpi_ctl->wSrcNodeType, hpi_ctl->wDstNodeType); */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001413}
1414
1415/*------------------------------------------------------------
1416 Volume controls
1417 ------------------------------------------------------------*/
1418#define VOL_STEP_mB 1
1419static int snd_asihpi_volume_info(struct snd_kcontrol *kcontrol,
1420 struct snd_ctl_elem_info *uinfo)
1421{
1422 u32 h_control = kcontrol->private_value;
1423 u16 err;
1424 /* native gains are in millibels */
1425 short min_gain_mB;
1426 short max_gain_mB;
1427 short step_gain_mB;
1428
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001429 err = hpi_volume_query_range(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001430 &min_gain_mB, &max_gain_mB, &step_gain_mB);
1431 if (err) {
1432 max_gain_mB = 0;
1433 min_gain_mB = -10000;
1434 step_gain_mB = VOL_STEP_mB;
1435 }
1436
1437 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1438 uinfo->count = 2;
1439 uinfo->value.integer.min = min_gain_mB / VOL_STEP_mB;
1440 uinfo->value.integer.max = max_gain_mB / VOL_STEP_mB;
1441 uinfo->value.integer.step = step_gain_mB / VOL_STEP_mB;
1442 return 0;
1443}
1444
1445static int snd_asihpi_volume_get(struct snd_kcontrol *kcontrol,
1446 struct snd_ctl_elem_value *ucontrol)
1447{
1448 u32 h_control = kcontrol->private_value;
1449 short an_gain_mB[HPI_MAX_CHANNELS];
1450
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001451 hpi_handle_error(hpi_volume_get_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001452 ucontrol->value.integer.value[0] = an_gain_mB[0] / VOL_STEP_mB;
1453 ucontrol->value.integer.value[1] = an_gain_mB[1] / VOL_STEP_mB;
1454
1455 return 0;
1456}
1457
1458static int snd_asihpi_volume_put(struct snd_kcontrol *kcontrol,
1459 struct snd_ctl_elem_value *ucontrol)
1460{
1461 int change;
1462 u32 h_control = kcontrol->private_value;
1463 short an_gain_mB[HPI_MAX_CHANNELS];
1464
1465 an_gain_mB[0] =
1466 (ucontrol->value.integer.value[0]) * VOL_STEP_mB;
1467 an_gain_mB[1] =
1468 (ucontrol->value.integer.value[1]) * VOL_STEP_mB;
1469 /* change = asihpi->mixer_volume[addr][0] != left ||
1470 asihpi->mixer_volume[addr][1] != right;
1471 */
1472 change = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001473 hpi_handle_error(hpi_volume_set_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001474 return change;
1475}
1476
1477static const DECLARE_TLV_DB_SCALE(db_scale_100, -10000, VOL_STEP_mB, 0);
1478
1479static int __devinit snd_asihpi_volume_add(struct snd_card_asihpi *asihpi,
1480 struct hpi_control *hpi_ctl)
1481{
1482 struct snd_card *card = asihpi->card;
1483 struct snd_kcontrol_new snd_control;
1484
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001485 asihpi_ctl_init(&snd_control, hpi_ctl, "Volume");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001486 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1487 SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1488 snd_control.info = snd_asihpi_volume_info;
1489 snd_control.get = snd_asihpi_volume_get;
1490 snd_control.put = snd_asihpi_volume_put;
1491 snd_control.tlv.p = db_scale_100;
1492
1493 return ctl_add(card, &snd_control, asihpi);
1494}
1495
1496/*------------------------------------------------------------
1497 Level controls
1498 ------------------------------------------------------------*/
1499static int snd_asihpi_level_info(struct snd_kcontrol *kcontrol,
1500 struct snd_ctl_elem_info *uinfo)
1501{
1502 u32 h_control = kcontrol->private_value;
1503 u16 err;
1504 short min_gain_mB;
1505 short max_gain_mB;
1506 short step_gain_mB;
1507
1508 err =
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001509 hpi_level_query_range(h_control, &min_gain_mB,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001510 &max_gain_mB, &step_gain_mB);
1511 if (err) {
1512 max_gain_mB = 2400;
1513 min_gain_mB = -1000;
1514 step_gain_mB = 100;
1515 }
1516
1517 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1518 uinfo->count = 2;
1519 uinfo->value.integer.min = min_gain_mB / HPI_UNITS_PER_dB;
1520 uinfo->value.integer.max = max_gain_mB / HPI_UNITS_PER_dB;
1521 uinfo->value.integer.step = step_gain_mB / HPI_UNITS_PER_dB;
1522 return 0;
1523}
1524
1525static int snd_asihpi_level_get(struct snd_kcontrol *kcontrol,
1526 struct snd_ctl_elem_value *ucontrol)
1527{
1528 u32 h_control = kcontrol->private_value;
1529 short an_gain_mB[HPI_MAX_CHANNELS];
1530
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001531 hpi_handle_error(hpi_level_get_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001532 ucontrol->value.integer.value[0] =
1533 an_gain_mB[0] / HPI_UNITS_PER_dB;
1534 ucontrol->value.integer.value[1] =
1535 an_gain_mB[1] / HPI_UNITS_PER_dB;
1536
1537 return 0;
1538}
1539
1540static int snd_asihpi_level_put(struct snd_kcontrol *kcontrol,
1541 struct snd_ctl_elem_value *ucontrol)
1542{
1543 int change;
1544 u32 h_control = kcontrol->private_value;
1545 short an_gain_mB[HPI_MAX_CHANNELS];
1546
1547 an_gain_mB[0] =
1548 (ucontrol->value.integer.value[0]) * HPI_UNITS_PER_dB;
1549 an_gain_mB[1] =
1550 (ucontrol->value.integer.value[1]) * HPI_UNITS_PER_dB;
1551 /* change = asihpi->mixer_level[addr][0] != left ||
1552 asihpi->mixer_level[addr][1] != right;
1553 */
1554 change = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001555 hpi_handle_error(hpi_level_set_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001556 return change;
1557}
1558
1559static const DECLARE_TLV_DB_SCALE(db_scale_level, -1000, 100, 0);
1560
1561static int __devinit snd_asihpi_level_add(struct snd_card_asihpi *asihpi,
1562 struct hpi_control *hpi_ctl)
1563{
1564 struct snd_card *card = asihpi->card;
1565 struct snd_kcontrol_new snd_control;
1566
1567 /* can't use 'volume' cos some nodes have volume as well */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001568 asihpi_ctl_init(&snd_control, hpi_ctl, "Level");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001569 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1570 SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1571 snd_control.info = snd_asihpi_level_info;
1572 snd_control.get = snd_asihpi_level_get;
1573 snd_control.put = snd_asihpi_level_put;
1574 snd_control.tlv.p = db_scale_level;
1575
1576 return ctl_add(card, &snd_control, asihpi);
1577}
1578
1579/*------------------------------------------------------------
1580 AESEBU controls
1581 ------------------------------------------------------------*/
1582
1583/* AESEBU format */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001584static const char * const asihpi_aesebu_format_names[] = {
1585 "N/A", "S/PDIF", "AES/EBU" };
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001586
1587static int snd_asihpi_aesebu_format_info(struct snd_kcontrol *kcontrol,
1588 struct snd_ctl_elem_info *uinfo)
1589{
1590 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1591 uinfo->count = 1;
1592 uinfo->value.enumerated.items = 3;
1593
1594 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1595 uinfo->value.enumerated.item =
1596 uinfo->value.enumerated.items - 1;
1597
1598 strcpy(uinfo->value.enumerated.name,
1599 asihpi_aesebu_format_names[uinfo->value.enumerated.item]);
1600
1601 return 0;
1602}
1603
1604static int snd_asihpi_aesebu_format_get(struct snd_kcontrol *kcontrol,
1605 struct snd_ctl_elem_value *ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001606 u16 (*func)(u32, u16 *))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001607{
1608 u32 h_control = kcontrol->private_value;
1609 u16 source, err;
1610
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001611 err = func(h_control, &source);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001612
1613 /* default to N/A */
1614 ucontrol->value.enumerated.item[0] = 0;
1615 /* return success but set the control to N/A */
1616 if (err)
1617 return 0;
1618 if (source == HPI_AESEBU_FORMAT_SPDIF)
1619 ucontrol->value.enumerated.item[0] = 1;
1620 if (source == HPI_AESEBU_FORMAT_AESEBU)
1621 ucontrol->value.enumerated.item[0] = 2;
1622
1623 return 0;
1624}
1625
1626static int snd_asihpi_aesebu_format_put(struct snd_kcontrol *kcontrol,
1627 struct snd_ctl_elem_value *ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001628 u16 (*func)(u32, u16))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001629{
1630 u32 h_control = kcontrol->private_value;
1631
1632 /* default to S/PDIF */
1633 u16 source = HPI_AESEBU_FORMAT_SPDIF;
1634
1635 if (ucontrol->value.enumerated.item[0] == 1)
1636 source = HPI_AESEBU_FORMAT_SPDIF;
1637 if (ucontrol->value.enumerated.item[0] == 2)
1638 source = HPI_AESEBU_FORMAT_AESEBU;
1639
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001640 if (func(h_control, source) != 0)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001641 return -EINVAL;
1642
1643 return 1;
1644}
1645
1646static int snd_asihpi_aesebu_rx_format_get(struct snd_kcontrol *kcontrol,
1647 struct snd_ctl_elem_value *ucontrol) {
1648 return snd_asihpi_aesebu_format_get(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001649 hpi_aesebu_receiver_get_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001650}
1651
1652static int snd_asihpi_aesebu_rx_format_put(struct snd_kcontrol *kcontrol,
1653 struct snd_ctl_elem_value *ucontrol) {
1654 return snd_asihpi_aesebu_format_put(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001655 hpi_aesebu_receiver_set_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001656}
1657
1658static int snd_asihpi_aesebu_rxstatus_info(struct snd_kcontrol *kcontrol,
1659 struct snd_ctl_elem_info *uinfo)
1660{
1661 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1662 uinfo->count = 1;
1663
1664 uinfo->value.integer.min = 0;
1665 uinfo->value.integer.max = 0X1F;
1666 uinfo->value.integer.step = 1;
1667
1668 return 0;
1669}
1670
1671static int snd_asihpi_aesebu_rxstatus_get(struct snd_kcontrol *kcontrol,
1672 struct snd_ctl_elem_value *ucontrol) {
1673
1674 u32 h_control = kcontrol->private_value;
1675 u16 status;
1676
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001677 hpi_handle_error(hpi_aesebu_receiver_get_error_status(
1678 h_control, &status));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001679 ucontrol->value.integer.value[0] = status;
1680 return 0;
1681}
1682
1683static int __devinit snd_asihpi_aesebu_rx_add(struct snd_card_asihpi *asihpi,
1684 struct hpi_control *hpi_ctl)
1685{
1686 struct snd_card *card = asihpi->card;
1687 struct snd_kcontrol_new snd_control;
1688
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001689 asihpi_ctl_init(&snd_control, hpi_ctl, "Format");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001690 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1691 snd_control.info = snd_asihpi_aesebu_format_info;
1692 snd_control.get = snd_asihpi_aesebu_rx_format_get;
1693 snd_control.put = snd_asihpi_aesebu_rx_format_put;
1694
1695
1696 if (ctl_add(card, &snd_control, asihpi) < 0)
1697 return -EINVAL;
1698
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001699 asihpi_ctl_init(&snd_control, hpi_ctl, "Status");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001700 snd_control.access =
1701 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
1702 snd_control.info = snd_asihpi_aesebu_rxstatus_info;
1703 snd_control.get = snd_asihpi_aesebu_rxstatus_get;
1704
1705 return ctl_add(card, &snd_control, asihpi);
1706}
1707
1708static int snd_asihpi_aesebu_tx_format_get(struct snd_kcontrol *kcontrol,
1709 struct snd_ctl_elem_value *ucontrol) {
1710 return snd_asihpi_aesebu_format_get(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001711 hpi_aesebu_transmitter_get_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001712}
1713
1714static int snd_asihpi_aesebu_tx_format_put(struct snd_kcontrol *kcontrol,
1715 struct snd_ctl_elem_value *ucontrol) {
1716 return snd_asihpi_aesebu_format_put(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001717 hpi_aesebu_transmitter_set_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001718}
1719
1720
1721static int __devinit snd_asihpi_aesebu_tx_add(struct snd_card_asihpi *asihpi,
1722 struct hpi_control *hpi_ctl)
1723{
1724 struct snd_card *card = asihpi->card;
1725 struct snd_kcontrol_new snd_control;
1726
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001727 asihpi_ctl_init(&snd_control, hpi_ctl, "Format");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001728 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1729 snd_control.info = snd_asihpi_aesebu_format_info;
1730 snd_control.get = snd_asihpi_aesebu_tx_format_get;
1731 snd_control.put = snd_asihpi_aesebu_tx_format_put;
1732
1733 return ctl_add(card, &snd_control, asihpi);
1734}
1735
1736/*------------------------------------------------------------
1737 Tuner controls
1738 ------------------------------------------------------------*/
1739
1740/* Gain */
1741
1742static int snd_asihpi_tuner_gain_info(struct snd_kcontrol *kcontrol,
1743 struct snd_ctl_elem_info *uinfo)
1744{
1745 u32 h_control = kcontrol->private_value;
1746 u16 err;
1747 short idx;
1748 u16 gain_range[3];
1749
1750 for (idx = 0; idx < 3; idx++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001751 err = hpi_tuner_query_gain(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001752 idx, &gain_range[idx]);
1753 if (err != 0)
1754 return err;
1755 }
1756
1757 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1758 uinfo->count = 1;
1759 uinfo->value.integer.min = ((int)gain_range[0]) / HPI_UNITS_PER_dB;
1760 uinfo->value.integer.max = ((int)gain_range[1]) / HPI_UNITS_PER_dB;
1761 uinfo->value.integer.step = ((int) gain_range[2]) / HPI_UNITS_PER_dB;
1762 return 0;
1763}
1764
1765static int snd_asihpi_tuner_gain_get(struct snd_kcontrol *kcontrol,
1766 struct snd_ctl_elem_value *ucontrol)
1767{
1768 /*
1769 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1770 */
1771 u32 h_control = kcontrol->private_value;
1772 short gain;
1773
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001774 hpi_handle_error(hpi_tuner_get_gain(h_control, &gain));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001775 ucontrol->value.integer.value[0] = gain / HPI_UNITS_PER_dB;
1776
1777 return 0;
1778}
1779
1780static int snd_asihpi_tuner_gain_put(struct snd_kcontrol *kcontrol,
1781 struct snd_ctl_elem_value *ucontrol)
1782{
1783 /*
1784 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1785 */
1786 u32 h_control = kcontrol->private_value;
1787 short gain;
1788
1789 gain = (ucontrol->value.integer.value[0]) * HPI_UNITS_PER_dB;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001790 hpi_handle_error(hpi_tuner_set_gain(h_control, gain));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001791
1792 return 1;
1793}
1794
1795/* Band */
1796
1797static int asihpi_tuner_band_query(struct snd_kcontrol *kcontrol,
1798 u16 *band_list, u32 len) {
1799 u32 h_control = kcontrol->private_value;
1800 u16 err = 0;
1801 u32 i;
1802
1803 for (i = 0; i < len; i++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001804 err = hpi_tuner_query_band(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001805 h_control, i, &band_list[i]);
1806 if (err != 0)
1807 break;
1808 }
1809
1810 if (err && (err != HPI_ERROR_INVALID_OBJ_INDEX))
1811 return -EIO;
1812
1813 return i;
1814}
1815
1816static int snd_asihpi_tuner_band_info(struct snd_kcontrol *kcontrol,
1817 struct snd_ctl_elem_info *uinfo)
1818{
1819 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1820 int num_bands = 0;
1821
1822 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1823 HPI_TUNER_BAND_LAST);
1824
1825 if (num_bands < 0)
1826 return num_bands;
1827
1828 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1829 uinfo->count = 1;
1830 uinfo->value.enumerated.items = num_bands;
1831
1832 if (num_bands > 0) {
1833 if (uinfo->value.enumerated.item >=
1834 uinfo->value.enumerated.items)
1835 uinfo->value.enumerated.item =
1836 uinfo->value.enumerated.items - 1;
1837
1838 strcpy(uinfo->value.enumerated.name,
1839 asihpi_tuner_band_names[
1840 tuner_bands[uinfo->value.enumerated.item]]);
1841
1842 }
1843 return 0;
1844}
1845
1846static int snd_asihpi_tuner_band_get(struct snd_kcontrol *kcontrol,
1847 struct snd_ctl_elem_value *ucontrol)
1848{
1849 u32 h_control = kcontrol->private_value;
1850 /*
1851 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1852 */
1853 u16 band, idx;
1854 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1855 u32 num_bands = 0;
1856
1857 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1858 HPI_TUNER_BAND_LAST);
1859
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001860 hpi_handle_error(hpi_tuner_get_band(h_control, &band));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001861
1862 ucontrol->value.enumerated.item[0] = -1;
1863 for (idx = 0; idx < HPI_TUNER_BAND_LAST; idx++)
1864 if (tuner_bands[idx] == band) {
1865 ucontrol->value.enumerated.item[0] = idx;
1866 break;
1867 }
1868
1869 return 0;
1870}
1871
1872static int snd_asihpi_tuner_band_put(struct snd_kcontrol *kcontrol,
1873 struct snd_ctl_elem_value *ucontrol)
1874{
1875 /*
1876 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1877 */
1878 u32 h_control = kcontrol->private_value;
1879 u16 band;
1880 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1881 u32 num_bands = 0;
1882
1883 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1884 HPI_TUNER_BAND_LAST);
1885
1886 band = tuner_bands[ucontrol->value.enumerated.item[0]];
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001887 hpi_handle_error(hpi_tuner_set_band(h_control, band));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001888
1889 return 1;
1890}
1891
1892/* Freq */
1893
1894static int snd_asihpi_tuner_freq_info(struct snd_kcontrol *kcontrol,
1895 struct snd_ctl_elem_info *uinfo)
1896{
1897 u32 h_control = kcontrol->private_value;
1898 u16 err;
1899 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1900 u16 num_bands = 0, band_iter, idx;
1901 u32 freq_range[3], temp_freq_range[3];
1902
1903 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1904 HPI_TUNER_BAND_LAST);
1905
1906 freq_range[0] = INT_MAX;
1907 freq_range[1] = 0;
1908 freq_range[2] = INT_MAX;
1909
1910 for (band_iter = 0; band_iter < num_bands; band_iter++) {
1911 for (idx = 0; idx < 3; idx++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001912 err = hpi_tuner_query_frequency(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001913 idx, tuner_bands[band_iter],
1914 &temp_freq_range[idx]);
1915 if (err != 0)
1916 return err;
1917 }
1918
1919 /* skip band with bogus stepping */
1920 if (temp_freq_range[2] <= 0)
1921 continue;
1922
1923 if (temp_freq_range[0] < freq_range[0])
1924 freq_range[0] = temp_freq_range[0];
1925 if (temp_freq_range[1] > freq_range[1])
1926 freq_range[1] = temp_freq_range[1];
1927 if (temp_freq_range[2] < freq_range[2])
1928 freq_range[2] = temp_freq_range[2];
1929 }
1930
1931 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1932 uinfo->count = 1;
1933 uinfo->value.integer.min = ((int)freq_range[0]);
1934 uinfo->value.integer.max = ((int)freq_range[1]);
1935 uinfo->value.integer.step = ((int)freq_range[2]);
1936 return 0;
1937}
1938
1939static int snd_asihpi_tuner_freq_get(struct snd_kcontrol *kcontrol,
1940 struct snd_ctl_elem_value *ucontrol)
1941{
1942 u32 h_control = kcontrol->private_value;
1943 u32 freq;
1944
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001945 hpi_handle_error(hpi_tuner_get_frequency(h_control, &freq));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001946 ucontrol->value.integer.value[0] = freq;
1947
1948 return 0;
1949}
1950
1951static int snd_asihpi_tuner_freq_put(struct snd_kcontrol *kcontrol,
1952 struct snd_ctl_elem_value *ucontrol)
1953{
1954 u32 h_control = kcontrol->private_value;
1955 u32 freq;
1956
1957 freq = ucontrol->value.integer.value[0];
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001958 hpi_handle_error(hpi_tuner_set_frequency(h_control, freq));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001959
1960 return 1;
1961}
1962
1963/* Tuner control group initializer */
1964static int __devinit snd_asihpi_tuner_add(struct snd_card_asihpi *asihpi,
1965 struct hpi_control *hpi_ctl)
1966{
1967 struct snd_card *card = asihpi->card;
1968 struct snd_kcontrol_new snd_control;
1969
1970 snd_control.private_value = hpi_ctl->h_control;
1971 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1972
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001973 if (!hpi_tuner_get_gain(hpi_ctl->h_control, NULL)) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001974 asihpi_ctl_init(&snd_control, hpi_ctl, "Gain");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001975 snd_control.info = snd_asihpi_tuner_gain_info;
1976 snd_control.get = snd_asihpi_tuner_gain_get;
1977 snd_control.put = snd_asihpi_tuner_gain_put;
1978
1979 if (ctl_add(card, &snd_control, asihpi) < 0)
1980 return -EINVAL;
1981 }
1982
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001983 asihpi_ctl_init(&snd_control, hpi_ctl, "Band");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001984 snd_control.info = snd_asihpi_tuner_band_info;
1985 snd_control.get = snd_asihpi_tuner_band_get;
1986 snd_control.put = snd_asihpi_tuner_band_put;
1987
1988 if (ctl_add(card, &snd_control, asihpi) < 0)
1989 return -EINVAL;
1990
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001991 asihpi_ctl_init(&snd_control, hpi_ctl, "Freq");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001992 snd_control.info = snd_asihpi_tuner_freq_info;
1993 snd_control.get = snd_asihpi_tuner_freq_get;
1994 snd_control.put = snd_asihpi_tuner_freq_put;
1995
1996 return ctl_add(card, &snd_control, asihpi);
1997}
1998
1999/*------------------------------------------------------------
2000 Meter controls
2001 ------------------------------------------------------------*/
2002static int snd_asihpi_meter_info(struct snd_kcontrol *kcontrol,
2003 struct snd_ctl_elem_info *uinfo)
2004{
2005 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2006 uinfo->count = HPI_MAX_CHANNELS;
2007 uinfo->value.integer.min = 0;
2008 uinfo->value.integer.max = 0x7FFFFFFF;
2009 return 0;
2010}
2011
2012/* linear values for 10dB steps */
2013static int log2lin[] = {
2014 0x7FFFFFFF, /* 0dB */
2015 679093956,
2016 214748365,
2017 67909396,
2018 21474837,
2019 6790940,
2020 2147484, /* -60dB */
2021 679094,
2022 214748, /* -80 */
2023 67909,
2024 21475, /* -100 */
2025 6791,
2026 2147,
2027 679,
2028 214,
2029 68,
2030 21,
2031 7,
2032 2
2033};
2034
2035static int snd_asihpi_meter_get(struct snd_kcontrol *kcontrol,
2036 struct snd_ctl_elem_value *ucontrol)
2037{
2038 u32 h_control = kcontrol->private_value;
2039 short an_gain_mB[HPI_MAX_CHANNELS], i;
2040 u16 err;
2041
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002042 err = hpi_meter_get_peak(h_control, an_gain_mB);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002043
2044 for (i = 0; i < HPI_MAX_CHANNELS; i++) {
2045 if (err) {
2046 ucontrol->value.integer.value[i] = 0;
2047 } else if (an_gain_mB[i] >= 0) {
2048 ucontrol->value.integer.value[i] =
2049 an_gain_mB[i] << 16;
2050 } else {
2051 /* -ve is log value in millibels < -60dB,
2052 * convert to (roughly!) linear,
2053 */
2054 ucontrol->value.integer.value[i] =
2055 log2lin[an_gain_mB[i] / -1000];
2056 }
2057 }
2058 return 0;
2059}
2060
2061static int __devinit snd_asihpi_meter_add(struct snd_card_asihpi *asihpi,
2062 struct hpi_control *hpi_ctl, int subidx)
2063{
2064 struct snd_card *card = asihpi->card;
2065 struct snd_kcontrol_new snd_control;
2066
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002067 asihpi_ctl_init(&snd_control, hpi_ctl, "Meter");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002068 snd_control.access =
2069 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
2070 snd_control.info = snd_asihpi_meter_info;
2071 snd_control.get = snd_asihpi_meter_get;
2072
2073 snd_control.index = subidx;
2074
2075 return ctl_add(card, &snd_control, asihpi);
2076}
2077
2078/*------------------------------------------------------------
2079 Multiplexer controls
2080 ------------------------------------------------------------*/
2081static int snd_card_asihpi_mux_count_sources(struct snd_kcontrol *snd_control)
2082{
2083 u32 h_control = snd_control->private_value;
2084 struct hpi_control hpi_ctl;
2085 int s, err;
2086 for (s = 0; s < 32; s++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002087 err = hpi_multiplexer_query_source(h_control, s,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002088 &hpi_ctl.
2089 src_node_type,
2090 &hpi_ctl.
2091 src_node_index);
2092 if (err)
2093 break;
2094 }
2095 return s;
2096}
2097
2098static int snd_asihpi_mux_info(struct snd_kcontrol *kcontrol,
2099 struct snd_ctl_elem_info *uinfo)
2100{
2101 int err;
2102 u16 src_node_type, src_node_index;
2103 u32 h_control = kcontrol->private_value;
2104
2105 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2106 uinfo->count = 1;
2107 uinfo->value.enumerated.items =
2108 snd_card_asihpi_mux_count_sources(kcontrol);
2109
2110 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2111 uinfo->value.enumerated.item =
2112 uinfo->value.enumerated.items - 1;
2113
2114 err =
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002115 hpi_multiplexer_query_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002116 uinfo->value.enumerated.item,
2117 &src_node_type, &src_node_index);
2118
2119 sprintf(uinfo->value.enumerated.name, "%s %d",
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12002120 asihpi_src_names[src_node_type - HPI_SOURCENODE_NONE],
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002121 src_node_index);
2122 return 0;
2123}
2124
2125static int snd_asihpi_mux_get(struct snd_kcontrol *kcontrol,
2126 struct snd_ctl_elem_value *ucontrol)
2127{
2128 u32 h_control = kcontrol->private_value;
2129 u16 source_type, source_index;
2130 u16 src_node_type, src_node_index;
2131 int s;
2132
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002133 hpi_handle_error(hpi_multiplexer_get_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002134 &source_type, &source_index));
2135 /* Should cache this search result! */
2136 for (s = 0; s < 256; s++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002137 if (hpi_multiplexer_query_source(h_control, s,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002138 &src_node_type, &src_node_index))
2139 break;
2140
2141 if ((source_type == src_node_type)
2142 && (source_index == src_node_index)) {
2143 ucontrol->value.enumerated.item[0] = s;
2144 return 0;
2145 }
2146 }
2147 snd_printd(KERN_WARNING
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002148 "Control %x failed to match mux source %hu %hu\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002149 h_control, source_type, source_index);
2150 ucontrol->value.enumerated.item[0] = 0;
2151 return 0;
2152}
2153
2154static int snd_asihpi_mux_put(struct snd_kcontrol *kcontrol,
2155 struct snd_ctl_elem_value *ucontrol)
2156{
2157 int change;
2158 u32 h_control = kcontrol->private_value;
2159 u16 source_type, source_index;
2160 u16 e;
2161
2162 change = 1;
2163
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002164 e = hpi_multiplexer_query_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002165 ucontrol->value.enumerated.item[0],
2166 &source_type, &source_index);
2167 if (!e)
2168 hpi_handle_error(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002169 hpi_multiplexer_set_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002170 source_type, source_index));
2171 return change;
2172}
2173
2174
2175static int __devinit snd_asihpi_mux_add(struct snd_card_asihpi *asihpi,
2176 struct hpi_control *hpi_ctl)
2177{
2178 struct snd_card *card = asihpi->card;
2179 struct snd_kcontrol_new snd_control;
2180
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002181 asihpi_ctl_init(&snd_control, hpi_ctl, "Route");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002182 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2183 snd_control.info = snd_asihpi_mux_info;
2184 snd_control.get = snd_asihpi_mux_get;
2185 snd_control.put = snd_asihpi_mux_put;
2186
2187 return ctl_add(card, &snd_control, asihpi);
2188
2189}
2190
2191/*------------------------------------------------------------
2192 Channel mode controls
2193 ------------------------------------------------------------*/
2194static int snd_asihpi_cmode_info(struct snd_kcontrol *kcontrol,
2195 struct snd_ctl_elem_info *uinfo)
2196{
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002197 static const char * const mode_names[HPI_CHANNEL_MODE_LAST + 1] = {
2198 "invalid",
2199 "Normal", "Swap",
2200 "From Left", "From Right",
2201 "To Left", "To Right"
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002202 };
2203
2204 u32 h_control = kcontrol->private_value;
2205 u16 mode;
2206 int i;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002207 u16 mode_map[6];
2208 int valid_modes = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002209
2210 /* HPI channel mode values can be from 1 to 6
2211 Some adapters only support a contiguous subset
2212 */
2213 for (i = 0; i < HPI_CHANNEL_MODE_LAST; i++)
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002214 if (!hpi_channel_mode_query_mode(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002215 h_control, i, &mode)) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002216 mode_map[valid_modes] = mode;
2217 valid_modes++;
2218 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002219
2220 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2221 uinfo->count = 1;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002222 uinfo->value.enumerated.items = valid_modes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002223
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002224 if (uinfo->value.enumerated.item >= valid_modes)
2225 uinfo->value.enumerated.item = valid_modes - 1;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002226
2227 strcpy(uinfo->value.enumerated.name,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002228 mode_names[mode_map[uinfo->value.enumerated.item]]);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002229
2230 return 0;
2231}
2232
2233static int snd_asihpi_cmode_get(struct snd_kcontrol *kcontrol,
2234 struct snd_ctl_elem_value *ucontrol)
2235{
2236 u32 h_control = kcontrol->private_value;
2237 u16 mode;
2238
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002239 if (hpi_channel_mode_get(h_control, &mode))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002240 mode = 1;
2241
2242 ucontrol->value.enumerated.item[0] = mode - 1;
2243
2244 return 0;
2245}
2246
2247static int snd_asihpi_cmode_put(struct snd_kcontrol *kcontrol,
2248 struct snd_ctl_elem_value *ucontrol)
2249{
2250 int change;
2251 u32 h_control = kcontrol->private_value;
2252
2253 change = 1;
2254
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002255 hpi_handle_error(hpi_channel_mode_set(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002256 ucontrol->value.enumerated.item[0] + 1));
2257 return change;
2258}
2259
2260
2261static int __devinit snd_asihpi_cmode_add(struct snd_card_asihpi *asihpi,
2262 struct hpi_control *hpi_ctl)
2263{
2264 struct snd_card *card = asihpi->card;
2265 struct snd_kcontrol_new snd_control;
2266
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002267 asihpi_ctl_init(&snd_control, hpi_ctl, "Mode");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002268 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2269 snd_control.info = snd_asihpi_cmode_info;
2270 snd_control.get = snd_asihpi_cmode_get;
2271 snd_control.put = snd_asihpi_cmode_put;
2272
2273 return ctl_add(card, &snd_control, asihpi);
2274}
2275
2276/*------------------------------------------------------------
2277 Sampleclock source controls
2278 ------------------------------------------------------------*/
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002279static char *sampleclock_sources[MAX_CLOCKSOURCES] = {
2280 "N/A", "Local PLL", "Digital Sync", "Word External", "Word Header",
2281 "SMPTE", "Digital1", "Auto", "Network", "Invalid",
2282 "Prev Module",
2283 "Digital2", "Digital3", "Digital4", "Digital5",
2284 "Digital6", "Digital7", "Digital8"};
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002285
2286static int snd_asihpi_clksrc_info(struct snd_kcontrol *kcontrol,
2287 struct snd_ctl_elem_info *uinfo)
2288{
2289 struct snd_card_asihpi *asihpi =
2290 (struct snd_card_asihpi *)(kcontrol->private_data);
2291 struct clk_cache *clkcache = &asihpi->cc;
2292 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2293 uinfo->count = 1;
2294 uinfo->value.enumerated.items = clkcache->count;
2295
2296 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2297 uinfo->value.enumerated.item =
2298 uinfo->value.enumerated.items - 1;
2299
2300 strcpy(uinfo->value.enumerated.name,
2301 clkcache->s[uinfo->value.enumerated.item].name);
2302 return 0;
2303}
2304
2305static int snd_asihpi_clksrc_get(struct snd_kcontrol *kcontrol,
2306 struct snd_ctl_elem_value *ucontrol)
2307{
2308 struct snd_card_asihpi *asihpi =
2309 (struct snd_card_asihpi *)(kcontrol->private_data);
2310 struct clk_cache *clkcache = &asihpi->cc;
2311 u32 h_control = kcontrol->private_value;
2312 u16 source, srcindex = 0;
2313 int i;
2314
2315 ucontrol->value.enumerated.item[0] = 0;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002316 if (hpi_sample_clock_get_source(h_control, &source))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002317 source = 0;
2318
2319 if (source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002320 if (hpi_sample_clock_get_source_index(h_control, &srcindex))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002321 srcindex = 0;
2322
2323 for (i = 0; i < clkcache->count; i++)
2324 if ((clkcache->s[i].source == source) &&
2325 (clkcache->s[i].index == srcindex))
2326 break;
2327
2328 ucontrol->value.enumerated.item[0] = i;
2329
2330 return 0;
2331}
2332
2333static int snd_asihpi_clksrc_put(struct snd_kcontrol *kcontrol,
2334 struct snd_ctl_elem_value *ucontrol)
2335{
2336 struct snd_card_asihpi *asihpi =
2337 (struct snd_card_asihpi *)(kcontrol->private_data);
2338 struct clk_cache *clkcache = &asihpi->cc;
2339 int change, item;
2340 u32 h_control = kcontrol->private_value;
2341
2342 change = 1;
2343 item = ucontrol->value.enumerated.item[0];
2344 if (item >= clkcache->count)
2345 item = clkcache->count-1;
2346
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002347 hpi_handle_error(hpi_sample_clock_set_source(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002348 h_control, clkcache->s[item].source));
2349
2350 if (clkcache->s[item].source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002351 hpi_handle_error(hpi_sample_clock_set_source_index(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002352 h_control, clkcache->s[item].index));
2353 return change;
2354}
2355
2356/*------------------------------------------------------------
2357 Clkrate controls
2358 ------------------------------------------------------------*/
2359/* Need to change this to enumerated control with list of rates */
2360static int snd_asihpi_clklocal_info(struct snd_kcontrol *kcontrol,
2361 struct snd_ctl_elem_info *uinfo)
2362{
2363 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2364 uinfo->count = 1;
2365 uinfo->value.integer.min = 8000;
2366 uinfo->value.integer.max = 192000;
2367 uinfo->value.integer.step = 100;
2368
2369 return 0;
2370}
2371
2372static int snd_asihpi_clklocal_get(struct snd_kcontrol *kcontrol,
2373 struct snd_ctl_elem_value *ucontrol)
2374{
2375 u32 h_control = kcontrol->private_value;
2376 u32 rate;
2377 u16 e;
2378
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002379 e = hpi_sample_clock_get_local_rate(h_control, &rate);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002380 if (!e)
2381 ucontrol->value.integer.value[0] = rate;
2382 else
2383 ucontrol->value.integer.value[0] = 0;
2384 return 0;
2385}
2386
2387static int snd_asihpi_clklocal_put(struct snd_kcontrol *kcontrol,
2388 struct snd_ctl_elem_value *ucontrol)
2389{
2390 int change;
2391 u32 h_control = kcontrol->private_value;
2392
2393 /* change = asihpi->mixer_clkrate[addr][0] != left ||
2394 asihpi->mixer_clkrate[addr][1] != right;
2395 */
2396 change = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002397 hpi_handle_error(hpi_sample_clock_set_local_rate(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002398 ucontrol->value.integer.value[0]));
2399 return change;
2400}
2401
2402static int snd_asihpi_clkrate_info(struct snd_kcontrol *kcontrol,
2403 struct snd_ctl_elem_info *uinfo)
2404{
2405 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2406 uinfo->count = 1;
2407 uinfo->value.integer.min = 8000;
2408 uinfo->value.integer.max = 192000;
2409 uinfo->value.integer.step = 100;
2410
2411 return 0;
2412}
2413
2414static int snd_asihpi_clkrate_get(struct snd_kcontrol *kcontrol,
2415 struct snd_ctl_elem_value *ucontrol)
2416{
2417 u32 h_control = kcontrol->private_value;
2418 u32 rate;
2419 u16 e;
2420
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002421 e = hpi_sample_clock_get_sample_rate(h_control, &rate);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002422 if (!e)
2423 ucontrol->value.integer.value[0] = rate;
2424 else
2425 ucontrol->value.integer.value[0] = 0;
2426 return 0;
2427}
2428
2429static int __devinit snd_asihpi_sampleclock_add(struct snd_card_asihpi *asihpi,
2430 struct hpi_control *hpi_ctl)
2431{
2432 struct snd_card *card = asihpi->card;
2433 struct snd_kcontrol_new snd_control;
2434
2435 struct clk_cache *clkcache = &asihpi->cc;
2436 u32 hSC = hpi_ctl->h_control;
2437 int has_aes_in = 0;
2438 int i, j;
2439 u16 source;
2440
2441 snd_control.private_value = hpi_ctl->h_control;
2442
2443 clkcache->has_local = 0;
2444
2445 for (i = 0; i <= HPI_SAMPLECLOCK_SOURCE_LAST; i++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002446 if (hpi_sample_clock_query_source(hSC,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002447 i, &source))
2448 break;
2449 clkcache->s[i].source = source;
2450 clkcache->s[i].index = 0;
2451 clkcache->s[i].name = sampleclock_sources[source];
2452 if (source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
2453 has_aes_in = 1;
2454 if (source == HPI_SAMPLECLOCK_SOURCE_LOCAL)
2455 clkcache->has_local = 1;
2456 }
2457 if (has_aes_in)
2458 /* already will have picked up index 0 above */
2459 for (j = 1; j < 8; j++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002460 if (hpi_sample_clock_query_source_index(hSC,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002461 j, HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT,
2462 &source))
2463 break;
2464 clkcache->s[i].source =
2465 HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT;
2466 clkcache->s[i].index = j;
2467 clkcache->s[i].name = sampleclock_sources[
2468 j+HPI_SAMPLECLOCK_SOURCE_LAST];
2469 i++;
2470 }
2471 clkcache->count = i;
2472
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002473 asihpi_ctl_init(&snd_control, hpi_ctl, "Source");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002474 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE ;
2475 snd_control.info = snd_asihpi_clksrc_info;
2476 snd_control.get = snd_asihpi_clksrc_get;
2477 snd_control.put = snd_asihpi_clksrc_put;
2478 if (ctl_add(card, &snd_control, asihpi) < 0)
2479 return -EINVAL;
2480
2481
2482 if (clkcache->has_local) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002483 asihpi_ctl_init(&snd_control, hpi_ctl, "Localrate");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002484 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE ;
2485 snd_control.info = snd_asihpi_clklocal_info;
2486 snd_control.get = snd_asihpi_clklocal_get;
2487 snd_control.put = snd_asihpi_clklocal_put;
2488
2489
2490 if (ctl_add(card, &snd_control, asihpi) < 0)
2491 return -EINVAL;
2492 }
2493
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002494 asihpi_ctl_init(&snd_control, hpi_ctl, "Rate");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002495 snd_control.access =
2496 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
2497 snd_control.info = snd_asihpi_clkrate_info;
2498 snd_control.get = snd_asihpi_clkrate_get;
2499
2500 return ctl_add(card, &snd_control, asihpi);
2501}
2502/*------------------------------------------------------------
2503 Mixer
2504 ------------------------------------------------------------*/
2505
2506static int __devinit snd_card_asihpi_mixer_new(struct snd_card_asihpi *asihpi)
2507{
2508 struct snd_card *card = asihpi->card;
2509 unsigned int idx = 0;
2510 unsigned int subindex = 0;
2511 int err;
2512 struct hpi_control hpi_ctl, prev_ctl;
2513
2514 if (snd_BUG_ON(!asihpi))
2515 return -EINVAL;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002516 strcpy(card->mixername, "Asihpi Mixer");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002517
2518 err =
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002519 hpi_mixer_open(asihpi->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002520 &asihpi->h_mixer);
2521 hpi_handle_error(err);
2522 if (err)
2523 return -err;
2524
Takashi Iwai21896bc2010-06-02 12:08:37 +02002525 memset(&prev_ctl, 0, sizeof(prev_ctl));
2526 prev_ctl.control_type = -1;
2527
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002528 for (idx = 0; idx < 2000; idx++) {
2529 err = hpi_mixer_get_control_by_index(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002530 asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002531 idx,
2532 &hpi_ctl.src_node_type,
2533 &hpi_ctl.src_node_index,
2534 &hpi_ctl.dst_node_type,
2535 &hpi_ctl.dst_node_index,
2536 &hpi_ctl.control_type,
2537 &hpi_ctl.h_control);
2538 if (err) {
2539 if (err == HPI_ERROR_CONTROL_DISABLED) {
2540 if (mixer_dump)
2541 snd_printk(KERN_INFO
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002542 "Disabled HPI Control(%d)\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002543 idx);
2544 continue;
2545 } else
2546 break;
2547
2548 }
2549
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12002550 hpi_ctl.src_node_type -= HPI_SOURCENODE_NONE;
2551 hpi_ctl.dst_node_type -= HPI_DESTNODE_NONE;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002552
2553 /* ASI50xx in SSX mode has multiple meters on the same node.
2554 Use subindex to create distinct ALSA controls
2555 for any duplicated controls.
2556 */
2557 if ((hpi_ctl.control_type == prev_ctl.control_type) &&
2558 (hpi_ctl.src_node_type == prev_ctl.src_node_type) &&
2559 (hpi_ctl.src_node_index == prev_ctl.src_node_index) &&
2560 (hpi_ctl.dst_node_type == prev_ctl.dst_node_type) &&
2561 (hpi_ctl.dst_node_index == prev_ctl.dst_node_index))
2562 subindex++;
2563 else
2564 subindex = 0;
2565
2566 prev_ctl = hpi_ctl;
2567
2568 switch (hpi_ctl.control_type) {
2569 case HPI_CONTROL_VOLUME:
2570 err = snd_asihpi_volume_add(asihpi, &hpi_ctl);
2571 break;
2572 case HPI_CONTROL_LEVEL:
2573 err = snd_asihpi_level_add(asihpi, &hpi_ctl);
2574 break;
2575 case HPI_CONTROL_MULTIPLEXER:
2576 err = snd_asihpi_mux_add(asihpi, &hpi_ctl);
2577 break;
2578 case HPI_CONTROL_CHANNEL_MODE:
2579 err = snd_asihpi_cmode_add(asihpi, &hpi_ctl);
2580 break;
2581 case HPI_CONTROL_METER:
2582 err = snd_asihpi_meter_add(asihpi, &hpi_ctl, subindex);
2583 break;
2584 case HPI_CONTROL_SAMPLECLOCK:
2585 err = snd_asihpi_sampleclock_add(
2586 asihpi, &hpi_ctl);
2587 break;
2588 case HPI_CONTROL_CONNECTION: /* ignore these */
2589 continue;
2590 case HPI_CONTROL_TUNER:
2591 err = snd_asihpi_tuner_add(asihpi, &hpi_ctl);
2592 break;
2593 case HPI_CONTROL_AESEBU_TRANSMITTER:
2594 err = snd_asihpi_aesebu_tx_add(asihpi, &hpi_ctl);
2595 break;
2596 case HPI_CONTROL_AESEBU_RECEIVER:
2597 err = snd_asihpi_aesebu_rx_add(asihpi, &hpi_ctl);
2598 break;
2599 case HPI_CONTROL_VOX:
2600 case HPI_CONTROL_BITSTREAM:
2601 case HPI_CONTROL_MICROPHONE:
2602 case HPI_CONTROL_PARAMETRIC_EQ:
2603 case HPI_CONTROL_COMPANDER:
2604 default:
2605 if (mixer_dump)
2606 snd_printk(KERN_INFO
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002607 "Untranslated HPI Control"
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002608 "(%d) %d %d %d %d %d\n",
2609 idx,
2610 hpi_ctl.control_type,
2611 hpi_ctl.src_node_type,
2612 hpi_ctl.src_node_index,
2613 hpi_ctl.dst_node_type,
2614 hpi_ctl.dst_node_index);
2615 continue;
2616 };
2617 if (err < 0)
2618 return err;
2619 }
2620 if (HPI_ERROR_INVALID_OBJ_INDEX != err)
2621 hpi_handle_error(err);
2622
2623 snd_printk(KERN_INFO "%d mixer controls found\n", idx);
2624
2625 return 0;
2626}
2627
2628/*------------------------------------------------------------
2629 /proc interface
2630 ------------------------------------------------------------*/
2631
2632static void
2633snd_asihpi_proc_read(struct snd_info_entry *entry,
2634 struct snd_info_buffer *buffer)
2635{
2636 struct snd_card_asihpi *asihpi = entry->private_data;
2637 u16 version;
2638 u32 h_control;
2639 u32 rate = 0;
2640 u16 source = 0;
2641 int err;
2642
2643 snd_iprintf(buffer, "ASIHPI driver proc file\n");
2644 snd_iprintf(buffer,
2645 "adapter ID=%4X\n_index=%d\n"
2646 "num_outstreams=%d\n_num_instreams=%d\n",
2647 asihpi->type, asihpi->adapter_index,
2648 asihpi->num_outstreams, asihpi->num_instreams);
2649
2650 version = asihpi->version;
2651 snd_iprintf(buffer,
2652 "serial#=%d\n_hw version %c%d\nDSP code version %03d\n",
2653 asihpi->serial_number, ((version >> 3) & 0xf) + 'A',
2654 version & 0x7,
2655 ((version >> 13) * 100) + ((version >> 7) & 0x3f));
2656
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002657 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002658 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
2659 HPI_CONTROL_SAMPLECLOCK, &h_control);
2660
2661 if (!err) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002662 err = hpi_sample_clock_get_sample_rate(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002663 h_control, &rate);
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002664 err += hpi_sample_clock_get_source(h_control, &source);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002665
2666 if (!err)
2667 snd_iprintf(buffer, "sample_clock=%d_hz, source %s\n",
2668 rate, sampleclock_sources[source]);
2669 }
2670
2671}
2672
2673
2674static void __devinit snd_asihpi_proc_init(struct snd_card_asihpi *asihpi)
2675{
2676 struct snd_info_entry *entry;
2677
2678 if (!snd_card_proc_new(asihpi->card, "info", &entry))
2679 snd_info_set_text_ops(entry, asihpi, snd_asihpi_proc_read);
2680}
2681
2682/*------------------------------------------------------------
2683 HWDEP
2684 ------------------------------------------------------------*/
2685
2686static int snd_asihpi_hpi_open(struct snd_hwdep *hw, struct file *file)
2687{
2688 if (enable_hpi_hwdep)
2689 return 0;
2690 else
2691 return -ENODEV;
2692
2693}
2694
2695static int snd_asihpi_hpi_release(struct snd_hwdep *hw, struct file *file)
2696{
2697 if (enable_hpi_hwdep)
2698 return asihpi_hpi_release(file);
2699 else
2700 return -ENODEV;
2701}
2702
2703static int snd_asihpi_hpi_ioctl(struct snd_hwdep *hw, struct file *file,
2704 unsigned int cmd, unsigned long arg)
2705{
2706 if (enable_hpi_hwdep)
2707 return asihpi_hpi_ioctl(file, cmd, arg);
2708 else
2709 return -ENODEV;
2710}
2711
2712
2713/* results in /dev/snd/hwC#D0 file for each card with index #
2714 also /proc/asound/hwdep will contain '#-00: asihpi (HPI) for each card'
2715*/
2716static int __devinit snd_asihpi_hpi_new(struct snd_card_asihpi *asihpi,
2717 int device, struct snd_hwdep **rhwdep)
2718{
2719 struct snd_hwdep *hw;
2720 int err;
2721
2722 if (rhwdep)
2723 *rhwdep = NULL;
2724 err = snd_hwdep_new(asihpi->card, "HPI", device, &hw);
2725 if (err < 0)
2726 return err;
2727 strcpy(hw->name, "asihpi (HPI)");
2728 hw->iface = SNDRV_HWDEP_IFACE_LAST;
2729 hw->ops.open = snd_asihpi_hpi_open;
2730 hw->ops.ioctl = snd_asihpi_hpi_ioctl;
2731 hw->ops.release = snd_asihpi_hpi_release;
2732 hw->private_data = asihpi;
2733 if (rhwdep)
2734 *rhwdep = hw;
2735 return 0;
2736}
2737
2738/*------------------------------------------------------------
2739 CARD
2740 ------------------------------------------------------------*/
2741static int __devinit snd_asihpi_probe(struct pci_dev *pci_dev,
2742 const struct pci_device_id *pci_id)
2743{
2744 int err;
2745
2746 u16 version;
2747 int pcm_substreams;
2748
2749 struct hpi_adapter *hpi_card;
2750 struct snd_card *card;
2751 struct snd_card_asihpi *asihpi;
2752
2753 u32 h_control;
2754 u32 h_stream;
2755
2756 static int dev;
2757 if (dev >= SNDRV_CARDS)
2758 return -ENODEV;
2759
2760 /* Should this be enable[hpi_card->index] ? */
2761 if (!enable[dev]) {
2762 dev++;
2763 return -ENOENT;
2764 }
2765
2766 err = asihpi_adapter_probe(pci_dev, pci_id);
2767 if (err < 0)
2768 return err;
2769
2770 hpi_card = pci_get_drvdata(pci_dev);
2771 /* first try to give the card the same index as its hardware index */
2772 err = snd_card_create(hpi_card->index,
2773 id[hpi_card->index], THIS_MODULE,
2774 sizeof(struct snd_card_asihpi),
2775 &card);
2776 if (err < 0) {
2777 /* if that fails, try the default index==next available */
2778 err =
2779 snd_card_create(index[dev], id[dev],
2780 THIS_MODULE,
2781 sizeof(struct snd_card_asihpi),
2782 &card);
2783 if (err < 0)
2784 return err;
2785 snd_printk(KERN_WARNING
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002786 "**** WARNING **** Adapter index %d->ALSA index %d\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002787 hpi_card->index, card->number);
2788 }
2789
Eliot Blennerhassett12253672011-02-10 17:26:10 +13002790 snd_card_set_dev(card, &pci_dev->dev);
2791
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002792 asihpi = (struct snd_card_asihpi *) card->private_data;
2793 asihpi->card = card;
Eliot Blennerhassett12253672011-02-10 17:26:10 +13002794 asihpi->pci = pci_dev;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002795 asihpi->adapter_index = hpi_card->index;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002796 hpi_handle_error(hpi_adapter_get_info(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002797 asihpi->adapter_index,
2798 &asihpi->num_outstreams,
2799 &asihpi->num_instreams,
2800 &asihpi->version,
2801 &asihpi->serial_number, &asihpi->type));
2802
2803 version = asihpi->version;
2804 snd_printk(KERN_INFO "adapter ID=%4X index=%d num_outstreams=%d "
2805 "num_instreams=%d S/N=%d\n"
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002806 "Hw Version %c%d DSP code version %03d\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002807 asihpi->type, asihpi->adapter_index,
2808 asihpi->num_outstreams,
2809 asihpi->num_instreams, asihpi->serial_number,
2810 ((version >> 3) & 0xf) + 'A',
2811 version & 0x7,
2812 ((version >> 13) * 100) + ((version >> 7) & 0x3f));
2813
2814 pcm_substreams = asihpi->num_outstreams;
2815 if (pcm_substreams < asihpi->num_instreams)
2816 pcm_substreams = asihpi->num_instreams;
2817
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002818 err = hpi_adapter_get_property(asihpi->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002819 HPI_ADAPTER_PROPERTY_CAPS1,
2820 NULL, &asihpi->support_grouping);
2821 if (err)
2822 asihpi->support_grouping = 0;
2823
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002824 err = hpi_adapter_get_property(asihpi->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002825 HPI_ADAPTER_PROPERTY_CAPS2,
2826 &asihpi->support_mrx, NULL);
2827 if (err)
2828 asihpi->support_mrx = 0;
2829
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002830 err = hpi_adapter_get_property(asihpi->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002831 HPI_ADAPTER_PROPERTY_INTERVAL,
2832 NULL, &asihpi->update_interval_frames);
2833 if (err)
2834 asihpi->update_interval_frames = 512;
2835
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12002836 if (!asihpi->can_dma)
Eliot Blennerhassett26aebef2011-03-25 15:25:47 +13002837 asihpi->update_interval_frames *= 2;
2838
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002839 hpi_handle_error(hpi_instream_open(asihpi->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002840 0, &h_stream));
2841
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002842 err = hpi_instream_host_buffer_free(h_stream);
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12002843 asihpi->can_dma = (!err);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002844
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002845 hpi_handle_error(hpi_instream_close(h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002846
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002847 err = hpi_adapter_get_property(asihpi->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002848 HPI_ADAPTER_PROPERTY_CURCHANNELS,
2849 &asihpi->in_max_chans, &asihpi->out_max_chans);
2850 if (err) {
2851 asihpi->in_max_chans = 2;
2852 asihpi->out_max_chans = 2;
2853 }
2854
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12002855 snd_printk(KERN_INFO "has dma:%d, grouping:%d, mrx:%d\n",
2856 asihpi->can_dma,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002857 asihpi->support_grouping,
2858 asihpi->support_mrx
2859 );
2860
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002861 err = snd_card_asihpi_pcm_new(asihpi, 0, pcm_substreams);
2862 if (err < 0) {
2863 snd_printk(KERN_ERR "pcm_new failed\n");
2864 goto __nodev;
2865 }
2866 err = snd_card_asihpi_mixer_new(asihpi);
2867 if (err < 0) {
2868 snd_printk(KERN_ERR "mixer_new failed\n");
2869 goto __nodev;
2870 }
2871
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002872 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002873 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
2874 HPI_CONTROL_SAMPLECLOCK, &h_control);
2875
2876 if (!err)
2877 err = hpi_sample_clock_set_local_rate(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002878 h_control, adapter_fs);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002879
2880 snd_asihpi_proc_init(asihpi);
2881
2882 /* always create, can be enabled or disabled dynamically
2883 by enable_hwdep module param*/
2884 snd_asihpi_hpi_new(asihpi, 0, NULL);
2885
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12002886 strcpy(card->driver, "ASIHPI");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002887
2888 sprintf(card->shortname, "AudioScience ASI%4X", asihpi->type);
2889 sprintf(card->longname, "%s %i",
2890 card->shortname, asihpi->adapter_index);
2891 err = snd_card_register(card);
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13002892
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002893 if (!err) {
2894 hpi_card->snd_card_asihpi = card;
2895 dev++;
2896 return 0;
2897 }
2898__nodev:
2899 snd_card_free(card);
2900 snd_printk(KERN_ERR "snd_asihpi_probe error %d\n", err);
2901 return err;
2902
2903}
2904
2905static void __devexit snd_asihpi_remove(struct pci_dev *pci_dev)
2906{
2907 struct hpi_adapter *hpi_card = pci_get_drvdata(pci_dev);
2908
2909 snd_card_free(hpi_card->snd_card_asihpi);
2910 hpi_card->snd_card_asihpi = NULL;
2911 asihpi_adapter_remove(pci_dev);
2912}
2913
2914static DEFINE_PCI_DEVICE_TABLE(asihpi_pci_tbl) = {
2915 {HPI_PCI_VENDOR_ID_TI, HPI_PCI_DEV_ID_DSP6205,
2916 HPI_PCI_VENDOR_ID_AUDIOSCIENCE, PCI_ANY_ID, 0, 0,
2917 (kernel_ulong_t)HPI_6205},
2918 {HPI_PCI_VENDOR_ID_TI, HPI_PCI_DEV_ID_PCI2040,
2919 HPI_PCI_VENDOR_ID_AUDIOSCIENCE, PCI_ANY_ID, 0, 0,
2920 (kernel_ulong_t)HPI_6000},
2921 {0,}
2922};
2923MODULE_DEVICE_TABLE(pci, asihpi_pci_tbl);
2924
2925static struct pci_driver driver = {
2926 .name = "asihpi",
2927 .id_table = asihpi_pci_tbl,
2928 .probe = snd_asihpi_probe,
2929 .remove = __devexit_p(snd_asihpi_remove),
2930#ifdef CONFIG_PM
2931/* .suspend = snd_asihpi_suspend,
2932 .resume = snd_asihpi_resume, */
2933#endif
2934};
2935
2936static int __init snd_asihpi_init(void)
2937{
2938 asihpi_init();
2939 return pci_register_driver(&driver);
2940}
2941
2942static void __exit snd_asihpi_exit(void)
2943{
2944
2945 pci_unregister_driver(&driver);
2946 asihpi_exit();
2947}
2948
2949module_init(snd_asihpi_init)
2950module_exit(snd_asihpi_exit)
2951