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
,
179 const struct firmware
*asic
)
181 const struct firmware
*fw
;
186 if ((err
= get_firmware(&fw
, asic
, chip
)) < 0) {
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 if ((i
= get_firmware(&fw
, &card_fw
[FW_361_LOADER
], chip
)) < 0) {
249 snd_printk(KERN_WARNING
"Firmware not found !\n");
253 /* The DSP code is an array of 16 bit words. The array is divided up
254 into sections. The first word of each section is the size in words,
255 followed by the section type.
256 Since DSP addresses and data are 24 bits wide, they each take up two
257 16 bit words in the array.
258 This is a lot like the other loader loop, but it's not a loop, you
259 don't write the memory type, and you don't write a zero at the end. */
261 /* Set DSP format bits for 24 bit mode */
262 set_dsp_register(chip
, CHI32_CONTROL_REG
,
263 get_dsp_register(chip
, CHI32_CONTROL_REG
) | 0x900);
265 code
= (u16
*)fw
->data
;
267 /* Skip the header section; the first word in the array is the size
268 of the first section, so the first real section of code is pointed
272 /* Skip the section size, LRS block type, and DSP memory type */
275 /* Get the number of DSP words to write */
276 words
= code
[index
++];
278 /* Get the DSP address for this block; 24 bits, so build from two words */
279 address
= ((u32
)code
[index
] << 16) + code
[index
+ 1];
282 /* Write the count to the DSP */
283 if (write_dsp(chip
, words
)) {
284 DE_INIT(("install_resident_loader: Failed to write word count!\n"));
287 /* Write the DSP address */
288 if (write_dsp(chip
, address
)) {
289 DE_INIT(("install_resident_loader: Failed to write DSP address!\n"));
292 /* Write out this block of code to the DSP */
293 for (i
= 0; i
< words
; i
++) {
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"));
304 /* Wait for flag 5 to come up */
305 for (i
= 0; i
< 200; i
++) { /* Timeout is 50us * 200 = 10ms */
307 status
= get_dsp_register(chip
, CHI32_STATUS_REG
);
308 if (status
& CHI32_STATUS_REG_HF5
)
313 DE_INIT(("Resident loader failed to set HF5\n"));
317 DE_INIT(("Resident loader successfully installed\n"));
326 #endif /* DSP_56361 */
329 static int load_dsp(struct echoaudio
*chip
, u16
*code
)
334 if (chip
->dsp_code
== code
) {
335 DE_INIT(("DSP is already loaded!\n"));
338 chip
->bad_board
= TRUE
; /* Set TRUE until DSP loaded */
339 chip
->dsp_code
= NULL
; /* Current DSP code not loaded */
340 chip
->asic_loaded
= FALSE
; /* Loading the DSP code will reset the ASIC */
342 DE_INIT(("load_dsp: Set bad_board to TRUE\n"));
344 /* If this board requires a resident loader, install it. */
346 if ((i
= install_resident_loader(chip
)) < 0)
350 /* Send software reset command */
351 if (send_vector(chip
, DSP_VC_RESET
) < 0) {
352 DE_INIT(("LoadDsp: send_vector DSP_VC_RESET failed, Critical Failure\n"));
358 /* Wait 10ms for HF3 to indicate that software reset is complete */
359 for (i
= 0; i
< 1000; i
++) { /* Timeout is 10us * 1000 = 10ms */
360 if (get_dsp_register(chip
, CHI32_STATUS_REG
) &
361 CHI32_STATUS_REG_HF3
)
367 DE_INIT(("load_dsp: Timeout waiting for CHI32_STATUS_REG_HF3\n"));
371 /* Set DSP format bits for 24 bit mode now that soft reset is done */
372 set_dsp_register(chip
, CHI32_CONTROL_REG
,
373 get_dsp_register(chip
, CHI32_CONTROL_REG
) | 0x900);
375 /* Main loader loop */
379 int block_type
, mem_type
;
381 /* Total Block Size */
385 block_type
= code
[index
];
386 if (block_type
== 4) /* We're finished */
391 /* Memory Type P=0,X=1,Y=2 */
392 mem_type
= code
[index
++];
394 /* Block Code Size */
395 words
= code
[index
++];
396 if (words
== 0) /* We're finished */
400 address
= ((u32
)code
[index
] << 16) + code
[index
+ 1];
403 if (write_dsp(chip
, words
) < 0) {
404 DE_INIT(("load_dsp: failed to write number of DSP words\n"));
407 if (write_dsp(chip
, address
) < 0) {
408 DE_INIT(("load_dsp: failed to write DSP address\n"));
411 if (write_dsp(chip
, mem_type
) < 0) {
412 DE_INIT(("load_dsp: failed to write DSP memory type\n"));
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"));
425 if (write_dsp(chip
, 0) < 0) { /* We're done!!! */
426 DE_INIT(("load_dsp: Failed to write final zero\n"));
431 for (i
= 0; i
< 5000; i
++) { /* Timeout is 100us * 5000 = 500ms */
432 /* Wait for flag 4 - indicates that the DSP loaded OK */
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);
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"));
443 if (write_dsp(chip
, chip
->comm_page_phys
) < 0) {
444 DE_INIT(("load_dsp: Failed to write comm page address\n"));
448 /* Get the serial number via slave mode.
449 This is triggered by the SET_COMMPAGE_ADDR command.
450 We don't actually use the serial number but we have to
451 get it as part of the DSP init voodoo. */
452 if (read_sn(chip
) < 0) {
453 DE_INIT(("load_dsp: Failed to read serial number\n"));
457 chip
->dsp_code
= code
; /* Show which DSP code loaded */
458 chip
->bad_board
= FALSE
; /* DSP OK */
459 DE_INIT(("load_dsp: OK!\n"));
465 DE_INIT(("load_dsp: DSP load timed out waiting for HF4\n"));
471 /* load_firmware takes care of loading the DSP and any ASIC code. */
472 static int load_firmware(struct echoaudio
*chip
)
474 const struct firmware
*fw
;
477 if (snd_BUG_ON(!chip
->dsp_code_to_load
|| !chip
->comm_page
))
480 /* See if the ASIC is present and working - only if the DSP is already loaded */
481 if (chip
->dsp_code
) {
482 if ((box_type
= check_asic_status(chip
)) >= 0)
484 /* ASIC check failed; force the DSP to reload */
485 chip
->dsp_code
= NULL
;
488 if ((err
= get_firmware(&fw
, chip
->dsp_code_to_load
, chip
)) < 0)
490 err
= load_dsp(chip
, (u16
*)fw
->data
);
495 if ((box_type
= load_asic(chip
)) < 0)
496 return box_type
; /* error */
498 if ((err
= restore_dsp_rettings(chip
)) < 0)
506 /****************************************************************************
508 ****************************************************************************/
510 #if defined(ECHOCARD_HAS_INPUT_NOMINAL_LEVEL) || \
511 defined(ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL)
513 /* Set the nominal level for an input or output bus (true = -10dBV, false = +4dBu) */
514 static int set_nominal_level(struct echoaudio
*chip
, u16 index
, char consumer
)
516 if (snd_BUG_ON(index
>= num_busses_out(chip
) + num_busses_in(chip
)))
519 /* Wait for the handshake (OK even if ASIC is not loaded) */
520 if (wait_handshake(chip
))
523 chip
->nominal_level
[index
] = consumer
;
526 chip
->comm_page
->nominal_level_mask
|= cpu_to_le32(1 << index
);
528 chip
->comm_page
->nominal_level_mask
&= ~cpu_to_le32(1 << index
);
533 #endif /* ECHOCARD_HAS_*_NOMINAL_LEVEL */
537 /* Set the gain for a single physical output channel (dB). */
538 static int set_output_gain(struct echoaudio
*chip
, u16 channel
, s8 gain
)
540 if (snd_BUG_ON(channel
>= num_busses_out(chip
)))
543 if (wait_handshake(chip
))
546 /* Save the new value */
547 chip
->output_gain
[channel
] = gain
;
548 chip
->comm_page
->line_out_level
[channel
] = gain
;
554 #ifdef ECHOCARD_HAS_MONITOR
555 /* Set the monitor level from an input bus to an output bus. */
556 static int set_monitor_gain(struct echoaudio
*chip
, u16 output
, u16 input
,
559 if (snd_BUG_ON(output
>= num_busses_out(chip
) ||
560 input
>= num_busses_in(chip
)))
563 if (wait_handshake(chip
))
566 chip
->monitor_gain
[output
][input
] = gain
;
567 chip
->comm_page
->monitors
[monitor_index(chip
, output
, input
)] = gain
;
570 #endif /* ECHOCARD_HAS_MONITOR */
573 /* Tell the DSP to read and update output, nominal & monitor levels in comm page. */
574 static int update_output_line_level(struct echoaudio
*chip
)
576 if (wait_handshake(chip
))
578 clear_handshake(chip
);
579 return send_vector(chip
, DSP_VC_UPDATE_OUTVOL
);
584 /* Tell the DSP to read and update input levels in comm page */
585 static int update_input_line_level(struct echoaudio
*chip
)
587 if (wait_handshake(chip
))
589 clear_handshake(chip
);
590 return send_vector(chip
, DSP_VC_UPDATE_INGAIN
);
595 /* set_meters_on turns the meters on or off. If meters are turned on, the DSP
596 will write the meter and clock detect values to the comm page at about 30Hz */
597 static void set_meters_on(struct echoaudio
*chip
, char on
)
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
,
607 memset((s8
*)chip
->comm_page
->peak_meter
, ECHOGAIN_MUTED
,
614 /* Fill out an the given array using the current values in the comm page.
615 Meters are written in the comm page by the DSP in this order:
618 Output pipes (vmixer cards only)
620 This function assumes there are no more than 16 in/out busses or pipes
621 Meters is an array [3][16][2] of long. */
622 static void get_audio_meters(struct echoaudio
*chip
, long *meters
)
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
];
635 #ifdef ECHOCARD_ECHO3G
636 m
= E3G_MAX_OUTPUTS
; /* Skip unused meters */
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
];
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
];
658 static int restore_dsp_rettings(struct echoaudio
*chip
)
661 DE_INIT(("restore_dsp_settings\n"));
663 if ((err
= check_asic_status(chip
)) < 0)
666 /* @ Gina20/Darla20 only. Should be harmless for other cards. */
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;
671 if ((err
= set_sample_rate(chip
, chip
->sample_rate
)) < 0)
674 if (chip
->meters_enabled
)
675 if (send_vector(chip
, DSP_VC_METERS_ON
) < 0)
678 #ifdef ECHOCARD_HAS_EXTERNAL_CLOCK
679 if (set_input_clock(chip
, chip
->input_clock
) < 0)
683 #ifdef ECHOCARD_HAS_OUTPUT_CLOCK_SWITCH
684 if (set_output_clock(chip
, chip
->output_clock
) < 0)
688 if (update_output_line_level(chip
) < 0)
691 if (update_input_line_level(chip
) < 0)
694 #ifdef ECHOCARD_HAS_VMIXER
695 if (update_vmixer_level(chip
) < 0)
699 if (wait_handshake(chip
) < 0)
701 clear_handshake(chip
);
703 DE_INIT(("restore_dsp_rettings done\n"));
704 return send_vector(chip
, DSP_VC_UPDATE_FLAGS
);
709 /****************************************************************************
711 ****************************************************************************/
713 /* set_audio_format() sets the format of the audio data in host memory for
714 this pipe. Note that _MS_ (mono-to-stereo) playback modes are not used by ALSA
715 but they are here because they are just mono while capturing */
716 static void set_audio_format(struct echoaudio
*chip
, u16 pipe_index
,
717 const struct audioformat
*format
)
721 dsp_format
= DSP_AUDIOFORM_SS_16LE
;
723 /* Look for super-interleave (no big-endian and 8 bits) */
724 if (format
->interleave
> 2) {
725 switch (format
->bits_per_sample
) {
727 dsp_format
= DSP_AUDIOFORM_SUPER_INTERLEAVE_16LE
;
730 dsp_format
= DSP_AUDIOFORM_SUPER_INTERLEAVE_24LE
;
733 dsp_format
= DSP_AUDIOFORM_SUPER_INTERLEAVE_32LE
;
736 dsp_format
|= format
->interleave
;
737 } else if (format
->data_are_bigendian
) {
738 /* For big-endian data, only 32 bit samples are supported */
739 switch (format
->interleave
) {
741 dsp_format
= DSP_AUDIOFORM_MM_32BE
;
743 #ifdef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
745 dsp_format
= DSP_AUDIOFORM_SS_32BE
;
749 } else if (format
->interleave
== 1 &&
750 format
->bits_per_sample
== 32 && !format
->mono_to_stereo
) {
751 /* 32 bit little-endian mono->mono case */
752 dsp_format
= DSP_AUDIOFORM_MM_32LE
;
754 /* Handle the other little-endian formats */
755 switch (format
->bits_per_sample
) {
757 if (format
->interleave
== 2)
758 dsp_format
= DSP_AUDIOFORM_SS_8
;
760 dsp_format
= DSP_AUDIOFORM_MS_8
;
764 if (format
->interleave
== 2)
765 dsp_format
= DSP_AUDIOFORM_SS_16LE
;
767 dsp_format
= DSP_AUDIOFORM_MS_16LE
;
770 if (format
->interleave
== 2)
771 dsp_format
= DSP_AUDIOFORM_SS_24LE
;
773 dsp_format
= DSP_AUDIOFORM_MS_24LE
;
776 if (format
->interleave
== 2)
777 dsp_format
= DSP_AUDIOFORM_SS_32LE
;
779 dsp_format
= DSP_AUDIOFORM_MS_32LE
;
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
);
789 /* start_transport starts transport for a set of pipes.
790 The bits 1 in channel_mask specify what pipes to start. Only the bit of the
791 first channel must be set, regardless its interleave.
792 Same thing for pause_ and stop_ -trasport below. */
793 static int start_transport(struct echoaudio
*chip
, u32 channel_mask
,
796 DE_ACT(("start_transport %x\n", channel_mask
));
798 if (wait_handshake(chip
))
801 chip
->comm_page
->cmd_start
|= cpu_to_le32(channel_mask
);
803 if (chip
->comm_page
->cmd_start
) {
804 clear_handshake(chip
);
805 send_vector(chip
, DSP_VC_START_TRANSFER
);
806 if (wait_handshake(chip
))
808 /* Keep track of which pipes are transporting */
809 chip
->active_mask
|= channel_mask
;
810 chip
->comm_page
->cmd_start
= 0;
814 DE_ACT(("start_transport: No pipes to start!\n"));
820 static int pause_transport(struct echoaudio
*chip
, u32 channel_mask
)
822 DE_ACT(("pause_transport %x\n", channel_mask
));
824 if (wait_handshake(chip
))
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
))
834 /* Keep track of which pipes are transporting */
835 chip
->active_mask
&= ~channel_mask
;
836 chip
->comm_page
->cmd_stop
= 0;
837 chip
->comm_page
->cmd_reset
= 0;
841 DE_ACT(("pause_transport: No pipes to stop!\n"));
847 static int stop_transport(struct echoaudio
*chip
, u32 channel_mask
)
849 DE_ACT(("stop_transport %x\n", channel_mask
));
851 if (wait_handshake(chip
))
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
))
861 /* Keep track of which pipes are transporting */
862 chip
->active_mask
&= ~channel_mask
;
863 chip
->comm_page
->cmd_stop
= 0;
864 chip
->comm_page
->cmd_reset
= 0;
868 DE_ACT(("stop_transport: No pipes to stop!\n"));
874 static inline int is_pipe_allocated(struct echoaudio
*chip
, u16 pipe_index
)
876 return (chip
->pipe_alloc_mask
& (1 << pipe_index
));
881 /* Stops everything and turns off the DSP. All pipes should be already
882 stopped and unallocated. */
883 static int rest_in_peace(struct echoaudio
*chip
)
885 DE_ACT(("rest_in_peace() open=%x\n", chip
->pipe_alloc_mask
));
887 /* Stops all active pipes (just to be sure) */
888 stop_transport(chip
, chip
->active_mask
);
890 set_meters_on(chip
, FALSE
);
892 #ifdef ECHOCARD_HAS_MIDI
893 enable_midi_input(chip
, FALSE
);
897 if (chip
->dsp_code
) {
898 /* Make load_firmware do a complete reload */
899 chip
->dsp_code
= NULL
;
900 /* Put the DSP to sleep */
901 return send_vector(chip
, DSP_VC_GO_COMATOSE
);
908 /* Fills the comm page with default values */
909 static int init_dsp_comm_page(struct echoaudio
*chip
)
911 /* Check if the compiler added extra padding inside the structure */
912 if (offsetof(struct comm_page
, midi_output
) != 0xbe0) {
913 DE_INIT(("init_dsp_comm_page() - Invalid struct comm_page structure\n"));
917 /* Init all the basic stuff */
918 chip
->card_name
= ECHOCARD_NAME
;
919 chip
->bad_board
= TRUE
; /* Set TRUE until DSP loaded */
920 chip
->dsp_code
= NULL
; /* Current DSP code not loaded */
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
));
927 /* Init the comm page */
928 chip
->comm_page
->comm_size
=
929 cpu_to_le32(sizeof(struct comm_page
));
930 chip
->comm_page
->handshake
= 0xffffffff;
931 chip
->comm_page
->midi_out_free_count
=
932 cpu_to_le32(DSP_MIDI_OUT_FIFO_SIZE
);
933 chip
->comm_page
->sample_rate
= cpu_to_le32(44100);
934 chip
->sample_rate
= 44100;
936 /* Set line levels so we don't blast any inputs on startup */
937 memset(chip
->comm_page
->monitors
, ECHOGAIN_MUTED
, MONITOR_ARRAY_SIZE
);
938 memset(chip
->comm_page
->vmixer
, ECHOGAIN_MUTED
, VMIXER_ARRAY_SIZE
);
945 /* This function initializes the several volume controls for busses and pipes.
946 This MUST be called after the DSP is up and running ! */
947 static int init_line_levels(struct echoaudio
*chip
)
951 DE_INIT(("init_line_levels\n"));
953 /* Mute output busses */
954 for (i
= 0; i
< num_busses_out(chip
); i
++)
955 if ((st
= set_output_gain(chip
, i
, ECHOGAIN_MUTED
)))
957 if ((st
= update_output_line_level(chip
)))
960 #ifdef ECHOCARD_HAS_VMIXER
961 /* Mute the Vmixer */
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
)))
966 if ((st
= update_vmixer_level(chip
)))
968 #endif /* ECHOCARD_HAS_VMIXER */
970 #ifdef ECHOCARD_HAS_MONITOR
971 /* Mute the monitor mixer */
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
)))
976 if ((st
= update_output_line_level(chip
)))
978 #endif /* ECHOCARD_HAS_MONITOR */
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
)))
984 if ((st
= update_input_line_level(chip
)))
986 #endif /* ECHOCARD_HAS_INPUT_GAIN */
993 /* This is low level part of the interrupt handler.
994 It returns -1 if the IRQ is not ours, or N>=0 if it is, where N is the number
995 of midi data in the input queue. */
996 static int service_irq(struct echoaudio
*chip
)
1000 /* Read the DSP status register and see if this DSP generated this interrupt */
1001 if (get_dsp_register(chip
, CHI32_STATUS_REG
) & CHI32_STATUS_IRQ
) {
1003 #ifdef ECHOCARD_HAS_MIDI
1004 /* Get and parse midi data if present */
1005 if (chip
->comm_page
->midi_input
[0]) /* The count is at index 0 */
1006 st
= midi_service_irq(chip
); /* Returns how many midi bytes we received */
1008 /* Clear the hardware interrupt */
1009 chip
->comm_page
->midi_input
[0] = 0;
1010 send_vector(chip
, DSP_VC_ACK_INT
);
1019 /******************************************************************************
1020 Functions for opening and closing pipes
1021 ******************************************************************************/
1023 /* allocate_pipes is used to reserve audio pipes for your exclusive use.
1024 The call will fail if some pipes are already allocated. */
1025 static int allocate_pipes(struct echoaudio
*chip
, struct audiopipe
*pipe
,
1026 int pipe_index
, int interleave
)
1032 DE_ACT(("allocate_pipes: ch=%d int=%d\n", pipe_index
, interleave
));
1034 if (chip
->bad_board
)
1037 is_cyclic
= 1; /* This driver uses cyclic buffers only */
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"));
1046 chip
->comm_page
->position
[pipe_index
] = 0;
1047 chip
->pipe_alloc_mask
|= channel_mask
;
1049 chip
->pipe_cyclic_mask
|= channel_mask
;
1050 pipe
->index
= pipe_index
;
1051 pipe
->interleave
= interleave
;
1052 pipe
->state
= PIPE_STATE_STOPPED
;
1054 /* The counter register is where the DSP writes the 32 bit DMA
1055 position for a pipe. The DSP is constantly updating this value as
1056 it moves data. The DMA counter is in units of bytes, not samples. */
1057 pipe
->dma_counter
= &chip
->comm_page
->position
[pipe_index
];
1058 *pipe
->dma_counter
= 0;
1059 DE_ACT(("allocate_pipes: ok\n"));
1065 static int free_pipes(struct echoaudio
*chip
, struct audiopipe
*pipe
)
1070 DE_ACT(("free_pipes: Pipe %d\n", pipe
->index
));
1071 if (snd_BUG_ON(!is_pipe_allocated(chip
, pipe
->index
)))
1073 if (snd_BUG_ON(pipe
->state
!= PIPE_STATE_STOPPED
))
1076 for (channel_mask
= i
= 0; i
< pipe
->interleave
; i
++)
1077 channel_mask
|= 1 << (pipe
->index
+ i
);
1079 chip
->pipe_alloc_mask
&= ~channel_mask
;
1080 chip
->pipe_cyclic_mask
&= ~channel_mask
;
1086 /******************************************************************************
1087 Functions for managing the scatter-gather list
1088 ******************************************************************************/
1090 static int sglist_init(struct echoaudio
*chip
, struct audiopipe
*pipe
)
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
);
1101 static int sglist_add_mapping(struct echoaudio
*chip
, struct audiopipe
*pipe
,
1102 dma_addr_t address
, size_t length
)
1104 int head
= pipe
->sglist_head
;
1105 struct sg_entry
*list
= (struct sg_entry
*)pipe
->sgpage
.area
;
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
++;
1112 DE_ACT(("SGlist: too many fragments\n"));
1120 static inline int sglist_add_irq(struct echoaudio
*chip
, struct audiopipe
*pipe
)
1122 return sglist_add_mapping(chip
, pipe
, 0, 0);
1127 static inline int sglist_wrap(struct echoaudio
*chip
, struct audiopipe
*pipe
)
1129 return sglist_add_mapping(chip
, pipe
, pipe
->sgpage
.addr
, 0);