summaryrefslogtreecommitdiffstats
path: root/package/bcm43xx-mac80211/src/bcm43xx/bcm43xx_lo.c
blob: a2210345eaf8f8a8dfe2dea3397dd56358d7efd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
/*

  Broadcom BCM43xx wireless driver

  G PHY LO (LocalOscillator) Measuring and Control routines

  Copyright (c) 2005 Martin Langer <martin-langer@gmx.de>,
  Copyright (c) 2005, 2006 Stefano Brivio <st3@riseup.net>
  Copyright (c) 2005-2007 Michael Buesch <mb@bu3sch.de>
  Copyright (c) 2005, 2006 Danny van Dyk <kugelfang@gentoo.org>
  Copyright (c) 2005, 2006 Andreas Jaggi <andreas.jaggi@waterwave.ch>

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; see the file COPYING.  If not, write to
  the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
  Boston, MA 02110-1301, USA.

*/

#include "bcm43xx.h"
#include "bcm43xx_lo.h"
#include "bcm43xx_phy.h"
#include "bcm43xx_main.h"

#include <linux/delay.h>


/* Write the LocalOscillator Control (adjust) value-pair. */
static void bcm43xx_lo_write(struct bcm43xx_wldev *dev,
			     struct bcm43xx_loctl *control)
{
	struct bcm43xx_phy *phy = &dev->phy;
	u16 value;
	u16 reg;

	if (BCM43xx_DEBUG) {
		if (unlikely(abs(control->i) > 16 ||
			     abs(control->q) > 16)) {
			printk(KERN_ERR PFX "ERROR: Invalid LO control pair "
					    "(I: %d, Q: %d)\n",
			       control->i, control->q);
			dump_stack();
			return;
		}
	}

	value = (u8)(control->q);
	value |= ((u8)(control->i)) << 8;

	reg = (phy->type == BCM43xx_PHYTYPE_B) ? 0x002F : BCM43xx_PHY_LO_CTL;
	bcm43xx_phy_write(dev, reg, value);
}

static inline
void assert_rfatt_and_bbatt(const struct bcm43xx_rfatt *rfatt,
			    const struct bcm43xx_bbatt *bbatt)
{
	if (BCM43xx_DEBUG) {
		int err = 0;

		if (unlikely(rfatt->att >= 16)) {
			dprintk(KERN_ERR PFX "ERROR: invalid rf_att: %u\n",
				rfatt->att);
			err = 1;
		}
		if (unlikely(bbatt->att >= 9)) {
			dprintk(KERN_ERR PFX "ERROR: invalid bband_att: %u\n",
				bbatt->att);
			err = 1;
		}
		if (unlikely(err))
			dump_stack();
	}
}

static
struct bcm43xx_loctl * bcm43xx_get_lo_g_ctl_nopadmix(struct bcm43xx_wldev *dev,
						  const struct bcm43xx_rfatt *rfatt,
						  const struct bcm43xx_bbatt *bbatt)
{
	struct bcm43xx_phy *phy = &dev->phy;
	struct bcm43xx_txpower_lo_control *lo = phy->lo_control;

	assert_rfatt_and_bbatt(rfatt, bbatt);
	return &(lo->no_padmix[bbatt->att][rfatt->att]);
}

struct bcm43xx_loctl * bcm43xx_get_lo_g_ctl(struct bcm43xx_wldev *dev,
					 const struct bcm43xx_rfatt *rfatt,
					 const struct bcm43xx_bbatt *bbatt)
{
	struct bcm43xx_phy *phy = &dev->phy;
	struct bcm43xx_txpower_lo_control *lo = phy->lo_control;
	struct bcm43xx_loctl *ret;

	assert_rfatt_and_bbatt(rfatt, bbatt);
	if (rfatt->with_padmix)
		ret = &(lo->with_padmix[bbatt->att][rfatt->att]);
	else
		ret = &(lo->no_padmix[bbatt->att][rfatt->att]);

	return ret;
}

/* Call a function for every possible LO control value-pair. */
static int bcm43xx_call_for_each_loctl(struct bcm43xx_wldev *dev,
				       int (*func)(struct bcm43xx_wldev *,
						   struct bcm43xx_loctl *))
{
	struct bcm43xx_phy *phy = &dev->phy;
	struct bcm43xx_txpower_lo_control *ctl = phy->lo_control;
	int i, j;
	int err;

	for (i = 0; i < BCM43xx_NR_BB; i++) {
		for (j = 0; j < BCM43xx_NR_RF; j++) {
			err = func(dev, &(ctl->with_padmix[i][j]));
			if (unlikely(err))
				return err;
		}
	}
	for (i = 0; i < BCM43xx_NR_BB; i++) {
		for (j = 0; j < BCM43xx_NR_RF; j++) {
			err = func(dev, &(ctl->no_padmix[i][j]));
			if (unlikely(err))
				return err;
		}
	}

	return 0;
}

static u16 lo_b_r15_loop(struct bcm43xx_wldev *dev)
{
	int i;
	u16 ret = 0;

	for (i = 0; i < 10; i++){
		bcm43xx_phy_write(dev, 0x0015, 0xAFA0);
		udelay(1);
		bcm43xx_phy_write(dev, 0x0015, 0xEFA0);
		udelay(10);
		bcm43xx_phy_write(dev, 0x0015, 0xFFA0);
		udelay(40);
		ret += bcm43xx_phy_read(dev, 0x002C);
	}

	return ret;
}

void bcm43xx_lo_b_measure(struct bcm43xx_wldev *dev)
{
	struct bcm43xx_phy *phy = &dev->phy;
	u16 regstack[12] = { 0 };
	u16 mls;
	u16 fval;
	int i, j;

	regstack[0] = bcm43xx_phy_read(dev, 0x0015);
	regstack[1] = bcm43xx_radio_read16(dev, 0x0052) & 0xFFF0;

	if (phy->radio_ver == 0x2053) {
		regstack[2] = bcm43xx_phy_read(dev, 0x000A);
		regstack[3] = bcm43xx_phy_read(dev, 0x002A);
		regstack[4] = bcm43xx_phy_read(dev, 0x0035);
		regstack[5] = bcm43xx_phy_read(dev, 0x0003);
		regstack[6] = bcm43xx_phy_read(dev, 0x0001);
		regstack[7] = bcm43xx_phy_read(dev, 0x0030);

		regstack[8] = bcm43xx_radio_read16(dev, 0x0043);
		regstack[9] = bcm43xx_radio_read16(dev, 0x007A);
		regstack[10] = bcm43xx_read16(dev, 0x03EC);
		regstack[11] = bcm43xx_radio_read16(dev, 0x0052) & 0x00F0;

		bcm43xx_phy_write(dev, 0x0030, 0x00FF);
		bcm43xx_write16(dev, 0x03EC, 0x3F3F);
		bcm43xx_phy_write(dev, 0x0035, regstack[4] & 0xFF7F);
		bcm43xx_radio_write16(dev, 0x007A, regstack[9] & 0xFFF0);
	}
	bcm43xx_phy_write(dev, 0x0015, 0xB000);
	bcm43xx_phy_write(dev, 0x002B, 0x0004);

	if (phy->radio_ver == 0x2053) {
		bcm43xx_phy_write(dev, 0x002B, 0x0203);
		bcm43xx_phy_write(dev, 0x002A, 0x08A3);
	}

	phy->minlowsig[0] = 0xFFFF;

	for (i = 0; i < 4; i++) {
		bcm43xx_radio_write16(dev, 0x0052, regstack[1] | i);
		lo_b_r15_loop(dev);
	}
	for (i = 0; i < 10; i++) {
		bcm43xx_radio_write16(dev, 0x0052, regstack[1] | i);
		mls = lo_b_r15_loop(dev) / 10;
		if (mls < phy->minlowsig[0]) {
			phy->minlowsig[0] = mls;
			phy->minlowsigpos[0] = i;
		}
	}
	bcm43xx_radio_write16(dev, 0x0052, regstack[1] | phy->minlowsigpos[0]);

	phy->minlowsig[1] = 0xFFFF;

	for (i = -4; i < 5; i += 2) {
		for (j = -4; j < 5; j += 2) {
			if (j < 0)
				fval = (0x0100 * i) + j + 0x0100;
			else
				fval = (0x0100 * i) + j;
			bcm43xx_phy_write(dev, 0x002F, fval);
			mls = lo_b_r15_loop(dev) / 10;
			if (mls < phy->minlowsig[1]) {
				phy->minlowsig[1] = mls;
				phy->minlowsigpos[1] = fval;
			}
		}
	}
	phy->minlowsigpos[1] += 0x0101;

	bcm43xx_phy_write(dev, 0x002F, phy->minlowsigpos[1]);
	if (phy->radio_ver == 0x2053) {
		bcm43xx_phy_write(dev, 0x000A, regstack[2]);
		bcm43xx_phy_write(dev, 0x002A, regstack[3]);
		bcm43xx_phy_write(dev, 0x0035, regstack[4]);
		bcm43xx_phy_write(dev, 0x0003, regstack[5]);
		bcm43xx_phy_write(dev, 0x0001, regstack[6]);
		bcm43xx_phy_write(dev, 0x0030, regstack[7]);

		bcm43xx_radio_write16(dev, 0x0043, regstack[8]);
		bcm43xx_radio_write16(dev, 0x007A, regstack[9]);

		bcm43xx_radio_write16(dev, 0x0052,
		                      (bcm43xx_radio_read16(dev, 0x0052) & 0x000F)
				      | regstack[11]);

		bcm43xx_write16(dev, 0x03EC, regstack[10]);
	}
	bcm43xx_phy_write(dev, 0x0015, regstack[0]);
}

static u16 lo_measure_feedthrough(struct bcm43xx_wldev *dev,
				  u16 lna, u16 pga, u16 trsw_rx)
{
	struct bcm43xx_phy *phy = &dev->phy;
	u16 rfover;

	if (phy->gmode) {
		lna <<= BCM43xx_PHY_RFOVERVAL_LNA_SHIFT;
		pga <<= BCM43xx_PHY_RFOVERVAL_PGA_SHIFT;

		assert((lna & ~BCM43xx_PHY_RFOVERVAL_LNA) == 0);
		assert((pga & ~BCM43xx_PHY_RFOVERVAL_PGA) == 0);
/*FIXME This assertion fails		assert((trsw_rx & ~(BCM43xx_PHY_RFOVERVAL_TRSWRX |
				    BCM43xx_PHY_RFOVERVAL_BW)) == 0);
*/
trsw_rx &= (BCM43xx_PHY_RFOVERVAL_TRSWRX | BCM43xx_PHY_RFOVERVAL_BW);

		/* Construct the RF Override Value */
		rfover = BCM43xx_PHY_RFOVERVAL_UNK;
		rfover |= pga;
		rfover |= lna;
		rfover |= trsw_rx;
		if ((dev->dev->bus->sprom.r1.boardflags_lo & BCM43xx_BFL_EXTLNA) &&
		    phy->rev > 6)
			rfover |= BCM43xx_PHY_RFOVERVAL_EXTLNA;

		bcm43xx_phy_write(dev, BCM43xx_PHY_PGACTL, 0xE300);
		bcm43xx_phy_write(dev, BCM43xx_PHY_RFOVERVAL, rfover);
		udelay(10);
		rfover |= BCM43xx_PHY_RFOVERVAL_BW_LBW;
		bcm43xx_phy_write(dev, BCM43xx_PHY_RFOVERVAL, rfover);
		udelay(10);
		rfover |= BCM43xx_PHY_RFOVERVAL_BW_LPF;
		bcm43xx_phy_write(dev, BCM43xx_PHY_RFOVERVAL, rfover);
		udelay(10);
		bcm43xx_phy_write(dev, BCM43xx_PHY_PGACTL, 0xF300);
	} else {
		pga |= BCM43xx_PHY_PGACTL_UNKNOWN;
		bcm43xx_phy_write(dev, BCM43xx_PHY_PGACTL, pga);
		udelay(10);
		pga |= BCM43xx_PHY_PGACTL_LOWBANDW;
		bcm43xx_phy_write(dev, BCM43xx_PHY_PGACTL, pga);
		udelay(10);
		pga |= BCM43xx_PHY_PGACTL_LPF;
		bcm43xx_phy_write(dev, BCM43xx_PHY_PGACTL, pga);
	}
	udelay(21);

	return bcm43xx_phy_read(dev, BCM43xx_PHY_LO_LEAKAGE);
}

/* TXCTL Register and Value Table.
 * Returns the "TXCTL Register".
 * "value" is the "TXCTL Value".
 * "pad_mix_gain" is the PAD Mixer Gain.
 */
static u16 lo_txctl_register_table(struct bcm43xx_wldev *dev,
				   u16 *value, u16 *pad_mix_gain)
{
	struct bcm43xx_phy *phy = &dev->phy;
	u16 reg, v, padmix;

	if (phy->type == BCM43xx_PHYTYPE_B) {
		v = 0x30;
		if (phy->radio_rev <= 5) {
			reg = 0x43;
			padmix = 0;
		} else {
			reg = 0x52;
			padmix = 5;
		}
	} else {
		if (phy->rev >= 2 && phy->radio_rev == 8) {
			reg = 0x43;
			v = 0x10;
			padmix = 2;
		} else {
			reg = 0x52;
			v = 0x30;
			padmix = 5;
		}
	}
	if (value)
		*value = v;
	if (pad_mix_gain)
		*pad_mix_gain = padmix;

	return reg;
}

static void lo_measure_txctl_values(struct bcm43xx_wldev *dev)
{
	struct bcm43xx_phy *phy = &dev->phy;
	struct bcm43xx_txpower_lo_control *lo = phy->lo_control;
	u16 reg, mask;
	u16 trsw_rx, pga;
	u16 radio_pctl_reg;

	static const u8 tx_bias_values[] = {
		0x09, 0x08, 0x0A, 0x01, 0x00,
		0x02, 0x05, 0x04, 0x06,
	};
	static const u8 tx_magn_values[] = {
		0x70, 0x40,
	};

	if (!has_loopback_gain(phy)) {
		radio_pctl_reg = 6;
		trsw_rx = 2;
		pga = 0;
	} else {
		int lb_gain; /* Loopback gain (in dB) */

		trsw_rx = 0;
		lb_gain = phy->max_lb_gain / 2;
		if (lb_gain > 10) {
			radio_pctl_reg = 0;
			pga = abs(10 - lb_gain) / 6;
			pga = limit_value(pga, 0, 15);
		} else {
			int cmp_val;
			int tmp;

			pga = 0;
			cmp_val = 0x24;
			if ((phy->rev >= 2) &&
			    (phy->radio_ver == 0x2050) &&
			    (phy->radio_rev == 8))
				cmp_val = 0x3C;
			tmp = lb_gain;
			if ((10 - lb_gain) < cmp_val)
				tmp = (10 - lb_gain);
			if (tmp < 0)
				tmp += 6;
			else
				tmp += 3;
			cmp_val /= 4;
			tmp /= 4;
			if (tmp >= cmp_val)
				radio_pctl_reg = cmp_val;
			else
				radio_pctl_reg = tmp;
		}
	}
	bcm43xx_radio_write16(dev, 0x43,
			      (bcm43xx_radio_read16(dev, 0x43)
			       & 0xFFF0) | radio_pctl_reg);
	bcm43xx_phy_set_baseband_attenuation(dev, 2);

	reg = lo_txctl_register_table(dev, &mask, NULL);
	mask = ~mask;
	bcm43xx_radio_write16(dev, reg,
			      bcm43xx_radio_read16(dev, reg)
			      & mask);

	if (has_tx_magnification(phy)) {
		int i, j;
		int feedthrough;
		int min_feedth = 0xFFFF;
		u8 tx_magn, tx_bias;

		for (i = 0; i < ARRAY_SIZE(tx_magn_values); i++) {
			tx_magn = tx_magn_values[i];
			bcm43xx_radio_write16(dev, 0x52,
					      (bcm43xx_radio_read16(dev, 0x52)
					       & 0xFF0F) | tx_magn);
			for (j = 0; j < ARRAY_SIZE(tx_bias_values); j++) {
				tx_bias = tx_bias_values[j];
				bcm43xx_radio_write16(dev, 0x52,
						      (bcm43xx_radio_read16(dev, 0x52)
						       & 0xFFF0) | tx_bias);
				feedthrough = lo_measure_feedthrough(dev, 0, pga, trsw_rx);
				if (feedthrough < min_feedth) {
					lo->tx_bias = tx_bias;
					lo->tx_magn = tx_magn;
					min_feedth = feedthrough;
				}
				if (lo->tx_bias == 0)
					break;
			}
			bcm43xx_radio_write16(dev, 0x52,
					      (bcm43xx_radio_read16(dev, 0x52)
					       & 0xFF00) | lo->tx_bias | lo->tx_magn);
		}
	} else {
		lo->tx_magn = 0; /* FIXME */
		lo->tx_bias = 0;
		bcm43xx_radio_write16(dev, 0x52,
				      bcm43xx_radio_read16(dev, 0x52)
				      & 0xFFF0); /* TX bias == 0 */
	}
}

static void lo_read_power_vector(struct bcm43xx_wldev *dev)
{
	struct bcm43xx_phy *phy = &dev->phy;
	struct bcm43xx_txpower_lo_control *lo = phy->lo_control;
	u16 i;
	u64 tmp;
	u64 power_vector = 0;
	int rf_offset, bb_offset;
	struct bcm43xx_loctl *loctl;

	for (i = 0; i < 8; i += 2) {
		tmp = bcm43xx_shm_read16(dev, BCM43xx_SHM_SHARED,
					 0x310 + i);
		/* Clear the top byte. We get holes in the bitmap... */
		tmp &= 0xFF;
		power_vector |= (tmp << (i * 8));
		/* Clear the vector on the device. */
		bcm43xx_shm_write16(dev, BCM43xx_SHM_SHARED,
				    0x310 + i, 0);
	}

	if (power_vector)
		lo->power_vector = power_vector;
	power_vector = lo->power_vector;

	for (i = 0; i < 64; i++) {
		if (power_vector & ((u64)1ULL << i)) {
			/* Now figure out which bcm43xx_loctl corresponds
			 * to this bit.
			 */
			rf_offset = i / lo->rfatt_list.len;
			bb_offset = i % lo->rfatt_list.len;//FIXME?
			loctl = bcm43xx_get_lo_g_ctl(dev, &lo->rfatt_list.list[rf_offset],
						  &lo->bbatt_list.list[bb_offset]);
			/* And mark it as "used", as the device told us
			 * through the bitmap it is using it.
			 */
			loctl->used = 1;
		}
	}
}

/* 802.11/LO/GPHY/MeasuringGains */
static void lo_measure_gain_values(struct bcm43xx_wldev *dev,
				   s16 max_rx_gain,
				   int use_trsw_rx)
{
	struct bcm43xx_phy *phy = &dev->phy;
	u16 tmp;

	if (max_rx_gain < 0)
		max_rx_gain = 0;

	if (has_loopback_gain(phy)) {
		int trsw_rx = 0;
		int trsw_rx_gain;

		if (use_trsw_rx) {
			trsw_rx_gain = phy->trsw_rx_gain / 2;
			if (max_rx_gain >= trsw_rx_gain) {
				trsw_rx_gain = max_rx_gain - trsw_rx_gain;
				trsw_rx = 0x20;
			}
		} else
			trsw_rx_gain = max_rx_gain;
		if (trsw_rx_gain < 9) {
			phy->lna_lod_gain = 0;
		} else {
			phy->lna_lod_gain = 1;
			trsw_rx_gain -= 8;
		}
		trsw_rx_gain = limit_value(trsw_rx_gain, 0, 0x2D);
		phy->pga_gain = trsw_rx_gain / 3;
		if (phy->pga_gain >= 5) {
			phy->pga_gain -= 5;
			phy->lna_gain = 2;
		} else
			phy->lna_gain = 0;
	} else {
		phy->lna_gain = 0;
		phy->trsw_rx_gain = 0x20;
		if (max_rx_gain >= 0x14) {
			phy->lna_lod_gain = 1;
			phy->pga_gain = 2;
		} else if (max_rx_gain >= 0x12) {
			phy->lna_lod_gain = 1;
			phy->pga_gain = 1;
		} else if (max_rx_gain >= 0xF) {
			phy->lna_lod_gain = 1;
			phy->pga_gain = 0;
		} else {
			phy->lna_lod_gain = 0;
			phy->pga_gain = 0;
		}
	}

	tmp = bcm43xx_radio_read16(dev, 0x7A);
	if (phy->lna_lod_gain == 0)
		tmp &= ~0x0008;
	else
		tmp |= 0x0008;
	bcm43xx_radio_write16(dev, 0x7A, tmp);
}

struct lo_g_saved_values {
	u8 old_channel;

	/* Core registers */
	u16 reg_3F4;
	u16 reg_3E2;

	/* PHY registers */
	u16 phy_lo_mask;
	u16 phy_extg_01;
	u16 phy_dacctl_hwpctl;
	u16 phy_dacctl;
	u16 phy_base_14;
	u16 phy_hpwr_tssictl;
	u16 phy_analogover;
	u16 phy_analogoverval;
	u16 phy_rfover;
	u16 phy_rfoverval;
	u16 phy_classctl;
	u16 phy_base_3E;
	u16 phy_crs0;
	u16 phy_pgactl;
	u16 phy_base_2A;
	u16 phy_syncctl;
	u16 phy_base_30;
	u16 phy_base_06;

	/* Radio registers */
	u16 radio_43;
	u16 radio_7A;
	u16 radio_52;
};

static void lo_measure_setup(struct bcm43xx_wldev *dev,
			     struct lo_g_saved_values *sav)
{
	struct ssb_sprom *sprom = &dev->dev->bus->sprom;
	struct bcm43xx_phy *phy = &dev->phy;
	struct bcm43xx_txpower_lo_control *lo = phy->lo_control;
	u16 tmp;

	if (has_hardware_pctl(phy)) {
		sav->phy_lo_mask = bcm43xx_phy_read(dev, BCM43xx_PHY_LO_MASK);
		sav->phy_extg_01 = bcm43xx_phy_read(dev, BCM43xx_PHY_EXTG(0x01));
		sav->phy_dacctl_hwpctl = bcm43xx_phy_read(dev, BCM43xx_PHY_DACCTL);
		sav->phy_base_14 = bcm43xx_phy_read(dev, BCM43xx_PHY_BASE(0x14));
		sav->phy_hpwr_tssictl = bcm43xx_phy_read(dev, BCM43xx_PHY_HPWR_TSSICTL);

		bcm43xx_phy_write(dev, BCM43xx_PHY_HPWR_TSSICTL,
				  bcm43xx_phy_read(dev, BCM43xx_PHY_HPWR_TSSICTL)
				  | 0x100);
		bcm43xx_phy_write(dev, BCM43xx_PHY_EXTG(0x01),
				  bcm43xx_phy_read(dev, BCM43xx_PHY_EXTG(0x01))
				  | 0x40);
		bcm43xx_phy_write(dev, BCM43xx_PHY_DACCTL,
				  bcm43xx_phy_read(dev, BCM43xx_PHY_DACCTL)
				  | 0x40);
		bcm43xx_phy_write(dev, BCM43xx_PHY_BASE(0x14),
				  bcm43xx_phy_read(dev, BCM43xx_PHY_BASE(0x14))
				  | 0x200);
	}
	if (phy->type == BCM43xx_PHYTYPE_B &&
	    phy->radio_ver == 0x2050 &&
	    phy->radio_rev < 6) {
		bcm43xx_phy_write(dev, BCM43xx_PHY_BASE(0x16), 0x410);
		bcm43xx_phy_write(dev, BCM43xx_PHY_BASE(0x17), 0x820);
	}
	if (!lo->rebuild && has_hardware_pctl(phy))
		lo_read_power_vector(dev);
	if (phy->rev >= 2) {
		sav->phy_analogover = bcm43xx_phy_read(dev, BCM43xx_PHY_ANALOGOVER);
		sav->phy_analogoverval = bcm43xx_phy_read(dev, BCM43xx_PHY_ANALOGOVERVAL);
		sav->phy_rfover = bcm43xx_phy_read(dev, BCM43xx_PHY_RFOVER);
		sav->phy_rfoverval = bcm43xx_phy_read(dev, BCM43xx_PHY_RFOVERVAL);
		sav->phy_classctl = bcm43xx_phy_read(dev, BCM43xx_PHY_CLASSCTL);
		sav->phy_base_3E = bcm43xx_phy_read(dev, BCM43xx_PHY_BASE(0x3E));
		sav->phy_crs0 = bcm43xx_phy_read(dev, BCM43xx_PHY_CRS0);

		bcm43xx_phy_write(dev, BCM43xx_PHY_CLASSCTL,
				  bcm43xx_phy_read(dev, BCM43xx_PHY_CLASSCTL)
				  & 0xFFFC);
		bcm43xx_phy_write(dev, BCM43xx_PHY_CRS0,
				  bcm43xx_phy_read(dev, BCM43xx_PHY_CRS0)
				  & 0x7FFF);
		bcm43xx_phy_write(dev, BCM43xx_PHY_ANALOGOVER,
				  bcm43xx_phy_read(dev, BCM43xx_PHY_ANALOGOVER)
				  | 0x0003);
		bcm43xx_phy_write(dev, BCM43xx_PHY_ANALOGOVERVAL,
				  bcm43xx_phy_read(dev, BCM43xx_PHY_ANALOGOVERVAL)
				  & 0xFFFC);
		if (phy->type == BCM43xx_PHYTYPE_G) {
			if ((phy->rev >= 7) &&
			    (sprom->r1.boardflags_lo & BCM43xx_BFL_EXTLNA)) {
				bcm43xx_phy_write(dev, BCM43xx_PHY_RFOVER, 0x933);
			} else {
				bcm43xx_phy_write(dev, BCM43xx_PHY_RFOVER, 0x133);
			}
		} else {
			bcm43xx_phy_write(dev, BCM43xx_PHY_RFOVER, 0);
		}
		bcm43xx_phy_write(dev, BCM43xx_PHY_BASE(0x3E), 0);
	}
	sav->reg_3F4 = bcm43xx_read16(dev, 0x3F4);
	sav->reg_3E2 = bcm43xx_read16(dev, 0x3E2);
	sav->radio_43 = bcm43xx_radio_read16(dev, 0x43);
	sav->radio_7A = bcm43xx_radio_read16(dev, 0x7A);
	sav->phy_pgactl = bcm43xx_phy_read(dev, BCM43xx_PHY_PGACTL);
	sav->phy_base_2A = bcm43xx_phy_read(dev, BCM43xx_PHY_BASE(0x2A));
	sav->phy_syncctl = bcm43xx_phy_read(dev, BCM43xx_PHY_SYNCCTL);
	sav->phy_dacctl = bcm43xx_phy_read(dev, BCM43xx_PHY_DACCTL);

	if (!has_tx_magnification(phy)) {
		sav->radio_52 = bcm43xx_radio_read16(dev, 0x52);
		sav->radio_52 &= 0x00F0;
	}
	if (phy->type == BCM43xx_PHYTYPE_B) {
		sav->phy_base_30 = bcm43xx_phy_read(dev, BCM43xx_PHY_BASE(0x30));
		sav->phy_base_06 = bcm43xx_phy_read(dev, BCM43xx_PHY_BASE(0x06));
		bcm43xx_phy_write(dev, BCM43xx_PHY_BASE(0x30), 0x00FF);
		bcm43xx_phy_write(dev, BCM43xx_PHY_BASE(0x06), 0x3F3F);
	} else {
		bcm43xx_write16(dev, 0x3E2,
				bcm43xx_read16(dev, 0x3E2)
				| 0x8000);
	}
	bcm43xx_write16(dev, 0x3F4,
			bcm43xx_read16(dev, 0x3F4)
			& 0xF000);

	tmp = (phy->type == BCM43xx_PHYTYPE_G) ? BCM43xx_PHY_LO_MASK : BCM43xx_PHY_BASE(0x2E);
	bcm43xx_phy_write(dev, tmp, 0x007F);

	tmp = sav->phy_syncctl;
	bcm43xx_phy_write(dev, BCM43xx_PHY_SYNCCTL, tmp & 0xFF7F);
	tmp = sav->radio_7A;
	bcm43xx_radio_write16(dev, 0x007A, tmp & 0xFFF0);

	bcm43xx_phy_write(dev, BCM43xx_PHY_BASE(0x2A), 0x8A3);
	if (phy->type == BCM43xx_PHYTYPE_G ||
	    (phy->type == BCM43xx_PHYTYPE_B &&
	     phy->radio_ver == 0x2050 &&
	     phy->radio_rev >= 6)) {
		bcm43xx_phy_write(dev, BCM43xx_PHY_BASE(0x2B), 0x1003);
	} else
		bcm43xx_phy_write(dev, BCM43xx_PHY_BASE(0x2B), 0x0802);
	if (phy->rev >= 2)
		bcm43xx_dummy_transmission(dev);
	bcm43xx_radio_selectchannel(dev, 6, 0);
	bcm43xx_radio_read16(dev, 0x51); /* dummy read */
	if (phy->type == BCM43xx_PHYTYPE_G)
		bcm43xx_phy_write(dev, BCM43xx_PHY_BASE(0x2F), 0);
	if (lo->rebuild)
		lo_measure_txctl_values(dev);
	if (phy->type == BCM43xx_PHYTYPE_G && phy->rev >= 3) {
		bcm43xx_phy_write(dev, BCM43xx_PHY_LO_MASK, 0xC078);
	} else {
		if (phy->type == BCM43xx_PHYTYPE_B)
			bcm43xx_phy_write(dev, BCM43xx_PHY_BASE(0x2E), 0x8078);
		else
			bcm43xx_phy_write(dev, BCM43xx_PHY_LO_MASK, 0x8078);
	}
}

static void lo_measure_restore(struct bcm43xx_wldev *dev,
			       struct lo_g_saved_values *sav)
{
	struct bcm43xx_phy *phy = &dev->phy;
	struct bcm43xx_txpower_lo_control *lo = phy->lo_control;
	u16 tmp;

	if (phy->rev >= 2) {
		bcm43xx_phy_write(dev, BCM43xx_PHY_PGACTL, 0xE300);
		tmp = (phy->pga_gain << 8);
		bcm43xx_phy_write(dev, BCM43xx_PHY_RFOVERVAL, tmp | 0xA0);
		udelay(5);
		bcm43xx_phy_write(dev, BCM43xx_PHY_RFOVERVAL, tmp | 0xA2);
		udelay(2);
		bcm43xx_phy_write(dev, BCM43xx_PHY_RFOVERVAL, tmp | 0xA3);
	} else {
		tmp = (phy->pga_gain | 0xEFA0);
		bcm43xx_phy_write(dev, BCM43xx_PHY_PGACTL, tmp);
	}
	if (has_hardware_pctl(phy)) {
		bcm43xx_gphy_dc_lt_init(dev);
	} else {
		if (lo->rebuild)
			bcm43xx_lo_g_adjust_to(dev, 3, 2, 0);
		else
			bcm43xx_lo_g_adjust(dev);
	}
	if (phy->type == BCM43xx_PHYTYPE_G) {
		if (phy->rev >= 3)
			bcm43xx_phy_write(dev, BCM43xx_PHY_BASE(0x2E), 0xC078);
		else
			bcm43xx_phy_write(dev, BCM43xx_PHY_BASE(0x2E), 0x8078);
		if (phy->rev >= 2)
			bcm43xx_phy_write(dev, BCM43xx_PHY_BASE(0x2F), 0x0202);
		else
			bcm43xx_phy_write(dev, BCM43xx_PHY_BASE(0x2F), 0x0101);
	}
	bcm43xx_write16(dev, 0x3F4, sav->reg_3F4);
	bcm43xx_phy_write(dev, BCM43xx_PHY_PGACTL, sav->phy_pgactl);
	bcm43xx_phy_write(dev, BCM43xx_PHY_BASE(0x2A), sav->phy_base_2A);
	bcm43xx_phy_write(dev, BCM43xx_PHY_SYNCCTL, sav->phy_syncctl);
	bcm43xx_phy_write(dev, BCM43xx_PHY_DACCTL, sav->phy_dacctl);
	bcm43xx_radio_write16(dev, 0x43, sav->radio_43);
	bcm43xx_radio_write16(dev, 0x7A, sav->radio_7A);
	if (!has_tx_magnification(phy)) {
		tmp = sav->radio_52;
		bcm43xx_radio_write16(dev, 0x52,
				      (bcm43xx_radio_read16(dev, 0x52)
				       & 0xFF0F) | tmp);
	}
	bcm43xx_write16(dev, 0x3E2, sav->reg_3E2);
	if (phy->type == BCM43xx_PHYTYPE_B &&
	    phy->radio_ver == 0x2050 &&
	    phy->radio_rev <= 5) {
		bcm43xx_phy_write(dev, BCM43xx_PHY_BASE(0x30), sav->phy_base_30);
		bcm43xx_phy_write(dev, BCM43xx_PHY_BASE(0x06), sav->phy_base_06);
	}
	if (phy->rev >= 2) {
		bcm43xx_phy_write(dev, BCM43xx_PHY_ANALOGOVER, sav->phy_analogover);
		bcm43xx_phy_write(dev, BCM43xx_PHY_ANALOGOVERVAL, sav->phy_analogoverval);
		bcm43xx_phy_write(dev, BCM43xx_PHY_CLASSCTL, sav->phy_classctl);
		bcm43xx_phy_write(dev, BCM43xx_PHY_RFOVER, sav->phy_rfover);
		bcm43xx_phy_write(dev, BCM43xx_PHY_RFOVERVAL, sav->phy_rfoverval);
		bcm43xx_phy_write(dev, BCM43xx_PHY_BASE(0x3E), sav->phy_base_3E);
		bcm43xx_phy_write(dev, BCM43xx_PHY_CRS0, sav->phy_crs0);
	}
	if (has_hardware_pctl(phy)) {
		tmp = (sav->phy_lo_mask & 0xBFFF);
		bcm43xx_phy_write(dev, BCM43xx_PHY_LO_MASK, tmp);
		bcm43xx_phy_write(dev, BCM43xx_PHY_EXTG(0x01), sav->phy_extg_01);
		bcm43xx_phy_write(dev, BCM43xx_PHY_DACCTL, sav->phy_dacctl_hwpctl);
		bcm43xx_phy_write(dev, BCM43xx_PHY_BASE(0x14), sav->phy_base_14);
		bcm43xx_phy_write(dev, BCM43xx_PHY_HPWR_TSSICTL, sav->phy_hpwr_tssictl);
	}
	bcm43xx_radio_selectchannel(dev, sav->old_channel, 1);
}

struct bcm43xx_lo_g_statemachine {
	int current_state;
	int nr_measured;
	int state_val_multiplier;
	u16 lowest_feedth;
	struct bcm43xx_loctl min_loctl;
};

/* Loop over each possible value in this state. */
static int lo_probe_possible_loctls(struct bcm43xx_wldev *dev,
				    struct bcm43xx_loctl *probe_loctl,
				    struct bcm43xx_lo_g_statemachine *d)
{
	struct bcm43xx_phy *phy = &dev->phy;
	struct bcm43xx_txpower_lo_control *lo = phy->lo_control;
	struct bcm43xx_loctl test_loctl;
	struct bcm43xx_loctl orig_loctl;
	struct bcm43xx_loctl prev_loctl = {
		.i = -100,
		.q = -100,
	};
	int i;
	int begin, end;
	int found_lower = 0;
	u16 feedth;

	static const struct bcm43xx_loctl modifiers[] = {
		{ .i =  1,  .q =  1, },
		{ .i =  1,  .q =  0, },
		{ .i =  1,  .q = -1, },
		{ .i =  0,  .q = -1, },
		{ .i = -1,  .q = -1, },
		{ .i = -1,  .q =  0, },
		{ .i = -1,  .q =  1, },
		{ .i =  0,  .q =  1, },
	};

	if (d->current_state == 0) {
		begin = 1;
		end = 8;
	} else if (d->current_state % 2 == 0) {
		begin = d->current_state - 1;
		end = d->current_state + 1;
	} else {
		begin = d->current_state - 2;
		end = d->current_state + 2;
	}
	if (begin < 1)
		begin += 8;
	if (end > 8)
		end -= 8;

	memcpy(&orig_loctl, probe_loctl, sizeof(struct bcm43xx_loctl));
	i = begin;
	d->current_state = i;
	while (1) {
		assert(i >= 1 && i <= 8);
		memcpy(&test_loctl, &orig_loctl, sizeof(struct bcm43xx_loctl));
		test_loctl.i += modifiers[i - 1].i * d->state_val_multiplier;
		test_loctl.q += modifiers[i - 1].q * d->state_val_multiplier;
		if ((test_loctl.i != prev_loctl.i ||
		     test_loctl.q != prev_loctl.q) &&
		    (abs(test_loctl.i) <= 16 &&
		     abs(test_loctl.q) <= 16)) {
			bcm43xx_lo_write(dev, &test_loctl);
			feedth = lo_measure_feedthrough(dev, phy->lna_gain,
							phy->pga_gain,
							phy->trsw_rx_gain);
			if (feedth < d->lowest_feedth) {
				memcpy(probe_loctl, &test_loctl, sizeof(struct bcm43xx_loctl));
				found_lower = 1;
				d->lowest_feedth = feedth;
				if ((d->nr_measured < 2) &&
				    (!has_loopback_gain(phy) || lo->rebuild))
					break;
			}
		}
		memcpy(&prev_loctl, &test_loctl, sizeof(prev_loctl));
		if (i == end)
			break;
		if (i == 8)
			i = 1;
		else
			i++;
		d->current_state = i;
	}

	return found_lower;
}

static void lo_probe_loctls_statemachine(struct bcm43xx_wldev *dev,
					 struct bcm43xx_loctl *loctl,
					 int *max_rx_gain)
{
	struct bcm43xx_phy *phy = &dev->phy;
	struct bcm43xx_txpower_lo_control *lo = phy->lo_control;
	struct bcm43xx_lo_g_statemachine d;
	u16 feedth;
	int found_lower;
	struct bcm43xx_loctl probe_loctl;
	int max_repeat = 1, repeat_cnt = 0;

	d.nr_measured = 0;
	d.state_val_multiplier = 1;
	if (has_loopback_gain(phy) && !lo->rebuild)
		d.state_val_multiplier = 3;

	memcpy(&d.min_loctl, loctl, sizeof(struct bcm43xx_loctl));
	if (has_loopback_gain(phy) && lo->rebuild)
		max_repeat = 4;
	do {
		bcm43xx_lo_write(dev, &d.min_loctl);
		feedth = lo_measure_feedthrough(dev, phy->lna_gain,
						phy->pga_gain,
						phy->trsw_rx_gain);
		if (!lo->rebuild && feedth < 0x258) {
			if (feedth >= 0x12C)
				*max_rx_gain += 6;
			else
				*max_rx_gain += 3;
			feedth = lo_measure_feedthrough(dev, phy->lna_gain,
							phy->pga_gain,
							phy->trsw_rx_gain);
		}
		d.lowest_feedth = feedth;

		d.current_state = 0;
		do {
			assert(d.current_state >= 0 && d.current_state <= 8);
			memcpy(&probe_loctl, &d.min_loctl, sizeof(struct bcm43xx_loctl));
			found_lower = lo_probe_possible_loctls(dev, &probe_loctl, &d);
			if (!found_lower)
				break;
			if ((probe_loctl.i == d.min_loctl.i) &&
			    (probe_loctl.q == d.min_loctl.q))
				break;
			memcpy(&d.min_loctl, &probe_loctl, sizeof(struct bcm43xx_loctl));
			d.nr_measured++;
		} while (d.nr_measured < 24);
		memcpy(loctl, &d.min_loctl, sizeof(struct bcm43xx_loctl));

		if (has_loopback_gain(phy)) {
			if (d.lowest_feedth > 0x1194)
				*max_rx_gain -= 6;
			else if (d.lowest_feedth < 0x5DC)
				*max_rx_gain += 3;
			if (repeat_cnt == 0) {
				if (d.lowest_feedth <= 0x5DC) {
					d.state_val_multiplier = 1;
					repeat_cnt++;
				} else
					d.state_val_multiplier = 2;
			} else if (repeat_cnt == 2)
				d.state_val_multiplier = 1;
		}
		lo_measure_gain_values(dev, *max_rx_gain, has_loopback_gain(phy));
	} while (++repeat_cnt < max_repeat);
}

static void lo_measure(struct bcm43xx_wldev *dev)
{
	struct bcm43xx_phy *phy = &dev->phy;
	struct bcm43xx_txpower_lo_control *lo = phy->lo_control;
	struct bcm43xx_loctl loctl = {
		.i	= 0,
		.q	= 0,
	};
	struct bcm43xx_loctl *ploctl;
	int max_rx_gain;
	int rfidx, bbidx;

	/* Values from the "TXCTL Register and Value Table" */
	u16 txctl_reg;
	u16 txctl_value;
	u16 pad_mix_gain;

	txctl_reg = lo_txctl_register_table(dev, &txctl_value, &pad_mix_gain);

	for (rfidx = 0; rfidx < lo->rfatt_list.len; rfidx++) {

		bcm43xx_radio_write16(dev, 0x43,
				      (bcm43xx_radio_read16(dev, 0x43)
				       & 0xFFF0) | lo->rfatt_list.list[rfidx].att);
		bcm43xx_radio_write16(dev, txctl_reg,
				      (bcm43xx_radio_read16(dev, txctl_reg)
				       & ~txctl_value)
				      | (lo->rfatt_list.list[rfidx].with_padmix ? txctl_value : 0));

		for (bbidx = 0; bbidx < lo->bbatt_list.len; bbidx++) {
			if (lo->rebuild) {
				ploctl = bcm43xx_get_lo_g_ctl_nopadmix(dev,
							&lo->rfatt_list.list[rfidx],
							&lo->bbatt_list.list[bbidx]);
			} else {
				ploctl = bcm43xx_get_lo_g_ctl(dev, &lo->rfatt_list.list[rfidx],
							   &lo->bbatt_list.list[bbidx]);
				if (!ploctl->used)
					continue;
			}
			memcpy(&loctl, ploctl, sizeof(loctl));

			max_rx_gain = lo->rfatt_list.list[rfidx].att * 2;
			max_rx_gain += lo->bbatt_list.list[bbidx].att / 2;
			if (lo->rfatt_list.list[rfidx].with_padmix)
				max_rx_gain -= pad_mix_gain;
			if (has_loopback_gain(phy))
				max_rx_gain += phy->max_lb_gain;
			lo_measure_gain_values(dev, max_rx_gain, has_loopback_gain(phy));

			bcm43xx_phy_set_baseband_attenuation(dev, lo->bbatt_list.list[bbidx].att);
			lo_probe_loctls_statemachine(dev, &loctl, &max_rx_gain);
			if (phy->type == BCM43xx_PHYTYPE_B) {
				loctl.i++;
				loctl.q++;
			}
			memcpy(ploctl, &loctl, sizeof(loctl));
		}
	}
}

#if BCM43xx_DEBUG
static int do_validate_loctl(struct bcm43xx_wldev *dev,
			     struct bcm43xx_loctl *control)
{
	const int is_initializing = (bcm43xx_status(dev) == BCM43xx_STAT_INITIALIZING);

	if (unlikely(abs(control->i) > 16 ||
		     abs(control->q) > 16 ||
		     (is_initializing && control->used))) {
		printk(KERN_ERR PFX "ERROR: LO control pair validation failed "
				    "(first: %d, second: %d, used %u)\n",
		       control->i, control->q, control->used);
	}
	return 0;
}
static void validate_all_loctls(struct bcm43xx_wldev *dev)
{
	bcm43xx_call_for_each_loctl(dev, do_validate_loctl);
}
#else /* BCM43xx_DEBUG */
static inline void validate_all_loctls(struct bcm43xx_wldev *dev) { }
#endif /* BCM43xx_DEBUG */

void bcm43xx_lo_g_measure(struct bcm43xx_wldev *dev)
{
	struct bcm43xx_phy *phy = &dev->phy;
	struct lo_g_saved_values sav;

	assert(phy->type == BCM43xx_PHYTYPE_B ||
	       phy->type == BCM43xx_PHYTYPE_G);

	sav.old_channel = phy->channel;
	lo_measure_setup(dev, &sav);
	lo_measure(dev);
	lo_measure_restore(dev, &sav);

	validate_all_loctls(dev);

	phy->lo_control->lo_measured = 1;
	phy->lo_control->rebuild = 0;
}

void bcm43xx_lo_g_adjust(struct bcm43xx_wldev *dev)
{
	bcm43xx_lo_write(dev, bcm43xx_lo_g_ctl_current(dev));
}

static inline void fixup_rfatt_for_txctl1(struct bcm43xx_rfatt *rf,
					  u16 txctl1)
{
	if ((rf->att < 5) && (txctl1 & 0x0001))
		rf->att = 4;
}

void bcm43xx_lo_g_adjust_to(struct bcm43xx_wldev *dev,
			  u16 rfatt, u16 bbatt, u16 txctl1)
{
	struct bcm43xx_rfatt rf;
	struct bcm43xx_bbatt bb;
	struct bcm43xx_loctl *loctl;

	memset(&rf, 0, sizeof(rf));
	memset(&bb, 0, sizeof(bb));
	rf.att = rfatt;
	bb.att = bbatt;
	fixup_rfatt_for_txctl1(&rf, txctl1);
	loctl = bcm43xx_get_lo_g_ctl(dev, &rf, &bb);
	bcm43xx_lo_write(dev, loctl);
}

struct bcm43xx_loctl * bcm43xx_lo_g_ctl_current(struct bcm43xx_wldev *dev)
{
	struct bcm43xx_phy *phy = &dev->phy;
	struct bcm43xx_txpower_lo_control *lo = phy->lo_control;
	struct bcm43xx_rfatt rf;

	memcpy(&rf, &lo->rfatt, sizeof(rf));
	fixup_rfatt_for_txctl1(&rf, phy->txctl1);

	return bcm43xx_get_lo_g_ctl(dev, &rf, &lo->bbatt);
}

static int do_mark_unused(struct bcm43xx_wldev *dev,
			  struct bcm43xx_loctl *control)
{
	control->used = 0;
	return 0;
}

void bcm43xx_lo_g_ctl_mark_all_unused(struct bcm43xx_wldev *dev)
{
	struct bcm43xx_phy *phy = &dev->phy;
	struct bcm43xx_txpower_lo_control *lo = phy->lo_control;

	bcm43xx_call_for_each_loctl(dev, do_mark_unused);
	lo->rebuild = 1;
}

void bcm43xx_lo_g_ctl_mark_cur_used(struct bcm43xx_wldev *dev)
{
	bcm43xx_lo_g_ctl_current(dev)->used = 1;
}