zd_mac.c 35 KB

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