zd_mac.c 27 KB

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