Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 1 | /* |
| 2 | Copyright (C) 2004 - 2007 rt2x00 SourceForge Project |
| 3 | <http://rt2x00.serialmonkey.com> |
| 4 | |
| 5 | This program is free software; you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation; either version 2 of the License, or |
| 8 | (at your option) any later version. |
| 9 | |
| 10 | This program is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with this program; if not, write to the |
| 17 | Free Software Foundation, Inc., |
| 18 | 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 19 | */ |
| 20 | |
| 21 | /* |
| 22 | Module: rt2x00lib |
| 23 | Abstract: rt2x00 generic device routines. |
| 24 | */ |
| 25 | |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 26 | #include <linux/kernel.h> |
| 27 | #include <linux/module.h> |
| 28 | |
| 29 | #include "rt2x00.h" |
| 30 | #include "rt2x00lib.h" |
Ivo van Doorn | 4d8dd66 | 2007-11-27 21:49:29 +0100 | [diff] [blame] | 31 | #include "rt2x00dump.h" |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 32 | |
| 33 | /* |
| 34 | * Ring handler. |
| 35 | */ |
| 36 | struct data_ring *rt2x00lib_get_ring(struct rt2x00_dev *rt2x00dev, |
| 37 | const unsigned int queue) |
| 38 | { |
Ivo van Doorn | 066cb63 | 2007-09-25 20:55:39 +0200 | [diff] [blame] | 39 | int beacon = test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags); |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 40 | |
| 41 | /* |
| 42 | * Check if we are requesting a reqular TX ring, |
| 43 | * or if we are requesting a Beacon or Atim ring. |
| 44 | * For Atim rings, we should check if it is supported. |
| 45 | */ |
| 46 | if (queue < rt2x00dev->hw->queues && rt2x00dev->tx) |
| 47 | return &rt2x00dev->tx[queue]; |
| 48 | |
| 49 | if (!rt2x00dev->bcn || !beacon) |
| 50 | return NULL; |
| 51 | |
| 52 | if (queue == IEEE80211_TX_QUEUE_BEACON) |
| 53 | return &rt2x00dev->bcn[0]; |
| 54 | else if (queue == IEEE80211_TX_QUEUE_AFTER_BEACON) |
| 55 | return &rt2x00dev->bcn[1]; |
| 56 | |
| 57 | return NULL; |
| 58 | } |
| 59 | EXPORT_SYMBOL_GPL(rt2x00lib_get_ring); |
| 60 | |
| 61 | /* |
| 62 | * Link tuning handlers |
| 63 | */ |
| 64 | static void rt2x00lib_start_link_tuner(struct rt2x00_dev *rt2x00dev) |
| 65 | { |
Ivo van Doorn | 8de8c51 | 2007-10-13 16:26:32 +0200 | [diff] [blame] | 66 | rt2x00dev->link.count = 0; |
| 67 | rt2x00dev->link.vgc_level = 0; |
| 68 | |
| 69 | memset(&rt2x00dev->link.qual, 0, sizeof(rt2x00dev->link.qual)); |
| 70 | |
| 71 | /* |
| 72 | * The RX and TX percentage should start at 50% |
| 73 | * this will assure we will get at least get some |
| 74 | * decent value when the link tuner starts. |
| 75 | * The value will be dropped and overwritten with |
| 76 | * the correct (measured )value anyway during the |
| 77 | * first run of the link tuner. |
| 78 | */ |
| 79 | rt2x00dev->link.qual.rx_percentage = 50; |
| 80 | rt2x00dev->link.qual.tx_percentage = 50; |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 81 | |
| 82 | /* |
| 83 | * Reset the link tuner. |
| 84 | */ |
| 85 | rt2x00dev->ops->lib->reset_tuner(rt2x00dev); |
| 86 | |
| 87 | queue_delayed_work(rt2x00dev->hw->workqueue, |
| 88 | &rt2x00dev->link.work, LINK_TUNE_INTERVAL); |
| 89 | } |
| 90 | |
| 91 | static void rt2x00lib_stop_link_tuner(struct rt2x00_dev *rt2x00dev) |
| 92 | { |
Ivo van Doorn | 3e30968 | 2007-09-25 20:56:36 +0200 | [diff] [blame] | 93 | cancel_delayed_work_sync(&rt2x00dev->link.work); |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | void rt2x00lib_reset_link_tuner(struct rt2x00_dev *rt2x00dev) |
| 97 | { |
Ivo van Doorn | fdd0abc | 2007-09-25 20:57:49 +0200 | [diff] [blame] | 98 | if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags)) |
| 99 | return; |
| 100 | |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 101 | rt2x00lib_stop_link_tuner(rt2x00dev); |
| 102 | rt2x00lib_start_link_tuner(rt2x00dev); |
| 103 | } |
| 104 | |
| 105 | /* |
Ivo van Doorn | 837e7f2 | 2008-01-06 23:41:45 +0100 | [diff] [blame] | 106 | * Ring initialization |
| 107 | */ |
| 108 | static void rt2x00lib_init_rxrings(struct rt2x00_dev *rt2x00dev) |
| 109 | { |
| 110 | struct data_ring *ring = rt2x00dev->rx; |
| 111 | unsigned int i; |
| 112 | |
| 113 | if (!rt2x00dev->ops->lib->init_rxentry) |
| 114 | return; |
| 115 | |
| 116 | if (ring->data_addr) |
| 117 | memset(ring->data_addr, 0, rt2x00_get_ring_size(ring)); |
| 118 | |
| 119 | for (i = 0; i < ring->stats.limit; i++) |
| 120 | rt2x00dev->ops->lib->init_rxentry(rt2x00dev, &ring->entry[i]); |
| 121 | |
| 122 | rt2x00_ring_index_clear(ring); |
| 123 | } |
| 124 | |
| 125 | static void rt2x00lib_init_txrings(struct rt2x00_dev *rt2x00dev) |
| 126 | { |
| 127 | struct data_ring *ring; |
| 128 | unsigned int i; |
| 129 | |
| 130 | if (!rt2x00dev->ops->lib->init_txentry) |
| 131 | return; |
| 132 | |
| 133 | txringall_for_each(rt2x00dev, ring) { |
| 134 | if (ring->data_addr) |
| 135 | memset(ring->data_addr, 0, rt2x00_get_ring_size(ring)); |
| 136 | |
| 137 | for (i = 0; i < ring->stats.limit; i++) |
| 138 | rt2x00dev->ops->lib->init_txentry(rt2x00dev, |
| 139 | &ring->entry[i]); |
| 140 | |
| 141 | rt2x00_ring_index_clear(ring); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | /* |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 146 | * Radio control handlers. |
| 147 | */ |
| 148 | int rt2x00lib_enable_radio(struct rt2x00_dev *rt2x00dev) |
| 149 | { |
| 150 | int status; |
| 151 | |
| 152 | /* |
| 153 | * Don't enable the radio twice. |
| 154 | * And check if the hardware button has been disabled. |
| 155 | */ |
| 156 | if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags) || |
Ivo van Doorn | 81873e9 | 2007-10-06 14:14:06 +0200 | [diff] [blame] | 157 | test_bit(DEVICE_DISABLED_RADIO_HW, &rt2x00dev->flags)) |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 158 | return 0; |
| 159 | |
| 160 | /* |
Ivo van Doorn | 837e7f2 | 2008-01-06 23:41:45 +0100 | [diff] [blame] | 161 | * Initialize all data rings. |
| 162 | */ |
| 163 | rt2x00lib_init_rxrings(rt2x00dev); |
| 164 | rt2x00lib_init_txrings(rt2x00dev); |
| 165 | |
| 166 | /* |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 167 | * Enable radio. |
| 168 | */ |
| 169 | status = rt2x00dev->ops->lib->set_device_state(rt2x00dev, |
| 170 | STATE_RADIO_ON); |
| 171 | if (status) |
| 172 | return status; |
| 173 | |
| 174 | __set_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags); |
| 175 | |
| 176 | /* |
| 177 | * Enable RX. |
| 178 | */ |
Ivo van Doorn | 5cbf830 | 2007-10-06 14:16:09 +0200 | [diff] [blame] | 179 | rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_ON); |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 180 | |
| 181 | /* |
| 182 | * Start the TX queues. |
| 183 | */ |
| 184 | ieee80211_start_queues(rt2x00dev->hw); |
| 185 | |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | void rt2x00lib_disable_radio(struct rt2x00_dev *rt2x00dev) |
| 190 | { |
| 191 | if (!__test_and_clear_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags)) |
| 192 | return; |
| 193 | |
| 194 | /* |
Johannes Berg | 4150c57 | 2007-09-17 01:29:23 -0400 | [diff] [blame] | 195 | * Stop all scheduled work. |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 196 | */ |
| 197 | if (work_pending(&rt2x00dev->beacon_work)) |
| 198 | cancel_work_sync(&rt2x00dev->beacon_work); |
Johannes Berg | 4150c57 | 2007-09-17 01:29:23 -0400 | [diff] [blame] | 199 | if (work_pending(&rt2x00dev->filter_work)) |
| 200 | cancel_work_sync(&rt2x00dev->filter_work); |
Ivo van Doorn | 5c58ee5 | 2007-10-06 13:34:52 +0200 | [diff] [blame] | 201 | if (work_pending(&rt2x00dev->config_work)) |
| 202 | cancel_work_sync(&rt2x00dev->config_work); |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 203 | |
| 204 | /* |
| 205 | * Stop the TX queues. |
| 206 | */ |
| 207 | ieee80211_stop_queues(rt2x00dev->hw); |
| 208 | |
| 209 | /* |
| 210 | * Disable RX. |
| 211 | */ |
Ivo van Doorn | 5cbf830 | 2007-10-06 14:16:09 +0200 | [diff] [blame] | 212 | rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_OFF); |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 213 | |
| 214 | /* |
| 215 | * Disable radio. |
| 216 | */ |
| 217 | rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_OFF); |
| 218 | } |
| 219 | |
Ivo van Doorn | 5cbf830 | 2007-10-06 14:16:09 +0200 | [diff] [blame] | 220 | void rt2x00lib_toggle_rx(struct rt2x00_dev *rt2x00dev, enum dev_state state) |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 221 | { |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 222 | /* |
| 223 | * When we are disabling the RX, we should also stop the link tuner. |
| 224 | */ |
Ivo van Doorn | 5cbf830 | 2007-10-06 14:16:09 +0200 | [diff] [blame] | 225 | if (state == STATE_RADIO_RX_OFF) |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 226 | rt2x00lib_stop_link_tuner(rt2x00dev); |
| 227 | |
| 228 | rt2x00dev->ops->lib->set_device_state(rt2x00dev, state); |
| 229 | |
| 230 | /* |
| 231 | * When we are enabling the RX, we should also start the link tuner. |
| 232 | */ |
Ivo van Doorn | 5cbf830 | 2007-10-06 14:16:09 +0200 | [diff] [blame] | 233 | if (state == STATE_RADIO_RX_ON && |
| 234 | is_interface_present(&rt2x00dev->interface)) |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 235 | rt2x00lib_start_link_tuner(rt2x00dev); |
| 236 | } |
| 237 | |
Ivo van Doorn | 69f81a2 | 2007-10-13 16:26:36 +0200 | [diff] [blame] | 238 | static void rt2x00lib_evaluate_antenna_sample(struct rt2x00_dev *rt2x00dev) |
| 239 | { |
| 240 | enum antenna rx = rt2x00dev->link.ant.active.rx; |
| 241 | enum antenna tx = rt2x00dev->link.ant.active.tx; |
| 242 | int sample_a = |
| 243 | rt2x00_get_link_ant_rssi_history(&rt2x00dev->link, ANTENNA_A); |
| 244 | int sample_b = |
| 245 | rt2x00_get_link_ant_rssi_history(&rt2x00dev->link, ANTENNA_B); |
| 246 | |
| 247 | /* |
| 248 | * We are done sampling. Now we should evaluate the results. |
| 249 | */ |
| 250 | rt2x00dev->link.ant.flags &= ~ANTENNA_MODE_SAMPLE; |
| 251 | |
| 252 | /* |
| 253 | * During the last period we have sampled the RSSI |
| 254 | * from both antenna's. It now is time to determine |
| 255 | * which antenna demonstrated the best performance. |
| 256 | * When we are already on the antenna with the best |
| 257 | * performance, then there really is nothing for us |
| 258 | * left to do. |
| 259 | */ |
| 260 | if (sample_a == sample_b) |
| 261 | return; |
| 262 | |
| 263 | if (rt2x00dev->link.ant.flags & ANTENNA_RX_DIVERSITY) { |
| 264 | if (sample_a > sample_b && rx == ANTENNA_B) |
| 265 | rx = ANTENNA_A; |
| 266 | else if (rx == ANTENNA_A) |
| 267 | rx = ANTENNA_B; |
| 268 | } |
| 269 | |
| 270 | if (rt2x00dev->link.ant.flags & ANTENNA_TX_DIVERSITY) { |
| 271 | if (sample_a > sample_b && tx == ANTENNA_B) |
| 272 | tx = ANTENNA_A; |
| 273 | else if (tx == ANTENNA_A) |
| 274 | tx = ANTENNA_B; |
| 275 | } |
| 276 | |
| 277 | rt2x00lib_config_antenna(rt2x00dev, rx, tx); |
| 278 | } |
| 279 | |
| 280 | static void rt2x00lib_evaluate_antenna_eval(struct rt2x00_dev *rt2x00dev) |
| 281 | { |
| 282 | enum antenna rx = rt2x00dev->link.ant.active.rx; |
| 283 | enum antenna tx = rt2x00dev->link.ant.active.tx; |
| 284 | int rssi_curr = rt2x00_get_link_ant_rssi(&rt2x00dev->link); |
| 285 | int rssi_old = rt2x00_update_ant_rssi(&rt2x00dev->link, rssi_curr); |
| 286 | |
| 287 | /* |
| 288 | * Legacy driver indicates that we should swap antenna's |
| 289 | * when the difference in RSSI is greater that 5. This |
| 290 | * also should be done when the RSSI was actually better |
| 291 | * then the previous sample. |
| 292 | * When the difference exceeds the threshold we should |
| 293 | * sample the rssi from the other antenna to make a valid |
| 294 | * comparison between the 2 antennas. |
| 295 | */ |
| 296 | if ((rssi_curr - rssi_old) > -5 || (rssi_curr - rssi_old) < 5) |
| 297 | return; |
| 298 | |
| 299 | rt2x00dev->link.ant.flags |= ANTENNA_MODE_SAMPLE; |
| 300 | |
| 301 | if (rt2x00dev->link.ant.flags & ANTENNA_RX_DIVERSITY) |
| 302 | rx = (rx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A; |
| 303 | |
| 304 | if (rt2x00dev->link.ant.flags & ANTENNA_TX_DIVERSITY) |
| 305 | tx = (tx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A; |
| 306 | |
| 307 | rt2x00lib_config_antenna(rt2x00dev, rx, tx); |
| 308 | } |
| 309 | |
| 310 | static void rt2x00lib_evaluate_antenna(struct rt2x00_dev *rt2x00dev) |
| 311 | { |
| 312 | /* |
| 313 | * Determine if software diversity is enabled for |
| 314 | * either the TX or RX antenna (or both). |
| 315 | * Always perform this check since within the link |
| 316 | * tuner interval the configuration might have changed. |
| 317 | */ |
| 318 | rt2x00dev->link.ant.flags &= ~ANTENNA_RX_DIVERSITY; |
| 319 | rt2x00dev->link.ant.flags &= ~ANTENNA_TX_DIVERSITY; |
| 320 | |
| 321 | if (rt2x00dev->hw->conf.antenna_sel_rx == 0 && |
| 322 | rt2x00dev->default_ant.rx != ANTENNA_SW_DIVERSITY) |
| 323 | rt2x00dev->link.ant.flags |= ANTENNA_RX_DIVERSITY; |
| 324 | if (rt2x00dev->hw->conf.antenna_sel_tx == 0 && |
| 325 | rt2x00dev->default_ant.tx != ANTENNA_SW_DIVERSITY) |
| 326 | rt2x00dev->link.ant.flags |= ANTENNA_TX_DIVERSITY; |
| 327 | |
| 328 | if (!(rt2x00dev->link.ant.flags & ANTENNA_RX_DIVERSITY) && |
| 329 | !(rt2x00dev->link.ant.flags & ANTENNA_TX_DIVERSITY)) { |
| 330 | rt2x00dev->link.ant.flags &= ~ANTENNA_MODE_SAMPLE; |
| 331 | return; |
| 332 | } |
| 333 | |
| 334 | /* |
| 335 | * If we have only sampled the data over the last period |
| 336 | * we should now harvest the data. Otherwise just evaluate |
| 337 | * the data. The latter should only be performed once |
| 338 | * every 2 seconds. |
| 339 | */ |
| 340 | if (rt2x00dev->link.ant.flags & ANTENNA_MODE_SAMPLE) |
| 341 | rt2x00lib_evaluate_antenna_sample(rt2x00dev); |
| 342 | else if (rt2x00dev->link.count & 1) |
| 343 | rt2x00lib_evaluate_antenna_eval(rt2x00dev); |
| 344 | } |
| 345 | |
| 346 | static void rt2x00lib_update_link_stats(struct link *link, int rssi) |
| 347 | { |
| 348 | int avg_rssi = rssi; |
| 349 | |
| 350 | /* |
| 351 | * Update global RSSI |
| 352 | */ |
| 353 | if (link->qual.avg_rssi) |
| 354 | avg_rssi = MOVING_AVERAGE(link->qual.avg_rssi, rssi, 8); |
| 355 | link->qual.avg_rssi = avg_rssi; |
| 356 | |
| 357 | /* |
| 358 | * Update antenna RSSI |
| 359 | */ |
| 360 | if (link->ant.rssi_ant) |
| 361 | rssi = MOVING_AVERAGE(link->ant.rssi_ant, rssi, 8); |
| 362 | link->ant.rssi_ant = rssi; |
| 363 | } |
| 364 | |
Ivo van Doorn | ebcf26d | 2007-10-13 16:26:12 +0200 | [diff] [blame] | 365 | static void rt2x00lib_precalculate_link_signal(struct link_qual *qual) |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 366 | { |
Ivo van Doorn | ebcf26d | 2007-10-13 16:26:12 +0200 | [diff] [blame] | 367 | if (qual->rx_failed || qual->rx_success) |
| 368 | qual->rx_percentage = |
| 369 | (qual->rx_success * 100) / |
| 370 | (qual->rx_failed + qual->rx_success); |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 371 | else |
Ivo van Doorn | ebcf26d | 2007-10-13 16:26:12 +0200 | [diff] [blame] | 372 | qual->rx_percentage = 50; |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 373 | |
Ivo van Doorn | ebcf26d | 2007-10-13 16:26:12 +0200 | [diff] [blame] | 374 | if (qual->tx_failed || qual->tx_success) |
| 375 | qual->tx_percentage = |
| 376 | (qual->tx_success * 100) / |
| 377 | (qual->tx_failed + qual->tx_success); |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 378 | else |
Ivo van Doorn | ebcf26d | 2007-10-13 16:26:12 +0200 | [diff] [blame] | 379 | qual->tx_percentage = 50; |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 380 | |
Ivo van Doorn | ebcf26d | 2007-10-13 16:26:12 +0200 | [diff] [blame] | 381 | qual->rx_success = 0; |
| 382 | qual->rx_failed = 0; |
| 383 | qual->tx_success = 0; |
| 384 | qual->tx_failed = 0; |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | static int rt2x00lib_calculate_link_signal(struct rt2x00_dev *rt2x00dev, |
| 388 | int rssi) |
| 389 | { |
| 390 | int rssi_percentage = 0; |
| 391 | int signal; |
| 392 | |
| 393 | /* |
| 394 | * We need a positive value for the RSSI. |
| 395 | */ |
| 396 | if (rssi < 0) |
| 397 | rssi += rt2x00dev->rssi_offset; |
| 398 | |
| 399 | /* |
| 400 | * Calculate the different percentages, |
| 401 | * which will be used for the signal. |
| 402 | */ |
| 403 | if (rt2x00dev->rssi_offset) |
| 404 | rssi_percentage = (rssi * 100) / rt2x00dev->rssi_offset; |
| 405 | |
| 406 | /* |
| 407 | * Add the individual percentages and use the WEIGHT |
| 408 | * defines to calculate the current link signal. |
| 409 | */ |
| 410 | signal = ((WEIGHT_RSSI * rssi_percentage) + |
Ivo van Doorn | ebcf26d | 2007-10-13 16:26:12 +0200 | [diff] [blame] | 411 | (WEIGHT_TX * rt2x00dev->link.qual.tx_percentage) + |
| 412 | (WEIGHT_RX * rt2x00dev->link.qual.rx_percentage)) / 100; |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 413 | |
| 414 | return (signal > 100) ? 100 : signal; |
| 415 | } |
| 416 | |
| 417 | static void rt2x00lib_link_tuner(struct work_struct *work) |
| 418 | { |
| 419 | struct rt2x00_dev *rt2x00dev = |
| 420 | container_of(work, struct rt2x00_dev, link.work.work); |
| 421 | |
| 422 | /* |
Ivo van Doorn | 25ab002 | 2007-09-25 20:57:04 +0200 | [diff] [blame] | 423 | * When the radio is shutting down we should |
| 424 | * immediately cease all link tuning. |
| 425 | */ |
| 426 | if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags)) |
| 427 | return; |
| 428 | |
| 429 | /* |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 430 | * Update statistics. |
| 431 | */ |
Ivo van Doorn | ebcf26d | 2007-10-13 16:26:12 +0200 | [diff] [blame] | 432 | rt2x00dev->ops->lib->link_stats(rt2x00dev, &rt2x00dev->link.qual); |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 433 | rt2x00dev->low_level_stats.dot11FCSErrorCount += |
Ivo van Doorn | ebcf26d | 2007-10-13 16:26:12 +0200 | [diff] [blame] | 434 | rt2x00dev->link.qual.rx_failed; |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 435 | |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 436 | /* |
| 437 | * Only perform the link tuning when Link tuning |
| 438 | * has been enabled (This could have been disabled from the EEPROM). |
| 439 | */ |
| 440 | if (!test_bit(CONFIG_DISABLE_LINK_TUNING, &rt2x00dev->flags)) |
| 441 | rt2x00dev->ops->lib->link_tuner(rt2x00dev); |
| 442 | |
| 443 | /* |
Ivo van Doorn | 69f81a2 | 2007-10-13 16:26:36 +0200 | [diff] [blame] | 444 | * Evaluate antenna setup. |
| 445 | */ |
| 446 | rt2x00lib_evaluate_antenna(rt2x00dev); |
| 447 | |
| 448 | /* |
Ivo van Doorn | 725d99d | 2007-09-25 20:53:20 +0200 | [diff] [blame] | 449 | * Precalculate a portion of the link signal which is |
| 450 | * in based on the tx/rx success/failure counters. |
| 451 | */ |
Ivo van Doorn | ebcf26d | 2007-10-13 16:26:12 +0200 | [diff] [blame] | 452 | rt2x00lib_precalculate_link_signal(&rt2x00dev->link.qual); |
Ivo van Doorn | 725d99d | 2007-09-25 20:53:20 +0200 | [diff] [blame] | 453 | |
| 454 | /* |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 455 | * Increase tuner counter, and reschedule the next link tuner run. |
| 456 | */ |
| 457 | rt2x00dev->link.count++; |
| 458 | queue_delayed_work(rt2x00dev->hw->workqueue, &rt2x00dev->link.work, |
| 459 | LINK_TUNE_INTERVAL); |
| 460 | } |
| 461 | |
Johannes Berg | 4150c57 | 2007-09-17 01:29:23 -0400 | [diff] [blame] | 462 | static void rt2x00lib_packetfilter_scheduled(struct work_struct *work) |
| 463 | { |
| 464 | struct rt2x00_dev *rt2x00dev = |
| 465 | container_of(work, struct rt2x00_dev, filter_work); |
Ivo van Doorn | 3c4f208 | 2008-01-06 23:40:49 +0100 | [diff] [blame] | 466 | unsigned int filter = rt2x00dev->packet_filter; |
Ivo van Doorn | 5886d0d | 2007-10-06 14:13:38 +0200 | [diff] [blame] | 467 | |
| 468 | /* |
| 469 | * Since we had stored the filter inside interface.filter, |
| 470 | * we should now clear that field. Otherwise the driver will |
| 471 | * assume nothing has changed (*total_flags will be compared |
| 472 | * to interface.filter to determine if any action is required). |
| 473 | */ |
Ivo van Doorn | 3c4f208 | 2008-01-06 23:40:49 +0100 | [diff] [blame] | 474 | rt2x00dev->packet_filter = 0; |
Johannes Berg | 4150c57 | 2007-09-17 01:29:23 -0400 | [diff] [blame] | 475 | |
| 476 | rt2x00dev->ops->hw->configure_filter(rt2x00dev->hw, |
Ivo van Doorn | 5886d0d | 2007-10-06 14:13:38 +0200 | [diff] [blame] | 477 | filter, &filter, 0, NULL); |
Johannes Berg | 4150c57 | 2007-09-17 01:29:23 -0400 | [diff] [blame] | 478 | } |
| 479 | |
Ivo van Doorn | 5c58ee5 | 2007-10-06 13:34:52 +0200 | [diff] [blame] | 480 | static void rt2x00lib_configuration_scheduled(struct work_struct *work) |
| 481 | { |
| 482 | struct rt2x00_dev *rt2x00dev = |
| 483 | container_of(work, struct rt2x00_dev, config_work); |
Johannes Berg | 471b3ef | 2007-12-28 14:32:58 +0100 | [diff] [blame^] | 484 | struct ieee80211_bss_conf bss_conf; |
Ivo van Doorn | 5c58ee5 | 2007-10-06 13:34:52 +0200 | [diff] [blame] | 485 | |
Johannes Berg | 471b3ef | 2007-12-28 14:32:58 +0100 | [diff] [blame^] | 486 | bss_conf.use_short_preamble = |
| 487 | test_bit(CONFIG_SHORT_PREAMBLE, &rt2x00dev->flags); |
| 488 | |
| 489 | /* |
| 490 | * FIXME: shouldn't invoke it this way because all other contents |
| 491 | * of bss_conf is invalid. |
| 492 | */ |
| 493 | rt2x00mac_bss_info_changed(rt2x00dev->hw, rt2x00dev->interface.id, |
| 494 | &bss_conf, BSS_CHANGED_ERP_PREAMBLE); |
Ivo van Doorn | 5c58ee5 | 2007-10-06 13:34:52 +0200 | [diff] [blame] | 495 | } |
| 496 | |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 497 | /* |
| 498 | * Interrupt context handlers. |
| 499 | */ |
| 500 | static void rt2x00lib_beacondone_scheduled(struct work_struct *work) |
| 501 | { |
| 502 | struct rt2x00_dev *rt2x00dev = |
| 503 | container_of(work, struct rt2x00_dev, beacon_work); |
| 504 | struct data_ring *ring = |
| 505 | rt2x00lib_get_ring(rt2x00dev, IEEE80211_TX_QUEUE_BEACON); |
| 506 | struct data_entry *entry = rt2x00_get_data_entry(ring); |
| 507 | struct sk_buff *skb; |
| 508 | |
| 509 | skb = ieee80211_beacon_get(rt2x00dev->hw, |
| 510 | rt2x00dev->interface.id, |
| 511 | &entry->tx_status.control); |
| 512 | if (!skb) |
| 513 | return; |
| 514 | |
| 515 | rt2x00dev->ops->hw->beacon_update(rt2x00dev->hw, skb, |
| 516 | &entry->tx_status.control); |
| 517 | |
| 518 | dev_kfree_skb(skb); |
| 519 | } |
| 520 | |
| 521 | void rt2x00lib_beacondone(struct rt2x00_dev *rt2x00dev) |
| 522 | { |
| 523 | if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags)) |
| 524 | return; |
| 525 | |
| 526 | queue_work(rt2x00dev->hw->workqueue, &rt2x00dev->beacon_work); |
| 527 | } |
| 528 | EXPORT_SYMBOL_GPL(rt2x00lib_beacondone); |
| 529 | |
| 530 | void rt2x00lib_txdone(struct data_entry *entry, |
| 531 | const int status, const int retry) |
| 532 | { |
| 533 | struct rt2x00_dev *rt2x00dev = entry->ring->rt2x00dev; |
| 534 | struct ieee80211_tx_status *tx_status = &entry->tx_status; |
| 535 | struct ieee80211_low_level_stats *stats = &rt2x00dev->low_level_stats; |
| 536 | int success = !!(status == TX_SUCCESS || status == TX_SUCCESS_RETRY); |
| 537 | int fail = !!(status == TX_FAIL_RETRY || status == TX_FAIL_INVALID || |
| 538 | status == TX_FAIL_OTHER); |
| 539 | |
| 540 | /* |
| 541 | * Update TX statistics. |
| 542 | */ |
| 543 | tx_status->flags = 0; |
| 544 | tx_status->ack_signal = 0; |
| 545 | tx_status->excessive_retries = (status == TX_FAIL_RETRY); |
| 546 | tx_status->retry_count = retry; |
Ivo van Doorn | ebcf26d | 2007-10-13 16:26:12 +0200 | [diff] [blame] | 547 | rt2x00dev->link.qual.tx_success += success; |
| 548 | rt2x00dev->link.qual.tx_failed += retry + fail; |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 549 | |
| 550 | if (!(tx_status->control.flags & IEEE80211_TXCTL_NO_ACK)) { |
| 551 | if (success) |
| 552 | tx_status->flags |= IEEE80211_TX_STATUS_ACK; |
| 553 | else |
| 554 | stats->dot11ACKFailureCount++; |
| 555 | } |
| 556 | |
| 557 | tx_status->queue_length = entry->ring->stats.limit; |
| 558 | tx_status->queue_number = tx_status->control.queue; |
| 559 | |
| 560 | if (tx_status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS) { |
| 561 | if (success) |
| 562 | stats->dot11RTSSuccessCount++; |
| 563 | else |
| 564 | stats->dot11RTSFailureCount++; |
| 565 | } |
| 566 | |
| 567 | /* |
Ivo van Doorn | 4d8dd66 | 2007-11-27 21:49:29 +0100 | [diff] [blame] | 568 | * Send the tx_status to mac80211 & debugfs. |
| 569 | * mac80211 will clean up the skb structure. |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 570 | */ |
Ivo van Doorn | 4d8dd66 | 2007-11-27 21:49:29 +0100 | [diff] [blame] | 571 | get_skb_desc(entry->skb)->frame_type = DUMP_FRAME_TXDONE; |
| 572 | rt2x00debug_dump_frame(rt2x00dev, entry->skb); |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 573 | ieee80211_tx_status_irqsafe(rt2x00dev->hw, entry->skb, tx_status); |
| 574 | entry->skb = NULL; |
| 575 | } |
| 576 | EXPORT_SYMBOL_GPL(rt2x00lib_txdone); |
| 577 | |
| 578 | void rt2x00lib_rxdone(struct data_entry *entry, struct sk_buff *skb, |
Johannes Berg | 4150c57 | 2007-09-17 01:29:23 -0400 | [diff] [blame] | 579 | struct rxdata_entry_desc *desc) |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 580 | { |
| 581 | struct rt2x00_dev *rt2x00dev = entry->ring->rt2x00dev; |
| 582 | struct ieee80211_rx_status *rx_status = &rt2x00dev->rx_status; |
| 583 | struct ieee80211_hw_mode *mode; |
| 584 | struct ieee80211_rate *rate; |
Mattias Nissler | 61af43c | 2007-11-27 21:50:26 +0100 | [diff] [blame] | 585 | struct ieee80211_hdr *hdr; |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 586 | unsigned int i; |
| 587 | int val = 0; |
Mattias Nissler | 61af43c | 2007-11-27 21:50:26 +0100 | [diff] [blame] | 588 | u16 fc; |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 589 | |
| 590 | /* |
| 591 | * Update RX statistics. |
| 592 | */ |
| 593 | mode = &rt2x00dev->hwmodes[rt2x00dev->curr_hwmode]; |
| 594 | for (i = 0; i < mode->num_rates; i++) { |
| 595 | rate = &mode->rates[i]; |
| 596 | |
| 597 | /* |
| 598 | * When frame was received with an OFDM bitrate, |
| 599 | * the signal is the PLCP value. If it was received with |
| 600 | * a CCK bitrate the signal is the rate in 0.5kbit/s. |
| 601 | */ |
Johannes Berg | 4150c57 | 2007-09-17 01:29:23 -0400 | [diff] [blame] | 602 | if (!desc->ofdm) |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 603 | val = DEVICE_GET_RATE_FIELD(rate->val, RATE); |
| 604 | else |
| 605 | val = DEVICE_GET_RATE_FIELD(rate->val, PLCP); |
| 606 | |
Johannes Berg | 4150c57 | 2007-09-17 01:29:23 -0400 | [diff] [blame] | 607 | if (val == desc->signal) { |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 608 | val = rate->val; |
| 609 | break; |
| 610 | } |
| 611 | } |
| 612 | |
Mattias Nissler | 61af43c | 2007-11-27 21:50:26 +0100 | [diff] [blame] | 613 | /* |
Ivo van Doorn | 7e56d38 | 2008-01-06 23:41:28 +0100 | [diff] [blame] | 614 | * Only update link status if this is a beacon frame carrying our bssid. |
Mattias Nissler | 61af43c | 2007-11-27 21:50:26 +0100 | [diff] [blame] | 615 | */ |
Ivo van Doorn | 7e56d38 | 2008-01-06 23:41:28 +0100 | [diff] [blame] | 616 | hdr = (struct ieee80211_hdr*)skb->data; |
| 617 | fc = le16_to_cpu(hdr->frame_control); |
| 618 | if (is_beacon(fc) && desc->my_bss) |
| 619 | rt2x00lib_update_link_stats(&rt2x00dev->link, desc->rssi); |
Mattias Nissler | 61af43c | 2007-11-27 21:50:26 +0100 | [diff] [blame] | 620 | |
Ivo van Doorn | ebcf26d | 2007-10-13 16:26:12 +0200 | [diff] [blame] | 621 | rt2x00dev->link.qual.rx_success++; |
Ivo van Doorn | 69f81a2 | 2007-10-13 16:26:36 +0200 | [diff] [blame] | 622 | |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 623 | rx_status->rate = val; |
Johannes Berg | 4150c57 | 2007-09-17 01:29:23 -0400 | [diff] [blame] | 624 | rx_status->signal = |
| 625 | rt2x00lib_calculate_link_signal(rt2x00dev, desc->rssi); |
| 626 | rx_status->ssi = desc->rssi; |
| 627 | rx_status->flag = desc->flags; |
Ivo van Doorn | 69f81a2 | 2007-10-13 16:26:36 +0200 | [diff] [blame] | 628 | rx_status->antenna = rt2x00dev->link.ant.active.rx; |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 629 | |
| 630 | /* |
Ivo van Doorn | 4d8dd66 | 2007-11-27 21:49:29 +0100 | [diff] [blame] | 631 | * Send frame to mac80211 & debugfs |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 632 | */ |
Ivo van Doorn | 4d8dd66 | 2007-11-27 21:49:29 +0100 | [diff] [blame] | 633 | get_skb_desc(skb)->frame_type = DUMP_FRAME_RXDONE; |
| 634 | rt2x00debug_dump_frame(rt2x00dev, skb); |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 635 | ieee80211_rx_irqsafe(rt2x00dev->hw, skb, rx_status); |
| 636 | } |
| 637 | EXPORT_SYMBOL_GPL(rt2x00lib_rxdone); |
| 638 | |
| 639 | /* |
| 640 | * TX descriptor initializer |
| 641 | */ |
| 642 | void rt2x00lib_write_tx_desc(struct rt2x00_dev *rt2x00dev, |
Ivo van Doorn | 08992f7 | 2008-01-24 01:56:25 -0800 | [diff] [blame] | 643 | struct sk_buff *skb, |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 644 | struct ieee80211_tx_control *control) |
| 645 | { |
Johannes Berg | 4150c57 | 2007-09-17 01:29:23 -0400 | [diff] [blame] | 646 | struct txdata_entry_desc desc; |
Ivo van Doorn | 08992f7 | 2008-01-24 01:56:25 -0800 | [diff] [blame] | 647 | struct skb_desc *skbdesc = get_skb_desc(skb); |
| 648 | struct ieee80211_hdr *ieee80211hdr = skbdesc->data; |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 649 | int tx_rate; |
| 650 | int bitrate; |
Ivo van Doorn | 08992f7 | 2008-01-24 01:56:25 -0800 | [diff] [blame] | 651 | int length; |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 652 | int duration; |
| 653 | int residual; |
| 654 | u16 frame_control; |
| 655 | u16 seq_ctrl; |
| 656 | |
Ivo van Doorn | 08992f7 | 2008-01-24 01:56:25 -0800 | [diff] [blame] | 657 | memset(&desc, 0, sizeof(desc)); |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 658 | |
Ivo van Doorn | 08992f7 | 2008-01-24 01:56:25 -0800 | [diff] [blame] | 659 | desc.cw_min = skbdesc->ring->tx_params.cw_min; |
| 660 | desc.cw_max = skbdesc->ring->tx_params.cw_max; |
| 661 | desc.aifs = skbdesc->ring->tx_params.aifs; |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 662 | |
| 663 | /* |
| 664 | * Identify queue |
| 665 | */ |
| 666 | if (control->queue < rt2x00dev->hw->queues) |
| 667 | desc.queue = control->queue; |
| 668 | else if (control->queue == IEEE80211_TX_QUEUE_BEACON || |
| 669 | control->queue == IEEE80211_TX_QUEUE_AFTER_BEACON) |
| 670 | desc.queue = QUEUE_MGMT; |
| 671 | else |
| 672 | desc.queue = QUEUE_OTHER; |
| 673 | |
| 674 | /* |
| 675 | * Read required fields from ieee80211 header. |
| 676 | */ |
| 677 | frame_control = le16_to_cpu(ieee80211hdr->frame_control); |
| 678 | seq_ctrl = le16_to_cpu(ieee80211hdr->seq_ctrl); |
| 679 | |
| 680 | tx_rate = control->tx_rate; |
| 681 | |
| 682 | /* |
Mattias Nissler | 2700f8b | 2007-10-27 13:43:49 +0200 | [diff] [blame] | 683 | * Check whether this frame is to be acked |
| 684 | */ |
| 685 | if (!(control->flags & IEEE80211_TXCTL_NO_ACK)) |
| 686 | __set_bit(ENTRY_TXD_ACK, &desc.flags); |
| 687 | |
| 688 | /* |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 689 | * Check if this is a RTS/CTS frame |
| 690 | */ |
| 691 | if (is_rts_frame(frame_control) || is_cts_frame(frame_control)) { |
| 692 | __set_bit(ENTRY_TXD_BURST, &desc.flags); |
Mattias Nissler | 2700f8b | 2007-10-27 13:43:49 +0200 | [diff] [blame] | 693 | if (is_rts_frame(frame_control)) { |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 694 | __set_bit(ENTRY_TXD_RTS_FRAME, &desc.flags); |
Mattias Nissler | 2700f8b | 2007-10-27 13:43:49 +0200 | [diff] [blame] | 695 | __set_bit(ENTRY_TXD_ACK, &desc.flags); |
| 696 | } else |
| 697 | __clear_bit(ENTRY_TXD_ACK, &desc.flags); |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 698 | if (control->rts_cts_rate) |
| 699 | tx_rate = control->rts_cts_rate; |
| 700 | } |
| 701 | |
| 702 | /* |
| 703 | * Check for OFDM |
| 704 | */ |
| 705 | if (DEVICE_GET_RATE_FIELD(tx_rate, RATEMASK) & DEV_OFDM_RATEMASK) |
| 706 | __set_bit(ENTRY_TXD_OFDM_RATE, &desc.flags); |
| 707 | |
| 708 | /* |
| 709 | * Check if more fragments are pending |
| 710 | */ |
| 711 | if (ieee80211_get_morefrag(ieee80211hdr)) { |
| 712 | __set_bit(ENTRY_TXD_BURST, &desc.flags); |
| 713 | __set_bit(ENTRY_TXD_MORE_FRAG, &desc.flags); |
| 714 | } |
| 715 | |
| 716 | /* |
| 717 | * Beacons and probe responses require the tsf timestamp |
| 718 | * to be inserted into the frame. |
| 719 | */ |
| 720 | if (control->queue == IEEE80211_TX_QUEUE_BEACON || |
| 721 | is_probe_resp(frame_control)) |
| 722 | __set_bit(ENTRY_TXD_REQ_TIMESTAMP, &desc.flags); |
| 723 | |
| 724 | /* |
| 725 | * Determine with what IFS priority this frame should be send. |
| 726 | * Set ifs to IFS_SIFS when the this is not the first fragment, |
| 727 | * or this fragment came after RTS/CTS. |
| 728 | */ |
| 729 | if ((seq_ctrl & IEEE80211_SCTL_FRAG) > 0 || |
| 730 | test_bit(ENTRY_TXD_RTS_FRAME, &desc.flags)) |
| 731 | desc.ifs = IFS_SIFS; |
| 732 | else |
| 733 | desc.ifs = IFS_BACKOFF; |
| 734 | |
| 735 | /* |
| 736 | * PLCP setup |
| 737 | * Length calculation depends on OFDM/CCK rate. |
| 738 | */ |
| 739 | desc.signal = DEVICE_GET_RATE_FIELD(tx_rate, PLCP); |
| 740 | desc.service = 0x04; |
| 741 | |
Ivo van Doorn | 08992f7 | 2008-01-24 01:56:25 -0800 | [diff] [blame] | 742 | length = skbdesc->data_len + FCS_LEN; |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 743 | if (test_bit(ENTRY_TXD_OFDM_RATE, &desc.flags)) { |
Ivo van Doorn | 08992f7 | 2008-01-24 01:56:25 -0800 | [diff] [blame] | 744 | desc.length_high = (length >> 6) & 0x3f; |
| 745 | desc.length_low = length & 0x3f; |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 746 | } else { |
| 747 | bitrate = DEVICE_GET_RATE_FIELD(tx_rate, RATE); |
| 748 | |
| 749 | /* |
| 750 | * Convert length to microseconds. |
| 751 | */ |
Ivo van Doorn | 08992f7 | 2008-01-24 01:56:25 -0800 | [diff] [blame] | 752 | residual = get_duration_res(length, bitrate); |
| 753 | duration = get_duration(length, bitrate); |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 754 | |
| 755 | if (residual != 0) { |
| 756 | duration++; |
| 757 | |
| 758 | /* |
| 759 | * Check if we need to set the Length Extension |
| 760 | */ |
Mattias Nissler | db15178 | 2007-10-13 16:26:52 +0200 | [diff] [blame] | 761 | if (bitrate == 110 && residual <= 30) |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 762 | desc.service |= 0x80; |
| 763 | } |
| 764 | |
| 765 | desc.length_high = (duration >> 8) & 0xff; |
| 766 | desc.length_low = duration & 0xff; |
| 767 | |
| 768 | /* |
| 769 | * When preamble is enabled we should set the |
| 770 | * preamble bit for the signal. |
| 771 | */ |
| 772 | if (DEVICE_GET_RATE_FIELD(tx_rate, PREAMBLE)) |
| 773 | desc.signal |= 0x08; |
| 774 | } |
| 775 | |
Ivo van Doorn | dd3193e | 2008-01-06 23:41:10 +0100 | [diff] [blame] | 776 | rt2x00dev->ops->lib->write_tx_desc(rt2x00dev, skb, &desc, control); |
Ivo van Doorn | 08992f7 | 2008-01-24 01:56:25 -0800 | [diff] [blame] | 777 | |
| 778 | /* |
| 779 | * Update ring entry. |
| 780 | */ |
| 781 | skbdesc->entry->skb = skb; |
| 782 | memcpy(&skbdesc->entry->tx_status.control, control, sizeof(*control)); |
Ivo van Doorn | 4d8dd66 | 2007-11-27 21:49:29 +0100 | [diff] [blame] | 783 | |
| 784 | /* |
| 785 | * The frame has been completely initialized and ready |
| 786 | * for sending to the device. The caller will push the |
| 787 | * frame to the device, but we are going to push the |
| 788 | * frame to debugfs here. |
| 789 | */ |
| 790 | skbdesc->frame_type = DUMP_FRAME_TX; |
| 791 | rt2x00debug_dump_frame(rt2x00dev, skb); |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 792 | } |
| 793 | EXPORT_SYMBOL_GPL(rt2x00lib_write_tx_desc); |
| 794 | |
| 795 | /* |
| 796 | * Driver initialization handlers. |
| 797 | */ |
| 798 | static void rt2x00lib_channel(struct ieee80211_channel *entry, |
| 799 | const int channel, const int tx_power, |
| 800 | const int value) |
| 801 | { |
| 802 | entry->chan = channel; |
| 803 | if (channel <= 14) |
| 804 | entry->freq = 2407 + (5 * channel); |
| 805 | else |
| 806 | entry->freq = 5000 + (5 * channel); |
| 807 | entry->val = value; |
| 808 | entry->flag = |
| 809 | IEEE80211_CHAN_W_IBSS | |
| 810 | IEEE80211_CHAN_W_ACTIVE_SCAN | |
| 811 | IEEE80211_CHAN_W_SCAN; |
| 812 | entry->power_level = tx_power; |
| 813 | entry->antenna_max = 0xff; |
| 814 | } |
| 815 | |
| 816 | static void rt2x00lib_rate(struct ieee80211_rate *entry, |
| 817 | const int rate, const int mask, |
| 818 | const int plcp, const int flags) |
| 819 | { |
| 820 | entry->rate = rate; |
| 821 | entry->val = |
| 822 | DEVICE_SET_RATE_FIELD(rate, RATE) | |
| 823 | DEVICE_SET_RATE_FIELD(mask, RATEMASK) | |
| 824 | DEVICE_SET_RATE_FIELD(plcp, PLCP); |
| 825 | entry->flags = flags; |
| 826 | entry->val2 = entry->val; |
| 827 | if (entry->flags & IEEE80211_RATE_PREAMBLE2) |
| 828 | entry->val2 |= DEVICE_SET_RATE_FIELD(1, PREAMBLE); |
| 829 | entry->min_rssi_ack = 0; |
| 830 | entry->min_rssi_ack_delta = 0; |
| 831 | } |
| 832 | |
| 833 | static int rt2x00lib_probe_hw_modes(struct rt2x00_dev *rt2x00dev, |
| 834 | struct hw_mode_spec *spec) |
| 835 | { |
| 836 | struct ieee80211_hw *hw = rt2x00dev->hw; |
| 837 | struct ieee80211_hw_mode *hwmodes; |
| 838 | struct ieee80211_channel *channels; |
| 839 | struct ieee80211_rate *rates; |
| 840 | unsigned int i; |
| 841 | unsigned char tx_power; |
| 842 | |
| 843 | hwmodes = kzalloc(sizeof(*hwmodes) * spec->num_modes, GFP_KERNEL); |
| 844 | if (!hwmodes) |
| 845 | goto exit; |
| 846 | |
| 847 | channels = kzalloc(sizeof(*channels) * spec->num_channels, GFP_KERNEL); |
| 848 | if (!channels) |
| 849 | goto exit_free_modes; |
| 850 | |
| 851 | rates = kzalloc(sizeof(*rates) * spec->num_rates, GFP_KERNEL); |
| 852 | if (!rates) |
| 853 | goto exit_free_channels; |
| 854 | |
| 855 | /* |
| 856 | * Initialize Rate list. |
| 857 | */ |
| 858 | rt2x00lib_rate(&rates[0], 10, DEV_RATEMASK_1MB, |
| 859 | 0x00, IEEE80211_RATE_CCK); |
| 860 | rt2x00lib_rate(&rates[1], 20, DEV_RATEMASK_2MB, |
| 861 | 0x01, IEEE80211_RATE_CCK_2); |
| 862 | rt2x00lib_rate(&rates[2], 55, DEV_RATEMASK_5_5MB, |
| 863 | 0x02, IEEE80211_RATE_CCK_2); |
| 864 | rt2x00lib_rate(&rates[3], 110, DEV_RATEMASK_11MB, |
| 865 | 0x03, IEEE80211_RATE_CCK_2); |
| 866 | |
| 867 | if (spec->num_rates > 4) { |
| 868 | rt2x00lib_rate(&rates[4], 60, DEV_RATEMASK_6MB, |
| 869 | 0x0b, IEEE80211_RATE_OFDM); |
| 870 | rt2x00lib_rate(&rates[5], 90, DEV_RATEMASK_9MB, |
| 871 | 0x0f, IEEE80211_RATE_OFDM); |
| 872 | rt2x00lib_rate(&rates[6], 120, DEV_RATEMASK_12MB, |
| 873 | 0x0a, IEEE80211_RATE_OFDM); |
| 874 | rt2x00lib_rate(&rates[7], 180, DEV_RATEMASK_18MB, |
| 875 | 0x0e, IEEE80211_RATE_OFDM); |
| 876 | rt2x00lib_rate(&rates[8], 240, DEV_RATEMASK_24MB, |
| 877 | 0x09, IEEE80211_RATE_OFDM); |
| 878 | rt2x00lib_rate(&rates[9], 360, DEV_RATEMASK_36MB, |
| 879 | 0x0d, IEEE80211_RATE_OFDM); |
| 880 | rt2x00lib_rate(&rates[10], 480, DEV_RATEMASK_48MB, |
| 881 | 0x08, IEEE80211_RATE_OFDM); |
| 882 | rt2x00lib_rate(&rates[11], 540, DEV_RATEMASK_54MB, |
| 883 | 0x0c, IEEE80211_RATE_OFDM); |
| 884 | } |
| 885 | |
| 886 | /* |
| 887 | * Initialize Channel list. |
| 888 | */ |
| 889 | for (i = 0; i < spec->num_channels; i++) { |
| 890 | if (spec->channels[i].channel <= 14) |
| 891 | tx_power = spec->tx_power_bg[i]; |
| 892 | else if (spec->tx_power_a) |
| 893 | tx_power = spec->tx_power_a[i]; |
| 894 | else |
| 895 | tx_power = spec->tx_power_default; |
| 896 | |
| 897 | rt2x00lib_channel(&channels[i], |
| 898 | spec->channels[i].channel, tx_power, i); |
| 899 | } |
| 900 | |
| 901 | /* |
| 902 | * Intitialize 802.11b |
| 903 | * Rates: CCK. |
| 904 | * Channels: OFDM. |
| 905 | */ |
| 906 | if (spec->num_modes > HWMODE_B) { |
| 907 | hwmodes[HWMODE_B].mode = MODE_IEEE80211B; |
| 908 | hwmodes[HWMODE_B].num_channels = 14; |
| 909 | hwmodes[HWMODE_B].num_rates = 4; |
| 910 | hwmodes[HWMODE_B].channels = channels; |
| 911 | hwmodes[HWMODE_B].rates = rates; |
| 912 | } |
| 913 | |
| 914 | /* |
| 915 | * Intitialize 802.11g |
| 916 | * Rates: CCK, OFDM. |
| 917 | * Channels: OFDM. |
| 918 | */ |
| 919 | if (spec->num_modes > HWMODE_G) { |
| 920 | hwmodes[HWMODE_G].mode = MODE_IEEE80211G; |
| 921 | hwmodes[HWMODE_G].num_channels = 14; |
| 922 | hwmodes[HWMODE_G].num_rates = spec->num_rates; |
| 923 | hwmodes[HWMODE_G].channels = channels; |
| 924 | hwmodes[HWMODE_G].rates = rates; |
| 925 | } |
| 926 | |
| 927 | /* |
| 928 | * Intitialize 802.11a |
| 929 | * Rates: OFDM. |
| 930 | * Channels: OFDM, UNII, HiperLAN2. |
| 931 | */ |
| 932 | if (spec->num_modes > HWMODE_A) { |
| 933 | hwmodes[HWMODE_A].mode = MODE_IEEE80211A; |
| 934 | hwmodes[HWMODE_A].num_channels = spec->num_channels - 14; |
| 935 | hwmodes[HWMODE_A].num_rates = spec->num_rates - 4; |
| 936 | hwmodes[HWMODE_A].channels = &channels[14]; |
| 937 | hwmodes[HWMODE_A].rates = &rates[4]; |
| 938 | } |
| 939 | |
| 940 | if (spec->num_modes > HWMODE_G && |
| 941 | ieee80211_register_hwmode(hw, &hwmodes[HWMODE_G])) |
| 942 | goto exit_free_rates; |
| 943 | |
| 944 | if (spec->num_modes > HWMODE_B && |
| 945 | ieee80211_register_hwmode(hw, &hwmodes[HWMODE_B])) |
| 946 | goto exit_free_rates; |
| 947 | |
| 948 | if (spec->num_modes > HWMODE_A && |
| 949 | ieee80211_register_hwmode(hw, &hwmodes[HWMODE_A])) |
| 950 | goto exit_free_rates; |
| 951 | |
| 952 | rt2x00dev->hwmodes = hwmodes; |
| 953 | |
| 954 | return 0; |
| 955 | |
| 956 | exit_free_rates: |
| 957 | kfree(rates); |
| 958 | |
| 959 | exit_free_channels: |
| 960 | kfree(channels); |
| 961 | |
| 962 | exit_free_modes: |
| 963 | kfree(hwmodes); |
| 964 | |
| 965 | exit: |
| 966 | ERROR(rt2x00dev, "Allocation ieee80211 modes failed.\n"); |
| 967 | return -ENOMEM; |
| 968 | } |
| 969 | |
| 970 | static void rt2x00lib_remove_hw(struct rt2x00_dev *rt2x00dev) |
| 971 | { |
Ivo van Doorn | 066cb63 | 2007-09-25 20:55:39 +0200 | [diff] [blame] | 972 | if (test_bit(DEVICE_REGISTERED_HW, &rt2x00dev->flags)) |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 973 | ieee80211_unregister_hw(rt2x00dev->hw); |
| 974 | |
| 975 | if (likely(rt2x00dev->hwmodes)) { |
| 976 | kfree(rt2x00dev->hwmodes->channels); |
| 977 | kfree(rt2x00dev->hwmodes->rates); |
| 978 | kfree(rt2x00dev->hwmodes); |
| 979 | rt2x00dev->hwmodes = NULL; |
| 980 | } |
| 981 | } |
| 982 | |
| 983 | static int rt2x00lib_probe_hw(struct rt2x00_dev *rt2x00dev) |
| 984 | { |
| 985 | struct hw_mode_spec *spec = &rt2x00dev->spec; |
| 986 | int status; |
| 987 | |
| 988 | /* |
| 989 | * Initialize HW modes. |
| 990 | */ |
| 991 | status = rt2x00lib_probe_hw_modes(rt2x00dev, spec); |
| 992 | if (status) |
| 993 | return status; |
| 994 | |
| 995 | /* |
| 996 | * Register HW. |
| 997 | */ |
| 998 | status = ieee80211_register_hw(rt2x00dev->hw); |
| 999 | if (status) { |
| 1000 | rt2x00lib_remove_hw(rt2x00dev); |
| 1001 | return status; |
| 1002 | } |
| 1003 | |
Ivo van Doorn | 066cb63 | 2007-09-25 20:55:39 +0200 | [diff] [blame] | 1004 | __set_bit(DEVICE_REGISTERED_HW, &rt2x00dev->flags); |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 1005 | |
| 1006 | return 0; |
| 1007 | } |
| 1008 | |
| 1009 | /* |
| 1010 | * Initialization/uninitialization handlers. |
| 1011 | */ |
| 1012 | static int rt2x00lib_alloc_entries(struct data_ring *ring, |
| 1013 | const u16 max_entries, const u16 data_size, |
| 1014 | const u16 desc_size) |
| 1015 | { |
| 1016 | struct data_entry *entry; |
| 1017 | unsigned int i; |
| 1018 | |
| 1019 | ring->stats.limit = max_entries; |
| 1020 | ring->data_size = data_size; |
| 1021 | ring->desc_size = desc_size; |
| 1022 | |
| 1023 | /* |
| 1024 | * Allocate all ring entries. |
| 1025 | */ |
| 1026 | entry = kzalloc(ring->stats.limit * sizeof(*entry), GFP_KERNEL); |
| 1027 | if (!entry) |
| 1028 | return -ENOMEM; |
| 1029 | |
| 1030 | for (i = 0; i < ring->stats.limit; i++) { |
| 1031 | entry[i].flags = 0; |
| 1032 | entry[i].ring = ring; |
| 1033 | entry[i].skb = NULL; |
Ivo van Doorn | 0426710 | 2008-01-06 23:39:25 +0100 | [diff] [blame] | 1034 | entry[i].entry_idx = i; |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 1035 | } |
| 1036 | |
| 1037 | ring->entry = entry; |
| 1038 | |
| 1039 | return 0; |
| 1040 | } |
| 1041 | |
| 1042 | static int rt2x00lib_alloc_ring_entries(struct rt2x00_dev *rt2x00dev) |
| 1043 | { |
| 1044 | struct data_ring *ring; |
| 1045 | |
| 1046 | /* |
| 1047 | * Allocate the RX ring. |
| 1048 | */ |
| 1049 | if (rt2x00lib_alloc_entries(rt2x00dev->rx, RX_ENTRIES, DATA_FRAME_SIZE, |
| 1050 | rt2x00dev->ops->rxd_size)) |
| 1051 | return -ENOMEM; |
| 1052 | |
| 1053 | /* |
| 1054 | * First allocate the TX rings. |
| 1055 | */ |
| 1056 | txring_for_each(rt2x00dev, ring) { |
| 1057 | if (rt2x00lib_alloc_entries(ring, TX_ENTRIES, DATA_FRAME_SIZE, |
| 1058 | rt2x00dev->ops->txd_size)) |
| 1059 | return -ENOMEM; |
| 1060 | } |
| 1061 | |
Ivo van Doorn | 066cb63 | 2007-09-25 20:55:39 +0200 | [diff] [blame] | 1062 | if (!test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags)) |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 1063 | return 0; |
| 1064 | |
| 1065 | /* |
| 1066 | * Allocate the BEACON ring. |
| 1067 | */ |
| 1068 | if (rt2x00lib_alloc_entries(&rt2x00dev->bcn[0], BEACON_ENTRIES, |
| 1069 | MGMT_FRAME_SIZE, rt2x00dev->ops->txd_size)) |
| 1070 | return -ENOMEM; |
| 1071 | |
| 1072 | /* |
| 1073 | * Allocate the Atim ring. |
| 1074 | */ |
| 1075 | if (rt2x00lib_alloc_entries(&rt2x00dev->bcn[1], ATIM_ENTRIES, |
| 1076 | DATA_FRAME_SIZE, rt2x00dev->ops->txd_size)) |
| 1077 | return -ENOMEM; |
| 1078 | |
| 1079 | return 0; |
| 1080 | } |
| 1081 | |
| 1082 | static void rt2x00lib_free_ring_entries(struct rt2x00_dev *rt2x00dev) |
| 1083 | { |
| 1084 | struct data_ring *ring; |
| 1085 | |
| 1086 | ring_for_each(rt2x00dev, ring) { |
| 1087 | kfree(ring->entry); |
| 1088 | ring->entry = NULL; |
| 1089 | } |
| 1090 | } |
| 1091 | |
Ivo van Doorn | e37ea21 | 2008-01-06 23:40:07 +0100 | [diff] [blame] | 1092 | static void rt2x00lib_uninitialize(struct rt2x00_dev *rt2x00dev) |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 1093 | { |
| 1094 | if (!__test_and_clear_bit(DEVICE_INITIALIZED, &rt2x00dev->flags)) |
| 1095 | return; |
| 1096 | |
| 1097 | /* |
| 1098 | * Unregister rfkill. |
| 1099 | */ |
| 1100 | rt2x00rfkill_unregister(rt2x00dev); |
| 1101 | |
| 1102 | /* |
| 1103 | * Allow the HW to uninitialize. |
| 1104 | */ |
| 1105 | rt2x00dev->ops->lib->uninitialize(rt2x00dev); |
| 1106 | |
| 1107 | /* |
| 1108 | * Free allocated ring entries. |
| 1109 | */ |
| 1110 | rt2x00lib_free_ring_entries(rt2x00dev); |
| 1111 | } |
| 1112 | |
Ivo van Doorn | e37ea21 | 2008-01-06 23:40:07 +0100 | [diff] [blame] | 1113 | static int rt2x00lib_initialize(struct rt2x00_dev *rt2x00dev) |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 1114 | { |
| 1115 | int status; |
| 1116 | |
| 1117 | if (test_bit(DEVICE_INITIALIZED, &rt2x00dev->flags)) |
| 1118 | return 0; |
| 1119 | |
| 1120 | /* |
| 1121 | * Allocate all ring entries. |
| 1122 | */ |
| 1123 | status = rt2x00lib_alloc_ring_entries(rt2x00dev); |
| 1124 | if (status) { |
| 1125 | ERROR(rt2x00dev, "Ring entries allocation failed.\n"); |
| 1126 | return status; |
| 1127 | } |
| 1128 | |
| 1129 | /* |
| 1130 | * Initialize the device. |
| 1131 | */ |
| 1132 | status = rt2x00dev->ops->lib->initialize(rt2x00dev); |
| 1133 | if (status) |
| 1134 | goto exit; |
| 1135 | |
| 1136 | __set_bit(DEVICE_INITIALIZED, &rt2x00dev->flags); |
| 1137 | |
| 1138 | /* |
| 1139 | * Register the rfkill handler. |
| 1140 | */ |
| 1141 | status = rt2x00rfkill_register(rt2x00dev); |
| 1142 | if (status) |
| 1143 | goto exit_unitialize; |
| 1144 | |
| 1145 | return 0; |
| 1146 | |
| 1147 | exit_unitialize: |
| 1148 | rt2x00lib_uninitialize(rt2x00dev); |
| 1149 | |
| 1150 | exit: |
| 1151 | rt2x00lib_free_ring_entries(rt2x00dev); |
| 1152 | |
| 1153 | return status; |
| 1154 | } |
| 1155 | |
Ivo van Doorn | e37ea21 | 2008-01-06 23:40:07 +0100 | [diff] [blame] | 1156 | int rt2x00lib_start(struct rt2x00_dev *rt2x00dev) |
| 1157 | { |
| 1158 | int retval; |
| 1159 | |
| 1160 | if (test_bit(DEVICE_STARTED, &rt2x00dev->flags)) |
| 1161 | return 0; |
| 1162 | |
| 1163 | /* |
| 1164 | * If this is the first interface which is added, |
| 1165 | * we should load the firmware now. |
| 1166 | */ |
| 1167 | if (test_bit(DRIVER_REQUIRE_FIRMWARE, &rt2x00dev->flags)) { |
| 1168 | retval = rt2x00lib_load_firmware(rt2x00dev); |
| 1169 | if (retval) |
| 1170 | return retval; |
| 1171 | } |
| 1172 | |
| 1173 | /* |
| 1174 | * Initialize the device. |
| 1175 | */ |
| 1176 | retval = rt2x00lib_initialize(rt2x00dev); |
| 1177 | if (retval) |
| 1178 | return retval; |
| 1179 | |
| 1180 | /* |
| 1181 | * Enable radio. |
| 1182 | */ |
| 1183 | retval = rt2x00lib_enable_radio(rt2x00dev); |
| 1184 | if (retval) { |
| 1185 | rt2x00lib_uninitialize(rt2x00dev); |
| 1186 | return retval; |
| 1187 | } |
| 1188 | |
| 1189 | __set_bit(DEVICE_STARTED, &rt2x00dev->flags); |
| 1190 | |
| 1191 | return 0; |
| 1192 | } |
| 1193 | |
| 1194 | void rt2x00lib_stop(struct rt2x00_dev *rt2x00dev) |
| 1195 | { |
| 1196 | if (!test_bit(DEVICE_STARTED, &rt2x00dev->flags)) |
| 1197 | return; |
| 1198 | |
| 1199 | /* |
| 1200 | * Perhaps we can add something smarter here, |
| 1201 | * but for now just disabling the radio should do. |
| 1202 | */ |
| 1203 | rt2x00lib_disable_radio(rt2x00dev); |
| 1204 | |
| 1205 | __clear_bit(DEVICE_STARTED, &rt2x00dev->flags); |
| 1206 | } |
| 1207 | |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 1208 | /* |
| 1209 | * driver allocation handlers. |
| 1210 | */ |
| 1211 | static int rt2x00lib_alloc_rings(struct rt2x00_dev *rt2x00dev) |
| 1212 | { |
| 1213 | struct data_ring *ring; |
Ivo van Doorn | 0426710 | 2008-01-06 23:39:25 +0100 | [diff] [blame] | 1214 | unsigned int index; |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 1215 | |
| 1216 | /* |
| 1217 | * We need the following rings: |
| 1218 | * RX: 1 |
| 1219 | * TX: hw->queues |
| 1220 | * Beacon: 1 (if required) |
| 1221 | * Atim: 1 (if required) |
| 1222 | */ |
| 1223 | rt2x00dev->data_rings = 1 + rt2x00dev->hw->queues + |
Ivo van Doorn | 066cb63 | 2007-09-25 20:55:39 +0200 | [diff] [blame] | 1224 | (2 * test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags)); |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 1225 | |
| 1226 | ring = kzalloc(rt2x00dev->data_rings * sizeof(*ring), GFP_KERNEL); |
| 1227 | if (!ring) { |
| 1228 | ERROR(rt2x00dev, "Ring allocation failed.\n"); |
| 1229 | return -ENOMEM; |
| 1230 | } |
| 1231 | |
| 1232 | /* |
| 1233 | * Initialize pointers |
| 1234 | */ |
| 1235 | rt2x00dev->rx = ring; |
| 1236 | rt2x00dev->tx = &rt2x00dev->rx[1]; |
Ivo van Doorn | 066cb63 | 2007-09-25 20:55:39 +0200 | [diff] [blame] | 1237 | if (test_bit(DRIVER_REQUIRE_BEACON_RING, &rt2x00dev->flags)) |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 1238 | rt2x00dev->bcn = &rt2x00dev->tx[rt2x00dev->hw->queues]; |
| 1239 | |
| 1240 | /* |
| 1241 | * Initialize ring parameters. |
| 1242 | * cw_min: 2^5 = 32. |
| 1243 | * cw_max: 2^10 = 1024. |
| 1244 | */ |
Ivo van Doorn | 0426710 | 2008-01-06 23:39:25 +0100 | [diff] [blame] | 1245 | index = 0; |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 1246 | ring_for_each(rt2x00dev, ring) { |
| 1247 | ring->rt2x00dev = rt2x00dev; |
Ivo van Doorn | 0426710 | 2008-01-06 23:39:25 +0100 | [diff] [blame] | 1248 | ring->queue_idx = index++; |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 1249 | ring->tx_params.aifs = 2; |
| 1250 | ring->tx_params.cw_min = 5; |
| 1251 | ring->tx_params.cw_max = 10; |
| 1252 | } |
| 1253 | |
| 1254 | return 0; |
| 1255 | } |
| 1256 | |
| 1257 | static void rt2x00lib_free_rings(struct rt2x00_dev *rt2x00dev) |
| 1258 | { |
| 1259 | kfree(rt2x00dev->rx); |
| 1260 | rt2x00dev->rx = NULL; |
| 1261 | rt2x00dev->tx = NULL; |
| 1262 | rt2x00dev->bcn = NULL; |
| 1263 | } |
| 1264 | |
| 1265 | int rt2x00lib_probe_dev(struct rt2x00_dev *rt2x00dev) |
| 1266 | { |
| 1267 | int retval = -ENOMEM; |
| 1268 | |
| 1269 | /* |
| 1270 | * Let the driver probe the device to detect the capabilities. |
| 1271 | */ |
| 1272 | retval = rt2x00dev->ops->lib->probe_hw(rt2x00dev); |
| 1273 | if (retval) { |
| 1274 | ERROR(rt2x00dev, "Failed to allocate device.\n"); |
| 1275 | goto exit; |
| 1276 | } |
| 1277 | |
| 1278 | /* |
| 1279 | * Initialize configuration work. |
| 1280 | */ |
| 1281 | INIT_WORK(&rt2x00dev->beacon_work, rt2x00lib_beacondone_scheduled); |
Johannes Berg | 4150c57 | 2007-09-17 01:29:23 -0400 | [diff] [blame] | 1282 | INIT_WORK(&rt2x00dev->filter_work, rt2x00lib_packetfilter_scheduled); |
Ivo van Doorn | 5c58ee5 | 2007-10-06 13:34:52 +0200 | [diff] [blame] | 1283 | INIT_WORK(&rt2x00dev->config_work, rt2x00lib_configuration_scheduled); |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 1284 | INIT_DELAYED_WORK(&rt2x00dev->link.work, rt2x00lib_link_tuner); |
| 1285 | |
| 1286 | /* |
| 1287 | * Reset current working type. |
| 1288 | */ |
Ivo van Doorn | d28c256 | 2007-11-27 21:49:50 +0100 | [diff] [blame] | 1289 | rt2x00dev->interface.type = IEEE80211_IF_TYPE_INVALID; |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 1290 | |
| 1291 | /* |
| 1292 | * Allocate ring array. |
| 1293 | */ |
| 1294 | retval = rt2x00lib_alloc_rings(rt2x00dev); |
| 1295 | if (retval) |
| 1296 | goto exit; |
| 1297 | |
| 1298 | /* |
| 1299 | * Initialize ieee80211 structure. |
| 1300 | */ |
| 1301 | retval = rt2x00lib_probe_hw(rt2x00dev); |
| 1302 | if (retval) { |
| 1303 | ERROR(rt2x00dev, "Failed to initialize hw.\n"); |
| 1304 | goto exit; |
| 1305 | } |
| 1306 | |
| 1307 | /* |
| 1308 | * Allocatie rfkill. |
| 1309 | */ |
| 1310 | retval = rt2x00rfkill_allocate(rt2x00dev); |
| 1311 | if (retval) |
| 1312 | goto exit; |
| 1313 | |
| 1314 | /* |
| 1315 | * Open the debugfs entry. |
| 1316 | */ |
| 1317 | rt2x00debug_register(rt2x00dev); |
| 1318 | |
Ivo van Doorn | 066cb63 | 2007-09-25 20:55:39 +0200 | [diff] [blame] | 1319 | __set_bit(DEVICE_PRESENT, &rt2x00dev->flags); |
| 1320 | |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 1321 | return 0; |
| 1322 | |
| 1323 | exit: |
| 1324 | rt2x00lib_remove_dev(rt2x00dev); |
| 1325 | |
| 1326 | return retval; |
| 1327 | } |
| 1328 | EXPORT_SYMBOL_GPL(rt2x00lib_probe_dev); |
| 1329 | |
| 1330 | void rt2x00lib_remove_dev(struct rt2x00_dev *rt2x00dev) |
| 1331 | { |
Ivo van Doorn | 066cb63 | 2007-09-25 20:55:39 +0200 | [diff] [blame] | 1332 | __clear_bit(DEVICE_PRESENT, &rt2x00dev->flags); |
| 1333 | |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 1334 | /* |
| 1335 | * Disable radio. |
| 1336 | */ |
| 1337 | rt2x00lib_disable_radio(rt2x00dev); |
| 1338 | |
| 1339 | /* |
| 1340 | * Uninitialize device. |
| 1341 | */ |
| 1342 | rt2x00lib_uninitialize(rt2x00dev); |
| 1343 | |
| 1344 | /* |
| 1345 | * Close debugfs entry. |
| 1346 | */ |
| 1347 | rt2x00debug_deregister(rt2x00dev); |
| 1348 | |
| 1349 | /* |
| 1350 | * Free rfkill |
| 1351 | */ |
| 1352 | rt2x00rfkill_free(rt2x00dev); |
| 1353 | |
| 1354 | /* |
| 1355 | * Free ieee80211_hw memory. |
| 1356 | */ |
| 1357 | rt2x00lib_remove_hw(rt2x00dev); |
| 1358 | |
| 1359 | /* |
| 1360 | * Free firmware image. |
| 1361 | */ |
| 1362 | rt2x00lib_free_firmware(rt2x00dev); |
| 1363 | |
| 1364 | /* |
| 1365 | * Free ring structures. |
| 1366 | */ |
| 1367 | rt2x00lib_free_rings(rt2x00dev); |
| 1368 | } |
| 1369 | EXPORT_SYMBOL_GPL(rt2x00lib_remove_dev); |
| 1370 | |
| 1371 | /* |
| 1372 | * Device state handlers |
| 1373 | */ |
| 1374 | #ifdef CONFIG_PM |
| 1375 | int rt2x00lib_suspend(struct rt2x00_dev *rt2x00dev, pm_message_t state) |
| 1376 | { |
| 1377 | int retval; |
| 1378 | |
| 1379 | NOTICE(rt2x00dev, "Going to sleep.\n"); |
Ivo van Doorn | 066cb63 | 2007-09-25 20:55:39 +0200 | [diff] [blame] | 1380 | __clear_bit(DEVICE_PRESENT, &rt2x00dev->flags); |
| 1381 | |
| 1382 | /* |
| 1383 | * Only continue if mac80211 has open interfaces. |
| 1384 | */ |
| 1385 | if (!test_bit(DEVICE_STARTED, &rt2x00dev->flags)) |
| 1386 | goto exit; |
Ivo van Doorn | 6d7f987 | 2007-10-06 14:12:42 +0200 | [diff] [blame] | 1387 | __set_bit(DEVICE_STARTED_SUSPEND, &rt2x00dev->flags); |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 1388 | |
| 1389 | /* |
| 1390 | * Disable radio and unitialize all items |
| 1391 | * that must be recreated on resume. |
| 1392 | */ |
Ivo van Doorn | e37ea21 | 2008-01-06 23:40:07 +0100 | [diff] [blame] | 1393 | rt2x00lib_stop(rt2x00dev); |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 1394 | rt2x00lib_uninitialize(rt2x00dev); |
| 1395 | rt2x00debug_deregister(rt2x00dev); |
| 1396 | |
Ivo van Doorn | 066cb63 | 2007-09-25 20:55:39 +0200 | [diff] [blame] | 1397 | exit: |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 1398 | /* |
| 1399 | * Set device mode to sleep for power management. |
| 1400 | */ |
| 1401 | retval = rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_SLEEP); |
| 1402 | if (retval) |
| 1403 | return retval; |
| 1404 | |
| 1405 | return 0; |
| 1406 | } |
| 1407 | EXPORT_SYMBOL_GPL(rt2x00lib_suspend); |
| 1408 | |
| 1409 | int rt2x00lib_resume(struct rt2x00_dev *rt2x00dev) |
| 1410 | { |
| 1411 | struct interface *intf = &rt2x00dev->interface; |
| 1412 | int retval; |
| 1413 | |
| 1414 | NOTICE(rt2x00dev, "Waking up.\n"); |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 1415 | |
| 1416 | /* |
| 1417 | * Open the debugfs entry. |
| 1418 | */ |
| 1419 | rt2x00debug_register(rt2x00dev); |
| 1420 | |
| 1421 | /* |
Ivo van Doorn | 6d7f987 | 2007-10-06 14:12:42 +0200 | [diff] [blame] | 1422 | * Only continue if mac80211 had open interfaces. |
Ivo van Doorn | 066cb63 | 2007-09-25 20:55:39 +0200 | [diff] [blame] | 1423 | */ |
Ivo van Doorn | 6d7f987 | 2007-10-06 14:12:42 +0200 | [diff] [blame] | 1424 | if (!__test_and_clear_bit(DEVICE_STARTED_SUSPEND, &rt2x00dev->flags)) |
Ivo van Doorn | 066cb63 | 2007-09-25 20:55:39 +0200 | [diff] [blame] | 1425 | return 0; |
| 1426 | |
| 1427 | /* |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 1428 | * Reinitialize device and all active interfaces. |
| 1429 | */ |
Ivo van Doorn | e37ea21 | 2008-01-06 23:40:07 +0100 | [diff] [blame] | 1430 | retval = rt2x00lib_start(rt2x00dev); |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 1431 | if (retval) |
| 1432 | goto exit; |
| 1433 | |
| 1434 | /* |
| 1435 | * Reconfigure device. |
| 1436 | */ |
Ivo van Doorn | 066cb63 | 2007-09-25 20:55:39 +0200 | [diff] [blame] | 1437 | rt2x00lib_config(rt2x00dev, &rt2x00dev->hw->conf, 1); |
| 1438 | if (!rt2x00dev->hw->conf.radio_enabled) |
| 1439 | rt2x00lib_disable_radio(rt2x00dev); |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 1440 | |
| 1441 | rt2x00lib_config_mac_addr(rt2x00dev, intf->mac); |
| 1442 | rt2x00lib_config_bssid(rt2x00dev, intf->bssid); |
| 1443 | rt2x00lib_config_type(rt2x00dev, intf->type); |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 1444 | |
| 1445 | /* |
Ivo van Doorn | e37ea21 | 2008-01-06 23:40:07 +0100 | [diff] [blame] | 1446 | * We are ready again to receive requests from mac80211. |
| 1447 | */ |
| 1448 | __set_bit(DEVICE_PRESENT, &rt2x00dev->flags); |
| 1449 | |
| 1450 | /* |
Ivo van Doorn | 066cb63 | 2007-09-25 20:55:39 +0200 | [diff] [blame] | 1451 | * It is possible that during that mac80211 has attempted |
| 1452 | * to send frames while we were suspending or resuming. |
| 1453 | * In that case we have disabled the TX queue and should |
| 1454 | * now enable it again |
| 1455 | */ |
| 1456 | ieee80211_start_queues(rt2x00dev->hw); |
| 1457 | |
| 1458 | /* |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 1459 | * When in Master or Ad-hoc mode, |
| 1460 | * restart Beacon transmitting by faking a beacondone event. |
| 1461 | */ |
| 1462 | if (intf->type == IEEE80211_IF_TYPE_AP || |
| 1463 | intf->type == IEEE80211_IF_TYPE_IBSS) |
| 1464 | rt2x00lib_beacondone(rt2x00dev); |
| 1465 | |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 1466 | return 0; |
| 1467 | |
| 1468 | exit: |
| 1469 | rt2x00lib_disable_radio(rt2x00dev); |
| 1470 | rt2x00lib_uninitialize(rt2x00dev); |
| 1471 | rt2x00debug_deregister(rt2x00dev); |
| 1472 | |
Ivo van Doorn | 95ea362 | 2007-09-25 17:57:13 -0700 | [diff] [blame] | 1473 | return retval; |
| 1474 | } |
| 1475 | EXPORT_SYMBOL_GPL(rt2x00lib_resume); |
| 1476 | #endif /* CONFIG_PM */ |
| 1477 | |
| 1478 | /* |
| 1479 | * rt2x00lib module information. |
| 1480 | */ |
| 1481 | MODULE_AUTHOR(DRV_PROJECT); |
| 1482 | MODULE_VERSION(DRV_VERSION); |
| 1483 | MODULE_DESCRIPTION("rt2x00 library"); |
| 1484 | MODULE_LICENSE("GPL"); |