ioctl.c 27.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/**
  * This file contains ioctl functions
  */

#include <linux/ctype.h>
#include <linux/delay.h>
#include <linux/if.h>
#include <linux/if_arp.h>
#include <linux/wireless.h>

#include <net/iw_handler.h>
#include <net/ieee80211.h>

#include "host.h"
#include "radiotap.h"
#include "decl.h"
#include "defs.h"
#include "dev.h"
#include "join.h"
#include "wext.h"

#define MAX_SCAN_CELL_SIZE      (IW_EV_ADDR_LEN + \
				IW_ESSID_MAX_SIZE + \
				IW_EV_UINT_LEN + IW_EV_FREQ_LEN + \
				IW_EV_QUAL_LEN + IW_ESSID_MAX_SIZE + \
				IW_EV_PARAM_LEN + 40)	/* 40 for WPAIE */

#define WAIT_FOR_SCAN_RRESULT_MAX_TIME (10 * HZ)

30
static int wlan_set_region(wlan_private * priv, u16 region_code)
31
{
32
	int i;
33
	int ret = 0;
34

35 36 37 38 39
	for (i = 0; i < MRVDRV_MAX_REGION_CODE; i++) {
		// use the region code to search for the index
		if (region_code == libertas_region_code_to_index[i]) {
			priv->adapter->regiontableindex = (u16) i;
			priv->adapter->regioncode = region_code;
40 41 42 43
			break;
		}
	}

44 45
	// if it's unidentified region code
	if (i >= MRVDRV_MAX_REGION_CODE) {
46 47 48
		lbs_deb_ioctl("region Code not identified\n");
		ret = -1;
		goto done;
49
	}
50

51
	if (libertas_set_regiontable(priv, priv->adapter->regioncode, 0)) {
52
		ret = -EINVAL;
53
	}
54

55 56 57
done:
	lbs_deb_leave_args(LBS_DEB_IOCTL, "ret %d", ret);
	return ret;
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
}

static inline int hex2int(char c)
{
	if (c >= '0' && c <= '9')
		return (c - '0');
	if (c >= 'a' && c <= 'f')
		return (c - 'a' + 10);
	if (c >= 'A' && c <= 'F')
		return (c - 'A' + 10);
	return -1;
}

/* Convert a string representation of a MAC address ("xx:xx:xx:xx:xx:xx")
   into binary format (6 bytes).

   This function expects that each byte is represented with 2 characters
   (e.g., 11:2:11:11:11:11 is invalid)

 */
static char *eth_str2addr(char *ethstr, u8 * addr)
{
	int i, val, val2;
	char *pos = ethstr;

	/* get rid of initial blanks */
	while (*pos == ' ' || *pos == '\t')
		++pos;

	for (i = 0; i < 6; i++) {
		val = hex2int(*pos++);
		if (val < 0)
			return NULL;
		val2 = hex2int(*pos++);
		if (val2 < 0)
			return NULL;
		addr[i] = (val * 16 + val2) & 0xff;

		if (i < 5 && *pos++ != ':')
			return NULL;
	}
	return pos;
}

/* this writes xx:xx:xx:xx:xx:xx into ethstr
   (ethstr must have space for 18 chars) */
static int eth_addr2str(u8 * addr, char *ethstr)
{
	int i;
	char *pos = ethstr;

	for (i = 0; i < 6; i++) {
		sprintf(pos, "%02x", addr[i] & 0xff);
		pos += 2;
		if (i < 5)
			*pos++ = ':';
	}
	return 17;
}

/**
 *  @brief          Add an entry to the BT table
 *  @param priv     A pointer to wlan_private structure
 *  @param req      A pointer to ifreq structure
 *  @return         0 --success, otherwise fail
 */
static int wlan_bt_add_ioctl(wlan_private * priv, struct ifreq *req)
{
	struct iwreq *wrq = (struct iwreq *)req;
	char ethaddrs_str[18];
	char *pos;
	u8 ethaddr[ETH_ALEN];
130 131 132
	int ret;

	lbs_deb_enter(LBS_DEB_IOCTL);
133 134 135 136 137 138 139 140 141 142

	if (copy_from_user(ethaddrs_str, wrq->u.data.pointer,
			   sizeof(ethaddrs_str)))
		return -EFAULT;

	if ((pos = eth_str2addr(ethaddrs_str, ethaddr)) == NULL) {
		lbs_pr_info("BT_ADD: Invalid MAC address\n");
		return -EINVAL;
	}

143 144
	lbs_deb_ioctl("BT: adding %s\n", ethaddrs_str);
	ret = libertas_prepare_and_send_command(priv, cmd_bt_access,
145
				      cmd_act_bt_access_add,
146 147 148
				      cmd_option_waitforrsp, 0, ethaddr);
	lbs_deb_leave_args(LBS_DEB_IOCTL, "ret %d", ret);
	return ret;
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
}

/**
 *  @brief          Delete an entry from the BT table
 *  @param priv     A pointer to wlan_private structure
 *  @param req      A pointer to ifreq structure
 *  @return         0 --success, otherwise fail
 */
static int wlan_bt_del_ioctl(wlan_private * priv, struct ifreq *req)
{
	struct iwreq *wrq = (struct iwreq *)req;
	char ethaddrs_str[18];
	u8 ethaddr[ETH_ALEN];
	char *pos;

164 165
	lbs_deb_enter(LBS_DEB_IOCTL);

166 167 168 169 170 171 172 173 174
	if (copy_from_user(ethaddrs_str, wrq->u.data.pointer,
			   sizeof(ethaddrs_str)))
		return -EFAULT;

	if ((pos = eth_str2addr(ethaddrs_str, ethaddr)) == NULL) {
		lbs_pr_info("Invalid MAC address\n");
		return -EINVAL;
	}

175
	lbs_deb_ioctl("BT: deleting %s\n", ethaddrs_str);
176 177 178 179 180

	return (libertas_prepare_and_send_command(priv,
				      cmd_bt_access,
				      cmd_act_bt_access_del,
				      cmd_option_waitforrsp, 0, ethaddr));
181 182

	lbs_deb_leave(LBS_DEB_IOCTL);
183 184 185 186 187 188 189 190 191 192
	return 0;
}

/**
 *  @brief          Reset all entries from the BT table
 *  @param priv     A pointer to wlan_private structure
 *  @return         0 --success, otherwise fail
 */
static int wlan_bt_reset_ioctl(wlan_private * priv)
{
193
	lbs_deb_enter(LBS_DEB_IOCTL);
194 195 196 197 198 199 200 201

	lbs_pr_alert( "BT: resetting\n");

	return (libertas_prepare_and_send_command(priv,
				      cmd_bt_access,
				      cmd_act_bt_access_reset,
				      cmd_option_waitforrsp, 0, NULL));

202
	lbs_deb_leave(LBS_DEB_IOCTL);
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
	return 0;
}

/**
 *  @brief          List an entry from the BT table
 *  @param priv     A pointer to wlan_private structure
 *  @param req      A pointer to ifreq structure
 *  @return         0 --success, otherwise fail
 */
static int wlan_bt_list_ioctl(wlan_private * priv, struct ifreq *req)
{
	int pos;
	char *addr1;
	struct iwreq *wrq = (struct iwreq *)req;
	/* used to pass id and store the bt entry returned by the FW */
	union {
		int id;
		char addr1addr2[2 * ETH_ALEN];
	} param;
	static char outstr[64];
	char *pbuf = outstr;
	int ret;

226
	lbs_deb_enter(LBS_DEB_IOCTL);
227 228

	if (copy_from_user(outstr, wrq->u.data.pointer, sizeof(outstr))) {
229
		lbs_deb_ioctl("Copy from user failed\n");
230 231 232 233 234 235 236 237 238 239 240 241 242 243
		return -1;
	}
	param.id = simple_strtoul(outstr, NULL, 10);
	pos = sprintf(pbuf, "%d: ", param.id);
	pbuf += pos;

	ret = libertas_prepare_and_send_command(priv, cmd_bt_access,
				    cmd_act_bt_access_list,
				    cmd_option_waitforrsp, 0,
				    (char *)&param);

	if (ret == 0) {
		addr1 = param.addr1addr2;

244
		pos = sprintf(pbuf, "BT includes node ");
245 246 247 248 249 250 251 252 253 254 255
		pbuf += pos;
		pos = eth_addr2str(addr1, pbuf);
		pbuf += pos;
	} else {
		sprintf(pbuf, "(null)");
		pbuf += pos;
	}

	wrq->u.data.length = strlen(outstr);
	if (copy_to_user(wrq->u.data.pointer, (char *)outstr,
			 wrq->u.data.length)) {
256
		lbs_deb_ioctl("BT_LIST: Copy to user failed!\n");
257 258 259
		return -EFAULT;
	}

260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
	lbs_deb_leave(LBS_DEB_IOCTL);
	return 0 ;
}

/**
 *  @brief          Sets inverted state of blacklist (non-zero if inverted)
 *  @param priv     A pointer to wlan_private structure
 *  @param req      A pointer to ifreq structure
 *  @return         0 --success, otherwise fail
 */
static int wlan_bt_set_invert_ioctl(wlan_private * priv, struct ifreq *req)
{
	int ret;
	struct iwreq *wrq = (struct iwreq *)req;
	union {
		int id;
		char addr1addr2[2 * ETH_ALEN];
	} param;

	lbs_deb_enter(LBS_DEB_IOCTL);

	param.id = SUBCMD_DATA(wrq) ;
	ret = libertas_prepare_and_send_command(priv, cmd_bt_access,
				    cmd_act_bt_access_set_invert,
				    cmd_option_waitforrsp, 0,
				    (char *)&param);
	if (ret != 0)
		return -EFAULT;
	lbs_deb_leave(LBS_DEB_IOCTL);
	return 0;
}

/**
 *  @brief          Gets inverted state of blacklist (non-zero if inverted)
 *  @param priv     A pointer to wlan_private structure
 *  @param req      A pointer to ifreq structure
 *  @return         0 --success, otherwise fail
 */
static int wlan_bt_get_invert_ioctl(wlan_private * priv, struct ifreq *req)
{
	int ret;
	union {
		int id;
		char addr1addr2[2 * ETH_ALEN];
	} param;

	lbs_deb_enter(LBS_DEB_IOCTL);

	ret = libertas_prepare_and_send_command(priv, cmd_bt_access,
				    cmd_act_bt_access_get_invert,
				    cmd_option_waitforrsp, 0,
				    (char *)&param);

	if (ret == 0)
		req->ifr_data = (char *)(le32_to_cpu(param.id));
	else
		return -EFAULT;

318
	lbs_deb_leave(LBS_DEB_IOCTL);
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
	return 0;
}

/**
 *  @brief          Find the next parameter in an input string
 *  @param ptr      A pointer to the input parameter string
 *  @return         A pointer to the next parameter, or 0 if no parameters left.
 */
static char * next_param(char * ptr)
{
	if (!ptr) return NULL;
	while (*ptr == ' ' || *ptr == '\t') ++ptr;
	return (*ptr == '\0') ? NULL : ptr;
}

/**
 *  @brief          Add an entry to the FWT table
 *  @param priv     A pointer to wlan_private structure
 *  @param req      A pointer to ifreq structure
 *  @return         0 --success, otherwise fail
 */
static int wlan_fwt_add_ioctl(wlan_private * priv, struct ifreq *req)
{
	struct iwreq *wrq = (struct iwreq *)req;
	char in_str[128];
	static struct cmd_ds_fwt_access fwt_access;
	char *ptr;
346 347 348
	int ret;

	lbs_deb_enter(LBS_DEB_IOCTL);
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373

	if (copy_from_user(in_str, wrq->u.data.pointer, sizeof(in_str)))
		return -EFAULT;

	if ((ptr = eth_str2addr(in_str, fwt_access.da)) == NULL) {
		lbs_pr_alert( "FWT_ADD: Invalid MAC address 1\n");
		return -EINVAL;
	}

	if ((ptr = eth_str2addr(ptr, fwt_access.ra)) == NULL) {
		lbs_pr_alert( "FWT_ADD: Invalid MAC address 2\n");
		return -EINVAL;
	}

	if ((ptr = next_param(ptr)))
		fwt_access.metric =
			cpu_to_le32(simple_strtoul(ptr, &ptr, 10));
	else
		fwt_access.metric = FWT_DEFAULT_METRIC;

	if ((ptr = next_param(ptr)))
		fwt_access.dir = (u8)simple_strtoul(ptr, &ptr, 10);
	else
		fwt_access.dir = FWT_DEFAULT_DIR;

374 375 376 377 378
	if ((ptr = next_param(ptr)))
		fwt_access.rate = (u8) simple_strtoul(ptr, &ptr, 10);
	else
		fwt_access.rate = FWT_DEFAULT_RATE;

379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
	if ((ptr = next_param(ptr)))
		fwt_access.ssn =
			cpu_to_le32(simple_strtoul(ptr, &ptr, 10));
	else
		fwt_access.ssn = FWT_DEFAULT_SSN;

	if ((ptr = next_param(ptr)))
		fwt_access.dsn =
			cpu_to_le32(simple_strtoul(ptr, &ptr, 10));
	else
		fwt_access.dsn = FWT_DEFAULT_DSN;

	if ((ptr = next_param(ptr)))
		fwt_access.hopcount = simple_strtoul(ptr, &ptr, 10);
	else
		fwt_access.hopcount = FWT_DEFAULT_HOPCOUNT;

	if ((ptr = next_param(ptr)))
		fwt_access.ttl = simple_strtoul(ptr, &ptr, 10);
	else
		fwt_access.ttl = FWT_DEFAULT_TTL;

	if ((ptr = next_param(ptr)))
		fwt_access.expiration =
			cpu_to_le32(simple_strtoul(ptr, &ptr, 10));
	else
		fwt_access.expiration = FWT_DEFAULT_EXPIRATION;

	if ((ptr = next_param(ptr)))
		fwt_access.sleepmode = (u8)simple_strtoul(ptr, &ptr, 10);
	else
		fwt_access.sleepmode = FWT_DEFAULT_SLEEPMODE;

	if ((ptr = next_param(ptr)))
		fwt_access.snr =
			cpu_to_le32(simple_strtoul(ptr, &ptr, 10));
	else
		fwt_access.snr = FWT_DEFAULT_SNR;

#ifdef DEBUG
	{
		char ethaddr1_str[18], ethaddr2_str[18];
		eth_addr2str(fwt_access.da, ethaddr1_str);
		eth_addr2str(fwt_access.ra, ethaddr2_str);
423
		lbs_deb_ioctl("FWT_ADD: adding (da:%s,%i,ra:%s)\n", ethaddr1_str,
424
		       fwt_access.dir, ethaddr2_str);
425
		lbs_deb_ioctl("FWT_ADD: ssn:%u dsn:%u met:%u hop:%u ttl:%u exp:%u slp:%u snr:%u\n",
426 427 428 429 430 431
		       fwt_access.ssn, fwt_access.dsn, fwt_access.metric,
		       fwt_access.hopcount, fwt_access.ttl, fwt_access.expiration,
		       fwt_access.sleepmode, fwt_access.snr);
	}
#endif

432 433 434 435 436 437 438
	ret = libertas_prepare_and_send_command(priv, cmd_fwt_access,
						cmd_act_fwt_access_add,
						cmd_option_waitforrsp, 0,
						(void *)&fwt_access);

	lbs_deb_leave_args(LBS_DEB_IOCTL, "ret %d", ret);
	return ret;
439 440 441 442 443 444 445 446 447 448 449 450 451 452
}

/**
 *  @brief          Delete an entry from the FWT table
 *  @param priv     A pointer to wlan_private structure
 *  @param req      A pointer to ifreq structure
 *  @return         0 --success, otherwise fail
 */
static int wlan_fwt_del_ioctl(wlan_private * priv, struct ifreq *req)
{
	struct iwreq *wrq = (struct iwreq *)req;
	char in_str[64];
	static struct cmd_ds_fwt_access fwt_access;
	char *ptr;
453 454 455
	int ret;

	lbs_deb_enter(LBS_DEB_IOCTL);
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477

	if (copy_from_user(in_str, wrq->u.data.pointer, sizeof(in_str)))
		return -EFAULT;

	if ((ptr = eth_str2addr(in_str, fwt_access.da)) == NULL) {
		lbs_pr_alert( "FWT_DEL: Invalid MAC address 1\n");
		return -EINVAL;
	}

	if ((ptr = eth_str2addr(ptr, fwt_access.ra)) == NULL) {
		lbs_pr_alert( "FWT_DEL: Invalid MAC address 2\n");
		return -EINVAL;
	}

	if ((ptr = next_param(ptr)))
		fwt_access.dir = (u8)simple_strtoul(ptr, &ptr, 10);
	else
		fwt_access.dir = FWT_DEFAULT_DIR;

#ifdef DEBUG
	{
		char ethaddr1_str[18], ethaddr2_str[18];
478
		lbs_deb_ioctl("FWT_DEL: line is %s\n", in_str);
479 480
		eth_addr2str(fwt_access.da, ethaddr1_str);
		eth_addr2str(fwt_access.ra, ethaddr2_str);
481
		lbs_deb_ioctl("FWT_DEL: removing (da:%s,ra:%s,dir:%d)\n", ethaddr1_str,
482 483 484 485
		       ethaddr2_str, fwt_access.dir);
	}
#endif

486 487 488 489 490 491 492
	ret = libertas_prepare_and_send_command(priv,
						cmd_fwt_access,
						cmd_act_fwt_access_del,
						cmd_option_waitforrsp, 0,
						(void *)&fwt_access);
	lbs_deb_leave_args(LBS_DEB_IOCTL, "ret %d", ret);
	return ret;
493 494 495 496 497 498 499 500 501 502 503 504 505 506
}


/**
 *  @brief             Print route parameters
 *  @param fwt_access  struct cmd_ds_fwt_access with route info
 *  @param buf         destination buffer for route info
 */
static void print_route(struct cmd_ds_fwt_access fwt_access, char *buf)
{
	buf += sprintf(buf, " ");
	buf += eth_addr2str(fwt_access.da, buf);
	buf += sprintf(buf, " ");
	buf += eth_addr2str(fwt_access.ra, buf);
507
	buf += sprintf(buf, " %u", fwt_access.valid);
508 509
	buf += sprintf(buf, " %u", le32_to_cpu(fwt_access.metric));
	buf += sprintf(buf, " %u", fwt_access.dir);
510
	buf += sprintf(buf, " %u", fwt_access.rate);
511 512 513 514 515 516
	buf += sprintf(buf, " %u", le32_to_cpu(fwt_access.ssn));
	buf += sprintf(buf, " %u", le32_to_cpu(fwt_access.dsn));
	buf += sprintf(buf, " %u", fwt_access.hopcount);
	buf += sprintf(buf, " %u", fwt_access.ttl);
	buf += sprintf(buf, " %u", le32_to_cpu(fwt_access.expiration));
	buf += sprintf(buf, " %u", fwt_access.sleepmode);
517 518
	buf += sprintf(buf, " %u ", le32_to_cpu(fwt_access.snr));
	buf += eth_addr2str(fwt_access.prec, buf);
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
}

/**
 *  @brief          Lookup an entry in the FWT table
 *  @param priv     A pointer to wlan_private structure
 *  @param req      A pointer to ifreq structure
 *  @return         0 --success, otherwise fail
 */
static int wlan_fwt_lookup_ioctl(wlan_private * priv, struct ifreq *req)
{
	struct iwreq *wrq = (struct iwreq *)req;
	char in_str[64];
	char *ptr;
	static struct cmd_ds_fwt_access fwt_access;
	static char out_str[128];
	int ret;

536 537
	lbs_deb_enter(LBS_DEB_IOCTL);

538 539 540 541 542 543 544 545 546 547 548
	if (copy_from_user(in_str, wrq->u.data.pointer, sizeof(in_str)))
		return -EFAULT;

	if ((ptr = eth_str2addr(in_str, fwt_access.da)) == NULL) {
		lbs_pr_alert( "FWT_LOOKUP: Invalid MAC address\n");
		return -EINVAL;
	}

#ifdef DEBUG
	{
		char ethaddr1_str[18];
549
		lbs_deb_ioctl("FWT_LOOKUP: line is %s\n", in_str);
550
		eth_addr2str(fwt_access.da, ethaddr1_str);
551
		lbs_deb_ioctl("FWT_LOOKUP: looking for (da:%s)\n", ethaddr1_str);
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
	}
#endif

	ret = libertas_prepare_and_send_command(priv,
						cmd_fwt_access,
						cmd_act_fwt_access_lookup,
						cmd_option_waitforrsp, 0,
						(void *)&fwt_access);

	if (ret == 0)
		print_route(fwt_access, out_str);
	else
		sprintf(out_str, "(null)");

	wrq->u.data.length = strlen(out_str);
	if (copy_to_user(wrq->u.data.pointer, (char *)out_str,
			 wrq->u.data.length)) {
569
		lbs_deb_ioctl("FWT_LOOKUP: Copy to user failed!\n");
570 571 572
		return -EFAULT;
	}

573
	lbs_deb_leave(LBS_DEB_IOCTL);
574 575 576 577 578 579 580 581 582 583
	return 0;
}

/**
 *  @brief          Reset all entries from the FWT table
 *  @param priv     A pointer to wlan_private structure
 *  @return         0 --success, otherwise fail
 */
static int wlan_fwt_reset_ioctl(wlan_private * priv)
{
584
	lbs_deb_ioctl("FWT: resetting\n");
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607

	return (libertas_prepare_and_send_command(priv,
				      cmd_fwt_access,
				      cmd_act_fwt_access_reset,
				      cmd_option_waitforrsp, 0, NULL));
}

/**
 *  @brief          List an entry from the FWT table
 *  @param priv     A pointer to wlan_private structure
 *  @param req      A pointer to ifreq structure
 *  @return         0 --success, otherwise fail
 */
static int wlan_fwt_list_ioctl(wlan_private * priv, struct ifreq *req)
{
	struct iwreq *wrq = (struct iwreq *)req;
	char in_str[8];
	static struct cmd_ds_fwt_access fwt_access;
	char *ptr = in_str;
	static char out_str[128];
	char *pbuf = out_str;
	int ret;

608 609
	lbs_deb_enter(LBS_DEB_IOCTL);

610 611 612 613 614 615 616
	if (copy_from_user(in_str, wrq->u.data.pointer, sizeof(in_str)))
		return -EFAULT;

	fwt_access.id = cpu_to_le32(simple_strtoul(ptr, &ptr, 10));

#ifdef DEBUG
	{
617 618
		lbs_deb_ioctl("FWT_LIST: line is %s\n", in_str);
		lbs_deb_ioctl("FWT_LIST: listing id:%i\n", le32_to_cpu(fwt_access.id));
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
	}
#endif

	ret = libertas_prepare_and_send_command(priv, cmd_fwt_access,
				    cmd_act_fwt_access_list,
				    cmd_option_waitforrsp, 0, (void *)&fwt_access);

	if (ret == 0)
		print_route(fwt_access, pbuf);
	else
		pbuf += sprintf(pbuf, " (null)");

	wrq->u.data.length = strlen(out_str);
	if (copy_to_user(wrq->u.data.pointer, (char *)out_str,
			 wrq->u.data.length)) {
634
		lbs_deb_ioctl("FWT_LIST: Copy to user failed!\n");
635 636 637
		return -EFAULT;
	}

638
	lbs_deb_leave(LBS_DEB_IOCTL);
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
	return 0;
}

/**
 *  @brief          List an entry from the FRT table
 *  @param priv     A pointer to wlan_private structure
 *  @param req      A pointer to ifreq structure
 *  @return         0 --success, otherwise fail
 */
static int wlan_fwt_list_route_ioctl(wlan_private * priv, struct ifreq *req)
{
	struct iwreq *wrq = (struct iwreq *)req;
	char in_str[64];
	static struct cmd_ds_fwt_access fwt_access;
	char *ptr = in_str;
	static char out_str[128];
	char *pbuf = out_str;
	int ret;

658 659
	lbs_deb_enter(LBS_DEB_IOCTL);

660 661 662 663 664 665 666
	if (copy_from_user(in_str, wrq->u.data.pointer, sizeof(in_str)))
		return -EFAULT;

	fwt_access.id = cpu_to_le32(simple_strtoul(ptr, &ptr, 10));

#ifdef DEBUG
	{
667 668
		lbs_deb_ioctl("FWT_LIST_ROUTE: line is %s\n", in_str);
		lbs_deb_ioctl("FWT_LIST_ROUTE: listing id:%i\n", le32_to_cpu(fwt_access.id));
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
	}
#endif

	ret = libertas_prepare_and_send_command(priv, cmd_fwt_access,
				    cmd_act_fwt_access_list_route,
				    cmd_option_waitforrsp, 0, (void *)&fwt_access);

	if (ret == 0) {
		pbuf += sprintf(pbuf, " ");
		pbuf += eth_addr2str(fwt_access.da, pbuf);
		pbuf += sprintf(pbuf, " %u", le32_to_cpu(fwt_access.metric));
		pbuf += sprintf(pbuf, " %u", fwt_access.dir);
		/* note that the firmware returns the nid in the id field */
		pbuf += sprintf(pbuf, " %u", le32_to_cpu(fwt_access.id));
		pbuf += sprintf(pbuf, " %u", le32_to_cpu(fwt_access.ssn));
		pbuf += sprintf(pbuf, " %u", le32_to_cpu(fwt_access.dsn));
		pbuf += sprintf(pbuf, "  hop %u", fwt_access.hopcount);
		pbuf += sprintf(pbuf, "  ttl %u", fwt_access.ttl);
		pbuf += sprintf(pbuf, " %u", le32_to_cpu(fwt_access.expiration));
	} else
		pbuf += sprintf(pbuf, " (null)");

	wrq->u.data.length = strlen(out_str);
	if (copy_to_user(wrq->u.data.pointer, (char *)out_str,
			 wrq->u.data.length)) {
694
		lbs_deb_ioctl("FWT_LIST_ROUTE: Copy to user failed!\n");
695 696 697
		return -EFAULT;
	}

698
	lbs_deb_leave(LBS_DEB_IOCTL);
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
	return 0;
}

/**
 *  @brief          List an entry from the FNT table
 *  @param priv     A pointer to wlan_private structure
 *  @param req      A pointer to ifreq structure
 *  @return         0 --success, otherwise fail
 */
static int wlan_fwt_list_neighbor_ioctl(wlan_private * priv, struct ifreq *req)
{
	struct iwreq *wrq = (struct iwreq *)req;
	char in_str[8];
	static struct cmd_ds_fwt_access fwt_access;
	char *ptr = in_str;
	static char out_str[128];
	char *pbuf = out_str;
	int ret;

718 719
	lbs_deb_enter(LBS_DEB_IOCTL);

720 721 722 723 724 725 726 727
	if (copy_from_user(in_str, wrq->u.data.pointer, sizeof(in_str)))
		return -EFAULT;

	memset(&fwt_access, 0, sizeof(fwt_access));
	fwt_access.id = cpu_to_le32(simple_strtoul(ptr, &ptr, 10));

#ifdef DEBUG
	{
728 729
		lbs_deb_ioctl("FWT_LIST_NEIGHBOR: line is %s\n", in_str);
		lbs_deb_ioctl("FWT_LIST_NEIGHBOR: listing id:%i\n", le32_to_cpu(fwt_access.id));
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
	}
#endif

	ret = libertas_prepare_and_send_command(priv, cmd_fwt_access,
				    cmd_act_fwt_access_list_neighbor,
				    cmd_option_waitforrsp, 0,
				    (void *)&fwt_access);

	if (ret == 0) {
		pbuf += sprintf(pbuf, " ra ");
		pbuf += eth_addr2str(fwt_access.ra, pbuf);
		pbuf += sprintf(pbuf, "  slp %u", fwt_access.sleepmode);
		pbuf += sprintf(pbuf, "  snr %u", le32_to_cpu(fwt_access.snr));
		pbuf += sprintf(pbuf, "  ref %u", le32_to_cpu(fwt_access.references));
	} else
		pbuf += sprintf(pbuf, " (null)");

	wrq->u.data.length = strlen(out_str);
	if (copy_to_user(wrq->u.data.pointer, (char *)out_str,
			 wrq->u.data.length)) {
750
		lbs_deb_ioctl("FWT_LIST_NEIGHBOR: Copy to user failed!\n");
751 752 753
		return -EFAULT;
	}

754
	lbs_deb_leave(LBS_DEB_IOCTL);
755 756 757 758 759 760 761 762 763 764 765 766
	return 0;
}

/**
 *  @brief          Cleans up the route (FRT) and neighbor (FNT) tables
 *                  (Garbage Collection)
 *  @param priv     A pointer to wlan_private structure
 *  @param req      A pointer to ifreq structure
 *  @return         0 --success, otherwise fail
 */
static int wlan_fwt_cleanup_ioctl(wlan_private * priv, struct ifreq *req)
{
D
Dan Williams 已提交
767
	struct iwreq *wrq = (struct iwreq *)req;
768 769 770
	static struct cmd_ds_fwt_access fwt_access;
	int ret;

771
	lbs_deb_enter(LBS_DEB_IOCTL);
772

773
	lbs_deb_ioctl("FWT: cleaning up\n");
774 775 776 777 778 779 780 781 782

	memset(&fwt_access, 0, sizeof(fwt_access));

	ret = libertas_prepare_and_send_command(priv, cmd_fwt_access,
				    cmd_act_fwt_access_cleanup,
				    cmd_option_waitforrsp, 0,
				    (void *)&fwt_access);

	if (ret == 0)
D
Dan Williams 已提交
783
		wrq->u.param.value = le32_to_cpu(fwt_access.references);
784 785 786
	else
		return -EFAULT;

787
	lbs_deb_leave(LBS_DEB_IOCTL);
788 789 790 791 792 793 794 795 796 797 798
	return 0;
}

/**
 *  @brief          Gets firmware internal time (debug purposes)
 *  @param priv     A pointer to wlan_private structure
 *  @param req      A pointer to ifreq structure
 *  @return         0 --success, otherwise fail
 */
static int wlan_fwt_time_ioctl(wlan_private * priv, struct ifreq *req)
{
D
Dan Williams 已提交
799
	struct iwreq *wrq = (struct iwreq *)req;
800 801 802
	static struct cmd_ds_fwt_access fwt_access;
	int ret;

803
	lbs_deb_enter(LBS_DEB_IOCTL);
804

805
	lbs_deb_ioctl("FWT: getting time\n");
806 807 808 809 810 811 812 813 814

	memset(&fwt_access, 0, sizeof(fwt_access));

	ret = libertas_prepare_and_send_command(priv, cmd_fwt_access,
				    cmd_act_fwt_access_time,
				    cmd_option_waitforrsp, 0,
				    (void *)&fwt_access);

	if (ret == 0)
D
Dan Williams 已提交
815
		wrq->u.param.value = le32_to_cpu(fwt_access.references);
816 817 818
	else
		return -EFAULT;

819
	lbs_deb_leave(LBS_DEB_IOCTL);
820 821 822 823 824 825 826 827 828 829 830
	return 0;
}

/**
 *  @brief          Gets mesh ttl from firmware
 *  @param priv     A pointer to wlan_private structure
 *  @param req      A pointer to ifreq structure
 *  @return         0 --success, otherwise fail
 */
static int wlan_mesh_get_ttl_ioctl(wlan_private * priv, struct ifreq *req)
{
D
Dan Williams 已提交
831
	struct iwreq *wrq = (struct iwreq *)req;
832 833 834
	struct cmd_ds_mesh_access mesh_access;
	int ret;

835
	lbs_deb_enter(LBS_DEB_IOCTL);
836 837 838 839 840 841 842 843

	memset(&mesh_access, 0, sizeof(mesh_access));

	ret = libertas_prepare_and_send_command(priv, cmd_mesh_access,
				    cmd_act_mesh_get_ttl,
				    cmd_option_waitforrsp, 0,
				    (void *)&mesh_access);

D
Dan Williams 已提交
844 845
	if (ret == 0)
		wrq->u.param.value = le32_to_cpu(mesh_access.data[0]);
846 847 848
	else
		return -EFAULT;

849
	lbs_deb_leave(LBS_DEB_IOCTL);
850 851 852 853 854 855 856 857 858 859 860 861 862 863
	return 0;
}

/**
 *  @brief          Gets mesh ttl from firmware
 *  @param priv     A pointer to wlan_private structure
 *  @param ttl      New ttl value
 *  @return         0 --success, otherwise fail
 */
static int wlan_mesh_set_ttl_ioctl(wlan_private * priv, int ttl)
{
	struct cmd_ds_mesh_access mesh_access;
	int ret;

864
	lbs_deb_enter(LBS_DEB_IOCTL);
865 866 867 868 869 870 871 872 873 874 875 876 877 878 879

	if( (ttl > 0xff) || (ttl < 0) )
		return -EINVAL;

	memset(&mesh_access, 0, sizeof(mesh_access));
	mesh_access.data[0] = ttl;

	ret = libertas_prepare_and_send_command(priv, cmd_mesh_access,
						cmd_act_mesh_set_ttl,
						cmd_option_waitforrsp, 0,
						(void *)&mesh_access);

	if (ret != 0)
		ret = -EFAULT;

880
	lbs_deb_leave(LBS_DEB_IOCTL);
881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901
	return ret;
}

/**
 *  @brief ioctl function - entry point
 *
 *  @param dev		A pointer to net_device structure
 *  @param req	   	A pointer to ifreq structure
 *  @param cmd 		command
 *  @return 	   	0--success, otherwise fail
 */
int libertas_do_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
{
	int subcmd = 0;
	int idata = 0;
	int *pdata;
	int ret = 0;
	wlan_private *priv = dev->priv;
	wlan_adapter *adapter = priv->adapter;
	struct iwreq *wrq = (struct iwreq *)req;

902
	lbs_deb_enter(LBS_DEB_IOCTL);
903

904
	lbs_deb_ioctl("libertas_do_ioctl: ioctl cmd = 0x%x\n", cmd);
905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
	switch (cmd) {
	case WLAN_SETNONE_GETNONE:	/* set WPA mode on/off ioctl #20 */
		switch (wrq->u.data.flags) {
		case WLAN_SUBCMD_BT_RESET:	/* bt_reset */
			wlan_bt_reset_ioctl(priv);
			break;
		case WLAN_SUBCMD_FWT_RESET:	/* fwt_reset */
			wlan_fwt_reset_ioctl(priv);
			break;
		}		/* End of switch */
		break;

	case WLAN_SETONEINT_GETNONE:
		/* The first 4 bytes of req->ifr_data is sub-ioctl number
		 * after 4 bytes sits the payload.
		 */
D
Dan Williams 已提交
921
		subcmd = wrq->u.data.flags;
922
		if (!subcmd)
D
Dan Williams 已提交
923
			subcmd = (int)wrq->u.param.value;
924 925 926 927 928 929 930 931 932 933 934

		switch (subcmd) {
		case WLANSETREGION:
			idata = SUBCMD_DATA(wrq);
			ret = wlan_set_region(priv, (u16) idata);
			break;
		case WLAN_SUBCMD_MESH_SET_TTL:
			idata = SUBCMD_DATA(wrq);
			ret = wlan_mesh_set_ttl_ioctl(priv, idata);
			break;

935 936 937 938
		case WLAN_SUBCMD_BT_SET_INVERT:
			ret = wlan_bt_set_invert_ioctl(priv, req);
			break ;

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 974 975 976 977 978
		default:
			ret = -EOPNOTSUPP;
			break;
		}

		break;

	case WLAN_SET128CHAR_GET128CHAR:
		switch ((int)wrq->u.data.flags) {
		case WLAN_SUBCMD_BT_ADD:
			ret = wlan_bt_add_ioctl(priv, req);
			break;
		case WLAN_SUBCMD_BT_DEL:
			ret = wlan_bt_del_ioctl(priv, req);
			break;
		case WLAN_SUBCMD_BT_LIST:
			ret = wlan_bt_list_ioctl(priv, req);
			break;
		case WLAN_SUBCMD_FWT_ADD:
			ret = wlan_fwt_add_ioctl(priv, req);
			break;
		case WLAN_SUBCMD_FWT_DEL:
			ret = wlan_fwt_del_ioctl(priv, req);
			break;
		case WLAN_SUBCMD_FWT_LOOKUP:
			ret = wlan_fwt_lookup_ioctl(priv, req);
			break;
		case WLAN_SUBCMD_FWT_LIST_NEIGHBOR:
			ret = wlan_fwt_list_neighbor_ioctl(priv, req);
			break;
		case WLAN_SUBCMD_FWT_LIST:
			ret = wlan_fwt_list_ioctl(priv, req);
			break;
		case WLAN_SUBCMD_FWT_LIST_ROUTE:
			ret = wlan_fwt_list_route_ioctl(priv, req);
			break;
		}
		break;

	case WLAN_SETNONE_GETONEINT:
D
Dan Williams 已提交
979
		switch (wrq->u.param.value) {
980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995
		case WLANGETREGION:
			pdata = (int *)wrq->u.name;
			*pdata = (int)adapter->regioncode;
			break;
		case WLAN_SUBCMD_FWT_CLEANUP:	/* fwt_cleanup */
			ret = wlan_fwt_cleanup_ioctl(priv, req);
			break;

		case WLAN_SUBCMD_FWT_TIME:	/* fwt_time */
			ret = wlan_fwt_time_ioctl(priv, req);
			break;

		case WLAN_SUBCMD_MESH_GET_TTL:
			ret = wlan_mesh_get_ttl_ioctl(priv, req);
			break;

996 997 998 999
		case WLAN_SUBCMD_BT_GET_INVERT:
			ret = wlan_bt_get_invert_ioctl(priv, req);
			break ;

1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
		default:
			ret = -EOPNOTSUPP;

		}

		break;

	case WLAN_SET_GET_SIXTEEN_INT:
		switch ((int)wrq->u.data.flags) {
		case WLAN_LED_GPIO_CTRL:
			{
				int i;
				int data[16];

				struct cmd_ds_802_11_led_ctrl ctrl;
				struct mrvlietypes_ledgpio *gpio =
				    (struct mrvlietypes_ledgpio *) ctrl.data;

				memset(&ctrl, 0, sizeof(ctrl));
				if (wrq->u.data.length > MAX_LEDS * 2)
					return -ENOTSUPP;
				if ((wrq->u.data.length % 2) != 0)
					return -ENOTSUPP;
				if (wrq->u.data.length == 0) {
					ctrl.action =
					    cpu_to_le16
					    (cmd_act_get);
				} else {
					if (copy_from_user
					    (data, wrq->u.data.pointer,
					     sizeof(int) *
					     wrq->u.data.length)) {
1032
						lbs_deb_ioctl(
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
						       "Copy from user failed\n");
						return -EFAULT;
					}

					ctrl.action =
					    cpu_to_le16
					    (cmd_act_set);
					ctrl.numled = cpu_to_le16(0);
					gpio->header.type =
					    cpu_to_le16(TLV_TYPE_LED_GPIO);
					gpio->header.len = wrq->u.data.length;
					for (i = 0; i < wrq->u.data.length;
					     i += 2) {
						gpio->ledpin[i / 2].led =
						    data[i];
						gpio->ledpin[i / 2].pin =
						    data[i + 1];
					}
				}
				ret =
				    libertas_prepare_and_send_command(priv,
							  cmd_802_11_led_gpio_ctrl,
							  0,
							  cmd_option_waitforrsp,
							  0, (void *)&ctrl);
				for (i = 0; i < gpio->header.len; i += 2) {
					data[i] = gpio->ledpin[i / 2].led;
					data[i + 1] = gpio->ledpin[i / 2].pin;
				}
				if (copy_to_user(wrq->u.data.pointer, data,
						 sizeof(int) *
						 gpio->header.len)) {
1065
					lbs_deb_ioctl("Copy to user failed\n");
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
					return -EFAULT;
				}

				wrq->u.data.length = gpio->header.len;
			}
			break;
		}
		break;

	default:
		ret = -EINVAL;
		break;
	}
1079 1080

	lbs_deb_leave_args(LBS_DEB_IOCTL, "ret %d", ret);
1081 1082 1083 1084
	return ret;
}