Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[cascardo/linux.git] / drivers / net / wireless / ath / ath9k / htc_drv_init.c
1 /*
2  * Copyright (c) 2010-2011 Atheros Communications Inc.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
19 #include "htc.h"
20
21 MODULE_AUTHOR("Atheros Communications");
22 MODULE_LICENSE("Dual BSD/GPL");
23 MODULE_DESCRIPTION("Atheros driver 802.11n HTC based wireless devices");
24
25 static unsigned int ath9k_debug = ATH_DBG_DEFAULT;
26 module_param_named(debug, ath9k_debug, uint, 0);
27 MODULE_PARM_DESC(debug, "Debugging mask");
28
29 int htc_modparam_nohwcrypt;
30 module_param_named(nohwcrypt, htc_modparam_nohwcrypt, int, 0444);
31 MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption");
32
33 static int ath9k_htc_btcoex_enable;
34 module_param_named(btcoex_enable, ath9k_htc_btcoex_enable, int, 0444);
35 MODULE_PARM_DESC(btcoex_enable, "Enable wifi-BT coexistence");
36
37 static int ath9k_ps_enable;
38 module_param_named(ps_enable, ath9k_ps_enable, int, 0444);
39 MODULE_PARM_DESC(ps_enable, "Enable WLAN PowerSave");
40
41 #ifdef CONFIG_MAC80211_LEDS
42 static const struct ieee80211_tpt_blink ath9k_htc_tpt_blink[] = {
43         { .throughput = 0 * 1024, .blink_time = 334 },
44         { .throughput = 1 * 1024, .blink_time = 260 },
45         { .throughput = 5 * 1024, .blink_time = 220 },
46         { .throughput = 10 * 1024, .blink_time = 190 },
47         { .throughput = 20 * 1024, .blink_time = 170 },
48         { .throughput = 50 * 1024, .blink_time = 150 },
49         { .throughput = 70 * 1024, .blink_time = 130 },
50         { .throughput = 100 * 1024, .blink_time = 110 },
51         { .throughput = 200 * 1024, .blink_time = 80 },
52         { .throughput = 300 * 1024, .blink_time = 50 },
53 };
54 #endif
55
56 static void ath9k_htc_op_ps_wakeup(struct ath_common *common)
57 {
58         ath9k_htc_ps_wakeup((struct ath9k_htc_priv *) common->priv);
59 }
60
61 static void ath9k_htc_op_ps_restore(struct ath_common *common)
62 {
63         ath9k_htc_ps_restore((struct ath9k_htc_priv *) common->priv);
64 }
65
66 static struct ath_ps_ops ath9k_htc_ps_ops = {
67         .wakeup = ath9k_htc_op_ps_wakeup,
68         .restore = ath9k_htc_op_ps_restore,
69 };
70
71 static int ath9k_htc_wait_for_target(struct ath9k_htc_priv *priv)
72 {
73         int time_left;
74
75         if (atomic_read(&priv->htc->tgt_ready) > 0) {
76                 atomic_dec(&priv->htc->tgt_ready);
77                 return 0;
78         }
79
80         /* Firmware can take up to 50ms to get ready, to be safe use 1 second */
81         time_left = wait_for_completion_timeout(&priv->htc->target_wait, HZ);
82         if (!time_left) {
83                 dev_err(priv->dev, "ath9k_htc: Target is unresponsive\n");
84                 return -ETIMEDOUT;
85         }
86
87         atomic_dec(&priv->htc->tgt_ready);
88
89         return 0;
90 }
91
92 static void ath9k_deinit_priv(struct ath9k_htc_priv *priv)
93 {
94         ath9k_hw_deinit(priv->ah);
95         kfree(priv->ah);
96         priv->ah = NULL;
97 }
98
99 static void ath9k_deinit_device(struct ath9k_htc_priv *priv)
100 {
101         struct ieee80211_hw *hw = priv->hw;
102
103         wiphy_rfkill_stop_polling(hw->wiphy);
104         ath9k_deinit_leds(priv);
105         ath9k_htc_deinit_debug(priv);
106         ieee80211_unregister_hw(hw);
107         ath9k_rx_cleanup(priv);
108         ath9k_tx_cleanup(priv);
109         ath9k_deinit_priv(priv);
110 }
111
112 static inline int ath9k_htc_connect_svc(struct ath9k_htc_priv *priv,
113                                         u16 service_id,
114                                         void (*tx) (void *,
115                                                     struct sk_buff *,
116                                                     enum htc_endpoint_id,
117                                                     bool txok),
118                                         enum htc_endpoint_id *ep_id)
119 {
120         struct htc_service_connreq req;
121
122         memset(&req, 0, sizeof(struct htc_service_connreq));
123
124         req.service_id = service_id;
125         req.ep_callbacks.priv = priv;
126         req.ep_callbacks.rx = ath9k_htc_rxep;
127         req.ep_callbacks.tx = tx;
128
129         return htc_connect_service(priv->htc, &req, ep_id);
130 }
131
132 static int ath9k_init_htc_services(struct ath9k_htc_priv *priv, u16 devid,
133                                    u32 drv_info)
134 {
135         int ret;
136
137         /* WMI CMD*/
138         ret = ath9k_wmi_connect(priv->htc, priv->wmi, &priv->wmi_cmd_ep);
139         if (ret)
140                 goto err;
141
142         /* Beacon */
143         ret = ath9k_htc_connect_svc(priv, WMI_BEACON_SVC, ath9k_htc_beaconep,
144                                     &priv->beacon_ep);
145         if (ret)
146                 goto err;
147
148         /* CAB */
149         ret = ath9k_htc_connect_svc(priv, WMI_CAB_SVC, ath9k_htc_txep,
150                                     &priv->cab_ep);
151         if (ret)
152                 goto err;
153
154
155         /* UAPSD */
156         ret = ath9k_htc_connect_svc(priv, WMI_UAPSD_SVC, ath9k_htc_txep,
157                                     &priv->uapsd_ep);
158         if (ret)
159                 goto err;
160
161         /* MGMT */
162         ret = ath9k_htc_connect_svc(priv, WMI_MGMT_SVC, ath9k_htc_txep,
163                                     &priv->mgmt_ep);
164         if (ret)
165                 goto err;
166
167         /* DATA BE */
168         ret = ath9k_htc_connect_svc(priv, WMI_DATA_BE_SVC, ath9k_htc_txep,
169                                     &priv->data_be_ep);
170         if (ret)
171                 goto err;
172
173         /* DATA BK */
174         ret = ath9k_htc_connect_svc(priv, WMI_DATA_BK_SVC, ath9k_htc_txep,
175                                     &priv->data_bk_ep);
176         if (ret)
177                 goto err;
178
179         /* DATA VI */
180         ret = ath9k_htc_connect_svc(priv, WMI_DATA_VI_SVC, ath9k_htc_txep,
181                                     &priv->data_vi_ep);
182         if (ret)
183                 goto err;
184
185         /* DATA VO */
186         ret = ath9k_htc_connect_svc(priv, WMI_DATA_VO_SVC, ath9k_htc_txep,
187                                     &priv->data_vo_ep);
188         if (ret)
189                 goto err;
190
191         /*
192          * Setup required credits before initializing HTC.
193          * This is a bit hacky, but, since queuing is done in
194          * the HIF layer, shouldn't matter much.
195          */
196
197         if (IS_AR7010_DEVICE(drv_info))
198                 priv->htc->credits = 45;
199         else
200                 priv->htc->credits = 33;
201
202         ret = htc_init(priv->htc);
203         if (ret)
204                 goto err;
205
206         dev_info(priv->dev, "ath9k_htc: HTC initialized with %d credits\n",
207                  priv->htc->credits);
208
209         return 0;
210
211 err:
212         dev_err(priv->dev, "ath9k_htc: Unable to initialize HTC services\n");
213         return ret;
214 }
215
216 static void ath9k_reg_notifier(struct wiphy *wiphy,
217                                struct regulatory_request *request)
218 {
219         struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
220         struct ath9k_htc_priv *priv = hw->priv;
221
222         ath_reg_notifier_apply(wiphy, request,
223                                ath9k_hw_regulatory(priv->ah));
224 }
225
226 static unsigned int ath9k_regread(void *hw_priv, u32 reg_offset)
227 {
228         struct ath_hw *ah = (struct ath_hw *) hw_priv;
229         struct ath_common *common = ath9k_hw_common(ah);
230         struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;
231         __be32 val, reg = cpu_to_be32(reg_offset);
232         int r;
233
234         r = ath9k_wmi_cmd(priv->wmi, WMI_REG_READ_CMDID,
235                           (u8 *) &reg, sizeof(reg),
236                           (u8 *) &val, sizeof(val),
237                           100);
238         if (unlikely(r)) {
239                 ath_dbg(common, WMI, "REGISTER READ FAILED: (0x%04x, %d)\n",
240                         reg_offset, r);
241                 return -EIO;
242         }
243
244         return be32_to_cpu(val);
245 }
246
247 static void ath9k_multi_regread(void *hw_priv, u32 *addr,
248                                 u32 *val, u16 count)
249 {
250         struct ath_hw *ah = (struct ath_hw *) hw_priv;
251         struct ath_common *common = ath9k_hw_common(ah);
252         struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;
253         __be32 tmpaddr[8];
254         __be32 tmpval[8];
255         int i, ret;
256
257        for (i = 0; i < count; i++) {
258                tmpaddr[i] = cpu_to_be32(addr[i]);
259        }
260
261        ret = ath9k_wmi_cmd(priv->wmi, WMI_REG_READ_CMDID,
262                            (u8 *)tmpaddr , sizeof(u32) * count,
263                            (u8 *)tmpval, sizeof(u32) * count,
264                            100);
265         if (unlikely(ret)) {
266                 ath_dbg(common, WMI,
267                         "Multiple REGISTER READ FAILED (count: %d)\n", count);
268         }
269
270        for (i = 0; i < count; i++) {
271                val[i] = be32_to_cpu(tmpval[i]);
272        }
273 }
274
275 static void ath9k_regwrite_multi(struct ath_common *common)
276 {
277         struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;
278         u32 rsp_status;
279         int r;
280
281         r = ath9k_wmi_cmd(priv->wmi, WMI_REG_WRITE_CMDID,
282                           (u8 *) &priv->wmi->multi_write,
283                           sizeof(struct register_write) * priv->wmi->multi_write_idx,
284                           (u8 *) &rsp_status, sizeof(rsp_status),
285                           100);
286         if (unlikely(r)) {
287                 ath_dbg(common, WMI,
288                         "REGISTER WRITE FAILED, multi len: %d\n",
289                         priv->wmi->multi_write_idx);
290         }
291         priv->wmi->multi_write_idx = 0;
292 }
293
294 static void ath9k_regwrite_single(void *hw_priv, u32 val, u32 reg_offset)
295 {
296         struct ath_hw *ah = (struct ath_hw *) hw_priv;
297         struct ath_common *common = ath9k_hw_common(ah);
298         struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;
299         const __be32 buf[2] = {
300                 cpu_to_be32(reg_offset),
301                 cpu_to_be32(val),
302         };
303         int r;
304
305         r = ath9k_wmi_cmd(priv->wmi, WMI_REG_WRITE_CMDID,
306                           (u8 *) &buf, sizeof(buf),
307                           (u8 *) &val, sizeof(val),
308                           100);
309         if (unlikely(r)) {
310                 ath_dbg(common, WMI, "REGISTER WRITE FAILED:(0x%04x, %d)\n",
311                         reg_offset, r);
312         }
313 }
314
315 static void ath9k_regwrite_buffer(void *hw_priv, u32 val, u32 reg_offset)
316 {
317         struct ath_hw *ah = (struct ath_hw *) hw_priv;
318         struct ath_common *common = ath9k_hw_common(ah);
319         struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;
320
321         mutex_lock(&priv->wmi->multi_write_mutex);
322
323         /* Store the register/value */
324         priv->wmi->multi_write[priv->wmi->multi_write_idx].reg =
325                 cpu_to_be32(reg_offset);
326         priv->wmi->multi_write[priv->wmi->multi_write_idx].val =
327                 cpu_to_be32(val);
328
329         priv->wmi->multi_write_idx++;
330
331         /* If the buffer is full, send it out. */
332         if (priv->wmi->multi_write_idx == MAX_CMD_NUMBER)
333                 ath9k_regwrite_multi(common);
334
335         mutex_unlock(&priv->wmi->multi_write_mutex);
336 }
337
338 static void ath9k_regwrite(void *hw_priv, u32 val, u32 reg_offset)
339 {
340         struct ath_hw *ah = (struct ath_hw *) hw_priv;
341         struct ath_common *common = ath9k_hw_common(ah);
342         struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;
343
344         if (atomic_read(&priv->wmi->mwrite_cnt))
345                 ath9k_regwrite_buffer(hw_priv, val, reg_offset);
346         else
347                 ath9k_regwrite_single(hw_priv, val, reg_offset);
348 }
349
350 static void ath9k_enable_regwrite_buffer(void *hw_priv)
351 {
352         struct ath_hw *ah = (struct ath_hw *) hw_priv;
353         struct ath_common *common = ath9k_hw_common(ah);
354         struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;
355
356         atomic_inc(&priv->wmi->mwrite_cnt);
357 }
358
359 static void ath9k_regwrite_flush(void *hw_priv)
360 {
361         struct ath_hw *ah = (struct ath_hw *) hw_priv;
362         struct ath_common *common = ath9k_hw_common(ah);
363         struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;
364
365         atomic_dec(&priv->wmi->mwrite_cnt);
366
367         mutex_lock(&priv->wmi->multi_write_mutex);
368
369         if (priv->wmi->multi_write_idx)
370                 ath9k_regwrite_multi(common);
371
372         mutex_unlock(&priv->wmi->multi_write_mutex);
373 }
374
375 static u32 ath9k_reg_rmw(void *hw_priv, u32 reg_offset, u32 set, u32 clr)
376 {
377         u32 val;
378
379         val = ath9k_regread(hw_priv, reg_offset);
380         val &= ~clr;
381         val |= set;
382         ath9k_regwrite(hw_priv, val, reg_offset);
383         return val;
384 }
385
386 static void ath_usb_read_cachesize(struct ath_common *common, int *csz)
387 {
388         *csz = L1_CACHE_BYTES >> 2;
389 }
390
391 static bool ath_usb_eeprom_read(struct ath_common *common, u32 off, u16 *data)
392 {
393         struct ath_hw *ah = (struct ath_hw *) common->ah;
394
395         (void)REG_READ(ah, AR5416_EEPROM_OFFSET + (off << AR5416_EEPROM_S));
396
397         if (!ath9k_hw_wait(ah,
398                            AR_EEPROM_STATUS_DATA,
399                            AR_EEPROM_STATUS_DATA_BUSY |
400                            AR_EEPROM_STATUS_DATA_PROT_ACCESS, 0,
401                            AH_WAIT_TIMEOUT))
402                 return false;
403
404         *data = MS(REG_READ(ah, AR_EEPROM_STATUS_DATA),
405                    AR_EEPROM_STATUS_DATA_VAL);
406
407         return true;
408 }
409
410 static const struct ath_bus_ops ath9k_usb_bus_ops = {
411         .ath_bus_type = ATH_USB,
412         .read_cachesize = ath_usb_read_cachesize,
413         .eeprom_read = ath_usb_eeprom_read,
414 };
415
416 static int ath9k_init_queues(struct ath9k_htc_priv *priv)
417 {
418         struct ath_common *common = ath9k_hw_common(priv->ah);
419         int i;
420
421         for (i = 0; i < ARRAY_SIZE(priv->hwq_map); i++)
422                 priv->hwq_map[i] = -1;
423
424         priv->beacon.beaconq = ath9k_hw_beaconq_setup(priv->ah);
425         if (priv->beacon.beaconq == -1) {
426                 ath_err(common, "Unable to setup BEACON xmit queue\n");
427                 goto err;
428         }
429
430         priv->cabq = ath9k_htc_cabq_setup(priv);
431         if (priv->cabq == -1) {
432                 ath_err(common, "Unable to setup CAB xmit queue\n");
433                 goto err;
434         }
435
436         if (!ath9k_htc_txq_setup(priv, IEEE80211_AC_BE)) {
437                 ath_err(common, "Unable to setup xmit queue for BE traffic\n");
438                 goto err;
439         }
440
441         if (!ath9k_htc_txq_setup(priv, IEEE80211_AC_BK)) {
442                 ath_err(common, "Unable to setup xmit queue for BK traffic\n");
443                 goto err;
444         }
445         if (!ath9k_htc_txq_setup(priv, IEEE80211_AC_VI)) {
446                 ath_err(common, "Unable to setup xmit queue for VI traffic\n");
447                 goto err;
448         }
449         if (!ath9k_htc_txq_setup(priv, IEEE80211_AC_VO)) {
450                 ath_err(common, "Unable to setup xmit queue for VO traffic\n");
451                 goto err;
452         }
453
454         return 0;
455
456 err:
457         return -EINVAL;
458 }
459
460 static void ath9k_init_misc(struct ath9k_htc_priv *priv)
461 {
462         struct ath_common *common = ath9k_hw_common(priv->ah);
463
464         memcpy(common->bssidmask, ath_bcast_mac, ETH_ALEN);
465
466         common->last_rssi = ATH_RSSI_DUMMY_MARKER;
467         priv->ah->opmode = NL80211_IFTYPE_STATION;
468
469         priv->spec_priv.ah = priv->ah;
470         priv->spec_priv.spec_config.enabled = 0;
471         priv->spec_priv.spec_config.short_repeat = false;
472         priv->spec_priv.spec_config.count = 8;
473         priv->spec_priv.spec_config.endless = false;
474         priv->spec_priv.spec_config.period = 0x12;
475         priv->spec_priv.spec_config.fft_period = 0x02;
476 }
477
478 static int ath9k_init_priv(struct ath9k_htc_priv *priv,
479                            u16 devid, char *product,
480                            u32 drv_info)
481 {
482         struct ath_hw *ah = NULL;
483         struct ath_common *common;
484         int i, ret = 0, csz = 0;
485
486         ah = kzalloc(sizeof(struct ath_hw), GFP_KERNEL);
487         if (!ah)
488                 return -ENOMEM;
489
490         ah->dev = priv->dev;
491         ah->hw = priv->hw;
492         ah->hw_version.devid = devid;
493         ah->hw_version.usbdev = drv_info;
494         ah->ah_flags |= AH_USE_EEPROM;
495         ah->reg_ops.read = ath9k_regread;
496         ah->reg_ops.multi_read = ath9k_multi_regread;
497         ah->reg_ops.write = ath9k_regwrite;
498         ah->reg_ops.enable_write_buffer = ath9k_enable_regwrite_buffer;
499         ah->reg_ops.write_flush = ath9k_regwrite_flush;
500         ah->reg_ops.rmw = ath9k_reg_rmw;
501         priv->ah = ah;
502
503         common = ath9k_hw_common(ah);
504         common->ops = &ah->reg_ops;
505         common->ps_ops = &ath9k_htc_ps_ops;
506         common->bus_ops = &ath9k_usb_bus_ops;
507         common->ah = ah;
508         common->hw = priv->hw;
509         common->priv = priv;
510         common->debug_mask = ath9k_debug;
511         common->btcoex_enabled = ath9k_htc_btcoex_enable == 1;
512         set_bit(ATH_OP_INVALID, &common->op_flags);
513
514         spin_lock_init(&priv->beacon_lock);
515         spin_lock_init(&priv->tx.tx_lock);
516         mutex_init(&priv->mutex);
517         mutex_init(&priv->htc_pm_lock);
518         tasklet_init(&priv->rx_tasklet, ath9k_rx_tasklet,
519                      (unsigned long)priv);
520         tasklet_init(&priv->tx_failed_tasklet, ath9k_tx_failed_tasklet,
521                      (unsigned long)priv);
522         INIT_DELAYED_WORK(&priv->ani_work, ath9k_htc_ani_work);
523         INIT_WORK(&priv->ps_work, ath9k_ps_work);
524         INIT_WORK(&priv->fatal_work, ath9k_fatal_work);
525         setup_timer(&priv->tx.cleanup_timer, ath9k_htc_tx_cleanup_timer,
526                     (unsigned long)priv);
527
528         /*
529          * Cache line size is used to size and align various
530          * structures used to communicate with the hardware.
531          */
532         ath_read_cachesize(common, &csz);
533         common->cachelsz = csz << 2; /* convert to bytes */
534
535         ret = ath9k_hw_init(ah);
536         if (ret) {
537                 ath_err(common,
538                         "Unable to initialize hardware; initialization status: %d\n",
539                         ret);
540                 goto err_hw;
541         }
542
543         ret = ath9k_init_queues(priv);
544         if (ret)
545                 goto err_queues;
546
547         for (i = 0; i < ATH9K_HTC_MAX_BCN_VIF; i++)
548                 priv->beacon.bslot[i] = NULL;
549         priv->beacon.slottime = ATH9K_SLOT_TIME_9;
550
551         ath9k_cmn_init_channels_rates(common);
552         ath9k_cmn_init_crypto(ah);
553         ath9k_init_misc(priv);
554         ath9k_htc_init_btcoex(priv, product);
555
556         return 0;
557
558 err_queues:
559         ath9k_hw_deinit(ah);
560 err_hw:
561
562         kfree(ah);
563         priv->ah = NULL;
564
565         return ret;
566 }
567
568 static const struct ieee80211_iface_limit if_limits[] = {
569         { .max = 2,     .types = BIT(NL80211_IFTYPE_STATION) |
570                                  BIT(NL80211_IFTYPE_P2P_CLIENT) },
571         { .max = 2,     .types = BIT(NL80211_IFTYPE_AP) |
572 #ifdef CONFIG_MAC80211_MESH
573                                  BIT(NL80211_IFTYPE_MESH_POINT) |
574 #endif
575                                  BIT(NL80211_IFTYPE_P2P_GO) },
576 };
577
578 static const struct ieee80211_iface_combination if_comb = {
579         .limits = if_limits,
580         .n_limits = ARRAY_SIZE(if_limits),
581         .max_interfaces = 2,
582         .num_different_channels = 1,
583 };
584
585 static void ath9k_set_hw_capab(struct ath9k_htc_priv *priv,
586                                struct ieee80211_hw *hw)
587 {
588         struct ath_hw *ah = priv->ah;
589         struct ath_common *common = ath9k_hw_common(priv->ah);
590         struct base_eep_header *pBase;
591
592         hw->flags = IEEE80211_HW_SIGNAL_DBM |
593                 IEEE80211_HW_AMPDU_AGGREGATION |
594                 IEEE80211_HW_SPECTRUM_MGMT |
595                 IEEE80211_HW_HAS_RATE_CONTROL |
596                 IEEE80211_HW_RX_INCLUDES_FCS |
597                 IEEE80211_HW_PS_NULLFUNC_STACK |
598                 IEEE80211_HW_REPORTS_TX_ACK_STATUS |
599                 IEEE80211_HW_MFP_CAPABLE |
600                 IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING;
601
602         if (ath9k_ps_enable)
603                 hw->flags |= IEEE80211_HW_SUPPORTS_PS;
604
605         hw->wiphy->interface_modes =
606                 BIT(NL80211_IFTYPE_STATION) |
607                 BIT(NL80211_IFTYPE_ADHOC) |
608                 BIT(NL80211_IFTYPE_AP) |
609                 BIT(NL80211_IFTYPE_P2P_GO) |
610                 BIT(NL80211_IFTYPE_P2P_CLIENT) |
611                 BIT(NL80211_IFTYPE_MESH_POINT);
612
613         hw->wiphy->iface_combinations = &if_comb;
614         hw->wiphy->n_iface_combinations = 1;
615
616         hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
617
618         hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN |
619                             WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
620
621         hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS;
622
623         hw->queues = 4;
624         hw->max_listen_interval = 1;
625
626         hw->vif_data_size = sizeof(struct ath9k_htc_vif);
627         hw->sta_data_size = sizeof(struct ath9k_htc_sta);
628
629         /* tx_frame_hdr is larger than tx_mgmt_hdr anyway */
630         hw->extra_tx_headroom = sizeof(struct tx_frame_hdr) +
631                 sizeof(struct htc_frame_hdr) + 4;
632
633         if (priv->ah->caps.hw_caps & ATH9K_HW_CAP_2GHZ)
634                 hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
635                         &common->sbands[IEEE80211_BAND_2GHZ];
636         if (priv->ah->caps.hw_caps & ATH9K_HW_CAP_5GHZ)
637                 hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
638                         &common->sbands[IEEE80211_BAND_5GHZ];
639
640         ath9k_cmn_reload_chainmask(ah);
641
642         pBase = ath9k_htc_get_eeprom_base(priv);
643         if (pBase) {
644                 hw->wiphy->available_antennas_rx = pBase->rxMask;
645                 hw->wiphy->available_antennas_tx = pBase->txMask;
646         }
647
648         SET_IEEE80211_PERM_ADDR(hw, common->macaddr);
649 }
650
651 static int ath9k_init_firmware_version(struct ath9k_htc_priv *priv)
652 {
653         struct ieee80211_hw *hw = priv->hw;
654         struct wmi_fw_version cmd_rsp;
655         int ret;
656
657         memset(&cmd_rsp, 0, sizeof(cmd_rsp));
658
659         WMI_CMD(WMI_GET_FW_VERSION);
660         if (ret)
661                 return -EINVAL;
662
663         priv->fw_version_major = be16_to_cpu(cmd_rsp.major);
664         priv->fw_version_minor = be16_to_cpu(cmd_rsp.minor);
665
666         snprintf(hw->wiphy->fw_version, sizeof(hw->wiphy->fw_version), "%d.%d",
667                  priv->fw_version_major,
668                  priv->fw_version_minor);
669
670         dev_info(priv->dev, "ath9k_htc: FW Version: %d.%d\n",
671                  priv->fw_version_major,
672                  priv->fw_version_minor);
673
674         /*
675          * Check if the available FW matches the driver's
676          * required version.
677          */
678         if (priv->fw_version_major != MAJOR_VERSION_REQ ||
679             priv->fw_version_minor < MINOR_VERSION_REQ) {
680                 dev_err(priv->dev, "ath9k_htc: Please upgrade to FW version %d.%d\n",
681                         MAJOR_VERSION_REQ, MINOR_VERSION_REQ);
682                 return -EINVAL;
683         }
684
685         return 0;
686 }
687
688 static int ath9k_init_device(struct ath9k_htc_priv *priv,
689                              u16 devid, char *product, u32 drv_info)
690 {
691         struct ieee80211_hw *hw = priv->hw;
692         struct ath_common *common;
693         struct ath_hw *ah;
694         int error = 0;
695         struct ath_regulatory *reg;
696         char hw_name[64];
697
698         /* Bring up device */
699         error = ath9k_init_priv(priv, devid, product, drv_info);
700         if (error != 0)
701                 goto err_init;
702
703         ah = priv->ah;
704         common = ath9k_hw_common(ah);
705         ath9k_set_hw_capab(priv, hw);
706
707         error = ath9k_init_firmware_version(priv);
708         if (error != 0)
709                 goto err_fw;
710
711         /* Initialize regulatory */
712         error = ath_regd_init(&common->regulatory, priv->hw->wiphy,
713                               ath9k_reg_notifier);
714         if (error)
715                 goto err_regd;
716
717         reg = &common->regulatory;
718
719         /* Setup TX */
720         error = ath9k_tx_init(priv);
721         if (error != 0)
722                 goto err_tx;
723
724         /* Setup RX */
725         error = ath9k_rx_init(priv);
726         if (error != 0)
727                 goto err_rx;
728
729         ath9k_hw_disable(priv->ah);
730 #ifdef CONFIG_MAC80211_LEDS
731         /* must be initialized before ieee80211_register_hw */
732         priv->led_cdev.default_trigger = ieee80211_create_tpt_led_trigger(priv->hw,
733                 IEEE80211_TPT_LEDTRIG_FL_RADIO, ath9k_htc_tpt_blink,
734                 ARRAY_SIZE(ath9k_htc_tpt_blink));
735 #endif
736
737         /* Register with mac80211 */
738         error = ieee80211_register_hw(hw);
739         if (error)
740                 goto err_register;
741
742         /* Handle world regulatory */
743         if (!ath_is_world_regd(reg)) {
744                 error = regulatory_hint(hw->wiphy, reg->alpha2);
745                 if (error)
746                         goto err_world;
747         }
748
749         error = ath9k_htc_init_debug(priv->ah);
750         if (error) {
751                 ath_err(common, "Unable to create debugfs files\n");
752                 goto err_world;
753         }
754
755         ath_dbg(common, CONFIG,
756                 "WMI:%d, BCN:%d, CAB:%d, UAPSD:%d, MGMT:%d, BE:%d, BK:%d, VI:%d, VO:%d\n",
757                 priv->wmi_cmd_ep,
758                 priv->beacon_ep,
759                 priv->cab_ep,
760                 priv->uapsd_ep,
761                 priv->mgmt_ep,
762                 priv->data_be_ep,
763                 priv->data_bk_ep,
764                 priv->data_vi_ep,
765                 priv->data_vo_ep);
766
767         ath9k_hw_name(priv->ah, hw_name, sizeof(hw_name));
768         wiphy_info(hw->wiphy, "%s\n", hw_name);
769
770         ath9k_init_leds(priv);
771         ath9k_start_rfkill_poll(priv);
772
773         return 0;
774
775 err_world:
776         ieee80211_unregister_hw(hw);
777 err_register:
778         ath9k_rx_cleanup(priv);
779 err_rx:
780         ath9k_tx_cleanup(priv);
781 err_tx:
782         /* Nothing */
783 err_regd:
784         /* Nothing */
785 err_fw:
786         ath9k_deinit_priv(priv);
787 err_init:
788         return error;
789 }
790
791 int ath9k_htc_probe_device(struct htc_target *htc_handle, struct device *dev,
792                            u16 devid, char *product, u32 drv_info)
793 {
794         struct ieee80211_hw *hw;
795         struct ath9k_htc_priv *priv;
796         int ret;
797
798         hw = ieee80211_alloc_hw(sizeof(struct ath9k_htc_priv), &ath9k_htc_ops);
799         if (!hw)
800                 return -ENOMEM;
801
802         priv = hw->priv;
803         priv->hw = hw;
804         priv->htc = htc_handle;
805         priv->dev = dev;
806         htc_handle->drv_priv = priv;
807         SET_IEEE80211_DEV(hw, priv->dev);
808
809         ret = ath9k_htc_wait_for_target(priv);
810         if (ret)
811                 goto err_free;
812
813         priv->wmi = ath9k_init_wmi(priv);
814         if (!priv->wmi) {
815                 ret = -EINVAL;
816                 goto err_free;
817         }
818
819         ret = ath9k_init_htc_services(priv, devid, drv_info);
820         if (ret)
821                 goto err_init;
822
823         ret = ath9k_init_device(priv, devid, product, drv_info);
824         if (ret)
825                 goto err_init;
826
827         return 0;
828
829 err_init:
830         ath9k_deinit_wmi(priv);
831 err_free:
832         ieee80211_free_hw(hw);
833         return ret;
834 }
835
836 void ath9k_htc_disconnect_device(struct htc_target *htc_handle, bool hotunplug)
837 {
838         if (htc_handle->drv_priv) {
839
840                 /* Check if the device has been yanked out. */
841                 if (hotunplug)
842                         htc_handle->drv_priv->ah->ah_flags |= AH_UNPLUGGED;
843
844                 ath9k_deinit_device(htc_handle->drv_priv);
845                 ath9k_deinit_wmi(htc_handle->drv_priv);
846                 ieee80211_free_hw(htc_handle->drv_priv->hw);
847         }
848 }
849
850 #ifdef CONFIG_PM
851
852 void ath9k_htc_suspend(struct htc_target *htc_handle)
853 {
854         ath9k_htc_setpower(htc_handle->drv_priv, ATH9K_PM_FULL_SLEEP);
855 }
856
857 int ath9k_htc_resume(struct htc_target *htc_handle)
858 {
859         struct ath9k_htc_priv *priv = htc_handle->drv_priv;
860         int ret;
861
862         ret = ath9k_htc_wait_for_target(priv);
863         if (ret)
864                 return ret;
865
866         ret = ath9k_init_htc_services(priv, priv->ah->hw_version.devid,
867                                       priv->ah->hw_version.usbdev);
868         ath9k_configure_leds(priv);
869
870         return ret;
871 }
872 #endif
873
874 static int __init ath9k_htc_init(void)
875 {
876         if (ath9k_hif_usb_init() < 0) {
877                 pr_err("No USB devices found, driver not installed\n");
878                 return -ENODEV;
879         }
880
881         return 0;
882 }
883 module_init(ath9k_htc_init);
884
885 static void __exit ath9k_htc_exit(void)
886 {
887         ath9k_hif_usb_exit();
888         pr_info("Driver unloaded\n");
889 }
890 module_exit(ath9k_htc_exit);