added 2.6.29.6 aldebaran kernel
[nao-ulib.git] / kernel / 2.6.29.6-aldebaran-rt / drivers / net / wimax / i2400m / op-rfkill.c
blob487ec58cea46563ccf7c561f2c83fc1890545f21
1 /*
2 * Intel Wireless WiMAX Connection 2400m
3 * Implement backend for the WiMAX stack rfkill support
6 * Copyright (C) 2007-2008 Intel Corporation <linux-wimax@intel.com>
7 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
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.
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.
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.
24 * The WiMAX kernel stack integrates into RF-Kill and keeps the
25 * switches's status. We just need to:
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 intial HW state.
32 * - implement indications from the stack to change the SW RF Kill
33 * switch (coming from sysfs, the wimax stack or user space).
35 #include "i2400m.h"
36 #include <linux/wimax/i2400m.h>
40 #define D_SUBMODULE rfkill
41 #include "debug-levels.h"
44 * Return true if the i2400m radio is in the requested wimax_rf_state state
47 static
48 int i2400m_radio_is(struct i2400m *i2400m, enum wimax_rf_state state)
50 if (state == WIMAX_RF_OFF)
51 return i2400m->state == I2400M_SS_RF_OFF
52 || i2400m->state == I2400M_SS_RF_SHUTDOWN;
53 else if (state == WIMAX_RF_ON)
54 /* state == WIMAX_RF_ON */
55 return i2400m->state != I2400M_SS_RF_OFF
56 && i2400m->state != I2400M_SS_RF_SHUTDOWN;
57 else
58 BUG();
63 * WiMAX stack operation: implement SW RFKill toggling
65 * @wimax_dev: device descriptor
66 * @skb: skb where the message has been received; skb->data is
67 * expected to point to the message payload.
68 * @genl_info: passed by the generic netlink layer
70 * Generic Netlink will call this function when a message is sent from
71 * userspace to change the software RF-Kill switch status.
73 * This function will set the device's sofware RF-Kill switch state to
74 * match what is requested.
76 * NOTE: the i2400m has a strict state machine; we can only set the
77 * RF-Kill switch when it is on, the HW RF-Kill is on and the
78 * device is initialized. So we ignore errors steaming from not
79 * being in the right state (-EILSEQ).
81 int i2400m_op_rfkill_sw_toggle(struct wimax_dev *wimax_dev,
82 enum wimax_rf_state state)
84 int result;
85 struct i2400m *i2400m = wimax_dev_to_i2400m(wimax_dev);
86 struct device *dev = i2400m_dev(i2400m);
87 struct sk_buff *ack_skb;
88 struct {
89 struct i2400m_l3l4_hdr hdr;
90 struct i2400m_tlv_rf_operation sw_rf;
91 } __attribute__((packed)) *cmd;
92 char strerr[32];
94 d_fnstart(4, dev, "(wimax_dev %p state %d)\n", wimax_dev, state);
96 result = -ENOMEM;
97 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
98 if (cmd == NULL)
99 goto error_alloc;
100 cmd->hdr.type = cpu_to_le16(I2400M_MT_CMD_RF_CONTROL);
101 cmd->hdr.length = sizeof(cmd->sw_rf);
102 cmd->hdr.version = cpu_to_le16(I2400M_L3L4_VERSION);
103 cmd->sw_rf.hdr.type = cpu_to_le16(I2400M_TLV_RF_OPERATION);
104 cmd->sw_rf.hdr.length = cpu_to_le16(sizeof(cmd->sw_rf.status));
105 switch (state) {
106 case WIMAX_RF_OFF: /* RFKILL ON, radio OFF */
107 cmd->sw_rf.status = cpu_to_le32(2);
108 break;
109 case WIMAX_RF_ON: /* RFKILL OFF, radio ON */
110 cmd->sw_rf.status = cpu_to_le32(1);
111 break;
112 default:
113 BUG();
116 ack_skb = i2400m_msg_to_dev(i2400m, cmd, sizeof(*cmd));
117 result = PTR_ERR(ack_skb);
118 if (IS_ERR(ack_skb)) {
119 dev_err(dev, "Failed to issue 'RF Control' command: %d\n",
120 result);
121 goto error_msg_to_dev;
123 result = i2400m_msg_check_status(wimax_msg_data(ack_skb),
124 strerr, sizeof(strerr));
125 if (result < 0) {
126 dev_err(dev, "'RF Control' (0x%04x) command failed: %d - %s\n",
127 I2400M_MT_CMD_RF_CONTROL, result, strerr);
128 goto error_cmd;
131 /* Now we wait for the state to change to RADIO_OFF or RADIO_ON */
132 result = wait_event_timeout(
133 i2400m->state_wq, i2400m_radio_is(i2400m, state),
134 5 * HZ);
135 if (result == 0)
136 result = -ETIMEDOUT;
137 if (result < 0)
138 dev_err(dev, "Error waiting for device to toggle RF state: "
139 "%d\n", result);
140 result = 0;
141 error_cmd:
142 kfree_skb(ack_skb);
143 error_msg_to_dev:
144 error_alloc:
145 d_fnend(4, dev, "(wimax_dev %p state %d) = %d\n",
146 wimax_dev, state, result);
147 return result;
152 * Inform the WiMAX stack of changes in the RF Kill switches reported
153 * by the device
155 * @i2400m: device descriptor
156 * @rfss: TLV for RF Switches status; already validated
158 * NOTE: the reports on RF switch status cannot be trusted
159 * or used until the device is in a state of RADIO_OFF
160 * or greater.
162 void i2400m_report_tlv_rf_switches_status(
163 struct i2400m *i2400m,
164 const struct i2400m_tlv_rf_switches_status *rfss)
166 struct device *dev = i2400m_dev(i2400m);
167 enum i2400m_rf_switch_status hw, sw;
168 enum wimax_st wimax_state;
170 sw = le32_to_cpu(rfss->sw_rf_switch);
171 hw = le32_to_cpu(rfss->hw_rf_switch);
173 d_fnstart(3, dev, "(i2400m %p rfss %p [hw %u sw %u])\n",
174 i2400m, rfss, hw, sw);
175 /* We only process rw switch evens when the device has been
176 * fully initialized */
177 wimax_state = wimax_state_get(&i2400m->wimax_dev);
178 if (wimax_state < WIMAX_ST_RADIO_OFF) {
179 d_printf(3, dev, "ignoring RF switches report, state %u\n",
180 wimax_state);
181 goto out;
183 switch (sw) {
184 case I2400M_RF_SWITCH_ON: /* RF Kill disabled (radio on) */
185 wimax_report_rfkill_sw(&i2400m->wimax_dev, WIMAX_RF_ON);
186 break;
187 case I2400M_RF_SWITCH_OFF: /* RF Kill enabled (radio off) */
188 wimax_report_rfkill_sw(&i2400m->wimax_dev, WIMAX_RF_OFF);
189 break;
190 default:
191 dev_err(dev, "HW BUG? Unknown RF SW state 0x%x\n", sw);
194 switch (hw) {
195 case I2400M_RF_SWITCH_ON: /* RF Kill disabled (radio on) */
196 wimax_report_rfkill_hw(&i2400m->wimax_dev, WIMAX_RF_ON);
197 break;
198 case I2400M_RF_SWITCH_OFF: /* RF Kill enabled (radio off) */
199 wimax_report_rfkill_hw(&i2400m->wimax_dev, WIMAX_RF_OFF);
200 break;
201 default:
202 dev_err(dev, "HW BUG? Unknown RF HW state 0x%x\n", hw);
204 out:
205 d_fnend(3, dev, "(i2400m %p rfss %p [hw %u sw %u]) = void\n",
206 i2400m, rfss, hw, sw);