op-rfkill.c 13 KB

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