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