2 * mac80211 glue code for mac80211 ST-Ericsson CW1200 drivers
5 * Copyright (c) 2010, ST-Ericsson
6 * Author: Dmitry Tarnyagin <dmitry.tarnyagin@lockless.no>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/module.h>
14 #include <linux/debugfs.h>
15 #include <linux/poll.h>
16 #include <linux/time.h>
17 #include <linux/random.h>
18 #include <linux/kallsyms.h>
19 #include <net/mac80211.h>
25 static int __cw1200_itp_open(struct cw1200_common
*priv
);
26 static int __cw1200_itp_close(struct cw1200_common
*priv
);
27 static void cw1200_itp_rx_start(struct cw1200_common
*priv
);
28 static void cw1200_itp_rx_stop(struct cw1200_common
*priv
);
29 static void cw1200_itp_rx_stats(struct cw1200_common
*priv
);
30 static void cw1200_itp_rx_reset(struct cw1200_common
*priv
);
31 static void cw1200_itp_tx_stop(struct cw1200_common
*priv
);
32 static void cw1200_itp_handle(struct cw1200_common
*priv
,
34 static void cw1200_itp_err(struct cw1200_common
*priv
,
37 static void __cw1200_itp_tx_stop(struct cw1200_common
*priv
);
39 static ssize_t
cw1200_itp_read(struct file
*file
,
40 char __user
*user_buf
, size_t count
, loff_t
*ppos
)
42 struct cw1200_common
*priv
= file
->private_data
;
43 struct cw1200_itp
*itp
= &priv
->debug
->itp
;
47 if (skb_queue_empty(&itp
->log_queue
))
50 skb
= skb_dequeue(&itp
->log_queue
);
51 ret
= copy_to_user(user_buf
, skb
->data
, skb
->len
);
53 skb
->data
[skb
->len
] = 0;
54 pr_debug("[ITP] >>> %s", skb
->data
);
57 return skb
->len
- ret
;
60 static ssize_t
cw1200_itp_write(struct file
*file
,
61 const char __user
*user_buf
, size_t count
, loff_t
*ppos
)
63 struct cw1200_common
*priv
= file
->private_data
;
66 if (!count
|| count
> 1024)
68 skb
= dev_alloc_skb(count
+ 1);
72 skb_put(skb
, count
+ 1);
73 if (copy_from_user(skb
->data
, user_buf
, count
)) {
79 cw1200_itp_handle(priv
, skb
);
84 static unsigned int cw1200_itp_poll(struct file
*file
, poll_table
*wait
)
86 struct cw1200_common
*priv
= file
->private_data
;
87 struct cw1200_itp
*itp
= &priv
->debug
->itp
;
88 unsigned int mask
= 0;
90 poll_wait(file
, &itp
->read_wait
, wait
);
92 if (!skb_queue_empty(&itp
->log_queue
))
93 mask
|= POLLIN
| POLLRDNORM
;
95 mask
|= POLLOUT
| POLLWRNORM
;
100 static int cw1200_itp_open(struct inode
*inode
, struct file
*file
)
102 struct cw1200_common
*priv
= inode
->i_private
;
103 struct cw1200_itp
*itp
= &priv
->debug
->itp
;
106 file
->private_data
= priv
;
107 if (atomic_inc_return(&itp
->open_count
) == 1) {
108 ret
= __cw1200_itp_open(priv
);
109 if (ret
&& !atomic_dec_return(&itp
->open_count
))
110 __cw1200_itp_close(priv
);
112 atomic_dec(&itp
->open_count
);
119 static int cw1200_itp_close(struct inode
*inode
, struct file
*file
)
121 struct cw1200_common
*priv
= file
->private_data
;
122 struct cw1200_itp
*itp
= &priv
->debug
->itp
;
123 if (!atomic_dec_return(&itp
->open_count
)) {
124 __cw1200_itp_close(priv
);
125 wake_up(&itp
->close_wait
);
130 static const struct file_operations fops_itp
= {
131 .open
= cw1200_itp_open
,
132 .read
= cw1200_itp_read
,
133 .write
= cw1200_itp_write
,
134 .poll
= cw1200_itp_poll
,
135 .release
= cw1200_itp_close
,
136 .llseek
= default_llseek
,
137 .owner
= THIS_MODULE
,
140 static void cw1200_itp_fill_pattern(u8
*data
, int size
,
141 enum cw1200_itp_data_modes mode
)
149 memset(data
, 0x0, size
);
152 memset(data
, 0xff, size
);
154 case ITP_DATA_ZERONES
:
155 memset(data
, 0x55, size
);
157 case ITP_DATA_RANDOM
:
158 get_random_bytes(data
, size
);
164 static void cw1200_itp_tx_work(struct work_struct
*work
)
166 struct cw1200_itp
*itp
= container_of(work
, struct cw1200_itp
,
168 struct cw1200_common
*priv
= itp
->priv
;
169 atomic_set(&priv
->bh_tx
, 1);
170 wake_up(&priv
->bh_wq
);
173 static void cw1200_itp_tx_finish(struct work_struct
*work
)
175 struct cw1200_itp
*itp
= container_of(work
, struct cw1200_itp
,
177 __cw1200_itp_tx_stop(itp
->priv
);
180 int cw1200_itp_init(struct cw1200_common
*priv
)
182 struct cw1200_itp
*itp
= &priv
->debug
->itp
;
185 atomic_set(&itp
->open_count
, 0);
186 atomic_set(&itp
->stop_tx
, 0);
187 atomic_set(&itp
->awaiting_confirm
, 0);
188 skb_queue_head_init(&itp
->log_queue
);
189 spin_lock_init(&itp
->tx_lock
);
190 init_waitqueue_head(&itp
->read_wait
);
191 init_waitqueue_head(&itp
->write_wait
);
192 init_waitqueue_head(&itp
->close_wait
);
193 INIT_DELAYED_WORK(&itp
->tx_work
, cw1200_itp_tx_work
);
194 INIT_DELAYED_WORK(&itp
->tx_finish
, cw1200_itp_tx_finish
);
196 itp
->hdr_len
= WSM_TX_EXTRA_HEADROOM
+
197 sizeof(struct ieee80211_hdr_3addr
);
199 if (!debugfs_create_file("itp", S_IRUSR
| S_IWUSR
,
200 priv
->debug
->debugfs_phy
, priv
, &fops_itp
))
206 void cw1200_itp_release(struct cw1200_common
*priv
)
208 struct cw1200_itp
*itp
= &priv
->debug
->itp
;
210 wait_event_interruptible(itp
->close_wait
,
211 !atomic_read(&itp
->open_count
));
213 WARN_ON(atomic_read(&itp
->open_count
));
215 skb_queue_purge(&itp
->log_queue
);
216 cw1200_itp_tx_stop(priv
);
219 static int __cw1200_itp_open(struct cw1200_common
*priv
)
221 struct cw1200_itp
*itp
= &priv
->debug
->itp
;
225 if (priv
->join_status
)
227 itp
->saved_channel
= priv
->channel
;
229 priv
->channel
= &priv
->hw
->wiphy
->bands
[IEEE80211_BAND_2GHZ
]->channels
[0];
230 wsm_set_bssid_filtering(priv
, false);
231 cw1200_itp_rx_reset(priv
);
235 static int __cw1200_itp_close(struct cw1200_common
*priv
)
237 struct cw1200_itp
*itp
= &priv
->debug
->itp
;
238 if (atomic_read(&itp
->test_mode
) == TEST_MODE_RX_TEST
)
239 cw1200_itp_rx_stop(priv
);
240 cw1200_itp_tx_stop(priv
);
241 cw1200_disable_listening(priv
);
242 cw1200_update_filtering(priv
);
243 priv
->channel
= itp
->saved_channel
;
247 bool cw1200_is_itp(struct cw1200_common
*priv
)
249 struct cw1200_itp
*itp
= &priv
->debug
->itp
;
250 return atomic_read(&itp
->open_count
) != 0;
253 static void cw1200_itp_rx_reset(struct cw1200_common
*priv
)
255 struct cw1200_itp
*itp
= &priv
->debug
->itp
;
258 itp
->rx_rssi_max
= -1000;
259 itp
->rx_rssi_min
= 1000;
262 static void cw1200_itp_rx_start(struct cw1200_common
*priv
)
264 struct cw1200_itp
*itp
= &priv
->debug
->itp
;
266 pr_debug("[ITP] RX start, band = %d, ch = %d\n",
268 atomic_set(&itp
->test_mode
, TEST_MODE_RX_TEST
);
269 cw1200_update_listening(priv
, false);
270 priv
->channel
= &priv
->hw
->
271 wiphy
->bands
[itp
->band
]->channels
[itp
->ch
];
272 cw1200_update_listening(priv
, true);
273 wsm_set_bssid_filtering(priv
, false);
276 static void cw1200_itp_rx_stop(struct cw1200_common
*priv
)
278 struct cw1200_itp
*itp
= &priv
->debug
->itp
;
279 pr_debug("[ITP] RX stop\n");
280 atomic_set(&itp
->test_mode
, TEST_MODE_NO_TEST
);
281 cw1200_itp_rx_reset(priv
);
284 static void cw1200_itp_rx_stats(struct cw1200_common
*priv
)
286 struct cw1200_itp
*itp
= &priv
->debug
->itp
;
290 struct wsm_mib_counters_table counters
;
292 ret
= wsm_get_counters_table(priv
, &counters
);
295 cw1200_itp_err(priv
, -EBUSY
, 20);
298 len
= snprintf(buf
, sizeof(buf
), "1,0,0,0,0,%d\n",
299 counters
.rx_packet_errors
);
301 len
= snprintf(buf
, sizeof(buf
), "1,%d,%ld,%d,%d,%d\n",
303 itp
->rx_cnt
? itp
->rx_rssi
/ itp
->rx_cnt
: 0,
304 itp
->rx_rssi_min
, itp
->rx_rssi_max
,
305 counters
.rx_packet_errors
);
308 cw1200_itp_err(priv
, -EBUSY
, 21);
312 skb
= dev_alloc_skb(len
);
314 cw1200_itp_err(priv
, -ENOMEM
, 22);
320 itp
->rx_rssi_max
= -1000;
321 itp
->rx_rssi_min
= 1000;
326 memcpy(skb
->data
, buf
, len
);
327 skb_queue_tail(&itp
->log_queue
, skb
);
328 wake_up(&itp
->read_wait
);
331 static void cw1200_itp_tx_start(struct cw1200_common
*priv
)
334 struct ieee80211_hdr_3addr
*hdr
;
335 struct cw1200_itp
*itp
= &priv
->debug
->itp
;
336 struct wsm_mib_association_mode assoc_mode
= {
337 .flags
= WSM_ASSOCIATION_MODE_USE_PREAMBLE_TYPE
,
338 .preamble
= itp
->preamble
,
341 u8 da_addr
[6] = ITP_DEFAULT_DA_ADDR
;
343 /* Rates index 4 and 5 are not supported */
347 pr_debug("[ITP] TX start: band = %d, ch = %d, rate = %d, preamble = %d, number = %d, data_mode = %d, interval = %d, power = %d, data_len = %d\n",
348 itp
->band
, itp
->ch
, itp
->rate
, itp
->preamble
,
349 itp
->number
, itp
->data_mode
, itp
->interval_us
,
350 itp
->power
, itp
->data_len
);
352 len
= itp
->hdr_len
+ itp
->data_len
;
354 itp
->data
= kmalloc(len
, GFP_KERNEL
);
355 tx
= (struct wsm_tx
*)itp
->data
;
356 tx
->hdr
.len
= itp
->data_len
+ itp
->hdr_len
;
357 tx
->hdr
.id
= __cpu_to_le16(0x0004 | 1 << 6);
358 tx
->max_tx_rate
= itp
->rate
;
362 tx
->packet_id
= 0x55ff55;
366 if (itp
->preamble
== ITP_PREAMBLE_GREENFIELD
)
367 tx
->ht_tx_parameters
= WSM_HT_TX_GREENFIELD
;
368 else if (itp
->preamble
== ITP_PREAMBLE_MIXED
)
369 tx
->ht_tx_parameters
= WSM_HT_TX_MIXED
;
371 hdr
= (struct ieee80211_hdr_3addr
*)&itp
->data
[sizeof(struct wsm_tx
)];
372 memset(hdr
, 0, sizeof(*hdr
));
373 hdr
->frame_control
= cpu_to_le16(IEEE80211_FTYPE_DATA
| IEEE80211_FCTL_TODS
);
374 memcpy(hdr
->addr1
, da_addr
, ETH_ALEN
);
375 memcpy(hdr
->addr2
, priv
->vif
->addr
, ETH_ALEN
);
376 memcpy(hdr
->addr3
, da_addr
, ETH_ALEN
);
378 cw1200_itp_fill_pattern(&itp
->data
[itp
->hdr_len
],
379 itp
->data_len
, itp
->data_mode
);
381 cw1200_update_listening(priv
, false);
382 priv
->channel
= &priv
->hw
->wiphy
->bands
[itp
->band
]->channels
[itp
->ch
];
383 WARN_ON(wsm_set_output_power(priv
, itp
->power
));
384 if (itp
->preamble
== ITP_PREAMBLE_SHORT
||
385 itp
->preamble
== ITP_PREAMBLE_LONG
)
386 WARN_ON(wsm_set_association_mode(priv
,
388 wsm_set_bssid_filtering(priv
, false);
389 cw1200_update_listening(priv
, true);
391 spin_lock_bh(&itp
->tx_lock
);
392 atomic_set(&itp
->test_mode
, TEST_MODE_TX_TEST
);
393 atomic_set(&itp
->awaiting_confirm
, 0);
394 atomic_set(&itp
->stop_tx
, 0);
395 atomic_set(&priv
->bh_tx
, 1);
396 ktime_get_ts(&itp
->last_sent
);
397 wake_up(&priv
->bh_wq
);
398 spin_unlock_bh(&itp
->tx_lock
);
401 void __cw1200_itp_tx_stop(struct cw1200_common
*priv
)
403 struct cw1200_itp
*itp
= &priv
->debug
->itp
;
404 spin_lock_bh(&itp
->tx_lock
);
407 atomic_set(&itp
->test_mode
, TEST_MODE_NO_TEST
);
408 spin_unlock_bh(&itp
->tx_lock
);
411 static void cw1200_itp_tx_stop(struct cw1200_common
*priv
)
413 struct cw1200_itp
*itp
= &priv
->debug
->itp
;
414 pr_debug("[ITP] TX stop\n");
415 atomic_set(&itp
->stop_tx
, 1);
416 flush_workqueue(priv
->workqueue
);
418 /* time for FW to confirm all tx requests */
421 __cw1200_itp_tx_stop(priv
);
424 static int cw1200_print_fw_version(struct cw1200_common
*priv
,
427 return snprintf(buf
, len
, "%s %d.%d",
428 cw1200_fw_types
[priv
->wsm_caps
.fw_type
],
429 priv
->wsm_caps
.fw_ver
,
430 priv
->wsm_caps
.fw_build
);
433 static void cw1200_itp_get_version(struct cw1200_common
*priv
,
434 enum cw1200_itp_version_type type
)
436 struct cw1200_itp
*itp
= &priv
->debug
->itp
;
438 char buf
[ITP_BUF_SIZE
];
441 pr_debug("[ITP] print %s version\n",
442 type
== ITP_CHIP_ID
? "chip" : "firmware");
444 len
= snprintf(buf
, ITP_BUF_SIZE
, "2,");
446 cw1200_itp_err(priv
, -EINVAL
, 40);
453 len
= cw1200_print_fw_version(priv
, buf
+size
,
454 ITP_BUF_SIZE
- size
);
457 cw1200_itp_err(priv
, -EINVAL
, 41);
463 len
= snprintf(buf
+size
, ITP_BUF_SIZE
- size
,
464 "%d.%d", priv
->wsm_caps
.hw_id
,
465 priv
->wsm_caps
.hw_subid
);
467 cw1200_itp_err(priv
, -EINVAL
, 42);
473 cw1200_itp_err(priv
, -EINVAL
, 43);
477 len
= snprintf(buf
+size
, ITP_BUF_SIZE
-size
, "\n");
479 cw1200_itp_err(priv
, -EINVAL
, 44);
484 skb
= dev_alloc_skb(size
);
486 cw1200_itp_err(priv
, -ENOMEM
, 45);
493 memcpy(skb
->data
, buf
, size
);
494 skb_queue_tail(&itp
->log_queue
, skb
);
495 wake_up(&itp
->read_wait
);
498 int cw1200_itp_get_tx(struct cw1200_common
*priv
, u8
**data
,
499 size_t *tx_len
, int *burst
)
501 struct cw1200_itp
*itp
;
508 itp
= &priv
->debug
->itp
;
513 spin_lock_bh(&itp
->tx_lock
);
514 if (atomic_read(&itp
->test_mode
) != TEST_MODE_TX_TEST
)
517 if (atomic_read(&itp
->stop_tx
))
520 if (itp
->number
== 0) {
521 atomic_set(&itp
->stop_tx
, 1);
522 queue_delayed_work(priv
->workqueue
, &itp
->tx_finish
, HZ
/10);
529 if (priv
->hw_bufs_used
>= 2) {
530 if (!atomic_read(&priv
->bh_rx
))
531 atomic_set(&priv
->bh_rx
, 1);
532 atomic_set(&priv
->bh_tx
, 1);
537 time_left_us
= (itp
->last_sent
.tv_sec
- now
.tv_sec
)*1000000 +
538 (itp
->last_sent
.tv_nsec
- now
.tv_nsec
)/1000 +
541 if (time_left_us
> ITP_TIME_THRES_US
) {
542 queue_delayed_work(priv
->workqueue
, &itp
->tx_work
,
543 ITP_US_TO_MS(time_left_us
)*HZ
/1000);
547 if (time_left_us
> 50)
548 udelay(time_left_us
);
554 *tx_len
= itp
->data_len
+ itp
->hdr_len
;
556 if (itp
->data_mode
== ITP_DATA_RANDOM
)
557 cw1200_itp_fill_pattern(&itp
->data
[itp
->hdr_len
],
558 itp
->data_len
, itp
->data_mode
);
560 atomic_set(&priv
->bh_tx
, 1);
561 ktime_get_ts(&itp
->last_sent
);
562 atomic_add(1, &itp
->awaiting_confirm
);
563 spin_unlock_bh(&itp
->tx_lock
);
567 spin_unlock_bh(&itp
->tx_lock
);
571 bool cw1200_itp_rxed(struct cw1200_common
*priv
, struct sk_buff
*skb
)
573 struct cw1200_itp
*itp
= &priv
->debug
->itp
;
574 struct ieee80211_rx_status
*rx
= IEEE80211_SKB_RXCB(skb
);
577 if (atomic_read(&itp
->test_mode
) != TEST_MODE_RX_TEST
)
578 return cw1200_is_itp(priv
);
579 if (rx
->freq
!= priv
->channel
->center_freq
)
584 itp
->rx_rssi
+= signal
;
585 if (itp
->rx_rssi_min
> rx
->signal
)
586 itp
->rx_rssi_min
= rx
->signal
;
587 if (itp
->rx_rssi_max
< rx
->signal
)
588 itp
->rx_rssi_max
= rx
->signal
;
593 void cw1200_itp_wake_up_tx(struct cw1200_common
*priv
)
595 wake_up(&priv
->debug
->itp
.write_wait
);
598 bool cw1200_itp_tx_running(struct cw1200_common
*priv
)
600 if (atomic_read(&priv
->debug
->itp
.awaiting_confirm
) ||
601 atomic_read(&priv
->debug
->itp
.test_mode
) ==
603 atomic_sub(1, &priv
->debug
->itp
.awaiting_confirm
);
609 static void cw1200_itp_handle(struct cw1200_common
*priv
,
612 struct cw1200_itp
*itp
= &priv
->debug
->itp
;
613 const struct wiphy
*wiphy
= priv
->hw
->wiphy
;
617 pr_debug("[ITP] <<< %s", skb
->data
);
618 if (sscanf(skb
->data
, "%d", &cmd
) != 1) {
619 cw1200_itp_err(priv
, -EINVAL
, 1);
624 case 1: /* RX test */
625 if (atomic_read(&itp
->test_mode
)) {
626 cw1200_itp_err(priv
, -EBUSY
, 0);
629 ret
= sscanf(skb
->data
, "%d,%d,%d",
630 &cmd
, &itp
->band
, &itp
->ch
);
632 cw1200_itp_err(priv
, -EINVAL
, ret
+ 1);
635 if (itp
->band
>= 2) {
636 cw1200_itp_err(priv
, -EINVAL
, 2);
637 } else if (!wiphy
->bands
[itp
->band
]) {
638 cw1200_itp_err(priv
, -EINVAL
, 2);
639 } else if (itp
->ch
>= wiphy
->bands
[itp
->band
]->n_channels
) {
640 cw1200_itp_err(priv
, -EINVAL
, 3);
642 cw1200_itp_rx_stats(priv
);
643 cw1200_itp_rx_start(priv
);
646 case 2: /* RX stat */
647 cw1200_itp_rx_stats(priv
);
649 case 3: /* RX/TX stop */
650 if (atomic_read(&itp
->test_mode
) == TEST_MODE_RX_TEST
) {
651 cw1200_itp_rx_stats(priv
);
652 cw1200_itp_rx_stop(priv
);
653 } else if (atomic_read(&itp
->test_mode
) == TEST_MODE_TX_TEST
) {
654 cw1200_itp_tx_stop(priv
);
656 cw1200_itp_err(priv
, -EBUSY
, 0);
659 case 4: /* TX start */
660 if (atomic_read(&itp
->test_mode
) != TEST_MODE_NO_TEST
) {
661 cw1200_itp_err(priv
, -EBUSY
, 0);
664 ret
= sscanf(skb
->data
, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",
665 &cmd
, &itp
->band
, &itp
->ch
, &itp
->rate
,
666 &itp
->preamble
, &itp
->number
, &itp
->data_mode
,
667 &itp
->interval_us
, &itp
->power
, &itp
->data_len
);
669 cw1200_itp_err(priv
, -EINVAL
, ret
+ 1);
672 if (itp
->band
>= 2) {
673 cw1200_itp_err(priv
, -EINVAL
, 2);
674 } else if (!wiphy
->bands
[itp
->band
]) {
675 cw1200_itp_err(priv
, -EINVAL
, 2);
676 } else if (itp
->ch
>= wiphy
->bands
[itp
->band
]->n_channels
) {
677 cw1200_itp_err(priv
, -EINVAL
, 3);
678 } else if (itp
->rate
>= 20) {
679 cw1200_itp_err(priv
, -EINVAL
, 4);
680 } else if (itp
->preamble
>= ITP_PREAMBLE_MAX
) {
681 cw1200_itp_err(priv
, -EINVAL
, 5);
682 } else if (itp
->data_mode
>= ITP_DATA_MAX_MODE
) {
683 cw1200_itp_err(priv
, -EINVAL
, 7);
684 } else if (itp
->data_len
< ITP_MIN_DATA_SIZE
||
685 itp
->data_len
> (priv
->wsm_caps
.input_buffer_size
- itp
->hdr_len
)) {
686 cw1200_itp_err(priv
, -EINVAL
, 8);
688 cw1200_itp_tx_start(priv
);
692 cw1200_itp_get_version(priv
, ITP_CHIP_ID
);
695 cw1200_itp_get_version(priv
, ITP_FW_VER
);
700 static void cw1200_itp_err(struct cw1200_common
*priv
,
703 struct cw1200_itp
*itp
= &priv
->debug
->itp
;
705 static char buf
[255];
708 len
= snprintf(buf
, sizeof(buf
), "%d,%d\n",
713 skb
= dev_alloc_skb(len
);
720 memcpy(skb
->data
, buf
, len
);
721 skb_queue_tail(&itp
->log_queue
, skb
);
722 wake_up(&itp
->read_wait
);
724 len
= sprint_symbol(buf
,
725 (unsigned long)__builtin_return_address(0));
728 pr_debug("[ITP] error %d,%d from %s\n",