htc_drv_init.c 26 KB

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