[RT2x00]: add driver for Ralink wireless hardware
[safe/jmp/linux-2.6] / drivers / net / wireless / rt2x00 / rt2x00config.c
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 configuration 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 void rt2x00lib_config_mac_addr(struct rt2x00_dev *rt2x00dev, u8 *mac)
38 {
39         if (mac)
40                 rt2x00dev->ops->lib->config_mac_addr(rt2x00dev, mac);
41 }
42
43 void rt2x00lib_config_bssid(struct rt2x00_dev *rt2x00dev, u8 *bssid)
44 {
45         if (bssid)
46                 rt2x00dev->ops->lib->config_bssid(rt2x00dev, bssid);
47 }
48
49 void rt2x00lib_config_packet_filter(struct rt2x00_dev *rt2x00dev, int filter)
50 {
51         /*
52          * Only configure the device when something has changed,
53          * or if we are in RESUME state in which case all configuration
54          * will be forced upon the device.
55          */
56         if (!test_bit(INTERFACE_RESUME, &rt2x00dev->flags) &&
57             !test_bit(PACKET_FILTER_PENDING, &rt2x00dev->flags))
58                 return;
59
60         /*
61          * Write configuration to device and clear the update flag.
62          */
63         rt2x00dev->ops->lib->config_packet_filter(rt2x00dev, filter);
64         __clear_bit(PACKET_FILTER_PENDING, &rt2x00dev->flags);
65 }
66
67 void rt2x00lib_config_type(struct rt2x00_dev *rt2x00dev, int type)
68 {
69         struct interface *intf = &rt2x00dev->interface;
70
71         /*
72          * Fallback when a invalid interface is attempted to
73          * be configured. If a monitor interface is present,
74          * we are going configure that, otherwise exit.
75          */
76         if (type == INVALID_INTERFACE) {
77                 if (is_monitor_present(intf))
78                         type = IEEE80211_IF_TYPE_MNTR;
79                 else
80                         return;
81         }
82
83         /*
84          * Only configure the device when something has changed,
85          * or if we are in RESUME state in which case all configuration
86          * will be forced upon the device.
87          */
88         if (!test_bit(INTERFACE_RESUME, &rt2x00dev->flags) &&
89             (!(is_interface_present(intf) ^
90                test_bit(INTERFACE_ENABLED, &rt2x00dev->flags)) &&
91              !(is_monitor_present(intf) ^
92                test_bit(INTERFACE_ENABLED_MONITOR, &rt2x00dev->flags))))
93                 return;
94
95         /*
96          * Configure device.
97          */
98         rt2x00dev->ops->lib->config_type(rt2x00dev, type);
99
100         /*
101          * Update the configuration flags.
102          */
103         if (type != IEEE80211_IF_TYPE_MNTR) {
104                 if (is_interface_present(intf))
105                         __set_bit(INTERFACE_ENABLED, &rt2x00dev->flags);
106                 else
107                         __clear_bit(INTERFACE_ENABLED, &rt2x00dev->flags);
108         } else {
109                 if (is_monitor_present(intf))
110                         __set_bit(INTERFACE_ENABLED_MONITOR, &rt2x00dev->flags);
111                 else
112                         __clear_bit(INTERFACE_ENABLED_MONITOR,
113                                     &rt2x00dev->flags);
114         }
115 }
116
117 void rt2x00lib_config(struct rt2x00_dev *rt2x00dev, struct ieee80211_conf *conf)
118 {
119         int flags = 0;
120
121         /*
122          * If we are in RESUME state we should
123          * force all configuration options.
124          */
125         if (test_bit(INTERFACE_RESUME, &rt2x00dev->flags)) {
126                 flags = CONFIG_UPDATE_ALL;
127                 goto config;
128         }
129
130         /*
131          * Check which configuration options have been
132          * updated and should be send to the device.
133          */
134         if (rt2x00dev->rx_status.phymode != conf->phymode)
135                 flags |= CONFIG_UPDATE_PHYMODE;
136         if (rt2x00dev->rx_status.channel != conf->channel)
137                 flags |= CONFIG_UPDATE_CHANNEL;
138         if (rt2x00dev->tx_power != conf->power_level)
139                 flags |= CONFIG_UPDATE_TXPOWER;
140         if (rt2x00dev->rx_status.antenna == conf->antenna_sel_rx)
141                 flags |= CONFIG_UPDATE_ANTENNA;
142
143         /*
144          * The following configuration options are never
145          * stored anywhere and will always be updated.
146          */
147         flags |= CONFIG_UPDATE_SLOT_TIME;
148         flags |= CONFIG_UPDATE_BEACON_INT;
149
150 config:
151         rt2x00dev->ops->lib->config(rt2x00dev, flags, conf);
152
153         /*
154          * Some configuration changes affect the link quality
155          * which means we need to reset the link tuner.
156          */
157         if (flags & (CONFIG_UPDATE_CHANNEL | CONFIG_UPDATE_ANTENNA))
158                 rt2x00lib_reset_link_tuner(rt2x00dev);
159
160         rt2x00dev->rx_status.phymode = conf->phymode;
161         rt2x00dev->rx_status.freq = conf->freq;
162         rt2x00dev->rx_status.channel = conf->channel;
163         rt2x00dev->tx_power = conf->power_level;
164         rt2x00dev->rx_status.antenna = conf->antenna_sel_rx;
165 }