blob: d520e9e8cdb2305757a46e2ac91772ea3908e0ff [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>
Johannes Berg2c7060022008-10-30 22:09:54 +010033#include <linux/ieee80211.h>
34#include <linux/if_arp.h>
Masakazu Mokuno09dde542008-02-07 19:58:57 +090035#include <linux/ctype.h>
36#include <linux/string.h>
37#include <net/iw_handler.h>
Masakazu Mokuno09dde542008-02-07 19:58:57 +090038
39#include <linux/dma-mapping.h>
40#include <net/checksum.h>
41#include <asm/firmware.h>
42#include <asm/ps3.h>
43#include <asm/lv1call.h>
44
45#include "ps3_gelic_net.h"
46#include "ps3_gelic_wireless.h"
47
48
Masakazu Mokuno7fd871e2008-06-02 12:49:16 +090049static int gelic_wl_start_scan(struct gelic_wl_info *wl, int always_scan,
50 u8 *essid, size_t essid_len);
Masakazu Mokuno09dde542008-02-07 19:58:57 +090051static int gelic_wl_try_associate(struct net_device *netdev);
52
53/*
54 * tables
55 */
56
57/* 802.11b/g channel to freq in MHz */
58static const int channel_freq[] = {
59 2412, 2417, 2422, 2427, 2432,
60 2437, 2442, 2447, 2452, 2457,
61 2462, 2467, 2472, 2484
62};
63#define NUM_CHANNELS ARRAY_SIZE(channel_freq)
64
65/* in bps */
66static const int bitrate_list[] = {
67 1000000,
68 2000000,
69 5500000,
70 11000000,
71 6000000,
72 9000000,
73 12000000,
74 18000000,
75 24000000,
76 36000000,
77 48000000,
78 54000000
79};
80#define NUM_BITRATES ARRAY_SIZE(bitrate_list)
81
82/*
83 * wpa2 support requires the hypervisor version 2.0 or later
84 */
85static inline int wpa2_capable(void)
86{
87 return (0 <= ps3_compare_firmware_version(2, 0, 0));
88}
89
90static inline int precise_ie(void)
91{
Masakazu Mokuno49d20fa2008-03-25 16:21:08 +090092 return (0 <= ps3_compare_firmware_version(2, 2, 0));
Masakazu Mokuno09dde542008-02-07 19:58:57 +090093}
94/*
95 * post_eurus_cmd helpers
96 */
97struct eurus_cmd_arg_info {
98 int pre_arg; /* command requres arg1, arg2 at POST COMMAND */
99 int post_arg; /* command requires arg1, arg2 at GET_RESULT */
100};
101
102static const struct eurus_cmd_arg_info cmd_info[GELIC_EURUS_CMD_MAX_INDEX] = {
103 [GELIC_EURUS_CMD_SET_COMMON_CFG] = { .pre_arg = 1},
104 [GELIC_EURUS_CMD_SET_WEP_CFG] = { .pre_arg = 1},
105 [GELIC_EURUS_CMD_SET_WPA_CFG] = { .pre_arg = 1},
106 [GELIC_EURUS_CMD_GET_COMMON_CFG] = { .post_arg = 1},
107 [GELIC_EURUS_CMD_GET_WEP_CFG] = { .post_arg = 1},
108 [GELIC_EURUS_CMD_GET_WPA_CFG] = { .post_arg = 1},
109 [GELIC_EURUS_CMD_GET_RSSI_CFG] = { .post_arg = 1},
Masakazu Mokuno7fd871e2008-06-02 12:49:16 +0900110 [GELIC_EURUS_CMD_START_SCAN] = { .pre_arg = 1},
Masakazu Mokuno09dde542008-02-07 19:58:57 +0900111 [GELIC_EURUS_CMD_GET_SCAN] = { .post_arg = 1},
112};
113
114#ifdef DEBUG
115static const char *cmdstr(enum gelic_eurus_command ix)
116{
117 switch (ix) {
118 case GELIC_EURUS_CMD_ASSOC:
119 return "ASSOC";
120 case GELIC_EURUS_CMD_DISASSOC:
121 return "DISASSOC";
122 case GELIC_EURUS_CMD_START_SCAN:
123 return "SCAN";
124 case GELIC_EURUS_CMD_GET_SCAN:
125 return "GET SCAN";
126 case GELIC_EURUS_CMD_SET_COMMON_CFG:
127 return "SET_COMMON_CFG";
128 case GELIC_EURUS_CMD_GET_COMMON_CFG:
129 return "GET_COMMON_CFG";
130 case GELIC_EURUS_CMD_SET_WEP_CFG:
131 return "SET_WEP_CFG";
132 case GELIC_EURUS_CMD_GET_WEP_CFG:
133 return "GET_WEP_CFG";
134 case GELIC_EURUS_CMD_SET_WPA_CFG:
135 return "SET_WPA_CFG";
136 case GELIC_EURUS_CMD_GET_WPA_CFG:
137 return "GET_WPA_CFG";
138 case GELIC_EURUS_CMD_GET_RSSI_CFG:
139 return "GET_RSSI";
140 default:
141 break;
142 }
143 return "";
144};
145#else
146static inline const char *cmdstr(enum gelic_eurus_command ix)
147{
148 return "";
149}
150#endif
151
152/* synchronously do eurus commands */
153static void gelic_eurus_sync_cmd_worker(struct work_struct *work)
154{
155 struct gelic_eurus_cmd *cmd;
156 struct gelic_card *card;
157 struct gelic_wl_info *wl;
158
159 u64 arg1, arg2;
160
161 pr_debug("%s: <-\n", __func__);
162 cmd = container_of(work, struct gelic_eurus_cmd, work);
163 BUG_ON(cmd_info[cmd->cmd].pre_arg &&
164 cmd_info[cmd->cmd].post_arg);
165 wl = cmd->wl;
166 card = port_to_card(wl_port(wl));
167
168 if (cmd_info[cmd->cmd].pre_arg) {
Masakazu Mokuno7fd871e2008-06-02 12:49:16 +0900169 arg1 = (cmd->buffer) ?
170 ps3_mm_phys_to_lpar(__pa(cmd->buffer)) :
171 0;
Masakazu Mokuno09dde542008-02-07 19:58:57 +0900172 arg2 = cmd->buf_size;
173 } else {
174 arg1 = 0;
175 arg2 = 0;
176 }
177 init_completion(&wl->cmd_done_intr);
178 pr_debug("%s: cmd='%s' start\n", __func__, cmdstr(cmd->cmd));
179 cmd->status = lv1_net_control(bus_id(card), dev_id(card),
180 GELIC_LV1_POST_WLAN_CMD,
181 cmd->cmd, arg1, arg2,
182 &cmd->tag, &cmd->size);
183 if (cmd->status) {
184 complete(&cmd->done);
185 pr_info("%s: cmd issue failed\n", __func__);
186 return;
187 }
188
189 wait_for_completion(&wl->cmd_done_intr);
190
191 if (cmd_info[cmd->cmd].post_arg) {
192 arg1 = ps3_mm_phys_to_lpar(__pa(cmd->buffer));
193 arg2 = cmd->buf_size;
194 } else {
195 arg1 = 0;
196 arg2 = 0;
197 }
198
199 cmd->status = lv1_net_control(bus_id(card), dev_id(card),
200 GELIC_LV1_GET_WLAN_CMD_RESULT,
201 cmd->tag, arg1, arg2,
202 &cmd->cmd_status, &cmd->size);
203#ifdef DEBUG
204 if (cmd->status || cmd->cmd_status) {
205 pr_debug("%s: cmd done tag=%#lx arg1=%#lx, arg2=%#lx\n", __func__,
206 cmd->tag, arg1, arg2);
207 pr_debug("%s: cmd done status=%#x cmd_status=%#lx size=%#lx\n",
208 __func__, cmd->status, cmd->cmd_status, cmd->size);
209 }
210#endif
211 complete(&cmd->done);
212 pr_debug("%s: cmd='%s' done\n", __func__, cmdstr(cmd->cmd));
213}
214
215static struct gelic_eurus_cmd *gelic_eurus_sync_cmd(struct gelic_wl_info *wl,
216 unsigned int eurus_cmd,
217 void *buffer,
218 unsigned int buf_size)
219{
220 struct gelic_eurus_cmd *cmd;
221
222 /* allocate cmd */
223 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
224 if (!cmd)
225 return NULL;
226
227 /* initialize members */
228 cmd->cmd = eurus_cmd;
229 cmd->buffer = buffer;
230 cmd->buf_size = buf_size;
231 cmd->wl = wl;
232 INIT_WORK(&cmd->work, gelic_eurus_sync_cmd_worker);
233 init_completion(&cmd->done);
234 queue_work(wl->eurus_cmd_queue, &cmd->work);
235
236 /* wait for command completion */
237 wait_for_completion(&cmd->done);
238
239 return cmd;
240}
241
242static u32 gelic_wl_get_link(struct net_device *netdev)
243{
244 struct gelic_wl_info *wl = port_wl(netdev_port(netdev));
245 u32 ret;
246
247 pr_debug("%s: <-\n", __func__);
Daniel Walkerbb2d67a2008-05-22 00:00:02 -0700248 mutex_lock(&wl->assoc_stat_lock);
Masakazu Mokuno09dde542008-02-07 19:58:57 +0900249 if (wl->assoc_stat == GELIC_WL_ASSOC_STAT_ASSOCIATED)
250 ret = 1;
251 else
252 ret = 0;
Daniel Walkerbb2d67a2008-05-22 00:00:02 -0700253 mutex_unlock(&wl->assoc_stat_lock);
Masakazu Mokuno09dde542008-02-07 19:58:57 +0900254 pr_debug("%s: ->\n", __func__);
255 return ret;
256}
257
258static void gelic_wl_send_iwap_event(struct gelic_wl_info *wl, u8 *bssid)
259{
260 union iwreq_data data;
261
262 memset(&data, 0, sizeof(data));
263 if (bssid)
264 memcpy(data.ap_addr.sa_data, bssid, ETH_ALEN);
265 data.ap_addr.sa_family = ARPHRD_ETHER;
266 wireless_send_event(port_to_netdev(wl_port(wl)), SIOCGIWAP,
267 &data, NULL);
268}
269
270/*
271 * wireless extension handlers and helpers
272 */
273
274/* SIOGIWNAME */
275static int gelic_wl_get_name(struct net_device *dev,
276 struct iw_request_info *info,
277 union iwreq_data *iwreq, char *extra)
278{
279 strcpy(iwreq->name, "IEEE 802.11bg");
280 return 0;
281}
282
283static void gelic_wl_get_ch_info(struct gelic_wl_info *wl)
284{
285 struct gelic_card *card = port_to_card(wl_port(wl));
286 u64 ch_info_raw, tmp;
287 int status;
288
289 if (!test_and_set_bit(GELIC_WL_STAT_CH_INFO, &wl->stat)) {
290 status = lv1_net_control(bus_id(card), dev_id(card),
291 GELIC_LV1_GET_CHANNEL, 0, 0, 0,
292 &ch_info_raw,
293 &tmp);
294 /* some fw versions may return error */
295 if (status) {
296 if (status != LV1_NO_ENTRY)
297 pr_info("%s: available ch unknown\n", __func__);
298 wl->ch_info = 0x07ff;/* 11 ch */
299 } else
300 /* 16 bits of MSB has available channels */
301 wl->ch_info = ch_info_raw >> 48;
302 }
303 return;
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 |
Masakazu Mokuno04b20462008-05-30 16:52:44 +0900358 IW_ENC_CAPA_CIPHER_TKIP | IW_ENC_CAPA_CIPHER_CCMP |
359 IW_ENC_CAPA_4WAY_HANDSHAKE;
Masakazu Mokuno09dde542008-02-07 19:58:57 +0900360 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
Masakazu Mokuno7fd871e2008-06-02 12:49:16 +0900368 /* scan capability */
369 range->scan_capa = IW_SCAN_CAPA_ESSID;
370
Masakazu Mokuno09dde542008-02-07 19:58:57 +0900371 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));
Masakazu Mokuno7fd871e2008-06-02 12:49:16 +0900382 struct iw_scan_req *req;
383 u8 *essid = NULL;
384 size_t essid_len = 0;
Masakazu Mokuno09dde542008-02-07 19:58:57 +0900385
Masakazu Mokuno7fd871e2008-06-02 12:49:16 +0900386 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);
Masakazu Mokuno09dde542008-02-07 19:58:57 +0900394}
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)
Johannes Berg2c7060022008-10-30 22:09:54 +0100453 *buf++ = WLAN_EID_RSN;
Masakazu Mokuno09dde542008-02-07 19:58:57 +0900454 else
Johannes Berg2c7060022008-10-30 22:09:54 +0100455 *buf++ = WLAN_EID_GENERIC;
Masakazu Mokuno09dde542008-02-07 19:58:57 +0900456
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
Masakazu Mokunob3584922008-04-14 18:07:21 +0900534 while (2 <= data_left) {
Masakazu Mokuno09dde542008-02-07 19:58:57 +0900535 item_id = *pos++;
536 item_len = *pos++;
Masakazu Mokunob3584922008-04-14 18:07:21 +0900537 data_left -= 2;
538
539 if (data_left < item_len)
540 break;
Masakazu Mokuno09dde542008-02-07 19:58:57 +0900541
542 switch (item_id) {
Johannes Berg2c7060022008-10-30 22:09:54 +0100543 case WLAN_EID_GENERIC:
Masakazu Mokunob3584922008-04-14 18:07:21 +0900544 if ((OUI_LEN + 1 <= item_len) &&
545 !memcmp(pos, wpa_oui, OUI_LEN) &&
Masakazu Mokuno09dde542008-02-07 19:58:57 +0900546 pos[OUI_LEN] == 0x01) {
547 ie_info->wpa.data = pos - 2;
548 ie_info->wpa.len = item_len + 2;
549 }
550 break;
Johannes Berg2c7060022008-10-30 22:09:54 +0100551 case WLAN_EID_RSN:
Masakazu Mokuno09dde542008-02-07 19:58:57 +0900552 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;
Masakazu Mokunob3584922008-04-14 18:07:21 +0900562 data_left -= item_len;
Masakazu Mokuno09dde542008-02-07 19:58:57 +0900563 }
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,
David S. Millerccc58052008-06-16 18:50:49 -0700575 struct iw_request_info *info,
Masakazu Mokuno09dde542008-02-07 19:58:57 +0900576 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;
Johannes Berg2c7060022008-10-30 22:09:54 +0100585 u8 buf[64]; /* arbitrary size large enough */
Masakazu Mokuno09dde542008-02-07 19:58:57 +0900586
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);
David S. Millerccc58052008-06-16 18:50:49 -0700593 ev = iwe_stream_add_event(info, ev, stop, &iwe, IW_EV_ADDR_LEN);
Masakazu Mokuno09dde542008-02-07 19:58:57 +0900594
595 /* ESSID */
596 iwe.cmd = SIOCGIWESSID;
597 iwe.u.data.flags = 1;
598 iwe.u.data.length = strnlen(scan->essid, 32);
David S. Millerccc58052008-06-16 18:50:49 -0700599 ev = iwe_stream_add_point(info, ev, stop, &iwe, scan->essid);
Masakazu Mokuno09dde542008-02-07 19:58:57 +0900600
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;
David S. Millerccc58052008-06-16 18:50:49 -0700606 ev = iwe_stream_add_event(info, ev, stop, &iwe, IW_EV_FREQ_LEN);
Masakazu Mokuno09dde542008-02-07 19:58:57 +0900607
608 /* RATES */
609 iwe.cmd = SIOCGIWRATE;
610 iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
611 /* to stuff multiple values in one event */
David S. Millerccc58052008-06-16 18:50:49 -0700612 tmp = ev + iwe_stream_lcp_len(info);
Masakazu Mokuno09dde542008-02-07 19:58:57 +0900613 /* 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 */
David S. Millerccc58052008-06-16 18:50:49 -0700625 tmp = iwe_stream_add_value(info, ev, tmp, stop, &iwe,
Masakazu Mokuno09dde542008-02-07 19:58:57 +0900626 IW_EV_PARAM_LEN);
627 }
628 while (j < network->rate_ext_len) {
629 iwe.u.bitrate.value = (scan->ext_rate[j++] & 0x7f) * 500000;
David S. Millerccc58052008-06-16 18:50:49 -0700630 tmp = iwe_stream_add_value(info, ev, tmp, stop, &iwe,
Masakazu Mokuno09dde542008-02-07 19:58:57 +0900631 IW_EV_PARAM_LEN);
632 }
633 /* Check if we added any rate */
David S. Millerccc58052008-06-16 18:50:49 -0700634 if (iwe_stream_lcp_len(info) < (tmp - ev))
Masakazu Mokuno09dde542008-02-07 19:58:57 +0900635 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;
David S. Millerccc58052008-06-16 18:50:49 -0700644 ev = iwe_stream_add_point(info, ev, stop, &iwe, scan->essid);
Masakazu Mokuno09dde542008-02-07 19:58:57 +0900645
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;
David S. Millerccc58052008-06-16 18:50:49 -0700654 ev = iwe_stream_add_event(info, ev, stop, &iwe, IW_EV_UINT_LEN);
Masakazu Mokuno09dde542008-02-07 19:58:57 +0900655 }
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;
David S. Millerccc58052008-06-16 18:50:49 -0700664 ev = iwe_stream_add_event(info, ev, stop, &iwe, IW_EV_QUAL_LEN);
Masakazu Mokuno09dde542008-02-07 19:58:57 +0900665
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;
David S. Millerccc58052008-06-16 18:50:49 -0700674 ev = iwe_stream_add_point(info, ev, stop, &iwe, buf);
Masakazu Mokuno09dde542008-02-07 19:58:57 +0900675 }
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;
David S. Millerccc58052008-06-16 18:50:49 -0700689 ev = iwe_stream_add_point(info, ev, stop, &iwe, buf);
Masakazu Mokuno09dde542008-02-07 19:58:57 +0900690 }
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;
David S. Millerccc58052008-06-16 18:50:49 -0700697 ev = iwe_stream_add_point(info, ev, stop, &iwe, buf);
Masakazu Mokuno09dde542008-02-07 19:58:57 +0900698 }
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__);
Daniel Walker706ddd62008-05-22 00:00:01 -0700718 if (mutex_lock_interruptible(&wl->scan_lock))
Masakazu Mokuno09dde542008-02-07 19:58:57 +0900719 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))
David S. Millerccc58052008-06-16 18:50:49 -0700742 ev = gelic_wl_translate_scan(netdev, info,
743 ev, stop,
Masakazu Mokuno09dde542008-02-07 19:58:57 +0900744 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:
Daniel Walker706ddd62008-05-22 00:00:01 -0700757 mutex_unlock(&wl->scan_lock);
Masakazu Mokuno09dde542008-02-07 19:58:57 +0900758 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;
Masakazu Mokuno09dde542008-02-07 19:58:57 +0900767
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 /* -- */
Johannes Berge1749612008-10-27 15:59:26 -0700778 pr_debug("bssid=%pM\n", &scan_info->hwinfo->bssid[2]);
Masakazu Mokuno09dde542008-02-07 19:58:57 +0900779 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); /* FIXME */
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__);
Daniel Walkerbb2d67a2008-05-22 00:00:02 -07001001 mutex_lock(&wl->assoc_stat_lock);
Masakazu Mokuno09dde542008-02-07 19:58:57 +09001002 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
Daniel Walkerbb2d67a2008-05-22 00:00:02 -07001011 mutex_unlock(&wl->assoc_stat_lock);
Masakazu Mokuno09dde542008-02-07 19:58:57 +09001012 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;
Vegard Nossum77bbadd2008-07-29 13:31:47 +02001026 unsigned long irqflag;
Masakazu Mokuno09dde542008-02-07 19:58:57 +09001027 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;
Vegard Nossum77bbadd2008-07-29 13:31:47 +02001099 unsigned long irqflag;
Masakazu Mokuno09dde542008-02-07 19:58:57 +09001100 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);
Johannes Berg7c510e42008-10-27 17:47:26 -07001169 pr_debug("%s: bss=%pM\n", __func__, wl->bssid);
Masakazu Mokuno09dde542008-02-07 19:58:57 +09001170 } 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__);
Daniel Walkerbb2d67a2008-05-22 00:00:02 -07001188 mutex_lock(&wl->assoc_stat_lock);
Masakazu Mokuno09dde542008-02-07 19:58:57 +09001189 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);
Daniel Walkerbb2d67a2008-05-22 00:00:02 -07001198 mutex_unlock(&wl->assoc_stat_lock);
Masakazu Mokuno09dde542008-02-07 19:58:57 +09001199 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;
Vegard Nossum77bbadd2008-07-29 13:31:47 +02001213 unsigned long irqflag;
Masakazu Mokuno09dde542008-02-07 19:58:57 +09001214 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);
Masakazu Mokuno04b20462008-05-30 16:52:44 +09001274 } 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);
Masakazu Mokuno09dde542008-02-07 19:58:57 +09001278 ret = -EINVAL;
1279 goto done;
1280 }
Masakazu Mokuno04b20462008-05-30 16:52:44 +09001281 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);
Masakazu Mokuno09dde542008-02-07 19:58:57 +09001287 }
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;
Vegard Nossum77bbadd2008-07-29 13:31:47 +02001301 unsigned long irqflag;
Masakazu Mokuno09dde542008-02-07 19:58:57 +09001302 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
Masakazu Mokuno09dde542008-02-07 19:58:57 +09001392/* 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;
Masakazu Mokuno13de15e2008-05-30 16:27:24 +09001414 void *buf;
Masakazu Mokuno09dde542008-02-07 19:58:57 +09001415
1416 pr_debug("%s: <-\n", __func__);
1417
Masakazu Mokuno13de15e2008-05-30 16:27:24 +09001418 buf = (void *)__get_free_page(GFP_KERNEL);
1419 if (!buf)
1420 return NULL;
1421
Masakazu Mokuno09dde542008-02-07 19:58:57 +09001422 is = &wl->iwstat;
1423 memset(is, 0, sizeof(*is));
1424 cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_GET_RSSI_CFG,
Masakazu Mokuno13de15e2008-05-30 16:27:24 +09001425 buf, sizeof(*rssi));
Masakazu Mokuno09dde542008-02-07 19:58:57 +09001426 if (cmd && !cmd->status && !cmd->cmd_status) {
Masakazu Mokuno13de15e2008-05-30 16:27:24 +09001427 rssi = buf;
Masakazu Mokuno09dde542008-02-07 19:58:57 +09001428 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);
Masakazu Mokuno13de15e2008-05-30 16:27:24 +09001436 free_page((unsigned long)buf);
Masakazu Mokuno09dde542008-02-07 19:58:57 +09001437 pr_debug("%s: ->\n", __func__);
1438 return is;
1439}
1440
1441/*
1442 * scanning helpers
1443 */
Masakazu Mokuno7fd871e2008-06-02 12:49:16 +09001444static int gelic_wl_start_scan(struct gelic_wl_info *wl, int always_scan,
1445 u8 *essid, size_t essid_len)
Masakazu Mokuno09dde542008-02-07 19:58:57 +09001446{
1447 struct gelic_eurus_cmd *cmd;
1448 int ret = 0;
Masakazu Mokuno7fd871e2008-06-02 12:49:16 +09001449 void *buf = NULL;
1450 size_t len;
Masakazu Mokuno09dde542008-02-07 19:58:57 +09001451
Hamish Guthrief2765862010-01-28 12:54:10 +01001452 pr_debug("%s: <- always=%d essid_len=%d\n", __func__,
1453 always_scan, essid_len);
Daniel Walker706ddd62008-05-22 00:00:01 -07001454 if (mutex_lock_interruptible(&wl->scan_lock))
Masakazu Mokuno09dde542008-02-07 19:58:57 +09001455 return -ERESTARTSYS;
1456
1457 /*
1458 * If already a scan in progress, do not trigger more
1459 */
1460 if (wl->scan_stat == GELIC_WL_SCAN_STAT_SCANNING) {
1461 pr_debug("%s: scanning now\n", __func__);
1462 goto out;
1463 }
1464
1465 init_completion(&wl->scan_done);
1466 /*
1467 * If we have already a bss list, don't try to get new
Hamish Guthrief2765862010-01-28 12:54:10 +01001468 * unless we are doing an ESSID scan
Masakazu Mokuno09dde542008-02-07 19:58:57 +09001469 */
Hamish Guthrief2765862010-01-28 12:54:10 +01001470 if ((!essid_len && !always_scan)
1471 && wl->scan_stat == GELIC_WL_SCAN_STAT_GOT_LIST) {
Masakazu Mokuno09dde542008-02-07 19:58:57 +09001472 pr_debug("%s: already has the list\n", __func__);
1473 complete(&wl->scan_done);
1474 goto out;
1475 }
Masakazu Mokuno7fd871e2008-06-02 12:49:16 +09001476
1477 /* ESSID scan ? */
1478 if (essid_len && essid) {
1479 buf = (void *)__get_free_page(GFP_KERNEL);
1480 if (!buf) {
1481 ret = -ENOMEM;
1482 goto out;
1483 }
1484 len = IW_ESSID_MAX_SIZE; /* hypervisor always requires 32 */
1485 memset(buf, 0, len);
1486 memcpy(buf, essid, essid_len);
1487 pr_debug("%s: essid scan='%s'\n", __func__, (char *)buf);
1488 } else
1489 len = 0;
1490
Masakazu Mokuno09dde542008-02-07 19:58:57 +09001491 /*
1492 * issue start scan request
1493 */
1494 wl->scan_stat = GELIC_WL_SCAN_STAT_SCANNING;
1495 cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_START_SCAN,
Masakazu Mokuno7fd871e2008-06-02 12:49:16 +09001496 buf, len);
Masakazu Mokuno09dde542008-02-07 19:58:57 +09001497 if (!cmd || cmd->status || cmd->cmd_status) {
1498 wl->scan_stat = GELIC_WL_SCAN_STAT_INIT;
1499 complete(&wl->scan_done);
1500 ret = -ENOMEM;
1501 goto out;
1502 }
1503 kfree(cmd);
1504out:
Masakazu Mokuno7fd871e2008-06-02 12:49:16 +09001505 free_page((unsigned long)buf);
Daniel Walker706ddd62008-05-22 00:00:01 -07001506 mutex_unlock(&wl->scan_lock);
Masakazu Mokuno09dde542008-02-07 19:58:57 +09001507 pr_debug("%s: ->\n", __func__);
1508 return ret;
1509}
1510
1511/*
1512 * retrieve scan result from the chip (hypervisor)
1513 * this function is invoked by schedule work.
1514 */
1515static void gelic_wl_scan_complete_event(struct gelic_wl_info *wl)
1516{
1517 struct gelic_eurus_cmd *cmd = NULL;
1518 struct gelic_wl_scan_info *target, *tmp;
1519 struct gelic_wl_scan_info *oldest = NULL;
1520 struct gelic_eurus_scan_info *scan_info;
1521 unsigned int scan_info_size;
1522 union iwreq_data data;
1523 unsigned long this_time = jiffies;
1524 unsigned int data_len, i, found, r;
Masakazu Mokuno13de15e2008-05-30 16:27:24 +09001525 void *buf;
Masakazu Mokuno09dde542008-02-07 19:58:57 +09001526
1527 pr_debug("%s:start\n", __func__);
Daniel Walker706ddd62008-05-22 00:00:01 -07001528 mutex_lock(&wl->scan_lock);
Masakazu Mokuno09dde542008-02-07 19:58:57 +09001529
Masakazu Mokuno13de15e2008-05-30 16:27:24 +09001530 buf = (void *)__get_free_page(GFP_KERNEL);
1531 if (!buf) {
1532 pr_info("%s: scan buffer alloc failed\n", __func__);
1533 goto out;
1534 }
1535
Masakazu Mokuno09dde542008-02-07 19:58:57 +09001536 if (wl->scan_stat != GELIC_WL_SCAN_STAT_SCANNING) {
1537 /*
1538 * stop() may be called while scanning, ignore result
1539 */
1540 pr_debug("%s: scan complete when stat != scanning(%d)\n",
1541 __func__, wl->scan_stat);
1542 goto out;
1543 }
1544
1545 cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_GET_SCAN,
Masakazu Mokuno13de15e2008-05-30 16:27:24 +09001546 buf, PAGE_SIZE);
Masakazu Mokuno09dde542008-02-07 19:58:57 +09001547 if (!cmd || cmd->status || cmd->cmd_status) {
1548 wl->scan_stat = GELIC_WL_SCAN_STAT_INIT;
1549 pr_info("%s:cmd failed\n", __func__);
1550 kfree(cmd);
1551 goto out;
1552 }
1553 data_len = cmd->size;
1554 pr_debug("%s: data_len = %d\n", __func__, data_len);
1555 kfree(cmd);
1556
1557 /* OK, bss list retrieved */
1558 wl->scan_stat = GELIC_WL_SCAN_STAT_GOT_LIST;
1559
1560 /* mark all entries are old */
1561 list_for_each_entry_safe(target, tmp, &wl->network_list, list) {
1562 target->valid = 0;
1563 /* expire too old entries */
1564 if (time_before(target->last_scanned + wl->scan_age,
1565 this_time)) {
1566 kfree(target->hwinfo);
1567 target->hwinfo = NULL;
1568 list_move_tail(&target->list, &wl->network_free_list);
1569 }
1570 }
1571
Hamish Guthrief2765862010-01-28 12:54:10 +01001572 /* put them in the network_list */
Masakazu Mokuno13de15e2008-05-30 16:27:24 +09001573 for (i = 0, scan_info_size = 0, scan_info = buf;
Masakazu Mokunoaad4c7d2008-03-11 13:15:44 +09001574 scan_info_size < data_len;
1575 i++, scan_info_size += be16_to_cpu(scan_info->size),
1576 scan_info = (void *)scan_info + be16_to_cpu(scan_info->size)) {
Johannes Berge1749612008-10-27 15:59:26 -07001577 pr_debug("%s:size=%d bssid=%pM scan_info=%p\n", __func__,
Masakazu Mokuno09dde542008-02-07 19:58:57 +09001578 be16_to_cpu(scan_info->size),
Johannes Berge1749612008-10-27 15:59:26 -07001579 &scan_info->bssid[2], scan_info);
Masakazu Mokunoaad4c7d2008-03-11 13:15:44 +09001580
1581 /*
1582 * The wireless firmware may return invalid channel 0 and/or
1583 * invalid rate if the AP emits zero length SSID ie. As this
1584 * scan information is useless, ignore it
1585 */
1586 if (!be16_to_cpu(scan_info->channel) || !scan_info->rate[0]) {
1587 pr_debug("%s: invalid scan info\n", __func__);
1588 continue;
1589 }
1590
Masakazu Mokuno09dde542008-02-07 19:58:57 +09001591 found = 0;
1592 oldest = NULL;
1593 list_for_each_entry(target, &wl->network_list, list) {
1594 if (!compare_ether_addr(&target->hwinfo->bssid[2],
1595 &scan_info->bssid[2])) {
1596 found = 1;
1597 pr_debug("%s: same BBS found scanned list\n",
1598 __func__);
1599 break;
1600 }
1601 if (!oldest ||
1602 (target->last_scanned < oldest->last_scanned))
1603 oldest = target;
1604 }
1605
1606 if (!found) {
1607 /* not found in the list */
1608 if (list_empty(&wl->network_free_list)) {
1609 /* expire oldest */
1610 target = oldest;
1611 } else {
1612 target = list_entry(wl->network_free_list.next,
1613 struct gelic_wl_scan_info,
1614 list);
1615 }
1616 }
1617
1618 /* update the item */
1619 target->last_scanned = this_time;
1620 target->valid = 1;
1621 target->eurus_index = i;
1622 kfree(target->hwinfo);
1623 target->hwinfo = kzalloc(be16_to_cpu(scan_info->size),
1624 GFP_KERNEL);
1625 if (!target->hwinfo) {
1626 pr_info("%s: kzalloc failed\n", __func__);
Masakazu Mokuno09dde542008-02-07 19:58:57 +09001627 continue;
1628 }
1629 /* copy hw scan info */
1630 memcpy(target->hwinfo, scan_info, scan_info->size);
1631 target->essid_len = strnlen(scan_info->essid,
1632 sizeof(scan_info->essid));
1633 target->rate_len = 0;
Johannes Berg2c7060022008-10-30 22:09:54 +01001634 for (r = 0; r < 12; r++)
Masakazu Mokuno09dde542008-02-07 19:58:57 +09001635 if (scan_info->rate[r])
1636 target->rate_len++;
1637 if (8 < target->rate_len)
1638 pr_info("%s: AP returns %d rates\n", __func__,
1639 target->rate_len);
1640 target->rate_ext_len = 0;
Johannes Berg2c7060022008-10-30 22:09:54 +01001641 for (r = 0; r < 16; r++)
Masakazu Mokuno09dde542008-02-07 19:58:57 +09001642 if (scan_info->ext_rate[r])
1643 target->rate_ext_len++;
1644 list_move_tail(&target->list, &wl->network_list);
Masakazu Mokuno09dde542008-02-07 19:58:57 +09001645 }
1646 memset(&data, 0, sizeof(data));
1647 wireless_send_event(port_to_netdev(wl_port(wl)), SIOCGIWSCAN, &data,
1648 NULL);
1649out:
Masakazu Mokuno13de15e2008-05-30 16:27:24 +09001650 free_page((unsigned long)buf);
Masakazu Mokuno09dde542008-02-07 19:58:57 +09001651 complete(&wl->scan_done);
Daniel Walker706ddd62008-05-22 00:00:01 -07001652 mutex_unlock(&wl->scan_lock);
Masakazu Mokuno09dde542008-02-07 19:58:57 +09001653 pr_debug("%s:end\n", __func__);
1654}
1655
1656/*
1657 * Select an appropriate bss from current scan list regarding
1658 * current settings from userspace.
1659 * The caller must hold wl->scan_lock,
1660 * and on the state of wl->scan_state == GELIC_WL_SCAN_GOT_LIST
1661 */
1662static void update_best(struct gelic_wl_scan_info **best,
1663 struct gelic_wl_scan_info *candid,
1664 int *best_weight,
1665 int *weight)
1666{
1667 if (*best_weight < ++(*weight)) {
1668 *best_weight = *weight;
1669 *best = candid;
1670 }
1671}
1672
1673static
1674struct gelic_wl_scan_info *gelic_wl_find_best_bss(struct gelic_wl_info *wl)
1675{
1676 struct gelic_wl_scan_info *scan_info;
1677 struct gelic_wl_scan_info *best_bss;
1678 int weight, best_weight;
1679 u16 security;
Masakazu Mokuno09dde542008-02-07 19:58:57 +09001680
1681 pr_debug("%s: <-\n", __func__);
1682
1683 best_bss = NULL;
1684 best_weight = 0;
1685
1686 list_for_each_entry(scan_info, &wl->network_list, list) {
1687 pr_debug("%s: station %p\n", __func__, scan_info);
1688
1689 if (!scan_info->valid) {
1690 pr_debug("%s: station invalid\n", __func__);
1691 continue;
1692 }
1693
1694 /* If bss specified, check it only */
1695 if (test_bit(GELIC_WL_STAT_BSSID_SET, &wl->stat)) {
1696 if (!compare_ether_addr(&scan_info->hwinfo->bssid[2],
1697 wl->bssid)) {
1698 best_bss = scan_info;
1699 pr_debug("%s: bssid matched\n", __func__);
1700 break;
1701 } else {
1702 pr_debug("%s: bssid unmached\n", __func__);
1703 continue;
1704 }
1705 }
1706
1707 weight = 0;
1708
1709 /* security */
1710 security = be16_to_cpu(scan_info->hwinfo->security) &
1711 GELIC_EURUS_SCAN_SEC_MASK;
1712 if (wl->wpa_level == GELIC_WL_WPA_LEVEL_WPA2) {
1713 if (security == GELIC_EURUS_SCAN_SEC_WPA2)
1714 update_best(&best_bss, scan_info,
1715 &best_weight, &weight);
1716 else
1717 continue;
1718 } else if (wl->wpa_level == GELIC_WL_WPA_LEVEL_WPA) {
1719 if (security == GELIC_EURUS_SCAN_SEC_WPA)
1720 update_best(&best_bss, scan_info,
1721 &best_weight, &weight);
1722 else
1723 continue;
1724 } else if (wl->wpa_level == GELIC_WL_WPA_LEVEL_NONE &&
1725 wl->group_cipher_method == GELIC_WL_CIPHER_WEP) {
1726 if (security == GELIC_EURUS_SCAN_SEC_WEP)
1727 update_best(&best_bss, scan_info,
1728 &best_weight, &weight);
1729 else
1730 continue;
1731 }
1732
1733 /* If ESSID is set, check it */
1734 if (test_bit(GELIC_WL_STAT_ESSID_SET, &wl->stat)) {
1735 if ((scan_info->essid_len == wl->essid_len) &&
1736 !strncmp(wl->essid,
1737 scan_info->hwinfo->essid,
1738 scan_info->essid_len))
1739 update_best(&best_bss, scan_info,
1740 &best_weight, &weight);
1741 else
1742 continue;
1743 }
1744 }
1745
1746#ifdef DEBUG
1747 pr_debug("%s: -> bss=%p\n", __func__, best_bss);
1748 if (best_bss) {
Johannes Berge1749612008-10-27 15:59:26 -07001749 pr_debug("%s:addr=%pM\n", __func__,
1750 &best_bss->hwinfo->bssid[2]);
Masakazu Mokuno09dde542008-02-07 19:58:57 +09001751 }
1752#endif
1753 return best_bss;
1754}
1755
1756/*
1757 * Setup WEP configuration to the chip
1758 * The caller must hold wl->scan_lock,
1759 * and on the state of wl->scan_state == GELIC_WL_SCAN_GOT_LIST
1760 */
1761static int gelic_wl_do_wep_setup(struct gelic_wl_info *wl)
1762{
1763 unsigned int i;
1764 struct gelic_eurus_wep_cfg *wep;
1765 struct gelic_eurus_cmd *cmd;
1766 int wep104 = 0;
1767 int have_key = 0;
1768 int ret = 0;
1769
1770 pr_debug("%s: <-\n", __func__);
1771 /* we can assume no one should uses the buffer */
Masakazu Mokuno13de15e2008-05-30 16:27:24 +09001772 wep = (struct gelic_eurus_wep_cfg *)__get_free_page(GFP_KERNEL);
1773 if (!wep)
1774 return -ENOMEM;
1775
Masakazu Mokuno09dde542008-02-07 19:58:57 +09001776 memset(wep, 0, sizeof(*wep));
1777
1778 if (wl->group_cipher_method == GELIC_WL_CIPHER_WEP) {
1779 pr_debug("%s: WEP mode\n", __func__);
1780 for (i = 0; i < GELIC_WEP_KEYS; i++) {
1781 if (!test_bit(i, &wl->key_enabled))
1782 continue;
1783
1784 pr_debug("%s: key#%d enabled\n", __func__, i);
1785 have_key = 1;
1786 if (wl->key_len[i] == 13)
1787 wep104 = 1;
1788 else if (wl->key_len[i] != 5) {
1789 pr_info("%s: wrong wep key[%d]=%d\n",
1790 __func__, i, wl->key_len[i]);
1791 ret = -EINVAL;
1792 goto out;
1793 }
1794 memcpy(wep->key[i], wl->key[i], wl->key_len[i]);
1795 }
1796
1797 if (!have_key) {
1798 pr_info("%s: all wep key disabled\n", __func__);
1799 ret = -EINVAL;
1800 goto out;
1801 }
1802
1803 if (wep104) {
1804 pr_debug("%s: 104bit key\n", __func__);
1805 wep->security = cpu_to_be16(GELIC_EURUS_WEP_SEC_104BIT);
1806 } else {
1807 pr_debug("%s: 40bit key\n", __func__);
1808 wep->security = cpu_to_be16(GELIC_EURUS_WEP_SEC_40BIT);
1809 }
1810 } else {
1811 pr_debug("%s: NO encryption\n", __func__);
1812 wep->security = cpu_to_be16(GELIC_EURUS_WEP_SEC_NONE);
1813 }
1814
1815 /* issue wep setup */
1816 cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_SET_WEP_CFG,
1817 wep, sizeof(*wep));
1818 if (!cmd)
1819 ret = -ENOMEM;
1820 else if (cmd->status || cmd->cmd_status)
1821 ret = -ENXIO;
1822
1823 kfree(cmd);
1824out:
Masakazu Mokuno13de15e2008-05-30 16:27:24 +09001825 free_page((unsigned long)wep);
Masakazu Mokuno09dde542008-02-07 19:58:57 +09001826 pr_debug("%s: ->\n", __func__);
1827 return ret;
1828}
1829
1830#ifdef DEBUG
1831static const char *wpasecstr(enum gelic_eurus_wpa_security sec)
1832{
1833 switch (sec) {
1834 case GELIC_EURUS_WPA_SEC_NONE:
1835 return "NONE";
1836 break;
1837 case GELIC_EURUS_WPA_SEC_WPA_TKIP_TKIP:
1838 return "WPA_TKIP_TKIP";
1839 break;
1840 case GELIC_EURUS_WPA_SEC_WPA_TKIP_AES:
1841 return "WPA_TKIP_AES";
1842 break;
1843 case GELIC_EURUS_WPA_SEC_WPA_AES_AES:
1844 return "WPA_AES_AES";
1845 break;
1846 case GELIC_EURUS_WPA_SEC_WPA2_TKIP_TKIP:
1847 return "WPA2_TKIP_TKIP";
1848 break;
1849 case GELIC_EURUS_WPA_SEC_WPA2_TKIP_AES:
1850 return "WPA2_TKIP_AES";
1851 break;
1852 case GELIC_EURUS_WPA_SEC_WPA2_AES_AES:
1853 return "WPA2_AES_AES";
1854 break;
1855 }
1856 return "";
1857};
1858#endif
1859
1860static int gelic_wl_do_wpa_setup(struct gelic_wl_info *wl)
1861{
1862 struct gelic_eurus_wpa_cfg *wpa;
1863 struct gelic_eurus_cmd *cmd;
1864 u16 security;
1865 int ret = 0;
1866
1867 pr_debug("%s: <-\n", __func__);
1868 /* we can assume no one should uses the buffer */
Masakazu Mokuno13de15e2008-05-30 16:27:24 +09001869 wpa = (struct gelic_eurus_wpa_cfg *)__get_free_page(GFP_KERNEL);
1870 if (!wpa)
1871 return -ENOMEM;
1872
Masakazu Mokuno09dde542008-02-07 19:58:57 +09001873 memset(wpa, 0, sizeof(*wpa));
1874
1875 if (!test_bit(GELIC_WL_STAT_WPA_PSK_SET, &wl->stat))
1876 pr_info("%s: PSK not configured yet\n", __func__);
1877
1878 /* copy key */
1879 memcpy(wpa->psk, wl->psk, wl->psk_len);
1880
1881 /* set security level */
1882 if (wl->wpa_level == GELIC_WL_WPA_LEVEL_WPA2) {
1883 if (wl->group_cipher_method == GELIC_WL_CIPHER_AES) {
1884 security = GELIC_EURUS_WPA_SEC_WPA2_AES_AES;
1885 } else {
1886 if (wl->pairwise_cipher_method == GELIC_WL_CIPHER_AES &&
1887 precise_ie())
1888 security = GELIC_EURUS_WPA_SEC_WPA2_TKIP_AES;
1889 else
1890 security = GELIC_EURUS_WPA_SEC_WPA2_TKIP_TKIP;
1891 }
1892 } else {
1893 if (wl->group_cipher_method == GELIC_WL_CIPHER_AES) {
1894 security = GELIC_EURUS_WPA_SEC_WPA_AES_AES;
1895 } else {
1896 if (wl->pairwise_cipher_method == GELIC_WL_CIPHER_AES &&
1897 precise_ie())
1898 security = GELIC_EURUS_WPA_SEC_WPA_TKIP_AES;
1899 else
1900 security = GELIC_EURUS_WPA_SEC_WPA_TKIP_TKIP;
1901 }
1902 }
1903 wpa->security = cpu_to_be16(security);
1904
1905 /* PSK type */
1906 wpa->psk_type = cpu_to_be16(wl->psk_type);
1907#ifdef DEBUG
Hamish Guthrief2765862010-01-28 12:54:10 +01001908 pr_debug("%s: sec=%s psktype=%s\n", __func__,
Masakazu Mokuno09dde542008-02-07 19:58:57 +09001909 wpasecstr(wpa->security),
1910 (wpa->psk_type == GELIC_EURUS_WPA_PSK_BIN) ?
1911 "BIN" : "passphrase");
1912#if 0
1913 /*
1914 * don't enable here if you plan to submit
1915 * the debug log because this dumps your precious
1916 * passphrase/key.
1917 */
Hamish Guthrief2765862010-01-28 12:54:10 +01001918 pr_debug("%s: psk=%s\n", __func__,
Masakazu Mokuno09dde542008-02-07 19:58:57 +09001919 (wpa->psk_type == GELIC_EURUS_WPA_PSK_BIN) ?
Hamish Guthrief2765862010-01-28 12:54:10 +01001920 "N/A" : wpa->psk);
Masakazu Mokuno09dde542008-02-07 19:58:57 +09001921#endif
1922#endif
1923 /* issue wpa setup */
1924 cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_SET_WPA_CFG,
1925 wpa, sizeof(*wpa));
1926 if (!cmd)
1927 ret = -ENOMEM;
1928 else if (cmd->status || cmd->cmd_status)
1929 ret = -ENXIO;
1930 kfree(cmd);
Masakazu Mokuno13de15e2008-05-30 16:27:24 +09001931 free_page((unsigned long)wpa);
Masakazu Mokuno09dde542008-02-07 19:58:57 +09001932 pr_debug("%s: --> %d\n", __func__, ret);
1933 return ret;
1934}
1935
1936/*
1937 * Start association. caller must hold assoc_stat_lock
1938 */
1939static int gelic_wl_associate_bss(struct gelic_wl_info *wl,
1940 struct gelic_wl_scan_info *bss)
1941{
1942 struct gelic_eurus_cmd *cmd;
1943 struct gelic_eurus_common_cfg *common;
1944 int ret = 0;
1945 unsigned long rc;
1946
1947 pr_debug("%s: <-\n", __func__);
1948
1949 /* do common config */
Masakazu Mokuno13de15e2008-05-30 16:27:24 +09001950 common = (struct gelic_eurus_common_cfg *)__get_free_page(GFP_KERNEL);
1951 if (!common)
1952 return -ENOMEM;
1953
Masakazu Mokuno09dde542008-02-07 19:58:57 +09001954 memset(common, 0, sizeof(*common));
1955 common->bss_type = cpu_to_be16(GELIC_EURUS_BSS_INFRA);
1956 common->op_mode = cpu_to_be16(GELIC_EURUS_OPMODE_11BG);
1957
1958 common->scan_index = cpu_to_be16(bss->eurus_index);
1959 switch (wl->auth_method) {
1960 case GELIC_EURUS_AUTH_OPEN:
1961 common->auth_method = cpu_to_be16(GELIC_EURUS_AUTH_OPEN);
1962 break;
1963 case GELIC_EURUS_AUTH_SHARED:
1964 common->auth_method = cpu_to_be16(GELIC_EURUS_AUTH_SHARED);
1965 break;
1966 }
1967
1968#ifdef DEBUG
1969 scan_list_dump(wl);
1970#endif
1971 pr_debug("%s: common cfg index=%d bsstype=%d auth=%d\n", __func__,
1972 be16_to_cpu(common->scan_index),
1973 be16_to_cpu(common->bss_type),
1974 be16_to_cpu(common->auth_method));
1975
1976 cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_SET_COMMON_CFG,
1977 common, sizeof(*common));
1978 if (!cmd || cmd->status || cmd->cmd_status) {
1979 ret = -ENOMEM;
1980 kfree(cmd);
1981 goto out;
1982 }
1983 kfree(cmd);
1984
1985 /* WEP/WPA */
1986 switch (wl->wpa_level) {
1987 case GELIC_WL_WPA_LEVEL_NONE:
1988 /* If WEP or no security, setup WEP config */
1989 ret = gelic_wl_do_wep_setup(wl);
1990 break;
1991 case GELIC_WL_WPA_LEVEL_WPA:
1992 case GELIC_WL_WPA_LEVEL_WPA2:
1993 ret = gelic_wl_do_wpa_setup(wl);
1994 break;
1995 };
1996
1997 if (ret) {
1998 pr_debug("%s: WEP/WPA setup failed %d\n", __func__,
1999 ret);
Masakazu Mokunoc6dbe172009-03-30 11:04:36 -07002000 ret = -EPERM;
2001 gelic_wl_send_iwap_event(wl, NULL);
2002 goto out;
Masakazu Mokuno09dde542008-02-07 19:58:57 +09002003 }
2004
2005 /* start association */
2006 init_completion(&wl->assoc_done);
2007 wl->assoc_stat = GELIC_WL_ASSOC_STAT_ASSOCIATING;
2008 cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_ASSOC,
2009 NULL, 0);
2010 if (!cmd || cmd->status || cmd->cmd_status) {
2011 pr_debug("%s: assoc request failed\n", __func__);
2012 wl->assoc_stat = GELIC_WL_ASSOC_STAT_DISCONN;
2013 kfree(cmd);
2014 ret = -ENOMEM;
2015 gelic_wl_send_iwap_event(wl, NULL);
2016 goto out;
2017 }
2018 kfree(cmd);
2019
2020 /* wait for connected event */
2021 rc = wait_for_completion_timeout(&wl->assoc_done, HZ * 4);/*FIXME*/
2022
2023 if (!rc) {
2024 /* timeouted. Maybe key or cyrpt mode is wrong */
2025 pr_info("%s: connect timeout \n", __func__);
2026 cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_DISASSOC,
2027 NULL, 0);
2028 kfree(cmd);
2029 wl->assoc_stat = GELIC_WL_ASSOC_STAT_DISCONN;
2030 gelic_wl_send_iwap_event(wl, NULL);
2031 ret = -ENXIO;
2032 } else {
2033 wl->assoc_stat = GELIC_WL_ASSOC_STAT_ASSOCIATED;
2034 /* copy bssid */
2035 memcpy(wl->active_bssid, &bss->hwinfo->bssid[2], ETH_ALEN);
2036
2037 /* send connect event */
2038 gelic_wl_send_iwap_event(wl, wl->active_bssid);
2039 pr_info("%s: connected\n", __func__);
2040 }
2041out:
Masakazu Mokuno13de15e2008-05-30 16:27:24 +09002042 free_page((unsigned long)common);
Masakazu Mokuno09dde542008-02-07 19:58:57 +09002043 pr_debug("%s: ->\n", __func__);
2044 return ret;
2045}
2046
2047/*
2048 * connected event
2049 */
2050static void gelic_wl_connected_event(struct gelic_wl_info *wl,
2051 u64 event)
2052{
2053 u64 desired_event = 0;
2054
2055 switch (wl->wpa_level) {
2056 case GELIC_WL_WPA_LEVEL_NONE:
2057 desired_event = GELIC_LV1_WL_EVENT_CONNECTED;
2058 break;
2059 case GELIC_WL_WPA_LEVEL_WPA:
2060 case GELIC_WL_WPA_LEVEL_WPA2:
2061 desired_event = GELIC_LV1_WL_EVENT_WPA_CONNECTED;
2062 break;
2063 }
2064
2065 if (desired_event == event) {
2066 pr_debug("%s: completed \n", __func__);
2067 complete(&wl->assoc_done);
2068 netif_carrier_on(port_to_netdev(wl_port(wl)));
2069 } else
Stephen Rothwell309ea622009-01-13 20:09:30 +00002070 pr_debug("%s: event %#llx under wpa\n",
Masakazu Mokuno09dde542008-02-07 19:58:57 +09002071 __func__, event);
2072}
2073
2074/*
2075 * disconnect event
2076 */
2077static void gelic_wl_disconnect_event(struct gelic_wl_info *wl,
2078 u64 event)
2079{
2080 struct gelic_eurus_cmd *cmd;
2081 int lock;
2082
2083 /*
2084 * If we fall here in the middle of association,
2085 * associate_bss() should be waiting for complation of
2086 * wl->assoc_done.
2087 * As it waits with timeout, just leave assoc_done
2088 * uncompleted, then it terminates with timeout
2089 */
Daniel Walkerbb2d67a2008-05-22 00:00:02 -07002090 if (!mutex_trylock(&wl->assoc_stat_lock)) {
Masakazu Mokuno09dde542008-02-07 19:58:57 +09002091 pr_debug("%s: already locked\n", __func__);
2092 lock = 0;
2093 } else {
2094 pr_debug("%s: obtain lock\n", __func__);
2095 lock = 1;
2096 }
2097
2098 cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_DISASSOC, NULL, 0);
2099 kfree(cmd);
2100
2101 /* send disconnected event to the supplicant */
2102 if (wl->assoc_stat == GELIC_WL_ASSOC_STAT_ASSOCIATED)
2103 gelic_wl_send_iwap_event(wl, NULL);
2104
2105 wl->assoc_stat = GELIC_WL_ASSOC_STAT_DISCONN;
2106 netif_carrier_off(port_to_netdev(wl_port(wl)));
2107
2108 if (lock)
Daniel Walkerbb2d67a2008-05-22 00:00:02 -07002109 mutex_unlock(&wl->assoc_stat_lock);
Masakazu Mokuno09dde542008-02-07 19:58:57 +09002110}
2111/*
2112 * event worker
2113 */
2114#ifdef DEBUG
2115static const char *eventstr(enum gelic_lv1_wl_event event)
2116{
2117 static char buf[32];
2118 char *ret;
2119 if (event & GELIC_LV1_WL_EVENT_DEVICE_READY)
2120 ret = "EURUS_READY";
2121 else if (event & GELIC_LV1_WL_EVENT_SCAN_COMPLETED)
2122 ret = "SCAN_COMPLETED";
2123 else if (event & GELIC_LV1_WL_EVENT_DEAUTH)
2124 ret = "DEAUTH";
2125 else if (event & GELIC_LV1_WL_EVENT_BEACON_LOST)
2126 ret = "BEACON_LOST";
2127 else if (event & GELIC_LV1_WL_EVENT_CONNECTED)
2128 ret = "CONNECTED";
2129 else if (event & GELIC_LV1_WL_EVENT_WPA_CONNECTED)
2130 ret = "WPA_CONNECTED";
2131 else if (event & GELIC_LV1_WL_EVENT_WPA_ERROR)
2132 ret = "WPA_ERROR";
2133 else {
2134 sprintf(buf, "Unknown(%#x)", event);
2135 ret = buf;
2136 }
2137 return ret;
2138}
2139#else
2140static const char *eventstr(enum gelic_lv1_wl_event event)
2141{
2142 return NULL;
2143}
2144#endif
2145static void gelic_wl_event_worker(struct work_struct *work)
2146{
2147 struct gelic_wl_info *wl;
2148 struct gelic_port *port;
2149 u64 event, tmp;
2150 int status;
2151
2152 pr_debug("%s:start\n", __func__);
2153 wl = container_of(work, struct gelic_wl_info, event_work.work);
2154 port = wl_port(wl);
2155 while (1) {
2156 status = lv1_net_control(bus_id(port->card), dev_id(port->card),
2157 GELIC_LV1_GET_WLAN_EVENT, 0, 0, 0,
2158 &event, &tmp);
2159 if (status) {
2160 if (status != LV1_NO_ENTRY)
2161 pr_debug("%s:wlan event failed %d\n",
2162 __func__, status);
2163 /* got all events */
2164 pr_debug("%s:end\n", __func__);
2165 return;
2166 }
2167 pr_debug("%s: event=%s\n", __func__, eventstr(event));
2168 switch (event) {
2169 case GELIC_LV1_WL_EVENT_SCAN_COMPLETED:
2170 gelic_wl_scan_complete_event(wl);
2171 break;
2172 case GELIC_LV1_WL_EVENT_BEACON_LOST:
2173 case GELIC_LV1_WL_EVENT_DEAUTH:
2174 gelic_wl_disconnect_event(wl, event);
2175 break;
2176 case GELIC_LV1_WL_EVENT_CONNECTED:
2177 case GELIC_LV1_WL_EVENT_WPA_CONNECTED:
2178 gelic_wl_connected_event(wl, event);
2179 break;
2180 default:
2181 break;
2182 }
2183 } /* while */
2184}
2185/*
2186 * association worker
2187 */
2188static void gelic_wl_assoc_worker(struct work_struct *work)
2189{
2190 struct gelic_wl_info *wl;
2191
2192 struct gelic_wl_scan_info *best_bss;
2193 int ret;
Masakazu Mokuno7fd871e2008-06-02 12:49:16 +09002194 unsigned long irqflag;
2195 u8 *essid;
2196 size_t essid_len;
Masakazu Mokuno09dde542008-02-07 19:58:57 +09002197
2198 wl = container_of(work, struct gelic_wl_info, assoc_work.work);
2199
Daniel Walkerbb2d67a2008-05-22 00:00:02 -07002200 mutex_lock(&wl->assoc_stat_lock);
Masakazu Mokuno09dde542008-02-07 19:58:57 +09002201
2202 if (wl->assoc_stat != GELIC_WL_ASSOC_STAT_DISCONN)
2203 goto out;
2204
Masakazu Mokuno7fd871e2008-06-02 12:49:16 +09002205 spin_lock_irqsave(&wl->lock, irqflag);
2206 if (test_bit(GELIC_WL_STAT_ESSID_SET, &wl->stat)) {
2207 pr_debug("%s: assoc ESSID configured %s\n", __func__,
2208 wl->essid);
2209 essid = wl->essid;
2210 essid_len = wl->essid_len;
2211 } else {
2212 essid = NULL;
2213 essid_len = 0;
2214 }
2215 spin_unlock_irqrestore(&wl->lock, irqflag);
2216
2217 ret = gelic_wl_start_scan(wl, 0, essid, essid_len);
Masakazu Mokuno09dde542008-02-07 19:58:57 +09002218 if (ret == -ERESTARTSYS) {
2219 pr_debug("%s: scan start failed association\n", __func__);
2220 schedule_delayed_work(&wl->assoc_work, HZ/10); /*FIXME*/
2221 goto out;
2222 } else if (ret) {
2223 pr_info("%s: scan prerequisite failed\n", __func__);
2224 goto out;
2225 }
2226
2227 /*
2228 * Wait for bss scan completion
2229 * If we have scan list already, gelic_wl_start_scan()
2230 * returns OK and raises the complete. Thus,
2231 * it's ok to wait unconditionally here
2232 */
2233 wait_for_completion(&wl->scan_done);
2234
2235 pr_debug("%s: scan done\n", __func__);
Daniel Walker706ddd62008-05-22 00:00:01 -07002236 mutex_lock(&wl->scan_lock);
Masakazu Mokuno09dde542008-02-07 19:58:57 +09002237 if (wl->scan_stat != GELIC_WL_SCAN_STAT_GOT_LIST) {
2238 gelic_wl_send_iwap_event(wl, NULL);
2239 pr_info("%s: no scan list. association failed\n", __func__);
2240 goto scan_lock_out;
2241 }
2242
2243 /* find best matching bss */
2244 best_bss = gelic_wl_find_best_bss(wl);
2245 if (!best_bss) {
2246 gelic_wl_send_iwap_event(wl, NULL);
2247 pr_info("%s: no bss matched. association failed\n", __func__);
2248 goto scan_lock_out;
2249 }
2250
2251 /* ok, do association */
2252 ret = gelic_wl_associate_bss(wl, best_bss);
2253 if (ret)
2254 pr_info("%s: association failed %d\n", __func__, ret);
2255scan_lock_out:
Daniel Walker706ddd62008-05-22 00:00:01 -07002256 mutex_unlock(&wl->scan_lock);
Masakazu Mokuno09dde542008-02-07 19:58:57 +09002257out:
Daniel Walkerbb2d67a2008-05-22 00:00:02 -07002258 mutex_unlock(&wl->assoc_stat_lock);
Masakazu Mokuno09dde542008-02-07 19:58:57 +09002259}
2260/*
2261 * Interrupt handler
2262 * Called from the ethernet interrupt handler
2263 * Processes wireless specific virtual interrupts only
2264 */
2265void gelic_wl_interrupt(struct net_device *netdev, u64 status)
2266{
2267 struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
2268
2269 if (status & GELIC_CARD_WLAN_COMMAND_COMPLETED) {
2270 pr_debug("%s:cmd complete\n", __func__);
2271 complete(&wl->cmd_done_intr);
2272 }
2273
2274 if (status & GELIC_CARD_WLAN_EVENT_RECEIVED) {
2275 pr_debug("%s:event received\n", __func__);
2276 queue_delayed_work(wl->event_queue, &wl->event_work, 0);
2277 }
2278}
2279
2280/*
2281 * driver helpers
2282 */
2283#define IW_IOCTL(n) [(n) - SIOCSIWCOMMIT]
2284static const iw_handler gelic_wl_wext_handler[] =
2285{
2286 IW_IOCTL(SIOCGIWNAME) = gelic_wl_get_name,
2287 IW_IOCTL(SIOCGIWRANGE) = gelic_wl_get_range,
2288 IW_IOCTL(SIOCSIWSCAN) = gelic_wl_set_scan,
2289 IW_IOCTL(SIOCGIWSCAN) = gelic_wl_get_scan,
2290 IW_IOCTL(SIOCSIWAUTH) = gelic_wl_set_auth,
2291 IW_IOCTL(SIOCGIWAUTH) = gelic_wl_get_auth,
2292 IW_IOCTL(SIOCSIWESSID) = gelic_wl_set_essid,
2293 IW_IOCTL(SIOCGIWESSID) = gelic_wl_get_essid,
2294 IW_IOCTL(SIOCSIWENCODE) = gelic_wl_set_encode,
2295 IW_IOCTL(SIOCGIWENCODE) = gelic_wl_get_encode,
2296 IW_IOCTL(SIOCSIWAP) = gelic_wl_set_ap,
2297 IW_IOCTL(SIOCGIWAP) = gelic_wl_get_ap,
2298 IW_IOCTL(SIOCSIWENCODEEXT) = gelic_wl_set_encodeext,
2299 IW_IOCTL(SIOCGIWENCODEEXT) = gelic_wl_get_encodeext,
2300 IW_IOCTL(SIOCSIWMODE) = gelic_wl_set_mode,
2301 IW_IOCTL(SIOCGIWMODE) = gelic_wl_get_mode,
2302 IW_IOCTL(SIOCGIWNICKN) = gelic_wl_get_nick,
2303};
2304
Masakazu Mokuno09dde542008-02-07 19:58:57 +09002305static const struct iw_handler_def gelic_wl_wext_handler_def = {
2306 .num_standard = ARRAY_SIZE(gelic_wl_wext_handler),
2307 .standard = gelic_wl_wext_handler,
2308 .get_wireless_stats = gelic_wl_get_wireless_stats,
Masakazu Mokuno09dde542008-02-07 19:58:57 +09002309};
2310
Geert Uytterhoeven48dce822009-06-10 04:38:58 +00002311static struct net_device * __devinit gelic_wl_alloc(struct gelic_card *card)
Masakazu Mokuno09dde542008-02-07 19:58:57 +09002312{
2313 struct net_device *netdev;
2314 struct gelic_port *port;
2315 struct gelic_wl_info *wl;
2316 unsigned int i;
2317
2318 pr_debug("%s:start\n", __func__);
2319 netdev = alloc_etherdev(sizeof(struct gelic_port) +
2320 sizeof(struct gelic_wl_info));
2321 pr_debug("%s: netdev =%p card=%p \np", __func__, netdev, card);
2322 if (!netdev)
2323 return NULL;
2324
Masakazu Mokunoc1e889b2008-03-12 16:41:11 +09002325 strcpy(netdev->name, "wlan%d");
2326
Masakazu Mokuno09dde542008-02-07 19:58:57 +09002327 port = netdev_priv(netdev);
2328 port->netdev = netdev;
2329 port->card = card;
2330 port->type = GELIC_PORT_WIRELESS;
2331
2332 wl = port_wl(port);
2333 pr_debug("%s: wl=%p port=%p\n", __func__, wl, port);
2334
2335 /* allocate scan list */
2336 wl->networks = kzalloc(sizeof(struct gelic_wl_scan_info) *
2337 GELIC_WL_BSS_MAX_ENT, GFP_KERNEL);
2338
2339 if (!wl->networks)
2340 goto fail_bss;
2341
2342 wl->eurus_cmd_queue = create_singlethread_workqueue("gelic_cmd");
2343 if (!wl->eurus_cmd_queue)
2344 goto fail_cmd_workqueue;
2345
2346 wl->event_queue = create_singlethread_workqueue("gelic_event");
2347 if (!wl->event_queue)
2348 goto fail_event_workqueue;
2349
2350 INIT_LIST_HEAD(&wl->network_free_list);
2351 INIT_LIST_HEAD(&wl->network_list);
2352 for (i = 0; i < GELIC_WL_BSS_MAX_ENT; i++)
2353 list_add_tail(&wl->networks[i].list,
2354 &wl->network_free_list);
2355 init_completion(&wl->cmd_done_intr);
2356
2357 INIT_DELAYED_WORK(&wl->event_work, gelic_wl_event_worker);
2358 INIT_DELAYED_WORK(&wl->assoc_work, gelic_wl_assoc_worker);
Daniel Walker706ddd62008-05-22 00:00:01 -07002359 mutex_init(&wl->scan_lock);
Daniel Walkerbb2d67a2008-05-22 00:00:02 -07002360 mutex_init(&wl->assoc_stat_lock);
Masakazu Mokuno09dde542008-02-07 19:58:57 +09002361
2362 init_completion(&wl->scan_done);
2363 /* for the case that no scan request is issued and stop() is called */
2364 complete(&wl->scan_done);
2365
2366 spin_lock_init(&wl->lock);
2367
2368 wl->scan_age = 5*HZ; /* FIXME */
2369
2370 /* buffer for receiving scanned list etc */
2371 BUILD_BUG_ON(PAGE_SIZE <
2372 sizeof(struct gelic_eurus_scan_info) *
2373 GELIC_EURUS_MAX_SCAN);
Masakazu Mokuno09dde542008-02-07 19:58:57 +09002374 pr_debug("%s:end\n", __func__);
2375 return netdev;
2376
Masakazu Mokuno09dde542008-02-07 19:58:57 +09002377fail_event_workqueue:
2378 destroy_workqueue(wl->eurus_cmd_queue);
2379fail_cmd_workqueue:
2380 kfree(wl->networks);
2381fail_bss:
2382 free_netdev(netdev);
2383 pr_debug("%s:end error\n", __func__);
2384 return NULL;
2385
2386}
2387
2388static void gelic_wl_free(struct gelic_wl_info *wl)
2389{
2390 struct gelic_wl_scan_info *scan_info;
2391 unsigned int i;
2392
2393 pr_debug("%s: <-\n", __func__);
2394
2395 pr_debug("%s: destroy queues\n", __func__);
2396 destroy_workqueue(wl->eurus_cmd_queue);
2397 destroy_workqueue(wl->event_queue);
2398
2399 scan_info = wl->networks;
2400 for (i = 0; i < GELIC_WL_BSS_MAX_ENT; i++, scan_info++)
2401 kfree(scan_info->hwinfo);
2402 kfree(wl->networks);
2403
2404 free_netdev(port_to_netdev(wl_port(wl)));
2405
2406 pr_debug("%s: ->\n", __func__);
2407}
2408
2409static int gelic_wl_try_associate(struct net_device *netdev)
2410{
2411 struct gelic_wl_info *wl = port_wl(netdev_priv(netdev));
2412 int ret = -1;
2413 unsigned int i;
2414
2415 pr_debug("%s: <-\n", __func__);
2416
2417 /* check constraits for start association */
2418 /* for no access restriction AP */
2419 if (wl->group_cipher_method == GELIC_WL_CIPHER_NONE) {
2420 if (test_bit(GELIC_WL_STAT_CONFIGURED,
2421 &wl->stat))
2422 goto do_associate;
2423 else {
2424 pr_debug("%s: no wep, not configured\n", __func__);
2425 return ret;
2426 }
2427 }
2428
2429 /* for WEP, one of four keys should be set */
2430 if (wl->group_cipher_method == GELIC_WL_CIPHER_WEP) {
2431 /* one of keys set */
2432 for (i = 0; i < GELIC_WEP_KEYS; i++) {
2433 if (test_bit(i, &wl->key_enabled))
2434 goto do_associate;
2435 }
2436 pr_debug("%s: WEP, but no key specified\n", __func__);
2437 return ret;
2438 }
2439
2440 /* for WPA[2], psk should be set */
2441 if ((wl->group_cipher_method == GELIC_WL_CIPHER_TKIP) ||
2442 (wl->group_cipher_method == GELIC_WL_CIPHER_AES)) {
2443 if (test_bit(GELIC_WL_STAT_WPA_PSK_SET,
2444 &wl->stat))
2445 goto do_associate;
2446 else {
2447 pr_debug("%s: AES/TKIP, but PSK not configured\n",
2448 __func__);
2449 return ret;
2450 }
2451 }
2452
2453do_associate:
2454 ret = schedule_delayed_work(&wl->assoc_work, 0);
2455 pr_debug("%s: start association work %d\n", __func__, ret);
2456 return ret;
2457}
2458
2459/*
2460 * netdev handlers
2461 */
2462static int gelic_wl_open(struct net_device *netdev)
2463{
2464 struct gelic_card *card = netdev_card(netdev);
2465
2466 pr_debug("%s:->%p\n", __func__, netdev);
2467
2468 gelic_card_up(card);
2469
2470 /* try to associate */
2471 gelic_wl_try_associate(netdev);
2472
2473 netif_start_queue(netdev);
2474
2475 pr_debug("%s:<-\n", __func__);
2476 return 0;
2477}
2478
2479/*
2480 * reset state machine
2481 */
2482static int gelic_wl_reset_state(struct gelic_wl_info *wl)
2483{
2484 struct gelic_wl_scan_info *target;
2485 struct gelic_wl_scan_info *tmp;
2486
2487 /* empty scan list */
2488 list_for_each_entry_safe(target, tmp, &wl->network_list, list) {
2489 list_move_tail(&target->list, &wl->network_free_list);
2490 }
2491 wl->scan_stat = GELIC_WL_SCAN_STAT_INIT;
2492
2493 /* clear configuration */
2494 wl->auth_method = GELIC_EURUS_AUTH_OPEN;
2495 wl->group_cipher_method = GELIC_WL_CIPHER_NONE;
2496 wl->pairwise_cipher_method = GELIC_WL_CIPHER_NONE;
2497 wl->wpa_level = GELIC_WL_WPA_LEVEL_NONE;
2498
2499 wl->key_enabled = 0;
2500 wl->current_key = 0;
2501
2502 wl->psk_type = GELIC_EURUS_WPA_PSK_PASSPHRASE;
2503 wl->psk_len = 0;
2504
2505 wl->essid_len = 0;
2506 memset(wl->essid, 0, sizeof(wl->essid));
2507 memset(wl->bssid, 0, sizeof(wl->bssid));
2508 memset(wl->active_bssid, 0, sizeof(wl->active_bssid));
2509
2510 wl->assoc_stat = GELIC_WL_ASSOC_STAT_DISCONN;
2511
2512 memset(&wl->iwstat, 0, sizeof(wl->iwstat));
2513 /* all status bit clear */
2514 wl->stat = 0;
2515 return 0;
2516}
2517
2518/*
2519 * Tell eurus to terminate association
2520 */
2521static void gelic_wl_disconnect(struct net_device *netdev)
2522{
2523 struct gelic_port *port = netdev_priv(netdev);
2524 struct gelic_wl_info *wl = port_wl(port);
2525 struct gelic_eurus_cmd *cmd;
2526
2527 /*
2528 * If scann process is running on chip,
2529 * further requests will be rejected
2530 */
2531 if (wl->scan_stat == GELIC_WL_SCAN_STAT_SCANNING)
2532 wait_for_completion_timeout(&wl->scan_done, HZ);
2533
2534 cmd = gelic_eurus_sync_cmd(wl, GELIC_EURUS_CMD_DISASSOC, NULL, 0);
2535 kfree(cmd);
2536 gelic_wl_send_iwap_event(wl, NULL);
2537};
2538
2539static int gelic_wl_stop(struct net_device *netdev)
2540{
2541 struct gelic_port *port = netdev_priv(netdev);
2542 struct gelic_wl_info *wl = port_wl(port);
2543 struct gelic_card *card = netdev_card(netdev);
2544
2545 pr_debug("%s:<-\n", __func__);
2546
2547 /*
2548 * Cancel pending association work.
2549 * event work can run after netdev down
2550 */
2551 cancel_delayed_work(&wl->assoc_work);
2552
2553 if (wl->assoc_stat == GELIC_WL_ASSOC_STAT_ASSOCIATED)
2554 gelic_wl_disconnect(netdev);
2555
2556 /* reset our state machine */
2557 gelic_wl_reset_state(wl);
2558
2559 netif_stop_queue(netdev);
2560
2561 gelic_card_down(card);
2562
2563 pr_debug("%s:->\n", __func__);
2564 return 0;
2565}
2566
2567/* -- */
2568
Masakazu Mokuno31e2b7b2009-01-15 22:52:55 +00002569static const struct net_device_ops gelic_wl_netdevice_ops = {
2570 .ndo_open = gelic_wl_open,
2571 .ndo_stop = gelic_wl_stop,
2572 .ndo_start_xmit = gelic_net_xmit,
2573 .ndo_set_multicast_list = gelic_net_set_multi,
2574 .ndo_change_mtu = gelic_net_change_mtu,
2575 .ndo_tx_timeout = gelic_net_tx_timeout,
Ben Hutchings240c1022009-07-09 17:54:35 +00002576 .ndo_set_mac_address = eth_mac_addr,
Masakazu Mokuno31e2b7b2009-01-15 22:52:55 +00002577 .ndo_validate_addr = eth_validate_addr,
2578#ifdef CONFIG_NET_POLL_CONTROLLER
2579 .ndo_poll_controller = gelic_net_poll_controller,
2580#endif
2581};
2582
Stephen Hemminger0fc0b732009-09-02 01:03:33 -07002583static const struct ethtool_ops gelic_wl_ethtool_ops = {
Masakazu Mokuno09dde542008-02-07 19:58:57 +09002584 .get_drvinfo = gelic_net_get_drvinfo,
2585 .get_link = gelic_wl_get_link,
2586 .get_tx_csum = ethtool_op_get_tx_csum,
2587 .set_tx_csum = ethtool_op_set_tx_csum,
2588 .get_rx_csum = gelic_net_get_rx_csum,
2589 .set_rx_csum = gelic_net_set_rx_csum,
2590};
2591
Geert Uytterhoeven48dce822009-06-10 04:38:58 +00002592static void __devinit gelic_wl_setup_netdev_ops(struct net_device *netdev)
Masakazu Mokuno09dde542008-02-07 19:58:57 +09002593{
2594 struct gelic_wl_info *wl;
2595 wl = port_wl(netdev_priv(netdev));
2596 BUG_ON(!wl);
Masakazu Mokuno09dde542008-02-07 19:58:57 +09002597 netdev->watchdog_timeo = GELIC_NET_WATCHDOG_TIMEOUT;
2598
2599 netdev->ethtool_ops = &gelic_wl_ethtool_ops;
Masakazu Mokuno31e2b7b2009-01-15 22:52:55 +00002600 netdev->netdev_ops = &gelic_wl_netdevice_ops;
2601 netdev->wireless_data = &wl->wireless_data;
2602 netdev->wireless_handlers = &gelic_wl_wext_handler_def;
Masakazu Mokuno09dde542008-02-07 19:58:57 +09002603}
2604
2605/*
2606 * driver probe/remove
2607 */
Geert Uytterhoeven48dce822009-06-10 04:38:58 +00002608int __devinit gelic_wl_driver_probe(struct gelic_card *card)
Masakazu Mokuno09dde542008-02-07 19:58:57 +09002609{
2610 int ret;
2611 struct net_device *netdev;
2612
2613 pr_debug("%s:start\n", __func__);
2614
2615 if (ps3_compare_firmware_version(1, 6, 0) < 0)
2616 return 0;
2617 if (!card->vlan[GELIC_PORT_WIRELESS].tx)
2618 return 0;
2619
2620 /* alloc netdevice for wireless */
2621 netdev = gelic_wl_alloc(card);
2622 if (!netdev)
2623 return -ENOMEM;
2624
2625 /* setup net_device structure */
Masakazu Mokuno4b748502008-02-22 16:45:26 +09002626 SET_NETDEV_DEV(netdev, &card->dev->core);
Masakazu Mokuno09dde542008-02-07 19:58:57 +09002627 gelic_wl_setup_netdev_ops(netdev);
2628
2629 /* setup some of net_device and register it */
2630 ret = gelic_net_setup_netdev(netdev, card);
2631 if (ret)
2632 goto fail_setup;
2633 card->netdev[GELIC_PORT_WIRELESS] = netdev;
2634
2635 /* add enable wireless interrupt */
2636 card->irq_mask |= GELIC_CARD_WLAN_EVENT_RECEIVED |
2637 GELIC_CARD_WLAN_COMMAND_COMPLETED;
2638 /* to allow wireless commands while both interfaces are down */
2639 gelic_card_set_irq_mask(card, GELIC_CARD_WLAN_EVENT_RECEIVED |
2640 GELIC_CARD_WLAN_COMMAND_COMPLETED);
2641 pr_debug("%s:end\n", __func__);
2642 return 0;
2643
2644fail_setup:
2645 gelic_wl_free(port_wl(netdev_port(netdev)));
2646
2647 return ret;
2648}
2649
2650int gelic_wl_driver_remove(struct gelic_card *card)
2651{
2652 struct gelic_wl_info *wl;
2653 struct net_device *netdev;
2654
2655 pr_debug("%s:start\n", __func__);
2656
2657 if (ps3_compare_firmware_version(1, 6, 0) < 0)
2658 return 0;
2659 if (!card->vlan[GELIC_PORT_WIRELESS].tx)
2660 return 0;
2661
2662 netdev = card->netdev[GELIC_PORT_WIRELESS];
2663 wl = port_wl(netdev_priv(netdev));
2664
2665 /* if the interface was not up, but associated */
2666 if (wl->assoc_stat == GELIC_WL_ASSOC_STAT_ASSOCIATED)
2667 gelic_wl_disconnect(netdev);
2668
2669 complete(&wl->cmd_done_intr);
2670
2671 /* cancel all work queue */
2672 cancel_delayed_work(&wl->assoc_work);
2673 cancel_delayed_work(&wl->event_work);
2674 flush_workqueue(wl->eurus_cmd_queue);
2675 flush_workqueue(wl->event_queue);
2676
2677 unregister_netdev(netdev);
2678
2679 /* disable wireless interrupt */
2680 pr_debug("%s: disable intr\n", __func__);
2681 card->irq_mask &= ~(GELIC_CARD_WLAN_EVENT_RECEIVED |
2682 GELIC_CARD_WLAN_COMMAND_COMPLETED);
2683 /* free bss list, netdev*/
2684 gelic_wl_free(wl);
2685 pr_debug("%s:end\n", __func__);
2686 return 0;
2687}