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