dca-sysfs.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright(c) 2007 - 2009 Intel Corporation. All rights reserved.
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/spinlock.h>
  7. #include <linux/device.h>
  8. #include <linux/idr.h>
  9. #include <linux/kdev_t.h>
  10. #include <linux/err.h>
  11. #include <linux/dca.h>
  12. #include <linux/gfp.h>
  13. #include <linux/export.h>
  14. static struct class *dca_class;
  15. static struct idr dca_idr;
  16. static spinlock_t dca_idr_lock;
  17. int dca_sysfs_add_req(struct dca_provider *dca, struct device *dev, int slot)
  18. {
  19. struct device *cd;
  20. static int req_count;
  21. cd = device_create(dca_class, dca->cd, MKDEV(0, slot + 1), NULL,
  22. "requester%d", req_count++);
  23. if (IS_ERR(cd))
  24. return PTR_ERR(cd);
  25. return 0;
  26. }
  27. void dca_sysfs_remove_req(struct dca_provider *dca, int slot)
  28. {
  29. device_destroy(dca_class, MKDEV(0, slot + 1));
  30. }
  31. int dca_sysfs_add_provider(struct dca_provider *dca, struct device *dev)
  32. {
  33. struct device *cd;
  34. int ret;
  35. idr_preload(GFP_KERNEL);
  36. spin_lock(&dca_idr_lock);
  37. ret = idr_alloc(&dca_idr, dca, 0, 0, GFP_NOWAIT);
  38. if (ret >= 0)
  39. dca->id = ret;
  40. spin_unlock(&dca_idr_lock);
  41. idr_preload_end();
  42. if (ret < 0)
  43. return ret;
  44. cd = device_create(dca_class, dev, MKDEV(0, 0), NULL, "dca%d", dca->id);
  45. if (IS_ERR(cd)) {
  46. spin_lock(&dca_idr_lock);
  47. idr_remove(&dca_idr, dca->id);
  48. spin_unlock(&dca_idr_lock);
  49. return PTR_ERR(cd);
  50. }
  51. dca->cd = cd;
  52. return 0;
  53. }
  54. void dca_sysfs_remove_provider(struct dca_provider *dca)
  55. {
  56. device_unregister(dca->cd);
  57. dca->cd = NULL;
  58. spin_lock(&dca_idr_lock);
  59. idr_remove(&dca_idr, dca->id);
  60. spin_unlock(&dca_idr_lock);
  61. }
  62. int __init dca_sysfs_init(void)
  63. {
  64. idr_init(&dca_idr);
  65. spin_lock_init(&dca_idr_lock);
  66. dca_class = class_create(THIS_MODULE, "dca");
  67. if (IS_ERR(dca_class)) {
  68. idr_destroy(&dca_idr);
  69. return PTR_ERR(dca_class);
  70. }
  71. return 0;
  72. }
  73. void __exit dca_sysfs_exit(void)
  74. {
  75. class_destroy(dca_class);
  76. idr_destroy(&dca_idr);
  77. }