op-rfkill.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Intel Wireless WiMAX Connection 2400m
  3. * Implement backend for the WiMAX stack rfkill support
  4. *
  5. *
  6. * Copyright (C) 2007-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. * The WiMAX kernel stack integrates into RF-Kill and keeps the
  25. * switches's status. We just need to:
  26. *
  27. * - report changes in the HW RF Kill switch [with
  28. * wimax_rfkill_{sw,hw}_report(), which happens when we detect those
  29. * indications coming through hardware reports]. We also do it on
  30. * initialization to let the stack know the initial HW state.
  31. *
  32. * - implement indications from the stack to change the SW RF Kill
  33. * switch (coming from sysfs, the wimax stack or user space).
  34. */
  35. #include "i2400m.h"
  36. #include <linux/wimax/i2400m.h>
  37. #include <linux/slab.h>
  38. #define D_SUBMODULE rfkill
  39. #include "debug-levels.h"
  40. /*
  41. * Return true if the i2400m radio is in the requested wimax_rf_state state
  42. *
  43. */
  44. static
  45. int i2400m_radio_is(struct i2400m *i2400m, enum wimax_rf_state state)
  46. {
  47. if (state == WIMAX_RF_OFF)
  48. return i2400m->state == I2400M_SS_RF_OFF
  49. || i2400m->state == I2400M_SS_RF_SHUTDOWN;
  50. else if (state == WIMAX_RF_ON)
  51. /* state == WIMAX_RF_ON */
  52. return i2400m->state != I2400M_SS_RF_OFF
  53. && i2400m->state != I2400M_SS_RF_SHUTDOWN;
  54. else {
  55. BUG();
  56. return -EINVAL; /* shut gcc warnings on certain arches */
  57. }
  58. }
  59. /*
  60. * WiMAX stack operation: implement SW RFKill toggling
  61. *
  62. * @wimax_dev: device descriptor
  63. * @skb: skb where the message has been received; skb->data is
  64. * expected to point to the message payload.
  65. * @genl_info: passed by the generic netlink layer
  66. *
  67. * Generic Netlink will call this function when a message is sent from
  68. * userspace to change the software RF-Kill switch status.
  69. *
  70. * This function will set the device's software RF-Kill switch state to
  71. * match what is requested.
  72. *
  73. * NOTE: the i2400m has a strict state machine; we can only set the
  74. * RF-Kill switch when it is on, the HW RF-Kill is on and the
  75. * device is initialized. So we ignore errors steaming from not
  76. * being in the right state (-EILSEQ).
  77. */
  78. int i2400m_op_rfkill_sw_toggle(struct wimax_dev *wimax_dev,
  79. enum wimax_rf_state state)
  80. {
  81. int result;
  82. struct i2400m *i2400m = wimax_dev_to_i2400m(wimax_dev);
  83. struct device *dev = i2400m_dev(i2400m);
  84. struct sk_buff *ack_skb;
  85. struct {
  86. struct i2400m_l3l4_hdr hdr;
  87. struct i2400m_tlv_rf_operation sw_rf;
  88. } __packed *cmd;
  89. char strerr[32];
  90. d_fnstart(4, dev, "(wimax_dev %p state %d)\n", wimax_dev, state);
  91. result = -ENOMEM;
  92. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  93. if (cmd == NULL)
  94. goto error_alloc;
  95. cmd->hdr.type = cpu_to_le16(I2400M_MT_CMD_RF_CONTROL);
  96. cmd->hdr.length = sizeof(cmd->sw_rf);
  97. cmd->hdr.version = cpu_to_le16(I2400M_L3L4_VERSION);
  98. cmd->sw_rf.hdr.type = cpu_to_le16(I2400M_TLV_RF_OPERATION);
  99. cmd->sw_rf.hdr.length = cpu_to_le16(sizeof(cmd->sw_rf.status));
  100. switch (state) {
  101. case WIMAX_RF_OFF: /* RFKILL ON, radio OFF */
  102. cmd->sw_rf.status = cpu_to_le32(2);
  103. break;
  104. case WIMAX_RF_ON: /* RFKILL OFF, radio ON */
  105. cmd->sw_rf.status = cpu_to_le32(1);
  106. break;
  107. default:
  108. BUG();
  109. }
  110. ack_skb = i2400m_msg_to_dev(i2400m, cmd, sizeof(*cmd));
  111. result = PTR_ERR(ack_skb);
  112. if (IS_ERR(ack_skb)) {
  113. dev_err(dev, "Failed to issue 'RF Control' command: %d\n",
  114. result);
  115. goto error_msg_to_dev;
  116. }
  117. result = i2400m_msg_check_status(wimax_msg_data(ack_skb),
  118. strerr, sizeof(strerr));
  119. if (result < 0) {
  120. dev_err(dev, "'RF Control' (0x%04x) command failed: %d - %s\n",
  121. I2400M_MT_CMD_RF_CONTROL, result, strerr);
  122. goto error_cmd;
  123. }
  124. /* Now we wait for the state to change to RADIO_OFF or RADIO_ON */
  125. result = wait_event_timeout(
  126. i2400m->state_wq, i2400m_radio_is(i2400m, state),
  127. 5 * HZ);
  128. if (result == 0)
  129. result = -ETIMEDOUT;
  130. if (result < 0)
  131. dev_err(dev, "Error waiting for device to toggle RF state: "
  132. "%d\n", result);
  133. result = 0;
  134. error_cmd:
  135. kfree_skb(ack_skb);
  136. error_msg_to_dev:
  137. error_alloc:
  138. d_fnend(4, dev, "(wimax_dev %p state %d) = %d\n",
  139. wimax_dev, state, result);
  140. return result;
  141. }
  142. /*
  143. * Inform the WiMAX stack of changes in the RF Kill switches reported
  144. * by the device
  145. *
  146. * @i2400m: device descriptor
  147. * @rfss: TLV for RF Switches status; already validated
  148. *
  149. * NOTE: the reports on RF switch status cannot be trusted
  150. * or used until the device is in a state of RADIO_OFF
  151. * or greater.
  152. */
  153. void i2400m_report_tlv_rf_switches_status(
  154. struct i2400m *i2400m,
  155. const struct i2400m_tlv_rf_switches_status *rfss)
  156. {
  157. struct device *dev = i2400m_dev(i2400m);
  158. enum i2400m_rf_switch_status hw, sw;
  159. enum wimax_st wimax_state;
  160. sw = le32_to_cpu(rfss->sw_rf_switch);
  161. hw = le32_to_cpu(rfss->hw_rf_switch);
  162. d_fnstart(3, dev, "(i2400m %p rfss %p [hw %u sw %u])\n",
  163. i2400m, rfss, hw, sw);
  164. /* We only process rw switch evens when the device has been
  165. * fully initialized */
  166. wimax_state = wimax_state_get(&i2400m->wimax_dev);
  167. if (wimax_state < WIMAX_ST_RADIO_OFF) {
  168. d_printf(3, dev, "ignoring RF switches report, state %u\n",
  169. wimax_state);
  170. goto out;
  171. }
  172. switch (sw) {
  173. case I2400M_RF_SWITCH_ON: /* RF Kill disabled (radio on) */
  174. wimax_report_rfkill_sw(&i2400m->wimax_dev, WIMAX_RF_ON);
  175. break;
  176. case I2400M_RF_SWITCH_OFF: /* RF Kill enabled (radio off) */
  177. wimax_report_rfkill_sw(&i2400m->wimax_dev, WIMAX_RF_OFF);
  178. break;
  179. default:
  180. dev_err(dev, "HW BUG? Unknown RF SW state 0x%x\n", sw);
  181. }
  182. switch (hw) {
  183. case I2400M_RF_SWITCH_ON: /* RF Kill disabled (radio on) */
  184. wimax_report_rfkill_hw(&i2400m->wimax_dev, WIMAX_RF_ON);
  185. break;
  186. case I2400M_RF_SWITCH_OFF: /* RF Kill enabled (radio off) */
  187. wimax_report_rfkill_hw(&i2400m->wimax_dev, WIMAX_RF_OFF);
  188. break;
  189. default:
  190. dev_err(dev, "HW BUG? Unknown RF HW state 0x%x\n", hw);
  191. }
  192. out:
  193. d_fnend(3, dev, "(i2400m %p rfss %p [hw %u sw %u]) = void\n",
  194. i2400m, rfss, hw, sw);
  195. }