zd_mac.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  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. u16 fctl = le16_to_cpu(header->frame_control);
  333. /*
  334. * CONTROL TODO:
  335. * - if backoff needed, enable bit 0
  336. * - if burst (backoff not needed) disable bit 0
  337. */
  338. cs->control = 0;
  339. /* First fragment */
  340. if (flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
  341. cs->control |= ZD_CS_NEED_RANDOM_BACKOFF;
  342. /* Multicast */
  343. if (is_multicast_ether_addr(header->addr1))
  344. cs->control |= ZD_CS_MULTICAST;
  345. /* PS-POLL */
  346. if ((fctl & (IEEE80211_FCTL_FTYPE|IEEE80211_FCTL_STYPE)) ==
  347. (IEEE80211_FTYPE_CTL|IEEE80211_STYPE_PSPOLL))
  348. cs->control |= ZD_CS_PS_POLL_FRAME;
  349. if (flags & IEEE80211_TX_CTL_USE_RTS_CTS)
  350. cs->control |= ZD_CS_RTS;
  351. if (flags & IEEE80211_TX_CTL_USE_CTS_PROTECT)
  352. cs->control |= ZD_CS_SELF_CTS;
  353. /* FIXME: Management frame? */
  354. }
  355. void zd_mac_config_beacon(struct ieee80211_hw *hw, struct sk_buff *beacon)
  356. {
  357. struct zd_mac *mac = zd_hw_mac(hw);
  358. u32 tmp, j = 0;
  359. /* 4 more bytes for tail CRC */
  360. u32 full_len = beacon->len + 4;
  361. zd_iowrite32(&mac->chip, CR_BCN_FIFO_SEMAPHORE, 0);
  362. zd_ioread32(&mac->chip, CR_BCN_FIFO_SEMAPHORE, &tmp);
  363. while (tmp & 0x2) {
  364. zd_ioread32(&mac->chip, CR_BCN_FIFO_SEMAPHORE, &tmp);
  365. if ((++j % 100) == 0) {
  366. printk(KERN_ERR "CR_BCN_FIFO_SEMAPHORE not ready\n");
  367. if (j >= 500) {
  368. printk(KERN_ERR "Giving up beacon config.\n");
  369. return;
  370. }
  371. }
  372. msleep(1);
  373. }
  374. zd_iowrite32(&mac->chip, CR_BCN_FIFO, full_len - 1);
  375. if (zd_chip_is_zd1211b(&mac->chip))
  376. zd_iowrite32(&mac->chip, CR_BCN_LENGTH, full_len - 1);
  377. for (j = 0 ; j < beacon->len; j++)
  378. zd_iowrite32(&mac->chip, CR_BCN_FIFO,
  379. *((u8 *)(beacon->data + j)));
  380. for (j = 0; j < 4; j++)
  381. zd_iowrite32(&mac->chip, CR_BCN_FIFO, 0x0);
  382. zd_iowrite32(&mac->chip, CR_BCN_FIFO_SEMAPHORE, 1);
  383. /* 802.11b/g 2.4G CCK 1Mb
  384. * 802.11a, not yet implemented, uses different values (see GPL vendor
  385. * driver)
  386. */
  387. zd_iowrite32(&mac->chip, CR_BCN_PLCP_CFG, 0x00000400 |
  388. (full_len << 19));
  389. }
  390. static int fill_ctrlset(struct zd_mac *mac,
  391. struct sk_buff *skb)
  392. {
  393. int r;
  394. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  395. unsigned int frag_len = skb->len + FCS_LEN;
  396. unsigned int packet_length;
  397. struct ieee80211_rate *txrate;
  398. struct zd_ctrlset *cs = (struct zd_ctrlset *)
  399. skb_push(skb, sizeof(struct zd_ctrlset));
  400. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  401. ZD_ASSERT(frag_len <= 0xffff);
  402. txrate = ieee80211_get_tx_rate(mac->hw, info);
  403. cs->modulation = txrate->hw_value;
  404. if (info->flags & IEEE80211_TX_CTL_SHORT_PREAMBLE)
  405. cs->modulation = txrate->hw_value_short;
  406. cs->tx_length = cpu_to_le16(frag_len);
  407. cs_set_control(mac, cs, hdr, info->flags);
  408. packet_length = frag_len + sizeof(struct zd_ctrlset) + 10;
  409. ZD_ASSERT(packet_length <= 0xffff);
  410. /* ZD1211B: Computing the length difference this way, gives us
  411. * flexibility to compute the packet length.
  412. */
  413. cs->packet_length = cpu_to_le16(zd_chip_is_zd1211b(&mac->chip) ?
  414. packet_length - frag_len : packet_length);
  415. /*
  416. * CURRENT LENGTH:
  417. * - transmit frame length in microseconds
  418. * - seems to be derived from frame length
  419. * - see Cal_Us_Service() in zdinlinef.h
  420. * - if macp->bTxBurstEnable is enabled, then multiply by 4
  421. * - bTxBurstEnable is never set in the vendor driver
  422. *
  423. * SERVICE:
  424. * - "for PLCP configuration"
  425. * - always 0 except in some situations at 802.11b 11M
  426. * - see line 53 of zdinlinef.h
  427. */
  428. cs->service = 0;
  429. r = zd_calc_tx_length_us(&cs->service, ZD_RATE(cs->modulation),
  430. le16_to_cpu(cs->tx_length));
  431. if (r < 0)
  432. return r;
  433. cs->current_length = cpu_to_le16(r);
  434. cs->next_frame_length = 0;
  435. return 0;
  436. }
  437. /**
  438. * zd_op_tx - transmits a network frame to the device
  439. *
  440. * @dev: mac80211 hardware device
  441. * @skb: socket buffer
  442. * @control: the control structure
  443. *
  444. * This function transmit an IEEE 802.11 network frame to the device. The
  445. * control block of the skbuff will be initialized. If necessary the incoming
  446. * mac80211 queues will be stopped.
  447. */
  448. static int zd_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
  449. {
  450. struct zd_mac *mac = zd_hw_mac(hw);
  451. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  452. int r;
  453. r = fill_ctrlset(mac, skb);
  454. if (r)
  455. return r;
  456. info->driver_data[0] = hw;
  457. r = zd_usb_tx(&mac->chip.usb, skb);
  458. if (r)
  459. return r;
  460. return 0;
  461. }
  462. /**
  463. * filter_ack - filters incoming packets for acknowledgements
  464. * @dev: the mac80211 device
  465. * @rx_hdr: received header
  466. * @stats: the status for the received packet
  467. *
  468. * This functions looks for ACK packets and tries to match them with the
  469. * frames in the tx queue. If a match is found the frame will be dequeued and
  470. * the upper layers is informed about the successful transmission. If
  471. * mac80211 queues have been stopped and the number of frames still to be
  472. * transmitted is low the queues will be opened again.
  473. *
  474. * Returns 1 if the frame was an ACK, 0 if it was ignored.
  475. */
  476. static int filter_ack(struct ieee80211_hw *hw, struct ieee80211_hdr *rx_hdr,
  477. struct ieee80211_rx_status *stats)
  478. {
  479. u16 fc = le16_to_cpu(rx_hdr->frame_control);
  480. struct sk_buff *skb;
  481. struct sk_buff_head *q;
  482. unsigned long flags;
  483. if ((fc & (IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE)) !=
  484. (IEEE80211_FTYPE_CTL | IEEE80211_STYPE_ACK))
  485. return 0;
  486. q = &zd_hw_mac(hw)->ack_wait_queue;
  487. spin_lock_irqsave(&q->lock, flags);
  488. for (skb = q->next; skb != (struct sk_buff *)q; skb = skb->next) {
  489. struct ieee80211_hdr *tx_hdr;
  490. tx_hdr = (struct ieee80211_hdr *)skb->data;
  491. if (likely(!compare_ether_addr(tx_hdr->addr2, rx_hdr->addr1)))
  492. {
  493. __skb_unlink(skb, q);
  494. tx_status(hw, skb, IEEE80211_TX_STAT_ACK, stats->signal, 1);
  495. goto out;
  496. }
  497. }
  498. out:
  499. spin_unlock_irqrestore(&q->lock, flags);
  500. return 1;
  501. }
  502. int zd_mac_rx(struct ieee80211_hw *hw, const u8 *buffer, unsigned int length)
  503. {
  504. struct zd_mac *mac = zd_hw_mac(hw);
  505. struct ieee80211_rx_status stats;
  506. const struct rx_status *status;
  507. struct sk_buff *skb;
  508. int bad_frame = 0;
  509. u16 fc;
  510. bool is_qos, is_4addr, need_padding;
  511. int i;
  512. u8 rate;
  513. if (length < ZD_PLCP_HEADER_SIZE + 10 /* IEEE80211_1ADDR_LEN */ +
  514. FCS_LEN + sizeof(struct rx_status))
  515. return -EINVAL;
  516. memset(&stats, 0, sizeof(stats));
  517. /* Note about pass_failed_fcs and pass_ctrl access below:
  518. * mac locking intentionally omitted here, as this is the only unlocked
  519. * reader and the only writer is configure_filter. Plus, if there were
  520. * any races accessing these variables, it wouldn't really matter.
  521. * If mac80211 ever provides a way for us to access filter flags
  522. * from outside configure_filter, we could improve on this. Also, this
  523. * situation may change once we implement some kind of DMA-into-skb
  524. * RX path. */
  525. /* Caller has to ensure that length >= sizeof(struct rx_status). */
  526. status = (struct rx_status *)
  527. (buffer + (length - sizeof(struct rx_status)));
  528. if (status->frame_status & ZD_RX_ERROR) {
  529. if (mac->pass_failed_fcs &&
  530. (status->frame_status & ZD_RX_CRC32_ERROR)) {
  531. stats.flag |= RX_FLAG_FAILED_FCS_CRC;
  532. bad_frame = 1;
  533. } else {
  534. return -EINVAL;
  535. }
  536. }
  537. stats.freq = zd_channels[_zd_chip_get_channel(&mac->chip) - 1].center_freq;
  538. stats.band = IEEE80211_BAND_2GHZ;
  539. stats.signal = status->signal_strength;
  540. stats.qual = zd_rx_qual_percent(buffer,
  541. length - sizeof(struct rx_status),
  542. status);
  543. rate = zd_rx_rate(buffer, status);
  544. /* todo: return index in the big switches in zd_rx_rate instead */
  545. for (i = 0; i < mac->band.n_bitrates; i++)
  546. if (rate == mac->band.bitrates[i].hw_value)
  547. stats.rate_idx = i;
  548. length -= ZD_PLCP_HEADER_SIZE + sizeof(struct rx_status);
  549. buffer += ZD_PLCP_HEADER_SIZE;
  550. /* Except for bad frames, filter each frame to see if it is an ACK, in
  551. * which case our internal TX tracking is updated. Normally we then
  552. * bail here as there's no need to pass ACKs on up to the stack, but
  553. * there is also the case where the stack has requested us to pass
  554. * control frames on up (pass_ctrl) which we must consider. */
  555. if (!bad_frame &&
  556. filter_ack(hw, (struct ieee80211_hdr *)buffer, &stats)
  557. && !mac->pass_ctrl)
  558. return 0;
  559. fc = le16_to_cpu(*((__le16 *) buffer));
  560. is_qos = ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
  561. ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_QOS_DATA);
  562. is_4addr = (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
  563. (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS);
  564. need_padding = is_qos ^ is_4addr;
  565. skb = dev_alloc_skb(length + (need_padding ? 2 : 0));
  566. if (skb == NULL)
  567. return -ENOMEM;
  568. if (need_padding) {
  569. /* Make sure the the payload data is 4 byte aligned. */
  570. skb_reserve(skb, 2);
  571. }
  572. memcpy(skb_put(skb, length), buffer, length);
  573. ieee80211_rx_irqsafe(hw, skb, &stats);
  574. return 0;
  575. }
  576. static int zd_op_add_interface(struct ieee80211_hw *hw,
  577. struct ieee80211_if_init_conf *conf)
  578. {
  579. struct zd_mac *mac = zd_hw_mac(hw);
  580. /* using IEEE80211_IF_TYPE_INVALID to indicate no mode selected */
  581. if (mac->type != IEEE80211_IF_TYPE_INVALID)
  582. return -EOPNOTSUPP;
  583. switch (conf->type) {
  584. case IEEE80211_IF_TYPE_MNTR:
  585. case IEEE80211_IF_TYPE_MESH_POINT:
  586. case IEEE80211_IF_TYPE_STA:
  587. case IEEE80211_IF_TYPE_IBSS:
  588. mac->type = conf->type;
  589. break;
  590. default:
  591. return -EOPNOTSUPP;
  592. }
  593. return zd_write_mac_addr(&mac->chip, conf->mac_addr);
  594. }
  595. static void zd_op_remove_interface(struct ieee80211_hw *hw,
  596. struct ieee80211_if_init_conf *conf)
  597. {
  598. struct zd_mac *mac = zd_hw_mac(hw);
  599. mac->type = IEEE80211_IF_TYPE_INVALID;
  600. zd_write_mac_addr(&mac->chip, NULL);
  601. }
  602. static int zd_op_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
  603. {
  604. struct zd_mac *mac = zd_hw_mac(hw);
  605. return zd_chip_set_channel(&mac->chip, conf->channel->hw_value);
  606. }
  607. static int zd_op_config_interface(struct ieee80211_hw *hw,
  608. struct ieee80211_vif *vif,
  609. struct ieee80211_if_conf *conf)
  610. {
  611. struct zd_mac *mac = zd_hw_mac(hw);
  612. int associated;
  613. if (mac->type == IEEE80211_IF_TYPE_MESH_POINT ||
  614. mac->type == IEEE80211_IF_TYPE_IBSS) {
  615. associated = true;
  616. if (conf->beacon) {
  617. zd_mac_config_beacon(hw, conf->beacon);
  618. kfree_skb(conf->beacon);
  619. zd_set_beacon_interval(&mac->chip, BCN_MODE_IBSS |
  620. hw->conf.beacon_int);
  621. }
  622. } else
  623. associated = is_valid_ether_addr(conf->bssid);
  624. spin_lock_irq(&mac->lock);
  625. mac->associated = associated;
  626. spin_unlock_irq(&mac->lock);
  627. /* TODO: do hardware bssid filtering */
  628. return 0;
  629. }
  630. void zd_process_intr(struct work_struct *work)
  631. {
  632. u16 int_status;
  633. struct zd_mac *mac = container_of(work, struct zd_mac, process_intr);
  634. int_status = le16_to_cpu(*(u16 *)(mac->intr_buffer+4));
  635. if (int_status & INT_CFG_NEXT_BCN) {
  636. if (net_ratelimit())
  637. dev_dbg_f(zd_mac_dev(mac), "INT_CFG_NEXT_BCN\n");
  638. } else
  639. dev_dbg_f(zd_mac_dev(mac), "Unsupported interrupt\n");
  640. zd_chip_enable_hwint(&mac->chip);
  641. }
  642. static void set_multicast_hash_handler(struct work_struct *work)
  643. {
  644. struct zd_mac *mac =
  645. container_of(work, struct zd_mac, set_multicast_hash_work);
  646. struct zd_mc_hash hash;
  647. spin_lock_irq(&mac->lock);
  648. hash = mac->multicast_hash;
  649. spin_unlock_irq(&mac->lock);
  650. zd_chip_set_multicast_hash(&mac->chip, &hash);
  651. }
  652. static void set_rx_filter_handler(struct work_struct *work)
  653. {
  654. struct zd_mac *mac =
  655. container_of(work, struct zd_mac, set_rx_filter_work);
  656. int r;
  657. dev_dbg_f(zd_mac_dev(mac), "\n");
  658. r = set_rx_filter(mac);
  659. if (r)
  660. dev_err(zd_mac_dev(mac), "set_rx_filter_handler error %d\n", r);
  661. }
  662. #define SUPPORTED_FIF_FLAGS \
  663. (FIF_PROMISC_IN_BSS | FIF_ALLMULTI | FIF_FCSFAIL | FIF_CONTROL | \
  664. FIF_OTHER_BSS | FIF_BCN_PRBRESP_PROMISC)
  665. static void zd_op_configure_filter(struct ieee80211_hw *hw,
  666. unsigned int changed_flags,
  667. unsigned int *new_flags,
  668. int mc_count, struct dev_mc_list *mclist)
  669. {
  670. struct zd_mc_hash hash;
  671. struct zd_mac *mac = zd_hw_mac(hw);
  672. unsigned long flags;
  673. int i;
  674. /* Only deal with supported flags */
  675. changed_flags &= SUPPORTED_FIF_FLAGS;
  676. *new_flags &= SUPPORTED_FIF_FLAGS;
  677. /* changed_flags is always populated but this driver
  678. * doesn't support all FIF flags so its possible we don't
  679. * need to do anything */
  680. if (!changed_flags)
  681. return;
  682. if (*new_flags & (FIF_PROMISC_IN_BSS | FIF_ALLMULTI)) {
  683. zd_mc_add_all(&hash);
  684. } else {
  685. DECLARE_MAC_BUF(macbuf);
  686. zd_mc_clear(&hash);
  687. for (i = 0; i < mc_count; i++) {
  688. if (!mclist)
  689. break;
  690. dev_dbg_f(zd_mac_dev(mac), "mc addr %s\n",
  691. print_mac(macbuf, mclist->dmi_addr));
  692. zd_mc_add_addr(&hash, mclist->dmi_addr);
  693. mclist = mclist->next;
  694. }
  695. }
  696. spin_lock_irqsave(&mac->lock, flags);
  697. mac->pass_failed_fcs = !!(*new_flags & FIF_FCSFAIL);
  698. mac->pass_ctrl = !!(*new_flags & FIF_CONTROL);
  699. mac->multicast_hash = hash;
  700. spin_unlock_irqrestore(&mac->lock, flags);
  701. queue_work(zd_workqueue, &mac->set_multicast_hash_work);
  702. if (changed_flags & FIF_CONTROL)
  703. queue_work(zd_workqueue, &mac->set_rx_filter_work);
  704. /* no handling required for FIF_OTHER_BSS as we don't currently
  705. * do BSSID filtering */
  706. /* FIXME: in future it would be nice to enable the probe response
  707. * filter (so that the driver doesn't see them) until
  708. * FIF_BCN_PRBRESP_PROMISC is set. however due to atomicity here, we'd
  709. * have to schedule work to enable prbresp reception, which might
  710. * happen too late. For now we'll just listen and forward them all the
  711. * time. */
  712. }
  713. static void set_rts_cts_work(struct work_struct *work)
  714. {
  715. struct zd_mac *mac =
  716. container_of(work, struct zd_mac, set_rts_cts_work);
  717. unsigned long flags;
  718. unsigned int short_preamble;
  719. mutex_lock(&mac->chip.mutex);
  720. spin_lock_irqsave(&mac->lock, flags);
  721. mac->updating_rts_rate = 0;
  722. short_preamble = mac->short_preamble;
  723. spin_unlock_irqrestore(&mac->lock, flags);
  724. zd_chip_set_rts_cts_rate_locked(&mac->chip, short_preamble);
  725. mutex_unlock(&mac->chip.mutex);
  726. }
  727. static void zd_op_bss_info_changed(struct ieee80211_hw *hw,
  728. struct ieee80211_vif *vif,
  729. struct ieee80211_bss_conf *bss_conf,
  730. u32 changes)
  731. {
  732. struct zd_mac *mac = zd_hw_mac(hw);
  733. unsigned long flags;
  734. dev_dbg_f(zd_mac_dev(mac), "changes: %x\n", changes);
  735. if (changes & BSS_CHANGED_ERP_PREAMBLE) {
  736. spin_lock_irqsave(&mac->lock, flags);
  737. mac->short_preamble = bss_conf->use_short_preamble;
  738. if (!mac->updating_rts_rate) {
  739. mac->updating_rts_rate = 1;
  740. /* FIXME: should disable TX here, until work has
  741. * completed and RTS_CTS reg is updated */
  742. queue_work(zd_workqueue, &mac->set_rts_cts_work);
  743. }
  744. spin_unlock_irqrestore(&mac->lock, flags);
  745. }
  746. }
  747. static int zd_op_beacon_update(struct ieee80211_hw *hw,
  748. struct sk_buff *skb)
  749. {
  750. struct zd_mac *mac = zd_hw_mac(hw);
  751. zd_mac_config_beacon(hw, skb);
  752. kfree_skb(skb);
  753. zd_set_beacon_interval(&mac->chip, BCN_MODE_IBSS |
  754. hw->conf.beacon_int);
  755. return 0;
  756. }
  757. static const struct ieee80211_ops zd_ops = {
  758. .tx = zd_op_tx,
  759. .start = zd_op_start,
  760. .stop = zd_op_stop,
  761. .add_interface = zd_op_add_interface,
  762. .remove_interface = zd_op_remove_interface,
  763. .config = zd_op_config,
  764. .config_interface = zd_op_config_interface,
  765. .configure_filter = zd_op_configure_filter,
  766. .bss_info_changed = zd_op_bss_info_changed,
  767. .beacon_update = zd_op_beacon_update,
  768. };
  769. struct ieee80211_hw *zd_mac_alloc_hw(struct usb_interface *intf)
  770. {
  771. struct zd_mac *mac;
  772. struct ieee80211_hw *hw;
  773. hw = ieee80211_alloc_hw(sizeof(struct zd_mac), &zd_ops);
  774. if (!hw) {
  775. dev_dbg_f(&intf->dev, "out of memory\n");
  776. return NULL;
  777. }
  778. mac = zd_hw_mac(hw);
  779. memset(mac, 0, sizeof(*mac));
  780. spin_lock_init(&mac->lock);
  781. mac->hw = hw;
  782. mac->type = IEEE80211_IF_TYPE_INVALID;
  783. memcpy(mac->channels, zd_channels, sizeof(zd_channels));
  784. memcpy(mac->rates, zd_rates, sizeof(zd_rates));
  785. mac->band.n_bitrates = ARRAY_SIZE(zd_rates);
  786. mac->band.bitrates = mac->rates;
  787. mac->band.n_channels = ARRAY_SIZE(zd_channels);
  788. mac->band.channels = mac->channels;
  789. hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &mac->band;
  790. hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
  791. IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE |
  792. IEEE80211_HW_SIGNAL_DB;
  793. hw->max_signal = 100;
  794. hw->queues = 1;
  795. hw->extra_tx_headroom = sizeof(struct zd_ctrlset);
  796. skb_queue_head_init(&mac->ack_wait_queue);
  797. zd_chip_init(&mac->chip, hw, intf);
  798. housekeeping_init(mac);
  799. INIT_WORK(&mac->set_multicast_hash_work, set_multicast_hash_handler);
  800. INIT_WORK(&mac->set_rts_cts_work, set_rts_cts_work);
  801. INIT_WORK(&mac->set_rx_filter_work, set_rx_filter_handler);
  802. INIT_WORK(&mac->process_intr, zd_process_intr);
  803. SET_IEEE80211_DEV(hw, &intf->dev);
  804. return hw;
  805. }
  806. #define LINK_LED_WORK_DELAY HZ
  807. static void link_led_handler(struct work_struct *work)
  808. {
  809. struct zd_mac *mac =
  810. container_of(work, struct zd_mac, housekeeping.link_led_work.work);
  811. struct zd_chip *chip = &mac->chip;
  812. int is_associated;
  813. int r;
  814. spin_lock_irq(&mac->lock);
  815. is_associated = mac->associated;
  816. spin_unlock_irq(&mac->lock);
  817. r = zd_chip_control_leds(chip,
  818. is_associated ? LED_ASSOCIATED : LED_SCANNING);
  819. if (r)
  820. dev_dbg_f(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r);
  821. queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
  822. LINK_LED_WORK_DELAY);
  823. }
  824. static void housekeeping_init(struct zd_mac *mac)
  825. {
  826. INIT_DELAYED_WORK(&mac->housekeeping.link_led_work, link_led_handler);
  827. }
  828. static void housekeeping_enable(struct zd_mac *mac)
  829. {
  830. dev_dbg_f(zd_mac_dev(mac), "\n");
  831. queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
  832. 0);
  833. }
  834. static void housekeeping_disable(struct zd_mac *mac)
  835. {
  836. dev_dbg_f(zd_mac_dev(mac), "\n");
  837. cancel_rearming_delayed_workqueue(zd_workqueue,
  838. &mac->housekeeping.link_led_work);
  839. zd_chip_control_leds(&mac->chip, LED_OFF);
  840. }