treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / staging / uwb / radio.c
blob6afb75ce1b5f2c159b4630f32c79c66b03468612
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * UWB radio (channel) management.
5 * Copyright (C) 2008 Cambridge Silicon Radio Ltd.
6 */
7 #include <linux/kernel.h>
8 #include <linux/export.h>
10 #include "uwb.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)
21 return -1;
22 if (rc->beaconing_forced)
23 return rc->beaconing_forced;
24 return 9;
29 * Notify all active PALs that the channel has changed.
31 static void uwb_radio_channel_changed(struct uwb_rc *rc, int channel)
33 struct uwb_pal *pal;
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
46 * channel.
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)
53 int ret = 0;
54 struct device *dev = &rc->uwb_dev.dev;
56 dev_dbg(dev, "%s: channel = %d, rc->beaconing = %d\n", __func__,
57 channel, rc->beaconing);
59 if (channel == -1)
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);
69 if (ret < 0)
70 return ret;
72 ret = uwb_rc_beacon(rc, channel, 0);
75 if (channel != -1)
76 uwb_radio_channel_changed(rc, rc->beaconing);
78 return ret;
81 /**
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;
91 int ret = 0;
93 mutex_lock(&rc->uwb_dev.mutex);
95 if (!pal->channel) {
96 pal->channel = -1;
97 rc->active_pals++;
98 ret = uwb_radio_change_channel(rc, uwb_radio_select_channel(rc));
101 mutex_unlock(&rc->uwb_dev.mutex);
102 return ret;
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);
118 if (pal->channel) {
119 rc->active_pals--;
120 uwb_radio_change_channel(rc, uwb_radio_select_channel(rc));
121 pal->channel = 0;
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)
136 int ret = 0;
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);
144 return ret;
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
152 * before it's used.
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)
168 struct uwb_pal *pal;
170 mutex_lock(&rc->uwb_dev.mutex);
172 list_for_each_entry(pal, &rc->pals, node) {
173 if (pal->channel) {
174 pal->channel = -1;
175 if (pal->channel_changed)
176 pal->channel_changed(pal, -1);
180 rc->beaconing = -1;
181 rc->scanning = -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);
195 uwb_rc_reset(rc);