xref: /openbmc/linux/sound/drivers/vx/vx_pcm.c (revision 37b9b9a5)
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_hw_params - hw_params callback for playback and capture
777  */
778 static int vx_pcm_hw_params(struct snd_pcm_substream *subs,
779 				     struct snd_pcm_hw_params *hw_params)
780 {
781 	return snd_pcm_lib_malloc_pages(subs, params_buffer_bytes(hw_params));
782 }
783 
784 /*
785  * vx_pcm_hw_free - hw_free callback for playback and capture
786  */
787 static int vx_pcm_hw_free(struct snd_pcm_substream *subs)
788 {
789 	return snd_pcm_lib_free_pages(subs);
790 }
791 
792 /*
793  * vx_pcm_prepare - prepare callback for playback and capture
794  */
795 static int vx_pcm_prepare(struct snd_pcm_substream *subs)
796 {
797 	struct vx_core *chip = snd_pcm_substream_chip(subs);
798 	struct snd_pcm_runtime *runtime = subs->runtime;
799 	struct vx_pipe *pipe = runtime->private_data;
800 	int err, data_mode;
801 	// int max_size, nchunks;
802 
803 	if (chip->chip_status & VX_STAT_IS_STALE)
804 		return -EBUSY;
805 
806 	data_mode = (chip->uer_bits & IEC958_AES0_NONAUDIO) != 0;
807 	if (data_mode != pipe->data_mode && ! pipe->is_capture) {
808 		/* IEC958 status (raw-mode) was changed */
809 		/* we reopen the pipe */
810 		struct vx_rmh rmh;
811 		snd_printdd(KERN_DEBUG "reopen the pipe with data_mode = %d\n", data_mode);
812 		vx_init_rmh(&rmh, CMD_FREE_PIPE);
813 		vx_set_pipe_cmd_params(&rmh, 0, pipe->number, 0);
814 		if ((err = vx_send_msg(chip, &rmh)) < 0)
815 			return err;
816 		vx_init_rmh(&rmh, CMD_RES_PIPE);
817 		vx_set_pipe_cmd_params(&rmh, 0, pipe->number, pipe->channels);
818 		if (data_mode)
819 			rmh.Cmd[0] |= BIT_DATA_MODE;
820 		if ((err = vx_send_msg(chip, &rmh)) < 0)
821 			return err;
822 		pipe->data_mode = data_mode;
823 	}
824 
825 	if (chip->pcm_running && chip->freq != runtime->rate) {
826 		snd_printk(KERN_ERR "vx: cannot set different clock %d "
827 			   "from the current %d\n", runtime->rate, chip->freq);
828 		return -EINVAL;
829 	}
830 	vx_set_clock(chip, runtime->rate);
831 
832 	if ((err = vx_set_format(chip, pipe, runtime)) < 0)
833 		return err;
834 
835 	if (vx_is_pcmcia(chip)) {
836 		pipe->align = 2; /* 16bit word */
837 	} else {
838 		pipe->align = 4; /* 32bit word */
839 	}
840 
841 	pipe->buffer_bytes = frames_to_bytes(runtime, runtime->buffer_size);
842 	pipe->period_bytes = frames_to_bytes(runtime, runtime->period_size);
843 	pipe->hw_ptr = 0;
844 
845 	/* set the timestamp */
846 	vx_update_pipe_position(chip, runtime, pipe);
847 	/* clear again */
848 	pipe->transferred = 0;
849 	pipe->position = 0;
850 
851 	pipe->prepared = 1;
852 
853 	return 0;
854 }
855 
856 
857 /*
858  * operators for PCM playback
859  */
860 static const struct snd_pcm_ops vx_pcm_playback_ops = {
861 	.open =		vx_pcm_playback_open,
862 	.close =	vx_pcm_playback_close,
863 	.ioctl =	snd_pcm_lib_ioctl,
864 	.hw_params =	vx_pcm_hw_params,
865 	.hw_free =	vx_pcm_hw_free,
866 	.prepare =	vx_pcm_prepare,
867 	.trigger =	vx_pcm_trigger,
868 	.pointer =	vx_pcm_playback_pointer,
869 };
870 
871 
872 /*
873  * playback hw information
874  */
875 
876 static const struct snd_pcm_hardware vx_pcm_capture_hw = {
877 	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
878 				 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_MMAP_VALID /*|*/
879 				 /*SNDRV_PCM_INFO_RESUME*/),
880 	.formats =		(/*SNDRV_PCM_FMTBIT_U8 |*/
881 				 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_3LE),
882 	.rates =		SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
883 	.rate_min =		5000,
884 	.rate_max =		48000,
885 	.channels_min =		1,
886 	.channels_max =		2,
887 	.buffer_bytes_max =	(128*1024),
888 	.period_bytes_min =	126,
889 	.period_bytes_max =	(128*1024),
890 	.periods_min =		2,
891 	.periods_max =		VX_MAX_PERIODS,
892 	.fifo_size =		126,
893 };
894 
895 
896 /*
897  * vx_pcm_capture_open - open callback for capture
898  */
899 static int vx_pcm_capture_open(struct snd_pcm_substream *subs)
900 {
901 	struct snd_pcm_runtime *runtime = subs->runtime;
902 	struct vx_core *chip = snd_pcm_substream_chip(subs);
903 	struct vx_pipe *pipe;
904 	struct vx_pipe *pipe_out_monitoring = NULL;
905 	unsigned int audio;
906 	int err;
907 
908 	if (chip->chip_status & VX_STAT_IS_STALE)
909 		return -EBUSY;
910 
911 	audio = subs->pcm->device * 2;
912 	if (snd_BUG_ON(audio >= chip->audio_ins))
913 		return -EINVAL;
914 	err = vx_alloc_pipe(chip, 1, audio, 2, &pipe);
915 	if (err < 0)
916 		return err;
917 	pipe->substream = subs;
918 	chip->capture_pipes[audio] = pipe;
919 
920 	/* check if monitoring is needed */
921 	if (chip->audio_monitor_active[audio]) {
922 		pipe_out_monitoring = chip->playback_pipes[audio];
923 		if (! pipe_out_monitoring) {
924 			/* allocate a pipe */
925 			err = vx_alloc_pipe(chip, 0, audio, 2, &pipe_out_monitoring);
926 			if (err < 0)
927 				return err;
928 			chip->playback_pipes[audio] = pipe_out_monitoring;
929 		}
930 		pipe_out_monitoring->references++;
931 		/*
932 		   if an output pipe is available, it's audios still may need to be
933 		   unmuted. hence we'll have to call a mixer entry point.
934 		*/
935 		vx_set_monitor_level(chip, audio, chip->audio_monitor[audio],
936 				     chip->audio_monitor_active[audio]);
937 		/* assuming stereo */
938 		vx_set_monitor_level(chip, audio+1, chip->audio_monitor[audio+1],
939 				     chip->audio_monitor_active[audio+1]);
940 	}
941 
942 	pipe->monitoring_pipe = pipe_out_monitoring; /* default value NULL */
943 
944 	runtime->hw = vx_pcm_capture_hw;
945 	runtime->hw.period_bytes_min = chip->ibl.size;
946 	runtime->private_data = pipe;
947 
948 	/* align to 4 bytes (otherwise will be problematic when 24bit is used) */
949 	snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 4);
950 	snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 4);
951 
952 	return 0;
953 }
954 
955 /*
956  * vx_pcm_capture_close - close callback for capture
957  */
958 static int vx_pcm_capture_close(struct snd_pcm_substream *subs)
959 {
960 	struct vx_core *chip = snd_pcm_substream_chip(subs);
961 	struct vx_pipe *pipe;
962 	struct vx_pipe *pipe_out_monitoring;
963 
964 	if (! subs->runtime->private_data)
965 		return -EINVAL;
966 	pipe = subs->runtime->private_data;
967 	chip->capture_pipes[pipe->number] = NULL;
968 
969 	pipe_out_monitoring = pipe->monitoring_pipe;
970 
971 	/*
972 	  if an output pipe is attached to this input,
973 	  check if it needs to be released.
974 	*/
975 	if (pipe_out_monitoring) {
976 		if (--pipe_out_monitoring->references == 0) {
977 			vx_free_pipe(chip, pipe_out_monitoring);
978 			chip->playback_pipes[pipe->number] = NULL;
979 			pipe->monitoring_pipe = NULL;
980 		}
981 	}
982 
983 	vx_free_pipe(chip, pipe);
984 	return 0;
985 }
986 
987 
988 
989 #define DMA_READ_ALIGN	6	/* hardware alignment for read */
990 
991 /*
992  * vx_pcm_capture_update - update the capture buffer
993  */
994 static void vx_pcm_capture_update(struct vx_core *chip, struct snd_pcm_substream *subs,
995 				  struct vx_pipe *pipe)
996 {
997 	int size, space, count;
998 	struct snd_pcm_runtime *runtime = subs->runtime;
999 
1000 	if (!pipe->running || (chip->chip_status & VX_STAT_IS_STALE))
1001 		return;
1002 
1003 	size = runtime->buffer_size - snd_pcm_capture_avail(runtime);
1004 	if (! size)
1005 		return;
1006 	size = frames_to_bytes(runtime, size);
1007 	space = vx_query_hbuffer_size(chip, pipe);
1008 	if (space < 0)
1009 		goto _error;
1010 	if (size > space)
1011 		size = space;
1012 	size = (size / 3) * 3; /* align to 3 bytes */
1013 	if (size < DMA_READ_ALIGN)
1014 		goto _error;
1015 
1016 	/* keep the last 6 bytes, they will be read after disconnection */
1017 	count = size - DMA_READ_ALIGN;
1018 	/* read bytes until the current pointer reaches to the aligned position
1019 	 * for word-transfer
1020 	 */
1021 	while (count > 0) {
1022 		if ((pipe->hw_ptr % pipe->align) == 0)
1023 			break;
1024 		if (vx_wait_for_rx_full(chip) < 0)
1025 			goto _error;
1026 		vx_pcm_read_per_bytes(chip, runtime, pipe);
1027 		count -= 3;
1028 	}
1029 	if (count > 0) {
1030 		/* ok, let's accelerate! */
1031 		int align = pipe->align * 3;
1032 		space = (count / align) * align;
1033 		if (space > 0) {
1034 			vx_pseudo_dma_read(chip, runtime, pipe, space);
1035 			count -= space;
1036 		}
1037 	}
1038 	/* read the rest of bytes */
1039 	while (count > 0) {
1040 		if (vx_wait_for_rx_full(chip) < 0)
1041 			goto _error;
1042 		vx_pcm_read_per_bytes(chip, runtime, pipe);
1043 		count -= 3;
1044 	}
1045 	/* disconnect the host, SIZE_HBUF command always switches to the stream mode */
1046 	vx_send_rih(chip, IRQ_CONNECT_STREAM_NEXT);
1047 	/* read the last pending 6 bytes */
1048 	count = DMA_READ_ALIGN;
1049 	while (count > 0) {
1050 		vx_pcm_read_per_bytes(chip, runtime, pipe);
1051 		count -= 3;
1052 	}
1053 	/* update the position */
1054 	pipe->transferred += size;
1055 	if (pipe->transferred >= pipe->period_bytes) {
1056 		pipe->transferred %= pipe->period_bytes;
1057 		snd_pcm_period_elapsed(subs);
1058 	}
1059 	return;
1060 
1061  _error:
1062 	/* disconnect the host, SIZE_HBUF command always switches to the stream mode */
1063 	vx_send_rih(chip, IRQ_CONNECT_STREAM_NEXT);
1064 	return;
1065 }
1066 
1067 /*
1068  * vx_pcm_capture_pointer - pointer callback for capture
1069  */
1070 static snd_pcm_uframes_t vx_pcm_capture_pointer(struct snd_pcm_substream *subs)
1071 {
1072 	struct snd_pcm_runtime *runtime = subs->runtime;
1073 	struct vx_pipe *pipe = runtime->private_data;
1074 	return bytes_to_frames(runtime, pipe->hw_ptr);
1075 }
1076 
1077 /*
1078  * operators for PCM capture
1079  */
1080 static const struct snd_pcm_ops vx_pcm_capture_ops = {
1081 	.open =		vx_pcm_capture_open,
1082 	.close =	vx_pcm_capture_close,
1083 	.ioctl =	snd_pcm_lib_ioctl,
1084 	.hw_params =	vx_pcm_hw_params,
1085 	.hw_free =	vx_pcm_hw_free,
1086 	.prepare =	vx_pcm_prepare,
1087 	.trigger =	vx_pcm_trigger,
1088 	.pointer =	vx_pcm_capture_pointer,
1089 };
1090 
1091 
1092 /*
1093  * interrupt handler for pcm streams
1094  */
1095 void vx_pcm_update_intr(struct vx_core *chip, unsigned int events)
1096 {
1097 	unsigned int i;
1098 	struct vx_pipe *pipe;
1099 
1100 #define EVENT_MASK	(END_OF_BUFFER_EVENTS_PENDING|ASYNC_EVENTS_PENDING)
1101 
1102 	if (events & EVENT_MASK) {
1103 		vx_init_rmh(&chip->irq_rmh, CMD_ASYNC);
1104 		if (events & ASYNC_EVENTS_PENDING)
1105 			chip->irq_rmh.Cmd[0] |= 0x00000001;	/* SEL_ASYNC_EVENTS */
1106 		if (events & END_OF_BUFFER_EVENTS_PENDING)
1107 			chip->irq_rmh.Cmd[0] |= 0x00000002;	/* SEL_END_OF_BUF_EVENTS */
1108 
1109 		if (vx_send_msg(chip, &chip->irq_rmh) < 0) {
1110 			snd_printdd(KERN_ERR "msg send error!!\n");
1111 			return;
1112 		}
1113 
1114 		i = 1;
1115 		while (i < chip->irq_rmh.LgStat) {
1116 			int p, buf, capture, eob;
1117 			p = chip->irq_rmh.Stat[i] & MASK_FIRST_FIELD;
1118 			capture = (chip->irq_rmh.Stat[i] & 0x400000) ? 1 : 0;
1119 			eob = (chip->irq_rmh.Stat[i] & 0x800000) ? 1 : 0;
1120 			i++;
1121 			if (events & ASYNC_EVENTS_PENDING)
1122 				i++;
1123 			buf = 1; /* force to transfer */
1124 			if (events & END_OF_BUFFER_EVENTS_PENDING) {
1125 				if (eob)
1126 					buf = chip->irq_rmh.Stat[i];
1127 				i++;
1128 			}
1129 			if (capture)
1130 				continue;
1131 			if (snd_BUG_ON(p < 0 || p >= chip->audio_outs))
1132 				continue;
1133 			pipe = chip->playback_pipes[p];
1134 			if (pipe && pipe->substream) {
1135 				vx_pcm_playback_update(chip, pipe->substream, pipe);
1136 				vx_pcm_playback_transfer(chip, pipe->substream, pipe, buf);
1137 			}
1138 		}
1139 	}
1140 
1141 	/* update the capture pcm pointers as frequently as possible */
1142 	for (i = 0; i < chip->audio_ins; i++) {
1143 		pipe = chip->capture_pipes[i];
1144 		if (pipe && pipe->substream)
1145 			vx_pcm_capture_update(chip, pipe->substream, pipe);
1146 	}
1147 }
1148 
1149 
1150 /*
1151  * vx_init_audio_io - check the available audio i/o and allocate pipe arrays
1152  */
1153 static int vx_init_audio_io(struct vx_core *chip)
1154 {
1155 	struct vx_rmh rmh;
1156 	int preferred;
1157 
1158 	vx_init_rmh(&rmh, CMD_SUPPORTED);
1159 	if (vx_send_msg(chip, &rmh) < 0) {
1160 		snd_printk(KERN_ERR "vx: cannot get the supported audio data\n");
1161 		return -ENXIO;
1162 	}
1163 
1164 	chip->audio_outs = rmh.Stat[0] & MASK_FIRST_FIELD;
1165 	chip->audio_ins = (rmh.Stat[0] >> (FIELD_SIZE*2)) & MASK_FIRST_FIELD;
1166 	chip->audio_info = rmh.Stat[1];
1167 
1168 	/* allocate pipes */
1169 	chip->playback_pipes = kcalloc(chip->audio_outs, sizeof(struct vx_pipe *), GFP_KERNEL);
1170 	if (!chip->playback_pipes)
1171 		return -ENOMEM;
1172 	chip->capture_pipes = kcalloc(chip->audio_ins, sizeof(struct vx_pipe *), GFP_KERNEL);
1173 	if (!chip->capture_pipes) {
1174 		kfree(chip->playback_pipes);
1175 		return -ENOMEM;
1176 	}
1177 
1178 	preferred = chip->ibl.size;
1179 	chip->ibl.size = 0;
1180 	vx_set_ibl(chip, &chip->ibl); /* query the info */
1181 	if (preferred > 0) {
1182 		chip->ibl.size = ((preferred + chip->ibl.granularity - 1) /
1183 				  chip->ibl.granularity) * chip->ibl.granularity;
1184 		if (chip->ibl.size > chip->ibl.max_size)
1185 			chip->ibl.size = chip->ibl.max_size;
1186 	} else
1187 		chip->ibl.size = chip->ibl.min_size; /* set to the minimum */
1188 	vx_set_ibl(chip, &chip->ibl);
1189 
1190 	return 0;
1191 }
1192 
1193 
1194 /*
1195  * free callback for pcm
1196  */
1197 static void snd_vx_pcm_free(struct snd_pcm *pcm)
1198 {
1199 	struct vx_core *chip = pcm->private_data;
1200 	chip->pcm[pcm->device] = NULL;
1201 	kfree(chip->playback_pipes);
1202 	chip->playback_pipes = NULL;
1203 	kfree(chip->capture_pipes);
1204 	chip->capture_pipes = NULL;
1205 }
1206 
1207 /*
1208  * snd_vx_pcm_new - create and initialize a pcm
1209  */
1210 int snd_vx_pcm_new(struct vx_core *chip)
1211 {
1212 	struct snd_pcm *pcm;
1213 	unsigned int i;
1214 	int err;
1215 
1216 	if ((err = vx_init_audio_io(chip)) < 0)
1217 		return err;
1218 
1219 	for (i = 0; i < chip->hw->num_codecs; i++) {
1220 		unsigned int outs, ins;
1221 		outs = chip->audio_outs > i * 2 ? 1 : 0;
1222 		ins = chip->audio_ins > i * 2 ? 1 : 0;
1223 		if (! outs && ! ins)
1224 			break;
1225 		err = snd_pcm_new(chip->card, "VX PCM", i,
1226 				  outs, ins, &pcm);
1227 		if (err < 0)
1228 			return err;
1229 		if (outs)
1230 			snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &vx_pcm_playback_ops);
1231 		if (ins)
1232 			snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &vx_pcm_capture_ops);
1233 		snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_VMALLOC,
1234 						      snd_dma_continuous_data(GFP_KERNEL | GFP_DMA32),
1235 						      0, 0);
1236 
1237 		pcm->private_data = chip;
1238 		pcm->private_free = snd_vx_pcm_free;
1239 		pcm->info_flags = 0;
1240 		pcm->nonatomic = true;
1241 		strcpy(pcm->name, chip->card->shortname);
1242 		chip->pcm[i] = pcm;
1243 	}
1244 
1245 	return 0;
1246 }
1247