radio.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. if (channel == -1)
  59. uwb_radio_channel_changed(rc, channel);
  60. if (channel != rc->beaconing) {
  61. if (rc->beaconing != -1 && channel != -1) {
  62. /*
  63. * FIXME: should signal the channel change
  64. * with a Channel Change IE.
  65. */
  66. ret = uwb_radio_change_channel(rc, -1);
  67. if (ret < 0)
  68. return ret;
  69. }
  70. ret = uwb_rc_beacon(rc, channel, 0);
  71. }
  72. if (channel != -1)
  73. uwb_radio_channel_changed(rc, rc->beaconing);
  74. return ret;
  75. }
  76. /**
  77. * uwb_radio_start - request that the radio be started
  78. * @pal: the PAL making the request.
  79. *
  80. * If the radio is not already active, aa suitable channel is selected
  81. * and beacons are started.
  82. */
  83. int uwb_radio_start(struct uwb_pal *pal)
  84. {
  85. struct uwb_rc *rc = pal->rc;
  86. int ret = 0;
  87. mutex_lock(&rc->uwb_dev.mutex);
  88. if (!pal->channel) {
  89. pal->channel = -1;
  90. rc->active_pals++;
  91. ret = uwb_radio_change_channel(rc, uwb_radio_select_channel(rc));
  92. }
  93. mutex_unlock(&rc->uwb_dev.mutex);
  94. return ret;
  95. }
  96. EXPORT_SYMBOL_GPL(uwb_radio_start);
  97. /**
  98. * uwb_radio_stop - request tha the radio be stopped.
  99. * @pal: the PAL making the request.
  100. *
  101. * Stops the radio if no other PAL is making use of it.
  102. */
  103. void uwb_radio_stop(struct uwb_pal *pal)
  104. {
  105. struct uwb_rc *rc = pal->rc;
  106. mutex_lock(&rc->uwb_dev.mutex);
  107. if (pal->channel) {
  108. rc->active_pals--;
  109. uwb_radio_change_channel(rc, uwb_radio_select_channel(rc));
  110. pal->channel = 0;
  111. }
  112. mutex_unlock(&rc->uwb_dev.mutex);
  113. }
  114. EXPORT_SYMBOL_GPL(uwb_radio_stop);
  115. /*
  116. * uwb_radio_force_channel - force a specific channel to be used
  117. * @rc: the radio controller.
  118. * @channel: the channel to use; -1 to force the radio to stop; 0 to
  119. * use the default channel selection algorithm.
  120. */
  121. int uwb_radio_force_channel(struct uwb_rc *rc, int channel)
  122. {
  123. int ret = 0;
  124. mutex_lock(&rc->uwb_dev.mutex);
  125. rc->beaconing_forced = channel;
  126. ret = uwb_radio_change_channel(rc, uwb_radio_select_channel(rc));
  127. mutex_unlock(&rc->uwb_dev.mutex);
  128. return ret;
  129. }
  130. /*
  131. * uwb_radio_setup - setup the radio manager
  132. * @rc: the radio controller.
  133. *
  134. * The radio controller is reset to ensure it's in a known state
  135. * before it's used.
  136. */
  137. int uwb_radio_setup(struct uwb_rc *rc)
  138. {
  139. return uwb_rc_reset(rc);
  140. }
  141. /*
  142. * uwb_radio_reset_state - reset any radio manager state
  143. * @rc: the radio controller.
  144. *
  145. * All internal radio manager state is reset to values corresponding
  146. * to a reset radio controller.
  147. */
  148. void uwb_radio_reset_state(struct uwb_rc *rc)
  149. {
  150. struct uwb_pal *pal;
  151. mutex_lock(&rc->uwb_dev.mutex);
  152. list_for_each_entry(pal, &rc->pals, node) {
  153. if (pal->channel) {
  154. pal->channel = -1;
  155. if (pal->channel_changed)
  156. pal->channel_changed(pal, -1);
  157. }
  158. }
  159. rc->beaconing = -1;
  160. rc->scanning = -1;
  161. mutex_unlock(&rc->uwb_dev.mutex);
  162. }
  163. /*
  164. * uwb_radio_shutdown - shutdown the radio manager
  165. * @rc: the radio controller.
  166. *
  167. * The radio controller is reset.
  168. */
  169. void uwb_radio_shutdown(struct uwb_rc *rc)
  170. {
  171. uwb_radio_reset_state(rc);
  172. uwb_rc_reset(rc);
  173. }