blob: 7e65a103fbb2f357ebbf59789cbac13c81aea8f5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 */
Takashi Iwaiaf263672005-11-17 14:46:59 +010064static struct page *snd_pcm_get_vmalloc_page(struct snd_pcm_substream *subs,
65 unsigned long offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
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 */
Takashi Iwaiaf263672005-11-17 14:46:59 +010076static int snd_pcm_alloc_vmalloc_buffer(struct snd_pcm_substream *subs, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
Takashi Iwaiaf263672005-11-17 14:46:59 +010078 struct snd_pcm_runtime *runtime = subs->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 if (runtime->dma_area) {
80 /* already allocated */
81 if (runtime->dma_bytes >= size)
82 return 0; /* already enough large */
Takashi Iwaib1d57762005-10-10 11:56:31 +020083 vfree(runtime->dma_area);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 }
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 */
Takashi Iwaiaf263672005-11-17 14:46:59 +010098static int snd_pcm_free_vmalloc_buffer(struct snd_pcm_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
Takashi Iwaiaf263672005-11-17 14:46:59 +0100100 struct snd_pcm_runtime *runtime = subs->runtime;
Jesper Juhl9c4be3d2006-02-09 20:04:16 +0100101
102 vfree(runtime->dma_area);
103 runtime->dma_area = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 return 0;
105}
106
107
108/*
109 * read three pending pcm bytes via inb()
110 */
Takashi Iwaiaf263672005-11-17 14:46:59 +0100111static void vx_pcm_read_per_bytes(struct vx_core *chip, struct snd_pcm_runtime *runtime,
112 struct vx_pipe *pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
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 */
Takashi Iwaiaf263672005-11-17 14:46:59 +0100139static void vx_set_pcx_time(struct vx_core *chip, pcx_time_t *pc_time,
140 unsigned int *dsp_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
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 */
Takashi Iwaiaf263672005-11-17 14:46:59 +0100156static int vx_set_differed_time(struct vx_core *chip, struct vx_rmh *rmh,
157 struct vx_pipe *pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
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 */
Takashi Iwaiaf263672005-11-17 14:46:59 +0100190static int vx_set_stream_format(struct vx_core *chip, struct vx_pipe *pipe,
191 unsigned int data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
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 */
Takashi Iwaiaf263672005-11-17 14:46:59 +0100217static int vx_set_format(struct vx_core *chip, struct vx_pipe *pipe,
218 struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
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 */
Takashi Iwaiaf263672005-11-17 14:46:59 +0100246static int vx_set_ibl(struct vx_core *chip, struct vx_ibl_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
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 */
Takashi Iwaiaf263672005-11-17 14:46:59 +0100276static int vx_get_pipe_state(struct vx_core *chip, struct vx_pipe *pipe, int *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
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 */
Takashi Iwaiaf263672005-11-17 14:46:59 +0100301static int vx_query_hbuffer_size(struct vx_core *chip, struct vx_pipe *pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
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 */
Takashi Iwaiaf263672005-11-17 14:46:59 +0100325static int vx_pipe_can_start(struct vx_core *chip, struct vx_pipe *pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
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 */
Takashi Iwaiaf263672005-11-17 14:46:59 +0100346static int vx_conf_pipe(struct vx_core *chip, struct vx_pipe *pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
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 */
Takashi Iwaiaf263672005-11-17 14:46:59 +0100360static int vx_send_irqa(struct vx_core *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
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 */
Takashi Iwaiaf263672005-11-17 14:46:59 +0100385static int vx_toggle_pipe(struct vx_core *chip, struct vx_pipe *pipe, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
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 */
Takashi Iwaiaf263672005-11-17 14:46:59 +0100438static int vx_stop_pipe(struct vx_core *chip, struct vx_pipe *pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
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 */
Takashi Iwaiaf263672005-11-17 14:46:59 +0100456static int vx_alloc_pipe(struct vx_core *chip, int capture,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 int audioid, int num_audio,
Takashi Iwaiaf263672005-11-17 14:46:59 +0100458 struct vx_pipe **pipep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
460 int err;
Takashi Iwaiaf263672005-11-17 14:46:59 +0100461 struct vx_pipe *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 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#if 0 // NYI
469 if (underrun_skip_sound)
470 rmh.Cmd[0] |= BIT_SKIP_SOUND;
471#endif // NYI
472 data_mode = (chip->uer_bits & IEC958_AES0_NONAUDIO) != 0;
473 if (! capture && data_mode)
474 rmh.Cmd[0] |= BIT_DATA_MODE;
475 err = vx_send_msg(chip, &rmh);
476 if (err < 0)
477 return err;
478
479 /* initialize the pipe record */
Takashi Iwai561b2202005-09-09 14:22:34 +0200480 pipe = kzalloc(sizeof(*pipe), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 if (! pipe) {
482 /* release the pipe */
483 vx_init_rmh(&rmh, CMD_FREE_PIPE);
484 vx_set_pipe_cmd_params(&rmh, capture, audioid, 0);
485 vx_send_msg(chip, &rmh);
486 return -ENOMEM;
487 }
488
489 /* the pipe index should be identical with the audio index */
490 pipe->number = audioid;
491 pipe->is_capture = capture;
492 pipe->channels = num_audio;
493 pipe->differed_type = 0;
494 pipe->pcx_time = 0;
495 pipe->data_mode = data_mode;
496 *pipep = pipe;
497
498 return 0;
499}
500
501
502/*
503 * vx_free_pipe - release a pipe
504 * @pipe: pipe to be released
505 */
Takashi Iwaiaf263672005-11-17 14:46:59 +0100506static int vx_free_pipe(struct vx_core *chip, struct vx_pipe *pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
508 struct vx_rmh rmh;
509
510 vx_init_rmh(&rmh, CMD_FREE_PIPE);
511 vx_set_pipe_cmd_params(&rmh, pipe->is_capture, pipe->number, 0);
512 vx_send_msg(chip, &rmh);
513
514 kfree(pipe);
515 return 0;
516}
517
518
519/*
520 * vx_start_stream - start the stream
521 *
522 * called from trigger callback only
523 */
Takashi Iwaiaf263672005-11-17 14:46:59 +0100524static int vx_start_stream(struct vx_core *chip, struct vx_pipe *pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
526 struct vx_rmh rmh;
527
528 vx_init_rmh(&rmh, CMD_START_ONE_STREAM);
529 vx_set_stream_cmd_params(&rmh, pipe->is_capture, pipe->number);
530 vx_set_differed_time(chip, &rmh, pipe);
531 return vx_send_msg_nolock(chip, &rmh); /* no lock needed for trigger */
532}
533
534
535/*
536 * vx_stop_stream - stop the stream
537 *
538 * called from trigger callback only
539 */
Takashi Iwaiaf263672005-11-17 14:46:59 +0100540static int vx_stop_stream(struct vx_core *chip, struct vx_pipe *pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
542 struct vx_rmh rmh;
543
544 vx_init_rmh(&rmh, CMD_STOP_STREAM);
545 vx_set_stream_cmd_params(&rmh, pipe->is_capture, pipe->number);
546 return vx_send_msg_nolock(chip, &rmh); /* no lock needed for trigger */
547}
548
549
550/*
551 * playback hw information
552 */
553
Takashi Iwaiaf263672005-11-17 14:46:59 +0100554static struct snd_pcm_hardware vx_pcm_playback_hw = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
Jaroslav Kysela41e48452005-08-18 13:43:12 +0200556 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_MMAP_VALID /*|*/
557 /*SNDRV_PCM_INFO_RESUME*/),
Takashi Iwaiaf263672005-11-17 14:46:59 +0100558 .formats = (/*SNDRV_PCM_FMTBIT_U8 |*/
559 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_3LE),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
561 .rate_min = 5000,
562 .rate_max = 48000,
563 .channels_min = 1,
564 .channels_max = 2,
565 .buffer_bytes_max = (128*1024),
566 .period_bytes_min = 126,
567 .period_bytes_max = (128*1024),
568 .periods_min = 2,
569 .periods_max = VX_MAX_PERIODS,
570 .fifo_size = 126,
571};
572
573
574static void vx_pcm_delayed_start(unsigned long arg);
575
576/*
577 * vx_pcm_playback_open - open callback for playback
578 */
Takashi Iwaiaf263672005-11-17 14:46:59 +0100579static int vx_pcm_playback_open(struct snd_pcm_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580{
Takashi Iwaiaf263672005-11-17 14:46:59 +0100581 struct snd_pcm_runtime *runtime = subs->runtime;
582 struct vx_core *chip = snd_pcm_substream_chip(subs);
583 struct vx_pipe *pipe = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 unsigned int audio;
585 int err;
586
587 if (chip->chip_status & VX_STAT_IS_STALE)
588 return -EBUSY;
589
590 audio = subs->pcm->device * 2;
591 snd_assert(audio < chip->audio_outs, return -EINVAL);
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 */
Takashi Iwaiaf263672005-11-17 14:46:59 +0100623static int vx_pcm_playback_close(struct snd_pcm_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
Takashi Iwaiaf263672005-11-17 14:46:59 +0100625 struct vx_core *chip = snd_pcm_substream_chip(subs);
626 struct vx_pipe *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
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 */
Takashi Iwaiaf263672005-11-17 14:46:59 +0100649static int vx_notify_end_of_buffer(struct vx_core *chip, struct vx_pipe *pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
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 */
Takashi Iwaiaf263672005-11-17 14:46:59 +0100677static int vx_pcm_playback_transfer_chunk(struct vx_core *chip,
678 struct snd_pcm_runtime *runtime,
679 struct vx_pipe *pipe, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
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 */
Takashi Iwaiaf263672005-11-17 14:46:59 +0100715static int vx_update_pipe_position(struct vx_core *chip,
716 struct snd_pcm_runtime *runtime,
717 struct vx_pipe *pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718{
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 */
Takashi Iwaiaf263672005-11-17 14:46:59 +0100743static void vx_pcm_playback_transfer(struct vx_core *chip,
744 struct snd_pcm_substream *subs,
745 struct vx_pipe *pipe, int nchunks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
747 int i, err;
Takashi Iwaiaf263672005-11-17 14:46:59 +0100748 struct snd_pcm_runtime *runtime = subs->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
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 */
Takashi Iwaiaf263672005-11-17 14:46:59 +0100763static void vx_pcm_playback_update(struct vx_core *chip,
764 struct snd_pcm_substream *subs,
765 struct vx_pipe *pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
767 int err;
Takashi Iwaiaf263672005-11-17 14:46:59 +0100768 struct snd_pcm_runtime *runtime = subs->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
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{
Takashi Iwaiaf263672005-11-17 14:46:59 +0100787 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 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 */
Takashi Iwaiaf263672005-11-17 14:46:59 +0100808static int vx_pcm_trigger(struct snd_pcm_substream *subs, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
Takashi Iwaiaf263672005-11-17 14:46:59 +0100810 struct vx_core *chip = snd_pcm_substream_chip(subs);
811 struct vx_pipe *pipe = subs->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 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 */
Takashi Iwaiaf263672005-11-17 14:46:59 +0100855static snd_pcm_uframes_t vx_pcm_playback_pointer(struct snd_pcm_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856{
Takashi Iwaiaf263672005-11-17 14:46:59 +0100857 struct snd_pcm_runtime *runtime = subs->runtime;
858 struct vx_pipe *pipe = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 return pipe->position;
860}
861
862/*
863 * vx_pcm_hw_params - hw_params callback for playback and capture
864 */
Takashi Iwaiaf263672005-11-17 14:46:59 +0100865static int vx_pcm_hw_params(struct snd_pcm_substream *subs,
866 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867{
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 */
Takashi Iwaiaf263672005-11-17 14:46:59 +0100874static int vx_pcm_hw_free(struct snd_pcm_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
876 return snd_pcm_free_vmalloc_buffer(subs);
877}
878
879/*
880 * vx_pcm_prepare - prepare callback for playback and capture
881 */
Takashi Iwaiaf263672005-11-17 14:46:59 +0100882static int vx_pcm_prepare(struct snd_pcm_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883{
Takashi Iwaiaf263672005-11-17 14:46:59 +0100884 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 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) {
Takashi Iwaiaf263672005-11-17 14:46:59 +0100913 snd_printk(KERN_ERR "vx: cannot set different clock %d "
914 "from the current %d\n", runtime->rate, chip->freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 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 */
Takashi Iwaiaf263672005-11-17 14:46:59 +0100947static struct snd_pcm_ops vx_pcm_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 .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
Takashi Iwaiaf263672005-11-17 14:46:59 +0100964static struct snd_pcm_hardware vx_pcm_capture_hw = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
Jaroslav Kysela41e48452005-08-18 13:43:12 +0200966 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_MMAP_VALID /*|*/
967 /*SNDRV_PCM_INFO_RESUME*/),
Takashi Iwaiaf263672005-11-17 14:46:59 +0100968 .formats = (/*SNDRV_PCM_FMTBIT_U8 |*/
969 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_3LE),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 .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 */
Takashi Iwaiaf263672005-11-17 14:46:59 +0100987static int vx_pcm_capture_open(struct snd_pcm_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988{
Takashi Iwaiaf263672005-11-17 14:46:59 +0100989 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 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;
1000 snd_assert(audio < chip->audio_ins, return -EINVAL);
1001 err = vx_alloc_pipe(chip, 1, audio, 2, &pipe);
1002 if (err < 0)
1003 return err;
1004 pipe->substream = subs;
1005 tasklet_init(&pipe->start_tq, vx_pcm_delayed_start, (unsigned long)subs);
1006 chip->capture_pipes[audio] = pipe;
1007
1008 /* check if monitoring is needed */
1009 if (chip->audio_monitor_active[audio]) {
1010 pipe_out_monitoring = chip->playback_pipes[audio];
1011 if (! pipe_out_monitoring) {
1012 /* allocate a pipe */
1013 err = vx_alloc_pipe(chip, 0, audio, 2, &pipe_out_monitoring);
1014 if (err < 0)
1015 return err;
1016 chip->playback_pipes[audio] = pipe_out_monitoring;
1017 }
1018 pipe_out_monitoring->references++;
1019 /*
1020 if an output pipe is available, it's audios still may need to be
1021 unmuted. hence we'll have to call a mixer entry point.
1022 */
Takashi Iwaiaf263672005-11-17 14:46:59 +01001023 vx_set_monitor_level(chip, audio, chip->audio_monitor[audio],
1024 chip->audio_monitor_active[audio]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 /* assuming stereo */
Takashi Iwaiaf263672005-11-17 14:46:59 +01001026 vx_set_monitor_level(chip, audio+1, chip->audio_monitor[audio+1],
1027 chip->audio_monitor_active[audio+1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 }
1029
1030 pipe->monitoring_pipe = pipe_out_monitoring; /* default value NULL */
1031
1032 runtime->hw = vx_pcm_capture_hw;
1033 runtime->hw.period_bytes_min = chip->ibl.size;
1034 runtime->private_data = pipe;
1035
1036 /* align to 4 bytes (otherwise will be problematic when 24bit is used) */
1037 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 4);
1038 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 4);
1039
1040 return 0;
1041}
1042
1043/*
1044 * vx_pcm_capture_close - close callback for capture
1045 */
Takashi Iwaiaf263672005-11-17 14:46:59 +01001046static int vx_pcm_capture_close(struct snd_pcm_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047{
Takashi Iwaiaf263672005-11-17 14:46:59 +01001048 struct vx_core *chip = snd_pcm_substream_chip(subs);
1049 struct vx_pipe *pipe;
1050 struct vx_pipe *pipe_out_monitoring;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
1052 if (! subs->runtime->private_data)
1053 return -EINVAL;
1054 pipe = subs->runtime->private_data;
1055 chip->capture_pipes[pipe->number] = NULL;
1056
1057 pipe_out_monitoring = pipe->monitoring_pipe;
1058
1059 /*
1060 if an output pipe is attached to this input,
1061 check if it needs to be released.
1062 */
1063 if (pipe_out_monitoring) {
1064 if (--pipe_out_monitoring->references == 0) {
1065 vx_free_pipe(chip, pipe_out_monitoring);
1066 chip->playback_pipes[pipe->number] = NULL;
1067 pipe->monitoring_pipe = NULL;
1068 }
1069 }
1070
1071 vx_free_pipe(chip, pipe);
1072 return 0;
1073}
1074
1075
1076
1077#define DMA_READ_ALIGN 6 /* hardware alignment for read */
1078
1079/*
1080 * vx_pcm_capture_update - update the capture buffer
1081 */
Takashi Iwaiaf263672005-11-17 14:46:59 +01001082static void vx_pcm_capture_update(struct vx_core *chip, struct snd_pcm_substream *subs,
1083 struct vx_pipe *pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084{
1085 int size, space, count;
Takashi Iwaiaf263672005-11-17 14:46:59 +01001086 struct snd_pcm_runtime *runtime = subs->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
1088 if (! pipe->prepared || (chip->chip_status & VX_STAT_IS_STALE))
1089 return;
1090
1091 size = runtime->buffer_size - snd_pcm_capture_avail(runtime);
1092 if (! size)
1093 return;
1094 size = frames_to_bytes(runtime, size);
1095 space = vx_query_hbuffer_size(chip, pipe);
1096 if (space < 0)
1097 goto _error;
1098 if (size > space)
1099 size = space;
1100 size = (size / 3) * 3; /* align to 3 bytes */
1101 if (size < DMA_READ_ALIGN)
1102 goto _error;
1103
1104 /* keep the last 6 bytes, they will be read after disconnection */
1105 count = size - DMA_READ_ALIGN;
1106 /* read bytes until the current pointer reaches to the aligned position
1107 * for word-transfer
1108 */
1109 while (count > 0) {
1110 if ((pipe->hw_ptr % pipe->align) == 0)
1111 break;
1112 if (vx_wait_for_rx_full(chip) < 0)
1113 goto _error;
1114 vx_pcm_read_per_bytes(chip, runtime, pipe);
1115 count -= 3;
1116 }
1117 if (count > 0) {
1118 /* ok, let's accelerate! */
1119 int align = pipe->align * 3;
1120 space = (count / align) * align;
1121 vx_pseudo_dma_read(chip, runtime, pipe, space);
1122 count -= space;
1123 }
1124 /* read the rest of bytes */
1125 while (count > 0) {
1126 if (vx_wait_for_rx_full(chip) < 0)
1127 goto _error;
1128 vx_pcm_read_per_bytes(chip, runtime, pipe);
1129 count -= 3;
1130 }
1131 /* disconnect the host, SIZE_HBUF command always switches to the stream mode */
1132 vx_send_rih_nolock(chip, IRQ_CONNECT_STREAM_NEXT);
1133 /* read the last pending 6 bytes */
1134 count = DMA_READ_ALIGN;
1135 while (count > 0) {
1136 vx_pcm_read_per_bytes(chip, runtime, pipe);
1137 count -= 3;
1138 }
1139 /* update the position */
1140 pipe->transferred += size;
1141 if (pipe->transferred >= pipe->period_bytes) {
1142 pipe->transferred %= pipe->period_bytes;
1143 snd_pcm_period_elapsed(subs);
1144 }
1145 return;
1146
1147 _error:
1148 /* disconnect the host, SIZE_HBUF command always switches to the stream mode */
1149 vx_send_rih_nolock(chip, IRQ_CONNECT_STREAM_NEXT);
1150 return;
1151}
1152
1153/*
1154 * vx_pcm_capture_pointer - pointer callback for capture
1155 */
Takashi Iwaiaf263672005-11-17 14:46:59 +01001156static snd_pcm_uframes_t vx_pcm_capture_pointer(struct snd_pcm_substream *subs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157{
Takashi Iwaiaf263672005-11-17 14:46:59 +01001158 struct snd_pcm_runtime *runtime = subs->runtime;
1159 struct vx_pipe *pipe = runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 return bytes_to_frames(runtime, pipe->hw_ptr);
1161}
1162
1163/*
1164 * operators for PCM capture
1165 */
Takashi Iwaiaf263672005-11-17 14:46:59 +01001166static struct snd_pcm_ops vx_pcm_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 .open = vx_pcm_capture_open,
1168 .close = vx_pcm_capture_close,
1169 .ioctl = snd_pcm_lib_ioctl,
1170 .hw_params = vx_pcm_hw_params,
1171 .hw_free = vx_pcm_hw_free,
1172 .prepare = vx_pcm_prepare,
1173 .trigger = vx_pcm_trigger,
1174 .pointer = vx_pcm_capture_pointer,
1175 .page = snd_pcm_get_vmalloc_page,
1176};
1177
1178
1179/*
1180 * interrupt handler for pcm streams
1181 */
Takashi Iwaiaf263672005-11-17 14:46:59 +01001182void vx_pcm_update_intr(struct vx_core *chip, unsigned int events)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183{
1184 unsigned int i;
Takashi Iwaiaf263672005-11-17 14:46:59 +01001185 struct vx_pipe *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
1187#define EVENT_MASK (END_OF_BUFFER_EVENTS_PENDING|ASYNC_EVENTS_PENDING)
1188
1189 if (events & EVENT_MASK) {
1190 vx_init_rmh(&chip->irq_rmh, CMD_ASYNC);
1191 if (events & ASYNC_EVENTS_PENDING)
1192 chip->irq_rmh.Cmd[0] |= 0x00000001; /* SEL_ASYNC_EVENTS */
1193 if (events & END_OF_BUFFER_EVENTS_PENDING)
1194 chip->irq_rmh.Cmd[0] |= 0x00000002; /* SEL_END_OF_BUF_EVENTS */
1195
1196 if (vx_send_msg(chip, &chip->irq_rmh) < 0) {
1197 snd_printdd(KERN_ERR "msg send error!!\n");
1198 return;
1199 }
1200
1201 i = 1;
1202 while (i < chip->irq_rmh.LgStat) {
1203 int p, buf, capture, eob;
1204 p = chip->irq_rmh.Stat[i] & MASK_FIRST_FIELD;
1205 capture = (chip->irq_rmh.Stat[i] & 0x400000) ? 1 : 0;
1206 eob = (chip->irq_rmh.Stat[i] & 0x800000) ? 1 : 0;
1207 i++;
1208 if (events & ASYNC_EVENTS_PENDING)
1209 i++;
1210 buf = 1; /* force to transfer */
1211 if (events & END_OF_BUFFER_EVENTS_PENDING) {
1212 if (eob)
1213 buf = chip->irq_rmh.Stat[i];
1214 i++;
1215 }
1216 if (capture)
1217 continue;
1218 snd_assert(p >= 0 && (unsigned int)p < chip->audio_outs,);
1219 pipe = chip->playback_pipes[p];
1220 if (pipe && pipe->substream) {
1221 vx_pcm_playback_update(chip, pipe->substream, pipe);
1222 vx_pcm_playback_transfer(chip, pipe->substream, pipe, buf);
1223 }
1224 }
1225 }
1226
1227 /* update the capture pcm pointers as frequently as possible */
1228 for (i = 0; i < chip->audio_ins; i++) {
1229 pipe = chip->capture_pipes[i];
1230 if (pipe && pipe->substream)
1231 vx_pcm_capture_update(chip, pipe->substream, pipe);
1232 }
1233}
1234
1235
1236/*
1237 * vx_init_audio_io - check the availabe audio i/o and allocate pipe arrays
1238 */
Takashi Iwaiaf263672005-11-17 14:46:59 +01001239static int vx_init_audio_io(struct vx_core *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240{
1241 struct vx_rmh rmh;
1242 int preferred;
1243
1244 vx_init_rmh(&rmh, CMD_SUPPORTED);
1245 if (vx_send_msg(chip, &rmh) < 0) {
1246 snd_printk(KERN_ERR "vx: cannot get the supported audio data\n");
1247 return -ENXIO;
1248 }
1249
1250 chip->audio_outs = rmh.Stat[0] & MASK_FIRST_FIELD;
1251 chip->audio_ins = (rmh.Stat[0] >> (FIELD_SIZE*2)) & MASK_FIRST_FIELD;
1252 chip->audio_info = rmh.Stat[1];
1253
1254 /* allocate pipes */
Panagiotis Issaris59feddb2006-07-25 15:28:03 +02001255 chip->playback_pipes = kcalloc(chip->audio_outs, sizeof(struct vx_pipe *), GFP_KERNEL);
Alexey Dobriyanac57b842006-03-06 13:21:30 +01001256 if (!chip->playback_pipes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 return -ENOMEM;
Panagiotis Issaris59feddb2006-07-25 15:28:03 +02001258 chip->capture_pipes = kcalloc(chip->audio_ins, sizeof(struct vx_pipe *), GFP_KERNEL);
Alexey Dobriyanac57b842006-03-06 13:21:30 +01001259 if (!chip->capture_pipes) {
1260 kfree(chip->playback_pipes);
1261 return -ENOMEM;
1262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 preferred = chip->ibl.size;
1265 chip->ibl.size = 0;
1266 vx_set_ibl(chip, &chip->ibl); /* query the info */
1267 if (preferred > 0) {
Takashi Iwaiaf263672005-11-17 14:46:59 +01001268 chip->ibl.size = ((preferred + chip->ibl.granularity - 1) /
1269 chip->ibl.granularity) * chip->ibl.granularity;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 if (chip->ibl.size > chip->ibl.max_size)
1271 chip->ibl.size = chip->ibl.max_size;
1272 } else
1273 chip->ibl.size = chip->ibl.min_size; /* set to the minimum */
1274 vx_set_ibl(chip, &chip->ibl);
1275
1276 return 0;
1277}
1278
1279
1280/*
1281 * free callback for pcm
1282 */
Takashi Iwaiaf263672005-11-17 14:46:59 +01001283static void snd_vx_pcm_free(struct snd_pcm *pcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284{
Takashi Iwaiaf263672005-11-17 14:46:59 +01001285 struct vx_core *chip = pcm->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 chip->pcm[pcm->device] = NULL;
Jesper Juhl4d572772005-05-30 17:30:32 +02001287 kfree(chip->playback_pipes);
1288 chip->playback_pipes = NULL;
1289 kfree(chip->capture_pipes);
1290 chip->capture_pipes = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291}
1292
1293/*
1294 * snd_vx_pcm_new - create and initialize a pcm
1295 */
Takashi Iwaiaf263672005-11-17 14:46:59 +01001296int snd_vx_pcm_new(struct vx_core *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297{
Takashi Iwaiaf263672005-11-17 14:46:59 +01001298 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 unsigned int i;
1300 int err;
1301
1302 if ((err = vx_init_audio_io(chip)) < 0)
1303 return err;
1304
1305 for (i = 0; i < chip->hw->num_codecs; i++) {
1306 unsigned int outs, ins;
1307 outs = chip->audio_outs > i * 2 ? 1 : 0;
1308 ins = chip->audio_ins > i * 2 ? 1 : 0;
1309 if (! outs && ! ins)
1310 break;
1311 err = snd_pcm_new(chip->card, "VX PCM", i,
1312 outs, ins, &pcm);
1313 if (err < 0)
1314 return err;
1315 if (outs)
1316 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &vx_pcm_playback_ops);
1317 if (ins)
1318 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &vx_pcm_capture_ops);
1319
1320 pcm->private_data = chip;
1321 pcm->private_free = snd_vx_pcm_free;
1322 pcm->info_flags = 0;
1323 strcpy(pcm->name, chip->card->shortname);
1324 chip->pcm[i] = pcm;
1325 }
1326
1327 return 0;
1328}