blob: 434342f874ff63534963935a629351281e02df6e [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 Blennerhassett719f82d2010-04-21 18:17:39 +0200168 struct snd_pcm_substream *substream;
169 u32 h_stream;
170 struct hpi_format format;
171};
172
173/* universal stream verbs work with out or in stream handles */
174
175/* Functions to allow driver to give a buffer to HPI for busmastering */
176
177static u16 hpi_stream_host_buffer_attach(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200178 u32 h_stream, /* handle to outstream. */
179 u32 size_in_bytes, /* size in bytes of bus mastering buffer */
180 u32 pci_address
181)
182{
183 struct hpi_message hm;
184 struct hpi_response hr;
185 unsigned int obj = hpi_handle_object(h_stream);
186
187 if (!h_stream)
188 return HPI_ERROR_INVALID_OBJ;
189 hpi_init_message_response(&hm, &hr, obj,
190 obj == HPI_OBJ_OSTREAM ?
191 HPI_OSTREAM_HOSTBUFFER_ALLOC :
192 HPI_ISTREAM_HOSTBUFFER_ALLOC);
193
194 hpi_handle_to_indexes(h_stream, &hm.adapter_index,
195 &hm.obj_index);
196
197 hm.u.d.u.buffer.buffer_size = size_in_bytes;
198 hm.u.d.u.buffer.pci_address = pci_address;
199 hm.u.d.u.buffer.command = HPI_BUFFER_CMD_INTERNAL_GRANTADAPTER;
200 hpi_send_recv(&hm, &hr);
201 return hr.error;
202}
203
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300204static u16 hpi_stream_host_buffer_detach(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200205{
206 struct hpi_message hm;
207 struct hpi_response hr;
208 unsigned int obj = hpi_handle_object(h_stream);
209
210 if (!h_stream)
211 return HPI_ERROR_INVALID_OBJ;
212
213 hpi_init_message_response(&hm, &hr, obj,
214 obj == HPI_OBJ_OSTREAM ?
215 HPI_OSTREAM_HOSTBUFFER_FREE :
216 HPI_ISTREAM_HOSTBUFFER_FREE);
217
218 hpi_handle_to_indexes(h_stream, &hm.adapter_index,
219 &hm.obj_index);
220 hm.u.d.u.buffer.command = HPI_BUFFER_CMD_INTERNAL_REVOKEADAPTER;
221 hpi_send_recv(&hm, &hr);
222 return hr.error;
223}
224
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300225static inline u16 hpi_stream_start(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200226{
227 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300228 return hpi_outstream_start(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200229 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300230 return hpi_instream_start(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200231}
232
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300233static inline u16 hpi_stream_stop(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200234{
235 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300236 return hpi_outstream_stop(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200237 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300238 return hpi_instream_stop(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200239}
240
241static inline u16 hpi_stream_get_info_ex(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200242 u32 h_stream,
243 u16 *pw_state,
244 u32 *pbuffer_size,
245 u32 *pdata_in_buffer,
246 u32 *psample_count,
247 u32 *pauxiliary_data
248)
249{
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300250 u16 e;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200251 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300252 e = hpi_outstream_get_info_ex(h_stream, pw_state,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200253 pbuffer_size, pdata_in_buffer,
254 psample_count, pauxiliary_data);
255 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300256 e = hpi_instream_get_info_ex(h_stream, pw_state,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200257 pbuffer_size, pdata_in_buffer,
258 psample_count, pauxiliary_data);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300259 return e;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200260}
261
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300262static inline u16 hpi_stream_group_add(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200263 u32 h_master,
264 u32 h_stream)
265{
266 if (hpi_handle_object(h_master) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300267 return hpi_outstream_group_add(h_master, h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200268 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300269 return hpi_instream_group_add(h_master, h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200270}
271
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300272static inline u16 hpi_stream_group_reset(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200273{
274 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300275 return hpi_outstream_group_reset(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200276 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300277 return hpi_instream_group_reset(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200278}
279
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300280static inline u16 hpi_stream_group_get_map(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200281 u32 h_stream, u32 *mo, u32 *mi)
282{
283 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300284 return hpi_outstream_group_get_map(h_stream, mo, mi);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200285 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300286 return hpi_instream_group_get_map(h_stream, mo, mi);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200287}
288
289static u16 handle_error(u16 err, int line, char *filename)
290{
291 if (err)
292 printk(KERN_WARNING
293 "in file %s, line %d: HPI error %d\n",
294 filename, line, err);
295 return err;
296}
297
298#define hpi_handle_error(x) handle_error(x, __LINE__, __FILE__)
299
300/***************************** GENERAL PCM ****************/
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200301
302static void print_hwparams(struct snd_pcm_substream *substream,
303 struct snd_pcm_hw_params *p)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200304{
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200305 DEBUG_NAME(substream, name);
306 snd_printd("%s HWPARAMS\n", name);
307 snd_printd(" samplerate %d Hz\n", params_rate(p));
308 snd_printd(" channels %d\n", params_channels(p));
309 snd_printd(" format %d\n", params_format(p));
310 snd_printd(" subformat %d\n", params_subformat(p));
311 snd_printd(" buffer %d B\n", params_buffer_bytes(p));
312 snd_printd(" period %d B\n", params_period_bytes(p));
313 snd_printd(" access %d\n", params_access(p));
314 snd_printd(" period_size %d\n", params_period_size(p));
315 snd_printd(" periods %d\n", params_periods(p));
316 snd_printd(" buffer_size %d\n", params_buffer_size(p));
317 snd_printd(" %d B/s\n", params_rate(p) *
318 params_channels(p) *
319 snd_pcm_format_width(params_format(p)) / 8);
320
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200321}
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200322
323static snd_pcm_format_t hpi_to_alsa_formats[] = {
324 -1, /* INVALID */
325 SNDRV_PCM_FORMAT_U8, /* HPI_FORMAT_PCM8_UNSIGNED 1 */
326 SNDRV_PCM_FORMAT_S16, /* HPI_FORMAT_PCM16_SIGNED 2 */
327 -1, /* HPI_FORMAT_MPEG_L1 3 */
328 SNDRV_PCM_FORMAT_MPEG, /* HPI_FORMAT_MPEG_L2 4 */
329 SNDRV_PCM_FORMAT_MPEG, /* HPI_FORMAT_MPEG_L3 5 */
330 -1, /* HPI_FORMAT_DOLBY_AC2 6 */
331 -1, /* HPI_FORMAT_DOLBY_AC3 7 */
332 SNDRV_PCM_FORMAT_S16_BE,/* HPI_FORMAT_PCM16_BIGENDIAN 8 */
333 -1, /* HPI_FORMAT_AA_TAGIT1_HITS 9 */
334 -1, /* HPI_FORMAT_AA_TAGIT1_INSERTS 10 */
335 SNDRV_PCM_FORMAT_S32, /* HPI_FORMAT_PCM32_SIGNED 11 */
336 -1, /* HPI_FORMAT_RAW_BITSTREAM 12 */
337 -1, /* HPI_FORMAT_AA_TAGIT1_HITS_EX1 13 */
338 SNDRV_PCM_FORMAT_FLOAT, /* HPI_FORMAT_PCM32_FLOAT 14 */
339#if 1
340 /* ALSA can't handle 3 byte sample size together with power-of-2
341 * constraint on buffer_bytes, so disable this format
342 */
343 -1
344#else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300345 /* SNDRV_PCM_FORMAT_S24_3LE */ /* HPI_FORMAT_PCM24_SIGNED 15 */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200346#endif
347};
348
349
350static int snd_card_asihpi_format_alsa2hpi(snd_pcm_format_t alsa_format,
351 u16 *hpi_format)
352{
353 u16 format;
354
355 for (format = HPI_FORMAT_PCM8_UNSIGNED;
356 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
357 if (hpi_to_alsa_formats[format] == alsa_format) {
358 *hpi_format = format;
359 return 0;
360 }
361 }
362
363 snd_printd(KERN_WARNING "failed match for alsa format %d\n",
364 alsa_format);
365 *hpi_format = 0;
366 return -EINVAL;
367}
368
369static void snd_card_asihpi_pcm_samplerates(struct snd_card_asihpi *asihpi,
370 struct snd_pcm_hardware *pcmhw)
371{
372 u16 err;
373 u32 h_control;
374 u32 sample_rate;
375 int idx;
376 unsigned int rate_min = 200000;
377 unsigned int rate_max = 0;
378 unsigned int rates = 0;
379
380 if (asihpi->support_mrx) {
381 rates |= SNDRV_PCM_RATE_CONTINUOUS;
382 rates |= SNDRV_PCM_RATE_8000_96000;
383 rate_min = 8000;
384 rate_max = 100000;
385 } else {
386 /* on cards without SRC,
387 valid rates are determined by sampleclock */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300388 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200389 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
390 HPI_CONTROL_SAMPLECLOCK, &h_control);
391 if (err) {
392 snd_printk(KERN_ERR
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300393 "No local sampleclock, err %d\n", err);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200394 }
395
Eliot Blennerhassett7bf76c32011-03-25 15:25:46 +1300396 for (idx = -1; idx < 100; idx++) {
397 if (idx == -1) {
398 if (hpi_sample_clock_get_sample_rate(h_control,
399 &sample_rate))
400 continue;
401 } else if (hpi_sample_clock_query_local_rate(h_control,
402 idx, &sample_rate)) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200403 break;
404 }
405
406 rate_min = min(rate_min, sample_rate);
407 rate_max = max(rate_max, sample_rate);
408
409 switch (sample_rate) {
410 case 5512:
411 rates |= SNDRV_PCM_RATE_5512;
412 break;
413 case 8000:
414 rates |= SNDRV_PCM_RATE_8000;
415 break;
416 case 11025:
417 rates |= SNDRV_PCM_RATE_11025;
418 break;
419 case 16000:
420 rates |= SNDRV_PCM_RATE_16000;
421 break;
422 case 22050:
423 rates |= SNDRV_PCM_RATE_22050;
424 break;
425 case 32000:
426 rates |= SNDRV_PCM_RATE_32000;
427 break;
428 case 44100:
429 rates |= SNDRV_PCM_RATE_44100;
430 break;
431 case 48000:
432 rates |= SNDRV_PCM_RATE_48000;
433 break;
434 case 64000:
435 rates |= SNDRV_PCM_RATE_64000;
436 break;
437 case 88200:
438 rates |= SNDRV_PCM_RATE_88200;
439 break;
440 case 96000:
441 rates |= SNDRV_PCM_RATE_96000;
442 break;
443 case 176400:
444 rates |= SNDRV_PCM_RATE_176400;
445 break;
446 case 192000:
447 rates |= SNDRV_PCM_RATE_192000;
448 break;
449 default: /* some other rate */
450 rates |= SNDRV_PCM_RATE_KNOT;
451 }
452 }
453 }
454
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200455 pcmhw->rates = rates;
456 pcmhw->rate_min = rate_min;
457 pcmhw->rate_max = rate_max;
458}
459
460static int snd_card_asihpi_pcm_hw_params(struct snd_pcm_substream *substream,
461 struct snd_pcm_hw_params *params)
462{
463 struct snd_pcm_runtime *runtime = substream->runtime;
464 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
465 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
466 int err;
467 u16 format;
Kulikov Vasiliy315e8f72010-07-15 22:48:19 +0400468 int width;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200469 unsigned int bytes_per_sec;
470
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200471 print_hwparams(substream, params);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200472 err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
473 if (err < 0)
474 return err;
475 err = snd_card_asihpi_format_alsa2hpi(params_format(params), &format);
476 if (err)
477 return err;
478
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200479 hpi_handle_error(hpi_format_create(&dpcm->format,
480 params_channels(params),
481 format, params_rate(params), 0, 0));
482
483 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300484 if (hpi_instream_reset(dpcm->h_stream) != 0)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200485 return -EINVAL;
486
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300487 if (hpi_instream_set_format(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200488 dpcm->h_stream, &dpcm->format) != 0)
489 return -EINVAL;
490 }
491
492 dpcm->hpi_buffer_attached = 0;
493 if (card->support_mmap) {
494
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300495 err = hpi_stream_host_buffer_attach(dpcm->h_stream,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200496 params_buffer_bytes(params), runtime->dma_addr);
497 if (err == 0) {
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300498 snd_printdd(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200499 "stream_host_buffer_attach succeeded %u %lu\n",
500 params_buffer_bytes(params),
501 (unsigned long)runtime->dma_addr);
502 } else {
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300503 snd_printd("stream_host_buffer_attach error %d\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200504 err);
505 return -ENOMEM;
506 }
507
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300508 err = hpi_stream_get_info_ex(dpcm->h_stream, NULL,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200509 &dpcm->hpi_buffer_attached,
510 NULL, NULL, NULL);
511
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300512 snd_printdd("stream_host_buffer_attach status 0x%x\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200513 dpcm->hpi_buffer_attached);
514 }
515 bytes_per_sec = params_rate(params) * params_channels(params);
Kulikov Vasiliy315e8f72010-07-15 22:48:19 +0400516 width = snd_pcm_format_width(params_format(params));
517 bytes_per_sec *= width;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200518 bytes_per_sec /= 8;
Kulikov Vasiliy315e8f72010-07-15 22:48:19 +0400519 if (width < 0 || bytes_per_sec == 0)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200520 return -EINVAL;
521
522 dpcm->bytes_per_sec = bytes_per_sec;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300523 dpcm->buffer_bytes = params_buffer_bytes(params);
524 dpcm->period_bytes = params_period_bytes(params);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200525
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200526 return 0;
527}
528
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300529static int
530snd_card_asihpi_hw_free(struct snd_pcm_substream *substream)
531{
532 struct snd_pcm_runtime *runtime = substream->runtime;
533 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
534 if (dpcm->hpi_buffer_attached)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300535 hpi_stream_host_buffer_detach(dpcm->h_stream);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300536
537 snd_pcm_lib_free_pages(substream);
538 return 0;
539}
540
541static void snd_card_asihpi_runtime_free(struct snd_pcm_runtime *runtime)
542{
543 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
544 kfree(dpcm);
545}
546
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200547static void snd_card_asihpi_pcm_timer_start(struct snd_pcm_substream *
548 substream)
549{
550 struct snd_pcm_runtime *runtime = substream->runtime;
551 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
552 int expiry;
553
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300554 expiry = HZ / 200;
555 /*? (dpcm->period_bytes * HZ / dpcm->bytes_per_sec); */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300556 expiry = max(expiry, 1); /* don't let it be zero! */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200557 dpcm->timer.expires = jiffies + expiry;
558 dpcm->respawn_timer = 1;
559 add_timer(&dpcm->timer);
560}
561
562static void snd_card_asihpi_pcm_timer_stop(struct snd_pcm_substream *substream)
563{
564 struct snd_pcm_runtime *runtime = substream->runtime;
565 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
566
567 dpcm->respawn_timer = 0;
568 del_timer(&dpcm->timer);
569}
570
571static int snd_card_asihpi_trigger(struct snd_pcm_substream *substream,
572 int cmd)
573{
574 struct snd_card_asihpi_pcm *dpcm = substream->runtime->private_data;
575 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
576 struct snd_pcm_substream *s;
577 u16 e;
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200578 DEBUG_NAME(substream, name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200579
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200580 snd_printdd("%s trigger\n", name);
581
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200582 switch (cmd) {
583 case SNDRV_PCM_TRIGGER_START:
584 snd_pcm_group_for_each_entry(s, substream) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300585 struct snd_pcm_runtime *runtime = s->runtime;
586 struct snd_card_asihpi_pcm *ds = runtime->private_data;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200587
588 if (snd_pcm_substream_chip(s) != card)
589 continue;
590
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300591 /* don't link Cap and Play */
592 if (substream->stream != s->stream)
593 continue;
594
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200595 if ((s->stream == SNDRV_PCM_STREAM_PLAYBACK) &&
596 (card->support_mmap)) {
597 /* How do I know how much valid data is present
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300598 * in buffer? Must be at least one period!
599 * Guessing 2 periods, but if
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200600 * buffer is bigger it may contain even more
601 * data??
602 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300603 unsigned int preload = ds->period_bytes * 1;
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300604 snd_printddd("%d preload x%x\n", s->number, preload);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200605 hpi_handle_error(hpi_outstream_write_buf(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300606 ds->h_stream,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300607 &runtime->dma_area[0],
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200608 preload,
609 &ds->format));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300610 ds->pcm_buf_host_rw_ofs = preload;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200611 }
612
613 if (card->support_grouping) {
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200614 snd_printdd("%d group\n", s->number);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300615 e = hpi_stream_group_add(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200616 dpcm->h_stream,
617 ds->h_stream);
618 if (!e) {
619 snd_pcm_trigger_done(s, substream);
620 } else {
621 hpi_handle_error(e);
622 break;
623 }
624 } else
625 break;
626 }
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300627 snd_printdd("start\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200628 /* start the master stream */
629 snd_card_asihpi_pcm_timer_start(substream);
Eliot Blennerhassettc4ed97d2011-02-10 17:26:20 +1300630 if ((substream->stream == SNDRV_PCM_STREAM_CAPTURE) ||
631 !card->support_mmap)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300632 hpi_handle_error(hpi_stream_start(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200633 break;
634
635 case SNDRV_PCM_TRIGGER_STOP:
636 snd_card_asihpi_pcm_timer_stop(substream);
637 snd_pcm_group_for_each_entry(s, substream) {
638 if (snd_pcm_substream_chip(s) != card)
639 continue;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300640 /* don't link Cap and Play */
641 if (substream->stream != s->stream)
642 continue;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200643
644 /*? workaround linked streams don't
645 transition to SETUP 20070706*/
646 s->runtime->status->state = SNDRV_PCM_STATE_SETUP;
647
648 if (card->support_grouping) {
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200649 snd_printdd("%d group\n", s->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200650 snd_pcm_trigger_done(s, substream);
651 } else
652 break;
653 }
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300654 snd_printdd("stop\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200655
656 /* _prepare and _hwparams reset the stream */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300657 hpi_handle_error(hpi_stream_stop(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200658 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
659 hpi_handle_error(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300660 hpi_outstream_reset(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200661
662 if (card->support_grouping)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300663 hpi_handle_error(hpi_stream_group_reset(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200664 break;
665
666 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300667 snd_printdd("pause release\n");
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300668 hpi_handle_error(hpi_stream_start(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200669 snd_card_asihpi_pcm_timer_start(substream);
670 break;
671 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300672 snd_printdd("pause\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200673 snd_card_asihpi_pcm_timer_stop(substream);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300674 hpi_handle_error(hpi_stream_stop(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200675 break;
676 default:
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300677 snd_printd(KERN_ERR "\tINVALID\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200678 return -EINVAL;
679 }
680
681 return 0;
682}
683
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200684/*algorithm outline
685 Without linking degenerates to getting single stream pos etc
686 Without mmap 2nd loop degenerates to snd_pcm_period_elapsed
687*/
688/*
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300689pcm_buf_dma_ofs=get_buf_pos(s);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200690for_each_linked_stream(s) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300691 pcm_buf_dma_ofs=get_buf_pos(s);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300692 min_buf_pos = modulo_min(min_buf_pos, pcm_buf_dma_ofs, buffer_bytes)
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300693 new_data = min(new_data, calc_new_data(pcm_buf_dma_ofs,irq_pos)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200694}
695timer.expires = jiffies + predict_next_period_ready(min_buf_pos);
696for_each_linked_stream(s) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300697 s->pcm_buf_dma_ofs = min_buf_pos;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300698 if (new_data > period_bytes) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200699 if (mmap) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300700 irq_pos = (irq_pos + period_bytes) % buffer_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200701 if (playback) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300702 write(period_bytes);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200703 } else {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300704 read(period_bytes);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200705 }
706 }
707 snd_pcm_period_elapsed(s);
708 }
709}
710*/
711
712/** Minimum of 2 modulo values. Works correctly when the difference between
713* the values is less than half the modulus
714*/
715static inline unsigned int modulo_min(unsigned int a, unsigned int b,
716 unsigned long int modulus)
717{
718 unsigned int result;
719 if (((a-b) % modulus) < (modulus/2))
720 result = b;
721 else
722 result = a;
723
724 return result;
725}
726
727/** Timer function, equivalent to interrupt service routine for cards
728*/
729static void snd_card_asihpi_timer_function(unsigned long data)
730{
731 struct snd_card_asihpi_pcm *dpcm = (struct snd_card_asihpi_pcm *)data;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300732 struct snd_pcm_substream *substream = dpcm->substream;
733 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200734 struct snd_pcm_runtime *runtime;
735 struct snd_pcm_substream *s;
736 unsigned int newdata = 0;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300737 unsigned int pcm_buf_dma_ofs, min_buf_pos = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200738 unsigned int remdata, xfercount, next_jiffies;
739 int first = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300740 int loops = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200741 u16 state;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300742 u32 buffer_size, bytes_avail, samples_played, on_card_bytes;
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200743 DEBUG_NAME(substream, name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200744
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200745 snd_printdd("%s snd_card_asihpi_timer_function\n", name);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300746
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200747 /* find minimum newdata and buffer pos in group */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300748 snd_pcm_group_for_each_entry(s, substream) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200749 struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
750 runtime = s->runtime;
751
752 if (snd_pcm_substream_chip(s) != card)
753 continue;
754
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300755 /* don't link Cap and Play */
756 if (substream->stream != s->stream)
757 continue;
758
759 hpi_handle_error(hpi_stream_get_info_ex(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200760 ds->h_stream, &state,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300761 &buffer_size, &bytes_avail,
762 &samples_played, &on_card_bytes));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200763
764 /* number of bytes in on-card buffer */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300765 runtime->delay = on_card_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200766
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300767 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300768 pcm_buf_dma_ofs = ds->pcm_buf_host_rw_ofs - bytes_avail;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300769 if (state == HPI_STATE_STOPPED) {
770 if ((bytes_avail == 0) &&
771 (on_card_bytes < ds->pcm_buf_host_rw_ofs)) {
772 hpi_handle_error(hpi_stream_start(ds->h_stream));
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300773 snd_printdd("P%d start\n", s->number);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300774 }
775 } else if (state == HPI_STATE_DRAINED) {
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300776 snd_printd(KERN_WARNING "P%d drained\n",
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300777 s->number);
778 /*snd_pcm_stop(s, SNDRV_PCM_STATE_XRUN);
779 continue; */
780 }
781 } else
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300782 pcm_buf_dma_ofs = bytes_avail + ds->pcm_buf_host_rw_ofs;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200783
784 if (first) {
785 /* can't statically init min when wrap is involved */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300786 min_buf_pos = pcm_buf_dma_ofs;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300787 newdata = (pcm_buf_dma_ofs - ds->pcm_buf_elapsed_dma_ofs) % ds->buffer_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200788 first = 0;
789 } else {
790 min_buf_pos =
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300791 modulo_min(min_buf_pos, pcm_buf_dma_ofs, UINT_MAX+1L);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200792 newdata = min(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300793 (pcm_buf_dma_ofs - ds->pcm_buf_elapsed_dma_ofs) % ds->buffer_bytes,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200794 newdata);
795 }
796
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200797 snd_printdd("hw_ptr 0x%04lX, appl_ptr 0x%04lX\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200798 (unsigned long)frames_to_bytes(runtime,
799 runtime->status->hw_ptr),
800 (unsigned long)frames_to_bytes(runtime,
801 runtime->control->appl_ptr));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300802
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200803 snd_printdd("%d S=%d, "
804 "rw=0x%04X, dma=0x%04X, left=0x%04X, "
805 "aux=0x%04X space=0x%04X\n",
806 s->number, state,
807 ds->pcm_buf_host_rw_ofs, pcm_buf_dma_ofs,
808 (int)bytes_avail,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300809 (int)on_card_bytes, buffer_size-bytes_avail);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300810 loops++;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200811 }
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300812 pcm_buf_dma_ofs = min_buf_pos;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200813
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300814 remdata = newdata % dpcm->period_bytes;
815 xfercount = newdata - remdata; /* a multiple of period_bytes */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300816 /* come back when on_card_bytes has decreased enough to allow
817 write to happen, or when data has been consumed to make another
818 period
819 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300820 if (xfercount && (on_card_bytes > dpcm->period_bytes))
821 next_jiffies = ((on_card_bytes - dpcm->period_bytes) * HZ / dpcm->bytes_per_sec);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300822 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300823 next_jiffies = ((dpcm->period_bytes - remdata) * HZ / dpcm->bytes_per_sec);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300824
825 next_jiffies = max(next_jiffies, 1U);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200826 dpcm->timer.expires = jiffies + next_jiffies;
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200827 snd_printdd("jif %d buf pos 0x%04X newdata 0x%04X xfer 0x%04X\n",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300828 next_jiffies, pcm_buf_dma_ofs, newdata, xfercount);
829
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300830 snd_pcm_group_for_each_entry(s, substream) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200831 struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200832
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300833 /* don't link Cap and Play */
834 if (substream->stream != s->stream)
835 continue;
836
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300837 ds->pcm_buf_dma_ofs = pcm_buf_dma_ofs;
838
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300839 if (xfercount && (on_card_bytes <= ds->period_bytes)) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200840 if (card->support_mmap) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200841 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300842 snd_printddd("P%d write x%04x\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200843 s->number,
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300844 ds->period_bytes);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200845 hpi_handle_error(
846 hpi_outstream_write_buf(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300847 ds->h_stream,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200848 &s->runtime->
849 dma_area[0],
850 xfercount,
851 &ds->format));
852 } else {
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300853 snd_printddd("C%d read x%04x\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200854 s->number,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300855 xfercount);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200856 hpi_handle_error(
857 hpi_instream_read_buf(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300858 ds->h_stream,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200859 NULL, xfercount));
860 }
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300861 ds->pcm_buf_host_rw_ofs = ds->pcm_buf_host_rw_ofs + xfercount;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200862 } /* else R/W will be handled by read/write callbacks */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300863 ds->pcm_buf_elapsed_dma_ofs = pcm_buf_dma_ofs;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200864 snd_pcm_period_elapsed(s);
865 }
866 }
867
868 if (dpcm->respawn_timer)
869 add_timer(&dpcm->timer);
870}
871
872/***************************** PLAYBACK OPS ****************/
873static int snd_card_asihpi_playback_ioctl(struct snd_pcm_substream *substream,
874 unsigned int cmd, void *arg)
875{
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200876 snd_printddd(KERN_INFO "P%d ioctl %d\n", substream->number, cmd);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200877 return snd_pcm_lib_ioctl(substream, cmd, arg);
878}
879
880static int snd_card_asihpi_playback_prepare(struct snd_pcm_substream *
881 substream)
882{
883 struct snd_pcm_runtime *runtime = substream->runtime;
884 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
885
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200886 snd_printdd("P%d prepare\n", substream->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200887
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300888 hpi_handle_error(hpi_outstream_reset(dpcm->h_stream));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300889 dpcm->pcm_buf_host_rw_ofs = 0;
890 dpcm->pcm_buf_dma_ofs = 0;
891 dpcm->pcm_buf_elapsed_dma_ofs = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200892 return 0;
893}
894
895static snd_pcm_uframes_t
896snd_card_asihpi_playback_pointer(struct snd_pcm_substream *substream)
897{
898 struct snd_pcm_runtime *runtime = substream->runtime;
899 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
900 snd_pcm_uframes_t ptr;
901
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300902 ptr = bytes_to_frames(runtime, dpcm->pcm_buf_dma_ofs % dpcm->buffer_bytes);
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200903 snd_printddd("P%d pointer = 0x%04lx\n", substream->number, (unsigned long)ptr);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200904 return ptr;
905}
906
907static void snd_card_asihpi_playback_format(struct snd_card_asihpi *asihpi,
908 u32 h_stream,
909 struct snd_pcm_hardware *pcmhw)
910{
911 struct hpi_format hpi_format;
912 u16 format;
913 u16 err;
914 u32 h_control;
915 u32 sample_rate = 48000;
916
917 /* on cards without SRC, must query at valid rate,
918 * maybe set by external sync
919 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300920 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200921 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
922 HPI_CONTROL_SAMPLECLOCK, &h_control);
923
924 if (!err)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300925 err = hpi_sample_clock_get_sample_rate(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200926 &sample_rate);
927
928 for (format = HPI_FORMAT_PCM8_UNSIGNED;
929 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
930 err = hpi_format_create(&hpi_format,
931 2, format, sample_rate, 128000, 0);
932 if (!err)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300933 err = hpi_outstream_query_format(h_stream,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200934 &hpi_format);
935 if (!err && (hpi_to_alsa_formats[format] != -1))
936 pcmhw->formats |=
937 (1ULL << hpi_to_alsa_formats[format]);
938 }
939}
940
941static struct snd_pcm_hardware snd_card_asihpi_playback = {
942 .channels_min = 1,
943 .channels_max = 2,
944 .buffer_bytes_max = BUFFER_BYTES_MAX,
945 .period_bytes_min = PERIOD_BYTES_MIN,
946 .period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN,
947 .periods_min = PERIODS_MIN,
948 .periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN,
949 .fifo_size = 0,
950};
951
952static int snd_card_asihpi_playback_open(struct snd_pcm_substream *substream)
953{
954 struct snd_pcm_runtime *runtime = substream->runtime;
955 struct snd_card_asihpi_pcm *dpcm;
956 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
957 int err;
958
959 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
960 if (dpcm == NULL)
961 return -ENOMEM;
962
963 err =
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300964 hpi_outstream_open(card->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200965 substream->number, &dpcm->h_stream);
966 hpi_handle_error(err);
967 if (err)
968 kfree(dpcm);
969 if (err == HPI_ERROR_OBJ_ALREADY_OPEN)
970 return -EBUSY;
971 if (err)
972 return -EIO;
973
974 /*? also check ASI5000 samplerate source
975 If external, only support external rate.
976 If internal and other stream playing, cant switch
977 */
978
979 init_timer(&dpcm->timer);
980 dpcm->timer.data = (unsigned long) dpcm;
981 dpcm->timer.function = snd_card_asihpi_timer_function;
982 dpcm->substream = substream;
983 runtime->private_data = dpcm;
984 runtime->private_free = snd_card_asihpi_runtime_free;
985
986 snd_card_asihpi_playback.channels_max = card->out_max_chans;
987 /*?snd_card_asihpi_playback.period_bytes_min =
988 card->out_max_chans * 4096; */
989
990 snd_card_asihpi_playback_format(card, dpcm->h_stream,
991 &snd_card_asihpi_playback);
992
993 snd_card_asihpi_pcm_samplerates(card, &snd_card_asihpi_playback);
994
995 snd_card_asihpi_playback.info = SNDRV_PCM_INFO_INTERLEAVED |
996 SNDRV_PCM_INFO_DOUBLE |
997 SNDRV_PCM_INFO_BATCH |
998 SNDRV_PCM_INFO_BLOCK_TRANSFER |
999 SNDRV_PCM_INFO_PAUSE;
1000
1001 if (card->support_mmap)
1002 snd_card_asihpi_playback.info |= SNDRV_PCM_INFO_MMAP |
1003 SNDRV_PCM_INFO_MMAP_VALID;
1004
1005 if (card->support_grouping)
1006 snd_card_asihpi_playback.info |= SNDRV_PCM_INFO_SYNC_START;
1007
1008 /* struct is copied, so can create initializer dynamically */
1009 runtime->hw = snd_card_asihpi_playback;
1010
1011 if (card->support_mmap)
1012 err = snd_pcm_hw_constraint_pow2(runtime, 0,
1013 SNDRV_PCM_HW_PARAM_BUFFER_BYTES);
1014 if (err < 0)
1015 return err;
1016
1017 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1018 card->update_interval_frames);
Eliot Blennerhassett26aebef2011-03-25 15:25:47 +13001019
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001020 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001021 card->update_interval_frames * 2, UINT_MAX);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001022
1023 snd_pcm_set_sync(substream);
1024
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001025 snd_printdd("playback open\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001026
1027 return 0;
1028}
1029
1030static int snd_card_asihpi_playback_close(struct snd_pcm_substream *substream)
1031{
1032 struct snd_pcm_runtime *runtime = substream->runtime;
1033 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1034
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001035 hpi_handle_error(hpi_outstream_close(dpcm->h_stream));
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001036 snd_printdd("playback close\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001037
1038 return 0;
1039}
1040
1041static int snd_card_asihpi_playback_copy(struct snd_pcm_substream *substream,
1042 int channel,
1043 snd_pcm_uframes_t pos,
1044 void __user *src,
1045 snd_pcm_uframes_t count)
1046{
1047 struct snd_pcm_runtime *runtime = substream->runtime;
1048 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1049 unsigned int len;
1050
1051 len = frames_to_bytes(runtime, count);
1052
1053 if (copy_from_user(runtime->dma_area, src, len))
1054 return -EFAULT;
1055
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001056 snd_printddd("playback copy%d %u bytes\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001057 substream->number, len);
1058
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001059 hpi_handle_error(hpi_outstream_write_buf(dpcm->h_stream,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001060 runtime->dma_area, len, &dpcm->format));
1061
Eliot Blennerhassett26aebef2011-03-25 15:25:47 +13001062 dpcm->pcm_buf_host_rw_ofs += len;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001063
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001064 return 0;
1065}
1066
1067static int snd_card_asihpi_playback_silence(struct snd_pcm_substream *
1068 substream, int channel,
1069 snd_pcm_uframes_t pos,
1070 snd_pcm_uframes_t count)
1071{
Eliot Blennerhassett26aebef2011-03-25 15:25:47 +13001072 /* Usually writes silence to DMA buffer, which should be overwritten
1073 by real audio later. Our fifos cannot be overwritten, and are not
1074 free-running DMAs. Silence is output on fifo underflow.
1075 This callback is still required to allow the copy callback to be used.
1076 */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001077 return 0;
1078}
1079
1080static struct snd_pcm_ops snd_card_asihpi_playback_ops = {
1081 .open = snd_card_asihpi_playback_open,
1082 .close = snd_card_asihpi_playback_close,
1083 .ioctl = snd_card_asihpi_playback_ioctl,
1084 .hw_params = snd_card_asihpi_pcm_hw_params,
1085 .hw_free = snd_card_asihpi_hw_free,
1086 .prepare = snd_card_asihpi_playback_prepare,
1087 .trigger = snd_card_asihpi_trigger,
1088 .pointer = snd_card_asihpi_playback_pointer,
1089 .copy = snd_card_asihpi_playback_copy,
1090 .silence = snd_card_asihpi_playback_silence,
1091};
1092
1093static struct snd_pcm_ops snd_card_asihpi_playback_mmap_ops = {
1094 .open = snd_card_asihpi_playback_open,
1095 .close = snd_card_asihpi_playback_close,
1096 .ioctl = snd_card_asihpi_playback_ioctl,
1097 .hw_params = snd_card_asihpi_pcm_hw_params,
1098 .hw_free = snd_card_asihpi_hw_free,
1099 .prepare = snd_card_asihpi_playback_prepare,
1100 .trigger = snd_card_asihpi_trigger,
1101 .pointer = snd_card_asihpi_playback_pointer,
1102};
1103
1104/***************************** CAPTURE OPS ****************/
1105static snd_pcm_uframes_t
1106snd_card_asihpi_capture_pointer(struct snd_pcm_substream *substream)
1107{
1108 struct snd_pcm_runtime *runtime = substream->runtime;
1109 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1110
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001111 snd_printddd("capture pointer %d=%d\n",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001112 substream->number, dpcm->pcm_buf_dma_ofs);
1113 /* NOTE Unlike playback can't use actual samples_played
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001114 for the capture position, because those samples aren't yet in
1115 the local buffer available for reading.
1116 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001117 return bytes_to_frames(runtime, dpcm->pcm_buf_dma_ofs % dpcm->buffer_bytes);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001118}
1119
1120static int snd_card_asihpi_capture_ioctl(struct snd_pcm_substream *substream,
1121 unsigned int cmd, void *arg)
1122{
1123 return snd_pcm_lib_ioctl(substream, cmd, arg);
1124}
1125
1126static int snd_card_asihpi_capture_prepare(struct snd_pcm_substream *substream)
1127{
1128 struct snd_pcm_runtime *runtime = substream->runtime;
1129 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1130
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001131 hpi_handle_error(hpi_instream_reset(dpcm->h_stream));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001132 dpcm->pcm_buf_host_rw_ofs = 0;
1133 dpcm->pcm_buf_dma_ofs = 0;
1134 dpcm->pcm_buf_elapsed_dma_ofs = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001135
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001136 snd_printdd("Capture Prepare %d\n", substream->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001137 return 0;
1138}
1139
1140
1141
1142static void snd_card_asihpi_capture_format(struct snd_card_asihpi *asihpi,
1143 u32 h_stream,
1144 struct snd_pcm_hardware *pcmhw)
1145{
1146 struct hpi_format hpi_format;
1147 u16 format;
1148 u16 err;
1149 u32 h_control;
1150 u32 sample_rate = 48000;
1151
1152 /* on cards without SRC, must query at valid rate,
1153 maybe set by external sync */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001154 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001155 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
1156 HPI_CONTROL_SAMPLECLOCK, &h_control);
1157
1158 if (!err)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001159 err = hpi_sample_clock_get_sample_rate(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001160 &sample_rate);
1161
1162 for (format = HPI_FORMAT_PCM8_UNSIGNED;
1163 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
1164
1165 err = hpi_format_create(&hpi_format, 2, format,
1166 sample_rate, 128000, 0);
1167 if (!err)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001168 err = hpi_instream_query_format(h_stream,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001169 &hpi_format);
1170 if (!err)
1171 pcmhw->formats |=
1172 (1ULL << hpi_to_alsa_formats[format]);
1173 }
1174}
1175
1176
1177static struct snd_pcm_hardware snd_card_asihpi_capture = {
1178 .channels_min = 1,
1179 .channels_max = 2,
1180 .buffer_bytes_max = BUFFER_BYTES_MAX,
1181 .period_bytes_min = PERIOD_BYTES_MIN,
1182 .period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN,
1183 .periods_min = PERIODS_MIN,
1184 .periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN,
1185 .fifo_size = 0,
1186};
1187
1188static int snd_card_asihpi_capture_open(struct snd_pcm_substream *substream)
1189{
1190 struct snd_pcm_runtime *runtime = substream->runtime;
1191 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
1192 struct snd_card_asihpi_pcm *dpcm;
1193 int err;
1194
1195 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
1196 if (dpcm == NULL)
1197 return -ENOMEM;
1198
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001199 snd_printdd("capture open adapter %d stream %d\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001200 card->adapter_index, substream->number);
1201
1202 err = hpi_handle_error(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001203 hpi_instream_open(card->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001204 substream->number, &dpcm->h_stream));
1205 if (err)
1206 kfree(dpcm);
1207 if (err == HPI_ERROR_OBJ_ALREADY_OPEN)
1208 return -EBUSY;
1209 if (err)
1210 return -EIO;
1211
1212
1213 init_timer(&dpcm->timer);
1214 dpcm->timer.data = (unsigned long) dpcm;
1215 dpcm->timer.function = snd_card_asihpi_timer_function;
1216 dpcm->substream = substream;
1217 runtime->private_data = dpcm;
1218 runtime->private_free = snd_card_asihpi_runtime_free;
1219
1220 snd_card_asihpi_capture.channels_max = card->in_max_chans;
1221 snd_card_asihpi_capture_format(card, dpcm->h_stream,
1222 &snd_card_asihpi_capture);
1223 snd_card_asihpi_pcm_samplerates(card, &snd_card_asihpi_capture);
1224 snd_card_asihpi_capture.info = SNDRV_PCM_INFO_INTERLEAVED;
1225
1226 if (card->support_mmap)
1227 snd_card_asihpi_capture.info |= SNDRV_PCM_INFO_MMAP |
1228 SNDRV_PCM_INFO_MMAP_VALID;
1229
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001230 if (card->support_grouping)
1231 snd_card_asihpi_capture.info |= SNDRV_PCM_INFO_SYNC_START;
1232
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001233 runtime->hw = snd_card_asihpi_capture;
1234
1235 if (card->support_mmap)
1236 err = snd_pcm_hw_constraint_pow2(runtime, 0,
1237 SNDRV_PCM_HW_PARAM_BUFFER_BYTES);
1238 if (err < 0)
1239 return err;
1240
1241 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1242 card->update_interval_frames);
1243 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1244 card->update_interval_frames * 2, UINT_MAX);
1245
1246 snd_pcm_set_sync(substream);
1247
1248 return 0;
1249}
1250
1251static int snd_card_asihpi_capture_close(struct snd_pcm_substream *substream)
1252{
1253 struct snd_card_asihpi_pcm *dpcm = substream->runtime->private_data;
1254
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001255 hpi_handle_error(hpi_instream_close(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001256 return 0;
1257}
1258
1259static int snd_card_asihpi_capture_copy(struct snd_pcm_substream *substream,
1260 int channel, snd_pcm_uframes_t pos,
1261 void __user *dst, snd_pcm_uframes_t count)
1262{
1263 struct snd_pcm_runtime *runtime = substream->runtime;
1264 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001265 u32 len;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001266
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001267 len = frames_to_bytes(runtime, count);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001268
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001269 snd_printddd("capture copy%d %d bytes\n", substream->number, len);
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001270 hpi_handle_error(hpi_instream_read_buf(dpcm->h_stream,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001271 runtime->dma_area, len));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001272
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001273 dpcm->pcm_buf_host_rw_ofs = dpcm->pcm_buf_host_rw_ofs + len;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001274
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001275 if (copy_to_user(dst, runtime->dma_area, len))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001276 return -EFAULT;
1277
1278 return 0;
1279}
1280
1281static struct snd_pcm_ops snd_card_asihpi_capture_mmap_ops = {
1282 .open = snd_card_asihpi_capture_open,
1283 .close = snd_card_asihpi_capture_close,
1284 .ioctl = snd_card_asihpi_capture_ioctl,
1285 .hw_params = snd_card_asihpi_pcm_hw_params,
1286 .hw_free = snd_card_asihpi_hw_free,
1287 .prepare = snd_card_asihpi_capture_prepare,
1288 .trigger = snd_card_asihpi_trigger,
1289 .pointer = snd_card_asihpi_capture_pointer,
1290};
1291
1292static struct snd_pcm_ops snd_card_asihpi_capture_ops = {
1293 .open = snd_card_asihpi_capture_open,
1294 .close = snd_card_asihpi_capture_close,
1295 .ioctl = snd_card_asihpi_capture_ioctl,
1296 .hw_params = snd_card_asihpi_pcm_hw_params,
1297 .hw_free = snd_card_asihpi_hw_free,
1298 .prepare = snd_card_asihpi_capture_prepare,
1299 .trigger = snd_card_asihpi_trigger,
1300 .pointer = snd_card_asihpi_capture_pointer,
1301 .copy = snd_card_asihpi_capture_copy
1302};
1303
1304static int __devinit snd_card_asihpi_pcm_new(struct snd_card_asihpi *asihpi,
1305 int device, int substreams)
1306{
1307 struct snd_pcm *pcm;
1308 int err;
1309
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001310 err = snd_pcm_new(asihpi->card, "Asihpi PCM", device,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001311 asihpi->num_outstreams, asihpi->num_instreams,
1312 &pcm);
1313 if (err < 0)
1314 return err;
1315 /* pointer to ops struct is stored, dont change ops afterwards! */
1316 if (asihpi->support_mmap) {
1317 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1318 &snd_card_asihpi_playback_mmap_ops);
1319 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1320 &snd_card_asihpi_capture_mmap_ops);
1321 } else {
1322 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1323 &snd_card_asihpi_playback_ops);
1324 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1325 &snd_card_asihpi_capture_ops);
1326 }
1327
1328 pcm->private_data = asihpi;
1329 pcm->info_flags = 0;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001330 strcpy(pcm->name, "Asihpi PCM");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001331
1332 /*? do we want to emulate MMAP for non-BBM cards?
1333 Jack doesn't work with ALSAs MMAP emulation - WHY NOT? */
1334 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1335 snd_dma_pci_data(asihpi->pci),
1336 64*1024, BUFFER_BYTES_MAX);
1337
1338 return 0;
1339}
1340
1341/***************************** MIXER CONTROLS ****************/
1342struct hpi_control {
1343 u32 h_control;
1344 u16 control_type;
1345 u16 src_node_type;
1346 u16 src_node_index;
1347 u16 dst_node_type;
1348 u16 dst_node_index;
1349 u16 band;
1350 char name[44]; /* copied to snd_ctl_elem_id.name[44]; */
1351};
1352
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001353static const char * const asihpi_tuner_band_names[] = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001354 "invalid",
1355 "AM",
1356 "FM mono",
1357 "TV NTSC-M",
1358 "FM stereo",
1359 "AUX",
1360 "TV PAL BG",
1361 "TV PAL I",
1362 "TV PAL DK",
1363 "TV SECAM",
1364};
1365
1366compile_time_assert(
1367 (ARRAY_SIZE(asihpi_tuner_band_names) ==
1368 (HPI_TUNER_BAND_LAST+1)),
1369 assert_tuner_band_names_size);
1370
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001371static const char * const asihpi_src_names[] = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001372 "no source",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001373 "PCM",
1374 "Line",
1375 "Digital",
1376 "Tuner",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001377 "RF",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001378 "Clock",
1379 "Bitstream",
1380 "Microphone",
1381 "Cobranet",
1382 "Analog",
1383 "Adapter",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001384};
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001385
1386compile_time_assert(
1387 (ARRAY_SIZE(asihpi_src_names) ==
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12001388 (HPI_SOURCENODE_LAST_INDEX-HPI_SOURCENODE_NONE+1)),
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001389 assert_src_names_size);
1390
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001391static const char * const asihpi_dst_names[] = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001392 "no destination",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001393 "PCM",
1394 "Line",
1395 "Digital",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001396 "RF",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001397 "Speaker",
1398 "Cobranet Out",
1399 "Analog"
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001400};
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001401
1402compile_time_assert(
1403 (ARRAY_SIZE(asihpi_dst_names) ==
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12001404 (HPI_DESTNODE_LAST_INDEX-HPI_DESTNODE_NONE+1)),
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001405 assert_dst_names_size);
1406
1407static inline int ctl_add(struct snd_card *card, struct snd_kcontrol_new *ctl,
1408 struct snd_card_asihpi *asihpi)
1409{
1410 int err;
1411
1412 err = snd_ctl_add(card, snd_ctl_new1(ctl, asihpi));
1413 if (err < 0)
1414 return err;
1415 else if (mixer_dump)
1416 snd_printk(KERN_INFO "added %s(%d)\n", ctl->name, ctl->index);
1417
1418 return 0;
1419}
1420
1421/* Convert HPI control name and location into ALSA control name */
1422static void asihpi_ctl_init(struct snd_kcontrol_new *snd_control,
1423 struct hpi_control *hpi_ctl,
1424 char *name)
1425{
Eliot Blennerhassett550ac6b2011-04-05 20:55:41 +12001426 char *dir;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001427 memset(snd_control, 0, sizeof(*snd_control));
1428 snd_control->name = hpi_ctl->name;
1429 snd_control->private_value = hpi_ctl->h_control;
1430 snd_control->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1431 snd_control->index = 0;
1432
Eliot Blennerhassett550ac6b2011-04-05 20:55:41 +12001433 if (hpi_ctl->src_node_type + HPI_SOURCENODE_NONE == HPI_SOURCENODE_CLOCK_SOURCE)
1434 dir = ""; /* clock is neither capture nor playback */
1435 else if (hpi_ctl->dst_node_type + HPI_DESTNODE_NONE == HPI_DESTNODE_ISTREAM)
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001436 dir = "Capture "; /* On or towards a PCM capture destination*/
1437 else if ((hpi_ctl->src_node_type + HPI_SOURCENODE_NONE != HPI_SOURCENODE_OSTREAM) &&
1438 (!hpi_ctl->dst_node_type))
1439 dir = "Capture "; /* On a source node that is not PCM playback */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001440 else if (hpi_ctl->src_node_type &&
1441 (hpi_ctl->src_node_type + HPI_SOURCENODE_NONE != HPI_SOURCENODE_OSTREAM) &&
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001442 (hpi_ctl->dst_node_type))
1443 dir = "Monitor Playback "; /* Between an input and an output */
1444 else
1445 dir = "Playback "; /* PCM Playback source, or output node */
1446
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001447 if (hpi_ctl->src_node_type && hpi_ctl->dst_node_type)
Eliot Blennerhassett550ac6b2011-04-05 20:55:41 +12001448 sprintf(hpi_ctl->name, "%s %d %s %d %s%s",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001449 asihpi_src_names[hpi_ctl->src_node_type],
1450 hpi_ctl->src_node_index,
1451 asihpi_dst_names[hpi_ctl->dst_node_type],
1452 hpi_ctl->dst_node_index,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001453 dir, name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001454 else if (hpi_ctl->dst_node_type) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001455 sprintf(hpi_ctl->name, "%s %d %s%s",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001456 asihpi_dst_names[hpi_ctl->dst_node_type],
1457 hpi_ctl->dst_node_index,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001458 dir, name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001459 } else {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001460 sprintf(hpi_ctl->name, "%s %d %s%s",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001461 asihpi_src_names[hpi_ctl->src_node_type],
1462 hpi_ctl->src_node_index,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001463 dir, name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001464 }
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001465 /* printk(KERN_INFO "Adding %s %d to %d ", hpi_ctl->name,
1466 hpi_ctl->wSrcNodeType, hpi_ctl->wDstNodeType); */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001467}
1468
1469/*------------------------------------------------------------
1470 Volume controls
1471 ------------------------------------------------------------*/
1472#define VOL_STEP_mB 1
1473static int snd_asihpi_volume_info(struct snd_kcontrol *kcontrol,
1474 struct snd_ctl_elem_info *uinfo)
1475{
1476 u32 h_control = kcontrol->private_value;
1477 u16 err;
1478 /* native gains are in millibels */
1479 short min_gain_mB;
1480 short max_gain_mB;
1481 short step_gain_mB;
1482
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001483 err = hpi_volume_query_range(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001484 &min_gain_mB, &max_gain_mB, &step_gain_mB);
1485 if (err) {
1486 max_gain_mB = 0;
1487 min_gain_mB = -10000;
1488 step_gain_mB = VOL_STEP_mB;
1489 }
1490
1491 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1492 uinfo->count = 2;
1493 uinfo->value.integer.min = min_gain_mB / VOL_STEP_mB;
1494 uinfo->value.integer.max = max_gain_mB / VOL_STEP_mB;
1495 uinfo->value.integer.step = step_gain_mB / VOL_STEP_mB;
1496 return 0;
1497}
1498
1499static int snd_asihpi_volume_get(struct snd_kcontrol *kcontrol,
1500 struct snd_ctl_elem_value *ucontrol)
1501{
1502 u32 h_control = kcontrol->private_value;
1503 short an_gain_mB[HPI_MAX_CHANNELS];
1504
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001505 hpi_handle_error(hpi_volume_get_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001506 ucontrol->value.integer.value[0] = an_gain_mB[0] / VOL_STEP_mB;
1507 ucontrol->value.integer.value[1] = an_gain_mB[1] / VOL_STEP_mB;
1508
1509 return 0;
1510}
1511
1512static int snd_asihpi_volume_put(struct snd_kcontrol *kcontrol,
1513 struct snd_ctl_elem_value *ucontrol)
1514{
1515 int change;
1516 u32 h_control = kcontrol->private_value;
1517 short an_gain_mB[HPI_MAX_CHANNELS];
1518
1519 an_gain_mB[0] =
1520 (ucontrol->value.integer.value[0]) * VOL_STEP_mB;
1521 an_gain_mB[1] =
1522 (ucontrol->value.integer.value[1]) * VOL_STEP_mB;
1523 /* change = asihpi->mixer_volume[addr][0] != left ||
1524 asihpi->mixer_volume[addr][1] != right;
1525 */
1526 change = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001527 hpi_handle_error(hpi_volume_set_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001528 return change;
1529}
1530
1531static const DECLARE_TLV_DB_SCALE(db_scale_100, -10000, VOL_STEP_mB, 0);
1532
1533static int __devinit snd_asihpi_volume_add(struct snd_card_asihpi *asihpi,
1534 struct hpi_control *hpi_ctl)
1535{
1536 struct snd_card *card = asihpi->card;
1537 struct snd_kcontrol_new snd_control;
1538
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001539 asihpi_ctl_init(&snd_control, hpi_ctl, "Volume");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001540 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1541 SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1542 snd_control.info = snd_asihpi_volume_info;
1543 snd_control.get = snd_asihpi_volume_get;
1544 snd_control.put = snd_asihpi_volume_put;
1545 snd_control.tlv.p = db_scale_100;
1546
1547 return ctl_add(card, &snd_control, asihpi);
1548}
1549
1550/*------------------------------------------------------------
1551 Level controls
1552 ------------------------------------------------------------*/
1553static int snd_asihpi_level_info(struct snd_kcontrol *kcontrol,
1554 struct snd_ctl_elem_info *uinfo)
1555{
1556 u32 h_control = kcontrol->private_value;
1557 u16 err;
1558 short min_gain_mB;
1559 short max_gain_mB;
1560 short step_gain_mB;
1561
1562 err =
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001563 hpi_level_query_range(h_control, &min_gain_mB,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001564 &max_gain_mB, &step_gain_mB);
1565 if (err) {
1566 max_gain_mB = 2400;
1567 min_gain_mB = -1000;
1568 step_gain_mB = 100;
1569 }
1570
1571 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1572 uinfo->count = 2;
1573 uinfo->value.integer.min = min_gain_mB / HPI_UNITS_PER_dB;
1574 uinfo->value.integer.max = max_gain_mB / HPI_UNITS_PER_dB;
1575 uinfo->value.integer.step = step_gain_mB / HPI_UNITS_PER_dB;
1576 return 0;
1577}
1578
1579static int snd_asihpi_level_get(struct snd_kcontrol *kcontrol,
1580 struct snd_ctl_elem_value *ucontrol)
1581{
1582 u32 h_control = kcontrol->private_value;
1583 short an_gain_mB[HPI_MAX_CHANNELS];
1584
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001585 hpi_handle_error(hpi_level_get_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001586 ucontrol->value.integer.value[0] =
1587 an_gain_mB[0] / HPI_UNITS_PER_dB;
1588 ucontrol->value.integer.value[1] =
1589 an_gain_mB[1] / HPI_UNITS_PER_dB;
1590
1591 return 0;
1592}
1593
1594static int snd_asihpi_level_put(struct snd_kcontrol *kcontrol,
1595 struct snd_ctl_elem_value *ucontrol)
1596{
1597 int change;
1598 u32 h_control = kcontrol->private_value;
1599 short an_gain_mB[HPI_MAX_CHANNELS];
1600
1601 an_gain_mB[0] =
1602 (ucontrol->value.integer.value[0]) * HPI_UNITS_PER_dB;
1603 an_gain_mB[1] =
1604 (ucontrol->value.integer.value[1]) * HPI_UNITS_PER_dB;
1605 /* change = asihpi->mixer_level[addr][0] != left ||
1606 asihpi->mixer_level[addr][1] != right;
1607 */
1608 change = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001609 hpi_handle_error(hpi_level_set_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001610 return change;
1611}
1612
1613static const DECLARE_TLV_DB_SCALE(db_scale_level, -1000, 100, 0);
1614
1615static int __devinit snd_asihpi_level_add(struct snd_card_asihpi *asihpi,
1616 struct hpi_control *hpi_ctl)
1617{
1618 struct snd_card *card = asihpi->card;
1619 struct snd_kcontrol_new snd_control;
1620
1621 /* can't use 'volume' cos some nodes have volume as well */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001622 asihpi_ctl_init(&snd_control, hpi_ctl, "Level");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001623 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1624 SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1625 snd_control.info = snd_asihpi_level_info;
1626 snd_control.get = snd_asihpi_level_get;
1627 snd_control.put = snd_asihpi_level_put;
1628 snd_control.tlv.p = db_scale_level;
1629
1630 return ctl_add(card, &snd_control, asihpi);
1631}
1632
1633/*------------------------------------------------------------
1634 AESEBU controls
1635 ------------------------------------------------------------*/
1636
1637/* AESEBU format */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001638static const char * const asihpi_aesebu_format_names[] = {
1639 "N/A", "S/PDIF", "AES/EBU" };
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001640
1641static int snd_asihpi_aesebu_format_info(struct snd_kcontrol *kcontrol,
1642 struct snd_ctl_elem_info *uinfo)
1643{
1644 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1645 uinfo->count = 1;
1646 uinfo->value.enumerated.items = 3;
1647
1648 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1649 uinfo->value.enumerated.item =
1650 uinfo->value.enumerated.items - 1;
1651
1652 strcpy(uinfo->value.enumerated.name,
1653 asihpi_aesebu_format_names[uinfo->value.enumerated.item]);
1654
1655 return 0;
1656}
1657
1658static int snd_asihpi_aesebu_format_get(struct snd_kcontrol *kcontrol,
1659 struct snd_ctl_elem_value *ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001660 u16 (*func)(u32, u16 *))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001661{
1662 u32 h_control = kcontrol->private_value;
1663 u16 source, err;
1664
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001665 err = func(h_control, &source);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001666
1667 /* default to N/A */
1668 ucontrol->value.enumerated.item[0] = 0;
1669 /* return success but set the control to N/A */
1670 if (err)
1671 return 0;
1672 if (source == HPI_AESEBU_FORMAT_SPDIF)
1673 ucontrol->value.enumerated.item[0] = 1;
1674 if (source == HPI_AESEBU_FORMAT_AESEBU)
1675 ucontrol->value.enumerated.item[0] = 2;
1676
1677 return 0;
1678}
1679
1680static int snd_asihpi_aesebu_format_put(struct snd_kcontrol *kcontrol,
1681 struct snd_ctl_elem_value *ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001682 u16 (*func)(u32, u16))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001683{
1684 u32 h_control = kcontrol->private_value;
1685
1686 /* default to S/PDIF */
1687 u16 source = HPI_AESEBU_FORMAT_SPDIF;
1688
1689 if (ucontrol->value.enumerated.item[0] == 1)
1690 source = HPI_AESEBU_FORMAT_SPDIF;
1691 if (ucontrol->value.enumerated.item[0] == 2)
1692 source = HPI_AESEBU_FORMAT_AESEBU;
1693
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001694 if (func(h_control, source) != 0)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001695 return -EINVAL;
1696
1697 return 1;
1698}
1699
1700static int snd_asihpi_aesebu_rx_format_get(struct snd_kcontrol *kcontrol,
1701 struct snd_ctl_elem_value *ucontrol) {
1702 return snd_asihpi_aesebu_format_get(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001703 hpi_aesebu_receiver_get_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001704}
1705
1706static int snd_asihpi_aesebu_rx_format_put(struct snd_kcontrol *kcontrol,
1707 struct snd_ctl_elem_value *ucontrol) {
1708 return snd_asihpi_aesebu_format_put(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001709 hpi_aesebu_receiver_set_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001710}
1711
1712static int snd_asihpi_aesebu_rxstatus_info(struct snd_kcontrol *kcontrol,
1713 struct snd_ctl_elem_info *uinfo)
1714{
1715 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1716 uinfo->count = 1;
1717
1718 uinfo->value.integer.min = 0;
1719 uinfo->value.integer.max = 0X1F;
1720 uinfo->value.integer.step = 1;
1721
1722 return 0;
1723}
1724
1725static int snd_asihpi_aesebu_rxstatus_get(struct snd_kcontrol *kcontrol,
1726 struct snd_ctl_elem_value *ucontrol) {
1727
1728 u32 h_control = kcontrol->private_value;
1729 u16 status;
1730
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001731 hpi_handle_error(hpi_aesebu_receiver_get_error_status(
1732 h_control, &status));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001733 ucontrol->value.integer.value[0] = status;
1734 return 0;
1735}
1736
1737static int __devinit snd_asihpi_aesebu_rx_add(struct snd_card_asihpi *asihpi,
1738 struct hpi_control *hpi_ctl)
1739{
1740 struct snd_card *card = asihpi->card;
1741 struct snd_kcontrol_new snd_control;
1742
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001743 asihpi_ctl_init(&snd_control, hpi_ctl, "Format");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001744 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1745 snd_control.info = snd_asihpi_aesebu_format_info;
1746 snd_control.get = snd_asihpi_aesebu_rx_format_get;
1747 snd_control.put = snd_asihpi_aesebu_rx_format_put;
1748
1749
1750 if (ctl_add(card, &snd_control, asihpi) < 0)
1751 return -EINVAL;
1752
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001753 asihpi_ctl_init(&snd_control, hpi_ctl, "Status");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001754 snd_control.access =
1755 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
1756 snd_control.info = snd_asihpi_aesebu_rxstatus_info;
1757 snd_control.get = snd_asihpi_aesebu_rxstatus_get;
1758
1759 return ctl_add(card, &snd_control, asihpi);
1760}
1761
1762static int snd_asihpi_aesebu_tx_format_get(struct snd_kcontrol *kcontrol,
1763 struct snd_ctl_elem_value *ucontrol) {
1764 return snd_asihpi_aesebu_format_get(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001765 hpi_aesebu_transmitter_get_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001766}
1767
1768static int snd_asihpi_aesebu_tx_format_put(struct snd_kcontrol *kcontrol,
1769 struct snd_ctl_elem_value *ucontrol) {
1770 return snd_asihpi_aesebu_format_put(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001771 hpi_aesebu_transmitter_set_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001772}
1773
1774
1775static int __devinit snd_asihpi_aesebu_tx_add(struct snd_card_asihpi *asihpi,
1776 struct hpi_control *hpi_ctl)
1777{
1778 struct snd_card *card = asihpi->card;
1779 struct snd_kcontrol_new snd_control;
1780
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001781 asihpi_ctl_init(&snd_control, hpi_ctl, "Format");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001782 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1783 snd_control.info = snd_asihpi_aesebu_format_info;
1784 snd_control.get = snd_asihpi_aesebu_tx_format_get;
1785 snd_control.put = snd_asihpi_aesebu_tx_format_put;
1786
1787 return ctl_add(card, &snd_control, asihpi);
1788}
1789
1790/*------------------------------------------------------------
1791 Tuner controls
1792 ------------------------------------------------------------*/
1793
1794/* Gain */
1795
1796static int snd_asihpi_tuner_gain_info(struct snd_kcontrol *kcontrol,
1797 struct snd_ctl_elem_info *uinfo)
1798{
1799 u32 h_control = kcontrol->private_value;
1800 u16 err;
1801 short idx;
1802 u16 gain_range[3];
1803
1804 for (idx = 0; idx < 3; idx++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001805 err = hpi_tuner_query_gain(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001806 idx, &gain_range[idx]);
1807 if (err != 0)
1808 return err;
1809 }
1810
1811 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1812 uinfo->count = 1;
1813 uinfo->value.integer.min = ((int)gain_range[0]) / HPI_UNITS_PER_dB;
1814 uinfo->value.integer.max = ((int)gain_range[1]) / HPI_UNITS_PER_dB;
1815 uinfo->value.integer.step = ((int) gain_range[2]) / HPI_UNITS_PER_dB;
1816 return 0;
1817}
1818
1819static int snd_asihpi_tuner_gain_get(struct snd_kcontrol *kcontrol,
1820 struct snd_ctl_elem_value *ucontrol)
1821{
1822 /*
1823 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1824 */
1825 u32 h_control = kcontrol->private_value;
1826 short gain;
1827
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001828 hpi_handle_error(hpi_tuner_get_gain(h_control, &gain));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001829 ucontrol->value.integer.value[0] = gain / HPI_UNITS_PER_dB;
1830
1831 return 0;
1832}
1833
1834static int snd_asihpi_tuner_gain_put(struct snd_kcontrol *kcontrol,
1835 struct snd_ctl_elem_value *ucontrol)
1836{
1837 /*
1838 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1839 */
1840 u32 h_control = kcontrol->private_value;
1841 short gain;
1842
1843 gain = (ucontrol->value.integer.value[0]) * HPI_UNITS_PER_dB;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001844 hpi_handle_error(hpi_tuner_set_gain(h_control, gain));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001845
1846 return 1;
1847}
1848
1849/* Band */
1850
1851static int asihpi_tuner_band_query(struct snd_kcontrol *kcontrol,
1852 u16 *band_list, u32 len) {
1853 u32 h_control = kcontrol->private_value;
1854 u16 err = 0;
1855 u32 i;
1856
1857 for (i = 0; i < len; i++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001858 err = hpi_tuner_query_band(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001859 h_control, i, &band_list[i]);
1860 if (err != 0)
1861 break;
1862 }
1863
1864 if (err && (err != HPI_ERROR_INVALID_OBJ_INDEX))
1865 return -EIO;
1866
1867 return i;
1868}
1869
1870static int snd_asihpi_tuner_band_info(struct snd_kcontrol *kcontrol,
1871 struct snd_ctl_elem_info *uinfo)
1872{
1873 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1874 int num_bands = 0;
1875
1876 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1877 HPI_TUNER_BAND_LAST);
1878
1879 if (num_bands < 0)
1880 return num_bands;
1881
1882 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1883 uinfo->count = 1;
1884 uinfo->value.enumerated.items = num_bands;
1885
1886 if (num_bands > 0) {
1887 if (uinfo->value.enumerated.item >=
1888 uinfo->value.enumerated.items)
1889 uinfo->value.enumerated.item =
1890 uinfo->value.enumerated.items - 1;
1891
1892 strcpy(uinfo->value.enumerated.name,
1893 asihpi_tuner_band_names[
1894 tuner_bands[uinfo->value.enumerated.item]]);
1895
1896 }
1897 return 0;
1898}
1899
1900static int snd_asihpi_tuner_band_get(struct snd_kcontrol *kcontrol,
1901 struct snd_ctl_elem_value *ucontrol)
1902{
1903 u32 h_control = kcontrol->private_value;
1904 /*
1905 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1906 */
1907 u16 band, idx;
1908 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1909 u32 num_bands = 0;
1910
1911 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1912 HPI_TUNER_BAND_LAST);
1913
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001914 hpi_handle_error(hpi_tuner_get_band(h_control, &band));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001915
1916 ucontrol->value.enumerated.item[0] = -1;
1917 for (idx = 0; idx < HPI_TUNER_BAND_LAST; idx++)
1918 if (tuner_bands[idx] == band) {
1919 ucontrol->value.enumerated.item[0] = idx;
1920 break;
1921 }
1922
1923 return 0;
1924}
1925
1926static int snd_asihpi_tuner_band_put(struct snd_kcontrol *kcontrol,
1927 struct snd_ctl_elem_value *ucontrol)
1928{
1929 /*
1930 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1931 */
1932 u32 h_control = kcontrol->private_value;
1933 u16 band;
1934 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1935 u32 num_bands = 0;
1936
1937 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1938 HPI_TUNER_BAND_LAST);
1939
1940 band = tuner_bands[ucontrol->value.enumerated.item[0]];
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001941 hpi_handle_error(hpi_tuner_set_band(h_control, band));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001942
1943 return 1;
1944}
1945
1946/* Freq */
1947
1948static int snd_asihpi_tuner_freq_info(struct snd_kcontrol *kcontrol,
1949 struct snd_ctl_elem_info *uinfo)
1950{
1951 u32 h_control = kcontrol->private_value;
1952 u16 err;
1953 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1954 u16 num_bands = 0, band_iter, idx;
1955 u32 freq_range[3], temp_freq_range[3];
1956
1957 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1958 HPI_TUNER_BAND_LAST);
1959
1960 freq_range[0] = INT_MAX;
1961 freq_range[1] = 0;
1962 freq_range[2] = INT_MAX;
1963
1964 for (band_iter = 0; band_iter < num_bands; band_iter++) {
1965 for (idx = 0; idx < 3; idx++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001966 err = hpi_tuner_query_frequency(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001967 idx, tuner_bands[band_iter],
1968 &temp_freq_range[idx]);
1969 if (err != 0)
1970 return err;
1971 }
1972
1973 /* skip band with bogus stepping */
1974 if (temp_freq_range[2] <= 0)
1975 continue;
1976
1977 if (temp_freq_range[0] < freq_range[0])
1978 freq_range[0] = temp_freq_range[0];
1979 if (temp_freq_range[1] > freq_range[1])
1980 freq_range[1] = temp_freq_range[1];
1981 if (temp_freq_range[2] < freq_range[2])
1982 freq_range[2] = temp_freq_range[2];
1983 }
1984
1985 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1986 uinfo->count = 1;
1987 uinfo->value.integer.min = ((int)freq_range[0]);
1988 uinfo->value.integer.max = ((int)freq_range[1]);
1989 uinfo->value.integer.step = ((int)freq_range[2]);
1990 return 0;
1991}
1992
1993static int snd_asihpi_tuner_freq_get(struct snd_kcontrol *kcontrol,
1994 struct snd_ctl_elem_value *ucontrol)
1995{
1996 u32 h_control = kcontrol->private_value;
1997 u32 freq;
1998
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001999 hpi_handle_error(hpi_tuner_get_frequency(h_control, &freq));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002000 ucontrol->value.integer.value[0] = freq;
2001
2002 return 0;
2003}
2004
2005static int snd_asihpi_tuner_freq_put(struct snd_kcontrol *kcontrol,
2006 struct snd_ctl_elem_value *ucontrol)
2007{
2008 u32 h_control = kcontrol->private_value;
2009 u32 freq;
2010
2011 freq = ucontrol->value.integer.value[0];
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002012 hpi_handle_error(hpi_tuner_set_frequency(h_control, freq));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002013
2014 return 1;
2015}
2016
2017/* Tuner control group initializer */
2018static int __devinit snd_asihpi_tuner_add(struct snd_card_asihpi *asihpi,
2019 struct hpi_control *hpi_ctl)
2020{
2021 struct snd_card *card = asihpi->card;
2022 struct snd_kcontrol_new snd_control;
2023
2024 snd_control.private_value = hpi_ctl->h_control;
2025 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2026
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002027 if (!hpi_tuner_get_gain(hpi_ctl->h_control, NULL)) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002028 asihpi_ctl_init(&snd_control, hpi_ctl, "Gain");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002029 snd_control.info = snd_asihpi_tuner_gain_info;
2030 snd_control.get = snd_asihpi_tuner_gain_get;
2031 snd_control.put = snd_asihpi_tuner_gain_put;
2032
2033 if (ctl_add(card, &snd_control, asihpi) < 0)
2034 return -EINVAL;
2035 }
2036
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002037 asihpi_ctl_init(&snd_control, hpi_ctl, "Band");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002038 snd_control.info = snd_asihpi_tuner_band_info;
2039 snd_control.get = snd_asihpi_tuner_band_get;
2040 snd_control.put = snd_asihpi_tuner_band_put;
2041
2042 if (ctl_add(card, &snd_control, asihpi) < 0)
2043 return -EINVAL;
2044
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002045 asihpi_ctl_init(&snd_control, hpi_ctl, "Freq");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002046 snd_control.info = snd_asihpi_tuner_freq_info;
2047 snd_control.get = snd_asihpi_tuner_freq_get;
2048 snd_control.put = snd_asihpi_tuner_freq_put;
2049
2050 return ctl_add(card, &snd_control, asihpi);
2051}
2052
2053/*------------------------------------------------------------
2054 Meter controls
2055 ------------------------------------------------------------*/
2056static int snd_asihpi_meter_info(struct snd_kcontrol *kcontrol,
2057 struct snd_ctl_elem_info *uinfo)
2058{
2059 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2060 uinfo->count = HPI_MAX_CHANNELS;
2061 uinfo->value.integer.min = 0;
2062 uinfo->value.integer.max = 0x7FFFFFFF;
2063 return 0;
2064}
2065
2066/* linear values for 10dB steps */
2067static int log2lin[] = {
2068 0x7FFFFFFF, /* 0dB */
2069 679093956,
2070 214748365,
2071 67909396,
2072 21474837,
2073 6790940,
2074 2147484, /* -60dB */
2075 679094,
2076 214748, /* -80 */
2077 67909,
2078 21475, /* -100 */
2079 6791,
2080 2147,
2081 679,
2082 214,
2083 68,
2084 21,
2085 7,
2086 2
2087};
2088
2089static int snd_asihpi_meter_get(struct snd_kcontrol *kcontrol,
2090 struct snd_ctl_elem_value *ucontrol)
2091{
2092 u32 h_control = kcontrol->private_value;
2093 short an_gain_mB[HPI_MAX_CHANNELS], i;
2094 u16 err;
2095
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002096 err = hpi_meter_get_peak(h_control, an_gain_mB);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002097
2098 for (i = 0; i < HPI_MAX_CHANNELS; i++) {
2099 if (err) {
2100 ucontrol->value.integer.value[i] = 0;
2101 } else if (an_gain_mB[i] >= 0) {
2102 ucontrol->value.integer.value[i] =
2103 an_gain_mB[i] << 16;
2104 } else {
2105 /* -ve is log value in millibels < -60dB,
2106 * convert to (roughly!) linear,
2107 */
2108 ucontrol->value.integer.value[i] =
2109 log2lin[an_gain_mB[i] / -1000];
2110 }
2111 }
2112 return 0;
2113}
2114
2115static int __devinit snd_asihpi_meter_add(struct snd_card_asihpi *asihpi,
2116 struct hpi_control *hpi_ctl, int subidx)
2117{
2118 struct snd_card *card = asihpi->card;
2119 struct snd_kcontrol_new snd_control;
2120
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002121 asihpi_ctl_init(&snd_control, hpi_ctl, "Meter");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002122 snd_control.access =
2123 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
2124 snd_control.info = snd_asihpi_meter_info;
2125 snd_control.get = snd_asihpi_meter_get;
2126
2127 snd_control.index = subidx;
2128
2129 return ctl_add(card, &snd_control, asihpi);
2130}
2131
2132/*------------------------------------------------------------
2133 Multiplexer controls
2134 ------------------------------------------------------------*/
2135static int snd_card_asihpi_mux_count_sources(struct snd_kcontrol *snd_control)
2136{
2137 u32 h_control = snd_control->private_value;
2138 struct hpi_control hpi_ctl;
2139 int s, err;
2140 for (s = 0; s < 32; s++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002141 err = hpi_multiplexer_query_source(h_control, s,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002142 &hpi_ctl.
2143 src_node_type,
2144 &hpi_ctl.
2145 src_node_index);
2146 if (err)
2147 break;
2148 }
2149 return s;
2150}
2151
2152static int snd_asihpi_mux_info(struct snd_kcontrol *kcontrol,
2153 struct snd_ctl_elem_info *uinfo)
2154{
2155 int err;
2156 u16 src_node_type, src_node_index;
2157 u32 h_control = kcontrol->private_value;
2158
2159 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2160 uinfo->count = 1;
2161 uinfo->value.enumerated.items =
2162 snd_card_asihpi_mux_count_sources(kcontrol);
2163
2164 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2165 uinfo->value.enumerated.item =
2166 uinfo->value.enumerated.items - 1;
2167
2168 err =
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002169 hpi_multiplexer_query_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002170 uinfo->value.enumerated.item,
2171 &src_node_type, &src_node_index);
2172
2173 sprintf(uinfo->value.enumerated.name, "%s %d",
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12002174 asihpi_src_names[src_node_type - HPI_SOURCENODE_NONE],
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002175 src_node_index);
2176 return 0;
2177}
2178
2179static int snd_asihpi_mux_get(struct snd_kcontrol *kcontrol,
2180 struct snd_ctl_elem_value *ucontrol)
2181{
2182 u32 h_control = kcontrol->private_value;
2183 u16 source_type, source_index;
2184 u16 src_node_type, src_node_index;
2185 int s;
2186
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002187 hpi_handle_error(hpi_multiplexer_get_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002188 &source_type, &source_index));
2189 /* Should cache this search result! */
2190 for (s = 0; s < 256; s++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002191 if (hpi_multiplexer_query_source(h_control, s,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002192 &src_node_type, &src_node_index))
2193 break;
2194
2195 if ((source_type == src_node_type)
2196 && (source_index == src_node_index)) {
2197 ucontrol->value.enumerated.item[0] = s;
2198 return 0;
2199 }
2200 }
2201 snd_printd(KERN_WARNING
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002202 "Control %x failed to match mux source %hu %hu\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002203 h_control, source_type, source_index);
2204 ucontrol->value.enumerated.item[0] = 0;
2205 return 0;
2206}
2207
2208static int snd_asihpi_mux_put(struct snd_kcontrol *kcontrol,
2209 struct snd_ctl_elem_value *ucontrol)
2210{
2211 int change;
2212 u32 h_control = kcontrol->private_value;
2213 u16 source_type, source_index;
2214 u16 e;
2215
2216 change = 1;
2217
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002218 e = hpi_multiplexer_query_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002219 ucontrol->value.enumerated.item[0],
2220 &source_type, &source_index);
2221 if (!e)
2222 hpi_handle_error(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002223 hpi_multiplexer_set_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002224 source_type, source_index));
2225 return change;
2226}
2227
2228
2229static int __devinit snd_asihpi_mux_add(struct snd_card_asihpi *asihpi,
2230 struct hpi_control *hpi_ctl)
2231{
2232 struct snd_card *card = asihpi->card;
2233 struct snd_kcontrol_new snd_control;
2234
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002235 asihpi_ctl_init(&snd_control, hpi_ctl, "Route");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002236 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2237 snd_control.info = snd_asihpi_mux_info;
2238 snd_control.get = snd_asihpi_mux_get;
2239 snd_control.put = snd_asihpi_mux_put;
2240
2241 return ctl_add(card, &snd_control, asihpi);
2242
2243}
2244
2245/*------------------------------------------------------------
2246 Channel mode controls
2247 ------------------------------------------------------------*/
2248static int snd_asihpi_cmode_info(struct snd_kcontrol *kcontrol,
2249 struct snd_ctl_elem_info *uinfo)
2250{
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002251 static const char * const mode_names[HPI_CHANNEL_MODE_LAST + 1] = {
2252 "invalid",
2253 "Normal", "Swap",
2254 "From Left", "From Right",
2255 "To Left", "To Right"
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002256 };
2257
2258 u32 h_control = kcontrol->private_value;
2259 u16 mode;
2260 int i;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002261 u16 mode_map[6];
2262 int valid_modes = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002263
2264 /* HPI channel mode values can be from 1 to 6
2265 Some adapters only support a contiguous subset
2266 */
2267 for (i = 0; i < HPI_CHANNEL_MODE_LAST; i++)
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002268 if (!hpi_channel_mode_query_mode(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002269 h_control, i, &mode)) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002270 mode_map[valid_modes] = mode;
2271 valid_modes++;
2272 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002273
2274 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2275 uinfo->count = 1;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002276 uinfo->value.enumerated.items = valid_modes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002277
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002278 if (uinfo->value.enumerated.item >= valid_modes)
2279 uinfo->value.enumerated.item = valid_modes - 1;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002280
2281 strcpy(uinfo->value.enumerated.name,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002282 mode_names[mode_map[uinfo->value.enumerated.item]]);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002283
2284 return 0;
2285}
2286
2287static int snd_asihpi_cmode_get(struct snd_kcontrol *kcontrol,
2288 struct snd_ctl_elem_value *ucontrol)
2289{
2290 u32 h_control = kcontrol->private_value;
2291 u16 mode;
2292
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002293 if (hpi_channel_mode_get(h_control, &mode))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002294 mode = 1;
2295
2296 ucontrol->value.enumerated.item[0] = mode - 1;
2297
2298 return 0;
2299}
2300
2301static int snd_asihpi_cmode_put(struct snd_kcontrol *kcontrol,
2302 struct snd_ctl_elem_value *ucontrol)
2303{
2304 int change;
2305 u32 h_control = kcontrol->private_value;
2306
2307 change = 1;
2308
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002309 hpi_handle_error(hpi_channel_mode_set(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002310 ucontrol->value.enumerated.item[0] + 1));
2311 return change;
2312}
2313
2314
2315static int __devinit snd_asihpi_cmode_add(struct snd_card_asihpi *asihpi,
2316 struct hpi_control *hpi_ctl)
2317{
2318 struct snd_card *card = asihpi->card;
2319 struct snd_kcontrol_new snd_control;
2320
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002321 asihpi_ctl_init(&snd_control, hpi_ctl, "Mode");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002322 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2323 snd_control.info = snd_asihpi_cmode_info;
2324 snd_control.get = snd_asihpi_cmode_get;
2325 snd_control.put = snd_asihpi_cmode_put;
2326
2327 return ctl_add(card, &snd_control, asihpi);
2328}
2329
2330/*------------------------------------------------------------
2331 Sampleclock source controls
2332 ------------------------------------------------------------*/
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002333static char *sampleclock_sources[MAX_CLOCKSOURCES] = {
2334 "N/A", "Local PLL", "Digital Sync", "Word External", "Word Header",
2335 "SMPTE", "Digital1", "Auto", "Network", "Invalid",
2336 "Prev Module",
2337 "Digital2", "Digital3", "Digital4", "Digital5",
2338 "Digital6", "Digital7", "Digital8"};
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002339
2340static int snd_asihpi_clksrc_info(struct snd_kcontrol *kcontrol,
2341 struct snd_ctl_elem_info *uinfo)
2342{
2343 struct snd_card_asihpi *asihpi =
2344 (struct snd_card_asihpi *)(kcontrol->private_data);
2345 struct clk_cache *clkcache = &asihpi->cc;
2346 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2347 uinfo->count = 1;
2348 uinfo->value.enumerated.items = clkcache->count;
2349
2350 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2351 uinfo->value.enumerated.item =
2352 uinfo->value.enumerated.items - 1;
2353
2354 strcpy(uinfo->value.enumerated.name,
2355 clkcache->s[uinfo->value.enumerated.item].name);
2356 return 0;
2357}
2358
2359static int snd_asihpi_clksrc_get(struct snd_kcontrol *kcontrol,
2360 struct snd_ctl_elem_value *ucontrol)
2361{
2362 struct snd_card_asihpi *asihpi =
2363 (struct snd_card_asihpi *)(kcontrol->private_data);
2364 struct clk_cache *clkcache = &asihpi->cc;
2365 u32 h_control = kcontrol->private_value;
2366 u16 source, srcindex = 0;
2367 int i;
2368
2369 ucontrol->value.enumerated.item[0] = 0;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002370 if (hpi_sample_clock_get_source(h_control, &source))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002371 source = 0;
2372
2373 if (source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002374 if (hpi_sample_clock_get_source_index(h_control, &srcindex))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002375 srcindex = 0;
2376
2377 for (i = 0; i < clkcache->count; i++)
2378 if ((clkcache->s[i].source == source) &&
2379 (clkcache->s[i].index == srcindex))
2380 break;
2381
2382 ucontrol->value.enumerated.item[0] = i;
2383
2384 return 0;
2385}
2386
2387static int snd_asihpi_clksrc_put(struct snd_kcontrol *kcontrol,
2388 struct snd_ctl_elem_value *ucontrol)
2389{
2390 struct snd_card_asihpi *asihpi =
2391 (struct snd_card_asihpi *)(kcontrol->private_data);
2392 struct clk_cache *clkcache = &asihpi->cc;
2393 int change, item;
2394 u32 h_control = kcontrol->private_value;
2395
2396 change = 1;
2397 item = ucontrol->value.enumerated.item[0];
2398 if (item >= clkcache->count)
2399 item = clkcache->count-1;
2400
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002401 hpi_handle_error(hpi_sample_clock_set_source(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002402 h_control, clkcache->s[item].source));
2403
2404 if (clkcache->s[item].source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002405 hpi_handle_error(hpi_sample_clock_set_source_index(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002406 h_control, clkcache->s[item].index));
2407 return change;
2408}
2409
2410/*------------------------------------------------------------
2411 Clkrate controls
2412 ------------------------------------------------------------*/
2413/* Need to change this to enumerated control with list of rates */
2414static int snd_asihpi_clklocal_info(struct snd_kcontrol *kcontrol,
2415 struct snd_ctl_elem_info *uinfo)
2416{
2417 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2418 uinfo->count = 1;
2419 uinfo->value.integer.min = 8000;
2420 uinfo->value.integer.max = 192000;
2421 uinfo->value.integer.step = 100;
2422
2423 return 0;
2424}
2425
2426static int snd_asihpi_clklocal_get(struct snd_kcontrol *kcontrol,
2427 struct snd_ctl_elem_value *ucontrol)
2428{
2429 u32 h_control = kcontrol->private_value;
2430 u32 rate;
2431 u16 e;
2432
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002433 e = hpi_sample_clock_get_local_rate(h_control, &rate);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002434 if (!e)
2435 ucontrol->value.integer.value[0] = rate;
2436 else
2437 ucontrol->value.integer.value[0] = 0;
2438 return 0;
2439}
2440
2441static int snd_asihpi_clklocal_put(struct snd_kcontrol *kcontrol,
2442 struct snd_ctl_elem_value *ucontrol)
2443{
2444 int change;
2445 u32 h_control = kcontrol->private_value;
2446
2447 /* change = asihpi->mixer_clkrate[addr][0] != left ||
2448 asihpi->mixer_clkrate[addr][1] != right;
2449 */
2450 change = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002451 hpi_handle_error(hpi_sample_clock_set_local_rate(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002452 ucontrol->value.integer.value[0]));
2453 return change;
2454}
2455
2456static int snd_asihpi_clkrate_info(struct snd_kcontrol *kcontrol,
2457 struct snd_ctl_elem_info *uinfo)
2458{
2459 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2460 uinfo->count = 1;
2461 uinfo->value.integer.min = 8000;
2462 uinfo->value.integer.max = 192000;
2463 uinfo->value.integer.step = 100;
2464
2465 return 0;
2466}
2467
2468static int snd_asihpi_clkrate_get(struct snd_kcontrol *kcontrol,
2469 struct snd_ctl_elem_value *ucontrol)
2470{
2471 u32 h_control = kcontrol->private_value;
2472 u32 rate;
2473 u16 e;
2474
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002475 e = hpi_sample_clock_get_sample_rate(h_control, &rate);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002476 if (!e)
2477 ucontrol->value.integer.value[0] = rate;
2478 else
2479 ucontrol->value.integer.value[0] = 0;
2480 return 0;
2481}
2482
2483static int __devinit snd_asihpi_sampleclock_add(struct snd_card_asihpi *asihpi,
2484 struct hpi_control *hpi_ctl)
2485{
2486 struct snd_card *card = asihpi->card;
2487 struct snd_kcontrol_new snd_control;
2488
2489 struct clk_cache *clkcache = &asihpi->cc;
2490 u32 hSC = hpi_ctl->h_control;
2491 int has_aes_in = 0;
2492 int i, j;
2493 u16 source;
2494
2495 snd_control.private_value = hpi_ctl->h_control;
2496
2497 clkcache->has_local = 0;
2498
2499 for (i = 0; i <= HPI_SAMPLECLOCK_SOURCE_LAST; i++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002500 if (hpi_sample_clock_query_source(hSC,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002501 i, &source))
2502 break;
2503 clkcache->s[i].source = source;
2504 clkcache->s[i].index = 0;
2505 clkcache->s[i].name = sampleclock_sources[source];
2506 if (source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
2507 has_aes_in = 1;
2508 if (source == HPI_SAMPLECLOCK_SOURCE_LOCAL)
2509 clkcache->has_local = 1;
2510 }
2511 if (has_aes_in)
2512 /* already will have picked up index 0 above */
2513 for (j = 1; j < 8; j++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002514 if (hpi_sample_clock_query_source_index(hSC,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002515 j, HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT,
2516 &source))
2517 break;
2518 clkcache->s[i].source =
2519 HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT;
2520 clkcache->s[i].index = j;
2521 clkcache->s[i].name = sampleclock_sources[
2522 j+HPI_SAMPLECLOCK_SOURCE_LAST];
2523 i++;
2524 }
2525 clkcache->count = i;
2526
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002527 asihpi_ctl_init(&snd_control, hpi_ctl, "Source");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002528 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE ;
2529 snd_control.info = snd_asihpi_clksrc_info;
2530 snd_control.get = snd_asihpi_clksrc_get;
2531 snd_control.put = snd_asihpi_clksrc_put;
2532 if (ctl_add(card, &snd_control, asihpi) < 0)
2533 return -EINVAL;
2534
2535
2536 if (clkcache->has_local) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002537 asihpi_ctl_init(&snd_control, hpi_ctl, "Localrate");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002538 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE ;
2539 snd_control.info = snd_asihpi_clklocal_info;
2540 snd_control.get = snd_asihpi_clklocal_get;
2541 snd_control.put = snd_asihpi_clklocal_put;
2542
2543
2544 if (ctl_add(card, &snd_control, asihpi) < 0)
2545 return -EINVAL;
2546 }
2547
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002548 asihpi_ctl_init(&snd_control, hpi_ctl, "Rate");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002549 snd_control.access =
2550 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
2551 snd_control.info = snd_asihpi_clkrate_info;
2552 snd_control.get = snd_asihpi_clkrate_get;
2553
2554 return ctl_add(card, &snd_control, asihpi);
2555}
2556/*------------------------------------------------------------
2557 Mixer
2558 ------------------------------------------------------------*/
2559
2560static int __devinit snd_card_asihpi_mixer_new(struct snd_card_asihpi *asihpi)
2561{
2562 struct snd_card *card = asihpi->card;
2563 unsigned int idx = 0;
2564 unsigned int subindex = 0;
2565 int err;
2566 struct hpi_control hpi_ctl, prev_ctl;
2567
2568 if (snd_BUG_ON(!asihpi))
2569 return -EINVAL;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002570 strcpy(card->mixername, "Asihpi Mixer");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002571
2572 err =
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002573 hpi_mixer_open(asihpi->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002574 &asihpi->h_mixer);
2575 hpi_handle_error(err);
2576 if (err)
2577 return -err;
2578
Takashi Iwai21896bc2010-06-02 12:08:37 +02002579 memset(&prev_ctl, 0, sizeof(prev_ctl));
2580 prev_ctl.control_type = -1;
2581
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002582 for (idx = 0; idx < 2000; idx++) {
2583 err = hpi_mixer_get_control_by_index(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002584 asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002585 idx,
2586 &hpi_ctl.src_node_type,
2587 &hpi_ctl.src_node_index,
2588 &hpi_ctl.dst_node_type,
2589 &hpi_ctl.dst_node_index,
2590 &hpi_ctl.control_type,
2591 &hpi_ctl.h_control);
2592 if (err) {
2593 if (err == HPI_ERROR_CONTROL_DISABLED) {
2594 if (mixer_dump)
2595 snd_printk(KERN_INFO
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002596 "Disabled HPI Control(%d)\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002597 idx);
2598 continue;
2599 } else
2600 break;
2601
2602 }
2603
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12002604 hpi_ctl.src_node_type -= HPI_SOURCENODE_NONE;
2605 hpi_ctl.dst_node_type -= HPI_DESTNODE_NONE;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002606
2607 /* ASI50xx in SSX mode has multiple meters on the same node.
2608 Use subindex to create distinct ALSA controls
2609 for any duplicated controls.
2610 */
2611 if ((hpi_ctl.control_type == prev_ctl.control_type) &&
2612 (hpi_ctl.src_node_type == prev_ctl.src_node_type) &&
2613 (hpi_ctl.src_node_index == prev_ctl.src_node_index) &&
2614 (hpi_ctl.dst_node_type == prev_ctl.dst_node_type) &&
2615 (hpi_ctl.dst_node_index == prev_ctl.dst_node_index))
2616 subindex++;
2617 else
2618 subindex = 0;
2619
2620 prev_ctl = hpi_ctl;
2621
2622 switch (hpi_ctl.control_type) {
2623 case HPI_CONTROL_VOLUME:
2624 err = snd_asihpi_volume_add(asihpi, &hpi_ctl);
2625 break;
2626 case HPI_CONTROL_LEVEL:
2627 err = snd_asihpi_level_add(asihpi, &hpi_ctl);
2628 break;
2629 case HPI_CONTROL_MULTIPLEXER:
2630 err = snd_asihpi_mux_add(asihpi, &hpi_ctl);
2631 break;
2632 case HPI_CONTROL_CHANNEL_MODE:
2633 err = snd_asihpi_cmode_add(asihpi, &hpi_ctl);
2634 break;
2635 case HPI_CONTROL_METER:
2636 err = snd_asihpi_meter_add(asihpi, &hpi_ctl, subindex);
2637 break;
2638 case HPI_CONTROL_SAMPLECLOCK:
2639 err = snd_asihpi_sampleclock_add(
2640 asihpi, &hpi_ctl);
2641 break;
2642 case HPI_CONTROL_CONNECTION: /* ignore these */
2643 continue;
2644 case HPI_CONTROL_TUNER:
2645 err = snd_asihpi_tuner_add(asihpi, &hpi_ctl);
2646 break;
2647 case HPI_CONTROL_AESEBU_TRANSMITTER:
2648 err = snd_asihpi_aesebu_tx_add(asihpi, &hpi_ctl);
2649 break;
2650 case HPI_CONTROL_AESEBU_RECEIVER:
2651 err = snd_asihpi_aesebu_rx_add(asihpi, &hpi_ctl);
2652 break;
2653 case HPI_CONTROL_VOX:
2654 case HPI_CONTROL_BITSTREAM:
2655 case HPI_CONTROL_MICROPHONE:
2656 case HPI_CONTROL_PARAMETRIC_EQ:
2657 case HPI_CONTROL_COMPANDER:
2658 default:
2659 if (mixer_dump)
2660 snd_printk(KERN_INFO
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002661 "Untranslated HPI Control"
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002662 "(%d) %d %d %d %d %d\n",
2663 idx,
2664 hpi_ctl.control_type,
2665 hpi_ctl.src_node_type,
2666 hpi_ctl.src_node_index,
2667 hpi_ctl.dst_node_type,
2668 hpi_ctl.dst_node_index);
2669 continue;
2670 };
2671 if (err < 0)
2672 return err;
2673 }
2674 if (HPI_ERROR_INVALID_OBJ_INDEX != err)
2675 hpi_handle_error(err);
2676
2677 snd_printk(KERN_INFO "%d mixer controls found\n", idx);
2678
2679 return 0;
2680}
2681
2682/*------------------------------------------------------------
2683 /proc interface
2684 ------------------------------------------------------------*/
2685
2686static void
2687snd_asihpi_proc_read(struct snd_info_entry *entry,
2688 struct snd_info_buffer *buffer)
2689{
2690 struct snd_card_asihpi *asihpi = entry->private_data;
2691 u16 version;
2692 u32 h_control;
2693 u32 rate = 0;
2694 u16 source = 0;
2695 int err;
2696
2697 snd_iprintf(buffer, "ASIHPI driver proc file\n");
2698 snd_iprintf(buffer,
2699 "adapter ID=%4X\n_index=%d\n"
2700 "num_outstreams=%d\n_num_instreams=%d\n",
2701 asihpi->type, asihpi->adapter_index,
2702 asihpi->num_outstreams, asihpi->num_instreams);
2703
2704 version = asihpi->version;
2705 snd_iprintf(buffer,
2706 "serial#=%d\n_hw version %c%d\nDSP code version %03d\n",
2707 asihpi->serial_number, ((version >> 3) & 0xf) + 'A',
2708 version & 0x7,
2709 ((version >> 13) * 100) + ((version >> 7) & 0x3f));
2710
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002711 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002712 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
2713 HPI_CONTROL_SAMPLECLOCK, &h_control);
2714
2715 if (!err) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002716 err = hpi_sample_clock_get_sample_rate(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002717 h_control, &rate);
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002718 err += hpi_sample_clock_get_source(h_control, &source);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002719
2720 if (!err)
2721 snd_iprintf(buffer, "sample_clock=%d_hz, source %s\n",
2722 rate, sampleclock_sources[source]);
2723 }
2724
2725}
2726
2727
2728static void __devinit snd_asihpi_proc_init(struct snd_card_asihpi *asihpi)
2729{
2730 struct snd_info_entry *entry;
2731
2732 if (!snd_card_proc_new(asihpi->card, "info", &entry))
2733 snd_info_set_text_ops(entry, asihpi, snd_asihpi_proc_read);
2734}
2735
2736/*------------------------------------------------------------
2737 HWDEP
2738 ------------------------------------------------------------*/
2739
2740static int snd_asihpi_hpi_open(struct snd_hwdep *hw, struct file *file)
2741{
2742 if (enable_hpi_hwdep)
2743 return 0;
2744 else
2745 return -ENODEV;
2746
2747}
2748
2749static int snd_asihpi_hpi_release(struct snd_hwdep *hw, struct file *file)
2750{
2751 if (enable_hpi_hwdep)
2752 return asihpi_hpi_release(file);
2753 else
2754 return -ENODEV;
2755}
2756
2757static int snd_asihpi_hpi_ioctl(struct snd_hwdep *hw, struct file *file,
2758 unsigned int cmd, unsigned long arg)
2759{
2760 if (enable_hpi_hwdep)
2761 return asihpi_hpi_ioctl(file, cmd, arg);
2762 else
2763 return -ENODEV;
2764}
2765
2766
2767/* results in /dev/snd/hwC#D0 file for each card with index #
2768 also /proc/asound/hwdep will contain '#-00: asihpi (HPI) for each card'
2769*/
2770static int __devinit snd_asihpi_hpi_new(struct snd_card_asihpi *asihpi,
2771 int device, struct snd_hwdep **rhwdep)
2772{
2773 struct snd_hwdep *hw;
2774 int err;
2775
2776 if (rhwdep)
2777 *rhwdep = NULL;
2778 err = snd_hwdep_new(asihpi->card, "HPI", device, &hw);
2779 if (err < 0)
2780 return err;
2781 strcpy(hw->name, "asihpi (HPI)");
2782 hw->iface = SNDRV_HWDEP_IFACE_LAST;
2783 hw->ops.open = snd_asihpi_hpi_open;
2784 hw->ops.ioctl = snd_asihpi_hpi_ioctl;
2785 hw->ops.release = snd_asihpi_hpi_release;
2786 hw->private_data = asihpi;
2787 if (rhwdep)
2788 *rhwdep = hw;
2789 return 0;
2790}
2791
2792/*------------------------------------------------------------
2793 CARD
2794 ------------------------------------------------------------*/
2795static int __devinit snd_asihpi_probe(struct pci_dev *pci_dev,
2796 const struct pci_device_id *pci_id)
2797{
2798 int err;
2799
2800 u16 version;
2801 int pcm_substreams;
2802
2803 struct hpi_adapter *hpi_card;
2804 struct snd_card *card;
2805 struct snd_card_asihpi *asihpi;
2806
2807 u32 h_control;
2808 u32 h_stream;
2809
2810 static int dev;
2811 if (dev >= SNDRV_CARDS)
2812 return -ENODEV;
2813
2814 /* Should this be enable[hpi_card->index] ? */
2815 if (!enable[dev]) {
2816 dev++;
2817 return -ENOENT;
2818 }
2819
2820 err = asihpi_adapter_probe(pci_dev, pci_id);
2821 if (err < 0)
2822 return err;
2823
2824 hpi_card = pci_get_drvdata(pci_dev);
2825 /* first try to give the card the same index as its hardware index */
2826 err = snd_card_create(hpi_card->index,
2827 id[hpi_card->index], THIS_MODULE,
2828 sizeof(struct snd_card_asihpi),
2829 &card);
2830 if (err < 0) {
2831 /* if that fails, try the default index==next available */
2832 err =
2833 snd_card_create(index[dev], id[dev],
2834 THIS_MODULE,
2835 sizeof(struct snd_card_asihpi),
2836 &card);
2837 if (err < 0)
2838 return err;
2839 snd_printk(KERN_WARNING
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002840 "**** WARNING **** Adapter index %d->ALSA index %d\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002841 hpi_card->index, card->number);
2842 }
2843
Eliot Blennerhassett12253672011-02-10 17:26:10 +13002844 snd_card_set_dev(card, &pci_dev->dev);
2845
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002846 asihpi = (struct snd_card_asihpi *) card->private_data;
2847 asihpi->card = card;
Eliot Blennerhassett12253672011-02-10 17:26:10 +13002848 asihpi->pci = pci_dev;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002849 asihpi->adapter_index = hpi_card->index;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002850 hpi_handle_error(hpi_adapter_get_info(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002851 asihpi->adapter_index,
2852 &asihpi->num_outstreams,
2853 &asihpi->num_instreams,
2854 &asihpi->version,
2855 &asihpi->serial_number, &asihpi->type));
2856
2857 version = asihpi->version;
2858 snd_printk(KERN_INFO "adapter ID=%4X index=%d num_outstreams=%d "
2859 "num_instreams=%d S/N=%d\n"
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002860 "Hw Version %c%d DSP code version %03d\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002861 asihpi->type, asihpi->adapter_index,
2862 asihpi->num_outstreams,
2863 asihpi->num_instreams, asihpi->serial_number,
2864 ((version >> 3) & 0xf) + 'A',
2865 version & 0x7,
2866 ((version >> 13) * 100) + ((version >> 7) & 0x3f));
2867
2868 pcm_substreams = asihpi->num_outstreams;
2869 if (pcm_substreams < asihpi->num_instreams)
2870 pcm_substreams = asihpi->num_instreams;
2871
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002872 err = hpi_adapter_get_property(asihpi->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002873 HPI_ADAPTER_PROPERTY_CAPS1,
2874 NULL, &asihpi->support_grouping);
2875 if (err)
2876 asihpi->support_grouping = 0;
2877
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002878 err = hpi_adapter_get_property(asihpi->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002879 HPI_ADAPTER_PROPERTY_CAPS2,
2880 &asihpi->support_mrx, NULL);
2881 if (err)
2882 asihpi->support_mrx = 0;
2883
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002884 err = hpi_adapter_get_property(asihpi->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002885 HPI_ADAPTER_PROPERTY_INTERVAL,
2886 NULL, &asihpi->update_interval_frames);
2887 if (err)
2888 asihpi->update_interval_frames = 512;
2889
Eliot Blennerhassett26aebef2011-03-25 15:25:47 +13002890 if (!asihpi->support_mmap)
2891 asihpi->update_interval_frames *= 2;
2892
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002893 hpi_handle_error(hpi_instream_open(asihpi->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002894 0, &h_stream));
2895
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002896 err = hpi_instream_host_buffer_free(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002897 asihpi->support_mmap = (!err);
2898
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002899 hpi_handle_error(hpi_instream_close(h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002900
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002901 err = hpi_adapter_get_property(asihpi->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002902 HPI_ADAPTER_PROPERTY_CURCHANNELS,
2903 &asihpi->in_max_chans, &asihpi->out_max_chans);
2904 if (err) {
2905 asihpi->in_max_chans = 2;
2906 asihpi->out_max_chans = 2;
2907 }
2908
2909 snd_printk(KERN_INFO "supports mmap:%d grouping:%d mrx:%d\n",
2910 asihpi->support_mmap,
2911 asihpi->support_grouping,
2912 asihpi->support_mrx
2913 );
2914
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002915 err = snd_card_asihpi_pcm_new(asihpi, 0, pcm_substreams);
2916 if (err < 0) {
2917 snd_printk(KERN_ERR "pcm_new failed\n");
2918 goto __nodev;
2919 }
2920 err = snd_card_asihpi_mixer_new(asihpi);
2921 if (err < 0) {
2922 snd_printk(KERN_ERR "mixer_new failed\n");
2923 goto __nodev;
2924 }
2925
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002926 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002927 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
2928 HPI_CONTROL_SAMPLECLOCK, &h_control);
2929
2930 if (!err)
2931 err = hpi_sample_clock_set_local_rate(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002932 h_control, adapter_fs);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002933
2934 snd_asihpi_proc_init(asihpi);
2935
2936 /* always create, can be enabled or disabled dynamically
2937 by enable_hwdep module param*/
2938 snd_asihpi_hpi_new(asihpi, 0, NULL);
2939
2940 if (asihpi->support_mmap)
2941 strcpy(card->driver, "ASIHPI-MMAP");
2942 else
2943 strcpy(card->driver, "ASIHPI");
2944
2945 sprintf(card->shortname, "AudioScience ASI%4X", asihpi->type);
2946 sprintf(card->longname, "%s %i",
2947 card->shortname, asihpi->adapter_index);
2948 err = snd_card_register(card);
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13002949
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002950 if (!err) {
2951 hpi_card->snd_card_asihpi = card;
2952 dev++;
2953 return 0;
2954 }
2955__nodev:
2956 snd_card_free(card);
2957 snd_printk(KERN_ERR "snd_asihpi_probe error %d\n", err);
2958 return err;
2959
2960}
2961
2962static void __devexit snd_asihpi_remove(struct pci_dev *pci_dev)
2963{
2964 struct hpi_adapter *hpi_card = pci_get_drvdata(pci_dev);
2965
2966 snd_card_free(hpi_card->snd_card_asihpi);
2967 hpi_card->snd_card_asihpi = NULL;
2968 asihpi_adapter_remove(pci_dev);
2969}
2970
2971static DEFINE_PCI_DEVICE_TABLE(asihpi_pci_tbl) = {
2972 {HPI_PCI_VENDOR_ID_TI, HPI_PCI_DEV_ID_DSP6205,
2973 HPI_PCI_VENDOR_ID_AUDIOSCIENCE, PCI_ANY_ID, 0, 0,
2974 (kernel_ulong_t)HPI_6205},
2975 {HPI_PCI_VENDOR_ID_TI, HPI_PCI_DEV_ID_PCI2040,
2976 HPI_PCI_VENDOR_ID_AUDIOSCIENCE, PCI_ANY_ID, 0, 0,
2977 (kernel_ulong_t)HPI_6000},
2978 {0,}
2979};
2980MODULE_DEVICE_TABLE(pci, asihpi_pci_tbl);
2981
2982static struct pci_driver driver = {
2983 .name = "asihpi",
2984 .id_table = asihpi_pci_tbl,
2985 .probe = snd_asihpi_probe,
2986 .remove = __devexit_p(snd_asihpi_remove),
2987#ifdef CONFIG_PM
2988/* .suspend = snd_asihpi_suspend,
2989 .resume = snd_asihpi_resume, */
2990#endif
2991};
2992
2993static int __init snd_asihpi_init(void)
2994{
2995 asihpi_init();
2996 return pci_register_driver(&driver);
2997}
2998
2999static void __exit snd_asihpi_exit(void)
3000{
3001
3002 pci_unregister_driver(&driver);
3003 asihpi_exit();
3004}
3005
3006module_init(snd_asihpi_init)
3007module_exit(snd_asihpi_exit)
3008