1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for Digigram VX soundcards
7 * Copyright (c) 2002,2003 by Takashi Iwai <tiwai@suse.de>
10 * for playback, we send series of "chunks", which size is equal with the
11 * IBL size, typically 126 samples. at each end of chunk, the end-of-buffer
12 * interrupt is notified, and the interrupt handler will feed the next chunk.
14 * the current position is calculated from the sample count RMH.
15 * pipe->transferred is the counter of data which has been already transferred.
16 * if this counter reaches to the period size, snd_pcm_period_elapsed() will
19 * for capture, the situation is much easier.
20 * to get a low latency response, we'll check the capture streams at each
21 * interrupt (capture stream has no EOB notification). if the pending
22 * data is accumulated to the period size, snd_pcm_period_elapsed() is
23 * called and the pointer is updated.
25 * the current point of read buffer is kept in pipe->hw_ptr. note that
29 * - linked trigger for full-duplex mode.
30 * - scheduled action on the stream.
33 #include <linux/slab.h>
34 #include <linux/delay.h>
35 #include <sound/core.h>
36 #include <sound/asoundef.h>
37 #include <sound/pcm.h>
38 #include <sound/vx_core.h>
43 * read three pending pcm bytes via inb()
45 static void vx_pcm_read_per_bytes(struct vx_core
*chip
, struct snd_pcm_runtime
*runtime
,
48 int offset
= pipe
->hw_ptr
;
49 unsigned char *buf
= (unsigned char *)(runtime
->dma_area
+ offset
);
50 *buf
++ = vx_inb(chip
, RXH
);
51 if (++offset
>= pipe
->buffer_bytes
) {
53 buf
= (unsigned char *)runtime
->dma_area
;
55 *buf
++ = vx_inb(chip
, RXM
);
56 if (++offset
>= pipe
->buffer_bytes
) {
58 buf
= (unsigned char *)runtime
->dma_area
;
60 *buf
++ = vx_inb(chip
, RXL
);
61 if (++offset
>= pipe
->buffer_bytes
) {
64 pipe
->hw_ptr
= offset
;
68 * vx_set_pcx_time - convert from the PC time to the RMH status time.
69 * @pc_time: the pointer for the PC-time to set
70 * @dsp_time: the pointer for RMH status time array
72 static void vx_set_pcx_time(struct vx_core
*chip
, pcx_time_t
*pc_time
,
73 unsigned int *dsp_time
)
75 dsp_time
[0] = (unsigned int)((*pc_time
) >> 24) & PCX_TIME_HI_MASK
;
76 dsp_time
[1] = (unsigned int)(*pc_time
) & MASK_DSP_WORD
;
80 * vx_set_differed_time - set the differed time if specified
81 * @rmh: the rmh record to modify
82 * @pipe: the pipe to be checked
84 * if the pipe is programmed with the differed time, set the DSP time
85 * on the rmh and changes its command length.
87 * returns the increase of the command length.
89 static int vx_set_differed_time(struct vx_core
*chip
, struct vx_rmh
*rmh
,
92 /* Update The length added to the RMH command by the timestamp */
93 if (! (pipe
->differed_type
& DC_DIFFERED_DELAY
))
97 rmh
->Cmd
[0] |= DSP_DIFFERED_COMMAND_MASK
;
99 /* Time stamp is the 1st following parameter */
100 vx_set_pcx_time(chip
, &pipe
->pcx_time
, &rmh
->Cmd
[1]);
102 /* Add the flags to a notified differed command */
103 if (pipe
->differed_type
& DC_NOTIFY_DELAY
)
104 rmh
->Cmd
[1] |= NOTIFY_MASK_TIME_HIGH
;
106 /* Add the flags to a multiple differed command */
107 if (pipe
->differed_type
& DC_MULTIPLE_DELAY
)
108 rmh
->Cmd
[1] |= MULTIPLE_MASK_TIME_HIGH
;
110 /* Add the flags to a stream-time differed command */
111 if (pipe
->differed_type
& DC_STREAM_TIME_DELAY
)
112 rmh
->Cmd
[1] |= STREAM_MASK_TIME_HIGH
;
119 * vx_set_stream_format - send the stream format command
120 * @pipe: the affected pipe
121 * @data: format bitmask
123 static int vx_set_stream_format(struct vx_core
*chip
, struct vx_pipe
*pipe
,
128 vx_init_rmh(&rmh
, pipe
->is_capture
?
129 CMD_FORMAT_STREAM_IN
: CMD_FORMAT_STREAM_OUT
);
130 rmh
.Cmd
[0] |= pipe
->number
<< FIELD_SIZE
;
132 /* Command might be longer since we may have to add a timestamp */
133 vx_set_differed_time(chip
, &rmh
, pipe
);
135 rmh
.Cmd
[rmh
.LgCmd
] = (data
& 0xFFFFFF00) >> 8;
136 rmh
.Cmd
[rmh
.LgCmd
+ 1] = (data
& 0xFF) << 16 /*| (datal & 0xFFFF00) >> 8*/;
139 return vx_send_msg(chip
, &rmh
);
144 * vx_set_format - set the format of a pipe
145 * @pipe: the affected pipe
146 * @runtime: pcm runtime instance to be referred
148 * returns 0 if successful, or a negative error code.
150 static int vx_set_format(struct vx_core
*chip
, struct vx_pipe
*pipe
,
151 struct snd_pcm_runtime
*runtime
)
153 unsigned int header
= HEADER_FMT_BASE
;
155 if (runtime
->channels
== 1)
156 header
|= HEADER_FMT_MONO
;
157 if (snd_pcm_format_little_endian(runtime
->format
))
158 header
|= HEADER_FMT_INTEL
;
159 if (runtime
->rate
< 32000 && runtime
->rate
> 11025)
160 header
|= HEADER_FMT_UPTO32
;
161 else if (runtime
->rate
<= 11025)
162 header
|= HEADER_FMT_UPTO11
;
164 switch (snd_pcm_format_physical_width(runtime
->format
)) {
166 case 16: header
|= HEADER_FMT_16BITS
; break;
167 case 24: header
|= HEADER_FMT_24BITS
; break;
173 return vx_set_stream_format(chip
, pipe
, header
);
177 * set / query the IBL size
179 static int vx_set_ibl(struct vx_core
*chip
, struct vx_ibl_info
*info
)
184 vx_init_rmh(&rmh
, CMD_IBL
);
185 rmh
.Cmd
[0] |= info
->size
& 0x03ffff;
186 err
= vx_send_msg(chip
, &rmh
);
189 info
->size
= rmh
.Stat
[0];
190 info
->max_size
= rmh
.Stat
[1];
191 info
->min_size
= rmh
.Stat
[2];
192 info
->granularity
= rmh
.Stat
[3];
193 snd_printdd(KERN_DEBUG
"vx_set_ibl: size = %d, max = %d, min = %d, gran = %d\n",
194 info
->size
, info
->max_size
, info
->min_size
, info
->granularity
);
200 * vx_get_pipe_state - get the state of a pipe
201 * @pipe: the pipe to be checked
202 * @state: the pointer for the returned state
204 * checks the state of a given pipe, and stores the state (1 = running,
205 * 0 = paused) on the given pointer.
207 * called from trigger callback only
209 static int vx_get_pipe_state(struct vx_core
*chip
, struct vx_pipe
*pipe
, int *state
)
214 vx_init_rmh(&rmh
, CMD_PIPE_STATE
);
215 vx_set_pipe_cmd_params(&rmh
, pipe
->is_capture
, pipe
->number
, 0);
216 err
= vx_send_msg(chip
, &rmh
);
218 *state
= (rmh
.Stat
[0] & (1 << pipe
->number
)) ? 1 : 0;
224 * vx_query_hbuffer_size - query available h-buffer size in bytes
225 * @pipe: the pipe to be checked
227 * return the available size on h-buffer in bytes,
228 * or a negative error code.
230 * NOTE: calling this function always switches to the stream mode.
231 * you'll need to disconnect the host to get back to the
234 static int vx_query_hbuffer_size(struct vx_core
*chip
, struct vx_pipe
*pipe
)
239 vx_init_rmh(&rmh
, CMD_SIZE_HBUFFER
);
240 vx_set_pipe_cmd_params(&rmh
, pipe
->is_capture
, pipe
->number
, 0);
241 if (pipe
->is_capture
)
242 rmh
.Cmd
[0] |= 0x00000001;
243 result
= vx_send_msg(chip
, &rmh
);
245 result
= rmh
.Stat
[0] & 0xffff;
251 * vx_pipe_can_start - query whether a pipe is ready for start
252 * @pipe: the pipe to be checked
254 * return 1 if ready, 0 if not ready, and negative value on error.
256 * called from trigger callback only
258 static int vx_pipe_can_start(struct vx_core
*chip
, struct vx_pipe
*pipe
)
263 vx_init_rmh(&rmh
, CMD_CAN_START_PIPE
);
264 vx_set_pipe_cmd_params(&rmh
, pipe
->is_capture
, pipe
->number
, 0);
267 err
= vx_send_msg(chip
, &rmh
);
276 * vx_conf_pipe - tell the pipe to stand by and wait for IRQA.
277 * @pipe: the pipe to be configured
279 static int vx_conf_pipe(struct vx_core
*chip
, struct vx_pipe
*pipe
)
283 vx_init_rmh(&rmh
, CMD_CONF_PIPE
);
284 if (pipe
->is_capture
)
285 rmh
.Cmd
[0] |= COMMAND_RECORD_MASK
;
286 rmh
.Cmd
[1] = 1 << pipe
->number
;
287 return vx_send_msg(chip
, &rmh
);
291 * vx_send_irqa - trigger IRQA
293 static int vx_send_irqa(struct vx_core
*chip
)
297 vx_init_rmh(&rmh
, CMD_SEND_IRQA
);
298 return vx_send_msg(chip
, &rmh
);
302 #define MAX_WAIT_FOR_DSP 250
304 * vx boards do not support inter-card sync, besides
305 * only 126 samples require to be prepared before a pipe can start
307 #define CAN_START_DELAY 2 /* wait 2ms only before asking if the pipe is ready*/
308 #define WAIT_STATE_DELAY 2 /* wait 2ms after irqA was requested and check if the pipe state toggled*/
311 * vx_toggle_pipe - start / pause a pipe
312 * @pipe: the pipe to be triggered
313 * @state: start = 1, pause = 0
315 * called from trigger callback only
318 static int vx_toggle_pipe(struct vx_core
*chip
, struct vx_pipe
*pipe
, int state
)
320 int err
, i
, cur_state
;
322 /* Check the pipe is not already in the requested state */
323 if (vx_get_pipe_state(chip
, pipe
, &cur_state
) < 0)
325 if (state
== cur_state
)
328 /* If a start is requested, ask the DSP to get prepared
329 * and wait for a positive acknowledge (when there are
330 * enough sound buffer for this pipe)
333 for (i
= 0 ; i
< MAX_WAIT_FOR_DSP
; i
++) {
334 err
= vx_pipe_can_start(chip
, pipe
);
337 /* Wait for a few, before asking again
338 * to avoid flooding the DSP with our requests
344 if ((err
= vx_conf_pipe(chip
, pipe
)) < 0)
347 if ((err
= vx_send_irqa(chip
)) < 0)
350 /* If it completes successfully, wait for the pipes
351 * reaching the expected state before returning
352 * Check one pipe only (since they are synchronous)
354 for (i
= 0; i
< MAX_WAIT_FOR_DSP
; i
++) {
355 err
= vx_get_pipe_state(chip
, pipe
, &cur_state
);
356 if (err
< 0 || cur_state
== state
)
361 return err
< 0 ? -EIO
: 0;
366 * vx_stop_pipe - stop a pipe
367 * @pipe: the pipe to be stopped
369 * called from trigger callback only
371 static int vx_stop_pipe(struct vx_core
*chip
, struct vx_pipe
*pipe
)
374 vx_init_rmh(&rmh
, CMD_STOP_PIPE
);
375 vx_set_pipe_cmd_params(&rmh
, pipe
->is_capture
, pipe
->number
, 0);
376 return vx_send_msg(chip
, &rmh
);
381 * vx_alloc_pipe - allocate a pipe and initialize the pipe instance
382 * @capture: 0 = playback, 1 = capture operation
383 * @audioid: the audio id to be assigned
384 * @num_audio: number of audio channels
385 * @pipep: the returned pipe instance
387 * return 0 on success, or a negative error code.
389 static int vx_alloc_pipe(struct vx_core
*chip
, int capture
,
390 int audioid
, int num_audio
,
391 struct vx_pipe
**pipep
)
394 struct vx_pipe
*pipe
;
399 vx_init_rmh(&rmh
, CMD_RES_PIPE
);
400 vx_set_pipe_cmd_params(&rmh
, capture
, audioid
, num_audio
);
402 if (underrun_skip_sound
)
403 rmh
.Cmd
[0] |= BIT_SKIP_SOUND
;
405 data_mode
= (chip
->uer_bits
& IEC958_AES0_NONAUDIO
) != 0;
406 if (! capture
&& data_mode
)
407 rmh
.Cmd
[0] |= BIT_DATA_MODE
;
408 err
= vx_send_msg(chip
, &rmh
);
412 /* initialize the pipe record */
413 pipe
= kzalloc(sizeof(*pipe
), GFP_KERNEL
);
415 /* release the pipe */
416 vx_init_rmh(&rmh
, CMD_FREE_PIPE
);
417 vx_set_pipe_cmd_params(&rmh
, capture
, audioid
, 0);
418 vx_send_msg(chip
, &rmh
);
422 /* the pipe index should be identical with the audio index */
423 pipe
->number
= audioid
;
424 pipe
->is_capture
= capture
;
425 pipe
->channels
= num_audio
;
426 pipe
->differed_type
= 0;
428 pipe
->data_mode
= data_mode
;
436 * vx_free_pipe - release a pipe
437 * @pipe: pipe to be released
439 static int vx_free_pipe(struct vx_core
*chip
, struct vx_pipe
*pipe
)
443 vx_init_rmh(&rmh
, CMD_FREE_PIPE
);
444 vx_set_pipe_cmd_params(&rmh
, pipe
->is_capture
, pipe
->number
, 0);
445 vx_send_msg(chip
, &rmh
);
453 * vx_start_stream - start the stream
455 * called from trigger callback only
457 static int vx_start_stream(struct vx_core
*chip
, struct vx_pipe
*pipe
)
461 vx_init_rmh(&rmh
, CMD_START_ONE_STREAM
);
462 vx_set_stream_cmd_params(&rmh
, pipe
->is_capture
, pipe
->number
);
463 vx_set_differed_time(chip
, &rmh
, pipe
);
464 return vx_send_msg(chip
, &rmh
);
469 * vx_stop_stream - stop the stream
471 * called from trigger callback only
473 static int vx_stop_stream(struct vx_core
*chip
, struct vx_pipe
*pipe
)
477 vx_init_rmh(&rmh
, CMD_STOP_STREAM
);
478 vx_set_stream_cmd_params(&rmh
, pipe
->is_capture
, pipe
->number
);
479 return vx_send_msg(chip
, &rmh
);
484 * playback hw information
487 static const struct snd_pcm_hardware vx_pcm_playback_hw
= {
488 .info
= (SNDRV_PCM_INFO_MMAP
| SNDRV_PCM_INFO_INTERLEAVED
|
489 SNDRV_PCM_INFO_PAUSE
| SNDRV_PCM_INFO_MMAP_VALID
/*|*/
490 /*SNDRV_PCM_INFO_RESUME*/),
491 .formats
= (/*SNDRV_PCM_FMTBIT_U8 |*/
492 SNDRV_PCM_FMTBIT_S16_LE
| SNDRV_PCM_FMTBIT_S24_3LE
),
493 .rates
= SNDRV_PCM_RATE_CONTINUOUS
| SNDRV_PCM_RATE_8000_48000
,
498 .buffer_bytes_max
= (128*1024),
499 .period_bytes_min
= 126,
500 .period_bytes_max
= (128*1024),
502 .periods_max
= VX_MAX_PERIODS
,
508 * vx_pcm_playback_open - open callback for playback
510 static int vx_pcm_playback_open(struct snd_pcm_substream
*subs
)
512 struct snd_pcm_runtime
*runtime
= subs
->runtime
;
513 struct vx_core
*chip
= snd_pcm_substream_chip(subs
);
514 struct vx_pipe
*pipe
= NULL
;
518 if (chip
->chip_status
& VX_STAT_IS_STALE
)
521 audio
= subs
->pcm
->device
* 2;
522 if (snd_BUG_ON(audio
>= chip
->audio_outs
))
525 /* playback pipe may have been already allocated for monitoring */
526 pipe
= chip
->playback_pipes
[audio
];
528 /* not allocated yet */
529 err
= vx_alloc_pipe(chip
, 0, audio
, 2, &pipe
); /* stereo playback */
533 /* open for playback */
536 pipe
->substream
= subs
;
537 chip
->playback_pipes
[audio
] = pipe
;
539 runtime
->hw
= vx_pcm_playback_hw
;
540 runtime
->hw
.period_bytes_min
= chip
->ibl
.size
;
541 runtime
->private_data
= pipe
;
543 /* align to 4 bytes (otherwise will be problematic when 24bit is used) */
544 snd_pcm_hw_constraint_step(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, 4);
545 snd_pcm_hw_constraint_step(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES
, 4);
551 * vx_pcm_playback_close - close callback for playback
553 static int vx_pcm_playback_close(struct snd_pcm_substream
*subs
)
555 struct vx_core
*chip
= snd_pcm_substream_chip(subs
);
556 struct vx_pipe
*pipe
;
558 if (! subs
->runtime
->private_data
)
561 pipe
= subs
->runtime
->private_data
;
563 if (--pipe
->references
== 0) {
564 chip
->playback_pipes
[pipe
->number
] = NULL
;
565 vx_free_pipe(chip
, pipe
);
574 * vx_notify_end_of_buffer - send "end-of-buffer" notifier at the given pipe
575 * @pipe: the pipe to notify
577 * NB: call with a certain lock.
579 static int vx_notify_end_of_buffer(struct vx_core
*chip
, struct vx_pipe
*pipe
)
582 struct vx_rmh rmh
; /* use a temporary rmh here */
584 /* Toggle Dsp Host Interface into Message mode */
585 vx_send_rih_nolock(chip
, IRQ_PAUSE_START_CONNECT
);
586 vx_init_rmh(&rmh
, CMD_NOTIFY_END_OF_BUFFER
);
587 vx_set_stream_cmd_params(&rmh
, 0, pipe
->number
);
588 err
= vx_send_msg_nolock(chip
, &rmh
);
591 /* Toggle Dsp Host Interface back to sound transfer mode */
592 vx_send_rih_nolock(chip
, IRQ_PAUSE_START_CONNECT
);
597 * vx_pcm_playback_transfer_chunk - transfer a single chunk
599 * @pipe: the pipe to transfer
600 * @size: chunk size in bytes
602 * transfer a single buffer chunk. EOB notificaton is added after that.
603 * called from the interrupt handler, too.
607 static int vx_pcm_playback_transfer_chunk(struct vx_core
*chip
,
608 struct snd_pcm_runtime
*runtime
,
609 struct vx_pipe
*pipe
, int size
)
613 space
= vx_query_hbuffer_size(chip
, pipe
);
615 /* disconnect the host, SIZE_HBUF command always switches to the stream mode */
616 vx_send_rih(chip
, IRQ_CONNECT_STREAM_NEXT
);
617 snd_printd("error hbuffer\n");
621 vx_send_rih(chip
, IRQ_CONNECT_STREAM_NEXT
);
622 snd_printd("no enough hbuffer space %d\n", space
);
623 return -EIO
; /* XRUN */
626 /* we don't need irqsave here, because this function
627 * is called from either trigger callback or irq handler
629 mutex_lock(&chip
->lock
);
630 vx_pseudo_dma_write(chip
, runtime
, pipe
, size
);
631 err
= vx_notify_end_of_buffer(chip
, pipe
);
632 /* disconnect the host, SIZE_HBUF command always switches to the stream mode */
633 vx_send_rih_nolock(chip
, IRQ_CONNECT_STREAM_NEXT
);
634 mutex_unlock(&chip
->lock
);
639 * update the position of the given pipe.
640 * pipe->position is updated and wrapped within the buffer size.
641 * pipe->transferred is updated, too, but the size is not wrapped,
642 * so that the caller can check the total transferred size later
643 * (to call snd_pcm_period_elapsed).
645 static int vx_update_pipe_position(struct vx_core
*chip
,
646 struct snd_pcm_runtime
*runtime
,
647 struct vx_pipe
*pipe
)
653 vx_init_rmh(&rmh
, CMD_STREAM_SAMPLE_COUNT
);
654 vx_set_pipe_cmd_params(&rmh
, pipe
->is_capture
, pipe
->number
, 0);
655 err
= vx_send_msg(chip
, &rmh
);
659 count
= ((u64
)(rmh
.Stat
[0] & 0xfffff) << 24) | (u64
)rmh
.Stat
[1];
660 update
= (int)(count
- pipe
->cur_count
);
661 pipe
->cur_count
= count
;
662 pipe
->position
+= update
;
663 if (pipe
->position
>= (int)runtime
->buffer_size
)
664 pipe
->position
%= runtime
->buffer_size
;
665 pipe
->transferred
+= update
;
670 * transfer the pending playback buffer data to DSP
671 * called from interrupt handler
673 static void vx_pcm_playback_transfer(struct vx_core
*chip
,
674 struct snd_pcm_substream
*subs
,
675 struct vx_pipe
*pipe
, int nchunks
)
678 struct snd_pcm_runtime
*runtime
= subs
->runtime
;
680 if (! pipe
->prepared
|| (chip
->chip_status
& VX_STAT_IS_STALE
))
682 for (i
= 0; i
< nchunks
; i
++) {
683 if ((err
= vx_pcm_playback_transfer_chunk(chip
, runtime
, pipe
,
684 chip
->ibl
.size
)) < 0)
690 * update the playback position and call snd_pcm_period_elapsed() if necessary
691 * called from interrupt handler
693 static void vx_pcm_playback_update(struct vx_core
*chip
,
694 struct snd_pcm_substream
*subs
,
695 struct vx_pipe
*pipe
)
698 struct snd_pcm_runtime
*runtime
= subs
->runtime
;
700 if (pipe
->running
&& ! (chip
->chip_status
& VX_STAT_IS_STALE
)) {
701 if ((err
= vx_update_pipe_position(chip
, runtime
, pipe
)) < 0)
703 if (pipe
->transferred
>= (int)runtime
->period_size
) {
704 pipe
->transferred
%= runtime
->period_size
;
705 snd_pcm_period_elapsed(subs
);
711 * vx_pcm_playback_trigger - trigger callback for playback
713 static int vx_pcm_trigger(struct snd_pcm_substream
*subs
, int cmd
)
715 struct vx_core
*chip
= snd_pcm_substream_chip(subs
);
716 struct vx_pipe
*pipe
= subs
->runtime
->private_data
;
719 if (chip
->chip_status
& VX_STAT_IS_STALE
)
723 case SNDRV_PCM_TRIGGER_START
:
724 case SNDRV_PCM_TRIGGER_RESUME
:
725 if (! pipe
->is_capture
)
726 vx_pcm_playback_transfer(chip
, subs
, pipe
, 2);
727 err
= vx_start_stream(chip
, pipe
);
729 pr_debug("vx: cannot start stream\n");
732 err
= vx_toggle_pipe(chip
, pipe
, 1);
734 pr_debug("vx: cannot start pipe\n");
735 vx_stop_stream(chip
, pipe
);
741 case SNDRV_PCM_TRIGGER_STOP
:
742 case SNDRV_PCM_TRIGGER_SUSPEND
:
743 vx_toggle_pipe(chip
, pipe
, 0);
744 vx_stop_pipe(chip
, pipe
);
745 vx_stop_stream(chip
, pipe
);
749 case SNDRV_PCM_TRIGGER_PAUSE_PUSH
:
750 if ((err
= vx_toggle_pipe(chip
, pipe
, 0)) < 0)
753 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE
:
754 if ((err
= vx_toggle_pipe(chip
, pipe
, 1)) < 0)
764 * vx_pcm_playback_pointer - pointer callback for playback
766 static snd_pcm_uframes_t
vx_pcm_playback_pointer(struct snd_pcm_substream
*subs
)
768 struct snd_pcm_runtime
*runtime
= subs
->runtime
;
769 struct vx_pipe
*pipe
= runtime
->private_data
;
770 return pipe
->position
;
774 * vx_pcm_prepare - prepare callback for playback and capture
776 static int vx_pcm_prepare(struct snd_pcm_substream
*subs
)
778 struct vx_core
*chip
= snd_pcm_substream_chip(subs
);
779 struct snd_pcm_runtime
*runtime
= subs
->runtime
;
780 struct vx_pipe
*pipe
= runtime
->private_data
;
782 // int max_size, nchunks;
784 if (chip
->chip_status
& VX_STAT_IS_STALE
)
787 data_mode
= (chip
->uer_bits
& IEC958_AES0_NONAUDIO
) != 0;
788 if (data_mode
!= pipe
->data_mode
&& ! pipe
->is_capture
) {
789 /* IEC958 status (raw-mode) was changed */
790 /* we reopen the pipe */
792 snd_printdd(KERN_DEBUG
"reopen the pipe with data_mode = %d\n", data_mode
);
793 vx_init_rmh(&rmh
, CMD_FREE_PIPE
);
794 vx_set_pipe_cmd_params(&rmh
, 0, pipe
->number
, 0);
795 if ((err
= vx_send_msg(chip
, &rmh
)) < 0)
797 vx_init_rmh(&rmh
, CMD_RES_PIPE
);
798 vx_set_pipe_cmd_params(&rmh
, 0, pipe
->number
, pipe
->channels
);
800 rmh
.Cmd
[0] |= BIT_DATA_MODE
;
801 if ((err
= vx_send_msg(chip
, &rmh
)) < 0)
803 pipe
->data_mode
= data_mode
;
806 if (chip
->pcm_running
&& chip
->freq
!= runtime
->rate
) {
807 snd_printk(KERN_ERR
"vx: cannot set different clock %d "
808 "from the current %d\n", runtime
->rate
, chip
->freq
);
811 vx_set_clock(chip
, runtime
->rate
);
813 if ((err
= vx_set_format(chip
, pipe
, runtime
)) < 0)
816 if (vx_is_pcmcia(chip
)) {
817 pipe
->align
= 2; /* 16bit word */
819 pipe
->align
= 4; /* 32bit word */
822 pipe
->buffer_bytes
= frames_to_bytes(runtime
, runtime
->buffer_size
);
823 pipe
->period_bytes
= frames_to_bytes(runtime
, runtime
->period_size
);
826 /* set the timestamp */
827 vx_update_pipe_position(chip
, runtime
, pipe
);
829 pipe
->transferred
= 0;
839 * operators for PCM playback
841 static const struct snd_pcm_ops vx_pcm_playback_ops
= {
842 .open
= vx_pcm_playback_open
,
843 .close
= vx_pcm_playback_close
,
844 .prepare
= vx_pcm_prepare
,
845 .trigger
= vx_pcm_trigger
,
846 .pointer
= vx_pcm_playback_pointer
,
851 * playback hw information
854 static const struct snd_pcm_hardware vx_pcm_capture_hw
= {
855 .info
= (SNDRV_PCM_INFO_MMAP
| SNDRV_PCM_INFO_INTERLEAVED
|
856 SNDRV_PCM_INFO_PAUSE
| SNDRV_PCM_INFO_MMAP_VALID
/*|*/
857 /*SNDRV_PCM_INFO_RESUME*/),
858 .formats
= (/*SNDRV_PCM_FMTBIT_U8 |*/
859 SNDRV_PCM_FMTBIT_S16_LE
| SNDRV_PCM_FMTBIT_S24_3LE
),
860 .rates
= SNDRV_PCM_RATE_CONTINUOUS
| SNDRV_PCM_RATE_8000_48000
,
865 .buffer_bytes_max
= (128*1024),
866 .period_bytes_min
= 126,
867 .period_bytes_max
= (128*1024),
869 .periods_max
= VX_MAX_PERIODS
,
875 * vx_pcm_capture_open - open callback for capture
877 static int vx_pcm_capture_open(struct snd_pcm_substream
*subs
)
879 struct snd_pcm_runtime
*runtime
= subs
->runtime
;
880 struct vx_core
*chip
= snd_pcm_substream_chip(subs
);
881 struct vx_pipe
*pipe
;
882 struct vx_pipe
*pipe_out_monitoring
= NULL
;
886 if (chip
->chip_status
& VX_STAT_IS_STALE
)
889 audio
= subs
->pcm
->device
* 2;
890 if (snd_BUG_ON(audio
>= chip
->audio_ins
))
892 err
= vx_alloc_pipe(chip
, 1, audio
, 2, &pipe
);
895 pipe
->substream
= subs
;
896 chip
->capture_pipes
[audio
] = pipe
;
898 /* check if monitoring is needed */
899 if (chip
->audio_monitor_active
[audio
]) {
900 pipe_out_monitoring
= chip
->playback_pipes
[audio
];
901 if (! pipe_out_monitoring
) {
902 /* allocate a pipe */
903 err
= vx_alloc_pipe(chip
, 0, audio
, 2, &pipe_out_monitoring
);
906 chip
->playback_pipes
[audio
] = pipe_out_monitoring
;
908 pipe_out_monitoring
->references
++;
910 if an output pipe is available, it's audios still may need to be
911 unmuted. hence we'll have to call a mixer entry point.
913 vx_set_monitor_level(chip
, audio
, chip
->audio_monitor
[audio
],
914 chip
->audio_monitor_active
[audio
]);
915 /* assuming stereo */
916 vx_set_monitor_level(chip
, audio
+1, chip
->audio_monitor
[audio
+1],
917 chip
->audio_monitor_active
[audio
+1]);
920 pipe
->monitoring_pipe
= pipe_out_monitoring
; /* default value NULL */
922 runtime
->hw
= vx_pcm_capture_hw
;
923 runtime
->hw
.period_bytes_min
= chip
->ibl
.size
;
924 runtime
->private_data
= pipe
;
926 /* align to 4 bytes (otherwise will be problematic when 24bit is used) */
927 snd_pcm_hw_constraint_step(runtime
, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES
, 4);
928 snd_pcm_hw_constraint_step(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES
, 4);
934 * vx_pcm_capture_close - close callback for capture
936 static int vx_pcm_capture_close(struct snd_pcm_substream
*subs
)
938 struct vx_core
*chip
= snd_pcm_substream_chip(subs
);
939 struct vx_pipe
*pipe
;
940 struct vx_pipe
*pipe_out_monitoring
;
942 if (! subs
->runtime
->private_data
)
944 pipe
= subs
->runtime
->private_data
;
945 chip
->capture_pipes
[pipe
->number
] = NULL
;
947 pipe_out_monitoring
= pipe
->monitoring_pipe
;
950 if an output pipe is attached to this input,
951 check if it needs to be released.
953 if (pipe_out_monitoring
) {
954 if (--pipe_out_monitoring
->references
== 0) {
955 vx_free_pipe(chip
, pipe_out_monitoring
);
956 chip
->playback_pipes
[pipe
->number
] = NULL
;
957 pipe
->monitoring_pipe
= NULL
;
961 vx_free_pipe(chip
, pipe
);
967 #define DMA_READ_ALIGN 6 /* hardware alignment for read */
970 * vx_pcm_capture_update - update the capture buffer
972 static void vx_pcm_capture_update(struct vx_core
*chip
, struct snd_pcm_substream
*subs
,
973 struct vx_pipe
*pipe
)
975 int size
, space
, count
;
976 struct snd_pcm_runtime
*runtime
= subs
->runtime
;
978 if (!pipe
->running
|| (chip
->chip_status
& VX_STAT_IS_STALE
))
981 size
= runtime
->buffer_size
- snd_pcm_capture_avail(runtime
);
984 size
= frames_to_bytes(runtime
, size
);
985 space
= vx_query_hbuffer_size(chip
, pipe
);
990 size
= (size
/ 3) * 3; /* align to 3 bytes */
991 if (size
< DMA_READ_ALIGN
)
994 /* keep the last 6 bytes, they will be read after disconnection */
995 count
= size
- DMA_READ_ALIGN
;
996 /* read bytes until the current pointer reaches to the aligned position
1000 if ((pipe
->hw_ptr
% pipe
->align
) == 0)
1002 if (vx_wait_for_rx_full(chip
) < 0)
1004 vx_pcm_read_per_bytes(chip
, runtime
, pipe
);
1008 /* ok, let's accelerate! */
1009 int align
= pipe
->align
* 3;
1010 space
= (count
/ align
) * align
;
1012 vx_pseudo_dma_read(chip
, runtime
, pipe
, space
);
1016 /* read the rest of bytes */
1018 if (vx_wait_for_rx_full(chip
) < 0)
1020 vx_pcm_read_per_bytes(chip
, runtime
, pipe
);
1023 /* disconnect the host, SIZE_HBUF command always switches to the stream mode */
1024 vx_send_rih(chip
, IRQ_CONNECT_STREAM_NEXT
);
1025 /* read the last pending 6 bytes */
1026 count
= DMA_READ_ALIGN
;
1028 vx_pcm_read_per_bytes(chip
, runtime
, pipe
);
1031 /* update the position */
1032 pipe
->transferred
+= size
;
1033 if (pipe
->transferred
>= pipe
->period_bytes
) {
1034 pipe
->transferred
%= pipe
->period_bytes
;
1035 snd_pcm_period_elapsed(subs
);
1040 /* disconnect the host, SIZE_HBUF command always switches to the stream mode */
1041 vx_send_rih(chip
, IRQ_CONNECT_STREAM_NEXT
);
1046 * vx_pcm_capture_pointer - pointer callback for capture
1048 static snd_pcm_uframes_t
vx_pcm_capture_pointer(struct snd_pcm_substream
*subs
)
1050 struct snd_pcm_runtime
*runtime
= subs
->runtime
;
1051 struct vx_pipe
*pipe
= runtime
->private_data
;
1052 return bytes_to_frames(runtime
, pipe
->hw_ptr
);
1056 * operators for PCM capture
1058 static const struct snd_pcm_ops vx_pcm_capture_ops
= {
1059 .open
= vx_pcm_capture_open
,
1060 .close
= vx_pcm_capture_close
,
1061 .prepare
= vx_pcm_prepare
,
1062 .trigger
= vx_pcm_trigger
,
1063 .pointer
= vx_pcm_capture_pointer
,
1068 * interrupt handler for pcm streams
1070 void vx_pcm_update_intr(struct vx_core
*chip
, unsigned int events
)
1073 struct vx_pipe
*pipe
;
1075 #define EVENT_MASK (END_OF_BUFFER_EVENTS_PENDING|ASYNC_EVENTS_PENDING)
1077 if (events
& EVENT_MASK
) {
1078 vx_init_rmh(&chip
->irq_rmh
, CMD_ASYNC
);
1079 if (events
& ASYNC_EVENTS_PENDING
)
1080 chip
->irq_rmh
.Cmd
[0] |= 0x00000001; /* SEL_ASYNC_EVENTS */
1081 if (events
& END_OF_BUFFER_EVENTS_PENDING
)
1082 chip
->irq_rmh
.Cmd
[0] |= 0x00000002; /* SEL_END_OF_BUF_EVENTS */
1084 if (vx_send_msg(chip
, &chip
->irq_rmh
) < 0) {
1085 snd_printdd(KERN_ERR
"msg send error!!\n");
1090 while (i
< chip
->irq_rmh
.LgStat
) {
1091 int p
, buf
, capture
, eob
;
1092 p
= chip
->irq_rmh
.Stat
[i
] & MASK_FIRST_FIELD
;
1093 capture
= (chip
->irq_rmh
.Stat
[i
] & 0x400000) ? 1 : 0;
1094 eob
= (chip
->irq_rmh
.Stat
[i
] & 0x800000) ? 1 : 0;
1096 if (events
& ASYNC_EVENTS_PENDING
)
1098 buf
= 1; /* force to transfer */
1099 if (events
& END_OF_BUFFER_EVENTS_PENDING
) {
1101 buf
= chip
->irq_rmh
.Stat
[i
];
1106 if (snd_BUG_ON(p
< 0 || p
>= chip
->audio_outs
))
1108 pipe
= chip
->playback_pipes
[p
];
1109 if (pipe
&& pipe
->substream
) {
1110 vx_pcm_playback_update(chip
, pipe
->substream
, pipe
);
1111 vx_pcm_playback_transfer(chip
, pipe
->substream
, pipe
, buf
);
1116 /* update the capture pcm pointers as frequently as possible */
1117 for (i
= 0; i
< chip
->audio_ins
; i
++) {
1118 pipe
= chip
->capture_pipes
[i
];
1119 if (pipe
&& pipe
->substream
)
1120 vx_pcm_capture_update(chip
, pipe
->substream
, pipe
);
1126 * vx_init_audio_io - check the available audio i/o and allocate pipe arrays
1128 static int vx_init_audio_io(struct vx_core
*chip
)
1133 vx_init_rmh(&rmh
, CMD_SUPPORTED
);
1134 if (vx_send_msg(chip
, &rmh
) < 0) {
1135 snd_printk(KERN_ERR
"vx: cannot get the supported audio data\n");
1139 chip
->audio_outs
= rmh
.Stat
[0] & MASK_FIRST_FIELD
;
1140 chip
->audio_ins
= (rmh
.Stat
[0] >> (FIELD_SIZE
*2)) & MASK_FIRST_FIELD
;
1141 chip
->audio_info
= rmh
.Stat
[1];
1143 /* allocate pipes */
1144 chip
->playback_pipes
= kcalloc(chip
->audio_outs
, sizeof(struct vx_pipe
*), GFP_KERNEL
);
1145 if (!chip
->playback_pipes
)
1147 chip
->capture_pipes
= kcalloc(chip
->audio_ins
, sizeof(struct vx_pipe
*), GFP_KERNEL
);
1148 if (!chip
->capture_pipes
) {
1149 kfree(chip
->playback_pipes
);
1153 preferred
= chip
->ibl
.size
;
1155 vx_set_ibl(chip
, &chip
->ibl
); /* query the info */
1156 if (preferred
> 0) {
1157 chip
->ibl
.size
= ((preferred
+ chip
->ibl
.granularity
- 1) /
1158 chip
->ibl
.granularity
) * chip
->ibl
.granularity
;
1159 if (chip
->ibl
.size
> chip
->ibl
.max_size
)
1160 chip
->ibl
.size
= chip
->ibl
.max_size
;
1162 chip
->ibl
.size
= chip
->ibl
.min_size
; /* set to the minimum */
1163 vx_set_ibl(chip
, &chip
->ibl
);
1170 * free callback for pcm
1172 static void snd_vx_pcm_free(struct snd_pcm
*pcm
)
1174 struct vx_core
*chip
= pcm
->private_data
;
1175 chip
->pcm
[pcm
->device
] = NULL
;
1176 kfree(chip
->playback_pipes
);
1177 chip
->playback_pipes
= NULL
;
1178 kfree(chip
->capture_pipes
);
1179 chip
->capture_pipes
= NULL
;
1183 * snd_vx_pcm_new - create and initialize a pcm
1185 int snd_vx_pcm_new(struct vx_core
*chip
)
1187 struct snd_pcm
*pcm
;
1191 if ((err
= vx_init_audio_io(chip
)) < 0)
1194 for (i
= 0; i
< chip
->hw
->num_codecs
; i
++) {
1195 unsigned int outs
, ins
;
1196 outs
= chip
->audio_outs
> i
* 2 ? 1 : 0;
1197 ins
= chip
->audio_ins
> i
* 2 ? 1 : 0;
1198 if (! outs
&& ! ins
)
1200 err
= snd_pcm_new(chip
->card
, "VX PCM", i
,
1205 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
, &vx_pcm_playback_ops
);
1207 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &vx_pcm_capture_ops
);
1208 snd_pcm_set_managed_buffer_all(pcm
, SNDRV_DMA_TYPE_VMALLOC
,
1209 snd_dma_continuous_data(GFP_KERNEL
| GFP_DMA32
),
1212 pcm
->private_data
= chip
;
1213 pcm
->private_free
= snd_vx_pcm_free
;
1214 pcm
->info_flags
= 0;
1215 pcm
->nonatomic
= true;
1216 strcpy(pcm
->name
, chip
->card
->shortname
);