auxiliary_sysfs.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2024, NVIDIA CORPORATION & AFFILIATES
  4. */
  5. #include <linux/auxiliary_bus.h>
  6. #include <linux/slab.h>
  7. #define AUXILIARY_MAX_IRQ_NAME 11
  8. struct auxiliary_irq_info {
  9. struct device_attribute sysfs_attr;
  10. char name[AUXILIARY_MAX_IRQ_NAME];
  11. };
  12. static struct attribute *auxiliary_irq_attrs[] = {
  13. NULL
  14. };
  15. static const struct attribute_group auxiliary_irqs_group = {
  16. .name = "irqs",
  17. .attrs = auxiliary_irq_attrs,
  18. };
  19. static int auxiliary_irq_dir_prepare(struct auxiliary_device *auxdev)
  20. {
  21. int ret = 0;
  22. guard(mutex)(&auxdev->sysfs.lock);
  23. if (auxdev->sysfs.irq_dir_exists)
  24. return 0;
  25. ret = devm_device_add_group(&auxdev->dev, &auxiliary_irqs_group);
  26. if (ret)
  27. return ret;
  28. auxdev->sysfs.irq_dir_exists = true;
  29. xa_init(&auxdev->sysfs.irqs);
  30. return 0;
  31. }
  32. /**
  33. * auxiliary_device_sysfs_irq_add - add a sysfs entry for the given IRQ
  34. * @auxdev: auxiliary bus device to add the sysfs entry.
  35. * @irq: The associated interrupt number.
  36. *
  37. * This function should be called after auxiliary device have successfully
  38. * received the irq.
  39. * The driver is responsible to add a unique irq for the auxiliary device. The
  40. * driver can invoke this function from multiple thread context safely for
  41. * unique irqs of the auxiliary devices. The driver must not invoke this API
  42. * multiple times if the irq is already added previously.
  43. *
  44. * Return: zero on success or an error code on failure.
  45. */
  46. int auxiliary_device_sysfs_irq_add(struct auxiliary_device *auxdev, int irq)
  47. {
  48. struct auxiliary_irq_info *info __free(kfree) = NULL;
  49. struct device *dev = &auxdev->dev;
  50. int ret;
  51. ret = auxiliary_irq_dir_prepare(auxdev);
  52. if (ret)
  53. return ret;
  54. info = kzalloc(sizeof(*info), GFP_KERNEL);
  55. if (!info)
  56. return -ENOMEM;
  57. sysfs_attr_init(&info->sysfs_attr.attr);
  58. snprintf(info->name, AUXILIARY_MAX_IRQ_NAME, "%d", irq);
  59. ret = xa_insert(&auxdev->sysfs.irqs, irq, info, GFP_KERNEL);
  60. if (ret)
  61. return ret;
  62. info->sysfs_attr.attr.name = info->name;
  63. ret = sysfs_add_file_to_group(&dev->kobj, &info->sysfs_attr.attr,
  64. auxiliary_irqs_group.name);
  65. if (ret)
  66. goto sysfs_add_err;
  67. xa_store(&auxdev->sysfs.irqs, irq, no_free_ptr(info), GFP_KERNEL);
  68. return 0;
  69. sysfs_add_err:
  70. xa_erase(&auxdev->sysfs.irqs, irq);
  71. return ret;
  72. }
  73. EXPORT_SYMBOL_GPL(auxiliary_device_sysfs_irq_add);
  74. /**
  75. * auxiliary_device_sysfs_irq_remove - remove a sysfs entry for the given IRQ
  76. * @auxdev: auxiliary bus device to add the sysfs entry.
  77. * @irq: the IRQ to remove.
  78. *
  79. * This function should be called to remove an IRQ sysfs entry.
  80. * The driver must invoke this API when IRQ is released by the device.
  81. */
  82. void auxiliary_device_sysfs_irq_remove(struct auxiliary_device *auxdev, int irq)
  83. {
  84. struct auxiliary_irq_info *info __free(kfree) = xa_load(&auxdev->sysfs.irqs, irq);
  85. struct device *dev = &auxdev->dev;
  86. if (!info) {
  87. dev_err(&auxdev->dev, "IRQ %d doesn't exist\n", irq);
  88. return;
  89. }
  90. sysfs_remove_file_from_group(&dev->kobj, &info->sysfs_attr.attr,
  91. auxiliary_irqs_group.name);
  92. xa_erase(&auxdev->sysfs.irqs, irq);
  93. }
  94. EXPORT_SYMBOL_GPL(auxiliary_device_sysfs_irq_remove);