zd_mac.c 32 KB

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