op-rfkill.c 12 KB

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