radio.c 4.5 KB

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