zd_mac.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552
  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, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <linux/netdevice.h>
  22. #include <linux/etherdevice.h>
  23. #include <linux/slab.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_2, "JP" },
  41. { ZD_REGDOMAIN_JAPAN_3, "JP" },
  42. { ZD_REGDOMAIN_SPAIN, "ES" },
  43. { ZD_REGDOMAIN_FRANCE, "FR" },
  44. };
  45. /* This table contains the hardware specific values for the modulation rates. */
  46. static const struct ieee80211_rate zd_rates[] = {
  47. { .bitrate = 10,
  48. .hw_value = ZD_CCK_RATE_1M, },
  49. { .bitrate = 20,
  50. .hw_value = ZD_CCK_RATE_2M,
  51. .hw_value_short = ZD_CCK_RATE_2M | ZD_CCK_PREA_SHORT,
  52. .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  53. { .bitrate = 55,
  54. .hw_value = ZD_CCK_RATE_5_5M,
  55. .hw_value_short = ZD_CCK_RATE_5_5M | ZD_CCK_PREA_SHORT,
  56. .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  57. { .bitrate = 110,
  58. .hw_value = ZD_CCK_RATE_11M,
  59. .hw_value_short = ZD_CCK_RATE_11M | ZD_CCK_PREA_SHORT,
  60. .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  61. { .bitrate = 60,
  62. .hw_value = ZD_OFDM_RATE_6M,
  63. .flags = 0 },
  64. { .bitrate = 90,
  65. .hw_value = ZD_OFDM_RATE_9M,
  66. .flags = 0 },
  67. { .bitrate = 120,
  68. .hw_value = ZD_OFDM_RATE_12M,
  69. .flags = 0 },
  70. { .bitrate = 180,
  71. .hw_value = ZD_OFDM_RATE_18M,
  72. .flags = 0 },
  73. { .bitrate = 240,
  74. .hw_value = ZD_OFDM_RATE_24M,
  75. .flags = 0 },
  76. { .bitrate = 360,
  77. .hw_value = ZD_OFDM_RATE_36M,
  78. .flags = 0 },
  79. { .bitrate = 480,
  80. .hw_value = ZD_OFDM_RATE_48M,
  81. .flags = 0 },
  82. { .bitrate = 540,
  83. .hw_value = ZD_OFDM_RATE_54M,
  84. .flags = 0 },
  85. };
  86. /*
  87. * Zydas retry rates table. Each line is listed in the same order as
  88. * in zd_rates[] and contains all the rate used when a packet is sent
  89. * starting with a given rates. Let's consider an example :
  90. *
  91. * "11 Mbits : 4, 3, 2, 1, 0" means :
  92. * - packet is sent using 4 different rates
  93. * - 1st rate is index 3 (ie 11 Mbits)
  94. * - 2nd rate is index 2 (ie 5.5 Mbits)
  95. * - 3rd rate is index 1 (ie 2 Mbits)
  96. * - 4th rate is index 0 (ie 1 Mbits)
  97. */
  98. static const struct tx_retry_rate zd_retry_rates[] = {
  99. { /* 1 Mbits */ 1, { 0 }},
  100. { /* 2 Mbits */ 2, { 1, 0 }},
  101. { /* 5.5 Mbits */ 3, { 2, 1, 0 }},
  102. { /* 11 Mbits */ 4, { 3, 2, 1, 0 }},
  103. { /* 6 Mbits */ 5, { 4, 3, 2, 1, 0 }},
  104. { /* 9 Mbits */ 6, { 5, 4, 3, 2, 1, 0}},
  105. { /* 12 Mbits */ 5, { 6, 3, 2, 1, 0 }},
  106. { /* 18 Mbits */ 6, { 7, 6, 3, 2, 1, 0 }},
  107. { /* 24 Mbits */ 6, { 8, 6, 3, 2, 1, 0 }},
  108. { /* 36 Mbits */ 7, { 9, 8, 6, 3, 2, 1, 0 }},
  109. { /* 48 Mbits */ 8, {10, 9, 8, 6, 3, 2, 1, 0 }},
  110. { /* 54 Mbits */ 9, {11, 10, 9, 8, 6, 3, 2, 1, 0 }}
  111. };
  112. static const struct ieee80211_channel zd_channels[] = {
  113. { .center_freq = 2412, .hw_value = 1 },
  114. { .center_freq = 2417, .hw_value = 2 },
  115. { .center_freq = 2422, .hw_value = 3 },
  116. { .center_freq = 2427, .hw_value = 4 },
  117. { .center_freq = 2432, .hw_value = 5 },
  118. { .center_freq = 2437, .hw_value = 6 },
  119. { .center_freq = 2442, .hw_value = 7 },
  120. { .center_freq = 2447, .hw_value = 8 },
  121. { .center_freq = 2452, .hw_value = 9 },
  122. { .center_freq = 2457, .hw_value = 10 },
  123. { .center_freq = 2462, .hw_value = 11 },
  124. { .center_freq = 2467, .hw_value = 12 },
  125. { .center_freq = 2472, .hw_value = 13 },
  126. { .center_freq = 2484, .hw_value = 14 },
  127. };
  128. static void housekeeping_init(struct zd_mac *mac);
  129. static void housekeeping_enable(struct zd_mac *mac);
  130. static void housekeeping_disable(struct zd_mac *mac);
  131. static void beacon_init(struct zd_mac *mac);
  132. static void beacon_enable(struct zd_mac *mac);
  133. static void beacon_disable(struct zd_mac *mac);
  134. static void set_rts_cts(struct zd_mac *mac, unsigned int short_preamble);
  135. static int zd_mac_config_beacon(struct ieee80211_hw *hw,
  136. struct sk_buff *beacon, bool in_intr);
  137. static int zd_reg2alpha2(u8 regdomain, char *alpha2)
  138. {
  139. unsigned int i;
  140. struct zd_reg_alpha2_map *reg_map;
  141. for (i = 0; i < ARRAY_SIZE(reg_alpha2_map); i++) {
  142. reg_map = &reg_alpha2_map[i];
  143. if (regdomain == reg_map->reg) {
  144. alpha2[0] = reg_map->alpha2[0];
  145. alpha2[1] = reg_map->alpha2[1];
  146. return 0;
  147. }
  148. }
  149. return 1;
  150. }
  151. static int zd_check_signal(struct ieee80211_hw *hw, int signal)
  152. {
  153. struct zd_mac *mac = zd_hw_mac(hw);
  154. dev_dbg_f_cond(zd_mac_dev(mac), signal < 0 || signal > 100,
  155. "%s: signal value from device not in range 0..100, "
  156. "but %d.\n", __func__, signal);
  157. if (signal < 0)
  158. signal = 0;
  159. else if (signal > 100)
  160. signal = 100;
  161. return signal;
  162. }
  163. int zd_mac_preinit_hw(struct ieee80211_hw *hw)
  164. {
  165. int r;
  166. u8 addr[ETH_ALEN];
  167. struct zd_mac *mac = zd_hw_mac(hw);
  168. r = zd_chip_read_mac_addr_fw(&mac->chip, addr);
  169. if (r)
  170. return r;
  171. SET_IEEE80211_PERM_ADDR(hw, addr);
  172. return 0;
  173. }
  174. int zd_mac_init_hw(struct ieee80211_hw *hw)
  175. {
  176. int r;
  177. struct zd_mac *mac = zd_hw_mac(hw);
  178. struct zd_chip *chip = &mac->chip;
  179. char alpha2[2];
  180. u8 default_regdomain;
  181. r = zd_chip_enable_int(chip);
  182. if (r)
  183. goto out;
  184. r = zd_chip_init_hw(chip);
  185. if (r)
  186. goto disable_int;
  187. ZD_ASSERT(!irqs_disabled());
  188. r = zd_read_regdomain(chip, &default_regdomain);
  189. if (r)
  190. goto disable_int;
  191. spin_lock_irq(&mac->lock);
  192. mac->regdomain = mac->default_regdomain = default_regdomain;
  193. spin_unlock_irq(&mac->lock);
  194. /* We must inform the device that we are doing encryption/decryption in
  195. * software at the moment. */
  196. r = zd_set_encryption_type(chip, ENC_SNIFFER);
  197. if (r)
  198. goto disable_int;
  199. r = zd_reg2alpha2(mac->regdomain, alpha2);
  200. if (r)
  201. goto disable_int;
  202. r = regulatory_hint(hw->wiphy, alpha2);
  203. disable_int:
  204. zd_chip_disable_int(chip);
  205. out:
  206. return r;
  207. }
  208. void zd_mac_clear(struct zd_mac *mac)
  209. {
  210. flush_workqueue(zd_workqueue);
  211. zd_chip_clear(&mac->chip);
  212. ZD_ASSERT(!spin_is_locked(&mac->lock));
  213. ZD_MEMCLEAR(mac, sizeof(struct zd_mac));
  214. }
  215. static int set_rx_filter(struct zd_mac *mac)
  216. {
  217. unsigned long flags;
  218. u32 filter = STA_RX_FILTER;
  219. spin_lock_irqsave(&mac->lock, flags);
  220. if (mac->pass_ctrl)
  221. filter |= RX_FILTER_CTRL;
  222. spin_unlock_irqrestore(&mac->lock, flags);
  223. return zd_iowrite32(&mac->chip, CR_RX_FILTER, filter);
  224. }
  225. static int set_mac_and_bssid(struct zd_mac *mac)
  226. {
  227. int r;
  228. if (!mac->vif)
  229. return -1;
  230. r = zd_write_mac_addr(&mac->chip, mac->vif->addr);
  231. if (r)
  232. return r;
  233. /* Vendor driver after setting MAC either sets BSSID for AP or
  234. * filter for other modes.
  235. */
  236. if (mac->type != NL80211_IFTYPE_AP)
  237. return set_rx_filter(mac);
  238. else
  239. return zd_write_bssid(&mac->chip, mac->vif->addr);
  240. }
  241. static int set_mc_hash(struct zd_mac *mac)
  242. {
  243. struct zd_mc_hash hash;
  244. zd_mc_clear(&hash);
  245. return zd_chip_set_multicast_hash(&mac->chip, &hash);
  246. }
  247. int zd_op_start(struct ieee80211_hw *hw)
  248. {
  249. struct zd_mac *mac = zd_hw_mac(hw);
  250. struct zd_chip *chip = &mac->chip;
  251. struct zd_usb *usb = &chip->usb;
  252. int r;
  253. if (!usb->initialized) {
  254. r = zd_usb_init_hw(usb);
  255. if (r)
  256. goto out;
  257. }
  258. r = zd_chip_enable_int(chip);
  259. if (r < 0)
  260. goto out;
  261. r = zd_chip_set_basic_rates(chip, CR_RATES_80211B | CR_RATES_80211G);
  262. if (r < 0)
  263. goto disable_int;
  264. r = set_rx_filter(mac);
  265. if (r)
  266. goto disable_int;
  267. r = set_mc_hash(mac);
  268. if (r)
  269. goto disable_int;
  270. /* Wait after setting the multicast hash table and powering on
  271. * the radio otherwise interface bring up will fail. This matches
  272. * what the vendor driver did.
  273. */
  274. msleep(10);
  275. r = zd_chip_switch_radio_on(chip);
  276. if (r < 0) {
  277. dev_err(zd_chip_dev(chip),
  278. "%s: failed to set radio on\n", __func__);
  279. goto disable_int;
  280. }
  281. r = zd_chip_enable_rxtx(chip);
  282. if (r < 0)
  283. goto disable_radio;
  284. r = zd_chip_enable_hwint(chip);
  285. if (r < 0)
  286. goto disable_rxtx;
  287. housekeeping_enable(mac);
  288. beacon_enable(mac);
  289. set_bit(ZD_DEVICE_RUNNING, &mac->flags);
  290. return 0;
  291. disable_rxtx:
  292. zd_chip_disable_rxtx(chip);
  293. disable_radio:
  294. zd_chip_switch_radio_off(chip);
  295. disable_int:
  296. zd_chip_disable_int(chip);
  297. out:
  298. return r;
  299. }
  300. void zd_op_stop(struct ieee80211_hw *hw)
  301. {
  302. struct zd_mac *mac = zd_hw_mac(hw);
  303. struct zd_chip *chip = &mac->chip;
  304. struct sk_buff *skb;
  305. struct sk_buff_head *ack_wait_queue = &mac->ack_wait_queue;
  306. clear_bit(ZD_DEVICE_RUNNING, &mac->flags);
  307. /* The order here deliberately is a little different from the open()
  308. * method, since we need to make sure there is no opportunity for RX
  309. * frames to be processed by mac80211 after we have stopped it.
  310. */
  311. zd_chip_disable_rxtx(chip);
  312. beacon_disable(mac);
  313. housekeeping_disable(mac);
  314. flush_workqueue(zd_workqueue);
  315. zd_chip_disable_hwint(chip);
  316. zd_chip_switch_radio_off(chip);
  317. zd_chip_disable_int(chip);
  318. while ((skb = skb_dequeue(ack_wait_queue)))
  319. dev_kfree_skb_any(skb);
  320. }
  321. int zd_restore_settings(struct zd_mac *mac)
  322. {
  323. struct sk_buff *beacon;
  324. struct zd_mc_hash multicast_hash;
  325. unsigned int short_preamble;
  326. int r, beacon_interval, beacon_period;
  327. u8 channel;
  328. dev_dbg_f(zd_mac_dev(mac), "\n");
  329. spin_lock_irq(&mac->lock);
  330. multicast_hash = mac->multicast_hash;
  331. short_preamble = mac->short_preamble;
  332. beacon_interval = mac->beacon.interval;
  333. beacon_period = mac->beacon.period;
  334. channel = mac->channel;
  335. spin_unlock_irq(&mac->lock);
  336. r = set_mac_and_bssid(mac);
  337. if (r < 0) {
  338. dev_dbg_f(zd_mac_dev(mac), "set_mac_and_bssid failed, %d\n", r);
  339. return r;
  340. }
  341. r = zd_chip_set_channel(&mac->chip, channel);
  342. if (r < 0) {
  343. dev_dbg_f(zd_mac_dev(mac), "zd_chip_set_channel failed, %d\n",
  344. r);
  345. return r;
  346. }
  347. set_rts_cts(mac, short_preamble);
  348. r = zd_chip_set_multicast_hash(&mac->chip, &multicast_hash);
  349. if (r < 0) {
  350. dev_dbg_f(zd_mac_dev(mac),
  351. "zd_chip_set_multicast_hash failed, %d\n", r);
  352. return r;
  353. }
  354. if (mac->type == NL80211_IFTYPE_MESH_POINT ||
  355. mac->type == NL80211_IFTYPE_ADHOC ||
  356. mac->type == NL80211_IFTYPE_AP) {
  357. if (mac->vif != NULL) {
  358. beacon = ieee80211_beacon_get(mac->hw, mac->vif);
  359. if (beacon)
  360. zd_mac_config_beacon(mac->hw, beacon, false);
  361. }
  362. zd_set_beacon_interval(&mac->chip, beacon_interval,
  363. beacon_period, mac->type);
  364. spin_lock_irq(&mac->lock);
  365. mac->beacon.last_update = jiffies;
  366. spin_unlock_irq(&mac->lock);
  367. }
  368. return 0;
  369. }
  370. /**
  371. * zd_mac_tx_status - reports tx status of a packet if required
  372. * @hw - a &struct ieee80211_hw pointer
  373. * @skb - a sk-buffer
  374. * @flags: extra flags to set in the TX status info
  375. * @ackssi: ACK signal strength
  376. * @success - True for successful transmission of the frame
  377. *
  378. * This information calls ieee80211_tx_status_irqsafe() if required by the
  379. * control information. It copies the control information into the status
  380. * information.
  381. *
  382. * If no status information has been requested, the skb is freed.
  383. */
  384. static void zd_mac_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb,
  385. int ackssi, struct tx_status *tx_status)
  386. {
  387. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  388. int i;
  389. int success = 1, retry = 1;
  390. int first_idx;
  391. const struct tx_retry_rate *retries;
  392. ieee80211_tx_info_clear_status(info);
  393. if (tx_status) {
  394. success = !tx_status->failure;
  395. retry = tx_status->retry + success;
  396. }
  397. if (success) {
  398. /* success */
  399. info->flags |= IEEE80211_TX_STAT_ACK;
  400. } else {
  401. /* failure */
  402. info->flags &= ~IEEE80211_TX_STAT_ACK;
  403. }
  404. first_idx = info->status.rates[0].idx;
  405. ZD_ASSERT(0<=first_idx && first_idx<ARRAY_SIZE(zd_retry_rates));
  406. retries = &zd_retry_rates[first_idx];
  407. ZD_ASSERT(1 <= retry && retry <= retries->count);
  408. info->status.rates[0].idx = retries->rate[0];
  409. info->status.rates[0].count = 1; // (retry > 1 ? 2 : 1);
  410. for (i=1; i<IEEE80211_TX_MAX_RATES-1 && i<retry; i++) {
  411. info->status.rates[i].idx = retries->rate[i];
  412. info->status.rates[i].count = 1; // ((i==retry-1) && success ? 1:2);
  413. }
  414. for (; i<IEEE80211_TX_MAX_RATES && i<retry; i++) {
  415. info->status.rates[i].idx = retries->rate[retry - 1];
  416. info->status.rates[i].count = 1; // (success ? 1:2);
  417. }
  418. if (i<IEEE80211_TX_MAX_RATES)
  419. info->status.rates[i].idx = -1; /* terminate */
  420. info->status.ack_signal = zd_check_signal(hw, ackssi);
  421. ieee80211_tx_status_irqsafe(hw, skb);
  422. }
  423. /**
  424. * zd_mac_tx_failed - callback for failed frames
  425. * @dev: the mac80211 wireless device
  426. *
  427. * This function is called if a frame couldn't be successfully
  428. * transferred. The first frame from the tx queue, will be selected and
  429. * reported as error to the upper layers.
  430. */
  431. void zd_mac_tx_failed(struct urb *urb)
  432. {
  433. struct ieee80211_hw * hw = zd_usb_to_hw(urb->context);
  434. struct zd_mac *mac = zd_hw_mac(hw);
  435. struct sk_buff_head *q = &mac->ack_wait_queue;
  436. struct sk_buff *skb;
  437. struct tx_status *tx_status = (struct tx_status *)urb->transfer_buffer;
  438. unsigned long flags;
  439. int success = !tx_status->failure;
  440. int retry = tx_status->retry + success;
  441. int found = 0;
  442. int i, position = 0;
  443. q = &mac->ack_wait_queue;
  444. spin_lock_irqsave(&q->lock, flags);
  445. skb_queue_walk(q, skb) {
  446. struct ieee80211_hdr *tx_hdr;
  447. struct ieee80211_tx_info *info;
  448. int first_idx, final_idx;
  449. const struct tx_retry_rate *retries;
  450. u8 final_rate;
  451. position ++;
  452. /* if the hardware reports a failure and we had a 802.11 ACK
  453. * pending, then we skip the first skb when searching for a
  454. * matching frame */
  455. if (tx_status->failure && mac->ack_pending &&
  456. skb_queue_is_first(q, skb)) {
  457. continue;
  458. }
  459. tx_hdr = (struct ieee80211_hdr *)skb->data;
  460. /* we skip all frames not matching the reported destination */
  461. if (unlikely(!ether_addr_equal(tx_hdr->addr1, tx_status->mac)))
  462. continue;
  463. /* we skip all frames not matching the reported final rate */
  464. info = IEEE80211_SKB_CB(skb);
  465. first_idx = info->status.rates[0].idx;
  466. ZD_ASSERT(0<=first_idx && first_idx<ARRAY_SIZE(zd_retry_rates));
  467. retries = &zd_retry_rates[first_idx];
  468. if (retry <= 0 || retry > retries->count)
  469. continue;
  470. final_idx = retries->rate[retry - 1];
  471. final_rate = zd_rates[final_idx].hw_value;
  472. if (final_rate != tx_status->rate) {
  473. continue;
  474. }
  475. found = 1;
  476. break;
  477. }
  478. if (found) {
  479. for (i=1; i<=position; i++) {
  480. skb = __skb_dequeue(q);
  481. zd_mac_tx_status(hw, skb,
  482. mac->ack_pending ? mac->ack_signal : 0,
  483. i == position ? tx_status : NULL);
  484. mac->ack_pending = 0;
  485. }
  486. }
  487. spin_unlock_irqrestore(&q->lock, flags);
  488. }
  489. /**
  490. * zd_mac_tx_to_dev - callback for USB layer
  491. * @skb: a &sk_buff pointer
  492. * @error: error value, 0 if transmission successful
  493. *
  494. * Informs the MAC layer that the frame has successfully transferred to the
  495. * device. If an ACK is required and the transfer to the device has been
  496. * successful, the packets are put on the @ack_wait_queue with
  497. * the control set removed.
  498. */
  499. void zd_mac_tx_to_dev(struct sk_buff *skb, int error)
  500. {
  501. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  502. struct ieee80211_hw *hw = info->rate_driver_data[0];
  503. struct zd_mac *mac = zd_hw_mac(hw);
  504. ieee80211_tx_info_clear_status(info);
  505. skb_pull(skb, sizeof(struct zd_ctrlset));
  506. if (unlikely(error ||
  507. (info->flags & IEEE80211_TX_CTL_NO_ACK))) {
  508. /*
  509. * FIXME : do we need to fill in anything ?
  510. */
  511. ieee80211_tx_status_irqsafe(hw, skb);
  512. } else {
  513. struct sk_buff_head *q = &mac->ack_wait_queue;
  514. skb_queue_tail(q, skb);
  515. while (skb_queue_len(q) > ZD_MAC_MAX_ACK_WAITERS) {
  516. zd_mac_tx_status(hw, skb_dequeue(q),
  517. mac->ack_pending ? mac->ack_signal : 0,
  518. NULL);
  519. mac->ack_pending = 0;
  520. }
  521. }
  522. }
  523. static int zd_calc_tx_length_us(u8 *service, u8 zd_rate, u16 tx_length)
  524. {
  525. /* ZD_PURE_RATE() must be used to remove the modulation type flag of
  526. * the zd-rate values.
  527. */
  528. static const u8 rate_divisor[] = {
  529. [ZD_PURE_RATE(ZD_CCK_RATE_1M)] = 1,
  530. [ZD_PURE_RATE(ZD_CCK_RATE_2M)] = 2,
  531. /* Bits must be doubled. */
  532. [ZD_PURE_RATE(ZD_CCK_RATE_5_5M)] = 11,
  533. [ZD_PURE_RATE(ZD_CCK_RATE_11M)] = 11,
  534. [ZD_PURE_RATE(ZD_OFDM_RATE_6M)] = 6,
  535. [ZD_PURE_RATE(ZD_OFDM_RATE_9M)] = 9,
  536. [ZD_PURE_RATE(ZD_OFDM_RATE_12M)] = 12,
  537. [ZD_PURE_RATE(ZD_OFDM_RATE_18M)] = 18,
  538. [ZD_PURE_RATE(ZD_OFDM_RATE_24M)] = 24,
  539. [ZD_PURE_RATE(ZD_OFDM_RATE_36M)] = 36,
  540. [ZD_PURE_RATE(ZD_OFDM_RATE_48M)] = 48,
  541. [ZD_PURE_RATE(ZD_OFDM_RATE_54M)] = 54,
  542. };
  543. u32 bits = (u32)tx_length * 8;
  544. u32 divisor;
  545. divisor = rate_divisor[ZD_PURE_RATE(zd_rate)];
  546. if (divisor == 0)
  547. return -EINVAL;
  548. switch (zd_rate) {
  549. case ZD_CCK_RATE_5_5M:
  550. bits = (2*bits) + 10; /* round up to the next integer */
  551. break;
  552. case ZD_CCK_RATE_11M:
  553. if (service) {
  554. u32 t = bits % 11;
  555. *service &= ~ZD_PLCP_SERVICE_LENGTH_EXTENSION;
  556. if (0 < t && t <= 3) {
  557. *service |= ZD_PLCP_SERVICE_LENGTH_EXTENSION;
  558. }
  559. }
  560. bits += 10; /* round up to the next integer */
  561. break;
  562. }
  563. return bits/divisor;
  564. }
  565. static void cs_set_control(struct zd_mac *mac, struct zd_ctrlset *cs,
  566. struct ieee80211_hdr *header,
  567. struct ieee80211_tx_info *info)
  568. {
  569. /*
  570. * CONTROL TODO:
  571. * - if backoff needed, enable bit 0
  572. * - if burst (backoff not needed) disable bit 0
  573. */
  574. cs->control = 0;
  575. /* First fragment */
  576. if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
  577. cs->control |= ZD_CS_NEED_RANDOM_BACKOFF;
  578. /* No ACK expected (multicast, etc.) */
  579. if (info->flags & IEEE80211_TX_CTL_NO_ACK)
  580. cs->control |= ZD_CS_NO_ACK;
  581. /* PS-POLL */
  582. if (ieee80211_is_pspoll(header->frame_control))
  583. cs->control |= ZD_CS_PS_POLL_FRAME;
  584. if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS)
  585. cs->control |= ZD_CS_RTS;
  586. if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
  587. cs->control |= ZD_CS_SELF_CTS;
  588. /* FIXME: Management frame? */
  589. }
  590. static bool zd_mac_match_cur_beacon(struct zd_mac *mac, struct sk_buff *beacon)
  591. {
  592. if (!mac->beacon.cur_beacon)
  593. return false;
  594. if (mac->beacon.cur_beacon->len != beacon->len)
  595. return false;
  596. return !memcmp(beacon->data, mac->beacon.cur_beacon->data, beacon->len);
  597. }
  598. static void zd_mac_free_cur_beacon_locked(struct zd_mac *mac)
  599. {
  600. ZD_ASSERT(mutex_is_locked(&mac->chip.mutex));
  601. kfree_skb(mac->beacon.cur_beacon);
  602. mac->beacon.cur_beacon = NULL;
  603. }
  604. static void zd_mac_free_cur_beacon(struct zd_mac *mac)
  605. {
  606. mutex_lock(&mac->chip.mutex);
  607. zd_mac_free_cur_beacon_locked(mac);
  608. mutex_unlock(&mac->chip.mutex);
  609. }
  610. static int zd_mac_config_beacon(struct ieee80211_hw *hw, struct sk_buff *beacon,
  611. bool in_intr)
  612. {
  613. struct zd_mac *mac = zd_hw_mac(hw);
  614. int r, ret, num_cmds, req_pos = 0;
  615. u32 tmp, j = 0;
  616. /* 4 more bytes for tail CRC */
  617. u32 full_len = beacon->len + 4;
  618. unsigned long end_jiffies, message_jiffies;
  619. struct zd_ioreq32 *ioreqs;
  620. mutex_lock(&mac->chip.mutex);
  621. /* Check if hw already has this beacon. */
  622. if (zd_mac_match_cur_beacon(mac, beacon)) {
  623. r = 0;
  624. goto out_nofree;
  625. }
  626. /* Alloc memory for full beacon write at once. */
  627. num_cmds = 1 + zd_chip_is_zd1211b(&mac->chip) + full_len;
  628. ioreqs = kmalloc(num_cmds * sizeof(struct zd_ioreq32), GFP_KERNEL);
  629. if (!ioreqs) {
  630. r = -ENOMEM;
  631. goto out_nofree;
  632. }
  633. r = zd_iowrite32_locked(&mac->chip, 0, CR_BCN_FIFO_SEMAPHORE);
  634. if (r < 0)
  635. goto out;
  636. r = zd_ioread32_locked(&mac->chip, &tmp, CR_BCN_FIFO_SEMAPHORE);
  637. if (r < 0)
  638. goto release_sema;
  639. if (in_intr && tmp & 0x2) {
  640. r = -EBUSY;
  641. goto release_sema;
  642. }
  643. end_jiffies = jiffies + HZ / 2; /*~500ms*/
  644. message_jiffies = jiffies + HZ / 10; /*~100ms*/
  645. while (tmp & 0x2) {
  646. r = zd_ioread32_locked(&mac->chip, &tmp, CR_BCN_FIFO_SEMAPHORE);
  647. if (r < 0)
  648. goto release_sema;
  649. if (time_is_before_eq_jiffies(message_jiffies)) {
  650. message_jiffies = jiffies + HZ / 10;
  651. dev_err(zd_mac_dev(mac),
  652. "CR_BCN_FIFO_SEMAPHORE not ready\n");
  653. if (time_is_before_eq_jiffies(end_jiffies)) {
  654. dev_err(zd_mac_dev(mac),
  655. "Giving up beacon config.\n");
  656. r = -ETIMEDOUT;
  657. goto reset_device;
  658. }
  659. }
  660. msleep(20);
  661. }
  662. ioreqs[req_pos].addr = CR_BCN_FIFO;
  663. ioreqs[req_pos].value = full_len - 1;
  664. req_pos++;
  665. if (zd_chip_is_zd1211b(&mac->chip)) {
  666. ioreqs[req_pos].addr = CR_BCN_LENGTH;
  667. ioreqs[req_pos].value = full_len - 1;
  668. req_pos++;
  669. }
  670. for (j = 0 ; j < beacon->len; j++) {
  671. ioreqs[req_pos].addr = CR_BCN_FIFO;
  672. ioreqs[req_pos].value = *((u8 *)(beacon->data + j));
  673. req_pos++;
  674. }
  675. for (j = 0; j < 4; j++) {
  676. ioreqs[req_pos].addr = CR_BCN_FIFO;
  677. ioreqs[req_pos].value = 0x0;
  678. req_pos++;
  679. }
  680. BUG_ON(req_pos != num_cmds);
  681. r = zd_iowrite32a_locked(&mac->chip, ioreqs, num_cmds);
  682. release_sema:
  683. /*
  684. * Try very hard to release device beacon semaphore, as otherwise
  685. * device/driver can be left in unusable state.
  686. */
  687. end_jiffies = jiffies + HZ / 2; /*~500ms*/
  688. ret = zd_iowrite32_locked(&mac->chip, 1, CR_BCN_FIFO_SEMAPHORE);
  689. while (ret < 0) {
  690. if (in_intr || time_is_before_eq_jiffies(end_jiffies)) {
  691. ret = -ETIMEDOUT;
  692. break;
  693. }
  694. msleep(20);
  695. ret = zd_iowrite32_locked(&mac->chip, 1, CR_BCN_FIFO_SEMAPHORE);
  696. }
  697. if (ret < 0)
  698. dev_err(zd_mac_dev(mac), "Could not release "
  699. "CR_BCN_FIFO_SEMAPHORE!\n");
  700. if (r < 0 || ret < 0) {
  701. if (r >= 0)
  702. r = ret;
  703. /* We don't know if beacon was written successfully or not,
  704. * so clear current. */
  705. zd_mac_free_cur_beacon_locked(mac);
  706. goto out;
  707. }
  708. /* Beacon has now been written successfully, update current. */
  709. zd_mac_free_cur_beacon_locked(mac);
  710. mac->beacon.cur_beacon = beacon;
  711. beacon = NULL;
  712. /* 802.11b/g 2.4G CCK 1Mb
  713. * 802.11a, not yet implemented, uses different values (see GPL vendor
  714. * driver)
  715. */
  716. r = zd_iowrite32_locked(&mac->chip, 0x00000400 | (full_len << 19),
  717. CR_BCN_PLCP_CFG);
  718. out:
  719. kfree(ioreqs);
  720. out_nofree:
  721. kfree_skb(beacon);
  722. mutex_unlock(&mac->chip.mutex);
  723. return r;
  724. reset_device:
  725. zd_mac_free_cur_beacon_locked(mac);
  726. kfree_skb(beacon);
  727. mutex_unlock(&mac->chip.mutex);
  728. kfree(ioreqs);
  729. /* semaphore stuck, reset device to avoid fw freeze later */
  730. dev_warn(zd_mac_dev(mac), "CR_BCN_FIFO_SEMAPHORE stuck, "
  731. "resetting device...");
  732. usb_queue_reset_device(mac->chip.usb.intf);
  733. return r;
  734. }
  735. static int fill_ctrlset(struct zd_mac *mac,
  736. struct sk_buff *skb)
  737. {
  738. int r;
  739. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  740. unsigned int frag_len = skb->len + FCS_LEN;
  741. unsigned int packet_length;
  742. struct ieee80211_rate *txrate;
  743. struct zd_ctrlset *cs = (struct zd_ctrlset *)
  744. skb_push(skb, sizeof(struct zd_ctrlset));
  745. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  746. ZD_ASSERT(frag_len <= 0xffff);
  747. /*
  748. * Firmware computes the duration itself (for all frames except PSPoll)
  749. * and needs the field set to 0 at input, otherwise firmware messes up
  750. * duration_id and sets bits 14 and 15 on.
  751. */
  752. if (!ieee80211_is_pspoll(hdr->frame_control))
  753. hdr->duration_id = 0;
  754. txrate = ieee80211_get_tx_rate(mac->hw, info);
  755. cs->modulation = txrate->hw_value;
  756. if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
  757. cs->modulation = txrate->hw_value_short;
  758. cs->tx_length = cpu_to_le16(frag_len);
  759. cs_set_control(mac, cs, hdr, info);
  760. packet_length = frag_len + sizeof(struct zd_ctrlset) + 10;
  761. ZD_ASSERT(packet_length <= 0xffff);
  762. /* ZD1211B: Computing the length difference this way, gives us
  763. * flexibility to compute the packet length.
  764. */
  765. cs->packet_length = cpu_to_le16(zd_chip_is_zd1211b(&mac->chip) ?
  766. packet_length - frag_len : packet_length);
  767. /*
  768. * CURRENT LENGTH:
  769. * - transmit frame length in microseconds
  770. * - seems to be derived from frame length
  771. * - see Cal_Us_Service() in zdinlinef.h
  772. * - if macp->bTxBurstEnable is enabled, then multiply by 4
  773. * - bTxBurstEnable is never set in the vendor driver
  774. *
  775. * SERVICE:
  776. * - "for PLCP configuration"
  777. * - always 0 except in some situations at 802.11b 11M
  778. * - see line 53 of zdinlinef.h
  779. */
  780. cs->service = 0;
  781. r = zd_calc_tx_length_us(&cs->service, ZD_RATE(cs->modulation),
  782. le16_to_cpu(cs->tx_length));
  783. if (r < 0)
  784. return r;
  785. cs->current_length = cpu_to_le16(r);
  786. cs->next_frame_length = 0;
  787. return 0;
  788. }
  789. /**
  790. * zd_op_tx - transmits a network frame to the device
  791. *
  792. * @dev: mac80211 hardware device
  793. * @skb: socket buffer
  794. * @control: the control structure
  795. *
  796. * This function transmit an IEEE 802.11 network frame to the device. The
  797. * control block of the skbuff will be initialized. If necessary the incoming
  798. * mac80211 queues will be stopped.
  799. */
  800. static void zd_op_tx(struct ieee80211_hw *hw,
  801. struct ieee80211_tx_control *control,
  802. struct sk_buff *skb)
  803. {
  804. struct zd_mac *mac = zd_hw_mac(hw);
  805. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  806. int r;
  807. r = fill_ctrlset(mac, skb);
  808. if (r)
  809. goto fail;
  810. info->rate_driver_data[0] = hw;
  811. r = zd_usb_tx(&mac->chip.usb, skb);
  812. if (r)
  813. goto fail;
  814. return;
  815. fail:
  816. dev_kfree_skb(skb);
  817. }
  818. /**
  819. * filter_ack - filters incoming packets for acknowledgements
  820. * @dev: the mac80211 device
  821. * @rx_hdr: received header
  822. * @stats: the status for the received packet
  823. *
  824. * This functions looks for ACK packets and tries to match them with the
  825. * frames in the tx queue. If a match is found the frame will be dequeued and
  826. * the upper layers is informed about the successful transmission. If
  827. * mac80211 queues have been stopped and the number of frames still to be
  828. * transmitted is low the queues will be opened again.
  829. *
  830. * Returns 1 if the frame was an ACK, 0 if it was ignored.
  831. */
  832. static int filter_ack(struct ieee80211_hw *hw, struct ieee80211_hdr *rx_hdr,
  833. struct ieee80211_rx_status *stats)
  834. {
  835. struct zd_mac *mac = zd_hw_mac(hw);
  836. struct sk_buff *skb;
  837. struct sk_buff_head *q;
  838. unsigned long flags;
  839. int found = 0;
  840. int i, position = 0;
  841. if (!ieee80211_is_ack(rx_hdr->frame_control))
  842. return 0;
  843. q = &mac->ack_wait_queue;
  844. spin_lock_irqsave(&q->lock, flags);
  845. skb_queue_walk(q, skb) {
  846. struct ieee80211_hdr *tx_hdr;
  847. position ++;
  848. if (mac->ack_pending && skb_queue_is_first(q, skb))
  849. continue;
  850. tx_hdr = (struct ieee80211_hdr *)skb->data;
  851. if (likely(ether_addr_equal(tx_hdr->addr2, rx_hdr->addr1)))
  852. {
  853. found = 1;
  854. break;
  855. }
  856. }
  857. if (found) {
  858. for (i=1; i<position; i++) {
  859. skb = __skb_dequeue(q);
  860. zd_mac_tx_status(hw, skb,
  861. mac->ack_pending ? mac->ack_signal : 0,
  862. NULL);
  863. mac->ack_pending = 0;
  864. }
  865. mac->ack_pending = 1;
  866. mac->ack_signal = stats->signal;
  867. /* Prevent pending tx-packet on AP-mode */
  868. if (mac->type == NL80211_IFTYPE_AP) {
  869. skb = __skb_dequeue(q);
  870. zd_mac_tx_status(hw, skb, mac->ack_signal, NULL);
  871. mac->ack_pending = 0;
  872. }
  873. }
  874. spin_unlock_irqrestore(&q->lock, flags);
  875. return 1;
  876. }
  877. int zd_mac_rx(struct ieee80211_hw *hw, const u8 *buffer, unsigned int length)
  878. {
  879. struct zd_mac *mac = zd_hw_mac(hw);
  880. struct ieee80211_rx_status stats;
  881. const struct rx_status *status;
  882. struct sk_buff *skb;
  883. int bad_frame = 0;
  884. __le16 fc;
  885. int need_padding;
  886. int i;
  887. u8 rate;
  888. if (length < ZD_PLCP_HEADER_SIZE + 10 /* IEEE80211_1ADDR_LEN */ +
  889. FCS_LEN + sizeof(struct rx_status))
  890. return -EINVAL;
  891. memset(&stats, 0, sizeof(stats));
  892. /* Note about pass_failed_fcs and pass_ctrl access below:
  893. * mac locking intentionally omitted here, as this is the only unlocked
  894. * reader and the only writer is configure_filter. Plus, if there were
  895. * any races accessing these variables, it wouldn't really matter.
  896. * If mac80211 ever provides a way for us to access filter flags
  897. * from outside configure_filter, we could improve on this. Also, this
  898. * situation may change once we implement some kind of DMA-into-skb
  899. * RX path. */
  900. /* Caller has to ensure that length >= sizeof(struct rx_status). */
  901. status = (struct rx_status *)
  902. (buffer + (length - sizeof(struct rx_status)));
  903. if (status->frame_status & ZD_RX_ERROR) {
  904. if (mac->pass_failed_fcs &&
  905. (status->frame_status & ZD_RX_CRC32_ERROR)) {
  906. stats.flag |= RX_FLAG_FAILED_FCS_CRC;
  907. bad_frame = 1;
  908. } else {
  909. return -EINVAL;
  910. }
  911. }
  912. stats.freq = zd_channels[_zd_chip_get_channel(&mac->chip) - 1].center_freq;
  913. stats.band = NL80211_BAND_2GHZ;
  914. stats.signal = zd_check_signal(hw, status->signal_strength);
  915. rate = zd_rx_rate(buffer, status);
  916. /* todo: return index in the big switches in zd_rx_rate instead */
  917. for (i = 0; i < mac->band.n_bitrates; i++)
  918. if (rate == mac->band.bitrates[i].hw_value)
  919. stats.rate_idx = i;
  920. length -= ZD_PLCP_HEADER_SIZE + sizeof(struct rx_status);
  921. buffer += ZD_PLCP_HEADER_SIZE;
  922. /* Except for bad frames, filter each frame to see if it is an ACK, in
  923. * which case our internal TX tracking is updated. Normally we then
  924. * bail here as there's no need to pass ACKs on up to the stack, but
  925. * there is also the case where the stack has requested us to pass
  926. * control frames on up (pass_ctrl) which we must consider. */
  927. if (!bad_frame &&
  928. filter_ack(hw, (struct ieee80211_hdr *)buffer, &stats)
  929. && !mac->pass_ctrl)
  930. return 0;
  931. fc = get_unaligned((__le16*)buffer);
  932. need_padding = ieee80211_is_data_qos(fc) ^ ieee80211_has_a4(fc);
  933. skb = dev_alloc_skb(length + (need_padding ? 2 : 0));
  934. if (skb == NULL)
  935. return -ENOMEM;
  936. if (need_padding) {
  937. /* Make sure the payload data is 4 byte aligned. */
  938. skb_reserve(skb, 2);
  939. }
  940. /* FIXME : could we avoid this big memcpy ? */
  941. memcpy(skb_put(skb, length), buffer, length);
  942. memcpy(IEEE80211_SKB_RXCB(skb), &stats, sizeof(stats));
  943. ieee80211_rx_irqsafe(hw, skb);
  944. return 0;
  945. }
  946. static int zd_op_add_interface(struct ieee80211_hw *hw,
  947. struct ieee80211_vif *vif)
  948. {
  949. struct zd_mac *mac = zd_hw_mac(hw);
  950. /* using NL80211_IFTYPE_UNSPECIFIED to indicate no mode selected */
  951. if (mac->type != NL80211_IFTYPE_UNSPECIFIED)
  952. return -EOPNOTSUPP;
  953. switch (vif->type) {
  954. case NL80211_IFTYPE_MONITOR:
  955. case NL80211_IFTYPE_MESH_POINT:
  956. case NL80211_IFTYPE_STATION:
  957. case NL80211_IFTYPE_ADHOC:
  958. case NL80211_IFTYPE_AP:
  959. mac->type = vif->type;
  960. break;
  961. default:
  962. return -EOPNOTSUPP;
  963. }
  964. mac->vif = vif;
  965. return set_mac_and_bssid(mac);
  966. }
  967. static void zd_op_remove_interface(struct ieee80211_hw *hw,
  968. struct ieee80211_vif *vif)
  969. {
  970. struct zd_mac *mac = zd_hw_mac(hw);
  971. mac->type = NL80211_IFTYPE_UNSPECIFIED;
  972. mac->vif = NULL;
  973. zd_set_beacon_interval(&mac->chip, 0, 0, NL80211_IFTYPE_UNSPECIFIED);
  974. zd_write_mac_addr(&mac->chip, NULL);
  975. zd_mac_free_cur_beacon(mac);
  976. }
  977. static int zd_op_config(struct ieee80211_hw *hw, u32 changed)
  978. {
  979. struct zd_mac *mac = zd_hw_mac(hw);
  980. struct ieee80211_conf *conf = &hw->conf;
  981. spin_lock_irq(&mac->lock);
  982. mac->channel = conf->chandef.chan->hw_value;
  983. spin_unlock_irq(&mac->lock);
  984. return zd_chip_set_channel(&mac->chip, conf->chandef.chan->hw_value);
  985. }
  986. static void zd_beacon_done(struct zd_mac *mac)
  987. {
  988. struct sk_buff *skb, *beacon;
  989. if (!test_bit(ZD_DEVICE_RUNNING, &mac->flags))
  990. return;
  991. if (!mac->vif || mac->vif->type != NL80211_IFTYPE_AP)
  992. return;
  993. /*
  994. * Send out buffered broad- and multicast frames.
  995. */
  996. while (!ieee80211_queue_stopped(mac->hw, 0)) {
  997. skb = ieee80211_get_buffered_bc(mac->hw, mac->vif);
  998. if (!skb)
  999. break;
  1000. zd_op_tx(mac->hw, NULL, skb);
  1001. }
  1002. /*
  1003. * Fetch next beacon so that tim_count is updated.
  1004. */
  1005. beacon = ieee80211_beacon_get(mac->hw, mac->vif);
  1006. if (beacon)
  1007. zd_mac_config_beacon(mac->hw, beacon, true);
  1008. spin_lock_irq(&mac->lock);
  1009. mac->beacon.last_update = jiffies;
  1010. spin_unlock_irq(&mac->lock);
  1011. }
  1012. static void zd_process_intr(struct work_struct *work)
  1013. {
  1014. u16 int_status;
  1015. unsigned long flags;
  1016. struct zd_mac *mac = container_of(work, struct zd_mac, process_intr);
  1017. spin_lock_irqsave(&mac->lock, flags);
  1018. int_status = le16_to_cpu(*(__le16 *)(mac->intr_buffer + 4));
  1019. spin_unlock_irqrestore(&mac->lock, flags);
  1020. if (int_status & INT_CFG_NEXT_BCN) {
  1021. /*dev_dbg_f_limit(zd_mac_dev(mac), "INT_CFG_NEXT_BCN\n");*/
  1022. zd_beacon_done(mac);
  1023. } else {
  1024. dev_dbg_f(zd_mac_dev(mac), "Unsupported interrupt\n");
  1025. }
  1026. zd_chip_enable_hwint(&mac->chip);
  1027. }
  1028. static u64 zd_op_prepare_multicast(struct ieee80211_hw *hw,
  1029. struct netdev_hw_addr_list *mc_list)
  1030. {
  1031. struct zd_mac *mac = zd_hw_mac(hw);
  1032. struct zd_mc_hash hash;
  1033. struct netdev_hw_addr *ha;
  1034. zd_mc_clear(&hash);
  1035. netdev_hw_addr_list_for_each(ha, mc_list) {
  1036. dev_dbg_f(zd_mac_dev(mac), "mc addr %pM\n", ha->addr);
  1037. zd_mc_add_addr(&hash, ha->addr);
  1038. }
  1039. return hash.low | ((u64)hash.high << 32);
  1040. }
  1041. #define SUPPORTED_FIF_FLAGS \
  1042. (FIF_ALLMULTI | FIF_FCSFAIL | FIF_CONTROL | \
  1043. FIF_OTHER_BSS | FIF_BCN_PRBRESP_PROMISC)
  1044. static void zd_op_configure_filter(struct ieee80211_hw *hw,
  1045. unsigned int changed_flags,
  1046. unsigned int *new_flags,
  1047. u64 multicast)
  1048. {
  1049. struct zd_mc_hash hash = {
  1050. .low = multicast,
  1051. .high = multicast >> 32,
  1052. };
  1053. struct zd_mac *mac = zd_hw_mac(hw);
  1054. unsigned long flags;
  1055. int r;
  1056. /* Only deal with supported flags */
  1057. changed_flags &= SUPPORTED_FIF_FLAGS;
  1058. *new_flags &= SUPPORTED_FIF_FLAGS;
  1059. /*
  1060. * If multicast parameter (as returned by zd_op_prepare_multicast)
  1061. * has changed, no bit in changed_flags is set. To handle this
  1062. * situation, we do not return if changed_flags is 0. If we do so,
  1063. * we will have some issue with IPv6 which uses multicast for link
  1064. * layer address resolution.
  1065. */
  1066. if (*new_flags & FIF_ALLMULTI)
  1067. zd_mc_add_all(&hash);
  1068. spin_lock_irqsave(&mac->lock, flags);
  1069. mac->pass_failed_fcs = !!(*new_flags & FIF_FCSFAIL);
  1070. mac->pass_ctrl = !!(*new_flags & FIF_CONTROL);
  1071. mac->multicast_hash = hash;
  1072. spin_unlock_irqrestore(&mac->lock, flags);
  1073. zd_chip_set_multicast_hash(&mac->chip, &hash);
  1074. if (changed_flags & FIF_CONTROL) {
  1075. r = set_rx_filter(mac);
  1076. if (r)
  1077. dev_err(zd_mac_dev(mac), "set_rx_filter error %d\n", r);
  1078. }
  1079. /* no handling required for FIF_OTHER_BSS as we don't currently
  1080. * do BSSID filtering */
  1081. /* FIXME: in future it would be nice to enable the probe response
  1082. * filter (so that the driver doesn't see them) until
  1083. * FIF_BCN_PRBRESP_PROMISC is set. however due to atomicity here, we'd
  1084. * have to schedule work to enable prbresp reception, which might
  1085. * happen too late. For now we'll just listen and forward them all the
  1086. * time. */
  1087. }
  1088. static void set_rts_cts(struct zd_mac *mac, unsigned int short_preamble)
  1089. {
  1090. mutex_lock(&mac->chip.mutex);
  1091. zd_chip_set_rts_cts_rate_locked(&mac->chip, short_preamble);
  1092. mutex_unlock(&mac->chip.mutex);
  1093. }
  1094. static void zd_op_bss_info_changed(struct ieee80211_hw *hw,
  1095. struct ieee80211_vif *vif,
  1096. struct ieee80211_bss_conf *bss_conf,
  1097. u32 changes)
  1098. {
  1099. struct zd_mac *mac = zd_hw_mac(hw);
  1100. int associated;
  1101. dev_dbg_f(zd_mac_dev(mac), "changes: %x\n", changes);
  1102. if (mac->type == NL80211_IFTYPE_MESH_POINT ||
  1103. mac->type == NL80211_IFTYPE_ADHOC ||
  1104. mac->type == NL80211_IFTYPE_AP) {
  1105. associated = true;
  1106. if (changes & BSS_CHANGED_BEACON) {
  1107. struct sk_buff *beacon = ieee80211_beacon_get(hw, vif);
  1108. if (beacon) {
  1109. zd_chip_disable_hwint(&mac->chip);
  1110. zd_mac_config_beacon(hw, beacon, false);
  1111. zd_chip_enable_hwint(&mac->chip);
  1112. }
  1113. }
  1114. if (changes & BSS_CHANGED_BEACON_ENABLED) {
  1115. u16 interval = 0;
  1116. u8 period = 0;
  1117. if (bss_conf->enable_beacon) {
  1118. period = bss_conf->dtim_period;
  1119. interval = bss_conf->beacon_int;
  1120. }
  1121. spin_lock_irq(&mac->lock);
  1122. mac->beacon.period = period;
  1123. mac->beacon.interval = interval;
  1124. mac->beacon.last_update = jiffies;
  1125. spin_unlock_irq(&mac->lock);
  1126. zd_set_beacon_interval(&mac->chip, interval, period,
  1127. mac->type);
  1128. }
  1129. } else
  1130. associated = is_valid_ether_addr(bss_conf->bssid);
  1131. spin_lock_irq(&mac->lock);
  1132. mac->associated = associated;
  1133. spin_unlock_irq(&mac->lock);
  1134. /* TODO: do hardware bssid filtering */
  1135. if (changes & BSS_CHANGED_ERP_PREAMBLE) {
  1136. spin_lock_irq(&mac->lock);
  1137. mac->short_preamble = bss_conf->use_short_preamble;
  1138. spin_unlock_irq(&mac->lock);
  1139. set_rts_cts(mac, bss_conf->use_short_preamble);
  1140. }
  1141. }
  1142. static u64 zd_op_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  1143. {
  1144. struct zd_mac *mac = zd_hw_mac(hw);
  1145. return zd_chip_get_tsf(&mac->chip);
  1146. }
  1147. static const struct ieee80211_ops zd_ops = {
  1148. .tx = zd_op_tx,
  1149. .start = zd_op_start,
  1150. .stop = zd_op_stop,
  1151. .add_interface = zd_op_add_interface,
  1152. .remove_interface = zd_op_remove_interface,
  1153. .config = zd_op_config,
  1154. .prepare_multicast = zd_op_prepare_multicast,
  1155. .configure_filter = zd_op_configure_filter,
  1156. .bss_info_changed = zd_op_bss_info_changed,
  1157. .get_tsf = zd_op_get_tsf,
  1158. };
  1159. struct ieee80211_hw *zd_mac_alloc_hw(struct usb_interface *intf)
  1160. {
  1161. struct zd_mac *mac;
  1162. struct ieee80211_hw *hw;
  1163. hw = ieee80211_alloc_hw(sizeof(struct zd_mac), &zd_ops);
  1164. if (!hw) {
  1165. dev_dbg_f(&intf->dev, "out of memory\n");
  1166. return NULL;
  1167. }
  1168. mac = zd_hw_mac(hw);
  1169. memset(mac, 0, sizeof(*mac));
  1170. spin_lock_init(&mac->lock);
  1171. mac->hw = hw;
  1172. mac->type = NL80211_IFTYPE_UNSPECIFIED;
  1173. memcpy(mac->channels, zd_channels, sizeof(zd_channels));
  1174. memcpy(mac->rates, zd_rates, sizeof(zd_rates));
  1175. mac->band.n_bitrates = ARRAY_SIZE(zd_rates);
  1176. mac->band.bitrates = mac->rates;
  1177. mac->band.n_channels = ARRAY_SIZE(zd_channels);
  1178. mac->band.channels = mac->channels;
  1179. hw->wiphy->bands[NL80211_BAND_2GHZ] = &mac->band;
  1180. ieee80211_hw_set(hw, MFP_CAPABLE);
  1181. ieee80211_hw_set(hw, HOST_BROADCAST_PS_BUFFERING);
  1182. ieee80211_hw_set(hw, RX_INCLUDES_FCS);
  1183. ieee80211_hw_set(hw, SIGNAL_UNSPEC);
  1184. hw->wiphy->interface_modes =
  1185. BIT(NL80211_IFTYPE_MESH_POINT) |
  1186. BIT(NL80211_IFTYPE_STATION) |
  1187. BIT(NL80211_IFTYPE_ADHOC) |
  1188. BIT(NL80211_IFTYPE_AP);
  1189. wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
  1190. hw->max_signal = 100;
  1191. hw->queues = 1;
  1192. hw->extra_tx_headroom = sizeof(struct zd_ctrlset);
  1193. /*
  1194. * Tell mac80211 that we support multi rate retries
  1195. */
  1196. hw->max_rates = IEEE80211_TX_MAX_RATES;
  1197. hw->max_rate_tries = 18; /* 9 rates * 2 retries/rate */
  1198. skb_queue_head_init(&mac->ack_wait_queue);
  1199. mac->ack_pending = 0;
  1200. zd_chip_init(&mac->chip, hw, intf);
  1201. housekeeping_init(mac);
  1202. beacon_init(mac);
  1203. INIT_WORK(&mac->process_intr, zd_process_intr);
  1204. SET_IEEE80211_DEV(hw, &intf->dev);
  1205. return hw;
  1206. }
  1207. #define BEACON_WATCHDOG_DELAY round_jiffies_relative(HZ)
  1208. static void beacon_watchdog_handler(struct work_struct *work)
  1209. {
  1210. struct zd_mac *mac =
  1211. container_of(work, struct zd_mac, beacon.watchdog_work.work);
  1212. struct sk_buff *beacon;
  1213. unsigned long timeout;
  1214. int interval, period;
  1215. if (!test_bit(ZD_DEVICE_RUNNING, &mac->flags))
  1216. goto rearm;
  1217. if (mac->type != NL80211_IFTYPE_AP || !mac->vif)
  1218. goto rearm;
  1219. spin_lock_irq(&mac->lock);
  1220. interval = mac->beacon.interval;
  1221. period = mac->beacon.period;
  1222. timeout = mac->beacon.last_update +
  1223. msecs_to_jiffies(interval * 1024 / 1000) * 3;
  1224. spin_unlock_irq(&mac->lock);
  1225. if (interval > 0 && time_is_before_jiffies(timeout)) {
  1226. dev_dbg_f(zd_mac_dev(mac), "beacon interrupt stalled, "
  1227. "restarting. "
  1228. "(interval: %d, dtim: %d)\n",
  1229. interval, period);
  1230. zd_chip_disable_hwint(&mac->chip);
  1231. beacon = ieee80211_beacon_get(mac->hw, mac->vif);
  1232. if (beacon) {
  1233. zd_mac_free_cur_beacon(mac);
  1234. zd_mac_config_beacon(mac->hw, beacon, false);
  1235. }
  1236. zd_set_beacon_interval(&mac->chip, interval, period, mac->type);
  1237. zd_chip_enable_hwint(&mac->chip);
  1238. spin_lock_irq(&mac->lock);
  1239. mac->beacon.last_update = jiffies;
  1240. spin_unlock_irq(&mac->lock);
  1241. }
  1242. rearm:
  1243. queue_delayed_work(zd_workqueue, &mac->beacon.watchdog_work,
  1244. BEACON_WATCHDOG_DELAY);
  1245. }
  1246. static void beacon_init(struct zd_mac *mac)
  1247. {
  1248. INIT_DELAYED_WORK(&mac->beacon.watchdog_work, beacon_watchdog_handler);
  1249. }
  1250. static void beacon_enable(struct zd_mac *mac)
  1251. {
  1252. dev_dbg_f(zd_mac_dev(mac), "\n");
  1253. mac->beacon.last_update = jiffies;
  1254. queue_delayed_work(zd_workqueue, &mac->beacon.watchdog_work,
  1255. BEACON_WATCHDOG_DELAY);
  1256. }
  1257. static void beacon_disable(struct zd_mac *mac)
  1258. {
  1259. dev_dbg_f(zd_mac_dev(mac), "\n");
  1260. cancel_delayed_work_sync(&mac->beacon.watchdog_work);
  1261. zd_mac_free_cur_beacon(mac);
  1262. }
  1263. #define LINK_LED_WORK_DELAY HZ
  1264. static void link_led_handler(struct work_struct *work)
  1265. {
  1266. struct zd_mac *mac =
  1267. container_of(work, struct zd_mac, housekeeping.link_led_work.work);
  1268. struct zd_chip *chip = &mac->chip;
  1269. int is_associated;
  1270. int r;
  1271. if (!test_bit(ZD_DEVICE_RUNNING, &mac->flags))
  1272. goto requeue;
  1273. spin_lock_irq(&mac->lock);
  1274. is_associated = mac->associated;
  1275. spin_unlock_irq(&mac->lock);
  1276. r = zd_chip_control_leds(chip,
  1277. is_associated ? ZD_LED_ASSOCIATED : ZD_LED_SCANNING);
  1278. if (r)
  1279. dev_dbg_f(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r);
  1280. requeue:
  1281. queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
  1282. LINK_LED_WORK_DELAY);
  1283. }
  1284. static void housekeeping_init(struct zd_mac *mac)
  1285. {
  1286. INIT_DELAYED_WORK(&mac->housekeeping.link_led_work, link_led_handler);
  1287. }
  1288. static void housekeeping_enable(struct zd_mac *mac)
  1289. {
  1290. dev_dbg_f(zd_mac_dev(mac), "\n");
  1291. queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
  1292. 0);
  1293. }
  1294. static void housekeeping_disable(struct zd_mac *mac)
  1295. {
  1296. dev_dbg_f(zd_mac_dev(mac), "\n");
  1297. cancel_delayed_work_sync(&mac->housekeeping.link_led_work);
  1298. zd_chip_control_leds(&mac->chip, ZD_LED_OFF);
  1299. }