Blame view
net/wimax/op-rfkill.c
12.4 KB
04672fe6d treewide: Replace... |
1 |
// SPDX-License-Identifier: GPL-2.0-only |
3e65646bb wimax: basic API:... |
2 3 4 5 |
/* * Linux WiMAX * RF-kill framework integration * |
3e65646bb wimax: basic API:... |
6 7 8 |
* Copyright (C) 2008 Intel Corporation <linux-wimax@intel.com> * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com> * |
3e65646bb wimax: basic API:... |
9 10 11 12 13 14 15 16 |
* This integrates into the Linux Kernel rfkill susbystem so that the * drivers just have to do the bare minimal work, which is providing a * method to set the software RF-Kill switch and to report changes in * the software and hardware switch status. * * A non-polled generic rfkill device is embedded into the WiMAX * subsystem's representation of a device. * |
19d337dff rfkill: rewrite |
17 18 |
* FIXME: Need polled support? Let drivers provide a poll routine * and hand it to rfkill ops then? |
3e65646bb wimax: basic API:... |
19 20 21 22 23 24 25 26 27 28 29 30 |
* * All device drivers have to do is after wimax_dev_init(), call * wimax_report_rfkill_hw() and wimax_report_rfkill_sw() to update * initial state and then every time it changes. See wimax.h:struct * wimax_dev for more information. * * ROADMAP * * wimax_gnl_doit_rfkill() User space calling wimax_rfkill() * wimax_rfkill() Kernel calling wimax_rfkill() * __wimax_rf_toggle_radio() * |
883931612 Fix typos in comm... |
31 |
* wimax_rfkill_set_radio_block() RF-Kill subsystem calling |
3e65646bb wimax: basic API:... |
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
* __wimax_rf_toggle_radio() * * __wimax_rf_toggle_radio() * wimax_dev->op_rfkill_sw_toggle() Driver backend * __wimax_state_change() * * wimax_report_rfkill_sw() Driver reports state change * __wimax_state_change() * * wimax_report_rfkill_hw() Driver reports state change * __wimax_state_change() * * wimax_rfkill_add() Initialize/shutdown rfkill support * wimax_rfkill_rm() [called by wimax_dev_add/rm()] */ #include <net/wimax.h> #include <net/genetlink.h> #include <linux/wimax.h> #include <linux/security.h> #include <linux/rfkill.h> |
bc3b2d7fb net: Add export.h... |
53 |
#include <linux/export.h> |
3e65646bb wimax: basic API:... |
54 55 56 57 |
#include "wimax-internal.h" #define D_SUBMODULE op_rfkill #include "debug-levels.h" |
3e65646bb wimax: basic API:... |
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
/** * wimax_report_rfkill_hw - Reports changes in the hardware RF switch * * @wimax_dev: WiMAX device descriptor * * @state: New state of the RF Kill switch. %WIMAX_RF_ON radio on, * %WIMAX_RF_OFF radio off. * * When the device detects a change in the state of thehardware RF * switch, it must call this function to let the WiMAX kernel stack * know that the state has changed so it can be properly propagated. * * The WiMAX stack caches the state (the driver doesn't need to). As * well, as the change is propagated it will come back as a request to * change the software state to mirror the hardware state. * * If the device doesn't have a hardware kill switch, just report * it on initialization as always on (%WIMAX_RF_ON, radio on). */ void wimax_report_rfkill_hw(struct wimax_dev *wimax_dev, enum wimax_rf_state state) { int result; struct device *dev = wimax_dev_to_dev(wimax_dev); enum wimax_st wimax_state; |
3e65646bb wimax: basic API:... |
83 84 85 86 87 88 89 90 91 92 93 94 95 |
d_fnstart(3, dev, "(wimax_dev %p state %u) ", wimax_dev, state); BUG_ON(state == WIMAX_RF_QUERY); BUG_ON(state != WIMAX_RF_ON && state != WIMAX_RF_OFF); mutex_lock(&wimax_dev->mutex); result = wimax_dev_is_ready(wimax_dev); if (result < 0) goto error_not_ready; if (state != wimax_dev->rf_hw) { wimax_dev->rf_hw = state; |
f64f9e719 net: Move && and ... |
96 97 |
if (wimax_dev->rf_hw == WIMAX_RF_ON && wimax_dev->rf_sw == WIMAX_RF_ON) |
3e65646bb wimax: basic API:... |
98 99 100 |
wimax_state = WIMAX_ST_READY; else wimax_state = WIMAX_ST_RADIO_OFF; |
19d337dff rfkill: rewrite |
101 |
|
d2f4c1054 wimax: fix warnin... |
102 103 |
result = rfkill_set_hw_state(wimax_dev->rfkill, state == WIMAX_RF_OFF); |
19d337dff rfkill: rewrite |
104 |
|
3e65646bb wimax: basic API:... |
105 |
__wimax_state_change(wimax_dev, wimax_state); |
3e65646bb wimax: basic API:... |
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
} error_not_ready: mutex_unlock(&wimax_dev->mutex); d_fnend(3, dev, "(wimax_dev %p state %u) = void [%d] ", wimax_dev, state, result); } EXPORT_SYMBOL_GPL(wimax_report_rfkill_hw); /** * wimax_report_rfkill_sw - Reports changes in the software RF switch * * @wimax_dev: WiMAX device descriptor * * @state: New state of the RF kill switch. %WIMAX_RF_ON radio on, * %WIMAX_RF_OFF radio off. * |
4933d85c5 net:wimax: Fix do... |
124 |
* Reports changes in the software RF switch state to the WiMAX stack. |
3e65646bb wimax: basic API:... |
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
* * The main use is during initialization, so the driver can query the * device for its current software radio kill switch state and feed it * to the system. * * On the side, the device does not change the software state by * itself. In practice, this can happen, as the device might decide to * switch (in software) the radio off for different reasons. */ void wimax_report_rfkill_sw(struct wimax_dev *wimax_dev, enum wimax_rf_state state) { int result; struct device *dev = wimax_dev_to_dev(wimax_dev); enum wimax_st wimax_state; d_fnstart(3, dev, "(wimax_dev %p state %u) ", wimax_dev, state); BUG_ON(state == WIMAX_RF_QUERY); BUG_ON(state != WIMAX_RF_ON && state != WIMAX_RF_OFF); mutex_lock(&wimax_dev->mutex); result = wimax_dev_is_ready(wimax_dev); if (result < 0) goto error_not_ready; if (state != wimax_dev->rf_sw) { wimax_dev->rf_sw = state; |
f64f9e719 net: Move && and ... |
153 154 |
if (wimax_dev->rf_hw == WIMAX_RF_ON && wimax_dev->rf_sw == WIMAX_RF_ON) |
3e65646bb wimax: basic API:... |
155 156 157 158 |
wimax_state = WIMAX_ST_READY; else wimax_state = WIMAX_ST_RADIO_OFF; __wimax_state_change(wimax_dev, wimax_state); |
19d337dff rfkill: rewrite |
159 |
rfkill_set_sw_state(wimax_dev->rfkill, state == WIMAX_RF_OFF); |
3e65646bb wimax: basic API:... |
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
} error_not_ready: mutex_unlock(&wimax_dev->mutex); d_fnend(3, dev, "(wimax_dev %p state %u) = void [%d] ", wimax_dev, state, result); } EXPORT_SYMBOL_GPL(wimax_report_rfkill_sw); /* * Callback for the RF Kill toggle operation * * This function is called by: * * - The rfkill subsystem when the RF-Kill key is pressed in the * hardware and the driver notifies through * wimax_report_rfkill_hw(). The rfkill subsystem ends up calling back * here so the software RF Kill switch state is changed to reflect * the hardware switch state. * * - When the user sets the state through sysfs' rfkill/state file * * - When the user calls wimax_rfkill(). * * This call blocks! * * WARNING! When we call rfkill_unregister(), this will be called with * state 0! * * WARNING: wimax_dev must be locked */ static int __wimax_rf_toggle_radio(struct wimax_dev *wimax_dev, enum wimax_rf_state state) { int result = 0; struct device *dev = wimax_dev_to_dev(wimax_dev); enum wimax_st wimax_state; might_sleep(); d_fnstart(3, dev, "(wimax_dev %p state %u) ", wimax_dev, state); if (wimax_dev->rf_sw == state) goto out_no_change; if (wimax_dev->op_rfkill_sw_toggle != NULL) result = wimax_dev->op_rfkill_sw_toggle(wimax_dev, state); else if (state == WIMAX_RF_OFF) /* No op? can't turn off */ result = -ENXIO; else /* No op? can turn on */ result = 0; /* should never happen tho */ if (result >= 0) { result = 0; wimax_dev->rf_sw = state; wimax_state = state == WIMAX_RF_ON ? WIMAX_ST_READY : WIMAX_ST_RADIO_OFF; __wimax_state_change(wimax_dev, wimax_state); } out_no_change: d_fnend(3, dev, "(wimax_dev %p state %u) = %d ", wimax_dev, state, result); return result; } /* * Translate from rfkill state to wimax state * * NOTE: Special state handling rules here * * Just pretend the call didn't happen if we are in a state where * we know for sure it cannot be handled (WIMAX_ST_DOWN or * __WIMAX_ST_QUIESCING). rfkill() needs it to register and * unregister, as it will run this path. * * NOTE: This call will block until the operation is completed. */ |
19d337dff rfkill: rewrite |
238 |
static int wimax_rfkill_set_radio_block(void *data, bool blocked) |
3e65646bb wimax: basic API:... |
239 240 241 242 243 |
{ int result; struct wimax_dev *wimax_dev = data; struct device *dev = wimax_dev_to_dev(wimax_dev); enum wimax_rf_state rf_state; |
19d337dff rfkill: rewrite |
244 245 246 247 |
d_fnstart(3, dev, "(wimax_dev %p blocked %u) ", wimax_dev, blocked); rf_state = WIMAX_RF_ON; if (blocked) |
3e65646bb wimax: basic API:... |
248 |
rf_state = WIMAX_RF_OFF; |
3e65646bb wimax: basic API:... |
249 250 |
mutex_lock(&wimax_dev->mutex); if (wimax_dev->state <= __WIMAX_ST_QUIESCING) |
19d337dff rfkill: rewrite |
251 |
result = 0; |
3e65646bb wimax: basic API:... |
252 253 254 |
else result = __wimax_rf_toggle_radio(wimax_dev, rf_state); mutex_unlock(&wimax_dev->mutex); |
19d337dff rfkill: rewrite |
255 256 257 |
d_fnend(3, dev, "(wimax_dev %p blocked %u) = %d ", wimax_dev, blocked, result); |
3e65646bb wimax: basic API:... |
258 259 |
return result; } |
19d337dff rfkill: rewrite |
260 261 262 |
static const struct rfkill_ops wimax_rfkill_ops = { .set_block = wimax_rfkill_set_radio_block, }; |
3e65646bb wimax: basic API:... |
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
/** * wimax_rfkill - Set the software RF switch state for a WiMAX device * * @wimax_dev: WiMAX device descriptor * * @state: New RF state. * * Returns: * * >= 0 toggle state if ok, < 0 errno code on error. The toggle state * is returned as a bitmap, bit 0 being the hardware RF state, bit 1 * the software RF state. * * 0 means disabled (%WIMAX_RF_ON, radio on), 1 means enabled radio * off (%WIMAX_RF_OFF). * * Description: * * Called by the user when he wants to request the WiMAX radio to be * switched on (%WIMAX_RF_ON) or off (%WIMAX_RF_OFF). With * %WIMAX_RF_QUERY, just the current state is returned. * * NOTE: * * This call will block until the operation is complete. */ int wimax_rfkill(struct wimax_dev *wimax_dev, enum wimax_rf_state state) { int result; struct device *dev = wimax_dev_to_dev(wimax_dev); d_fnstart(3, dev, "(wimax_dev %p state %u) ", wimax_dev, state); mutex_lock(&wimax_dev->mutex); result = wimax_dev_is_ready(wimax_dev); |
81d3f9053 wimax: allow WIMA... |
299 300 301 302 303 304 305 |
if (result < 0) { /* While initializing, < 1.4.3 wimax-tools versions use * this call to check if the device is a valid WiMAX * device; so we allow it to proceed always, * considering the radios are all off. */ if (result == -ENOMEDIUM && state == WIMAX_RF_QUERY) result = WIMAX_RF_OFF << 1 | WIMAX_RF_OFF; |
3e65646bb wimax: basic API:... |
306 |
goto error_not_ready; |
81d3f9053 wimax: allow WIMA... |
307 |
} |
3e65646bb wimax: basic API:... |
308 309 310 311 312 313 |
switch (state) { case WIMAX_RF_ON: case WIMAX_RF_OFF: result = __wimax_rf_toggle_radio(wimax_dev, state); if (result < 0) goto error; |
19d337dff rfkill: rewrite |
314 |
rfkill_set_sw_state(wimax_dev->rfkill, state == WIMAX_RF_OFF); |
3e65646bb wimax: basic API:... |
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 |
break; case WIMAX_RF_QUERY: break; default: result = -EINVAL; goto error; } result = wimax_dev->rf_sw << 1 | wimax_dev->rf_hw; error: error_not_ready: mutex_unlock(&wimax_dev->mutex); d_fnend(3, dev, "(wimax_dev %p state %u) = %d ", wimax_dev, state, result); return result; } EXPORT_SYMBOL(wimax_rfkill); /* * Register a new WiMAX device's RF Kill support * * WARNING: wimax_dev->mutex must be unlocked */ int wimax_rfkill_add(struct wimax_dev *wimax_dev) { int result; struct rfkill *rfkill; |
3e65646bb wimax: basic API:... |
343 344 345 346 347 348 |
struct device *dev = wimax_dev_to_dev(wimax_dev); d_fnstart(3, dev, "(wimax_dev %p) ", wimax_dev); /* Initialize RF Kill */ result = -ENOMEM; |
19d337dff rfkill: rewrite |
349 350 |
rfkill = rfkill_alloc(wimax_dev->name, dev, RFKILL_TYPE_WIMAX, &wimax_rfkill_ops, wimax_dev); |
3e65646bb wimax: basic API:... |
351 352 |
if (rfkill == NULL) goto error_rfkill_allocate; |
19d337dff rfkill: rewrite |
353 354 355 |
d_printf(1, dev, "rfkill %p ", rfkill); |
3e65646bb wimax: basic API:... |
356 |
wimax_dev->rfkill = rfkill; |
c29eaf3fa wimax: indicate i... |
357 |
rfkill_init_sw_state(rfkill, 1); |
3e65646bb wimax: basic API:... |
358 359 360 361 362 363 364 365 366 367 368 |
result = rfkill_register(wimax_dev->rfkill); if (result < 0) goto error_rfkill_register; /* If there is no SW toggle op, SW RFKill is always on */ if (wimax_dev->op_rfkill_sw_toggle == NULL) wimax_dev->rf_sw = WIMAX_RF_ON; d_fnend(3, dev, "(wimax_dev %p) = 0 ", wimax_dev); return 0; |
3e65646bb wimax: basic API:... |
369 |
error_rfkill_register: |
19d337dff rfkill: rewrite |
370 |
rfkill_destroy(wimax_dev->rfkill); |
3e65646bb wimax: basic API:... |
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 |
error_rfkill_allocate: d_fnend(3, dev, "(wimax_dev %p) = %d ", wimax_dev, result); return result; } /* * Deregister a WiMAX device's RF Kill support * * Ick, we can't call rfkill_free() after rfkill_unregister()...oh * well. * * WARNING: wimax_dev->mutex must be unlocked */ void wimax_rfkill_rm(struct wimax_dev *wimax_dev) { struct device *dev = wimax_dev_to_dev(wimax_dev); d_fnstart(3, dev, "(wimax_dev %p) ", wimax_dev); |
19d337dff rfkill: rewrite |
391 392 |
rfkill_unregister(wimax_dev->rfkill); rfkill_destroy(wimax_dev->rfkill); |
3e65646bb wimax: basic API:... |
393 394 395 |
d_fnend(3, dev, "(wimax_dev %p) ", wimax_dev); } |
3e65646bb wimax: basic API:... |
396 397 398 399 400 401 402 403 404 |
/* * Exporting to user space over generic netlink * * Parse the rfkill command from user space, return a combination * value that describe the states of the different toggles. * * Only one attribute: the new state requested (on, off or no change, * just query). */ |
3e65646bb wimax: basic API:... |
405 406 407 408 409 410 411 412 413 414 415 |
int wimax_gnl_doit_rfkill(struct sk_buff *skb, struct genl_info *info) { int result, ifindex; struct wimax_dev *wimax_dev; struct device *dev; enum wimax_rf_state new_state; d_fnstart(3, NULL, "(skb %p info %p) ", skb, info); result = -ENODEV; if (info->attrs[WIMAX_GNL_RFKILL_IFIDX] == NULL) { |
28b7deae7 wimax: convert pr... |
416 417 |
pr_err("WIMAX_GNL_OP_RFKILL: can't find IFIDX attribute "); |
3e65646bb wimax: basic API:... |
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 |
goto error_no_wimax_dev; } ifindex = nla_get_u32(info->attrs[WIMAX_GNL_RFKILL_IFIDX]); wimax_dev = wimax_dev_get_by_genl_info(info, ifindex); if (wimax_dev == NULL) goto error_no_wimax_dev; dev = wimax_dev_to_dev(wimax_dev); result = -EINVAL; if (info->attrs[WIMAX_GNL_RFKILL_STATE] == NULL) { dev_err(dev, "WIMAX_GNL_RFKILL: can't find RFKILL_STATE " "attribute "); goto error_no_pid; } new_state = nla_get_u32(info->attrs[WIMAX_GNL_RFKILL_STATE]); /* Execute the operation and send the result back to user space */ result = wimax_rfkill(wimax_dev, new_state); error_no_pid: dev_put(wimax_dev->net_dev); error_no_wimax_dev: d_fnend(3, NULL, "(skb %p info %p) = %d ", skb, info, result); return result; } |