Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[pandora-kernel.git] / drivers / net / wireless / iwlwifi / iwl-sv-open.c
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
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
80
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
85  */
86 static
87 struct nla_policy iwl_testmode_gnl_msg_policy[IWL_TM_ATTR_MAX] = {
88         [IWL_TM_ATTR_COMMAND] = { .type = NLA_U32, },
89
90         [IWL_TM_ATTR_UCODE_CMD_ID] = { .type = NLA_U8, },
91         [IWL_TM_ATTR_UCODE_CMD_DATA] = { .type = NLA_UNSPEC, },
92
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, },
96
97         [IWL_TM_ATTR_SYNC_RSP] = { .type = NLA_UNSPEC, },
98         [IWL_TM_ATTR_UCODE_RX_PKT] = { .type = NLA_UNSPEC, },
99
100         [IWL_TM_ATTR_EEPROM] = { .type = NLA_UNSPEC, },
101
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, },
105
106         [IWL_TM_ATTR_FIXRATE] = { .type = NLA_U32, },
107
108 };
109
110 /*
111  * See the struct iwl_rx_packet in iwl-commands.h for the format of the
112  * received events from the device
113  */
114 static inline int get_event_length(struct iwl_rx_mem_buffer *rxb)
115 {
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;
121 }
122
123
124 /*
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.
129  *
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.
133  *
134  * @priv: the instance of iwlwifi device
135  * @rxb: pointer to rx data content received by the ISR
136  *
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
142  */
143
144 static void iwl_testmode_ucode_rx_pkt(struct iwl_priv *priv,
145                                 struct iwl_rx_mem_buffer *rxb)
146 {
147         struct ieee80211_hw *hw = priv->hw;
148         struct sk_buff *skb;
149         void *data;
150         int length;
151
152         data = (void *)rxb_addr(rxb);
153         length = get_event_length(rxb);
154
155         if (!data || length == 0)
156                 return;
157
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;
164         }
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;
169
170 nla_put_failure:
171         kfree_skb(skb);
172         IWL_DEBUG_INFO(priv, "Ouch, overran buffer, check allocation!\n");
173 }
174
175 void iwl_testmode_init(struct iwl_priv *priv)
176 {
177         priv->pre_rx_handler = iwl_testmode_ucode_rx_pkt;
178         priv->testmode_trace.trace_enabled = false;
179 }
180
181 static void iwl_trace_cleanup(struct iwl_priv *priv)
182 {
183         struct device *dev = priv->bus.dev;
184
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;
198         }
199 }
200
201
202 void iwl_testmode_cleanup(struct iwl_priv *priv)
203 {
204         iwl_trace_cleanup(priv);
205 }
206
207 /*
208  * This function handles the user application commands to the ucode.
209  *
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.
213  *
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.
217  *
218  * @hw: ieee80211_hw object that represents the device
219  * @tb: gnl message fields from the user space
220  */
221 static int iwl_testmode_ucode(struct ieee80211_hw *hw, struct nlattr **tb)
222 {
223         struct iwl_priv *priv = hw->priv;
224         struct iwl_host_cmd cmd;
225
226         memset(&cmd, 0, sizeof(struct iwl_host_cmd));
227
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;
233         }
234
235         cmd.id = nla_get_u8(tb[IWL_TM_ATTR_UCODE_CMD_ID]);
236         cmd.data[0] = nla_data(tb[IWL_TM_ATTR_UCODE_CMD_DATA]);
237         cmd.len[0] = nla_len(tb[IWL_TM_ATTR_UCODE_CMD_DATA]);
238         cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
239         IWL_INFO(priv, "testmode ucode command ID 0x%x, flags 0x%x,"
240                                 " len %d\n", cmd.id, cmd.flags, cmd.len[0]);
241         /* ok, let's submit the command to ucode */
242         return iwl_send_cmd(priv, &cmd);
243 }
244
245
246 /*
247  * This function handles the user application commands for register access.
248  *
249  * It retrieves command ID carried with IWL_TM_ATTR_COMMAND and calls to the
250  * handlers respectively.
251  *
252  * If it's an unknown commdn ID, -ENOSYS is returned; or -ENOMSG if the
253  * mandatory fields(IWL_TM_ATTR_REG_OFFSET,IWL_TM_ATTR_REG_VALUE32,
254  * IWL_TM_ATTR_REG_VALUE8) are missing; Otherwise 0 is replied indicating
255  * the success of the command execution.
256  *
257  * If IWL_TM_ATTR_COMMAND is IWL_TM_CMD_APP2DEV_REG_READ32, the register read
258  * value is returned with IWL_TM_ATTR_REG_VALUE32.
259  *
260  * @hw: ieee80211_hw object that represents the device
261  * @tb: gnl message fields from the user space
262  */
263 static int iwl_testmode_reg(struct ieee80211_hw *hw, struct nlattr **tb)
264 {
265         struct iwl_priv *priv = hw->priv;
266         u32 ofs, val32;
267         u8 val8;
268         struct sk_buff *skb;
269         int status = 0;
270
271         if (!tb[IWL_TM_ATTR_REG_OFFSET]) {
272                 IWL_DEBUG_INFO(priv, "Error finding register offset\n");
273                 return -ENOMSG;
274         }
275         ofs = nla_get_u32(tb[IWL_TM_ATTR_REG_OFFSET]);
276         IWL_INFO(priv, "testmode register access command offset 0x%x\n", ofs);
277
278         switch (nla_get_u32(tb[IWL_TM_ATTR_COMMAND])) {
279         case IWL_TM_CMD_APP2DEV_REG_READ32:
280                 val32 = iwl_read32(priv, ofs);
281                 IWL_INFO(priv, "32bit value to read 0x%x\n", val32);
282
283                 skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy, 20);
284                 if (!skb) {
285                         IWL_DEBUG_INFO(priv, "Error allocating memory\n");
286                         return -ENOMEM;
287                 }
288                 NLA_PUT_U32(skb, IWL_TM_ATTR_REG_VALUE32, val32);
289                 status = cfg80211_testmode_reply(skb);
290                 if (status < 0)
291                         IWL_DEBUG_INFO(priv,
292                                        "Error sending msg : %d\n", status);
293                 break;
294         case IWL_TM_CMD_APP2DEV_REG_WRITE32:
295                 if (!tb[IWL_TM_ATTR_REG_VALUE32]) {
296                         IWL_DEBUG_INFO(priv,
297                                        "Error finding value to write\n");
298                         return -ENOMSG;
299                 } else {
300                         val32 = nla_get_u32(tb[IWL_TM_ATTR_REG_VALUE32]);
301                         IWL_INFO(priv, "32bit value to write 0x%x\n", val32);
302                         iwl_write32(priv, ofs, val32);
303                 }
304                 break;
305         case IWL_TM_CMD_APP2DEV_REG_WRITE8:
306                 if (!tb[IWL_TM_ATTR_REG_VALUE8]) {
307                         IWL_DEBUG_INFO(priv, "Error finding value to write\n");
308                         return -ENOMSG;
309                 } else {
310                         val8 = nla_get_u8(tb[IWL_TM_ATTR_REG_VALUE8]);
311                         IWL_INFO(priv, "8bit value to write 0x%x\n", val8);
312                         iwl_write8(priv, ofs, val8);
313                 }
314                 break;
315         default:
316                 IWL_DEBUG_INFO(priv, "Unknown testmode register command ID\n");
317                 return -ENOSYS;
318         }
319
320         return status;
321
322 nla_put_failure:
323         kfree_skb(skb);
324         return -EMSGSIZE;
325 }
326
327
328 static int iwl_testmode_cfg_init_calib(struct iwl_priv *priv)
329 {
330         struct iwl_notification_wait calib_wait;
331         int ret;
332
333         iwlagn_init_notification_wait(priv, &calib_wait,
334                                       CALIBRATION_COMPLETE_NOTIFICATION,
335                                       NULL, NULL);
336         ret = iwlagn_init_alive_start(priv);
337         if (ret) {
338                 IWL_DEBUG_INFO(priv,
339                         "Error configuring init calibration: %d\n", ret);
340                 goto cfg_init_calib_error;
341         }
342
343         ret = iwlagn_wait_notification(priv, &calib_wait, 2 * HZ);
344         if (ret)
345                 IWL_DEBUG_INFO(priv, "Error detecting"
346                         " CALIBRATION_COMPLETE_NOTIFICATION: %d\n", ret);
347         return ret;
348
349 cfg_init_calib_error:
350         iwlagn_remove_notification(priv, &calib_wait);
351         return ret;
352 }
353
354 /*
355  * This function handles the user application commands for driver.
356  *
357  * It retrieves command ID carried with IWL_TM_ATTR_COMMAND and calls to the
358  * handlers respectively.
359  *
360  * If it's an unknown commdn ID, -ENOSYS is replied; otherwise, the returned
361  * value of the actual command execution is replied to the user application.
362  *
363  * If there's any message responding to the user space, IWL_TM_ATTR_SYNC_RSP
364  * is used for carry the message while IWL_TM_ATTR_COMMAND must set to
365  * IWL_TM_CMD_DEV2APP_SYNC_RSP.
366  *
367  * @hw: ieee80211_hw object that represents the device
368  * @tb: gnl message fields from the user space
369  */
370 static int iwl_testmode_driver(struct ieee80211_hw *hw, struct nlattr **tb)
371 {
372         struct iwl_priv *priv = hw->priv;
373         struct sk_buff *skb;
374         unsigned char *rsp_data_ptr = NULL;
375         int status = 0, rsp_data_len = 0;
376
377         switch (nla_get_u32(tb[IWL_TM_ATTR_COMMAND])) {
378         case IWL_TM_CMD_APP2DEV_GET_DEVICENAME:
379                 rsp_data_ptr = (unsigned char *)priv->cfg->name;
380                 rsp_data_len = strlen(priv->cfg->name);
381                 skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
382                                                         rsp_data_len + 20);
383                 if (!skb) {
384                         IWL_DEBUG_INFO(priv,
385                                        "Error allocating memory\n");
386                         return -ENOMEM;
387                 }
388                 NLA_PUT_U32(skb, IWL_TM_ATTR_COMMAND,
389                             IWL_TM_CMD_DEV2APP_SYNC_RSP);
390                 NLA_PUT(skb, IWL_TM_ATTR_SYNC_RSP,
391                         rsp_data_len, rsp_data_ptr);
392                 status = cfg80211_testmode_reply(skb);
393                 if (status < 0)
394                         IWL_DEBUG_INFO(priv, "Error sending msg : %d\n",
395                                        status);
396                 break;
397
398         case IWL_TM_CMD_APP2DEV_LOAD_INIT_FW:
399                 status = iwlagn_load_ucode_wait_alive(priv, &priv->ucode_init,
400                                                       IWL_UCODE_INIT);
401                 if (status)
402                         IWL_DEBUG_INFO(priv,
403                                 "Error loading init ucode: %d\n", status);
404                 break;
405
406         case IWL_TM_CMD_APP2DEV_CFG_INIT_CALIB:
407                 iwl_testmode_cfg_init_calib(priv);
408                 iwlagn_stop_device(priv);
409                 break;
410
411         case IWL_TM_CMD_APP2DEV_LOAD_RUNTIME_FW:
412                 status = iwlagn_load_ucode_wait_alive(priv,
413                                            &priv->ucode_rt,
414                                            IWL_UCODE_REGULAR);
415                 if (status) {
416                         IWL_DEBUG_INFO(priv,
417                                 "Error loading runtime ucode: %d\n", status);
418                         break;
419                 }
420                 status = iwl_alive_start(priv);
421                 if (status)
422                         IWL_DEBUG_INFO(priv,
423                                 "Error starting the device: %d\n", status);
424                 break;
425
426         case IWL_TM_CMD_APP2DEV_GET_EEPROM:
427                 if (priv->eeprom) {
428                         skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
429                                 priv->cfg->base_params->eeprom_size + 20);
430                         if (!skb) {
431                                 IWL_DEBUG_INFO(priv,
432                                        "Error allocating memory\n");
433                                 return -ENOMEM;
434                         }
435                         NLA_PUT_U32(skb, IWL_TM_ATTR_COMMAND,
436                                 IWL_TM_CMD_DEV2APP_EEPROM_RSP);
437                         NLA_PUT(skb, IWL_TM_ATTR_EEPROM,
438                                 priv->cfg->base_params->eeprom_size,
439                                 priv->eeprom);
440                         status = cfg80211_testmode_reply(skb);
441                         if (status < 0)
442                                 IWL_DEBUG_INFO(priv,
443                                                "Error sending msg : %d\n",
444                                                status);
445                 } else
446                         return -EFAULT;
447                 break;
448
449         case IWL_TM_CMD_APP2DEV_FIXRATE_REQ:
450                 if (!tb[IWL_TM_ATTR_FIXRATE]) {
451                         IWL_DEBUG_INFO(priv,
452                                        "Error finding fixrate setting\n");
453                         return -ENOMSG;
454                 }
455                 priv->dbg_fixed_rate = nla_get_u32(tb[IWL_TM_ATTR_FIXRATE]);
456                 break;
457
458         default:
459                 IWL_DEBUG_INFO(priv, "Unknown testmode driver command ID\n");
460                 return -ENOSYS;
461         }
462         return status;
463
464 nla_put_failure:
465         kfree_skb(skb);
466         return -EMSGSIZE;
467 }
468
469
470 /*
471  * This function handles the user application commands for uCode trace
472  *
473  * It retrieves command ID carried with IWL_TM_ATTR_COMMAND and calls to the
474  * handlers respectively.
475  *
476  * If it's an unknown commdn ID, -ENOSYS is replied; otherwise, the returned
477  * value of the actual command execution is replied to the user application.
478  *
479  * @hw: ieee80211_hw object that represents the device
480  * @tb: gnl message fields from the user space
481  */
482 static int iwl_testmode_trace(struct ieee80211_hw *hw, struct nlattr **tb)
483 {
484         struct iwl_priv *priv = hw->priv;
485         struct sk_buff *skb;
486         int status = 0;
487         struct device *dev = priv->bus.dev;
488
489         switch (nla_get_u32(tb[IWL_TM_ATTR_COMMAND])) {
490         case IWL_TM_CMD_APP2DEV_BEGIN_TRACE:
491                 if (priv->testmode_trace.trace_enabled)
492                         return -EBUSY;
493
494                 if (!tb[IWL_TM_ATTR_TRACE_SIZE])
495                         priv->testmode_trace.buff_size = TRACE_BUFF_SIZE_DEF;
496                 else
497                         priv->testmode_trace.buff_size =
498                                 nla_get_u32(tb[IWL_TM_ATTR_TRACE_SIZE]);
499                 if (!priv->testmode_trace.buff_size)
500                         return -EINVAL;
501                 if (priv->testmode_trace.buff_size < TRACE_BUFF_SIZE_MIN ||
502                     priv->testmode_trace.buff_size > TRACE_BUFF_SIZE_MAX)
503                         return -EINVAL;
504
505                 priv->testmode_trace.total_size =
506                         priv->testmode_trace.buff_size + TRACE_BUFF_PADD;
507                 priv->testmode_trace.cpu_addr =
508                         dma_alloc_coherent(dev,
509                                            priv->testmode_trace.total_size,
510                                            &priv->testmode_trace.dma_addr,
511                                            GFP_KERNEL);
512                 if (!priv->testmode_trace.cpu_addr)
513                         return -ENOMEM;
514                 priv->testmode_trace.trace_enabled = true;
515                 priv->testmode_trace.trace_addr = (u8 *)PTR_ALIGN(
516                         priv->testmode_trace.cpu_addr, 0x100);
517                 memset(priv->testmode_trace.trace_addr, 0x03B,
518                         priv->testmode_trace.buff_size);
519                 skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
520                         sizeof(priv->testmode_trace.dma_addr) + 20);
521                 if (!skb) {
522                         IWL_DEBUG_INFO(priv,
523                                 "Error allocating memory\n");
524                         iwl_trace_cleanup(priv);
525                         return -ENOMEM;
526                 }
527                 NLA_PUT(skb, IWL_TM_ATTR_TRACE_ADDR,
528                         sizeof(priv->testmode_trace.dma_addr),
529                         (u64 *)&priv->testmode_trace.dma_addr);
530                 status = cfg80211_testmode_reply(skb);
531                 if (status < 0) {
532                         IWL_DEBUG_INFO(priv,
533                                        "Error sending msg : %d\n",
534                                        status);
535                 }
536                 priv->testmode_trace.num_chunks =
537                         DIV_ROUND_UP(priv->testmode_trace.buff_size,
538                                      TRACE_CHUNK_SIZE);
539                 break;
540
541         case IWL_TM_CMD_APP2DEV_END_TRACE:
542                 iwl_trace_cleanup(priv);
543                 break;
544         default:
545                 IWL_DEBUG_INFO(priv, "Unknown testmode mem command ID\n");
546                 return -ENOSYS;
547         }
548         return status;
549
550 nla_put_failure:
551         kfree_skb(skb);
552         if (nla_get_u32(tb[IWL_TM_ATTR_COMMAND]) ==
553             IWL_TM_CMD_APP2DEV_BEGIN_TRACE)
554                 iwl_trace_cleanup(priv);
555         return -EMSGSIZE;
556 }
557
558 static int iwl_testmode_trace_dump(struct ieee80211_hw *hw, struct nlattr **tb,
559                                    struct sk_buff *skb,
560                                    struct netlink_callback *cb)
561 {
562         struct iwl_priv *priv = hw->priv;
563         int idx, length;
564
565         if (priv->testmode_trace.trace_enabled &&
566             priv->testmode_trace.trace_addr) {
567                 idx = cb->args[4];
568                 if (idx >= priv->testmode_trace.num_chunks)
569                         return -ENOENT;
570                 length = TRACE_CHUNK_SIZE;
571                 if (((idx + 1) == priv->testmode_trace.num_chunks) &&
572                     (priv->testmode_trace.buff_size % TRACE_CHUNK_SIZE))
573                         length = priv->testmode_trace.buff_size %
574                                 TRACE_CHUNK_SIZE;
575
576                 NLA_PUT(skb, IWL_TM_ATTR_TRACE_DUMP, length,
577                         priv->testmode_trace.trace_addr +
578                         (TRACE_CHUNK_SIZE * idx));
579                 idx++;
580                 cb->args[4] = idx;
581                 return 0;
582         } else
583                 return -EFAULT;
584
585  nla_put_failure:
586         return -ENOBUFS;
587 }
588
589 /* The testmode gnl message handler that takes the gnl message from the
590  * user space and parses it per the policy iwl_testmode_gnl_msg_policy, then
591  * invoke the corresponding handlers.
592  *
593  * This function is invoked when there is user space application sending
594  * gnl message through the testmode tunnel NL80211_CMD_TESTMODE regulated
595  * by nl80211.
596  *
597  * It retrieves the mandatory field, IWL_TM_ATTR_COMMAND, before
598  * dispatching it to the corresponding handler.
599  *
600  * If IWL_TM_ATTR_COMMAND is missing, -ENOMSG is replied to user application;
601  * -ENOSYS is replied to the user application if the command is unknown;
602  * Otherwise, the command is dispatched to the respective handler.
603  *
604  * @hw: ieee80211_hw object that represents the device
605  * @data: pointer to user space message
606  * @len: length in byte of @data
607  */
608 int iwl_testmode_cmd(struct ieee80211_hw *hw, void *data, int len)
609 {
610         struct nlattr *tb[IWL_TM_ATTR_MAX - 1];
611         struct iwl_priv *priv = hw->priv;
612         int result;
613
614         result = nla_parse(tb, IWL_TM_ATTR_MAX - 1, data, len,
615                         iwl_testmode_gnl_msg_policy);
616         if (result != 0) {
617                 IWL_DEBUG_INFO(priv,
618                                "Error parsing the gnl message : %d\n", result);
619                 return result;
620         }
621
622         /* IWL_TM_ATTR_COMMAND is absolutely mandatory */
623         if (!tb[IWL_TM_ATTR_COMMAND]) {
624                 IWL_DEBUG_INFO(priv, "Error finding testmode command type\n");
625                 return -ENOMSG;
626         }
627         /* in case multiple accesses to the device happens */
628         mutex_lock(&priv->mutex);
629
630         switch (nla_get_u32(tb[IWL_TM_ATTR_COMMAND])) {
631         case IWL_TM_CMD_APP2DEV_UCODE:
632                 IWL_DEBUG_INFO(priv, "testmode cmd to uCode\n");
633                 result = iwl_testmode_ucode(hw, tb);
634                 break;
635         case IWL_TM_CMD_APP2DEV_REG_READ32:
636         case IWL_TM_CMD_APP2DEV_REG_WRITE32:
637         case IWL_TM_CMD_APP2DEV_REG_WRITE8:
638                 IWL_DEBUG_INFO(priv, "testmode cmd to register\n");
639                 result = iwl_testmode_reg(hw, tb);
640                 break;
641         case IWL_TM_CMD_APP2DEV_GET_DEVICENAME:
642         case IWL_TM_CMD_APP2DEV_LOAD_INIT_FW:
643         case IWL_TM_CMD_APP2DEV_CFG_INIT_CALIB:
644         case IWL_TM_CMD_APP2DEV_LOAD_RUNTIME_FW:
645         case IWL_TM_CMD_APP2DEV_GET_EEPROM:
646         case IWL_TM_CMD_APP2DEV_FIXRATE_REQ:
647                 IWL_DEBUG_INFO(priv, "testmode cmd to driver\n");
648                 result = iwl_testmode_driver(hw, tb);
649                 break;
650
651         case IWL_TM_CMD_APP2DEV_BEGIN_TRACE:
652         case IWL_TM_CMD_APP2DEV_END_TRACE:
653         case IWL_TM_CMD_APP2DEV_READ_TRACE:
654                 IWL_DEBUG_INFO(priv, "testmode uCode trace cmd to driver\n");
655                 result = iwl_testmode_trace(hw, tb);
656                 break;
657
658         default:
659                 IWL_DEBUG_INFO(priv, "Unknown testmode command\n");
660                 result = -ENOSYS;
661                 break;
662         }
663
664         mutex_unlock(&priv->mutex);
665         return result;
666 }
667
668 int iwl_testmode_dump(struct ieee80211_hw *hw, struct sk_buff *skb,
669                       struct netlink_callback *cb,
670                       void *data, int len)
671 {
672         struct nlattr *tb[IWL_TM_ATTR_MAX];
673         struct iwl_priv *priv = hw->priv;
674         int result;
675         u32 cmd;
676
677         if (cb->args[3]) {
678                 /* offset by 1 since commands start at 0 */
679                 cmd = cb->args[3] - 1;
680         } else {
681                 result = nla_parse(tb, IWL_TM_ATTR_MAX - 1, data, len,
682                                 iwl_testmode_gnl_msg_policy);
683                 if (result) {
684                         IWL_DEBUG_INFO(priv,
685                                "Error parsing the gnl message : %d\n", result);
686                         return result;
687                 }
688
689                 /* IWL_TM_ATTR_COMMAND is absolutely mandatory */
690                 if (!tb[IWL_TM_ATTR_COMMAND]) {
691                         IWL_DEBUG_INFO(priv,
692                                 "Error finding testmode command type\n");
693                         return -ENOMSG;
694                 }
695                 cmd = nla_get_u32(tb[IWL_TM_ATTR_COMMAND]);
696                 cb->args[3] = cmd + 1;
697         }
698
699         /* in case multiple accesses to the device happens */
700         mutex_lock(&priv->mutex);
701         switch (cmd) {
702         case IWL_TM_CMD_APP2DEV_READ_TRACE:
703                 IWL_DEBUG_INFO(priv, "uCode trace cmd to driver\n");
704                 result = iwl_testmode_trace_dump(hw, tb, skb, cb);
705                 break;
706         default:
707                 result = -EINVAL;
708                 break;
709         }
710
711         mutex_unlock(&priv->mutex);
712         return result;
713 }