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