]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/net/wireless/wl12xx/wl1271_main.c
wl1271: Multicast filtering configuration
[net-next-2.6.git] / drivers / net / wireless / wl12xx / wl1271_main.c
CommitLineData
f5fc0f86
LC
1/*
2 * This file is part of wl1271
3 *
4 * Copyright (C) 2008-2009 Nokia Corporation
5 *
6 * Contact: Luciano Coelho <luciano.coelho@nokia.com>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA
21 *
22 */
23
24#include <linux/module.h>
25#include <linux/platform_device.h>
26#include <linux/interrupt.h>
27#include <linux/firmware.h>
28#include <linux/delay.h>
29#include <linux/irq.h>
30#include <linux/spi/spi.h>
31#include <linux/crc32.h>
32#include <linux/etherdevice.h>
33#include <linux/spi/wl12xx.h>
34
35#include "wl1271.h"
36#include "wl12xx_80211.h"
37#include "wl1271_reg.h"
38#include "wl1271_spi.h"
39#include "wl1271_event.h"
40#include "wl1271_tx.h"
41#include "wl1271_rx.h"
42#include "wl1271_ps.h"
43#include "wl1271_init.h"
44#include "wl1271_debugfs.h"
45#include "wl1271_cmd.h"
46#include "wl1271_boot.h"
47
48static int wl1271_plt_init(struct wl1271 *wl)
49{
50 int ret;
51
52 ret = wl1271_acx_init_mem_config(wl);
53 if (ret < 0)
54 return ret;
55
56 ret = wl1271_cmd_data_path(wl, wl->channel, 1);
57 if (ret < 0)
58 return ret;
59
60 return 0;
61}
62
63static void wl1271_disable_interrupts(struct wl1271 *wl)
64{
65 disable_irq(wl->irq);
66}
67
68static void wl1271_power_off(struct wl1271 *wl)
69{
70 wl->set_power(false);
71}
72
73static void wl1271_power_on(struct wl1271 *wl)
74{
75 wl->set_power(true);
76}
77
78static void wl1271_fw_status(struct wl1271 *wl, struct wl1271_fw_status *status)
79{
80 u32 total = 0;
81 int i;
82
83 /*
84 * FIXME: Reading the FW status directly from the registers seems to
85 * be the right thing to do, but it doesn't work. And in the
86 * reference driver, there is a workaround called
87 * USE_SDIO_24M_WORKAROUND, which reads the status from memory
88 * instead, so we do the same here.
89 */
90
91 wl1271_spi_mem_read(wl, STATUS_MEM_ADDRESS, status, sizeof(*status));
92
93 wl1271_debug(DEBUG_IRQ, "intr: 0x%x (fw_rx_counter = %d, "
94 "drv_rx_counter = %d, tx_results_counter = %d)",
95 status->intr,
96 status->fw_rx_counter,
97 status->drv_rx_counter,
98 status->tx_results_counter);
99
100 /* update number of available TX blocks */
101 for (i = 0; i < NUM_TX_QUEUES; i++) {
102 u32 cnt = status->tx_released_blks[i] - wl->tx_blocks_freed[i];
103 wl->tx_blocks_freed[i] = status->tx_released_blks[i];
104 wl->tx_blocks_available += cnt;
105 total += cnt;
106 }
107
108 /* if more blocks are available now, schedule some tx work */
109 if (total && !skb_queue_empty(&wl->tx_queue))
a64b07e8 110 ieee80211_queue_work(wl->hw, &wl->tx_work);
f5fc0f86
LC
111
112 /* update the host-chipset time offset */
113 wl->time_offset = jiffies_to_usecs(jiffies) - status->fw_localtime;
114}
115
116#define WL1271_IRQ_MAX_LOOPS 10
117static void wl1271_irq_work(struct work_struct *work)
118{
119 u32 intr, ctr = WL1271_IRQ_MAX_LOOPS;
120 int ret;
121 struct wl1271 *wl =
122 container_of(work, struct wl1271, irq_work);
123
124 mutex_lock(&wl->mutex);
125
126 wl1271_debug(DEBUG_IRQ, "IRQ work");
127
128 if (wl->state == WL1271_STATE_OFF)
129 goto out;
130
131 ret = wl1271_ps_elp_wakeup(wl, true);
132 if (ret < 0)
133 goto out;
134
135 wl1271_reg_write32(wl, ACX_REG_INTERRUPT_MASK, WL1271_ACX_INTR_ALL);
136
137 intr = wl1271_reg_read32(wl, ACX_REG_INTERRUPT_CLEAR);
138 if (!intr) {
139 wl1271_debug(DEBUG_IRQ, "Zero interrupt received.");
140 goto out_sleep;
141 }
142
143 intr &= WL1271_INTR_MASK;
144
145 do {
146 wl1271_fw_status(wl, wl->fw_status);
147
148
149 if (intr & (WL1271_ACX_INTR_EVENT_A |
150 WL1271_ACX_INTR_EVENT_B)) {
151 wl1271_debug(DEBUG_IRQ,
152 "WL1271_ACX_INTR_EVENT (0x%x)", intr);
153 if (intr & WL1271_ACX_INTR_EVENT_A)
154 wl1271_event_handle(wl, 0);
155 else
156 wl1271_event_handle(wl, 1);
157 }
158
159 if (intr & WL1271_ACX_INTR_INIT_COMPLETE)
160 wl1271_debug(DEBUG_IRQ,
161 "WL1271_ACX_INTR_INIT_COMPLETE");
162
163 if (intr & WL1271_ACX_INTR_HW_AVAILABLE)
164 wl1271_debug(DEBUG_IRQ, "WL1271_ACX_INTR_HW_AVAILABLE");
165
166 if (intr & WL1271_ACX_INTR_DATA) {
167 u8 tx_res_cnt = wl->fw_status->tx_results_counter -
168 wl->tx_results_count;
169
170 wl1271_debug(DEBUG_IRQ, "WL1271_ACX_INTR_DATA");
171
172 /* check for tx results */
173 if (tx_res_cnt)
174 wl1271_tx_complete(wl, tx_res_cnt);
175
176 wl1271_rx(wl, wl->fw_status);
177 }
178
179 intr = wl1271_reg_read32(wl, ACX_REG_INTERRUPT_CLEAR);
180 intr &= WL1271_INTR_MASK;
181 } while (intr && --ctr);
182
183out_sleep:
73d0a13c
LC
184 wl1271_reg_write32(wl, ACX_REG_INTERRUPT_MASK,
185 WL1271_ACX_INTR_ALL & ~(WL1271_INTR_MASK));
f5fc0f86
LC
186 wl1271_ps_elp_sleep(wl);
187
188out:
189 mutex_unlock(&wl->mutex);
190}
191
192static irqreturn_t wl1271_irq(int irq, void *cookie)
193{
194 struct wl1271 *wl;
195 unsigned long flags;
196
197 wl1271_debug(DEBUG_IRQ, "IRQ");
198
199 wl = cookie;
200
201 /* complete the ELP completion */
202 spin_lock_irqsave(&wl->wl_lock, flags);
203 if (wl->elp_compl) {
204 complete(wl->elp_compl);
205 wl->elp_compl = NULL;
206 }
207
a64b07e8 208 ieee80211_queue_work(wl->hw, &wl->irq_work);
f5fc0f86
LC
209 spin_unlock_irqrestore(&wl->wl_lock, flags);
210
211 return IRQ_HANDLED;
212}
213
214static int wl1271_fetch_firmware(struct wl1271 *wl)
215{
216 const struct firmware *fw;
217 int ret;
218
219 ret = request_firmware(&fw, WL1271_FW_NAME, &wl->spi->dev);
220
221 if (ret < 0) {
222 wl1271_error("could not get firmware: %d", ret);
223 return ret;
224 }
225
226 if (fw->size % 4) {
227 wl1271_error("firmware size is not multiple of 32 bits: %zu",
228 fw->size);
229 ret = -EILSEQ;
230 goto out;
231 }
232
233 wl->fw_len = fw->size;
234 wl->fw = kmalloc(wl->fw_len, GFP_KERNEL);
235
236 if (!wl->fw) {
237 wl1271_error("could not allocate memory for the firmware");
238 ret = -ENOMEM;
239 goto out;
240 }
241
242 memcpy(wl->fw, fw->data, wl->fw_len);
243
244 ret = 0;
245
246out:
247 release_firmware(fw);
248
249 return ret;
250}
251
252static int wl1271_fetch_nvs(struct wl1271 *wl)
253{
254 const struct firmware *fw;
255 int ret;
256
257 ret = request_firmware(&fw, WL1271_NVS_NAME, &wl->spi->dev);
258
259 if (ret < 0) {
260 wl1271_error("could not get nvs file: %d", ret);
261 return ret;
262 }
263
264 if (fw->size % 4) {
265 wl1271_error("nvs size is not multiple of 32 bits: %zu",
266 fw->size);
267 ret = -EILSEQ;
268 goto out;
269 }
270
271 wl->nvs_len = fw->size;
272 wl->nvs = kmalloc(wl->nvs_len, GFP_KERNEL);
273
274 if (!wl->nvs) {
275 wl1271_error("could not allocate memory for the nvs file");
276 ret = -ENOMEM;
277 goto out;
278 }
279
280 memcpy(wl->nvs, fw->data, wl->nvs_len);
281
282 ret = 0;
283
284out:
285 release_firmware(fw);
286
287 return ret;
288}
289
290static void wl1271_fw_wakeup(struct wl1271 *wl)
291{
292 u32 elp_reg;
293
294 elp_reg = ELPCTRL_WAKE_UP;
295 wl1271_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, elp_reg);
296}
297
298static int wl1271_setup(struct wl1271 *wl)
299{
300 wl->fw_status = kmalloc(sizeof(*wl->fw_status), GFP_KERNEL);
301 if (!wl->fw_status)
302 return -ENOMEM;
303
304 wl->tx_res_if = kmalloc(sizeof(*wl->tx_res_if), GFP_KERNEL);
305 if (!wl->tx_res_if) {
306 kfree(wl->fw_status);
307 return -ENOMEM;
308 }
309
310 INIT_WORK(&wl->irq_work, wl1271_irq_work);
311 INIT_WORK(&wl->tx_work, wl1271_tx_work);
312 return 0;
313}
314
315static int wl1271_chip_wakeup(struct wl1271 *wl)
316{
317 int ret = 0;
318
319 wl1271_power_on(wl);
320 msleep(WL1271_POWER_ON_SLEEP);
321 wl1271_spi_reset(wl);
322 wl1271_spi_init(wl);
323
324 /* We don't need a real memory partition here, because we only want
325 * to use the registers at this point. */
326 wl1271_set_partition(wl,
327 0x00000000,
328 0x00000000,
329 REGISTERS_BASE,
330 REGISTERS_DOWN_SIZE);
331
332 /* ELP module wake up */
333 wl1271_fw_wakeup(wl);
334
335 /* whal_FwCtrl_BootSm() */
336
337 /* 0. read chip id from CHIP_ID */
338 wl->chip.id = wl1271_reg_read32(wl, CHIP_ID_B);
339
340 /* 1. check if chip id is valid */
341
342 switch (wl->chip.id) {
343 case CHIP_ID_1271_PG10:
344 wl1271_warning("chip id 0x%x (1271 PG10) support is obsolete",
345 wl->chip.id);
346
347 ret = wl1271_setup(wl);
348 if (ret < 0)
349 goto out;
350 break;
351 case CHIP_ID_1271_PG20:
352 wl1271_debug(DEBUG_BOOT, "chip id 0x%x (1271 PG20)",
353 wl->chip.id);
354
355 ret = wl1271_setup(wl);
356 if (ret < 0)
357 goto out;
358 break;
359 default:
360 wl1271_error("unsupported chip id: 0x%x", wl->chip.id);
361 ret = -ENODEV;
362 goto out;
363 }
364
365 if (wl->fw == NULL) {
366 ret = wl1271_fetch_firmware(wl);
367 if (ret < 0)
368 goto out;
369 }
370
371 /* No NVS from netlink, try to get it from the filesystem */
372 if (wl->nvs == NULL) {
373 ret = wl1271_fetch_nvs(wl);
374 if (ret < 0)
375 goto out;
376 }
377
378out:
379 return ret;
380}
381
c87dec9f
JO
382struct wl1271_filter_params {
383 unsigned int filters;
384 unsigned int changed;
385 int mc_list_length;
386 u8 mc_list[ACX_MC_ADDRESS_GROUP_MAX][ETH_ALEN];
387};
388
389#define WL1271_SUPPORTED_FILTERS (FIF_PROMISC_IN_BSS | \
390 FIF_ALLMULTI | \
391 FIF_FCSFAIL | \
392 FIF_BCN_PRBRESP_PROMISC | \
393 FIF_CONTROL | \
394 FIF_OTHER_BSS)
395
f5fc0f86
LC
396static void wl1271_filter_work(struct work_struct *work)
397{
398 struct wl1271 *wl =
399 container_of(work, struct wl1271, filter_work);
c87dec9f
JO
400 struct wl1271_filter_params *fp;
401 unsigned long flags;
402 bool enabled = true;
f5fc0f86
LC
403 int ret;
404
c87dec9f
JO
405 /* first, get the filter parameters */
406 spin_lock_irqsave(&wl->wl_lock, flags);
407 fp = wl->filter_params;
408 wl->filter_params = NULL;
409 spin_unlock_irqrestore(&wl->wl_lock, flags);
410
411 if (!fp)
412 return;
413
414 /* then, lock the mutex without risk of lock-up */
f5fc0f86
LC
415 mutex_lock(&wl->mutex);
416
417 if (wl->state == WL1271_STATE_OFF)
418 goto out;
419
420 ret = wl1271_ps_elp_wakeup(wl, false);
421 if (ret < 0)
422 goto out;
423
c87dec9f
JO
424 /* configure the mc filter regardless of the changed flags */
425 if (fp->filters & FIF_ALLMULTI)
426 enabled = false;
427
428 ret = wl1271_acx_group_address_tbl(wl, enabled,
429 fp->mc_list, fp->mc_list_length);
430 if (ret < 0)
431 goto out_sleep;
432
433 /* determine, whether supported filter values have changed */
434 if (fp->changed == 0)
435 goto out;
436
437 /* apply configured filters */
d94cd297 438 ret = wl1271_cmd_join(wl);
f5fc0f86
LC
439 if (ret < 0)
440 goto out_sleep;
441
442out_sleep:
443 wl1271_ps_elp_sleep(wl);
444
445out:
446 mutex_unlock(&wl->mutex);
c87dec9f 447 kfree(fp);
f5fc0f86
LC
448}
449
450int wl1271_plt_start(struct wl1271 *wl)
451{
452 int ret;
453
454 mutex_lock(&wl->mutex);
455
456 wl1271_notice("power up");
457
458 if (wl->state != WL1271_STATE_OFF) {
459 wl1271_error("cannot go into PLT state because not "
460 "in off state: %d", wl->state);
461 ret = -EBUSY;
462 goto out;
463 }
464
465 wl->state = WL1271_STATE_PLT;
466
467 ret = wl1271_chip_wakeup(wl);
468 if (ret < 0)
469 goto out;
470
471 ret = wl1271_boot(wl);
472 if (ret < 0)
473 goto out;
474
475 wl1271_notice("firmware booted in PLT mode (%s)", wl->chip.fw_ver);
476
477 ret = wl1271_plt_init(wl);
478 if (ret < 0)
479 goto out;
480
481out:
482 mutex_unlock(&wl->mutex);
483
484 return ret;
485}
486
487int wl1271_plt_stop(struct wl1271 *wl)
488{
489 int ret = 0;
490
491 mutex_lock(&wl->mutex);
492
493 wl1271_notice("power down");
494
495 if (wl->state != WL1271_STATE_PLT) {
496 wl1271_error("cannot power down because not in PLT "
497 "state: %d", wl->state);
498 ret = -EBUSY;
499 goto out;
500 }
501
502 wl1271_disable_interrupts(wl);
503 wl1271_power_off(wl);
504
505 wl->state = WL1271_STATE_OFF;
506
507out:
508 mutex_unlock(&wl->mutex);
509
510 return ret;
511}
512
513
514static int wl1271_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
515{
516 struct wl1271 *wl = hw->priv;
517
518 skb_queue_tail(&wl->tx_queue, skb);
519
520 /*
521 * The chip specific setup must run before the first TX packet -
522 * before that, the tx_work will not be initialized!
523 */
524
a64b07e8 525 ieee80211_queue_work(wl->hw, &wl->tx_work);
f5fc0f86
LC
526
527 /*
528 * The workqueue is slow to process the tx_queue and we need stop
529 * the queue here, otherwise the queue will get too long.
530 */
531 if (skb_queue_len(&wl->tx_queue) >= WL1271_TX_QUEUE_MAX_LENGTH) {
532 ieee80211_stop_queues(wl->hw);
533
534 /*
535 * FIXME: this is racy, the variable is not properly
536 * protected. Maybe fix this by removing the stupid
537 * variable altogether and checking the real queue state?
538 */
539 wl->tx_queue_stopped = true;
540 }
541
542 return NETDEV_TX_OK;
543}
544
545static int wl1271_op_start(struct ieee80211_hw *hw)
546{
547 struct wl1271 *wl = hw->priv;
548 int ret = 0;
549
550 wl1271_debug(DEBUG_MAC80211, "mac80211 start");
551
552 mutex_lock(&wl->mutex);
553
554 if (wl->state != WL1271_STATE_OFF) {
555 wl1271_error("cannot start because not in off state: %d",
556 wl->state);
557 ret = -EBUSY;
558 goto out;
559 }
560
561 ret = wl1271_chip_wakeup(wl);
562 if (ret < 0)
563 goto out;
564
565 ret = wl1271_boot(wl);
566 if (ret < 0)
567 goto out;
568
569 ret = wl1271_hw_init(wl);
570 if (ret < 0)
571 goto out;
572
573 wl->state = WL1271_STATE_ON;
574
575 wl1271_info("firmware booted (%s)", wl->chip.fw_ver);
576
577out:
578 if (ret < 0)
579 wl1271_power_off(wl);
580
581 mutex_unlock(&wl->mutex);
582
583 return ret;
584}
585
586static void wl1271_op_stop(struct ieee80211_hw *hw)
587{
588 struct wl1271 *wl = hw->priv;
c87dec9f 589 unsigned long flags;
f5fc0f86
LC
590 int i;
591
592 wl1271_info("down");
593
594 wl1271_debug(DEBUG_MAC80211, "mac80211 stop");
595
c87dec9f
JO
596 /* complete/cancel ongoing work */
597 cancel_work_sync(&wl->filter_work);
598 spin_lock_irqsave(&wl->wl_lock, flags);
599 kfree(wl->filter_params);
600 wl->filter_params = NULL;
601 spin_unlock_irqrestore(&wl->wl_lock, flags);
602
f5fc0f86
LC
603 mutex_lock(&wl->mutex);
604
605 WARN_ON(wl->state != WL1271_STATE_ON);
606
607 if (wl->scanning) {
608 mutex_unlock(&wl->mutex);
609 ieee80211_scan_completed(wl->hw, true);
610 mutex_lock(&wl->mutex);
611 wl->scanning = false;
612 }
613
614 wl->state = WL1271_STATE_OFF;
615
616 wl1271_disable_interrupts(wl);
617
618 mutex_unlock(&wl->mutex);
619
620 cancel_work_sync(&wl->irq_work);
621 cancel_work_sync(&wl->tx_work);
622 cancel_work_sync(&wl->filter_work);
623
624 mutex_lock(&wl->mutex);
625
626 /* let's notify MAC80211 about the remaining pending TX frames */
627 wl1271_tx_flush(wl);
628 wl1271_power_off(wl);
629
630 memset(wl->bssid, 0, ETH_ALEN);
631 memset(wl->ssid, 0, IW_ESSID_MAX_SIZE + 1);
632 wl->ssid_len = 0;
633 wl->listen_int = 1;
634 wl->bss_type = MAX_BSS_TYPE;
8a5a37a6 635 wl->band = IEEE80211_BAND_2GHZ;
f5fc0f86
LC
636
637 wl->rx_counter = 0;
638 wl->elp = false;
639 wl->psm = 0;
640 wl->tx_queue_stopped = false;
641 wl->power_level = WL1271_DEFAULT_POWER_LEVEL;
642 wl->tx_blocks_available = 0;
643 wl->tx_results_count = 0;
644 wl->tx_packets_count = 0;
ac4e4ce5
JO
645 wl->tx_security_last_seq = 0;
646 wl->tx_security_seq_16 = 0;
647 wl->tx_security_seq_32 = 0;
f5fc0f86
LC
648 wl->time_offset = 0;
649 wl->session_counter = 0;
650 for (i = 0; i < NUM_TX_QUEUES; i++)
651 wl->tx_blocks_freed[i] = 0;
652
653 wl1271_debugfs_reset(wl);
654 mutex_unlock(&wl->mutex);
655}
656
657static int wl1271_op_add_interface(struct ieee80211_hw *hw,
658 struct ieee80211_if_init_conf *conf)
659{
660 struct wl1271 *wl = hw->priv;
f5fc0f86
LC
661 int ret = 0;
662
e5539bcb
JL
663 wl1271_debug(DEBUG_MAC80211, "mac80211 add interface type %d mac %pM",
664 conf->type, conf->mac_addr);
f5fc0f86
LC
665
666 mutex_lock(&wl->mutex);
667
668 switch (conf->type) {
669 case NL80211_IFTYPE_STATION:
670 wl->bss_type = BSS_TYPE_STA_BSS;
671 break;
672 case NL80211_IFTYPE_ADHOC:
673 wl->bss_type = BSS_TYPE_IBSS;
674 break;
675 default:
676 ret = -EOPNOTSUPP;
677 goto out;
678 }
679
680 /* FIXME: what if conf->mac_addr changes? */
681
682out:
683 mutex_unlock(&wl->mutex);
684 return ret;
685}
686
687static void wl1271_op_remove_interface(struct ieee80211_hw *hw,
688 struct ieee80211_if_init_conf *conf)
689{
690 wl1271_debug(DEBUG_MAC80211, "mac80211 remove interface");
691}
692
693#if 0
694static int wl1271_op_config_interface(struct ieee80211_hw *hw,
695 struct ieee80211_vif *vif,
696 struct ieee80211_if_conf *conf)
697{
698 struct wl1271 *wl = hw->priv;
699 struct sk_buff *beacon;
f5fc0f86
LC
700 int ret;
701
3264690b
DM
702 wl1271_debug(DEBUG_MAC80211, "mac80211 config_interface bssid %pM",
703 conf->bssid);
f5fc0f86
LC
704 wl1271_dump_ascii(DEBUG_MAC80211, "ssid: ", conf->ssid,
705 conf->ssid_len);
706
707 mutex_lock(&wl->mutex);
708
709 ret = wl1271_ps_elp_wakeup(wl, false);
710 if (ret < 0)
711 goto out;
712
713 memcpy(wl->bssid, conf->bssid, ETH_ALEN);
714
715 ret = wl1271_cmd_build_null_data(wl);
716 if (ret < 0)
717 goto out_sleep;
718
719 wl->ssid_len = conf->ssid_len;
720 if (wl->ssid_len)
721 memcpy(wl->ssid, conf->ssid, wl->ssid_len);
722
723 if (wl->bss_type != BSS_TYPE_IBSS) {
d94cd297 724 ret = wl1271_cmd_join(wl);
f5fc0f86
LC
725 if (ret < 0)
726 goto out_sleep;
727 }
728
729 if (conf->changed & IEEE80211_IFCC_BEACON) {
730 beacon = ieee80211_beacon_get(hw, vif);
731 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_BEACON,
732 beacon->data, beacon->len);
733
734 if (ret < 0) {
735 dev_kfree_skb(beacon);
736 goto out_sleep;
737 }
738
739 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_PROBE_RESPONSE,
740 beacon->data, beacon->len);
741
742 dev_kfree_skb(beacon);
743
744 if (ret < 0)
745 goto out_sleep;
746
d94cd297 747 ret = wl1271_cmd_join(wl);
f5fc0f86
LC
748
749 if (ret < 0)
750 goto out_sleep;
751 }
752
753out_sleep:
754 wl1271_ps_elp_sleep(wl);
755
756out:
757 mutex_unlock(&wl->mutex);
758
759 return ret;
760}
761#endif
762
763static int wl1271_op_config(struct ieee80211_hw *hw, u32 changed)
764{
765 struct wl1271 *wl = hw->priv;
766 struct ieee80211_conf *conf = &hw->conf;
767 int channel, ret = 0;
768
769 channel = ieee80211_frequency_to_channel(conf->channel->center_freq);
770
771 wl1271_debug(DEBUG_MAC80211, "mac80211 config ch %d psm %s power %d",
772 channel,
773 conf->flags & IEEE80211_CONF_PS ? "on" : "off",
774 conf->power_level);
775
776 mutex_lock(&wl->mutex);
777
8a5a37a6
JO
778 wl->band = conf->channel->band;
779
f5fc0f86
LC
780 ret = wl1271_ps_elp_wakeup(wl, false);
781 if (ret < 0)
782 goto out;
783
784 if (channel != wl->channel) {
785 u8 old_channel = wl->channel;
786 wl->channel = channel;
787
d94cd297 788 ret = wl1271_cmd_join(wl);
f5fc0f86
LC
789 if (ret < 0) {
790 wl->channel = old_channel;
791 goto out_sleep;
792 }
793 }
794
795 ret = wl1271_cmd_build_null_data(wl);
796 if (ret < 0)
797 goto out_sleep;
798
799 if (conf->flags & IEEE80211_CONF_PS && !wl->psm_requested) {
800 wl1271_info("psm enabled");
801
802 wl->psm_requested = true;
803
804 /*
805 * We enter PSM only if we're already associated.
806 * If we're not, we'll enter it when joining an SSID,
807 * through the bss_info_changed() hook.
808 */
809 ret = wl1271_ps_set_mode(wl, STATION_POWER_SAVE_MODE);
810 } else if (!(conf->flags & IEEE80211_CONF_PS) &&
811 wl->psm_requested) {
812 wl1271_info("psm disabled");
813
814 wl->psm_requested = false;
815
816 if (wl->psm)
817 ret = wl1271_ps_set_mode(wl, STATION_ACTIVE_MODE);
818 }
819
820 if (conf->power_level != wl->power_level) {
821 ret = wl1271_acx_tx_power(wl, conf->power_level);
822 if (ret < 0)
823 goto out;
824
825 wl->power_level = conf->power_level;
826 }
827
828out_sleep:
829 wl1271_ps_elp_sleep(wl);
830
831out:
832 mutex_unlock(&wl->mutex);
833
834 return ret;
835}
836
c87dec9f
JO
837static u64 wl1271_op_prepare_multicast(struct ieee80211_hw *hw, int mc_count,
838 struct dev_addr_list *mc_list)
839{
840 struct wl1271 *wl = hw->priv;
841 struct wl1271_filter_params *fp;
842 unsigned long flags;
843 int i;
844
845 /*
846 * FIXME: we should return a hash that will be passed to
847 * configure_filter() instead of saving everything in the context.
848 */
849
850 fp = kzalloc(sizeof(*fp), GFP_KERNEL);
851 if (!fp) {
852 wl1271_error("Out of memory setting filters.");
853 return 0;
854 }
855
856 /* update multicast filtering parameters */
857 if (mc_count > ACX_MC_ADDRESS_GROUP_MAX) {
858 mc_count = 0;
859 fp->filters |= FIF_ALLMULTI;
860 }
861
862 fp->mc_list_length = 0;
863 for (i = 0; i < mc_count; i++) {
864 if (mc_list->da_addrlen == ETH_ALEN) {
865 memcpy(fp->mc_list[fp->mc_list_length],
866 mc_list->da_addr, ETH_ALEN);
867 fp->mc_list_length++;
868 } else
869 wl1271_warning("Unknown mc address length.");
870 }
871
872 spin_lock_irqsave(&wl->wl_lock, flags);
873 kfree(wl->filter_params);
874 wl->filter_params = fp;
875 spin_unlock_irqrestore(&wl->wl_lock, flags);
876
877 return 1;
878}
f5fc0f86
LC
879
880static void wl1271_op_configure_filter(struct ieee80211_hw *hw,
881 unsigned int changed,
c87dec9f 882 unsigned int *total, u64 multicast)
f5fc0f86
LC
883{
884 struct wl1271 *wl = hw->priv;
885
886 wl1271_debug(DEBUG_MAC80211, "mac80211 configure filter");
887
888 *total &= WL1271_SUPPORTED_FILTERS;
889 changed &= WL1271_SUPPORTED_FILTERS;
890
c87dec9f 891 if (!multicast)
f5fc0f86
LC
892 return;
893
f5fc0f86 894 /*
c87dec9f
JO
895 * FIXME: for now we are still using a workqueue for filter
896 * configuration, but with the new mac80211, this is not needed,
897 * since configure_filter can now sleep. We now have
898 * prepare_multicast, which needs to be atomic instead.
f5fc0f86 899 */
c87dec9f
JO
900
901 /* store current filter config */
902 wl->filter_params->filters = *total;
903 wl->filter_params->changed = changed;
904
905 ieee80211_queue_work(wl->hw, &wl->filter_work);
f5fc0f86
LC
906}
907
908static int wl1271_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
909 struct ieee80211_vif *vif,
910 struct ieee80211_sta *sta,
911 struct ieee80211_key_conf *key_conf)
912{
913 struct wl1271 *wl = hw->priv;
914 const u8 *addr;
915 int ret;
ac4e4ce5
JO
916 u32 tx_seq_32 = 0;
917 u16 tx_seq_16 = 0;
f5fc0f86
LC
918 u8 key_type;
919
920 static const u8 bcast_addr[ETH_ALEN] =
921 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
922
923 wl1271_debug(DEBUG_MAC80211, "mac80211 set key");
924
925 addr = sta ? sta->addr : bcast_addr;
926
927 wl1271_debug(DEBUG_CRYPT, "CMD: 0x%x", cmd);
928 wl1271_dump(DEBUG_CRYPT, "ADDR: ", addr, ETH_ALEN);
929 wl1271_debug(DEBUG_CRYPT, "Key: algo:0x%x, id:%d, len:%d flags 0x%x",
930 key_conf->alg, key_conf->keyidx,
931 key_conf->keylen, key_conf->flags);
932 wl1271_dump(DEBUG_CRYPT, "KEY: ", key_conf->key, key_conf->keylen);
933
934 if (is_zero_ether_addr(addr)) {
935 /* We dont support TX only encryption */
936 ret = -EOPNOTSUPP;
937 goto out;
938 }
939
940 mutex_lock(&wl->mutex);
941
942 ret = wl1271_ps_elp_wakeup(wl, false);
943 if (ret < 0)
944 goto out_unlock;
945
946 switch (key_conf->alg) {
947 case ALG_WEP:
948 key_type = KEY_WEP;
949
950 key_conf->hw_key_idx = key_conf->keyidx;
951 break;
952 case ALG_TKIP:
953 key_type = KEY_TKIP;
954
955 key_conf->hw_key_idx = key_conf->keyidx;
ac4e4ce5
JO
956 tx_seq_32 = wl->tx_security_seq_32;
957 tx_seq_16 = wl->tx_security_seq_16;
f5fc0f86
LC
958 break;
959 case ALG_CCMP:
960 key_type = KEY_AES;
961
962 key_conf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
ac4e4ce5
JO
963 tx_seq_32 = wl->tx_security_seq_32;
964 tx_seq_16 = wl->tx_security_seq_16;
f5fc0f86
LC
965 break;
966 default:
967 wl1271_error("Unknown key algo 0x%x", key_conf->alg);
968
969 ret = -EOPNOTSUPP;
970 goto out_sleep;
971 }
972
973 switch (cmd) {
974 case SET_KEY:
975 ret = wl1271_cmd_set_key(wl, KEY_ADD_OR_REPLACE,
976 key_conf->keyidx, key_type,
977 key_conf->keylen, key_conf->key,
ac4e4ce5 978 addr, tx_seq_32, tx_seq_16);
f5fc0f86
LC
979 if (ret < 0) {
980 wl1271_error("Could not add or replace key");
981 goto out_sleep;
982 }
983 break;
984
985 case DISABLE_KEY:
986 ret = wl1271_cmd_set_key(wl, KEY_REMOVE,
987 key_conf->keyidx, key_type,
988 key_conf->keylen, key_conf->key,
ac4e4ce5 989 addr, 0, 0);
f5fc0f86
LC
990 if (ret < 0) {
991 wl1271_error("Could not remove key");
992 goto out_sleep;
993 }
994 break;
995
996 default:
997 wl1271_error("Unsupported key cmd 0x%x", cmd);
998 ret = -EOPNOTSUPP;
999 goto out_sleep;
1000
1001 break;
1002 }
1003
1004out_sleep:
1005 wl1271_ps_elp_sleep(wl);
1006
1007out_unlock:
1008 mutex_unlock(&wl->mutex);
1009
1010out:
1011 return ret;
1012}
1013
1014static int wl1271_op_hw_scan(struct ieee80211_hw *hw,
1015 struct cfg80211_scan_request *req)
1016{
1017 struct wl1271 *wl = hw->priv;
1018 int ret;
1019 u8 *ssid = NULL;
1020 size_t ssid_len = 0;
1021
1022 wl1271_debug(DEBUG_MAC80211, "mac80211 hw scan");
1023
1024 if (req->n_ssids) {
1025 ssid = req->ssids[0].ssid;
1026 ssid_len = req->ssids[0].ssid_len;
1027 }
1028
1029 mutex_lock(&wl->mutex);
1030
1031 ret = wl1271_ps_elp_wakeup(wl, false);
1032 if (ret < 0)
1033 goto out;
1034
1035 ret = wl1271_cmd_scan(hw->priv, ssid, ssid_len, 1, 0, 13, 3);
1036
1037 wl1271_ps_elp_sleep(wl);
1038
1039out:
1040 mutex_unlock(&wl->mutex);
1041
1042 return ret;
1043}
1044
1045static int wl1271_op_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
1046{
1047 struct wl1271 *wl = hw->priv;
1048 int ret;
1049
1050 mutex_lock(&wl->mutex);
1051
1052 ret = wl1271_ps_elp_wakeup(wl, false);
1053 if (ret < 0)
1054 goto out;
1055
1056 ret = wl1271_acx_rts_threshold(wl, (u16) value);
1057 if (ret < 0)
1058 wl1271_warning("wl1271_op_set_rts_threshold failed: %d", ret);
1059
1060 wl1271_ps_elp_sleep(wl);
1061
1062out:
1063 mutex_unlock(&wl->mutex);
1064
1065 return ret;
1066}
1067
8a5a37a6
JO
1068static u32 wl1271_enabled_rates_get(struct wl1271 *wl, u64 basic_rate_set)
1069{
1070 struct ieee80211_supported_band *band;
1071 u32 enabled_rates = 0;
1072 int bit;
1073
1074 band = wl->hw->wiphy->bands[wl->band];
1075 for (bit = 0; bit < band->n_bitrates; bit++) {
1076 if (basic_rate_set & 0x1)
1077 enabled_rates |= band->bitrates[bit].hw_value;
1078 basic_rate_set >>= 1;
1079 }
1080
1081 return enabled_rates;
1082}
1083
f5fc0f86
LC
1084static void wl1271_op_bss_info_changed(struct ieee80211_hw *hw,
1085 struct ieee80211_vif *vif,
1086 struct ieee80211_bss_conf *bss_conf,
1087 u32 changed)
1088{
1089 enum wl1271_cmd_ps_mode mode;
1090 struct wl1271 *wl = hw->priv;
1091 int ret;
1092
1093 wl1271_debug(DEBUG_MAC80211, "mac80211 bss info changed");
1094
1095 mutex_lock(&wl->mutex);
1096
1097 ret = wl1271_ps_elp_wakeup(wl, false);
1098 if (ret < 0)
1099 goto out;
1100
1101 if (changed & BSS_CHANGED_ASSOC) {
1102 if (bss_conf->assoc) {
d94cd297
JO
1103 wl->beacon_int = bss_conf->beacon_int;
1104 wl->dtim_period = bss_conf->dtim_period;
f5fc0f86
LC
1105 wl->aid = bss_conf->aid;
1106
d94cd297
JO
1107 ret = wl1271_cmd_join(wl);
1108 if (ret < 0) {
1109 wl1271_warning("Association configuration "
1110 "failed %d", ret);
1111 goto out_sleep;
1112 }
1113
f5fc0f86
LC
1114 ret = wl1271_cmd_build_ps_poll(wl, wl->aid);
1115 if (ret < 0)
1116 goto out_sleep;
1117
1118 ret = wl1271_acx_aid(wl, wl->aid);
1119 if (ret < 0)
1120 goto out_sleep;
1121
1122 /* If we want to go in PSM but we're not there yet */
1123 if (wl->psm_requested && !wl->psm) {
1124 mode = STATION_POWER_SAVE_MODE;
1125 ret = wl1271_ps_set_mode(wl, mode);
1126 if (ret < 0)
1127 goto out_sleep;
1128 }
d94cd297
JO
1129 } else {
1130 /* use defaults when not associated */
1131 wl->beacon_int = WL1271_DEFAULT_BEACON_INT;
1132 wl->dtim_period = WL1271_DEFAULT_DTIM_PERIOD;
1133 wl->basic_rate_set = WL1271_DEFAULT_BASIC_RATE_SET;
1134 wl->aid = 0;
f5fc0f86 1135 }
d94cd297 1136
f5fc0f86 1137 }
8a5a37a6 1138
f5fc0f86
LC
1139 if (changed & BSS_CHANGED_ERP_SLOT) {
1140 if (bss_conf->use_short_slot)
1141 ret = wl1271_acx_slot(wl, SLOT_TIME_SHORT);
1142 else
1143 ret = wl1271_acx_slot(wl, SLOT_TIME_LONG);
1144 if (ret < 0) {
1145 wl1271_warning("Set slot time failed %d", ret);
1146 goto out_sleep;
1147 }
1148 }
1149
1150 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
1151 if (bss_conf->use_short_preamble)
1152 wl1271_acx_set_preamble(wl, ACX_PREAMBLE_SHORT);
1153 else
1154 wl1271_acx_set_preamble(wl, ACX_PREAMBLE_LONG);
1155 }
1156
1157 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
1158 if (bss_conf->use_cts_prot)
1159 ret = wl1271_acx_cts_protect(wl, CTSPROTECT_ENABLE);
1160 else
1161 ret = wl1271_acx_cts_protect(wl, CTSPROTECT_DISABLE);
1162 if (ret < 0) {
1163 wl1271_warning("Set ctsprotect failed %d", ret);
1164 goto out_sleep;
1165 }
1166 }
1167
8a5a37a6 1168 if (changed & BSS_CHANGED_BASIC_RATES) {
d94cd297 1169 wl->basic_rate_set = wl1271_enabled_rates_get(
8a5a37a6 1170 wl, bss_conf->basic_rates);
d94cd297
JO
1171 ret = wl1271_acx_rate_policies(wl, wl->basic_rate_set);
1172
8a5a37a6
JO
1173 if (ret < 0) {
1174 wl1271_warning("Set rate policies failed %d", ret);
1175 goto out_sleep;
1176 }
d94cd297
JO
1177 ret = wl1271_cmd_join(wl);
1178 if (ret < 0) {
1179 wl1271_warning("Join with new basic rate "
1180 "set failed %d", ret);
1181 goto out_sleep;
1182 }
8a5a37a6
JO
1183 }
1184
f5fc0f86
LC
1185out_sleep:
1186 wl1271_ps_elp_sleep(wl);
1187
1188out:
1189 mutex_unlock(&wl->mutex);
1190}
1191
1192
1193/* can't be const, mac80211 writes to this */
1194static struct ieee80211_rate wl1271_rates[] = {
1195 { .bitrate = 10,
1196 .hw_value = 0x1,
1197 .hw_value_short = 0x1, },
1198 { .bitrate = 20,
1199 .hw_value = 0x2,
1200 .hw_value_short = 0x2,
1201 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1202 { .bitrate = 55,
1203 .hw_value = 0x4,
1204 .hw_value_short = 0x4,
1205 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1206 { .bitrate = 110,
1207 .hw_value = 0x20,
1208 .hw_value_short = 0x20,
1209 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1210 { .bitrate = 60,
1211 .hw_value = 0x8,
1212 .hw_value_short = 0x8, },
1213 { .bitrate = 90,
1214 .hw_value = 0x10,
1215 .hw_value_short = 0x10, },
1216 { .bitrate = 120,
1217 .hw_value = 0x40,
1218 .hw_value_short = 0x40, },
1219 { .bitrate = 180,
1220 .hw_value = 0x80,
1221 .hw_value_short = 0x80, },
1222 { .bitrate = 240,
1223 .hw_value = 0x200,
1224 .hw_value_short = 0x200, },
1225 { .bitrate = 360,
1226 .hw_value = 0x400,
1227 .hw_value_short = 0x400, },
1228 { .bitrate = 480,
1229 .hw_value = 0x800,
1230 .hw_value_short = 0x800, },
1231 { .bitrate = 540,
1232 .hw_value = 0x1000,
1233 .hw_value_short = 0x1000, },
1234};
1235
1236/* can't be const, mac80211 writes to this */
1237static struct ieee80211_channel wl1271_channels[] = {
1238 { .hw_value = 1, .center_freq = 2412},
1239 { .hw_value = 2, .center_freq = 2417},
1240 { .hw_value = 3, .center_freq = 2422},
1241 { .hw_value = 4, .center_freq = 2427},
1242 { .hw_value = 5, .center_freq = 2432},
1243 { .hw_value = 6, .center_freq = 2437},
1244 { .hw_value = 7, .center_freq = 2442},
1245 { .hw_value = 8, .center_freq = 2447},
1246 { .hw_value = 9, .center_freq = 2452},
1247 { .hw_value = 10, .center_freq = 2457},
1248 { .hw_value = 11, .center_freq = 2462},
1249 { .hw_value = 12, .center_freq = 2467},
1250 { .hw_value = 13, .center_freq = 2472},
1251};
1252
1253/* can't be const, mac80211 writes to this */
1254static struct ieee80211_supported_band wl1271_band_2ghz = {
1255 .channels = wl1271_channels,
1256 .n_channels = ARRAY_SIZE(wl1271_channels),
1257 .bitrates = wl1271_rates,
1258 .n_bitrates = ARRAY_SIZE(wl1271_rates),
1259};
1260
1261static const struct ieee80211_ops wl1271_ops = {
1262 .start = wl1271_op_start,
1263 .stop = wl1271_op_stop,
1264 .add_interface = wl1271_op_add_interface,
1265 .remove_interface = wl1271_op_remove_interface,
1266 .config = wl1271_op_config,
1267/* .config_interface = wl1271_op_config_interface, */
c87dec9f 1268 .prepare_multicast = wl1271_op_prepare_multicast,
f5fc0f86
LC
1269 .configure_filter = wl1271_op_configure_filter,
1270 .tx = wl1271_op_tx,
1271 .set_key = wl1271_op_set_key,
1272 .hw_scan = wl1271_op_hw_scan,
1273 .bss_info_changed = wl1271_op_bss_info_changed,
1274 .set_rts_threshold = wl1271_op_set_rts_threshold,
1275};
1276
1277static int wl1271_register_hw(struct wl1271 *wl)
1278{
1279 int ret;
1280
1281 if (wl->mac80211_registered)
1282 return 0;
1283
1284 SET_IEEE80211_PERM_ADDR(wl->hw, wl->mac_addr);
1285
1286 ret = ieee80211_register_hw(wl->hw);
1287 if (ret < 0) {
1288 wl1271_error("unable to register mac80211 hw: %d", ret);
1289 return ret;
1290 }
1291
1292 wl->mac80211_registered = true;
1293
1294 wl1271_notice("loaded");
1295
1296 return 0;
1297}
1298
1299static int wl1271_init_ieee80211(struct wl1271 *wl)
1300{
1e2b7976
JO
1301 /* The tx descriptor buffer and the TKIP space. */
1302 wl->hw->extra_tx_headroom = WL1271_TKIP_IV_SPACE +
1303 sizeof(struct wl1271_tx_hw_descr);
f5fc0f86
LC
1304
1305 /* unit us */
1306 /* FIXME: find a proper value */
1307 wl->hw->channel_change_time = 10000;
1308
1309 wl->hw->flags = IEEE80211_HW_SIGNAL_DBM |
1310 IEEE80211_HW_NOISE_DBM;
1311
1312 wl->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
1313 wl->hw->wiphy->max_scan_ssids = 1;
1314 wl->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &wl1271_band_2ghz;
1315
1316 SET_IEEE80211_DEV(wl->hw, &wl->spi->dev);
1317
1318 return 0;
1319}
1320
1321static void wl1271_device_release(struct device *dev)
1322{
1323
1324}
1325
1326static struct platform_device wl1271_device = {
1327 .name = "wl1271",
1328 .id = -1,
1329
1330 /* device model insists to have a release function */
1331 .dev = {
1332 .release = wl1271_device_release,
1333 },
1334};
1335
1336#define WL1271_DEFAULT_CHANNEL 0
1337static int __devinit wl1271_probe(struct spi_device *spi)
1338{
1339 struct wl12xx_platform_data *pdata;
1340 struct ieee80211_hw *hw;
1341 struct wl1271 *wl;
1342 int ret, i;
1343 static const u8 nokia_oui[3] = {0x00, 0x1f, 0xdf};
1344
1345 pdata = spi->dev.platform_data;
1346 if (!pdata) {
1347 wl1271_error("no platform data");
1348 return -ENODEV;
1349 }
1350
1351 hw = ieee80211_alloc_hw(sizeof(*wl), &wl1271_ops);
1352 if (!hw) {
1353 wl1271_error("could not alloc ieee80211_hw");
1354 return -ENOMEM;
1355 }
1356
1357 wl = hw->priv;
1358 memset(wl, 0, sizeof(*wl));
1359
1360 wl->hw = hw;
1361 dev_set_drvdata(&spi->dev, wl);
1362 wl->spi = spi;
1363
1364 skb_queue_head_init(&wl->tx_queue);
1365
1366 INIT_WORK(&wl->filter_work, wl1271_filter_work);
37b70a81 1367 INIT_DELAYED_WORK(&wl->elp_work, wl1271_elp_work);
f5fc0f86
LC
1368 wl->channel = WL1271_DEFAULT_CHANNEL;
1369 wl->scanning = false;
1370 wl->default_key = 0;
1371 wl->listen_int = 1;
1372 wl->rx_counter = 0;
1373 wl->rx_config = WL1271_DEFAULT_RX_CONFIG;
1374 wl->rx_filter = WL1271_DEFAULT_RX_FILTER;
1375 wl->elp = false;
1376 wl->psm = 0;
1377 wl->psm_requested = false;
1378 wl->tx_queue_stopped = false;
1379 wl->power_level = WL1271_DEFAULT_POWER_LEVEL;
d94cd297
JO
1380 wl->beacon_int = WL1271_DEFAULT_BEACON_INT;
1381 wl->dtim_period = WL1271_DEFAULT_DTIM_PERIOD;
1382 wl->basic_rate_set = WL1271_DEFAULT_BASIC_RATE_SET;
8a5a37a6 1383 wl->band = IEEE80211_BAND_2GHZ;
f5fc0f86 1384
be7078c2 1385 for (i = 0; i < ACX_TX_DESCRIPTORS; i++)
f5fc0f86
LC
1386 wl->tx_frames[i] = NULL;
1387
1388 spin_lock_init(&wl->wl_lock);
1389
1390 /*
1391 * In case our MAC address is not correctly set,
1392 * we use a random but Nokia MAC.
1393 */
1394 memcpy(wl->mac_addr, nokia_oui, 3);
1395 get_random_bytes(wl->mac_addr + 3, 3);
1396
1397 wl->state = WL1271_STATE_OFF;
1398 mutex_init(&wl->mutex);
1399
1400 wl->rx_descriptor = kmalloc(sizeof(*wl->rx_descriptor), GFP_KERNEL);
1401 if (!wl->rx_descriptor) {
1402 wl1271_error("could not allocate memory for rx descriptor");
1403 ret = -ENOMEM;
1404 goto out_free;
1405 }
1406
1407 /* This is the only SPI value that we need to set here, the rest
1408 * comes from the board-peripherals file */
1409 spi->bits_per_word = 32;
1410
1411 ret = spi_setup(spi);
1412 if (ret < 0) {
1413 wl1271_error("spi_setup failed");
1414 goto out_free;
1415 }
1416
1417 wl->set_power = pdata->set_power;
1418 if (!wl->set_power) {
1419 wl1271_error("set power function missing in platform data");
1420 ret = -ENODEV;
1421 goto out_free;
1422 }
1423
1424 wl->irq = spi->irq;
1425 if (wl->irq < 0) {
1426 wl1271_error("irq missing in platform data");
1427 ret = -ENODEV;
1428 goto out_free;
1429 }
1430
1431 ret = request_irq(wl->irq, wl1271_irq, 0, DRIVER_NAME, wl);
1432 if (ret < 0) {
1433 wl1271_error("request_irq() failed: %d", ret);
1434 goto out_free;
1435 }
1436
1437 set_irq_type(wl->irq, IRQ_TYPE_EDGE_RISING);
1438
1439 disable_irq(wl->irq);
1440
1441 ret = platform_device_register(&wl1271_device);
1442 if (ret) {
1443 wl1271_error("couldn't register platform device");
1444 goto out_irq;
1445 }
1446 dev_set_drvdata(&wl1271_device.dev, wl);
1447
1448 ret = wl1271_init_ieee80211(wl);
1449 if (ret)
1450 goto out_platform;
1451
1452 ret = wl1271_register_hw(wl);
1453 if (ret)
1454 goto out_platform;
1455
1456 wl1271_debugfs_init(wl);
1457
1458 wl1271_notice("initialized");
1459
1460 return 0;
1461
1462 out_platform:
1463 platform_device_unregister(&wl1271_device);
1464
1465 out_irq:
1466 free_irq(wl->irq, wl);
1467
1468 out_free:
1469 kfree(wl->rx_descriptor);
1470 wl->rx_descriptor = NULL;
1471
1472 ieee80211_free_hw(hw);
1473
1474 return ret;
1475}
1476
1477static int __devexit wl1271_remove(struct spi_device *spi)
1478{
1479 struct wl1271 *wl = dev_get_drvdata(&spi->dev);
1480
1481 ieee80211_unregister_hw(wl->hw);
1482
1483 wl1271_debugfs_exit(wl);
1484 platform_device_unregister(&wl1271_device);
1485 free_irq(wl->irq, wl);
1486 kfree(wl->target_mem_map);
1487 kfree(wl->fw);
1488 wl->fw = NULL;
1489 kfree(wl->nvs);
1490 wl->nvs = NULL;
1491
1492 kfree(wl->rx_descriptor);
1493 wl->rx_descriptor = NULL;
1494
1495 kfree(wl->fw_status);
1496 kfree(wl->tx_res_if);
1497
1498 ieee80211_free_hw(wl->hw);
1499
1500 return 0;
1501}
1502
1503
1504static struct spi_driver wl1271_spi_driver = {
1505 .driver = {
1506 .name = "wl1271",
1507 .bus = &spi_bus_type,
1508 .owner = THIS_MODULE,
1509 },
1510
1511 .probe = wl1271_probe,
1512 .remove = __devexit_p(wl1271_remove),
1513};
1514
1515static int __init wl1271_init(void)
1516{
1517 int ret;
1518
1519 ret = spi_register_driver(&wl1271_spi_driver);
1520 if (ret < 0) {
1521 wl1271_error("failed to register spi driver: %d", ret);
1522 goto out;
1523 }
1524
1525out:
1526 return ret;
1527}
1528
1529static void __exit wl1271_exit(void)
1530{
1531 spi_unregister_driver(&wl1271_spi_driver);
1532
1533 wl1271_notice("unloaded");
1534}
1535
1536module_init(wl1271_init);
1537module_exit(wl1271_exit);
1538
1539MODULE_LICENSE("GPL");
1540MODULE_AUTHOR("Luciano Coelho <luciano.coelho@nokia.com>");
2f018725 1541MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");