dca-sysfs.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. static struct class *dca_class;
  29. static struct idr dca_idr;
  30. static spinlock_t dca_idr_lock;
  31. int dca_sysfs_add_req(struct dca_provider *dca, struct device *dev, int slot)
  32. {
  33. struct device *cd;
  34. static int req_count;
  35. cd = device_create(dca_class, dca->cd, MKDEV(0, slot + 1), NULL,
  36. "requester%d", req_count++);
  37. if (IS_ERR(cd))
  38. return PTR_ERR(cd);
  39. return 0;
  40. }
  41. void dca_sysfs_remove_req(struct dca_provider *dca, int slot)
  42. {
  43. device_destroy(dca_class, MKDEV(0, slot + 1));
  44. }
  45. int dca_sysfs_add_provider(struct dca_provider *dca, struct device *dev)
  46. {
  47. struct device *cd;
  48. int err = 0;
  49. idr_try_again:
  50. if (!idr_pre_get(&dca_idr, GFP_KERNEL))
  51. return -ENOMEM;
  52. spin_lock(&dca_idr_lock);
  53. err = idr_get_new(&dca_idr, dca, &dca->id);
  54. spin_unlock(&dca_idr_lock);
  55. switch (err) {
  56. case 0:
  57. break;
  58. case -EAGAIN:
  59. goto idr_try_again;
  60. default:
  61. return err;
  62. }
  63. cd = device_create(dca_class, dev, MKDEV(0, 0), NULL, "dca%d", dca->id);
  64. if (IS_ERR(cd)) {
  65. spin_lock(&dca_idr_lock);
  66. idr_remove(&dca_idr, dca->id);
  67. spin_unlock(&dca_idr_lock);
  68. return PTR_ERR(cd);
  69. }
  70. dca->cd = cd;
  71. return 0;
  72. }
  73. void dca_sysfs_remove_provider(struct dca_provider *dca)
  74. {
  75. device_unregister(dca->cd);
  76. dca->cd = NULL;
  77. spin_lock(&dca_idr_lock);
  78. idr_remove(&dca_idr, dca->id);
  79. spin_unlock(&dca_idr_lock);
  80. }
  81. int __init dca_sysfs_init(void)
  82. {
  83. idr_init(&dca_idr);
  84. spin_lock_init(&dca_idr_lock);
  85. dca_class = class_create(THIS_MODULE, "dca");
  86. if (IS_ERR(dca_class)) {
  87. idr_destroy(&dca_idr);
  88. return PTR_ERR(dca_class);
  89. }
  90. return 0;
  91. }
  92. void __exit dca_sysfs_exit(void)
  93. {
  94. class_destroy(dca_class);
  95. idr_destroy(&dca_idr);
  96. }