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