zd_mac.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228
  1. /* zd_mac.c
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. */
  17. #include <linux/netdevice.h>
  18. #include <linux/etherdevice.h>
  19. #include <linux/wireless.h>
  20. #include <linux/usb.h>
  21. #include <linux/jiffies.h>
  22. #include <net/ieee80211_radiotap.h>
  23. #include "zd_def.h"
  24. #include "zd_chip.h"
  25. #include "zd_mac.h"
  26. #include "zd_ieee80211.h"
  27. #include "zd_netdev.h"
  28. #include "zd_rf.h"
  29. #include "zd_util.h"
  30. static void ieee_init(struct ieee80211_device *ieee);
  31. static void softmac_init(struct ieee80211softmac_device *sm);
  32. static void set_rts_cts_work(struct work_struct *work);
  33. static void set_basic_rates_work(struct work_struct *work);
  34. static void housekeeping_init(struct zd_mac *mac);
  35. static void housekeeping_enable(struct zd_mac *mac);
  36. static void housekeeping_disable(struct zd_mac *mac);
  37. int zd_mac_init(struct zd_mac *mac,
  38. struct net_device *netdev,
  39. struct usb_interface *intf)
  40. {
  41. struct ieee80211_device *ieee = zd_netdev_ieee80211(netdev);
  42. memset(mac, 0, sizeof(*mac));
  43. spin_lock_init(&mac->lock);
  44. mac->netdev = netdev;
  45. INIT_DELAYED_WORK(&mac->set_rts_cts_work, set_rts_cts_work);
  46. INIT_DELAYED_WORK(&mac->set_basic_rates_work, set_basic_rates_work);
  47. ieee_init(ieee);
  48. softmac_init(ieee80211_priv(netdev));
  49. zd_chip_init(&mac->chip, netdev, intf);
  50. housekeeping_init(mac);
  51. return 0;
  52. }
  53. static int reset_channel(struct zd_mac *mac)
  54. {
  55. int r;
  56. unsigned long flags;
  57. const struct channel_range *range;
  58. spin_lock_irqsave(&mac->lock, flags);
  59. range = zd_channel_range(mac->regdomain);
  60. if (!range->start) {
  61. r = -EINVAL;
  62. goto out;
  63. }
  64. mac->requested_channel = range->start;
  65. r = 0;
  66. out:
  67. spin_unlock_irqrestore(&mac->lock, flags);
  68. return r;
  69. }
  70. int zd_mac_init_hw(struct zd_mac *mac, u8 device_type)
  71. {
  72. int r;
  73. struct zd_chip *chip = &mac->chip;
  74. u8 addr[ETH_ALEN];
  75. u8 default_regdomain;
  76. r = zd_chip_enable_int(chip);
  77. if (r)
  78. goto out;
  79. r = zd_chip_init_hw(chip, device_type);
  80. if (r)
  81. goto disable_int;
  82. zd_get_e2p_mac_addr(chip, addr);
  83. r = zd_write_mac_addr(chip, addr);
  84. if (r)
  85. goto disable_int;
  86. ZD_ASSERT(!irqs_disabled());
  87. spin_lock_irq(&mac->lock);
  88. memcpy(mac->netdev->dev_addr, addr, ETH_ALEN);
  89. spin_unlock_irq(&mac->lock);
  90. r = zd_read_regdomain(chip, &default_regdomain);
  91. if (r)
  92. goto disable_int;
  93. if (!zd_regdomain_supported(default_regdomain)) {
  94. dev_dbg_f(zd_mac_dev(mac),
  95. "Regulatory Domain %#04x is not supported.\n",
  96. default_regdomain);
  97. r = -EINVAL;
  98. goto disable_int;
  99. }
  100. spin_lock_irq(&mac->lock);
  101. mac->regdomain = mac->default_regdomain = default_regdomain;
  102. spin_unlock_irq(&mac->lock);
  103. r = reset_channel(mac);
  104. if (r)
  105. goto disable_int;
  106. /* We must inform the device that we are doing encryption/decryption in
  107. * software at the moment. */
  108. r = zd_set_encryption_type(chip, ENC_SNIFFER);
  109. if (r)
  110. goto disable_int;
  111. r = zd_geo_init(zd_mac_to_ieee80211(mac), mac->regdomain);
  112. if (r)
  113. goto disable_int;
  114. r = 0;
  115. disable_int:
  116. zd_chip_disable_int(chip);
  117. out:
  118. return r;
  119. }
  120. void zd_mac_clear(struct zd_mac *mac)
  121. {
  122. zd_chip_clear(&mac->chip);
  123. ZD_ASSERT(!spin_is_locked(&mac->lock));
  124. ZD_MEMCLEAR(mac, sizeof(struct zd_mac));
  125. }
  126. static int reset_mode(struct zd_mac *mac)
  127. {
  128. struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
  129. struct zd_ioreq32 ioreqs[3] = {
  130. { CR_RX_FILTER, STA_RX_FILTER },
  131. { CR_SNIFFER_ON, 0U },
  132. };
  133. if (ieee->iw_mode == IW_MODE_MONITOR) {
  134. ioreqs[0].value = 0xffffffff;
  135. ioreqs[1].value = 0x1;
  136. ioreqs[2].value = ENC_SNIFFER;
  137. }
  138. return zd_iowrite32a(&mac->chip, ioreqs, 3);
  139. }
  140. int zd_mac_open(struct net_device *netdev)
  141. {
  142. struct zd_mac *mac = zd_netdev_mac(netdev);
  143. struct zd_chip *chip = &mac->chip;
  144. int r;
  145. r = zd_chip_enable_int(chip);
  146. if (r < 0)
  147. goto out;
  148. r = zd_chip_set_basic_rates(chip, CR_RATES_80211B | CR_RATES_80211G);
  149. if (r < 0)
  150. goto disable_int;
  151. r = reset_mode(mac);
  152. if (r)
  153. goto disable_int;
  154. r = zd_chip_switch_radio_on(chip);
  155. if (r < 0)
  156. goto disable_int;
  157. r = zd_chip_set_channel(chip, mac->requested_channel);
  158. if (r < 0)
  159. goto disable_radio;
  160. r = zd_chip_enable_rx(chip);
  161. if (r < 0)
  162. goto disable_radio;
  163. r = zd_chip_enable_hwint(chip);
  164. if (r < 0)
  165. goto disable_rx;
  166. housekeeping_enable(mac);
  167. ieee80211softmac_start(netdev);
  168. return 0;
  169. disable_rx:
  170. zd_chip_disable_rx(chip);
  171. disable_radio:
  172. zd_chip_switch_radio_off(chip);
  173. disable_int:
  174. zd_chip_disable_int(chip);
  175. out:
  176. return r;
  177. }
  178. int zd_mac_stop(struct net_device *netdev)
  179. {
  180. struct zd_mac *mac = zd_netdev_mac(netdev);
  181. struct zd_chip *chip = &mac->chip;
  182. netif_stop_queue(netdev);
  183. /*
  184. * The order here deliberately is a little different from the open()
  185. * method, since we need to make sure there is no opportunity for RX
  186. * frames to be processed by softmac after we have stopped it.
  187. */
  188. zd_chip_disable_rx(chip);
  189. housekeeping_disable(mac);
  190. ieee80211softmac_stop(netdev);
  191. /* Ensure no work items are running or queued from this point */
  192. cancel_delayed_work(&mac->set_rts_cts_work);
  193. cancel_delayed_work(&mac->set_basic_rates_work);
  194. flush_workqueue(zd_workqueue);
  195. mac->updating_rts_rate = 0;
  196. mac->updating_basic_rates = 0;
  197. zd_chip_disable_hwint(chip);
  198. zd_chip_switch_radio_off(chip);
  199. zd_chip_disable_int(chip);
  200. return 0;
  201. }
  202. int zd_mac_set_mac_address(struct net_device *netdev, void *p)
  203. {
  204. int r;
  205. unsigned long flags;
  206. struct sockaddr *addr = p;
  207. struct zd_mac *mac = zd_netdev_mac(netdev);
  208. struct zd_chip *chip = &mac->chip;
  209. if (!is_valid_ether_addr(addr->sa_data))
  210. return -EADDRNOTAVAIL;
  211. dev_dbg_f(zd_mac_dev(mac),
  212. "Setting MAC to " MAC_FMT "\n", MAC_ARG(addr->sa_data));
  213. r = zd_write_mac_addr(chip, addr->sa_data);
  214. if (r)
  215. return r;
  216. spin_lock_irqsave(&mac->lock, flags);
  217. memcpy(netdev->dev_addr, addr->sa_data, ETH_ALEN);
  218. spin_unlock_irqrestore(&mac->lock, flags);
  219. return 0;
  220. }
  221. int zd_mac_set_regdomain(struct zd_mac *mac, u8 regdomain)
  222. {
  223. int r;
  224. u8 channel;
  225. ZD_ASSERT(!irqs_disabled());
  226. spin_lock_irq(&mac->lock);
  227. if (regdomain == 0) {
  228. regdomain = mac->default_regdomain;
  229. }
  230. if (!zd_regdomain_supported(regdomain)) {
  231. spin_unlock_irq(&mac->lock);
  232. return -EINVAL;
  233. }
  234. mac->regdomain = regdomain;
  235. channel = mac->requested_channel;
  236. spin_unlock_irq(&mac->lock);
  237. r = zd_geo_init(zd_mac_to_ieee80211(mac), regdomain);
  238. if (r)
  239. return r;
  240. if (!zd_regdomain_supports_channel(regdomain, channel)) {
  241. r = reset_channel(mac);
  242. if (r)
  243. return r;
  244. }
  245. return 0;
  246. }
  247. u8 zd_mac_get_regdomain(struct zd_mac *mac)
  248. {
  249. unsigned long flags;
  250. u8 regdomain;
  251. spin_lock_irqsave(&mac->lock, flags);
  252. regdomain = mac->regdomain;
  253. spin_unlock_irqrestore(&mac->lock, flags);
  254. return regdomain;
  255. }
  256. /* Fallback to lowest rate, if rate is unknown. */
  257. static u8 rate_to_zd_rate(u8 rate)
  258. {
  259. switch (rate) {
  260. case IEEE80211_CCK_RATE_2MB:
  261. return ZD_CCK_RATE_2M;
  262. case IEEE80211_CCK_RATE_5MB:
  263. return ZD_CCK_RATE_5_5M;
  264. case IEEE80211_CCK_RATE_11MB:
  265. return ZD_CCK_RATE_11M;
  266. case IEEE80211_OFDM_RATE_6MB:
  267. return ZD_OFDM_RATE_6M;
  268. case IEEE80211_OFDM_RATE_9MB:
  269. return ZD_OFDM_RATE_9M;
  270. case IEEE80211_OFDM_RATE_12MB:
  271. return ZD_OFDM_RATE_12M;
  272. case IEEE80211_OFDM_RATE_18MB:
  273. return ZD_OFDM_RATE_18M;
  274. case IEEE80211_OFDM_RATE_24MB:
  275. return ZD_OFDM_RATE_24M;
  276. case IEEE80211_OFDM_RATE_36MB:
  277. return ZD_OFDM_RATE_36M;
  278. case IEEE80211_OFDM_RATE_48MB:
  279. return ZD_OFDM_RATE_48M;
  280. case IEEE80211_OFDM_RATE_54MB:
  281. return ZD_OFDM_RATE_54M;
  282. }
  283. return ZD_CCK_RATE_1M;
  284. }
  285. static u16 rate_to_cr_rate(u8 rate)
  286. {
  287. switch (rate) {
  288. case IEEE80211_CCK_RATE_2MB:
  289. return CR_RATE_1M;
  290. case IEEE80211_CCK_RATE_5MB:
  291. return CR_RATE_5_5M;
  292. case IEEE80211_CCK_RATE_11MB:
  293. return CR_RATE_11M;
  294. case IEEE80211_OFDM_RATE_6MB:
  295. return CR_RATE_6M;
  296. case IEEE80211_OFDM_RATE_9MB:
  297. return CR_RATE_9M;
  298. case IEEE80211_OFDM_RATE_12MB:
  299. return CR_RATE_12M;
  300. case IEEE80211_OFDM_RATE_18MB:
  301. return CR_RATE_18M;
  302. case IEEE80211_OFDM_RATE_24MB:
  303. return CR_RATE_24M;
  304. case IEEE80211_OFDM_RATE_36MB:
  305. return CR_RATE_36M;
  306. case IEEE80211_OFDM_RATE_48MB:
  307. return CR_RATE_48M;
  308. case IEEE80211_OFDM_RATE_54MB:
  309. return CR_RATE_54M;
  310. }
  311. return CR_RATE_1M;
  312. }
  313. static void try_enable_tx(struct zd_mac *mac)
  314. {
  315. unsigned long flags;
  316. spin_lock_irqsave(&mac->lock, flags);
  317. if (mac->updating_rts_rate == 0 && mac->updating_basic_rates == 0)
  318. netif_wake_queue(mac->netdev);
  319. spin_unlock_irqrestore(&mac->lock, flags);
  320. }
  321. static void set_rts_cts_work(struct work_struct *work)
  322. {
  323. struct zd_mac *mac =
  324. container_of(work, struct zd_mac, set_rts_cts_work.work);
  325. unsigned long flags;
  326. u8 rts_rate;
  327. unsigned int short_preamble;
  328. mutex_lock(&mac->chip.mutex);
  329. spin_lock_irqsave(&mac->lock, flags);
  330. mac->updating_rts_rate = 0;
  331. rts_rate = mac->rts_rate;
  332. short_preamble = mac->short_preamble;
  333. spin_unlock_irqrestore(&mac->lock, flags);
  334. zd_chip_set_rts_cts_rate_locked(&mac->chip, rts_rate, short_preamble);
  335. mutex_unlock(&mac->chip.mutex);
  336. try_enable_tx(mac);
  337. }
  338. static void set_basic_rates_work(struct work_struct *work)
  339. {
  340. struct zd_mac *mac =
  341. container_of(work, struct zd_mac, set_basic_rates_work.work);
  342. unsigned long flags;
  343. u16 basic_rates;
  344. mutex_lock(&mac->chip.mutex);
  345. spin_lock_irqsave(&mac->lock, flags);
  346. mac->updating_basic_rates = 0;
  347. basic_rates = mac->basic_rates;
  348. spin_unlock_irqrestore(&mac->lock, flags);
  349. zd_chip_set_basic_rates_locked(&mac->chip, basic_rates);
  350. mutex_unlock(&mac->chip.mutex);
  351. try_enable_tx(mac);
  352. }
  353. static void bssinfo_change(struct net_device *netdev, u32 changes)
  354. {
  355. struct zd_mac *mac = zd_netdev_mac(netdev);
  356. struct ieee80211softmac_device *softmac = ieee80211_priv(netdev);
  357. struct ieee80211softmac_bss_info *bssinfo = &softmac->bssinfo;
  358. int need_set_rts_cts = 0;
  359. int need_set_rates = 0;
  360. u16 basic_rates;
  361. unsigned long flags;
  362. dev_dbg_f(zd_mac_dev(mac), "changes: %x\n", changes);
  363. if (changes & IEEE80211SOFTMAC_BSSINFOCHG_SHORT_PREAMBLE) {
  364. spin_lock_irqsave(&mac->lock, flags);
  365. mac->short_preamble = bssinfo->short_preamble;
  366. spin_unlock_irqrestore(&mac->lock, flags);
  367. need_set_rts_cts = 1;
  368. }
  369. if (changes & IEEE80211SOFTMAC_BSSINFOCHG_RATES) {
  370. /* Set RTS rate to highest available basic rate */
  371. u8 rate = ieee80211softmac_highest_supported_rate(softmac,
  372. &bssinfo->supported_rates, 1);
  373. rate = rate_to_zd_rate(rate);
  374. spin_lock_irqsave(&mac->lock, flags);
  375. if (rate != mac->rts_rate) {
  376. mac->rts_rate = rate;
  377. need_set_rts_cts = 1;
  378. }
  379. spin_unlock_irqrestore(&mac->lock, flags);
  380. /* Set basic rates */
  381. need_set_rates = 1;
  382. if (bssinfo->supported_rates.count == 0) {
  383. /* Allow the device to be flexible */
  384. basic_rates = CR_RATES_80211B | CR_RATES_80211G;
  385. } else {
  386. int i = 0;
  387. basic_rates = 0;
  388. for (i = 0; i < bssinfo->supported_rates.count; i++) {
  389. u16 rate = bssinfo->supported_rates.rates[i];
  390. if ((rate & IEEE80211_BASIC_RATE_MASK) == 0)
  391. continue;
  392. rate &= ~IEEE80211_BASIC_RATE_MASK;
  393. basic_rates |= rate_to_cr_rate(rate);
  394. }
  395. }
  396. spin_lock_irqsave(&mac->lock, flags);
  397. mac->basic_rates = basic_rates;
  398. spin_unlock_irqrestore(&mac->lock, flags);
  399. }
  400. /* Schedule any changes we made above */
  401. spin_lock_irqsave(&mac->lock, flags);
  402. if (need_set_rts_cts && !mac->updating_rts_rate) {
  403. mac->updating_rts_rate = 1;
  404. netif_stop_queue(mac->netdev);
  405. queue_delayed_work(zd_workqueue, &mac->set_rts_cts_work, 0);
  406. }
  407. if (need_set_rates && !mac->updating_basic_rates) {
  408. mac->updating_basic_rates = 1;
  409. netif_stop_queue(mac->netdev);
  410. queue_delayed_work(zd_workqueue, &mac->set_basic_rates_work,
  411. 0);
  412. }
  413. spin_unlock_irqrestore(&mac->lock, flags);
  414. }
  415. static void set_channel(struct net_device *netdev, u8 channel)
  416. {
  417. struct zd_mac *mac = zd_netdev_mac(netdev);
  418. dev_dbg_f(zd_mac_dev(mac), "channel %d\n", channel);
  419. zd_chip_set_channel(&mac->chip, channel);
  420. }
  421. int zd_mac_request_channel(struct zd_mac *mac, u8 channel)
  422. {
  423. unsigned long lock_flags;
  424. struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
  425. if (ieee->iw_mode == IW_MODE_INFRA)
  426. return -EPERM;
  427. spin_lock_irqsave(&mac->lock, lock_flags);
  428. if (!zd_regdomain_supports_channel(mac->regdomain, channel)) {
  429. spin_unlock_irqrestore(&mac->lock, lock_flags);
  430. return -EINVAL;
  431. }
  432. mac->requested_channel = channel;
  433. spin_unlock_irqrestore(&mac->lock, lock_flags);
  434. if (netif_running(mac->netdev))
  435. return zd_chip_set_channel(&mac->chip, channel);
  436. else
  437. return 0;
  438. }
  439. u8 zd_mac_get_channel(struct zd_mac *mac)
  440. {
  441. u8 channel = zd_chip_get_channel(&mac->chip);
  442. dev_dbg_f(zd_mac_dev(mac), "channel %u\n", channel);
  443. return channel;
  444. }
  445. /* If wrong rate is given, we are falling back to the slowest rate: 1MBit/s */
  446. static u8 zd_rate_typed(u8 zd_rate)
  447. {
  448. static const u8 typed_rates[16] = {
  449. [ZD_CCK_RATE_1M] = ZD_CS_CCK|ZD_CCK_RATE_1M,
  450. [ZD_CCK_RATE_2M] = ZD_CS_CCK|ZD_CCK_RATE_2M,
  451. [ZD_CCK_RATE_5_5M] = ZD_CS_CCK|ZD_CCK_RATE_5_5M,
  452. [ZD_CCK_RATE_11M] = ZD_CS_CCK|ZD_CCK_RATE_11M,
  453. [ZD_OFDM_RATE_6M] = ZD_CS_OFDM|ZD_OFDM_RATE_6M,
  454. [ZD_OFDM_RATE_9M] = ZD_CS_OFDM|ZD_OFDM_RATE_9M,
  455. [ZD_OFDM_RATE_12M] = ZD_CS_OFDM|ZD_OFDM_RATE_12M,
  456. [ZD_OFDM_RATE_18M] = ZD_CS_OFDM|ZD_OFDM_RATE_18M,
  457. [ZD_OFDM_RATE_24M] = ZD_CS_OFDM|ZD_OFDM_RATE_24M,
  458. [ZD_OFDM_RATE_36M] = ZD_CS_OFDM|ZD_OFDM_RATE_36M,
  459. [ZD_OFDM_RATE_48M] = ZD_CS_OFDM|ZD_OFDM_RATE_48M,
  460. [ZD_OFDM_RATE_54M] = ZD_CS_OFDM|ZD_OFDM_RATE_54M,
  461. };
  462. ZD_ASSERT(ZD_CS_RATE_MASK == 0x0f);
  463. return typed_rates[zd_rate & ZD_CS_RATE_MASK];
  464. }
  465. int zd_mac_set_mode(struct zd_mac *mac, u32 mode)
  466. {
  467. struct ieee80211_device *ieee;
  468. switch (mode) {
  469. case IW_MODE_AUTO:
  470. case IW_MODE_ADHOC:
  471. case IW_MODE_INFRA:
  472. mac->netdev->type = ARPHRD_ETHER;
  473. break;
  474. case IW_MODE_MONITOR:
  475. mac->netdev->type = ARPHRD_IEEE80211_RADIOTAP;
  476. break;
  477. default:
  478. dev_dbg_f(zd_mac_dev(mac), "wrong mode %u\n", mode);
  479. return -EINVAL;
  480. }
  481. ieee = zd_mac_to_ieee80211(mac);
  482. ZD_ASSERT(!irqs_disabled());
  483. spin_lock_irq(&ieee->lock);
  484. ieee->iw_mode = mode;
  485. spin_unlock_irq(&ieee->lock);
  486. if (netif_running(mac->netdev))
  487. return reset_mode(mac);
  488. return 0;
  489. }
  490. int zd_mac_get_mode(struct zd_mac *mac, u32 *mode)
  491. {
  492. unsigned long flags;
  493. struct ieee80211_device *ieee;
  494. ieee = zd_mac_to_ieee80211(mac);
  495. spin_lock_irqsave(&ieee->lock, flags);
  496. *mode = ieee->iw_mode;
  497. spin_unlock_irqrestore(&ieee->lock, flags);
  498. return 0;
  499. }
  500. int zd_mac_get_range(struct zd_mac *mac, struct iw_range *range)
  501. {
  502. int i;
  503. const struct channel_range *channel_range;
  504. u8 regdomain;
  505. memset(range, 0, sizeof(*range));
  506. /* FIXME: Not so important and depends on the mode. For 802.11g
  507. * usually this value is used. It seems to be that Bit/s number is
  508. * given here.
  509. */
  510. range->throughput = 27 * 1000 * 1000;
  511. range->max_qual.qual = 100;
  512. range->max_qual.level = 100;
  513. /* FIXME: Needs still to be tuned. */
  514. range->avg_qual.qual = 71;
  515. range->avg_qual.level = 80;
  516. /* FIXME: depends on standard? */
  517. range->min_rts = 256;
  518. range->max_rts = 2346;
  519. range->min_frag = MIN_FRAG_THRESHOLD;
  520. range->max_frag = MAX_FRAG_THRESHOLD;
  521. range->max_encoding_tokens = WEP_KEYS;
  522. range->num_encoding_sizes = 2;
  523. range->encoding_size[0] = 5;
  524. range->encoding_size[1] = WEP_KEY_LEN;
  525. range->we_version_compiled = WIRELESS_EXT;
  526. range->we_version_source = 20;
  527. ZD_ASSERT(!irqs_disabled());
  528. spin_lock_irq(&mac->lock);
  529. regdomain = mac->regdomain;
  530. spin_unlock_irq(&mac->lock);
  531. channel_range = zd_channel_range(regdomain);
  532. range->num_channels = channel_range->end - channel_range->start;
  533. range->old_num_channels = range->num_channels;
  534. range->num_frequency = range->num_channels;
  535. range->old_num_frequency = range->num_frequency;
  536. for (i = 0; i < range->num_frequency; i++) {
  537. struct iw_freq *freq = &range->freq[i];
  538. freq->i = channel_range->start + i;
  539. zd_channel_to_freq(freq, freq->i);
  540. }
  541. return 0;
  542. }
  543. static int zd_calc_tx_length_us(u8 *service, u8 zd_rate, u16 tx_length)
  544. {
  545. static const u8 rate_divisor[] = {
  546. [ZD_CCK_RATE_1M] = 1,
  547. [ZD_CCK_RATE_2M] = 2,
  548. [ZD_CCK_RATE_5_5M] = 11, /* bits must be doubled */
  549. [ZD_CCK_RATE_11M] = 11,
  550. [ZD_OFDM_RATE_6M] = 6,
  551. [ZD_OFDM_RATE_9M] = 9,
  552. [ZD_OFDM_RATE_12M] = 12,
  553. [ZD_OFDM_RATE_18M] = 18,
  554. [ZD_OFDM_RATE_24M] = 24,
  555. [ZD_OFDM_RATE_36M] = 36,
  556. [ZD_OFDM_RATE_48M] = 48,
  557. [ZD_OFDM_RATE_54M] = 54,
  558. };
  559. u32 bits = (u32)tx_length * 8;
  560. u32 divisor;
  561. divisor = rate_divisor[zd_rate];
  562. if (divisor == 0)
  563. return -EINVAL;
  564. switch (zd_rate) {
  565. case ZD_CCK_RATE_5_5M:
  566. bits = (2*bits) + 10; /* round up to the next integer */
  567. break;
  568. case ZD_CCK_RATE_11M:
  569. if (service) {
  570. u32 t = bits % 11;
  571. *service &= ~ZD_PLCP_SERVICE_LENGTH_EXTENSION;
  572. if (0 < t && t <= 3) {
  573. *service |= ZD_PLCP_SERVICE_LENGTH_EXTENSION;
  574. }
  575. }
  576. bits += 10; /* round up to the next integer */
  577. break;
  578. }
  579. return bits/divisor;
  580. }
  581. enum {
  582. R2M_SHORT_PREAMBLE = 0x01,
  583. R2M_11A = 0x02,
  584. };
  585. static u8 zd_rate_to_modulation(u8 zd_rate, int flags)
  586. {
  587. u8 modulation;
  588. modulation = zd_rate_typed(zd_rate);
  589. if (flags & R2M_SHORT_PREAMBLE) {
  590. switch (ZD_CS_RATE(modulation)) {
  591. case ZD_CCK_RATE_2M:
  592. case ZD_CCK_RATE_5_5M:
  593. case ZD_CCK_RATE_11M:
  594. modulation |= ZD_CS_CCK_PREA_SHORT;
  595. return modulation;
  596. }
  597. }
  598. if (flags & R2M_11A) {
  599. if (ZD_CS_TYPE(modulation) == ZD_CS_OFDM)
  600. modulation |= ZD_CS_OFDM_MODE_11A;
  601. }
  602. return modulation;
  603. }
  604. static void cs_set_modulation(struct zd_mac *mac, struct zd_ctrlset *cs,
  605. struct ieee80211_hdr_4addr *hdr)
  606. {
  607. struct ieee80211softmac_device *softmac = ieee80211_priv(mac->netdev);
  608. u16 ftype = WLAN_FC_GET_TYPE(le16_to_cpu(hdr->frame_ctl));
  609. u8 rate, zd_rate;
  610. int is_mgt = (ftype == IEEE80211_FTYPE_MGMT) != 0;
  611. int is_multicast = is_multicast_ether_addr(hdr->addr1);
  612. int short_preamble = ieee80211softmac_short_preamble_ok(softmac,
  613. is_multicast, is_mgt);
  614. int flags = 0;
  615. /* FIXME: 802.11a? */
  616. rate = ieee80211softmac_suggest_txrate(softmac, is_multicast, is_mgt);
  617. if (short_preamble)
  618. flags |= R2M_SHORT_PREAMBLE;
  619. zd_rate = rate_to_zd_rate(rate);
  620. cs->modulation = zd_rate_to_modulation(zd_rate, flags);
  621. }
  622. static void cs_set_control(struct zd_mac *mac, struct zd_ctrlset *cs,
  623. struct ieee80211_hdr_4addr *header)
  624. {
  625. struct ieee80211softmac_device *softmac = ieee80211_priv(mac->netdev);
  626. unsigned int tx_length = le16_to_cpu(cs->tx_length);
  627. u16 fctl = le16_to_cpu(header->frame_ctl);
  628. u16 ftype = WLAN_FC_GET_TYPE(fctl);
  629. u16 stype = WLAN_FC_GET_STYPE(fctl);
  630. /*
  631. * CONTROL TODO:
  632. * - if backoff needed, enable bit 0
  633. * - if burst (backoff not needed) disable bit 0
  634. */
  635. cs->control = 0;
  636. /* First fragment */
  637. if (WLAN_GET_SEQ_FRAG(le16_to_cpu(header->seq_ctl)) == 0)
  638. cs->control |= ZD_CS_NEED_RANDOM_BACKOFF;
  639. /* Multicast */
  640. if (is_multicast_ether_addr(header->addr1))
  641. cs->control |= ZD_CS_MULTICAST;
  642. /* PS-POLL */
  643. if (stype == IEEE80211_STYPE_PSPOLL)
  644. cs->control |= ZD_CS_PS_POLL_FRAME;
  645. /* Unicast data frames over the threshold should have RTS */
  646. if (!is_multicast_ether_addr(header->addr1) &&
  647. ftype != IEEE80211_FTYPE_MGMT &&
  648. tx_length > zd_netdev_ieee80211(mac->netdev)->rts)
  649. cs->control |= ZD_CS_RTS;
  650. /* Use CTS-to-self protection if required */
  651. if (ZD_CS_TYPE(cs->modulation) == ZD_CS_OFDM &&
  652. ieee80211softmac_protection_needed(softmac)) {
  653. /* FIXME: avoid sending RTS *and* self-CTS, is that correct? */
  654. cs->control &= ~ZD_CS_RTS;
  655. cs->control |= ZD_CS_SELF_CTS;
  656. }
  657. /* FIXME: Management frame? */
  658. }
  659. static int fill_ctrlset(struct zd_mac *mac,
  660. struct ieee80211_txb *txb,
  661. int frag_num)
  662. {
  663. int r;
  664. struct sk_buff *skb = txb->fragments[frag_num];
  665. struct ieee80211_hdr_4addr *hdr =
  666. (struct ieee80211_hdr_4addr *) skb->data;
  667. unsigned int frag_len = skb->len + IEEE80211_FCS_LEN;
  668. unsigned int next_frag_len;
  669. unsigned int packet_length;
  670. struct zd_ctrlset *cs = (struct zd_ctrlset *)
  671. skb_push(skb, sizeof(struct zd_ctrlset));
  672. if (frag_num+1 < txb->nr_frags) {
  673. next_frag_len = txb->fragments[frag_num+1]->len +
  674. IEEE80211_FCS_LEN;
  675. } else {
  676. next_frag_len = 0;
  677. }
  678. ZD_ASSERT(frag_len <= 0xffff);
  679. ZD_ASSERT(next_frag_len <= 0xffff);
  680. cs_set_modulation(mac, cs, hdr);
  681. cs->tx_length = cpu_to_le16(frag_len);
  682. cs_set_control(mac, cs, hdr);
  683. packet_length = frag_len + sizeof(struct zd_ctrlset) + 10;
  684. ZD_ASSERT(packet_length <= 0xffff);
  685. /* ZD1211B: Computing the length difference this way, gives us
  686. * flexibility to compute the packet length.
  687. */
  688. cs->packet_length = cpu_to_le16(mac->chip.is_zd1211b ?
  689. packet_length - frag_len : packet_length);
  690. /*
  691. * CURRENT LENGTH:
  692. * - transmit frame length in microseconds
  693. * - seems to be derived from frame length
  694. * - see Cal_Us_Service() in zdinlinef.h
  695. * - if macp->bTxBurstEnable is enabled, then multiply by 4
  696. * - bTxBurstEnable is never set in the vendor driver
  697. *
  698. * SERVICE:
  699. * - "for PLCP configuration"
  700. * - always 0 except in some situations at 802.11b 11M
  701. * - see line 53 of zdinlinef.h
  702. */
  703. cs->service = 0;
  704. r = zd_calc_tx_length_us(&cs->service, ZD_CS_RATE(cs->modulation),
  705. le16_to_cpu(cs->tx_length));
  706. if (r < 0)
  707. return r;
  708. cs->current_length = cpu_to_le16(r);
  709. if (next_frag_len == 0) {
  710. cs->next_frame_length = 0;
  711. } else {
  712. r = zd_calc_tx_length_us(NULL, ZD_CS_RATE(cs->modulation),
  713. next_frag_len);
  714. if (r < 0)
  715. return r;
  716. cs->next_frame_length = cpu_to_le16(r);
  717. }
  718. return 0;
  719. }
  720. static int zd_mac_tx(struct zd_mac *mac, struct ieee80211_txb *txb, int pri)
  721. {
  722. int i, r;
  723. for (i = 0; i < txb->nr_frags; i++) {
  724. struct sk_buff *skb = txb->fragments[i];
  725. r = fill_ctrlset(mac, txb, i);
  726. if (r)
  727. return r;
  728. r = zd_usb_tx(&mac->chip.usb, skb->data, skb->len);
  729. if (r)
  730. return r;
  731. }
  732. /* FIXME: shouldn't this be handled by the upper layers? */
  733. mac->netdev->trans_start = jiffies;
  734. ieee80211_txb_free(txb);
  735. return 0;
  736. }
  737. struct zd_rt_hdr {
  738. struct ieee80211_radiotap_header rt_hdr;
  739. u8 rt_flags;
  740. u8 rt_rate;
  741. u16 rt_channel;
  742. u16 rt_chbitmask;
  743. } __attribute__((packed));
  744. static void fill_rt_header(void *buffer, struct zd_mac *mac,
  745. const struct ieee80211_rx_stats *stats,
  746. const struct rx_status *status)
  747. {
  748. struct zd_rt_hdr *hdr = buffer;
  749. hdr->rt_hdr.it_version = PKTHDR_RADIOTAP_VERSION;
  750. hdr->rt_hdr.it_pad = 0;
  751. hdr->rt_hdr.it_len = cpu_to_le16(sizeof(struct zd_rt_hdr));
  752. hdr->rt_hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
  753. (1 << IEEE80211_RADIOTAP_CHANNEL) |
  754. (1 << IEEE80211_RADIOTAP_RATE));
  755. hdr->rt_flags = 0;
  756. if (status->decryption_type & (ZD_RX_WEP64|ZD_RX_WEP128|ZD_RX_WEP256))
  757. hdr->rt_flags |= IEEE80211_RADIOTAP_F_WEP;
  758. hdr->rt_rate = stats->rate / 5;
  759. /* FIXME: 802.11a */
  760. hdr->rt_channel = cpu_to_le16(ieee80211chan2mhz(
  761. _zd_chip_get_channel(&mac->chip)));
  762. hdr->rt_chbitmask = cpu_to_le16(IEEE80211_CHAN_2GHZ |
  763. ((status->frame_status & ZD_RX_FRAME_MODULATION_MASK) ==
  764. ZD_RX_OFDM ? IEEE80211_CHAN_OFDM : IEEE80211_CHAN_CCK));
  765. }
  766. /* Returns 1 if the data packet is for us and 0 otherwise. */
  767. static int is_data_packet_for_us(struct ieee80211_device *ieee,
  768. struct ieee80211_hdr_4addr *hdr)
  769. {
  770. struct net_device *netdev = ieee->dev;
  771. u16 fc = le16_to_cpu(hdr->frame_ctl);
  772. ZD_ASSERT(WLAN_FC_GET_TYPE(fc) == IEEE80211_FTYPE_DATA);
  773. switch (ieee->iw_mode) {
  774. case IW_MODE_ADHOC:
  775. if ((fc & (IEEE80211_FCTL_TODS|IEEE80211_FCTL_FROMDS)) != 0 ||
  776. memcmp(hdr->addr3, ieee->bssid, ETH_ALEN) != 0)
  777. return 0;
  778. break;
  779. case IW_MODE_AUTO:
  780. case IW_MODE_INFRA:
  781. if ((fc & (IEEE80211_FCTL_TODS|IEEE80211_FCTL_FROMDS)) !=
  782. IEEE80211_FCTL_FROMDS ||
  783. memcmp(hdr->addr2, ieee->bssid, ETH_ALEN) != 0)
  784. return 0;
  785. break;
  786. default:
  787. ZD_ASSERT(ieee->iw_mode != IW_MODE_MONITOR);
  788. return 0;
  789. }
  790. return memcmp(hdr->addr1, netdev->dev_addr, ETH_ALEN) == 0 ||
  791. is_multicast_ether_addr(hdr->addr1) ||
  792. (netdev->flags & IFF_PROMISC);
  793. }
  794. /* Filters received packets. The function returns 1 if the packet should be
  795. * forwarded to ieee80211_rx(). If the packet should be ignored the function
  796. * returns 0. If an invalid packet is found the function returns -EINVAL.
  797. *
  798. * The function calls ieee80211_rx_mgt() directly.
  799. *
  800. * It has been based on ieee80211_rx_any.
  801. */
  802. static int filter_rx(struct ieee80211_device *ieee,
  803. const u8 *buffer, unsigned int length,
  804. struct ieee80211_rx_stats *stats)
  805. {
  806. struct ieee80211_hdr_4addr *hdr;
  807. u16 fc;
  808. if (ieee->iw_mode == IW_MODE_MONITOR)
  809. return 1;
  810. hdr = (struct ieee80211_hdr_4addr *)buffer;
  811. fc = le16_to_cpu(hdr->frame_ctl);
  812. if ((fc & IEEE80211_FCTL_VERS) != 0)
  813. return -EINVAL;
  814. switch (WLAN_FC_GET_TYPE(fc)) {
  815. case IEEE80211_FTYPE_MGMT:
  816. if (length < sizeof(struct ieee80211_hdr_3addr))
  817. return -EINVAL;
  818. ieee80211_rx_mgt(ieee, hdr, stats);
  819. return 0;
  820. case IEEE80211_FTYPE_CTL:
  821. return 0;
  822. case IEEE80211_FTYPE_DATA:
  823. /* Ignore invalid short buffers */
  824. if (length < sizeof(struct ieee80211_hdr_3addr))
  825. return -EINVAL;
  826. return is_data_packet_for_us(ieee, hdr);
  827. }
  828. return -EINVAL;
  829. }
  830. static void update_qual_rssi(struct zd_mac *mac,
  831. const u8 *buffer, unsigned int length,
  832. u8 qual_percent, u8 rssi_percent)
  833. {
  834. unsigned long flags;
  835. struct ieee80211_hdr_3addr *hdr;
  836. int i;
  837. hdr = (struct ieee80211_hdr_3addr *)buffer;
  838. if (length < offsetof(struct ieee80211_hdr_3addr, addr3))
  839. return;
  840. if (memcmp(hdr->addr2, zd_mac_to_ieee80211(mac)->bssid, ETH_ALEN) != 0)
  841. return;
  842. spin_lock_irqsave(&mac->lock, flags);
  843. i = mac->stats_count % ZD_MAC_STATS_BUFFER_SIZE;
  844. mac->qual_buffer[i] = qual_percent;
  845. mac->rssi_buffer[i] = rssi_percent;
  846. mac->stats_count++;
  847. spin_unlock_irqrestore(&mac->lock, flags);
  848. }
  849. static int fill_rx_stats(struct ieee80211_rx_stats *stats,
  850. const struct rx_status **pstatus,
  851. struct zd_mac *mac,
  852. const u8 *buffer, unsigned int length)
  853. {
  854. const struct rx_status *status;
  855. *pstatus = status = zd_tail(buffer, length, sizeof(struct rx_status));
  856. if (status->frame_status & ZD_RX_ERROR) {
  857. /* FIXME: update? */
  858. return -EINVAL;
  859. }
  860. memset(stats, 0, sizeof(struct ieee80211_rx_stats));
  861. stats->len = length - (ZD_PLCP_HEADER_SIZE + IEEE80211_FCS_LEN +
  862. + sizeof(struct rx_status));
  863. /* FIXME: 802.11a */
  864. stats->freq = IEEE80211_24GHZ_BAND;
  865. stats->received_channel = _zd_chip_get_channel(&mac->chip);
  866. stats->rssi = zd_rx_strength_percent(status->signal_strength);
  867. stats->signal = zd_rx_qual_percent(buffer,
  868. length - sizeof(struct rx_status),
  869. status);
  870. stats->mask = IEEE80211_STATMASK_RSSI | IEEE80211_STATMASK_SIGNAL;
  871. stats->rate = zd_rx_rate(buffer, status);
  872. if (stats->rate)
  873. stats->mask |= IEEE80211_STATMASK_RATE;
  874. return 0;
  875. }
  876. int zd_mac_rx(struct zd_mac *mac, const u8 *buffer, unsigned int length)
  877. {
  878. int r;
  879. struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);
  880. struct ieee80211_rx_stats stats;
  881. const struct rx_status *status;
  882. struct sk_buff *skb;
  883. if (length < ZD_PLCP_HEADER_SIZE + IEEE80211_1ADDR_LEN +
  884. IEEE80211_FCS_LEN + sizeof(struct rx_status))
  885. return -EINVAL;
  886. r = fill_rx_stats(&stats, &status, mac, buffer, length);
  887. if (r)
  888. return r;
  889. length -= ZD_PLCP_HEADER_SIZE+IEEE80211_FCS_LEN+
  890. sizeof(struct rx_status);
  891. buffer += ZD_PLCP_HEADER_SIZE;
  892. update_qual_rssi(mac, buffer, length, stats.signal, stats.rssi);
  893. r = filter_rx(ieee, buffer, length, &stats);
  894. if (r <= 0)
  895. return r;
  896. skb = dev_alloc_skb(sizeof(struct zd_rt_hdr) + length);
  897. if (!skb)
  898. return -ENOMEM;
  899. if (ieee->iw_mode == IW_MODE_MONITOR)
  900. fill_rt_header(skb_put(skb, sizeof(struct zd_rt_hdr)), mac,
  901. &stats, status);
  902. memcpy(skb_put(skb, length), buffer, length);
  903. r = ieee80211_rx(ieee, skb, &stats);
  904. if (!r) {
  905. ZD_ASSERT(in_irq());
  906. dev_kfree_skb_irq(skb);
  907. }
  908. return 0;
  909. }
  910. static int netdev_tx(struct ieee80211_txb *txb, struct net_device *netdev,
  911. int pri)
  912. {
  913. return zd_mac_tx(zd_netdev_mac(netdev), txb, pri);
  914. }
  915. static void set_security(struct net_device *netdev,
  916. struct ieee80211_security *sec)
  917. {
  918. struct ieee80211_device *ieee = zd_netdev_ieee80211(netdev);
  919. struct ieee80211_security *secinfo = &ieee->sec;
  920. int keyidx;
  921. dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)), "\n");
  922. for (keyidx = 0; keyidx<WEP_KEYS; keyidx++)
  923. if (sec->flags & (1<<keyidx)) {
  924. secinfo->encode_alg[keyidx] = sec->encode_alg[keyidx];
  925. secinfo->key_sizes[keyidx] = sec->key_sizes[keyidx];
  926. memcpy(secinfo->keys[keyidx], sec->keys[keyidx],
  927. SCM_KEY_LEN);
  928. }
  929. if (sec->flags & SEC_ACTIVE_KEY) {
  930. secinfo->active_key = sec->active_key;
  931. dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
  932. " .active_key = %d\n", sec->active_key);
  933. }
  934. if (sec->flags & SEC_UNICAST_GROUP) {
  935. secinfo->unicast_uses_group = sec->unicast_uses_group;
  936. dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
  937. " .unicast_uses_group = %d\n",
  938. sec->unicast_uses_group);
  939. }
  940. if (sec->flags & SEC_LEVEL) {
  941. secinfo->level = sec->level;
  942. dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
  943. " .level = %d\n", sec->level);
  944. }
  945. if (sec->flags & SEC_ENABLED) {
  946. secinfo->enabled = sec->enabled;
  947. dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
  948. " .enabled = %d\n", sec->enabled);
  949. }
  950. if (sec->flags & SEC_ENCRYPT) {
  951. secinfo->encrypt = sec->encrypt;
  952. dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
  953. " .encrypt = %d\n", sec->encrypt);
  954. }
  955. if (sec->flags & SEC_AUTH_MODE) {
  956. secinfo->auth_mode = sec->auth_mode;
  957. dev_dbg_f(zd_mac_dev(zd_netdev_mac(netdev)),
  958. " .auth_mode = %d\n", sec->auth_mode);
  959. }
  960. }
  961. static void ieee_init(struct ieee80211_device *ieee)
  962. {
  963. ieee->mode = IEEE_B | IEEE_G;
  964. ieee->freq_band = IEEE80211_24GHZ_BAND;
  965. ieee->modulation = IEEE80211_OFDM_MODULATION | IEEE80211_CCK_MODULATION;
  966. ieee->tx_headroom = sizeof(struct zd_ctrlset);
  967. ieee->set_security = set_security;
  968. ieee->hard_start_xmit = netdev_tx;
  969. /* Software encryption/decryption for now */
  970. ieee->host_build_iv = 0;
  971. ieee->host_encrypt = 1;
  972. ieee->host_decrypt = 1;
  973. /* FIXME: default to managed mode, until ieee80211 and zd1211rw can
  974. * correctly support AUTO */
  975. ieee->iw_mode = IW_MODE_INFRA;
  976. }
  977. static void softmac_init(struct ieee80211softmac_device *sm)
  978. {
  979. sm->set_channel = set_channel;
  980. sm->bssinfo_change = bssinfo_change;
  981. }
  982. struct iw_statistics *zd_mac_get_wireless_stats(struct net_device *ndev)
  983. {
  984. struct zd_mac *mac = zd_netdev_mac(ndev);
  985. struct iw_statistics *iw_stats = &mac->iw_stats;
  986. unsigned int i, count, qual_total, rssi_total;
  987. memset(iw_stats, 0, sizeof(struct iw_statistics));
  988. /* We are not setting the status, because ieee->state is not updated
  989. * at all and this driver doesn't track authentication state.
  990. */
  991. spin_lock_irq(&mac->lock);
  992. count = mac->stats_count < ZD_MAC_STATS_BUFFER_SIZE ?
  993. mac->stats_count : ZD_MAC_STATS_BUFFER_SIZE;
  994. qual_total = rssi_total = 0;
  995. for (i = 0; i < count; i++) {
  996. qual_total += mac->qual_buffer[i];
  997. rssi_total += mac->rssi_buffer[i];
  998. }
  999. spin_unlock_irq(&mac->lock);
  1000. iw_stats->qual.updated = IW_QUAL_NOISE_INVALID;
  1001. if (count > 0) {
  1002. iw_stats->qual.qual = qual_total / count;
  1003. iw_stats->qual.level = rssi_total / count;
  1004. iw_stats->qual.updated |=
  1005. IW_QUAL_QUAL_UPDATED|IW_QUAL_LEVEL_UPDATED;
  1006. } else {
  1007. iw_stats->qual.updated |=
  1008. IW_QUAL_QUAL_INVALID|IW_QUAL_LEVEL_INVALID;
  1009. }
  1010. /* TODO: update counter */
  1011. return iw_stats;
  1012. }
  1013. #define LINK_LED_WORK_DELAY HZ
  1014. static void link_led_handler(struct work_struct *work)
  1015. {
  1016. struct zd_mac *mac =
  1017. container_of(work, struct zd_mac, housekeeping.link_led_work.work);
  1018. struct zd_chip *chip = &mac->chip;
  1019. struct ieee80211softmac_device *sm = ieee80211_priv(mac->netdev);
  1020. int is_associated;
  1021. int r;
  1022. spin_lock_irq(&mac->lock);
  1023. is_associated = sm->associnfo.associated != 0;
  1024. spin_unlock_irq(&mac->lock);
  1025. r = zd_chip_control_leds(chip,
  1026. is_associated ? LED_ASSOCIATED : LED_SCANNING);
  1027. if (r)
  1028. dev_err(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r);
  1029. queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
  1030. LINK_LED_WORK_DELAY);
  1031. }
  1032. static void housekeeping_init(struct zd_mac *mac)
  1033. {
  1034. INIT_DELAYED_WORK(&mac->housekeeping.link_led_work, link_led_handler);
  1035. }
  1036. static void housekeeping_enable(struct zd_mac *mac)
  1037. {
  1038. dev_dbg_f(zd_mac_dev(mac), "\n");
  1039. queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
  1040. 0);
  1041. }
  1042. static void housekeeping_disable(struct zd_mac *mac)
  1043. {
  1044. dev_dbg_f(zd_mac_dev(mac), "\n");
  1045. cancel_rearming_delayed_workqueue(zd_workqueue,
  1046. &mac->housekeeping.link_led_work);
  1047. zd_chip_control_leds(&mac->chip, LED_OFF);
  1048. }