zd_mac.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550
  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, bool in_intr);
  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. static int zd_check_signal(struct ieee80211_hw *hw, int signal)
  153. {
  154. struct zd_mac *mac = zd_hw_mac(hw);
  155. dev_dbg_f_cond(zd_mac_dev(mac), signal < 0 || signal > 100,
  156. "%s: signal value from device not in range 0..100, "
  157. "but %d.\n", __func__, signal);
  158. if (signal < 0)
  159. signal = 0;
  160. else if (signal > 100)
  161. signal = 100;
  162. return signal;
  163. }
  164. int zd_mac_preinit_hw(struct ieee80211_hw *hw)
  165. {
  166. int r;
  167. u8 addr[ETH_ALEN];
  168. struct zd_mac *mac = zd_hw_mac(hw);
  169. r = zd_chip_read_mac_addr_fw(&mac->chip, addr);
  170. if (r)
  171. return r;
  172. SET_IEEE80211_PERM_ADDR(hw, addr);
  173. return 0;
  174. }
  175. int zd_mac_init_hw(struct ieee80211_hw *hw)
  176. {
  177. int r;
  178. struct zd_mac *mac = zd_hw_mac(hw);
  179. struct zd_chip *chip = &mac->chip;
  180. char alpha2[2];
  181. u8 default_regdomain;
  182. r = zd_chip_enable_int(chip);
  183. if (r)
  184. goto out;
  185. r = zd_chip_init_hw(chip);
  186. if (r)
  187. goto disable_int;
  188. ZD_ASSERT(!irqs_disabled());
  189. r = zd_read_regdomain(chip, &default_regdomain);
  190. if (r)
  191. goto disable_int;
  192. spin_lock_irq(&mac->lock);
  193. mac->regdomain = mac->default_regdomain = default_regdomain;
  194. spin_unlock_irq(&mac->lock);
  195. /* We must inform the device that we are doing encryption/decryption in
  196. * software at the moment. */
  197. r = zd_set_encryption_type(chip, ENC_SNIFFER);
  198. if (r)
  199. goto disable_int;
  200. r = zd_reg2alpha2(mac->regdomain, alpha2);
  201. if (r)
  202. goto disable_int;
  203. r = regulatory_hint(hw->wiphy, alpha2);
  204. disable_int:
  205. zd_chip_disable_int(chip);
  206. out:
  207. return r;
  208. }
  209. void zd_mac_clear(struct zd_mac *mac)
  210. {
  211. flush_workqueue(zd_workqueue);
  212. zd_chip_clear(&mac->chip);
  213. ZD_ASSERT(!spin_is_locked(&mac->lock));
  214. ZD_MEMCLEAR(mac, sizeof(struct zd_mac));
  215. }
  216. static int set_rx_filter(struct zd_mac *mac)
  217. {
  218. unsigned long flags;
  219. u32 filter = STA_RX_FILTER;
  220. spin_lock_irqsave(&mac->lock, flags);
  221. if (mac->pass_ctrl)
  222. filter |= RX_FILTER_CTRL;
  223. spin_unlock_irqrestore(&mac->lock, flags);
  224. return zd_iowrite32(&mac->chip, CR_RX_FILTER, filter);
  225. }
  226. static int set_mac_and_bssid(struct zd_mac *mac)
  227. {
  228. int r;
  229. if (!mac->vif)
  230. return -1;
  231. r = zd_write_mac_addr(&mac->chip, mac->vif->addr);
  232. if (r)
  233. return r;
  234. /* Vendor driver after setting MAC either sets BSSID for AP or
  235. * filter for other modes.
  236. */
  237. if (mac->type != NL80211_IFTYPE_AP)
  238. return set_rx_filter(mac);
  239. else
  240. return zd_write_bssid(&mac->chip, mac->vif->addr);
  241. }
  242. static int set_mc_hash(struct zd_mac *mac)
  243. {
  244. struct zd_mc_hash hash;
  245. zd_mc_clear(&hash);
  246. return zd_chip_set_multicast_hash(&mac->chip, &hash);
  247. }
  248. int zd_op_start(struct ieee80211_hw *hw)
  249. {
  250. struct zd_mac *mac = zd_hw_mac(hw);
  251. struct zd_chip *chip = &mac->chip;
  252. struct zd_usb *usb = &chip->usb;
  253. int r;
  254. if (!usb->initialized) {
  255. r = zd_usb_init_hw(usb);
  256. if (r)
  257. goto out;
  258. }
  259. r = zd_chip_enable_int(chip);
  260. if (r < 0)
  261. goto out;
  262. r = zd_chip_set_basic_rates(chip, CR_RATES_80211B | CR_RATES_80211G);
  263. if (r < 0)
  264. goto disable_int;
  265. r = set_rx_filter(mac);
  266. if (r)
  267. goto disable_int;
  268. r = set_mc_hash(mac);
  269. if (r)
  270. goto disable_int;
  271. /* Wait after setting the multicast hash table and powering on
  272. * the radio otherwise interface bring up will fail. This matches
  273. * what the vendor driver did.
  274. */
  275. msleep(10);
  276. r = zd_chip_switch_radio_on(chip);
  277. if (r < 0) {
  278. dev_err(zd_chip_dev(chip),
  279. "%s: failed to set radio on\n", __func__);
  280. goto disable_int;
  281. }
  282. r = zd_chip_enable_rxtx(chip);
  283. if (r < 0)
  284. goto disable_radio;
  285. r = zd_chip_enable_hwint(chip);
  286. if (r < 0)
  287. goto disable_rxtx;
  288. housekeeping_enable(mac);
  289. beacon_enable(mac);
  290. set_bit(ZD_DEVICE_RUNNING, &mac->flags);
  291. return 0;
  292. disable_rxtx:
  293. zd_chip_disable_rxtx(chip);
  294. disable_radio:
  295. zd_chip_switch_radio_off(chip);
  296. disable_int:
  297. zd_chip_disable_int(chip);
  298. out:
  299. return r;
  300. }
  301. void zd_op_stop(struct ieee80211_hw *hw)
  302. {
  303. struct zd_mac *mac = zd_hw_mac(hw);
  304. struct zd_chip *chip = &mac->chip;
  305. struct sk_buff *skb;
  306. struct sk_buff_head *ack_wait_queue = &mac->ack_wait_queue;
  307. clear_bit(ZD_DEVICE_RUNNING, &mac->flags);
  308. /* The order here deliberately is a little different from the open()
  309. * method, since we need to make sure there is no opportunity for RX
  310. * frames to be processed by mac80211 after we have stopped it.
  311. */
  312. zd_chip_disable_rxtx(chip);
  313. beacon_disable(mac);
  314. housekeeping_disable(mac);
  315. flush_workqueue(zd_workqueue);
  316. zd_chip_disable_hwint(chip);
  317. zd_chip_switch_radio_off(chip);
  318. zd_chip_disable_int(chip);
  319. while ((skb = skb_dequeue(ack_wait_queue)))
  320. dev_kfree_skb_any(skb);
  321. }
  322. int zd_restore_settings(struct zd_mac *mac)
  323. {
  324. struct sk_buff *beacon;
  325. struct zd_mc_hash multicast_hash;
  326. unsigned int short_preamble;
  327. int r, beacon_interval, beacon_period;
  328. u8 channel;
  329. dev_dbg_f(zd_mac_dev(mac), "\n");
  330. spin_lock_irq(&mac->lock);
  331. multicast_hash = mac->multicast_hash;
  332. short_preamble = mac->short_preamble;
  333. beacon_interval = mac->beacon.interval;
  334. beacon_period = mac->beacon.period;
  335. channel = mac->channel;
  336. spin_unlock_irq(&mac->lock);
  337. r = set_mac_and_bssid(mac);
  338. if (r < 0) {
  339. dev_dbg_f(zd_mac_dev(mac), "set_mac_and_bssid failed, %d\n", r);
  340. return r;
  341. }
  342. r = zd_chip_set_channel(&mac->chip, channel);
  343. if (r < 0) {
  344. dev_dbg_f(zd_mac_dev(mac), "zd_chip_set_channel failed, %d\n",
  345. r);
  346. return r;
  347. }
  348. set_rts_cts(mac, short_preamble);
  349. r = zd_chip_set_multicast_hash(&mac->chip, &multicast_hash);
  350. if (r < 0) {
  351. dev_dbg_f(zd_mac_dev(mac),
  352. "zd_chip_set_multicast_hash failed, %d\n", r);
  353. return r;
  354. }
  355. if (mac->type == NL80211_IFTYPE_MESH_POINT ||
  356. mac->type == NL80211_IFTYPE_ADHOC ||
  357. mac->type == NL80211_IFTYPE_AP) {
  358. if (mac->vif != NULL) {
  359. beacon = ieee80211_beacon_get(mac->hw, mac->vif);
  360. if (beacon)
  361. zd_mac_config_beacon(mac->hw, beacon, false);
  362. }
  363. zd_set_beacon_interval(&mac->chip, beacon_interval,
  364. beacon_period, mac->type);
  365. spin_lock_irq(&mac->lock);
  366. mac->beacon.last_update = jiffies;
  367. spin_unlock_irq(&mac->lock);
  368. }
  369. return 0;
  370. }
  371. /**
  372. * zd_mac_tx_status - reports tx status of a packet if required
  373. * @hw - a &struct ieee80211_hw pointer
  374. * @skb - a sk-buffer
  375. * @flags: extra flags to set in the TX status info
  376. * @ackssi: ACK signal strength
  377. * @success - True for successful transmission of the frame
  378. *
  379. * This information calls ieee80211_tx_status_irqsafe() if required by the
  380. * control information. It copies the control information into the status
  381. * information.
  382. *
  383. * If no status information has been requested, the skb is freed.
  384. */
  385. static void zd_mac_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb,
  386. int ackssi, struct tx_status *tx_status)
  387. {
  388. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  389. int i;
  390. int success = 1, retry = 1;
  391. int first_idx;
  392. const struct tx_retry_rate *retries;
  393. ieee80211_tx_info_clear_status(info);
  394. if (tx_status) {
  395. success = !tx_status->failure;
  396. retry = tx_status->retry + success;
  397. }
  398. if (success) {
  399. /* success */
  400. info->flags |= IEEE80211_TX_STAT_ACK;
  401. } else {
  402. /* failure */
  403. info->flags &= ~IEEE80211_TX_STAT_ACK;
  404. }
  405. first_idx = info->status.rates[0].idx;
  406. ZD_ASSERT(0<=first_idx && first_idx<ARRAY_SIZE(zd_retry_rates));
  407. retries = &zd_retry_rates[first_idx];
  408. ZD_ASSERT(1 <= retry && retry <= retries->count);
  409. info->status.rates[0].idx = retries->rate[0];
  410. info->status.rates[0].count = 1; // (retry > 1 ? 2 : 1);
  411. for (i=1; i<IEEE80211_TX_MAX_RATES-1 && i<retry; i++) {
  412. info->status.rates[i].idx = retries->rate[i];
  413. info->status.rates[i].count = 1; // ((i==retry-1) && success ? 1:2);
  414. }
  415. for (; i<IEEE80211_TX_MAX_RATES && i<retry; i++) {
  416. info->status.rates[i].idx = retries->rate[retry - 1];
  417. info->status.rates[i].count = 1; // (success ? 1:2);
  418. }
  419. if (i<IEEE80211_TX_MAX_RATES)
  420. info->status.rates[i].idx = -1; /* terminate */
  421. info->status.ack_signal = zd_check_signal(hw, ackssi);
  422. ieee80211_tx_status_irqsafe(hw, skb);
  423. }
  424. /**
  425. * zd_mac_tx_failed - callback for failed frames
  426. * @dev: the mac80211 wireless device
  427. *
  428. * This function is called if a frame couldn't be successfully
  429. * transferred. The first frame from the tx queue, will be selected and
  430. * reported as error to the upper layers.
  431. */
  432. void zd_mac_tx_failed(struct urb *urb)
  433. {
  434. struct ieee80211_hw * hw = zd_usb_to_hw(urb->context);
  435. struct zd_mac *mac = zd_hw_mac(hw);
  436. struct sk_buff_head *q = &mac->ack_wait_queue;
  437. struct sk_buff *skb;
  438. struct tx_status *tx_status = (struct tx_status *)urb->transfer_buffer;
  439. unsigned long flags;
  440. int success = !tx_status->failure;
  441. int retry = tx_status->retry + success;
  442. int found = 0;
  443. int i, position = 0;
  444. q = &mac->ack_wait_queue;
  445. spin_lock_irqsave(&q->lock, flags);
  446. skb_queue_walk(q, skb) {
  447. struct ieee80211_hdr *tx_hdr;
  448. struct ieee80211_tx_info *info;
  449. int first_idx, final_idx;
  450. const struct tx_retry_rate *retries;
  451. u8 final_rate;
  452. position ++;
  453. /* if the hardware reports a failure and we had a 802.11 ACK
  454. * pending, then we skip the first skb when searching for a
  455. * matching frame */
  456. if (tx_status->failure && mac->ack_pending &&
  457. skb_queue_is_first(q, skb)) {
  458. continue;
  459. }
  460. tx_hdr = (struct ieee80211_hdr *)skb->data;
  461. /* we skip all frames not matching the reported destination */
  462. if (unlikely(memcmp(tx_hdr->addr1, tx_status->mac, ETH_ALEN))) {
  463. continue;
  464. }
  465. /* we skip all frames not matching the reported final rate */
  466. info = IEEE80211_SKB_CB(skb);
  467. first_idx = info->status.rates[0].idx;
  468. ZD_ASSERT(0<=first_idx && first_idx<ARRAY_SIZE(zd_retry_rates));
  469. retries = &zd_retry_rates[first_idx];
  470. if (retry <= 0 || retry > retries->count)
  471. continue;
  472. final_idx = retries->rate[retry - 1];
  473. final_rate = zd_rates[final_idx].hw_value;
  474. if (final_rate != tx_status->rate) {
  475. continue;
  476. }
  477. found = 1;
  478. break;
  479. }
  480. if (found) {
  481. for (i=1; i<=position; i++) {
  482. skb = __skb_dequeue(q);
  483. zd_mac_tx_status(hw, skb,
  484. mac->ack_pending ? mac->ack_signal : 0,
  485. i == position ? tx_status : NULL);
  486. mac->ack_pending = 0;
  487. }
  488. }
  489. spin_unlock_irqrestore(&q->lock, flags);
  490. }
  491. /**
  492. * zd_mac_tx_to_dev - callback for USB layer
  493. * @skb: a &sk_buff pointer
  494. * @error: error value, 0 if transmission successful
  495. *
  496. * Informs the MAC layer that the frame has successfully transferred to the
  497. * device. If an ACK is required and the transfer to the device has been
  498. * successful, the packets are put on the @ack_wait_queue with
  499. * the control set removed.
  500. */
  501. void zd_mac_tx_to_dev(struct sk_buff *skb, int error)
  502. {
  503. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  504. struct ieee80211_hw *hw = info->rate_driver_data[0];
  505. struct zd_mac *mac = zd_hw_mac(hw);
  506. ieee80211_tx_info_clear_status(info);
  507. skb_pull(skb, sizeof(struct zd_ctrlset));
  508. if (unlikely(error ||
  509. (info->flags & IEEE80211_TX_CTL_NO_ACK))) {
  510. /*
  511. * FIXME : do we need to fill in anything ?
  512. */
  513. ieee80211_tx_status_irqsafe(hw, skb);
  514. } else {
  515. struct sk_buff_head *q = &mac->ack_wait_queue;
  516. skb_queue_tail(q, skb);
  517. while (skb_queue_len(q) > ZD_MAC_MAX_ACK_WAITERS) {
  518. zd_mac_tx_status(hw, skb_dequeue(q),
  519. mac->ack_pending ? mac->ack_signal : 0,
  520. NULL);
  521. mac->ack_pending = 0;
  522. }
  523. }
  524. }
  525. static int zd_calc_tx_length_us(u8 *service, u8 zd_rate, u16 tx_length)
  526. {
  527. /* ZD_PURE_RATE() must be used to remove the modulation type flag of
  528. * the zd-rate values.
  529. */
  530. static const u8 rate_divisor[] = {
  531. [ZD_PURE_RATE(ZD_CCK_RATE_1M)] = 1,
  532. [ZD_PURE_RATE(ZD_CCK_RATE_2M)] = 2,
  533. /* Bits must be doubled. */
  534. [ZD_PURE_RATE(ZD_CCK_RATE_5_5M)] = 11,
  535. [ZD_PURE_RATE(ZD_CCK_RATE_11M)] = 11,
  536. [ZD_PURE_RATE(ZD_OFDM_RATE_6M)] = 6,
  537. [ZD_PURE_RATE(ZD_OFDM_RATE_9M)] = 9,
  538. [ZD_PURE_RATE(ZD_OFDM_RATE_12M)] = 12,
  539. [ZD_PURE_RATE(ZD_OFDM_RATE_18M)] = 18,
  540. [ZD_PURE_RATE(ZD_OFDM_RATE_24M)] = 24,
  541. [ZD_PURE_RATE(ZD_OFDM_RATE_36M)] = 36,
  542. [ZD_PURE_RATE(ZD_OFDM_RATE_48M)] = 48,
  543. [ZD_PURE_RATE(ZD_OFDM_RATE_54M)] = 54,
  544. };
  545. u32 bits = (u32)tx_length * 8;
  546. u32 divisor;
  547. divisor = rate_divisor[ZD_PURE_RATE(zd_rate)];
  548. if (divisor == 0)
  549. return -EINVAL;
  550. switch (zd_rate) {
  551. case ZD_CCK_RATE_5_5M:
  552. bits = (2*bits) + 10; /* round up to the next integer */
  553. break;
  554. case ZD_CCK_RATE_11M:
  555. if (service) {
  556. u32 t = bits % 11;
  557. *service &= ~ZD_PLCP_SERVICE_LENGTH_EXTENSION;
  558. if (0 < t && t <= 3) {
  559. *service |= ZD_PLCP_SERVICE_LENGTH_EXTENSION;
  560. }
  561. }
  562. bits += 10; /* round up to the next integer */
  563. break;
  564. }
  565. return bits/divisor;
  566. }
  567. static void cs_set_control(struct zd_mac *mac, struct zd_ctrlset *cs,
  568. struct ieee80211_hdr *header,
  569. struct ieee80211_tx_info *info)
  570. {
  571. /*
  572. * CONTROL TODO:
  573. * - if backoff needed, enable bit 0
  574. * - if burst (backoff not needed) disable bit 0
  575. */
  576. cs->control = 0;
  577. /* First fragment */
  578. if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
  579. cs->control |= ZD_CS_NEED_RANDOM_BACKOFF;
  580. /* No ACK expected (multicast, etc.) */
  581. if (info->flags & IEEE80211_TX_CTL_NO_ACK)
  582. cs->control |= ZD_CS_NO_ACK;
  583. /* PS-POLL */
  584. if (ieee80211_is_pspoll(header->frame_control))
  585. cs->control |= ZD_CS_PS_POLL_FRAME;
  586. if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS)
  587. cs->control |= ZD_CS_RTS;
  588. if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
  589. cs->control |= ZD_CS_SELF_CTS;
  590. /* FIXME: Management frame? */
  591. }
  592. static bool zd_mac_match_cur_beacon(struct zd_mac *mac, struct sk_buff *beacon)
  593. {
  594. if (!mac->beacon.cur_beacon)
  595. return false;
  596. if (mac->beacon.cur_beacon->len != beacon->len)
  597. return false;
  598. return !memcmp(beacon->data, mac->beacon.cur_beacon->data, beacon->len);
  599. }
  600. static void zd_mac_free_cur_beacon_locked(struct zd_mac *mac)
  601. {
  602. ZD_ASSERT(mutex_is_locked(&mac->chip.mutex));
  603. kfree_skb(mac->beacon.cur_beacon);
  604. mac->beacon.cur_beacon = NULL;
  605. }
  606. static void zd_mac_free_cur_beacon(struct zd_mac *mac)
  607. {
  608. mutex_lock(&mac->chip.mutex);
  609. zd_mac_free_cur_beacon_locked(mac);
  610. mutex_unlock(&mac->chip.mutex);
  611. }
  612. static int zd_mac_config_beacon(struct ieee80211_hw *hw, struct sk_buff *beacon,
  613. bool in_intr)
  614. {
  615. struct zd_mac *mac = zd_hw_mac(hw);
  616. int r, ret, num_cmds, req_pos = 0;
  617. u32 tmp, j = 0;
  618. /* 4 more bytes for tail CRC */
  619. u32 full_len = beacon->len + 4;
  620. unsigned long end_jiffies, message_jiffies;
  621. struct zd_ioreq32 *ioreqs;
  622. mutex_lock(&mac->chip.mutex);
  623. /* Check if hw already has this beacon. */
  624. if (zd_mac_match_cur_beacon(mac, beacon)) {
  625. r = 0;
  626. goto out_nofree;
  627. }
  628. /* Alloc memory for full beacon write at once. */
  629. num_cmds = 1 + zd_chip_is_zd1211b(&mac->chip) + full_len;
  630. ioreqs = kmalloc(num_cmds * sizeof(struct zd_ioreq32), GFP_KERNEL);
  631. if (!ioreqs) {
  632. r = -ENOMEM;
  633. goto out_nofree;
  634. }
  635. r = zd_iowrite32_locked(&mac->chip, 0, CR_BCN_FIFO_SEMAPHORE);
  636. if (r < 0)
  637. goto out;
  638. r = zd_ioread32_locked(&mac->chip, &tmp, CR_BCN_FIFO_SEMAPHORE);
  639. if (r < 0)
  640. goto release_sema;
  641. if (in_intr && tmp & 0x2) {
  642. r = -EBUSY;
  643. goto release_sema;
  644. }
  645. end_jiffies = jiffies + HZ / 2; /*~500ms*/
  646. message_jiffies = jiffies + HZ / 10; /*~100ms*/
  647. while (tmp & 0x2) {
  648. r = zd_ioread32_locked(&mac->chip, &tmp, CR_BCN_FIFO_SEMAPHORE);
  649. if (r < 0)
  650. goto release_sema;
  651. if (time_is_before_eq_jiffies(message_jiffies)) {
  652. message_jiffies = jiffies + HZ / 10;
  653. dev_err(zd_mac_dev(mac),
  654. "CR_BCN_FIFO_SEMAPHORE not ready\n");
  655. if (time_is_before_eq_jiffies(end_jiffies)) {
  656. dev_err(zd_mac_dev(mac),
  657. "Giving up beacon config.\n");
  658. r = -ETIMEDOUT;
  659. goto reset_device;
  660. }
  661. }
  662. msleep(20);
  663. }
  664. ioreqs[req_pos].addr = CR_BCN_FIFO;
  665. ioreqs[req_pos].value = full_len - 1;
  666. req_pos++;
  667. if (zd_chip_is_zd1211b(&mac->chip)) {
  668. ioreqs[req_pos].addr = CR_BCN_LENGTH;
  669. ioreqs[req_pos].value = full_len - 1;
  670. req_pos++;
  671. }
  672. for (j = 0 ; j < beacon->len; j++) {
  673. ioreqs[req_pos].addr = CR_BCN_FIFO;
  674. ioreqs[req_pos].value = *((u8 *)(beacon->data + j));
  675. req_pos++;
  676. }
  677. for (j = 0; j < 4; j++) {
  678. ioreqs[req_pos].addr = CR_BCN_FIFO;
  679. ioreqs[req_pos].value = 0x0;
  680. req_pos++;
  681. }
  682. BUG_ON(req_pos != num_cmds);
  683. r = zd_iowrite32a_locked(&mac->chip, ioreqs, num_cmds);
  684. release_sema:
  685. /*
  686. * Try very hard to release device beacon semaphore, as otherwise
  687. * device/driver can be left in unusable state.
  688. */
  689. end_jiffies = jiffies + HZ / 2; /*~500ms*/
  690. ret = zd_iowrite32_locked(&mac->chip, 1, CR_BCN_FIFO_SEMAPHORE);
  691. while (ret < 0) {
  692. if (in_intr || time_is_before_eq_jiffies(end_jiffies)) {
  693. ret = -ETIMEDOUT;
  694. break;
  695. }
  696. msleep(20);
  697. ret = zd_iowrite32_locked(&mac->chip, 1, CR_BCN_FIFO_SEMAPHORE);
  698. }
  699. if (ret < 0)
  700. dev_err(zd_mac_dev(mac), "Could not release "
  701. "CR_BCN_FIFO_SEMAPHORE!\n");
  702. if (r < 0 || ret < 0) {
  703. if (r >= 0)
  704. r = ret;
  705. /* We don't know if beacon was written successfully or not,
  706. * so clear current. */
  707. zd_mac_free_cur_beacon_locked(mac);
  708. goto out;
  709. }
  710. /* Beacon has now been written successfully, update current. */
  711. zd_mac_free_cur_beacon_locked(mac);
  712. mac->beacon.cur_beacon = beacon;
  713. beacon = NULL;
  714. /* 802.11b/g 2.4G CCK 1Mb
  715. * 802.11a, not yet implemented, uses different values (see GPL vendor
  716. * driver)
  717. */
  718. r = zd_iowrite32_locked(&mac->chip, 0x00000400 | (full_len << 19),
  719. CR_BCN_PLCP_CFG);
  720. out:
  721. kfree(ioreqs);
  722. out_nofree:
  723. kfree_skb(beacon);
  724. mutex_unlock(&mac->chip.mutex);
  725. return r;
  726. reset_device:
  727. zd_mac_free_cur_beacon_locked(mac);
  728. kfree_skb(beacon);
  729. mutex_unlock(&mac->chip.mutex);
  730. kfree(ioreqs);
  731. /* semaphore stuck, reset device to avoid fw freeze later */
  732. dev_warn(zd_mac_dev(mac), "CR_BCN_FIFO_SEMAPHORE stuck, "
  733. "resetting device...");
  734. usb_queue_reset_device(mac->chip.usb.intf);
  735. return r;
  736. }
  737. static int fill_ctrlset(struct zd_mac *mac,
  738. struct sk_buff *skb)
  739. {
  740. int r;
  741. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  742. unsigned int frag_len = skb->len + FCS_LEN;
  743. unsigned int packet_length;
  744. struct ieee80211_rate *txrate;
  745. struct zd_ctrlset *cs = (struct zd_ctrlset *)
  746. skb_push(skb, sizeof(struct zd_ctrlset));
  747. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  748. ZD_ASSERT(frag_len <= 0xffff);
  749. /*
  750. * Firmware computes the duration itself (for all frames except PSPoll)
  751. * and needs the field set to 0 at input, otherwise firmware messes up
  752. * duration_id and sets bits 14 and 15 on.
  753. */
  754. if (!ieee80211_is_pspoll(hdr->frame_control))
  755. hdr->duration_id = 0;
  756. txrate = ieee80211_get_tx_rate(mac->hw, info);
  757. cs->modulation = txrate->hw_value;
  758. if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
  759. cs->modulation = txrate->hw_value_short;
  760. cs->tx_length = cpu_to_le16(frag_len);
  761. cs_set_control(mac, cs, hdr, info);
  762. packet_length = frag_len + sizeof(struct zd_ctrlset) + 10;
  763. ZD_ASSERT(packet_length <= 0xffff);
  764. /* ZD1211B: Computing the length difference this way, gives us
  765. * flexibility to compute the packet length.
  766. */
  767. cs->packet_length = cpu_to_le16(zd_chip_is_zd1211b(&mac->chip) ?
  768. packet_length - frag_len : packet_length);
  769. /*
  770. * CURRENT LENGTH:
  771. * - transmit frame length in microseconds
  772. * - seems to be derived from frame length
  773. * - see Cal_Us_Service() in zdinlinef.h
  774. * - if macp->bTxBurstEnable is enabled, then multiply by 4
  775. * - bTxBurstEnable is never set in the vendor driver
  776. *
  777. * SERVICE:
  778. * - "for PLCP configuration"
  779. * - always 0 except in some situations at 802.11b 11M
  780. * - see line 53 of zdinlinef.h
  781. */
  782. cs->service = 0;
  783. r = zd_calc_tx_length_us(&cs->service, ZD_RATE(cs->modulation),
  784. le16_to_cpu(cs->tx_length));
  785. if (r < 0)
  786. return r;
  787. cs->current_length = cpu_to_le16(r);
  788. cs->next_frame_length = 0;
  789. return 0;
  790. }
  791. /**
  792. * zd_op_tx - transmits a network frame to the device
  793. *
  794. * @dev: mac80211 hardware device
  795. * @skb: socket buffer
  796. * @control: the control structure
  797. *
  798. * This function transmit an IEEE 802.11 network frame to the device. The
  799. * control block of the skbuff will be initialized. If necessary the incoming
  800. * mac80211 queues will be stopped.
  801. */
  802. static void zd_op_tx(struct ieee80211_hw *hw, 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(!memcmp(tx_hdr->addr2, rx_hdr->addr1, ETH_ALEN)))
  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 = IEEE80211_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->channel->hw_value;
  983. spin_unlock_irq(&mac->lock);
  984. return zd_chip_set_channel(&mac->chip, conf->channel->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, 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_PROMISC_IN_BSS | 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_PROMISC_IN_BSS | 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[IEEE80211_BAND_2GHZ] = &mac->band;
  1180. hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
  1181. IEEE80211_HW_SIGNAL_UNSPEC |
  1182. IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING;
  1183. hw->wiphy->interface_modes =
  1184. BIT(NL80211_IFTYPE_MESH_POINT) |
  1185. BIT(NL80211_IFTYPE_STATION) |
  1186. BIT(NL80211_IFTYPE_ADHOC) |
  1187. BIT(NL80211_IFTYPE_AP);
  1188. hw->max_signal = 100;
  1189. hw->queues = 1;
  1190. hw->extra_tx_headroom = sizeof(struct zd_ctrlset);
  1191. /*
  1192. * Tell mac80211 that we support multi rate retries
  1193. */
  1194. hw->max_rates = IEEE80211_TX_MAX_RATES;
  1195. hw->max_rate_tries = 18; /* 9 rates * 2 retries/rate */
  1196. skb_queue_head_init(&mac->ack_wait_queue);
  1197. mac->ack_pending = 0;
  1198. zd_chip_init(&mac->chip, hw, intf);
  1199. housekeeping_init(mac);
  1200. beacon_init(mac);
  1201. INIT_WORK(&mac->process_intr, zd_process_intr);
  1202. SET_IEEE80211_DEV(hw, &intf->dev);
  1203. return hw;
  1204. }
  1205. #define BEACON_WATCHDOG_DELAY round_jiffies_relative(HZ)
  1206. static void beacon_watchdog_handler(struct work_struct *work)
  1207. {
  1208. struct zd_mac *mac =
  1209. container_of(work, struct zd_mac, beacon.watchdog_work.work);
  1210. struct sk_buff *beacon;
  1211. unsigned long timeout;
  1212. int interval, period;
  1213. if (!test_bit(ZD_DEVICE_RUNNING, &mac->flags))
  1214. goto rearm;
  1215. if (mac->type != NL80211_IFTYPE_AP || !mac->vif)
  1216. goto rearm;
  1217. spin_lock_irq(&mac->lock);
  1218. interval = mac->beacon.interval;
  1219. period = mac->beacon.period;
  1220. timeout = mac->beacon.last_update +
  1221. msecs_to_jiffies(interval * 1024 / 1000) * 3;
  1222. spin_unlock_irq(&mac->lock);
  1223. if (interval > 0 && time_is_before_jiffies(timeout)) {
  1224. dev_dbg_f(zd_mac_dev(mac), "beacon interrupt stalled, "
  1225. "restarting. "
  1226. "(interval: %d, dtim: %d)\n",
  1227. interval, period);
  1228. zd_chip_disable_hwint(&mac->chip);
  1229. beacon = ieee80211_beacon_get(mac->hw, mac->vif);
  1230. if (beacon) {
  1231. zd_mac_free_cur_beacon(mac);
  1232. zd_mac_config_beacon(mac->hw, beacon, false);
  1233. }
  1234. zd_set_beacon_interval(&mac->chip, interval, period, mac->type);
  1235. zd_chip_enable_hwint(&mac->chip);
  1236. spin_lock_irq(&mac->lock);
  1237. mac->beacon.last_update = jiffies;
  1238. spin_unlock_irq(&mac->lock);
  1239. }
  1240. rearm:
  1241. queue_delayed_work(zd_workqueue, &mac->beacon.watchdog_work,
  1242. BEACON_WATCHDOG_DELAY);
  1243. }
  1244. static void beacon_init(struct zd_mac *mac)
  1245. {
  1246. INIT_DELAYED_WORK(&mac->beacon.watchdog_work, beacon_watchdog_handler);
  1247. }
  1248. static void beacon_enable(struct zd_mac *mac)
  1249. {
  1250. dev_dbg_f(zd_mac_dev(mac), "\n");
  1251. mac->beacon.last_update = jiffies;
  1252. queue_delayed_work(zd_workqueue, &mac->beacon.watchdog_work,
  1253. BEACON_WATCHDOG_DELAY);
  1254. }
  1255. static void beacon_disable(struct zd_mac *mac)
  1256. {
  1257. dev_dbg_f(zd_mac_dev(mac), "\n");
  1258. cancel_delayed_work_sync(&mac->beacon.watchdog_work);
  1259. zd_mac_free_cur_beacon(mac);
  1260. }
  1261. #define LINK_LED_WORK_DELAY HZ
  1262. static void link_led_handler(struct work_struct *work)
  1263. {
  1264. struct zd_mac *mac =
  1265. container_of(work, struct zd_mac, housekeeping.link_led_work.work);
  1266. struct zd_chip *chip = &mac->chip;
  1267. int is_associated;
  1268. int r;
  1269. if (!test_bit(ZD_DEVICE_RUNNING, &mac->flags))
  1270. goto requeue;
  1271. spin_lock_irq(&mac->lock);
  1272. is_associated = mac->associated;
  1273. spin_unlock_irq(&mac->lock);
  1274. r = zd_chip_control_leds(chip,
  1275. is_associated ? ZD_LED_ASSOCIATED : ZD_LED_SCANNING);
  1276. if (r)
  1277. dev_dbg_f(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r);
  1278. requeue:
  1279. queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
  1280. LINK_LED_WORK_DELAY);
  1281. }
  1282. static void housekeeping_init(struct zd_mac *mac)
  1283. {
  1284. INIT_DELAYED_WORK(&mac->housekeeping.link_led_work, link_led_handler);
  1285. }
  1286. static void housekeeping_enable(struct zd_mac *mac)
  1287. {
  1288. dev_dbg_f(zd_mac_dev(mac), "\n");
  1289. queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
  1290. 0);
  1291. }
  1292. static void housekeeping_disable(struct zd_mac *mac)
  1293. {
  1294. dev_dbg_f(zd_mac_dev(mac), "\n");
  1295. cancel_delayed_work_sync(&mac->housekeeping.link_led_work);
  1296. zd_chip_control_leds(&mac->chip, ZD_LED_OFF);
  1297. }