1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * HD-audio bus
4 */
5#include <linux/init.h>
6#include <linux/device.h>
7#include <linux/module.h>
8#include <linux/mod_devicetable.h>
9#include <linux/export.h>
10#include <sound/hdaudio.h>
11
12MODULE_DESCRIPTION("HD-audio bus");
13MODULE_LICENSE("GPL");
14
15/**
16 * hdac_get_device_id - gets the hdac device id entry
17 * @hdev: HD-audio core device
18 * @drv: HD-audio codec driver
19 *
20 * Compares the hdac device vendor_id and revision_id to the hdac_device
21 * driver id_table and returns the matching device id entry.
22 */
23const struct hda_device_id *
24hdac_get_device_id(struct hdac_device *hdev, struct hdac_driver *drv)
25{
26	if (drv->id_table) {
27		const struct hda_device_id *id  = drv->id_table;
28
29		while (id->vendor_id) {
30			if (hdev->vendor_id == id->vendor_id &&
31				(!id->rev_id || id->rev_id == hdev->revision_id))
32				return id;
33			id++;
34		}
35	}
36
37	return NULL;
38}
39EXPORT_SYMBOL_GPL(hdac_get_device_id);
40
41static int hdac_codec_match(struct hdac_device *dev, struct hdac_driver *drv)
42{
43	if (hdac_get_device_id(dev, drv))
44		return 1;
45	else
46		return 0;
47}
48
49static int hda_bus_match(struct device *dev, struct device_driver *drv)
50{
51	struct hdac_device *hdev = dev_to_hdac_dev(dev);
52	struct hdac_driver *hdrv = drv_to_hdac_driver(drv);
53
54	if (hdev->type != hdrv->type)
55		return 0;
56
57	/*
58	 * if driver provided a match function use that otherwise we will
59	 * use hdac_codec_match function
60	 */
61	if (hdrv->match)
62		return hdrv->match(hdev, hdrv);
63	else
64		return hdac_codec_match(hdev, hdrv);
65	return 1;
66}
67
68static int hda_uevent(const struct device *dev, struct kobj_uevent_env *env)
69{
70	char modalias[32];
71
72	snd_hdac_codec_modalias(dev_to_hdac_dev(dev), modalias,
73				sizeof(modalias));
74	if (add_uevent_var(env, "MODALIAS=%s", modalias))
75		return -ENOMEM;
76	return 0;
77}
78
79const struct bus_type snd_hda_bus_type = {
80	.name = "hdaudio",
81	.match = hda_bus_match,
82	.uevent = hda_uevent,
83};
84EXPORT_SYMBOL_GPL(snd_hda_bus_type);
85
86static int __init hda_bus_init(void)
87{
88	return bus_register(&snd_hda_bus_type);
89}
90
91static void __exit hda_bus_exit(void)
92{
93	bus_unregister(&snd_hda_bus_type);
94}
95
96subsys_initcall(hda_bus_init);
97module_exit(hda_bus_exit);
98