zd_mac.c 38 KB

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