xref: /haiku/src/add-ons/kernel/drivers/audio/emuxki/emuxki.h (revision 1acbe440b8dd798953bec31d18ee589aa3f71b73)
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 #ifdef __HAIKU__
59 	#define MIDI	1
60 #else
61 	#define MIDI	0	// disabled MIDI for the time being on R5
62 #endif
63 
64 #define VERSION "Version alpha 7, Copyright (c) 2002,2003 Jérôme Duval, compiled on " ## __DATE__ ## " " ## __TIME__
65 #define DRIVER_NAME "emuxki"
66 #define FRIENDLY_NAME "Emuxki"
67 #define FRIENDLY_NAME_LIVE FRIENDLY_NAME" SB Live"
68 #define FRIENDLY_NAME_LIVE_5_1 FRIENDLY_NAME_LIVE" 5.1"
69 #define FRIENDLY_NAME_AUDIGY FRIENDLY_NAME" Audigy"
70 #define FRIENDLY_NAME_AUDIGY2 FRIENDLY_NAME_AUDIGY" 2"
71 #define AUTHOR "Jérôme Duval"
72 
73 /*
74  * Emuxki settings
75  */
76 
77 typedef struct {
78 	uint8   channels;
79 	uint8   bitsPerSample;
80 	uint32  sample_rate;
81 	uint32  buffer_frames;
82 	int32   buffer_count;
83 } emuxki_settings;
84 
85 extern emuxki_settings current_settings;
86 
87 /*
88  * Emu10k1 midi
89  */
90 
91 typedef struct _midi_dev {
92 	struct _emuxki_dev *
93 				card;
94 	void *		driver;
95 	void *		cookie;
96 	int32		count;
97 	char		name[64];
98 } midi_dev;
99 
100 
101 /*
102  * Emu10k1 hardware limits
103  */
104 
105 #define	EMU_PTESIZE		4096
106 #define	EMU_MAXPTE ((EMU_CHAN_PSST_LOOPSTARTADDR_MASK + 1) /	\
107 			EMU_PTESIZE)
108 #define EMU_NUMCHAN		64
109 #define EMU_NUMRECSRCS	3
110 
111 #define	EMU_DMA_ALIGN	4096
112 #define	EMU_DMAMEM_NSEG	1
113 
114 /*
115  * Emu10k1 memory managment
116  */
117 
118 typedef struct _emuxki_mem {
119 	LIST_ENTRY(_emuxki_mem) next;
120 	uint16	       ptbidx;
121 	void	*log_base;
122 	void	*phy_base;
123 	area_id area;
124 	size_t	size;
125 #define	EMU_RMEM		0xffff		/* recording memory */
126 } emuxki_mem;
127 
128 /*
129  * Emu10k1 play channel params
130  */
131 
132 typedef struct _emuxki_chanparms_fxsend {
133 	struct {
134 		uint8        level, dest;
135 	}               a, b, c, d, e, f, g, h;
136 } emuxki_chanparms_fxsend;
137 
138 typedef struct _emuxki_chanparms_pitch {
139 	uint16       intial;	/* 4 bits of octave, 12 bits of fractional
140 				 * octave */
141 	uint16       current;/* 0x4000 == unity pitch shift */
142 	uint16       target;	/* 0x4000 == unity pitch shift */
143 	uint8        envelope_amount;	/* Signed 2's complement, +/-
144 						 * one octave peak extremes */
145 } emuxki_chanparms_pitch;
146 
147 typedef struct _emuxki_chanparms_envelope {
148 	uint16       current_state;	/* 0x8000-n == 666*n usec delay */
149 	uint8        hold_time;	/* 127-n == n*(volume ? 88.2 :
150 					 * 42)msec */
151 	uint8        attack_time;	/* 0 = infinite, 1 = (volume ? 11 :
152 					 * 10.9) msec, 0x7f = 5.5msec */
153 	uint8        sustain_level;	/* 127 = full, 0 = off, 0.75dB
154 					 * increments */
155 	uint8        decay_time;	/* 0 = 43.7msec, 1 = 21.8msec, 0x7f =
156 					 * 22msec */
157 } emuxki_chanparms_envelope;
158 
159 typedef struct _emuxki_chanparms_volume {
160 	uint16       current;
161 	uint16       target;
162 	emuxki_chanparms_envelope envelope;
163 } emuxki_chanparms_volume;
164 
165 typedef struct _emuxki_chanparms_filter {
166 	uint16       initial_cutoff_frequency;
167 	/*
168 	 * 6 most  significant bits are semitones, 2 least significant bits
169 	 * are fractions
170 	 */
171 	uint16       current_cutoff_frequency;
172 	uint16       target_cutoff_frequency;
173 	uint8        lowpass_resonance_height;
174 	uint8        interpolation_ROM;	/* 1 = full band, 7 = low
175 						 * pass */
176 	uint8        envelope_amount;	/* Signed 2's complement, +/-
177 						 * six octaves peak extremes */
178 	uint8        LFO_modulation_depth;	/* Signed 2's complement, +/-
179 						 * three octave extremes */
180 } emuxki_chanparms_filter;
181 
182 typedef struct _emuxki_chanparms_loop {
183 	uint32       start;	/* index in the PTB (in samples) */
184 	uint32       end;	/* index in the PTB (in samples) */
185 } emuxki_chanparms_loop;
186 
187 typedef struct _emuxki_chanparms_modulation {
188 	emuxki_chanparms_envelope envelope;
189 	uint16       LFO_state;	/* 0x8000-n = 666*n usec delay */
190 } emuxki_chanparms_modulation;
191 
192 typedef struct _emuxki_chanparms_vibrato_LFO {
193 	uint16       state;	/* 0x8000-n == 666*n usec delay */
194 	uint8        modulation_depth;	/* Signed 2's complement, +/-
195 						 * one octave extremes */
196 	uint8        vibrato_depth;	/* Signed 2's complement, +/- one
197 					 * octave extremes */
198 	uint8        frequency;	/* 0.039Hz steps, maximum of 9.85 Hz */
199 } emuxki_chanparms_vibrato_LFO;
200 
201 typedef struct _emuxki_channel {
202 	uint8        num;	/* voice number */
203 	struct _emuxki_voice *voice;
204 	emuxki_chanparms_fxsend fxsend;
205 	emuxki_chanparms_pitch pitch;
206 	uint16       initial_attenuation;	/* 0.375dB steps */
207 	emuxki_chanparms_volume volume;
208 	emuxki_chanparms_filter filter;
209 	emuxki_chanparms_loop loop;
210 	emuxki_chanparms_modulation modulation;
211 	emuxki_chanparms_vibrato_LFO vibrato_LFO;
212 	uint8        tremolo_depth;
213 } emuxki_channel;
214 
215 typedef struct _emuxki_recparams {
216 	uint32		efx_voices[2];
217 } emuxki_recparams;
218 
219 
220 /*
221  * Voices
222  */
223 
224 typedef enum {
225 	EMU_RECSRC_MIC = 0,
226 	EMU_RECSRC_ADC,
227 	EMU_RECSRC_FX,
228 	EMU_RECSRC_NOTSET
229 } emuxki_recsrc_t;
230 
231 #define	EMU_USE_PLAY		(1 << 0)
232 #define	EMU_USE_RECORD	(1 << 1)
233 #define EMU_STATE_STARTED	(1 << 0)
234 #define	EMU_STEREO_NOTSET	0xFF
235 
236 typedef struct _emuxki_voice {
237 	struct _emuxki_stream 	 *stream;
238 	uint8        use;
239 	uint8  		 voicenum;
240 	uint8        state;
241 	uint8        stereo;
242 	uint8        b16;
243 	uint32       sample_rate;
244 	union {
245 		emuxki_channel *chan[2];
246 		emuxki_recsrc_t source;
247 	}  			 dataloc;
248 	emuxki_recparams recparams;
249 	emuxki_mem *buffer;
250 	uint16       blksize;/* in samples */
251 	uint16       trigblk;/* blk on which to trigger inth */
252 	uint16       blkmod;	/* Modulo value to wrap trigblk */
253 	uint16       timerate;
254 
255 	LIST_ENTRY(_emuxki_voice) next;
256 
257 } emuxki_voice;
258 
259 /*
260  * Streams
261  */
262 
263 typedef struct _emuxki_stream {
264 	struct _emuxki_dev 	*card;
265 	uint8        		use;
266 	uint8        		state;
267 	uint8        		stereo;
268 	uint8        		b16;
269 	uint32       		sample_rate;
270 	uint8				nmono;
271 	uint8				nstereo;
272 	uint32 				bufframes;
273 	uint8 				bufcount;
274 	LIST_HEAD(, _emuxki_voice) voices;
275 	emuxki_voice		*first_voice;
276 
277 	LIST_ENTRY(_emuxki_stream)	next;
278 
279 	void            	(*inth) (void *);
280 	void           		*inthparam;
281 
282 	/* multi_audio */
283 	volatile int64	frames_count;	// for play or record
284 	volatile bigtime_t real_time;	// for play or record
285 	volatile int32	buffer_cycle;	// for play or record
286 	int32 first_channel;
287 	bool update_needed;
288 } emuxki_stream;
289 
290 /*
291  * Mixer controls gpr
292  */
293 #define EMU_GPR_FIRST_MIX 16
294 
295 typedef enum {
296 	EMU_MIX_GAIN = 1 << 0,
297 	EMU_MIX_MONO = 1 << 1,
298 	EMU_MIX_STEREO = 1 << 2,
299 	EMU_MIX_TMP = 1 << 3,
300 	EMU_MIX_MUTE = 1 << 4,
301 	EMU_MIX_PLAYBACK = 1 << 5,
302 	EMU_MIX_RECORD = 1 << 6
303 } emuxki_gpr_type;
304 
305 typedef struct _emuxki_gpr {
306 	char name[32];
307 	emuxki_gpr_type  type;
308 	uint16	gpr;
309 	float	default_value;
310 	float	min_gain;
311 	float	max_gain;
312 	float	granularity;
313 	float 	current[2];
314 	bool	mute;
315 } emuxki_gpr;
316 
317 typedef enum {
318 	EMU_DIGITAL_MODE = 1 << 0,
319 	EMU_AUDIO_MODE = 1 << 1
320 } emuxki_parameter_type;
321 
322 /*
323  * Devices
324  */
325 
326 typedef struct _emuxki_dev {
327 	char		name[DEVNAME];	/* used for resources */
328 	pci_info	info;
329 	device_config config;
330 
331 	void	*ptb_log_base;
332 	void	*ptb_phy_base;
333 	area_id ptb_area;
334 	void	*silentpage_log_base;
335 	void	*silentpage_phy_base;
336 	area_id silentpage_area;
337 
338 	emuxki_channel	*channel[EMU_NUMCHAN];
339 	emuxki_voice	*recsrc[EMU_NUMRECSRCS];
340 
341 	LIST_HEAD(, _emuxki_mem) mem;
342 
343 	LIST_HEAD(, _emuxki_stream) streams;
344 
345 	uint8	timerstate;
346 	uint16	timerate;
347 #define	EMU_TIMER_STATE_ENABLED	1
348 	uint8	play_mode; // number of channels to be played : 2, 4, 6
349 	bool	digital_enabled;	// if digital is enabled and analog is disabled
350 
351 	emuxki_stream	*pstream;
352 	emuxki_stream	*pstream2;
353 	emuxki_stream	*rstream;
354 	emuxki_stream	*rstream2;
355 
356 	sem_id buffer_ready_sem;
357 
358 	midi_dev	midi;
359 
360 	/* mixer controls */
361 	emuxki_gpr	gpr[256];
362 	uint32 gpr_count;
363 
364 	/* multi_audio */
365 	multi_dev	multi;
366 } emuxki_dev;
367 
368 extern int32 num_cards;
369 extern emuxki_dev cards[NUM_CARDS];
370 
371 void emuxki_mem_free(emuxki_dev *card, void *ptr);
372 void * emuxki_pmem_alloc(emuxki_dev *card, size_t size);
373 void * emuxki_rmem_alloc(emuxki_dev *card, size_t size);
374 status_t emuxki_voice_commit_parms(emuxki_voice *voice);
375 status_t emuxki_voice_set_audioparms(emuxki_voice *voice, uint8 stereo,
376 			     uint8 b16, uint32 srate);
377 status_t emuxki_voice_set_recparms(emuxki_voice *voice, emuxki_recsrc_t recsrc,
378 			     	emuxki_recparams *recparams);
379 status_t emuxki_voice_set_bufparms(emuxki_voice *voice, void *ptr,
380 			   uint32 bufsize, uint16 blksize);
381 void emuxki_voice_start(emuxki_voice *voice);
382 void emuxki_voice_halt(emuxki_voice *voice);
383 emuxki_voice *emuxki_voice_new(emuxki_stream *stream, uint8 use, uint8 voicenum);
384 void emuxki_voice_delete(emuxki_voice *voice);
385 
386 status_t emuxki_stream_set_audioparms(emuxki_stream *stream, bool stereo, uint8 channels,
387 			     uint8 b16, uint32 sample_rate);
388 status_t emuxki_stream_set_recparms(emuxki_stream *stream, emuxki_recsrc_t recsrc,
389 			     	emuxki_recparams *recparams);
390 status_t emuxki_stream_commit_parms(emuxki_stream *stream);
391 status_t emuxki_stream_get_nth_buffer(emuxki_stream *stream, uint8 chan, uint8 buf,
392 					char** buffer, size_t *stride);
393 void emuxki_stream_start(emuxki_stream *stream, void (*inth) (void *), void *inthparam);
394 void emuxki_stream_halt(emuxki_stream *stream);
395 emuxki_stream *emuxki_stream_new(emuxki_dev *card, uint8 use, uint32 bufframes, uint8 bufcount);
396 void emuxki_stream_delete(emuxki_stream *stream);
397 
398 void emuxki_dump_fx(emuxki_dev * card);
399 void emuxki_gpr_dump(emuxki_dev * card, uint16 count);
400 void emuxki_gpr_set(emuxki_dev *card, emuxki_gpr *gpr, int32 type, float *values);
401 void emuxki_gpr_get(emuxki_dev *card, emuxki_gpr *gpr, int32 type, float *values);
402 
403 void emuxki_parameter_set(emuxki_dev *card, const void*, int32 type, int32 *value);
404 void emuxki_parameter_get(emuxki_dev *card, const void*, int32 type, int32 *value);
405 
406 extern void midi_interrupt_op(int32 op, void * data);
407 extern bool midi_interrupt(emuxki_dev *card);
408 
409 #endif
410 
411