debugfs.c 26.9 KB
Newer Older
1 2 3 4 5
#include <linux/module.h>
#include <linux/dcache.h>
#include <linux/debugfs.h>
#include <linux/delay.h>
#include <linux/mm.h>
6
#include <linux/string.h>
7
#include <net/iw_handler.h>
8

9 10 11
#include "dev.h"
#include "decl.h"
#include "host.h"
12
#include "debugfs.h"
13

14
static struct dentry *lbs_dir;
15 16 17 18 19
static char *szStates[] = {
	"Connected",
	"Disconnected"
};

20
#ifdef PROC_DEBUG
21
static void lbs_debug_init(struct lbs_private *priv, struct net_device *dev);
22
#endif
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

static int open_file_generic(struct inode *inode, struct file *file)
{
	file->private_data = inode->i_private;
	return 0;
}

static ssize_t write_file_dummy(struct file *file, const char __user *buf,
                                size_t count, loff_t *ppos)
{
        return -EINVAL;
}

static const size_t len = PAGE_SIZE;

38
static ssize_t lbs_dev_info(struct file *file, char __user *userbuf,
39 40
				  size_t count, loff_t *ppos)
{
41
	struct lbs_private *priv = file->private_data;
42 43 44 45 46 47
	size_t pos = 0;
	unsigned long addr = get_zeroed_page(GFP_KERNEL);
	char *buf = (char *)addr;
	ssize_t res;

	pos += snprintf(buf+pos, len-pos, "state = %s\n",
48
				szStates[priv->connect_status]);
49
	pos += snprintf(buf+pos, len-pos, "region_code = %02x\n",
50
				(u32) priv->regioncode);
51 52 53 54 55 56 57 58

	res = simple_read_from_buffer(userbuf, count, ppos, buf, pos);

	free_page(addr);
	return res;
}


59
static ssize_t lbs_getscantable(struct file *file, char __user *userbuf,
60 61
				  size_t count, loff_t *ppos)
{
62
	struct lbs_private *priv = file->private_data;
63 64 65 66
	size_t pos = 0;
	int numscansdone = 0, res;
	unsigned long addr = get_zeroed_page(GFP_KERNEL);
	char *buf = (char *)addr;
67
	DECLARE_MAC_BUF(mac);
68
	struct bss_descriptor * iter_bss;
69 70

	pos += snprintf(buf+pos, len-pos,
71
		"# | ch  | rssi |       bssid       |   cap    | Qual | SSID \n");
72

73 74
	mutex_lock(&priv->lock);
	list_for_each_entry (iter_bss, &priv->network_list, list) {
75 76 77
		u16 ibss = (iter_bss->capability & WLAN_CAPABILITY_IBSS);
		u16 privacy = (iter_bss->capability & WLAN_CAPABILITY_PRIVACY);
		u16 spectrum_mgmt = (iter_bss->capability & WLAN_CAPABILITY_SPECTRUM_MGMT);
78 79

		pos += snprintf(buf+pos, len-pos,
80
			"%02u| %03d | %04ld | %s |",
81
			numscansdone, iter_bss->channel, iter_bss->rssi,
82
			print_mac(mac, iter_bss->bssid));
83
		pos += snprintf(buf+pos, len-pos, " %04x-", iter_bss->capability);
84
		pos += snprintf(buf+pos, len-pos, "%c%c%c |",
85 86
				ibss ? 'A' : 'I', privacy ? 'P' : ' ',
				spectrum_mgmt ? 'S' : ' ');
87
		pos += snprintf(buf+pos, len-pos, " %04d |", SCAN_RSSI(iter_bss->rssi));
88 89
		pos += snprintf(buf+pos, len-pos, " %s\n",
		                escape_essid(iter_bss->ssid, iter_bss->ssid_len));
90 91 92

		numscansdone++;
	}
93
	mutex_unlock(&priv->lock);
94 95 96 97 98 99 100

	res = simple_read_from_buffer(userbuf, count, ppos, buf, pos);

	free_page(addr);
	return res;
}

101
static ssize_t lbs_sleepparams_write(struct file *file,
102 103 104
				const char __user *user_buf, size_t count,
				loff_t *ppos)
{
105
	struct lbs_private *priv = file->private_data;
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
	ssize_t buf_size, res;
	int p1, p2, p3, p4, p5, p6;
	unsigned long addr = get_zeroed_page(GFP_KERNEL);
	char *buf = (char *)addr;

	buf_size = min(count, len - 1);
	if (copy_from_user(buf, user_buf, buf_size)) {
		res = -EFAULT;
		goto out_unlock;
	}
	res = sscanf(buf, "%d %d %d %d %d %d", &p1, &p2, &p3, &p4, &p5, &p6);
	if (res != 6) {
		res = -EFAULT;
		goto out_unlock;
	}
121 122 123 124 125 126
	priv->sp.sp_error = p1;
	priv->sp.sp_offset = p2;
	priv->sp.sp_stabletime = p3;
	priv->sp.sp_calcontrol = p4;
	priv->sp.sp_extsleepclk = p5;
	priv->sp.sp_reserved = p6;
127

128
	res = lbs_prepare_and_send_command(priv,
129 130 131
				CMD_802_11_SLEEP_PARAMS,
				CMD_ACT_SET,
				CMD_OPTION_WAITFORRSP, 0, NULL);
132 133 134 135 136 137 138 139 140 141 142

	if (!res)
		res = count;
	else
		res = -EINVAL;

out_unlock:
	free_page(addr);
	return res;
}

143
static ssize_t lbs_sleepparams_read(struct file *file, char __user *userbuf,
144 145
				  size_t count, loff_t *ppos)
{
146
	struct lbs_private *priv = file->private_data;
147 148 149 150 151
	ssize_t res;
	size_t pos = 0;
	unsigned long addr = get_zeroed_page(GFP_KERNEL);
	char *buf = (char *)addr;

152
	res = lbs_prepare_and_send_command(priv,
153 154 155
				CMD_802_11_SLEEP_PARAMS,
				CMD_ACT_GET,
				CMD_OPTION_WAITFORRSP, 0, NULL);
156 157 158 159 160
	if (res) {
		res = -EFAULT;
		goto out_unlock;
	}

161 162 163 164
	pos += snprintf(buf, len, "%d %d %d %d %d %d\n", priv->sp.sp_error,
			priv->sp.sp_offset, priv->sp.sp_stabletime,
			priv->sp.sp_calcontrol, priv->sp.sp_extsleepclk,
			priv->sp.sp_reserved);
165 166 167 168 169 170 171 172

	res = simple_read_from_buffer(userbuf, count, ppos, buf, pos);

out_unlock:
	free_page(addr);
	return res;
}

173
static ssize_t lbs_extscan(struct file *file, const char __user *userbuf,
174 175
				  size_t count, loff_t *ppos)
{
176
	struct lbs_private *priv = file->private_data;
177 178 179 180 181 182 183 184 185 186 187
	ssize_t res, buf_size;
	union iwreq_data wrqu;
	unsigned long addr = get_zeroed_page(GFP_KERNEL);
	char *buf = (char *)addr;

	buf_size = min(count, len - 1);
	if (copy_from_user(buf, userbuf, buf_size)) {
		res = -EFAULT;
		goto out_unlock;
	}

188
	lbs_send_specific_ssid_scan(priv, buf, strlen(buf)-1, 0);
189 190

	memset(&wrqu, 0, sizeof(union iwreq_data));
191
	wireless_send_event(priv->dev, SIOCGIWSCAN, &wrqu, NULL);
192 193 194 195 196 197

out_unlock:
	free_page(addr);
	return count;
}

198 199
static void lbs_parse_bssid(char *buf, size_t count,
	struct lbs_ioctl_user_scan_cfg *scan_cfg)
200 201 202 203 204 205 206 207
{
	char *hold;
	unsigned int mac[ETH_ALEN];

	hold = strstr(buf, "bssid=");
	if (!hold)
		return;
	hold += 6;
208 209
	sscanf(hold, "%02x:%02x:%02x:%02x:%02x:%02x",
	       mac, mac+1, mac+2, mac+3, mac+4, mac+5);
210
	memcpy(scan_cfg->bssid, mac, ETH_ALEN);
211 212
}

213 214
static void lbs_parse_ssid(char *buf, size_t count,
	struct lbs_ioctl_user_scan_cfg *scan_cfg)
215 216 217 218 219 220 221 222
{
	char *hold, *end;
	ssize_t size;

	hold = strstr(buf, "ssid=");
	if (!hold)
		return;
	hold += 5;
223
	end = strchr(hold, ' ');
224 225 226
	if (!end)
		end = buf + count - 1;

D
Dan Williams 已提交
227
	size = min((size_t)IW_ESSID_MAX_SIZE, (size_t) (end - hold));
228
	strncpy(scan_cfg->ssid, hold, size);
229 230 231 232

	return;
}

233
static int lbs_parse_clear(char *buf, size_t count, const char *tag)
234 235 236 237
{
	char *hold;
	int val;

238
	hold = strstr(buf, tag);
239
	if (!hold)
240 241
		return 0;
	hold += strlen(tag);
242 243 244 245 246
	sscanf(hold, "%d", &val);

	if (val != 0)
		val = 1;

247
	return val;
248 249
}

250 251
static int lbs_parse_dur(char *buf, size_t count,
	struct lbs_ioctl_user_scan_cfg *scan_cfg)
252 253 254 255 256 257 258 259 260 261 262 263 264
{
	char *hold;
	int val;

	hold = strstr(buf, "dur=");
	if (!hold)
		return 0;
	hold += 4;
	sscanf(hold, "%d", &val);

	return val;
}

265 266
static void lbs_parse_type(char *buf, size_t count,
	struct lbs_ioctl_user_scan_cfg *scan_cfg)
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
{
	char *hold;
	int val;

	hold = strstr(buf, "type=");
	if (!hold)
		return;
	hold += 5;
	sscanf(hold, "%d", &val);

	/* type=1,2 or 3 */
	if (val < 1 || val > 3)
		return;

	scan_cfg->bsstype = val;

	return;
}

286
static ssize_t lbs_setuserscan(struct file *file,
287 288 289
				    const char __user *userbuf,
				    size_t count, loff_t *ppos)
{
290
	struct lbs_private *priv = file->private_data;
291
	ssize_t res, buf_size;
292
	struct lbs_ioctl_user_scan_cfg *scan_cfg;
293 294
	union iwreq_data wrqu;
	int dur;
295
	char *buf = (char *)get_zeroed_page(GFP_KERNEL);
296

297
	if (!buf)
298
		return -ENOMEM;
299
		
300 301 302
	buf_size = min(count, len - 1);
	if (copy_from_user(buf, userbuf, buf_size)) {
		res = -EFAULT;
303
		goto out_buf;
304 305
	}

306 307 308 309 310 311 312
	scan_cfg = kzalloc(sizeof(struct lbs_ioctl_user_scan_cfg), GFP_KERNEL);
	if (!scan_cfg) {
		res = -ENOMEM;
		goto out_buf;
	}
	res = count;

313
	scan_cfg->bsstype = LBS_SCAN_BSS_TYPE_ANY;
314

315 316 317 318 319 320
	dur = lbs_parse_dur(buf, count, scan_cfg);
	lbs_parse_bssid(buf, count, scan_cfg);
	scan_cfg->clear_bssid = lbs_parse_clear(buf, count, "clear_bssid=");
	lbs_parse_ssid(buf, count, scan_cfg);
	scan_cfg->clear_ssid = lbs_parse_clear(buf, count, "clear_ssid=");
	lbs_parse_type(buf, count, scan_cfg);
321

322
	lbs_scan_networks(priv, scan_cfg, 1);
323
	wait_event_interruptible(priv->cmd_pending,
324
				 priv->surpriseremoved || !priv->last_scanned_channel);
325

326
	if (priv->surpriseremoved)
327
		goto out_scan_cfg;
328 329

	memset(&wrqu, 0x00, sizeof(union iwreq_data));
330
	wireless_send_event(priv->dev, SIOCGIWSCAN, &wrqu, NULL);
331

332
 out_scan_cfg:
333
	kfree(scan_cfg);
334 335 336
 out_buf:
	free_page((unsigned long)buf);
	return res;
337 338 339
}


340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
/*
 * When calling CMD_802_11_SUBSCRIBE_EVENT with CMD_ACT_GET, me might
 * get a bunch of vendor-specific TLVs (a.k.a. IEs) back from the
 * firmware. Here's an example:
 *	04 01 02 00 00 00 05 01 02 00 00 00 06 01 02 00
 *	00 00 07 01 02 00 3c 00 00 00 00 00 00 00 03 03
 *	00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
 *
 * The 04 01 is the TLV type (here TLV_TYPE_RSSI_LOW), 02 00 is the length,
 * 00 00 are the data bytes of this TLV. For this TLV, their meaning is
 * defined in mrvlietypes_thresholds
 *
 * This function searches in this TLV data chunk for a given TLV type
 * and returns a pointer to the first data byte of the TLV, or to NULL
 * if the TLV hasn't been found.
 */
static void *lbs_tlv_find(u16 tlv_type, const u8 *tlv, u16 size)
357
{
358
	__le16 le_type = cpu_to_le16(tlv_type);
359
	ssize_t pos = 0;
360 361 362 363 364 365 366 367 368 369 370 371 372 373
	struct mrvlietypesheader *tlv_h;
	while (pos < size) {
		u16 length;
		tlv_h = (struct mrvlietypesheader *) tlv;
		if (tlv_h->type == le_type)
			return tlv_h;
		if (tlv_h->len == 0)
			return NULL;
		length = le16_to_cpu(tlv_h->len) +
			sizeof(struct mrvlietypesheader);
		pos += length;
		tlv += length;
	}
	return NULL;
374 375
}

376 377 378 379 380

/*
 * This just gets the bitmap of currently subscribed events. Used when
 * adding an additonal event subscription.
 */
381
static u16 lbs_get_events_bitmap(struct lbs_private *priv)
382
{
383
	ssize_t res;
384

385 386 387
	struct cmd_ds_802_11_subscribe_event *events = kzalloc(
		sizeof(struct cmd_ds_802_11_subscribe_event),
		GFP_KERNEL);
388

389 390 391
	res = lbs_prepare_and_send_command(priv,
			CMD_802_11_SUBSCRIBE_EVENT, CMD_ACT_GET,
			CMD_OPTION_WAITFORRSP, 0, events);
392

393 394
	if (res) {
		kfree(events);
395 396
		return 0;
	}
397
	return le16_to_cpu(events->events);
398 399 400
}


401 402 403 404
static ssize_t lbs_threshold_read(
	u16 tlv_type, u16 event_mask,
	struct file *file, char __user *userbuf,
	size_t count, loff_t *ppos)
405
{
406
	struct lbs_private *priv = file->private_data;
407 408
	ssize_t res = 0;
	size_t pos = 0;
409 410
	unsigned long addr = get_zeroed_page(GFP_KERNEL);
	char *buf = (char *)addr;
411 412
	u8 value;
	u8 freq;
413

414 415 416 417
	struct cmd_ds_802_11_subscribe_event *events = kzalloc(
		sizeof(struct cmd_ds_802_11_subscribe_event),
		GFP_KERNEL);
	struct mrvlietypes_thresholds *got;
418

419 420 421 422 423 424
	res = lbs_prepare_and_send_command(priv,
			CMD_802_11_SUBSCRIBE_EVENT, CMD_ACT_GET,
			CMD_OPTION_WAITFORRSP, 0, events);
	if (res) {
		kfree(events);
		return res;
425 426
	}

427 428 429 430
	got = lbs_tlv_find(tlv_type, events->tlv, sizeof(events->tlv));
	if (got) {
		value = got->value;
		freq  = got->freq;
431
	}
432
	kfree(events);
433

434 435 436
	if (got)
		pos += snprintf(buf, len, "%d %d %d\n", value, freq,
			!!(le16_to_cpu(events->events) & event_mask));
437 438 439 440 441 442 443 444

	res = simple_read_from_buffer(userbuf, count, ppos, buf, pos);

	free_page(addr);
	return res;
}


445 446 447 448 449
static ssize_t lbs_threshold_write(
	u16 tlv_type, u16 event_mask,
	struct file *file,
	const char __user *userbuf,
	size_t count, loff_t *ppos)
450
{
451
	struct lbs_private *priv = file->private_data;
452
	ssize_t res, buf_size;
453
	int value, freq, curr_mask, new_mask;
454 455
	unsigned long addr = get_zeroed_page(GFP_KERNEL);
	char *buf = (char *)addr;
456
	struct cmd_ds_802_11_subscribe_event *events;
457 458 459 460 461 462

	buf_size = min(count, len - 1);
	if (copy_from_user(buf, userbuf, buf_size)) {
		res = -EFAULT;
		goto out_unlock;
	}
463
	res = sscanf(buf, "%d %d %d", &value, &freq, &new_mask);
464 465 466 467
	if (res != 3) {
		res = -EFAULT;
		goto out_unlock;
	}
468
	curr_mask = lbs_get_events_bitmap(priv);
469

470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
	if (new_mask)
		new_mask = curr_mask | event_mask;
	else
		new_mask = curr_mask & ~event_mask;

	/* Now everything is set and we can send stuff down to the firmware */
	events = kzalloc(
		sizeof(struct cmd_ds_802_11_subscribe_event),
		GFP_KERNEL);
	if (events) {
		struct mrvlietypes_thresholds *tlv =
			(struct mrvlietypes_thresholds *) events->tlv;
		events->action = cpu_to_le16(CMD_ACT_SET);
		events->events = cpu_to_le16(new_mask);
		tlv->header.type = cpu_to_le16(tlv_type);
		tlv->header.len = cpu_to_le16(
			sizeof(struct mrvlietypes_thresholds) -
			sizeof(struct mrvlietypesheader));
		tlv->value = value;
		if (tlv_type != TLV_TYPE_BCNMISS)
			tlv->freq = freq;
		lbs_prepare_and_send_command(priv,
			CMD_802_11_SUBSCRIBE_EVENT, CMD_ACT_SET,
			CMD_OPTION_WAITFORRSP, 0, events);
		kfree(events);
495 496 497 498 499 500 501 502 503
	}

	res = count;
out_unlock:
	free_page(addr);
	return res;
}


504 505 506
static ssize_t lbs_lowrssi_read(
	struct file *file, char __user *userbuf,
	size_t count, loff_t *ppos)
507
{
508 509
	return lbs_threshold_read(TLV_TYPE_RSSI_LOW, CMD_SUBSCRIBE_RSSI_LOW,
		file, userbuf, count, ppos);
510 511 512
}


513 514 515 516 517 518 519
static ssize_t lbs_lowrssi_write(
	struct file *file, const char __user *userbuf,
	size_t count, loff_t *ppos)
{
	return lbs_threshold_write(TLV_TYPE_RSSI_LOW, CMD_SUBSCRIBE_RSSI_LOW,
		file, userbuf, count, ppos);
}
520 521


522 523 524 525 526 527 528
static ssize_t lbs_lowsnr_read(
	struct file *file, char __user *userbuf,
	size_t count, loff_t *ppos)
{
	return lbs_threshold_read(TLV_TYPE_SNR_LOW, CMD_SUBSCRIBE_SNR_LOW,
		file, userbuf, count, ppos);
}
529 530


531 532 533 534 535 536 537
static ssize_t lbs_lowsnr_write(
	struct file *file, const char __user *userbuf,
	size_t count, loff_t *ppos)
{
	return lbs_threshold_write(TLV_TYPE_SNR_LOW, CMD_SUBSCRIBE_SNR_LOW,
		file, userbuf, count, ppos);
}
538 539


540 541 542 543 544 545 546
static ssize_t lbs_failcount_read(
	struct file *file, char __user *userbuf,
	size_t count, loff_t *ppos)
{
	return lbs_threshold_read(TLV_TYPE_FAILCOUNT, CMD_SUBSCRIBE_FAILCOUNT,
		file, userbuf, count, ppos);
}
547 548


549 550 551 552 553 554
static ssize_t lbs_failcount_write(
	struct file *file, const char __user *userbuf,
	size_t count, loff_t *ppos)
{
	return lbs_threshold_write(TLV_TYPE_FAILCOUNT, CMD_SUBSCRIBE_FAILCOUNT,
		file, userbuf, count, ppos);
555 556
}

557 558 559 560

static ssize_t lbs_highrssi_read(
	struct file *file, char __user *userbuf,
	size_t count, loff_t *ppos)
561
{
562 563 564
	return lbs_threshold_read(TLV_TYPE_RSSI_HIGH, CMD_SUBSCRIBE_RSSI_HIGH,
		file, userbuf, count, ppos);
}
565 566


567 568 569 570 571 572 573
static ssize_t lbs_highrssi_write(
	struct file *file, const char __user *userbuf,
	size_t count, loff_t *ppos)
{
	return lbs_threshold_write(TLV_TYPE_RSSI_HIGH, CMD_SUBSCRIBE_RSSI_HIGH,
		file, userbuf, count, ppos);
}
574 575


576 577 578 579 580 581 582
static ssize_t lbs_highsnr_read(
	struct file *file, char __user *userbuf,
	size_t count, loff_t *ppos)
{
	return lbs_threshold_read(TLV_TYPE_SNR_HIGH, CMD_SUBSCRIBE_SNR_HIGH,
		file, userbuf, count, ppos);
}
583 584


585 586 587 588 589 590
static ssize_t lbs_highsnr_write(
	struct file *file, const char __user *userbuf,
	size_t count, loff_t *ppos)
{
	return lbs_threshold_write(TLV_TYPE_SNR_HIGH, CMD_SUBSCRIBE_SNR_HIGH,
		file, userbuf, count, ppos);
591 592
}

593 594 595
static ssize_t lbs_bcnmiss_read(
	struct file *file, char __user *userbuf,
	size_t count, loff_t *ppos)
596
{
597 598 599
	return lbs_threshold_read(TLV_TYPE_BCNMISS, CMD_SUBSCRIBE_BCNMISS,
		file, userbuf, count, ppos);
}
600 601


602 603 604 605 606 607
static ssize_t lbs_bcnmiss_write(
	struct file *file, const char __user *userbuf,
	size_t count, loff_t *ppos)
{
	return lbs_threshold_write(TLV_TYPE_BCNMISS, CMD_SUBSCRIBE_BCNMISS,
		file, userbuf, count, ppos);
608 609 610 611 612 613 614 615 616
}








617
static ssize_t lbs_rdmac_read(struct file *file, char __user *userbuf,
618 619
				  size_t count, loff_t *ppos)
{
620
	struct lbs_private *priv = file->private_data;
621
	struct lbs_offset_value offval;
622 623 624 625 626 627 628 629
	ssize_t pos = 0;
	int ret;
	unsigned long addr = get_zeroed_page(GFP_KERNEL);
	char *buf = (char *)addr;

	offval.offset = priv->mac_offset;
	offval.value = 0;

630
	ret = lbs_prepare_and_send_command(priv,
631 632
				CMD_MAC_REG_ACCESS, 0,
				CMD_OPTION_WAITFORRSP, 0, &offval);
633 634
	mdelay(10);
	pos += snprintf(buf+pos, len-pos, "MAC[0x%x] = 0x%08x\n",
635
				priv->mac_offset, priv->offsetvalue.value);
636 637 638 639 640 641

	ret = simple_read_from_buffer(userbuf, count, ppos, buf, pos);
	free_page(addr);
	return ret;
}

642
static ssize_t lbs_rdmac_write(struct file *file,
643 644 645
				    const char __user *userbuf,
				    size_t count, loff_t *ppos)
{
646
	struct lbs_private *priv = file->private_data;
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
	ssize_t res, buf_size;
	unsigned long addr = get_zeroed_page(GFP_KERNEL);
	char *buf = (char *)addr;

	buf_size = min(count, len - 1);
	if (copy_from_user(buf, userbuf, buf_size)) {
		res = -EFAULT;
		goto out_unlock;
	}
	priv->mac_offset = simple_strtoul((char *)buf, NULL, 16);
	res = count;
out_unlock:
	free_page(addr);
	return res;
}

663
static ssize_t lbs_wrmac_write(struct file *file,
664 665 666 667
				    const char __user *userbuf,
				    size_t count, loff_t *ppos)
{

668
	struct lbs_private *priv = file->private_data;
669 670
	ssize_t res, buf_size;
	u32 offset, value;
671
	struct lbs_offset_value offval;
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
	unsigned long addr = get_zeroed_page(GFP_KERNEL);
	char *buf = (char *)addr;

	buf_size = min(count, len - 1);
	if (copy_from_user(buf, userbuf, buf_size)) {
		res = -EFAULT;
		goto out_unlock;
	}
	res = sscanf(buf, "%x %x", &offset, &value);
	if (res != 2) {
		res = -EFAULT;
		goto out_unlock;
	}

	offval.offset = offset;
	offval.value = value;
688
	res = lbs_prepare_and_send_command(priv,
689 690
				CMD_MAC_REG_ACCESS, 1,
				CMD_OPTION_WAITFORRSP, 0, &offval);
691 692 693 694 695 696 697 698
	mdelay(10);

	res = count;
out_unlock:
	free_page(addr);
	return res;
}

699
static ssize_t lbs_rdbbp_read(struct file *file, char __user *userbuf,
700 701
				  size_t count, loff_t *ppos)
{
702
	struct lbs_private *priv = file->private_data;
703
	struct lbs_offset_value offval;
704 705 706 707 708 709 710 711
	ssize_t pos = 0;
	int ret;
	unsigned long addr = get_zeroed_page(GFP_KERNEL);
	char *buf = (char *)addr;

	offval.offset = priv->bbp_offset;
	offval.value = 0;

712
	ret = lbs_prepare_and_send_command(priv,
713 714
				CMD_BBP_REG_ACCESS, 0,
				CMD_OPTION_WAITFORRSP, 0, &offval);
715 716
	mdelay(10);
	pos += snprintf(buf+pos, len-pos, "BBP[0x%x] = 0x%08x\n",
717
				priv->bbp_offset, priv->offsetvalue.value);
718 719 720 721 722 723 724

	ret = simple_read_from_buffer(userbuf, count, ppos, buf, pos);
	free_page(addr);

	return ret;
}

725
static ssize_t lbs_rdbbp_write(struct file *file,
726 727 728
				    const char __user *userbuf,
				    size_t count, loff_t *ppos)
{
729
	struct lbs_private *priv = file->private_data;
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745
	ssize_t res, buf_size;
	unsigned long addr = get_zeroed_page(GFP_KERNEL);
	char *buf = (char *)addr;

	buf_size = min(count, len - 1);
	if (copy_from_user(buf, userbuf, buf_size)) {
		res = -EFAULT;
		goto out_unlock;
	}
	priv->bbp_offset = simple_strtoul((char *)buf, NULL, 16);
	res = count;
out_unlock:
	free_page(addr);
	return res;
}

746
static ssize_t lbs_wrbbp_write(struct file *file,
747 748 749 750
				    const char __user *userbuf,
				    size_t count, loff_t *ppos)
{

751
	struct lbs_private *priv = file->private_data;
752 753
	ssize_t res, buf_size;
	u32 offset, value;
754
	struct lbs_offset_value offval;
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
	unsigned long addr = get_zeroed_page(GFP_KERNEL);
	char *buf = (char *)addr;

	buf_size = min(count, len - 1);
	if (copy_from_user(buf, userbuf, buf_size)) {
		res = -EFAULT;
		goto out_unlock;
	}
	res = sscanf(buf, "%x %x", &offset, &value);
	if (res != 2) {
		res = -EFAULT;
		goto out_unlock;
	}

	offval.offset = offset;
	offval.value = value;
771
	res = lbs_prepare_and_send_command(priv,
772 773
				CMD_BBP_REG_ACCESS, 1,
				CMD_OPTION_WAITFORRSP, 0, &offval);
774 775 776 777 778 779 780 781
	mdelay(10);

	res = count;
out_unlock:
	free_page(addr);
	return res;
}

782
static ssize_t lbs_rdrf_read(struct file *file, char __user *userbuf,
783 784
				  size_t count, loff_t *ppos)
{
785
	struct lbs_private *priv = file->private_data;
786
	struct lbs_offset_value offval;
787 788 789 790 791 792 793 794
	ssize_t pos = 0;
	int ret;
	unsigned long addr = get_zeroed_page(GFP_KERNEL);
	char *buf = (char *)addr;

	offval.offset = priv->rf_offset;
	offval.value = 0;

795
	ret = lbs_prepare_and_send_command(priv,
796 797
				CMD_RF_REG_ACCESS, 0,
				CMD_OPTION_WAITFORRSP, 0, &offval);
798 799
	mdelay(10);
	pos += snprintf(buf+pos, len-pos, "RF[0x%x] = 0x%08x\n",
800
				priv->rf_offset, priv->offsetvalue.value);
801 802 803 804 805 806 807

	ret = simple_read_from_buffer(userbuf, count, ppos, buf, pos);
	free_page(addr);

	return ret;
}

808
static ssize_t lbs_rdrf_write(struct file *file,
809 810 811
				    const char __user *userbuf,
				    size_t count, loff_t *ppos)
{
812
	struct lbs_private *priv = file->private_data;
813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
	ssize_t res, buf_size;
	unsigned long addr = get_zeroed_page(GFP_KERNEL);
	char *buf = (char *)addr;

	buf_size = min(count, len - 1);
	if (copy_from_user(buf, userbuf, buf_size)) {
		res = -EFAULT;
		goto out_unlock;
	}
	priv->rf_offset = simple_strtoul((char *)buf, NULL, 16);
	res = count;
out_unlock:
	free_page(addr);
	return res;
}

829
static ssize_t lbs_wrrf_write(struct file *file,
830 831 832 833
				    const char __user *userbuf,
				    size_t count, loff_t *ppos)
{

834
	struct lbs_private *priv = file->private_data;
835 836
	ssize_t res, buf_size;
	u32 offset, value;
837
	struct lbs_offset_value offval;
838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853
	unsigned long addr = get_zeroed_page(GFP_KERNEL);
	char *buf = (char *)addr;

	buf_size = min(count, len - 1);
	if (copy_from_user(buf, userbuf, buf_size)) {
		res = -EFAULT;
		goto out_unlock;
	}
	res = sscanf(buf, "%x %x", &offset, &value);
	if (res != 2) {
		res = -EFAULT;
		goto out_unlock;
	}

	offval.offset = offset;
	offval.value = value;
854
	res = lbs_prepare_and_send_command(priv,
855 856
				CMD_RF_REG_ACCESS, 1,
				CMD_OPTION_WAITFORRSP, 0, &offval);
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871
	mdelay(10);

	res = count;
out_unlock:
	free_page(addr);
	return res;
}

#define FOPS(fread, fwrite) { \
	.owner = THIS_MODULE, \
	.open = open_file_generic, \
	.read = (fread), \
	.write = (fwrite), \
}

872
struct lbs_debugfs_files {
873 874 875 876 877
	char *name;
	int perm;
	struct file_operations fops;
};

878 879 880
static struct lbs_debugfs_files debugfs_files[] = {
	{ "info", 0444, FOPS(lbs_dev_info, write_file_dummy), },
	{ "getscantable", 0444, FOPS(lbs_getscantable,
881
					write_file_dummy), },
882 883 884 885
	{ "sleepparams", 0644, FOPS(lbs_sleepparams_read,
				lbs_sleepparams_write), },
	{ "extscan", 0600, FOPS(NULL, lbs_extscan), },
	{ "setuserscan", 0600, FOPS(NULL, lbs_setuserscan), },
886 887
};

888 889 890 891 892 893 894 895 896 897 898 899 900
static struct lbs_debugfs_files debugfs_events_files[] = {
	{"low_rssi", 0644, FOPS(lbs_lowrssi_read,
				lbs_lowrssi_write), },
	{"low_snr", 0644, FOPS(lbs_lowsnr_read,
				lbs_lowsnr_write), },
	{"failure_count", 0644, FOPS(lbs_failcount_read,
				lbs_failcount_write), },
	{"beacon_missed", 0644, FOPS(lbs_bcnmiss_read,
				lbs_bcnmiss_write), },
	{"high_rssi", 0644, FOPS(lbs_highrssi_read,
				lbs_highrssi_write), },
	{"high_snr", 0644, FOPS(lbs_highsnr_read,
				lbs_highsnr_write), },
901 902
};

903 904 905 906 907 908 909
static struct lbs_debugfs_files debugfs_regs_files[] = {
	{"rdmac", 0644, FOPS(lbs_rdmac_read, lbs_rdmac_write), },
	{"wrmac", 0600, FOPS(NULL, lbs_wrmac_write), },
	{"rdbbp", 0644, FOPS(lbs_rdbbp_read, lbs_rdbbp_write), },
	{"wrbbp", 0600, FOPS(NULL, lbs_wrbbp_write), },
	{"rdrf", 0644, FOPS(lbs_rdrf_read, lbs_rdrf_write), },
	{"wrrf", 0600, FOPS(NULL, lbs_wrrf_write), },
910 911
};

912
void lbs_debugfs_init(void)
913
{
914 915
	if (!lbs_dir)
		lbs_dir = debugfs_create_dir("lbs_wireless", NULL);
916 917 918 919

	return;
}

920
void lbs_debugfs_remove(void)
921
{
922 923
	if (lbs_dir)
		 debugfs_remove(lbs_dir);
924 925 926
	return;
}

927
void lbs_debugfs_init_one(struct lbs_private *priv, struct net_device *dev)
928 929
{
	int i;
930 931
	struct lbs_debugfs_files *files;
	if (!lbs_dir)
932 933
		goto exit;

934
	priv->debugfs_dir = debugfs_create_dir(dev->name, lbs_dir);
935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973
	if (!priv->debugfs_dir)
		goto exit;

	for (i=0; i<ARRAY_SIZE(debugfs_files); i++) {
		files = &debugfs_files[i];
		priv->debugfs_files[i] = debugfs_create_file(files->name,
							     files->perm,
							     priv->debugfs_dir,
							     priv,
							     &files->fops);
	}

	priv->events_dir = debugfs_create_dir("subscribed_events", priv->debugfs_dir);
	if (!priv->events_dir)
		goto exit;

	for (i=0; i<ARRAY_SIZE(debugfs_events_files); i++) {
		files = &debugfs_events_files[i];
		priv->debugfs_events_files[i] = debugfs_create_file(files->name,
							     files->perm,
							     priv->events_dir,
							     priv,
							     &files->fops);
	}

	priv->regs_dir = debugfs_create_dir("registers", priv->debugfs_dir);
	if (!priv->regs_dir)
		goto exit;

	for (i=0; i<ARRAY_SIZE(debugfs_regs_files); i++) {
		files = &debugfs_regs_files[i];
		priv->debugfs_regs_files[i] = debugfs_create_file(files->name,
							     files->perm,
							     priv->regs_dir,
							     priv,
							     &files->fops);
	}

#ifdef PROC_DEBUG
974
	lbs_debug_init(priv, dev);
975 976 977 978 979
#endif
exit:
	return;
}

980
void lbs_debugfs_remove_one(struct lbs_private *priv)
981 982 983 984 985 986 987 988
{
	int i;

	for(i=0; i<ARRAY_SIZE(debugfs_regs_files); i++)
		debugfs_remove(priv->debugfs_regs_files[i]);

	debugfs_remove(priv->regs_dir);

989
	for(i=0; i<ARRAY_SIZE(debugfs_events_files); i++)
990 991 992 993 994 995 996 997
		debugfs_remove(priv->debugfs_events_files[i]);

	debugfs_remove(priv->events_dir);
#ifdef PROC_DEBUG
	debugfs_remove(priv->debugfs_debug);
#endif
	for(i=0; i<ARRAY_SIZE(debugfs_files); i++)
		debugfs_remove(priv->debugfs_files[i]);
998
	debugfs_remove(priv->debugfs_dir);
999 1000
}

1001 1002


1003 1004
/* debug entry */

1005 1006
#ifdef PROC_DEBUG

1007 1008
#define item_size(n)	(FIELD_SIZEOF(struct lbs_private, n))
#define item_addr(n)	(offsetof(struct lbs_private, n))
1009

1010

1011 1012 1013
struct debug_data {
	char name[32];
	u32 size;
D
Dan Williams 已提交
1014
	size_t addr;
1015 1016
};

1017
/* To debug any member of struct lbs_private, simply add one line here.
1018 1019 1020 1021 1022 1023 1024
 */
static struct debug_data items[] = {
	{"intcounter", item_size(intcounter), item_addr(intcounter)},
	{"psmode", item_size(psmode), item_addr(psmode)},
	{"psstate", item_size(psstate), item_addr(psstate)},
};

1025
static int num_of_items = ARRAY_SIZE(items);
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037

/**
 *  @brief proc read function
 *
 *  @param page	   pointer to buffer
 *  @param s       read data starting position
 *  @param off     offset
 *  @param cnt     counter
 *  @param eof     end of file flag
 *  @param data    data to output
 *  @return 	   number of output data
 */
1038
static ssize_t lbs_debugfs_read(struct file *file, char __user *userbuf,
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
			size_t count, loff_t *ppos)
{
	int val = 0;
	size_t pos = 0;
	ssize_t res;
	char *p;
	int i;
	struct debug_data *d;
	unsigned long addr = get_zeroed_page(GFP_KERNEL);
	char *buf = (char *)addr;

	p = buf;

	d = (struct debug_data *)file->private_data;

	for (i = 0; i < num_of_items; i++) {
		if (d[i].size == 1)
			val = *((u8 *) d[i].addr);
		else if (d[i].size == 2)
			val = *((u16 *) d[i].addr);
		else if (d[i].size == 4)
			val = *((u32 *) d[i].addr);
D
Dan Williams 已提交
1061 1062
		else if (d[i].size == 8)
			val = *((u64 *) d[i].addr);
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081

		pos += sprintf(p + pos, "%s=%d\n", d[i].name, val);
	}

	res = simple_read_from_buffer(userbuf, count, ppos, p, pos);

	free_page(addr);
	return res;
}

/**
 *  @brief proc write function
 *
 *  @param f	   file pointer
 *  @param buf     pointer to data buffer
 *  @param cnt     data number to write
 *  @param data    data to write
 *  @return 	   number of data
 */
1082
static ssize_t lbs_debugfs_write(struct file *f, const char __user *buf,
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092
			    size_t cnt, loff_t *ppos)
{
	int r, i;
	char *pdata;
	char *p;
	char *p0;
	char *p1;
	char *p2;
	struct debug_data *d = (struct debug_data *)f->private_data;

1093
	pdata = kmalloc(cnt, GFP_KERNEL);
1094 1095 1096 1097
	if (pdata == NULL)
		return 0;

	if (copy_from_user(pdata, buf, cnt)) {
1098
		lbs_deb_debugfs("Copy from user failed\n");
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
		kfree(pdata);
		return 0;
	}

	p0 = pdata;
	for (i = 0; i < num_of_items; i++) {
		do {
			p = strstr(p0, d[i].name);
			if (p == NULL)
				break;
			p1 = strchr(p, '\n');
			if (p1 == NULL)
				break;
			p0 = p1++;
			p2 = strchr(p, '=');
			if (!p2)
				break;
			p2++;
1117
			r = simple_strtoul(p2, NULL, 0);
1118 1119 1120 1121 1122 1123
			if (d[i].size == 1)
				*((u8 *) d[i].addr) = (u8) r;
			else if (d[i].size == 2)
				*((u16 *) d[i].addr) = (u16) r;
			else if (d[i].size == 4)
				*((u32 *) d[i].addr) = (u32) r;
D
Dan Williams 已提交
1124 1125
			else if (d[i].size == 8)
				*((u64 *) d[i].addr) = (u64) r;
1126 1127 1128 1129 1130
			break;
		} while (1);
	}
	kfree(pdata);

D
Dan Williams 已提交
1131
	return (ssize_t)cnt;
1132 1133
}

1134
static struct file_operations lbs_debug_fops = {
1135 1136
	.owner = THIS_MODULE,
	.open = open_file_generic,
1137 1138
	.write = lbs_debugfs_write,
	.read = lbs_debugfs_read,
1139 1140 1141 1142 1143
};

/**
 *  @brief create debug proc file
 *
1144
 *  @param priv	   pointer struct lbs_private
1145 1146 1147
 *  @param dev     pointer net_device
 *  @return 	   N/A
 */
1148
static void lbs_debug_init(struct lbs_private *priv, struct net_device *dev)
1149 1150 1151 1152 1153 1154 1155
{
	int i;

	if (!priv->debugfs_dir)
		return;

	for (i = 0; i < num_of_items; i++)
1156
		items[i].addr += (size_t) priv;
1157 1158 1159

	priv->debugfs_debug = debugfs_create_file("debug", 0644,
						  priv->debugfs_dir, &items[0],
1160
						  &lbs_debug_fops);
1161
}
1162
#endif