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(snd_pcm_substream_t
*subs
, unsigned long offset
)
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(snd_pcm_substream_t
*subs
, size_t size
)
77 snd_pcm_runtime_t
*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_nocheck(runtime
->dma_area
); /* bypass the memory wrapper */
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(snd_pcm_substream_t
*subs
)
99 snd_pcm_runtime_t
*runtime
= subs
->runtime
;
100 if (runtime
->dma_area
) {
101 vfree_nocheck(runtime
->dma_area
); /* bypass the memory wrapper */
102 runtime
->dma_area
= NULL
;
109 * read three pending pcm bytes via inb()
111 static void vx_pcm_read_per_bytes(vx_core_t
*chip
, snd_pcm_runtime_t
*runtime
, vx_pipe_t
*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(vx_core_t
*chip
, pcx_time_t
*pc_time
, unsigned int *dsp_time
)
140 dsp_time
[0] = (unsigned int)((*pc_time
) >> 24) & PCX_TIME_HI_MASK
;
141 dsp_time
[1] = (unsigned int)(*pc_time
) & MASK_DSP_WORD
;
145 * vx_set_differed_time - set the differed time if specified
146 * @rmh: the rmh record to modify
147 * @pipe: the pipe to be checked
149 * if the pipe is programmed with the differed time, set the DSP time
150 * on the rmh and changes its command length.
152 * returns the increase of the command length.
154 static int vx_set_differed_time(vx_core_t
*chip
, struct vx_rmh
*rmh
, vx_pipe_t
*pipe
)
156 /* Update The length added to the RMH command by the timestamp */
157 if (! (pipe
->differed_type
& DC_DIFFERED_DELAY
))
161 rmh
->Cmd
[0] |= DSP_DIFFERED_COMMAND_MASK
;
163 /* Time stamp is the 1st following parameter */
164 vx_set_pcx_time(chip
, &pipe
->pcx_time
, &rmh
->Cmd
[1]);
166 /* Add the flags to a notified differed command */
167 if (pipe
->differed_type
& DC_NOTIFY_DELAY
)
168 rmh
->Cmd
[1] |= NOTIFY_MASK_TIME_HIGH
;
170 /* Add the flags to a multiple differed command */
171 if (pipe
->differed_type
& DC_MULTIPLE_DELAY
)
172 rmh
->Cmd
[1] |= MULTIPLE_MASK_TIME_HIGH
;
174 /* Add the flags to a stream-time differed command */
175 if (pipe
->differed_type
& DC_STREAM_TIME_DELAY
)
176 rmh
->Cmd
[1] |= STREAM_MASK_TIME_HIGH
;
183 * vx_set_stream_format - send the stream format command
184 * @pipe: the affected pipe
185 * @data: format bitmask
187 static int vx_set_stream_format(vx_core_t
*chip
, vx_pipe_t
*pipe
, unsigned int data
)
191 vx_init_rmh(&rmh
, pipe
->is_capture
?
192 CMD_FORMAT_STREAM_IN
: CMD_FORMAT_STREAM_OUT
);
193 rmh
.Cmd
[0] |= pipe
->number
<< FIELD_SIZE
;
195 /* Command might be longer since we may have to add a timestamp */
196 vx_set_differed_time(chip
, &rmh
, pipe
);
198 rmh
.Cmd
[rmh
.LgCmd
] = (data
& 0xFFFFFF00) >> 8;
199 rmh
.Cmd
[rmh
.LgCmd
+ 1] = (data
& 0xFF) << 16 /*| (datal & 0xFFFF00) >> 8*/;
202 return vx_send_msg(chip
, &rmh
);
207 * vx_set_format - set the format of a pipe
208 * @pipe: the affected pipe
209 * @runtime: pcm runtime instance to be referred
211 * returns 0 if successful, or a negative error code.
213 static int vx_set_format(vx_core_t
*chip
, vx_pipe_t
*pipe
,
214 snd_pcm_runtime_t
*runtime
)
216 unsigned int header
= HEADER_FMT_BASE
;
218 if (runtime
->channels
== 1)
219 header
|= HEADER_FMT_MONO
;
220 if (snd_pcm_format_little_endian(runtime
->format
))
221 header
|= HEADER_FMT_INTEL
;
222 if (runtime
->rate
< 32000 && runtime
->rate
> 11025)
223 header
|= HEADER_FMT_UPTO32
;
224 else if (runtime
->rate
<= 11025)
225 header
|= HEADER_FMT_UPTO11
;
227 switch (snd_pcm_format_physical_width(runtime
->format
)) {
229 case 16: header
|= HEADER_FMT_16BITS
; break;
230 case 24: header
|= HEADER_FMT_24BITS
; break;
236 return vx_set_stream_format(chip
, pipe
, header
);
240 * set / query the IBL size
242 static int vx_set_ibl(vx_core_t
*chip
, struct vx_ibl_info
*info
)
247 vx_init_rmh(&rmh
, CMD_IBL
);
248 rmh
.Cmd
[0] |= info
->size
& 0x03ffff;
249 err
= vx_send_msg(chip
, &rmh
);
252 info
->size
= rmh
.Stat
[0];
253 info
->max_size
= rmh
.Stat
[1];
254 info
->min_size
= rmh
.Stat
[2];
255 info
->granularity
= rmh
.Stat
[3];
256 snd_printdd(KERN_DEBUG
"vx_set_ibl: size = %d, max = %d, min = %d, gran = %d\n",
257 info
->size
, info
->max_size
, info
->min_size
, info
->granularity
);
263 * vx_get_pipe_state - get the state of a pipe
264 * @pipe: the pipe to be checked
265 * @state: the pointer for the returned state
267 * checks the state of a given pipe, and stores the state (1 = running,
268 * 0 = paused) on the given pointer.
270 * called from trigger callback only
272 static int vx_get_pipe_state(vx_core_t
*chip
, vx_pipe_t
*pipe
, int *state
)
277 vx_init_rmh(&rmh
, CMD_PIPE_STATE
);
278 vx_set_pipe_cmd_params(&rmh
, pipe
->is_capture
, pipe
->number
, 0);
279 err
= vx_send_msg_nolock(chip
, &rmh
); /* no lock needed for trigger */
281 *state
= (rmh
.Stat
[0] & (1 << pipe
->number
)) ? 1 : 0;
287 * vx_query_hbuffer_size - query available h-buffer size in bytes
288 * @pipe: the pipe to be checked
290 * return the available size on h-buffer in bytes,
291 * or a negative error code.
293 * NOTE: calling this function always switches to the stream mode.
294 * you'll need to disconnect the host to get back to the
297 static int vx_query_hbuffer_size(vx_core_t
*chip
, vx_pipe_t
*pipe
)
302 vx_init_rmh(&rmh
, CMD_SIZE_HBUFFER
);
303 vx_set_pipe_cmd_params(&rmh
, pipe
->is_capture
, pipe
->number
, 0);
304 if (pipe
->is_capture
)
305 rmh
.Cmd
[0] |= 0x00000001;
306 result
= vx_send_msg(chip
, &rmh
);
308 result
= rmh
.Stat
[0] & 0xffff;
314 * vx_pipe_can_start - query whether a pipe is ready for start
315 * @pipe: the pipe to be checked
317 * return 1 if ready, 0 if not ready, and negative value on error.
319 * called from trigger callback only
321 static int vx_pipe_can_start(vx_core_t
*chip
, vx_pipe_t
*pipe
)
326 vx_init_rmh(&rmh
, CMD_CAN_START_PIPE
);
327 vx_set_pipe_cmd_params(&rmh
, pipe
->is_capture
, pipe
->number
, 0);
330 err
= vx_send_msg_nolock(chip
, &rmh
); /* no lock needed for trigger */
339 * vx_conf_pipe - tell the pipe to stand by and wait for IRQA.
340 * @pipe: the pipe to be configured
342 static int vx_conf_pipe(vx_core_t
*chip
, vx_pipe_t
*pipe
)
346 vx_init_rmh(&rmh
, CMD_CONF_PIPE
);
347 if (pipe
->is_capture
)
348 rmh
.Cmd
[0] |= COMMAND_RECORD_MASK
;
349 rmh
.Cmd
[1] = 1 << pipe
->number
;
350 return vx_send_msg_nolock(chip
, &rmh
); /* no lock needed for trigger */
354 * vx_send_irqa - trigger IRQA
356 static int vx_send_irqa(vx_core_t
*chip
)
360 vx_init_rmh(&rmh
, CMD_SEND_IRQA
);
361 return vx_send_msg_nolock(chip
, &rmh
); /* no lock needed for trigger */
365 #define MAX_WAIT_FOR_DSP 250
367 * vx boards do not support inter-card sync, besides
368 * only 126 samples require to be prepared before a pipe can start
370 #define CAN_START_DELAY 2 /* wait 2ms only before asking if the pipe is ready*/
371 #define WAIT_STATE_DELAY 2 /* wait 2ms after irqA was requested and check if the pipe state toggled*/
374 * vx_toggle_pipe - start / pause a pipe
375 * @pipe: the pipe to be triggered
376 * @state: start = 1, pause = 0
378 * called from trigger callback only
381 static int vx_toggle_pipe(vx_core_t
*chip
, vx_pipe_t
*pipe
, int state
)
383 int err
, i
, cur_state
;
385 /* Check the pipe is not already in the requested state */
386 if (vx_get_pipe_state(chip
, pipe
, &cur_state
) < 0)
388 if (state
== cur_state
)
391 /* If a start is requested, ask the DSP to get prepared
392 * and wait for a positive acknowledge (when there are
393 * enough sound buffer for this pipe)
396 for (i
= 0 ; i
< MAX_WAIT_FOR_DSP
; i
++) {
397 err
= vx_pipe_can_start(chip
, pipe
);
400 /* Wait for a few, before asking again
401 * to avoid flooding the DSP with our requests
407 if ((err
= vx_conf_pipe(chip
, pipe
)) < 0)
410 if ((err
= vx_send_irqa(chip
)) < 0)
413 /* If it completes successfully, wait for the pipes
414 * reaching the expected state before returning
415 * Check one pipe only (since they are synchronous)
417 for (i
= 0; i
< MAX_WAIT_FOR_DSP
; i
++) {
418 err
= vx_get_pipe_state(chip
, pipe
, &cur_state
);
419 if (err
< 0 || cur_state
== state
)
424 return err
< 0 ? -EIO
: 0;
429 * vx_stop_pipe - stop a pipe
430 * @pipe: the pipe to be stopped
432 * called from trigger callback only
434 static int vx_stop_pipe(vx_core_t
*chip
, vx_pipe_t
*pipe
)
437 vx_init_rmh(&rmh
, CMD_STOP_PIPE
);
438 vx_set_pipe_cmd_params(&rmh
, pipe
->is_capture
, pipe
->number
, 0);
439 return vx_send_msg_nolock(chip
, &rmh
); /* no lock needed for trigger */
444 * vx_alloc_pipe - allocate a pipe and initialize the pipe instance
445 * @capture: 0 = playback, 1 = capture operation
446 * @audioid: the audio id to be assigned
447 * @num_audio: number of audio channels
448 * @pipep: the returned pipe instance
450 * return 0 on success, or a negative error code.
452 static int vx_alloc_pipe(vx_core_t
*chip
, int capture
,
453 int audioid
, int num_audio
,
462 vx_init_rmh(&rmh
, CMD_RES_PIPE
);
463 vx_set_pipe_cmd_params(&rmh
, capture
, audioid
, num_audio
);
465 if (underrun_skip_sound
)
466 rmh
.Cmd
[0] |= BIT_SKIP_SOUND
;
468 data_mode
= (chip
->uer_bits
& IEC958_AES0_NONAUDIO
) != 0;
469 if (! capture
&& data_mode
)
470 rmh
.Cmd
[0] |= BIT_DATA_MODE
;
471 err
= vx_send_msg(chip
, &rmh
);
475 /* initialize the pipe record */
476 pipe
= kcalloc(1, sizeof(*pipe
), GFP_KERNEL
);
478 /* release the pipe */
479 vx_init_rmh(&rmh
, CMD_FREE_PIPE
);
480 vx_set_pipe_cmd_params(&rmh
, capture
, audioid
, 0);
481 vx_send_msg(chip
, &rmh
);
485 /* the pipe index should be identical with the audio index */
486 pipe
->number
= audioid
;
487 pipe
->is_capture
= capture
;
488 pipe
->channels
= num_audio
;
489 pipe
->differed_type
= 0;
491 pipe
->data_mode
= data_mode
;
499 * vx_free_pipe - release a pipe
500 * @pipe: pipe to be released
502 static int vx_free_pipe(vx_core_t
*chip
, vx_pipe_t
*pipe
)
506 vx_init_rmh(&rmh
, CMD_FREE_PIPE
);
507 vx_set_pipe_cmd_params(&rmh
, pipe
->is_capture
, pipe
->number
, 0);
508 vx_send_msg(chip
, &rmh
);
516 * vx_start_stream - start the stream
518 * called from trigger callback only
520 static int vx_start_stream(vx_core_t
*chip
, vx_pipe_t
*pipe
)
524 vx_init_rmh(&rmh
, CMD_START_ONE_STREAM
);
525 vx_set_stream_cmd_params(&rmh
, pipe
->is_capture
, pipe
->number
);
526 vx_set_differed_time(chip
, &rmh
, pipe
);
527 return vx_send_msg_nolock(chip
, &rmh
); /* no lock needed for trigger */
532 * vx_stop_stream - stop the stream
534 * called from trigger callback only
536 static int vx_stop_stream(vx_core_t
*chip
, vx_pipe_t
*pipe
)
540 vx_init_rmh(&rmh
, CMD_STOP_STREAM
);
541 vx_set_stream_cmd_params(&rmh
, pipe
->is_capture
, pipe
->number
);
542 return vx_send_msg_nolock(chip
, &rmh
); /* no lock needed for trigger */
547 * playback hw information
550 static snd_pcm_hardware_t vx_pcm_playback_hw
= {
551 .info
= (SNDRV_PCM_INFO_MMAP
| SNDRV_PCM_INFO_INTERLEAVED
|
552 SNDRV_PCM_INFO_PAUSE
| SNDRV_PCM_INFO_MMAP_VALID
),
553 .formats
= /*SNDRV_PCM_FMTBIT_U8 |*/ SNDRV_PCM_FMTBIT_S16_LE
| SNDRV_PCM_FMTBIT_S24_3LE
,
554 .rates
= SNDRV_PCM_RATE_CONTINUOUS
| SNDRV_PCM_RATE_8000_48000
,
559 .buffer_bytes_max
= (128*1024),
560 .period_bytes_min
= 126,
561 .period_bytes_max
= (128*1024),
563 .periods_max
= VX_MAX_PERIODS
,
568 static void vx_pcm_delayed_start(unsigned long arg
);
571 * vx_pcm_playback_open - open callback for playback
573 static int vx_pcm_playback_open(snd_pcm_substream_t
*subs
)
575 snd_pcm_runtime_t
*runtime
= subs
->runtime
;
576 vx_core_t
*chip
= snd_pcm_substream_chip(subs
);
577 vx_pipe_t
*pipe
= NULL
;
581 if (chip
->chip_status
& VX_STAT_IS_STALE
)
584 audio
= subs
->pcm
->device
* 2;
585 snd_assert(audio
< chip
->audio_outs
, return -EINVAL
);
587 /* playback pipe may have been already allocated for monitoring */
588 pipe
= chip
->playback_pipes
[audio
];
590 /* not allocated yet */
591 err
= vx_alloc_pipe(chip
, 0, audio
, 2, &pipe
); /* stereo playback */
594 chip
->playback_pipes
[audio
] = pipe
;
596 /* open for playback */
599 pipe
->substream
= subs
;
600 tasklet_init(&pipe
->start_tq
, vx_pcm_delayed_start
, (unsigned long)subs
);
601 chip
->playback_pipes
[audio
] = pipe
;
603 runtime
->hw
= vx_pcm_playback_hw
;
604 runtime
->hw
.period_bytes_min
= chip
->ibl
.size
;
605 runtime
->private_data
= pipe
;
607 /* align to 4 bytes (otherwise will be problematic when 24bit is used) */
608 snd_pcm_hw_constraint_step(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, 4);
609 snd_pcm_hw_constraint_step(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES
, 4);
615 * vx_pcm_playback_close - close callback for playback
617 static int vx_pcm_playback_close(snd_pcm_substream_t
*subs
)
619 vx_core_t
*chip
= snd_pcm_substream_chip(subs
);
622 if (! subs
->runtime
->private_data
)
625 pipe
= subs
->runtime
->private_data
;
627 if (--pipe
->references
== 0) {
628 chip
->playback_pipes
[pipe
->number
] = NULL
;
629 vx_free_pipe(chip
, pipe
);
638 * vx_notify_end_of_buffer - send "end-of-buffer" notifier at the given pipe
639 * @pipe: the pipe to notify
641 * NB: call with a certain lock.
643 static int vx_notify_end_of_buffer(vx_core_t
*chip
, vx_pipe_t
*pipe
)
646 struct vx_rmh rmh
; /* use a temporary rmh here */
648 /* Toggle Dsp Host Interface into Message mode */
649 vx_send_rih_nolock(chip
, IRQ_PAUSE_START_CONNECT
);
650 vx_init_rmh(&rmh
, CMD_NOTIFY_END_OF_BUFFER
);
651 vx_set_stream_cmd_params(&rmh
, 0, pipe
->number
);
652 err
= vx_send_msg_nolock(chip
, &rmh
);
655 /* Toggle Dsp Host Interface back to sound transfer mode */
656 vx_send_rih_nolock(chip
, IRQ_PAUSE_START_CONNECT
);
661 * vx_pcm_playback_transfer_chunk - transfer a single chunk
663 * @pipe: the pipe to transfer
664 * @size: chunk size in bytes
666 * transfer a single buffer chunk. EOB notificaton is added after that.
667 * called from the interrupt handler, too.
671 static int vx_pcm_playback_transfer_chunk(vx_core_t
*chip
, snd_pcm_runtime_t
*runtime
, vx_pipe_t
*pipe
, int size
)
675 space
= vx_query_hbuffer_size(chip
, pipe
);
677 /* disconnect the host, SIZE_HBUF command always switches to the stream mode */
678 vx_send_rih(chip
, IRQ_CONNECT_STREAM_NEXT
);
679 snd_printd("error hbuffer\n");
683 vx_send_rih(chip
, IRQ_CONNECT_STREAM_NEXT
);
684 snd_printd("no enough hbuffer space %d\n", space
);
685 return -EIO
; /* XRUN */
688 /* we don't need irqsave here, because this function
689 * is called from either trigger callback or irq handler
691 spin_lock(&chip
->lock
);
692 vx_pseudo_dma_write(chip
, runtime
, pipe
, size
);
693 err
= vx_notify_end_of_buffer(chip
, pipe
);
694 /* disconnect the host, SIZE_HBUF command always switches to the stream mode */
695 vx_send_rih_nolock(chip
, IRQ_CONNECT_STREAM_NEXT
);
696 spin_unlock(&chip
->lock
);
701 * update the position of the given pipe.
702 * pipe->position is updated and wrapped within the buffer size.
703 * pipe->transferred is updated, too, but the size is not wrapped,
704 * so that the caller can check the total transferred size later
705 * (to call snd_pcm_period_elapsed).
707 static int vx_update_pipe_position(vx_core_t
*chip
, snd_pcm_runtime_t
*runtime
, vx_pipe_t
*pipe
)
713 vx_init_rmh(&rmh
, CMD_STREAM_SAMPLE_COUNT
);
714 vx_set_pipe_cmd_params(&rmh
, pipe
->is_capture
, pipe
->number
, 0);
715 err
= vx_send_msg(chip
, &rmh
);
719 count
= ((u64
)(rmh
.Stat
[0] & 0xfffff) << 24) | (u64
)rmh
.Stat
[1];
720 update
= (int)(count
- pipe
->cur_count
);
721 pipe
->cur_count
= count
;
722 pipe
->position
+= update
;
723 if (pipe
->position
>= (int)runtime
->buffer_size
)
724 pipe
->position
%= runtime
->buffer_size
;
725 pipe
->transferred
+= update
;
730 * transfer the pending playback buffer data to DSP
731 * called from interrupt handler
733 static void vx_pcm_playback_transfer(vx_core_t
*chip
, snd_pcm_substream_t
*subs
, vx_pipe_t
*pipe
, int nchunks
)
736 snd_pcm_runtime_t
*runtime
= subs
->runtime
;
738 if (! pipe
->prepared
|| (chip
->chip_status
& VX_STAT_IS_STALE
))
740 for (i
= 0; i
< nchunks
; i
++) {
741 if ((err
= vx_pcm_playback_transfer_chunk(chip
, runtime
, pipe
,
742 chip
->ibl
.size
)) < 0)
748 * update the playback position and call snd_pcm_period_elapsed() if necessary
749 * called from interrupt handler
751 static void vx_pcm_playback_update(vx_core_t
*chip
, snd_pcm_substream_t
*subs
, vx_pipe_t
*pipe
)
754 snd_pcm_runtime_t
*runtime
= subs
->runtime
;
756 if (pipe
->running
&& ! (chip
->chip_status
& VX_STAT_IS_STALE
)) {
757 if ((err
= vx_update_pipe_position(chip
, runtime
, pipe
)) < 0)
759 if (pipe
->transferred
>= (int)runtime
->period_size
) {
760 pipe
->transferred
%= runtime
->period_size
;
761 snd_pcm_period_elapsed(subs
);
767 * start the stream and pipe.
768 * this function is called from tasklet, which is invoked by the trigger
771 static void vx_pcm_delayed_start(unsigned long arg
)
773 snd_pcm_substream_t
*subs
= (snd_pcm_substream_t
*)arg
;
774 vx_core_t
*chip
= subs
->pcm
->private_data
;
775 vx_pipe_t
*pipe
= subs
->runtime
->private_data
;
778 /* printk( KERN_DEBUG "DDDD tasklet delayed start jiffies = %ld\n", jiffies);*/
780 if ((err
= vx_start_stream(chip
, pipe
)) < 0) {
781 snd_printk(KERN_ERR
"vx: cannot start stream\n");
784 if ((err
= vx_toggle_pipe(chip
, pipe
, 1)) < 0) {
785 snd_printk(KERN_ERR
"vx: cannot start pipe\n");
788 /* printk( KERN_DEBUG "dddd tasklet delayed start jiffies = %ld \n", jiffies);*/
792 * vx_pcm_playback_trigger - trigger callback for playback
794 static int vx_pcm_trigger(snd_pcm_substream_t
*subs
, int cmd
)
796 vx_core_t
*chip
= snd_pcm_substream_chip(subs
);
797 vx_pipe_t
*pipe
= subs
->runtime
->private_data
;
800 if (chip
->chip_status
& VX_STAT_IS_STALE
)
804 case SNDRV_PCM_TRIGGER_START
:
805 if (! pipe
->is_capture
)
806 vx_pcm_playback_transfer(chip
, subs
, pipe
, 2);
808 * we trigger the pipe using tasklet, so that the interrupts are
809 * issued surely after the trigger is completed.
811 tasklet_hi_schedule(&pipe
->start_tq
);
815 case SNDRV_PCM_TRIGGER_STOP
:
816 vx_toggle_pipe(chip
, pipe
, 0);
817 vx_stop_pipe(chip
, pipe
);
818 vx_stop_stream(chip
, pipe
);
822 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
823 if ((err
= vx_toggle_pipe(chip
, pipe
, 0)) < 0)
826 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
827 if ((err
= vx_toggle_pipe(chip
, pipe
, 1)) < 0)
837 * vx_pcm_playback_pointer - pointer callback for playback
839 static snd_pcm_uframes_t
vx_pcm_playback_pointer(snd_pcm_substream_t
*subs
)
841 snd_pcm_runtime_t
*runtime
= subs
->runtime
;
842 vx_pipe_t
*pipe
= runtime
->private_data
;
843 return pipe
->position
;
847 * vx_pcm_hw_params - hw_params callback for playback and capture
849 static int vx_pcm_hw_params(snd_pcm_substream_t
*subs
,
850 snd_pcm_hw_params_t
*hw_params
)
852 return snd_pcm_alloc_vmalloc_buffer(subs
, params_buffer_bytes(hw_params
));
856 * vx_pcm_hw_free - hw_free callback for playback and capture
858 static int vx_pcm_hw_free(snd_pcm_substream_t
*subs
)
860 return snd_pcm_free_vmalloc_buffer(subs
);
864 * vx_pcm_prepare - prepare callback for playback and capture
866 static int vx_pcm_prepare(snd_pcm_substream_t
*subs
)
868 vx_core_t
*chip
= snd_pcm_substream_chip(subs
);
869 snd_pcm_runtime_t
*runtime
= subs
->runtime
;
870 vx_pipe_t
*pipe
= runtime
->private_data
;
872 // int max_size, nchunks;
874 if (chip
->chip_status
& VX_STAT_IS_STALE
)
877 data_mode
= (chip
->uer_bits
& IEC958_AES0_NONAUDIO
) != 0;
878 if (data_mode
!= pipe
->data_mode
&& ! pipe
->is_capture
) {
879 /* IEC958 status (raw-mode) was changed */
880 /* we reopen the pipe */
882 snd_printdd(KERN_DEBUG
"reopen the pipe with data_mode = %d\n", data_mode
);
883 vx_init_rmh(&rmh
, CMD_FREE_PIPE
);
884 vx_set_pipe_cmd_params(&rmh
, 0, pipe
->number
, 0);
885 if ((err
= vx_send_msg(chip
, &rmh
)) < 0)
887 vx_init_rmh(&rmh
, CMD_RES_PIPE
);
888 vx_set_pipe_cmd_params(&rmh
, 0, pipe
->number
, pipe
->channels
);
890 rmh
.Cmd
[0] |= BIT_DATA_MODE
;
891 if ((err
= vx_send_msg(chip
, &rmh
)) < 0)
893 pipe
->data_mode
= data_mode
;
896 if (chip
->pcm_running
&& chip
->freq
!= runtime
->rate
) {
897 snd_printk(KERN_ERR
"vx: cannot set different clock %d from the current %d\n", runtime
->rate
, chip
->freq
);
900 vx_set_clock(chip
, runtime
->rate
);
902 if ((err
= vx_set_format(chip
, pipe
, runtime
)) < 0)
905 if (vx_is_pcmcia(chip
)) {
906 pipe
->align
= 2; /* 16bit word */
908 pipe
->align
= 4; /* 32bit word */
911 pipe
->buffer_bytes
= frames_to_bytes(runtime
, runtime
->buffer_size
);
912 pipe
->period_bytes
= frames_to_bytes(runtime
, runtime
->period_size
);
915 /* set the timestamp */
916 vx_update_pipe_position(chip
, runtime
, pipe
);
918 pipe
->transferred
= 0;
928 * operators for PCM playback
930 static snd_pcm_ops_t vx_pcm_playback_ops
= {
931 .open
= vx_pcm_playback_open
,
932 .close
= vx_pcm_playback_close
,
933 .ioctl
= snd_pcm_lib_ioctl
,
934 .hw_params
= vx_pcm_hw_params
,
935 .hw_free
= vx_pcm_hw_free
,
936 .prepare
= vx_pcm_prepare
,
937 .trigger
= vx_pcm_trigger
,
938 .pointer
= vx_pcm_playback_pointer
,
939 .page
= snd_pcm_get_vmalloc_page
,
944 * playback hw information
947 static snd_pcm_hardware_t vx_pcm_capture_hw
= {
948 .info
= (SNDRV_PCM_INFO_MMAP
| SNDRV_PCM_INFO_INTERLEAVED
|
949 SNDRV_PCM_INFO_PAUSE
| SNDRV_PCM_INFO_MMAP_VALID
),
950 .formats
= /*SNDRV_PCM_FMTBIT_U8 |*/ SNDRV_PCM_FMTBIT_S16_LE
| SNDRV_PCM_FMTBIT_S24_3LE
,
951 .rates
= SNDRV_PCM_RATE_CONTINUOUS
| SNDRV_PCM_RATE_8000_48000
,
956 .buffer_bytes_max
= (128*1024),
957 .period_bytes_min
= 126,
958 .period_bytes_max
= (128*1024),
960 .periods_max
= VX_MAX_PERIODS
,
966 * vx_pcm_capture_open - open callback for capture
968 static int vx_pcm_capture_open(snd_pcm_substream_t
*subs
)
970 snd_pcm_runtime_t
*runtime
= subs
->runtime
;
971 vx_core_t
*chip
= snd_pcm_substream_chip(subs
);
973 vx_pipe_t
*pipe_out_monitoring
= NULL
;
977 if (chip
->chip_status
& VX_STAT_IS_STALE
)
980 audio
= subs
->pcm
->device
* 2;
981 snd_assert(audio
< chip
->audio_ins
, return -EINVAL
);
982 err
= vx_alloc_pipe(chip
, 1, audio
, 2, &pipe
);
985 pipe
->substream
= subs
;
986 tasklet_init(&pipe
->start_tq
, vx_pcm_delayed_start
, (unsigned long)subs
);
987 chip
->capture_pipes
[audio
] = pipe
;
989 /* check if monitoring is needed */
990 if (chip
->audio_monitor_active
[audio
]) {
991 pipe_out_monitoring
= chip
->playback_pipes
[audio
];
992 if (! pipe_out_monitoring
) {
993 /* allocate a pipe */
994 err
= vx_alloc_pipe(chip
, 0, audio
, 2, &pipe_out_monitoring
);
997 chip
->playback_pipes
[audio
] = pipe_out_monitoring
;
999 pipe_out_monitoring
->references
++;
1001 if an output pipe is available, it's audios still may need to be
1002 unmuted. hence we'll have to call a mixer entry point.
1004 vx_set_monitor_level(chip
, audio
, chip
->audio_monitor
[audio
], chip
->audio_monitor_active
[audio
]);
1005 /* assuming stereo */
1006 vx_set_monitor_level(chip
, audio
+1, chip
->audio_monitor
[audio
+1], chip
->audio_monitor_active
[audio
+1]);
1009 pipe
->monitoring_pipe
= pipe_out_monitoring
; /* default value NULL */
1011 runtime
->hw
= vx_pcm_capture_hw
;
1012 runtime
->hw
.period_bytes_min
= chip
->ibl
.size
;
1013 runtime
->private_data
= pipe
;
1015 /* align to 4 bytes (otherwise will be problematic when 24bit is used) */
1016 snd_pcm_hw_constraint_step(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, 4);
1017 snd_pcm_hw_constraint_step(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES
, 4);
1023 * vx_pcm_capture_close - close callback for capture
1025 static int vx_pcm_capture_close(snd_pcm_substream_t
*subs
)
1027 vx_core_t
*chip
= snd_pcm_substream_chip(subs
);
1029 vx_pipe_t
*pipe_out_monitoring
;
1031 if (! subs
->runtime
->private_data
)
1033 pipe
= subs
->runtime
->private_data
;
1034 chip
->capture_pipes
[pipe
->number
] = NULL
;
1036 pipe_out_monitoring
= pipe
->monitoring_pipe
;
1039 if an output pipe is attached to this input,
1040 check if it needs to be released.
1042 if (pipe_out_monitoring
) {
1043 if (--pipe_out_monitoring
->references
== 0) {
1044 vx_free_pipe(chip
, pipe_out_monitoring
);
1045 chip
->playback_pipes
[pipe
->number
] = NULL
;
1046 pipe
->monitoring_pipe
= NULL
;
1050 vx_free_pipe(chip
, pipe
);
1056 #define DMA_READ_ALIGN 6 /* hardware alignment for read */
1059 * vx_pcm_capture_update - update the capture buffer
1061 static void vx_pcm_capture_update(vx_core_t
*chip
, snd_pcm_substream_t
*subs
, vx_pipe_t
*pipe
)
1063 int size
, space
, count
;
1064 snd_pcm_runtime_t
*runtime
= subs
->runtime
;
1066 if (! pipe
->prepared
|| (chip
->chip_status
& VX_STAT_IS_STALE
))
1069 size
= runtime
->buffer_size
- snd_pcm_capture_avail(runtime
);
1072 size
= frames_to_bytes(runtime
, size
);
1073 space
= vx_query_hbuffer_size(chip
, pipe
);
1078 size
= (size
/ 3) * 3; /* align to 3 bytes */
1079 if (size
< DMA_READ_ALIGN
)
1082 /* keep the last 6 bytes, they will be read after disconnection */
1083 count
= size
- DMA_READ_ALIGN
;
1084 /* read bytes until the current pointer reaches to the aligned position
1088 if ((pipe
->hw_ptr
% pipe
->align
) == 0)
1090 if (vx_wait_for_rx_full(chip
) < 0)
1092 vx_pcm_read_per_bytes(chip
, runtime
, pipe
);
1096 /* ok, let's accelerate! */
1097 int align
= pipe
->align
* 3;
1098 space
= (count
/ align
) * align
;
1099 vx_pseudo_dma_read(chip
, runtime
, pipe
, space
);
1102 /* read the rest of bytes */
1104 if (vx_wait_for_rx_full(chip
) < 0)
1106 vx_pcm_read_per_bytes(chip
, runtime
, pipe
);
1109 /* disconnect the host, SIZE_HBUF command always switches to the stream mode */
1110 vx_send_rih_nolock(chip
, IRQ_CONNECT_STREAM_NEXT
);
1111 /* read the last pending 6 bytes */
1112 count
= DMA_READ_ALIGN
;
1114 vx_pcm_read_per_bytes(chip
, runtime
, pipe
);
1117 /* update the position */
1118 pipe
->transferred
+= size
;
1119 if (pipe
->transferred
>= pipe
->period_bytes
) {
1120 pipe
->transferred
%= pipe
->period_bytes
;
1121 snd_pcm_period_elapsed(subs
);
1126 /* disconnect the host, SIZE_HBUF command always switches to the stream mode */
1127 vx_send_rih_nolock(chip
, IRQ_CONNECT_STREAM_NEXT
);
1132 * vx_pcm_capture_pointer - pointer callback for capture
1134 static snd_pcm_uframes_t
vx_pcm_capture_pointer(snd_pcm_substream_t
*subs
)
1136 snd_pcm_runtime_t
*runtime
= subs
->runtime
;
1137 vx_pipe_t
*pipe
= runtime
->private_data
;
1138 return bytes_to_frames(runtime
, pipe
->hw_ptr
);
1142 * operators for PCM capture
1144 static snd_pcm_ops_t vx_pcm_capture_ops
= {
1145 .open
= vx_pcm_capture_open
,
1146 .close
= vx_pcm_capture_close
,
1147 .ioctl
= snd_pcm_lib_ioctl
,
1148 .hw_params
= vx_pcm_hw_params
,
1149 .hw_free
= vx_pcm_hw_free
,
1150 .prepare
= vx_pcm_prepare
,
1151 .trigger
= vx_pcm_trigger
,
1152 .pointer
= vx_pcm_capture_pointer
,
1153 .page
= snd_pcm_get_vmalloc_page
,
1158 * interrupt handler for pcm streams
1160 void vx_pcm_update_intr(vx_core_t
*chip
, unsigned int events
)
1165 #define EVENT_MASK (END_OF_BUFFER_EVENTS_PENDING|ASYNC_EVENTS_PENDING)
1167 if (events
& EVENT_MASK
) {
1168 vx_init_rmh(&chip
->irq_rmh
, CMD_ASYNC
);
1169 if (events
& ASYNC_EVENTS_PENDING
)
1170 chip
->irq_rmh
.Cmd
[0] |= 0x00000001; /* SEL_ASYNC_EVENTS */
1171 if (events
& END_OF_BUFFER_EVENTS_PENDING
)
1172 chip
->irq_rmh
.Cmd
[0] |= 0x00000002; /* SEL_END_OF_BUF_EVENTS */
1174 if (vx_send_msg(chip
, &chip
->irq_rmh
) < 0) {
1175 snd_printdd(KERN_ERR
"msg send error!!\n");
1180 while (i
< chip
->irq_rmh
.LgStat
) {
1181 int p
, buf
, capture
, eob
;
1182 p
= chip
->irq_rmh
.Stat
[i
] & MASK_FIRST_FIELD
;
1183 capture
= (chip
->irq_rmh
.Stat
[i
] & 0x400000) ? 1 : 0;
1184 eob
= (chip
->irq_rmh
.Stat
[i
] & 0x800000) ? 1 : 0;
1186 if (events
& ASYNC_EVENTS_PENDING
)
1188 buf
= 1; /* force to transfer */
1189 if (events
& END_OF_BUFFER_EVENTS_PENDING
) {
1191 buf
= chip
->irq_rmh
.Stat
[i
];
1196 snd_assert(p
>= 0 && (unsigned int)p
< chip
->audio_outs
,);
1197 pipe
= chip
->playback_pipes
[p
];
1198 if (pipe
&& pipe
->substream
) {
1199 vx_pcm_playback_update(chip
, pipe
->substream
, pipe
);
1200 vx_pcm_playback_transfer(chip
, pipe
->substream
, pipe
, buf
);
1205 /* update the capture pcm pointers as frequently as possible */
1206 for (i
= 0; i
< chip
->audio_ins
; i
++) {
1207 pipe
= chip
->capture_pipes
[i
];
1208 if (pipe
&& pipe
->substream
)
1209 vx_pcm_capture_update(chip
, pipe
->substream
, pipe
);
1215 * vx_init_audio_io - check the availabe audio i/o and allocate pipe arrays
1217 static int vx_init_audio_io(vx_core_t
*chip
)
1222 vx_init_rmh(&rmh
, CMD_SUPPORTED
);
1223 if (vx_send_msg(chip
, &rmh
) < 0) {
1224 snd_printk(KERN_ERR
"vx: cannot get the supported audio data\n");
1228 chip
->audio_outs
= rmh
.Stat
[0] & MASK_FIRST_FIELD
;
1229 chip
->audio_ins
= (rmh
.Stat
[0] >> (FIELD_SIZE
*2)) & MASK_FIRST_FIELD
;
1230 chip
->audio_info
= rmh
.Stat
[1];
1232 /* allocate pipes */
1233 chip
->playback_pipes
= kmalloc(sizeof(vx_pipe_t
*) * chip
->audio_outs
, GFP_KERNEL
);
1234 chip
->capture_pipes
= kmalloc(sizeof(vx_pipe_t
*) * chip
->audio_ins
, GFP_KERNEL
);
1235 if (! chip
->playback_pipes
|| ! chip
->capture_pipes
)
1238 memset(chip
->playback_pipes
, 0, sizeof(vx_pipe_t
*) * chip
->audio_outs
);
1239 memset(chip
->capture_pipes
, 0, sizeof(vx_pipe_t
*) * chip
->audio_ins
);
1241 preferred
= chip
->ibl
.size
;
1243 vx_set_ibl(chip
, &chip
->ibl
); /* query the info */
1244 if (preferred
> 0) {
1245 chip
->ibl
.size
= ((preferred
+ chip
->ibl
.granularity
- 1) / chip
->ibl
.granularity
) * chip
->ibl
.granularity
;
1246 if (chip
->ibl
.size
> chip
->ibl
.max_size
)
1247 chip
->ibl
.size
= chip
->ibl
.max_size
;
1249 chip
->ibl
.size
= chip
->ibl
.min_size
; /* set to the minimum */
1250 vx_set_ibl(chip
, &chip
->ibl
);
1257 * free callback for pcm
1259 static void snd_vx_pcm_free(snd_pcm_t
*pcm
)
1261 vx_core_t
*chip
= pcm
->private_data
;
1262 chip
->pcm
[pcm
->device
] = NULL
;
1263 if (chip
->playback_pipes
) {
1264 kfree(chip
->playback_pipes
);
1265 chip
->playback_pipes
= NULL
;
1267 if (chip
->capture_pipes
) {
1268 kfree(chip
->capture_pipes
);
1269 chip
->capture_pipes
= NULL
;
1274 * snd_vx_pcm_new - create and initialize a pcm
1276 int snd_vx_pcm_new(vx_core_t
*chip
)
1282 if ((err
= vx_init_audio_io(chip
)) < 0)
1285 for (i
= 0; i
< chip
->hw
->num_codecs
; i
++) {
1286 unsigned int outs
, ins
;
1287 outs
= chip
->audio_outs
> i
* 2 ? 1 : 0;
1288 ins
= chip
->audio_ins
> i
* 2 ? 1 : 0;
1289 if (! outs
&& ! ins
)
1291 err
= snd_pcm_new(chip
->card
, "VX PCM", i
,
1296 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
, &vx_pcm_playback_ops
);
1298 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &vx_pcm_capture_ops
);
1300 pcm
->private_data
= chip
;
1301 pcm
->private_free
= snd_vx_pcm_free
;
1302 pcm
->info_flags
= 0;
1303 strcpy(pcm
->name
, chip
->card
->shortname
);