]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - sound/drivers/vx/vx_pcm.c
ALSA: Kill snd_assert() in other places
[mirror_ubuntu-bionic-kernel.git] / sound / drivers / vx / vx_pcm.c
CommitLineData
1da177e4
LT
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
1da177e4
LT
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"
56
57
58/*
59 * we use a vmalloc'ed (sg-)buffer
60 */
61
62/* get the physical page pointer on the given offset */
af26367f
TI
63static struct page *snd_pcm_get_vmalloc_page(struct snd_pcm_substream *subs,
64 unsigned long offset)
1da177e4
LT
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 */
af26367f 75static int snd_pcm_alloc_vmalloc_buffer(struct snd_pcm_substream *subs, size_t size)
1da177e4 76{
af26367f 77 struct snd_pcm_runtime *runtime = subs->runtime;
1da177e4
LT
78 if (runtime->dma_area) {
79 /* already allocated */
80 if (runtime->dma_bytes >= size)
81 return 0; /* already enough large */
b1d5776d 82 vfree(runtime->dma_area);
1da177e4
LT
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 */
af26367f 97static int snd_pcm_free_vmalloc_buffer(struct snd_pcm_substream *subs)
1da177e4 98{
af26367f 99 struct snd_pcm_runtime *runtime = subs->runtime;
9c4be3d3
JJ
100
101 vfree(runtime->dma_area);
102 runtime->dma_area = NULL;
1da177e4
LT
103 return 0;
104}
105
106
107/*
108 * read three pending pcm bytes via inb()
109 */
af26367f
TI
110static void vx_pcm_read_per_bytes(struct vx_core *chip, struct snd_pcm_runtime *runtime,
111 struct vx_pipe *pipe)
1da177e4
LT
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 */
af26367f
TI
138static void vx_set_pcx_time(struct vx_core *chip, pcx_time_t *pc_time,
139 unsigned int *dsp_time)
1da177e4
LT
140{
141 dsp_time[0] = (unsigned int)((*pc_time) >> 24) & PCX_TIME_HI_MASK;
142 dsp_time[1] = (unsigned int)(*pc_time) & MASK_DSP_WORD;
143}
144
145/*
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
149 *
150 * if the pipe is programmed with the differed time, set the DSP time
151 * on the rmh and changes its command length.
152 *
153 * returns the increase of the command length.
154 */
af26367f
TI
155static int vx_set_differed_time(struct vx_core *chip, struct vx_rmh *rmh,
156 struct vx_pipe *pipe)
1da177e4
LT
157{
158 /* Update The length added to the RMH command by the timestamp */
159 if (! (pipe->differed_type & DC_DIFFERED_DELAY))
160 return 0;
161
162 /* Set the T bit */
163 rmh->Cmd[0] |= DSP_DIFFERED_COMMAND_MASK;
164
165 /* Time stamp is the 1st following parameter */
166 vx_set_pcx_time(chip, &pipe->pcx_time, &rmh->Cmd[1]);
167
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 ;
171
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;
175
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;
179
180 rmh->LgCmd += 2;
181 return 2;
182}
183
184/*
185 * vx_set_stream_format - send the stream format command
186 * @pipe: the affected pipe
187 * @data: format bitmask
188 */
af26367f
TI
189static int vx_set_stream_format(struct vx_core *chip, struct vx_pipe *pipe,
190 unsigned int data)
1da177e4
LT
191{
192 struct vx_rmh rmh;
193
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;
197
198 /* Command might be longer since we may have to add a timestamp */
199 vx_set_differed_time(chip, &rmh, pipe);
200
201 rmh.Cmd[rmh.LgCmd] = (data & 0xFFFFFF00) >> 8;
202 rmh.Cmd[rmh.LgCmd + 1] = (data & 0xFF) << 16 /*| (datal & 0xFFFF00) >> 8*/;
203 rmh.LgCmd += 2;
204
205 return vx_send_msg(chip, &rmh);
206}
207
208
209/*
210 * vx_set_format - set the format of a pipe
211 * @pipe: the affected pipe
212 * @runtime: pcm runtime instance to be referred
213 *
214 * returns 0 if successful, or a negative error code.
215 */
af26367f
TI
216static int vx_set_format(struct vx_core *chip, struct vx_pipe *pipe,
217 struct snd_pcm_runtime *runtime)
1da177e4
LT
218{
219 unsigned int header = HEADER_FMT_BASE;
220
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;
229
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;
237 };
238
239 return vx_set_stream_format(chip, pipe, header);
240}
241
242/*
243 * set / query the IBL size
244 */
af26367f 245static int vx_set_ibl(struct vx_core *chip, struct vx_ibl_info *info)
1da177e4
LT
246{
247 int err;
248 struct vx_rmh rmh;
249
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;
262}
263
264
265/*
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
269 *
270 * checks the state of a given pipe, and stores the state (1 = running,
271 * 0 = paused) on the given pointer.
272 *
273 * called from trigger callback only
274 */
af26367f 275static int vx_get_pipe_state(struct vx_core *chip, struct vx_pipe *pipe, int *state)
1da177e4
LT
276{
277 int err;
278 struct vx_rmh rmh;
279
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;
286}
287
288
289/*
290 * vx_query_hbuffer_size - query available h-buffer size in bytes
291 * @pipe: the pipe to be checked
292 *
293 * return the available size on h-buffer in bytes,
294 * or a negative error code.
295 *
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.
299 */
af26367f 300static int vx_query_hbuffer_size(struct vx_core *chip, struct vx_pipe *pipe)
1da177e4
LT
301{
302 int result;
303 struct vx_rmh rmh;
304
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;
313}
314
315
316/*
317 * vx_pipe_can_start - query whether a pipe is ready for start
318 * @pipe: the pipe to be checked
319 *
320 * return 1 if ready, 0 if not ready, and negative value on error.
321 *
322 * called from trigger callback only
323 */
af26367f 324static int vx_pipe_can_start(struct vx_core *chip, struct vx_pipe *pipe)
1da177e4
LT
325{
326 int err;
327 struct vx_rmh rmh;
328
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;
332
333 err = vx_send_msg_nolock(chip, &rmh); /* no lock needed for trigger */
334 if (! err) {
335 if (rmh.Stat[0])
336 err = 1;
337 }
338 return err;
339}
340
341/*
342 * vx_conf_pipe - tell the pipe to stand by and wait for IRQA.
343 * @pipe: the pipe to be configured
344 */
af26367f 345static int vx_conf_pipe(struct vx_core *chip, struct vx_pipe *pipe)
1da177e4
LT
346{
347 struct vx_rmh rmh;
348
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 */
354}
355
356/*
357 * vx_send_irqa - trigger IRQA
358 */
af26367f 359static int vx_send_irqa(struct vx_core *chip)
1da177e4
LT
360{
361 struct vx_rmh rmh;
362
363 vx_init_rmh(&rmh, CMD_SEND_IRQA);
364 return vx_send_msg_nolock(chip, &rmh); /* no lock needed for trigger */
365}
366
367
368#define MAX_WAIT_FOR_DSP 250
369/*
370 * vx boards do not support inter-card sync, besides
371 * only 126 samples require to be prepared before a pipe can start
372 */
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*/
375
376/*
377 * vx_toggle_pipe - start / pause a pipe
378 * @pipe: the pipe to be triggered
379 * @state: start = 1, pause = 0
380 *
381 * called from trigger callback only
382 *
383 */
af26367f 384static int vx_toggle_pipe(struct vx_core *chip, struct vx_pipe *pipe, int state)
1da177e4
LT
385{
386 int err, i, cur_state;
387
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;
393
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)
397 */
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
405 */
406 mdelay(1);
407 }
408 }
409
410 if ((err = vx_conf_pipe(chip, pipe)) < 0)
411 return err;
412
413 if ((err = vx_send_irqa(chip)) < 0)
414 return err;
415
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)
419 */
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);
426 }
427 return err < 0 ? -EIO : 0;
428}
429
430
431/*
432 * vx_stop_pipe - stop a pipe
433 * @pipe: the pipe to be stopped
434 *
435 * called from trigger callback only
436 */
af26367f 437static int vx_stop_pipe(struct vx_core *chip, struct vx_pipe *pipe)
1da177e4
LT
438{
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 */
443}
444
445
446/*
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
452 *
453 * return 0 on success, or a negative error code.
454 */
af26367f 455static int vx_alloc_pipe(struct vx_core *chip, int capture,
1da177e4 456 int audioid, int num_audio,
af26367f 457 struct vx_pipe **pipep)
1da177e4
LT
458{
459 int err;
af26367f 460 struct vx_pipe *pipe;
1da177e4
LT
461 struct vx_rmh rmh;
462 int data_mode;
463
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;
477
478 /* initialize the pipe record */
561b220a 479 pipe = kzalloc(sizeof(*pipe), GFP_KERNEL);
1da177e4
LT
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;
486 }
487
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;
496
497 return 0;
498}
499
500
501/*
502 * vx_free_pipe - release a pipe
503 * @pipe: pipe to be released
504 */
af26367f 505static int vx_free_pipe(struct vx_core *chip, struct vx_pipe *pipe)
1da177e4
LT
506{
507 struct vx_rmh rmh;
508
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);
512
513 kfree(pipe);
514 return 0;
515}
516
517
518/*
519 * vx_start_stream - start the stream
520 *
521 * called from trigger callback only
522 */
af26367f 523static int vx_start_stream(struct vx_core *chip, struct vx_pipe *pipe)
1da177e4
LT
524{
525 struct vx_rmh rmh;
526
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 */
531}
532
533
534/*
535 * vx_stop_stream - stop the stream
536 *
537 * called from trigger callback only
538 */
af26367f 539static int vx_stop_stream(struct vx_core *chip, struct vx_pipe *pipe)
1da177e4
LT
540{
541 struct vx_rmh rmh;
542
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 */
546}
547
548
549/*
550 * playback hw information
551 */
552
af26367f 553static struct snd_pcm_hardware vx_pcm_playback_hw = {
1da177e4 554 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
41e4845c
JK
555 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_MMAP_VALID /*|*/
556 /*SNDRV_PCM_INFO_RESUME*/),
af26367f
TI
557 .formats = (/*SNDRV_PCM_FMTBIT_U8 |*/
558 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_3LE),
1da177e4
LT
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,
570};
571
572
573static void vx_pcm_delayed_start(unsigned long arg);
574
575/*
576 * vx_pcm_playback_open - open callback for playback
577 */
af26367f 578static int vx_pcm_playback_open(struct snd_pcm_substream *subs)
1da177e4 579{
af26367f
TI
580 struct snd_pcm_runtime *runtime = subs->runtime;
581 struct vx_core *chip = snd_pcm_substream_chip(subs);
582 struct vx_pipe *pipe = NULL;
1da177e4
LT
583 unsigned int audio;
584 int err;
585
586 if (chip->chip_status & VX_STAT_IS_STALE)
587 return -EBUSY;
588
589 audio = subs->pcm->device * 2;
5e246b85
TI
590 if (snd_BUG_ON(audio >= chip->audio_outs))
591 return -EINVAL;
1da177e4
LT
592
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;
601 }
602 /* open for playback */
603 pipe->references++;
604
605 pipe->substream = subs;
606 tasklet_init(&pipe->start_tq, vx_pcm_delayed_start, (unsigned long)subs);
607 chip->playback_pipes[audio] = pipe;
608
609 runtime->hw = vx_pcm_playback_hw;
610 runtime->hw.period_bytes_min = chip->ibl.size;
611 runtime->private_data = pipe;
612
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);
616
617 return 0;
618}
619
620/*
621 * vx_pcm_playback_close - close callback for playback
622 */
af26367f 623static int vx_pcm_playback_close(struct snd_pcm_substream *subs)
1da177e4 624{
af26367f
TI
625 struct vx_core *chip = snd_pcm_substream_chip(subs);
626 struct vx_pipe *pipe;
1da177e4
LT
627
628 if (! subs->runtime->private_data)
629 return -EINVAL;
630
631 pipe = subs->runtime->private_data;
632
633 if (--pipe->references == 0) {
634 chip->playback_pipes[pipe->number] = NULL;
635 vx_free_pipe(chip, pipe);
636 }
637
638 return 0;
639
640}
641
642
643/*
644 * vx_notify_end_of_buffer - send "end-of-buffer" notifier at the given pipe
645 * @pipe: the pipe to notify
646 *
647 * NB: call with a certain lock.
648 */
af26367f 649static int vx_notify_end_of_buffer(struct vx_core *chip, struct vx_pipe *pipe)
1da177e4
LT
650{
651 int err;
652 struct vx_rmh rmh; /* use a temporary rmh here */
653
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;
664}
665
666/*
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
671 *
672 * transfer a single buffer chunk. EOB notificaton is added after that.
673 * called from the interrupt handler, too.
674 *
675 * return 0 if ok.
676 */
af26367f
TI
677static int vx_pcm_playback_transfer_chunk(struct vx_core *chip,
678 struct snd_pcm_runtime *runtime,
679 struct vx_pipe *pipe, int size)
1da177e4
LT
680{
681 int space, err = 0;
682
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;
689 }
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 */
694 }
695
696 /* we don't need irqsave here, because this function
697 * is called from either trigger callback or irq handler
698 */
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;
706}
707
708/*
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).
714 */
af26367f
TI
715static int vx_update_pipe_position(struct vx_core *chip,
716 struct snd_pcm_runtime *runtime,
717 struct vx_pipe *pipe)
1da177e4
LT
718{
719 struct vx_rmh rmh;
720 int err, update;
721 u64 count;
722
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;
728
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;
737}
738
739/*
740 * transfer the pending playback buffer data to DSP
741 * called from interrupt handler
742 */
af26367f
TI
743static void vx_pcm_playback_transfer(struct vx_core *chip,
744 struct snd_pcm_substream *subs,
745 struct vx_pipe *pipe, int nchunks)
1da177e4
LT
746{
747 int i, err;
af26367f 748 struct snd_pcm_runtime *runtime = subs->runtime;
1da177e4
LT
749
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;
756 }
757}
758
759/*
760 * update the playback position and call snd_pcm_period_elapsed() if necessary
761 * called from interrupt handler
762 */
af26367f
TI
763static void vx_pcm_playback_update(struct vx_core *chip,
764 struct snd_pcm_substream *subs,
765 struct vx_pipe *pipe)
1da177e4
LT
766{
767 int err;
af26367f 768 struct snd_pcm_runtime *runtime = subs->runtime;
1da177e4
LT
769
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);
776 }
777 }
778}
779
780/*
781 * start the stream and pipe.
782 * this function is called from tasklet, which is invoked by the trigger
783 * START callback.
784 */
785static void vx_pcm_delayed_start(unsigned long arg)
786{
af26367f
TI
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;
1da177e4
LT
790 int err;
791
792 /* printk( KERN_DEBUG "DDDD tasklet delayed start jiffies = %ld\n", jiffies);*/
793
794 if ((err = vx_start_stream(chip, pipe)) < 0) {
795 snd_printk(KERN_ERR "vx: cannot start stream\n");
796 return;
797 }
798 if ((err = vx_toggle_pipe(chip, pipe, 1)) < 0) {
799 snd_printk(KERN_ERR "vx: cannot start pipe\n");
800 return;
801 }
802 /* printk( KERN_DEBUG "dddd tasklet delayed start jiffies = %ld \n", jiffies);*/
803}
804
805/*
806 * vx_pcm_playback_trigger - trigger callback for playback
807 */
af26367f 808static int vx_pcm_trigger(struct snd_pcm_substream *subs, int cmd)
1da177e4 809{
af26367f
TI
810 struct vx_core *chip = snd_pcm_substream_chip(subs);
811 struct vx_pipe *pipe = subs->runtime->private_data;
1da177e4
LT
812 int err;
813
814 if (chip->chip_status & VX_STAT_IS_STALE)
815 return -EBUSY;
816
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.
825 */
826 tasklet_hi_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;
848 }
849 return 0;
850}
851
852/*
853 * vx_pcm_playback_pointer - pointer callback for playback
854 */
af26367f 855static snd_pcm_uframes_t vx_pcm_playback_pointer(struct snd_pcm_substream *subs)
1da177e4 856{
af26367f
TI
857 struct snd_pcm_runtime *runtime = subs->runtime;
858 struct vx_pipe *pipe = runtime->private_data;
1da177e4
LT
859 return pipe->position;
860}
861
862/*
863 * vx_pcm_hw_params - hw_params callback for playback and capture
864 */
af26367f
TI
865static int vx_pcm_hw_params(struct snd_pcm_substream *subs,
866 struct snd_pcm_hw_params *hw_params)
1da177e4
LT
867{
868 return snd_pcm_alloc_vmalloc_buffer(subs, params_buffer_bytes(hw_params));
869}
870
871/*
872 * vx_pcm_hw_free - hw_free callback for playback and capture
873 */
af26367f 874static int vx_pcm_hw_free(struct snd_pcm_substream *subs)
1da177e4
LT
875{
876 return snd_pcm_free_vmalloc_buffer(subs);
877}
878
879/*
880 * vx_pcm_prepare - prepare callback for playback and capture
881 */
af26367f 882static int vx_pcm_prepare(struct snd_pcm_substream *subs)
1da177e4 883{
af26367f
TI
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;
1da177e4
LT
887 int err, data_mode;
888 // int max_size, nchunks;
889
890 if (chip->chip_status & VX_STAT_IS_STALE)
891 return -EBUSY;
892
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;
910 }
911
912 if (chip->pcm_running && chip->freq != runtime->rate) {
af26367f
TI
913 snd_printk(KERN_ERR "vx: cannot set different clock %d "
914 "from the current %d\n", runtime->rate, chip->freq);
1da177e4
LT
915 return -EINVAL;
916 }
917 vx_set_clock(chip, runtime->rate);
918
919 if ((err = vx_set_format(chip, pipe, runtime)) < 0)
920 return err;
921
922 if (vx_is_pcmcia(chip)) {
923 pipe->align = 2; /* 16bit word */
924 } else {
925 pipe->align = 4; /* 32bit word */
926 }
927
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;
931
932 /* set the timestamp */
933 vx_update_pipe_position(chip, runtime, pipe);
934 /* clear again */
935 pipe->transferred = 0;
936 pipe->position = 0;
937
938 pipe->prepared = 1;
939
940 return 0;
941}
942
943
944/*
945 * operators for PCM playback
946 */
af26367f 947static struct snd_pcm_ops vx_pcm_playback_ops = {
1da177e4
LT
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,
957};
958
959
960/*
961 * playback hw information
962 */
963
af26367f 964static struct snd_pcm_hardware vx_pcm_capture_hw = {
1da177e4 965 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
41e4845c
JK
966 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_MMAP_VALID /*|*/
967 /*SNDRV_PCM_INFO_RESUME*/),
af26367f
TI
968 .formats = (/*SNDRV_PCM_FMTBIT_U8 |*/
969 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_3LE),
1da177e4
LT
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,
981};
982
983
984/*
985 * vx_pcm_capture_open - open callback for capture
986 */
af26367f 987static int vx_pcm_capture_open(struct snd_pcm_substream *subs)
1da177e4 988{
af26367f
TI
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;
1da177e4
LT
993 unsigned int audio;
994 int err;
995
996 if (chip->chip_status & VX_STAT_IS_STALE)
997 return -EBUSY;
998
999 audio = subs->pcm->device * 2;
5e246b85
TI
1000 if (snd_BUG_ON(audio >= chip->audio_ins))
1001 return -EINVAL;
1da177e4
LT
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;
1008
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;
1018 }
1019 pipe_out_monitoring->references++;
1020 /*
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.
1023 */
af26367f
TI
1024 vx_set_monitor_level(chip, audio, chip->audio_monitor[audio],
1025 chip->audio_monitor_active[audio]);
1da177e4 1026 /* assuming stereo */
af26367f
TI
1027 vx_set_monitor_level(chip, audio+1, chip->audio_monitor[audio+1],
1028 chip->audio_monitor_active[audio+1]);
1da177e4
LT
1029 }
1030
1031 pipe->monitoring_pipe = pipe_out_monitoring; /* default value NULL */
1032
1033 runtime->hw = vx_pcm_capture_hw;
1034 runtime->hw.period_bytes_min = chip->ibl.size;
1035 runtime->private_data = pipe;
1036
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);
1040
1041 return 0;
1042}
1043
1044/*
1045 * vx_pcm_capture_close - close callback for capture
1046 */
af26367f 1047static int vx_pcm_capture_close(struct snd_pcm_substream *subs)
1da177e4 1048{
af26367f
TI
1049 struct vx_core *chip = snd_pcm_substream_chip(subs);
1050 struct vx_pipe *pipe;
1051 struct vx_pipe *pipe_out_monitoring;
1da177e4
LT
1052
1053 if (! subs->runtime->private_data)
1054 return -EINVAL;
1055 pipe = subs->runtime->private_data;
1056 chip->capture_pipes[pipe->number] = NULL;
1057
1058 pipe_out_monitoring = pipe->monitoring_pipe;
1059
1060 /*
1061 if an output pipe is attached to this input,
1062 check if it needs to be released.
1063 */
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;
1069 }
1070 }
1071
1072 vx_free_pipe(chip, pipe);
1073 return 0;
1074}
1075
1076
1077
1078#define DMA_READ_ALIGN 6 /* hardware alignment for read */
1079
1080/*
1081 * vx_pcm_capture_update - update the capture buffer
1082 */
af26367f
TI
1083static void vx_pcm_capture_update(struct vx_core *chip, struct snd_pcm_substream *subs,
1084 struct vx_pipe *pipe)
1da177e4
LT
1085{
1086 int size, space, count;
af26367f 1087 struct snd_pcm_runtime *runtime = subs->runtime;
1da177e4
LT
1088
1089 if (! pipe->prepared || (chip->chip_status & VX_STAT_IS_STALE))
1090 return;
1091
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;
1104
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
1109 */
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;
1117 }
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;
1124 }
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;
1131 }
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;
1139 }
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);
1145 }
1146 return;
1147
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;
1152}
1153
1154/*
1155 * vx_pcm_capture_pointer - pointer callback for capture
1156 */
af26367f 1157static snd_pcm_uframes_t vx_pcm_capture_pointer(struct snd_pcm_substream *subs)
1da177e4 1158{
af26367f
TI
1159 struct snd_pcm_runtime *runtime = subs->runtime;
1160 struct vx_pipe *pipe = runtime->private_data;
1da177e4
LT
1161 return bytes_to_frames(runtime, pipe->hw_ptr);
1162}
1163
1164/*
1165 * operators for PCM capture
1166 */
af26367f 1167static struct snd_pcm_ops vx_pcm_capture_ops = {
1da177e4
LT
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,
1177};
1178
1179
1180/*
1181 * interrupt handler for pcm streams
1182 */
af26367f 1183void vx_pcm_update_intr(struct vx_core *chip, unsigned int events)
1da177e4
LT
1184{
1185 unsigned int i;
af26367f 1186 struct vx_pipe *pipe;
1da177e4
LT
1187
1188#define EVENT_MASK (END_OF_BUFFER_EVENTS_PENDING|ASYNC_EVENTS_PENDING)
1189
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 */
1196
1197 if (vx_send_msg(chip, &chip->irq_rmh) < 0) {
1198 snd_printdd(KERN_ERR "msg send error!!\n");
1199 return;
1200 }
1201
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++;
1216 }
1217 if (capture)
1218 continue;
5e246b85
TI
1219 if (snd_BUG_ON(p < 0 || p >= chip->audio_outs))
1220 continue;
1da177e4
LT
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);
1225 }
1226 }
1227 }
1228
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);
1234 }
1235}
1236
1237
1238/*
1239 * vx_init_audio_io - check the availabe audio i/o and allocate pipe arrays
1240 */
af26367f 1241static int vx_init_audio_io(struct vx_core *chip)
1da177e4
LT
1242{
1243 struct vx_rmh rmh;
1244 int preferred;
1245
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;
1250 }
1251
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];
1255
1256 /* allocate pipes */
59feddb2 1257 chip->playback_pipes = kcalloc(chip->audio_outs, sizeof(struct vx_pipe *), GFP_KERNEL);
ac57b849
AD
1258 if (!chip->playback_pipes)
1259 return -ENOMEM;
59feddb2 1260 chip->capture_pipes = kcalloc(chip->audio_ins, sizeof(struct vx_pipe *), GFP_KERNEL);
ac57b849
AD
1261 if (!chip->capture_pipes) {
1262 kfree(chip->playback_pipes);
1da177e4 1263 return -ENOMEM;
ac57b849 1264 }
1da177e4 1265
1da177e4
LT
1266 preferred = chip->ibl.size;
1267 chip->ibl.size = 0;
1268 vx_set_ibl(chip, &chip->ibl); /* query the info */
1269 if (preferred > 0) {
af26367f
TI
1270 chip->ibl.size = ((preferred + chip->ibl.granularity - 1) /
1271 chip->ibl.granularity) * chip->ibl.granularity;
1da177e4
LT
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);
1277
1278 return 0;
1279}
1280
1281
1282/*
1283 * free callback for pcm
1284 */
af26367f 1285static void snd_vx_pcm_free(struct snd_pcm *pcm)
1da177e4 1286{
af26367f 1287 struct vx_core *chip = pcm->private_data;
1da177e4 1288 chip->pcm[pcm->device] = NULL;
4d572776
JJ
1289 kfree(chip->playback_pipes);
1290 chip->playback_pipes = NULL;
1291 kfree(chip->capture_pipes);
1292 chip->capture_pipes = NULL;
1da177e4
LT
1293}
1294
1295/*
1296 * snd_vx_pcm_new - create and initialize a pcm
1297 */
af26367f 1298int snd_vx_pcm_new(struct vx_core *chip)
1da177e4 1299{
af26367f 1300 struct snd_pcm *pcm;
1da177e4
LT
1301 unsigned int i;
1302 int err;
1303
1304 if ((err = vx_init_audio_io(chip)) < 0)
1305 return err;
1306
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);
1321
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;
1327 }
1328
1329 return 0;
1330}