zd_mac.c 36 KB

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