Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/bart/ide-2.6
[linux-2.6] / drivers / net / ps3_gelic_wireless.c
1 /*
2  *  PS3 gelic network driver.
3  *
4  * Copyright (C) 2007 Sony Computer Entertainment Inc.
5  * Copyright 2007 Sony Corporation
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2
9  * as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20 #undef DEBUG
21
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24
25 #include <linux/etherdevice.h>
26 #include <linux/ethtool.h>
27 #include <linux/if_vlan.h>
28
29 #include <linux/in.h>
30 #include <linux/ip.h>
31 #include <linux/tcp.h>
32 #include <linux/wireless.h>
33 #include <linux/ieee80211.h>
34 #include <linux/if_arp.h>
35 #include <linux/ctype.h>
36 #include <linux/string.h>
37 #include <net/iw_handler.h>
38
39 #include <linux/dma-mapping.h>
40 #include <net/checksum.h>
41 #include <asm/firmware.h>
42 #include <asm/ps3.h>
43 #include <asm/lv1call.h>
44
45 #include "ps3_gelic_net.h"
46 #include "ps3_gelic_wireless.h"
47
48
49 static int gelic_wl_start_scan(struct gelic_wl_info *wl, int always_scan,
50                                u8 *essid, size_t essid_len);
51 static int gelic_wl_try_associate(struct net_device *netdev);
52
53 /*
54  * tables
55  */
56
57 /* 802.11b/g channel to freq in MHz */
58 static const int channel_freq[] = {
59         2412, 2417, 2422, 2427, 2432,
60         2437, 2442, 2447, 2452, 2457,
61         2462, 2467, 2472, 2484
62 };
63 #define NUM_CHANNELS ARRAY_SIZE(channel_freq)
64
65 /* in bps */
66 static const int bitrate_list[] = {
67           1000000,
68           2000000,
69           5500000,
70          11000000,
71           6000000,
72           9000000,
73          12000000,
74          18000000,
75          24000000,
76          36000000,
77          48000000,
78          54000000
79 };
80 #define NUM_BITRATES ARRAY_SIZE(bitrate_list)
81
82 /*
83  * wpa2 support requires the hypervisor version 2.0 or later
84  */
85 static inline int wpa2_capable(void)
86 {
87         return (0 <= ps3_compare_firmware_version(2, 0, 0));
88 }
89
90 static inline int precise_ie(void)
91 {
92         return (0 <= ps3_compare_firmware_version(2, 2, 0));
93 }
94 /*
95  * post_eurus_cmd helpers
96  */
97 struct eurus_cmd_arg_info {
98         int pre_arg; /* command requres arg1, arg2 at POST COMMAND */
99         int post_arg; /* command requires arg1, arg2 at GET_RESULT */
100 };
101
102 static const struct eurus_cmd_arg_info cmd_info[GELIC_EURUS_CMD_MAX_INDEX] = {
103         [GELIC_EURUS_CMD_SET_COMMON_CFG] = { .pre_arg = 1},
104         [GELIC_EURUS_CMD_SET_WEP_CFG]    = { .pre_arg = 1},
105         [GELIC_EURUS_CMD_SET_WPA_CFG]    = { .pre_arg = 1},
106         [GELIC_EURUS_CMD_GET_COMMON_CFG] = { .post_arg = 1},
107         [GELIC_EURUS_CMD_GET_WEP_CFG]    = { .post_arg = 1},
108         [GELIC_EURUS_CMD_GET_WPA_CFG]    = { .post_arg = 1},
109         [GELIC_EURUS_CMD_GET_RSSI_CFG]   = { .post_arg = 1},
110         [GELIC_EURUS_CMD_START_SCAN]     = { .pre_arg = 1},
111         [GELIC_EURUS_CMD_GET_SCAN]       = { .post_arg = 1},
112 };
113
114 #ifdef DEBUG
115 static const char *cmdstr(enum gelic_eurus_command ix)
116 {
117         switch (ix) {
118         case GELIC_EURUS_CMD_ASSOC:
119                 return "ASSOC";
120         case GELIC_EURUS_CMD_DISASSOC:
121                 return "DISASSOC";
122         case GELIC_EURUS_CMD_START_SCAN:
123                 return "SCAN";
124         case GELIC_EURUS_CMD_GET_SCAN:
125                 return "GET SCAN";
126         case GELIC_EURUS_CMD_SET_COMMON_CFG:
127                 return "SET_COMMON_CFG";
128         case GELIC_EURUS_CMD_GET_COMMON_CFG:
129                 return "GET_COMMON_CFG";
130         case GELIC_EURUS_CMD_SET_WEP_CFG:
131                 return "SET_WEP_CFG";
132         case GELIC_EURUS_CMD_GET_WEP_CFG:
133                 return "GET_WEP_CFG";
134         case GELIC_EURUS_CMD_SET_WPA_CFG:
135                 return "SET_WPA_CFG";
136         case GELIC_EURUS_CMD_GET_WPA_CFG:
137                 return "GET_WPA_CFG";
138         case GELIC_EURUS_CMD_GET_RSSI_CFG:
139                 return "GET_RSSI";
140         default:
141                 break;
142         }
143         return "";
144 };
145 #else
146 static inline const char *cmdstr(enum gelic_eurus_command ix)
147 {
148         return "";
149 }
150 #endif
151
152 /* synchronously do eurus commands */
153 static void gelic_eurus_sync_cmd_worker(struct work_struct *work)
154 {
155         struct gelic_eurus_cmd *cmd;
156         struct gelic_card *card;
157         struct gelic_wl_info *wl;
158
159         u64 arg1, arg2;
160
161         pr_debug("%s: <-\n", __func__);
162         cmd = container_of(work, struct gelic_eurus_cmd, work);
163         BUG_ON(cmd_info[cmd->cmd].pre_arg &&
164                cmd_info[cmd->cmd].post_arg);
165         wl = cmd->wl;
166         card = port_to_card(wl_port(wl));
167
168         if (cmd_info[cmd->cmd].pre_arg) {
169                 arg1 = (cmd->buffer) ?
170                         ps3_mm_phys_to_lpar(__pa(cmd->buffer)) :
171                         0;
172                 arg2 = cmd->buf_size;
173         } else {
174                 arg1 = 0;
175                 arg2 = 0;
176         }
177         init_completion(&wl->cmd_done_intr);
178         pr_debug("%s: cmd='%s' start\n", __func__, cmdstr(cmd->cmd));
179         cmd->status = lv1_net_control(bus_id(card), dev_id(card),
180                                       GELIC_LV1_POST_WLAN_CMD,
181                                       cmd->cmd, arg1, arg2,
182                                       &cmd->tag, &cmd->size);
183         if (cmd->status) {
184                 complete(&cmd->done);
185                 pr_info("%s: cmd issue failed\n", __func__);
186                 return;
187         }
188
189         wait_for_completion(&wl->cmd_done_intr);
190
191         if (cmd_info[cmd->cmd].post_arg) {
192                 arg1 = ps3_mm_phys_to_lpar(__pa(cmd->buffer));
193                 arg2 = cmd->buf_size;
194         } else {
195                 arg1 = 0;
196                 arg2 = 0;
197         }
198
199         cmd->status = lv1_net_control(bus_id(card), dev_id(card),
200                                       GELIC_LV1_GET_WLAN_CMD_RESULT,
201                                       cmd->tag, arg1, arg2,
202                                       &cmd->cmd_status, &cmd->size);
203 #ifdef DEBUG
204         if (cmd->status || cmd->cmd_status) {
205         pr_debug("%s: cmd done tag=%#lx arg1=%#lx, arg2=%#lx\n", __func__,
206                  cmd->tag, arg1, arg2);
207         pr_debug("%s: cmd done status=%#x cmd_status=%#lx size=%#lx\n",
208                  __func__, cmd->status, cmd->cmd_status, cmd->size);
209         }
210 #endif
211         complete(&cmd->done);
212         pr_debug("%s: cmd='%s' done\n", __func__, cmdstr(cmd->cmd));
213 }
214
215 static struct gelic_eurus_cmd *gelic_eurus_sync_cmd(struct gelic_wl_info *wl,
216                                                     unsigned int eurus_cmd,
217                                                     void *buffer,
218                                                     unsigned int buf_size)
219 {
220         struct gelic_eurus_cmd *cmd;
221
222         /* allocate cmd */
223         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
224         if (!cmd)
225                 return NULL;
226
227         /* initialize members */
228         cmd->cmd = eurus_cmd;
229         cmd->buffer = buffer;
230         cmd->buf_size = buf_size;
231         cmd->wl = wl;
232         INIT_WORK(&cmd->work, gelic_eurus_sync_cmd_worker);
233         init_completion(&cmd->done);
234         queue_work(wl->eurus_cmd_queue, &cmd->work);
235
236         /* wait for command completion */
237         wait_for_completion(&cmd->done);
238
239         return cmd;
240 }
241
242 static u32 gelic_wl_get_link(struct net_device *netdev)
243 {
244         struct gelic_wl_info *wl = port_wl(netdev_port(netdev));
245         u32 ret;
246
247         pr_debug("%s: <-\n", __func__);
248         mutex_lock(&wl->assoc_stat_lock);
249         if (wl->assoc_stat == GELIC_WL_ASSOC_STAT_ASSOCIATED)
250                 ret = 1;
251         else
252                 ret = 0;
253         mutex_unlock(&wl->assoc_stat_lock);
254         pr_debug("%s: ->\n", __func__);
255         return ret;
256 }
257
258 static void gelic_wl_send_iwap_event(struct gelic_wl_info *wl, u8 *bssid)
259 {
260         union iwreq_data data;
261
262         memset(&data, 0, sizeof(data));
263         if (bssid)
264                 memcpy(data.ap_addr.sa_data, bssid, ETH_ALEN);
265         data.ap_addr.sa_family = ARPHRD_ETHER;
266         wireless_send_event(port_to_netdev(wl_port(wl)), SIOCGIWAP,
267                             &data, NULL);
268 }
269
270 /*
271  * wireless extension handlers and helpers
272  */
273
274 /* SIOGIWNAME */
275 static int gelic_wl_get_name(struct net_device *dev,
276                              struct iw_request_info *info,
277                              union iwreq_data *iwreq, char *extra)
278 {
279         strcpy(iwreq->name, "IEEE 802.11bg");
280         return 0;
281 }
282
283 static void gelic_wl_get_ch_info(struct gelic_wl_info *wl)
284 {
285         struct gelic_card *card = port_to_card(wl_port(wl));
286         u64 ch_info_raw, tmp;
287         int status;
288
289         if (!test_and_set_bit(GELIC_WL_STAT_CH_INFO, &wl->stat)) {
290                 status = lv1_net_control(bus_id(card), dev_id(card),
291                                          GELIC_LV1_GET_CHANNEL, 0, 0, 0,
292                                          &ch_info_raw,
293                                          &tmp);
294                 /* some fw versions may return error */
295                 if (status) {
296                         if (status != LV1_NO_ENTRY)
297                                 pr_info("%s: available ch unknown\n", __func__);
298                         wl->ch_info = 0x07ff;/* 11 ch */
299                 } else
300                         /* 16 bits of MSB has available channels */
301                         wl->ch_info = ch_info_raw >> 48;
302         }
303         return;
304 }
305
306 /* SIOGIWRANGE */
307 static int gelic_wl_get_range(struct net_device *netdev,
308                               struct iw_request_info *info,
309                               union iwreq_data *iwreq, char *extra)
310 {
311         struct iw_point *point = &iwreq->data;
312         struct iw_range *range = (struct iw_range *)extra;
313         struct gelic_wl_info *wl = port_wl(netdev_port(netdev));
314         unsigned int i, chs;
315
316         pr_debug("%s: <-\n", __func__);
317         point->length = sizeof(struct iw_range);
318         memset(range, 0, sizeof(struct iw_range));
319
320         range->we_version_compiled = WIRELESS_EXT;
321         range->we_version_source = 22;
322
323         /* available channels and frequencies */
324         gelic_wl_get_ch_info(wl);
325
326         for (i = 0, chs = 0;
327              i < NUM_CHANNELS && chs < IW_MAX_FREQUENCIES; i++)
328                 if (wl->ch_info & (1 << i)) {
329                         range->freq[chs].i = i + 1;
330                         range->freq[chs].m = channel_freq[i];
331                         range->freq[chs].e = 6;
332                         chs++;
333                 }
334         range->num_frequency = chs;
335         range->old_num_frequency = chs;
336         range->num_channels = chs;
337         range->old_num_channels = chs;
338
339         /* bitrates */
340         for (i = 0; i < NUM_BITRATES; i++)
341                 range->bitrate[i] = bitrate_list[i];
342         range->num_bitrates = i;
343
344         /* signal levels */
345         range->max_qual.qual = 100; /* relative value */
346         range->max_qual.level = 100;
347         range->avg_qual.qual = 50;
348         range->avg_qual.level = 50;
349         range->sensitivity = 0;
350
351         /* Event capability */
352         IW_EVENT_CAPA_SET_KERNEL(range->event_capa);
353         IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWAP);
354         IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWSCAN);
355
356         /* encryption capability */
357         range->enc_capa = IW_ENC_CAPA_WPA |
358                 IW_ENC_CAPA_CIPHER_TKIP | IW_ENC_CAPA_CIPHER_CCMP |
359                 IW_ENC_CAPA_4WAY_HANDSHAKE;
360         if (wpa2_capable())
361                 range->enc_capa |= IW_ENC_CAPA_WPA2;
362         range->encoding_size[0] = 5;    /* 40bit WEP */
363         range->encoding_size[1] = 13;   /* 104bit WEP */
364         range->encoding_size[2] = 32;   /* WPA-PSK */
365         range->num_encoding_sizes = 3;
366         range->max_encoding_tokens = GELIC_WEP_KEYS;
367
368         /* scan capability */
369         range->scan_capa = IW_SCAN_CAPA_ESSID;
370
371         pr_debug("%s: ->\n", __func__);
372         return 0;
373
374 }
375
376 /* SIOC{G,S}IWSCAN */
377 static int gelic_wl_set_scan(struct net_device *netdev,
378                            struct iw_request_info *info,
379                            union iwreq_data *wrqu, char *extra)
380 {
381         struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
382         struct iw_scan_req *req;
383         u8 *essid = NULL;
384         size_t essid_len = 0;
385
386         if (wrqu->data.length == sizeof(struct iw_scan_req) &&
387             wrqu->data.flags & IW_SCAN_THIS_ESSID) {
388                 req = (struct iw_scan_req*)extra;
389                 essid = req->essid;
390                 essid_len = req->essid_len;
391                 pr_debug("%s: ESSID scan =%s\n", __func__, essid);
392         }
393         return gelic_wl_start_scan(wl, 1, essid, essid_len);
394 }
395
396 #define OUI_LEN 3
397 static const u8 rsn_oui[OUI_LEN] = { 0x00, 0x0f, 0xac };
398 static const u8 wpa_oui[OUI_LEN] = { 0x00, 0x50, 0xf2 };
399
400 /*
401  * synthesize WPA/RSN IE data
402  * See WiFi WPA specification and IEEE 802.11-2007 7.3.2.25
403  * for the format
404  */
405 static size_t gelic_wl_synthesize_ie(u8 *buf,
406                                      struct gelic_eurus_scan_info *scan)
407 {
408
409         const u8 *oui_header;
410         u8 *start = buf;
411         int rsn;
412         int ccmp;
413
414         pr_debug("%s: <- sec=%16x\n", __func__, scan->security);
415         switch (be16_to_cpu(scan->security) & GELIC_EURUS_SCAN_SEC_MASK) {
416         case GELIC_EURUS_SCAN_SEC_WPA:
417                 rsn = 0;
418                 break;
419         case GELIC_EURUS_SCAN_SEC_WPA2:
420                 rsn = 1;
421                 break;
422         default:
423                 /* WEP or none.  No IE returned */
424                 return 0;
425         }
426
427         switch (be16_to_cpu(scan->security) & GELIC_EURUS_SCAN_SEC_WPA_MASK) {
428         case GELIC_EURUS_SCAN_SEC_WPA_TKIP:
429                 ccmp = 0;
430                 break;
431         case GELIC_EURUS_SCAN_SEC_WPA_AES:
432                 ccmp = 1;
433                 break;
434         default:
435                 if (rsn) {
436                         ccmp = 1;
437                         pr_info("%s: no cipher info. defaulted to CCMP\n",
438                                 __func__);
439                 } else {
440                         ccmp = 0;
441                         pr_info("%s: no cipher info. defaulted to TKIP\n",
442                                 __func__);
443                 }
444         }
445
446         if (rsn)
447                 oui_header = rsn_oui;
448         else
449                 oui_header = wpa_oui;
450
451         /* element id */
452         if (rsn)
453                 *buf++ = WLAN_EID_RSN;
454         else
455                 *buf++ = WLAN_EID_GENERIC;
456
457         /* length filed; set later */
458         buf++;
459
460         /* wpa special header */
461         if (!rsn) {
462                 memcpy(buf, wpa_oui, OUI_LEN);
463                 buf += OUI_LEN;
464                 *buf++ = 0x01;
465         }
466
467         /* version */
468         *buf++ = 0x01; /* version 1.0 */
469         *buf++ = 0x00;
470
471         /* group cipher */
472         memcpy(buf, oui_header, OUI_LEN);
473         buf += OUI_LEN;
474
475         if (ccmp)
476                 *buf++ = 0x04; /* CCMP */
477         else
478                 *buf++ = 0x02; /* TKIP */
479
480         /* pairwise key count always 1 */
481         *buf++ = 0x01;
482         *buf++ = 0x00;
483
484         /* pairwise key suit */
485         memcpy(buf, oui_header, OUI_LEN);
486         buf += OUI_LEN;
487         if (ccmp)
488                 *buf++ = 0x04; /* CCMP */
489         else
490                 *buf++ = 0x02; /* TKIP */
491
492         /* AKM count is 1 */
493         *buf++ = 0x01;
494         *buf++ = 0x00;
495
496         /* AKM suite is assumed as PSK*/
497         memcpy(buf, oui_header, OUI_LEN);
498         buf += OUI_LEN;
499         *buf++ = 0x02; /* PSK */
500
501         /* RSN capabilities is 0 */
502         *buf++ = 0x00;
503         *buf++ = 0x00;
504
505         /* set length field */
506         start[1] = (buf - start - 2);
507
508         pr_debug("%s: ->\n", __func__);
509         return (buf - start);
510 }
511
512 struct ie_item {
513         u8 *data;
514         u8 len;
515 };
516
517 struct ie_info {
518         struct ie_item wpa;
519         struct ie_item rsn;
520 };
521
522 static void gelic_wl_parse_ie(u8 *data, size_t len,
523                               struct ie_info *ie_info)
524 {
525         size_t data_left = len;
526         u8 *pos = data;
527         u8 item_len;
528         u8 item_id;
529
530         pr_debug("%s: data=%p len=%ld \n", __func__,
531                  data, len);
532         memset(ie_info, 0, sizeof(struct ie_info));
533
534         while (2 <= data_left) {
535                 item_id = *pos++;
536                 item_len = *pos++;
537                 data_left -= 2;
538
539                 if (data_left < item_len)
540                         break;
541
542                 switch (item_id) {
543                 case WLAN_EID_GENERIC:
544                         if ((OUI_LEN + 1 <= item_len) &&
545                             !memcmp(pos, wpa_oui, OUI_LEN) &&
546                             pos[OUI_LEN] == 0x01) {
547                                 ie_info->wpa.data = pos - 2;
548                                 ie_info->wpa.len = item_len + 2;
549                         }
550                         break;
551                 case WLAN_EID_RSN:
552                         ie_info->rsn.data = pos - 2;
553                         /* length includes the header */
554                         ie_info->rsn.len = item_len + 2;
555                         break;
556                 default:
557                         pr_debug("%s: ignore %#x,%d\n", __func__,
558                                  item_id, item_len);
559                         break;
560                 }
561                 pos += item_len;
562                 data_left -= item_len;
563         }
564         pr_debug("%s: wpa=%p,%d wpa2=%p,%d\n", __func__,
565                  ie_info->wpa.data, ie_info->wpa.len,
566                  ie_info->rsn.data, ie_info->rsn.len);
567 }
568
569
570 /*
571  * translate the scan informations from hypervisor to a
572  * independent format
573  */
574 static char *gelic_wl_translate_scan(struct net_device *netdev,
575                                      struct iw_request_info *info,
576                                      char *ev,
577                                      char *stop,
578                                      struct gelic_wl_scan_info *network)
579 {
580         struct iw_event iwe;
581         struct gelic_eurus_scan_info *scan = network->hwinfo;
582         char *tmp;
583         u8 rate;
584         unsigned int i, j, len;
585         u8 buf[64]; /* arbitrary size large enough */
586
587         pr_debug("%s: <-\n", __func__);
588
589         /* first entry should be AP's mac address */
590         iwe.cmd = SIOCGIWAP;
591         iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
592         memcpy(iwe.u.ap_addr.sa_data, &scan->bssid[2], ETH_ALEN);
593         ev = iwe_stream_add_event(info, ev, stop, &iwe, IW_EV_ADDR_LEN);
594
595         /* ESSID */
596         iwe.cmd = SIOCGIWESSID;
597         iwe.u.data.flags = 1;
598         iwe.u.data.length = strnlen(scan->essid, 32);
599         ev = iwe_stream_add_point(info, ev, stop, &iwe, scan->essid);
600
601         /* FREQUENCY */
602         iwe.cmd = SIOCGIWFREQ;
603         iwe.u.freq.m = be16_to_cpu(scan->channel);
604         iwe.u.freq.e = 0; /* table value in MHz */
605         iwe.u.freq.i = 0;
606         ev = iwe_stream_add_event(info, ev, stop, &iwe, IW_EV_FREQ_LEN);
607
608         /* RATES */
609         iwe.cmd = SIOCGIWRATE;
610         iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
611         /* to stuff multiple values in one event */
612         tmp = ev + iwe_stream_lcp_len(info);
613         /* put them in ascendant order (older is first) */
614         i = 0;
615         j = 0;
616         pr_debug("%s: rates=%d rate=%d\n", __func__,
617                  network->rate_len, network->rate_ext_len);
618         while (i < network->rate_len) {
619                 if (j < network->rate_ext_len &&
620                     ((scan->ext_rate[j] & 0x7f) < (scan->rate[i] & 0x7f)))
621                     rate = scan->ext_rate[j++] & 0x7f;
622                 else
623                     rate = scan->rate[i++] & 0x7f;
624                 iwe.u.bitrate.value = rate * 500000; /* 500kbps unit */
625                 tmp = iwe_stream_add_value(info, ev, tmp, stop, &iwe,
626                                            IW_EV_PARAM_LEN);
627         }
628         while (j < network->rate_ext_len) {
629                 iwe.u.bitrate.value = (scan->ext_rate[j++] & 0x7f) * 500000;
630                 tmp = iwe_stream_add_value(info, ev, tmp, stop, &iwe,
631                                            IW_EV_PARAM_LEN);
632         }
633         /* Check if we added any rate */
634         if (iwe_stream_lcp_len(info) < (tmp - ev))
635                 ev = tmp;
636
637         /* ENCODE */
638         iwe.cmd = SIOCGIWENCODE;
639         if (be16_to_cpu(scan->capability) & WLAN_CAPABILITY_PRIVACY)
640                 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
641         else
642                 iwe.u.data.flags = IW_ENCODE_DISABLED;
643         iwe.u.data.length = 0;
644         ev = iwe_stream_add_point(info, ev, stop, &iwe, scan->essid);
645
646         /* MODE */
647         iwe.cmd = SIOCGIWMODE;
648         if (be16_to_cpu(scan->capability) &
649             (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)) {
650                 if (be16_to_cpu(scan->capability) & WLAN_CAPABILITY_ESS)
651                         iwe.u.mode = IW_MODE_MASTER;
652                 else
653                         iwe.u.mode = IW_MODE_ADHOC;
654                 ev = iwe_stream_add_event(info, ev, stop, &iwe, IW_EV_UINT_LEN);
655         }
656
657         /* QUAL */
658         iwe.cmd = IWEVQUAL;
659         iwe.u.qual.updated  = IW_QUAL_ALL_UPDATED |
660                         IW_QUAL_QUAL_INVALID | IW_QUAL_NOISE_INVALID;
661         iwe.u.qual.level = be16_to_cpu(scan->rssi);
662         iwe.u.qual.qual = be16_to_cpu(scan->rssi);
663         iwe.u.qual.noise = 0;
664         ev  = iwe_stream_add_event(info, ev, stop, &iwe, IW_EV_QUAL_LEN);
665
666         /* RSN */
667         memset(&iwe, 0, sizeof(iwe));
668         if (be16_to_cpu(scan->size) <= sizeof(*scan)) {
669                 /* If wpa[2] capable station, synthesize IE and put it */
670                 len = gelic_wl_synthesize_ie(buf, scan);
671                 if (len) {
672                         iwe.cmd = IWEVGENIE;
673                         iwe.u.data.length = len;
674                         ev = iwe_stream_add_point(info, ev, stop, &iwe, buf);
675                 }
676         } else {
677                 /* this scan info has IE data */
678                 struct ie_info ie_info;
679                 size_t data_len;
680
681                 data_len = be16_to_cpu(scan->size) - sizeof(*scan);
682
683                 gelic_wl_parse_ie(scan->elements, data_len, &ie_info);
684
685                 if (ie_info.wpa.len && (ie_info.wpa.len <= sizeof(buf))) {
686                         memcpy(buf, ie_info.wpa.data, ie_info.wpa.len);
687                         iwe.cmd = IWEVGENIE;
688                         iwe.u.data.length = ie_info.wpa.len;
689                         ev = iwe_stream_add_point(info, ev, stop, &iwe, buf);
690                 }
691
692                 if (ie_info.rsn.len && (ie_info.rsn.len <= sizeof(buf))) {
693                         memset(&iwe, 0, sizeof(iwe));
694                         memcpy(buf, ie_info.rsn.data, ie_info.rsn.len);
695                         iwe.cmd = IWEVGENIE;
696                         iwe.u.data.length = ie_info.rsn.len;
697                         ev = iwe_stream_add_point(info, ev, stop, &iwe, buf);
698                 }
699         }
700
701         pr_debug("%s: ->\n", __func__);
702         return ev;
703 }
704
705
706 static int gelic_wl_get_scan(struct net_device *netdev,
707                              struct iw_request_info *info,
708                              union iwreq_data *wrqu, char *extra)
709 {
710         struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
711         struct gelic_wl_scan_info *scan_info;
712         char *ev = extra;
713         char *stop = ev + wrqu->data.length;
714         int ret = 0;
715         unsigned long this_time = jiffies;
716
717         pr_debug("%s: <-\n", __func__);
718         if (mutex_lock_interruptible(&wl->scan_lock))
719                 return -EAGAIN;
720
721         switch (wl->scan_stat) {
722         case GELIC_WL_SCAN_STAT_SCANNING:
723                 /* If a scan in progress, caller should call me again */
724                 ret = -EAGAIN;
725                 goto out;
726                 break;
727
728         case GELIC_WL_SCAN_STAT_INIT:
729                 /* last scan request failed or never issued */
730                 ret = -ENODEV;
731                 goto out;
732                 break;
733         case GELIC_WL_SCAN_STAT_GOT_LIST:
734                 /* ok, use current list */
735                 break;
736         }
737
738         list_for_each_entry(scan_info, &wl->network_list, list) {
739                 if (wl->scan_age == 0 ||
740                     time_after(scan_info->last_scanned + wl->scan_age,
741                                this_time))
742                         ev = gelic_wl_translate_scan(netdev, info,
743                                                      ev, stop,
744                                                      scan_info);
745                 else
746                         pr_debug("%s:entry too old\n", __func__);
747
748                 if (stop - ev <= IW_EV_ADDR_LEN) {
749                         ret = -E2BIG;
750                         goto out;
751                 }
752         }
753
754         wrqu->data.length = ev - extra;
755         wrqu->data.flags = 0;
756 out:
757         mutex_unlock(&wl->scan_lock);
758         pr_debug("%s: -> %d %d\n", __func__, ret, wrqu->data.length);
759         return ret;
760 }
761
762 #ifdef DEBUG
763 static void scan_list_dump(struct gelic_wl_info *wl)
764 {
765         struct gelic_wl_scan_info *scan_info;
766         int i;
767
768         i = 0;
769         list_for_each_entry(scan_info, &wl->network_list, list) {
770                 pr_debug("%s: item %d\n", __func__, i++);
771                 pr_debug("valid=%d eurusindex=%d last=%lx\n",
772                          scan_info->valid, scan_info->eurus_index,
773                          scan_info->last_scanned);
774                 pr_debug("r_len=%d r_ext_len=%d essid_len=%d\n",
775                          scan_info->rate_len, scan_info->rate_ext_len,
776                          scan_info->essid_len);
777                 /* -- */
778                 pr_debug("bssid=%pM\n", &scan_info->hwinfo->bssid[2]);
779                 pr_debug("essid=%s\n", scan_info->hwinfo->essid);
780         }
781 }
782 #endif
783
784 static int gelic_wl_set_auth(struct net_device *netdev,
785                              struct iw_request_info *info,
786                              union iwreq_data *data, char *extra)
787 {
788         struct iw_param *param = &data->param;
789         struct gelic_wl_info *wl = port_wl(netdev_port(netdev));
790         unsigned long irqflag;
791         int ret = 0;
792
793         pr_debug("%s: <- %d\n", __func__, param->flags & IW_AUTH_INDEX);
794         spin_lock_irqsave(&wl->lock, irqflag);
795         switch (param->flags & IW_AUTH_INDEX) {
796         case IW_AUTH_WPA_VERSION:
797                 if (param->value & IW_AUTH_WPA_VERSION_DISABLED) {
798                         pr_debug("%s: NO WPA selected\n", __func__);
799                         wl->wpa_level = GELIC_WL_WPA_LEVEL_NONE;
800                         wl->group_cipher_method = GELIC_WL_CIPHER_WEP;
801                         wl->pairwise_cipher_method = GELIC_WL_CIPHER_WEP;
802                 }
803                 if (param->value & IW_AUTH_WPA_VERSION_WPA) {
804                         pr_debug("%s: WPA version 1 selected\n", __func__);
805                         wl->wpa_level = GELIC_WL_WPA_LEVEL_WPA;
806                         wl->group_cipher_method = GELIC_WL_CIPHER_TKIP;
807                         wl->pairwise_cipher_method = GELIC_WL_CIPHER_TKIP;
808                         wl->auth_method = GELIC_EURUS_AUTH_OPEN;
809                 }
810                 if (param->value & IW_AUTH_WPA_VERSION_WPA2) {
811                         /*
812                          * As the hypervisor may not tell the cipher
813                          * information of the AP if it is WPA2,
814                          * you will not decide suitable cipher from
815                          * its beacon.
816                          * You should have knowledge about the AP's
817                          * cipher infomation in other method prior to
818                          * the association.
819                          */
820                         if (!precise_ie())
821                                 pr_info("%s: WPA2 may not work\n", __func__);
822                         if (wpa2_capable()) {
823                                 wl->wpa_level = GELIC_WL_WPA_LEVEL_WPA2;
824                                 wl->group_cipher_method = GELIC_WL_CIPHER_AES;
825                                 wl->pairwise_cipher_method =
826                                         GELIC_WL_CIPHER_AES;
827                                 wl->auth_method = GELIC_EURUS_AUTH_OPEN;
828                         } else
829                                 ret = -EINVAL;
830                 }
831                 break;
832
833         case IW_AUTH_CIPHER_PAIRWISE:
834                 if (param->value &
835                     (IW_AUTH_CIPHER_WEP104 | IW_AUTH_CIPHER_WEP40)) {
836                         pr_debug("%s: WEP selected\n", __func__);
837                         wl->pairwise_cipher_method = GELIC_WL_CIPHER_WEP;
838                 }
839                 if (param->value & IW_AUTH_CIPHER_TKIP) {
840                         pr_debug("%s: TKIP selected\n", __func__);
841                         wl->pairwise_cipher_method = GELIC_WL_CIPHER_TKIP;
842                 }
843                 if (param->value & IW_AUTH_CIPHER_CCMP) {
844                         pr_debug("%s: CCMP selected\n", __func__);
845                         wl->pairwise_cipher_method = GELIC_WL_CIPHER_AES;
846                 }
847                 if (param->value & IW_AUTH_CIPHER_NONE) {
848                         pr_debug("%s: no auth selected\n", __func__);
849                         wl->pairwise_cipher_method = GELIC_WL_CIPHER_NONE;
850                 }
851                 break;
852         case IW_AUTH_CIPHER_GROUP:
853                 if (param->value &
854                     (IW_AUTH_CIPHER_WEP104 | IW_AUTH_CIPHER_WEP40)) {
855                         pr_debug("%s: WEP selected\n", __func__);
856                         wl->group_cipher_method = GELIC_WL_CIPHER_WEP;
857                 }
858                 if (param->value & IW_AUTH_CIPHER_TKIP) {
859                         pr_debug("%s: TKIP selected\n", __func__);
860                         wl->group_cipher_method = GELIC_WL_CIPHER_TKIP;
861                 }
862                 if (param->value & IW_AUTH_CIPHER_CCMP) {
863                         pr_debug("%s: CCMP selected\n", __func__);
864                         wl->group_cipher_method = GELIC_WL_CIPHER_AES;
865                 }
866                 if (param->value & IW_AUTH_CIPHER_NONE) {
867                         pr_debug("%s: no auth selected\n", __func__);
868                         wl->group_cipher_method = GELIC_WL_CIPHER_NONE;
869                 }
870                 break;
871         case IW_AUTH_80211_AUTH_ALG:
872                 if (param->value & IW_AUTH_ALG_SHARED_KEY) {
873                         pr_debug("%s: shared key specified\n", __func__);
874                         wl->auth_method = GELIC_EURUS_AUTH_SHARED;
875                 } else if (param->value & IW_AUTH_ALG_OPEN_SYSTEM) {
876                         pr_debug("%s: open system specified\n", __func__);
877                         wl->auth_method = GELIC_EURUS_AUTH_OPEN;
878                 } else
879                         ret = -EINVAL;
880                 break;
881
882         case IW_AUTH_WPA_ENABLED:
883                 if (param->value) {
884                         pr_debug("%s: WPA enabled\n", __func__);
885                         wl->wpa_level = GELIC_WL_WPA_LEVEL_WPA;
886                 } else {
887                         pr_debug("%s: WPA disabled\n", __func__);
888                         wl->wpa_level = GELIC_WL_WPA_LEVEL_NONE;
889                 }
890                 break;
891
892         case IW_AUTH_KEY_MGMT:
893                 if (param->value & IW_AUTH_KEY_MGMT_PSK)
894                         break;
895                 /* intentionally fall through */
896         default:
897                 ret = -EOPNOTSUPP;
898                 break;
899         };
900
901         if (!ret)
902                 set_bit(GELIC_WL_STAT_CONFIGURED, &wl->stat);
903
904         spin_unlock_irqrestore(&wl->lock, irqflag);
905         pr_debug("%s: -> %d\n", __func__, ret);
906         return ret;
907 }
908
909 static int gelic_wl_get_auth(struct net_device *netdev,
910                              struct iw_request_info *info,
911                              union iwreq_data *iwreq, char *extra)
912 {
913         struct iw_param *param = &iwreq->param;
914         struct gelic_wl_info *wl = port_wl(netdev_port(netdev));
915         unsigned long irqflag;
916         int ret = 0;
917
918         pr_debug("%s: <- %d\n", __func__, param->flags & IW_AUTH_INDEX);
919         spin_lock_irqsave(&wl->lock, irqflag);
920         switch (param->flags & IW_AUTH_INDEX) {
921         case IW_AUTH_WPA_VERSION:
922                 switch (wl->wpa_level) {
923                 case GELIC_WL_WPA_LEVEL_WPA:
924                         param->value |= IW_AUTH_WPA_VERSION_WPA;
925                         break;
926                 case GELIC_WL_WPA_LEVEL_WPA2:
927                         param->value |= IW_AUTH_WPA_VERSION_WPA2;
928                         break;
929                 default:
930                         param->value |= IW_AUTH_WPA_VERSION_DISABLED;
931                 }
932                 break;
933
934         case IW_AUTH_80211_AUTH_ALG:
935                 if (wl->auth_method == GELIC_EURUS_AUTH_SHARED)
936                         param->value = IW_AUTH_ALG_SHARED_KEY;
937                 else if (wl->auth_method == GELIC_EURUS_AUTH_OPEN)
938                         param->value = IW_AUTH_ALG_OPEN_SYSTEM;
939                 break;
940
941         case IW_AUTH_WPA_ENABLED:
942                 switch (wl->wpa_level) {
943                 case GELIC_WL_WPA_LEVEL_WPA:
944                 case GELIC_WL_WPA_LEVEL_WPA2:
945                         param->value = 1;
946                         break;
947                 default:
948                         param->value = 0;
949                         break;
950                 }
951                 break;
952         default:
953                 ret = -EOPNOTSUPP;
954         }
955
956         spin_unlock_irqrestore(&wl->lock, irqflag);
957         pr_debug("%s: -> %d\n", __func__, ret);
958         return ret;
959 }
960
961 /* SIOC{S,G}IWESSID */
962 static int gelic_wl_set_essid(struct net_device *netdev,
963                               struct iw_request_info *info,
964                               union iwreq_data *data, char *extra)
965 {
966         struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
967         unsigned long irqflag;
968
969         pr_debug("%s: <- l=%d f=%d\n", __func__,
970                  data->essid.length, data->essid.flags);
971         if (IW_ESSID_MAX_SIZE < data->essid.length)
972                 return -EINVAL;
973
974         spin_lock_irqsave(&wl->lock, irqflag);
975         if (data->essid.flags) {
976                 wl->essid_len = data->essid.length;
977                 memcpy(wl->essid, extra, wl->essid_len);
978                 pr_debug("%s: essid = '%s'\n", __func__, extra);
979                 set_bit(GELIC_WL_STAT_ESSID_SET, &wl->stat);
980         } else {
981                 pr_debug("%s: ESSID any \n", __func__);
982                 clear_bit(GELIC_WL_STAT_ESSID_SET, &wl->stat);
983         }
984         set_bit(GELIC_WL_STAT_CONFIGURED, &wl->stat);
985         spin_unlock_irqrestore(&wl->lock, irqflag);
986
987
988         gelic_wl_try_associate(netdev); /* FIXME */
989         pr_debug("%s: -> \n", __func__);
990         return 0;
991 }
992
993 static int gelic_wl_get_essid(struct net_device *netdev,
994                               struct iw_request_info *info,
995                               union iwreq_data *data, char *extra)
996 {
997         struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
998         unsigned long irqflag;
999
1000         pr_debug("%s: <- \n", __func__);
1001         mutex_lock(&wl->assoc_stat_lock);
1002         spin_lock_irqsave(&wl->lock, irqflag);
1003         if (test_bit(GELIC_WL_STAT_ESSID_SET, &wl->stat) ||
1004             wl->assoc_stat == GELIC_WL_ASSOC_STAT_ASSOCIATED) {
1005                 memcpy(extra, wl->essid, wl->essid_len);
1006                 data->essid.length = wl->essid_len;
1007                 data->essid.flags = 1;
1008         } else
1009                 data->essid.flags = 0;
1010
1011         mutex_unlock(&wl->assoc_stat_lock);
1012         spin_unlock_irqrestore(&wl->lock, irqflag);
1013         pr_debug("%s: -> len=%d \n", __func__, data->essid.length);
1014
1015         return 0;
1016 }
1017
1018 /* SIO{S,G}IWENCODE */
1019 static int gelic_wl_set_encode(struct net_device *netdev,
1020                                struct iw_request_info *info,
1021                                union iwreq_data *data, char *extra)
1022 {
1023         struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
1024         struct iw_point *enc = &data->encoding;
1025         __u16 flags;
1026         unsigned long irqflag;
1027         int key_index, index_specified;
1028         int ret = 0;
1029
1030         pr_debug("%s: <- \n", __func__);
1031         flags = enc->flags & IW_ENCODE_FLAGS;
1032         key_index = enc->flags & IW_ENCODE_INDEX;
1033
1034         pr_debug("%s: key_index = %d\n", __func__, key_index);
1035         pr_debug("%s: key_len = %d\n", __func__, enc->length);
1036         pr_debug("%s: flag=%x\n", __func__, enc->flags & IW_ENCODE_FLAGS);
1037
1038         if (GELIC_WEP_KEYS < key_index)
1039                 return -EINVAL;
1040
1041         spin_lock_irqsave(&wl->lock, irqflag);
1042         if (key_index) {
1043                 index_specified = 1;
1044                 key_index--;
1045         } else {
1046                 index_specified = 0;
1047                 key_index = wl->current_key;
1048         }
1049
1050         if (flags & IW_ENCODE_NOKEY) {
1051                 /* if just IW_ENCODE_NOKEY, change current key index */
1052                 if (!flags && index_specified) {
1053                         wl->current_key = key_index;
1054                         goto done;
1055                 }
1056
1057                 if (flags & IW_ENCODE_DISABLED) {
1058                         if (!index_specified) {
1059                                 /* disable encryption */
1060                                 wl->group_cipher_method = GELIC_WL_CIPHER_NONE;
1061                                 wl->pairwise_cipher_method =
1062                                         GELIC_WL_CIPHER_NONE;
1063                                 /* invalidate all key */
1064                                 wl->key_enabled = 0;
1065                         } else
1066                                 clear_bit(key_index, &wl->key_enabled);
1067                 }
1068
1069                 if (flags & IW_ENCODE_OPEN)
1070                         wl->auth_method = GELIC_EURUS_AUTH_OPEN;
1071                 if (flags & IW_ENCODE_RESTRICTED) {
1072                         pr_info("%s: shared key mode enabled\n", __func__);
1073                         wl->auth_method = GELIC_EURUS_AUTH_SHARED;
1074                 }
1075         } else {
1076                 if (IW_ENCODING_TOKEN_MAX < enc->length) {
1077                         ret = -EINVAL;
1078                         goto done;
1079                 }
1080                 wl->key_len[key_index] = enc->length;
1081                 memcpy(wl->key[key_index], extra, enc->length);
1082                 set_bit(key_index, &wl->key_enabled);
1083                 wl->pairwise_cipher_method = GELIC_WL_CIPHER_WEP;
1084                 wl->group_cipher_method = GELIC_WL_CIPHER_WEP;
1085         }
1086         set_bit(GELIC_WL_STAT_CONFIGURED, &wl->stat);
1087 done:
1088         spin_unlock_irqrestore(&wl->lock, irqflag);
1089         pr_debug("%s: -> \n", __func__);
1090         return ret;
1091 }
1092
1093 static int gelic_wl_get_encode(struct net_device *netdev,
1094                                struct iw_request_info *info,
1095                                union iwreq_data *data, char *extra)
1096 {
1097         struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
1098         struct iw_point *enc = &data->encoding;
1099         unsigned long irqflag;
1100         unsigned int key_index, index_specified;
1101         int ret = 0;
1102
1103         pr_debug("%s: <- \n", __func__);
1104         key_index = enc->flags & IW_ENCODE_INDEX;
1105         pr_debug("%s: flag=%#x point=%p len=%d extra=%p\n", __func__,
1106                  enc->flags, enc->pointer, enc->length, extra);
1107         if (GELIC_WEP_KEYS < key_index)
1108                 return -EINVAL;
1109
1110         spin_lock_irqsave(&wl->lock, irqflag);
1111         if (key_index) {
1112                 index_specified = 1;
1113                 key_index--;
1114         } else {
1115                 index_specified = 0;
1116                 key_index = wl->current_key;
1117         }
1118
1119         if (wl->group_cipher_method == GELIC_WL_CIPHER_WEP) {
1120                 switch (wl->auth_method) {
1121                 case GELIC_EURUS_AUTH_OPEN:
1122                         enc->flags = IW_ENCODE_OPEN;
1123                         break;
1124                 case GELIC_EURUS_AUTH_SHARED:
1125                         enc->flags = IW_ENCODE_RESTRICTED;
1126                         break;
1127                 }
1128         } else
1129                 enc->flags = IW_ENCODE_DISABLED;
1130
1131         if (test_bit(key_index, &wl->key_enabled)) {
1132                 if (enc->length < wl->key_len[key_index]) {
1133                         ret = -EINVAL;
1134                         goto done;
1135                 }
1136                 enc->length = wl->key_len[key_index];
1137                 memcpy(extra, wl->key[key_index], wl->key_len[key_index]);
1138         } else {
1139                 enc->length = 0;
1140                 enc->flags |= IW_ENCODE_NOKEY;
1141         }
1142         enc->flags |= key_index + 1;
1143         pr_debug("%s: -> flag=%x len=%d\n", __func__,
1144                  enc->flags, enc->length);
1145
1146 done:
1147         spin_unlock_irqrestore(&wl->lock, irqflag);
1148         return ret;
1149 }
1150
1151 /* SIOC{S,G}IWAP */
1152 static int gelic_wl_set_ap(struct net_device *netdev,
1153                            struct iw_request_info *info,
1154                            union iwreq_data *data, char *extra)
1155 {
1156         struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
1157         unsigned long irqflag;
1158
1159         pr_debug("%s: <-\n", __func__);
1160         if (data->ap_addr.sa_family != ARPHRD_ETHER)
1161                 return -EINVAL;
1162
1163         spin_lock_irqsave(&wl->lock, irqflag);
1164         if (is_valid_ether_addr(data->ap_addr.sa_data)) {
1165                 memcpy(wl->bssid, data->ap_addr.sa_data,
1166                        ETH_ALEN);
1167                 set_bit(GELIC_WL_STAT_BSSID_SET, &wl->stat);
1168                 set_bit(GELIC_WL_STAT_CONFIGURED, &wl->stat);
1169                 pr_debug("%s: bss=%pM\n", __func__, wl->bssid);
1170         } else {
1171                 pr_debug("%s: clear bssid\n", __func__);
1172                 clear_bit(GELIC_WL_STAT_BSSID_SET, &wl->stat);
1173                 memset(wl->bssid, 0, ETH_ALEN);
1174         }
1175         spin_unlock_irqrestore(&wl->lock, irqflag);
1176         pr_debug("%s: ->\n", __func__);
1177         return 0;
1178 }
1179
1180 static int gelic_wl_get_ap(struct net_device *netdev,
1181                            struct iw_request_info *info,
1182                            union iwreq_data *data, char *extra)
1183 {
1184         struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
1185         unsigned long irqflag;
1186
1187         pr_debug("%s: <-\n", __func__);
1188         mutex_lock(&wl->assoc_stat_lock);
1189         spin_lock_irqsave(&wl->lock, irqflag);
1190         if (wl->assoc_stat == GELIC_WL_ASSOC_STAT_ASSOCIATED) {
1191                 data->ap_addr.sa_family = ARPHRD_ETHER;
1192                 memcpy(data->ap_addr.sa_data, wl->active_bssid,
1193                        ETH_ALEN);
1194         } else
1195                 memset(data->ap_addr.sa_data, 0, ETH_ALEN);
1196
1197         spin_unlock_irqrestore(&wl->lock, irqflag);
1198         mutex_unlock(&wl->assoc_stat_lock);
1199         pr_debug("%s: ->\n", __func__);
1200         return 0;
1201 }
1202
1203 /* SIOC{S,G}IWENCODEEXT */
1204 static int gelic_wl_set_encodeext(struct net_device *netdev,
1205                                   struct iw_request_info *info,
1206                                   union iwreq_data *data, char *extra)
1207 {
1208         struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
1209         struct iw_point *enc = &data->encoding;
1210         struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
1211         __u16 alg;
1212         __u16 flags;
1213         unsigned long irqflag;
1214         int key_index;
1215         int ret = 0;
1216
1217         pr_debug("%s: <- \n", __func__);
1218         flags = enc->flags & IW_ENCODE_FLAGS;
1219         alg = ext->alg;
1220         key_index = enc->flags & IW_ENCODE_INDEX;
1221
1222         pr_debug("%s: key_index = %d\n", __func__, key_index);
1223         pr_debug("%s: key_len = %d\n", __func__, enc->length);
1224         pr_debug("%s: flag=%x\n", __func__, enc->flags & IW_ENCODE_FLAGS);
1225         pr_debug("%s: ext_flag=%x\n", __func__, ext->ext_flags);
1226         pr_debug("%s: ext_key_len=%x\n", __func__, ext->key_len);
1227
1228         if (GELIC_WEP_KEYS < key_index)
1229                 return -EINVAL;
1230
1231         spin_lock_irqsave(&wl->lock, irqflag);
1232         if (key_index)
1233                 key_index--;
1234         else
1235                 key_index = wl->current_key;
1236
1237         if (!enc->length && (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY)) {
1238                 /* reques to change default key index */
1239                 pr_debug("%s: request to change default key to %d\n",
1240                          __func__, key_index);
1241                 wl->current_key = key_index;
1242                 goto done;
1243         }
1244
1245         if (alg == IW_ENCODE_ALG_NONE || (flags & IW_ENCODE_DISABLED)) {
1246                 pr_debug("%s: alg disabled\n", __func__);
1247                 wl->wpa_level = GELIC_WL_WPA_LEVEL_NONE;
1248                 wl->group_cipher_method = GELIC_WL_CIPHER_NONE;
1249                 wl->pairwise_cipher_method = GELIC_WL_CIPHER_NONE;
1250                 wl->auth_method = GELIC_EURUS_AUTH_OPEN; /* should be open */
1251         } else if (alg == IW_ENCODE_ALG_WEP) {
1252                 pr_debug("%s: WEP requested\n", __func__);
1253                 if (flags & IW_ENCODE_OPEN) {
1254                         pr_debug("%s: open key mode\n", __func__);
1255                         wl->auth_method = GELIC_EURUS_AUTH_OPEN;
1256                 }
1257                 if (flags & IW_ENCODE_RESTRICTED) {
1258                         pr_debug("%s: shared key mode\n", __func__);
1259                         wl->auth_method = GELIC_EURUS_AUTH_SHARED;
1260                 }
1261                 if (IW_ENCODING_TOKEN_MAX < ext->key_len) {
1262                         pr_info("%s: key is too long %d\n", __func__,
1263                                 ext->key_len);
1264                         ret = -EINVAL;
1265                         goto done;
1266                 }
1267                 /* OK, update the key */
1268                 wl->key_len[key_index] = ext->key_len;
1269                 memset(wl->key[key_index], 0, IW_ENCODING_TOKEN_MAX);
1270                 memcpy(wl->key[key_index], ext->key, ext->key_len);
1271                 set_bit(key_index, &wl->key_enabled);
1272                 /* remember wep info changed */
1273                 set_bit(GELIC_WL_STAT_CONFIGURED, &wl->stat);
1274         } else if (alg == IW_ENCODE_ALG_PMK) {
1275                 if (ext->key_len != WPA_PSK_LEN) {
1276                         pr_err("%s: PSK length wrong %d\n", __func__,
1277                                ext->key_len);
1278                         ret = -EINVAL;
1279                         goto done;
1280                 }
1281                 memset(wl->psk, 0, sizeof(wl->psk));
1282                 memcpy(wl->psk, ext->key, ext->key_len);
1283                 wl->psk_len = ext->key_len;
1284                 wl->psk_type = GELIC_EURUS_WPA_PSK_BIN;
1285                 /* remember PSK configured */
1286                 set_bit(GELIC_WL_STAT_WPA_PSK_SET, &wl->stat);
1287         }
1288 done:
1289         spin_unlock_irqrestore(&wl->lock, irqflag);
1290         pr_debug("%s: -> \n", __func__);
1291         return ret;
1292 }
1293
1294 static int gelic_wl_get_encodeext(struct net_device *netdev,
1295                                   struct iw_request_info *info,
1296                                   union iwreq_data *data, char *extra)
1297 {
1298         struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
1299         struct iw_point *enc = &data->encoding;
1300         struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
1301         unsigned long irqflag;
1302         int key_index;
1303         int ret = 0;
1304         int max_key_len;
1305
1306         pr_debug("%s: <- \n", __func__);
1307
1308         max_key_len = enc->length - sizeof(struct iw_encode_ext);
1309         if (max_key_len < 0)
1310                 return -EINVAL;
1311         key_index = enc->flags & IW_ENCODE_INDEX;
1312
1313         pr_debug("%s: key_index = %d\n", __func__, key_index);
1314         pr_debug("%s: key_len = %d\n", __func__, enc->length);
1315         pr_debug("%s: flag=%x\n", __func__, enc->flags & IW_ENCODE_FLAGS);
1316
1317         if (GELIC_WEP_KEYS < key_index)
1318                 return -EINVAL;
1319
1320         spin_lock_irqsave(&wl->lock, irqflag);
1321         if (key_index)
1322                 key_index--;
1323         else
1324                 key_index = wl->current_key;
1325
1326         memset(ext, 0, sizeof(struct iw_encode_ext));
1327         switch (wl->group_cipher_method) {
1328         case GELIC_WL_CIPHER_WEP:
1329                 ext->alg = IW_ENCODE_ALG_WEP;
1330                 enc->flags |= IW_ENCODE_ENABLED;
1331                 break;
1332         case GELIC_WL_CIPHER_TKIP:
1333                 ext->alg = IW_ENCODE_ALG_TKIP;
1334                 enc->flags |= IW_ENCODE_ENABLED;
1335                 break;
1336         case GELIC_WL_CIPHER_AES:
1337                 ext->alg = IW_ENCODE_ALG_CCMP;
1338                 enc->flags |= IW_ENCODE_ENABLED;
1339                 break;
1340         case GELIC_WL_CIPHER_NONE:
1341         default:
1342                 ext->alg = IW_ENCODE_ALG_NONE;
1343                 enc->flags |= IW_ENCODE_NOKEY;
1344                 break;
1345         }
1346
1347         if (!(enc->flags & IW_ENCODE_NOKEY)) {
1348                 if (max_key_len < wl->key_len[key_index]) {
1349                         ret = -E2BIG;
1350                         goto out;
1351                 }
1352                 if (test_bit(key_index, &wl->key_enabled))
1353                         memcpy(ext->key, wl->key[key_index],
1354                                wl->key_len[key_index]);
1355                 else
1356                         pr_debug("%s: disabled key requested ix=%d\n",
1357                                  __func__, key_index);
1358         }
1359 out:
1360         spin_unlock_irqrestore(&wl->lock, irqflag);
1361         pr_debug("%s: -> \n", __func__);
1362         return ret;
1363 }
1364 /* SIOC{S,G}IWMODE */
1365 static int gelic_wl_set_mode(struct net_device *netdev,
1366                              struct iw_request_info *info,
1367                              union iwreq_data *data, char *extra)
1368 {
1369         __u32 mode = data->mode;
1370         int ret;
1371
1372         pr_debug("%s: <- \n", __func__);
1373         if (mode == IW_MODE_INFRA)
1374                 ret = 0;
1375         else
1376                 ret = -EOPNOTSUPP;
1377         pr_debug("%s: -> %d\n", __func__, ret);
1378         return ret;
1379 }
1380
1381 static int gelic_wl_get_mode(struct net_device *netdev,
1382                              struct iw_request_info *info,
1383                              union iwreq_data *data, char *extra)
1384 {
1385         __u32 *mode = &data->mode;
1386         pr_debug("%s: <- \n", __func__);
1387         *mode = IW_MODE_INFRA;
1388         pr_debug("%s: ->\n", __func__);
1389         return 0;
1390 }
1391
1392 #ifdef CONFIG_GELIC_WIRELESS_OLD_PSK_INTERFACE
1393 /* SIOCIWFIRSTPRIV */
1394 static int hex2bin(u8 *str, u8 *bin, unsigned int len)
1395 {
1396         unsigned int i;
1397         static unsigned char *hex = "0123456789ABCDEF";
1398         unsigned char *p, *q;
1399         u8 tmp;
1400
1401         if (len != WPA_PSK_LEN * 2)
1402                 return -EINVAL;
1403
1404         for (i = 0; i < WPA_PSK_LEN * 2; i += 2) {
1405                 p = strchr(hex, toupper(str[i]));
1406                 q = strchr(hex, toupper(str[i + 1]));
1407                 if (!p || !q) {
1408                         pr_info("%s: unconvertible PSK digit=%d\n",
1409                                 __func__, i);
1410                         return -EINVAL;
1411                 }
1412                 tmp = ((p - hex) << 4) + (q - hex);
1413                 *bin++ = tmp;
1414         }
1415         return 0;
1416 };
1417
1418 static int gelic_wl_priv_set_psk(struct net_device *net_dev,
1419                                  struct iw_request_info *info,
1420                                  union iwreq_data *data, char *extra)
1421 {
1422         struct gelic_wl_info *wl = port_wl(netdev_priv(net_dev));
1423         unsigned int len;
1424         unsigned long irqflag;
1425         int ret = 0;
1426
1427         pr_debug("%s:<- len=%d\n", __func__, data->data.length);
1428         len = data->data.length - 1;
1429         if (len <= 2)
1430                 return -EINVAL;
1431
1432         spin_lock_irqsave(&wl->lock, irqflag);
1433         if (extra[0] == '"' && extra[len - 1] == '"') {
1434                 pr_debug("%s: passphrase mode\n", __func__);
1435                 /* pass phrase */
1436                 if (GELIC_WL_EURUS_PSK_MAX_LEN < (len - 2)) {
1437                         pr_info("%s: passphrase too long\n", __func__);
1438                         ret = -E2BIG;
1439                         goto out;
1440                 }
1441                 memset(wl->psk, 0, sizeof(wl->psk));
1442                 wl->psk_len = len - 2;
1443                 memcpy(wl->psk, &(extra[1]), wl->psk_len);
1444                 wl->psk_type = GELIC_EURUS_WPA_PSK_PASSPHRASE;
1445         } else {
1446                 ret = hex2bin(extra, wl->psk, len);
1447                 if (ret)
1448                         goto out;
1449                 wl->psk_len = WPA_PSK_LEN;
1450                 wl->psk_type = GELIC_EURUS_WPA_PSK_BIN;
1451         }
1452         set_bit(GELIC_WL_STAT_WPA_PSK_SET, &wl->stat);
1453 out:
1454         spin_unlock_irqrestore(&wl->lock, irqflag);
1455         pr_debug("%s:->\n", __func__);
1456         return ret;
1457 }
1458
1459 static int gelic_wl_priv_get_psk(struct net_device *net_dev,
1460                                  struct iw_request_info *info,
1461                                  union iwreq_data *data, char *extra)
1462 {
1463         struct gelic_wl_info *wl = port_wl(netdev_priv(net_dev));
1464         char *p;
1465         unsigned long irqflag;
1466         unsigned int i;
1467
1468         pr_debug("%s:<-\n", __func__);
1469         if (!capable(CAP_NET_ADMIN))
1470                 return -EPERM;
1471
1472         spin_lock_irqsave(&wl->lock, irqflag);
1473         p = extra;
1474         if (test_bit(GELIC_WL_STAT_WPA_PSK_SET, &wl->stat)) {
1475                 if (wl->psk_type == GELIC_EURUS_WPA_PSK_BIN) {
1476                         for (i = 0; i < wl->psk_len; i++) {
1477                                 sprintf(p, "%02xu", wl->psk[i]);
1478                                 p += 2;
1479                         }
1480                         *p = '\0';
1481                         data->data.length = wl->psk_len * 2;
1482                 } else {
1483                         *p++ = '"';
1484                         memcpy(p, wl->psk, wl->psk_len);
1485                         p += wl->psk_len;
1486                         *p++ = '"';
1487                         *p = '\0';
1488                         data->data.length = wl->psk_len + 2;
1489                 }
1490         } else
1491                 /* no psk set */
1492                 data->data.length = 0;
1493         spin_unlock_irqrestore(&wl->lock, irqflag);
1494         pr_debug("%s:-> %d\n", __func__, data->data.length);
1495         return 0;
1496 }
1497 #endif
1498
1499 /* SIOCGIWNICKN */
1500 static int gelic_wl_get_nick(struct net_device *net_dev,
1501                                   struct iw_request_info *info,
1502                                   union iwreq_data *data, char *extra)
1503 {
1504         strcpy(extra, "gelic_wl");
1505         data->data.length = strlen(extra);
1506         data->data.flags = 1;
1507         return 0;
1508 }
1509
1510
1511 /* --- */
1512
1513 static struct iw_statistics *gelic_wl_get_wireless_stats(
1514         struct net_device *netdev)
1515 {
1516
1517         struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
1518         struct gelic_eurus_cmd *cmd;
1519         struct iw_statistics *is;
1520         struct gelic_eurus_rssi_info *rssi;
1521         void *buf;
1522
1523         pr_debug("%s: <-\n", __func__);
1524
1525         buf = (void *)__get_free_page(GFP_KERNEL);
1526         if (!buf)
1527                 return NULL;
1528
1529         is = &wl->iwstat;
1530         memset(is, 0, sizeof(*is));
1531         cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_GET_RSSI_CFG,
1532                                    buf, sizeof(*rssi));
1533         if (cmd && !cmd->status && !cmd->cmd_status) {
1534                 rssi = buf;
1535                 is->qual.level = be16_to_cpu(rssi->rssi);
1536                 is->qual.updated = IW_QUAL_LEVEL_UPDATED |
1537                         IW_QUAL_QUAL_INVALID | IW_QUAL_NOISE_INVALID;
1538         } else
1539                 /* not associated */
1540                 is->qual.updated = IW_QUAL_ALL_INVALID;
1541
1542         kfree(cmd);
1543         free_page((unsigned long)buf);
1544         pr_debug("%s: ->\n", __func__);
1545         return is;
1546 }
1547
1548 /*
1549  *  scanning helpers
1550  */
1551 static int gelic_wl_start_scan(struct gelic_wl_info *wl, int always_scan,
1552                                u8 *essid, size_t essid_len)
1553 {
1554         struct gelic_eurus_cmd *cmd;
1555         int ret = 0;
1556         void *buf = NULL;
1557         size_t len;
1558
1559         pr_debug("%s: <- always=%d\n", __func__, always_scan);
1560         if (mutex_lock_interruptible(&wl->scan_lock))
1561                 return -ERESTARTSYS;
1562
1563         /*
1564          * If already a scan in progress, do not trigger more
1565          */
1566         if (wl->scan_stat == GELIC_WL_SCAN_STAT_SCANNING) {
1567                 pr_debug("%s: scanning now\n", __func__);
1568                 goto out;
1569         }
1570
1571         init_completion(&wl->scan_done);
1572         /*
1573          * If we have already a bss list, don't try to get new
1574          */
1575         if (!always_scan && wl->scan_stat == GELIC_WL_SCAN_STAT_GOT_LIST) {
1576                 pr_debug("%s: already has the list\n", __func__);
1577                 complete(&wl->scan_done);
1578                 goto out;
1579         }
1580
1581         /* ESSID scan ? */
1582         if (essid_len && essid) {
1583                 buf = (void *)__get_free_page(GFP_KERNEL);
1584                 if (!buf) {
1585                         ret = -ENOMEM;
1586                         goto out;
1587                 }
1588                 len = IW_ESSID_MAX_SIZE; /* hypervisor always requires 32 */
1589                 memset(buf, 0, len);
1590                 memcpy(buf, essid, essid_len);
1591                 pr_debug("%s: essid scan='%s'\n", __func__, (char *)buf);
1592         } else
1593                 len = 0;
1594
1595         /*
1596          * issue start scan request
1597          */
1598         wl->scan_stat = GELIC_WL_SCAN_STAT_SCANNING;
1599         cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_START_SCAN,
1600                                    buf, len);
1601         if (!cmd || cmd->status || cmd->cmd_status) {
1602                 wl->scan_stat = GELIC_WL_SCAN_STAT_INIT;
1603                 complete(&wl->scan_done);
1604                 ret = -ENOMEM;
1605                 goto out;
1606         }
1607         kfree(cmd);
1608 out:
1609         free_page((unsigned long)buf);
1610         mutex_unlock(&wl->scan_lock);
1611         pr_debug("%s: ->\n", __func__);
1612         return ret;
1613 }
1614
1615 /*
1616  * retrieve scan result from the chip (hypervisor)
1617  * this function is invoked by schedule work.
1618  */
1619 static void gelic_wl_scan_complete_event(struct gelic_wl_info *wl)
1620 {
1621         struct gelic_eurus_cmd *cmd = NULL;
1622         struct gelic_wl_scan_info *target, *tmp;
1623         struct gelic_wl_scan_info *oldest = NULL;
1624         struct gelic_eurus_scan_info *scan_info;
1625         unsigned int scan_info_size;
1626         union iwreq_data data;
1627         unsigned long this_time = jiffies;
1628         unsigned int data_len, i, found, r;
1629         void *buf;
1630
1631         pr_debug("%s:start\n", __func__);
1632         mutex_lock(&wl->scan_lock);
1633
1634         buf = (void *)__get_free_page(GFP_KERNEL);
1635         if (!buf) {
1636                 pr_info("%s: scan buffer alloc failed\n", __func__);
1637                 goto out;
1638         }
1639
1640         if (wl->scan_stat != GELIC_WL_SCAN_STAT_SCANNING) {
1641                 /*
1642                  * stop() may be called while scanning, ignore result
1643                  */
1644                 pr_debug("%s: scan complete when stat != scanning(%d)\n",
1645                          __func__, wl->scan_stat);
1646                 goto out;
1647         }
1648
1649         cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_GET_SCAN,
1650                                    buf, PAGE_SIZE);
1651         if (!cmd || cmd->status || cmd->cmd_status) {
1652                 wl->scan_stat = GELIC_WL_SCAN_STAT_INIT;
1653                 pr_info("%s:cmd failed\n", __func__);
1654                 kfree(cmd);
1655                 goto out;
1656         }
1657         data_len = cmd->size;
1658         pr_debug("%s: data_len = %d\n", __func__, data_len);
1659         kfree(cmd);
1660
1661         /* OK, bss list retrieved */
1662         wl->scan_stat = GELIC_WL_SCAN_STAT_GOT_LIST;
1663
1664         /* mark all entries are old */
1665         list_for_each_entry_safe(target, tmp, &wl->network_list, list) {
1666                 target->valid = 0;
1667                 /* expire too old entries */
1668                 if (time_before(target->last_scanned + wl->scan_age,
1669                                 this_time)) {
1670                         kfree(target->hwinfo);
1671                         target->hwinfo = NULL;
1672                         list_move_tail(&target->list, &wl->network_free_list);
1673                 }
1674         }
1675
1676         /* put them in the newtork_list */
1677         for (i = 0, scan_info_size = 0, scan_info = buf;
1678              scan_info_size < data_len;
1679              i++, scan_info_size += be16_to_cpu(scan_info->size),
1680              scan_info = (void *)scan_info + be16_to_cpu(scan_info->size)) {
1681                 pr_debug("%s:size=%d bssid=%pM scan_info=%p\n", __func__,
1682                          be16_to_cpu(scan_info->size),
1683                          &scan_info->bssid[2], scan_info);
1684
1685                 /*
1686                  * The wireless firmware may return invalid channel 0 and/or
1687                  * invalid rate if the AP emits zero length SSID ie. As this
1688                  * scan information is useless, ignore it
1689                  */
1690                 if (!be16_to_cpu(scan_info->channel) || !scan_info->rate[0]) {
1691                         pr_debug("%s: invalid scan info\n", __func__);
1692                         continue;
1693                 }
1694
1695                 found = 0;
1696                 oldest = NULL;
1697                 list_for_each_entry(target, &wl->network_list, list) {
1698                         if (!compare_ether_addr(&target->hwinfo->bssid[2],
1699                                                 &scan_info->bssid[2])) {
1700                                 found = 1;
1701                                 pr_debug("%s: same BBS found scanned list\n",
1702                                          __func__);
1703                                 break;
1704                         }
1705                         if (!oldest ||
1706                             (target->last_scanned < oldest->last_scanned))
1707                                 oldest = target;
1708                 }
1709
1710                 if (!found) {
1711                         /* not found in the list */
1712                         if (list_empty(&wl->network_free_list)) {
1713                                 /* expire oldest */
1714                                 target = oldest;
1715                         } else {
1716                                 target = list_entry(wl->network_free_list.next,
1717                                                     struct gelic_wl_scan_info,
1718                                                     list);
1719                         }
1720                 }
1721
1722                 /* update the item */
1723                 target->last_scanned = this_time;
1724                 target->valid = 1;
1725                 target->eurus_index = i;
1726                 kfree(target->hwinfo);
1727                 target->hwinfo = kzalloc(be16_to_cpu(scan_info->size),
1728                                          GFP_KERNEL);
1729                 if (!target->hwinfo) {
1730                         pr_info("%s: kzalloc failed\n", __func__);
1731                         continue;
1732                 }
1733                 /* copy hw scan info */
1734                 memcpy(target->hwinfo, scan_info, scan_info->size);
1735                 target->essid_len = strnlen(scan_info->essid,
1736                                             sizeof(scan_info->essid));
1737                 target->rate_len = 0;
1738                 for (r = 0; r < 12; r++)
1739                         if (scan_info->rate[r])
1740                                 target->rate_len++;
1741                 if (8 < target->rate_len)
1742                         pr_info("%s: AP returns %d rates\n", __func__,
1743                                 target->rate_len);
1744                 target->rate_ext_len = 0;
1745                 for (r = 0; r < 16; r++)
1746                         if (scan_info->ext_rate[r])
1747                                 target->rate_ext_len++;
1748                 list_move_tail(&target->list, &wl->network_list);
1749         }
1750         memset(&data, 0, sizeof(data));
1751         wireless_send_event(port_to_netdev(wl_port(wl)), SIOCGIWSCAN, &data,
1752                             NULL);
1753 out:
1754         free_page((unsigned long)buf);
1755         complete(&wl->scan_done);
1756         mutex_unlock(&wl->scan_lock);
1757         pr_debug("%s:end\n", __func__);
1758 }
1759
1760 /*
1761  * Select an appropriate bss from current scan list regarding
1762  * current settings from userspace.
1763  * The caller must hold wl->scan_lock,
1764  * and on the state of wl->scan_state == GELIC_WL_SCAN_GOT_LIST
1765  */
1766 static void update_best(struct gelic_wl_scan_info **best,
1767                         struct gelic_wl_scan_info *candid,
1768                         int *best_weight,
1769                         int *weight)
1770 {
1771         if (*best_weight < ++(*weight)) {
1772                 *best_weight = *weight;
1773                 *best = candid;
1774         }
1775 }
1776
1777 static
1778 struct gelic_wl_scan_info *gelic_wl_find_best_bss(struct gelic_wl_info *wl)
1779 {
1780         struct gelic_wl_scan_info *scan_info;
1781         struct gelic_wl_scan_info *best_bss;
1782         int weight, best_weight;
1783         u16 security;
1784
1785         pr_debug("%s: <-\n", __func__);
1786
1787         best_bss = NULL;
1788         best_weight = 0;
1789
1790         list_for_each_entry(scan_info, &wl->network_list, list) {
1791                 pr_debug("%s: station %p\n", __func__, scan_info);
1792
1793                 if (!scan_info->valid) {
1794                         pr_debug("%s: station invalid\n", __func__);
1795                         continue;
1796                 }
1797
1798                 /* If bss specified, check it only */
1799                 if (test_bit(GELIC_WL_STAT_BSSID_SET, &wl->stat)) {
1800                         if (!compare_ether_addr(&scan_info->hwinfo->bssid[2],
1801                                                 wl->bssid)) {
1802                                 best_bss = scan_info;
1803                                 pr_debug("%s: bssid matched\n", __func__);
1804                                 break;
1805                         } else {
1806                                 pr_debug("%s: bssid unmached\n", __func__);
1807                                 continue;
1808                         }
1809                 }
1810
1811                 weight = 0;
1812
1813                 /* security */
1814                 security = be16_to_cpu(scan_info->hwinfo->security) &
1815                         GELIC_EURUS_SCAN_SEC_MASK;
1816                 if (wl->wpa_level == GELIC_WL_WPA_LEVEL_WPA2) {
1817                         if (security == GELIC_EURUS_SCAN_SEC_WPA2)
1818                                 update_best(&best_bss, scan_info,
1819                                             &best_weight, &weight);
1820                         else
1821                                 continue;
1822                 } else if (wl->wpa_level == GELIC_WL_WPA_LEVEL_WPA) {
1823                         if (security == GELIC_EURUS_SCAN_SEC_WPA)
1824                                 update_best(&best_bss, scan_info,
1825                                             &best_weight, &weight);
1826                         else
1827                                 continue;
1828                 } else if (wl->wpa_level == GELIC_WL_WPA_LEVEL_NONE &&
1829                            wl->group_cipher_method == GELIC_WL_CIPHER_WEP) {
1830                         if (security == GELIC_EURUS_SCAN_SEC_WEP)
1831                                 update_best(&best_bss, scan_info,
1832                                             &best_weight, &weight);
1833                         else
1834                                 continue;
1835                 }
1836
1837                 /* If ESSID is set, check it */
1838                 if (test_bit(GELIC_WL_STAT_ESSID_SET, &wl->stat)) {
1839                         if ((scan_info->essid_len == wl->essid_len) &&
1840                             !strncmp(wl->essid,
1841                                      scan_info->hwinfo->essid,
1842                                      scan_info->essid_len))
1843                                 update_best(&best_bss, scan_info,
1844                                             &best_weight, &weight);
1845                         else
1846                                 continue;
1847                 }
1848         }
1849
1850 #ifdef DEBUG
1851         pr_debug("%s: -> bss=%p\n", __func__, best_bss);
1852         if (best_bss) {
1853                 pr_debug("%s:addr=%pM\n", __func__,
1854                          &best_bss->hwinfo->bssid[2]);
1855         }
1856 #endif
1857         return best_bss;
1858 }
1859
1860 /*
1861  * Setup WEP configuration to the chip
1862  * The caller must hold wl->scan_lock,
1863  * and on the state of wl->scan_state == GELIC_WL_SCAN_GOT_LIST
1864  */
1865 static int gelic_wl_do_wep_setup(struct gelic_wl_info *wl)
1866 {
1867         unsigned int i;
1868         struct gelic_eurus_wep_cfg *wep;
1869         struct gelic_eurus_cmd *cmd;
1870         int wep104 = 0;
1871         int have_key = 0;
1872         int ret = 0;
1873
1874         pr_debug("%s: <-\n", __func__);
1875         /* we can assume no one should uses the buffer */
1876         wep = (struct gelic_eurus_wep_cfg *)__get_free_page(GFP_KERNEL);
1877         if (!wep)
1878                 return -ENOMEM;
1879
1880         memset(wep, 0, sizeof(*wep));
1881
1882         if (wl->group_cipher_method == GELIC_WL_CIPHER_WEP) {
1883                 pr_debug("%s: WEP mode\n", __func__);
1884                 for (i = 0; i < GELIC_WEP_KEYS; i++) {
1885                         if (!test_bit(i, &wl->key_enabled))
1886                                 continue;
1887
1888                         pr_debug("%s: key#%d enabled\n", __func__, i);
1889                         have_key = 1;
1890                         if (wl->key_len[i] == 13)
1891                                 wep104 = 1;
1892                         else if (wl->key_len[i] != 5) {
1893                                 pr_info("%s: wrong wep key[%d]=%d\n",
1894                                         __func__, i, wl->key_len[i]);
1895                                 ret = -EINVAL;
1896                                 goto out;
1897                         }
1898                         memcpy(wep->key[i], wl->key[i], wl->key_len[i]);
1899                 }
1900
1901                 if (!have_key) {
1902                         pr_info("%s: all wep key disabled\n", __func__);
1903                         ret = -EINVAL;
1904                         goto out;
1905                 }
1906
1907                 if (wep104) {
1908                         pr_debug("%s: 104bit key\n", __func__);
1909                         wep->security = cpu_to_be16(GELIC_EURUS_WEP_SEC_104BIT);
1910                 } else {
1911                         pr_debug("%s: 40bit key\n", __func__);
1912                         wep->security = cpu_to_be16(GELIC_EURUS_WEP_SEC_40BIT);
1913                 }
1914         } else {
1915                 pr_debug("%s: NO encryption\n", __func__);
1916                 wep->security = cpu_to_be16(GELIC_EURUS_WEP_SEC_NONE);
1917         }
1918
1919         /* issue wep setup */
1920         cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_SET_WEP_CFG,
1921                                    wep, sizeof(*wep));
1922         if (!cmd)
1923                 ret = -ENOMEM;
1924         else if (cmd->status || cmd->cmd_status)
1925                 ret = -ENXIO;
1926
1927         kfree(cmd);
1928 out:
1929         free_page((unsigned long)wep);
1930         pr_debug("%s: ->\n", __func__);
1931         return ret;
1932 }
1933
1934 #ifdef DEBUG
1935 static const char *wpasecstr(enum gelic_eurus_wpa_security sec)
1936 {
1937         switch (sec) {
1938         case GELIC_EURUS_WPA_SEC_NONE:
1939                 return "NONE";
1940                 break;
1941         case GELIC_EURUS_WPA_SEC_WPA_TKIP_TKIP:
1942                 return "WPA_TKIP_TKIP";
1943                 break;
1944         case GELIC_EURUS_WPA_SEC_WPA_TKIP_AES:
1945                 return "WPA_TKIP_AES";
1946                 break;
1947         case GELIC_EURUS_WPA_SEC_WPA_AES_AES:
1948                 return "WPA_AES_AES";
1949                 break;
1950         case GELIC_EURUS_WPA_SEC_WPA2_TKIP_TKIP:
1951                 return "WPA2_TKIP_TKIP";
1952                 break;
1953         case GELIC_EURUS_WPA_SEC_WPA2_TKIP_AES:
1954                 return "WPA2_TKIP_AES";
1955                 break;
1956         case GELIC_EURUS_WPA_SEC_WPA2_AES_AES:
1957                 return "WPA2_AES_AES";
1958                 break;
1959         }
1960         return "";
1961 };
1962 #endif
1963
1964 static int gelic_wl_do_wpa_setup(struct gelic_wl_info *wl)
1965 {
1966         struct gelic_eurus_wpa_cfg *wpa;
1967         struct gelic_eurus_cmd *cmd;
1968         u16 security;
1969         int ret = 0;
1970
1971         pr_debug("%s: <-\n", __func__);
1972         /* we can assume no one should uses the buffer */
1973         wpa = (struct gelic_eurus_wpa_cfg *)__get_free_page(GFP_KERNEL);
1974         if (!wpa)
1975                 return -ENOMEM;
1976
1977         memset(wpa, 0, sizeof(*wpa));
1978
1979         if (!test_bit(GELIC_WL_STAT_WPA_PSK_SET, &wl->stat))
1980                 pr_info("%s: PSK not configured yet\n", __func__);
1981
1982         /* copy key */
1983         memcpy(wpa->psk, wl->psk, wl->psk_len);
1984
1985         /* set security level */
1986         if (wl->wpa_level == GELIC_WL_WPA_LEVEL_WPA2) {
1987                 if (wl->group_cipher_method == GELIC_WL_CIPHER_AES) {
1988                         security = GELIC_EURUS_WPA_SEC_WPA2_AES_AES;
1989                 } else {
1990                         if (wl->pairwise_cipher_method == GELIC_WL_CIPHER_AES &&
1991                             precise_ie())
1992                                 security = GELIC_EURUS_WPA_SEC_WPA2_TKIP_AES;
1993                         else
1994                                 security = GELIC_EURUS_WPA_SEC_WPA2_TKIP_TKIP;
1995                 }
1996         } else {
1997                 if (wl->group_cipher_method == GELIC_WL_CIPHER_AES) {
1998                         security = GELIC_EURUS_WPA_SEC_WPA_AES_AES;
1999                 } else {
2000                         if (wl->pairwise_cipher_method == GELIC_WL_CIPHER_AES &&
2001                             precise_ie())
2002                                 security = GELIC_EURUS_WPA_SEC_WPA_TKIP_AES;
2003                         else
2004                                 security = GELIC_EURUS_WPA_SEC_WPA_TKIP_TKIP;
2005                 }
2006         }
2007         wpa->security = cpu_to_be16(security);
2008
2009         /* PSK type */
2010         wpa->psk_type = cpu_to_be16(wl->psk_type);
2011 #ifdef DEBUG
2012         pr_debug("%s: sec=%s psktype=%s\nn", __func__,
2013                  wpasecstr(wpa->security),
2014                  (wpa->psk_type == GELIC_EURUS_WPA_PSK_BIN) ?
2015                  "BIN" : "passphrase");
2016 #if 0
2017         /*
2018          * don't enable here if you plan to submit
2019          * the debug log because this dumps your precious
2020          * passphrase/key.
2021          */
2022         pr_debug("%s: psk=%s\n",
2023                  (wpa->psk_type == GELIC_EURUS_WPA_PSK_BIN) ?
2024                  (char *)"N/A" : (char *)wpa->psk);
2025 #endif
2026 #endif
2027         /* issue wpa setup */
2028         cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_SET_WPA_CFG,
2029                                    wpa, sizeof(*wpa));
2030         if (!cmd)
2031                 ret = -ENOMEM;
2032         else if (cmd->status || cmd->cmd_status)
2033                 ret = -ENXIO;
2034         kfree(cmd);
2035         free_page((unsigned long)wpa);
2036         pr_debug("%s: --> %d\n", __func__, ret);
2037         return ret;
2038 }
2039
2040 /*
2041  * Start association. caller must hold assoc_stat_lock
2042  */
2043 static int gelic_wl_associate_bss(struct gelic_wl_info *wl,
2044                                   struct gelic_wl_scan_info *bss)
2045 {
2046         struct gelic_eurus_cmd *cmd;
2047         struct gelic_eurus_common_cfg *common;
2048         int ret = 0;
2049         unsigned long rc;
2050
2051         pr_debug("%s: <-\n", __func__);
2052
2053         /* do common config */
2054         common = (struct gelic_eurus_common_cfg *)__get_free_page(GFP_KERNEL);
2055         if (!common)
2056                 return -ENOMEM;
2057
2058         memset(common, 0, sizeof(*common));
2059         common->bss_type = cpu_to_be16(GELIC_EURUS_BSS_INFRA);
2060         common->op_mode = cpu_to_be16(GELIC_EURUS_OPMODE_11BG);
2061
2062         common->scan_index = cpu_to_be16(bss->eurus_index);
2063         switch (wl->auth_method) {
2064         case GELIC_EURUS_AUTH_OPEN:
2065                 common->auth_method = cpu_to_be16(GELIC_EURUS_AUTH_OPEN);
2066                 break;
2067         case GELIC_EURUS_AUTH_SHARED:
2068                 common->auth_method = cpu_to_be16(GELIC_EURUS_AUTH_SHARED);
2069                 break;
2070         }
2071
2072 #ifdef DEBUG
2073         scan_list_dump(wl);
2074 #endif
2075         pr_debug("%s: common cfg index=%d bsstype=%d auth=%d\n", __func__,
2076                  be16_to_cpu(common->scan_index),
2077                  be16_to_cpu(common->bss_type),
2078                  be16_to_cpu(common->auth_method));
2079
2080         cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_SET_COMMON_CFG,
2081                                    common, sizeof(*common));
2082         if (!cmd || cmd->status || cmd->cmd_status) {
2083                 ret = -ENOMEM;
2084                 kfree(cmd);
2085                 goto out;
2086         }
2087         kfree(cmd);
2088
2089         /* WEP/WPA */
2090         switch (wl->wpa_level) {
2091         case GELIC_WL_WPA_LEVEL_NONE:
2092                 /* If WEP or no security, setup WEP config */
2093                 ret = gelic_wl_do_wep_setup(wl);
2094                 break;
2095         case GELIC_WL_WPA_LEVEL_WPA:
2096         case GELIC_WL_WPA_LEVEL_WPA2:
2097                 ret = gelic_wl_do_wpa_setup(wl);
2098                 break;
2099         };
2100
2101         if (ret) {
2102                 pr_debug("%s: WEP/WPA setup failed %d\n", __func__,
2103                          ret);
2104                 ret = -EPERM;
2105                 gelic_wl_send_iwap_event(wl, NULL);
2106                 goto out;
2107         }
2108
2109         /* start association */
2110         init_completion(&wl->assoc_done);
2111         wl->assoc_stat = GELIC_WL_ASSOC_STAT_ASSOCIATING;
2112         cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_ASSOC,
2113                                    NULL, 0);
2114         if (!cmd || cmd->status || cmd->cmd_status) {
2115                 pr_debug("%s: assoc request failed\n", __func__);
2116                 wl->assoc_stat = GELIC_WL_ASSOC_STAT_DISCONN;
2117                 kfree(cmd);
2118                 ret = -ENOMEM;
2119                 gelic_wl_send_iwap_event(wl, NULL);
2120                 goto out;
2121         }
2122         kfree(cmd);
2123
2124         /* wait for connected event */
2125         rc = wait_for_completion_timeout(&wl->assoc_done, HZ * 4);/*FIXME*/
2126
2127         if (!rc) {
2128                 /* timeouted.  Maybe key or cyrpt mode is wrong */
2129                 pr_info("%s: connect timeout \n", __func__);
2130                 cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_DISASSOC,
2131                                            NULL, 0);
2132                 kfree(cmd);
2133                 wl->assoc_stat = GELIC_WL_ASSOC_STAT_DISCONN;
2134                 gelic_wl_send_iwap_event(wl, NULL);
2135                 ret = -ENXIO;
2136         } else {
2137                 wl->assoc_stat = GELIC_WL_ASSOC_STAT_ASSOCIATED;
2138                 /* copy bssid */
2139                 memcpy(wl->active_bssid, &bss->hwinfo->bssid[2], ETH_ALEN);
2140
2141                 /* send connect event */
2142                 gelic_wl_send_iwap_event(wl, wl->active_bssid);
2143                 pr_info("%s: connected\n", __func__);
2144         }
2145 out:
2146         free_page((unsigned long)common);
2147         pr_debug("%s: ->\n", __func__);
2148         return ret;
2149 }
2150
2151 /*
2152  * connected event
2153  */
2154 static void gelic_wl_connected_event(struct gelic_wl_info *wl,
2155                                      u64 event)
2156 {
2157         u64 desired_event = 0;
2158
2159         switch (wl->wpa_level) {
2160         case GELIC_WL_WPA_LEVEL_NONE:
2161                 desired_event = GELIC_LV1_WL_EVENT_CONNECTED;
2162                 break;
2163         case GELIC_WL_WPA_LEVEL_WPA:
2164         case GELIC_WL_WPA_LEVEL_WPA2:
2165                 desired_event = GELIC_LV1_WL_EVENT_WPA_CONNECTED;
2166                 break;
2167         }
2168
2169         if (desired_event == event) {
2170                 pr_debug("%s: completed \n", __func__);
2171                 complete(&wl->assoc_done);
2172                 netif_carrier_on(port_to_netdev(wl_port(wl)));
2173         } else
2174                 pr_debug("%s: event %#llx under wpa\n",
2175                                  __func__, event);
2176 }
2177
2178 /*
2179  * disconnect event
2180  */
2181 static void gelic_wl_disconnect_event(struct gelic_wl_info *wl,
2182                                       u64 event)
2183 {
2184         struct gelic_eurus_cmd *cmd;
2185         int lock;
2186
2187         /*
2188          * If we fall here in the middle of association,
2189          * associate_bss() should be waiting for complation of
2190          * wl->assoc_done.
2191          * As it waits with timeout, just leave assoc_done
2192          * uncompleted, then it terminates with timeout
2193          */
2194         if (!mutex_trylock(&wl->assoc_stat_lock)) {
2195                 pr_debug("%s: already locked\n", __func__);
2196                 lock = 0;
2197         } else {
2198                 pr_debug("%s: obtain lock\n", __func__);
2199                 lock = 1;
2200         }
2201
2202         cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_DISASSOC, NULL, 0);
2203         kfree(cmd);
2204
2205         /* send disconnected event to the supplicant */
2206         if (wl->assoc_stat == GELIC_WL_ASSOC_STAT_ASSOCIATED)
2207                 gelic_wl_send_iwap_event(wl, NULL);
2208
2209         wl->assoc_stat = GELIC_WL_ASSOC_STAT_DISCONN;
2210         netif_carrier_off(port_to_netdev(wl_port(wl)));
2211
2212         if (lock)
2213                 mutex_unlock(&wl->assoc_stat_lock);
2214 }
2215 /*
2216  * event worker
2217  */
2218 #ifdef DEBUG
2219 static const char *eventstr(enum gelic_lv1_wl_event event)
2220 {
2221         static char buf[32];
2222         char *ret;
2223         if (event & GELIC_LV1_WL_EVENT_DEVICE_READY)
2224                 ret = "EURUS_READY";
2225         else if (event & GELIC_LV1_WL_EVENT_SCAN_COMPLETED)
2226                 ret = "SCAN_COMPLETED";
2227         else if (event & GELIC_LV1_WL_EVENT_DEAUTH)
2228                 ret = "DEAUTH";
2229         else if (event & GELIC_LV1_WL_EVENT_BEACON_LOST)
2230                 ret = "BEACON_LOST";
2231         else if (event & GELIC_LV1_WL_EVENT_CONNECTED)
2232                 ret = "CONNECTED";
2233         else if (event & GELIC_LV1_WL_EVENT_WPA_CONNECTED)
2234                 ret = "WPA_CONNECTED";
2235         else if (event & GELIC_LV1_WL_EVENT_WPA_ERROR)
2236                 ret = "WPA_ERROR";
2237         else {
2238                 sprintf(buf, "Unknown(%#x)", event);
2239                 ret = buf;
2240         }
2241         return ret;
2242 }
2243 #else
2244 static const char *eventstr(enum gelic_lv1_wl_event event)
2245 {
2246         return NULL;
2247 }
2248 #endif
2249 static void gelic_wl_event_worker(struct work_struct *work)
2250 {
2251         struct gelic_wl_info *wl;
2252         struct gelic_port *port;
2253         u64 event, tmp;
2254         int status;
2255
2256         pr_debug("%s:start\n", __func__);
2257         wl = container_of(work, struct gelic_wl_info, event_work.work);
2258         port = wl_port(wl);
2259         while (1) {
2260                 status = lv1_net_control(bus_id(port->card), dev_id(port->card),
2261                                          GELIC_LV1_GET_WLAN_EVENT, 0, 0, 0,
2262                                          &event, &tmp);
2263                 if (status) {
2264                         if (status != LV1_NO_ENTRY)
2265                                 pr_debug("%s:wlan event failed %d\n",
2266                                          __func__, status);
2267                         /* got all events */
2268                         pr_debug("%s:end\n", __func__);
2269                         return;
2270                 }
2271                 pr_debug("%s: event=%s\n", __func__, eventstr(event));
2272                 switch (event) {
2273                 case GELIC_LV1_WL_EVENT_SCAN_COMPLETED:
2274                         gelic_wl_scan_complete_event(wl);
2275                         break;
2276                 case GELIC_LV1_WL_EVENT_BEACON_LOST:
2277                 case GELIC_LV1_WL_EVENT_DEAUTH:
2278                         gelic_wl_disconnect_event(wl, event);
2279                         break;
2280                 case GELIC_LV1_WL_EVENT_CONNECTED:
2281                 case GELIC_LV1_WL_EVENT_WPA_CONNECTED:
2282                         gelic_wl_connected_event(wl, event);
2283                         break;
2284                 default:
2285                         break;
2286                 }
2287         } /* while */
2288 }
2289 /*
2290  * association worker
2291  */
2292 static void gelic_wl_assoc_worker(struct work_struct *work)
2293 {
2294         struct gelic_wl_info *wl;
2295
2296         struct gelic_wl_scan_info *best_bss;
2297         int ret;
2298         unsigned long irqflag;
2299         u8 *essid;
2300         size_t essid_len;
2301
2302         wl = container_of(work, struct gelic_wl_info, assoc_work.work);
2303
2304         mutex_lock(&wl->assoc_stat_lock);
2305
2306         if (wl->assoc_stat != GELIC_WL_ASSOC_STAT_DISCONN)
2307                 goto out;
2308
2309         spin_lock_irqsave(&wl->lock, irqflag);
2310         if (test_bit(GELIC_WL_STAT_ESSID_SET, &wl->stat)) {
2311                 pr_debug("%s: assoc ESSID configured %s\n", __func__,
2312                          wl->essid);
2313                 essid = wl->essid;
2314                 essid_len = wl->essid_len;
2315         } else {
2316                 essid = NULL;
2317                 essid_len = 0;
2318         }
2319         spin_unlock_irqrestore(&wl->lock, irqflag);
2320
2321         ret = gelic_wl_start_scan(wl, 0, essid, essid_len);
2322         if (ret == -ERESTARTSYS) {
2323                 pr_debug("%s: scan start failed association\n", __func__);
2324                 schedule_delayed_work(&wl->assoc_work, HZ/10); /*FIXME*/
2325                 goto out;
2326         } else if (ret) {
2327                 pr_info("%s: scan prerequisite failed\n", __func__);
2328                 goto out;
2329         }
2330
2331         /*
2332          * Wait for bss scan completion
2333          * If we have scan list already, gelic_wl_start_scan()
2334          * returns OK and raises the complete.  Thus,
2335          * it's ok to wait unconditionally here
2336          */
2337         wait_for_completion(&wl->scan_done);
2338
2339         pr_debug("%s: scan done\n", __func__);
2340         mutex_lock(&wl->scan_lock);
2341         if (wl->scan_stat != GELIC_WL_SCAN_STAT_GOT_LIST) {
2342                 gelic_wl_send_iwap_event(wl, NULL);
2343                 pr_info("%s: no scan list. association failed\n", __func__);
2344                 goto scan_lock_out;
2345         }
2346
2347         /* find best matching bss */
2348         best_bss = gelic_wl_find_best_bss(wl);
2349         if (!best_bss) {
2350                 gelic_wl_send_iwap_event(wl, NULL);
2351                 pr_info("%s: no bss matched. association failed\n", __func__);
2352                 goto scan_lock_out;
2353         }
2354
2355         /* ok, do association */
2356         ret = gelic_wl_associate_bss(wl, best_bss);
2357         if (ret)
2358                 pr_info("%s: association failed %d\n", __func__, ret);
2359 scan_lock_out:
2360         mutex_unlock(&wl->scan_lock);
2361 out:
2362         mutex_unlock(&wl->assoc_stat_lock);
2363 }
2364 /*
2365  * Interrupt handler
2366  * Called from the ethernet interrupt handler
2367  * Processes wireless specific virtual interrupts only
2368  */
2369 void gelic_wl_interrupt(struct net_device *netdev, u64 status)
2370 {
2371         struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
2372
2373         if (status & GELIC_CARD_WLAN_COMMAND_COMPLETED) {
2374                 pr_debug("%s:cmd complete\n", __func__);
2375                 complete(&wl->cmd_done_intr);
2376         }
2377
2378         if (status & GELIC_CARD_WLAN_EVENT_RECEIVED) {
2379                 pr_debug("%s:event received\n", __func__);
2380                 queue_delayed_work(wl->event_queue, &wl->event_work, 0);
2381         }
2382 }
2383
2384 /*
2385  * driver helpers
2386  */
2387 #define IW_IOCTL(n) [(n) - SIOCSIWCOMMIT]
2388 static const iw_handler gelic_wl_wext_handler[] =
2389 {
2390         IW_IOCTL(SIOCGIWNAME)           = gelic_wl_get_name,
2391         IW_IOCTL(SIOCGIWRANGE)          = gelic_wl_get_range,
2392         IW_IOCTL(SIOCSIWSCAN)           = gelic_wl_set_scan,
2393         IW_IOCTL(SIOCGIWSCAN)           = gelic_wl_get_scan,
2394         IW_IOCTL(SIOCSIWAUTH)           = gelic_wl_set_auth,
2395         IW_IOCTL(SIOCGIWAUTH)           = gelic_wl_get_auth,
2396         IW_IOCTL(SIOCSIWESSID)          = gelic_wl_set_essid,
2397         IW_IOCTL(SIOCGIWESSID)          = gelic_wl_get_essid,
2398         IW_IOCTL(SIOCSIWENCODE)         = gelic_wl_set_encode,
2399         IW_IOCTL(SIOCGIWENCODE)         = gelic_wl_get_encode,
2400         IW_IOCTL(SIOCSIWAP)             = gelic_wl_set_ap,
2401         IW_IOCTL(SIOCGIWAP)             = gelic_wl_get_ap,
2402         IW_IOCTL(SIOCSIWENCODEEXT)      = gelic_wl_set_encodeext,
2403         IW_IOCTL(SIOCGIWENCODEEXT)      = gelic_wl_get_encodeext,
2404         IW_IOCTL(SIOCSIWMODE)           = gelic_wl_set_mode,
2405         IW_IOCTL(SIOCGIWMODE)           = gelic_wl_get_mode,
2406         IW_IOCTL(SIOCGIWNICKN)          = gelic_wl_get_nick,
2407 };
2408
2409 #ifdef CONFIG_GELIC_WIRELESS_OLD_PSK_INTERFACE
2410 static struct iw_priv_args gelic_wl_private_args[] =
2411 {
2412         {
2413                 .cmd = GELIC_WL_PRIV_SET_PSK,
2414                 .set_args = IW_PRIV_TYPE_CHAR |
2415                 (GELIC_WL_EURUS_PSK_MAX_LEN + 2),
2416                 .name = "set_psk"
2417         },
2418         {
2419                 .cmd = GELIC_WL_PRIV_GET_PSK,
2420                 .get_args = IW_PRIV_TYPE_CHAR |
2421                 (GELIC_WL_EURUS_PSK_MAX_LEN + 2),
2422                 .name = "get_psk"
2423         }
2424 };
2425
2426 static const iw_handler gelic_wl_private_handler[] =
2427 {
2428         gelic_wl_priv_set_psk,
2429         gelic_wl_priv_get_psk,
2430 };
2431 #endif
2432
2433 static const struct iw_handler_def gelic_wl_wext_handler_def = {
2434         .num_standard           = ARRAY_SIZE(gelic_wl_wext_handler),
2435         .standard               = gelic_wl_wext_handler,
2436         .get_wireless_stats     = gelic_wl_get_wireless_stats,
2437 #ifdef CONFIG_GELIC_WIRELESS_OLD_PSK_INTERFACE
2438         .num_private            = ARRAY_SIZE(gelic_wl_private_handler),
2439         .num_private_args       = ARRAY_SIZE(gelic_wl_private_args),
2440         .private                = gelic_wl_private_handler,
2441         .private_args           = gelic_wl_private_args,
2442 #endif
2443 };
2444
2445 static struct net_device *gelic_wl_alloc(struct gelic_card *card)
2446 {
2447         struct net_device *netdev;
2448         struct gelic_port *port;
2449         struct gelic_wl_info *wl;
2450         unsigned int i;
2451
2452         pr_debug("%s:start\n", __func__);
2453         netdev = alloc_etherdev(sizeof(struct gelic_port) +
2454                                 sizeof(struct gelic_wl_info));
2455         pr_debug("%s: netdev =%p card=%p \np", __func__, netdev, card);
2456         if (!netdev)
2457                 return NULL;
2458
2459         strcpy(netdev->name, "wlan%d");
2460
2461         port = netdev_priv(netdev);
2462         port->netdev = netdev;
2463         port->card = card;
2464         port->type = GELIC_PORT_WIRELESS;
2465
2466         wl = port_wl(port);
2467         pr_debug("%s: wl=%p port=%p\n", __func__, wl, port);
2468
2469         /* allocate scan list */
2470         wl->networks = kzalloc(sizeof(struct gelic_wl_scan_info) *
2471                                GELIC_WL_BSS_MAX_ENT, GFP_KERNEL);
2472
2473         if (!wl->networks)
2474                 goto fail_bss;
2475
2476         wl->eurus_cmd_queue = create_singlethread_workqueue("gelic_cmd");
2477         if (!wl->eurus_cmd_queue)
2478                 goto fail_cmd_workqueue;
2479
2480         wl->event_queue = create_singlethread_workqueue("gelic_event");
2481         if (!wl->event_queue)
2482                 goto fail_event_workqueue;
2483
2484         INIT_LIST_HEAD(&wl->network_free_list);
2485         INIT_LIST_HEAD(&wl->network_list);
2486         for (i = 0; i < GELIC_WL_BSS_MAX_ENT; i++)
2487                 list_add_tail(&wl->networks[i].list,
2488                               &wl->network_free_list);
2489         init_completion(&wl->cmd_done_intr);
2490
2491         INIT_DELAYED_WORK(&wl->event_work, gelic_wl_event_worker);
2492         INIT_DELAYED_WORK(&wl->assoc_work, gelic_wl_assoc_worker);
2493         mutex_init(&wl->scan_lock);
2494         mutex_init(&wl->assoc_stat_lock);
2495
2496         init_completion(&wl->scan_done);
2497         /* for the case that no scan request is issued and stop() is called */
2498         complete(&wl->scan_done);
2499
2500         spin_lock_init(&wl->lock);
2501
2502         wl->scan_age = 5*HZ; /* FIXME */
2503
2504         /* buffer for receiving scanned list etc */
2505         BUILD_BUG_ON(PAGE_SIZE <
2506                      sizeof(struct gelic_eurus_scan_info) *
2507                      GELIC_EURUS_MAX_SCAN);
2508         pr_debug("%s:end\n", __func__);
2509         return netdev;
2510
2511 fail_event_workqueue:
2512         destroy_workqueue(wl->eurus_cmd_queue);
2513 fail_cmd_workqueue:
2514         kfree(wl->networks);
2515 fail_bss:
2516         free_netdev(netdev);
2517         pr_debug("%s:end error\n", __func__);
2518         return NULL;
2519
2520 }
2521
2522 static void gelic_wl_free(struct gelic_wl_info *wl)
2523 {
2524         struct gelic_wl_scan_info *scan_info;
2525         unsigned int i;
2526
2527         pr_debug("%s: <-\n", __func__);
2528
2529         pr_debug("%s: destroy queues\n", __func__);
2530         destroy_workqueue(wl->eurus_cmd_queue);
2531         destroy_workqueue(wl->event_queue);
2532
2533         scan_info = wl->networks;
2534         for (i = 0; i < GELIC_WL_BSS_MAX_ENT; i++, scan_info++)
2535                 kfree(scan_info->hwinfo);
2536         kfree(wl->networks);
2537
2538         free_netdev(port_to_netdev(wl_port(wl)));
2539
2540         pr_debug("%s: ->\n", __func__);
2541 }
2542
2543 static int gelic_wl_try_associate(struct net_device *netdev)
2544 {
2545         struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
2546         int ret = -1;
2547         unsigned int i;
2548
2549         pr_debug("%s: <-\n", __func__);
2550
2551         /* check constraits for start association */
2552         /* for no access restriction AP */
2553         if (wl->group_cipher_method == GELIC_WL_CIPHER_NONE) {
2554                 if (test_bit(GELIC_WL_STAT_CONFIGURED,
2555                              &wl->stat))
2556                         goto do_associate;
2557                 else {
2558                         pr_debug("%s: no wep, not configured\n", __func__);
2559                         return ret;
2560                 }
2561         }
2562
2563         /* for WEP, one of four keys should be set */
2564         if (wl->group_cipher_method == GELIC_WL_CIPHER_WEP) {
2565                 /* one of keys set */
2566                 for (i = 0; i < GELIC_WEP_KEYS; i++) {
2567                         if (test_bit(i, &wl->key_enabled))
2568                             goto do_associate;
2569                 }
2570                 pr_debug("%s: WEP, but no key specified\n", __func__);
2571                 return ret;
2572         }
2573
2574         /* for WPA[2], psk should be set */
2575         if ((wl->group_cipher_method == GELIC_WL_CIPHER_TKIP) ||
2576             (wl->group_cipher_method == GELIC_WL_CIPHER_AES)) {
2577                 if (test_bit(GELIC_WL_STAT_WPA_PSK_SET,
2578                              &wl->stat))
2579                         goto do_associate;
2580                 else {
2581                         pr_debug("%s: AES/TKIP, but PSK not configured\n",
2582                                  __func__);
2583                         return ret;
2584                 }
2585         }
2586
2587 do_associate:
2588         ret = schedule_delayed_work(&wl->assoc_work, 0);
2589         pr_debug("%s: start association work %d\n", __func__, ret);
2590         return ret;
2591 }
2592
2593 /*
2594  * netdev handlers
2595  */
2596 static int gelic_wl_open(struct net_device *netdev)
2597 {
2598         struct gelic_card *card = netdev_card(netdev);
2599
2600         pr_debug("%s:->%p\n", __func__, netdev);
2601
2602         gelic_card_up(card);
2603
2604         /* try to associate */
2605         gelic_wl_try_associate(netdev);
2606
2607         netif_start_queue(netdev);
2608
2609         pr_debug("%s:<-\n", __func__);
2610         return 0;
2611 }
2612
2613 /*
2614  * reset state machine
2615  */
2616 static int gelic_wl_reset_state(struct gelic_wl_info *wl)
2617 {
2618         struct gelic_wl_scan_info *target;
2619         struct gelic_wl_scan_info *tmp;
2620
2621         /* empty scan list */
2622         list_for_each_entry_safe(target, tmp, &wl->network_list, list) {
2623                 list_move_tail(&target->list, &wl->network_free_list);
2624         }
2625         wl->scan_stat = GELIC_WL_SCAN_STAT_INIT;
2626
2627         /* clear configuration */
2628         wl->auth_method = GELIC_EURUS_AUTH_OPEN;
2629         wl->group_cipher_method = GELIC_WL_CIPHER_NONE;
2630         wl->pairwise_cipher_method = GELIC_WL_CIPHER_NONE;
2631         wl->wpa_level = GELIC_WL_WPA_LEVEL_NONE;
2632
2633         wl->key_enabled = 0;
2634         wl->current_key = 0;
2635
2636         wl->psk_type = GELIC_EURUS_WPA_PSK_PASSPHRASE;
2637         wl->psk_len = 0;
2638
2639         wl->essid_len = 0;
2640         memset(wl->essid, 0, sizeof(wl->essid));
2641         memset(wl->bssid, 0, sizeof(wl->bssid));
2642         memset(wl->active_bssid, 0, sizeof(wl->active_bssid));
2643
2644         wl->assoc_stat = GELIC_WL_ASSOC_STAT_DISCONN;
2645
2646         memset(&wl->iwstat, 0, sizeof(wl->iwstat));
2647         /* all status bit clear */
2648         wl->stat = 0;
2649         return 0;
2650 }
2651
2652 /*
2653  * Tell eurus to terminate association
2654  */
2655 static void gelic_wl_disconnect(struct net_device *netdev)
2656 {
2657         struct gelic_port *port = netdev_priv(netdev);
2658         struct gelic_wl_info *wl = port_wl(port);
2659         struct gelic_eurus_cmd *cmd;
2660
2661         /*
2662          * If scann process is running on chip,
2663          * further requests will be rejected
2664          */
2665         if (wl->scan_stat == GELIC_WL_SCAN_STAT_SCANNING)
2666                 wait_for_completion_timeout(&wl->scan_done, HZ);
2667
2668         cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_DISASSOC, NULL, 0);
2669         kfree(cmd);
2670         gelic_wl_send_iwap_event(wl, NULL);
2671 };
2672
2673 static int gelic_wl_stop(struct net_device *netdev)
2674 {
2675         struct gelic_port *port = netdev_priv(netdev);
2676         struct gelic_wl_info *wl = port_wl(port);
2677         struct gelic_card *card = netdev_card(netdev);
2678
2679         pr_debug("%s:<-\n", __func__);
2680
2681         /*
2682          * Cancel pending association work.
2683          * event work can run after netdev down
2684          */
2685         cancel_delayed_work(&wl->assoc_work);
2686
2687         if (wl->assoc_stat == GELIC_WL_ASSOC_STAT_ASSOCIATED)
2688                 gelic_wl_disconnect(netdev);
2689
2690         /* reset our state machine */
2691         gelic_wl_reset_state(wl);
2692
2693         netif_stop_queue(netdev);
2694
2695         gelic_card_down(card);
2696
2697         pr_debug("%s:->\n", __func__);
2698         return 0;
2699 }
2700
2701 /* -- */
2702
2703 static const struct net_device_ops gelic_wl_netdevice_ops = {
2704         .ndo_open = gelic_wl_open,
2705         .ndo_stop = gelic_wl_stop,
2706         .ndo_start_xmit = gelic_net_xmit,
2707         .ndo_set_multicast_list = gelic_net_set_multi,
2708         .ndo_change_mtu = gelic_net_change_mtu,
2709         .ndo_tx_timeout = gelic_net_tx_timeout,
2710         .ndo_validate_addr = eth_validate_addr,
2711 #ifdef CONFIG_NET_POLL_CONTROLLER
2712         .ndo_poll_controller = gelic_net_poll_controller,
2713 #endif
2714 };
2715
2716 static struct ethtool_ops gelic_wl_ethtool_ops = {
2717         .get_drvinfo    = gelic_net_get_drvinfo,
2718         .get_link       = gelic_wl_get_link,
2719         .get_tx_csum    = ethtool_op_get_tx_csum,
2720         .set_tx_csum    = ethtool_op_set_tx_csum,
2721         .get_rx_csum    = gelic_net_get_rx_csum,
2722         .set_rx_csum    = gelic_net_set_rx_csum,
2723 };
2724
2725 static void gelic_wl_setup_netdev_ops(struct net_device *netdev)
2726 {
2727         struct gelic_wl_info *wl;
2728         wl = port_wl(netdev_priv(netdev));
2729         BUG_ON(!wl);
2730         netdev->watchdog_timeo = GELIC_NET_WATCHDOG_TIMEOUT;
2731
2732         netdev->ethtool_ops = &gelic_wl_ethtool_ops;
2733         netdev->netdev_ops = &gelic_wl_netdevice_ops;
2734         netdev->wireless_data = &wl->wireless_data;
2735         netdev->wireless_handlers = &gelic_wl_wext_handler_def;
2736 }
2737
2738 /*
2739  * driver probe/remove
2740  */
2741 int gelic_wl_driver_probe(struct gelic_card *card)
2742 {
2743         int ret;
2744         struct net_device *netdev;
2745
2746         pr_debug("%s:start\n", __func__);
2747
2748         if (ps3_compare_firmware_version(1, 6, 0) < 0)
2749                 return 0;
2750         if (!card->vlan[GELIC_PORT_WIRELESS].tx)
2751                 return 0;
2752
2753         /* alloc netdevice for wireless */
2754         netdev = gelic_wl_alloc(card);
2755         if (!netdev)
2756                 return -ENOMEM;
2757
2758         /* setup net_device structure */
2759         SET_NETDEV_DEV(netdev, &card->dev->core);
2760         gelic_wl_setup_netdev_ops(netdev);
2761
2762         /* setup some of net_device and register it */
2763         ret = gelic_net_setup_netdev(netdev, card);
2764         if (ret)
2765                 goto fail_setup;
2766         card->netdev[GELIC_PORT_WIRELESS] = netdev;
2767
2768         /* add enable wireless interrupt */
2769         card->irq_mask |= GELIC_CARD_WLAN_EVENT_RECEIVED |
2770                 GELIC_CARD_WLAN_COMMAND_COMPLETED;
2771         /* to allow wireless commands while both interfaces are down */
2772         gelic_card_set_irq_mask(card, GELIC_CARD_WLAN_EVENT_RECEIVED |
2773                                 GELIC_CARD_WLAN_COMMAND_COMPLETED);
2774         pr_debug("%s:end\n", __func__);
2775         return 0;
2776
2777 fail_setup:
2778         gelic_wl_free(port_wl(netdev_port(netdev)));
2779
2780         return ret;
2781 }
2782
2783 int gelic_wl_driver_remove(struct gelic_card *card)
2784 {
2785         struct gelic_wl_info *wl;
2786         struct net_device *netdev;
2787
2788         pr_debug("%s:start\n", __func__);
2789
2790         if (ps3_compare_firmware_version(1, 6, 0) < 0)
2791                 return 0;
2792         if (!card->vlan[GELIC_PORT_WIRELESS].tx)
2793                 return 0;
2794
2795         netdev = card->netdev[GELIC_PORT_WIRELESS];
2796         wl = port_wl(netdev_priv(netdev));
2797
2798         /* if the interface was not up, but associated */
2799         if (wl->assoc_stat == GELIC_WL_ASSOC_STAT_ASSOCIATED)
2800                 gelic_wl_disconnect(netdev);
2801
2802         complete(&wl->cmd_done_intr);
2803
2804         /* cancel all work queue */
2805         cancel_delayed_work(&wl->assoc_work);
2806         cancel_delayed_work(&wl->event_work);
2807         flush_workqueue(wl->eurus_cmd_queue);
2808         flush_workqueue(wl->event_queue);
2809
2810         unregister_netdev(netdev);
2811
2812         /* disable wireless interrupt */
2813         pr_debug("%s: disable intr\n", __func__);
2814         card->irq_mask &= ~(GELIC_CARD_WLAN_EVENT_RECEIVED |
2815                             GELIC_CARD_WLAN_COMMAND_COMPLETED);
2816         /* free bss list, netdev*/
2817         gelic_wl_free(wl);
2818         pr_debug("%s:end\n", __func__);
2819         return 0;
2820 }