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 <linux/delay.h>
52 #include <sound/core.h>
53 #include <sound/asoundef.h>
54 #include <sound/pcm.h>
55 #include <sound/vx_core.h>
60 * we use a vmalloc'ed (sg-)buffer
63 /* get the physical page pointer on the given offset */
64 static struct page *snd_pcm_get_vmalloc_page(struct snd_pcm_substream *subs,
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(struct snd_pcm_substream *subs, size_t size)
78 struct snd_pcm_runtime *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(runtime->dma_area);
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(struct snd_pcm_substream *subs)
100 struct snd_pcm_runtime *runtime = subs->runtime;
102 vfree(runtime->dma_area);
103 runtime->dma_area = NULL;
109 * read three pending pcm bytes via inb()
111 static void vx_pcm_read_per_bytes(struct vx_core *chip, struct snd_pcm_runtime *runtime,
112 struct vx_pipe *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(struct vx_core *chip, pcx_time_t *pc_time,
140 unsigned int *dsp_time)
142 dsp_time[0] = (unsigned int)((*pc_time) >> 24) & PCX_TIME_HI_MASK;
143 dsp_time[1] = (unsigned int)(*pc_time) & MASK_DSP_WORD;
147 * vx_set_differed_time - set the differed time if specified
148 * @rmh: the rmh record to modify
149 * @pipe: the pipe to be checked
151 * if the pipe is programmed with the differed time, set the DSP time
152 * on the rmh and changes its command length.
154 * returns the increase of the command length.
156 static int vx_set_differed_time(struct vx_core *chip, struct vx_rmh *rmh,
157 struct vx_pipe *pipe)
159 /* Update The length added to the RMH command by the timestamp */
160 if (! (pipe->differed_type & DC_DIFFERED_DELAY))
164 rmh->Cmd[0] |= DSP_DIFFERED_COMMAND_MASK;
166 /* Time stamp is the 1st following parameter */
167 vx_set_pcx_time(chip, &pipe->pcx_time, &rmh->Cmd[1]);
169 /* Add the flags to a notified differed command */
170 if (pipe->differed_type & DC_NOTIFY_DELAY)
171 rmh->Cmd[1] |= NOTIFY_MASK_TIME_HIGH ;
173 /* Add the flags to a multiple differed command */
174 if (pipe->differed_type & DC_MULTIPLE_DELAY)
175 rmh->Cmd[1] |= MULTIPLE_MASK_TIME_HIGH;
177 /* Add the flags to a stream-time differed command */
178 if (pipe->differed_type & DC_STREAM_TIME_DELAY)
179 rmh->Cmd[1] |= STREAM_MASK_TIME_HIGH;
186 * vx_set_stream_format - send the stream format command
187 * @pipe: the affected pipe
188 * @data: format bitmask
190 static int vx_set_stream_format(struct vx_core *chip, struct vx_pipe *pipe,
195 vx_init_rmh(&rmh, pipe->is_capture ?
196 CMD_FORMAT_STREAM_IN : CMD_FORMAT_STREAM_OUT);
197 rmh.Cmd[0] |= pipe->number << FIELD_SIZE;
199 /* Command might be longer since we may have to add a timestamp */
200 vx_set_differed_time(chip, &rmh, pipe);
202 rmh.Cmd[rmh.LgCmd] = (data & 0xFFFFFF00) >> 8;
203 rmh.Cmd[rmh.LgCmd + 1] = (data & 0xFF) << 16 /*| (datal & 0xFFFF00) >> 8*/;
206 return vx_send_msg(chip, &rmh);
211 * vx_set_format - set the format of a pipe
212 * @pipe: the affected pipe
213 * @runtime: pcm runtime instance to be referred
215 * returns 0 if successful, or a negative error code.
217 static int vx_set_format(struct vx_core *chip, struct vx_pipe *pipe,
218 struct snd_pcm_runtime *runtime)
220 unsigned int header = HEADER_FMT_BASE;
222 if (runtime->channels == 1)
223 header |= HEADER_FMT_MONO;
224 if (snd_pcm_format_little_endian(runtime->format))
225 header |= HEADER_FMT_INTEL;
226 if (runtime->rate < 32000 && runtime->rate > 11025)
227 header |= HEADER_FMT_UPTO32;
228 else if (runtime->rate <= 11025)
229 header |= HEADER_FMT_UPTO11;
231 switch (snd_pcm_format_physical_width(runtime->format)) {
233 case 16: header |= HEADER_FMT_16BITS; break;
234 case 24: header |= HEADER_FMT_24BITS; break;
240 return vx_set_stream_format(chip, pipe, header);
244 * set / query the IBL size
246 static int vx_set_ibl(struct vx_core *chip, struct vx_ibl_info *info)
251 vx_init_rmh(&rmh, CMD_IBL);
252 rmh.Cmd[0] |= info->size & 0x03ffff;
253 err = vx_send_msg(chip, &rmh);
256 info->size = rmh.Stat[0];
257 info->max_size = rmh.Stat[1];
258 info->min_size = rmh.Stat[2];
259 info->granularity = rmh.Stat[3];
260 snd_printdd(KERN_DEBUG "vx_set_ibl: size = %d, max = %d, min = %d, gran = %d\n",
261 info->size, info->max_size, info->min_size, info->granularity);
267 * vx_get_pipe_state - get the state of a pipe
268 * @pipe: the pipe to be checked
269 * @state: the pointer for the returned state
271 * checks the state of a given pipe, and stores the state (1 = running,
272 * 0 = paused) on the given pointer.
274 * called from trigger callback only
276 static int vx_get_pipe_state(struct vx_core *chip, struct vx_pipe *pipe, int *state)
281 vx_init_rmh(&rmh, CMD_PIPE_STATE);
282 vx_set_pipe_cmd_params(&rmh, pipe->is_capture, pipe->number, 0);
283 err = vx_send_msg_nolock(chip, &rmh); /* no lock needed for trigger */
285 *state = (rmh.Stat[0] & (1 << pipe->number)) ? 1 : 0;
291 * vx_query_hbuffer_size - query available h-buffer size in bytes
292 * @pipe: the pipe to be checked
294 * return the available size on h-buffer in bytes,
295 * or a negative error code.
297 * NOTE: calling this function always switches to the stream mode.
298 * you'll need to disconnect the host to get back to the
301 static int vx_query_hbuffer_size(struct vx_core *chip, struct vx_pipe *pipe)
306 vx_init_rmh(&rmh, CMD_SIZE_HBUFFER);
307 vx_set_pipe_cmd_params(&rmh, pipe->is_capture, pipe->number, 0);
308 if (pipe->is_capture)
309 rmh.Cmd[0] |= 0x00000001;
310 result = vx_send_msg(chip, &rmh);
312 result = rmh.Stat[0] & 0xffff;
318 * vx_pipe_can_start - query whether a pipe is ready for start
319 * @pipe: the pipe to be checked
321 * return 1 if ready, 0 if not ready, and negative value on error.
323 * called from trigger callback only
325 static int vx_pipe_can_start(struct vx_core *chip, struct vx_pipe *pipe)
330 vx_init_rmh(&rmh, CMD_CAN_START_PIPE);
331 vx_set_pipe_cmd_params(&rmh, pipe->is_capture, pipe->number, 0);
334 err = vx_send_msg_nolock(chip, &rmh); /* no lock needed for trigger */
343 * vx_conf_pipe - tell the pipe to stand by and wait for IRQA.
344 * @pipe: the pipe to be configured
346 static int vx_conf_pipe(struct vx_core *chip, struct vx_pipe *pipe)
350 vx_init_rmh(&rmh, CMD_CONF_PIPE);
351 if (pipe->is_capture)
352 rmh.Cmd[0] |= COMMAND_RECORD_MASK;
353 rmh.Cmd[1] = 1 << pipe->number;
354 return vx_send_msg_nolock(chip, &rmh); /* no lock needed for trigger */
358 * vx_send_irqa - trigger IRQA
360 static int vx_send_irqa(struct vx_core *chip)
364 vx_init_rmh(&rmh, CMD_SEND_IRQA);
365 return vx_send_msg_nolock(chip, &rmh); /* no lock needed for trigger */
369 #define MAX_WAIT_FOR_DSP 250
371 * vx boards do not support inter-card sync, besides
372 * only 126 samples require to be prepared before a pipe can start
374 #define CAN_START_DELAY 2 /* wait 2ms only before asking if the pipe is ready*/
375 #define WAIT_STATE_DELAY 2 /* wait 2ms after irqA was requested and check if the pipe state toggled*/
378 * vx_toggle_pipe - start / pause a pipe
379 * @pipe: the pipe to be triggered
380 * @state: start = 1, pause = 0
382 * called from trigger callback only
385 static int vx_toggle_pipe(struct vx_core *chip, struct vx_pipe *pipe, int state)
387 int err, i, cur_state;
389 /* Check the pipe is not already in the requested state */
390 if (vx_get_pipe_state(chip, pipe, &cur_state) < 0)
392 if (state == cur_state)
395 /* If a start is requested, ask the DSP to get prepared
396 * and wait for a positive acknowledge (when there are
397 * enough sound buffer for this pipe)
400 for (i = 0 ; i < MAX_WAIT_FOR_DSP; i++) {
401 err = vx_pipe_can_start(chip, pipe);
404 /* Wait for a few, before asking again
405 * 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 for (i = 0; i < MAX_WAIT_FOR_DSP; i++) {
422 err = vx_get_pipe_state(chip, pipe, &cur_state);
423 if (err < 0 || cur_state == state)
428 return err < 0 ? -EIO : 0;
433 * vx_stop_pipe - stop a pipe
434 * @pipe: the pipe to be stopped
436 * called from trigger callback only
438 static int vx_stop_pipe(struct vx_core *chip, struct vx_pipe *pipe)
441 vx_init_rmh(&rmh, CMD_STOP_PIPE);
442 vx_set_pipe_cmd_params(&rmh, pipe->is_capture, pipe->number, 0);
443 return vx_send_msg_nolock(chip, &rmh); /* no lock needed for trigger */
448 * vx_alloc_pipe - allocate a pipe and initialize the pipe instance
449 * @capture: 0 = playback, 1 = capture operation
450 * @audioid: the audio id to be assigned
451 * @num_audio: number of audio channels
452 * @pipep: the returned pipe instance
454 * return 0 on success, or a negative error code.
456 static int vx_alloc_pipe(struct vx_core *chip, int capture,
457 int audioid, int num_audio,
458 struct vx_pipe **pipep)
461 struct vx_pipe *pipe;
466 vx_init_rmh(&rmh, CMD_RES_PIPE);
467 vx_set_pipe_cmd_params(&rmh, capture, audioid, num_audio);
469 if (underrun_skip_sound)
470 rmh.Cmd[0] |= BIT_SKIP_SOUND;
472 data_mode = (chip->uer_bits & IEC958_AES0_NONAUDIO) != 0;
473 if (! capture && data_mode)
474 rmh.Cmd[0] |= BIT_DATA_MODE;
475 err = vx_send_msg(chip, &rmh);
479 /* initialize the pipe record */
480 pipe = kzalloc(sizeof(*pipe), GFP_KERNEL);
482 /* release the pipe */
483 vx_init_rmh(&rmh, CMD_FREE_PIPE);
484 vx_set_pipe_cmd_params(&rmh, capture, audioid, 0);
485 vx_send_msg(chip, &rmh);
489 /* the pipe index should be identical with the audio index */
490 pipe->number = audioid;
491 pipe->is_capture = capture;
492 pipe->channels = num_audio;
493 pipe->differed_type = 0;
495 pipe->data_mode = data_mode;
503 * vx_free_pipe - release a pipe
504 * @pipe: pipe to be released
506 static int vx_free_pipe(struct vx_core *chip, struct vx_pipe *pipe)
510 vx_init_rmh(&rmh, CMD_FREE_PIPE);
511 vx_set_pipe_cmd_params(&rmh, pipe->is_capture, pipe->number, 0);
512 vx_send_msg(chip, &rmh);
520 * vx_start_stream - start the stream
522 * called from trigger callback only
524 static int vx_start_stream(struct vx_core *chip, struct vx_pipe *pipe)
528 vx_init_rmh(&rmh, CMD_START_ONE_STREAM);
529 vx_set_stream_cmd_params(&rmh, pipe->is_capture, pipe->number);
530 vx_set_differed_time(chip, &rmh, pipe);
531 return vx_send_msg_nolock(chip, &rmh); /* no lock needed for trigger */
536 * vx_stop_stream - stop the stream
538 * called from trigger callback only
540 static int vx_stop_stream(struct vx_core *chip, struct vx_pipe *pipe)
544 vx_init_rmh(&rmh, CMD_STOP_STREAM);
545 vx_set_stream_cmd_params(&rmh, pipe->is_capture, pipe->number);
546 return vx_send_msg_nolock(chip, &rmh); /* no lock needed for trigger */
551 * playback hw information
554 static struct snd_pcm_hardware vx_pcm_playback_hw = {
555 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
556 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_MMAP_VALID /*|*/
557 /*SNDRV_PCM_INFO_RESUME*/),
558 .formats = (/*SNDRV_PCM_FMTBIT_U8 |*/
559 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_3LE),
560 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
565 .buffer_bytes_max = (128*1024),
566 .period_bytes_min = 126,
567 .period_bytes_max = (128*1024),
569 .periods_max = VX_MAX_PERIODS,
574 static void vx_pcm_delayed_start(unsigned long arg);
577 * vx_pcm_playback_open - open callback for playback
579 static int vx_pcm_playback_open(struct snd_pcm_substream *subs)
581 struct snd_pcm_runtime *runtime = subs->runtime;
582 struct vx_core *chip = snd_pcm_substream_chip(subs);
583 struct vx_pipe *pipe = NULL;
587 if (chip->chip_status & VX_STAT_IS_STALE)
590 audio = subs->pcm->device * 2;
591 snd_assert(audio < chip->audio_outs, return -EINVAL);
593 /* playback pipe may have been already allocated for monitoring */
594 pipe = chip->playback_pipes[audio];
596 /* not allocated yet */
597 err = vx_alloc_pipe(chip, 0, audio, 2, &pipe); /* stereo playback */
600 chip->playback_pipes[audio] = pipe;
602 /* open for playback */
605 pipe->substream = subs;
606 tasklet_init(&pipe->start_tq, vx_pcm_delayed_start, (unsigned long)subs);
607 chip->playback_pipes[audio] = pipe;
609 runtime->hw = vx_pcm_playback_hw;
610 runtime->hw.period_bytes_min = chip->ibl.size;
611 runtime->private_data = pipe;
613 /* align to 4 bytes (otherwise will be problematic when 24bit is used) */
614 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 4);
615 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 4);
621 * vx_pcm_playback_close - close callback for playback
623 static int vx_pcm_playback_close(struct snd_pcm_substream *subs)
625 struct vx_core *chip = snd_pcm_substream_chip(subs);
626 struct vx_pipe *pipe;
628 if (! subs->runtime->private_data)
631 pipe = subs->runtime->private_data;
633 if (--pipe->references == 0) {
634 chip->playback_pipes[pipe->number] = NULL;
635 vx_free_pipe(chip, pipe);
644 * vx_notify_end_of_buffer - send "end-of-buffer" notifier at the given pipe
645 * @pipe: the pipe to notify
647 * NB: call with a certain lock.
649 static int vx_notify_end_of_buffer(struct vx_core *chip, struct vx_pipe *pipe)
652 struct vx_rmh rmh; /* use a temporary rmh here */
654 /* Toggle Dsp Host Interface into Message mode */
655 vx_send_rih_nolock(chip, IRQ_PAUSE_START_CONNECT);
656 vx_init_rmh(&rmh, CMD_NOTIFY_END_OF_BUFFER);
657 vx_set_stream_cmd_params(&rmh, 0, pipe->number);
658 err = vx_send_msg_nolock(chip, &rmh);
661 /* Toggle Dsp Host Interface back to sound transfer mode */
662 vx_send_rih_nolock(chip, IRQ_PAUSE_START_CONNECT);
667 * vx_pcm_playback_transfer_chunk - transfer a single chunk
669 * @pipe: the pipe to transfer
670 * @size: chunk size in bytes
672 * transfer a single buffer chunk. EOB notificaton is added after that.
673 * called from the interrupt handler, too.
677 static int vx_pcm_playback_transfer_chunk(struct vx_core *chip,
678 struct snd_pcm_runtime *runtime,
679 struct vx_pipe *pipe, int size)
683 space = vx_query_hbuffer_size(chip, pipe);
685 /* disconnect the host, SIZE_HBUF command always switches to the stream mode */
686 vx_send_rih(chip, IRQ_CONNECT_STREAM_NEXT);
687 snd_printd("error hbuffer\n");
691 vx_send_rih(chip, IRQ_CONNECT_STREAM_NEXT);
692 snd_printd("no enough hbuffer space %d\n", space);
693 return -EIO; /* XRUN */
696 /* we don't need irqsave here, because this function
697 * is called from either trigger callback or irq handler
699 spin_lock(&chip->lock);
700 vx_pseudo_dma_write(chip, runtime, pipe, size);
701 err = vx_notify_end_of_buffer(chip, pipe);
702 /* disconnect the host, SIZE_HBUF command always switches to the stream mode */
703 vx_send_rih_nolock(chip, IRQ_CONNECT_STREAM_NEXT);
704 spin_unlock(&chip->lock);
709 * update the position of the given pipe.
710 * pipe->position is updated and wrapped within the buffer size.
711 * pipe->transferred is updated, too, but the size is not wrapped,
712 * so that the caller can check the total transferred size later
713 * (to call snd_pcm_period_elapsed).
715 static int vx_update_pipe_position(struct vx_core *chip,
716 struct snd_pcm_runtime *runtime,
717 struct vx_pipe *pipe)
723 vx_init_rmh(&rmh, CMD_STREAM_SAMPLE_COUNT);
724 vx_set_pipe_cmd_params(&rmh, pipe->is_capture, pipe->number, 0);
725 err = vx_send_msg(chip, &rmh);
729 count = ((u64)(rmh.Stat[0] & 0xfffff) << 24) | (u64)rmh.Stat[1];
730 update = (int)(count - pipe->cur_count);
731 pipe->cur_count = count;
732 pipe->position += update;
733 if (pipe->position >= (int)runtime->buffer_size)
734 pipe->position %= runtime->buffer_size;
735 pipe->transferred += update;
740 * transfer the pending playback buffer data to DSP
741 * called from interrupt handler
743 static void vx_pcm_playback_transfer(struct vx_core *chip,
744 struct snd_pcm_substream *subs,
745 struct vx_pipe *pipe, int nchunks)
748 struct snd_pcm_runtime *runtime = subs->runtime;
750 if (! pipe->prepared || (chip->chip_status & VX_STAT_IS_STALE))
752 for (i = 0; i < nchunks; i++) {
753 if ((err = vx_pcm_playback_transfer_chunk(chip, runtime, pipe,
754 chip->ibl.size)) < 0)
760 * update the playback position and call snd_pcm_period_elapsed() if necessary
761 * called from interrupt handler
763 static void vx_pcm_playback_update(struct vx_core *chip,
764 struct snd_pcm_substream *subs,
765 struct vx_pipe *pipe)
768 struct snd_pcm_runtime *runtime = subs->runtime;
770 if (pipe->running && ! (chip->chip_status & VX_STAT_IS_STALE)) {
771 if ((err = vx_update_pipe_position(chip, runtime, pipe)) < 0)
773 if (pipe->transferred >= (int)runtime->period_size) {
774 pipe->transferred %= runtime->period_size;
775 snd_pcm_period_elapsed(subs);
781 * start the stream and pipe.
782 * this function is called from tasklet, which is invoked by the trigger
785 static void vx_pcm_delayed_start(unsigned long arg)
787 struct snd_pcm_substream *subs = (struct snd_pcm_substream *)arg;
788 struct vx_core *chip = subs->pcm->private_data;
789 struct vx_pipe *pipe = subs->runtime->private_data;
792 /* printk( KERN_DEBUG "DDDD tasklet delayed start jiffies = %ld\n", jiffies);*/
794 if ((err = vx_start_stream(chip, pipe)) < 0) {
795 snd_printk(KERN_ERR "vx: cannot start stream\n");
798 if ((err = vx_toggle_pipe(chip, pipe, 1)) < 0) {
799 snd_printk(KERN_ERR "vx: cannot start pipe\n");
802 /* printk( KERN_DEBUG "dddd tasklet delayed start jiffies = %ld \n", jiffies);*/
806 * vx_pcm_playback_trigger - trigger callback for playback
808 static int vx_pcm_trigger(struct snd_pcm_substream *subs, int cmd)
810 struct vx_core *chip = snd_pcm_substream_chip(subs);
811 struct vx_pipe *pipe = subs->runtime->private_data;
814 if (chip->chip_status & VX_STAT_IS_STALE)
818 case SNDRV_PCM_TRIGGER_START:
819 case SNDRV_PCM_TRIGGER_RESUME:
820 if (! pipe->is_capture)
821 vx_pcm_playback_transfer(chip, subs, pipe, 2);
823 * we trigger the pipe using tasklet, so that the interrupts are
824 * issued surely after the trigger is completed.
826 tasklet_hi_schedule(&pipe->start_tq);
830 case SNDRV_PCM_TRIGGER_STOP:
831 case SNDRV_PCM_TRIGGER_SUSPEND:
832 vx_toggle_pipe(chip, pipe, 0);
833 vx_stop_pipe(chip, pipe);
834 vx_stop_stream(chip, pipe);
838 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
839 if ((err = vx_toggle_pipe(chip, pipe, 0)) < 0)
842 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
843 if ((err = vx_toggle_pipe(chip, pipe, 1)) < 0)
853 * vx_pcm_playback_pointer - pointer callback for playback
855 static snd_pcm_uframes_t vx_pcm_playback_pointer(struct snd_pcm_substream *subs)
857 struct snd_pcm_runtime *runtime = subs->runtime;
858 struct vx_pipe *pipe = runtime->private_data;
859 return pipe->position;
863 * vx_pcm_hw_params - hw_params callback for playback and capture
865 static int vx_pcm_hw_params(struct snd_pcm_substream *subs,
866 struct snd_pcm_hw_params *hw_params)
868 return snd_pcm_alloc_vmalloc_buffer(subs, params_buffer_bytes(hw_params));
872 * vx_pcm_hw_free - hw_free callback for playback and capture
874 static int vx_pcm_hw_free(struct snd_pcm_substream *subs)
876 return snd_pcm_free_vmalloc_buffer(subs);
880 * vx_pcm_prepare - prepare callback for playback and capture
882 static int vx_pcm_prepare(struct snd_pcm_substream *subs)
884 struct vx_core *chip = snd_pcm_substream_chip(subs);
885 struct snd_pcm_runtime *runtime = subs->runtime;
886 struct vx_pipe *pipe = runtime->private_data;
888 // int max_size, nchunks;
890 if (chip->chip_status & VX_STAT_IS_STALE)
893 data_mode = (chip->uer_bits & IEC958_AES0_NONAUDIO) != 0;
894 if (data_mode != pipe->data_mode && ! pipe->is_capture) {
895 /* IEC958 status (raw-mode) was changed */
896 /* we reopen the pipe */
898 snd_printdd(KERN_DEBUG "reopen the pipe with data_mode = %d\n", data_mode);
899 vx_init_rmh(&rmh, CMD_FREE_PIPE);
900 vx_set_pipe_cmd_params(&rmh, 0, pipe->number, 0);
901 if ((err = vx_send_msg(chip, &rmh)) < 0)
903 vx_init_rmh(&rmh, CMD_RES_PIPE);
904 vx_set_pipe_cmd_params(&rmh, 0, pipe->number, pipe->channels);
906 rmh.Cmd[0] |= BIT_DATA_MODE;
907 if ((err = vx_send_msg(chip, &rmh)) < 0)
909 pipe->data_mode = data_mode;
912 if (chip->pcm_running && chip->freq != runtime->rate) {
913 snd_printk(KERN_ERR "vx: cannot set different clock %d "
914 "from the current %d\n", runtime->rate, chip->freq);
917 vx_set_clock(chip, runtime->rate);
919 if ((err = vx_set_format(chip, pipe, runtime)) < 0)
922 if (vx_is_pcmcia(chip)) {
923 pipe->align = 2; /* 16bit word */
925 pipe->align = 4; /* 32bit word */
928 pipe->buffer_bytes = frames_to_bytes(runtime, runtime->buffer_size);
929 pipe->period_bytes = frames_to_bytes(runtime, runtime->period_size);
932 /* set the timestamp */
933 vx_update_pipe_position(chip, runtime, pipe);
935 pipe->transferred = 0;
945 * operators for PCM playback
947 static struct snd_pcm_ops vx_pcm_playback_ops = {
948 .open = vx_pcm_playback_open,
949 .close = vx_pcm_playback_close,
950 .ioctl = snd_pcm_lib_ioctl,
951 .hw_params = vx_pcm_hw_params,
952 .hw_free = vx_pcm_hw_free,
953 .prepare = vx_pcm_prepare,
954 .trigger = vx_pcm_trigger,
955 .pointer = vx_pcm_playback_pointer,
956 .page = snd_pcm_get_vmalloc_page,
961 * playback hw information
964 static struct snd_pcm_hardware vx_pcm_capture_hw = {
965 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
966 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_MMAP_VALID /*|*/
967 /*SNDRV_PCM_INFO_RESUME*/),
968 .formats = (/*SNDRV_PCM_FMTBIT_U8 |*/
969 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_3LE),
970 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
975 .buffer_bytes_max = (128*1024),
976 .period_bytes_min = 126,
977 .period_bytes_max = (128*1024),
979 .periods_max = VX_MAX_PERIODS,
985 * vx_pcm_capture_open - open callback for capture
987 static int vx_pcm_capture_open(struct snd_pcm_substream *subs)
989 struct snd_pcm_runtime *runtime = subs->runtime;
990 struct vx_core *chip = snd_pcm_substream_chip(subs);
991 struct vx_pipe *pipe;
992 struct vx_pipe *pipe_out_monitoring = NULL;
996 if (chip->chip_status & VX_STAT_IS_STALE)
999 audio = subs->pcm->device * 2;
1000 snd_assert(audio < chip->audio_ins, return -EINVAL);
1001 err = vx_alloc_pipe(chip, 1, audio, 2, &pipe);
1004 pipe->substream = subs;
1005 tasklet_init(&pipe->start_tq, vx_pcm_delayed_start, (unsigned long)subs);
1006 chip->capture_pipes[audio] = pipe;
1008 /* check if monitoring is needed */
1009 if (chip->audio_monitor_active[audio]) {
1010 pipe_out_monitoring = chip->playback_pipes[audio];
1011 if (! pipe_out_monitoring) {
1012 /* allocate a pipe */
1013 err = vx_alloc_pipe(chip, 0, audio, 2, &pipe_out_monitoring);
1016 chip->playback_pipes[audio] = pipe_out_monitoring;
1018 pipe_out_monitoring->references++;
1020 if an output pipe is available, it's audios still may need to be
1021 unmuted. hence we'll have to call a mixer entry point.
1023 vx_set_monitor_level(chip, audio, chip->audio_monitor[audio],
1024 chip->audio_monitor_active[audio]);
1025 /* assuming stereo */
1026 vx_set_monitor_level(chip, audio+1, chip->audio_monitor[audio+1],
1027 chip->audio_monitor_active[audio+1]);
1030 pipe->monitoring_pipe = pipe_out_monitoring; /* default value NULL */
1032 runtime->hw = vx_pcm_capture_hw;
1033 runtime->hw.period_bytes_min = chip->ibl.size;
1034 runtime->private_data = pipe;
1036 /* align to 4 bytes (otherwise will be problematic when 24bit is used) */
1037 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 4);
1038 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 4);
1044 * vx_pcm_capture_close - close callback for capture
1046 static int vx_pcm_capture_close(struct snd_pcm_substream *subs)
1048 struct vx_core *chip = snd_pcm_substream_chip(subs);
1049 struct vx_pipe *pipe;
1050 struct vx_pipe *pipe_out_monitoring;
1052 if (! subs->runtime->private_data)
1054 pipe = subs->runtime->private_data;
1055 chip->capture_pipes[pipe->number] = NULL;
1057 pipe_out_monitoring = pipe->monitoring_pipe;
1060 if an output pipe is attached to this input,
1061 check if it needs to be released.
1063 if (pipe_out_monitoring) {
1064 if (--pipe_out_monitoring->references == 0) {
1065 vx_free_pipe(chip, pipe_out_monitoring);
1066 chip->playback_pipes[pipe->number] = NULL;
1067 pipe->monitoring_pipe = NULL;
1071 vx_free_pipe(chip, pipe);
1077 #define DMA_READ_ALIGN 6 /* hardware alignment for read */
1080 * vx_pcm_capture_update - update the capture buffer
1082 static void vx_pcm_capture_update(struct vx_core *chip, struct snd_pcm_substream *subs,
1083 struct vx_pipe *pipe)
1085 int size, space, count;
1086 struct snd_pcm_runtime *runtime = subs->runtime;
1088 if (! pipe->prepared || (chip->chip_status & VX_STAT_IS_STALE))
1091 size = runtime->buffer_size - snd_pcm_capture_avail(runtime);
1094 size = frames_to_bytes(runtime, size);
1095 space = vx_query_hbuffer_size(chip, pipe);
1100 size = (size / 3) * 3; /* align to 3 bytes */
1101 if (size < DMA_READ_ALIGN)
1104 /* keep the last 6 bytes, they will be read after disconnection */
1105 count = size - DMA_READ_ALIGN;
1106 /* read bytes until the current pointer reaches to the aligned position
1110 if ((pipe->hw_ptr % pipe->align) == 0)
1112 if (vx_wait_for_rx_full(chip) < 0)
1114 vx_pcm_read_per_bytes(chip, runtime, pipe);
1118 /* ok, let's accelerate! */
1119 int align = pipe->align * 3;
1120 space = (count / align) * align;
1121 vx_pseudo_dma_read(chip, runtime, pipe, space);
1124 /* read the rest of bytes */
1126 if (vx_wait_for_rx_full(chip) < 0)
1128 vx_pcm_read_per_bytes(chip, runtime, pipe);
1131 /* disconnect the host, SIZE_HBUF command always switches to the stream mode */
1132 vx_send_rih_nolock(chip, IRQ_CONNECT_STREAM_NEXT);
1133 /* read the last pending 6 bytes */
1134 count = DMA_READ_ALIGN;
1136 vx_pcm_read_per_bytes(chip, runtime, pipe);
1139 /* update the position */
1140 pipe->transferred += size;
1141 if (pipe->transferred >= pipe->period_bytes) {
1142 pipe->transferred %= pipe->period_bytes;
1143 snd_pcm_period_elapsed(subs);
1148 /* disconnect the host, SIZE_HBUF command always switches to the stream mode */
1149 vx_send_rih_nolock(chip, IRQ_CONNECT_STREAM_NEXT);
1154 * vx_pcm_capture_pointer - pointer callback for capture
1156 static snd_pcm_uframes_t vx_pcm_capture_pointer(struct snd_pcm_substream *subs)
1158 struct snd_pcm_runtime *runtime = subs->runtime;
1159 struct vx_pipe *pipe = runtime->private_data;
1160 return bytes_to_frames(runtime, pipe->hw_ptr);
1164 * operators for PCM capture
1166 static struct snd_pcm_ops vx_pcm_capture_ops = {
1167 .open = vx_pcm_capture_open,
1168 .close = vx_pcm_capture_close,
1169 .ioctl = snd_pcm_lib_ioctl,
1170 .hw_params = vx_pcm_hw_params,
1171 .hw_free = vx_pcm_hw_free,
1172 .prepare = vx_pcm_prepare,
1173 .trigger = vx_pcm_trigger,
1174 .pointer = vx_pcm_capture_pointer,
1175 .page = snd_pcm_get_vmalloc_page,
1180 * interrupt handler for pcm streams
1182 void vx_pcm_update_intr(struct vx_core *chip, unsigned int events)
1185 struct vx_pipe *pipe;
1187 #define EVENT_MASK (END_OF_BUFFER_EVENTS_PENDING|ASYNC_EVENTS_PENDING)
1189 if (events & EVENT_MASK) {
1190 vx_init_rmh(&chip->irq_rmh, CMD_ASYNC);
1191 if (events & ASYNC_EVENTS_PENDING)
1192 chip->irq_rmh.Cmd[0] |= 0x00000001; /* SEL_ASYNC_EVENTS */
1193 if (events & END_OF_BUFFER_EVENTS_PENDING)
1194 chip->irq_rmh.Cmd[0] |= 0x00000002; /* SEL_END_OF_BUF_EVENTS */
1196 if (vx_send_msg(chip, &chip->irq_rmh) < 0) {
1197 snd_printdd(KERN_ERR "msg send error!!\n");
1202 while (i < chip->irq_rmh.LgStat) {
1203 int p, buf, capture, eob;
1204 p = chip->irq_rmh.Stat[i] & MASK_FIRST_FIELD;
1205 capture = (chip->irq_rmh.Stat[i] & 0x400000) ? 1 : 0;
1206 eob = (chip->irq_rmh.Stat[i] & 0x800000) ? 1 : 0;
1208 if (events & ASYNC_EVENTS_PENDING)
1210 buf = 1; /* force to transfer */
1211 if (events & END_OF_BUFFER_EVENTS_PENDING) {
1213 buf = chip->irq_rmh.Stat[i];
1218 snd_assert(p >= 0 && (unsigned int)p < chip->audio_outs,);
1219 pipe = chip->playback_pipes[p];
1220 if (pipe && pipe->substream) {
1221 vx_pcm_playback_update(chip, pipe->substream, pipe);
1222 vx_pcm_playback_transfer(chip, pipe->substream, pipe, buf);
1227 /* update the capture pcm pointers as frequently as possible */
1228 for (i = 0; i < chip->audio_ins; i++) {
1229 pipe = chip->capture_pipes[i];
1230 if (pipe && pipe->substream)
1231 vx_pcm_capture_update(chip, pipe->substream, pipe);
1237 * vx_init_audio_io - check the availabe audio i/o and allocate pipe arrays
1239 static int vx_init_audio_io(struct vx_core *chip)
1244 vx_init_rmh(&rmh, CMD_SUPPORTED);
1245 if (vx_send_msg(chip, &rmh) < 0) {
1246 snd_printk(KERN_ERR "vx: cannot get the supported audio data\n");
1250 chip->audio_outs = rmh.Stat[0] & MASK_FIRST_FIELD;
1251 chip->audio_ins = (rmh.Stat[0] >> (FIELD_SIZE*2)) & MASK_FIRST_FIELD;
1252 chip->audio_info = rmh.Stat[1];
1254 /* allocate pipes */
1255 chip->playback_pipes = kcalloc(chip->audio_outs, sizeof(struct vx_pipe *), GFP_KERNEL);
1256 if (!chip->playback_pipes)
1258 chip->capture_pipes = kcalloc(chip->audio_ins, sizeof(struct vx_pipe *), GFP_KERNEL);
1259 if (!chip->capture_pipes) {
1260 kfree(chip->playback_pipes);
1264 preferred = chip->ibl.size;
1266 vx_set_ibl(chip, &chip->ibl); /* query the info */
1267 if (preferred > 0) {
1268 chip->ibl.size = ((preferred + chip->ibl.granularity - 1) /
1269 chip->ibl.granularity) * chip->ibl.granularity;
1270 if (chip->ibl.size > chip->ibl.max_size)
1271 chip->ibl.size = chip->ibl.max_size;
1273 chip->ibl.size = chip->ibl.min_size; /* set to the minimum */
1274 vx_set_ibl(chip, &chip->ibl);
1281 * free callback for pcm
1283 static void snd_vx_pcm_free(struct snd_pcm *pcm)
1285 struct vx_core *chip = pcm->private_data;
1286 chip->pcm[pcm->device] = NULL;
1287 kfree(chip->playback_pipes);
1288 chip->playback_pipes = NULL;
1289 kfree(chip->capture_pipes);
1290 chip->capture_pipes = NULL;
1294 * snd_vx_pcm_new - create and initialize a pcm
1296 int snd_vx_pcm_new(struct vx_core *chip)
1298 struct snd_pcm *pcm;
1302 if ((err = vx_init_audio_io(chip)) < 0)
1305 for (i = 0; i < chip->hw->num_codecs; i++) {
1306 unsigned int outs, ins;
1307 outs = chip->audio_outs > i * 2 ? 1 : 0;
1308 ins = chip->audio_ins > i * 2 ? 1 : 0;
1309 if (! outs && ! ins)
1311 err = snd_pcm_new(chip->card, "VX PCM", i,
1316 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &vx_pcm_playback_ops);
1318 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &vx_pcm_capture_ops);
1320 pcm->private_data = chip;
1321 pcm->private_free = snd_vx_pcm_free;
1322 pcm->info_flags = 0;
1323 strcpy(pcm->name, chip->card->shortname);