99b4cf41edf2c59d5d5d2ea2baa5273a21d28d43
[pandora-kernel.git] / drivers / net / wireless / hostap / hostap_info.c
1 /* Host AP driver Info Frame processing (part of hostap.o module) */
2
3 #include "hostap_wlan.h"
4 #include "hostap.h"
5 #include "hostap_ap.h"
6
7 /* Called only as a tasklet (software IRQ) */
8 static void prism2_info_commtallies16(local_info_t *local, unsigned char *buf,
9                                       int left)
10 {
11         struct hfa384x_comm_tallies *tallies;
12
13         if (left < sizeof(struct hfa384x_comm_tallies)) {
14                 printk(KERN_DEBUG "%s: too short (len=%d) commtallies "
15                        "info frame\n", local->dev->name, left);
16                 return;
17         }
18
19         tallies = (struct hfa384x_comm_tallies *) buf;
20 #define ADD_COMM_TALLIES(name) \
21 local->comm_tallies.name += le16_to_cpu(tallies->name)
22         ADD_COMM_TALLIES(tx_unicast_frames);
23         ADD_COMM_TALLIES(tx_multicast_frames);
24         ADD_COMM_TALLIES(tx_fragments);
25         ADD_COMM_TALLIES(tx_unicast_octets);
26         ADD_COMM_TALLIES(tx_multicast_octets);
27         ADD_COMM_TALLIES(tx_deferred_transmissions);
28         ADD_COMM_TALLIES(tx_single_retry_frames);
29         ADD_COMM_TALLIES(tx_multiple_retry_frames);
30         ADD_COMM_TALLIES(tx_retry_limit_exceeded);
31         ADD_COMM_TALLIES(tx_discards);
32         ADD_COMM_TALLIES(rx_unicast_frames);
33         ADD_COMM_TALLIES(rx_multicast_frames);
34         ADD_COMM_TALLIES(rx_fragments);
35         ADD_COMM_TALLIES(rx_unicast_octets);
36         ADD_COMM_TALLIES(rx_multicast_octets);
37         ADD_COMM_TALLIES(rx_fcs_errors);
38         ADD_COMM_TALLIES(rx_discards_no_buffer);
39         ADD_COMM_TALLIES(tx_discards_wrong_sa);
40         ADD_COMM_TALLIES(rx_discards_wep_undecryptable);
41         ADD_COMM_TALLIES(rx_message_in_msg_fragments);
42         ADD_COMM_TALLIES(rx_message_in_bad_msg_fragments);
43 #undef ADD_COMM_TALLIES
44 }
45
46
47 /* Called only as a tasklet (software IRQ) */
48 static void prism2_info_commtallies32(local_info_t *local, unsigned char *buf,
49                                       int left)
50 {
51         struct hfa384x_comm_tallies32 *tallies;
52
53         if (left < sizeof(struct hfa384x_comm_tallies32)) {
54                 printk(KERN_DEBUG "%s: too short (len=%d) commtallies32 "
55                        "info frame\n", local->dev->name, left);
56                 return;
57         }
58
59         tallies = (struct hfa384x_comm_tallies32 *) buf;
60 #define ADD_COMM_TALLIES(name) \
61 local->comm_tallies.name += le32_to_cpu(tallies->name)
62         ADD_COMM_TALLIES(tx_unicast_frames);
63         ADD_COMM_TALLIES(tx_multicast_frames);
64         ADD_COMM_TALLIES(tx_fragments);
65         ADD_COMM_TALLIES(tx_unicast_octets);
66         ADD_COMM_TALLIES(tx_multicast_octets);
67         ADD_COMM_TALLIES(tx_deferred_transmissions);
68         ADD_COMM_TALLIES(tx_single_retry_frames);
69         ADD_COMM_TALLIES(tx_multiple_retry_frames);
70         ADD_COMM_TALLIES(tx_retry_limit_exceeded);
71         ADD_COMM_TALLIES(tx_discards);
72         ADD_COMM_TALLIES(rx_unicast_frames);
73         ADD_COMM_TALLIES(rx_multicast_frames);
74         ADD_COMM_TALLIES(rx_fragments);
75         ADD_COMM_TALLIES(rx_unicast_octets);
76         ADD_COMM_TALLIES(rx_multicast_octets);
77         ADD_COMM_TALLIES(rx_fcs_errors);
78         ADD_COMM_TALLIES(rx_discards_no_buffer);
79         ADD_COMM_TALLIES(tx_discards_wrong_sa);
80         ADD_COMM_TALLIES(rx_discards_wep_undecryptable);
81         ADD_COMM_TALLIES(rx_message_in_msg_fragments);
82         ADD_COMM_TALLIES(rx_message_in_bad_msg_fragments);
83 #undef ADD_COMM_TALLIES
84 }
85
86
87 /* Called only as a tasklet (software IRQ) */
88 static void prism2_info_commtallies(local_info_t *local, unsigned char *buf,
89                                     int left)
90 {
91         if (local->tallies32)
92                 prism2_info_commtallies32(local, buf, left);
93         else
94                 prism2_info_commtallies16(local, buf, left);
95 }
96
97
98 #ifndef PRISM2_NO_STATION_MODES
99 #ifndef PRISM2_NO_DEBUG
100 static const char* hfa384x_linkstatus_str(u16 linkstatus)
101 {
102         switch (linkstatus) {
103         case HFA384X_LINKSTATUS_CONNECTED:
104                 return "Connected";
105         case HFA384X_LINKSTATUS_DISCONNECTED:
106                 return "Disconnected";
107         case HFA384X_LINKSTATUS_AP_CHANGE:
108                 return "Access point change";
109         case HFA384X_LINKSTATUS_AP_OUT_OF_RANGE:
110                 return "Access point out of range";
111         case HFA384X_LINKSTATUS_AP_IN_RANGE:
112                 return "Access point in range";
113         case HFA384X_LINKSTATUS_ASSOC_FAILED:
114                 return "Association failed";
115         default:
116                 return "Unknown";
117         }
118 }
119 #endif /* PRISM2_NO_DEBUG */
120
121
122 /* Called only as a tasklet (software IRQ) */
123 static void prism2_info_linkstatus(local_info_t *local, unsigned char *buf,
124                                     int left)
125 {
126         u16 val;
127         int non_sta_mode;
128
129         /* Alloc new JoinRequests to occur since LinkStatus for the previous
130          * has been received */
131         local->last_join_time = 0;
132
133         if (left != 2) {
134                 printk(KERN_DEBUG "%s: invalid linkstatus info frame "
135                        "length %d\n", local->dev->name, left);
136                 return;
137         }
138
139         non_sta_mode = local->iw_mode == IW_MODE_MASTER ||
140                 local->iw_mode == IW_MODE_REPEAT ||
141                 local->iw_mode == IW_MODE_MONITOR;
142
143         val = buf[0] | (buf[1] << 8);
144         if (!non_sta_mode || val != HFA384X_LINKSTATUS_DISCONNECTED) {
145                 PDEBUG(DEBUG_EXTRA, "%s: LinkStatus=%d (%s)\n",
146                        local->dev->name, val, hfa384x_linkstatus_str(val));
147         }
148
149         if (non_sta_mode) {
150                 netif_carrier_on(local->dev);
151                 netif_carrier_on(local->ddev);
152                 return;
153         }
154
155         /* Get current BSSID later in scheduled task */
156         set_bit(PRISM2_INFO_PENDING_LINKSTATUS, &local->pending_info);
157         local->prev_link_status = val;
158         schedule_work(&local->info_queue);
159 }
160
161
162 static void prism2_host_roaming(local_info_t *local)
163 {
164         struct hfa384x_join_request req;
165         struct net_device *dev = local->dev;
166         struct hfa384x_hostscan_result *selected, *entry;
167         int i;
168         unsigned long flags;
169
170         if (local->last_join_time &&
171             time_before(jiffies, local->last_join_time + 10 * HZ)) {
172                 PDEBUG(DEBUG_EXTRA, "%s: last join request has not yet been "
173                        "completed - waiting for it before issuing new one\n",
174                        dev->name);
175                 return;
176         }
177
178         /* ScanResults are sorted: first ESS results in decreasing signal
179          * quality then IBSS results in similar order.
180          * Trivial roaming policy: just select the first entry.
181          * This could probably be improved by adding hysteresis to limit
182          * number of handoffs, etc.
183          *
184          * Could do periodic RID_SCANREQUEST or Inquire F101 to get new
185          * ScanResults */
186         spin_lock_irqsave(&local->lock, flags);
187         if (local->last_scan_results == NULL ||
188             local->last_scan_results_count == 0) {
189                 spin_unlock_irqrestore(&local->lock, flags);
190                 PDEBUG(DEBUG_EXTRA, "%s: no scan results for host roaming\n",
191                        dev->name);
192                 return;
193         }
194
195         selected = &local->last_scan_results[0];
196
197         if (local->preferred_ap[0] || local->preferred_ap[1] ||
198             local->preferred_ap[2] || local->preferred_ap[3] ||
199             local->preferred_ap[4] || local->preferred_ap[5]) {
200                 /* Try to find preferred AP */
201                 PDEBUG(DEBUG_EXTRA, "%s: Preferred AP BSSID %pM\n",
202                        dev->name, local->preferred_ap);
203                 for (i = 0; i < local->last_scan_results_count; i++) {
204                         entry = &local->last_scan_results[i];
205                         if (memcmp(local->preferred_ap, entry->bssid, 6) == 0)
206                         {
207                                 PDEBUG(DEBUG_EXTRA, "%s: using preferred AP "
208                                        "selection\n", dev->name);
209                                 selected = entry;
210                                 break;
211                         }
212                 }
213         }
214
215         memcpy(req.bssid, selected->bssid, 6);
216         req.channel = selected->chid;
217         spin_unlock_irqrestore(&local->lock, flags);
218
219         PDEBUG(DEBUG_EXTRA, "%s: JoinRequest: BSSID=%pM"
220                " channel=%d\n",
221                dev->name, req.bssid, le16_to_cpu(req.channel));
222         if (local->func->set_rid(dev, HFA384X_RID_JOINREQUEST, &req,
223                                  sizeof(req))) {
224                 printk(KERN_DEBUG "%s: JoinRequest failed\n", dev->name);
225         }
226         local->last_join_time = jiffies;
227 }
228
229
230 static void hostap_report_scan_complete(local_info_t *local)
231 {
232         union iwreq_data wrqu;
233
234         /* Inform user space about new scan results (just empty event,
235          * SIOCGIWSCAN can be used to fetch data */
236         wrqu.data.length = 0;
237         wrqu.data.flags = 0;
238         wireless_send_event(local->dev, SIOCGIWSCAN, &wrqu, NULL);
239
240         /* Allow SIOCGIWSCAN handling to occur since we have received
241          * scanning result */
242         local->scan_timestamp = 0;
243 }
244
245
246 /* Called only as a tasklet (software IRQ) */
247 static void prism2_info_scanresults(local_info_t *local, unsigned char *buf,
248                                     int left)
249 {
250         u16 *pos;
251         int new_count, i;
252         unsigned long flags;
253         struct hfa384x_scan_result *res;
254         struct hfa384x_hostscan_result *results, *prev;
255
256         if (left < 4) {
257                 printk(KERN_DEBUG "%s: invalid scanresult info frame "
258                        "length %d\n", local->dev->name, left);
259                 return;
260         }
261
262         pos = (u16 *) buf;
263         pos++;
264         pos++;
265         left -= 4;
266
267         new_count = left / sizeof(struct hfa384x_scan_result);
268         results = kmalloc(new_count * sizeof(struct hfa384x_hostscan_result),
269                           GFP_ATOMIC);
270         if (results == NULL)
271                 return;
272
273         /* Convert to hostscan result format. */
274         res = (struct hfa384x_scan_result *) pos;
275         for (i = 0; i < new_count; i++) {
276                 memcpy(&results[i], &res[i],
277                        sizeof(struct hfa384x_scan_result));
278                 results[i].atim = 0;
279         }
280
281         spin_lock_irqsave(&local->lock, flags);
282         local->last_scan_type = PRISM2_SCAN;
283         prev = local->last_scan_results;
284         local->last_scan_results = results;
285         local->last_scan_results_count = new_count;
286         spin_unlock_irqrestore(&local->lock, flags);
287         kfree(prev);
288
289         hostap_report_scan_complete(local);
290
291         /* Perform rest of ScanResults handling later in scheduled task */
292         set_bit(PRISM2_INFO_PENDING_SCANRESULTS, &local->pending_info);
293         schedule_work(&local->info_queue);
294 }
295
296
297 /* Called only as a tasklet (software IRQ) */
298 static void prism2_info_hostscanresults(local_info_t *local,
299                                         unsigned char *buf, int left)
300 {
301         int i, result_size, copy_len, new_count;
302         struct hfa384x_hostscan_result *results, *prev;
303         unsigned long flags;
304         __le16 *pos;
305         u8 *ptr;
306
307         wake_up_interruptible(&local->hostscan_wq);
308
309         if (left < 4) {
310                 printk(KERN_DEBUG "%s: invalid hostscanresult info frame "
311                        "length %d\n", local->dev->name, left);
312                 return;
313         }
314
315         pos = (__le16 *) buf;
316         copy_len = result_size = le16_to_cpu(*pos);
317         if (result_size == 0) {
318                 printk(KERN_DEBUG "%s: invalid result_size (0) in "
319                        "hostscanresults\n", local->dev->name);
320                 return;
321         }
322         if (copy_len > sizeof(struct hfa384x_hostscan_result))
323                 copy_len = sizeof(struct hfa384x_hostscan_result);
324
325         pos++;
326         pos++;
327         left -= 4;
328         ptr = (u8 *) pos;
329
330         new_count = left / result_size;
331         results = kcalloc(new_count, sizeof(struct hfa384x_hostscan_result),
332                           GFP_ATOMIC);
333         if (results == NULL)
334                 return;
335
336         for (i = 0; i < new_count; i++) {
337                 memcpy(&results[i], ptr, copy_len);
338                 ptr += result_size;
339                 left -= result_size;
340         }
341
342         if (left) {
343                 printk(KERN_DEBUG "%s: short HostScan result entry (%d/%d)\n",
344                        local->dev->name, left, result_size);
345         }
346
347         spin_lock_irqsave(&local->lock, flags);
348         local->last_scan_type = PRISM2_HOSTSCAN;
349         prev = local->last_scan_results;
350         local->last_scan_results = results;
351         local->last_scan_results_count = new_count;
352         spin_unlock_irqrestore(&local->lock, flags);
353         kfree(prev);
354
355         hostap_report_scan_complete(local);
356 }
357 #endif /* PRISM2_NO_STATION_MODES */
358
359
360 /* Called only as a tasklet (software IRQ) */
361 void hostap_info_process(local_info_t *local, struct sk_buff *skb)
362 {
363         struct hfa384x_info_frame *info;
364         unsigned char *buf;
365         int left;
366 #ifndef PRISM2_NO_DEBUG
367         int i;
368 #endif /* PRISM2_NO_DEBUG */
369
370         info = (struct hfa384x_info_frame *) skb->data;
371         buf = skb->data + sizeof(*info);
372         left = skb->len - sizeof(*info);
373
374         switch (le16_to_cpu(info->type)) {
375         case HFA384X_INFO_COMMTALLIES:
376                 prism2_info_commtallies(local, buf, left);
377                 break;
378
379 #ifndef PRISM2_NO_STATION_MODES
380         case HFA384X_INFO_LINKSTATUS:
381                 prism2_info_linkstatus(local, buf, left);
382                 break;
383
384         case HFA384X_INFO_SCANRESULTS:
385                 prism2_info_scanresults(local, buf, left);
386                 break;
387
388         case HFA384X_INFO_HOSTSCANRESULTS:
389                 prism2_info_hostscanresults(local, buf, left);
390                 break;
391 #endif /* PRISM2_NO_STATION_MODES */
392
393 #ifndef PRISM2_NO_DEBUG
394         default:
395                 PDEBUG(DEBUG_EXTRA, "%s: INFO - len=%d type=0x%04x\n",
396                        local->dev->name, le16_to_cpu(info->len),
397                        le16_to_cpu(info->type));
398                 PDEBUG(DEBUG_EXTRA, "Unknown info frame:");
399                 for (i = 0; i < (left < 100 ? left : 100); i++)
400                         PDEBUG2(DEBUG_EXTRA, " %02x", buf[i]);
401                 PDEBUG2(DEBUG_EXTRA, "\n");
402                 break;
403 #endif /* PRISM2_NO_DEBUG */
404         }
405 }
406
407
408 #ifndef PRISM2_NO_STATION_MODES
409 static void handle_info_queue_linkstatus(local_info_t *local)
410 {
411         int val = local->prev_link_status;
412         int connected;
413         union iwreq_data wrqu;
414
415         connected =
416                 val == HFA384X_LINKSTATUS_CONNECTED ||
417                 val == HFA384X_LINKSTATUS_AP_CHANGE ||
418                 val == HFA384X_LINKSTATUS_AP_IN_RANGE;
419
420         if (local->func->get_rid(local->dev, HFA384X_RID_CURRENTBSSID,
421                                  local->bssid, ETH_ALEN, 1) < 0) {
422                 printk(KERN_DEBUG "%s: could not read CURRENTBSSID after "
423                        "LinkStatus event\n", local->dev->name);
424         } else {
425                 PDEBUG(DEBUG_EXTRA, "%s: LinkStatus: BSSID=%pM\n",
426                        local->dev->name,
427                        (unsigned char *) local->bssid);
428                 if (local->wds_type & HOSTAP_WDS_AP_CLIENT)
429                         hostap_add_sta(local->ap, local->bssid);
430         }
431
432         /* Get BSSID if we have a valid AP address */
433         if (connected) {
434                 netif_carrier_on(local->dev);
435                 netif_carrier_on(local->ddev);
436                 memcpy(wrqu.ap_addr.sa_data, local->bssid, ETH_ALEN);
437         } else {
438                 netif_carrier_off(local->dev);
439                 netif_carrier_off(local->ddev);
440                 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
441         }
442         wrqu.ap_addr.sa_family = ARPHRD_ETHER;
443
444         /*
445          * Filter out sequential disconnect events in order not to cause a
446          * flood of SIOCGIWAP events that have a race condition with EAPOL
447          * frames and can confuse wpa_supplicant about the current association
448          * status.
449          */
450         if (connected || local->prev_linkstatus_connected)
451                 wireless_send_event(local->dev, SIOCGIWAP, &wrqu, NULL);
452         local->prev_linkstatus_connected = connected;
453 }
454
455
456 static void handle_info_queue_scanresults(local_info_t *local)
457 {
458         if (local->host_roaming == 1 && local->iw_mode == IW_MODE_INFRA)
459                 prism2_host_roaming(local);
460
461         if (local->host_roaming == 2 && local->iw_mode == IW_MODE_INFRA &&
462             memcmp(local->preferred_ap, "\x00\x00\x00\x00\x00\x00",
463                    ETH_ALEN) != 0) {
464                 /*
465                  * Firmware seems to be getting into odd state in host_roaming
466                  * mode 2 when hostscan is used without join command, so try
467                  * to fix this by re-joining the current AP. This does not
468                  * actually trigger a new association if the current AP is
469                  * still in the scan results.
470                  */
471                 prism2_host_roaming(local);
472         }
473 }
474
475
476 /* Called only as scheduled task after receiving info frames (used to avoid
477  * pending too much time in HW IRQ handler). */
478 static void handle_info_queue(struct work_struct *work)
479 {
480         local_info_t *local = container_of(work, local_info_t, info_queue);
481
482         if (test_and_clear_bit(PRISM2_INFO_PENDING_LINKSTATUS,
483                                &local->pending_info))
484                 handle_info_queue_linkstatus(local);
485
486         if (test_and_clear_bit(PRISM2_INFO_PENDING_SCANRESULTS,
487                                &local->pending_info))
488                 handle_info_queue_scanresults(local);
489 }
490 #endif /* PRISM2_NO_STATION_MODES */
491
492
493 void hostap_info_init(local_info_t *local)
494 {
495         skb_queue_head_init(&local->info_list);
496 #ifndef PRISM2_NO_STATION_MODES
497         INIT_WORK(&local->info_queue, handle_info_queue);
498 #endif /* PRISM2_NO_STATION_MODES */
499 }
500
501
502 EXPORT_SYMBOL(hostap_info_init);
503 EXPORT_SYMBOL(hostap_info_process);