hda_bus_type.c 2.1 KB

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