7f1093cd816c9b58c1d57646ba23ae086f8c91bb
[linux-2.6.git] / drivers / net / wireless / wl12xx / wl1271_main.c
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/vmalloc.h>
34 #include <linux/spi/wl12xx.h>
35
36 #include "wl1271.h"
37 #include "wl12xx_80211.h"
38 #include "wl1271_reg.h"
39 #include "wl1271_spi.h"
40 #include "wl1271_event.h"
41 #include "wl1271_tx.h"
42 #include "wl1271_rx.h"
43 #include "wl1271_ps.h"
44 #include "wl1271_init.h"
45 #include "wl1271_debugfs.h"
46 #include "wl1271_cmd.h"
47 #include "wl1271_boot.h"
48
49 static struct conf_drv_settings default_conf = {
50         .sg = {
51                 .per_threshold               = 7500,
52                 .max_scan_compensation_time  = 120000,
53                 .nfs_sample_interval         = 400,
54                 .load_ratio                  = 50,
55                 .auto_ps_mode                = 0,
56                 .probe_req_compensation      = 170,
57                 .scan_window_compensation    = 50,
58                 .antenna_config              = 0,
59                 .beacon_miss_threshold       = 60,
60                 .rate_adaptation_threshold   = CONF_HW_BIT_RATE_12MBPS,
61                 .rate_adaptation_snr         = 0
62         },
63         .rx = {
64                 .rx_msdu_life_time           = 512000,
65                 .packet_detection_threshold  = 0,
66                 .ps_poll_timeout             = 15,
67                 .upsd_timeout                = 15,
68                 .rts_threshold               = 2347,
69                 .rx_cca_threshold            = 0xFFEF,
70                 .irq_blk_threshold           = 0,
71                 .irq_pkt_threshold           = USHORT_MAX,
72                 .irq_timeout                 = 5,
73                 .queue_type                  = CONF_RX_QUEUE_TYPE_LOW_PRIORITY,
74         },
75         .tx = {
76                 .tx_energy_detection         = 0,
77                 .rc_conf                     = {
78                         .enabled_rates       = CONF_TX_RATE_MASK_UNSPECIFIED,
79                         .short_retry_limit   = 10,
80                         .long_retry_limit    = 10,
81                         .aflags              = 0
82                 },
83                 .ac_conf_count               = 4,
84                 .ac_conf                     = {
85                         [0] = {
86                                 .ac          = CONF_TX_AC_BE,
87                                 .cw_min      = 15,
88                                 .cw_max      = 63,
89                                 .aifsn       = 3,
90                                 .tx_op_limit = 0,
91                         },
92                         [1] = {
93                                 .ac          = CONF_TX_AC_BK,
94                                 .cw_min      = 15,
95                                 .cw_max      = 63,
96                                 .aifsn       = 7,
97                                 .tx_op_limit = 0,
98                         },
99                         [2] = {
100                                 .ac          = CONF_TX_AC_VI,
101                                 .cw_min      = 15,
102                                 .cw_max      = 63,
103                                 .aifsn       = CONF_TX_AIFS_PIFS,
104                                 .tx_op_limit = 3008,
105                         },
106                         [3] = {
107                                 .ac          = CONF_TX_AC_VO,
108                                 .cw_min      = 15,
109                                 .cw_max      = 63,
110                                 .aifsn       = CONF_TX_AIFS_PIFS,
111                                 .tx_op_limit = 1504,
112                         },
113                 },
114                 .tid_conf_count = 7,
115                 .tid_conf = {
116                         [0] = {
117                                 .queue_id    = 0,
118                                 .channel_type = CONF_CHANNEL_TYPE_DCF,
119                                 .tsid        = CONF_TX_AC_BE,
120                                 .ps_scheme   = CONF_PS_SCHEME_LEGACY,
121                                 .ack_policy  = CONF_ACK_POLICY_LEGACY,
122                                 .apsd_conf   = {0, 0},
123                         },
124                         [1] = {
125                                 .queue_id    = 1,
126                                 .channel_type = CONF_CHANNEL_TYPE_DCF,
127                                 .tsid        = CONF_TX_AC_BE,
128                                 .ps_scheme   = CONF_PS_SCHEME_LEGACY,
129                                 .ack_policy  = CONF_ACK_POLICY_LEGACY,
130                                 .apsd_conf   = {0, 0},
131                         },
132                         [2] = {
133                                 .queue_id    = 2,
134                                 .channel_type = CONF_CHANNEL_TYPE_DCF,
135                                 .tsid        = CONF_TX_AC_BE,
136                                 .ps_scheme   = CONF_PS_SCHEME_LEGACY,
137                                 .ack_policy  = CONF_ACK_POLICY_LEGACY,
138                                 .apsd_conf   = {0, 0},
139                         },
140                         [3] = {
141                                 .queue_id    = 3,
142                                 .channel_type = CONF_CHANNEL_TYPE_DCF,
143                                 .tsid        = CONF_TX_AC_BE,
144                                 .ps_scheme   = CONF_PS_SCHEME_LEGACY,
145                                 .ack_policy  = CONF_ACK_POLICY_LEGACY,
146                                 .apsd_conf   = {0, 0},
147                         },
148                         [4] = {
149                                 .queue_id    = 4,
150                                 .channel_type = CONF_CHANNEL_TYPE_DCF,
151                                 .tsid        = CONF_TX_AC_BE,
152                                 .ps_scheme   = CONF_PS_SCHEME_LEGACY,
153                                 .ack_policy  = CONF_ACK_POLICY_LEGACY,
154                                 .apsd_conf   = {0, 0},
155                         },
156                         [5] = {
157                                 .queue_id    = 5,
158                                 .channel_type = CONF_CHANNEL_TYPE_DCF,
159                                 .tsid        = CONF_TX_AC_BE,
160                                 .ps_scheme   = CONF_PS_SCHEME_LEGACY,
161                                 .ack_policy  = CONF_ACK_POLICY_LEGACY,
162                                 .apsd_conf   = {0, 0},
163                         },
164                         [6] = {
165                                 .queue_id    = 6,
166                                 .channel_type = CONF_CHANNEL_TYPE_DCF,
167                                 .tsid        = CONF_TX_AC_BE,
168                                 .ps_scheme   = CONF_PS_SCHEME_LEGACY,
169                                 .ack_policy  = CONF_ACK_POLICY_LEGACY,
170                                 .apsd_conf   = {0, 0},
171                         }
172                 },
173                 .frag_threshold              = IEEE80211_MAX_FRAG_THRESHOLD,
174                 .tx_compl_timeout            = 5,
175                 .tx_compl_threshold          = 5
176         },
177         .conn = {
178                 .wake_up_event               = CONF_WAKE_UP_EVENT_DTIM,
179                 .listen_interval             = 0,
180                 .bcn_filt_mode               = CONF_BCN_FILT_MODE_ENABLED,
181                 .bcn_filt_ie_count           = 1,
182                 .bcn_filt_ie = {
183                         [0] = {
184                                 .ie          = WLAN_EID_CHANNEL_SWITCH,
185                                 .rule        = CONF_BCN_RULE_PASS_ON_APPEARANCE,
186                         }
187                 },
188                 .synch_fail_thold            = 5,
189                 .bss_lose_timeout            = 100,
190                 .beacon_rx_timeout           = 10000,
191                 .broadcast_timeout           = 20000,
192                 .rx_broadcast_in_ps          = 1,
193                 .ps_poll_threshold           = 4,
194                 .sig_trigger_count           = 2,
195                 .sig_trigger = {
196                         [0] = {
197                                 .threshold   = -75,
198                                 .pacing      = 500,
199                                 .metric      = CONF_TRIG_METRIC_RSSI_BEACON,
200                                 .type        = CONF_TRIG_EVENT_TYPE_EDGE,
201                                 .direction   = CONF_TRIG_EVENT_DIR_LOW,
202                                 .hysteresis  = 2,
203                                 .index       = 0,
204                                 .enable      = 1
205                         },
206                         [1] = {
207                                 .threshold   = -75,
208                                 .pacing      = 500,
209                                 .metric      = CONF_TRIG_METRIC_RSSI_BEACON,
210                                 .type        = CONF_TRIG_EVENT_TYPE_EDGE,
211                                 .direction   = CONF_TRIG_EVENT_DIR_HIGH,
212                                 .hysteresis  = 2,
213                                 .index       = 1,
214                                 .enable      = 1
215                         }
216                 },
217                 .sig_weights = {
218                         .rssi_bcn_avg_weight = 10,
219                         .rssi_pkt_avg_weight = 10,
220                         .snr_bcn_avg_weight  = 10,
221                         .snr_pkt_avg_weight  = 10
222                 }
223         },
224         .init = {
225                 .sr_err_tbl = {
226                         [0] = {
227                                 .len         = 7,
228                                 .upper_limit = 0x03,
229                                 .values      = {
230                                         0x18, 0x10, 0x05, 0xfb, 0xf0, 0xe8,
231                                         0x00 }
232                         },
233                         [1] = {
234                                 .len         = 7,
235                                 .upper_limit = 0x03,
236                                 .values      = {
237                                         0x18, 0x10, 0x05, 0xf6, 0xf0, 0xe8,
238                                         0x00 }
239                         },
240                         [2] = {
241                                 .len         = 7,
242                                 .upper_limit = 0x03,
243                                 .values      = {
244                                         0x18, 0x10, 0x05, 0xfb, 0xf0, 0xe8,
245                                         0x00 }
246                         }
247                 },
248                 .sr_enable                   = 1,
249                 .genparam                    = {
250                         /*
251                          * FIXME: The correct value CONF_REF_CLK_38_4_E
252                          *        causes the firmware to crash on boot.
253                          *        The value 5 apparently is an
254                          *        unnoficial XTAL configuration of the
255                          *        same frequency, which appears to work.
256                          */
257                         .ref_clk             = 5,
258                         .settling_time       = 5,
259                         .clk_valid_on_wakeup = 0,
260                         .dc2dcmode           = 0,
261                         .single_dual_band    = 0,
262                         .tx_bip_fem_autodetect = 0,
263                         .tx_bip_fem_manufacturer = 1,
264                         .settings = 1,
265                 },
266                 .radioparam = {
267                         /* FIXME: 5GHz values unset! */
268                         .rx_trace_loss       = 10,
269                         .tx_trace_loss       = 10,
270                         .rx_rssi_and_proc_compens = {
271                                 0xec, 0xf6, 0x00, 0x0c, 0x18, 0xf8,
272                                 0xfc, 0x00, 0x08, 0x10, 0xf0, 0xf8,
273                                 0x00, 0x0a, 0x14 },
274                         .rx_trace_loss_5     = { 0, 0, 0, 0, 0, 0, 0 },
275                         .tx_trace_loss_5     = { 0, 0, 0, 0, 0, 0, 0 },
276                         .rx_rssi_and_proc_compens_5 = {
277                                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
278                                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
279                                 0x00, 0x00, 0x00 },
280                         .tx_ref_pd_voltage   = 0x24e,
281                         .tx_ref_power        = 0x78,
282                         .tx_offset_db        = 0x0,
283                         .tx_rate_limits_normal = {
284                                 0x1e, 0x1f, 0x22, 0x24, 0x28, 0x29 },
285                         .tx_rate_limits_degraded = {
286                                 0x1b, 0x1c, 0x1e, 0x20, 0x24, 0x25 },
287                         .tx_channel_limits_11b = {
288                                 0x22, 0x50, 0x50, 0x50, 0x50, 0x50,
289                                 0x50, 0x50, 0x50, 0x50, 0x22, 0x50,
290                                 0x22, 0x50 },
291                         .tx_channel_limits_ofdm = {
292                                 0x20, 0x50, 0x50, 0x50, 0x50, 0x50,
293                                 0x50, 0x50, 0x50, 0x50, 0x20, 0x50,
294                                 0x20, 0x50 },
295                         .tx_pdv_rate_offsets = {
296                                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
297                         .tx_ibias            = {
298                                 0x1a, 0x1a, 0x1a, 0x1a, 0x1a, 0x27 },
299                         .rx_fem_insertion_loss = 0x14,
300                         .tx_ref_pd_voltage_5 = { 0, 0, 0, 0, 0, 0, 0 },
301                         .tx_ref_power_5      = { 0, 0, 0, 0, 0, 0, 0 },
302                         .tx_offset_db_5      = {0, 0, 0, 0, 0, 0, 0 },
303                         .tx_rate_limits_normal_5 = {
304                                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
305                         .tx_rate_limits_degraded_5 = {
306                                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
307                         .tx_channel_limits_ofdm_5 = {
308                                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
309                                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
310                                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
311                                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
312                                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
313                                 0x00, 0x00, 0x00, 0x00, 0x00},
314                         .tx_pdv_rate_offsets_5 = {
315                                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
316                         .tx_ibias_5          = {
317                                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
318                         .rx_fem_insertion_loss_5 = { 0, 0, 0, 0, 0, 0, 0 }
319                 }
320         }
321 };
322
323 static void wl1271_conf_init(struct wl1271 *wl)
324 {
325
326         /*
327          * This function applies the default configuration to the driver. This
328          * function is invoked upon driver load (spi probe.)
329          *
330          * The configuration is stored in a run-time structure in order to
331          * facilitate for run-time adjustment of any of the parameters. Making
332          * changes to the configuration structure will apply the new values on
333          * the next interface up (wl1271_op_start.)
334          */
335
336         /* apply driver default configuration */
337         memcpy(&wl->conf, &default_conf, sizeof(default_conf));
338 }
339
340
341 static int wl1271_plt_init(struct wl1271 *wl)
342 {
343         int ret;
344
345         ret = wl1271_acx_init_mem_config(wl);
346         if (ret < 0)
347                 return ret;
348
349         ret = wl1271_cmd_data_path(wl, wl->channel, 1);
350         if (ret < 0)
351                 return ret;
352
353         return 0;
354 }
355
356 static void wl1271_disable_interrupts(struct wl1271 *wl)
357 {
358         disable_irq(wl->irq);
359 }
360
361 static void wl1271_power_off(struct wl1271 *wl)
362 {
363         wl->set_power(false);
364 }
365
366 static void wl1271_power_on(struct wl1271 *wl)
367 {
368         wl->set_power(true);
369 }
370
371 static void wl1271_fw_status(struct wl1271 *wl,
372                              struct wl1271_fw_status *status)
373 {
374         u32 total = 0;
375         int i;
376
377         wl1271_spi_read(wl, FW_STATUS_ADDR, status,
378                         sizeof(*status), false);
379
380         wl1271_debug(DEBUG_IRQ, "intr: 0x%x (fw_rx_counter = %d, "
381                      "drv_rx_counter = %d, tx_results_counter = %d)",
382                      status->intr,
383                      status->fw_rx_counter,
384                      status->drv_rx_counter,
385                      status->tx_results_counter);
386
387         /* update number of available TX blocks */
388         for (i = 0; i < NUM_TX_QUEUES; i++) {
389                 u32 cnt = status->tx_released_blks[i] - wl->tx_blocks_freed[i];
390                 wl->tx_blocks_freed[i] = status->tx_released_blks[i];
391                 wl->tx_blocks_available += cnt;
392                 total += cnt;
393         }
394
395         /* if more blocks are available now, schedule some tx work */
396         if (total && !skb_queue_empty(&wl->tx_queue))
397                 ieee80211_queue_work(wl->hw, &wl->tx_work);
398
399         /* update the host-chipset time offset */
400         wl->time_offset = jiffies_to_usecs(jiffies) - status->fw_localtime;
401 }
402
403 static void wl1271_irq_work(struct work_struct *work)
404 {
405         int ret;
406         u32 intr;
407         struct wl1271 *wl =
408                 container_of(work, struct wl1271, irq_work);
409
410         mutex_lock(&wl->mutex);
411
412         wl1271_debug(DEBUG_IRQ, "IRQ work");
413
414         if (wl->state == WL1271_STATE_OFF)
415                 goto out;
416
417         ret = wl1271_ps_elp_wakeup(wl, true);
418         if (ret < 0)
419                 goto out;
420
421         wl1271_spi_write32(wl, ACX_REG_INTERRUPT_MASK, WL1271_ACX_INTR_ALL);
422
423         wl1271_fw_status(wl, wl->fw_status);
424         intr = wl->fw_status->intr;
425         if (!intr) {
426                 wl1271_debug(DEBUG_IRQ, "Zero interrupt received.");
427                 goto out_sleep;
428         }
429
430         intr &= WL1271_INTR_MASK;
431
432         if (intr & (WL1271_ACX_INTR_EVENT_A |
433                     WL1271_ACX_INTR_EVENT_B)) {
434                 wl1271_debug(DEBUG_IRQ,
435                              "WL1271_ACX_INTR_EVENT (0x%x)", intr);
436                 if (intr & WL1271_ACX_INTR_EVENT_A)
437                         wl1271_event_handle(wl, 0);
438                 else
439                         wl1271_event_handle(wl, 1);
440         }
441
442         if (intr & WL1271_ACX_INTR_INIT_COMPLETE)
443                 wl1271_debug(DEBUG_IRQ,
444                              "WL1271_ACX_INTR_INIT_COMPLETE");
445
446         if (intr & WL1271_ACX_INTR_HW_AVAILABLE)
447                 wl1271_debug(DEBUG_IRQ, "WL1271_ACX_INTR_HW_AVAILABLE");
448
449         if (intr & WL1271_ACX_INTR_DATA) {
450                 u8 tx_res_cnt = wl->fw_status->tx_results_counter -
451                         wl->tx_results_count;
452
453                 wl1271_debug(DEBUG_IRQ, "WL1271_ACX_INTR_DATA");
454
455                 /* check for tx results */
456                 if (tx_res_cnt)
457                         wl1271_tx_complete(wl, tx_res_cnt);
458
459                 wl1271_rx(wl, wl->fw_status);
460         }
461
462 out_sleep:
463         wl1271_spi_write32(wl, ACX_REG_INTERRUPT_MASK,
464                            WL1271_ACX_INTR_ALL & ~(WL1271_INTR_MASK));
465         wl1271_ps_elp_sleep(wl);
466
467 out:
468         mutex_unlock(&wl->mutex);
469 }
470
471 static irqreturn_t wl1271_irq(int irq, void *cookie)
472 {
473         struct wl1271 *wl;
474         unsigned long flags;
475
476         wl1271_debug(DEBUG_IRQ, "IRQ");
477
478         wl = cookie;
479
480         /* complete the ELP completion */
481         spin_lock_irqsave(&wl->wl_lock, flags);
482         if (wl->elp_compl) {
483                 complete(wl->elp_compl);
484                 wl->elp_compl = NULL;
485         }
486
487         ieee80211_queue_work(wl->hw, &wl->irq_work);
488         spin_unlock_irqrestore(&wl->wl_lock, flags);
489
490         return IRQ_HANDLED;
491 }
492
493 static int wl1271_fetch_firmware(struct wl1271 *wl)
494 {
495         const struct firmware *fw;
496         int ret;
497
498         ret = request_firmware(&fw, WL1271_FW_NAME, &wl->spi->dev);
499
500         if (ret < 0) {
501                 wl1271_error("could not get firmware: %d", ret);
502                 return ret;
503         }
504
505         if (fw->size % 4) {
506                 wl1271_error("firmware size is not multiple of 32 bits: %zu",
507                              fw->size);
508                 ret = -EILSEQ;
509                 goto out;
510         }
511
512         wl->fw_len = fw->size;
513         wl->fw = vmalloc(wl->fw_len);
514
515         if (!wl->fw) {
516                 wl1271_error("could not allocate memory for the firmware");
517                 ret = -ENOMEM;
518                 goto out;
519         }
520
521         memcpy(wl->fw, fw->data, wl->fw_len);
522
523         ret = 0;
524
525 out:
526         release_firmware(fw);
527
528         return ret;
529 }
530
531 static int wl1271_fetch_nvs(struct wl1271 *wl)
532 {
533         const struct firmware *fw;
534         int ret;
535
536         ret = request_firmware(&fw, WL1271_NVS_NAME, &wl->spi->dev);
537
538         if (ret < 0) {
539                 wl1271_error("could not get nvs file: %d", ret);
540                 return ret;
541         }
542
543         if (fw->size % 4) {
544                 wl1271_error("nvs size is not multiple of 32 bits: %zu",
545                              fw->size);
546                 ret = -EILSEQ;
547                 goto out;
548         }
549
550         wl->nvs_len = fw->size;
551         wl->nvs = kmalloc(wl->nvs_len, GFP_KERNEL);
552
553         if (!wl->nvs) {
554                 wl1271_error("could not allocate memory for the nvs file");
555                 ret = -ENOMEM;
556                 goto out;
557         }
558
559         memcpy(wl->nvs, fw->data, wl->nvs_len);
560
561         ret = 0;
562
563 out:
564         release_firmware(fw);
565
566         return ret;
567 }
568
569 static void wl1271_fw_wakeup(struct wl1271 *wl)
570 {
571         u32 elp_reg;
572
573         elp_reg = ELPCTRL_WAKE_UP;
574         wl1271_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, elp_reg);
575 }
576
577 static int wl1271_setup(struct wl1271 *wl)
578 {
579         wl->fw_status = kmalloc(sizeof(*wl->fw_status), GFP_KERNEL);
580         if (!wl->fw_status)
581                 return -ENOMEM;
582
583         wl->tx_res_if = kmalloc(sizeof(*wl->tx_res_if), GFP_KERNEL);
584         if (!wl->tx_res_if) {
585                 kfree(wl->fw_status);
586                 return -ENOMEM;
587         }
588
589         INIT_WORK(&wl->irq_work, wl1271_irq_work);
590         INIT_WORK(&wl->tx_work, wl1271_tx_work);
591         return 0;
592 }
593
594 static int wl1271_chip_wakeup(struct wl1271 *wl)
595 {
596         struct wl1271_partition_set partition;
597         int ret = 0;
598
599         wl1271_power_on(wl);
600         msleep(WL1271_POWER_ON_SLEEP);
601         wl1271_spi_reset(wl);
602         wl1271_spi_init(wl);
603
604         /* We don't need a real memory partition here, because we only want
605          * to use the registers at this point. */
606         memset(&partition, 0, sizeof(partition));
607         partition.reg.start = REGISTERS_BASE;
608         partition.reg.size = REGISTERS_DOWN_SIZE;
609         wl1271_set_partition(wl, &partition);
610
611         /* ELP module wake up */
612         wl1271_fw_wakeup(wl);
613
614         /* whal_FwCtrl_BootSm() */
615
616         /* 0. read chip id from CHIP_ID */
617         wl->chip.id = wl1271_spi_read32(wl, CHIP_ID_B);
618
619         /* 1. check if chip id is valid */
620
621         switch (wl->chip.id) {
622         case CHIP_ID_1271_PG10:
623                 wl1271_warning("chip id 0x%x (1271 PG10) support is obsolete",
624                                wl->chip.id);
625
626                 ret = wl1271_setup(wl);
627                 if (ret < 0)
628                         goto out_power_off;
629                 break;
630         case CHIP_ID_1271_PG20:
631                 wl1271_debug(DEBUG_BOOT, "chip id 0x%x (1271 PG20)",
632                              wl->chip.id);
633
634                 ret = wl1271_setup(wl);
635                 if (ret < 0)
636                         goto out_power_off;
637                 break;
638         default:
639                 wl1271_error("unsupported chip id: 0x%x", wl->chip.id);
640                 ret = -ENODEV;
641                 goto out_power_off;
642         }
643
644         if (wl->fw == NULL) {
645                 ret = wl1271_fetch_firmware(wl);
646                 if (ret < 0)
647                         goto out_power_off;
648         }
649
650         /* No NVS from netlink, try to get it from the filesystem */
651         if (wl->nvs == NULL) {
652                 ret = wl1271_fetch_nvs(wl);
653                 if (ret < 0)
654                         goto out_power_off;
655         }
656
657         goto out;
658
659 out_power_off:
660         wl1271_power_off(wl);
661
662 out:
663         return ret;
664 }
665
666 struct wl1271_filter_params {
667         unsigned int filters;
668         unsigned int changed;
669         int mc_list_length;
670         u8 mc_list[ACX_MC_ADDRESS_GROUP_MAX][ETH_ALEN];
671 };
672
673 #define WL1271_SUPPORTED_FILTERS (FIF_PROMISC_IN_BSS | \
674                                   FIF_ALLMULTI | \
675                                   FIF_FCSFAIL | \
676                                   FIF_BCN_PRBRESP_PROMISC | \
677                                   FIF_CONTROL | \
678                                   FIF_OTHER_BSS)
679
680 static void wl1271_filter_work(struct work_struct *work)
681 {
682         struct wl1271 *wl =
683                 container_of(work, struct wl1271, filter_work);
684         struct wl1271_filter_params *fp;
685         unsigned long flags;
686         bool enabled = true;
687         int ret;
688
689         /* first, get the filter parameters */
690         spin_lock_irqsave(&wl->wl_lock, flags);
691         fp = wl->filter_params;
692         wl->filter_params = NULL;
693         spin_unlock_irqrestore(&wl->wl_lock, flags);
694
695         if (!fp)
696                 return;
697
698         /* then, lock the mutex without risk of lock-up */
699         mutex_lock(&wl->mutex);
700
701         if (wl->state == WL1271_STATE_OFF)
702                 goto out;
703
704         ret = wl1271_ps_elp_wakeup(wl, false);
705         if (ret < 0)
706                 goto out;
707
708         /* configure the mc filter regardless of the changed flags */
709         if (fp->filters & FIF_ALLMULTI)
710                 enabled = false;
711
712         ret = wl1271_acx_group_address_tbl(wl, enabled,
713                                            fp->mc_list, fp->mc_list_length);
714         if (ret < 0)
715                 goto out_sleep;
716
717         /* determine, whether supported filter values have changed */
718         if (fp->changed == 0)
719                 goto out;
720
721         /* apply configured filters */
722         ret = wl1271_acx_rx_config(wl, wl->rx_config, wl->rx_filter);
723         if (ret < 0)
724                 goto out_sleep;
725
726 out_sleep:
727         wl1271_ps_elp_sleep(wl);
728
729 out:
730         mutex_unlock(&wl->mutex);
731         kfree(fp);
732 }
733
734 int wl1271_plt_start(struct wl1271 *wl)
735 {
736         int ret;
737
738         mutex_lock(&wl->mutex);
739
740         wl1271_notice("power up");
741
742         if (wl->state != WL1271_STATE_OFF) {
743                 wl1271_error("cannot go into PLT state because not "
744                              "in off state: %d", wl->state);
745                 ret = -EBUSY;
746                 goto out;
747         }
748
749         wl->state = WL1271_STATE_PLT;
750
751         ret = wl1271_chip_wakeup(wl);
752         if (ret < 0)
753                 goto out;
754
755         ret = wl1271_boot(wl);
756         if (ret < 0)
757                 goto out_power_off;
758
759         wl1271_notice("firmware booted in PLT mode (%s)", wl->chip.fw_ver);
760
761         ret = wl1271_plt_init(wl);
762         if (ret < 0)
763                 goto out_irq_disable;
764
765         goto out;
766
767 out_irq_disable:
768         wl1271_disable_interrupts(wl);
769
770 out_power_off:
771         wl1271_power_off(wl);
772
773 out:
774         mutex_unlock(&wl->mutex);
775
776         return ret;
777 }
778
779 int wl1271_plt_stop(struct wl1271 *wl)
780 {
781         int ret = 0;
782
783         mutex_lock(&wl->mutex);
784
785         wl1271_notice("power down");
786
787         if (wl->state != WL1271_STATE_PLT) {
788                 wl1271_error("cannot power down because not in PLT "
789                              "state: %d", wl->state);
790                 ret = -EBUSY;
791                 goto out;
792         }
793
794         wl1271_disable_interrupts(wl);
795         wl1271_power_off(wl);
796
797         wl->state = WL1271_STATE_OFF;
798
799 out:
800         mutex_unlock(&wl->mutex);
801
802         return ret;
803 }
804
805
806 static int wl1271_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
807 {
808         struct wl1271 *wl = hw->priv;
809
810         skb_queue_tail(&wl->tx_queue, skb);
811
812         /*
813          * The chip specific setup must run before the first TX packet -
814          * before that, the tx_work will not be initialized!
815          */
816
817         ieee80211_queue_work(wl->hw, &wl->tx_work);
818
819         /*
820          * The workqueue is slow to process the tx_queue and we need stop
821          * the queue here, otherwise the queue will get too long.
822          */
823         if (skb_queue_len(&wl->tx_queue) >= WL1271_TX_QUEUE_MAX_LENGTH) {
824                 ieee80211_stop_queues(wl->hw);
825
826                 /*
827                  * FIXME: this is racy, the variable is not properly
828                  * protected. Maybe fix this by removing the stupid
829                  * variable altogether and checking the real queue state?
830                  */
831                 wl->tx_queue_stopped = true;
832         }
833
834         return NETDEV_TX_OK;
835 }
836
837 static int wl1271_op_start(struct ieee80211_hw *hw)
838 {
839         struct wl1271 *wl = hw->priv;
840         int ret = 0;
841
842         wl1271_debug(DEBUG_MAC80211, "mac80211 start");
843
844         mutex_lock(&wl->mutex);
845
846         if (wl->state != WL1271_STATE_OFF) {
847                 wl1271_error("cannot start because not in off state: %d",
848                              wl->state);
849                 ret = -EBUSY;
850                 goto out;
851         }
852
853         ret = wl1271_chip_wakeup(wl);
854         if (ret < 0)
855                 goto out;
856
857         ret = wl1271_boot(wl);
858         if (ret < 0)
859                 goto out_power_off;
860
861         ret = wl1271_hw_init(wl);
862         if (ret < 0)
863                 goto out_irq_disable;
864
865         wl->state = WL1271_STATE_ON;
866
867         wl1271_info("firmware booted (%s)", wl->chip.fw_ver);
868
869         goto out;
870
871 out_irq_disable:
872         wl1271_disable_interrupts(wl);
873
874 out_power_off:
875         wl1271_power_off(wl);
876
877 out:
878         mutex_unlock(&wl->mutex);
879
880         return ret;
881 }
882
883 static void wl1271_op_stop(struct ieee80211_hw *hw)
884 {
885         struct wl1271 *wl = hw->priv;
886         unsigned long flags;
887         int i;
888
889         wl1271_info("down");
890
891         wl1271_debug(DEBUG_MAC80211, "mac80211 stop");
892
893         /* complete/cancel ongoing work */
894         cancel_work_sync(&wl->filter_work);
895         spin_lock_irqsave(&wl->wl_lock, flags);
896         kfree(wl->filter_params);
897         wl->filter_params = NULL;
898         spin_unlock_irqrestore(&wl->wl_lock, flags);
899
900         mutex_lock(&wl->mutex);
901
902         WARN_ON(wl->state != WL1271_STATE_ON);
903
904         if (wl->scanning) {
905                 mutex_unlock(&wl->mutex);
906                 ieee80211_scan_completed(wl->hw, true);
907                 mutex_lock(&wl->mutex);
908                 wl->scanning = false;
909         }
910
911         wl->state = WL1271_STATE_OFF;
912
913         wl1271_disable_interrupts(wl);
914
915         mutex_unlock(&wl->mutex);
916
917         cancel_work_sync(&wl->irq_work);
918         cancel_work_sync(&wl->tx_work);
919         cancel_work_sync(&wl->filter_work);
920
921         mutex_lock(&wl->mutex);
922
923         /* let's notify MAC80211 about the remaining pending TX frames */
924         wl1271_tx_flush(wl);
925         wl1271_power_off(wl);
926
927         memset(wl->bssid, 0, ETH_ALEN);
928         memset(wl->ssid, 0, IW_ESSID_MAX_SIZE + 1);
929         wl->ssid_len = 0;
930         wl->bss_type = MAX_BSS_TYPE;
931         wl->band = IEEE80211_BAND_2GHZ;
932
933         wl->rx_counter = 0;
934         wl->elp = false;
935         wl->psm = 0;
936         wl->tx_queue_stopped = false;
937         wl->power_level = WL1271_DEFAULT_POWER_LEVEL;
938         wl->tx_blocks_available = 0;
939         wl->tx_results_count = 0;
940         wl->tx_packets_count = 0;
941         wl->tx_security_last_seq = 0;
942         wl->tx_security_seq_16 = 0;
943         wl->tx_security_seq_32 = 0;
944         wl->time_offset = 0;
945         wl->session_counter = 0;
946         wl->joined = false;
947
948         for (i = 0; i < NUM_TX_QUEUES; i++)
949                 wl->tx_blocks_freed[i] = 0;
950
951         wl1271_debugfs_reset(wl);
952         mutex_unlock(&wl->mutex);
953 }
954
955 static int wl1271_op_add_interface(struct ieee80211_hw *hw,
956                                    struct ieee80211_if_init_conf *conf)
957 {
958         struct wl1271 *wl = hw->priv;
959         int ret = 0;
960
961         wl1271_debug(DEBUG_MAC80211, "mac80211 add interface type %d mac %pM",
962                      conf->type, conf->mac_addr);
963
964         mutex_lock(&wl->mutex);
965         if (wl->vif) {
966                 ret = -EBUSY;
967                 goto out;
968         }
969
970         wl->vif = conf->vif;
971
972         switch (conf->type) {
973         case NL80211_IFTYPE_STATION:
974                 wl->bss_type = BSS_TYPE_STA_BSS;
975                 break;
976         case NL80211_IFTYPE_ADHOC:
977                 wl->bss_type = BSS_TYPE_IBSS;
978                 break;
979         default:
980                 ret = -EOPNOTSUPP;
981                 goto out;
982         }
983
984         /* FIXME: what if conf->mac_addr changes? */
985
986 out:
987         mutex_unlock(&wl->mutex);
988         return ret;
989 }
990
991 static void wl1271_op_remove_interface(struct ieee80211_hw *hw,
992                                          struct ieee80211_if_init_conf *conf)
993 {
994         struct wl1271 *wl = hw->priv;
995
996         mutex_lock(&wl->mutex);
997         wl1271_debug(DEBUG_MAC80211, "mac80211 remove interface");
998         wl->vif = NULL;
999         mutex_unlock(&wl->mutex);
1000 }
1001
1002 #if 0
1003 static int wl1271_op_config_interface(struct ieee80211_hw *hw,
1004                                       struct ieee80211_vif *vif,
1005                                       struct ieee80211_if_conf *conf)
1006 {
1007         struct wl1271 *wl = hw->priv;
1008         struct sk_buff *beacon;
1009         int ret;
1010
1011         wl1271_debug(DEBUG_MAC80211, "mac80211 config_interface bssid %pM",
1012                      conf->bssid);
1013         wl1271_dump_ascii(DEBUG_MAC80211, "ssid: ", conf->ssid,
1014                           conf->ssid_len);
1015
1016         mutex_lock(&wl->mutex);
1017
1018         ret = wl1271_ps_elp_wakeup(wl, false);
1019         if (ret < 0)
1020                 goto out;
1021
1022         if (memcmp(wl->bssid, conf->bssid, ETH_ALEN)) {
1023                 wl1271_debug(DEBUG_MAC80211, "bssid changed");
1024
1025                 memcpy(wl->bssid, conf->bssid, ETH_ALEN);
1026
1027                 ret = wl1271_cmd_join(wl);
1028                 if (ret < 0)
1029                         goto out_sleep;
1030         }
1031
1032         ret = wl1271_cmd_build_null_data(wl);
1033         if (ret < 0)
1034                 goto out_sleep;
1035
1036         wl->ssid_len = conf->ssid_len;
1037         if (wl->ssid_len)
1038                 memcpy(wl->ssid, conf->ssid, wl->ssid_len);
1039
1040         if (conf->changed & IEEE80211_IFCC_BEACON) {
1041                 beacon = ieee80211_beacon_get(hw, vif);
1042                 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_BEACON,
1043                                               beacon->data, beacon->len);
1044
1045                 if (ret < 0) {
1046                         dev_kfree_skb(beacon);
1047                         goto out_sleep;
1048                 }
1049
1050                 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_PROBE_RESPONSE,
1051                                               beacon->data, beacon->len);
1052
1053                 dev_kfree_skb(beacon);
1054
1055                 if (ret < 0)
1056                         goto out_sleep;
1057         }
1058
1059 out_sleep:
1060         wl1271_ps_elp_sleep(wl);
1061
1062 out:
1063         mutex_unlock(&wl->mutex);
1064
1065         return ret;
1066 }
1067 #endif
1068
1069 static int wl1271_op_config(struct ieee80211_hw *hw, u32 changed)
1070 {
1071         struct wl1271 *wl = hw->priv;
1072         struct ieee80211_conf *conf = &hw->conf;
1073         int channel, ret = 0;
1074
1075         channel = ieee80211_frequency_to_channel(conf->channel->center_freq);
1076
1077         wl1271_debug(DEBUG_MAC80211, "mac80211 config ch %d psm %s power %d",
1078                      channel,
1079                      conf->flags & IEEE80211_CONF_PS ? "on" : "off",
1080                      conf->power_level);
1081
1082         mutex_lock(&wl->mutex);
1083
1084         wl->band = conf->channel->band;
1085
1086         ret = wl1271_ps_elp_wakeup(wl, false);
1087         if (ret < 0)
1088                 goto out;
1089
1090         if (channel != wl->channel) {
1091                 /*
1092                  * We assume that the stack will configure the right channel
1093                  * before associating, so we don't need to send a join
1094                  * command here.  We will join the right channel when the
1095                  * BSSID changes
1096                  */
1097                 wl->channel = channel;
1098         }
1099
1100         ret = wl1271_cmd_build_null_data(wl);
1101         if (ret < 0)
1102                 goto out_sleep;
1103
1104         if (conf->flags & IEEE80211_CONF_PS && !wl->psm_requested) {
1105                 wl1271_info("psm enabled");
1106
1107                 wl->psm_requested = true;
1108
1109                 /*
1110                  * We enter PSM only if we're already associated.
1111                  * If we're not, we'll enter it when joining an SSID,
1112                  * through the bss_info_changed() hook.
1113                  */
1114                 ret = wl1271_ps_set_mode(wl, STATION_POWER_SAVE_MODE);
1115         } else if (!(conf->flags & IEEE80211_CONF_PS) &&
1116                    wl->psm_requested) {
1117                 wl1271_info("psm disabled");
1118
1119                 wl->psm_requested = false;
1120
1121                 if (wl->psm)
1122                         ret = wl1271_ps_set_mode(wl, STATION_ACTIVE_MODE);
1123         }
1124
1125         if (conf->power_level != wl->power_level) {
1126                 ret = wl1271_acx_tx_power(wl, conf->power_level);
1127                 if (ret < 0)
1128                         goto out;
1129
1130                 wl->power_level = conf->power_level;
1131         }
1132
1133 out_sleep:
1134         wl1271_ps_elp_sleep(wl);
1135
1136 out:
1137         mutex_unlock(&wl->mutex);
1138
1139         return ret;
1140 }
1141
1142 static u64 wl1271_op_prepare_multicast(struct ieee80211_hw *hw, int mc_count,
1143                                        struct dev_addr_list *mc_list)
1144 {
1145         struct wl1271 *wl = hw->priv;
1146         struct wl1271_filter_params *fp;
1147         unsigned long flags;
1148         int i;
1149
1150         /*
1151          * FIXME: we should return a hash that will be passed to
1152          * configure_filter() instead of saving everything in the context.
1153          */
1154
1155         fp = kzalloc(sizeof(*fp), GFP_KERNEL);
1156         if (!fp) {
1157                 wl1271_error("Out of memory setting filters.");
1158                 return 0;
1159         }
1160
1161         /* update multicast filtering parameters */
1162         if (mc_count > ACX_MC_ADDRESS_GROUP_MAX) {
1163                 mc_count = 0;
1164                 fp->filters |= FIF_ALLMULTI;
1165         }
1166
1167         fp->mc_list_length = 0;
1168         for (i = 0; i < mc_count; i++) {
1169                 if (mc_list->da_addrlen == ETH_ALEN) {
1170                         memcpy(fp->mc_list[fp->mc_list_length],
1171                                mc_list->da_addr, ETH_ALEN);
1172                         fp->mc_list_length++;
1173                 } else
1174                         wl1271_warning("Unknown mc address length.");
1175         }
1176
1177         /* FIXME: We still need to set our filters properly */
1178
1179         spin_lock_irqsave(&wl->wl_lock, flags);
1180         kfree(wl->filter_params);
1181         wl->filter_params = fp;
1182         spin_unlock_irqrestore(&wl->wl_lock, flags);
1183
1184         return 1;
1185 }
1186
1187 static void wl1271_op_configure_filter(struct ieee80211_hw *hw,
1188                                        unsigned int changed,
1189                                        unsigned int *total, u64 multicast)
1190 {
1191         struct wl1271 *wl = hw->priv;
1192
1193         wl1271_debug(DEBUG_MAC80211, "mac80211 configure filter");
1194
1195         *total &= WL1271_SUPPORTED_FILTERS;
1196         changed &= WL1271_SUPPORTED_FILTERS;
1197
1198         if (!multicast)
1199                 return;
1200
1201         /*
1202          * FIXME: for now we are still using a workqueue for filter
1203          * configuration, but with the new mac80211, this is not needed,
1204          * since configure_filter can now sleep.  We now have
1205          * prepare_multicast, which needs to be atomic instead.
1206          */
1207
1208         /* store current filter config */
1209         wl->filter_params->filters = *total;
1210         wl->filter_params->changed = changed;
1211
1212         ieee80211_queue_work(wl->hw, &wl->filter_work);
1213 }
1214
1215 static int wl1271_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
1216                              struct ieee80211_vif *vif,
1217                              struct ieee80211_sta *sta,
1218                              struct ieee80211_key_conf *key_conf)
1219 {
1220         struct wl1271 *wl = hw->priv;
1221         const u8 *addr;
1222         int ret;
1223         u32 tx_seq_32 = 0;
1224         u16 tx_seq_16 = 0;
1225         u8 key_type;
1226
1227         static const u8 bcast_addr[ETH_ALEN] =
1228                 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
1229
1230         wl1271_debug(DEBUG_MAC80211, "mac80211 set key");
1231
1232         addr = sta ? sta->addr : bcast_addr;
1233
1234         wl1271_debug(DEBUG_CRYPT, "CMD: 0x%x", cmd);
1235         wl1271_dump(DEBUG_CRYPT, "ADDR: ", addr, ETH_ALEN);
1236         wl1271_debug(DEBUG_CRYPT, "Key: algo:0x%x, id:%d, len:%d flags 0x%x",
1237                      key_conf->alg, key_conf->keyidx,
1238                      key_conf->keylen, key_conf->flags);
1239         wl1271_dump(DEBUG_CRYPT, "KEY: ", key_conf->key, key_conf->keylen);
1240
1241         if (is_zero_ether_addr(addr)) {
1242                 /* We dont support TX only encryption */
1243                 ret = -EOPNOTSUPP;
1244                 goto out;
1245         }
1246
1247         mutex_lock(&wl->mutex);
1248
1249         ret = wl1271_ps_elp_wakeup(wl, false);
1250         if (ret < 0)
1251                 goto out_unlock;
1252
1253         switch (key_conf->alg) {
1254         case ALG_WEP:
1255                 key_type = KEY_WEP;
1256
1257                 key_conf->hw_key_idx = key_conf->keyidx;
1258                 break;
1259         case ALG_TKIP:
1260                 key_type = KEY_TKIP;
1261
1262                 key_conf->hw_key_idx = key_conf->keyidx;
1263                 tx_seq_32 = wl->tx_security_seq_32;
1264                 tx_seq_16 = wl->tx_security_seq_16;
1265                 break;
1266         case ALG_CCMP:
1267                 key_type = KEY_AES;
1268
1269                 key_conf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
1270                 tx_seq_32 = wl->tx_security_seq_32;
1271                 tx_seq_16 = wl->tx_security_seq_16;
1272                 break;
1273         default:
1274                 wl1271_error("Unknown key algo 0x%x", key_conf->alg);
1275
1276                 ret = -EOPNOTSUPP;
1277                 goto out_sleep;
1278         }
1279
1280         switch (cmd) {
1281         case SET_KEY:
1282                 ret = wl1271_cmd_set_key(wl, KEY_ADD_OR_REPLACE,
1283                                          key_conf->keyidx, key_type,
1284                                          key_conf->keylen, key_conf->key,
1285                                          addr, tx_seq_32, tx_seq_16);
1286                 if (ret < 0) {
1287                         wl1271_error("Could not add or replace key");
1288                         goto out_sleep;
1289                 }
1290                 break;
1291
1292         case DISABLE_KEY:
1293                 ret = wl1271_cmd_set_key(wl, KEY_REMOVE,
1294                                          key_conf->keyidx, key_type,
1295                                          key_conf->keylen, key_conf->key,
1296                                          addr, 0, 0);
1297                 if (ret < 0) {
1298                         wl1271_error("Could not remove key");
1299                         goto out_sleep;
1300                 }
1301                 break;
1302
1303         default:
1304                 wl1271_error("Unsupported key cmd 0x%x", cmd);
1305                 ret = -EOPNOTSUPP;
1306                 goto out_sleep;
1307
1308                 break;
1309         }
1310
1311 out_sleep:
1312         wl1271_ps_elp_sleep(wl);
1313
1314 out_unlock:
1315         mutex_unlock(&wl->mutex);
1316
1317 out:
1318         return ret;
1319 }
1320
1321 static int wl1271_op_hw_scan(struct ieee80211_hw *hw,
1322                              struct cfg80211_scan_request *req)
1323 {
1324         struct wl1271 *wl = hw->priv;
1325         int ret;
1326         u8 *ssid = NULL;
1327         size_t ssid_len = 0;
1328
1329         wl1271_debug(DEBUG_MAC80211, "mac80211 hw scan");
1330
1331         if (req->n_ssids) {
1332                 ssid = req->ssids[0].ssid;
1333                 ssid_len = req->ssids[0].ssid_len;
1334         }
1335
1336         mutex_lock(&wl->mutex);
1337
1338         ret = wl1271_ps_elp_wakeup(wl, false);
1339         if (ret < 0)
1340                 goto out;
1341
1342         ret = wl1271_cmd_scan(hw->priv, ssid, ssid_len, 1, 0, 13, 3);
1343
1344         wl1271_ps_elp_sleep(wl);
1345
1346 out:
1347         mutex_unlock(&wl->mutex);
1348
1349         return ret;
1350 }
1351
1352 static int wl1271_op_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
1353 {
1354         struct wl1271 *wl = hw->priv;
1355         int ret;
1356
1357         mutex_lock(&wl->mutex);
1358
1359         ret = wl1271_ps_elp_wakeup(wl, false);
1360         if (ret < 0)
1361                 goto out;
1362
1363         ret = wl1271_acx_rts_threshold(wl, (u16) value);
1364         if (ret < 0)
1365                 wl1271_warning("wl1271_op_set_rts_threshold failed: %d", ret);
1366
1367         wl1271_ps_elp_sleep(wl);
1368
1369 out:
1370         mutex_unlock(&wl->mutex);
1371
1372         return ret;
1373 }
1374
1375 static u32 wl1271_enabled_rates_get(struct wl1271 *wl, u64 basic_rate_set)
1376 {
1377         struct ieee80211_supported_band *band;
1378         u32 enabled_rates = 0;
1379         int bit;
1380
1381         band = wl->hw->wiphy->bands[wl->band];
1382         for (bit = 0; bit < band->n_bitrates; bit++) {
1383                 if (basic_rate_set & 0x1)
1384                         enabled_rates |= band->bitrates[bit].hw_value;
1385                 basic_rate_set >>= 1;
1386         }
1387
1388         return enabled_rates;
1389 }
1390
1391 static void wl1271_op_bss_info_changed(struct ieee80211_hw *hw,
1392                                        struct ieee80211_vif *vif,
1393                                        struct ieee80211_bss_conf *bss_conf,
1394                                        u32 changed)
1395 {
1396         enum wl1271_cmd_ps_mode mode;
1397         struct wl1271 *wl = hw->priv;
1398         int ret;
1399
1400         wl1271_debug(DEBUG_MAC80211, "mac80211 bss info changed");
1401
1402         mutex_lock(&wl->mutex);
1403
1404         ret = wl1271_ps_elp_wakeup(wl, false);
1405         if (ret < 0)
1406                 goto out;
1407
1408         if (changed & BSS_CHANGED_ASSOC) {
1409                 if (bss_conf->assoc) {
1410                         wl->aid = bss_conf->aid;
1411
1412                         /*
1413                          * with wl1271, we don't need to update the
1414                          * beacon_int and dtim_period, because the firmware
1415                          * updates it by itself when the first beacon is
1416                          * received after a join.
1417                          */
1418                         ret = wl1271_cmd_build_ps_poll(wl, wl->aid);
1419                         if (ret < 0)
1420                                 goto out_sleep;
1421
1422                         ret = wl1271_acx_aid(wl, wl->aid);
1423                         if (ret < 0)
1424                                 goto out_sleep;
1425
1426                         /* If we want to go in PSM but we're not there yet */
1427                         if (wl->psm_requested && !wl->psm) {
1428                                 mode = STATION_POWER_SAVE_MODE;
1429                                 ret = wl1271_ps_set_mode(wl, mode);
1430                                 if (ret < 0)
1431                                         goto out_sleep;
1432                         }
1433                 } else {
1434                         /* use defaults when not associated */
1435                         wl->basic_rate_set = WL1271_DEFAULT_BASIC_RATE_SET;
1436                         wl->aid = 0;
1437                 }
1438
1439         }
1440
1441         if (changed & BSS_CHANGED_ERP_SLOT) {
1442                 if (bss_conf->use_short_slot)
1443                         ret = wl1271_acx_slot(wl, SLOT_TIME_SHORT);
1444                 else
1445                         ret = wl1271_acx_slot(wl, SLOT_TIME_LONG);
1446                 if (ret < 0) {
1447                         wl1271_warning("Set slot time failed %d", ret);
1448                         goto out_sleep;
1449                 }
1450         }
1451
1452         if (changed & BSS_CHANGED_ERP_PREAMBLE) {
1453                 if (bss_conf->use_short_preamble)
1454                         wl1271_acx_set_preamble(wl, ACX_PREAMBLE_SHORT);
1455                 else
1456                         wl1271_acx_set_preamble(wl, ACX_PREAMBLE_LONG);
1457         }
1458
1459         if (changed & BSS_CHANGED_ERP_CTS_PROT) {
1460                 if (bss_conf->use_cts_prot)
1461                         ret = wl1271_acx_cts_protect(wl, CTSPROTECT_ENABLE);
1462                 else
1463                         ret = wl1271_acx_cts_protect(wl, CTSPROTECT_DISABLE);
1464                 if (ret < 0) {
1465                         wl1271_warning("Set ctsprotect failed %d", ret);
1466                         goto out_sleep;
1467                 }
1468         }
1469
1470         if (changed & BSS_CHANGED_BASIC_RATES) {
1471                 wl->basic_rate_set = wl1271_enabled_rates_get(
1472                         wl, bss_conf->basic_rates);
1473
1474                 ret = wl1271_acx_rate_policies(wl, wl->basic_rate_set);
1475                 if (ret < 0) {
1476                         wl1271_warning("Set rate policies failed %d", ret);
1477                         goto out_sleep;
1478                 }
1479         }
1480
1481 out_sleep:
1482         wl1271_ps_elp_sleep(wl);
1483
1484 out:
1485         mutex_unlock(&wl->mutex);
1486 }
1487
1488
1489 /* can't be const, mac80211 writes to this */
1490 static struct ieee80211_rate wl1271_rates[] = {
1491         { .bitrate = 10,
1492           .hw_value = CONF_HW_BIT_RATE_1MBPS,
1493           .hw_value_short = CONF_HW_BIT_RATE_1MBPS, },
1494         { .bitrate = 20,
1495           .hw_value = CONF_HW_BIT_RATE_2MBPS,
1496           .hw_value_short = CONF_HW_BIT_RATE_2MBPS,
1497           .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1498         { .bitrate = 55,
1499           .hw_value = CONF_HW_BIT_RATE_5_5MBPS,
1500           .hw_value_short = CONF_HW_BIT_RATE_5_5MBPS,
1501           .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1502         { .bitrate = 110,
1503           .hw_value = CONF_HW_BIT_RATE_11MBPS,
1504           .hw_value_short = CONF_HW_BIT_RATE_11MBPS,
1505           .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1506         { .bitrate = 60,
1507           .hw_value = CONF_HW_BIT_RATE_6MBPS,
1508           .hw_value_short = CONF_HW_BIT_RATE_6MBPS, },
1509         { .bitrate = 90,
1510           .hw_value = CONF_HW_BIT_RATE_9MBPS,
1511           .hw_value_short = CONF_HW_BIT_RATE_9MBPS, },
1512         { .bitrate = 120,
1513           .hw_value = CONF_HW_BIT_RATE_12MBPS,
1514           .hw_value_short = CONF_HW_BIT_RATE_12MBPS, },
1515         { .bitrate = 180,
1516           .hw_value = CONF_HW_BIT_RATE_18MBPS,
1517           .hw_value_short = CONF_HW_BIT_RATE_18MBPS, },
1518         { .bitrate = 240,
1519           .hw_value = CONF_HW_BIT_RATE_24MBPS,
1520           .hw_value_short = CONF_HW_BIT_RATE_24MBPS, },
1521         { .bitrate = 360,
1522          .hw_value = CONF_HW_BIT_RATE_36MBPS,
1523          .hw_value_short = CONF_HW_BIT_RATE_36MBPS, },
1524         { .bitrate = 480,
1525           .hw_value = CONF_HW_BIT_RATE_48MBPS,
1526           .hw_value_short = CONF_HW_BIT_RATE_48MBPS, },
1527         { .bitrate = 540,
1528           .hw_value = CONF_HW_BIT_RATE_54MBPS,
1529           .hw_value_short = CONF_HW_BIT_RATE_54MBPS, },
1530 };
1531
1532 /* can't be const, mac80211 writes to this */
1533 static struct ieee80211_channel wl1271_channels[] = {
1534         { .hw_value = 1, .center_freq = 2412},
1535         { .hw_value = 2, .center_freq = 2417},
1536         { .hw_value = 3, .center_freq = 2422},
1537         { .hw_value = 4, .center_freq = 2427},
1538         { .hw_value = 5, .center_freq = 2432},
1539         { .hw_value = 6, .center_freq = 2437},
1540         { .hw_value = 7, .center_freq = 2442},
1541         { .hw_value = 8, .center_freq = 2447},
1542         { .hw_value = 9, .center_freq = 2452},
1543         { .hw_value = 10, .center_freq = 2457},
1544         { .hw_value = 11, .center_freq = 2462},
1545         { .hw_value = 12, .center_freq = 2467},
1546         { .hw_value = 13, .center_freq = 2472},
1547 };
1548
1549 /* can't be const, mac80211 writes to this */
1550 static struct ieee80211_supported_band wl1271_band_2ghz = {
1551         .channels = wl1271_channels,
1552         .n_channels = ARRAY_SIZE(wl1271_channels),
1553         .bitrates = wl1271_rates,
1554         .n_bitrates = ARRAY_SIZE(wl1271_rates),
1555 };
1556
1557 static const struct ieee80211_ops wl1271_ops = {
1558         .start = wl1271_op_start,
1559         .stop = wl1271_op_stop,
1560         .add_interface = wl1271_op_add_interface,
1561         .remove_interface = wl1271_op_remove_interface,
1562         .config = wl1271_op_config,
1563 /*      .config_interface = wl1271_op_config_interface, */
1564         .prepare_multicast = wl1271_op_prepare_multicast,
1565         .configure_filter = wl1271_op_configure_filter,
1566         .tx = wl1271_op_tx,
1567         .set_key = wl1271_op_set_key,
1568         .hw_scan = wl1271_op_hw_scan,
1569         .bss_info_changed = wl1271_op_bss_info_changed,
1570         .set_rts_threshold = wl1271_op_set_rts_threshold,
1571 };
1572
1573 static int wl1271_register_hw(struct wl1271 *wl)
1574 {
1575         int ret;
1576
1577         if (wl->mac80211_registered)
1578                 return 0;
1579
1580         SET_IEEE80211_PERM_ADDR(wl->hw, wl->mac_addr);
1581
1582         ret = ieee80211_register_hw(wl->hw);
1583         if (ret < 0) {
1584                 wl1271_error("unable to register mac80211 hw: %d", ret);
1585                 return ret;
1586         }
1587
1588         wl->mac80211_registered = true;
1589
1590         wl1271_notice("loaded");
1591
1592         return 0;
1593 }
1594
1595 static int wl1271_init_ieee80211(struct wl1271 *wl)
1596 {
1597         /* The tx descriptor buffer and the TKIP space. */
1598         wl->hw->extra_tx_headroom = WL1271_TKIP_IV_SPACE +
1599                 sizeof(struct wl1271_tx_hw_descr);
1600
1601         /* unit us */
1602         /* FIXME: find a proper value */
1603         wl->hw->channel_change_time = 10000;
1604
1605         wl->hw->flags = IEEE80211_HW_SIGNAL_DBM |
1606                 IEEE80211_HW_NOISE_DBM |
1607                 IEEE80211_HW_BEACON_FILTER;
1608
1609         wl->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
1610         wl->hw->wiphy->max_scan_ssids = 1;
1611         wl->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &wl1271_band_2ghz;
1612
1613         SET_IEEE80211_DEV(wl->hw, &wl->spi->dev);
1614
1615         return 0;
1616 }
1617
1618 static void wl1271_device_release(struct device *dev)
1619 {
1620
1621 }
1622
1623 static struct platform_device wl1271_device = {
1624         .name           = "wl1271",
1625         .id             = -1,
1626
1627         /* device model insists to have a release function */
1628         .dev            = {
1629                 .release = wl1271_device_release,
1630         },
1631 };
1632
1633 #define WL1271_DEFAULT_CHANNEL 0
1634 static int __devinit wl1271_probe(struct spi_device *spi)
1635 {
1636         struct wl12xx_platform_data *pdata;
1637         struct ieee80211_hw *hw;
1638         struct wl1271 *wl;
1639         int ret, i;
1640         static const u8 nokia_oui[3] = {0x00, 0x1f, 0xdf};
1641
1642         pdata = spi->dev.platform_data;
1643         if (!pdata) {
1644                 wl1271_error("no platform data");
1645                 return -ENODEV;
1646         }
1647
1648         hw = ieee80211_alloc_hw(sizeof(*wl), &wl1271_ops);
1649         if (!hw) {
1650                 wl1271_error("could not alloc ieee80211_hw");
1651                 return -ENOMEM;
1652         }
1653
1654         wl = hw->priv;
1655         memset(wl, 0, sizeof(*wl));
1656
1657         wl->hw = hw;
1658         dev_set_drvdata(&spi->dev, wl);
1659         wl->spi = spi;
1660
1661         skb_queue_head_init(&wl->tx_queue);
1662
1663         INIT_WORK(&wl->filter_work, wl1271_filter_work);
1664         INIT_DELAYED_WORK(&wl->elp_work, wl1271_elp_work);
1665         wl->channel = WL1271_DEFAULT_CHANNEL;
1666         wl->scanning = false;
1667         wl->default_key = 0;
1668         wl->rx_counter = 0;
1669         wl->rx_config = WL1271_DEFAULT_RX_CONFIG;
1670         wl->rx_filter = WL1271_DEFAULT_RX_FILTER;
1671         wl->elp = false;
1672         wl->psm = 0;
1673         wl->psm_requested = false;
1674         wl->tx_queue_stopped = false;
1675         wl->power_level = WL1271_DEFAULT_POWER_LEVEL;
1676         wl->basic_rate_set = WL1271_DEFAULT_BASIC_RATE_SET;
1677         wl->band = IEEE80211_BAND_2GHZ;
1678         wl->vif = NULL;
1679         wl->joined = false;
1680
1681         for (i = 0; i < ACX_TX_DESCRIPTORS; i++)
1682                 wl->tx_frames[i] = NULL;
1683
1684         spin_lock_init(&wl->wl_lock);
1685
1686         /*
1687          * In case our MAC address is not correctly set,
1688          * we use a random but Nokia MAC.
1689          */
1690         memcpy(wl->mac_addr, nokia_oui, 3);
1691         get_random_bytes(wl->mac_addr + 3, 3);
1692
1693         wl->state = WL1271_STATE_OFF;
1694         mutex_init(&wl->mutex);
1695
1696         wl->rx_descriptor = kmalloc(sizeof(*wl->rx_descriptor), GFP_KERNEL);
1697         if (!wl->rx_descriptor) {
1698                 wl1271_error("could not allocate memory for rx descriptor");
1699                 ret = -ENOMEM;
1700                 goto out_free;
1701         }
1702
1703         /* This is the only SPI value that we need to set here, the rest
1704          * comes from the board-peripherals file */
1705         spi->bits_per_word = 32;
1706
1707         ret = spi_setup(spi);
1708         if (ret < 0) {
1709                 wl1271_error("spi_setup failed");
1710                 goto out_free;
1711         }
1712
1713         wl->set_power = pdata->set_power;
1714         if (!wl->set_power) {
1715                 wl1271_error("set power function missing in platform data");
1716                 ret = -ENODEV;
1717                 goto out_free;
1718         }
1719
1720         wl->irq = spi->irq;
1721         if (wl->irq < 0) {
1722                 wl1271_error("irq missing in platform data");
1723                 ret = -ENODEV;
1724                 goto out_free;
1725         }
1726
1727         ret = request_irq(wl->irq, wl1271_irq, 0, DRIVER_NAME, wl);
1728         if (ret < 0) {
1729                 wl1271_error("request_irq() failed: %d", ret);
1730                 goto out_free;
1731         }
1732
1733         set_irq_type(wl->irq, IRQ_TYPE_EDGE_RISING);
1734
1735         disable_irq(wl->irq);
1736
1737         ret = platform_device_register(&wl1271_device);
1738         if (ret) {
1739                 wl1271_error("couldn't register platform device");
1740                 goto out_irq;
1741         }
1742         dev_set_drvdata(&wl1271_device.dev, wl);
1743
1744         /* Apply default driver configuration. */
1745         wl1271_conf_init(wl);
1746
1747         ret = wl1271_init_ieee80211(wl);
1748         if (ret)
1749                 goto out_platform;
1750
1751         ret = wl1271_register_hw(wl);
1752         if (ret)
1753                 goto out_platform;
1754
1755         wl1271_debugfs_init(wl);
1756
1757         wl1271_notice("initialized");
1758
1759         return 0;
1760
1761  out_platform:
1762         platform_device_unregister(&wl1271_device);
1763
1764  out_irq:
1765         free_irq(wl->irq, wl);
1766
1767  out_free:
1768         kfree(wl->rx_descriptor);
1769         wl->rx_descriptor = NULL;
1770
1771         ieee80211_free_hw(hw);
1772
1773         return ret;
1774 }
1775
1776 static int __devexit wl1271_remove(struct spi_device *spi)
1777 {
1778         struct wl1271 *wl = dev_get_drvdata(&spi->dev);
1779
1780         ieee80211_unregister_hw(wl->hw);
1781
1782         wl1271_debugfs_exit(wl);
1783         platform_device_unregister(&wl1271_device);
1784         free_irq(wl->irq, wl);
1785         kfree(wl->target_mem_map);
1786         vfree(wl->fw);
1787         wl->fw = NULL;
1788         kfree(wl->nvs);
1789         wl->nvs = NULL;
1790
1791         kfree(wl->rx_descriptor);
1792         wl->rx_descriptor = NULL;
1793
1794         kfree(wl->fw_status);
1795         kfree(wl->tx_res_if);
1796
1797         ieee80211_free_hw(wl->hw);
1798
1799         return 0;
1800 }
1801
1802
1803 static struct spi_driver wl1271_spi_driver = {
1804         .driver = {
1805                 .name           = "wl1271",
1806                 .bus            = &spi_bus_type,
1807                 .owner          = THIS_MODULE,
1808         },
1809
1810         .probe          = wl1271_probe,
1811         .remove         = __devexit_p(wl1271_remove),
1812 };
1813
1814 static int __init wl1271_init(void)
1815 {
1816         int ret;
1817
1818         ret = spi_register_driver(&wl1271_spi_driver);
1819         if (ret < 0) {
1820                 wl1271_error("failed to register spi driver: %d", ret);
1821                 goto out;
1822         }
1823
1824 out:
1825         return ret;
1826 }
1827
1828 static void __exit wl1271_exit(void)
1829 {
1830         spi_unregister_driver(&wl1271_spi_driver);
1831
1832         wl1271_notice("unloaded");
1833 }
1834
1835 module_init(wl1271_init);
1836 module_exit(wl1271_exit);
1837
1838 MODULE_LICENSE("GPL");
1839 MODULE_AUTHOR("Luciano Coelho <luciano.coelho@nokia.com>");
1840 MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");