iwlagn: add CMD_ON_DEMAND flag for host command from testmode
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / wireless / iwlwifi / iwl-sv-open.c
blob653b7870adc1d2df51915c3463e9cc1eab9bde53
1 /******************************************************************************
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.
6 * GPL LICENSE SUMMARY
8 * Copyright(c) 2010 - 2011 Intel Corporation. All rights reserved.
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.
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.
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
24 * The full GNU General Public License is included in this distribution
25 * in the file called LICENSE.GPL.
27 * Contact Information:
28 * Intel Linux Wireless <ilw@linux.intel.com>
29 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
31 * BSD LICENSE
33 * Copyright(c) 2010 - 2011 Intel Corporation. All rights reserved.
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
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.
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.
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>
72 #include "iwl-dev.h"
73 #include "iwl-core.h"
74 #include "iwl-debug.h"
75 #include "iwl-fh.h"
76 #include "iwl-io.h"
77 #include "iwl-agn.h"
78 #include "iwl-testmode.h"
79 #include "iwl-trans.h"
81 /* The TLVs used in the gnl message policy between the kernel module and
82 * user space application. iwl_testmode_gnl_msg_policy is to be carried
83 * through the NL80211_CMD_TESTMODE channel regulated by nl80211.
84 * See iwl-testmode.h
86 static
87 struct nla_policy iwl_testmode_gnl_msg_policy[IWL_TM_ATTR_MAX] = {
88 [IWL_TM_ATTR_COMMAND] = { .type = NLA_U32, },
90 [IWL_TM_ATTR_UCODE_CMD_ID] = { .type = NLA_U8, },
91 [IWL_TM_ATTR_UCODE_CMD_DATA] = { .type = NLA_UNSPEC, },
93 [IWL_TM_ATTR_REG_OFFSET] = { .type = NLA_U32, },
94 [IWL_TM_ATTR_REG_VALUE8] = { .type = NLA_U8, },
95 [IWL_TM_ATTR_REG_VALUE32] = { .type = NLA_U32, },
97 [IWL_TM_ATTR_SYNC_RSP] = { .type = NLA_UNSPEC, },
98 [IWL_TM_ATTR_UCODE_RX_PKT] = { .type = NLA_UNSPEC, },
100 [IWL_TM_ATTR_EEPROM] = { .type = NLA_UNSPEC, },
102 [IWL_TM_ATTR_TRACE_ADDR] = { .type = NLA_UNSPEC, },
103 [IWL_TM_ATTR_TRACE_DUMP] = { .type = NLA_UNSPEC, },
104 [IWL_TM_ATTR_TRACE_SIZE] = { .type = NLA_U32, },
106 [IWL_TM_ATTR_FIXRATE] = { .type = NLA_U32, },
111 * See the struct iwl_rx_packet in iwl-commands.h for the format of the
112 * received events from the device
114 static inline int get_event_length(struct iwl_rx_mem_buffer *rxb)
116 struct iwl_rx_packet *pkt = rxb_addr(rxb);
117 if (pkt)
118 return le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
119 else
120 return 0;
125 * This function multicasts the spontaneous messages from the device to the
126 * user space. It is invoked whenever there is a received messages
127 * from the device. This function is called within the ISR of the rx handlers
128 * in iwlagn driver.
130 * The parsing of the message content is left to the user space application,
131 * The message content is treated as unattacked raw data and is encapsulated
132 * with IWL_TM_ATTR_UCODE_RX_PKT multicasting to the user space.
134 * @priv: the instance of iwlwifi device
135 * @rxb: pointer to rx data content received by the ISR
137 * See the message policies and TLVs in iwl_testmode_gnl_msg_policy[].
138 * For the messages multicasting to the user application, the mandatory
139 * TLV fields are :
140 * IWL_TM_ATTR_COMMAND must be IWL_TM_CMD_DEV2APP_UCODE_RX_PKT
141 * IWL_TM_ATTR_UCODE_RX_PKT for carrying the message content
144 static void iwl_testmode_ucode_rx_pkt(struct iwl_priv *priv,
145 struct iwl_rx_mem_buffer *rxb)
147 struct ieee80211_hw *hw = priv->hw;
148 struct sk_buff *skb;
149 void *data;
150 int length;
152 data = (void *)rxb_addr(rxb);
153 length = get_event_length(rxb);
155 if (!data || length == 0)
156 return;
158 skb = cfg80211_testmode_alloc_event_skb(hw->wiphy, 20 + length,
159 GFP_ATOMIC);
160 if (skb == NULL) {
161 IWL_DEBUG_INFO(priv,
162 "Run out of memory for messages to user space ?\n");
163 return;
165 NLA_PUT_U32(skb, IWL_TM_ATTR_COMMAND, IWL_TM_CMD_DEV2APP_UCODE_RX_PKT);
166 NLA_PUT(skb, IWL_TM_ATTR_UCODE_RX_PKT, length, data);
167 cfg80211_testmode_event(skb, GFP_ATOMIC);
168 return;
170 nla_put_failure:
171 kfree_skb(skb);
172 IWL_DEBUG_INFO(priv, "Ouch, overran buffer, check allocation!\n");
175 void iwl_testmode_init(struct iwl_priv *priv)
177 priv->pre_rx_handler = iwl_testmode_ucode_rx_pkt;
178 priv->testmode_trace.trace_enabled = false;
181 static void iwl_trace_cleanup(struct iwl_priv *priv)
183 struct device *dev = priv->bus.dev;
185 if (priv->testmode_trace.trace_enabled) {
186 if (priv->testmode_trace.cpu_addr &&
187 priv->testmode_trace.dma_addr)
188 dma_free_coherent(dev,
189 priv->testmode_trace.total_size,
190 priv->testmode_trace.cpu_addr,
191 priv->testmode_trace.dma_addr);
192 priv->testmode_trace.trace_enabled = false;
193 priv->testmode_trace.cpu_addr = NULL;
194 priv->testmode_trace.trace_addr = NULL;
195 priv->testmode_trace.dma_addr = 0;
196 priv->testmode_trace.buff_size = 0;
197 priv->testmode_trace.total_size = 0;
202 void iwl_testmode_cleanup(struct iwl_priv *priv)
204 iwl_trace_cleanup(priv);
208 * This function handles the user application commands to the ucode.
210 * It retrieves the mandatory fields IWL_TM_ATTR_UCODE_CMD_ID and
211 * IWL_TM_ATTR_UCODE_CMD_DATA and calls to the handler to send the
212 * host command to the ucode.
214 * If any mandatory field is missing, -ENOMSG is replied to the user space
215 * application; otherwise, the actual execution result of the host command to
216 * ucode is replied.
218 * @hw: ieee80211_hw object that represents the device
219 * @tb: gnl message fields from the user space
221 static int iwl_testmode_ucode(struct ieee80211_hw *hw, struct nlattr **tb)
223 struct iwl_priv *priv = hw->priv;
224 struct iwl_host_cmd cmd;
226 memset(&cmd, 0, sizeof(struct iwl_host_cmd));
228 if (!tb[IWL_TM_ATTR_UCODE_CMD_ID] ||
229 !tb[IWL_TM_ATTR_UCODE_CMD_DATA]) {
230 IWL_DEBUG_INFO(priv,
231 "Error finding ucode command mandatory fields\n");
232 return -ENOMSG;
235 cmd.flags = CMD_ON_DEMAND;
236 cmd.id = nla_get_u8(tb[IWL_TM_ATTR_UCODE_CMD_ID]);
237 cmd.data[0] = nla_data(tb[IWL_TM_ATTR_UCODE_CMD_DATA]);
238 cmd.len[0] = nla_len(tb[IWL_TM_ATTR_UCODE_CMD_DATA]);
239 cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
240 IWL_INFO(priv, "testmode ucode command ID 0x%x, flags 0x%x,"
241 " len %d\n", cmd.id, cmd.flags, cmd.len[0]);
242 /* ok, let's submit the command to ucode */
243 return trans_send_cmd(priv, &cmd);
248 * This function handles the user application commands for register access.
250 * It retrieves command ID carried with IWL_TM_ATTR_COMMAND and calls to the
251 * handlers respectively.
253 * If it's an unknown commdn ID, -ENOSYS is returned; or -ENOMSG if the
254 * mandatory fields(IWL_TM_ATTR_REG_OFFSET,IWL_TM_ATTR_REG_VALUE32,
255 * IWL_TM_ATTR_REG_VALUE8) are missing; Otherwise 0 is replied indicating
256 * the success of the command execution.
258 * If IWL_TM_ATTR_COMMAND is IWL_TM_CMD_APP2DEV_REG_READ32, the register read
259 * value is returned with IWL_TM_ATTR_REG_VALUE32.
261 * @hw: ieee80211_hw object that represents the device
262 * @tb: gnl message fields from the user space
264 static int iwl_testmode_reg(struct ieee80211_hw *hw, struct nlattr **tb)
266 struct iwl_priv *priv = hw->priv;
267 u32 ofs, val32;
268 u8 val8;
269 struct sk_buff *skb;
270 int status = 0;
272 if (!tb[IWL_TM_ATTR_REG_OFFSET]) {
273 IWL_DEBUG_INFO(priv, "Error finding register offset\n");
274 return -ENOMSG;
276 ofs = nla_get_u32(tb[IWL_TM_ATTR_REG_OFFSET]);
277 IWL_INFO(priv, "testmode register access command offset 0x%x\n", ofs);
279 switch (nla_get_u32(tb[IWL_TM_ATTR_COMMAND])) {
280 case IWL_TM_CMD_APP2DEV_REG_READ32:
281 val32 = iwl_read32(priv, ofs);
282 IWL_INFO(priv, "32bit value to read 0x%x\n", val32);
284 skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy, 20);
285 if (!skb) {
286 IWL_DEBUG_INFO(priv, "Error allocating memory\n");
287 return -ENOMEM;
289 NLA_PUT_U32(skb, IWL_TM_ATTR_REG_VALUE32, val32);
290 status = cfg80211_testmode_reply(skb);
291 if (status < 0)
292 IWL_DEBUG_INFO(priv,
293 "Error sending msg : %d\n", status);
294 break;
295 case IWL_TM_CMD_APP2DEV_REG_WRITE32:
296 if (!tb[IWL_TM_ATTR_REG_VALUE32]) {
297 IWL_DEBUG_INFO(priv,
298 "Error finding value to write\n");
299 return -ENOMSG;
300 } else {
301 val32 = nla_get_u32(tb[IWL_TM_ATTR_REG_VALUE32]);
302 IWL_INFO(priv, "32bit value to write 0x%x\n", val32);
303 iwl_write32(priv, ofs, val32);
305 break;
306 case IWL_TM_CMD_APP2DEV_REG_WRITE8:
307 if (!tb[IWL_TM_ATTR_REG_VALUE8]) {
308 IWL_DEBUG_INFO(priv, "Error finding value to write\n");
309 return -ENOMSG;
310 } else {
311 val8 = nla_get_u8(tb[IWL_TM_ATTR_REG_VALUE8]);
312 IWL_INFO(priv, "8bit value to write 0x%x\n", val8);
313 iwl_write8(priv, ofs, val8);
315 break;
316 default:
317 IWL_DEBUG_INFO(priv, "Unknown testmode register command ID\n");
318 return -ENOSYS;
321 return status;
323 nla_put_failure:
324 kfree_skb(skb);
325 return -EMSGSIZE;
329 static int iwl_testmode_cfg_init_calib(struct iwl_priv *priv)
331 struct iwl_notification_wait calib_wait;
332 int ret;
334 iwlagn_init_notification_wait(priv, &calib_wait,
335 CALIBRATION_COMPLETE_NOTIFICATION,
336 NULL, NULL);
337 ret = iwlagn_init_alive_start(priv);
338 if (ret) {
339 IWL_DEBUG_INFO(priv,
340 "Error configuring init calibration: %d\n", ret);
341 goto cfg_init_calib_error;
344 ret = iwlagn_wait_notification(priv, &calib_wait, 2 * HZ);
345 if (ret)
346 IWL_DEBUG_INFO(priv, "Error detecting"
347 " CALIBRATION_COMPLETE_NOTIFICATION: %d\n", ret);
348 return ret;
350 cfg_init_calib_error:
351 iwlagn_remove_notification(priv, &calib_wait);
352 return ret;
356 * This function handles the user application commands for driver.
358 * It retrieves command ID carried with IWL_TM_ATTR_COMMAND and calls to the
359 * handlers respectively.
361 * If it's an unknown commdn ID, -ENOSYS is replied; otherwise, the returned
362 * value of the actual command execution is replied to the user application.
364 * If there's any message responding to the user space, IWL_TM_ATTR_SYNC_RSP
365 * is used for carry the message while IWL_TM_ATTR_COMMAND must set to
366 * IWL_TM_CMD_DEV2APP_SYNC_RSP.
368 * @hw: ieee80211_hw object that represents the device
369 * @tb: gnl message fields from the user space
371 static int iwl_testmode_driver(struct ieee80211_hw *hw, struct nlattr **tb)
373 struct iwl_priv *priv = hw->priv;
374 struct sk_buff *skb;
375 unsigned char *rsp_data_ptr = NULL;
376 int status = 0, rsp_data_len = 0;
378 switch (nla_get_u32(tb[IWL_TM_ATTR_COMMAND])) {
379 case IWL_TM_CMD_APP2DEV_GET_DEVICENAME:
380 rsp_data_ptr = (unsigned char *)priv->cfg->name;
381 rsp_data_len = strlen(priv->cfg->name);
382 skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
383 rsp_data_len + 20);
384 if (!skb) {
385 IWL_DEBUG_INFO(priv,
386 "Error allocating memory\n");
387 return -ENOMEM;
389 NLA_PUT_U32(skb, IWL_TM_ATTR_COMMAND,
390 IWL_TM_CMD_DEV2APP_SYNC_RSP);
391 NLA_PUT(skb, IWL_TM_ATTR_SYNC_RSP,
392 rsp_data_len, rsp_data_ptr);
393 status = cfg80211_testmode_reply(skb);
394 if (status < 0)
395 IWL_DEBUG_INFO(priv, "Error sending msg : %d\n",
396 status);
397 break;
399 case IWL_TM_CMD_APP2DEV_LOAD_INIT_FW:
400 status = iwlagn_load_ucode_wait_alive(priv, &priv->ucode_init,
401 IWL_UCODE_INIT);
402 if (status)
403 IWL_DEBUG_INFO(priv,
404 "Error loading init ucode: %d\n", status);
405 break;
407 case IWL_TM_CMD_APP2DEV_CFG_INIT_CALIB:
408 iwl_testmode_cfg_init_calib(priv);
409 iwlagn_stop_device(priv);
410 break;
412 case IWL_TM_CMD_APP2DEV_LOAD_RUNTIME_FW:
413 status = iwlagn_load_ucode_wait_alive(priv,
414 &priv->ucode_rt,
415 IWL_UCODE_REGULAR);
416 if (status) {
417 IWL_DEBUG_INFO(priv,
418 "Error loading runtime ucode: %d\n", status);
419 break;
421 status = iwl_alive_start(priv);
422 if (status)
423 IWL_DEBUG_INFO(priv,
424 "Error starting the device: %d\n", status);
425 break;
427 case IWL_TM_CMD_APP2DEV_GET_EEPROM:
428 if (priv->eeprom) {
429 skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
430 priv->cfg->base_params->eeprom_size + 20);
431 if (!skb) {
432 IWL_DEBUG_INFO(priv,
433 "Error allocating memory\n");
434 return -ENOMEM;
436 NLA_PUT_U32(skb, IWL_TM_ATTR_COMMAND,
437 IWL_TM_CMD_DEV2APP_EEPROM_RSP);
438 NLA_PUT(skb, IWL_TM_ATTR_EEPROM,
439 priv->cfg->base_params->eeprom_size,
440 priv->eeprom);
441 status = cfg80211_testmode_reply(skb);
442 if (status < 0)
443 IWL_DEBUG_INFO(priv,
444 "Error sending msg : %d\n",
445 status);
446 } else
447 return -EFAULT;
448 break;
450 case IWL_TM_CMD_APP2DEV_FIXRATE_REQ:
451 if (!tb[IWL_TM_ATTR_FIXRATE]) {
452 IWL_DEBUG_INFO(priv,
453 "Error finding fixrate setting\n");
454 return -ENOMSG;
456 priv->dbg_fixed_rate = nla_get_u32(tb[IWL_TM_ATTR_FIXRATE]);
457 break;
459 default:
460 IWL_DEBUG_INFO(priv, "Unknown testmode driver command ID\n");
461 return -ENOSYS;
463 return status;
465 nla_put_failure:
466 kfree_skb(skb);
467 return -EMSGSIZE;
472 * This function handles the user application commands for uCode trace
474 * It retrieves command ID carried with IWL_TM_ATTR_COMMAND and calls to the
475 * handlers respectively.
477 * If it's an unknown commdn ID, -ENOSYS is replied; otherwise, the returned
478 * value of the actual command execution is replied to the user application.
480 * @hw: ieee80211_hw object that represents the device
481 * @tb: gnl message fields from the user space
483 static int iwl_testmode_trace(struct ieee80211_hw *hw, struct nlattr **tb)
485 struct iwl_priv *priv = hw->priv;
486 struct sk_buff *skb;
487 int status = 0;
488 struct device *dev = priv->bus.dev;
490 switch (nla_get_u32(tb[IWL_TM_ATTR_COMMAND])) {
491 case IWL_TM_CMD_APP2DEV_BEGIN_TRACE:
492 if (priv->testmode_trace.trace_enabled)
493 return -EBUSY;
495 if (!tb[IWL_TM_ATTR_TRACE_SIZE])
496 priv->testmode_trace.buff_size = TRACE_BUFF_SIZE_DEF;
497 else
498 priv->testmode_trace.buff_size =
499 nla_get_u32(tb[IWL_TM_ATTR_TRACE_SIZE]);
500 if (!priv->testmode_trace.buff_size)
501 return -EINVAL;
502 if (priv->testmode_trace.buff_size < TRACE_BUFF_SIZE_MIN ||
503 priv->testmode_trace.buff_size > TRACE_BUFF_SIZE_MAX)
504 return -EINVAL;
506 priv->testmode_trace.total_size =
507 priv->testmode_trace.buff_size + TRACE_BUFF_PADD;
508 priv->testmode_trace.cpu_addr =
509 dma_alloc_coherent(dev,
510 priv->testmode_trace.total_size,
511 &priv->testmode_trace.dma_addr,
512 GFP_KERNEL);
513 if (!priv->testmode_trace.cpu_addr)
514 return -ENOMEM;
515 priv->testmode_trace.trace_enabled = true;
516 priv->testmode_trace.trace_addr = (u8 *)PTR_ALIGN(
517 priv->testmode_trace.cpu_addr, 0x100);
518 memset(priv->testmode_trace.trace_addr, 0x03B,
519 priv->testmode_trace.buff_size);
520 skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
521 sizeof(priv->testmode_trace.dma_addr) + 20);
522 if (!skb) {
523 IWL_DEBUG_INFO(priv,
524 "Error allocating memory\n");
525 iwl_trace_cleanup(priv);
526 return -ENOMEM;
528 NLA_PUT(skb, IWL_TM_ATTR_TRACE_ADDR,
529 sizeof(priv->testmode_trace.dma_addr),
530 (u64 *)&priv->testmode_trace.dma_addr);
531 status = cfg80211_testmode_reply(skb);
532 if (status < 0) {
533 IWL_DEBUG_INFO(priv,
534 "Error sending msg : %d\n",
535 status);
537 priv->testmode_trace.num_chunks =
538 DIV_ROUND_UP(priv->testmode_trace.buff_size,
539 TRACE_CHUNK_SIZE);
540 break;
542 case IWL_TM_CMD_APP2DEV_END_TRACE:
543 iwl_trace_cleanup(priv);
544 break;
545 default:
546 IWL_DEBUG_INFO(priv, "Unknown testmode mem command ID\n");
547 return -ENOSYS;
549 return status;
551 nla_put_failure:
552 kfree_skb(skb);
553 if (nla_get_u32(tb[IWL_TM_ATTR_COMMAND]) ==
554 IWL_TM_CMD_APP2DEV_BEGIN_TRACE)
555 iwl_trace_cleanup(priv);
556 return -EMSGSIZE;
559 static int iwl_testmode_trace_dump(struct ieee80211_hw *hw, struct nlattr **tb,
560 struct sk_buff *skb,
561 struct netlink_callback *cb)
563 struct iwl_priv *priv = hw->priv;
564 int idx, length;
566 if (priv->testmode_trace.trace_enabled &&
567 priv->testmode_trace.trace_addr) {
568 idx = cb->args[4];
569 if (idx >= priv->testmode_trace.num_chunks)
570 return -ENOENT;
571 length = TRACE_CHUNK_SIZE;
572 if (((idx + 1) == priv->testmode_trace.num_chunks) &&
573 (priv->testmode_trace.buff_size % TRACE_CHUNK_SIZE))
574 length = priv->testmode_trace.buff_size %
575 TRACE_CHUNK_SIZE;
577 NLA_PUT(skb, IWL_TM_ATTR_TRACE_DUMP, length,
578 priv->testmode_trace.trace_addr +
579 (TRACE_CHUNK_SIZE * idx));
580 idx++;
581 cb->args[4] = idx;
582 return 0;
583 } else
584 return -EFAULT;
586 nla_put_failure:
587 return -ENOBUFS;
590 /* The testmode gnl message handler that takes the gnl message from the
591 * user space and parses it per the policy iwl_testmode_gnl_msg_policy, then
592 * invoke the corresponding handlers.
594 * This function is invoked when there is user space application sending
595 * gnl message through the testmode tunnel NL80211_CMD_TESTMODE regulated
596 * by nl80211.
598 * It retrieves the mandatory field, IWL_TM_ATTR_COMMAND, before
599 * dispatching it to the corresponding handler.
601 * If IWL_TM_ATTR_COMMAND is missing, -ENOMSG is replied to user application;
602 * -ENOSYS is replied to the user application if the command is unknown;
603 * Otherwise, the command is dispatched to the respective handler.
605 * @hw: ieee80211_hw object that represents the device
606 * @data: pointer to user space message
607 * @len: length in byte of @data
609 int iwl_testmode_cmd(struct ieee80211_hw *hw, void *data, int len)
611 struct nlattr *tb[IWL_TM_ATTR_MAX - 1];
612 struct iwl_priv *priv = hw->priv;
613 int result;
615 result = nla_parse(tb, IWL_TM_ATTR_MAX - 1, data, len,
616 iwl_testmode_gnl_msg_policy);
617 if (result != 0) {
618 IWL_DEBUG_INFO(priv,
619 "Error parsing the gnl message : %d\n", result);
620 return result;
623 /* IWL_TM_ATTR_COMMAND is absolutely mandatory */
624 if (!tb[IWL_TM_ATTR_COMMAND]) {
625 IWL_DEBUG_INFO(priv, "Error finding testmode command type\n");
626 return -ENOMSG;
628 /* in case multiple accesses to the device happens */
629 mutex_lock(&priv->mutex);
631 switch (nla_get_u32(tb[IWL_TM_ATTR_COMMAND])) {
632 case IWL_TM_CMD_APP2DEV_UCODE:
633 IWL_DEBUG_INFO(priv, "testmode cmd to uCode\n");
634 result = iwl_testmode_ucode(hw, tb);
635 break;
636 case IWL_TM_CMD_APP2DEV_REG_READ32:
637 case IWL_TM_CMD_APP2DEV_REG_WRITE32:
638 case IWL_TM_CMD_APP2DEV_REG_WRITE8:
639 IWL_DEBUG_INFO(priv, "testmode cmd to register\n");
640 result = iwl_testmode_reg(hw, tb);
641 break;
642 case IWL_TM_CMD_APP2DEV_GET_DEVICENAME:
643 case IWL_TM_CMD_APP2DEV_LOAD_INIT_FW:
644 case IWL_TM_CMD_APP2DEV_CFG_INIT_CALIB:
645 case IWL_TM_CMD_APP2DEV_LOAD_RUNTIME_FW:
646 case IWL_TM_CMD_APP2DEV_GET_EEPROM:
647 case IWL_TM_CMD_APP2DEV_FIXRATE_REQ:
648 IWL_DEBUG_INFO(priv, "testmode cmd to driver\n");
649 result = iwl_testmode_driver(hw, tb);
650 break;
652 case IWL_TM_CMD_APP2DEV_BEGIN_TRACE:
653 case IWL_TM_CMD_APP2DEV_END_TRACE:
654 case IWL_TM_CMD_APP2DEV_READ_TRACE:
655 IWL_DEBUG_INFO(priv, "testmode uCode trace cmd to driver\n");
656 result = iwl_testmode_trace(hw, tb);
657 break;
659 default:
660 IWL_DEBUG_INFO(priv, "Unknown testmode command\n");
661 result = -ENOSYS;
662 break;
665 mutex_unlock(&priv->mutex);
666 return result;
669 int iwl_testmode_dump(struct ieee80211_hw *hw, struct sk_buff *skb,
670 struct netlink_callback *cb,
671 void *data, int len)
673 struct nlattr *tb[IWL_TM_ATTR_MAX];
674 struct iwl_priv *priv = hw->priv;
675 int result;
676 u32 cmd;
678 if (cb->args[3]) {
679 /* offset by 1 since commands start at 0 */
680 cmd = cb->args[3] - 1;
681 } else {
682 result = nla_parse(tb, IWL_TM_ATTR_MAX - 1, data, len,
683 iwl_testmode_gnl_msg_policy);
684 if (result) {
685 IWL_DEBUG_INFO(priv,
686 "Error parsing the gnl message : %d\n", result);
687 return result;
690 /* IWL_TM_ATTR_COMMAND is absolutely mandatory */
691 if (!tb[IWL_TM_ATTR_COMMAND]) {
692 IWL_DEBUG_INFO(priv,
693 "Error finding testmode command type\n");
694 return -ENOMSG;
696 cmd = nla_get_u32(tb[IWL_TM_ATTR_COMMAND]);
697 cb->args[3] = cmd + 1;
700 /* in case multiple accesses to the device happens */
701 mutex_lock(&priv->mutex);
702 switch (cmd) {
703 case IWL_TM_CMD_APP2DEV_READ_TRACE:
704 IWL_DEBUG_INFO(priv, "uCode trace cmd to driver\n");
705 result = iwl_testmode_trace_dump(hw, tb, skb, cb);
706 break;
707 default:
708 result = -EINVAL;
709 break;
712 mutex_unlock(&priv->mutex);
713 return result;