144f16897SAxel Dörfler/*
241828f15SAxel Dörfler * Copyright 2007-2012, Haiku, Inc. All Rights Reserved.
344f16897SAxel Dörfler * Distributed under the terms of the MIT License.
444f16897SAxel Dörfler *
544f16897SAxel Dörfler * Authors:
644f16897SAxel Dörfler *		Ithamar Adema, ithamar AT unet DOT nl
744f16897SAxel Dörfler */
844f16897SAxel Dörfler
944f16897SAxel Dörfler
1089db0a5bSIthamar R. Adema#include "driver.h"
1189db0a5bSIthamar R. Adema
1244f16897SAxel Dörfler
1389db0a5bSIthamar R. Ademastatic status_t
1441828f15SAxel Dörflerhda_open(const char* name, uint32 flags, void** cookie)
1589db0a5bSIthamar R. Adema{
1644f16897SAxel Dörfler	hda_controller* controller = NULL;
1789db0a5bSIthamar R. Adema
18340dd4feSAxel Dörfler	for (uint32 i = 0; i < gNumCards; i++) {
1944f16897SAxel Dörfler		if (strcmp(gCards[i].devfs_path, name) == 0) {
2044f16897SAxel Dörfler			controller = &gCards[i];
2144f16897SAxel Dörfler			break;
2289db0a5bSIthamar R. Adema		}
2389db0a5bSIthamar R. Adema	}
2444f16897SAxel Dörfler
2544f16897SAxel Dörfler	if (controller == NULL)
2689db0a5bSIthamar R. Adema		return ENODEV;
2744f16897SAxel Dörfler
2873ad2473SPawel Dziepak	if (atomic_get(&controller->opened) != 0)
2989db0a5bSIthamar R. Adema		return B_BUSY;
3089db0a5bSIthamar R. Adema
31340dd4feSAxel Dörfler	status_t status = hda_hw_init(controller);
32340dd4feSAxel Dörfler	if (status != B_OK)
33340dd4feSAxel Dörfler		return status;
3489db0a5bSIthamar R. Adema
35340dd4feSAxel Dörfler	atomic_add(&controller->opened, 1);
36afc104a0SIthamar R. Adema
3744f16897SAxel Dörfler	*cookie = controller;
388aea77d0SHumdinger
398aea77d0SHumdinger	// optional user-settable buffer frames and count
408aea77d0SHumdinger	get_settings_from_file();
418aea77d0SHumdinger
4289db0a5bSIthamar R. Adema	return B_OK;
4389db0a5bSIthamar R. Adema}
4489db0a5bSIthamar R. Adema
4544f16897SAxel Dörfler
4689db0a5bSIthamar R. Ademastatic status_t
4741828f15SAxel Dörflerhda_read(void* cookie, off_t position, void* buffer, size_t* numBytes)
4889db0a5bSIthamar R. Adema{
4944f16897SAxel Dörfler	*numBytes = 0;
5089db0a5bSIthamar R. Adema	return B_IO_ERROR;
5189db0a5bSIthamar R. Adema}
5289db0a5bSIthamar R. Adema
5344f16897SAxel Dörfler
5489db0a5bSIthamar R. Ademastatic status_t
5544f16897SAxel Dörflerhda_write(void* cookie, off_t position, const void* buffer, size_t* numBytes)
5689db0a5bSIthamar R. Adema{
5744f16897SAxel Dörfler	*numBytes = 0;
5889db0a5bSIthamar R. Adema	return B_IO_ERROR;
5989db0a5bSIthamar R. Adema}
6089db0a5bSIthamar R. Adema
6144f16897SAxel Dörfler
6289db0a5bSIthamar R. Ademastatic status_t
6344f16897SAxel Dörflerhda_control(void* cookie, uint32 op, void* arg, size_t length)
6489db0a5bSIthamar R. Adema{
6544f16897SAxel Dörfler	hda_controller* controller = (hda_controller*)cookie;
6641828f15SAxel Dörfler	if (controller->active_codec != NULL)
6744f16897SAxel Dörfler		return multi_audio_control(controller->active_codec, op, arg, length);
68afc104a0SIthamar R. Adema
6989db0a5bSIthamar R. Adema	return B_BAD_VALUE;
7089db0a5bSIthamar R. Adema}
7189db0a5bSIthamar R. Adema
7244f16897SAxel Dörfler
7389db0a5bSIthamar R. Ademastatic status_t
7444f16897SAxel Dörflerhda_close(void* cookie)
7589db0a5bSIthamar R. Adema{
7644f16897SAxel Dörfler	hda_controller* controller = (hda_controller*)cookie;
7744f16897SAxel Dörfler	hda_hw_stop(controller);
78340dd4feSAxel Dörfler	atomic_add(&controller->opened, -1);
7989db0a5bSIthamar R. Adema
8089db0a5bSIthamar R. Adema	return B_OK;
8189db0a5bSIthamar R. Adema}
8289db0a5bSIthamar R. Adema
8344f16897SAxel Dörfler
8489db0a5bSIthamar R. Ademastatic status_t
8544f16897SAxel Dörflerhda_free(void* cookie)
8689db0a5bSIthamar R. Adema{
8744f16897SAxel Dörfler	hda_controller* controller = (hda_controller*)cookie;
8844f16897SAxel Dörfler	hda_hw_uninit(controller);
8989db0a5bSIthamar R. Adema
9089db0a5bSIthamar R. Adema	return B_OK;
9189db0a5bSIthamar R. Adema}
9289db0a5bSIthamar R. Adema
9344f16897SAxel Dörfler
9444f16897SAxel Dörflerdevice_hooks gDriverHooks = {
9541828f15SAxel Dörfler	hda_open,
9641828f15SAxel Dörfler	hda_close,
9741828f15SAxel Dörfler	hda_free,
9841828f15SAxel Dörfler	hda_control,
9941828f15SAxel Dörfler	hda_read,
10041828f15SAxel Dörfler	hda_write
10189db0a5bSIthamar R. Adema};
102