dca-sysfs.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. return PTR_ERR_OR_ZERO(cd);
  24. }
  25. void dca_sysfs_remove_req(struct dca_provider *dca, int slot)
  26. {
  27. device_destroy(dca_class, MKDEV(0, slot + 1));
  28. }
  29. int dca_sysfs_add_provider(struct dca_provider *dca, struct device *dev)
  30. {
  31. struct device *cd;
  32. int ret;
  33. idr_preload(GFP_KERNEL);
  34. spin_lock(&dca_idr_lock);
  35. ret = idr_alloc(&dca_idr, dca, 0, 0, GFP_NOWAIT);
  36. if (ret >= 0)
  37. dca->id = ret;
  38. spin_unlock(&dca_idr_lock);
  39. idr_preload_end();
  40. if (ret < 0)
  41. return ret;
  42. cd = device_create(dca_class, dev, MKDEV(0, 0), NULL, "dca%d", dca->id);
  43. if (IS_ERR(cd)) {
  44. spin_lock(&dca_idr_lock);
  45. idr_remove(&dca_idr, dca->id);
  46. spin_unlock(&dca_idr_lock);
  47. return PTR_ERR(cd);
  48. }
  49. dca->cd = cd;
  50. return 0;
  51. }
  52. void dca_sysfs_remove_provider(struct dca_provider *dca)
  53. {
  54. device_unregister(dca->cd);
  55. dca->cd = NULL;
  56. spin_lock(&dca_idr_lock);
  57. idr_remove(&dca_idr, dca->id);
  58. spin_unlock(&dca_idr_lock);
  59. }
  60. int __init dca_sysfs_init(void)
  61. {
  62. idr_init(&dca_idr);
  63. spin_lock_init(&dca_idr_lock);
  64. dca_class = class_create(THIS_MODULE, "dca");
  65. if (IS_ERR(dca_class)) {
  66. idr_destroy(&dca_idr);
  67. return PTR_ERR(dca_class);
  68. }
  69. return 0;
  70. }
  71. void __exit dca_sysfs_exit(void)
  72. {
  73. class_destroy(dca_class);
  74. idr_destroy(&dca_idr);
  75. }