zd_mac.c 26 KB

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