zd_mac.c 32 KB

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