zd_mac.c 27 KB

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