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. #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 ret;
  51. idr_preload(GFP_KERNEL);
  52. spin_lock(&dca_idr_lock);
  53. ret = idr_alloc(&dca_idr, dca, 0, 0, GFP_NOWAIT);
  54. if (ret >= 0)
  55. dca->id = ret;
  56. spin_unlock(&dca_idr_lock);
  57. idr_preload_end();
  58. if (ret < 0)
  59. return ret;
  60. cd = device_create(dca_class, dev, MKDEV(0, 0), NULL, "dca%d", dca->id);
  61. if (IS_ERR(cd)) {
  62. spin_lock(&dca_idr_lock);
  63. idr_remove(&dca_idr, dca->id);
  64. spin_unlock(&dca_idr_lock);
  65. return PTR_ERR(cd);
  66. }
  67. dca->cd = cd;
  68. return 0;
  69. }
  70. void dca_sysfs_remove_provider(struct dca_provider *dca)
  71. {
  72. device_unregister(dca->cd);
  73. dca->cd = NULL;
  74. spin_lock(&dca_idr_lock);
  75. idr_remove(&dca_idr, dca->id);
  76. spin_unlock(&dca_idr_lock);
  77. }
  78. int __init dca_sysfs_init(void)
  79. {
  80. idr_init(&dca_idr);
  81. spin_lock_init(&dca_idr_lock);
  82. dca_class = class_create(THIS_MODULE, "dca");
  83. if (IS_ERR(dca_class)) {
  84. idr_destroy(&dca_idr);
  85. return PTR_ERR(dca_class);
  86. }
  87. return 0;
  88. }
  89. void __exit dca_sysfs_exit(void)
  90. {
  91. class_destroy(dca_class);
  92. idr_destroy(&dca_idr);
  93. }