2ca6f4f85b412a18502ac0f6370d19cc604d7d56
[deliverable/linux.git] / sound / pci / asihpi / asihpi.c
1 /*
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 */
24
25 #include "hpi_internal.h"
26 #include "hpimsginit.h"
27 #include "hpioctl.h"
28
29 #include <linux/pci.h>
30 #include <linux/version.h>
31 #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
45
46 MODULE_LICENSE("GPL");
47 MODULE_AUTHOR("AudioScience inc. <support@audioscience.com>");
48 MODULE_DESCRIPTION("AudioScience ALSA ASI5000 ASI6000 ASI87xx ASI89xx");
49
50 #if defined CONFIG_SND_DEBUG
51 /* copied from pcm_lib.c, hope later patch will make that version public
52 and this copy can be removed */
53 static void pcm_debug_name(struct snd_pcm_substream *substream,
54 char *name, size_t len)
55 {
56 snprintf(name, len, "pcmC%dD%d%c:%d",
57 substream->pcm->card->number,
58 substream->pcm->device,
59 substream->stream ? 'c' : 'p',
60 substream->number);
61 }
62 #define DEBUG_NAME(substream, name) char name[16]; pcm_debug_name(substream, name, sizeof(name))
63 #else
64 #define pcm_debug_name(s, n, l) do { } while (0)
65 #define DEBUG_NAME(name, substream) do { } while (0)
66 #endif
67
68 #if defined CONFIG_SND_DEBUG_VERBOSE
69 /**
70 * snd_printddd - very verbose debug printk
71 * @format: format string
72 *
73 * Works like snd_printk() for debugging purposes.
74 * Ignored when CONFIG_SND_DEBUG_VERBOSE is not set.
75 * Must set snd module debug parameter to 3 to enable at runtime.
76 */
77 #define snd_printddd(format, args...) \
78 __snd_printk(3, __FILE__, __LINE__, format, ##args)
79 #else
80 #define snd_printddd(format, args...) do { } while (0)
81 #endif
82
83 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* index 0-MAX */
84 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
85 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
86 static int enable_hpi_hwdep = 1;
87
88 module_param_array(index, int, NULL, S_IRUGO);
89 MODULE_PARM_DESC(index, "ALSA index value for AudioScience soundcard.");
90
91 module_param_array(id, charp, NULL, S_IRUGO);
92 MODULE_PARM_DESC(id, "ALSA ID string for AudioScience soundcard.");
93
94 module_param_array(enable, bool, NULL, S_IRUGO);
95 MODULE_PARM_DESC(enable, "ALSA enable AudioScience soundcard.");
96
97 module_param(enable_hpi_hwdep, bool, S_IRUGO|S_IWUSR);
98 MODULE_PARM_DESC(enable_hpi_hwdep,
99 "ALSA enable HPI hwdep for AudioScience soundcard ");
100
101 /* identify driver */
102 #ifdef KERNEL_ALSA_BUILD
103 static char *build_info = "Built using headers from kernel source";
104 module_param(build_info, charp, S_IRUGO);
105 MODULE_PARM_DESC(build_info, "built using headers from kernel source");
106 #else
107 static char *build_info = "Built within ALSA source";
108 module_param(build_info, charp, S_IRUGO);
109 MODULE_PARM_DESC(build_info, "built within ALSA source");
110 #endif
111
112 /* set to 1 to dump every control from adapter to log */
113 static const int mixer_dump;
114
115 #define DEFAULT_SAMPLERATE 44100
116 static int adapter_fs = DEFAULT_SAMPLERATE;
117
118 /* defaults */
119 #define PERIODS_MIN 2
120 #define PERIOD_BYTES_MIN 2048
121 #define BUFFER_BYTES_MAX (512 * 1024)
122
123 #define MAX_CLOCKSOURCES (HPI_SAMPLECLOCK_SOURCE_LAST + 1 + 7)
124
125 struct clk_source {
126 int source;
127 int index;
128 char *name;
129 };
130
131 struct clk_cache {
132 int count;
133 int has_local;
134 struct clk_source s[MAX_CLOCKSOURCES];
135 };
136
137 /* Per card data */
138 struct snd_card_asihpi {
139 struct snd_card *card;
140 struct pci_dev *pci;
141 u16 adapter_index;
142 u32 serial_number;
143 u16 type;
144 u16 version;
145 u16 num_outstreams;
146 u16 num_instreams;
147
148 u32 h_mixer;
149 struct clk_cache cc;
150
151 u16 can_dma;
152 u16 support_grouping;
153 u16 support_mrx;
154 u16 update_interval_frames;
155 u16 in_max_chans;
156 u16 out_max_chans;
157 };
158
159 /* Per stream data */
160 struct snd_card_asihpi_pcm {
161 struct timer_list timer;
162 unsigned int respawn_timer;
163 unsigned int hpi_buffer_attached;
164 unsigned int buffer_bytes;
165 unsigned int period_bytes;
166 unsigned int bytes_per_sec;
167 unsigned int pcm_buf_host_rw_ofs; /* Host R/W pos */
168 unsigned int pcm_buf_dma_ofs; /* DMA R/W offset in buffer */
169 unsigned int pcm_buf_elapsed_dma_ofs; /* DMA R/W offset in buffer */
170 unsigned int drained_count;
171 struct snd_pcm_substream *substream;
172 u32 h_stream;
173 struct hpi_format format;
174 };
175
176 /* universal stream verbs work with out or in stream handles */
177
178 /* Functions to allow driver to give a buffer to HPI for busmastering */
179
180 static u16 hpi_stream_host_buffer_attach(
181 u32 h_stream, /* handle to outstream. */
182 u32 size_in_bytes, /* size in bytes of bus mastering buffer */
183 u32 pci_address
184 )
185 {
186 struct hpi_message hm;
187 struct hpi_response hr;
188 unsigned int obj = hpi_handle_object(h_stream);
189
190 if (!h_stream)
191 return HPI_ERROR_INVALID_OBJ;
192 hpi_init_message_response(&hm, &hr, obj,
193 obj == HPI_OBJ_OSTREAM ?
194 HPI_OSTREAM_HOSTBUFFER_ALLOC :
195 HPI_ISTREAM_HOSTBUFFER_ALLOC);
196
197 hpi_handle_to_indexes(h_stream, &hm.adapter_index,
198 &hm.obj_index);
199
200 hm.u.d.u.buffer.buffer_size = size_in_bytes;
201 hm.u.d.u.buffer.pci_address = pci_address;
202 hm.u.d.u.buffer.command = HPI_BUFFER_CMD_INTERNAL_GRANTADAPTER;
203 hpi_send_recv(&hm, &hr);
204 return hr.error;
205 }
206
207 static u16 hpi_stream_host_buffer_detach(u32 h_stream)
208 {
209 struct hpi_message hm;
210 struct hpi_response hr;
211 unsigned int obj = hpi_handle_object(h_stream);
212
213 if (!h_stream)
214 return HPI_ERROR_INVALID_OBJ;
215
216 hpi_init_message_response(&hm, &hr, obj,
217 obj == HPI_OBJ_OSTREAM ?
218 HPI_OSTREAM_HOSTBUFFER_FREE :
219 HPI_ISTREAM_HOSTBUFFER_FREE);
220
221 hpi_handle_to_indexes(h_stream, &hm.adapter_index,
222 &hm.obj_index);
223 hm.u.d.u.buffer.command = HPI_BUFFER_CMD_INTERNAL_REVOKEADAPTER;
224 hpi_send_recv(&hm, &hr);
225 return hr.error;
226 }
227
228 static inline u16 hpi_stream_start(u32 h_stream)
229 {
230 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
231 return hpi_outstream_start(h_stream);
232 else
233 return hpi_instream_start(h_stream);
234 }
235
236 static inline u16 hpi_stream_stop(u32 h_stream)
237 {
238 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
239 return hpi_outstream_stop(h_stream);
240 else
241 return hpi_instream_stop(h_stream);
242 }
243
244 static inline u16 hpi_stream_get_info_ex(
245 u32 h_stream,
246 u16 *pw_state,
247 u32 *pbuffer_size,
248 u32 *pdata_in_buffer,
249 u32 *psample_count,
250 u32 *pauxiliary_data
251 )
252 {
253 u16 e;
254 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
255 e = hpi_outstream_get_info_ex(h_stream, pw_state,
256 pbuffer_size, pdata_in_buffer,
257 psample_count, pauxiliary_data);
258 else
259 e = hpi_instream_get_info_ex(h_stream, pw_state,
260 pbuffer_size, pdata_in_buffer,
261 psample_count, pauxiliary_data);
262 return e;
263 }
264
265 static inline u16 hpi_stream_group_add(
266 u32 h_master,
267 u32 h_stream)
268 {
269 if (hpi_handle_object(h_master) == HPI_OBJ_OSTREAM)
270 return hpi_outstream_group_add(h_master, h_stream);
271 else
272 return hpi_instream_group_add(h_master, h_stream);
273 }
274
275 static inline u16 hpi_stream_group_reset(u32 h_stream)
276 {
277 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
278 return hpi_outstream_group_reset(h_stream);
279 else
280 return hpi_instream_group_reset(h_stream);
281 }
282
283 static inline u16 hpi_stream_group_get_map(
284 u32 h_stream, u32 *mo, u32 *mi)
285 {
286 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
287 return hpi_outstream_group_get_map(h_stream, mo, mi);
288 else
289 return hpi_instream_group_get_map(h_stream, mo, mi);
290 }
291
292 static u16 handle_error(u16 err, int line, char *filename)
293 {
294 if (err)
295 printk(KERN_WARNING
296 "in file %s, line %d: HPI error %d\n",
297 filename, line, err);
298 return err;
299 }
300
301 #define hpi_handle_error(x) handle_error(x, __LINE__, __FILE__)
302
303 /***************************** GENERAL PCM ****************/
304
305 static void print_hwparams(struct snd_pcm_substream *substream,
306 struct snd_pcm_hw_params *p)
307 {
308 DEBUG_NAME(substream, name);
309 snd_printd("%s HWPARAMS\n", name);
310 snd_printd(" samplerate %d Hz\n", params_rate(p));
311 snd_printd(" channels %d\n", params_channels(p));
312 snd_printd(" format %d\n", params_format(p));
313 snd_printd(" subformat %d\n", params_subformat(p));
314 snd_printd(" buffer %d B\n", params_buffer_bytes(p));
315 snd_printd(" period %d B\n", params_period_bytes(p));
316 snd_printd(" access %d\n", params_access(p));
317 snd_printd(" period_size %d\n", params_period_size(p));
318 snd_printd(" periods %d\n", params_periods(p));
319 snd_printd(" buffer_size %d\n", params_buffer_size(p));
320 snd_printd(" %d B/s\n", params_rate(p) *
321 params_channels(p) *
322 snd_pcm_format_width(params_format(p)) / 8);
323
324 }
325
326 static snd_pcm_format_t hpi_to_alsa_formats[] = {
327 -1, /* INVALID */
328 SNDRV_PCM_FORMAT_U8, /* HPI_FORMAT_PCM8_UNSIGNED 1 */
329 SNDRV_PCM_FORMAT_S16, /* HPI_FORMAT_PCM16_SIGNED 2 */
330 -1, /* HPI_FORMAT_MPEG_L1 3 */
331 SNDRV_PCM_FORMAT_MPEG, /* HPI_FORMAT_MPEG_L2 4 */
332 SNDRV_PCM_FORMAT_MPEG, /* HPI_FORMAT_MPEG_L3 5 */
333 -1, /* HPI_FORMAT_DOLBY_AC2 6 */
334 -1, /* HPI_FORMAT_DOLBY_AC3 7 */
335 SNDRV_PCM_FORMAT_S16_BE,/* HPI_FORMAT_PCM16_BIGENDIAN 8 */
336 -1, /* HPI_FORMAT_AA_TAGIT1_HITS 9 */
337 -1, /* HPI_FORMAT_AA_TAGIT1_INSERTS 10 */
338 SNDRV_PCM_FORMAT_S32, /* HPI_FORMAT_PCM32_SIGNED 11 */
339 -1, /* HPI_FORMAT_RAW_BITSTREAM 12 */
340 -1, /* HPI_FORMAT_AA_TAGIT1_HITS_EX1 13 */
341 SNDRV_PCM_FORMAT_FLOAT, /* HPI_FORMAT_PCM32_FLOAT 14 */
342 #if 1
343 /* ALSA can't handle 3 byte sample size together with power-of-2
344 * constraint on buffer_bytes, so disable this format
345 */
346 -1
347 #else
348 /* SNDRV_PCM_FORMAT_S24_3LE */ /* HPI_FORMAT_PCM24_SIGNED 15 */
349 #endif
350 };
351
352
353 static int snd_card_asihpi_format_alsa2hpi(snd_pcm_format_t alsa_format,
354 u16 *hpi_format)
355 {
356 u16 format;
357
358 for (format = HPI_FORMAT_PCM8_UNSIGNED;
359 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
360 if (hpi_to_alsa_formats[format] == alsa_format) {
361 *hpi_format = format;
362 return 0;
363 }
364 }
365
366 snd_printd(KERN_WARNING "failed match for alsa format %d\n",
367 alsa_format);
368 *hpi_format = 0;
369 return -EINVAL;
370 }
371
372 static void snd_card_asihpi_pcm_samplerates(struct snd_card_asihpi *asihpi,
373 struct snd_pcm_hardware *pcmhw)
374 {
375 u16 err;
376 u32 h_control;
377 u32 sample_rate;
378 int idx;
379 unsigned int rate_min = 200000;
380 unsigned int rate_max = 0;
381 unsigned int rates = 0;
382
383 if (asihpi->support_mrx) {
384 rates |= SNDRV_PCM_RATE_CONTINUOUS;
385 rates |= SNDRV_PCM_RATE_8000_96000;
386 rate_min = 8000;
387 rate_max = 100000;
388 } else {
389 /* on cards without SRC,
390 valid rates are determined by sampleclock */
391 err = hpi_mixer_get_control(asihpi->h_mixer,
392 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
393 HPI_CONTROL_SAMPLECLOCK, &h_control);
394 if (err) {
395 snd_printk(KERN_ERR
396 "No local sampleclock, err %d\n", err);
397 }
398
399 for (idx = -1; idx < 100; idx++) {
400 if (idx == -1) {
401 if (hpi_sample_clock_get_sample_rate(h_control,
402 &sample_rate))
403 continue;
404 } else if (hpi_sample_clock_query_local_rate(h_control,
405 idx, &sample_rate)) {
406 break;
407 }
408
409 rate_min = min(rate_min, sample_rate);
410 rate_max = max(rate_max, sample_rate);
411
412 switch (sample_rate) {
413 case 5512:
414 rates |= SNDRV_PCM_RATE_5512;
415 break;
416 case 8000:
417 rates |= SNDRV_PCM_RATE_8000;
418 break;
419 case 11025:
420 rates |= SNDRV_PCM_RATE_11025;
421 break;
422 case 16000:
423 rates |= SNDRV_PCM_RATE_16000;
424 break;
425 case 22050:
426 rates |= SNDRV_PCM_RATE_22050;
427 break;
428 case 32000:
429 rates |= SNDRV_PCM_RATE_32000;
430 break;
431 case 44100:
432 rates |= SNDRV_PCM_RATE_44100;
433 break;
434 case 48000:
435 rates |= SNDRV_PCM_RATE_48000;
436 break;
437 case 64000:
438 rates |= SNDRV_PCM_RATE_64000;
439 break;
440 case 88200:
441 rates |= SNDRV_PCM_RATE_88200;
442 break;
443 case 96000:
444 rates |= SNDRV_PCM_RATE_96000;
445 break;
446 case 176400:
447 rates |= SNDRV_PCM_RATE_176400;
448 break;
449 case 192000:
450 rates |= SNDRV_PCM_RATE_192000;
451 break;
452 default: /* some other rate */
453 rates |= SNDRV_PCM_RATE_KNOT;
454 }
455 }
456 }
457
458 pcmhw->rates = rates;
459 pcmhw->rate_min = rate_min;
460 pcmhw->rate_max = rate_max;
461 }
462
463 static int snd_card_asihpi_pcm_hw_params(struct snd_pcm_substream *substream,
464 struct snd_pcm_hw_params *params)
465 {
466 struct snd_pcm_runtime *runtime = substream->runtime;
467 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
468 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
469 int err;
470 u16 format;
471 int width;
472 unsigned int bytes_per_sec;
473
474 print_hwparams(substream, params);
475 err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
476 if (err < 0)
477 return err;
478 err = snd_card_asihpi_format_alsa2hpi(params_format(params), &format);
479 if (err)
480 return err;
481
482 hpi_handle_error(hpi_format_create(&dpcm->format,
483 params_channels(params),
484 format, params_rate(params), 0, 0));
485
486 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
487 if (hpi_instream_reset(dpcm->h_stream) != 0)
488 return -EINVAL;
489
490 if (hpi_instream_set_format(
491 dpcm->h_stream, &dpcm->format) != 0)
492 return -EINVAL;
493 }
494
495 dpcm->hpi_buffer_attached = 0;
496 if (card->can_dma) {
497 err = hpi_stream_host_buffer_attach(dpcm->h_stream,
498 params_buffer_bytes(params), runtime->dma_addr);
499 if (err == 0) {
500 snd_printdd(
501 "stream_host_buffer_attach succeeded %u %lu\n",
502 params_buffer_bytes(params),
503 (unsigned long)runtime->dma_addr);
504 } else {
505 snd_printd("stream_host_buffer_attach error %d\n",
506 err);
507 return -ENOMEM;
508 }
509
510 err = hpi_stream_get_info_ex(dpcm->h_stream, NULL,
511 &dpcm->hpi_buffer_attached,
512 NULL, NULL, NULL);
513
514 snd_printdd("stream_host_buffer_attach status 0x%x\n",
515 dpcm->hpi_buffer_attached);
516 }
517 bytes_per_sec = params_rate(params) * params_channels(params);
518 width = snd_pcm_format_width(params_format(params));
519 bytes_per_sec *= width;
520 bytes_per_sec /= 8;
521 if (width < 0 || bytes_per_sec == 0)
522 return -EINVAL;
523
524 dpcm->bytes_per_sec = bytes_per_sec;
525 dpcm->buffer_bytes = params_buffer_bytes(params);
526 dpcm->period_bytes = params_period_bytes(params);
527
528 return 0;
529 }
530
531 static int
532 snd_card_asihpi_hw_free(struct snd_pcm_substream *substream)
533 {
534 struct snd_pcm_runtime *runtime = substream->runtime;
535 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
536 if (dpcm->hpi_buffer_attached)
537 hpi_stream_host_buffer_detach(dpcm->h_stream);
538
539 snd_pcm_lib_free_pages(substream);
540 return 0;
541 }
542
543 static void snd_card_asihpi_runtime_free(struct snd_pcm_runtime *runtime)
544 {
545 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
546 kfree(dpcm);
547 }
548
549 static void snd_card_asihpi_pcm_timer_start(struct snd_pcm_substream *
550 substream)
551 {
552 struct snd_pcm_runtime *runtime = substream->runtime;
553 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
554 int expiry;
555
556 expiry = HZ / 200;
557 /*? (dpcm->period_bytes * HZ / dpcm->bytes_per_sec); */
558 expiry = max(expiry, 1); /* don't let it be zero! */
559 dpcm->timer.expires = jiffies + expiry;
560 dpcm->respawn_timer = 1;
561 add_timer(&dpcm->timer);
562 }
563
564 static void snd_card_asihpi_pcm_timer_stop(struct snd_pcm_substream *substream)
565 {
566 struct snd_pcm_runtime *runtime = substream->runtime;
567 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
568
569 dpcm->respawn_timer = 0;
570 del_timer(&dpcm->timer);
571 }
572
573 static int snd_card_asihpi_trigger(struct snd_pcm_substream *substream,
574 int cmd)
575 {
576 struct snd_card_asihpi_pcm *dpcm = substream->runtime->private_data;
577 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
578 struct snd_pcm_substream *s;
579 u16 e;
580 DEBUG_NAME(substream, name);
581
582 snd_printdd("%s trigger\n", name);
583
584 switch (cmd) {
585 case SNDRV_PCM_TRIGGER_START:
586 snd_pcm_group_for_each_entry(s, substream) {
587 struct snd_pcm_runtime *runtime = s->runtime;
588 struct snd_card_asihpi_pcm *ds = runtime->private_data;
589
590 if (snd_pcm_substream_chip(s) != card)
591 continue;
592
593 /* don't link Cap and Play */
594 if (substream->stream != s->stream)
595 continue;
596
597 ds->drained_count = 0;
598 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
599 /* How do I know how much valid data is present
600 * in buffer? Must be at least one period!
601 * Guessing 2 periods, but if
602 * buffer is bigger it may contain even more
603 * data??
604 */
605 unsigned int preload = ds->period_bytes * 1;
606 snd_printddd("%d preload x%x\n", s->number, preload);
607 hpi_handle_error(hpi_outstream_write_buf(
608 ds->h_stream,
609 &runtime->dma_area[0],
610 preload,
611 &ds->format));
612 ds->pcm_buf_host_rw_ofs = preload;
613 }
614
615 if (card->support_grouping) {
616 snd_printdd("%d group\n", s->number);
617 e = hpi_stream_group_add(
618 dpcm->h_stream,
619 ds->h_stream);
620 if (!e) {
621 snd_pcm_trigger_done(s, substream);
622 } else {
623 hpi_handle_error(e);
624 break;
625 }
626 } else
627 break;
628 }
629 snd_printdd("start\n");
630 /* start the master stream */
631 snd_card_asihpi_pcm_timer_start(substream);
632 if ((substream->stream == SNDRV_PCM_STREAM_CAPTURE) ||
633 !card->can_dma)
634 hpi_handle_error(hpi_stream_start(dpcm->h_stream));
635 break;
636
637 case SNDRV_PCM_TRIGGER_STOP:
638 snd_card_asihpi_pcm_timer_stop(substream);
639 snd_pcm_group_for_each_entry(s, substream) {
640 if (snd_pcm_substream_chip(s) != card)
641 continue;
642 /* don't link Cap and Play */
643 if (substream->stream != s->stream)
644 continue;
645
646 /*? workaround linked streams don't
647 transition to SETUP 20070706*/
648 s->runtime->status->state = SNDRV_PCM_STATE_SETUP;
649
650 if (card->support_grouping) {
651 snd_printdd("%d group\n", s->number);
652 snd_pcm_trigger_done(s, substream);
653 } else
654 break;
655 }
656 snd_printdd("stop\n");
657
658 /* _prepare and _hwparams reset the stream */
659 hpi_handle_error(hpi_stream_stop(dpcm->h_stream));
660 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
661 hpi_handle_error(
662 hpi_outstream_reset(dpcm->h_stream));
663
664 if (card->support_grouping)
665 hpi_handle_error(hpi_stream_group_reset(dpcm->h_stream));
666 break;
667
668 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
669 snd_printdd("pause release\n");
670 hpi_handle_error(hpi_stream_start(dpcm->h_stream));
671 snd_card_asihpi_pcm_timer_start(substream);
672 break;
673 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
674 snd_printdd("pause\n");
675 snd_card_asihpi_pcm_timer_stop(substream);
676 hpi_handle_error(hpi_stream_stop(dpcm->h_stream));
677 break;
678 default:
679 snd_printd(KERN_ERR "\tINVALID\n");
680 return -EINVAL;
681 }
682
683 return 0;
684 }
685
686 /*algorithm outline
687 Without linking degenerates to getting single stream pos etc
688 Without mmap 2nd loop degenerates to snd_pcm_period_elapsed
689 */
690 /*
691 pcm_buf_dma_ofs=get_buf_pos(s);
692 for_each_linked_stream(s) {
693 pcm_buf_dma_ofs=get_buf_pos(s);
694 min_buf_pos = modulo_min(min_buf_pos, pcm_buf_dma_ofs, buffer_bytes)
695 new_data = min(new_data, calc_new_data(pcm_buf_dma_ofs,irq_pos)
696 }
697 timer.expires = jiffies + predict_next_period_ready(min_buf_pos);
698 for_each_linked_stream(s) {
699 s->pcm_buf_dma_ofs = min_buf_pos;
700 if (new_data > period_bytes) {
701 if (mmap) {
702 irq_pos = (irq_pos + period_bytes) % buffer_bytes;
703 if (playback) {
704 write(period_bytes);
705 } else {
706 read(period_bytes);
707 }
708 }
709 snd_pcm_period_elapsed(s);
710 }
711 }
712 */
713
714 /** Minimum of 2 modulo values. Works correctly when the difference between
715 * the values is less than half the modulus
716 */
717 static inline unsigned int modulo_min(unsigned int a, unsigned int b,
718 unsigned long int modulus)
719 {
720 unsigned int result;
721 if (((a-b) % modulus) < (modulus/2))
722 result = b;
723 else
724 result = a;
725
726 return result;
727 }
728
729 /** Timer function, equivalent to interrupt service routine for cards
730 */
731 static void snd_card_asihpi_timer_function(unsigned long data)
732 {
733 struct snd_card_asihpi_pcm *dpcm = (struct snd_card_asihpi_pcm *)data;
734 struct snd_pcm_substream *substream = dpcm->substream;
735 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
736 struct snd_pcm_runtime *runtime;
737 struct snd_pcm_substream *s;
738 unsigned int newdata = 0;
739 unsigned int pcm_buf_dma_ofs, min_buf_pos = 0;
740 unsigned int remdata, xfercount, next_jiffies;
741 int first = 1;
742 int loops = 0;
743 u16 state;
744 u32 buffer_size, bytes_avail, samples_played, on_card_bytes;
745 DEBUG_NAME(substream, name);
746
747 snd_printdd("%s snd_card_asihpi_timer_function\n", name);
748
749 /* find minimum newdata and buffer pos in group */
750 snd_pcm_group_for_each_entry(s, substream) {
751 struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
752 runtime = s->runtime;
753
754 if (snd_pcm_substream_chip(s) != card)
755 continue;
756
757 /* don't link Cap and Play */
758 if (substream->stream != s->stream)
759 continue;
760
761 hpi_handle_error(hpi_stream_get_info_ex(
762 ds->h_stream, &state,
763 &buffer_size, &bytes_avail,
764 &samples_played, &on_card_bytes));
765
766 /* number of bytes in on-card buffer */
767 runtime->delay = on_card_bytes;
768
769 if (!card->can_dma)
770 on_card_bytes = bytes_avail;
771
772 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
773 pcm_buf_dma_ofs = ds->pcm_buf_host_rw_ofs - bytes_avail;
774 if (state == HPI_STATE_STOPPED) {
775 if ((bytes_avail == 0) &&
776 (on_card_bytes < ds->pcm_buf_host_rw_ofs)) {
777 hpi_handle_error(hpi_stream_start(ds->h_stream));
778 snd_printdd("P%d start\n", s->number);
779 ds->drained_count = 0;
780 }
781 } else if (state == HPI_STATE_DRAINED) {
782 snd_printd(KERN_WARNING "P%d drained\n",
783 s->number);
784 ds->drained_count++;
785 if (ds->drained_count > 2) {
786 snd_pcm_stop(s, SNDRV_PCM_STATE_XRUN);
787 continue;
788 }
789 } else {
790 ds->drained_count = 0;
791 }
792 } else
793 pcm_buf_dma_ofs = bytes_avail + ds->pcm_buf_host_rw_ofs;
794
795 if (first) {
796 /* can't statically init min when wrap is involved */
797 min_buf_pos = pcm_buf_dma_ofs;
798 newdata = (pcm_buf_dma_ofs - ds->pcm_buf_elapsed_dma_ofs) % ds->buffer_bytes;
799 first = 0;
800 } else {
801 min_buf_pos =
802 modulo_min(min_buf_pos, pcm_buf_dma_ofs, UINT_MAX+1L);
803 newdata = min(
804 (pcm_buf_dma_ofs - ds->pcm_buf_elapsed_dma_ofs) % ds->buffer_bytes,
805 newdata);
806 }
807
808 snd_printdd("hw_ptr 0x%04lX, appl_ptr 0x%04lX\n",
809 (unsigned long)frames_to_bytes(runtime,
810 runtime->status->hw_ptr),
811 (unsigned long)frames_to_bytes(runtime,
812 runtime->control->appl_ptr));
813
814 snd_printdd("%d S=%d, "
815 "rw=0x%04X, dma=0x%04X, left=0x%04X, "
816 "aux=0x%04X space=0x%04X\n",
817 s->number, state,
818 ds->pcm_buf_host_rw_ofs, pcm_buf_dma_ofs,
819 (int)bytes_avail,
820 (int)on_card_bytes, buffer_size-bytes_avail);
821 loops++;
822 }
823 pcm_buf_dma_ofs = min_buf_pos;
824
825 remdata = newdata % dpcm->period_bytes;
826 xfercount = newdata - remdata; /* a multiple of period_bytes */
827 /* come back when on_card_bytes has decreased enough to allow
828 write to happen, or when data has been consumed to make another
829 period
830 */
831 if (xfercount && (on_card_bytes > dpcm->period_bytes))
832 next_jiffies = ((on_card_bytes - dpcm->period_bytes) * HZ / dpcm->bytes_per_sec);
833 else
834 next_jiffies = ((dpcm->period_bytes - remdata) * HZ / dpcm->bytes_per_sec);
835
836 next_jiffies = max(next_jiffies, 1U);
837 dpcm->timer.expires = jiffies + next_jiffies;
838 snd_printdd("jif %d buf pos 0x%04X newdata 0x%04X xfer 0x%04X\n",
839 next_jiffies, pcm_buf_dma_ofs, newdata, xfercount);
840
841 snd_pcm_group_for_each_entry(s, substream) {
842 struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
843
844 /* don't link Cap and Play */
845 if (substream->stream != s->stream)
846 continue;
847
848 ds->pcm_buf_dma_ofs = pcm_buf_dma_ofs;
849
850 if (xfercount &&
851 /* Limit use of on card fifo for playback */
852 ((on_card_bytes <= ds->period_bytes) ||
853 (s->stream == SNDRV_PCM_STREAM_CAPTURE)))
854
855 {
856
857 unsigned int buf_ofs = ds->pcm_buf_host_rw_ofs % ds->buffer_bytes;
858 unsigned int xfer1, xfer2;
859 char *pd = &s->runtime->dma_area[buf_ofs];
860
861 if (card->can_dma) { /* buffer wrap is handled at lower level */
862 xfer1 = xfercount;
863 xfer2 = 0;
864 } else {
865 xfer1 = min(xfercount, ds->buffer_bytes - buf_ofs);
866 xfer2 = xfercount - xfer1;
867 }
868
869 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
870 snd_printddd("P%d write1 0x%04X 0x%04X\n",
871 s->number, xfer1, buf_ofs);
872 hpi_handle_error(
873 hpi_outstream_write_buf(
874 ds->h_stream, pd, xfer1,
875 &ds->format));
876
877 if (xfer2) {
878 pd = s->runtime->dma_area;
879
880 snd_printddd("P%d write2 0x%04X 0x%04X\n",
881 s->number,
882 xfercount - xfer1, buf_ofs);
883 hpi_handle_error(
884 hpi_outstream_write_buf(
885 ds->h_stream, pd,
886 xfercount - xfer1,
887 &ds->format));
888 }
889 } else {
890 snd_printddd("C%d read1 0x%04x\n",
891 s->number, xfer1);
892 hpi_handle_error(
893 hpi_instream_read_buf(
894 ds->h_stream,
895 pd, xfer1));
896 if (xfer2) {
897 pd = s->runtime->dma_area;
898 snd_printddd("C%d read2 0x%04x\n",
899 s->number, xfer2);
900 hpi_handle_error(
901 hpi_instream_read_buf(
902 ds->h_stream,
903 pd, xfer2));
904 }
905 }
906 ds->pcm_buf_host_rw_ofs = ds->pcm_buf_host_rw_ofs + xfercount;
907 ds->pcm_buf_elapsed_dma_ofs = pcm_buf_dma_ofs;
908 snd_pcm_period_elapsed(s);
909 }
910 }
911
912 if (dpcm->respawn_timer)
913 add_timer(&dpcm->timer);
914 }
915
916 /***************************** PLAYBACK OPS ****************/
917 static int snd_card_asihpi_playback_ioctl(struct snd_pcm_substream *substream,
918 unsigned int cmd, void *arg)
919 {
920 snd_printddd(KERN_INFO "P%d ioctl %d\n", substream->number, cmd);
921 return snd_pcm_lib_ioctl(substream, cmd, arg);
922 }
923
924 static int snd_card_asihpi_playback_prepare(struct snd_pcm_substream *
925 substream)
926 {
927 struct snd_pcm_runtime *runtime = substream->runtime;
928 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
929
930 snd_printdd("P%d prepare\n", substream->number);
931
932 hpi_handle_error(hpi_outstream_reset(dpcm->h_stream));
933 dpcm->pcm_buf_host_rw_ofs = 0;
934 dpcm->pcm_buf_dma_ofs = 0;
935 dpcm->pcm_buf_elapsed_dma_ofs = 0;
936 return 0;
937 }
938
939 static snd_pcm_uframes_t
940 snd_card_asihpi_playback_pointer(struct snd_pcm_substream *substream)
941 {
942 struct snd_pcm_runtime *runtime = substream->runtime;
943 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
944 snd_pcm_uframes_t ptr;
945
946 ptr = bytes_to_frames(runtime, dpcm->pcm_buf_dma_ofs % dpcm->buffer_bytes);
947 snd_printddd("P%d pointer = 0x%04lx\n", substream->number, (unsigned long)ptr);
948 return ptr;
949 }
950
951 static void snd_card_asihpi_playback_format(struct snd_card_asihpi *asihpi,
952 u32 h_stream,
953 struct snd_pcm_hardware *pcmhw)
954 {
955 struct hpi_format hpi_format;
956 u16 format;
957 u16 err;
958 u32 h_control;
959 u32 sample_rate = 48000;
960
961 /* on cards without SRC, must query at valid rate,
962 * maybe set by external sync
963 */
964 err = hpi_mixer_get_control(asihpi->h_mixer,
965 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
966 HPI_CONTROL_SAMPLECLOCK, &h_control);
967
968 if (!err)
969 err = hpi_sample_clock_get_sample_rate(h_control,
970 &sample_rate);
971
972 for (format = HPI_FORMAT_PCM8_UNSIGNED;
973 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
974 err = hpi_format_create(&hpi_format,
975 2, format, sample_rate, 128000, 0);
976 if (!err)
977 err = hpi_outstream_query_format(h_stream,
978 &hpi_format);
979 if (!err && (hpi_to_alsa_formats[format] != -1))
980 pcmhw->formats |=
981 (1ULL << hpi_to_alsa_formats[format]);
982 }
983 }
984
985 static struct snd_pcm_hardware snd_card_asihpi_playback = {
986 .channels_min = 1,
987 .channels_max = 2,
988 .buffer_bytes_max = BUFFER_BYTES_MAX,
989 .period_bytes_min = PERIOD_BYTES_MIN,
990 .period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN,
991 .periods_min = PERIODS_MIN,
992 .periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN,
993 .fifo_size = 0,
994 };
995
996 static int snd_card_asihpi_playback_open(struct snd_pcm_substream *substream)
997 {
998 struct snd_pcm_runtime *runtime = substream->runtime;
999 struct snd_card_asihpi_pcm *dpcm;
1000 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
1001 int err;
1002
1003 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
1004 if (dpcm == NULL)
1005 return -ENOMEM;
1006
1007 err =
1008 hpi_outstream_open(card->adapter_index,
1009 substream->number, &dpcm->h_stream);
1010 hpi_handle_error(err);
1011 if (err)
1012 kfree(dpcm);
1013 if (err == HPI_ERROR_OBJ_ALREADY_OPEN)
1014 return -EBUSY;
1015 if (err)
1016 return -EIO;
1017
1018 /*? also check ASI5000 samplerate source
1019 If external, only support external rate.
1020 If internal and other stream playing, can't switch
1021 */
1022
1023 init_timer(&dpcm->timer);
1024 dpcm->timer.data = (unsigned long) dpcm;
1025 dpcm->timer.function = snd_card_asihpi_timer_function;
1026 dpcm->substream = substream;
1027 runtime->private_data = dpcm;
1028 runtime->private_free = snd_card_asihpi_runtime_free;
1029
1030 snd_card_asihpi_playback.channels_max = card->out_max_chans;
1031 /*?snd_card_asihpi_playback.period_bytes_min =
1032 card->out_max_chans * 4096; */
1033
1034 snd_card_asihpi_playback_format(card, dpcm->h_stream,
1035 &snd_card_asihpi_playback);
1036
1037 snd_card_asihpi_pcm_samplerates(card, &snd_card_asihpi_playback);
1038
1039 snd_card_asihpi_playback.info = SNDRV_PCM_INFO_INTERLEAVED |
1040 SNDRV_PCM_INFO_DOUBLE |
1041 SNDRV_PCM_INFO_BATCH |
1042 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1043 SNDRV_PCM_INFO_PAUSE |
1044 SNDRV_PCM_INFO_MMAP |
1045 SNDRV_PCM_INFO_MMAP_VALID;
1046
1047 if (card->support_grouping)
1048 snd_card_asihpi_playback.info |= SNDRV_PCM_INFO_SYNC_START;
1049
1050 /* struct is copied, so can create initializer dynamically */
1051 runtime->hw = snd_card_asihpi_playback;
1052
1053 if (card->can_dma)
1054 err = snd_pcm_hw_constraint_pow2(runtime, 0,
1055 SNDRV_PCM_HW_PARAM_BUFFER_BYTES);
1056 if (err < 0)
1057 return err;
1058
1059 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1060 card->update_interval_frames);
1061
1062 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1063 card->update_interval_frames * 2, UINT_MAX);
1064
1065 snd_pcm_set_sync(substream);
1066
1067 snd_printdd("playback open\n");
1068
1069 return 0;
1070 }
1071
1072 static int snd_card_asihpi_playback_close(struct snd_pcm_substream *substream)
1073 {
1074 struct snd_pcm_runtime *runtime = substream->runtime;
1075 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1076
1077 hpi_handle_error(hpi_outstream_close(dpcm->h_stream));
1078 snd_printdd("playback close\n");
1079
1080 return 0;
1081 }
1082
1083 static struct snd_pcm_ops snd_card_asihpi_playback_mmap_ops = {
1084 .open = snd_card_asihpi_playback_open,
1085 .close = snd_card_asihpi_playback_close,
1086 .ioctl = snd_card_asihpi_playback_ioctl,
1087 .hw_params = snd_card_asihpi_pcm_hw_params,
1088 .hw_free = snd_card_asihpi_hw_free,
1089 .prepare = snd_card_asihpi_playback_prepare,
1090 .trigger = snd_card_asihpi_trigger,
1091 .pointer = snd_card_asihpi_playback_pointer,
1092 };
1093
1094 /***************************** CAPTURE OPS ****************/
1095 static snd_pcm_uframes_t
1096 snd_card_asihpi_capture_pointer(struct snd_pcm_substream *substream)
1097 {
1098 struct snd_pcm_runtime *runtime = substream->runtime;
1099 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1100
1101 snd_printddd("capture pointer %d=%d\n",
1102 substream->number, dpcm->pcm_buf_dma_ofs);
1103 /* NOTE Unlike playback can't use actual samples_played
1104 for the capture position, because those samples aren't yet in
1105 the local buffer available for reading.
1106 */
1107 return bytes_to_frames(runtime, dpcm->pcm_buf_dma_ofs % dpcm->buffer_bytes);
1108 }
1109
1110 static int snd_card_asihpi_capture_ioctl(struct snd_pcm_substream *substream,
1111 unsigned int cmd, void *arg)
1112 {
1113 return snd_pcm_lib_ioctl(substream, cmd, arg);
1114 }
1115
1116 static int snd_card_asihpi_capture_prepare(struct snd_pcm_substream *substream)
1117 {
1118 struct snd_pcm_runtime *runtime = substream->runtime;
1119 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1120
1121 hpi_handle_error(hpi_instream_reset(dpcm->h_stream));
1122 dpcm->pcm_buf_host_rw_ofs = 0;
1123 dpcm->pcm_buf_dma_ofs = 0;
1124 dpcm->pcm_buf_elapsed_dma_ofs = 0;
1125
1126 snd_printdd("Capture Prepare %d\n", substream->number);
1127 return 0;
1128 }
1129
1130
1131
1132 static void snd_card_asihpi_capture_format(struct snd_card_asihpi *asihpi,
1133 u32 h_stream,
1134 struct snd_pcm_hardware *pcmhw)
1135 {
1136 struct hpi_format hpi_format;
1137 u16 format;
1138 u16 err;
1139 u32 h_control;
1140 u32 sample_rate = 48000;
1141
1142 /* on cards without SRC, must query at valid rate,
1143 maybe set by external sync */
1144 err = hpi_mixer_get_control(asihpi->h_mixer,
1145 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
1146 HPI_CONTROL_SAMPLECLOCK, &h_control);
1147
1148 if (!err)
1149 err = hpi_sample_clock_get_sample_rate(h_control,
1150 &sample_rate);
1151
1152 for (format = HPI_FORMAT_PCM8_UNSIGNED;
1153 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
1154
1155 err = hpi_format_create(&hpi_format, 2, format,
1156 sample_rate, 128000, 0);
1157 if (!err)
1158 err = hpi_instream_query_format(h_stream,
1159 &hpi_format);
1160 if (!err)
1161 pcmhw->formats |=
1162 (1ULL << hpi_to_alsa_formats[format]);
1163 }
1164 }
1165
1166
1167 static struct snd_pcm_hardware snd_card_asihpi_capture = {
1168 .channels_min = 1,
1169 .channels_max = 2,
1170 .buffer_bytes_max = BUFFER_BYTES_MAX,
1171 .period_bytes_min = PERIOD_BYTES_MIN,
1172 .period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN,
1173 .periods_min = PERIODS_MIN,
1174 .periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN,
1175 .fifo_size = 0,
1176 };
1177
1178 static int snd_card_asihpi_capture_open(struct snd_pcm_substream *substream)
1179 {
1180 struct snd_pcm_runtime *runtime = substream->runtime;
1181 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
1182 struct snd_card_asihpi_pcm *dpcm;
1183 int err;
1184
1185 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
1186 if (dpcm == NULL)
1187 return -ENOMEM;
1188
1189 snd_printdd("capture open adapter %d stream %d\n",
1190 card->adapter_index, substream->number);
1191
1192 err = hpi_handle_error(
1193 hpi_instream_open(card->adapter_index,
1194 substream->number, &dpcm->h_stream));
1195 if (err)
1196 kfree(dpcm);
1197 if (err == HPI_ERROR_OBJ_ALREADY_OPEN)
1198 return -EBUSY;
1199 if (err)
1200 return -EIO;
1201
1202
1203 init_timer(&dpcm->timer);
1204 dpcm->timer.data = (unsigned long) dpcm;
1205 dpcm->timer.function = snd_card_asihpi_timer_function;
1206 dpcm->substream = substream;
1207 runtime->private_data = dpcm;
1208 runtime->private_free = snd_card_asihpi_runtime_free;
1209
1210 snd_card_asihpi_capture.channels_max = card->in_max_chans;
1211 snd_card_asihpi_capture_format(card, dpcm->h_stream,
1212 &snd_card_asihpi_capture);
1213 snd_card_asihpi_pcm_samplerates(card, &snd_card_asihpi_capture);
1214 snd_card_asihpi_capture.info = SNDRV_PCM_INFO_INTERLEAVED |
1215 SNDRV_PCM_INFO_MMAP |
1216 SNDRV_PCM_INFO_MMAP_VALID;
1217
1218 if (card->support_grouping)
1219 snd_card_asihpi_capture.info |= SNDRV_PCM_INFO_SYNC_START;
1220
1221 runtime->hw = snd_card_asihpi_capture;
1222
1223 if (card->can_dma)
1224 err = snd_pcm_hw_constraint_pow2(runtime, 0,
1225 SNDRV_PCM_HW_PARAM_BUFFER_BYTES);
1226 if (err < 0)
1227 return err;
1228
1229 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1230 card->update_interval_frames);
1231 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1232 card->update_interval_frames * 2, UINT_MAX);
1233
1234 snd_pcm_set_sync(substream);
1235
1236 return 0;
1237 }
1238
1239 static int snd_card_asihpi_capture_close(struct snd_pcm_substream *substream)
1240 {
1241 struct snd_card_asihpi_pcm *dpcm = substream->runtime->private_data;
1242
1243 hpi_handle_error(hpi_instream_close(dpcm->h_stream));
1244 return 0;
1245 }
1246
1247 static struct snd_pcm_ops snd_card_asihpi_capture_mmap_ops = {
1248 .open = snd_card_asihpi_capture_open,
1249 .close = snd_card_asihpi_capture_close,
1250 .ioctl = snd_card_asihpi_capture_ioctl,
1251 .hw_params = snd_card_asihpi_pcm_hw_params,
1252 .hw_free = snd_card_asihpi_hw_free,
1253 .prepare = snd_card_asihpi_capture_prepare,
1254 .trigger = snd_card_asihpi_trigger,
1255 .pointer = snd_card_asihpi_capture_pointer,
1256 };
1257
1258 static int __devinit snd_card_asihpi_pcm_new(struct snd_card_asihpi *asihpi,
1259 int device, int substreams)
1260 {
1261 struct snd_pcm *pcm;
1262 int err;
1263
1264 err = snd_pcm_new(asihpi->card, "Asihpi PCM", device,
1265 asihpi->num_outstreams, asihpi->num_instreams,
1266 &pcm);
1267 if (err < 0)
1268 return err;
1269 /* pointer to ops struct is stored, dont change ops afterwards! */
1270 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1271 &snd_card_asihpi_playback_mmap_ops);
1272 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1273 &snd_card_asihpi_capture_mmap_ops);
1274
1275 pcm->private_data = asihpi;
1276 pcm->info_flags = 0;
1277 strcpy(pcm->name, "Asihpi PCM");
1278
1279 /*? do we want to emulate MMAP for non-BBM cards?
1280 Jack doesn't work with ALSAs MMAP emulation - WHY NOT? */
1281 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1282 snd_dma_pci_data(asihpi->pci),
1283 64*1024, BUFFER_BYTES_MAX);
1284
1285 return 0;
1286 }
1287
1288 /***************************** MIXER CONTROLS ****************/
1289 struct hpi_control {
1290 u32 h_control;
1291 u16 control_type;
1292 u16 src_node_type;
1293 u16 src_node_index;
1294 u16 dst_node_type;
1295 u16 dst_node_index;
1296 u16 band;
1297 char name[44]; /* copied to snd_ctl_elem_id.name[44]; */
1298 };
1299
1300 static const char * const asihpi_tuner_band_names[] = {
1301 "invalid",
1302 "AM",
1303 "FM mono",
1304 "TV NTSC-M",
1305 "FM stereo",
1306 "AUX",
1307 "TV PAL BG",
1308 "TV PAL I",
1309 "TV PAL DK",
1310 "TV SECAM",
1311 };
1312
1313 compile_time_assert(
1314 (ARRAY_SIZE(asihpi_tuner_band_names) ==
1315 (HPI_TUNER_BAND_LAST+1)),
1316 assert_tuner_band_names_size);
1317
1318 static const char * const asihpi_src_names[] = {
1319 "no source",
1320 "PCM",
1321 "Line",
1322 "Digital",
1323 "Tuner",
1324 "RF",
1325 "Clock",
1326 "Bitstream",
1327 "Microphone",
1328 "Cobranet",
1329 "Analog",
1330 "Adapter",
1331 };
1332
1333 compile_time_assert(
1334 (ARRAY_SIZE(asihpi_src_names) ==
1335 (HPI_SOURCENODE_LAST_INDEX-HPI_SOURCENODE_NONE+1)),
1336 assert_src_names_size);
1337
1338 static const char * const asihpi_dst_names[] = {
1339 "no destination",
1340 "PCM",
1341 "Line",
1342 "Digital",
1343 "RF",
1344 "Speaker",
1345 "Cobranet Out",
1346 "Analog"
1347 };
1348
1349 compile_time_assert(
1350 (ARRAY_SIZE(asihpi_dst_names) ==
1351 (HPI_DESTNODE_LAST_INDEX-HPI_DESTNODE_NONE+1)),
1352 assert_dst_names_size);
1353
1354 static inline int ctl_add(struct snd_card *card, struct snd_kcontrol_new *ctl,
1355 struct snd_card_asihpi *asihpi)
1356 {
1357 int err;
1358
1359 err = snd_ctl_add(card, snd_ctl_new1(ctl, asihpi));
1360 if (err < 0)
1361 return err;
1362 else if (mixer_dump)
1363 snd_printk(KERN_INFO "added %s(%d)\n", ctl->name, ctl->index);
1364
1365 return 0;
1366 }
1367
1368 /* Convert HPI control name and location into ALSA control name */
1369 static void asihpi_ctl_init(struct snd_kcontrol_new *snd_control,
1370 struct hpi_control *hpi_ctl,
1371 char *name)
1372 {
1373 char *dir;
1374 memset(snd_control, 0, sizeof(*snd_control));
1375 snd_control->name = hpi_ctl->name;
1376 snd_control->private_value = hpi_ctl->h_control;
1377 snd_control->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1378 snd_control->index = 0;
1379
1380 if (hpi_ctl->src_node_type + HPI_SOURCENODE_NONE == HPI_SOURCENODE_CLOCK_SOURCE)
1381 dir = ""; /* clock is neither capture nor playback */
1382 else if (hpi_ctl->dst_node_type + HPI_DESTNODE_NONE == HPI_DESTNODE_ISTREAM)
1383 dir = "Capture "; /* On or towards a PCM capture destination*/
1384 else if ((hpi_ctl->src_node_type + HPI_SOURCENODE_NONE != HPI_SOURCENODE_OSTREAM) &&
1385 (!hpi_ctl->dst_node_type))
1386 dir = "Capture "; /* On a source node that is not PCM playback */
1387 else if (hpi_ctl->src_node_type &&
1388 (hpi_ctl->src_node_type + HPI_SOURCENODE_NONE != HPI_SOURCENODE_OSTREAM) &&
1389 (hpi_ctl->dst_node_type))
1390 dir = "Monitor Playback "; /* Between an input and an output */
1391 else
1392 dir = "Playback "; /* PCM Playback source, or output node */
1393
1394 if (hpi_ctl->src_node_type && hpi_ctl->dst_node_type)
1395 sprintf(hpi_ctl->name, "%s %d %s %d %s%s",
1396 asihpi_src_names[hpi_ctl->src_node_type],
1397 hpi_ctl->src_node_index,
1398 asihpi_dst_names[hpi_ctl->dst_node_type],
1399 hpi_ctl->dst_node_index,
1400 dir, name);
1401 else if (hpi_ctl->dst_node_type) {
1402 sprintf(hpi_ctl->name, "%s %d %s%s",
1403 asihpi_dst_names[hpi_ctl->dst_node_type],
1404 hpi_ctl->dst_node_index,
1405 dir, name);
1406 } else {
1407 sprintf(hpi_ctl->name, "%s %d %s%s",
1408 asihpi_src_names[hpi_ctl->src_node_type],
1409 hpi_ctl->src_node_index,
1410 dir, name);
1411 }
1412 /* printk(KERN_INFO "Adding %s %d to %d ", hpi_ctl->name,
1413 hpi_ctl->wSrcNodeType, hpi_ctl->wDstNodeType); */
1414 }
1415
1416 /*------------------------------------------------------------
1417 Volume controls
1418 ------------------------------------------------------------*/
1419 #define VOL_STEP_mB 1
1420 static int snd_asihpi_volume_info(struct snd_kcontrol *kcontrol,
1421 struct snd_ctl_elem_info *uinfo)
1422 {
1423 u32 h_control = kcontrol->private_value;
1424 u16 err;
1425 /* native gains are in millibels */
1426 short min_gain_mB;
1427 short max_gain_mB;
1428 short step_gain_mB;
1429
1430 err = hpi_volume_query_range(h_control,
1431 &min_gain_mB, &max_gain_mB, &step_gain_mB);
1432 if (err) {
1433 max_gain_mB = 0;
1434 min_gain_mB = -10000;
1435 step_gain_mB = VOL_STEP_mB;
1436 }
1437
1438 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1439 uinfo->count = 2;
1440 uinfo->value.integer.min = min_gain_mB / VOL_STEP_mB;
1441 uinfo->value.integer.max = max_gain_mB / VOL_STEP_mB;
1442 uinfo->value.integer.step = step_gain_mB / VOL_STEP_mB;
1443 return 0;
1444 }
1445
1446 static int snd_asihpi_volume_get(struct snd_kcontrol *kcontrol,
1447 struct snd_ctl_elem_value *ucontrol)
1448 {
1449 u32 h_control = kcontrol->private_value;
1450 short an_gain_mB[HPI_MAX_CHANNELS];
1451
1452 hpi_handle_error(hpi_volume_get_gain(h_control, an_gain_mB));
1453 ucontrol->value.integer.value[0] = an_gain_mB[0] / VOL_STEP_mB;
1454 ucontrol->value.integer.value[1] = an_gain_mB[1] / VOL_STEP_mB;
1455
1456 return 0;
1457 }
1458
1459 static int snd_asihpi_volume_put(struct snd_kcontrol *kcontrol,
1460 struct snd_ctl_elem_value *ucontrol)
1461 {
1462 int change;
1463 u32 h_control = kcontrol->private_value;
1464 short an_gain_mB[HPI_MAX_CHANNELS];
1465
1466 an_gain_mB[0] =
1467 (ucontrol->value.integer.value[0]) * VOL_STEP_mB;
1468 an_gain_mB[1] =
1469 (ucontrol->value.integer.value[1]) * VOL_STEP_mB;
1470 /* change = asihpi->mixer_volume[addr][0] != left ||
1471 asihpi->mixer_volume[addr][1] != right;
1472 */
1473 change = 1;
1474 hpi_handle_error(hpi_volume_set_gain(h_control, an_gain_mB));
1475 return change;
1476 }
1477
1478 static const DECLARE_TLV_DB_SCALE(db_scale_100, -10000, VOL_STEP_mB, 0);
1479
1480 static int __devinit snd_asihpi_volume_add(struct snd_card_asihpi *asihpi,
1481 struct hpi_control *hpi_ctl)
1482 {
1483 struct snd_card *card = asihpi->card;
1484 struct snd_kcontrol_new snd_control;
1485
1486 asihpi_ctl_init(&snd_control, hpi_ctl, "Volume");
1487 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1488 SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1489 snd_control.info = snd_asihpi_volume_info;
1490 snd_control.get = snd_asihpi_volume_get;
1491 snd_control.put = snd_asihpi_volume_put;
1492 snd_control.tlv.p = db_scale_100;
1493
1494 return ctl_add(card, &snd_control, asihpi);
1495 }
1496
1497 /*------------------------------------------------------------
1498 Level controls
1499 ------------------------------------------------------------*/
1500 static int snd_asihpi_level_info(struct snd_kcontrol *kcontrol,
1501 struct snd_ctl_elem_info *uinfo)
1502 {
1503 u32 h_control = kcontrol->private_value;
1504 u16 err;
1505 short min_gain_mB;
1506 short max_gain_mB;
1507 short step_gain_mB;
1508
1509 err =
1510 hpi_level_query_range(h_control, &min_gain_mB,
1511 &max_gain_mB, &step_gain_mB);
1512 if (err) {
1513 max_gain_mB = 2400;
1514 min_gain_mB = -1000;
1515 step_gain_mB = 100;
1516 }
1517
1518 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1519 uinfo->count = 2;
1520 uinfo->value.integer.min = min_gain_mB / HPI_UNITS_PER_dB;
1521 uinfo->value.integer.max = max_gain_mB / HPI_UNITS_PER_dB;
1522 uinfo->value.integer.step = step_gain_mB / HPI_UNITS_PER_dB;
1523 return 0;
1524 }
1525
1526 static int snd_asihpi_level_get(struct snd_kcontrol *kcontrol,
1527 struct snd_ctl_elem_value *ucontrol)
1528 {
1529 u32 h_control = kcontrol->private_value;
1530 short an_gain_mB[HPI_MAX_CHANNELS];
1531
1532 hpi_handle_error(hpi_level_get_gain(h_control, an_gain_mB));
1533 ucontrol->value.integer.value[0] =
1534 an_gain_mB[0] / HPI_UNITS_PER_dB;
1535 ucontrol->value.integer.value[1] =
1536 an_gain_mB[1] / HPI_UNITS_PER_dB;
1537
1538 return 0;
1539 }
1540
1541 static int snd_asihpi_level_put(struct snd_kcontrol *kcontrol,
1542 struct snd_ctl_elem_value *ucontrol)
1543 {
1544 int change;
1545 u32 h_control = kcontrol->private_value;
1546 short an_gain_mB[HPI_MAX_CHANNELS];
1547
1548 an_gain_mB[0] =
1549 (ucontrol->value.integer.value[0]) * HPI_UNITS_PER_dB;
1550 an_gain_mB[1] =
1551 (ucontrol->value.integer.value[1]) * HPI_UNITS_PER_dB;
1552 /* change = asihpi->mixer_level[addr][0] != left ||
1553 asihpi->mixer_level[addr][1] != right;
1554 */
1555 change = 1;
1556 hpi_handle_error(hpi_level_set_gain(h_control, an_gain_mB));
1557 return change;
1558 }
1559
1560 static const DECLARE_TLV_DB_SCALE(db_scale_level, -1000, 100, 0);
1561
1562 static int __devinit snd_asihpi_level_add(struct snd_card_asihpi *asihpi,
1563 struct hpi_control *hpi_ctl)
1564 {
1565 struct snd_card *card = asihpi->card;
1566 struct snd_kcontrol_new snd_control;
1567
1568 /* can't use 'volume' cos some nodes have volume as well */
1569 asihpi_ctl_init(&snd_control, hpi_ctl, "Level");
1570 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1571 SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1572 snd_control.info = snd_asihpi_level_info;
1573 snd_control.get = snd_asihpi_level_get;
1574 snd_control.put = snd_asihpi_level_put;
1575 snd_control.tlv.p = db_scale_level;
1576
1577 return ctl_add(card, &snd_control, asihpi);
1578 }
1579
1580 /*------------------------------------------------------------
1581 AESEBU controls
1582 ------------------------------------------------------------*/
1583
1584 /* AESEBU format */
1585 static const char * const asihpi_aesebu_format_names[] = {
1586 "N/A", "S/PDIF", "AES/EBU" };
1587
1588 static int snd_asihpi_aesebu_format_info(struct snd_kcontrol *kcontrol,
1589 struct snd_ctl_elem_info *uinfo)
1590 {
1591 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1592 uinfo->count = 1;
1593 uinfo->value.enumerated.items = 3;
1594
1595 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1596 uinfo->value.enumerated.item =
1597 uinfo->value.enumerated.items - 1;
1598
1599 strcpy(uinfo->value.enumerated.name,
1600 asihpi_aesebu_format_names[uinfo->value.enumerated.item]);
1601
1602 return 0;
1603 }
1604
1605 static int snd_asihpi_aesebu_format_get(struct snd_kcontrol *kcontrol,
1606 struct snd_ctl_elem_value *ucontrol,
1607 u16 (*func)(u32, u16 *))
1608 {
1609 u32 h_control = kcontrol->private_value;
1610 u16 source, err;
1611
1612 err = func(h_control, &source);
1613
1614 /* default to N/A */
1615 ucontrol->value.enumerated.item[0] = 0;
1616 /* return success but set the control to N/A */
1617 if (err)
1618 return 0;
1619 if (source == HPI_AESEBU_FORMAT_SPDIF)
1620 ucontrol->value.enumerated.item[0] = 1;
1621 if (source == HPI_AESEBU_FORMAT_AESEBU)
1622 ucontrol->value.enumerated.item[0] = 2;
1623
1624 return 0;
1625 }
1626
1627 static int snd_asihpi_aesebu_format_put(struct snd_kcontrol *kcontrol,
1628 struct snd_ctl_elem_value *ucontrol,
1629 u16 (*func)(u32, u16))
1630 {
1631 u32 h_control = kcontrol->private_value;
1632
1633 /* default to S/PDIF */
1634 u16 source = HPI_AESEBU_FORMAT_SPDIF;
1635
1636 if (ucontrol->value.enumerated.item[0] == 1)
1637 source = HPI_AESEBU_FORMAT_SPDIF;
1638 if (ucontrol->value.enumerated.item[0] == 2)
1639 source = HPI_AESEBU_FORMAT_AESEBU;
1640
1641 if (func(h_control, source) != 0)
1642 return -EINVAL;
1643
1644 return 1;
1645 }
1646
1647 static int snd_asihpi_aesebu_rx_format_get(struct snd_kcontrol *kcontrol,
1648 struct snd_ctl_elem_value *ucontrol) {
1649 return snd_asihpi_aesebu_format_get(kcontrol, ucontrol,
1650 hpi_aesebu_receiver_get_format);
1651 }
1652
1653 static int snd_asihpi_aesebu_rx_format_put(struct snd_kcontrol *kcontrol,
1654 struct snd_ctl_elem_value *ucontrol) {
1655 return snd_asihpi_aesebu_format_put(kcontrol, ucontrol,
1656 hpi_aesebu_receiver_set_format);
1657 }
1658
1659 static int snd_asihpi_aesebu_rxstatus_info(struct snd_kcontrol *kcontrol,
1660 struct snd_ctl_elem_info *uinfo)
1661 {
1662 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1663 uinfo->count = 1;
1664
1665 uinfo->value.integer.min = 0;
1666 uinfo->value.integer.max = 0X1F;
1667 uinfo->value.integer.step = 1;
1668
1669 return 0;
1670 }
1671
1672 static int snd_asihpi_aesebu_rxstatus_get(struct snd_kcontrol *kcontrol,
1673 struct snd_ctl_elem_value *ucontrol) {
1674
1675 u32 h_control = kcontrol->private_value;
1676 u16 status;
1677
1678 hpi_handle_error(hpi_aesebu_receiver_get_error_status(
1679 h_control, &status));
1680 ucontrol->value.integer.value[0] = status;
1681 return 0;
1682 }
1683
1684 static int __devinit snd_asihpi_aesebu_rx_add(struct snd_card_asihpi *asihpi,
1685 struct hpi_control *hpi_ctl)
1686 {
1687 struct snd_card *card = asihpi->card;
1688 struct snd_kcontrol_new snd_control;
1689
1690 asihpi_ctl_init(&snd_control, hpi_ctl, "Format");
1691 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1692 snd_control.info = snd_asihpi_aesebu_format_info;
1693 snd_control.get = snd_asihpi_aesebu_rx_format_get;
1694 snd_control.put = snd_asihpi_aesebu_rx_format_put;
1695
1696
1697 if (ctl_add(card, &snd_control, asihpi) < 0)
1698 return -EINVAL;
1699
1700 asihpi_ctl_init(&snd_control, hpi_ctl, "Status");
1701 snd_control.access =
1702 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
1703 snd_control.info = snd_asihpi_aesebu_rxstatus_info;
1704 snd_control.get = snd_asihpi_aesebu_rxstatus_get;
1705
1706 return ctl_add(card, &snd_control, asihpi);
1707 }
1708
1709 static int snd_asihpi_aesebu_tx_format_get(struct snd_kcontrol *kcontrol,
1710 struct snd_ctl_elem_value *ucontrol) {
1711 return snd_asihpi_aesebu_format_get(kcontrol, ucontrol,
1712 hpi_aesebu_transmitter_get_format);
1713 }
1714
1715 static int snd_asihpi_aesebu_tx_format_put(struct snd_kcontrol *kcontrol,
1716 struct snd_ctl_elem_value *ucontrol) {
1717 return snd_asihpi_aesebu_format_put(kcontrol, ucontrol,
1718 hpi_aesebu_transmitter_set_format);
1719 }
1720
1721
1722 static int __devinit snd_asihpi_aesebu_tx_add(struct snd_card_asihpi *asihpi,
1723 struct hpi_control *hpi_ctl)
1724 {
1725 struct snd_card *card = asihpi->card;
1726 struct snd_kcontrol_new snd_control;
1727
1728 asihpi_ctl_init(&snd_control, hpi_ctl, "Format");
1729 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1730 snd_control.info = snd_asihpi_aesebu_format_info;
1731 snd_control.get = snd_asihpi_aesebu_tx_format_get;
1732 snd_control.put = snd_asihpi_aesebu_tx_format_put;
1733
1734 return ctl_add(card, &snd_control, asihpi);
1735 }
1736
1737 /*------------------------------------------------------------
1738 Tuner controls
1739 ------------------------------------------------------------*/
1740
1741 /* Gain */
1742
1743 static int snd_asihpi_tuner_gain_info(struct snd_kcontrol *kcontrol,
1744 struct snd_ctl_elem_info *uinfo)
1745 {
1746 u32 h_control = kcontrol->private_value;
1747 u16 err;
1748 short idx;
1749 u16 gain_range[3];
1750
1751 for (idx = 0; idx < 3; idx++) {
1752 err = hpi_tuner_query_gain(h_control,
1753 idx, &gain_range[idx]);
1754 if (err != 0)
1755 return err;
1756 }
1757
1758 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1759 uinfo->count = 1;
1760 uinfo->value.integer.min = ((int)gain_range[0]) / HPI_UNITS_PER_dB;
1761 uinfo->value.integer.max = ((int)gain_range[1]) / HPI_UNITS_PER_dB;
1762 uinfo->value.integer.step = ((int) gain_range[2]) / HPI_UNITS_PER_dB;
1763 return 0;
1764 }
1765
1766 static int snd_asihpi_tuner_gain_get(struct snd_kcontrol *kcontrol,
1767 struct snd_ctl_elem_value *ucontrol)
1768 {
1769 /*
1770 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1771 */
1772 u32 h_control = kcontrol->private_value;
1773 short gain;
1774
1775 hpi_handle_error(hpi_tuner_get_gain(h_control, &gain));
1776 ucontrol->value.integer.value[0] = gain / HPI_UNITS_PER_dB;
1777
1778 return 0;
1779 }
1780
1781 static int snd_asihpi_tuner_gain_put(struct snd_kcontrol *kcontrol,
1782 struct snd_ctl_elem_value *ucontrol)
1783 {
1784 /*
1785 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1786 */
1787 u32 h_control = kcontrol->private_value;
1788 short gain;
1789
1790 gain = (ucontrol->value.integer.value[0]) * HPI_UNITS_PER_dB;
1791 hpi_handle_error(hpi_tuner_set_gain(h_control, gain));
1792
1793 return 1;
1794 }
1795
1796 /* Band */
1797
1798 static int asihpi_tuner_band_query(struct snd_kcontrol *kcontrol,
1799 u16 *band_list, u32 len) {
1800 u32 h_control = kcontrol->private_value;
1801 u16 err = 0;
1802 u32 i;
1803
1804 for (i = 0; i < len; i++) {
1805 err = hpi_tuner_query_band(
1806 h_control, i, &band_list[i]);
1807 if (err != 0)
1808 break;
1809 }
1810
1811 if (err && (err != HPI_ERROR_INVALID_OBJ_INDEX))
1812 return -EIO;
1813
1814 return i;
1815 }
1816
1817 static int snd_asihpi_tuner_band_info(struct snd_kcontrol *kcontrol,
1818 struct snd_ctl_elem_info *uinfo)
1819 {
1820 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1821 int num_bands = 0;
1822
1823 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1824 HPI_TUNER_BAND_LAST);
1825
1826 if (num_bands < 0)
1827 return num_bands;
1828
1829 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1830 uinfo->count = 1;
1831 uinfo->value.enumerated.items = num_bands;
1832
1833 if (num_bands > 0) {
1834 if (uinfo->value.enumerated.item >=
1835 uinfo->value.enumerated.items)
1836 uinfo->value.enumerated.item =
1837 uinfo->value.enumerated.items - 1;
1838
1839 strcpy(uinfo->value.enumerated.name,
1840 asihpi_tuner_band_names[
1841 tuner_bands[uinfo->value.enumerated.item]]);
1842
1843 }
1844 return 0;
1845 }
1846
1847 static int snd_asihpi_tuner_band_get(struct snd_kcontrol *kcontrol,
1848 struct snd_ctl_elem_value *ucontrol)
1849 {
1850 u32 h_control = kcontrol->private_value;
1851 /*
1852 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1853 */
1854 u16 band, idx;
1855 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1856 u32 num_bands = 0;
1857
1858 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1859 HPI_TUNER_BAND_LAST);
1860
1861 hpi_handle_error(hpi_tuner_get_band(h_control, &band));
1862
1863 ucontrol->value.enumerated.item[0] = -1;
1864 for (idx = 0; idx < HPI_TUNER_BAND_LAST; idx++)
1865 if (tuner_bands[idx] == band) {
1866 ucontrol->value.enumerated.item[0] = idx;
1867 break;
1868 }
1869
1870 return 0;
1871 }
1872
1873 static int snd_asihpi_tuner_band_put(struct snd_kcontrol *kcontrol,
1874 struct snd_ctl_elem_value *ucontrol)
1875 {
1876 /*
1877 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1878 */
1879 u32 h_control = kcontrol->private_value;
1880 u16 band;
1881 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1882 u32 num_bands = 0;
1883
1884 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1885 HPI_TUNER_BAND_LAST);
1886
1887 band = tuner_bands[ucontrol->value.enumerated.item[0]];
1888 hpi_handle_error(hpi_tuner_set_band(h_control, band));
1889
1890 return 1;
1891 }
1892
1893 /* Freq */
1894
1895 static int snd_asihpi_tuner_freq_info(struct snd_kcontrol *kcontrol,
1896 struct snd_ctl_elem_info *uinfo)
1897 {
1898 u32 h_control = kcontrol->private_value;
1899 u16 err;
1900 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1901 u16 num_bands = 0, band_iter, idx;
1902 u32 freq_range[3], temp_freq_range[3];
1903
1904 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1905 HPI_TUNER_BAND_LAST);
1906
1907 freq_range[0] = INT_MAX;
1908 freq_range[1] = 0;
1909 freq_range[2] = INT_MAX;
1910
1911 for (band_iter = 0; band_iter < num_bands; band_iter++) {
1912 for (idx = 0; idx < 3; idx++) {
1913 err = hpi_tuner_query_frequency(h_control,
1914 idx, tuner_bands[band_iter],
1915 &temp_freq_range[idx]);
1916 if (err != 0)
1917 return err;
1918 }
1919
1920 /* skip band with bogus stepping */
1921 if (temp_freq_range[2] <= 0)
1922 continue;
1923
1924 if (temp_freq_range[0] < freq_range[0])
1925 freq_range[0] = temp_freq_range[0];
1926 if (temp_freq_range[1] > freq_range[1])
1927 freq_range[1] = temp_freq_range[1];
1928 if (temp_freq_range[2] < freq_range[2])
1929 freq_range[2] = temp_freq_range[2];
1930 }
1931
1932 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1933 uinfo->count = 1;
1934 uinfo->value.integer.min = ((int)freq_range[0]);
1935 uinfo->value.integer.max = ((int)freq_range[1]);
1936 uinfo->value.integer.step = ((int)freq_range[2]);
1937 return 0;
1938 }
1939
1940 static int snd_asihpi_tuner_freq_get(struct snd_kcontrol *kcontrol,
1941 struct snd_ctl_elem_value *ucontrol)
1942 {
1943 u32 h_control = kcontrol->private_value;
1944 u32 freq;
1945
1946 hpi_handle_error(hpi_tuner_get_frequency(h_control, &freq));
1947 ucontrol->value.integer.value[0] = freq;
1948
1949 return 0;
1950 }
1951
1952 static int snd_asihpi_tuner_freq_put(struct snd_kcontrol *kcontrol,
1953 struct snd_ctl_elem_value *ucontrol)
1954 {
1955 u32 h_control = kcontrol->private_value;
1956 u32 freq;
1957
1958 freq = ucontrol->value.integer.value[0];
1959 hpi_handle_error(hpi_tuner_set_frequency(h_control, freq));
1960
1961 return 1;
1962 }
1963
1964 /* Tuner control group initializer */
1965 static int __devinit snd_asihpi_tuner_add(struct snd_card_asihpi *asihpi,
1966 struct hpi_control *hpi_ctl)
1967 {
1968 struct snd_card *card = asihpi->card;
1969 struct snd_kcontrol_new snd_control;
1970
1971 snd_control.private_value = hpi_ctl->h_control;
1972 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1973
1974 if (!hpi_tuner_get_gain(hpi_ctl->h_control, NULL)) {
1975 asihpi_ctl_init(&snd_control, hpi_ctl, "Gain");
1976 snd_control.info = snd_asihpi_tuner_gain_info;
1977 snd_control.get = snd_asihpi_tuner_gain_get;
1978 snd_control.put = snd_asihpi_tuner_gain_put;
1979
1980 if (ctl_add(card, &snd_control, asihpi) < 0)
1981 return -EINVAL;
1982 }
1983
1984 asihpi_ctl_init(&snd_control, hpi_ctl, "Band");
1985 snd_control.info = snd_asihpi_tuner_band_info;
1986 snd_control.get = snd_asihpi_tuner_band_get;
1987 snd_control.put = snd_asihpi_tuner_band_put;
1988
1989 if (ctl_add(card, &snd_control, asihpi) < 0)
1990 return -EINVAL;
1991
1992 asihpi_ctl_init(&snd_control, hpi_ctl, "Freq");
1993 snd_control.info = snd_asihpi_tuner_freq_info;
1994 snd_control.get = snd_asihpi_tuner_freq_get;
1995 snd_control.put = snd_asihpi_tuner_freq_put;
1996
1997 return ctl_add(card, &snd_control, asihpi);
1998 }
1999
2000 /*------------------------------------------------------------
2001 Meter controls
2002 ------------------------------------------------------------*/
2003 static int snd_asihpi_meter_info(struct snd_kcontrol *kcontrol,
2004 struct snd_ctl_elem_info *uinfo)
2005 {
2006 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2007 uinfo->count = HPI_MAX_CHANNELS;
2008 uinfo->value.integer.min = 0;
2009 uinfo->value.integer.max = 0x7FFFFFFF;
2010 return 0;
2011 }
2012
2013 /* linear values for 10dB steps */
2014 static int log2lin[] = {
2015 0x7FFFFFFF, /* 0dB */
2016 679093956,
2017 214748365,
2018 67909396,
2019 21474837,
2020 6790940,
2021 2147484, /* -60dB */
2022 679094,
2023 214748, /* -80 */
2024 67909,
2025 21475, /* -100 */
2026 6791,
2027 2147,
2028 679,
2029 214,
2030 68,
2031 21,
2032 7,
2033 2
2034 };
2035
2036 static int snd_asihpi_meter_get(struct snd_kcontrol *kcontrol,
2037 struct snd_ctl_elem_value *ucontrol)
2038 {
2039 u32 h_control = kcontrol->private_value;
2040 short an_gain_mB[HPI_MAX_CHANNELS], i;
2041 u16 err;
2042
2043 err = hpi_meter_get_peak(h_control, an_gain_mB);
2044
2045 for (i = 0; i < HPI_MAX_CHANNELS; i++) {
2046 if (err) {
2047 ucontrol->value.integer.value[i] = 0;
2048 } else if (an_gain_mB[i] >= 0) {
2049 ucontrol->value.integer.value[i] =
2050 an_gain_mB[i] << 16;
2051 } else {
2052 /* -ve is log value in millibels < -60dB,
2053 * convert to (roughly!) linear,
2054 */
2055 ucontrol->value.integer.value[i] =
2056 log2lin[an_gain_mB[i] / -1000];
2057 }
2058 }
2059 return 0;
2060 }
2061
2062 static int __devinit snd_asihpi_meter_add(struct snd_card_asihpi *asihpi,
2063 struct hpi_control *hpi_ctl, int subidx)
2064 {
2065 struct snd_card *card = asihpi->card;
2066 struct snd_kcontrol_new snd_control;
2067
2068 asihpi_ctl_init(&snd_control, hpi_ctl, "Meter");
2069 snd_control.access =
2070 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
2071 snd_control.info = snd_asihpi_meter_info;
2072 snd_control.get = snd_asihpi_meter_get;
2073
2074 snd_control.index = subidx;
2075
2076 return ctl_add(card, &snd_control, asihpi);
2077 }
2078
2079 /*------------------------------------------------------------
2080 Multiplexer controls
2081 ------------------------------------------------------------*/
2082 static int snd_card_asihpi_mux_count_sources(struct snd_kcontrol *snd_control)
2083 {
2084 u32 h_control = snd_control->private_value;
2085 struct hpi_control hpi_ctl;
2086 int s, err;
2087 for (s = 0; s < 32; s++) {
2088 err = hpi_multiplexer_query_source(h_control, s,
2089 &hpi_ctl.
2090 src_node_type,
2091 &hpi_ctl.
2092 src_node_index);
2093 if (err)
2094 break;
2095 }
2096 return s;
2097 }
2098
2099 static int snd_asihpi_mux_info(struct snd_kcontrol *kcontrol,
2100 struct snd_ctl_elem_info *uinfo)
2101 {
2102 int err;
2103 u16 src_node_type, src_node_index;
2104 u32 h_control = kcontrol->private_value;
2105
2106 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2107 uinfo->count = 1;
2108 uinfo->value.enumerated.items =
2109 snd_card_asihpi_mux_count_sources(kcontrol);
2110
2111 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2112 uinfo->value.enumerated.item =
2113 uinfo->value.enumerated.items - 1;
2114
2115 err =
2116 hpi_multiplexer_query_source(h_control,
2117 uinfo->value.enumerated.item,
2118 &src_node_type, &src_node_index);
2119
2120 sprintf(uinfo->value.enumerated.name, "%s %d",
2121 asihpi_src_names[src_node_type - HPI_SOURCENODE_NONE],
2122 src_node_index);
2123 return 0;
2124 }
2125
2126 static int snd_asihpi_mux_get(struct snd_kcontrol *kcontrol,
2127 struct snd_ctl_elem_value *ucontrol)
2128 {
2129 u32 h_control = kcontrol->private_value;
2130 u16 source_type, source_index;
2131 u16 src_node_type, src_node_index;
2132 int s;
2133
2134 hpi_handle_error(hpi_multiplexer_get_source(h_control,
2135 &source_type, &source_index));
2136 /* Should cache this search result! */
2137 for (s = 0; s < 256; s++) {
2138 if (hpi_multiplexer_query_source(h_control, s,
2139 &src_node_type, &src_node_index))
2140 break;
2141
2142 if ((source_type == src_node_type)
2143 && (source_index == src_node_index)) {
2144 ucontrol->value.enumerated.item[0] = s;
2145 return 0;
2146 }
2147 }
2148 snd_printd(KERN_WARNING
2149 "Control %x failed to match mux source %hu %hu\n",
2150 h_control, source_type, source_index);
2151 ucontrol->value.enumerated.item[0] = 0;
2152 return 0;
2153 }
2154
2155 static int snd_asihpi_mux_put(struct snd_kcontrol *kcontrol,
2156 struct snd_ctl_elem_value *ucontrol)
2157 {
2158 int change;
2159 u32 h_control = kcontrol->private_value;
2160 u16 source_type, source_index;
2161 u16 e;
2162
2163 change = 1;
2164
2165 e = hpi_multiplexer_query_source(h_control,
2166 ucontrol->value.enumerated.item[0],
2167 &source_type, &source_index);
2168 if (!e)
2169 hpi_handle_error(
2170 hpi_multiplexer_set_source(h_control,
2171 source_type, source_index));
2172 return change;
2173 }
2174
2175
2176 static int __devinit snd_asihpi_mux_add(struct snd_card_asihpi *asihpi,
2177 struct hpi_control *hpi_ctl)
2178 {
2179 struct snd_card *card = asihpi->card;
2180 struct snd_kcontrol_new snd_control;
2181
2182 asihpi_ctl_init(&snd_control, hpi_ctl, "Route");
2183 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2184 snd_control.info = snd_asihpi_mux_info;
2185 snd_control.get = snd_asihpi_mux_get;
2186 snd_control.put = snd_asihpi_mux_put;
2187
2188 return ctl_add(card, &snd_control, asihpi);
2189
2190 }
2191
2192 /*------------------------------------------------------------
2193 Channel mode controls
2194 ------------------------------------------------------------*/
2195 static int snd_asihpi_cmode_info(struct snd_kcontrol *kcontrol,
2196 struct snd_ctl_elem_info *uinfo)
2197 {
2198 static const char * const mode_names[HPI_CHANNEL_MODE_LAST + 1] = {
2199 "invalid",
2200 "Normal", "Swap",
2201 "From Left", "From Right",
2202 "To Left", "To Right"
2203 };
2204
2205 u32 h_control = kcontrol->private_value;
2206 u16 mode;
2207 int i;
2208 u16 mode_map[6];
2209 int valid_modes = 0;
2210
2211 /* HPI channel mode values can be from 1 to 6
2212 Some adapters only support a contiguous subset
2213 */
2214 for (i = 0; i < HPI_CHANNEL_MODE_LAST; i++)
2215 if (!hpi_channel_mode_query_mode(
2216 h_control, i, &mode)) {
2217 mode_map[valid_modes] = mode;
2218 valid_modes++;
2219 }
2220
2221 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2222 uinfo->count = 1;
2223 uinfo->value.enumerated.items = valid_modes;
2224
2225 if (uinfo->value.enumerated.item >= valid_modes)
2226 uinfo->value.enumerated.item = valid_modes - 1;
2227
2228 strcpy(uinfo->value.enumerated.name,
2229 mode_names[mode_map[uinfo->value.enumerated.item]]);
2230
2231 return 0;
2232 }
2233
2234 static int snd_asihpi_cmode_get(struct snd_kcontrol *kcontrol,
2235 struct snd_ctl_elem_value *ucontrol)
2236 {
2237 u32 h_control = kcontrol->private_value;
2238 u16 mode;
2239
2240 if (hpi_channel_mode_get(h_control, &mode))
2241 mode = 1;
2242
2243 ucontrol->value.enumerated.item[0] = mode - 1;
2244
2245 return 0;
2246 }
2247
2248 static int snd_asihpi_cmode_put(struct snd_kcontrol *kcontrol,
2249 struct snd_ctl_elem_value *ucontrol)
2250 {
2251 int change;
2252 u32 h_control = kcontrol->private_value;
2253
2254 change = 1;
2255
2256 hpi_handle_error(hpi_channel_mode_set(h_control,
2257 ucontrol->value.enumerated.item[0] + 1));
2258 return change;
2259 }
2260
2261
2262 static int __devinit snd_asihpi_cmode_add(struct snd_card_asihpi *asihpi,
2263 struct hpi_control *hpi_ctl)
2264 {
2265 struct snd_card *card = asihpi->card;
2266 struct snd_kcontrol_new snd_control;
2267
2268 asihpi_ctl_init(&snd_control, hpi_ctl, "Mode");
2269 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2270 snd_control.info = snd_asihpi_cmode_info;
2271 snd_control.get = snd_asihpi_cmode_get;
2272 snd_control.put = snd_asihpi_cmode_put;
2273
2274 return ctl_add(card, &snd_control, asihpi);
2275 }
2276
2277 /*------------------------------------------------------------
2278 Sampleclock source controls
2279 ------------------------------------------------------------*/
2280 static char *sampleclock_sources[MAX_CLOCKSOURCES] = {
2281 "N/A", "Local PLL", "Digital Sync", "Word External", "Word Header",
2282 "SMPTE", "Digital1", "Auto", "Network", "Invalid",
2283 "Prev Module",
2284 "Digital2", "Digital3", "Digital4", "Digital5",
2285 "Digital6", "Digital7", "Digital8"};
2286
2287 static int snd_asihpi_clksrc_info(struct snd_kcontrol *kcontrol,
2288 struct snd_ctl_elem_info *uinfo)
2289 {
2290 struct snd_card_asihpi *asihpi =
2291 (struct snd_card_asihpi *)(kcontrol->private_data);
2292 struct clk_cache *clkcache = &asihpi->cc;
2293 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2294 uinfo->count = 1;
2295 uinfo->value.enumerated.items = clkcache->count;
2296
2297 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2298 uinfo->value.enumerated.item =
2299 uinfo->value.enumerated.items - 1;
2300
2301 strcpy(uinfo->value.enumerated.name,
2302 clkcache->s[uinfo->value.enumerated.item].name);
2303 return 0;
2304 }
2305
2306 static int snd_asihpi_clksrc_get(struct snd_kcontrol *kcontrol,
2307 struct snd_ctl_elem_value *ucontrol)
2308 {
2309 struct snd_card_asihpi *asihpi =
2310 (struct snd_card_asihpi *)(kcontrol->private_data);
2311 struct clk_cache *clkcache = &asihpi->cc;
2312 u32 h_control = kcontrol->private_value;
2313 u16 source, srcindex = 0;
2314 int i;
2315
2316 ucontrol->value.enumerated.item[0] = 0;
2317 if (hpi_sample_clock_get_source(h_control, &source))
2318 source = 0;
2319
2320 if (source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
2321 if (hpi_sample_clock_get_source_index(h_control, &srcindex))
2322 srcindex = 0;
2323
2324 for (i = 0; i < clkcache->count; i++)
2325 if ((clkcache->s[i].source == source) &&
2326 (clkcache->s[i].index == srcindex))
2327 break;
2328
2329 ucontrol->value.enumerated.item[0] = i;
2330
2331 return 0;
2332 }
2333
2334 static int snd_asihpi_clksrc_put(struct snd_kcontrol *kcontrol,
2335 struct snd_ctl_elem_value *ucontrol)
2336 {
2337 struct snd_card_asihpi *asihpi =
2338 (struct snd_card_asihpi *)(kcontrol->private_data);
2339 struct clk_cache *clkcache = &asihpi->cc;
2340 int change, item;
2341 u32 h_control = kcontrol->private_value;
2342
2343 change = 1;
2344 item = ucontrol->value.enumerated.item[0];
2345 if (item >= clkcache->count)
2346 item = clkcache->count-1;
2347
2348 hpi_handle_error(hpi_sample_clock_set_source(
2349 h_control, clkcache->s[item].source));
2350
2351 if (clkcache->s[item].source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
2352 hpi_handle_error(hpi_sample_clock_set_source_index(
2353 h_control, clkcache->s[item].index));
2354 return change;
2355 }
2356
2357 /*------------------------------------------------------------
2358 Clkrate controls
2359 ------------------------------------------------------------*/
2360 /* Need to change this to enumerated control with list of rates */
2361 static int snd_asihpi_clklocal_info(struct snd_kcontrol *kcontrol,
2362 struct snd_ctl_elem_info *uinfo)
2363 {
2364 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2365 uinfo->count = 1;
2366 uinfo->value.integer.min = 8000;
2367 uinfo->value.integer.max = 192000;
2368 uinfo->value.integer.step = 100;
2369
2370 return 0;
2371 }
2372
2373 static int snd_asihpi_clklocal_get(struct snd_kcontrol *kcontrol,
2374 struct snd_ctl_elem_value *ucontrol)
2375 {
2376 u32 h_control = kcontrol->private_value;
2377 u32 rate;
2378 u16 e;
2379
2380 e = hpi_sample_clock_get_local_rate(h_control, &rate);
2381 if (!e)
2382 ucontrol->value.integer.value[0] = rate;
2383 else
2384 ucontrol->value.integer.value[0] = 0;
2385 return 0;
2386 }
2387
2388 static int snd_asihpi_clklocal_put(struct snd_kcontrol *kcontrol,
2389 struct snd_ctl_elem_value *ucontrol)
2390 {
2391 int change;
2392 u32 h_control = kcontrol->private_value;
2393
2394 /* change = asihpi->mixer_clkrate[addr][0] != left ||
2395 asihpi->mixer_clkrate[addr][1] != right;
2396 */
2397 change = 1;
2398 hpi_handle_error(hpi_sample_clock_set_local_rate(h_control,
2399 ucontrol->value.integer.value[0]));
2400 return change;
2401 }
2402
2403 static int snd_asihpi_clkrate_info(struct snd_kcontrol *kcontrol,
2404 struct snd_ctl_elem_info *uinfo)
2405 {
2406 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2407 uinfo->count = 1;
2408 uinfo->value.integer.min = 8000;
2409 uinfo->value.integer.max = 192000;
2410 uinfo->value.integer.step = 100;
2411
2412 return 0;
2413 }
2414
2415 static int snd_asihpi_clkrate_get(struct snd_kcontrol *kcontrol,
2416 struct snd_ctl_elem_value *ucontrol)
2417 {
2418 u32 h_control = kcontrol->private_value;
2419 u32 rate;
2420 u16 e;
2421
2422 e = hpi_sample_clock_get_sample_rate(h_control, &rate);
2423 if (!e)
2424 ucontrol->value.integer.value[0] = rate;
2425 else
2426 ucontrol->value.integer.value[0] = 0;
2427 return 0;
2428 }
2429
2430 static int __devinit snd_asihpi_sampleclock_add(struct snd_card_asihpi *asihpi,
2431 struct hpi_control *hpi_ctl)
2432 {
2433 struct snd_card *card = asihpi->card;
2434 struct snd_kcontrol_new snd_control;
2435
2436 struct clk_cache *clkcache = &asihpi->cc;
2437 u32 hSC = hpi_ctl->h_control;
2438 int has_aes_in = 0;
2439 int i, j;
2440 u16 source;
2441
2442 snd_control.private_value = hpi_ctl->h_control;
2443
2444 clkcache->has_local = 0;
2445
2446 for (i = 0; i <= HPI_SAMPLECLOCK_SOURCE_LAST; i++) {
2447 if (hpi_sample_clock_query_source(hSC,
2448 i, &source))
2449 break;
2450 clkcache->s[i].source = source;
2451 clkcache->s[i].index = 0;
2452 clkcache->s[i].name = sampleclock_sources[source];
2453 if (source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
2454 has_aes_in = 1;
2455 if (source == HPI_SAMPLECLOCK_SOURCE_LOCAL)
2456 clkcache->has_local = 1;
2457 }
2458 if (has_aes_in)
2459 /* already will have picked up index 0 above */
2460 for (j = 1; j < 8; j++) {
2461 if (hpi_sample_clock_query_source_index(hSC,
2462 j, HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT,
2463 &source))
2464 break;
2465 clkcache->s[i].source =
2466 HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT;
2467 clkcache->s[i].index = j;
2468 clkcache->s[i].name = sampleclock_sources[
2469 j+HPI_SAMPLECLOCK_SOURCE_LAST];
2470 i++;
2471 }
2472 clkcache->count = i;
2473
2474 asihpi_ctl_init(&snd_control, hpi_ctl, "Source");
2475 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE ;
2476 snd_control.info = snd_asihpi_clksrc_info;
2477 snd_control.get = snd_asihpi_clksrc_get;
2478 snd_control.put = snd_asihpi_clksrc_put;
2479 if (ctl_add(card, &snd_control, asihpi) < 0)
2480 return -EINVAL;
2481
2482
2483 if (clkcache->has_local) {
2484 asihpi_ctl_init(&snd_control, hpi_ctl, "Localrate");
2485 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE ;
2486 snd_control.info = snd_asihpi_clklocal_info;
2487 snd_control.get = snd_asihpi_clklocal_get;
2488 snd_control.put = snd_asihpi_clklocal_put;
2489
2490
2491 if (ctl_add(card, &snd_control, asihpi) < 0)
2492 return -EINVAL;
2493 }
2494
2495 asihpi_ctl_init(&snd_control, hpi_ctl, "Rate");
2496 snd_control.access =
2497 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
2498 snd_control.info = snd_asihpi_clkrate_info;
2499 snd_control.get = snd_asihpi_clkrate_get;
2500
2501 return ctl_add(card, &snd_control, asihpi);
2502 }
2503 /*------------------------------------------------------------
2504 Mixer
2505 ------------------------------------------------------------*/
2506
2507 static int __devinit snd_card_asihpi_mixer_new(struct snd_card_asihpi *asihpi)
2508 {
2509 struct snd_card *card = asihpi->card;
2510 unsigned int idx = 0;
2511 unsigned int subindex = 0;
2512 int err;
2513 struct hpi_control hpi_ctl, prev_ctl;
2514
2515 if (snd_BUG_ON(!asihpi))
2516 return -EINVAL;
2517 strcpy(card->mixername, "Asihpi Mixer");
2518
2519 err =
2520 hpi_mixer_open(asihpi->adapter_index,
2521 &asihpi->h_mixer);
2522 hpi_handle_error(err);
2523 if (err)
2524 return -err;
2525
2526 memset(&prev_ctl, 0, sizeof(prev_ctl));
2527 prev_ctl.control_type = -1;
2528
2529 for (idx = 0; idx < 2000; idx++) {
2530 err = hpi_mixer_get_control_by_index(
2531 asihpi->h_mixer,
2532 idx,
2533 &hpi_ctl.src_node_type,
2534 &hpi_ctl.src_node_index,
2535 &hpi_ctl.dst_node_type,
2536 &hpi_ctl.dst_node_index,
2537 &hpi_ctl.control_type,
2538 &hpi_ctl.h_control);
2539 if (err) {
2540 if (err == HPI_ERROR_CONTROL_DISABLED) {
2541 if (mixer_dump)
2542 snd_printk(KERN_INFO
2543 "Disabled HPI Control(%d)\n",
2544 idx);
2545 continue;
2546 } else
2547 break;
2548
2549 }
2550
2551 hpi_ctl.src_node_type -= HPI_SOURCENODE_NONE;
2552 hpi_ctl.dst_node_type -= HPI_DESTNODE_NONE;
2553
2554 /* ASI50xx in SSX mode has multiple meters on the same node.
2555 Use subindex to create distinct ALSA controls
2556 for any duplicated controls.
2557 */
2558 if ((hpi_ctl.control_type == prev_ctl.control_type) &&
2559 (hpi_ctl.src_node_type == prev_ctl.src_node_type) &&
2560 (hpi_ctl.src_node_index == prev_ctl.src_node_index) &&
2561 (hpi_ctl.dst_node_type == prev_ctl.dst_node_type) &&
2562 (hpi_ctl.dst_node_index == prev_ctl.dst_node_index))
2563 subindex++;
2564 else
2565 subindex = 0;
2566
2567 prev_ctl = hpi_ctl;
2568
2569 switch (hpi_ctl.control_type) {
2570 case HPI_CONTROL_VOLUME:
2571 err = snd_asihpi_volume_add(asihpi, &hpi_ctl);
2572 break;
2573 case HPI_CONTROL_LEVEL:
2574 err = snd_asihpi_level_add(asihpi, &hpi_ctl);
2575 break;
2576 case HPI_CONTROL_MULTIPLEXER:
2577 err = snd_asihpi_mux_add(asihpi, &hpi_ctl);
2578 break;
2579 case HPI_CONTROL_CHANNEL_MODE:
2580 err = snd_asihpi_cmode_add(asihpi, &hpi_ctl);
2581 break;
2582 case HPI_CONTROL_METER:
2583 err = snd_asihpi_meter_add(asihpi, &hpi_ctl, subindex);
2584 break;
2585 case HPI_CONTROL_SAMPLECLOCK:
2586 err = snd_asihpi_sampleclock_add(
2587 asihpi, &hpi_ctl);
2588 break;
2589 case HPI_CONTROL_CONNECTION: /* ignore these */
2590 continue;
2591 case HPI_CONTROL_TUNER:
2592 err = snd_asihpi_tuner_add(asihpi, &hpi_ctl);
2593 break;
2594 case HPI_CONTROL_AESEBU_TRANSMITTER:
2595 err = snd_asihpi_aesebu_tx_add(asihpi, &hpi_ctl);
2596 break;
2597 case HPI_CONTROL_AESEBU_RECEIVER:
2598 err = snd_asihpi_aesebu_rx_add(asihpi, &hpi_ctl);
2599 break;
2600 case HPI_CONTROL_VOX:
2601 case HPI_CONTROL_BITSTREAM:
2602 case HPI_CONTROL_MICROPHONE:
2603 case HPI_CONTROL_PARAMETRIC_EQ:
2604 case HPI_CONTROL_COMPANDER:
2605 default:
2606 if (mixer_dump)
2607 snd_printk(KERN_INFO
2608 "Untranslated HPI Control"
2609 "(%d) %d %d %d %d %d\n",
2610 idx,
2611 hpi_ctl.control_type,
2612 hpi_ctl.src_node_type,
2613 hpi_ctl.src_node_index,
2614 hpi_ctl.dst_node_type,
2615 hpi_ctl.dst_node_index);
2616 continue;
2617 };
2618 if (err < 0)
2619 return err;
2620 }
2621 if (HPI_ERROR_INVALID_OBJ_INDEX != err)
2622 hpi_handle_error(err);
2623
2624 snd_printk(KERN_INFO "%d mixer controls found\n", idx);
2625
2626 return 0;
2627 }
2628
2629 /*------------------------------------------------------------
2630 /proc interface
2631 ------------------------------------------------------------*/
2632
2633 static void
2634 snd_asihpi_proc_read(struct snd_info_entry *entry,
2635 struct snd_info_buffer *buffer)
2636 {
2637 struct snd_card_asihpi *asihpi = entry->private_data;
2638 u16 version;
2639 u32 h_control;
2640 u32 rate = 0;
2641 u16 source = 0;
2642 int err;
2643
2644 snd_iprintf(buffer, "ASIHPI driver proc file\n");
2645 snd_iprintf(buffer,
2646 "adapter ID=%4X\n_index=%d\n"
2647 "num_outstreams=%d\n_num_instreams=%d\n",
2648 asihpi->type, asihpi->adapter_index,
2649 asihpi->num_outstreams, asihpi->num_instreams);
2650
2651 version = asihpi->version;
2652 snd_iprintf(buffer,
2653 "serial#=%d\n_hw version %c%d\nDSP code version %03d\n",
2654 asihpi->serial_number, ((version >> 3) & 0xf) + 'A',
2655 version & 0x7,
2656 ((version >> 13) * 100) + ((version >> 7) & 0x3f));
2657
2658 err = hpi_mixer_get_control(asihpi->h_mixer,
2659 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
2660 HPI_CONTROL_SAMPLECLOCK, &h_control);
2661
2662 if (!err) {
2663 err = hpi_sample_clock_get_sample_rate(
2664 h_control, &rate);
2665 err += hpi_sample_clock_get_source(h_control, &source);
2666
2667 if (!err)
2668 snd_iprintf(buffer, "sample_clock=%d_hz, source %s\n",
2669 rate, sampleclock_sources[source]);
2670 }
2671
2672 }
2673
2674
2675 static void __devinit snd_asihpi_proc_init(struct snd_card_asihpi *asihpi)
2676 {
2677 struct snd_info_entry *entry;
2678
2679 if (!snd_card_proc_new(asihpi->card, "info", &entry))
2680 snd_info_set_text_ops(entry, asihpi, snd_asihpi_proc_read);
2681 }
2682
2683 /*------------------------------------------------------------
2684 HWDEP
2685 ------------------------------------------------------------*/
2686
2687 static int snd_asihpi_hpi_open(struct snd_hwdep *hw, struct file *file)
2688 {
2689 if (enable_hpi_hwdep)
2690 return 0;
2691 else
2692 return -ENODEV;
2693
2694 }
2695
2696 static int snd_asihpi_hpi_release(struct snd_hwdep *hw, struct file *file)
2697 {
2698 if (enable_hpi_hwdep)
2699 return asihpi_hpi_release(file);
2700 else
2701 return -ENODEV;
2702 }
2703
2704 static int snd_asihpi_hpi_ioctl(struct snd_hwdep *hw, struct file *file,
2705 unsigned int cmd, unsigned long arg)
2706 {
2707 if (enable_hpi_hwdep)
2708 return asihpi_hpi_ioctl(file, cmd, arg);
2709 else
2710 return -ENODEV;
2711 }
2712
2713
2714 /* results in /dev/snd/hwC#D0 file for each card with index #
2715 also /proc/asound/hwdep will contain '#-00: asihpi (HPI) for each card'
2716 */
2717 static int __devinit snd_asihpi_hpi_new(struct snd_card_asihpi *asihpi,
2718 int device, struct snd_hwdep **rhwdep)
2719 {
2720 struct snd_hwdep *hw;
2721 int err;
2722
2723 if (rhwdep)
2724 *rhwdep = NULL;
2725 err = snd_hwdep_new(asihpi->card, "HPI", device, &hw);
2726 if (err < 0)
2727 return err;
2728 strcpy(hw->name, "asihpi (HPI)");
2729 hw->iface = SNDRV_HWDEP_IFACE_LAST;
2730 hw->ops.open = snd_asihpi_hpi_open;
2731 hw->ops.ioctl = snd_asihpi_hpi_ioctl;
2732 hw->ops.release = snd_asihpi_hpi_release;
2733 hw->private_data = asihpi;
2734 if (rhwdep)
2735 *rhwdep = hw;
2736 return 0;
2737 }
2738
2739 /*------------------------------------------------------------
2740 CARD
2741 ------------------------------------------------------------*/
2742 static int __devinit snd_asihpi_probe(struct pci_dev *pci_dev,
2743 const struct pci_device_id *pci_id)
2744 {
2745 int err;
2746
2747 u16 version;
2748 int pcm_substreams;
2749
2750 struct hpi_adapter *hpi_card;
2751 struct snd_card *card;
2752 struct snd_card_asihpi *asihpi;
2753
2754 u32 h_control;
2755 u32 h_stream;
2756
2757 static int dev;
2758 if (dev >= SNDRV_CARDS)
2759 return -ENODEV;
2760
2761 /* Should this be enable[hpi_card->index] ? */
2762 if (!enable[dev]) {
2763 dev++;
2764 return -ENOENT;
2765 }
2766
2767 err = asihpi_adapter_probe(pci_dev, pci_id);
2768 if (err < 0)
2769 return err;
2770
2771 hpi_card = pci_get_drvdata(pci_dev);
2772 /* first try to give the card the same index as its hardware index */
2773 err = snd_card_create(hpi_card->index,
2774 id[hpi_card->index], THIS_MODULE,
2775 sizeof(struct snd_card_asihpi),
2776 &card);
2777 if (err < 0) {
2778 /* if that fails, try the default index==next available */
2779 err =
2780 snd_card_create(index[dev], id[dev],
2781 THIS_MODULE,
2782 sizeof(struct snd_card_asihpi),
2783 &card);
2784 if (err < 0)
2785 return err;
2786 snd_printk(KERN_WARNING
2787 "**** WARNING **** Adapter index %d->ALSA index %d\n",
2788 hpi_card->index, card->number);
2789 }
2790
2791 snd_card_set_dev(card, &pci_dev->dev);
2792
2793 asihpi = (struct snd_card_asihpi *) card->private_data;
2794 asihpi->card = card;
2795 asihpi->pci = pci_dev;
2796 asihpi->adapter_index = hpi_card->index;
2797 hpi_handle_error(hpi_adapter_get_info(
2798 asihpi->adapter_index,
2799 &asihpi->num_outstreams,
2800 &asihpi->num_instreams,
2801 &asihpi->version,
2802 &asihpi->serial_number, &asihpi->type));
2803
2804 version = asihpi->version;
2805 snd_printk(KERN_INFO "adapter ID=%4X index=%d num_outstreams=%d "
2806 "num_instreams=%d S/N=%d\n"
2807 "Hw Version %c%d DSP code version %03d\n",
2808 asihpi->type, asihpi->adapter_index,
2809 asihpi->num_outstreams,
2810 asihpi->num_instreams, asihpi->serial_number,
2811 ((version >> 3) & 0xf) + 'A',
2812 version & 0x7,
2813 ((version >> 13) * 100) + ((version >> 7) & 0x3f));
2814
2815 pcm_substreams = asihpi->num_outstreams;
2816 if (pcm_substreams < asihpi->num_instreams)
2817 pcm_substreams = asihpi->num_instreams;
2818
2819 err = hpi_adapter_get_property(asihpi->adapter_index,
2820 HPI_ADAPTER_PROPERTY_CAPS1,
2821 NULL, &asihpi->support_grouping);
2822 if (err)
2823 asihpi->support_grouping = 0;
2824
2825 err = hpi_adapter_get_property(asihpi->adapter_index,
2826 HPI_ADAPTER_PROPERTY_CAPS2,
2827 &asihpi->support_mrx, NULL);
2828 if (err)
2829 asihpi->support_mrx = 0;
2830
2831 err = hpi_adapter_get_property(asihpi->adapter_index,
2832 HPI_ADAPTER_PROPERTY_INTERVAL,
2833 NULL, &asihpi->update_interval_frames);
2834 if (err)
2835 asihpi->update_interval_frames = 512;
2836
2837 if (!asihpi->can_dma)
2838 asihpi->update_interval_frames *= 2;
2839
2840 hpi_handle_error(hpi_instream_open(asihpi->adapter_index,
2841 0, &h_stream));
2842
2843 err = hpi_instream_host_buffer_free(h_stream);
2844 asihpi->can_dma = (!err);
2845
2846 hpi_handle_error(hpi_instream_close(h_stream));
2847
2848 err = hpi_adapter_get_property(asihpi->adapter_index,
2849 HPI_ADAPTER_PROPERTY_CURCHANNELS,
2850 &asihpi->in_max_chans, &asihpi->out_max_chans);
2851 if (err) {
2852 asihpi->in_max_chans = 2;
2853 asihpi->out_max_chans = 2;
2854 }
2855
2856 snd_printk(KERN_INFO "has dma:%d, grouping:%d, mrx:%d\n",
2857 asihpi->can_dma,
2858 asihpi->support_grouping,
2859 asihpi->support_mrx
2860 );
2861
2862 err = snd_card_asihpi_pcm_new(asihpi, 0, pcm_substreams);
2863 if (err < 0) {
2864 snd_printk(KERN_ERR "pcm_new failed\n");
2865 goto __nodev;
2866 }
2867 err = snd_card_asihpi_mixer_new(asihpi);
2868 if (err < 0) {
2869 snd_printk(KERN_ERR "mixer_new failed\n");
2870 goto __nodev;
2871 }
2872
2873 err = hpi_mixer_get_control(asihpi->h_mixer,
2874 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
2875 HPI_CONTROL_SAMPLECLOCK, &h_control);
2876
2877 if (!err)
2878 err = hpi_sample_clock_set_local_rate(
2879 h_control, adapter_fs);
2880
2881 snd_asihpi_proc_init(asihpi);
2882
2883 /* always create, can be enabled or disabled dynamically
2884 by enable_hwdep module param*/
2885 snd_asihpi_hpi_new(asihpi, 0, NULL);
2886
2887 strcpy(card->driver, "ASIHPI");
2888
2889 sprintf(card->shortname, "AudioScience ASI%4X", asihpi->type);
2890 sprintf(card->longname, "%s %i",
2891 card->shortname, asihpi->adapter_index);
2892 err = snd_card_register(card);
2893
2894 if (!err) {
2895 hpi_card->snd_card_asihpi = card;
2896 dev++;
2897 return 0;
2898 }
2899 __nodev:
2900 snd_card_free(card);
2901 snd_printk(KERN_ERR "snd_asihpi_probe error %d\n", err);
2902 return err;
2903
2904 }
2905
2906 static void __devexit snd_asihpi_remove(struct pci_dev *pci_dev)
2907 {
2908 struct hpi_adapter *hpi_card = pci_get_drvdata(pci_dev);
2909
2910 snd_card_free(hpi_card->snd_card_asihpi);
2911 hpi_card->snd_card_asihpi = NULL;
2912 asihpi_adapter_remove(pci_dev);
2913 }
2914
2915 static DEFINE_PCI_DEVICE_TABLE(asihpi_pci_tbl) = {
2916 {HPI_PCI_VENDOR_ID_TI, HPI_PCI_DEV_ID_DSP6205,
2917 HPI_PCI_VENDOR_ID_AUDIOSCIENCE, PCI_ANY_ID, 0, 0,
2918 (kernel_ulong_t)HPI_6205},
2919 {HPI_PCI_VENDOR_ID_TI, HPI_PCI_DEV_ID_PCI2040,
2920 HPI_PCI_VENDOR_ID_AUDIOSCIENCE, PCI_ANY_ID, 0, 0,
2921 (kernel_ulong_t)HPI_6000},
2922 {0,}
2923 };
2924 MODULE_DEVICE_TABLE(pci, asihpi_pci_tbl);
2925
2926 static struct pci_driver driver = {
2927 .name = "asihpi",
2928 .id_table = asihpi_pci_tbl,
2929 .probe = snd_asihpi_probe,
2930 .remove = __devexit_p(snd_asihpi_remove),
2931 #ifdef CONFIG_PM
2932 /* .suspend = snd_asihpi_suspend,
2933 .resume = snd_asihpi_resume, */
2934 #endif
2935 };
2936
2937 static int __init snd_asihpi_init(void)
2938 {
2939 asihpi_init();
2940 return pci_register_driver(&driver);
2941 }
2942
2943 static void __exit snd_asihpi_exit(void)
2944 {
2945
2946 pci_unregister_driver(&driver);
2947 asihpi_exit();
2948 }
2949
2950 module_init(snd_asihpi_init)
2951 module_exit(snd_asihpi_exit)
2952
This page took 0.086261 seconds and 4 git commands to generate.