Merge branch 'master' of master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6
[linux-2.6] / drivers / net / ps3_gelic_wireless.c
1 /*
2  *  PS3 gelic network driver.
3  *
4  * Copyright (C) 2007 Sony Computer Entertainment Inc.
5  * Copyright 2007 Sony Corporation
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2
9  * as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20 #undef DEBUG
21
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24
25 #include <linux/etherdevice.h>
26 #include <linux/ethtool.h>
27 #include <linux/if_vlan.h>
28
29 #include <linux/in.h>
30 #include <linux/ip.h>
31 #include <linux/tcp.h>
32 #include <linux/wireless.h>
33 #include <linux/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                                      struct iw_request_info *info,
575                                      char *ev,
576                                      char *stop,
577                                      struct gelic_wl_scan_info *network)
578 {
579         struct iw_event iwe;
580         struct gelic_eurus_scan_info *scan = network->hwinfo;
581         char *tmp;
582         u8 rate;
583         unsigned int i, j, len;
584         u8 buf[MAX_WPA_IE_LEN];
585
586         pr_debug("%s: <-\n", __func__);
587
588         /* first entry should be AP's mac address */
589         iwe.cmd = SIOCGIWAP;
590         iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
591         memcpy(iwe.u.ap_addr.sa_data, &scan->bssid[2], ETH_ALEN);
592         ev = iwe_stream_add_event(info, ev, stop, &iwe, IW_EV_ADDR_LEN);
593
594         /* ESSID */
595         iwe.cmd = SIOCGIWESSID;
596         iwe.u.data.flags = 1;
597         iwe.u.data.length = strnlen(scan->essid, 32);
598         ev = iwe_stream_add_point(info, ev, stop, &iwe, scan->essid);
599
600         /* FREQUENCY */
601         iwe.cmd = SIOCGIWFREQ;
602         iwe.u.freq.m = be16_to_cpu(scan->channel);
603         iwe.u.freq.e = 0; /* table value in MHz */
604         iwe.u.freq.i = 0;
605         ev = iwe_stream_add_event(info, ev, stop, &iwe, IW_EV_FREQ_LEN);
606
607         /* RATES */
608         iwe.cmd = SIOCGIWRATE;
609         iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
610         /* to stuff multiple values in one event */
611         tmp = ev + iwe_stream_lcp_len(info);
612         /* put them in ascendant order (older is first) */
613         i = 0;
614         j = 0;
615         pr_debug("%s: rates=%d rate=%d\n", __func__,
616                  network->rate_len, network->rate_ext_len);
617         while (i < network->rate_len) {
618                 if (j < network->rate_ext_len &&
619                     ((scan->ext_rate[j] & 0x7f) < (scan->rate[i] & 0x7f)))
620                     rate = scan->ext_rate[j++] & 0x7f;
621                 else
622                     rate = scan->rate[i++] & 0x7f;
623                 iwe.u.bitrate.value = rate * 500000; /* 500kbps unit */
624                 tmp = iwe_stream_add_value(info, ev, tmp, stop, &iwe,
625                                            IW_EV_PARAM_LEN);
626         }
627         while (j < network->rate_ext_len) {
628                 iwe.u.bitrate.value = (scan->ext_rate[j++] & 0x7f) * 500000;
629                 tmp = iwe_stream_add_value(info, ev, tmp, stop, &iwe,
630                                            IW_EV_PARAM_LEN);
631         }
632         /* Check if we added any rate */
633         if (iwe_stream_lcp_len(info) < (tmp - ev))
634                 ev = tmp;
635
636         /* ENCODE */
637         iwe.cmd = SIOCGIWENCODE;
638         if (be16_to_cpu(scan->capability) & WLAN_CAPABILITY_PRIVACY)
639                 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
640         else
641                 iwe.u.data.flags = IW_ENCODE_DISABLED;
642         iwe.u.data.length = 0;
643         ev = iwe_stream_add_point(info, ev, stop, &iwe, scan->essid);
644
645         /* MODE */
646         iwe.cmd = SIOCGIWMODE;
647         if (be16_to_cpu(scan->capability) &
648             (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)) {
649                 if (be16_to_cpu(scan->capability) & WLAN_CAPABILITY_ESS)
650                         iwe.u.mode = IW_MODE_MASTER;
651                 else
652                         iwe.u.mode = IW_MODE_ADHOC;
653                 ev = iwe_stream_add_event(info, ev, stop, &iwe, IW_EV_UINT_LEN);
654         }
655
656         /* QUAL */
657         iwe.cmd = IWEVQUAL;
658         iwe.u.qual.updated  = IW_QUAL_ALL_UPDATED |
659                         IW_QUAL_QUAL_INVALID | IW_QUAL_NOISE_INVALID;
660         iwe.u.qual.level = be16_to_cpu(scan->rssi);
661         iwe.u.qual.qual = be16_to_cpu(scan->rssi);
662         iwe.u.qual.noise = 0;
663         ev  = iwe_stream_add_event(info, ev, stop, &iwe, IW_EV_QUAL_LEN);
664
665         /* RSN */
666         memset(&iwe, 0, sizeof(iwe));
667         if (be16_to_cpu(scan->size) <= sizeof(*scan)) {
668                 /* If wpa[2] capable station, synthesize IE and put it */
669                 len = gelic_wl_synthesize_ie(buf, scan);
670                 if (len) {
671                         iwe.cmd = IWEVGENIE;
672                         iwe.u.data.length = len;
673                         ev = iwe_stream_add_point(info, ev, stop, &iwe, buf);
674                 }
675         } else {
676                 /* this scan info has IE data */
677                 struct ie_info ie_info;
678                 size_t data_len;
679
680                 data_len = be16_to_cpu(scan->size) - sizeof(*scan);
681
682                 gelic_wl_parse_ie(scan->elements, data_len, &ie_info);
683
684                 if (ie_info.wpa.len && (ie_info.wpa.len <= sizeof(buf))) {
685                         memcpy(buf, ie_info.wpa.data, ie_info.wpa.len);
686                         iwe.cmd = IWEVGENIE;
687                         iwe.u.data.length = ie_info.wpa.len;
688                         ev = iwe_stream_add_point(info, ev, stop, &iwe, buf);
689                 }
690
691                 if (ie_info.rsn.len && (ie_info.rsn.len <= sizeof(buf))) {
692                         memset(&iwe, 0, sizeof(iwe));
693                         memcpy(buf, ie_info.rsn.data, ie_info.rsn.len);
694                         iwe.cmd = IWEVGENIE;
695                         iwe.u.data.length = ie_info.rsn.len;
696                         ev = iwe_stream_add_point(info, ev, stop, &iwe, buf);
697                 }
698         }
699
700         pr_debug("%s: ->\n", __func__);
701         return ev;
702 }
703
704
705 static int gelic_wl_get_scan(struct net_device *netdev,
706                              struct iw_request_info *info,
707                              union iwreq_data *wrqu, char *extra)
708 {
709         struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
710         struct gelic_wl_scan_info *scan_info;
711         char *ev = extra;
712         char *stop = ev + wrqu->data.length;
713         int ret = 0;
714         unsigned long this_time = jiffies;
715
716         pr_debug("%s: <-\n", __func__);
717         if (mutex_lock_interruptible(&wl->scan_lock))
718                 return -EAGAIN;
719
720         switch (wl->scan_stat) {
721         case GELIC_WL_SCAN_STAT_SCANNING:
722                 /* If a scan in progress, caller should call me again */
723                 ret = -EAGAIN;
724                 goto out;
725                 break;
726
727         case GELIC_WL_SCAN_STAT_INIT:
728                 /* last scan request failed or never issued */
729                 ret = -ENODEV;
730                 goto out;
731                 break;
732         case GELIC_WL_SCAN_STAT_GOT_LIST:
733                 /* ok, use current list */
734                 break;
735         }
736
737         list_for_each_entry(scan_info, &wl->network_list, list) {
738                 if (wl->scan_age == 0 ||
739                     time_after(scan_info->last_scanned + wl->scan_age,
740                                this_time))
741                         ev = gelic_wl_translate_scan(netdev, info,
742                                                      ev, stop,
743                                                      scan_info);
744                 else
745                         pr_debug("%s:entry too old\n", __func__);
746
747                 if (stop - ev <= IW_EV_ADDR_LEN) {
748                         ret = -E2BIG;
749                         goto out;
750                 }
751         }
752
753         wrqu->data.length = ev - extra;
754         wrqu->data.flags = 0;
755 out:
756         mutex_unlock(&wl->scan_lock);
757         pr_debug("%s: -> %d %d\n", __func__, ret, wrqu->data.length);
758         return ret;
759 }
760
761 #ifdef DEBUG
762 static void scan_list_dump(struct gelic_wl_info *wl)
763 {
764         struct gelic_wl_scan_info *scan_info;
765         int i;
766
767         i = 0;
768         list_for_each_entry(scan_info, &wl->network_list, list) {
769                 pr_debug("%s: item %d\n", __func__, i++);
770                 pr_debug("valid=%d eurusindex=%d last=%lx\n",
771                          scan_info->valid, scan_info->eurus_index,
772                          scan_info->last_scanned);
773                 pr_debug("r_len=%d r_ext_len=%d essid_len=%d\n",
774                          scan_info->rate_len, scan_info->rate_ext_len,
775                          scan_info->essid_len);
776                 /* -- */
777                 pr_debug("bssid=%pM\n", &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 long 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 long 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=%pM\n", __func__, wl->bssid);
1169         } else {
1170                 pr_debug("%s: clear bssid\n", __func__);
1171                 clear_bit(GELIC_WL_STAT_BSSID_SET, &wl->stat);
1172                 memset(wl->bssid, 0, ETH_ALEN);
1173         }
1174         spin_unlock_irqrestore(&wl->lock, irqflag);
1175         pr_debug("%s: ->\n", __func__);
1176         return 0;
1177 }
1178
1179 static int gelic_wl_get_ap(struct net_device *netdev,
1180                            struct iw_request_info *info,
1181                            union iwreq_data *data, char *extra)
1182 {
1183         struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
1184         unsigned long irqflag;
1185
1186         pr_debug("%s: <-\n", __func__);
1187         mutex_lock(&wl->assoc_stat_lock);
1188         spin_lock_irqsave(&wl->lock, irqflag);
1189         if (wl->assoc_stat == GELIC_WL_ASSOC_STAT_ASSOCIATED) {
1190                 data->ap_addr.sa_family = ARPHRD_ETHER;
1191                 memcpy(data->ap_addr.sa_data, wl->active_bssid,
1192                        ETH_ALEN);
1193         } else
1194                 memset(data->ap_addr.sa_data, 0, ETH_ALEN);
1195
1196         spin_unlock_irqrestore(&wl->lock, irqflag);
1197         mutex_unlock(&wl->assoc_stat_lock);
1198         pr_debug("%s: ->\n", __func__);
1199         return 0;
1200 }
1201
1202 /* SIOC{S,G}IWENCODEEXT */
1203 static int gelic_wl_set_encodeext(struct net_device *netdev,
1204                                   struct iw_request_info *info,
1205                                   union iwreq_data *data, char *extra)
1206 {
1207         struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
1208         struct iw_point *enc = &data->encoding;
1209         struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
1210         __u16 alg;
1211         __u16 flags;
1212         unsigned long irqflag;
1213         int key_index;
1214         int ret = 0;
1215
1216         pr_debug("%s: <- \n", __func__);
1217         flags = enc->flags & IW_ENCODE_FLAGS;
1218         alg = ext->alg;
1219         key_index = enc->flags & IW_ENCODE_INDEX;
1220
1221         pr_debug("%s: key_index = %d\n", __func__, key_index);
1222         pr_debug("%s: key_len = %d\n", __func__, enc->length);
1223         pr_debug("%s: flag=%x\n", __func__, enc->flags & IW_ENCODE_FLAGS);
1224         pr_debug("%s: ext_flag=%x\n", __func__, ext->ext_flags);
1225         pr_debug("%s: ext_key_len=%x\n", __func__, ext->key_len);
1226
1227         if (GELIC_WEP_KEYS < key_index)
1228                 return -EINVAL;
1229
1230         spin_lock_irqsave(&wl->lock, irqflag);
1231         if (key_index)
1232                 key_index--;
1233         else
1234                 key_index = wl->current_key;
1235
1236         if (!enc->length && (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY)) {
1237                 /* reques to change default key index */
1238                 pr_debug("%s: request to change default key to %d\n",
1239                          __func__, key_index);
1240                 wl->current_key = key_index;
1241                 goto done;
1242         }
1243
1244         if (alg == IW_ENCODE_ALG_NONE || (flags & IW_ENCODE_DISABLED)) {
1245                 pr_debug("%s: alg disabled\n", __func__);
1246                 wl->wpa_level = GELIC_WL_WPA_LEVEL_NONE;
1247                 wl->group_cipher_method = GELIC_WL_CIPHER_NONE;
1248                 wl->pairwise_cipher_method = GELIC_WL_CIPHER_NONE;
1249                 wl->auth_method = GELIC_EURUS_AUTH_OPEN; /* should be open */
1250         } else if (alg == IW_ENCODE_ALG_WEP) {
1251                 pr_debug("%s: WEP requested\n", __func__);
1252                 if (flags & IW_ENCODE_OPEN) {
1253                         pr_debug("%s: open key mode\n", __func__);
1254                         wl->auth_method = GELIC_EURUS_AUTH_OPEN;
1255                 }
1256                 if (flags & IW_ENCODE_RESTRICTED) {
1257                         pr_debug("%s: shared key mode\n", __func__);
1258                         wl->auth_method = GELIC_EURUS_AUTH_SHARED;
1259                 }
1260                 if (IW_ENCODING_TOKEN_MAX < ext->key_len) {
1261                         pr_info("%s: key is too long %d\n", __func__,
1262                                 ext->key_len);
1263                         ret = -EINVAL;
1264                         goto done;
1265                 }
1266                 /* OK, update the key */
1267                 wl->key_len[key_index] = ext->key_len;
1268                 memset(wl->key[key_index], 0, IW_ENCODING_TOKEN_MAX);
1269                 memcpy(wl->key[key_index], ext->key, ext->key_len);
1270                 set_bit(key_index, &wl->key_enabled);
1271                 /* remember wep info changed */
1272                 set_bit(GELIC_WL_STAT_CONFIGURED, &wl->stat);
1273         } else if (alg == IW_ENCODE_ALG_PMK) {
1274                 if (ext->key_len != WPA_PSK_LEN) {
1275                         pr_err("%s: PSK length wrong %d\n", __func__,
1276                                ext->key_len);
1277                         ret = -EINVAL;
1278                         goto done;
1279                 }
1280                 memset(wl->psk, 0, sizeof(wl->psk));
1281                 memcpy(wl->psk, ext->key, ext->key_len);
1282                 wl->psk_len = ext->key_len;
1283                 wl->psk_type = GELIC_EURUS_WPA_PSK_BIN;
1284                 /* remember PSK configured */
1285                 set_bit(GELIC_WL_STAT_WPA_PSK_SET, &wl->stat);
1286         }
1287 done:
1288         spin_unlock_irqrestore(&wl->lock, irqflag);
1289         pr_debug("%s: -> \n", __func__);
1290         return ret;
1291 }
1292
1293 static int gelic_wl_get_encodeext(struct net_device *netdev,
1294                                   struct iw_request_info *info,
1295                                   union iwreq_data *data, char *extra)
1296 {
1297         struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
1298         struct iw_point *enc = &data->encoding;
1299         struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
1300         unsigned long irqflag;
1301         int key_index;
1302         int ret = 0;
1303         int max_key_len;
1304
1305         pr_debug("%s: <- \n", __func__);
1306
1307         max_key_len = enc->length - sizeof(struct iw_encode_ext);
1308         if (max_key_len < 0)
1309                 return -EINVAL;
1310         key_index = enc->flags & IW_ENCODE_INDEX;
1311
1312         pr_debug("%s: key_index = %d\n", __func__, key_index);
1313         pr_debug("%s: key_len = %d\n", __func__, enc->length);
1314         pr_debug("%s: flag=%x\n", __func__, enc->flags & IW_ENCODE_FLAGS);
1315
1316         if (GELIC_WEP_KEYS < key_index)
1317                 return -EINVAL;
1318
1319         spin_lock_irqsave(&wl->lock, irqflag);
1320         if (key_index)
1321                 key_index--;
1322         else
1323                 key_index = wl->current_key;
1324
1325         memset(ext, 0, sizeof(struct iw_encode_ext));
1326         switch (wl->group_cipher_method) {
1327         case GELIC_WL_CIPHER_WEP:
1328                 ext->alg = IW_ENCODE_ALG_WEP;
1329                 enc->flags |= IW_ENCODE_ENABLED;
1330                 break;
1331         case GELIC_WL_CIPHER_TKIP:
1332                 ext->alg = IW_ENCODE_ALG_TKIP;
1333                 enc->flags |= IW_ENCODE_ENABLED;
1334                 break;
1335         case GELIC_WL_CIPHER_AES:
1336                 ext->alg = IW_ENCODE_ALG_CCMP;
1337                 enc->flags |= IW_ENCODE_ENABLED;
1338                 break;
1339         case GELIC_WL_CIPHER_NONE:
1340         default:
1341                 ext->alg = IW_ENCODE_ALG_NONE;
1342                 enc->flags |= IW_ENCODE_NOKEY;
1343                 break;
1344         }
1345
1346         if (!(enc->flags & IW_ENCODE_NOKEY)) {
1347                 if (max_key_len < wl->key_len[key_index]) {
1348                         ret = -E2BIG;
1349                         goto out;
1350                 }
1351                 if (test_bit(key_index, &wl->key_enabled))
1352                         memcpy(ext->key, wl->key[key_index],
1353                                wl->key_len[key_index]);
1354                 else
1355                         pr_debug("%s: disabled key requested ix=%d\n",
1356                                  __func__, key_index);
1357         }
1358 out:
1359         spin_unlock_irqrestore(&wl->lock, irqflag);
1360         pr_debug("%s: -> \n", __func__);
1361         return ret;
1362 }
1363 /* SIOC{S,G}IWMODE */
1364 static int gelic_wl_set_mode(struct net_device *netdev,
1365                              struct iw_request_info *info,
1366                              union iwreq_data *data, char *extra)
1367 {
1368         __u32 mode = data->mode;
1369         int ret;
1370
1371         pr_debug("%s: <- \n", __func__);
1372         if (mode == IW_MODE_INFRA)
1373                 ret = 0;
1374         else
1375                 ret = -EOPNOTSUPP;
1376         pr_debug("%s: -> %d\n", __func__, ret);
1377         return ret;
1378 }
1379
1380 static int gelic_wl_get_mode(struct net_device *netdev,
1381                              struct iw_request_info *info,
1382                              union iwreq_data *data, char *extra)
1383 {
1384         __u32 *mode = &data->mode;
1385         pr_debug("%s: <- \n", __func__);
1386         *mode = IW_MODE_INFRA;
1387         pr_debug("%s: ->\n", __func__);
1388         return 0;
1389 }
1390
1391 #ifdef CONFIG_GELIC_WIRELESS_OLD_PSK_INTERFACE
1392 /* SIOCIWFIRSTPRIV */
1393 static int hex2bin(u8 *str, u8 *bin, unsigned int len)
1394 {
1395         unsigned int i;
1396         static unsigned char *hex = "0123456789ABCDEF";
1397         unsigned char *p, *q;
1398         u8 tmp;
1399
1400         if (len != WPA_PSK_LEN * 2)
1401                 return -EINVAL;
1402
1403         for (i = 0; i < WPA_PSK_LEN * 2; i += 2) {
1404                 p = strchr(hex, toupper(str[i]));
1405                 q = strchr(hex, toupper(str[i + 1]));
1406                 if (!p || !q) {
1407                         pr_info("%s: unconvertible PSK digit=%d\n",
1408                                 __func__, i);
1409                         return -EINVAL;
1410                 }
1411                 tmp = ((p - hex) << 4) + (q - hex);
1412                 *bin++ = tmp;
1413         }
1414         return 0;
1415 };
1416
1417 static int gelic_wl_priv_set_psk(struct net_device *net_dev,
1418                                  struct iw_request_info *info,
1419                                  union iwreq_data *data, char *extra)
1420 {
1421         struct gelic_wl_info *wl = port_wl(netdev_priv(net_dev));
1422         unsigned int len;
1423         unsigned long irqflag;
1424         int ret = 0;
1425
1426         pr_debug("%s:<- len=%d\n", __func__, data->data.length);
1427         len = data->data.length - 1;
1428         if (len <= 2)
1429                 return -EINVAL;
1430
1431         spin_lock_irqsave(&wl->lock, irqflag);
1432         if (extra[0] == '"' && extra[len - 1] == '"') {
1433                 pr_debug("%s: passphrase mode\n", __func__);
1434                 /* pass phrase */
1435                 if (GELIC_WL_EURUS_PSK_MAX_LEN < (len - 2)) {
1436                         pr_info("%s: passphrase too long\n", __func__);
1437                         ret = -E2BIG;
1438                         goto out;
1439                 }
1440                 memset(wl->psk, 0, sizeof(wl->psk));
1441                 wl->psk_len = len - 2;
1442                 memcpy(wl->psk, &(extra[1]), wl->psk_len);
1443                 wl->psk_type = GELIC_EURUS_WPA_PSK_PASSPHRASE;
1444         } else {
1445                 ret = hex2bin(extra, wl->psk, len);
1446                 if (ret)
1447                         goto out;
1448                 wl->psk_len = WPA_PSK_LEN;
1449                 wl->psk_type = GELIC_EURUS_WPA_PSK_BIN;
1450         }
1451         set_bit(GELIC_WL_STAT_WPA_PSK_SET, &wl->stat);
1452 out:
1453         spin_unlock_irqrestore(&wl->lock, irqflag);
1454         pr_debug("%s:->\n", __func__);
1455         return ret;
1456 }
1457
1458 static int gelic_wl_priv_get_psk(struct net_device *net_dev,
1459                                  struct iw_request_info *info,
1460                                  union iwreq_data *data, char *extra)
1461 {
1462         struct gelic_wl_info *wl = port_wl(netdev_priv(net_dev));
1463         char *p;
1464         unsigned long irqflag;
1465         unsigned int i;
1466
1467         pr_debug("%s:<-\n", __func__);
1468         if (!capable(CAP_NET_ADMIN))
1469                 return -EPERM;
1470
1471         spin_lock_irqsave(&wl->lock, irqflag);
1472         p = extra;
1473         if (test_bit(GELIC_WL_STAT_WPA_PSK_SET, &wl->stat)) {
1474                 if (wl->psk_type == GELIC_EURUS_WPA_PSK_BIN) {
1475                         for (i = 0; i < wl->psk_len; i++) {
1476                                 sprintf(p, "%02xu", wl->psk[i]);
1477                                 p += 2;
1478                         }
1479                         *p = '\0';
1480                         data->data.length = wl->psk_len * 2;
1481                 } else {
1482                         *p++ = '"';
1483                         memcpy(p, wl->psk, wl->psk_len);
1484                         p += wl->psk_len;
1485                         *p++ = '"';
1486                         *p = '\0';
1487                         data->data.length = wl->psk_len + 2;
1488                 }
1489         } else
1490                 /* no psk set */
1491                 data->data.length = 0;
1492         spin_unlock_irqrestore(&wl->lock, irqflag);
1493         pr_debug("%s:-> %d\n", __func__, data->data.length);
1494         return 0;
1495 }
1496 #endif
1497
1498 /* SIOCGIWNICKN */
1499 static int gelic_wl_get_nick(struct net_device *net_dev,
1500                                   struct iw_request_info *info,
1501                                   union iwreq_data *data, char *extra)
1502 {
1503         strcpy(extra, "gelic_wl");
1504         data->data.length = strlen(extra);
1505         data->data.flags = 1;
1506         return 0;
1507 }
1508
1509
1510 /* --- */
1511
1512 static struct iw_statistics *gelic_wl_get_wireless_stats(
1513         struct net_device *netdev)
1514 {
1515
1516         struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
1517         struct gelic_eurus_cmd *cmd;
1518         struct iw_statistics *is;
1519         struct gelic_eurus_rssi_info *rssi;
1520         void *buf;
1521
1522         pr_debug("%s: <-\n", __func__);
1523
1524         buf = (void *)__get_free_page(GFP_KERNEL);
1525         if (!buf)
1526                 return NULL;
1527
1528         is = &wl->iwstat;
1529         memset(is, 0, sizeof(*is));
1530         cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_GET_RSSI_CFG,
1531                                    buf, sizeof(*rssi));
1532         if (cmd && !cmd->status && !cmd->cmd_status) {
1533                 rssi = buf;
1534                 is->qual.level = be16_to_cpu(rssi->rssi);
1535                 is->qual.updated = IW_QUAL_LEVEL_UPDATED |
1536                         IW_QUAL_QUAL_INVALID | IW_QUAL_NOISE_INVALID;
1537         } else
1538                 /* not associated */
1539                 is->qual.updated = IW_QUAL_ALL_INVALID;
1540
1541         kfree(cmd);
1542         free_page((unsigned long)buf);
1543         pr_debug("%s: ->\n", __func__);
1544         return is;
1545 }
1546
1547 /*
1548  *  scanning helpers
1549  */
1550 static int gelic_wl_start_scan(struct gelic_wl_info *wl, int always_scan,
1551                                u8 *essid, size_t essid_len)
1552 {
1553         struct gelic_eurus_cmd *cmd;
1554         int ret = 0;
1555         void *buf = NULL;
1556         size_t len;
1557
1558         pr_debug("%s: <- always=%d\n", __func__, always_scan);
1559         if (mutex_lock_interruptible(&wl->scan_lock))
1560                 return -ERESTARTSYS;
1561
1562         /*
1563          * If already a scan in progress, do not trigger more
1564          */
1565         if (wl->scan_stat == GELIC_WL_SCAN_STAT_SCANNING) {
1566                 pr_debug("%s: scanning now\n", __func__);
1567                 goto out;
1568         }
1569
1570         init_completion(&wl->scan_done);
1571         /*
1572          * If we have already a bss list, don't try to get new
1573          */
1574         if (!always_scan && wl->scan_stat == GELIC_WL_SCAN_STAT_GOT_LIST) {
1575                 pr_debug("%s: already has the list\n", __func__);
1576                 complete(&wl->scan_done);
1577                 goto out;
1578         }
1579
1580         /* ESSID scan ? */
1581         if (essid_len && essid) {
1582                 buf = (void *)__get_free_page(GFP_KERNEL);
1583                 if (!buf) {
1584                         ret = -ENOMEM;
1585                         goto out;
1586                 }
1587                 len = IW_ESSID_MAX_SIZE; /* hypervisor always requires 32 */
1588                 memset(buf, 0, len);
1589                 memcpy(buf, essid, essid_len);
1590                 pr_debug("%s: essid scan='%s'\n", __func__, (char *)buf);
1591         } else
1592                 len = 0;
1593
1594         /*
1595          * issue start scan request
1596          */
1597         wl->scan_stat = GELIC_WL_SCAN_STAT_SCANNING;
1598         cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_START_SCAN,
1599                                    buf, len);
1600         if (!cmd || cmd->status || cmd->cmd_status) {
1601                 wl->scan_stat = GELIC_WL_SCAN_STAT_INIT;
1602                 complete(&wl->scan_done);
1603                 ret = -ENOMEM;
1604                 goto out;
1605         }
1606         kfree(cmd);
1607 out:
1608         free_page((unsigned long)buf);
1609         mutex_unlock(&wl->scan_lock);
1610         pr_debug("%s: ->\n", __func__);
1611         return ret;
1612 }
1613
1614 /*
1615  * retrieve scan result from the chip (hypervisor)
1616  * this function is invoked by schedule work.
1617  */
1618 static void gelic_wl_scan_complete_event(struct gelic_wl_info *wl)
1619 {
1620         struct gelic_eurus_cmd *cmd = NULL;
1621         struct gelic_wl_scan_info *target, *tmp;
1622         struct gelic_wl_scan_info *oldest = NULL;
1623         struct gelic_eurus_scan_info *scan_info;
1624         unsigned int scan_info_size;
1625         union iwreq_data data;
1626         unsigned long this_time = jiffies;
1627         unsigned int data_len, i, found, r;
1628         void *buf;
1629
1630         pr_debug("%s:start\n", __func__);
1631         mutex_lock(&wl->scan_lock);
1632
1633         buf = (void *)__get_free_page(GFP_KERNEL);
1634         if (!buf) {
1635                 pr_info("%s: scan buffer alloc failed\n", __func__);
1636                 goto out;
1637         }
1638
1639         if (wl->scan_stat != GELIC_WL_SCAN_STAT_SCANNING) {
1640                 /*
1641                  * stop() may be called while scanning, ignore result
1642                  */
1643                 pr_debug("%s: scan complete when stat != scanning(%d)\n",
1644                          __func__, wl->scan_stat);
1645                 goto out;
1646         }
1647
1648         cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_GET_SCAN,
1649                                    buf, PAGE_SIZE);
1650         if (!cmd || cmd->status || cmd->cmd_status) {
1651                 wl->scan_stat = GELIC_WL_SCAN_STAT_INIT;
1652                 pr_info("%s:cmd failed\n", __func__);
1653                 kfree(cmd);
1654                 goto out;
1655         }
1656         data_len = cmd->size;
1657         pr_debug("%s: data_len = %d\n", __func__, data_len);
1658         kfree(cmd);
1659
1660         /* OK, bss list retrieved */
1661         wl->scan_stat = GELIC_WL_SCAN_STAT_GOT_LIST;
1662
1663         /* mark all entries are old */
1664         list_for_each_entry_safe(target, tmp, &wl->network_list, list) {
1665                 target->valid = 0;
1666                 /* expire too old entries */
1667                 if (time_before(target->last_scanned + wl->scan_age,
1668                                 this_time)) {
1669                         kfree(target->hwinfo);
1670                         target->hwinfo = NULL;
1671                         list_move_tail(&target->list, &wl->network_free_list);
1672                 }
1673         }
1674
1675         /* put them in the newtork_list */
1676         for (i = 0, scan_info_size = 0, scan_info = buf;
1677              scan_info_size < data_len;
1678              i++, scan_info_size += be16_to_cpu(scan_info->size),
1679              scan_info = (void *)scan_info + be16_to_cpu(scan_info->size)) {
1680                 pr_debug("%s:size=%d bssid=%pM scan_info=%p\n", __func__,
1681                          be16_to_cpu(scan_info->size),
1682                          &scan_info->bssid[2], scan_info);
1683
1684                 /*
1685                  * The wireless firmware may return invalid channel 0 and/or
1686                  * invalid rate if the AP emits zero length SSID ie. As this
1687                  * scan information is useless, ignore it
1688                  */
1689                 if (!be16_to_cpu(scan_info->channel) || !scan_info->rate[0]) {
1690                         pr_debug("%s: invalid scan info\n", __func__);
1691                         continue;
1692                 }
1693
1694                 found = 0;
1695                 oldest = NULL;
1696                 list_for_each_entry(target, &wl->network_list, list) {
1697                         if (!compare_ether_addr(&target->hwinfo->bssid[2],
1698                                                 &scan_info->bssid[2])) {
1699                                 found = 1;
1700                                 pr_debug("%s: same BBS found scanned list\n",
1701                                          __func__);
1702                                 break;
1703                         }
1704                         if (!oldest ||
1705                             (target->last_scanned < oldest->last_scanned))
1706                                 oldest = target;
1707                 }
1708
1709                 if (!found) {
1710                         /* not found in the list */
1711                         if (list_empty(&wl->network_free_list)) {
1712                                 /* expire oldest */
1713                                 target = oldest;
1714                         } else {
1715                                 target = list_entry(wl->network_free_list.next,
1716                                                     struct gelic_wl_scan_info,
1717                                                     list);
1718                         }
1719                 }
1720
1721                 /* update the item */
1722                 target->last_scanned = this_time;
1723                 target->valid = 1;
1724                 target->eurus_index = i;
1725                 kfree(target->hwinfo);
1726                 target->hwinfo = kzalloc(be16_to_cpu(scan_info->size),
1727                                          GFP_KERNEL);
1728                 if (!target->hwinfo) {
1729                         pr_info("%s: kzalloc failed\n", __func__);
1730                         continue;
1731                 }
1732                 /* copy hw scan info */
1733                 memcpy(target->hwinfo, scan_info, scan_info->size);
1734                 target->essid_len = strnlen(scan_info->essid,
1735                                             sizeof(scan_info->essid));
1736                 target->rate_len = 0;
1737                 for (r = 0; r < MAX_RATES_LENGTH; r++)
1738                         if (scan_info->rate[r])
1739                                 target->rate_len++;
1740                 if (8 < target->rate_len)
1741                         pr_info("%s: AP returns %d rates\n", __func__,
1742                                 target->rate_len);
1743                 target->rate_ext_len = 0;
1744                 for (r = 0; r < MAX_RATES_EX_LENGTH; r++)
1745                         if (scan_info->ext_rate[r])
1746                                 target->rate_ext_len++;
1747                 list_move_tail(&target->list, &wl->network_list);
1748         }
1749         memset(&data, 0, sizeof(data));
1750         wireless_send_event(port_to_netdev(wl_port(wl)), SIOCGIWSCAN, &data,
1751                             NULL);
1752 out:
1753         free_page((unsigned long)buf);
1754         complete(&wl->scan_done);
1755         mutex_unlock(&wl->scan_lock);
1756         pr_debug("%s:end\n", __func__);
1757 }
1758
1759 /*
1760  * Select an appropriate bss from current scan list regarding
1761  * current settings from userspace.
1762  * The caller must hold wl->scan_lock,
1763  * and on the state of wl->scan_state == GELIC_WL_SCAN_GOT_LIST
1764  */
1765 static void update_best(struct gelic_wl_scan_info **best,
1766                         struct gelic_wl_scan_info *candid,
1767                         int *best_weight,
1768                         int *weight)
1769 {
1770         if (*best_weight < ++(*weight)) {
1771                 *best_weight = *weight;
1772                 *best = candid;
1773         }
1774 }
1775
1776 static
1777 struct gelic_wl_scan_info *gelic_wl_find_best_bss(struct gelic_wl_info *wl)
1778 {
1779         struct gelic_wl_scan_info *scan_info;
1780         struct gelic_wl_scan_info *best_bss;
1781         int weight, best_weight;
1782         u16 security;
1783
1784         pr_debug("%s: <-\n", __func__);
1785
1786         best_bss = NULL;
1787         best_weight = 0;
1788
1789         list_for_each_entry(scan_info, &wl->network_list, list) {
1790                 pr_debug("%s: station %p\n", __func__, scan_info);
1791
1792                 if (!scan_info->valid) {
1793                         pr_debug("%s: station invalid\n", __func__);
1794                         continue;
1795                 }
1796
1797                 /* If bss specified, check it only */
1798                 if (test_bit(GELIC_WL_STAT_BSSID_SET, &wl->stat)) {
1799                         if (!compare_ether_addr(&scan_info->hwinfo->bssid[2],
1800                                                 wl->bssid)) {
1801                                 best_bss = scan_info;
1802                                 pr_debug("%s: bssid matched\n", __func__);
1803                                 break;
1804                         } else {
1805                                 pr_debug("%s: bssid unmached\n", __func__);
1806                                 continue;
1807                         }
1808                 }
1809
1810                 weight = 0;
1811
1812                 /* security */
1813                 security = be16_to_cpu(scan_info->hwinfo->security) &
1814                         GELIC_EURUS_SCAN_SEC_MASK;
1815                 if (wl->wpa_level == GELIC_WL_WPA_LEVEL_WPA2) {
1816                         if (security == GELIC_EURUS_SCAN_SEC_WPA2)
1817                                 update_best(&best_bss, scan_info,
1818                                             &best_weight, &weight);
1819                         else
1820                                 continue;
1821                 } else if (wl->wpa_level == GELIC_WL_WPA_LEVEL_WPA) {
1822                         if (security == GELIC_EURUS_SCAN_SEC_WPA)
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_NONE &&
1828                            wl->group_cipher_method == GELIC_WL_CIPHER_WEP) {
1829                         if (security == GELIC_EURUS_SCAN_SEC_WEP)
1830                                 update_best(&best_bss, scan_info,
1831                                             &best_weight, &weight);
1832                         else
1833                                 continue;
1834                 }
1835
1836                 /* If ESSID is set, check it */
1837                 if (test_bit(GELIC_WL_STAT_ESSID_SET, &wl->stat)) {
1838                         if ((scan_info->essid_len == wl->essid_len) &&
1839                             !strncmp(wl->essid,
1840                                      scan_info->hwinfo->essid,
1841                                      scan_info->essid_len))
1842                                 update_best(&best_bss, scan_info,
1843                                             &best_weight, &weight);
1844                         else
1845                                 continue;
1846                 }
1847         }
1848
1849 #ifdef DEBUG
1850         pr_debug("%s: -> bss=%p\n", __func__, best_bss);
1851         if (best_bss) {
1852                 pr_debug("%s:addr=%pM\n", __func__,
1853                          &best_bss->hwinfo->bssid[2]);
1854         }
1855 #endif
1856         return best_bss;
1857 }
1858
1859 /*
1860  * Setup WEP configuration to the chip
1861  * The caller must hold wl->scan_lock,
1862  * and on the state of wl->scan_state == GELIC_WL_SCAN_GOT_LIST
1863  */
1864 static int gelic_wl_do_wep_setup(struct gelic_wl_info *wl)
1865 {
1866         unsigned int i;
1867         struct gelic_eurus_wep_cfg *wep;
1868         struct gelic_eurus_cmd *cmd;
1869         int wep104 = 0;
1870         int have_key = 0;
1871         int ret = 0;
1872
1873         pr_debug("%s: <-\n", __func__);
1874         /* we can assume no one should uses the buffer */
1875         wep = (struct gelic_eurus_wep_cfg *)__get_free_page(GFP_KERNEL);
1876         if (!wep)
1877                 return -ENOMEM;
1878
1879         memset(wep, 0, sizeof(*wep));
1880
1881         if (wl->group_cipher_method == GELIC_WL_CIPHER_WEP) {
1882                 pr_debug("%s: WEP mode\n", __func__);
1883                 for (i = 0; i < GELIC_WEP_KEYS; i++) {
1884                         if (!test_bit(i, &wl->key_enabled))
1885                                 continue;
1886
1887                         pr_debug("%s: key#%d enabled\n", __func__, i);
1888                         have_key = 1;
1889                         if (wl->key_len[i] == 13)
1890                                 wep104 = 1;
1891                         else if (wl->key_len[i] != 5) {
1892                                 pr_info("%s: wrong wep key[%d]=%d\n",
1893                                         __func__, i, wl->key_len[i]);
1894                                 ret = -EINVAL;
1895                                 goto out;
1896                         }
1897                         memcpy(wep->key[i], wl->key[i], wl->key_len[i]);
1898                 }
1899
1900                 if (!have_key) {
1901                         pr_info("%s: all wep key disabled\n", __func__);
1902                         ret = -EINVAL;
1903                         goto out;
1904                 }
1905
1906                 if (wep104) {
1907                         pr_debug("%s: 104bit key\n", __func__);
1908                         wep->security = cpu_to_be16(GELIC_EURUS_WEP_SEC_104BIT);
1909                 } else {
1910                         pr_debug("%s: 40bit key\n", __func__);
1911                         wep->security = cpu_to_be16(GELIC_EURUS_WEP_SEC_40BIT);
1912                 }
1913         } else {
1914                 pr_debug("%s: NO encryption\n", __func__);
1915                 wep->security = cpu_to_be16(GELIC_EURUS_WEP_SEC_NONE);
1916         }
1917
1918         /* issue wep setup */
1919         cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_SET_WEP_CFG,
1920                                    wep, sizeof(*wep));
1921         if (!cmd)
1922                 ret = -ENOMEM;
1923         else if (cmd->status || cmd->cmd_status)
1924                 ret = -ENXIO;
1925
1926         kfree(cmd);
1927 out:
1928         free_page((unsigned long)wep);
1929         pr_debug("%s: ->\n", __func__);
1930         return ret;
1931 }
1932
1933 #ifdef DEBUG
1934 static const char *wpasecstr(enum gelic_eurus_wpa_security sec)
1935 {
1936         switch (sec) {
1937         case GELIC_EURUS_WPA_SEC_NONE:
1938                 return "NONE";
1939                 break;
1940         case GELIC_EURUS_WPA_SEC_WPA_TKIP_TKIP:
1941                 return "WPA_TKIP_TKIP";
1942                 break;
1943         case GELIC_EURUS_WPA_SEC_WPA_TKIP_AES:
1944                 return "WPA_TKIP_AES";
1945                 break;
1946         case GELIC_EURUS_WPA_SEC_WPA_AES_AES:
1947                 return "WPA_AES_AES";
1948                 break;
1949         case GELIC_EURUS_WPA_SEC_WPA2_TKIP_TKIP:
1950                 return "WPA2_TKIP_TKIP";
1951                 break;
1952         case GELIC_EURUS_WPA_SEC_WPA2_TKIP_AES:
1953                 return "WPA2_TKIP_AES";
1954                 break;
1955         case GELIC_EURUS_WPA_SEC_WPA2_AES_AES:
1956                 return "WPA2_AES_AES";
1957                 break;
1958         }
1959         return "";
1960 };
1961 #endif
1962
1963 static int gelic_wl_do_wpa_setup(struct gelic_wl_info *wl)
1964 {
1965         struct gelic_eurus_wpa_cfg *wpa;
1966         struct gelic_eurus_cmd *cmd;
1967         u16 security;
1968         int ret = 0;
1969
1970         pr_debug("%s: <-\n", __func__);
1971         /* we can assume no one should uses the buffer */
1972         wpa = (struct gelic_eurus_wpa_cfg *)__get_free_page(GFP_KERNEL);
1973         if (!wpa)
1974                 return -ENOMEM;
1975
1976         memset(wpa, 0, sizeof(*wpa));
1977
1978         if (!test_bit(GELIC_WL_STAT_WPA_PSK_SET, &wl->stat))
1979                 pr_info("%s: PSK not configured yet\n", __func__);
1980
1981         /* copy key */
1982         memcpy(wpa->psk, wl->psk, wl->psk_len);
1983
1984         /* set security level */
1985         if (wl->wpa_level == GELIC_WL_WPA_LEVEL_WPA2) {
1986                 if (wl->group_cipher_method == GELIC_WL_CIPHER_AES) {
1987                         security = GELIC_EURUS_WPA_SEC_WPA2_AES_AES;
1988                 } else {
1989                         if (wl->pairwise_cipher_method == GELIC_WL_CIPHER_AES &&
1990                             precise_ie())
1991                                 security = GELIC_EURUS_WPA_SEC_WPA2_TKIP_AES;
1992                         else
1993                                 security = GELIC_EURUS_WPA_SEC_WPA2_TKIP_TKIP;
1994                 }
1995         } else {
1996                 if (wl->group_cipher_method == GELIC_WL_CIPHER_AES) {
1997                         security = GELIC_EURUS_WPA_SEC_WPA_AES_AES;
1998                 } else {
1999                         if (wl->pairwise_cipher_method == GELIC_WL_CIPHER_AES &&
2000                             precise_ie())
2001                                 security = GELIC_EURUS_WPA_SEC_WPA_TKIP_AES;
2002                         else
2003                                 security = GELIC_EURUS_WPA_SEC_WPA_TKIP_TKIP;
2004                 }
2005         }
2006         wpa->security = cpu_to_be16(security);
2007
2008         /* PSK type */
2009         wpa->psk_type = cpu_to_be16(wl->psk_type);
2010 #ifdef DEBUG
2011         pr_debug("%s: sec=%s psktype=%s\nn", __func__,
2012                  wpasecstr(wpa->security),
2013                  (wpa->psk_type == GELIC_EURUS_WPA_PSK_BIN) ?
2014                  "BIN" : "passphrase");
2015 #if 0
2016         /*
2017          * don't enable here if you plan to submit
2018          * the debug log because this dumps your precious
2019          * passphrase/key.
2020          */
2021         pr_debug("%s: psk=%s\n",
2022                  (wpa->psk_type == GELIC_EURUS_WPA_PSK_BIN) ?
2023                  (char *)"N/A" : (char *)wpa->psk);
2024 #endif
2025 #endif
2026         /* issue wpa setup */
2027         cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_SET_WPA_CFG,
2028                                    wpa, sizeof(*wpa));
2029         if (!cmd)
2030                 ret = -ENOMEM;
2031         else if (cmd->status || cmd->cmd_status)
2032                 ret = -ENXIO;
2033         kfree(cmd);
2034         free_page((unsigned long)wpa);
2035         pr_debug("%s: --> %d\n", __func__, ret);
2036         return ret;
2037 }
2038
2039 /*
2040  * Start association. caller must hold assoc_stat_lock
2041  */
2042 static int gelic_wl_associate_bss(struct gelic_wl_info *wl,
2043                                   struct gelic_wl_scan_info *bss)
2044 {
2045         struct gelic_eurus_cmd *cmd;
2046         struct gelic_eurus_common_cfg *common;
2047         int ret = 0;
2048         unsigned long rc;
2049
2050         pr_debug("%s: <-\n", __func__);
2051
2052         /* do common config */
2053         common = (struct gelic_eurus_common_cfg *)__get_free_page(GFP_KERNEL);
2054         if (!common)
2055                 return -ENOMEM;
2056
2057         memset(common, 0, sizeof(*common));
2058         common->bss_type = cpu_to_be16(GELIC_EURUS_BSS_INFRA);
2059         common->op_mode = cpu_to_be16(GELIC_EURUS_OPMODE_11BG);
2060
2061         common->scan_index = cpu_to_be16(bss->eurus_index);
2062         switch (wl->auth_method) {
2063         case GELIC_EURUS_AUTH_OPEN:
2064                 common->auth_method = cpu_to_be16(GELIC_EURUS_AUTH_OPEN);
2065                 break;
2066         case GELIC_EURUS_AUTH_SHARED:
2067                 common->auth_method = cpu_to_be16(GELIC_EURUS_AUTH_SHARED);
2068                 break;
2069         }
2070
2071 #ifdef DEBUG
2072         scan_list_dump(wl);
2073 #endif
2074         pr_debug("%s: common cfg index=%d bsstype=%d auth=%d\n", __func__,
2075                  be16_to_cpu(common->scan_index),
2076                  be16_to_cpu(common->bss_type),
2077                  be16_to_cpu(common->auth_method));
2078
2079         cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_SET_COMMON_CFG,
2080                                    common, sizeof(*common));
2081         if (!cmd || cmd->status || cmd->cmd_status) {
2082                 ret = -ENOMEM;
2083                 kfree(cmd);
2084                 goto out;
2085         }
2086         kfree(cmd);
2087
2088         /* WEP/WPA */
2089         switch (wl->wpa_level) {
2090         case GELIC_WL_WPA_LEVEL_NONE:
2091                 /* If WEP or no security, setup WEP config */
2092                 ret = gelic_wl_do_wep_setup(wl);
2093                 break;
2094         case GELIC_WL_WPA_LEVEL_WPA:
2095         case GELIC_WL_WPA_LEVEL_WPA2:
2096                 ret = gelic_wl_do_wpa_setup(wl);
2097                 break;
2098         };
2099
2100         if (ret) {
2101                 pr_debug("%s: WEP/WPA setup failed %d\n", __func__,
2102                          ret);
2103         }
2104
2105         /* start association */
2106         init_completion(&wl->assoc_done);
2107         wl->assoc_stat = GELIC_WL_ASSOC_STAT_ASSOCIATING;
2108         cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_ASSOC,
2109                                    NULL, 0);
2110         if (!cmd || cmd->status || cmd->cmd_status) {
2111                 pr_debug("%s: assoc request failed\n", __func__);
2112                 wl->assoc_stat = GELIC_WL_ASSOC_STAT_DISCONN;
2113                 kfree(cmd);
2114                 ret = -ENOMEM;
2115                 gelic_wl_send_iwap_event(wl, NULL);
2116                 goto out;
2117         }
2118         kfree(cmd);
2119
2120         /* wait for connected event */
2121         rc = wait_for_completion_timeout(&wl->assoc_done, HZ * 4);/*FIXME*/
2122
2123         if (!rc) {
2124                 /* timeouted.  Maybe key or cyrpt mode is wrong */
2125                 pr_info("%s: connect timeout \n", __func__);
2126                 cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_DISASSOC,
2127                                            NULL, 0);
2128                 kfree(cmd);
2129                 wl->assoc_stat = GELIC_WL_ASSOC_STAT_DISCONN;
2130                 gelic_wl_send_iwap_event(wl, NULL);
2131                 ret = -ENXIO;
2132         } else {
2133                 wl->assoc_stat = GELIC_WL_ASSOC_STAT_ASSOCIATED;
2134                 /* copy bssid */
2135                 memcpy(wl->active_bssid, &bss->hwinfo->bssid[2], ETH_ALEN);
2136
2137                 /* send connect event */
2138                 gelic_wl_send_iwap_event(wl, wl->active_bssid);
2139                 pr_info("%s: connected\n", __func__);
2140         }
2141 out:
2142         free_page((unsigned long)common);
2143         pr_debug("%s: ->\n", __func__);
2144         return ret;
2145 }
2146
2147 /*
2148  * connected event
2149  */
2150 static void gelic_wl_connected_event(struct gelic_wl_info *wl,
2151                                      u64 event)
2152 {
2153         u64 desired_event = 0;
2154
2155         switch (wl->wpa_level) {
2156         case GELIC_WL_WPA_LEVEL_NONE:
2157                 desired_event = GELIC_LV1_WL_EVENT_CONNECTED;
2158                 break;
2159         case GELIC_WL_WPA_LEVEL_WPA:
2160         case GELIC_WL_WPA_LEVEL_WPA2:
2161                 desired_event = GELIC_LV1_WL_EVENT_WPA_CONNECTED;
2162                 break;
2163         }
2164
2165         if (desired_event == event) {
2166                 pr_debug("%s: completed \n", __func__);
2167                 complete(&wl->assoc_done);
2168                 netif_carrier_on(port_to_netdev(wl_port(wl)));
2169         } else
2170                 pr_debug("%s: event %#lx under wpa\n",
2171                                  __func__, event);
2172 }
2173
2174 /*
2175  * disconnect event
2176  */
2177 static void gelic_wl_disconnect_event(struct gelic_wl_info *wl,
2178                                       u64 event)
2179 {
2180         struct gelic_eurus_cmd *cmd;
2181         int lock;
2182
2183         /*
2184          * If we fall here in the middle of association,
2185          * associate_bss() should be waiting for complation of
2186          * wl->assoc_done.
2187          * As it waits with timeout, just leave assoc_done
2188          * uncompleted, then it terminates with timeout
2189          */
2190         if (!mutex_trylock(&wl->assoc_stat_lock)) {
2191                 pr_debug("%s: already locked\n", __func__);
2192                 lock = 0;
2193         } else {
2194                 pr_debug("%s: obtain lock\n", __func__);
2195                 lock = 1;
2196         }
2197
2198         cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_DISASSOC, NULL, 0);
2199         kfree(cmd);
2200
2201         /* send disconnected event to the supplicant */
2202         if (wl->assoc_stat == GELIC_WL_ASSOC_STAT_ASSOCIATED)
2203                 gelic_wl_send_iwap_event(wl, NULL);
2204
2205         wl->assoc_stat = GELIC_WL_ASSOC_STAT_DISCONN;
2206         netif_carrier_off(port_to_netdev(wl_port(wl)));
2207
2208         if (lock)
2209                 mutex_unlock(&wl->assoc_stat_lock);
2210 }
2211 /*
2212  * event worker
2213  */
2214 #ifdef DEBUG
2215 static const char *eventstr(enum gelic_lv1_wl_event event)
2216 {
2217         static char buf[32];
2218         char *ret;
2219         if (event & GELIC_LV1_WL_EVENT_DEVICE_READY)
2220                 ret = "EURUS_READY";
2221         else if (event & GELIC_LV1_WL_EVENT_SCAN_COMPLETED)
2222                 ret = "SCAN_COMPLETED";
2223         else if (event & GELIC_LV1_WL_EVENT_DEAUTH)
2224                 ret = "DEAUTH";
2225         else if (event & GELIC_LV1_WL_EVENT_BEACON_LOST)
2226                 ret = "BEACON_LOST";
2227         else if (event & GELIC_LV1_WL_EVENT_CONNECTED)
2228                 ret = "CONNECTED";
2229         else if (event & GELIC_LV1_WL_EVENT_WPA_CONNECTED)
2230                 ret = "WPA_CONNECTED";
2231         else if (event & GELIC_LV1_WL_EVENT_WPA_ERROR)
2232                 ret = "WPA_ERROR";
2233         else {
2234                 sprintf(buf, "Unknown(%#x)", event);
2235                 ret = buf;
2236         }
2237         return ret;
2238 }
2239 #else
2240 static const char *eventstr(enum gelic_lv1_wl_event event)
2241 {
2242         return NULL;
2243 }
2244 #endif
2245 static void gelic_wl_event_worker(struct work_struct *work)
2246 {
2247         struct gelic_wl_info *wl;
2248         struct gelic_port *port;
2249         u64 event, tmp;
2250         int status;
2251
2252         pr_debug("%s:start\n", __func__);
2253         wl = container_of(work, struct gelic_wl_info, event_work.work);
2254         port = wl_port(wl);
2255         while (1) {
2256                 status = lv1_net_control(bus_id(port->card), dev_id(port->card),
2257                                          GELIC_LV1_GET_WLAN_EVENT, 0, 0, 0,
2258                                          &event, &tmp);
2259                 if (status) {
2260                         if (status != LV1_NO_ENTRY)
2261                                 pr_debug("%s:wlan event failed %d\n",
2262                                          __func__, status);
2263                         /* got all events */
2264                         pr_debug("%s:end\n", __func__);
2265                         return;
2266                 }
2267                 pr_debug("%s: event=%s\n", __func__, eventstr(event));
2268                 switch (event) {
2269                 case GELIC_LV1_WL_EVENT_SCAN_COMPLETED:
2270                         gelic_wl_scan_complete_event(wl);
2271                         break;
2272                 case GELIC_LV1_WL_EVENT_BEACON_LOST:
2273                 case GELIC_LV1_WL_EVENT_DEAUTH:
2274                         gelic_wl_disconnect_event(wl, event);
2275                         break;
2276                 case GELIC_LV1_WL_EVENT_CONNECTED:
2277                 case GELIC_LV1_WL_EVENT_WPA_CONNECTED:
2278                         gelic_wl_connected_event(wl, event);
2279                         break;
2280                 default:
2281                         break;
2282                 }
2283         } /* while */
2284 }
2285 /*
2286  * association worker
2287  */
2288 static void gelic_wl_assoc_worker(struct work_struct *work)
2289 {
2290         struct gelic_wl_info *wl;
2291
2292         struct gelic_wl_scan_info *best_bss;
2293         int ret;
2294         unsigned long irqflag;
2295         u8 *essid;
2296         size_t essid_len;
2297
2298         wl = container_of(work, struct gelic_wl_info, assoc_work.work);
2299
2300         mutex_lock(&wl->assoc_stat_lock);
2301
2302         if (wl->assoc_stat != GELIC_WL_ASSOC_STAT_DISCONN)
2303                 goto out;
2304
2305         spin_lock_irqsave(&wl->lock, irqflag);
2306         if (test_bit(GELIC_WL_STAT_ESSID_SET, &wl->stat)) {
2307                 pr_debug("%s: assoc ESSID configured %s\n", __func__,
2308                          wl->essid);
2309                 essid = wl->essid;
2310                 essid_len = wl->essid_len;
2311         } else {
2312                 essid = NULL;
2313                 essid_len = 0;
2314         }
2315         spin_unlock_irqrestore(&wl->lock, irqflag);
2316
2317         ret = gelic_wl_start_scan(wl, 0, essid, essid_len);
2318         if (ret == -ERESTARTSYS) {
2319                 pr_debug("%s: scan start failed association\n", __func__);
2320                 schedule_delayed_work(&wl->assoc_work, HZ/10); /*FIXME*/
2321                 goto out;
2322         } else if (ret) {
2323                 pr_info("%s: scan prerequisite failed\n", __func__);
2324                 goto out;
2325         }
2326
2327         /*
2328          * Wait for bss scan completion
2329          * If we have scan list already, gelic_wl_start_scan()
2330          * returns OK and raises the complete.  Thus,
2331          * it's ok to wait unconditionally here
2332          */
2333         wait_for_completion(&wl->scan_done);
2334
2335         pr_debug("%s: scan done\n", __func__);
2336         mutex_lock(&wl->scan_lock);
2337         if (wl->scan_stat != GELIC_WL_SCAN_STAT_GOT_LIST) {
2338                 gelic_wl_send_iwap_event(wl, NULL);
2339                 pr_info("%s: no scan list. association failed\n", __func__);
2340                 goto scan_lock_out;
2341         }
2342
2343         /* find best matching bss */
2344         best_bss = gelic_wl_find_best_bss(wl);
2345         if (!best_bss) {
2346                 gelic_wl_send_iwap_event(wl, NULL);
2347                 pr_info("%s: no bss matched. association failed\n", __func__);
2348                 goto scan_lock_out;
2349         }
2350
2351         /* ok, do association */
2352         ret = gelic_wl_associate_bss(wl, best_bss);
2353         if (ret)
2354                 pr_info("%s: association failed %d\n", __func__, ret);
2355 scan_lock_out:
2356         mutex_unlock(&wl->scan_lock);
2357 out:
2358         mutex_unlock(&wl->assoc_stat_lock);
2359 }
2360 /*
2361  * Interrupt handler
2362  * Called from the ethernet interrupt handler
2363  * Processes wireless specific virtual interrupts only
2364  */
2365 void gelic_wl_interrupt(struct net_device *netdev, u64 status)
2366 {
2367         struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
2368
2369         if (status & GELIC_CARD_WLAN_COMMAND_COMPLETED) {
2370                 pr_debug("%s:cmd complete\n", __func__);
2371                 complete(&wl->cmd_done_intr);
2372         }
2373
2374         if (status & GELIC_CARD_WLAN_EVENT_RECEIVED) {
2375                 pr_debug("%s:event received\n", __func__);
2376                 queue_delayed_work(wl->event_queue, &wl->event_work, 0);
2377         }
2378 }
2379
2380 /*
2381  * driver helpers
2382  */
2383 #define IW_IOCTL(n) [(n) - SIOCSIWCOMMIT]
2384 static const iw_handler gelic_wl_wext_handler[] =
2385 {
2386         IW_IOCTL(SIOCGIWNAME)           = gelic_wl_get_name,
2387         IW_IOCTL(SIOCGIWRANGE)          = gelic_wl_get_range,
2388         IW_IOCTL(SIOCSIWSCAN)           = gelic_wl_set_scan,
2389         IW_IOCTL(SIOCGIWSCAN)           = gelic_wl_get_scan,
2390         IW_IOCTL(SIOCSIWAUTH)           = gelic_wl_set_auth,
2391         IW_IOCTL(SIOCGIWAUTH)           = gelic_wl_get_auth,
2392         IW_IOCTL(SIOCSIWESSID)          = gelic_wl_set_essid,
2393         IW_IOCTL(SIOCGIWESSID)          = gelic_wl_get_essid,
2394         IW_IOCTL(SIOCSIWENCODE)         = gelic_wl_set_encode,
2395         IW_IOCTL(SIOCGIWENCODE)         = gelic_wl_get_encode,
2396         IW_IOCTL(SIOCSIWAP)             = gelic_wl_set_ap,
2397         IW_IOCTL(SIOCGIWAP)             = gelic_wl_get_ap,
2398         IW_IOCTL(SIOCSIWENCODEEXT)      = gelic_wl_set_encodeext,
2399         IW_IOCTL(SIOCGIWENCODEEXT)      = gelic_wl_get_encodeext,
2400         IW_IOCTL(SIOCSIWMODE)           = gelic_wl_set_mode,
2401         IW_IOCTL(SIOCGIWMODE)           = gelic_wl_get_mode,
2402         IW_IOCTL(SIOCGIWNICKN)          = gelic_wl_get_nick,
2403 };
2404
2405 #ifdef CONFIG_GELIC_WIRELESS_OLD_PSK_INTERFACE
2406 static struct iw_priv_args gelic_wl_private_args[] =
2407 {
2408         {
2409                 .cmd = GELIC_WL_PRIV_SET_PSK,
2410                 .set_args = IW_PRIV_TYPE_CHAR |
2411                 (GELIC_WL_EURUS_PSK_MAX_LEN + 2),
2412                 .name = "set_psk"
2413         },
2414         {
2415                 .cmd = GELIC_WL_PRIV_GET_PSK,
2416                 .get_args = IW_PRIV_TYPE_CHAR |
2417                 (GELIC_WL_EURUS_PSK_MAX_LEN + 2),
2418                 .name = "get_psk"
2419         }
2420 };
2421
2422 static const iw_handler gelic_wl_private_handler[] =
2423 {
2424         gelic_wl_priv_set_psk,
2425         gelic_wl_priv_get_psk,
2426 };
2427 #endif
2428
2429 static const struct iw_handler_def gelic_wl_wext_handler_def = {
2430         .num_standard           = ARRAY_SIZE(gelic_wl_wext_handler),
2431         .standard               = gelic_wl_wext_handler,
2432         .get_wireless_stats     = gelic_wl_get_wireless_stats,
2433 #ifdef CONFIG_GELIC_WIRELESS_OLD_PSK_INTERFACE
2434         .num_private            = ARRAY_SIZE(gelic_wl_private_handler),
2435         .num_private_args       = ARRAY_SIZE(gelic_wl_private_args),
2436         .private                = gelic_wl_private_handler,
2437         .private_args           = gelic_wl_private_args,
2438 #endif
2439 };
2440
2441 static struct net_device *gelic_wl_alloc(struct gelic_card *card)
2442 {
2443         struct net_device *netdev;
2444         struct gelic_port *port;
2445         struct gelic_wl_info *wl;
2446         unsigned int i;
2447
2448         pr_debug("%s:start\n", __func__);
2449         netdev = alloc_etherdev(sizeof(struct gelic_port) +
2450                                 sizeof(struct gelic_wl_info));
2451         pr_debug("%s: netdev =%p card=%p \np", __func__, netdev, card);
2452         if (!netdev)
2453                 return NULL;
2454
2455         strcpy(netdev->name, "wlan%d");
2456
2457         port = netdev_priv(netdev);
2458         port->netdev = netdev;
2459         port->card = card;
2460         port->type = GELIC_PORT_WIRELESS;
2461
2462         wl = port_wl(port);
2463         pr_debug("%s: wl=%p port=%p\n", __func__, wl, port);
2464
2465         /* allocate scan list */
2466         wl->networks = kzalloc(sizeof(struct gelic_wl_scan_info) *
2467                                GELIC_WL_BSS_MAX_ENT, GFP_KERNEL);
2468
2469         if (!wl->networks)
2470                 goto fail_bss;
2471
2472         wl->eurus_cmd_queue = create_singlethread_workqueue("gelic_cmd");
2473         if (!wl->eurus_cmd_queue)
2474                 goto fail_cmd_workqueue;
2475
2476         wl->event_queue = create_singlethread_workqueue("gelic_event");
2477         if (!wl->event_queue)
2478                 goto fail_event_workqueue;
2479
2480         INIT_LIST_HEAD(&wl->network_free_list);
2481         INIT_LIST_HEAD(&wl->network_list);
2482         for (i = 0; i < GELIC_WL_BSS_MAX_ENT; i++)
2483                 list_add_tail(&wl->networks[i].list,
2484                               &wl->network_free_list);
2485         init_completion(&wl->cmd_done_intr);
2486
2487         INIT_DELAYED_WORK(&wl->event_work, gelic_wl_event_worker);
2488         INIT_DELAYED_WORK(&wl->assoc_work, gelic_wl_assoc_worker);
2489         mutex_init(&wl->scan_lock);
2490         mutex_init(&wl->assoc_stat_lock);
2491
2492         init_completion(&wl->scan_done);
2493         /* for the case that no scan request is issued and stop() is called */
2494         complete(&wl->scan_done);
2495
2496         spin_lock_init(&wl->lock);
2497
2498         wl->scan_age = 5*HZ; /* FIXME */
2499
2500         /* buffer for receiving scanned list etc */
2501         BUILD_BUG_ON(PAGE_SIZE <
2502                      sizeof(struct gelic_eurus_scan_info) *
2503                      GELIC_EURUS_MAX_SCAN);
2504         pr_debug("%s:end\n", __func__);
2505         return netdev;
2506
2507 fail_event_workqueue:
2508         destroy_workqueue(wl->eurus_cmd_queue);
2509 fail_cmd_workqueue:
2510         kfree(wl->networks);
2511 fail_bss:
2512         free_netdev(netdev);
2513         pr_debug("%s:end error\n", __func__);
2514         return NULL;
2515
2516 }
2517
2518 static void gelic_wl_free(struct gelic_wl_info *wl)
2519 {
2520         struct gelic_wl_scan_info *scan_info;
2521         unsigned int i;
2522
2523         pr_debug("%s: <-\n", __func__);
2524
2525         pr_debug("%s: destroy queues\n", __func__);
2526         destroy_workqueue(wl->eurus_cmd_queue);
2527         destroy_workqueue(wl->event_queue);
2528
2529         scan_info = wl->networks;
2530         for (i = 0; i < GELIC_WL_BSS_MAX_ENT; i++, scan_info++)
2531                 kfree(scan_info->hwinfo);
2532         kfree(wl->networks);
2533
2534         free_netdev(port_to_netdev(wl_port(wl)));
2535
2536         pr_debug("%s: ->\n", __func__);
2537 }
2538
2539 static int gelic_wl_try_associate(struct net_device *netdev)
2540 {
2541         struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
2542         int ret = -1;
2543         unsigned int i;
2544
2545         pr_debug("%s: <-\n", __func__);
2546
2547         /* check constraits for start association */
2548         /* for no access restriction AP */
2549         if (wl->group_cipher_method == GELIC_WL_CIPHER_NONE) {
2550                 if (test_bit(GELIC_WL_STAT_CONFIGURED,
2551                              &wl->stat))
2552                         goto do_associate;
2553                 else {
2554                         pr_debug("%s: no wep, not configured\n", __func__);
2555                         return ret;
2556                 }
2557         }
2558
2559         /* for WEP, one of four keys should be set */
2560         if (wl->group_cipher_method == GELIC_WL_CIPHER_WEP) {
2561                 /* one of keys set */
2562                 for (i = 0; i < GELIC_WEP_KEYS; i++) {
2563                         if (test_bit(i, &wl->key_enabled))
2564                             goto do_associate;
2565                 }
2566                 pr_debug("%s: WEP, but no key specified\n", __func__);
2567                 return ret;
2568         }
2569
2570         /* for WPA[2], psk should be set */
2571         if ((wl->group_cipher_method == GELIC_WL_CIPHER_TKIP) ||
2572             (wl->group_cipher_method == GELIC_WL_CIPHER_AES)) {
2573                 if (test_bit(GELIC_WL_STAT_WPA_PSK_SET,
2574                              &wl->stat))
2575                         goto do_associate;
2576                 else {
2577                         pr_debug("%s: AES/TKIP, but PSK not configured\n",
2578                                  __func__);
2579                         return ret;
2580                 }
2581         }
2582
2583 do_associate:
2584         ret = schedule_delayed_work(&wl->assoc_work, 0);
2585         pr_debug("%s: start association work %d\n", __func__, ret);
2586         return ret;
2587 }
2588
2589 /*
2590  * netdev handlers
2591  */
2592 static int gelic_wl_open(struct net_device *netdev)
2593 {
2594         struct gelic_card *card = netdev_card(netdev);
2595
2596         pr_debug("%s:->%p\n", __func__, netdev);
2597
2598         gelic_card_up(card);
2599
2600         /* try to associate */
2601         gelic_wl_try_associate(netdev);
2602
2603         netif_start_queue(netdev);
2604
2605         pr_debug("%s:<-\n", __func__);
2606         return 0;
2607 }
2608
2609 /*
2610  * reset state machine
2611  */
2612 static int gelic_wl_reset_state(struct gelic_wl_info *wl)
2613 {
2614         struct gelic_wl_scan_info *target;
2615         struct gelic_wl_scan_info *tmp;
2616
2617         /* empty scan list */
2618         list_for_each_entry_safe(target, tmp, &wl->network_list, list) {
2619                 list_move_tail(&target->list, &wl->network_free_list);
2620         }
2621         wl->scan_stat = GELIC_WL_SCAN_STAT_INIT;
2622
2623         /* clear configuration */
2624         wl->auth_method = GELIC_EURUS_AUTH_OPEN;
2625         wl->group_cipher_method = GELIC_WL_CIPHER_NONE;
2626         wl->pairwise_cipher_method = GELIC_WL_CIPHER_NONE;
2627         wl->wpa_level = GELIC_WL_WPA_LEVEL_NONE;
2628
2629         wl->key_enabled = 0;
2630         wl->current_key = 0;
2631
2632         wl->psk_type = GELIC_EURUS_WPA_PSK_PASSPHRASE;
2633         wl->psk_len = 0;
2634
2635         wl->essid_len = 0;
2636         memset(wl->essid, 0, sizeof(wl->essid));
2637         memset(wl->bssid, 0, sizeof(wl->bssid));
2638         memset(wl->active_bssid, 0, sizeof(wl->active_bssid));
2639
2640         wl->assoc_stat = GELIC_WL_ASSOC_STAT_DISCONN;
2641
2642         memset(&wl->iwstat, 0, sizeof(wl->iwstat));
2643         /* all status bit clear */
2644         wl->stat = 0;
2645         return 0;
2646 }
2647
2648 /*
2649  * Tell eurus to terminate association
2650  */
2651 static void gelic_wl_disconnect(struct net_device *netdev)
2652 {
2653         struct gelic_port *port = netdev_priv(netdev);
2654         struct gelic_wl_info *wl = port_wl(port);
2655         struct gelic_eurus_cmd *cmd;
2656
2657         /*
2658          * If scann process is running on chip,
2659          * further requests will be rejected
2660          */
2661         if (wl->scan_stat == GELIC_WL_SCAN_STAT_SCANNING)
2662                 wait_for_completion_timeout(&wl->scan_done, HZ);
2663
2664         cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_DISASSOC, NULL, 0);
2665         kfree(cmd);
2666         gelic_wl_send_iwap_event(wl, NULL);
2667 };
2668
2669 static int gelic_wl_stop(struct net_device *netdev)
2670 {
2671         struct gelic_port *port = netdev_priv(netdev);
2672         struct gelic_wl_info *wl = port_wl(port);
2673         struct gelic_card *card = netdev_card(netdev);
2674
2675         pr_debug("%s:<-\n", __func__);
2676
2677         /*
2678          * Cancel pending association work.
2679          * event work can run after netdev down
2680          */
2681         cancel_delayed_work(&wl->assoc_work);
2682
2683         if (wl->assoc_stat == GELIC_WL_ASSOC_STAT_ASSOCIATED)
2684                 gelic_wl_disconnect(netdev);
2685
2686         /* reset our state machine */
2687         gelic_wl_reset_state(wl);
2688
2689         netif_stop_queue(netdev);
2690
2691         gelic_card_down(card);
2692
2693         pr_debug("%s:->\n", __func__);
2694         return 0;
2695 }
2696
2697 /* -- */
2698
2699 static struct ethtool_ops gelic_wl_ethtool_ops = {
2700         .get_drvinfo    = gelic_net_get_drvinfo,
2701         .get_link       = gelic_wl_get_link,
2702         .get_tx_csum    = ethtool_op_get_tx_csum,
2703         .set_tx_csum    = ethtool_op_set_tx_csum,
2704         .get_rx_csum    = gelic_net_get_rx_csum,
2705         .set_rx_csum    = gelic_net_set_rx_csum,
2706 };
2707
2708 static void gelic_wl_setup_netdev_ops(struct net_device *netdev)
2709 {
2710         struct gelic_wl_info *wl;
2711         wl = port_wl(netdev_priv(netdev));
2712         BUG_ON(!wl);
2713         netdev->open = &gelic_wl_open;
2714         netdev->stop = &gelic_wl_stop;
2715         netdev->hard_start_xmit = &gelic_net_xmit;
2716         netdev->set_multicast_list = &gelic_net_set_multi;
2717         netdev->change_mtu = &gelic_net_change_mtu;
2718         netdev->wireless_data = &wl->wireless_data;
2719         netdev->wireless_handlers = &gelic_wl_wext_handler_def;
2720         /* tx watchdog */
2721         netdev->tx_timeout = &gelic_net_tx_timeout;
2722         netdev->watchdog_timeo = GELIC_NET_WATCHDOG_TIMEOUT;
2723
2724         netdev->ethtool_ops = &gelic_wl_ethtool_ops;
2725 #ifdef CONFIG_NET_POLL_CONTROLLER
2726         netdev->poll_controller = gelic_net_poll_controller;
2727 #endif
2728 }
2729
2730 /*
2731  * driver probe/remove
2732  */
2733 int gelic_wl_driver_probe(struct gelic_card *card)
2734 {
2735         int ret;
2736         struct net_device *netdev;
2737
2738         pr_debug("%s:start\n", __func__);
2739
2740         if (ps3_compare_firmware_version(1, 6, 0) < 0)
2741                 return 0;
2742         if (!card->vlan[GELIC_PORT_WIRELESS].tx)
2743                 return 0;
2744
2745         /* alloc netdevice for wireless */
2746         netdev = gelic_wl_alloc(card);
2747         if (!netdev)
2748                 return -ENOMEM;
2749
2750         /* setup net_device structure */
2751         SET_NETDEV_DEV(netdev, &card->dev->core);
2752         gelic_wl_setup_netdev_ops(netdev);
2753
2754         /* setup some of net_device and register it */
2755         ret = gelic_net_setup_netdev(netdev, card);
2756         if (ret)
2757                 goto fail_setup;
2758         card->netdev[GELIC_PORT_WIRELESS] = netdev;
2759
2760         /* add enable wireless interrupt */
2761         card->irq_mask |= GELIC_CARD_WLAN_EVENT_RECEIVED |
2762                 GELIC_CARD_WLAN_COMMAND_COMPLETED;
2763         /* to allow wireless commands while both interfaces are down */
2764         gelic_card_set_irq_mask(card, GELIC_CARD_WLAN_EVENT_RECEIVED |
2765                                 GELIC_CARD_WLAN_COMMAND_COMPLETED);
2766         pr_debug("%s:end\n", __func__);
2767         return 0;
2768
2769 fail_setup:
2770         gelic_wl_free(port_wl(netdev_port(netdev)));
2771
2772         return ret;
2773 }
2774
2775 int gelic_wl_driver_remove(struct gelic_card *card)
2776 {
2777         struct gelic_wl_info *wl;
2778         struct net_device *netdev;
2779
2780         pr_debug("%s:start\n", __func__);
2781
2782         if (ps3_compare_firmware_version(1, 6, 0) < 0)
2783                 return 0;
2784         if (!card->vlan[GELIC_PORT_WIRELESS].tx)
2785                 return 0;
2786
2787         netdev = card->netdev[GELIC_PORT_WIRELESS];
2788         wl = port_wl(netdev_priv(netdev));
2789
2790         /* if the interface was not up, but associated */
2791         if (wl->assoc_stat == GELIC_WL_ASSOC_STAT_ASSOCIATED)
2792                 gelic_wl_disconnect(netdev);
2793
2794         complete(&wl->cmd_done_intr);
2795
2796         /* cancel all work queue */
2797         cancel_delayed_work(&wl->assoc_work);
2798         cancel_delayed_work(&wl->event_work);
2799         flush_workqueue(wl->eurus_cmd_queue);
2800         flush_workqueue(wl->event_queue);
2801
2802         unregister_netdev(netdev);
2803
2804         /* disable wireless interrupt */
2805         pr_debug("%s: disable intr\n", __func__);
2806         card->irq_mask &= ~(GELIC_CARD_WLAN_EVENT_RECEIVED |
2807                             GELIC_CARD_WLAN_COMMAND_COMPLETED);
2808         /* free bss list, netdev*/
2809         gelic_wl_free(wl);
2810         pr_debug("%s:end\n", __func__);
2811         return 0;
2812 }