• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Test code implementation for XRadio drivers
3  *
4  * Copyright (c) 2013
5  * Xradio Technology Co., Ltd. <www.xradiotech.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11 
12 #include <linux/module.h>
13 #include <linux/debugfs.h>
14 #include <linux/poll.h>
15 #include <linux/time.h>
16 #include <linux/kallsyms.h>
17 #include <net/mac80211.h>
18 #include "xradio.h"
19 #include "itp.h"
20 #include "sta.h"
21 
22 static int __xradio_itp_open(struct xradio_common *priv);
23 static int __xradio_itp_close(struct xradio_common *priv);
24 static void xradio_itp_rx_start(struct xradio_common *priv);
25 static void xradio_itp_rx_stop(struct xradio_common *priv);
26 static void xradio_itp_rx_stats(struct xradio_common *priv);
27 static void xradio_itp_rx_reset(struct xradio_common *priv);
28 static void xradio_itp_tx_stop(struct xradio_common *priv);
29 static void xradio_itp_handle(struct xradio_common *priv,
30 			      struct sk_buff *skb);
31 static void xradio_itp_err(struct xradio_common *priv,
32 			   int err,
33 			   int arg);
34 static void __xradio_itp_tx_stop(struct xradio_common *priv);
35 
xradio_itp_read(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)36 static ssize_t xradio_itp_read(struct file *file,
37 	char __user *user_buf, size_t count, loff_t *ppos)
38 {
39 	struct xradio_common *priv = file->private_data;
40 	struct xradio_itp *itp = &priv->debug->itp;
41 	struct sk_buff *skb;
42 	int ret;
43 
44 	if (skb_queue_empty(&itp->log_queue))
45 		return 0;
46 
47 	skb = skb_dequeue(&itp->log_queue);
48 	ret = copy_to_user(user_buf, skb->data, skb->len);
49 	*ppos += skb->len;
50 	skb->data[skb->len] = 0;
51 	itp_printk(XRADIO_DBG_MSG, "[ITP] >>> %s", skb->data);
52 	consume_skb(skb);
53 
54 	return skb->len - ret;
55 }
56 
xradio_itp_write(struct file * file,const char __user * user_buf,size_t count,loff_t * ppos)57 static ssize_t xradio_itp_write(struct file *file,
58 	const char __user *user_buf, size_t count, loff_t *ppos)
59 {
60 	struct xradio_common *priv = file->private_data;
61 	struct sk_buff *skb;
62 
63 	if (!count || count > 1024)
64 		return -EINVAL;
65 	skb = xr_alloc_skb(count + 1);
66 	if (!skb)
67 		return -ENOMEM;
68 	skb_trim(skb, 0);
69 	skb_put(skb, count + 1);
70 	if (copy_from_user(skb->data, user_buf, count)) {
71 		kfree_skb(skb);
72 		return -EFAULT;
73 	}
74 	skb->data[count] = 0;
75 
76 	xradio_itp_handle(priv, skb);
77 	consume_skb(skb);
78 	return count;
79 }
80 
xradio_itp_poll(struct file * file,poll_table * wait)81 static unsigned int xradio_itp_poll(struct file *file, poll_table *wait)
82 {
83 	struct xradio_common *priv = file->private_data;
84 	struct xradio_itp *itp = &priv->debug->itp;
85 	unsigned int mask = 0;
86 
87 	poll_wait(file, &itp->read_wait, wait);
88 
89 	if (!skb_queue_empty(&itp->log_queue))
90 		mask |= POLLIN | POLLRDNORM;
91 
92 	mask |= POLLOUT | POLLWRNORM;
93 
94 	return mask;
95 }
96 
xradio_itp_open(struct inode * inode,struct file * file)97 static int xradio_itp_open(struct inode *inode, struct file *file)
98 {
99 	struct xradio_common *priv = inode->i_private;
100 	struct xradio_itp *itp = &priv->debug->itp;
101 	int ret = 0;
102 
103 	file->private_data = priv;
104 	if (atomic_inc_return(&itp->open_count) == 1) {
105 		ret = __xradio_itp_open(priv);
106 		if (ret && !atomic_dec_return(&itp->open_count))
107 			__xradio_itp_close(priv);
108 	} else {
109 		atomic_dec(&itp->open_count);
110 		ret = -EBUSY;
111 	}
112 
113 	return ret;
114 }
115 
xradio_itp_close(struct inode * inode,struct file * file)116 static int xradio_itp_close(struct inode *inode, struct file *file)
117 {
118 	struct xradio_common *priv = file->private_data;
119 	struct xradio_itp *itp = &priv->debug->itp;
120 	if (!atomic_dec_return(&itp->open_count)) {
121 		__xradio_itp_close(priv);
122 		wake_up(&itp->close_wait);
123 	}
124 	return 0;
125 }
126 
127 static const struct file_operations fops_itp = {
128 	.open = xradio_itp_open,
129 	.read = xradio_itp_read,
130 	.write = xradio_itp_write,
131 	.poll = xradio_itp_poll,
132 	.release = xradio_itp_close,
133 	.llseek = default_llseek,
134 	.owner = THIS_MODULE,
135 };
136 
xradio_itp_fill_pattern(u8 * data,int size,enum xradio_itp_data_modes mode)137 static void xradio_itp_fill_pattern(u8 *data, int size,
138 		enum xradio_itp_data_modes mode)
139 {
140 	u8 *p = data;
141 
142 	if (size <= 0)
143 		return;
144 
145 	switch (mode) {
146 	default:
147 	case ITP_DATA_ZEROS:
148 		memset(data, 0x0, size);
149 		break;
150 	case ITP_DATA_ONES:
151 		memset(data, 0xff, size);
152 		break;
153 	case ITP_DATA_ZERONES:
154 		memset(data, 0x55, size);
155 		break;
156 	case ITP_DATA_RANDOM:
157 		while (p < data+size-sizeof(u32)) {
158 			(*(u32 *)p) = random32();
159 			p += sizeof(u32);
160 		}
161 		while (p < data+size) {
162 			(*p) = random32() & 0xFF;
163 			p++;
164 		}
165 		break;
166 	}
167 	return;
168 }
169 
xradio_itp_tx_work(struct work_struct * work)170 static void xradio_itp_tx_work(struct work_struct *work)
171 {
172 	struct xradio_itp *itp = container_of(work, struct xradio_itp,
173 		    tx_work.work);
174 	struct xradio_common *priv = itp->priv;
175 	atomic_set(&priv->bh_tx, 1);
176 
177 #ifdef BH_USE_SEMAPHORE
178 	up(&priv->bh_sem);
179 #else
180 	wake_up(&priv->bh_wq);
181 #endif
182 }
183 
xradio_itp_tx_finish(struct work_struct * work)184 static void xradio_itp_tx_finish(struct work_struct *work)
185 {
186 	struct xradio_itp *itp = container_of(work, struct xradio_itp,
187 		    tx_finish.work);
188 	__xradio_itp_tx_stop(itp->priv);
189 }
190 
xradio_itp_init(struct xradio_common * priv)191 int xradio_itp_init(struct xradio_common *priv)
192 {
193 	struct xradio_itp *itp = &priv->debug->itp;
194 
195 	itp->priv = priv;
196 	atomic_set(&itp->open_count, 0);
197 	atomic_set(&itp->stop_tx, 0);
198 	atomic_set(&itp->awaiting_confirm, 0);
199 	skb_queue_head_init(&itp->log_queue);
200 	spin_lock_init(&itp->tx_lock);
201 	init_waitqueue_head(&itp->read_wait);
202 	init_waitqueue_head(&itp->write_wait);
203 	init_waitqueue_head(&itp->close_wait);
204 	INIT_DELAYED_WORK(&itp->tx_work, xradio_itp_tx_work);
205 	INIT_DELAYED_WORK(&itp->tx_finish, xradio_itp_tx_finish);
206 	itp->data = NULL;
207 	itp->hdr_len = WSM_TX_EXTRA_HEADROOM +
208 			sizeof(struct ieee80211_hdr_3addr);
209 	itp->id = 0;
210 
211 	if (!debugfs_create_file("itp", S_IRUSR | S_IWUSR,
212 			priv->debug->debugfs_phy, priv, &fops_itp))
213 		return -ENOMEM;
214 
215 	return 0;
216 }
217 
xradio_itp_release(struct xradio_common * priv)218 void xradio_itp_release(struct xradio_common *priv)
219 {
220 	struct xradio_itp *itp = &priv->debug->itp;
221 
222 	wait_event_interruptible(itp->close_wait,
223 			!atomic_read(&itp->open_count));
224 
225 	SYS_WARN(atomic_read(&itp->open_count));
226 
227 	skb_queue_purge(&itp->log_queue);
228 	xradio_itp_tx_stop(priv);
229 }
230 
__xradio_itp_open(struct xradio_common * priv)231 static int __xradio_itp_open(struct xradio_common *priv)
232 {
233 	struct xradio_itp *itp = &priv->debug->itp;
234 
235 	if (!priv->vif)
236 		return -EINVAL;
237 	if (priv->join_status)
238 		return -EINVAL;
239 	itp->saved_channel = priv->channel;
240 	if (!priv->channel)
241 		priv->channel = &priv->hw->
242 			wiphy->bands[NL80211_BAND_2GHZ]->channels[0];
243 	wsm_set_bssid_filtering(priv, false);
244 	xradio_itp_rx_reset(priv);
245 	return 0;
246 }
247 
__xradio_itp_close(struct xradio_common * priv)248 static int __xradio_itp_close(struct xradio_common *priv)
249 {
250 	struct xradio_itp *itp = &priv->debug->itp;
251 	if (atomic_read(&itp->test_mode) == TEST_MODE_RX_TEST)
252 		xradio_itp_rx_stop(priv);
253 	xradio_itp_tx_stop(priv);
254 	xradio_disable_listening(priv);
255 	xradio_update_filtering(priv);
256 	priv->channel = itp->saved_channel;
257 	return 0;
258 }
259 
xradio_is_itp(struct xradio_common * priv)260 bool xradio_is_itp(struct xradio_common *priv)
261 {
262 	struct xradio_itp *itp = &priv->debug->itp;
263 	return atomic_read(&itp->open_count) != 0;
264 }
265 
xradio_itp_rx_reset(struct xradio_common * priv)266 static void xradio_itp_rx_reset(struct xradio_common *priv)
267 {
268 	struct xradio_itp *itp = &priv->debug->itp;
269 	itp->rx_cnt = 0;
270 	itp->rx_rssi = 0;
271 	itp->rx_rssi_max = -1000;
272 	itp->rx_rssi_min = 1000;
273 }
274 
xradio_itp_rx_start(struct xradio_common * priv)275 static void xradio_itp_rx_start(struct xradio_common *priv)
276 {
277 	struct xradio_itp *itp = &priv->debug->itp;
278 
279 	itp_printk(XRADIO_DBG_MSG, "[ITP] RX start, band = %d, ch = %d\n",
280 			itp->band, itp->ch);
281 	atomic_set(&itp->test_mode, TEST_MODE_RX_TEST);
282 	xradio_disable_listening(priv, false);
283 	priv->channel = &priv->hw->
284 		wiphy->bands[itp->band]->channels[itp->ch];
285 	xradio_enable_listening(priv, priv->channel);
286 	wsm_set_bssid_filtering(priv, false);
287 }
288 
xradio_itp_rx_stop(struct xradio_common * priv)289 static void xradio_itp_rx_stop(struct xradio_common *priv)
290 {
291 	struct xradio_itp *itp = &priv->debug->itp;
292 	itp_printk(XRADIO_DBG_MSG, "[ITP] RX stop\n");
293 	atomic_set(&itp->test_mode, TEST_MODE_NO_TEST);
294 	xradio_itp_rx_reset(priv);
295 }
296 
xradio_itp_rx_stats(struct xradio_common * priv)297 static void xradio_itp_rx_stats(struct xradio_common *priv)
298 {
299 	struct xradio_itp *itp = &priv->debug->itp;
300 	struct sk_buff *skb;
301 	char buf[128];
302 	int len, ret;
303 	struct wsm_counters_table counters;
304 
305 	ret = wsm_get_counters_table(priv, &counters);
306 
307 	if (ret)
308 		xradio_itp_err(priv, -EBUSY, 20);
309 
310 	if (!itp->rx_cnt)
311 		len = snprintf(buf, sizeof(buf), "1, 0, 0, 0, 0, %d\n",
312 				counters.countRxPacketErrors);
313 	else
314 		len = snprintf(buf, sizeof(buf), "1, %d, %ld, %d, %d, %d\n",
315 			itp->rx_cnt,
316 			itp->rx_cnt ? itp->rx_rssi / itp->rx_cnt : 0,
317 			itp->rx_rssi_min, itp->rx_rssi_max,
318 			counters.countRxPacketErrors);
319 
320 	if (len <= 0) {
321 		xradio_itp_err(priv, -EBUSY, 21);
322 		return;
323 	}
324 
325 	skb = xr_alloc_skb(len);
326 	if (!skb) {
327 		xradio_itp_err(priv, -ENOMEM, 22);
328 		return;
329 	}
330 
331 	itp->rx_cnt = 0;
332 	itp->rx_rssi = 0;
333 	itp->rx_rssi_max = -1000;
334 	itp->rx_rssi_min = 1000;
335 
336 	skb_trim(skb, 0);
337 	skb_put(skb, len);
338 
339 	memcpy(skb->data, buf, len);
340 	skb_queue_tail(&itp->log_queue, skb);
341 	wake_up(&itp->read_wait);
342 }
343 
xradio_itp_tx_start(struct xradio_common * priv)344 static void xradio_itp_tx_start(struct xradio_common *priv)
345 {
346 	struct wsm_tx *tx;
347 	struct ieee80211_hdr_3addr *hdr;
348 	struct xradio_itp *itp = &priv->debug->itp;
349 	struct wsm_association_mode assoc_mode = {
350 		.flags = WSM_ASSOCIATION_MODE_USE_PREAMBLE_TYPE,
351 		.preambleType = itp->preamble,
352 	};
353 	int len;
354 	u8 da_addr[6] = ITP_DEFAULT_DA_ADDR;
355 
356 	/* Rates index 4 and 5 are not supported */
357 	if (itp->rate > 3)
358 		itp->rate += 2;
359 
360 	itp_printk(XRADIO_DBG_MSG,
361 		"[ITP] TX start: band = %d, ch = %d, rate = %d, "
362 			" preamble = %d, number = %d, data_mode = %d, "
363 			" interval = %d, power = %d, data_len = %d\n",
364 			itp->band, itp->ch, itp->rate, itp->preamble,
365 			itp->number, itp->data_mode, itp->interval_us,
366 			itp->power, itp->data_len);
367 
368 	len = itp->hdr_len + itp->data_len;
369 
370 	itp->data = xr_kmalloc(len, false);
371 	tx = (struct wsm_tx *)itp->data;
372 	tx->hdr.len = itp->data_len + itp->hdr_len;
373 	tx->hdr.id = __cpu_to_le16(0x0004 | 1 << 6);
374 	tx->maxTxRate = itp->rate;
375 	tx->queueId = 3;
376 	tx->more = 0;
377 	tx->flags = 0xc;
378 	tx->packetID = 0;
379 	tx->reserved = 0;
380 	tx->expireTime = 0;
381 
382 	if (itp->preamble == ITP_PREAMBLE_GREENFIELD)
383 		tx->htTxParameters = WSM_HT_TX_GREENFIELD;
384 	else if (itp->preamble == ITP_PREAMBLE_MIXED)
385 		tx->htTxParameters = WSM_HT_TX_MIXED;
386 
387 	hdr = (struct ieee80211_hdr_3addr *)&itp->data[sizeof(struct wsm_tx)];
388 	memset(hdr, 0, sizeof(*hdr));
389 	hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
390 			IEEE80211_FCTL_TODS);
391 	memcpy(hdr->addr1, da_addr, ETH_ALEN);
392 	memcpy(hdr->addr2, priv->vif->addr, ETH_ALEN);
393 	memcpy(hdr->addr3, da_addr, ETH_ALEN);
394 
395 	xradio_itp_fill_pattern(&itp->data[itp->hdr_len],
396 			itp->data_len, itp->data_mode);
397 
398 	xradio_disable_listening(priv);
399 	priv->channel = &priv->hw->
400 		wiphy->bands[itp->band]->channels[itp->ch];
401 	SYS_WARN(wsm_set_output_power(priv, itp->power));
402 	if (itp->preamble == ITP_PREAMBLE_SHORT ||
403 			itp->preamble == ITP_PREAMBLE_LONG)
404 		SYS_WARN(wsm_set_association_mode(priv,
405 					&assoc_mode));
406 	wsm_set_bssid_filtering(priv, false);
407 	xradio_enable_listening(priv, priv->channel);
408 
409 	spin_lock_bh(&itp->tx_lock);
410 	atomic_set(&itp->test_mode, TEST_MODE_TX_TEST);
411 	atomic_set(&itp->awaiting_confirm, 0);
412 	atomic_set(&itp->stop_tx, 0);
413 	atomic_set(&priv->bh_tx, 1);
414 	ktime_get_ts(&itp->last_sent);
415 #ifdef BH_USE_SEMAPHORE
416 	up(&priv->bh_sem);
417 #else
418 	wake_up(&priv->bh_wq);
419 #endif
420 	spin_unlock_bh(&itp->tx_lock);
421 }
422 
__xradio_itp_tx_stop(struct xradio_common * priv)423 void __xradio_itp_tx_stop(struct xradio_common *priv)
424 {
425 	struct xradio_itp *itp = &priv->debug->itp;
426 	spin_lock_bh(&itp->tx_lock);
427 	kfree(itp->data);
428 	itp->data = NULL;
429 	atomic_set(&itp->test_mode, TEST_MODE_NO_TEST);
430 	spin_unlock_bh(&itp->tx_lock);
431 }
432 
xradio_itp_tx_stop(struct xradio_common * priv)433 static void xradio_itp_tx_stop(struct xradio_common *priv)
434 {
435 	struct xradio_itp *itp = &priv->debug->itp;
436 	itp_printk(XRADIO_DBG_MSG, "[ITP] TX stop\n");
437 	atomic_set(&itp->stop_tx, 1);
438 	flush_workqueue(priv->workqueue);
439 	flush_workqueue(priv->spare_workqueue);
440 
441 	/* time for FW to confirm all tx requests */
442 	msleep(500);
443 
444 	__xradio_itp_tx_stop(priv);
445 }
446 
xradio_itp_get_version(struct xradio_common * priv,enum xradio_itp_version_type type)447 static void xradio_itp_get_version(struct xradio_common *priv,
448 		enum xradio_itp_version_type type)
449 {
450 	struct xradio_itp *itp = &priv->debug->itp;
451 	struct sk_buff *skb;
452 	char buf[ITP_BUF_SIZE];
453 	size_t size = 0;
454 	int len;
455 	itp_printk(XRADIO_DBG_MSG, "[ITP] print %s version\n",
456 		   type == ITP_CHIP_ID ? "chip" : "firmware");
457 
458 	len = snprintf(buf, ITP_BUF_SIZE, "2, ");
459 	if (len <= 0) {
460 		xradio_itp_err(priv, -EINVAL, 40);
461 		return;
462 	}
463 	size += len;
464 
465 	switch (type) {
466 	case ITP_CHIP_ID:
467 		len = xradio_print_fw_version(priv, buf+size,
468 				ITP_BUF_SIZE - size);
469 
470 		if (len <= 0) {
471 			xradio_itp_err(priv, -EINVAL, 41);
472 			return;
473 		}
474 		size += len;
475 		break;
476 	case ITP_FW_VER:
477 		len = snprintf(buf+size, ITP_BUF_SIZE - size,
478 				"%d.%d", priv->wsm_caps.hardwareId,
479 				priv->wsm_caps.hardwareSubId);
480 		if (len <= 0) {
481 			xradio_itp_err(priv, -EINVAL, 42);
482 			return;
483 		}
484 		size += len;
485 		break;
486 	default:
487 		xradio_itp_err(priv, -EINVAL, 43);
488 		break;
489 	}
490 
491 	len = snprintf(buf+size, ITP_BUF_SIZE-size, "\n");
492 	if (len <= 0) {
493 		xradio_itp_err(priv, -EINVAL, 44);
494 		return;
495 	}
496 	size += len;
497 
498 	skb = xr_alloc_skb(size);
499 	if (!skb) {
500 		xradio_itp_err(priv, -ENOMEM, 45);
501 		return;
502 	}
503 
504 	skb_trim(skb, 0);
505 	skb_put(skb, size);
506 
507 	memcpy(skb->data, buf, size);
508 	skb_queue_tail(&itp->log_queue, skb);
509 	wake_up(&itp->read_wait);
510 }
511 
xradio_itp_get_tx(struct xradio_common * priv,u8 ** data,size_t * tx_len,int * burst)512 int xradio_itp_get_tx(struct xradio_common *priv, u8 **data,
513 		size_t *tx_len, int *burst)
514 {
515 	struct xradio_itp *itp;
516 	struct wsm_tx *tx;
517 	struct timespec64 now;
518 	int time_left_us;
519 
520 	if (!priv->debug)
521 		return 0;
522 
523 	itp	= &priv->debug->itp;
524 
525 	if (!itp)
526 		return 0;
527 
528 	spin_lock_bh(&itp->tx_lock);
529 	if (atomic_read(&itp->test_mode) != TEST_MODE_TX_TEST)
530 		goto out;
531 
532 	if (atomic_read(&itp->stop_tx))
533 		goto out;
534 
535 	if (itp->number == 0) {
536 		atomic_set(&itp->stop_tx, 1);
537 		queue_delayed_work(priv->workqueue, &itp->tx_finish,
538 				HZ/10);
539 		goto out;
540 	}
541 
542 	if (!itp->data)
543 		goto out;
544 
545 	if (priv->hw_bufs_used >= 2) {
546 		if (!atomic_read(&priv->bh_rx))
547 				atomic_set(&priv->bh_rx, 1);
548 		atomic_set(&priv->bh_tx, 1);
549 		goto out;
550 	}
551 
552 	ktime_get_ts(&now);
553 	time_left_us = (itp->last_sent.tv_sec -
554 			now.tv_sec)*1000000 +
555 		(itp->last_sent.tv_nsec - now.tv_nsec)/1000
556 		+ itp->interval_us;
557 
558 	if (time_left_us > ITP_TIME_THRES_US) {
559 		queue_delayed_work(priv->workqueue, &itp->tx_work,
560 				ITP_US_TO_MS(time_left_us)*HZ/1000);
561 		goto out;
562 	}
563 
564 	if (time_left_us > 50)
565 		udelay(time_left_us);
566 
567 	if (itp->number > 0)
568 		itp->number--;
569 
570 	*data = itp->data;
571 	*tx_len = itp->data_len + itp->hdr_len;
572 
573 	if (itp->data_mode == ITP_DATA_RANDOM)
574 		xradio_itp_fill_pattern(&itp->data[itp->hdr_len],
575 				itp->data_len, itp->data_mode);
576 
577 	tx = (struct wsm_tx *)itp->data;
578 	tx->packetID = __cpu_to_le32(itp->id++);
579 	*burst = 2;
580 	atomic_set(&priv->bh_tx, 1);
581 	ktime_get_ts(&itp->last_sent);
582 	atomic_add(1, &itp->awaiting_confirm);
583 	spin_unlock_bh(&itp->tx_lock);
584 	return 1;
585 
586 out:
587 	spin_unlock_bh(&itp->tx_lock);
588 	return 0;
589 }
590 
xradio_itp_rxed(struct xradio_common * priv,struct sk_buff * skb)591 bool xradio_itp_rxed(struct xradio_common *priv, struct sk_buff *skb)
592 {
593 	struct xradio_itp *itp = &priv->debug->itp;
594 	struct ieee80211_rx_status *rx = IEEE80211_SKB_RXCB(skb);
595 	int signal;
596 
597 	if (atomic_read(&itp->test_mode) != TEST_MODE_RX_TEST)
598 		return xradio_is_itp(priv);
599 	if (rx->freq != priv->channel->center_freq)
600 		return true;
601 
602 	signal = rx->signal;
603 	itp->rx_cnt++;
604 	itp->rx_rssi += signal;
605 	if (itp->rx_rssi_min > rx->signal)
606 		itp->rx_rssi_min = rx->signal;
607 	if (itp->rx_rssi_max < rx->signal)
608 		itp->rx_rssi_max = rx->signal;
609 
610 	return true;
611 }
612 
xradio_itp_wake_up_tx(struct xradio_common * priv)613 void xradio_itp_wake_up_tx(struct xradio_common *priv)
614 {
615 	wake_up(&priv->debug->itp.write_wait);
616 }
617 
xradio_itp_tx_running(struct xradio_common * priv)618 bool xradio_itp_tx_running(struct xradio_common *priv)
619 {
620 	if (atomic_read(&priv->debug->itp.awaiting_confirm) ||
621 			atomic_read(&priv->debug->itp.test_mode) ==
622 			TEST_MODE_TX_TEST) {
623 		atomic_sub(1, &priv->debug->itp.awaiting_confirm);
624 		return true;
625 	}
626 	return false;
627 }
628 
xradio_itp_handle(struct xradio_common * priv,struct sk_buff * skb)629 static void xradio_itp_handle(struct xradio_common *priv,
630 			      struct sk_buff *skb)
631 {
632 	struct xradio_itp *itp = &priv->debug->itp;
633 	const struct wiphy *wiphy = priv->hw->wiphy;
634 	int cmd;
635 	int ret;
636 
637 	itp_printk(XRADIO_DBG_MSG, "[ITP] <<< %s", skb->data);
638 	if (sscanf(skb->data, "%d", &cmd) != 1) {
639 		xradio_itp_err(priv, -EINVAL, 1);
640 		return;
641 	}
642 
643 	switch (cmd) {
644 	case 1: /* RX test */
645 		if (atomic_read(&itp->test_mode)) {
646 			xradio_itp_err(priv, -EBUSY, 0);
647 			return;
648 		}
649 		ret = sscanf(skb->data, "%d, %u, %u",
650 				&cmd, &itp->band, &itp->ch);
651 		if (ret != 3) {
652 			xradio_itp_err(priv, -EINVAL, ret + 1);
653 			return;
654 		}
655 		if (itp->band >= 2)
656 			xradio_itp_err(priv, -EINVAL, 2);
657 		else if (!wiphy->bands[itp->band])
658 			xradio_itp_err(priv, -EINVAL, 2);
659 		else if (itp->ch >=
660 				wiphy->bands[itp->band]->n_channels)
661 			xradio_itp_err(priv, -EINVAL, 3);
662 		else {
663 			xradio_itp_rx_stats(priv);
664 			xradio_itp_rx_start(priv);
665 		}
666 		break;
667 	case 2: /* RX stat */
668 		xradio_itp_rx_stats(priv);
669 		break;
670 	case 3: /* RX/TX stop */
671 		if (atomic_read(&itp->test_mode) == TEST_MODE_RX_TEST) {
672 			xradio_itp_rx_stats(priv);
673 			xradio_itp_rx_stop(priv);
674 		} else if (atomic_read(&itp->test_mode) == TEST_MODE_TX_TEST) {
675 			xradio_itp_tx_stop(priv);
676 		} else
677 			xradio_itp_err(priv, -EBUSY, 0);
678 		break;
679 	case 4: /* TX start */
680 		if (atomic_read(&itp->test_mode) != TEST_MODE_NO_TEST) {
681 			xradio_itp_err(priv, -EBUSY, 0);
682 			return;
683 		}
684 		ret = sscanf(skb->data, "%d, %u, %u, %u, %u, %u, %u, %d, %d, %d",
685 				&cmd, &itp->band, &itp->ch, &itp->rate,
686 				&itp->preamble, &itp->number, &itp->data_mode,
687 				&itp->interval_us, &itp->power, &itp->data_len);
688 		if (ret != 10) {
689 			xradio_itp_err(priv, -EINVAL, ret + 1);
690 			return;
691 		}
692 		if (itp->band >= 2)
693 			xradio_itp_err(priv, -EINVAL, 2);
694 		else if (!wiphy->bands[itp->band])
695 			xradio_itp_err(priv, -EINVAL, 2);
696 		else if (itp->ch >=
697 				wiphy->bands[itp->band]->n_channels)
698 			xradio_itp_err(priv, -EINVAL, 3);
699 		else if (itp->rate >= 20)
700 			xradio_itp_err(priv, -EINVAL, 4);
701 		else if (itp->preamble >= ITP_PREAMBLE_MAX)
702 			xradio_itp_err(priv, -EINVAL, 5);
703 		else if (itp->data_mode >= ITP_DATA_MAX_MODE)
704 			xradio_itp_err(priv, -EINVAL, 7);
705 		else if (itp->data_len < ITP_MIN_DATA_SIZE ||
706 				itp->data_len > priv->wsm_caps.sizeInpChBuf -
707 				itp->hdr_len)
708 			xradio_itp_err(priv, -EINVAL, 8);
709 		else {
710 		    xradio_itp_tx_start(priv);
711 		}
712 		break;
713 	case 5:
714 		xradio_itp_get_version(priv, ITP_CHIP_ID);
715 		break;
716 	case 6:
717 		xradio_itp_get_version(priv, ITP_FW_VER);
718 		break;
719 
720 	}
721 }
722 
xradio_itp_err(struct xradio_common * priv,int err,int arg)723 static void xradio_itp_err(struct xradio_common *priv,
724 			   int err, int arg)
725 {
726 	struct xradio_itp *itp = &priv->debug->itp;
727 	struct sk_buff *skb;
728 	static char buf[255];
729 	int len;
730 
731 	len = snprintf(buf, sizeof(buf), "%d, %d\n",
732 		err, arg);
733 	if (len <= 0)
734 		return;
735 
736 	skb = xr_alloc_skb(len);
737 	if (!skb)
738 		return;
739 
740 	skb_trim(skb, 0);
741 	skb_put(skb, len);
742 
743 	memcpy(skb->data, buf, len);
744 	skb_queue_tail(&itp->log_queue, skb);
745 	wake_up(&itp->read_wait);
746 
747 	len = sprint_symbol(buf,
748 			(unsigned long)__builtin_return_address(0));
749 	if (len <= 0)
750 		return;
751 	itp_printk(XRADIO_DBG_MSG, "[ITP] error %d, %d from %s\n",
752 			err, arg, buf);
753 }
754