2 * Copyright 2007-2012, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
6 * Ithamar Adema, ithamar AT unet DOT nl
7 * Axel Dörfler, axeld@pinc-software.de
12 #include <KernelExport.h>
20 #ifndef HAIKU_TARGET_PLATFORM_HAIKU
21 # define DEVFS_PATH_FORMAT "audio/multi/hda/%lu"
22 # include <multi_audio.h>
24 # define DEVFS_PATH_FORMAT "audio/hmulti/hda/%lu"
25 # include <hmulti_audio.h>
28 #include "hda_controller_defs.h"
29 #include "hda_codec_defs.h"
33 /* values for the class_sub field for class_base = 0x04 (multimedia device) */
35 # define PCI_hd_audio 3
38 #define HDA_MAX_AUDIO_GROUPS 15
39 #define HDA_MAX_CODECS 15
40 #define HDA_MAX_STREAMS 16
41 #define MAX_CODEC_RESPONSES 16
42 #define MAX_CODEC_UNSOL_RESPONSES 16
44 #define MAX_IO_WIDGETS 8
45 #define MAX_ASSOCIATIONS 16
46 #define MAX_ASSOCIATION_PINS 16
48 #define STREAM_MAX_BUFFERS 10
49 #define STREAM_MIN_BUFFERS 2
61 /*! This structure describes a single HDA compliant
62 controller. It contains a list of available streams
63 for use by the codecs contained, and the messaging queue
64 (verb/response) buffers for communication.
66 struct hda_controller
{
67 struct pci_info pci_info
;
69 const char* devfs_path
;
78 uint32 num_input_streams
;
79 uint32 num_output_streams
;
80 uint32 num_bidir_streams
;
85 uint32 corb_write_pos
;
86 area_id corb_rirb_pos_area
;
89 uint32
* stream_positions
;
91 hda_codec
* codecs
[HDA_MAX_CODECS
+ 1];
92 hda_codec
* active_codec
;
95 hda_stream
* streams
[HDA_MAX_STREAMS
];
96 sem_id buffer_ready_sem
;
98 uint8
Read8(uint32 reg
)
100 return *(regs
+ reg
);
103 uint16
Read16(uint32 reg
)
105 return *(vuint16
*)(regs
+ reg
);
108 uint32
Read32(uint32 reg
)
110 return *(vuint32
*)(regs
+ reg
);
113 void Write8(uint32 reg
, uint8 value
)
115 *(regs
+ reg
) = value
;
118 void Write16(uint32 reg
, uint16 value
)
120 *(vuint16
*)(regs
+ reg
) = value
;
123 void Write32(uint32 reg
, uint32 value
)
125 *(vuint32
*)(regs
+ reg
) = value
;
128 void ReadModifyWrite8(uint32 reg
, uint8 mask
, uint8 value
)
130 uint8 temp
= Read8(reg
);
136 void ReadModifyWrite16(uint32 reg
, uint16 mask
, uint16 value
)
138 uint16 temp
= Read16(reg
);
144 void ReadModifyWrite32(uint32 reg
, uint32 mask
, uint32 value
)
146 uint32 temp
= Read32(reg
);
153 /*! This structure describes a single stream of audio data,
154 which is can have multiple channels (for stereo or better).
157 uint32 id
; /* HDA controller stream # */
158 uint32 offset
; /* HDA I/O/B descriptor offset */
160 spinlock lock
; /* Write lock */
163 hda_controller
* controller
;
165 uint32 pin_widget
; /* PIN Widget ID */
166 uint32 io_widgets
[MAX_IO_WIDGETS
]; /* Input/Output Converter Widget ID */
167 uint32 num_io_widgets
;
170 uint32 sample_format
;
174 uint32 buffer_length
; /* size of buffer in samples */
175 uint32 buffer_size
; /* actual (aligned) size of buffer in bytes */
177 uint8
* buffers
[STREAM_MAX_BUFFERS
];
178 /* Virtual addresses for buffer */
179 phys_addr_t physical_buffers
[STREAM_MAX_BUFFERS
];
180 /* Physical addresses for buffer */
182 volatile bigtime_t real_time
;
183 volatile uint64 frames_count
;
184 uint32 last_link_frame_position
;
185 volatile int32 buffer_cycle
;
187 uint32 rate
, bps
; /* Samplerate & bits per sample */
190 area_id buffer_descriptors_area
;
191 phys_addr_t physical_buffer_descriptors
; /* BDL physical address */
193 int32 incorrect_position_count
;
194 bool use_dma_position
;
196 uint8
Read8(uint32 reg
)
198 return controller
->Read8(HDAC_STREAM_BASE
+ offset
+ reg
);
201 uint16
Read16(uint32 reg
)
203 return controller
->Read16(HDAC_STREAM_BASE
+ offset
+ reg
);
206 uint32
Read32(uint32 reg
)
208 return controller
->Read32(HDAC_STREAM_BASE
+ offset
+ reg
);
211 void Write8(uint32 reg
, uint8 value
)
213 *(controller
->regs
+ HDAC_STREAM_BASE
+ offset
+ reg
) = value
;
216 void Write16(uint32 reg
, uint16 value
)
218 *(vuint16
*)(controller
->regs
+ HDAC_STREAM_BASE
+ offset
+ reg
) = value
;
221 void Write32(uint32 reg
, uint32 value
)
223 *(vuint32
*)(controller
->regs
+ HDAC_STREAM_BASE
+ offset
+ reg
) = value
;
232 uint32 inputs
[MAX_INPUTS
];
235 hda_widget_type type
;
240 uint32 output_amplifier
;
241 uint32 input_amplifier
;
258 struct hda_association
{
262 uint32 pins
[MAX_ASSOCIATION_PINS
];
265 #define WIDGET_FLAG_OUTPUT_PATH 0x01
266 #define WIDGET_FLAG_INPUT_PATH 0x02
267 #define WIDGET_FLAG_WIDGET_PATH 0x04
269 /*! This structure describes a single Audio Function Group. An AFG
270 is a group of audio widgets which can be used to configure multiple
271 streams of audio either from the HDA Link to an output device (= playback)
272 or from an input device to the HDA link (= recording).
274 struct hda_audio_group
{
278 /* Multi Audio API data */
279 hda_stream
* playback_stream
;
280 hda_stream
* record_stream
;
285 uint32 association_count
;
289 hda_association associations
[MAX_ASSOCIATIONS
];
294 /*! This structure describes a single codec module in the
295 HDA compliant device. This is a discrete component, which
296 can contain both Audio Function Groups, Modem Function Groups,
297 and other customized (vendor specific) Function Groups.
299 NOTE: ATM, only Audio Function Groups are supported.
313 uint32 responses
[MAX_CODEC_RESPONSES
];
314 uint32 response_count
;
316 sem_id unsol_response_sem
;
317 thread_id unsol_response_thread
;
318 uint32 unsol_responses
[MAX_CODEC_UNSOL_RESPONSES
];
319 uint32 unsol_response_read
, unsol_response_write
;
321 hda_audio_group
* audio_groups
[HDA_MAX_AUDIO_GROUPS
];
322 uint32 num_audio_groups
;
324 struct hda_controller
* controller
;
328 #define MULTI_CONTROL_FIRSTID 1024
329 #define MULTI_CONTROL_MASTERID 0
330 #define MULTI_MAX_CONTROLS 128
331 #define MULTI_MAX_CHANNELS 128
333 struct hda_multi_mixer_control
{
342 multi_mix_control mix_control
;
347 hda_audio_group
*group
;
348 hda_multi_mixer_control controls
[MULTI_MAX_CONTROLS
];
349 uint32 control_count
;
351 multi_channel_info chans
[MULTI_MAX_CHANNELS
];
352 uint32 output_channel_count
;
353 uint32 input_channel_count
;
354 uint32 output_bus_channel_count
;
355 uint32 input_bus_channel_count
;
356 uint32 aux_bus_channel_count
;
361 extern device_hooks gDriverHooks
;
362 extern pci_module_info
* gPci
;
363 extern pci_x86_module_info
* gPCIx86Module
;
364 extern hda_controller gCards
[MAX_CARDS
];
365 extern uint32 gNumCards
;
368 const char* get_widget_location(uint32 location
);
369 hda_widget
* hda_audio_group_get_widget(hda_audio_group
* audioGroup
, uint32 nodeID
);
371 status_t
hda_audio_group_get_widgets(hda_audio_group
* audioGroup
,
373 hda_codec
* hda_codec_new(hda_controller
* controller
, uint32 cad
);
374 void hda_codec_delete(hda_codec
* codec
);
376 /* hda_multi_audio.c */
377 status_t
multi_audio_control(void* cookie
, uint32 op
, void* arg
, size_t length
);
379 /* hda_controller.c: Basic controller support */
380 status_t
hda_hw_init(hda_controller
* controller
);
381 void hda_hw_stop(hda_controller
* controller
);
382 void hda_hw_uninit(hda_controller
* controller
);
383 status_t
hda_send_verbs(hda_codec
* codec
, corb_t
* verbs
, uint32
* responses
,
385 status_t
hda_verb_write(hda_codec
* codec
, uint32 nid
, uint32 vid
, uint16 payload
);
386 status_t
hda_verb_read(hda_codec
* codec
, uint32 nid
, uint32 vid
, uint32
*response
);
388 /* hda_controller.c: Stream support */
389 hda_stream
* hda_stream_new(hda_audio_group
* audioGroup
, int type
);
390 void hda_stream_delete(hda_stream
* stream
);
391 status_t
hda_stream_setup_buffers(hda_audio_group
* audioGroup
,
392 hda_stream
* stream
, const char* desc
);
393 status_t
hda_stream_start(hda_controller
* controller
, hda_stream
* stream
);
394 status_t
hda_stream_stop(hda_controller
* controller
, hda_stream
* stream
);