zd_mac.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  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 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. goto fail;
  503. info->rate_driver_data[0] = hw;
  504. r = zd_usb_tx(&mac->chip.usb, skb);
  505. if (r)
  506. goto fail;
  507. return 0;
  508. fail:
  509. dev_kfree_skb(skb);
  510. return 0;
  511. }
  512. /**
  513. * filter_ack - filters incoming packets for acknowledgements
  514. * @dev: the mac80211 device
  515. * @rx_hdr: received header
  516. * @stats: the status for the received packet
  517. *
  518. * This functions looks for ACK packets and tries to match them with the
  519. * frames in the tx queue. If a match is found the frame will be dequeued and
  520. * the upper layers is informed about the successful transmission. If
  521. * mac80211 queues have been stopped and the number of frames still to be
  522. * transmitted is low the queues will be opened again.
  523. *
  524. * Returns 1 if the frame was an ACK, 0 if it was ignored.
  525. */
  526. static int filter_ack(struct ieee80211_hw *hw, struct ieee80211_hdr *rx_hdr,
  527. struct ieee80211_rx_status *stats)
  528. {
  529. struct sk_buff *skb;
  530. struct sk_buff_head *q;
  531. unsigned long flags;
  532. if (!ieee80211_is_ack(rx_hdr->frame_control))
  533. return 0;
  534. q = &zd_hw_mac(hw)->ack_wait_queue;
  535. spin_lock_irqsave(&q->lock, flags);
  536. skb_queue_walk(q, skb) {
  537. struct ieee80211_hdr *tx_hdr;
  538. tx_hdr = (struct ieee80211_hdr *)skb->data;
  539. if (likely(!memcmp(tx_hdr->addr2, rx_hdr->addr1, ETH_ALEN)))
  540. {
  541. __skb_unlink(skb, q);
  542. tx_status(hw, skb, stats->signal, 1);
  543. goto out;
  544. }
  545. }
  546. out:
  547. spin_unlock_irqrestore(&q->lock, flags);
  548. return 1;
  549. }
  550. int zd_mac_rx(struct ieee80211_hw *hw, const u8 *buffer, unsigned int length)
  551. {
  552. struct zd_mac *mac = zd_hw_mac(hw);
  553. struct ieee80211_rx_status stats;
  554. const struct rx_status *status;
  555. struct sk_buff *skb;
  556. int bad_frame = 0;
  557. __le16 fc;
  558. int need_padding;
  559. int i;
  560. u8 rate;
  561. if (length < ZD_PLCP_HEADER_SIZE + 10 /* IEEE80211_1ADDR_LEN */ +
  562. FCS_LEN + sizeof(struct rx_status))
  563. return -EINVAL;
  564. memset(&stats, 0, sizeof(stats));
  565. /* Note about pass_failed_fcs and pass_ctrl access below:
  566. * mac locking intentionally omitted here, as this is the only unlocked
  567. * reader and the only writer is configure_filter. Plus, if there were
  568. * any races accessing these variables, it wouldn't really matter.
  569. * If mac80211 ever provides a way for us to access filter flags
  570. * from outside configure_filter, we could improve on this. Also, this
  571. * situation may change once we implement some kind of DMA-into-skb
  572. * RX path. */
  573. /* Caller has to ensure that length >= sizeof(struct rx_status). */
  574. status = (struct rx_status *)
  575. (buffer + (length - sizeof(struct rx_status)));
  576. if (status->frame_status & ZD_RX_ERROR) {
  577. if (mac->pass_failed_fcs &&
  578. (status->frame_status & ZD_RX_CRC32_ERROR)) {
  579. stats.flag |= RX_FLAG_FAILED_FCS_CRC;
  580. bad_frame = 1;
  581. } else {
  582. return -EINVAL;
  583. }
  584. }
  585. stats.freq = zd_channels[_zd_chip_get_channel(&mac->chip) - 1].center_freq;
  586. stats.band = IEEE80211_BAND_2GHZ;
  587. stats.signal = status->signal_strength;
  588. stats.qual = zd_rx_qual_percent(buffer,
  589. length - sizeof(struct rx_status),
  590. status);
  591. rate = zd_rx_rate(buffer, status);
  592. /* todo: return index in the big switches in zd_rx_rate instead */
  593. for (i = 0; i < mac->band.n_bitrates; i++)
  594. if (rate == mac->band.bitrates[i].hw_value)
  595. stats.rate_idx = i;
  596. length -= ZD_PLCP_HEADER_SIZE + sizeof(struct rx_status);
  597. buffer += ZD_PLCP_HEADER_SIZE;
  598. /* Except for bad frames, filter each frame to see if it is an ACK, in
  599. * which case our internal TX tracking is updated. Normally we then
  600. * bail here as there's no need to pass ACKs on up to the stack, but
  601. * there is also the case where the stack has requested us to pass
  602. * control frames on up (pass_ctrl) which we must consider. */
  603. if (!bad_frame &&
  604. filter_ack(hw, (struct ieee80211_hdr *)buffer, &stats)
  605. && !mac->pass_ctrl)
  606. return 0;
  607. fc = *(__le16 *)buffer;
  608. need_padding = ieee80211_is_data_qos(fc) ^ ieee80211_has_a4(fc);
  609. skb = dev_alloc_skb(length + (need_padding ? 2 : 0));
  610. if (skb == NULL)
  611. return -ENOMEM;
  612. if (need_padding) {
  613. /* Make sure the the payload data is 4 byte aligned. */
  614. skb_reserve(skb, 2);
  615. }
  616. memcpy(skb_put(skb, length), buffer, length);
  617. ieee80211_rx_irqsafe(hw, skb, &stats);
  618. return 0;
  619. }
  620. static int zd_op_add_interface(struct ieee80211_hw *hw,
  621. struct ieee80211_if_init_conf *conf)
  622. {
  623. struct zd_mac *mac = zd_hw_mac(hw);
  624. /* using NL80211_IFTYPE_UNSPECIFIED to indicate no mode selected */
  625. if (mac->type != NL80211_IFTYPE_UNSPECIFIED)
  626. return -EOPNOTSUPP;
  627. switch (conf->type) {
  628. case NL80211_IFTYPE_MONITOR:
  629. case NL80211_IFTYPE_MESH_POINT:
  630. case NL80211_IFTYPE_STATION:
  631. case NL80211_IFTYPE_ADHOC:
  632. mac->type = conf->type;
  633. break;
  634. default:
  635. return -EOPNOTSUPP;
  636. }
  637. return zd_write_mac_addr(&mac->chip, conf->mac_addr);
  638. }
  639. static void zd_op_remove_interface(struct ieee80211_hw *hw,
  640. struct ieee80211_if_init_conf *conf)
  641. {
  642. struct zd_mac *mac = zd_hw_mac(hw);
  643. mac->type = NL80211_IFTYPE_UNSPECIFIED;
  644. zd_set_beacon_interval(&mac->chip, 0);
  645. zd_write_mac_addr(&mac->chip, NULL);
  646. }
  647. static int zd_op_config(struct ieee80211_hw *hw, u32 changed)
  648. {
  649. struct zd_mac *mac = zd_hw_mac(hw);
  650. struct ieee80211_conf *conf = &hw->conf;
  651. return zd_chip_set_channel(&mac->chip, conf->channel->hw_value);
  652. }
  653. static int zd_op_config_interface(struct ieee80211_hw *hw,
  654. struct ieee80211_vif *vif,
  655. struct ieee80211_if_conf *conf)
  656. {
  657. struct zd_mac *mac = zd_hw_mac(hw);
  658. int associated;
  659. int r;
  660. if (mac->type == NL80211_IFTYPE_MESH_POINT ||
  661. mac->type == NL80211_IFTYPE_ADHOC) {
  662. associated = true;
  663. if (conf->changed & IEEE80211_IFCC_BEACON) {
  664. struct sk_buff *beacon = ieee80211_beacon_get(hw, vif);
  665. if (!beacon)
  666. return -ENOMEM;
  667. r = zd_mac_config_beacon(hw, beacon);
  668. if (r < 0)
  669. return r;
  670. r = zd_set_beacon_interval(&mac->chip, BCN_MODE_IBSS |
  671. hw->conf.beacon_int);
  672. if (r < 0)
  673. return r;
  674. kfree_skb(beacon);
  675. }
  676. } else
  677. associated = is_valid_ether_addr(conf->bssid);
  678. spin_lock_irq(&mac->lock);
  679. mac->associated = associated;
  680. spin_unlock_irq(&mac->lock);
  681. /* TODO: do hardware bssid filtering */
  682. return 0;
  683. }
  684. static void zd_process_intr(struct work_struct *work)
  685. {
  686. u16 int_status;
  687. struct zd_mac *mac = container_of(work, struct zd_mac, process_intr);
  688. int_status = le16_to_cpu(*(__le16 *)(mac->intr_buffer+4));
  689. if (int_status & INT_CFG_NEXT_BCN) {
  690. if (net_ratelimit())
  691. dev_dbg_f(zd_mac_dev(mac), "INT_CFG_NEXT_BCN\n");
  692. } else
  693. dev_dbg_f(zd_mac_dev(mac), "Unsupported interrupt\n");
  694. zd_chip_enable_hwint(&mac->chip);
  695. }
  696. static void set_multicast_hash_handler(struct work_struct *work)
  697. {
  698. struct zd_mac *mac =
  699. container_of(work, struct zd_mac, set_multicast_hash_work);
  700. struct zd_mc_hash hash;
  701. spin_lock_irq(&mac->lock);
  702. hash = mac->multicast_hash;
  703. spin_unlock_irq(&mac->lock);
  704. zd_chip_set_multicast_hash(&mac->chip, &hash);
  705. }
  706. static void set_rx_filter_handler(struct work_struct *work)
  707. {
  708. struct zd_mac *mac =
  709. container_of(work, struct zd_mac, set_rx_filter_work);
  710. int r;
  711. dev_dbg_f(zd_mac_dev(mac), "\n");
  712. r = set_rx_filter(mac);
  713. if (r)
  714. dev_err(zd_mac_dev(mac), "set_rx_filter_handler error %d\n", r);
  715. }
  716. #define SUPPORTED_FIF_FLAGS \
  717. (FIF_PROMISC_IN_BSS | FIF_ALLMULTI | FIF_FCSFAIL | FIF_CONTROL | \
  718. FIF_OTHER_BSS | FIF_BCN_PRBRESP_PROMISC)
  719. static void zd_op_configure_filter(struct ieee80211_hw *hw,
  720. unsigned int changed_flags,
  721. unsigned int *new_flags,
  722. int mc_count, struct dev_mc_list *mclist)
  723. {
  724. struct zd_mc_hash hash;
  725. struct zd_mac *mac = zd_hw_mac(hw);
  726. unsigned long flags;
  727. int i;
  728. /* Only deal with supported flags */
  729. changed_flags &= SUPPORTED_FIF_FLAGS;
  730. *new_flags &= SUPPORTED_FIF_FLAGS;
  731. /* changed_flags is always populated but this driver
  732. * doesn't support all FIF flags so its possible we don't
  733. * need to do anything */
  734. if (!changed_flags)
  735. return;
  736. if (*new_flags & (FIF_PROMISC_IN_BSS | FIF_ALLMULTI)) {
  737. zd_mc_add_all(&hash);
  738. } else {
  739. zd_mc_clear(&hash);
  740. for (i = 0; i < mc_count; i++) {
  741. if (!mclist)
  742. break;
  743. dev_dbg_f(zd_mac_dev(mac), "mc addr %pM\n",
  744. mclist->dmi_addr);
  745. zd_mc_add_addr(&hash, mclist->dmi_addr);
  746. mclist = mclist->next;
  747. }
  748. }
  749. spin_lock_irqsave(&mac->lock, flags);
  750. mac->pass_failed_fcs = !!(*new_flags & FIF_FCSFAIL);
  751. mac->pass_ctrl = !!(*new_flags & FIF_CONTROL);
  752. mac->multicast_hash = hash;
  753. spin_unlock_irqrestore(&mac->lock, flags);
  754. queue_work(zd_workqueue, &mac->set_multicast_hash_work);
  755. if (changed_flags & FIF_CONTROL)
  756. queue_work(zd_workqueue, &mac->set_rx_filter_work);
  757. /* no handling required for FIF_OTHER_BSS as we don't currently
  758. * do BSSID filtering */
  759. /* FIXME: in future it would be nice to enable the probe response
  760. * filter (so that the driver doesn't see them) until
  761. * FIF_BCN_PRBRESP_PROMISC is set. however due to atomicity here, we'd
  762. * have to schedule work to enable prbresp reception, which might
  763. * happen too late. For now we'll just listen and forward them all the
  764. * time. */
  765. }
  766. static void set_rts_cts_work(struct work_struct *work)
  767. {
  768. struct zd_mac *mac =
  769. container_of(work, struct zd_mac, set_rts_cts_work);
  770. unsigned long flags;
  771. unsigned int short_preamble;
  772. mutex_lock(&mac->chip.mutex);
  773. spin_lock_irqsave(&mac->lock, flags);
  774. mac->updating_rts_rate = 0;
  775. short_preamble = mac->short_preamble;
  776. spin_unlock_irqrestore(&mac->lock, flags);
  777. zd_chip_set_rts_cts_rate_locked(&mac->chip, short_preamble);
  778. mutex_unlock(&mac->chip.mutex);
  779. }
  780. static void zd_op_bss_info_changed(struct ieee80211_hw *hw,
  781. struct ieee80211_vif *vif,
  782. struct ieee80211_bss_conf *bss_conf,
  783. u32 changes)
  784. {
  785. struct zd_mac *mac = zd_hw_mac(hw);
  786. unsigned long flags;
  787. dev_dbg_f(zd_mac_dev(mac), "changes: %x\n", changes);
  788. if (changes & BSS_CHANGED_ERP_PREAMBLE) {
  789. spin_lock_irqsave(&mac->lock, flags);
  790. mac->short_preamble = bss_conf->use_short_preamble;
  791. if (!mac->updating_rts_rate) {
  792. mac->updating_rts_rate = 1;
  793. /* FIXME: should disable TX here, until work has
  794. * completed and RTS_CTS reg is updated */
  795. queue_work(zd_workqueue, &mac->set_rts_cts_work);
  796. }
  797. spin_unlock_irqrestore(&mac->lock, flags);
  798. }
  799. }
  800. static const struct ieee80211_ops zd_ops = {
  801. .tx = zd_op_tx,
  802. .start = zd_op_start,
  803. .stop = zd_op_stop,
  804. .add_interface = zd_op_add_interface,
  805. .remove_interface = zd_op_remove_interface,
  806. .config = zd_op_config,
  807. .config_interface = zd_op_config_interface,
  808. .configure_filter = zd_op_configure_filter,
  809. .bss_info_changed = zd_op_bss_info_changed,
  810. };
  811. struct ieee80211_hw *zd_mac_alloc_hw(struct usb_interface *intf)
  812. {
  813. struct zd_mac *mac;
  814. struct ieee80211_hw *hw;
  815. hw = ieee80211_alloc_hw(sizeof(struct zd_mac), &zd_ops);
  816. if (!hw) {
  817. dev_dbg_f(&intf->dev, "out of memory\n");
  818. return NULL;
  819. }
  820. mac = zd_hw_mac(hw);
  821. memset(mac, 0, sizeof(*mac));
  822. spin_lock_init(&mac->lock);
  823. mac->hw = hw;
  824. mac->type = NL80211_IFTYPE_UNSPECIFIED;
  825. memcpy(mac->channels, zd_channels, sizeof(zd_channels));
  826. memcpy(mac->rates, zd_rates, sizeof(zd_rates));
  827. mac->band.n_bitrates = ARRAY_SIZE(zd_rates);
  828. mac->band.bitrates = mac->rates;
  829. mac->band.n_channels = ARRAY_SIZE(zd_channels);
  830. mac->band.channels = mac->channels;
  831. hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &mac->band;
  832. hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
  833. IEEE80211_HW_SIGNAL_DB;
  834. hw->wiphy->interface_modes =
  835. BIT(NL80211_IFTYPE_MESH_POINT) |
  836. BIT(NL80211_IFTYPE_STATION) |
  837. BIT(NL80211_IFTYPE_ADHOC);
  838. hw->max_signal = 100;
  839. hw->queues = 1;
  840. hw->extra_tx_headroom = sizeof(struct zd_ctrlset);
  841. skb_queue_head_init(&mac->ack_wait_queue);
  842. zd_chip_init(&mac->chip, hw, intf);
  843. housekeeping_init(mac);
  844. INIT_WORK(&mac->set_multicast_hash_work, set_multicast_hash_handler);
  845. INIT_WORK(&mac->set_rts_cts_work, set_rts_cts_work);
  846. INIT_WORK(&mac->set_rx_filter_work, set_rx_filter_handler);
  847. INIT_WORK(&mac->process_intr, zd_process_intr);
  848. SET_IEEE80211_DEV(hw, &intf->dev);
  849. return hw;
  850. }
  851. #define LINK_LED_WORK_DELAY HZ
  852. static void link_led_handler(struct work_struct *work)
  853. {
  854. struct zd_mac *mac =
  855. container_of(work, struct zd_mac, housekeeping.link_led_work.work);
  856. struct zd_chip *chip = &mac->chip;
  857. int is_associated;
  858. int r;
  859. spin_lock_irq(&mac->lock);
  860. is_associated = mac->associated;
  861. spin_unlock_irq(&mac->lock);
  862. r = zd_chip_control_leds(chip,
  863. is_associated ? LED_ASSOCIATED : LED_SCANNING);
  864. if (r)
  865. dev_dbg_f(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r);
  866. queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
  867. LINK_LED_WORK_DELAY);
  868. }
  869. static void housekeeping_init(struct zd_mac *mac)
  870. {
  871. INIT_DELAYED_WORK(&mac->housekeeping.link_led_work, link_led_handler);
  872. }
  873. static void housekeeping_enable(struct zd_mac *mac)
  874. {
  875. dev_dbg_f(zd_mac_dev(mac), "\n");
  876. queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
  877. 0);
  878. }
  879. static void housekeeping_disable(struct zd_mac *mac)
  880. {
  881. dev_dbg_f(zd_mac_dev(mac), "\n");
  882. cancel_rearming_delayed_workqueue(zd_workqueue,
  883. &mac->housekeeping.link_led_work);
  884. zd_chip_control_leds(&mac->chip, LED_OFF);
  885. }