1/*
2 * Driver for Digigram VX soundcards
3 *
4 * PCM part
5 *
6 * Copyright (c) 2002,2003 by Takashi Iwai <tiwai@suse.de>
7 *
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.
12 *
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.
17 *
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
21 *
22 *
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.
27 *
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.
32 *
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.
38 *
39 *  the current point of read buffer is kept in pipe->hw_ptr.  note that
40 *  this is in bytes.
41 *
42 *
43 * TODO
44 *  - linked trigger for full-duplex mode.
45 *  - scheduled action on the stream.
46 */
47
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"
57
58
59/*
60 * we use a vmalloc'ed (sg-)buffer
61 */
62
63/* get the physical page pointer on the given offset */
64static struct page *snd_pcm_get_vmalloc_page(struct snd_pcm_substream *subs,
65					     unsigned long offset)
66{
67	void *pageptr = subs->runtime->dma_area + offset;
68	return vmalloc_to_page(pageptr);
69}
70
71/*
72 * allocate a buffer via vmalloc_32().
73 * called from hw_params
74 * NOTE: this may be called not only once per pcm open!
75 */
76static int snd_pcm_alloc_vmalloc_buffer(struct snd_pcm_substream *subs, size_t size)
77{
78	struct snd_pcm_runtime *runtime = subs->runtime;
79	if (runtime->dma_area) {
80		/* already allocated */
81		if (runtime->dma_bytes >= size)
82			return 0; /* already enough large */
83		vfree(runtime->dma_area);
84	}
85	runtime->dma_area = vmalloc_32(size);
86	if (! runtime->dma_area)
87		return -ENOMEM;
88	memset(runtime->dma_area, 0, size);
89	runtime->dma_bytes = size;
90	return 1; /* changed */
91}
92
93/*
94 * free the buffer.
95 * called from hw_free callback
96 * NOTE: this may be called not only once per pcm open!
97 */
98static int snd_pcm_free_vmalloc_buffer(struct snd_pcm_substream *subs)
99{
100	struct snd_pcm_runtime *runtime = subs->runtime;
101
102	vfree(runtime->dma_area);
103	runtime->dma_area = NULL;
104	return 0;
105}
106
107
108/*
109 * read three pending pcm bytes via inb()
110 */
111static void vx_pcm_read_per_bytes(struct vx_core *chip, struct snd_pcm_runtime *runtime,
112				  struct vx_pipe *pipe)
113{
114	int offset = pipe->hw_ptr;
115	unsigned char *buf = (unsigned char *)(runtime->dma_area + offset);
116	*buf++ = vx_inb(chip, RXH);
117	if (++offset >= pipe->buffer_bytes) {
118		offset = 0;
119		buf = (unsigned char *)runtime->dma_area;
120	}
121	*buf++ = vx_inb(chip, RXM);
122	if (++offset >= pipe->buffer_bytes) {
123		offset = 0;
124		buf = (unsigned char *)runtime->dma_area;
125	}
126	*buf++ = vx_inb(chip, RXL);
127	if (++offset >= pipe->buffer_bytes) {
128		offset = 0;
129		buf = (unsigned char *)runtime->dma_area;
130	}
131	pipe->hw_ptr = offset;
132}
133
134/*
135 * vx_set_pcx_time - convert from the PC time to the RMH status time.
136 * @pc_time: the pointer for the PC-time to set
137 * @dsp_time: the pointer for RMH status time array
138 */
139static void vx_set_pcx_time(struct vx_core *chip, pcx_time_t *pc_time,
140			    unsigned int *dsp_time)
141{
142	dsp_time[0] = (unsigned int)((*pc_time) >> 24) & PCX_TIME_HI_MASK;
143	dsp_time[1] = (unsigned int)(*pc_time) &  MASK_DSP_WORD;
144}
145
146/*
147 * vx_set_differed_time - set the differed time if specified
148 * @rmh: the rmh record to modify
149 * @pipe: the pipe to be checked
150 *
151 * if the pipe is programmed with the differed time, set the DSP time
152 * on the rmh and changes its command length.
153 *
154 * returns the increase of the command length.
155 */
156static int vx_set_differed_time(struct vx_core *chip, struct vx_rmh *rmh,
157				struct vx_pipe *pipe)
158{
159	/* Update The length added to the RMH command by the timestamp */
160	if (! (pipe->differed_type & DC_DIFFERED_DELAY))
161		return 0;
162
163	/* Set the T bit */
164	rmh->Cmd[0] |= DSP_DIFFERED_COMMAND_MASK;
165
166	/* Time stamp is the 1st following parameter */
167	vx_set_pcx_time(chip, &pipe->pcx_time, &rmh->Cmd[1]);
168
169	/* Add the flags to a notified differed command */
170	if (pipe->differed_type & DC_NOTIFY_DELAY)
171		rmh->Cmd[1] |= NOTIFY_MASK_TIME_HIGH ;
172
173	/* Add the flags to a multiple differed command */
174	if (pipe->differed_type & DC_MULTIPLE_DELAY)
175		rmh->Cmd[1] |= MULTIPLE_MASK_TIME_HIGH;
176
177	/* Add the flags to a stream-time differed command */
178	if (pipe->differed_type & DC_STREAM_TIME_DELAY)
179		rmh->Cmd[1] |= STREAM_MASK_TIME_HIGH;
180
181	rmh->LgCmd += 2;
182	return 2;
183}
184
185/*
186 * vx_set_stream_format - send the stream format command
187 * @pipe: the affected pipe
188 * @data: format bitmask
189 */
190static int vx_set_stream_format(struct vx_core *chip, struct vx_pipe *pipe,
191				unsigned int data)
192{
193	struct vx_rmh rmh;
194
195	vx_init_rmh(&rmh, pipe->is_capture ?
196		    CMD_FORMAT_STREAM_IN : CMD_FORMAT_STREAM_OUT);
197	rmh.Cmd[0] |= pipe->number << FIELD_SIZE;
198
199        /* Command might be longer since we may have to add a timestamp */
200	vx_set_differed_time(chip, &rmh, pipe);
201
202	rmh.Cmd[rmh.LgCmd] = (data & 0xFFFFFF00) >> 8;
203	rmh.Cmd[rmh.LgCmd + 1] = (data & 0xFF) << 16 /*| (datal & 0xFFFF00) >> 8*/;
204	rmh.LgCmd += 2;
205
206	return vx_send_msg(chip, &rmh);
207}
208
209
210/*
211 * vx_set_format - set the format of a pipe
212 * @pipe: the affected pipe
213 * @runtime: pcm runtime instance to be referred
214 *
215 * returns 0 if successful, or a negative error code.
216 */
217static int vx_set_format(struct vx_core *chip, struct vx_pipe *pipe,
218			 struct snd_pcm_runtime *runtime)
219{
220	unsigned int header = HEADER_FMT_BASE;
221
222	if (runtime->channels == 1)
223		header |= HEADER_FMT_MONO;
224	if (snd_pcm_format_little_endian(runtime->format))
225		header |= HEADER_FMT_INTEL;
226	if (runtime->rate < 32000 && runtime->rate > 11025)
227		header |= HEADER_FMT_UPTO32;
228	else if (runtime->rate <= 11025)
229		header |= HEADER_FMT_UPTO11;
230
231	switch (snd_pcm_format_physical_width(runtime->format)) {
232	// case 8: break;
233	case 16: header |= HEADER_FMT_16BITS; break;
234	case 24: header |= HEADER_FMT_24BITS; break;
235	default :
236		snd_BUG();
237		return -EINVAL;
238        };
239
240	return vx_set_stream_format(chip, pipe, header);
241}
242
243/*
244 * set / query the IBL size
245 */
246static int vx_set_ibl(struct vx_core *chip, struct vx_ibl_info *info)
247{
248	int err;
249	struct vx_rmh rmh;
250
251	vx_init_rmh(&rmh, CMD_IBL);
252	rmh.Cmd[0] |= info->size & 0x03ffff;
253	err = vx_send_msg(chip, &rmh);
254	if (err < 0)
255		return err;
256	info->size = rmh.Stat[0];
257	info->max_size = rmh.Stat[1];
258	info->min_size = rmh.Stat[2];
259	info->granularity = rmh.Stat[3];
260	snd_printdd(KERN_DEBUG "vx_set_ibl: size = %d, max = %d, min = %d, gran = %d\n",
261		   info->size, info->max_size, info->min_size, info->granularity);
262	return 0;
263}
264
265
266/*
267 * vx_get_pipe_state - get the state of a pipe
268 * @pipe: the pipe to be checked
269 * @state: the pointer for the returned state
270 *
271 * checks the state of a given pipe, and stores the state (1 = running,
272 * 0 = paused) on the given pointer.
273 *
274 * called from trigger callback only
275 */
276static int vx_get_pipe_state(struct vx_core *chip, struct vx_pipe *pipe, int *state)
277{
278	int err;
279	struct vx_rmh rmh;
280
281	vx_init_rmh(&rmh, CMD_PIPE_STATE);
282	vx_set_pipe_cmd_params(&rmh, pipe->is_capture, pipe->number, 0);
283	err = vx_send_msg_nolock(chip, &rmh); /* no lock needed for trigger */
284	if (! err)
285		*state = (rmh.Stat[0] & (1 << pipe->number)) ? 1 : 0;
286	return err;
287}
288
289
290/*
291 * vx_query_hbuffer_size - query available h-buffer size in bytes
292 * @pipe: the pipe to be checked
293 *
294 * return the available size on h-buffer in bytes,
295 * or a negative error code.
296 *
297 * NOTE: calling this function always switches to the stream mode.
298 *       you'll need to disconnect the host to get back to the
299 *       normal mode.
300 */
301static int vx_query_hbuffer_size(struct vx_core *chip, struct vx_pipe *pipe)
302{
303	int result;
304	struct vx_rmh rmh;
305
306	vx_init_rmh(&rmh, CMD_SIZE_HBUFFER);
307	vx_set_pipe_cmd_params(&rmh, pipe->is_capture, pipe->number, 0);
308	if (pipe->is_capture)
309		rmh.Cmd[0] |= 0x00000001;
310	result = vx_send_msg(chip, &rmh);
311	if (! result)
312		result = rmh.Stat[0] & 0xffff;
313	return result;
314}
315
316
317/*
318 * vx_pipe_can_start - query whether a pipe is ready for start
319 * @pipe: the pipe to be checked
320 *
321 * return 1 if ready, 0 if not ready, and negative value on error.
322 *
323 * called from trigger callback only
324 */
325static int vx_pipe_can_start(struct vx_core *chip, struct vx_pipe *pipe)
326{
327	int err;
328	struct vx_rmh rmh;
329
330	vx_init_rmh(&rmh, CMD_CAN_START_PIPE);
331	vx_set_pipe_cmd_params(&rmh, pipe->is_capture, pipe->number, 0);
332	rmh.Cmd[0] |= 1;
333
334	err = vx_send_msg_nolock(chip, &rmh); /* no lock needed for trigger */
335	if (! err) {
336		if (rmh.Stat[0])
337			err = 1;
338	}
339	return err;
340}
341
342/*
343 * vx_conf_pipe - tell the pipe to stand by and wait for IRQA.
344 * @pipe: the pipe to be configured
345 */
346static int vx_conf_pipe(struct vx_core *chip, struct vx_pipe *pipe)
347{
348	struct vx_rmh rmh;
349
350	vx_init_rmh(&rmh, CMD_CONF_PIPE);
351	if (pipe->is_capture)
352		rmh.Cmd[0] |= COMMAND_RECORD_MASK;
353	rmh.Cmd[1] = 1 << pipe->number;
354	return vx_send_msg_nolock(chip, &rmh); /* no lock needed for trigger */
355}
356
357/*
358 * vx_send_irqa - trigger IRQA
359 */
360static int vx_send_irqa(struct vx_core *chip)
361{
362	struct vx_rmh rmh;
363
364	vx_init_rmh(&rmh, CMD_SEND_IRQA);
365	return vx_send_msg_nolock(chip, &rmh); /* no lock needed for trigger */
366}
367
368
369#define MAX_WAIT_FOR_DSP        250
370/*
371 * vx boards do not support inter-card sync, besides
372 * only 126 samples require to be prepared before a pipe can start
373 */
374#define CAN_START_DELAY         2	/* wait 2ms only before asking if the pipe is ready*/
375#define WAIT_STATE_DELAY        2	/* wait 2ms after irqA was requested and check if the pipe state toggled*/
376
377/*
378 * vx_toggle_pipe - start / pause a pipe
379 * @pipe: the pipe to be triggered
380 * @state: start = 1, pause = 0
381 *
382 * called from trigger callback only
383 *
384 */
385static int vx_toggle_pipe(struct vx_core *chip, struct vx_pipe *pipe, int state)
386{
387	int err, i, cur_state;
388
389	/* Check the pipe is not already in the requested state */
390	if (vx_get_pipe_state(chip, pipe, &cur_state) < 0)
391		return -EBADFD;
392	if (state == cur_state)
393		return 0;
394
395	/* If a start is requested, ask the DSP to get prepared
396	 * and wait for a positive acknowledge (when there are
397	 * enough sound buffer for this pipe)
398	 */
399	if (state) {
400		for (i = 0 ; i < MAX_WAIT_FOR_DSP; i++) {
401			err = vx_pipe_can_start(chip, pipe);
402			if (err > 0)
403				break;
404			/* Wait for a few, before asking again
405			 * to avoid flooding the DSP with our requests
406			 */
407			mdelay(1);
408		}
409	}
410
411	if ((err = vx_conf_pipe(chip, pipe)) < 0)
412		return err;
413
414	if ((err = vx_send_irqa(chip)) < 0)
415		return err;
416
417	/* If it completes successfully, wait for the pipes
418	 * reaching the expected state before returning
419	 * Check one pipe only (since they are synchronous)
420	 */
421	for (i = 0; i < MAX_WAIT_FOR_DSP; i++) {
422		err = vx_get_pipe_state(chip, pipe, &cur_state);
423		if (err < 0 || cur_state == state)
424			break;
425		err = -EIO;
426		mdelay(1);
427	}
428	return err < 0 ? -EIO : 0;
429}
430
431
432/*
433 * vx_stop_pipe - stop a pipe
434 * @pipe: the pipe to be stopped
435 *
436 * called from trigger callback only
437 */
438static int vx_stop_pipe(struct vx_core *chip, struct vx_pipe *pipe)
439{
440	struct vx_rmh rmh;
441	vx_init_rmh(&rmh, CMD_STOP_PIPE);
442	vx_set_pipe_cmd_params(&rmh, pipe->is_capture, pipe->number, 0);
443	return vx_send_msg_nolock(chip, &rmh); /* no lock needed for trigger */
444}
445
446
447/*
448 * vx_alloc_pipe - allocate a pipe and initialize the pipe instance
449 * @capture: 0 = playback, 1 = capture operation
450 * @audioid: the audio id to be assigned
451 * @num_audio: number of audio channels
452 * @pipep: the returned pipe instance
453 *
454 * return 0 on success, or a negative error code.
455 */
456static int vx_alloc_pipe(struct vx_core *chip, int capture,
457			 int audioid, int num_audio,
458			 struct vx_pipe **pipep)
459{
460	int err;
461	struct vx_pipe *pipe;
462	struct vx_rmh rmh;
463	int data_mode;
464
465	*pipep = NULL;
466	vx_init_rmh(&rmh, CMD_RES_PIPE);
467	vx_set_pipe_cmd_params(&rmh, capture, audioid, num_audio);
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;
474
475	/* initialize the pipe record */
476	pipe = kzalloc(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;
483	}
484
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;
493
494	return 0;
495}
496
497
498/*
499 * vx_free_pipe - release a pipe
500 * @pipe: pipe to be released
501 */
502static int vx_free_pipe(struct vx_core *chip, struct vx_pipe *pipe)
503{
504	struct vx_rmh rmh;
505
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);
509
510	kfree(pipe);
511	return 0;
512}
513
514
515/*
516 * vx_start_stream - start the stream
517 *
518 * called from trigger callback only
519 */
520static int vx_start_stream(struct vx_core *chip, struct vx_pipe *pipe)
521{
522	struct vx_rmh rmh;
523
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 */
528}
529
530
531/*
532 * vx_stop_stream - stop the stream
533 *
534 * called from trigger callback only
535 */
536static int vx_stop_stream(struct vx_core *chip, struct vx_pipe *pipe)
537{
538	struct vx_rmh rmh;
539
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 */
543}
544
545
546/*
547 * playback hw information
548 */
549
550static struct snd_pcm_hardware 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 |*/
555				 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_3LE),
556	.rates =		SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
557	.rate_min =		5000,
558	.rate_max =		48000,
559	.channels_min =		1,
560	.channels_max =		2,
561	.buffer_bytes_max =	(128*1024),
562	.period_bytes_min =	126,
563	.period_bytes_max =	(128*1024),
564	.periods_min =		2,
565	.periods_max =		VX_MAX_PERIODS,
566	.fifo_size =		126,
567};
568
569
570static void vx_pcm_delayed_start(unsigned long arg);
571
572/*
573 * vx_pcm_playback_open - open callback for playback
574 */
575static int vx_pcm_playback_open(struct snd_pcm_substream *subs)
576{
577	struct snd_pcm_runtime *runtime = subs->runtime;
578	struct vx_core *chip = snd_pcm_substream_chip(subs);
579	struct vx_pipe *pipe = NULL;
580	unsigned int audio;
581	int err;
582
583	if (chip->chip_status & VX_STAT_IS_STALE)
584		return -EBUSY;
585
586	audio = subs->pcm->device * 2;
587	snd_assert(audio < chip->audio_outs, return -EINVAL);
588
589	/* playback pipe may have been already allocated for monitoring */
590	pipe = chip->playback_pipes[audio];
591	if (! pipe) {
592		/* not allocated yet */
593		err = vx_alloc_pipe(chip, 0, audio, 2, &pipe); /* stereo playback */
594		if (err < 0)
595			return err;
596		chip->playback_pipes[audio] = pipe;
597	}
598	/* open for playback */
599	pipe->references++;
600
601	pipe->substream = subs;
602	tasklet_init(&pipe->start_tq, vx_pcm_delayed_start, (unsigned long)subs);
603	chip->playback_pipes[audio] = pipe;
604
605	runtime->hw = vx_pcm_playback_hw;
606	runtime->hw.period_bytes_min = chip->ibl.size;
607	runtime->private_data = pipe;
608
609	/* align to 4 bytes (otherwise will be problematic when 24bit is used) */
610	snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 4);
611	snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 4);
612
613	return 0;
614}
615
616/*
617 * vx_pcm_playback_close - close callback for playback
618 */
619static int vx_pcm_playback_close(struct snd_pcm_substream *subs)
620{
621	struct vx_core *chip = snd_pcm_substream_chip(subs);
622	struct vx_pipe *pipe;
623
624	if (! subs->runtime->private_data)
625		return -EINVAL;
626
627	pipe = subs->runtime->private_data;
628
629	if (--pipe->references == 0) {
630		chip->playback_pipes[pipe->number] = NULL;
631		vx_free_pipe(chip, pipe);
632	}
633
634	return 0;
635
636}
637
638
639/*
640 * vx_notify_end_of_buffer - send "end-of-buffer" notifier at the given pipe
641 * @pipe: the pipe to notify
642 *
643 * NB: call with a certain lock.
644 */
645static int vx_notify_end_of_buffer(struct vx_core *chip, struct vx_pipe *pipe)
646{
647	int err;
648	struct vx_rmh rmh;  /* use a temporary rmh here */
649
650	/* Toggle Dsp Host Interface into Message mode */
651	vx_send_rih_nolock(chip, IRQ_PAUSE_START_CONNECT);
652	vx_init_rmh(&rmh, CMD_NOTIFY_END_OF_BUFFER);
653	vx_set_stream_cmd_params(&rmh, 0, pipe->number);
654	err = vx_send_msg_nolock(chip, &rmh);
655	if (err < 0)
656		return err;
657	/* Toggle Dsp Host Interface back to sound transfer mode */
658	vx_send_rih_nolock(chip, IRQ_PAUSE_START_CONNECT);
659	return 0;
660}
661
662/*
663 * vx_pcm_playback_transfer_chunk - transfer a single chunk
664 * @subs: substream
665 * @pipe: the pipe to transfer
666 * @size: chunk size in bytes
667 *
668 * transfer a single buffer chunk.  EOB notificaton is added after that.
669 * called from the interrupt handler, too.
670 *
671 * return 0 if ok.
672 */
673static int vx_pcm_playback_transfer_chunk(struct vx_core *chip,
674					  struct snd_pcm_runtime *runtime,
675					  struct vx_pipe *pipe, int size)
676{
677	int space, err = 0;
678
679	space = vx_query_hbuffer_size(chip, pipe);
680	if (space < 0) {
681		/* disconnect the host, SIZE_HBUF command always switches to the stream mode */
682		vx_send_rih(chip, IRQ_CONNECT_STREAM_NEXT);
683		snd_printd("error hbuffer\n");
684		return space;
685	}
686	if (space < size) {
687		vx_send_rih(chip, IRQ_CONNECT_STREAM_NEXT);
688		snd_printd("no enough hbuffer space %d\n", space);
689		return -EIO; /* XRUN */
690	}
691
692	/* we don't need irqsave here, because this function
693	 * is called from either trigger callback or irq handler
694	 */
695	spin_lock(&chip->lock);
696	vx_pseudo_dma_write(chip, runtime, pipe, size);
697	err = vx_notify_end_of_buffer(chip, pipe);
698	/* disconnect the host, SIZE_HBUF command always switches to the stream mode */
699	vx_send_rih_nolock(chip, IRQ_CONNECT_STREAM_NEXT);
700	spin_unlock(&chip->lock);
701	return err;
702}
703
704/*
705 * update the position of the given pipe.
706 * pipe->position is updated and wrapped within the buffer size.
707 * pipe->transferred is updated, too, but the size is not wrapped,
708 * so that the caller can check the total transferred size later
709 * (to call snd_pcm_period_elapsed).
710 */
711static int vx_update_pipe_position(struct vx_core *chip,
712				   struct snd_pcm_runtime *runtime,
713				   struct vx_pipe *pipe)
714{
715	struct vx_rmh rmh;
716	int err, update;
717	u64 count;
718
719	vx_init_rmh(&rmh, CMD_STREAM_SAMPLE_COUNT);
720	vx_set_pipe_cmd_params(&rmh, pipe->is_capture, pipe->number, 0);
721	err = vx_send_msg(chip, &rmh);
722	if (err < 0)
723		return err;
724
725	count = ((u64)(rmh.Stat[0] & 0xfffff) << 24) | (u64)rmh.Stat[1];
726	update = (int)(count - pipe->cur_count);
727	pipe->cur_count = count;
728	pipe->position += update;
729	if (pipe->position >= (int)runtime->buffer_size)
730		pipe->position %= runtime->buffer_size;
731	pipe->transferred += update;
732	return 0;
733}
734
735/*
736 * transfer the pending playback buffer data to DSP
737 * called from interrupt handler
738 */
739static void vx_pcm_playback_transfer(struct vx_core *chip,
740				     struct snd_pcm_substream *subs,
741				     struct vx_pipe *pipe, int nchunks)
742{
743	int i, err;
744	struct snd_pcm_runtime *runtime = subs->runtime;
745
746	if (! pipe->prepared || (chip->chip_status & VX_STAT_IS_STALE))
747		return;
748	for (i = 0; i < nchunks; i++) {
749		if ((err = vx_pcm_playback_transfer_chunk(chip, runtime, pipe,
750							  chip->ibl.size)) < 0)
751			return;
752	}
753}
754
755/*
756 * update the playback position and call snd_pcm_period_elapsed() if necessary
757 * called from interrupt handler
758 */
759static void vx_pcm_playback_update(struct vx_core *chip,
760				   struct snd_pcm_substream *subs,
761				   struct vx_pipe *pipe)
762{
763	int err;
764	struct snd_pcm_runtime *runtime = subs->runtime;
765
766	if (pipe->running && ! (chip->chip_status & VX_STAT_IS_STALE)) {
767		if ((err = vx_update_pipe_position(chip, runtime, pipe)) < 0)
768			return;
769		if (pipe->transferred >= (int)runtime->period_size) {
770			pipe->transferred %= runtime->period_size;
771			snd_pcm_period_elapsed(subs);
772		}
773	}
774}
775
776/*
777 * start the stream and pipe.
778 * this function is called from tasklet, which is invoked by the trigger
779 * START callback.
780 */
781static void vx_pcm_delayed_start(unsigned long arg)
782{
783	struct snd_pcm_substream *subs = (struct snd_pcm_substream *)arg;
784	struct vx_core *chip = subs->pcm->private_data;
785	struct vx_pipe *pipe = subs->runtime->private_data;
786	int err;
787
788	/*  printk( KERN_DEBUG "DDDD tasklet delayed start jiffies = %ld\n", jiffies);*/
789
790	if ((err = vx_start_stream(chip, pipe)) < 0) {
791		snd_printk(KERN_ERR "vx: cannot start stream\n");
792		return;
793	}
794	if ((err = vx_toggle_pipe(chip, pipe, 1)) < 0) {
795		snd_printk(KERN_ERR "vx: cannot start pipe\n");
796		return;
797	}
798	/*   printk( KERN_DEBUG "dddd tasklet delayed start jiffies = %ld \n", jiffies);*/
799}
800
801/*
802 * vx_pcm_playback_trigger - trigger callback for playback
803 */
804static int vx_pcm_trigger(struct snd_pcm_substream *subs, int cmd)
805{
806	struct vx_core *chip = snd_pcm_substream_chip(subs);
807	struct vx_pipe *pipe = subs->runtime->private_data;
808	int err;
809
810	if (chip->chip_status & VX_STAT_IS_STALE)
811		return -EBUSY;
812
813	switch (cmd) {
814	case SNDRV_PCM_TRIGGER_START:
815	case SNDRV_PCM_TRIGGER_RESUME:
816		if (! pipe->is_capture)
817			vx_pcm_playback_transfer(chip, subs, pipe, 2);
818		tasklet_hi_schedule(&pipe->start_tq);
819		chip->pcm_running++;
820		pipe->running = 1;
821		break;
822	case SNDRV_PCM_TRIGGER_STOP:
823	case SNDRV_PCM_TRIGGER_SUSPEND:
824		vx_toggle_pipe(chip, pipe, 0);
825		vx_stop_pipe(chip, pipe);
826		vx_stop_stream(chip, pipe);
827		chip->pcm_running--;
828		pipe->running = 0;
829		break;
830	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
831		if ((err = vx_toggle_pipe(chip, pipe, 0)) < 0)
832			return err;
833		break;
834	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
835		if ((err = vx_toggle_pipe(chip, pipe, 1)) < 0)
836			return err;
837		break;
838	default:
839		return -EINVAL;
840	}
841	return 0;
842}
843
844/*
845 * vx_pcm_playback_pointer - pointer callback for playback
846 */
847static snd_pcm_uframes_t vx_pcm_playback_pointer(struct snd_pcm_substream *subs)
848{
849	struct snd_pcm_runtime *runtime = subs->runtime;
850	struct vx_pipe *pipe = runtime->private_data;
851	return pipe->position;
852}
853
854/*
855 * vx_pcm_hw_params - hw_params callback for playback and capture
856 */
857static int vx_pcm_hw_params(struct snd_pcm_substream *subs,
858				     struct snd_pcm_hw_params *hw_params)
859{
860	return snd_pcm_alloc_vmalloc_buffer(subs, params_buffer_bytes(hw_params));
861}
862
863/*
864 * vx_pcm_hw_free - hw_free callback for playback and capture
865 */
866static int vx_pcm_hw_free(struct snd_pcm_substream *subs)
867{
868	return snd_pcm_free_vmalloc_buffer(subs);
869}
870
871/*
872 * vx_pcm_prepare - prepare callback for playback and capture
873 */
874static int vx_pcm_prepare(struct snd_pcm_substream *subs)
875{
876	struct vx_core *chip = snd_pcm_substream_chip(subs);
877	struct snd_pcm_runtime *runtime = subs->runtime;
878	struct vx_pipe *pipe = runtime->private_data;
879	int err, data_mode;
880	// int max_size, nchunks;
881
882	if (chip->chip_status & VX_STAT_IS_STALE)
883		return -EBUSY;
884
885	data_mode = (chip->uer_bits & IEC958_AES0_NONAUDIO) != 0;
886	if (data_mode != pipe->data_mode && ! pipe->is_capture) {
887		/* IEC958 status (raw-mode) was changed */
888		/* we reopen the pipe */
889		struct vx_rmh rmh;
890		snd_printdd(KERN_DEBUG "reopen the pipe with data_mode = %d\n", data_mode);
891		vx_init_rmh(&rmh, CMD_FREE_PIPE);
892		vx_set_pipe_cmd_params(&rmh, 0, pipe->number, 0);
893		if ((err = vx_send_msg(chip, &rmh)) < 0)
894			return err;
895		vx_init_rmh(&rmh, CMD_RES_PIPE);
896		vx_set_pipe_cmd_params(&rmh, 0, pipe->number, pipe->channels);
897		if (data_mode)
898			rmh.Cmd[0] |= BIT_DATA_MODE;
899		if ((err = vx_send_msg(chip, &rmh)) < 0)
900			return err;
901		pipe->data_mode = data_mode;
902	}
903
904	if (chip->pcm_running && chip->freq != runtime->rate) {
905		snd_printk(KERN_ERR "vx: cannot set different clock %d "
906			   "from the current %d\n", runtime->rate, chip->freq);
907		return -EINVAL;
908	}
909	vx_set_clock(chip, runtime->rate);
910
911	if ((err = vx_set_format(chip, pipe, runtime)) < 0)
912		return err;
913
914	if (vx_is_pcmcia(chip)) {
915		pipe->align = 2; /* 16bit word */
916	} else {
917		pipe->align = 4; /* 32bit word */
918	}
919
920	pipe->buffer_bytes = frames_to_bytes(runtime, runtime->buffer_size);
921	pipe->period_bytes = frames_to_bytes(runtime, runtime->period_size);
922	pipe->hw_ptr = 0;
923
924	/* set the timestamp */
925	vx_update_pipe_position(chip, runtime, pipe);
926	/* clear again */
927	pipe->transferred = 0;
928	pipe->position = 0;
929
930	pipe->prepared = 1;
931
932	return 0;
933}
934
935
936/*
937 * operators for PCM playback
938 */
939static struct snd_pcm_ops vx_pcm_playback_ops = {
940	.open =		vx_pcm_playback_open,
941	.close =	vx_pcm_playback_close,
942	.ioctl =	snd_pcm_lib_ioctl,
943	.hw_params =	vx_pcm_hw_params,
944	.hw_free =	vx_pcm_hw_free,
945	.prepare =	vx_pcm_prepare,
946	.trigger =	vx_pcm_trigger,
947	.pointer =	vx_pcm_playback_pointer,
948	.page =		snd_pcm_get_vmalloc_page,
949};
950
951
952/*
953 * playback hw information
954 */
955
956static struct snd_pcm_hardware vx_pcm_capture_hw = {
957	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
958				 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_MMAP_VALID /*|*/
959				 /*SNDRV_PCM_INFO_RESUME*/),
960	.formats =		(/*SNDRV_PCM_FMTBIT_U8 |*/
961				 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_3LE),
962	.rates =		SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
963	.rate_min =		5000,
964	.rate_max =		48000,
965	.channels_min =		1,
966	.channels_max =		2,
967	.buffer_bytes_max =	(128*1024),
968	.period_bytes_min =	126,
969	.period_bytes_max =	(128*1024),
970	.periods_min =		2,
971	.periods_max =		VX_MAX_PERIODS,
972	.fifo_size =		126,
973};
974
975
976/*
977 * vx_pcm_capture_open - open callback for capture
978 */
979static int vx_pcm_capture_open(struct snd_pcm_substream *subs)
980{
981	struct snd_pcm_runtime *runtime = subs->runtime;
982	struct vx_core *chip = snd_pcm_substream_chip(subs);
983	struct vx_pipe *pipe;
984	struct vx_pipe *pipe_out_monitoring = NULL;
985	unsigned int audio;
986	int err;
987
988	if (chip->chip_status & VX_STAT_IS_STALE)
989		return -EBUSY;
990
991	audio = subs->pcm->device * 2;
992	snd_assert(audio < chip->audio_ins, return -EINVAL);
993	err = vx_alloc_pipe(chip, 1, audio, 2, &pipe);
994	if (err < 0)
995		return err;
996	pipe->substream = subs;
997	tasklet_init(&pipe->start_tq, vx_pcm_delayed_start, (unsigned long)subs);
998	chip->capture_pipes[audio] = pipe;
999
1000	/* check if monitoring is needed */
1001	if (chip->audio_monitor_active[audio]) {
1002		pipe_out_monitoring = chip->playback_pipes[audio];
1003		if (! pipe_out_monitoring) {
1004			/* allocate a pipe */
1005			err = vx_alloc_pipe(chip, 0, audio, 2, &pipe_out_monitoring);
1006			if (err < 0)
1007				return err;
1008			chip->playback_pipes[audio] = pipe_out_monitoring;
1009		}
1010		pipe_out_monitoring->references++;
1011		/*
1012		   if an output pipe is available, it's audios still may need to be
1013		   unmuted. hence we'll have to call a mixer entry point.
1014		*/
1015		vx_set_monitor_level(chip, audio, chip->audio_monitor[audio],
1016				     chip->audio_monitor_active[audio]);
1017		/* assuming stereo */
1018		vx_set_monitor_level(chip, audio+1, chip->audio_monitor[audio+1],
1019				     chip->audio_monitor_active[audio+1]);
1020	}
1021
1022	pipe->monitoring_pipe = pipe_out_monitoring; /* default value NULL */
1023
1024	runtime->hw = vx_pcm_capture_hw;
1025	runtime->hw.period_bytes_min = chip->ibl.size;
1026	runtime->private_data = pipe;
1027
1028	/* align to 4 bytes (otherwise will be problematic when 24bit is used) */
1029	snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 4);
1030	snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 4);
1031
1032	return 0;
1033}
1034
1035/*
1036 * vx_pcm_capture_close - close callback for capture
1037 */
1038static int vx_pcm_capture_close(struct snd_pcm_substream *subs)
1039{
1040	struct vx_core *chip = snd_pcm_substream_chip(subs);
1041	struct vx_pipe *pipe;
1042	struct vx_pipe *pipe_out_monitoring;
1043
1044	if (! subs->runtime->private_data)
1045		return -EINVAL;
1046	pipe = subs->runtime->private_data;
1047	chip->capture_pipes[pipe->number] = NULL;
1048
1049	pipe_out_monitoring = pipe->monitoring_pipe;
1050
1051	/*
1052	  if an output pipe is attached to this input,
1053	  check if it needs to be released.
1054	*/
1055	if (pipe_out_monitoring) {
1056		if (--pipe_out_monitoring->references == 0) {
1057			vx_free_pipe(chip, pipe_out_monitoring);
1058			chip->playback_pipes[pipe->number] = NULL;
1059			pipe->monitoring_pipe = NULL;
1060		}
1061	}
1062
1063	vx_free_pipe(chip, pipe);
1064	return 0;
1065}
1066
1067
1068
1069#define DMA_READ_ALIGN	6	/* hardware alignment for read */
1070
1071/*
1072 * vx_pcm_capture_update - update the capture buffer
1073 */
1074static void vx_pcm_capture_update(struct vx_core *chip, struct snd_pcm_substream *subs,
1075				  struct vx_pipe *pipe)
1076{
1077	int size, space, count;
1078	struct snd_pcm_runtime *runtime = subs->runtime;
1079
1080	if (! pipe->prepared || (chip->chip_status & VX_STAT_IS_STALE))
1081		return;
1082
1083	size = runtime->buffer_size - snd_pcm_capture_avail(runtime);
1084	if (! size)
1085		return;
1086	size = frames_to_bytes(runtime, size);
1087	space = vx_query_hbuffer_size(chip, pipe);
1088	if (space < 0)
1089		goto _error;
1090	if (size > space)
1091		size = space;
1092	size = (size / 3) * 3; /* align to 3 bytes */
1093	if (size < DMA_READ_ALIGN)
1094		goto _error;
1095
1096	/* keep the last 6 bytes, they will be read after disconnection */
1097	count = size - DMA_READ_ALIGN;
1098	/* read bytes until the current pointer reaches to the aligned position
1099	 * for word-transfer
1100	 */
1101	while (count > 0) {
1102		if ((pipe->hw_ptr % pipe->align) == 0)
1103			break;
1104		if (vx_wait_for_rx_full(chip) < 0)
1105			goto _error;
1106		vx_pcm_read_per_bytes(chip, runtime, pipe);
1107		count -= 3;
1108	}
1109	if (count > 0) {
1110		/* ok, let's accelerate! */
1111		int align = pipe->align * 3;
1112		space = (count / align) * align;
1113		vx_pseudo_dma_read(chip, runtime, pipe, space);
1114		count -= space;
1115	}
1116	/* read the rest of bytes */
1117	while (count > 0) {
1118		if (vx_wait_for_rx_full(chip) < 0)
1119			goto _error;
1120		vx_pcm_read_per_bytes(chip, runtime, pipe);
1121		count -= 3;
1122	}
1123	/* disconnect the host, SIZE_HBUF command always switches to the stream mode */
1124	vx_send_rih_nolock(chip, IRQ_CONNECT_STREAM_NEXT);
1125	/* read the last pending 6 bytes */
1126	count = DMA_READ_ALIGN;
1127	while (count > 0) {
1128		vx_pcm_read_per_bytes(chip, runtime, pipe);
1129		count -= 3;
1130	}
1131	/* update the position */
1132	pipe->transferred += size;
1133	if (pipe->transferred >= pipe->period_bytes) {
1134		pipe->transferred %= pipe->period_bytes;
1135		snd_pcm_period_elapsed(subs);
1136	}
1137	return;
1138
1139 _error:
1140	/* disconnect the host, SIZE_HBUF command always switches to the stream mode */
1141	vx_send_rih_nolock(chip, IRQ_CONNECT_STREAM_NEXT);
1142	return;
1143}
1144
1145/*
1146 * vx_pcm_capture_pointer - pointer callback for capture
1147 */
1148static snd_pcm_uframes_t vx_pcm_capture_pointer(struct snd_pcm_substream *subs)
1149{
1150	struct snd_pcm_runtime *runtime = subs->runtime;
1151	struct vx_pipe *pipe = runtime->private_data;
1152	return bytes_to_frames(runtime, pipe->hw_ptr);
1153}
1154
1155/*
1156 * operators for PCM capture
1157 */
1158static struct snd_pcm_ops vx_pcm_capture_ops = {
1159	.open =		vx_pcm_capture_open,
1160	.close =	vx_pcm_capture_close,
1161	.ioctl =	snd_pcm_lib_ioctl,
1162	.hw_params =	vx_pcm_hw_params,
1163	.hw_free =	vx_pcm_hw_free,
1164	.prepare =	vx_pcm_prepare,
1165	.trigger =	vx_pcm_trigger,
1166	.pointer =	vx_pcm_capture_pointer,
1167	.page =		snd_pcm_get_vmalloc_page,
1168};
1169
1170
1171/*
1172 * interrupt handler for pcm streams
1173 */
1174void vx_pcm_update_intr(struct vx_core *chip, unsigned int events)
1175{
1176	unsigned int i;
1177	struct vx_pipe *pipe;
1178
1179#define EVENT_MASK	(END_OF_BUFFER_EVENTS_PENDING|ASYNC_EVENTS_PENDING)
1180
1181	if (events & EVENT_MASK) {
1182		vx_init_rmh(&chip->irq_rmh, CMD_ASYNC);
1183		if (events & ASYNC_EVENTS_PENDING)
1184			chip->irq_rmh.Cmd[0] |= 0x00000001;	/* SEL_ASYNC_EVENTS */
1185		if (events & END_OF_BUFFER_EVENTS_PENDING)
1186			chip->irq_rmh.Cmd[0] |= 0x00000002;	/* SEL_END_OF_BUF_EVENTS */
1187
1188		if (vx_send_msg(chip, &chip->irq_rmh) < 0) {
1189			snd_printdd(KERN_ERR "msg send error!!\n");
1190			return;
1191		}
1192
1193		i = 1;
1194		while (i < chip->irq_rmh.LgStat) {
1195			int p, buf, capture, eob;
1196			p = chip->irq_rmh.Stat[i] & MASK_FIRST_FIELD;
1197			capture = (chip->irq_rmh.Stat[i] & 0x400000) ? 1 : 0;
1198			eob = (chip->irq_rmh.Stat[i] & 0x800000) ? 1 : 0;
1199			i++;
1200			if (events & ASYNC_EVENTS_PENDING)
1201				i++;
1202			buf = 1; /* force to transfer */
1203			if (events & END_OF_BUFFER_EVENTS_PENDING) {
1204				if (eob)
1205					buf = chip->irq_rmh.Stat[i];
1206				i++;
1207			}
1208			if (capture)
1209				continue;
1210			snd_assert(p >= 0 && (unsigned int)p < chip->audio_outs,);
1211			pipe = chip->playback_pipes[p];
1212			if (pipe && pipe->substream) {
1213				vx_pcm_playback_update(chip, pipe->substream, pipe);
1214				vx_pcm_playback_transfer(chip, pipe->substream, pipe, buf);
1215			}
1216		}
1217	}
1218
1219	/* update the capture pcm pointers as frequently as possible */
1220	for (i = 0; i < chip->audio_ins; i++) {
1221		pipe = chip->capture_pipes[i];
1222		if (pipe && pipe->substream)
1223			vx_pcm_capture_update(chip, pipe->substream, pipe);
1224	}
1225}
1226
1227
1228/*
1229 * vx_init_audio_io - check the availabe audio i/o and allocate pipe arrays
1230 */
1231static int vx_init_audio_io(struct vx_core *chip)
1232{
1233	struct vx_rmh rmh;
1234	int preferred;
1235
1236	vx_init_rmh(&rmh, CMD_SUPPORTED);
1237	if (vx_send_msg(chip, &rmh) < 0) {
1238		snd_printk(KERN_ERR "vx: cannot get the supported audio data\n");
1239		return -ENXIO;
1240	}
1241
1242	chip->audio_outs = rmh.Stat[0] & MASK_FIRST_FIELD;
1243	chip->audio_ins = (rmh.Stat[0] >> (FIELD_SIZE*2)) & MASK_FIRST_FIELD;
1244	chip->audio_info = rmh.Stat[1];
1245
1246	/* allocate pipes */
1247	chip->playback_pipes = kcalloc(chip->audio_outs, sizeof(struct vx_pipe *), GFP_KERNEL);
1248	if (!chip->playback_pipes)
1249		return -ENOMEM;
1250	chip->capture_pipes = kcalloc(chip->audio_ins, sizeof(struct vx_pipe *), GFP_KERNEL);
1251	if (!chip->capture_pipes) {
1252		kfree(chip->playback_pipes);
1253		return -ENOMEM;
1254	}
1255
1256	preferred = chip->ibl.size;
1257	chip->ibl.size = 0;
1258	vx_set_ibl(chip, &chip->ibl); /* query the info */
1259	if (preferred > 0) {
1260		chip->ibl.size = ((preferred + chip->ibl.granularity - 1) /
1261				  chip->ibl.granularity) * chip->ibl.granularity;
1262		if (chip->ibl.size > chip->ibl.max_size)
1263			chip->ibl.size = chip->ibl.max_size;
1264	} else
1265		chip->ibl.size = chip->ibl.min_size; /* set to the minimum */
1266	vx_set_ibl(chip, &chip->ibl);
1267
1268	return 0;
1269}
1270
1271
1272/*
1273 * free callback for pcm
1274 */
1275static void snd_vx_pcm_free(struct snd_pcm *pcm)
1276{
1277	struct vx_core *chip = pcm->private_data;
1278	chip->pcm[pcm->device] = NULL;
1279	kfree(chip->playback_pipes);
1280	chip->playback_pipes = NULL;
1281	kfree(chip->capture_pipes);
1282	chip->capture_pipes = NULL;
1283}
1284
1285/*
1286 * snd_vx_pcm_new - create and initialize a pcm
1287 */
1288int snd_vx_pcm_new(struct vx_core *chip)
1289{
1290	struct snd_pcm *pcm;
1291	unsigned int i;
1292	int err;
1293
1294	if ((err = vx_init_audio_io(chip)) < 0)
1295		return err;
1296
1297	for (i = 0; i < chip->hw->num_codecs; i++) {
1298		unsigned int outs, ins;
1299		outs = chip->audio_outs > i * 2 ? 1 : 0;
1300		ins = chip->audio_ins > i * 2 ? 1 : 0;
1301		if (! outs && ! ins)
1302			break;
1303		err = snd_pcm_new(chip->card, "VX PCM", i,
1304				  outs, ins, &pcm);
1305		if (err < 0)
1306			return err;
1307		if (outs)
1308			snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &vx_pcm_playback_ops);
1309		if (ins)
1310			snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &vx_pcm_capture_ops);
1311
1312		pcm->private_data = chip;
1313		pcm->private_free = snd_vx_pcm_free;
1314		pcm->info_flags = 0;
1315		strcpy(pcm->name, chip->card->shortname);
1316		chip->pcm[i] = pcm;
1317	}
1318
1319	return 0;
1320}
1321