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