htc_drv_init.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  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, ATH_DBG_WMI,
  257. "REGISTER READ FAILED: (0x%04x, %d)\n",
  258. reg_offset, r);
  259. return -EIO;
  260. }
  261. return be32_to_cpu(val);
  262. }
  263. static void ath9k_multi_regread(void *hw_priv, u32 *addr,
  264. u32 *val, u16 count)
  265. {
  266. struct ath_hw *ah = (struct ath_hw *) hw_priv;
  267. struct ath_common *common = ath9k_hw_common(ah);
  268. struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;
  269. __be32 tmpaddr[8];
  270. __be32 tmpval[8];
  271. int i, ret;
  272. for (i = 0; i < count; i++) {
  273. tmpaddr[i] = cpu_to_be32(addr[i]);
  274. }
  275. ret = ath9k_wmi_cmd(priv->wmi, WMI_REG_READ_CMDID,
  276. (u8 *)tmpaddr , sizeof(u32) * count,
  277. (u8 *)tmpval, sizeof(u32) * count,
  278. 100);
  279. if (unlikely(ret)) {
  280. ath_dbg(common, ATH_DBG_WMI,
  281. "Multiple REGISTER READ FAILED (count: %d)\n", count);
  282. }
  283. for (i = 0; i < count; i++) {
  284. val[i] = be32_to_cpu(tmpval[i]);
  285. }
  286. }
  287. static void ath9k_regwrite_single(void *hw_priv, u32 val, u32 reg_offset)
  288. {
  289. struct ath_hw *ah = (struct ath_hw *) hw_priv;
  290. struct ath_common *common = ath9k_hw_common(ah);
  291. struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;
  292. const __be32 buf[2] = {
  293. cpu_to_be32(reg_offset),
  294. cpu_to_be32(val),
  295. };
  296. int r;
  297. r = ath9k_wmi_cmd(priv->wmi, WMI_REG_WRITE_CMDID,
  298. (u8 *) &buf, sizeof(buf),
  299. (u8 *) &val, sizeof(val),
  300. 100);
  301. if (unlikely(r)) {
  302. ath_dbg(common, ATH_DBG_WMI,
  303. "REGISTER WRITE FAILED:(0x%04x, %d)\n",
  304. reg_offset, r);
  305. }
  306. }
  307. static void ath9k_regwrite_buffer(void *hw_priv, u32 val, u32 reg_offset)
  308. {
  309. struct ath_hw *ah = (struct ath_hw *) hw_priv;
  310. struct ath_common *common = ath9k_hw_common(ah);
  311. struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;
  312. u32 rsp_status;
  313. int r;
  314. mutex_lock(&priv->wmi->multi_write_mutex);
  315. /* Store the register/value */
  316. priv->wmi->multi_write[priv->wmi->multi_write_idx].reg =
  317. cpu_to_be32(reg_offset);
  318. priv->wmi->multi_write[priv->wmi->multi_write_idx].val =
  319. cpu_to_be32(val);
  320. priv->wmi->multi_write_idx++;
  321. /* If the buffer is full, send it out. */
  322. if (priv->wmi->multi_write_idx == MAX_CMD_NUMBER) {
  323. r = ath9k_wmi_cmd(priv->wmi, WMI_REG_WRITE_CMDID,
  324. (u8 *) &priv->wmi->multi_write,
  325. sizeof(struct register_write) * priv->wmi->multi_write_idx,
  326. (u8 *) &rsp_status, sizeof(rsp_status),
  327. 100);
  328. if (unlikely(r)) {
  329. ath_dbg(common, ATH_DBG_WMI,
  330. "REGISTER WRITE FAILED, multi len: %d\n",
  331. priv->wmi->multi_write_idx);
  332. }
  333. priv->wmi->multi_write_idx = 0;
  334. }
  335. mutex_unlock(&priv->wmi->multi_write_mutex);
  336. }
  337. static void ath9k_regwrite(void *hw_priv, u32 val, u32 reg_offset)
  338. {
  339. struct ath_hw *ah = (struct ath_hw *) hw_priv;
  340. struct ath_common *common = ath9k_hw_common(ah);
  341. struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;
  342. if (atomic_read(&priv->wmi->mwrite_cnt))
  343. ath9k_regwrite_buffer(hw_priv, val, reg_offset);
  344. else
  345. ath9k_regwrite_single(hw_priv, val, reg_offset);
  346. }
  347. static void ath9k_enable_regwrite_buffer(void *hw_priv)
  348. {
  349. struct ath_hw *ah = (struct ath_hw *) hw_priv;
  350. struct ath_common *common = ath9k_hw_common(ah);
  351. struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;
  352. atomic_inc(&priv->wmi->mwrite_cnt);
  353. }
  354. static void ath9k_regwrite_flush(void *hw_priv)
  355. {
  356. struct ath_hw *ah = (struct ath_hw *) hw_priv;
  357. struct ath_common *common = ath9k_hw_common(ah);
  358. struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *) common->priv;
  359. u32 rsp_status;
  360. int r;
  361. atomic_dec(&priv->wmi->mwrite_cnt);
  362. mutex_lock(&priv->wmi->multi_write_mutex);
  363. if (priv->wmi->multi_write_idx) {
  364. r = ath9k_wmi_cmd(priv->wmi, WMI_REG_WRITE_CMDID,
  365. (u8 *) &priv->wmi->multi_write,
  366. sizeof(struct register_write) * priv->wmi->multi_write_idx,
  367. (u8 *) &rsp_status, sizeof(rsp_status),
  368. 100);
  369. if (unlikely(r)) {
  370. ath_dbg(common, ATH_DBG_WMI,
  371. "REGISTER WRITE FAILED, multi len: %d\n",
  372. priv->wmi->multi_write_idx);
  373. }
  374. priv->wmi->multi_write_idx = 0;
  375. }
  376. mutex_unlock(&priv->wmi->multi_write_mutex);
  377. }
  378. static u32 ath9k_reg_rmw(void *hw_priv, u32 reg_offset, u32 set, u32 clr)
  379. {
  380. u32 val;
  381. val = ath9k_regread(hw_priv, reg_offset);
  382. val &= ~clr;
  383. val |= set;
  384. ath9k_regwrite(hw_priv, val, reg_offset);
  385. return val;
  386. }
  387. static void ath_usb_read_cachesize(struct ath_common *common, int *csz)
  388. {
  389. *csz = L1_CACHE_BYTES >> 2;
  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. (void)REG_READ(ah, AR5416_EEPROM_OFFSET + (off << AR5416_EEPROM_S));
  395. if (!ath9k_hw_wait(ah,
  396. AR_EEPROM_STATUS_DATA,
  397. AR_EEPROM_STATUS_DATA_BUSY |
  398. AR_EEPROM_STATUS_DATA_PROT_ACCESS, 0,
  399. AH_WAIT_TIMEOUT))
  400. return false;
  401. *data = MS(REG_READ(ah, AR_EEPROM_STATUS_DATA),
  402. AR_EEPROM_STATUS_DATA_VAL);
  403. return true;
  404. }
  405. static const struct ath_bus_ops ath9k_usb_bus_ops = {
  406. .ath_bus_type = ATH_USB,
  407. .read_cachesize = ath_usb_read_cachesize,
  408. .eeprom_read = ath_usb_eeprom_read,
  409. };
  410. static void setup_ht_cap(struct ath9k_htc_priv *priv,
  411. struct ieee80211_sta_ht_cap *ht_info)
  412. {
  413. struct ath_common *common = ath9k_hw_common(priv->ah);
  414. u8 tx_streams, rx_streams;
  415. int i;
  416. ht_info->ht_supported = true;
  417. ht_info->cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
  418. IEEE80211_HT_CAP_SM_PS |
  419. IEEE80211_HT_CAP_SGI_40 |
  420. IEEE80211_HT_CAP_DSSSCCK40;
  421. if (priv->ah->caps.hw_caps & ATH9K_HW_CAP_SGI_20)
  422. ht_info->cap |= IEEE80211_HT_CAP_SGI_20;
  423. ht_info->cap |= (1 << IEEE80211_HT_CAP_RX_STBC_SHIFT);
  424. ht_info->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
  425. ht_info->ampdu_density = IEEE80211_HT_MPDU_DENSITY_8;
  426. memset(&ht_info->mcs, 0, sizeof(ht_info->mcs));
  427. /* ath9k_htc supports only 1 or 2 stream devices */
  428. tx_streams = ath9k_cmn_count_streams(common->tx_chainmask, 2);
  429. rx_streams = ath9k_cmn_count_streams(common->rx_chainmask, 2);
  430. ath_dbg(common, ATH_DBG_CONFIG,
  431. "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, WME_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, WME_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, WME_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, WME_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_crypto(struct ath9k_htc_priv *priv)
  479. {
  480. struct ath_common *common = ath9k_hw_common(priv->ah);
  481. int i = 0;
  482. /* Get the hardware key cache size. */
  483. common->keymax = AR_KEYTABLE_SIZE;
  484. if (priv->ah->misc_mode & AR_PCU_MIC_NEW_LOC_ENA)
  485. common->crypt_caps |= ATH_CRYPT_CAP_MIC_COMBINED;
  486. /*
  487. * Reset the key cache since some parts do not
  488. * reset the contents on initial power up.
  489. */
  490. for (i = 0; i < common->keymax; i++)
  491. ath_hw_keyreset(common, (u16) i);
  492. }
  493. static void ath9k_init_channels_rates(struct ath9k_htc_priv *priv)
  494. {
  495. if (priv->ah->caps.hw_caps & ATH9K_HW_CAP_2GHZ) {
  496. priv->sbands[IEEE80211_BAND_2GHZ].channels =
  497. ath9k_2ghz_channels;
  498. priv->sbands[IEEE80211_BAND_2GHZ].band = IEEE80211_BAND_2GHZ;
  499. priv->sbands[IEEE80211_BAND_2GHZ].n_channels =
  500. ARRAY_SIZE(ath9k_2ghz_channels);
  501. priv->sbands[IEEE80211_BAND_2GHZ].bitrates = ath9k_legacy_rates;
  502. priv->sbands[IEEE80211_BAND_2GHZ].n_bitrates =
  503. ARRAY_SIZE(ath9k_legacy_rates);
  504. }
  505. if (priv->ah->caps.hw_caps & ATH9K_HW_CAP_5GHZ) {
  506. priv->sbands[IEEE80211_BAND_5GHZ].channels = ath9k_5ghz_channels;
  507. priv->sbands[IEEE80211_BAND_5GHZ].band = IEEE80211_BAND_5GHZ;
  508. priv->sbands[IEEE80211_BAND_5GHZ].n_channels =
  509. ARRAY_SIZE(ath9k_5ghz_channels);
  510. priv->sbands[IEEE80211_BAND_5GHZ].bitrates =
  511. ath9k_legacy_rates + 4;
  512. priv->sbands[IEEE80211_BAND_5GHZ].n_bitrates =
  513. ARRAY_SIZE(ath9k_legacy_rates) - 4;
  514. }
  515. }
  516. static void ath9k_init_misc(struct ath9k_htc_priv *priv)
  517. {
  518. struct ath_common *common = ath9k_hw_common(priv->ah);
  519. common->tx_chainmask = priv->ah->caps.tx_chainmask;
  520. common->rx_chainmask = priv->ah->caps.rx_chainmask;
  521. memcpy(common->bssidmask, ath_bcast_mac, ETH_ALEN);
  522. priv->ah->opmode = NL80211_IFTYPE_STATION;
  523. }
  524. static void ath9k_init_btcoex(struct ath9k_htc_priv *priv)
  525. {
  526. int qnum;
  527. switch (priv->ah->btcoex_hw.scheme) {
  528. case ATH_BTCOEX_CFG_NONE:
  529. break;
  530. case ATH_BTCOEX_CFG_3WIRE:
  531. priv->ah->btcoex_hw.btactive_gpio = 7;
  532. priv->ah->btcoex_hw.btpriority_gpio = 6;
  533. priv->ah->btcoex_hw.wlanactive_gpio = 8;
  534. priv->btcoex.bt_stomp_type = ATH_BTCOEX_STOMP_LOW;
  535. ath9k_hw_btcoex_init_3wire(priv->ah);
  536. ath_htc_init_btcoex_work(priv);
  537. qnum = priv->hwq_map[WME_AC_BE];
  538. ath9k_hw_init_btcoex_hw(priv->ah, qnum);
  539. break;
  540. default:
  541. WARN_ON(1);
  542. break;
  543. }
  544. }
  545. static int ath9k_init_priv(struct ath9k_htc_priv *priv,
  546. u16 devid, char *product,
  547. u32 drv_info)
  548. {
  549. struct ath_hw *ah = NULL;
  550. struct ath_common *common;
  551. int i, ret = 0, csz = 0;
  552. priv->op_flags |= OP_INVALID;
  553. ah = kzalloc(sizeof(struct ath_hw), GFP_KERNEL);
  554. if (!ah)
  555. return -ENOMEM;
  556. ah->hw_version.devid = devid;
  557. ah->hw_version.subsysid = 0; /* FIXME */
  558. ah->hw_version.usbdev = drv_info;
  559. ah->ah_flags |= AH_USE_EEPROM;
  560. ah->reg_ops.read = ath9k_regread;
  561. ah->reg_ops.multi_read = ath9k_multi_regread;
  562. ah->reg_ops.write = ath9k_regwrite;
  563. ah->reg_ops.enable_write_buffer = ath9k_enable_regwrite_buffer;
  564. ah->reg_ops.write_flush = ath9k_regwrite_flush;
  565. ah->reg_ops.rmw = ath9k_reg_rmw;
  566. priv->ah = ah;
  567. common = ath9k_hw_common(ah);
  568. common->ops = &ah->reg_ops;
  569. common->bus_ops = &ath9k_usb_bus_ops;
  570. common->ah = ah;
  571. common->hw = priv->hw;
  572. common->priv = priv;
  573. common->debug_mask = ath9k_debug;
  574. spin_lock_init(&priv->beacon_lock);
  575. spin_lock_init(&priv->tx.tx_lock);
  576. mutex_init(&priv->mutex);
  577. mutex_init(&priv->htc_pm_lock);
  578. tasklet_init(&priv->rx_tasklet, ath9k_rx_tasklet,
  579. (unsigned long)priv);
  580. tasklet_init(&priv->tx_failed_tasklet, ath9k_tx_failed_tasklet,
  581. (unsigned long)priv);
  582. INIT_DELAYED_WORK(&priv->ani_work, ath9k_htc_ani_work);
  583. INIT_WORK(&priv->ps_work, ath9k_ps_work);
  584. INIT_WORK(&priv->fatal_work, ath9k_fatal_work);
  585. setup_timer(&priv->tx.cleanup_timer, ath9k_htc_tx_cleanup_timer,
  586. (unsigned long)priv);
  587. /*
  588. * Cache line size is used to size and align various
  589. * structures used to communicate with the hardware.
  590. */
  591. ath_read_cachesize(common, &csz);
  592. common->cachelsz = csz << 2; /* convert to bytes */
  593. ret = ath9k_hw_init(ah);
  594. if (ret) {
  595. ath_err(common,
  596. "Unable to initialize hardware; initialization status: %d\n",
  597. ret);
  598. goto err_hw;
  599. }
  600. ret = ath9k_init_queues(priv);
  601. if (ret)
  602. goto err_queues;
  603. for (i = 0; i < ATH9K_HTC_MAX_BCN_VIF; i++)
  604. priv->cur_beacon_conf.bslot[i] = NULL;
  605. ath9k_init_crypto(priv);
  606. ath9k_init_channels_rates(priv);
  607. ath9k_init_misc(priv);
  608. if (product && strncmp(product, ATH_HTC_BTCOEX_PRODUCT_ID, 5) == 0) {
  609. ah->btcoex_hw.scheme = ATH_BTCOEX_CFG_3WIRE;
  610. ath9k_init_btcoex(priv);
  611. }
  612. return 0;
  613. err_queues:
  614. ath9k_hw_deinit(ah);
  615. err_hw:
  616. kfree(ah);
  617. priv->ah = NULL;
  618. return ret;
  619. }
  620. static void ath9k_set_hw_capab(struct ath9k_htc_priv *priv,
  621. struct ieee80211_hw *hw)
  622. {
  623. struct ath_common *common = ath9k_hw_common(priv->ah);
  624. hw->flags = IEEE80211_HW_SIGNAL_DBM |
  625. IEEE80211_HW_AMPDU_AGGREGATION |
  626. IEEE80211_HW_SPECTRUM_MGMT |
  627. IEEE80211_HW_HAS_RATE_CONTROL |
  628. IEEE80211_HW_RX_INCLUDES_FCS |
  629. IEEE80211_HW_SUPPORTS_PS |
  630. IEEE80211_HW_PS_NULLFUNC_STACK |
  631. IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING;
  632. hw->wiphy->interface_modes =
  633. BIT(NL80211_IFTYPE_STATION) |
  634. BIT(NL80211_IFTYPE_ADHOC) |
  635. BIT(NL80211_IFTYPE_AP) |
  636. BIT(NL80211_IFTYPE_P2P_GO) |
  637. BIT(NL80211_IFTYPE_P2P_CLIENT);
  638. hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
  639. hw->queues = 4;
  640. hw->channel_change_time = 5000;
  641. hw->max_listen_interval = 10;
  642. hw->vif_data_size = sizeof(struct ath9k_htc_vif);
  643. hw->sta_data_size = sizeof(struct ath9k_htc_sta);
  644. /* tx_frame_hdr is larger than tx_mgmt_hdr anyway */
  645. hw->extra_tx_headroom = sizeof(struct tx_frame_hdr) +
  646. sizeof(struct htc_frame_hdr) + 4;
  647. if (priv->ah->caps.hw_caps & ATH9K_HW_CAP_2GHZ)
  648. hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
  649. &priv->sbands[IEEE80211_BAND_2GHZ];
  650. if (priv->ah->caps.hw_caps & ATH9K_HW_CAP_5GHZ)
  651. hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
  652. &priv->sbands[IEEE80211_BAND_5GHZ];
  653. if (priv->ah->caps.hw_caps & ATH9K_HW_CAP_HT) {
  654. if (priv->ah->caps.hw_caps & ATH9K_HW_CAP_2GHZ)
  655. setup_ht_cap(priv,
  656. &priv->sbands[IEEE80211_BAND_2GHZ].ht_cap);
  657. if (priv->ah->caps.hw_caps & ATH9K_HW_CAP_5GHZ)
  658. setup_ht_cap(priv,
  659. &priv->sbands[IEEE80211_BAND_5GHZ].ht_cap);
  660. }
  661. SET_IEEE80211_PERM_ADDR(hw, common->macaddr);
  662. }
  663. static int ath9k_init_firmware_version(struct ath9k_htc_priv *priv)
  664. {
  665. struct ieee80211_hw *hw = priv->hw;
  666. struct wmi_fw_version cmd_rsp;
  667. int ret;
  668. memset(&cmd_rsp, 0, sizeof(cmd_rsp));
  669. WMI_CMD(WMI_GET_FW_VERSION);
  670. if (ret)
  671. return -EINVAL;
  672. priv->fw_version_major = be16_to_cpu(cmd_rsp.major);
  673. priv->fw_version_minor = be16_to_cpu(cmd_rsp.minor);
  674. snprintf(hw->wiphy->fw_version, ETHTOOL_BUSINFO_LEN, "%d.%d",
  675. priv->fw_version_major,
  676. priv->fw_version_minor);
  677. dev_info(priv->dev, "ath9k_htc: FW Version: %d.%d\n",
  678. priv->fw_version_major,
  679. priv->fw_version_minor);
  680. /*
  681. * Check if the available FW matches the driver's
  682. * required version.
  683. */
  684. if (priv->fw_version_major != MAJOR_VERSION_REQ ||
  685. priv->fw_version_minor != MINOR_VERSION_REQ) {
  686. dev_err(priv->dev, "ath9k_htc: Please upgrade to FW version %d.%d\n",
  687. MAJOR_VERSION_REQ, MINOR_VERSION_REQ);
  688. return -EINVAL;
  689. }
  690. return 0;
  691. }
  692. static int ath9k_init_device(struct ath9k_htc_priv *priv,
  693. u16 devid, char *product, u32 drv_info)
  694. {
  695. struct ieee80211_hw *hw = priv->hw;
  696. struct ath_common *common;
  697. struct ath_hw *ah;
  698. int error = 0;
  699. struct ath_regulatory *reg;
  700. char hw_name[64];
  701. /* Bring up device */
  702. error = ath9k_init_priv(priv, devid, product, drv_info);
  703. if (error != 0)
  704. goto err_init;
  705. ah = priv->ah;
  706. common = ath9k_hw_common(ah);
  707. ath9k_set_hw_capab(priv, hw);
  708. error = ath9k_init_firmware_version(priv);
  709. if (error != 0)
  710. goto err_fw;
  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. reg = &common->regulatory;
  717. /* Setup TX */
  718. error = ath9k_tx_init(priv);
  719. if (error != 0)
  720. goto err_tx;
  721. /* Setup RX */
  722. error = ath9k_rx_init(priv);
  723. if (error != 0)
  724. goto err_rx;
  725. #ifdef CONFIG_MAC80211_LEDS
  726. /* must be initialized before ieee80211_register_hw */
  727. priv->led_cdev.default_trigger = ieee80211_create_tpt_led_trigger(priv->hw,
  728. IEEE80211_TPT_LEDTRIG_FL_RADIO, ath9k_htc_tpt_blink,
  729. ARRAY_SIZE(ath9k_htc_tpt_blink));
  730. #endif
  731. /* Register with mac80211 */
  732. error = ieee80211_register_hw(hw);
  733. if (error)
  734. goto err_register;
  735. /* Handle world regulatory */
  736. if (!ath_is_world_regd(reg)) {
  737. error = regulatory_hint(hw->wiphy, reg->alpha2);
  738. if (error)
  739. goto err_world;
  740. }
  741. error = ath9k_htc_init_debug(priv->ah);
  742. if (error) {
  743. ath_err(common, "Unable to create debugfs files\n");
  744. goto err_world;
  745. }
  746. ath_dbg(common, ATH_DBG_CONFIG,
  747. "WMI:%d, BCN:%d, CAB:%d, UAPSD:%d, MGMT:%d, "
  748. "BE:%d, BK:%d, VI:%d, VO:%d\n",
  749. priv->wmi_cmd_ep,
  750. priv->beacon_ep,
  751. priv->cab_ep,
  752. priv->uapsd_ep,
  753. priv->mgmt_ep,
  754. priv->data_be_ep,
  755. priv->data_bk_ep,
  756. priv->data_vi_ep,
  757. priv->data_vo_ep);
  758. ath9k_hw_name(priv->ah, hw_name, sizeof(hw_name));
  759. wiphy_info(hw->wiphy, "%s\n", hw_name);
  760. ath9k_init_leds(priv);
  761. ath9k_start_rfkill_poll(priv);
  762. return 0;
  763. err_world:
  764. ieee80211_unregister_hw(hw);
  765. err_register:
  766. ath9k_rx_cleanup(priv);
  767. err_rx:
  768. ath9k_tx_cleanup(priv);
  769. err_tx:
  770. /* Nothing */
  771. err_regd:
  772. /* Nothing */
  773. err_fw:
  774. ath9k_deinit_priv(priv);
  775. err_init:
  776. return error;
  777. }
  778. int ath9k_htc_probe_device(struct htc_target *htc_handle, struct device *dev,
  779. u16 devid, char *product, u32 drv_info)
  780. {
  781. struct ieee80211_hw *hw;
  782. struct ath9k_htc_priv *priv;
  783. int ret;
  784. hw = ieee80211_alloc_hw(sizeof(struct ath9k_htc_priv), &ath9k_htc_ops);
  785. if (!hw)
  786. return -ENOMEM;
  787. priv = hw->priv;
  788. priv->hw = hw;
  789. priv->htc = htc_handle;
  790. priv->dev = dev;
  791. htc_handle->drv_priv = priv;
  792. SET_IEEE80211_DEV(hw, priv->dev);
  793. ret = ath9k_htc_wait_for_target(priv);
  794. if (ret)
  795. goto err_free;
  796. priv->wmi = ath9k_init_wmi(priv);
  797. if (!priv->wmi) {
  798. ret = -EINVAL;
  799. goto err_free;
  800. }
  801. ret = ath9k_init_htc_services(priv, devid, drv_info);
  802. if (ret)
  803. goto err_init;
  804. ret = ath9k_init_device(priv, devid, product, drv_info);
  805. if (ret)
  806. goto err_init;
  807. return 0;
  808. err_init:
  809. ath9k_deinit_wmi(priv);
  810. err_free:
  811. ieee80211_free_hw(hw);
  812. return ret;
  813. }
  814. void ath9k_htc_disconnect_device(struct htc_target *htc_handle, bool hotunplug)
  815. {
  816. if (htc_handle->drv_priv) {
  817. /* Check if the device has been yanked out. */
  818. if (hotunplug)
  819. htc_handle->drv_priv->ah->ah_flags |= AH_UNPLUGGED;
  820. ath9k_deinit_device(htc_handle->drv_priv);
  821. ath9k_deinit_wmi(htc_handle->drv_priv);
  822. ieee80211_free_hw(htc_handle->drv_priv->hw);
  823. }
  824. }
  825. #ifdef CONFIG_PM
  826. void ath9k_htc_suspend(struct htc_target *htc_handle)
  827. {
  828. ath9k_htc_setpower(htc_handle->drv_priv, ATH9K_PM_FULL_SLEEP);
  829. }
  830. int ath9k_htc_resume(struct htc_target *htc_handle)
  831. {
  832. struct ath9k_htc_priv *priv = htc_handle->drv_priv;
  833. int ret;
  834. ret = ath9k_htc_wait_for_target(priv);
  835. if (ret)
  836. return ret;
  837. ret = ath9k_init_htc_services(priv, priv->ah->hw_version.devid,
  838. priv->ah->hw_version.usbdev);
  839. return ret;
  840. }
  841. #endif
  842. static int __init ath9k_htc_init(void)
  843. {
  844. if (ath9k_hif_usb_init() < 0) {
  845. printk(KERN_ERR
  846. "ath9k_htc: No USB devices found,"
  847. " driver not installed.\n");
  848. return -ENODEV;
  849. }
  850. return 0;
  851. }
  852. module_init(ath9k_htc_init);
  853. static void __exit ath9k_htc_exit(void)
  854. {
  855. ath9k_hif_usb_exit();
  856. printk(KERN_INFO "ath9k_htc: Driver unloaded\n");
  857. }
  858. module_exit(ath9k_htc_exit);