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 
12 MODULE_DESCRIPTION("HD-audio bus");
13 MODULE_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  */
23 const struct hda_device_id *
hdac_get_device_id(struct hdac_device * hdev,struct hdac_driver * drv)24 hdac_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 }
39 EXPORT_SYMBOL_GPL(hdac_get_device_id);
40 
hdac_codec_match(struct hdac_device * dev,struct hdac_driver * drv)41 static 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 
hda_bus_match(struct device * dev,struct device_driver * drv)49 static 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 
hda_uevent(struct device * dev,struct kobj_uevent_env * env)68 static int hda_uevent(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 
79 struct bus_type snd_hda_bus_type = {
80 	.name = "hdaudio",
81 	.match = hda_bus_match,
82 	.uevent = hda_uevent,
83 };
84 EXPORT_SYMBOL_GPL(snd_hda_bus_type);
85 
hda_bus_init(void)86 static int __init hda_bus_init(void)
87 {
88 	return bus_register(&snd_hda_bus_type);
89 }
90 
hda_bus_exit(void)91 static void __exit hda_bus_exit(void)
92 {
93 	bus_unregister(&snd_hda_bus_type);
94 }
95 
96 subsys_initcall(hda_bus_init);
97 module_exit(hda_bus_exit);
98