Path: blob/master/sound/pci/echoaudio/echoaudio_dsp.c
10817 views
/****************************************************************************12Copyright Echo Digital Audio Corporation (c) 1998 - 20043All rights reserved4www.echoaudio.com56This file is part of Echo Digital Audio's generic driver library.78Echo Digital Audio's generic driver library is free software;9you can redistribute it and/or modify it under the terms of10the GNU General Public License as published by the Free Software11Foundation.1213This program is distributed in the hope that it will be useful,14but WITHOUT ANY WARRANTY; without even the implied warranty of15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the16GNU General Public License for more details.1718You should have received a copy of the GNU General Public License19along with this program; if not, write to the Free Software20Foundation, Inc., 59 Temple Place - Suite 330, Boston,21MA 02111-1307, USA.2223*************************************************************************2425Translation from C++ and adaptation for use in ALSA-Driver26were made by Giuliano Pochini <[email protected]>2728****************************************************************************/2930#if PAGE_SIZE < 409631#error PAGE_SIZE is < 4k32#endif3334static int restore_dsp_rettings(struct echoaudio *chip);353637/* Some vector commands involve the DSP reading or writing data to and from the38comm page; if you send one of these commands to the DSP, it will complete the39command and then write a non-zero value to the Handshake field in the40comm page. This function waits for the handshake to show up. */41static int wait_handshake(struct echoaudio *chip)42{43int i;4445/* Wait up to 20ms for the handshake from the DSP */46for (i = 0; i < HANDSHAKE_TIMEOUT; i++) {47/* Look for the handshake value */48barrier();49if (chip->comm_page->handshake) {50return 0;51}52udelay(1);53}5455snd_printk(KERN_ERR "wait_handshake(): Timeout waiting for DSP\n");56return -EBUSY;57}58596061/* Much of the interaction between the DSP and the driver is done via vector62commands; send_vector writes a vector command to the DSP. Typically, this63causes the DSP to read or write fields in the comm page.64PCI posting is not required thanks to the handshake logic. */65static int send_vector(struct echoaudio *chip, u32 command)66{67int i;6869wmb(); /* Flush all pending writes before sending the command */7071/* Wait up to 100ms for the "vector busy" bit to be off */72for (i = 0; i < VECTOR_BUSY_TIMEOUT; i++) {73if (!(get_dsp_register(chip, CHI32_VECTOR_REG) &74CHI32_VECTOR_BUSY)) {75set_dsp_register(chip, CHI32_VECTOR_REG, command);76/*if (i) DE_ACT(("send_vector time: %d\n", i));*/77return 0;78}79udelay(1);80}8182DE_ACT((KERN_ERR "timeout on send_vector\n"));83return -EBUSY;84}85868788/* write_dsp writes a 32-bit value to the DSP; this is used almost89exclusively for loading the DSP. */90static int write_dsp(struct echoaudio *chip, u32 data)91{92u32 status, i;9394for (i = 0; i < 10000000; i++) { /* timeout = 10s */95status = get_dsp_register(chip, CHI32_STATUS_REG);96if ((status & CHI32_STATUS_HOST_WRITE_EMPTY) != 0) {97set_dsp_register(chip, CHI32_DATA_REG, data);98wmb(); /* write it immediately */99return 0;100}101udelay(1);102cond_resched();103}104105chip->bad_board = TRUE; /* Set TRUE until DSP re-loaded */106DE_ACT((KERN_ERR "write_dsp: Set bad_board to TRUE\n"));107return -EIO;108}109110111112/* read_dsp reads a 32-bit value from the DSP; this is used almost113exclusively for loading the DSP and checking the status of the ASIC. */114static int read_dsp(struct echoaudio *chip, u32 *data)115{116u32 status, i;117118for (i = 0; i < READ_DSP_TIMEOUT; i++) {119status = get_dsp_register(chip, CHI32_STATUS_REG);120if ((status & CHI32_STATUS_HOST_READ_FULL) != 0) {121*data = get_dsp_register(chip, CHI32_DATA_REG);122return 0;123}124udelay(1);125cond_resched();126}127128chip->bad_board = TRUE; /* Set TRUE until DSP re-loaded */129DE_INIT((KERN_ERR "read_dsp: Set bad_board to TRUE\n"));130return -EIO;131}132133134135/****************************************************************************136Firmware loading functions137****************************************************************************/138139/* This function is used to read back the serial number from the DSP;140this is triggered by the SET_COMMPAGE_ADDR command.141Only some early Echogals products have serial numbers in the ROM;142the serial number is not used, but you still need to do this as143part of the DSP load process. */144static int read_sn(struct echoaudio *chip)145{146int i;147u32 sn[6];148149for (i = 0; i < 5; i++) {150if (read_dsp(chip, &sn[i])) {151snd_printk(KERN_ERR "Failed to read serial number\n");152return -EIO;153}154}155DE_INIT(("Read serial number %08x %08x %08x %08x %08x\n",156sn[0], sn[1], sn[2], sn[3], sn[4]));157return 0;158}159160161162#ifndef ECHOCARD_HAS_ASIC163/* This card has no ASIC, just return ok */164static inline int check_asic_status(struct echoaudio *chip)165{166chip->asic_loaded = TRUE;167return 0;168}169170#endif /* !ECHOCARD_HAS_ASIC */171172173174#ifdef ECHOCARD_HAS_ASIC175176/* Load ASIC code - done after the DSP is loaded */177static int load_asic_generic(struct echoaudio *chip, u32 cmd, short asic)178{179const struct firmware *fw;180int err;181u32 i, size;182u8 *code;183184err = get_firmware(&fw, chip, asic);185if (err < 0) {186snd_printk(KERN_WARNING "Firmware not found !\n");187return err;188}189190code = (u8 *)fw->data;191size = fw->size;192193/* Send the "Here comes the ASIC" command */194if (write_dsp(chip, cmd) < 0)195goto la_error;196197/* Write length of ASIC file in bytes */198if (write_dsp(chip, size) < 0)199goto la_error;200201for (i = 0; i < size; i++) {202if (write_dsp(chip, code[i]) < 0)203goto la_error;204}205206DE_INIT(("ASIC loaded\n"));207free_firmware(fw);208return 0;209210la_error:211DE_INIT(("failed on write_dsp\n"));212free_firmware(fw);213return -EIO;214}215216#endif /* ECHOCARD_HAS_ASIC */217218219220#ifdef DSP_56361221222/* Install the resident loader for 56361 DSPs; The resident loader is on223the EPROM on the board for 56301 DSP. The resident loader is a tiny little224program that is used to load the real DSP code. */225static int install_resident_loader(struct echoaudio *chip)226{227u32 address;228int index, words, i;229u16 *code;230u32 status;231const struct firmware *fw;232233/* 56361 cards only! This check is required by the old 56301-based234Mona and Gina24 */235if (chip->device_id != DEVICE_ID_56361)236return 0;237238/* Look to see if the resident loader is present. If the resident239loader is already installed, host flag 5 will be on. */240status = get_dsp_register(chip, CHI32_STATUS_REG);241if (status & CHI32_STATUS_REG_HF5) {242DE_INIT(("Resident loader already installed; status is 0x%x\n",243status));244return 0;245}246247i = get_firmware(&fw, chip, FW_361_LOADER);248if (i < 0) {249snd_printk(KERN_WARNING "Firmware not found !\n");250return i;251}252253/* The DSP code is an array of 16 bit words. The array is divided up254into sections. The first word of each section is the size in words,255followed by the section type.256Since DSP addresses and data are 24 bits wide, they each take up two25716 bit words in the array.258This is a lot like the other loader loop, but it's not a loop, you259don't write the memory type, and you don't write a zero at the end. */260261/* Set DSP format bits for 24 bit mode */262set_dsp_register(chip, CHI32_CONTROL_REG,263get_dsp_register(chip, CHI32_CONTROL_REG) | 0x900);264265code = (u16 *)fw->data;266267/* Skip the header section; the first word in the array is the size268of the first section, so the first real section of code is pointed269to by Code[0]. */270index = code[0];271272/* Skip the section size, LRS block type, and DSP memory type */273index += 3;274275/* Get the number of DSP words to write */276words = code[index++];277278/* Get the DSP address for this block; 24 bits, so build from two words */279address = ((u32)code[index] << 16) + code[index + 1];280index += 2;281282/* Write the count to the DSP */283if (write_dsp(chip, words)) {284DE_INIT(("install_resident_loader: Failed to write word count!\n"));285goto irl_error;286}287/* Write the DSP address */288if (write_dsp(chip, address)) {289DE_INIT(("install_resident_loader: Failed to write DSP address!\n"));290goto irl_error;291}292/* Write out this block of code to the DSP */293for (i = 0; i < words; i++) {294u32 data;295296data = ((u32)code[index] << 16) + code[index + 1];297if (write_dsp(chip, data)) {298DE_INIT(("install_resident_loader: Failed to write DSP code\n"));299goto irl_error;300}301index += 2;302}303304/* Wait for flag 5 to come up */305for (i = 0; i < 200; i++) { /* Timeout is 50us * 200 = 10ms */306udelay(50);307status = get_dsp_register(chip, CHI32_STATUS_REG);308if (status & CHI32_STATUS_REG_HF5)309break;310}311312if (i == 200) {313DE_INIT(("Resident loader failed to set HF5\n"));314goto irl_error;315}316317DE_INIT(("Resident loader successfully installed\n"));318free_firmware(fw);319return 0;320321irl_error:322free_firmware(fw);323return -EIO;324}325326#endif /* DSP_56361 */327328329static int load_dsp(struct echoaudio *chip, u16 *code)330{331u32 address, data;332int index, words, i;333334if (chip->dsp_code == code) {335DE_INIT(("DSP is already loaded!\n"));336return 0;337}338chip->bad_board = TRUE; /* Set TRUE until DSP loaded */339chip->dsp_code = NULL; /* Current DSP code not loaded */340chip->asic_loaded = FALSE; /* Loading the DSP code will reset the ASIC */341342DE_INIT(("load_dsp: Set bad_board to TRUE\n"));343344/* If this board requires a resident loader, install it. */345#ifdef DSP_56361346if ((i = install_resident_loader(chip)) < 0)347return i;348#endif349350/* Send software reset command */351if (send_vector(chip, DSP_VC_RESET) < 0) {352DE_INIT(("LoadDsp: send_vector DSP_VC_RESET failed, Critical Failure\n"));353return -EIO;354}355/* Delay 10us */356udelay(10);357358/* Wait 10ms for HF3 to indicate that software reset is complete */359for (i = 0; i < 1000; i++) { /* Timeout is 10us * 1000 = 10ms */360if (get_dsp_register(chip, CHI32_STATUS_REG) &361CHI32_STATUS_REG_HF3)362break;363udelay(10);364}365366if (i == 1000) {367DE_INIT(("load_dsp: Timeout waiting for CHI32_STATUS_REG_HF3\n"));368return -EIO;369}370371/* Set DSP format bits for 24 bit mode now that soft reset is done */372set_dsp_register(chip, CHI32_CONTROL_REG,373get_dsp_register(chip, CHI32_CONTROL_REG) | 0x900);374375/* Main loader loop */376377index = code[0];378for (;;) {379int block_type, mem_type;380381/* Total Block Size */382index++;383384/* Block Type */385block_type = code[index];386if (block_type == 4) /* We're finished */387break;388389index++;390391/* Memory Type P=0,X=1,Y=2 */392mem_type = code[index++];393394/* Block Code Size */395words = code[index++];396if (words == 0) /* We're finished */397break;398399/* Start Address */400address = ((u32)code[index] << 16) + code[index + 1];401index += 2;402403if (write_dsp(chip, words) < 0) {404DE_INIT(("load_dsp: failed to write number of DSP words\n"));405return -EIO;406}407if (write_dsp(chip, address) < 0) {408DE_INIT(("load_dsp: failed to write DSP address\n"));409return -EIO;410}411if (write_dsp(chip, mem_type) < 0) {412DE_INIT(("load_dsp: failed to write DSP memory type\n"));413return -EIO;414}415/* Code */416for (i = 0; i < words; i++, index+=2) {417data = ((u32)code[index] << 16) + code[index + 1];418if (write_dsp(chip, data) < 0) {419DE_INIT(("load_dsp: failed to write DSP data\n"));420return -EIO;421}422}423}424425if (write_dsp(chip, 0) < 0) { /* We're done!!! */426DE_INIT(("load_dsp: Failed to write final zero\n"));427return -EIO;428}429udelay(10);430431for (i = 0; i < 5000; i++) { /* Timeout is 100us * 5000 = 500ms */432/* Wait for flag 4 - indicates that the DSP loaded OK */433if (get_dsp_register(chip, CHI32_STATUS_REG) &434CHI32_STATUS_REG_HF4) {435set_dsp_register(chip, CHI32_CONTROL_REG,436get_dsp_register(chip, CHI32_CONTROL_REG) & ~0x1b00);437438if (write_dsp(chip, DSP_FNC_SET_COMMPAGE_ADDR) < 0) {439DE_INIT(("load_dsp: Failed to write DSP_FNC_SET_COMMPAGE_ADDR\n"));440return -EIO;441}442443if (write_dsp(chip, chip->comm_page_phys) < 0) {444DE_INIT(("load_dsp: Failed to write comm page address\n"));445return -EIO;446}447448/* Get the serial number via slave mode.449This is triggered by the SET_COMMPAGE_ADDR command.450We don't actually use the serial number but we have to451get it as part of the DSP init voodoo. */452if (read_sn(chip) < 0) {453DE_INIT(("load_dsp: Failed to read serial number\n"));454return -EIO;455}456457chip->dsp_code = code; /* Show which DSP code loaded */458chip->bad_board = FALSE; /* DSP OK */459DE_INIT(("load_dsp: OK!\n"));460return 0;461}462udelay(100);463}464465DE_INIT(("load_dsp: DSP load timed out waiting for HF4\n"));466return -EIO;467}468469470471/* load_firmware takes care of loading the DSP and any ASIC code. */472static int load_firmware(struct echoaudio *chip)473{474const struct firmware *fw;475int box_type, err;476477if (snd_BUG_ON(!chip->dsp_code_to_load || !chip->comm_page))478return -EPERM;479480/* See if the ASIC is present and working - only if the DSP is already loaded */481if (chip->dsp_code) {482if ((box_type = check_asic_status(chip)) >= 0)483return box_type;484/* ASIC check failed; force the DSP to reload */485chip->dsp_code = NULL;486}487488err = get_firmware(&fw, chip, chip->dsp_code_to_load);489if (err < 0)490return err;491err = load_dsp(chip, (u16 *)fw->data);492free_firmware(fw);493if (err < 0)494return err;495496if ((box_type = load_asic(chip)) < 0)497return box_type; /* error */498499return box_type;500}501502503504/****************************************************************************505Mixer functions506****************************************************************************/507508#if defined(ECHOCARD_HAS_INPUT_NOMINAL_LEVEL) || \509defined(ECHOCARD_HAS_OUTPUT_NOMINAL_LEVEL)510511/* Set the nominal level for an input or output bus (true = -10dBV, false = +4dBu) */512static int set_nominal_level(struct echoaudio *chip, u16 index, char consumer)513{514if (snd_BUG_ON(index >= num_busses_out(chip) + num_busses_in(chip)))515return -EINVAL;516517/* Wait for the handshake (OK even if ASIC is not loaded) */518if (wait_handshake(chip))519return -EIO;520521chip->nominal_level[index] = consumer;522523if (consumer)524chip->comm_page->nominal_level_mask |= cpu_to_le32(1 << index);525else526chip->comm_page->nominal_level_mask &= ~cpu_to_le32(1 << index);527528return 0;529}530531#endif /* ECHOCARD_HAS_*_NOMINAL_LEVEL */532533534535/* Set the gain for a single physical output channel (dB). */536static int set_output_gain(struct echoaudio *chip, u16 channel, s8 gain)537{538if (snd_BUG_ON(channel >= num_busses_out(chip)))539return -EINVAL;540541if (wait_handshake(chip))542return -EIO;543544/* Save the new value */545chip->output_gain[channel] = gain;546chip->comm_page->line_out_level[channel] = gain;547return 0;548}549550551552#ifdef ECHOCARD_HAS_MONITOR553/* Set the monitor level from an input bus to an output bus. */554static int set_monitor_gain(struct echoaudio *chip, u16 output, u16 input,555s8 gain)556{557if (snd_BUG_ON(output >= num_busses_out(chip) ||558input >= num_busses_in(chip)))559return -EINVAL;560561if (wait_handshake(chip))562return -EIO;563564chip->monitor_gain[output][input] = gain;565chip->comm_page->monitors[monitor_index(chip, output, input)] = gain;566return 0;567}568#endif /* ECHOCARD_HAS_MONITOR */569570571/* Tell the DSP to read and update output, nominal & monitor levels in comm page. */572static int update_output_line_level(struct echoaudio *chip)573{574if (wait_handshake(chip))575return -EIO;576clear_handshake(chip);577return send_vector(chip, DSP_VC_UPDATE_OUTVOL);578}579580581582/* Tell the DSP to read and update input levels in comm page */583static int update_input_line_level(struct echoaudio *chip)584{585if (wait_handshake(chip))586return -EIO;587clear_handshake(chip);588return send_vector(chip, DSP_VC_UPDATE_INGAIN);589}590591592593/* set_meters_on turns the meters on or off. If meters are turned on, the DSP594will write the meter and clock detect values to the comm page at about 30Hz */595static void set_meters_on(struct echoaudio *chip, char on)596{597if (on && !chip->meters_enabled) {598send_vector(chip, DSP_VC_METERS_ON);599chip->meters_enabled = 1;600} else if (!on && chip->meters_enabled) {601send_vector(chip, DSP_VC_METERS_OFF);602chip->meters_enabled = 0;603memset((s8 *)chip->comm_page->vu_meter, ECHOGAIN_MUTED,604DSP_MAXPIPES);605memset((s8 *)chip->comm_page->peak_meter, ECHOGAIN_MUTED,606DSP_MAXPIPES);607}608}609610611612/* Fill out an the given array using the current values in the comm page.613Meters are written in the comm page by the DSP in this order:614Output busses615Input busses616Output pipes (vmixer cards only)617618This function assumes there are no more than 16 in/out busses or pipes619Meters is an array [3][16][2] of long. */620static void get_audio_meters(struct echoaudio *chip, long *meters)621{622int i, m, n;623624m = 0;625n = 0;626for (i = 0; i < num_busses_out(chip); i++, m++) {627meters[n++] = chip->comm_page->vu_meter[m];628meters[n++] = chip->comm_page->peak_meter[m];629}630for (; n < 32; n++)631meters[n] = 0;632633#ifdef ECHOCARD_ECHO3G634m = E3G_MAX_OUTPUTS; /* Skip unused meters */635#endif636637for (i = 0; i < num_busses_in(chip); i++, m++) {638meters[n++] = chip->comm_page->vu_meter[m];639meters[n++] = chip->comm_page->peak_meter[m];640}641for (; n < 64; n++)642meters[n] = 0;643644#ifdef ECHOCARD_HAS_VMIXER645for (i = 0; i < num_pipes_out(chip); i++, m++) {646meters[n++] = chip->comm_page->vu_meter[m];647meters[n++] = chip->comm_page->peak_meter[m];648}649#endif650for (; n < 96; n++)651meters[n] = 0;652}653654655656static int restore_dsp_rettings(struct echoaudio *chip)657{658int i, o, err;659DE_INIT(("restore_dsp_settings\n"));660661if ((err = check_asic_status(chip)) < 0)662return err;663664/* Gina20/Darla20 only. Should be harmless for other cards. */665chip->comm_page->gd_clock_state = GD_CLOCK_UNDEF;666chip->comm_page->gd_spdif_status = GD_SPDIF_STATUS_UNDEF;667chip->comm_page->handshake = 0xffffffff;668669/* Restore output busses */670for (i = 0; i < num_busses_out(chip); i++) {671err = set_output_gain(chip, i, chip->output_gain[i]);672if (err < 0)673return err;674}675676#ifdef ECHOCARD_HAS_VMIXER677for (i = 0; i < num_pipes_out(chip); i++)678for (o = 0; o < num_busses_out(chip); o++) {679err = set_vmixer_gain(chip, o, i,680chip->vmixer_gain[o][i]);681if (err < 0)682return err;683}684if (update_vmixer_level(chip) < 0)685return -EIO;686#endif /* ECHOCARD_HAS_VMIXER */687688#ifdef ECHOCARD_HAS_MONITOR689for (o = 0; o < num_busses_out(chip); o++)690for (i = 0; i < num_busses_in(chip); i++) {691err = set_monitor_gain(chip, o, i,692chip->monitor_gain[o][i]);693if (err < 0)694return err;695}696#endif /* ECHOCARD_HAS_MONITOR */697698#ifdef ECHOCARD_HAS_INPUT_GAIN699for (i = 0; i < num_busses_in(chip); i++) {700err = set_input_gain(chip, i, chip->input_gain[i]);701if (err < 0)702return err;703}704#endif /* ECHOCARD_HAS_INPUT_GAIN */705706err = update_output_line_level(chip);707if (err < 0)708return err;709710err = update_input_line_level(chip);711if (err < 0)712return err;713714err = set_sample_rate(chip, chip->sample_rate);715if (err < 0)716return err;717718if (chip->meters_enabled) {719err = send_vector(chip, DSP_VC_METERS_ON);720if (err < 0)721return err;722}723724#ifdef ECHOCARD_HAS_DIGITAL_MODE_SWITCH725if (set_digital_mode(chip, chip->digital_mode) < 0)726return -EIO;727#endif728729#ifdef ECHOCARD_HAS_DIGITAL_IO730if (set_professional_spdif(chip, chip->professional_spdif) < 0)731return -EIO;732#endif733734#ifdef ECHOCARD_HAS_PHANTOM_POWER735if (set_phantom_power(chip, chip->phantom_power) < 0)736return -EIO;737#endif738739#ifdef ECHOCARD_HAS_EXTERNAL_CLOCK740/* set_input_clock() also restores automute setting */741if (set_input_clock(chip, chip->input_clock) < 0)742return -EIO;743#endif744745#ifdef ECHOCARD_HAS_OUTPUT_CLOCK_SWITCH746if (set_output_clock(chip, chip->output_clock) < 0)747return -EIO;748#endif749750if (wait_handshake(chip) < 0)751return -EIO;752clear_handshake(chip);753if (send_vector(chip, DSP_VC_UPDATE_FLAGS) < 0)754return -EIO;755756DE_INIT(("restore_dsp_rettings done\n"));757return 0;758}759760761762/****************************************************************************763Transport functions764****************************************************************************/765766/* set_audio_format() sets the format of the audio data in host memory for767this pipe. Note that _MS_ (mono-to-stereo) playback modes are not used by ALSA768but they are here because they are just mono while capturing */769static void set_audio_format(struct echoaudio *chip, u16 pipe_index,770const struct audioformat *format)771{772u16 dsp_format;773774dsp_format = DSP_AUDIOFORM_SS_16LE;775776/* Look for super-interleave (no big-endian and 8 bits) */777if (format->interleave > 2) {778switch (format->bits_per_sample) {779case 16:780dsp_format = DSP_AUDIOFORM_SUPER_INTERLEAVE_16LE;781break;782case 24:783dsp_format = DSP_AUDIOFORM_SUPER_INTERLEAVE_24LE;784break;785case 32:786dsp_format = DSP_AUDIOFORM_SUPER_INTERLEAVE_32LE;787break;788}789dsp_format |= format->interleave;790} else if (format->data_are_bigendian) {791/* For big-endian data, only 32 bit samples are supported */792switch (format->interleave) {793case 1:794dsp_format = DSP_AUDIOFORM_MM_32BE;795break;796#ifdef ECHOCARD_HAS_STEREO_BIG_ENDIAN32797case 2:798dsp_format = DSP_AUDIOFORM_SS_32BE;799break;800#endif801}802} else if (format->interleave == 1 &&803format->bits_per_sample == 32 && !format->mono_to_stereo) {804/* 32 bit little-endian mono->mono case */805dsp_format = DSP_AUDIOFORM_MM_32LE;806} else {807/* Handle the other little-endian formats */808switch (format->bits_per_sample) {809case 8:810if (format->interleave == 2)811dsp_format = DSP_AUDIOFORM_SS_8;812else813dsp_format = DSP_AUDIOFORM_MS_8;814break;815default:816case 16:817if (format->interleave == 2)818dsp_format = DSP_AUDIOFORM_SS_16LE;819else820dsp_format = DSP_AUDIOFORM_MS_16LE;821break;822case 24:823if (format->interleave == 2)824dsp_format = DSP_AUDIOFORM_SS_24LE;825else826dsp_format = DSP_AUDIOFORM_MS_24LE;827break;828case 32:829if (format->interleave == 2)830dsp_format = DSP_AUDIOFORM_SS_32LE;831else832dsp_format = DSP_AUDIOFORM_MS_32LE;833break;834}835}836DE_ACT(("set_audio_format[%d] = %x\n", pipe_index, dsp_format));837chip->comm_page->audio_format[pipe_index] = cpu_to_le16(dsp_format);838}839840841842/* start_transport starts transport for a set of pipes.843The bits 1 in channel_mask specify what pipes to start. Only the bit of the844first channel must be set, regardless its interleave.845Same thing for pause_ and stop_ -trasport below. */846static int start_transport(struct echoaudio *chip, u32 channel_mask,847u32 cyclic_mask)848{849DE_ACT(("start_transport %x\n", channel_mask));850851if (wait_handshake(chip))852return -EIO;853854chip->comm_page->cmd_start |= cpu_to_le32(channel_mask);855856if (chip->comm_page->cmd_start) {857clear_handshake(chip);858send_vector(chip, DSP_VC_START_TRANSFER);859if (wait_handshake(chip))860return -EIO;861/* Keep track of which pipes are transporting */862chip->active_mask |= channel_mask;863chip->comm_page->cmd_start = 0;864return 0;865}866867DE_ACT(("start_transport: No pipes to start!\n"));868return -EINVAL;869}870871872873static int pause_transport(struct echoaudio *chip, u32 channel_mask)874{875DE_ACT(("pause_transport %x\n", channel_mask));876877if (wait_handshake(chip))878return -EIO;879880chip->comm_page->cmd_stop |= cpu_to_le32(channel_mask);881chip->comm_page->cmd_reset = 0;882if (chip->comm_page->cmd_stop) {883clear_handshake(chip);884send_vector(chip, DSP_VC_STOP_TRANSFER);885if (wait_handshake(chip))886return -EIO;887/* Keep track of which pipes are transporting */888chip->active_mask &= ~channel_mask;889chip->comm_page->cmd_stop = 0;890chip->comm_page->cmd_reset = 0;891return 0;892}893894DE_ACT(("pause_transport: No pipes to stop!\n"));895return 0;896}897898899900static int stop_transport(struct echoaudio *chip, u32 channel_mask)901{902DE_ACT(("stop_transport %x\n", channel_mask));903904if (wait_handshake(chip))905return -EIO;906907chip->comm_page->cmd_stop |= cpu_to_le32(channel_mask);908chip->comm_page->cmd_reset |= cpu_to_le32(channel_mask);909if (chip->comm_page->cmd_reset) {910clear_handshake(chip);911send_vector(chip, DSP_VC_STOP_TRANSFER);912if (wait_handshake(chip))913return -EIO;914/* Keep track of which pipes are transporting */915chip->active_mask &= ~channel_mask;916chip->comm_page->cmd_stop = 0;917chip->comm_page->cmd_reset = 0;918return 0;919}920921DE_ACT(("stop_transport: No pipes to stop!\n"));922return 0;923}924925926927static inline int is_pipe_allocated(struct echoaudio *chip, u16 pipe_index)928{929return (chip->pipe_alloc_mask & (1 << pipe_index));930}931932933934/* Stops everything and turns off the DSP. All pipes should be already935stopped and unallocated. */936static int rest_in_peace(struct echoaudio *chip)937{938DE_ACT(("rest_in_peace() open=%x\n", chip->pipe_alloc_mask));939940/* Stops all active pipes (just to be sure) */941stop_transport(chip, chip->active_mask);942943set_meters_on(chip, FALSE);944945#ifdef ECHOCARD_HAS_MIDI946enable_midi_input(chip, FALSE);947#endif948949/* Go to sleep */950if (chip->dsp_code) {951/* Make load_firmware do a complete reload */952chip->dsp_code = NULL;953/* Put the DSP to sleep */954return send_vector(chip, DSP_VC_GO_COMATOSE);955}956return 0;957}958959960961/* Fills the comm page with default values */962static int init_dsp_comm_page(struct echoaudio *chip)963{964/* Check if the compiler added extra padding inside the structure */965if (offsetof(struct comm_page, midi_output) != 0xbe0) {966DE_INIT(("init_dsp_comm_page() - Invalid struct comm_page structure\n"));967return -EPERM;968}969970/* Init all the basic stuff */971chip->card_name = ECHOCARD_NAME;972chip->bad_board = TRUE; /* Set TRUE until DSP loaded */973chip->dsp_code = NULL; /* Current DSP code not loaded */974chip->asic_loaded = FALSE;975memset(chip->comm_page, 0, sizeof(struct comm_page));976977/* Init the comm page */978chip->comm_page->comm_size =979cpu_to_le32(sizeof(struct comm_page));980chip->comm_page->handshake = 0xffffffff;981chip->comm_page->midi_out_free_count =982cpu_to_le32(DSP_MIDI_OUT_FIFO_SIZE);983chip->comm_page->sample_rate = cpu_to_le32(44100);984985/* Set line levels so we don't blast any inputs on startup */986memset(chip->comm_page->monitors, ECHOGAIN_MUTED, MONITOR_ARRAY_SIZE);987memset(chip->comm_page->vmixer, ECHOGAIN_MUTED, VMIXER_ARRAY_SIZE);988989return 0;990}991992993994/* This function initializes the chip structure with default values, ie. all995* muted and internal clock source. Then it copies the settings to the DSP.996* This MUST be called after the DSP is up and running !997*/998static int init_line_levels(struct echoaudio *chip)999{1000DE_INIT(("init_line_levels\n"));1001memset(chip->output_gain, ECHOGAIN_MUTED, sizeof(chip->output_gain));1002memset(chip->input_gain, ECHOGAIN_MUTED, sizeof(chip->input_gain));1003memset(chip->monitor_gain, ECHOGAIN_MUTED, sizeof(chip->monitor_gain));1004memset(chip->vmixer_gain, ECHOGAIN_MUTED, sizeof(chip->vmixer_gain));1005chip->input_clock = ECHO_CLOCK_INTERNAL;1006chip->output_clock = ECHO_CLOCK_WORD;1007chip->sample_rate = 44100;1008return restore_dsp_rettings(chip);1009}1010101110121013/* This is low level part of the interrupt handler.1014It returns -1 if the IRQ is not ours, or N>=0 if it is, where N is the number1015of midi data in the input queue. */1016static int service_irq(struct echoaudio *chip)1017{1018int st;10191020/* Read the DSP status register and see if this DSP generated this interrupt */1021if (get_dsp_register(chip, CHI32_STATUS_REG) & CHI32_STATUS_IRQ) {1022st = 0;1023#ifdef ECHOCARD_HAS_MIDI1024/* Get and parse midi data if present */1025if (chip->comm_page->midi_input[0]) /* The count is at index 0 */1026st = midi_service_irq(chip); /* Returns how many midi bytes we received */1027#endif1028/* Clear the hardware interrupt */1029chip->comm_page->midi_input[0] = 0;1030send_vector(chip, DSP_VC_ACK_INT);1031return st;1032}1033return -1;1034}10351036103710381039/******************************************************************************1040Functions for opening and closing pipes1041******************************************************************************/10421043/* allocate_pipes is used to reserve audio pipes for your exclusive use.1044The call will fail if some pipes are already allocated. */1045static int allocate_pipes(struct echoaudio *chip, struct audiopipe *pipe,1046int pipe_index, int interleave)1047{1048int i;1049u32 channel_mask;1050char is_cyclic;10511052DE_ACT(("allocate_pipes: ch=%d int=%d\n", pipe_index, interleave));10531054if (chip->bad_board)1055return -EIO;10561057is_cyclic = 1; /* This driver uses cyclic buffers only */10581059for (channel_mask = i = 0; i < interleave; i++)1060channel_mask |= 1 << (pipe_index + i);1061if (chip->pipe_alloc_mask & channel_mask) {1062DE_ACT(("allocate_pipes: channel already open\n"));1063return -EAGAIN;1064}10651066chip->comm_page->position[pipe_index] = 0;1067chip->pipe_alloc_mask |= channel_mask;1068if (is_cyclic)1069chip->pipe_cyclic_mask |= channel_mask;1070pipe->index = pipe_index;1071pipe->interleave = interleave;1072pipe->state = PIPE_STATE_STOPPED;10731074/* The counter register is where the DSP writes the 32 bit DMA1075position for a pipe. The DSP is constantly updating this value as1076it moves data. The DMA counter is in units of bytes, not samples. */1077pipe->dma_counter = &chip->comm_page->position[pipe_index];1078*pipe->dma_counter = 0;1079DE_ACT(("allocate_pipes: ok\n"));1080return pipe_index;1081}1082108310841085static int free_pipes(struct echoaudio *chip, struct audiopipe *pipe)1086{1087u32 channel_mask;1088int i;10891090DE_ACT(("free_pipes: Pipe %d\n", pipe->index));1091if (snd_BUG_ON(!is_pipe_allocated(chip, pipe->index)))1092return -EINVAL;1093if (snd_BUG_ON(pipe->state != PIPE_STATE_STOPPED))1094return -EINVAL;10951096for (channel_mask = i = 0; i < pipe->interleave; i++)1097channel_mask |= 1 << (pipe->index + i);10981099chip->pipe_alloc_mask &= ~channel_mask;1100chip->pipe_cyclic_mask &= ~channel_mask;1101return 0;1102}1103110411051106/******************************************************************************1107Functions for managing the scatter-gather list1108******************************************************************************/11091110static int sglist_init(struct echoaudio *chip, struct audiopipe *pipe)1111{1112pipe->sglist_head = 0;1113memset(pipe->sgpage.area, 0, PAGE_SIZE);1114chip->comm_page->sglist_addr[pipe->index].addr =1115cpu_to_le32(pipe->sgpage.addr);1116return 0;1117}1118111911201121static int sglist_add_mapping(struct echoaudio *chip, struct audiopipe *pipe,1122dma_addr_t address, size_t length)1123{1124int head = pipe->sglist_head;1125struct sg_entry *list = (struct sg_entry *)pipe->sgpage.area;11261127if (head < MAX_SGLIST_ENTRIES - 1) {1128list[head].addr = cpu_to_le32(address);1129list[head].size = cpu_to_le32(length);1130pipe->sglist_head++;1131} else {1132DE_ACT(("SGlist: too many fragments\n"));1133return -ENOMEM;1134}1135return 0;1136}1137113811391140static inline int sglist_add_irq(struct echoaudio *chip, struct audiopipe *pipe)1141{1142return sglist_add_mapping(chip, pipe, 0, 0);1143}1144114511461147static inline int sglist_wrap(struct echoaudio *chip, struct audiopipe *pipe)1148{1149return sglist_add_mapping(chip, pipe, pipe->sgpage.addr, 0);1150}115111521153