zd_mac.c 35 KB

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