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