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