PageRenderTime 973ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/net/wireless/zd1211rw/zd_mac.c

https://github.com/Mengqi/linux-2.6
C | 1531 lines | 1082 code | 249 blank | 200 comment | 155 complexity | 08e076584e4c8cc58e2c68c28a503028 MD5 | raw file
  1. /* ZD1211 USB-WLAN driver for Linux
  2. *
  3. * Copyright (C) 2005-2007 Ulrich Kunitz <kune@deine-taler.de>
  4. * Copyright (C) 2006-2007 Daniel Drake <dsd@gentoo.org>
  5. * Copyright (C) 2006-2007 Michael Wu <flamingice@sourmilk.net>
  6. * Copyright (C) 2007-2008 Luis R. Rodriguez <mcgrof@winlab.rutgers.edu>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <linux/netdevice.h>
  23. #include <linux/etherdevice.h>
  24. #include <linux/slab.h>
  25. #include <linux/usb.h>
  26. #include <linux/jiffies.h>
  27. #include <net/ieee80211_radiotap.h>
  28. #include "zd_def.h"
  29. #include "zd_chip.h"
  30. #include "zd_mac.h"
  31. #include "zd_rf.h"
  32. struct zd_reg_alpha2_map {
  33. u32 reg;
  34. char alpha2[2];
  35. };
  36. static struct zd_reg_alpha2_map reg_alpha2_map[] = {
  37. { ZD_REGDOMAIN_FCC, "US" },
  38. { ZD_REGDOMAIN_IC, "CA" },
  39. { ZD_REGDOMAIN_ETSI, "DE" }, /* Generic ETSI, use most restrictive */
  40. { ZD_REGDOMAIN_JAPAN, "JP" },
  41. { ZD_REGDOMAIN_JAPAN_2, "JP" },
  42. { ZD_REGDOMAIN_JAPAN_3, "JP" },
  43. { ZD_REGDOMAIN_SPAIN, "ES" },
  44. { ZD_REGDOMAIN_FRANCE, "FR" },
  45. };
  46. /* This table contains the hardware specific values for the modulation rates. */
  47. static const struct ieee80211_rate zd_rates[] = {
  48. { .bitrate = 10,
  49. .hw_value = ZD_CCK_RATE_1M, },
  50. { .bitrate = 20,
  51. .hw_value = ZD_CCK_RATE_2M,
  52. .hw_value_short = ZD_CCK_RATE_2M | ZD_CCK_PREA_SHORT,
  53. .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  54. { .bitrate = 55,
  55. .hw_value = ZD_CCK_RATE_5_5M,
  56. .hw_value_short = ZD_CCK_RATE_5_5M | ZD_CCK_PREA_SHORT,
  57. .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  58. { .bitrate = 110,
  59. .hw_value = ZD_CCK_RATE_11M,
  60. .hw_value_short = ZD_CCK_RATE_11M | ZD_CCK_PREA_SHORT,
  61. .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  62. { .bitrate = 60,
  63. .hw_value = ZD_OFDM_RATE_6M,
  64. .flags = 0 },
  65. { .bitrate = 90,
  66. .hw_value = ZD_OFDM_RATE_9M,
  67. .flags = 0 },
  68. { .bitrate = 120,
  69. .hw_value = ZD_OFDM_RATE_12M,
  70. .flags = 0 },
  71. { .bitrate = 180,
  72. .hw_value = ZD_OFDM_RATE_18M,
  73. .flags = 0 },
  74. { .bitrate = 240,
  75. .hw_value = ZD_OFDM_RATE_24M,
  76. .flags = 0 },
  77. { .bitrate = 360,
  78. .hw_value = ZD_OFDM_RATE_36M,
  79. .flags = 0 },
  80. { .bitrate = 480,
  81. .hw_value = ZD_OFDM_RATE_48M,
  82. .flags = 0 },
  83. { .bitrate = 540,
  84. .hw_value = ZD_OFDM_RATE_54M,
  85. .flags = 0 },
  86. };
  87. /*
  88. * Zydas retry rates table. Each line is listed in the same order as
  89. * in zd_rates[] and contains all the rate used when a packet is sent
  90. * starting with a given rates. Let's consider an example :
  91. *
  92. * "11 Mbits : 4, 3, 2, 1, 0" means :
  93. * - packet is sent using 4 different rates
  94. * - 1st rate is index 3 (ie 11 Mbits)
  95. * - 2nd rate is index 2 (ie 5.5 Mbits)
  96. * - 3rd rate is index 1 (ie 2 Mbits)
  97. * - 4th rate is index 0 (ie 1 Mbits)
  98. */
  99. static const struct tx_retry_rate zd_retry_rates[] = {
  100. { /* 1 Mbits */ 1, { 0 }},
  101. { /* 2 Mbits */ 2, { 1, 0 }},
  102. { /* 5.5 Mbits */ 3, { 2, 1, 0 }},
  103. { /* 11 Mbits */ 4, { 3, 2, 1, 0 }},
  104. { /* 6 Mbits */ 5, { 4, 3, 2, 1, 0 }},
  105. { /* 9 Mbits */ 6, { 5, 4, 3, 2, 1, 0}},
  106. { /* 12 Mbits */ 5, { 6, 3, 2, 1, 0 }},
  107. { /* 18 Mbits */ 6, { 7, 6, 3, 2, 1, 0 }},
  108. { /* 24 Mbits */ 6, { 8, 6, 3, 2, 1, 0 }},
  109. { /* 36 Mbits */ 7, { 9, 8, 6, 3, 2, 1, 0 }},
  110. { /* 48 Mbits */ 8, {10, 9, 8, 6, 3, 2, 1, 0 }},
  111. { /* 54 Mbits */ 9, {11, 10, 9, 8, 6, 3, 2, 1, 0 }}
  112. };
  113. static const struct ieee80211_channel zd_channels[] = {
  114. { .center_freq = 2412, .hw_value = 1 },
  115. { .center_freq = 2417, .hw_value = 2 },
  116. { .center_freq = 2422, .hw_value = 3 },
  117. { .center_freq = 2427, .hw_value = 4 },
  118. { .center_freq = 2432, .hw_value = 5 },
  119. { .center_freq = 2437, .hw_value = 6 },
  120. { .center_freq = 2442, .hw_value = 7 },
  121. { .center_freq = 2447, .hw_value = 8 },
  122. { .center_freq = 2452, .hw_value = 9 },
  123. { .center_freq = 2457, .hw_value = 10 },
  124. { .center_freq = 2462, .hw_value = 11 },
  125. { .center_freq = 2467, .hw_value = 12 },
  126. { .center_freq = 2472, .hw_value = 13 },
  127. { .center_freq = 2484, .hw_value = 14 },
  128. };
  129. static void housekeeping_init(struct zd_mac *mac);
  130. static void housekeeping_enable(struct zd_mac *mac);
  131. static void housekeeping_disable(struct zd_mac *mac);
  132. static void beacon_init(struct zd_mac *mac);
  133. static void beacon_enable(struct zd_mac *mac);
  134. static void beacon_disable(struct zd_mac *mac);
  135. static void set_rts_cts(struct zd_mac *mac, unsigned int short_preamble);
  136. static int zd_mac_config_beacon(struct ieee80211_hw *hw,
  137. struct sk_buff *beacon, bool in_intr);
  138. static int zd_reg2alpha2(u8 regdomain, char *alpha2)
  139. {
  140. unsigned int i;
  141. struct zd_reg_alpha2_map *reg_map;
  142. for (i = 0; i < ARRAY_SIZE(reg_alpha2_map); i++) {
  143. reg_map = &reg_alpha2_map[i];
  144. if (regdomain == reg_map->reg) {
  145. alpha2[0] = reg_map->alpha2[0];
  146. alpha2[1] = reg_map->alpha2[1];
  147. return 0;
  148. }
  149. }
  150. return 1;
  151. }
  152. static int zd_check_signal(struct ieee80211_hw *hw, int signal)
  153. {
  154. struct zd_mac *mac = zd_hw_mac(hw);
  155. dev_dbg_f_cond(zd_mac_dev(mac), signal < 0 || signal > 100,
  156. "%s: signal value from device not in range 0..100, "
  157. "but %d.\n", __func__, signal);
  158. if (signal < 0)
  159. signal = 0;
  160. else if (signal > 100)
  161. signal = 100;
  162. return signal;
  163. }
  164. int zd_mac_preinit_hw(struct ieee80211_hw *hw)
  165. {
  166. int r;
  167. u8 addr[ETH_ALEN];
  168. struct zd_mac *mac = zd_hw_mac(hw);
  169. r = zd_chip_read_mac_addr_fw(&mac->chip, addr);
  170. if (r)
  171. return r;
  172. SET_IEEE80211_PERM_ADDR(hw, addr);
  173. return 0;
  174. }
  175. int zd_mac_init_hw(struct ieee80211_hw *hw)
  176. {
  177. int r;
  178. struct zd_mac *mac = zd_hw_mac(hw);
  179. struct zd_chip *chip = &mac->chip;
  180. char alpha2[2];
  181. u8 default_regdomain;
  182. r = zd_chip_enable_int(chip);
  183. if (r)
  184. goto out;
  185. r = zd_chip_init_hw(chip);
  186. if (r)
  187. goto disable_int;
  188. ZD_ASSERT(!irqs_disabled());
  189. r = zd_read_regdomain(chip, &default_regdomain);
  190. if (r)
  191. goto disable_int;
  192. spin_lock_irq(&mac->lock);
  193. mac->regdomain = mac->default_regdomain = default_regdomain;
  194. spin_unlock_irq(&mac->lock);
  195. /* We must inform the device that we are doing encryption/decryption in
  196. * software at the moment. */
  197. r = zd_set_encryption_type(chip, ENC_SNIFFER);
  198. if (r)
  199. goto disable_int;
  200. r = zd_reg2alpha2(mac->regdomain, alpha2);
  201. if (r)
  202. goto disable_int;
  203. r = regulatory_hint(hw->wiphy, alpha2);
  204. disable_int:
  205. zd_chip_disable_int(chip);
  206. out:
  207. return r;
  208. }
  209. void zd_mac_clear(struct zd_mac *mac)
  210. {
  211. flush_workqueue(zd_workqueue);
  212. zd_chip_clear(&mac->chip);
  213. ZD_ASSERT(!spin_is_locked(&mac->lock));
  214. ZD_MEMCLEAR(mac, sizeof(struct zd_mac));
  215. }
  216. static int set_rx_filter(struct zd_mac *mac)
  217. {
  218. unsigned long flags;
  219. u32 filter = STA_RX_FILTER;
  220. spin_lock_irqsave(&mac->lock, flags);
  221. if (mac->pass_ctrl)
  222. filter |= RX_FILTER_CTRL;
  223. spin_unlock_irqrestore(&mac->lock, flags);
  224. return zd_iowrite32(&mac->chip, CR_RX_FILTER, filter);
  225. }
  226. static int set_mac_and_bssid(struct zd_mac *mac)
  227. {
  228. int r;
  229. if (!mac->vif)
  230. return -1;
  231. r = zd_write_mac_addr(&mac->chip, mac->vif->addr);
  232. if (r)
  233. return r;
  234. /* Vendor driver after setting MAC either sets BSSID for AP or
  235. * filter for other modes.
  236. */
  237. if (mac->type != NL80211_IFTYPE_AP)
  238. return set_rx_filter(mac);
  239. else
  240. return zd_write_bssid(&mac->chip, mac->vif->addr);
  241. }
  242. static int set_mc_hash(struct zd_mac *mac)
  243. {
  244. struct zd_mc_hash hash;
  245. zd_mc_clear(&hash);
  246. return zd_chip_set_multicast_hash(&mac->chip, &hash);
  247. }
  248. int zd_op_start(struct ieee80211_hw *hw)
  249. {
  250. struct zd_mac *mac = zd_hw_mac(hw);
  251. struct zd_chip *chip = &mac->chip;
  252. struct zd_usb *usb = &chip->usb;
  253. int r;
  254. if (!usb->initialized) {
  255. r = zd_usb_init_hw(usb);
  256. if (r)
  257. goto out;
  258. }
  259. r = zd_chip_enable_int(chip);
  260. if (r < 0)
  261. goto out;
  262. r = zd_chip_set_basic_rates(chip, CR_RATES_80211B | CR_RATES_80211G);
  263. if (r < 0)
  264. goto disable_int;
  265. r = set_rx_filter(mac);
  266. if (r)
  267. goto disable_int;
  268. r = set_mc_hash(mac);
  269. if (r)
  270. goto disable_int;
  271. r = zd_chip_switch_radio_on(chip);
  272. if (r < 0)
  273. goto disable_int;
  274. r = zd_chip_enable_rxtx(chip);
  275. if (r < 0)
  276. goto disable_radio;
  277. r = zd_chip_enable_hwint(chip);
  278. if (r < 0)
  279. goto disable_rxtx;
  280. housekeeping_enable(mac);
  281. beacon_enable(mac);
  282. set_bit(ZD_DEVICE_RUNNING, &mac->flags);
  283. return 0;
  284. disable_rxtx:
  285. zd_chip_disable_rxtx(chip);
  286. disable_radio:
  287. zd_chip_switch_radio_off(chip);
  288. disable_int:
  289. zd_chip_disable_int(chip);
  290. out:
  291. return r;
  292. }
  293. void zd_op_stop(struct ieee80211_hw *hw)
  294. {
  295. struct zd_mac *mac = zd_hw_mac(hw);
  296. struct zd_chip *chip = &mac->chip;
  297. struct sk_buff *skb;
  298. struct sk_buff_head *ack_wait_queue = &mac->ack_wait_queue;
  299. clear_bit(ZD_DEVICE_RUNNING, &mac->flags);
  300. /* The order here deliberately is a little different from the open()
  301. * method, since we need to make sure there is no opportunity for RX
  302. * frames to be processed by mac80211 after we have stopped it.
  303. */
  304. zd_chip_disable_rxtx(chip);
  305. beacon_disable(mac);
  306. housekeeping_disable(mac);
  307. flush_workqueue(zd_workqueue);
  308. zd_chip_disable_hwint(chip);
  309. zd_chip_switch_radio_off(chip);
  310. zd_chip_disable_int(chip);
  311. while ((skb = skb_dequeue(ack_wait_queue)))
  312. dev_kfree_skb_any(skb);
  313. }
  314. int zd_restore_settings(struct zd_mac *mac)
  315. {
  316. struct sk_buff *beacon;
  317. struct zd_mc_hash multicast_hash;
  318. unsigned int short_preamble;
  319. int r, beacon_interval, beacon_period;
  320. u8 channel;
  321. dev_dbg_f(zd_mac_dev(mac), "\n");
  322. spin_lock_irq(&mac->lock);
  323. multicast_hash = mac->multicast_hash;
  324. short_preamble = mac->short_preamble;
  325. beacon_interval = mac->beacon.interval;
  326. beacon_period = mac->beacon.period;
  327. channel = mac->channel;
  328. spin_unlock_irq(&mac->lock);
  329. r = set_mac_and_bssid(mac);
  330. if (r < 0) {
  331. dev_dbg_f(zd_mac_dev(mac), "set_mac_and_bssid failed, %d\n", r);
  332. return r;
  333. }
  334. r = zd_chip_set_channel(&mac->chip, channel);
  335. if (r < 0) {
  336. dev_dbg_f(zd_mac_dev(mac), "zd_chip_set_channel failed, %d\n",
  337. r);
  338. return r;
  339. }
  340. set_rts_cts(mac, short_preamble);
  341. r = zd_chip_set_multicast_hash(&mac->chip, &multicast_hash);
  342. if (r < 0) {
  343. dev_dbg_f(zd_mac_dev(mac),
  344. "zd_chip_set_multicast_hash failed, %d\n", r);
  345. return r;
  346. }
  347. if (mac->type == NL80211_IFTYPE_MESH_POINT ||
  348. mac->type == NL80211_IFTYPE_ADHOC ||
  349. mac->type == NL80211_IFTYPE_AP) {
  350. if (mac->vif != NULL) {
  351. beacon = ieee80211_beacon_get(mac->hw, mac->vif);
  352. if (beacon)
  353. zd_mac_config_beacon(mac->hw, beacon, false);
  354. }
  355. zd_set_beacon_interval(&mac->chip, beacon_interval,
  356. beacon_period, mac->type);
  357. spin_lock_irq(&mac->lock);
  358. mac->beacon.last_update = jiffies;
  359. spin_unlock_irq(&mac->lock);
  360. }
  361. return 0;
  362. }
  363. /**
  364. * zd_mac_tx_status - reports tx status of a packet if required
  365. * @hw - a &struct ieee80211_hw pointer
  366. * @skb - a sk-buffer
  367. * @flags: extra flags to set in the TX status info
  368. * @ackssi: ACK signal strength
  369. * @success - True for successful transmission of the frame
  370. *
  371. * This information calls ieee80211_tx_status_irqsafe() if required by the
  372. * control information. It copies the control information into the status
  373. * information.
  374. *
  375. * If no status information has been requested, the skb is freed.
  376. */
  377. static void zd_mac_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb,
  378. int ackssi, struct tx_status *tx_status)
  379. {
  380. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  381. int i;
  382. int success = 1, retry = 1;
  383. int first_idx;
  384. const struct tx_retry_rate *retries;
  385. ieee80211_tx_info_clear_status(info);
  386. if (tx_status) {
  387. success = !tx_status->failure;
  388. retry = tx_status->retry + success;
  389. }
  390. if (success) {
  391. /* success */
  392. info->flags |= IEEE80211_TX_STAT_ACK;
  393. } else {
  394. /* failure */
  395. info->flags &= ~IEEE80211_TX_STAT_ACK;
  396. }
  397. first_idx = info->status.rates[0].idx;
  398. ZD_ASSERT(0<=first_idx && first_idx<ARRAY_SIZE(zd_retry_rates));
  399. retries = &zd_retry_rates[first_idx];
  400. ZD_ASSERT(1 <= retry && retry <= retries->count);
  401. info->status.rates[0].idx = retries->rate[0];
  402. info->status.rates[0].count = 1; // (retry > 1 ? 2 : 1);
  403. for (i=1; i<IEEE80211_TX_MAX_RATES-1 && i<retry; i++) {
  404. info->status.rates[i].idx = retries->rate[i];
  405. info->status.rates[i].count = 1; // ((i==retry-1) && success ? 1:2);
  406. }
  407. for (; i<IEEE80211_TX_MAX_RATES && i<retry; i++) {
  408. info->status.rates[i].idx = retries->rate[retry - 1];
  409. info->status.rates[i].count = 1; // (success ? 1:2);
  410. }
  411. if (i<IEEE80211_TX_MAX_RATES)
  412. info->status.rates[i].idx = -1; /* terminate */
  413. info->status.ack_signal = zd_check_signal(hw, ackssi);
  414. ieee80211_tx_status_irqsafe(hw, skb);
  415. }
  416. /**
  417. * zd_mac_tx_failed - callback for failed frames
  418. * @dev: the mac80211 wireless device
  419. *
  420. * This function is called if a frame couldn't be successfully
  421. * transferred. The first frame from the tx queue, will be selected and
  422. * reported as error to the upper layers.
  423. */
  424. void zd_mac_tx_failed(struct urb *urb)
  425. {
  426. struct ieee80211_hw * hw = zd_usb_to_hw(urb->context);
  427. struct zd_mac *mac = zd_hw_mac(hw);
  428. struct sk_buff_head *q = &mac->ack_wait_queue;
  429. struct sk_buff *skb;
  430. struct tx_status *tx_status = (struct tx_status *)urb->transfer_buffer;
  431. unsigned long flags;
  432. int success = !tx_status->failure;
  433. int retry = tx_status->retry + success;
  434. int found = 0;
  435. int i, position = 0;
  436. q = &mac->ack_wait_queue;
  437. spin_lock_irqsave(&q->lock, flags);
  438. skb_queue_walk(q, skb) {
  439. struct ieee80211_hdr *tx_hdr;
  440. struct ieee80211_tx_info *info;
  441. int first_idx, final_idx;
  442. const struct tx_retry_rate *retries;
  443. u8 final_rate;
  444. position ++;
  445. /* if the hardware reports a failure and we had a 802.11 ACK
  446. * pending, then we skip the first skb when searching for a
  447. * matching frame */
  448. if (tx_status->failure && mac->ack_pending &&
  449. skb_queue_is_first(q, skb)) {
  450. continue;
  451. }
  452. tx_hdr = (struct ieee80211_hdr *)skb->data;
  453. /* we skip all frames not matching the reported destination */
  454. if (unlikely(memcmp(tx_hdr->addr1, tx_status->mac, ETH_ALEN))) {
  455. continue;
  456. }
  457. /* we skip all frames not matching the reported final rate */
  458. info = IEEE80211_SKB_CB(skb);
  459. first_idx = info->status.rates[0].idx;
  460. ZD_ASSERT(0<=first_idx && first_idx<ARRAY_SIZE(zd_retry_rates));
  461. retries = &zd_retry_rates[first_idx];
  462. if (retry <= 0 || retry > retries->count)
  463. continue;
  464. final_idx = retries->rate[retry - 1];
  465. final_rate = zd_rates[final_idx].hw_value;
  466. if (final_rate != tx_status->rate) {
  467. continue;
  468. }
  469. found = 1;
  470. break;
  471. }
  472. if (found) {
  473. for (i=1; i<=position; i++) {
  474. skb = __skb_dequeue(q);
  475. zd_mac_tx_status(hw, skb,
  476. mac->ack_pending ? mac->ack_signal : 0,
  477. i == position ? tx_status : NULL);
  478. mac->ack_pending = 0;
  479. }
  480. }
  481. spin_unlock_irqrestore(&q->lock, flags);
  482. }
  483. /**
  484. * zd_mac_tx_to_dev - callback for USB layer
  485. * @skb: a &sk_buff pointer
  486. * @error: error value, 0 if transmission successful
  487. *
  488. * Informs the MAC layer that the frame has successfully transferred to the
  489. * device. If an ACK is required and the transfer to the device has been
  490. * successful, the packets are put on the @ack_wait_queue with
  491. * the control set removed.
  492. */
  493. void zd_mac_tx_to_dev(struct sk_buff *skb, int error)
  494. {
  495. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  496. struct ieee80211_hw *hw = info->rate_driver_data[0];
  497. struct zd_mac *mac = zd_hw_mac(hw);
  498. ieee80211_tx_info_clear_status(info);
  499. skb_pull(skb, sizeof(struct zd_ctrlset));
  500. if (unlikely(error ||
  501. (info->flags & IEEE80211_TX_CTL_NO_ACK))) {
  502. /*
  503. * FIXME : do we need to fill in anything ?
  504. */
  505. ieee80211_tx_status_irqsafe(hw, skb);
  506. } else {
  507. struct sk_buff_head *q = &mac->ack_wait_queue;
  508. skb_queue_tail(q, skb);
  509. while (skb_queue_len(q) > ZD_MAC_MAX_ACK_WAITERS) {
  510. zd_mac_tx_status(hw, skb_dequeue(q),
  511. mac->ack_pending ? mac->ack_signal : 0,
  512. NULL);
  513. mac->ack_pending = 0;
  514. }
  515. }
  516. }
  517. static int zd_calc_tx_length_us(u8 *service, u8 zd_rate, u16 tx_length)
  518. {
  519. /* ZD_PURE_RATE() must be used to remove the modulation type flag of
  520. * the zd-rate values.
  521. */
  522. static const u8 rate_divisor[] = {
  523. [ZD_PURE_RATE(ZD_CCK_RATE_1M)] = 1,
  524. [ZD_PURE_RATE(ZD_CCK_RATE_2M)] = 2,
  525. /* Bits must be doubled. */
  526. [ZD_PURE_RATE(ZD_CCK_RATE_5_5M)] = 11,
  527. [ZD_PURE_RATE(ZD_CCK_RATE_11M)] = 11,
  528. [ZD_PURE_RATE(ZD_OFDM_RATE_6M)] = 6,
  529. [ZD_PURE_RATE(ZD_OFDM_RATE_9M)] = 9,
  530. [ZD_PURE_RATE(ZD_OFDM_RATE_12M)] = 12,
  531. [ZD_PURE_RATE(ZD_OFDM_RATE_18M)] = 18,
  532. [ZD_PURE_RATE(ZD_OFDM_RATE_24M)] = 24,
  533. [ZD_PURE_RATE(ZD_OFDM_RATE_36M)] = 36,
  534. [ZD_PURE_RATE(ZD_OFDM_RATE_48M)] = 48,
  535. [ZD_PURE_RATE(ZD_OFDM_RATE_54M)] = 54,
  536. };
  537. u32 bits = (u32)tx_length * 8;
  538. u32 divisor;
  539. divisor = rate_divisor[ZD_PURE_RATE(zd_rate)];
  540. if (divisor == 0)
  541. return -EINVAL;
  542. switch (zd_rate) {
  543. case ZD_CCK_RATE_5_5M:
  544. bits = (2*bits) + 10; /* round up to the next integer */
  545. break;
  546. case ZD_CCK_RATE_11M:
  547. if (service) {
  548. u32 t = bits % 11;
  549. *service &= ~ZD_PLCP_SERVICE_LENGTH_EXTENSION;
  550. if (0 < t && t <= 3) {
  551. *service |= ZD_PLCP_SERVICE_LENGTH_EXTENSION;
  552. }
  553. }
  554. bits += 10; /* round up to the next integer */
  555. break;
  556. }
  557. return bits/divisor;
  558. }
  559. static void cs_set_control(struct zd_mac *mac, struct zd_ctrlset *cs,
  560. struct ieee80211_hdr *header,
  561. struct ieee80211_tx_info *info)
  562. {
  563. /*
  564. * CONTROL TODO:
  565. * - if backoff needed, enable bit 0
  566. * - if burst (backoff not needed) disable bit 0
  567. */
  568. cs->control = 0;
  569. /* First fragment */
  570. if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
  571. cs->control |= ZD_CS_NEED_RANDOM_BACKOFF;
  572. /* No ACK expected (multicast, etc.) */
  573. if (info->flags & IEEE80211_TX_CTL_NO_ACK)
  574. cs->control |= ZD_CS_NO_ACK;
  575. /* PS-POLL */
  576. if (ieee80211_is_pspoll(header->frame_control))
  577. cs->control |= ZD_CS_PS_POLL_FRAME;
  578. if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS)
  579. cs->control |= ZD_CS_RTS;
  580. if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
  581. cs->control |= ZD_CS_SELF_CTS;
  582. /* FIXME: Management frame? */
  583. }
  584. static bool zd_mac_match_cur_beacon(struct zd_mac *mac, struct sk_buff *beacon)
  585. {
  586. if (!mac->beacon.cur_beacon)
  587. return false;
  588. if (mac->beacon.cur_beacon->len != beacon->len)
  589. return false;
  590. return !memcmp(beacon->data, mac->beacon.cur_beacon->data, beacon->len);
  591. }
  592. static void zd_mac_free_cur_beacon_locked(struct zd_mac *mac)
  593. {
  594. ZD_ASSERT(mutex_is_locked(&mac->chip.mutex));
  595. kfree_skb(mac->beacon.cur_beacon);
  596. mac->beacon.cur_beacon = NULL;
  597. }
  598. static void zd_mac_free_cur_beacon(struct zd_mac *mac)
  599. {
  600. mutex_lock(&mac->chip.mutex);
  601. zd_mac_free_cur_beacon_locked(mac);
  602. mutex_unlock(&mac->chip.mutex);
  603. }
  604. static int zd_mac_config_beacon(struct ieee80211_hw *hw, struct sk_buff *beacon,
  605. bool in_intr)
  606. {
  607. struct zd_mac *mac = zd_hw_mac(hw);
  608. int r, ret, num_cmds, req_pos = 0;
  609. u32 tmp, j = 0;
  610. /* 4 more bytes for tail CRC */
  611. u32 full_len = beacon->len + 4;
  612. unsigned long end_jiffies, message_jiffies;
  613. struct zd_ioreq32 *ioreqs;
  614. mutex_lock(&mac->chip.mutex);
  615. /* Check if hw already has this beacon. */
  616. if (zd_mac_match_cur_beacon(mac, beacon)) {
  617. r = 0;
  618. goto out_nofree;
  619. }
  620. /* Alloc memory for full beacon write at once. */
  621. num_cmds = 1 + zd_chip_is_zd1211b(&mac->chip) + full_len;
  622. ioreqs = kmalloc(num_cmds * sizeof(struct zd_ioreq32), GFP_KERNEL);
  623. if (!ioreqs) {
  624. r = -ENOMEM;
  625. goto out_nofree;
  626. }
  627. r = zd_iowrite32_locked(&mac->chip, 0, CR_BCN_FIFO_SEMAPHORE);
  628. if (r < 0)
  629. goto out;
  630. r = zd_ioread32_locked(&mac->chip, &tmp, CR_BCN_FIFO_SEMAPHORE);
  631. if (r < 0)
  632. goto release_sema;
  633. if (in_intr && tmp & 0x2) {
  634. r = -EBUSY;
  635. goto release_sema;
  636. }
  637. end_jiffies = jiffies + HZ / 2; /*~500ms*/
  638. message_jiffies = jiffies + HZ / 10; /*~100ms*/
  639. while (tmp & 0x2) {
  640. r = zd_ioread32_locked(&mac->chip, &tmp, CR_BCN_FIFO_SEMAPHORE);
  641. if (r < 0)
  642. goto release_sema;
  643. if (time_is_before_eq_jiffies(message_jiffies)) {
  644. message_jiffies = jiffies + HZ / 10;
  645. dev_err(zd_mac_dev(mac),
  646. "CR_BCN_FIFO_SEMAPHORE not ready\n");
  647. if (time_is_before_eq_jiffies(end_jiffies)) {
  648. dev_err(zd_mac_dev(mac),
  649. "Giving up beacon config.\n");
  650. r = -ETIMEDOUT;
  651. goto reset_device;
  652. }
  653. }
  654. msleep(20);
  655. }
  656. ioreqs[req_pos].addr = CR_BCN_FIFO;
  657. ioreqs[req_pos].value = full_len - 1;
  658. req_pos++;
  659. if (zd_chip_is_zd1211b(&mac->chip)) {
  660. ioreqs[req_pos].addr = CR_BCN_LENGTH;
  661. ioreqs[req_pos].value = full_len - 1;
  662. req_pos++;
  663. }
  664. for (j = 0 ; j < beacon->len; j++) {
  665. ioreqs[req_pos].addr = CR_BCN_FIFO;
  666. ioreqs[req_pos].value = *((u8 *)(beacon->data + j));
  667. req_pos++;
  668. }
  669. for (j = 0; j < 4; j++) {
  670. ioreqs[req_pos].addr = CR_BCN_FIFO;
  671. ioreqs[req_pos].value = 0x0;
  672. req_pos++;
  673. }
  674. BUG_ON(req_pos != num_cmds);
  675. r = zd_iowrite32a_locked(&mac->chip, ioreqs, num_cmds);
  676. release_sema:
  677. /*
  678. * Try very hard to release device beacon semaphore, as otherwise
  679. * device/driver can be left in unusable state.
  680. */
  681. end_jiffies = jiffies + HZ / 2; /*~500ms*/
  682. ret = zd_iowrite32_locked(&mac->chip, 1, CR_BCN_FIFO_SEMAPHORE);
  683. while (ret < 0) {
  684. if (in_intr || time_is_before_eq_jiffies(end_jiffies)) {
  685. ret = -ETIMEDOUT;
  686. break;
  687. }
  688. msleep(20);
  689. ret = zd_iowrite32_locked(&mac->chip, 1, CR_BCN_FIFO_SEMAPHORE);
  690. }
  691. if (ret < 0)
  692. dev_err(zd_mac_dev(mac), "Could not release "
  693. "CR_BCN_FIFO_SEMAPHORE!\n");
  694. if (r < 0 || ret < 0) {
  695. if (r >= 0)
  696. r = ret;
  697. /* We don't know if beacon was written successfully or not,
  698. * so clear current. */
  699. zd_mac_free_cur_beacon_locked(mac);
  700. goto out;
  701. }
  702. /* Beacon has now been written successfully, update current. */
  703. zd_mac_free_cur_beacon_locked(mac);
  704. mac->beacon.cur_beacon = beacon;
  705. beacon = NULL;
  706. /* 802.11b/g 2.4G CCK 1Mb
  707. * 802.11a, not yet implemented, uses different values (see GPL vendor
  708. * driver)
  709. */
  710. r = zd_iowrite32_locked(&mac->chip, 0x00000400 | (full_len << 19),
  711. CR_BCN_PLCP_CFG);
  712. out:
  713. kfree(ioreqs);
  714. out_nofree:
  715. kfree_skb(beacon);
  716. mutex_unlock(&mac->chip.mutex);
  717. return r;
  718. reset_device:
  719. zd_mac_free_cur_beacon_locked(mac);
  720. kfree_skb(beacon);
  721. mutex_unlock(&mac->chip.mutex);
  722. kfree(ioreqs);
  723. /* semaphore stuck, reset device to avoid fw freeze later */
  724. dev_warn(zd_mac_dev(mac), "CR_BCN_FIFO_SEMAPHORE stuck, "
  725. "reseting device...");
  726. usb_queue_reset_device(mac->chip.usb.intf);
  727. return r;
  728. }
  729. static int fill_ctrlset(struct zd_mac *mac,
  730. struct sk_buff *skb)
  731. {
  732. int r;
  733. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  734. unsigned int frag_len = skb->len + FCS_LEN;
  735. unsigned int packet_length;
  736. struct ieee80211_rate *txrate;
  737. struct zd_ctrlset *cs = (struct zd_ctrlset *)
  738. skb_push(skb, sizeof(struct zd_ctrlset));
  739. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  740. ZD_ASSERT(frag_len <= 0xffff);
  741. txrate = ieee80211_get_tx_rate(mac->hw, info);
  742. cs->modulation = txrate->hw_value;
  743. if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
  744. cs->modulation = txrate->hw_value_short;
  745. cs->tx_length = cpu_to_le16(frag_len);
  746. cs_set_control(mac, cs, hdr, info);
  747. packet_length = frag_len + sizeof(struct zd_ctrlset) + 10;
  748. ZD_ASSERT(packet_length <= 0xffff);
  749. /* ZD1211B: Computing the length difference this way, gives us
  750. * flexibility to compute the packet length.
  751. */
  752. cs->packet_length = cpu_to_le16(zd_chip_is_zd1211b(&mac->chip) ?
  753. packet_length - frag_len : packet_length);
  754. /*
  755. * CURRENT LENGTH:
  756. * - transmit frame length in microseconds
  757. * - seems to be derived from frame length
  758. * - see Cal_Us_Service() in zdinlinef.h
  759. * - if macp->bTxBurstEnable is enabled, then multiply by 4
  760. * - bTxBurstEnable is never set in the vendor driver
  761. *
  762. * SERVICE:
  763. * - "for PLCP configuration"
  764. * - always 0 except in some situations at 802.11b 11M
  765. * - see line 53 of zdinlinef.h
  766. */
  767. cs->service = 0;
  768. r = zd_calc_tx_length_us(&cs->service, ZD_RATE(cs->modulation),
  769. le16_to_cpu(cs->tx_length));
  770. if (r < 0)
  771. return r;
  772. cs->current_length = cpu_to_le16(r);
  773. cs->next_frame_length = 0;
  774. return 0;
  775. }
  776. /**
  777. * zd_op_tx - transmits a network frame to the device
  778. *
  779. * @dev: mac80211 hardware device
  780. * @skb: socket buffer
  781. * @control: the control structure
  782. *
  783. * This function transmit an IEEE 802.11 network frame to the device. The
  784. * control block of the skbuff will be initialized. If necessary the incoming
  785. * mac80211 queues will be stopped.
  786. */
  787. static void zd_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
  788. {
  789. struct zd_mac *mac = zd_hw_mac(hw);
  790. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  791. int r;
  792. r = fill_ctrlset(mac, skb);
  793. if (r)
  794. goto fail;
  795. info->rate_driver_data[0] = hw;
  796. r = zd_usb_tx(&mac->chip.usb, skb);
  797. if (r)
  798. goto fail;
  799. return;
  800. fail:
  801. dev_kfree_skb(skb);
  802. }
  803. /**
  804. * filter_ack - filters incoming packets for acknowledgements
  805. * @dev: the mac80211 device
  806. * @rx_hdr: received header
  807. * @stats: the status for the received packet
  808. *
  809. * This functions looks for ACK packets and tries to match them with the
  810. * frames in the tx queue. If a match is found the frame will be dequeued and
  811. * the upper layers is informed about the successful transmission. If
  812. * mac80211 queues have been stopped and the number of frames still to be
  813. * transmitted is low the queues will be opened again.
  814. *
  815. * Returns 1 if the frame was an ACK, 0 if it was ignored.
  816. */
  817. static int filter_ack(struct ieee80211_hw *hw, struct ieee80211_hdr *rx_hdr,
  818. struct ieee80211_rx_status *stats)
  819. {
  820. struct zd_mac *mac = zd_hw_mac(hw);
  821. struct sk_buff *skb;
  822. struct sk_buff_head *q;
  823. unsigned long flags;
  824. int found = 0;
  825. int i, position = 0;
  826. if (!ieee80211_is_ack(rx_hdr->frame_control))
  827. return 0;
  828. q = &mac->ack_wait_queue;
  829. spin_lock_irqsave(&q->lock, flags);
  830. skb_queue_walk(q, skb) {
  831. struct ieee80211_hdr *tx_hdr;
  832. position ++;
  833. if (mac->ack_pending && skb_queue_is_first(q, skb))
  834. continue;
  835. tx_hdr = (struct ieee80211_hdr *)skb->data;
  836. if (likely(!memcmp(tx_hdr->addr2, rx_hdr->addr1, ETH_ALEN)))
  837. {
  838. found = 1;
  839. break;
  840. }
  841. }
  842. if (found) {
  843. for (i=1; i<position; i++) {
  844. skb = __skb_dequeue(q);
  845. zd_mac_tx_status(hw, skb,
  846. mac->ack_pending ? mac->ack_signal : 0,
  847. NULL);
  848. mac->ack_pending = 0;
  849. }
  850. mac->ack_pending = 1;
  851. mac->ack_signal = stats->signal;
  852. /* Prevent pending tx-packet on AP-mode */
  853. if (mac->type == NL80211_IFTYPE_AP) {
  854. skb = __skb_dequeue(q);
  855. zd_mac_tx_status(hw, skb, mac->ack_signal, NULL);
  856. mac->ack_pending = 0;
  857. }
  858. }
  859. spin_unlock_irqrestore(&q->lock, flags);
  860. return 1;
  861. }
  862. int zd_mac_rx(struct ieee80211_hw *hw, const u8 *buffer, unsigned int length)
  863. {
  864. struct zd_mac *mac = zd_hw_mac(hw);
  865. struct ieee80211_rx_status stats;
  866. const struct rx_status *status;
  867. struct sk_buff *skb;
  868. int bad_frame = 0;
  869. __le16 fc;
  870. int need_padding;
  871. int i;
  872. u8 rate;
  873. if (length < ZD_PLCP_HEADER_SIZE + 10 /* IEEE80211_1ADDR_LEN */ +
  874. FCS_LEN + sizeof(struct rx_status))
  875. return -EINVAL;
  876. memset(&stats, 0, sizeof(stats));
  877. /* Note about pass_failed_fcs and pass_ctrl access below:
  878. * mac locking intentionally omitted here, as this is the only unlocked
  879. * reader and the only writer is configure_filter. Plus, if there were
  880. * any races accessing these variables, it wouldn't really matter.
  881. * If mac80211 ever provides a way for us to access filter flags
  882. * from outside configure_filter, we could improve on this. Also, this
  883. * situation may change once we implement some kind of DMA-into-skb
  884. * RX path. */
  885. /* Caller has to ensure that length >= sizeof(struct rx_status). */
  886. status = (struct rx_status *)
  887. (buffer + (length - sizeof(struct rx_status)));
  888. if (status->frame_status & ZD_RX_ERROR) {
  889. if (mac->pass_failed_fcs &&
  890. (status->frame_status & ZD_RX_CRC32_ERROR)) {
  891. stats.flag |= RX_FLAG_FAILED_FCS_CRC;
  892. bad_frame = 1;
  893. } else {
  894. return -EINVAL;
  895. }
  896. }
  897. stats.freq = zd_channels[_zd_chip_get_channel(&mac->chip) - 1].center_freq;
  898. stats.band = IEEE80211_BAND_2GHZ;
  899. stats.signal = zd_check_signal(hw, status->signal_strength);
  900. rate = zd_rx_rate(buffer, status);
  901. /* todo: return index in the big switches in zd_rx_rate instead */
  902. for (i = 0; i < mac->band.n_bitrates; i++)
  903. if (rate == mac->band.bitrates[i].hw_value)
  904. stats.rate_idx = i;
  905. length -= ZD_PLCP_HEADER_SIZE + sizeof(struct rx_status);
  906. buffer += ZD_PLCP_HEADER_SIZE;
  907. /* Except for bad frames, filter each frame to see if it is an ACK, in
  908. * which case our internal TX tracking is updated. Normally we then
  909. * bail here as there's no need to pass ACKs on up to the stack, but
  910. * there is also the case where the stack has requested us to pass
  911. * control frames on up (pass_ctrl) which we must consider. */
  912. if (!bad_frame &&
  913. filter_ack(hw, (struct ieee80211_hdr *)buffer, &stats)
  914. && !mac->pass_ctrl)
  915. return 0;
  916. fc = get_unaligned((__le16*)buffer);
  917. need_padding = ieee80211_is_data_qos(fc) ^ ieee80211_has_a4(fc);
  918. skb = dev_alloc_skb(length + (need_padding ? 2 : 0));
  919. if (skb == NULL)
  920. return -ENOMEM;
  921. if (need_padding) {
  922. /* Make sure the payload data is 4 byte aligned. */
  923. skb_reserve(skb, 2);
  924. }
  925. /* FIXME : could we avoid this big memcpy ? */
  926. memcpy(skb_put(skb, length), buffer, length);
  927. memcpy(IEEE80211_SKB_RXCB(skb), &stats, sizeof(stats));
  928. ieee80211_rx_irqsafe(hw, skb);
  929. return 0;
  930. }
  931. static int zd_op_add_interface(struct ieee80211_hw *hw,
  932. struct ieee80211_vif *vif)
  933. {
  934. struct zd_mac *mac = zd_hw_mac(hw);
  935. /* using NL80211_IFTYPE_UNSPECIFIED to indicate no mode selected */
  936. if (mac->type != NL80211_IFTYPE_UNSPECIFIED)
  937. return -EOPNOTSUPP;
  938. switch (vif->type) {
  939. case NL80211_IFTYPE_MONITOR:
  940. case NL80211_IFTYPE_MESH_POINT:
  941. case NL80211_IFTYPE_STATION:
  942. case NL80211_IFTYPE_ADHOC:
  943. case NL80211_IFTYPE_AP:
  944. mac->type = vif->type;
  945. break;
  946. default:
  947. return -EOPNOTSUPP;
  948. }
  949. mac->vif = vif;
  950. return set_mac_and_bssid(mac);
  951. }
  952. static void zd_op_remove_interface(struct ieee80211_hw *hw,
  953. struct ieee80211_vif *vif)
  954. {
  955. struct zd_mac *mac = zd_hw_mac(hw);
  956. mac->type = NL80211_IFTYPE_UNSPECIFIED;
  957. mac->vif = NULL;
  958. zd_set_beacon_interval(&mac->chip, 0, 0, NL80211_IFTYPE_UNSPECIFIED);
  959. zd_write_mac_addr(&mac->chip, NULL);
  960. zd_mac_free_cur_beacon(mac);
  961. }
  962. static int zd_op_config(struct ieee80211_hw *hw, u32 changed)
  963. {
  964. struct zd_mac *mac = zd_hw_mac(hw);
  965. struct ieee80211_conf *conf = &hw->conf;
  966. spin_lock_irq(&mac->lock);
  967. mac->channel = conf->channel->hw_value;
  968. spin_unlock_irq(&mac->lock);
  969. return zd_chip_set_channel(&mac->chip, conf->channel->hw_value);
  970. }
  971. static void zd_beacon_done(struct zd_mac *mac)
  972. {
  973. struct sk_buff *skb, *beacon;
  974. if (!test_bit(ZD_DEVICE_RUNNING, &mac->flags))
  975. return;
  976. if (!mac->vif || mac->vif->type != NL80211_IFTYPE_AP)
  977. return;
  978. /*
  979. * Send out buffered broad- and multicast frames.
  980. */
  981. while (!ieee80211_queue_stopped(mac->hw, 0)) {
  982. skb = ieee80211_get_buffered_bc(mac->hw, mac->vif);
  983. if (!skb)
  984. break;
  985. zd_op_tx(mac->hw, skb);
  986. }
  987. /*
  988. * Fetch next beacon so that tim_count is updated.
  989. */
  990. beacon = ieee80211_beacon_get(mac->hw, mac->vif);
  991. if (beacon)
  992. zd_mac_config_beacon(mac->hw, beacon, true);
  993. spin_lock_irq(&mac->lock);
  994. mac->beacon.last_update = jiffies;
  995. spin_unlock_irq(&mac->lock);
  996. }
  997. static void zd_process_intr(struct work_struct *work)
  998. {
  999. u16 int_status;
  1000. unsigned long flags;
  1001. struct zd_mac *mac = container_of(work, struct zd_mac, process_intr);
  1002. spin_lock_irqsave(&mac->lock, flags);
  1003. int_status = le16_to_cpu(*(__le16 *)(mac->intr_buffer + 4));
  1004. spin_unlock_irqrestore(&mac->lock, flags);
  1005. if (int_status & INT_CFG_NEXT_BCN) {
  1006. /*dev_dbg_f_limit(zd_mac_dev(mac), "INT_CFG_NEXT_BCN\n");*/
  1007. zd_beacon_done(mac);
  1008. } else {
  1009. dev_dbg_f(zd_mac_dev(mac), "Unsupported interrupt\n");
  1010. }
  1011. zd_chip_enable_hwint(&mac->chip);
  1012. }
  1013. static u64 zd_op_prepare_multicast(struct ieee80211_hw *hw,
  1014. struct netdev_hw_addr_list *mc_list)
  1015. {
  1016. struct zd_mac *mac = zd_hw_mac(hw);
  1017. struct zd_mc_hash hash;
  1018. struct netdev_hw_addr *ha;
  1019. zd_mc_clear(&hash);
  1020. netdev_hw_addr_list_for_each(ha, mc_list) {
  1021. dev_dbg_f(zd_mac_dev(mac), "mc addr %pM\n", ha->addr);
  1022. zd_mc_add_addr(&hash, ha->addr);
  1023. }
  1024. return hash.low | ((u64)hash.high << 32);
  1025. }
  1026. #define SUPPORTED_FIF_FLAGS \
  1027. (FIF_PROMISC_IN_BSS | FIF_ALLMULTI | FIF_FCSFAIL | FIF_CONTROL | \
  1028. FIF_OTHER_BSS | FIF_BCN_PRBRESP_PROMISC)
  1029. static void zd_op_configure_filter(struct ieee80211_hw *hw,
  1030. unsigned int changed_flags,
  1031. unsigned int *new_flags,
  1032. u64 multicast)
  1033. {
  1034. struct zd_mc_hash hash = {
  1035. .low = multicast,
  1036. .high = multicast >> 32,
  1037. };
  1038. struct zd_mac *mac = zd_hw_mac(hw);
  1039. unsigned long flags;
  1040. int r;
  1041. /* Only deal with supported flags */
  1042. changed_flags &= SUPPORTED_FIF_FLAGS;
  1043. *new_flags &= SUPPORTED_FIF_FLAGS;
  1044. /*
  1045. * If multicast parameter (as returned by zd_op_prepare_multicast)
  1046. * has changed, no bit in changed_flags is set. To handle this
  1047. * situation, we do not return if changed_flags is 0. If we do so,
  1048. * we will have some issue with IPv6 which uses multicast for link
  1049. * layer address resolution.
  1050. */
  1051. if (*new_flags & (FIF_PROMISC_IN_BSS | FIF_ALLMULTI))
  1052. zd_mc_add_all(&hash);
  1053. spin_lock_irqsave(&mac->lock, flags);
  1054. mac->pass_failed_fcs = !!(*new_flags & FIF_FCSFAIL);
  1055. mac->pass_ctrl = !!(*new_flags & FIF_CONTROL);
  1056. mac->multicast_hash = hash;
  1057. spin_unlock_irqrestore(&mac->lock, flags);
  1058. zd_chip_set_multicast_hash(&mac->chip, &hash);
  1059. if (changed_flags & FIF_CONTROL) {
  1060. r = set_rx_filter(mac);
  1061. if (r)
  1062. dev_err(zd_mac_dev(mac), "set_rx_filter error %d\n", r);
  1063. }
  1064. /* no handling required for FIF_OTHER_BSS as we don't currently
  1065. * do BSSID filtering */
  1066. /* FIXME: in future it would be nice to enable the probe response
  1067. * filter (so that the driver doesn't see them) until
  1068. * FIF_BCN_PRBRESP_PROMISC is set. however due to atomicity here, we'd
  1069. * have to schedule work to enable prbresp reception, which might
  1070. * happen too late. For now we'll just listen and forward them all the
  1071. * time. */
  1072. }
  1073. static void set_rts_cts(struct zd_mac *mac, unsigned int short_preamble)
  1074. {
  1075. mutex_lock(&mac->chip.mutex);
  1076. zd_chip_set_rts_cts_rate_locked(&mac->chip, short_preamble);
  1077. mutex_unlock(&mac->chip.mutex);
  1078. }
  1079. static void zd_op_bss_info_changed(struct ieee80211_hw *hw,
  1080. struct ieee80211_vif *vif,
  1081. struct ieee80211_bss_conf *bss_conf,
  1082. u32 changes)
  1083. {
  1084. struct zd_mac *mac = zd_hw_mac(hw);
  1085. int associated;
  1086. dev_dbg_f(zd_mac_dev(mac), "changes: %x\n", changes);
  1087. if (mac->type == NL80211_IFTYPE_MESH_POINT ||
  1088. mac->type == NL80211_IFTYPE_ADHOC ||
  1089. mac->type == NL80211_IFTYPE_AP) {
  1090. associated = true;
  1091. if (changes & BSS_CHANGED_BEACON) {
  1092. struct sk_buff *beacon = ieee80211_beacon_get(hw, vif);
  1093. if (beacon) {
  1094. zd_chip_disable_hwint(&mac->chip);
  1095. zd_mac_config_beacon(hw, beacon, false);
  1096. zd_chip_enable_hwint(&mac->chip);
  1097. }
  1098. }
  1099. if (changes & BSS_CHANGED_BEACON_ENABLED) {
  1100. u16 interval = 0;
  1101. u8 period = 0;
  1102. if (bss_conf->enable_beacon) {
  1103. period = bss_conf->dtim_period;
  1104. interval = bss_conf->beacon_int;
  1105. }
  1106. spin_lock_irq(&mac->lock);
  1107. mac->beacon.period = period;
  1108. mac->beacon.interval = interval;
  1109. mac->beacon.last_update = jiffies;
  1110. spin_unlock_irq(&mac->lock);
  1111. zd_set_beacon_interval(&mac->chip, interval, period,
  1112. mac->type);
  1113. }
  1114. } else
  1115. associated = is_valid_ether_addr(bss_conf->bssid);
  1116. spin_lock_irq(&mac->lock);
  1117. mac->associated = associated;
  1118. spin_unlock_irq(&mac->lock);
  1119. /* TODO: do hardware bssid filtering */
  1120. if (changes & BSS_CHANGED_ERP_PREAMBLE) {
  1121. spin_lock_irq(&mac->lock);
  1122. mac->short_preamble = bss_conf->use_short_preamble;
  1123. spin_unlock_irq(&mac->lock);
  1124. set_rts_cts(mac, bss_conf->use_short_preamble);
  1125. }
  1126. }
  1127. static u64 zd_op_get_tsf(struct ieee80211_hw *hw)
  1128. {
  1129. struct zd_mac *mac = zd_hw_mac(hw);
  1130. return zd_chip_get_tsf(&mac->chip);
  1131. }
  1132. static const struct ieee80211_ops zd_ops = {
  1133. .tx = zd_op_tx,
  1134. .start = zd_op_start,
  1135. .stop = zd_op_stop,
  1136. .add_interface = zd_op_add_interface,
  1137. .remove_interface = zd_op_remove_interface,
  1138. .config = zd_op_config,
  1139. .prepare_multicast = zd_op_prepare_multicast,
  1140. .configure_filter = zd_op_configure_filter,
  1141. .bss_info_changed = zd_op_bss_info_changed,
  1142. .get_tsf = zd_op_get_tsf,
  1143. };
  1144. struct ieee80211_hw *zd_mac_alloc_hw(struct usb_interface *intf)
  1145. {
  1146. struct zd_mac *mac;
  1147. struct ieee80211_hw *hw;
  1148. hw = ieee80211_alloc_hw(sizeof(struct zd_mac), &zd_ops);
  1149. if (!hw) {
  1150. dev_dbg_f(&intf->dev, "out of memory\n");
  1151. return NULL;
  1152. }
  1153. mac = zd_hw_mac(hw);
  1154. memset(mac, 0, sizeof(*mac));
  1155. spin_lock_init(&mac->lock);
  1156. mac->hw = hw;
  1157. mac->type = NL80211_IFTYPE_UNSPECIFIED;
  1158. memcpy(mac->channels, zd_channels, sizeof(zd_channels));
  1159. memcpy(mac->rates, zd_rates, sizeof(zd_rates));
  1160. mac->band.n_bitrates = ARRAY_SIZE(zd_rates);
  1161. mac->band.bitrates = mac->rates;
  1162. mac->band.n_channels = ARRAY_SIZE(zd_channels);
  1163. mac->band.channels = mac->channels;
  1164. hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &mac->band;
  1165. hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
  1166. IEEE80211_HW_SIGNAL_UNSPEC |
  1167. IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING;
  1168. hw->wiphy->interface_modes =
  1169. BIT(NL80211_IFTYPE_MESH_POINT) |
  1170. BIT(NL80211_IFTYPE_STATION) |
  1171. BIT(NL80211_IFTYPE_ADHOC) |
  1172. BIT(NL80211_IFTYPE_AP);
  1173. hw->max_signal = 100;
  1174. hw->queues = 1;
  1175. hw->extra_tx_headroom = sizeof(struct zd_ctrlset);
  1176. /*
  1177. * Tell mac80211 that we support multi rate retries
  1178. */
  1179. hw->max_rates = IEEE80211_TX_MAX_RATES;
  1180. hw->max_rate_tries = 18; /* 9 rates * 2 retries/rate */
  1181. skb_queue_head_init(&mac->ack_wait_queue);
  1182. mac->ack_pending = 0;
  1183. zd_chip_init(&mac->chip, hw, intf);
  1184. housekeeping_init(mac);
  1185. beacon_init(mac);
  1186. INIT_WORK(&mac->process_intr, zd_process_intr);
  1187. SET_IEEE80211_DEV(hw, &intf->dev);
  1188. return hw;
  1189. }
  1190. #define BEACON_WATCHDOG_DELAY round_jiffies_relative(HZ)
  1191. static void beacon_watchdog_handler(struct work_struct *work)
  1192. {
  1193. struct zd_mac *mac =
  1194. container_of(work, struct zd_mac, beacon.watchdog_work.work);
  1195. struct sk_buff *beacon;
  1196. unsigned long timeout;
  1197. int interval, period;
  1198. if (!test_bit(ZD_DEVICE_RUNNING, &mac->flags))
  1199. goto rearm;
  1200. if (mac->type != NL80211_IFTYPE_AP || !mac->vif)
  1201. goto rearm;
  1202. spin_lock_irq(&mac->lock);
  1203. interval = mac->beacon.interval;
  1204. period = mac->beacon.period;
  1205. timeout = mac->beacon.last_update +
  1206. msecs_to_jiffies(interval * 1024 / 1000) * 3;
  1207. spin_unlock_irq(&mac->lock);
  1208. if (interval > 0 && time_is_before_jiffies(timeout)) {
  1209. dev_dbg_f(zd_mac_dev(mac), "beacon interrupt stalled, "
  1210. "restarting. "
  1211. "(interval: %d, dtim: %d)\n",
  1212. interval, period);
  1213. zd_chip_disable_hwint(&mac->chip);
  1214. beacon = ieee80211_beacon_get(mac->hw, mac->vif);
  1215. if (beacon) {
  1216. zd_mac_free_cur_beacon(mac);
  1217. zd_mac_config_beacon(mac->hw, beacon, false);
  1218. }
  1219. zd_set_beacon_interval(&mac->chip, interval, period, mac->type);
  1220. zd_chip_enable_hwint(&mac->chip);
  1221. spin_lock_irq(&mac->lock);
  1222. mac->beacon.last_update = jiffies;
  1223. spin_unlock_irq(&mac->lock);
  1224. }
  1225. rearm:
  1226. queue_delayed_work(zd_workqueue, &mac->beacon.watchdog_work,
  1227. BEACON_WATCHDOG_DELAY);
  1228. }
  1229. static void beacon_init(struct zd_mac *mac)
  1230. {
  1231. INIT_DELAYED_WORK(&mac->beacon.watchdog_work, beacon_watchdog_handler);
  1232. }
  1233. static void beacon_enable(struct zd_mac *mac)
  1234. {
  1235. dev_dbg_f(zd_mac_dev(mac), "\n");
  1236. mac->beacon.last_update = jiffies;
  1237. queue_delayed_work(zd_workqueue, &mac->beacon.watchdog_work,
  1238. BEACON_WATCHDOG_DELAY);
  1239. }
  1240. static void beacon_disable(struct zd_mac *mac)
  1241. {
  1242. dev_dbg_f(zd_mac_dev(mac), "\n");
  1243. cancel_delayed_work_sync(&mac->beacon.watchdog_work);
  1244. zd_mac_free_cur_beacon(mac);
  1245. }
  1246. #define LINK_LED_WORK_DELAY HZ
  1247. static void link_led_handler(struct work_struct *work)
  1248. {
  1249. struct zd_mac *mac =
  1250. container_of(work, struct zd_mac, housekeeping.link_led_work.work);
  1251. struct zd_chip *chip = &mac->chip;
  1252. int is_associated;
  1253. int r;
  1254. if (!test_bit(ZD_DEVICE_RUNNING, &mac->flags))
  1255. goto requeue;
  1256. spin_lock_irq(&mac->lock);
  1257. is_associated = mac->associated;
  1258. spin_unlock_irq(&mac->lock);
  1259. r = zd_chip_control_leds(chip,
  1260. is_associated ? ZD_LED_ASSOCIATED : ZD_LED_SCANNING);
  1261. if (r)
  1262. dev_dbg_f(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r);
  1263. requeue:
  1264. queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
  1265. LINK_LED_WORK_DELAY);
  1266. }
  1267. static void housekeeping_init(struct zd_mac *mac)
  1268. {
  1269. INIT_DELAYED_WORK(&mac->housekeeping.link_led_work, link_led_handler);
  1270. }
  1271. static void housekeeping_enable(struct zd_mac *mac)
  1272. {
  1273. dev_dbg_f(zd_mac_dev(mac), "\n");
  1274. queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
  1275. 0);
  1276. }
  1277. static void housekeeping_disable(struct zd_mac *mac)
  1278. {
  1279. dev_dbg_f(zd_mac_dev(mac), "\n");
  1280. cancel_delayed_work_sync(&mac->housekeeping.link_led_work);
  1281. zd_chip_control_leds(&mac->chip, ZD_LED_OFF);
  1282. }