zd_mac.c 36 KB

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