rsi_91x_main.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 "rsi_mgmt.h"
  20. #include "rsi_common.h"
  21. #include "rsi_hal.h"
  22. u32 rsi_zone_enabled = /* INFO_ZONE |
  23. INIT_ZONE |
  24. MGMT_TX_ZONE |
  25. MGMT_RX_ZONE |
  26. DATA_TX_ZONE |
  27. DATA_RX_ZONE |
  28. FSM_ZONE |
  29. ISR_ZONE | */
  30. ERR_ZONE |
  31. 0;
  32. EXPORT_SYMBOL_GPL(rsi_zone_enabled);
  33. /**
  34. * rsi_dbg() - This function outputs informational messages.
  35. * @zone: Zone of interest for output message.
  36. * @fmt: printf-style format for output message.
  37. *
  38. * Return: none
  39. */
  40. void rsi_dbg(u32 zone, const char *fmt, ...)
  41. {
  42. struct va_format vaf;
  43. va_list args;
  44. va_start(args, fmt);
  45. vaf.fmt = fmt;
  46. vaf.va = &args;
  47. if (zone & rsi_zone_enabled)
  48. pr_info("%pV", &vaf);
  49. va_end(args);
  50. }
  51. EXPORT_SYMBOL_GPL(rsi_dbg);
  52. static char *opmode_str(int oper_mode)
  53. {
  54. switch (oper_mode) {
  55. case RSI_DEV_OPMODE_WIFI_ALONE:
  56. return "Wi-Fi alone";
  57. }
  58. return "Unknown";
  59. }
  60. void rsi_print_version(struct rsi_common *common)
  61. {
  62. rsi_dbg(ERR_ZONE, "================================================\n");
  63. rsi_dbg(ERR_ZONE, "================ RSI Version Info ==============\n");
  64. rsi_dbg(ERR_ZONE, "================================================\n");
  65. rsi_dbg(ERR_ZONE, "FW Version\t: %d.%d.%d\n",
  66. common->lmac_ver.major, common->lmac_ver.minor,
  67. common->lmac_ver.release_num);
  68. rsi_dbg(ERR_ZONE, "Operating mode\t: %d [%s]",
  69. common->oper_mode, opmode_str(common->oper_mode));
  70. rsi_dbg(ERR_ZONE, "Firmware file\t: %s", common->priv->fw_file_name);
  71. rsi_dbg(ERR_ZONE, "================================================\n");
  72. }
  73. /**
  74. * rsi_prepare_skb() - This function prepares the skb.
  75. * @common: Pointer to the driver private structure.
  76. * @buffer: Pointer to the packet data.
  77. * @pkt_len: Length of the packet.
  78. * @extended_desc: Extended descriptor.
  79. *
  80. * Return: Successfully skb.
  81. */
  82. static struct sk_buff *rsi_prepare_skb(struct rsi_common *common,
  83. u8 *buffer,
  84. u32 pkt_len,
  85. u8 extended_desc)
  86. {
  87. struct ieee80211_tx_info *info;
  88. struct skb_info *rx_params;
  89. struct sk_buff *skb = NULL;
  90. u8 payload_offset;
  91. struct ieee80211_vif *vif;
  92. struct ieee80211_hdr *wh;
  93. if (WARN(!pkt_len, "%s: Dummy pkt received", __func__))
  94. return NULL;
  95. if (pkt_len > (RSI_RCV_BUFFER_LEN * 4)) {
  96. rsi_dbg(ERR_ZONE, "%s: Pkt size > max rx buf size %d\n",
  97. __func__, pkt_len);
  98. pkt_len = RSI_RCV_BUFFER_LEN * 4;
  99. }
  100. pkt_len -= extended_desc;
  101. skb = dev_alloc_skb(pkt_len + FRAME_DESC_SZ);
  102. if (skb == NULL)
  103. return NULL;
  104. payload_offset = (extended_desc + FRAME_DESC_SZ);
  105. skb_put(skb, pkt_len);
  106. memcpy((skb->data), (buffer + payload_offset), skb->len);
  107. wh = (struct ieee80211_hdr *)skb->data;
  108. vif = rsi_get_vif(common->priv, wh->addr1);
  109. info = IEEE80211_SKB_CB(skb);
  110. rx_params = (struct skb_info *)info->driver_data;
  111. rx_params->rssi = rsi_get_rssi(buffer);
  112. rx_params->channel = rsi_get_connected_channel(vif);
  113. return skb;
  114. }
  115. /**
  116. * rsi_read_pkt() - This function reads frames from the card.
  117. * @common: Pointer to the driver private structure.
  118. * @rcv_pkt_len: Received pkt length. In case of USB it is 0.
  119. *
  120. * Return: 0 on success, -1 on failure.
  121. */
  122. int rsi_read_pkt(struct rsi_common *common, s32 rcv_pkt_len)
  123. {
  124. u8 *frame_desc = NULL, extended_desc = 0;
  125. u32 index, length = 0, queueno = 0;
  126. u16 actual_length = 0, offset;
  127. struct sk_buff *skb = NULL;
  128. index = 0;
  129. do {
  130. frame_desc = &common->rx_data_pkt[index];
  131. actual_length = *(u16 *)&frame_desc[0];
  132. offset = *(u16 *)&frame_desc[2];
  133. queueno = rsi_get_queueno(frame_desc, offset);
  134. length = rsi_get_length(frame_desc, offset);
  135. /* Extended descriptor is valid for WLAN queues only */
  136. if (queueno == RSI_WIFI_DATA_Q || queueno == RSI_WIFI_MGMT_Q)
  137. extended_desc = rsi_get_extended_desc(frame_desc,
  138. offset);
  139. switch (queueno) {
  140. case RSI_COEX_Q:
  141. rsi_mgmt_pkt_recv(common, (frame_desc + offset));
  142. break;
  143. case RSI_WIFI_DATA_Q:
  144. skb = rsi_prepare_skb(common,
  145. (frame_desc + offset),
  146. length,
  147. extended_desc);
  148. if (skb == NULL)
  149. goto fail;
  150. rsi_indicate_pkt_to_os(common, skb);
  151. break;
  152. case RSI_WIFI_MGMT_Q:
  153. rsi_mgmt_pkt_recv(common, (frame_desc + offset));
  154. break;
  155. default:
  156. rsi_dbg(ERR_ZONE, "%s: pkt from invalid queue: %d\n",
  157. __func__, queueno);
  158. goto fail;
  159. }
  160. index += actual_length;
  161. rcv_pkt_len -= actual_length;
  162. } while (rcv_pkt_len > 0);
  163. return 0;
  164. fail:
  165. return -EINVAL;
  166. }
  167. EXPORT_SYMBOL_GPL(rsi_read_pkt);
  168. /**
  169. * rsi_tx_scheduler_thread() - This function is a kernel thread to send the
  170. * packets to the device.
  171. * @common: Pointer to the driver private structure.
  172. *
  173. * Return: None.
  174. */
  175. static void rsi_tx_scheduler_thread(struct rsi_common *common)
  176. {
  177. struct rsi_hw *adapter = common->priv;
  178. u32 timeout = EVENT_WAIT_FOREVER;
  179. do {
  180. if (adapter->determine_event_timeout)
  181. timeout = adapter->determine_event_timeout(adapter);
  182. rsi_wait_event(&common->tx_thread.event, timeout);
  183. rsi_reset_event(&common->tx_thread.event);
  184. if (common->init_done)
  185. rsi_core_qos_processor(common);
  186. } while (atomic_read(&common->tx_thread.thread_done) == 0);
  187. complete_and_exit(&common->tx_thread.completion, 0);
  188. }
  189. /**
  190. * rsi_91x_init() - This function initializes os interface operations.
  191. * @void: Void.
  192. *
  193. * Return: Pointer to the adapter structure on success, NULL on failure .
  194. */
  195. struct rsi_hw *rsi_91x_init(void)
  196. {
  197. struct rsi_hw *adapter = NULL;
  198. struct rsi_common *common = NULL;
  199. u8 ii = 0;
  200. adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
  201. if (!adapter)
  202. return NULL;
  203. adapter->priv = kzalloc(sizeof(*common), GFP_KERNEL);
  204. if (adapter->priv == NULL) {
  205. rsi_dbg(ERR_ZONE, "%s: Failed in allocation of memory\n",
  206. __func__);
  207. kfree(adapter);
  208. return NULL;
  209. } else {
  210. common = adapter->priv;
  211. common->priv = adapter;
  212. }
  213. for (ii = 0; ii < NUM_SOFT_QUEUES; ii++)
  214. skb_queue_head_init(&common->tx_queue[ii]);
  215. rsi_init_event(&common->tx_thread.event);
  216. mutex_init(&common->mutex);
  217. mutex_init(&common->tx_lock);
  218. mutex_init(&common->rx_lock);
  219. if (rsi_create_kthread(common,
  220. &common->tx_thread,
  221. rsi_tx_scheduler_thread,
  222. "Tx-Thread")) {
  223. rsi_dbg(ERR_ZONE, "%s: Unable to init tx thrd\n", __func__);
  224. goto err;
  225. }
  226. rsi_default_ps_params(adapter);
  227. spin_lock_init(&adapter->ps_lock);
  228. timer_setup(&common->roc_timer, rsi_roc_timeout, 0);
  229. init_completion(&common->wlan_init_completion);
  230. common->init_done = true;
  231. return adapter;
  232. err:
  233. kfree(common);
  234. kfree(adapter);
  235. return NULL;
  236. }
  237. EXPORT_SYMBOL_GPL(rsi_91x_init);
  238. /**
  239. * rsi_91x_deinit() - This function de-intializes os intf operations.
  240. * @adapter: Pointer to the adapter structure.
  241. *
  242. * Return: None.
  243. */
  244. void rsi_91x_deinit(struct rsi_hw *adapter)
  245. {
  246. struct rsi_common *common = adapter->priv;
  247. u8 ii;
  248. rsi_dbg(INFO_ZONE, "%s: Performing deinit os ops\n", __func__);
  249. rsi_kill_thread(&common->tx_thread);
  250. for (ii = 0; ii < NUM_SOFT_QUEUES; ii++)
  251. skb_queue_purge(&common->tx_queue[ii]);
  252. common->init_done = false;
  253. kfree(common);
  254. kfree(adapter->rsi_dev);
  255. kfree(adapter);
  256. }
  257. EXPORT_SYMBOL_GPL(rsi_91x_deinit);
  258. /**
  259. * rsi_91x_hal_module_init() - This function is invoked when the module is
  260. * loaded into the kernel.
  261. * It registers the client driver.
  262. * @void: Void.
  263. *
  264. * Return: 0 on success, -1 on failure.
  265. */
  266. static int rsi_91x_hal_module_init(void)
  267. {
  268. rsi_dbg(INIT_ZONE, "%s: Module init called\n", __func__);
  269. return 0;
  270. }
  271. /**
  272. * rsi_91x_hal_module_exit() - This function is called at the time of
  273. * removing/unloading the module.
  274. * It unregisters the client driver.
  275. * @void: Void.
  276. *
  277. * Return: None.
  278. */
  279. static void rsi_91x_hal_module_exit(void)
  280. {
  281. rsi_dbg(INIT_ZONE, "%s: Module exit called\n", __func__);
  282. }
  283. module_init(rsi_91x_hal_module_init);
  284. module_exit(rsi_91x_hal_module_exit);
  285. MODULE_AUTHOR("Redpine Signals Inc");
  286. MODULE_DESCRIPTION("Station driver for RSI 91x devices");
  287. MODULE_SUPPORTED_DEVICE("RSI-91x");
  288. MODULE_VERSION("0.1");
  289. MODULE_LICENSE("Dual BSD/GPL");