1/****************************************************************************
2
3   Copyright Echo Digital Audio Corporation (c) 1998 - 2004
4   All rights reserved
5   www.echoaudio.com
6
7   This file is part of Echo Digital Audio's generic driver library.
8
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
12   Foundation.
13
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.
18
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,
22   MA  02111-1307, USA.
23
24 ****************************************************************************
25
26 Translation from C++ and adaptation for use in ALSA-Driver
27 were made by Giuliano Pochini <pochini@shiny.it>
28
29 ****************************************************************************
30
31
32   Here's a block diagram of how most of the cards work:
33
34                  +-----------+
35           record |           |<-------------------- Inputs
36          <-------|           |        |
37     PCI          | Transport |        |
38     bus          |  engine   |       \|/
39          ------->|           |    +-------+
40            play  |           |--->|monitor|-------> Outputs
41                  +-----------+    | mixer |
42                                   +-------+
43
44   The lines going to and from the PCI bus represent "pipes".  A pipe performs
45   audio transport - moving audio data to and from buffers on the host via
46   bus mastering.
47
48   The inputs and outputs on the right represent input and output "busses."
49   A bus is a physical, real connection to the outside world.  An example
50   of a bus would be the 1/4" analog connectors on the back of Layla or
51   an RCA S/PDIF connector.
52
53   For most cards, there is a one-to-one correspondence between outputs
54   and busses; that is, each individual pipe is hard-wired to a single bus.
55
56   Cards that work this way are Darla20, Gina20, Layla20, Darla24, Gina24,
57   Layla24, Mona, and Indigo.
58
59
60   Mia has a feature called "virtual outputs."
61
62
63                  +-----------+
64           record |           |<----------------------------- Inputs
65          <-------|           |                  |
66     PCI          | Transport |                  |
67     bus          |  engine   |                 \|/
68          ------->|           |   +------+   +-------+
69            play  |           |-->|vmixer|-->|monitor|-------> Outputs
70                  +-----------+   +------+   | mixer |
71                                             +-------+
72
73
74   Obviously, the difference here is the box labeled "vmixer."  Vmixer is
75   short for "virtual output mixer."  For Mia, pipes are *not* hard-wired
76   to a single bus; the vmixer lets you mix any pipe to any bus in any
77   combination.
78
79   Note, however, that the left-hand side of the diagram is unchanged.
80   Transport works exactly the same way - the difference is in the mixer stage.
81
82
83   Pipes and busses are numbered starting at zero.
84
85
86
87   Pipe index
88   ==========
89
90   A number of calls in CEchoGals refer to a "pipe index".  A pipe index is
91   a unique number for a pipe that unambiguously refers to a playback or record
92   pipe.  Pipe indices are numbered starting with analog outputs, followed by
93   digital outputs, then analog inputs, then digital inputs.
94
95   Take Gina24 as an example:
96
97   Pipe index
98
99   0-7            Analog outputs (0 .. FirstDigitalBusOut-1)
100   8-15           Digital outputs (FirstDigitalBusOut .. NumBussesOut-1)
101   16-17          Analog inputs
102   18-25          Digital inputs
103
104
105   You get the pipe index by calling CEchoGals::OpenAudio; the other transport
106   functions take the pipe index as a parameter.  If you need a pipe index for
107   some other reason, use the handy Makepipe_index method.
108
109
110   Some calls take a CChannelMask parameter; CChannelMask is a handy way to
111   group pipe indices.
112
113
114
115   Digital mode switch
116   ===================
117
118   Some cards (right now, Gina24, Layla24, and Mona) have a Digital Mode Switch
119   or DMS.  Cards with a DMS can be set to one of three mutually exclusive
120   digital modes: S/PDIF RCA, S/PDIF optical, or ADAT optical.
121
122   This may create some confusion since ADAT optical is 8 channels wide and
123   S/PDIF is only two channels wide.  Gina24, Layla24, and Mona handle this
124   by acting as if they always have 8 digital outs and ins.  If you are in
125   either S/PDIF mode, the last 6 channels don't do anything - data sent
126   out these channels is thrown away and you will always record zeros.
127
128   Note that with Gina24, Layla24, and Mona, sample rates above 50 kHz are
129   only available if you have the card configured for S/PDIF optical or S/PDIF
130   RCA.
131
132
133
134   Double speed mode
135   =================
136
137   Some of the cards support 88.2 kHz and 96 kHz sampling (Darla24, Gina24,
138   Layla24, Mona, Mia, and Indigo).  For these cards, the driver sometimes has
139   to worry about "double speed mode"; double speed mode applies whenever the
140   sampling rate is above 50 kHz.
141
142   For instance, Mona and Layla24 support word clock sync.  However, they
143   actually support two different word clock modes - single speed (below
144   50 kHz) and double speed (above 50 kHz).  The hardware detects if a single
145   or double speed word clock signal is present; the generic code uses that
146   information to determine which mode to use.
147
148   The generic code takes care of all this for you.
149*/
150
151
152#ifndef _ECHOAUDIO_H_
153#define _ECHOAUDIO_H_
154
155
156#define TRUE 1
157#define FALSE 0
158
159#include "echoaudio_dsp.h"
160
161
162
163/***********************************************************************
164
165	PCI configuration space
166
167***********************************************************************/
168
169/*
170 * PCI vendor ID and device IDs for the hardware
171 */
172#define VENDOR_ID		0x1057
173#define DEVICE_ID_56301		0x1801
174#define DEVICE_ID_56361		0x3410
175#define SUBVENDOR_ID		0xECC0
176
177
178/*
179 * Valid Echo PCI subsystem card IDs
180 */
181#define DARLA20			0x0010
182#define GINA20			0x0020
183#define LAYLA20			0x0030
184#define DARLA24			0x0040
185#define GINA24			0x0050
186#define LAYLA24			0x0060
187#define MONA			0x0070
188#define MIA			0x0080
189#define INDIGO			0x0090
190#define INDIGO_IO		0x00a0
191#define INDIGO_DJ		0x00b0
192#define DC8			0x00c0
193#define INDIGO_IOX		0x00d0
194#define INDIGO_DJX		0x00e0
195#define ECHO3G			0x0100
196
197
198/************************************************************************
199
200	Array sizes and so forth
201
202***********************************************************************/
203
204/*
205 * Sizes
206 */
207#define ECHO_MAXAUDIOINPUTS	32	/* Max audio input channels */
208#define ECHO_MAXAUDIOOUTPUTS	32	/* Max audio output channels */
209#define ECHO_MAXAUDIOPIPES	32	/* Max number of input and output
210					 * pipes */
211#define E3G_MAX_OUTPUTS		16
212#define ECHO_MAXMIDIJACKS	1	/* Max MIDI ports */
213#define ECHO_MIDI_QUEUE_SZ 	512	/* Max MIDI input queue entries */
214#define ECHO_MTC_QUEUE_SZ	32	/* Max MIDI time code input queue
215					 * entries */
216
217/*
218 * MIDI activity indicator timeout
219 */
220#define MIDI_ACTIVITY_TIMEOUT_USEC	200000
221
222
223/****************************************************************************
224
225   Clocks
226
227*****************************************************************************/
228
229/*
230 * Clock numbers
231 */
232#define ECHO_CLOCK_INTERNAL		0
233#define ECHO_CLOCK_WORD			1
234#define ECHO_CLOCK_SUPER		2
235#define ECHO_CLOCK_SPDIF		3
236#define ECHO_CLOCK_ADAT			4
237#define ECHO_CLOCK_ESYNC		5
238#define ECHO_CLOCK_ESYNC96		6
239#define ECHO_CLOCK_MTC			7
240#define ECHO_CLOCK_NUMBER		8
241#define ECHO_CLOCKS			0xffff
242
243/*
244 * Clock bit numbers - used to report capabilities and whatever clocks
245 * are being detected dynamically.
246 */
247#define ECHO_CLOCK_BIT_INTERNAL		(1 << ECHO_CLOCK_INTERNAL)
248#define ECHO_CLOCK_BIT_WORD		(1 << ECHO_CLOCK_WORD)
249#define ECHO_CLOCK_BIT_SUPER		(1 << ECHO_CLOCK_SUPER)
250#define ECHO_CLOCK_BIT_SPDIF		(1 << ECHO_CLOCK_SPDIF)
251#define ECHO_CLOCK_BIT_ADAT		(1 << ECHO_CLOCK_ADAT)
252#define ECHO_CLOCK_BIT_ESYNC		(1 << ECHO_CLOCK_ESYNC)
253#define ECHO_CLOCK_BIT_ESYNC96		(1 << ECHO_CLOCK_ESYNC96)
254#define ECHO_CLOCK_BIT_MTC		(1<<ECHO_CLOCK_MTC)
255
256
257/***************************************************************************
258
259   Digital modes
260
261****************************************************************************/
262
263/*
264 * Digital modes for Mona, Layla24, and Gina24
265 */
266#define DIGITAL_MODE_NONE			0xFF
267#define DIGITAL_MODE_SPDIF_RCA			0
268#define DIGITAL_MODE_SPDIF_OPTICAL		1
269#define DIGITAL_MODE_ADAT			2
270#define DIGITAL_MODE_SPDIF_CDROM		3
271#define DIGITAL_MODES				4
272
273/*
274 * Digital mode capability masks
275 */
276#define ECHOCAPS_HAS_DIGITAL_MODE_SPDIF_RCA	(1 << DIGITAL_MODE_SPDIF_RCA)
277#define ECHOCAPS_HAS_DIGITAL_MODE_SPDIF_OPTICAL	(1 << DIGITAL_MODE_SPDIF_OPTICAL)
278#define ECHOCAPS_HAS_DIGITAL_MODE_ADAT		(1 << DIGITAL_MODE_ADAT)
279#define ECHOCAPS_HAS_DIGITAL_MODE_SPDIF_CDROM	(1 << DIGITAL_MODE_SPDIF_CDROM)
280
281
282#define EXT_3GBOX_NC			0x01	/* 3G box not connected */
283#define EXT_3GBOX_NOT_SET		0x02	/* 3G box not detected yet */
284
285
286#define ECHOGAIN_MUTED		(-128)	/* Minimum possible gain */
287#define ECHOGAIN_MINOUT		(-128)	/* Min output gain (dB) */
288#define ECHOGAIN_MAXOUT		(6)	/* Max output gain (dB) */
289#define ECHOGAIN_MININP		(-50)	/* Min input gain (0.5 dB) */
290#define ECHOGAIN_MAXINP		(50)	/* Max input gain (0.5 dB) */
291
292#define PIPE_STATE_STOPPED	0	/* Pipe has been reset */
293#define PIPE_STATE_PAUSED	1	/* Pipe has been stopped */
294#define PIPE_STATE_STARTED	2	/* Pipe has been started */
295#define PIPE_STATE_PENDING	3	/* Pipe has pending start */
296
297
298
299struct audiopipe {
300	volatile u32 *dma_counter;	/* Commpage register that contains
301					 * the current dma position
302					 * (lower 32 bits only)
303					 */
304	u32 last_counter;		/* The last position, which is used
305					 * to compute...
306					 */
307	u32 position;			/* ...the number of bytes tranferred
308					 * by the DMA engine, modulo the
309					 * buffer size
310					 */
311	short index;			/* Index of the first channel or <0
312					 * if hw is not configured yet
313					 */
314	short interleave;
315	struct snd_dma_buffer sgpage;	/* Room for the scatter-gather list */
316	struct snd_pcm_hardware hw;
317	struct snd_pcm_hw_constraint_list constr;
318	short sglist_head;
319	char state;			/* pipe state */
320};
321
322
323struct audioformat {
324	u8 interleave;			/* How the data is arranged in memory:
325					 * mono = 1, stereo = 2, ...
326					 */
327	u8 bits_per_sample;		/* 8, 16, 24, 32 (24 bits left aligned) */
328	char mono_to_stereo;		/* Only used if interleave is 1 and
329					 * if this is an output pipe.
330					 */
331	char data_are_bigendian;	/* 1 = big endian, 0 = little endian */
332};
333
334
335struct echoaudio {
336	spinlock_t lock;
337	struct snd_pcm_substream *substream[DSP_MAXPIPES];
338	int last_period[DSP_MAXPIPES];
339	struct mutex mode_mutex;
340	u16 num_digital_modes, digital_mode_list[6];
341	u16 num_clock_sources, clock_source_list[10];
342	atomic_t opencount;
343	struct snd_kcontrol *clock_src_ctl;
344	struct snd_pcm *analog_pcm, *digital_pcm;
345	struct snd_card *card;
346	const char *card_name;
347	struct pci_dev *pci;
348	unsigned long dsp_registers_phys;
349	struct resource *iores;
350	struct snd_dma_buffer commpage_dma_buf;
351	int irq;
352#ifdef ECHOCARD_HAS_MIDI
353	struct snd_rawmidi *rmidi;
354	struct snd_rawmidi_substream *midi_in, *midi_out;
355#endif
356	struct timer_list timer;
357	char tinuse;				/* Timer in use */
358	char midi_full;				/* MIDI output buffer is full */
359	char can_set_rate;
360	char rate_set;
361
362	/* This stuff is used mainly by the lowlevel code */
363	struct comm_page *comm_page;	/* Virtual address of the memory
364					 * seen by DSP
365					 */
366	u32 pipe_alloc_mask;		/* Bitmask of allocated pipes */
367	u32 pipe_cyclic_mask;		/* Bitmask of pipes with cyclic
368					 * buffers
369					 */
370	u32 sample_rate;		/* Card sample rate in Hz */
371	u8 digital_mode;		/* Current digital mode
372					 * (see DIGITAL_MODE_*)
373					 */
374	u8 spdif_status;		/* Gina20, Darla20, Darla24 - only */
375	u8 clock_state;			/* Gina20, Darla20, Darla24 - only */
376	u8 input_clock;			/* Currently selected sample clock
377					 * source
378					 */
379	u8 output_clock;		/* Layla20 only */
380	char meters_enabled;		/* VU-meters status */
381	char asic_loaded;		/* Set TRUE when ASIC loaded */
382	char bad_board;			/* Set TRUE if DSP won't load */
383	char professional_spdif;	/* 0 = consumer; 1 = professional */
384	char non_audio_spdif;		/* 3G - only */
385	char digital_in_automute;	/* Gina24, Layla24, Mona - only */
386	char has_phantom_power;
387	char hasnt_input_nominal_level;	/* Gina3G */
388	char phantom_power;		/* Gina3G - only */
389	char has_midi;
390	char midi_input_enabled;
391
392#ifdef ECHOCARD_ECHO3G
393	/* External module -dependent pipe and bus indexes */
394	char px_digital_out, px_analog_in, px_digital_in, px_num;
395	char bx_digital_out, bx_analog_in, bx_digital_in, bx_num;
396#endif
397
398	char nominal_level[ECHO_MAXAUDIOPIPES];	/* True == -10dBV
399						 * False == +4dBu */
400	s8 input_gain[ECHO_MAXAUDIOINPUTS];	/* Input level -50..+50
401						 * unit is 0.5dB */
402	s8 output_gain[ECHO_MAXAUDIOOUTPUTS];	/* Output level -128..+6 dB
403						 * (-128=muted) */
404	s8 monitor_gain[ECHO_MAXAUDIOOUTPUTS][ECHO_MAXAUDIOINPUTS];
405		/* -128..+6 dB */
406	s8 vmixer_gain[ECHO_MAXAUDIOOUTPUTS][ECHO_MAXAUDIOOUTPUTS];
407		/* -128..+6 dB */
408
409	u16 digital_modes;		/* Bitmask of supported modes
410					 * (see ECHOCAPS_HAS_DIGITAL_MODE_*) */
411	u16 input_clock_types;		/* Suppoted input clock types */
412	u16 output_clock_types;		/* Suppoted output clock types -
413					 * Layla20 only */
414	u16 device_id, subdevice_id;
415	u16 *dsp_code;			/* Current DSP code loaded,
416					 * NULL if nothing loaded */
417	short dsp_code_to_load;		/* DSP code to load */
418	short asic_code;		/* Current ASIC code */
419	u32 comm_page_phys;			/* Physical address of the
420						 * memory seen by DSP */
421	volatile u32 __iomem *dsp_registers;	/* DSP's register base */
422	u32 active_mask;			/* Chs. active mask or
423						 * punks out */
424#ifdef CONFIG_PM_SLEEP
425	const struct firmware *fw_cache[8];	/* Cached firmwares */
426#endif
427
428#ifdef ECHOCARD_HAS_MIDI
429	u16 mtc_state;				/* State for MIDI input parsing state machine */
430	u8 midi_buffer[MIDI_IN_BUFFER_SIZE];
431#endif
432};
433
434
435static int init_dsp_comm_page(struct echoaudio *chip);
436static int init_line_levels(struct echoaudio *chip);
437static int free_pipes(struct echoaudio *chip, struct audiopipe *pipe);
438static int load_firmware(struct echoaudio *chip);
439static int wait_handshake(struct echoaudio *chip);
440static int send_vector(struct echoaudio *chip, u32 command);
441static int get_firmware(const struct firmware **fw_entry,
442			struct echoaudio *chip, const short fw_index);
443static void free_firmware(const struct firmware *fw_entry,
444			  struct echoaudio *chip);
445
446#ifdef ECHOCARD_HAS_MIDI
447static int enable_midi_input(struct echoaudio *chip, char enable);
448static void snd_echo_midi_output_trigger(
449			struct snd_rawmidi_substream *substream, int up);
450static int midi_service_irq(struct echoaudio *chip);
451static int snd_echo_midi_create(struct snd_card *card,
452				struct echoaudio *chip);
453#endif
454
455
456static inline void clear_handshake(struct echoaudio *chip)
457{
458	chip->comm_page->handshake = 0;
459}
460
461static inline u32 get_dsp_register(struct echoaudio *chip, u32 index)
462{
463	return readl(&chip->dsp_registers[index]);
464}
465
466static inline void set_dsp_register(struct echoaudio *chip, u32 index,
467				    u32 value)
468{
469	writel(value, &chip->dsp_registers[index]);
470}
471
472
473/* Pipe and bus indexes. PX_* and BX_* are defined as chip->px_* and chip->bx_*
474for 3G cards because they depend on the external box. They are integer
475constants for all other cards.
476Never use those defines directly, use the following functions instead. */
477
478static inline int px_digital_out(const struct echoaudio *chip)
479{
480	return PX_DIGITAL_OUT;
481}
482
483static inline int px_analog_in(const struct echoaudio *chip)
484{
485	return PX_ANALOG_IN;
486}
487
488static inline int px_digital_in(const struct echoaudio *chip)
489{
490	return PX_DIGITAL_IN;
491}
492
493static inline int px_num(const struct echoaudio *chip)
494{
495	return PX_NUM;
496}
497
498static inline int bx_digital_out(const struct echoaudio *chip)
499{
500	return BX_DIGITAL_OUT;
501}
502
503static inline int bx_analog_in(const struct echoaudio *chip)
504{
505	return BX_ANALOG_IN;
506}
507
508static inline int bx_digital_in(const struct echoaudio *chip)
509{
510	return BX_DIGITAL_IN;
511}
512
513static inline int bx_num(const struct echoaudio *chip)
514{
515	return BX_NUM;
516}
517
518static inline int num_pipes_out(const struct echoaudio *chip)
519{
520	return px_analog_in(chip);
521}
522
523static inline int num_pipes_in(const struct echoaudio *chip)
524{
525	return px_num(chip) - px_analog_in(chip);
526}
527
528static inline int num_busses_out(const struct echoaudio *chip)
529{
530	return bx_analog_in(chip);
531}
532
533static inline int num_busses_in(const struct echoaudio *chip)
534{
535	return bx_num(chip) - bx_analog_in(chip);
536}
537
538static inline int num_analog_busses_out(const struct echoaudio *chip)
539{
540	return bx_digital_out(chip);
541}
542
543static inline int num_analog_busses_in(const struct echoaudio *chip)
544{
545	return bx_digital_in(chip) - bx_analog_in(chip);
546}
547
548static inline int num_digital_busses_out(const struct echoaudio *chip)
549{
550	return num_busses_out(chip) - num_analog_busses_out(chip);
551}
552
553static inline int num_digital_busses_in(const struct echoaudio *chip)
554{
555	return num_busses_in(chip) - num_analog_busses_in(chip);
556}
557
558/* The monitor array is a one-dimensional array; compute the offset
559 * into the array */
560static inline int monitor_index(const struct echoaudio *chip, int out, int in)
561{
562	return out * num_busses_in(chip) + in;
563}
564
565
566#ifndef pci_device
567#define pci_device(chip) (&chip->pci->dev)
568#endif
569
570
571#endif /* _ECHOAUDIO_H_ */
572