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