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