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 <linux/slab.h>
49 #include <linux/vmalloc.h>
50 #include <linux/delay.h>
51 #include <sound/core.h>
52 #include <sound/asoundef.h>
53 #include <sound/pcm.h>
54 #include <sound/vx_core.h>
59 * we use a vmalloc'ed (sg-)buffer
62 /* get the physical page pointer on the given offset */
63 static struct page
*snd_pcm_get_vmalloc_page(struct snd_pcm_substream
*subs
,
66 void *pageptr
= subs
->runtime
->dma_area
+ offset
;
67 return vmalloc_to_page(pageptr
);
71 * allocate a buffer via vmalloc_32().
72 * called from hw_params
73 * NOTE: this may be called not only once per pcm open!
75 static int snd_pcm_alloc_vmalloc_buffer(struct snd_pcm_substream
*subs
, size_t size
)
77 struct snd_pcm_runtime
*runtime
= subs
->runtime
;
78 if (runtime
->dma_area
) {
79 /* already allocated */
80 if (runtime
->dma_bytes
>= size
)
81 return 0; /* already enough large */
82 vfree(runtime
->dma_area
);
84 runtime
->dma_area
= vmalloc_32(size
);
85 if (! runtime
->dma_area
)
87 memset(runtime
->dma_area
, 0, size
);
88 runtime
->dma_bytes
= size
;
89 return 1; /* changed */
94 * called from hw_free callback
95 * NOTE: this may be called not only once per pcm open!
97 static int snd_pcm_free_vmalloc_buffer(struct snd_pcm_substream
*subs
)
99 struct snd_pcm_runtime
*runtime
= subs
->runtime
;
101 vfree(runtime
->dma_area
);
102 runtime
->dma_area
= NULL
;
108 * read three pending pcm bytes via inb()
110 static void vx_pcm_read_per_bytes(struct vx_core
*chip
, struct snd_pcm_runtime
*runtime
,
111 struct vx_pipe
*pipe
)
113 int offset
= pipe
->hw_ptr
;
114 unsigned char *buf
= (unsigned char *)(runtime
->dma_area
+ offset
);
115 *buf
++ = vx_inb(chip
, RXH
);
116 if (++offset
>= pipe
->buffer_bytes
) {
118 buf
= (unsigned char *)runtime
->dma_area
;
120 *buf
++ = vx_inb(chip
, RXM
);
121 if (++offset
>= pipe
->buffer_bytes
) {
123 buf
= (unsigned char *)runtime
->dma_area
;
125 *buf
++ = vx_inb(chip
, RXL
);
126 if (++offset
>= pipe
->buffer_bytes
) {
128 buf
= (unsigned char *)runtime
->dma_area
;
130 pipe
->hw_ptr
= offset
;
134 * vx_set_pcx_time - convert from the PC time to the RMH status time.
135 * @pc_time: the pointer for the PC-time to set
136 * @dsp_time: the pointer for RMH status time array
138 static void vx_set_pcx_time(struct vx_core
*chip
, pcx_time_t
*pc_time
,
139 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(struct vx_core
*chip
, struct vx_rmh
*rmh
,
156 struct vx_pipe
*pipe
)
158 /* Update The length added to the RMH command by the timestamp */
159 if (! (pipe
->differed_type
& DC_DIFFERED_DELAY
))
163 rmh
->Cmd
[0] |= DSP_DIFFERED_COMMAND_MASK
;
165 /* Time stamp is the 1st following parameter */
166 vx_set_pcx_time(chip
, &pipe
->pcx_time
, &rmh
->Cmd
[1]);
168 /* Add the flags to a notified differed command */
169 if (pipe
->differed_type
& DC_NOTIFY_DELAY
)
170 rmh
->Cmd
[1] |= NOTIFY_MASK_TIME_HIGH
;
172 /* Add the flags to a multiple differed command */
173 if (pipe
->differed_type
& DC_MULTIPLE_DELAY
)
174 rmh
->Cmd
[1] |= MULTIPLE_MASK_TIME_HIGH
;
176 /* Add the flags to a stream-time differed command */
177 if (pipe
->differed_type
& DC_STREAM_TIME_DELAY
)
178 rmh
->Cmd
[1] |= STREAM_MASK_TIME_HIGH
;
185 * vx_set_stream_format - send the stream format command
186 * @pipe: the affected pipe
187 * @data: format bitmask
189 static int vx_set_stream_format(struct vx_core
*chip
, struct vx_pipe
*pipe
,
194 vx_init_rmh(&rmh
, pipe
->is_capture
?
195 CMD_FORMAT_STREAM_IN
: CMD_FORMAT_STREAM_OUT
);
196 rmh
.Cmd
[0] |= pipe
->number
<< FIELD_SIZE
;
198 /* Command might be longer since we may have to add a timestamp */
199 vx_set_differed_time(chip
, &rmh
, pipe
);
201 rmh
.Cmd
[rmh
.LgCmd
] = (data
& 0xFFFFFF00) >> 8;
202 rmh
.Cmd
[rmh
.LgCmd
+ 1] = (data
& 0xFF) << 16 /*| (datal & 0xFFFF00) >> 8*/;
205 return vx_send_msg(chip
, &rmh
);
210 * vx_set_format - set the format of a pipe
211 * @pipe: the affected pipe
212 * @runtime: pcm runtime instance to be referred
214 * returns 0 if successful, or a negative error code.
216 static int vx_set_format(struct vx_core
*chip
, struct vx_pipe
*pipe
,
217 struct snd_pcm_runtime
*runtime
)
219 unsigned int header
= HEADER_FMT_BASE
;
221 if (runtime
->channels
== 1)
222 header
|= HEADER_FMT_MONO
;
223 if (snd_pcm_format_little_endian(runtime
->format
))
224 header
|= HEADER_FMT_INTEL
;
225 if (runtime
->rate
< 32000 && runtime
->rate
> 11025)
226 header
|= HEADER_FMT_UPTO32
;
227 else if (runtime
->rate
<= 11025)
228 header
|= HEADER_FMT_UPTO11
;
230 switch (snd_pcm_format_physical_width(runtime
->format
)) {
232 case 16: header
|= HEADER_FMT_16BITS
; break;
233 case 24: header
|= HEADER_FMT_24BITS
; break;
239 return vx_set_stream_format(chip
, pipe
, header
);
243 * set / query the IBL size
245 static int vx_set_ibl(struct vx_core
*chip
, struct vx_ibl_info
*info
)
250 vx_init_rmh(&rmh
, CMD_IBL
);
251 rmh
.Cmd
[0] |= info
->size
& 0x03ffff;
252 err
= vx_send_msg(chip
, &rmh
);
255 info
->size
= rmh
.Stat
[0];
256 info
->max_size
= rmh
.Stat
[1];
257 info
->min_size
= rmh
.Stat
[2];
258 info
->granularity
= rmh
.Stat
[3];
259 snd_printdd(KERN_DEBUG
"vx_set_ibl: size = %d, max = %d, min = %d, gran = %d\n",
260 info
->size
, info
->max_size
, info
->min_size
, info
->granularity
);
266 * vx_get_pipe_state - get the state of a pipe
267 * @pipe: the pipe to be checked
268 * @state: the pointer for the returned state
270 * checks the state of a given pipe, and stores the state (1 = running,
271 * 0 = paused) on the given pointer.
273 * called from trigger callback only
275 static int vx_get_pipe_state(struct vx_core
*chip
, struct vx_pipe
*pipe
, int *state
)
280 vx_init_rmh(&rmh
, CMD_PIPE_STATE
);
281 vx_set_pipe_cmd_params(&rmh
, pipe
->is_capture
, pipe
->number
, 0);
282 err
= vx_send_msg_nolock(chip
, &rmh
); /* no lock needed for trigger */
284 *state
= (rmh
.Stat
[0] & (1 << pipe
->number
)) ? 1 : 0;
290 * vx_query_hbuffer_size - query available h-buffer size in bytes
291 * @pipe: the pipe to be checked
293 * return the available size on h-buffer in bytes,
294 * or a negative error code.
296 * NOTE: calling this function always switches to the stream mode.
297 * you'll need to disconnect the host to get back to the
300 static int vx_query_hbuffer_size(struct vx_core
*chip
, struct vx_pipe
*pipe
)
305 vx_init_rmh(&rmh
, CMD_SIZE_HBUFFER
);
306 vx_set_pipe_cmd_params(&rmh
, pipe
->is_capture
, pipe
->number
, 0);
307 if (pipe
->is_capture
)
308 rmh
.Cmd
[0] |= 0x00000001;
309 result
= vx_send_msg(chip
, &rmh
);
311 result
= rmh
.Stat
[0] & 0xffff;
317 * vx_pipe_can_start - query whether a pipe is ready for start
318 * @pipe: the pipe to be checked
320 * return 1 if ready, 0 if not ready, and negative value on error.
322 * called from trigger callback only
324 static int vx_pipe_can_start(struct vx_core
*chip
, struct vx_pipe
*pipe
)
329 vx_init_rmh(&rmh
, CMD_CAN_START_PIPE
);
330 vx_set_pipe_cmd_params(&rmh
, pipe
->is_capture
, pipe
->number
, 0);
333 err
= vx_send_msg_nolock(chip
, &rmh
); /* no lock needed for trigger */
342 * vx_conf_pipe - tell the pipe to stand by and wait for IRQA.
343 * @pipe: the pipe to be configured
345 static int vx_conf_pipe(struct vx_core
*chip
, struct vx_pipe
*pipe
)
349 vx_init_rmh(&rmh
, CMD_CONF_PIPE
);
350 if (pipe
->is_capture
)
351 rmh
.Cmd
[0] |= COMMAND_RECORD_MASK
;
352 rmh
.Cmd
[1] = 1 << pipe
->number
;
353 return vx_send_msg_nolock(chip
, &rmh
); /* no lock needed for trigger */
357 * vx_send_irqa - trigger IRQA
359 static int vx_send_irqa(struct vx_core
*chip
)
363 vx_init_rmh(&rmh
, CMD_SEND_IRQA
);
364 return vx_send_msg_nolock(chip
, &rmh
); /* no lock needed for trigger */
368 #define MAX_WAIT_FOR_DSP 250
370 * vx boards do not support inter-card sync, besides
371 * only 126 samples require to be prepared before a pipe can start
373 #define CAN_START_DELAY 2 /* wait 2ms only before asking if the pipe is ready*/
374 #define WAIT_STATE_DELAY 2 /* wait 2ms after irqA was requested and check if the pipe state toggled*/
377 * vx_toggle_pipe - start / pause a pipe
378 * @pipe: the pipe to be triggered
379 * @state: start = 1, pause = 0
381 * called from trigger callback only
384 static int vx_toggle_pipe(struct vx_core
*chip
, struct vx_pipe
*pipe
, int state
)
386 int err
, i
, cur_state
;
388 /* Check the pipe is not already in the requested state */
389 if (vx_get_pipe_state(chip
, pipe
, &cur_state
) < 0)
391 if (state
== cur_state
)
394 /* If a start is requested, ask the DSP to get prepared
395 * and wait for a positive acknowledge (when there are
396 * enough sound buffer for this pipe)
399 for (i
= 0 ; i
< MAX_WAIT_FOR_DSP
; i
++) {
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
410 if ((err
= vx_conf_pipe(chip
, pipe
)) < 0)
413 if ((err
= vx_send_irqa(chip
)) < 0)
416 /* If it completes successfully, wait for the pipes
417 * reaching the expected state before returning
418 * Check one pipe only (since they are synchronous)
420 for (i
= 0; i
< MAX_WAIT_FOR_DSP
; i
++) {
421 err
= vx_get_pipe_state(chip
, pipe
, &cur_state
);
422 if (err
< 0 || cur_state
== state
)
427 return err
< 0 ? -EIO
: 0;
432 * vx_stop_pipe - stop a pipe
433 * @pipe: the pipe to be stopped
435 * called from trigger callback only
437 static int vx_stop_pipe(struct vx_core
*chip
, struct vx_pipe
*pipe
)
440 vx_init_rmh(&rmh
, CMD_STOP_PIPE
);
441 vx_set_pipe_cmd_params(&rmh
, pipe
->is_capture
, pipe
->number
, 0);
442 return vx_send_msg_nolock(chip
, &rmh
); /* no lock needed for trigger */
447 * vx_alloc_pipe - allocate a pipe and initialize the pipe instance
448 * @capture: 0 = playback, 1 = capture operation
449 * @audioid: the audio id to be assigned
450 * @num_audio: number of audio channels
451 * @pipep: the returned pipe instance
453 * return 0 on success, or a negative error code.
455 static int vx_alloc_pipe(struct vx_core
*chip
, int capture
,
456 int audioid
, int num_audio
,
457 struct vx_pipe
**pipep
)
460 struct vx_pipe
*pipe
;
465 vx_init_rmh(&rmh
, CMD_RES_PIPE
);
466 vx_set_pipe_cmd_params(&rmh
, capture
, audioid
, num_audio
);
468 if (underrun_skip_sound
)
469 rmh
.Cmd
[0] |= BIT_SKIP_SOUND
;
471 data_mode
= (chip
->uer_bits
& IEC958_AES0_NONAUDIO
) != 0;
472 if (! capture
&& data_mode
)
473 rmh
.Cmd
[0] |= BIT_DATA_MODE
;
474 err
= vx_send_msg(chip
, &rmh
);
478 /* initialize the pipe record */
479 pipe
= kzalloc(sizeof(*pipe
), GFP_KERNEL
);
481 /* release the pipe */
482 vx_init_rmh(&rmh
, CMD_FREE_PIPE
);
483 vx_set_pipe_cmd_params(&rmh
, capture
, audioid
, 0);
484 vx_send_msg(chip
, &rmh
);
488 /* the pipe index should be identical with the audio index */
489 pipe
->number
= audioid
;
490 pipe
->is_capture
= capture
;
491 pipe
->channels
= num_audio
;
492 pipe
->differed_type
= 0;
494 pipe
->data_mode
= data_mode
;
502 * vx_free_pipe - release a pipe
503 * @pipe: pipe to be released
505 static int vx_free_pipe(struct vx_core
*chip
, struct vx_pipe
*pipe
)
509 vx_init_rmh(&rmh
, CMD_FREE_PIPE
);
510 vx_set_pipe_cmd_params(&rmh
, pipe
->is_capture
, pipe
->number
, 0);
511 vx_send_msg(chip
, &rmh
);
519 * vx_start_stream - start the stream
521 * called from trigger callback only
523 static int vx_start_stream(struct vx_core
*chip
, struct vx_pipe
*pipe
)
527 vx_init_rmh(&rmh
, CMD_START_ONE_STREAM
);
528 vx_set_stream_cmd_params(&rmh
, pipe
->is_capture
, pipe
->number
);
529 vx_set_differed_time(chip
, &rmh
, pipe
);
530 return vx_send_msg_nolock(chip
, &rmh
); /* no lock needed for trigger */
535 * vx_stop_stream - stop the stream
537 * called from trigger callback only
539 static int vx_stop_stream(struct vx_core
*chip
, struct vx_pipe
*pipe
)
543 vx_init_rmh(&rmh
, CMD_STOP_STREAM
);
544 vx_set_stream_cmd_params(&rmh
, pipe
->is_capture
, pipe
->number
);
545 return vx_send_msg_nolock(chip
, &rmh
); /* no lock needed for trigger */
550 * playback hw information
553 static struct snd_pcm_hardware vx_pcm_playback_hw
= {
554 .info
= (SNDRV_PCM_INFO_MMAP
| SNDRV_PCM_INFO_INTERLEAVED
|
555 SNDRV_PCM_INFO_PAUSE
| SNDRV_PCM_INFO_MMAP_VALID
/*|*/
556 /*SNDRV_PCM_INFO_RESUME*/),
557 .formats
= (/*SNDRV_PCM_FMTBIT_U8 |*/
558 SNDRV_PCM_FMTBIT_S16_LE
| SNDRV_PCM_FMTBIT_S24_3LE
),
559 .rates
= SNDRV_PCM_RATE_CONTINUOUS
| SNDRV_PCM_RATE_8000_48000
,
564 .buffer_bytes_max
= (128*1024),
565 .period_bytes_min
= 126,
566 .period_bytes_max
= (128*1024),
568 .periods_max
= VX_MAX_PERIODS
,
573 static void vx_pcm_delayed_start(unsigned long arg
);
576 * vx_pcm_playback_open - open callback for playback
578 static int vx_pcm_playback_open(struct snd_pcm_substream
*subs
)
580 struct snd_pcm_runtime
*runtime
= subs
->runtime
;
581 struct vx_core
*chip
= snd_pcm_substream_chip(subs
);
582 struct vx_pipe
*pipe
= NULL
;
586 if (chip
->chip_status
& VX_STAT_IS_STALE
)
589 audio
= subs
->pcm
->device
* 2;
590 snd_assert(audio
< chip
->audio_outs
, return -EINVAL
);
592 /* playback pipe may have been already allocated for monitoring */
593 pipe
= chip
->playback_pipes
[audio
];
595 /* not allocated yet */
596 err
= vx_alloc_pipe(chip
, 0, audio
, 2, &pipe
); /* stereo playback */
599 chip
->playback_pipes
[audio
] = pipe
;
601 /* open for playback */
604 pipe
->substream
= subs
;
605 tasklet_init(&pipe
->start_tq
, vx_pcm_delayed_start
, (unsigned long)subs
);
606 chip
->playback_pipes
[audio
] = pipe
;
608 runtime
->hw
= vx_pcm_playback_hw
;
609 runtime
->hw
.period_bytes_min
= chip
->ibl
.size
;
610 runtime
->private_data
= pipe
;
612 /* align to 4 bytes (otherwise will be problematic when 24bit is used) */
613 snd_pcm_hw_constraint_step(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, 4);
614 snd_pcm_hw_constraint_step(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES
, 4);
620 * vx_pcm_playback_close - close callback for playback
622 static int vx_pcm_playback_close(struct snd_pcm_substream
*subs
)
624 struct vx_core
*chip
= snd_pcm_substream_chip(subs
);
625 struct vx_pipe
*pipe
;
627 if (! subs
->runtime
->private_data
)
630 pipe
= subs
->runtime
->private_data
;
632 if (--pipe
->references
== 0) {
633 chip
->playback_pipes
[pipe
->number
] = NULL
;
634 vx_free_pipe(chip
, pipe
);
643 * vx_notify_end_of_buffer - send "end-of-buffer" notifier at the given pipe
644 * @pipe: the pipe to notify
646 * NB: call with a certain lock.
648 static int vx_notify_end_of_buffer(struct vx_core
*chip
, struct vx_pipe
*pipe
)
651 struct vx_rmh rmh
; /* use a temporary rmh here */
653 /* Toggle Dsp Host Interface into Message mode */
654 vx_send_rih_nolock(chip
, IRQ_PAUSE_START_CONNECT
);
655 vx_init_rmh(&rmh
, CMD_NOTIFY_END_OF_BUFFER
);
656 vx_set_stream_cmd_params(&rmh
, 0, pipe
->number
);
657 err
= vx_send_msg_nolock(chip
, &rmh
);
660 /* Toggle Dsp Host Interface back to sound transfer mode */
661 vx_send_rih_nolock(chip
, IRQ_PAUSE_START_CONNECT
);
666 * vx_pcm_playback_transfer_chunk - transfer a single chunk
668 * @pipe: the pipe to transfer
669 * @size: chunk size in bytes
671 * transfer a single buffer chunk. EOB notificaton is added after that.
672 * called from the interrupt handler, too.
676 static int vx_pcm_playback_transfer_chunk(struct vx_core
*chip
,
677 struct snd_pcm_runtime
*runtime
,
678 struct vx_pipe
*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(struct vx_core
*chip
,
715 struct snd_pcm_runtime
*runtime
,
716 struct vx_pipe
*pipe
)
722 vx_init_rmh(&rmh
, CMD_STREAM_SAMPLE_COUNT
);
723 vx_set_pipe_cmd_params(&rmh
, pipe
->is_capture
, pipe
->number
, 0);
724 err
= vx_send_msg(chip
, &rmh
);
728 count
= ((u64
)(rmh
.Stat
[0] & 0xfffff) << 24) | (u64
)rmh
.Stat
[1];
729 update
= (int)(count
- pipe
->cur_count
);
730 pipe
->cur_count
= count
;
731 pipe
->position
+= update
;
732 if (pipe
->position
>= (int)runtime
->buffer_size
)
733 pipe
->position
%= runtime
->buffer_size
;
734 pipe
->transferred
+= update
;
739 * transfer the pending playback buffer data to DSP
740 * called from interrupt handler
742 static void vx_pcm_playback_transfer(struct vx_core
*chip
,
743 struct snd_pcm_substream
*subs
,
744 struct vx_pipe
*pipe
, int nchunks
)
747 struct snd_pcm_runtime
*runtime
= subs
->runtime
;
749 if (! pipe
->prepared
|| (chip
->chip_status
& VX_STAT_IS_STALE
))
751 for (i
= 0; i
< nchunks
; i
++) {
752 if ((err
= vx_pcm_playback_transfer_chunk(chip
, runtime
, pipe
,
753 chip
->ibl
.size
)) < 0)
759 * update the playback position and call snd_pcm_period_elapsed() if necessary
760 * called from interrupt handler
762 static void vx_pcm_playback_update(struct vx_core
*chip
,
763 struct snd_pcm_substream
*subs
,
764 struct vx_pipe
*pipe
)
767 struct snd_pcm_runtime
*runtime
= subs
->runtime
;
769 if (pipe
->running
&& ! (chip
->chip_status
& VX_STAT_IS_STALE
)) {
770 if ((err
= vx_update_pipe_position(chip
, runtime
, pipe
)) < 0)
772 if (pipe
->transferred
>= (int)runtime
->period_size
) {
773 pipe
->transferred
%= runtime
->period_size
;
774 snd_pcm_period_elapsed(subs
);
780 * start the stream and pipe.
781 * this function is called from tasklet, which is invoked by the trigger
784 static void vx_pcm_delayed_start(unsigned long arg
)
786 struct snd_pcm_substream
*subs
= (struct snd_pcm_substream
*)arg
;
787 struct vx_core
*chip
= subs
->pcm
->private_data
;
788 struct vx_pipe
*pipe
= subs
->runtime
->private_data
;
791 /* printk( KERN_DEBUG "DDDD tasklet delayed start jiffies = %ld\n", jiffies);*/
793 if ((err
= vx_start_stream(chip
, pipe
)) < 0) {
794 snd_printk(KERN_ERR
"vx: cannot start stream\n");
797 if ((err
= vx_toggle_pipe(chip
, pipe
, 1)) < 0) {
798 snd_printk(KERN_ERR
"vx: cannot start pipe\n");
801 /* printk( KERN_DEBUG "dddd tasklet delayed start jiffies = %ld \n", jiffies);*/
805 * vx_pcm_playback_trigger - trigger callback for playback
807 static int vx_pcm_trigger(struct snd_pcm_substream
*subs
, int cmd
)
809 struct vx_core
*chip
= snd_pcm_substream_chip(subs
);
810 struct vx_pipe
*pipe
= subs
->runtime
->private_data
;
813 if (chip
->chip_status
& VX_STAT_IS_STALE
)
817 case SNDRV_PCM_TRIGGER_START
:
818 case SNDRV_PCM_TRIGGER_RESUME
:
819 if (! pipe
->is_capture
)
820 vx_pcm_playback_transfer(chip
, subs
, pipe
, 2);
822 * we trigger the pipe using tasklet, so that the interrupts are
823 * issued surely after the trigger is completed.
825 tasklet_hi_schedule(&pipe
->start_tq
);
829 case SNDRV_PCM_TRIGGER_STOP
:
830 case SNDRV_PCM_TRIGGER_SUSPEND
:
831 vx_toggle_pipe(chip
, pipe
, 0);
832 vx_stop_pipe(chip
, pipe
);
833 vx_stop_stream(chip
, pipe
);
837 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
838 if ((err
= vx_toggle_pipe(chip
, pipe
, 0)) < 0)
841 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
842 if ((err
= vx_toggle_pipe(chip
, pipe
, 1)) < 0)
852 * vx_pcm_playback_pointer - pointer callback for playback
854 static snd_pcm_uframes_t
vx_pcm_playback_pointer(struct snd_pcm_substream
*subs
)
856 struct snd_pcm_runtime
*runtime
= subs
->runtime
;
857 struct vx_pipe
*pipe
= runtime
->private_data
;
858 return pipe
->position
;
862 * vx_pcm_hw_params - hw_params callback for playback and capture
864 static int vx_pcm_hw_params(struct snd_pcm_substream
*subs
,
865 struct snd_pcm_hw_params
*hw_params
)
867 return snd_pcm_alloc_vmalloc_buffer(subs
, params_buffer_bytes(hw_params
));
871 * vx_pcm_hw_free - hw_free callback for playback and capture
873 static int vx_pcm_hw_free(struct snd_pcm_substream
*subs
)
875 return snd_pcm_free_vmalloc_buffer(subs
);
879 * vx_pcm_prepare - prepare callback for playback and capture
881 static int vx_pcm_prepare(struct snd_pcm_substream
*subs
)
883 struct vx_core
*chip
= snd_pcm_substream_chip(subs
);
884 struct snd_pcm_runtime
*runtime
= subs
->runtime
;
885 struct vx_pipe
*pipe
= runtime
->private_data
;
887 // int max_size, nchunks;
889 if (chip
->chip_status
& VX_STAT_IS_STALE
)
892 data_mode
= (chip
->uer_bits
& IEC958_AES0_NONAUDIO
) != 0;
893 if (data_mode
!= pipe
->data_mode
&& ! pipe
->is_capture
) {
894 /* IEC958 status (raw-mode) was changed */
895 /* we reopen the pipe */
897 snd_printdd(KERN_DEBUG
"reopen the pipe with data_mode = %d\n", data_mode
);
898 vx_init_rmh(&rmh
, CMD_FREE_PIPE
);
899 vx_set_pipe_cmd_params(&rmh
, 0, pipe
->number
, 0);
900 if ((err
= vx_send_msg(chip
, &rmh
)) < 0)
902 vx_init_rmh(&rmh
, CMD_RES_PIPE
);
903 vx_set_pipe_cmd_params(&rmh
, 0, pipe
->number
, pipe
->channels
);
905 rmh
.Cmd
[0] |= BIT_DATA_MODE
;
906 if ((err
= vx_send_msg(chip
, &rmh
)) < 0)
908 pipe
->data_mode
= data_mode
;
911 if (chip
->pcm_running
&& chip
->freq
!= runtime
->rate
) {
912 snd_printk(KERN_ERR
"vx: cannot set different clock %d "
913 "from the current %d\n", runtime
->rate
, chip
->freq
);
916 vx_set_clock(chip
, runtime
->rate
);
918 if ((err
= vx_set_format(chip
, pipe
, runtime
)) < 0)
921 if (vx_is_pcmcia(chip
)) {
922 pipe
->align
= 2; /* 16bit word */
924 pipe
->align
= 4; /* 32bit word */
927 pipe
->buffer_bytes
= frames_to_bytes(runtime
, runtime
->buffer_size
);
928 pipe
->period_bytes
= frames_to_bytes(runtime
, runtime
->period_size
);
931 /* set the timestamp */
932 vx_update_pipe_position(chip
, runtime
, pipe
);
934 pipe
->transferred
= 0;
944 * operators for PCM playback
946 static struct snd_pcm_ops vx_pcm_playback_ops
= {
947 .open
= vx_pcm_playback_open
,
948 .close
= vx_pcm_playback_close
,
949 .ioctl
= snd_pcm_lib_ioctl
,
950 .hw_params
= vx_pcm_hw_params
,
951 .hw_free
= vx_pcm_hw_free
,
952 .prepare
= vx_pcm_prepare
,
953 .trigger
= vx_pcm_trigger
,
954 .pointer
= vx_pcm_playback_pointer
,
955 .page
= snd_pcm_get_vmalloc_page
,
960 * playback hw information
963 static struct snd_pcm_hardware vx_pcm_capture_hw
= {
964 .info
= (SNDRV_PCM_INFO_MMAP
| SNDRV_PCM_INFO_INTERLEAVED
|
965 SNDRV_PCM_INFO_PAUSE
| SNDRV_PCM_INFO_MMAP_VALID
/*|*/
966 /*SNDRV_PCM_INFO_RESUME*/),
967 .formats
= (/*SNDRV_PCM_FMTBIT_U8 |*/
968 SNDRV_PCM_FMTBIT_S16_LE
| SNDRV_PCM_FMTBIT_S24_3LE
),
969 .rates
= SNDRV_PCM_RATE_CONTINUOUS
| SNDRV_PCM_RATE_8000_48000
,
974 .buffer_bytes_max
= (128*1024),
975 .period_bytes_min
= 126,
976 .period_bytes_max
= (128*1024),
978 .periods_max
= VX_MAX_PERIODS
,
984 * vx_pcm_capture_open - open callback for capture
986 static int vx_pcm_capture_open(struct snd_pcm_substream
*subs
)
988 struct snd_pcm_runtime
*runtime
= subs
->runtime
;
989 struct vx_core
*chip
= snd_pcm_substream_chip(subs
);
990 struct vx_pipe
*pipe
;
991 struct vx_pipe
*pipe_out_monitoring
= NULL
;
995 if (chip
->chip_status
& VX_STAT_IS_STALE
)
998 audio
= subs
->pcm
->device
* 2;
999 snd_assert(audio
< chip
->audio_ins
, return -EINVAL
);
1000 err
= vx_alloc_pipe(chip
, 1, audio
, 2, &pipe
);
1003 pipe
->substream
= subs
;
1004 tasklet_init(&pipe
->start_tq
, vx_pcm_delayed_start
, (unsigned long)subs
);
1005 chip
->capture_pipes
[audio
] = pipe
;
1007 /* check if monitoring is needed */
1008 if (chip
->audio_monitor_active
[audio
]) {
1009 pipe_out_monitoring
= chip
->playback_pipes
[audio
];
1010 if (! pipe_out_monitoring
) {
1011 /* allocate a pipe */
1012 err
= vx_alloc_pipe(chip
, 0, audio
, 2, &pipe_out_monitoring
);
1015 chip
->playback_pipes
[audio
] = pipe_out_monitoring
;
1017 pipe_out_monitoring
->references
++;
1019 if an output pipe is available, it's audios still may need to be
1020 unmuted. hence we'll have to call a mixer entry point.
1022 vx_set_monitor_level(chip
, audio
, chip
->audio_monitor
[audio
],
1023 chip
->audio_monitor_active
[audio
]);
1024 /* assuming stereo */
1025 vx_set_monitor_level(chip
, audio
+1, chip
->audio_monitor
[audio
+1],
1026 chip
->audio_monitor_active
[audio
+1]);
1029 pipe
->monitoring_pipe
= pipe_out_monitoring
; /* default value NULL */
1031 runtime
->hw
= vx_pcm_capture_hw
;
1032 runtime
->hw
.period_bytes_min
= chip
->ibl
.size
;
1033 runtime
->private_data
= pipe
;
1035 /* align to 4 bytes (otherwise will be problematic when 24bit is used) */
1036 snd_pcm_hw_constraint_step(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, 4);
1037 snd_pcm_hw_constraint_step(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES
, 4);
1043 * vx_pcm_capture_close - close callback for capture
1045 static int vx_pcm_capture_close(struct snd_pcm_substream
*subs
)
1047 struct vx_core
*chip
= snd_pcm_substream_chip(subs
);
1048 struct vx_pipe
*pipe
;
1049 struct vx_pipe
*pipe_out_monitoring
;
1051 if (! subs
->runtime
->private_data
)
1053 pipe
= subs
->runtime
->private_data
;
1054 chip
->capture_pipes
[pipe
->number
] = NULL
;
1056 pipe_out_monitoring
= pipe
->monitoring_pipe
;
1059 if an output pipe is attached to this input,
1060 check if it needs to be released.
1062 if (pipe_out_monitoring
) {
1063 if (--pipe_out_monitoring
->references
== 0) {
1064 vx_free_pipe(chip
, pipe_out_monitoring
);
1065 chip
->playback_pipes
[pipe
->number
] = NULL
;
1066 pipe
->monitoring_pipe
= NULL
;
1070 vx_free_pipe(chip
, pipe
);
1076 #define DMA_READ_ALIGN 6 /* hardware alignment for read */
1079 * vx_pcm_capture_update - update the capture buffer
1081 static void vx_pcm_capture_update(struct vx_core
*chip
, struct snd_pcm_substream
*subs
,
1082 struct vx_pipe
*pipe
)
1084 int size
, space
, count
;
1085 struct snd_pcm_runtime
*runtime
= subs
->runtime
;
1087 if (! pipe
->prepared
|| (chip
->chip_status
& VX_STAT_IS_STALE
))
1090 size
= runtime
->buffer_size
- snd_pcm_capture_avail(runtime
);
1093 size
= frames_to_bytes(runtime
, size
);
1094 space
= vx_query_hbuffer_size(chip
, pipe
);
1099 size
= (size
/ 3) * 3; /* align to 3 bytes */
1100 if (size
< DMA_READ_ALIGN
)
1103 /* keep the last 6 bytes, they will be read after disconnection */
1104 count
= size
- DMA_READ_ALIGN
;
1105 /* read bytes until the current pointer reaches to the aligned position
1109 if ((pipe
->hw_ptr
% pipe
->align
) == 0)
1111 if (vx_wait_for_rx_full(chip
) < 0)
1113 vx_pcm_read_per_bytes(chip
, runtime
, pipe
);
1117 /* ok, let's accelerate! */
1118 int align
= pipe
->align
* 3;
1119 space
= (count
/ align
) * align
;
1120 vx_pseudo_dma_read(chip
, runtime
, pipe
, space
);
1123 /* read the rest of bytes */
1125 if (vx_wait_for_rx_full(chip
) < 0)
1127 vx_pcm_read_per_bytes(chip
, runtime
, pipe
);
1130 /* disconnect the host, SIZE_HBUF command always switches to the stream mode */
1131 vx_send_rih_nolock(chip
, IRQ_CONNECT_STREAM_NEXT
);
1132 /* read the last pending 6 bytes */
1133 count
= DMA_READ_ALIGN
;
1135 vx_pcm_read_per_bytes(chip
, runtime
, pipe
);
1138 /* update the position */
1139 pipe
->transferred
+= size
;
1140 if (pipe
->transferred
>= pipe
->period_bytes
) {
1141 pipe
->transferred
%= pipe
->period_bytes
;
1142 snd_pcm_period_elapsed(subs
);
1147 /* disconnect the host, SIZE_HBUF command always switches to the stream mode */
1148 vx_send_rih_nolock(chip
, IRQ_CONNECT_STREAM_NEXT
);
1153 * vx_pcm_capture_pointer - pointer callback for capture
1155 static snd_pcm_uframes_t
vx_pcm_capture_pointer(struct snd_pcm_substream
*subs
)
1157 struct snd_pcm_runtime
*runtime
= subs
->runtime
;
1158 struct vx_pipe
*pipe
= runtime
->private_data
;
1159 return bytes_to_frames(runtime
, pipe
->hw_ptr
);
1163 * operators for PCM capture
1165 static struct snd_pcm_ops vx_pcm_capture_ops
= {
1166 .open
= vx_pcm_capture_open
,
1167 .close
= vx_pcm_capture_close
,
1168 .ioctl
= snd_pcm_lib_ioctl
,
1169 .hw_params
= vx_pcm_hw_params
,
1170 .hw_free
= vx_pcm_hw_free
,
1171 .prepare
= vx_pcm_prepare
,
1172 .trigger
= vx_pcm_trigger
,
1173 .pointer
= vx_pcm_capture_pointer
,
1174 .page
= snd_pcm_get_vmalloc_page
,
1179 * interrupt handler for pcm streams
1181 void vx_pcm_update_intr(struct vx_core
*chip
, unsigned int events
)
1184 struct vx_pipe
*pipe
;
1186 #define EVENT_MASK (END_OF_BUFFER_EVENTS_PENDING|ASYNC_EVENTS_PENDING)
1188 if (events
& EVENT_MASK
) {
1189 vx_init_rmh(&chip
->irq_rmh
, CMD_ASYNC
);
1190 if (events
& ASYNC_EVENTS_PENDING
)
1191 chip
->irq_rmh
.Cmd
[0] |= 0x00000001; /* SEL_ASYNC_EVENTS */
1192 if (events
& END_OF_BUFFER_EVENTS_PENDING
)
1193 chip
->irq_rmh
.Cmd
[0] |= 0x00000002; /* SEL_END_OF_BUF_EVENTS */
1195 if (vx_send_msg(chip
, &chip
->irq_rmh
) < 0) {
1196 snd_printdd(KERN_ERR
"msg send error!!\n");
1201 while (i
< chip
->irq_rmh
.LgStat
) {
1202 int p
, buf
, capture
, eob
;
1203 p
= chip
->irq_rmh
.Stat
[i
] & MASK_FIRST_FIELD
;
1204 capture
= (chip
->irq_rmh
.Stat
[i
] & 0x400000) ? 1 : 0;
1205 eob
= (chip
->irq_rmh
.Stat
[i
] & 0x800000) ? 1 : 0;
1207 if (events
& ASYNC_EVENTS_PENDING
)
1209 buf
= 1; /* force to transfer */
1210 if (events
& END_OF_BUFFER_EVENTS_PENDING
) {
1212 buf
= chip
->irq_rmh
.Stat
[i
];
1217 snd_assert(p
>= 0 && (unsigned int)p
< chip
->audio_outs
,);
1218 pipe
= chip
->playback_pipes
[p
];
1219 if (pipe
&& pipe
->substream
) {
1220 vx_pcm_playback_update(chip
, pipe
->substream
, pipe
);
1221 vx_pcm_playback_transfer(chip
, pipe
->substream
, pipe
, buf
);
1226 /* update the capture pcm pointers as frequently as possible */
1227 for (i
= 0; i
< chip
->audio_ins
; i
++) {
1228 pipe
= chip
->capture_pipes
[i
];
1229 if (pipe
&& pipe
->substream
)
1230 vx_pcm_capture_update(chip
, pipe
->substream
, pipe
);
1236 * vx_init_audio_io - check the availabe audio i/o and allocate pipe arrays
1238 static int vx_init_audio_io(struct vx_core
*chip
)
1243 vx_init_rmh(&rmh
, CMD_SUPPORTED
);
1244 if (vx_send_msg(chip
, &rmh
) < 0) {
1245 snd_printk(KERN_ERR
"vx: cannot get the supported audio data\n");
1249 chip
->audio_outs
= rmh
.Stat
[0] & MASK_FIRST_FIELD
;
1250 chip
->audio_ins
= (rmh
.Stat
[0] >> (FIELD_SIZE
*2)) & MASK_FIRST_FIELD
;
1251 chip
->audio_info
= rmh
.Stat
[1];
1253 /* allocate pipes */
1254 chip
->playback_pipes
= kcalloc(chip
->audio_outs
, sizeof(struct vx_pipe
*), GFP_KERNEL
);
1255 if (!chip
->playback_pipes
)
1257 chip
->capture_pipes
= kcalloc(chip
->audio_ins
, sizeof(struct vx_pipe
*), GFP_KERNEL
);
1258 if (!chip
->capture_pipes
) {
1259 kfree(chip
->playback_pipes
);
1263 preferred
= chip
->ibl
.size
;
1265 vx_set_ibl(chip
, &chip
->ibl
); /* query the info */
1266 if (preferred
> 0) {
1267 chip
->ibl
.size
= ((preferred
+ chip
->ibl
.granularity
- 1) /
1268 chip
->ibl
.granularity
) * chip
->ibl
.granularity
;
1269 if (chip
->ibl
.size
> chip
->ibl
.max_size
)
1270 chip
->ibl
.size
= chip
->ibl
.max_size
;
1272 chip
->ibl
.size
= chip
->ibl
.min_size
; /* set to the minimum */
1273 vx_set_ibl(chip
, &chip
->ibl
);
1280 * free callback for pcm
1282 static void snd_vx_pcm_free(struct snd_pcm
*pcm
)
1284 struct vx_core
*chip
= pcm
->private_data
;
1285 chip
->pcm
[pcm
->device
] = NULL
;
1286 kfree(chip
->playback_pipes
);
1287 chip
->playback_pipes
= NULL
;
1288 kfree(chip
->capture_pipes
);
1289 chip
->capture_pipes
= NULL
;
1293 * snd_vx_pcm_new - create and initialize a pcm
1295 int snd_vx_pcm_new(struct vx_core
*chip
)
1297 struct snd_pcm
*pcm
;
1301 if ((err
= vx_init_audio_io(chip
)) < 0)
1304 for (i
= 0; i
< chip
->hw
->num_codecs
; i
++) {
1305 unsigned int outs
, ins
;
1306 outs
= chip
->audio_outs
> i
* 2 ? 1 : 0;
1307 ins
= chip
->audio_ins
> i
* 2 ? 1 : 0;
1308 if (! outs
&& ! ins
)
1310 err
= snd_pcm_new(chip
->card
, "VX PCM", i
,
1315 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
, &vx_pcm_playback_ops
);
1317 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &vx_pcm_capture_ops
);
1319 pcm
->private_data
= chip
;
1320 pcm
->private_free
= snd_vx_pcm_free
;
1321 pcm
->info_flags
= 0;
1322 strcpy(pcm
->name
, chip
->card
->shortname
);