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