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