2 * Copyright (c) 2010 Atheros Communications Inc.
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.
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.
19 MODULE_AUTHOR("Atheros Communications");
20 MODULE_LICENSE("Dual BSD/GPL");
21 MODULE_DESCRIPTION("Atheros driver 802.11n HTC based wireless devices");
23 static unsigned int ath9k_debug = ATH_DBG_DEFAULT;
24 module_param_named(debug, ath9k_debug, uint, 0);
25 MODULE_PARM_DESC(debug, "Debugging mask");
27 int htc_modparam_nohwcrypt;
28 module_param_named(nohwcrypt, htc_modparam_nohwcrypt, int, 0444);
29 MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption");
31 #define CHAN2G(_freq, _idx) { \
32 .center_freq = (_freq), \
37 static struct ieee80211_channel ath9k_2ghz_channels[] = {
38 CHAN2G(2412, 0), /* Channel 1 */
39 CHAN2G(2417, 1), /* Channel 2 */
40 CHAN2G(2422, 2), /* Channel 3 */
41 CHAN2G(2427, 3), /* Channel 4 */
42 CHAN2G(2432, 4), /* Channel 5 */
43 CHAN2G(2437, 5), /* Channel 6 */
44 CHAN2G(2442, 6), /* Channel 7 */
45 CHAN2G(2447, 7), /* Channel 8 */
46 CHAN2G(2452, 8), /* Channel 9 */
47 CHAN2G(2457, 9), /* Channel 10 */
48 CHAN2G(2462, 10), /* Channel 11 */
49 CHAN2G(2467, 11), /* Channel 12 */
50 CHAN2G(2472, 12), /* Channel 13 */
51 CHAN2G(2484, 13), /* Channel 14 */
54 /* Atheros hardware rate code addition for short premble */
55 #define SHPCHECK(__hw_rate, __flags) \
56 ((__flags & IEEE80211_RATE_SHORT_PREAMBLE) ? (__hw_rate | 0x04) : 0)
58 #define RATE(_bitrate, _hw_rate, _flags) { \
59 .bitrate = (_bitrate), \
61 .hw_value = (_hw_rate), \
62 .hw_value_short = (SHPCHECK(_hw_rate, _flags)) \
65 static struct ieee80211_rate ath9k_legacy_rates[] = {
67 RATE(20, 0x1a, IEEE80211_RATE_SHORT_PREAMBLE), /* shortp : 0x1e */
68 RATE(55, 0x19, IEEE80211_RATE_SHORT_PREAMBLE), /* shortp: 0x1d */
69 RATE(110, 0x18, IEEE80211_RATE_SHORT_PREAMBLE), /* short: 0x1c */
80 static int ath9k_htc_wait_for_target(struct ath9k_htc_priv *priv)
84 /* Firmware can take up to 50ms to get ready, to be safe use 1 second */
85 time_left = wait_for_completion_timeout(&priv->htc->target_wait, HZ);
87 dev_err(priv->dev, "ath9k_htc: Target is unresponsive\n");
94 static void ath9k_deinit_priv(struct ath9k_htc_priv *priv)
96 ath9k_htc_exit_debug(priv->ah);
97 ath9k_hw_deinit(priv->ah);
98 tasklet_kill(&priv->wmi_tasklet);
99 tasklet_kill(&priv->rx_tasklet);
100 tasklet_kill(&priv->tx_tasklet);
105 static void ath9k_deinit_device(struct ath9k_htc_priv *priv)
107 struct ieee80211_hw *hw = priv->hw;
109 wiphy_rfkill_stop_polling(hw->wiphy);
110 ath9k_deinit_leds(priv);
111 ieee80211_unregister_hw(hw);
112 ath9k_rx_cleanup(priv);
113 ath9k_tx_cleanup(priv);
114 ath9k_deinit_priv(priv);
117 static inline int ath9k_htc_connect_svc(struct ath9k_htc_priv *priv,
121 enum htc_endpoint_id,
123 enum htc_endpoint_id *ep_id)
125 struct htc_service_connreq req;
127 memset(&req, 0, sizeof(struct htc_service_connreq));
129 req.service_id = service_id;
130 req.ep_callbacks.priv = priv;
131 req.ep_callbacks.rx = ath9k_htc_rxep;
132 req.ep_callbacks.tx = tx;
134 return htc_connect_service(priv->htc, &req, ep_id);
137 static int ath9k_init_htc_services(struct ath9k_htc_priv *priv)
142 ret = ath9k_wmi_connect(priv->htc, priv->wmi, &priv->wmi_cmd_ep);
147 ret = ath9k_htc_connect_svc(priv, WMI_BEACON_SVC, NULL,
153 ret = ath9k_htc_connect_svc(priv, WMI_CAB_SVC, ath9k_htc_txep,
160 ret = ath9k_htc_connect_svc(priv, WMI_UAPSD_SVC, ath9k_htc_txep,
166 ret = ath9k_htc_connect_svc(priv, WMI_MGMT_SVC, ath9k_htc_txep,
172 ret = ath9k_htc_connect_svc(priv, WMI_DATA_BE_SVC, ath9k_htc_txep,
178 ret = ath9k_htc_connect_svc(priv, WMI_DATA_BK_SVC, ath9k_htc_txep,
184 ret = ath9k_htc_connect_svc(priv, WMI_DATA_VI_SVC, ath9k_htc_txep,
190 ret = ath9k_htc_connect_svc(priv, WMI_DATA_VO_SVC, ath9k_htc_txep,
195 ret = htc_init(priv->htc);
202 dev_err(priv->dev, "ath9k_htc: Unable to initialize HTC services\n");
206 static int ath9k_reg_notifier(struct wiphy *wiphy,
207 struct regulatory_request *request)
209 struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
210 struct ath9k_htc_priv *priv = hw->priv;
212 return ath_reg_notifier_apply(wiphy, request,
213 ath9k_hw_regulatory(priv->ah));
216 static unsigned int ath9k_ioread32(void *hw_priv, u32 reg_offset)
218 struct ath_hw *ah = (struct ath_hw *) hw_priv;
219 struct ath_common *common = ath9k_hw_common(ah);
220 struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;
221 __be32 val, reg = cpu_to_be32(reg_offset);
224 r = ath9k_wmi_cmd(priv->wmi, WMI_REG_READ_CMDID,
225 (u8 *) ®, sizeof(reg),
226 (u8 *) &val, sizeof(val),
229 ath_print(common, ATH_DBG_WMI,
230 "REGISTER READ FAILED: (0x%04x, %d)\n",
235 return be32_to_cpu(val);
238 static void ath9k_iowrite32(void *hw_priv, u32 val, u32 reg_offset)
240 struct ath_hw *ah = (struct ath_hw *) hw_priv;
241 struct ath_common *common = ath9k_hw_common(ah);
242 struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;
244 cpu_to_be32(reg_offset),
249 r = ath9k_wmi_cmd(priv->wmi, WMI_REG_WRITE_CMDID,
250 (u8 *) &buf, sizeof(buf),
251 (u8 *) &val, sizeof(val),
254 ath_print(common, ATH_DBG_WMI,
255 "REGISTER WRITE FAILED:(0x%04x, %d)\n",
260 static const struct ath_ops ath9k_common_ops = {
261 .read = ath9k_ioread32,
262 .write = ath9k_iowrite32,
265 static void ath_usb_read_cachesize(struct ath_common *common, int *csz)
267 *csz = L1_CACHE_BYTES >> 2;
270 static bool ath_usb_eeprom_read(struct ath_common *common, u32 off, u16 *data)
272 struct ath_hw *ah = (struct ath_hw *) common->ah;
274 (void)REG_READ(ah, AR5416_EEPROM_OFFSET + (off << AR5416_EEPROM_S));
276 if (!ath9k_hw_wait(ah,
277 AR_EEPROM_STATUS_DATA,
278 AR_EEPROM_STATUS_DATA_BUSY |
279 AR_EEPROM_STATUS_DATA_PROT_ACCESS, 0,
283 *data = MS(REG_READ(ah, AR_EEPROM_STATUS_DATA),
284 AR_EEPROM_STATUS_DATA_VAL);
289 static const struct ath_bus_ops ath9k_usb_bus_ops = {
290 .ath_bus_type = ATH_USB,
291 .read_cachesize = ath_usb_read_cachesize,
292 .eeprom_read = ath_usb_eeprom_read,
295 static void setup_ht_cap(struct ath9k_htc_priv *priv,
296 struct ieee80211_sta_ht_cap *ht_info)
298 ht_info->ht_supported = true;
299 ht_info->cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
300 IEEE80211_HT_CAP_SM_PS |
301 IEEE80211_HT_CAP_SGI_40 |
302 IEEE80211_HT_CAP_DSSSCCK40;
304 ht_info->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
305 ht_info->ampdu_density = IEEE80211_HT_MPDU_DENSITY_8;
307 memset(&ht_info->mcs, 0, sizeof(ht_info->mcs));
308 ht_info->mcs.rx_mask[0] = 0xff;
309 ht_info->mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
312 static int ath9k_init_queues(struct ath9k_htc_priv *priv)
314 struct ath_common *common = ath9k_hw_common(priv->ah);
317 for (i = 0; i < ARRAY_SIZE(priv->hwq_map); i++)
318 priv->hwq_map[i] = -1;
320 if (!ath9k_htc_txq_setup(priv, ATH9K_WME_AC_BE)) {
321 ath_print(common, ATH_DBG_FATAL,
322 "Unable to setup xmit queue for BE traffic\n");
326 if (!ath9k_htc_txq_setup(priv, ATH9K_WME_AC_BK)) {
327 ath_print(common, ATH_DBG_FATAL,
328 "Unable to setup xmit queue for BK traffic\n");
331 if (!ath9k_htc_txq_setup(priv, ATH9K_WME_AC_VI)) {
332 ath_print(common, ATH_DBG_FATAL,
333 "Unable to setup xmit queue for VI traffic\n");
336 if (!ath9k_htc_txq_setup(priv, ATH9K_WME_AC_VO)) {
337 ath_print(common, ATH_DBG_FATAL,
338 "Unable to setup xmit queue for VO traffic\n");
348 static void ath9k_init_crypto(struct ath9k_htc_priv *priv)
350 struct ath_common *common = ath9k_hw_common(priv->ah);
353 /* Get the hardware key cache size. */
354 common->keymax = priv->ah->caps.keycache_size;
355 if (common->keymax > ATH_KEYMAX) {
356 ath_print(common, ATH_DBG_ANY,
357 "Warning, using only %u entries in %u key cache\n",
358 ATH_KEYMAX, common->keymax);
359 common->keymax = ATH_KEYMAX;
363 * Reset the key cache since some parts do not
364 * reset the contents on initial power up.
366 for (i = 0; i < common->keymax; i++)
367 ath9k_hw_keyreset(priv->ah, (u16) i);
369 if (ath9k_hw_getcapability(priv->ah, ATH9K_CAP_CIPHER,
370 ATH9K_CIPHER_TKIP, NULL)) {
372 * Whether we should enable h/w TKIP MIC.
373 * XXX: if we don't support WME TKIP MIC, then we wouldn't
374 * report WMM capable, so it's always safe to turn on
375 * TKIP MIC in this case.
377 ath9k_hw_setcapability(priv->ah, ATH9K_CAP_TKIP_MIC, 0, 1, NULL);
381 * Check whether the separate key cache entries
382 * are required to handle both tx+rx MIC keys.
383 * With split mic keys the number of stations is limited
384 * to 27 otherwise 59.
386 if (ath9k_hw_getcapability(priv->ah, ATH9K_CAP_CIPHER,
387 ATH9K_CIPHER_TKIP, NULL)
388 && ath9k_hw_getcapability(priv->ah, ATH9K_CAP_CIPHER,
389 ATH9K_CIPHER_MIC, NULL)
390 && ath9k_hw_getcapability(priv->ah, ATH9K_CAP_TKIP_SPLIT,
392 common->splitmic = 1;
394 /* turn on mcast key search if possible */
395 if (!ath9k_hw_getcapability(priv->ah, ATH9K_CAP_MCAST_KEYSRCH, 0, NULL))
396 (void)ath9k_hw_setcapability(priv->ah, ATH9K_CAP_MCAST_KEYSRCH,
400 static void ath9k_init_channels_rates(struct ath9k_htc_priv *priv)
402 if (test_bit(ATH9K_MODE_11G, priv->ah->caps.wireless_modes)) {
403 priv->sbands[IEEE80211_BAND_2GHZ].channels =
405 priv->sbands[IEEE80211_BAND_2GHZ].band = IEEE80211_BAND_2GHZ;
406 priv->sbands[IEEE80211_BAND_2GHZ].n_channels =
407 ARRAY_SIZE(ath9k_2ghz_channels);
408 priv->sbands[IEEE80211_BAND_2GHZ].bitrates = ath9k_legacy_rates;
409 priv->sbands[IEEE80211_BAND_2GHZ].n_bitrates =
410 ARRAY_SIZE(ath9k_legacy_rates);
414 static void ath9k_init_misc(struct ath9k_htc_priv *priv)
416 struct ath_common *common = ath9k_hw_common(priv->ah);
418 common->tx_chainmask = priv->ah->caps.tx_chainmask;
419 common->rx_chainmask = priv->ah->caps.rx_chainmask;
421 if (priv->ah->caps.hw_caps & ATH9K_HW_CAP_BSSIDMASK)
422 memcpy(common->bssidmask, ath_bcast_mac, ETH_ALEN);
424 priv->op_flags |= OP_TXAGGR;
425 priv->ah->opmode = NL80211_IFTYPE_STATION;
428 static int ath9k_init_priv(struct ath9k_htc_priv *priv, u16 devid)
430 struct ath_hw *ah = NULL;
431 struct ath_common *common;
432 int ret = 0, csz = 0;
434 priv->op_flags |= OP_INVALID;
436 ah = kzalloc(sizeof(struct ath_hw), GFP_KERNEL);
440 ah->hw_version.devid = devid;
441 ah->hw_version.subsysid = 0; /* FIXME */
444 common = ath9k_hw_common(ah);
445 common->ops = &ath9k_common_ops;
446 common->bus_ops = &ath9k_usb_bus_ops;
448 common->hw = priv->hw;
450 common->debug_mask = ath9k_debug;
452 spin_lock_init(&priv->wmi->wmi_lock);
453 spin_lock_init(&priv->beacon_lock);
454 spin_lock_init(&priv->tx_lock);
455 mutex_init(&priv->mutex);
456 mutex_init(&priv->aggr_work.mutex);
457 tasklet_init(&priv->wmi_tasklet, ath9k_wmi_tasklet,
458 (unsigned long)priv);
459 tasklet_init(&priv->rx_tasklet, ath9k_rx_tasklet,
460 (unsigned long)priv);
461 tasklet_init(&priv->tx_tasklet, ath9k_tx_tasklet, (unsigned long)priv);
462 INIT_DELAYED_WORK(&priv->ath9k_aggr_work, ath9k_htc_aggr_work);
463 INIT_DELAYED_WORK(&priv->ath9k_ani_work, ath9k_ani_work);
466 * Cache line size is used to size and align various
467 * structures used to communicate with the hardware.
469 ath_read_cachesize(common, &csz);
470 common->cachelsz = csz << 2; /* convert to bytes */
472 ret = ath9k_hw_init(ah);
474 ath_print(common, ATH_DBG_FATAL,
475 "Unable to initialize hardware; "
476 "initialization status: %d\n", ret);
480 ret = ath9k_htc_init_debug(ah);
482 ath_print(common, ATH_DBG_FATAL,
483 "Unable to create debugfs files\n");
487 ret = ath9k_init_queues(priv);
491 ath9k_init_crypto(priv);
492 ath9k_init_channels_rates(priv);
493 ath9k_init_misc(priv);
498 ath9k_htc_exit_debug(ah);
509 static void ath9k_set_hw_capab(struct ath9k_htc_priv *priv,
510 struct ieee80211_hw *hw)
512 struct ath_common *common = ath9k_hw_common(priv->ah);
514 hw->flags = IEEE80211_HW_SIGNAL_DBM |
515 IEEE80211_HW_AMPDU_AGGREGATION |
516 IEEE80211_HW_SPECTRUM_MGMT |
517 IEEE80211_HW_HAS_RATE_CONTROL |
518 IEEE80211_HW_RX_INCLUDES_FCS;
520 hw->wiphy->interface_modes =
521 BIT(NL80211_IFTYPE_STATION) |
522 BIT(NL80211_IFTYPE_ADHOC);
525 hw->channel_change_time = 5000;
526 hw->max_listen_interval = 10;
527 hw->vif_data_size = sizeof(struct ath9k_htc_vif);
528 hw->sta_data_size = sizeof(struct ath9k_htc_sta);
530 /* tx_frame_hdr is larger than tx_mgmt_hdr anyway */
531 hw->extra_tx_headroom = sizeof(struct tx_frame_hdr) +
532 sizeof(struct htc_frame_hdr) + 4;
534 if (test_bit(ATH9K_MODE_11G, priv->ah->caps.wireless_modes))
535 hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
536 &priv->sbands[IEEE80211_BAND_2GHZ];
538 if (priv->ah->caps.hw_caps & ATH9K_HW_CAP_HT) {
539 if (test_bit(ATH9K_MODE_11G, priv->ah->caps.wireless_modes))
541 &priv->sbands[IEEE80211_BAND_2GHZ].ht_cap);
544 SET_IEEE80211_PERM_ADDR(hw, common->macaddr);
547 static int ath9k_init_device(struct ath9k_htc_priv *priv, u16 devid)
549 struct ieee80211_hw *hw = priv->hw;
550 struct ath_common *common;
553 struct ath_regulatory *reg;
555 /* Bring up device */
556 error = ath9k_init_priv(priv, devid);
561 common = ath9k_hw_common(ah);
562 ath9k_set_hw_capab(priv, hw);
564 /* Initialize regulatory */
565 error = ath_regd_init(&common->regulatory, priv->hw->wiphy,
570 reg = &common->regulatory;
573 error = ath9k_tx_init(priv);
578 error = ath9k_rx_init(priv);
582 /* Register with mac80211 */
583 error = ieee80211_register_hw(hw);
587 /* Handle world regulatory */
588 if (!ath_is_world_regd(reg)) {
589 error = regulatory_hint(hw->wiphy, reg->alpha2);
594 ath9k_init_leds(priv);
595 ath9k_start_rfkill_poll(priv);
600 ieee80211_unregister_hw(hw);
602 ath9k_rx_cleanup(priv);
604 ath9k_tx_cleanup(priv);
608 ath9k_deinit_priv(priv);
613 int ath9k_htc_probe_device(struct htc_target *htc_handle, struct device *dev,
616 struct ieee80211_hw *hw;
617 struct ath9k_htc_priv *priv;
620 hw = ieee80211_alloc_hw(sizeof(struct ath9k_htc_priv), &ath9k_htc_ops);
626 priv->htc = htc_handle;
628 htc_handle->drv_priv = priv;
629 SET_IEEE80211_DEV(hw, priv->dev);
631 ret = ath9k_htc_wait_for_target(priv);
635 priv->wmi = ath9k_init_wmi(priv);
641 ret = ath9k_init_htc_services(priv);
645 ret = ath9k_init_device(priv, devid);
652 ath9k_deinit_wmi(priv);
654 ieee80211_free_hw(hw);
658 void ath9k_htc_disconnect_device(struct htc_target *htc_handle, bool hotunplug)
660 if (htc_handle->drv_priv) {
661 ath9k_deinit_device(htc_handle->drv_priv);
662 ath9k_deinit_wmi(htc_handle->drv_priv);
663 ieee80211_free_hw(htc_handle->drv_priv->hw);
668 int ath9k_htc_resume(struct htc_target *htc_handle)
672 ret = ath9k_htc_wait_for_target(htc_handle->drv_priv);
676 ret = ath9k_init_htc_services(htc_handle->drv_priv);
681 static int __init ath9k_htc_init(void)
685 error = ath9k_htc_debug_create_root();
688 "ath9k_htc: Unable to create debugfs root: %d\n",
693 error = ath9k_hif_usb_init();
696 "ath9k_htc: No USB devices found,"
697 " driver not installed.\n");
705 ath9k_htc_debug_remove_root();
709 module_init(ath9k_htc_init);
711 static void __exit ath9k_htc_exit(void)
713 ath9k_hif_usb_exit();
714 ath9k_htc_debug_remove_root();
715 printk(KERN_INFO "ath9k_htc: Driver unloaded\n");
717 module_exit(ath9k_htc_exit);