zd_mac.c 29 KB

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