zd_mac.c 27 KB

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