summaryrefslogtreecommitdiffstats
path: root/drivers/staging/iio/industrialio-trigger.c
blob: 57dd9232cf0d91884dad5af732ab9e21fdbe150b (plain) (blame)
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
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
318
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
346
347
348
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
374
375
376
377
378
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
423
424
425
426
427
428
429
430
431
/* The industrial I/O core, trigger handling functions
 *
 * Copyright (c) 2008 Jonathan Cameron
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published by
 * the Free Software Foundation.
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/idr.h>
#include <linux/err.h>
#include <linux/device.h>
#include <linux/interrupt.h>
#include <linux/list.h>
#include <linux/slab.h>

#include "iio.h"
#include "trigger.h"
#include "trigger_consumer.h"

/* RFC - Question of approach
 * Make the common case (single sensor single trigger)
 * simple by starting trigger capture from when first sensors
 * is added.
 *
 * Complex simultaneous start requires use of 'hold' functionality
 * of the trigger. (not implemented)
 *
 * Any other suggestions?
 */

static DEFINE_IDR(iio_trigger_idr);
static DEFINE_SPINLOCK(iio_trigger_idr_lock);

/* Single list of all available triggers */
static LIST_HEAD(iio_trigger_list);
static DEFINE_MUTEX(iio_trigger_list_lock);

/**
 * iio_trigger_register_sysfs() - create a device for this trigger
 * @trig_info:	the trigger
 *
 * Also adds any control attribute registered by the trigger driver
 **/
static int iio_trigger_register_sysfs(struct iio_trigger *trig_info)
{
	int ret = 0;

	if (trig_info->control_attrs)
		ret = sysfs_create_group(&trig_info->dev.kobj,
					 trig_info->control_attrs);

	return ret;
}

static void iio_trigger_unregister_sysfs(struct iio_trigger *trig_info)
{
	if (trig_info->control_attrs)
		sysfs_remove_group(&trig_info->dev.kobj,
				   trig_info->control_attrs);
}


/**
 * iio_trigger_register_id() - get a unique id for this trigger
 * @trig_info:	the trigger
 **/
static int iio_trigger_register_id(struct iio_trigger *trig_info)
{
	int ret = 0;

idr_again:
	if (unlikely(idr_pre_get(&iio_trigger_idr, GFP_KERNEL) == 0))
		return -ENOMEM;

	spin_lock(&iio_trigger_idr_lock);
	ret = idr_get_new(&iio_trigger_idr, NULL, &trig_info->id);
	spin_unlock(&iio_trigger_idr_lock);
	if (unlikely(ret == -EAGAIN))
		goto idr_again;
	else if (likely(!ret))
		trig_info->id = trig_info->id & MAX_ID_MASK;

	return ret;
}

/**
 * iio_trigger_unregister_id() - free up unique id for use by another trigger
 * @trig_info: the trigger
 **/
static void iio_trigger_unregister_id(struct iio_trigger *trig_info)
{
	spin_lock(&iio_trigger_idr_lock);
	idr_remove(&iio_trigger_idr, trig_info->id);
	spin_unlock(&iio_trigger_idr_lock);
}

int iio_trigger_register(struct iio_trigger *trig_info)
{
	int ret;

	ret = iio_trigger_register_id(trig_info);
	if (ret)
		goto error_ret;
	/* Set the name used for the sysfs directory etc */
	dev_set_name(&trig_info->dev, "trigger%ld",
		     (unsigned long) trig_info->id);

	ret = device_add(&trig_info->dev);
	if (ret)
		goto error_unregister_id;

	ret = iio_trigger_register_sysfs(trig_info);
	if (ret)
		goto error_device_del;

	/* Add to list of available triggers held by the IIO core */
	mutex_lock(&iio_trigger_list_lock);
	list_add_tail(&trig_info->list, &iio_trigger_list);
	mutex_unlock(&iio_trigger_list_lock);

	return 0;

error_device_del:
	device_del(&trig_info->dev);
error_unregister_id:
	iio_trigger_unregister_id(trig_info);
error_ret:
	return ret;
}
EXPORT_SYMBOL(iio_trigger_register);

void iio_trigger_unregister(struct iio_trigger *trig_info)
{
	struct iio_trigger *cursor;

	mutex_lock(&iio_trigger_list_lock);
	list_for_each_entry(cursor, &iio_trigger_list, list)
		if (cursor == trig_info) {
			list_del(&cursor->list);
			break;
		}
	mutex_unlock(&iio_trigger_list_lock);

	iio_trigger_unregister_sysfs(trig_info);
	iio_trigger_unregister_id(trig_info);
	/* Possible issue in here */
	device_unregister(&trig_info->dev);
}
EXPORT_SYMBOL(iio_trigger_unregister);

struct iio_trigger *iio_trigger_find_by_name(const char *name, size_t len)
{
	struct iio_trigger *trig;
	bool found = false;

	if (len && name[len - 1] == '\n')
		len--;

	mutex_lock(&iio_trigger_list_lock);
	list_for_each_entry(trig, &iio_trigger_list, list) {
		if (strncmp(trig->name, name, len) == 0) {
			found = true;
			break;
		}
	}
	mutex_unlock(&iio_trigger_list_lock);

	return found ? trig : NULL;
}
EXPORT_SYMBOL(iio_trigger_find_by_name);

void iio_trigger_poll(struct iio_trigger *trig, s64 time)
{
	struct iio_poll_func *pf_cursor;

	list_for_each_entry(pf_cursor, &trig->pollfunc_list, list) {
		if (pf_cursor->poll_func_immediate) {
			pf_cursor->poll_func_immediate(pf_cursor->private_data);
			trig->use_count++;
		}
	}
	list_for_each_entry(pf_cursor, &trig->pollfunc_list, list) {
		if (pf_cursor->poll_func_main) {
			pf_cursor->poll_func_main(pf_cursor->private_data,
						  time);
			trig->use_count++;
		}
	}
}
EXPORT_SYMBOL(iio_trigger_poll);

void iio_trigger_notify_done(struct iio_trigger *trig)
{
	trig->use_count--;
	if (trig->use_count == 0 && trig->try_reenable)
		if (trig->try_reenable(trig)) {
			/* Missed and interrupt so launch new poll now */
			iio_trigger_poll(trig, 0);
		}
}
EXPORT_SYMBOL(iio_trigger_notify_done);

/**
 * iio_trigger_read_name() - retrieve useful identifying name
 **/
ssize_t iio_trigger_read_name(struct device *dev,
			      struct device_attribute *attr,
			      char *buf)
{
	struct iio_trigger *trig = dev_get_drvdata(dev);
	return sprintf(buf, "%s\n", trig->name);
}
EXPORT_SYMBOL(iio_trigger_read_name);

/* Trigger Consumer related functions */

/* Complexity in here.  With certain triggers (datardy) an acknowledgement
 * may be needed if the pollfuncs do not include the data read for the
 * triggering device.
 * This is not currently handled.  Alternative of not enabling trigger unless
 * the relevant function is in there may be the best option.
 */
/* Worth protecting against double additions?*/
int iio_trigger_attach_poll_func(struct iio_trigger *trig,
				 struct iio_poll_func *pf)
{
	int ret = 0;
	unsigned long flags;

	spin_lock_irqsave(&trig->pollfunc_list_lock, flags);
	list_add_tail(&pf->list, &trig->pollfunc_list);
	spin_unlock_irqrestore(&trig->pollfunc_list_lock, flags);

	if (trig->set_trigger_state)
		ret = trig->set_trigger_state(trig, true);
	if (ret) {
		printk(KERN_ERR "set trigger state failed\n");
		list_del(&pf->list);
	}
	return ret;
}
EXPORT_SYMBOL(iio_trigger_attach_poll_func);

int iio_trigger_dettach_poll_func(struct iio_trigger *trig,
				  struct iio_poll_func *pf)
{
	struct iio_poll_func *pf_cursor;
	unsigned long flags;
	int ret = -EINVAL;

	spin_lock_irqsave(&trig->pollfunc_list_lock, flags);
	list_for_each_entry(pf_cursor, &trig->pollfunc_list, list)
		if (pf_cursor == pf) {
			ret = 0;
			break;
		}
	if (!ret) {
		if (list_is_singular(&trig->pollfunc_list)
		    && trig->set_trigger_state) {
			spin_unlock_irqrestore(&trig->pollfunc_list_lock,
					       flags);
			/* May sleep hence cannot hold the spin lock */
			ret = trig->set_trigger_state(trig, false);
			if (ret)
				goto error_ret;
			spin_lock_irqsave(&trig->pollfunc_list_lock, flags);
		}
		/*
		 * Now we can delete safe in the knowledge that, if this is
		 * the last pollfunc then we have disabled the trigger anyway
		 * and so nothing should be able to call the pollfunc.
		 */
		list_del(&pf_cursor->list);
	}
	spin_unlock_irqrestore(&trig->pollfunc_list_lock, flags);

error_ret:
	return ret;
}
EXPORT_SYMBOL(iio_trigger_dettach_poll_func);

/**
 * iio_trigger_read_currrent() - trigger consumer sysfs query which trigger
 *
 * For trigger consumers the current_trigger interface allows the trigger
 * used by the device to be queried.
 **/
static ssize_t iio_trigger_read_current(struct device *dev,
					struct device_attribute *attr,
					char *buf)
{
	struct iio_dev *dev_info = dev_get_drvdata(dev);
	int len = 0;
	if (dev_info->trig)
		len = sprintf(buf,
			      "%s\n",
			      dev_info->trig->name);
	return len;
}

/**
 * iio_trigger_write_current() trigger consumer sysfs set current trigger
 *
 * For trigger consumers the current_trigger interface allows the trigger
 * used for this device to be specified at run time based on the triggers
 * name.
 **/
static ssize_t iio_trigger_write_current(struct device *dev,
					 struct device_attribute *attr,
					 const char *buf,
					 size_t len)
{
	struct iio_dev *dev_info = dev_get_drvdata(dev);
	struct iio_trigger *oldtrig = dev_info->trig;
	mutex_lock(&dev_info->mlock);
	if (dev_info->currentmode == INDIO_RING_TRIGGERED) {
		mutex_unlock(&dev_info->mlock);
		return -EBUSY;
	}
	mutex_unlock(&dev_info->mlock);

	dev_info->trig = iio_trigger_find_by_name(buf, len);
	if (oldtrig && dev_info->trig != oldtrig)
		iio_put_trigger(oldtrig);
	if (dev_info->trig)
		iio_get_trigger(dev_info->trig);

	return len;
}

static DEVICE_ATTR(current_trigger, S_IRUGO | S_IWUSR,
		   iio_trigger_read_current,
		   iio_trigger_write_current);

static struct attribute *iio_trigger_consumer_attrs[] = {
	&dev_attr_current_trigger.attr,
	NULL,
};

static const struct attribute_group iio_trigger_consumer_attr_group = {
	.name = "trigger",
	.attrs = iio_trigger_consumer_attrs,
};

static void iio_trig_release(struct device *device)
{
	struct iio_trigger *trig = to_iio_trigger(device);
	kfree(trig);
	iio_put();
}

static struct device_type iio_trig_type = {
	.release = iio_trig_release,
};

struct iio_trigger *iio_allocate_trigger(void)
{
	struct iio_trigger *trig;
	trig = kzalloc(sizeof *trig, GFP_KERNEL);
	if (trig) {
		trig->dev.type = &iio_trig_type;
		trig->dev.bus = &iio_bus_type;
		device_initialize(&trig->dev);
		dev_set_drvdata(&trig->dev, (void *)trig);
		spin_lock_init(&trig->pollfunc_list_lock);
		INIT_LIST_HEAD(&trig->list);
		INIT_LIST_HEAD(&trig->pollfunc_list);
		iio_get();
	}
	return trig;
}
EXPORT_SYMBOL(iio_allocate_trigger);

void iio_free_trigger(struct iio_trigger *trig)
{
	if (trig)
		put_device(&trig->dev);
}
EXPORT_SYMBOL(iio_free_trigger);

int iio_device_register_trigger_consumer(struct iio_dev *dev_info)
{
	int ret;
	ret = sysfs_create_group(&dev_info->dev.kobj,
				 &iio_trigger_consumer_attr_group);
	return ret;
}
EXPORT_SYMBOL(iio_device_register_trigger_consumer);

int iio_device_unregister_trigger_consumer(struct iio_dev *dev_info)
{
	sysfs_remove_group(&dev_info->dev.kobj,
			   &iio_trigger_consumer_attr_group);
	return 0;
}
EXPORT_SYMBOL(iio_device_unregister_trigger_consumer);

int iio_alloc_pollfunc(struct iio_dev *indio_dev,
		       void (*immediate)(struct iio_dev *indio_dev),
		       void (*main)(struct iio_dev *private_data, s64 time))
{
	indio_dev->pollfunc = kzalloc(sizeof(*indio_dev->pollfunc), GFP_KERNEL);
	if (indio_dev->pollfunc == NULL)
		return -ENOMEM;
	indio_dev->pollfunc->poll_func_immediate = immediate;
	indio_dev->pollfunc->poll_func_main = main;
	indio_dev->pollfunc->private_data = indio_dev;
	return 0;
}
EXPORT_SYMBOL(iio_alloc_pollfunc);

int iio_triggered_ring_postenable(struct iio_dev *indio_dev)
{
	return indio_dev->trig
		? iio_trigger_attach_poll_func(indio_dev->trig,
					       indio_dev->pollfunc)
		: 0;
}
EXPORT_SYMBOL(iio_triggered_ring_postenable);

int iio_triggered_ring_predisable(struct iio_dev *indio_dev)
{
	return indio_dev->trig
		? iio_trigger_dettach_poll_func(indio_dev->trig,
						indio_dev->pollfunc)
		: 0;
}
EXPORT_SYMBOL(iio_triggered_ring_predisable);
n1617'>1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695
/*
 * Copyright (c) 2010 Broadcom Corporation
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <linux/kthread.h>
#include <linux/semaphore.h>
#include <bcmdefs.h>
#include <linux/netdevice.h>
#include <wlioctl.h>

#include <bcmutils.h>

#include <linux/if_arp.h>
#include <asm/uaccess.h>

#include <dngl_stats.h>
#include <dhd.h>
#include <dhdioctl.h>
#include <linux/ieee80211.h>
typedef const struct si_pub si_t;
#include <wlioctl.h>

#include <dngl_stats.h>
#include <dhd.h>

#define WL_ERROR(fmt, args...)	printk(fmt, ##args)
#define WL_TRACE(fmt, args...)	no_printk(fmt, ##args)
#define WL_INFORM(fmt, args...)	no_printk(fmt, ##args)
#define WL_WSEC(fmt, args...)	no_printk(fmt, ##args)
#define WL_SCAN(fmt, args...)	no_printk(fmt, ##args)

#include <wl_iw.h>

#define IW_WSEC_ENABLED(wsec)	((wsec) & (WEP_ENABLED |	\
					 TKIP_ENABLED | AES_ENABLED))

#include <linux/rtnetlink.h>

#define WL_IW_USE_ISCAN  1
#define ENABLE_ACTIVE_PASSIVE_SCAN_SUPPRESS  1

bool g_set_essid_before_scan = true;

#define WL_IW_IOCTL_CALL(func_call) \
	do {				\
		func_call;		\
	} while (0)

static int g_onoff = G_WLAN_SET_ON;
wl_iw_extra_params_t g_wl_iw_params;

extern bool wl_iw_conn_status_str(u32 event_type, u32 status,
				  u32 reason, char *stringBuf, uint buflen);

uint wl_msg_level = WL_ERROR_VAL;

#define MAX_WLIW_IOCTL_LEN 1024

#ifdef CONFIG_WIRELESS_EXT
extern int dhd_wait_pend8021x(struct net_device *dev);
#endif

#if WIRELESS_EXT < 19
#define IW_IOCTL_IDX(cmd)	((cmd) - SIOCIWFIRST)
#define IW_EVENT_IDX(cmd)	((cmd) - IWEVFIRST)
#endif

static void *g_scan;
static volatile uint g_scan_specified_ssid;
static wlc_ssid_t g_specific_ssid;

static wlc_ssid_t g_ssid;

#if defined(WL_IW_USE_ISCAN)
#define ISCAN_STATE_IDLE   0
#define ISCAN_STATE_SCANING 1

#define WLC_IW_ISCAN_MAXLEN   2048
typedef struct iscan_buf {
	struct iscan_buf *next;
	char iscan_buf[WLC_IW_ISCAN_MAXLEN];
} iscan_buf_t;

typedef struct iscan_info {
	struct net_device *dev;
	struct timer_list timer;
	u32 timer_ms;
	u32 timer_on;
	int iscan_state;
	iscan_buf_t *list_hdr;
	iscan_buf_t *list_cur;

	struct task_struct *sysioc_tsk;
	struct semaphore sysioc_sem;

#if defined CSCAN
	char ioctlbuf[WLC_IOCTL_MEDLEN];
#else
	char ioctlbuf[WLC_IOCTL_SMLEN];
#endif
	wl_iscan_params_t *iscan_ex_params_p;
	int iscan_ex_param_size;
} iscan_info_t;
iscan_info_t *g_iscan;

static const u8 ether_bcast[ETH_ALEN] = {255, 255, 255, 255, 255, 255};

/* Global ASSERT type flag */
u32 g_assert_type;

static void wl_iw_timerfunc(unsigned long data);
static void wl_iw_set_event_mask(struct net_device *dev);
static int wl_iw_iscan(iscan_info_t *iscan, wlc_ssid_t *ssid, u16 action);
#endif				/* defined(WL_IW_USE_ISCAN) */

static int
wl_iw_set_scan(struct net_device *dev,
	       struct iw_request_info *info,
	       union iwreq_data *wrqu, char *extra);

static int
wl_iw_get_scan(struct net_device *dev,
	       struct iw_request_info *info,
	       struct iw_point *dwrq, char *extra);

static uint
wl_iw_get_scan_prep(wl_scan_results_t *list,
		    struct iw_request_info *info, char *extra, short max_size);

static void swap_key_from_BE(wl_wsec_key_t *key)
{
	key->index = cpu_to_le32(key->index);
	key->len = cpu_to_le32(key->len);
	key->algo = cpu_to_le32(key->algo);
	key->flags = cpu_to_le32(key->flags);
	key->rxiv.hi = cpu_to_le32(key->rxiv.hi);
	key->rxiv.lo = cpu_to_le16(key->rxiv.lo);
	key->iv_initialized = cpu_to_le32(key->iv_initialized);
}

static void swap_key_to_BE(wl_wsec_key_t *key)
{
	key->index = le32_to_cpu(key->index);
	key->len = le32_to_cpu(key->len);
	key->algo = le32_to_cpu(key->algo);
	key->flags = le32_to_cpu(key->flags);
	key->rxiv.hi = le32_to_cpu(key->rxiv.hi);
	key->rxiv.lo = le16_to_cpu(key->rxiv.lo);
	key->iv_initialized = le32_to_cpu(key->iv_initialized);
}

static int dev_wlc_ioctl(struct net_device *dev, int cmd, void *arg, int len)
{
	struct ifreq ifr;
	wl_ioctl_t ioc;
	mm_segment_t fs;
	int ret = -EINVAL;

	if (!dev) {
		WL_ERROR("%s: dev is null\n", __func__);
		return ret;
	}

	WL_INFORM("\n%s, PID:%x: send Local IOCTL -> dhd: cmd:0x%x, buf:%p, len:%d\n",
		  __func__, current->pid, cmd, arg, len);

	if (g_onoff == G_WLAN_SET_ON) {
		memset(&ioc, 0, sizeof(ioc));
		ioc.cmd = cmd;
		ioc.buf = arg;
		ioc.len = len;

		strcpy(ifr.ifr_name, dev->name);
		ifr.ifr_data = (caddr_t)&ioc;

		ret = dev_open(dev);
		if (ret) {
			WL_ERROR("%s: Error dev_open: %d\n", __func__, ret);
			return ret;
		}

		fs = get_fs();
		set_fs(get_ds());
		ret = dev->netdev_ops->ndo_do_ioctl(dev, &ifr, SIOCDEVPRIVATE);
		set_fs(fs);
	} else {
		WL_TRACE("%s: call after driver stop : ignored\n", __func__);
	}
	return ret;
}

static int dev_wlc_intvar_set(struct net_device *dev, char *name, int val)
{
	char buf[WLC_IOCTL_SMLEN];
	uint len;

	val = cpu_to_le32(val);
	len = bcm_mkiovar(name, (char *)(&val), sizeof(val), buf, sizeof(buf));
	ASSERT(len);

	return dev_wlc_ioctl(dev, WLC_SET_VAR, buf, len);
}

#if defined(WL_IW_USE_ISCAN)
static int
dev_iw_iovar_setbuf(struct net_device *dev,
		    char *iovar,
		    void *param, int paramlen, void *bufptr, int buflen)
{
	int iolen;

	iolen = bcm_mkiovar(iovar, param, paramlen, bufptr, buflen);
	ASSERT(iolen);

	if (iolen == 0)
		return 0;

	return dev_wlc_ioctl(dev, WLC_SET_VAR, bufptr, iolen);
}

static int
dev_iw_iovar_getbuf(struct net_device *dev,
		    char *iovar,
		    void *param, int paramlen, void *bufptr, int buflen)
{
	int iolen;

	iolen = bcm_mkiovar(iovar, param, paramlen, bufptr, buflen);
	ASSERT(iolen);

	return dev_wlc_ioctl(dev, WLC_GET_VAR, bufptr, buflen);
}
#endif				/* defined(WL_IW_USE_ISCAN) */

#if WIRELESS_EXT > 17
static int
dev_wlc_bufvar_set(struct net_device *dev, char *name, char *buf, int len)
{
	static char ioctlbuf[MAX_WLIW_IOCTL_LEN];
	uint buflen;

	buflen = bcm_mkiovar(name, buf, len, ioctlbuf, sizeof(ioctlbuf));
	ASSERT(buflen);

	return dev_wlc_ioctl(dev, WLC_SET_VAR, ioctlbuf, buflen);
}
#endif				/* WIRELESS_EXT > 17 */

static int
dev_wlc_bufvar_get(struct net_device *dev, char *name, char *buf, int buflen)
{
	static char ioctlbuf[MAX_WLIW_IOCTL_LEN];
	int error;
	uint len;

	len = bcm_mkiovar(name, NULL, 0, ioctlbuf, sizeof(ioctlbuf));
	ASSERT(len);
	error =
	    dev_wlc_ioctl(dev, WLC_GET_VAR, (void *)ioctlbuf,
			  MAX_WLIW_IOCTL_LEN);
	if (!error)
		memcpy(buf, ioctlbuf, buflen);

	return error;
}

static int dev_wlc_intvar_get(struct net_device *dev, char *name, int *retval)
{
	union {
		char buf[WLC_IOCTL_SMLEN];
		int val;
	} var;
	int error;

	uint len;
	uint data_null;

	len =
	    bcm_mkiovar(name, (char *)(&data_null), 0, (char *)(&var),
			sizeof(var.buf));
	ASSERT(len);
	error = dev_wlc_ioctl(dev, WLC_GET_VAR, (void *)&var, len);

	*retval = le32_to_cpu(var.val);

	return error;
}

#if WIRELESS_EXT < 13
struct iw_request_info {
	__u16 cmd;
	__u16 flags;
};

typedef int (*iw_handler) (struct net_device *dev,
			   struct iw_request_info *info,
			   void *wrqu, char *extra);
#endif

static int
wl_iw_config_commit(struct net_device *dev,
		    struct iw_request_info *info, void *zwrq, char *extra)
{
	wlc_ssid_t ssid;
	int error;
	struct sockaddr bssid;

	WL_TRACE("%s: SIOCSIWCOMMIT\n", dev->name);

	error = dev_wlc_ioctl(dev, WLC_GET_SSID, &ssid, sizeof(ssid));
	if (error)
		return error;

	ssid.SSID_len = le32_to_cpu(ssid.SSID_len);

	if (!ssid.SSID_len)
		return 0;

	memset(&bssid, 0, sizeof(struct sockaddr));
	error = dev_wlc_ioctl(dev, WLC_REASSOC, &bssid, ETH_ALEN);
	if (error) {
		WL_ERROR("%s: WLC_REASSOC to %s failed\n",
			 __func__, ssid.SSID);
		return error;
	}

	return 0;
}

static int
wl_iw_get_name(struct net_device *dev,
	       struct iw_request_info *info, char *cwrq, char *extra)
{
	WL_TRACE("%s: SIOCGIWNAME\n", dev->name);

	strcpy(cwrq, "IEEE 802.11-DS");

	return 0;
}

static int
wl_iw_set_freq(struct net_device *dev,
	       struct iw_request_info *info, struct iw_freq *fwrq, char *extra)
{
	int error, chan;
	uint sf = 0;

	WL_TRACE("\n %s %s: SIOCSIWFREQ\n", __func__, dev->name);

	if (fwrq->e == 0 && fwrq->m < MAXCHANNEL) {
		chan = fwrq->m;
	} else {
		if (fwrq->e >= 6) {
			fwrq->e -= 6;
			while (fwrq->e--)
				fwrq->m *= 10;
		} else if (fwrq->e < 6) {
			while (fwrq->e++ < 6)
				fwrq->m /= 10;
		}
		if (fwrq->m > 4000 && fwrq->m < 5000)
			sf = WF_CHAN_FACTOR_4_G;

		chan = bcm_mhz2channel(fwrq->m, sf);
	}
	chan = cpu_to_le32(chan);

	error = dev_wlc_ioctl(dev, WLC_SET_CHANNEL, &chan, sizeof(chan));
	if (error)
		return error;

	g_wl_iw_params.target_channel = chan;
	return -EINPROGRESS;
}

static int
wl_iw_get_freq(struct net_device *dev,
	       struct iw_request_info *info, struct iw_freq *fwrq, char *extra)
{
	channel_info_t ci;
	int error;

	WL_TRACE("%s: SIOCGIWFREQ\n", dev->name);

	error = dev_wlc_ioctl(dev, WLC_GET_CHANNEL, &ci, sizeof(ci));
	if (error)
		return error;

	fwrq->m = le32_to_cpu(ci.hw_channel);
	fwrq->e = le32_to_cpu(0);
	return 0;
}

static int
wl_iw_set_mode(struct net_device *dev,
	       struct iw_request_info *info, __u32 *uwrq, char *extra)
{
	int infra = 0, ap = 0, error = 0;

	WL_TRACE("%s: SIOCSIWMODE\n", dev->name);

	switch (*uwrq) {
	case IW_MODE_MASTER:
		infra = ap = 1;
		break;
	case IW_MODE_ADHOC:
	case IW_MODE_AUTO:
		break;
	case IW_MODE_INFRA:
		infra = 1;
		break;
	default:
		return -EINVAL;
	}
	infra = cpu_to_le32(infra);
	ap = cpu_to_le32(ap);

	error = dev_wlc_ioctl(dev, WLC_SET_INFRA, &infra, sizeof(infra));
	if (error)
		return error;

	error = dev_wlc_ioctl(dev, WLC_SET_AP, &ap, sizeof(ap));
	if (error)
		return error;

	return -EINPROGRESS;
}

static int
wl_iw_get_mode(struct net_device *dev,
	       struct iw_request_info *info, __u32 *uwrq, char *extra)
{
	int error, infra = 0, ap = 0;

	WL_TRACE("%s: SIOCGIWMODE\n", dev->name);

	error = dev_wlc_ioctl(dev, WLC_GET_INFRA, &infra, sizeof(infra));
	if (error)
		return error;

	error = dev_wlc_ioctl(dev, WLC_GET_AP, &ap, sizeof(ap));
	if (error)
		return error;

	infra = le32_to_cpu(infra);
	ap = le32_to_cpu(ap);
	*uwrq = infra ? ap ? IW_MODE_MASTER : IW_MODE_INFRA : IW_MODE_ADHOC;

	return 0;
}

static int
wl_iw_get_range(struct net_device *dev,
		struct iw_request_info *info,
		struct iw_point *dwrq, char *extra)
{
	struct iw_range *range = (struct iw_range *)extra;
	wl_u32_list_t *list;
	wl_rateset_t rateset;
	s8 *channels;
	int error, i, k;
	uint ch;

	int phytype;
	int bw_cap = 0, sgi_tx = 0, nmode = 0;
	channel_info_t ci;
	u8 nrate_list2copy = 0;
	u16 nrate_list[4][8] = { {13, 26, 39, 52, 78, 104, 117, 130},
	{14, 29, 43, 58, 87, 116, 130, 144},
	{27, 54, 81, 108, 162, 216, 243, 270},
	{30, 60, 90, 120, 180, 240, 270, 300}
	};

	WL_TRACE("%s: SIOCGIWRANGE\n", dev->name);

	if (!extra)
		return -EINVAL;

	channels = kmalloc((MAXCHANNEL + 1) * 4, GFP_KERNEL);
	if (!channels) {
		WL_ERROR("Could not alloc channels\n");
		return -ENOMEM;
	}
	list = (wl_u32_list_t *) channels;

	dwrq->length = sizeof(struct iw_range);
	memset(range, 0, sizeof(*range));

	list->count = cpu_to_le32(MAXCHANNEL);
	error = dev_wlc_ioctl(dev, WLC_GET_VALID_CHANNELS, channels,
				(MAXCHANNEL + 1) * 4);
	if (error) {
		kfree(channels);
		return error;
	}
	for (i = 0; i < le32_to_cpu(list->count) && i < IW_MAX_FREQUENCIES;
	     i++) {
		range->freq[i].i = le32_to_cpu(list->element[i]);

		ch = le32_to_cpu(list->element[i]);
		if (ch <= CH_MAX_2G_CHANNEL) {
			range->freq[i].m = ieee80211_dsss_chan_to_freq(ch);
		} else {
			range->freq[i].m = ieee80211_ofdm_chan_to_freq(
						WF_CHAN_FACTOR_5_G/2, ch);
		}
		range->freq[i].e = 6;
	}
	range->num_frequency = range->num_channels = i;

	range->max_qual.qual = 5;
	range->max_qual.level = 0x100 - 200;
	range->max_qual.noise = 0x100 - 200;
	range->sensitivity = 65535;

#if WIRELESS_EXT > 11
	range->avg_qual.qual = 3;
	range->avg_qual.level = 0x100 + WL_IW_RSSI_GOOD;
	range->avg_qual.noise = 0x100 - 75;
#endif

	error = dev_wlc_ioctl(dev, WLC_GET_CURR_RATESET, &rateset,
				sizeof(rateset));
	if (error) {
		kfree(channels);
		return error;
	}
	rateset.count = le32_to_cpu(rateset.count);
	range->num_bitrates = rateset.count;
	for (i = 0; i < rateset.count && i < IW_MAX_BITRATES; i++)
		range->bitrate[i] = (rateset.rates[i] & 0x7f) * 500000;
	dev_wlc_intvar_get(dev, "nmode", &nmode);
	dev_wlc_ioctl(dev, WLC_GET_PHYTYPE, &phytype, sizeof(phytype));

	if (nmode == 1 && phytype == WLC_PHY_TYPE_SSN) {
		dev_wlc_intvar_get(dev, "mimo_bw_cap", &bw_cap);
		dev_wlc_intvar_get(dev, "sgi_tx", &sgi_tx);
		dev_wlc_ioctl(dev, WLC_GET_CHANNEL, &ci,
			      sizeof(channel_info_t));
		ci.hw_channel = le32_to_cpu(ci.hw_channel);

		if (bw_cap == 0 || (bw_cap == 2 && ci.hw_channel <= 14)) {
			if (sgi_tx == 0)
				nrate_list2copy = 0;
			else
				nrate_list2copy = 1;
		}
		if (bw_cap == 1 || (bw_cap == 2 && ci.hw_channel >= 36)) {
			if (sgi_tx == 0)
				nrate_list2copy = 2;
			else
				nrate_list2copy = 3;
		}
		range->num_bitrates += 8;
		for (k = 0; i < range->num_bitrates; k++, i++) {
			range->bitrate[i] =
			    (nrate_list[nrate_list2copy][k]) * 500000;
		}
	}

	error = dev_wlc_ioctl(dev, WLC_GET_PHYTYPE, &i, sizeof(i));
	if (error) {
		kfree(channels);
		return error;
	}
	i = le32_to_cpu(i);
	if (i == WLC_PHY_TYPE_A)
		range->throughput = 24000000;
	else
		range->throughput = 1500000;

	range->min_rts = 0;
	range->max_rts = 2347;
	range->min_frag = 256;
	range->max_frag = 2346;

	range->max_encoding_tokens = DOT11_MAX_DEFAULT_KEYS;
	range->num_encoding_sizes = 4;
	range->encoding_size[0] = WLAN_KEY_LEN_WEP40;
	range->encoding_size[1] = WLAN_KEY_LEN_WEP104;
#if WIRELESS_EXT > 17
	range->encoding_size[2] = WLAN_KEY_LEN_TKIP;
#else
	range->encoding_size[2] = 0;
#endif
	range->encoding_size[3] = WLAN_KEY_LEN_AES_CMAC;

	range->min_pmp = 0;
	range->max_pmp = 0;
	range->min_pmt = 0;
	range->max_pmt = 0;
	range->pmp_flags = 0;
	range->pm_capa = 0;

	range->num_txpower = 2;
	range->txpower[0] = 1;
	range->txpower[1] = 255;
	range->txpower_capa = IW_TXPOW_MWATT;

#if WIRELESS_EXT > 10
	range->we_version_compiled = WIRELESS_EXT;
	range->we_version_source = 19;

	range->retry_capa = IW_RETRY_LIMIT;
	range->retry_flags = IW_RETRY_LIMIT;
	range->r_time_flags = 0;
	range->min_retry = 1;
	range->max_retry = 255;
	range->min_r_time = 0;
	range->max_r_time = 0;
#endif

#if WIRELESS_EXT > 17
	range->enc_capa = IW_ENC_CAPA_WPA;
	range->enc_capa |= IW_ENC_CAPA_CIPHER_TKIP;
	range->enc_capa |= IW_ENC_CAPA_CIPHER_CCMP;
	range->enc_capa |= IW_ENC_CAPA_WPA2;

	IW_EVENT_CAPA_SET_KERNEL(range->event_capa);
	IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWAP);
	IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWSCAN);
	IW_EVENT_CAPA_SET(range->event_capa, IWEVTXDROP);
	IW_EVENT_CAPA_SET(range->event_capa, IWEVMICHAELMICFAILURE);
	IW_EVENT_CAPA_SET(range->event_capa, IWEVPMKIDCAND);
#endif				/* WIRELESS_EXT > 17 */

	kfree(channels);

	return 0;
}

static int rssi_to_qual(int rssi)
{
	if (rssi <= WL_IW_RSSI_NO_SIGNAL)
		return 0;
	else if (rssi <= WL_IW_RSSI_VERY_LOW)
		return 1;
	else if (rssi <= WL_IW_RSSI_LOW)
		return 2;
	else if (rssi <= WL_IW_RSSI_GOOD)
		return 3;
	else if (rssi <= WL_IW_RSSI_VERY_GOOD)
		return 4;
	else
		return 5;
}

static int
wl_iw_set_spy(struct net_device *dev,
	      struct iw_request_info *info, struct iw_point *dwrq, char *extra)
{
	wl_iw_t *iw = *(wl_iw_t **) netdev_priv(dev);
	struct sockaddr *addr = (struct sockaddr *)extra;
	int i;

	WL_TRACE("%s: SIOCSIWSPY\n", dev->name);

	if (!extra)
		return -EINVAL;

	iw->spy_num = min_t(int, ARRAY_SIZE(iw->spy_addr), dwrq->length);
	for (i = 0; i < iw->spy_num; i++)
		memcpy(iw->spy_addr[i], addr[i].sa_data, ETH_ALEN);
	memset(iw->spy_qual, 0, sizeof(iw->spy_qual));

	return 0;
}

static int
wl_iw_get_spy(struct net_device *dev,
	      struct iw_request_info *info, struct iw_point *dwrq, char *extra)
{
	wl_iw_t *iw = *(wl_iw_t **) netdev_priv(dev);
	struct sockaddr *addr = (struct sockaddr *)extra;
	struct iw_quality *qual = (struct iw_quality *)&addr[iw->spy_num];
	int i;

	WL_TRACE("%s: SIOCGIWSPY\n", dev->name);

	if (!extra)
		return -EINVAL;

	dwrq->length = iw->spy_num;
	for (i = 0; i < iw->spy_num; i++) {
		memcpy(addr[i].sa_data, iw->spy_addr[i], ETH_ALEN);
		addr[i].sa_family = AF_UNIX;
		memcpy(&qual[i], &iw->spy_qual[i], sizeof(struct iw_quality));
		iw->spy_qual[i].updated = 0;
	}

	return 0;
}

static int
wl_iw_ch_to_chanspec(int ch, wl_join_params_t *join_params,
		     int *join_params_size)
{
	chanspec_t chanspec = 0;

	if (ch != 0) {
		join_params->params.chanspec_num = 1;
		join_params->params.chanspec_list[0] = ch;

		if (join_params->params.chanspec_list[0])
			chanspec |= WL_CHANSPEC_BAND_2G;
		else
			chanspec |= WL_CHANSPEC_BAND_5G;

		chanspec |= WL_CHANSPEC_BW_20;
		chanspec |= WL_CHANSPEC_CTL_SB_NONE;

		*join_params_size += WL_ASSOC_PARAMS_FIXED_SIZE +
		    join_params->params.chanspec_num * sizeof(chanspec_t);

		join_params->params.chanspec_list[0] &= WL_CHANSPEC_CHAN_MASK;
		join_params->params.chanspec_list[0] |= chanspec;
		join_params->params.chanspec_list[0] =
		    cpu_to_le16(join_params->params.chanspec_list[0]);

		join_params->params.chanspec_num =
		    cpu_to_le32(join_params->params.chanspec_num);

		WL_TRACE("%s  join_params->params.chanspec_list[0]= %X\n",
			 __func__, join_params->params.chanspec_list[0]);
	}
	return 1;
}

static int
wl_iw_set_wap(struct net_device *dev,
	      struct iw_request_info *info, struct sockaddr *awrq, char *extra)
{
	int error = -EINVAL;
	wl_join_params_t join_params;
	int join_params_size;

	WL_TRACE("%s: SIOCSIWAP\n", dev->name);

	if (awrq->sa_family != ARPHRD_ETHER) {
		WL_ERROR("Invalid Header...sa_family\n");
		return -EINVAL;
	}

	if (is_broadcast_ether_addr(awrq->sa_data) ||
	    is_zero_ether_addr(awrq->sa_data)) {
		scb_val_t scbval;
		memset(&scbval, 0, sizeof(scb_val_t));
		(void)dev_wlc_ioctl(dev, WLC_DISASSOC, &scbval,
				    sizeof(scb_val_t));
		return 0;
	}

	memset(&join_params, 0, sizeof(join_params));
	join_params_size = sizeof(join_params.ssid);

	memcpy(join_params.ssid.SSID, g_ssid.SSID, g_ssid.SSID_len);
	join_params.ssid.SSID_len = cpu_to_le32(g_ssid.SSID_len);
	memcpy(&join_params.params.bssid, awrq->sa_data, ETH_ALEN);

	WL_TRACE("%s  target_channel=%d\n",
		 __func__, g_wl_iw_params.target_channel);
	wl_iw_ch_to_chanspec(g_wl_iw_params.target_channel, &join_params,
			     &join_params_size);

	error = dev_wlc_ioctl(dev, WLC_SET_SSID, &join_params,
				join_params_size);
	if (error) {
		WL_ERROR("%s Invalid ioctl data=%d\n", __func__, error);
	}

	if (g_ssid.SSID_len) {
		WL_TRACE("%s: join SSID=%s BSSID=%pM ch=%d\n",
			 __func__, g_ssid.SSID, awrq->sa_data,
			 g_wl_iw_params.target_channel);
	}

	memset(&g_ssid, 0, sizeof(g_ssid));
	return 0;
}

static int
wl_iw_get_wap(struct net_device *dev,
	      struct iw_request_info *info, struct sockaddr *awrq, char *extra)
{
	WL_TRACE("%s: SIOCGIWAP\n", dev->name);

	awrq->sa_family = ARPHRD_ETHER;
	memset(awrq->sa_data, 0, ETH_ALEN);

	(void)dev_wlc_ioctl(dev, WLC_GET_BSSID, awrq->sa_data, ETH_ALEN);

	return 0;
}

#if WIRELESS_EXT > 17
static int
wl_iw_mlme(struct net_device *dev,
	   struct iw_request_info *info, struct sockaddr *awrq, char *extra)
{
	struct iw_mlme *mlme;
	scb_val_t scbval;
	int error = -EINVAL;

	WL_TRACE("%s: SIOCSIWMLME DISASSOC/DEAUTH\n", dev->name);

	mlme = (struct iw_mlme *)extra;
	if (mlme == NULL) {
		WL_ERROR("Invalid ioctl data\n");
		return error;
	}

	scbval.val = mlme->reason_code;
	memcpy(&scbval.ea, &mlme->addr.sa_data, ETH_ALEN);

	if (mlme->cmd == IW_MLME_DISASSOC) {
		scbval.val = cpu_to_le32(scbval.val);
		error =
		    dev_wlc_ioctl(dev, WLC_DISASSOC, &scbval,
				  sizeof(scb_val_t));
	} else if (mlme->cmd == IW_MLME_DEAUTH) {
		scbval.val = cpu_to_le32(scbval.val);
		error =
		    dev_wlc_ioctl(dev, WLC_SCB_DEAUTHENTICATE_FOR_REASON,
				  &scbval, sizeof(scb_val_t));
	} else {
		WL_ERROR("Invalid ioctl data\n");
		return error;
	}

	return error;
}
#endif				/* WIRELESS_EXT > 17 */

#ifndef WL_IW_USE_ISCAN
static int
wl_iw_get_aplist(struct net_device *dev,
		 struct iw_request_info *info,
		 struct iw_point *dwrq, char *extra)
{
	wl_scan_results_t *list;
	struct sockaddr *addr = (struct sockaddr *)extra;
	struct iw_quality qual[IW_MAX_AP];
	wl_bss_info_t *bi = NULL;
	int error, i;
	uint buflen = dwrq->length;

	WL_TRACE("%s: SIOCGIWAPLIST\n", dev->name);

	if (!extra)
		return -EINVAL;

	list = kzalloc(buflen, GFP_KERNEL);
	if (!list)
		return -ENOMEM;
	list->buflen = cpu_to_le32(buflen);
	error = dev_wlc_ioctl(dev, WLC_SCAN_RESULTS, list, buflen);
	if (error) {
		WL_ERROR("%d: Scan results error %d\n", __LINE__, error);
		kfree(list);
		return error;
	}
	list->buflen = le32_to_cpu(list->buflen);
	list->version = le32_to_cpu(list->version);
	list->count = le32_to_cpu(list->count);
	if (list->version != WL_BSS_INFO_VERSION) {
		WL_ERROR("%s : list->version %d != WL_BSS_INFO_VERSION\n",
			 __func__, list->version);
		kfree(list);
		return -EINVAL;
	}

	for (i = 0, dwrq->length = 0;
	     i < list->count && dwrq->length < IW_MAX_AP; i++) {
		bi = bi ? (wl_bss_info_t *) ((unsigned long)bi +
					     le32_to_cpu(bi->length)) : list->
		    bss_info;
		ASSERT(((unsigned long)bi + le32_to_cpu(bi->length)) <=
		       ((unsigned long)list + buflen));

		if (!(le16_to_cpu(bi->capability) & WLAN_CAPABILITY_ESS))
			continue;

		memcpy(addr[dwrq->length].sa_data, &bi->BSSID, ETH_ALEN);
		addr[dwrq->length].sa_family = ARPHRD_ETHER;
		qual[dwrq->length].qual = rssi_to_qual(le16_to_cpu(bi->RSSI));
		qual[dwrq->length].level = 0x100 + le16_to_cpu(bi->RSSI);
		qual[dwrq->length].noise = 0x100 + bi->phy_noise;

#if WIRELESS_EXT > 18
		qual[dwrq->length].updated = IW_QUAL_ALL_UPDATED | IW_QUAL_DBM;
#else
		qual[dwrq->length].updated = 7;
#endif
		dwrq->length++;
	}

	kfree(list);

	if (dwrq->length) {
		memcpy(&addr[dwrq->length], qual,
		       sizeof(struct iw_quality) * dwrq->length);
		dwrq->flags = 1;
	}

	return 0;
}
#endif				/* WL_IW_USE_ISCAN */

#ifdef WL_IW_USE_ISCAN
static int
wl_iw_iscan_get_aplist(struct net_device *dev,
		       struct iw_request_info *info,
		       struct iw_point *dwrq, char *extra)
{
	wl_scan_results_t *list;
	iscan_buf_t *buf;
	iscan_info_t *iscan = g_iscan;

	struct sockaddr *addr = (struct sockaddr *)extra;
	struct iw_quality qual[IW_MAX_AP];
	wl_bss_info_t *bi = NULL;
	int i;

	WL_TRACE("%s: SIOCGIWAPLIST\n", dev->name);

	if (!extra)
		return -EINVAL;

	if ((!iscan) || (!iscan->sysioc_tsk)) {
		WL_ERROR("%s error\n", __func__);
		return 0;
	}

	buf = iscan->list_hdr;
	while (buf) {
		list = &((wl_iscan_results_t *) buf->iscan_buf)->results;
		if (list->version != WL_BSS_INFO_VERSION) {
			WL_ERROR("%s : list->version %d != WL_BSS_INFO_VERSION\n",
				 __func__, list->version);
			return -EINVAL;
		}

		bi = NULL;
		for (i = 0, dwrq->length = 0;
		     i < list->count && dwrq->length < IW_MAX_AP; i++) {
			bi = bi ? (wl_bss_info_t *) ((unsigned long)bi +
						     le32_to_cpu(bi->length)) :
			    list->bss_info;
			ASSERT(((unsigned long)bi + le32_to_cpu(bi->length)) <=
			       ((unsigned long)list + WLC_IW_ISCAN_MAXLEN));

			if (!(le16_to_cpu(bi->capability) &
			      WLAN_CAPABILITY_ESS))
				continue;

			memcpy(addr[dwrq->length].sa_data, &bi->BSSID,
			       ETH_ALEN);
			addr[dwrq->length].sa_family = ARPHRD_ETHER;
			qual[dwrq->length].qual =
			    rssi_to_qual(le16_to_cpu(bi->RSSI));
			qual[dwrq->length].level = 0x100 +
							le16_to_cpu(bi->RSSI);
			qual[dwrq->length].noise = 0x100 + bi->phy_noise;

#if WIRELESS_EXT > 18
			qual[dwrq->length].updated =
			    IW_QUAL_ALL_UPDATED | IW_QUAL_DBM;
#else
			qual[dwrq->length].updated = 7;
#endif

			dwrq->length++;
		}
		buf = buf->next;
	}
	if (dwrq->length) {
		memcpy(&addr[dwrq->length], qual,
		       sizeof(struct iw_quality) * dwrq->length);
		dwrq->flags = 1;
	}

	return 0;
}

static int wl_iw_iscan_prep(wl_scan_params_t *params, wlc_ssid_t *ssid)
{
	int err = 0;

	memcpy(params->bssid, ether_bcast, ETH_ALEN);
	params->bss_type = DOT11_BSSTYPE_ANY;
	params->scan_type = 0;
	params->nprobes = -1;
	params->active_time = -1;
	params->passive_time = -1;
	params->home_time = -1;
	params->channel_num = 0;

	params->nprobes = cpu_to_le32(params->nprobes);
	params->active_time = cpu_to_le32(params->active_time);
	params->passive_time = cpu_to_le32(params->passive_time);
	params->home_time = cpu_to_le32(params->home_time);
	if (ssid && ssid->SSID_len)
		memcpy(&params->ssid, ssid, sizeof(wlc_ssid_t));

	return err;
}

static int wl_iw_iscan(iscan_info_t *iscan, wlc_ssid_t *ssid, u16 action)
{
	int err = 0;

	iscan->iscan_ex_params_p->version = cpu_to_le32(ISCAN_REQ_VERSION);
	iscan->iscan_ex_params_p->action = cpu_to_le16(action);
	iscan->iscan_ex_params_p->scan_duration = cpu_to_le16(0);

	WL_SCAN("%s : nprobes=%d\n",
		__func__, iscan->iscan_ex_params_p->params.nprobes);
	WL_SCAN("active_time=%d\n",
		 iscan->iscan_ex_params_p->params.active_time);
	WL_SCAN("passive_time=%d\n",
		 iscan->iscan_ex_params_p->params.passive_time);
	WL_SCAN("home_time=%d\n", iscan->iscan_ex_params_p->params.home_time);
	WL_SCAN("scan_type=%d\n", iscan->iscan_ex_params_p->params.scan_type);
	WL_SCAN("bss_type=%d\n", iscan->iscan_ex_params_p->params.bss_type);

	(void)dev_iw_iovar_setbuf(iscan->dev, "iscan", iscan->iscan_ex_params_p,
				  iscan->iscan_ex_param_size, iscan->ioctlbuf,
				  sizeof(iscan->ioctlbuf));

	return err;
}

static void wl_iw_timerfunc(unsigned long data)
{
	iscan_info_t *iscan = (iscan_info_t *) data;
	if (iscan) {
		iscan->timer_on = 0;
		if (iscan->iscan_state != ISCAN_STATE_IDLE) {
			WL_TRACE("timer trigger\n");
			up(&iscan->sysioc_sem);
		}
	}
}

static void wl_iw_set_event_mask(struct net_device *dev)
{
	char eventmask[WL_EVENTING_MASK_LEN];
	char iovbuf[WL_EVENTING_MASK_LEN + 12];

	dev_iw_iovar_getbuf(dev, "event_msgs", "", 0, iovbuf, sizeof(iovbuf));
	memcpy(eventmask, iovbuf, WL_EVENTING_MASK_LEN);
	setbit(eventmask, WLC_E_SCAN_COMPLETE);
	dev_iw_iovar_setbuf(dev, "event_msgs", eventmask, WL_EVENTING_MASK_LEN,
			    iovbuf, sizeof(iovbuf));
}

static u32 wl_iw_iscan_get(iscan_info_t *iscan)
{
	iscan_buf_t *buf;
	iscan_buf_t *ptr;
	wl_iscan_results_t *list_buf;
	wl_iscan_results_t list;
	wl_scan_results_t *results;
	u32 status;
	int res = 0;

	MUTEX_LOCK_WL_SCAN_SET();
	if (iscan->list_cur) {
		buf = iscan->list_cur;
		iscan->list_cur = buf->next;
	} else {
		buf = kmalloc(sizeof(iscan_buf_t), GFP_KERNEL);
		if (!buf) {
			WL_ERROR("%s can't alloc iscan_buf_t : going to abort current iscan\n",
				 __func__);
			MUTEX_UNLOCK_WL_SCAN_SET();
			return WL_SCAN_RESULTS_NO_MEM;
		}
		buf->next = NULL;
		if (!iscan->list_hdr)
			iscan->list_hdr = buf;
		else {
			ptr = iscan->list_hdr;
			while (ptr->next) {
				ptr = ptr->next;
			}
			ptr->next = buf;
		}
	}
	memset(buf->iscan_buf, 0, WLC_IW_ISCAN_MAXLEN);
	list_buf = (wl_iscan_results_t *) buf->iscan_buf;
	results = &list_buf->results;
	results->buflen = WL_ISCAN_RESULTS_FIXED_SIZE;
	results->version = 0;
	results->count = 0;

	memset(&list, 0, sizeof(list));
	list.results.buflen = cpu_to_le32(WLC_IW_ISCAN_MAXLEN);
	res = dev_iw_iovar_getbuf(iscan->dev,
				  "iscanresults",
				  &list,
				  WL_ISCAN_RESULTS_FIXED_SIZE,
				  buf->iscan_buf, WLC_IW_ISCAN_MAXLEN);
	if (res == 0) {
		results->buflen = le32_to_cpu(results->buflen);
		results->version = le32_to_cpu(results->version);
		results->count = le32_to_cpu(results->count);
		WL_TRACE("results->count = %d\n", results->count);
		WL_TRACE("results->buflen = %d\n", results->buflen);
		status = le32_to_cpu(list_buf->status);
	} else {
		WL_ERROR("%s returns error %d\n", __func__, res);
		status = WL_SCAN_RESULTS_NO_MEM;
	}
	MUTEX_UNLOCK_WL_SCAN_SET();
	return status;
}

static void wl_iw_force_specific_scan(iscan_info_t *iscan)
{
	WL_TRACE("%s force Specific SCAN for %s\n",
		 __func__, g_specific_ssid.SSID);
	rtnl_lock();

	(void)dev_wlc_ioctl(iscan->dev, WLC_SCAN, &g_specific_ssid,
			    sizeof(g_specific_ssid));

	rtnl_unlock();
}

static void wl_iw_send_scan_complete(iscan_info_t *iscan)
{
#ifndef SANDGATE2G
	union iwreq_data wrqu;

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

	wireless_send_event(iscan->dev, SIOCGIWSCAN, &wrqu, NULL);
	WL_TRACE("Send Event ISCAN complete\n");
#endif
}

static int _iscan_sysioc_thread(void *data)
{
	u32 status;
	iscan_info_t *iscan = (iscan_info_t *) data;
	static bool iscan_pass_abort = false;

	allow_signal(SIGTERM);
	status = WL_SCAN_RESULTS_PARTIAL;
	while (down_interruptible(&iscan->sysioc_sem) == 0) {
		if (kthread_should_stop())
			break;

		if (iscan->timer_on) {
			del_timer_sync(&iscan->timer);
			iscan->timer_on = 0;
		}
		rtnl_lock();
		status = wl_iw_iscan_get(iscan);
		rtnl_unlock();
		if (g_scan_specified_ssid && (iscan_pass_abort == true)) {
			WL_TRACE("%s Get results from specific scan status = %d\n",
				 __func__, status);
			wl_iw_send_scan_complete(iscan);
			iscan_pass_abort = false;
			status = -1;
		}

		switch (status) {
		case WL_SCAN_RESULTS_PARTIAL:
			WL_TRACE("iscanresults incomplete\n");
			rtnl_lock();
			wl_iw_iscan(iscan, NULL, WL_SCAN_ACTION_CONTINUE);
			rtnl_unlock();
			mod_timer(&iscan->timer,
				  jiffies + iscan->timer_ms * HZ / 1000);
			iscan->timer_on = 1;
			break;
		case WL_SCAN_RESULTS_SUCCESS:
			WL_TRACE("iscanresults complete\n");
			iscan->iscan_state = ISCAN_STATE_IDLE;
			wl_iw_send_scan_complete(iscan);
			break;
		case WL_SCAN_RESULTS_PENDING:
			WL_TRACE("iscanresults pending\n");
			mod_timer(&iscan->timer,
				  jiffies + iscan->timer_ms * HZ / 1000);
			iscan->timer_on = 1;
			break;
		case WL_SCAN_RESULTS_ABORTED:
			WL_TRACE("iscanresults aborted\n");
			iscan->iscan_state = ISCAN_STATE_IDLE;
			if (g_scan_specified_ssid == 0)
				wl_iw_send_scan_complete(iscan);
			else {
				iscan_pass_abort = true;
				wl_iw_force_specific_scan(iscan);
			}
			break;
		case WL_SCAN_RESULTS_NO_MEM:
			WL_TRACE("iscanresults can't alloc memory: skip\n");
			iscan->iscan_state = ISCAN_STATE_IDLE;
			break;
		default:
			WL_TRACE("iscanresults returned unknown status %d\n",
				 status);
			break;
		}
	}

	if (iscan->timer_on) {
		del_timer_sync(&iscan->timer);
		iscan->timer_on = 0;
	}
	return 0;
}
#endif				/* WL_IW_USE_ISCAN */

static int
wl_iw_set_scan(struct net_device *dev,
	       struct iw_request_info *info,
	       union iwreq_data *wrqu, char *extra)
{
	int error;
	WL_TRACE("\n:%s dev:%s: SIOCSIWSCAN : SCAN\n", __func__, dev->name);

	g_set_essid_before_scan = false;
#if defined(CSCAN)
	WL_ERROR("%s: Scan from SIOCGIWSCAN not supported\n", __func__);
	return -EINVAL;
#endif

	if (g_onoff == G_WLAN_SET_OFF)
		return 0;

	memset(&g_specific_ssid, 0, sizeof(g_specific_ssid));
#ifndef WL_IW_USE_ISCAN
	g_scan_specified_ssid = 0;
#endif

#if WIRELESS_EXT > 17
	if (wrqu->data.length == sizeof(struct iw_scan_req)) {
		if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
			struct iw_scan_req *req = (struct iw_scan_req *)extra;
			if (g_scan_specified_ssid) {
				WL_TRACE("%s Specific SCAN is not done ignore scan for = %s\n",
					 __func__, req->essid);
				return -EBUSY;
			} else {
				g_specific_ssid.SSID_len = min_t(size_t,
						sizeof(g_specific_ssid.SSID),
						req->essid_len);
				memcpy(g_specific_ssid.SSID, req->essid,
				       g_specific_ssid.SSID_len);
				g_specific_ssid.SSID_len =
				    cpu_to_le32(g_specific_ssid.SSID_len);
				g_scan_specified_ssid = 1;
				WL_TRACE("### Specific scan ssid=%s len=%d\n",
					 g_specific_ssid.SSID,
					 g_specific_ssid.SSID_len);
			}
		}
	}
#endif				/* WIRELESS_EXT > 17 */
	error = dev_wlc_ioctl(dev, WLC_SCAN, &g_specific_ssid,
				sizeof(g_specific_ssid));
	if (error) {
		WL_TRACE("#### Set SCAN for %s failed with %d\n",
			 g_specific_ssid.SSID, error);
		g_scan_specified_ssid = 0;
		return -EBUSY;
	}

	return 0;
}

#ifdef WL_IW_USE_ISCAN
int wl_iw_iscan_set_scan_broadcast_prep(struct net_device *dev, uint flag)
{
	wlc_ssid_t ssid;
	iscan_info_t *iscan = g_iscan;

	if (flag)
		rtnl_lock();

	wl_iw_set_event_mask(dev);

	WL_TRACE("+++: Set Broadcast ISCAN\n");
	memset(&ssid, 0, sizeof(ssid));

	iscan->list_cur = iscan->list_hdr;
	iscan->iscan_state = ISCAN_STATE_SCANING;

	memset(&iscan->iscan_ex_params_p->params, 0,
	       iscan->iscan_ex_param_size);
	wl_iw_iscan_prep(&iscan->iscan_ex_params_p->params, &ssid);
	wl_iw_iscan(iscan, &ssid, WL_SCAN_ACTION_START);

	if (flag)
		rtnl_unlock();

	mod_timer(&iscan->timer, jiffies + iscan->timer_ms * HZ / 1000);

	iscan->timer_on = 1;

	return 0;
}

static int
wl_iw_iscan_set_scan(struct net_device *dev,
		     struct iw_request_info *info,
		     union iwreq_data *wrqu, char *extra)
{
	wlc_ssid_t ssid;
	iscan_info_t *iscan = g_iscan;

	WL_TRACE("%s: SIOCSIWSCAN : ISCAN\n", dev->name);

#if defined(CSCAN)
	WL_ERROR("%s: Scan from SIOCGIWSCAN not supported\n", __func__);
	return -EINVAL;
#endif

	if (g_onoff == G_WLAN_SET_OFF) {
		WL_TRACE("%s: driver is not up yet after START\n", __func__);
		return 0;
	}
#ifdef PNO_SUPPORT
	if (dhd_dev_get_pno_status(dev)) {
		WL_ERROR("%s: Scan called when PNO is active\n", __func__);
	}
#endif

	if ((!iscan) || (!iscan->sysioc_tsk))
		return wl_iw_set_scan(dev, info, wrqu, extra);

	if (g_scan_specified_ssid) {
		WL_TRACE("%s Specific SCAN already running ignoring BC scan\n",
			 __func__);
		return -EBUSY;
	}

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

#if WIRELESS_EXT > 17
	if (wrqu->data.length == sizeof(struct iw_scan_req)) {
		if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
			struct iw_scan_req *req = (struct iw_scan_req *)extra;
			ssid.SSID_len = min_t(size_t, sizeof(ssid.SSID),
						req->essid_len);
			memcpy(ssid.SSID, req->essid, ssid.SSID_len);
			ssid.SSID_len = cpu_to_le32(ssid.SSID_len);
		} else {
			g_scan_specified_ssid = 0;

			if (iscan->iscan_state == ISCAN_STATE_SCANING) {
				WL_TRACE("%s ISCAN already in progress\n",
					 __func__);
				return 0;
			}
		}
	}
#endif				/* WIRELESS_EXT > 17 */
	wl_iw_iscan_set_scan_broadcast_prep(dev, 0);

	return 0;
}
#endif				/* WL_IW_USE_ISCAN */

#if WIRELESS_EXT > 17
static bool ie_is_wpa_ie(u8 **wpaie, u8 **tlvs, int *tlvs_len)
{

	u8 *ie = *wpaie;

	if ((ie[1] >= 6) &&
	    !memcmp((const void *)&ie[2], (const void *)(WPA_OUI "\x01"), 4)) {
		return true;
	}

	ie += ie[1] + 2;
	*tlvs_len -= (int)(ie - *tlvs);
	*tlvs = ie;
	return false;
}

static bool ie_is_wps_ie(u8 **wpsie, u8 **tlvs, int *tlvs_len)
{

	u8 *ie = *wpsie;

	if ((ie[1] >= 4) &&
	    !memcmp((const void *)&ie[2], (const void *)(WPA_OUI "\x04"), 4)) {
		return true;
	}

	ie += ie[1] + 2;
	*tlvs_len -= (int)(ie - *tlvs);
	*tlvs = ie;
	return false;
}
#endif				/* WIRELESS_EXT > 17 */

static int
wl_iw_handle_scanresults_ies(char **event_p, char *end,
			     struct iw_request_info *info, wl_bss_info_t *bi)
{
#if WIRELESS_EXT > 17
	struct iw_event iwe;
	char *event;

	event = *event_p;
	if (bi->ie_length) {
		bcm_tlv_t *ie;
		u8 *ptr = ((u8 *) bi) + sizeof(wl_bss_info_t);
		int ptr_len = bi->ie_length;

		ie = bcm_parse_tlvs(ptr, ptr_len, DOT11_MNG_RSN_ID);
		if (ie) {
			iwe.cmd = IWEVGENIE;
			iwe.u.data.length = ie->len + 2;
			event =
			    IWE_STREAM_ADD_POINT(info, event, end, &iwe,
						 (char *)ie);
		}
		ptr = ((u8 *) bi) + sizeof(wl_bss_info_t);

		while ((ie = bcm_parse_tlvs(ptr, ptr_len, DOT11_MNG_WPA_ID))) {
			if (ie_is_wps_ie(((u8 **)&ie), &ptr, &ptr_len)) {
				iwe.cmd = IWEVGENIE;
				iwe.u.data.length = ie->len + 2;
				event =
				    IWE_STREAM_ADD_POINT(info, event, end, &iwe,
							 (char *)ie);
				break;
			}
		}

		ptr = ((u8 *) bi) + sizeof(wl_bss_info_t);
		ptr_len = bi->ie_length;
		while ((ie = bcm_parse_tlvs(ptr, ptr_len, DOT11_MNG_WPA_ID))) {
			if (ie_is_wpa_ie(((u8 **)&ie), &ptr, &ptr_len)) {
				iwe.cmd = IWEVGENIE;
				iwe.u.data.length = ie->len + 2;
				event =
				    IWE_STREAM_ADD_POINT(info, event, end, &iwe,
							 (char *)ie);
				break;
			}
		}

		*event_p = event;
	}
#endif		/* WIRELESS_EXT > 17 */
	return 0;
}

static uint
wl_iw_get_scan_prep(wl_scan_results_t *list,
		    struct iw_request_info *info, char *extra, short max_size)
{
	int i, j;
	struct iw_event iwe;
	wl_bss_info_t *bi = NULL;
	char *event = extra, *end = extra + max_size - WE_ADD_EVENT_FIX, *value;
	int ret = 0;

	ASSERT(list);

	for (i = 0; i < list->count && i < IW_MAX_AP; i++) {
		if (list->version != WL_BSS_INFO_VERSION) {
			WL_ERROR("%s : list->version %d != WL_BSS_INFO_VERSION\n",
				 __func__, list->version);
			return ret;
		}

		bi = bi ? (wl_bss_info_t *)((unsigned long)bi +
					     le32_to_cpu(bi->length)) : list->
		    bss_info;

		WL_TRACE("%s : %s\n", __func__, bi->SSID);

		iwe.cmd = SIOCGIWAP;
		iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
		memcpy(iwe.u.ap_addr.sa_data, &bi->BSSID, ETH_ALEN);
		event =
		    IWE_STREAM_ADD_EVENT(info, event, end, &iwe,
					 IW_EV_ADDR_LEN);
		iwe.u.data.length = le32_to_cpu(bi->SSID_len);
		iwe.cmd = SIOCGIWESSID;
		iwe.u.data.flags = 1;
		event = IWE_STREAM_ADD_POINT(info, event, end, &iwe, bi->SSID);

		if (le16_to_cpu(bi->capability) & (WLAN_CAPABILITY_ESS |
		    WLAN_CAPABILITY_IBSS)) {
			iwe.cmd = SIOCGIWMODE;
			if (le16_to_cpu(bi->capability) & WLAN_CAPABILITY_ESS)
				iwe.u.mode = IW_MODE_INFRA;
			else
				iwe.u.mode = IW_MODE_ADHOC;
			event =
			    IWE_STREAM_ADD_EVENT(info, event, end, &iwe,
						 IW_EV_UINT_LEN);
		}

		iwe.cmd = SIOCGIWFREQ;

		if (CHSPEC_CHANNEL(bi->chanspec) <= CH_MAX_2G_CHANNEL)
			iwe.u.freq.m = ieee80211_dsss_chan_to_freq(
						CHSPEC_CHANNEL(bi->chanspec));
		else
			iwe.u.freq.m = ieee80211_ofdm_chan_to_freq(
						WF_CHAN_FACTOR_5_G/2,
						CHSPEC_CHANNEL(bi->chanspec));

		iwe.u.freq.e = 6;
		event =
		    IWE_STREAM_ADD_EVENT(info, event, end, &iwe,
					 IW_EV_FREQ_LEN);

		iwe.cmd = IWEVQUAL;
		iwe.u.qual.qual = rssi_to_qual(le16_to_cpu(bi->RSSI));
		iwe.u.qual.level = 0x100 + le16_to_cpu(bi->RSSI);
		iwe.u.qual.noise = 0x100 + bi->phy_noise;
		event =
		    IWE_STREAM_ADD_EVENT(info, event, end, &iwe,
					 IW_EV_QUAL_LEN);

		wl_iw_handle_scanresults_ies(&event, end, info, bi);

		iwe.cmd = SIOCGIWENCODE;
		if (le16_to_cpu(bi->capability) & WLAN_CAPABILITY_PRIVACY)
			iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
		else
			iwe.u.data.flags = IW_ENCODE_DISABLED;
		iwe.u.data.length = 0;
		event =
		    IWE_STREAM_ADD_POINT(info, event, end, &iwe, (char *)event);

		if (bi->rateset.count) {
			if (((event - extra) +
				IW_EV_LCP_LEN) <= (unsigned long)end) {
				value = event + IW_EV_LCP_LEN;
				iwe.cmd = SIOCGIWRATE;
				iwe.u.bitrate.fixed = iwe.u.bitrate.disabled =
				    0;
				for (j = 0;
				     j < bi->rateset.count
				     && j < IW_MAX_BITRATES; j++) {
					iwe.u.bitrate.value =
					    (bi->rateset.rates[j] & 0x7f) *
					    500000;
					value =
					    IWE_STREAM_ADD_VALUE(info, event,
						 value, end, &iwe,
						 IW_EV_PARAM_LEN);
				}
				event = value;
			}
		}
	}

	ret = event - extra;
	if (ret < 0) {
		WL_ERROR("==> Wrong size\n");
		ret = 0;
	}
	WL_TRACE("%s: size=%d bytes prepared\n",
		 __func__, (unsigned int)(event - extra));
	return (uint)ret;
}

static int
wl_iw_get_scan(struct net_device *dev,
	       struct iw_request_info *info, struct iw_point *dwrq, char *extra)
{
	channel_info_t ci;
	wl_scan_results_t *list_merge;
	wl_scan_results_t *list = (wl_scan_results_t *) g_scan;
	int error;
	uint buflen_from_user = dwrq->length;
	uint len = G_SCAN_RESULTS;
	__u16 len_ret = 0;
#if defined(WL_IW_USE_ISCAN)
	iscan_info_t *iscan = g_iscan;
	iscan_buf_t *p_buf;
#endif

	WL_TRACE("%s: buflen_from_user %d:\n", dev->name, buflen_from_user);

	if (!extra) {
		WL_TRACE("%s: wl_iw_get_scan return -EINVAL\n", dev->name);
		return -EINVAL;
	}

	error = dev_wlc_ioctl(dev, WLC_GET_CHANNEL, &ci, sizeof(ci));
	if (error)
		return error;
	ci.scan_channel = le32_to_cpu(ci.scan_channel);
	if (ci.scan_channel)
		return -EAGAIN;

	if (g_scan_specified_ssid) {
		list = kmalloc(len, GFP_KERNEL);
		if (!list) {
			WL_TRACE("%s: wl_iw_get_scan return -ENOMEM\n",
				 dev->name);
			g_scan_specified_ssid = 0;
			return -ENOMEM;
		}
	}

	memset(list, 0, len);
	list->buflen = cpu_to_le32(len);
	error = dev_wlc_ioctl(dev, WLC_SCAN_RESULTS, list, len);
	if (error) {
		WL_ERROR("%s: %s : Scan_results ERROR %d\n",
			 dev->name, __func__, error);
		dwrq->length = len;
		if (g_scan_specified_ssid) {
			g_scan_specified_ssid = 0;
			kfree(list);
		}
		return 0;
	}
	list->buflen = le32_to_cpu(list->buflen);
	list->version = le32_to_cpu(list->version);
	list->count = le32_to_cpu(list->count);

	if (list->version != WL_BSS_INFO_VERSION) {
		WL_ERROR("%s : list->version %d != WL_BSS_INFO_VERSION\n",
			 __func__, list->version);
		if (g_scan_specified_ssid) {
			g_scan_specified_ssid = 0;
			kfree(list);
		}
		return -EINVAL;
	}

	if (g_scan_specified_ssid) {
		WL_TRACE("%s: Specified scan APs in the list =%d\n",
			 __func__, list->count);
		len_ret =
		    (__u16) wl_iw_get_scan_prep(list, info, extra,
						buflen_from_user);
		kfree(list);

#if defined(WL_IW_USE_ISCAN)
		p_buf = iscan->list_hdr;
		while (p_buf != iscan->list_cur) {
			list_merge =
			    &((wl_iscan_results_t *) p_buf->iscan_buf)->results;
			WL_TRACE("%s: Bcast APs list=%d\n",
				 __func__, list_merge->count);
			if (list_merge->count > 0)
				len_ret +=
				    (__u16) wl_iw_get_scan_prep(list_merge,
					info, extra + len_ret,
					buflen_from_user - len_ret);
			p_buf = p_buf->next;
		}
#else
		list_merge = (wl_scan_results_t *) g_scan;
		WL_TRACE("%s: Bcast APs list=%d\n",
			 __func__, list_merge->count);
		if (list_merge->count > 0)
			len_ret +=
			    (__u16) wl_iw_get_scan_prep(list_merge, info,
							extra + len_ret,
							buflen_from_user -
							len_ret);
#endif				/* defined(WL_IW_USE_ISCAN) */
	} else {
		list = (wl_scan_results_t *) g_scan;
		len_ret =
		    (__u16) wl_iw_get_scan_prep(list, info, extra,
						buflen_from_user);
	}

#if defined(WL_IW_USE_ISCAN)
	g_scan_specified_ssid = 0;
#endif
	if ((len_ret + WE_ADD_EVENT_FIX) < buflen_from_user)
		len = len_ret;

	dwrq->length = len;
	dwrq->flags = 0;

	WL_TRACE("%s return to WE %d bytes APs=%d\n",
		 __func__, dwrq->length, list->count);
	return 0;
}

#if defined(WL_IW_USE_ISCAN)
static int
wl_iw_iscan_get_scan(struct net_device *dev,
		     struct iw_request_info *info,
		     struct iw_point *dwrq, char *extra)
{
	wl_scan_results_t *list;
	struct iw_event iwe;
	wl_bss_info_t *bi = NULL;
	int ii, j;
	int apcnt;
	char *event = extra, *end = extra + dwrq->length, *value;
	iscan_info_t *iscan = g_iscan;
	iscan_buf_t *p_buf;
	u32 counter = 0;
	u8 channel;

	WL_TRACE("%s %s buflen_from_user %d:\n",
		 dev->name, __func__, dwrq->length);

	if (!extra) {
		WL_TRACE("%s: INVALID SIOCGIWSCAN GET bad parameter\n",
			 dev->name);
		return -EINVAL;
	}

	if ((!iscan) || (!iscan->sysioc_tsk)) {
		WL_ERROR("%ssysioc_tsk\n", __func__);
		return wl_iw_get_scan(dev, info, dwrq, extra);
	}

	if (iscan->iscan_state == ISCAN_STATE_SCANING) {
		WL_TRACE("%s: SIOCGIWSCAN GET still scanning\n", dev->name);
		return -EAGAIN;
	}

	WL_TRACE("%s: SIOCGIWSCAN GET broadcast results\n", dev->name);
	apcnt = 0;
	p_buf = iscan->list_hdr;
	while (p_buf != iscan->list_cur) {
		list = &((wl_iscan_results_t *) p_buf->iscan_buf)->results;

		counter += list->count;

		if (list->version != WL_BSS_INFO_VERSION) {
			WL_ERROR("%s : list->version %d != WL_BSS_INFO_VERSION\n",
				 __func__, list->version);
			return -EINVAL;
		}

		bi = NULL;
		for (ii = 0; ii < list->count && apcnt < IW_MAX_AP;
		     apcnt++, ii++) {
			bi = bi ? (wl_bss_info_t *)((unsigned long)bi +
						     le32_to_cpu(bi->length)) :
			    list->bss_info;
			ASSERT(((unsigned long)bi + le32_to_cpu(bi->length)) <=
			       ((unsigned long)list + WLC_IW_ISCAN_MAXLEN));

			if (event + ETH_ALEN + bi->SSID_len +
			    IW_EV_UINT_LEN + IW_EV_FREQ_LEN + IW_EV_QUAL_LEN >=
			    end)
				return -E2BIG;
			iwe.cmd = SIOCGIWAP;
			iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
			memcpy(iwe.u.ap_addr.sa_data, &bi->BSSID,
			       ETH_ALEN);
			event =
			    IWE_STREAM_ADD_EVENT(info, event, end, &iwe,
						 IW_EV_ADDR_LEN);

			iwe.u.data.length = le32_to_cpu(bi->SSID_len);
			iwe.cmd = SIOCGIWESSID;
			iwe.u.data.flags = 1;
			event =
			    IWE_STREAM_ADD_POINT(info, event, end, &iwe,
						 bi->SSID);

			if (le16_to_cpu(bi->capability) &
			    (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)) {
				iwe.cmd = SIOCGIWMODE;
				if (le16_to_cpu(bi->capability) &
				    WLAN_CAPABILITY_ESS)
					iwe.u.mode = IW_MODE_INFRA;
				else
					iwe.u.mode = IW_MODE_ADHOC;
				event =
				    IWE_STREAM_ADD_EVENT(info, event, end, &iwe,
							 IW_EV_UINT_LEN);
			}

			iwe.cmd = SIOCGIWFREQ;
			channel =
			    (bi->ctl_ch ==
			     0) ? CHSPEC_CHANNEL(bi->chanspec) : bi->ctl_ch;

			if (channel <= CH_MAX_2G_CHANNEL)
				iwe.u.freq.m =
					ieee80211_dsss_chan_to_freq(channel);
			else
				iwe.u.freq.m = ieee80211_ofdm_chan_to_freq(
							WF_CHAN_FACTOR_5_G/2,
							channel);

			iwe.u.freq.e = 6;
			event =
			    IWE_STREAM_ADD_EVENT(info, event, end, &iwe,
						 IW_EV_FREQ_LEN);

			iwe.cmd = IWEVQUAL;
			iwe.u.qual.qual = rssi_to_qual(le16_to_cpu(bi->RSSI));
			iwe.u.qual.level = 0x100 + le16_to_cpu(bi->RSSI);
			iwe.u.qual.noise = 0x100 + bi->phy_noise;
			event =
			    IWE_STREAM_ADD_EVENT(info, event, end, &iwe,
						 IW_EV_QUAL_LEN);

			wl_iw_handle_scanresults_ies(&event, end, info, bi);

			iwe.cmd = SIOCGIWENCODE;
			if (le16_to_cpu(bi->capability) &
			    WLAN_CAPABILITY_PRIVACY)
				iwe.u.data.flags =
				    IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
			else
				iwe.u.data.flags = IW_ENCODE_DISABLED;
			iwe.u.data.length = 0;
			event =
			    IWE_STREAM_ADD_POINT(info, event, end, &iwe,
						 (char *)event);

			if (bi->rateset.count) {
				if (event + IW_MAX_BITRATES * IW_EV_PARAM_LEN >=
				    end)
					return -E2BIG;

				value = event + IW_EV_LCP_LEN;
				iwe.cmd = SIOCGIWRATE;
				iwe.u.bitrate.fixed = iwe.u.bitrate.disabled =
				    0;
				for (j = 0;
				     j < bi->rateset.count
				     && j < IW_MAX_BITRATES; j++) {
					iwe.u.bitrate.value =
					    (bi->rateset.rates[j] & 0x7f) *
					    500000;
					value =
					    IWE_STREAM_ADD_VALUE(info, event,
						 value, end,
						 &iwe,
						 IW_EV_PARAM_LEN);
				}
				event = value;
			}
		}
		p_buf = p_buf->next;
	}

	dwrq->length = event - extra;
	dwrq->flags = 0;

	WL_TRACE("%s return to WE %d bytes APs=%d\n",
		 __func__, dwrq->length, counter);

	if (!dwrq->length)
		return -EAGAIN;

	return 0;
}
#endif				/* defined(WL_IW_USE_ISCAN) */

static int
wl_iw_set_essid(struct net_device *dev,
		struct iw_request_info *info,
		struct iw_point *dwrq, char *extra)
{
	int error;
	wl_join_params_t join_params;
	int join_params_size;

	WL_TRACE("%s: SIOCSIWESSID\n", dev->name);

	if (g_set_essid_before_scan)
		return -EAGAIN;

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

	CHECK_EXTRA_FOR_NULL(extra);

	if (dwrq->length && extra) {
#if WIRELESS_EXT > 20
		g_ssid.SSID_len = min_t(size_t, sizeof(g_ssid.SSID),
					dwrq->length);
#else
		g_ssid.SSID_len = min_t(size_t, sizeof(g_ssid.SSID),
					dwrq->length - 1);
#endif
		memcpy(g_ssid.SSID, extra, g_ssid.SSID_len);
	} else {
		g_ssid.SSID_len = 0;
	}
	g_ssid.SSID_len = cpu_to_le32(g_ssid.SSID_len);

	memset(&join_params, 0, sizeof(join_params));
	join_params_size = sizeof(join_params.ssid);

	memcpy(&join_params.ssid.SSID, g_ssid.SSID, g_ssid.SSID_len);
	join_params.ssid.SSID_len = cpu_to_le32(g_ssid.SSID_len);
	memcpy(join_params.params.bssid, ether_bcast, ETH_ALEN);

	wl_iw_ch_to_chanspec(g_wl_iw_params.target_channel, &join_params,
			     &join_params_size);

	error = dev_wlc_ioctl(dev, WLC_SET_SSID, &join_params,
				join_params_size);
	if (error)
		WL_ERROR("Invalid ioctl data=%d\n", error);

	if (g_ssid.SSID_len) {
		WL_TRACE("%s: join SSID=%s ch=%d\n",
			 __func__, g_ssid.SSID, g_wl_iw_params.target_channel);
	}
	return 0;
}

static int
wl_iw_get_essid(struct net_device *dev,
		struct iw_request_info *info,
		struct iw_point *dwrq, char *extra)
{
	wlc_ssid_t ssid;
	int error;

	WL_TRACE("%s: SIOCGIWESSID\n", dev->name);

	if (!extra)
		return -EINVAL;

	error = dev_wlc_ioctl(dev, WLC_GET_SSID, &ssid, sizeof(ssid));
	if (error) {
		WL_ERROR("Error getting the SSID\n");
		return error;
	}

	ssid.SSID_len = le32_to_cpu(ssid.SSID_len);

	memcpy(extra, ssid.SSID, ssid.SSID_len);

	dwrq->length = ssid.SSID_len;

	dwrq->flags = 1;

	return 0;
}

static int
wl_iw_set_nick(struct net_device *dev,
	       struct iw_request_info *info, struct iw_point *dwrq, char *extra)
{
	wl_iw_t *iw = *(wl_iw_t **) netdev_priv(dev);

	WL_TRACE("%s: SIOCSIWNICKN\n", dev->name);

	if (!extra)
		return -EINVAL;

	if (dwrq->length > sizeof(iw->nickname))
		return -E2BIG;

	memcpy(iw->nickname, extra, dwrq->length);
	iw->nickname[dwrq->length - 1] = '\0';

	return 0;
}

static int
wl_iw_get_nick(struct net_device *dev,
	       struct iw_request_info *info, struct iw_point *dwrq, char *extra)
{
	wl_iw_t *iw = *(wl_iw_t **) netdev_priv(dev);

	WL_TRACE("%s: SIOCGIWNICKN\n", dev->name);

	if (!extra)
		return -EINVAL;

	strcpy(extra, iw->nickname);
	dwrq->length = strlen(extra) + 1;

	return 0;
}

static int
wl_iw_set_rate(struct net_device *dev,
	       struct iw_request_info *info, struct iw_param *vwrq, char *extra)
{
	wl_rateset_t rateset;
	int error, rate, i, error_bg, error_a;

	WL_TRACE("%s: SIOCSIWRATE\n", dev->name);

	error = dev_wlc_ioctl(dev, WLC_GET_CURR_RATESET, &rateset,
				sizeof(rateset));
	if (error)
		return error;

	rateset.count = le32_to_cpu(rateset.count);

	if (vwrq->value < 0)
		rate = rateset.rates[rateset.count - 1] & 0x7f;
	else if (vwrq->value < rateset.count)
		rate = rateset.rates[vwrq->value] & 0x7f;
	else
		rate = vwrq->value / 500000;

	if (vwrq->fixed) {
		error_bg = dev_wlc_intvar_set(dev, "bg_rate", rate);
		error_a = dev_wlc_intvar_set(dev, "a_rate", rate);

		if (error_bg && error_a)
			return error_bg | error_a;
	} else {
		error_bg = dev_wlc_intvar_set(dev, "bg_rate", 0);
		error_a = dev_wlc_intvar_set(dev, "a_rate", 0);

		if (error_bg && error_a)
			return error_bg | error_a;

		for (i = 0; i < rateset.count; i++)
			if ((rateset.rates[i] & 0x7f) > rate)
				break;
		rateset.count = cpu_to_le32(i);

		error = dev_wlc_ioctl(dev, WLC_SET_RATESET, &rateset,
					sizeof(rateset));
		if (error)
			return error;
	}

	return 0;
}

static int
wl_iw_get_rate(struct net_device *dev,
	       struct iw_request_info *info, struct iw_param *vwrq, char *extra)
{
	int error, rate;

	WL_TRACE("%s: SIOCGIWRATE\n", dev->name);

	error = dev_wlc_ioctl(dev, WLC_GET_RATE, &rate, sizeof(rate));
	if (error)
		return error;
	rate = le32_to_cpu(rate);
	vwrq->value = rate * 500000;

	return 0;
}

static int
wl_iw_set_rts(struct net_device *dev,
	      struct iw_request_info *info, struct iw_param *vwrq, char *extra)
{
	int error, rts;

	WL_TRACE("%s: SIOCSIWRTS\n", dev->name);

	if (vwrq->disabled)
		rts = DOT11_DEFAULT_RTS_LEN;
	else if (vwrq->value < 0 || vwrq->value > DOT11_DEFAULT_RTS_LEN)
		return -EINVAL;
	else
		rts = vwrq->value;

	error = dev_wlc_intvar_set(dev, "rtsthresh", rts);
	if (error)
		return error;

	return 0;
}

static int
wl_iw_get_rts(struct net_device *dev,
	      struct iw_request_info *info, struct iw_param *vwrq, char *extra)
{
	int error, rts;

	WL_TRACE("%s: SIOCGIWRTS\n", dev->name);

	error = dev_wlc_intvar_get(dev, "rtsthresh", &rts);
	if (error)
		return error;

	vwrq->value = rts;
	vwrq->disabled = (rts >= DOT11_DEFAULT_RTS_LEN);
	vwrq->fixed = 1;

	return 0;
}

static int
wl_iw_set_frag(struct net_device *dev,
	       struct iw_request_info *info, struct iw_param *vwrq, char *extra)
{
	int error, frag;

	WL_TRACE("%s: SIOCSIWFRAG\n", dev->name);

	if (vwrq->disabled)
		frag = DOT11_DEFAULT_FRAG_LEN;
	else if (vwrq->value < 0 || vwrq->value > DOT11_DEFAULT_FRAG_LEN)
		return -EINVAL;
	else
		frag = vwrq->value;

	error = dev_wlc_intvar_set(dev, "fragthresh", frag);
	if (error)
		return error;

	return 0;
}

static int
wl_iw_get_frag(struct net_device *dev,
	       struct iw_request_info *info, struct iw_param *vwrq, char *extra)
{
	int error, fragthreshold;

	WL_TRACE("%s: SIOCGIWFRAG\n", dev->name);

	error = dev_wlc_intvar_get(dev, "fragthresh", &fragthreshold);
	if (error)
		return error;

	vwrq->value = fragthreshold;
	vwrq->disabled = (fragthreshold >= DOT11_DEFAULT_FRAG_LEN);
	vwrq->fixed = 1;

	return 0;
}

static int
wl_iw_set_txpow(struct net_device *dev,
		struct iw_request_info *info,
		struct iw_param *vwrq, char *extra)
{
	int error, disable;
	u16 txpwrmw;
	WL_TRACE("%s: SIOCSIWTXPOW\n", dev->name);

	disable = vwrq->disabled ? WL_RADIO_SW_DISABLE : 0;
	disable += WL_RADIO_SW_DISABLE << 16;

	disable = cpu_to_le32(disable);
	error = dev_wlc_ioctl(dev, WLC_SET_RADIO, &disable, sizeof(disable));
	if (error)
		return error;

	if (disable & WL_RADIO_SW_DISABLE)
		return 0;

	if (!(vwrq->flags & IW_TXPOW_MWATT))
		return -EINVAL;

	if (vwrq->value < 0)
		return 0;

	if (vwrq->value > 0xffff)
		txpwrmw = 0xffff;
	else
		txpwrmw = (u16) vwrq->value;

	error =
	    dev_wlc_intvar_set(dev, "qtxpower", (int)(bcm_mw_to_qdbm(txpwrmw)));
	return error;
}

static int
wl_iw_get_txpow(struct net_device *dev,
		struct iw_request_info *info,
		struct iw_param *vwrq, char *extra)
{
	int error, disable, txpwrdbm;
	u8 result;

	WL_TRACE("%s: SIOCGIWTXPOW\n", dev->name);

	error = dev_wlc_ioctl(dev, WLC_GET_RADIO, &disable, sizeof(disable));
	if (error)
		return error;

	error = dev_wlc_intvar_get(dev, "qtxpower", &txpwrdbm);
	if (error)
		return error;

	disable = le32_to_cpu(disable);
	result = (u8) (txpwrdbm & ~WL_TXPWR_OVERRIDE);
	vwrq->value = (s32) bcm_qdbm_to_mw(result);
	vwrq->fixed = 0;
	vwrq->disabled =
	    (disable & (WL_RADIO_SW_DISABLE | WL_RADIO_HW_DISABLE)) ? 1 : 0;
	vwrq->flags = IW_TXPOW_MWATT;

	return 0;
}

#if WIRELESS_EXT > 10
static int
wl_iw_set_retry(struct net_device *dev,
		struct iw_request_info *info,
		struct iw_param *vwrq, char *extra)
{
	int error, lrl, srl;

	WL_TRACE("%s: SIOCSIWRETRY\n", dev->name);

	if (vwrq->disabled || (vwrq->flags & IW_RETRY_LIFETIME))
		return -EINVAL;

	if (vwrq->flags & IW_RETRY_LIMIT) {

#if WIRELESS_EXT > 20
		if ((vwrq->flags & IW_RETRY_LONG)
		    || (vwrq->flags & IW_RETRY_MAX)
		    || !((vwrq->flags & IW_RETRY_SHORT)
			 || (vwrq->flags & IW_RETRY_MIN))) {
#else
		if ((vwrq->flags & IW_RETRY_MAX)
		    || !(vwrq->flags & IW_RETRY_MIN)) {
#endif
			lrl = cpu_to_le32(vwrq->value);
			error = dev_wlc_ioctl(dev, WLC_SET_LRL, &lrl,
						sizeof(lrl));
			if (error)
				return error;
		}
#if WIRELESS_EXT > 20
		if ((vwrq->flags & IW_RETRY_SHORT)
		    || (vwrq->flags & IW_RETRY_MIN)
		    || !((vwrq->flags & IW_RETRY_LONG)
			 || (vwrq->flags & IW_RETRY_MAX))) {
#else
		if ((vwrq->flags & IW_RETRY_MIN)
		    || !(vwrq->flags & IW_RETRY_MAX)) {
#endif
			srl = cpu_to_le32(vwrq->value);
			error = dev_wlc_ioctl(dev, WLC_SET_SRL, &srl,
						sizeof(srl));
			if (error)
				return error;
		}
	}
	return 0;
}

static int
wl_iw_get_retry(struct net_device *dev,
		struct iw_request_info *info,
		struct iw_param *vwrq, char *extra)
{
	int error, lrl, srl;

	WL_TRACE("%s: SIOCGIWRETRY\n", dev->name);

	vwrq->disabled = 0;

	if ((vwrq->flags & IW_RETRY_TYPE) == IW_RETRY_LIFETIME)
		return -EINVAL;

	error = dev_wlc_ioctl(dev, WLC_GET_LRL, &lrl, sizeof(lrl));
	if (error)
		return error;

	error = dev_wlc_ioctl(dev, WLC_GET_SRL, &srl, sizeof(srl));
	if (error)
		return error;

	lrl = le32_to_cpu(lrl);
	srl = le32_to_cpu(srl);

	if (vwrq->flags & IW_RETRY_MAX) {
		vwrq->flags = IW_RETRY_LIMIT | IW_RETRY_MAX;
		vwrq->value = lrl;
	} else {
		vwrq->flags = IW_RETRY_LIMIT;
		vwrq->value = srl;
		if (srl != lrl)
			vwrq->flags |= IW_RETRY_MIN;
	}

	return 0;
}
#endif				/* WIRELESS_EXT > 10 */

static int
wl_iw_set_encode(struct net_device *dev,
		 struct iw_request_info *info,
		 struct iw_point *dwrq, char *extra)
{
	wl_wsec_key_t key;
	int error, val, wsec;

	WL_TRACE("%s: SIOCSIWENCODE\n", dev->name);

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

	if ((dwrq->flags & IW_ENCODE_INDEX) == 0) {
		for (key.index = 0; key.index < DOT11_MAX_DEFAULT_KEYS;
		     key.index++) {
			val = cpu_to_le32(key.index);
			error = dev_wlc_ioctl(dev, WLC_GET_KEY_PRIMARY, &val,
						sizeof(val));
			if (error)
				return error;
			val = le32_to_cpu(val);
			if (val)
				break;
		}
		if (key.index == DOT11_MAX_DEFAULT_KEYS)
			key.index = 0;
	} else {
		key.index = (dwrq->flags & IW_ENCODE_INDEX) - 1;
		if (key.index >= DOT11_MAX_DEFAULT_KEYS)
			return -EINVAL;
	}

	if (!extra || !dwrq->length || (dwrq->flags & IW_ENCODE_NOKEY)) {
		val = cpu_to_le32(key.index);
		error = dev_wlc_ioctl(dev, WLC_SET_KEY_PRIMARY, &val,
					sizeof(val));
		if (error)
			return error;
	} else {
		key.len = dwrq->length;

		if (dwrq->length > sizeof(key.data))
			return -EINVAL;

		memcpy(key.data, extra, dwrq->length);

		key.flags = WL_PRIMARY_KEY;
		switch (key.len) {
		case WLAN_KEY_LEN_WEP40:
			key.algo = CRYPTO_ALGO_WEP1;
			break;
		case WLAN_KEY_LEN_WEP104:
			key.algo = CRYPTO_ALGO_WEP128;
			break;
		case WLAN_KEY_LEN_TKIP:
			key.algo = CRYPTO_ALGO_TKIP;
			break;
		case WLAN_KEY_LEN_AES_CMAC:
			key.algo = CRYPTO_ALGO_AES_CCM;
			break;
		default:
			return -EINVAL;
		}

		swap_key_from_BE(&key);
		error = dev_wlc_ioctl(dev, WLC_SET_KEY, &key, sizeof(key));
		if (error)
			return error;
	}

	val = (dwrq->flags & IW_ENCODE_DISABLED) ? 0 : WEP_ENABLED;

	error = dev_wlc_intvar_get(dev, "wsec", &wsec);
	if (error)
		return error;

	wsec &= ~(WEP_ENABLED);
	wsec |= val;

	error = dev_wlc_intvar_set(dev, "wsec", wsec);
	if (error)
		return error;

	val = (dwrq->flags & IW_ENCODE_RESTRICTED) ? 1 : 0;
	val = cpu_to_le32(val);
	error = dev_wlc_ioctl(dev, WLC_SET_AUTH, &val, sizeof(val));
	if (error)
		return error;

	return 0;
}

static int
wl_iw_get_encode(struct net_device *dev,
		 struct iw_request_info *info,
		 struct iw_point *dwrq, char *extra)
{
	wl_wsec_key_t key;
	int error, val, wsec, auth;

	WL_TRACE("%s: SIOCGIWENCODE\n", dev->name);

	memset(&key, 0, sizeof(wl_wsec_key_t));

	if ((dwrq->flags & IW_ENCODE_INDEX) == 0) {
		for (key.index = 0; key.index < DOT11_MAX_DEFAULT_KEYS;
		     key.index++) {
			val = key.index;
			error = dev_wlc_ioctl(dev, WLC_GET_KEY_PRIMARY, &val,
						sizeof(val));
			if (error)
				return error;
			val = le32_to_cpu(val);
			if (val)
				break;
		}
	} else
		key.index = (dwrq->flags & IW_ENCODE_INDEX) - 1;

	if (key.index >= DOT11_MAX_DEFAULT_KEYS)
		key.index = 0;

	error = dev_wlc_ioctl(dev, WLC_GET_WSEC, &wsec, sizeof(wsec));
	if (error)
		return error;

	error = dev_wlc_ioctl(dev, WLC_GET_AUTH, &auth, sizeof(auth));
	if (error)
		return error;

	swap_key_to_BE(&key);

	wsec = le32_to_cpu(wsec);
	auth = le32_to_cpu(auth);
	dwrq->length = min_t(u16, WLAN_MAX_KEY_LEN, key.len);

	dwrq->flags = key.index + 1;
	if (!(wsec & (WEP_ENABLED | TKIP_ENABLED | AES_ENABLED)))
		dwrq->flags |= IW_ENCODE_DISABLED;

	if (auth)
		dwrq->flags |= IW_ENCODE_RESTRICTED;

	if (dwrq->length && extra)
		memcpy(extra, key.data, dwrq->length);

	return 0;
}

static int
wl_iw_set_power(struct net_device *dev,
		struct iw_request_info *info,
		struct iw_param *vwrq, char *extra)
{
	int error, pm;

	WL_TRACE("%s: SIOCSIWPOWER\n", dev->name);

	pm = vwrq->disabled ? PM_OFF : PM_MAX;

	pm = cpu_to_le32(pm);
	error = dev_wlc_ioctl(dev, WLC_SET_PM, &pm, sizeof(pm));
	if (error)
		return error;

	return 0;
}

static int
wl_iw_get_power(struct net_device *dev,
		struct iw_request_info *info,
		struct iw_param *vwrq, char *extra)
{
	int error, pm;

	WL_TRACE("%s: SIOCGIWPOWER\n", dev->name);

	error = dev_wlc_ioctl(dev, WLC_GET_PM, &pm, sizeof(pm));
	if (error)
		return error;

	pm = le32_to_cpu(pm);
	vwrq->disabled = pm ? 0 : 1;
	vwrq->flags = IW_POWER_ALL_R;

	return 0;
}

#if WIRELESS_EXT > 17
static int
wl_iw_set_wpaie(struct net_device *dev,
		struct iw_request_info *info, struct iw_point *iwp, char *extra)
{

	WL_TRACE("%s: SIOCSIWGENIE\n", dev->name);

	CHECK_EXTRA_FOR_NULL(extra);

	dev_wlc_bufvar_set(dev, "wpaie", extra, iwp->length);

	return 0;
}

static int
wl_iw_get_wpaie(struct net_device *dev,
		struct iw_request_info *info, struct iw_point *iwp, char *extra)
{
	WL_TRACE("%s: SIOCGIWGENIE\n", dev->name);
	iwp->length = 64;
	dev_wlc_bufvar_get(dev, "wpaie", extra, iwp->length);
	return 0;
}

static int
wl_iw_set_encodeext(struct net_device *dev,
		    struct iw_request_info *info,
		    struct iw_point *dwrq, char *extra)
{
	wl_wsec_key_t key;
	int error;
	struct iw_encode_ext *iwe;

	WL_TRACE("%s: SIOCSIWENCODEEXT\n", dev->name);

	CHECK_EXTRA_FOR_NULL(extra);

	memset(&key, 0, sizeof(key));
	iwe = (struct iw_encode_ext *)extra;

	if (dwrq->flags & IW_ENCODE_DISABLED) {

	}

	key.index = 0;
	if (dwrq->flags & IW_ENCODE_INDEX)
		key.index = (dwrq->flags & IW_ENCODE_INDEX) - 1;

	key.len = iwe->key_len;

	if (!is_multicast_ether_addr(iwe->addr.sa_data))
		memcpy(&key.ea, &iwe->addr.sa_data, ETH_ALEN);

	if (key.len == 0) {
		if (iwe->ext_flags & IW_ENCODE_EXT_SET_TX_KEY) {
			WL_WSEC("Changing the the primary Key to %d\n",
				key.index);
			key.index = cpu_to_le32(key.index);
			error = dev_wlc_ioctl(dev, WLC_SET_KEY_PRIMARY,
					      &key.index, sizeof(key.index));
			if (error)
				return error;
		} else {
			swap_key_from_BE(&key);
			dev_wlc_ioctl(dev, WLC_SET_KEY, &key, sizeof(key));
		}
	} else {
		if (iwe->key_len > sizeof(key.data))
			return -EINVAL;

		WL_WSEC("Setting the key index %d\n", key.index);
		if (iwe->ext_flags & IW_ENCODE_EXT_SET_TX_KEY) {
			WL_WSEC("key is a Primary Key\n");
			key.flags = WL_PRIMARY_KEY;
		}

		memcpy(key.data, iwe->key, iwe->key_len);

		if (iwe->alg == IW_ENCODE_ALG_TKIP) {
			u8 keybuf[8];
			memcpy(keybuf, &key.data[24], sizeof(keybuf));
			memcpy(&key.data[24], &key.data[16], sizeof(keybuf));
			memcpy(&key.data[16], keybuf, sizeof(keybuf));
		}

		if (iwe->ext_flags & IW_ENCODE_EXT_RX_SEQ_VALID) {
			unsigned char *ivptr;
			ivptr = (unsigned char *) iwe->rx_seq;
			key.rxiv.hi = (ivptr[5] << 24) | (ivptr[4] << 16) |
			    (ivptr[3] << 8) | ivptr[2];
			key.rxiv.lo = (ivptr[1] << 8) | ivptr[0];
			key.iv_initialized = true;
		}

		switch (iwe->alg) {
		case IW_ENCODE_ALG_NONE:
			key.algo = CRYPTO_ALGO_OFF;
			break;
		case IW_ENCODE_ALG_WEP:
			if (iwe->key_len == WLAN_KEY_LEN_WEP40)
				key.algo = CRYPTO_ALGO_WEP1;
			else
				key.algo = CRYPTO_ALGO_WEP128;
			break;
		case IW_ENCODE_ALG_TKIP:
			key.algo = CRYPTO_ALGO_TKIP;
			break;
		case IW_ENCODE_ALG_CCMP:
			key.algo = CRYPTO_ALGO_AES_CCM;
			break;
		default:
			break;
		}
		swap_key_from_BE(&key);

		dhd_wait_pend8021x(dev);

		error = dev_wlc_ioctl(dev, WLC_SET_KEY, &key, sizeof(key));
		if (error)
			return error;
	}
	return 0;
}

#if WIRELESS_EXT > 17
struct {
	pmkid_list_t pmkids;
	pmkid_t foo[MAXPMKID - 1];
} pmkid_list;

static int
wl_iw_set_pmksa(struct net_device *dev,
		struct iw_request_info *info,
		struct iw_param *vwrq, char *extra)
{
	struct iw_pmksa *iwpmksa;
	uint i;
	int ret = 0;

	WL_WSEC("%s: SIOCSIWPMKSA\n", dev->name);

	CHECK_EXTRA_FOR_NULL(extra);

	iwpmksa = (struct iw_pmksa *)extra;

	if (iwpmksa->cmd == IW_PMKSA_FLUSH) {
		WL_WSEC("wl_iw_set_pmksa - IW_PMKSA_FLUSH\n");
		memset((char *)&pmkid_list, 0, sizeof(pmkid_list));
	}

	else if (iwpmksa->cmd == IW_PMKSA_REMOVE) {
		{
			pmkid_list_t pmkid, *pmkidptr;
			uint j;
			pmkidptr = &pmkid;

			memcpy(&pmkidptr->pmkid[0].BSSID,
			       &iwpmksa->bssid.sa_data[0],
			       ETH_ALEN);
			memcpy(&pmkidptr->pmkid[0].PMKID,
			       &iwpmksa->pmkid[0],
			       WLAN_PMKID_LEN);

			WL_WSEC("wl_iw_set_pmksa:IW_PMKSA_REMOVE:PMKID: "
				"%pM = ", &pmkidptr->pmkid[0].BSSID);
			for (j = 0; j < WLAN_PMKID_LEN; j++)
				WL_WSEC("%02x ", pmkidptr->pmkid[0].PMKID[j]);
			WL_WSEC("\n");
		}

		for (i = 0; i < pmkid_list.pmkids.npmkid; i++)
			if (!memcmp
			    (&iwpmksa->bssid.sa_data[0],
			     &pmkid_list.pmkids.pmkid[i].BSSID, ETH_ALEN))
				break;

		if ((pmkid_list.pmkids.npmkid > 0)
		    && (i < pmkid_list.pmkids.npmkid)) {
			memset(&pmkid_list.pmkids.pmkid[i], 0, sizeof(pmkid_t));
			for (; i < (pmkid_list.pmkids.npmkid - 1); i++) {
				memcpy(&pmkid_list.pmkids.pmkid[i].BSSID,
				       &pmkid_list.pmkids.pmkid[i + 1].BSSID,
				       ETH_ALEN);
				memcpy(&pmkid_list.pmkids.pmkid[i].PMKID,
				       &pmkid_list.pmkids.pmkid[i + 1].PMKID,
				       WLAN_PMKID_LEN);
			}
			pmkid_list.pmkids.npmkid--;
		} else
			ret = -EINVAL;
	}

	else if (iwpmksa->cmd == IW_PMKSA_ADD) {
		for (i = 0; i < pmkid_list.pmkids.npmkid; i++)
			if (!memcmp
			    (&iwpmksa->bssid.sa_data[0],
			     &pmkid_list.pmkids.pmkid[i].BSSID, ETH_ALEN))
				break;
		if (i < MAXPMKID) {
			memcpy(&pmkid_list.pmkids.pmkid[i].BSSID,
			       &iwpmksa->bssid.sa_data[0],
			       ETH_ALEN);
			memcpy(&pmkid_list.pmkids.pmkid[i].PMKID,
			       &iwpmksa->pmkid[0],
			       WLAN_PMKID_LEN);
			if (i == pmkid_list.pmkids.npmkid)
				pmkid_list.pmkids.npmkid++;
		} else
			ret = -EINVAL;
		{
			uint j;
			uint k;
			k = pmkid_list.pmkids.npmkid;
			WL_WSEC("wl_iw_set_pmksa,IW_PMKSA_ADD - PMKID: %pM = ",
				&pmkid_list.pmkids.pmkid[k].BSSID);
			for (j = 0; j < WLAN_PMKID_LEN; j++)
				WL_WSEC("%02x ",
					pmkid_list.pmkids.pmkid[k].PMKID[j]);
			WL_WSEC("\n");
		}
	}
	WL_WSEC("PRINTING pmkid LIST - No of elements %d\n",
		pmkid_list.pmkids.npmkid);
	for (i = 0; i < pmkid_list.pmkids.npmkid; i++) {
		uint j;
		WL_WSEC("PMKID[%d]: %pM = ",
			i, &pmkid_list.pmkids.pmkid[i].BSSID);
		for (j = 0; j < WLAN_PMKID_LEN; j++)
			WL_WSEC("%02x ", pmkid_list.pmkids.pmkid[i].PMKID[j]);
		WL_WSEC("\n");
	}
	WL_WSEC("\n");

	if (!ret)
		ret = dev_wlc_bufvar_set(dev, "pmkid_info", (char *)&pmkid_list,
					 sizeof(pmkid_list));
	return ret;
}
#endif				/* WIRELESS_EXT > 17 */

static int
wl_iw_get_encodeext(struct net_device *dev,
		    struct iw_request_info *info,
		    struct iw_param *vwrq, char *extra)
{
	WL_TRACE("%s: SIOCGIWENCODEEXT\n", dev->name);
	return 0;
}

static int
wl_iw_set_wpaauth(struct net_device *dev,
		  struct iw_request_info *info,
		  struct iw_param *vwrq, char *extra)
{
	int error = 0;
	int paramid;
	int paramval;
	int val = 0;
	wl_iw_t *iw = *(wl_iw_t **) netdev_priv(dev);

	WL_TRACE("%s: SIOCSIWAUTH\n", dev->name);

	paramid = vwrq->flags & IW_AUTH_INDEX;
	paramval = vwrq->value;

	WL_TRACE("%s: SIOCSIWAUTH, paramid = 0x%0x, paramval = 0x%0x\n",
		 dev->name, paramid, paramval);

	switch (paramid) {
	case IW_AUTH_WPA_VERSION:
		if (paramval & IW_AUTH_WPA_VERSION_DISABLED)
			val = WPA_AUTH_DISABLED;
		else if (paramval & (IW_AUTH_WPA_VERSION_WPA))
			val = WPA_AUTH_PSK | WPA_AUTH_UNSPECIFIED;
		else if (paramval & IW_AUTH_WPA_VERSION_WPA2)
			val = WPA2_AUTH_PSK | WPA2_AUTH_UNSPECIFIED;
		WL_INFORM("%s: %d: setting wpa_auth to 0x%0x\n",
			  __func__, __LINE__, val);
		error = dev_wlc_intvar_set(dev, "wpa_auth", val);
		if (error)
			return error;
		break;
	case IW_AUTH_CIPHER_PAIRWISE:
	case IW_AUTH_CIPHER_GROUP:
		if (paramval & (IW_AUTH_CIPHER_WEP40 | IW_AUTH_CIPHER_WEP104))
			val = WEP_ENABLED;
		if (paramval & IW_AUTH_CIPHER_TKIP)
			val = TKIP_ENABLED;
		if (paramval & IW_AUTH_CIPHER_CCMP)
			val = AES_ENABLED;

		if (paramid == IW_AUTH_CIPHER_PAIRWISE) {
			iw->pwsec = val;
			val |= iw->gwsec;
		} else {
			iw->gwsec = val;
			val |= iw->pwsec;
		}

		if (iw->privacy_invoked && !val) {
			WL_WSEC("%s: %s: 'Privacy invoked' true but clearing wsec, assuming we're a WPS enrollee\n",
				dev->name, __func__);
			error = dev_wlc_intvar_set(dev, "is_WPS_enrollee",
							true);
			if (error) {
				WL_WSEC("Failed to set is_WPS_enrollee\n");
				return error;
			}
		} else if (val) {
			error = dev_wlc_intvar_set(dev, "is_WPS_enrollee",
							false);
			if (error) {
				WL_WSEC("Failed to clear is_WPS_enrollee\n");
				return error;
			}
		}

		error = dev_wlc_intvar_set(dev, "wsec", val);
		if (error)
			return error;

		break;

	case IW_AUTH_KEY_MGMT:
		error = dev_wlc_intvar_get(dev, "wpa_auth", &val);
		if (error)
			return error;

		if (val & (WPA_AUTH_PSK | WPA_AUTH_UNSPECIFIED)) {
			if (paramval & IW_AUTH_KEY_MGMT_PSK)
				val = WPA_AUTH_PSK;
			else
				val = WPA_AUTH_UNSPECIFIED;
		} else if (val & (WPA2_AUTH_PSK | WPA2_AUTH_UNSPECIFIED)) {
			if (paramval & IW_AUTH_KEY_MGMT_PSK)
				val = WPA2_AUTH_PSK;
			else
				val = WPA2_AUTH_UNSPECIFIED;
		}
		WL_INFORM("%s: %d: setting wpa_auth to %d\n",
			  __func__, __LINE__, val);
		error = dev_wlc_intvar_set(dev, "wpa_auth", val);
		if (error)
			return error;

		break;
	case IW_AUTH_TKIP_COUNTERMEASURES:
		dev_wlc_bufvar_set(dev, "tkip_countermeasures",
				   (char *)&paramval, 1);
		break;

	case IW_AUTH_80211_AUTH_ALG:
		WL_INFORM("Setting the D11auth %d\n", paramval);
		if (paramval == IW_AUTH_ALG_OPEN_SYSTEM)
			val = 0;
		else if (paramval == IW_AUTH_ALG_SHARED_KEY)
			val = 1;
		else if (paramval ==
			 (IW_AUTH_ALG_OPEN_SYSTEM | IW_AUTH_ALG_SHARED_KEY))
			val = 2;
		else
			error = 1;
		if (!error) {
			error = dev_wlc_intvar_set(dev, "auth", val);
			if (error)
				return error;
		}
		break;

	case IW_AUTH_WPA_ENABLED:
		if (paramval == 0) {
			iw->pwsec = 0;
			iw->gwsec = 0;
			error = dev_wlc_intvar_get(dev, "wsec", &val);
			if (error)
				return error;
			if (val & (TKIP_ENABLED | AES_ENABLED)) {
				val &= ~(TKIP_ENABLED | AES_ENABLED);
				dev_wlc_intvar_set(dev, "wsec", val);
			}
			val = 0;
			WL_INFORM("%s: %d: setting wpa_auth to %d\n",
				  __func__, __LINE__, val);
			dev_wlc_intvar_set(dev, "wpa_auth", 0);
			return error;
		}
		break;

	case IW_AUTH_DROP_UNENCRYPTED:
		dev_wlc_bufvar_set(dev, "wsec_restrict", (char *)&paramval, 1);
		break;

	case IW_AUTH_RX_UNENCRYPTED_EAPOL:
		dev_wlc_bufvar_set(dev, "rx_unencrypted_eapol",
				   (char *)&paramval, 1);
		break;

#if WIRELESS_EXT > 17
	case IW_AUTH_ROAMING_CONTROL:
		WL_INFORM("%s: IW_AUTH_ROAMING_CONTROL\n", __func__);
		break;
	case IW_AUTH_PRIVACY_INVOKED:
		{
			int wsec;

			if (paramval == 0) {
				iw->privacy_invoked = false;
				error = dev_wlc_intvar_set(dev,
						"is_WPS_enrollee", false);
				if (error) {
					WL_WSEC("Failed to clear iovar is_WPS_enrollee\n");
					return error;
				}
			} else {
				iw->privacy_invoked = true;
				error = dev_wlc_intvar_get(dev, "wsec", &wsec);
				if (error)
					return error;

				if (!(IW_WSEC_ENABLED(wsec))) {
					error = dev_wlc_intvar_set(dev,
							"is_WPS_enrollee",
							true);
					if (error) {
						WL_WSEC("Failed to set iovar is_WPS_enrollee\n");
						return error;
					}
				} else {
					error = dev_wlc_intvar_set(dev,
							"is_WPS_enrollee",
							false);
					if (error) {
						WL_WSEC("Failed to clear is_WPS_enrollee\n");
						return error;
					}
				}
			}
			break;
		}
#endif				/* WIRELESS_EXT > 17 */
	default:
		break;
	}
	return 0;
}

#define VAL_PSK(_val) (((_val) & WPA_AUTH_PSK) || ((_val) & WPA2_AUTH_PSK))

static int
wl_iw_get_wpaauth(struct net_device *dev,
		  struct iw_request_info *info,
		  struct iw_param *vwrq, char *extra)
{
	int error;
	int paramid;
	int paramval = 0;
	int val;
	wl_iw_t *iw = *(wl_iw_t **) netdev_priv(dev);

	WL_TRACE("%s: SIOCGIWAUTH\n", dev->name);

	paramid = vwrq->flags & IW_AUTH_INDEX;

	switch (paramid) {
	case IW_AUTH_WPA_VERSION:
		error = dev_wlc_intvar_get(dev, "wpa_auth", &val);
		if (error)
			return error;
		if (val & (WPA_AUTH_NONE | WPA_AUTH_DISABLED))
			paramval = IW_AUTH_WPA_VERSION_DISABLED;
		else if (val & (WPA_AUTH_PSK | WPA_AUTH_UNSPECIFIED))
			paramval = IW_AUTH_WPA_VERSION_WPA;
		else if (val & (WPA2_AUTH_PSK | WPA2_AUTH_UNSPECIFIED))
			paramval = IW_AUTH_WPA_VERSION_WPA2;
		break;
	case IW_AUTH_CIPHER_PAIRWISE:
	case IW_AUTH_CIPHER_GROUP:
		if (paramid == IW_AUTH_CIPHER_PAIRWISE)
			val = iw->pwsec;
		else
			val = iw->gwsec;

		paramval = 0;
		if (val) {
			if (val & WEP_ENABLED)
				paramval |=
				    (IW_AUTH_CIPHER_WEP40 |
				     IW_AUTH_CIPHER_WEP104);
			if (val & TKIP_ENABLED)
				paramval |= (IW_AUTH_CIPHER_TKIP);
			if (val & AES_ENABLED)
				paramval |= (IW_AUTH_CIPHER_CCMP);
		} else
			paramval = IW_AUTH_CIPHER_NONE;
		break;
	case IW_AUTH_KEY_MGMT:
		error = dev_wlc_intvar_get(dev, "wpa_auth", &val);
		if (error)
			return error;
		if (VAL_PSK(val))
			paramval = IW_AUTH_KEY_MGMT_PSK;
		else
			paramval = IW_AUTH_KEY_MGMT_802_1X;

		break;
	case IW_AUTH_TKIP_COUNTERMEASURES:
		dev_wlc_bufvar_get(dev, "tkip_countermeasures",
				   (char *)&paramval, 1);
		break;

	case IW_AUTH_DROP_UNENCRYPTED:
		dev_wlc_bufvar_get(dev, "wsec_restrict", (char *)&paramval, 1);
		break;

	case IW_AUTH_RX_UNENCRYPTED_EAPOL:
		dev_wlc_bufvar_get(dev, "rx_unencrypted_eapol",
				   (char *)&paramval, 1);
		break;

	case IW_AUTH_80211_AUTH_ALG:
		error = dev_wlc_intvar_get(dev, "auth", &val);
		if (error)
			return error;
		if (!val)
			paramval = IW_AUTH_ALG_OPEN_SYSTEM;
		else
			paramval = IW_AUTH_ALG_SHARED_KEY;
		break;
	case IW_AUTH_WPA_ENABLED:
		error = dev_wlc_intvar_get(dev, "wpa_auth", &val);
		if (error)
			return error;
		if (val)
			paramval = true;
		else
			paramval = false;
		break;
#if WIRELESS_EXT > 17
	case IW_AUTH_ROAMING_CONTROL:
		WL_ERROR("%s: IW_AUTH_ROAMING_CONTROL\n", __func__);
		break;
	case IW_AUTH_PRIVACY_INVOKED:
		paramval = iw->privacy_invoked;
		break;

#endif
	}
	vwrq->value = paramval;
	return 0;
}
#endif				/* WIRELESS_EXT > 17 */

static const iw_handler wl_iw_handler[] = {
	(iw_handler) wl_iw_config_commit,
	(iw_handler) wl_iw_get_name,
	(iw_handler) NULL,
	(iw_handler) NULL,
	(iw_handler) wl_iw_set_freq,
	(iw_handler) wl_iw_get_freq,
	(iw_handler) wl_iw_set_mode,
	(iw_handler) wl_iw_get_mode,
	(iw_handler) NULL,
	(iw_handler) NULL,
	(iw_handler) NULL,
	(iw_handler) wl_iw_get_range,
	(iw_handler) NULL,
	(iw_handler) NULL,
	(iw_handler) NULL,
	(iw_handler) NULL,
	(iw_handler) wl_iw_set_spy,
	(iw_handler) wl_iw_get_spy,
	(iw_handler) NULL,
	(iw_handler) NULL,
	(iw_handler) wl_iw_set_wap,
	(iw_handler) wl_iw_get_wap,
#if WIRELESS_EXT > 17
	(iw_handler) wl_iw_mlme,
#else
	(iw_handler) NULL,
#endif
#if defined(WL_IW_USE_ISCAN)
	(iw_handler) wl_iw_iscan_get_aplist,
#else
	(iw_handler) wl_iw_get_aplist,
#endif
#if WIRELESS_EXT > 13
#if defined(WL_IW_USE_ISCAN)
	(iw_handler) wl_iw_iscan_set_scan,
	(iw_handler) wl_iw_iscan_get_scan,
#else
	(iw_handler) wl_iw_set_scan,
	(iw_handler) wl_iw_get_scan,
#endif
#else
	(iw_handler) NULL,
	(iw_handler) NULL,
#endif				/* WIRELESS_EXT > 13 */
	(iw_handler) wl_iw_set_essid,
	(iw_handler) wl_iw_get_essid,