xref: /haiku/src/add-ons/kernel/drivers/audio/hda/driver.h (revision c9ad965c81b08802fed0827fd1dd16f45297928a)
1 /*
2  * Copyright 2007-2008, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Ithamar Adema, ithamar AT unet DOT nl
7  *		Axel Dörfler, axeld@pinc-software.de
8  */
9 #ifndef _HDA_H_
10 #define _HDA_H_
11 
12 #include <KernelExport.h>
13 #include <Drivers.h>
14 #include <PCI.h>
15 
16 #include <string.h>
17 #include <stdlib.h>
18 
19 #ifndef HAIKU_TARGET_PLATFORM_HAIKU
20 #	define DEVFS_PATH_FORMAT	"audio/multi/hda/%lu"
21 #	include <multi_audio.h>
22 #else
23 #	define DEVFS_PATH_FORMAT	"audio/hmulti/hda/%lu"
24 #	include <hmulti_audio.h>
25 #endif
26 
27 #include "hda_controller_defs.h"
28 #include "hda_codec_defs.h"
29 
30 #define MAX_CARDS				4
31 
32 /* values for the class_sub field for class_base = 0x04 (multimedia device) */
33 #ifndef __HAIKU__
34 #	define PCI_hd_audio			3
35 #endif
36 
37 #define HDA_MAX_AUDIO_GROUPS	15
38 #define HDA_MAX_CODECS			15
39 #define HDA_MAX_STREAMS			16
40 #define MAX_CODEC_RESPONSES		16
41 #define MAX_INPUTS				32
42 #define MAX_IO_WIDGETS			8
43 #define MAX_ASSOCIATIONS		16
44 #define MAX_ASSOCIATION_PINS	16
45 
46 #define STREAM_MAX_BUFFERS	10
47 #define STREAM_MIN_BUFFERS	2
48 
49 enum {
50 	STREAM_PLAYBACK,
51 	STREAM_RECORD
52 };
53 
54 struct hda_codec;
55 struct hda_stream;
56 struct hda_multi;
57 
58 /*!	This structure describes a single HDA compliant
59 	controller. It contains a list of available streams
60 	for use by the codecs contained, and the messaging queue
61 	(verb/response) buffers for communication.
62 */
63 struct hda_controller {
64 	struct pci_info	pci_info;
65 	vint32			opened;
66 	const char*		devfs_path;
67 
68 	area_id			regs_area;
69 	vuint8*			regs;
70 	uint32			irq;
71 
72 	uint16			codec_status;
73 	uint32			num_input_streams;
74 	uint32			num_output_streams;
75 	uint32			num_bidir_streams;
76 
77 	uint32			corb_length;
78 	uint32			rirb_length;
79 	uint32			rirb_read_pos;
80 	uint32			corb_write_pos;
81 	area_id			corb_rirb_pos_area;
82 	corb_t*			corb;
83 	rirb_t*			rirb;
84 	uint32*			stream_positions;
85 
86 	hda_codec*		codecs[HDA_MAX_CODECS + 1];
87 	hda_codec*		active_codec;
88 	uint32			num_codecs;
89 
90 	hda_stream*		streams[HDA_MAX_STREAMS];
91 	sem_id			buffer_ready_sem;
92 
93 	uint8 Read8(uint32 reg)
94 	{
95 		return *(regs + reg);
96 	}
97 
98 	uint16 Read16(uint32 reg)
99 	{
100 		return *(vuint16*)(regs + reg);
101 	}
102 
103 	uint32 Read32(uint32 reg)
104 	{
105 		return *(vuint32*)(regs + reg);
106 	}
107 
108 	void Write8(uint32 reg, uint8 value)
109 	{
110 		*(regs + reg) = value;
111 	}
112 
113 	void Write16(uint32 reg, uint16 value)
114 	{
115 		*(vuint16*)(regs + reg) = value;
116 	}
117 
118 	void Write32(uint32 reg, uint32 value)
119 	{
120 		*(vuint32*)(regs + reg) = value;
121 	}
122 };
123 
124 /*!	This structure describes a single stream of audio data,
125 	which is can have multiple channels (for stereo or better).
126 */
127 struct hda_stream {
128 	uint32		id;					/* HDA controller stream # */
129 	uint32		offset;				/* HDA I/O/B descriptor offset */
130 	bool		running;
131 	spinlock	lock;				/* Write lock */
132 	uint32		type;
133 	int32		warn_count;
134 
135 	hda_controller* controller;
136 
137 	uint32		pin_widget;			/* PIN Widget ID */
138 	uint32		io_widgets[MAX_IO_WIDGETS];	/* Input/Output Converter Widget ID */
139 	uint32		num_io_widgets;
140 
141 	uint32		sample_rate;
142 	uint32		sample_format;
143 
144 	uint32		num_buffers;
145 	uint32		num_channels;
146 	uint32		buffer_length;		/* size of buffer in samples */
147 	uint32		sample_size;
148 	uint8*		buffers[STREAM_MAX_BUFFERS];
149 					/* Virtual addresses for buffer */
150 	uint32		physical_buffers[STREAM_MAX_BUFFERS];
151 					/* Physical addresses for buffer */
152 
153 	volatile bigtime_t	real_time;
154 	volatile uint64		frames_count;
155 	volatile int32		buffer_cycle;
156 
157 	uint32		rate, bps;			/* Samplerate & bits per sample */
158 
159 	area_id		buffer_area;
160 	area_id		buffer_descriptors_area;
161 	uint32		physical_buffer_descriptors;	/* BDL physical address */
162 
163 	uint8 Read8(uint32 reg)
164 	{
165 		return controller->Read8(HDAC_STREAM_BASE + offset + reg);
166 	}
167 
168 	uint16 Read16(uint32 reg)
169 	{
170 		return controller->Read16(HDAC_STREAM_BASE + offset + reg);
171 	}
172 
173 	uint32 Read32(uint32 reg)
174 	{
175 		return controller->Read32(HDAC_STREAM_BASE + offset + reg);
176 	}
177 
178 	void Write8(uint32 reg, uint8 value)
179 	{
180 		*(controller->regs + HDAC_STREAM_BASE + offset + reg) = value;
181 	}
182 
183 	void Write16(uint32 reg, uint16 value)
184 	{
185 		*(vuint16*)(controller->regs + HDAC_STREAM_BASE + offset + reg) = value;
186 	}
187 
188 	void Write32(uint32 reg, uint32 value)
189 	{
190 		*(vuint32*)(controller->regs + HDAC_STREAM_BASE + offset + reg) = value;
191 	}
192 };
193 
194 struct hda_widget {
195 	uint32			node_id;
196 
197 	uint32			num_inputs;
198 	int32			active_input;
199 	uint32			inputs[MAX_INPUTS];
200 	uint32			flags;
201 
202 	hda_widget_type	type;
203 	uint32			pm;
204 
205 	struct {
206 		uint32		audio;
207 		uint32		output_amplifier;
208 		uint32		input_amplifier;
209 	} capabilities;
210 
211 	union {
212 		struct {
213 			uint32	formats;
214 			uint32	rates;
215 		} io;
216 		struct {
217 		} mixer;
218 		struct {
219 			uint32	capabilities;
220 			uint32	config;
221 		} pin;
222 	} d;
223 };
224 
225 struct hda_association {
226 	uint32	index;
227 	bool	enabled;
228 	uint32 	pin_count;
229 	uint32 	pins[MAX_ASSOCIATION_PINS];
230 };
231 
232 #define WIDGET_FLAG_OUTPUT_PATH	0x01
233 #define WIDGET_FLAG_INPUT_PATH	0x02
234 #define WIDGET_FLAG_WIDGET_PATH	0x04
235 
236 /*!	This structure describes a single Audio Function Group. An AFG
237 	is a group of audio widgets which can be used to configure multiple
238 	streams of audio either from the HDA Link to an output device (= playback)
239 	or from an input device to the HDA link (= recording).
240 */
241 struct hda_audio_group {
242 	hda_codec*		codec;
243 	hda_widget		widget;
244 
245 	/* Multi Audio API data */
246 	hda_stream*		playback_stream;
247 	hda_stream*		record_stream;
248 
249 	uint32			widget_start;
250 	uint32			widget_count;
251 
252 	uint32			association_count;
253 
254 	hda_widget*		widgets;
255 	hda_association		associations[MAX_ASSOCIATIONS];
256 
257 	hda_multi*		multi;
258 };
259 
260 /*!	This structure describes a single codec module in the
261 	HDA compliant device. This is a discrete component, which
262 	can contain both Audio Function Groups, Modem Function Groups,
263 	and other customized (vendor specific) Function Groups.
264 
265 	NOTE: ATM, only Audio Function Groups are supported.
266 */
267 struct hda_codec {
268 	uint16		vendor_id;
269 	uint16		product_id;
270 	uint8		major;
271 	uint8		minor;
272 	uint8		revision;
273 	uint8		stepping;
274 	uint8		addr;
275 
276 	sem_id		response_sem;
277 	uint32		responses[MAX_CODEC_RESPONSES];
278 	uint32		response_count;
279 
280 	hda_audio_group* audio_groups[HDA_MAX_AUDIO_GROUPS];
281 	uint32		num_audio_groups;
282 
283 	struct hda_controller* controller;
284 };
285 
286 
287 #define MULTI_CONTROL_FIRSTID	1024
288 #define MULTI_CONTROL_MASTERID	0
289 #define MULTI_MAX_CONTROLS 128
290 #define MULTI_MAX_CHANNELS 128
291 
292 struct hda_multi_mixer_control {
293 	hda_multi	*multi;
294 	int32 	nid;
295 	int32 type;
296 	bool input;
297 	uint32 mute;
298 	uint32 gain;
299 	uint32 capabilities;
300 	int32 index;
301 	multi_mix_control	mix_control;
302 };
303 
304 
305 struct hda_multi {
306 	hda_audio_group *group;
307 	hda_multi_mixer_control controls[MULTI_MAX_CONTROLS];
308 	uint32 control_count;
309 
310 	multi_channel_info chans[MULTI_MAX_CHANNELS];
311 	uint32 output_channel_count;
312 	uint32 input_channel_count;
313 	uint32 output_bus_channel_count;
314 	uint32 input_bus_channel_count;
315 	uint32 aux_bus_channel_count;
316 };
317 
318 
319 /* driver.c */
320 extern device_hooks gDriverHooks;
321 extern pci_module_info* gPci;
322 extern hda_controller gCards[MAX_CARDS];
323 extern uint32 gNumCards;
324 
325 /* hda_codec.c */
326 hda_widget* hda_audio_group_get_widget(hda_audio_group* audioGroup, uint32 nodeID);
327 
328 status_t hda_audio_group_get_widgets(hda_audio_group* audioGroup,
329 	hda_stream* stream);
330 hda_codec* hda_codec_new(hda_controller* controller, uint32 cad);
331 void hda_codec_delete(hda_codec* codec);
332 
333 /* hda_multi_audio.c */
334 status_t multi_audio_control(void* cookie, uint32 op, void* arg, size_t length);
335 
336 /* hda_controller.c: Basic controller support */
337 status_t hda_hw_init(hda_controller* controller);
338 void hda_hw_stop(hda_controller* controller);
339 void hda_hw_uninit(hda_controller* controller);
340 status_t hda_send_verbs(hda_codec* codec, corb_t* verbs, uint32* responses,
341 	uint32 count);
342 
343 /* hda_controller.c: Stream support */
344 hda_stream* hda_stream_new(hda_audio_group* audioGroup, int type);
345 void hda_stream_delete(hda_stream* stream);
346 status_t hda_stream_setup_buffers(hda_audio_group* audioGroup,
347 	hda_stream* stream, const char* desc);
348 status_t hda_stream_start(hda_controller* controller, hda_stream* stream);
349 status_t hda_stream_stop(hda_controller* controller, hda_stream* stream);
350 
351 #endif	/* _HDA_H_ */
352