htc_drv_init.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  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. #include "htc.h"
  17. MODULE_AUTHOR("Atheros Communications");
  18. MODULE_LICENSE("Dual BSD/GPL");
  19. MODULE_DESCRIPTION("Atheros driver 802.11n HTC based wireless devices");
  20. static unsigned int ath9k_debug = ATH_DBG_DEFAULT;
  21. module_param_named(debug, ath9k_debug, uint, 0);
  22. MODULE_PARM_DESC(debug, "Debugging mask");
  23. int htc_modparam_nohwcrypt;
  24. module_param_named(nohwcrypt, htc_modparam_nohwcrypt, int, 0444);
  25. MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption");
  26. #define CHAN2G(_freq, _idx) { \
  27. .center_freq = (_freq), \
  28. .hw_value = (_idx), \
  29. .max_power = 20, \
  30. }
  31. #define CHAN5G(_freq, _idx) { \
  32. .band = IEEE80211_BAND_5GHZ, \
  33. .center_freq = (_freq), \
  34. .hw_value = (_idx), \
  35. .max_power = 20, \
  36. }
  37. #define ATH_HTC_BTCOEX_PRODUCT_ID "wb193"
  38. static struct ieee80211_channel ath9k_2ghz_channels[] = {
  39. CHAN2G(2412, 0), /* Channel 1 */
  40. CHAN2G(2417, 1), /* Channel 2 */
  41. CHAN2G(2422, 2), /* Channel 3 */
  42. CHAN2G(2427, 3), /* Channel 4 */
  43. CHAN2G(2432, 4), /* Channel 5 */
  44. CHAN2G(2437, 5), /* Channel 6 */
  45. CHAN2G(2442, 6), /* Channel 7 */
  46. CHAN2G(2447, 7), /* Channel 8 */
  47. CHAN2G(2452, 8), /* Channel 9 */
  48. CHAN2G(2457, 9), /* Channel 10 */
  49. CHAN2G(2462, 10), /* Channel 11 */
  50. CHAN2G(2467, 11), /* Channel 12 */
  51. CHAN2G(2472, 12), /* Channel 13 */
  52. CHAN2G(2484, 13), /* Channel 14 */
  53. };
  54. static struct ieee80211_channel ath9k_5ghz_channels[] = {
  55. /* _We_ call this UNII 1 */
  56. CHAN5G(5180, 14), /* Channel 36 */
  57. CHAN5G(5200, 15), /* Channel 40 */
  58. CHAN5G(5220, 16), /* Channel 44 */
  59. CHAN5G(5240, 17), /* Channel 48 */
  60. /* _We_ call this UNII 2 */
  61. CHAN5G(5260, 18), /* Channel 52 */
  62. CHAN5G(5280, 19), /* Channel 56 */
  63. CHAN5G(5300, 20), /* Channel 60 */
  64. CHAN5G(5320, 21), /* Channel 64 */
  65. /* _We_ call this "Middle band" */
  66. CHAN5G(5500, 22), /* Channel 100 */
  67. CHAN5G(5520, 23), /* Channel 104 */
  68. CHAN5G(5540, 24), /* Channel 108 */
  69. CHAN5G(5560, 25), /* Channel 112 */
  70. CHAN5G(5580, 26), /* Channel 116 */
  71. CHAN5G(5600, 27), /* Channel 120 */
  72. CHAN5G(5620, 28), /* Channel 124 */
  73. CHAN5G(5640, 29), /* Channel 128 */
  74. CHAN5G(5660, 30), /* Channel 132 */
  75. CHAN5G(5680, 31), /* Channel 136 */
  76. CHAN5G(5700, 32), /* Channel 140 */
  77. /* _We_ call this UNII 3 */
  78. CHAN5G(5745, 33), /* Channel 149 */
  79. CHAN5G(5765, 34), /* Channel 153 */
  80. CHAN5G(5785, 35), /* Channel 157 */
  81. CHAN5G(5805, 36), /* Channel 161 */
  82. CHAN5G(5825, 37), /* Channel 165 */
  83. };
  84. /* Atheros hardware rate code addition for short premble */
  85. #define SHPCHECK(__hw_rate, __flags) \
  86. ((__flags & IEEE80211_RATE_SHORT_PREAMBLE) ? (__hw_rate | 0x04) : 0)
  87. #define RATE(_bitrate, _hw_rate, _flags) { \
  88. .bitrate = (_bitrate), \
  89. .flags = (_flags), \
  90. .hw_value = (_hw_rate), \
  91. .hw_value_short = (SHPCHECK(_hw_rate, _flags)) \
  92. }
  93. static struct ieee80211_rate ath9k_legacy_rates[] = {
  94. RATE(10, 0x1b, 0),
  95. RATE(20, 0x1a, IEEE80211_RATE_SHORT_PREAMBLE), /* shortp : 0x1e */
  96. RATE(55, 0x19, IEEE80211_RATE_SHORT_PREAMBLE), /* shortp: 0x1d */
  97. RATE(110, 0x18, IEEE80211_RATE_SHORT_PREAMBLE), /* short: 0x1c */
  98. RATE(60, 0x0b, 0),
  99. RATE(90, 0x0f, 0),
  100. RATE(120, 0x0a, 0),
  101. RATE(180, 0x0e, 0),
  102. RATE(240, 0x09, 0),
  103. RATE(360, 0x0d, 0),
  104. RATE(480, 0x08, 0),
  105. RATE(540, 0x0c, 0),
  106. };
  107. #ifdef CONFIG_MAC80211_LEDS
  108. static const struct ieee80211_tpt_blink ath9k_htc_tpt_blink[] = {
  109. { .throughput = 0 * 1024, .blink_time = 334 },
  110. { .throughput = 1 * 1024, .blink_time = 260 },
  111. { .throughput = 5 * 1024, .blink_time = 220 },
  112. { .throughput = 10 * 1024, .blink_time = 190 },
  113. { .throughput = 20 * 1024, .blink_time = 170 },
  114. { .throughput = 50 * 1024, .blink_time = 150 },
  115. { .throughput = 70 * 1024, .blink_time = 130 },
  116. { .throughput = 100 * 1024, .blink_time = 110 },
  117. { .throughput = 200 * 1024, .blink_time = 80 },
  118. { .throughput = 300 * 1024, .blink_time = 50 },
  119. };
  120. #endif
  121. static int ath9k_htc_wait_for_target(struct ath9k_htc_priv *priv)
  122. {
  123. int time_left;
  124. if (atomic_read(&priv->htc->tgt_ready) > 0) {
  125. atomic_dec(&priv->htc->tgt_ready);
  126. return 0;
  127. }
  128. /* Firmware can take up to 50ms to get ready, to be safe use 1 second */
  129. time_left = wait_for_completion_timeout(&priv->htc->target_wait, HZ);
  130. if (!time_left) {
  131. dev_err(priv->dev, "ath9k_htc: Target is unresponsive\n");
  132. return -ETIMEDOUT;
  133. }
  134. atomic_dec(&priv->htc->tgt_ready);
  135. return 0;
  136. }
  137. static void ath9k_deinit_priv(struct ath9k_htc_priv *priv)
  138. {
  139. ath9k_hw_deinit(priv->ah);
  140. kfree(priv->ah);
  141. priv->ah = NULL;
  142. }
  143. static void ath9k_deinit_device(struct ath9k_htc_priv *priv)
  144. {
  145. struct ieee80211_hw *hw = priv->hw;
  146. wiphy_rfkill_stop_polling(hw->wiphy);
  147. ath9k_deinit_leds(priv);
  148. ieee80211_unregister_hw(hw);
  149. ath9k_rx_cleanup(priv);
  150. ath9k_tx_cleanup(priv);
  151. ath9k_deinit_priv(priv);
  152. }
  153. static inline int ath9k_htc_connect_svc(struct ath9k_htc_priv *priv,
  154. u16 service_id,
  155. void (*tx) (void *,
  156. struct sk_buff *,
  157. enum htc_endpoint_id,
  158. bool txok),
  159. enum htc_endpoint_id *ep_id)
  160. {
  161. struct htc_service_connreq req;
  162. memset(&req, 0, sizeof(struct htc_service_connreq));
  163. req.service_id = service_id;
  164. req.ep_callbacks.priv = priv;
  165. req.ep_callbacks.rx = ath9k_htc_rxep;
  166. req.ep_callbacks.tx = tx;
  167. return htc_connect_service(priv->htc, &req, ep_id);
  168. }
  169. static int ath9k_init_htc_services(struct ath9k_htc_priv *priv, u16 devid,
  170. u32 drv_info)
  171. {
  172. int ret;
  173. /* WMI CMD*/
  174. ret = ath9k_wmi_connect(priv->htc, priv->wmi, &priv->wmi_cmd_ep);
  175. if (ret)
  176. goto err;
  177. /* Beacon */
  178. ret = ath9k_htc_connect_svc(priv, WMI_BEACON_SVC, ath9k_htc_beaconep,
  179. &priv->beacon_ep);
  180. if (ret)
  181. goto err;
  182. /* CAB */
  183. ret = ath9k_htc_connect_svc(priv, WMI_CAB_SVC, ath9k_htc_txep,
  184. &priv->cab_ep);
  185. if (ret)
  186. goto err;
  187. /* UAPSD */
  188. ret = ath9k_htc_connect_svc(priv, WMI_UAPSD_SVC, ath9k_htc_txep,
  189. &priv->uapsd_ep);
  190. if (ret)
  191. goto err;
  192. /* MGMT */
  193. ret = ath9k_htc_connect_svc(priv, WMI_MGMT_SVC, ath9k_htc_txep,
  194. &priv->mgmt_ep);
  195. if (ret)
  196. goto err;
  197. /* DATA BE */
  198. ret = ath9k_htc_connect_svc(priv, WMI_DATA_BE_SVC, ath9k_htc_txep,
  199. &priv->data_be_ep);
  200. if (ret)
  201. goto err;
  202. /* DATA BK */
  203. ret = ath9k_htc_connect_svc(priv, WMI_DATA_BK_SVC, ath9k_htc_txep,
  204. &priv->data_bk_ep);
  205. if (ret)
  206. goto err;
  207. /* DATA VI */
  208. ret = ath9k_htc_connect_svc(priv, WMI_DATA_VI_SVC, ath9k_htc_txep,
  209. &priv->data_vi_ep);
  210. if (ret)
  211. goto err;
  212. /* DATA VO */
  213. ret = ath9k_htc_connect_svc(priv, WMI_DATA_VO_SVC, ath9k_htc_txep,
  214. &priv->data_vo_ep);
  215. if (ret)
  216. goto err;
  217. /*
  218. * Setup required credits before initializing HTC.
  219. * This is a bit hacky, but, since queuing is done in
  220. * the HIF layer, shouldn't matter much.
  221. */
  222. if (IS_AR7010_DEVICE(drv_info))
  223. priv->htc->credits = 45;
  224. else
  225. priv->htc->credits = 33;
  226. ret = htc_init(priv->htc);
  227. if (ret)
  228. goto err;
  229. dev_info(priv->dev, "ath9k_htc: HTC initialized with %d credits\n",
  230. priv->htc->credits);
  231. return 0;
  232. err:
  233. dev_err(priv->dev, "ath9k_htc: Unable to initialize HTC services\n");
  234. return ret;
  235. }
  236. static int ath9k_reg_notifier(struct wiphy *wiphy,
  237. struct regulatory_request *request)
  238. {
  239. struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
  240. struct ath9k_htc_priv *priv = hw->priv;
  241. return ath_reg_notifier_apply(wiphy, request,
  242. ath9k_hw_regulatory(priv->ah));
  243. }
  244. static unsigned int ath9k_regread(void *hw_priv, u32 reg_offset)
  245. {
  246. struct ath_hw *ah = (struct ath_hw *) hw_priv;
  247. struct ath_common *common = ath9k_hw_common(ah);
  248. struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;
  249. __be32 val, reg = cpu_to_be32(reg_offset);
  250. int r;
  251. r = ath9k_wmi_cmd(priv->wmi, WMI_REG_READ_CMDID,
  252. (u8 *) &reg, sizeof(reg),
  253. (u8 *) &val, sizeof(val),
  254. 100);
  255. if (unlikely(r)) {
  256. ath_dbg(common, WMI, "REGISTER READ FAILED: (0x%04x, %d)\n",
  257. reg_offset, r);
  258. return -EIO;
  259. }
  260. return be32_to_cpu(val);
  261. }
  262. static void ath9k_multi_regread(void *hw_priv, u32 *addr,
  263. u32 *val, u16 count)
  264. {
  265. struct ath_hw *ah = (struct ath_hw *) hw_priv;
  266. struct ath_common *common = ath9k_hw_common(ah);
  267. struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;
  268. __be32 tmpaddr[8];
  269. __be32 tmpval[8];
  270. int i, ret;
  271. for (i = 0; i < count; i++) {
  272. tmpaddr[i] = cpu_to_be32(addr[i]);
  273. }
  274. ret = ath9k_wmi_cmd(priv->wmi, WMI_REG_READ_CMDID,
  275. (u8 *)tmpaddr , sizeof(u32) * count,
  276. (u8 *)tmpval, sizeof(u32) * count,
  277. 100);
  278. if (unlikely(ret)) {
  279. ath_dbg(common, WMI,
  280. "Multiple REGISTER READ FAILED (count: %d)\n", count);
  281. }
  282. for (i = 0; i < count; i++) {
  283. val[i] = be32_to_cpu(tmpval[i]);
  284. }
  285. }
  286. static void ath9k_regwrite_single(void *hw_priv, u32 val, u32 reg_offset)
  287. {
  288. struct ath_hw *ah = (struct ath_hw *) hw_priv;
  289. struct ath_common *common = ath9k_hw_common(ah);
  290. struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;
  291. const __be32 buf[2] = {
  292. cpu_to_be32(reg_offset),
  293. cpu_to_be32(val),
  294. };
  295. int r;
  296. r = ath9k_wmi_cmd(priv->wmi, WMI_REG_WRITE_CMDID,
  297. (u8 *) &buf, sizeof(buf),
  298. (u8 *) &val, sizeof(val),
  299. 100);
  300. if (unlikely(r)) {
  301. ath_dbg(common, WMI, "REGISTER WRITE FAILED:(0x%04x, %d)\n",
  302. reg_offset, r);
  303. }
  304. }
  305. static void ath9k_regwrite_buffer(void *hw_priv, u32 val, u32 reg_offset)
  306. {
  307. struct ath_hw *ah = (struct ath_hw *) hw_priv;
  308. struct ath_common *common = ath9k_hw_common(ah);
  309. struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;
  310. u32 rsp_status;
  311. int r;
  312. mutex_lock(&priv->wmi->multi_write_mutex);
  313. /* Store the register/value */
  314. priv->wmi->multi_write[priv->wmi->multi_write_idx].reg =
  315. cpu_to_be32(reg_offset);
  316. priv->wmi->multi_write[priv->wmi->multi_write_idx].val =
  317. cpu_to_be32(val);
  318. priv->wmi->multi_write_idx++;
  319. /* If the buffer is full, send it out. */
  320. if (priv->wmi->multi_write_idx == MAX_CMD_NUMBER) {
  321. r = ath9k_wmi_cmd(priv->wmi, WMI_REG_WRITE_CMDID,
  322. (u8 *) &priv->wmi->multi_write,
  323. sizeof(struct register_write) * priv->wmi->multi_write_idx,
  324. (u8 *) &rsp_status, sizeof(rsp_status),
  325. 100);
  326. if (unlikely(r)) {
  327. ath_dbg(common, WMI,
  328. "REGISTER WRITE FAILED, multi len: %d\n",
  329. priv->wmi->multi_write_idx);
  330. }
  331. priv->wmi->multi_write_idx = 0;
  332. }
  333. mutex_unlock(&priv->wmi->multi_write_mutex);
  334. }
  335. static void ath9k_regwrite(void *hw_priv, u32 val, u32 reg_offset)
  336. {
  337. struct ath_hw *ah = (struct ath_hw *) hw_priv;
  338. struct ath_common *common = ath9k_hw_common(ah);
  339. struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;
  340. if (atomic_read(&priv->wmi->mwrite_cnt))
  341. ath9k_regwrite_buffer(hw_priv, val, reg_offset);
  342. else
  343. ath9k_regwrite_single(hw_priv, val, reg_offset);
  344. }
  345. static void ath9k_enable_regwrite_buffer(void *hw_priv)
  346. {
  347. struct ath_hw *ah = (struct ath_hw *) hw_priv;
  348. struct ath_common *common = ath9k_hw_common(ah);
  349. struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;
  350. atomic_inc(&priv->wmi->mwrite_cnt);
  351. }
  352. static void ath9k_regwrite_flush(void *hw_priv)
  353. {
  354. struct ath_hw *ah = (struct ath_hw *) hw_priv;
  355. struct ath_common *common = ath9k_hw_common(ah);
  356. struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;
  357. u32 rsp_status;
  358. int r;
  359. atomic_dec(&priv->wmi->mwrite_cnt);
  360. mutex_lock(&priv->wmi->multi_write_mutex);
  361. if (priv->wmi->multi_write_idx) {
  362. r = ath9k_wmi_cmd(priv->wmi, WMI_REG_WRITE_CMDID,
  363. (u8 *) &priv->wmi->multi_write,
  364. sizeof(struct register_write) * priv->wmi->multi_write_idx,
  365. (u8 *) &rsp_status, sizeof(rsp_status),
  366. 100);
  367. if (unlikely(r)) {
  368. ath_dbg(common, WMI,
  369. "REGISTER WRITE FAILED, multi len: %d\n",
  370. priv->wmi->multi_write_idx);
  371. }
  372. priv->wmi->multi_write_idx = 0;
  373. }
  374. mutex_unlock(&priv->wmi->multi_write_mutex);
  375. }
  376. static u32 ath9k_reg_rmw(void *hw_priv, u32 reg_offset, u32 set, u32 clr)
  377. {
  378. u32 val;
  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. static void ath_usb_read_cachesize(struct ath_common *common, int *csz)
  386. {
  387. *csz = L1_CACHE_BYTES >> 2;
  388. }
  389. static bool ath_usb_eeprom_read(struct ath_common *common, u32 off, u16 *data)
  390. {
  391. struct ath_hw *ah = (struct ath_hw *) common->ah;
  392. (void)REG_READ(ah, AR5416_EEPROM_OFFSET + (off << AR5416_EEPROM_S));
  393. if (!ath9k_hw_wait(ah,
  394. AR_EEPROM_STATUS_DATA,
  395. AR_EEPROM_STATUS_DATA_BUSY |
  396. AR_EEPROM_STATUS_DATA_PROT_ACCESS, 0,
  397. AH_WAIT_TIMEOUT))
  398. return false;
  399. *data = MS(REG_READ(ah, AR_EEPROM_STATUS_DATA),
  400. AR_EEPROM_STATUS_DATA_VAL);
  401. return true;
  402. }
  403. static const struct ath_bus_ops ath9k_usb_bus_ops = {
  404. .ath_bus_type = ATH_USB,
  405. .read_cachesize = ath_usb_read_cachesize,
  406. .eeprom_read = ath_usb_eeprom_read,
  407. };
  408. static void setup_ht_cap(struct ath9k_htc_priv *priv,
  409. struct ieee80211_sta_ht_cap *ht_info)
  410. {
  411. struct ath_common *common = ath9k_hw_common(priv->ah);
  412. u8 tx_streams, rx_streams;
  413. int i;
  414. ht_info->ht_supported = true;
  415. ht_info->cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
  416. IEEE80211_HT_CAP_SM_PS |
  417. IEEE80211_HT_CAP_SGI_40 |
  418. IEEE80211_HT_CAP_DSSSCCK40;
  419. if (priv->ah->caps.hw_caps & ATH9K_HW_CAP_SGI_20)
  420. ht_info->cap |= IEEE80211_HT_CAP_SGI_20;
  421. ht_info->cap |= (1 << IEEE80211_HT_CAP_RX_STBC_SHIFT);
  422. ht_info->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
  423. ht_info->ampdu_density = IEEE80211_HT_MPDU_DENSITY_8;
  424. memset(&ht_info->mcs, 0, sizeof(ht_info->mcs));
  425. /* ath9k_htc supports only 1 or 2 stream devices */
  426. tx_streams = ath9k_cmn_count_streams(priv->ah->txchainmask, 2);
  427. rx_streams = ath9k_cmn_count_streams(priv->ah->rxchainmask, 2);
  428. ath_dbg(common, CONFIG, "TX streams %d, RX streams: %d\n",
  429. tx_streams, rx_streams);
  430. if (tx_streams != rx_streams) {
  431. ht_info->mcs.tx_params |= IEEE80211_HT_MCS_TX_RX_DIFF;
  432. ht_info->mcs.tx_params |= ((tx_streams - 1) <<
  433. IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT);
  434. }
  435. for (i = 0; i < rx_streams; i++)
  436. ht_info->mcs.rx_mask[i] = 0xff;
  437. ht_info->mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
  438. }
  439. static int ath9k_init_queues(struct ath9k_htc_priv *priv)
  440. {
  441. struct ath_common *common = ath9k_hw_common(priv->ah);
  442. int i;
  443. for (i = 0; i < ARRAY_SIZE(priv->hwq_map); i++)
  444. priv->hwq_map[i] = -1;
  445. priv->beaconq = ath9k_hw_beaconq_setup(priv->ah);
  446. if (priv->beaconq == -1) {
  447. ath_err(common, "Unable to setup BEACON xmit queue\n");
  448. goto err;
  449. }
  450. priv->cabq = ath9k_htc_cabq_setup(priv);
  451. if (priv->cabq == -1) {
  452. ath_err(common, "Unable to setup CAB xmit queue\n");
  453. goto err;
  454. }
  455. if (!ath9k_htc_txq_setup(priv, WME_AC_BE)) {
  456. ath_err(common, "Unable to setup xmit queue for BE traffic\n");
  457. goto err;
  458. }
  459. if (!ath9k_htc_txq_setup(priv, WME_AC_BK)) {
  460. ath_err(common, "Unable to setup xmit queue for BK traffic\n");
  461. goto err;
  462. }
  463. if (!ath9k_htc_txq_setup(priv, WME_AC_VI)) {
  464. ath_err(common, "Unable to setup xmit queue for VI traffic\n");
  465. goto err;
  466. }
  467. if (!ath9k_htc_txq_setup(priv, WME_AC_VO)) {
  468. ath_err(common, "Unable to setup xmit queue for VO traffic\n");
  469. goto err;
  470. }
  471. return 0;
  472. err:
  473. return -EINVAL;
  474. }
  475. static void ath9k_init_channels_rates(struct ath9k_htc_priv *priv)
  476. {
  477. if (priv->ah->caps.hw_caps & ATH9K_HW_CAP_2GHZ) {
  478. priv->sbands[IEEE80211_BAND_2GHZ].channels =
  479. ath9k_2ghz_channels;
  480. priv->sbands[IEEE80211_BAND_2GHZ].band = IEEE80211_BAND_2GHZ;
  481. priv->sbands[IEEE80211_BAND_2GHZ].n_channels =
  482. ARRAY_SIZE(ath9k_2ghz_channels);
  483. priv->sbands[IEEE80211_BAND_2GHZ].bitrates = ath9k_legacy_rates;
  484. priv->sbands[IEEE80211_BAND_2GHZ].n_bitrates =
  485. ARRAY_SIZE(ath9k_legacy_rates);
  486. }
  487. if (priv->ah->caps.hw_caps & ATH9K_HW_CAP_5GHZ) {
  488. priv->sbands[IEEE80211_BAND_5GHZ].channels = ath9k_5ghz_channels;
  489. priv->sbands[IEEE80211_BAND_5GHZ].band = IEEE80211_BAND_5GHZ;
  490. priv->sbands[IEEE80211_BAND_5GHZ].n_channels =
  491. ARRAY_SIZE(ath9k_5ghz_channels);
  492. priv->sbands[IEEE80211_BAND_5GHZ].bitrates =
  493. ath9k_legacy_rates + 4;
  494. priv->sbands[IEEE80211_BAND_5GHZ].n_bitrates =
  495. ARRAY_SIZE(ath9k_legacy_rates) - 4;
  496. }
  497. }
  498. static void ath9k_init_misc(struct ath9k_htc_priv *priv)
  499. {
  500. struct ath_common *common = ath9k_hw_common(priv->ah);
  501. memcpy(common->bssidmask, ath_bcast_mac, ETH_ALEN);
  502. priv->ah->opmode = NL80211_IFTYPE_STATION;
  503. }
  504. static void ath9k_init_btcoex(struct ath9k_htc_priv *priv)
  505. {
  506. int qnum;
  507. switch (ath9k_hw_get_btcoex_scheme(priv->ah)) {
  508. case ATH_BTCOEX_CFG_NONE:
  509. break;
  510. case ATH_BTCOEX_CFG_3WIRE:
  511. priv->ah->btcoex_hw.btactive_gpio = 7;
  512. priv->ah->btcoex_hw.btpriority_gpio = 6;
  513. priv->ah->btcoex_hw.wlanactive_gpio = 8;
  514. priv->btcoex.bt_stomp_type = ATH_BTCOEX_STOMP_LOW;
  515. ath9k_hw_btcoex_init_3wire(priv->ah);
  516. ath_htc_init_btcoex_work(priv);
  517. qnum = priv->hwq_map[WME_AC_BE];
  518. ath9k_hw_init_btcoex_hw(priv->ah, qnum);
  519. break;
  520. default:
  521. WARN_ON(1);
  522. break;
  523. }
  524. }
  525. static int ath9k_init_priv(struct ath9k_htc_priv *priv,
  526. u16 devid, char *product,
  527. u32 drv_info)
  528. {
  529. struct ath_hw *ah = NULL;
  530. struct ath_common *common;
  531. int i, ret = 0, csz = 0;
  532. priv->op_flags |= OP_INVALID;
  533. ah = kzalloc(sizeof(struct ath_hw), GFP_KERNEL);
  534. if (!ah)
  535. return -ENOMEM;
  536. ah->hw_version.devid = devid;
  537. ah->hw_version.usbdev = drv_info;
  538. ah->ah_flags |= AH_USE_EEPROM;
  539. ah->reg_ops.read = ath9k_regread;
  540. ah->reg_ops.multi_read = ath9k_multi_regread;
  541. ah->reg_ops.write = ath9k_regwrite;
  542. ah->reg_ops.enable_write_buffer = ath9k_enable_regwrite_buffer;
  543. ah->reg_ops.write_flush = ath9k_regwrite_flush;
  544. ah->reg_ops.rmw = ath9k_reg_rmw;
  545. priv->ah = ah;
  546. common = ath9k_hw_common(ah);
  547. common->ops = &ah->reg_ops;
  548. common->bus_ops = &ath9k_usb_bus_ops;
  549. common->ah = ah;
  550. common->hw = priv->hw;
  551. common->priv = priv;
  552. common->debug_mask = ath9k_debug;
  553. spin_lock_init(&priv->beacon_lock);
  554. spin_lock_init(&priv->tx.tx_lock);
  555. mutex_init(&priv->mutex);
  556. mutex_init(&priv->htc_pm_lock);
  557. tasklet_init(&priv->rx_tasklet, ath9k_rx_tasklet,
  558. (unsigned long)priv);
  559. tasklet_init(&priv->tx_failed_tasklet, ath9k_tx_failed_tasklet,
  560. (unsigned long)priv);
  561. INIT_DELAYED_WORK(&priv->ani_work, ath9k_htc_ani_work);
  562. INIT_WORK(&priv->ps_work, ath9k_ps_work);
  563. INIT_WORK(&priv->fatal_work, ath9k_fatal_work);
  564. setup_timer(&priv->tx.cleanup_timer, ath9k_htc_tx_cleanup_timer,
  565. (unsigned long)priv);
  566. /*
  567. * Cache line size is used to size and align various
  568. * structures used to communicate with the hardware.
  569. */
  570. ath_read_cachesize(common, &csz);
  571. common->cachelsz = csz << 2; /* convert to bytes */
  572. ret = ath9k_hw_init(ah);
  573. if (ret) {
  574. ath_err(common,
  575. "Unable to initialize hardware; initialization status: %d\n",
  576. ret);
  577. goto err_hw;
  578. }
  579. ret = ath9k_init_queues(priv);
  580. if (ret)
  581. goto err_queues;
  582. for (i = 0; i < ATH9K_HTC_MAX_BCN_VIF; i++)
  583. priv->cur_beacon_conf.bslot[i] = NULL;
  584. ath9k_cmn_init_crypto(ah);
  585. ath9k_init_channels_rates(priv);
  586. ath9k_init_misc(priv);
  587. if (product && strncmp(product, ATH_HTC_BTCOEX_PRODUCT_ID, 5) == 0) {
  588. ah->btcoex_hw.scheme = ATH_BTCOEX_CFG_3WIRE;
  589. if (ath9k_hw_get_btcoex_scheme(ah) != ATH_BTCOEX_CFG_NONE)
  590. ath9k_init_btcoex(priv);
  591. }
  592. return 0;
  593. err_queues:
  594. ath9k_hw_deinit(ah);
  595. err_hw:
  596. kfree(ah);
  597. priv->ah = NULL;
  598. return ret;
  599. }
  600. static void ath9k_set_hw_capab(struct ath9k_htc_priv *priv,
  601. struct ieee80211_hw *hw)
  602. {
  603. struct ath_common *common = ath9k_hw_common(priv->ah);
  604. hw->flags = IEEE80211_HW_SIGNAL_DBM |
  605. IEEE80211_HW_AMPDU_AGGREGATION |
  606. IEEE80211_HW_SPECTRUM_MGMT |
  607. IEEE80211_HW_HAS_RATE_CONTROL |
  608. IEEE80211_HW_RX_INCLUDES_FCS |
  609. IEEE80211_HW_SUPPORTS_PS |
  610. IEEE80211_HW_PS_NULLFUNC_STACK |
  611. IEEE80211_HW_REPORTS_TX_ACK_STATUS |
  612. IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING;
  613. hw->wiphy->interface_modes =
  614. BIT(NL80211_IFTYPE_STATION) |
  615. BIT(NL80211_IFTYPE_ADHOC) |
  616. BIT(NL80211_IFTYPE_AP) |
  617. BIT(NL80211_IFTYPE_P2P_GO) |
  618. BIT(NL80211_IFTYPE_P2P_CLIENT);
  619. hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
  620. hw->queues = 4;
  621. hw->channel_change_time = 5000;
  622. hw->max_listen_interval = 10;
  623. hw->vif_data_size = sizeof(struct ath9k_htc_vif);
  624. hw->sta_data_size = sizeof(struct ath9k_htc_sta);
  625. /* tx_frame_hdr is larger than tx_mgmt_hdr anyway */
  626. hw->extra_tx_headroom = sizeof(struct tx_frame_hdr) +
  627. sizeof(struct htc_frame_hdr) + 4;
  628. if (priv->ah->caps.hw_caps & ATH9K_HW_CAP_2GHZ)
  629. hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
  630. &priv->sbands[IEEE80211_BAND_2GHZ];
  631. if (priv->ah->caps.hw_caps & ATH9K_HW_CAP_5GHZ)
  632. hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
  633. &priv->sbands[IEEE80211_BAND_5GHZ];
  634. if (priv->ah->caps.hw_caps & ATH9K_HW_CAP_HT) {
  635. if (priv->ah->caps.hw_caps & ATH9K_HW_CAP_2GHZ)
  636. setup_ht_cap(priv,
  637. &priv->sbands[IEEE80211_BAND_2GHZ].ht_cap);
  638. if (priv->ah->caps.hw_caps & ATH9K_HW_CAP_5GHZ)
  639. setup_ht_cap(priv,
  640. &priv->sbands[IEEE80211_BAND_5GHZ].ht_cap);
  641. }
  642. SET_IEEE80211_PERM_ADDR(hw, common->macaddr);
  643. }
  644. static int ath9k_init_firmware_version(struct ath9k_htc_priv *priv)
  645. {
  646. struct ieee80211_hw *hw = priv->hw;
  647. struct wmi_fw_version cmd_rsp;
  648. int ret;
  649. memset(&cmd_rsp, 0, sizeof(cmd_rsp));
  650. WMI_CMD(WMI_GET_FW_VERSION);
  651. if (ret)
  652. return -EINVAL;
  653. priv->fw_version_major = be16_to_cpu(cmd_rsp.major);
  654. priv->fw_version_minor = be16_to_cpu(cmd_rsp.minor);
  655. snprintf(hw->wiphy->fw_version, ETHTOOL_BUSINFO_LEN, "%d.%d",
  656. priv->fw_version_major,
  657. priv->fw_version_minor);
  658. dev_info(priv->dev, "ath9k_htc: FW Version: %d.%d\n",
  659. priv->fw_version_major,
  660. priv->fw_version_minor);
  661. /*
  662. * Check if the available FW matches the driver's
  663. * required version.
  664. */
  665. if (priv->fw_version_major != MAJOR_VERSION_REQ ||
  666. priv->fw_version_minor != MINOR_VERSION_REQ) {
  667. dev_err(priv->dev, "ath9k_htc: Please upgrade to FW version %d.%d\n",
  668. MAJOR_VERSION_REQ, MINOR_VERSION_REQ);
  669. return -EINVAL;
  670. }
  671. return 0;
  672. }
  673. static int ath9k_init_device(struct ath9k_htc_priv *priv,
  674. u16 devid, char *product, u32 drv_info)
  675. {
  676. struct ieee80211_hw *hw = priv->hw;
  677. struct ath_common *common;
  678. struct ath_hw *ah;
  679. int error = 0;
  680. struct ath_regulatory *reg;
  681. char hw_name[64];
  682. /* Bring up device */
  683. error = ath9k_init_priv(priv, devid, product, drv_info);
  684. if (error != 0)
  685. goto err_init;
  686. ah = priv->ah;
  687. common = ath9k_hw_common(ah);
  688. ath9k_set_hw_capab(priv, hw);
  689. error = ath9k_init_firmware_version(priv);
  690. if (error != 0)
  691. goto err_fw;
  692. /* Initialize regulatory */
  693. error = ath_regd_init(&common->regulatory, priv->hw->wiphy,
  694. ath9k_reg_notifier);
  695. if (error)
  696. goto err_regd;
  697. reg = &common->regulatory;
  698. /* Setup TX */
  699. error = ath9k_tx_init(priv);
  700. if (error != 0)
  701. goto err_tx;
  702. /* Setup RX */
  703. error = ath9k_rx_init(priv);
  704. if (error != 0)
  705. goto err_rx;
  706. #ifdef CONFIG_MAC80211_LEDS
  707. /* must be initialized before ieee80211_register_hw */
  708. priv->led_cdev.default_trigger = ieee80211_create_tpt_led_trigger(priv->hw,
  709. IEEE80211_TPT_LEDTRIG_FL_RADIO, ath9k_htc_tpt_blink,
  710. ARRAY_SIZE(ath9k_htc_tpt_blink));
  711. #endif
  712. /* Register with mac80211 */
  713. error = ieee80211_register_hw(hw);
  714. if (error)
  715. goto err_register;
  716. /* Handle world regulatory */
  717. if (!ath_is_world_regd(reg)) {
  718. error = regulatory_hint(hw->wiphy, reg->alpha2);
  719. if (error)
  720. goto err_world;
  721. }
  722. error = ath9k_htc_init_debug(priv->ah);
  723. if (error) {
  724. ath_err(common, "Unable to create debugfs files\n");
  725. goto err_world;
  726. }
  727. ath_dbg(common, CONFIG,
  728. "WMI:%d, BCN:%d, CAB:%d, UAPSD:%d, MGMT:%d, BE:%d, BK:%d, VI:%d, VO:%d\n",
  729. priv->wmi_cmd_ep,
  730. priv->beacon_ep,
  731. priv->cab_ep,
  732. priv->uapsd_ep,
  733. priv->mgmt_ep,
  734. priv->data_be_ep,
  735. priv->data_bk_ep,
  736. priv->data_vi_ep,
  737. priv->data_vo_ep);
  738. ath9k_hw_name(priv->ah, hw_name, sizeof(hw_name));
  739. wiphy_info(hw->wiphy, "%s\n", hw_name);
  740. ath9k_init_leds(priv);
  741. ath9k_start_rfkill_poll(priv);
  742. return 0;
  743. err_world:
  744. ieee80211_unregister_hw(hw);
  745. err_register:
  746. ath9k_rx_cleanup(priv);
  747. err_rx:
  748. ath9k_tx_cleanup(priv);
  749. err_tx:
  750. /* Nothing */
  751. err_regd:
  752. /* Nothing */
  753. err_fw:
  754. ath9k_deinit_priv(priv);
  755. err_init:
  756. return error;
  757. }
  758. int ath9k_htc_probe_device(struct htc_target *htc_handle, struct device *dev,
  759. u16 devid, char *product, u32 drv_info)
  760. {
  761. struct ieee80211_hw *hw;
  762. struct ath9k_htc_priv *priv;
  763. int ret;
  764. hw = ieee80211_alloc_hw(sizeof(struct ath9k_htc_priv), &ath9k_htc_ops);
  765. if (!hw)
  766. return -ENOMEM;
  767. priv = hw->priv;
  768. priv->hw = hw;
  769. priv->htc = htc_handle;
  770. priv->dev = dev;
  771. htc_handle->drv_priv = priv;
  772. SET_IEEE80211_DEV(hw, priv->dev);
  773. ret = ath9k_htc_wait_for_target(priv);
  774. if (ret)
  775. goto err_free;
  776. priv->wmi = ath9k_init_wmi(priv);
  777. if (!priv->wmi) {
  778. ret = -EINVAL;
  779. goto err_free;
  780. }
  781. ret = ath9k_init_htc_services(priv, devid, drv_info);
  782. if (ret)
  783. goto err_init;
  784. ret = ath9k_init_device(priv, devid, product, drv_info);
  785. if (ret)
  786. goto err_init;
  787. return 0;
  788. err_init:
  789. ath9k_deinit_wmi(priv);
  790. err_free:
  791. ieee80211_free_hw(hw);
  792. return ret;
  793. }
  794. void ath9k_htc_disconnect_device(struct htc_target *htc_handle, bool hotunplug)
  795. {
  796. if (htc_handle->drv_priv) {
  797. /* Check if the device has been yanked out. */
  798. if (hotunplug)
  799. htc_handle->drv_priv->ah->ah_flags |= AH_UNPLUGGED;
  800. ath9k_deinit_device(htc_handle->drv_priv);
  801. ath9k_deinit_wmi(htc_handle->drv_priv);
  802. ieee80211_free_hw(htc_handle->drv_priv->hw);
  803. }
  804. }
  805. #ifdef CONFIG_PM
  806. void ath9k_htc_suspend(struct htc_target *htc_handle)
  807. {
  808. ath9k_htc_setpower(htc_handle->drv_priv, ATH9K_PM_FULL_SLEEP);
  809. }
  810. int ath9k_htc_resume(struct htc_target *htc_handle)
  811. {
  812. struct ath9k_htc_priv *priv = htc_handle->drv_priv;
  813. int ret;
  814. ret = ath9k_htc_wait_for_target(priv);
  815. if (ret)
  816. return ret;
  817. ret = ath9k_init_htc_services(priv, priv->ah->hw_version.devid,
  818. priv->ah->hw_version.usbdev);
  819. return ret;
  820. }
  821. #endif
  822. static int __init ath9k_htc_init(void)
  823. {
  824. if (ath9k_hif_usb_init() < 0) {
  825. printk(KERN_ERR
  826. "ath9k_htc: No USB devices found,"
  827. " driver not installed.\n");
  828. return -ENODEV;
  829. }
  830. return 0;
  831. }
  832. module_init(ath9k_htc_init);
  833. static void __exit ath9k_htc_exit(void)
  834. {
  835. ath9k_hif_usb_exit();
  836. printk(KERN_INFO "ath9k_htc: Driver unloaded\n");
  837. }
  838. module_exit(ath9k_htc_exit);