op-rfkill.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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? use a timer or add the implementation
  33. * to the stack.
  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_toggle_radio() 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 <linux/input.h>
  68. #include "wimax-internal.h"
  69. #define D_SUBMODULE op_rfkill
  70. #include "debug-levels.h"
  71. #if defined(CONFIG_RFKILL) || defined(CONFIG_RFKILL_MODULE)
  72. /**
  73. * wimax_report_rfkill_hw - Reports changes in the hardware RF switch
  74. *
  75. * @wimax_dev: WiMAX device descriptor
  76. *
  77. * @state: New state of the RF Kill switch. %WIMAX_RF_ON radio on,
  78. * %WIMAX_RF_OFF radio off.
  79. *
  80. * When the device detects a change in the state of thehardware RF
  81. * switch, it must call this function to let the WiMAX kernel stack
  82. * know that the state has changed so it can be properly propagated.
  83. *
  84. * The WiMAX stack caches the state (the driver doesn't need to). As
  85. * well, as the change is propagated it will come back as a request to
  86. * change the software state to mirror the hardware state.
  87. *
  88. * If the device doesn't have a hardware kill switch, just report
  89. * it on initialization as always on (%WIMAX_RF_ON, radio on).
  90. */
  91. void wimax_report_rfkill_hw(struct wimax_dev *wimax_dev,
  92. enum wimax_rf_state state)
  93. {
  94. int result;
  95. struct device *dev = wimax_dev_to_dev(wimax_dev);
  96. enum wimax_st wimax_state;
  97. enum rfkill_state rfkill_state;
  98. d_fnstart(3, dev, "(wimax_dev %p state %u)\n", wimax_dev, state);
  99. BUG_ON(state == WIMAX_RF_QUERY);
  100. BUG_ON(state != WIMAX_RF_ON && state != WIMAX_RF_OFF);
  101. mutex_lock(&wimax_dev->mutex);
  102. result = wimax_dev_is_ready(wimax_dev);
  103. if (result < 0)
  104. goto error_not_ready;
  105. if (state != wimax_dev->rf_hw) {
  106. wimax_dev->rf_hw = state;
  107. rfkill_state = state == WIMAX_RF_ON ?
  108. RFKILL_STATE_OFF : RFKILL_STATE_ON;
  109. if (wimax_dev->rf_hw == WIMAX_RF_ON
  110. && wimax_dev->rf_sw == WIMAX_RF_ON)
  111. wimax_state = WIMAX_ST_READY;
  112. else
  113. wimax_state = WIMAX_ST_RADIO_OFF;
  114. __wimax_state_change(wimax_dev, wimax_state);
  115. input_report_key(wimax_dev->rfkill_input, KEY_WIMAX,
  116. rfkill_state);
  117. }
  118. error_not_ready:
  119. mutex_unlock(&wimax_dev->mutex);
  120. d_fnend(3, dev, "(wimax_dev %p state %u) = void [%d]\n",
  121. wimax_dev, state, result);
  122. }
  123. EXPORT_SYMBOL_GPL(wimax_report_rfkill_hw);
  124. /**
  125. * wimax_report_rfkill_sw - Reports changes in the software RF switch
  126. *
  127. * @wimax_dev: WiMAX device descriptor
  128. *
  129. * @state: New state of the RF kill switch. %WIMAX_RF_ON radio on,
  130. * %WIMAX_RF_OFF radio off.
  131. *
  132. * Reports changes in the software RF switch state to the the WiMAX
  133. * stack.
  134. *
  135. * The main use is during initialization, so the driver can query the
  136. * device for its current software radio kill switch state and feed it
  137. * to the system.
  138. *
  139. * On the side, the device does not change the software state by
  140. * itself. In practice, this can happen, as the device might decide to
  141. * switch (in software) the radio off for different reasons.
  142. */
  143. void wimax_report_rfkill_sw(struct wimax_dev *wimax_dev,
  144. enum wimax_rf_state state)
  145. {
  146. int result;
  147. struct device *dev = wimax_dev_to_dev(wimax_dev);
  148. enum wimax_st wimax_state;
  149. d_fnstart(3, dev, "(wimax_dev %p state %u)\n", wimax_dev, state);
  150. BUG_ON(state == WIMAX_RF_QUERY);
  151. BUG_ON(state != WIMAX_RF_ON && state != WIMAX_RF_OFF);
  152. mutex_lock(&wimax_dev->mutex);
  153. result = wimax_dev_is_ready(wimax_dev);
  154. if (result < 0)
  155. goto error_not_ready;
  156. if (state != wimax_dev->rf_sw) {
  157. wimax_dev->rf_sw = state;
  158. if (wimax_dev->rf_hw == WIMAX_RF_ON
  159. && wimax_dev->rf_sw == WIMAX_RF_ON)
  160. wimax_state = WIMAX_ST_READY;
  161. else
  162. wimax_state = WIMAX_ST_RADIO_OFF;
  163. __wimax_state_change(wimax_dev, wimax_state);
  164. }
  165. error_not_ready:
  166. mutex_unlock(&wimax_dev->mutex);
  167. d_fnend(3, dev, "(wimax_dev %p state %u) = void [%d]\n",
  168. wimax_dev, state, result);
  169. }
  170. EXPORT_SYMBOL_GPL(wimax_report_rfkill_sw);
  171. /*
  172. * Callback for the RF Kill toggle operation
  173. *
  174. * This function is called by:
  175. *
  176. * - The rfkill subsystem when the RF-Kill key is pressed in the
  177. * hardware and the driver notifies through
  178. * wimax_report_rfkill_hw(). The rfkill subsystem ends up calling back
  179. * here so the software RF Kill switch state is changed to reflect
  180. * the hardware switch state.
  181. *
  182. * - When the user sets the state through sysfs' rfkill/state file
  183. *
  184. * - When the user calls wimax_rfkill().
  185. *
  186. * This call blocks!
  187. *
  188. * WARNING! When we call rfkill_unregister(), this will be called with
  189. * state 0!
  190. *
  191. * WARNING: wimax_dev must be locked
  192. */
  193. static
  194. int __wimax_rf_toggle_radio(struct wimax_dev *wimax_dev,
  195. enum wimax_rf_state state)
  196. {
  197. int result = 0;
  198. struct device *dev = wimax_dev_to_dev(wimax_dev);
  199. enum wimax_st wimax_state;
  200. might_sleep();
  201. d_fnstart(3, dev, "(wimax_dev %p state %u)\n", wimax_dev, state);
  202. if (wimax_dev->rf_sw == state)
  203. goto out_no_change;
  204. if (wimax_dev->op_rfkill_sw_toggle != NULL)
  205. result = wimax_dev->op_rfkill_sw_toggle(wimax_dev, state);
  206. else if (state == WIMAX_RF_OFF) /* No op? can't turn off */
  207. result = -ENXIO;
  208. else /* No op? can turn on */
  209. result = 0; /* should never happen tho */
  210. if (result >= 0) {
  211. result = 0;
  212. wimax_dev->rf_sw = state;
  213. wimax_state = state == WIMAX_RF_ON ?
  214. WIMAX_ST_READY : WIMAX_ST_RADIO_OFF;
  215. __wimax_state_change(wimax_dev, wimax_state);
  216. }
  217. out_no_change:
  218. d_fnend(3, dev, "(wimax_dev %p state %u) = %d\n",
  219. wimax_dev, state, result);
  220. return result;
  221. }
  222. /*
  223. * Translate from rfkill state to wimax state
  224. *
  225. * NOTE: Special state handling rules here
  226. *
  227. * Just pretend the call didn't happen if we are in a state where
  228. * we know for sure it cannot be handled (WIMAX_ST_DOWN or
  229. * __WIMAX_ST_QUIESCING). rfkill() needs it to register and
  230. * unregister, as it will run this path.
  231. *
  232. * NOTE: This call will block until the operation is completed.
  233. */
  234. static
  235. int wimax_rfkill_toggle_radio(void *data, enum rfkill_state state)
  236. {
  237. int result;
  238. struct wimax_dev *wimax_dev = data;
  239. struct device *dev = wimax_dev_to_dev(wimax_dev);
  240. enum wimax_rf_state rf_state;
  241. d_fnstart(3, dev, "(wimax_dev %p state %u)\n", wimax_dev, state);
  242. switch (state) {
  243. case RFKILL_STATE_ON:
  244. rf_state = WIMAX_RF_OFF;
  245. break;
  246. case RFKILL_STATE_OFF:
  247. rf_state = WIMAX_RF_ON;
  248. break;
  249. default:
  250. BUG();
  251. }
  252. mutex_lock(&wimax_dev->mutex);
  253. if (wimax_dev->state <= __WIMAX_ST_QUIESCING)
  254. result = 0; /* just pretend it didn't happen */
  255. else
  256. result = __wimax_rf_toggle_radio(wimax_dev, rf_state);
  257. mutex_unlock(&wimax_dev->mutex);
  258. d_fnend(3, dev, "(wimax_dev %p state %u) = %d\n",
  259. wimax_dev, state, result);
  260. return result;
  261. }
  262. /**
  263. * wimax_rfkill - Set the software RF switch state for a WiMAX device
  264. *
  265. * @wimax_dev: WiMAX device descriptor
  266. *
  267. * @state: New RF state.
  268. *
  269. * Returns:
  270. *
  271. * >= 0 toggle state if ok, < 0 errno code on error. The toggle state
  272. * is returned as a bitmap, bit 0 being the hardware RF state, bit 1
  273. * the software RF state.
  274. *
  275. * 0 means disabled (%WIMAX_RF_ON, radio on), 1 means enabled radio
  276. * off (%WIMAX_RF_OFF).
  277. *
  278. * Description:
  279. *
  280. * Called by the user when he wants to request the WiMAX radio to be
  281. * switched on (%WIMAX_RF_ON) or off (%WIMAX_RF_OFF). With
  282. * %WIMAX_RF_QUERY, just the current state is returned.
  283. *
  284. * NOTE:
  285. *
  286. * This call will block until the operation is complete.
  287. */
  288. int wimax_rfkill(struct wimax_dev *wimax_dev, enum wimax_rf_state state)
  289. {
  290. int result;
  291. struct device *dev = wimax_dev_to_dev(wimax_dev);
  292. d_fnstart(3, dev, "(wimax_dev %p state %u)\n", wimax_dev, state);
  293. mutex_lock(&wimax_dev->mutex);
  294. result = wimax_dev_is_ready(wimax_dev);
  295. if (result < 0)
  296. goto error_not_ready;
  297. switch (state) {
  298. case WIMAX_RF_ON:
  299. case WIMAX_RF_OFF:
  300. result = __wimax_rf_toggle_radio(wimax_dev, state);
  301. if (result < 0)
  302. goto error;
  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 input_dev *input_dev;
  329. struct device *dev = wimax_dev_to_dev(wimax_dev);
  330. d_fnstart(3, dev, "(wimax_dev %p)\n", wimax_dev);
  331. /* Initialize RF Kill */
  332. result = -ENOMEM;
  333. rfkill = rfkill_allocate(dev, RFKILL_TYPE_WIMAX);
  334. if (rfkill == NULL)
  335. goto error_rfkill_allocate;
  336. wimax_dev->rfkill = rfkill;
  337. rfkill->name = wimax_dev->name;
  338. rfkill->state = RFKILL_STATE_OFF;
  339. rfkill->data = wimax_dev;
  340. rfkill->toggle_radio = wimax_rfkill_toggle_radio;
  341. rfkill->user_claim_unsupported = 1;
  342. /* Initialize the input device for the hw key */
  343. input_dev = input_allocate_device();
  344. if (input_dev == NULL)
  345. goto error_input_allocate;
  346. wimax_dev->rfkill_input = input_dev;
  347. d_printf(1, dev, "rfkill %p input %p\n", rfkill, input_dev);
  348. input_dev->name = wimax_dev->name;
  349. /* FIXME: get a real device bus ID and stuff? do we care? */
  350. input_dev->id.bustype = BUS_HOST;
  351. input_dev->id.vendor = 0xffff;
  352. input_dev->evbit[0] = BIT(EV_KEY);
  353. set_bit(KEY_WIMAX, input_dev->keybit);
  354. /* Register both */
  355. result = input_register_device(wimax_dev->rfkill_input);
  356. if (result < 0)
  357. goto error_input_register;
  358. result = rfkill_register(wimax_dev->rfkill);
  359. if (result < 0)
  360. goto error_rfkill_register;
  361. /* If there is no SW toggle op, SW RFKill is always on */
  362. if (wimax_dev->op_rfkill_sw_toggle == NULL)
  363. wimax_dev->rf_sw = WIMAX_RF_ON;
  364. d_fnend(3, dev, "(wimax_dev %p) = 0\n", wimax_dev);
  365. return 0;
  366. /* if rfkill_register() suceeds, can't use rfkill_free() any
  367. * more, only rfkill_unregister() [it owns the refcount]; with
  368. * the input device we have the same issue--hence the if. */
  369. error_rfkill_register:
  370. input_unregister_device(wimax_dev->rfkill_input);
  371. wimax_dev->rfkill_input = NULL;
  372. error_input_register:
  373. if (wimax_dev->rfkill_input)
  374. input_free_device(wimax_dev->rfkill_input);
  375. error_input_allocate:
  376. rfkill_free(wimax_dev->rfkill);
  377. error_rfkill_allocate:
  378. d_fnend(3, dev, "(wimax_dev %p) = %d\n", wimax_dev, result);
  379. return result;
  380. }
  381. /*
  382. * Deregister a WiMAX device's RF Kill support
  383. *
  384. * Ick, we can't call rfkill_free() after rfkill_unregister()...oh
  385. * well.
  386. *
  387. * WARNING: wimax_dev->mutex must be unlocked
  388. */
  389. void wimax_rfkill_rm(struct wimax_dev *wimax_dev)
  390. {
  391. struct device *dev = wimax_dev_to_dev(wimax_dev);
  392. d_fnstart(3, dev, "(wimax_dev %p)\n", wimax_dev);
  393. rfkill_unregister(wimax_dev->rfkill); /* frees */
  394. input_unregister_device(wimax_dev->rfkill_input);
  395. d_fnend(3, dev, "(wimax_dev %p)\n", wimax_dev);
  396. }
  397. #else /* #ifdef CONFIG_RFKILL */
  398. void wimax_report_rfkill_hw(struct wimax_dev *wimax_dev,
  399. enum wimax_rf_state state)
  400. {
  401. }
  402. EXPORT_SYMBOL_GPL(wimax_report_rfkill_hw);
  403. void wimax_report_rfkill_sw(struct wimax_dev *wimax_dev,
  404. enum wimax_rf_state state)
  405. {
  406. }
  407. EXPORT_SYMBOL_GPL(wimax_report_rfkill_sw);
  408. int wimax_rfkill(struct wimax_dev *wimax_dev,
  409. enum wimax_rf_state state)
  410. {
  411. return WIMAX_RF_ON << 1 | WIMAX_RF_ON;
  412. }
  413. EXPORT_SYMBOL_GPL(wimax_rfkill);
  414. int wimax_rfkill_add(struct wimax_dev *wimax_dev)
  415. {
  416. return 0;
  417. }
  418. void wimax_rfkill_rm(struct wimax_dev *wimax_dev)
  419. {
  420. }
  421. #endif /* #ifdef CONFIG_RFKILL */
  422. /*
  423. * Exporting to user space over generic netlink
  424. *
  425. * Parse the rfkill command from user space, return a combination
  426. * value that describe the states of the different toggles.
  427. *
  428. * Only one attribute: the new state requested (on, off or no change,
  429. * just query).
  430. */
  431. static const
  432. struct nla_policy wimax_gnl_rfkill_policy[WIMAX_GNL_ATTR_MAX + 1] = {
  433. [WIMAX_GNL_RFKILL_IFIDX] = {
  434. .type = NLA_U32,
  435. },
  436. [WIMAX_GNL_RFKILL_STATE] = {
  437. .type = NLA_U32 /* enum wimax_rf_state */
  438. },
  439. };
  440. static
  441. int wimax_gnl_doit_rfkill(struct sk_buff *skb, struct genl_info *info)
  442. {
  443. int result, ifindex;
  444. struct wimax_dev *wimax_dev;
  445. struct device *dev;
  446. enum wimax_rf_state new_state;
  447. d_fnstart(3, NULL, "(skb %p info %p)\n", skb, info);
  448. result = -ENODEV;
  449. if (info->attrs[WIMAX_GNL_RFKILL_IFIDX] == NULL) {
  450. printk(KERN_ERR "WIMAX_GNL_OP_RFKILL: can't find IFIDX "
  451. "attribute\n");
  452. goto error_no_wimax_dev;
  453. }
  454. ifindex = nla_get_u32(info->attrs[WIMAX_GNL_RFKILL_IFIDX]);
  455. wimax_dev = wimax_dev_get_by_genl_info(info, ifindex);
  456. if (wimax_dev == NULL)
  457. goto error_no_wimax_dev;
  458. dev = wimax_dev_to_dev(wimax_dev);
  459. result = -EINVAL;
  460. if (info->attrs[WIMAX_GNL_RFKILL_STATE] == NULL) {
  461. dev_err(dev, "WIMAX_GNL_RFKILL: can't find RFKILL_STATE "
  462. "attribute\n");
  463. goto error_no_pid;
  464. }
  465. new_state = nla_get_u32(info->attrs[WIMAX_GNL_RFKILL_STATE]);
  466. /* Execute the operation and send the result back to user space */
  467. result = wimax_rfkill(wimax_dev, new_state);
  468. error_no_pid:
  469. dev_put(wimax_dev->net_dev);
  470. error_no_wimax_dev:
  471. d_fnend(3, NULL, "(skb %p info %p) = %d\n", skb, info, result);
  472. return result;
  473. }
  474. struct genl_ops wimax_gnl_rfkill = {
  475. .cmd = WIMAX_GNL_OP_RFKILL,
  476. .flags = GENL_ADMIN_PERM,
  477. .policy = wimax_gnl_rfkill_policy,
  478. .doit = wimax_gnl_doit_rfkill,
  479. .dumpit = NULL,
  480. };