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 dev_err(chip
->card
->dev
, "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 dev_err(chip
->card
->dev
, "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 dev_dbg(chip
->card
->dev
, "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 dev_err(chip
->card
->dev
, "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 dev_err(chip
->card
->dev
,
153 "Failed to read serial number\n");
157 dev_dbg(chip
->card
->dev
,
158 "Read serial number %08x %08x %08x %08x %08x\n",
159 sn
[0], sn
[1], sn
[2], sn
[3], sn
[4]);
165 #ifndef ECHOCARD_HAS_ASIC
166 /* This card has no ASIC, just return ok */
167 static inline int check_asic_status(struct echoaudio
*chip
)
169 chip
->asic_loaded
= true;
173 #endif /* !ECHOCARD_HAS_ASIC */
177 #ifdef ECHOCARD_HAS_ASIC
179 /* Load ASIC code - done after the DSP is loaded */
180 static int load_asic_generic(struct echoaudio
*chip
, u32 cmd
, short asic
)
182 const struct firmware
*fw
;
187 err
= get_firmware(&fw
, chip
, asic
);
189 dev_warn(chip
->card
->dev
, "Firmware not found !\n");
193 code
= (u8
*)fw
->data
;
196 /* Send the "Here comes the ASIC" command */
197 if (write_dsp(chip
, cmd
) < 0)
200 /* Write length of ASIC file in bytes */
201 if (write_dsp(chip
, size
) < 0)
204 for (i
= 0; i
< size
; i
++) {
205 if (write_dsp(chip
, code
[i
]) < 0)
209 free_firmware(fw
, chip
);
213 dev_err(chip
->card
->dev
, "failed on write_dsp\n");
214 free_firmware(fw
, chip
);
218 #endif /* ECHOCARD_HAS_ASIC */
224 /* Install the resident loader for 56361 DSPs; The resident loader is on
225 the EPROM on the board for 56301 DSP. The resident loader is a tiny little
226 program that is used to load the real DSP code. */
227 static int install_resident_loader(struct echoaudio
*chip
)
233 const struct firmware
*fw
;
235 /* 56361 cards only! This check is required by the old 56301-based
237 if (chip
->device_id
!= DEVICE_ID_56361
)
240 /* Look to see if the resident loader is present. If the resident
241 loader is already installed, host flag 5 will be on. */
242 status
= get_dsp_register(chip
, CHI32_STATUS_REG
);
243 if (status
& CHI32_STATUS_REG_HF5
) {
244 dev_dbg(chip
->card
->dev
,
245 "Resident loader already installed; status is 0x%x\n",
250 i
= get_firmware(&fw
, chip
, FW_361_LOADER
);
252 dev_warn(chip
->card
->dev
, "Firmware not found !\n");
256 /* The DSP code is an array of 16 bit words. The array is divided up
257 into sections. The first word of each section is the size in words,
258 followed by the section type.
259 Since DSP addresses and data are 24 bits wide, they each take up two
260 16 bit words in the array.
261 This is a lot like the other loader loop, but it's not a loop, you
262 don't write the memory type, and you don't write a zero at the end. */
264 /* Set DSP format bits for 24 bit mode */
265 set_dsp_register(chip
, CHI32_CONTROL_REG
,
266 get_dsp_register(chip
, CHI32_CONTROL_REG
) | 0x900);
268 code
= (u16
*)fw
->data
;
270 /* Skip the header section; the first word in the array is the size
271 of the first section, so the first real section of code is pointed
275 /* Skip the section size, LRS block type, and DSP memory type */
278 /* Get the number of DSP words to write */
279 words
= code
[index
++];
281 /* Get the DSP address for this block; 24 bits, so build from two words */
282 address
= ((u32
)code
[index
] << 16) + code
[index
+ 1];
285 /* Write the count to the DSP */
286 if (write_dsp(chip
, words
)) {
287 dev_err(chip
->card
->dev
,
288 "install_resident_loader: Failed to write word count!\n");
291 /* Write the DSP address */
292 if (write_dsp(chip
, address
)) {
293 dev_err(chip
->card
->dev
,
294 "install_resident_loader: Failed to write DSP address!\n");
297 /* Write out this block of code to the DSP */
298 for (i
= 0; i
< words
; i
++) {
301 data
= ((u32
)code
[index
] << 16) + code
[index
+ 1];
302 if (write_dsp(chip
, data
)) {
303 dev_err(chip
->card
->dev
,
304 "install_resident_loader: Failed to write DSP code\n");
310 /* Wait for flag 5 to come up */
311 for (i
= 0; i
< 200; i
++) { /* Timeout is 50us * 200 = 10ms */
313 status
= get_dsp_register(chip
, CHI32_STATUS_REG
);
314 if (status
& CHI32_STATUS_REG_HF5
)
319 dev_err(chip
->card
->dev
, "Resident loader failed to set HF5\n");
323 dev_dbg(chip
->card
->dev
, "Resident loader successfully installed\n");
324 free_firmware(fw
, chip
);
328 free_firmware(fw
, chip
);
332 #endif /* DSP_56361 */
335 static int load_dsp(struct echoaudio
*chip
, u16
*code
)
340 if (chip
->dsp_code
== code
) {
341 dev_warn(chip
->card
->dev
, "DSP is already loaded!\n");
344 chip
->bad_board
= true; /* Set true until DSP loaded */
345 chip
->dsp_code
= NULL
; /* Current DSP code not loaded */
346 chip
->asic_loaded
= false; /* Loading the DSP code will reset the ASIC */
348 dev_dbg(chip
->card
->dev
, "load_dsp: Set bad_board to true\n");
350 /* If this board requires a resident loader, install it. */
352 i
= install_resident_loader(chip
);
357 /* Send software reset command */
358 if (send_vector(chip
, DSP_VC_RESET
) < 0) {
359 dev_err(chip
->card
->dev
,
360 "LoadDsp: send_vector DSP_VC_RESET failed, Critical Failure\n");
366 /* Wait 10ms for HF3 to indicate that software reset is complete */
367 for (i
= 0; i
< 1000; i
++) { /* Timeout is 10us * 1000 = 10ms */
368 if (get_dsp_register(chip
, CHI32_STATUS_REG
) &
369 CHI32_STATUS_REG_HF3
)
375 dev_err(chip
->card
->dev
,
376 "load_dsp: Timeout waiting for CHI32_STATUS_REG_HF3\n");
380 /* Set DSP format bits for 24 bit mode now that soft reset is done */
381 set_dsp_register(chip
, CHI32_CONTROL_REG
,
382 get_dsp_register(chip
, CHI32_CONTROL_REG
) | 0x900);
384 /* Main loader loop */
388 int block_type
, mem_type
;
390 /* Total Block Size */
394 block_type
= code
[index
];
395 if (block_type
== 4) /* We're finished */
400 /* Memory Type P=0,X=1,Y=2 */
401 mem_type
= code
[index
++];
403 /* Block Code Size */
404 words
= code
[index
++];
405 if (words
== 0) /* We're finished */
409 address
= ((u32
)code
[index
] << 16) + code
[index
+ 1];
412 if (write_dsp(chip
, words
) < 0) {
413 dev_err(chip
->card
->dev
,
414 "load_dsp: failed to write number of DSP words\n");
417 if (write_dsp(chip
, address
) < 0) {
418 dev_err(chip
->card
->dev
,
419 "load_dsp: failed to write DSP address\n");
422 if (write_dsp(chip
, mem_type
) < 0) {
423 dev_err(chip
->card
->dev
,
424 "load_dsp: failed to write DSP memory type\n");
428 for (i
= 0; i
< words
; i
++, index
+=2) {
429 data
= ((u32
)code
[index
] << 16) + code
[index
+ 1];
430 if (write_dsp(chip
, data
) < 0) {
431 dev_err(chip
->card
->dev
,
432 "load_dsp: failed to write DSP data\n");
438 if (write_dsp(chip
, 0) < 0) { /* We're done!!! */
439 dev_err(chip
->card
->dev
,
440 "load_dsp: Failed to write final zero\n");
445 for (i
= 0; i
< 5000; i
++) { /* Timeout is 100us * 5000 = 500ms */
446 /* Wait for flag 4 - indicates that the DSP loaded OK */
447 if (get_dsp_register(chip
, CHI32_STATUS_REG
) &
448 CHI32_STATUS_REG_HF4
) {
449 set_dsp_register(chip
, CHI32_CONTROL_REG
,
450 get_dsp_register(chip
, CHI32_CONTROL_REG
) & ~0x1b00);
452 if (write_dsp(chip
, DSP_FNC_SET_COMMPAGE_ADDR
) < 0) {
453 dev_err(chip
->card
->dev
,
454 "load_dsp: Failed to write DSP_FNC_SET_COMMPAGE_ADDR\n");
458 if (write_dsp(chip
, chip
->comm_page_phys
) < 0) {
459 dev_err(chip
->card
->dev
,
460 "load_dsp: Failed to write comm page address\n");
464 /* Get the serial number via slave mode.
465 This is triggered by the SET_COMMPAGE_ADDR command.
466 We don't actually use the serial number but we have to
467 get it as part of the DSP init voodoo. */
468 if (read_sn(chip
) < 0) {
469 dev_err(chip
->card
->dev
,
470 "load_dsp: Failed to read serial number\n");
474 chip
->dsp_code
= code
; /* Show which DSP code loaded */
475 chip
->bad_board
= false; /* DSP OK */
481 dev_err(chip
->card
->dev
,
482 "load_dsp: DSP load timed out waiting for HF4\n");
488 /* load_firmware takes care of loading the DSP and any ASIC code. */
489 static int load_firmware(struct echoaudio
*chip
)
491 const struct firmware
*fw
;
494 if (snd_BUG_ON(!chip
->comm_page
))
497 /* See if the ASIC is present and working - only if the DSP is already loaded */
498 if (chip
->dsp_code
) {
499 box_type
= check_asic_status(chip
);
502 /* ASIC check failed; force the DSP to reload */
503 chip
->dsp_code
= NULL
;
506 err
= get_firmware(&fw
, chip
, chip
->dsp_code_to_load
);
509 err
= load_dsp(chip
, (u16
*)fw
->data
);
510 free_firmware(fw
, chip
);
514 box_type
= load_asic(chip
);
516 return box_type
; /* error */
523 /****************************************************************************
525 ****************************************************************************/
527 #if defined(ECHOCARD_HAS_INPUT_NOMINAL_LEVEL) || \
528 defined(ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL)
530 /* Set the nominal level for an input or output bus (true = -10dBV, false = +4dBu) */
531 static int set_nominal_level(struct echoaudio
*chip
, u16 index
, char consumer
)
533 if (snd_BUG_ON(index
>= num_busses_out(chip
) + num_busses_in(chip
)))
536 /* Wait for the handshake (OK even if ASIC is not loaded) */
537 if (wait_handshake(chip
))
540 chip
->nominal_level
[index
] = consumer
;
543 chip
->comm_page
->nominal_level_mask
|= cpu_to_le32(1 << index
);
545 chip
->comm_page
->nominal_level_mask
&= ~cpu_to_le32(1 << index
);
550 #endif /* ECHOCARD_HAS_*_NOMINAL_LEVEL */
554 /* Set the gain for a single physical output channel (dB). */
555 static int set_output_gain(struct echoaudio
*chip
, u16 channel
, s8 gain
)
557 if (snd_BUG_ON(channel
>= num_busses_out(chip
)))
560 if (wait_handshake(chip
))
563 /* Save the new value */
564 chip
->output_gain
[channel
] = gain
;
565 chip
->comm_page
->line_out_level
[channel
] = gain
;
571 #ifdef ECHOCARD_HAS_MONITOR
572 /* Set the monitor level from an input bus to an output bus. */
573 static int set_monitor_gain(struct echoaudio
*chip
, u16 output
, u16 input
,
576 if (snd_BUG_ON(output
>= num_busses_out(chip
) ||
577 input
>= num_busses_in(chip
)))
580 if (wait_handshake(chip
))
583 chip
->monitor_gain
[output
][input
] = gain
;
584 chip
->comm_page
->monitors
[monitor_index(chip
, output
, input
)] = gain
;
587 #endif /* ECHOCARD_HAS_MONITOR */
590 /* Tell the DSP to read and update output, nominal & monitor levels in comm page. */
591 static int update_output_line_level(struct echoaudio
*chip
)
593 if (wait_handshake(chip
))
595 clear_handshake(chip
);
596 return send_vector(chip
, DSP_VC_UPDATE_OUTVOL
);
601 /* Tell the DSP to read and update input levels in comm page */
602 static int update_input_line_level(struct echoaudio
*chip
)
604 if (wait_handshake(chip
))
606 clear_handshake(chip
);
607 return send_vector(chip
, DSP_VC_UPDATE_INGAIN
);
612 /* set_meters_on turns the meters on or off. If meters are turned on, the DSP
613 will write the meter and clock detect values to the comm page at about 30Hz */
614 static void set_meters_on(struct echoaudio
*chip
, char on
)
616 if (on
&& !chip
->meters_enabled
) {
617 send_vector(chip
, DSP_VC_METERS_ON
);
618 chip
->meters_enabled
= 1;
619 } else if (!on
&& chip
->meters_enabled
) {
620 send_vector(chip
, DSP_VC_METERS_OFF
);
621 chip
->meters_enabled
= 0;
622 memset((s8
*)chip
->comm_page
->vu_meter
, ECHOGAIN_MUTED
,
624 memset((s8
*)chip
->comm_page
->peak_meter
, ECHOGAIN_MUTED
,
631 /* Fill out an the given array using the current values in the comm page.
632 Meters are written in the comm page by the DSP in this order:
635 Output pipes (vmixer cards only)
637 This function assumes there are no more than 16 in/out busses or pipes
638 Meters is an array [3][16][2] of long. */
639 static void get_audio_meters(struct echoaudio
*chip
, long *meters
)
641 unsigned int i
, m
, n
;
643 for (i
= 0 ; i
< 96; i
++)
646 for (m
= 0, n
= 0, i
= 0; i
< num_busses_out(chip
); i
++, m
++) {
647 meters
[n
++] = chip
->comm_page
->vu_meter
[m
];
648 meters
[n
++] = chip
->comm_page
->peak_meter
[m
];
651 #ifdef ECHOCARD_ECHO3G
652 m
= E3G_MAX_OUTPUTS
; /* Skip unused meters */
655 for (n
= 32, i
= 0; i
< num_busses_in(chip
); i
++, m
++) {
656 meters
[n
++] = chip
->comm_page
->vu_meter
[m
];
657 meters
[n
++] = chip
->comm_page
->peak_meter
[m
];
659 #ifdef ECHOCARD_HAS_VMIXER
660 for (n
= 64, i
= 0; i
< num_pipes_out(chip
); i
++, m
++) {
661 meters
[n
++] = chip
->comm_page
->vu_meter
[m
];
662 meters
[n
++] = chip
->comm_page
->peak_meter
[m
];
669 static int restore_dsp_rettings(struct echoaudio
*chip
)
673 err
= check_asic_status(chip
);
677 /* Gina20/Darla20 only. Should be harmless for other cards. */
678 chip
->comm_page
->gd_clock_state
= GD_CLOCK_UNDEF
;
679 chip
->comm_page
->gd_spdif_status
= GD_SPDIF_STATUS_UNDEF
;
680 chip
->comm_page
->handshake
= cpu_to_le32(0xffffffff);
682 /* Restore output busses */
683 for (i
= 0; i
< num_busses_out(chip
); i
++) {
684 err
= set_output_gain(chip
, i
, chip
->output_gain
[i
]);
689 #ifdef ECHOCARD_HAS_VMIXER
690 for (i
= 0; i
< num_pipes_out(chip
); i
++)
691 for (o
= 0; o
< num_busses_out(chip
); o
++) {
692 err
= set_vmixer_gain(chip
, o
, i
,
693 chip
->vmixer_gain
[o
][i
]);
697 if (update_vmixer_level(chip
) < 0)
699 #endif /* ECHOCARD_HAS_VMIXER */
701 #ifdef ECHOCARD_HAS_MONITOR
702 for (o
= 0; o
< num_busses_out(chip
); o
++)
703 for (i
= 0; i
< num_busses_in(chip
); i
++) {
704 err
= set_monitor_gain(chip
, o
, i
,
705 chip
->monitor_gain
[o
][i
]);
709 #endif /* ECHOCARD_HAS_MONITOR */
711 #ifdef ECHOCARD_HAS_INPUT_GAIN
712 for (i
= 0; i
< num_busses_in(chip
); i
++) {
713 err
= set_input_gain(chip
, i
, chip
->input_gain
[i
]);
717 #endif /* ECHOCARD_HAS_INPUT_GAIN */
719 err
= update_output_line_level(chip
);
723 err
= update_input_line_level(chip
);
727 err
= set_sample_rate(chip
, chip
->sample_rate
);
731 if (chip
->meters_enabled
) {
732 err
= send_vector(chip
, DSP_VC_METERS_ON
);
737 #ifdef ECHOCARD_HAS_DIGITAL_MODE_SWITCH
738 if (set_digital_mode(chip
, chip
->digital_mode
) < 0)
742 #ifdef ECHOCARD_HAS_DIGITAL_IO
743 if (set_professional_spdif(chip
, chip
->professional_spdif
) < 0)
747 #ifdef ECHOCARD_HAS_PHANTOM_POWER
748 if (set_phantom_power(chip
, chip
->phantom_power
) < 0)
752 #ifdef ECHOCARD_HAS_EXTERNAL_CLOCK
753 /* set_input_clock() also restores automute setting */
754 if (set_input_clock(chip
, chip
->input_clock
) < 0)
758 #ifdef ECHOCARD_HAS_OUTPUT_CLOCK_SWITCH
759 if (set_output_clock(chip
, chip
->output_clock
) < 0)
763 if (wait_handshake(chip
) < 0)
765 clear_handshake(chip
);
766 if (send_vector(chip
, DSP_VC_UPDATE_FLAGS
) < 0)
774 /****************************************************************************
776 ****************************************************************************/
778 /* set_audio_format() sets the format of the audio data in host memory for
779 this pipe. Note that _MS_ (mono-to-stereo) playback modes are not used by ALSA
780 but they are here because they are just mono while capturing */
781 static void set_audio_format(struct echoaudio
*chip
, u16 pipe_index
,
782 const struct audioformat
*format
)
786 dsp_format
= DSP_AUDIOFORM_SS_16LE
;
788 /* Look for super-interleave (no big-endian and 8 bits) */
789 if (format
->interleave
> 2) {
790 switch (format
->bits_per_sample
) {
792 dsp_format
= DSP_AUDIOFORM_SUPER_INTERLEAVE_16LE
;
795 dsp_format
= DSP_AUDIOFORM_SUPER_INTERLEAVE_24LE
;
798 dsp_format
= DSP_AUDIOFORM_SUPER_INTERLEAVE_32LE
;
801 dsp_format
|= format
->interleave
;
802 } else if (format
->data_are_bigendian
) {
803 /* For big-endian data, only 32 bit samples are supported */
804 switch (format
->interleave
) {
806 dsp_format
= DSP_AUDIOFORM_MM_32BE
;
808 #ifdef ECHOCARD_HAS_STEREO_BIG_ENDIAN32
810 dsp_format
= DSP_AUDIOFORM_SS_32BE
;
814 } else if (format
->interleave
== 1 &&
815 format
->bits_per_sample
== 32 && !format
->mono_to_stereo
) {
816 /* 32 bit little-endian mono->mono case */
817 dsp_format
= DSP_AUDIOFORM_MM_32LE
;
819 /* Handle the other little-endian formats */
820 switch (format
->bits_per_sample
) {
822 if (format
->interleave
== 2)
823 dsp_format
= DSP_AUDIOFORM_SS_8
;
825 dsp_format
= DSP_AUDIOFORM_MS_8
;
829 if (format
->interleave
== 2)
830 dsp_format
= DSP_AUDIOFORM_SS_16LE
;
832 dsp_format
= DSP_AUDIOFORM_MS_16LE
;
835 if (format
->interleave
== 2)
836 dsp_format
= DSP_AUDIOFORM_SS_24LE
;
838 dsp_format
= DSP_AUDIOFORM_MS_24LE
;
841 if (format
->interleave
== 2)
842 dsp_format
= DSP_AUDIOFORM_SS_32LE
;
844 dsp_format
= DSP_AUDIOFORM_MS_32LE
;
848 dev_dbg(chip
->card
->dev
,
849 "set_audio_format[%d] = %x\n", pipe_index
, dsp_format
);
850 chip
->comm_page
->audio_format
[pipe_index
] = cpu_to_le16(dsp_format
);
855 /* start_transport starts transport for a set of pipes.
856 The bits 1 in channel_mask specify what pipes to start. Only the bit of the
857 first channel must be set, regardless its interleave.
858 Same thing for pause_ and stop_ -trasport below. */
859 static int start_transport(struct echoaudio
*chip
, u32 channel_mask
,
863 if (wait_handshake(chip
))
866 chip
->comm_page
->cmd_start
|= cpu_to_le32(channel_mask
);
868 if (chip
->comm_page
->cmd_start
) {
869 clear_handshake(chip
);
870 send_vector(chip
, DSP_VC_START_TRANSFER
);
871 if (wait_handshake(chip
))
873 /* Keep track of which pipes are transporting */
874 chip
->active_mask
|= channel_mask
;
875 chip
->comm_page
->cmd_start
= 0;
879 dev_err(chip
->card
->dev
, "start_transport: No pipes to start!\n");
885 static int pause_transport(struct echoaudio
*chip
, u32 channel_mask
)
888 if (wait_handshake(chip
))
891 chip
->comm_page
->cmd_stop
|= cpu_to_le32(channel_mask
);
892 chip
->comm_page
->cmd_reset
= 0;
893 if (chip
->comm_page
->cmd_stop
) {
894 clear_handshake(chip
);
895 send_vector(chip
, DSP_VC_STOP_TRANSFER
);
896 if (wait_handshake(chip
))
898 /* Keep track of which pipes are transporting */
899 chip
->active_mask
&= ~channel_mask
;
900 chip
->comm_page
->cmd_stop
= 0;
901 chip
->comm_page
->cmd_reset
= 0;
905 dev_dbg(chip
->card
->dev
, "pause_transport: No pipes to stop!\n");
911 static int stop_transport(struct echoaudio
*chip
, u32 channel_mask
)
914 if (wait_handshake(chip
))
917 chip
->comm_page
->cmd_stop
|= cpu_to_le32(channel_mask
);
918 chip
->comm_page
->cmd_reset
|= cpu_to_le32(channel_mask
);
919 if (chip
->comm_page
->cmd_reset
) {
920 clear_handshake(chip
);
921 send_vector(chip
, DSP_VC_STOP_TRANSFER
);
922 if (wait_handshake(chip
))
924 /* Keep track of which pipes are transporting */
925 chip
->active_mask
&= ~channel_mask
;
926 chip
->comm_page
->cmd_stop
= 0;
927 chip
->comm_page
->cmd_reset
= 0;
931 dev_dbg(chip
->card
->dev
, "stop_transport: No pipes to stop!\n");
937 static inline int is_pipe_allocated(struct echoaudio
*chip
, u16 pipe_index
)
939 return (chip
->pipe_alloc_mask
& (1 << pipe_index
));
944 /* Stops everything and turns off the DSP. All pipes should be already
945 stopped and unallocated. */
946 static int rest_in_peace(struct echoaudio
*chip
)
949 /* Stops all active pipes (just to be sure) */
950 stop_transport(chip
, chip
->active_mask
);
952 set_meters_on(chip
, false);
954 #ifdef ECHOCARD_HAS_MIDI
955 enable_midi_input(chip
, false);
959 if (chip
->dsp_code
) {
960 /* Make load_firmware do a complete reload */
961 chip
->dsp_code
= NULL
;
962 /* Put the DSP to sleep */
963 return send_vector(chip
, DSP_VC_GO_COMATOSE
);
970 /* Fills the comm page with default values */
971 static int init_dsp_comm_page(struct echoaudio
*chip
)
973 /* Check if the compiler added extra padding inside the structure */
974 if (offsetof(struct comm_page
, midi_output
) != 0xbe0) {
975 dev_err(chip
->card
->dev
,
976 "init_dsp_comm_page() - Invalid struct comm_page structure\n");
980 /* Init all the basic stuff */
981 chip
->card_name
= ECHOCARD_NAME
;
982 chip
->bad_board
= true; /* Set true until DSP loaded */
983 chip
->dsp_code
= NULL
; /* Current DSP code not loaded */
984 chip
->asic_loaded
= false;
985 memset(chip
->comm_page
, 0, sizeof(struct comm_page
));
987 /* Init the comm page */
988 chip
->comm_page
->comm_size
=
989 cpu_to_le32(sizeof(struct comm_page
));
990 chip
->comm_page
->handshake
= cpu_to_le32(0xffffffff);
991 chip
->comm_page
->midi_out_free_count
=
992 cpu_to_le32(DSP_MIDI_OUT_FIFO_SIZE
);
993 chip
->comm_page
->sample_rate
= cpu_to_le32(44100);
995 /* Set line levels so we don't blast any inputs on startup */
996 memset(chip
->comm_page
->monitors
, ECHOGAIN_MUTED
, MONITOR_ARRAY_SIZE
);
997 memset(chip
->comm_page
->vmixer
, ECHOGAIN_MUTED
, VMIXER_ARRAY_SIZE
);
1004 /* This function initializes the chip structure with default values, ie. all
1005 * muted and internal clock source. Then it copies the settings to the DSP.
1006 * This MUST be called after the DSP is up and running !
1008 static int init_line_levels(struct echoaudio
*chip
)
1010 memset(chip
->output_gain
, ECHOGAIN_MUTED
, sizeof(chip
->output_gain
));
1011 memset(chip
->input_gain
, ECHOGAIN_MUTED
, sizeof(chip
->input_gain
));
1012 memset(chip
->monitor_gain
, ECHOGAIN_MUTED
, sizeof(chip
->monitor_gain
));
1013 memset(chip
->vmixer_gain
, ECHOGAIN_MUTED
, sizeof(chip
->vmixer_gain
));
1014 chip
->input_clock
= ECHO_CLOCK_INTERNAL
;
1015 chip
->output_clock
= ECHO_CLOCK_WORD
;
1016 chip
->sample_rate
= 44100;
1017 return restore_dsp_rettings(chip
);
1022 /* This is low level part of the interrupt handler.
1023 It returns -1 if the IRQ is not ours, or N>=0 if it is, where N is the number
1024 of midi data in the input queue. */
1025 static int service_irq(struct echoaudio
*chip
)
1029 /* Read the DSP status register and see if this DSP generated this interrupt */
1030 if (get_dsp_register(chip
, CHI32_STATUS_REG
) & CHI32_STATUS_IRQ
) {
1032 #ifdef ECHOCARD_HAS_MIDI
1033 /* Get and parse midi data if present */
1034 if (chip
->comm_page
->midi_input
[0]) /* The count is at index 0 */
1035 st
= midi_service_irq(chip
); /* Returns how many midi bytes we received */
1037 /* Clear the hardware interrupt */
1038 chip
->comm_page
->midi_input
[0] = 0;
1039 send_vector(chip
, DSP_VC_ACK_INT
);
1048 /******************************************************************************
1049 Functions for opening and closing pipes
1050 ******************************************************************************/
1052 /* allocate_pipes is used to reserve audio pipes for your exclusive use.
1053 The call will fail if some pipes are already allocated. */
1054 static int allocate_pipes(struct echoaudio
*chip
, struct audiopipe
*pipe
,
1055 int pipe_index
, int interleave
)
1060 dev_dbg(chip
->card
->dev
,
1061 "allocate_pipes: ch=%d int=%d\n", pipe_index
, interleave
);
1063 if (chip
->bad_board
)
1066 for (channel_mask
= i
= 0; i
< interleave
; i
++)
1067 channel_mask
|= 1 << (pipe_index
+ i
);
1068 if (chip
->pipe_alloc_mask
& channel_mask
) {
1069 dev_err(chip
->card
->dev
,
1070 "allocate_pipes: channel already open\n");
1074 chip
->comm_page
->position
[pipe_index
] = 0;
1075 chip
->pipe_alloc_mask
|= channel_mask
;
1076 /* This driver uses cyclic buffers only */
1077 chip
->pipe_cyclic_mask
|= channel_mask
;
1078 pipe
->index
= pipe_index
;
1079 pipe
->interleave
= interleave
;
1080 pipe
->state
= PIPE_STATE_STOPPED
;
1082 /* The counter register is where the DSP writes the 32 bit DMA
1083 position for a pipe. The DSP is constantly updating this value as
1084 it moves data. The DMA counter is in units of bytes, not samples. */
1085 pipe
->dma_counter
= (__le32
*)&chip
->comm_page
->position
[pipe_index
];
1086 *pipe
->dma_counter
= 0;
1092 static int free_pipes(struct echoaudio
*chip
, struct audiopipe
*pipe
)
1097 if (snd_BUG_ON(!is_pipe_allocated(chip
, pipe
->index
)))
1099 if (snd_BUG_ON(pipe
->state
!= PIPE_STATE_STOPPED
))
1102 for (channel_mask
= i
= 0; i
< pipe
->interleave
; i
++)
1103 channel_mask
|= 1 << (pipe
->index
+ i
);
1105 chip
->pipe_alloc_mask
&= ~channel_mask
;
1106 chip
->pipe_cyclic_mask
&= ~channel_mask
;
1112 /******************************************************************************
1113 Functions for managing the scatter-gather list
1114 ******************************************************************************/
1116 static int sglist_init(struct echoaudio
*chip
, struct audiopipe
*pipe
)
1118 pipe
->sglist_head
= 0;
1119 memset(pipe
->sgpage
.area
, 0, PAGE_SIZE
);
1120 chip
->comm_page
->sglist_addr
[pipe
->index
].addr
=
1121 cpu_to_le32(pipe
->sgpage
.addr
);
1127 static int sglist_add_mapping(struct echoaudio
*chip
, struct audiopipe
*pipe
,
1128 dma_addr_t address
, size_t length
)
1130 int head
= pipe
->sglist_head
;
1131 struct sg_entry
*list
= (struct sg_entry
*)pipe
->sgpage
.area
;
1133 if (head
< MAX_SGLIST_ENTRIES
- 1) {
1134 list
[head
].addr
= cpu_to_le32(address
);
1135 list
[head
].size
= cpu_to_le32(length
);
1136 pipe
->sglist_head
++;
1138 dev_err(chip
->card
->dev
, "SGlist: too many fragments\n");
1146 static inline int sglist_add_irq(struct echoaudio
*chip
, struct audiopipe
*pipe
)
1148 return sglist_add_mapping(chip
, pipe
, 0, 0);
1153 static inline int sglist_wrap(struct echoaudio
*chip
, struct audiopipe
*pipe
)
1155 return sglist_add_mapping(chip
, pipe
, pipe
->sgpage
.addr
, 0);