zd_mac.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  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. goto disable_int;
  157. r = regulatory_hint(hw->wiphy, alpha2);
  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 successful 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. kfree_skb(beacon);
  666. if (r < 0)
  667. return r;
  668. }
  669. if (conf->changed & IEEE80211_IFCC_BEACON_ENABLED) {
  670. u32 interval;
  671. if (conf->enable_beacon)
  672. interval = BCN_MODE_IBSS | hw->conf.beacon_int;
  673. else
  674. interval = 0;
  675. r = zd_set_beacon_interval(&mac->chip, interval);
  676. if (r < 0)
  677. return r;
  678. }
  679. } else
  680. associated = is_valid_ether_addr(conf->bssid);
  681. spin_lock_irq(&mac->lock);
  682. mac->associated = associated;
  683. spin_unlock_irq(&mac->lock);
  684. /* TODO: do hardware bssid filtering */
  685. return 0;
  686. }
  687. static void zd_process_intr(struct work_struct *work)
  688. {
  689. u16 int_status;
  690. struct zd_mac *mac = container_of(work, struct zd_mac, process_intr);
  691. int_status = le16_to_cpu(*(__le16 *)(mac->intr_buffer+4));
  692. if (int_status & INT_CFG_NEXT_BCN)
  693. dev_dbg_f_limit(zd_mac_dev(mac), "INT_CFG_NEXT_BCN\n");
  694. else
  695. dev_dbg_f(zd_mac_dev(mac), "Unsupported interrupt\n");
  696. zd_chip_enable_hwint(&mac->chip);
  697. }
  698. static void set_multicast_hash_handler(struct work_struct *work)
  699. {
  700. struct zd_mac *mac =
  701. container_of(work, struct zd_mac, set_multicast_hash_work);
  702. struct zd_mc_hash hash;
  703. spin_lock_irq(&mac->lock);
  704. hash = mac->multicast_hash;
  705. spin_unlock_irq(&mac->lock);
  706. zd_chip_set_multicast_hash(&mac->chip, &hash);
  707. }
  708. static void set_rx_filter_handler(struct work_struct *work)
  709. {
  710. struct zd_mac *mac =
  711. container_of(work, struct zd_mac, set_rx_filter_work);
  712. int r;
  713. dev_dbg_f(zd_mac_dev(mac), "\n");
  714. r = set_rx_filter(mac);
  715. if (r)
  716. dev_err(zd_mac_dev(mac), "set_rx_filter_handler error %d\n", r);
  717. }
  718. #define SUPPORTED_FIF_FLAGS \
  719. (FIF_PROMISC_IN_BSS | FIF_ALLMULTI | FIF_FCSFAIL | FIF_CONTROL | \
  720. FIF_OTHER_BSS | FIF_BCN_PRBRESP_PROMISC)
  721. static void zd_op_configure_filter(struct ieee80211_hw *hw,
  722. unsigned int changed_flags,
  723. unsigned int *new_flags,
  724. int mc_count, struct dev_mc_list *mclist)
  725. {
  726. struct zd_mc_hash hash;
  727. struct zd_mac *mac = zd_hw_mac(hw);
  728. unsigned long flags;
  729. int i;
  730. /* Only deal with supported flags */
  731. changed_flags &= SUPPORTED_FIF_FLAGS;
  732. *new_flags &= SUPPORTED_FIF_FLAGS;
  733. /* changed_flags is always populated but this driver
  734. * doesn't support all FIF flags so its possible we don't
  735. * need to do anything */
  736. if (!changed_flags)
  737. return;
  738. if (*new_flags & (FIF_PROMISC_IN_BSS | FIF_ALLMULTI)) {
  739. zd_mc_add_all(&hash);
  740. } else {
  741. zd_mc_clear(&hash);
  742. for (i = 0; i < mc_count; i++) {
  743. if (!mclist)
  744. break;
  745. dev_dbg_f(zd_mac_dev(mac), "mc addr %pM\n",
  746. mclist->dmi_addr);
  747. zd_mc_add_addr(&hash, mclist->dmi_addr);
  748. mclist = mclist->next;
  749. }
  750. }
  751. spin_lock_irqsave(&mac->lock, flags);
  752. mac->pass_failed_fcs = !!(*new_flags & FIF_FCSFAIL);
  753. mac->pass_ctrl = !!(*new_flags & FIF_CONTROL);
  754. mac->multicast_hash = hash;
  755. spin_unlock_irqrestore(&mac->lock, flags);
  756. queue_work(zd_workqueue, &mac->set_multicast_hash_work);
  757. if (changed_flags & FIF_CONTROL)
  758. queue_work(zd_workqueue, &mac->set_rx_filter_work);
  759. /* no handling required for FIF_OTHER_BSS as we don't currently
  760. * do BSSID filtering */
  761. /* FIXME: in future it would be nice to enable the probe response
  762. * filter (so that the driver doesn't see them) until
  763. * FIF_BCN_PRBRESP_PROMISC is set. however due to atomicity here, we'd
  764. * have to schedule work to enable prbresp reception, which might
  765. * happen too late. For now we'll just listen and forward them all the
  766. * time. */
  767. }
  768. static void set_rts_cts_work(struct work_struct *work)
  769. {
  770. struct zd_mac *mac =
  771. container_of(work, struct zd_mac, set_rts_cts_work);
  772. unsigned long flags;
  773. unsigned int short_preamble;
  774. mutex_lock(&mac->chip.mutex);
  775. spin_lock_irqsave(&mac->lock, flags);
  776. mac->updating_rts_rate = 0;
  777. short_preamble = mac->short_preamble;
  778. spin_unlock_irqrestore(&mac->lock, flags);
  779. zd_chip_set_rts_cts_rate_locked(&mac->chip, short_preamble);
  780. mutex_unlock(&mac->chip.mutex);
  781. }
  782. static void zd_op_bss_info_changed(struct ieee80211_hw *hw,
  783. struct ieee80211_vif *vif,
  784. struct ieee80211_bss_conf *bss_conf,
  785. u32 changes)
  786. {
  787. struct zd_mac *mac = zd_hw_mac(hw);
  788. unsigned long flags;
  789. dev_dbg_f(zd_mac_dev(mac), "changes: %x\n", changes);
  790. if (changes & BSS_CHANGED_ERP_PREAMBLE) {
  791. spin_lock_irqsave(&mac->lock, flags);
  792. mac->short_preamble = bss_conf->use_short_preamble;
  793. if (!mac->updating_rts_rate) {
  794. mac->updating_rts_rate = 1;
  795. /* FIXME: should disable TX here, until work has
  796. * completed and RTS_CTS reg is updated */
  797. queue_work(zd_workqueue, &mac->set_rts_cts_work);
  798. }
  799. spin_unlock_irqrestore(&mac->lock, flags);
  800. }
  801. }
  802. static u64 zd_op_get_tsf(struct ieee80211_hw *hw)
  803. {
  804. struct zd_mac *mac = zd_hw_mac(hw);
  805. return zd_chip_get_tsf(&mac->chip);
  806. }
  807. static const struct ieee80211_ops zd_ops = {
  808. .tx = zd_op_tx,
  809. .start = zd_op_start,
  810. .stop = zd_op_stop,
  811. .add_interface = zd_op_add_interface,
  812. .remove_interface = zd_op_remove_interface,
  813. .config = zd_op_config,
  814. .config_interface = zd_op_config_interface,
  815. .configure_filter = zd_op_configure_filter,
  816. .bss_info_changed = zd_op_bss_info_changed,
  817. .get_tsf = zd_op_get_tsf,
  818. };
  819. struct ieee80211_hw *zd_mac_alloc_hw(struct usb_interface *intf)
  820. {
  821. struct zd_mac *mac;
  822. struct ieee80211_hw *hw;
  823. hw = ieee80211_alloc_hw(sizeof(struct zd_mac), &zd_ops);
  824. if (!hw) {
  825. dev_dbg_f(&intf->dev, "out of memory\n");
  826. return NULL;
  827. }
  828. mac = zd_hw_mac(hw);
  829. memset(mac, 0, sizeof(*mac));
  830. spin_lock_init(&mac->lock);
  831. mac->hw = hw;
  832. mac->type = NL80211_IFTYPE_UNSPECIFIED;
  833. memcpy(mac->channels, zd_channels, sizeof(zd_channels));
  834. memcpy(mac->rates, zd_rates, sizeof(zd_rates));
  835. mac->band.n_bitrates = ARRAY_SIZE(zd_rates);
  836. mac->band.bitrates = mac->rates;
  837. mac->band.n_channels = ARRAY_SIZE(zd_channels);
  838. mac->band.channels = mac->channels;
  839. hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &mac->band;
  840. hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
  841. IEEE80211_HW_SIGNAL_UNSPEC;
  842. hw->wiphy->interface_modes =
  843. BIT(NL80211_IFTYPE_MESH_POINT) |
  844. BIT(NL80211_IFTYPE_STATION) |
  845. BIT(NL80211_IFTYPE_ADHOC);
  846. hw->max_signal = 100;
  847. hw->queues = 1;
  848. hw->extra_tx_headroom = sizeof(struct zd_ctrlset);
  849. skb_queue_head_init(&mac->ack_wait_queue);
  850. zd_chip_init(&mac->chip, hw, intf);
  851. housekeeping_init(mac);
  852. INIT_WORK(&mac->set_multicast_hash_work, set_multicast_hash_handler);
  853. INIT_WORK(&mac->set_rts_cts_work, set_rts_cts_work);
  854. INIT_WORK(&mac->set_rx_filter_work, set_rx_filter_handler);
  855. INIT_WORK(&mac->process_intr, zd_process_intr);
  856. SET_IEEE80211_DEV(hw, &intf->dev);
  857. return hw;
  858. }
  859. #define LINK_LED_WORK_DELAY HZ
  860. static void link_led_handler(struct work_struct *work)
  861. {
  862. struct zd_mac *mac =
  863. container_of(work, struct zd_mac, housekeeping.link_led_work.work);
  864. struct zd_chip *chip = &mac->chip;
  865. int is_associated;
  866. int r;
  867. spin_lock_irq(&mac->lock);
  868. is_associated = mac->associated;
  869. spin_unlock_irq(&mac->lock);
  870. r = zd_chip_control_leds(chip,
  871. is_associated ? LED_ASSOCIATED : LED_SCANNING);
  872. if (r)
  873. dev_dbg_f(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r);
  874. queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
  875. LINK_LED_WORK_DELAY);
  876. }
  877. static void housekeeping_init(struct zd_mac *mac)
  878. {
  879. INIT_DELAYED_WORK(&mac->housekeeping.link_led_work, link_led_handler);
  880. }
  881. static void housekeeping_enable(struct zd_mac *mac)
  882. {
  883. dev_dbg_f(zd_mac_dev(mac), "\n");
  884. queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
  885. 0);
  886. }
  887. static void housekeeping_disable(struct zd_mac *mac)
  888. {
  889. dev_dbg_f(zd_mac_dev(mac), "\n");
  890. cancel_rearming_delayed_workqueue(zd_workqueue,
  891. &mac->housekeeping.link_led_work);
  892. zd_chip_control_leds(&mac->chip, LED_OFF);
  893. }