Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tiwai/sound-2.6
[GitHub/mt8127/android_kernel_alcatel_ttab.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 /* >0: print Hw params, timer vars. >1: print stream write/copy sizes */
26 #define REALLY_VERBOSE_LOGGING 0
27
28 #if REALLY_VERBOSE_LOGGING
29 #define VPRINTK1 snd_printd
30 #else
31 #define VPRINTK1(...)
32 #endif
33
34 #if REALLY_VERBOSE_LOGGING > 1
35 #define VPRINTK2 snd_printd
36 #else
37 #define VPRINTK2(...)
38 #endif
39
40 #include "hpi_internal.h"
41 #include "hpimsginit.h"
42 #include "hpioctl.h"
43
44 #include <linux/pci.h>
45 #include <linux/version.h>
46 #include <linux/init.h>
47 #include <linux/jiffies.h>
48 #include <linux/slab.h>
49 #include <linux/time.h>
50 #include <linux/wait.h>
51 #include <sound/core.h>
52 #include <sound/control.h>
53 #include <sound/pcm.h>
54 #include <sound/pcm_params.h>
55 #include <sound/info.h>
56 #include <sound/initval.h>
57 #include <sound/tlv.h>
58 #include <sound/hwdep.h>
59
60
61 MODULE_LICENSE("GPL");
62 MODULE_AUTHOR("AudioScience inc. <support@audioscience.com>");
63 MODULE_DESCRIPTION("AudioScience ALSA ASI5000 ASI6000 ASI87xx ASI89xx");
64
65 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* index 0-MAX */
66 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
67 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
68 static int enable_hpi_hwdep = 1;
69
70 module_param_array(index, int, NULL, S_IRUGO);
71 MODULE_PARM_DESC(index, "ALSA index value for AudioScience soundcard.");
72
73 module_param_array(id, charp, NULL, S_IRUGO);
74 MODULE_PARM_DESC(id, "ALSA ID string for AudioScience soundcard.");
75
76 module_param_array(enable, bool, NULL, S_IRUGO);
77 MODULE_PARM_DESC(enable, "ALSA enable AudioScience soundcard.");
78
79 module_param(enable_hpi_hwdep, bool, S_IRUGO|S_IWUSR);
80 MODULE_PARM_DESC(enable_hpi_hwdep,
81 "ALSA enable HPI hwdep for AudioScience soundcard ");
82
83 /* identify driver */
84 #ifdef KERNEL_ALSA_BUILD
85 static char *build_info = "Built using headers from kernel source";
86 module_param(build_info, charp, S_IRUGO);
87 MODULE_PARM_DESC(build_info, "built using headers from kernel source");
88 #else
89 static char *build_info = "Built within ALSA source";
90 module_param(build_info, charp, S_IRUGO);
91 MODULE_PARM_DESC(build_info, "built within ALSA source");
92 #endif
93
94 /* set to 1 to dump every control from adapter to log */
95 static const int mixer_dump;
96
97 #define DEFAULT_SAMPLERATE 44100
98 static int adapter_fs = DEFAULT_SAMPLERATE;
99
100 /* defaults */
101 #define PERIODS_MIN 2
102 #define PERIOD_BYTES_MIN 2048
103 #define BUFFER_BYTES_MAX (512 * 1024)
104
105 /* convert stream to character */
106 #define SCHR(s) ((s == SNDRV_PCM_STREAM_PLAYBACK) ? 'P' : 'C')
107
108 /*#define TIMER_MILLISECONDS 20
109 #define FORCE_TIMER_JIFFIES ((TIMER_MILLISECONDS * HZ + 999)/1000)
110 */
111
112 #define MAX_CLOCKSOURCES (HPI_SAMPLECLOCK_SOURCE_LAST + 1 + 7)
113
114 struct clk_source {
115 int source;
116 int index;
117 char *name;
118 };
119
120 struct clk_cache {
121 int count;
122 int has_local;
123 struct clk_source s[MAX_CLOCKSOURCES];
124 };
125
126 /* Per card data */
127 struct snd_card_asihpi {
128 struct snd_card *card;
129 struct pci_dev *pci;
130 u16 adapter_index;
131 u32 serial_number;
132 u16 type;
133 u16 version;
134 u16 num_outstreams;
135 u16 num_instreams;
136
137 u32 h_mixer;
138 struct clk_cache cc;
139
140 u16 support_mmap;
141 u16 support_grouping;
142 u16 support_mrx;
143 u16 update_interval_frames;
144 u16 in_max_chans;
145 u16 out_max_chans;
146 };
147
148 /* Per stream data */
149 struct snd_card_asihpi_pcm {
150 struct timer_list timer;
151 unsigned int respawn_timer;
152 unsigned int hpi_buffer_attached;
153 unsigned int buffer_bytes;
154 unsigned int period_bytes;
155 unsigned int bytes_per_sec;
156 unsigned int pcm_buf_host_rw_ofs; /* Host R/W pos */
157 unsigned int pcm_buf_dma_ofs; /* DMA R/W offset in buffer */
158 unsigned int pcm_buf_elapsed_dma_ofs; /* DMA R/W offset in buffer */
159 struct snd_pcm_substream *substream;
160 u32 h_stream;
161 struct hpi_format format;
162 };
163
164 /* universal stream verbs work with out or in stream handles */
165
166 /* Functions to allow driver to give a buffer to HPI for busmastering */
167
168 static u16 hpi_stream_host_buffer_attach(
169 u32 h_stream, /* handle to outstream. */
170 u32 size_in_bytes, /* size in bytes of bus mastering buffer */
171 u32 pci_address
172 )
173 {
174 struct hpi_message hm;
175 struct hpi_response hr;
176 unsigned int obj = hpi_handle_object(h_stream);
177
178 if (!h_stream)
179 return HPI_ERROR_INVALID_OBJ;
180 hpi_init_message_response(&hm, &hr, obj,
181 obj == HPI_OBJ_OSTREAM ?
182 HPI_OSTREAM_HOSTBUFFER_ALLOC :
183 HPI_ISTREAM_HOSTBUFFER_ALLOC);
184
185 hpi_handle_to_indexes(h_stream, &hm.adapter_index,
186 &hm.obj_index);
187
188 hm.u.d.u.buffer.buffer_size = size_in_bytes;
189 hm.u.d.u.buffer.pci_address = pci_address;
190 hm.u.d.u.buffer.command = HPI_BUFFER_CMD_INTERNAL_GRANTADAPTER;
191 hpi_send_recv(&hm, &hr);
192 return hr.error;
193 }
194
195 static u16 hpi_stream_host_buffer_detach(u32 h_stream)
196 {
197 struct hpi_message hm;
198 struct hpi_response hr;
199 unsigned int obj = hpi_handle_object(h_stream);
200
201 if (!h_stream)
202 return HPI_ERROR_INVALID_OBJ;
203
204 hpi_init_message_response(&hm, &hr, obj,
205 obj == HPI_OBJ_OSTREAM ?
206 HPI_OSTREAM_HOSTBUFFER_FREE :
207 HPI_ISTREAM_HOSTBUFFER_FREE);
208
209 hpi_handle_to_indexes(h_stream, &hm.adapter_index,
210 &hm.obj_index);
211 hm.u.d.u.buffer.command = HPI_BUFFER_CMD_INTERNAL_REVOKEADAPTER;
212 hpi_send_recv(&hm, &hr);
213 return hr.error;
214 }
215
216 static inline u16 hpi_stream_start(u32 h_stream)
217 {
218 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
219 return hpi_outstream_start(h_stream);
220 else
221 return hpi_instream_start(h_stream);
222 }
223
224 static inline u16 hpi_stream_stop(u32 h_stream)
225 {
226 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
227 return hpi_outstream_stop(h_stream);
228 else
229 return hpi_instream_stop(h_stream);
230 }
231
232 static inline u16 hpi_stream_get_info_ex(
233 u32 h_stream,
234 u16 *pw_state,
235 u32 *pbuffer_size,
236 u32 *pdata_in_buffer,
237 u32 *psample_count,
238 u32 *pauxiliary_data
239 )
240 {
241 u16 e;
242 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
243 e = hpi_outstream_get_info_ex(h_stream, pw_state,
244 pbuffer_size, pdata_in_buffer,
245 psample_count, pauxiliary_data);
246 else
247 e = hpi_instream_get_info_ex(h_stream, pw_state,
248 pbuffer_size, pdata_in_buffer,
249 psample_count, pauxiliary_data);
250 return e;
251 }
252
253 static inline u16 hpi_stream_group_add(
254 u32 h_master,
255 u32 h_stream)
256 {
257 if (hpi_handle_object(h_master) == HPI_OBJ_OSTREAM)
258 return hpi_outstream_group_add(h_master, h_stream);
259 else
260 return hpi_instream_group_add(h_master, h_stream);
261 }
262
263 static inline u16 hpi_stream_group_reset(u32 h_stream)
264 {
265 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
266 return hpi_outstream_group_reset(h_stream);
267 else
268 return hpi_instream_group_reset(h_stream);
269 }
270
271 static inline u16 hpi_stream_group_get_map(
272 u32 h_stream, u32 *mo, u32 *mi)
273 {
274 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
275 return hpi_outstream_group_get_map(h_stream, mo, mi);
276 else
277 return hpi_instream_group_get_map(h_stream, mo, mi);
278 }
279
280 static u16 handle_error(u16 err, int line, char *filename)
281 {
282 if (err)
283 printk(KERN_WARNING
284 "in file %s, line %d: HPI error %d\n",
285 filename, line, err);
286 return err;
287 }
288
289 #define hpi_handle_error(x) handle_error(x, __LINE__, __FILE__)
290
291 /***************************** GENERAL PCM ****************/
292 #if REALLY_VERBOSE_LOGGING
293 static void print_hwparams(struct snd_pcm_hw_params *p)
294 {
295 snd_printd("HWPARAMS \n");
296 snd_printd("samplerate %d \n", params_rate(p));
297 snd_printd("Channels %d \n", params_channels(p));
298 snd_printd("Format %d \n", params_format(p));
299 snd_printd("subformat %d \n", params_subformat(p));
300 snd_printd("Buffer bytes %d \n", params_buffer_bytes(p));
301 snd_printd("Period bytes %d \n", params_period_bytes(p));
302 snd_printd("access %d \n", params_access(p));
303 snd_printd("period_size %d \n", params_period_size(p));
304 snd_printd("periods %d \n", params_periods(p));
305 snd_printd("buffer_size %d \n", params_buffer_size(p));
306 }
307 #else
308 #define print_hwparams(x)
309 #endif
310
311 static snd_pcm_format_t hpi_to_alsa_formats[] = {
312 -1, /* INVALID */
313 SNDRV_PCM_FORMAT_U8, /* HPI_FORMAT_PCM8_UNSIGNED 1 */
314 SNDRV_PCM_FORMAT_S16, /* HPI_FORMAT_PCM16_SIGNED 2 */
315 -1, /* HPI_FORMAT_MPEG_L1 3 */
316 SNDRV_PCM_FORMAT_MPEG, /* HPI_FORMAT_MPEG_L2 4 */
317 SNDRV_PCM_FORMAT_MPEG, /* HPI_FORMAT_MPEG_L3 5 */
318 -1, /* HPI_FORMAT_DOLBY_AC2 6 */
319 -1, /* HPI_FORMAT_DOLBY_AC3 7 */
320 SNDRV_PCM_FORMAT_S16_BE,/* HPI_FORMAT_PCM16_BIGENDIAN 8 */
321 -1, /* HPI_FORMAT_AA_TAGIT1_HITS 9 */
322 -1, /* HPI_FORMAT_AA_TAGIT1_INSERTS 10 */
323 SNDRV_PCM_FORMAT_S32, /* HPI_FORMAT_PCM32_SIGNED 11 */
324 -1, /* HPI_FORMAT_RAW_BITSTREAM 12 */
325 -1, /* HPI_FORMAT_AA_TAGIT1_HITS_EX1 13 */
326 SNDRV_PCM_FORMAT_FLOAT, /* HPI_FORMAT_PCM32_FLOAT 14 */
327 #if 1
328 /* ALSA can't handle 3 byte sample size together with power-of-2
329 * constraint on buffer_bytes, so disable this format
330 */
331 -1
332 #else
333 /* SNDRV_PCM_FORMAT_S24_3LE */ /* HPI_FORMAT_PCM24_SIGNED 15 */
334 #endif
335 };
336
337
338 static int snd_card_asihpi_format_alsa2hpi(snd_pcm_format_t alsa_format,
339 u16 *hpi_format)
340 {
341 u16 format;
342
343 for (format = HPI_FORMAT_PCM8_UNSIGNED;
344 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
345 if (hpi_to_alsa_formats[format] == alsa_format) {
346 *hpi_format = format;
347 return 0;
348 }
349 }
350
351 snd_printd(KERN_WARNING "failed match for alsa format %d\n",
352 alsa_format);
353 *hpi_format = 0;
354 return -EINVAL;
355 }
356
357 static void snd_card_asihpi_pcm_samplerates(struct snd_card_asihpi *asihpi,
358 struct snd_pcm_hardware *pcmhw)
359 {
360 u16 err;
361 u32 h_control;
362 u32 sample_rate;
363 int idx;
364 unsigned int rate_min = 200000;
365 unsigned int rate_max = 0;
366 unsigned int rates = 0;
367
368 if (asihpi->support_mrx) {
369 rates |= SNDRV_PCM_RATE_CONTINUOUS;
370 rates |= SNDRV_PCM_RATE_8000_96000;
371 rate_min = 8000;
372 rate_max = 100000;
373 } else {
374 /* on cards without SRC,
375 valid rates are determined by sampleclock */
376 err = hpi_mixer_get_control(asihpi->h_mixer,
377 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
378 HPI_CONTROL_SAMPLECLOCK, &h_control);
379 if (err) {
380 snd_printk(KERN_ERR
381 "No local sampleclock, err %d\n", err);
382 }
383
384 for (idx = 0; idx < 100; idx++) {
385 if (hpi_sample_clock_query_local_rate(
386 h_control, idx, &sample_rate)) {
387 if (!idx)
388 snd_printk(KERN_ERR
389 "Local rate query failed\n");
390
391 break;
392 }
393
394 rate_min = min(rate_min, sample_rate);
395 rate_max = max(rate_max, sample_rate);
396
397 switch (sample_rate) {
398 case 5512:
399 rates |= SNDRV_PCM_RATE_5512;
400 break;
401 case 8000:
402 rates |= SNDRV_PCM_RATE_8000;
403 break;
404 case 11025:
405 rates |= SNDRV_PCM_RATE_11025;
406 break;
407 case 16000:
408 rates |= SNDRV_PCM_RATE_16000;
409 break;
410 case 22050:
411 rates |= SNDRV_PCM_RATE_22050;
412 break;
413 case 32000:
414 rates |= SNDRV_PCM_RATE_32000;
415 break;
416 case 44100:
417 rates |= SNDRV_PCM_RATE_44100;
418 break;
419 case 48000:
420 rates |= SNDRV_PCM_RATE_48000;
421 break;
422 case 64000:
423 rates |= SNDRV_PCM_RATE_64000;
424 break;
425 case 88200:
426 rates |= SNDRV_PCM_RATE_88200;
427 break;
428 case 96000:
429 rates |= SNDRV_PCM_RATE_96000;
430 break;
431 case 176400:
432 rates |= SNDRV_PCM_RATE_176400;
433 break;
434 case 192000:
435 rates |= SNDRV_PCM_RATE_192000;
436 break;
437 default: /* some other rate */
438 rates |= SNDRV_PCM_RATE_KNOT;
439 }
440 }
441 }
442
443 /* printk(KERN_INFO "Supported rates %X %d %d\n",
444 rates, rate_min, rate_max); */
445 pcmhw->rates = rates;
446 pcmhw->rate_min = rate_min;
447 pcmhw->rate_max = rate_max;
448 }
449
450 static int snd_card_asihpi_pcm_hw_params(struct snd_pcm_substream *substream,
451 struct snd_pcm_hw_params *params)
452 {
453 struct snd_pcm_runtime *runtime = substream->runtime;
454 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
455 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
456 int err;
457 u16 format;
458 int width;
459 unsigned int bytes_per_sec;
460
461 print_hwparams(params);
462 err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
463 if (err < 0)
464 return err;
465 err = snd_card_asihpi_format_alsa2hpi(params_format(params), &format);
466 if (err)
467 return err;
468
469 VPRINTK1(KERN_INFO "format %d, %d chans, %d_hz\n",
470 format, params_channels(params),
471 params_rate(params));
472
473 hpi_handle_error(hpi_format_create(&dpcm->format,
474 params_channels(params),
475 format, params_rate(params), 0, 0));
476
477 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
478 if (hpi_instream_reset(dpcm->h_stream) != 0)
479 return -EINVAL;
480
481 if (hpi_instream_set_format(
482 dpcm->h_stream, &dpcm->format) != 0)
483 return -EINVAL;
484 }
485
486 dpcm->hpi_buffer_attached = 0;
487 if (card->support_mmap) {
488
489 err = hpi_stream_host_buffer_attach(dpcm->h_stream,
490 params_buffer_bytes(params), runtime->dma_addr);
491 if (err == 0) {
492 VPRINTK1(KERN_INFO
493 "stream_host_buffer_attach succeeded %u %lu\n",
494 params_buffer_bytes(params),
495 (unsigned long)runtime->dma_addr);
496 } else {
497 snd_printd(KERN_INFO
498 "stream_host_buffer_attach error %d\n",
499 err);
500 return -ENOMEM;
501 }
502
503 err = hpi_stream_get_info_ex(dpcm->h_stream, NULL,
504 &dpcm->hpi_buffer_attached,
505 NULL, NULL, NULL);
506
507 VPRINTK1(KERN_INFO "stream_host_buffer_attach status 0x%x\n",
508 dpcm->hpi_buffer_attached);
509 }
510 bytes_per_sec = params_rate(params) * params_channels(params);
511 width = snd_pcm_format_width(params_format(params));
512 bytes_per_sec *= width;
513 bytes_per_sec /= 8;
514 if (width < 0 || bytes_per_sec == 0)
515 return -EINVAL;
516
517 dpcm->bytes_per_sec = bytes_per_sec;
518 dpcm->buffer_bytes = params_buffer_bytes(params);
519 dpcm->period_bytes = params_period_bytes(params);
520 VPRINTK1(KERN_INFO "buffer_bytes=%d, period_bytes=%d, bps=%d\n",
521 dpcm->buffer_bytes, dpcm->period_bytes, bytes_per_sec);
522
523 return 0;
524 }
525
526 static int
527 snd_card_asihpi_hw_free(struct snd_pcm_substream *substream)
528 {
529 struct snd_pcm_runtime *runtime = substream->runtime;
530 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
531 if (dpcm->hpi_buffer_attached)
532 hpi_stream_host_buffer_detach(dpcm->h_stream);
533
534 snd_pcm_lib_free_pages(substream);
535 return 0;
536 }
537
538 static void snd_card_asihpi_runtime_free(struct snd_pcm_runtime *runtime)
539 {
540 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
541 kfree(dpcm);
542 }
543
544 static void snd_card_asihpi_pcm_timer_start(struct snd_pcm_substream *
545 substream)
546 {
547 struct snd_pcm_runtime *runtime = substream->runtime;
548 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
549 int expiry;
550
551 expiry = HZ / 200;
552 /*? (dpcm->period_bytes * HZ / dpcm->bytes_per_sec); */
553 expiry = max(expiry, 1); /* don't let it be zero! */
554 dpcm->timer.expires = jiffies + expiry;
555 dpcm->respawn_timer = 1;
556 add_timer(&dpcm->timer);
557 }
558
559 static void snd_card_asihpi_pcm_timer_stop(struct snd_pcm_substream *substream)
560 {
561 struct snd_pcm_runtime *runtime = substream->runtime;
562 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
563
564 dpcm->respawn_timer = 0;
565 del_timer(&dpcm->timer);
566 }
567
568 static int snd_card_asihpi_trigger(struct snd_pcm_substream *substream,
569 int cmd)
570 {
571 struct snd_card_asihpi_pcm *dpcm = substream->runtime->private_data;
572 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
573 struct snd_pcm_substream *s;
574 u16 e;
575
576 VPRINTK1(KERN_INFO "%c%d trigger\n",
577 SCHR(substream->stream), substream->number);
578 switch (cmd) {
579 case SNDRV_PCM_TRIGGER_START:
580 snd_pcm_group_for_each_entry(s, substream) {
581 struct snd_pcm_runtime *runtime = s->runtime;
582 struct snd_card_asihpi_pcm *ds = runtime->private_data;
583
584 if (snd_pcm_substream_chip(s) != card)
585 continue;
586
587 /* don't link Cap and Play */
588 if (substream->stream != s->stream)
589 continue;
590
591 if ((s->stream == SNDRV_PCM_STREAM_PLAYBACK) &&
592 (card->support_mmap)) {
593 /* How do I know how much valid data is present
594 * in buffer? Must be at least one period!
595 * Guessing 2 periods, but if
596 * buffer is bigger it may contain even more
597 * data??
598 */
599 unsigned int preload = ds->period_bytes * 1;
600 VPRINTK2(KERN_INFO "%d preload x%x\n", s->number, preload);
601 hpi_handle_error(hpi_outstream_write_buf(
602 ds->h_stream,
603 &runtime->dma_area[0],
604 preload,
605 &ds->format));
606 ds->pcm_buf_host_rw_ofs = preload;
607 }
608
609 if (card->support_grouping) {
610 VPRINTK1(KERN_INFO "\t%c%d group\n",
611 SCHR(s->stream),
612 s->number);
613 e = hpi_stream_group_add(
614 dpcm->h_stream,
615 ds->h_stream);
616 if (!e) {
617 snd_pcm_trigger_done(s, substream);
618 } else {
619 hpi_handle_error(e);
620 break;
621 }
622 } else
623 break;
624 }
625 VPRINTK1(KERN_INFO "start\n");
626 /* start the master stream */
627 snd_card_asihpi_pcm_timer_start(substream);
628 if ((substream->stream == SNDRV_PCM_STREAM_CAPTURE) ||
629 !card->support_mmap)
630 hpi_handle_error(hpi_stream_start(dpcm->h_stream));
631 break;
632
633 case SNDRV_PCM_TRIGGER_STOP:
634 snd_card_asihpi_pcm_timer_stop(substream);
635 snd_pcm_group_for_each_entry(s, substream) {
636 if (snd_pcm_substream_chip(s) != card)
637 continue;
638 /* don't link Cap and Play */
639 if (substream->stream != s->stream)
640 continue;
641
642 /*? workaround linked streams don't
643 transition to SETUP 20070706*/
644 s->runtime->status->state = SNDRV_PCM_STATE_SETUP;
645
646 if (card->support_grouping) {
647 VPRINTK1(KERN_INFO "\t%c%d group\n",
648 SCHR(s->stream),
649 s->number);
650 snd_pcm_trigger_done(s, substream);
651 } else
652 break;
653 }
654 VPRINTK1(KERN_INFO "stop\n");
655
656 /* _prepare and _hwparams reset the stream */
657 hpi_handle_error(hpi_stream_stop(dpcm->h_stream));
658 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
659 hpi_handle_error(
660 hpi_outstream_reset(dpcm->h_stream));
661
662 if (card->support_grouping)
663 hpi_handle_error(hpi_stream_group_reset(dpcm->h_stream));
664 break;
665
666 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
667 VPRINTK1(KERN_INFO "pause release\n");
668 hpi_handle_error(hpi_stream_start(dpcm->h_stream));
669 snd_card_asihpi_pcm_timer_start(substream);
670 break;
671 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
672 VPRINTK1(KERN_INFO "pause\n");
673 snd_card_asihpi_pcm_timer_stop(substream);
674 hpi_handle_error(hpi_stream_stop(dpcm->h_stream));
675 break;
676 default:
677 snd_printd(KERN_ERR "\tINVALID\n");
678 return -EINVAL;
679 }
680
681 return 0;
682 }
683
684 /*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 /*
689 pcm_buf_dma_ofs=get_buf_pos(s);
690 for_each_linked_stream(s) {
691 pcm_buf_dma_ofs=get_buf_pos(s);
692 min_buf_pos = modulo_min(min_buf_pos, pcm_buf_dma_ofs, buffer_bytes)
693 new_data = min(new_data, calc_new_data(pcm_buf_dma_ofs,irq_pos)
694 }
695 timer.expires = jiffies + predict_next_period_ready(min_buf_pos);
696 for_each_linked_stream(s) {
697 s->pcm_buf_dma_ofs = min_buf_pos;
698 if (new_data > period_bytes) {
699 if (mmap) {
700 irq_pos = (irq_pos + period_bytes) % buffer_bytes;
701 if (playback) {
702 write(period_bytes);
703 } else {
704 read(period_bytes);
705 }
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 */
715 static 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 */
729 static void snd_card_asihpi_timer_function(unsigned long data)
730 {
731 struct snd_card_asihpi_pcm *dpcm = (struct snd_card_asihpi_pcm *)data;
732 struct snd_pcm_substream *substream = dpcm->substream;
733 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
734 struct snd_pcm_runtime *runtime;
735 struct snd_pcm_substream *s;
736 unsigned int newdata = 0;
737 unsigned int pcm_buf_dma_ofs, min_buf_pos = 0;
738 unsigned int remdata, xfercount, next_jiffies;
739 int first = 1;
740 int loops = 0;
741 u16 state;
742 u32 buffer_size, bytes_avail, samples_played, on_card_bytes;
743
744 VPRINTK1(KERN_INFO "%c%d snd_card_asihpi_timer_function\n",
745 SCHR(substream->stream), substream->number);
746
747 /* find minimum newdata and buffer pos in group */
748 snd_pcm_group_for_each_entry(s, substream) {
749 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
755 /* don't link Cap and Play */
756 if (substream->stream != s->stream)
757 continue;
758
759 hpi_handle_error(hpi_stream_get_info_ex(
760 ds->h_stream, &state,
761 &buffer_size, &bytes_avail,
762 &samples_played, &on_card_bytes));
763
764 /* number of bytes in on-card buffer */
765 runtime->delay = on_card_bytes;
766
767 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
768 pcm_buf_dma_ofs = ds->pcm_buf_host_rw_ofs - bytes_avail;
769 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));
773 VPRINTK1(KERN_INFO "P%d start\n", s->number);
774 }
775 } else if (state == HPI_STATE_DRAINED) {
776 VPRINTK1(KERN_WARNING "P%d drained\n",
777 s->number);
778 /*snd_pcm_stop(s, SNDRV_PCM_STATE_XRUN);
779 continue; */
780 }
781 } else
782 pcm_buf_dma_ofs = bytes_avail + ds->pcm_buf_host_rw_ofs;
783
784 if (first) {
785 /* can't statically init min when wrap is involved */
786 min_buf_pos = pcm_buf_dma_ofs;
787 newdata = (pcm_buf_dma_ofs - ds->pcm_buf_elapsed_dma_ofs) % ds->buffer_bytes;
788 first = 0;
789 } else {
790 min_buf_pos =
791 modulo_min(min_buf_pos, pcm_buf_dma_ofs, UINT_MAX+1L);
792 newdata = min(
793 (pcm_buf_dma_ofs - ds->pcm_buf_elapsed_dma_ofs) % ds->buffer_bytes,
794 newdata);
795 }
796
797 VPRINTK1(KERN_INFO "PB timer hw_ptr x%04lX, appl_ptr x%04lX\n",
798 (unsigned long)frames_to_bytes(runtime,
799 runtime->status->hw_ptr),
800 (unsigned long)frames_to_bytes(runtime,
801 runtime->control->appl_ptr));
802
803 VPRINTK1(KERN_INFO "%d %c%d S=%d, rw=%04X, dma=x%04X, left=x%04X,"
804 " aux=x%04X space=x%04X\n",
805 loops, SCHR(s->stream), s->number,
806 state, ds->pcm_buf_host_rw_ofs, pcm_buf_dma_ofs, (int)bytes_avail,
807 (int)on_card_bytes, buffer_size-bytes_avail);
808 loops++;
809 }
810 pcm_buf_dma_ofs = min_buf_pos;
811
812 remdata = newdata % dpcm->period_bytes;
813 xfercount = newdata - remdata; /* a multiple of period_bytes */
814 /* come back when on_card_bytes has decreased enough to allow
815 write to happen, or when data has been consumed to make another
816 period
817 */
818 if (xfercount && (on_card_bytes > dpcm->period_bytes))
819 next_jiffies = ((on_card_bytes - dpcm->period_bytes) * HZ / dpcm->bytes_per_sec);
820 else
821 next_jiffies = ((dpcm->period_bytes - remdata) * HZ / dpcm->bytes_per_sec);
822
823 next_jiffies = max(next_jiffies, 1U);
824 dpcm->timer.expires = jiffies + next_jiffies;
825 VPRINTK1(KERN_INFO "jif %d buf pos x%04X newdata x%04X xfer x%04X\n",
826 next_jiffies, pcm_buf_dma_ofs, newdata, xfercount);
827
828 snd_pcm_group_for_each_entry(s, substream) {
829 struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
830
831 /* don't link Cap and Play */
832 if (substream->stream != s->stream)
833 continue;
834
835 ds->pcm_buf_dma_ofs = pcm_buf_dma_ofs;
836
837 if (xfercount && (on_card_bytes <= ds->period_bytes)) {
838 if (card->support_mmap) {
839 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
840 VPRINTK2(KERN_INFO "P%d write x%04x\n",
841 s->number,
842 ds->period_bytes);
843 hpi_handle_error(
844 hpi_outstream_write_buf(
845 ds->h_stream,
846 &s->runtime->
847 dma_area[0],
848 xfercount,
849 &ds->format));
850 } else {
851 VPRINTK2(KERN_INFO "C%d read x%04x\n",
852 s->number,
853 xfercount);
854 hpi_handle_error(
855 hpi_instream_read_buf(
856 ds->h_stream,
857 NULL, xfercount));
858 }
859 ds->pcm_buf_host_rw_ofs = ds->pcm_buf_host_rw_ofs + xfercount;
860 } /* else R/W will be handled by read/write callbacks */
861 ds->pcm_buf_elapsed_dma_ofs = pcm_buf_dma_ofs;
862 snd_pcm_period_elapsed(s);
863 }
864 }
865
866 if (dpcm->respawn_timer)
867 add_timer(&dpcm->timer);
868 }
869
870 /***************************** PLAYBACK OPS ****************/
871 static int snd_card_asihpi_playback_ioctl(struct snd_pcm_substream *substream,
872 unsigned int cmd, void *arg)
873 {
874 /* snd_printd(KERN_INFO "Playback ioctl %d\n", cmd); */
875 return snd_pcm_lib_ioctl(substream, cmd, arg);
876 }
877
878 static int snd_card_asihpi_playback_prepare(struct snd_pcm_substream *
879 substream)
880 {
881 struct snd_pcm_runtime *runtime = substream->runtime;
882 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
883
884 VPRINTK1(KERN_INFO "playback prepare %d\n", substream->number);
885
886 hpi_handle_error(hpi_outstream_reset(dpcm->h_stream));
887 dpcm->pcm_buf_host_rw_ofs = 0;
888 dpcm->pcm_buf_dma_ofs = 0;
889 dpcm->pcm_buf_elapsed_dma_ofs = 0;
890 return 0;
891 }
892
893 static snd_pcm_uframes_t
894 snd_card_asihpi_playback_pointer(struct snd_pcm_substream *substream)
895 {
896 struct snd_pcm_runtime *runtime = substream->runtime;
897 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
898 snd_pcm_uframes_t ptr;
899
900 ptr = bytes_to_frames(runtime, dpcm->pcm_buf_dma_ofs % dpcm->buffer_bytes);
901 /* VPRINTK2(KERN_INFO "playback_pointer=x%04lx\n", (unsigned long)ptr); */
902 return ptr;
903 }
904
905 static void snd_card_asihpi_playback_format(struct snd_card_asihpi *asihpi,
906 u32 h_stream,
907 struct snd_pcm_hardware *pcmhw)
908 {
909 struct hpi_format hpi_format;
910 u16 format;
911 u16 err;
912 u32 h_control;
913 u32 sample_rate = 48000;
914
915 /* on cards without SRC, must query at valid rate,
916 * maybe set by external sync
917 */
918 err = hpi_mixer_get_control(asihpi->h_mixer,
919 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
920 HPI_CONTROL_SAMPLECLOCK, &h_control);
921
922 if (!err)
923 err = hpi_sample_clock_get_sample_rate(h_control,
924 &sample_rate);
925
926 for (format = HPI_FORMAT_PCM8_UNSIGNED;
927 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
928 err = hpi_format_create(&hpi_format,
929 2, format, sample_rate, 128000, 0);
930 if (!err)
931 err = hpi_outstream_query_format(h_stream,
932 &hpi_format);
933 if (!err && (hpi_to_alsa_formats[format] != -1))
934 pcmhw->formats |=
935 (1ULL << hpi_to_alsa_formats[format]);
936 }
937 }
938
939 static struct snd_pcm_hardware snd_card_asihpi_playback = {
940 .channels_min = 1,
941 .channels_max = 2,
942 .buffer_bytes_max = BUFFER_BYTES_MAX,
943 .period_bytes_min = PERIOD_BYTES_MIN,
944 .period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN,
945 .periods_min = PERIODS_MIN,
946 .periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN,
947 .fifo_size = 0,
948 };
949
950 static int snd_card_asihpi_playback_open(struct snd_pcm_substream *substream)
951 {
952 struct snd_pcm_runtime *runtime = substream->runtime;
953 struct snd_card_asihpi_pcm *dpcm;
954 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
955 int err;
956
957 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
958 if (dpcm == NULL)
959 return -ENOMEM;
960
961 err =
962 hpi_outstream_open(card->adapter_index,
963 substream->number, &dpcm->h_stream);
964 hpi_handle_error(err);
965 if (err)
966 kfree(dpcm);
967 if (err == HPI_ERROR_OBJ_ALREADY_OPEN)
968 return -EBUSY;
969 if (err)
970 return -EIO;
971
972 /*? also check ASI5000 samplerate source
973 If external, only support external rate.
974 If internal and other stream playing, cant switch
975 */
976
977 init_timer(&dpcm->timer);
978 dpcm->timer.data = (unsigned long) dpcm;
979 dpcm->timer.function = snd_card_asihpi_timer_function;
980 dpcm->substream = substream;
981 runtime->private_data = dpcm;
982 runtime->private_free = snd_card_asihpi_runtime_free;
983
984 snd_card_asihpi_playback.channels_max = card->out_max_chans;
985 /*?snd_card_asihpi_playback.period_bytes_min =
986 card->out_max_chans * 4096; */
987
988 snd_card_asihpi_playback_format(card, dpcm->h_stream,
989 &snd_card_asihpi_playback);
990
991 snd_card_asihpi_pcm_samplerates(card, &snd_card_asihpi_playback);
992
993 snd_card_asihpi_playback.info = SNDRV_PCM_INFO_INTERLEAVED |
994 SNDRV_PCM_INFO_DOUBLE |
995 SNDRV_PCM_INFO_BATCH |
996 SNDRV_PCM_INFO_BLOCK_TRANSFER |
997 SNDRV_PCM_INFO_PAUSE;
998
999 if (card->support_mmap)
1000 snd_card_asihpi_playback.info |= SNDRV_PCM_INFO_MMAP |
1001 SNDRV_PCM_INFO_MMAP_VALID;
1002
1003 if (card->support_grouping)
1004 snd_card_asihpi_playback.info |= SNDRV_PCM_INFO_SYNC_START;
1005
1006 /* struct is copied, so can create initializer dynamically */
1007 runtime->hw = snd_card_asihpi_playback;
1008
1009 if (card->support_mmap)
1010 err = snd_pcm_hw_constraint_pow2(runtime, 0,
1011 SNDRV_PCM_HW_PARAM_BUFFER_BYTES);
1012 if (err < 0)
1013 return err;
1014
1015 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1016 card->update_interval_frames);
1017 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1018 card->update_interval_frames * 2, UINT_MAX);
1019
1020 snd_pcm_set_sync(substream);
1021
1022 VPRINTK1(KERN_INFO "playback open\n");
1023
1024 return 0;
1025 }
1026
1027 static int snd_card_asihpi_playback_close(struct snd_pcm_substream *substream)
1028 {
1029 struct snd_pcm_runtime *runtime = substream->runtime;
1030 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1031
1032 hpi_handle_error(hpi_outstream_close(dpcm->h_stream));
1033 VPRINTK1(KERN_INFO "playback close\n");
1034
1035 return 0;
1036 }
1037
1038 static int snd_card_asihpi_playback_copy(struct snd_pcm_substream *substream,
1039 int channel,
1040 snd_pcm_uframes_t pos,
1041 void __user *src,
1042 snd_pcm_uframes_t count)
1043 {
1044 struct snd_pcm_runtime *runtime = substream->runtime;
1045 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1046 unsigned int len;
1047
1048 len = frames_to_bytes(runtime, count);
1049
1050 if (copy_from_user(runtime->dma_area, src, len))
1051 return -EFAULT;
1052
1053 VPRINTK2(KERN_DEBUG "playback copy%d %u bytes\n",
1054 substream->number, len);
1055
1056 hpi_handle_error(hpi_outstream_write_buf(dpcm->h_stream,
1057 runtime->dma_area, len, &dpcm->format));
1058
1059 dpcm->pcm_buf_host_rw_ofs = dpcm->pcm_buf_host_rw_ofs + len;
1060
1061 return 0;
1062 }
1063
1064 static int snd_card_asihpi_playback_silence(struct snd_pcm_substream *
1065 substream, int channel,
1066 snd_pcm_uframes_t pos,
1067 snd_pcm_uframes_t count)
1068 {
1069 unsigned int len;
1070 struct snd_pcm_runtime *runtime = substream->runtime;
1071 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1072
1073 len = frames_to_bytes(runtime, count);
1074 VPRINTK1(KERN_INFO "playback silence %u bytes\n", len);
1075
1076 memset(runtime->dma_area, 0, len);
1077 hpi_handle_error(hpi_outstream_write_buf(dpcm->h_stream,
1078 runtime->dma_area, len, &dpcm->format));
1079 return 0;
1080 }
1081
1082 static struct snd_pcm_ops snd_card_asihpi_playback_ops = {
1083 .open = snd_card_asihpi_playback_open,
1084 .close = snd_card_asihpi_playback_close,
1085 .ioctl = snd_card_asihpi_playback_ioctl,
1086 .hw_params = snd_card_asihpi_pcm_hw_params,
1087 .hw_free = snd_card_asihpi_hw_free,
1088 .prepare = snd_card_asihpi_playback_prepare,
1089 .trigger = snd_card_asihpi_trigger,
1090 .pointer = snd_card_asihpi_playback_pointer,
1091 .copy = snd_card_asihpi_playback_copy,
1092 .silence = snd_card_asihpi_playback_silence,
1093 };
1094
1095 static struct snd_pcm_ops snd_card_asihpi_playback_mmap_ops = {
1096 .open = snd_card_asihpi_playback_open,
1097 .close = snd_card_asihpi_playback_close,
1098 .ioctl = snd_card_asihpi_playback_ioctl,
1099 .hw_params = snd_card_asihpi_pcm_hw_params,
1100 .hw_free = snd_card_asihpi_hw_free,
1101 .prepare = snd_card_asihpi_playback_prepare,
1102 .trigger = snd_card_asihpi_trigger,
1103 .pointer = snd_card_asihpi_playback_pointer,
1104 };
1105
1106 /***************************** CAPTURE OPS ****************/
1107 static snd_pcm_uframes_t
1108 snd_card_asihpi_capture_pointer(struct snd_pcm_substream *substream)
1109 {
1110 struct snd_pcm_runtime *runtime = substream->runtime;
1111 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1112
1113 VPRINTK2(KERN_INFO "capture pointer %d=%d\n",
1114 substream->number, dpcm->pcm_buf_dma_ofs);
1115 /* NOTE Unlike playback can't use actual samples_played
1116 for the capture position, because those samples aren't yet in
1117 the local buffer available for reading.
1118 */
1119 return bytes_to_frames(runtime, dpcm->pcm_buf_dma_ofs % dpcm->buffer_bytes);
1120 }
1121
1122 static int snd_card_asihpi_capture_ioctl(struct snd_pcm_substream *substream,
1123 unsigned int cmd, void *arg)
1124 {
1125 return snd_pcm_lib_ioctl(substream, cmd, arg);
1126 }
1127
1128 static int snd_card_asihpi_capture_prepare(struct snd_pcm_substream *substream)
1129 {
1130 struct snd_pcm_runtime *runtime = substream->runtime;
1131 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1132
1133 hpi_handle_error(hpi_instream_reset(dpcm->h_stream));
1134 dpcm->pcm_buf_host_rw_ofs = 0;
1135 dpcm->pcm_buf_dma_ofs = 0;
1136 dpcm->pcm_buf_elapsed_dma_ofs = 0;
1137
1138 VPRINTK1("Capture Prepare %d\n", substream->number);
1139 return 0;
1140 }
1141
1142
1143
1144 static void snd_card_asihpi_capture_format(struct snd_card_asihpi *asihpi,
1145 u32 h_stream,
1146 struct snd_pcm_hardware *pcmhw)
1147 {
1148 struct hpi_format hpi_format;
1149 u16 format;
1150 u16 err;
1151 u32 h_control;
1152 u32 sample_rate = 48000;
1153
1154 /* on cards without SRC, must query at valid rate,
1155 maybe set by external sync */
1156 err = hpi_mixer_get_control(asihpi->h_mixer,
1157 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
1158 HPI_CONTROL_SAMPLECLOCK, &h_control);
1159
1160 if (!err)
1161 err = hpi_sample_clock_get_sample_rate(h_control,
1162 &sample_rate);
1163
1164 for (format = HPI_FORMAT_PCM8_UNSIGNED;
1165 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
1166
1167 err = hpi_format_create(&hpi_format, 2, format,
1168 sample_rate, 128000, 0);
1169 if (!err)
1170 err = hpi_instream_query_format(h_stream,
1171 &hpi_format);
1172 if (!err)
1173 pcmhw->formats |=
1174 (1ULL << hpi_to_alsa_formats[format]);
1175 }
1176 }
1177
1178
1179 static struct snd_pcm_hardware snd_card_asihpi_capture = {
1180 .channels_min = 1,
1181 .channels_max = 2,
1182 .buffer_bytes_max = BUFFER_BYTES_MAX,
1183 .period_bytes_min = PERIOD_BYTES_MIN,
1184 .period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN,
1185 .periods_min = PERIODS_MIN,
1186 .periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN,
1187 .fifo_size = 0,
1188 };
1189
1190 static int snd_card_asihpi_capture_open(struct snd_pcm_substream *substream)
1191 {
1192 struct snd_pcm_runtime *runtime = substream->runtime;
1193 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
1194 struct snd_card_asihpi_pcm *dpcm;
1195 int err;
1196
1197 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
1198 if (dpcm == NULL)
1199 return -ENOMEM;
1200
1201 VPRINTK1("hpi_instream_open adapter %d stream %d\n",
1202 card->adapter_index, substream->number);
1203
1204 err = hpi_handle_error(
1205 hpi_instream_open(card->adapter_index,
1206 substream->number, &dpcm->h_stream));
1207 if (err)
1208 kfree(dpcm);
1209 if (err == HPI_ERROR_OBJ_ALREADY_OPEN)
1210 return -EBUSY;
1211 if (err)
1212 return -EIO;
1213
1214
1215 init_timer(&dpcm->timer);
1216 dpcm->timer.data = (unsigned long) dpcm;
1217 dpcm->timer.function = snd_card_asihpi_timer_function;
1218 dpcm->substream = substream;
1219 runtime->private_data = dpcm;
1220 runtime->private_free = snd_card_asihpi_runtime_free;
1221
1222 snd_card_asihpi_capture.channels_max = card->in_max_chans;
1223 snd_card_asihpi_capture_format(card, dpcm->h_stream,
1224 &snd_card_asihpi_capture);
1225 snd_card_asihpi_pcm_samplerates(card, &snd_card_asihpi_capture);
1226 snd_card_asihpi_capture.info = SNDRV_PCM_INFO_INTERLEAVED;
1227
1228 if (card->support_mmap)
1229 snd_card_asihpi_capture.info |= SNDRV_PCM_INFO_MMAP |
1230 SNDRV_PCM_INFO_MMAP_VALID;
1231
1232 if (card->support_grouping)
1233 snd_card_asihpi_capture.info |= SNDRV_PCM_INFO_SYNC_START;
1234
1235 runtime->hw = snd_card_asihpi_capture;
1236
1237 if (card->support_mmap)
1238 err = snd_pcm_hw_constraint_pow2(runtime, 0,
1239 SNDRV_PCM_HW_PARAM_BUFFER_BYTES);
1240 if (err < 0)
1241 return err;
1242
1243 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1244 card->update_interval_frames);
1245 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1246 card->update_interval_frames * 2, UINT_MAX);
1247
1248 snd_pcm_set_sync(substream);
1249
1250 return 0;
1251 }
1252
1253 static int snd_card_asihpi_capture_close(struct snd_pcm_substream *substream)
1254 {
1255 struct snd_card_asihpi_pcm *dpcm = substream->runtime->private_data;
1256
1257 hpi_handle_error(hpi_instream_close(dpcm->h_stream));
1258 return 0;
1259 }
1260
1261 static int snd_card_asihpi_capture_copy(struct snd_pcm_substream *substream,
1262 int channel, snd_pcm_uframes_t pos,
1263 void __user *dst, snd_pcm_uframes_t count)
1264 {
1265 struct snd_pcm_runtime *runtime = substream->runtime;
1266 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1267 u32 len;
1268
1269 len = frames_to_bytes(runtime, count);
1270
1271 VPRINTK2(KERN_INFO "capture copy%d %d bytes\n", substream->number, len);
1272 hpi_handle_error(hpi_instream_read_buf(dpcm->h_stream,
1273 runtime->dma_area, len));
1274
1275 dpcm->pcm_buf_host_rw_ofs = dpcm->pcm_buf_host_rw_ofs + len;
1276
1277 if (copy_to_user(dst, runtime->dma_area, len))
1278 return -EFAULT;
1279
1280 return 0;
1281 }
1282
1283 static struct snd_pcm_ops snd_card_asihpi_capture_mmap_ops = {
1284 .open = snd_card_asihpi_capture_open,
1285 .close = snd_card_asihpi_capture_close,
1286 .ioctl = snd_card_asihpi_capture_ioctl,
1287 .hw_params = snd_card_asihpi_pcm_hw_params,
1288 .hw_free = snd_card_asihpi_hw_free,
1289 .prepare = snd_card_asihpi_capture_prepare,
1290 .trigger = snd_card_asihpi_trigger,
1291 .pointer = snd_card_asihpi_capture_pointer,
1292 };
1293
1294 static struct snd_pcm_ops snd_card_asihpi_capture_ops = {
1295 .open = snd_card_asihpi_capture_open,
1296 .close = snd_card_asihpi_capture_close,
1297 .ioctl = snd_card_asihpi_capture_ioctl,
1298 .hw_params = snd_card_asihpi_pcm_hw_params,
1299 .hw_free = snd_card_asihpi_hw_free,
1300 .prepare = snd_card_asihpi_capture_prepare,
1301 .trigger = snd_card_asihpi_trigger,
1302 .pointer = snd_card_asihpi_capture_pointer,
1303 .copy = snd_card_asihpi_capture_copy
1304 };
1305
1306 static int __devinit snd_card_asihpi_pcm_new(struct snd_card_asihpi *asihpi,
1307 int device, int substreams)
1308 {
1309 struct snd_pcm *pcm;
1310 int err;
1311
1312 err = snd_pcm_new(asihpi->card, "Asihpi PCM", device,
1313 asihpi->num_outstreams, asihpi->num_instreams,
1314 &pcm);
1315 if (err < 0)
1316 return err;
1317 /* pointer to ops struct is stored, dont change ops afterwards! */
1318 if (asihpi->support_mmap) {
1319 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1320 &snd_card_asihpi_playback_mmap_ops);
1321 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1322 &snd_card_asihpi_capture_mmap_ops);
1323 } else {
1324 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1325 &snd_card_asihpi_playback_ops);
1326 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1327 &snd_card_asihpi_capture_ops);
1328 }
1329
1330 pcm->private_data = asihpi;
1331 pcm->info_flags = 0;
1332 strcpy(pcm->name, "Asihpi PCM");
1333
1334 /*? do we want to emulate MMAP for non-BBM cards?
1335 Jack doesn't work with ALSAs MMAP emulation - WHY NOT? */
1336 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1337 snd_dma_pci_data(asihpi->pci),
1338 64*1024, BUFFER_BYTES_MAX);
1339
1340 return 0;
1341 }
1342
1343 /***************************** MIXER CONTROLS ****************/
1344 struct hpi_control {
1345 u32 h_control;
1346 u16 control_type;
1347 u16 src_node_type;
1348 u16 src_node_index;
1349 u16 dst_node_type;
1350 u16 dst_node_index;
1351 u16 band;
1352 char name[44]; /* copied to snd_ctl_elem_id.name[44]; */
1353 };
1354
1355 static const char * const asihpi_tuner_band_names[] = {
1356 "invalid",
1357 "AM",
1358 "FM mono",
1359 "TV NTSC-M",
1360 "FM stereo",
1361 "AUX",
1362 "TV PAL BG",
1363 "TV PAL I",
1364 "TV PAL DK",
1365 "TV SECAM",
1366 };
1367
1368 compile_time_assert(
1369 (ARRAY_SIZE(asihpi_tuner_band_names) ==
1370 (HPI_TUNER_BAND_LAST+1)),
1371 assert_tuner_band_names_size);
1372
1373 static const char * const asihpi_src_names[] = {
1374 "no source",
1375 "PCM",
1376 "Line",
1377 "Digital",
1378 "Tuner",
1379 "RF",
1380 "Clock",
1381 "Bitstream",
1382 "Microphone",
1383 "Cobranet",
1384 "Analog",
1385 "Adapter",
1386 };
1387
1388 compile_time_assert(
1389 (ARRAY_SIZE(asihpi_src_names) ==
1390 (HPI_SOURCENODE_LAST_INDEX-HPI_SOURCENODE_NONE+1)),
1391 assert_src_names_size);
1392
1393 static const char * const asihpi_dst_names[] = {
1394 "no destination",
1395 "PCM",
1396 "Line",
1397 "Digital",
1398 "RF",
1399 "Speaker",
1400 "Cobranet Out",
1401 "Analog"
1402 };
1403
1404 compile_time_assert(
1405 (ARRAY_SIZE(asihpi_dst_names) ==
1406 (HPI_DESTNODE_LAST_INDEX-HPI_DESTNODE_NONE+1)),
1407 assert_dst_names_size);
1408
1409 static inline int ctl_add(struct snd_card *card, struct snd_kcontrol_new *ctl,
1410 struct snd_card_asihpi *asihpi)
1411 {
1412 int err;
1413
1414 err = snd_ctl_add(card, snd_ctl_new1(ctl, asihpi));
1415 if (err < 0)
1416 return err;
1417 else if (mixer_dump)
1418 snd_printk(KERN_INFO "added %s(%d)\n", ctl->name, ctl->index);
1419
1420 return 0;
1421 }
1422
1423 /* Convert HPI control name and location into ALSA control name */
1424 static void asihpi_ctl_init(struct snd_kcontrol_new *snd_control,
1425 struct hpi_control *hpi_ctl,
1426 char *name)
1427 {
1428 char *dir = "";
1429 memset(snd_control, 0, sizeof(*snd_control));
1430 snd_control->name = hpi_ctl->name;
1431 snd_control->private_value = hpi_ctl->h_control;
1432 snd_control->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1433 snd_control->index = 0;
1434
1435 if (hpi_ctl->dst_node_type + HPI_DESTNODE_NONE == HPI_DESTNODE_ISTREAM)
1436 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 */
1440 else if (hpi_ctl->src_node_type &&
1441 (hpi_ctl->src_node_type + HPI_SOURCENODE_NONE != HPI_SOURCENODE_OSTREAM) &&
1442 (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
1447 if (hpi_ctl->src_node_type && hpi_ctl->dst_node_type)
1448 sprintf(hpi_ctl->name, "%s%d %s%d %s%s",
1449 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,
1453 dir, name);
1454 else if (hpi_ctl->dst_node_type) {
1455 sprintf(hpi_ctl->name, "%s %d %s%s",
1456 asihpi_dst_names[hpi_ctl->dst_node_type],
1457 hpi_ctl->dst_node_index,
1458 dir, name);
1459 } else {
1460 sprintf(hpi_ctl->name, "%s %d %s%s",
1461 asihpi_src_names[hpi_ctl->src_node_type],
1462 hpi_ctl->src_node_index,
1463 dir, name);
1464 }
1465 /* printk(KERN_INFO "Adding %s %d to %d ", hpi_ctl->name,
1466 hpi_ctl->wSrcNodeType, hpi_ctl->wDstNodeType); */
1467 }
1468
1469 /*------------------------------------------------------------
1470 Volume controls
1471 ------------------------------------------------------------*/
1472 #define VOL_STEP_mB 1
1473 static 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
1483 err = hpi_volume_query_range(h_control,
1484 &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
1499 static 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
1505 hpi_handle_error(hpi_volume_get_gain(h_control, an_gain_mB));
1506 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
1512 static 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;
1527 hpi_handle_error(hpi_volume_set_gain(h_control, an_gain_mB));
1528 return change;
1529 }
1530
1531 static const DECLARE_TLV_DB_SCALE(db_scale_100, -10000, VOL_STEP_mB, 0);
1532
1533 static 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
1539 asihpi_ctl_init(&snd_control, hpi_ctl, "Volume");
1540 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 ------------------------------------------------------------*/
1553 static 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 =
1563 hpi_level_query_range(h_control, &min_gain_mB,
1564 &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
1579 static 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
1585 hpi_handle_error(hpi_level_get_gain(h_control, an_gain_mB));
1586 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
1594 static 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;
1609 hpi_handle_error(hpi_level_set_gain(h_control, an_gain_mB));
1610 return change;
1611 }
1612
1613 static const DECLARE_TLV_DB_SCALE(db_scale_level, -1000, 100, 0);
1614
1615 static 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 */
1622 asihpi_ctl_init(&snd_control, hpi_ctl, "Level");
1623 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 */
1638 static const char * const asihpi_aesebu_format_names[] = {
1639 "N/A", "S/PDIF", "AES/EBU" };
1640
1641 static 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
1658 static int snd_asihpi_aesebu_format_get(struct snd_kcontrol *kcontrol,
1659 struct snd_ctl_elem_value *ucontrol,
1660 u16 (*func)(u32, u16 *))
1661 {
1662 u32 h_control = kcontrol->private_value;
1663 u16 source, err;
1664
1665 err = func(h_control, &source);
1666
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
1680 static int snd_asihpi_aesebu_format_put(struct snd_kcontrol *kcontrol,
1681 struct snd_ctl_elem_value *ucontrol,
1682 u16 (*func)(u32, u16))
1683 {
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
1694 if (func(h_control, source) != 0)
1695 return -EINVAL;
1696
1697 return 1;
1698 }
1699
1700 static 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,
1703 hpi_aesebu_receiver_get_format);
1704 }
1705
1706 static 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,
1709 hpi_aesebu_receiver_set_format);
1710 }
1711
1712 static 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
1725 static 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
1731 hpi_handle_error(hpi_aesebu_receiver_get_error_status(
1732 h_control, &status));
1733 ucontrol->value.integer.value[0] = status;
1734 return 0;
1735 }
1736
1737 static 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
1743 asihpi_ctl_init(&snd_control, hpi_ctl, "Format");
1744 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
1753 asihpi_ctl_init(&snd_control, hpi_ctl, "Status");
1754 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
1762 static 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,
1765 hpi_aesebu_transmitter_get_format);
1766 }
1767
1768 static 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,
1771 hpi_aesebu_transmitter_set_format);
1772 }
1773
1774
1775 static 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
1781 asihpi_ctl_init(&snd_control, hpi_ctl, "Format");
1782 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
1796 static 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++) {
1805 err = hpi_tuner_query_gain(h_control,
1806 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
1819 static 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
1828 hpi_handle_error(hpi_tuner_get_gain(h_control, &gain));
1829 ucontrol->value.integer.value[0] = gain / HPI_UNITS_PER_dB;
1830
1831 return 0;
1832 }
1833
1834 static 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;
1844 hpi_handle_error(hpi_tuner_set_gain(h_control, gain));
1845
1846 return 1;
1847 }
1848
1849 /* Band */
1850
1851 static 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++) {
1858 err = hpi_tuner_query_band(
1859 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
1870 static 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
1900 static 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
1914 hpi_handle_error(hpi_tuner_get_band(h_control, &band));
1915
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
1926 static 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]];
1941 hpi_handle_error(hpi_tuner_set_band(h_control, band));
1942
1943 return 1;
1944 }
1945
1946 /* Freq */
1947
1948 static 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++) {
1966 err = hpi_tuner_query_frequency(h_control,
1967 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
1993 static 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
1999 hpi_handle_error(hpi_tuner_get_frequency(h_control, &freq));
2000 ucontrol->value.integer.value[0] = freq;
2001
2002 return 0;
2003 }
2004
2005 static 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];
2012 hpi_handle_error(hpi_tuner_set_frequency(h_control, freq));
2013
2014 return 1;
2015 }
2016
2017 /* Tuner control group initializer */
2018 static 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
2027 if (!hpi_tuner_get_gain(hpi_ctl->h_control, NULL)) {
2028 asihpi_ctl_init(&snd_control, hpi_ctl, "Gain");
2029 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
2037 asihpi_ctl_init(&snd_control, hpi_ctl, "Band");
2038 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
2045 asihpi_ctl_init(&snd_control, hpi_ctl, "Freq");
2046 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 ------------------------------------------------------------*/
2056 static 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 */
2067 static 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
2089 static 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
2096 err = hpi_meter_get_peak(h_control, an_gain_mB);
2097
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
2115 static 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
2121 asihpi_ctl_init(&snd_control, hpi_ctl, "Meter");
2122 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 ------------------------------------------------------------*/
2135 static 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++) {
2141 err = hpi_multiplexer_query_source(h_control, s,
2142 &hpi_ctl.
2143 src_node_type,
2144 &hpi_ctl.
2145 src_node_index);
2146 if (err)
2147 break;
2148 }
2149 return s;
2150 }
2151
2152 static 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 =
2169 hpi_multiplexer_query_source(h_control,
2170 uinfo->value.enumerated.item,
2171 &src_node_type, &src_node_index);
2172
2173 sprintf(uinfo->value.enumerated.name, "%s %d",
2174 asihpi_src_names[src_node_type - HPI_SOURCENODE_NONE],
2175 src_node_index);
2176 return 0;
2177 }
2178
2179 static 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
2187 hpi_handle_error(hpi_multiplexer_get_source(h_control,
2188 &source_type, &source_index));
2189 /* Should cache this search result! */
2190 for (s = 0; s < 256; s++) {
2191 if (hpi_multiplexer_query_source(h_control, s,
2192 &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
2202 "Control %x failed to match mux source %hu %hu\n",
2203 h_control, source_type, source_index);
2204 ucontrol->value.enumerated.item[0] = 0;
2205 return 0;
2206 }
2207
2208 static 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
2218 e = hpi_multiplexer_query_source(h_control,
2219 ucontrol->value.enumerated.item[0],
2220 &source_type, &source_index);
2221 if (!e)
2222 hpi_handle_error(
2223 hpi_multiplexer_set_source(h_control,
2224 source_type, source_index));
2225 return change;
2226 }
2227
2228
2229 static 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
2235 asihpi_ctl_init(&snd_control, hpi_ctl, "Route");
2236 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 ------------------------------------------------------------*/
2248 static int snd_asihpi_cmode_info(struct snd_kcontrol *kcontrol,
2249 struct snd_ctl_elem_info *uinfo)
2250 {
2251 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"
2256 };
2257
2258 u32 h_control = kcontrol->private_value;
2259 u16 mode;
2260 int i;
2261 u16 mode_map[6];
2262 int valid_modes = 0;
2263
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++)
2268 if (!hpi_channel_mode_query_mode(
2269 h_control, i, &mode)) {
2270 mode_map[valid_modes] = mode;
2271 valid_modes++;
2272 }
2273
2274 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2275 uinfo->count = 1;
2276 uinfo->value.enumerated.items = valid_modes;
2277
2278 if (uinfo->value.enumerated.item >= valid_modes)
2279 uinfo->value.enumerated.item = valid_modes - 1;
2280
2281 strcpy(uinfo->value.enumerated.name,
2282 mode_names[mode_map[uinfo->value.enumerated.item]]);
2283
2284 return 0;
2285 }
2286
2287 static 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
2293 if (hpi_channel_mode_get(h_control, &mode))
2294 mode = 1;
2295
2296 ucontrol->value.enumerated.item[0] = mode - 1;
2297
2298 return 0;
2299 }
2300
2301 static 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
2309 hpi_handle_error(hpi_channel_mode_set(h_control,
2310 ucontrol->value.enumerated.item[0] + 1));
2311 return change;
2312 }
2313
2314
2315 static 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
2321 asihpi_ctl_init(&snd_control, hpi_ctl, "Mode");
2322 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 ------------------------------------------------------------*/
2333 static 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"};
2339
2340 static 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
2359 static 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;
2370 if (hpi_sample_clock_get_source(h_control, &source))
2371 source = 0;
2372
2373 if (source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
2374 if (hpi_sample_clock_get_source_index(h_control, &srcindex))
2375 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
2387 static 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
2401 hpi_handle_error(hpi_sample_clock_set_source(
2402 h_control, clkcache->s[item].source));
2403
2404 if (clkcache->s[item].source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
2405 hpi_handle_error(hpi_sample_clock_set_source_index(
2406 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 */
2414 static 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
2426 static 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
2433 e = hpi_sample_clock_get_local_rate(h_control, &rate);
2434 if (!e)
2435 ucontrol->value.integer.value[0] = rate;
2436 else
2437 ucontrol->value.integer.value[0] = 0;
2438 return 0;
2439 }
2440
2441 static 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;
2451 hpi_handle_error(hpi_sample_clock_set_local_rate(h_control,
2452 ucontrol->value.integer.value[0]));
2453 return change;
2454 }
2455
2456 static 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
2468 static 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
2475 e = hpi_sample_clock_get_sample_rate(h_control, &rate);
2476 if (!e)
2477 ucontrol->value.integer.value[0] = rate;
2478 else
2479 ucontrol->value.integer.value[0] = 0;
2480 return 0;
2481 }
2482
2483 static 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++) {
2500 if (hpi_sample_clock_query_source(hSC,
2501 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++) {
2514 if (hpi_sample_clock_query_source_index(hSC,
2515 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
2527 asihpi_ctl_init(&snd_control, hpi_ctl, "Source");
2528 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) {
2537 asihpi_ctl_init(&snd_control, hpi_ctl, "Localrate");
2538 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
2548 asihpi_ctl_init(&snd_control, hpi_ctl, "Rate");
2549 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
2560 static 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;
2570 strcpy(card->mixername, "Asihpi Mixer");
2571
2572 err =
2573 hpi_mixer_open(asihpi->adapter_index,
2574 &asihpi->h_mixer);
2575 hpi_handle_error(err);
2576 if (err)
2577 return -err;
2578
2579 memset(&prev_ctl, 0, sizeof(prev_ctl));
2580 prev_ctl.control_type = -1;
2581
2582 for (idx = 0; idx < 2000; idx++) {
2583 err = hpi_mixer_get_control_by_index(
2584 asihpi->h_mixer,
2585 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
2596 "Disabled HPI Control(%d)\n",
2597 idx);
2598 continue;
2599 } else
2600 break;
2601
2602 }
2603
2604 hpi_ctl.src_node_type -= HPI_SOURCENODE_NONE;
2605 hpi_ctl.dst_node_type -= HPI_DESTNODE_NONE;
2606
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
2661 "Untranslated HPI Control"
2662 "(%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
2686 static void
2687 snd_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
2711 err = hpi_mixer_get_control(asihpi->h_mixer,
2712 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
2713 HPI_CONTROL_SAMPLECLOCK, &h_control);
2714
2715 if (!err) {
2716 err = hpi_sample_clock_get_sample_rate(
2717 h_control, &rate);
2718 err += hpi_sample_clock_get_source(h_control, &source);
2719
2720 if (!err)
2721 snd_iprintf(buffer, "sample_clock=%d_hz, source %s\n",
2722 rate, sampleclock_sources[source]);
2723 }
2724
2725 }
2726
2727
2728 static 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
2740 static 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
2749 static 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
2757 static 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 */
2770 static 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 ------------------------------------------------------------*/
2795 static 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
2840 "**** WARNING **** Adapter index %d->ALSA index %d\n",
2841 hpi_card->index, card->number);
2842 }
2843
2844 snd_card_set_dev(card, &pci_dev->dev);
2845
2846 asihpi = (struct snd_card_asihpi *) card->private_data;
2847 asihpi->card = card;
2848 asihpi->pci = pci_dev;
2849 asihpi->adapter_index = hpi_card->index;
2850 hpi_handle_error(hpi_adapter_get_info(
2851 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"
2860 "Hw Version %c%d DSP code version %03d\n",
2861 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
2872 err = hpi_adapter_get_property(asihpi->adapter_index,
2873 HPI_ADAPTER_PROPERTY_CAPS1,
2874 NULL, &asihpi->support_grouping);
2875 if (err)
2876 asihpi->support_grouping = 0;
2877
2878 err = hpi_adapter_get_property(asihpi->adapter_index,
2879 HPI_ADAPTER_PROPERTY_CAPS2,
2880 &asihpi->support_mrx, NULL);
2881 if (err)
2882 asihpi->support_mrx = 0;
2883
2884 err = hpi_adapter_get_property(asihpi->adapter_index,
2885 HPI_ADAPTER_PROPERTY_INTERVAL,
2886 NULL, &asihpi->update_interval_frames);
2887 if (err)
2888 asihpi->update_interval_frames = 512;
2889
2890 hpi_handle_error(hpi_instream_open(asihpi->adapter_index,
2891 0, &h_stream));
2892
2893 err = hpi_instream_host_buffer_free(h_stream);
2894 asihpi->support_mmap = (!err);
2895
2896 hpi_handle_error(hpi_instream_close(h_stream));
2897
2898 err = hpi_adapter_get_property(asihpi->adapter_index,
2899 HPI_ADAPTER_PROPERTY_CURCHANNELS,
2900 &asihpi->in_max_chans, &asihpi->out_max_chans);
2901 if (err) {
2902 asihpi->in_max_chans = 2;
2903 asihpi->out_max_chans = 2;
2904 }
2905
2906 snd_printk(KERN_INFO "supports mmap:%d grouping:%d mrx:%d\n",
2907 asihpi->support_mmap,
2908 asihpi->support_grouping,
2909 asihpi->support_mrx
2910 );
2911
2912
2913 err = snd_card_asihpi_pcm_new(asihpi, 0, pcm_substreams);
2914 if (err < 0) {
2915 snd_printk(KERN_ERR "pcm_new failed\n");
2916 goto __nodev;
2917 }
2918 err = snd_card_asihpi_mixer_new(asihpi);
2919 if (err < 0) {
2920 snd_printk(KERN_ERR "mixer_new failed\n");
2921 goto __nodev;
2922 }
2923
2924 err = hpi_mixer_get_control(asihpi->h_mixer,
2925 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
2926 HPI_CONTROL_SAMPLECLOCK, &h_control);
2927
2928 if (!err)
2929 err = hpi_sample_clock_set_local_rate(
2930 h_control, adapter_fs);
2931
2932 snd_asihpi_proc_init(asihpi);
2933
2934 /* always create, can be enabled or disabled dynamically
2935 by enable_hwdep module param*/
2936 snd_asihpi_hpi_new(asihpi, 0, NULL);
2937
2938 if (asihpi->support_mmap)
2939 strcpy(card->driver, "ASIHPI-MMAP");
2940 else
2941 strcpy(card->driver, "ASIHPI");
2942
2943 sprintf(card->shortname, "AudioScience ASI%4X", asihpi->type);
2944 sprintf(card->longname, "%s %i",
2945 card->shortname, asihpi->adapter_index);
2946 err = snd_card_register(card);
2947 if (!err) {
2948 hpi_card->snd_card_asihpi = card;
2949 dev++;
2950 return 0;
2951 }
2952 __nodev:
2953 snd_card_free(card);
2954 snd_printk(KERN_ERR "snd_asihpi_probe error %d\n", err);
2955 return err;
2956
2957 }
2958
2959 static void __devexit snd_asihpi_remove(struct pci_dev *pci_dev)
2960 {
2961 struct hpi_adapter *hpi_card = pci_get_drvdata(pci_dev);
2962
2963 snd_card_free(hpi_card->snd_card_asihpi);
2964 hpi_card->snd_card_asihpi = NULL;
2965 asihpi_adapter_remove(pci_dev);
2966 }
2967
2968 static DEFINE_PCI_DEVICE_TABLE(asihpi_pci_tbl) = {
2969 {HPI_PCI_VENDOR_ID_TI, HPI_PCI_DEV_ID_DSP6205,
2970 HPI_PCI_VENDOR_ID_AUDIOSCIENCE, PCI_ANY_ID, 0, 0,
2971 (kernel_ulong_t)HPI_6205},
2972 {HPI_PCI_VENDOR_ID_TI, HPI_PCI_DEV_ID_PCI2040,
2973 HPI_PCI_VENDOR_ID_AUDIOSCIENCE, PCI_ANY_ID, 0, 0,
2974 (kernel_ulong_t)HPI_6000},
2975 {0,}
2976 };
2977 MODULE_DEVICE_TABLE(pci, asihpi_pci_tbl);
2978
2979 static struct pci_driver driver = {
2980 .name = "asihpi",
2981 .id_table = asihpi_pci_tbl,
2982 .probe = snd_asihpi_probe,
2983 .remove = __devexit_p(snd_asihpi_remove),
2984 #ifdef CONFIG_PM
2985 /* .suspend = snd_asihpi_suspend,
2986 .resume = snd_asihpi_resume, */
2987 #endif
2988 };
2989
2990 static int __init snd_asihpi_init(void)
2991 {
2992 asihpi_init();
2993 return pci_register_driver(&driver);
2994 }
2995
2996 static void __exit snd_asihpi_exit(void)
2997 {
2998
2999 pci_unregister_driver(&driver);
3000 asihpi_exit();
3001 }
3002
3003 module_init(snd_asihpi_init)
3004 module_exit(snd_asihpi_exit)
3005