zd_mac.c 28 KB

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