rsi_91x_main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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 sk_buff *skb = NULL;
  113. u8 payload_offset;
  114. if (WARN(!pkt_len, "%s: Dummy pkt received", __func__))
  115. return NULL;
  116. if (pkt_len > (RSI_RCV_BUFFER_LEN * 4)) {
  117. rsi_dbg(ERR_ZONE, "%s: Pkt size > max rx buf size %d\n",
  118. __func__, pkt_len);
  119. pkt_len = RSI_RCV_BUFFER_LEN * 4;
  120. }
  121. pkt_len -= extended_desc;
  122. skb = dev_alloc_skb(pkt_len + FRAME_DESC_SZ);
  123. if (skb == NULL)
  124. return NULL;
  125. payload_offset = (extended_desc + FRAME_DESC_SZ);
  126. skb_put(skb, pkt_len);
  127. memcpy((skb->data), (buffer + payload_offset), skb->len);
  128. return skb;
  129. }
  130. /**
  131. * rsi_read_pkt() - This function reads frames from the card.
  132. * @common: Pointer to the driver private structure.
  133. * @rcv_pkt_len: Received pkt length. In case of USB it is 0.
  134. *
  135. * Return: 0 on success, -1 on failure.
  136. */
  137. int rsi_read_pkt(struct rsi_common *common, u8 *rx_pkt, s32 rcv_pkt_len)
  138. {
  139. u8 *frame_desc = NULL, extended_desc = 0;
  140. u32 index, length = 0, queueno = 0;
  141. u16 actual_length = 0, offset;
  142. struct sk_buff *skb = NULL;
  143. #ifdef CONFIG_RSI_COEX
  144. u8 bt_pkt_type;
  145. #endif
  146. index = 0;
  147. do {
  148. frame_desc = &rx_pkt[index];
  149. actual_length = *(u16 *)&frame_desc[0];
  150. offset = *(u16 *)&frame_desc[2];
  151. queueno = rsi_get_queueno(frame_desc, offset);
  152. length = rsi_get_length(frame_desc, offset);
  153. /* Extended descriptor is valid for WLAN queues only */
  154. if (queueno == RSI_WIFI_DATA_Q || queueno == RSI_WIFI_MGMT_Q)
  155. extended_desc = rsi_get_extended_desc(frame_desc,
  156. offset);
  157. switch (queueno) {
  158. case RSI_COEX_Q:
  159. #ifdef CONFIG_RSI_COEX
  160. if (common->coex_mode > 1)
  161. rsi_coex_recv_pkt(common, frame_desc + offset);
  162. else
  163. #endif
  164. rsi_mgmt_pkt_recv(common,
  165. (frame_desc + offset));
  166. break;
  167. case RSI_WIFI_DATA_Q:
  168. skb = rsi_prepare_skb(common,
  169. (frame_desc + offset),
  170. length,
  171. extended_desc);
  172. if (skb == NULL)
  173. goto fail;
  174. rsi_indicate_pkt_to_os(common, skb);
  175. break;
  176. case RSI_WIFI_MGMT_Q:
  177. rsi_mgmt_pkt_recv(common, (frame_desc + offset));
  178. break;
  179. #ifdef CONFIG_RSI_COEX
  180. case RSI_BT_MGMT_Q:
  181. case RSI_BT_DATA_Q:
  182. #define BT_RX_PKT_TYPE_OFST 14
  183. #define BT_CARD_READY_IND 0x89
  184. bt_pkt_type = frame_desc[offset + BT_RX_PKT_TYPE_OFST];
  185. if (bt_pkt_type == BT_CARD_READY_IND) {
  186. rsi_dbg(INFO_ZONE, "BT Card ready recvd\n");
  187. if (rsi_bt_ops.attach(common, &g_proto_ops))
  188. rsi_dbg(ERR_ZONE,
  189. "Failed to attach BT module\n");
  190. } else {
  191. if (common->bt_adapter)
  192. rsi_bt_ops.recv_pkt(common->bt_adapter,
  193. frame_desc + offset);
  194. }
  195. break;
  196. #endif
  197. default:
  198. rsi_dbg(ERR_ZONE, "%s: pkt from invalid queue: %d\n",
  199. __func__, queueno);
  200. goto fail;
  201. }
  202. index += actual_length;
  203. rcv_pkt_len -= actual_length;
  204. } while (rcv_pkt_len > 0);
  205. return 0;
  206. fail:
  207. return -EINVAL;
  208. }
  209. EXPORT_SYMBOL_GPL(rsi_read_pkt);
  210. /**
  211. * rsi_tx_scheduler_thread() - This function is a kernel thread to send the
  212. * packets to the device.
  213. * @common: Pointer to the driver private structure.
  214. *
  215. * Return: None.
  216. */
  217. static void rsi_tx_scheduler_thread(struct rsi_common *common)
  218. {
  219. struct rsi_hw *adapter = common->priv;
  220. u32 timeout = EVENT_WAIT_FOREVER;
  221. do {
  222. if (adapter->determine_event_timeout)
  223. timeout = adapter->determine_event_timeout(adapter);
  224. rsi_wait_event(&common->tx_thread.event, timeout);
  225. rsi_reset_event(&common->tx_thread.event);
  226. if (common->init_done)
  227. rsi_core_qos_processor(common);
  228. } while (atomic_read(&common->tx_thread.thread_done) == 0);
  229. complete_and_exit(&common->tx_thread.completion, 0);
  230. }
  231. #ifdef CONFIG_RSI_COEX
  232. enum rsi_host_intf rsi_get_host_intf(void *priv)
  233. {
  234. struct rsi_common *common = (struct rsi_common *)priv;
  235. return common->priv->rsi_host_intf;
  236. }
  237. void rsi_set_bt_context(void *priv, void *bt_context)
  238. {
  239. struct rsi_common *common = (struct rsi_common *)priv;
  240. common->bt_adapter = bt_context;
  241. }
  242. #endif
  243. /**
  244. * rsi_91x_init() - This function initializes os interface operations.
  245. * @void: Void.
  246. *
  247. * Return: Pointer to the adapter structure on success, NULL on failure .
  248. */
  249. struct rsi_hw *rsi_91x_init(u16 oper_mode)
  250. {
  251. struct rsi_hw *adapter = NULL;
  252. struct rsi_common *common = NULL;
  253. u8 ii = 0;
  254. adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
  255. if (!adapter)
  256. return NULL;
  257. adapter->priv = kzalloc(sizeof(*common), GFP_KERNEL);
  258. if (adapter->priv == NULL) {
  259. rsi_dbg(ERR_ZONE, "%s: Failed in allocation of memory\n",
  260. __func__);
  261. kfree(adapter);
  262. return NULL;
  263. } else {
  264. common = adapter->priv;
  265. common->priv = adapter;
  266. }
  267. for (ii = 0; ii < NUM_SOFT_QUEUES; ii++)
  268. skb_queue_head_init(&common->tx_queue[ii]);
  269. rsi_init_event(&common->tx_thread.event);
  270. mutex_init(&common->mutex);
  271. mutex_init(&common->tx_lock);
  272. mutex_init(&common->rx_lock);
  273. mutex_init(&common->tx_bus_mutex);
  274. if (rsi_create_kthread(common,
  275. &common->tx_thread,
  276. rsi_tx_scheduler_thread,
  277. "Tx-Thread")) {
  278. rsi_dbg(ERR_ZONE, "%s: Unable to init tx thrd\n", __func__);
  279. goto err;
  280. }
  281. rsi_default_ps_params(adapter);
  282. init_bgscan_params(common);
  283. spin_lock_init(&adapter->ps_lock);
  284. timer_setup(&common->roc_timer, rsi_roc_timeout, 0);
  285. init_completion(&common->wlan_init_completion);
  286. adapter->device_model = RSI_DEV_9113;
  287. common->oper_mode = oper_mode;
  288. /* Determine coex mode */
  289. switch (common->oper_mode) {
  290. case DEV_OPMODE_STA_BT_DUAL:
  291. case DEV_OPMODE_STA_BT:
  292. case DEV_OPMODE_STA_BT_LE:
  293. case DEV_OPMODE_BT_ALONE:
  294. case DEV_OPMODE_BT_LE_ALONE:
  295. case DEV_OPMODE_BT_DUAL:
  296. common->coex_mode = 2;
  297. break;
  298. case DEV_OPMODE_AP_BT_DUAL:
  299. case DEV_OPMODE_AP_BT:
  300. common->coex_mode = 4;
  301. break;
  302. case DEV_OPMODE_WIFI_ALONE:
  303. common->coex_mode = 1;
  304. break;
  305. default:
  306. common->oper_mode = 1;
  307. common->coex_mode = 1;
  308. }
  309. rsi_dbg(INFO_ZONE, "%s: oper_mode = %d, coex_mode = %d\n",
  310. __func__, common->oper_mode, common->coex_mode);
  311. adapter->device_model = RSI_DEV_9113;
  312. #ifdef CONFIG_RSI_COEX
  313. if (common->coex_mode > 1) {
  314. if (rsi_coex_attach(common)) {
  315. rsi_dbg(ERR_ZONE, "Failed to init coex module\n");
  316. goto err;
  317. }
  318. }
  319. #endif
  320. common->init_done = true;
  321. return adapter;
  322. err:
  323. kfree(common);
  324. kfree(adapter);
  325. return NULL;
  326. }
  327. EXPORT_SYMBOL_GPL(rsi_91x_init);
  328. /**
  329. * rsi_91x_deinit() - This function de-intializes os intf operations.
  330. * @adapter: Pointer to the adapter structure.
  331. *
  332. * Return: None.
  333. */
  334. void rsi_91x_deinit(struct rsi_hw *adapter)
  335. {
  336. struct rsi_common *common = adapter->priv;
  337. u8 ii;
  338. rsi_dbg(INFO_ZONE, "%s: Performing deinit os ops\n", __func__);
  339. rsi_kill_thread(&common->tx_thread);
  340. for (ii = 0; ii < NUM_SOFT_QUEUES; ii++)
  341. skb_queue_purge(&common->tx_queue[ii]);
  342. #ifdef CONFIG_RSI_COEX
  343. if (common->coex_mode > 1) {
  344. if (common->bt_adapter) {
  345. rsi_bt_ops.detach(common->bt_adapter);
  346. common->bt_adapter = NULL;
  347. }
  348. rsi_coex_detach(common);
  349. }
  350. #endif
  351. common->init_done = false;
  352. kfree(common);
  353. kfree(adapter->rsi_dev);
  354. kfree(adapter);
  355. }
  356. EXPORT_SYMBOL_GPL(rsi_91x_deinit);
  357. /**
  358. * rsi_91x_hal_module_init() - This function is invoked when the module is
  359. * loaded into the kernel.
  360. * It registers the client driver.
  361. * @void: Void.
  362. *
  363. * Return: 0 on success, -1 on failure.
  364. */
  365. static int rsi_91x_hal_module_init(void)
  366. {
  367. rsi_dbg(INIT_ZONE, "%s: Module init called\n", __func__);
  368. return 0;
  369. }
  370. /**
  371. * rsi_91x_hal_module_exit() - This function is called at the time of
  372. * removing/unloading the module.
  373. * It unregisters the client driver.
  374. * @void: Void.
  375. *
  376. * Return: None.
  377. */
  378. static void rsi_91x_hal_module_exit(void)
  379. {
  380. rsi_dbg(INIT_ZONE, "%s: Module exit called\n", __func__);
  381. }
  382. module_init(rsi_91x_hal_module_init);
  383. module_exit(rsi_91x_hal_module_exit);
  384. MODULE_AUTHOR("Redpine Signals Inc");
  385. MODULE_DESCRIPTION("Station driver for RSI 91x devices");
  386. MODULE_SUPPORTED_DEVICE("RSI-91x");
  387. MODULE_VERSION("0.1");
  388. MODULE_LICENSE("Dual BSD/GPL");