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
19struct VirtIOSoundDriverInfo {
20	device_node* 				node;
21	::virtio_device 			virtio_dev;
22	virtio_device_interface*	iface;
23	uint32						features;
24};
25
26struct VirtIOSoundHandle {
27    VirtIOSoundDriverInfo*		info;
28};
29
30static device_manager_info*		sDeviceManager;
31
32
33const char*
34get_feature_name(uint32 feature)
35{
36	// TODO: Implement this.
37	return NULL;
38}
39
40
41static float
42SupportsDevice(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
63static status_t
64InitDriver(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
78static void
79UninitDriver(void* cookie)
80{
81	free(cookie);
82}
83
84
85struct 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
99struct device_module_info sVirtioSoundDevice = {
100	{
101		VIRTIO_SOUND_DEVICE_MODULE_NAME,
102		0,
103		NULL
104	},
105};
106
107
108module_info* modules[] = {
109	(module_info*)&sVirtioSoundDriver,
110	(module_info*)&sVirtioSoundDevice,
111	NULL
112};
113
114
115module_dependency module_dependencies[] = {
116	{
117		B_DEVICE_MANAGER_MODULE_NAME,
118		(module_info**)&sDeviceManager
119	},
120	{}
121};
122