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