[TG3]: Set minimal hw interrupt mitigation.
[linux-2.6/verdex.git] / sound / drivers / vx / vx_pcm.c
blob98587176b32744469818e9074e9a9508e4821f45
1 /*
2 * Driver for Digigram VX soundcards
4 * PCM part
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
23 * STRATEGY
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
31 * be issued.
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
40 * this is in bytes.
43 * TODO
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>
56 #include "vx_cmd.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)
86 return -ENOMEM;
87 memset(runtime->dma_area, 0, size);
88 runtime->dma_bytes = size;
89 return 1; /* changed */
93 * free the buffer.
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;
104 return 0;
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) {
117 offset = 0;
118 buf = (unsigned char *)runtime->dma_area;
120 *buf++ = vx_inb(chip, RXM);
121 if (++offset >= pipe->buffer_bytes) {
122 offset = 0;
123 buf = (unsigned char *)runtime->dma_area;
125 *buf++ = vx_inb(chip, RXL);
126 if (++offset >= pipe->buffer_bytes) {
127 offset = 0;
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))
158 return 0;
160 /* Set the T bit */
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;
178 rmh->LgCmd += 2;
179 return 2;
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)
189 struct vx_rmh rmh;
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*/;
200 rmh.LgCmd += 2;
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)) {
228 // case 8: break;
229 case 16: header |= HEADER_FMT_16BITS; break;
230 case 24: header |= HEADER_FMT_24BITS; break;
231 default :
232 snd_BUG();
233 return -EINVAL;
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)
244 int err;
245 struct vx_rmh rmh;
247 vx_init_rmh(&rmh, CMD_IBL);
248 rmh.Cmd[0] |= info->size & 0x03ffff;
249 err = vx_send_msg(chip, &rmh);
250 if (err < 0)
251 return err;
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);
258 return 0;
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)
274 int err;
275 struct vx_rmh rmh;
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 */
280 if (! err)
281 *state = (rmh.Stat[0] & (1 << pipe->number)) ? 1 : 0;
282 return err;
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
295 * normal mode.
297 static int vx_query_hbuffer_size(vx_core_t *chip, vx_pipe_t *pipe)
299 int result;
300 struct vx_rmh rmh;
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);
307 if (! result)
308 result = rmh.Stat[0] & 0xffff;
309 return result;
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)
323 int err;
324 struct vx_rmh rmh;
326 vx_init_rmh(&rmh, CMD_CAN_START_PIPE);
327 vx_set_pipe_cmd_params(&rmh, pipe->is_capture, pipe->number, 0);
328 rmh.Cmd[0] |= 1;
330 err = vx_send_msg_nolock(chip, &rmh); /* no lock needed for trigger */
331 if (! err) {
332 if (rmh.Stat[0])
333 err = 1;
335 return err;
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)
344 struct vx_rmh rmh;
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)
358 struct vx_rmh rmh;
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)
387 return -EBADFD;
388 if (state == cur_state)
389 return 0;
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)
395 if (state) {
396 for (i = 0 ; i < MAX_WAIT_FOR_DSP; i++) {
397 err = vx_pipe_can_start(chip, pipe);
398 if (err > 0)
399 break;
400 /* Wait for a few, before asking again
401 * to avoid flooding the DSP with our requests
403 mdelay(1);
407 if ((err = vx_conf_pipe(chip, pipe)) < 0)
408 return err;
410 if ((err = vx_send_irqa(chip)) < 0)
411 return err;
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)
420 break;
421 err = -EIO;
422 mdelay(1);
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)
436 struct vx_rmh rmh;
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,
454 vx_pipe_t **pipep)
456 int err;
457 vx_pipe_t *pipe;
458 struct vx_rmh rmh;
459 int data_mode;
461 *pipep = NULL;
462 vx_init_rmh(&rmh, CMD_RES_PIPE);
463 vx_set_pipe_cmd_params(&rmh, capture, audioid, num_audio);
464 #if 0 // NYI
465 if (underrun_skip_sound)
466 rmh.Cmd[0] |= BIT_SKIP_SOUND;
467 #endif // NYI
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);
472 if (err < 0)
473 return err;
475 /* initialize the pipe record */
476 pipe = kcalloc(1, sizeof(*pipe), GFP_KERNEL);
477 if (! pipe) {
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);
482 return -ENOMEM;
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;
490 pipe->pcx_time = 0;
491 pipe->data_mode = data_mode;
492 *pipep = pipe;
494 return 0;
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)
504 struct vx_rmh rmh;
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);
510 kfree(pipe);
511 return 0;
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)
522 struct vx_rmh rmh;
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)
538 struct vx_rmh rmh;
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 SNDRV_PCM_INFO_RESUME),
554 .formats = /*SNDRV_PCM_FMTBIT_U8 |*/ SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_3LE,
555 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
556 .rate_min = 5000,
557 .rate_max = 48000,
558 .channels_min = 1,
559 .channels_max = 2,
560 .buffer_bytes_max = (128*1024),
561 .period_bytes_min = 126,
562 .period_bytes_max = (128*1024),
563 .periods_min = 2,
564 .periods_max = VX_MAX_PERIODS,
565 .fifo_size = 126,
569 static void vx_pcm_delayed_start(unsigned long arg);
572 * vx_pcm_playback_open - open callback for playback
574 static int vx_pcm_playback_open(snd_pcm_substream_t *subs)
576 snd_pcm_runtime_t *runtime = subs->runtime;
577 vx_core_t *chip = snd_pcm_substream_chip(subs);
578 vx_pipe_t *pipe = NULL;
579 unsigned int audio;
580 int err;
582 if (chip->chip_status & VX_STAT_IS_STALE)
583 return -EBUSY;
585 audio = subs->pcm->device * 2;
586 snd_assert(audio < chip->audio_outs, return -EINVAL);
588 /* playback pipe may have been already allocated for monitoring */
589 pipe = chip->playback_pipes[audio];
590 if (! pipe) {
591 /* not allocated yet */
592 err = vx_alloc_pipe(chip, 0, audio, 2, &pipe); /* stereo playback */
593 if (err < 0)
594 return err;
595 chip->playback_pipes[audio] = pipe;
597 /* open for playback */
598 pipe->references++;
600 pipe->substream = subs;
601 tasklet_init(&pipe->start_tq, vx_pcm_delayed_start, (unsigned long)subs);
602 chip->playback_pipes[audio] = pipe;
604 runtime->hw = vx_pcm_playback_hw;
605 runtime->hw.period_bytes_min = chip->ibl.size;
606 runtime->private_data = pipe;
608 /* align to 4 bytes (otherwise will be problematic when 24bit is used) */
609 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 4);
610 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 4);
612 return 0;
616 * vx_pcm_playback_close - close callback for playback
618 static int vx_pcm_playback_close(snd_pcm_substream_t *subs)
620 vx_core_t *chip = snd_pcm_substream_chip(subs);
621 vx_pipe_t *pipe;
623 if (! subs->runtime->private_data)
624 return -EINVAL;
626 pipe = subs->runtime->private_data;
628 if (--pipe->references == 0) {
629 chip->playback_pipes[pipe->number] = NULL;
630 vx_free_pipe(chip, pipe);
633 return 0;
639 * vx_notify_end_of_buffer - send "end-of-buffer" notifier at the given pipe
640 * @pipe: the pipe to notify
642 * NB: call with a certain lock.
644 static int vx_notify_end_of_buffer(vx_core_t *chip, vx_pipe_t *pipe)
646 int err;
647 struct vx_rmh rmh; /* use a temporary rmh here */
649 /* Toggle Dsp Host Interface into Message mode */
650 vx_send_rih_nolock(chip, IRQ_PAUSE_START_CONNECT);
651 vx_init_rmh(&rmh, CMD_NOTIFY_END_OF_BUFFER);
652 vx_set_stream_cmd_params(&rmh, 0, pipe->number);
653 err = vx_send_msg_nolock(chip, &rmh);
654 if (err < 0)
655 return err;
656 /* Toggle Dsp Host Interface back to sound transfer mode */
657 vx_send_rih_nolock(chip, IRQ_PAUSE_START_CONNECT);
658 return 0;
662 * vx_pcm_playback_transfer_chunk - transfer a single chunk
663 * @subs: substream
664 * @pipe: the pipe to transfer
665 * @size: chunk size in bytes
667 * transfer a single buffer chunk. EOB notificaton is added after that.
668 * called from the interrupt handler, too.
670 * return 0 if ok.
672 static int vx_pcm_playback_transfer_chunk(vx_core_t *chip, snd_pcm_runtime_t *runtime, vx_pipe_t *pipe, int size)
674 int space, err = 0;
676 space = vx_query_hbuffer_size(chip, pipe);
677 if (space < 0) {
678 /* disconnect the host, SIZE_HBUF command always switches to the stream mode */
679 vx_send_rih(chip, IRQ_CONNECT_STREAM_NEXT);
680 snd_printd("error hbuffer\n");
681 return space;
683 if (space < size) {
684 vx_send_rih(chip, IRQ_CONNECT_STREAM_NEXT);
685 snd_printd("no enough hbuffer space %d\n", space);
686 return -EIO; /* XRUN */
689 /* we don't need irqsave here, because this function
690 * is called from either trigger callback or irq handler
692 spin_lock(&chip->lock);
693 vx_pseudo_dma_write(chip, runtime, pipe, size);
694 err = vx_notify_end_of_buffer(chip, pipe);
695 /* disconnect the host, SIZE_HBUF command always switches to the stream mode */
696 vx_send_rih_nolock(chip, IRQ_CONNECT_STREAM_NEXT);
697 spin_unlock(&chip->lock);
698 return err;
702 * update the position of the given pipe.
703 * pipe->position is updated and wrapped within the buffer size.
704 * pipe->transferred is updated, too, but the size is not wrapped,
705 * so that the caller can check the total transferred size later
706 * (to call snd_pcm_period_elapsed).
708 static int vx_update_pipe_position(vx_core_t *chip, snd_pcm_runtime_t *runtime, vx_pipe_t *pipe)
710 struct vx_rmh rmh;
711 int err, update;
712 u64 count;
714 vx_init_rmh(&rmh, CMD_STREAM_SAMPLE_COUNT);
715 vx_set_pipe_cmd_params(&rmh, pipe->is_capture, pipe->number, 0);
716 err = vx_send_msg(chip, &rmh);
717 if (err < 0)
718 return err;
720 count = ((u64)(rmh.Stat[0] & 0xfffff) << 24) | (u64)rmh.Stat[1];
721 update = (int)(count - pipe->cur_count);
722 pipe->cur_count = count;
723 pipe->position += update;
724 if (pipe->position >= (int)runtime->buffer_size)
725 pipe->position %= runtime->buffer_size;
726 pipe->transferred += update;
727 return 0;
731 * transfer the pending playback buffer data to DSP
732 * called from interrupt handler
734 static void vx_pcm_playback_transfer(vx_core_t *chip, snd_pcm_substream_t *subs, vx_pipe_t *pipe, int nchunks)
736 int i, err;
737 snd_pcm_runtime_t *runtime = subs->runtime;
739 if (! pipe->prepared || (chip->chip_status & VX_STAT_IS_STALE))
740 return;
741 for (i = 0; i < nchunks; i++) {
742 if ((err = vx_pcm_playback_transfer_chunk(chip, runtime, pipe,
743 chip->ibl.size)) < 0)
744 return;
749 * update the playback position and call snd_pcm_period_elapsed() if necessary
750 * called from interrupt handler
752 static void vx_pcm_playback_update(vx_core_t *chip, snd_pcm_substream_t *subs, vx_pipe_t *pipe)
754 int err;
755 snd_pcm_runtime_t *runtime = subs->runtime;
757 if (pipe->running && ! (chip->chip_status & VX_STAT_IS_STALE)) {
758 if ((err = vx_update_pipe_position(chip, runtime, pipe)) < 0)
759 return;
760 if (pipe->transferred >= (int)runtime->period_size) {
761 pipe->transferred %= runtime->period_size;
762 snd_pcm_period_elapsed(subs);
768 * start the stream and pipe.
769 * this function is called from tasklet, which is invoked by the trigger
770 * START callback.
772 static void vx_pcm_delayed_start(unsigned long arg)
774 snd_pcm_substream_t *subs = (snd_pcm_substream_t *)arg;
775 vx_core_t *chip = subs->pcm->private_data;
776 vx_pipe_t *pipe = subs->runtime->private_data;
777 int err;
779 /* printk( KERN_DEBUG "DDDD tasklet delayed start jiffies = %ld\n", jiffies);*/
781 if ((err = vx_start_stream(chip, pipe)) < 0) {
782 snd_printk(KERN_ERR "vx: cannot start stream\n");
783 return;
785 if ((err = vx_toggle_pipe(chip, pipe, 1)) < 0) {
786 snd_printk(KERN_ERR "vx: cannot start pipe\n");
787 return;
789 /* printk( KERN_DEBUG "dddd tasklet delayed start jiffies = %ld \n", jiffies);*/
793 * vx_pcm_playback_trigger - trigger callback for playback
795 static int vx_pcm_trigger(snd_pcm_substream_t *subs, int cmd)
797 vx_core_t *chip = snd_pcm_substream_chip(subs);
798 vx_pipe_t *pipe = subs->runtime->private_data;
799 int err;
801 if (chip->chip_status & VX_STAT_IS_STALE)
802 return -EBUSY;
804 switch (cmd) {
805 case SNDRV_PCM_TRIGGER_START:
806 case SNDRV_PCM_TRIGGER_RESUME:
807 if (! pipe->is_capture)
808 vx_pcm_playback_transfer(chip, subs, pipe, 2);
809 /* FIXME:
810 * we trigger the pipe using tasklet, so that the interrupts are
811 * issued surely after the trigger is completed.
813 tasklet_hi_schedule(&pipe->start_tq);
814 chip->pcm_running++;
815 pipe->running = 1;
816 break;
817 case SNDRV_PCM_TRIGGER_STOP:
818 case SNDRV_PCM_TRIGGER_SUSPEND:
819 vx_toggle_pipe(chip, pipe, 0);
820 vx_stop_pipe(chip, pipe);
821 vx_stop_stream(chip, pipe);
822 chip->pcm_running--;
823 pipe->running = 0;
824 break;
825 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
826 if ((err = vx_toggle_pipe(chip, pipe, 0)) < 0)
827 return err;
828 break;
829 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
830 if ((err = vx_toggle_pipe(chip, pipe, 1)) < 0)
831 return err;
832 break;
833 default:
834 return -EINVAL;
836 return 0;
840 * vx_pcm_playback_pointer - pointer callback for playback
842 static snd_pcm_uframes_t vx_pcm_playback_pointer(snd_pcm_substream_t *subs)
844 snd_pcm_runtime_t *runtime = subs->runtime;
845 vx_pipe_t *pipe = runtime->private_data;
846 return pipe->position;
850 * vx_pcm_hw_params - hw_params callback for playback and capture
852 static int vx_pcm_hw_params(snd_pcm_substream_t *subs,
853 snd_pcm_hw_params_t *hw_params)
855 return snd_pcm_alloc_vmalloc_buffer(subs, params_buffer_bytes(hw_params));
859 * vx_pcm_hw_free - hw_free callback for playback and capture
861 static int vx_pcm_hw_free(snd_pcm_substream_t *subs)
863 return snd_pcm_free_vmalloc_buffer(subs);
867 * vx_pcm_prepare - prepare callback for playback and capture
869 static int vx_pcm_prepare(snd_pcm_substream_t *subs)
871 vx_core_t *chip = snd_pcm_substream_chip(subs);
872 snd_pcm_runtime_t *runtime = subs->runtime;
873 vx_pipe_t *pipe = runtime->private_data;
874 int err, data_mode;
875 // int max_size, nchunks;
877 if (chip->chip_status & VX_STAT_IS_STALE)
878 return -EBUSY;
880 data_mode = (chip->uer_bits & IEC958_AES0_NONAUDIO) != 0;
881 if (data_mode != pipe->data_mode && ! pipe->is_capture) {
882 /* IEC958 status (raw-mode) was changed */
883 /* we reopen the pipe */
884 struct vx_rmh rmh;
885 snd_printdd(KERN_DEBUG "reopen the pipe with data_mode = %d\n", data_mode);
886 vx_init_rmh(&rmh, CMD_FREE_PIPE);
887 vx_set_pipe_cmd_params(&rmh, 0, pipe->number, 0);
888 if ((err = vx_send_msg(chip, &rmh)) < 0)
889 return err;
890 vx_init_rmh(&rmh, CMD_RES_PIPE);
891 vx_set_pipe_cmd_params(&rmh, 0, pipe->number, pipe->channels);
892 if (data_mode)
893 rmh.Cmd[0] |= BIT_DATA_MODE;
894 if ((err = vx_send_msg(chip, &rmh)) < 0)
895 return err;
896 pipe->data_mode = data_mode;
899 if (chip->pcm_running && chip->freq != runtime->rate) {
900 snd_printk(KERN_ERR "vx: cannot set different clock %d from the current %d\n", runtime->rate, chip->freq);
901 return -EINVAL;
903 vx_set_clock(chip, runtime->rate);
905 if ((err = vx_set_format(chip, pipe, runtime)) < 0)
906 return err;
908 if (vx_is_pcmcia(chip)) {
909 pipe->align = 2; /* 16bit word */
910 } else {
911 pipe->align = 4; /* 32bit word */
914 pipe->buffer_bytes = frames_to_bytes(runtime, runtime->buffer_size);
915 pipe->period_bytes = frames_to_bytes(runtime, runtime->period_size);
916 pipe->hw_ptr = 0;
918 /* set the timestamp */
919 vx_update_pipe_position(chip, runtime, pipe);
920 /* clear again */
921 pipe->transferred = 0;
922 pipe->position = 0;
924 pipe->prepared = 1;
926 return 0;
931 * operators for PCM playback
933 static snd_pcm_ops_t vx_pcm_playback_ops = {
934 .open = vx_pcm_playback_open,
935 .close = vx_pcm_playback_close,
936 .ioctl = snd_pcm_lib_ioctl,
937 .hw_params = vx_pcm_hw_params,
938 .hw_free = vx_pcm_hw_free,
939 .prepare = vx_pcm_prepare,
940 .trigger = vx_pcm_trigger,
941 .pointer = vx_pcm_playback_pointer,
942 .page = snd_pcm_get_vmalloc_page,
947 * playback hw information
950 static snd_pcm_hardware_t vx_pcm_capture_hw = {
951 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
952 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_MMAP_VALID |
953 SNDRV_PCM_INFO_RESUME),
954 .formats = /*SNDRV_PCM_FMTBIT_U8 |*/ SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_3LE,
955 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
956 .rate_min = 5000,
957 .rate_max = 48000,
958 .channels_min = 1,
959 .channels_max = 2,
960 .buffer_bytes_max = (128*1024),
961 .period_bytes_min = 126,
962 .period_bytes_max = (128*1024),
963 .periods_min = 2,
964 .periods_max = VX_MAX_PERIODS,
965 .fifo_size = 126,
970 * vx_pcm_capture_open - open callback for capture
972 static int vx_pcm_capture_open(snd_pcm_substream_t *subs)
974 snd_pcm_runtime_t *runtime = subs->runtime;
975 vx_core_t *chip = snd_pcm_substream_chip(subs);
976 vx_pipe_t *pipe;
977 vx_pipe_t *pipe_out_monitoring = NULL;
978 unsigned int audio;
979 int err;
981 if (chip->chip_status & VX_STAT_IS_STALE)
982 return -EBUSY;
984 audio = subs->pcm->device * 2;
985 snd_assert(audio < chip->audio_ins, return -EINVAL);
986 err = vx_alloc_pipe(chip, 1, audio, 2, &pipe);
987 if (err < 0)
988 return err;
989 pipe->substream = subs;
990 tasklet_init(&pipe->start_tq, vx_pcm_delayed_start, (unsigned long)subs);
991 chip->capture_pipes[audio] = pipe;
993 /* check if monitoring is needed */
994 if (chip->audio_monitor_active[audio]) {
995 pipe_out_monitoring = chip->playback_pipes[audio];
996 if (! pipe_out_monitoring) {
997 /* allocate a pipe */
998 err = vx_alloc_pipe(chip, 0, audio, 2, &pipe_out_monitoring);
999 if (err < 0)
1000 return err;
1001 chip->playback_pipes[audio] = pipe_out_monitoring;
1003 pipe_out_monitoring->references++;
1005 if an output pipe is available, it's audios still may need to be
1006 unmuted. hence we'll have to call a mixer entry point.
1008 vx_set_monitor_level(chip, audio, chip->audio_monitor[audio], chip->audio_monitor_active[audio]);
1009 /* assuming stereo */
1010 vx_set_monitor_level(chip, audio+1, chip->audio_monitor[audio+1], chip->audio_monitor_active[audio+1]);
1013 pipe->monitoring_pipe = pipe_out_monitoring; /* default value NULL */
1015 runtime->hw = vx_pcm_capture_hw;
1016 runtime->hw.period_bytes_min = chip->ibl.size;
1017 runtime->private_data = pipe;
1019 /* align to 4 bytes (otherwise will be problematic when 24bit is used) */
1020 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 4);
1021 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 4);
1023 return 0;
1027 * vx_pcm_capture_close - close callback for capture
1029 static int vx_pcm_capture_close(snd_pcm_substream_t *subs)
1031 vx_core_t *chip = snd_pcm_substream_chip(subs);
1032 vx_pipe_t *pipe;
1033 vx_pipe_t *pipe_out_monitoring;
1035 if (! subs->runtime->private_data)
1036 return -EINVAL;
1037 pipe = subs->runtime->private_data;
1038 chip->capture_pipes[pipe->number] = NULL;
1040 pipe_out_monitoring = pipe->monitoring_pipe;
1043 if an output pipe is attached to this input,
1044 check if it needs to be released.
1046 if (pipe_out_monitoring) {
1047 if (--pipe_out_monitoring->references == 0) {
1048 vx_free_pipe(chip, pipe_out_monitoring);
1049 chip->playback_pipes[pipe->number] = NULL;
1050 pipe->monitoring_pipe = NULL;
1054 vx_free_pipe(chip, pipe);
1055 return 0;
1060 #define DMA_READ_ALIGN 6 /* hardware alignment for read */
1063 * vx_pcm_capture_update - update the capture buffer
1065 static void vx_pcm_capture_update(vx_core_t *chip, snd_pcm_substream_t *subs, vx_pipe_t *pipe)
1067 int size, space, count;
1068 snd_pcm_runtime_t *runtime = subs->runtime;
1070 if (! pipe->prepared || (chip->chip_status & VX_STAT_IS_STALE))
1071 return;
1073 size = runtime->buffer_size - snd_pcm_capture_avail(runtime);
1074 if (! size)
1075 return;
1076 size = frames_to_bytes(runtime, size);
1077 space = vx_query_hbuffer_size(chip, pipe);
1078 if (space < 0)
1079 goto _error;
1080 if (size > space)
1081 size = space;
1082 size = (size / 3) * 3; /* align to 3 bytes */
1083 if (size < DMA_READ_ALIGN)
1084 goto _error;
1086 /* keep the last 6 bytes, they will be read after disconnection */
1087 count = size - DMA_READ_ALIGN;
1088 /* read bytes until the current pointer reaches to the aligned position
1089 * for word-transfer
1091 while (count > 0) {
1092 if ((pipe->hw_ptr % pipe->align) == 0)
1093 break;
1094 if (vx_wait_for_rx_full(chip) < 0)
1095 goto _error;
1096 vx_pcm_read_per_bytes(chip, runtime, pipe);
1097 count -= 3;
1099 if (count > 0) {
1100 /* ok, let's accelerate! */
1101 int align = pipe->align * 3;
1102 space = (count / align) * align;
1103 vx_pseudo_dma_read(chip, runtime, pipe, space);
1104 count -= space;
1106 /* read the rest of bytes */
1107 while (count > 0) {
1108 if (vx_wait_for_rx_full(chip) < 0)
1109 goto _error;
1110 vx_pcm_read_per_bytes(chip, runtime, pipe);
1111 count -= 3;
1113 /* disconnect the host, SIZE_HBUF command always switches to the stream mode */
1114 vx_send_rih_nolock(chip, IRQ_CONNECT_STREAM_NEXT);
1115 /* read the last pending 6 bytes */
1116 count = DMA_READ_ALIGN;
1117 while (count > 0) {
1118 vx_pcm_read_per_bytes(chip, runtime, pipe);
1119 count -= 3;
1121 /* update the position */
1122 pipe->transferred += size;
1123 if (pipe->transferred >= pipe->period_bytes) {
1124 pipe->transferred %= pipe->period_bytes;
1125 snd_pcm_period_elapsed(subs);
1127 return;
1129 _error:
1130 /* disconnect the host, SIZE_HBUF command always switches to the stream mode */
1131 vx_send_rih_nolock(chip, IRQ_CONNECT_STREAM_NEXT);
1132 return;
1136 * vx_pcm_capture_pointer - pointer callback for capture
1138 static snd_pcm_uframes_t vx_pcm_capture_pointer(snd_pcm_substream_t *subs)
1140 snd_pcm_runtime_t *runtime = subs->runtime;
1141 vx_pipe_t *pipe = runtime->private_data;
1142 return bytes_to_frames(runtime, pipe->hw_ptr);
1146 * operators for PCM capture
1148 static snd_pcm_ops_t vx_pcm_capture_ops = {
1149 .open = vx_pcm_capture_open,
1150 .close = vx_pcm_capture_close,
1151 .ioctl = snd_pcm_lib_ioctl,
1152 .hw_params = vx_pcm_hw_params,
1153 .hw_free = vx_pcm_hw_free,
1154 .prepare = vx_pcm_prepare,
1155 .trigger = vx_pcm_trigger,
1156 .pointer = vx_pcm_capture_pointer,
1157 .page = snd_pcm_get_vmalloc_page,
1162 * interrupt handler for pcm streams
1164 void vx_pcm_update_intr(vx_core_t *chip, unsigned int events)
1166 unsigned int i;
1167 vx_pipe_t *pipe;
1169 #define EVENT_MASK (END_OF_BUFFER_EVENTS_PENDING|ASYNC_EVENTS_PENDING)
1171 if (events & EVENT_MASK) {
1172 vx_init_rmh(&chip->irq_rmh, CMD_ASYNC);
1173 if (events & ASYNC_EVENTS_PENDING)
1174 chip->irq_rmh.Cmd[0] |= 0x00000001; /* SEL_ASYNC_EVENTS */
1175 if (events & END_OF_BUFFER_EVENTS_PENDING)
1176 chip->irq_rmh.Cmd[0] |= 0x00000002; /* SEL_END_OF_BUF_EVENTS */
1178 if (vx_send_msg(chip, &chip->irq_rmh) < 0) {
1179 snd_printdd(KERN_ERR "msg send error!!\n");
1180 return;
1183 i = 1;
1184 while (i < chip->irq_rmh.LgStat) {
1185 int p, buf, capture, eob;
1186 p = chip->irq_rmh.Stat[i] & MASK_FIRST_FIELD;
1187 capture = (chip->irq_rmh.Stat[i] & 0x400000) ? 1 : 0;
1188 eob = (chip->irq_rmh.Stat[i] & 0x800000) ? 1 : 0;
1189 i++;
1190 if (events & ASYNC_EVENTS_PENDING)
1191 i++;
1192 buf = 1; /* force to transfer */
1193 if (events & END_OF_BUFFER_EVENTS_PENDING) {
1194 if (eob)
1195 buf = chip->irq_rmh.Stat[i];
1196 i++;
1198 if (capture)
1199 continue;
1200 snd_assert(p >= 0 && (unsigned int)p < chip->audio_outs,);
1201 pipe = chip->playback_pipes[p];
1202 if (pipe && pipe->substream) {
1203 vx_pcm_playback_update(chip, pipe->substream, pipe);
1204 vx_pcm_playback_transfer(chip, pipe->substream, pipe, buf);
1209 /* update the capture pcm pointers as frequently as possible */
1210 for (i = 0; i < chip->audio_ins; i++) {
1211 pipe = chip->capture_pipes[i];
1212 if (pipe && pipe->substream)
1213 vx_pcm_capture_update(chip, pipe->substream, pipe);
1219 * vx_init_audio_io - check the availabe audio i/o and allocate pipe arrays
1221 static int vx_init_audio_io(vx_core_t *chip)
1223 struct vx_rmh rmh;
1224 int preferred;
1226 vx_init_rmh(&rmh, CMD_SUPPORTED);
1227 if (vx_send_msg(chip, &rmh) < 0) {
1228 snd_printk(KERN_ERR "vx: cannot get the supported audio data\n");
1229 return -ENXIO;
1232 chip->audio_outs = rmh.Stat[0] & MASK_FIRST_FIELD;
1233 chip->audio_ins = (rmh.Stat[0] >> (FIELD_SIZE*2)) & MASK_FIRST_FIELD;
1234 chip->audio_info = rmh.Stat[1];
1236 /* allocate pipes */
1237 chip->playback_pipes = kmalloc(sizeof(vx_pipe_t *) * chip->audio_outs, GFP_KERNEL);
1238 chip->capture_pipes = kmalloc(sizeof(vx_pipe_t *) * chip->audio_ins, GFP_KERNEL);
1239 if (! chip->playback_pipes || ! chip->capture_pipes)
1240 return -ENOMEM;
1242 memset(chip->playback_pipes, 0, sizeof(vx_pipe_t *) * chip->audio_outs);
1243 memset(chip->capture_pipes, 0, sizeof(vx_pipe_t *) * chip->audio_ins);
1245 preferred = chip->ibl.size;
1246 chip->ibl.size = 0;
1247 vx_set_ibl(chip, &chip->ibl); /* query the info */
1248 if (preferred > 0) {
1249 chip->ibl.size = ((preferred + chip->ibl.granularity - 1) / chip->ibl.granularity) * chip->ibl.granularity;
1250 if (chip->ibl.size > chip->ibl.max_size)
1251 chip->ibl.size = chip->ibl.max_size;
1252 } else
1253 chip->ibl.size = chip->ibl.min_size; /* set to the minimum */
1254 vx_set_ibl(chip, &chip->ibl);
1256 return 0;
1261 * free callback for pcm
1263 static void snd_vx_pcm_free(snd_pcm_t *pcm)
1265 vx_core_t *chip = pcm->private_data;
1266 chip->pcm[pcm->device] = NULL;
1267 if (chip->playback_pipes) {
1268 kfree(chip->playback_pipes);
1269 chip->playback_pipes = NULL;
1271 if (chip->capture_pipes) {
1272 kfree(chip->capture_pipes);
1273 chip->capture_pipes = NULL;
1278 * snd_vx_pcm_new - create and initialize a pcm
1280 int snd_vx_pcm_new(vx_core_t *chip)
1282 snd_pcm_t *pcm;
1283 unsigned int i;
1284 int err;
1286 if ((err = vx_init_audio_io(chip)) < 0)
1287 return err;
1289 for (i = 0; i < chip->hw->num_codecs; i++) {
1290 unsigned int outs, ins;
1291 outs = chip->audio_outs > i * 2 ? 1 : 0;
1292 ins = chip->audio_ins > i * 2 ? 1 : 0;
1293 if (! outs && ! ins)
1294 break;
1295 err = snd_pcm_new(chip->card, "VX PCM", i,
1296 outs, ins, &pcm);
1297 if (err < 0)
1298 return err;
1299 if (outs)
1300 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &vx_pcm_playback_ops);
1301 if (ins)
1302 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &vx_pcm_capture_ops);
1304 pcm->private_data = chip;
1305 pcm->private_free = snd_vx_pcm_free;
1306 pcm->info_flags = 0;
1307 strcpy(pcm->name, chip->card->shortname);
1308 chip->pcm[i] = pcm;
1311 return 0;