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