zd_mac.c 26 KB

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