xref: /haiku/src/add-ons/kernel/drivers/audio/emuxki/emuxki.h (revision 4f00613311d0bd6b70fa82ce19931c41f071ea4e)
1 /*
2  * Emuxki BeOS Driver for Creative Labs SBLive!/Audigy series
3  *
4  * Copyright (c) 2002, Jerome Duval (jerome.duval@free.fr)
5  *
6  * Copyright (c) 2001 The NetBSD Foundation, Inc.
7  * All rights reserved.
8  *
9  * This code is derived from software contributed to The NetBSD Foundation
10  * by Yannick Montulet.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *	This product includes software developed by the NetBSD
23  *	Foundation, Inc. and its contributors.
24  * 4. Neither the name of The NetBSD Foundation nor the names of its
25  *    contributors may be used to endorse or promote products derived
26  *    from this software without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGE.
39  */
40 #if !defined(_EMUXKI_H)
41 #define _EMUXKI_H
42 
43 #include <Drivers.h>
44 #include <SupportDefs.h>
45 #include <OS.h>
46 #include "emuxkireg.h"
47 #include "config.h"
48 #include "queue.h"
49 #include "multi_audio.h"
50 #include "multi.h"
51 
52 
53 #define CREATIVELABS_VENDOR_ID	0x1102	/* Creative Labs */
54 #define CREATIVELABS_SBLIVE_DEVICE_ID	0x0002	/* Creative Labs SoundBlaster Live */
55 #define CREATIVELABS_AUDIGY_DEVICE_ID	0x0004	/* Creative Labs Audigy and Audigy 2 */
56 
57 #define AUDIGY 1	// Audigy seems to work
58 #define MIDI 0 		// Midi is not supported for now
59 
60 #define VERSION "Version alpha 7, Copyright (c) 2002,2003 Jérôme Duval, compiled on " ## __DATE__ ## " " ## __TIME__
61 #define DRIVER_NAME "emuxki"
62 #define FRIENDLY_NAME "Emuxki"
63 #define FRIENDLY_NAME_LIVE FRIENDLY_NAME" SB Live"
64 #define FRIENDLY_NAME_LIVE_5_1 FRIENDLY_NAME_LIVE" 5.1"
65 #define FRIENDLY_NAME_AUDIGY FRIENDLY_NAME" Audigy"
66 #define FRIENDLY_NAME_AUDIGY2 FRIENDLY_NAME_AUDIGY" 2"
67 #define AUTHOR "Jérôme Duval"
68 
69 /*
70  * Emu10k1 midi
71  */
72 
73 typedef struct _midi_dev {
74 	struct _emuxki_dev *
75 				card;
76 	void *		driver;
77 	void *		cookie;
78 	int32		count;
79 	char		name[64];
80 } midi_dev;
81 
82 
83 /*
84  * Emu10k1 hardware limits
85  */
86 
87 #define	EMU_PTESIZE		4096
88 #define	EMU_MAXPTE ((EMU_CHAN_PSST_LOOPSTARTADDR_MASK + 1) /	\
89 			EMU_PTESIZE)
90 #define EMU_NUMCHAN		64
91 #define EMU_NUMRECSRCS	3
92 
93 #define	EMU_DMA_ALIGN	4096
94 #define	EMU_DMAMEM_NSEG	1
95 
96 /*
97  * Emu10k1 memory managment
98  */
99 
100 typedef struct _emuxki_mem {
101 	LIST_ENTRY(_emuxki_mem) next;
102 	uint16	       ptbidx;
103 	void	*log_base;
104 	void	*phy_base;
105 	area_id area;
106 	size_t	size;
107 #define	EMU_RMEM		0xffff		/* recording memory */
108 } emuxki_mem;
109 
110 /*
111  * Emu10k1 play channel params
112  */
113 
114 typedef struct _emuxki_chanparms_fxsend {
115 	struct {
116 		uint8        level, dest;
117 	}               a, b, c, d, e, f, g, h;
118 } emuxki_chanparms_fxsend;
119 
120 typedef struct _emuxki_chanparms_pitch {
121 	uint16       intial;	/* 4 bits of octave, 12 bits of fractional
122 				 * octave */
123 	uint16       current;/* 0x4000 == unity pitch shift */
124 	uint16       target;	/* 0x4000 == unity pitch shift */
125 	uint8        envelope_amount;	/* Signed 2's complement, +/-
126 						 * one octave peak extremes */
127 } emuxki_chanparms_pitch;
128 
129 typedef struct _emuxki_chanparms_envelope {
130 	uint16       current_state;	/* 0x8000-n == 666*n usec delay */
131 	uint8        hold_time;	/* 127-n == n*(volume ? 88.2 :
132 					 * 42)msec */
133 	uint8        attack_time;	/* 0 = infinite, 1 = (volume ? 11 :
134 					 * 10.9) msec, 0x7f = 5.5msec */
135 	uint8        sustain_level;	/* 127 = full, 0 = off, 0.75dB
136 					 * increments */
137 	uint8        decay_time;	/* 0 = 43.7msec, 1 = 21.8msec, 0x7f =
138 					 * 22msec */
139 } emuxki_chanparms_envelope;
140 
141 typedef struct _emuxki_chanparms_volume {
142 	uint16       current;
143 	uint16       target;
144 	emuxki_chanparms_envelope envelope;
145 } emuxki_chanparms_volume;
146 
147 typedef struct _emuxki_chanparms_filter {
148 	uint16       initial_cutoff_frequency;
149 	/*
150 	 * 6 most  significant bits are semitones, 2 least significant bits
151 	 * are fractions
152 	 */
153 	uint16       current_cutoff_frequency;
154 	uint16       target_cutoff_frequency;
155 	uint8        lowpass_resonance_height;
156 	uint8        interpolation_ROM;	/* 1 = full band, 7 = low
157 						 * pass */
158 	uint8        envelope_amount;	/* Signed 2's complement, +/-
159 						 * six octaves peak extremes */
160 	uint8        LFO_modulation_depth;	/* Signed 2's complement, +/-
161 						 * three octave extremes */
162 } emuxki_chanparms_filter;
163 
164 typedef struct _emuxki_chanparms_loop {
165 	uint32       start;	/* index in the PTB (in samples) */
166 	uint32       end;	/* index in the PTB (in samples) */
167 } emuxki_chanparms_loop;
168 
169 typedef struct _emuxki_chanparms_modulation {
170 	emuxki_chanparms_envelope envelope;
171 	uint16       LFO_state;	/* 0x8000-n = 666*n usec delay */
172 } emuxki_chanparms_modulation;
173 
174 typedef struct _emuxki_chanparms_vibrato_LFO {
175 	uint16       state;	/* 0x8000-n == 666*n usec delay */
176 	uint8        modulation_depth;	/* Signed 2's complement, +/-
177 						 * one octave extremes */
178 	uint8        vibrato_depth;	/* Signed 2's complement, +/- one
179 					 * octave extremes */
180 	uint8        frequency;	/* 0.039Hz steps, maximum of 9.85 Hz */
181 } emuxki_chanparms_vibrato_LFO;
182 
183 typedef struct _emuxki_channel {
184 	uint8        num;	/* voice number */
185 	struct _emuxki_voice *voice;
186 	emuxki_chanparms_fxsend fxsend;
187 	emuxki_chanparms_pitch pitch;
188 	uint16       initial_attenuation;	/* 0.375dB steps */
189 	emuxki_chanparms_volume volume;
190 	emuxki_chanparms_filter filter;
191 	emuxki_chanparms_loop loop;
192 	emuxki_chanparms_modulation modulation;
193 	emuxki_chanparms_vibrato_LFO vibrato_LFO;
194 	uint8        tremolo_depth;
195 } emuxki_channel;
196 
197 typedef struct _emuxki_recparams {
198 	uint32		efx_voices[2];
199 } emuxki_recparams;
200 
201 
202 /*
203  * Voices
204  */
205 
206 typedef enum {
207 	EMU_RECSRC_MIC = 0,
208 	EMU_RECSRC_ADC,
209 	EMU_RECSRC_FX,
210 	EMU_RECSRC_NOTSET
211 } emuxki_recsrc_t;
212 
213 #define	EMU_USE_PLAY		(1 << 0)
214 #define	EMU_USE_RECORD	(1 << 1)
215 #define EMU_STATE_STARTED	(1 << 0)
216 #define	EMU_STEREO_NOTSET	0xFF
217 
218 typedef struct _emuxki_voice {
219 	struct _emuxki_stream 	 *stream;
220 	uint8        use;
221 	uint8  		 voicenum;
222 	uint8        state;
223 	uint8        stereo;
224 	uint8        b16;
225 	uint32       sample_rate;
226 	union {
227 		emuxki_channel *chan[2];
228 		emuxki_recsrc_t source;
229 	}  			 dataloc;
230 	emuxki_recparams recparams;
231 	emuxki_mem *buffer;
232 	uint16       blksize;/* in samples */
233 	uint16       trigblk;/* blk on which to trigger inth */
234 	uint16       blkmod;	/* Modulo value to wrap trigblk */
235 	uint16       timerate;
236 
237 	LIST_ENTRY(_emuxki_voice) next;
238 
239 } emuxki_voice;
240 
241 /*
242  * Streams
243  */
244 
245 typedef struct _emuxki_stream {
246 	struct _emuxki_dev 	*card;
247 	uint8        		use;
248 	uint8        		state;
249 	uint8        		stereo;
250 	uint8        		b16;
251 	uint32       		sample_rate;
252 	uint8				nmono;
253 	uint8				nstereo;
254 	uint32 				bufframes;
255 	uint8 				bufcount;
256 	LIST_HEAD(, _emuxki_voice) voices;
257 	emuxki_voice		*first_voice;
258 
259 	LIST_ENTRY(_emuxki_stream)	next;
260 
261 	void            	(*inth) (void *);
262 	void           		*inthparam;
263 
264 	/* multi_audio */
265 	volatile int64	frames_count;	// for play or record
266 	volatile bigtime_t real_time;	// for play or record
267 	volatile int32	buffer_cycle;	// for play or record
268 	int32 first_channel;
269 	bool update_needed;
270 } emuxki_stream;
271 
272 /*
273  * Mixer controls gpr
274  */
275 #define EMU_GPR_FIRST_MIX 16
276 
277 typedef enum {
278 	EMU_MIX_GAIN = 1 << 0,
279 	EMU_MIX_MONO = 1 << 1,
280 	EMU_MIX_STEREO = 1 << 2,
281 	EMU_MIX_TMP = 1 << 3,
282 	EMU_MIX_MUTE = 1 << 4,
283 	EMU_MIX_PLAYBACK = 1 << 5,
284 	EMU_MIX_RECORD = 1 << 6
285 } emuxki_gpr_type;
286 
287 typedef struct _emuxki_gpr {
288 	char name[32];
289 	emuxki_gpr_type  type;
290 	uint16	gpr;
291 	float	default_value;
292 	float	min_gain;
293 	float	max_gain;
294 	float	granularity;
295 	float 	current[2];
296 	bool	mute;
297 } emuxki_gpr;
298 
299 typedef enum {
300 	EMU_DIGITAL_MODE = 1 << 0,
301 	EMU_AUDIO_MODE = 1 << 1
302 } emuxki_parameter_type;
303 
304 /*
305  * Devices
306  */
307 
308 typedef struct _emuxki_dev {
309 	char		name[DEVNAME];	/* used for resources */
310 	pci_info	info;
311 	device_config config;
312 
313 	void	*ptb_log_base;
314 	void	*ptb_phy_base;
315 	area_id ptb_area;
316 	void	*silentpage_log_base;
317 	void	*silentpage_phy_base;
318 	area_id silentpage_area;
319 
320 	emuxki_channel	*channel[EMU_NUMCHAN];
321 	emuxki_voice	*recsrc[EMU_NUMRECSRCS];
322 
323 	LIST_HEAD(, _emuxki_mem) mem;
324 
325 	LIST_HEAD(, _emuxki_stream) streams;
326 
327 	uint8	timerstate;
328 	uint16	timerate;
329 #define	EMU_TIMER_STATE_ENABLED	1
330 	uint8	play_mode; // number of channels to be played : 2, 4, 6
331 	bool	digital_enabled;	// if digital is enabled and analog is disabled
332 
333 	emuxki_stream	*pstream;
334 	emuxki_stream	*pstream2;
335 	emuxki_stream	*rstream;
336 	emuxki_stream	*rstream2;
337 
338 	sem_id buffer_ready_sem;
339 
340 	midi_dev	midi;
341 
342 	/* mixer controls */
343 	emuxki_gpr	gpr[256];
344 	uint32 gpr_count;
345 
346 	/* multi_audio */
347 	multi_dev	multi;
348 } emuxki_dev;
349 
350 extern int32 num_cards;
351 extern emuxki_dev cards[NUM_CARDS];
352 
353 void emuxki_mem_free(emuxki_dev *card, void *ptr);
354 void * emuxki_pmem_alloc(emuxki_dev *card, size_t size);
355 void * emuxki_rmem_alloc(emuxki_dev *card, size_t size);
356 status_t emuxki_voice_commit_parms(emuxki_voice *voice);
357 status_t emuxki_voice_set_audioparms(emuxki_voice *voice, uint8 stereo,
358 			     uint8 b16, uint32 srate);
359 status_t emuxki_voice_set_recparms(emuxki_voice *voice, emuxki_recsrc_t recsrc,
360 			     	emuxki_recparams *recparams);
361 status_t emuxki_voice_set_bufparms(emuxki_voice *voice, void *ptr,
362 			   uint32 bufsize, uint16 blksize);
363 void emuxki_voice_start(emuxki_voice *voice);
364 void emuxki_voice_halt(emuxki_voice *voice);
365 emuxki_voice *emuxki_voice_new(emuxki_stream *stream, uint8 use, uint8 voicenum);
366 void emuxki_voice_delete(emuxki_voice *voice);
367 
368 status_t emuxki_stream_set_audioparms(emuxki_stream *stream, bool stereo, uint8 channels,
369 			     uint8 b16, uint32 sample_rate);
370 status_t emuxki_stream_set_recparms(emuxki_stream *stream, emuxki_recsrc_t recsrc,
371 			     	emuxki_recparams *recparams);
372 status_t emuxki_stream_commit_parms(emuxki_stream *stream);
373 status_t emuxki_stream_get_nth_buffer(emuxki_stream *stream, uint8 chan, uint8 buf,
374 					char** buffer, size_t *stride);
375 void emuxki_stream_start(emuxki_stream *stream, void (*inth) (void *), void *inthparam);
376 void emuxki_stream_halt(emuxki_stream *stream);
377 emuxki_stream *emuxki_stream_new(emuxki_dev *card, uint8 use, uint32 bufframes, uint8 bufcount);
378 void emuxki_stream_delete(emuxki_stream *stream);
379 
380 void emuxki_dump_fx(emuxki_dev * card);
381 void emuxki_gpr_dump(emuxki_dev * card, uint16 count);
382 void emuxki_gpr_set(emuxki_dev *card, emuxki_gpr *gpr, int32 type, float *values);
383 void emuxki_gpr_get(emuxki_dev *card, emuxki_gpr *gpr, int32 type, float *values);
384 
385 void emuxki_parameter_set(emuxki_dev *card, const void*, int32 type, int32 *value);
386 void emuxki_parameter_get(emuxki_dev *card, const void*, int32 type, int32 *value);
387 
388 extern void midi_interrupt_op(int32 op, void * data);
389 extern bool midi_interrupt(emuxki_dev *card);
390 
391 #endif
392 
393