blob: a5226e3af3d7964f12dbdc360c7f45b61168ae83 [file] [log] [blame]
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001/*
2 * Asihpi soundcard
3 * Copyright (c) by AudioScience Inc <alsa@audioscience.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 *
19 * The following is not a condition of use, merely a request:
20 * If you modify this program, particularly if you fix errors, AudioScience Inc
21 * would appreciate it if you grant us the right to use those modifications
22 * for any purpose including commercial applications.
23 */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +130024
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020025#include "hpi_internal.h"
26#include "hpimsginit.h"
27#include "hpioctl.h"
28
29#include <linux/pci.h>
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +130030#include <linux/version.h>
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020031#include <linux/init.h>
32#include <linux/jiffies.h>
33#include <linux/slab.h>
34#include <linux/time.h>
35#include <linux/wait.h>
36#include <sound/core.h>
37#include <sound/control.h>
38#include <sound/pcm.h>
39#include <sound/pcm_params.h>
40#include <sound/info.h>
41#include <sound/initval.h>
42#include <sound/tlv.h>
43#include <sound/hwdep.h>
44
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020045MODULE_LICENSE("GPL");
46MODULE_AUTHOR("AudioScience inc. <support@audioscience.com>");
47MODULE_DESCRIPTION("AudioScience ALSA ASI5000 ASI6000 ASI87xx ASI89xx");
48
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +130049#if defined CONFIG_SND_DEBUG_VERBOSE
50/**
51 * snd_printddd - very verbose debug printk
52 * @format: format string
53 *
54 * Works like snd_printk() for debugging purposes.
55 * Ignored when CONFIG_SND_DEBUG_VERBOSE is not set.
56 * Must set snd module debug parameter to 3 to enable at runtime.
57 */
58#define snd_printddd(format, args...) \
59 __snd_printk(3, __FILE__, __LINE__, format, ##args)
Eliot Blennerhassetta6477132011-04-05 20:55:42 +120060
61/* copied from pcm_lib.c, hope later patch will make that version public
62and this copy can be removed */
63static void pcm_debug_name(struct snd_pcm_substream *substream,
64 char *name, size_t len)
65{
66 snprintf(name, len, "pcmC%dD%d%c:%d",
67 substream->pcm->card->number,
68 substream->pcm->device,
69 substream->stream ? 'c' : 'p',
70 substream->number);
71}
72#define DEBUG_NAME(substream, name) char name[16]; pcm_debug_name(substream, name, sizeof(name))
73
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +130074#else
Eliot Blennerhassetta6477132011-04-05 20:55:42 +120075#define snd_printddd(format, args...) do { } while (0)
76#define pcm_debug_name(s, n, l) do { } while (0)
77#define DEBUG_NAME(name, substream) do { } while (0)
78
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +130079#endif
80
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020081static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* index 0-MAX */
82static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
83static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
84static int enable_hpi_hwdep = 1;
85
86module_param_array(index, int, NULL, S_IRUGO);
87MODULE_PARM_DESC(index, "ALSA index value for AudioScience soundcard.");
88
89module_param_array(id, charp, NULL, S_IRUGO);
90MODULE_PARM_DESC(id, "ALSA ID string for AudioScience soundcard.");
91
92module_param_array(enable, bool, NULL, S_IRUGO);
93MODULE_PARM_DESC(enable, "ALSA enable AudioScience soundcard.");
94
95module_param(enable_hpi_hwdep, bool, S_IRUGO|S_IWUSR);
96MODULE_PARM_DESC(enable_hpi_hwdep,
97 "ALSA enable HPI hwdep for AudioScience soundcard ");
98
99/* identify driver */
100#ifdef KERNEL_ALSA_BUILD
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300101static char *build_info = "Built using headers from kernel source";
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200102module_param(build_info, charp, S_IRUGO);
103MODULE_PARM_DESC(build_info, "built using headers from kernel source");
104#else
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300105static char *build_info = "Built within ALSA source";
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200106module_param(build_info, charp, S_IRUGO);
107MODULE_PARM_DESC(build_info, "built within ALSA source");
108#endif
109
110/* set to 1 to dump every control from adapter to log */
111static const int mixer_dump;
112
113#define DEFAULT_SAMPLERATE 44100
114static int adapter_fs = DEFAULT_SAMPLERATE;
115
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200116/* defaults */
117#define PERIODS_MIN 2
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300118#define PERIOD_BYTES_MIN 2048
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200119#define BUFFER_BYTES_MAX (512 * 1024)
120
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200121#define MAX_CLOCKSOURCES (HPI_SAMPLECLOCK_SOURCE_LAST + 1 + 7)
122
123struct clk_source {
124 int source;
125 int index;
126 char *name;
127};
128
129struct clk_cache {
130 int count;
131 int has_local;
132 struct clk_source s[MAX_CLOCKSOURCES];
133};
134
135/* Per card data */
136struct snd_card_asihpi {
137 struct snd_card *card;
138 struct pci_dev *pci;
139 u16 adapter_index;
140 u32 serial_number;
141 u16 type;
142 u16 version;
143 u16 num_outstreams;
144 u16 num_instreams;
145
146 u32 h_mixer;
147 struct clk_cache cc;
148
149 u16 support_mmap;
150 u16 support_grouping;
151 u16 support_mrx;
152 u16 update_interval_frames;
153 u16 in_max_chans;
154 u16 out_max_chans;
155};
156
157/* Per stream data */
158struct snd_card_asihpi_pcm {
159 struct timer_list timer;
160 unsigned int respawn_timer;
161 unsigned int hpi_buffer_attached;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300162 unsigned int buffer_bytes;
163 unsigned int period_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200164 unsigned int bytes_per_sec;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300165 unsigned int pcm_buf_host_rw_ofs; /* Host R/W pos */
166 unsigned int pcm_buf_dma_ofs; /* DMA R/W offset in buffer */
167 unsigned int pcm_buf_elapsed_dma_ofs; /* DMA R/W offset in buffer */
Eliot Blennerhassett0b7ce9e2011-04-05 20:55:43 +1200168 unsigned int drained_count;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200169 struct snd_pcm_substream *substream;
170 u32 h_stream;
171 struct hpi_format format;
172};
173
174/* universal stream verbs work with out or in stream handles */
175
176/* Functions to allow driver to give a buffer to HPI for busmastering */
177
178static u16 hpi_stream_host_buffer_attach(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200179 u32 h_stream, /* handle to outstream. */
180 u32 size_in_bytes, /* size in bytes of bus mastering buffer */
181 u32 pci_address
182)
183{
184 struct hpi_message hm;
185 struct hpi_response hr;
186 unsigned int obj = hpi_handle_object(h_stream);
187
188 if (!h_stream)
189 return HPI_ERROR_INVALID_OBJ;
190 hpi_init_message_response(&hm, &hr, obj,
191 obj == HPI_OBJ_OSTREAM ?
192 HPI_OSTREAM_HOSTBUFFER_ALLOC :
193 HPI_ISTREAM_HOSTBUFFER_ALLOC);
194
195 hpi_handle_to_indexes(h_stream, &hm.adapter_index,
196 &hm.obj_index);
197
198 hm.u.d.u.buffer.buffer_size = size_in_bytes;
199 hm.u.d.u.buffer.pci_address = pci_address;
200 hm.u.d.u.buffer.command = HPI_BUFFER_CMD_INTERNAL_GRANTADAPTER;
201 hpi_send_recv(&hm, &hr);
202 return hr.error;
203}
204
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300205static u16 hpi_stream_host_buffer_detach(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200206{
207 struct hpi_message hm;
208 struct hpi_response hr;
209 unsigned int obj = hpi_handle_object(h_stream);
210
211 if (!h_stream)
212 return HPI_ERROR_INVALID_OBJ;
213
214 hpi_init_message_response(&hm, &hr, obj,
215 obj == HPI_OBJ_OSTREAM ?
216 HPI_OSTREAM_HOSTBUFFER_FREE :
217 HPI_ISTREAM_HOSTBUFFER_FREE);
218
219 hpi_handle_to_indexes(h_stream, &hm.adapter_index,
220 &hm.obj_index);
221 hm.u.d.u.buffer.command = HPI_BUFFER_CMD_INTERNAL_REVOKEADAPTER;
222 hpi_send_recv(&hm, &hr);
223 return hr.error;
224}
225
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300226static inline u16 hpi_stream_start(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200227{
228 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300229 return hpi_outstream_start(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200230 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300231 return hpi_instream_start(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200232}
233
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300234static inline u16 hpi_stream_stop(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200235{
236 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300237 return hpi_outstream_stop(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200238 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300239 return hpi_instream_stop(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200240}
241
242static inline u16 hpi_stream_get_info_ex(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200243 u32 h_stream,
244 u16 *pw_state,
245 u32 *pbuffer_size,
246 u32 *pdata_in_buffer,
247 u32 *psample_count,
248 u32 *pauxiliary_data
249)
250{
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300251 u16 e;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200252 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300253 e = hpi_outstream_get_info_ex(h_stream, pw_state,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200254 pbuffer_size, pdata_in_buffer,
255 psample_count, pauxiliary_data);
256 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300257 e = hpi_instream_get_info_ex(h_stream, pw_state,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200258 pbuffer_size, pdata_in_buffer,
259 psample_count, pauxiliary_data);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300260 return e;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200261}
262
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300263static inline u16 hpi_stream_group_add(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200264 u32 h_master,
265 u32 h_stream)
266{
267 if (hpi_handle_object(h_master) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300268 return hpi_outstream_group_add(h_master, h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200269 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300270 return hpi_instream_group_add(h_master, h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200271}
272
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300273static inline u16 hpi_stream_group_reset(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200274{
275 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300276 return hpi_outstream_group_reset(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200277 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300278 return hpi_instream_group_reset(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200279}
280
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300281static inline u16 hpi_stream_group_get_map(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200282 u32 h_stream, u32 *mo, u32 *mi)
283{
284 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300285 return hpi_outstream_group_get_map(h_stream, mo, mi);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200286 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300287 return hpi_instream_group_get_map(h_stream, mo, mi);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200288}
289
290static u16 handle_error(u16 err, int line, char *filename)
291{
292 if (err)
293 printk(KERN_WARNING
294 "in file %s, line %d: HPI error %d\n",
295 filename, line, err);
296 return err;
297}
298
299#define hpi_handle_error(x) handle_error(x, __LINE__, __FILE__)
300
301/***************************** GENERAL PCM ****************/
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200302
303static void print_hwparams(struct snd_pcm_substream *substream,
304 struct snd_pcm_hw_params *p)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200305{
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200306 DEBUG_NAME(substream, name);
307 snd_printd("%s HWPARAMS\n", name);
308 snd_printd(" samplerate %d Hz\n", params_rate(p));
309 snd_printd(" channels %d\n", params_channels(p));
310 snd_printd(" format %d\n", params_format(p));
311 snd_printd(" subformat %d\n", params_subformat(p));
312 snd_printd(" buffer %d B\n", params_buffer_bytes(p));
313 snd_printd(" period %d B\n", params_period_bytes(p));
314 snd_printd(" access %d\n", params_access(p));
315 snd_printd(" period_size %d\n", params_period_size(p));
316 snd_printd(" periods %d\n", params_periods(p));
317 snd_printd(" buffer_size %d\n", params_buffer_size(p));
318 snd_printd(" %d B/s\n", params_rate(p) *
319 params_channels(p) *
320 snd_pcm_format_width(params_format(p)) / 8);
321
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200322}
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200323
324static snd_pcm_format_t hpi_to_alsa_formats[] = {
325 -1, /* INVALID */
326 SNDRV_PCM_FORMAT_U8, /* HPI_FORMAT_PCM8_UNSIGNED 1 */
327 SNDRV_PCM_FORMAT_S16, /* HPI_FORMAT_PCM16_SIGNED 2 */
328 -1, /* HPI_FORMAT_MPEG_L1 3 */
329 SNDRV_PCM_FORMAT_MPEG, /* HPI_FORMAT_MPEG_L2 4 */
330 SNDRV_PCM_FORMAT_MPEG, /* HPI_FORMAT_MPEG_L3 5 */
331 -1, /* HPI_FORMAT_DOLBY_AC2 6 */
332 -1, /* HPI_FORMAT_DOLBY_AC3 7 */
333 SNDRV_PCM_FORMAT_S16_BE,/* HPI_FORMAT_PCM16_BIGENDIAN 8 */
334 -1, /* HPI_FORMAT_AA_TAGIT1_HITS 9 */
335 -1, /* HPI_FORMAT_AA_TAGIT1_INSERTS 10 */
336 SNDRV_PCM_FORMAT_S32, /* HPI_FORMAT_PCM32_SIGNED 11 */
337 -1, /* HPI_FORMAT_RAW_BITSTREAM 12 */
338 -1, /* HPI_FORMAT_AA_TAGIT1_HITS_EX1 13 */
339 SNDRV_PCM_FORMAT_FLOAT, /* HPI_FORMAT_PCM32_FLOAT 14 */
340#if 1
341 /* ALSA can't handle 3 byte sample size together with power-of-2
342 * constraint on buffer_bytes, so disable this format
343 */
344 -1
345#else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300346 /* SNDRV_PCM_FORMAT_S24_3LE */ /* HPI_FORMAT_PCM24_SIGNED 15 */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200347#endif
348};
349
350
351static int snd_card_asihpi_format_alsa2hpi(snd_pcm_format_t alsa_format,
352 u16 *hpi_format)
353{
354 u16 format;
355
356 for (format = HPI_FORMAT_PCM8_UNSIGNED;
357 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
358 if (hpi_to_alsa_formats[format] == alsa_format) {
359 *hpi_format = format;
360 return 0;
361 }
362 }
363
364 snd_printd(KERN_WARNING "failed match for alsa format %d\n",
365 alsa_format);
366 *hpi_format = 0;
367 return -EINVAL;
368}
369
370static void snd_card_asihpi_pcm_samplerates(struct snd_card_asihpi *asihpi,
371 struct snd_pcm_hardware *pcmhw)
372{
373 u16 err;
374 u32 h_control;
375 u32 sample_rate;
376 int idx;
377 unsigned int rate_min = 200000;
378 unsigned int rate_max = 0;
379 unsigned int rates = 0;
380
381 if (asihpi->support_mrx) {
382 rates |= SNDRV_PCM_RATE_CONTINUOUS;
383 rates |= SNDRV_PCM_RATE_8000_96000;
384 rate_min = 8000;
385 rate_max = 100000;
386 } else {
387 /* on cards without SRC,
388 valid rates are determined by sampleclock */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300389 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200390 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
391 HPI_CONTROL_SAMPLECLOCK, &h_control);
392 if (err) {
393 snd_printk(KERN_ERR
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300394 "No local sampleclock, err %d\n", err);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200395 }
396
Eliot Blennerhassett7bf76c32011-03-25 15:25:46 +1300397 for (idx = -1; idx < 100; idx++) {
398 if (idx == -1) {
399 if (hpi_sample_clock_get_sample_rate(h_control,
400 &sample_rate))
401 continue;
402 } else if (hpi_sample_clock_query_local_rate(h_control,
403 idx, &sample_rate)) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200404 break;
405 }
406
407 rate_min = min(rate_min, sample_rate);
408 rate_max = max(rate_max, sample_rate);
409
410 switch (sample_rate) {
411 case 5512:
412 rates |= SNDRV_PCM_RATE_5512;
413 break;
414 case 8000:
415 rates |= SNDRV_PCM_RATE_8000;
416 break;
417 case 11025:
418 rates |= SNDRV_PCM_RATE_11025;
419 break;
420 case 16000:
421 rates |= SNDRV_PCM_RATE_16000;
422 break;
423 case 22050:
424 rates |= SNDRV_PCM_RATE_22050;
425 break;
426 case 32000:
427 rates |= SNDRV_PCM_RATE_32000;
428 break;
429 case 44100:
430 rates |= SNDRV_PCM_RATE_44100;
431 break;
432 case 48000:
433 rates |= SNDRV_PCM_RATE_48000;
434 break;
435 case 64000:
436 rates |= SNDRV_PCM_RATE_64000;
437 break;
438 case 88200:
439 rates |= SNDRV_PCM_RATE_88200;
440 break;
441 case 96000:
442 rates |= SNDRV_PCM_RATE_96000;
443 break;
444 case 176400:
445 rates |= SNDRV_PCM_RATE_176400;
446 break;
447 case 192000:
448 rates |= SNDRV_PCM_RATE_192000;
449 break;
450 default: /* some other rate */
451 rates |= SNDRV_PCM_RATE_KNOT;
452 }
453 }
454 }
455
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200456 pcmhw->rates = rates;
457 pcmhw->rate_min = rate_min;
458 pcmhw->rate_max = rate_max;
459}
460
461static int snd_card_asihpi_pcm_hw_params(struct snd_pcm_substream *substream,
462 struct snd_pcm_hw_params *params)
463{
464 struct snd_pcm_runtime *runtime = substream->runtime;
465 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
466 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
467 int err;
468 u16 format;
Kulikov Vasiliy315e8f72010-07-15 22:48:19 +0400469 int width;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200470 unsigned int bytes_per_sec;
471
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200472 print_hwparams(substream, params);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200473 err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
474 if (err < 0)
475 return err;
476 err = snd_card_asihpi_format_alsa2hpi(params_format(params), &format);
477 if (err)
478 return err;
479
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200480 hpi_handle_error(hpi_format_create(&dpcm->format,
481 params_channels(params),
482 format, params_rate(params), 0, 0));
483
484 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300485 if (hpi_instream_reset(dpcm->h_stream) != 0)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200486 return -EINVAL;
487
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300488 if (hpi_instream_set_format(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200489 dpcm->h_stream, &dpcm->format) != 0)
490 return -EINVAL;
491 }
492
493 dpcm->hpi_buffer_attached = 0;
494 if (card->support_mmap) {
495
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 Blennerhassett719f82d2010-04-21 18:17:39 +0200597 if ((s->stream == SNDRV_PCM_STREAM_PLAYBACK) &&
598 (card->support_mmap)) {
599 /* How do I know how much valid data is present
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300600 * in buffer? Must be at least one period!
601 * Guessing 2 periods, but if
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200602 * buffer is bigger it may contain even more
603 * data??
604 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300605 unsigned int preload = ds->period_bytes * 1;
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300606 snd_printddd("%d preload x%x\n", s->number, preload);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200607 hpi_handle_error(hpi_outstream_write_buf(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300608 ds->h_stream,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300609 &runtime->dma_area[0],
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200610 preload,
611 &ds->format));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300612 ds->pcm_buf_host_rw_ofs = preload;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200613 }
614
615 if (card->support_grouping) {
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200616 snd_printdd("%d group\n", s->number);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300617 e = hpi_stream_group_add(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200618 dpcm->h_stream,
619 ds->h_stream);
620 if (!e) {
621 snd_pcm_trigger_done(s, substream);
622 } else {
623 hpi_handle_error(e);
624 break;
625 }
626 } else
627 break;
628 }
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300629 snd_printdd("start\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200630 /* start the master stream */
631 snd_card_asihpi_pcm_timer_start(substream);
Eliot Blennerhassettc4ed97d2011-02-10 17:26:20 +1300632 if ((substream->stream == SNDRV_PCM_STREAM_CAPTURE) ||
633 !card->support_mmap)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300634 hpi_handle_error(hpi_stream_start(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200635 break;
636
637 case SNDRV_PCM_TRIGGER_STOP:
638 snd_card_asihpi_pcm_timer_stop(substream);
639 snd_pcm_group_for_each_entry(s, substream) {
640 if (snd_pcm_substream_chip(s) != card)
641 continue;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300642 /* don't link Cap and Play */
643 if (substream->stream != s->stream)
644 continue;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200645
646 /*? workaround linked streams don't
647 transition to SETUP 20070706*/
648 s->runtime->status->state = SNDRV_PCM_STATE_SETUP;
649
650 if (card->support_grouping) {
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200651 snd_printdd("%d group\n", s->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200652 snd_pcm_trigger_done(s, substream);
653 } else
654 break;
655 }
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300656 snd_printdd("stop\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200657
658 /* _prepare and _hwparams reset the stream */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300659 hpi_handle_error(hpi_stream_stop(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200660 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
661 hpi_handle_error(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300662 hpi_outstream_reset(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200663
664 if (card->support_grouping)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300665 hpi_handle_error(hpi_stream_group_reset(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200666 break;
667
668 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300669 snd_printdd("pause release\n");
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300670 hpi_handle_error(hpi_stream_start(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200671 snd_card_asihpi_pcm_timer_start(substream);
672 break;
673 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300674 snd_printdd("pause\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200675 snd_card_asihpi_pcm_timer_stop(substream);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300676 hpi_handle_error(hpi_stream_stop(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200677 break;
678 default:
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300679 snd_printd(KERN_ERR "\tINVALID\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200680 return -EINVAL;
681 }
682
683 return 0;
684}
685
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200686/*algorithm outline
687 Without linking degenerates to getting single stream pos etc
688 Without mmap 2nd loop degenerates to snd_pcm_period_elapsed
689*/
690/*
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300691pcm_buf_dma_ofs=get_buf_pos(s);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200692for_each_linked_stream(s) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300693 pcm_buf_dma_ofs=get_buf_pos(s);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300694 min_buf_pos = modulo_min(min_buf_pos, pcm_buf_dma_ofs, buffer_bytes)
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300695 new_data = min(new_data, calc_new_data(pcm_buf_dma_ofs,irq_pos)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200696}
697timer.expires = jiffies + predict_next_period_ready(min_buf_pos);
698for_each_linked_stream(s) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300699 s->pcm_buf_dma_ofs = min_buf_pos;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300700 if (new_data > period_bytes) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200701 if (mmap) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300702 irq_pos = (irq_pos + period_bytes) % buffer_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200703 if (playback) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300704 write(period_bytes);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200705 } else {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300706 read(period_bytes);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200707 }
708 }
709 snd_pcm_period_elapsed(s);
710 }
711}
712*/
713
714/** Minimum of 2 modulo values. Works correctly when the difference between
715* the values is less than half the modulus
716*/
717static inline unsigned int modulo_min(unsigned int a, unsigned int b,
718 unsigned long int modulus)
719{
720 unsigned int result;
721 if (((a-b) % modulus) < (modulus/2))
722 result = b;
723 else
724 result = a;
725
726 return result;
727}
728
729/** Timer function, equivalent to interrupt service routine for cards
730*/
731static void snd_card_asihpi_timer_function(unsigned long data)
732{
733 struct snd_card_asihpi_pcm *dpcm = (struct snd_card_asihpi_pcm *)data;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300734 struct snd_pcm_substream *substream = dpcm->substream;
735 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200736 struct snd_pcm_runtime *runtime;
737 struct snd_pcm_substream *s;
738 unsigned int newdata = 0;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300739 unsigned int pcm_buf_dma_ofs, min_buf_pos = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200740 unsigned int remdata, xfercount, next_jiffies;
741 int first = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300742 int loops = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200743 u16 state;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300744 u32 buffer_size, bytes_avail, samples_played, on_card_bytes;
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200745 DEBUG_NAME(substream, name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200746
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200747 snd_printdd("%s snd_card_asihpi_timer_function\n", name);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300748
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200749 /* find minimum newdata and buffer pos in group */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300750 snd_pcm_group_for_each_entry(s, substream) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200751 struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
752 runtime = s->runtime;
753
754 if (snd_pcm_substream_chip(s) != card)
755 continue;
756
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300757 /* don't link Cap and Play */
758 if (substream->stream != s->stream)
759 continue;
760
761 hpi_handle_error(hpi_stream_get_info_ex(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200762 ds->h_stream, &state,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300763 &buffer_size, &bytes_avail,
764 &samples_played, &on_card_bytes));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200765
766 /* number of bytes in on-card buffer */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300767 runtime->delay = on_card_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200768
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300769 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300770 pcm_buf_dma_ofs = ds->pcm_buf_host_rw_ofs - bytes_avail;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300771 if (state == HPI_STATE_STOPPED) {
772 if ((bytes_avail == 0) &&
773 (on_card_bytes < ds->pcm_buf_host_rw_ofs)) {
774 hpi_handle_error(hpi_stream_start(ds->h_stream));
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300775 snd_printdd("P%d start\n", s->number);
Eliot Blennerhassett0b7ce9e2011-04-05 20:55:43 +1200776 ds->drained_count = 0;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300777 }
778 } else if (state == HPI_STATE_DRAINED) {
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300779 snd_printd(KERN_WARNING "P%d drained\n",
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300780 s->number);
Eliot Blennerhassett0b7ce9e2011-04-05 20:55:43 +1200781 ds->drained_count++;
782 if (ds->drained_count > 2) {
783 snd_pcm_stop(s, SNDRV_PCM_STATE_XRUN);
784 continue;
785 }
786 } else {
787 ds->drained_count = 0;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300788 }
789 } else
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300790 pcm_buf_dma_ofs = bytes_avail + ds->pcm_buf_host_rw_ofs;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200791
792 if (first) {
793 /* can't statically init min when wrap is involved */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300794 min_buf_pos = pcm_buf_dma_ofs;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300795 newdata = (pcm_buf_dma_ofs - ds->pcm_buf_elapsed_dma_ofs) % ds->buffer_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200796 first = 0;
797 } else {
798 min_buf_pos =
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300799 modulo_min(min_buf_pos, pcm_buf_dma_ofs, UINT_MAX+1L);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200800 newdata = min(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300801 (pcm_buf_dma_ofs - ds->pcm_buf_elapsed_dma_ofs) % ds->buffer_bytes,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200802 newdata);
803 }
804
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200805 snd_printdd("hw_ptr 0x%04lX, appl_ptr 0x%04lX\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200806 (unsigned long)frames_to_bytes(runtime,
807 runtime->status->hw_ptr),
808 (unsigned long)frames_to_bytes(runtime,
809 runtime->control->appl_ptr));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300810
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200811 snd_printdd("%d S=%d, "
812 "rw=0x%04X, dma=0x%04X, left=0x%04X, "
813 "aux=0x%04X space=0x%04X\n",
814 s->number, state,
815 ds->pcm_buf_host_rw_ofs, pcm_buf_dma_ofs,
816 (int)bytes_avail,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300817 (int)on_card_bytes, buffer_size-bytes_avail);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300818 loops++;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200819 }
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300820 pcm_buf_dma_ofs = min_buf_pos;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200821
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300822 remdata = newdata % dpcm->period_bytes;
823 xfercount = newdata - remdata; /* a multiple of period_bytes */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300824 /* come back when on_card_bytes has decreased enough to allow
825 write to happen, or when data has been consumed to make another
826 period
827 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300828 if (xfercount && (on_card_bytes > dpcm->period_bytes))
829 next_jiffies = ((on_card_bytes - dpcm->period_bytes) * HZ / dpcm->bytes_per_sec);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300830 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300831 next_jiffies = ((dpcm->period_bytes - remdata) * HZ / dpcm->bytes_per_sec);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300832
833 next_jiffies = max(next_jiffies, 1U);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200834 dpcm->timer.expires = jiffies + next_jiffies;
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200835 snd_printdd("jif %d buf pos 0x%04X newdata 0x%04X xfer 0x%04X\n",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300836 next_jiffies, pcm_buf_dma_ofs, newdata, xfercount);
837
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300838 snd_pcm_group_for_each_entry(s, substream) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200839 struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200840
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300841 /* don't link Cap and Play */
842 if (substream->stream != s->stream)
843 continue;
844
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300845 ds->pcm_buf_dma_ofs = pcm_buf_dma_ofs;
846
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300847 if (xfercount && (on_card_bytes <= ds->period_bytes)) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200848 if (card->support_mmap) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200849 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300850 snd_printddd("P%d write x%04x\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200851 s->number,
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300852 ds->period_bytes);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200853 hpi_handle_error(
854 hpi_outstream_write_buf(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300855 ds->h_stream,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200856 &s->runtime->
857 dma_area[0],
858 xfercount,
859 &ds->format));
860 } else {
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300861 snd_printddd("C%d read x%04x\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200862 s->number,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300863 xfercount);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200864 hpi_handle_error(
865 hpi_instream_read_buf(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300866 ds->h_stream,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200867 NULL, xfercount));
868 }
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300869 ds->pcm_buf_host_rw_ofs = ds->pcm_buf_host_rw_ofs + xfercount;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200870 } /* else R/W will be handled by read/write callbacks */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300871 ds->pcm_buf_elapsed_dma_ofs = pcm_buf_dma_ofs;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200872 snd_pcm_period_elapsed(s);
873 }
874 }
875
876 if (dpcm->respawn_timer)
877 add_timer(&dpcm->timer);
878}
879
880/***************************** PLAYBACK OPS ****************/
881static int snd_card_asihpi_playback_ioctl(struct snd_pcm_substream *substream,
882 unsigned int cmd, void *arg)
883{
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200884 snd_printddd(KERN_INFO "P%d ioctl %d\n", substream->number, cmd);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200885 return snd_pcm_lib_ioctl(substream, cmd, arg);
886}
887
888static int snd_card_asihpi_playback_prepare(struct snd_pcm_substream *
889 substream)
890{
891 struct snd_pcm_runtime *runtime = substream->runtime;
892 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
893
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200894 snd_printdd("P%d prepare\n", substream->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200895
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300896 hpi_handle_error(hpi_outstream_reset(dpcm->h_stream));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300897 dpcm->pcm_buf_host_rw_ofs = 0;
898 dpcm->pcm_buf_dma_ofs = 0;
899 dpcm->pcm_buf_elapsed_dma_ofs = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200900 return 0;
901}
902
903static snd_pcm_uframes_t
904snd_card_asihpi_playback_pointer(struct snd_pcm_substream *substream)
905{
906 struct snd_pcm_runtime *runtime = substream->runtime;
907 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
908 snd_pcm_uframes_t ptr;
909
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300910 ptr = bytes_to_frames(runtime, dpcm->pcm_buf_dma_ofs % dpcm->buffer_bytes);
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200911 snd_printddd("P%d pointer = 0x%04lx\n", substream->number, (unsigned long)ptr);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200912 return ptr;
913}
914
915static void snd_card_asihpi_playback_format(struct snd_card_asihpi *asihpi,
916 u32 h_stream,
917 struct snd_pcm_hardware *pcmhw)
918{
919 struct hpi_format hpi_format;
920 u16 format;
921 u16 err;
922 u32 h_control;
923 u32 sample_rate = 48000;
924
925 /* on cards without SRC, must query at valid rate,
926 * maybe set by external sync
927 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300928 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200929 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
930 HPI_CONTROL_SAMPLECLOCK, &h_control);
931
932 if (!err)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300933 err = hpi_sample_clock_get_sample_rate(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200934 &sample_rate);
935
936 for (format = HPI_FORMAT_PCM8_UNSIGNED;
937 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
938 err = hpi_format_create(&hpi_format,
939 2, format, sample_rate, 128000, 0);
940 if (!err)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300941 err = hpi_outstream_query_format(h_stream,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200942 &hpi_format);
943 if (!err && (hpi_to_alsa_formats[format] != -1))
944 pcmhw->formats |=
945 (1ULL << hpi_to_alsa_formats[format]);
946 }
947}
948
949static struct snd_pcm_hardware snd_card_asihpi_playback = {
950 .channels_min = 1,
951 .channels_max = 2,
952 .buffer_bytes_max = BUFFER_BYTES_MAX,
953 .period_bytes_min = PERIOD_BYTES_MIN,
954 .period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN,
955 .periods_min = PERIODS_MIN,
956 .periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN,
957 .fifo_size = 0,
958};
959
960static int snd_card_asihpi_playback_open(struct snd_pcm_substream *substream)
961{
962 struct snd_pcm_runtime *runtime = substream->runtime;
963 struct snd_card_asihpi_pcm *dpcm;
964 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
965 int err;
966
967 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
968 if (dpcm == NULL)
969 return -ENOMEM;
970
971 err =
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300972 hpi_outstream_open(card->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200973 substream->number, &dpcm->h_stream);
974 hpi_handle_error(err);
975 if (err)
976 kfree(dpcm);
977 if (err == HPI_ERROR_OBJ_ALREADY_OPEN)
978 return -EBUSY;
979 if (err)
980 return -EIO;
981
982 /*? also check ASI5000 samplerate source
983 If external, only support external rate.
984 If internal and other stream playing, cant switch
985 */
986
987 init_timer(&dpcm->timer);
988 dpcm->timer.data = (unsigned long) dpcm;
989 dpcm->timer.function = snd_card_asihpi_timer_function;
990 dpcm->substream = substream;
991 runtime->private_data = dpcm;
992 runtime->private_free = snd_card_asihpi_runtime_free;
993
994 snd_card_asihpi_playback.channels_max = card->out_max_chans;
995 /*?snd_card_asihpi_playback.period_bytes_min =
996 card->out_max_chans * 4096; */
997
998 snd_card_asihpi_playback_format(card, dpcm->h_stream,
999 &snd_card_asihpi_playback);
1000
1001 snd_card_asihpi_pcm_samplerates(card, &snd_card_asihpi_playback);
1002
1003 snd_card_asihpi_playback.info = SNDRV_PCM_INFO_INTERLEAVED |
1004 SNDRV_PCM_INFO_DOUBLE |
1005 SNDRV_PCM_INFO_BATCH |
1006 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1007 SNDRV_PCM_INFO_PAUSE;
1008
1009 if (card->support_mmap)
1010 snd_card_asihpi_playback.info |= SNDRV_PCM_INFO_MMAP |
1011 SNDRV_PCM_INFO_MMAP_VALID;
1012
1013 if (card->support_grouping)
1014 snd_card_asihpi_playback.info |= SNDRV_PCM_INFO_SYNC_START;
1015
1016 /* struct is copied, so can create initializer dynamically */
1017 runtime->hw = snd_card_asihpi_playback;
1018
1019 if (card->support_mmap)
1020 err = snd_pcm_hw_constraint_pow2(runtime, 0,
1021 SNDRV_PCM_HW_PARAM_BUFFER_BYTES);
1022 if (err < 0)
1023 return err;
1024
1025 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1026 card->update_interval_frames);
Eliot Blennerhassett26aebef2011-03-25 15:25:47 +13001027
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001028 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001029 card->update_interval_frames * 2, UINT_MAX);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001030
1031 snd_pcm_set_sync(substream);
1032
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001033 snd_printdd("playback open\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001034
1035 return 0;
1036}
1037
1038static int snd_card_asihpi_playback_close(struct snd_pcm_substream *substream)
1039{
1040 struct snd_pcm_runtime *runtime = substream->runtime;
1041 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1042
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001043 hpi_handle_error(hpi_outstream_close(dpcm->h_stream));
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001044 snd_printdd("playback close\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001045
1046 return 0;
1047}
1048
1049static int snd_card_asihpi_playback_copy(struct snd_pcm_substream *substream,
1050 int channel,
1051 snd_pcm_uframes_t pos,
1052 void __user *src,
1053 snd_pcm_uframes_t count)
1054{
1055 struct snd_pcm_runtime *runtime = substream->runtime;
1056 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1057 unsigned int len;
1058
1059 len = frames_to_bytes(runtime, count);
1060
1061 if (copy_from_user(runtime->dma_area, src, len))
1062 return -EFAULT;
1063
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001064 snd_printddd("playback copy%d %u bytes\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001065 substream->number, len);
1066
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001067 hpi_handle_error(hpi_outstream_write_buf(dpcm->h_stream,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001068 runtime->dma_area, len, &dpcm->format));
1069
Eliot Blennerhassett26aebef2011-03-25 15:25:47 +13001070 dpcm->pcm_buf_host_rw_ofs += len;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001071
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001072 return 0;
1073}
1074
1075static int snd_card_asihpi_playback_silence(struct snd_pcm_substream *
1076 substream, int channel,
1077 snd_pcm_uframes_t pos,
1078 snd_pcm_uframes_t count)
1079{
Eliot Blennerhassett26aebef2011-03-25 15:25:47 +13001080 /* Usually writes silence to DMA buffer, which should be overwritten
1081 by real audio later. Our fifos cannot be overwritten, and are not
1082 free-running DMAs. Silence is output on fifo underflow.
1083 This callback is still required to allow the copy callback to be used.
1084 */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001085 return 0;
1086}
1087
1088static struct snd_pcm_ops snd_card_asihpi_playback_ops = {
1089 .open = snd_card_asihpi_playback_open,
1090 .close = snd_card_asihpi_playback_close,
1091 .ioctl = snd_card_asihpi_playback_ioctl,
1092 .hw_params = snd_card_asihpi_pcm_hw_params,
1093 .hw_free = snd_card_asihpi_hw_free,
1094 .prepare = snd_card_asihpi_playback_prepare,
1095 .trigger = snd_card_asihpi_trigger,
1096 .pointer = snd_card_asihpi_playback_pointer,
1097 .copy = snd_card_asihpi_playback_copy,
1098 .silence = snd_card_asihpi_playback_silence,
1099};
1100
1101static struct snd_pcm_ops snd_card_asihpi_playback_mmap_ops = {
1102 .open = snd_card_asihpi_playback_open,
1103 .close = snd_card_asihpi_playback_close,
1104 .ioctl = snd_card_asihpi_playback_ioctl,
1105 .hw_params = snd_card_asihpi_pcm_hw_params,
1106 .hw_free = snd_card_asihpi_hw_free,
1107 .prepare = snd_card_asihpi_playback_prepare,
1108 .trigger = snd_card_asihpi_trigger,
1109 .pointer = snd_card_asihpi_playback_pointer,
1110};
1111
1112/***************************** CAPTURE OPS ****************/
1113static snd_pcm_uframes_t
1114snd_card_asihpi_capture_pointer(struct snd_pcm_substream *substream)
1115{
1116 struct snd_pcm_runtime *runtime = substream->runtime;
1117 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1118
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001119 snd_printddd("capture pointer %d=%d\n",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001120 substream->number, dpcm->pcm_buf_dma_ofs);
1121 /* NOTE Unlike playback can't use actual samples_played
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001122 for the capture position, because those samples aren't yet in
1123 the local buffer available for reading.
1124 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001125 return bytes_to_frames(runtime, dpcm->pcm_buf_dma_ofs % dpcm->buffer_bytes);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001126}
1127
1128static int snd_card_asihpi_capture_ioctl(struct snd_pcm_substream *substream,
1129 unsigned int cmd, void *arg)
1130{
1131 return snd_pcm_lib_ioctl(substream, cmd, arg);
1132}
1133
1134static int snd_card_asihpi_capture_prepare(struct snd_pcm_substream *substream)
1135{
1136 struct snd_pcm_runtime *runtime = substream->runtime;
1137 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1138
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001139 hpi_handle_error(hpi_instream_reset(dpcm->h_stream));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001140 dpcm->pcm_buf_host_rw_ofs = 0;
1141 dpcm->pcm_buf_dma_ofs = 0;
1142 dpcm->pcm_buf_elapsed_dma_ofs = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001143
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001144 snd_printdd("Capture Prepare %d\n", substream->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001145 return 0;
1146}
1147
1148
1149
1150static void snd_card_asihpi_capture_format(struct snd_card_asihpi *asihpi,
1151 u32 h_stream,
1152 struct snd_pcm_hardware *pcmhw)
1153{
1154 struct hpi_format hpi_format;
1155 u16 format;
1156 u16 err;
1157 u32 h_control;
1158 u32 sample_rate = 48000;
1159
1160 /* on cards without SRC, must query at valid rate,
1161 maybe set by external sync */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001162 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001163 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
1164 HPI_CONTROL_SAMPLECLOCK, &h_control);
1165
1166 if (!err)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001167 err = hpi_sample_clock_get_sample_rate(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001168 &sample_rate);
1169
1170 for (format = HPI_FORMAT_PCM8_UNSIGNED;
1171 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
1172
1173 err = hpi_format_create(&hpi_format, 2, format,
1174 sample_rate, 128000, 0);
1175 if (!err)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001176 err = hpi_instream_query_format(h_stream,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001177 &hpi_format);
1178 if (!err)
1179 pcmhw->formats |=
1180 (1ULL << hpi_to_alsa_formats[format]);
1181 }
1182}
1183
1184
1185static struct snd_pcm_hardware snd_card_asihpi_capture = {
1186 .channels_min = 1,
1187 .channels_max = 2,
1188 .buffer_bytes_max = BUFFER_BYTES_MAX,
1189 .period_bytes_min = PERIOD_BYTES_MIN,
1190 .period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN,
1191 .periods_min = PERIODS_MIN,
1192 .periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN,
1193 .fifo_size = 0,
1194};
1195
1196static int snd_card_asihpi_capture_open(struct snd_pcm_substream *substream)
1197{
1198 struct snd_pcm_runtime *runtime = substream->runtime;
1199 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
1200 struct snd_card_asihpi_pcm *dpcm;
1201 int err;
1202
1203 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
1204 if (dpcm == NULL)
1205 return -ENOMEM;
1206
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001207 snd_printdd("capture open adapter %d stream %d\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001208 card->adapter_index, substream->number);
1209
1210 err = hpi_handle_error(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001211 hpi_instream_open(card->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001212 substream->number, &dpcm->h_stream));
1213 if (err)
1214 kfree(dpcm);
1215 if (err == HPI_ERROR_OBJ_ALREADY_OPEN)
1216 return -EBUSY;
1217 if (err)
1218 return -EIO;
1219
1220
1221 init_timer(&dpcm->timer);
1222 dpcm->timer.data = (unsigned long) dpcm;
1223 dpcm->timer.function = snd_card_asihpi_timer_function;
1224 dpcm->substream = substream;
1225 runtime->private_data = dpcm;
1226 runtime->private_free = snd_card_asihpi_runtime_free;
1227
1228 snd_card_asihpi_capture.channels_max = card->in_max_chans;
1229 snd_card_asihpi_capture_format(card, dpcm->h_stream,
1230 &snd_card_asihpi_capture);
1231 snd_card_asihpi_pcm_samplerates(card, &snd_card_asihpi_capture);
1232 snd_card_asihpi_capture.info = SNDRV_PCM_INFO_INTERLEAVED;
1233
1234 if (card->support_mmap)
1235 snd_card_asihpi_capture.info |= SNDRV_PCM_INFO_MMAP |
1236 SNDRV_PCM_INFO_MMAP_VALID;
1237
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001238 if (card->support_grouping)
1239 snd_card_asihpi_capture.info |= SNDRV_PCM_INFO_SYNC_START;
1240
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001241 runtime->hw = snd_card_asihpi_capture;
1242
1243 if (card->support_mmap)
1244 err = snd_pcm_hw_constraint_pow2(runtime, 0,
1245 SNDRV_PCM_HW_PARAM_BUFFER_BYTES);
1246 if (err < 0)
1247 return err;
1248
1249 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1250 card->update_interval_frames);
1251 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1252 card->update_interval_frames * 2, UINT_MAX);
1253
1254 snd_pcm_set_sync(substream);
1255
1256 return 0;
1257}
1258
1259static int snd_card_asihpi_capture_close(struct snd_pcm_substream *substream)
1260{
1261 struct snd_card_asihpi_pcm *dpcm = substream->runtime->private_data;
1262
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001263 hpi_handle_error(hpi_instream_close(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001264 return 0;
1265}
1266
1267static int snd_card_asihpi_capture_copy(struct snd_pcm_substream *substream,
1268 int channel, snd_pcm_uframes_t pos,
1269 void __user *dst, snd_pcm_uframes_t count)
1270{
1271 struct snd_pcm_runtime *runtime = substream->runtime;
1272 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001273 u32 len;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001274
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001275 len = frames_to_bytes(runtime, count);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001276
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001277 snd_printddd("capture copy%d %d bytes\n", substream->number, len);
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001278 hpi_handle_error(hpi_instream_read_buf(dpcm->h_stream,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001279 runtime->dma_area, len));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001280
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001281 dpcm->pcm_buf_host_rw_ofs = dpcm->pcm_buf_host_rw_ofs + len;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001282
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001283 if (copy_to_user(dst, runtime->dma_area, len))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001284 return -EFAULT;
1285
1286 return 0;
1287}
1288
1289static struct snd_pcm_ops snd_card_asihpi_capture_mmap_ops = {
1290 .open = snd_card_asihpi_capture_open,
1291 .close = snd_card_asihpi_capture_close,
1292 .ioctl = snd_card_asihpi_capture_ioctl,
1293 .hw_params = snd_card_asihpi_pcm_hw_params,
1294 .hw_free = snd_card_asihpi_hw_free,
1295 .prepare = snd_card_asihpi_capture_prepare,
1296 .trigger = snd_card_asihpi_trigger,
1297 .pointer = snd_card_asihpi_capture_pointer,
1298};
1299
1300static struct snd_pcm_ops snd_card_asihpi_capture_ops = {
1301 .open = snd_card_asihpi_capture_open,
1302 .close = snd_card_asihpi_capture_close,
1303 .ioctl = snd_card_asihpi_capture_ioctl,
1304 .hw_params = snd_card_asihpi_pcm_hw_params,
1305 .hw_free = snd_card_asihpi_hw_free,
1306 .prepare = snd_card_asihpi_capture_prepare,
1307 .trigger = snd_card_asihpi_trigger,
1308 .pointer = snd_card_asihpi_capture_pointer,
1309 .copy = snd_card_asihpi_capture_copy
1310};
1311
1312static int __devinit snd_card_asihpi_pcm_new(struct snd_card_asihpi *asihpi,
1313 int device, int substreams)
1314{
1315 struct snd_pcm *pcm;
1316 int err;
1317
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001318 err = snd_pcm_new(asihpi->card, "Asihpi PCM", device,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001319 asihpi->num_outstreams, asihpi->num_instreams,
1320 &pcm);
1321 if (err < 0)
1322 return err;
1323 /* pointer to ops struct is stored, dont change ops afterwards! */
1324 if (asihpi->support_mmap) {
1325 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1326 &snd_card_asihpi_playback_mmap_ops);
1327 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1328 &snd_card_asihpi_capture_mmap_ops);
1329 } else {
1330 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1331 &snd_card_asihpi_playback_ops);
1332 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1333 &snd_card_asihpi_capture_ops);
1334 }
1335
1336 pcm->private_data = asihpi;
1337 pcm->info_flags = 0;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001338 strcpy(pcm->name, "Asihpi PCM");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001339
1340 /*? do we want to emulate MMAP for non-BBM cards?
1341 Jack doesn't work with ALSAs MMAP emulation - WHY NOT? */
1342 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1343 snd_dma_pci_data(asihpi->pci),
1344 64*1024, BUFFER_BYTES_MAX);
1345
1346 return 0;
1347}
1348
1349/***************************** MIXER CONTROLS ****************/
1350struct hpi_control {
1351 u32 h_control;
1352 u16 control_type;
1353 u16 src_node_type;
1354 u16 src_node_index;
1355 u16 dst_node_type;
1356 u16 dst_node_index;
1357 u16 band;
1358 char name[44]; /* copied to snd_ctl_elem_id.name[44]; */
1359};
1360
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001361static const char * const asihpi_tuner_band_names[] = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001362 "invalid",
1363 "AM",
1364 "FM mono",
1365 "TV NTSC-M",
1366 "FM stereo",
1367 "AUX",
1368 "TV PAL BG",
1369 "TV PAL I",
1370 "TV PAL DK",
1371 "TV SECAM",
1372};
1373
1374compile_time_assert(
1375 (ARRAY_SIZE(asihpi_tuner_band_names) ==
1376 (HPI_TUNER_BAND_LAST+1)),
1377 assert_tuner_band_names_size);
1378
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001379static const char * const asihpi_src_names[] = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001380 "no source",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001381 "PCM",
1382 "Line",
1383 "Digital",
1384 "Tuner",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001385 "RF",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001386 "Clock",
1387 "Bitstream",
1388 "Microphone",
1389 "Cobranet",
1390 "Analog",
1391 "Adapter",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001392};
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001393
1394compile_time_assert(
1395 (ARRAY_SIZE(asihpi_src_names) ==
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12001396 (HPI_SOURCENODE_LAST_INDEX-HPI_SOURCENODE_NONE+1)),
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001397 assert_src_names_size);
1398
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001399static const char * const asihpi_dst_names[] = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001400 "no destination",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001401 "PCM",
1402 "Line",
1403 "Digital",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001404 "RF",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001405 "Speaker",
1406 "Cobranet Out",
1407 "Analog"
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001408};
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001409
1410compile_time_assert(
1411 (ARRAY_SIZE(asihpi_dst_names) ==
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12001412 (HPI_DESTNODE_LAST_INDEX-HPI_DESTNODE_NONE+1)),
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001413 assert_dst_names_size);
1414
1415static inline int ctl_add(struct snd_card *card, struct snd_kcontrol_new *ctl,
1416 struct snd_card_asihpi *asihpi)
1417{
1418 int err;
1419
1420 err = snd_ctl_add(card, snd_ctl_new1(ctl, asihpi));
1421 if (err < 0)
1422 return err;
1423 else if (mixer_dump)
1424 snd_printk(KERN_INFO "added %s(%d)\n", ctl->name, ctl->index);
1425
1426 return 0;
1427}
1428
1429/* Convert HPI control name and location into ALSA control name */
1430static void asihpi_ctl_init(struct snd_kcontrol_new *snd_control,
1431 struct hpi_control *hpi_ctl,
1432 char *name)
1433{
Eliot Blennerhassett550ac6b2011-04-05 20:55:41 +12001434 char *dir;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001435 memset(snd_control, 0, sizeof(*snd_control));
1436 snd_control->name = hpi_ctl->name;
1437 snd_control->private_value = hpi_ctl->h_control;
1438 snd_control->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1439 snd_control->index = 0;
1440
Eliot Blennerhassett550ac6b2011-04-05 20:55:41 +12001441 if (hpi_ctl->src_node_type + HPI_SOURCENODE_NONE == HPI_SOURCENODE_CLOCK_SOURCE)
1442 dir = ""; /* clock is neither capture nor playback */
1443 else if (hpi_ctl->dst_node_type + HPI_DESTNODE_NONE == HPI_DESTNODE_ISTREAM)
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001444 dir = "Capture "; /* On or towards a PCM capture destination*/
1445 else if ((hpi_ctl->src_node_type + HPI_SOURCENODE_NONE != HPI_SOURCENODE_OSTREAM) &&
1446 (!hpi_ctl->dst_node_type))
1447 dir = "Capture "; /* On a source node that is not PCM playback */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001448 else if (hpi_ctl->src_node_type &&
1449 (hpi_ctl->src_node_type + HPI_SOURCENODE_NONE != HPI_SOURCENODE_OSTREAM) &&
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001450 (hpi_ctl->dst_node_type))
1451 dir = "Monitor Playback "; /* Between an input and an output */
1452 else
1453 dir = "Playback "; /* PCM Playback source, or output node */
1454
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001455 if (hpi_ctl->src_node_type && hpi_ctl->dst_node_type)
Eliot Blennerhassett550ac6b2011-04-05 20:55:41 +12001456 sprintf(hpi_ctl->name, "%s %d %s %d %s%s",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001457 asihpi_src_names[hpi_ctl->src_node_type],
1458 hpi_ctl->src_node_index,
1459 asihpi_dst_names[hpi_ctl->dst_node_type],
1460 hpi_ctl->dst_node_index,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001461 dir, name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001462 else if (hpi_ctl->dst_node_type) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001463 sprintf(hpi_ctl->name, "%s %d %s%s",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001464 asihpi_dst_names[hpi_ctl->dst_node_type],
1465 hpi_ctl->dst_node_index,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001466 dir, name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001467 } else {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001468 sprintf(hpi_ctl->name, "%s %d %s%s",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001469 asihpi_src_names[hpi_ctl->src_node_type],
1470 hpi_ctl->src_node_index,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001471 dir, name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001472 }
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001473 /* printk(KERN_INFO "Adding %s %d to %d ", hpi_ctl->name,
1474 hpi_ctl->wSrcNodeType, hpi_ctl->wDstNodeType); */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001475}
1476
1477/*------------------------------------------------------------
1478 Volume controls
1479 ------------------------------------------------------------*/
1480#define VOL_STEP_mB 1
1481static int snd_asihpi_volume_info(struct snd_kcontrol *kcontrol,
1482 struct snd_ctl_elem_info *uinfo)
1483{
1484 u32 h_control = kcontrol->private_value;
1485 u16 err;
1486 /* native gains are in millibels */
1487 short min_gain_mB;
1488 short max_gain_mB;
1489 short step_gain_mB;
1490
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001491 err = hpi_volume_query_range(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001492 &min_gain_mB, &max_gain_mB, &step_gain_mB);
1493 if (err) {
1494 max_gain_mB = 0;
1495 min_gain_mB = -10000;
1496 step_gain_mB = VOL_STEP_mB;
1497 }
1498
1499 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1500 uinfo->count = 2;
1501 uinfo->value.integer.min = min_gain_mB / VOL_STEP_mB;
1502 uinfo->value.integer.max = max_gain_mB / VOL_STEP_mB;
1503 uinfo->value.integer.step = step_gain_mB / VOL_STEP_mB;
1504 return 0;
1505}
1506
1507static int snd_asihpi_volume_get(struct snd_kcontrol *kcontrol,
1508 struct snd_ctl_elem_value *ucontrol)
1509{
1510 u32 h_control = kcontrol->private_value;
1511 short an_gain_mB[HPI_MAX_CHANNELS];
1512
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001513 hpi_handle_error(hpi_volume_get_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001514 ucontrol->value.integer.value[0] = an_gain_mB[0] / VOL_STEP_mB;
1515 ucontrol->value.integer.value[1] = an_gain_mB[1] / VOL_STEP_mB;
1516
1517 return 0;
1518}
1519
1520static int snd_asihpi_volume_put(struct snd_kcontrol *kcontrol,
1521 struct snd_ctl_elem_value *ucontrol)
1522{
1523 int change;
1524 u32 h_control = kcontrol->private_value;
1525 short an_gain_mB[HPI_MAX_CHANNELS];
1526
1527 an_gain_mB[0] =
1528 (ucontrol->value.integer.value[0]) * VOL_STEP_mB;
1529 an_gain_mB[1] =
1530 (ucontrol->value.integer.value[1]) * VOL_STEP_mB;
1531 /* change = asihpi->mixer_volume[addr][0] != left ||
1532 asihpi->mixer_volume[addr][1] != right;
1533 */
1534 change = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001535 hpi_handle_error(hpi_volume_set_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001536 return change;
1537}
1538
1539static const DECLARE_TLV_DB_SCALE(db_scale_100, -10000, VOL_STEP_mB, 0);
1540
1541static int __devinit snd_asihpi_volume_add(struct snd_card_asihpi *asihpi,
1542 struct hpi_control *hpi_ctl)
1543{
1544 struct snd_card *card = asihpi->card;
1545 struct snd_kcontrol_new snd_control;
1546
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001547 asihpi_ctl_init(&snd_control, hpi_ctl, "Volume");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001548 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1549 SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1550 snd_control.info = snd_asihpi_volume_info;
1551 snd_control.get = snd_asihpi_volume_get;
1552 snd_control.put = snd_asihpi_volume_put;
1553 snd_control.tlv.p = db_scale_100;
1554
1555 return ctl_add(card, &snd_control, asihpi);
1556}
1557
1558/*------------------------------------------------------------
1559 Level controls
1560 ------------------------------------------------------------*/
1561static int snd_asihpi_level_info(struct snd_kcontrol *kcontrol,
1562 struct snd_ctl_elem_info *uinfo)
1563{
1564 u32 h_control = kcontrol->private_value;
1565 u16 err;
1566 short min_gain_mB;
1567 short max_gain_mB;
1568 short step_gain_mB;
1569
1570 err =
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001571 hpi_level_query_range(h_control, &min_gain_mB,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001572 &max_gain_mB, &step_gain_mB);
1573 if (err) {
1574 max_gain_mB = 2400;
1575 min_gain_mB = -1000;
1576 step_gain_mB = 100;
1577 }
1578
1579 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1580 uinfo->count = 2;
1581 uinfo->value.integer.min = min_gain_mB / HPI_UNITS_PER_dB;
1582 uinfo->value.integer.max = max_gain_mB / HPI_UNITS_PER_dB;
1583 uinfo->value.integer.step = step_gain_mB / HPI_UNITS_PER_dB;
1584 return 0;
1585}
1586
1587static int snd_asihpi_level_get(struct snd_kcontrol *kcontrol,
1588 struct snd_ctl_elem_value *ucontrol)
1589{
1590 u32 h_control = kcontrol->private_value;
1591 short an_gain_mB[HPI_MAX_CHANNELS];
1592
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001593 hpi_handle_error(hpi_level_get_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001594 ucontrol->value.integer.value[0] =
1595 an_gain_mB[0] / HPI_UNITS_PER_dB;
1596 ucontrol->value.integer.value[1] =
1597 an_gain_mB[1] / HPI_UNITS_PER_dB;
1598
1599 return 0;
1600}
1601
1602static int snd_asihpi_level_put(struct snd_kcontrol *kcontrol,
1603 struct snd_ctl_elem_value *ucontrol)
1604{
1605 int change;
1606 u32 h_control = kcontrol->private_value;
1607 short an_gain_mB[HPI_MAX_CHANNELS];
1608
1609 an_gain_mB[0] =
1610 (ucontrol->value.integer.value[0]) * HPI_UNITS_PER_dB;
1611 an_gain_mB[1] =
1612 (ucontrol->value.integer.value[1]) * HPI_UNITS_PER_dB;
1613 /* change = asihpi->mixer_level[addr][0] != left ||
1614 asihpi->mixer_level[addr][1] != right;
1615 */
1616 change = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001617 hpi_handle_error(hpi_level_set_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001618 return change;
1619}
1620
1621static const DECLARE_TLV_DB_SCALE(db_scale_level, -1000, 100, 0);
1622
1623static int __devinit snd_asihpi_level_add(struct snd_card_asihpi *asihpi,
1624 struct hpi_control *hpi_ctl)
1625{
1626 struct snd_card *card = asihpi->card;
1627 struct snd_kcontrol_new snd_control;
1628
1629 /* can't use 'volume' cos some nodes have volume as well */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001630 asihpi_ctl_init(&snd_control, hpi_ctl, "Level");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001631 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1632 SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1633 snd_control.info = snd_asihpi_level_info;
1634 snd_control.get = snd_asihpi_level_get;
1635 snd_control.put = snd_asihpi_level_put;
1636 snd_control.tlv.p = db_scale_level;
1637
1638 return ctl_add(card, &snd_control, asihpi);
1639}
1640
1641/*------------------------------------------------------------
1642 AESEBU controls
1643 ------------------------------------------------------------*/
1644
1645/* AESEBU format */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001646static const char * const asihpi_aesebu_format_names[] = {
1647 "N/A", "S/PDIF", "AES/EBU" };
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001648
1649static int snd_asihpi_aesebu_format_info(struct snd_kcontrol *kcontrol,
1650 struct snd_ctl_elem_info *uinfo)
1651{
1652 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1653 uinfo->count = 1;
1654 uinfo->value.enumerated.items = 3;
1655
1656 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1657 uinfo->value.enumerated.item =
1658 uinfo->value.enumerated.items - 1;
1659
1660 strcpy(uinfo->value.enumerated.name,
1661 asihpi_aesebu_format_names[uinfo->value.enumerated.item]);
1662
1663 return 0;
1664}
1665
1666static int snd_asihpi_aesebu_format_get(struct snd_kcontrol *kcontrol,
1667 struct snd_ctl_elem_value *ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001668 u16 (*func)(u32, u16 *))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001669{
1670 u32 h_control = kcontrol->private_value;
1671 u16 source, err;
1672
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001673 err = func(h_control, &source);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001674
1675 /* default to N/A */
1676 ucontrol->value.enumerated.item[0] = 0;
1677 /* return success but set the control to N/A */
1678 if (err)
1679 return 0;
1680 if (source == HPI_AESEBU_FORMAT_SPDIF)
1681 ucontrol->value.enumerated.item[0] = 1;
1682 if (source == HPI_AESEBU_FORMAT_AESEBU)
1683 ucontrol->value.enumerated.item[0] = 2;
1684
1685 return 0;
1686}
1687
1688static int snd_asihpi_aesebu_format_put(struct snd_kcontrol *kcontrol,
1689 struct snd_ctl_elem_value *ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001690 u16 (*func)(u32, u16))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001691{
1692 u32 h_control = kcontrol->private_value;
1693
1694 /* default to S/PDIF */
1695 u16 source = HPI_AESEBU_FORMAT_SPDIF;
1696
1697 if (ucontrol->value.enumerated.item[0] == 1)
1698 source = HPI_AESEBU_FORMAT_SPDIF;
1699 if (ucontrol->value.enumerated.item[0] == 2)
1700 source = HPI_AESEBU_FORMAT_AESEBU;
1701
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001702 if (func(h_control, source) != 0)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001703 return -EINVAL;
1704
1705 return 1;
1706}
1707
1708static int snd_asihpi_aesebu_rx_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_receiver_get_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001712}
1713
1714static int snd_asihpi_aesebu_rx_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_receiver_set_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001718}
1719
1720static int snd_asihpi_aesebu_rxstatus_info(struct snd_kcontrol *kcontrol,
1721 struct snd_ctl_elem_info *uinfo)
1722{
1723 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1724 uinfo->count = 1;
1725
1726 uinfo->value.integer.min = 0;
1727 uinfo->value.integer.max = 0X1F;
1728 uinfo->value.integer.step = 1;
1729
1730 return 0;
1731}
1732
1733static int snd_asihpi_aesebu_rxstatus_get(struct snd_kcontrol *kcontrol,
1734 struct snd_ctl_elem_value *ucontrol) {
1735
1736 u32 h_control = kcontrol->private_value;
1737 u16 status;
1738
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001739 hpi_handle_error(hpi_aesebu_receiver_get_error_status(
1740 h_control, &status));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001741 ucontrol->value.integer.value[0] = status;
1742 return 0;
1743}
1744
1745static int __devinit snd_asihpi_aesebu_rx_add(struct snd_card_asihpi *asihpi,
1746 struct hpi_control *hpi_ctl)
1747{
1748 struct snd_card *card = asihpi->card;
1749 struct snd_kcontrol_new snd_control;
1750
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001751 asihpi_ctl_init(&snd_control, hpi_ctl, "Format");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001752 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1753 snd_control.info = snd_asihpi_aesebu_format_info;
1754 snd_control.get = snd_asihpi_aesebu_rx_format_get;
1755 snd_control.put = snd_asihpi_aesebu_rx_format_put;
1756
1757
1758 if (ctl_add(card, &snd_control, asihpi) < 0)
1759 return -EINVAL;
1760
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001761 asihpi_ctl_init(&snd_control, hpi_ctl, "Status");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001762 snd_control.access =
1763 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
1764 snd_control.info = snd_asihpi_aesebu_rxstatus_info;
1765 snd_control.get = snd_asihpi_aesebu_rxstatus_get;
1766
1767 return ctl_add(card, &snd_control, asihpi);
1768}
1769
1770static int snd_asihpi_aesebu_tx_format_get(struct snd_kcontrol *kcontrol,
1771 struct snd_ctl_elem_value *ucontrol) {
1772 return snd_asihpi_aesebu_format_get(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001773 hpi_aesebu_transmitter_get_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001774}
1775
1776static int snd_asihpi_aesebu_tx_format_put(struct snd_kcontrol *kcontrol,
1777 struct snd_ctl_elem_value *ucontrol) {
1778 return snd_asihpi_aesebu_format_put(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001779 hpi_aesebu_transmitter_set_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001780}
1781
1782
1783static int __devinit snd_asihpi_aesebu_tx_add(struct snd_card_asihpi *asihpi,
1784 struct hpi_control *hpi_ctl)
1785{
1786 struct snd_card *card = asihpi->card;
1787 struct snd_kcontrol_new snd_control;
1788
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001789 asihpi_ctl_init(&snd_control, hpi_ctl, "Format");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001790 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1791 snd_control.info = snd_asihpi_aesebu_format_info;
1792 snd_control.get = snd_asihpi_aesebu_tx_format_get;
1793 snd_control.put = snd_asihpi_aesebu_tx_format_put;
1794
1795 return ctl_add(card, &snd_control, asihpi);
1796}
1797
1798/*------------------------------------------------------------
1799 Tuner controls
1800 ------------------------------------------------------------*/
1801
1802/* Gain */
1803
1804static int snd_asihpi_tuner_gain_info(struct snd_kcontrol *kcontrol,
1805 struct snd_ctl_elem_info *uinfo)
1806{
1807 u32 h_control = kcontrol->private_value;
1808 u16 err;
1809 short idx;
1810 u16 gain_range[3];
1811
1812 for (idx = 0; idx < 3; idx++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001813 err = hpi_tuner_query_gain(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001814 idx, &gain_range[idx]);
1815 if (err != 0)
1816 return err;
1817 }
1818
1819 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1820 uinfo->count = 1;
1821 uinfo->value.integer.min = ((int)gain_range[0]) / HPI_UNITS_PER_dB;
1822 uinfo->value.integer.max = ((int)gain_range[1]) / HPI_UNITS_PER_dB;
1823 uinfo->value.integer.step = ((int) gain_range[2]) / HPI_UNITS_PER_dB;
1824 return 0;
1825}
1826
1827static int snd_asihpi_tuner_gain_get(struct snd_kcontrol *kcontrol,
1828 struct snd_ctl_elem_value *ucontrol)
1829{
1830 /*
1831 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1832 */
1833 u32 h_control = kcontrol->private_value;
1834 short gain;
1835
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001836 hpi_handle_error(hpi_tuner_get_gain(h_control, &gain));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001837 ucontrol->value.integer.value[0] = gain / HPI_UNITS_PER_dB;
1838
1839 return 0;
1840}
1841
1842static int snd_asihpi_tuner_gain_put(struct snd_kcontrol *kcontrol,
1843 struct snd_ctl_elem_value *ucontrol)
1844{
1845 /*
1846 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1847 */
1848 u32 h_control = kcontrol->private_value;
1849 short gain;
1850
1851 gain = (ucontrol->value.integer.value[0]) * HPI_UNITS_PER_dB;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001852 hpi_handle_error(hpi_tuner_set_gain(h_control, gain));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001853
1854 return 1;
1855}
1856
1857/* Band */
1858
1859static int asihpi_tuner_band_query(struct snd_kcontrol *kcontrol,
1860 u16 *band_list, u32 len) {
1861 u32 h_control = kcontrol->private_value;
1862 u16 err = 0;
1863 u32 i;
1864
1865 for (i = 0; i < len; i++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001866 err = hpi_tuner_query_band(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001867 h_control, i, &band_list[i]);
1868 if (err != 0)
1869 break;
1870 }
1871
1872 if (err && (err != HPI_ERROR_INVALID_OBJ_INDEX))
1873 return -EIO;
1874
1875 return i;
1876}
1877
1878static int snd_asihpi_tuner_band_info(struct snd_kcontrol *kcontrol,
1879 struct snd_ctl_elem_info *uinfo)
1880{
1881 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1882 int num_bands = 0;
1883
1884 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1885 HPI_TUNER_BAND_LAST);
1886
1887 if (num_bands < 0)
1888 return num_bands;
1889
1890 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1891 uinfo->count = 1;
1892 uinfo->value.enumerated.items = num_bands;
1893
1894 if (num_bands > 0) {
1895 if (uinfo->value.enumerated.item >=
1896 uinfo->value.enumerated.items)
1897 uinfo->value.enumerated.item =
1898 uinfo->value.enumerated.items - 1;
1899
1900 strcpy(uinfo->value.enumerated.name,
1901 asihpi_tuner_band_names[
1902 tuner_bands[uinfo->value.enumerated.item]]);
1903
1904 }
1905 return 0;
1906}
1907
1908static int snd_asihpi_tuner_band_get(struct snd_kcontrol *kcontrol,
1909 struct snd_ctl_elem_value *ucontrol)
1910{
1911 u32 h_control = kcontrol->private_value;
1912 /*
1913 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1914 */
1915 u16 band, idx;
1916 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1917 u32 num_bands = 0;
1918
1919 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1920 HPI_TUNER_BAND_LAST);
1921
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001922 hpi_handle_error(hpi_tuner_get_band(h_control, &band));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001923
1924 ucontrol->value.enumerated.item[0] = -1;
1925 for (idx = 0; idx < HPI_TUNER_BAND_LAST; idx++)
1926 if (tuner_bands[idx] == band) {
1927 ucontrol->value.enumerated.item[0] = idx;
1928 break;
1929 }
1930
1931 return 0;
1932}
1933
1934static int snd_asihpi_tuner_band_put(struct snd_kcontrol *kcontrol,
1935 struct snd_ctl_elem_value *ucontrol)
1936{
1937 /*
1938 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1939 */
1940 u32 h_control = kcontrol->private_value;
1941 u16 band;
1942 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1943 u32 num_bands = 0;
1944
1945 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1946 HPI_TUNER_BAND_LAST);
1947
1948 band = tuner_bands[ucontrol->value.enumerated.item[0]];
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001949 hpi_handle_error(hpi_tuner_set_band(h_control, band));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001950
1951 return 1;
1952}
1953
1954/* Freq */
1955
1956static int snd_asihpi_tuner_freq_info(struct snd_kcontrol *kcontrol,
1957 struct snd_ctl_elem_info *uinfo)
1958{
1959 u32 h_control = kcontrol->private_value;
1960 u16 err;
1961 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1962 u16 num_bands = 0, band_iter, idx;
1963 u32 freq_range[3], temp_freq_range[3];
1964
1965 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1966 HPI_TUNER_BAND_LAST);
1967
1968 freq_range[0] = INT_MAX;
1969 freq_range[1] = 0;
1970 freq_range[2] = INT_MAX;
1971
1972 for (band_iter = 0; band_iter < num_bands; band_iter++) {
1973 for (idx = 0; idx < 3; idx++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001974 err = hpi_tuner_query_frequency(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001975 idx, tuner_bands[band_iter],
1976 &temp_freq_range[idx]);
1977 if (err != 0)
1978 return err;
1979 }
1980
1981 /* skip band with bogus stepping */
1982 if (temp_freq_range[2] <= 0)
1983 continue;
1984
1985 if (temp_freq_range[0] < freq_range[0])
1986 freq_range[0] = temp_freq_range[0];
1987 if (temp_freq_range[1] > freq_range[1])
1988 freq_range[1] = temp_freq_range[1];
1989 if (temp_freq_range[2] < freq_range[2])
1990 freq_range[2] = temp_freq_range[2];
1991 }
1992
1993 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1994 uinfo->count = 1;
1995 uinfo->value.integer.min = ((int)freq_range[0]);
1996 uinfo->value.integer.max = ((int)freq_range[1]);
1997 uinfo->value.integer.step = ((int)freq_range[2]);
1998 return 0;
1999}
2000
2001static int snd_asihpi_tuner_freq_get(struct snd_kcontrol *kcontrol,
2002 struct snd_ctl_elem_value *ucontrol)
2003{
2004 u32 h_control = kcontrol->private_value;
2005 u32 freq;
2006
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002007 hpi_handle_error(hpi_tuner_get_frequency(h_control, &freq));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002008 ucontrol->value.integer.value[0] = freq;
2009
2010 return 0;
2011}
2012
2013static int snd_asihpi_tuner_freq_put(struct snd_kcontrol *kcontrol,
2014 struct snd_ctl_elem_value *ucontrol)
2015{
2016 u32 h_control = kcontrol->private_value;
2017 u32 freq;
2018
2019 freq = ucontrol->value.integer.value[0];
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002020 hpi_handle_error(hpi_tuner_set_frequency(h_control, freq));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002021
2022 return 1;
2023}
2024
2025/* Tuner control group initializer */
2026static int __devinit snd_asihpi_tuner_add(struct snd_card_asihpi *asihpi,
2027 struct hpi_control *hpi_ctl)
2028{
2029 struct snd_card *card = asihpi->card;
2030 struct snd_kcontrol_new snd_control;
2031
2032 snd_control.private_value = hpi_ctl->h_control;
2033 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2034
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002035 if (!hpi_tuner_get_gain(hpi_ctl->h_control, NULL)) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002036 asihpi_ctl_init(&snd_control, hpi_ctl, "Gain");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002037 snd_control.info = snd_asihpi_tuner_gain_info;
2038 snd_control.get = snd_asihpi_tuner_gain_get;
2039 snd_control.put = snd_asihpi_tuner_gain_put;
2040
2041 if (ctl_add(card, &snd_control, asihpi) < 0)
2042 return -EINVAL;
2043 }
2044
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002045 asihpi_ctl_init(&snd_control, hpi_ctl, "Band");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002046 snd_control.info = snd_asihpi_tuner_band_info;
2047 snd_control.get = snd_asihpi_tuner_band_get;
2048 snd_control.put = snd_asihpi_tuner_band_put;
2049
2050 if (ctl_add(card, &snd_control, asihpi) < 0)
2051 return -EINVAL;
2052
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002053 asihpi_ctl_init(&snd_control, hpi_ctl, "Freq");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002054 snd_control.info = snd_asihpi_tuner_freq_info;
2055 snd_control.get = snd_asihpi_tuner_freq_get;
2056 snd_control.put = snd_asihpi_tuner_freq_put;
2057
2058 return ctl_add(card, &snd_control, asihpi);
2059}
2060
2061/*------------------------------------------------------------
2062 Meter controls
2063 ------------------------------------------------------------*/
2064static int snd_asihpi_meter_info(struct snd_kcontrol *kcontrol,
2065 struct snd_ctl_elem_info *uinfo)
2066{
2067 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2068 uinfo->count = HPI_MAX_CHANNELS;
2069 uinfo->value.integer.min = 0;
2070 uinfo->value.integer.max = 0x7FFFFFFF;
2071 return 0;
2072}
2073
2074/* linear values for 10dB steps */
2075static int log2lin[] = {
2076 0x7FFFFFFF, /* 0dB */
2077 679093956,
2078 214748365,
2079 67909396,
2080 21474837,
2081 6790940,
2082 2147484, /* -60dB */
2083 679094,
2084 214748, /* -80 */
2085 67909,
2086 21475, /* -100 */
2087 6791,
2088 2147,
2089 679,
2090 214,
2091 68,
2092 21,
2093 7,
2094 2
2095};
2096
2097static int snd_asihpi_meter_get(struct snd_kcontrol *kcontrol,
2098 struct snd_ctl_elem_value *ucontrol)
2099{
2100 u32 h_control = kcontrol->private_value;
2101 short an_gain_mB[HPI_MAX_CHANNELS], i;
2102 u16 err;
2103
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002104 err = hpi_meter_get_peak(h_control, an_gain_mB);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002105
2106 for (i = 0; i < HPI_MAX_CHANNELS; i++) {
2107 if (err) {
2108 ucontrol->value.integer.value[i] = 0;
2109 } else if (an_gain_mB[i] >= 0) {
2110 ucontrol->value.integer.value[i] =
2111 an_gain_mB[i] << 16;
2112 } else {
2113 /* -ve is log value in millibels < -60dB,
2114 * convert to (roughly!) linear,
2115 */
2116 ucontrol->value.integer.value[i] =
2117 log2lin[an_gain_mB[i] / -1000];
2118 }
2119 }
2120 return 0;
2121}
2122
2123static int __devinit snd_asihpi_meter_add(struct snd_card_asihpi *asihpi,
2124 struct hpi_control *hpi_ctl, int subidx)
2125{
2126 struct snd_card *card = asihpi->card;
2127 struct snd_kcontrol_new snd_control;
2128
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002129 asihpi_ctl_init(&snd_control, hpi_ctl, "Meter");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002130 snd_control.access =
2131 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
2132 snd_control.info = snd_asihpi_meter_info;
2133 snd_control.get = snd_asihpi_meter_get;
2134
2135 snd_control.index = subidx;
2136
2137 return ctl_add(card, &snd_control, asihpi);
2138}
2139
2140/*------------------------------------------------------------
2141 Multiplexer controls
2142 ------------------------------------------------------------*/
2143static int snd_card_asihpi_mux_count_sources(struct snd_kcontrol *snd_control)
2144{
2145 u32 h_control = snd_control->private_value;
2146 struct hpi_control hpi_ctl;
2147 int s, err;
2148 for (s = 0; s < 32; s++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002149 err = hpi_multiplexer_query_source(h_control, s,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002150 &hpi_ctl.
2151 src_node_type,
2152 &hpi_ctl.
2153 src_node_index);
2154 if (err)
2155 break;
2156 }
2157 return s;
2158}
2159
2160static int snd_asihpi_mux_info(struct snd_kcontrol *kcontrol,
2161 struct snd_ctl_elem_info *uinfo)
2162{
2163 int err;
2164 u16 src_node_type, src_node_index;
2165 u32 h_control = kcontrol->private_value;
2166
2167 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2168 uinfo->count = 1;
2169 uinfo->value.enumerated.items =
2170 snd_card_asihpi_mux_count_sources(kcontrol);
2171
2172 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2173 uinfo->value.enumerated.item =
2174 uinfo->value.enumerated.items - 1;
2175
2176 err =
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002177 hpi_multiplexer_query_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002178 uinfo->value.enumerated.item,
2179 &src_node_type, &src_node_index);
2180
2181 sprintf(uinfo->value.enumerated.name, "%s %d",
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12002182 asihpi_src_names[src_node_type - HPI_SOURCENODE_NONE],
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002183 src_node_index);
2184 return 0;
2185}
2186
2187static int snd_asihpi_mux_get(struct snd_kcontrol *kcontrol,
2188 struct snd_ctl_elem_value *ucontrol)
2189{
2190 u32 h_control = kcontrol->private_value;
2191 u16 source_type, source_index;
2192 u16 src_node_type, src_node_index;
2193 int s;
2194
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002195 hpi_handle_error(hpi_multiplexer_get_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002196 &source_type, &source_index));
2197 /* Should cache this search result! */
2198 for (s = 0; s < 256; s++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002199 if (hpi_multiplexer_query_source(h_control, s,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002200 &src_node_type, &src_node_index))
2201 break;
2202
2203 if ((source_type == src_node_type)
2204 && (source_index == src_node_index)) {
2205 ucontrol->value.enumerated.item[0] = s;
2206 return 0;
2207 }
2208 }
2209 snd_printd(KERN_WARNING
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002210 "Control %x failed to match mux source %hu %hu\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002211 h_control, source_type, source_index);
2212 ucontrol->value.enumerated.item[0] = 0;
2213 return 0;
2214}
2215
2216static int snd_asihpi_mux_put(struct snd_kcontrol *kcontrol,
2217 struct snd_ctl_elem_value *ucontrol)
2218{
2219 int change;
2220 u32 h_control = kcontrol->private_value;
2221 u16 source_type, source_index;
2222 u16 e;
2223
2224 change = 1;
2225
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002226 e = hpi_multiplexer_query_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002227 ucontrol->value.enumerated.item[0],
2228 &source_type, &source_index);
2229 if (!e)
2230 hpi_handle_error(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002231 hpi_multiplexer_set_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002232 source_type, source_index));
2233 return change;
2234}
2235
2236
2237static int __devinit snd_asihpi_mux_add(struct snd_card_asihpi *asihpi,
2238 struct hpi_control *hpi_ctl)
2239{
2240 struct snd_card *card = asihpi->card;
2241 struct snd_kcontrol_new snd_control;
2242
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002243 asihpi_ctl_init(&snd_control, hpi_ctl, "Route");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002244 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2245 snd_control.info = snd_asihpi_mux_info;
2246 snd_control.get = snd_asihpi_mux_get;
2247 snd_control.put = snd_asihpi_mux_put;
2248
2249 return ctl_add(card, &snd_control, asihpi);
2250
2251}
2252
2253/*------------------------------------------------------------
2254 Channel mode controls
2255 ------------------------------------------------------------*/
2256static int snd_asihpi_cmode_info(struct snd_kcontrol *kcontrol,
2257 struct snd_ctl_elem_info *uinfo)
2258{
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002259 static const char * const mode_names[HPI_CHANNEL_MODE_LAST + 1] = {
2260 "invalid",
2261 "Normal", "Swap",
2262 "From Left", "From Right",
2263 "To Left", "To Right"
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002264 };
2265
2266 u32 h_control = kcontrol->private_value;
2267 u16 mode;
2268 int i;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002269 u16 mode_map[6];
2270 int valid_modes = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002271
2272 /* HPI channel mode values can be from 1 to 6
2273 Some adapters only support a contiguous subset
2274 */
2275 for (i = 0; i < HPI_CHANNEL_MODE_LAST; i++)
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002276 if (!hpi_channel_mode_query_mode(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002277 h_control, i, &mode)) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002278 mode_map[valid_modes] = mode;
2279 valid_modes++;
2280 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002281
2282 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2283 uinfo->count = 1;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002284 uinfo->value.enumerated.items = valid_modes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002285
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002286 if (uinfo->value.enumerated.item >= valid_modes)
2287 uinfo->value.enumerated.item = valid_modes - 1;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002288
2289 strcpy(uinfo->value.enumerated.name,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002290 mode_names[mode_map[uinfo->value.enumerated.item]]);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002291
2292 return 0;
2293}
2294
2295static int snd_asihpi_cmode_get(struct snd_kcontrol *kcontrol,
2296 struct snd_ctl_elem_value *ucontrol)
2297{
2298 u32 h_control = kcontrol->private_value;
2299 u16 mode;
2300
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002301 if (hpi_channel_mode_get(h_control, &mode))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002302 mode = 1;
2303
2304 ucontrol->value.enumerated.item[0] = mode - 1;
2305
2306 return 0;
2307}
2308
2309static int snd_asihpi_cmode_put(struct snd_kcontrol *kcontrol,
2310 struct snd_ctl_elem_value *ucontrol)
2311{
2312 int change;
2313 u32 h_control = kcontrol->private_value;
2314
2315 change = 1;
2316
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002317 hpi_handle_error(hpi_channel_mode_set(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002318 ucontrol->value.enumerated.item[0] + 1));
2319 return change;
2320}
2321
2322
2323static int __devinit snd_asihpi_cmode_add(struct snd_card_asihpi *asihpi,
2324 struct hpi_control *hpi_ctl)
2325{
2326 struct snd_card *card = asihpi->card;
2327 struct snd_kcontrol_new snd_control;
2328
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002329 asihpi_ctl_init(&snd_control, hpi_ctl, "Mode");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002330 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2331 snd_control.info = snd_asihpi_cmode_info;
2332 snd_control.get = snd_asihpi_cmode_get;
2333 snd_control.put = snd_asihpi_cmode_put;
2334
2335 return ctl_add(card, &snd_control, asihpi);
2336}
2337
2338/*------------------------------------------------------------
2339 Sampleclock source controls
2340 ------------------------------------------------------------*/
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002341static char *sampleclock_sources[MAX_CLOCKSOURCES] = {
2342 "N/A", "Local PLL", "Digital Sync", "Word External", "Word Header",
2343 "SMPTE", "Digital1", "Auto", "Network", "Invalid",
2344 "Prev Module",
2345 "Digital2", "Digital3", "Digital4", "Digital5",
2346 "Digital6", "Digital7", "Digital8"};
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002347
2348static int snd_asihpi_clksrc_info(struct snd_kcontrol *kcontrol,
2349 struct snd_ctl_elem_info *uinfo)
2350{
2351 struct snd_card_asihpi *asihpi =
2352 (struct snd_card_asihpi *)(kcontrol->private_data);
2353 struct clk_cache *clkcache = &asihpi->cc;
2354 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2355 uinfo->count = 1;
2356 uinfo->value.enumerated.items = clkcache->count;
2357
2358 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2359 uinfo->value.enumerated.item =
2360 uinfo->value.enumerated.items - 1;
2361
2362 strcpy(uinfo->value.enumerated.name,
2363 clkcache->s[uinfo->value.enumerated.item].name);
2364 return 0;
2365}
2366
2367static int snd_asihpi_clksrc_get(struct snd_kcontrol *kcontrol,
2368 struct snd_ctl_elem_value *ucontrol)
2369{
2370 struct snd_card_asihpi *asihpi =
2371 (struct snd_card_asihpi *)(kcontrol->private_data);
2372 struct clk_cache *clkcache = &asihpi->cc;
2373 u32 h_control = kcontrol->private_value;
2374 u16 source, srcindex = 0;
2375 int i;
2376
2377 ucontrol->value.enumerated.item[0] = 0;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002378 if (hpi_sample_clock_get_source(h_control, &source))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002379 source = 0;
2380
2381 if (source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002382 if (hpi_sample_clock_get_source_index(h_control, &srcindex))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002383 srcindex = 0;
2384
2385 for (i = 0; i < clkcache->count; i++)
2386 if ((clkcache->s[i].source == source) &&
2387 (clkcache->s[i].index == srcindex))
2388 break;
2389
2390 ucontrol->value.enumerated.item[0] = i;
2391
2392 return 0;
2393}
2394
2395static int snd_asihpi_clksrc_put(struct snd_kcontrol *kcontrol,
2396 struct snd_ctl_elem_value *ucontrol)
2397{
2398 struct snd_card_asihpi *asihpi =
2399 (struct snd_card_asihpi *)(kcontrol->private_data);
2400 struct clk_cache *clkcache = &asihpi->cc;
2401 int change, item;
2402 u32 h_control = kcontrol->private_value;
2403
2404 change = 1;
2405 item = ucontrol->value.enumerated.item[0];
2406 if (item >= clkcache->count)
2407 item = clkcache->count-1;
2408
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002409 hpi_handle_error(hpi_sample_clock_set_source(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002410 h_control, clkcache->s[item].source));
2411
2412 if (clkcache->s[item].source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002413 hpi_handle_error(hpi_sample_clock_set_source_index(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002414 h_control, clkcache->s[item].index));
2415 return change;
2416}
2417
2418/*------------------------------------------------------------
2419 Clkrate controls
2420 ------------------------------------------------------------*/
2421/* Need to change this to enumerated control with list of rates */
2422static int snd_asihpi_clklocal_info(struct snd_kcontrol *kcontrol,
2423 struct snd_ctl_elem_info *uinfo)
2424{
2425 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2426 uinfo->count = 1;
2427 uinfo->value.integer.min = 8000;
2428 uinfo->value.integer.max = 192000;
2429 uinfo->value.integer.step = 100;
2430
2431 return 0;
2432}
2433
2434static int snd_asihpi_clklocal_get(struct snd_kcontrol *kcontrol,
2435 struct snd_ctl_elem_value *ucontrol)
2436{
2437 u32 h_control = kcontrol->private_value;
2438 u32 rate;
2439 u16 e;
2440
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002441 e = hpi_sample_clock_get_local_rate(h_control, &rate);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002442 if (!e)
2443 ucontrol->value.integer.value[0] = rate;
2444 else
2445 ucontrol->value.integer.value[0] = 0;
2446 return 0;
2447}
2448
2449static int snd_asihpi_clklocal_put(struct snd_kcontrol *kcontrol,
2450 struct snd_ctl_elem_value *ucontrol)
2451{
2452 int change;
2453 u32 h_control = kcontrol->private_value;
2454
2455 /* change = asihpi->mixer_clkrate[addr][0] != left ||
2456 asihpi->mixer_clkrate[addr][1] != right;
2457 */
2458 change = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002459 hpi_handle_error(hpi_sample_clock_set_local_rate(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002460 ucontrol->value.integer.value[0]));
2461 return change;
2462}
2463
2464static int snd_asihpi_clkrate_info(struct snd_kcontrol *kcontrol,
2465 struct snd_ctl_elem_info *uinfo)
2466{
2467 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2468 uinfo->count = 1;
2469 uinfo->value.integer.min = 8000;
2470 uinfo->value.integer.max = 192000;
2471 uinfo->value.integer.step = 100;
2472
2473 return 0;
2474}
2475
2476static int snd_asihpi_clkrate_get(struct snd_kcontrol *kcontrol,
2477 struct snd_ctl_elem_value *ucontrol)
2478{
2479 u32 h_control = kcontrol->private_value;
2480 u32 rate;
2481 u16 e;
2482
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002483 e = hpi_sample_clock_get_sample_rate(h_control, &rate);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002484 if (!e)
2485 ucontrol->value.integer.value[0] = rate;
2486 else
2487 ucontrol->value.integer.value[0] = 0;
2488 return 0;
2489}
2490
2491static int __devinit snd_asihpi_sampleclock_add(struct snd_card_asihpi *asihpi,
2492 struct hpi_control *hpi_ctl)
2493{
2494 struct snd_card *card = asihpi->card;
2495 struct snd_kcontrol_new snd_control;
2496
2497 struct clk_cache *clkcache = &asihpi->cc;
2498 u32 hSC = hpi_ctl->h_control;
2499 int has_aes_in = 0;
2500 int i, j;
2501 u16 source;
2502
2503 snd_control.private_value = hpi_ctl->h_control;
2504
2505 clkcache->has_local = 0;
2506
2507 for (i = 0; i <= HPI_SAMPLECLOCK_SOURCE_LAST; i++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002508 if (hpi_sample_clock_query_source(hSC,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002509 i, &source))
2510 break;
2511 clkcache->s[i].source = source;
2512 clkcache->s[i].index = 0;
2513 clkcache->s[i].name = sampleclock_sources[source];
2514 if (source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
2515 has_aes_in = 1;
2516 if (source == HPI_SAMPLECLOCK_SOURCE_LOCAL)
2517 clkcache->has_local = 1;
2518 }
2519 if (has_aes_in)
2520 /* already will have picked up index 0 above */
2521 for (j = 1; j < 8; j++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002522 if (hpi_sample_clock_query_source_index(hSC,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002523 j, HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT,
2524 &source))
2525 break;
2526 clkcache->s[i].source =
2527 HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT;
2528 clkcache->s[i].index = j;
2529 clkcache->s[i].name = sampleclock_sources[
2530 j+HPI_SAMPLECLOCK_SOURCE_LAST];
2531 i++;
2532 }
2533 clkcache->count = i;
2534
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002535 asihpi_ctl_init(&snd_control, hpi_ctl, "Source");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002536 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE ;
2537 snd_control.info = snd_asihpi_clksrc_info;
2538 snd_control.get = snd_asihpi_clksrc_get;
2539 snd_control.put = snd_asihpi_clksrc_put;
2540 if (ctl_add(card, &snd_control, asihpi) < 0)
2541 return -EINVAL;
2542
2543
2544 if (clkcache->has_local) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002545 asihpi_ctl_init(&snd_control, hpi_ctl, "Localrate");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002546 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE ;
2547 snd_control.info = snd_asihpi_clklocal_info;
2548 snd_control.get = snd_asihpi_clklocal_get;
2549 snd_control.put = snd_asihpi_clklocal_put;
2550
2551
2552 if (ctl_add(card, &snd_control, asihpi) < 0)
2553 return -EINVAL;
2554 }
2555
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002556 asihpi_ctl_init(&snd_control, hpi_ctl, "Rate");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002557 snd_control.access =
2558 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
2559 snd_control.info = snd_asihpi_clkrate_info;
2560 snd_control.get = snd_asihpi_clkrate_get;
2561
2562 return ctl_add(card, &snd_control, asihpi);
2563}
2564/*------------------------------------------------------------
2565 Mixer
2566 ------------------------------------------------------------*/
2567
2568static int __devinit snd_card_asihpi_mixer_new(struct snd_card_asihpi *asihpi)
2569{
2570 struct snd_card *card = asihpi->card;
2571 unsigned int idx = 0;
2572 unsigned int subindex = 0;
2573 int err;
2574 struct hpi_control hpi_ctl, prev_ctl;
2575
2576 if (snd_BUG_ON(!asihpi))
2577 return -EINVAL;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002578 strcpy(card->mixername, "Asihpi Mixer");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002579
2580 err =
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002581 hpi_mixer_open(asihpi->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002582 &asihpi->h_mixer);
2583 hpi_handle_error(err);
2584 if (err)
2585 return -err;
2586
Takashi Iwai21896bc2010-06-02 12:08:37 +02002587 memset(&prev_ctl, 0, sizeof(prev_ctl));
2588 prev_ctl.control_type = -1;
2589
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002590 for (idx = 0; idx < 2000; idx++) {
2591 err = hpi_mixer_get_control_by_index(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002592 asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002593 idx,
2594 &hpi_ctl.src_node_type,
2595 &hpi_ctl.src_node_index,
2596 &hpi_ctl.dst_node_type,
2597 &hpi_ctl.dst_node_index,
2598 &hpi_ctl.control_type,
2599 &hpi_ctl.h_control);
2600 if (err) {
2601 if (err == HPI_ERROR_CONTROL_DISABLED) {
2602 if (mixer_dump)
2603 snd_printk(KERN_INFO
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002604 "Disabled HPI Control(%d)\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002605 idx);
2606 continue;
2607 } else
2608 break;
2609
2610 }
2611
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12002612 hpi_ctl.src_node_type -= HPI_SOURCENODE_NONE;
2613 hpi_ctl.dst_node_type -= HPI_DESTNODE_NONE;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002614
2615 /* ASI50xx in SSX mode has multiple meters on the same node.
2616 Use subindex to create distinct ALSA controls
2617 for any duplicated controls.
2618 */
2619 if ((hpi_ctl.control_type == prev_ctl.control_type) &&
2620 (hpi_ctl.src_node_type == prev_ctl.src_node_type) &&
2621 (hpi_ctl.src_node_index == prev_ctl.src_node_index) &&
2622 (hpi_ctl.dst_node_type == prev_ctl.dst_node_type) &&
2623 (hpi_ctl.dst_node_index == prev_ctl.dst_node_index))
2624 subindex++;
2625 else
2626 subindex = 0;
2627
2628 prev_ctl = hpi_ctl;
2629
2630 switch (hpi_ctl.control_type) {
2631 case HPI_CONTROL_VOLUME:
2632 err = snd_asihpi_volume_add(asihpi, &hpi_ctl);
2633 break;
2634 case HPI_CONTROL_LEVEL:
2635 err = snd_asihpi_level_add(asihpi, &hpi_ctl);
2636 break;
2637 case HPI_CONTROL_MULTIPLEXER:
2638 err = snd_asihpi_mux_add(asihpi, &hpi_ctl);
2639 break;
2640 case HPI_CONTROL_CHANNEL_MODE:
2641 err = snd_asihpi_cmode_add(asihpi, &hpi_ctl);
2642 break;
2643 case HPI_CONTROL_METER:
2644 err = snd_asihpi_meter_add(asihpi, &hpi_ctl, subindex);
2645 break;
2646 case HPI_CONTROL_SAMPLECLOCK:
2647 err = snd_asihpi_sampleclock_add(
2648 asihpi, &hpi_ctl);
2649 break;
2650 case HPI_CONTROL_CONNECTION: /* ignore these */
2651 continue;
2652 case HPI_CONTROL_TUNER:
2653 err = snd_asihpi_tuner_add(asihpi, &hpi_ctl);
2654 break;
2655 case HPI_CONTROL_AESEBU_TRANSMITTER:
2656 err = snd_asihpi_aesebu_tx_add(asihpi, &hpi_ctl);
2657 break;
2658 case HPI_CONTROL_AESEBU_RECEIVER:
2659 err = snd_asihpi_aesebu_rx_add(asihpi, &hpi_ctl);
2660 break;
2661 case HPI_CONTROL_VOX:
2662 case HPI_CONTROL_BITSTREAM:
2663 case HPI_CONTROL_MICROPHONE:
2664 case HPI_CONTROL_PARAMETRIC_EQ:
2665 case HPI_CONTROL_COMPANDER:
2666 default:
2667 if (mixer_dump)
2668 snd_printk(KERN_INFO
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002669 "Untranslated HPI Control"
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002670 "(%d) %d %d %d %d %d\n",
2671 idx,
2672 hpi_ctl.control_type,
2673 hpi_ctl.src_node_type,
2674 hpi_ctl.src_node_index,
2675 hpi_ctl.dst_node_type,
2676 hpi_ctl.dst_node_index);
2677 continue;
2678 };
2679 if (err < 0)
2680 return err;
2681 }
2682 if (HPI_ERROR_INVALID_OBJ_INDEX != err)
2683 hpi_handle_error(err);
2684
2685 snd_printk(KERN_INFO "%d mixer controls found\n", idx);
2686
2687 return 0;
2688}
2689
2690/*------------------------------------------------------------
2691 /proc interface
2692 ------------------------------------------------------------*/
2693
2694static void
2695snd_asihpi_proc_read(struct snd_info_entry *entry,
2696 struct snd_info_buffer *buffer)
2697{
2698 struct snd_card_asihpi *asihpi = entry->private_data;
2699 u16 version;
2700 u32 h_control;
2701 u32 rate = 0;
2702 u16 source = 0;
2703 int err;
2704
2705 snd_iprintf(buffer, "ASIHPI driver proc file\n");
2706 snd_iprintf(buffer,
2707 "adapter ID=%4X\n_index=%d\n"
2708 "num_outstreams=%d\n_num_instreams=%d\n",
2709 asihpi->type, asihpi->adapter_index,
2710 asihpi->num_outstreams, asihpi->num_instreams);
2711
2712 version = asihpi->version;
2713 snd_iprintf(buffer,
2714 "serial#=%d\n_hw version %c%d\nDSP code version %03d\n",
2715 asihpi->serial_number, ((version >> 3) & 0xf) + 'A',
2716 version & 0x7,
2717 ((version >> 13) * 100) + ((version >> 7) & 0x3f));
2718
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002719 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002720 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
2721 HPI_CONTROL_SAMPLECLOCK, &h_control);
2722
2723 if (!err) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002724 err = hpi_sample_clock_get_sample_rate(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002725 h_control, &rate);
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002726 err += hpi_sample_clock_get_source(h_control, &source);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002727
2728 if (!err)
2729 snd_iprintf(buffer, "sample_clock=%d_hz, source %s\n",
2730 rate, sampleclock_sources[source]);
2731 }
2732
2733}
2734
2735
2736static void __devinit snd_asihpi_proc_init(struct snd_card_asihpi *asihpi)
2737{
2738 struct snd_info_entry *entry;
2739
2740 if (!snd_card_proc_new(asihpi->card, "info", &entry))
2741 snd_info_set_text_ops(entry, asihpi, snd_asihpi_proc_read);
2742}
2743
2744/*------------------------------------------------------------
2745 HWDEP
2746 ------------------------------------------------------------*/
2747
2748static int snd_asihpi_hpi_open(struct snd_hwdep *hw, struct file *file)
2749{
2750 if (enable_hpi_hwdep)
2751 return 0;
2752 else
2753 return -ENODEV;
2754
2755}
2756
2757static int snd_asihpi_hpi_release(struct snd_hwdep *hw, struct file *file)
2758{
2759 if (enable_hpi_hwdep)
2760 return asihpi_hpi_release(file);
2761 else
2762 return -ENODEV;
2763}
2764
2765static int snd_asihpi_hpi_ioctl(struct snd_hwdep *hw, struct file *file,
2766 unsigned int cmd, unsigned long arg)
2767{
2768 if (enable_hpi_hwdep)
2769 return asihpi_hpi_ioctl(file, cmd, arg);
2770 else
2771 return -ENODEV;
2772}
2773
2774
2775/* results in /dev/snd/hwC#D0 file for each card with index #
2776 also /proc/asound/hwdep will contain '#-00: asihpi (HPI) for each card'
2777*/
2778static int __devinit snd_asihpi_hpi_new(struct snd_card_asihpi *asihpi,
2779 int device, struct snd_hwdep **rhwdep)
2780{
2781 struct snd_hwdep *hw;
2782 int err;
2783
2784 if (rhwdep)
2785 *rhwdep = NULL;
2786 err = snd_hwdep_new(asihpi->card, "HPI", device, &hw);
2787 if (err < 0)
2788 return err;
2789 strcpy(hw->name, "asihpi (HPI)");
2790 hw->iface = SNDRV_HWDEP_IFACE_LAST;
2791 hw->ops.open = snd_asihpi_hpi_open;
2792 hw->ops.ioctl = snd_asihpi_hpi_ioctl;
2793 hw->ops.release = snd_asihpi_hpi_release;
2794 hw->private_data = asihpi;
2795 if (rhwdep)
2796 *rhwdep = hw;
2797 return 0;
2798}
2799
2800/*------------------------------------------------------------
2801 CARD
2802 ------------------------------------------------------------*/
2803static int __devinit snd_asihpi_probe(struct pci_dev *pci_dev,
2804 const struct pci_device_id *pci_id)
2805{
2806 int err;
2807
2808 u16 version;
2809 int pcm_substreams;
2810
2811 struct hpi_adapter *hpi_card;
2812 struct snd_card *card;
2813 struct snd_card_asihpi *asihpi;
2814
2815 u32 h_control;
2816 u32 h_stream;
2817
2818 static int dev;
2819 if (dev >= SNDRV_CARDS)
2820 return -ENODEV;
2821
2822 /* Should this be enable[hpi_card->index] ? */
2823 if (!enable[dev]) {
2824 dev++;
2825 return -ENOENT;
2826 }
2827
2828 err = asihpi_adapter_probe(pci_dev, pci_id);
2829 if (err < 0)
2830 return err;
2831
2832 hpi_card = pci_get_drvdata(pci_dev);
2833 /* first try to give the card the same index as its hardware index */
2834 err = snd_card_create(hpi_card->index,
2835 id[hpi_card->index], THIS_MODULE,
2836 sizeof(struct snd_card_asihpi),
2837 &card);
2838 if (err < 0) {
2839 /* if that fails, try the default index==next available */
2840 err =
2841 snd_card_create(index[dev], id[dev],
2842 THIS_MODULE,
2843 sizeof(struct snd_card_asihpi),
2844 &card);
2845 if (err < 0)
2846 return err;
2847 snd_printk(KERN_WARNING
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002848 "**** WARNING **** Adapter index %d->ALSA index %d\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002849 hpi_card->index, card->number);
2850 }
2851
Eliot Blennerhassett12253672011-02-10 17:26:10 +13002852 snd_card_set_dev(card, &pci_dev->dev);
2853
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002854 asihpi = (struct snd_card_asihpi *) card->private_data;
2855 asihpi->card = card;
Eliot Blennerhassett12253672011-02-10 17:26:10 +13002856 asihpi->pci = pci_dev;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002857 asihpi->adapter_index = hpi_card->index;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002858 hpi_handle_error(hpi_adapter_get_info(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002859 asihpi->adapter_index,
2860 &asihpi->num_outstreams,
2861 &asihpi->num_instreams,
2862 &asihpi->version,
2863 &asihpi->serial_number, &asihpi->type));
2864
2865 version = asihpi->version;
2866 snd_printk(KERN_INFO "adapter ID=%4X index=%d num_outstreams=%d "
2867 "num_instreams=%d S/N=%d\n"
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002868 "Hw Version %c%d DSP code version %03d\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002869 asihpi->type, asihpi->adapter_index,
2870 asihpi->num_outstreams,
2871 asihpi->num_instreams, asihpi->serial_number,
2872 ((version >> 3) & 0xf) + 'A',
2873 version & 0x7,
2874 ((version >> 13) * 100) + ((version >> 7) & 0x3f));
2875
2876 pcm_substreams = asihpi->num_outstreams;
2877 if (pcm_substreams < asihpi->num_instreams)
2878 pcm_substreams = asihpi->num_instreams;
2879
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002880 err = hpi_adapter_get_property(asihpi->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002881 HPI_ADAPTER_PROPERTY_CAPS1,
2882 NULL, &asihpi->support_grouping);
2883 if (err)
2884 asihpi->support_grouping = 0;
2885
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002886 err = hpi_adapter_get_property(asihpi->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002887 HPI_ADAPTER_PROPERTY_CAPS2,
2888 &asihpi->support_mrx, NULL);
2889 if (err)
2890 asihpi->support_mrx = 0;
2891
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002892 err = hpi_adapter_get_property(asihpi->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002893 HPI_ADAPTER_PROPERTY_INTERVAL,
2894 NULL, &asihpi->update_interval_frames);
2895 if (err)
2896 asihpi->update_interval_frames = 512;
2897
Eliot Blennerhassett26aebef2011-03-25 15:25:47 +13002898 if (!asihpi->support_mmap)
2899 asihpi->update_interval_frames *= 2;
2900
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002901 hpi_handle_error(hpi_instream_open(asihpi->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002902 0, &h_stream));
2903
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002904 err = hpi_instream_host_buffer_free(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002905 asihpi->support_mmap = (!err);
2906
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002907 hpi_handle_error(hpi_instream_close(h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002908
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002909 err = hpi_adapter_get_property(asihpi->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002910 HPI_ADAPTER_PROPERTY_CURCHANNELS,
2911 &asihpi->in_max_chans, &asihpi->out_max_chans);
2912 if (err) {
2913 asihpi->in_max_chans = 2;
2914 asihpi->out_max_chans = 2;
2915 }
2916
2917 snd_printk(KERN_INFO "supports mmap:%d grouping:%d mrx:%d\n",
2918 asihpi->support_mmap,
2919 asihpi->support_grouping,
2920 asihpi->support_mrx
2921 );
2922
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002923 err = snd_card_asihpi_pcm_new(asihpi, 0, pcm_substreams);
2924 if (err < 0) {
2925 snd_printk(KERN_ERR "pcm_new failed\n");
2926 goto __nodev;
2927 }
2928 err = snd_card_asihpi_mixer_new(asihpi);
2929 if (err < 0) {
2930 snd_printk(KERN_ERR "mixer_new failed\n");
2931 goto __nodev;
2932 }
2933
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002934 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002935 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
2936 HPI_CONTROL_SAMPLECLOCK, &h_control);
2937
2938 if (!err)
2939 err = hpi_sample_clock_set_local_rate(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002940 h_control, adapter_fs);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002941
2942 snd_asihpi_proc_init(asihpi);
2943
2944 /* always create, can be enabled or disabled dynamically
2945 by enable_hwdep module param*/
2946 snd_asihpi_hpi_new(asihpi, 0, NULL);
2947
2948 if (asihpi->support_mmap)
2949 strcpy(card->driver, "ASIHPI-MMAP");
2950 else
2951 strcpy(card->driver, "ASIHPI");
2952
2953 sprintf(card->shortname, "AudioScience ASI%4X", asihpi->type);
2954 sprintf(card->longname, "%s %i",
2955 card->shortname, asihpi->adapter_index);
2956 err = snd_card_register(card);
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13002957
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002958 if (!err) {
2959 hpi_card->snd_card_asihpi = card;
2960 dev++;
2961 return 0;
2962 }
2963__nodev:
2964 snd_card_free(card);
2965 snd_printk(KERN_ERR "snd_asihpi_probe error %d\n", err);
2966 return err;
2967
2968}
2969
2970static void __devexit snd_asihpi_remove(struct pci_dev *pci_dev)
2971{
2972 struct hpi_adapter *hpi_card = pci_get_drvdata(pci_dev);
2973
2974 snd_card_free(hpi_card->snd_card_asihpi);
2975 hpi_card->snd_card_asihpi = NULL;
2976 asihpi_adapter_remove(pci_dev);
2977}
2978
2979static DEFINE_PCI_DEVICE_TABLE(asihpi_pci_tbl) = {
2980 {HPI_PCI_VENDOR_ID_TI, HPI_PCI_DEV_ID_DSP6205,
2981 HPI_PCI_VENDOR_ID_AUDIOSCIENCE, PCI_ANY_ID, 0, 0,
2982 (kernel_ulong_t)HPI_6205},
2983 {HPI_PCI_VENDOR_ID_TI, HPI_PCI_DEV_ID_PCI2040,
2984 HPI_PCI_VENDOR_ID_AUDIOSCIENCE, PCI_ANY_ID, 0, 0,
2985 (kernel_ulong_t)HPI_6000},
2986 {0,}
2987};
2988MODULE_DEVICE_TABLE(pci, asihpi_pci_tbl);
2989
2990static struct pci_driver driver = {
2991 .name = "asihpi",
2992 .id_table = asihpi_pci_tbl,
2993 .probe = snd_asihpi_probe,
2994 .remove = __devexit_p(snd_asihpi_remove),
2995#ifdef CONFIG_PM
2996/* .suspend = snd_asihpi_suspend,
2997 .resume = snd_asihpi_resume, */
2998#endif
2999};
3000
3001static int __init snd_asihpi_init(void)
3002{
3003 asihpi_init();
3004 return pci_register_driver(&driver);
3005}
3006
3007static void __exit snd_asihpi_exit(void)
3008{
3009
3010 pci_unregister_driver(&driver);
3011 asihpi_exit();
3012}
3013
3014module_init(snd_asihpi_init)
3015module_exit(snd_asihpi_exit)
3016