iwl-sv-open.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /******************************************************************************
  2. *
  3. * This file is provided under a dual BSD/GPLv2 license. When using or
  4. * redistributing this file, you may do so under either license.
  5. *
  6. * GPL LICENSE SUMMARY
  7. *
  8. * Copyright(c) 2010 - 2011 Intel Corporation. All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of version 2 of the GNU General Public License as
  12. * published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  22. * USA
  23. *
  24. * The full GNU General Public License is included in this distribution
  25. * in the file called LICENSE.GPL.
  26. *
  27. * Contact Information:
  28. * Intel Linux Wireless <ilw@linux.intel.com>
  29. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  30. *
  31. * BSD LICENSE
  32. *
  33. * Copyright(c) 2010 - 2011 Intel Corporation. All rights reserved.
  34. * All rights reserved.
  35. *
  36. * Redistribution and use in source and binary forms, with or without
  37. * modification, are permitted provided that the following conditions
  38. * are met:
  39. *
  40. * * Redistributions of source code must retain the above copyright
  41. * notice, this list of conditions and the following disclaimer.
  42. * * Redistributions in binary form must reproduce the above copyright
  43. * notice, this list of conditions and the following disclaimer in
  44. * the documentation and/or other materials provided with the
  45. * distribution.
  46. * * Neither the name Intel Corporation nor the names of its
  47. * contributors may be used to endorse or promote products derived
  48. * from this software without specific prior written permission.
  49. *
  50. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  51. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  52. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  53. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  54. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  55. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  56. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  57. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  58. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  59. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  60. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  61. *
  62. *****************************************************************************/
  63. #include <linux/init.h>
  64. #include <linux/kernel.h>
  65. #include <linux/module.h>
  66. #include <net/net_namespace.h>
  67. #include <linux/netdevice.h>
  68. #include <net/cfg80211.h>
  69. #include <net/mac80211.h>
  70. #include <net/netlink.h>
  71. #include "iwl-dev.h"
  72. #include "iwl-core.h"
  73. #include "iwl-debug.h"
  74. #include "iwl-fh.h"
  75. #include "iwl-io.h"
  76. #include "iwl-agn.h"
  77. #include "iwl-testmode.h"
  78. /* The TLVs used in the gnl message policy between the kernel module and
  79. * user space application. iwl_testmode_gnl_msg_policy is to be carried
  80. * through the NL80211_CMD_TESTMODE channel regulated by nl80211.
  81. * See iwl-testmode.h
  82. */
  83. static
  84. struct nla_policy iwl_testmode_gnl_msg_policy[IWL_TM_ATTR_MAX] = {
  85. [IWL_TM_ATTR_COMMAND] = { .type = NLA_U32, },
  86. [IWL_TM_ATTR_UCODE_CMD_ID] = { .type = NLA_U8, },
  87. [IWL_TM_ATTR_UCODE_CMD_DATA] = { .type = NLA_UNSPEC, },
  88. [IWL_TM_ATTR_REG_OFFSET] = { .type = NLA_U32, },
  89. [IWL_TM_ATTR_REG_VALUE8] = { .type = NLA_U8, },
  90. [IWL_TM_ATTR_REG_VALUE32] = { .type = NLA_U32, },
  91. [IWL_TM_ATTR_SYNC_RSP] = { .type = NLA_UNSPEC, },
  92. [IWL_TM_ATTR_UCODE_RX_PKT] = { .type = NLA_UNSPEC, },
  93. };
  94. /*
  95. * See the struct iwl_rx_packet in iwl-commands.h for the format of the
  96. * received events from the device
  97. */
  98. static inline int get_event_length(struct iwl_rx_mem_buffer *rxb)
  99. {
  100. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  101. if (pkt)
  102. return le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
  103. else
  104. return 0;
  105. }
  106. /*
  107. * This function multicasts the spontaneous messages from the device to the
  108. * user space. It is invoked whenever there is a received messages
  109. * from the device. This function is called within the ISR of the rx handlers
  110. * in iwlagn driver.
  111. *
  112. * The parsing of the message content is left to the user space application,
  113. * The message content is treated as unattacked raw data and is encapsulated
  114. * with IWL_TM_ATTR_UCODE_RX_PKT multicasting to the user space.
  115. *
  116. * @priv: the instance of iwlwifi device
  117. * @rxb: pointer to rx data content received by the ISR
  118. *
  119. * See the message policies and TLVs in iwl_testmode_gnl_msg_policy[].
  120. * For the messages multicasting to the user application, the mandatory
  121. * TLV fields are :
  122. * IWL_TM_ATTR_COMMAND must be IWL_TM_CMD_DEV2APP_UCODE_RX_PKT
  123. * IWL_TM_ATTR_UCODE_RX_PKT for carrying the message content
  124. */
  125. static void iwl_testmode_ucode_rx_pkt(struct iwl_priv *priv,
  126. struct iwl_rx_mem_buffer *rxb)
  127. {
  128. struct ieee80211_hw *hw = priv->hw;
  129. struct sk_buff *skb;
  130. void *data;
  131. int length;
  132. data = (void *)rxb_addr(rxb);
  133. length = get_event_length(rxb);
  134. if (!data || length == 0)
  135. return;
  136. skb = cfg80211_testmode_alloc_event_skb(hw->wiphy, 20 + length,
  137. GFP_ATOMIC);
  138. if (skb == NULL) {
  139. IWL_DEBUG_INFO(priv,
  140. "Run out of memory for messages to user space ?\n");
  141. return;
  142. }
  143. NLA_PUT_U32(skb, IWL_TM_ATTR_COMMAND, IWL_TM_CMD_DEV2APP_UCODE_RX_PKT);
  144. NLA_PUT(skb, IWL_TM_ATTR_UCODE_RX_PKT, length, data);
  145. cfg80211_testmode_event(skb, GFP_ATOMIC);
  146. return;
  147. nla_put_failure:
  148. kfree_skb(skb);
  149. IWL_DEBUG_INFO(priv, "Ouch, overran buffer, check allocation!\n");
  150. }
  151. void iwl_testmode_init(struct iwl_priv *priv)
  152. {
  153. priv->pre_rx_handler = iwl_testmode_ucode_rx_pkt;
  154. }
  155. /*
  156. * This function handles the user application commands to the ucode.
  157. *
  158. * It retrieves the mandatory fields IWL_TM_ATTR_UCODE_CMD_ID and
  159. * IWL_TM_ATTR_UCODE_CMD_DATA and calls to the handler to send the
  160. * host command to the ucode.
  161. *
  162. * If any mandatory field is missing, -ENOMSG is replied to the user space
  163. * application; otherwise, the actual execution result of the host command to
  164. * ucode is replied.
  165. *
  166. * @hw: ieee80211_hw object that represents the device
  167. * @tb: gnl message fields from the user space
  168. */
  169. static int iwl_testmode_ucode(struct ieee80211_hw *hw, struct nlattr **tb)
  170. {
  171. struct iwl_priv *priv = hw->priv;
  172. struct iwl_host_cmd cmd;
  173. memset(&cmd, 0, sizeof(struct iwl_host_cmd));
  174. if (!tb[IWL_TM_ATTR_UCODE_CMD_ID] ||
  175. !tb[IWL_TM_ATTR_UCODE_CMD_DATA]) {
  176. IWL_DEBUG_INFO(priv,
  177. "Error finding ucode command mandatory fields\n");
  178. return -ENOMSG;
  179. }
  180. cmd.id = nla_get_u8(tb[IWL_TM_ATTR_UCODE_CMD_ID]);
  181. cmd.data[0] = nla_data(tb[IWL_TM_ATTR_UCODE_CMD_DATA]);
  182. cmd.len[0] = nla_len(tb[IWL_TM_ATTR_UCODE_CMD_DATA]);
  183. cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
  184. IWL_INFO(priv, "testmode ucode command ID 0x%x, flags 0x%x,"
  185. " len %d\n", cmd.id, cmd.flags, cmd.len[0]);
  186. /* ok, let's submit the command to ucode */
  187. return iwl_send_cmd(priv, &cmd);
  188. }
  189. /*
  190. * This function handles the user application commands for register access.
  191. *
  192. * It retrieves command ID carried with IWL_TM_ATTR_COMMAND and calls to the
  193. * handlers respectively.
  194. *
  195. * If it's an unknown commdn ID, -ENOSYS is returned; or -ENOMSG if the
  196. * mandatory fields(IWL_TM_ATTR_REG_OFFSET,IWL_TM_ATTR_REG_VALUE32,
  197. * IWL_TM_ATTR_REG_VALUE8) are missing; Otherwise 0 is replied indicating
  198. * the success of the command execution.
  199. *
  200. * If IWL_TM_ATTR_COMMAND is IWL_TM_CMD_APP2DEV_REG_READ32, the register read
  201. * value is returned with IWL_TM_ATTR_REG_VALUE32.
  202. *
  203. * @hw: ieee80211_hw object that represents the device
  204. * @tb: gnl message fields from the user space
  205. */
  206. static int iwl_testmode_reg(struct ieee80211_hw *hw, struct nlattr **tb)
  207. {
  208. struct iwl_priv *priv = hw->priv;
  209. u32 ofs, val32;
  210. u8 val8;
  211. struct sk_buff *skb;
  212. int status = 0;
  213. if (!tb[IWL_TM_ATTR_REG_OFFSET]) {
  214. IWL_DEBUG_INFO(priv, "Error finding register offset\n");
  215. return -ENOMSG;
  216. }
  217. ofs = nla_get_u32(tb[IWL_TM_ATTR_REG_OFFSET]);
  218. IWL_INFO(priv, "testmode register access command offset 0x%x\n", ofs);
  219. switch (nla_get_u32(tb[IWL_TM_ATTR_COMMAND])) {
  220. case IWL_TM_CMD_APP2DEV_REG_READ32:
  221. val32 = iwl_read32(priv, ofs);
  222. IWL_INFO(priv, "32bit value to read 0x%x\n", val32);
  223. skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy, 20);
  224. if (!skb) {
  225. IWL_DEBUG_INFO(priv, "Error allocating memory\n");
  226. return -ENOMEM;
  227. }
  228. NLA_PUT_U32(skb, IWL_TM_ATTR_REG_VALUE32, val32);
  229. status = cfg80211_testmode_reply(skb);
  230. if (status < 0)
  231. IWL_DEBUG_INFO(priv,
  232. "Error sending msg : %d\n", status);
  233. break;
  234. case IWL_TM_CMD_APP2DEV_REG_WRITE32:
  235. if (!tb[IWL_TM_ATTR_REG_VALUE32]) {
  236. IWL_DEBUG_INFO(priv,
  237. "Error finding value to write\n");
  238. return -ENOMSG;
  239. } else {
  240. val32 = nla_get_u32(tb[IWL_TM_ATTR_REG_VALUE32]);
  241. IWL_INFO(priv, "32bit value to write 0x%x\n", val32);
  242. iwl_write32(priv, ofs, val32);
  243. }
  244. break;
  245. case IWL_TM_CMD_APP2DEV_REG_WRITE8:
  246. if (!tb[IWL_TM_ATTR_REG_VALUE8]) {
  247. IWL_DEBUG_INFO(priv, "Error finding value to write\n");
  248. return -ENOMSG;
  249. } else {
  250. val8 = nla_get_u8(tb[IWL_TM_ATTR_REG_VALUE8]);
  251. IWL_INFO(priv, "8bit value to write 0x%x\n", val8);
  252. iwl_write8(priv, ofs, val8);
  253. }
  254. break;
  255. default:
  256. IWL_DEBUG_INFO(priv, "Unknown testmode register command ID\n");
  257. return -ENOSYS;
  258. }
  259. return status;
  260. nla_put_failure:
  261. kfree_skb(skb);
  262. return -EMSGSIZE;
  263. }
  264. static int iwl_testmode_cfg_init_calib(struct iwl_priv *priv)
  265. {
  266. struct iwl_notification_wait calib_wait;
  267. int ret;
  268. iwlagn_init_notification_wait(priv, &calib_wait,
  269. CALIBRATION_COMPLETE_NOTIFICATION,
  270. NULL, NULL);
  271. ret = iwlagn_init_alive_start(priv);
  272. if (ret) {
  273. IWL_DEBUG_INFO(priv,
  274. "Error configuring init calibration: %d\n", ret);
  275. goto cfg_init_calib_error;
  276. }
  277. ret = iwlagn_wait_notification(priv, &calib_wait, 2 * HZ);
  278. if (ret)
  279. IWL_DEBUG_INFO(priv, "Error detecting"
  280. " CALIBRATION_COMPLETE_NOTIFICATION: %d\n", ret);
  281. return ret;
  282. cfg_init_calib_error:
  283. iwlagn_remove_notification(priv, &calib_wait);
  284. return ret;
  285. }
  286. /*
  287. * This function handles the user application commands for driver.
  288. *
  289. * It retrieves command ID carried with IWL_TM_ATTR_COMMAND and calls to the
  290. * handlers respectively.
  291. *
  292. * If it's an unknown commdn ID, -ENOSYS is replied; otherwise, the returned
  293. * value of the actual command execution is replied to the user application.
  294. *
  295. * If there's any message responding to the user space, IWL_TM_ATTR_SYNC_RSP
  296. * is used for carry the message while IWL_TM_ATTR_COMMAND must set to
  297. * IWL_TM_CMD_DEV2APP_SYNC_RSP.
  298. *
  299. * @hw: ieee80211_hw object that represents the device
  300. * @tb: gnl message fields from the user space
  301. */
  302. static int iwl_testmode_driver(struct ieee80211_hw *hw, struct nlattr **tb)
  303. {
  304. struct iwl_priv *priv = hw->priv;
  305. struct sk_buff *skb;
  306. unsigned char *rsp_data_ptr = NULL;
  307. int status = 0, rsp_data_len = 0;
  308. switch (nla_get_u32(tb[IWL_TM_ATTR_COMMAND])) {
  309. case IWL_TM_CMD_APP2DEV_GET_DEVICENAME:
  310. rsp_data_ptr = (unsigned char *)priv->cfg->name;
  311. rsp_data_len = strlen(priv->cfg->name);
  312. skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
  313. rsp_data_len + 20);
  314. if (!skb) {
  315. IWL_DEBUG_INFO(priv,
  316. "Error allocating memory\n");
  317. return -ENOMEM;
  318. }
  319. NLA_PUT_U32(skb, IWL_TM_ATTR_COMMAND,
  320. IWL_TM_CMD_DEV2APP_SYNC_RSP);
  321. NLA_PUT(skb, IWL_TM_ATTR_SYNC_RSP,
  322. rsp_data_len, rsp_data_ptr);
  323. status = cfg80211_testmode_reply(skb);
  324. if (status < 0)
  325. IWL_DEBUG_INFO(priv, "Error sending msg : %d\n",
  326. status);
  327. break;
  328. case IWL_TM_CMD_APP2DEV_LOAD_INIT_FW:
  329. status = iwlagn_load_ucode_wait_alive(priv, &priv->ucode_init,
  330. UCODE_SUBTYPE_INIT, -1);
  331. if (status)
  332. IWL_DEBUG_INFO(priv,
  333. "Error loading init ucode: %d\n", status);
  334. break;
  335. case IWL_TM_CMD_APP2DEV_CFG_INIT_CALIB:
  336. iwl_testmode_cfg_init_calib(priv);
  337. iwlagn_stop_device(priv);
  338. break;
  339. case IWL_TM_CMD_APP2DEV_LOAD_RUNTIME_FW:
  340. status = iwlagn_load_ucode_wait_alive(priv,
  341. &priv->ucode_rt,
  342. UCODE_SUBTYPE_REGULAR,
  343. UCODE_SUBTYPE_REGULAR_NEW);
  344. if (status) {
  345. IWL_DEBUG_INFO(priv,
  346. "Error loading runtime ucode: %d\n", status);
  347. break;
  348. }
  349. status = iwl_alive_start(priv);
  350. if (status)
  351. IWL_DEBUG_INFO(priv,
  352. "Error starting the device: %d\n", status);
  353. break;
  354. default:
  355. IWL_DEBUG_INFO(priv, "Unknown testmode driver command ID\n");
  356. return -ENOSYS;
  357. }
  358. return status;
  359. nla_put_failure:
  360. kfree_skb(skb);
  361. return -EMSGSIZE;
  362. }
  363. /* The testmode gnl message handler that takes the gnl message from the
  364. * user space and parses it per the policy iwl_testmode_gnl_msg_policy, then
  365. * invoke the corresponding handlers.
  366. *
  367. * This function is invoked when there is user space application sending
  368. * gnl message through the testmode tunnel NL80211_CMD_TESTMODE regulated
  369. * by nl80211.
  370. *
  371. * It retrieves the mandatory field, IWL_TM_ATTR_COMMAND, before
  372. * dispatching it to the corresponding handler.
  373. *
  374. * If IWL_TM_ATTR_COMMAND is missing, -ENOMSG is replied to user application;
  375. * -ENOSYS is replied to the user application if the command is unknown;
  376. * Otherwise, the command is dispatched to the respective handler.
  377. *
  378. * @hw: ieee80211_hw object that represents the device
  379. * @data: pointer to user space message
  380. * @len: length in byte of @data
  381. */
  382. int iwl_testmode_cmd(struct ieee80211_hw *hw, void *data, int len)
  383. {
  384. struct nlattr *tb[IWL_TM_ATTR_MAX - 1];
  385. struct iwl_priv *priv = hw->priv;
  386. int result;
  387. result = nla_parse(tb, IWL_TM_ATTR_MAX - 1, data, len,
  388. iwl_testmode_gnl_msg_policy);
  389. if (result != 0) {
  390. IWL_DEBUG_INFO(priv,
  391. "Error parsing the gnl message : %d\n", result);
  392. return result;
  393. }
  394. /* IWL_TM_ATTR_COMMAND is absolutely mandatory */
  395. if (!tb[IWL_TM_ATTR_COMMAND]) {
  396. IWL_DEBUG_INFO(priv, "Error finding testmode command type\n");
  397. return -ENOMSG;
  398. }
  399. /* in case multiple accesses to the device happens */
  400. mutex_lock(&priv->mutex);
  401. switch (nla_get_u32(tb[IWL_TM_ATTR_COMMAND])) {
  402. case IWL_TM_CMD_APP2DEV_UCODE:
  403. IWL_DEBUG_INFO(priv, "testmode cmd to uCode\n");
  404. result = iwl_testmode_ucode(hw, tb);
  405. break;
  406. case IWL_TM_CMD_APP2DEV_REG_READ32:
  407. case IWL_TM_CMD_APP2DEV_REG_WRITE32:
  408. case IWL_TM_CMD_APP2DEV_REG_WRITE8:
  409. IWL_DEBUG_INFO(priv, "testmode cmd to register\n");
  410. result = iwl_testmode_reg(hw, tb);
  411. break;
  412. case IWL_TM_CMD_APP2DEV_GET_DEVICENAME:
  413. case IWL_TM_CMD_APP2DEV_LOAD_INIT_FW:
  414. case IWL_TM_CMD_APP2DEV_CFG_INIT_CALIB:
  415. case IWL_TM_CMD_APP2DEV_LOAD_RUNTIME_FW:
  416. IWL_DEBUG_INFO(priv, "testmode cmd to driver\n");
  417. result = iwl_testmode_driver(hw, tb);
  418. break;
  419. default:
  420. IWL_DEBUG_INFO(priv, "Unknown testmode command\n");
  421. result = -ENOSYS;
  422. break;
  423. }
  424. mutex_unlock(&priv->mutex);
  425. return result;
  426. }