2 * Driver for Digigram VX soundcards
6 * Copyright (c) 2002,2003 by Takashi Iwai <tiwai@suse.de>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * for playback, we send series of "chunks", which size is equal with the
25 * IBL size, typically 126 samples. at each end of chunk, the end-of-buffer
26 * interrupt is notified, and the interrupt handler will feed the next chunk.
28 * the current position is calculated from the sample count RMH.
29 * pipe->transferred is the counter of data which has been already transferred.
30 * if this counter reaches to the period size, snd_pcm_period_elapsed() will
33 * for capture, the situation is much easier.
34 * to get a low latency response, we'll check the capture streams at each
35 * interrupt (capture stream has no EOB notification). if the pending
36 * data is accumulated to the period size, snd_pcm_period_elapsed() is
37 * called and the pointer is updated.
39 * the current point of read buffer is kept in pipe->hw_ptr. note that
44 * - linked trigger for full-duplex mode.
45 * - scheduled action on the stream.
48 #include <sound/driver.h>
49 #include <linux/slab.h>
50 #include <linux/vmalloc.h>
51 #include <sound/core.h>
52 #include <sound/asoundef.h>
53 #include <sound/pcm.h>
54 #include <sound/vx_core.h>
57 #define chip_t vx_core_t
61 * we use a vmalloc'ed (sg-)buffer
64 /* get the physical page pointer on the given offset */
65 static struct page *snd_pcm_get_vmalloc_page(snd_pcm_substream_t *subs, unsigned long offset)
67 void *pageptr = subs->runtime->dma_area + offset;
68 return vmalloc_to_page(pageptr);
72 * allocate a buffer via vmalloc_32().
73 * called from hw_params
74 * NOTE: this may be called not only once per pcm open!
76 static int snd_pcm_alloc_vmalloc_buffer(snd_pcm_substream_t *subs, size_t size)
78 snd_pcm_runtime_t *runtime = subs->runtime;
79 if (runtime->dma_area) {
80 /* already allocated */
81 if (runtime->dma_bytes >= size)
82 return 0; /* already enough large */
83 vfree_nocheck(runtime->dma_area); /* bypass the memory wrapper */
85 runtime->dma_area = vmalloc_32(size);
86 if (! runtime->dma_area)
88 memset(runtime->dma_area, 0, size);
89 runtime->dma_bytes = size;
90 return 1; /* changed */
95 * called from hw_free callback
96 * NOTE: this may be called not only once per pcm open!
98 static int snd_pcm_free_vmalloc_buffer(snd_pcm_substream_t *subs)
100 snd_pcm_runtime_t *runtime = subs->runtime;
101 if (runtime->dma_area) {
102 vfree_nocheck(runtime->dma_area); /* bypass the memory wrapper */
103 runtime->dma_area = NULL;
110 * read three pending pcm bytes via inb()
112 static void vx_pcm_read_per_bytes(vx_core_t *chip, snd_pcm_runtime_t *runtime, vx_pipe_t *pipe)
114 int offset = pipe->hw_ptr;
115 unsigned char *buf = (unsigned char *)(runtime->dma_area + offset);
116 *buf++ = vx_inb(chip, RXH);
117 if (++offset >= pipe->buffer_bytes) {
119 buf = (unsigned char *)runtime->dma_area;
121 *buf++ = vx_inb(chip, RXM);
122 if (++offset >= pipe->buffer_bytes) {
124 buf = (unsigned char *)runtime->dma_area;
126 *buf++ = vx_inb(chip, RXL);
127 if (++offset >= pipe->buffer_bytes) {
129 buf = (unsigned char *)runtime->dma_area;
131 pipe->hw_ptr = offset;
135 * vx_set_pcx_time - convert from the PC time to the RMH status time.
136 * @pc_time: the pointer for the PC-time to set
137 * @dsp_time: the pointer for RMH status time array
139 static void vx_set_pcx_time(vx_core_t *chip, pcx_time_t *pc_time, unsigned int *dsp_time)
141 dsp_time[0] = (unsigned int)((*pc_time) >> 24) & PCX_TIME_HI_MASK;
142 dsp_time[1] = (unsigned int)(*pc_time) & MASK_DSP_WORD;
146 * vx_set_differed_time - set the differed time if specified
147 * @rmh: the rmh record to modify
148 * @pipe: the pipe to be checked
150 * if the pipe is programmed with the differed time, set the DSP time
151 * on the rmh and changes its command length.
153 * returns the increase of the command length.
155 static int vx_set_differed_time(vx_core_t *chip, struct vx_rmh *rmh, vx_pipe_t *pipe)
157 /* Update The length added to the RMH command by the timestamp */
158 if (! (pipe->differed_type & DC_DIFFERED_DELAY))
162 rmh->Cmd[0] |= DSP_DIFFERED_COMMAND_MASK;
164 /* Time stamp is the 1st following parameter */
165 vx_set_pcx_time(chip, &pipe->pcx_time, &rmh->Cmd[1]);
167 /* Add the flags to a notified differed command */
168 if (pipe->differed_type & DC_NOTIFY_DELAY)
169 rmh->Cmd[1] |= NOTIFY_MASK_TIME_HIGH ;
171 /* Add the flags to a multiple differed command */
172 if (pipe->differed_type & DC_MULTIPLE_DELAY)
173 rmh->Cmd[1] |= MULTIPLE_MASK_TIME_HIGH;
175 /* Add the flags to a stream-time differed command */
176 if (pipe->differed_type & DC_STREAM_TIME_DELAY)
177 rmh->Cmd[1] |= STREAM_MASK_TIME_HIGH;
184 * vx_set_stream_format - send the stream format command
185 * @pipe: the affected pipe
186 * @data: format bitmask
188 static int vx_set_stream_format(vx_core_t *chip, vx_pipe_t *pipe, unsigned int data)
192 vx_init_rmh(&rmh, pipe->is_capture ?
193 CMD_FORMAT_STREAM_IN : CMD_FORMAT_STREAM_OUT);
194 rmh.Cmd[0] |= pipe->number << FIELD_SIZE;
196 /* Command might be longer since we may have to add a timestamp */
197 vx_set_differed_time(chip, &rmh, pipe);
199 rmh.Cmd[rmh.LgCmd] = (data & 0xFFFFFF00) >> 8;
200 rmh.Cmd[rmh.LgCmd + 1] = (data & 0xFF) << 16 /*| (datal & 0xFFFF00) >> 8*/;
203 return vx_send_msg(chip, &rmh);
208 * vx_set_format - set the format of a pipe
209 * @pipe: the affected pipe
210 * @runtime: pcm runtime instance to be referred
212 * returns 0 if successful, or a negative error code.
214 static int vx_set_format(vx_core_t *chip, vx_pipe_t *pipe,
215 snd_pcm_runtime_t *runtime)
217 unsigned int header = HEADER_FMT_BASE;
219 if (runtime->channels == 1)
220 header |= HEADER_FMT_MONO;
221 if (snd_pcm_format_little_endian(runtime->format))
222 header |= HEADER_FMT_INTEL;
223 if (runtime->rate < 32000 && runtime->rate > 11025)
224 header |= HEADER_FMT_UPTO32;
225 else if (runtime->rate <= 11025)
226 header |= HEADER_FMT_UPTO11;
228 switch (snd_pcm_format_physical_width(runtime->format)) {
230 case 16: header |= HEADER_FMT_16BITS; break;
231 case 24: header |= HEADER_FMT_24BITS; break;
237 return vx_set_stream_format(chip, pipe, header);
241 * set / query the IBL size
243 static int vx_set_ibl(vx_core_t *chip, struct vx_ibl_info *info)
248 vx_init_rmh(&rmh, CMD_IBL);
249 rmh.Cmd[0] |= info->size & 0x03ffff;
250 err = vx_send_msg(chip, &rmh);
253 info->size = rmh.Stat[0];
254 info->max_size = rmh.Stat[1];
255 info->min_size = rmh.Stat[2];
256 info->granularity = rmh.Stat[3];
257 snd_printdd(KERN_DEBUG "vx_set_ibl: size = %d, max = %d, min = %d, gran = %d\n",
258 info->size, info->max_size, info->min_size, info->granularity);
264 * vx_get_pipe_state - get the state of a pipe
265 * @pipe: the pipe to be checked
266 * @state: the pointer for the returned state
268 * checks the state of a given pipe, and stores the state (1 = running,
269 * 0 = paused) on the given pointer.
271 * called from trigger callback only
273 static int vx_get_pipe_state(vx_core_t *chip, vx_pipe_t *pipe, int *state)
278 vx_init_rmh(&rmh, CMD_PIPE_STATE);
279 vx_set_pipe_cmd_params(&rmh, pipe->is_capture, pipe->number, 0);
280 err = vx_send_msg_nolock(chip, &rmh); /* no lock needed for trigger */
282 *state = (rmh.Stat[0] & (1 << pipe->number)) ? 1 : 0;
288 * vx_query_hbuffer_size - query available h-buffer size in bytes
289 * @pipe: the pipe to be checked
291 * return the available size on h-buffer in bytes,
292 * or a negative error code.
294 * NOTE: calling this function always switches to the stream mode.
295 * you'll need to disconnect the host to get back to the
298 static int vx_query_hbuffer_size(vx_core_t *chip, vx_pipe_t *pipe)
303 vx_init_rmh(&rmh, CMD_SIZE_HBUFFER);
304 vx_set_pipe_cmd_params(&rmh, pipe->is_capture, pipe->number, 0);
305 if (pipe->is_capture)
306 rmh.Cmd[0] |= 0x00000001;
307 result = vx_send_msg(chip, &rmh);
309 result = rmh.Stat[0] & 0xffff;
315 * vx_pipe_can_start - query whether a pipe is ready for start
316 * @pipe: the pipe to be checked
318 * return 1 if ready, 0 if not ready, and negative value on error.
320 * called from trigger callback only
322 static int vx_pipe_can_start(vx_core_t *chip, vx_pipe_t *pipe)
327 vx_init_rmh(&rmh, CMD_CAN_START_PIPE);
328 vx_set_pipe_cmd_params(&rmh, pipe->is_capture, pipe->number, 0);
331 err = vx_send_msg_nolock(chip, &rmh); /* no lock needed for trigger */
340 * vx_conf_pipe - tell the pipe to stand by and wait for IRQA.
341 * @pipe: the pipe to be configured
343 static int vx_conf_pipe(vx_core_t *chip, vx_pipe_t *pipe)
347 vx_init_rmh(&rmh, CMD_CONF_PIPE);
348 if (pipe->is_capture)
349 rmh.Cmd[0] |= COMMAND_RECORD_MASK;
350 rmh.Cmd[1] = 1 << pipe->number;
351 return vx_send_msg_nolock(chip, &rmh); /* no lock needed for trigger */
355 * vx_send_irqa - trigger IRQA
357 static int vx_send_irqa(vx_core_t *chip)
361 vx_init_rmh(&rmh, CMD_SEND_IRQA);
362 return vx_send_msg_nolock(chip, &rmh); /* no lock needed for trigger */
366 #define MAX_WAIT_FOR_DSP 250
368 * vx boards do not support inter-card sync, besides
369 * only 126 samples require to be prepared before a pipe can start
371 #define CAN_START_DELAY 2 /* wait 2ms only before asking if the pipe is ready*/
372 #define WAIT_STATE_DELAY 2 /* wait 2ms after irqA was requested and check if the pipe state toggled*/
375 * vx_toggle_pipe - start / pause a pipe
376 * @pipe: the pipe to be triggered
377 * @state: start = 1, pause = 0
379 * called from trigger callback only
382 static int vx_toggle_pipe(vx_core_t *chip, vx_pipe_t *pipe, int state)
384 int err, i, cur_state, delay;
386 /* Check the pipe is not already in the requested state */
387 if (vx_get_pipe_state(chip, pipe, &cur_state) < 0)
389 if (state == cur_state)
392 /* If a start is requested, ask the DSP to get prepared
393 * and wait for a positive acknowledge (when there are
394 * enough sound buffer for this pipe)
397 int delay = CAN_START_DELAY;
398 for (i = 0 ; i < MAX_WAIT_FOR_DSP; i++) {
399 snd_vx_delay(chip, delay);
400 err = vx_pipe_can_start(chip, pipe);
403 /* Wait for a few, before asking again
404 * to avoid flooding the DSP with our requests
411 if ((err = vx_conf_pipe(chip, pipe)) < 0)
414 if ((err = vx_send_irqa(chip)) < 0)
417 /* If it completes successfully, wait for the pipes
418 * reaching the expected state before returning
419 * Check one pipe only (since they are synchronous)
421 delay = WAIT_STATE_DELAY;
422 for (i = 0; i < MAX_WAIT_FOR_DSP; i++) {
423 snd_vx_delay(chip, delay);
424 err = vx_get_pipe_state(chip, pipe, &cur_state);
425 if (err < 0 || cur_state == state)
431 return err < 0 ? -EIO : 0;
436 * vx_stop_pipe - stop a pipe
437 * @pipe: the pipe to be stopped
439 * called from trigger callback only
441 static int vx_stop_pipe(vx_core_t *chip, vx_pipe_t *pipe)
444 vx_init_rmh(&rmh, CMD_STOP_PIPE);
445 vx_set_pipe_cmd_params(&rmh, pipe->is_capture, pipe->number, 0);
446 return vx_send_msg_nolock(chip, &rmh); /* no lock needed for trigger */
451 * vx_alloc_pipe - allocate a pipe and initialize the pipe instance
452 * @capture: 0 = playback, 1 = capture operation
453 * @audioid: the audio id to be assigned
454 * @num_audio: number of audio channels
455 * @pipep: the returned pipe instance
457 * return 0 on success, or a negative error code.
459 static int vx_alloc_pipe(vx_core_t *chip, int capture,
460 int audioid, int num_audio,
469 vx_init_rmh(&rmh, CMD_RES_PIPE);
470 vx_set_pipe_cmd_params(&rmh, capture, audioid, num_audio);
472 if (underrun_skip_sound)
473 rmh.Cmd[0] |= BIT_SKIP_SOUND;
475 data_mode = (chip->uer_bits & IEC958_AES0_NONAUDIO) != 0;
476 if (! capture && data_mode)
477 rmh.Cmd[0] |= BIT_DATA_MODE;
478 err = vx_send_msg(chip, &rmh);
482 /* initialize the pipe record */
483 pipe = snd_magic_kcalloc(vx_pipe_t, 0, GFP_KERNEL);
485 /* release the pipe */
486 vx_init_rmh(&rmh, CMD_FREE_PIPE);
487 vx_set_pipe_cmd_params(&rmh, capture, audioid, 0);
488 vx_send_msg(chip, &rmh);
492 /* the pipe index should be identical with the audio index */
493 pipe->number = audioid;
494 pipe->is_capture = capture;
495 pipe->channels = num_audio;
496 pipe->differed_type = 0;
498 pipe->data_mode = data_mode;
506 * vx_free_pipe - release a pipe
507 * @pipe: pipe to be released
509 static int vx_free_pipe(vx_core_t *chip, vx_pipe_t *pipe)
513 vx_init_rmh(&rmh, CMD_FREE_PIPE);
514 vx_set_pipe_cmd_params(&rmh, pipe->is_capture, pipe->number, 0);
515 vx_send_msg(chip, &rmh);
517 snd_magic_kfree(pipe);
523 * vx_start_stream - start the stream
525 * called from trigger callback only
527 static int vx_start_stream(vx_core_t *chip, vx_pipe_t *pipe)
531 vx_init_rmh(&rmh, CMD_START_ONE_STREAM);
532 vx_set_stream_cmd_params(&rmh, pipe->is_capture, pipe->number);
533 vx_set_differed_time(chip, &rmh, pipe);
534 return vx_send_msg_nolock(chip, &rmh); /* no lock needed for trigger */
539 * vx_stop_stream - stop the stream
541 * called from trigger callback only
543 static int vx_stop_stream(vx_core_t *chip, vx_pipe_t *pipe)
547 vx_init_rmh(&rmh, CMD_STOP_STREAM);
548 vx_set_stream_cmd_params(&rmh, pipe->is_capture, pipe->number);
549 return vx_send_msg_nolock(chip, &rmh); /* no lock needed for trigger */
554 * playback hw information
557 static snd_pcm_hardware_t vx_pcm_playback_hw = {
558 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
559 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_MMAP_VALID),
560 .formats = /*SNDRV_PCM_FMTBIT_U8 |*/ SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_3LE,
561 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
566 .buffer_bytes_max = (128*1024),
567 .period_bytes_min = 126,
568 .period_bytes_max = (128*1024),
570 .periods_max = VX_MAX_PERIODS,
575 static void vx_pcm_delayed_start(unsigned long arg);
578 * vx_pcm_playback_open - open callback for playback
580 static int vx_pcm_playback_open(snd_pcm_substream_t *subs)
582 snd_pcm_runtime_t *runtime = subs->runtime;
583 vx_core_t *chip = snd_pcm_substream_chip(subs);
584 vx_pipe_t *pipe = NULL;
588 if (chip->chip_status & VX_STAT_IS_STALE)
591 audio = subs->pcm->device * 2;
592 snd_assert(audio < chip->audio_outs, return -EINVAL);
594 /* playback pipe may have been already allocated for monitoring */
595 pipe = chip->playback_pipes[audio];
597 /* not allocated yet */
598 err = vx_alloc_pipe(chip, 0, audio, 2, &pipe); /* stereo playback */
601 chip->playback_pipes[audio] = pipe;
603 /* open for playback */
606 pipe->substream = subs;
607 tasklet_init(&pipe->start_tq, vx_pcm_delayed_start, (unsigned long)subs);
608 chip->playback_pipes[audio] = pipe;
610 runtime->hw = vx_pcm_playback_hw;
611 runtime->hw.period_bytes_min = chip->ibl.size;
612 runtime->private_data = pipe;
614 /* align to 4 bytes (otherwise will be problematic when 24bit is used) */
615 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 4);
616 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 4);
622 * vx_pcm_playback_close - close callback for playback
624 static int vx_pcm_playback_close(snd_pcm_substream_t *subs)
626 vx_core_t *chip = snd_pcm_substream_chip(subs);
629 if (! subs->runtime->private_data)
632 pipe = snd_magic_cast(vx_pipe_t, subs->runtime->private_data, return -EINVAL);
634 if (--pipe->references == 0) {
635 chip->playback_pipes[pipe->number] = NULL;
636 vx_free_pipe(chip, pipe);
645 * vx_notify_end_of_buffer - send "end-of-buffer" notifier at the given pipe
646 * @pipe: the pipe to notify
648 * NB: call with a certain lock.
650 static int vx_notify_end_of_buffer(vx_core_t *chip, vx_pipe_t *pipe)
653 struct vx_rmh rmh; /* use a temporary rmh here */
655 /* Toggle Dsp Host Interface into Message mode */
656 vx_send_rih_nolock(chip, IRQ_PAUSE_START_CONNECT);
657 vx_init_rmh(&rmh, CMD_NOTIFY_END_OF_BUFFER);
658 vx_set_stream_cmd_params(&rmh, 0, pipe->number);
659 err = vx_send_msg_nolock(chip, &rmh);
662 /* Toggle Dsp Host Interface back to sound transfer mode */
663 vx_send_rih_nolock(chip, IRQ_PAUSE_START_CONNECT);
668 * vx_pcm_playback_transfer_chunk - transfer a single chunk
670 * @pipe: the pipe to transfer
671 * @size: chunk size in bytes
673 * transfer a single buffer chunk. EOB notificaton is added after that.
674 * called from the interrupt handler, too.
678 static int vx_pcm_playback_transfer_chunk(vx_core_t *chip, snd_pcm_runtime_t *runtime, vx_pipe_t *pipe, int size)
682 space = vx_query_hbuffer_size(chip, pipe);
684 /* disconnect the host, SIZE_HBUF command always switches to the stream mode */
685 vx_send_rih(chip, IRQ_CONNECT_STREAM_NEXT);
686 snd_printd("error hbuffer\n");
690 vx_send_rih(chip, IRQ_CONNECT_STREAM_NEXT);
691 snd_printd("no enough hbuffer space %d\n", space);
692 return -EIO; /* XRUN */
695 /* we don't need irqsave here, because this function
696 * is called from either trigger callback or irq handler
698 spin_lock(&chip->lock);
699 vx_pseudo_dma_write(chip, runtime, pipe, size);
700 err = vx_notify_end_of_buffer(chip, pipe);
701 /* disconnect the host, SIZE_HBUF command always switches to the stream mode */
702 vx_send_rih_nolock(chip, IRQ_CONNECT_STREAM_NEXT);
703 spin_unlock(&chip->lock);
708 * update the position of the given pipe.
709 * pipe->position is updated and wrapped within the buffer size.
710 * pipe->transferred is updated, too, but the size is not wrapped,
711 * so that the caller can check the total transferred size later
712 * (to call snd_pcm_period_elapsed).
714 static int vx_update_pipe_position(vx_core_t *chip, snd_pcm_runtime_t *runtime, vx_pipe_t *pipe)
720 vx_init_rmh(&rmh, CMD_STREAM_SAMPLE_COUNT);
721 vx_set_pipe_cmd_params(&rmh, pipe->is_capture, pipe->number, 0);
722 err = vx_send_msg(chip, &rmh);
726 count = ((u64)(rmh.Stat[0] & 0xfffff) << 24) | (u64)rmh.Stat[1];
727 update = (int)(count - pipe->cur_count);
728 pipe->cur_count = count;
729 pipe->position += update;
730 if (pipe->position >= (int)runtime->buffer_size)
731 pipe->position %= runtime->buffer_size;
732 pipe->transferred += update;
737 * transfer the pending playback buffer data to DSP
738 * called from interrupt handler
740 static void vx_pcm_playback_transfer(vx_core_t *chip, snd_pcm_substream_t *subs, vx_pipe_t *pipe, int nchunks)
743 snd_pcm_runtime_t *runtime = subs->runtime;
745 if (! pipe->prepared || (chip->chip_status & VX_STAT_IS_STALE))
747 for (i = 0; i < nchunks; i++) {
748 if ((err = vx_pcm_playback_transfer_chunk(chip, runtime, pipe,
749 chip->ibl.size)) < 0)
755 * update the playback position and call snd_pcm_period_elapsed() if necessary
756 * called from interrupt handler
758 static void vx_pcm_playback_update(vx_core_t *chip, snd_pcm_substream_t *subs, vx_pipe_t *pipe)
761 snd_pcm_runtime_t *runtime = subs->runtime;
763 if (pipe->running && ! (chip->chip_status & VX_STAT_IS_STALE)) {
764 if ((err = vx_update_pipe_position(chip, runtime, pipe)) < 0)
766 if (pipe->transferred >= (int)runtime->period_size) {
767 pipe->transferred %= runtime->period_size;
768 snd_pcm_period_elapsed(subs);
774 * start the stream and pipe.
775 * this function is called from tasklet, which is invoked by the trigger
778 static void vx_pcm_delayed_start(unsigned long arg)
780 snd_pcm_substream_t *subs = (snd_pcm_substream_t *)arg;
781 vx_core_t *chip = snd_magic_cast(vx_core_t, subs->pcm->private_data, return);
782 vx_pipe_t *pipe = snd_magic_cast(vx_pipe_t, subs->runtime->private_data, return);
785 /* printk( KERN_DEBUG "DDDD tasklet delayed start jiffies = %ld\n", jiffies);*/
787 if ((err = vx_start_stream(chip, pipe)) < 0) {
788 snd_printk(KERN_ERR "vx: cannot start stream\n");
791 if ((err = vx_toggle_pipe(chip, pipe, 1)) < 0) {
792 snd_printk(KERN_ERR "vx: cannot start pipe\n");
795 /* printk( KERN_DEBUG "dddd tasklet delayed start jiffies = %ld \n", jiffies);*/
799 * vx_pcm_playback_trigger - trigger callback for playback
801 static int vx_pcm_trigger(snd_pcm_substream_t *subs, int cmd)
803 vx_core_t *chip = snd_pcm_substream_chip(subs);
804 vx_pipe_t *pipe = snd_magic_cast(vx_pipe_t, subs->runtime->private_data, return -EINVAL);
807 if (chip->chip_status & VX_STAT_IS_STALE)
811 case SNDRV_PCM_TRIGGER_START:
812 if (! pipe->is_capture)
813 vx_pcm_playback_transfer(chip, subs, pipe, 2);
815 * we trigger the pipe using tasklet, so that the interrupts are
816 * issued surely after the trigger is completed.
818 tasklet_hi_schedule(&pipe->start_tq);
822 case SNDRV_PCM_TRIGGER_STOP:
823 vx_toggle_pipe(chip, pipe, 0);
824 vx_stop_pipe(chip, pipe);
825 vx_stop_stream(chip, pipe);
829 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
830 if ((err = vx_toggle_pipe(chip, pipe, 0)) < 0)
833 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
834 if ((err = vx_toggle_pipe(chip, pipe, 1)) < 0)
844 * vx_pcm_playback_pointer - pointer callback for playback
846 static snd_pcm_uframes_t vx_pcm_playback_pointer(snd_pcm_substream_t *subs)
848 snd_pcm_runtime_t *runtime = subs->runtime;
849 vx_pipe_t *pipe = snd_magic_cast(vx_pipe_t, runtime->private_data, return -EINVAL);
850 return pipe->position;
854 * vx_pcm_hw_params - hw_params callback for playback and capture
856 static int vx_pcm_hw_params(snd_pcm_substream_t *subs,
857 snd_pcm_hw_params_t *hw_params)
859 return snd_pcm_alloc_vmalloc_buffer(subs, params_buffer_bytes(hw_params));
863 * vx_pcm_hw_free - hw_free callback for playback and capture
865 static int vx_pcm_hw_free(snd_pcm_substream_t *subs)
867 return snd_pcm_free_vmalloc_buffer(subs);
871 * vx_pcm_prepare - prepare callback for playback and capture
873 static int vx_pcm_prepare(snd_pcm_substream_t *subs)
875 vx_core_t *chip = snd_pcm_substream_chip(subs);
876 snd_pcm_runtime_t *runtime = subs->runtime;
877 vx_pipe_t *pipe = snd_magic_cast(vx_pipe_t, runtime->private_data, return -EINVAL);
879 // int max_size, nchunks;
881 if (chip->chip_status & VX_STAT_IS_STALE)
884 data_mode = (chip->uer_bits & IEC958_AES0_NONAUDIO) != 0;
885 if (data_mode != pipe->data_mode && ! pipe->is_capture) {
886 /* IEC958 status (raw-mode) was changed */
887 /* we reopen the pipe */
889 snd_printdd(KERN_DEBUG "reopen the pipe with data_mode = %d\n", data_mode);
890 vx_init_rmh(&rmh, CMD_FREE_PIPE);
891 vx_set_pipe_cmd_params(&rmh, 0, pipe->number, 0);
892 if ((err = vx_send_msg(chip, &rmh)) < 0)
894 vx_init_rmh(&rmh, CMD_RES_PIPE);
895 vx_set_pipe_cmd_params(&rmh, 0, pipe->number, pipe->channels);
897 rmh.Cmd[0] |= BIT_DATA_MODE;
898 if ((err = vx_send_msg(chip, &rmh)) < 0)
900 pipe->data_mode = data_mode;
903 if (chip->pcm_running && chip->freq != runtime->rate) {
904 snd_printk(KERN_ERR "vx: cannot set different clock %d from the current %d\n", runtime->rate, chip->freq);
907 vx_set_clock(chip, runtime->rate);
909 if ((err = vx_set_format(chip, pipe, runtime)) < 0)
912 if (vx_is_pcmcia(chip)) {
913 pipe->align = 2; /* 16bit word */
915 pipe->align = 4; /* 32bit word */
918 pipe->buffer_bytes = frames_to_bytes(runtime, runtime->buffer_size);
919 pipe->period_bytes = frames_to_bytes(runtime, runtime->period_size);
922 /* set the timestamp */
923 vx_update_pipe_position(chip, runtime, pipe);
925 pipe->transferred = 0;
935 * operators for PCM playback
937 static snd_pcm_ops_t vx_pcm_playback_ops = {
938 .open = vx_pcm_playback_open,
939 .close = vx_pcm_playback_close,
940 .ioctl = snd_pcm_lib_ioctl,
941 .hw_params = vx_pcm_hw_params,
942 .hw_free = vx_pcm_hw_free,
943 .prepare = vx_pcm_prepare,
944 .trigger = vx_pcm_trigger,
945 .pointer = vx_pcm_playback_pointer,
946 .page = snd_pcm_get_vmalloc_page,
951 * playback hw information
954 static snd_pcm_hardware_t vx_pcm_capture_hw = {
955 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
956 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_MMAP_VALID),
957 .formats = /*SNDRV_PCM_FMTBIT_U8 |*/ SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_3LE,
958 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
963 .buffer_bytes_max = (128*1024),
964 .period_bytes_min = 126,
965 .period_bytes_max = (128*1024),
967 .periods_max = VX_MAX_PERIODS,
973 * vx_pcm_capture_open - open callback for capture
975 static int vx_pcm_capture_open(snd_pcm_substream_t *subs)
977 snd_pcm_runtime_t *runtime = subs->runtime;
978 vx_core_t *chip = snd_pcm_substream_chip(subs);
980 vx_pipe_t *pipe_out_monitoring = NULL;
984 if (chip->chip_status & VX_STAT_IS_STALE)
987 audio = subs->pcm->device * 2;
988 snd_assert(audio < chip->audio_ins, return -EINVAL);
989 err = vx_alloc_pipe(chip, 1, audio, 2, &pipe);
992 pipe->substream = subs;
993 tasklet_init(&pipe->start_tq, vx_pcm_delayed_start, (unsigned long)subs);
994 chip->capture_pipes[audio] = pipe;
996 /* check if monitoring is needed */
997 if (chip->audio_monitor_active[audio]) {
998 pipe_out_monitoring = chip->playback_pipes[audio];
999 if (! pipe_out_monitoring) {
1000 /* allocate a pipe */
1001 err = vx_alloc_pipe(chip, 0, audio, 2, &pipe_out_monitoring);
1004 chip->playback_pipes[audio] = pipe_out_monitoring;
1006 pipe_out_monitoring->references++;
1008 if an output pipe is available, it's audios still may need to be
1009 unmuted. hence we'll have to call a mixer entry point.
1011 vx_set_monitor_level(chip, audio, chip->audio_monitor[audio], chip->audio_monitor_active[audio]);
1012 /* assuming stereo */
1013 vx_set_monitor_level(chip, audio+1, chip->audio_monitor[audio+1], chip->audio_monitor_active[audio+1]);
1016 pipe->monitoring_pipe = pipe_out_monitoring; /* default value NULL */
1018 runtime->hw = vx_pcm_capture_hw;
1019 runtime->hw.period_bytes_min = chip->ibl.size;
1020 runtime->private_data = pipe;
1022 /* align to 4 bytes (otherwise will be problematic when 24bit is used) */
1023 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 4);
1024 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 4);
1030 * vx_pcm_capture_close - close callback for capture
1032 static int vx_pcm_capture_close(snd_pcm_substream_t *subs)
1034 vx_core_t *chip = snd_pcm_substream_chip(subs);
1036 vx_pipe_t *pipe_out_monitoring;
1038 if (! subs->runtime->private_data)
1040 pipe = snd_magic_cast(vx_pipe_t, subs->runtime->private_data, return -EINVAL);
1041 chip->capture_pipes[pipe->number] = NULL;
1043 pipe_out_monitoring = pipe->monitoring_pipe;
1046 if an output pipe is attached to this input,
1047 check if it needs to be released.
1049 if (pipe_out_monitoring) {
1050 if (--pipe_out_monitoring->references == 0) {
1051 vx_free_pipe(chip, pipe_out_monitoring);
1052 chip->playback_pipes[pipe->number] = NULL;
1053 pipe->monitoring_pipe = NULL;
1057 vx_free_pipe(chip, pipe);
1063 #define DMA_READ_ALIGN 6 /* hardware alignment for read */
1066 * vx_pcm_capture_update - update the capture buffer
1068 static void vx_pcm_capture_update(vx_core_t *chip, snd_pcm_substream_t *subs, vx_pipe_t *pipe)
1070 int size, space, count;
1071 snd_pcm_runtime_t *runtime = subs->runtime;
1073 if (! pipe->prepared || (chip->chip_status & VX_STAT_IS_STALE))
1076 size = runtime->buffer_size - snd_pcm_capture_avail(runtime);
1079 size = frames_to_bytes(runtime, size);
1080 space = vx_query_hbuffer_size(chip, pipe);
1085 size = (size / 3) * 3; /* align to 3 bytes */
1086 if (size < DMA_READ_ALIGN)
1089 /* keep the last 6 bytes, they will be read after disconnection */
1090 count = size - DMA_READ_ALIGN;
1091 /* read bytes until the current pointer reaches to the aligned position
1095 if ((pipe->hw_ptr % pipe->align) == 0)
1097 if (vx_wait_for_rx_full(chip) < 0)
1099 vx_pcm_read_per_bytes(chip, runtime, pipe);
1103 /* ok, let's accelerate! */
1104 int align = pipe->align * 3;
1105 space = (count / align) * align;
1106 vx_pseudo_dma_read(chip, runtime, pipe, space);
1109 /* read the rest of bytes */
1111 if (vx_wait_for_rx_full(chip) < 0)
1113 vx_pcm_read_per_bytes(chip, runtime, pipe);
1116 /* disconnect the host, SIZE_HBUF command always switches to the stream mode */
1117 vx_send_rih_nolock(chip, IRQ_CONNECT_STREAM_NEXT);
1118 /* read the last pending 6 bytes */
1119 count = DMA_READ_ALIGN;
1121 vx_pcm_read_per_bytes(chip, runtime, pipe);
1124 /* update the position */
1125 pipe->transferred += size;
1126 if (pipe->transferred >= pipe->period_bytes) {
1127 pipe->transferred %= pipe->period_bytes;
1128 snd_pcm_period_elapsed(subs);
1133 /* disconnect the host, SIZE_HBUF command always switches to the stream mode */
1134 vx_send_rih_nolock(chip, IRQ_CONNECT_STREAM_NEXT);
1139 * vx_pcm_capture_pointer - pointer callback for capture
1141 static snd_pcm_uframes_t vx_pcm_capture_pointer(snd_pcm_substream_t *subs)
1143 snd_pcm_runtime_t *runtime = subs->runtime;
1144 vx_pipe_t *pipe = snd_magic_cast(vx_pipe_t, runtime->private_data, return -EINVAL);
1145 return bytes_to_frames(runtime, pipe->hw_ptr);
1149 * operators for PCM capture
1151 static snd_pcm_ops_t vx_pcm_capture_ops = {
1152 .open = vx_pcm_capture_open,
1153 .close = vx_pcm_capture_close,
1154 .ioctl = snd_pcm_lib_ioctl,
1155 .hw_params = vx_pcm_hw_params,
1156 .hw_free = vx_pcm_hw_free,
1157 .prepare = vx_pcm_prepare,
1158 .trigger = vx_pcm_trigger,
1159 .pointer = vx_pcm_capture_pointer,
1160 .page = snd_pcm_get_vmalloc_page,
1165 * interrupt handler for pcm streams
1167 void vx_pcm_update_intr(vx_core_t *chip, unsigned int events)
1172 #define EVENT_MASK (END_OF_BUFFER_EVENTS_PENDING|ASYNC_EVENTS_PENDING)
1174 if (events & EVENT_MASK) {
1175 vx_init_rmh(&chip->irq_rmh, CMD_ASYNC);
1176 if (events & ASYNC_EVENTS_PENDING)
1177 chip->irq_rmh.Cmd[0] |= 0x00000001; /* SEL_ASYNC_EVENTS */
1178 if (events & END_OF_BUFFER_EVENTS_PENDING)
1179 chip->irq_rmh.Cmd[0] |= 0x00000002; /* SEL_END_OF_BUF_EVENTS */
1181 if (vx_send_msg(chip, &chip->irq_rmh) < 0) {
1182 snd_printdd(KERN_ERR "msg send error!!\n");
1187 while (i < chip->irq_rmh.LgStat) {
1188 int p, buf, capture, eob;
1189 p = chip->irq_rmh.Stat[i] & MASK_FIRST_FIELD;
1190 capture = (chip->irq_rmh.Stat[i] & 0x400000) ? 1 : 0;
1191 eob = (chip->irq_rmh.Stat[i] & 0x800000) ? 1 : 0;
1193 if (events & ASYNC_EVENTS_PENDING)
1195 buf = 1; /* force to transfer */
1196 if (events & END_OF_BUFFER_EVENTS_PENDING) {
1198 buf = chip->irq_rmh.Stat[i];
1203 snd_assert(p >= 0 && (unsigned int)p < chip->audio_outs,);
1204 pipe = chip->playback_pipes[p];
1205 if (pipe && pipe->substream) {
1206 vx_pcm_playback_update(chip, pipe->substream, pipe);
1207 vx_pcm_playback_transfer(chip, pipe->substream, pipe, buf);
1212 /* update the capture pcm pointers as frequently as possible */
1213 for (i = 0; i < chip->audio_ins; i++) {
1214 pipe = chip->capture_pipes[i];
1215 if (pipe && pipe->substream)
1216 vx_pcm_capture_update(chip, pipe->substream, pipe);
1222 * vx_init_audio_io - check the availabe audio i/o and allocate pipe arrays
1224 static int vx_init_audio_io(vx_core_t *chip)
1229 vx_init_rmh(&rmh, CMD_SUPPORTED);
1230 if (vx_send_msg(chip, &rmh) < 0) {
1231 snd_printk(KERN_ERR "vx: cannot get the supported audio data\n");
1235 chip->audio_outs = rmh.Stat[0] & MASK_FIRST_FIELD;
1236 chip->audio_ins = (rmh.Stat[0] >> (FIELD_SIZE*2)) & MASK_FIRST_FIELD;
1237 chip->audio_info = rmh.Stat[1];
1239 /* allocate pipes */
1240 chip->playback_pipes = kmalloc(sizeof(vx_pipe_t *) * chip->audio_outs, GFP_KERNEL);
1241 chip->capture_pipes = kmalloc(sizeof(vx_pipe_t *) * chip->audio_ins, GFP_KERNEL);
1242 if (! chip->playback_pipes || ! chip->capture_pipes)
1245 memset(chip->playback_pipes, 0, sizeof(vx_pipe_t *) * chip->audio_outs);
1246 memset(chip->capture_pipes, 0, sizeof(vx_pipe_t *) * chip->audio_ins);
1248 preferred = chip->ibl.size;
1250 vx_set_ibl(chip, &chip->ibl); /* query the info */
1251 if (preferred > 0) {
1252 chip->ibl.size = ((preferred + chip->ibl.granularity - 1) / chip->ibl.granularity) * chip->ibl.granularity;
1253 if (chip->ibl.size > chip->ibl.max_size)
1254 chip->ibl.size = chip->ibl.max_size;
1256 chip->ibl.size = chip->ibl.min_size; /* set to the minimum */
1257 vx_set_ibl(chip, &chip->ibl);
1264 * free callback for pcm
1266 static void snd_vx_pcm_free(snd_pcm_t *pcm)
1268 vx_core_t *chip = snd_magic_cast(vx_core_t, pcm->private_data, return);
1269 chip->pcm[pcm->device] = NULL;
1270 if (chip->playback_pipes) {
1271 kfree(chip->playback_pipes);
1272 chip->playback_pipes = NULL;
1274 if (chip->capture_pipes) {
1275 kfree(chip->capture_pipes);
1276 chip->capture_pipes = NULL;
1281 * snd_vx_pcm_new - create and initialize a pcm
1283 int snd_vx_pcm_new(vx_core_t *chip)
1289 if ((err = vx_init_audio_io(chip)) < 0)
1292 for (i = 0; i < chip->hw->num_codecs; i++) {
1293 unsigned int outs, ins;
1294 outs = chip->audio_outs > i * 2 ? 1 : 0;
1295 ins = chip->audio_ins > i * 2 ? 1 : 0;
1296 if (! outs && ! ins)
1298 err = snd_pcm_new(chip->card, "VX PCM", i,
1303 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &vx_pcm_playback_ops);
1305 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &vx_pcm_capture_ops);
1307 pcm->private_data = chip;
1308 pcm->private_free = snd_vx_pcm_free;
1309 pcm->info_flags = 0;
1310 strcpy(pcm->name, chip->card->shortname);