/****************************************************************************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****************************************************************************293031Here's a block diagram of how most of the cards work:3233+-----------+34record | |<-------------------- Inputs35<-------| | |36PCI | Transport | |37bus | engine | \|/38------->| | +-------+39play | |--->|monitor|-------> Outputs40+-----------+ | mixer |41+-------+4243The lines going to and from the PCI bus represent "pipes". A pipe performs44audio transport - moving audio data to and from buffers on the host via45bus mastering.4647The inputs and outputs on the right represent input and output "busses."48A bus is a physical, real connection to the outside world. An example49of a bus would be the 1/4" analog connectors on the back of Layla or50an RCA S/PDIF connector.5152For most cards, there is a one-to-one correspondence between outputs53and busses; that is, each individual pipe is hard-wired to a single bus.5455Cards that work this way are Darla20, Gina20, Layla20, Darla24, Gina24,56Layla24, Mona, and Indigo.575859Mia has a feature called "virtual outputs."606162+-----------+63record | |<----------------------------- Inputs64<-------| | |65PCI | Transport | |66bus | engine | \|/67------->| | +------+ +-------+68play | |-->|vmixer|-->|monitor|-------> Outputs69+-----------+ +------+ | mixer |70+-------+717273Obviously, the difference here is the box labeled "vmixer." Vmixer is74short for "virtual output mixer." For Mia, pipes are *not* hard-wired75to a single bus; the vmixer lets you mix any pipe to any bus in any76combination.7778Note, however, that the left-hand side of the diagram is unchanged.79Transport works exactly the same way - the difference is in the mixer stage.808182Pipes and busses are numbered starting at zero.83848586Pipe index87==========8889A number of calls in CEchoGals refer to a "pipe index". A pipe index is90a unique number for a pipe that unambiguously refers to a playback or record91pipe. Pipe indices are numbered starting with analog outputs, followed by92digital outputs, then analog inputs, then digital inputs.9394Take Gina24 as an example:9596Pipe index97980-7 Analog outputs (0 .. FirstDigitalBusOut-1)998-15 Digital outputs (FirstDigitalBusOut .. NumBussesOut-1)10016-17 Analog inputs10118-25 Digital inputs102103104You get the pipe index by calling CEchoGals::OpenAudio; the other transport105functions take the pipe index as a parameter. If you need a pipe index for106some other reason, use the handy Makepipe_index method.107108109Some calls take a CChannelMask parameter; CChannelMask is a handy way to110group pipe indices.111112113114Digital mode switch115===================116117Some cards (right now, Gina24, Layla24, and Mona) have a Digital Mode Switch118or DMS. Cards with a DMS can be set to one of three mutually exclusive119digital modes: S/PDIF RCA, S/PDIF optical, or ADAT optical.120121This may create some confusion since ADAT optical is 8 channels wide and122S/PDIF is only two channels wide. Gina24, Layla24, and Mona handle this123by acting as if they always have 8 digital outs and ins. If you are in124either S/PDIF mode, the last 6 channels don't do anything - data sent125out these channels is thrown away and you will always record zeros.126127Note that with Gina24, Layla24, and Mona, sample rates above 50 kHz are128only available if you have the card configured for S/PDIF optical or S/PDIF129RCA.130131132133Double speed mode134=================135136Some of the cards support 88.2 kHz and 96 kHz sampling (Darla24, Gina24,137Layla24, Mona, Mia, and Indigo). For these cards, the driver sometimes has138to worry about "double speed mode"; double speed mode applies whenever the139sampling rate is above 50 kHz.140141For instance, Mona and Layla24 support word clock sync. However, they142actually support two different word clock modes - single speed (below14350 kHz) and double speed (above 50 kHz). The hardware detects if a single144or double speed word clock signal is present; the generic code uses that145information to determine which mode to use.146147The generic code takes care of all this for you.148*/149150151#ifndef _ECHOAUDIO_H_152#define _ECHOAUDIO_H_153154155#define TRUE 1156#define FALSE 0157158#include "echoaudio_dsp.h"159160161162/***********************************************************************163164PCI configuration space165166***********************************************************************/167168/*169* PCI vendor ID and device IDs for the hardware170*/171#define VENDOR_ID 0x1057172#define DEVICE_ID_56301 0x1801173#define DEVICE_ID_56361 0x3410174#define SUBVENDOR_ID 0xECC0175176177/*178* Valid Echo PCI subsystem card IDs179*/180#define DARLA20 0x0010181#define GINA20 0x0020182#define LAYLA20 0x0030183#define DARLA24 0x0040184#define GINA24 0x0050185#define LAYLA24 0x0060186#define MONA 0x0070187#define MIA 0x0080188#define INDIGO 0x0090189#define INDIGO_IO 0x00a0190#define INDIGO_DJ 0x00b0191#define DC8 0x00c0192#define INDIGO_IOX 0x00d0193#define INDIGO_DJX 0x00e0194#define ECHO3G 0x0100195196197/************************************************************************198199Array sizes and so forth200201***********************************************************************/202203/*204* Sizes205*/206#define ECHO_MAXAUDIOINPUTS 32 /* Max audio input channels */207#define ECHO_MAXAUDIOOUTPUTS 32 /* Max audio output channels */208#define ECHO_MAXAUDIOPIPES 32 /* Max number of input and output209* pipes */210#define E3G_MAX_OUTPUTS 16211#define ECHO_MAXMIDIJACKS 1 /* Max MIDI ports */212#define ECHO_MIDI_QUEUE_SZ 512 /* Max MIDI input queue entries */213#define ECHO_MTC_QUEUE_SZ 32 /* Max MIDI time code input queue214* entries */215216/*217* MIDI activity indicator timeout218*/219#define MIDI_ACTIVITY_TIMEOUT_USEC 200000220221222/****************************************************************************223224Clocks225226*****************************************************************************/227228/*229* Clock numbers230*/231#define ECHO_CLOCK_INTERNAL 0232#define ECHO_CLOCK_WORD 1233#define ECHO_CLOCK_SUPER 2234#define ECHO_CLOCK_SPDIF 3235#define ECHO_CLOCK_ADAT 4236#define ECHO_CLOCK_ESYNC 5237#define ECHO_CLOCK_ESYNC96 6238#define ECHO_CLOCK_MTC 7239#define ECHO_CLOCK_NUMBER 8240#define ECHO_CLOCKS 0xffff241242/*243* Clock bit numbers - used to report capabilities and whatever clocks244* are being detected dynamically.245*/246#define ECHO_CLOCK_BIT_INTERNAL (1 << ECHO_CLOCK_INTERNAL)247#define ECHO_CLOCK_BIT_WORD (1 << ECHO_CLOCK_WORD)248#define ECHO_CLOCK_BIT_SUPER (1 << ECHO_CLOCK_SUPER)249#define ECHO_CLOCK_BIT_SPDIF (1 << ECHO_CLOCK_SPDIF)250#define ECHO_CLOCK_BIT_ADAT (1 << ECHO_CLOCK_ADAT)251#define ECHO_CLOCK_BIT_ESYNC (1 << ECHO_CLOCK_ESYNC)252#define ECHO_CLOCK_BIT_ESYNC96 (1 << ECHO_CLOCK_ESYNC96)253#define ECHO_CLOCK_BIT_MTC (1<<ECHO_CLOCK_MTC)254255256/***************************************************************************257258Digital modes259260****************************************************************************/261262/*263* Digital modes for Mona, Layla24, and Gina24264*/265#define DIGITAL_MODE_NONE 0xFF266#define DIGITAL_MODE_SPDIF_RCA 0267#define DIGITAL_MODE_SPDIF_OPTICAL 1268#define DIGITAL_MODE_ADAT 2269#define DIGITAL_MODE_SPDIF_CDROM 3270#define DIGITAL_MODES 4271272/*273* Digital mode capability masks274*/275#define ECHOCAPS_HAS_DIGITAL_MODE_SPDIF_RCA (1 << DIGITAL_MODE_SPDIF_RCA)276#define ECHOCAPS_HAS_DIGITAL_MODE_SPDIF_OPTICAL (1 << DIGITAL_MODE_SPDIF_OPTICAL)277#define ECHOCAPS_HAS_DIGITAL_MODE_ADAT (1 << DIGITAL_MODE_ADAT)278#define ECHOCAPS_HAS_DIGITAL_MODE_SPDIF_CDROM (1 << DIGITAL_MODE_SPDIF_CDROM)279280281#define EXT_3GBOX_NC 0x01 /* 3G box not connected */282#define EXT_3GBOX_NOT_SET 0x02 /* 3G box not detected yet */283284285#define ECHOGAIN_MUTED (-128) /* Minimum possible gain */286#define ECHOGAIN_MINOUT (-128) /* Min output gain (dB) */287#define ECHOGAIN_MAXOUT (6) /* Max output gain (dB) */288#define ECHOGAIN_MININP (-50) /* Min input gain (0.5 dB) */289#define ECHOGAIN_MAXINP (50) /* Max input gain (0.5 dB) */290291#define PIPE_STATE_STOPPED 0 /* Pipe has been reset */292#define PIPE_STATE_PAUSED 1 /* Pipe has been stopped */293#define PIPE_STATE_STARTED 2 /* Pipe has been started */294#define PIPE_STATE_PENDING 3 /* Pipe has pending start */295296297/* Debug initialization */298#ifdef CONFIG_SND_DEBUG299#define DE_INIT(x) snd_printk x300#else301#define DE_INIT(x)302#endif303304/* Debug hw_params callbacks */305#ifdef CONFIG_SND_DEBUG306#define DE_HWP(x) snd_printk x307#else308#define DE_HWP(x)309#endif310311/* Debug normal activity (open, start, stop...) */312#ifdef CONFIG_SND_DEBUG313#define DE_ACT(x) snd_printk x314#else315#define DE_ACT(x)316#endif317318/* Debug midi activity */319#ifdef CONFIG_SND_DEBUG320#define DE_MID(x) snd_printk x321#else322#define DE_MID(x)323#endif324325326struct audiopipe {327volatile u32 *dma_counter; /* Commpage register that contains328* the current dma position329* (lower 32 bits only)330*/331u32 last_counter; /* The last position, which is used332* to compute...333*/334u32 position; /* ...the number of bytes tranferred335* by the DMA engine, modulo the336* buffer size337*/338short index; /* Index of the first channel or <0339* if hw is not configured yet340*/341short interleave;342struct snd_dma_buffer sgpage; /* Room for the scatter-gather list */343struct snd_pcm_hardware hw;344struct snd_pcm_hw_constraint_list constr;345short sglist_head;346char state; /* pipe state */347};348349350struct audioformat {351u8 interleave; /* How the data is arranged in memory:352* mono = 1, stereo = 2, ...353*/354u8 bits_per_sample; /* 8, 16, 24, 32 (24 bits left aligned) */355char mono_to_stereo; /* Only used if interleave is 1 and356* if this is an output pipe.357*/358char data_are_bigendian; /* 1 = big endian, 0 = little endian */359};360361362struct echoaudio {363spinlock_t lock;364struct snd_pcm_substream *substream[DSP_MAXPIPES];365int last_period[DSP_MAXPIPES];366struct mutex mode_mutex;367u16 num_digital_modes, digital_mode_list[6];368u16 num_clock_sources, clock_source_list[10];369atomic_t opencount;370struct snd_kcontrol *clock_src_ctl;371struct snd_pcm *analog_pcm, *digital_pcm;372struct snd_card *card;373const char *card_name;374struct pci_dev *pci;375unsigned long dsp_registers_phys;376struct resource *iores;377struct snd_dma_buffer commpage_dma_buf;378int irq;379#ifdef ECHOCARD_HAS_MIDI380struct snd_rawmidi *rmidi;381struct snd_rawmidi_substream *midi_in, *midi_out;382#endif383struct timer_list timer;384char tinuse; /* Timer in use */385char midi_full; /* MIDI output buffer is full */386char can_set_rate;387char rate_set;388389/* This stuff is used mainly by the lowlevel code */390struct comm_page *comm_page; /* Virtual address of the memory391* seen by DSP392*/393u32 pipe_alloc_mask; /* Bitmask of allocated pipes */394u32 pipe_cyclic_mask; /* Bitmask of pipes with cyclic395* buffers396*/397u32 sample_rate; /* Card sample rate in Hz */398u8 digital_mode; /* Current digital mode399* (see DIGITAL_MODE_*)400*/401u8 spdif_status; /* Gina20, Darla20, Darla24 - only */402u8 clock_state; /* Gina20, Darla20, Darla24 - only */403u8 input_clock; /* Currently selected sample clock404* source405*/406u8 output_clock; /* Layla20 only */407char meters_enabled; /* VU-meters status */408char asic_loaded; /* Set TRUE when ASIC loaded */409char bad_board; /* Set TRUE if DSP won't load */410char professional_spdif; /* 0 = consumer; 1 = professional */411char non_audio_spdif; /* 3G - only */412char digital_in_automute; /* Gina24, Layla24, Mona - only */413char has_phantom_power;414char hasnt_input_nominal_level; /* Gina3G */415char phantom_power; /* Gina3G - only */416char has_midi;417char midi_input_enabled;418419#ifdef ECHOCARD_ECHO3G420/* External module -dependent pipe and bus indexes */421char px_digital_out, px_analog_in, px_digital_in, px_num;422char bx_digital_out, bx_analog_in, bx_digital_in, bx_num;423#endif424425char nominal_level[ECHO_MAXAUDIOPIPES]; /* True == -10dBV426* False == +4dBu */427s8 input_gain[ECHO_MAXAUDIOINPUTS]; /* Input level -50..+50428* unit is 0.5dB */429s8 output_gain[ECHO_MAXAUDIOOUTPUTS]; /* Output level -128..+6 dB430* (-128=muted) */431s8 monitor_gain[ECHO_MAXAUDIOOUTPUTS][ECHO_MAXAUDIOINPUTS];432/* -128..+6 dB */433s8 vmixer_gain[ECHO_MAXAUDIOOUTPUTS][ECHO_MAXAUDIOOUTPUTS];434/* -128..+6 dB */435436u16 digital_modes; /* Bitmask of supported modes437* (see ECHOCAPS_HAS_DIGITAL_MODE_*) */438u16 input_clock_types; /* Suppoted input clock types */439u16 output_clock_types; /* Suppoted output clock types -440* Layla20 only */441u16 device_id, subdevice_id;442u16 *dsp_code; /* Current DSP code loaded,443* NULL if nothing loaded */444short dsp_code_to_load; /* DSP code to load */445short asic_code; /* Current ASIC code */446u32 comm_page_phys; /* Physical address of the447* memory seen by DSP */448volatile u32 __iomem *dsp_registers; /* DSP's register base */449u32 active_mask; /* Chs. active mask or450* punks out */451#ifdef CONFIG_PM452const struct firmware *fw_cache[8]; /* Cached firmwares */453#endif454455#ifdef ECHOCARD_HAS_MIDI456u16 mtc_state; /* State for MIDI input parsing state machine */457u8 midi_buffer[MIDI_IN_BUFFER_SIZE];458#endif459};460461462static int init_dsp_comm_page(struct echoaudio *chip);463static int init_line_levels(struct echoaudio *chip);464static int free_pipes(struct echoaudio *chip, struct audiopipe *pipe);465static int load_firmware(struct echoaudio *chip);466static int wait_handshake(struct echoaudio *chip);467static int send_vector(struct echoaudio *chip, u32 command);468static int get_firmware(const struct firmware **fw_entry,469struct echoaudio *chip, const short fw_index);470static void free_firmware(const struct firmware *fw_entry);471472#ifdef ECHOCARD_HAS_MIDI473static int enable_midi_input(struct echoaudio *chip, char enable);474static void snd_echo_midi_output_trigger(475struct snd_rawmidi_substream *substream, int up);476static int midi_service_irq(struct echoaudio *chip);477static int __devinit snd_echo_midi_create(struct snd_card *card,478struct echoaudio *chip);479#endif480481482static inline void clear_handshake(struct echoaudio *chip)483{484chip->comm_page->handshake = 0;485}486487static inline u32 get_dsp_register(struct echoaudio *chip, u32 index)488{489return readl(&chip->dsp_registers[index]);490}491492static inline void set_dsp_register(struct echoaudio *chip, u32 index,493u32 value)494{495writel(value, &chip->dsp_registers[index]);496}497498499/* Pipe and bus indexes. PX_* and BX_* are defined as chip->px_* and chip->bx_*500for 3G cards because they depend on the external box. They are integer501constants for all other cards.502Never use those defines directly, use the following functions instead. */503504static inline int px_digital_out(const struct echoaudio *chip)505{506return PX_DIGITAL_OUT;507}508509static inline int px_analog_in(const struct echoaudio *chip)510{511return PX_ANALOG_IN;512}513514static inline int px_digital_in(const struct echoaudio *chip)515{516return PX_DIGITAL_IN;517}518519static inline int px_num(const struct echoaudio *chip)520{521return PX_NUM;522}523524static inline int bx_digital_out(const struct echoaudio *chip)525{526return BX_DIGITAL_OUT;527}528529static inline int bx_analog_in(const struct echoaudio *chip)530{531return BX_ANALOG_IN;532}533534static inline int bx_digital_in(const struct echoaudio *chip)535{536return BX_DIGITAL_IN;537}538539static inline int bx_num(const struct echoaudio *chip)540{541return BX_NUM;542}543544static inline int num_pipes_out(const struct echoaudio *chip)545{546return px_analog_in(chip);547}548549static inline int num_pipes_in(const struct echoaudio *chip)550{551return px_num(chip) - px_analog_in(chip);552}553554static inline int num_busses_out(const struct echoaudio *chip)555{556return bx_analog_in(chip);557}558559static inline int num_busses_in(const struct echoaudio *chip)560{561return bx_num(chip) - bx_analog_in(chip);562}563564static inline int num_analog_busses_out(const struct echoaudio *chip)565{566return bx_digital_out(chip);567}568569static inline int num_analog_busses_in(const struct echoaudio *chip)570{571return bx_digital_in(chip) - bx_analog_in(chip);572}573574static inline int num_digital_busses_out(const struct echoaudio *chip)575{576return num_busses_out(chip) - num_analog_busses_out(chip);577}578579static inline int num_digital_busses_in(const struct echoaudio *chip)580{581return num_busses_in(chip) - num_analog_busses_in(chip);582}583584/* The monitor array is a one-dimensional array; compute the offset585* into the array */586static inline int monitor_index(const struct echoaudio *chip, int out, int in)587{588return out * num_busses_in(chip) + in;589}590591592#ifndef pci_device593#define pci_device(chip) (&chip->pci->dev)594#endif595596597#endif /* _ECHOAUDIO_H_ */598599600