USB: fix usb serial pm counter decrement for disconnected interfaces
[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         DECLARE_MAC_BUF(mac);
767
768         i = 0;
769         list_for_each_entry(scan_info, &wl->network_list, list) {
770                 pr_debug("%s: item %d\n", __func__, i++);
771                 pr_debug("valid=%d eurusindex=%d last=%lx\n",
772                          scan_info->valid, scan_info->eurus_index,
773                          scan_info->last_scanned);
774                 pr_debug("r_len=%d r_ext_len=%d essid_len=%d\n",
775                          scan_info->rate_len, scan_info->rate_ext_len,
776                          scan_info->essid_len);
777                 /* -- */
778                 pr_debug("bssid=%s\n",
779                          print_mac(mac, &scan_info->hwinfo->bssid[2]));
780                 pr_debug("essid=%s\n", scan_info->hwinfo->essid);
781         }
782 }
783 #endif
784
785 static int gelic_wl_set_auth(struct net_device *netdev,
786                              struct iw_request_info *info,
787                              union iwreq_data *data, char *extra)
788 {
789         struct iw_param *param = &data->param;
790         struct gelic_wl_info *wl = port_wl(netdev_port(netdev));
791         unsigned long irqflag;
792         int ret = 0;
793
794         pr_debug("%s: <- %d\n", __func__, param->flags & IW_AUTH_INDEX);
795         spin_lock_irqsave(&wl->lock, irqflag);
796         switch (param->flags & IW_AUTH_INDEX) {
797         case IW_AUTH_WPA_VERSION:
798                 if (param->value & IW_AUTH_WPA_VERSION_DISABLED) {
799                         pr_debug("%s: NO WPA selected\n", __func__);
800                         wl->wpa_level = GELIC_WL_WPA_LEVEL_NONE;
801                         wl->group_cipher_method = GELIC_WL_CIPHER_WEP;
802                         wl->pairwise_cipher_method = GELIC_WL_CIPHER_WEP;
803                 }
804                 if (param->value & IW_AUTH_WPA_VERSION_WPA) {
805                         pr_debug("%s: WPA version 1 selected\n", __func__);
806                         wl->wpa_level = GELIC_WL_WPA_LEVEL_WPA;
807                         wl->group_cipher_method = GELIC_WL_CIPHER_TKIP;
808                         wl->pairwise_cipher_method = GELIC_WL_CIPHER_TKIP;
809                         wl->auth_method = GELIC_EURUS_AUTH_OPEN;
810                 }
811                 if (param->value & IW_AUTH_WPA_VERSION_WPA2) {
812                         /*
813                          * As the hypervisor may not tell the cipher
814                          * information of the AP if it is WPA2,
815                          * you will not decide suitable cipher from
816                          * its beacon.
817                          * You should have knowledge about the AP's
818                          * cipher infomation in other method prior to
819                          * the association.
820                          */
821                         if (!precise_ie())
822                                 pr_info("%s: WPA2 may not work\n", __func__);
823                         if (wpa2_capable()) {
824                                 wl->wpa_level = GELIC_WL_WPA_LEVEL_WPA2;
825                                 wl->group_cipher_method = GELIC_WL_CIPHER_AES;
826                                 wl->pairwise_cipher_method =
827                                         GELIC_WL_CIPHER_AES;
828                                 wl->auth_method = GELIC_EURUS_AUTH_OPEN;
829                         } else
830                                 ret = -EINVAL;
831                 }
832                 break;
833
834         case IW_AUTH_CIPHER_PAIRWISE:
835                 if (param->value &
836                     (IW_AUTH_CIPHER_WEP104 | IW_AUTH_CIPHER_WEP40)) {
837                         pr_debug("%s: WEP selected\n", __func__);
838                         wl->pairwise_cipher_method = GELIC_WL_CIPHER_WEP;
839                 }
840                 if (param->value & IW_AUTH_CIPHER_TKIP) {
841                         pr_debug("%s: TKIP selected\n", __func__);
842                         wl->pairwise_cipher_method = GELIC_WL_CIPHER_TKIP;
843                 }
844                 if (param->value & IW_AUTH_CIPHER_CCMP) {
845                         pr_debug("%s: CCMP selected\n", __func__);
846                         wl->pairwise_cipher_method = GELIC_WL_CIPHER_AES;
847                 }
848                 if (param->value & IW_AUTH_CIPHER_NONE) {
849                         pr_debug("%s: no auth selected\n", __func__);
850                         wl->pairwise_cipher_method = GELIC_WL_CIPHER_NONE;
851                 }
852                 break;
853         case IW_AUTH_CIPHER_GROUP:
854                 if (param->value &
855                     (IW_AUTH_CIPHER_WEP104 | IW_AUTH_CIPHER_WEP40)) {
856                         pr_debug("%s: WEP selected\n", __func__);
857                         wl->group_cipher_method = GELIC_WL_CIPHER_WEP;
858                 }
859                 if (param->value & IW_AUTH_CIPHER_TKIP) {
860                         pr_debug("%s: TKIP selected\n", __func__);
861                         wl->group_cipher_method = GELIC_WL_CIPHER_TKIP;
862                 }
863                 if (param->value & IW_AUTH_CIPHER_CCMP) {
864                         pr_debug("%s: CCMP selected\n", __func__);
865                         wl->group_cipher_method = GELIC_WL_CIPHER_AES;
866                 }
867                 if (param->value & IW_AUTH_CIPHER_NONE) {
868                         pr_debug("%s: no auth selected\n", __func__);
869                         wl->group_cipher_method = GELIC_WL_CIPHER_NONE;
870                 }
871                 break;
872         case IW_AUTH_80211_AUTH_ALG:
873                 if (param->value & IW_AUTH_ALG_SHARED_KEY) {
874                         pr_debug("%s: shared key specified\n", __func__);
875                         wl->auth_method = GELIC_EURUS_AUTH_SHARED;
876                 } else if (param->value & IW_AUTH_ALG_OPEN_SYSTEM) {
877                         pr_debug("%s: open system specified\n", __func__);
878                         wl->auth_method = GELIC_EURUS_AUTH_OPEN;
879                 } else
880                         ret = -EINVAL;
881                 break;
882
883         case IW_AUTH_WPA_ENABLED:
884                 if (param->value) {
885                         pr_debug("%s: WPA enabled\n", __func__);
886                         wl->wpa_level = GELIC_WL_WPA_LEVEL_WPA;
887                 } else {
888                         pr_debug("%s: WPA disabled\n", __func__);
889                         wl->wpa_level = GELIC_WL_WPA_LEVEL_NONE;
890                 }
891                 break;
892
893         case IW_AUTH_KEY_MGMT:
894                 if (param->value & IW_AUTH_KEY_MGMT_PSK)
895                         break;
896                 /* intentionally fall through */
897         default:
898                 ret = -EOPNOTSUPP;
899                 break;
900         };
901
902         if (!ret)
903                 set_bit(GELIC_WL_STAT_CONFIGURED, &wl->stat);
904
905         spin_unlock_irqrestore(&wl->lock, irqflag);
906         pr_debug("%s: -> %d\n", __func__, ret);
907         return ret;
908 }
909
910 static int gelic_wl_get_auth(struct net_device *netdev,
911                              struct iw_request_info *info,
912                              union iwreq_data *iwreq, char *extra)
913 {
914         struct iw_param *param = &iwreq->param;
915         struct gelic_wl_info *wl = port_wl(netdev_port(netdev));
916         unsigned long irqflag;
917         int ret = 0;
918
919         pr_debug("%s: <- %d\n", __func__, param->flags & IW_AUTH_INDEX);
920         spin_lock_irqsave(&wl->lock, irqflag);
921         switch (param->flags & IW_AUTH_INDEX) {
922         case IW_AUTH_WPA_VERSION:
923                 switch (wl->wpa_level) {
924                 case GELIC_WL_WPA_LEVEL_WPA:
925                         param->value |= IW_AUTH_WPA_VERSION_WPA;
926                         break;
927                 case GELIC_WL_WPA_LEVEL_WPA2:
928                         param->value |= IW_AUTH_WPA_VERSION_WPA2;
929                         break;
930                 default:
931                         param->value |= IW_AUTH_WPA_VERSION_DISABLED;
932                 }
933                 break;
934
935         case IW_AUTH_80211_AUTH_ALG:
936                 if (wl->auth_method == GELIC_EURUS_AUTH_SHARED)
937                         param->value = IW_AUTH_ALG_SHARED_KEY;
938                 else if (wl->auth_method == GELIC_EURUS_AUTH_OPEN)
939                         param->value = IW_AUTH_ALG_OPEN_SYSTEM;
940                 break;
941
942         case IW_AUTH_WPA_ENABLED:
943                 switch (wl->wpa_level) {
944                 case GELIC_WL_WPA_LEVEL_WPA:
945                 case GELIC_WL_WPA_LEVEL_WPA2:
946                         param->value = 1;
947                         break;
948                 default:
949                         param->value = 0;
950                         break;
951                 }
952                 break;
953         default:
954                 ret = -EOPNOTSUPP;
955         }
956
957         spin_unlock_irqrestore(&wl->lock, irqflag);
958         pr_debug("%s: -> %d\n", __func__, ret);
959         return ret;
960 }
961
962 /* SIOC{S,G}IWESSID */
963 static int gelic_wl_set_essid(struct net_device *netdev,
964                               struct iw_request_info *info,
965                               union iwreq_data *data, char *extra)
966 {
967         struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
968         unsigned long irqflag;
969
970         pr_debug("%s: <- l=%d f=%d\n", __func__,
971                  data->essid.length, data->essid.flags);
972         if (IW_ESSID_MAX_SIZE < data->essid.length)
973                 return -EINVAL;
974
975         spin_lock_irqsave(&wl->lock, irqflag);
976         if (data->essid.flags) {
977                 wl->essid_len = data->essid.length;
978                 memcpy(wl->essid, extra, wl->essid_len);
979                 pr_debug("%s: essid = '%s'\n", __func__, extra);
980                 set_bit(GELIC_WL_STAT_ESSID_SET, &wl->stat);
981         } else {
982                 pr_debug("%s: ESSID any \n", __func__);
983                 clear_bit(GELIC_WL_STAT_ESSID_SET, &wl->stat);
984         }
985         set_bit(GELIC_WL_STAT_CONFIGURED, &wl->stat);
986         spin_unlock_irqrestore(&wl->lock, irqflag);
987
988
989         gelic_wl_try_associate(netdev); /* FIXME */
990         pr_debug("%s: -> \n", __func__);
991         return 0;
992 }
993
994 static int gelic_wl_get_essid(struct net_device *netdev,
995                               struct iw_request_info *info,
996                               union iwreq_data *data, char *extra)
997 {
998         struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
999         unsigned long irqflag;
1000
1001         pr_debug("%s: <- \n", __func__);
1002         mutex_lock(&wl->assoc_stat_lock);
1003         spin_lock_irqsave(&wl->lock, irqflag);
1004         if (test_bit(GELIC_WL_STAT_ESSID_SET, &wl->stat) ||
1005             wl->assoc_stat == GELIC_WL_ASSOC_STAT_ASSOCIATED) {
1006                 memcpy(extra, wl->essid, wl->essid_len);
1007                 data->essid.length = wl->essid_len;
1008                 data->essid.flags = 1;
1009         } else
1010                 data->essid.flags = 0;
1011
1012         mutex_unlock(&wl->assoc_stat_lock);
1013         spin_unlock_irqrestore(&wl->lock, irqflag);
1014         pr_debug("%s: -> len=%d \n", __func__, data->essid.length);
1015
1016         return 0;
1017 }
1018
1019 /* SIO{S,G}IWENCODE */
1020 static int gelic_wl_set_encode(struct net_device *netdev,
1021                                struct iw_request_info *info,
1022                                union iwreq_data *data, char *extra)
1023 {
1024         struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
1025         struct iw_point *enc = &data->encoding;
1026         __u16 flags;
1027         unsigned int irqflag;
1028         int key_index, index_specified;
1029         int ret = 0;
1030
1031         pr_debug("%s: <- \n", __func__);
1032         flags = enc->flags & IW_ENCODE_FLAGS;
1033         key_index = enc->flags & IW_ENCODE_INDEX;
1034
1035         pr_debug("%s: key_index = %d\n", __func__, key_index);
1036         pr_debug("%s: key_len = %d\n", __func__, enc->length);
1037         pr_debug("%s: flag=%x\n", __func__, enc->flags & IW_ENCODE_FLAGS);
1038
1039         if (GELIC_WEP_KEYS < key_index)
1040                 return -EINVAL;
1041
1042         spin_lock_irqsave(&wl->lock, irqflag);
1043         if (key_index) {
1044                 index_specified = 1;
1045                 key_index--;
1046         } else {
1047                 index_specified = 0;
1048                 key_index = wl->current_key;
1049         }
1050
1051         if (flags & IW_ENCODE_NOKEY) {
1052                 /* if just IW_ENCODE_NOKEY, change current key index */
1053                 if (!flags && index_specified) {
1054                         wl->current_key = key_index;
1055                         goto done;
1056                 }
1057
1058                 if (flags & IW_ENCODE_DISABLED) {
1059                         if (!index_specified) {
1060                                 /* disable encryption */
1061                                 wl->group_cipher_method = GELIC_WL_CIPHER_NONE;
1062                                 wl->pairwise_cipher_method =
1063                                         GELIC_WL_CIPHER_NONE;
1064                                 /* invalidate all key */
1065                                 wl->key_enabled = 0;
1066                         } else
1067                                 clear_bit(key_index, &wl->key_enabled);
1068                 }
1069
1070                 if (flags & IW_ENCODE_OPEN)
1071                         wl->auth_method = GELIC_EURUS_AUTH_OPEN;
1072                 if (flags & IW_ENCODE_RESTRICTED) {
1073                         pr_info("%s: shared key mode enabled\n", __func__);
1074                         wl->auth_method = GELIC_EURUS_AUTH_SHARED;
1075                 }
1076         } else {
1077                 if (IW_ENCODING_TOKEN_MAX < enc->length) {
1078                         ret = -EINVAL;
1079                         goto done;
1080                 }
1081                 wl->key_len[key_index] = enc->length;
1082                 memcpy(wl->key[key_index], extra, enc->length);
1083                 set_bit(key_index, &wl->key_enabled);
1084                 wl->pairwise_cipher_method = GELIC_WL_CIPHER_WEP;
1085                 wl->group_cipher_method = GELIC_WL_CIPHER_WEP;
1086         }
1087         set_bit(GELIC_WL_STAT_CONFIGURED, &wl->stat);
1088 done:
1089         spin_unlock_irqrestore(&wl->lock, irqflag);
1090         pr_debug("%s: -> \n", __func__);
1091         return ret;
1092 }
1093
1094 static int gelic_wl_get_encode(struct net_device *netdev,
1095                                struct iw_request_info *info,
1096                                union iwreq_data *data, char *extra)
1097 {
1098         struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
1099         struct iw_point *enc = &data->encoding;
1100         unsigned int irqflag;
1101         unsigned int key_index, index_specified;
1102         int ret = 0;
1103
1104         pr_debug("%s: <- \n", __func__);
1105         key_index = enc->flags & IW_ENCODE_INDEX;
1106         pr_debug("%s: flag=%#x point=%p len=%d extra=%p\n", __func__,
1107                  enc->flags, enc->pointer, enc->length, extra);
1108         if (GELIC_WEP_KEYS < key_index)
1109                 return -EINVAL;
1110
1111         spin_lock_irqsave(&wl->lock, irqflag);
1112         if (key_index) {
1113                 index_specified = 1;
1114                 key_index--;
1115         } else {
1116                 index_specified = 0;
1117                 key_index = wl->current_key;
1118         }
1119
1120         if (wl->group_cipher_method == GELIC_WL_CIPHER_WEP) {
1121                 switch (wl->auth_method) {
1122                 case GELIC_EURUS_AUTH_OPEN:
1123                         enc->flags = IW_ENCODE_OPEN;
1124                         break;
1125                 case GELIC_EURUS_AUTH_SHARED:
1126                         enc->flags = IW_ENCODE_RESTRICTED;
1127                         break;
1128                 }
1129         } else
1130                 enc->flags = IW_ENCODE_DISABLED;
1131
1132         if (test_bit(key_index, &wl->key_enabled)) {
1133                 if (enc->length < wl->key_len[key_index]) {
1134                         ret = -EINVAL;
1135                         goto done;
1136                 }
1137                 enc->length = wl->key_len[key_index];
1138                 memcpy(extra, wl->key[key_index], wl->key_len[key_index]);
1139         } else {
1140                 enc->length = 0;
1141                 enc->flags |= IW_ENCODE_NOKEY;
1142         }
1143         enc->flags |= key_index + 1;
1144         pr_debug("%s: -> flag=%x len=%d\n", __func__,
1145                  enc->flags, enc->length);
1146
1147 done:
1148         spin_unlock_irqrestore(&wl->lock, irqflag);
1149         return ret;
1150 }
1151
1152 /* SIOC{S,G}IWAP */
1153 static int gelic_wl_set_ap(struct net_device *netdev,
1154                            struct iw_request_info *info,
1155                            union iwreq_data *data, char *extra)
1156 {
1157         struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
1158         unsigned long irqflag;
1159
1160         pr_debug("%s: <-\n", __func__);
1161         if (data->ap_addr.sa_family != ARPHRD_ETHER)
1162                 return -EINVAL;
1163
1164         spin_lock_irqsave(&wl->lock, irqflag);
1165         if (is_valid_ether_addr(data->ap_addr.sa_data)) {
1166                 memcpy(wl->bssid, data->ap_addr.sa_data,
1167                        ETH_ALEN);
1168                 set_bit(GELIC_WL_STAT_BSSID_SET, &wl->stat);
1169                 set_bit(GELIC_WL_STAT_CONFIGURED, &wl->stat);
1170                 pr_debug("%s: bss=%02x:%02x:%02x:%02x:%02x:%02x\n",
1171                          __func__,
1172                          wl->bssid[0], wl->bssid[1],
1173                          wl->bssid[2], wl->bssid[3],
1174                          wl->bssid[4], wl->bssid[5]);
1175         } else {
1176                 pr_debug("%s: clear bssid\n", __func__);
1177                 clear_bit(GELIC_WL_STAT_BSSID_SET, &wl->stat);
1178                 memset(wl->bssid, 0, ETH_ALEN);
1179         }
1180         spin_unlock_irqrestore(&wl->lock, irqflag);
1181         pr_debug("%s: ->\n", __func__);
1182         return 0;
1183 }
1184
1185 static int gelic_wl_get_ap(struct net_device *netdev,
1186                            struct iw_request_info *info,
1187                            union iwreq_data *data, char *extra)
1188 {
1189         struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
1190         unsigned long irqflag;
1191
1192         pr_debug("%s: <-\n", __func__);
1193         mutex_lock(&wl->assoc_stat_lock);
1194         spin_lock_irqsave(&wl->lock, irqflag);
1195         if (wl->assoc_stat == GELIC_WL_ASSOC_STAT_ASSOCIATED) {
1196                 data->ap_addr.sa_family = ARPHRD_ETHER;
1197                 memcpy(data->ap_addr.sa_data, wl->active_bssid,
1198                        ETH_ALEN);
1199         } else
1200                 memset(data->ap_addr.sa_data, 0, ETH_ALEN);
1201
1202         spin_unlock_irqrestore(&wl->lock, irqflag);
1203         mutex_unlock(&wl->assoc_stat_lock);
1204         pr_debug("%s: ->\n", __func__);
1205         return 0;
1206 }
1207
1208 /* SIOC{S,G}IWENCODEEXT */
1209 static int gelic_wl_set_encodeext(struct net_device *netdev,
1210                                   struct iw_request_info *info,
1211                                   union iwreq_data *data, char *extra)
1212 {
1213         struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
1214         struct iw_point *enc = &data->encoding;
1215         struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
1216         __u16 alg;
1217         __u16 flags;
1218         unsigned int irqflag;
1219         int key_index;
1220         int ret = 0;
1221
1222         pr_debug("%s: <- \n", __func__);
1223         flags = enc->flags & IW_ENCODE_FLAGS;
1224         alg = ext->alg;
1225         key_index = enc->flags & IW_ENCODE_INDEX;
1226
1227         pr_debug("%s: key_index = %d\n", __func__, key_index);
1228         pr_debug("%s: key_len = %d\n", __func__, enc->length);
1229         pr_debug("%s: flag=%x\n", __func__, enc->flags & IW_ENCODE_FLAGS);
1230         pr_debug("%s: ext_flag=%x\n", __func__, ext->ext_flags);
1231         pr_debug("%s: ext_key_len=%x\n", __func__, ext->key_len);
1232
1233         if (GELIC_WEP_KEYS < key_index)
1234                 return -EINVAL;
1235
1236         spin_lock_irqsave(&wl->lock, irqflag);
1237         if (key_index)
1238                 key_index--;
1239         else
1240                 key_index = wl->current_key;
1241
1242         if (!enc->length && (ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY)) {
1243                 /* reques to change default key index */
1244                 pr_debug("%s: request to change default key to %d\n",
1245                          __func__, key_index);
1246                 wl->current_key = key_index;
1247                 goto done;
1248         }
1249
1250         if (alg == IW_ENCODE_ALG_NONE || (flags & IW_ENCODE_DISABLED)) {
1251                 pr_debug("%s: alg disabled\n", __func__);
1252                 wl->wpa_level = GELIC_WL_WPA_LEVEL_NONE;
1253                 wl->group_cipher_method = GELIC_WL_CIPHER_NONE;
1254                 wl->pairwise_cipher_method = GELIC_WL_CIPHER_NONE;
1255                 wl->auth_method = GELIC_EURUS_AUTH_OPEN; /* should be open */
1256         } else if (alg == IW_ENCODE_ALG_WEP) {
1257                 pr_debug("%s: WEP requested\n", __func__);
1258                 if (flags & IW_ENCODE_OPEN) {
1259                         pr_debug("%s: open key mode\n", __func__);
1260                         wl->auth_method = GELIC_EURUS_AUTH_OPEN;
1261                 }
1262                 if (flags & IW_ENCODE_RESTRICTED) {
1263                         pr_debug("%s: shared key mode\n", __func__);
1264                         wl->auth_method = GELIC_EURUS_AUTH_SHARED;
1265                 }
1266                 if (IW_ENCODING_TOKEN_MAX < ext->key_len) {
1267                         pr_info("%s: key is too long %d\n", __func__,
1268                                 ext->key_len);
1269                         ret = -EINVAL;
1270                         goto done;
1271                 }
1272                 /* OK, update the key */
1273                 wl->key_len[key_index] = ext->key_len;
1274                 memset(wl->key[key_index], 0, IW_ENCODING_TOKEN_MAX);
1275                 memcpy(wl->key[key_index], ext->key, ext->key_len);
1276                 set_bit(key_index, &wl->key_enabled);
1277                 /* remember wep info changed */
1278                 set_bit(GELIC_WL_STAT_CONFIGURED, &wl->stat);
1279         } else if (alg == IW_ENCODE_ALG_PMK) {
1280                 if (ext->key_len != WPA_PSK_LEN) {
1281                         pr_err("%s: PSK length wrong %d\n", __func__,
1282                                ext->key_len);
1283                         ret = -EINVAL;
1284                         goto done;
1285                 }
1286                 memset(wl->psk, 0, sizeof(wl->psk));
1287                 memcpy(wl->psk, ext->key, ext->key_len);
1288                 wl->psk_len = ext->key_len;
1289                 wl->psk_type = GELIC_EURUS_WPA_PSK_BIN;
1290                 /* remember PSK configured */
1291                 set_bit(GELIC_WL_STAT_WPA_PSK_SET, &wl->stat);
1292         }
1293 done:
1294         spin_unlock_irqrestore(&wl->lock, irqflag);
1295         pr_debug("%s: -> \n", __func__);
1296         return ret;
1297 }
1298
1299 static int gelic_wl_get_encodeext(struct net_device *netdev,
1300                                   struct iw_request_info *info,
1301                                   union iwreq_data *data, char *extra)
1302 {
1303         struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
1304         struct iw_point *enc = &data->encoding;
1305         struct iw_encode_ext *ext = (struct iw_encode_ext *)extra;
1306         unsigned int irqflag;
1307         int key_index;
1308         int ret = 0;
1309         int max_key_len;
1310
1311         pr_debug("%s: <- \n", __func__);
1312
1313         max_key_len = enc->length - sizeof(struct iw_encode_ext);
1314         if (max_key_len < 0)
1315                 return -EINVAL;
1316         key_index = enc->flags & IW_ENCODE_INDEX;
1317
1318         pr_debug("%s: key_index = %d\n", __func__, key_index);
1319         pr_debug("%s: key_len = %d\n", __func__, enc->length);
1320         pr_debug("%s: flag=%x\n", __func__, enc->flags & IW_ENCODE_FLAGS);
1321
1322         if (GELIC_WEP_KEYS < key_index)
1323                 return -EINVAL;
1324
1325         spin_lock_irqsave(&wl->lock, irqflag);
1326         if (key_index)
1327                 key_index--;
1328         else
1329                 key_index = wl->current_key;
1330
1331         memset(ext, 0, sizeof(struct iw_encode_ext));
1332         switch (wl->group_cipher_method) {
1333         case GELIC_WL_CIPHER_WEP:
1334                 ext->alg = IW_ENCODE_ALG_WEP;
1335                 enc->flags |= IW_ENCODE_ENABLED;
1336                 break;
1337         case GELIC_WL_CIPHER_TKIP:
1338                 ext->alg = IW_ENCODE_ALG_TKIP;
1339                 enc->flags |= IW_ENCODE_ENABLED;
1340                 break;
1341         case GELIC_WL_CIPHER_AES:
1342                 ext->alg = IW_ENCODE_ALG_CCMP;
1343                 enc->flags |= IW_ENCODE_ENABLED;
1344                 break;
1345         case GELIC_WL_CIPHER_NONE:
1346         default:
1347                 ext->alg = IW_ENCODE_ALG_NONE;
1348                 enc->flags |= IW_ENCODE_NOKEY;
1349                 break;
1350         }
1351
1352         if (!(enc->flags & IW_ENCODE_NOKEY)) {
1353                 if (max_key_len < wl->key_len[key_index]) {
1354                         ret = -E2BIG;
1355                         goto out;
1356                 }
1357                 if (test_bit(key_index, &wl->key_enabled))
1358                         memcpy(ext->key, wl->key[key_index],
1359                                wl->key_len[key_index]);
1360                 else
1361                         pr_debug("%s: disabled key requested ix=%d\n",
1362                                  __func__, key_index);
1363         }
1364 out:
1365         spin_unlock_irqrestore(&wl->lock, irqflag);
1366         pr_debug("%s: -> \n", __func__);
1367         return ret;
1368 }
1369 /* SIOC{S,G}IWMODE */
1370 static int gelic_wl_set_mode(struct net_device *netdev,
1371                              struct iw_request_info *info,
1372                              union iwreq_data *data, char *extra)
1373 {
1374         __u32 mode = data->mode;
1375         int ret;
1376
1377         pr_debug("%s: <- \n", __func__);
1378         if (mode == IW_MODE_INFRA)
1379                 ret = 0;
1380         else
1381                 ret = -EOPNOTSUPP;
1382         pr_debug("%s: -> %d\n", __func__, ret);
1383         return ret;
1384 }
1385
1386 static int gelic_wl_get_mode(struct net_device *netdev,
1387                              struct iw_request_info *info,
1388                              union iwreq_data *data, char *extra)
1389 {
1390         __u32 *mode = &data->mode;
1391         pr_debug("%s: <- \n", __func__);
1392         *mode = IW_MODE_INFRA;
1393         pr_debug("%s: ->\n", __func__);
1394         return 0;
1395 }
1396
1397 #ifdef CONFIG_GELIC_WIRELESS_OLD_PSK_INTERFACE
1398 /* SIOCIWFIRSTPRIV */
1399 static int hex2bin(u8 *str, u8 *bin, unsigned int len)
1400 {
1401         unsigned int i;
1402         static unsigned char *hex = "0123456789ABCDEF";
1403         unsigned char *p, *q;
1404         u8 tmp;
1405
1406         if (len != WPA_PSK_LEN * 2)
1407                 return -EINVAL;
1408
1409         for (i = 0; i < WPA_PSK_LEN * 2; i += 2) {
1410                 p = strchr(hex, toupper(str[i]));
1411                 q = strchr(hex, toupper(str[i + 1]));
1412                 if (!p || !q) {
1413                         pr_info("%s: unconvertible PSK digit=%d\n",
1414                                 __func__, i);
1415                         return -EINVAL;
1416                 }
1417                 tmp = ((p - hex) << 4) + (q - hex);
1418                 *bin++ = tmp;
1419         }
1420         return 0;
1421 };
1422
1423 static int gelic_wl_priv_set_psk(struct net_device *net_dev,
1424                                  struct iw_request_info *info,
1425                                  union iwreq_data *data, char *extra)
1426 {
1427         struct gelic_wl_info *wl = port_wl(netdev_priv(net_dev));
1428         unsigned int len;
1429         unsigned int irqflag;
1430         int ret = 0;
1431
1432         pr_debug("%s:<- len=%d\n", __func__, data->data.length);
1433         len = data->data.length - 1;
1434         if (len <= 2)
1435                 return -EINVAL;
1436
1437         spin_lock_irqsave(&wl->lock, irqflag);
1438         if (extra[0] == '"' && extra[len - 1] == '"') {
1439                 pr_debug("%s: passphrase mode\n", __func__);
1440                 /* pass phrase */
1441                 if (GELIC_WL_EURUS_PSK_MAX_LEN < (len - 2)) {
1442                         pr_info("%s: passphrase too long\n", __func__);
1443                         ret = -E2BIG;
1444                         goto out;
1445                 }
1446                 memset(wl->psk, 0, sizeof(wl->psk));
1447                 wl->psk_len = len - 2;
1448                 memcpy(wl->psk, &(extra[1]), wl->psk_len);
1449                 wl->psk_type = GELIC_EURUS_WPA_PSK_PASSPHRASE;
1450         } else {
1451                 ret = hex2bin(extra, wl->psk, len);
1452                 if (ret)
1453                         goto out;
1454                 wl->psk_len = WPA_PSK_LEN;
1455                 wl->psk_type = GELIC_EURUS_WPA_PSK_BIN;
1456         }
1457         set_bit(GELIC_WL_STAT_WPA_PSK_SET, &wl->stat);
1458 out:
1459         spin_unlock_irqrestore(&wl->lock, irqflag);
1460         pr_debug("%s:->\n", __func__);
1461         return ret;
1462 }
1463
1464 static int gelic_wl_priv_get_psk(struct net_device *net_dev,
1465                                  struct iw_request_info *info,
1466                                  union iwreq_data *data, char *extra)
1467 {
1468         struct gelic_wl_info *wl = port_wl(netdev_priv(net_dev));
1469         char *p;
1470         unsigned int irqflag;
1471         unsigned int i;
1472
1473         pr_debug("%s:<-\n", __func__);
1474         if (!capable(CAP_NET_ADMIN))
1475                 return -EPERM;
1476
1477         spin_lock_irqsave(&wl->lock, irqflag);
1478         p = extra;
1479         if (test_bit(GELIC_WL_STAT_WPA_PSK_SET, &wl->stat)) {
1480                 if (wl->psk_type == GELIC_EURUS_WPA_PSK_BIN) {
1481                         for (i = 0; i < wl->psk_len; i++) {
1482                                 sprintf(p, "%02xu", wl->psk[i]);
1483                                 p += 2;
1484                         }
1485                         *p = '\0';
1486                         data->data.length = wl->psk_len * 2;
1487                 } else {
1488                         *p++ = '"';
1489                         memcpy(p, wl->psk, wl->psk_len);
1490                         p += wl->psk_len;
1491                         *p++ = '"';
1492                         *p = '\0';
1493                         data->data.length = wl->psk_len + 2;
1494                 }
1495         } else
1496                 /* no psk set */
1497                 data->data.length = 0;
1498         spin_unlock_irqrestore(&wl->lock, irqflag);
1499         pr_debug("%s:-> %d\n", __func__, data->data.length);
1500         return 0;
1501 }
1502 #endif
1503
1504 /* SIOCGIWNICKN */
1505 static int gelic_wl_get_nick(struct net_device *net_dev,
1506                                   struct iw_request_info *info,
1507                                   union iwreq_data *data, char *extra)
1508 {
1509         strcpy(extra, "gelic_wl");
1510         data->data.length = strlen(extra);
1511         data->data.flags = 1;
1512         return 0;
1513 }
1514
1515
1516 /* --- */
1517
1518 static struct iw_statistics *gelic_wl_get_wireless_stats(
1519         struct net_device *netdev)
1520 {
1521
1522         struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
1523         struct gelic_eurus_cmd *cmd;
1524         struct iw_statistics *is;
1525         struct gelic_eurus_rssi_info *rssi;
1526         void *buf;
1527
1528         pr_debug("%s: <-\n", __func__);
1529
1530         buf = (void *)__get_free_page(GFP_KERNEL);
1531         if (!buf)
1532                 return NULL;
1533
1534         is = &wl->iwstat;
1535         memset(is, 0, sizeof(*is));
1536         cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_GET_RSSI_CFG,
1537                                    buf, sizeof(*rssi));
1538         if (cmd && !cmd->status && !cmd->cmd_status) {
1539                 rssi = buf;
1540                 is->qual.level = be16_to_cpu(rssi->rssi);
1541                 is->qual.updated = IW_QUAL_LEVEL_UPDATED |
1542                         IW_QUAL_QUAL_INVALID | IW_QUAL_NOISE_INVALID;
1543         } else
1544                 /* not associated */
1545                 is->qual.updated = IW_QUAL_ALL_INVALID;
1546
1547         kfree(cmd);
1548         free_page((unsigned long)buf);
1549         pr_debug("%s: ->\n", __func__);
1550         return is;
1551 }
1552
1553 /*
1554  *  scanning helpers
1555  */
1556 static int gelic_wl_start_scan(struct gelic_wl_info *wl, int always_scan,
1557                                u8 *essid, size_t essid_len)
1558 {
1559         struct gelic_eurus_cmd *cmd;
1560         int ret = 0;
1561         void *buf = NULL;
1562         size_t len;
1563
1564         pr_debug("%s: <- always=%d\n", __func__, always_scan);
1565         if (mutex_lock_interruptible(&wl->scan_lock))
1566                 return -ERESTARTSYS;
1567
1568         /*
1569          * If already a scan in progress, do not trigger more
1570          */
1571         if (wl->scan_stat == GELIC_WL_SCAN_STAT_SCANNING) {
1572                 pr_debug("%s: scanning now\n", __func__);
1573                 goto out;
1574         }
1575
1576         init_completion(&wl->scan_done);
1577         /*
1578          * If we have already a bss list, don't try to get new
1579          */
1580         if (!always_scan && wl->scan_stat == GELIC_WL_SCAN_STAT_GOT_LIST) {
1581                 pr_debug("%s: already has the list\n", __func__);
1582                 complete(&wl->scan_done);
1583                 goto out;
1584         }
1585
1586         /* ESSID scan ? */
1587         if (essid_len && essid) {
1588                 buf = (void *)__get_free_page(GFP_KERNEL);
1589                 if (!buf) {
1590                         ret = -ENOMEM;
1591                         goto out;
1592                 }
1593                 len = IW_ESSID_MAX_SIZE; /* hypervisor always requires 32 */
1594                 memset(buf, 0, len);
1595                 memcpy(buf, essid, essid_len);
1596                 pr_debug("%s: essid scan='%s'\n", __func__, (char *)buf);
1597         } else
1598                 len = 0;
1599
1600         /*
1601          * issue start scan request
1602          */
1603         wl->scan_stat = GELIC_WL_SCAN_STAT_SCANNING;
1604         cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_START_SCAN,
1605                                    buf, len);
1606         if (!cmd || cmd->status || cmd->cmd_status) {
1607                 wl->scan_stat = GELIC_WL_SCAN_STAT_INIT;
1608                 complete(&wl->scan_done);
1609                 ret = -ENOMEM;
1610                 goto out;
1611         }
1612         kfree(cmd);
1613 out:
1614         free_page((unsigned long)buf);
1615         mutex_unlock(&wl->scan_lock);
1616         pr_debug("%s: ->\n", __func__);
1617         return ret;
1618 }
1619
1620 /*
1621  * retrieve scan result from the chip (hypervisor)
1622  * this function is invoked by schedule work.
1623  */
1624 static void gelic_wl_scan_complete_event(struct gelic_wl_info *wl)
1625 {
1626         struct gelic_eurus_cmd *cmd = NULL;
1627         struct gelic_wl_scan_info *target, *tmp;
1628         struct gelic_wl_scan_info *oldest = NULL;
1629         struct gelic_eurus_scan_info *scan_info;
1630         unsigned int scan_info_size;
1631         union iwreq_data data;
1632         unsigned long this_time = jiffies;
1633         unsigned int data_len, i, found, r;
1634         void *buf;
1635         DECLARE_MAC_BUF(mac);
1636
1637         pr_debug("%s:start\n", __func__);
1638         mutex_lock(&wl->scan_lock);
1639
1640         buf = (void *)__get_free_page(GFP_KERNEL);
1641         if (!buf) {
1642                 pr_info("%s: scan buffer alloc failed\n", __func__);
1643                 goto out;
1644         }
1645
1646         if (wl->scan_stat != GELIC_WL_SCAN_STAT_SCANNING) {
1647                 /*
1648                  * stop() may be called while scanning, ignore result
1649                  */
1650                 pr_debug("%s: scan complete when stat != scanning(%d)\n",
1651                          __func__, wl->scan_stat);
1652                 goto out;
1653         }
1654
1655         cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_GET_SCAN,
1656                                    buf, PAGE_SIZE);
1657         if (!cmd || cmd->status || cmd->cmd_status) {
1658                 wl->scan_stat = GELIC_WL_SCAN_STAT_INIT;
1659                 pr_info("%s:cmd failed\n", __func__);
1660                 kfree(cmd);
1661                 goto out;
1662         }
1663         data_len = cmd->size;
1664         pr_debug("%s: data_len = %d\n", __func__, data_len);
1665         kfree(cmd);
1666
1667         /* OK, bss list retrieved */
1668         wl->scan_stat = GELIC_WL_SCAN_STAT_GOT_LIST;
1669
1670         /* mark all entries are old */
1671         list_for_each_entry_safe(target, tmp, &wl->network_list, list) {
1672                 target->valid = 0;
1673                 /* expire too old entries */
1674                 if (time_before(target->last_scanned + wl->scan_age,
1675                                 this_time)) {
1676                         kfree(target->hwinfo);
1677                         target->hwinfo = NULL;
1678                         list_move_tail(&target->list, &wl->network_free_list);
1679                 }
1680         }
1681
1682         /* put them in the newtork_list */
1683         for (i = 0, scan_info_size = 0, scan_info = buf;
1684              scan_info_size < data_len;
1685              i++, scan_info_size += be16_to_cpu(scan_info->size),
1686              scan_info = (void *)scan_info + be16_to_cpu(scan_info->size)) {
1687                 pr_debug("%s:size=%d bssid=%s scan_info=%p\n", __func__,
1688                          be16_to_cpu(scan_info->size),
1689                          print_mac(mac, &scan_info->bssid[2]), scan_info);
1690
1691                 /*
1692                  * The wireless firmware may return invalid channel 0 and/or
1693                  * invalid rate if the AP emits zero length SSID ie. As this
1694                  * scan information is useless, ignore it
1695                  */
1696                 if (!be16_to_cpu(scan_info->channel) || !scan_info->rate[0]) {
1697                         pr_debug("%s: invalid scan info\n", __func__);
1698                         continue;
1699                 }
1700
1701                 found = 0;
1702                 oldest = NULL;
1703                 list_for_each_entry(target, &wl->network_list, list) {
1704                         if (!compare_ether_addr(&target->hwinfo->bssid[2],
1705                                                 &scan_info->bssid[2])) {
1706                                 found = 1;
1707                                 pr_debug("%s: same BBS found scanned list\n",
1708                                          __func__);
1709                                 break;
1710                         }
1711                         if (!oldest ||
1712                             (target->last_scanned < oldest->last_scanned))
1713                                 oldest = target;
1714                 }
1715
1716                 if (!found) {
1717                         /* not found in the list */
1718                         if (list_empty(&wl->network_free_list)) {
1719                                 /* expire oldest */
1720                                 target = oldest;
1721                         } else {
1722                                 target = list_entry(wl->network_free_list.next,
1723                                                     struct gelic_wl_scan_info,
1724                                                     list);
1725                         }
1726                 }
1727
1728                 /* update the item */
1729                 target->last_scanned = this_time;
1730                 target->valid = 1;
1731                 target->eurus_index = i;
1732                 kfree(target->hwinfo);
1733                 target->hwinfo = kzalloc(be16_to_cpu(scan_info->size),
1734                                          GFP_KERNEL);
1735                 if (!target->hwinfo) {
1736                         pr_info("%s: kzalloc failed\n", __func__);
1737                         continue;
1738                 }
1739                 /* copy hw scan info */
1740                 memcpy(target->hwinfo, scan_info, scan_info->size);
1741                 target->essid_len = strnlen(scan_info->essid,
1742                                             sizeof(scan_info->essid));
1743                 target->rate_len = 0;
1744                 for (r = 0; r < MAX_RATES_LENGTH; r++)
1745                         if (scan_info->rate[r])
1746                                 target->rate_len++;
1747                 if (8 < target->rate_len)
1748                         pr_info("%s: AP returns %d rates\n", __func__,
1749                                 target->rate_len);
1750                 target->rate_ext_len = 0;
1751                 for (r = 0; r < MAX_RATES_EX_LENGTH; r++)
1752                         if (scan_info->ext_rate[r])
1753                                 target->rate_ext_len++;
1754                 list_move_tail(&target->list, &wl->network_list);
1755         }
1756         memset(&data, 0, sizeof(data));
1757         wireless_send_event(port_to_netdev(wl_port(wl)), SIOCGIWSCAN, &data,
1758                             NULL);
1759 out:
1760         free_page((unsigned long)buf);
1761         complete(&wl->scan_done);
1762         mutex_unlock(&wl->scan_lock);
1763         pr_debug("%s:end\n", __func__);
1764 }
1765
1766 /*
1767  * Select an appropriate bss from current scan list regarding
1768  * current settings from userspace.
1769  * The caller must hold wl->scan_lock,
1770  * and on the state of wl->scan_state == GELIC_WL_SCAN_GOT_LIST
1771  */
1772 static void update_best(struct gelic_wl_scan_info **best,
1773                         struct gelic_wl_scan_info *candid,
1774                         int *best_weight,
1775                         int *weight)
1776 {
1777         if (*best_weight < ++(*weight)) {
1778                 *best_weight = *weight;
1779                 *best = candid;
1780         }
1781 }
1782
1783 static
1784 struct gelic_wl_scan_info *gelic_wl_find_best_bss(struct gelic_wl_info *wl)
1785 {
1786         struct gelic_wl_scan_info *scan_info;
1787         struct gelic_wl_scan_info *best_bss;
1788         int weight, best_weight;
1789         u16 security;
1790         DECLARE_MAC_BUF(mac);
1791
1792         pr_debug("%s: <-\n", __func__);
1793
1794         best_bss = NULL;
1795         best_weight = 0;
1796
1797         list_for_each_entry(scan_info, &wl->network_list, list) {
1798                 pr_debug("%s: station %p\n", __func__, scan_info);
1799
1800                 if (!scan_info->valid) {
1801                         pr_debug("%s: station invalid\n", __func__);
1802                         continue;
1803                 }
1804
1805                 /* If bss specified, check it only */
1806                 if (test_bit(GELIC_WL_STAT_BSSID_SET, &wl->stat)) {
1807                         if (!compare_ether_addr(&scan_info->hwinfo->bssid[2],
1808                                                 wl->bssid)) {
1809                                 best_bss = scan_info;
1810                                 pr_debug("%s: bssid matched\n", __func__);
1811                                 break;
1812                         } else {
1813                                 pr_debug("%s: bssid unmached\n", __func__);
1814                                 continue;
1815                         }
1816                 }
1817
1818                 weight = 0;
1819
1820                 /* security */
1821                 security = be16_to_cpu(scan_info->hwinfo->security) &
1822                         GELIC_EURUS_SCAN_SEC_MASK;
1823                 if (wl->wpa_level == GELIC_WL_WPA_LEVEL_WPA2) {
1824                         if (security == GELIC_EURUS_SCAN_SEC_WPA2)
1825                                 update_best(&best_bss, scan_info,
1826                                             &best_weight, &weight);
1827                         else
1828                                 continue;
1829                 } else if (wl->wpa_level == GELIC_WL_WPA_LEVEL_WPA) {
1830                         if (security == GELIC_EURUS_SCAN_SEC_WPA)
1831                                 update_best(&best_bss, scan_info,
1832                                             &best_weight, &weight);
1833                         else
1834                                 continue;
1835                 } else if (wl->wpa_level == GELIC_WL_WPA_LEVEL_NONE &&
1836                            wl->group_cipher_method == GELIC_WL_CIPHER_WEP) {
1837                         if (security == GELIC_EURUS_SCAN_SEC_WEP)
1838                                 update_best(&best_bss, scan_info,
1839                                             &best_weight, &weight);
1840                         else
1841                                 continue;
1842                 }
1843
1844                 /* If ESSID is set, check it */
1845                 if (test_bit(GELIC_WL_STAT_ESSID_SET, &wl->stat)) {
1846                         if ((scan_info->essid_len == wl->essid_len) &&
1847                             !strncmp(wl->essid,
1848                                      scan_info->hwinfo->essid,
1849                                      scan_info->essid_len))
1850                                 update_best(&best_bss, scan_info,
1851                                             &best_weight, &weight);
1852                         else
1853                                 continue;
1854                 }
1855         }
1856
1857 #ifdef DEBUG
1858         pr_debug("%s: -> bss=%p\n", __func__, best_bss);
1859         if (best_bss) {
1860                 pr_debug("%s:addr=%s\n", __func__,
1861                          print_mac(mac, &best_bss->hwinfo->bssid[2]));
1862         }
1863 #endif
1864         return best_bss;
1865 }
1866
1867 /*
1868  * Setup WEP configuration to the chip
1869  * The caller must hold wl->scan_lock,
1870  * and on the state of wl->scan_state == GELIC_WL_SCAN_GOT_LIST
1871  */
1872 static int gelic_wl_do_wep_setup(struct gelic_wl_info *wl)
1873 {
1874         unsigned int i;
1875         struct gelic_eurus_wep_cfg *wep;
1876         struct gelic_eurus_cmd *cmd;
1877         int wep104 = 0;
1878         int have_key = 0;
1879         int ret = 0;
1880
1881         pr_debug("%s: <-\n", __func__);
1882         /* we can assume no one should uses the buffer */
1883         wep = (struct gelic_eurus_wep_cfg *)__get_free_page(GFP_KERNEL);
1884         if (!wep)
1885                 return -ENOMEM;
1886
1887         memset(wep, 0, sizeof(*wep));
1888
1889         if (wl->group_cipher_method == GELIC_WL_CIPHER_WEP) {
1890                 pr_debug("%s: WEP mode\n", __func__);
1891                 for (i = 0; i < GELIC_WEP_KEYS; i++) {
1892                         if (!test_bit(i, &wl->key_enabled))
1893                                 continue;
1894
1895                         pr_debug("%s: key#%d enabled\n", __func__, i);
1896                         have_key = 1;
1897                         if (wl->key_len[i] == 13)
1898                                 wep104 = 1;
1899                         else if (wl->key_len[i] != 5) {
1900                                 pr_info("%s: wrong wep key[%d]=%d\n",
1901                                         __func__, i, wl->key_len[i]);
1902                                 ret = -EINVAL;
1903                                 goto out;
1904                         }
1905                         memcpy(wep->key[i], wl->key[i], wl->key_len[i]);
1906                 }
1907
1908                 if (!have_key) {
1909                         pr_info("%s: all wep key disabled\n", __func__);
1910                         ret = -EINVAL;
1911                         goto out;
1912                 }
1913
1914                 if (wep104) {
1915                         pr_debug("%s: 104bit key\n", __func__);
1916                         wep->security = cpu_to_be16(GELIC_EURUS_WEP_SEC_104BIT);
1917                 } else {
1918                         pr_debug("%s: 40bit key\n", __func__);
1919                         wep->security = cpu_to_be16(GELIC_EURUS_WEP_SEC_40BIT);
1920                 }
1921         } else {
1922                 pr_debug("%s: NO encryption\n", __func__);
1923                 wep->security = cpu_to_be16(GELIC_EURUS_WEP_SEC_NONE);
1924         }
1925
1926         /* issue wep setup */
1927         cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_SET_WEP_CFG,
1928                                    wep, sizeof(*wep));
1929         if (!cmd)
1930                 ret = -ENOMEM;
1931         else if (cmd->status || cmd->cmd_status)
1932                 ret = -ENXIO;
1933
1934         kfree(cmd);
1935 out:
1936         free_page((unsigned long)wep);
1937         pr_debug("%s: ->\n", __func__);
1938         return ret;
1939 }
1940
1941 #ifdef DEBUG
1942 static const char *wpasecstr(enum gelic_eurus_wpa_security sec)
1943 {
1944         switch (sec) {
1945         case GELIC_EURUS_WPA_SEC_NONE:
1946                 return "NONE";
1947                 break;
1948         case GELIC_EURUS_WPA_SEC_WPA_TKIP_TKIP:
1949                 return "WPA_TKIP_TKIP";
1950                 break;
1951         case GELIC_EURUS_WPA_SEC_WPA_TKIP_AES:
1952                 return "WPA_TKIP_AES";
1953                 break;
1954         case GELIC_EURUS_WPA_SEC_WPA_AES_AES:
1955                 return "WPA_AES_AES";
1956                 break;
1957         case GELIC_EURUS_WPA_SEC_WPA2_TKIP_TKIP:
1958                 return "WPA2_TKIP_TKIP";
1959                 break;
1960         case GELIC_EURUS_WPA_SEC_WPA2_TKIP_AES:
1961                 return "WPA2_TKIP_AES";
1962                 break;
1963         case GELIC_EURUS_WPA_SEC_WPA2_AES_AES:
1964                 return "WPA2_AES_AES";
1965                 break;
1966         }
1967         return "";
1968 };
1969 #endif
1970
1971 static int gelic_wl_do_wpa_setup(struct gelic_wl_info *wl)
1972 {
1973         struct gelic_eurus_wpa_cfg *wpa;
1974         struct gelic_eurus_cmd *cmd;
1975         u16 security;
1976         int ret = 0;
1977
1978         pr_debug("%s: <-\n", __func__);
1979         /* we can assume no one should uses the buffer */
1980         wpa = (struct gelic_eurus_wpa_cfg *)__get_free_page(GFP_KERNEL);
1981         if (!wpa)
1982                 return -ENOMEM;
1983
1984         memset(wpa, 0, sizeof(*wpa));
1985
1986         if (!test_bit(GELIC_WL_STAT_WPA_PSK_SET, &wl->stat))
1987                 pr_info("%s: PSK not configured yet\n", __func__);
1988
1989         /* copy key */
1990         memcpy(wpa->psk, wl->psk, wl->psk_len);
1991
1992         /* set security level */
1993         if (wl->wpa_level == GELIC_WL_WPA_LEVEL_WPA2) {
1994                 if (wl->group_cipher_method == GELIC_WL_CIPHER_AES) {
1995                         security = GELIC_EURUS_WPA_SEC_WPA2_AES_AES;
1996                 } else {
1997                         if (wl->pairwise_cipher_method == GELIC_WL_CIPHER_AES &&
1998                             precise_ie())
1999                                 security = GELIC_EURUS_WPA_SEC_WPA2_TKIP_AES;
2000                         else
2001                                 security = GELIC_EURUS_WPA_SEC_WPA2_TKIP_TKIP;
2002                 }
2003         } else {
2004                 if (wl->group_cipher_method == GELIC_WL_CIPHER_AES) {
2005                         security = GELIC_EURUS_WPA_SEC_WPA_AES_AES;
2006                 } else {
2007                         if (wl->pairwise_cipher_method == GELIC_WL_CIPHER_AES &&
2008                             precise_ie())
2009                                 security = GELIC_EURUS_WPA_SEC_WPA_TKIP_AES;
2010                         else
2011                                 security = GELIC_EURUS_WPA_SEC_WPA_TKIP_TKIP;
2012                 }
2013         }
2014         wpa->security = cpu_to_be16(security);
2015
2016         /* PSK type */
2017         wpa->psk_type = cpu_to_be16(wl->psk_type);
2018 #ifdef DEBUG
2019         pr_debug("%s: sec=%s psktype=%s\nn", __func__,
2020                  wpasecstr(wpa->security),
2021                  (wpa->psk_type == GELIC_EURUS_WPA_PSK_BIN) ?
2022                  "BIN" : "passphrase");
2023 #if 0
2024         /*
2025          * don't enable here if you plan to submit
2026          * the debug log because this dumps your precious
2027          * passphrase/key.
2028          */
2029         pr_debug("%s: psk=%s\n",
2030                  (wpa->psk_type == GELIC_EURUS_WPA_PSK_BIN) ?
2031                  (char *)"N/A" : (char *)wpa->psk);
2032 #endif
2033 #endif
2034         /* issue wpa setup */
2035         cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_SET_WPA_CFG,
2036                                    wpa, sizeof(*wpa));
2037         if (!cmd)
2038                 ret = -ENOMEM;
2039         else if (cmd->status || cmd->cmd_status)
2040                 ret = -ENXIO;
2041         kfree(cmd);
2042         free_page((unsigned long)wpa);
2043         pr_debug("%s: --> %d\n", __func__, ret);
2044         return ret;
2045 }
2046
2047 /*
2048  * Start association. caller must hold assoc_stat_lock
2049  */
2050 static int gelic_wl_associate_bss(struct gelic_wl_info *wl,
2051                                   struct gelic_wl_scan_info *bss)
2052 {
2053         struct gelic_eurus_cmd *cmd;
2054         struct gelic_eurus_common_cfg *common;
2055         int ret = 0;
2056         unsigned long rc;
2057
2058         pr_debug("%s: <-\n", __func__);
2059
2060         /* do common config */
2061         common = (struct gelic_eurus_common_cfg *)__get_free_page(GFP_KERNEL);
2062         if (!common)
2063                 return -ENOMEM;
2064
2065         memset(common, 0, sizeof(*common));
2066         common->bss_type = cpu_to_be16(GELIC_EURUS_BSS_INFRA);
2067         common->op_mode = cpu_to_be16(GELIC_EURUS_OPMODE_11BG);
2068
2069         common->scan_index = cpu_to_be16(bss->eurus_index);
2070         switch (wl->auth_method) {
2071         case GELIC_EURUS_AUTH_OPEN:
2072                 common->auth_method = cpu_to_be16(GELIC_EURUS_AUTH_OPEN);
2073                 break;
2074         case GELIC_EURUS_AUTH_SHARED:
2075                 common->auth_method = cpu_to_be16(GELIC_EURUS_AUTH_SHARED);
2076                 break;
2077         }
2078
2079 #ifdef DEBUG
2080         scan_list_dump(wl);
2081 #endif
2082         pr_debug("%s: common cfg index=%d bsstype=%d auth=%d\n", __func__,
2083                  be16_to_cpu(common->scan_index),
2084                  be16_to_cpu(common->bss_type),
2085                  be16_to_cpu(common->auth_method));
2086
2087         cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_SET_COMMON_CFG,
2088                                    common, sizeof(*common));
2089         if (!cmd || cmd->status || cmd->cmd_status) {
2090                 ret = -ENOMEM;
2091                 kfree(cmd);
2092                 goto out;
2093         }
2094         kfree(cmd);
2095
2096         /* WEP/WPA */
2097         switch (wl->wpa_level) {
2098         case GELIC_WL_WPA_LEVEL_NONE:
2099                 /* If WEP or no security, setup WEP config */
2100                 ret = gelic_wl_do_wep_setup(wl);
2101                 break;
2102         case GELIC_WL_WPA_LEVEL_WPA:
2103         case GELIC_WL_WPA_LEVEL_WPA2:
2104                 ret = gelic_wl_do_wpa_setup(wl);
2105                 break;
2106         };
2107
2108         if (ret) {
2109                 pr_debug("%s: WEP/WPA setup failed %d\n", __func__,
2110                          ret);
2111         }
2112
2113         /* start association */
2114         init_completion(&wl->assoc_done);
2115         wl->assoc_stat = GELIC_WL_ASSOC_STAT_ASSOCIATING;
2116         cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_ASSOC,
2117                                    NULL, 0);
2118         if (!cmd || cmd->status || cmd->cmd_status) {
2119                 pr_debug("%s: assoc request failed\n", __func__);
2120                 wl->assoc_stat = GELIC_WL_ASSOC_STAT_DISCONN;
2121                 kfree(cmd);
2122                 ret = -ENOMEM;
2123                 gelic_wl_send_iwap_event(wl, NULL);
2124                 goto out;
2125         }
2126         kfree(cmd);
2127
2128         /* wait for connected event */
2129         rc = wait_for_completion_timeout(&wl->assoc_done, HZ * 4);/*FIXME*/
2130
2131         if (!rc) {
2132                 /* timeouted.  Maybe key or cyrpt mode is wrong */
2133                 pr_info("%s: connect timeout \n", __func__);
2134                 cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_DISASSOC,
2135                                            NULL, 0);
2136                 kfree(cmd);
2137                 wl->assoc_stat = GELIC_WL_ASSOC_STAT_DISCONN;
2138                 gelic_wl_send_iwap_event(wl, NULL);
2139                 ret = -ENXIO;
2140         } else {
2141                 wl->assoc_stat = GELIC_WL_ASSOC_STAT_ASSOCIATED;
2142                 /* copy bssid */
2143                 memcpy(wl->active_bssid, &bss->hwinfo->bssid[2], ETH_ALEN);
2144
2145                 /* send connect event */
2146                 gelic_wl_send_iwap_event(wl, wl->active_bssid);
2147                 pr_info("%s: connected\n", __func__);
2148         }
2149 out:
2150         free_page((unsigned long)common);
2151         pr_debug("%s: ->\n", __func__);
2152         return ret;
2153 }
2154
2155 /*
2156  * connected event
2157  */
2158 static void gelic_wl_connected_event(struct gelic_wl_info *wl,
2159                                      u64 event)
2160 {
2161         u64 desired_event = 0;
2162
2163         switch (wl->wpa_level) {
2164         case GELIC_WL_WPA_LEVEL_NONE:
2165                 desired_event = GELIC_LV1_WL_EVENT_CONNECTED;
2166                 break;
2167         case GELIC_WL_WPA_LEVEL_WPA:
2168         case GELIC_WL_WPA_LEVEL_WPA2:
2169                 desired_event = GELIC_LV1_WL_EVENT_WPA_CONNECTED;
2170                 break;
2171         }
2172
2173         if (desired_event == event) {
2174                 pr_debug("%s: completed \n", __func__);
2175                 complete(&wl->assoc_done);
2176                 netif_carrier_on(port_to_netdev(wl_port(wl)));
2177         } else
2178                 pr_debug("%s: event %#lx under wpa\n",
2179                                  __func__, event);
2180 }
2181
2182 /*
2183  * disconnect event
2184  */
2185 static void gelic_wl_disconnect_event(struct gelic_wl_info *wl,
2186                                       u64 event)
2187 {
2188         struct gelic_eurus_cmd *cmd;
2189         int lock;
2190
2191         /*
2192          * If we fall here in the middle of association,
2193          * associate_bss() should be waiting for complation of
2194          * wl->assoc_done.
2195          * As it waits with timeout, just leave assoc_done
2196          * uncompleted, then it terminates with timeout
2197          */
2198         if (!mutex_trylock(&wl->assoc_stat_lock)) {
2199                 pr_debug("%s: already locked\n", __func__);
2200                 lock = 0;
2201         } else {
2202                 pr_debug("%s: obtain lock\n", __func__);
2203                 lock = 1;
2204         }
2205
2206         cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_DISASSOC, NULL, 0);
2207         kfree(cmd);
2208
2209         /* send disconnected event to the supplicant */
2210         if (wl->assoc_stat == GELIC_WL_ASSOC_STAT_ASSOCIATED)
2211                 gelic_wl_send_iwap_event(wl, NULL);
2212
2213         wl->assoc_stat = GELIC_WL_ASSOC_STAT_DISCONN;
2214         netif_carrier_off(port_to_netdev(wl_port(wl)));
2215
2216         if (lock)
2217                 mutex_unlock(&wl->assoc_stat_lock);
2218 }
2219 /*
2220  * event worker
2221  */
2222 #ifdef DEBUG
2223 static const char *eventstr(enum gelic_lv1_wl_event event)
2224 {
2225         static char buf[32];
2226         char *ret;
2227         if (event & GELIC_LV1_WL_EVENT_DEVICE_READY)
2228                 ret = "EURUS_READY";
2229         else if (event & GELIC_LV1_WL_EVENT_SCAN_COMPLETED)
2230                 ret = "SCAN_COMPLETED";
2231         else if (event & GELIC_LV1_WL_EVENT_DEAUTH)
2232                 ret = "DEAUTH";
2233         else if (event & GELIC_LV1_WL_EVENT_BEACON_LOST)
2234                 ret = "BEACON_LOST";
2235         else if (event & GELIC_LV1_WL_EVENT_CONNECTED)
2236                 ret = "CONNECTED";
2237         else if (event & GELIC_LV1_WL_EVENT_WPA_CONNECTED)
2238                 ret = "WPA_CONNECTED";
2239         else if (event & GELIC_LV1_WL_EVENT_WPA_ERROR)
2240                 ret = "WPA_ERROR";
2241         else {
2242                 sprintf(buf, "Unknown(%#x)", event);
2243                 ret = buf;
2244         }
2245         return ret;
2246 }
2247 #else
2248 static const char *eventstr(enum gelic_lv1_wl_event event)
2249 {
2250         return NULL;
2251 }
2252 #endif
2253 static void gelic_wl_event_worker(struct work_struct *work)
2254 {
2255         struct gelic_wl_info *wl;
2256         struct gelic_port *port;
2257         u64 event, tmp;
2258         int status;
2259
2260         pr_debug("%s:start\n", __func__);
2261         wl = container_of(work, struct gelic_wl_info, event_work.work);
2262         port = wl_port(wl);
2263         while (1) {
2264                 status = lv1_net_control(bus_id(port->card), dev_id(port->card),
2265                                          GELIC_LV1_GET_WLAN_EVENT, 0, 0, 0,
2266                                          &event, &tmp);
2267                 if (status) {
2268                         if (status != LV1_NO_ENTRY)
2269                                 pr_debug("%s:wlan event failed %d\n",
2270                                          __func__, status);
2271                         /* got all events */
2272                         pr_debug("%s:end\n", __func__);
2273                         return;
2274                 }
2275                 pr_debug("%s: event=%s\n", __func__, eventstr(event));
2276                 switch (event) {
2277                 case GELIC_LV1_WL_EVENT_SCAN_COMPLETED:
2278                         gelic_wl_scan_complete_event(wl);
2279                         break;
2280                 case GELIC_LV1_WL_EVENT_BEACON_LOST:
2281                 case GELIC_LV1_WL_EVENT_DEAUTH:
2282                         gelic_wl_disconnect_event(wl, event);
2283                         break;
2284                 case GELIC_LV1_WL_EVENT_CONNECTED:
2285                 case GELIC_LV1_WL_EVENT_WPA_CONNECTED:
2286                         gelic_wl_connected_event(wl, event);
2287                         break;
2288                 default:
2289                         break;
2290                 }
2291         } /* while */
2292 }
2293 /*
2294  * association worker
2295  */
2296 static void gelic_wl_assoc_worker(struct work_struct *work)
2297 {
2298         struct gelic_wl_info *wl;
2299
2300         struct gelic_wl_scan_info *best_bss;
2301         int ret;
2302         unsigned long irqflag;
2303         u8 *essid;
2304         size_t essid_len;
2305
2306         wl = container_of(work, struct gelic_wl_info, assoc_work.work);
2307
2308         mutex_lock(&wl->assoc_stat_lock);
2309
2310         if (wl->assoc_stat != GELIC_WL_ASSOC_STAT_DISCONN)
2311                 goto out;
2312
2313         spin_lock_irqsave(&wl->lock, irqflag);
2314         if (test_bit(GELIC_WL_STAT_ESSID_SET, &wl->stat)) {
2315                 pr_debug("%s: assoc ESSID configured %s\n", __func__,
2316                          wl->essid);
2317                 essid = wl->essid;
2318                 essid_len = wl->essid_len;
2319         } else {
2320                 essid = NULL;
2321                 essid_len = 0;
2322         }
2323         spin_unlock_irqrestore(&wl->lock, irqflag);
2324
2325         ret = gelic_wl_start_scan(wl, 0, essid, essid_len);
2326         if (ret == -ERESTARTSYS) {
2327                 pr_debug("%s: scan start failed association\n", __func__);
2328                 schedule_delayed_work(&wl->assoc_work, HZ/10); /*FIXME*/
2329                 goto out;
2330         } else if (ret) {
2331                 pr_info("%s: scan prerequisite failed\n", __func__);
2332                 goto out;
2333         }
2334
2335         /*
2336          * Wait for bss scan completion
2337          * If we have scan list already, gelic_wl_start_scan()
2338          * returns OK and raises the complete.  Thus,
2339          * it's ok to wait unconditionally here
2340          */
2341         wait_for_completion(&wl->scan_done);
2342
2343         pr_debug("%s: scan done\n", __func__);
2344         mutex_lock(&wl->scan_lock);
2345         if (wl->scan_stat != GELIC_WL_SCAN_STAT_GOT_LIST) {
2346                 gelic_wl_send_iwap_event(wl, NULL);
2347                 pr_info("%s: no scan list. association failed\n", __func__);
2348                 goto scan_lock_out;
2349         }
2350
2351         /* find best matching bss */
2352         best_bss = gelic_wl_find_best_bss(wl);
2353         if (!best_bss) {
2354                 gelic_wl_send_iwap_event(wl, NULL);
2355                 pr_info("%s: no bss matched. association failed\n", __func__);
2356                 goto scan_lock_out;
2357         }
2358
2359         /* ok, do association */
2360         ret = gelic_wl_associate_bss(wl, best_bss);
2361         if (ret)
2362                 pr_info("%s: association failed %d\n", __func__, ret);
2363 scan_lock_out:
2364         mutex_unlock(&wl->scan_lock);
2365 out:
2366         mutex_unlock(&wl->assoc_stat_lock);
2367 }
2368 /*
2369  * Interrupt handler
2370  * Called from the ethernet interrupt handler
2371  * Processes wireless specific virtual interrupts only
2372  */
2373 void gelic_wl_interrupt(struct net_device *netdev, u64 status)
2374 {
2375         struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
2376
2377         if (status & GELIC_CARD_WLAN_COMMAND_COMPLETED) {
2378                 pr_debug("%s:cmd complete\n", __func__);
2379                 complete(&wl->cmd_done_intr);
2380         }
2381
2382         if (status & GELIC_CARD_WLAN_EVENT_RECEIVED) {
2383                 pr_debug("%s:event received\n", __func__);
2384                 queue_delayed_work(wl->event_queue, &wl->event_work, 0);
2385         }
2386 }
2387
2388 /*
2389  * driver helpers
2390  */
2391 #define IW_IOCTL(n) [(n) - SIOCSIWCOMMIT]
2392 static const iw_handler gelic_wl_wext_handler[] =
2393 {
2394         IW_IOCTL(SIOCGIWNAME)           = gelic_wl_get_name,
2395         IW_IOCTL(SIOCGIWRANGE)          = gelic_wl_get_range,
2396         IW_IOCTL(SIOCSIWSCAN)           = gelic_wl_set_scan,
2397         IW_IOCTL(SIOCGIWSCAN)           = gelic_wl_get_scan,
2398         IW_IOCTL(SIOCSIWAUTH)           = gelic_wl_set_auth,
2399         IW_IOCTL(SIOCGIWAUTH)           = gelic_wl_get_auth,
2400         IW_IOCTL(SIOCSIWESSID)          = gelic_wl_set_essid,
2401         IW_IOCTL(SIOCGIWESSID)          = gelic_wl_get_essid,
2402         IW_IOCTL(SIOCSIWENCODE)         = gelic_wl_set_encode,
2403         IW_IOCTL(SIOCGIWENCODE)         = gelic_wl_get_encode,
2404         IW_IOCTL(SIOCSIWAP)             = gelic_wl_set_ap,
2405         IW_IOCTL(SIOCGIWAP)             = gelic_wl_get_ap,
2406         IW_IOCTL(SIOCSIWENCODEEXT)      = gelic_wl_set_encodeext,
2407         IW_IOCTL(SIOCGIWENCODEEXT)      = gelic_wl_get_encodeext,
2408         IW_IOCTL(SIOCSIWMODE)           = gelic_wl_set_mode,
2409         IW_IOCTL(SIOCGIWMODE)           = gelic_wl_get_mode,
2410         IW_IOCTL(SIOCGIWNICKN)          = gelic_wl_get_nick,
2411 };
2412
2413 #ifdef CONFIG_GELIC_WIRELESS_OLD_PSK_INTERFACE
2414 static struct iw_priv_args gelic_wl_private_args[] =
2415 {
2416         {
2417                 .cmd = GELIC_WL_PRIV_SET_PSK,
2418                 .set_args = IW_PRIV_TYPE_CHAR |
2419                 (GELIC_WL_EURUS_PSK_MAX_LEN + 2),
2420                 .name = "set_psk"
2421         },
2422         {
2423                 .cmd = GELIC_WL_PRIV_GET_PSK,
2424                 .get_args = IW_PRIV_TYPE_CHAR |
2425                 (GELIC_WL_EURUS_PSK_MAX_LEN + 2),
2426                 .name = "get_psk"
2427         }
2428 };
2429
2430 static const iw_handler gelic_wl_private_handler[] =
2431 {
2432         gelic_wl_priv_set_psk,
2433         gelic_wl_priv_get_psk,
2434 };
2435 #endif
2436
2437 static const struct iw_handler_def gelic_wl_wext_handler_def = {
2438         .num_standard           = ARRAY_SIZE(gelic_wl_wext_handler),
2439         .standard               = gelic_wl_wext_handler,
2440         .get_wireless_stats     = gelic_wl_get_wireless_stats,
2441 #ifdef CONFIG_GELIC_WIRELESS_OLD_PSK_INTERFACE
2442         .num_private            = ARRAY_SIZE(gelic_wl_private_handler),
2443         .num_private_args       = ARRAY_SIZE(gelic_wl_private_args),
2444         .private                = gelic_wl_private_handler,
2445         .private_args           = gelic_wl_private_args,
2446 #endif
2447 };
2448
2449 static struct net_device *gelic_wl_alloc(struct gelic_card *card)
2450 {
2451         struct net_device *netdev;
2452         struct gelic_port *port;
2453         struct gelic_wl_info *wl;
2454         unsigned int i;
2455
2456         pr_debug("%s:start\n", __func__);
2457         netdev = alloc_etherdev(sizeof(struct gelic_port) +
2458                                 sizeof(struct gelic_wl_info));
2459         pr_debug("%s: netdev =%p card=%p \np", __func__, netdev, card);
2460         if (!netdev)
2461                 return NULL;
2462
2463         strcpy(netdev->name, "wlan%d");
2464
2465         port = netdev_priv(netdev);
2466         port->netdev = netdev;
2467         port->card = card;
2468         port->type = GELIC_PORT_WIRELESS;
2469
2470         wl = port_wl(port);
2471         pr_debug("%s: wl=%p port=%p\n", __func__, wl, port);
2472
2473         /* allocate scan list */
2474         wl->networks = kzalloc(sizeof(struct gelic_wl_scan_info) *
2475                                GELIC_WL_BSS_MAX_ENT, GFP_KERNEL);
2476
2477         if (!wl->networks)
2478                 goto fail_bss;
2479
2480         wl->eurus_cmd_queue = create_singlethread_workqueue("gelic_cmd");
2481         if (!wl->eurus_cmd_queue)
2482                 goto fail_cmd_workqueue;
2483
2484         wl->event_queue = create_singlethread_workqueue("gelic_event");
2485         if (!wl->event_queue)
2486                 goto fail_event_workqueue;
2487
2488         INIT_LIST_HEAD(&wl->network_free_list);
2489         INIT_LIST_HEAD(&wl->network_list);
2490         for (i = 0; i < GELIC_WL_BSS_MAX_ENT; i++)
2491                 list_add_tail(&wl->networks[i].list,
2492                               &wl->network_free_list);
2493         init_completion(&wl->cmd_done_intr);
2494
2495         INIT_DELAYED_WORK(&wl->event_work, gelic_wl_event_worker);
2496         INIT_DELAYED_WORK(&wl->assoc_work, gelic_wl_assoc_worker);
2497         mutex_init(&wl->scan_lock);
2498         mutex_init(&wl->assoc_stat_lock);
2499
2500         init_completion(&wl->scan_done);
2501         /* for the case that no scan request is issued and stop() is called */
2502         complete(&wl->scan_done);
2503
2504         spin_lock_init(&wl->lock);
2505
2506         wl->scan_age = 5*HZ; /* FIXME */
2507
2508         /* buffer for receiving scanned list etc */
2509         BUILD_BUG_ON(PAGE_SIZE <
2510                      sizeof(struct gelic_eurus_scan_info) *
2511                      GELIC_EURUS_MAX_SCAN);
2512         pr_debug("%s:end\n", __func__);
2513         return netdev;
2514
2515 fail_event_workqueue:
2516         destroy_workqueue(wl->eurus_cmd_queue);
2517 fail_cmd_workqueue:
2518         kfree(wl->networks);
2519 fail_bss:
2520         free_netdev(netdev);
2521         pr_debug("%s:end error\n", __func__);
2522         return NULL;
2523
2524 }
2525
2526 static void gelic_wl_free(struct gelic_wl_info *wl)
2527 {
2528         struct gelic_wl_scan_info *scan_info;
2529         unsigned int i;
2530
2531         pr_debug("%s: <-\n", __func__);
2532
2533         pr_debug("%s: destroy queues\n", __func__);
2534         destroy_workqueue(wl->eurus_cmd_queue);
2535         destroy_workqueue(wl->event_queue);
2536
2537         scan_info = wl->networks;
2538         for (i = 0; i < GELIC_WL_BSS_MAX_ENT; i++, scan_info++)
2539                 kfree(scan_info->hwinfo);
2540         kfree(wl->networks);
2541
2542         free_netdev(port_to_netdev(wl_port(wl)));
2543
2544         pr_debug("%s: ->\n", __func__);
2545 }
2546
2547 static int gelic_wl_try_associate(struct net_device *netdev)
2548 {
2549         struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
2550         int ret = -1;
2551         unsigned int i;
2552
2553         pr_debug("%s: <-\n", __func__);
2554
2555         /* check constraits for start association */
2556         /* for no access restriction AP */
2557         if (wl->group_cipher_method == GELIC_WL_CIPHER_NONE) {
2558                 if (test_bit(GELIC_WL_STAT_CONFIGURED,
2559                              &wl->stat))
2560                         goto do_associate;
2561                 else {
2562                         pr_debug("%s: no wep, not configured\n", __func__);
2563                         return ret;
2564                 }
2565         }
2566
2567         /* for WEP, one of four keys should be set */
2568         if (wl->group_cipher_method == GELIC_WL_CIPHER_WEP) {
2569                 /* one of keys set */
2570                 for (i = 0; i < GELIC_WEP_KEYS; i++) {
2571                         if (test_bit(i, &wl->key_enabled))
2572                             goto do_associate;
2573                 }
2574                 pr_debug("%s: WEP, but no key specified\n", __func__);
2575                 return ret;
2576         }
2577
2578         /* for WPA[2], psk should be set */
2579         if ((wl->group_cipher_method == GELIC_WL_CIPHER_TKIP) ||
2580             (wl->group_cipher_method == GELIC_WL_CIPHER_AES)) {
2581                 if (test_bit(GELIC_WL_STAT_WPA_PSK_SET,
2582                              &wl->stat))
2583                         goto do_associate;
2584                 else {
2585                         pr_debug("%s: AES/TKIP, but PSK not configured\n",
2586                                  __func__);
2587                         return ret;
2588                 }
2589         }
2590
2591 do_associate:
2592         ret = schedule_delayed_work(&wl->assoc_work, 0);
2593         pr_debug("%s: start association work %d\n", __func__, ret);
2594         return ret;
2595 }
2596
2597 /*
2598  * netdev handlers
2599  */
2600 static int gelic_wl_open(struct net_device *netdev)
2601 {
2602         struct gelic_card *card = netdev_card(netdev);
2603
2604         pr_debug("%s:->%p\n", __func__, netdev);
2605
2606         gelic_card_up(card);
2607
2608         /* try to associate */
2609         gelic_wl_try_associate(netdev);
2610
2611         netif_start_queue(netdev);
2612
2613         pr_debug("%s:<-\n", __func__);
2614         return 0;
2615 }
2616
2617 /*
2618  * reset state machine
2619  */
2620 static int gelic_wl_reset_state(struct gelic_wl_info *wl)
2621 {
2622         struct gelic_wl_scan_info *target;
2623         struct gelic_wl_scan_info *tmp;
2624
2625         /* empty scan list */
2626         list_for_each_entry_safe(target, tmp, &wl->network_list, list) {
2627                 list_move_tail(&target->list, &wl->network_free_list);
2628         }
2629         wl->scan_stat = GELIC_WL_SCAN_STAT_INIT;
2630
2631         /* clear configuration */
2632         wl->auth_method = GELIC_EURUS_AUTH_OPEN;
2633         wl->group_cipher_method = GELIC_WL_CIPHER_NONE;
2634         wl->pairwise_cipher_method = GELIC_WL_CIPHER_NONE;
2635         wl->wpa_level = GELIC_WL_WPA_LEVEL_NONE;
2636
2637         wl->key_enabled = 0;
2638         wl->current_key = 0;
2639
2640         wl->psk_type = GELIC_EURUS_WPA_PSK_PASSPHRASE;
2641         wl->psk_len = 0;
2642
2643         wl->essid_len = 0;
2644         memset(wl->essid, 0, sizeof(wl->essid));
2645         memset(wl->bssid, 0, sizeof(wl->bssid));
2646         memset(wl->active_bssid, 0, sizeof(wl->active_bssid));
2647
2648         wl->assoc_stat = GELIC_WL_ASSOC_STAT_DISCONN;
2649
2650         memset(&wl->iwstat, 0, sizeof(wl->iwstat));
2651         /* all status bit clear */
2652         wl->stat = 0;
2653         return 0;
2654 }
2655
2656 /*
2657  * Tell eurus to terminate association
2658  */
2659 static void gelic_wl_disconnect(struct net_device *netdev)
2660 {
2661         struct gelic_port *port = netdev_priv(netdev);
2662         struct gelic_wl_info *wl = port_wl(port);
2663         struct gelic_eurus_cmd *cmd;
2664
2665         /*
2666          * If scann process is running on chip,
2667          * further requests will be rejected
2668          */
2669         if (wl->scan_stat == GELIC_WL_SCAN_STAT_SCANNING)
2670                 wait_for_completion_timeout(&wl->scan_done, HZ);
2671
2672         cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_DISASSOC, NULL, 0);
2673         kfree(cmd);
2674         gelic_wl_send_iwap_event(wl, NULL);
2675 };
2676
2677 static int gelic_wl_stop(struct net_device *netdev)
2678 {
2679         struct gelic_port *port = netdev_priv(netdev);
2680         struct gelic_wl_info *wl = port_wl(port);
2681         struct gelic_card *card = netdev_card(netdev);
2682
2683         pr_debug("%s:<-\n", __func__);
2684
2685         /*
2686          * Cancel pending association work.
2687          * event work can run after netdev down
2688          */
2689         cancel_delayed_work(&wl->assoc_work);
2690
2691         if (wl->assoc_stat == GELIC_WL_ASSOC_STAT_ASSOCIATED)
2692                 gelic_wl_disconnect(netdev);
2693
2694         /* reset our state machine */
2695         gelic_wl_reset_state(wl);
2696
2697         netif_stop_queue(netdev);
2698
2699         gelic_card_down(card);
2700
2701         pr_debug("%s:->\n", __func__);
2702         return 0;
2703 }
2704
2705 /* -- */
2706
2707 static struct ethtool_ops gelic_wl_ethtool_ops = {
2708         .get_drvinfo    = gelic_net_get_drvinfo,
2709         .get_link       = gelic_wl_get_link,
2710         .get_tx_csum    = ethtool_op_get_tx_csum,
2711         .set_tx_csum    = ethtool_op_set_tx_csum,
2712         .get_rx_csum    = gelic_net_get_rx_csum,
2713         .set_rx_csum    = gelic_net_set_rx_csum,
2714 };
2715
2716 static void gelic_wl_setup_netdev_ops(struct net_device *netdev)
2717 {
2718         struct gelic_wl_info *wl;
2719         wl = port_wl(netdev_priv(netdev));
2720         BUG_ON(!wl);
2721         netdev->open = &gelic_wl_open;
2722         netdev->stop = &gelic_wl_stop;
2723         netdev->hard_start_xmit = &gelic_net_xmit;
2724         netdev->set_multicast_list = &gelic_net_set_multi;
2725         netdev->change_mtu = &gelic_net_change_mtu;
2726         netdev->wireless_data = &wl->wireless_data;
2727         netdev->wireless_handlers = &gelic_wl_wext_handler_def;
2728         /* tx watchdog */
2729         netdev->tx_timeout = &gelic_net_tx_timeout;
2730         netdev->watchdog_timeo = GELIC_NET_WATCHDOG_TIMEOUT;
2731
2732         netdev->ethtool_ops = &gelic_wl_ethtool_ops;
2733 #ifdef CONFIG_NET_POLL_CONTROLLER
2734         netdev->poll_controller = gelic_net_poll_controller;
2735 #endif
2736 }
2737
2738 /*
2739  * driver probe/remove
2740  */
2741 int gelic_wl_driver_probe(struct gelic_card *card)
2742 {
2743         int ret;
2744         struct net_device *netdev;
2745
2746         pr_debug("%s:start\n", __func__);
2747
2748         if (ps3_compare_firmware_version(1, 6, 0) < 0)
2749                 return 0;
2750         if (!card->vlan[GELIC_PORT_WIRELESS].tx)
2751                 return 0;
2752
2753         /* alloc netdevice for wireless */
2754         netdev = gelic_wl_alloc(card);
2755         if (!netdev)
2756                 return -ENOMEM;
2757
2758         /* setup net_device structure */
2759         SET_NETDEV_DEV(netdev, &card->dev->core);
2760         gelic_wl_setup_netdev_ops(netdev);
2761
2762         /* setup some of net_device and register it */
2763         ret = gelic_net_setup_netdev(netdev, card);
2764         if (ret)
2765                 goto fail_setup;
2766         card->netdev[GELIC_PORT_WIRELESS] = netdev;
2767
2768         /* add enable wireless interrupt */
2769         card->irq_mask |= GELIC_CARD_WLAN_EVENT_RECEIVED |
2770                 GELIC_CARD_WLAN_COMMAND_COMPLETED;
2771         /* to allow wireless commands while both interfaces are down */
2772         gelic_card_set_irq_mask(card, GELIC_CARD_WLAN_EVENT_RECEIVED |
2773                                 GELIC_CARD_WLAN_COMMAND_COMPLETED);
2774         pr_debug("%s:end\n", __func__);
2775         return 0;
2776
2777 fail_setup:
2778         gelic_wl_free(port_wl(netdev_port(netdev)));
2779
2780         return ret;
2781 }
2782
2783 int gelic_wl_driver_remove(struct gelic_card *card)
2784 {
2785         struct gelic_wl_info *wl;
2786         struct net_device *netdev;
2787
2788         pr_debug("%s:start\n", __func__);
2789
2790         if (ps3_compare_firmware_version(1, 6, 0) < 0)
2791                 return 0;
2792         if (!card->vlan[GELIC_PORT_WIRELESS].tx)
2793                 return 0;
2794
2795         netdev = card->netdev[GELIC_PORT_WIRELESS];
2796         wl = port_wl(netdev_priv(netdev));
2797
2798         /* if the interface was not up, but associated */
2799         if (wl->assoc_stat == GELIC_WL_ASSOC_STAT_ASSOCIATED)
2800                 gelic_wl_disconnect(netdev);
2801
2802         complete(&wl->cmd_done_intr);
2803
2804         /* cancel all work queue */
2805         cancel_delayed_work(&wl->assoc_work);
2806         cancel_delayed_work(&wl->event_work);
2807         flush_workqueue(wl->eurus_cmd_queue);
2808         flush_workqueue(wl->event_queue);
2809
2810         unregister_netdev(netdev);
2811
2812         /* disable wireless interrupt */
2813         pr_debug("%s: disable intr\n", __func__);
2814         card->irq_mask &= ~(GELIC_CARD_WLAN_EVENT_RECEIVED |
2815                             GELIC_CARD_WLAN_COMMAND_COMPLETED);
2816         /* free bss list, netdev*/
2817         gelic_wl_free(wl);
2818         pr_debug("%s:end\n", __func__);
2819         return 0;
2820 }