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