phy_common.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. /*
  2. Broadcom B43 wireless driver
  3. Common PHY routines
  4. Copyright (c) 2005 Martin Langer <martin-langer@gmx.de>,
  5. Copyright (c) 2005-2007 Stefano Brivio <stefano.brivio@polimi.it>
  6. Copyright (c) 2005-2008 Michael Buesch <m@bues.ch>
  7. Copyright (c) 2005, 2006 Danny van Dyk <kugelfang@gentoo.org>
  8. Copyright (c) 2005, 2006 Andreas Jaggi <andreas.jaggi@waterwave.ch>
  9. This program is free software; you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation; either version 2 of the License, or
  12. (at your option) any later version.
  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. You should have received a copy of the GNU General Public License
  18. along with this program; see the file COPYING. If not, write to
  19. the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
  20. Boston, MA 02110-1301, USA.
  21. */
  22. #include "phy_common.h"
  23. #include "phy_g.h"
  24. #include "phy_a.h"
  25. #include "phy_n.h"
  26. #include "phy_lp.h"
  27. #include "phy_ht.h"
  28. #include "phy_lcn.h"
  29. #include "b43.h"
  30. #include "main.h"
  31. int b43_phy_allocate(struct b43_wldev *dev)
  32. {
  33. struct b43_phy *phy = &(dev->phy);
  34. int err;
  35. phy->ops = NULL;
  36. switch (phy->type) {
  37. case B43_PHYTYPE_A:
  38. phy->ops = &b43_phyops_a;
  39. break;
  40. case B43_PHYTYPE_G:
  41. phy->ops = &b43_phyops_g;
  42. break;
  43. case B43_PHYTYPE_N:
  44. #ifdef CONFIG_B43_PHY_N
  45. phy->ops = &b43_phyops_n;
  46. #endif
  47. break;
  48. case B43_PHYTYPE_LP:
  49. #ifdef CONFIG_B43_PHY_LP
  50. phy->ops = &b43_phyops_lp;
  51. #endif
  52. break;
  53. case B43_PHYTYPE_HT:
  54. #ifdef CONFIG_B43_PHY_HT
  55. phy->ops = &b43_phyops_ht;
  56. #endif
  57. break;
  58. case B43_PHYTYPE_LCN:
  59. #ifdef CONFIG_B43_PHY_LCN
  60. phy->ops = &b43_phyops_lcn;
  61. #endif
  62. break;
  63. }
  64. if (B43_WARN_ON(!phy->ops))
  65. return -ENODEV;
  66. err = phy->ops->allocate(dev);
  67. if (err)
  68. phy->ops = NULL;
  69. return err;
  70. }
  71. void b43_phy_free(struct b43_wldev *dev)
  72. {
  73. dev->phy.ops->free(dev);
  74. dev->phy.ops = NULL;
  75. }
  76. int b43_phy_init(struct b43_wldev *dev)
  77. {
  78. struct b43_phy *phy = &dev->phy;
  79. const struct b43_phy_operations *ops = phy->ops;
  80. int err;
  81. phy->channel = ops->get_default_chan(dev);
  82. phy->ops->switch_analog(dev, true);
  83. b43_software_rfkill(dev, false);
  84. err = ops->init(dev);
  85. if (err) {
  86. b43err(dev->wl, "PHY init failed\n");
  87. goto err_block_rf;
  88. }
  89. phy->do_full_init = false;
  90. /* Make sure to switch hardware and firmware (SHM) to
  91. * the default channel. */
  92. err = b43_switch_channel(dev, ops->get_default_chan(dev));
  93. if (err) {
  94. b43err(dev->wl, "PHY init: Channel switch to default failed\n");
  95. goto err_phy_exit;
  96. }
  97. return 0;
  98. err_phy_exit:
  99. phy->do_full_init = true;
  100. if (ops->exit)
  101. ops->exit(dev);
  102. err_block_rf:
  103. b43_software_rfkill(dev, true);
  104. return err;
  105. }
  106. void b43_phy_exit(struct b43_wldev *dev)
  107. {
  108. const struct b43_phy_operations *ops = dev->phy.ops;
  109. b43_software_rfkill(dev, true);
  110. dev->phy.do_full_init = true;
  111. if (ops->exit)
  112. ops->exit(dev);
  113. }
  114. bool b43_has_hardware_pctl(struct b43_wldev *dev)
  115. {
  116. if (!dev->phy.hardware_power_control)
  117. return false;
  118. if (!dev->phy.ops->supports_hwpctl)
  119. return false;
  120. return dev->phy.ops->supports_hwpctl(dev);
  121. }
  122. void b43_radio_lock(struct b43_wldev *dev)
  123. {
  124. u32 macctl;
  125. #if B43_DEBUG
  126. B43_WARN_ON(dev->phy.radio_locked);
  127. dev->phy.radio_locked = true;
  128. #endif
  129. macctl = b43_read32(dev, B43_MMIO_MACCTL);
  130. macctl |= B43_MACCTL_RADIOLOCK;
  131. b43_write32(dev, B43_MMIO_MACCTL, macctl);
  132. /* Commit the write and wait for the firmware
  133. * to finish any radio register access. */
  134. b43_read32(dev, B43_MMIO_MACCTL);
  135. udelay(10);
  136. }
  137. void b43_radio_unlock(struct b43_wldev *dev)
  138. {
  139. u32 macctl;
  140. #if B43_DEBUG
  141. B43_WARN_ON(!dev->phy.radio_locked);
  142. dev->phy.radio_locked = false;
  143. #endif
  144. /* Commit any write */
  145. b43_read16(dev, B43_MMIO_PHY_VER);
  146. /* unlock */
  147. macctl = b43_read32(dev, B43_MMIO_MACCTL);
  148. macctl &= ~B43_MACCTL_RADIOLOCK;
  149. b43_write32(dev, B43_MMIO_MACCTL, macctl);
  150. }
  151. void b43_phy_lock(struct b43_wldev *dev)
  152. {
  153. #if B43_DEBUG
  154. B43_WARN_ON(dev->phy.phy_locked);
  155. dev->phy.phy_locked = true;
  156. #endif
  157. B43_WARN_ON(dev->dev->core_rev < 3);
  158. if (!b43_is_mode(dev->wl, NL80211_IFTYPE_AP))
  159. b43_power_saving_ctl_bits(dev, B43_PS_AWAKE);
  160. }
  161. void b43_phy_unlock(struct b43_wldev *dev)
  162. {
  163. #if B43_DEBUG
  164. B43_WARN_ON(!dev->phy.phy_locked);
  165. dev->phy.phy_locked = false;
  166. #endif
  167. B43_WARN_ON(dev->dev->core_rev < 3);
  168. if (!b43_is_mode(dev->wl, NL80211_IFTYPE_AP))
  169. b43_power_saving_ctl_bits(dev, 0);
  170. }
  171. static inline void assert_mac_suspended(struct b43_wldev *dev)
  172. {
  173. if (!B43_DEBUG)
  174. return;
  175. if ((b43_status(dev) >= B43_STAT_INITIALIZED) &&
  176. (dev->mac_suspended <= 0)) {
  177. b43dbg(dev->wl, "PHY/RADIO register access with "
  178. "enabled MAC.\n");
  179. dump_stack();
  180. }
  181. }
  182. u16 b43_radio_read(struct b43_wldev *dev, u16 reg)
  183. {
  184. assert_mac_suspended(dev);
  185. return dev->phy.ops->radio_read(dev, reg);
  186. }
  187. void b43_radio_write(struct b43_wldev *dev, u16 reg, u16 value)
  188. {
  189. assert_mac_suspended(dev);
  190. dev->phy.ops->radio_write(dev, reg, value);
  191. }
  192. void b43_radio_mask(struct b43_wldev *dev, u16 offset, u16 mask)
  193. {
  194. b43_radio_write16(dev, offset,
  195. b43_radio_read16(dev, offset) & mask);
  196. }
  197. void b43_radio_set(struct b43_wldev *dev, u16 offset, u16 set)
  198. {
  199. b43_radio_write16(dev, offset,
  200. b43_radio_read16(dev, offset) | set);
  201. }
  202. void b43_radio_maskset(struct b43_wldev *dev, u16 offset, u16 mask, u16 set)
  203. {
  204. b43_radio_write16(dev, offset,
  205. (b43_radio_read16(dev, offset) & mask) | set);
  206. }
  207. bool b43_radio_wait_value(struct b43_wldev *dev, u16 offset, u16 mask,
  208. u16 value, int delay, int timeout)
  209. {
  210. u16 val;
  211. int i;
  212. for (i = 0; i < timeout; i += delay) {
  213. val = b43_radio_read(dev, offset);
  214. if ((val & mask) == value)
  215. return true;
  216. udelay(delay);
  217. }
  218. return false;
  219. }
  220. u16 b43_phy_read(struct b43_wldev *dev, u16 reg)
  221. {
  222. assert_mac_suspended(dev);
  223. dev->phy.writes_counter = 0;
  224. return dev->phy.ops->phy_read(dev, reg);
  225. }
  226. void b43_phy_write(struct b43_wldev *dev, u16 reg, u16 value)
  227. {
  228. assert_mac_suspended(dev);
  229. dev->phy.ops->phy_write(dev, reg, value);
  230. if (++dev->phy.writes_counter == B43_MAX_WRITES_IN_ROW) {
  231. b43_read16(dev, B43_MMIO_PHY_VER);
  232. dev->phy.writes_counter = 0;
  233. }
  234. }
  235. void b43_phy_copy(struct b43_wldev *dev, u16 destreg, u16 srcreg)
  236. {
  237. assert_mac_suspended(dev);
  238. dev->phy.ops->phy_write(dev, destreg,
  239. dev->phy.ops->phy_read(dev, srcreg));
  240. }
  241. void b43_phy_mask(struct b43_wldev *dev, u16 offset, u16 mask)
  242. {
  243. if (dev->phy.ops->phy_maskset) {
  244. assert_mac_suspended(dev);
  245. dev->phy.ops->phy_maskset(dev, offset, mask, 0);
  246. } else {
  247. b43_phy_write(dev, offset,
  248. b43_phy_read(dev, offset) & mask);
  249. }
  250. }
  251. void b43_phy_set(struct b43_wldev *dev, u16 offset, u16 set)
  252. {
  253. if (dev->phy.ops->phy_maskset) {
  254. assert_mac_suspended(dev);
  255. dev->phy.ops->phy_maskset(dev, offset, 0xFFFF, set);
  256. } else {
  257. b43_phy_write(dev, offset,
  258. b43_phy_read(dev, offset) | set);
  259. }
  260. }
  261. void b43_phy_maskset(struct b43_wldev *dev, u16 offset, u16 mask, u16 set)
  262. {
  263. if (dev->phy.ops->phy_maskset) {
  264. assert_mac_suspended(dev);
  265. dev->phy.ops->phy_maskset(dev, offset, mask, set);
  266. } else {
  267. b43_phy_write(dev, offset,
  268. (b43_phy_read(dev, offset) & mask) | set);
  269. }
  270. }
  271. void b43_phy_put_into_reset(struct b43_wldev *dev)
  272. {
  273. u32 tmp;
  274. switch (dev->dev->bus_type) {
  275. #ifdef CONFIG_B43_BCMA
  276. case B43_BUS_BCMA:
  277. tmp = bcma_aread32(dev->dev->bdev, BCMA_IOCTL);
  278. tmp &= ~B43_BCMA_IOCTL_GMODE;
  279. tmp |= B43_BCMA_IOCTL_PHY_RESET;
  280. tmp |= BCMA_IOCTL_FGC;
  281. bcma_awrite32(dev->dev->bdev, BCMA_IOCTL, tmp);
  282. udelay(1);
  283. tmp = bcma_aread32(dev->dev->bdev, BCMA_IOCTL);
  284. tmp &= ~BCMA_IOCTL_FGC;
  285. bcma_awrite32(dev->dev->bdev, BCMA_IOCTL, tmp);
  286. udelay(1);
  287. break;
  288. #endif
  289. #ifdef CONFIG_B43_SSB
  290. case B43_BUS_SSB:
  291. tmp = ssb_read32(dev->dev->sdev, SSB_TMSLOW);
  292. tmp &= ~B43_TMSLOW_GMODE;
  293. tmp |= B43_TMSLOW_PHYRESET;
  294. tmp |= SSB_TMSLOW_FGC;
  295. ssb_write32(dev->dev->sdev, SSB_TMSLOW, tmp);
  296. usleep_range(1000, 2000);
  297. tmp = ssb_read32(dev->dev->sdev, SSB_TMSLOW);
  298. tmp &= ~SSB_TMSLOW_FGC;
  299. ssb_write32(dev->dev->sdev, SSB_TMSLOW, tmp);
  300. usleep_range(1000, 2000);
  301. break;
  302. #endif
  303. }
  304. }
  305. void b43_phy_take_out_of_reset(struct b43_wldev *dev)
  306. {
  307. u32 tmp;
  308. switch (dev->dev->bus_type) {
  309. #ifdef CONFIG_B43_BCMA
  310. case B43_BUS_BCMA:
  311. /* Unset reset bit (with forcing clock) */
  312. tmp = bcma_aread32(dev->dev->bdev, BCMA_IOCTL);
  313. tmp &= ~B43_BCMA_IOCTL_PHY_RESET;
  314. tmp &= ~B43_BCMA_IOCTL_PHY_CLKEN;
  315. tmp |= BCMA_IOCTL_FGC;
  316. bcma_awrite32(dev->dev->bdev, BCMA_IOCTL, tmp);
  317. udelay(1);
  318. /* Do not force clock anymore */
  319. tmp = bcma_aread32(dev->dev->bdev, BCMA_IOCTL);
  320. tmp &= ~BCMA_IOCTL_FGC;
  321. tmp |= B43_BCMA_IOCTL_PHY_CLKEN;
  322. bcma_awrite32(dev->dev->bdev, BCMA_IOCTL, tmp);
  323. udelay(1);
  324. break;
  325. #endif
  326. #ifdef CONFIG_B43_SSB
  327. case B43_BUS_SSB:
  328. /* Unset reset bit (with forcing clock) */
  329. tmp = ssb_read32(dev->dev->sdev, SSB_TMSLOW);
  330. tmp &= ~B43_TMSLOW_PHYRESET;
  331. tmp &= ~B43_TMSLOW_PHYCLKEN;
  332. tmp |= SSB_TMSLOW_FGC;
  333. ssb_write32(dev->dev->sdev, SSB_TMSLOW, tmp);
  334. ssb_read32(dev->dev->sdev, SSB_TMSLOW); /* flush */
  335. usleep_range(1000, 2000);
  336. tmp = ssb_read32(dev->dev->sdev, SSB_TMSLOW);
  337. tmp &= ~SSB_TMSLOW_FGC;
  338. tmp |= B43_TMSLOW_PHYCLKEN;
  339. ssb_write32(dev->dev->sdev, SSB_TMSLOW, tmp);
  340. ssb_read32(dev->dev->sdev, SSB_TMSLOW); /* flush */
  341. usleep_range(1000, 2000);
  342. break;
  343. #endif
  344. }
  345. }
  346. int b43_switch_channel(struct b43_wldev *dev, unsigned int new_channel)
  347. {
  348. struct b43_phy *phy = &(dev->phy);
  349. u16 channelcookie, savedcookie;
  350. int err;
  351. if (new_channel == B43_DEFAULT_CHANNEL)
  352. new_channel = phy->ops->get_default_chan(dev);
  353. /* First we set the channel radio code to prevent the
  354. * firmware from sending ghost packets.
  355. */
  356. channelcookie = new_channel;
  357. if (b43_current_band(dev->wl) == IEEE80211_BAND_5GHZ)
  358. channelcookie |= B43_SHM_SH_CHAN_5GHZ;
  359. /* FIXME: set 40Mhz flag if required */
  360. if (0)
  361. channelcookie |= B43_SHM_SH_CHAN_40MHZ;
  362. savedcookie = b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_CHAN);
  363. b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_CHAN, channelcookie);
  364. /* Now try to switch the PHY hardware channel. */
  365. err = phy->ops->switch_channel(dev, new_channel);
  366. if (err)
  367. goto err_restore_cookie;
  368. dev->phy.channel = new_channel;
  369. /* Wait for the radio to tune to the channel and stabilize. */
  370. msleep(8);
  371. return 0;
  372. err_restore_cookie:
  373. b43_shm_write16(dev, B43_SHM_SHARED,
  374. B43_SHM_SH_CHAN, savedcookie);
  375. return err;
  376. }
  377. void b43_software_rfkill(struct b43_wldev *dev, bool blocked)
  378. {
  379. struct b43_phy *phy = &dev->phy;
  380. b43_mac_suspend(dev);
  381. phy->ops->software_rfkill(dev, blocked);
  382. phy->radio_on = !blocked;
  383. b43_mac_enable(dev);
  384. }
  385. /**
  386. * b43_phy_txpower_adjust_work - TX power workqueue.
  387. *
  388. * Workqueue for updating the TX power parameters in hardware.
  389. */
  390. void b43_phy_txpower_adjust_work(struct work_struct *work)
  391. {
  392. struct b43_wl *wl = container_of(work, struct b43_wl,
  393. txpower_adjust_work);
  394. struct b43_wldev *dev;
  395. mutex_lock(&wl->mutex);
  396. dev = wl->current_dev;
  397. if (likely(dev && (b43_status(dev) >= B43_STAT_STARTED)))
  398. dev->phy.ops->adjust_txpower(dev);
  399. mutex_unlock(&wl->mutex);
  400. }
  401. void b43_phy_txpower_check(struct b43_wldev *dev, unsigned int flags)
  402. {
  403. struct b43_phy *phy = &dev->phy;
  404. unsigned long now = jiffies;
  405. enum b43_txpwr_result result;
  406. if (!(flags & B43_TXPWR_IGNORE_TIME)) {
  407. /* Check if it's time for a TXpower check. */
  408. if (time_before(now, phy->next_txpwr_check_time))
  409. return; /* Not yet */
  410. }
  411. /* The next check will be needed in two seconds, or later. */
  412. phy->next_txpwr_check_time = round_jiffies(now + (HZ * 2));
  413. if ((dev->dev->board_vendor == SSB_BOARDVENDOR_BCM) &&
  414. (dev->dev->board_type == SSB_BOARD_BU4306))
  415. return; /* No software txpower adjustment needed */
  416. result = phy->ops->recalc_txpower(dev, !!(flags & B43_TXPWR_IGNORE_TSSI));
  417. if (result == B43_TXPWR_RES_DONE)
  418. return; /* We are done. */
  419. B43_WARN_ON(result != B43_TXPWR_RES_NEED_ADJUST);
  420. B43_WARN_ON(phy->ops->adjust_txpower == NULL);
  421. /* We must adjust the transmission power in hardware.
  422. * Schedule b43_phy_txpower_adjust_work(). */
  423. ieee80211_queue_work(dev->wl->hw, &dev->wl->txpower_adjust_work);
  424. }
  425. int b43_phy_shm_tssi_read(struct b43_wldev *dev, u16 shm_offset)
  426. {
  427. const bool is_ofdm = (shm_offset != B43_SHM_SH_TSSI_CCK);
  428. unsigned int a, b, c, d;
  429. unsigned int average;
  430. u32 tmp;
  431. tmp = b43_shm_read32(dev, B43_SHM_SHARED, shm_offset);
  432. a = tmp & 0xFF;
  433. b = (tmp >> 8) & 0xFF;
  434. c = (tmp >> 16) & 0xFF;
  435. d = (tmp >> 24) & 0xFF;
  436. if (a == 0 || a == B43_TSSI_MAX ||
  437. b == 0 || b == B43_TSSI_MAX ||
  438. c == 0 || c == B43_TSSI_MAX ||
  439. d == 0 || d == B43_TSSI_MAX)
  440. return -ENOENT;
  441. /* The values are OK. Clear them. */
  442. tmp = B43_TSSI_MAX | (B43_TSSI_MAX << 8) |
  443. (B43_TSSI_MAX << 16) | (B43_TSSI_MAX << 24);
  444. b43_shm_write32(dev, B43_SHM_SHARED, shm_offset, tmp);
  445. if (is_ofdm) {
  446. a = (a + 32) & 0x3F;
  447. b = (b + 32) & 0x3F;
  448. c = (c + 32) & 0x3F;
  449. d = (d + 32) & 0x3F;
  450. }
  451. /* Get the average of the values with 0.5 added to each value. */
  452. average = (a + b + c + d + 2) / 4;
  453. if (is_ofdm) {
  454. /* Adjust for CCK-boost */
  455. if (b43_shm_read16(dev, B43_SHM_SHARED, B43_SHM_SH_HOSTF1)
  456. & B43_HF_CCKBOOST)
  457. average = (average >= 13) ? (average - 13) : 0;
  458. }
  459. return average;
  460. }
  461. void b43_phyop_switch_analog_generic(struct b43_wldev *dev, bool on)
  462. {
  463. b43_write16(dev, B43_MMIO_PHY0, on ? 0 : 0xF4);
  464. }
  465. bool b43_channel_type_is_40mhz(enum nl80211_channel_type channel_type)
  466. {
  467. return (channel_type == NL80211_CHAN_HT40MINUS ||
  468. channel_type == NL80211_CHAN_HT40PLUS);
  469. }
  470. /* http://bcm-v4.sipsolutions.net/802.11/PHY/N/BmacPhyClkFgc */
  471. void b43_phy_force_clock(struct b43_wldev *dev, bool force)
  472. {
  473. u32 tmp;
  474. WARN_ON(dev->phy.type != B43_PHYTYPE_N &&
  475. dev->phy.type != B43_PHYTYPE_HT);
  476. switch (dev->dev->bus_type) {
  477. #ifdef CONFIG_B43_BCMA
  478. case B43_BUS_BCMA:
  479. tmp = bcma_aread32(dev->dev->bdev, BCMA_IOCTL);
  480. if (force)
  481. tmp |= BCMA_IOCTL_FGC;
  482. else
  483. tmp &= ~BCMA_IOCTL_FGC;
  484. bcma_awrite32(dev->dev->bdev, BCMA_IOCTL, tmp);
  485. break;
  486. #endif
  487. #ifdef CONFIG_B43_SSB
  488. case B43_BUS_SSB:
  489. tmp = ssb_read32(dev->dev->sdev, SSB_TMSLOW);
  490. if (force)
  491. tmp |= SSB_TMSLOW_FGC;
  492. else
  493. tmp &= ~SSB_TMSLOW_FGC;
  494. ssb_write32(dev->dev->sdev, SSB_TMSLOW, tmp);
  495. break;
  496. #endif
  497. }
  498. }
  499. /* http://bcm-v4.sipsolutions.net/802.11/PHY/Cordic */
  500. struct b43_c32 b43_cordic(int theta)
  501. {
  502. static const u32 arctg[] = {
  503. 2949120, 1740967, 919879, 466945, 234379, 117304,
  504. 58666, 29335, 14668, 7334, 3667, 1833,
  505. 917, 458, 229, 115, 57, 29,
  506. };
  507. u8 i;
  508. s32 tmp;
  509. s8 signx = 1;
  510. u32 angle = 0;
  511. struct b43_c32 ret = { .i = 39797, .q = 0, };
  512. while (theta > (180 << 16))
  513. theta -= (360 << 16);
  514. while (theta < -(180 << 16))
  515. theta += (360 << 16);
  516. if (theta > (90 << 16)) {
  517. theta -= (180 << 16);
  518. signx = -1;
  519. } else if (theta < -(90 << 16)) {
  520. theta += (180 << 16);
  521. signx = -1;
  522. }
  523. for (i = 0; i <= 17; i++) {
  524. if (theta > angle) {
  525. tmp = ret.i - (ret.q >> i);
  526. ret.q += ret.i >> i;
  527. ret.i = tmp;
  528. angle += arctg[i];
  529. } else {
  530. tmp = ret.i + (ret.q >> i);
  531. ret.q -= ret.i >> i;
  532. ret.i = tmp;
  533. angle -= arctg[i];
  534. }
  535. }
  536. ret.i *= signx;
  537. ret.q *= signx;
  538. return ret;
  539. }