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