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