Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/bart/ide-2.6
[linux-2.6] / drivers / net / wireless / iwlwifi / iwl-spectrum.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2003 - 2009 Intel Corporation. All rights reserved.
4  *
5  * Portions of this file are derived from the ipw3945 project, as well
6  * as portions of the ieee80211 subsystem header files.
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of version 2 of the GNU General Public License as
10  * published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
20  *
21  * The full GNU General Public License is included in this distribution in the
22  * file called LICENSE.
23  *
24  * Contact Information:
25  *  Intel Linux Wireless <ilw@linux.intel.com>
26  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27  *
28  *****************************************************************************/
29
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/init.h>
33 #include <linux/pci.h>
34 #include <linux/delay.h>
35 #include <linux/skbuff.h>
36 #include <linux/netdevice.h>
37 #include <linux/wireless.h>
38
39 #include <net/mac80211.h>
40
41 #include "iwl-eeprom.h"
42 #include "iwl-dev.h"
43 #include "iwl-core.h"
44 #include "iwl-io.h"
45 #include "iwl-spectrum.h"
46
47 #define BEACON_TIME_MASK_LOW    0x00FFFFFF
48 #define BEACON_TIME_MASK_HIGH   0xFF000000
49 #define TIME_UNIT               1024
50
51 /*
52  * extended beacon time format
53  * time in usec will be changed into a 32-bit value in 8:24 format
54  * the high 1 byte is the beacon counts
55  * the lower 3 bytes is the time in usec within one beacon interval
56  */
57
58 /* TOOD: was used in sysfs debug interface need to add to mac */
59 #if 0
60 static u32 iwl_usecs_to_beacons(u32 usec, u32 beacon_interval)
61 {
62         u32 quot;
63         u32 rem;
64         u32 interval = beacon_interval * 1024;
65
66         if (!interval || !usec)
67                 return 0;
68
69         quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
70         rem = (usec % interval) & BEACON_TIME_MASK_LOW;
71
72         return (quot << 24) + rem;
73 }
74
75 /* base is usually what we get from ucode with each received frame,
76  * the same as HW timer counter counting down
77  */
78
79 static __le32 iwl_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
80 {
81         u32 base_low = base & BEACON_TIME_MASK_LOW;
82         u32 addon_low = addon & BEACON_TIME_MASK_LOW;
83         u32 interval = beacon_interval * TIME_UNIT;
84         u32 res = (base & BEACON_TIME_MASK_HIGH) +
85             (addon & BEACON_TIME_MASK_HIGH);
86
87         if (base_low > addon_low)
88                 res += base_low - addon_low;
89         else if (base_low < addon_low) {
90                 res += interval + base_low - addon_low;
91                 res += (1 << 24);
92         } else
93                 res += (1 << 24);
94
95         return cpu_to_le32(res);
96 }
97 static int iwl_get_measurement(struct iwl_priv *priv,
98                                struct ieee80211_measurement_params *params,
99                                u8 type)
100 {
101         struct iwl4965_spectrum_cmd spectrum;
102         struct iwl_rx_packet *res;
103         struct iwl_host_cmd cmd = {
104                 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
105                 .data = (void *)&spectrum,
106                 .meta.flags = CMD_WANT_SKB,
107         };
108         u32 add_time = le64_to_cpu(params->start_time);
109         int rc;
110         int spectrum_resp_status;
111         int duration = le16_to_cpu(params->duration);
112
113         if (iwl_is_associated(priv))
114                 add_time =
115                     iwl_usecs_to_beacons(
116                         le64_to_cpu(params->start_time) - priv->last_tsf,
117                         le16_to_cpu(priv->rxon_timing.beacon_interval));
118
119         memset(&spectrum, 0, sizeof(spectrum));
120
121         spectrum.channel_count = cpu_to_le16(1);
122         spectrum.flags =
123             RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
124         spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
125         cmd.len = sizeof(spectrum);
126         spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
127
128         if (iwl_is_associated(priv))
129                 spectrum.start_time =
130                     iwl_add_beacon_time(priv->last_beacon_time,
131                                 add_time,
132                                 le16_to_cpu(priv->rxon_timing.beacon_interval));
133         else
134                 spectrum.start_time = 0;
135
136         spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
137         spectrum.channels[0].channel = params->channel;
138         spectrum.channels[0].type = type;
139         if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
140                 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
141                     RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
142
143         rc = iwl_send_cmd_sync(priv, &cmd);
144         if (rc)
145                 return rc;
146
147         res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
148         if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
149                 IWL_ERR(priv, "Bad return from REPLY_RX_ON_ASSOC command\n");
150                 rc = -EIO;
151         }
152
153         spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
154         switch (spectrum_resp_status) {
155         case 0:         /* Command will be handled */
156                 if (res->u.spectrum.id != 0xff) {
157                         IWL_DEBUG_INFO(priv,
158                                 "Replaced existing measurement: %d\n",
159                                 res->u.spectrum.id);
160                         priv->measurement_status &= ~MEASUREMENT_READY;
161                 }
162                 priv->measurement_status |= MEASUREMENT_ACTIVE;
163                 rc = 0;
164                 break;
165
166         case 1:         /* Command will not be handled */
167                 rc = -EAGAIN;
168                 break;
169         }
170
171         dev_kfree_skb_any(cmd.meta.u.skb);
172
173         return rc;
174 }
175 #endif
176
177 static void iwl_rx_spectrum_measure_notif(struct iwl_priv *priv,
178                                           struct iwl_rx_mem_buffer *rxb)
179 {
180         struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
181         struct iwl_spectrum_notification *report = &(pkt->u.spectrum_notif);
182
183         if (!report->state) {
184                 IWL_DEBUG_11H(priv,
185                         "Spectrum Measure Notification: Start\n");
186                 return;
187         }
188
189         memcpy(&priv->measure_report, report, sizeof(*report));
190         priv->measurement_status |= MEASUREMENT_READY;
191 }
192
193 void iwl_setup_spectrum_handlers(struct iwl_priv *priv)
194 {
195         priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
196                         iwl_rx_spectrum_measure_notif;
197 }
198 EXPORT_SYMBOL(iwl_setup_spectrum_handlers);