op-rfkill.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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. result = rfkill_set_hw_state(wimax_dev->rfkill,
  110. state == WIMAX_RF_OFF);
  111. __wimax_state_change(wimax_dev, wimax_state);
  112. }
  113. error_not_ready:
  114. mutex_unlock(&wimax_dev->mutex);
  115. d_fnend(3, dev, "(wimax_dev %p state %u) = void [%d]\n",
  116. wimax_dev, state, result);
  117. }
  118. EXPORT_SYMBOL_GPL(wimax_report_rfkill_hw);
  119. /**
  120. * wimax_report_rfkill_sw - Reports changes in the software RF switch
  121. *
  122. * @wimax_dev: WiMAX device descriptor
  123. *
  124. * @state: New state of the RF kill switch. %WIMAX_RF_ON radio on,
  125. * %WIMAX_RF_OFF radio off.
  126. *
  127. * Reports changes in the software RF switch state to the the WiMAX
  128. * 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. goto error_not_ready;
  288. switch (state) {
  289. case WIMAX_RF_ON:
  290. case WIMAX_RF_OFF:
  291. result = __wimax_rf_toggle_radio(wimax_dev, state);
  292. if (result < 0)
  293. goto error;
  294. rfkill_set_sw_state(wimax_dev->rfkill, state == WIMAX_RF_OFF);
  295. break;
  296. case WIMAX_RF_QUERY:
  297. break;
  298. default:
  299. result = -EINVAL;
  300. goto error;
  301. }
  302. result = wimax_dev->rf_sw << 1 | wimax_dev->rf_hw;
  303. error:
  304. error_not_ready:
  305. mutex_unlock(&wimax_dev->mutex);
  306. d_fnend(3, dev, "(wimax_dev %p state %u) = %d\n",
  307. wimax_dev, state, result);
  308. return result;
  309. }
  310. EXPORT_SYMBOL(wimax_rfkill);
  311. /*
  312. * Register a new WiMAX device's RF Kill support
  313. *
  314. * WARNING: wimax_dev->mutex must be unlocked
  315. */
  316. int wimax_rfkill_add(struct wimax_dev *wimax_dev)
  317. {
  318. int result;
  319. struct rfkill *rfkill;
  320. struct device *dev = wimax_dev_to_dev(wimax_dev);
  321. d_fnstart(3, dev, "(wimax_dev %p)\n", wimax_dev);
  322. /* Initialize RF Kill */
  323. result = -ENOMEM;
  324. rfkill = rfkill_alloc(wimax_dev->name, dev, RFKILL_TYPE_WIMAX,
  325. &wimax_rfkill_ops, wimax_dev);
  326. if (rfkill == NULL)
  327. goto error_rfkill_allocate;
  328. d_printf(1, dev, "rfkill %p\n", rfkill);
  329. wimax_dev->rfkill = rfkill;
  330. result = rfkill_register(wimax_dev->rfkill);
  331. if (result < 0)
  332. goto error_rfkill_register;
  333. /* If there is no SW toggle op, SW RFKill is always on */
  334. if (wimax_dev->op_rfkill_sw_toggle == NULL)
  335. wimax_dev->rf_sw = WIMAX_RF_ON;
  336. d_fnend(3, dev, "(wimax_dev %p) = 0\n", wimax_dev);
  337. return 0;
  338. error_rfkill_register:
  339. rfkill_destroy(wimax_dev->rfkill);
  340. error_rfkill_allocate:
  341. d_fnend(3, dev, "(wimax_dev %p) = %d\n", wimax_dev, result);
  342. return result;
  343. }
  344. /*
  345. * Deregister a WiMAX device's RF Kill support
  346. *
  347. * Ick, we can't call rfkill_free() after rfkill_unregister()...oh
  348. * well.
  349. *
  350. * WARNING: wimax_dev->mutex must be unlocked
  351. */
  352. void wimax_rfkill_rm(struct wimax_dev *wimax_dev)
  353. {
  354. struct device *dev = wimax_dev_to_dev(wimax_dev);
  355. d_fnstart(3, dev, "(wimax_dev %p)\n", wimax_dev);
  356. rfkill_unregister(wimax_dev->rfkill);
  357. rfkill_destroy(wimax_dev->rfkill);
  358. d_fnend(3, dev, "(wimax_dev %p)\n", wimax_dev);
  359. }
  360. /*
  361. * Exporting to user space over generic netlink
  362. *
  363. * Parse the rfkill command from user space, return a combination
  364. * value that describe the states of the different toggles.
  365. *
  366. * Only one attribute: the new state requested (on, off or no change,
  367. * just query).
  368. */
  369. static const
  370. struct nla_policy wimax_gnl_rfkill_policy[WIMAX_GNL_ATTR_MAX + 1] = {
  371. [WIMAX_GNL_RFKILL_IFIDX] = {
  372. .type = NLA_U32,
  373. },
  374. [WIMAX_GNL_RFKILL_STATE] = {
  375. .type = NLA_U32 /* enum wimax_rf_state */
  376. },
  377. };
  378. static
  379. int wimax_gnl_doit_rfkill(struct sk_buff *skb, struct genl_info *info)
  380. {
  381. int result, ifindex;
  382. struct wimax_dev *wimax_dev;
  383. struct device *dev;
  384. enum wimax_rf_state new_state;
  385. d_fnstart(3, NULL, "(skb %p info %p)\n", skb, info);
  386. result = -ENODEV;
  387. if (info->attrs[WIMAX_GNL_RFKILL_IFIDX] == NULL) {
  388. printk(KERN_ERR "WIMAX_GNL_OP_RFKILL: can't find IFIDX "
  389. "attribute\n");
  390. goto error_no_wimax_dev;
  391. }
  392. ifindex = nla_get_u32(info->attrs[WIMAX_GNL_RFKILL_IFIDX]);
  393. wimax_dev = wimax_dev_get_by_genl_info(info, ifindex);
  394. if (wimax_dev == NULL)
  395. goto error_no_wimax_dev;
  396. dev = wimax_dev_to_dev(wimax_dev);
  397. result = -EINVAL;
  398. if (info->attrs[WIMAX_GNL_RFKILL_STATE] == NULL) {
  399. dev_err(dev, "WIMAX_GNL_RFKILL: can't find RFKILL_STATE "
  400. "attribute\n");
  401. goto error_no_pid;
  402. }
  403. new_state = nla_get_u32(info->attrs[WIMAX_GNL_RFKILL_STATE]);
  404. /* Execute the operation and send the result back to user space */
  405. result = wimax_rfkill(wimax_dev, new_state);
  406. error_no_pid:
  407. dev_put(wimax_dev->net_dev);
  408. error_no_wimax_dev:
  409. d_fnend(3, NULL, "(skb %p info %p) = %d\n", skb, info, result);
  410. return result;
  411. }
  412. struct genl_ops wimax_gnl_rfkill = {
  413. .cmd = WIMAX_GNL_OP_RFKILL,
  414. .flags = GENL_ADMIN_PERM,
  415. .policy = wimax_gnl_rfkill_policy,
  416. .doit = wimax_gnl_doit_rfkill,
  417. .dumpit = NULL,
  418. };