zd_mac.c 32 KB

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