qeth: Convert ethtool get_stats_count() ops to get_sset_count()
[linux-2.6/libata-dev.git] / sound / drivers / vx / vx_pcm.c
blob6644d0034fbaa357b88f7207b676343881ff4207
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 <linux/slab.h>
49 #include <linux/vmalloc.h>
50 #include <linux/delay.h>
51 #include <sound/core.h>
52 #include <sound/asoundef.h>
53 #include <sound/pcm.h>
54 #include <sound/vx_core.h>
55 #include "vx_cmd.h"
59 * we use a vmalloc'ed (sg-)buffer
62 /* get the physical page pointer on the given offset */
63 static struct page *snd_pcm_get_vmalloc_page(struct snd_pcm_substream *subs,
64 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(struct snd_pcm_substream *subs, size_t size)
77 struct snd_pcm_runtime *runtime = subs->runtime;
78 if (runtime->dma_area) {
79 /* already allocated */
80 if (runtime->dma_bytes >= size)
81 return 0; /* already enough large */
82 vfree(runtime->dma_area);
84 runtime->dma_area = vmalloc_32(size);
85 if (! runtime->dma_area)
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(struct snd_pcm_substream *subs)
99 struct snd_pcm_runtime *runtime = subs->runtime;
101 vfree(runtime->dma_area);
102 runtime->dma_area = NULL;
103 return 0;
108 * read three pending pcm bytes via inb()
110 static void vx_pcm_read_per_bytes(struct vx_core *chip, struct snd_pcm_runtime *runtime,
111 struct vx_pipe *pipe)
113 int offset = pipe->hw_ptr;
114 unsigned char *buf = (unsigned char *)(runtime->dma_area + offset);
115 *buf++ = vx_inb(chip, RXH);
116 if (++offset >= pipe->buffer_bytes) {
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(struct vx_core *chip, pcx_time_t *pc_time,
139 unsigned int *dsp_time)
141 dsp_time[0] = (unsigned int)((*pc_time) >> 24) & PCX_TIME_HI_MASK;
142 dsp_time[1] = (unsigned int)(*pc_time) & MASK_DSP_WORD;
146 * vx_set_differed_time - set the differed time if specified
147 * @rmh: the rmh record to modify
148 * @pipe: the pipe to be checked
150 * if the pipe is programmed with the differed time, set the DSP time
151 * on the rmh and changes its command length.
153 * returns the increase of the command length.
155 static int vx_set_differed_time(struct vx_core *chip, struct vx_rmh *rmh,
156 struct vx_pipe *pipe)
158 /* Update The length added to the RMH command by the timestamp */
159 if (! (pipe->differed_type & DC_DIFFERED_DELAY))
160 return 0;
162 /* Set the T bit */
163 rmh->Cmd[0] |= DSP_DIFFERED_COMMAND_MASK;
165 /* Time stamp is the 1st following parameter */
166 vx_set_pcx_time(chip, &pipe->pcx_time, &rmh->Cmd[1]);
168 /* Add the flags to a notified differed command */
169 if (pipe->differed_type & DC_NOTIFY_DELAY)
170 rmh->Cmd[1] |= NOTIFY_MASK_TIME_HIGH ;
172 /* Add the flags to a multiple differed command */
173 if (pipe->differed_type & DC_MULTIPLE_DELAY)
174 rmh->Cmd[1] |= MULTIPLE_MASK_TIME_HIGH;
176 /* Add the flags to a stream-time differed command */
177 if (pipe->differed_type & DC_STREAM_TIME_DELAY)
178 rmh->Cmd[1] |= STREAM_MASK_TIME_HIGH;
180 rmh->LgCmd += 2;
181 return 2;
185 * vx_set_stream_format - send the stream format command
186 * @pipe: the affected pipe
187 * @data: format bitmask
189 static int vx_set_stream_format(struct vx_core *chip, struct vx_pipe *pipe,
190 unsigned int data)
192 struct vx_rmh rmh;
194 vx_init_rmh(&rmh, pipe->is_capture ?
195 CMD_FORMAT_STREAM_IN : CMD_FORMAT_STREAM_OUT);
196 rmh.Cmd[0] |= pipe->number << FIELD_SIZE;
198 /* Command might be longer since we may have to add a timestamp */
199 vx_set_differed_time(chip, &rmh, pipe);
201 rmh.Cmd[rmh.LgCmd] = (data & 0xFFFFFF00) >> 8;
202 rmh.Cmd[rmh.LgCmd + 1] = (data & 0xFF) << 16 /*| (datal & 0xFFFF00) >> 8*/;
203 rmh.LgCmd += 2;
205 return vx_send_msg(chip, &rmh);
210 * vx_set_format - set the format of a pipe
211 * @pipe: the affected pipe
212 * @runtime: pcm runtime instance to be referred
214 * returns 0 if successful, or a negative error code.
216 static int vx_set_format(struct vx_core *chip, struct vx_pipe *pipe,
217 struct snd_pcm_runtime *runtime)
219 unsigned int header = HEADER_FMT_BASE;
221 if (runtime->channels == 1)
222 header |= HEADER_FMT_MONO;
223 if (snd_pcm_format_little_endian(runtime->format))
224 header |= HEADER_FMT_INTEL;
225 if (runtime->rate < 32000 && runtime->rate > 11025)
226 header |= HEADER_FMT_UPTO32;
227 else if (runtime->rate <= 11025)
228 header |= HEADER_FMT_UPTO11;
230 switch (snd_pcm_format_physical_width(runtime->format)) {
231 // case 8: break;
232 case 16: header |= HEADER_FMT_16BITS; break;
233 case 24: header |= HEADER_FMT_24BITS; break;
234 default :
235 snd_BUG();
236 return -EINVAL;
239 return vx_set_stream_format(chip, pipe, header);
243 * set / query the IBL size
245 static int vx_set_ibl(struct vx_core *chip, struct vx_ibl_info *info)
247 int err;
248 struct vx_rmh rmh;
250 vx_init_rmh(&rmh, CMD_IBL);
251 rmh.Cmd[0] |= info->size & 0x03ffff;
252 err = vx_send_msg(chip, &rmh);
253 if (err < 0)
254 return err;
255 info->size = rmh.Stat[0];
256 info->max_size = rmh.Stat[1];
257 info->min_size = rmh.Stat[2];
258 info->granularity = rmh.Stat[3];
259 snd_printdd(KERN_DEBUG "vx_set_ibl: size = %d, max = %d, min = %d, gran = %d\n",
260 info->size, info->max_size, info->min_size, info->granularity);
261 return 0;
266 * vx_get_pipe_state - get the state of a pipe
267 * @pipe: the pipe to be checked
268 * @state: the pointer for the returned state
270 * checks the state of a given pipe, and stores the state (1 = running,
271 * 0 = paused) on the given pointer.
273 * called from trigger callback only
275 static int vx_get_pipe_state(struct vx_core *chip, struct vx_pipe *pipe, int *state)
277 int err;
278 struct vx_rmh rmh;
280 vx_init_rmh(&rmh, CMD_PIPE_STATE);
281 vx_set_pipe_cmd_params(&rmh, pipe->is_capture, pipe->number, 0);
282 err = vx_send_msg_nolock(chip, &rmh); /* no lock needed for trigger */
283 if (! err)
284 *state = (rmh.Stat[0] & (1 << pipe->number)) ? 1 : 0;
285 return err;
290 * vx_query_hbuffer_size - query available h-buffer size in bytes
291 * @pipe: the pipe to be checked
293 * return the available size on h-buffer in bytes,
294 * or a negative error code.
296 * NOTE: calling this function always switches to the stream mode.
297 * you'll need to disconnect the host to get back to the
298 * normal mode.
300 static int vx_query_hbuffer_size(struct vx_core *chip, struct vx_pipe *pipe)
302 int result;
303 struct vx_rmh rmh;
305 vx_init_rmh(&rmh, CMD_SIZE_HBUFFER);
306 vx_set_pipe_cmd_params(&rmh, pipe->is_capture, pipe->number, 0);
307 if (pipe->is_capture)
308 rmh.Cmd[0] |= 0x00000001;
309 result = vx_send_msg(chip, &rmh);
310 if (! result)
311 result = rmh.Stat[0] & 0xffff;
312 return result;
317 * vx_pipe_can_start - query whether a pipe is ready for start
318 * @pipe: the pipe to be checked
320 * return 1 if ready, 0 if not ready, and negative value on error.
322 * called from trigger callback only
324 static int vx_pipe_can_start(struct vx_core *chip, struct vx_pipe *pipe)
326 int err;
327 struct vx_rmh rmh;
329 vx_init_rmh(&rmh, CMD_CAN_START_PIPE);
330 vx_set_pipe_cmd_params(&rmh, pipe->is_capture, pipe->number, 0);
331 rmh.Cmd[0] |= 1;
333 err = vx_send_msg_nolock(chip, &rmh); /* no lock needed for trigger */
334 if (! err) {
335 if (rmh.Stat[0])
336 err = 1;
338 return err;
342 * vx_conf_pipe - tell the pipe to stand by and wait for IRQA.
343 * @pipe: the pipe to be configured
345 static int vx_conf_pipe(struct vx_core *chip, struct vx_pipe *pipe)
347 struct vx_rmh rmh;
349 vx_init_rmh(&rmh, CMD_CONF_PIPE);
350 if (pipe->is_capture)
351 rmh.Cmd[0] |= COMMAND_RECORD_MASK;
352 rmh.Cmd[1] = 1 << pipe->number;
353 return vx_send_msg_nolock(chip, &rmh); /* no lock needed for trigger */
357 * vx_send_irqa - trigger IRQA
359 static int vx_send_irqa(struct vx_core *chip)
361 struct vx_rmh rmh;
363 vx_init_rmh(&rmh, CMD_SEND_IRQA);
364 return vx_send_msg_nolock(chip, &rmh); /* no lock needed for trigger */
368 #define MAX_WAIT_FOR_DSP 250
370 * vx boards do not support inter-card sync, besides
371 * only 126 samples require to be prepared before a pipe can start
373 #define CAN_START_DELAY 2 /* wait 2ms only before asking if the pipe is ready*/
374 #define WAIT_STATE_DELAY 2 /* wait 2ms after irqA was requested and check if the pipe state toggled*/
377 * vx_toggle_pipe - start / pause a pipe
378 * @pipe: the pipe to be triggered
379 * @state: start = 1, pause = 0
381 * called from trigger callback only
384 static int vx_toggle_pipe(struct vx_core *chip, struct vx_pipe *pipe, int state)
386 int err, i, cur_state;
388 /* Check the pipe is not already in the requested state */
389 if (vx_get_pipe_state(chip, pipe, &cur_state) < 0)
390 return -EBADFD;
391 if (state == cur_state)
392 return 0;
394 /* If a start is requested, ask the DSP to get prepared
395 * and wait for a positive acknowledge (when there are
396 * enough sound buffer for this pipe)
398 if (state) {
399 for (i = 0 ; i < MAX_WAIT_FOR_DSP; i++) {
400 err = vx_pipe_can_start(chip, pipe);
401 if (err > 0)
402 break;
403 /* Wait for a few, before asking again
404 * to avoid flooding the DSP with our requests
406 mdelay(1);
410 if ((err = vx_conf_pipe(chip, pipe)) < 0)
411 return err;
413 if ((err = vx_send_irqa(chip)) < 0)
414 return err;
416 /* If it completes successfully, wait for the pipes
417 * reaching the expected state before returning
418 * Check one pipe only (since they are synchronous)
420 for (i = 0; i < MAX_WAIT_FOR_DSP; i++) {
421 err = vx_get_pipe_state(chip, pipe, &cur_state);
422 if (err < 0 || cur_state == state)
423 break;
424 err = -EIO;
425 mdelay(1);
427 return err < 0 ? -EIO : 0;
432 * vx_stop_pipe - stop a pipe
433 * @pipe: the pipe to be stopped
435 * called from trigger callback only
437 static int vx_stop_pipe(struct vx_core *chip, struct vx_pipe *pipe)
439 struct vx_rmh rmh;
440 vx_init_rmh(&rmh, CMD_STOP_PIPE);
441 vx_set_pipe_cmd_params(&rmh, pipe->is_capture, pipe->number, 0);
442 return vx_send_msg_nolock(chip, &rmh); /* no lock needed for trigger */
447 * vx_alloc_pipe - allocate a pipe and initialize the pipe instance
448 * @capture: 0 = playback, 1 = capture operation
449 * @audioid: the audio id to be assigned
450 * @num_audio: number of audio channels
451 * @pipep: the returned pipe instance
453 * return 0 on success, or a negative error code.
455 static int vx_alloc_pipe(struct vx_core *chip, int capture,
456 int audioid, int num_audio,
457 struct vx_pipe **pipep)
459 int err;
460 struct vx_pipe *pipe;
461 struct vx_rmh rmh;
462 int data_mode;
464 *pipep = NULL;
465 vx_init_rmh(&rmh, CMD_RES_PIPE);
466 vx_set_pipe_cmd_params(&rmh, capture, audioid, num_audio);
467 #if 0 // NYI
468 if (underrun_skip_sound)
469 rmh.Cmd[0] |= BIT_SKIP_SOUND;
470 #endif // NYI
471 data_mode = (chip->uer_bits & IEC958_AES0_NONAUDIO) != 0;
472 if (! capture && data_mode)
473 rmh.Cmd[0] |= BIT_DATA_MODE;
474 err = vx_send_msg(chip, &rmh);
475 if (err < 0)
476 return err;
478 /* initialize the pipe record */
479 pipe = kzalloc(sizeof(*pipe), GFP_KERNEL);
480 if (! pipe) {
481 /* release the pipe */
482 vx_init_rmh(&rmh, CMD_FREE_PIPE);
483 vx_set_pipe_cmd_params(&rmh, capture, audioid, 0);
484 vx_send_msg(chip, &rmh);
485 return -ENOMEM;
488 /* the pipe index should be identical with the audio index */
489 pipe->number = audioid;
490 pipe->is_capture = capture;
491 pipe->channels = num_audio;
492 pipe->differed_type = 0;
493 pipe->pcx_time = 0;
494 pipe->data_mode = data_mode;
495 *pipep = pipe;
497 return 0;
502 * vx_free_pipe - release a pipe
503 * @pipe: pipe to be released
505 static int vx_free_pipe(struct vx_core *chip, struct vx_pipe *pipe)
507 struct vx_rmh rmh;
509 vx_init_rmh(&rmh, CMD_FREE_PIPE);
510 vx_set_pipe_cmd_params(&rmh, pipe->is_capture, pipe->number, 0);
511 vx_send_msg(chip, &rmh);
513 kfree(pipe);
514 return 0;
519 * vx_start_stream - start the stream
521 * called from trigger callback only
523 static int vx_start_stream(struct vx_core *chip, struct vx_pipe *pipe)
525 struct vx_rmh rmh;
527 vx_init_rmh(&rmh, CMD_START_ONE_STREAM);
528 vx_set_stream_cmd_params(&rmh, pipe->is_capture, pipe->number);
529 vx_set_differed_time(chip, &rmh, pipe);
530 return vx_send_msg_nolock(chip, &rmh); /* no lock needed for trigger */
535 * vx_stop_stream - stop the stream
537 * called from trigger callback only
539 static int vx_stop_stream(struct vx_core *chip, struct vx_pipe *pipe)
541 struct vx_rmh rmh;
543 vx_init_rmh(&rmh, CMD_STOP_STREAM);
544 vx_set_stream_cmd_params(&rmh, pipe->is_capture, pipe->number);
545 return vx_send_msg_nolock(chip, &rmh); /* no lock needed for trigger */
550 * playback hw information
553 static struct snd_pcm_hardware vx_pcm_playback_hw = {
554 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
555 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_MMAP_VALID /*|*/
556 /*SNDRV_PCM_INFO_RESUME*/),
557 .formats = (/*SNDRV_PCM_FMTBIT_U8 |*/
558 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_3LE),
559 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
560 .rate_min = 5000,
561 .rate_max = 48000,
562 .channels_min = 1,
563 .channels_max = 2,
564 .buffer_bytes_max = (128*1024),
565 .period_bytes_min = 126,
566 .period_bytes_max = (128*1024),
567 .periods_min = 2,
568 .periods_max = VX_MAX_PERIODS,
569 .fifo_size = 126,
573 static void vx_pcm_delayed_start(unsigned long arg);
576 * vx_pcm_playback_open - open callback for playback
578 static int vx_pcm_playback_open(struct snd_pcm_substream *subs)
580 struct snd_pcm_runtime *runtime = subs->runtime;
581 struct vx_core *chip = snd_pcm_substream_chip(subs);
582 struct vx_pipe *pipe = NULL;
583 unsigned int audio;
584 int err;
586 if (chip->chip_status & VX_STAT_IS_STALE)
587 return -EBUSY;
589 audio = subs->pcm->device * 2;
590 if (snd_BUG_ON(audio >= chip->audio_outs))
591 return -EINVAL;
593 /* playback pipe may have been already allocated for monitoring */
594 pipe = chip->playback_pipes[audio];
595 if (! pipe) {
596 /* not allocated yet */
597 err = vx_alloc_pipe(chip, 0, audio, 2, &pipe); /* stereo playback */
598 if (err < 0)
599 return err;
600 chip->playback_pipes[audio] = pipe;
602 /* open for playback */
603 pipe->references++;
605 pipe->substream = subs;
606 tasklet_init(&pipe->start_tq, vx_pcm_delayed_start, (unsigned long)subs);
607 chip->playback_pipes[audio] = pipe;
609 runtime->hw = vx_pcm_playback_hw;
610 runtime->hw.period_bytes_min = chip->ibl.size;
611 runtime->private_data = pipe;
613 /* align to 4 bytes (otherwise will be problematic when 24bit is used) */
614 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 4);
615 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 4);
617 return 0;
621 * vx_pcm_playback_close - close callback for playback
623 static int vx_pcm_playback_close(struct snd_pcm_substream *subs)
625 struct vx_core *chip = snd_pcm_substream_chip(subs);
626 struct vx_pipe *pipe;
628 if (! subs->runtime->private_data)
629 return -EINVAL;
631 pipe = subs->runtime->private_data;
633 if (--pipe->references == 0) {
634 chip->playback_pipes[pipe->number] = NULL;
635 vx_free_pipe(chip, pipe);
638 return 0;
644 * vx_notify_end_of_buffer - send "end-of-buffer" notifier at the given pipe
645 * @pipe: the pipe to notify
647 * NB: call with a certain lock.
649 static int vx_notify_end_of_buffer(struct vx_core *chip, struct vx_pipe *pipe)
651 int err;
652 struct vx_rmh rmh; /* use a temporary rmh here */
654 /* Toggle Dsp Host Interface into Message mode */
655 vx_send_rih_nolock(chip, IRQ_PAUSE_START_CONNECT);
656 vx_init_rmh(&rmh, CMD_NOTIFY_END_OF_BUFFER);
657 vx_set_stream_cmd_params(&rmh, 0, pipe->number);
658 err = vx_send_msg_nolock(chip, &rmh);
659 if (err < 0)
660 return err;
661 /* Toggle Dsp Host Interface back to sound transfer mode */
662 vx_send_rih_nolock(chip, IRQ_PAUSE_START_CONNECT);
663 return 0;
667 * vx_pcm_playback_transfer_chunk - transfer a single chunk
668 * @subs: substream
669 * @pipe: the pipe to transfer
670 * @size: chunk size in bytes
672 * transfer a single buffer chunk. EOB notificaton is added after that.
673 * called from the interrupt handler, too.
675 * return 0 if ok.
677 static int vx_pcm_playback_transfer_chunk(struct vx_core *chip,
678 struct snd_pcm_runtime *runtime,
679 struct vx_pipe *pipe, int size)
681 int space, err = 0;
683 space = vx_query_hbuffer_size(chip, pipe);
684 if (space < 0) {
685 /* disconnect the host, SIZE_HBUF command always switches to the stream mode */
686 vx_send_rih(chip, IRQ_CONNECT_STREAM_NEXT);
687 snd_printd("error hbuffer\n");
688 return space;
690 if (space < size) {
691 vx_send_rih(chip, IRQ_CONNECT_STREAM_NEXT);
692 snd_printd("no enough hbuffer space %d\n", space);
693 return -EIO; /* XRUN */
696 /* we don't need irqsave here, because this function
697 * is called from either trigger callback or irq handler
699 spin_lock(&chip->lock);
700 vx_pseudo_dma_write(chip, runtime, pipe, size);
701 err = vx_notify_end_of_buffer(chip, pipe);
702 /* disconnect the host, SIZE_HBUF command always switches to the stream mode */
703 vx_send_rih_nolock(chip, IRQ_CONNECT_STREAM_NEXT);
704 spin_unlock(&chip->lock);
705 return err;
709 * update the position of the given pipe.
710 * pipe->position is updated and wrapped within the buffer size.
711 * pipe->transferred is updated, too, but the size is not wrapped,
712 * so that the caller can check the total transferred size later
713 * (to call snd_pcm_period_elapsed).
715 static int vx_update_pipe_position(struct vx_core *chip,
716 struct snd_pcm_runtime *runtime,
717 struct vx_pipe *pipe)
719 struct vx_rmh rmh;
720 int err, update;
721 u64 count;
723 vx_init_rmh(&rmh, CMD_STREAM_SAMPLE_COUNT);
724 vx_set_pipe_cmd_params(&rmh, pipe->is_capture, pipe->number, 0);
725 err = vx_send_msg(chip, &rmh);
726 if (err < 0)
727 return err;
729 count = ((u64)(rmh.Stat[0] & 0xfffff) << 24) | (u64)rmh.Stat[1];
730 update = (int)(count - pipe->cur_count);
731 pipe->cur_count = count;
732 pipe->position += update;
733 if (pipe->position >= (int)runtime->buffer_size)
734 pipe->position %= runtime->buffer_size;
735 pipe->transferred += update;
736 return 0;
740 * transfer the pending playback buffer data to DSP
741 * called from interrupt handler
743 static void vx_pcm_playback_transfer(struct vx_core *chip,
744 struct snd_pcm_substream *subs,
745 struct vx_pipe *pipe, int nchunks)
747 int i, err;
748 struct snd_pcm_runtime *runtime = subs->runtime;
750 if (! pipe->prepared || (chip->chip_status & VX_STAT_IS_STALE))
751 return;
752 for (i = 0; i < nchunks; i++) {
753 if ((err = vx_pcm_playback_transfer_chunk(chip, runtime, pipe,
754 chip->ibl.size)) < 0)
755 return;
760 * update the playback position and call snd_pcm_period_elapsed() if necessary
761 * called from interrupt handler
763 static void vx_pcm_playback_update(struct vx_core *chip,
764 struct snd_pcm_substream *subs,
765 struct vx_pipe *pipe)
767 int err;
768 struct snd_pcm_runtime *runtime = subs->runtime;
770 if (pipe->running && ! (chip->chip_status & VX_STAT_IS_STALE)) {
771 if ((err = vx_update_pipe_position(chip, runtime, pipe)) < 0)
772 return;
773 if (pipe->transferred >= (int)runtime->period_size) {
774 pipe->transferred %= runtime->period_size;
775 snd_pcm_period_elapsed(subs);
781 * start the stream and pipe.
782 * this function is called from tasklet, which is invoked by the trigger
783 * START callback.
785 static void vx_pcm_delayed_start(unsigned long arg)
787 struct snd_pcm_substream *subs = (struct snd_pcm_substream *)arg;
788 struct vx_core *chip = subs->pcm->private_data;
789 struct vx_pipe *pipe = subs->runtime->private_data;
790 int err;
792 /* printk( KERN_DEBUG "DDDD tasklet delayed start jiffies = %ld\n", jiffies);*/
794 if ((err = vx_start_stream(chip, pipe)) < 0) {
795 snd_printk(KERN_ERR "vx: cannot start stream\n");
796 return;
798 if ((err = vx_toggle_pipe(chip, pipe, 1)) < 0) {
799 snd_printk(KERN_ERR "vx: cannot start pipe\n");
800 return;
802 /* printk( KERN_DEBUG "dddd tasklet delayed start jiffies = %ld \n", jiffies);*/
806 * vx_pcm_playback_trigger - trigger callback for playback
808 static int vx_pcm_trigger(struct snd_pcm_substream *subs, int cmd)
810 struct vx_core *chip = snd_pcm_substream_chip(subs);
811 struct vx_pipe *pipe = subs->runtime->private_data;
812 int err;
814 if (chip->chip_status & VX_STAT_IS_STALE)
815 return -EBUSY;
817 switch (cmd) {
818 case SNDRV_PCM_TRIGGER_START:
819 case SNDRV_PCM_TRIGGER_RESUME:
820 if (! pipe->is_capture)
821 vx_pcm_playback_transfer(chip, subs, pipe, 2);
822 /* FIXME:
823 * we trigger the pipe using tasklet, so that the interrupts are
824 * issued surely after the trigger is completed.
826 tasklet_schedule(&pipe->start_tq);
827 chip->pcm_running++;
828 pipe->running = 1;
829 break;
830 case SNDRV_PCM_TRIGGER_STOP:
831 case SNDRV_PCM_TRIGGER_SUSPEND:
832 vx_toggle_pipe(chip, pipe, 0);
833 vx_stop_pipe(chip, pipe);
834 vx_stop_stream(chip, pipe);
835 chip->pcm_running--;
836 pipe->running = 0;
837 break;
838 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
839 if ((err = vx_toggle_pipe(chip, pipe, 0)) < 0)
840 return err;
841 break;
842 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
843 if ((err = vx_toggle_pipe(chip, pipe, 1)) < 0)
844 return err;
845 break;
846 default:
847 return -EINVAL;
849 return 0;
853 * vx_pcm_playback_pointer - pointer callback for playback
855 static snd_pcm_uframes_t vx_pcm_playback_pointer(struct snd_pcm_substream *subs)
857 struct snd_pcm_runtime *runtime = subs->runtime;
858 struct vx_pipe *pipe = runtime->private_data;
859 return pipe->position;
863 * vx_pcm_hw_params - hw_params callback for playback and capture
865 static int vx_pcm_hw_params(struct snd_pcm_substream *subs,
866 struct snd_pcm_hw_params *hw_params)
868 return snd_pcm_alloc_vmalloc_buffer(subs, params_buffer_bytes(hw_params));
872 * vx_pcm_hw_free - hw_free callback for playback and capture
874 static int vx_pcm_hw_free(struct snd_pcm_substream *subs)
876 return snd_pcm_free_vmalloc_buffer(subs);
880 * vx_pcm_prepare - prepare callback for playback and capture
882 static int vx_pcm_prepare(struct snd_pcm_substream *subs)
884 struct vx_core *chip = snd_pcm_substream_chip(subs);
885 struct snd_pcm_runtime *runtime = subs->runtime;
886 struct vx_pipe *pipe = runtime->private_data;
887 int err, data_mode;
888 // int max_size, nchunks;
890 if (chip->chip_status & VX_STAT_IS_STALE)
891 return -EBUSY;
893 data_mode = (chip->uer_bits & IEC958_AES0_NONAUDIO) != 0;
894 if (data_mode != pipe->data_mode && ! pipe->is_capture) {
895 /* IEC958 status (raw-mode) was changed */
896 /* we reopen the pipe */
897 struct vx_rmh rmh;
898 snd_printdd(KERN_DEBUG "reopen the pipe with data_mode = %d\n", data_mode);
899 vx_init_rmh(&rmh, CMD_FREE_PIPE);
900 vx_set_pipe_cmd_params(&rmh, 0, pipe->number, 0);
901 if ((err = vx_send_msg(chip, &rmh)) < 0)
902 return err;
903 vx_init_rmh(&rmh, CMD_RES_PIPE);
904 vx_set_pipe_cmd_params(&rmh, 0, pipe->number, pipe->channels);
905 if (data_mode)
906 rmh.Cmd[0] |= BIT_DATA_MODE;
907 if ((err = vx_send_msg(chip, &rmh)) < 0)
908 return err;
909 pipe->data_mode = data_mode;
912 if (chip->pcm_running && chip->freq != runtime->rate) {
913 snd_printk(KERN_ERR "vx: cannot set different clock %d "
914 "from the current %d\n", runtime->rate, chip->freq);
915 return -EINVAL;
917 vx_set_clock(chip, runtime->rate);
919 if ((err = vx_set_format(chip, pipe, runtime)) < 0)
920 return err;
922 if (vx_is_pcmcia(chip)) {
923 pipe->align = 2; /* 16bit word */
924 } else {
925 pipe->align = 4; /* 32bit word */
928 pipe->buffer_bytes = frames_to_bytes(runtime, runtime->buffer_size);
929 pipe->period_bytes = frames_to_bytes(runtime, runtime->period_size);
930 pipe->hw_ptr = 0;
932 /* set the timestamp */
933 vx_update_pipe_position(chip, runtime, pipe);
934 /* clear again */
935 pipe->transferred = 0;
936 pipe->position = 0;
938 pipe->prepared = 1;
940 return 0;
945 * operators for PCM playback
947 static struct snd_pcm_ops vx_pcm_playback_ops = {
948 .open = vx_pcm_playback_open,
949 .close = vx_pcm_playback_close,
950 .ioctl = snd_pcm_lib_ioctl,
951 .hw_params = vx_pcm_hw_params,
952 .hw_free = vx_pcm_hw_free,
953 .prepare = vx_pcm_prepare,
954 .trigger = vx_pcm_trigger,
955 .pointer = vx_pcm_playback_pointer,
956 .page = snd_pcm_get_vmalloc_page,
961 * playback hw information
964 static struct snd_pcm_hardware vx_pcm_capture_hw = {
965 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
966 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_MMAP_VALID /*|*/
967 /*SNDRV_PCM_INFO_RESUME*/),
968 .formats = (/*SNDRV_PCM_FMTBIT_U8 |*/
969 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_3LE),
970 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
971 .rate_min = 5000,
972 .rate_max = 48000,
973 .channels_min = 1,
974 .channels_max = 2,
975 .buffer_bytes_max = (128*1024),
976 .period_bytes_min = 126,
977 .period_bytes_max = (128*1024),
978 .periods_min = 2,
979 .periods_max = VX_MAX_PERIODS,
980 .fifo_size = 126,
985 * vx_pcm_capture_open - open callback for capture
987 static int vx_pcm_capture_open(struct snd_pcm_substream *subs)
989 struct snd_pcm_runtime *runtime = subs->runtime;
990 struct vx_core *chip = snd_pcm_substream_chip(subs);
991 struct vx_pipe *pipe;
992 struct vx_pipe *pipe_out_monitoring = NULL;
993 unsigned int audio;
994 int err;
996 if (chip->chip_status & VX_STAT_IS_STALE)
997 return -EBUSY;
999 audio = subs->pcm->device * 2;
1000 if (snd_BUG_ON(audio >= chip->audio_ins))
1001 return -EINVAL;
1002 err = vx_alloc_pipe(chip, 1, audio, 2, &pipe);
1003 if (err < 0)
1004 return err;
1005 pipe->substream = subs;
1006 tasklet_init(&pipe->start_tq, vx_pcm_delayed_start, (unsigned long)subs);
1007 chip->capture_pipes[audio] = pipe;
1009 /* check if monitoring is needed */
1010 if (chip->audio_monitor_active[audio]) {
1011 pipe_out_monitoring = chip->playback_pipes[audio];
1012 if (! pipe_out_monitoring) {
1013 /* allocate a pipe */
1014 err = vx_alloc_pipe(chip, 0, audio, 2, &pipe_out_monitoring);
1015 if (err < 0)
1016 return err;
1017 chip->playback_pipes[audio] = pipe_out_monitoring;
1019 pipe_out_monitoring->references++;
1021 if an output pipe is available, it's audios still may need to be
1022 unmuted. hence we'll have to call a mixer entry point.
1024 vx_set_monitor_level(chip, audio, chip->audio_monitor[audio],
1025 chip->audio_monitor_active[audio]);
1026 /* assuming stereo */
1027 vx_set_monitor_level(chip, audio+1, chip->audio_monitor[audio+1],
1028 chip->audio_monitor_active[audio+1]);
1031 pipe->monitoring_pipe = pipe_out_monitoring; /* default value NULL */
1033 runtime->hw = vx_pcm_capture_hw;
1034 runtime->hw.period_bytes_min = chip->ibl.size;
1035 runtime->private_data = pipe;
1037 /* align to 4 bytes (otherwise will be problematic when 24bit is used) */
1038 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 4);
1039 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 4);
1041 return 0;
1045 * vx_pcm_capture_close - close callback for capture
1047 static int vx_pcm_capture_close(struct snd_pcm_substream *subs)
1049 struct vx_core *chip = snd_pcm_substream_chip(subs);
1050 struct vx_pipe *pipe;
1051 struct vx_pipe *pipe_out_monitoring;
1053 if (! subs->runtime->private_data)
1054 return -EINVAL;
1055 pipe = subs->runtime->private_data;
1056 chip->capture_pipes[pipe->number] = NULL;
1058 pipe_out_monitoring = pipe->monitoring_pipe;
1061 if an output pipe is attached to this input,
1062 check if it needs to be released.
1064 if (pipe_out_monitoring) {
1065 if (--pipe_out_monitoring->references == 0) {
1066 vx_free_pipe(chip, pipe_out_monitoring);
1067 chip->playback_pipes[pipe->number] = NULL;
1068 pipe->monitoring_pipe = NULL;
1072 vx_free_pipe(chip, pipe);
1073 return 0;
1078 #define DMA_READ_ALIGN 6 /* hardware alignment for read */
1081 * vx_pcm_capture_update - update the capture buffer
1083 static void vx_pcm_capture_update(struct vx_core *chip, struct snd_pcm_substream *subs,
1084 struct vx_pipe *pipe)
1086 int size, space, count;
1087 struct snd_pcm_runtime *runtime = subs->runtime;
1089 if (! pipe->prepared || (chip->chip_status & VX_STAT_IS_STALE))
1090 return;
1092 size = runtime->buffer_size - snd_pcm_capture_avail(runtime);
1093 if (! size)
1094 return;
1095 size = frames_to_bytes(runtime, size);
1096 space = vx_query_hbuffer_size(chip, pipe);
1097 if (space < 0)
1098 goto _error;
1099 if (size > space)
1100 size = space;
1101 size = (size / 3) * 3; /* align to 3 bytes */
1102 if (size < DMA_READ_ALIGN)
1103 goto _error;
1105 /* keep the last 6 bytes, they will be read after disconnection */
1106 count = size - DMA_READ_ALIGN;
1107 /* read bytes until the current pointer reaches to the aligned position
1108 * for word-transfer
1110 while (count > 0) {
1111 if ((pipe->hw_ptr % pipe->align) == 0)
1112 break;
1113 if (vx_wait_for_rx_full(chip) < 0)
1114 goto _error;
1115 vx_pcm_read_per_bytes(chip, runtime, pipe);
1116 count -= 3;
1118 if (count > 0) {
1119 /* ok, let's accelerate! */
1120 int align = pipe->align * 3;
1121 space = (count / align) * align;
1122 vx_pseudo_dma_read(chip, runtime, pipe, space);
1123 count -= space;
1125 /* read the rest of bytes */
1126 while (count > 0) {
1127 if (vx_wait_for_rx_full(chip) < 0)
1128 goto _error;
1129 vx_pcm_read_per_bytes(chip, runtime, pipe);
1130 count -= 3;
1132 /* disconnect the host, SIZE_HBUF command always switches to the stream mode */
1133 vx_send_rih_nolock(chip, IRQ_CONNECT_STREAM_NEXT);
1134 /* read the last pending 6 bytes */
1135 count = DMA_READ_ALIGN;
1136 while (count > 0) {
1137 vx_pcm_read_per_bytes(chip, runtime, pipe);
1138 count -= 3;
1140 /* update the position */
1141 pipe->transferred += size;
1142 if (pipe->transferred >= pipe->period_bytes) {
1143 pipe->transferred %= pipe->period_bytes;
1144 snd_pcm_period_elapsed(subs);
1146 return;
1148 _error:
1149 /* disconnect the host, SIZE_HBUF command always switches to the stream mode */
1150 vx_send_rih_nolock(chip, IRQ_CONNECT_STREAM_NEXT);
1151 return;
1155 * vx_pcm_capture_pointer - pointer callback for capture
1157 static snd_pcm_uframes_t vx_pcm_capture_pointer(struct snd_pcm_substream *subs)
1159 struct snd_pcm_runtime *runtime = subs->runtime;
1160 struct vx_pipe *pipe = runtime->private_data;
1161 return bytes_to_frames(runtime, pipe->hw_ptr);
1165 * operators for PCM capture
1167 static struct snd_pcm_ops vx_pcm_capture_ops = {
1168 .open = vx_pcm_capture_open,
1169 .close = vx_pcm_capture_close,
1170 .ioctl = snd_pcm_lib_ioctl,
1171 .hw_params = vx_pcm_hw_params,
1172 .hw_free = vx_pcm_hw_free,
1173 .prepare = vx_pcm_prepare,
1174 .trigger = vx_pcm_trigger,
1175 .pointer = vx_pcm_capture_pointer,
1176 .page = snd_pcm_get_vmalloc_page,
1181 * interrupt handler for pcm streams
1183 void vx_pcm_update_intr(struct vx_core *chip, unsigned int events)
1185 unsigned int i;
1186 struct vx_pipe *pipe;
1188 #define EVENT_MASK (END_OF_BUFFER_EVENTS_PENDING|ASYNC_EVENTS_PENDING)
1190 if (events & EVENT_MASK) {
1191 vx_init_rmh(&chip->irq_rmh, CMD_ASYNC);
1192 if (events & ASYNC_EVENTS_PENDING)
1193 chip->irq_rmh.Cmd[0] |= 0x00000001; /* SEL_ASYNC_EVENTS */
1194 if (events & END_OF_BUFFER_EVENTS_PENDING)
1195 chip->irq_rmh.Cmd[0] |= 0x00000002; /* SEL_END_OF_BUF_EVENTS */
1197 if (vx_send_msg(chip, &chip->irq_rmh) < 0) {
1198 snd_printdd(KERN_ERR "msg send error!!\n");
1199 return;
1202 i = 1;
1203 while (i < chip->irq_rmh.LgStat) {
1204 int p, buf, capture, eob;
1205 p = chip->irq_rmh.Stat[i] & MASK_FIRST_FIELD;
1206 capture = (chip->irq_rmh.Stat[i] & 0x400000) ? 1 : 0;
1207 eob = (chip->irq_rmh.Stat[i] & 0x800000) ? 1 : 0;
1208 i++;
1209 if (events & ASYNC_EVENTS_PENDING)
1210 i++;
1211 buf = 1; /* force to transfer */
1212 if (events & END_OF_BUFFER_EVENTS_PENDING) {
1213 if (eob)
1214 buf = chip->irq_rmh.Stat[i];
1215 i++;
1217 if (capture)
1218 continue;
1219 if (snd_BUG_ON(p < 0 || p >= chip->audio_outs))
1220 continue;
1221 pipe = chip->playback_pipes[p];
1222 if (pipe && pipe->substream) {
1223 vx_pcm_playback_update(chip, pipe->substream, pipe);
1224 vx_pcm_playback_transfer(chip, pipe->substream, pipe, buf);
1229 /* update the capture pcm pointers as frequently as possible */
1230 for (i = 0; i < chip->audio_ins; i++) {
1231 pipe = chip->capture_pipes[i];
1232 if (pipe && pipe->substream)
1233 vx_pcm_capture_update(chip, pipe->substream, pipe);
1239 * vx_init_audio_io - check the availabe audio i/o and allocate pipe arrays
1241 static int vx_init_audio_io(struct vx_core *chip)
1243 struct vx_rmh rmh;
1244 int preferred;
1246 vx_init_rmh(&rmh, CMD_SUPPORTED);
1247 if (vx_send_msg(chip, &rmh) < 0) {
1248 snd_printk(KERN_ERR "vx: cannot get the supported audio data\n");
1249 return -ENXIO;
1252 chip->audio_outs = rmh.Stat[0] & MASK_FIRST_FIELD;
1253 chip->audio_ins = (rmh.Stat[0] >> (FIELD_SIZE*2)) & MASK_FIRST_FIELD;
1254 chip->audio_info = rmh.Stat[1];
1256 /* allocate pipes */
1257 chip->playback_pipes = kcalloc(chip->audio_outs, sizeof(struct vx_pipe *), GFP_KERNEL);
1258 if (!chip->playback_pipes)
1259 return -ENOMEM;
1260 chip->capture_pipes = kcalloc(chip->audio_ins, sizeof(struct vx_pipe *), GFP_KERNEL);
1261 if (!chip->capture_pipes) {
1262 kfree(chip->playback_pipes);
1263 return -ENOMEM;
1266 preferred = chip->ibl.size;
1267 chip->ibl.size = 0;
1268 vx_set_ibl(chip, &chip->ibl); /* query the info */
1269 if (preferred > 0) {
1270 chip->ibl.size = ((preferred + chip->ibl.granularity - 1) /
1271 chip->ibl.granularity) * chip->ibl.granularity;
1272 if (chip->ibl.size > chip->ibl.max_size)
1273 chip->ibl.size = chip->ibl.max_size;
1274 } else
1275 chip->ibl.size = chip->ibl.min_size; /* set to the minimum */
1276 vx_set_ibl(chip, &chip->ibl);
1278 return 0;
1283 * free callback for pcm
1285 static void snd_vx_pcm_free(struct snd_pcm *pcm)
1287 struct vx_core *chip = pcm->private_data;
1288 chip->pcm[pcm->device] = NULL;
1289 kfree(chip->playback_pipes);
1290 chip->playback_pipes = NULL;
1291 kfree(chip->capture_pipes);
1292 chip->capture_pipes = NULL;
1296 * snd_vx_pcm_new - create and initialize a pcm
1298 int snd_vx_pcm_new(struct vx_core *chip)
1300 struct snd_pcm *pcm;
1301 unsigned int i;
1302 int err;
1304 if ((err = vx_init_audio_io(chip)) < 0)
1305 return err;
1307 for (i = 0; i < chip->hw->num_codecs; i++) {
1308 unsigned int outs, ins;
1309 outs = chip->audio_outs > i * 2 ? 1 : 0;
1310 ins = chip->audio_ins > i * 2 ? 1 : 0;
1311 if (! outs && ! ins)
1312 break;
1313 err = snd_pcm_new(chip->card, "VX PCM", i,
1314 outs, ins, &pcm);
1315 if (err < 0)
1316 return err;
1317 if (outs)
1318 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &vx_pcm_playback_ops);
1319 if (ins)
1320 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &vx_pcm_capture_ops);
1322 pcm->private_data = chip;
1323 pcm->private_free = snd_vx_pcm_free;
1324 pcm->info_flags = 0;
1325 strcpy(pcm->name, chip->card->shortname);
1326 chip->pcm[i] = pcm;
1329 return 0;