zd_mac.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  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 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/usb.h>
  25. #include <linux/jiffies.h>
  26. #include <net/ieee80211_radiotap.h>
  27. #include "zd_def.h"
  28. #include "zd_chip.h"
  29. #include "zd_mac.h"
  30. #include "zd_ieee80211.h"
  31. #include "zd_rf.h"
  32. /* This table contains the hardware specific values for the modulation rates. */
  33. static const struct ieee80211_rate zd_rates[] = {
  34. { .bitrate = 10,
  35. .hw_value = ZD_CCK_RATE_1M, },
  36. { .bitrate = 20,
  37. .hw_value = ZD_CCK_RATE_2M,
  38. .hw_value_short = ZD_CCK_RATE_2M | ZD_CCK_PREA_SHORT,
  39. .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  40. { .bitrate = 55,
  41. .hw_value = ZD_CCK_RATE_5_5M,
  42. .hw_value_short = ZD_CCK_RATE_5_5M | ZD_CCK_PREA_SHORT,
  43. .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  44. { .bitrate = 110,
  45. .hw_value = ZD_CCK_RATE_11M,
  46. .hw_value_short = ZD_CCK_RATE_11M | ZD_CCK_PREA_SHORT,
  47. .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  48. { .bitrate = 60,
  49. .hw_value = ZD_OFDM_RATE_6M,
  50. .flags = 0 },
  51. { .bitrate = 90,
  52. .hw_value = ZD_OFDM_RATE_9M,
  53. .flags = 0 },
  54. { .bitrate = 120,
  55. .hw_value = ZD_OFDM_RATE_12M,
  56. .flags = 0 },
  57. { .bitrate = 180,
  58. .hw_value = ZD_OFDM_RATE_18M,
  59. .flags = 0 },
  60. { .bitrate = 240,
  61. .hw_value = ZD_OFDM_RATE_24M,
  62. .flags = 0 },
  63. { .bitrate = 360,
  64. .hw_value = ZD_OFDM_RATE_36M,
  65. .flags = 0 },
  66. { .bitrate = 480,
  67. .hw_value = ZD_OFDM_RATE_48M,
  68. .flags = 0 },
  69. { .bitrate = 540,
  70. .hw_value = ZD_OFDM_RATE_54M,
  71. .flags = 0 },
  72. };
  73. static const struct ieee80211_channel zd_channels[] = {
  74. { .center_freq = 2412, .hw_value = 1 },
  75. { .center_freq = 2417, .hw_value = 2 },
  76. { .center_freq = 2422, .hw_value = 3 },
  77. { .center_freq = 2427, .hw_value = 4 },
  78. { .center_freq = 2432, .hw_value = 5 },
  79. { .center_freq = 2437, .hw_value = 6 },
  80. { .center_freq = 2442, .hw_value = 7 },
  81. { .center_freq = 2447, .hw_value = 8 },
  82. { .center_freq = 2452, .hw_value = 9 },
  83. { .center_freq = 2457, .hw_value = 10 },
  84. { .center_freq = 2462, .hw_value = 11 },
  85. { .center_freq = 2467, .hw_value = 12 },
  86. { .center_freq = 2472, .hw_value = 13 },
  87. { .center_freq = 2484, .hw_value = 14 },
  88. };
  89. static void housekeeping_init(struct zd_mac *mac);
  90. static void housekeeping_enable(struct zd_mac *mac);
  91. static void housekeeping_disable(struct zd_mac *mac);
  92. int zd_mac_preinit_hw(struct ieee80211_hw *hw)
  93. {
  94. int r;
  95. u8 addr[ETH_ALEN];
  96. struct zd_mac *mac = zd_hw_mac(hw);
  97. r = zd_chip_read_mac_addr_fw(&mac->chip, addr);
  98. if (r)
  99. return r;
  100. SET_IEEE80211_PERM_ADDR(hw, addr);
  101. return 0;
  102. }
  103. int zd_mac_init_hw(struct ieee80211_hw *hw)
  104. {
  105. int r;
  106. struct zd_mac *mac = zd_hw_mac(hw);
  107. struct zd_chip *chip = &mac->chip;
  108. u8 default_regdomain;
  109. r = zd_chip_enable_int(chip);
  110. if (r)
  111. goto out;
  112. r = zd_chip_init_hw(chip);
  113. if (r)
  114. goto disable_int;
  115. ZD_ASSERT(!irqs_disabled());
  116. r = zd_read_regdomain(chip, &default_regdomain);
  117. if (r)
  118. goto disable_int;
  119. spin_lock_irq(&mac->lock);
  120. mac->regdomain = mac->default_regdomain = default_regdomain;
  121. spin_unlock_irq(&mac->lock);
  122. /* We must inform the device that we are doing encryption/decryption in
  123. * software at the moment. */
  124. r = zd_set_encryption_type(chip, ENC_SNIFFER);
  125. if (r)
  126. goto disable_int;
  127. zd_geo_init(hw, mac->regdomain);
  128. r = 0;
  129. disable_int:
  130. zd_chip_disable_int(chip);
  131. out:
  132. return r;
  133. }
  134. void zd_mac_clear(struct zd_mac *mac)
  135. {
  136. flush_workqueue(zd_workqueue);
  137. zd_chip_clear(&mac->chip);
  138. ZD_ASSERT(!spin_is_locked(&mac->lock));
  139. ZD_MEMCLEAR(mac, sizeof(struct zd_mac));
  140. }
  141. static int set_rx_filter(struct zd_mac *mac)
  142. {
  143. unsigned long flags;
  144. u32 filter = STA_RX_FILTER;
  145. spin_lock_irqsave(&mac->lock, flags);
  146. if (mac->pass_ctrl)
  147. filter |= RX_FILTER_CTRL;
  148. spin_unlock_irqrestore(&mac->lock, flags);
  149. return zd_iowrite32(&mac->chip, CR_RX_FILTER, filter);
  150. }
  151. static int set_mc_hash(struct zd_mac *mac)
  152. {
  153. struct zd_mc_hash hash;
  154. zd_mc_clear(&hash);
  155. return zd_chip_set_multicast_hash(&mac->chip, &hash);
  156. }
  157. static int zd_op_start(struct ieee80211_hw *hw)
  158. {
  159. struct zd_mac *mac = zd_hw_mac(hw);
  160. struct zd_chip *chip = &mac->chip;
  161. struct zd_usb *usb = &chip->usb;
  162. int r;
  163. if (!usb->initialized) {
  164. r = zd_usb_init_hw(usb);
  165. if (r)
  166. goto out;
  167. }
  168. r = zd_chip_enable_int(chip);
  169. if (r < 0)
  170. goto out;
  171. r = zd_chip_set_basic_rates(chip, CR_RATES_80211B | CR_RATES_80211G);
  172. if (r < 0)
  173. goto disable_int;
  174. r = set_rx_filter(mac);
  175. if (r)
  176. goto disable_int;
  177. r = set_mc_hash(mac);
  178. if (r)
  179. goto disable_int;
  180. r = zd_chip_switch_radio_on(chip);
  181. if (r < 0)
  182. goto disable_int;
  183. r = zd_chip_enable_rxtx(chip);
  184. if (r < 0)
  185. goto disable_radio;
  186. r = zd_chip_enable_hwint(chip);
  187. if (r < 0)
  188. goto disable_rxtx;
  189. housekeeping_enable(mac);
  190. return 0;
  191. disable_rxtx:
  192. zd_chip_disable_rxtx(chip);
  193. disable_radio:
  194. zd_chip_switch_radio_off(chip);
  195. disable_int:
  196. zd_chip_disable_int(chip);
  197. out:
  198. return r;
  199. }
  200. static void zd_op_stop(struct ieee80211_hw *hw)
  201. {
  202. struct zd_mac *mac = zd_hw_mac(hw);
  203. struct zd_chip *chip = &mac->chip;
  204. struct sk_buff *skb;
  205. struct sk_buff_head *ack_wait_queue = &mac->ack_wait_queue;
  206. /* The order here deliberately is a little different from the open()
  207. * method, since we need to make sure there is no opportunity for RX
  208. * frames to be processed by mac80211 after we have stopped it.
  209. */
  210. zd_chip_disable_rxtx(chip);
  211. housekeeping_disable(mac);
  212. flush_workqueue(zd_workqueue);
  213. zd_chip_disable_hwint(chip);
  214. zd_chip_switch_radio_off(chip);
  215. zd_chip_disable_int(chip);
  216. while ((skb = skb_dequeue(ack_wait_queue)))
  217. dev_kfree_skb_any(skb);
  218. }
  219. /**
  220. * tx_status - reports tx status of a packet if required
  221. * @hw - a &struct ieee80211_hw pointer
  222. * @skb - a sk-buffer
  223. * @flags: extra flags to set in the TX status info
  224. * @ackssi: ACK signal strength
  225. * @success - True for successfull transmission of the frame
  226. *
  227. * This information calls ieee80211_tx_status_irqsafe() if required by the
  228. * control information. It copies the control information into the status
  229. * information.
  230. *
  231. * If no status information has been requested, the skb is freed.
  232. */
  233. static void tx_status(struct ieee80211_hw *hw, struct sk_buff *skb,
  234. u32 flags, int ackssi, bool success)
  235. {
  236. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  237. memset(&info->status, 0, sizeof(info->status));
  238. if (!success)
  239. info->status.excessive_retries = 1;
  240. info->flags |= flags;
  241. info->status.ack_signal = ackssi;
  242. ieee80211_tx_status_irqsafe(hw, skb);
  243. }
  244. /**
  245. * zd_mac_tx_failed - callback for failed frames
  246. * @dev: the mac80211 wireless device
  247. *
  248. * This function is called if a frame couldn't be succesfully be
  249. * transferred. The first frame from the tx queue, will be selected and
  250. * reported as error to the upper layers.
  251. */
  252. void zd_mac_tx_failed(struct ieee80211_hw *hw)
  253. {
  254. struct sk_buff_head *q = &zd_hw_mac(hw)->ack_wait_queue;
  255. struct sk_buff *skb;
  256. skb = skb_dequeue(q);
  257. if (skb == NULL)
  258. return;
  259. tx_status(hw, skb, 0, 0, 0);
  260. }
  261. /**
  262. * zd_mac_tx_to_dev - callback for USB layer
  263. * @skb: a &sk_buff pointer
  264. * @error: error value, 0 if transmission successful
  265. *
  266. * Informs the MAC layer that the frame has successfully transferred to the
  267. * device. If an ACK is required and the transfer to the device has been
  268. * successful, the packets are put on the @ack_wait_queue with
  269. * the control set removed.
  270. */
  271. void zd_mac_tx_to_dev(struct sk_buff *skb, int error)
  272. {
  273. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  274. struct ieee80211_hw *hw = info->driver_data[0];
  275. skb_pull(skb, sizeof(struct zd_ctrlset));
  276. if (unlikely(error ||
  277. (info->flags & IEEE80211_TX_CTL_NO_ACK))) {
  278. tx_status(hw, skb, 0, 0, !error);
  279. } else {
  280. struct sk_buff_head *q =
  281. &zd_hw_mac(hw)->ack_wait_queue;
  282. skb_queue_tail(q, skb);
  283. while (skb_queue_len(q) > ZD_MAC_MAX_ACK_WAITERS)
  284. zd_mac_tx_failed(hw);
  285. }
  286. }
  287. static int zd_calc_tx_length_us(u8 *service, u8 zd_rate, u16 tx_length)
  288. {
  289. /* ZD_PURE_RATE() must be used to remove the modulation type flag of
  290. * the zd-rate values.
  291. */
  292. static const u8 rate_divisor[] = {
  293. [ZD_PURE_RATE(ZD_CCK_RATE_1M)] = 1,
  294. [ZD_PURE_RATE(ZD_CCK_RATE_2M)] = 2,
  295. /* Bits must be doubled. */
  296. [ZD_PURE_RATE(ZD_CCK_RATE_5_5M)] = 11,
  297. [ZD_PURE_RATE(ZD_CCK_RATE_11M)] = 11,
  298. [ZD_PURE_RATE(ZD_OFDM_RATE_6M)] = 6,
  299. [ZD_PURE_RATE(ZD_OFDM_RATE_9M)] = 9,
  300. [ZD_PURE_RATE(ZD_OFDM_RATE_12M)] = 12,
  301. [ZD_PURE_RATE(ZD_OFDM_RATE_18M)] = 18,
  302. [ZD_PURE_RATE(ZD_OFDM_RATE_24M)] = 24,
  303. [ZD_PURE_RATE(ZD_OFDM_RATE_36M)] = 36,
  304. [ZD_PURE_RATE(ZD_OFDM_RATE_48M)] = 48,
  305. [ZD_PURE_RATE(ZD_OFDM_RATE_54M)] = 54,
  306. };
  307. u32 bits = (u32)tx_length * 8;
  308. u32 divisor;
  309. divisor = rate_divisor[ZD_PURE_RATE(zd_rate)];
  310. if (divisor == 0)
  311. return -EINVAL;
  312. switch (zd_rate) {
  313. case ZD_CCK_RATE_5_5M:
  314. bits = (2*bits) + 10; /* round up to the next integer */
  315. break;
  316. case ZD_CCK_RATE_11M:
  317. if (service) {
  318. u32 t = bits % 11;
  319. *service &= ~ZD_PLCP_SERVICE_LENGTH_EXTENSION;
  320. if (0 < t && t <= 3) {
  321. *service |= ZD_PLCP_SERVICE_LENGTH_EXTENSION;
  322. }
  323. }
  324. bits += 10; /* round up to the next integer */
  325. break;
  326. }
  327. return bits/divisor;
  328. }
  329. static void cs_set_control(struct zd_mac *mac, struct zd_ctrlset *cs,
  330. struct ieee80211_hdr *header, u32 flags)
  331. {
  332. /*
  333. * CONTROL TODO:
  334. * - if backoff needed, enable bit 0
  335. * - if burst (backoff not needed) disable bit 0
  336. */
  337. cs->control = 0;
  338. /* First fragment */
  339. if (flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
  340. cs->control |= ZD_CS_NEED_RANDOM_BACKOFF;
  341. /* Multicast */
  342. if (is_multicast_ether_addr(header->addr1))
  343. cs->control |= ZD_CS_MULTICAST;
  344. /* PS-POLL */
  345. if (ieee80211_is_pspoll(header->frame_control))
  346. cs->control |= ZD_CS_PS_POLL_FRAME;
  347. if (flags & IEEE80211_TX_CTL_USE_RTS_CTS)
  348. cs->control |= ZD_CS_RTS;
  349. if (flags & IEEE80211_TX_CTL_USE_CTS_PROTECT)
  350. cs->control |= ZD_CS_SELF_CTS;
  351. /* FIXME: Management frame? */
  352. }
  353. static int zd_mac_config_beacon(struct ieee80211_hw *hw, struct sk_buff *beacon)
  354. {
  355. struct zd_mac *mac = zd_hw_mac(hw);
  356. int r;
  357. u32 tmp, j = 0;
  358. /* 4 more bytes for tail CRC */
  359. u32 full_len = beacon->len + 4;
  360. r = zd_iowrite32(&mac->chip, CR_BCN_FIFO_SEMAPHORE, 0);
  361. if (r < 0)
  362. return r;
  363. r = zd_ioread32(&mac->chip, CR_BCN_FIFO_SEMAPHORE, &tmp);
  364. if (r < 0)
  365. return r;
  366. while (tmp & 0x2) {
  367. r = zd_ioread32(&mac->chip, CR_BCN_FIFO_SEMAPHORE, &tmp);
  368. if (r < 0)
  369. return r;
  370. if ((++j % 100) == 0) {
  371. printk(KERN_ERR "CR_BCN_FIFO_SEMAPHORE not ready\n");
  372. if (j >= 500) {
  373. printk(KERN_ERR "Giving up beacon config.\n");
  374. return -ETIMEDOUT;
  375. }
  376. }
  377. msleep(1);
  378. }
  379. r = zd_iowrite32(&mac->chip, CR_BCN_FIFO, full_len - 1);
  380. if (r < 0)
  381. return r;
  382. if (zd_chip_is_zd1211b(&mac->chip)) {
  383. r = zd_iowrite32(&mac->chip, CR_BCN_LENGTH, full_len - 1);
  384. if (r < 0)
  385. return r;
  386. }
  387. for (j = 0 ; j < beacon->len; j++) {
  388. r = zd_iowrite32(&mac->chip, CR_BCN_FIFO,
  389. *((u8 *)(beacon->data + j)));
  390. if (r < 0)
  391. return r;
  392. }
  393. for (j = 0; j < 4; j++) {
  394. r = zd_iowrite32(&mac->chip, CR_BCN_FIFO, 0x0);
  395. if (r < 0)
  396. return r;
  397. }
  398. r = zd_iowrite32(&mac->chip, CR_BCN_FIFO_SEMAPHORE, 1);
  399. if (r < 0)
  400. return r;
  401. /* 802.11b/g 2.4G CCK 1Mb
  402. * 802.11a, not yet implemented, uses different values (see GPL vendor
  403. * driver)
  404. */
  405. return zd_iowrite32(&mac->chip, CR_BCN_PLCP_CFG, 0x00000400 |
  406. (full_len << 19));
  407. }
  408. static int fill_ctrlset(struct zd_mac *mac,
  409. struct sk_buff *skb)
  410. {
  411. int r;
  412. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  413. unsigned int frag_len = skb->len + FCS_LEN;
  414. unsigned int packet_length;
  415. struct ieee80211_rate *txrate;
  416. struct zd_ctrlset *cs = (struct zd_ctrlset *)
  417. skb_push(skb, sizeof(struct zd_ctrlset));
  418. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  419. ZD_ASSERT(frag_len <= 0xffff);
  420. txrate = ieee80211_get_tx_rate(mac->hw, info);
  421. cs->modulation = txrate->hw_value;
  422. if (info->flags & IEEE80211_TX_CTL_SHORT_PREAMBLE)
  423. cs->modulation = txrate->hw_value_short;
  424. cs->tx_length = cpu_to_le16(frag_len);
  425. cs_set_control(mac, cs, hdr, info->flags);
  426. packet_length = frag_len + sizeof(struct zd_ctrlset) + 10;
  427. ZD_ASSERT(packet_length <= 0xffff);
  428. /* ZD1211B: Computing the length difference this way, gives us
  429. * flexibility to compute the packet length.
  430. */
  431. cs->packet_length = cpu_to_le16(zd_chip_is_zd1211b(&mac->chip) ?
  432. packet_length - frag_len : packet_length);
  433. /*
  434. * CURRENT LENGTH:
  435. * - transmit frame length in microseconds
  436. * - seems to be derived from frame length
  437. * - see Cal_Us_Service() in zdinlinef.h
  438. * - if macp->bTxBurstEnable is enabled, then multiply by 4
  439. * - bTxBurstEnable is never set in the vendor driver
  440. *
  441. * SERVICE:
  442. * - "for PLCP configuration"
  443. * - always 0 except in some situations at 802.11b 11M
  444. * - see line 53 of zdinlinef.h
  445. */
  446. cs->service = 0;
  447. r = zd_calc_tx_length_us(&cs->service, ZD_RATE(cs->modulation),
  448. le16_to_cpu(cs->tx_length));
  449. if (r < 0)
  450. return r;
  451. cs->current_length = cpu_to_le16(r);
  452. cs->next_frame_length = 0;
  453. return 0;
  454. }
  455. /**
  456. * zd_op_tx - transmits a network frame to the device
  457. *
  458. * @dev: mac80211 hardware device
  459. * @skb: socket buffer
  460. * @control: the control structure
  461. *
  462. * This function transmit an IEEE 802.11 network frame to the device. The
  463. * control block of the skbuff will be initialized. If necessary the incoming
  464. * mac80211 queues will be stopped.
  465. */
  466. static int zd_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
  467. {
  468. struct zd_mac *mac = zd_hw_mac(hw);
  469. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  470. int r;
  471. r = fill_ctrlset(mac, skb);
  472. if (r)
  473. return r;
  474. info->driver_data[0] = hw;
  475. r = zd_usb_tx(&mac->chip.usb, skb);
  476. if (r)
  477. return r;
  478. return 0;
  479. }
  480. /**
  481. * filter_ack - filters incoming packets for acknowledgements
  482. * @dev: the mac80211 device
  483. * @rx_hdr: received header
  484. * @stats: the status for the received packet
  485. *
  486. * This functions looks for ACK packets and tries to match them with the
  487. * frames in the tx queue. If a match is found the frame will be dequeued and
  488. * the upper layers is informed about the successful transmission. If
  489. * mac80211 queues have been stopped and the number of frames still to be
  490. * transmitted is low the queues will be opened again.
  491. *
  492. * Returns 1 if the frame was an ACK, 0 if it was ignored.
  493. */
  494. static int filter_ack(struct ieee80211_hw *hw, struct ieee80211_hdr *rx_hdr,
  495. struct ieee80211_rx_status *stats)
  496. {
  497. struct sk_buff *skb;
  498. struct sk_buff_head *q;
  499. unsigned long flags;
  500. if (!ieee80211_is_ack(rx_hdr->frame_control))
  501. return 0;
  502. q = &zd_hw_mac(hw)->ack_wait_queue;
  503. spin_lock_irqsave(&q->lock, flags);
  504. for (skb = q->next; skb != (struct sk_buff *)q; skb = skb->next) {
  505. struct ieee80211_hdr *tx_hdr;
  506. tx_hdr = (struct ieee80211_hdr *)skb->data;
  507. if (likely(!compare_ether_addr(tx_hdr->addr2, rx_hdr->addr1)))
  508. {
  509. __skb_unlink(skb, q);
  510. tx_status(hw, skb, IEEE80211_TX_STAT_ACK, stats->signal, 1);
  511. goto out;
  512. }
  513. }
  514. out:
  515. spin_unlock_irqrestore(&q->lock, flags);
  516. return 1;
  517. }
  518. int zd_mac_rx(struct ieee80211_hw *hw, const u8 *buffer, unsigned int length)
  519. {
  520. struct zd_mac *mac = zd_hw_mac(hw);
  521. struct ieee80211_rx_status stats;
  522. const struct rx_status *status;
  523. struct sk_buff *skb;
  524. int bad_frame = 0;
  525. __le16 fc;
  526. int need_padding;
  527. int i;
  528. u8 rate;
  529. if (length < ZD_PLCP_HEADER_SIZE + 10 /* IEEE80211_1ADDR_LEN */ +
  530. FCS_LEN + sizeof(struct rx_status))
  531. return -EINVAL;
  532. memset(&stats, 0, sizeof(stats));
  533. /* Note about pass_failed_fcs and pass_ctrl access below:
  534. * mac locking intentionally omitted here, as this is the only unlocked
  535. * reader and the only writer is configure_filter. Plus, if there were
  536. * any races accessing these variables, it wouldn't really matter.
  537. * If mac80211 ever provides a way for us to access filter flags
  538. * from outside configure_filter, we could improve on this. Also, this
  539. * situation may change once we implement some kind of DMA-into-skb
  540. * RX path. */
  541. /* Caller has to ensure that length >= sizeof(struct rx_status). */
  542. status = (struct rx_status *)
  543. (buffer + (length - sizeof(struct rx_status)));
  544. if (status->frame_status & ZD_RX_ERROR) {
  545. if (mac->pass_failed_fcs &&
  546. (status->frame_status & ZD_RX_CRC32_ERROR)) {
  547. stats.flag |= RX_FLAG_FAILED_FCS_CRC;
  548. bad_frame = 1;
  549. } else {
  550. return -EINVAL;
  551. }
  552. }
  553. stats.freq = zd_channels[_zd_chip_get_channel(&mac->chip) - 1].center_freq;
  554. stats.band = IEEE80211_BAND_2GHZ;
  555. stats.signal = status->signal_strength;
  556. stats.qual = zd_rx_qual_percent(buffer,
  557. length - sizeof(struct rx_status),
  558. status);
  559. rate = zd_rx_rate(buffer, status);
  560. /* todo: return index in the big switches in zd_rx_rate instead */
  561. for (i = 0; i < mac->band.n_bitrates; i++)
  562. if (rate == mac->band.bitrates[i].hw_value)
  563. stats.rate_idx = i;
  564. length -= ZD_PLCP_HEADER_SIZE + sizeof(struct rx_status);
  565. buffer += ZD_PLCP_HEADER_SIZE;
  566. /* Except for bad frames, filter each frame to see if it is an ACK, in
  567. * which case our internal TX tracking is updated. Normally we then
  568. * bail here as there's no need to pass ACKs on up to the stack, but
  569. * there is also the case where the stack has requested us to pass
  570. * control frames on up (pass_ctrl) which we must consider. */
  571. if (!bad_frame &&
  572. filter_ack(hw, (struct ieee80211_hdr *)buffer, &stats)
  573. && !mac->pass_ctrl)
  574. return 0;
  575. fc = *(__le16 *)buffer;
  576. need_padding = ieee80211_is_data_qos(fc) ^ ieee80211_has_a4(fc);
  577. skb = dev_alloc_skb(length + (need_padding ? 2 : 0));
  578. if (skb == NULL)
  579. return -ENOMEM;
  580. if (need_padding) {
  581. /* Make sure the the payload data is 4 byte aligned. */
  582. skb_reserve(skb, 2);
  583. }
  584. memcpy(skb_put(skb, length), buffer, length);
  585. ieee80211_rx_irqsafe(hw, skb, &stats);
  586. return 0;
  587. }
  588. static int zd_op_add_interface(struct ieee80211_hw *hw,
  589. struct ieee80211_if_init_conf *conf)
  590. {
  591. struct zd_mac *mac = zd_hw_mac(hw);
  592. /* using IEEE80211_IF_TYPE_INVALID to indicate no mode selected */
  593. if (mac->type != IEEE80211_IF_TYPE_INVALID)
  594. return -EOPNOTSUPP;
  595. switch (conf->type) {
  596. case IEEE80211_IF_TYPE_MNTR:
  597. case IEEE80211_IF_TYPE_MESH_POINT:
  598. case IEEE80211_IF_TYPE_STA:
  599. case IEEE80211_IF_TYPE_IBSS:
  600. mac->type = conf->type;
  601. break;
  602. default:
  603. return -EOPNOTSUPP;
  604. }
  605. return zd_write_mac_addr(&mac->chip, conf->mac_addr);
  606. }
  607. static void zd_op_remove_interface(struct ieee80211_hw *hw,
  608. struct ieee80211_if_init_conf *conf)
  609. {
  610. struct zd_mac *mac = zd_hw_mac(hw);
  611. mac->type = IEEE80211_IF_TYPE_INVALID;
  612. zd_set_beacon_interval(&mac->chip, 0);
  613. zd_write_mac_addr(&mac->chip, NULL);
  614. }
  615. static int zd_op_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
  616. {
  617. struct zd_mac *mac = zd_hw_mac(hw);
  618. return zd_chip_set_channel(&mac->chip, conf->channel->hw_value);
  619. }
  620. static int zd_op_config_interface(struct ieee80211_hw *hw,
  621. struct ieee80211_vif *vif,
  622. struct ieee80211_if_conf *conf)
  623. {
  624. struct zd_mac *mac = zd_hw_mac(hw);
  625. int associated;
  626. int r;
  627. if (mac->type == IEEE80211_IF_TYPE_MESH_POINT ||
  628. mac->type == IEEE80211_IF_TYPE_IBSS) {
  629. associated = true;
  630. if (conf->changed & IEEE80211_IFCC_BEACON) {
  631. struct sk_buff *beacon = ieee80211_beacon_get(hw, vif);
  632. if (!beacon)
  633. return -ENOMEM;
  634. r = zd_mac_config_beacon(hw, beacon);
  635. if (r < 0)
  636. return r;
  637. r = zd_set_beacon_interval(&mac->chip, BCN_MODE_IBSS |
  638. hw->conf.beacon_int);
  639. if (r < 0)
  640. return r;
  641. kfree_skb(beacon);
  642. }
  643. } else
  644. associated = is_valid_ether_addr(conf->bssid);
  645. spin_lock_irq(&mac->lock);
  646. mac->associated = associated;
  647. spin_unlock_irq(&mac->lock);
  648. /* TODO: do hardware bssid filtering */
  649. return 0;
  650. }
  651. void zd_process_intr(struct work_struct *work)
  652. {
  653. u16 int_status;
  654. struct zd_mac *mac = container_of(work, struct zd_mac, process_intr);
  655. int_status = le16_to_cpu(*(__le16 *)(mac->intr_buffer+4));
  656. if (int_status & INT_CFG_NEXT_BCN) {
  657. if (net_ratelimit())
  658. dev_dbg_f(zd_mac_dev(mac), "INT_CFG_NEXT_BCN\n");
  659. } else
  660. dev_dbg_f(zd_mac_dev(mac), "Unsupported interrupt\n");
  661. zd_chip_enable_hwint(&mac->chip);
  662. }
  663. static void set_multicast_hash_handler(struct work_struct *work)
  664. {
  665. struct zd_mac *mac =
  666. container_of(work, struct zd_mac, set_multicast_hash_work);
  667. struct zd_mc_hash hash;
  668. spin_lock_irq(&mac->lock);
  669. hash = mac->multicast_hash;
  670. spin_unlock_irq(&mac->lock);
  671. zd_chip_set_multicast_hash(&mac->chip, &hash);
  672. }
  673. static void set_rx_filter_handler(struct work_struct *work)
  674. {
  675. struct zd_mac *mac =
  676. container_of(work, struct zd_mac, set_rx_filter_work);
  677. int r;
  678. dev_dbg_f(zd_mac_dev(mac), "\n");
  679. r = set_rx_filter(mac);
  680. if (r)
  681. dev_err(zd_mac_dev(mac), "set_rx_filter_handler error %d\n", r);
  682. }
  683. #define SUPPORTED_FIF_FLAGS \
  684. (FIF_PROMISC_IN_BSS | FIF_ALLMULTI | FIF_FCSFAIL | FIF_CONTROL | \
  685. FIF_OTHER_BSS | FIF_BCN_PRBRESP_PROMISC)
  686. static void zd_op_configure_filter(struct ieee80211_hw *hw,
  687. unsigned int changed_flags,
  688. unsigned int *new_flags,
  689. int mc_count, struct dev_mc_list *mclist)
  690. {
  691. struct zd_mc_hash hash;
  692. struct zd_mac *mac = zd_hw_mac(hw);
  693. unsigned long flags;
  694. int i;
  695. /* Only deal with supported flags */
  696. changed_flags &= SUPPORTED_FIF_FLAGS;
  697. *new_flags &= SUPPORTED_FIF_FLAGS;
  698. /* changed_flags is always populated but this driver
  699. * doesn't support all FIF flags so its possible we don't
  700. * need to do anything */
  701. if (!changed_flags)
  702. return;
  703. if (*new_flags & (FIF_PROMISC_IN_BSS | FIF_ALLMULTI)) {
  704. zd_mc_add_all(&hash);
  705. } else {
  706. DECLARE_MAC_BUF(macbuf);
  707. zd_mc_clear(&hash);
  708. for (i = 0; i < mc_count; i++) {
  709. if (!mclist)
  710. break;
  711. dev_dbg_f(zd_mac_dev(mac), "mc addr %s\n",
  712. print_mac(macbuf, mclist->dmi_addr));
  713. zd_mc_add_addr(&hash, mclist->dmi_addr);
  714. mclist = mclist->next;
  715. }
  716. }
  717. spin_lock_irqsave(&mac->lock, flags);
  718. mac->pass_failed_fcs = !!(*new_flags & FIF_FCSFAIL);
  719. mac->pass_ctrl = !!(*new_flags & FIF_CONTROL);
  720. mac->multicast_hash = hash;
  721. spin_unlock_irqrestore(&mac->lock, flags);
  722. queue_work(zd_workqueue, &mac->set_multicast_hash_work);
  723. if (changed_flags & FIF_CONTROL)
  724. queue_work(zd_workqueue, &mac->set_rx_filter_work);
  725. /* no handling required for FIF_OTHER_BSS as we don't currently
  726. * do BSSID filtering */
  727. /* FIXME: in future it would be nice to enable the probe response
  728. * filter (so that the driver doesn't see them) until
  729. * FIF_BCN_PRBRESP_PROMISC is set. however due to atomicity here, we'd
  730. * have to schedule work to enable prbresp reception, which might
  731. * happen too late. For now we'll just listen and forward them all the
  732. * time. */
  733. }
  734. static void set_rts_cts_work(struct work_struct *work)
  735. {
  736. struct zd_mac *mac =
  737. container_of(work, struct zd_mac, set_rts_cts_work);
  738. unsigned long flags;
  739. unsigned int short_preamble;
  740. mutex_lock(&mac->chip.mutex);
  741. spin_lock_irqsave(&mac->lock, flags);
  742. mac->updating_rts_rate = 0;
  743. short_preamble = mac->short_preamble;
  744. spin_unlock_irqrestore(&mac->lock, flags);
  745. zd_chip_set_rts_cts_rate_locked(&mac->chip, short_preamble);
  746. mutex_unlock(&mac->chip.mutex);
  747. }
  748. static void zd_op_bss_info_changed(struct ieee80211_hw *hw,
  749. struct ieee80211_vif *vif,
  750. struct ieee80211_bss_conf *bss_conf,
  751. u32 changes)
  752. {
  753. struct zd_mac *mac = zd_hw_mac(hw);
  754. unsigned long flags;
  755. dev_dbg_f(zd_mac_dev(mac), "changes: %x\n", changes);
  756. if (changes & BSS_CHANGED_ERP_PREAMBLE) {
  757. spin_lock_irqsave(&mac->lock, flags);
  758. mac->short_preamble = bss_conf->use_short_preamble;
  759. if (!mac->updating_rts_rate) {
  760. mac->updating_rts_rate = 1;
  761. /* FIXME: should disable TX here, until work has
  762. * completed and RTS_CTS reg is updated */
  763. queue_work(zd_workqueue, &mac->set_rts_cts_work);
  764. }
  765. spin_unlock_irqrestore(&mac->lock, flags);
  766. }
  767. }
  768. static const struct ieee80211_ops zd_ops = {
  769. .tx = zd_op_tx,
  770. .start = zd_op_start,
  771. .stop = zd_op_stop,
  772. .add_interface = zd_op_add_interface,
  773. .remove_interface = zd_op_remove_interface,
  774. .config = zd_op_config,
  775. .config_interface = zd_op_config_interface,
  776. .configure_filter = zd_op_configure_filter,
  777. .bss_info_changed = zd_op_bss_info_changed,
  778. };
  779. struct ieee80211_hw *zd_mac_alloc_hw(struct usb_interface *intf)
  780. {
  781. struct zd_mac *mac;
  782. struct ieee80211_hw *hw;
  783. hw = ieee80211_alloc_hw(sizeof(struct zd_mac), &zd_ops);
  784. if (!hw) {
  785. dev_dbg_f(&intf->dev, "out of memory\n");
  786. return NULL;
  787. }
  788. mac = zd_hw_mac(hw);
  789. memset(mac, 0, sizeof(*mac));
  790. spin_lock_init(&mac->lock);
  791. mac->hw = hw;
  792. mac->type = IEEE80211_IF_TYPE_INVALID;
  793. memcpy(mac->channels, zd_channels, sizeof(zd_channels));
  794. memcpy(mac->rates, zd_rates, sizeof(zd_rates));
  795. mac->band.n_bitrates = ARRAY_SIZE(zd_rates);
  796. mac->band.bitrates = mac->rates;
  797. mac->band.n_channels = ARRAY_SIZE(zd_channels);
  798. mac->band.channels = mac->channels;
  799. hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &mac->band;
  800. hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
  801. IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE |
  802. IEEE80211_HW_SIGNAL_DB;
  803. hw->max_signal = 100;
  804. hw->queues = 1;
  805. hw->extra_tx_headroom = sizeof(struct zd_ctrlset);
  806. skb_queue_head_init(&mac->ack_wait_queue);
  807. zd_chip_init(&mac->chip, hw, intf);
  808. housekeeping_init(mac);
  809. INIT_WORK(&mac->set_multicast_hash_work, set_multicast_hash_handler);
  810. INIT_WORK(&mac->set_rts_cts_work, set_rts_cts_work);
  811. INIT_WORK(&mac->set_rx_filter_work, set_rx_filter_handler);
  812. INIT_WORK(&mac->process_intr, zd_process_intr);
  813. SET_IEEE80211_DEV(hw, &intf->dev);
  814. return hw;
  815. }
  816. #define LINK_LED_WORK_DELAY HZ
  817. static void link_led_handler(struct work_struct *work)
  818. {
  819. struct zd_mac *mac =
  820. container_of(work, struct zd_mac, housekeeping.link_led_work.work);
  821. struct zd_chip *chip = &mac->chip;
  822. int is_associated;
  823. int r;
  824. spin_lock_irq(&mac->lock);
  825. is_associated = mac->associated;
  826. spin_unlock_irq(&mac->lock);
  827. r = zd_chip_control_leds(chip,
  828. is_associated ? LED_ASSOCIATED : LED_SCANNING);
  829. if (r)
  830. dev_dbg_f(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r);
  831. queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
  832. LINK_LED_WORK_DELAY);
  833. }
  834. static void housekeeping_init(struct zd_mac *mac)
  835. {
  836. INIT_DELAYED_WORK(&mac->housekeeping.link_led_work, link_led_handler);
  837. }
  838. static void housekeeping_enable(struct zd_mac *mac)
  839. {
  840. dev_dbg_f(zd_mac_dev(mac), "\n");
  841. queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
  842. 0);
  843. }
  844. static void housekeeping_disable(struct zd_mac *mac)
  845. {
  846. dev_dbg_f(zd_mac_dev(mac), "\n");
  847. cancel_rearming_delayed_workqueue(zd_workqueue,
  848. &mac->housekeeping.link_led_work);
  849. zd_chip_control_leds(&mac->chip, LED_OFF);
  850. }