radio.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * UWB radio (channel) management.
  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/uwb.h>
  20. #include <linux/export.h>
  21. #include "uwb-internal.h"
  22. static int uwb_radio_select_channel(struct uwb_rc *rc)
  23. {
  24. /*
  25. * Default to channel 9 (BG1, TFC1) unless the user has
  26. * selected a specific channel or there are no active PALs.
  27. */
  28. if (rc->active_pals == 0)
  29. return -1;
  30. if (rc->beaconing_forced)
  31. return rc->beaconing_forced;
  32. return 9;
  33. }
  34. /*
  35. * Notify all active PALs that the channel has changed.
  36. */
  37. static void uwb_radio_channel_changed(struct uwb_rc *rc, int channel)
  38. {
  39. struct uwb_pal *pal;
  40. list_for_each_entry(pal, &rc->pals, node) {
  41. if (pal->channel && channel != pal->channel) {
  42. pal->channel = channel;
  43. if (pal->channel_changed)
  44. pal->channel_changed(pal, pal->channel);
  45. }
  46. }
  47. }
  48. /*
  49. * Change to a new channel and notify any active PALs of the new
  50. * channel.
  51. *
  52. * When stopping the radio, PALs need to be notified first so they can
  53. * terminate any active reservations.
  54. */
  55. static int uwb_radio_change_channel(struct uwb_rc *rc, int channel)
  56. {
  57. int ret = 0;
  58. struct device *dev = &rc->uwb_dev.dev;
  59. dev_dbg(dev, "%s: channel = %d, rc->beaconing = %d\n", __func__,
  60. channel, rc->beaconing);
  61. if (channel == -1)
  62. uwb_radio_channel_changed(rc, channel);
  63. if (channel != rc->beaconing) {
  64. if (rc->beaconing != -1 && channel != -1) {
  65. /*
  66. * FIXME: should signal the channel change
  67. * with a Channel Change IE.
  68. */
  69. ret = uwb_radio_change_channel(rc, -1);
  70. if (ret < 0)
  71. return ret;
  72. }
  73. ret = uwb_rc_beacon(rc, channel, 0);
  74. }
  75. if (channel != -1)
  76. uwb_radio_channel_changed(rc, rc->beaconing);
  77. return ret;
  78. }
  79. /**
  80. * uwb_radio_start - request that the radio be started
  81. * @pal: the PAL making the request.
  82. *
  83. * If the radio is not already active, a suitable channel is selected
  84. * and beacons are started.
  85. */
  86. int uwb_radio_start(struct uwb_pal *pal)
  87. {
  88. struct uwb_rc *rc = pal->rc;
  89. int ret = 0;
  90. mutex_lock(&rc->uwb_dev.mutex);
  91. if (!pal->channel) {
  92. pal->channel = -1;
  93. rc->active_pals++;
  94. ret = uwb_radio_change_channel(rc, uwb_radio_select_channel(rc));
  95. }
  96. mutex_unlock(&rc->uwb_dev.mutex);
  97. return ret;
  98. }
  99. EXPORT_SYMBOL_GPL(uwb_radio_start);
  100. /**
  101. * uwb_radio_stop - request that the radio be stopped.
  102. * @pal: the PAL making the request.
  103. *
  104. * Stops the radio if no other PAL is making use of it.
  105. */
  106. void uwb_radio_stop(struct uwb_pal *pal)
  107. {
  108. struct uwb_rc *rc = pal->rc;
  109. mutex_lock(&rc->uwb_dev.mutex);
  110. if (pal->channel) {
  111. rc->active_pals--;
  112. uwb_radio_change_channel(rc, uwb_radio_select_channel(rc));
  113. pal->channel = 0;
  114. }
  115. mutex_unlock(&rc->uwb_dev.mutex);
  116. }
  117. EXPORT_SYMBOL_GPL(uwb_radio_stop);
  118. /*
  119. * uwb_radio_force_channel - force a specific channel to be used
  120. * @rc: the radio controller.
  121. * @channel: the channel to use; -1 to force the radio to stop; 0 to
  122. * use the default channel selection algorithm.
  123. */
  124. int uwb_radio_force_channel(struct uwb_rc *rc, int channel)
  125. {
  126. int ret = 0;
  127. mutex_lock(&rc->uwb_dev.mutex);
  128. rc->beaconing_forced = channel;
  129. ret = uwb_radio_change_channel(rc, uwb_radio_select_channel(rc));
  130. mutex_unlock(&rc->uwb_dev.mutex);
  131. return ret;
  132. }
  133. /*
  134. * uwb_radio_setup - setup the radio manager
  135. * @rc: the radio controller.
  136. *
  137. * The radio controller is reset to ensure it's in a known state
  138. * before it's used.
  139. */
  140. int uwb_radio_setup(struct uwb_rc *rc)
  141. {
  142. return uwb_rc_reset(rc);
  143. }
  144. /*
  145. * uwb_radio_reset_state - reset any radio manager state
  146. * @rc: the radio controller.
  147. *
  148. * All internal radio manager state is reset to values corresponding
  149. * to a reset radio controller.
  150. */
  151. void uwb_radio_reset_state(struct uwb_rc *rc)
  152. {
  153. struct uwb_pal *pal;
  154. mutex_lock(&rc->uwb_dev.mutex);
  155. list_for_each_entry(pal, &rc->pals, node) {
  156. if (pal->channel) {
  157. pal->channel = -1;
  158. if (pal->channel_changed)
  159. pal->channel_changed(pal, -1);
  160. }
  161. }
  162. rc->beaconing = -1;
  163. rc->scanning = -1;
  164. mutex_unlock(&rc->uwb_dev.mutex);
  165. }
  166. /*
  167. * uwb_radio_shutdown - shutdown the radio manager
  168. * @rc: the radio controller.
  169. *
  170. * The radio controller is reset.
  171. */
  172. void uwb_radio_shutdown(struct uwb_rc *rc)
  173. {
  174. uwb_radio_reset_state(rc);
  175. uwb_rc_reset(rc);
  176. }