zd_mac.c 27 KB

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