xref: /haiku/src/add-ons/kernel/drivers/audio/hda/driver.h (revision 89755088d790ff4fe36f8aa77dacb2bd15507108)
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 #define PCI_hd_audio			3
34 
35 #define HDA_MAX_AUDIO_GROUPS	15
36 #define HDA_MAX_CODECS			15
37 #define HDA_MAX_STREAMS			16
38 #define MAX_CODEC_RESPONSES		10
39 #define MAX_INPUTS				32
40 #define MAX_IO_WIDGETS			8
41 
42 /* FIXME: Find out why we need so much! */
43 #define DEFAULT_FRAMES_PER_BUFFER	4096
44 
45 #define STREAM_MAX_BUFFERS	10
46 #define STREAM_MIN_BUFFERS	2
47 
48 enum {
49 	STREAM_PLAYBACK,
50 	STREAM_RECORD
51 };
52 
53 struct hda_codec;
54 
55 /*!	This structure describes a single stream of audio data,
56 	which is can have multiple channels (for stereo or better).
57 */
58 struct hda_stream {
59 	uint32		id;					/* HDA controller stream # */
60 	uint32		off;				/* HDA I/O/B descriptor offset */
61 	bool		running;			/* Is this stream active? */
62 	spinlock	lock;				/* Write lock */
63 	uint32		type;
64 
65 	uint32		pin_widget;			/* PIN Widget ID */
66 	uint32		io_widgets[MAX_IO_WIDGETS];	/* Input/Output Converter Widget ID */
67 	uint32		num_io_widgets;
68 
69 	uint32		sample_rate;
70 	uint32		sample_format;
71 
72 	uint32		num_buffers;
73 	uint32		num_channels;
74 	uint32		buffer_length;		/* size of buffer in samples */
75 	uint32		sample_size;
76 	uint8*		buffers[STREAM_MAX_BUFFERS];
77 					/* Virtual addresses for buffer */
78 	uint32		physical_buffers[STREAM_MAX_BUFFERS];
79 					/* Physical addresses for buffer */
80 	sem_id		buffer_ready_sem;
81 	bigtime_t	real_time;
82 	uint32		frames_count;
83 	uint32		buffer_cycle;
84 
85 	uint32		rate, bps;			/* Samplerate & bits per sample */
86 
87 	area_id		buffer_area;
88 	area_id		buffer_descriptors_area;
89 	uint32		physical_buffer_descriptors;	/* BDL physical address */
90 };
91 
92 struct hda_widget {
93 	uint32			node_id;
94 
95 	uint32			num_inputs;
96 	int32			active_input;
97 	uint32			inputs[MAX_INPUTS];
98 	uint32			flags;
99 
100 	hda_widget_type	type;
101 	uint32			pm;
102 
103 	struct {
104 		uint32		audio;
105 		uint32		output_amplifier;
106 		uint32		input_amplifier;
107 	} capabilities;
108 
109 	union {
110 		struct {
111 			uint32	formats;
112 			uint32	rates;
113 		} io;
114 		struct {
115 		} mixer;
116 		struct {
117 			uint32	output;
118 			uint32	input;
119 			pin_dev_type device;
120 		} pin;
121 	} d;
122 };
123 
124 #define WIDGET_FLAG_OUTPUT_PATH	0x01
125 #define WIDGET_FLAG_INPUT_PATH	0x02
126 
127 /*!	This structure describes a single Audio Function Group. An AFG
128 	is a group of audio widgets which can be used to configure multiple
129 	streams of audio either from the HDA Link to an output device (= playback)
130 	or from an input device to the HDA link (= recording).
131 */
132 struct hda_audio_group {
133 	hda_codec*		codec;
134 
135 	/* Multi Audio API data */
136 	hda_stream*		playback_stream;
137 	hda_stream*		record_stream;
138 
139 	uint32			root_node_id;
140 	uint32			widget_start;
141 	uint32			widget_count;
142 
143 	uint32			supported_formats;
144 	uint32			supported_rates;
145 	uint32			supported_pm;
146 
147 	uint32			input_amplifier_capabilities;
148 	uint32			output_amplifier_capabilities;
149 
150 	hda_widget*		widgets;
151 };
152 
153 /*!	This structure describes a single codec module in the
154 	HDA compliant device. This is a discrete component, which
155 	can contain both Audio Function Groups, Modem Function Groups,
156 	and other customized (vendor specific) Function Groups.
157 
158 	NOTE: ATM, only Audio Function Groups are supported.
159 */
160 struct hda_codec {
161 	uint16		vendor_id;
162 	uint16		product_id;
163 	uint8		revision;
164 	uint16		stepping;
165 	uint8		addr;
166 
167 	sem_id		response_sem;
168 	uint32		responses[MAX_CODEC_RESPONSES];
169 	uint32		response_count;
170 
171 	hda_audio_group* audio_groups[HDA_MAX_AUDIO_GROUPS];
172 	uint32		num_audio_groups;
173 
174 	struct hda_controller* controller;
175 };
176 
177 /*!	This structure describes a single HDA compliant
178 	controller. It contains a list of available streams
179 	for use by the codecs contained, and the messaging queue
180 	(verb/response) buffers for communication.
181 */
182 struct hda_controller {
183 	struct pci_info	pci_info;
184 	vint32			opened;
185 	const char*		devfs_path;
186 
187 	area_id			regs_area;
188 	vuint8*			regs;
189 	uint32			irq;
190 
191 	uint16			codec_status;
192 	uint32			num_input_streams;
193 	uint32			num_output_streams;
194 	uint32			num_bidir_streams;
195 
196 	uint32			corb_length;
197 	uint32			rirb_length;
198 	uint32			rirb_read_pos;
199 	uint32			corb_write_pos;
200 	area_id			corb_rirb_pos_area;
201 	corb_t*			corb;
202 	rirb_t*			rirb;
203 	uint32*			stream_positions;
204 
205 	hda_codec*		codecs[HDA_MAX_CODECS + 1];
206 	hda_codec*		active_codec;
207 	uint32			num_codecs;
208 
209 	hda_stream*		streams[HDA_MAX_STREAMS];
210 };
211 
212 /* driver.c */
213 extern device_hooks gDriverHooks;
214 extern pci_module_info* gPci;
215 extern hda_controller gCards[MAX_CARDS];
216 extern uint32 gNumCards;
217 
218 /* hda_codec.c */
219 status_t hda_audio_group_get_widgets(hda_audio_group* audioGroup,
220 	hda_stream* stream);
221 hda_codec* hda_codec_new(hda_controller* controller, uint32 cad);
222 void hda_codec_delete(hda_codec* codec);
223 
224 /* hda_multi_audio.c */
225 status_t multi_audio_control(void* cookie, uint32 op, void* arg, size_t length);
226 
227 /* hda_controller.c: Basic controller support */
228 status_t hda_hw_init(hda_controller* controller);
229 void hda_hw_stop(hda_controller* controller);
230 void hda_hw_uninit(hda_controller* controller);
231 status_t hda_send_verbs(hda_codec* codec, corb_t* verbs, uint32* responses,
232 	uint32 count);
233 
234 /* hda_controller.c: Stream support */
235 hda_stream* hda_stream_new(hda_audio_group* audioGroup, int type);
236 void hda_stream_delete(hda_stream* stream);
237 status_t hda_stream_setup_buffers(hda_audio_group* audioGroup,
238 	hda_stream* stream, const char* desc);
239 status_t hda_stream_start(hda_controller* controller, hda_stream* stream);
240 status_t hda_stream_stop(hda_controller* controller, hda_stream* stream);
241 
242 #endif	/* _HDA_H_ */
243