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