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