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) {
63 buf = (unsigned char *)runtime->dma_area;
65 pipe->hw_ptr = offset;
69 * vx_set_pcx_time - convert from the PC time to the RMH status time.
70 * @pc_time: the pointer for the PC-time to set
71 * @dsp_time: the pointer for RMH status time array
73 static void vx_set_pcx_time(struct vx_core *chip, pcx_time_t *pc_time,
74 unsigned int *dsp_time)
76 dsp_time[0] = (unsigned int)((*pc_time) >> 24) & PCX_TIME_HI_MASK;
77 dsp_time[1] = (unsigned int)(*pc_time) & MASK_DSP_WORD;
81 * vx_set_differed_time - set the differed time if specified
82 * @rmh: the rmh record to modify
83 * @pipe: the pipe to be checked
85 * if the pipe is programmed with the differed time, set the DSP time
86 * on the rmh and changes its command length.
88 * returns the increase of the command length.
90 static int vx_set_differed_time(struct vx_core *chip, struct vx_rmh *rmh,
93 /* Update The length added to the RMH command by the timestamp */
94 if (! (pipe->differed_type & DC_DIFFERED_DELAY))
98 rmh->Cmd[0] |= DSP_DIFFERED_COMMAND_MASK;
100 /* Time stamp is the 1st following parameter */
101 vx_set_pcx_time(chip, &pipe->pcx_time, &rmh->Cmd[1]);
103 /* Add the flags to a notified differed command */
104 if (pipe->differed_type & DC_NOTIFY_DELAY)
105 rmh->Cmd[1] |= NOTIFY_MASK_TIME_HIGH ;
107 /* Add the flags to a multiple differed command */
108 if (pipe->differed_type & DC_MULTIPLE_DELAY)
109 rmh->Cmd[1] |= MULTIPLE_MASK_TIME_HIGH;
111 /* Add the flags to a stream-time differed command */
112 if (pipe->differed_type & DC_STREAM_TIME_DELAY)
113 rmh->Cmd[1] |= STREAM_MASK_TIME_HIGH;
120 * vx_set_stream_format - send the stream format command
121 * @pipe: the affected pipe
122 * @data: format bitmask
124 static int vx_set_stream_format(struct vx_core *chip, struct vx_pipe *pipe,
129 vx_init_rmh(&rmh, pipe->is_capture ?
130 CMD_FORMAT_STREAM_IN : CMD_FORMAT_STREAM_OUT);
131 rmh.Cmd[0] |= pipe->number << FIELD_SIZE;
133 /* Command might be longer since we may have to add a timestamp */
134 vx_set_differed_time(chip, &rmh, pipe);
136 rmh.Cmd[rmh.LgCmd] = (data & 0xFFFFFF00) >> 8;
137 rmh.Cmd[rmh.LgCmd + 1] = (data & 0xFF) << 16 /*| (datal & 0xFFFF00) >> 8*/;
140 return vx_send_msg(chip, &rmh);
145 * vx_set_format - set the format of a pipe
146 * @pipe: the affected pipe
147 * @runtime: pcm runtime instance to be referred
149 * returns 0 if successful, or a negative error code.
151 static int vx_set_format(struct vx_core *chip, struct vx_pipe *pipe,
152 struct snd_pcm_runtime *runtime)
154 unsigned int header = HEADER_FMT_BASE;
156 if (runtime->channels == 1)
157 header |= HEADER_FMT_MONO;
158 if (snd_pcm_format_little_endian(runtime->format))
159 header |= HEADER_FMT_INTEL;
160 if (runtime->rate < 32000 && runtime->rate > 11025)
161 header |= HEADER_FMT_UPTO32;
162 else if (runtime->rate <= 11025)
163 header |= HEADER_FMT_UPTO11;
165 switch (snd_pcm_format_physical_width(runtime->format)) {
167 case 16: header |= HEADER_FMT_16BITS; break;
168 case 24: header |= HEADER_FMT_24BITS; break;
174 return vx_set_stream_format(chip, pipe, header);
178 * set / query the IBL size
180 static int vx_set_ibl(struct vx_core *chip, struct vx_ibl_info *info)
185 vx_init_rmh(&rmh, CMD_IBL);
186 rmh.Cmd[0] |= info->size & 0x03ffff;
187 err = vx_send_msg(chip, &rmh);
190 info->size = rmh.Stat[0];
191 info->max_size = rmh.Stat[1];
192 info->min_size = rmh.Stat[2];
193 info->granularity = rmh.Stat[3];
194 snd_printdd(KERN_DEBUG "vx_set_ibl: size = %d, max = %d, min = %d, gran = %d\n",
195 info->size, info->max_size, info->min_size, info->granularity);
201 * vx_get_pipe_state - get the state of a pipe
202 * @pipe: the pipe to be checked
203 * @state: the pointer for the returned state
205 * checks the state of a given pipe, and stores the state (1 = running,
206 * 0 = paused) on the given pointer.
208 * called from trigger callback only
210 static int vx_get_pipe_state(struct vx_core *chip, struct vx_pipe *pipe, int *state)
215 vx_init_rmh(&rmh, CMD_PIPE_STATE);
216 vx_set_pipe_cmd_params(&rmh, pipe->is_capture, pipe->number, 0);
217 err = vx_send_msg(chip, &rmh);
219 *state = (rmh.Stat[0] & (1 << pipe->number)) ? 1 : 0;
225 * vx_query_hbuffer_size - query available h-buffer size in bytes
226 * @pipe: the pipe to be checked
228 * return the available size on h-buffer in bytes,
229 * or a negative error code.
231 * NOTE: calling this function always switches to the stream mode.
232 * you'll need to disconnect the host to get back to the
235 static int vx_query_hbuffer_size(struct vx_core *chip, struct vx_pipe *pipe)
240 vx_init_rmh(&rmh, CMD_SIZE_HBUFFER);
241 vx_set_pipe_cmd_params(&rmh, pipe->is_capture, pipe->number, 0);
242 if (pipe->is_capture)
243 rmh.Cmd[0] |= 0x00000001;
244 result = vx_send_msg(chip, &rmh);
246 result = rmh.Stat[0] & 0xffff;
252 * vx_pipe_can_start - query whether a pipe is ready for start
253 * @pipe: the pipe to be checked
255 * return 1 if ready, 0 if not ready, and negative value on error.
257 * called from trigger callback only
259 static int vx_pipe_can_start(struct vx_core *chip, struct vx_pipe *pipe)
264 vx_init_rmh(&rmh, CMD_CAN_START_PIPE);
265 vx_set_pipe_cmd_params(&rmh, pipe->is_capture, pipe->number, 0);
268 err = vx_send_msg(chip, &rmh);
277 * vx_conf_pipe - tell the pipe to stand by and wait for IRQA.
278 * @pipe: the pipe to be configured
280 static int vx_conf_pipe(struct vx_core *chip, struct vx_pipe *pipe)
284 vx_init_rmh(&rmh, CMD_CONF_PIPE);
285 if (pipe->is_capture)
286 rmh.Cmd[0] |= COMMAND_RECORD_MASK;
287 rmh.Cmd[1] = 1 << pipe->number;
288 return vx_send_msg(chip, &rmh);
292 * vx_send_irqa - trigger IRQA
294 static int vx_send_irqa(struct vx_core *chip)
298 vx_init_rmh(&rmh, CMD_SEND_IRQA);
299 return vx_send_msg(chip, &rmh);
303 #define MAX_WAIT_FOR_DSP 250
305 * vx boards do not support inter-card sync, besides
306 * only 126 samples require to be prepared before a pipe can start
308 #define CAN_START_DELAY 2 /* wait 2ms only before asking if the pipe is ready*/
309 #define WAIT_STATE_DELAY 2 /* wait 2ms after irqA was requested and check if the pipe state toggled*/
312 * vx_toggle_pipe - start / pause a pipe
313 * @pipe: the pipe to be triggered
314 * @state: start = 1, pause = 0
316 * called from trigger callback only
319 static int vx_toggle_pipe(struct vx_core *chip, struct vx_pipe *pipe, int state)
321 int err, i, cur_state;
323 /* Check the pipe is not already in the requested state */
324 if (vx_get_pipe_state(chip, pipe, &cur_state) < 0)
326 if (state == cur_state)
329 /* If a start is requested, ask the DSP to get prepared
330 * and wait for a positive acknowledge (when there are
331 * enough sound buffer for this pipe)
334 for (i = 0 ; i < MAX_WAIT_FOR_DSP; i++) {
335 err = vx_pipe_can_start(chip, pipe);
338 /* Wait for a few, before asking again
339 * to avoid flooding the DSP with our requests
345 if ((err = vx_conf_pipe(chip, pipe)) < 0)
348 if ((err = vx_send_irqa(chip)) < 0)
351 /* If it completes successfully, wait for the pipes
352 * reaching the expected state before returning
353 * Check one pipe only (since they are synchronous)
355 for (i = 0; i < MAX_WAIT_FOR_DSP; i++) {
356 err = vx_get_pipe_state(chip, pipe, &cur_state);
357 if (err < 0 || cur_state == state)
362 return err < 0 ? -EIO : 0;
367 * vx_stop_pipe - stop a pipe
368 * @pipe: the pipe to be stopped
370 * called from trigger callback only
372 static int vx_stop_pipe(struct vx_core *chip, struct vx_pipe *pipe)
375 vx_init_rmh(&rmh, CMD_STOP_PIPE);
376 vx_set_pipe_cmd_params(&rmh, pipe->is_capture, pipe->number, 0);
377 return vx_send_msg(chip, &rmh);
382 * vx_alloc_pipe - allocate a pipe and initialize the pipe instance
383 * @capture: 0 = playback, 1 = capture operation
384 * @audioid: the audio id to be assigned
385 * @num_audio: number of audio channels
386 * @pipep: the returned pipe instance
388 * return 0 on success, or a negative error code.
390 static int vx_alloc_pipe(struct vx_core *chip, int capture,
391 int audioid, int num_audio,
392 struct vx_pipe **pipep)
395 struct vx_pipe *pipe;
400 vx_init_rmh(&rmh, CMD_RES_PIPE);
401 vx_set_pipe_cmd_params(&rmh, capture, audioid, num_audio);
403 if (underrun_skip_sound)
404 rmh.Cmd[0] |= BIT_SKIP_SOUND;
406 data_mode = (chip->uer_bits & IEC958_AES0_NONAUDIO) != 0;
407 if (! capture && data_mode)
408 rmh.Cmd[0] |= BIT_DATA_MODE;
409 err = vx_send_msg(chip, &rmh);
413 /* initialize the pipe record */
414 pipe = kzalloc(sizeof(*pipe), GFP_KERNEL);
416 /* release the pipe */
417 vx_init_rmh(&rmh, CMD_FREE_PIPE);
418 vx_set_pipe_cmd_params(&rmh, capture, audioid, 0);
419 vx_send_msg(chip, &rmh);
423 /* the pipe index should be identical with the audio index */
424 pipe->number = audioid;
425 pipe->is_capture = capture;
426 pipe->channels = num_audio;
427 pipe->differed_type = 0;
429 pipe->data_mode = data_mode;
437 * vx_free_pipe - release a pipe
438 * @pipe: pipe to be released
440 static int vx_free_pipe(struct vx_core *chip, struct vx_pipe *pipe)
444 vx_init_rmh(&rmh, CMD_FREE_PIPE);
445 vx_set_pipe_cmd_params(&rmh, pipe->is_capture, pipe->number, 0);
446 vx_send_msg(chip, &rmh);
454 * vx_start_stream - start the stream
456 * called from trigger callback only
458 static int vx_start_stream(struct vx_core *chip, struct vx_pipe *pipe)
462 vx_init_rmh(&rmh, CMD_START_ONE_STREAM);
463 vx_set_stream_cmd_params(&rmh, pipe->is_capture, pipe->number);
464 vx_set_differed_time(chip, &rmh, pipe);
465 return vx_send_msg(chip, &rmh);
470 * vx_stop_stream - stop the stream
472 * called from trigger callback only
474 static int vx_stop_stream(struct vx_core *chip, struct vx_pipe *pipe)
478 vx_init_rmh(&rmh, CMD_STOP_STREAM);
479 vx_set_stream_cmd_params(&rmh, pipe->is_capture, pipe->number);
480 return vx_send_msg(chip, &rmh);
485 * playback hw information
488 static const struct snd_pcm_hardware vx_pcm_playback_hw = {
489 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
490 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_MMAP_VALID /*|*/
491 /*SNDRV_PCM_INFO_RESUME*/),
492 .formats = (/*SNDRV_PCM_FMTBIT_U8 |*/
493 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_3LE),
494 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
499 .buffer_bytes_max = (128*1024),
500 .period_bytes_min = 126,
501 .period_bytes_max = (128*1024),
503 .periods_max = VX_MAX_PERIODS,
509 * vx_pcm_playback_open - open callback for playback
511 static int vx_pcm_playback_open(struct snd_pcm_substream *subs)
513 struct snd_pcm_runtime *runtime = subs->runtime;
514 struct vx_core *chip = snd_pcm_substream_chip(subs);
515 struct vx_pipe *pipe = NULL;
519 if (chip->chip_status & VX_STAT_IS_STALE)
522 audio = subs->pcm->device * 2;
523 if (snd_BUG_ON(audio >= chip->audio_outs))
526 /* playback pipe may have been already allocated for monitoring */
527 pipe = chip->playback_pipes[audio];
529 /* not allocated yet */
530 err = vx_alloc_pipe(chip, 0, audio, 2, &pipe); /* stereo playback */
533 chip->playback_pipes[audio] = pipe;
535 /* open for playback */
538 pipe->substream = subs;
539 chip->playback_pipes[audio] = pipe;
541 runtime->hw = vx_pcm_playback_hw;
542 runtime->hw.period_bytes_min = chip->ibl.size;
543 runtime->private_data = pipe;
545 /* align to 4 bytes (otherwise will be problematic when 24bit is used) */
546 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 4);
547 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 4);
553 * vx_pcm_playback_close - close callback for playback
555 static int vx_pcm_playback_close(struct snd_pcm_substream *subs)
557 struct vx_core *chip = snd_pcm_substream_chip(subs);
558 struct vx_pipe *pipe;
560 if (! subs->runtime->private_data)
563 pipe = subs->runtime->private_data;
565 if (--pipe->references == 0) {
566 chip->playback_pipes[pipe->number] = NULL;
567 vx_free_pipe(chip, pipe);
576 * vx_notify_end_of_buffer - send "end-of-buffer" notifier at the given pipe
577 * @pipe: the pipe to notify
579 * NB: call with a certain lock.
581 static int vx_notify_end_of_buffer(struct vx_core *chip, struct vx_pipe *pipe)
584 struct vx_rmh rmh; /* use a temporary rmh here */
586 /* Toggle Dsp Host Interface into Message mode */
587 vx_send_rih_nolock(chip, IRQ_PAUSE_START_CONNECT);
588 vx_init_rmh(&rmh, CMD_NOTIFY_END_OF_BUFFER);
589 vx_set_stream_cmd_params(&rmh, 0, pipe->number);
590 err = vx_send_msg_nolock(chip, &rmh);
593 /* Toggle Dsp Host Interface back to sound transfer mode */
594 vx_send_rih_nolock(chip, IRQ_PAUSE_START_CONNECT);
599 * vx_pcm_playback_transfer_chunk - transfer a single chunk
601 * @pipe: the pipe to transfer
602 * @size: chunk size in bytes
604 * transfer a single buffer chunk. EOB notificaton is added after that.
605 * called from the interrupt handler, too.
609 static int vx_pcm_playback_transfer_chunk(struct vx_core *chip,
610 struct snd_pcm_runtime *runtime,
611 struct vx_pipe *pipe, int size)
615 space = vx_query_hbuffer_size(chip, pipe);
617 /* disconnect the host, SIZE_HBUF command always switches to the stream mode */
618 vx_send_rih(chip, IRQ_CONNECT_STREAM_NEXT);
619 snd_printd("error hbuffer\n");
623 vx_send_rih(chip, IRQ_CONNECT_STREAM_NEXT);
624 snd_printd("no enough hbuffer space %d\n", space);
625 return -EIO; /* XRUN */
628 /* we don't need irqsave here, because this function
629 * is called from either trigger callback or irq handler
631 mutex_lock(&chip->lock);
632 vx_pseudo_dma_write(chip, runtime, pipe, size);
633 err = vx_notify_end_of_buffer(chip, pipe);
634 /* disconnect the host, SIZE_HBUF command always switches to the stream mode */
635 vx_send_rih_nolock(chip, IRQ_CONNECT_STREAM_NEXT);
636 mutex_unlock(&chip->lock);
641 * update the position of the given pipe.
642 * pipe->position is updated and wrapped within the buffer size.
643 * pipe->transferred is updated, too, but the size is not wrapped,
644 * so that the caller can check the total transferred size later
645 * (to call snd_pcm_period_elapsed).
647 static int vx_update_pipe_position(struct vx_core *chip,
648 struct snd_pcm_runtime *runtime,
649 struct vx_pipe *pipe)
655 vx_init_rmh(&rmh, CMD_STREAM_SAMPLE_COUNT);
656 vx_set_pipe_cmd_params(&rmh, pipe->is_capture, pipe->number, 0);
657 err = vx_send_msg(chip, &rmh);
661 count = ((u64)(rmh.Stat[0] & 0xfffff) << 24) | (u64)rmh.Stat[1];
662 update = (int)(count - pipe->cur_count);
663 pipe->cur_count = count;
664 pipe->position += update;
665 if (pipe->position >= (int)runtime->buffer_size)
666 pipe->position %= runtime->buffer_size;
667 pipe->transferred += update;
672 * transfer the pending playback buffer data to DSP
673 * called from interrupt handler
675 static void vx_pcm_playback_transfer(struct vx_core *chip,
676 struct snd_pcm_substream *subs,
677 struct vx_pipe *pipe, int nchunks)
680 struct snd_pcm_runtime *runtime = subs->runtime;
682 if (! pipe->prepared || (chip->chip_status & VX_STAT_IS_STALE))
684 for (i = 0; i < nchunks; i++) {
685 if ((err = vx_pcm_playback_transfer_chunk(chip, runtime, pipe,
686 chip->ibl.size)) < 0)
692 * update the playback position and call snd_pcm_period_elapsed() if necessary
693 * called from interrupt handler
695 static void vx_pcm_playback_update(struct vx_core *chip,
696 struct snd_pcm_substream *subs,
697 struct vx_pipe *pipe)
700 struct snd_pcm_runtime *runtime = subs->runtime;
702 if (pipe->running && ! (chip->chip_status & VX_STAT_IS_STALE)) {
703 if ((err = vx_update_pipe_position(chip, runtime, pipe)) < 0)
705 if (pipe->transferred >= (int)runtime->period_size) {
706 pipe->transferred %= runtime->period_size;
707 snd_pcm_period_elapsed(subs);
713 * vx_pcm_playback_trigger - trigger callback for playback
715 static int vx_pcm_trigger(struct snd_pcm_substream *subs, int cmd)
717 struct vx_core *chip = snd_pcm_substream_chip(subs);
718 struct vx_pipe *pipe = subs->runtime->private_data;
721 if (chip->chip_status & VX_STAT_IS_STALE)
725 case SNDRV_PCM_TRIGGER_START:
726 case SNDRV_PCM_TRIGGER_RESUME:
727 if (! pipe->is_capture)
728 vx_pcm_playback_transfer(chip, subs, pipe, 2);
729 err = vx_start_stream(chip, pipe);
731 pr_debug("vx: cannot start stream\n");
734 err = vx_toggle_pipe(chip, pipe, 1);
736 pr_debug("vx: cannot start pipe\n");
737 vx_stop_stream(chip, pipe);
743 case SNDRV_PCM_TRIGGER_STOP:
744 case SNDRV_PCM_TRIGGER_SUSPEND:
745 vx_toggle_pipe(chip, pipe, 0);
746 vx_stop_pipe(chip, pipe);
747 vx_stop_stream(chip, pipe);
751 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
752 if ((err = vx_toggle_pipe(chip, pipe, 0)) < 0)
755 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
756 if ((err = vx_toggle_pipe(chip, pipe, 1)) < 0)
766 * vx_pcm_playback_pointer - pointer callback for playback
768 static snd_pcm_uframes_t vx_pcm_playback_pointer(struct snd_pcm_substream *subs)
770 struct snd_pcm_runtime *runtime = subs->runtime;
771 struct vx_pipe *pipe = runtime->private_data;
772 return pipe->position;
776 * vx_pcm_hw_params - hw_params callback for playback and capture
778 static int vx_pcm_hw_params(struct snd_pcm_substream *subs,
779 struct snd_pcm_hw_params *hw_params)
781 return snd_pcm_lib_alloc_vmalloc_32_buffer
782 (subs, params_buffer_bytes(hw_params));
786 * vx_pcm_hw_free - hw_free callback for playback and capture
788 static int vx_pcm_hw_free(struct snd_pcm_substream *subs)
790 return snd_pcm_lib_free_vmalloc_buffer(subs);
794 * vx_pcm_prepare - prepare callback for playback and capture
796 static int vx_pcm_prepare(struct snd_pcm_substream *subs)
798 struct vx_core *chip = snd_pcm_substream_chip(subs);
799 struct snd_pcm_runtime *runtime = subs->runtime;
800 struct vx_pipe *pipe = runtime->private_data;
802 // int max_size, nchunks;
804 if (chip->chip_status & VX_STAT_IS_STALE)
807 data_mode = (chip->uer_bits & IEC958_AES0_NONAUDIO) != 0;
808 if (data_mode != pipe->data_mode && ! pipe->is_capture) {
809 /* IEC958 status (raw-mode) was changed */
810 /* we reopen the pipe */
812 snd_printdd(KERN_DEBUG "reopen the pipe with data_mode = %d\n", data_mode);
813 vx_init_rmh(&rmh, CMD_FREE_PIPE);
814 vx_set_pipe_cmd_params(&rmh, 0, pipe->number, 0);
815 if ((err = vx_send_msg(chip, &rmh)) < 0)
817 vx_init_rmh(&rmh, CMD_RES_PIPE);
818 vx_set_pipe_cmd_params(&rmh, 0, pipe->number, pipe->channels);
820 rmh.Cmd[0] |= BIT_DATA_MODE;
821 if ((err = vx_send_msg(chip, &rmh)) < 0)
823 pipe->data_mode = data_mode;
826 if (chip->pcm_running && chip->freq != runtime->rate) {
827 snd_printk(KERN_ERR "vx: cannot set different clock %d "
828 "from the current %d\n", runtime->rate, chip->freq);
831 vx_set_clock(chip, runtime->rate);
833 if ((err = vx_set_format(chip, pipe, runtime)) < 0)
836 if (vx_is_pcmcia(chip)) {
837 pipe->align = 2; /* 16bit word */
839 pipe->align = 4; /* 32bit word */
842 pipe->buffer_bytes = frames_to_bytes(runtime, runtime->buffer_size);
843 pipe->period_bytes = frames_to_bytes(runtime, runtime->period_size);
846 /* set the timestamp */
847 vx_update_pipe_position(chip, runtime, pipe);
849 pipe->transferred = 0;
859 * operators for PCM playback
861 static const struct snd_pcm_ops vx_pcm_playback_ops = {
862 .open = vx_pcm_playback_open,
863 .close = vx_pcm_playback_close,
864 .ioctl = snd_pcm_lib_ioctl,
865 .hw_params = vx_pcm_hw_params,
866 .hw_free = vx_pcm_hw_free,
867 .prepare = vx_pcm_prepare,
868 .trigger = vx_pcm_trigger,
869 .pointer = vx_pcm_playback_pointer,
870 .page = snd_pcm_lib_get_vmalloc_page,
875 * playback hw information
878 static const struct snd_pcm_hardware vx_pcm_capture_hw = {
879 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
880 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_MMAP_VALID /*|*/
881 /*SNDRV_PCM_INFO_RESUME*/),
882 .formats = (/*SNDRV_PCM_FMTBIT_U8 |*/
883 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_3LE),
884 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
889 .buffer_bytes_max = (128*1024),
890 .period_bytes_min = 126,
891 .period_bytes_max = (128*1024),
893 .periods_max = VX_MAX_PERIODS,
899 * vx_pcm_capture_open - open callback for capture
901 static int vx_pcm_capture_open(struct snd_pcm_substream *subs)
903 struct snd_pcm_runtime *runtime = subs->runtime;
904 struct vx_core *chip = snd_pcm_substream_chip(subs);
905 struct vx_pipe *pipe;
906 struct vx_pipe *pipe_out_monitoring = NULL;
910 if (chip->chip_status & VX_STAT_IS_STALE)
913 audio = subs->pcm->device * 2;
914 if (snd_BUG_ON(audio >= chip->audio_ins))
916 err = vx_alloc_pipe(chip, 1, audio, 2, &pipe);
919 pipe->substream = subs;
920 chip->capture_pipes[audio] = pipe;
922 /* check if monitoring is needed */
923 if (chip->audio_monitor_active[audio]) {
924 pipe_out_monitoring = chip->playback_pipes[audio];
925 if (! pipe_out_monitoring) {
926 /* allocate a pipe */
927 err = vx_alloc_pipe(chip, 0, audio, 2, &pipe_out_monitoring);
930 chip->playback_pipes[audio] = pipe_out_monitoring;
932 pipe_out_monitoring->references++;
934 if an output pipe is available, it's audios still may need to be
935 unmuted. hence we'll have to call a mixer entry point.
937 vx_set_monitor_level(chip, audio, chip->audio_monitor[audio],
938 chip->audio_monitor_active[audio]);
939 /* assuming stereo */
940 vx_set_monitor_level(chip, audio+1, chip->audio_monitor[audio+1],
941 chip->audio_monitor_active[audio+1]);
944 pipe->monitoring_pipe = pipe_out_monitoring; /* default value NULL */
946 runtime->hw = vx_pcm_capture_hw;
947 runtime->hw.period_bytes_min = chip->ibl.size;
948 runtime->private_data = pipe;
950 /* align to 4 bytes (otherwise will be problematic when 24bit is used) */
951 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 4);
952 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 4);
958 * vx_pcm_capture_close - close callback for capture
960 static int vx_pcm_capture_close(struct snd_pcm_substream *subs)
962 struct vx_core *chip = snd_pcm_substream_chip(subs);
963 struct vx_pipe *pipe;
964 struct vx_pipe *pipe_out_monitoring;
966 if (! subs->runtime->private_data)
968 pipe = subs->runtime->private_data;
969 chip->capture_pipes[pipe->number] = NULL;
971 pipe_out_monitoring = pipe->monitoring_pipe;
974 if an output pipe is attached to this input,
975 check if it needs to be released.
977 if (pipe_out_monitoring) {
978 if (--pipe_out_monitoring->references == 0) {
979 vx_free_pipe(chip, pipe_out_monitoring);
980 chip->playback_pipes[pipe->number] = NULL;
981 pipe->monitoring_pipe = NULL;
985 vx_free_pipe(chip, pipe);
991 #define DMA_READ_ALIGN 6 /* hardware alignment for read */
994 * vx_pcm_capture_update - update the capture buffer
996 static void vx_pcm_capture_update(struct vx_core *chip, struct snd_pcm_substream *subs,
997 struct vx_pipe *pipe)
999 int size, space, count;
1000 struct snd_pcm_runtime *runtime = subs->runtime;
1002 if (!pipe->running || (chip->chip_status & VX_STAT_IS_STALE))
1005 size = runtime->buffer_size - snd_pcm_capture_avail(runtime);
1008 size = frames_to_bytes(runtime, size);
1009 space = vx_query_hbuffer_size(chip, pipe);
1014 size = (size / 3) * 3; /* align to 3 bytes */
1015 if (size < DMA_READ_ALIGN)
1018 /* keep the last 6 bytes, they will be read after disconnection */
1019 count = size - DMA_READ_ALIGN;
1020 /* read bytes until the current pointer reaches to the aligned position
1024 if ((pipe->hw_ptr % pipe->align) == 0)
1026 if (vx_wait_for_rx_full(chip) < 0)
1028 vx_pcm_read_per_bytes(chip, runtime, pipe);
1032 /* ok, let's accelerate! */
1033 int align = pipe->align * 3;
1034 space = (count / align) * align;
1036 vx_pseudo_dma_read(chip, runtime, pipe, space);
1040 /* read the rest of bytes */
1042 if (vx_wait_for_rx_full(chip) < 0)
1044 vx_pcm_read_per_bytes(chip, runtime, pipe);
1047 /* disconnect the host, SIZE_HBUF command always switches to the stream mode */
1048 vx_send_rih(chip, IRQ_CONNECT_STREAM_NEXT);
1049 /* read the last pending 6 bytes */
1050 count = DMA_READ_ALIGN;
1052 vx_pcm_read_per_bytes(chip, runtime, pipe);
1055 /* update the position */
1056 pipe->transferred += size;
1057 if (pipe->transferred >= pipe->period_bytes) {
1058 pipe->transferred %= pipe->period_bytes;
1059 snd_pcm_period_elapsed(subs);
1064 /* disconnect the host, SIZE_HBUF command always switches to the stream mode */
1065 vx_send_rih(chip, IRQ_CONNECT_STREAM_NEXT);
1070 * vx_pcm_capture_pointer - pointer callback for capture
1072 static snd_pcm_uframes_t vx_pcm_capture_pointer(struct snd_pcm_substream *subs)
1074 struct snd_pcm_runtime *runtime = subs->runtime;
1075 struct vx_pipe *pipe = runtime->private_data;
1076 return bytes_to_frames(runtime, pipe->hw_ptr);
1080 * operators for PCM capture
1082 static const struct snd_pcm_ops vx_pcm_capture_ops = {
1083 .open = vx_pcm_capture_open,
1084 .close = vx_pcm_capture_close,
1085 .ioctl = snd_pcm_lib_ioctl,
1086 .hw_params = vx_pcm_hw_params,
1087 .hw_free = vx_pcm_hw_free,
1088 .prepare = vx_pcm_prepare,
1089 .trigger = vx_pcm_trigger,
1090 .pointer = vx_pcm_capture_pointer,
1091 .page = snd_pcm_lib_get_vmalloc_page,
1096 * interrupt handler for pcm streams
1098 void vx_pcm_update_intr(struct vx_core *chip, unsigned int events)
1101 struct vx_pipe *pipe;
1103 #define EVENT_MASK (END_OF_BUFFER_EVENTS_PENDING|ASYNC_EVENTS_PENDING)
1105 if (events & EVENT_MASK) {
1106 vx_init_rmh(&chip->irq_rmh, CMD_ASYNC);
1107 if (events & ASYNC_EVENTS_PENDING)
1108 chip->irq_rmh.Cmd[0] |= 0x00000001; /* SEL_ASYNC_EVENTS */
1109 if (events & END_OF_BUFFER_EVENTS_PENDING)
1110 chip->irq_rmh.Cmd[0] |= 0x00000002; /* SEL_END_OF_BUF_EVENTS */
1112 if (vx_send_msg(chip, &chip->irq_rmh) < 0) {
1113 snd_printdd(KERN_ERR "msg send error!!\n");
1118 while (i < chip->irq_rmh.LgStat) {
1119 int p, buf, capture, eob;
1120 p = chip->irq_rmh.Stat[i] & MASK_FIRST_FIELD;
1121 capture = (chip->irq_rmh.Stat[i] & 0x400000) ? 1 : 0;
1122 eob = (chip->irq_rmh.Stat[i] & 0x800000) ? 1 : 0;
1124 if (events & ASYNC_EVENTS_PENDING)
1126 buf = 1; /* force to transfer */
1127 if (events & END_OF_BUFFER_EVENTS_PENDING) {
1129 buf = chip->irq_rmh.Stat[i];
1134 if (snd_BUG_ON(p < 0 || p >= chip->audio_outs))
1136 pipe = chip->playback_pipes[p];
1137 if (pipe && pipe->substream) {
1138 vx_pcm_playback_update(chip, pipe->substream, pipe);
1139 vx_pcm_playback_transfer(chip, pipe->substream, pipe, buf);
1144 /* update the capture pcm pointers as frequently as possible */
1145 for (i = 0; i < chip->audio_ins; i++) {
1146 pipe = chip->capture_pipes[i];
1147 if (pipe && pipe->substream)
1148 vx_pcm_capture_update(chip, pipe->substream, pipe);
1154 * vx_init_audio_io - check the available audio i/o and allocate pipe arrays
1156 static int vx_init_audio_io(struct vx_core *chip)
1161 vx_init_rmh(&rmh, CMD_SUPPORTED);
1162 if (vx_send_msg(chip, &rmh) < 0) {
1163 snd_printk(KERN_ERR "vx: cannot get the supported audio data\n");
1167 chip->audio_outs = rmh.Stat[0] & MASK_FIRST_FIELD;
1168 chip->audio_ins = (rmh.Stat[0] >> (FIELD_SIZE*2)) & MASK_FIRST_FIELD;
1169 chip->audio_info = rmh.Stat[1];
1171 /* allocate pipes */
1172 chip->playback_pipes = kcalloc(chip->audio_outs, sizeof(struct vx_pipe *), GFP_KERNEL);
1173 if (!chip->playback_pipes)
1175 chip->capture_pipes = kcalloc(chip->audio_ins, sizeof(struct vx_pipe *), GFP_KERNEL);
1176 if (!chip->capture_pipes) {
1177 kfree(chip->playback_pipes);
1181 preferred = chip->ibl.size;
1183 vx_set_ibl(chip, &chip->ibl); /* query the info */
1184 if (preferred > 0) {
1185 chip->ibl.size = ((preferred + chip->ibl.granularity - 1) /
1186 chip->ibl.granularity) * chip->ibl.granularity;
1187 if (chip->ibl.size > chip->ibl.max_size)
1188 chip->ibl.size = chip->ibl.max_size;
1190 chip->ibl.size = chip->ibl.min_size; /* set to the minimum */
1191 vx_set_ibl(chip, &chip->ibl);
1198 * free callback for pcm
1200 static void snd_vx_pcm_free(struct snd_pcm *pcm)
1202 struct vx_core *chip = pcm->private_data;
1203 chip->pcm[pcm->device] = NULL;
1204 kfree(chip->playback_pipes);
1205 chip->playback_pipes = NULL;
1206 kfree(chip->capture_pipes);
1207 chip->capture_pipes = NULL;
1211 * snd_vx_pcm_new - create and initialize a pcm
1213 int snd_vx_pcm_new(struct vx_core *chip)
1215 struct snd_pcm *pcm;
1219 if ((err = vx_init_audio_io(chip)) < 0)
1222 for (i = 0; i < chip->hw->num_codecs; i++) {
1223 unsigned int outs, ins;
1224 outs = chip->audio_outs > i * 2 ? 1 : 0;
1225 ins = chip->audio_ins > i * 2 ? 1 : 0;
1226 if (! outs && ! ins)
1228 err = snd_pcm_new(chip->card, "VX PCM", i,
1233 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &vx_pcm_playback_ops);
1235 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &vx_pcm_capture_ops);
1237 pcm->private_data = chip;
1238 pcm->private_free = snd_vx_pcm_free;
1239 pcm->info_flags = 0;
1240 pcm->nonatomic = true;
1241 strcpy(pcm->name, chip->card->shortname);