xref: /haiku/src/add-ons/kernel/drivers/audio/hda/device.cpp (revision 4f2fd49bdc6078128b1391191e4edac647044c3d)
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  */
8 
9 
10 #include "driver.h"
11 
12 
13 static status_t
14 hda_open(const char *name, uint32 flags, void** cookie)
15 {
16 	hda_controller* controller = NULL;
17 
18 	for (uint32 i = 0; i < gNumCards; i++) {
19 		if (strcmp(gCards[i].devfs_path, name) == 0) {
20 			controller = &gCards[i];
21 			break;
22 		}
23 	}
24 
25 	if (controller == NULL)
26 		return ENODEV;
27 
28 	if (controller->opened)
29 		return B_BUSY;
30 
31 	status_t status = hda_hw_init(controller);
32 	if (status != B_OK)
33 		return status;
34 
35 	atomic_add(&controller->opened, 1);
36 
37 	*cookie = controller;
38 	return B_OK;
39 }
40 
41 
42 static status_t
43 hda_read(void* cookie, off_t position, void *buf, size_t* numBytes)
44 {
45 	*numBytes = 0;
46 		/* tell caller nothing was read */
47 	return B_IO_ERROR;
48 }
49 
50 
51 static status_t
52 hda_write(void* cookie, off_t position, const void* buffer, size_t* numBytes)
53 {
54 	*numBytes = 0;
55 		/* tell caller nothing was written */
56 	return B_IO_ERROR;
57 }
58 
59 
60 static status_t
61 hda_control(void* cookie, uint32 op, void* arg, size_t length)
62 {
63 	hda_controller* controller = (hda_controller*)cookie;
64 	if (controller->active_codec)
65 		return multi_audio_control(controller->active_codec, op, arg, length);
66 
67 	return B_BAD_VALUE;
68 }
69 
70 
71 static status_t
72 hda_close(void* cookie)
73 {
74 	hda_controller* controller = (hda_controller*)cookie;
75 	hda_hw_stop(controller);
76 	atomic_add(&controller->opened, -1);
77 
78 	return B_OK;
79 }
80 
81 
82 static status_t
83 hda_free(void* cookie)
84 {
85 	hda_controller* controller = (hda_controller*)cookie;
86 	hda_hw_uninit(controller);
87 
88 	return B_OK;
89 }
90 
91 
92 device_hooks gDriverHooks = {
93 	hda_open, 		/* -> open entry point */
94 	hda_close, 		/* -> close entry point */
95 	hda_free,		/* -> free cookie */
96 	hda_control, 	/* -> control entry point */
97 	hda_read,		/* -> read entry point */
98 	hda_write		/* -> write entry point */
99 };
100