zd_mac.c 27 KB

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