xref: /haiku/src/add-ons/kernel/drivers/audio/virtio/virtio_sound.cpp (revision dd2a1e350b303b855a50fd64e6cb55618be1ae6a)
1 /*
2  *  Copyright 2024, Diego Roux, diegoroux04 at proton dot me
3  *  Distributed under the terms of the MIT License.
4  */
5 
6 #include <fs/devfs.h>
7 #include <virtio.h>
8 
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 
13 
14 #define VIRTIO_SOUND_DRIVER_MODULE_NAME 	"drivers/audio/virtio/driver_v1"
15 #define VIRTIO_SOUND_DEVICE_MODULE_NAME 	"drivers/audio/virtio/device_v1"
16 #define VIRTIO_SOUND_DEVICE_ID_GEN 			"virtio_sound/device_id"
17 
18 
19 struct VirtIOSoundDriverInfo {
20 	device_node* 				node;
21 	::virtio_device 			virtio_dev;
22 	virtio_device_interface*	iface;
23 	uint32						features;
24 };
25 
26 struct VirtIOSoundHandle {
27     VirtIOSoundDriverInfo*		info;
28 };
29 
30 static device_manager_info*		sDeviceManager;
31 
32 
33 const char*
34 get_feature_name(uint32 feature)
35 {
36 	// TODO: Implement this.
37 	return NULL;
38 }
39 
40 
41 static float
42 SupportsDevice(device_node* parent)
43 {
44 	uint16 deviceType;
45 	const char* bus;
46 
47 	if (sDeviceManager->get_attr_string(parent, B_DEVICE_BUS, &bus, false) != B_OK
48 		|| sDeviceManager->get_attr_uint16(parent, VIRTIO_DEVICE_TYPE_ITEM,
49 			&deviceType, true) != B_OK) {
50 		return 0.0f;
51 	}
52 
53 	if (strcmp(bus, "virtio") != 0)
54 		return 0.0f;
55 
56 	if (deviceType != VIRTIO_DEVICE_ID_SOUND)
57 		return 0.0f;
58 
59 	return 1.0f;
60 }
61 
62 
63 static status_t
64 InitDriver(device_node* node, void** cookie)
65 {
66 	VirtIOSoundDriverInfo* info = (VirtIOSoundDriverInfo*)malloc(sizeof(VirtIOSoundDriverInfo));
67 
68 	if (info == NULL)
69 		return B_NO_MEMORY;
70 
71 	info->node = node;
72 	*cookie = info;
73 
74 	return B_OK;
75 }
76 
77 
78 static void
79 UninitDriver(void* cookie)
80 {
81 	free(cookie);
82 }
83 
84 
85 struct driver_module_info sVirtioSoundDriver = {
86 	{
87 		VIRTIO_SOUND_DRIVER_MODULE_NAME,
88 		0,
89 		NULL
90 	},
91 
92 	.supports_device = SupportsDevice,
93 
94 	.init_driver = InitDriver,
95 	.uninit_driver = UninitDriver,
96 };
97 
98 
99 struct device_module_info sVirtioSoundDevice = {
100 	{
101 		VIRTIO_SOUND_DEVICE_MODULE_NAME,
102 		0,
103 		NULL
104 	},
105 };
106 
107 
108 module_info* modules[] = {
109 	(module_info*)&sVirtioSoundDriver,
110 	(module_info*)&sVirtioSoundDevice,
111 	NULL
112 };
113 
114 
115 module_dependency module_dependencies[] = {
116 	{
117 		B_DEVICE_MANAGER_MODULE_NAME,
118 		(module_info**)&sDeviceManager
119 	},
120 	{}
121 };
122