dca-sysfs.c 2.7 KB

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