rsi_91x_main.c 11 KB

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