rsi_91x_main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /**
  2. * Copyright (c) 2014 Redpine Signals 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 <linux/module.h>
  18. #include <linux/firmware.h>
  19. #include <net/rsi_91x.h>
  20. #include "rsi_mgmt.h"
  21. #include "rsi_common.h"
  22. #include "rsi_coex.h"
  23. #include "rsi_hal.h"
  24. u32 rsi_zone_enabled = /* INFO_ZONE |
  25. INIT_ZONE |
  26. MGMT_TX_ZONE |
  27. MGMT_RX_ZONE |
  28. DATA_TX_ZONE |
  29. DATA_RX_ZONE |
  30. FSM_ZONE |
  31. ISR_ZONE | */
  32. ERR_ZONE |
  33. 0;
  34. EXPORT_SYMBOL_GPL(rsi_zone_enabled);
  35. #ifdef CONFIG_RSI_COEX
  36. static struct rsi_proto_ops g_proto_ops = {
  37. .coex_send_pkt = rsi_coex_send_pkt,
  38. .get_host_intf = rsi_get_host_intf,
  39. .set_bt_context = rsi_set_bt_context,
  40. };
  41. #endif
  42. /**
  43. * rsi_dbg() - This function outputs informational messages.
  44. * @zone: Zone of interest for output message.
  45. * @fmt: printf-style format for output message.
  46. *
  47. * Return: none
  48. */
  49. void rsi_dbg(u32 zone, const char *fmt, ...)
  50. {
  51. struct va_format vaf;
  52. va_list args;
  53. va_start(args, fmt);
  54. vaf.fmt = fmt;
  55. vaf.va = &args;
  56. if (zone & rsi_zone_enabled)
  57. pr_info("%pV", &vaf);
  58. va_end(args);
  59. }
  60. EXPORT_SYMBOL_GPL(rsi_dbg);
  61. static char *opmode_str(int oper_mode)
  62. {
  63. switch (oper_mode) {
  64. case DEV_OPMODE_WIFI_ALONE:
  65. return "Wi-Fi alone";
  66. case DEV_OPMODE_BT_ALONE:
  67. return "BT EDR alone";
  68. case DEV_OPMODE_BT_LE_ALONE:
  69. return "BT LE alone";
  70. case DEV_OPMODE_BT_DUAL:
  71. return "BT Dual";
  72. case DEV_OPMODE_STA_BT:
  73. return "Wi-Fi STA + BT EDR";
  74. case DEV_OPMODE_STA_BT_LE:
  75. return "Wi-Fi STA + BT LE";
  76. case DEV_OPMODE_STA_BT_DUAL:
  77. return "Wi-Fi STA + BT DUAL";
  78. case DEV_OPMODE_AP_BT:
  79. return "Wi-Fi AP + BT EDR";
  80. case DEV_OPMODE_AP_BT_DUAL:
  81. return "Wi-Fi AP + BT DUAL";
  82. }
  83. return "Unknown";
  84. }
  85. void rsi_print_version(struct rsi_common *common)
  86. {
  87. rsi_dbg(ERR_ZONE, "================================================\n");
  88. rsi_dbg(ERR_ZONE, "================ RSI Version Info ==============\n");
  89. rsi_dbg(ERR_ZONE, "================================================\n");
  90. rsi_dbg(ERR_ZONE, "FW Version\t: %d.%d.%d\n",
  91. common->lmac_ver.major, common->lmac_ver.minor,
  92. common->lmac_ver.release_num);
  93. rsi_dbg(ERR_ZONE, "Operating mode\t: %d [%s]",
  94. common->oper_mode, opmode_str(common->oper_mode));
  95. rsi_dbg(ERR_ZONE, "Firmware file\t: %s", common->priv->fw_file_name);
  96. rsi_dbg(ERR_ZONE, "================================================\n");
  97. }
  98. /**
  99. * rsi_prepare_skb() - This function prepares the skb.
  100. * @common: Pointer to the driver private structure.
  101. * @buffer: Pointer to the packet data.
  102. * @pkt_len: Length of the packet.
  103. * @extended_desc: Extended descriptor.
  104. *
  105. * Return: Successfully skb.
  106. */
  107. static struct sk_buff *rsi_prepare_skb(struct rsi_common *common,
  108. u8 *buffer,
  109. u32 pkt_len,
  110. u8 extended_desc)
  111. {
  112. struct ieee80211_tx_info *info;
  113. struct sk_buff *skb = NULL;
  114. u8 payload_offset;
  115. struct ieee80211_vif *vif;
  116. struct ieee80211_hdr *wh;
  117. if (WARN(!pkt_len, "%s: Dummy pkt received", __func__))
  118. return NULL;
  119. if (pkt_len > (RSI_RCV_BUFFER_LEN * 4)) {
  120. rsi_dbg(ERR_ZONE, "%s: Pkt size > max rx buf size %d\n",
  121. __func__, pkt_len);
  122. pkt_len = RSI_RCV_BUFFER_LEN * 4;
  123. }
  124. pkt_len -= extended_desc;
  125. skb = dev_alloc_skb(pkt_len + FRAME_DESC_SZ);
  126. if (skb == NULL)
  127. return NULL;
  128. payload_offset = (extended_desc + FRAME_DESC_SZ);
  129. skb_put(skb, pkt_len);
  130. memcpy((skb->data), (buffer + payload_offset), skb->len);
  131. wh = (struct ieee80211_hdr *)skb->data;
  132. vif = rsi_get_vif(common->priv, wh->addr1);
  133. info = IEEE80211_SKB_CB(skb);
  134. return skb;
  135. }
  136. /**
  137. * rsi_read_pkt() - This function reads frames from the card.
  138. * @common: Pointer to the driver private structure.
  139. * @rcv_pkt_len: Received pkt length. In case of USB it is 0.
  140. *
  141. * Return: 0 on success, -1 on failure.
  142. */
  143. int rsi_read_pkt(struct rsi_common *common, u8 *rx_pkt, s32 rcv_pkt_len)
  144. {
  145. u8 *frame_desc = NULL, extended_desc = 0;
  146. u32 index, length = 0, queueno = 0;
  147. u16 actual_length = 0, offset;
  148. struct sk_buff *skb = NULL;
  149. #ifdef CONFIG_RSI_COEX
  150. u8 bt_pkt_type;
  151. #endif
  152. index = 0;
  153. do {
  154. frame_desc = &rx_pkt[index];
  155. actual_length = *(u16 *)&frame_desc[0];
  156. offset = *(u16 *)&frame_desc[2];
  157. queueno = rsi_get_queueno(frame_desc, offset);
  158. length = rsi_get_length(frame_desc, offset);
  159. /* Extended descriptor is valid for WLAN queues only */
  160. if (queueno == RSI_WIFI_DATA_Q || queueno == RSI_WIFI_MGMT_Q)
  161. extended_desc = rsi_get_extended_desc(frame_desc,
  162. offset);
  163. switch (queueno) {
  164. case RSI_COEX_Q:
  165. #ifdef CONFIG_RSI_COEX
  166. if (common->coex_mode > 1)
  167. rsi_coex_recv_pkt(common, frame_desc + offset);
  168. else
  169. #endif
  170. rsi_mgmt_pkt_recv(common,
  171. (frame_desc + offset));
  172. break;
  173. case RSI_WIFI_DATA_Q:
  174. skb = rsi_prepare_skb(common,
  175. (frame_desc + offset),
  176. length,
  177. extended_desc);
  178. if (skb == NULL)
  179. goto fail;
  180. rsi_indicate_pkt_to_os(common, skb);
  181. break;
  182. case RSI_WIFI_MGMT_Q:
  183. rsi_mgmt_pkt_recv(common, (frame_desc + offset));
  184. break;
  185. #ifdef CONFIG_RSI_COEX
  186. case RSI_BT_MGMT_Q:
  187. case RSI_BT_DATA_Q:
  188. #define BT_RX_PKT_TYPE_OFST 14
  189. #define BT_CARD_READY_IND 0x89
  190. bt_pkt_type = frame_desc[offset + BT_RX_PKT_TYPE_OFST];
  191. if (bt_pkt_type == BT_CARD_READY_IND) {
  192. rsi_dbg(INFO_ZONE, "BT Card ready recvd\n");
  193. if (rsi_bt_ops.attach(common, &g_proto_ops))
  194. rsi_dbg(ERR_ZONE,
  195. "Failed to attach BT module\n");
  196. } else {
  197. if (common->bt_adapter)
  198. rsi_bt_ops.recv_pkt(common->bt_adapter,
  199. frame_desc + offset);
  200. }
  201. break;
  202. #endif
  203. default:
  204. rsi_dbg(ERR_ZONE, "%s: pkt from invalid queue: %d\n",
  205. __func__, queueno);
  206. goto fail;
  207. }
  208. index += actual_length;
  209. rcv_pkt_len -= actual_length;
  210. } while (rcv_pkt_len > 0);
  211. return 0;
  212. fail:
  213. return -EINVAL;
  214. }
  215. EXPORT_SYMBOL_GPL(rsi_read_pkt);
  216. /**
  217. * rsi_tx_scheduler_thread() - This function is a kernel thread to send the
  218. * packets to the device.
  219. * @common: Pointer to the driver private structure.
  220. *
  221. * Return: None.
  222. */
  223. static void rsi_tx_scheduler_thread(struct rsi_common *common)
  224. {
  225. struct rsi_hw *adapter = common->priv;
  226. u32 timeout = EVENT_WAIT_FOREVER;
  227. do {
  228. if (adapter->determine_event_timeout)
  229. timeout = adapter->determine_event_timeout(adapter);
  230. rsi_wait_event(&common->tx_thread.event, timeout);
  231. rsi_reset_event(&common->tx_thread.event);
  232. if (common->init_done)
  233. rsi_core_qos_processor(common);
  234. } while (atomic_read(&common->tx_thread.thread_done) == 0);
  235. complete_and_exit(&common->tx_thread.completion, 0);
  236. }
  237. #ifdef CONFIG_RSI_COEX
  238. enum rsi_host_intf rsi_get_host_intf(void *priv)
  239. {
  240. struct rsi_common *common = (struct rsi_common *)priv;
  241. return common->priv->rsi_host_intf;
  242. }
  243. void rsi_set_bt_context(void *priv, void *bt_context)
  244. {
  245. struct rsi_common *common = (struct rsi_common *)priv;
  246. common->bt_adapter = bt_context;
  247. }
  248. #endif
  249. /**
  250. * rsi_91x_init() - This function initializes os interface operations.
  251. * @void: Void.
  252. *
  253. * Return: Pointer to the adapter structure on success, NULL on failure .
  254. */
  255. struct rsi_hw *rsi_91x_init(u16 oper_mode)
  256. {
  257. struct rsi_hw *adapter = NULL;
  258. struct rsi_common *common = NULL;
  259. u8 ii = 0;
  260. adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
  261. if (!adapter)
  262. return NULL;
  263. adapter->priv = kzalloc(sizeof(*common), GFP_KERNEL);
  264. if (adapter->priv == NULL) {
  265. rsi_dbg(ERR_ZONE, "%s: Failed in allocation of memory\n",
  266. __func__);
  267. kfree(adapter);
  268. return NULL;
  269. } else {
  270. common = adapter->priv;
  271. common->priv = adapter;
  272. }
  273. for (ii = 0; ii < NUM_SOFT_QUEUES; ii++)
  274. skb_queue_head_init(&common->tx_queue[ii]);
  275. rsi_init_event(&common->tx_thread.event);
  276. mutex_init(&common->mutex);
  277. mutex_init(&common->tx_lock);
  278. mutex_init(&common->rx_lock);
  279. mutex_init(&common->tx_bus_mutex);
  280. if (rsi_create_kthread(common,
  281. &common->tx_thread,
  282. rsi_tx_scheduler_thread,
  283. "Tx-Thread")) {
  284. rsi_dbg(ERR_ZONE, "%s: Unable to init tx thrd\n", __func__);
  285. goto err;
  286. }
  287. rsi_default_ps_params(adapter);
  288. spin_lock_init(&adapter->ps_lock);
  289. timer_setup(&common->roc_timer, rsi_roc_timeout, 0);
  290. init_completion(&common->wlan_init_completion);
  291. adapter->device_model = RSI_DEV_9113;
  292. common->oper_mode = oper_mode;
  293. /* Determine coex mode */
  294. switch (common->oper_mode) {
  295. case DEV_OPMODE_STA_BT_DUAL:
  296. case DEV_OPMODE_STA_BT:
  297. case DEV_OPMODE_STA_BT_LE:
  298. case DEV_OPMODE_BT_ALONE:
  299. case DEV_OPMODE_BT_LE_ALONE:
  300. case DEV_OPMODE_BT_DUAL:
  301. common->coex_mode = 2;
  302. break;
  303. case DEV_OPMODE_AP_BT_DUAL:
  304. case DEV_OPMODE_AP_BT:
  305. common->coex_mode = 4;
  306. break;
  307. case DEV_OPMODE_WIFI_ALONE:
  308. common->coex_mode = 1;
  309. break;
  310. default:
  311. common->oper_mode = 1;
  312. common->coex_mode = 1;
  313. }
  314. rsi_dbg(INFO_ZONE, "%s: oper_mode = %d, coex_mode = %d\n",
  315. __func__, common->oper_mode, common->coex_mode);
  316. adapter->device_model = RSI_DEV_9113;
  317. #ifdef CONFIG_RSI_COEX
  318. if (common->coex_mode > 1) {
  319. if (rsi_coex_attach(common)) {
  320. rsi_dbg(ERR_ZONE, "Failed to init coex module\n");
  321. goto err;
  322. }
  323. }
  324. #endif
  325. common->init_done = true;
  326. return adapter;
  327. err:
  328. kfree(common);
  329. kfree(adapter);
  330. return NULL;
  331. }
  332. EXPORT_SYMBOL_GPL(rsi_91x_init);
  333. /**
  334. * rsi_91x_deinit() - This function de-intializes os intf operations.
  335. * @adapter: Pointer to the adapter structure.
  336. *
  337. * Return: None.
  338. */
  339. void rsi_91x_deinit(struct rsi_hw *adapter)
  340. {
  341. struct rsi_common *common = adapter->priv;
  342. u8 ii;
  343. rsi_dbg(INFO_ZONE, "%s: Performing deinit os ops\n", __func__);
  344. rsi_kill_thread(&common->tx_thread);
  345. for (ii = 0; ii < NUM_SOFT_QUEUES; ii++)
  346. skb_queue_purge(&common->tx_queue[ii]);
  347. #ifdef CONFIG_RSI_COEX
  348. if (common->coex_mode > 1) {
  349. if (common->bt_adapter) {
  350. rsi_bt_ops.detach(common->bt_adapter);
  351. common->bt_adapter = NULL;
  352. }
  353. rsi_coex_detach(common);
  354. }
  355. #endif
  356. common->init_done = false;
  357. kfree(common);
  358. kfree(adapter->rsi_dev);
  359. kfree(adapter);
  360. }
  361. EXPORT_SYMBOL_GPL(rsi_91x_deinit);
  362. /**
  363. * rsi_91x_hal_module_init() - This function is invoked when the module is
  364. * loaded into the kernel.
  365. * It registers the client driver.
  366. * @void: Void.
  367. *
  368. * Return: 0 on success, -1 on failure.
  369. */
  370. static int rsi_91x_hal_module_init(void)
  371. {
  372. rsi_dbg(INIT_ZONE, "%s: Module init called\n", __func__);
  373. return 0;
  374. }
  375. /**
  376. * rsi_91x_hal_module_exit() - This function is called at the time of
  377. * removing/unloading the module.
  378. * It unregisters the client driver.
  379. * @void: Void.
  380. *
  381. * Return: None.
  382. */
  383. static void rsi_91x_hal_module_exit(void)
  384. {
  385. rsi_dbg(INIT_ZONE, "%s: Module exit called\n", __func__);
  386. }
  387. module_init(rsi_91x_hal_module_init);
  388. module_exit(rsi_91x_hal_module_exit);
  389. MODULE_AUTHOR("Redpine Signals Inc");
  390. MODULE_DESCRIPTION("Station driver for RSI 91x devices");
  391. MODULE_SUPPORTED_DEVICE("RSI-91x");
  392. MODULE_VERSION("0.1");
  393. MODULE_LICENSE("Dual BSD/GPL");