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