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#if PAGE_SIZE < 4096
32#error PAGE_SIZE is < 4k
33#endif
34
35static int restore_dsp_rettings(struct echoaudio *chip);
36
37
38
39
40
41
42static int wait_handshake(struct echoaudio *chip)
43{
44 int i;
45
46
47 for (i = 0; i < HANDSHAKE_TIMEOUT; i++) {
48
49 barrier();
50 if (chip->comm_page->handshake) {
51 return 0;
52 }
53 udelay(1);
54 }
55
56 snd_printk(KERN_ERR "wait_handshake(): Timeout waiting for DSP\n");
57 return -EBUSY;
58}
59
60
61
62
63
64
65
66static int send_vector(struct echoaudio *chip, u32 command)
67{
68 int i;
69
70 wmb();
71
72
73 for (i = 0; i < VECTOR_BUSY_TIMEOUT; i++) {
74 if (!(get_dsp_register(chip, CHI32_VECTOR_REG) &
75 CHI32_VECTOR_BUSY)) {
76 set_dsp_register(chip, CHI32_VECTOR_REG, command);
77
78 return 0;
79 }
80 udelay(1);
81 }
82
83 DE_ACT((KERN_ERR "timeout on send_vector\n"));
84 return -EBUSY;
85}
86
87
88
89
90
91static int write_dsp(struct echoaudio *chip, u32 data)
92{
93 u32 status, i;
94
95 for (i = 0; i < 10000000; i++) {
96 status = get_dsp_register(chip, CHI32_STATUS_REG);
97 if ((status & CHI32_STATUS_HOST_WRITE_EMPTY) != 0) {
98 set_dsp_register(chip, CHI32_DATA_REG, data);
99 wmb();
100 return 0;
101 }
102 udelay(1);
103 cond_resched();
104 }
105
106 chip->bad_board = TRUE;
107 DE_ACT((KERN_ERR "write_dsp: Set bad_board to TRUE\n"));
108 return -EIO;
109}
110
111
112
113
114
115static int read_dsp(struct echoaudio *chip, u32 *data)
116{
117 u32 status, i;
118
119 for (i = 0; i < READ_DSP_TIMEOUT; i++) {
120 status = get_dsp_register(chip, CHI32_STATUS_REG);
121 if ((status & CHI32_STATUS_HOST_READ_FULL) != 0) {
122 *data = get_dsp_register(chip, CHI32_DATA_REG);
123 return 0;
124 }
125 udelay(1);
126 cond_resched();
127 }
128
129 chip->bad_board = TRUE;
130 DE_INIT((KERN_ERR "read_dsp: Set bad_board to TRUE\n"));
131 return -EIO;
132}
133
134
135
136
137
138
139
140
141
142
143
144
145static int read_sn(struct echoaudio *chip)
146{
147 int i;
148 u32 sn[6];
149
150 for (i = 0; i < 5; i++) {
151 if (read_dsp(chip, &sn[i])) {
152 snd_printk(KERN_ERR "Failed to read serial number\n");
153 return -EIO;
154 }
155 }
156 DE_INIT(("Read serial number %08x %08x %08x %08x %08x\n",
157 sn[0], sn[1], sn[2], sn[3], sn[4]));
158 return 0;
159}
160
161
162
163#ifndef ECHOCARD_HAS_ASIC
164
165static inline int check_asic_status(struct echoaudio *chip)
166{
167 chip->asic_loaded = TRUE;
168 return 0;
169}
170
171#endif
172
173
174
175#ifdef ECHOCARD_HAS_ASIC
176
177
178static int load_asic_generic(struct echoaudio *chip, u32 cmd,
179 const struct firmware *asic)
180{
181 const struct firmware *fw;
182 int err;
183 u32 i, size;
184 u8 *code;
185
186 if ((err = get_firmware(&fw, asic, chip)) < 0) {
187 snd_printk(KERN_WARNING "Firmware not found !\n");
188 return err;
189 }
190
191 code = (u8 *)fw->data;
192 size = fw->size;
193
194
195 if (write_dsp(chip, cmd) < 0)
196 goto la_error;
197
198
199 if (write_dsp(chip, size) < 0)
200 goto la_error;
201
202 for (i = 0; i < size; i++) {
203 if (write_dsp(chip, code[i]) < 0)
204 goto la_error;
205 }
206
207 DE_INIT(("ASIC loaded\n"));
208 free_firmware(fw);
209 return 0;
210
211la_error:
212 DE_INIT(("failed on write_dsp\n"));
213 free_firmware(fw);
214 return -EIO;
215}
216
217#endif
218
219
220
221#ifdef DSP_56361
222
223
224
225
226static int install_resident_loader(struct echoaudio *chip)
227{
228 u32 address;
229 int index, words, i;
230 u16 *code;
231 u32 status;
232 const struct firmware *fw;
233
234
235
236 if (chip->device_id != DEVICE_ID_56361)
237 return 0;
238
239
240
241 status = get_dsp_register(chip, CHI32_STATUS_REG);
242 if (status & CHI32_STATUS_REG_HF5) {
243 DE_INIT(("Resident loader already installed; status is 0x%x\n",
244 status));
245 return 0;
246 }
247
248 if ((i = get_firmware(&fw, &card_fw[FW_361_LOADER], chip)) < 0) {
249 snd_printk(KERN_WARNING "Firmware not found !\n");
250 return i;
251 }
252
253
254
255
256
257
258
259
260
261
262 set_dsp_register(chip, CHI32_CONTROL_REG,
263 get_dsp_register(chip, CHI32_CONTROL_REG) | 0x900);
264
265 code = (u16 *)fw->data;
266
267
268
269
270 index = code[0];
271
272
273 index += 3;
274
275
276 words = code[index++];
277
278
279 address = ((u32)code[index] << 16) + code[index + 1];
280 index += 2;
281
282
283 if (write_dsp(chip, words)) {
284 DE_INIT(("install_resident_loader: Failed to write word count!\n"));
285 goto irl_error;
286 }
287
288 if (write_dsp(chip, address)) {
289 DE_INIT(("install_resident_loader: Failed to write DSP address!\n"));
290 goto irl_error;
291 }
292
293 for (i = 0; i < words; i++) {
294 u32 data;
295
296 data = ((u32)code[index] << 16) + code[index + 1];
297 if (write_dsp(chip, data)) {
298 DE_INIT(("install_resident_loader: Failed to write DSP code\n"));
299 goto irl_error;
300 }
301 index += 2;
302 }
303
304
305 for (i = 0; i < 200; i++) {
306 udelay(50);
307 status = get_dsp_register(chip, CHI32_STATUS_REG);
308 if (status & CHI32_STATUS_REG_HF5)
309 break;
310 }
311
312 if (i == 200) {
313 DE_INIT(("Resident loader failed to set HF5\n"));
314 goto irl_error;
315 }
316
317 DE_INIT(("Resident loader successfully installed\n"));
318 free_firmware(fw);
319 return 0;
320
321irl_error:
322 free_firmware(fw);
323 return -EIO;
324}
325
326#endif
327
328
329static int load_dsp(struct echoaudio *chip, u16 *code)
330{
331 u32 address, data;
332 int index, words, i;
333
334 if (chip->dsp_code == code) {
335 DE_INIT(("DSP is already loaded!\n"));
336 return 0;
337 }
338 chip->bad_board = TRUE;
339 chip->dsp_code = NULL;
340 chip->asic_loaded = FALSE;
341
342 DE_INIT(("load_dsp: Set bad_board to TRUE\n"));
343
344
345#ifdef DSP_56361
346 if ((i = install_resident_loader(chip)) < 0)
347 return i;
348#endif
349
350
351 if (send_vector(chip, DSP_VC_RESET) < 0) {
352 DE_INIT(("LoadDsp: send_vector DSP_VC_RESET failed, Critical Failure\n"));
353 return -EIO;
354 }
355
356 udelay(10);
357
358
359 for (i = 0; i < 1000; i++) {
360 if (get_dsp_register(chip, CHI32_STATUS_REG) &
361 CHI32_STATUS_REG_HF3)
362 break;
363 udelay(10);
364 }
365
366 if (i == 1000) {
367 DE_INIT(("load_dsp: Timeout waiting for CHI32_STATUS_REG_HF3\n"));
368 return -EIO;
369 }
370
371
372 set_dsp_register(chip, CHI32_CONTROL_REG,
373 get_dsp_register(chip, CHI32_CONTROL_REG) | 0x900);
374
375
376
377 index = code[0];
378 for (;;) {
379 int block_type, mem_type;
380
381
382 index++;
383
384
385 block_type = code[index];
386 if (block_type == 4)
387 break;
388
389 index++;
390
391
392 mem_type = code[index++];
393
394
395 words = code[index++];
396 if (words == 0)
397 break;
398
399
400 address = ((u32)code[index] << 16) + code[index + 1];
401 index += 2;
402
403 if (write_dsp(chip, words) < 0) {
404 DE_INIT(("load_dsp: failed to write number of DSP words\n"));
405 return -EIO;
406 }
407 if (write_dsp(chip, address) < 0) {
408 DE_INIT(("load_dsp: failed to write DSP address\n"));
409 return -EIO;
410 }
411 if (write_dsp(chip, mem_type) < 0) {
412 DE_INIT(("load_dsp: failed to write DSP memory type\n"));
413 return -EIO;
414 }
415
416 for (i = 0; i < words; i++, index+=2) {
417 data = ((u32)code[index] << 16) + code[index + 1];
418 if (write_dsp(chip, data) < 0) {
419 DE_INIT(("load_dsp: failed to write DSP data\n"));
420 return -EIO;
421 }
422 }
423 }
424
425 if (write_dsp(chip, 0) < 0) {
426 DE_INIT(("load_dsp: Failed to write final zero\n"));
427 return -EIO;
428 }
429 udelay(10);
430
431 for (i = 0; i < 5000; i++) {
432
433 if (get_dsp_register(chip, CHI32_STATUS_REG) &
434 CHI32_STATUS_REG_HF4) {
435 set_dsp_register(chip, CHI32_CONTROL_REG,
436 get_dsp_register(chip, CHI32_CONTROL_REG) & ~0x1b00);
437
438 if (write_dsp(chip, DSP_FNC_SET_COMMPAGE_ADDR) < 0) {
439 DE_INIT(("load_dsp: Failed to write DSP_FNC_SET_COMMPAGE_ADDR\n"));
440 return -EIO;
441 }
442
443 if (write_dsp(chip, chip->comm_page_phys) < 0) {
444 DE_INIT(("load_dsp: Failed to write comm page address\n"));
445 return -EIO;
446 }
447
448
449
450
451
452 if (read_sn(chip) < 0) {
453 DE_INIT(("load_dsp: Failed to read serial number\n"));
454 return -EIO;
455 }
456
457 chip->dsp_code = code;
458 chip->bad_board = FALSE;
459 DE_INIT(("load_dsp: OK!\n"));
460 return 0;
461 }
462 udelay(100);
463 }
464
465 DE_INIT(("load_dsp: DSP load timed out waiting for HF4\n"));
466 return -EIO;
467}
468
469
470
471
472static int load_firmware(struct echoaudio *chip)
473{
474 const struct firmware *fw;
475 int box_type, err;
476
477 if (snd_BUG_ON(!chip->dsp_code_to_load || !chip->comm_page))
478 return -EPERM;
479
480
481 if (chip->dsp_code) {
482 if ((box_type = check_asic_status(chip)) >= 0)
483 return box_type;
484
485 chip->dsp_code = NULL;
486 }
487
488 if ((err = get_firmware(&fw, chip->dsp_code_to_load, chip)) < 0)
489 return err;
490 err = load_dsp(chip, (u16 *)fw->data);
491 free_firmware(fw);
492 if (err < 0)
493 return err;
494
495 if ((box_type = load_asic(chip)) < 0)
496 return box_type;
497
498 if ((err = restore_dsp_rettings(chip)) < 0)
499 return err;
500
501 return box_type;
502}
503
504
505
506
507
508
509
510#if defined(ECHOCARD_HAS_INPUT_NOMINAL_LEVEL) || \
511 defined(ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL)
512
513
514static int set_nominal_level(struct echoaudio *chip, u16 index, char consumer)
515{
516 if (snd_BUG_ON(index >= num_busses_out(chip) + num_busses_in(chip)))
517 return -EINVAL;
518
519
520 if (wait_handshake(chip))
521 return -EIO;
522
523 chip->nominal_level[index] = consumer;
524
525 if (consumer)
526 chip->comm_page->nominal_level_mask |= cpu_to_le32(1 << index);
527 else
528 chip->comm_page->nominal_level_mask &= ~cpu_to_le32(1 << index);
529
530 return 0;
531}
532
533#endif
534
535
536
537
538static int set_output_gain(struct echoaudio *chip, u16 channel, s8 gain)
539{
540 if (snd_BUG_ON(channel >= num_busses_out(chip)))
541 return -EINVAL;
542
543 if (wait_handshake(chip))
544 return -EIO;
545
546
547 chip->output_gain[channel] = gain;
548 chip->comm_page->line_out_level[channel] = gain;
549 return 0;
550}
551
552
553
554#ifdef ECHOCARD_HAS_MONITOR
555
556static int set_monitor_gain(struct echoaudio *chip, u16 output, u16 input,
557 s8 gain)
558{
559 if (snd_BUG_ON(output >= num_busses_out(chip) ||
560 input >= num_busses_in(chip)))
561 return -EINVAL;
562
563 if (wait_handshake(chip))
564 return -EIO;
565
566 chip->monitor_gain[output][input] = gain;
567 chip->comm_page->monitors[monitor_index(chip, output, input)] = gain;
568 return 0;
569}
570#endif
571
572
573
574static int update_output_line_level(struct echoaudio *chip)
575{
576 if (wait_handshake(chip))
577 return -EIO;
578 clear_handshake(chip);
579 return send_vector(chip, DSP_VC_UPDATE_OUTVOL);
580}
581
582
583
584
585static int update_input_line_level(struct echoaudio *chip)
586{
587 if (wait_handshake(chip))
588 return -EIO;
589 clear_handshake(chip);
590 return send_vector(chip, DSP_VC_UPDATE_INGAIN);
591}
592
593
594
595
596
597static void set_meters_on(struct echoaudio *chip, char on)
598{
599 if (on && !chip->meters_enabled) {
600 send_vector(chip, DSP_VC_METERS_ON);
601 chip->meters_enabled = 1;
602 } else if (!on && chip->meters_enabled) {
603 send_vector(chip, DSP_VC_METERS_OFF);
604 chip->meters_enabled = 0;
605 memset((s8 *)chip->comm_page->vu_meter, ECHOGAIN_MUTED,
606 DSP_MAXPIPES);
607 memset((s8 *)chip->comm_page->peak_meter, ECHOGAIN_MUTED,
608 DSP_MAXPIPES);
609 }
610}
611
612
613
614
615
616
617
618
619
620
621
622static void get_audio_meters(struct echoaudio *chip, long *meters)
623{
624 int i, m, n;
625
626 m = 0;
627 n = 0;
628 for (i = 0; i < num_busses_out(chip); i++, m++) {
629 meters[n++] = chip->comm_page->vu_meter[m];
630 meters[n++] = chip->comm_page->peak_meter[m];
631 }
632 for (; n < 32; n++)
633 meters[n] = 0;
634
635#ifdef ECHOCARD_ECHO3G
636 m = E3G_MAX_OUTPUTS;
637#endif
638
639 for (i = 0; i < num_busses_in(chip); i++, m++) {
640 meters[n++] = chip->comm_page->vu_meter[m];
641 meters[n++] = chip->comm_page->peak_meter[m];
642 }
643 for (; n < 64; n++)
644 meters[n] = 0;
645
646#ifdef ECHOCARD_HAS_VMIXER
647 for (i = 0; i < num_pipes_out(chip); i++, m++) {
648 meters[n++] = chip->comm_page->vu_meter[m];
649 meters[n++] = chip->comm_page->peak_meter[m];
650 }
651#endif
652 for (; n < 96; n++)
653 meters[n] = 0;
654}
655
656
657
658static int restore_dsp_rettings(struct echoaudio *chip)
659{
660 int err;
661 DE_INIT(("restore_dsp_settings\n"));
662
663 if ((err = check_asic_status(chip)) < 0)
664 return err;
665
666
667 chip->comm_page->gd_clock_state = GD_CLOCK_UNDEF;
668 chip->comm_page->gd_spdif_status = GD_SPDIF_STATUS_UNDEF;
669 chip->comm_page->handshake = 0xffffffff;
670
671 if ((err = set_sample_rate(chip, chip->sample_rate)) < 0)
672 return err;
673
674 if (chip->meters_enabled)
675 if (send_vector(chip, DSP_VC_METERS_ON) < 0)
676 return -EIO;
677
678#ifdef ECHOCARD_HAS_EXTERNAL_CLOCK
679 if (set_input_clock(chip, chip->input_clock) < 0)
680 return -EIO;
681#endif
682
683#ifdef ECHOCARD_HAS_OUTPUT_CLOCK_SWITCH
684 if (set_output_clock(chip, chip->output_clock) < 0)
685 return -EIO;
686#endif
687
688 if (update_output_line_level(chip) < 0)
689 return -EIO;
690
691 if (update_input_line_level(chip) < 0)
692 return -EIO;
693
694#ifdef ECHOCARD_HAS_VMIXER
695 if (update_vmixer_level(chip) < 0)
696 return -EIO;
697#endif
698
699 if (wait_handshake(chip) < 0)
700 return -EIO;
701 clear_handshake(chip);
702
703 DE_INIT(("restore_dsp_rettings done\n"));
704 return send_vector(chip, DSP_VC_UPDATE_FLAGS);
705}
706
707
708
709
710
711
712
713
714
715
716static void set_audio_format(struct echoaudio *chip, u16 pipe_index,
717 const struct audioformat *format)
718{
719 u16 dsp_format;
720
721 dsp_format = DSP_AUDIOFORM_SS_16LE;
722
723
724 if (format->interleave > 2) {
725 switch (format->bits_per_sample) {
726 case 16:
727 dsp_format = DSP_AUDIOFORM_SUPER_INTERLEAVE_16LE;
728 break;
729 case 24:
730 dsp_format = DSP_AUDIOFORM_SUPER_INTERLEAVE_24LE;
731 break;
732 case 32:
733 dsp_format = DSP_AUDIOFORM_SUPER_INTERLEAVE_32LE;
734 break;
735 }
736 dsp_format |= format->interleave;
737 } else if (format->data_are_bigendian) {
738
739 switch (format->interleave) {
740 case 1:
741 dsp_format = DSP_AUDIOFORM_MM_32BE;
742 break;
743#ifdef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
744 case 2:
745 dsp_format = DSP_AUDIOFORM_SS_32BE;
746 break;
747#endif
748 }
749 } else if (format->interleave == 1 &&
750 format->bits_per_sample == 32 && !format->mono_to_stereo) {
751
752 dsp_format = DSP_AUDIOFORM_MM_32LE;
753 } else {
754
755 switch (format->bits_per_sample) {
756 case 8:
757 if (format->interleave == 2)
758 dsp_format = DSP_AUDIOFORM_SS_8;
759 else
760 dsp_format = DSP_AUDIOFORM_MS_8;
761 break;
762 default:
763 case 16:
764 if (format->interleave == 2)
765 dsp_format = DSP_AUDIOFORM_SS_16LE;
766 else
767 dsp_format = DSP_AUDIOFORM_MS_16LE;
768 break;
769 case 24:
770 if (format->interleave == 2)
771 dsp_format = DSP_AUDIOFORM_SS_24LE;
772 else
773 dsp_format = DSP_AUDIOFORM_MS_24LE;
774 break;
775 case 32:
776 if (format->interleave == 2)
777 dsp_format = DSP_AUDIOFORM_SS_32LE;
778 else
779 dsp_format = DSP_AUDIOFORM_MS_32LE;
780 break;
781 }
782 }
783 DE_ACT(("set_audio_format[%d] = %x\n", pipe_index, dsp_format));
784 chip->comm_page->audio_format[pipe_index] = cpu_to_le16(dsp_format);
785}
786
787
788
789
790
791
792
793static int start_transport(struct echoaudio *chip, u32 channel_mask,
794 u32 cyclic_mask)
795{
796 DE_ACT(("start_transport %x\n", channel_mask));
797
798 if (wait_handshake(chip))
799 return -EIO;
800
801 chip->comm_page->cmd_start |= cpu_to_le32(channel_mask);
802
803 if (chip->comm_page->cmd_start) {
804 clear_handshake(chip);
805 send_vector(chip, DSP_VC_START_TRANSFER);
806 if (wait_handshake(chip))
807 return -EIO;
808
809 chip->active_mask |= channel_mask;
810 chip->comm_page->cmd_start = 0;
811 return 0;
812 }
813
814 DE_ACT(("start_transport: No pipes to start!\n"));
815 return -EINVAL;
816}
817
818
819
820static int pause_transport(struct echoaudio *chip, u32 channel_mask)
821{
822 DE_ACT(("pause_transport %x\n", channel_mask));
823
824 if (wait_handshake(chip))
825 return -EIO;
826
827 chip->comm_page->cmd_stop |= cpu_to_le32(channel_mask);
828 chip->comm_page->cmd_reset = 0;
829 if (chip->comm_page->cmd_stop) {
830 clear_handshake(chip);
831 send_vector(chip, DSP_VC_STOP_TRANSFER);
832 if (wait_handshake(chip))
833 return -EIO;
834
835 chip->active_mask &= ~channel_mask;
836 chip->comm_page->cmd_stop = 0;
837 chip->comm_page->cmd_reset = 0;
838 return 0;
839 }
840
841 DE_ACT(("pause_transport: No pipes to stop!\n"));
842 return 0;
843}
844
845
846
847static int stop_transport(struct echoaudio *chip, u32 channel_mask)
848{
849 DE_ACT(("stop_transport %x\n", channel_mask));
850
851 if (wait_handshake(chip))
852 return -EIO;
853
854 chip->comm_page->cmd_stop |= cpu_to_le32(channel_mask);
855 chip->comm_page->cmd_reset |= cpu_to_le32(channel_mask);
856 if (chip->comm_page->cmd_reset) {
857 clear_handshake(chip);
858 send_vector(chip, DSP_VC_STOP_TRANSFER);
859 if (wait_handshake(chip))
860 return -EIO;
861
862 chip->active_mask &= ~channel_mask;
863 chip->comm_page->cmd_stop = 0;
864 chip->comm_page->cmd_reset = 0;
865 return 0;
866 }
867
868 DE_ACT(("stop_transport: No pipes to stop!\n"));
869 return 0;
870}
871
872
873
874static inline int is_pipe_allocated(struct echoaudio *chip, u16 pipe_index)
875{
876 return (chip->pipe_alloc_mask & (1 << pipe_index));
877}
878
879
880
881
882
883static int rest_in_peace(struct echoaudio *chip)
884{
885 DE_ACT(("rest_in_peace() open=%x\n", chip->pipe_alloc_mask));
886
887
888 stop_transport(chip, chip->active_mask);
889
890 set_meters_on(chip, FALSE);
891
892#ifdef ECHOCARD_HAS_MIDI
893 enable_midi_input(chip, FALSE);
894#endif
895
896
897 if (chip->dsp_code) {
898
899 chip->dsp_code = NULL;
900
901 return send_vector(chip, DSP_VC_GO_COMATOSE);
902 }
903 return 0;
904}
905
906
907
908
909static int init_dsp_comm_page(struct echoaudio *chip)
910{
911
912 if (offsetof(struct comm_page, midi_output) != 0xbe0) {
913 DE_INIT(("init_dsp_comm_page() - Invalid struct comm_page structure\n"));
914 return -EPERM;
915 }
916
917
918 chip->card_name = ECHOCARD_NAME;
919 chip->bad_board = TRUE;
920 chip->dsp_code = NULL;
921 chip->digital_mode = DIGITAL_MODE_NONE;
922 chip->input_clock = ECHO_CLOCK_INTERNAL;
923 chip->output_clock = ECHO_CLOCK_WORD;
924 chip->asic_loaded = FALSE;
925 memset(chip->comm_page, 0, sizeof(struct comm_page));
926
927
928 chip->comm_page->comm_size =
929 __constant_cpu_to_le32(sizeof(struct comm_page));
930 chip->comm_page->handshake = 0xffffffff;
931 chip->comm_page->midi_out_free_count =
932 __constant_cpu_to_le32(DSP_MIDI_OUT_FIFO_SIZE);
933 chip->comm_page->sample_rate = __constant_cpu_to_le32(44100);
934 chip->sample_rate = 44100;
935
936
937 memset(chip->comm_page->monitors, ECHOGAIN_MUTED, MONITOR_ARRAY_SIZE);
938 memset(chip->comm_page->vmixer, ECHOGAIN_MUTED, VMIXER_ARRAY_SIZE);
939
940 return 0;
941}
942
943
944
945
946
947static int init_line_levels(struct echoaudio *chip)
948{
949 int st, i, o;
950
951 DE_INIT(("init_line_levels\n"));
952
953
954 for (i = 0; i < num_busses_out(chip); i++)
955 if ((st = set_output_gain(chip, i, ECHOGAIN_MUTED)))
956 return st;
957 if ((st = update_output_line_level(chip)))
958 return st;
959
960#ifdef ECHOCARD_HAS_VMIXER
961
962 for (i = 0; i < num_pipes_out(chip); i++)
963 for (o = 0; o < num_busses_out(chip); o++)
964 if ((st = set_vmixer_gain(chip, o, i, ECHOGAIN_MUTED)))
965 return st;
966 if ((st = update_vmixer_level(chip)))
967 return st;
968#endif
969
970#ifdef ECHOCARD_HAS_MONITOR
971
972 for (o = 0; o < num_busses_out(chip); o++)
973 for (i = 0; i < num_busses_in(chip); i++)
974 if ((st = set_monitor_gain(chip, o, i, ECHOGAIN_MUTED)))
975 return st;
976 if ((st = update_output_line_level(chip)))
977 return st;
978#endif
979
980#ifdef ECHOCARD_HAS_INPUT_GAIN
981 for (i = 0; i < num_busses_in(chip); i++)
982 if ((st = set_input_gain(chip, i, ECHOGAIN_MUTED)))
983 return st;
984 if ((st = update_input_line_level(chip)))
985 return st;
986#endif
987
988 return 0;
989}
990
991
992
993
994
995
996static int service_irq(struct echoaudio *chip)
997{
998 int st;
999
1000
1001 if (get_dsp_register(chip, CHI32_STATUS_REG) & CHI32_STATUS_IRQ) {
1002 st = 0;
1003#ifdef ECHOCARD_HAS_MIDI
1004
1005 if (chip->comm_page->midi_input[0])
1006 st = midi_service_irq(chip);
1007#endif
1008
1009 chip->comm_page->midi_input[0] = 0;
1010 send_vector(chip, DSP_VC_ACK_INT);
1011 return st;
1012 }
1013 return -1;
1014}
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025static int allocate_pipes(struct echoaudio *chip, struct audiopipe *pipe,
1026 int pipe_index, int interleave)
1027{
1028 int i;
1029 u32 channel_mask;
1030 char is_cyclic;
1031
1032 DE_ACT(("allocate_pipes: ch=%d int=%d\n", pipe_index, interleave));
1033
1034 if (chip->bad_board)
1035 return -EIO;
1036
1037 is_cyclic = 1;
1038
1039 for (channel_mask = i = 0; i < interleave; i++)
1040 channel_mask |= 1 << (pipe_index + i);
1041 if (chip->pipe_alloc_mask & channel_mask) {
1042 DE_ACT(("allocate_pipes: channel already open\n"));
1043 return -EAGAIN;
1044 }
1045
1046 chip->comm_page->position[pipe_index] = 0;
1047 chip->pipe_alloc_mask |= channel_mask;
1048 if (is_cyclic)
1049 chip->pipe_cyclic_mask |= channel_mask;
1050 pipe->index = pipe_index;
1051 pipe->interleave = interleave;
1052 pipe->state = PIPE_STATE_STOPPED;
1053
1054
1055
1056
1057 pipe->dma_counter = &chip->comm_page->position[pipe_index];
1058 *pipe->dma_counter = 0;
1059 DE_ACT(("allocate_pipes: ok\n"));
1060 return pipe_index;
1061}
1062
1063
1064
1065static int free_pipes(struct echoaudio *chip, struct audiopipe *pipe)
1066{
1067 u32 channel_mask;
1068 int i;
1069
1070 DE_ACT(("free_pipes: Pipe %d\n", pipe->index));
1071 if (snd_BUG_ON(!is_pipe_allocated(chip, pipe->index)))
1072 return -EINVAL;
1073 if (snd_BUG_ON(pipe->state != PIPE_STATE_STOPPED))
1074 return -EINVAL;
1075
1076 for (channel_mask = i = 0; i < pipe->interleave; i++)
1077 channel_mask |= 1 << (pipe->index + i);
1078
1079 chip->pipe_alloc_mask &= ~channel_mask;
1080 chip->pipe_cyclic_mask &= ~channel_mask;
1081 return 0;
1082}
1083
1084
1085
1086
1087
1088
1089
1090static int sglist_init(struct echoaudio *chip, struct audiopipe *pipe)
1091{
1092 pipe->sglist_head = 0;
1093 memset(pipe->sgpage.area, 0, PAGE_SIZE);
1094 chip->comm_page->sglist_addr[pipe->index].addr =
1095 cpu_to_le32(pipe->sgpage.addr);
1096 return 0;
1097}
1098
1099
1100
1101static int sglist_add_mapping(struct echoaudio *chip, struct audiopipe *pipe,
1102 dma_addr_t address, size_t length)
1103{
1104 int head = pipe->sglist_head;
1105 struct sg_entry *list = (struct sg_entry *)pipe->sgpage.area;
1106
1107 if (head < MAX_SGLIST_ENTRIES - 1) {
1108 list[head].addr = cpu_to_le32(address);
1109 list[head].size = cpu_to_le32(length);
1110 pipe->sglist_head++;
1111 } else {
1112 DE_ACT(("SGlist: too many fragments\n"));
1113 return -ENOMEM;
1114 }
1115 return 0;
1116}
1117
1118
1119
1120static inline int sglist_add_irq(struct echoaudio *chip, struct audiopipe *pipe)
1121{
1122 return sglist_add_mapping(chip, pipe, 0, 0);
1123}
1124
1125
1126
1127static inline int sglist_wrap(struct echoaudio *chip, struct audiopipe *pipe)
1128{
1129 return sglist_add_mapping(chip, pipe, pipe->sgpage.addr, 0);
1130}
1131