op-rfkill.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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 the WiMAX
  129. * stack.
  130. *
  131. * The main use is during initialization, so the driver can query the
  132. * device for its current software radio kill switch state and feed it
  133. * to the system.
  134. *
  135. * On the side, the device does not change the software state by
  136. * itself. In practice, this can happen, as the device might decide to
  137. * switch (in software) the radio off for different reasons.
  138. */
  139. void wimax_report_rfkill_sw(struct wimax_dev *wimax_dev,
  140. enum wimax_rf_state state)
  141. {
  142. int result;
  143. struct device *dev = wimax_dev_to_dev(wimax_dev);
  144. enum wimax_st wimax_state;
  145. d_fnstart(3, dev, "(wimax_dev %p state %u)\n", wimax_dev, state);
  146. BUG_ON(state == WIMAX_RF_QUERY);
  147. BUG_ON(state != WIMAX_RF_ON && state != WIMAX_RF_OFF);
  148. mutex_lock(&wimax_dev->mutex);
  149. result = wimax_dev_is_ready(wimax_dev);
  150. if (result < 0)
  151. goto error_not_ready;
  152. if (state != wimax_dev->rf_sw) {
  153. wimax_dev->rf_sw = state;
  154. if (wimax_dev->rf_hw == WIMAX_RF_ON &&
  155. wimax_dev->rf_sw == WIMAX_RF_ON)
  156. wimax_state = WIMAX_ST_READY;
  157. else
  158. wimax_state = WIMAX_ST_RADIO_OFF;
  159. __wimax_state_change(wimax_dev, wimax_state);
  160. rfkill_set_sw_state(wimax_dev->rfkill, state == WIMAX_RF_OFF);
  161. }
  162. error_not_ready:
  163. mutex_unlock(&wimax_dev->mutex);
  164. d_fnend(3, dev, "(wimax_dev %p state %u) = void [%d]\n",
  165. wimax_dev, state, result);
  166. }
  167. EXPORT_SYMBOL_GPL(wimax_report_rfkill_sw);
  168. /*
  169. * Callback for the RF Kill toggle operation
  170. *
  171. * This function is called by:
  172. *
  173. * - The rfkill subsystem when the RF-Kill key is pressed in the
  174. * hardware and the driver notifies through
  175. * wimax_report_rfkill_hw(). The rfkill subsystem ends up calling back
  176. * here so the software RF Kill switch state is changed to reflect
  177. * the hardware switch state.
  178. *
  179. * - When the user sets the state through sysfs' rfkill/state file
  180. *
  181. * - When the user calls wimax_rfkill().
  182. *
  183. * This call blocks!
  184. *
  185. * WARNING! When we call rfkill_unregister(), this will be called with
  186. * state 0!
  187. *
  188. * WARNING: wimax_dev must be locked
  189. */
  190. static
  191. int __wimax_rf_toggle_radio(struct wimax_dev *wimax_dev,
  192. enum wimax_rf_state state)
  193. {
  194. int result = 0;
  195. struct device *dev = wimax_dev_to_dev(wimax_dev);
  196. enum wimax_st wimax_state;
  197. might_sleep();
  198. d_fnstart(3, dev, "(wimax_dev %p state %u)\n", wimax_dev, state);
  199. if (wimax_dev->rf_sw == state)
  200. goto out_no_change;
  201. if (wimax_dev->op_rfkill_sw_toggle != NULL)
  202. result = wimax_dev->op_rfkill_sw_toggle(wimax_dev, state);
  203. else if (state == WIMAX_RF_OFF) /* No op? can't turn off */
  204. result = -ENXIO;
  205. else /* No op? can turn on */
  206. result = 0; /* should never happen tho */
  207. if (result >= 0) {
  208. result = 0;
  209. wimax_dev->rf_sw = state;
  210. wimax_state = state == WIMAX_RF_ON ?
  211. WIMAX_ST_READY : WIMAX_ST_RADIO_OFF;
  212. __wimax_state_change(wimax_dev, wimax_state);
  213. }
  214. out_no_change:
  215. d_fnend(3, dev, "(wimax_dev %p state %u) = %d\n",
  216. wimax_dev, state, result);
  217. return result;
  218. }
  219. /*
  220. * Translate from rfkill state to wimax state
  221. *
  222. * NOTE: Special state handling rules here
  223. *
  224. * Just pretend the call didn't happen if we are in a state where
  225. * we know for sure it cannot be handled (WIMAX_ST_DOWN or
  226. * __WIMAX_ST_QUIESCING). rfkill() needs it to register and
  227. * unregister, as it will run this path.
  228. *
  229. * NOTE: This call will block until the operation is completed.
  230. */
  231. static int wimax_rfkill_set_radio_block(void *data, bool blocked)
  232. {
  233. int result;
  234. struct wimax_dev *wimax_dev = data;
  235. struct device *dev = wimax_dev_to_dev(wimax_dev);
  236. enum wimax_rf_state rf_state;
  237. d_fnstart(3, dev, "(wimax_dev %p blocked %u)\n", wimax_dev, blocked);
  238. rf_state = WIMAX_RF_ON;
  239. if (blocked)
  240. rf_state = WIMAX_RF_OFF;
  241. mutex_lock(&wimax_dev->mutex);
  242. if (wimax_dev->state <= __WIMAX_ST_QUIESCING)
  243. result = 0;
  244. else
  245. result = __wimax_rf_toggle_radio(wimax_dev, rf_state);
  246. mutex_unlock(&wimax_dev->mutex);
  247. d_fnend(3, dev, "(wimax_dev %p blocked %u) = %d\n",
  248. wimax_dev, blocked, result);
  249. return result;
  250. }
  251. static const struct rfkill_ops wimax_rfkill_ops = {
  252. .set_block = wimax_rfkill_set_radio_block,
  253. };
  254. /**
  255. * wimax_rfkill - Set the software RF switch state for a WiMAX device
  256. *
  257. * @wimax_dev: WiMAX device descriptor
  258. *
  259. * @state: New RF state.
  260. *
  261. * Returns:
  262. *
  263. * >= 0 toggle state if ok, < 0 errno code on error. The toggle state
  264. * is returned as a bitmap, bit 0 being the hardware RF state, bit 1
  265. * the software RF state.
  266. *
  267. * 0 means disabled (%WIMAX_RF_ON, radio on), 1 means enabled radio
  268. * off (%WIMAX_RF_OFF).
  269. *
  270. * Description:
  271. *
  272. * Called by the user when he wants to request the WiMAX radio to be
  273. * switched on (%WIMAX_RF_ON) or off (%WIMAX_RF_OFF). With
  274. * %WIMAX_RF_QUERY, just the current state is returned.
  275. *
  276. * NOTE:
  277. *
  278. * This call will block until the operation is complete.
  279. */
  280. int wimax_rfkill(struct wimax_dev *wimax_dev, enum wimax_rf_state state)
  281. {
  282. int result;
  283. struct device *dev = wimax_dev_to_dev(wimax_dev);
  284. d_fnstart(3, dev, "(wimax_dev %p state %u)\n", wimax_dev, state);
  285. mutex_lock(&wimax_dev->mutex);
  286. result = wimax_dev_is_ready(wimax_dev);
  287. if (result < 0) {
  288. /* While initializing, < 1.4.3 wimax-tools versions use
  289. * this call to check if the device is a valid WiMAX
  290. * device; so we allow it to proceed always,
  291. * considering the radios are all off. */
  292. if (result == -ENOMEDIUM && state == WIMAX_RF_QUERY)
  293. result = WIMAX_RF_OFF << 1 | WIMAX_RF_OFF;
  294. goto error_not_ready;
  295. }
  296. switch (state) {
  297. case WIMAX_RF_ON:
  298. case WIMAX_RF_OFF:
  299. result = __wimax_rf_toggle_radio(wimax_dev, state);
  300. if (result < 0)
  301. goto error;
  302. rfkill_set_sw_state(wimax_dev->rfkill, state == WIMAX_RF_OFF);
  303. break;
  304. case WIMAX_RF_QUERY:
  305. break;
  306. default:
  307. result = -EINVAL;
  308. goto error;
  309. }
  310. result = wimax_dev->rf_sw << 1 | wimax_dev->rf_hw;
  311. error:
  312. error_not_ready:
  313. mutex_unlock(&wimax_dev->mutex);
  314. d_fnend(3, dev, "(wimax_dev %p state %u) = %d\n",
  315. wimax_dev, state, result);
  316. return result;
  317. }
  318. EXPORT_SYMBOL(wimax_rfkill);
  319. /*
  320. * Register a new WiMAX device's RF Kill support
  321. *
  322. * WARNING: wimax_dev->mutex must be unlocked
  323. */
  324. int wimax_rfkill_add(struct wimax_dev *wimax_dev)
  325. {
  326. int result;
  327. struct rfkill *rfkill;
  328. struct device *dev = wimax_dev_to_dev(wimax_dev);
  329. d_fnstart(3, dev, "(wimax_dev %p)\n", wimax_dev);
  330. /* Initialize RF Kill */
  331. result = -ENOMEM;
  332. rfkill = rfkill_alloc(wimax_dev->name, dev, RFKILL_TYPE_WIMAX,
  333. &wimax_rfkill_ops, wimax_dev);
  334. if (rfkill == NULL)
  335. goto error_rfkill_allocate;
  336. d_printf(1, dev, "rfkill %p\n", rfkill);
  337. wimax_dev->rfkill = rfkill;
  338. rfkill_init_sw_state(rfkill, 1);
  339. result = rfkill_register(wimax_dev->rfkill);
  340. if (result < 0)
  341. goto error_rfkill_register;
  342. /* If there is no SW toggle op, SW RFKill is always on */
  343. if (wimax_dev->op_rfkill_sw_toggle == NULL)
  344. wimax_dev->rf_sw = WIMAX_RF_ON;
  345. d_fnend(3, dev, "(wimax_dev %p) = 0\n", wimax_dev);
  346. return 0;
  347. error_rfkill_register:
  348. rfkill_destroy(wimax_dev->rfkill);
  349. error_rfkill_allocate:
  350. d_fnend(3, dev, "(wimax_dev %p) = %d\n", wimax_dev, result);
  351. return result;
  352. }
  353. /*
  354. * Deregister a WiMAX device's RF Kill support
  355. *
  356. * Ick, we can't call rfkill_free() after rfkill_unregister()...oh
  357. * well.
  358. *
  359. * WARNING: wimax_dev->mutex must be unlocked
  360. */
  361. void wimax_rfkill_rm(struct wimax_dev *wimax_dev)
  362. {
  363. struct device *dev = wimax_dev_to_dev(wimax_dev);
  364. d_fnstart(3, dev, "(wimax_dev %p)\n", wimax_dev);
  365. rfkill_unregister(wimax_dev->rfkill);
  366. rfkill_destroy(wimax_dev->rfkill);
  367. d_fnend(3, dev, "(wimax_dev %p)\n", wimax_dev);
  368. }
  369. /*
  370. * Exporting to user space over generic netlink
  371. *
  372. * Parse the rfkill command from user space, return a combination
  373. * value that describe the states of the different toggles.
  374. *
  375. * Only one attribute: the new state requested (on, off or no change,
  376. * just query).
  377. */
  378. int wimax_gnl_doit_rfkill(struct sk_buff *skb, struct genl_info *info)
  379. {
  380. int result, ifindex;
  381. struct wimax_dev *wimax_dev;
  382. struct device *dev;
  383. enum wimax_rf_state new_state;
  384. d_fnstart(3, NULL, "(skb %p info %p)\n", skb, info);
  385. result = -ENODEV;
  386. if (info->attrs[WIMAX_GNL_RFKILL_IFIDX] == NULL) {
  387. pr_err("WIMAX_GNL_OP_RFKILL: can't find IFIDX attribute\n");
  388. goto error_no_wimax_dev;
  389. }
  390. ifindex = nla_get_u32(info->attrs[WIMAX_GNL_RFKILL_IFIDX]);
  391. wimax_dev = wimax_dev_get_by_genl_info(info, ifindex);
  392. if (wimax_dev == NULL)
  393. goto error_no_wimax_dev;
  394. dev = wimax_dev_to_dev(wimax_dev);
  395. result = -EINVAL;
  396. if (info->attrs[WIMAX_GNL_RFKILL_STATE] == NULL) {
  397. dev_err(dev, "WIMAX_GNL_RFKILL: can't find RFKILL_STATE "
  398. "attribute\n");
  399. goto error_no_pid;
  400. }
  401. new_state = nla_get_u32(info->attrs[WIMAX_GNL_RFKILL_STATE]);
  402. /* Execute the operation and send the result back to user space */
  403. result = wimax_rfkill(wimax_dev, new_state);
  404. error_no_pid:
  405. dev_put(wimax_dev->net_dev);
  406. error_no_wimax_dev:
  407. d_fnend(3, NULL, "(skb %p info %p) = %d\n", skb, info, result);
  408. return result;
  409. }