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