dca-sysfs.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright(c) 2007 - 2009 Intel Corporation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation; either version 2 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc., 59
  16. * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. *
  18. * The full GNU General Public License is included in this distribution in the
  19. * file called COPYING.
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/device.h>
  24. #include <linux/idr.h>
  25. #include <linux/kdev_t.h>
  26. #include <linux/err.h>
  27. #include <linux/dca.h>
  28. #include <linux/gfp.h>
  29. #include <linux/export.h>
  30. static struct class *dca_class;
  31. static struct idr dca_idr;
  32. static spinlock_t dca_idr_lock;
  33. int dca_sysfs_add_req(struct dca_provider *dca, struct device *dev, int slot)
  34. {
  35. struct device *cd;
  36. static int req_count;
  37. cd = device_create(dca_class, dca->cd, MKDEV(0, slot + 1), NULL,
  38. "requester%d", req_count++);
  39. if (IS_ERR(cd))
  40. return PTR_ERR(cd);
  41. return 0;
  42. }
  43. void dca_sysfs_remove_req(struct dca_provider *dca, int slot)
  44. {
  45. device_destroy(dca_class, MKDEV(0, slot + 1));
  46. }
  47. int dca_sysfs_add_provider(struct dca_provider *dca, struct device *dev)
  48. {
  49. struct device *cd;
  50. int err = 0;
  51. idr_try_again:
  52. if (!idr_pre_get(&dca_idr, GFP_KERNEL))
  53. return -ENOMEM;
  54. spin_lock(&dca_idr_lock);
  55. err = idr_get_new(&dca_idr, dca, &dca->id);
  56. spin_unlock(&dca_idr_lock);
  57. switch (err) {
  58. case 0:
  59. break;
  60. case -EAGAIN:
  61. goto idr_try_again;
  62. default:
  63. return err;
  64. }
  65. cd = device_create(dca_class, dev, MKDEV(0, 0), NULL, "dca%d", dca->id);
  66. if (IS_ERR(cd)) {
  67. spin_lock(&dca_idr_lock);
  68. idr_remove(&dca_idr, dca->id);
  69. spin_unlock(&dca_idr_lock);
  70. return PTR_ERR(cd);
  71. }
  72. dca->cd = cd;
  73. return 0;
  74. }
  75. void dca_sysfs_remove_provider(struct dca_provider *dca)
  76. {
  77. device_unregister(dca->cd);
  78. dca->cd = NULL;
  79. spin_lock(&dca_idr_lock);
  80. idr_remove(&dca_idr, dca->id);
  81. spin_unlock(&dca_idr_lock);
  82. }
  83. int __init dca_sysfs_init(void)
  84. {
  85. idr_init(&dca_idr);
  86. spin_lock_init(&dca_idr_lock);
  87. dca_class = class_create(THIS_MODULE, "dca");
  88. if (IS_ERR(dca_class)) {
  89. idr_destroy(&dca_idr);
  90. return PTR_ERR(dca_class);
  91. }
  92. return 0;
  93. }
  94. void __exit dca_sysfs_exit(void)
  95. {
  96. class_destroy(dca_class);
  97. idr_destroy(&dca_idr);
  98. }