1 // SPDX-License-Identifier: GPL-2.0-only
3 * UWB radio (channel) management.
5 * Copyright (C) 2008 Cambridge Silicon Radio Ltd.
7 #include <linux/kernel.h>
9 #include <linux/export.h>
11 #include "uwb-internal.h"
14 static int uwb_radio_select_channel(struct uwb_rc *rc)
17 * Default to channel 9 (BG1, TFC1) unless the user has
18 * selected a specific channel or there are no active PALs.
20 if (rc->active_pals == 0)
22 if (rc->beaconing_forced)
23 return rc->beaconing_forced;
29 * Notify all active PALs that the channel has changed.
31 static void uwb_radio_channel_changed(struct uwb_rc *rc, int channel)
35 list_for_each_entry(pal, &rc->pals, node) {
36 if (pal->channel && channel != pal->channel) {
37 pal->channel = channel;
38 if (pal->channel_changed)
39 pal->channel_changed(pal, pal->channel);
45 * Change to a new channel and notify any active PALs of the new
48 * When stopping the radio, PALs need to be notified first so they can
49 * terminate any active reservations.
51 static int uwb_radio_change_channel(struct uwb_rc *rc, int channel)
54 struct device *dev = &rc->uwb_dev.dev;
56 dev_dbg(dev, "%s: channel = %d, rc->beaconing = %d\n", __func__,
57 channel, rc->beaconing);
60 uwb_radio_channel_changed(rc, channel);
62 if (channel != rc->beaconing) {
63 if (rc->beaconing != -1 && channel != -1) {
65 * FIXME: should signal the channel change
66 * with a Channel Change IE.
68 ret = uwb_radio_change_channel(rc, -1);
72 ret = uwb_rc_beacon(rc, channel, 0);
76 uwb_radio_channel_changed(rc, rc->beaconing);
82 * uwb_radio_start - request that the radio be started
83 * @pal: the PAL making the request.
85 * If the radio is not already active, a suitable channel is selected
86 * and beacons are started.
88 int uwb_radio_start(struct uwb_pal *pal)
90 struct uwb_rc *rc = pal->rc;
93 mutex_lock(&rc->uwb_dev.mutex);
98 ret = uwb_radio_change_channel(rc, uwb_radio_select_channel(rc));
101 mutex_unlock(&rc->uwb_dev.mutex);
104 EXPORT_SYMBOL_GPL(uwb_radio_start);
107 * uwb_radio_stop - request that the radio be stopped.
108 * @pal: the PAL making the request.
110 * Stops the radio if no other PAL is making use of it.
112 void uwb_radio_stop(struct uwb_pal *pal)
114 struct uwb_rc *rc = pal->rc;
116 mutex_lock(&rc->uwb_dev.mutex);
120 uwb_radio_change_channel(rc, uwb_radio_select_channel(rc));
124 mutex_unlock(&rc->uwb_dev.mutex);
126 EXPORT_SYMBOL_GPL(uwb_radio_stop);
129 * uwb_radio_force_channel - force a specific channel to be used
130 * @rc: the radio controller.
131 * @channel: the channel to use; -1 to force the radio to stop; 0 to
132 * use the default channel selection algorithm.
134 int uwb_radio_force_channel(struct uwb_rc *rc, int channel)
138 mutex_lock(&rc->uwb_dev.mutex);
140 rc->beaconing_forced = channel;
141 ret = uwb_radio_change_channel(rc, uwb_radio_select_channel(rc));
143 mutex_unlock(&rc->uwb_dev.mutex);
148 * uwb_radio_setup - setup the radio manager
149 * @rc: the radio controller.
151 * The radio controller is reset to ensure it's in a known state
154 int uwb_radio_setup(struct uwb_rc *rc)
156 return uwb_rc_reset(rc);
160 * uwb_radio_reset_state - reset any radio manager state
161 * @rc: the radio controller.
163 * All internal radio manager state is reset to values corresponding
164 * to a reset radio controller.
166 void uwb_radio_reset_state(struct uwb_rc *rc)
170 mutex_lock(&rc->uwb_dev.mutex);
172 list_for_each_entry(pal, &rc->pals, node) {
175 if (pal->channel_changed)
176 pal->channel_changed(pal, -1);
183 mutex_unlock(&rc->uwb_dev.mutex);
187 * uwb_radio_shutdown - shutdown the radio manager
188 * @rc: the radio controller.
190 * The radio controller is reset.
192 void uwb_radio_shutdown(struct uwb_rc *rc)
194 uwb_radio_reset_state(rc);