ac97_bus.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Linux driver model AC97 bus interface
  4. *
  5. * Author: Nicolas Pitre
  6. * Created: Jan 14, 2005
  7. * Copyright: (C) MontaVista Software Inc.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/device.h>
  12. #include <linux/string.h>
  13. #include <sound/ac97_codec.h>
  14. /*
  15. * snd_ac97_check_id() - Reads and checks the vendor ID of the device
  16. * @ac97: The AC97 device to check
  17. * @id: The ID to compare to
  18. * @id_mask: Mask that is applied to the device ID before comparing to @id
  19. *
  20. * If @id is 0 this function returns true if the read device vendor ID is
  21. * a valid ID. If @id is non 0 this functions returns true if @id
  22. * matches the read vendor ID. Otherwise the function returns false.
  23. */
  24. static bool snd_ac97_check_id(struct snd_ac97 *ac97, unsigned int id,
  25. unsigned int id_mask)
  26. {
  27. ac97->id = ac97->bus->ops->read(ac97, AC97_VENDOR_ID1) << 16;
  28. ac97->id |= ac97->bus->ops->read(ac97, AC97_VENDOR_ID2);
  29. if (ac97->id == 0x0 || ac97->id == 0xffffffff)
  30. return false;
  31. if (id != 0 && id != (ac97->id & id_mask))
  32. return false;
  33. return true;
  34. }
  35. /**
  36. * snd_ac97_reset() - Reset AC'97 device
  37. * @ac97: The AC'97 device to reset
  38. * @try_warm: Try a warm reset first
  39. * @id: Expected device vendor ID
  40. * @id_mask: Mask that is applied to the device ID before comparing to @id
  41. *
  42. * This function resets the AC'97 device. If @try_warm is true the function
  43. * first performs a warm reset. If @try_warm is false the function issues
  44. * cold reset followed by a warm reset. If @id is 0 any valid device ID
  45. * will be accepted, otherwise only the ID that matches @id and @id_mask
  46. * is accepted.
  47. * Returns:
  48. * * %1 - if warm reset is successful
  49. * * %0 - if cold reset and warm reset is successful
  50. * * %-ENODEV - if @id and @id_mask not matching
  51. */
  52. int snd_ac97_reset(struct snd_ac97 *ac97, bool try_warm, unsigned int id,
  53. unsigned int id_mask)
  54. {
  55. const struct snd_ac97_bus_ops *ops = ac97->bus->ops;
  56. if (try_warm && ops->warm_reset) {
  57. ops->warm_reset(ac97);
  58. if (snd_ac97_check_id(ac97, id, id_mask))
  59. return 1;
  60. }
  61. if (ops->reset)
  62. ops->reset(ac97);
  63. if (ops->warm_reset)
  64. ops->warm_reset(ac97);
  65. if (snd_ac97_check_id(ac97, id, id_mask))
  66. return 0;
  67. return -ENODEV;
  68. }
  69. EXPORT_SYMBOL_GPL(snd_ac97_reset);
  70. const struct bus_type ac97_bus_type = {
  71. .name = "ac97",
  72. };
  73. static int __init ac97_bus_init(void)
  74. {
  75. return bus_register(&ac97_bus_type);
  76. }
  77. subsys_initcall(ac97_bus_init);
  78. static void __exit ac97_bus_exit(void)
  79. {
  80. bus_unregister(&ac97_bus_type);
  81. }
  82. module_exit(ac97_bus_exit);
  83. EXPORT_SYMBOL(ac97_bus_type);
  84. MODULE_DESCRIPTION("Legacy AC97 bus interface");
  85. MODULE_LICENSE("GPL");