pal.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * UWB PAL support.
  3. *
  4. * Copyright (C) 2008 Cambridge Silicon Radio Ltd.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License version
  8. * 2 as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/debugfs.h>
  20. #include <linux/uwb.h>
  21. #include <linux/export.h>
  22. #include "uwb-internal.h"
  23. /**
  24. * uwb_pal_init - initialize a UWB PAL
  25. * @pal: the PAL to initialize
  26. */
  27. void uwb_pal_init(struct uwb_pal *pal)
  28. {
  29. INIT_LIST_HEAD(&pal->node);
  30. }
  31. EXPORT_SYMBOL_GPL(uwb_pal_init);
  32. /**
  33. * uwb_pal_register - register a UWB PAL
  34. * @pal: the PAL
  35. *
  36. * The PAL must be initialized with uwb_pal_init().
  37. */
  38. int uwb_pal_register(struct uwb_pal *pal)
  39. {
  40. struct uwb_rc *rc = pal->rc;
  41. int ret;
  42. if (pal->device) {
  43. /* create a link to the uwb_rc in the PAL device's directory. */
  44. ret = sysfs_create_link(&pal->device->kobj,
  45. &rc->uwb_dev.dev.kobj, "uwb_rc");
  46. if (ret < 0)
  47. return ret;
  48. /* create a link to the PAL in the UWB device's directory. */
  49. ret = sysfs_create_link(&rc->uwb_dev.dev.kobj,
  50. &pal->device->kobj, pal->name);
  51. if (ret < 0) {
  52. sysfs_remove_link(&pal->device->kobj, "uwb_rc");
  53. return ret;
  54. }
  55. }
  56. pal->debugfs_dir = uwb_dbg_create_pal_dir(pal);
  57. mutex_lock(&rc->uwb_dev.mutex);
  58. list_add(&pal->node, &rc->pals);
  59. mutex_unlock(&rc->uwb_dev.mutex);
  60. return 0;
  61. }
  62. EXPORT_SYMBOL_GPL(uwb_pal_register);
  63. static int find_rc(struct device *dev, const void *data)
  64. {
  65. const struct uwb_rc *target_rc = data;
  66. struct uwb_rc *rc = dev_get_drvdata(dev);
  67. if (rc == NULL) {
  68. WARN_ON(1);
  69. return 0;
  70. }
  71. if (rc == target_rc) {
  72. if (rc->ready == 0)
  73. return 0;
  74. else
  75. return 1;
  76. }
  77. return 0;
  78. }
  79. /**
  80. * Given a radio controller descriptor see if it is registered.
  81. *
  82. * @returns false if the rc does not exist or is quiescing; true otherwise.
  83. */
  84. static bool uwb_rc_class_device_exists(struct uwb_rc *target_rc)
  85. {
  86. struct device *dev;
  87. dev = class_find_device(&uwb_rc_class, NULL, target_rc, find_rc);
  88. put_device(dev);
  89. return (dev != NULL);
  90. }
  91. /**
  92. * uwb_pal_unregister - unregister a UWB PAL
  93. * @pal: the PAL
  94. */
  95. void uwb_pal_unregister(struct uwb_pal *pal)
  96. {
  97. struct uwb_rc *rc = pal->rc;
  98. uwb_radio_stop(pal);
  99. mutex_lock(&rc->uwb_dev.mutex);
  100. list_del(&pal->node);
  101. mutex_unlock(&rc->uwb_dev.mutex);
  102. debugfs_remove(pal->debugfs_dir);
  103. if (pal->device) {
  104. /* remove link to the PAL in the UWB device's directory. */
  105. if (uwb_rc_class_device_exists(rc))
  106. sysfs_remove_link(&rc->uwb_dev.dev.kobj, pal->name);
  107. /* remove link to uwb_rc in the PAL device's directory. */
  108. sysfs_remove_link(&pal->device->kobj, "uwb_rc");
  109. }
  110. }
  111. EXPORT_SYMBOL_GPL(uwb_pal_unregister);
  112. /**
  113. * uwb_rc_pal_init - initialize the PAL related parts of a radio controller
  114. * @rc: the radio controller
  115. */
  116. void uwb_rc_pal_init(struct uwb_rc *rc)
  117. {
  118. INIT_LIST_HEAD(&rc->pals);
  119. }