zd_mac.c 35 KB

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