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