1 /****************************************************************************
3 Copyright Echo Digital Audio Corporation (c) 1998 - 2004
7 This file is part of Echo Digital Audio's generic driver library.
9 Echo Digital Audio's generic driver library is free software;
10 you can redistribute it and/or modify it under the terms of
11 the GNU General Public License as published by the Free Software
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston,
24 *************************************************************************
26 Translation from C++ and adaptation for use in ALSA-Driver
27 were made by Giuliano Pochini <pochini@shiny.it>
29 ****************************************************************************/
32 #error PAGE_SIZE is < 4k
35 static int restore_dsp_rettings(struct echoaudio
*chip
);
38 /* Some vector commands involve the DSP reading or writing data to and from the
39 comm page; if you send one of these commands to the DSP, it will complete the
40 command and then write a non-zero value to the Handshake field in the
41 comm page. This function waits for the handshake to show up. */
42 static int wait_handshake(struct echoaudio
*chip
)
46 /* Wait up to 20ms for the handshake from the DSP */
47 for (i
= 0; i
< HANDSHAKE_TIMEOUT
; i
++) {
48 /* Look for the handshake value */
50 if (chip
->comm_page
->handshake
) {
56 snd_printk(KERN_ERR
"wait_handshake(): Timeout waiting for DSP\n");
62 /* Much of the interaction between the DSP and the driver is done via vector
63 commands; send_vector writes a vector command to the DSP. Typically, this
64 causes the DSP to read or write fields in the comm page.
65 PCI posting is not required thanks to the handshake logic. */
66 static int send_vector(struct echoaudio
*chip
, u32 command
)
70 wmb(); /* Flush all pending writes before sending the command */
72 /* Wait up to 100ms for the "vector busy" bit to be off */
73 for (i
= 0; i
< VECTOR_BUSY_TIMEOUT
; i
++) {
74 if (!(get_dsp_register(chip
, CHI32_VECTOR_REG
) &
76 set_dsp_register(chip
, CHI32_VECTOR_REG
, command
);
77 /*if (i) DE_ACT(("send_vector time: %d\n", i));*/
83 DE_ACT((KERN_ERR
"timeout on send_vector\n"));
89 /* write_dsp writes a 32-bit value to the DSP; this is used almost
90 exclusively for loading the DSP. */
91 static int write_dsp(struct echoaudio
*chip
, u32 data
)
95 for (i
= 0; i
< 10000000; i
++) { /* timeout = 10s */
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(); /* write it immediately */
106 chip
->bad_board
= TRUE
; /* Set TRUE until DSP re-loaded */
107 DE_ACT((KERN_ERR
"write_dsp: Set bad_board to TRUE\n"));
113 /* read_dsp reads a 32-bit value from the DSP; this is used almost
114 exclusively for loading the DSP and checking the status of the ASIC. */
115 static int read_dsp(struct echoaudio
*chip
, u32
*data
)
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
);
129 chip
->bad_board
= TRUE
; /* Set TRUE until DSP re-loaded */
130 DE_INIT((KERN_ERR
"read_dsp: Set bad_board to TRUE\n"));
136 /****************************************************************************
137 Firmware loading functions
138 ****************************************************************************/
140 /* This function is used to read back the serial number from the DSP;
141 this is triggered by the SET_COMMPAGE_ADDR command.
142 Only some early Echogals products have serial numbers in the ROM;
143 the serial number is not used, but you still need to do this as
144 part of the DSP load process. */
145 static int read_sn(struct echoaudio
*chip
)
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");
156 DE_INIT(("Read serial number %08x %08x %08x %08x %08x\n",
157 sn
[0], sn
[1], sn
[2], sn
[3], sn
[4]));
163 #ifndef ECHOCARD_HAS_ASIC
164 /* This card has no ASIC, just return ok */
165 static inline int check_asic_status(struct echoaudio
*chip
)
167 chip
->asic_loaded
= TRUE
;
171 #endif /* !ECHOCARD_HAS_ASIC */
175 #ifdef ECHOCARD_HAS_ASIC
177 /* Load ASIC code - done after the DSP is loaded */
178 static int load_asic_generic(struct echoaudio
*chip
, u32 cmd
, short asic
)
180 const struct firmware
*fw
;
185 err
= get_firmware(&fw
, chip
, asic
);
187 snd_printk(KERN_WARNING
"Firmware not found !\n");
191 code
= (u8
*)fw
->data
;
194 /* Send the "Here comes the ASIC" command */
195 if (write_dsp(chip
, cmd
) < 0)
198 /* Write length of ASIC file in bytes */
199 if (write_dsp(chip
, size
) < 0)
202 for (i
= 0; i
< size
; i
++) {
203 if (write_dsp(chip
, code
[i
]) < 0)
207 DE_INIT(("ASIC loaded\n"));
212 DE_INIT(("failed on write_dsp\n"));
217 #endif /* ECHOCARD_HAS_ASIC */
223 /* Install the resident loader for 56361 DSPs; The resident loader is on
224 the EPROM on the board for 56301 DSP. The resident loader is a tiny little
225 program that is used to load the real DSP code. */
226 static int install_resident_loader(struct echoaudio
*chip
)
232 const struct firmware
*fw
;
234 /* 56361 cards only! This check is required by the old 56301-based
236 if (chip
->device_id
!= DEVICE_ID_56361
)
239 /* Look to see if the resident loader is present. If the resident
240 loader is already installed, host flag 5 will be on. */
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",
248 i
= get_firmware(&fw
, chip
, FW_361_LOADER
);
250 snd_printk(KERN_WARNING
"Firmware not found !\n");
254 /* The DSP code is an array of 16 bit words. The array is divided up
255 into sections. The first word of each section is the size in words,
256 followed by the section type.
257 Since DSP addresses and data are 24 bits wide, they each take up two
258 16 bit words in the array.
259 This is a lot like the other loader loop, but it's not a loop, you
260 don't write the memory type, and you don't write a zero at the end. */
262 /* Set DSP format bits for 24 bit mode */
263 set_dsp_register(chip
, CHI32_CONTROL_REG
,
264 get_dsp_register(chip
, CHI32_CONTROL_REG
) | 0x900);
266 code
= (u16
*)fw
->data
;
268 /* Skip the header section; the first word in the array is the size
269 of the first section, so the first real section of code is pointed
273 /* Skip the section size, LRS block type, and DSP memory type */
276 /* Get the number of DSP words to write */
277 words
= code
[index
++];
279 /* Get the DSP address for this block; 24 bits, so build from two words */
280 address
= ((u32
)code
[index
] << 16) + code
[index
+ 1];
283 /* Write the count to the DSP */
284 if (write_dsp(chip
, words
)) {
285 DE_INIT(("install_resident_loader: Failed to write word count!\n"));
288 /* Write the DSP address */
289 if (write_dsp(chip
, address
)) {
290 DE_INIT(("install_resident_loader: Failed to write DSP address!\n"));
293 /* Write out this block of code to the DSP */
294 for (i
= 0; i
< words
; i
++) {
297 data
= ((u32
)code
[index
] << 16) + code
[index
+ 1];
298 if (write_dsp(chip
, data
)) {
299 DE_INIT(("install_resident_loader: Failed to write DSP code\n"));
305 /* Wait for flag 5 to come up */
306 for (i
= 0; i
< 200; i
++) { /* Timeout is 50us * 200 = 10ms */
308 status
= get_dsp_register(chip
, CHI32_STATUS_REG
);
309 if (status
& CHI32_STATUS_REG_HF5
)
314 DE_INIT(("Resident loader failed to set HF5\n"));
318 DE_INIT(("Resident loader successfully installed\n"));
327 #endif /* DSP_56361 */
330 static int load_dsp(struct echoaudio
*chip
, u16
*code
)
335 if (chip
->dsp_code
== code
) {
336 DE_INIT(("DSP is already loaded!\n"));
339 chip
->bad_board
= TRUE
; /* Set TRUE until DSP loaded */
340 chip
->dsp_code
= NULL
; /* Current DSP code not loaded */
341 chip
->asic_loaded
= FALSE
; /* Loading the DSP code will reset the ASIC */
343 DE_INIT(("load_dsp: Set bad_board to TRUE\n"));
345 /* If this board requires a resident loader, install it. */
347 if ((i
= install_resident_loader(chip
)) < 0)
351 /* Send software reset command */
352 if (send_vector(chip
, DSP_VC_RESET
) < 0) {
353 DE_INIT(("LoadDsp: send_vector DSP_VC_RESET failed, Critical Failure\n"));
359 /* Wait 10ms for HF3 to indicate that software reset is complete */
360 for (i
= 0; i
< 1000; i
++) { /* Timeout is 10us * 1000 = 10ms */
361 if (get_dsp_register(chip
, CHI32_STATUS_REG
) &
362 CHI32_STATUS_REG_HF3
)
368 DE_INIT(("load_dsp: Timeout waiting for CHI32_STATUS_REG_HF3\n"));
372 /* Set DSP format bits for 24 bit mode now that soft reset is done */
373 set_dsp_register(chip
, CHI32_CONTROL_REG
,
374 get_dsp_register(chip
, CHI32_CONTROL_REG
) | 0x900);
376 /* Main loader loop */
380 int block_type
, mem_type
;
382 /* Total Block Size */
386 block_type
= code
[index
];
387 if (block_type
== 4) /* We're finished */
392 /* Memory Type P=0,X=1,Y=2 */
393 mem_type
= code
[index
++];
395 /* Block Code Size */
396 words
= code
[index
++];
397 if (words
== 0) /* We're finished */
401 address
= ((u32
)code
[index
] << 16) + code
[index
+ 1];
404 if (write_dsp(chip
, words
) < 0) {
405 DE_INIT(("load_dsp: failed to write number of DSP words\n"));
408 if (write_dsp(chip
, address
) < 0) {
409 DE_INIT(("load_dsp: failed to write DSP address\n"));
412 if (write_dsp(chip
, mem_type
) < 0) {
413 DE_INIT(("load_dsp: failed to write DSP memory type\n"));
417 for (i
= 0; i
< words
; i
++, index
+=2) {
418 data
= ((u32
)code
[index
] << 16) + code
[index
+ 1];
419 if (write_dsp(chip
, data
) < 0) {
420 DE_INIT(("load_dsp: failed to write DSP data\n"));
426 if (write_dsp(chip
, 0) < 0) { /* We're done!!! */
427 DE_INIT(("load_dsp: Failed to write final zero\n"));
432 for (i
= 0; i
< 5000; i
++) { /* Timeout is 100us * 5000 = 500ms */
433 /* Wait for flag 4 - indicates that the DSP loaded OK */
434 if (get_dsp_register(chip
, CHI32_STATUS_REG
) &
435 CHI32_STATUS_REG_HF4
) {
436 set_dsp_register(chip
, CHI32_CONTROL_REG
,
437 get_dsp_register(chip
, CHI32_CONTROL_REG
) & ~0x1b00);
439 if (write_dsp(chip
, DSP_FNC_SET_COMMPAGE_ADDR
) < 0) {
440 DE_INIT(("load_dsp: Failed to write DSP_FNC_SET_COMMPAGE_ADDR\n"));
444 if (write_dsp(chip
, chip
->comm_page_phys
) < 0) {
445 DE_INIT(("load_dsp: Failed to write comm page address\n"));
449 /* Get the serial number via slave mode.
450 This is triggered by the SET_COMMPAGE_ADDR command.
451 We don't actually use the serial number but we have to
452 get it as part of the DSP init voodoo. */
453 if (read_sn(chip
) < 0) {
454 DE_INIT(("load_dsp: Failed to read serial number\n"));
458 chip
->dsp_code
= code
; /* Show which DSP code loaded */
459 chip
->bad_board
= FALSE
; /* DSP OK */
460 DE_INIT(("load_dsp: OK!\n"));
466 DE_INIT(("load_dsp: DSP load timed out waiting for HF4\n"));
472 /* load_firmware takes care of loading the DSP and any ASIC code. */
473 static int load_firmware(struct echoaudio
*chip
)
475 const struct firmware
*fw
;
478 if (snd_BUG_ON(!chip
->dsp_code_to_load
|| !chip
->comm_page
))
481 /* See if the ASIC is present and working - only if the DSP is already loaded */
482 if (chip
->dsp_code
) {
483 if ((box_type
= check_asic_status(chip
)) >= 0)
485 /* ASIC check failed; force the DSP to reload */
486 chip
->dsp_code
= NULL
;
489 err
= get_firmware(&fw
, chip
, chip
->dsp_code_to_load
);
492 err
= load_dsp(chip
, (u16
*)fw
->data
);
497 if ((box_type
= load_asic(chip
)) < 0)
498 return box_type
; /* error */
505 /****************************************************************************
507 ****************************************************************************/
509 #if defined(ECHOCARD_HAS_INPUT_NOMINAL_LEVEL) || \
510 defined(ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL)
512 /* Set the nominal level for an input or output bus (true = -10dBV, false = +4dBu) */
513 static int set_nominal_level(struct echoaudio
*chip
, u16 index
, char consumer
)
515 if (snd_BUG_ON(index
>= num_busses_out(chip
) + num_busses_in(chip
)))
518 /* Wait for the handshake (OK even if ASIC is not loaded) */
519 if (wait_handshake(chip
))
522 chip
->nominal_level
[index
] = consumer
;
525 chip
->comm_page
->nominal_level_mask
|= cpu_to_le32(1 << index
);
527 chip
->comm_page
->nominal_level_mask
&= ~cpu_to_le32(1 << index
);
532 #endif /* ECHOCARD_HAS_*_NOMINAL_LEVEL */
536 /* Set the gain for a single physical output channel (dB). */
537 static int set_output_gain(struct echoaudio
*chip
, u16 channel
, s8 gain
)
539 if (snd_BUG_ON(channel
>= num_busses_out(chip
)))
542 if (wait_handshake(chip
))
545 /* Save the new value */
546 chip
->output_gain
[channel
] = gain
;
547 chip
->comm_page
->line_out_level
[channel
] = gain
;
553 #ifdef ECHOCARD_HAS_MONITOR
554 /* Set the monitor level from an input bus to an output bus. */
555 static int set_monitor_gain(struct echoaudio
*chip
, u16 output
, u16 input
,
558 if (snd_BUG_ON(output
>= num_busses_out(chip
) ||
559 input
>= num_busses_in(chip
)))
562 if (wait_handshake(chip
))
565 chip
->monitor_gain
[output
][input
] = gain
;
566 chip
->comm_page
->monitors
[monitor_index(chip
, output
, input
)] = gain
;
569 #endif /* ECHOCARD_HAS_MONITOR */
572 /* Tell the DSP to read and update output, nominal & monitor levels in comm page. */
573 static int update_output_line_level(struct echoaudio
*chip
)
575 if (wait_handshake(chip
))
577 clear_handshake(chip
);
578 return send_vector(chip
, DSP_VC_UPDATE_OUTVOL
);
583 /* Tell the DSP to read and update input levels in comm page */
584 static int update_input_line_level(struct echoaudio
*chip
)
586 if (wait_handshake(chip
))
588 clear_handshake(chip
);
589 return send_vector(chip
, DSP_VC_UPDATE_INGAIN
);
594 /* set_meters_on turns the meters on or off. If meters are turned on, the DSP
595 will write the meter and clock detect values to the comm page at about 30Hz */
596 static void set_meters_on(struct echoaudio
*chip
, char on
)
598 if (on
&& !chip
->meters_enabled
) {
599 send_vector(chip
, DSP_VC_METERS_ON
);
600 chip
->meters_enabled
= 1;
601 } else if (!on
&& chip
->meters_enabled
) {
602 send_vector(chip
, DSP_VC_METERS_OFF
);
603 chip
->meters_enabled
= 0;
604 memset((s8
*)chip
->comm_page
->vu_meter
, ECHOGAIN_MUTED
,
606 memset((s8
*)chip
->comm_page
->peak_meter
, ECHOGAIN_MUTED
,
613 /* Fill out an the given array using the current values in the comm page.
614 Meters are written in the comm page by the DSP in this order:
617 Output pipes (vmixer cards only)
619 This function assumes there are no more than 16 in/out busses or pipes
620 Meters is an array [3][16][2] of long. */
621 static void get_audio_meters(struct echoaudio
*chip
, long *meters
)
627 for (i
= 0; i
< num_busses_out(chip
); i
++, m
++) {
628 meters
[n
++] = chip
->comm_page
->vu_meter
[m
];
629 meters
[n
++] = chip
->comm_page
->peak_meter
[m
];
634 #ifdef ECHOCARD_ECHO3G
635 m
= E3G_MAX_OUTPUTS
; /* Skip unused meters */
638 for (i
= 0; i
< num_busses_in(chip
); i
++, m
++) {
639 meters
[n
++] = chip
->comm_page
->vu_meter
[m
];
640 meters
[n
++] = chip
->comm_page
->peak_meter
[m
];
645 #ifdef ECHOCARD_HAS_VMIXER
646 for (i
= 0; i
< num_pipes_out(chip
); i
++, m
++) {
647 meters
[n
++] = chip
->comm_page
->vu_meter
[m
];
648 meters
[n
++] = chip
->comm_page
->peak_meter
[m
];
657 static int restore_dsp_rettings(struct echoaudio
*chip
)
660 DE_INIT(("restore_dsp_settings\n"));
662 if ((err
= check_asic_status(chip
)) < 0)
665 /* Gina20/Darla20 only. Should be harmless for other cards. */
666 chip
->comm_page
->gd_clock_state
= GD_CLOCK_UNDEF
;
667 chip
->comm_page
->gd_spdif_status
= GD_SPDIF_STATUS_UNDEF
;
668 chip
->comm_page
->handshake
= 0xffffffff;
670 /* Restore output busses */
671 for (i
= 0; i
< num_busses_out(chip
); i
++) {
672 err
= set_output_gain(chip
, i
, chip
->output_gain
[i
]);
677 #ifdef ECHOCARD_HAS_VMIXER
678 for (i
= 0; i
< num_pipes_out(chip
); i
++)
679 for (o
= 0; o
< num_busses_out(chip
); o
++) {
680 err
= set_vmixer_gain(chip
, o
, i
,
681 chip
->vmixer_gain
[o
][i
]);
685 if (update_vmixer_level(chip
) < 0)
687 #endif /* ECHOCARD_HAS_VMIXER */
689 #ifdef ECHOCARD_HAS_MONITOR
690 for (o
= 0; o
< num_busses_out(chip
); o
++)
691 for (i
= 0; i
< num_busses_in(chip
); i
++) {
692 err
= set_monitor_gain(chip
, o
, i
,
693 chip
->monitor_gain
[o
][i
]);
697 #endif /* ECHOCARD_HAS_MONITOR */
699 #ifdef ECHOCARD_HAS_INPUT_GAIN
700 for (i
= 0; i
< num_busses_in(chip
); i
++) {
701 err
= set_input_gain(chip
, i
, chip
->input_gain
[i
]);
705 #endif /* ECHOCARD_HAS_INPUT_GAIN */
707 err
= update_output_line_level(chip
);
711 err
= update_input_line_level(chip
);
715 err
= set_sample_rate(chip
, chip
->sample_rate
);
719 if (chip
->meters_enabled
) {
720 err
= send_vector(chip
, DSP_VC_METERS_ON
);
725 #ifdef ECHOCARD_HAS_DIGITAL_MODE_SWITCH
726 if (set_digital_mode(chip
, chip
->digital_mode
) < 0)
730 #ifdef ECHOCARD_HAS_DIGITAL_IO
731 if (set_professional_spdif(chip
, chip
->professional_spdif
) < 0)
735 #ifdef ECHOCARD_HAS_PHANTOM_POWER
736 if (set_phantom_power(chip
, chip
->phantom_power
) < 0)
740 #ifdef ECHOCARD_HAS_EXTERNAL_CLOCK
741 /* set_input_clock() also restores automute setting */
742 if (set_input_clock(chip
, chip
->input_clock
) < 0)
746 #ifdef ECHOCARD_HAS_OUTPUT_CLOCK_SWITCH
747 if (set_output_clock(chip
, chip
->output_clock
) < 0)
751 if (wait_handshake(chip
) < 0)
753 clear_handshake(chip
);
754 if (send_vector(chip
, DSP_VC_UPDATE_FLAGS
) < 0)
757 DE_INIT(("restore_dsp_rettings done\n"));
763 /****************************************************************************
765 ****************************************************************************/
767 /* set_audio_format() sets the format of the audio data in host memory for
768 this pipe. Note that _MS_ (mono-to-stereo) playback modes are not used by ALSA
769 but they are here because they are just mono while capturing */
770 static void set_audio_format(struct echoaudio
*chip
, u16 pipe_index
,
771 const struct audioformat
*format
)
775 dsp_format
= DSP_AUDIOFORM_SS_16LE
;
777 /* Look for super-interleave (no big-endian and 8 bits) */
778 if (format
->interleave
> 2) {
779 switch (format
->bits_per_sample
) {
781 dsp_format
= DSP_AUDIOFORM_SUPER_INTERLEAVE_16LE
;
784 dsp_format
= DSP_AUDIOFORM_SUPER_INTERLEAVE_24LE
;
787 dsp_format
= DSP_AUDIOFORM_SUPER_INTERLEAVE_32LE
;
790 dsp_format
|= format
->interleave
;
791 } else if (format
->data_are_bigendian
) {
792 /* For big-endian data, only 32 bit samples are supported */
793 switch (format
->interleave
) {
795 dsp_format
= DSP_AUDIOFORM_MM_32BE
;
797 #ifdef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
799 dsp_format
= DSP_AUDIOFORM_SS_32BE
;
803 } else if (format
->interleave
== 1 &&
804 format
->bits_per_sample
== 32 && !format
->mono_to_stereo
) {
805 /* 32 bit little-endian mono->mono case */
806 dsp_format
= DSP_AUDIOFORM_MM_32LE
;
808 /* Handle the other little-endian formats */
809 switch (format
->bits_per_sample
) {
811 if (format
->interleave
== 2)
812 dsp_format
= DSP_AUDIOFORM_SS_8
;
814 dsp_format
= DSP_AUDIOFORM_MS_8
;
818 if (format
->interleave
== 2)
819 dsp_format
= DSP_AUDIOFORM_SS_16LE
;
821 dsp_format
= DSP_AUDIOFORM_MS_16LE
;
824 if (format
->interleave
== 2)
825 dsp_format
= DSP_AUDIOFORM_SS_24LE
;
827 dsp_format
= DSP_AUDIOFORM_MS_24LE
;
830 if (format
->interleave
== 2)
831 dsp_format
= DSP_AUDIOFORM_SS_32LE
;
833 dsp_format
= DSP_AUDIOFORM_MS_32LE
;
837 DE_ACT(("set_audio_format[%d] = %x\n", pipe_index
, dsp_format
));
838 chip
->comm_page
->audio_format
[pipe_index
] = cpu_to_le16(dsp_format
);
843 /* start_transport starts transport for a set of pipes.
844 The bits 1 in channel_mask specify what pipes to start. Only the bit of the
845 first channel must be set, regardless its interleave.
846 Same thing for pause_ and stop_ -trasport below. */
847 static int start_transport(struct echoaudio
*chip
, u32 channel_mask
,
850 DE_ACT(("start_transport %x\n", channel_mask
));
852 if (wait_handshake(chip
))
855 chip
->comm_page
->cmd_start
|= cpu_to_le32(channel_mask
);
857 if (chip
->comm_page
->cmd_start
) {
858 clear_handshake(chip
);
859 send_vector(chip
, DSP_VC_START_TRANSFER
);
860 if (wait_handshake(chip
))
862 /* Keep track of which pipes are transporting */
863 chip
->active_mask
|= channel_mask
;
864 chip
->comm_page
->cmd_start
= 0;
868 DE_ACT(("start_transport: No pipes to start!\n"));
874 static int pause_transport(struct echoaudio
*chip
, u32 channel_mask
)
876 DE_ACT(("pause_transport %x\n", channel_mask
));
878 if (wait_handshake(chip
))
881 chip
->comm_page
->cmd_stop
|= cpu_to_le32(channel_mask
);
882 chip
->comm_page
->cmd_reset
= 0;
883 if (chip
->comm_page
->cmd_stop
) {
884 clear_handshake(chip
);
885 send_vector(chip
, DSP_VC_STOP_TRANSFER
);
886 if (wait_handshake(chip
))
888 /* Keep track of which pipes are transporting */
889 chip
->active_mask
&= ~channel_mask
;
890 chip
->comm_page
->cmd_stop
= 0;
891 chip
->comm_page
->cmd_reset
= 0;
895 DE_ACT(("pause_transport: No pipes to stop!\n"));
901 static int stop_transport(struct echoaudio
*chip
, u32 channel_mask
)
903 DE_ACT(("stop_transport %x\n", channel_mask
));
905 if (wait_handshake(chip
))
908 chip
->comm_page
->cmd_stop
|= cpu_to_le32(channel_mask
);
909 chip
->comm_page
->cmd_reset
|= cpu_to_le32(channel_mask
);
910 if (chip
->comm_page
->cmd_reset
) {
911 clear_handshake(chip
);
912 send_vector(chip
, DSP_VC_STOP_TRANSFER
);
913 if (wait_handshake(chip
))
915 /* Keep track of which pipes are transporting */
916 chip
->active_mask
&= ~channel_mask
;
917 chip
->comm_page
->cmd_stop
= 0;
918 chip
->comm_page
->cmd_reset
= 0;
922 DE_ACT(("stop_transport: No pipes to stop!\n"));
928 static inline int is_pipe_allocated(struct echoaudio
*chip
, u16 pipe_index
)
930 return (chip
->pipe_alloc_mask
& (1 << pipe_index
));
935 /* Stops everything and turns off the DSP. All pipes should be already
936 stopped and unallocated. */
937 static int rest_in_peace(struct echoaudio
*chip
)
939 DE_ACT(("rest_in_peace() open=%x\n", chip
->pipe_alloc_mask
));
941 /* Stops all active pipes (just to be sure) */
942 stop_transport(chip
, chip
->active_mask
);
944 set_meters_on(chip
, FALSE
);
946 #ifdef ECHOCARD_HAS_MIDI
947 enable_midi_input(chip
, FALSE
);
951 if (chip
->dsp_code
) {
952 /* Make load_firmware do a complete reload */
953 chip
->dsp_code
= NULL
;
954 /* Put the DSP to sleep */
955 return send_vector(chip
, DSP_VC_GO_COMATOSE
);
962 /* Fills the comm page with default values */
963 static int init_dsp_comm_page(struct echoaudio
*chip
)
965 /* Check if the compiler added extra padding inside the structure */
966 if (offsetof(struct comm_page
, midi_output
) != 0xbe0) {
967 DE_INIT(("init_dsp_comm_page() - Invalid struct comm_page structure\n"));
971 /* Init all the basic stuff */
972 chip
->card_name
= ECHOCARD_NAME
;
973 chip
->bad_board
= TRUE
; /* Set TRUE until DSP loaded */
974 chip
->dsp_code
= NULL
; /* Current DSP code not loaded */
975 chip
->asic_loaded
= FALSE
;
976 memset(chip
->comm_page
, 0, sizeof(struct comm_page
));
978 /* Init the comm page */
979 chip
->comm_page
->comm_size
=
980 cpu_to_le32(sizeof(struct comm_page
));
981 chip
->comm_page
->handshake
= 0xffffffff;
982 chip
->comm_page
->midi_out_free_count
=
983 cpu_to_le32(DSP_MIDI_OUT_FIFO_SIZE
);
984 chip
->comm_page
->sample_rate
= cpu_to_le32(44100);
986 /* Set line levels so we don't blast any inputs on startup */
987 memset(chip
->comm_page
->monitors
, ECHOGAIN_MUTED
, MONITOR_ARRAY_SIZE
);
988 memset(chip
->comm_page
->vmixer
, ECHOGAIN_MUTED
, VMIXER_ARRAY_SIZE
);
995 /* This function initializes the chip structure with default values, ie. all
996 * muted and internal clock source. Then it copies the settings to the DSP.
997 * This MUST be called after the DSP is up and running !
999 static int init_line_levels(struct echoaudio
*chip
)
1001 DE_INIT(("init_line_levels\n"));
1002 memset(chip
->output_gain
, ECHOGAIN_MUTED
, sizeof(chip
->output_gain
));
1003 memset(chip
->input_gain
, ECHOGAIN_MUTED
, sizeof(chip
->input_gain
));
1004 memset(chip
->monitor_gain
, ECHOGAIN_MUTED
, sizeof(chip
->monitor_gain
));
1005 memset(chip
->vmixer_gain
, ECHOGAIN_MUTED
, sizeof(chip
->vmixer_gain
));
1006 chip
->input_clock
= ECHO_CLOCK_INTERNAL
;
1007 chip
->output_clock
= ECHO_CLOCK_WORD
;
1008 chip
->sample_rate
= 44100;
1009 return restore_dsp_rettings(chip
);
1014 /* This is low level part of the interrupt handler.
1015 It returns -1 if the IRQ is not ours, or N>=0 if it is, where N is the number
1016 of midi data in the input queue. */
1017 static int service_irq(struct echoaudio
*chip
)
1021 /* Read the DSP status register and see if this DSP generated this interrupt */
1022 if (get_dsp_register(chip
, CHI32_STATUS_REG
) & CHI32_STATUS_IRQ
) {
1024 #ifdef ECHOCARD_HAS_MIDI
1025 /* Get and parse midi data if present */
1026 if (chip
->comm_page
->midi_input
[0]) /* The count is at index 0 */
1027 st
= midi_service_irq(chip
); /* Returns how many midi bytes we received */
1029 /* Clear the hardware interrupt */
1030 chip
->comm_page
->midi_input
[0] = 0;
1031 send_vector(chip
, DSP_VC_ACK_INT
);
1040 /******************************************************************************
1041 Functions for opening and closing pipes
1042 ******************************************************************************/
1044 /* allocate_pipes is used to reserve audio pipes for your exclusive use.
1045 The call will fail if some pipes are already allocated. */
1046 static int allocate_pipes(struct echoaudio
*chip
, struct audiopipe
*pipe
,
1047 int pipe_index
, int interleave
)
1053 DE_ACT(("allocate_pipes: ch=%d int=%d\n", pipe_index
, interleave
));
1055 if (chip
->bad_board
)
1058 is_cyclic
= 1; /* This driver uses cyclic buffers only */
1060 for (channel_mask
= i
= 0; i
< interleave
; i
++)
1061 channel_mask
|= 1 << (pipe_index
+ i
);
1062 if (chip
->pipe_alloc_mask
& channel_mask
) {
1063 DE_ACT(("allocate_pipes: channel already open\n"));
1067 chip
->comm_page
->position
[pipe_index
] = 0;
1068 chip
->pipe_alloc_mask
|= channel_mask
;
1070 chip
->pipe_cyclic_mask
|= channel_mask
;
1071 pipe
->index
= pipe_index
;
1072 pipe
->interleave
= interleave
;
1073 pipe
->state
= PIPE_STATE_STOPPED
;
1075 /* The counter register is where the DSP writes the 32 bit DMA
1076 position for a pipe. The DSP is constantly updating this value as
1077 it moves data. The DMA counter is in units of bytes, not samples. */
1078 pipe
->dma_counter
= &chip
->comm_page
->position
[pipe_index
];
1079 *pipe
->dma_counter
= 0;
1080 DE_ACT(("allocate_pipes: ok\n"));
1086 static int free_pipes(struct echoaudio
*chip
, struct audiopipe
*pipe
)
1091 DE_ACT(("free_pipes: Pipe %d\n", pipe
->index
));
1092 if (snd_BUG_ON(!is_pipe_allocated(chip
, pipe
->index
)))
1094 if (snd_BUG_ON(pipe
->state
!= PIPE_STATE_STOPPED
))
1097 for (channel_mask
= i
= 0; i
< pipe
->interleave
; i
++)
1098 channel_mask
|= 1 << (pipe
->index
+ i
);
1100 chip
->pipe_alloc_mask
&= ~channel_mask
;
1101 chip
->pipe_cyclic_mask
&= ~channel_mask
;
1107 /******************************************************************************
1108 Functions for managing the scatter-gather list
1109 ******************************************************************************/
1111 static int sglist_init(struct echoaudio
*chip
, struct audiopipe
*pipe
)
1113 pipe
->sglist_head
= 0;
1114 memset(pipe
->sgpage
.area
, 0, PAGE_SIZE
);
1115 chip
->comm_page
->sglist_addr
[pipe
->index
].addr
=
1116 cpu_to_le32(pipe
->sgpage
.addr
);
1122 static int sglist_add_mapping(struct echoaudio
*chip
, struct audiopipe
*pipe
,
1123 dma_addr_t address
, size_t length
)
1125 int head
= pipe
->sglist_head
;
1126 struct sg_entry
*list
= (struct sg_entry
*)pipe
->sgpage
.area
;
1128 if (head
< MAX_SGLIST_ENTRIES
- 1) {
1129 list
[head
].addr
= cpu_to_le32(address
);
1130 list
[head
].size
= cpu_to_le32(length
);
1131 pipe
->sglist_head
++;
1133 DE_ACT(("SGlist: too many fragments\n"));
1141 static inline int sglist_add_irq(struct echoaudio
*chip
, struct audiopipe
*pipe
)
1143 return sglist_add_mapping(chip
, pipe
, 0, 0);
1148 static inline int sglist_wrap(struct echoaudio
*chip
, struct audiopipe
*pipe
)
1150 return sglist_add_mapping(chip
, pipe
, pipe
->sgpage
.addr
, 0);