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