-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathVoodooIntel3945.cpp
2372 lines (1957 loc) · 67.5 KB
/
VoodooIntel3945.cpp
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
/*
* VoodooIntel3945.cpp
* VoodooIntel3945
*
* Created by Prashant Vaibhav on 31/08/09.
* Copyright 2009 Prashant Vaibhav. All rights reserved.
*
*/
#include "VoodooIntel3945.h"
#include "if_wpireg.h"
#include "Firmware.h"
#include <sys/sysctl.h>
#pragma mark Defines
#define DPRINTF(x) { if (wpi_debug != 0) { printf("VoodooIntel3945: "); printf x; } }
#define DPRINTFN(n, x) { if (wpi_debug & n) { printf("VoodooIntel3945: "); printf x; } }
#define WPI_DEBUG_SET (wpi_debug != 0)
#define WPI_READ(a) (*((uint32_t*) (m_Registers + (a))))
#define WPI_WRITE(a, d) (*((uint32_t*) (m_Registers + (a)))) = (d)
#define abs(x) (((x) < 0) ? (0-(x)) : (x))
enum {
WPI_DEBUG_UNUSED = 0x00000001, /* Unused */
WPI_DEBUG_HW = 0x00000002, /* Stage 1 (eeprom) debugging */
WPI_DEBUG_TX = 0x00000004, /* Stage 2 TX intrp debugging*/
WPI_DEBUG_RX = 0x00000008, /* Stage 2 RX intrp debugging */
WPI_DEBUG_CMD = 0x00000010, /* Stage 2 CMD intrp debugging*/
WPI_DEBUG_FIRMWARE = 0x00000020, /* firmware(9) loading debug */
WPI_DEBUG_DMA = 0x00000040, /* DMA (de)allocations/syncs */
WPI_DEBUG_SCANNING = 0x00000080, /* Stage 2 Scanning debugging */
WPI_DEBUG_NOTIFY = 0x00000100, /* State 2 Noftif intr debug */
WPI_DEBUG_TEMP = 0x00000200, /* TXPower/Temp Calibration */
WPI_DEBUG_OPS = 0x00000400, /* wpi_ops taskq debug */
WPI_DEBUG_WATCHDOG = 0x00000800, /* Watch dog debug */
WPI_DEBUG_ANY = 0xffffffff
};
static unsigned int wpi_debug = 0;
SYSCTL_UINT(_debug, OID_AUTO, wpi, CTLFLAG_RW, &wpi_debug, 0, "VoodooIntel3945 wpi driver debug output level");
#define org_voodoo_wireless_debug 3
static const uint8_t wpi_ridx_to_plcp[] = {
/* OFDM: IEEE Std 802.11a-1999, pp. 14 Table 80 */
/* R1-R4 */
0xd, 0xf, 0x5, 0x7, 0x9, 0xb, 0x1, 0x3,
/* CCK: device-dependent */
10, 20, 55, 110
};
static const uint8_t wpi_ridx_to_rate[] = {
12, 18, 24, 36, 48, 72, 96, 108, /* OFDM */
2, 4, 11, 22 /*CCK */
};
static const uint8_t broadcast_bssid[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
/* First of all, implement RTTI methods required by I/OKit -- */
OSDefineMetaClassAndStructors(VoodooIntel3945, VoodooWirelessDevice)
#pragma mark -
#pragma mark Subclassed functions
IOReturn MyClass::allocateResources
( IOService* provider )
{
int i;
/* First of all setup debug logging control knob */
sysctl_register_oid(&sysctl__debug_wpi);
DBG(dbgInfo, "Voodoo Intel 3945 driver version 0.4 starting up...\n");
/* Set up our PCI nub */
m_PciDevice = OSDynamicCast(IOPCIDevice, provider);
if (!m_PciDevice) {
DBG(dbgFatal, "PCI device cast failed.\n");
goto fail;
}
m_PciDevice->retain();
m_PciDevice->open(this);
/* Request D0 power state */
if (m_PciDevice->requestPowerDomainState(kIOPMPowerOn,
(IOPowerConnection *) getParentEntry(gIOPowerPlane),
IOPMLowestState) != IOPMNoErr)
{
DBG(dbgFatal, "Could not put PCI device in power state D0.\n");
goto fail;
}
m_PciDevice->configWrite8(0x41, 0); /* This comes from FreeBSD driver */
m_PciDevice->setBusMasterEnable(true); /* The adapter uses bus mastering, so turn it on */
/* Map the device memory at BAR0 and get a pointer to it */
m_DeviceMap = m_PciDevice->mapDeviceMemoryWithRegister(kIOPCIConfigBaseAddress0);
if (!m_DeviceMap) {
DBG(dbgFatal, "PCI device memory could not be mapped.\n");
goto fail;
}
DBG(dbgInfo, "PCI device memory at VMaddr 0x%x, size %u\n",
m_DeviceMap->getVirtualAddress(), m_DeviceMap->getSize());
m_Registers = reinterpret_cast<uint8_t*> (m_DeviceMap->getVirtualAddress());
/* Allocate hardware interrupt source */
m_WorkLoop = OSDynamicCast(IO80211WorkLoop, getWorkLoop());
m_WorkLoop->retain();
m_InterruptSrc = IOInterruptEventSource::
interruptEventSource(this,
OSMemberFunctionCast(IOInterruptEventAction, this,
&MyClass::interruptOccurred),
m_PciDevice);
if (!m_InterruptSrc) {
DBG(dbgFatal, "Could not create an interrupt event source.\n");
goto fail;
}
if (m_WorkLoop->addEventSource(m_InterruptSrc) != kIOReturnSuccess) {
DBG(dbgFatal, "Could not add interrupt event source to the workloop.\n");
goto fail;
}
m_InterruptSrc->enable(); // Enable immediately in case the HW interrupt is shared
DBG(dbgInfo, "Interrupt was enabled.\n");
/* Allocate timers */
m_Timer = IOTimerEventSource::
timerEventSource(this, OSMemberFunctionCast(IOTimerEventSource::Action, this, &MyClass::timerOccurred));
if (!m_Timer) {
DBG(dbgFatal, "Could not create a TIMER event source.\n");
goto fail;
}
if (m_WorkLoop->addEventSource(m_Timer) != kIOReturnSuccess) {
DBG(dbgFatal, "Could not add timer event source to the workloop.\n");
goto fail;
}
m_Timer->setTimeoutMS(10000);
m_Timer->enable();
/* Allocate firmware memory */
m_Firmware = IOBufferMemoryDescriptor::
inTaskWithPhysicalMask(kernel_task,
kIODirectionInOut | kIOMemoryPhysicallyContiguous,
WPI_FW_MAIN_TEXT_MAXSZ + WPI_FW_MAIN_DATA_MAXSZ,
0x00000000FFFFFFFFull /* 32bit, 1 byte alignment */);
if (!m_Firmware) {
DBG(dbgFatal, "Could not allocate firmware memory.\n");
goto fail;
}
m_Firmware->prepare();
/* Initialize the mbuf cursor for tx */
m_MbufCursor = IOMbufLittleMemoryCursor::withSpecification(WPI_MAX_SEG_LEN, WPI_MAX_SCATTER - 1);
if (!m_MbufCursor) {
DBG(dbgFatal, "Could not allocate mbuf memory cursor\n");
goto fail;
}
/* Put the adapter in an initial state */
if (resetAdapter() != kIOReturnSuccess) {
DBG(dbgFatal, "Could not reset adapter.\n");
goto fail;
}
/* Print hardware revision info */
memLock();
{
uint32_t tmp = memRead(WPI_MEM_PCIDEV);
DBG(dbgInfo, "Hardware Revision (0x%X)\n", tmp);
}
memUnlock();
/*
* ------------------------- Allocate Rings ---------------------
*/
/* Allocate shared page */
m_SharedPage = IOBufferMemoryDescriptor::
inTaskWithPhysicalMask(kernel_task,
kIOMemoryPhysicallyContiguous,
sizeof(wpi_shared),
0x00000000fffff000ull);
if (!m_SharedPage) {
DBG(dbgFatal, "Could not allocate shared page.\n");
goto fail;
}
m_SharedPage->prepare();
m_SharedPagePtr = (wpi_shared*) m_SharedPage->getBytesNoCopy();
/* Allocate rx ring */
m_RxRingMemory = allocDmaMemory(WPI_RX_RING_COUNT * sizeof(uint32_t),
WPI_RING_DMA_ALIGN,
(void**) &m_RxRing.rx_pkt_ptr,
&m_RxRing.physAdd);
if (!m_RxRingMemory) {
DBG(dbgFatal, "Could not allocate Rx ring memory page.\n");
goto fail;
}
for (i = 0; i < WPI_RX_RING_COUNT; i++) {
m_RxRing.mbufs[i] = 0; // initilize to 0 so we can check if allocatePacket failed
m_RxRing.mbufs[i] = allocatePacket(MCLBYTES); // allocate a packet into which HW will write rx'd packet
if (m_RxRing.mbufs[i] == 0) {
DBG(dbgFatal, "Could not allocate packet no. %d during Rx ring allocation!\n", i);
goto fail;
}
m_RxRing.rx_pkt_ptr[i] = mbuf_data_to_physical(mbuf_data(m_RxRing.mbufs[i])); // tell HW where it is
}
DBG(dbgInfo, "Rx ring allocated successfully, PHaddr = 0x%x\n", m_RxRing.physAdd);
/* Allocate Tx and cmd rings (rings 0-3 and ring 4) */
TxRing* ring; int ring_item_count;
for (i = 0; i <= 4; i++) {
if (i < 4) {
ring = &m_TxRing[i];
ring_item_count = WPI_TX_RING_COUNT;
} else {
ring = &m_CmdRing;
ring_item_count = WPI_CMD_RING_COUNT;
}
/*
* Here we have to use a dirty hack to make sure it is 16kB aligned. OS X seems to have trouble
* aligning to such large boundary, so we allocate unaligned physcont memory, and allocate extra
* 16 KB. Then we manually align it by zeroing out the lower bits.
*/
ring->descMemory = 0;
ring->descMemory = allocDmaMemory(ring_item_count * sizeof(wpi_tx_desc),
WPI_RING_DMA_ALIGN,
(void**) &ring->descriptors,
&m_SharedPagePtr->txbase[i]);
if (ring->descMemory == 0) {
DBG(dbgFatal, "Couldn't allocate desc memory for Tx ring %u\n", i);
goto fail;
}
DBG(dbgInfo, "Tx ring %u, VMaddr = 0x%x, PHaddr = 0x%x\n",
i, ring->descriptors, m_SharedPagePtr->txbase[i]);
ring->cmdMemory = 0;
ring->cmdMemory = allocDmaMemory(ring_item_count * sizeof(wpi_tx_cmd),
WPI_RING_DMA_ALIGN,
(void**) &ring->cmdSlots,
&ring->cmdPhysAdd);
if (ring->cmdMemory == 0) {
DBG(dbgFatal, "Couldn't allocate cmd memory for Tx ring %u\n", i);
goto fail;
}
ring->cmdMemory->prepare();
ring->cmdSlots = (wpi_tx_cmd*) ring->cmdMemory->getBytesNoCopy();
ring->cmdPhysAdd = ring->cmdMemory->getPhysicalAddress();
/* Initialize mbuf array to all zeros */
for (int mi = 0; mi < ring_item_count; mi++)
ring->mbufs[mi] = 0;
DBG(dbgInfo, "Tx cmd %u, VMaddr = 0x%x, PHaddr = 0x%x\n", i, ring->cmdSlots, ring->cmdPhysAdd);
ring->count = ring_item_count;
ring->qid = i;
ring->current = 0;
ring->queued = 0;
}
return kIOReturnSuccess;
fail:
return kIOReturnError;
}
IOReturn MyClass::turnPowerOn( )
{
uint32_t tmp;
int ntries, qid;
turnPowerOff();
resetAdapter();
memLock();
memWrite(WPI_MEM_CLOCK1, 0xa00);
IODelay(20);
tmp = memRead(WPI_MEM_PCIDEV);
memWrite(WPI_MEM_PCIDEV, tmp | 0x800);
memUnlock();
powerUp();
configureHardware();
/* init Rx ring */
memLock();
WPI_WRITE(WPI_RX_BASE, m_RxRing.physAdd);
WPI_WRITE(WPI_RX_RIDX_PTR, m_SharedPage->getPhysicalAddress() + offsetof(wpi_shared, next));
WPI_WRITE(WPI_RX_WIDX, (WPI_RX_RING_COUNT - 1) & ~7);
WPI_WRITE(WPI_RX_CONFIG, 0xa9601010);
memUnlock();
/* init Tx rings */
memLock();
memWrite(WPI_MEM_MODE, 2); /* bypass mode */
memWrite(WPI_MEM_RA, 1); /* enable RA0 */
memWrite(WPI_MEM_TXCFG, 0x3f); /* enable all 6 Tx rings */
memWrite(WPI_MEM_BYPASS1, 0x10000);
memWrite(WPI_MEM_BYPASS2, 0x30002);
memWrite(WPI_MEM_MAGIC4, 4);
memWrite(WPI_MEM_MAGIC5, 5);
WPI_WRITE(WPI_TX_BASE_PTR, m_SharedPage->getPhysicalAddress());
WPI_WRITE(WPI_MSG_CONFIG, 0xffff05a5);
for (qid = 0; qid < 6; qid++) {
WPI_WRITE(WPI_TX_CTL(qid), 0);
WPI_WRITE(WPI_TX_BASE(qid), 0);
WPI_WRITE(WPI_TX_CONFIG(qid), 0x80200008);
}
memUnlock();
/* clear "radio off" and "disable command" bits (reversed logic) */
WPI_WRITE(WPI_UCODE_CLR, WPI_RADIO_OFF);
WPI_WRITE(WPI_UCODE_CLR, WPI_DISABLE_CMD);
m_Flags &= ~(WPI_FLAG_HW_RADIO_OFF | WPI_FLAG_SCANNING);
/* clear any pending interrupts */
WPI_WRITE(WPI_INTR, 0xffffffff);
/* enable interrupts */
WPI_WRITE(WPI_MASK, WPI_INTR_MASK);
WPI_WRITE(WPI_UCODE_CLR, WPI_RADIO_OFF);
WPI_WRITE(WPI_UCODE_CLR, WPI_RADIO_OFF);
if (uploadFirmware() != kIOReturnSuccess) {
DBG(dbgFatal, "A problem occurred loading the firmware to the driver\n");
return kIOReturnError;
}
/* At this point the firmware is up and running. If the hardware
* RF switch is turned off thermal calibration will fail, though
* the card is still happy to continue to accept commands, catch
* this case and fail if radio is off.
*/
memLock();
tmp = memRead(WPI_MEM_HW_RADIO_OFF);
memUnlock();
if (!(tmp & 0x1)) {
m_Flags |= WPI_FLAG_HW_RADIO_OFF;
DBG(dbgFatal,"Radio Transmitter is switched off, failing power on\n");
return kIOReturnError;
}
/* wait for thermal sensors to calibrate */
for (ntries = 0; ntries < 1000; ntries++) {
if ((m_Temperature = (int)WPI_READ(WPI_TEMPERATURE)) != 0)
break;
IODelay(10);
}
if (ntries == 1000) {
DBG(dbgFatal, "Timeout waiting for thermal sensors calibration\n");
return kIOReturnTimeout;
}
DPRINTFN(WPI_DEBUG_TEMP,("Temperature %d\n", m_Temperature));
/* Set the default power-up channel before configuring */
m_CurrentChannel.number = 11; // XXX
m_CurrentChannel.flags = IEEE::Channel::default11BGChannelFlags;
if (configure() != kIOReturnSuccess) {
DBG(dbgFatal, "Device config failed\n");
return kIOReturnError;
}
m_AssocState = staInit;
return kIOReturnSuccess;
}
IOReturn MyClass::turnPowerOff( )
{
uint32_t tmp;
int ac;
m_Flags &= ~(WPI_FLAG_HW_RADIO_OFF | WPI_FLAG_SCANNING);
// TODO: watchdog and calibration timer reset
/* disable interrupts */
WPI_WRITE(WPI_MASK, 0);
WPI_WRITE(WPI_INTR, WPI_INTR_MASK);
WPI_WRITE(WPI_INTR_STATUS, 0xff);
WPI_WRITE(WPI_INTR_STATUS, 0x00070000);
memLock();
memWrite(WPI_MEM_MODE, 0);
memUnlock();
/* reset all Tx rings */
for (ac = 0; ac < 4; ac++)
resetTxRing(&m_TxRing[ac]);
resetTxRing(&m_CmdRing);
/* reset Rx ring */
resetRxRing(&m_RxRing);
memLock();
memWrite(WPI_MEM_CLOCK2, 0x200);
memUnlock();
IODelay(5);
stopMaster();
tmp = WPI_READ(WPI_RESET);
WPI_WRITE(WPI_RESET, tmp | WPI_SW_RESET);
m_Flags &= ~(WPI_FLAG_BUSY);
return kIOReturnSuccess;
}
void MyClass::freeResources
( IOService* provider )
{
int i;
sysctl_unregister_oid(&sysctl__debug_wpi);
if (m_Timer) m_Timer->disable();
RELEASE(m_Timer);
if (m_Firmware) m_Firmware->complete();
RELEASE(m_Firmware);
if (m_RxRingMemory) m_RxRingMemory->complete();
RELEASE(m_RxRingMemory);
/* Free Rx mbufs if any */
for (i = 0; i < WPI_RX_RING_COUNT; i++) {
if (m_RxRing.mbufs[i] != 0) {
freePacket(m_RxRing.mbufs[i]);
m_RxRing.mbufs[i] = 0;
}
}
/* Free tx and cmd rings */
TxRing* ring;
for (i = 0; i <= 4; i++) {
if (i < 4)
ring = &m_TxRing[i];
else
ring = &m_CmdRing;
if (ring->descMemory) {
ring->descMemory->complete();
RELEASE(ring->descMemory);
}
if (ring->cmdMemory) {
ring->cmdMemory->complete();
RELEASE(ring->cmdMemory);
}
}
if (m_InterruptSrc) m_InterruptSrc->disable();
RELEASE(m_InterruptSrc);
if (m_DeviceMap) m_DeviceMap->unmap();
RELEASE(m_DeviceMap);
if (m_PciDevice) m_PciDevice->close(this);
RELEASE(m_PciDevice);
return;
}
IOReturn MyClass::startScan
( const ScanParameters* params, const IEEE::ChannelList* channels )
{
/*
* Tonight we dine in hell
*/
wpi_scan_hdr* hdr;
wpi_scan_chan* chan;
uint8_t cmd_data[360]; // buffer to hold our scan command
uint8_t* frm;
int i;
bool directed = false;
DPRINTFN(WPI_DEBUG_SCANNING, ("SCAN: Starting a scan...\n"));
IEEE::ManagementFrameHeader* wh;
IEEE::Channel::Flags chflags;
hdr = (wpi_scan_hdr*) cmd_data;
bzero(hdr, sizeof(wpi_scan_hdr));
/*
* Move to the next channel if no packets are received within 5 msecs
* after sending the probe request (this helps to reduce the duration
* of active scans).
*/
hdr->quiet = 10; // was 5
hdr->threshold = 1;
DPRINTFN(WPI_DEBUG_SCANNING, ("SCAN: Dwell time %u, rest time %u\n", params->dwellTime, params->restTime));
switch (params->scanPhyMode) {
case IEEE::phyModeAuto:
case IEEE::dot11B:
case IEEE::dot11G:
chflags = IEEE::Channel::band2GHz;
break;
case IEEE::dot11A:
chflags = IEEE::Channel::band5GHz;
break;
default:
DPRINTFN(WPI_DEBUG_SCANNING, ("SCAN: phy mode %d not supported, defaulting to bg\n",
(int)params->scanPhyMode));
chflags = IEEE::Channel::band2GHz;
};
if (chflags & IEEE::Channel::band5GHz) {
DPRINTFN(WPI_DEBUG_SCANNING, ("SCAN: 5 GHz mode\n"));
hdr->tx.rate = wpi_ridx_to_plcp[WPI_OFDM6]; /* send probe requests at 6Mbps */
hdr->promotion = 1; /* Enable crc checking */
} else {
DPRINTFN(WPI_DEBUG_SCANNING, ("SCAN: 2.4 GHz mode\n"));
hdr->flags = WPI_CONFIG_24GHZ | WPI_CONFIG_AUTO;
hdr->tx.rate = wpi_ridx_to_plcp[WPI_CCK1]; /* send probe requests at 1Mbps */
}
hdr->tx.id = WPI_ID_BROADCAST;
hdr->tx.lifetime = WPI_LIFETIME_INFINITE;
hdr->tx.flags = WPI_TX_AUTO_SEQ;
bzero(hdr->scan_essids, sizeof(hdr->scan_essids));
if (params->ssid)
if (params->ssid->getLength() > 0)
directed = true;
if (directed) {
DPRINTFN(WPI_DEBUG_SCANNING, ("SCAN: Directed for SSID '%s' of length %u\n",
params->ssid->getBytesNoCopy(), params->ssid->getLength()));
hdr->scan_essids[0].id = IEEE::IE::ieSSID;
hdr->scan_essids[0].esslen = MIN(params->ssid->getLength(), 32);
memcpy(hdr->scan_essids[0].essid, params->ssid->getBytesNoCopy(), hdr->scan_essids[0].esslen);
} else {
DPRINTFN(WPI_DEBUG_SCANNING, ( "SCAN: Broadcast mode\n"));
}
/*
* Build a probe request frame.
*/
wh = (IEEE::ManagementFrameHeader*) &hdr->scan_essids[4]; // i.e. just after the last essid
bzero(wh, sizeof(*wh));
wh->hdr.protocolVersion = 0;
wh->hdr.type = IEEE::WiFiFrameHeader::ManagementFrame;
wh->hdr.subtype = IEEE::WiFiFrameHeader::ProbeRequest;
bcopy(broadcast_bssid, wh->da, 6);
bcopy(broadcast_bssid, wh->bssid, 6);
bcopy(&m_MacAddress, wh->sa, 6);
frm = (uint8_t*) (wh + 1);
/* add essid IE, the hardware will fill this in for us */
*frm++ = IEEE::IE::ieSSID;
*frm++ = 0;
/* add supported rates IE */
*frm++ = IEEE::IE::ieSupportedRates;
*frm++ = 8; // max size of this IE is 8
/* We'll add all the CCK rates, and some OFDM rates. Remaining will be added in extra supported rates IE.
* As for which ones are "basic" .. I think all of them could be, but I don't know. */
*frm++ = (uint8_t) (IEEE::rate1Mbps | IEEE::rateIsBasic);
*frm++ = (uint8_t) (IEEE::rate2Mbps | IEEE::rateIsBasic);
*frm++ = (uint8_t) (IEEE::rate5Mbps | IEEE::rateIsBasic);
*frm++ = (uint8_t) (IEEE::rate11Mbps | IEEE::rateIsBasic);
*frm++ = (uint8_t) (IEEE::rate18Mbps);
*frm++ = (uint8_t) (IEEE::rate24Mbps | IEEE::rateIsBasic);
*frm++ = (uint8_t) (IEEE::rate36Mbps);
*frm++ = (uint8_t) (IEEE::rate54Mbps);
/* add supported xrates IE */
*frm++ = IEEE::IE::ieExtendedSupportedRates;
*frm++ = 4; // there are 4 more OFDM rates
*frm++ = (uint8_t) (IEEE::rate6Mbps | IEEE::rateIsBasic);
*frm++ = (uint8_t) (IEEE::rate9Mbps);
*frm++ = (uint8_t) (IEEE::rate12Mbps | IEEE::rateIsBasic);
*frm++ = (uint8_t) (IEEE::rate48Mbps);
/* setup length of probe request */
hdr->tx.len = (frm - (uint8_t *)wh);
DPRINTFN(WPI_DEBUG_SCANNING, ("SCAN: Probe request frame length: %d\n", hdr->tx.len));
/*
* Construct information about the channel that we
* want to scan. The firmware expects this to be directly
* after the scan probe request
*/
chan = (wpi_scan_chan*) frm;
DPRINTFN(WPI_DEBUG_SCANNING, ("SCAN: Told to scan %u channels, frm=0x%x\n", channels->numItems, frm));
// we are limiting the scan to only those channels which fit in 360 bytes
for (i = 1; i <= 11; i++) {
hdr->nchan++;
chan->chan = i;
chan->flags = 0;
chan->gain_dsp = 0x6e; /* Default level */
if (params->scanType == ScanParameters::scanTypeActive)
{
chan->flags |= WPI_CHAN_ACTIVE;
if (directed)
chan->flags |= WPI_CHAN_DIRECT;
}
chan->active = 20;
chan->passive = 200; // was dwellTime
chan->gain_radio= 0x28;
chan++;
frm += sizeof (wpi_scan_chan);
DPRINTFN(WPI_DEBUG_SCANNING, ("SCAN: Ch %u, cmd_len %u, frm=0x%x, hdr=0x%x\n", i,
frm - (uint8_t*)hdr, frm, hdr));
}
hdr->len = frm - (uint8_t *)hdr;
DPRINTFN(WPI_DEBUG_SCANNING, ("SCAN: Sending start command with cmd_len=%u, probe_len=%u, nchan=%u\n",
hdr->len, hdr->tx.len, hdr->nchan));
IOReturn err = sendCommand(WPI_CMD_SCAN, cmd_data, hdr->len, 0);
if (err == kIOReturnSuccess)
m_Flags |= WPI_FLAG_SCANNING;
else
m_Flags &= ~(WPI_FLAG_SCANNING);
return err;
}
void MyClass::abortScan( )
{
m_Flags &= ~(WPI_FLAG_SCANNING);
return;
}
IOReturn MyClass::associate
( const AssociationParameters* params )
{
#pragma mark Association 7-step procedure
/*---------------------------------------------------------------------------------------------
In Soviet Russia, hell dines in us.
So how this works is the following:
1. Prepare the card for auth/assoc by doing some stuff
2. Send auth request frame, set state to auth_in_progress
3. Receive auth response. If success, go to 4, else send msg that auth failed
4. Set state to assoc_in_progress. Send assoc request frame
5. Receive assoc response. If success, go to 6, else send msg that assoc failed
6. Store AID from assoc response, prepare card for RUN state by doing more stuff
7. Send msg that assoc succeeded, set state to assoc_run
In this function, we will only perform steps 1 and 2.
---------------------------------------------------------------------------------------------*/
if (m_AssocState != staInit) {
DBG(dbgWarning, "Assoc status not in intial state\n");
return kIOReturnBusy;
}
/*
* Step 1
*/
wpi_node_info node;
DBG(dbgInfo, "Auth/association 7-step-gymnastic begins...\n");
/* First make a copy of the association parameters as they'll be used later */
if (m_AssocParams.ssid)
m_AssocParams.ssid->release();
bcopy(params, &m_AssocParams, sizeof(m_AssocParams));
m_AssocParams.ssid = OSData::withData(params->ssid);
/* Check if we are in .11b only mode */
bool dot11bMode = true;
for (int i = 0; i < params->supportedRates.numItems; i++)
if ( WPI_RATE_IS_OFDM(0x7f & params->supportedRates.rate[i]) ) {
dot11bMode = false;
break;
}
/* update adapter's configuration */
bcopy(¶ms->bssid, m_Config.bssid, 6);
m_Config.associd = 0;
m_Config.filter &= ~(WPI_FILTER_BSS);
m_Config.chan = params->channel.number;
if (params->channel.flags & IEEE::Channel::band2GHz)
m_Config.flags |= (WPI_CONFIG_AUTO | WPI_CONFIG_24GHZ);
if (params->channel.flags & IEEE::Channel::band5GHz) {
m_Config.cck_mask = 0;
m_Config.ofdm_mask = 0x15;
} else if (dot11bMode) {
m_Config.cck_mask = 0x03;
m_Config.ofdm_mask = 0;
} else {
/* XXX assume 802.11b/g */
m_Config.cck_mask = 0x0f;
m_Config.ofdm_mask = 0xff;
}
DPRINTF(("config chan %d flags %x cck %x ofdm %x\n",
m_Config.chan, m_Config.flags, m_Config.cck_mask, m_Config.ofdm_mask));
if (sendCommand(WPI_CMD_CONFIGURE, &m_Config, sizeof (wpi_config), 1) != kIOReturnSuccess) {
DBG(dbgWarning, "Couldn't configure during auth/assoc\n");
return kIOReturnError;
}
/* configuration has changed, set Tx power accordingly */
if (setTxPower(params->channel, 1) != kIOReturnSuccess) {
DBG(dbgWarning, "Couldn't set TX power during auth/assoc\n");
return kIOReturnError;
}
/* add default node */
bzero(&node, sizeof node);
bcopy(¶ms->bssid, node.bssid, 6);
node.id = WPI_ID_BSS;
node.rate = (params->channel.flags & IEEE::Channel::band5GHz) ? plcpSignal(12) : plcpSignal(2);
node.action = WPI_ACTION_SET_RATE;
node.antenna = WPI_ANTENNA_BOTH;
if (sendCommand(WPI_CMD_ADD_NODE, &node, sizeof node, 1) != kIOReturnSuccess) {
DBG(dbgWarning, "Couldn't add node during auth/assoc\n");
return kIOReturnError;
}
DBG(dbgWarning, "ASSOC: Step 1 done\n");
IOSleep(50); // hack
/*
* Step 2
*/
m_AssocState = staAuthTry;
AuthenticationFrame af;
bzero(&af, sizeof(af));
af.hdr.hdr.type = IEEE::WiFiFrameHeader::ManagementFrame;
af.hdr.hdr.subtype = IEEE::WiFiFrameHeader::Authentication;
bcopy(&m_MacAddress, af.hdr.sa, 6);
bcopy(broadcast_bssid, af.hdr.bssid, 6);
bcopy(¶ms->bssid, af.hdr.da, 6);
af.algorithm = 0; // open
af.sequence = 1; // request
DBG(dbgWarning, "ASSOC: Step 2 (almost) done\n");
return sendManagementFrame((uint8_t*) &af, sizeof(af));
}
IOReturn MyClass::disassociate( )
{
return kIOReturnSuccess;
}
void MyClass::getHardwareInfo
( HardwareInfo* info )
{
info->manufacturer = OSString::withCString("Intel");
info->model = OSString::withCString("PRO/Wireless 3945ABG");
info->hardwareRevision = OSString::withCString("1");
info->driverVersion = OSString::withCString("0.4");
info->firmwareVersion = OSString::withCString("15.32.2.9");
readPromData(WPI_EEPROM_MAC, &info->hardwareAddress, 6);
readPromData(WPI_EEPROM_MAC, &m_MacAddress, 6); // for our own use
if (m_SupportsA)
info->supportedModes = (IEEE::PHYModes) (IEEE::dot11B | IEEE::dot11G | IEEE::dot11A);
else
info->supportedModes = (IEEE::PHYModes) (IEEE::dot11B | IEEE::dot11G);
/*
* Read the EEPROM to get the channels we support
* Adapted from wpi_read_eeprom_channels()
*/
const wpi_chan_band* band;
wpi_eeprom_chan channels[WPI_MAX_CHAN_PER_BAND];
IEEE::Channel* c;
int chan, i, n;
info->supportedChannels.numItems = 0;
for (n = 0; n < WPI_CHAN_BANDS_COUNT; n++) {
band = &wpi_bands[n];
bzero(channels, sizeof(channels));
readPromData(band->addr, channels, band->nchan * sizeof(wpi_eeprom_chan));
for (i = 0; i < band->nchan; i++) {
if (!(channels[i].flags & WPI_EEPROM_CHAN_VALID)) {
DPRINTFN(WPI_DEBUG_HW, ("Channel Not Valid: %d, band %d\n", band->chan[i],n));
continue;
}
chan = band->chan[i];
c = &info->supportedChannels.channel[info->supportedChannels.numItems++];
if (n == 0) { /* 2GHz band */
c->number = chan;
c->flags = IEEE::Channel::default11BGChannelFlags;
} else { /* 5GHz band */
c->number = chan;
c->flags = IEEE::Channel::default11AChannelFlags;
}
if (!(channels[i].flags & WPI_EEPROM_CHAN_ACTIVE)) {
/* Active scanning not supported, turn off active scanning flag */
c->flags &= ~(IEEE::Channel::supportsActiveScanning);
}
/* save maximum allowed power for this channel */
m_MaxPower[chan] = channels[i].maxpwr;
DPRINTF(("adding chan %d flags=0x%x maxpwr=%d active=%s, offset %d\n",
chan, channels[i].flags, m_MaxPower[chan],
(c->flags & IEEE::Channel::supportsActiveScanning) ? "true" : "false",
info->supportedChannels.numItems));
}
}
DBG(dbgInfo, "Total channels supported = %u\n", info->supportedChannels.numItems);
/*
* Now read the EEPROM for Tx power groups. Adapted from wpi_read_eeprom_group().
* This data is used internally only.
*/
wpi_power_group* group;
wpi_eeprom_group rgroup;
for (n = 0; n < WPI_POWER_GROUPS_COUNT; n++) {
group = &m_PowerGroups[n];
readPromData(WPI_EEPROM_POWER_GRP + n * 32, &rgroup, sizeof rgroup);
/* save power group information */
group->chan = rgroup.chan;
group->maxpwr = rgroup.maxpwr;
/* temperature at which the samples were taken */
group->temp = (int16_t) (rgroup.temp);
DPRINTF(("power group %d: chan=%d maxpwr=%d temp=%d\n", n, group->chan, group->maxpwr, group->temp));
for (i = 0; i < WPI_SAMPLES_COUNT; i++) {
group->samples[i].index = rgroup.samples[i].index;
group->samples[i].power = rgroup.samples[i].power;
DPRINTF(("\tsample %d: index=%d power=%d\n",
i, group->samples[i].index, group->samples[i].power));
}
}
/* XXX: Are all rates "basic" ? */
info->supportedRates.numItems = 12;
info->supportedRates.rate[0] = (IEEE::DataRate) (IEEE::rate1Mbps | IEEE::rateIsBasic);
info->supportedRates.rate[1] = (IEEE::DataRate) (IEEE::rate2Mbps | IEEE::rateIsBasic);
info->supportedRates.rate[2] = (IEEE::DataRate) (IEEE::rate5Mbps | IEEE::rateIsBasic);
info->supportedRates.rate[3] = (IEEE::DataRate) (IEEE::rate11Mbps | IEEE::rateIsBasic);
info->supportedRates.rate[4] = (IEEE::DataRate) (IEEE::rate6Mbps | IEEE::rateIsBasic);
info->supportedRates.rate[5] = (IEEE::DataRate) (IEEE::rate9Mbps);
info->supportedRates.rate[6] = (IEEE::DataRate) (IEEE::rate12Mbps | IEEE::rateIsBasic);
info->supportedRates.rate[7] = (IEEE::DataRate) (IEEE::rate18Mbps);
info->supportedRates.rate[8] = (IEEE::DataRate) (IEEE::rate24Mbps | IEEE::rateIsBasic);
info->supportedRates.rate[9] = (IEEE::DataRate) (IEEE::rate36Mbps);
info->supportedRates.rate[10] = (IEEE::DataRate) (IEEE::rate48Mbps);
info->supportedRates.rate[11] = (IEEE::DataRate) (IEEE::rate54Mbps);
info->maxTxPower = 15; // FIXME: find and put proper value here
info->snrUnit = HardwareInfo::unit_dBm;
info->powerSavingModes = powerSaveAlwaysOn;
info->maxPacketSize = 2300;
info->txQueueSize = WPI_TX_RING_COUNT * 4;
info->capabilities.ShortSlot = true;
info->capabilities.ShortPreamble = true;
/* Read some EEPROM values for our own use */
readPromData(WPI_EEPROM_CAPABILITIES, &m_HWCap, 1);
readPromData(WPI_EEPROM_REVISION, &m_HWRev, 2);
readPromData(WPI_EEPROM_TYPE, &m_HWType,1);
}
IOReturn MyClass::getConfiguration
( HardwareConfigType type, void* param )
{
return kIOReturnUnsupported;
}
IOReturn MyClass::setConfiguration
( HardwareConfigType type, void* param )
{
return kIOReturnUnsupported;
}
IOReturn MyClass::outputFrame
( TxFrameHeader hdr, mbuf_t m )
{
TxRing* ring = &m_TxRing[0];
wpi_tx_desc* desc;
wpi_tx_cmd* cmd;
wpi_cmd_data* tx;
IEEE::TxDataFrameHeader* wh;
int i, nsegs, rate, hdrlen, ismcast;
/* Prevent sending data while hardware is busy or scan is in progress
if (m_Flags & WPI_FLAG_BUSY || m_Flags & WPI_FLAG_SCANNING) {
freePacket(m);
return kIOReturnOutputDropped;
}
*/
if (ring->queued > ring->count - 8) {
DBG(dbgWarning, "Packet dropped, queue full\n");
freePacket(m);
return kIOReturnOutputDropped;
}
wh = (IEEE::TxDataFrameHeader*) mbuf_data(m);
if (wh->hdr.type == IEEE::WiFiFrameHeader::ManagementFrame)
ring = &m_TxRing[0];
desc = &ring->descriptors[ring->current];
hdrlen = sizeof(IEEE::TxDataFrameHeader);
ismcast = wh->bssid[0] & 0x01;
cmd = &ring->cmdSlots[ring->current];
cmd->code = WPI_CMD_TX_DATA;
cmd->flags = 0;
cmd->qid = ring->qid;
cmd->idx = ring->current;
tx = (wpi_cmd_data*) cmd->data;
tx->flags = WPI_TX_AUTO_SEQ;
tx->timeout = 0;
tx->ofdm_mask = 0xff;
tx->cck_mask = 0x0f;
tx->lifetime = WPI_LIFETIME_INFINITE;
tx->id = ismcast ? WPI_ID_BROADCAST : WPI_ID_BSS;
tx->len = mbuf_len(m);
if (!ismcast) {
tx->flags |= (WPI_TX_NEED_ACK);
if (mbuf_len(m) > /* RTS threshold */ 2346) {
tx->flags |= (WPI_TX_NEED_RTS|WPI_TX_FULL_TXOP);
tx->rts_ntries = 7;
}
}
/* pick a rate */
if (wh->hdr.type == IEEE::WiFiFrameHeader::ManagementFrame) {
if (wh->hdr.subtype == IEEE::WiFiFrameHeader::AssocRequest ||
wh->hdr.subtype == IEEE::WiFiFrameHeader::ReassocRequest)
tx->timeout = 3;
else
tx->timeout = 2;
rate = IEEE::rate1Mbps;
} else if (ismcast) {
rate = IEEE::rate1Mbps;
} else {
ieee80211_amrr_choose(&amrr, &amrrNode, &m_TxRateIndex, &m_AssocParams.supportedRates);
rate = (0x7f & m_AssocParams.supportedRates.rate[m_TxRateIndex]);
DPRINTFN(WPI_DEBUG_TX, ("Choosing Tx rate %u Mbps (index %u)\n", rate/2, m_TxRateIndex));
}
tx->rate = plcpSignal(rate);
/* be very persistant at sending frames out */
tx->data_ntries = 15;
/* save and trim IEEE802.11 header */
mbuf_copydata(m, 0, sizeof(IEEE::TxDataFrameHeader), &tx->wh);
mbuf_adj(m, hdrlen);
/* Set the actual data addresses into hw tx desc */
IOPhysicalSegment segs[WPI_MAX_SCATTER-1];
nsegs = m_MbufCursor->getPhysicalSegmentsWithCoalesce(m, segs, WPI_MAX_SCATTER-1);
if (!nsegs) {
DBG(dbgWarning, "Could not get physical segments for mbuf. Dropping packet.\n");
if (m) freePacket(m);
return kIOReturnOutputDropped;
}
DPRINTFN(WPI_DEBUG_TX, ("sending data: qid=%d idx=%d len=%d nsegs=%d\n",
ring->qid, ring->current, mbuf_len(m), nsegs));
/* first scatter/gather segment is used by the tx data command */
desc->flags = (WPI_PAD32(mbuf_len(m)) << 28 | (1 + nsegs) << 24);