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