op-rfkill.c 13 KB

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